Search Unity

Wind

Discussion in 'Scripting' started by MakakWasTaken, Mar 30, 2015.

?

Should I use a script or a shader

  1. Shader

    100.0%
  2. Script

    0 vote(s)
    0.0%
  1. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    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.
     
  2. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    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.
     
  3. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    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)

    Code (Shader):
    1. Shader "Custom/Wind" {
    2.     Properties {
    3.         _Color ("Tint", Color) = (1,1,1,1)
    4.         _Wind ("Wind", Vector) = (0,0,0,0)
    5.         _MainTex ("Main", 2D) = "white" {}
    6.     }
    7.     SubShader {
    8.         CGPROGRAM
    9.         #pragma surface surf Lambert vertex:vert
    10.         #include "UnityCG.cginc"
    11.            
    12.         uniform sampler2D _MainTex;
    13.         uniform float4 _Wind;
    14.         uniform float4 _Color;
    15.  
    16.         struct Input {
    17.             float2 MainTex;
    18.         };
    19.  
    20.         void vert(inout appdata_full v)
    21.         {
    22.             v.vertex.z += sin(_Wind.x)/10.0;
    23.         }
    24.      
    25.         void surf (Input IN, inout SurfaceOutput o) {
    26.             fixed4 difTex = tex2D(_MainTex, IN.MainTex);
    27.             o.Albedo = difTex.rgb * _Color.rgb;
    28.             o.Alpha = difTex.a * _Color.a;
    29.         }
    30.         ENDCG
    31.     }
    32. }
     
  4. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    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
     
  5. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    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:
    Code (Shader):
    1.         void vert (inout appdata_full v)
    2.         {
    3.             v.vertex.z += (sin(_Wind.x)/10.0) * v.texcoord.y;
    4.             v.vertex.x += (sin(_Wind.y)/10.0) * v.texcoord.y;
    5.         }
    I would still like to do this with the gradient map as it would give me much more control over it.
     
    Last edited: Mar 30, 2015
  6. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    I now have this, but I still can't get the gradient map to work:
    Code (Shader):
    1. Why doesn't this work?
    2. Shader "Selfmade/FlagWave"
    3. {
    4.     Properties
    5.     {
    6.         _Color ("Tint", Color) = (1,1,1,1)
    7.         _Wind ("Wind", Vector) = (0,0,0,0)
    8.         _MainTex ("Main", 2D) = "white" {
    9.             }
    10.         _WindTex ("Wind texture", 2D) = "white" {
    11.             }
    12.     }
    13.     Category
    14.     {
    15.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    16.         Blend SrcAlpha OneMinusSrcAlpha
    17.         AlphaTest Greater .01
    18.          
    19.          
    20.         // important
    21.         ZWrite off
    22.         SubShader
    23.         {
    24.             Pass
    25.             {
    26.                 CULL Off
    27.      
    28.                 CGPROGRAM
    29.                 #pragma vertex vert
    30.                 #pragma fragment frag
    31.                 #include "UnityCG.cginc"
    32.      
    33.  
    34.                 uniform sampler2D _MainTex;
    35.                 uniform sampler2D _WindTex;
    36.                 uniform float4 _Wind;
    37.                 uniform fixed4 _Color;
    38.                 //float4 _Time;
    39.      
    40.                 // vertex input: position, normal
    41.                 struct appdata {
    42.                     float4 vertex : POSITION;
    43.                     float4 texcoord : TEXCOORD0;
    44.                 };
    45.                 struct v2f {
    46.                     float4 pos : POSITION;
    47.                     float2 uv : TEXCOORD0;
    48.                 };
    49.                 v2f vert (appdata v) {
    50.                     v2f o;
    51.  
    52.                     half4 wind = tex2D(_WindTex, o.uv);
    53.  
    54.                     v.vertex.x += (sin(_Wind.x)/5.0) * wind.x;
    55.                     v.vertex.z += (sin(_Wind.y)/10.0) * wind.x;
    56.  
    57.                     o.pos = mul( UNITY_MATRIX_MVP, v.vertex );
    58.                     o.uv = v.texcoord;
    59.                     return o;
    60.                 }
    61.  
    62.      
    63.                 float4 frag (v2f i) : COLOR
    64.                 {
    65.                     half4 color = tex2D(_MainTex, i.uv);
    66.                     return color * _Color;
    67.                 }
    68.  
    69.  
    70.                 ENDCG
    71.             }
    72.         }
    73.     }
    74.     Fallback "VertexLit"
    75. }
     
  7. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    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.
     
  8. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    I have no idea on how to do such things, can you give me a starting point?
     
  9. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    will see what i can do tomorrow when i get to my main pc
     
  10. MakakWasTaken

    MakakWasTaken

    Joined:
    Mar 26, 2015
    Posts:
    45
    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.
     
  11. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    Good that you got it working, sorry that I wasn't able to send you a example but I was working on a tablet