Search Unity

Passing parameters to fullscreen shader (noob question)

Discussion in 'Shaders' started by malu05b, Jul 25, 2015.

  1. malu05b

    malu05b

    Joined:
    Apr 30, 2013
    Posts:
    13
    Im trying to make a full screen colorcorrection shader (camera shader).
    For that i want to be able to adjust parameters directly on my camera, and then pass them onto the shader when needed.

    I have made a little test. I have added a parameter called "Gamma" that i want to move from the C# code onto the shader. However im not sure how i can do it.

    So in my C# file that i attach to the camera i add:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. namespace UnityStandardAssets.ImageEffects
    5. {
    6.     [ExecuteInEditMode]
    7.     [AddComponentMenu("MyShaders/CustomColor")]
    8.     public class CustomColor : ImageEffectBase
    9.     {
    10.         public Color gamma = Color.white; // This is the value i want to pass
    11.         void OnRenderImage (RenderTexture source, RenderTexture destination)
    12.         {
    13.             Graphics.Blit (source, destination, material);
    14.         }
    15.     }
    16. }

    And then i have the shader:


    Code (CSharp):
    1. Shader "Hidden/CustomColor" {
    2. Properties {
    3.     _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     _Gamma ("Gamma", Color) = (1.0,1.0,1.0,1.0)
    5. }
    6.  
    7. SubShader {
    8.     Pass {
    9.         ZTest Always Cull Off ZWrite Off
    10.              
    11. CGPROGRAM
    12. #pragma vertex vert_img
    13. #pragma fragment frag
    14. #include "UnityCG.cginc"
    15.  
    16. uniform sampler2D _MainTex;
    17. uniform float _Gamma; //The color i want to get!
    18.  
    19. fixed3 frag (v2f_img i) : SV_Target
    20. {  
    21.     fixed3 original = tex2D(_MainTex, i.uv);
    22.     float lift = 0.0;
    23.     //float gamma = 0.5;
    24.     float gain = 1.0;
    25.  
    26.     //LiftGammaGain
    27.     fixed3 LGG = pow( gain*(original.rgb + lift*(1-original.rgb)) ,(1/_Gamma)); //sudo code, won't work yet
    28.     output = LGG;
    29.     return output;
    30. }
    31. ENDCG
    32.  
    33.     }
    34. }
    35.  
    36. Fallback off
    37.  
    38. }
    39.  
     
  2. malu05b

    malu05b

    Joined:
    Apr 30, 2013
    Posts:
    13
    To answer my own question:

    material.SetColor("_Gamma",gamma);