I’m trying to do uv offset animations at runtime. My script looks like this:
import { Script } from 'playcanvas';
/** @enum {string} */
const TextureMap = {
DIFFUSE: 'diffuseMap',
EMISSIVE: 'emissiveMap',
OPACITY: 'opacityMap'
};
export class UvOffsetAnimation extends Script {
static scriptName = 'uvOffsetAnimation';
/**
* @attribute
* @type {TextureMap}
*/
map = TextureMap.EMISSIVE;
/**
* @attribute
*/
uSpeed = 1.0;
/**
* @attribute
*/
vSpeed = 1.0;
uTime = 0;
vTime = 0;
initialize() {
// Need to set map offset to some other value than 0. Else texture map transforms wont be defined in vertex shader
this.entity.render.material[`${this.map}Offset`].set(1.0, 1.0);
}
/**
* Called for enabled (running state) scripts on each tick.
*
* @param {number} dt - The delta time in seconds since the last frame.
*/
update(dt) {
this.uTime += dt * this.uSpeed;
this.vTime += dt * this.vSpeed;
this.uTime %= 1.0;
this.vTime %= 1.0;
this.entity.render.meshInstances.forEach((meshInstance) => {
meshInstance.setParameter(`texture_${this.map}Transform0`, [1.0, 0.0, this.uTime]);
meshInstance.setParameter(`texture_${this.map}Transform1`, [0.0, 1.0, this.vTime]);
});
}
}
This will work as long as I only animate 1 texture per material. However when using the same material on multiple entities and wanting to animate diffuse on one entity and emissive on the other this breaks and only the diffuse entity is animated.
Inspecting with spectorjs shows the texture_emissiveMapTransform is missing in the vertex shader. Why?
Check this example:
https://playcanvas.com/project/1429322/overview/uvoffsetanimation
PS: I know I can use emissiveMapOffset API and similar, however I’d like to only have one material with different uv animation offsets per entity/meshinstance.