Unity Shader 實現雨天的水面漣漪效果

Gb16978發表於2022-07-15

Unity Shader 實現雨天的水面漣漪效果

Shader "Custom/Ripple" {
 Properties {    _Color ( "Color", Color) = ( 1, 1, 1, 1)    _MainTex ( "Albedo (RGB)", 2D) = "white" {}    _Glossiness ( "Smoothness", Range( 0, 1)) = 0.5    _Metallic ( "Metallic", Range( 0, 1)) = 0.0    _RippleTex( "RippleTex", 2D) = "white" {}    _RippleScale( "RippleScale",Range( 1, 10)) = 1
 }  SubShader {    Tags { "RenderType"= "Opaque" }    LOD 200
   CGPROGRAM     // Physically based Standard lighting model, and enable shadows on all light types     # pragma surface surf Standard fullforwardshadows     # pragma target 3.0     # define PI 3.141592653    sampler2D _MainTex;
    struct Input {      float2 uv_MainTex;      half2 texcoord;    };
   half _Glossiness;    half _Metallic;    fixed4 _Color;    sampler2D _RippleTex;     float _RippleScale;    UNITY_INSTANCING_BUFFER_START(Props)    UNITY_INSTANCING_BUFFER_END(Props)
    //計算波紋的主函式     float3 ComputeRipple( float2 uv, float t)    {       //波紋貼圖取樣,並把取樣的高度值擴充套件到-1到1。      float4 ripple = tex2D(_RippleTex, uv);      ripple.yz = ripple.yz * 2.0 - 1.0;       //獲取波紋的時間,從A通道獲取不同的波紋時間,       float dropFrac = frac(ripple.a + t);       //把時間限制在R通道內       float timeFrac = dropFrac - 1.0 + ripple.x;       //做淡出處理       float dropFactor = 1-saturate( dropFrac);       //計算最終的高度,用一個sin計算出隨時間的振幅,修改一下值就知道什麼效果了       float final = dropFactor* sin(clamp(timeFrac * 9.0, 0.0, 4.0) * PI);       return float3(ripple.yz * final, 1.0);    }
    void surf ( Input IN, inout SurfaceOutputStandard o) {      fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;      o.Albedo = c.rgb;
      //呼叫方法,獲取高度。這裡我懶得用一個新uv,索性直接用的主貼圖UV      float3 ripple = ComputeRipple(IN.uv_MainTex / _RippleScale, _Time.y);       // Metallic and smoothness come from slider variables      o.Metallic = _Metallic;      o.Smoothness = _Glossiness;       //賦值到法線上      o.Normal = ripple;      o.Alpha = c.a;    }    ENDCG  }  FallBack "Diffuse" }


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/70019421/viewspace-2906020/,如需轉載,請註明出處,否則將追究法律責任。

相關文章