Search Unity

Simulating Snowfall issues

Discussion in 'Shaders' started by Mr_Admirals, Feb 9, 2019.

  1. Mr_Admirals

    Mr_Admirals

    Joined:
    May 13, 2017
    Posts:
    86
    Hey,

    So for my snow deformation shader, I'm trying to implement Peer Play's snowfall shader into my pipeline. I've got everything set up but for some reason, the snowfall results are erased every frame, and I don't quite know why.



    Code (CSharp):
    1. Shader "Hidden/Snowfall"
    2. {
    3.     Properties
    4.     {
    5.         _DepthTex ("Depth Texture", 2D) = "black" {}
    6.         _FlakeAmount("Flake Amount", Float) = 0.0
    7.         _FlakeThickness("Flake Thickness", Float) = 0.0
    8.     }
    9.     SubShader
    10.     {
    11.         Pass
    12.         {
    13.             CGPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.  
    17.             #include "UnityCG.cginc"
    18.  
    19.             struct appdata
    20.             {
    21.                 float4 vertex : POSITION;
    22.                 float2 uv : TEXCOORD0;
    23.             };
    24.  
    25.             struct v2f
    26.             {
    27.                 float2 uv : TEXCOORD0;
    28.                 float4 vertex : SV_POSITION;
    29.             };
    30.  
    31.             float rand(float3 co)
    32.             {
    33.                 return frac(sin(dot(co.xyz, float3(12.9898, 78.233, 45.5432))) * 43758.5453);
    34.             }
    35.  
    36.             sampler2D _DepthTex;
    37.             float4 _DepthTex_ST;
    38.             half _FlakeAmount;
    39.             half _FlakeThickness;
    40.  
    41.             v2f vert (appdata v)
    42.             {
    43.                 v2f o;
    44.                 o.vertex = UnityObjectToClipPos(v.vertex);
    45.                 o.uv = TRANSFORM_TEX(v.uv, _DepthTex);
    46.                 return o;
    47.             }
    48.  
    49.             fixed4 frag (v2f i, out float outDepth : SV_Depth) : SV_Target
    50.             {
    51.                 float depth = SAMPLE_DEPTH_TEXTURE(_DepthTex, i.uv);
    52.                 float rValue = ceil(rand(float3(i.uv.xy, 0) * _Time.x) - (1 - _FlakeAmount));
    53.                 outDepth = saturate(depth - (rValue * _FlakeThickness));
    54.                
    55.                 return 0;
    56.             }
    57.             ENDCG
    58.         }
    59.     }
    60. }
    Code (CSharp):
    1. void OnRenderImage(RenderTexture source, RenderTexture destination)
    2.     {
    3.         if(!active)
    4.         {
    5.             return;
    6.         }
    7.  
    8.         Graphics.Blit(groundDepth, depthTexture, normalizeMat);
    9.  
    10.         RenderTexture rt1, rt2;
    11.  
    12.         blurMat.SetTexture("_MyDepthTex", depthTexture);
    13.  
    14.         if (downSampleMode == DownSampleMode.Half)
    15.         {
    16.             rt1 = RenderTexture.GetTemporary(depthTexture.width / 2, depthTexture.height / 2, 24, depthTexture.format);
    17.             rt2 = RenderTexture.GetTemporary(depthTexture.width / 2, depthTexture.height / 2, 24, depthTexture.format);
    18.             Graphics.Blit(depthTexture, rt1, blurMat, 0);
    19.         }
    20.         else if (downSampleMode == DownSampleMode.Quarter)
    21.         {
    22.             rt1 = RenderTexture.GetTemporary(depthTexture.width / 4, depthTexture.height / 4, 24, depthTexture.format);
    23.             rt2 = RenderTexture.GetTemporary(depthTexture.width / 4, depthTexture.height / 4, 24, depthTexture.format);
    24.             Graphics.Blit(depthTexture, rt1, blurMat, 1);
    25.         }
    26.         else
    27.         {
    28.             rt1 = RenderTexture.GetTemporary(depthTexture.width, depthTexture.height, 24, depthTexture.format);
    29.             rt2 = RenderTexture.GetTemporary(depthTexture.width, depthTexture.height, 24, depthTexture.format);
    30.             Graphics.Blit(depthTexture, rt1, blurMat, 0);
    31.         }
    32.  
    33.         for (var i = 0; i < blurIterations; i++)
    34.         {
    35.             blurMat.SetTexture("_MyDepthTex", rt1);
    36.             Graphics.Blit(rt1, rt2, blurMat, 2);
    37.             blurMat.SetTexture("_MyDepthTex", rt2);
    38.             Graphics.Blit(rt2, rt1, blurMat, 3);
    39.         }
    40.  
    41.         if (snowfall)
    42.         {
    43.             snowfallMat.SetTexture("_DepthTex", rt1);
    44.             Graphics.Blit(rt1, blurredDepthTexture, snowfallMat);
    45.             snowMat.SetTexture("_DispTex", blurredDepthTexture);
    46.         }
    47.         else
    48.         {
    49.             blurMat.SetTexture("_MyDepthTex", rt1);
    50.             Graphics.Blit(rt1, blurredDepthTexture, blurMat, 0);
    51.             snowMat.SetTexture("_DispTex", blurredDepthTexture);
    52.         }
    53.  
    54.         RenderTexture.ReleaseTemporary(rt1);
    55.         RenderTexture.ReleaseTemporary(rt2);
    56.     }
    I know it's working in some capacity, because when I pause the game, and move an object over some snow, I can see the built up snow appearing. (The results below are exaggerated because I've got max values)

    upload_2019-2-9_10-21-48.png
     
unityunity