Last active
July 31, 2018 18:36
-
-
Save cjacobwade/86e1e43e2dc62158e94d8eed85f661ba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' | |
Shader "Sausage/Water/Toon" | |
{ | |
Properties | |
{ | |
_Color ("Color", Color) = (1,1,1,1) | |
_SplashNormalFactor("Normal Factor", Range(1, 5)) = 1 | |
_Smoothing("Normal Smoothing", Range(0, 1)) = 0.3 | |
_WaveRange("Wave Range", Float) = 1 | |
_WaveHeight("Wave Height", Float) = -1 | |
_ReflectDistort("Reflect Distort", Float) = 0.173 | |
_RefractDistort("Refract Distort", Range(0, 1.5)) = 0.211 | |
_BumpTex("Wave Normal (RGB)", 2D) = "bump" {} | |
_Bump("Wave Normal Scale", Range(-3, 3)) = 0.77 | |
_NormalScale("Normal Scale", Float) = 0.001 | |
_FoamEdge("Foam Edge Distance", Float) = 1.54 | |
_FoamNoiseScale("Foam Noise", Float) = 4 | |
_RefractFade("Refract Fade", Range(0, 1)) = 0.7 | |
_FogColor("Fog Color (RGB)", Color) = (0.05, 0.1, 0.8, 1.0) | |
_FogColor2("Fog Color 2", Color) = (1, 1, 1, 1) | |
_FogScale("Fog Scale", Range(0, 1)) = 0.332 | |
_Glossiness("Smoothness", Range(0,1)) = 0.0 | |
_Metallic("Metallic", Range(0,1)) = 0.0 | |
_Emission("Emission Strength", Range(0, 1)) = 0.446 | |
} | |
SubShader | |
{ | |
Tags { "RenderType" = "Transparent" "Queue" = "Transparent+1" } | |
LOD 200 | |
Blend SrcAlpha OneMinusSrcAlpha | |
GrabPass{"_RefractionTex"} | |
CGPROGRAM | |
#pragma surface surf Standard vertex:ToonWaterVert fullforwardshadows keepalpha nolightmap | |
#pragma target 3.0 | |
struct Input | |
{ | |
half4 screenPos; | |
half4 waveUV; | |
half4 grabPos; | |
half3 worldPos; | |
}; | |
// Built in | |
half4 _Color; | |
half _Glossiness; | |
half _Metallic; | |
half _Emission; | |
sampler2D _BumpTex; | |
half _Bump; | |
half _NormalScale; | |
// Refraction | |
sampler2D _RefractionTex; | |
half _RefractDistort; | |
// Foam | |
sampler2D _CameraDepthTexture; | |
half _FoamEdge; | |
half _FoamNoiseScale; | |
// Fog | |
half4 _FogColor; | |
half4 _FogColor2; | |
half _FogScale; | |
half _RefractFade; | |
float hash(float n) | |
{ | |
return frac(sin(n)*43758.5453); | |
} | |
float noise(float3 v) | |
{ | |
// The noise function returns a value in the range -1.0f -> 1.0f | |
float3 p = floor(v); | |
float3 f = frac(v); | |
f = f*f*(3.0 - 2.0*f); | |
float n = p.x + p.y*57.0 + 113.0*p.z; | |
return lerp(lerp(lerp(hash(n + 0.0), hash(n + 1.0), f.x), | |
lerp(hash(n + 57.0), hash(n + 58.0), f.x), f.y), | |
lerp(lerp(hash(n + 113.0), hash(n + 114.0), f.x), | |
lerp(hash(n + 170.0), hash(n + 171.0), f.x), f.y), f.z); | |
} | |
void ToonWaterVert(inout appdata_full v, out Input o) | |
{ | |
UNITY_INITIALIZE_OUTPUT(Input, o); | |
half4 clipPos = UnityObjectToClipPos(v.vertex); | |
o.screenPos = ComputeScreenPos(clipPos); | |
o.grabPos = ComputeGrabScreenPos(clipPos); | |
o.worldPos = mul(unity_ObjectToWorld, v.vertex); | |
o.waveUV.xy = v.vertex.xz/100 * _NormalScale + _Time.x * 0.1; | |
o.waveUV.zw = v.vertex.xz/100 * _NormalScale * 2 - _Time.x * 0.05; | |
} | |
void surf(Input IN, inout SurfaceOutputStandard o) | |
{ | |
// Combine bumps to get rid of repeating pattern in water | |
half3 bump1 = UnpackScaleNormal(tex2D(_BumpTex, IN.waveUV.xy), _Bump).rgb; | |
half3 bump2 = UnpackScaleNormal(tex2D(_BumpTex, IN.waveUV.zw), _Bump).rgb; | |
half3 bump = (bump1 + bump2) * 0.5; | |
// Foam | |
half4 edgeBlendFactors = half4(1.0, 0.0, 0.0, 0.0); | |
half depth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)); | |
depth = LinearEyeDepth(depth); | |
half depthDist = depth - IN.screenPos.w; | |
edgeBlendFactors = saturate(_FoamEdge * depthDist); | |
edgeBlendFactors.y = 1.0 - edgeBlendFactors.y; | |
// Refraction | |
half4 screenRefract = IN.screenPos; | |
screenRefract.xy -= bump * _RefractDistort; | |
half refractDepth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(screenRefract)); | |
refractDepth = LinearEyeDepth(refractDepth); | |
half refractDepthDist = refractDepth - screenRefract.w; | |
// Then unrefract anything that's above the water | |
screenRefract = IN.screenPos; | |
screenRefract.xy -= bump * _RefractDistort * saturate(refractDepthDist); | |
refractDepth = SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(screenRefract)); | |
refractDepth = LinearEyeDepth(refractDepth); | |
// Now get refract tex | |
half4 refractUV = IN.grabPos; | |
refractUV.xy -= bump * _RefractDistort * saturate(refractDepthDist); | |
half4 refract = tex2Dproj(_RefractionTex, refractUV); | |
refractDepthDist = refractDepth - screenRefract.w; | |
half fogAlpha = saturate(refractDepthDist * _FogScale); | |
refract = lerp(refract, refract * _FogColor2, fogAlpha * _FogColor2.a); | |
refract = lerp(refract, _FogColor, fogAlpha * _FogColor.a); | |
o.Albedo = lerp(_Color, refract, _RefractFade); | |
// Apply foam | |
half foamAlpha = edgeBlendFactors.x + noise(IN.worldPos.xyz * _FoamNoiseScale + half3(1, 1, 1) * _Time.y); | |
half isFoam = step(foamAlpha * edgeBlendFactors.x, 0.5); | |
o.Albedo = lerp(o.Albedo, half3(1.0, 1.0, 1.0), isFoam); | |
o.Normal = lerp(bump, fixed3(0, 1, 0), isFoam); | |
o.Emission = lerp(o.Albedo * _Emission, o.Albedo, isFoam); | |
half notFoam = 1 - isFoam; | |
o.Metallic = _Metallic * notFoam; | |
o.Smoothness = _Glossiness * notFoam; | |
o.Alpha = lerp(_Color.a, 1, isFoam); | |
} | |
ENDCG | |
} | |
FallBack "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment