Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Rendering Layer Mask Issue in Build - Setting Layers in URP

Discussion in 'Universal Render Pipeline' started by SteenPetersen, Jun 15, 2024.

  1. SteenPetersen

    SteenPetersen

    Joined:
    Mar 13, 2016
    Posts:
    113
    Using unity version 2022.3.22f1

    Question:


    Hi all,

    I'm encountering an issue where setting the renderingLayerMask on a DecalProjector works correctly in the Unity Editor but fails in the build. Here’s a detailed breakdown of what I’ve tried and the problem I’m facing:

    Description:
    I have a function to add blood spatter decals to my character. The logic involves performing a raycast to find the hit surface, adjusting the decal’s rotation to align with the surface normal, and setting its rendering layer mask. While this works perfectly in the Editor, it doesn’t display correctly in the build.

    Code Snippet:

    public void AddBloodSpatter(int boneID, Vector3 position, Quaternion rotation)
    {
    Debug.Log("Adding blood spatter");

    // Get the parent bone's position
    var parent = character.characterCustomizationManager.GetBoneFromArray(boneID);
    Debug.Assert(parent != null, "Parent bone is null");
    Debug.Log($"Parent bone position: {parent.position}");

    // Perform a raycast from the position towards the parent bone to find the surface
    Vector3 direction = (parent.position - position).normalized;
    Debug.Log($"Raycast direction: {direction}");

    if (Physics.Raycast(position, direction, out RaycastHit hit))
    {
    // Get the position and normal of the hit surface
    Vector3 hitPosition = hit.point;
    Vector3 hitNormal = hit.normal;

    // Log the hit position and normal
    Debug.Log($"Hit position: {hitPosition}, Hit normal: {hitNormal}");

    // Adjust the rotation to align with the normal
    rotation = Quaternion.LookRotation(hitNormal);
    Debug.Log($"Adjusted rotation: {rotation}");

    var decal = particleCollisionHandler.GetBloodDecal(hitPosition, rotation);
    Debug.Assert(decal != null, "Decal is null");

    decal.transform.SetParent(parent);
    var projector = decal.GetComponent<DecalProjector>();
    Debug.Assert(projector != null, "Projector is null");

    // Verify and set the rendering layer mask
    int layerIndex = WorldUtilityManager.Instance.playerBloodSpatterIndex;
    Debug.Log($"Layer Index: {layerIndex} (should be 3)");

    uint renderingLayerMask = (uint)(0x1 << layerIndex);
    Debug.Log($"Setting renderingLayerMask to: {renderingLayerMask} (should be 8)");
    projector.renderingLayerMask = renderingLayerMask;

    BloodSpatterElement bloodSpatterElement = new BloodSpatterElement
    {
    boneID = boneID,
    position = hitPosition,
    rotation = rotation,
    projector = projector
    };

    bloodSpattersOnPlayer.Add(bloodSpatterElement);
    Debug.Log("Blood spatter added successfully.");

    // Verify rendering layer names
    #if UNITY_EDITOR
    var globalSettings = UniversalRenderPipelineGlobalSettings.instance;
    if (globalSettings != null)
    {
    var renderingLayerNames = globalSettings.renderingLayerNames;
    Debug.Log($"Rendering Layer Name for Index {layerIndex}: {renderingLayerNames[layerIndex]}");
    }
    else
    {
    Debug.LogError("UniversalRenderPipelineGlobalSettings is not set. Please ensure URP is configured correctly.");
    }
    #endif
    }
    else
    {
    Debug.LogWarning("Blood spatter could not find a surface to attach to.");
    }
    }

    Observations:

    • Editor Behavior: The rendering layer mask is set correctly, this means the decals are changed from rendering on the terrain surface ("blood spatter") to rendering on the players skin and armor ("player blood spatter") and decals appear as expected.
    • Build Behavior: The decals are displayed incorrectly and are still rendeing on the terrain layer and NOT on the players skin and armor as if the renderinglayermask has not been changed, despite the mask being set correctly according to Debug logs as they show the mask is set to the expected value (e.g., 8 for the 'player blood spatter' layer).
    Additional Information:
    • Layer Configuration: The UniversalRenderPipelineGlobalSettings is correctly configured.

      upload_2024-6-15_12-38-16.png
    • The players skinnedmesh renderer has the correct layers set as well

      upload_2024-6-15_12-41-58.png
    Questions:
    1. Why does the renderingLayerMask setting work in the Editor but not in the build?
    2. How can I ensure that all rendering settings, including layers, are correctly applied in the build?
    3. Are there any specific settings or configurations I might be missing that could affect rendering layers in the build?
    Any help or insights would be greatly appreciated! Thank you!
     
  2. SteenPetersen

    SteenPetersen

    Joined:
    Mar 13, 2016
    Posts:
    113
    I have now checked to see if I change the decals default layer to be the "player blood spatter", it works fine in the build.



    So it seems like the build just doesnt allow me to change the bit value of the renderlayermask at runtime. Can anyone confirm this?

    Basically that I cannot run this line in a build of unity 2022.3.22f1

    Code (CSharp):
    1.     // Set the projector's layer to the 'player blood spatter' layer
    2.     int layerIndex = WorldUtilityManager.Instance.playerBloodSpatterIndex;
    3.     Debug.Log($"Layer index: {layerIndex}");
    4.     Debug.Assert(layerIndex >= 0 && layerIndex < 32, "Invalid playerBloodSpatterIndex");
    5.  
    6.     uint renderingLayerMask = (uint)(0x1 << layerIndex);  // this line
    7.     Debug.Log($"Setting renderingLayerMask: {renderingLayerMask}");
    8.     projector.renderingLayerMask = renderingLayerMask;
     
    Last edited: Jun 15, 2024