Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Camera culling mask does not exclude objects when using a custom shader

Discussion in 'Shaders' started by Absinthe23, Sep 11, 2023.

  1. Absinthe23

    Absinthe23

    Joined:
    Feb 1, 2015
    Posts:
    16
    I'm using a shader and the below scripts attached to the camera to outline objects and want a way to exclude some objects from processing without using additional cameras. I added the layer mask / camera culling mask to the camera which is created at runtime but it has no effect. The shader picks up normals info which the script reads.

    The scripts are quite simple so I'm not sure what else to try.
    Code (CSharp):
    1.     Shader "Hidden/Roystan/Normals Texture"
    2.     {
    3.         Properties
    4.         {
    5.             // none
    6.         }
    7.         SubShader
    8.         {
    9.             Tags
    10.            {
    11.                "RenderType" = "Opaque"
    12.            }
    13.  
    14.             Pass
    15.             {
    16.                 CGPROGRAM
    17.                 #pragma vertex vert
    18.                 #pragma fragment frag
    19.  
    20.                 #include "UnityCG.cginc"
    21.  
    22.                 struct appdata
    23.                 {
    24.                     float4 vertex : POSITION;
    25.                    float3 normal : NORMAL;
    26.                 };
    27.  
    28.                 struct v2f
    29.                 {
    30.                     float4 vertex : SV_POSITION;
    31.                    float3 viewNormal : NORMAL;
    32.                 };
    33.  
    34.                 v2f vert (appdata v)
    35.                 {
    36.                     v2f o;
    37.                     o.vertex = UnityObjectToClipPos(v.vertex);
    38.                     o.viewNormal = COMPUTE_VIEW_NORMAL;
    39.                     return o;
    40.                 }
    41.  
    42.                 float4 frag (v2f i) : SV_Target
    43.                 {
    44.                     return float4(normalize(i.viewNormal) * 0.5 + 0.5, 0);
    45.                 }
    46.                 ENDCG
    47.             }
    48.         }
    49.     }
    50.  
    51.     using UnityEngine;
    52.  
    53.     public class RenderReplacementShaderToTexture : MonoBehaviour
    54.     {
    55.         [SerializeField]
    56.         Shader replacementShader;
    57.  
    58.         [SerializeField]
    59.         RenderTextureFormat renderTextureFormat = RenderTextureFormat.ARGB32;
    60.  
    61.         [SerializeField]
    62.         FilterMode filterMode = FilterMode.Point;
    63.  
    64.         [SerializeField]
    65.         int renderTextureDepth = 24;
    66.  
    67.         [SerializeField]
    68.         CameraClearFlags cameraClearFlags = CameraClearFlags.Color;
    69.  
    70.         [SerializeField]
    71.         Color background = Color.black;
    72.  
    73.         [SerializeField]
    74.         string targetTexture = "_RenderTexture";
    75.  
    76.         private RenderTexture renderTexture;
    77.         private new Camera camera;
    78.  
    79.         public LayerMask addedCamMask;
    80.  
    81.         private void Start()
    82.         {
    83.  
    84.             Camera thisCamera = GetComponent<Camera>();
    85.  
    86.             // Create a render texture matching the main camera's current dimensions.
    87.             renderTexture = new RenderTexture(thisCamera.pixelWidth, thisCamera.pixelHeight, renderTextureDepth, renderTextureFormat);
    88.             renderTexture.filterMode = filterMode;
    89.             // Surface the render texture as a global variable, available to all shaders.
    90.             Shader.SetGlobalTexture(targetTexture, renderTexture);
    91.  
    92.             // Setup a copy of the camera to render the scene using the normals shader.
    93.             GameObject copy = new GameObject("Camera" + targetTexture);
    94.             camera = copy.AddComponent<Camera>();
    95.             camera.CopyFrom(thisCamera);
    96.             camera.cullingMask = addedCamMask;
    97.             camera.transform.SetParent(transform);
    98.             camera.targetTexture = renderTexture;
    99.             camera.SetReplacementShader(replacementShader, "RenderType");
    100.             camera.depth = thisCamera.depth - 1;
    101.             camera.clearFlags = cameraClearFlags;
    102.             camera.backgroundColor = background;
    103.  
    104.         }
    105.     }
    106.  
    107.  
    108.     public class CameraDepthTextureMode : MonoBehaviour
    109.     {
    110.         [SerializeField]
    111.         DepthTextureMode depthTextureMode;
    112.  
    113.         private void OnValidate()
    114.         {
    115.             SetCameraDepthTextureMode();
    116.         }
    117.  
    118.         private void Awake()
    119.         {
    120.             SetCameraDepthTextureMode();
    121.         }
    122.  
    123.         private void SetCameraDepthTextureMode()
    124.         {
    125.             GetComponent<Camera>().depthTextureMode = depthTextureMode;
    126.         }
    127.     }