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.
3 - I applied this material to my image (instead of a model from the tutorial).
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:
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.
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.
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.
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.
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?
// 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);
};
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.