How to transform character's world position to UV coordinate?

Hi, I have a character’s world position - let’s say [10, 0, 20] and my terrain has a material that I’m rendering some textures on it. I’m using shaders.

I can position my texture on the ground with this e.g.: texture2DSRGB(texture_splat, ($UV - vec2(2))) but the problem is world position and $UV coordinate can’t talk to each other (they are different system).

So I’d like to render my texture where my character at, I need to transform the character’s world position to UV coordinate. I have to update this part somehow ($UV - vec2(2))

How can I do it?

Hi @Ertugrul_Cetin,

You just need to generate a math expression that transforms world positions to UV space.

You will have to pass the terrain’s width and depth (terrain bounds) to the shader, together with the terrain’s world position (usually that would be the mesh center).

So the terrain world center would be at 0.5, 0.5 in UV coordinates. Any other position can be similarly converted to UV space using that equation.

// pseudo code for a 2D world point (x and z axis)
vec2 size = bounds.max - bounds.min;
vec2 uvpoint = (worldPoint - bounds.min) / size;

Hi @Leonidas, thank you for spending time and helping me! I’m a bit lost sorry. My terrain is 100x100 [width x depth].

together with the terrain’s world position…

Is it hard coded [0.5, 0.5]? If not, how to access it?

I can pass terrain’s width and depth as uniforms but did not understand what to do afterward.

bounds.max - bounds.min;.

What are bounds.max and bounds.min? bounds.max = terrain’s width/depth? How to access them?

Once I got the uvpoint how to use it, like this;?

texture2DSRGB(texture_splat, uvpoint)
1 Like

I guess found it, thank you @Leonidas!

Here is my code for others who might need it;


 // terrain size is hard-coded for now...
 vec2 size = vec2(10);
 vec2 uvpoint = character_position / size;

// vec2(0.44) is a hard-coded value for centering texture for now...
// * vec2(8), making the texture look smaller on terrain 
vec4 texture = texture2DSRGB(texture_splat, ($UV - uvpoint - vec2(0.44))  * vec2(8));
1 Like