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

What's the best way to apply texture under a given object via code?

Discussion in 'World Building' started by antiflasher, Jan 21, 2021.

  1. antiflasher

    antiflasher

    Joined:
    Apr 9, 2020
    Posts:
    8
    I've been googled for hours and haven't found a solid solution to this problem.

    See the attach. I have an arbitrary drawn polygonal object on the top of a curvy terrain (arbitrary = polygon's vertices doesn't match to terrain's mesh vertices, and the size of the polygon is not rectangular). Now I want to apply a texture within this polygon on the terrain surface. How to do that?

    Options I've read about:
    1. Create a mash with tiny y-offset. Terrain is curvy, so I need to follow the terrain heights very precisely. Doesn't seem a right direction to my.
    2. Use terrain's alphamap to draw a texture. Alphamap draws textures using heightmap resolution, thus I'll get a square-ish shaped patch, while I want the texture exactly following the polygon shape.
    3. Use a Projector. That doesn't seem working at all. Projector creates a shadow or light, it doesn't project a full color texture.
    4. Use Decal shader. This shader returns a strange error
    NullReferenceException: Object reference not set to an instance of an object
    UnityEngine.Rendering.HighDefinition.DecalSystem+DecalSet.InitializeMaterialValues () (at Library/PackageCache/com.unity.render-pipelines.high-definition@7.3.1/Runtime/Material/Decal/DecalSystem.cs:373)...
    Plus it's sounds a little odd having a projector on each polygon (there'll be many of them in the game).

    I imagine a shader that's renders given texture in intersection of the polygon and the terrain. But I failed googling any examples of such shader. Any suggestions? Or maybe the pest solution is something else? Screenshot 2021-01-21 at 13.07.08.png
     
  2. crysicle

    crysicle

    Joined:
    Oct 24, 2018
    Posts:
    95
    Try stencil buffers.