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

Question strange artifacts with custom lit shader in hdrp (using uvs to sample certain colors)

Discussion in 'Shader Graph' started by Dvantus, May 20, 2022.

  1. Dvantus

    Dvantus

    Joined:
    Jun 1, 2020
    Posts:
    1
    Hello!
    I am following an Youtube tutorial from Sebastian Lague about procedural planet generation. In episode 5 & 6 he creates a custom shader to color the planet terrain. He uses a PBR in the light weight renderpipeline from Unity 2018, but I am using Unity 2020 for this Project, so I did my best to follow his steps in a Lit Shader of the hdrp. The problem occured after the step from 8:54 to 10:55 in episode 6 (
    ).

    THE PROBLEM: When I assign multiple biomes like in the tutorial, the planet gets strange artifacts. The artifacts only occur on faces, that are steep in relation to the camera. The artifacts have the color of the adjacent biome.
    highResProblem.PNG highResProblemClose.PNG LowResProblemClose.PNG

    OVERVIEW: In code a Texture2D is generated, that has fixed width of 50 and height corresponding to the number of biomes. In the width the texture samples an gradient for the color corresponding to the elevation and in the height the different biomes are stored. In code for each vertex the y value of the uv is set to the corresponding value of the biome. The x value is set to a percent value between the min elevation and the max elevation of the planet.

    SHADER GRAPH:
    ShaderGraph.PNG

    CODE:
    Set the uv array:
    Code (CSharp):
    1. public void UpdateUVs(ColourGenerator colourGenerator)
    2.     {
    3.         Vector2[] uv = new Vector2[resolutionX * resolutionY];
    4.  
    5.         for (int y = 0; y < resolutionY; y++)
    6.         {
    7.             for (int x = 0; x < resolutionX; x++)
    8.             {
    9.                 int i = x + (y * resolutionX);
    10.  
    11.                 float percentX = (float)x / (float)(resolutionX - 1);
    12.                 float percentY = (float)y / (float)(resolutionY - 1);
    13.  
    14.                 Vector3 pointOnUnitCube = origin + ((percentX) * axisX) + ((percentY) * axisY);
    15.                 Vector3 pointOnUnitSphere = pointOnUnitCube.normalized;
    16.  
    17.                 uv[i] = new Vector2(colourGenerator.BiomePercentFromPoint(pointOnUnitSphere), 0f);
    18.             }
    19.         }
    20.  
    21.         mesh.uv = uv;
    22.     }
    get the biome values for the uv array:
    Code (CSharp):
    1. public float BiomePercentFromPoint(Vector3 pointOnUnitSphereIN)
    2.     {
    3.         float heightPercent = (pointOnUnitSphereIN.y + 1f) / 2f;
    4.         float biomeIndex = 0;
    5.         int numBiomes = colourSettings.biomeSettings.biomes.Length;
    6.  
    7.         for(int i = 0; i < numBiomes; i++)
    8.         {
    9.             if(colourSettings.biomeSettings.biomes[i].startHeight < heightPercent)
    10.             {
    11.                 biomeIndex = i;
    12.             }
    13.             else
    14.             {
    15.                 break;
    16.             }
    17.         }
    18.  
    19.         return biomeIndex / Mathf.Max(1f, numBiomes - 1f);
    20.     }
    create the texture:
    Code (CSharp):
    1. public void UpdateColours()
    2.     {
    3.         Color[] colours = new Color[texture.width * texture.height];
    4.  
    5.         int colourIndex = 0;
    6.  
    7.         foreach (var biome in colourSettings.biomeSettings.biomes)
    8.         {
    9.             for (int i = 0; i < textureResolution; i++)
    10.             {
    11.                 Color gradientColour = biome.gradient.Evaluate(i / (textureResolution - 1f));
    12.                 Color tintColour = biome.tint;
    13.                 colours[colourIndex] = (gradientColour * (1 - biome.tintPercent)) + (tintColour * biome.tintPercent);
    14.                 colourIndex++;
    15.             }
    16.         }
    17.  
    18.  
    19.  
    20.         texture.SetPixels(colours);
    21.         texture.Apply();
    22.         colourSettings.planetMaterial.SetTexture("_planetTexture", texture);
    23.  
    24.     }
    ATTEMPTS TO FIX IT:
    1. I have changed the Sample State for the Sample Texture node, but it did not help.
    2. I have disconnected the x value of the uv for the Sample Texture node and the artifacts vanished, but also the different colors for the elevation:
      ShaderGraphWithOutHight.PNG
    3. I have also played with the settings of the shader graph, but that did not help either.