- Home /
Question by
alessandro_88 · Oct 17, 2013 at 10:38 PM ·
shadergrasswind
making grass move at different speeds
Hi! I'm using the atsGrass-Unlit-Wind Shader for making grassplanes move as in the wind. The planes are being instantiated as I press play, every plane has it's WindShader script where I set a random Value to the windforces and set that value to the shader. If I select any plane, the values will therefore be different, but the planes still move all at the same speed. Does anybody know, why that could be?
Here is the shadercode:
// - Unlit
Shader "GrassShader" {
Properties {
_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "LightMode"="ForwardBase"}
LOD 100
Cull Off
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
CGINCLUDE
#include "UnityCG.cginc"
sampler2D _MainTex;
half4 _MainTex_ST;
float4 _GrassWind; //is not defined in terrainengine.cginc
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
inline float4 AnimateGrass(float4 pos, float3 normal, float animParams)
{
// animParams stored in color
// animParams = primary factor = blue
// Primary bending
// Displace position
pos.xyz += animParams * _GrassWind.xyz * _GrassWind.w; // controlled by vertex color blue
return pos;
}
v2f vert (appdata_full v)
{
v2f o;
float4 windParams = float(v.color.b);
// call vertex animation
float4 mdlPos = AnimateGrass(v.vertex, v.normal, windParams);
o.pos = mul(UNITY_MATRIX_MVP,mdlPos);
o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
return o;
}
ENDCG
Pass {
CGPROGRAM
#pragma debug
#pragma vertex vert
#pragma fragment frag
#pragma fragmentoption ARB_precision_hint_fastest
fixed4 frag (v2f i) : COLOR
{
fixed4 tex = tex2D (_MainTex, i.uv);
fixed4 c;
c.rgb = tex.rgb;
c.a = tex.a;
return c;
}
ENDCG
}
}
}
and here is the script:
using UnityEngine;
using System.Collections;
public class WindShader : MonoBehaviour
{
public Color VertexLitTranslucencyColor;
public Vector4 Wind;
public float GrassWindFrequency;
void Start ()
{
Wind = new Vector4 (UnityEngine.Random.Range (0.5f, 1.5f), 0, 0, UnityEngine.Random.Range (0.5f, 1.5f));
VertexLitTranslucencyColor = new Color (UnityEngine.Random.Range (0, 1), UnityEngine.Random.Range (0, 1), UnityEngine.Random.Range (0, 1), UnityEngine.Random.Range (0, 1));
GrassWindFrequency = UnityEngine.Random.Range (1, 2);
Application.targetFrameRate = 100;
Shader.SetGlobalColor("_TranslucencyColor", VertexLitTranslucencyColor);
Shader.SetGlobalFloat("_TranslucencyViewDependency;", 0.65f);
Shader.SetGlobalFloat("_ShadowOffsetScale;", 1.0f);
}
void Update ()
{
Shader.SetGlobalColor("_GrassWind", Wind);
Color GrassWindRGBA = Wind * ( (Mathf.Sin(Time.realtimeSinceStartup * GrassWindFrequency)));
GrassWindRGBA.a = Wind.w;
Shader.SetGlobalColor("_GrassWind", GrassWindRGBA);
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220613120132im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
grass on objects 0 Answers
Replacing built-in shaders for terrain billboard grass. 2 Answers
how to get the color of the ground on which the object lies 0 Answers
How can I give an object realistic wind? 0 Answers
Wind Effect On GameObject 0 Answers