Last active
February 26, 2019 02:22
-
-
Save phosphoer/a71d04931ceab265a697db22233c990b to your computer and use it in GitHub Desktop.
Fish Shader
This file contains 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
Shader "Custom/Fish" | |
{ | |
Properties | |
{ | |
_Color ("Color", Color) = (1,1,1,1) | |
_MainTex ("Texture", 2D) = "white" {} | |
_LightRamp ("Light Ramp", 2D) = "white" {} | |
_VertexColorWeight ("Vertex Color Weight", float) = 1 | |
_FishLength ("Fish Length", float) = 1 | |
_FishFrontOffset ("Fish Front Offset", float) = 0 | |
_SwimScaleX ("Swim Scale X", float) = 1 | |
_SwimSpeedX ("Swim Speed X", float) = 1 | |
_SwimFreqX ("Swim Frequency X", float) = 1 | |
_SwimScaleY ("Swim Scale Y", float) = 1 | |
_SwimSpeedY ("Swim Speed Y", float) = 1 | |
_SwimFreqY ("Swim Frequency Y", float) = 1 | |
_FlapScale ("Flap Scale", float) = 0 | |
_FlapFrequency ("Flap Frequency", float) = 1 | |
_FlapSpeed ("Flap Speed", float) = 1 | |
_SwimTimeOffset ("Swim Time Offset", float) = 0 | |
_SwimTurnAmount ("Swim Turn Amount", float) = 0 | |
_SwimTurnScale ("Swim Turn Scale", float) = 1 | |
} | |
SubShader | |
{ | |
Tags { "IgnoreProjector" = "True" } | |
CGINCLUDE | |
#include "UnityCG.cginc" | |
#include "CellShading.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
fixed4 normal : NORMAL; | |
fixed4 color : COLOR; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float4 pos : SV_POSITION; | |
fixed4 color : COLOR; | |
float2 uv : TEXCOORD0; | |
SHADOW_COORDS(1) | |
fixed3 worldNormal : TEXCOORD2; | |
UNITY_FOG_COORDS(3) | |
}; | |
sampler2D _MainTex; | |
float4 _Color; | |
float4 _TintColor; | |
float _VertexColorWeight; | |
float _FishLength; | |
float _FishFrontOffset; | |
float _SwimScaleX; | |
float _SwimSpeedX; | |
float _SwimFreqX; | |
float _SwimScaleY; | |
float _SwimSpeedY; | |
float _SwimFreqY; | |
float _FlapScale; | |
float _FlapSpeed; | |
float _FlapFrequency; | |
float _SwimTurnAmount; | |
float _SwimTurnScale; | |
float _SwimTimeOffset; | |
v2f vert (appdata v) | |
{ | |
// Figure out some basic animation parameters like current time | |
// and where we are along the length of the fish | |
float4 objPos = v.vertex; | |
float time = _Time.y + _SwimTimeOffset; | |
float lengthT = saturate((-objPos.z + _FishFrontOffset) / _FishLength); | |
// Apply swimming animation to the fish, with more sway at the tail-end | |
objPos.x += sin(time * _SwimSpeedX + objPos.z * _SwimFreqX) * _SwimScaleX * lengthT; | |
objPos.y += sin(time * _SwimSpeedY + objPos.z * _SwimFreqY) * _SwimScaleY * lengthT; | |
// Apply flap animation | |
float widthT = saturate(abs(objPos.x) * _FlapFrequency); | |
objPos.y += sin(time * _FlapSpeed + abs(objPos.x) * _FlapFrequency) * _FlapScale * widthT; | |
// Rotate the tail-end of the fish with the turn parameter | |
float turnAmount = lengthT * lengthT; | |
turnAmount = clamp(_SwimTurnAmount, -1.0, 1.0) * turnAmount; | |
objPos.z += _FishFrontOffset; | |
objPos.z = objPos.z * cos(turnAmount * _SwimTurnScale) - objPos.x * sin(turnAmount * _SwimTurnScale); | |
objPos.x = objPos.z * sin(turnAmount * _SwimTurnScale) + objPos.x * cos(turnAmount * _SwimTurnScale); | |
objPos.x -= turnAmount * _SwimTurnScale * 2; | |
objPos.z -= _FishFrontOffset; | |
v2f o; | |
o.pos = UnityObjectToClipPos(objPos); | |
o.uv = v.uv; | |
o.worldNormal = mul(unity_ObjectToWorld, fixed4(v.normal.xyz, 0)); | |
o.color = v.color; | |
TRANSFER_SHADOW(o) | |
UNITY_TRANSFER_FOG(o, o.pos); | |
return o; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
// Get base diffuse color | |
fixed3 diffuse = _Color.rgb * tex2D(_MainTex, i.uv).rgb * lerp(fixed3(1, 1, 1), i.color.rgb, _VertexColorWeight); | |
diffuse *= CalculateLighting(normalize(i.worldNormal), SHADOW_ATTENUATION(i)).rgb; | |
UNITY_APPLY_FOG(i.fogCoord, diffuse); | |
return fixed4(diffuse, 1); | |
} | |
ENDCG | |
Pass | |
{ | |
Name "ShadowCaster" | |
Tags { "LightMode" = "ShadowCaster" } | |
Fog { Mode Off } | |
ZWrite On | |
ZTest Less | |
Cull Back | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment shadowCastFrag | |
float4 shadowCastFrag(v2f i) : COLOR | |
{ | |
SHADOW_CASTER_FRAGMENT(i) | |
} | |
ENDCG | |
} | |
Pass | |
{ | |
Tags { "RenderType"="Opaque" "LightMode" = "ForwardBase" } | |
ZWrite On | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#pragma multi_compile_fog | |
#pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight | |
ENDCG | |
} | |
} | |
Fallback "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment