Skip to content

Instantly share code, notes, and snippets.

@End3r6
Created August 14, 2022 00:41
Show Gist options
  • Select an option

  • Save End3r6/7ecfc8c16dbbc302ab338dce1701bbb2 to your computer and use it in GitHub Desktop.

Select an option

Save End3r6/7ecfc8c16dbbc302ab338dce1701bbb2 to your computer and use it in GitHub Desktop.
Basic gerstner water with ambient and diffuse lighting.
Shader "Learning/LitGerstner"
{
Properties
{
_BaseColor("Base Color", COLOR) = (74, 162, 255, 1)
_Smoothness("Smoothness", Range(0, 1)) = 0.5
_SpecularTint ("Specular Color", Color) = (0.5, 0.5, 0.5)
_WhiteCapIntensity("White Caps Intensity", Float) = 0.5
_WaveA ("Wave A", Vector) = (1, 1, 0.25, 60)//Rough names for testing
_WaveB ("Wave B", Vector) = (1, 0.6, 0.25, 31)
_WaveC ("Wave C", Vector) = (1, 1.3, 0.15, 18)
}
SubShader
{
Tags
{
"RenderType" = "Transparent"
"RenderPipeline" = "UniversalRenderPipeline"
"LightMode" = "LightWeightForward"
}
Pass
{
Name "Gerstner Waves"
HLSLPROGRAM
#pragma target 4.5
#pragma vertex vert
#pragma fragment frag Standard fullforwardshadows vertex:vert addshadow
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct appdata
{
float4 vertex : POSITION;
// float2 uv : TEXCOORD0;
float4 normal : NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
// float2 uv : TEXCOORD0;
float4 normal : NORMAL;
float3 worldPos : TEXCOORD2;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
half4 _SpecularTint;
CBUFFER_END
float3 GerstnerWave (float4 wave, float3 p, inout float3 tangent, inout float3 binormal)
{
float steepness = wave.z;
float wavelength = wave.w;
float k = 2 * PI / wavelength;
float c = sqrt(9.8 / k);
float2 d = normalize(wave.xy);
float f = k * (dot(d, p.xz) - c * _Time.y);
float a = steepness / k;
tangent = float3
(
1 - d.x * d.x * (steepness * sin(f)),
d.x * (steepness * cos(f)),
-d.x * d.y * (steepness * sin(f))
);
binormal = float3
(
-d.x * d.y * (steepness * sin(f)),
d.y * (steepness * cos(f)),
1 - d.y * d.y * (steepness * sin(f))
);
return float3
(
d.x * (a * cos(f)),
a * sin(f),
d.y * (a * cos(f))
);
}
float4 _WaveA, _WaveB, _WaveC;
float _WhiteCapIntensity, _Smoothness;
v2f vert (appdata v)
{
v2f o;
float3 gridPoint = v.vertex.xyz;
float3 tangent = 0;
float3 binormal = 0;
float3 p = gridPoint;
p += GerstnerWave(_WaveA, gridPoint, tangent, binormal);
p += GerstnerWave(_WaveB, gridPoint, tangent, binormal);
p += GerstnerWave(_WaveC, gridPoint, tangent, binormal);
float3 normal = normalize(cross(binormal, tangent));
v.vertex.xyz = p;
v.normal.xyz = normal;
o.vertex = TransformObjectToHClip(v.vertex.xyz);
o.normal = mul
(
transpose((float4x4)unity_WorldToObject),
v.normal
);
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
o.normal = normalize(o.normal);
return o;
}
float4 frag(v2f i) : SV_TARGET
{
//White Caps
// float whiteCapValue = 1;
// float whiteCapMask = (whiteCapValue * _WhiteCapIntensity);
//Light info
float3 lightDir = _MainLightPosition.xyz;
float3 lightColor = _MainLightColor.rgb;
float3 viewDir = normalize(_WorldSpaceCameraPos - i.worldPos);
float3 halfVector = normalize(lightDir + viewDir);
//Lighting
float3 diffuse = lightColor * saturate(dot(lightDir, i.normal)) * _BaseColor;
float3 ambient = _BaseColor * 0.2;
//Specular
float3 spec = _SpecularTint * lightColor * pow(saturate(dot(halfVector, i.normal)), _Smoothness * 100);
float3 finalLighting = ambient + diffuse + spec;
return float4(finalLighting, 1);
}
ENDHLSL
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment