I have tried to implement some kind of custom system for wind, since unity’s system is so closed. I have created a singleton which holds a vector2 representing the wind. Does anybody have an idea on where to start? Should I use a shader or should I do it with a script? The system is going to be used on about 250+ gameobject and is also going to be used on grass in the future if the system gets good enough.
I would likely do it in a vertex shader, than you can use vertex color to mask out parts of your foliage that you don’t want effected, like the very bottom of grass blades. You can do the motion in the shader or just feed the shader data with various Shader.setGlobalT methods.
I have now succesfully made a shader that contains movements, but how can I make the movement only affect the top part as you said (First time writing a shader)
Shader "Custom/Wind" {
Properties {
_Color ("Tint", Color) = (1,1,1,1)
_Wind ("Wind", Vector) = (0,0,0,0)
_MainTex ("Main", 2D) = "white" {}
}
SubShader {
CGPROGRAM
#pragma surface surf Lambert vertex:vert
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform float4 _Wind;
uniform float4 _Color;
struct Input {
float2 MainTex;
};
void vert(inout appdata_full v)
{
v.vertex.z += sin(_Wind.x)/10.0;
}
void surf (Input IN, inout SurfaceOutput o) {
fixed4 difTex = tex2D(_MainTex, IN.MainTex);
o.Albedo = difTex.rgb * _Color.rgb;
o.Alpha = difTex.a * _Color.a;
}
ENDCG
}
}
multiply it by your vertex color on one channel so somehting like v.color.r. than in your 3d pacakge of choice vertex color the parts you want to move in the wind red
How would I implement that, I have tried a couple of things with no luck. I have also created one more texture called windTex which is holding the gradient. I have no idea what the appdata_full class is.
EDIT: I got a better result with this:
void vert (inout appdata_full v)
{
v.vertex.z += (sin(_Wind.x)/10.0) * v.texcoord.y;
v.vertex.x += (sin(_Wind.y)/10.0) * v.texcoord.y;
}
I would still like to do this with the gradient map as it would give me much more control over it.
I now have this, but I still can’t get the gradient map to work:
Why doesn't this work?
Shader "Selfmade/FlagWave"
{
Properties
{
_Color ("Tint", Color) = (1,1,1,1)
_Wind ("Wind", Vector) = (0,0,0,0)
_MainTex ("Main", 2D) = "white" {
}
_WindTex ("Wind texture", 2D) = "white" {
}
}
Category
{
Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
AlphaTest Greater .01
// important
ZWrite off
SubShader
{
Pass
{
CULL Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
uniform sampler2D _MainTex;
uniform sampler2D _WindTex;
uniform float4 _Wind;
uniform fixed4 _Color;
//float4 _Time;
// vertex input: position, normal
struct appdata {
float4 vertex : POSITION;
float4 texcoord : TEXCOORD0;
};
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
v2f vert (appdata v) {
v2f o;
half4 wind = tex2D(_WindTex, o.uv);
v.vertex.x += (sin(_Wind.x)/5.0) * wind.x;
v.vertex.z += (sin(_Wind.y)/10.0) * wind.x;
o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
o.uv = v.texcoord;
return o;
}
float4 frag (v2f i) : COLOR
{
half4 color = tex2D(_MainTex, i.uv);
return color * _Color;
}
ENDCG
}
}
}
Fallback "VertexLit"
}
you cant do it with a gradient texture but have to use vertex color on your meshes, than use that vertex colour and multply it by your wind.
I have no idea on how to do such things, can you give me a starting point?
will see what i can do tomorrow when i get to my main pc
I got it working after a little bit of googling up on vertex colors in unity. Thank you so much you have been a big help.
Good that you got it working, sorry that I wasn’t able to send you a example but I was working on a tablet