Skip to content

Instantly share code, notes, and snippets.

@cjacobwade
Created August 26, 2017 20:33
Show Gist options
  • Save cjacobwade/398526271af085966a5d446e405166e8 to your computer and use it in GitHub Desktop.
Save cjacobwade/398526271af085966a5d446e405166e8 to your computer and use it in GitHub Desktop.
struct Input
{
float3 worldPos;
half4 screenPos;
half4 grabPassPos;
half4 waveUV;
half4 grabPos;
};
half _NormalScale;
// Splash Offset
float4 _SplashData[10]; // Position XYZ, Timer W
half _WaveRange;
half _WaveHeight;
half _SplashTime;
half _Smoothing;
half _SplashNormalFactor;
float3 GetSplashOffset(float3 wPos)
{
float3 totalOffset = half3(0, 0, 0);
for (int i = 0; i < 10; i++)
{
// Add splash strength info to array
// Add ripples
half timeLeft = _SplashTime - _SplashData[i].w;
half sqrDist = length(_SplashData[i].xyz - wPos);
half distMod = (_WaveRange - clamp(sqrDist * sqrDist, 0, _WaveRange)) * 1 / _WaveRange;
totalOffset += float3(0, _WaveHeight * timeLeft, 0) * sin(_SplashData[i].w * 15) * distMod;
}
return totalOffset;
}
void ToonWaterVert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input, o);
float4 world = mul(unity_ObjectToWorld, v.vertex);
world.xyz += GetSplashOffset(world) * v.color.r;
// Create fake neighbor verts who we'll run the same effect over
// so we can calculate the right normal for after we've moved
half3 rightV = world.xyz + half3(0.35, 0, 0);
half3 forwardV = world.xyz + half3(0, 0, 0.35);
rightV += GetSplashOffset(rightV) * _SplashNormalFactor;
rightV -= (rightV - world.xyz) * _Smoothing;
forwardV += GetSplashOffset(forwardV) * 2;
forwardV -= (forwardV - world.xyz) * _Smoothing;
v.normal = normalize(cross(forwardV - world.xyz, rightV - world.xyz));
o.worldPos.xyz = v.vertex = mul(unity_WorldToObject, world);
half4 clipPos = mul(UNITY_MATRIX_MVP, v.vertex);
o.screenPos = ComputeScreenPos(clipPos);
o.grabPos = ComputeGrabScreenPos(clipPos);
o.waveUV.xy = o.worldPos.xz/100 * _NormalScale + _Time.x * 0.1;
o.waveUV.zw = o.worldPos.xz/100 * _NormalScale * 2 - _Time.x * 0.05;
}
@cjacobwade
Copy link
Author

Because this might cause confusion, you don't need to calculate screenPos and grabPos. I use those in the fragment function for refraction and edge foam.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment