# \[SOLVED\] How can I create an animated sprite on 2D screen UI in a 3D application?

**URL:** <https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404>\
**Category:** Help & Support\
**Created:** [July 19, 2019, 8:31pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404 "2019-07-19T20:31:13Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mystik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mystik/32/5730_2.png) [@Mystik](https://forum.playcanvas.com/u/Mystik)\
**Post date:** [July 19, 2019, 8:31pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/1 "2019-07-19T20:31:13Z")

</div>

Hi,

I just want to have an animated image on 2D screen UI (in HUD) for a 3D application.  
I thought that I just need to add an animated GIF but it’s not supported.

So I have searched and found two ways to create an animated images.

1 - By using the animated sprite like this: [https://youtu.be/x4xMP-J7WlU](https://youtu.be/x4xMP-J7WlU)  
2 - By using an animated material like this: [https://developer.playcanvas.com/en/tutorials/animated-textures/](https://developer.playcanvas.com/en/tutorials/animated-textures/) (The title of this tutorial is misleading because it doesn’t animated the texture here but the material by changing his texture)

Both methods work well in the 3D space but not in 2D.

The first one uses an animated sprite, which can’t be child of a Screen element as specifed by @yaustar:

> Sprites weren’t supposed to be supported as children of the screen

Source from a similar question: [Display sprite animations on 2D screen - #2 by Mal\_Duffin](https://forum.playcanvas.com/t/display-sprite-animations-on-2d-screen/9456/2)

The second one uses a model of type plane, which still stay on 3D position even if it’s a child of a 2D screen object.

So, my goal is to have an animated texture, in the 2D space, on top of everything in a 3D application. Can you help me with that?

Thank you in advance.

---

<div class="post-metadata">

**Author:** ![Mystik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mystik/32/5730_2.png) [@Mystik](https://forum.playcanvas.com/u/Mystik)\
**Post date:** [July 19, 2019, 9:11pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/2 "2019-07-19T21:11:14Z")

</div>

I have found an hacky way to get this works. I will document this here but please, tell me if there’s a better solution for that.

So I managed this with theses steps:

1 - By creating an image element and set this as a child of a 2D screen element.

![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/b/bca5ea5184477ef093a4cd73114eec6da1151521.png)

2 - I have created an animated material like this tutoriel: [https://developer.playcanvas.com/en/tutorials/animated-textures/](https://developer.playcanvas.com/en/tutorials/animated-textures/)

3 - I applied this material to my image (instead of a model from the tutorial).  
 ![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/4/4a3b3be918433db793d6e16704fd3c16b336cb29.png)

4 - I modified the function _updateMaterial_ of `animated-texture.js` script from the tutoriel because it was working only on model with meshInstances. Here my modified version:

```javascript
AnimatedTexture.prototype.updateMaterial = function (frame) {
    // calculate how much to change UV to go to next frame
    var dx = 1 / this.width;
    var dy = 1 / this.height;

    // Convert frame number into UV co-ordinate
    var x = frame % this.width;
    var y = Math.floor(frame / this.width);

    // create the tiling vector (tilingx, tilingy, offsetx, offsety)
    var tiling = new pc.Vec2(dx, dy); 
    
    // create the offset vector (offsetx, offsety)
    var offset = new pc.Vec2(x * dx, (1 - dy) - (y * dy)); 

    //Override the material properties for this material
    this.entity.element.material.diffuseMapTiling = tiling;
    this.entity.element.material.diffuseMapOffset = offset;
    this.entity.element.material.emissiveMapTiling = tiling;
    this.entity.element.material.emissiveMapOffset = offset;
    this.entity.element.material.opacityMapTiling = tiling;
    this.entity.element.material.opacityMapOffset = offset;
    
    //Applies any changes made to the material's properties.
    this.entity.element.material.update();
    
};

```

So, it’s working but I hope there’s a better/easier way.

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [July 20, 2019, 7:49am UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/3 "2019-07-20T07:49:40Z")

</div>

> [@Mystik](#):
>
> So, it’s working but I hope there’s a better/easier way.

Not to my knowledge. I was thinking of using a sprite asset and advancing the frame by code or going through a series of sprite assets.

You can also use a MP4 and play the video in a texture.

---

<div class="post-metadata">

**Author:** ![Mal\_Duffin](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mal_duffin/32/6794_2.png) [@Mal\_Duffin](https://forum.playcanvas.com/u/Mal_Duffin)\
**Post date:** [July 20, 2019, 11:21am UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/4 "2019-07-20T11:21:40Z")

</div>

It would make sense for the developers to just add animated sprite support to the UI - this has been brought up a few times now by different customers.

---

<div class="post-metadata">

**Author:** ![Mystik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mystik/32/5730_2.png) [@Mystik](https://forum.playcanvas.com/u/Mystik)\
**Post date:** [July 22, 2019, 12:37pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/5 "2019-07-22T12:37:21Z")

</div>

I agree with @Mal_Duffin. Here, I managed to reach the result I was looking for but for a non-developper, it would have been difficult to find a solution.

---

<div class="post-metadata">

**Author:** ![Mystik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mystik/32/5730_2.png) [@Mystik](https://forum.playcanvas.com/u/Mystik)\
**Post date:** [July 31, 2019, 9:33pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/6 "2019-07-31T21:33:27Z")

</div>

Sadly, I still got a problem with my solution. The material is influenced by the Skybox and the Ambiant color of my scene. So, my animated images are not rendered with their truly colors and are impacted by the ligths… 😥

I still hope that one day, I will be able to drag and drop an animated gif as an image that is unlit and set that on the 2D screen space. 🙏

---

<div class="post-metadata">

**Author:** ![Mystik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/mystik/32/5730_2.png) [@Mystik](https://forum.playcanvas.com/u/Mystik)\
**Post date:** [July 31, 2019, 10:08pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/7 "2019-07-31T22:08:24Z")

</div>

Ok, here another technique that fixed my problem described in my last post. The technique uses the sprite element on 2D screen with a script.

0 - First, you need to have an animated sprite. This tutorial explained how to do that: [https://youtu.be/x4xMP-J7WlU](https://youtu.be/x4xMP-J7WlU)

1 - Create an image element and set this as a child of a 2D screen element.

![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/b/bca5ea5184477ef093a4cd73114eec6da1151521.png)

2 - This image element are like this:

 ![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/a/adbf0e4c0d660cb18a603cecc2bfaa65728b7ad4.png)

3 - Here the script I made and put on that image element. You can controle the start frame, the number of frames and the FPS.

```javascript
var AnimateSprite = pc.createScript('animateSprite');

AnimateSprite.attributes.add('startFrame', {
    type: 'number',
    default: 0,
    description: 'Frame to start animation from'
});

AnimateSprite.attributes.add('numFrames', {
    type: 'number',
    default: 1,
    description: 'Number of frames to play before looping'
});

AnimateSprite.attributes.add('frameRate', {
    type: 'number',
    default: 1,
    description: 'Playback frames per second'
});

// initialize code called once per entity
AnimateSprite.prototype.initialize = function() {
    this.timer = 1/this.frameRate;
    this.frame = this.startFrame;
};

// update code called every frame
AnimateSprite.prototype.update = function(dt) {
            
    // calculate when to animate to next frame
    this.timer -= dt;            
    if (this.timer < 0) {
        // move to next frame
        this.frame++;
        if (this.frame >= (this.numFrames + this.startFrame)) {
            this.frame = this.startFrame;
        }

        this.entity.element.spriteFrame = this.frame;
        
         // reset the timer
        this.timer = 1/this.frameRate;
    }
};

```

So, it’s working well even if:

@yaustar:

> Sprites weren’t supposed to be supported as children of the screen

---

<div class="post-metadata">

**Author:** ![bjorn.syse](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/bjorn.syse/32/31022_2.png) [@bjorn.syse](https://forum.playcanvas.com/u/bjorn.syse)\
**Post date:** [September 17, 2020, 1:49pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/8 "2020-09-17T13:49:43Z")

</div>

I get these warnings in the log when using this technique:

![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/4/454c8370ee252cd8efa05392eb2cb88f89a3a2d9.png)

Any problems with that?

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [September 17, 2020, 1:50pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/9 "2020-09-17T13:50:52Z")

</div>

What’s the code?

---

<div class="post-metadata">

**Author:** ![Leonidas](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/leonidas/32/6495_2.png) [@Leonidas](https://forum.playcanvas.com/u/Leonidas)\
**Post date:** [September 17, 2020, 2:01pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/10 "2020-09-17T14:01:12Z")

</div>

Any instance of pc.Vec3/Vec4/Color that is accessed through its .data array has been made a private api.

You will get this in many older shader examples which use the .data property to pass the array to shader uniforms (quite handy I have to say).

Now you will have to access the object values through their respected properties only:

```javascript
this.vec.x;
this.vec.y;
this.color.r;
// etc

```

---

<div class="post-metadata">

**Author:** ![DerpySlurrpe](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/derpyslurrpe/32/9560_2.png) [@DerpySlurrpe](https://forum.playcanvas.com/u/DerpySlurrpe)\
**Post date:** [September 17, 2020, 5:54pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/11 "2020-09-17T17:54:22Z")

</div>

will this help A [

pc.SpriteComponent]([https://developer.playcanvas.com/en/api/pc.SpriteComponent.html](https://developer.playcanvas.com/en/api/pc.SpriteComponent.html)) that renders sprite animations.

---

<div class="post-metadata">

**Author:** ![bjorn.syse](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/bjorn.syse/32/31022_2.png) [@bjorn.syse](https://forum.playcanvas.com/u/bjorn.syse)\
**Post date:** [September 22, 2020, 8:25am UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/12 "2020-09-22T08:25:33Z")

</div>

Yes, I was going through the sprite component aswell and I think it does what I need. Perhaps this thread was written before the animated sprite system was in place?

The only thing this code above with `animated-texture.js` has which I haven’t found in an animated sprite is the possibility to further adjust the material properties of the plane. For example I can use the emissive-channel on a standard material (icon on the right side),where as a sprite component (left icon) only presents a color to use. Is that right?

 ![2020-09-22_10-25-59](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/e/e698bf97eeba73b5f5f6e32dd04fdea137ebfdc1.gif)  
 ![image](https://forum-files-playcanvas-com.s3.dualstack.eu-west-1.amazonaws.com/original/2X/3/3a5315e5da7093764b8cce37662bb460509c52cf.png)

---

<div class="post-metadata">

**Author:** ![bjorn.syse](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/bjorn.syse/32/31022_2.png) [@bjorn.syse](https://forum.playcanvas.com/u/bjorn.syse)\
**Post date:** [September 22, 2020, 8:29am UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/13 "2020-09-22T08:29:32Z")

</div>

> [@yaustar](#):
>
> What’s the code?

It’s this code here: [https://developer.playcanvas.com/en/tutorials/animated-textures/](https://developer.playcanvas.com/en/tutorials/animated-textures/)

```javascript
    // This allows us to use different settings for different Entities, but share the same material
    this.transform.set(dx, dy, x * dx, (1 - dy) - (y * dy));
    meshes[0].setParameter("texture_diffuseMapTransform", this.transform.data);
    meshes[0].setParameter("texture_emissiveMapTransform", this.transform.data);
    meshes[0].setParameter("texture_opacityMapTransform", this.transform.data);
};

```

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [September 22, 2020, 8:48am UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/14 "2020-09-22T08:48:49Z")

</div>

Ah yes, that project needs to be updated The `.data` API no longer exists so you would have to copy the X, Y, Z, W values into your own float array and pass that instead to get rid of the warning.

You can see it being done here: [https://github.com/playcanvas/engine/blob/master/src/deprecated.js#L278](https://github.com/playcanvas/engine/blob/master/src/deprecated.js#L278)

---

<div class="post-metadata">

**Author:** ![bjorn.syse](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/bjorn.syse/32/31022_2.png) [@bjorn.syse](https://forum.playcanvas.com/u/bjorn.syse)\
**Post date:** [September 22, 2020, 9:08am UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/15 "2020-09-22T09:08:33Z")

</div>

I see, thanks!

But would you also say the strategy is outdated aswell? Should I just use an animated sprite?

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [September 22, 2020, 9:56am UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/16 "2020-09-22T09:56:12Z")

</div>

> [@bjorn.syse](#):
>
> But would you also say the strategy is outdated aswell? Should I just use an animated sprite?

Either is fine to be honest. Whatever you are more comfortable with in this case.

---

<div class="post-metadata">

**Author:** ![DerpySlurrpe](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/derpyslurrpe/32/9560_2.png) [@DerpySlurrpe](https://forum.playcanvas.com/u/DerpySlurrpe)\
**Post date:** [September 22, 2020, 12:38pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/17 "2020-09-22T12:38:59Z")

</div>

then how did i find it if it didnt exist?

---

<div class="post-metadata">

**Author:** ![yaustar](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/yaustar/32/2268_2.png) [@yaustar](https://forum.playcanvas.com/u/yaustar)\
**Post date:** [September 22, 2020, 12:53pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/18 "2020-09-22T12:53:01Z")

</div>

Erm, what do you mean by ‘how did I find it’?

---

<div class="post-metadata">

**Author:** ![LeXXik](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/lexxik/32/6953_2.png) [@LeXXik](https://forum.playcanvas.com/u/LeXXik)\
**Post date:** [September 22, 2020, 8:16pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/19 "2020-09-22T20:16:05Z")

</div>

@bjorn.syse perhaps this could be of use:

> [@\[SOLVED\] Vector data usage warning](https://forum.playcanvas.com/t/solved-vector-data-usage-warning/14335):
>
> Hello, I would like to use the vector’s Float32Array directly (e.g. pc.Vec3().data). Is there a way to do it without the pesky console warning?

---

<div class="post-metadata">

**Author:** ![KpoKec](https://forum.playcanvas.com/user_avatar/forum.playcanvas.com/kpokec/32/8260_2.png) [@KpoKec](https://forum.playcanvas.com/u/KpoKec)\
**Post date:** [September 22, 2020, 10:38pm UTC](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404/20 "2020-09-22T22:38:03Z")

</div>

Why not use sprite & spriteFrame of Image element for animation? That’s more easy, than animate material.

[Next page](https://forum.playcanvas.com/t/solved-how-can-i-create-an-animated-sprite-on-2d-screen-ui-in-a-3d-application/10404.md?page=2)
