yanjinyun
yanjinyun

Reputation: 23

the difference between uv and position in threejs glsl?

vec2 uv = vec2(position.x / 360.0 + 0.5, position.y / 180.0 + 0.5);

when use uv and when use position? And Can they be converted between them?

Upvotes: 0

Views: 5042

Answers (1)

manthrax
manthrax

Reputation: 5036

UV and position are points in two different coordinate systems.

UV coordinates are also called Texture coordinates.

Texture coordinates can be any 2d coordinate and are usually created/defined in your modelling software, or in the code you use to generate your 3d models.

For each point (vertex) in the model there are between 0 and 2 sets of texture coordinates. These sets are often called "UV Channels"

So... pretty much all models have vertices. but not all models have texture coordinates, so there isn't a guaranteed way to convert between them, because they dont both always exist. :) If they do exist and are both set to something predictable, then you can sometimes convert between them, but that is of limited usefulness, since there are usually easier ways to accomplish finding the point associated with a UV.

That said, there are times where the UV coordinate is useful in terms of figuring out which part of your model you are rendering. For instance in a grass shader... You may have lots of little squares represented clumps of grass, spread out over your world... the Positions of all these clumps vertices will be all over the place, but the UVs will be in the range of 0 to 1 usually and are the coordinates of the texel on the grass texture. So if you only want to morph the top of the blades of grass, you can check if the UV.v is < 0.5, and know that you are dealing with a vertex that is part of the top of a clump.

Anyway this is kindof a big subject, so here is a tutorial on it... check it out!

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/

Upvotes: 6

Related Questions