Untitled

 avatar
unknown
plain_text
2 months ago
3.5 kB
6
Indexable
Shader "Custom/GerstnerWaves"
{
    Properties
    {
        _BaseColor ("Color", Color) = (1,1,1,1)
        _WaveA ("Wave A", Vector) = (1,1,1,1) 
        _WaveB ("Wave B", Vector) = (1,1,1,1) 
        _WaveC ("Wave C", Vector) = (1,1,1,1) 
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
        Pass {
		    Name "Main"
		    Tags { "LightMode"="UniversalForward" }
	     
		    HLSLPROGRAM
                #pragma vertex vert
                #pragma fragment frag

                #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
                #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

                
                float3 GerstnerWave(float4 waveData, float3 gridPoint, float time)
                {
                    float steepness = waveData.z;
                    float wavelength = waveData.w;
                    float k = 2 * 3.14159265 / wavelength;
                    float c = sqrt(9.8 / k);
                    float2 d = normalize(waveData.xy);
                    float f = k * (dot(d, gridPoint.xz) - c * time);
                    float a = steepness / k;
                
                    return float3(
                        d.x * (a * cos(f)),
                        a * sin(f),
                        d.y * (a * cos(f))
                    );
                }

                struct Attributes
                {
                    float4 positionOS   : POSITION;
                    half3 normal        : NORMAL;
                    half4 tangent       : TANGENT;

                };

                struct Varyings
                {
                    float4 positionHCS  : SV_POSITION;
                    float3 positionOS : POSITION;
                    half3 normal        : TEXCOORD0;


                };

                // To make the Unity shader SRP Batcher compatible, declare all
                // properties related to a Material in a a single CBUFFER block with
                // the name UnityPerMaterial.
                CBUFFER_START(UnityPerMaterial)
                    half4 _BaseColor;
                    half4 _WaveA;
                    half4 _WaveB;
                    half4 _WaveC;
                CBUFFER_END

                Varyings vert(Attributes IN)
                {
                    Varyings OUT;


                    VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);
                    float3 pos = IN.positionOS;
                    pos += GerstnerWave(_WaveA, positionInputs.positionWS, _Time);
                    pos += GerstnerWave(_WaveB, positionInputs.positionWS, _Time);
                    pos += GerstnerWave(_WaveC, positionInputs.positionWS, _Time);
                    
                    positionInputs = GetVertexPositionInputs(pos);

                    OUT.positionHCS = positionInputs.positionCS;
                    OUT.normal = IN.tangent;
                    
                    return OUT;
                }

                half4 frag(Varyings IN) : SV_Target
                {
                    
                    normalize(cross(ddx(IN.positionOS), ddy(IN.positionOS)));
                    // Returning the _BaseColor value.
                    return _BaseColor;
                }
            ENDHLSL
        }
       
    }
    FallBack "Diffuse"
}
Editor is loading...
Leave a Comment