I used thisa vertex shader for my flag , It works correctly but when I duplicate It distortion will happen In game (this is mean It works just for single flagbased on this example) to animate a flag waving.how can When I usehave a single flag in my shader for multiple flags with same materialscene, it works correctly.
shouldWhen I instancing new material foreach object? but this is't optimize.
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
duplicate the flag, all of the copies become wildly distorted after I think this Is bug because there is no reason for distorting here:hit play.
http://www.shaderslab.com/index.php?post/Animated-flag 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