Skip to content

Instantly share code, notes, and snippets.

@fangzhangmnm
Last active March 1, 2022 04:50
Show Gist options
  • Save fangzhangmnm/38eeeca077db38d0e0b7af53a620947b to your computer and use it in GitHub Desktop.
Save fangzhangmnm/38eeeca077db38d0e0b7af53a620947b to your computer and use it in GitHub Desktop.
//https://gist.github.com/fangzhangmnm/38eeeca077db38d0e0b7af53a620947b
//Credit: Inspired by noriben's work https://booth.pm/ja/items/1671087
//reference https://docs.unity3d.com/Manual/SL-VertexFragmentShaderExamples.html
Shader "fzmnm/mobileWaterToon"
{
Properties
{
[Header(Scatter)]
_ShallowColor ("ShallowColor", Color) = (0.7, 0.95, 1)
_DeepColor ("DeepColor", Color) = (0, 0.5, 0.75)
_ScatterStrength ("ScatterStrength", Range(0, 10)) = 1
[PowerSlider(2)]_ScatterDepth("ScatterDepth",Range(0,10))=.5
[PowerSlider(2)]_DeepDepth("DeepDepth",Range(0,10))=5
[Space]
[Header(Reflection)]
_ReflectionColor ("ReflectionColor", Color) = (0.8, 0.9, 1)
_ReflectionStrength ("ReflectionStrength", Range(0, 10)) = 5
[PowerSlider(2)]_FresnelCoeff ("FresnelCoeff", Range(0, 128)) = 10
[PowerSlider(2)]_ReflectionDistortion ("ReflectionDistortion", Range(0, 2)) = 0.05
[Space]
[Header(Specular)]
_SpecularColor ("SpecularColor", Color) = (1, 1, 1)
_SpecularStrength ("SpecularStrength", Range(0, 5)) = 3
_SpecularLightDir ("SpecularLightDir",Vector)=(.1,.5,-1)
[PowerSlider(2)]_SpecularCoeff ("SpecularCoeff", Range(0, 128)) = 32
[PowerSlider(2)]_SpecularDistortion ("SpecularDistortion", Range(0, 2)) = 2
[Space]
[Header(Foam)]
_FoamColor ("FoamColor", Color) = (1, 1, 1,.8)
_FoamDepth("FoamDepth",Range(0,1))=.1
_FoamTexture("FoamTexture",2D)="white"{}
[PowerSlider(2)]_FoamScale ("FoamScale", Range(0, 1000)) = 100
_FoamScroll ("FoamScroll" , Vector) = (0,0.0005,0)
[Space]
[Header(Distortion)]
[Normal]_DistortionTexture ("DistortionTexture", 2D) = "blue" {}
[PowerSlider(2)]_DistortionScale ("DistortionScale", Range(0, 100)) = 10
_DistortionScroll ("DistortionScroll" , Vector) = (0,0.002,0)
[Space]
[Header(Displacement)]
_DisplacementTexture("DisplacementTexture",2D)="black"{}
[PowerSlider(2)]_DisplacementStrength ("DisplacementStrength", Range(0, 5)) = 0.1
[PowerSlider(2)]_DisplacementScale ("DisplacementScale", Range(0, 100)) = 20
_DisplacementScroll ("DisplacementScroll" , Vector) = (0,0.003,0)
[Space]
[Header(Color Correction)]
[PowerSlider(2)]_Gamma ("Gamma", Range(0, 5)) = 1.1
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue" = "Transparent" "LightMode" = "ForwardBase"}
LOD 100
Cull Back
Blend SrcAlpha OneMinusSrcAlpha
ZWrite On
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
#include "UnityLightingCommon.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
half3 tangent : TANGENT;
half4 vertColor : COLOR;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
// TANGENT, BINORMAL, NORMAL semantics are only available for input of vertex shader
half3 normal: TEXCOORD2;
half3 tangent: TEXCOORD3;
half3 worldPos : TEXCOORD4;
half4 vertColor:COLOR;
};
fixed3 _ShallowColor,_DeepColor;
half _ScatterStrength,_ScatterDepth,_DeepDepth;
fixed3 _ReflectionColor;
half _ReflectionStrength,_FresnelCoeff,_ReflectionDistortion;
fixed3 _SpecularColor,_SpecularLightDir;
half _SpecularStrength,_SpecularCoeff,_SpecularDistortion;
sampler2D _FoamTexture;
float _FoamScale;
float2 _FoamScroll;
fixed4 _FoamColor;
half _FoamDepth;
half _Gamma;
sampler2D _DistortionTexture;
float _DistortionScale;
float2 _DistortionScroll;
sampler2D _DisplacementTexture;
float _DisplacementScale,_DisplacementStrength;
float2 _DisplacementScroll;
v2f vert (appdata v)
{
v2f o;
float2 displacementTextureUV=(v.uv+_Time.y*_DisplacementScroll)*_DisplacementScale;
float h=tex2Dlod(_DisplacementTexture,float4(displacementTextureUV,0,0))*2-1;
v.vertex.xyz+=v.normal*h*_DisplacementStrength;
o.vertex = UnityObjectToClipPos(v.vertex);
UNITY_TRANSFER_FOG(o,o.vertex);
o.uv=v.uv;
o.normal=UnityObjectToWorldNormal(v.normal);
o.tangent=UnityObjectToWorldDir(v.tangent);
o.vertColor=v.vertColor;
o.worldPos=mul(unity_ObjectToWorld,v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
i.normal=normalize(i.normal);
half3 viewDir=normalize(UnityWorldSpaceViewDir(i.worldPos));
float3x3 TBN=transpose(float3x3(normalize(i.tangent),normalize(cross(i.normal,i.tangent)),i.normal));
half3 normal=normalize(mul(TBN,UnpackNormal(tex2D(_DistortionTexture,(i.uv+_Time.y*_DistortionScroll)*_DistortionScale))));
half depth=i.vertColor.r/saturate(dot(i.normal,viewDir));
half transmissive=exp(-depth/_ScatterDepth);
half3 scatteredLight=lerp(_DeepColor,_ShallowColor,exp(-depth/_DeepDepth))*_ScatterStrength;
half3 normalReflection=normalize(lerp(i.normal,normal,_ReflectionDistortion));
half3 reflectionDir=reflect(-viewDir,normalReflection);
reflectionDir.y*=reflectionDir.y<0?-1:1;
half fresnelStrength=pow(1-saturate(dot(normalReflection,viewDir)),_FresnelCoeff);
half3 reflectedLight=UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0,reflectionDir,0)*_ReflectionColor*_ReflectionStrength*fresnelStrength;
half3 normalSpecular=normalize(lerp(i.normal,normal,_SpecularDistortion));
half3 halfDir=normalize(normalize(_SpecularLightDir)+viewDir);
half3 specularLight=_SpecularColor*pow(saturate(dot(normalSpecular,halfDir)),_SpecularCoeff)*_SpecularStrength*fresnelStrength;
half3 light=scatteredLight+reflectedLight+specularLight;
half foam=saturate(1-i.vertColor.r/_FoamDepth);
half foamTex=tex2D(_FoamTexture,(i.uv+_Time.y*_FoamScroll)*_FoamScale);
foam=saturate(2*foam+foamTex-1);
light=lerp(light,_FoamColor.rgb,foam);
transmissive=lerp(transmissive,1-_FoamColor.a,foam);
light=pow(light,_Gamma);
//return fixed4(fixed3(1,1,1)*foam,1);
return fixed4(light,1-transmissive);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment