Search Unity

Getting trouble with camera.RenderWithShader()

Discussion in 'Scripting' started by melong, Mar 17, 2014.

  1. melong

    melong

    Joined:
    Sep 2, 2011
    Posts:
    22
    Hi there,

    I am trying to use the RenderWithShader function of the camera but i had no luck untill now and i cannot find why. So i could use some help here.

    The idea is to generate a mask through Render Texture in order to desaturate the whole screen except some objects (which i identify by setting their layer to "HighlightedCharacter").
    I set the camera background to white and i replace the concerned objects' shader with a plain black coloring shader, finally using the output of the camera render as a mask in my post effect shader.
    The problem i have is that the shader does not seem to be replaced, it renders the object normally (with its original shader).

    Here is the effect script:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. [ExecuteInEditMode]
    4. [AddComponentMenu("Image Effects/Color Adjustments/Desaturation")]
    5. public class _DesaturationEffect : ImageEffectBase
    6. {
    7.     public float saturationFactor;
    8.     public Shader maskShader;
    9.  
    10.     private RenderTexture _maskTex;
    11.     private GameObject _maskCam;
    12.  
    13.     void CleanUpTextures()
    14.     {
    15.         if (_maskTex)
    16.         {
    17.             RenderTexture.ReleaseTemporary(_maskTex);
    18.             _maskTex = null;
    19.         }
    20.     }
    21.    
    22.     void OnPreRender()
    23.     {
    24.         if (!enabled || !gameObject.activeInHierarchy) return;
    25.        
    26.         CleanUpTextures();
    27.        
    28.         if (!_maskCam)
    29.         {
    30.             _maskCam = new GameObject("MaskCamera");
    31.             _maskCam.AddComponent<Camera>();
    32.             _maskCam.camera.enabled = false;
    33.             _maskCam.hideFlags = HideFlags.HideAndDontSave;
    34.         }
    35.         _maskCam.camera.CopyFrom(camera);
    36.         _maskCam.camera.backgroundColor = new Color(1,1,1,1);
    37.         _maskCam.camera.clearFlags = CameraClearFlags.SolidColor;
    38.         _maskCam.camera.cullingMask = 1 << LayerMask.NameToLayer("HighlightedCharacter");
    39.  
    40.         _maskTex = RenderTexture.GetTemporary((int)camera.pixelWidth, (int)camera.pixelHeight, 16, RenderTextureFormat.ARGB32);
    41.         _maskCam.camera.targetTexture = _maskTex;
    42.         _maskCam.camera.RenderWithShader(maskShader, null);
    43.     }
    44.    
    45.     void OnRenderImage (RenderTexture source, RenderTexture destination)
    46.     {
    47.         material.SetFloat("_Desaturate", saturationFactor);
    48.         material.SetTexture("_MaskTex", _maskTex);
    49.         Graphics.Blit(source, destination, material);
    50.         CleanUpTextures();
    51.     }
    52.  
    53.     new void OnDisable()
    54.     {
    55.         if (_maskCam)
    56.         {
    57.             DestroyImmediate(_maskCam);
    58.         }
    59.         base.OnDisable();
    60.     }
    61. }
    And the shader code:
    Code (csharp):
    1. Shader "Hidden/_DesaturationEffect"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.     }
    7.  
    8.     SubShader
    9.     {
    10.         Pass
    11.         {
    12.             ZTest Always Cull Off ZWrite Off
    13.             Fog { Mode off }
    14.                
    15.             CGPROGRAM
    16.            
    17.                 #pragma vertex vert_img
    18.                 #pragma fragment frag
    19.                 #pragma fragmentoption ARB_precision_hint_fastest
    20.                 #include "UnityCG.cginc"
    21.  
    22.                 uniform sampler2D _MainTex;
    23.                 uniform sampler2D _MaskTex;
    24.                 uniform half _Desaturate;
    25.  
    26.                 fixed4 frag (v2f_img i) : COLOR
    27.                 {
    28.                     fixed4 tex = tex2D(_MainTex, i.uv);
    29.                     fixed4 mask = tex2D(_MaskTex, i.uv);
    30.  
    31.                     half3 grayXfer = half3(0.3, 0.59, 0.11);
    32.                     half grayf = dot(grayXfer, tex.rgb);
    33.                     half3 gray = half3(grayf, grayf, grayf);
    34.                     fixed4 col = fixed4(lerp(tex.rgb, gray, _Desaturate * mask.r), 1.0);
    35.  
    36.                     return col;
    37.                 }
    38.                
    39.             ENDCG
    40.  
    41.         }
    42.     }
    43.     Fallback off
    44. }
    Thanks in advance for your help :)