151 lines
5.7 KiB
GLSL
151 lines
5.7 KiB
GLSL
Shader "Custom/WaterToggleURP"
|
|
{
|
|
Properties
|
|
{
|
|
_NormalMap ("Normal Map", 2D) = "bump" {}
|
|
_ReflectMap ("Reflection Map", 2D) = "white" {}
|
|
_Strength ("Distortion Strength", Range(0, 0.2)) = 0.05
|
|
_WaterTint ("Water Tint", Color) = (0.85, 0.85, 0.85, 1)
|
|
_FacingColor ("Facing Color", Color) = (1,1,1,1)
|
|
_GrazingColor ("Grazing Color", Color) = (0.5,0.5,0.5,1)
|
|
_SpecColor ("Spec Color", Color) = (1,1,1,1)
|
|
_SpecIntensity ("Spec Intensity", Range(0, 4)) = 1
|
|
_RefractionStrength ("Refraction Strength", Range(0, 0.1)) = 0.02
|
|
}
|
|
|
|
SubShader
|
|
{
|
|
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
|
|
LOD 300
|
|
|
|
Pass
|
|
{
|
|
Name "ForwardLit"
|
|
Tags { "LightMode"="UniversalForward" }
|
|
|
|
HLSLPROGRAM
|
|
//-------------------------------------
|
|
// Setup
|
|
//-------------------------------------
|
|
#pragma vertex vert
|
|
#pragma fragment frag
|
|
#pragma multi_compile _ _EXPENSIVE_WATER
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
|
|
TEXTURE2D(_NormalMap); SAMPLER(sampler_NormalMap);
|
|
TEXTURE2D(_ReflectMap); SAMPLER(sampler_ReflectMap);
|
|
TEXTURE2D(_CameraOpaqueTexture); SAMPLER(sampler_CameraOpaqueTexture);
|
|
|
|
float _Strength;
|
|
float4 _WaterTint;
|
|
float4 _FacingColor;
|
|
float4 _GrazingColor;
|
|
float4 _SpecColor;
|
|
float _SpecIntensity;
|
|
float _RefractionStrength;
|
|
|
|
//-------------------------------------
|
|
// Varyings
|
|
//-------------------------------------
|
|
struct Attributes {
|
|
float4 positionOS : POSITION;
|
|
float2 uv : TEXCOORD0;
|
|
float3 normalOS : NORMAL;
|
|
};
|
|
|
|
struct Varyings {
|
|
float4 positionHCS : SV_POSITION;
|
|
float2 uv : TEXCOORD0;
|
|
float3 viewDirWS : TEXCOORD1;
|
|
float3 normalWS : TEXCOORD2;
|
|
};
|
|
|
|
//-------------------------------------
|
|
// Vertex shader
|
|
//-------------------------------------
|
|
Varyings vert(Attributes IN)
|
|
{
|
|
Varyings OUT;
|
|
OUT.positionHCS = TransformObjectToHClip(IN.positionOS);
|
|
|
|
VertexPositionInputs posInputs = GetVertexPositionInputs(IN.positionOS.xyz);
|
|
VertexNormalInputs nrmInputs = GetVertexNormalInputs(IN.normalOS);
|
|
|
|
OUT.uv = IN.uv;
|
|
OUT.normalWS = nrmInputs.normalWS;
|
|
OUT.viewDirWS = GetCameraPositionWS() - posInputs.positionWS;
|
|
|
|
return OUT;
|
|
}
|
|
|
|
//-------------------------------------
|
|
// Fresnel
|
|
//-------------------------------------
|
|
float ComputeFresnel(float3 view, float3 normal)
|
|
{
|
|
float NdotV = saturate(dot(normalize(view), normalize(normal)));
|
|
return pow(1 - NdotV, 5);
|
|
}
|
|
|
|
//-------------------------------------
|
|
// Fragment shader
|
|
//-------------------------------------
|
|
float4 frag(Varyings IN) : SV_Target
|
|
{
|
|
float3 Nmap = SAMPLE_TEXTURE2D(_NormalMap, sampler_NormalMap, IN.uv).xyz;
|
|
Nmap = normalize(Nmap * 2 - 1);
|
|
|
|
// Combine geometry normal + normal map
|
|
float3 N = normalize(IN.normalWS + Nmap);
|
|
|
|
//-------------------------------------
|
|
// UV distortion
|
|
//-------------------------------------
|
|
float2 uvDistort = IN.uv + N.xy * _Strength;
|
|
|
|
//-------------------------------------
|
|
// Fresnel
|
|
//-------------------------------------
|
|
float fresnel = ComputeFresnel(IN.viewDirWS, N);
|
|
|
|
//-------------------------------------
|
|
// Reflection
|
|
//-------------------------------------
|
|
float3 reflectCol = SAMPLE_TEXTURE2D(_ReflectMap, sampler_ReflectMap, uvDistort).rgb;
|
|
reflectCol *= _WaterTint.rgb;
|
|
|
|
//-------------------------------------
|
|
// Refraction (only if expensive mode)
|
|
//-------------------------------------
|
|
float3 refractCol = reflectCol;
|
|
|
|
#ifdef _EXPENSIVE_WATER
|
|
float2 refractUV = IN.uv + N.xy * _RefractionStrength;
|
|
refractCol = SAMPLE_TEXTURE2D(_CameraOpaqueTexture, sampler_CameraOpaqueTexture, refractUV).rgb;
|
|
#endif
|
|
|
|
//-------------------------------------
|
|
// Fresnel blend (reflect vs refract)
|
|
//-------------------------------------
|
|
float3 combined = lerp(refractCol, reflectCol, fresnel);
|
|
|
|
//-------------------------------------
|
|
// Water color facing/grazing
|
|
//-------------------------------------
|
|
float3 waterCol = lerp(_FacingColor.rgb, _GrazingColor.rgb, fresnel);
|
|
combined *= waterCol;
|
|
|
|
//-------------------------------------
|
|
// Specular
|
|
//-------------------------------------
|
|
float3 spec = _SpecColor.rgb * fresnel * _SpecIntensity;
|
|
|
|
return float4(combined + spec, 1.0);
|
|
}
|
|
|
|
ENDHLSL
|
|
}
|
|
}
|
|
}
|