[SOLVED] Changing texture coordinates for one entity when using shared material?

I have many entities, sharing the same material with a diffuse texture. Now I want to scale just some of the entities without stretching the texture (I want it to repeat).

Question: Can I scale some of the entities without stretching the texture on any of the entities? I suppose I need to scale the texture coordinates on my modified entities:

this.entity.model.model.getMaterials()[0].diffuseMapTiling = new pc.Vec2(…

The problem is that I also scale the texture on the other entities. How can I solve this?

I guess I could write a custom shader that uses world coordinates for texturing, Is there a built in function for this already? Or is there a way to change texture coordinates for an entity (model?), rather than a material?

Yes, it’s possible, you can override shader constants per-object:

var transform = new pc.Vec4(scaleX, scaleY, offsetX, offsetY);
var meshInstances = this.entity.model.model.meshInstances;
for(var i=0; i<meshInstances.length; i++) {
 meshInstances[i].setParameter("texture_diffuseMapTransform", transform.data);
}

The original material has to have some non-default scale or offset for this method to work, as it doesn’t force shader recompilation, and if material’s scale is one and offset is zero, then its shader just won’t have the transformation code (as an optimization).

So if I put (1, 1, 0, 0) for the four transform parameters above it should work? I tried but the material still stretches with the geometry.

Little example: https://playcanvas.com/project/388529/overview/tile

1 Like

Thanks. I will take a look.