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

Edit Time Add Sprite to Terrain as Decal

Discussion in 'World Building' started by WillG5677, Jun 27, 2019.

  1. WillG5677

    WillG5677

    Joined:
    Jun 30, 2016
    Posts:
    3
    At edit time I am attempting to map a sprite onto a section of a Unity Terrain object to produce a decal effect.
    My attempt that has gotten the closest (doesn't produce any errors) shows no visible change, though. I am using the TerrainPaintUtility PaintTexture functions and blitting the sprite's texture to the PaintContext's DestinationRenderTexture:
    Code (CSharp):
    1. PaintContext paintContext = TerrainPaintUtility.BeginPaintTexture(combinedTerrainObj, new Rect(0, 0, 1, 1), combinedTerrainObj.terrainData.terrainLayers[0]);
    2.  
    3. SpriteRenderer spriteRenderer = FindObjectOfType<SpriteRenderer>();
    4. if (spriteRenderer != null)
    5. {
    6.     Graphics.Blit(spriteRenderer.sprite.texture, paintContext.destinationRenderTexture);
    7. }
    8.  
    9. TerrainPaintUtility.EndPaintTexture(paintContext, "Undo terrain paint");
    At this stage I am just trying to get the sprite to appear pasted on the terrain without worrying about size and position. The new Rect(0, 0, 1, 1) is an arbitrary guess, but I've tried with larger rects as well, still with no visual change.
    How can I map a sprite to a portion of a Unity terrain? Am I on the right track?
     
  2. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    The terrain itself does not have a single texture you can blit a decal on.
    It has an internal splatmap that holds the density of the the terrain textures at each point.
    The combined blend interpolated between these points creates the final texture you see on the terrain.
    This is how a huge terrain can have detailed textures at any point. You are not limited to a 4k texture of the entire terrain
     
  3. WillG5677

    WillG5677

    Joined:
    Jun 30, 2016
    Posts:
    3
    I think I understand. So then would I need a terrain layer for each sprite I plan to use as a decal and then use TerrainData.SetAlphamaps to make the blend weight 1 for the sprite's layer wherever I want it to appear as a decal?
     
  4. LennartJohansen

    LennartJohansen

    Joined:
    Dec 1, 2014
    Posts:
    2,394
    You will get no detailed control on where it ends up. It would probably be better to use some kind of deferred decal system to project them