Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Shader "Custom/Fish" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
[PowerSlider(5.0)] _Shininess ("Shininess", Range (0.03, 1)) = 0.078125
_Clipping ("Clipping", float) = 1
_MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_WiggleAmp ("Wiggle Amplitude", float) = 1
_WigglePeriod("Wiggle Period", float) = 1
_FishDistance("How far the fish hath traveled", float) = 0
}
CGINCLUDE
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
half _Shininess;
float _WiggleAmp;
float _WigglePeriod;
float _FishDistance;
float _Clipping;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};
void vert(inout appdata_full v) {
// add the normal to the vertex
float3 localpos = v.vertex;
float3 worldpos = mul(unity_ObjectToWorld, v.vertex).xyz;
v.vertex.x += sin((_FishDistance + v.vertex.z) * _WigglePeriod) * _WiggleAmp;
}
void surf (Input i, inout SurfaceOutputStandard o) {
fixed4 tex = tex2D(_MainTex, i.uv_MainTex);
o.Albedo = tex.rgb * _Color.rgb;
clip(tex.a + -_Clipping);
o.Alpha = tex.a * _Color.a;
o.Smoothness = _Shininess;
o.Normal = UnpackNormal(tex2D(_BumpMap, i.uv_BumpMap));
}
ENDCG
SubShader {
Tags { "RenderType"="Opaque"
"Queue"="Geometry"
}
LOD 400
CGPROGRAM
#pragma surface surf Standard fullforwardshadows vertex:vert addshadow
#pragma target 3.0
ENDCG
}
SubShader {
Tags { "RenderType"="Opaque"
"Queue"="Geometry"
}
LOD 400
CGPROGRAM
#pragma surface surf Standard fullforwardshadows vertex:vert addshadow
ENDCG
}
FallBack "Legacy Shaders/Specular"
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FishWiggle : MonoBehaviour
{
public AnimationCurve WiggleAmplitude;
public float DistanceScalar = 1;
public float BaseSpeed = 0f;
private Renderer fishRenderer;
private float fishDistance = 0;
private Vector3 lastPosition = Vector3.zero;
private float wiggleAmplitude = 0;
private float wiggleLerp = 2f;
// Start is called before the first frame update
void Start()
{
lastPosition = transform.position;
fishRenderer = GetComponentInChildren<Renderer>();
}
// Update is called once per frame
void Update()
{
var distance = Vector3.Distance(transform.position, lastPosition);
lastPosition = transform.position;
fishDistance += distance;
fishDistance += BaseSpeed * Time.deltaTime;
wiggleAmplitude = Mathf.Lerp(wiggleAmplitude, WiggleAmplitude.Evaluate(distance / Time.deltaTime), Time.deltaTime * wiggleLerp);
if(float.IsNaN(wiggleAmplitude))
{
wiggleAmplitude = 0;
}
fishRenderer.material.SetFloat("_FishDistance", fishDistance * DistanceScalar);
fishRenderer.material.SetFloat("_WiggleAmp", wiggleAmplitude);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment