Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

how to apply texture to terrain in realtime

Discussion in 'General Graphics' started by Trild123787898, Apr 1, 2019.

  1. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    How can I apply a texture with an object as shown in the photo in RealTime, I can not figure out how to do this!
     

    Attached Files:

    • 0000.png
      0000.png
      File size:
      1.4 MB
      Views:
      1,039
  2. Sh-Shahrabi

    Sh-Shahrabi

    Joined:
    Sep 28, 2018
    Posts:
    56
    No idea how it is done in the screen shot, but the typical way to do this is having two textures which you sample and blend between based on some condition. To simplify things, let's assume an orthogonal projection on the plane. You have grass texture which you sample like this : tex2D(_GroundTextureGrass, i.worldPos.xz) and a dirt texture which you sample in the same way. Now the simplest way to blend between them is through something like vertex color. For example:
    return lerp(tex2D(_GroundTextureGrass, i.worldPos.xz),tex2D(_GroundTextureDirt, i.worldPos.xz), i.vertCol.x);

    Now you could technically color the mesh in real time in the CPU by changing the vertex color. This is, to be honest not a good option. What people mostly do is to use a render texture:

    You have a camera from above (for the sake of simplicity) which renders just the cube that is suppose to color the area (this could be the player for example). The result of the camera render is blended on an already existing render texture in an additive way. In this way as your player moves around, it leaves a trace behind. Then you need to reproject this texture on your geometry, sample it, and use it to determine which texture to use. To do the reprojection, the easiest and most expensive way is to convert the fragments of the plane from world position, to screen space of the camera which was rendering the player from above. You need to normalize the x, y value to 0 to 1. and use that as a UV to sample the render texture.

    The above method is not trivial to do, to make sure your character doesn't always change the color of the surface, but only what it is touching it, you need to implement a GPU collision detection of sort, and all in all it is a pain in a butt to maintain. So think about if you really need this real time painting in your game.
     
  3. Trild123787898

    Trild123787898

    Joined:
    Dec 9, 2018
    Posts:
    206
    Thank you for a very big answer =). Honestly, I create a farm simulator! I have fields on the tire, that is, a square of a different texture (dirt) I thought if my equipment would touch the tire, it would paint a different texture within the field! It is possible that this subsoil is complex, I just don’t know how else to implement this!
     
  4. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,492
    You don't have to use a camera though, if can draw directly to the texture, you convert the player position to the blending texture space and stamp a brush of the shape you want. Which mean you would have to search how to draw on a texture at minimal cost.
     
  5. Sh-Shahrabi

    Sh-Shahrabi

    Joined:
    Sep 28, 2018
    Posts:
    56
    Mauri likes this.
  6. neoshaman

    neoshaman

    Joined:
    Feb 11, 2011
    Posts:
    6,492
    Yeah I was saying that as a complement to point at a different way, since you covered the simple part :D
     
    Sh-Shahrabi likes this.