Search Unity

Camera.RenderWithShader and Camera.SetReplacementShader

Discussion in 'Scripting' started by PentagramPro, Oct 11, 2015.

  1. PentagramPro

    PentagramPro

    Joined:
    Apr 5, 2014
    Posts:
    23
    Hello!
    I`m trying to implement a special effect when all objects in scene at once gain a "schematic" look. One can use RenderWithShader or SetReplacementShader to make camera render whole scene with a specific shader. However I have a problem with these methods.

    If I use SetReplacementShader on my main camera, the scene becomes empty no matter what I pass as 'tag' parameter (null, "", "Geometry", "Opaque", etc):
    Code (CSharp):
    1.  
    2.         if (enableSchematic)
    3.         {
    4.             MainCamera.SetReplacementShader(BlueprintCameraShader,null);
    5.         }
    6.         else
    7.         {
    8.             MainCamera.ResetReplacementShader();
    9.         }
    10.  
    If I use RenderWithShader, nothing happens at all. I`ve read many articles so I found that there is some bug with this method and one must use two cameras:
    Code (CSharp):
    1.  
    2.         if (enableSchematic)
    3.         {
    4.             MainCamera.gameObject.SetActive(false);
    5.             BlueprintCamera.gameObject.SetActive(true);
    6.             BlueprintCamera.RenderWithShader(BlueprintCameraShader, null);
    7.         }
    8.         else
    9.         {
    10.             BlueprintCamera.gameObject.SetActive(false);
    11.             MainCamera.gameObject.SetActive(true);
    12.             MainCamera.Render();
    13.         }
    14.  
    Nothing happens with this implementation either.

    All objects in the scene have either standard or legacy specular shaders. I`m trying to replace them with simple shader like this (taken from some Unity documentation):
    Code (CSharp):
    1.  
    2. Shader "Simple colored lighting"
    3. {
    4.     // a single color property
    5.     Properties{
    6.         _Color("Main Color", Color) = (.5,.5,.5,1)
    7.     }
    8.         // define one subshader
    9.     SubShader
    10.     {
    11.         Tags {"Queue" = "Geometry"}
    12.         // a single pass in our subshader
    13.         Pass
    14.         {
    15.             // use fixed function per-vertex lighting
    16.             Material
    17.             {
    18.                 Diffuse[_Color]
    19.             }
    20.             Lighting On
    21.         }
    22.     }
    23. }
    24.  
    Please help me :)