Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying a texture on a mesh at given world coordinate

Tags:

c++

directx-11

Im making an editor in which I want to build a terrain map. I want to use the mouse to increase/decrease terrain altitude to create mountains and lakes.

Technically I have a heightmap I want to modify at a certain texcoord that I pick out with my mouse. To do this I first go from screen coordinates to world position - I have done that. The next step, going from world position to picking the right texture coordinate puzzles me though. How do I do that?

like image 532
KaiserJohaan Avatar asked Feb 12 '17 13:02

KaiserJohaan


People also ask

What are the texture coordinates of mesh vertices?

Texture coordinates, also called UVs, are pairs of numbers stored in the vertices of a mesh. These numbers are often used to stretch a 2D texture onto a 3D mesh, but they can be used for other things like coloring the mesh (see Vertex color), controlling the flow across the surface (see Flow map), etc.

How do you define texture coordinates?

Texture coordinates define how an image (or portion of an image) gets mapped to a geometry. A texture coordinate is associated with each vertex on the geometry, and it indicates what point within the texture image should be mapped to that vertex.

How do you add a mapping and texture to a coordinate node?

Open up the Shader Editor. Press Shift+A, then hover over Input and select “Texture Coordinate”. Press Shift+A again, then hover over Vector and select “Mapping”. Connect the relevant “Texture Coordinate” output (usually “Generated” or “UV”) to the top “Vector” input on the “Mapping” node.


2 Answers

If you are using a simple hightmap, that you use as a displacement map in lets say the y direction. The base mesh lays in the xz plain (y=0).

You can discard the y coordinate from world coordinate that you have calculated and you get the point on the base mesh. From there you can map it to texture space the way, you map your texture.

I would not implement it that way. I would render the scene to a framebuffer and instead of rendering a texture the the mesh, colorcode the texture coordinate onto the mesh. If i click somewhere in screen space, i can simple read the pixel value from the framebuffer and get the texture coordinate directly. The rendering to the framebuffer should be very inexpensive anyway.

like image 85
OutOfBound Avatar answered Oct 23 '22 09:10

OutOfBound


Assuming your terrain is a simple rectangle you first calculate the vector between the mouse world position and the origin of your terrain. (The vertex of your terrain quad where the top left corner of your height map is mapped to). E.g. mouse (50,25) - origin(-100,-100) = (150,125).
Now divide the x and y coordinates by the world space width and height of your terrain quad.
150 / 200 = 0.75 and 125 / 200 = 0.625. This gives you the texture coordinates, if you need them as pixel coordinates instead simply multiply with the size of your texture.

like image 27
Mormund Avatar answered Oct 23 '22 11:10

Mormund