Search Unity

SSAO shader

Discussion in 'Shaders' started by bugsbun, Jun 26, 2017.

  1. bugsbun

    bugsbun

    Joined:
    Jun 26, 2017
    Posts:
    27
    Hello Everybody
    I am new to shaders and wanted to build SSAO shader for which I found a very nice tutorial on :

    http://john-chapman-graphics.blogspot.co.uk/2013/01/ssao-tutorial.html

    I have implemented till generating the noise texture and have the normal and depth information from the cameras point of view , but cannot proceed to the real stuff where he asks to
    calcualte the origin? :

    Code (CSharp):
    1. vec3 origin = vViewRay * texture(uTexLinearDepth, vTexcoord).r;
    Here what is vViewRay and is uTexLinearDepth same as depthValue in my code below?

    Here is my code till the noise generation part :

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3.  
    4. Shader "Custom/CameraShader6" {
    5.  
    6.     Properties{
    7.  
    8.     _Depthcheck("Depthcheck", Range(0, 100)) = 40
    9.      _NormalHigh("_NormalHigh",Range(0,100)) = 1
    10.  
    11.     }
    12.    SubShader{
    13.        //Tags{ "RenderType" = "Opaque" }
    14.  
    15.        Pass{
    16.        CGPROGRAM
    17. #pragma vertex vert
    18. #pragma fragment frag
    19. #include "UnityCG.cginc"
    20.  
    21.    sampler2D _CameraDepthNormalsTexture;
    22.    sampler2D _CameraDepthTexture;
    23.    float _Depthcheck;
    24.    float _NormalHigh;
    25.  
    26.    struct v2f {
    27.        float4 pos : SV_POSITION;
    28.        float4 uv:TEXCOORD1;
    29.        //float3 normal:NORMAL;
    30.    };
    31.  
    32.    //Vertex Shader
    33.    v2f vert(appdata_full v) {
    34.        v2f o;
    35.        o.pos = UnityObjectToClipPos(v.vertex);
    36.        o.uv = ComputeScreenPos(o.pos);
    37.        //o.normal = v.normal;
    38.        return o;
    39.    }
    40.    float rand(float3 posit)
    41.    {
    42.        return frac(sin( dot(posit.xyz ,float3(12.9898,78.233,45.5432) )) * 43758.5453);
    43.    }
    44.  
    45.    float4 frag(v2f i) : COLOR{
    46.  
    47.        float depthValue;
    48.        float3 normalValues;
    49.        float4 depth;
    50.        float3 eyePos;
    51.        float4 randPos;
    52.        float randArray[64];
    53.        float noise[16];
    54.        float scale;
    55.        //float xx = rand();
    56.  
    57.  
    58.        DecodeDepthNormal(tex2D(_CameraDepthNormalsTexture, i.uv), depthValue, normalValues);
    59.        depthValue = (depthValue)*_Depthcheck;
    60.        normalValues = normalValues*_NormalHigh;
    61.        eyePos = float3(i.uv.xy,depthValue);
    62.  
    63.     //Generating the Sample Kernel
    64.        for (unsigned int i = 0; i < 64; ++i){
    65.            randArray[i] = float3((rand(_SinTime.xyz)*2.0 - 1.0),(rand(_SinTime.xyz)*2.0 - 1.0),rand(_SinTime.xyz));
    66.            randArray[i] = normalize(randArray[i]);
    67.            // scale each of the sample positions to distribute them within the hemisphere.
    68.            randArray[i] *= rand(_SinTime.xyz);
    69.            scale = (float)i / 64.0;
    70.            scale = lerp(0.1, 1.0, scale*scale);
    71.            randArray[i] *= scale;
    72.        }
    73.  
    74.        //generating the noise texture
    75.        for (int i = 0; i< 16; ++i){
    76.            noise[i] = float3((rand(_SinTime.xyz)*2.0 - 1.0),(rand(_SinTime.xyz)*2.0 - 1.0),0.0);
    77.            noise[i] = normalize(noise[i]);
    78.  
    79.        }
    80.  
    81.        return float4(0.5,0.3,0.7,1); //return something random right now till the shader is complete
    82.  
    83.      }
    84.            ENDCG
    85.        }
    86.    }
    87.        FallBack "Diffuse"
    88. }
    Can somebody guide me how to further with this... Thanks in advance !
     
    Last edited: Jun 27, 2017