unity-game-enginetexturesurpshader-graphdecal

Unity URP set decal Base Map via script


private void GenerateBrushTexture()
{
    int textureSize = Mathf.CeilToInt(radius * 2);
    brushTexture = new Texture2D(textureSize, textureSize);

    for (int x = 0; x < textureSize; x++)
    {
        for (int y = 0; y < textureSize; y++)
        {
            // Calculate the distance from the center of the texture
            float distance = Vector2.Distance(new Vector2(x, y), new Vector2(textureSize / 2, textureSize / 2));

            // Normalize the distance
            float normalizedDistance = Mathf.Clamp01(distance / radius);

            // Calculate the falloff using a power function to smooth the edges
            float falloffValue = Mathf.Pow(1 - normalizedDistance, falloff);

            // Set the pixel color based on the falloff value
            brushTexture.SetPixel(x, y, new Color(falloffValue, falloffValue, falloffValue, falloffValue));
        }
    }
    Debug.Log("set texture !");
//UPDATE : I forgot          brushTexture.Apply();
//So there it is :
    brushTexture.Apply();
    brushMaterial.SetTexture("_BaseMap", brushTexture);
}

does not do anything, while I would have expected the decal to update its texture.

I tried brushMaterial.SetTexture("Base Map", brushTexture); and brushMaterial.SetTexture("_Base Map", brushTexture); , but it does not work either. I tried looking at the names in the shader graph editor, but there was nothing in graph inspector -> node settings.

UPDATE : in the shader editor, when i click on Base Map, i get the following information : name : Base Map Reference Base_Map (i tried both to no avail)


Solution

  • As was pointed out in the comments and mentioned for Texture2D.SetPixels

    you must call Apply after SetPixels to upload the changed pixels to the GPU.

    Besides that of course you have to confirm that the shader ID is actually correct. Note that the property ID (real name) and display name (how it is called in the material Inspector) can be completely different (see Material.SetTexture). If the ID is wrong there will be no error message but rather simply no effect