I used a vertex shader (based on this example) to animate a flag waving. When I have a single flag in my scene, it works correctly.
When I duplicate the flag, all of the copies become wildly distorted after I hit play.
Here is the shader code I'm using:
Shader "Custom/Flag" {
Properties {
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Speed ("Speed", Range(0, 5.0)) = 1
_Frequency ("Frequency", Range(0, 1.3)) = 1
_Amplitude ("Amplitude", Range(0, 5.0)) = 1
}
SubShader {
Tags { "RenderType"="Opaque" }
Cull off
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
float _Speed;
float _Frequency;
float _Amplitude;
v2f vert(appdata_base v)
{
v2f o;
v.vertex.y += cos((v.vertex.x + _Time.y * _Speed) * _Frequency) * _Amplitude * (v.vertex.x - 5);
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return tex2D(_MainTex, i.uv);
}
ENDCG
}
}
FallBack "Diffuse"
}
How can I make this work with multiple flags using the same material?
Should I use a new material instance for each object like this? It doesn't seem optimal:
material = new Material (Shader); // Create new material
material.SetFloat ("_Speed", _Speed);
material.SetFloat ("_Frequency", _Frequency);
material.SetFloat ("_Amplitude", _Amplitude);
material.SetTexture ("_MainTex", _MainTex);
flagObj.GetComponent<MeshRenderer>().sharedMaterial = material; // Set values