How to render same texture multiple times with different positions on a material?

Hi, after dealing with How to render a texture only once on the material? - #8 by Ertugrul_Cetin I got another issue.

I have two materials and blending these two. One is for the ground (terrain/map) and applied 3 ground textures on it. And the other one is showing some magic textures.

I’d like to render this magic texture multiple times on the material. For example, it will be rendered in the position [10, 10] for character #1, and the other magic texture will be rendered in the position [20, 20] for character #2.

diffuseMapOffset can’t help here because I need to render multiple times the same texture in different locations. diffuseMapOffset only helps if you need one texture, if I go with this path I need to create multiple materials for multiple textures so it’s not ideal IMHO.

So any suggestions would be great!

My current shader;

uniform sampler2D texture_splat;
vec2 character_position;
vec2 my_cord;

void getAlbedo() {

    my_cord = $UV;

    vec4 texture = texture2DSRGB(texture_splat, $UV * vec2(8,8));

    dAlbedo = mix(dAlbedo, texture.rgb,  texture.a);
}

Here is the image, the texture is only rendered once - location [0,0]

One option is to use clustered spot lights with cookie texture, similar to
https://playcanvas.github.io/#/graphics/clustered-spot-shadows

If your terrain is just a flat plane, you could do this
https://playcanvas.github.io/#/graphics/mesh-decals

You could also clone the terrain many times, and use simple shader on it with emissive texture, and use texture transform to move your texture to where you need to.

Or you could extend your current shader that projects a single texture, to accept lets say 5 positions, and project it 5 times.

1 Like

One option is to use clustered spot lights with cookie texture, similar to

I know this, but for performance reasons, I can’t use it. My terrain is not flat, I’m using heightmap - just shared this image for a simple example.

Or you could extend your current shader that projects a single texture, to accept lets say 5 positions, and project it 5 times.

Could it be problematic (performance-wise) if I need to increase size 5 from to for example 70?

Due to my limited knowledge with shaders, I don’t know how - maybe you can point me some directions to achieve this?

yes, clustered lights would perform better in this case for sure

you currently have a uniform for playerPosition. Have 5 of those uniforms.
And clone the code you have to use that uniform.
But this will be hard to extend to 70 … the cost would be crazy high.

If you need 70 or so, clustered lights sound like a great option.

The other way could be to have dynamic mesh for each projected image - it should only contain few triangles where needed, instead of whole terrain.

2 Likes

Thank you for the help, let me check those!