Search Unity

Applying the shader to a manually rendered camera, renders the Main Camera instead

Discussion in 'Shaders' started by jamesp124, May 19, 2020.

  1. jamesp124

    jamesp124

    Joined:
    Oct 7, 2019
    Posts:
    7
    Hi,

    Currently I am running 2020.1.0b8 on Linux, as I require some of its other non-graphical features.

    My scene has a disabled Camera object, which I would like to apply a shader (code at the bottom) via RenderWithShader() as for my purpose the camera should only render on demand. In another project that I pulled over from Windows running 2020.1.0b6 this works as expected, allowing me to then use GetRawTextureData() etc.

    In a new project created in Linux the same code doesn't appear to work. RenderWithShader() does not produce any meaningful result. Render() followed by Blit() applies the shader to the Main Camera and only when the Game window is selected in the Editor. Blit() doesn't seem to care what the source texture argument is.

    Is there some kind of magic project setting that was carried over from Windows/0b6 that allows RenderWithShader() to function on a disabled Camera? Is my problem also a known issue in *nix?

    Some of my attempts:
    Code (CSharp):
    1. cam.RenderWithShader(depthShader, null);
    : this works in a project pulled over from Windows, but not in another project created fresh in Linux.
    Code (CSharp):
    1. cam.Render();
    2. Camera.main.targetTexture = null;
    3. Graphics.Blit(cam.targetTexture, cam.targetTexture, depthMaterial);
    : this applies the shader to what is shown in the Game window, and only when the Game window is selected in Editor. In the previous project I can go back to the Scene window and have it continue to function on another camera.

    The depth shader code for completeness:
    Code (CSharp):
    1. // all credit to: https://github.com/ronja-tutorials/ShaderTutorials/blob/master/Assets/017_DepthPostprocessing/DepthPostprocessing.shader
    2.  
    3. Shader "EyeDepth"{
    4.     SubShader{
    5.         // markers that specify that we don't need culling
    6.         // or comparing/writing to the depth buffer
    7.         Cull Off
    8.         ZWrite Off
    9.         ZTest Always
    10.  
    11.             Tags { "RenderType" = "Opaque" }
    12.  
    13.         Pass{
    14.             CGPROGRAM
    15.             //include useful shader functions
    16.             #include "UnityCG.cginc"
    17.  
    18.             //define vertex and fragment shader
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.  
    22.             //the depth texture
    23.             sampler2D _CameraDepthTexture;
    24.  
    25.             //the object data that's put into the vertex shader
    26.             struct appdata{
    27.                 float4 vertex : POSITION;
    28.                 float2 uv : TEXCOORD0;
    29.             };
    30.  
    31.             //the data that's used to generate fragments and can be read by the fragment shader
    32.             struct v2f{
    33.                 float4 position : SV_POSITION;
    34.                 float2 uv : TEXCOORD0;
    35.             };
    36.  
    37.             //the vertex shader
    38.             v2f vert(appdata v){
    39.                 v2f o;
    40.                 //convert the vertex positions from object space to clip space so they can be rendered
    41.                 o.position = UnityObjectToClipPos(v.vertex);
    42.                 o.uv = v.uv;
    43.                 return o;
    44.             }
    45.  
    46.             //the fragment shader
    47.             float frag(v2f i) : SV_TARGET{
    48.                 //get depth from depth texture
    49.                 float depth = tex2D(_CameraDepthTexture, i.uv).r;
    50.                 //linear depth between camera and far clipping plane
    51.                 depth = Linear01Depth(depth) * _ProjectionParams.z;
    52.  
    53.                 return depth;      
    54.             }
    55.  
    56.             ENDCG
    57.         }
    58.     }
    59.  
    60.             FallBack "Diffuse"
    61. }
    62.  
     
    Last edited: May 19, 2020
  2. jamesp124

    jamesp124

    Joined:
    Oct 7, 2019
    Posts:
    7
    At least for my admittedly narrow scope of problem, it appears to be fixed. I added the following to Project Settings > Graphics > Video > Always Included Shaders:
    • Hidden/Compositing
    • Hidden/VideoComposite
    • Hidden/VideoDecode

    This, and disabling MSAA on the Camera rendering depth, has resolved this issue for the time being.