简体   繁体   中英

Modifying a texture on a mesh at given world coordinate

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?

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.

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). Eg 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.

I assume the following:

  1. The world coordinates you computed are those of the mouse pointer within the view frustrum. I name them mouseCoord
  2. We also have the camera coordinates, camCoord
  3. The world consists of triangles
  4. Each triangle point has texture coordiantes, those are interpolated by barycentric coordinates

If so, the solution goes like this:

  1. use camCoord as origin. Compute the direction of a ray as mouseCoord - camCoord.
  2. Compute the point of intersection with a triangle. Naive variant is to check for every triangle if it is intersected, more sophisticated would be to rule out several triangles first by some other algorithm, like parting the world in cubes, trace the ray along the cubes and only look at the triangles that have overlappings with the cube. Intersection with a triangle can be computed like on this website: http://www.lighthouse3d.com/tutorials/maths/ray-triangle-intersection/
  3. Compute the intersection points barycentric coordinates with respect to that triangle, like that: https://www.scratchapixel.com/lessons/3d-basic-rendering/ray-tracing-rendering-a-triangle/barycentric-coordinates
  4. Use the barycentric coordinates as weights for the texture coordinates of the corresponding triangle points. The result are the texture coordinates of the intersection point, aka what you want.

If I misunderstood what you wanted, please edit your question with additional information.

Another variant specific for a height map:

Assumed that the assumptions are changed like that:

  1. The world has ground tiles over x and y
  2. The ground tiles have height values in their corners
  3. For a point within the tile, the height value is interpolated somehow, like by bilinear interpolation.
  4. The texture is interpolated in the same way, again with given texture coordinates for the corners

A feasible algorithm for that (approximative):

  1. Again, compute origin and direction.
  2. Wlog, we assume that the direction has a higher change in x-direction. If not, exchange x and y in the algorithm.
  3. Trace the ray in a given step length for x, that is, in each step, the x-coordinate changes by that step length. (take the direction, multiply it with step size divided by it's x value, add that new direction to the current position starting at the origin)
  4. For your current coordinate, check whether it's z value is below the current height (aka has just collided with the ground)
  5. If so, either finish or decrease step size and do a finer search in that vicinity, going backwards until you are above the height again, then maybe go forwards in even finer steps again et cetera. The result are the current x and y coordinates
  6. Compute the relative position of your x and y coordinates within the current tile. Use that for weights for the corner texture coordinates.

This algorithm can theoretically jump over very thin tops. Choose a small enough step size to counter that. I cannot give an exact algorithm without knowing what type of interpolation the height map uses. Might be not the worst idea to create triangles anyway, out of bilinear interpolated coordinates maybe? In any case, the algorithm is good to find the tile in which it collides.

Another variant would be to trace the ray over the points at which it's xy-coordinates cross the tile grid and then look if the z coordinate went below the height map. Then we know that it collides in this tile. This could produce a false negative if the height can be bigger inside the tile than at it's edges, as certain forms of interpolation can produce, especially those that consider the neighbour tiles. Works just fine with bilinear interpolation, though.

In bilinear interpolation, the exact intersection can be found like that: Take the two (x,y) coordinates at which the grid is crossed by the ray. Compute the height of those to retrieve two (x,y,z) coordinates. Create a line out of them. Compute the intersection of that line with the ray. The intersection of those is that of the intersection with the tile's height map.

Simplest way is to render the mesh as a pre-pass with the uvs as the colour. No screen to world needed. The uv is the value at the mouse position. Just be careful though with mips/filtering etv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM