zoukankan      html  css  js  c++  java
  • Unity Shader入门精要学习笔记

    转载自 冯乐乐的 《Unity Shader 入门精要》

    消融效果

    消融效果常见于游戏中的角色死亡、地图烧毁等效果。这这些效果中,消融往往从不同的区域开始,并向看似随机的方向扩张,最后整个物体都将消失不见。我们将学习如何在Unity中实现这种效果。效果如下图所示。

    要实现上图中的效果,原理非常简单,概括来说就是噪声纹理+透明度测试。我们使用对噪声纹理采样的结果和某个控制消融程度的阈值比较,如果小于阈值,就使用clip函数把它对应的像素裁剪掉,这些部分就对应了图中被“烧毁”的区域。而镂空区域边缘的烧焦效果则是将两种颜色混合,再用pow函数处理后,与原纹理颜色混合后的效果。

    我们进行如下准备工作。

    1)新建一个场景,去掉天空盒子。

    2)新建一个材质,新建一个Shader,赋给材质

    3)构建一个包含3面墙的房间,并放置一个立方体,上步材质赋给立方体

    我们修改Shader 代码。

    [plain] view plain copy
     
    1. Shader "Unity Shaders Book/Chapter 15/Dissolve" {  
    2.     Properties {  
    3.         //控制消融程度  
    4.         _BurnAmount ("Burn Amount", Range(0.0, 1.0)) = 0.0  
    5.         //控制模拟烧焦效果时的线宽  
    6.         _LineWidth("Burn Line Width", Range(0.0, 0.2)) = 0.1  
    7.         _MainTex ("Base (RGB)", 2D) = "white" {}  
    8.         _BumpMap ("Normal Map", 2D) = "bump" {}  
    9.         //火焰边缘颜色  
    10.         _BurnFirstColor("Burn First Color", Color) = (1, 0, 0, 1)  
    11.         //火焰边缘颜色  
    12.         _BurnSecondColor("Burn Second Color", Color) = (1, 0, 0, 1)  
    13.         _BurnMap("Burn Map", 2D) = "white"{}  
    14.     }  
    15.     SubShader {  
    16.         Tags { "RenderType"="Opaque" "Queue"="Geometry"}  
    17.           
    18.         Pass {  
    19.             Tags { "LightMode"="ForwardBase" }  
    20.               
    21.             //关闭剔除,正面和背面都会被渲染  
    22.             Cull Off  
    23.               
    24.             CGPROGRAM  
    25.               
    26.             #include "Lighting.cginc"  
    27.             #include "AutoLight.cginc"  
    28.               
    29.             #pragma multi_compile_fwdbase  
    30.               
    31.             #pragma vertex vert  
    32.             #pragma fragment frag  
    33.               
    34.             fixed _BurnAmount;  
    35.             fixed _LineWidth;  
    36.             sampler2D _MainTex;  
    37.             sampler2D _BumpMap;  
    38.             fixed4 _BurnFirstColor;  
    39.             fixed4 _BurnSecondColor;  
    40.             sampler2D _BurnMap;  
    41.               
    42.             float4 _MainTex_ST;  
    43.             float4 _BumpMap_ST;  
    44.             float4 _BurnMap_ST;  
    45.               
    46.             struct a2v {  
    47.                 float4 vertex : POSITION;  
    48.                 float3 normal : NORMAL;  
    49.                 float4 tangent : TANGENT;  
    50.                 float4 texcoord : TEXCOORD0;  
    51.             };  
    52.               
    53.             struct v2f {  
    54.                 float4 pos : SV_POSITION;  
    55.                 float2 uvMainTex : TEXCOORD0;  
    56.                 float2 uvBumpMap : TEXCOORD1;  
    57.                 float2 uvBurnMap : TEXCOORD2;  
    58.                 float3 lightDir : TEXCOORD3;  
    59.                 float3 worldPos : TEXCOORD4;  
    60.                 SHADOW_COORDS(5)  
    61.             };  
    62.               
    63.             v2f vert(a2v v) {  
    64.                 v2f o;  
    65.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);  
    66.                   
    67.                 o.uvMainTex = TRANSFORM_TEX(v.texcoord, _MainTex);  
    68.                 o.uvBumpMap = TRANSFORM_TEX(v.texcoord, _BumpMap);  
    69.                 o.uvBurnMap = TRANSFORM_TEX(v.texcoord, _BurnMap);  
    70.                   
    71.                 TANGENT_SPACE_ROTATION;  
    72.                 o.lightDir = mul(rotation, ObjSpaceLightDir(v.vertex)).xyz;  
    73.                   
    74.                 o.worldPos = mul(_Object2World, v.vertex).xyz;  
    75.                   
    76.                 TRANSFER_SHADOW(o);  
    77.                   
    78.                 return o;  
    79.             }  
    80.               
    81.             fixed4 frag(v2f i) : SV_Target {  
    82.                 //对噪声纹理采样  
    83.                 fixed3 burn = tex2D(_BurnMap, i.uvBurnMap).rgb;  
    84.                 //将采样结果与_BurnAmount相减后 传递给clip  
    85.                 //小于0则会被剔除,不会显示到屏幕上  
    86.                 clip(burn.r - _BurnAmount);  
    87.                   
    88.                 float3 tangentLightDir = normalize(i.lightDir);  
    89.                 fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap, i.uvBumpMap));  
    90.                   
    91.                 fixed3 albedo = tex2D(_MainTex, i.uvMainTex).rgb;  
    92.                   
    93.                 fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;  
    94.                   
    95.                 fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(tangentNormal, tangentLightDir));  
    96.   
    97.                 fixed t = 1 - smoothstep(0.0, _LineWidth, burn.r - _BurnAmount);  
    98.                 fixed3 burnColor = lerp(_BurnFirstColor, _BurnSecondColor, t);  
    99.                 burnColor = pow(burnColor, 5);  
    100.                   
    101.                 UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);  
    102.                 fixed3 finalColor = lerp(ambient + diffuse * atten, burnColor, t * step(0.0001, _BurnAmount));  
    103.                   
    104.                 return fixed4(finalColor, 1);  
    105.             }  
    106.               
    107.             ENDCG  
    108.         }  
    109.           
    110.         //用于投射阴影  
    111.         Pass {  
    112.             Tags { "LightMode" = "ShadowCaster" }  
    113.               
    114.             CGPROGRAM  
    115.               
    116.             #pragma vertex vert  
    117.             #pragma fragment frag  
    118.               
    119.             #pragma multi_compile_shadowcaster  
    120.               
    121.             #include "UnityCG.cginc"  
    122.               
    123.             fixed _BurnAmount;  
    124.             sampler2D _BurnMap;  
    125.             float4 _BurnMap_ST;  
    126.               
    127.             struct v2f {  
    128.                 V2F_SHADOW_CASTER;  
    129.                 float2 uvBurnMap : TEXCOORD1;  
    130.             };  
    131.               
    132.             v2f vert(appdata_base v) {  
    133.                 v2f o;  
    134.                   
    135.                 TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)  
    136.                   
    137.                 o.uvBurnMap = TRANSFORM_TEX(v.texcoord, _BurnMap);  
    138.                   
    139.                 return o;  
    140.             }  
    141.               
    142.             fixed4 frag(v2f i) : SV_Target {  
    143.                 fixed3 burn = tex2D(_BurnMap, i.uvBurnMap).rgb;  
    144.                   
    145.                 clip(burn.r - _BurnAmount);  
    146.                   
    147.                 SHADOW_CASTER_FRAGMENT(i)  
    148.             }  
    149.             ENDCG  
    150.         }  
    151.     }  
    152.     FallBack "Diffuse"  
    153. }  

    在本例中,我们使用的噪声纹理如下图所示。



    水波效果

    在模拟实时水面的过程中,我们往往也会使用噪声纹理。此时,噪声纹理通常会用作一个高度图,以不断修改水面的法线方向。为了模拟水不断流动的效果,我们会使用和时间相关的变量来对噪声纹理进行采样,当得到法线信息后,再进行正常的反射+折射计算,得到最后的水面波动效果。

    我们将使用一个由噪声纹理得到的法线贴图,实现一个包含菲涅耳发射的水面效果,如下图所示。

    我们在之前介绍过如何使用反射和折射来模拟一个透明玻璃的效果。我们要使用的Shader 和之前的类似。我们使用一张立方体纹理作为环境纹理,模拟反射。为了模拟折射效果,我们使用GrabPass来获取当前屏幕的渲染纹理,并使用切线空间下的法线方向对像素的屏幕坐标进行偏移,再使用该坐标对渲染为进行屏幕采样,从而模拟近似的折射效果。水波的法线纹理是由一张噪声纹理生成而得,而且会随着时间变化不断平移,模拟波光粼粼的效果。除此之外,我们没有使用一个定值来混合反射和折射颜色,而是使用之前提到的菲涅耳系数来动态决定混合系数。我们使用如下公式来计算菲涅耳系数:

    其中,v 和 n 分别对应了视角方向和法线方向。它们之间的夹角越小,fresnel 值越小,反射越弱,折射越强。菲涅耳系数还经常会用于边缘光照的计算中。

    我们做如下准备工作。

    1)新建一个场景,去掉天空盒子

    2)新建一个材质,新建一个Shader,赋给材质

    3)我们构建一个由6面墙围成的封闭房间,房间里放置了一个平面来模拟水面。上步材质赋给平面。

    4)为了得到本场景适用的环境纹理,我们使用了之前实现的创建立方体纹理的脚本 (通过GameObject -> Render into Cubemap 打开编辑器窗口)来创建它,如下图所示。

    然后我们开始编写Shader

    [plain] view plain copy
     
    1. Shader "Unity Shaders Book/Chapter 15/Water Wave" {  
    2.     Properties {  
    3.         //控制水面颜色  
    4.         _Color ("Main Color", Color) = (0, 0.15, 0.115, 1)  
    5.         //水面波纹材质纹理  
    6.         _MainTex ("Base (RGB)", 2D) = "white" {}  
    7.         //由噪声纹理生成的法线纹理  
    8.         _WaveMap ("Wave Map", 2D) = "bump" {}  
    9.         //模拟反射的立方体纹理  
    10.         _Cubemap ("Environment Cubemap", Cube) = "_Skybox" {}  
    11.         _WaveXSpeed ("Wave Horizontal Speed", Range(-0.1, 0.1)) = 0.01  
    12.         _WaveYSpeed ("Wave Vertical Speed", Range(-0.1, 0.1)) = 0.01  
    13.         //控制模拟折射时图像的扭曲程度  
    14.         _Distortion ("Distortion", Range(0, 100)) = 10  
    15.     }  
    16.     SubShader {  
    17.         // We must be transparent, so other objects are drawn before this one.  
    18.         Tags { "Queue"="Transparent" "RenderType"="Opaque" }  
    19.           
    20.         //使用GrabPass 来获取屏幕图像  
    21.         GrabPass { "_RefractionTex" }  
    22.           
    23.         Pass {  
    24.             Tags { "LightMode"="ForwardBase" }  
    25.               
    26.             CGPROGRAM  
    27.               
    28.             #include "UnityCG.cginc"  
    29.             #include "Lighting.cginc"  
    30.               
    31.             #pragma multi_compile_fwdbase  
    32.               
    33.             #pragma vertex vert  
    34.             #pragma fragment frag  
    35.               
    36.             fixed4 _Color;  
    37.             sampler2D _MainTex;  
    38.             float4 _MainTex_ST;  
    39.             sampler2D _WaveMap;  
    40.             float4 _WaveMap_ST;  
    41.             samplerCUBE _Cubemap;  
    42.             fixed _WaveXSpeed;  
    43.             fixed _WaveYSpeed;  
    44.             float _Distortion;    
    45.             sampler2D _RefractionTex;  
    46.             float4 _RefractionTex_TexelSize;  
    47.               
    48.             struct a2v {  
    49.                 float4 vertex : POSITION;  
    50.                 float3 normal : NORMAL;  
    51.                 float4 tangent : TANGENT;   
    52.                 float4 texcoord : TEXCOORD0;  
    53.             };  
    54.               
    55.             struct v2f {  
    56.                 float4 pos : SV_POSITION;  
    57.                 float4 scrPos : TEXCOORD0;  
    58.                 float4 uv : TEXCOORD1;  
    59.                 float4 TtoW0 : TEXCOORD2;    
    60.                 float4 TtoW1 : TEXCOORD3;    
    61.                 float4 TtoW2 : TEXCOORD4;   
    62.             };  
    63.               
    64.             v2f vert(a2v v) {  
    65.                 v2f o;  
    66.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);  
    67.                   
    68.                 o.scrPos = ComputeGrabScreenPos(o.pos);  
    69.                   
    70.                 o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex);  
    71.                 o.uv.zw = TRANSFORM_TEX(v.texcoord, _WaveMap);  
    72.                   
    73.                 float3 worldPos = mul(_Object2World, v.vertex).xyz;    
    74.                 fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);    
    75.                 fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);    
    76.                 fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;   
    77.                   
    78.                 o.TtoW0 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);    
    79.                 o.TtoW1 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);    
    80.                 o.TtoW2 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);    
    81.                   
    82.                 return o;  
    83.             }  
    84.               
    85.             fixed4 frag(v2f i) : SV_Target {  
    86.                 float3 worldPos = float3(i.TtoW0.w, i.TtoW1.w, i.TtoW2.w);  
    87.                 fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));  
    88.                 float2 speed = _Time.y * float2(_WaveXSpeed, _WaveYSpeed);  
    89.                   
    90.                 // Get the normal in tangent space  
    91.                 fixed3 bump1 = UnpackNormal(tex2D(_WaveMap, i.uv.zw + speed)).rgb;  
    92.                 fixed3 bump2 = UnpackNormal(tex2D(_WaveMap, i.uv.zw - speed)).rgb;  
    93.                 fixed3 bump = normalize(bump1 + bump2);  
    94.                   
    95.                 // Compute the offset in tangent space  
    96.                 float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy;  
    97.                 i.scrPos.xy = offset * i.scrPos.z + i.scrPos.xy;  
    98.                 fixed3 refrCol = tex2D( _RefractionTex, i.scrPos.xy/i.scrPos.w).rgb;  
    99.                   
    100.                 // Convert the normal to world space  
    101.                 bump = normalize(half3(dot(i.TtoW0.xyz, bump), dot(i.TtoW1.xyz, bump), dot(i.TtoW2.xyz, bump)));  
    102.                 fixed4 texColor = tex2D(_MainTex, i.uv.xy + speed);  
    103.                 fixed3 reflDir = reflect(-viewDir, bump);  
    104.                 fixed3 reflCol = texCUBE(_Cubemap, reflDir).rgb * texColor.rgb * _Color.rgb;  
    105.                   
    106.                 fixed fresnel = pow(1 - saturate(dot(viewDir, bump)), 4);  
    107.                 fixed3 finalColor = reflCol * fresnel + refrCol * (1 - fresnel);  
    108.                   
    109.                 return fixed4(finalColor, 1);  
    110.             }  
    111.               
    112.             ENDCG  
    113.         }  
    114.     }  
    115.     // Do not cast shadow  
    116.     FallBack Off  
    117. }  

    再谈全局雾效

    我们之前讲到了如何使用深度纹理来实现一种基于屏幕后处理的全局雾效。我们由深度纹理重建每个像素在世界空间系下的位置,再使用一个基于高度的公式来计算雾效的混合系数,最后使用该系数来混合雾的颜色和原屏幕颜色。这种雾效是一个基于高度的均匀雾效,即在同一个高度上,雾的浓度是相同的,如下图左图所示。然而,一些时候我们希望可以模拟一种不均匀的雾效,同时让雾不断飘动,使雾看起来更加缥缈,如下图右图所示。而这就可通过使用一张噪声纹理来实现。

    实现非常简单,绝大代码和之前的一样,我们只是添加了噪声相关的参数和属性,并在Shader 的片元着色器中对高度的计算添加了噪声影响。

    准备工作如下:

    1)新建一个场景,去掉天空盒子

    2)构建一个包含3面墙的房间,并放置两个立方体和两个球体

    3)摄像机上添加脚本FogWithNoise.cs

    4)新建一个Shader。

    首先,我们编写FogWithNoise.cs

     
    1. public class FogWithNoise : PostEffectsBase {  
    2.   
    3.     public Shader fogShader;  
    4.     private Material fogMaterial = null;  
    5.   
    6.     public Material material {    
    7.         get {  
    8.             fogMaterial = CheckShaderAndCreateMaterial(fogShader, fogMaterial);  
    9.             return fogMaterial;  
    10.         }    
    11.     }  
    12.       
    13.     private Camera myCamera;  
    14.     public Camera camera {  
    15.         get {  
    16.             if (myCamera == null) {  
    17.                 myCamera = GetComponent<Camera>();  
    18.             }  
    19.             return myCamera;  
    20.         }  
    21.     }  
    22.   
    23.     private Transform myCameraTransform;  
    24.     public Transform cameraTransform {  
    25.         get {  
    26.             if (myCameraTransform == null) {  
    27.                 myCameraTransform = camera.transform;  
    28.             }  
    29.               
    30.             return myCameraTransform;  
    31.         }  
    32.     }  
    33.   
    34.     [Range(0.1f, 3.0f)]  
    35.     public float fogDensity = 1.0f;  
    36.   
    37.     public Color fogColor = Color.white;  
    38.   
    39.     public float fogStart = 0.0f;  
    40.     public float fogEnd = 2.0f;  
    41.   
    42.     public Texture noiseTexture;  
    43.   
    44.     [Range(-0.5f, 0.5f)]  
    45.     public float fogXSpeed = 0.1f;  
    46.   
    47.     [Range(-0.5f, 0.5f)]  
    48.     public float fogYSpeed = 0.1f;  
    49.   
    50.     [Range(0.0f, 3.0f)]  
    51.     public float noiseAmount = 1.0f;  
    52.   
    53.     void OnEnable() {  
    54.         GetComponent<Camera>().depthTextureMode |= DepthTextureMode.Depth;  
    55.     }  
    56.           
    57.     void OnRenderImage (RenderTexture src, RenderTexture dest) {  
    58.         if (material != null) {  
    59.             Matrix4x4 frustumCorners = Matrix4x4.identity;  
    60.               
    61.             float fov = camera.fieldOfView;  
    62.             float near = camera.nearClipPlane;  
    63.             float aspect = camera.aspect;  
    64.               
    65.             float halfHeight = near * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);  
    66.             Vector3 toRight = cameraTransform.right * halfHeight * aspect;  
    67.             Vector3 toTop = cameraTransform.up * halfHeight;  
    68.               
    69.             Vector3 topLeft = cameraTransform.forward * near + toTop - toRight;  
    70.             float scale = topLeft.magnitude / near;  
    71.               
    72.             topLeft.Normalize();  
    73.             topLeft *= scale;  
    74.               
    75.             Vector3 topRight = cameraTransform.forward * near + toRight + toTop;  
    76.             topRight.Normalize();  
    77.             topRight *= scale;  
    78.               
    79.             Vector3 bottomLeft = cameraTransform.forward * near - toTop - toRight;  
    80.             bottomLeft.Normalize();  
    81.             bottomLeft *= scale;  
    82.               
    83.             Vector3 bottomRight = cameraTransform.forward * near + toRight - toTop;  
    84.             bottomRight.Normalize();  
    85.             bottomRight *= scale;  
    86.               
    87.             frustumCorners.SetRow(0, bottomLeft);  
    88.             frustumCorners.SetRow(1, bottomRight);  
    89.             frustumCorners.SetRow(2, topRight);  
    90.             frustumCorners.SetRow(3, topLeft);  
    91.               
    92.             material.SetMatrix("_FrustumCornersRay", frustumCorners);  
    93.   
    94.             material.SetFloat("_FogDensity", fogDensity);  
    95.             material.SetColor("_FogColor", fogColor);  
    96.             material.SetFloat("_FogStart", fogStart);  
    97.             material.SetFloat("_FogEnd", fogEnd);  
    98.   
    99.             material.SetTexture("_NoiseTex", noiseTexture);  
    100.             material.SetFloat("_FogXSpeed", fogXSpeed);  
    101.             material.SetFloat("_FogYSpeed", fogYSpeed);  
    102.             material.SetFloat("_NoiseAmount", noiseAmount);  
    103.   
    104.             Graphics.Blit (src, dest, material);  
    105.         } else {  
    106.             Graphics.Blit(src, dest);  
    107.         }  
    108.     }  
    109. }  

    然后,再修改Shader

    [plain] view plain copy
     
      1. Shader "Unity Shaders Book/Chapter 15/Fog With Noise" {  
      2.     Properties {  
      3.         _MainTex ("Base (RGB)", 2D) = "white" {}  
      4.         _FogDensity ("Fog Density", Float) = 1.0  
      5.         _FogColor ("Fog Color", Color) = (1, 1, 1, 1)  
      6.         _FogStart ("Fog Start", Float) = 0.0  
      7.         _FogEnd ("Fog End", Float) = 1.0  
      8.         _NoiseTex ("Noise Texture", 2D) = "white" {}  
      9.         _FogXSpeed ("Fog Horizontal Speed", Float) = 0.1  
      10.         _FogYSpeed ("Fog Vertical Speed", Float) = 0.1  
      11.         _NoiseAmount ("Noise Amount", Float) = 1  
      12.     }  
      13.     SubShader {  
      14.         CGINCLUDE  
      15.           
      16.         #include "UnityCG.cginc"  
      17.           
      18.         float4x4 _FrustumCornersRay;  
      19.           
      20.         sampler2D _MainTex;  
      21.         half4 _MainTex_TexelSize;  
      22.         sampler2D _CameraDepthTexture;  
      23.         half _FogDensity;  
      24.         fixed4 _FogColor;  
      25.         float _FogStart;  
      26.         float _FogEnd;  
      27.         sampler2D _NoiseTex;  
      28.         half _FogXSpeed;  
      29.         half _FogYSpeed;  
      30.         half _NoiseAmount;  
      31.           
      32.         struct v2f {  
      33.             float4 pos : SV_POSITION;  
      34.             float2 uv : TEXCOORD0;  
      35.             float2 uv_depth : TEXCOORD1;  
      36.             float4 interpolatedRay : TEXCOORD2;  
      37.         };  
      38.           
      39.         v2f vert(appdata_img v) {  
      40.             v2f o;  
      41.             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);  
      42.               
      43.             o.uv = v.texcoord;  
      44.             o.uv_depth = v.texcoord;  
      45.               
      46.             #if UNITY_UV_STARTS_AT_TOP  
      47.             if (_MainTex_TexelSize.y < 0)  
      48.                 o.uv_depth.y = 1 - o.uv_depth.y;  
      49.             #endif  
      50.               
      51.             int index = 0;  
      52.             if (v.texcoord.x < 0.5 && v.texcoord.y < 0.5) {  
      53.                 index = 0;  
      54.             } else if (v.texcoord.x > 0.5 && v.texcoord.y < 0.5) {  
      55.                 index = 1;  
      56.             } else if (v.texcoord.x > 0.5 && v.texcoord.y > 0.5) {  
      57.                 index = 2;  
      58.             } else {  
      59.                 index = 3;  
      60.             }  
      61.             #if UNITY_UV_STARTS_AT_TOP  
      62.             if (_MainTex_TexelSize.y < 0)  
      63.                 index = 3 - index;  
      64.             #endif  
      65.               
      66.             o.interpolatedRay = _FrustumCornersRay[index];  
      67.                        
      68.             return o;  
      69.         }  
      70.           
      71.         fixed4 frag(v2f i) : SV_Target {  
      72.             float linearDepth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv_depth));  
      73.             float3 worldPos = _WorldSpaceCameraPos + linearDepth * i.interpolatedRay.xyz;  
      74.               
      75.             float2 speed = _Time.y * float2(_FogXSpeed, _FogYSpeed);  
      76.             float noise = (tex2D(_NoiseTex, i.uv + speed).r - 0.5) * _NoiseAmount;  
      77.                       
      78.             float fogDensity = (_FogEnd - worldPos.y) / (_FogEnd - _FogStart);   
      79.             fogDensity = saturate(fogDensity * _FogDensity * (1 + noise));  
      80.               
      81.             fixed4 finalColor = tex2D(_MainTex, i.uv);  
      82.             finalColor.rgb = lerp(finalColor.rgb, _FogColor.rgb, fogDensity);  
      83.               
      84.             return finalColor;  
      85.         }  
      86.           
      87.         ENDCG  
      88.           
      89.         Pass {                
      90.             CGPROGRAM    
      91.               
      92.             #pragma vertex vert    
      93.             #pragma fragment frag    
      94.                 
      95.             ENDCG  
      96.         }  
      97.     }   
      98.     FallBack Off  
      99. }  
  • 相关阅读:
    WPF 基础
    设计模式
    设计模式
    设计模式
    设计模式
    设计模式
    设计模式
    【DFS】hdu 1584 蜘蛛牌
    【优先队列】hdu 1434 幸福列车
    【最长公共子序列】hdu 1243 反恐训练营
  • 原文地址:https://www.cnblogs.com/kanekiken/p/7616720.html
Copyright © 2011-2022 走看看