Unity Community

Register or Sign In:

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 20 of 34

  1. Posts
    15

    waving flag shader: feedback please!

    hi, i've just written my first shader, it's for displaying a waving flag. here's the code:

    if someone with more experience could check my code and has any suggestions on how to improve it, i'd be very grateful

    Code:  
    1. Shader "Selfmade/FlagWave"
    2. {
    3.  
    4. Properties
    5. {
    6.     _Color ("Main Color", Color) = (1,1,1,1)
    7.     _MainTex ("Texture", 2D) = "white" { }
    8. }
    9.  
    10. SubShader
    11. {
    12.     Pass
    13.     {
    14.         CULL Off
    15.        
    16.         CGPROGRAM
    17.         #pragma vertex vert
    18.         #pragma fragment frag
    19.         #include "UnityCG.cginc"
    20.         #include "AutoLight.cginc"
    21.        
    22.         float4 _Color;
    23.         sampler2D _MainTex;
    24.        
    25.         //float4 _Time;
    26.        
    27.         // vertex input: position, normal
    28.         struct appdata {
    29.             float4 vertex : POSITION;
    30.             float4 texcoord : TEXCOORD0;
    31.         };
    32.        
    33.         struct v2f {
    34.             float4 pos : POSITION;
    35.             float2 uv: TEXCOORD0;
    36.         };
    37.        
    38.         v2f vert (appdata v) {
    39.             v2f o;
    40.            
    41.             float angle= _Time * 50;
    42.            
    43.             v.vertex.y =  v.texcoord.x * sin(v.vertex.x + angle);
    44.             v.vertex.y += sin(v.vertex.z / 2 + angle);
    45.             v.vertex.y *= v.vertex.x * 0.1f;
    46.            
    47.             o.pos = mul( glstate.matrix.mvp, v.vertex );
    48.             o.uv = v.texcoord;
    49.             return o;
    50.         }
    51.        
    52.         float4 frag (v2f i) : COLOR
    53.         {
    54.             half4 color = tex2D(_MainTex, i.uv);
    55.             return color;
    56.         }
    57.  
    58.         ENDCG
    59.        
    60.  
    61.         //SetTexture [_MainTex] {combine texture}
    62.     }
    63. }
    64. Fallback "VertexLit"
    65. }


    thx


  2. Posts
    62
    Hello cboe,

    it is quite nice. As improvement I would make 1 edge of the flag fixed as in reality.


  3. Location
    Melbourne, Australia
    Posts
    1,371
    Was any progress made on this? I would be a useful shader if it was pinned at the side the flag was attached to the pole.

    EDIT: ok, this shader doesn't work or do anything. I know it was written a long time ago, but has anyone got any ideas as to whats wrong with it in Unity 2.6.1 ?

    Cheers :-)
    Seon :: Main Code Monkey
    3 Sprockets :: http://www.3sprockets.com :: @3sprockets
    http://cubementd.com :: TD meets RTS in a whole new way!


  4. Location
    The sunken city of R'lyeh
    Posts
    2,061
    Hello Seon,

    This sort of shader is used in often in vertex shader 101 tutorials. You can probably find numerous example shaders, maybe even pinned to one side as you want, searching about with google.

    In fact I think the first time I saw one was on NeHe's famous OpenGL tutorial site. I remember it giving good insight in to how it works.

    http://nehe.gamedev.net/data/lessons....asp?lesson=11

  5. Volunteer Moderator
    Posts
    17,426
    There's nothing wrong with it...works fine as-is.

    --Eric


  6. Location
    Melbourne, Australia
    Posts
    1,371
    Hmm, i have it applied to a mesh and its not doing anything

    I must be missing something very obvious!

    I have a flag mesh, i have created a material for it called flag, have selected the FlagWave shader for it, and have added a texture to the texture slot.

    I see NO movement and I see no flag, other than when I select the object, i see the wireframe of the mesh.
    Seon :: Main Code Monkey
    3 Sprockets :: http://www.3sprockets.com :: @3sprockets
    http://cubementd.com :: TD meets RTS in a whole new way!

  7. Volunteer Moderator
    Posts
    17,426
    Does your mesh have a reasonable number of vertices so the effect has something to work with? And you pressed Play? Also, one end of the flag can be pinned if you create the mesh the right way, such as using this.

    --Eric


  8. Location
    Melbourne, Australia
    Posts
    1,371
    yes, i think its enough. here is an example image.



    and here is a screenie of my editor panel

    Seon :: Main Code Monkey
    3 Sprockets :: http://www.3sprockets.com :: @3sprockets
    http://cubementd.com :: TD meets RTS in a whole new way!


  9. Location
    Melbourne, Australia
    Posts
    1,371
    Ok, I got it working just using that pane creation script, but the same settings on my flag mesh don't do anything... oh well!

    Thanks for your help Eric!
    Seon :: Main Code Monkey
    3 Sprockets :: http://www.3sprockets.com :: @3sprockets
    http://cubementd.com :: TD meets RTS in a whole new way!

  10. Volunteer Moderator
    Posts
    17,426
    The mesh is oriented wrong...the shader is animating the vertex.y coordinates. You can rotate the mesh vertical, but it has to be created as a horizontal plane.

    --Eric


  11. Location
    Melbourne, Australia
    Posts
    1,371
    Yeah, I finally worked that out myself. Figures! My Trig is shite! So didnt really glance in that area of the shader to see it was trying to displace the wrong axis.

    So, now that I have established that my trig is shite... I want to get the flag displacing on the z axis too (and maybe even the x) to give it 3 axis movement.

    Anyone wanna give the math a try? my attempts so far are moving the z correctly, but squishing the flag on the z too...

    Code:  
    1. v.vertex.z =  v.texcoord.x * cos(v.vertex.x + angle); // this is where I am going wrong I think....
    2. v.vertex.z += cos(v.vertex.x / 2 + angle);
    3. v.vertex.z *= v.vertex.x * 0.1f; // ok, this is scaling movement down closer to U = 0

    Suggestions?
    Seon :: Main Code Monkey
    3 Sprockets :: http://www.3sprockets.com :: @3sprockets
    http://cubementd.com :: TD meets RTS in a whole new way!

  12. Administrator
    Location
    Blackpool, United Kingdom
    Posts
    8,593
    Quote Originally Posted by Seon
    Anyone wanna give the math a try?
    Can you just clarify exactly what you are doing here? Are you aiming to get a single ripple (from the cosine shape) that runs from the top of the flag to the bottom?
    I'm wired to the world... that's how I... know... everything...


  13. Location
    Melbourne, Australia
    Posts
    1,371
    Well yes, though wether its Cos or Sin, I have no idea!

    I was to extend the maths in the original shader to add an extra ripple from V0 - V1 that is also stronger at U1 and muted at U0.

    I have found some different math code (online) that ripples really well and has adjustments for ripples on 3 axis, but again it produces the same results on all axis other than y.

    Code:  
    1.       float sinOff=v.vertex.x+v.vertex.y+v.vertex.z;
    2.       float t=-_Time*50;
    3.       float sin1=sin(t*1.45+sinOff);
    4.       float sin2=sin(t*3.12+sinOff);
    5.       float sin3=sin(t*2.2+sinOff);
    6.       float fx=v.texcoord.x;
    7.       float fy=v.texcoord.x*v.texcoord.y;
    8.  
    9.       //v.vertex.x=sin1*fx*0.5;
    10.       v.vertex.y=sin2*fx*0.5-fy*0.9;
    11.       v.vertex.z=-sin3*fx*0.5;

    Produces this result:-


    Now commenting out this line puts it back to single y axis ripple that works fine.

    Code:  
    1. //v.vertex.z=-sin3*fx*0.5;

    Produces this result:-



    My parents used to tell me in school that I needed to pay attention in Maths classes and I used to argue, what's the point, like I am ever going to use this stuff in the real world! Sheesh... life is full of regrets hey !
    Seon :: Main Code Monkey
    3 Sprockets :: http://www.3sprockets.com :: @3sprockets
    http://cubementd.com :: TD meets RTS in a whole new way!

  14. Administrator
    Location
    Blackpool, United Kingdom
    Posts
    8,593
    You shouldn't set vertex.y at any point - this is what is squashing the flag. If you want a two-dimensional ripple, add the two sin values together and assign them to vertex.z:-
    Code:  
    1. v.vertex.z = sin3*fx*0.5 + sin2*fx*0.5;
    ...or something along those lines.
    I'm wired to the world... that's how I... know... everything...


  15. Location
    Melbourne, Australia
    Posts
    1,371
    Hey Andeee, it's Y I need in this case because of how the mesh is created (as a flat X*Z plane).

    Ok, I have it working now, I was replacing X & Z values rather that adding to them. That didn't work, this does!

    Quicktime movie of flag blowing - http://games.sector3.com.au/examples...lowing.mov.zip

    Here is a new shader that animates flag on all axis, and works a treat!

    Code:  
    1. // Original shader by cboe - Mar, 23, 2009
    2. // Enhanced to 3 axis movement by Seon - Jan, 21, 2010
    3. //
    4. // Requirements: assumes you are using a subdivided plane created with X (width) * Z (height) where Y is flat.
    5. // Requirements: assumes UV as: left X (U0) is attatched to pole, and Top Z (V1) is at top of pole.  
    6. //
    7. // Enjoy!
    8.  
    9. Shader "Selfmade/FlagWave"
    10. {
    11.  
    12. Properties
    13. {
    14.     _Color ("Main Color", Color) = (1,1,1,1)
    15.     _MainTex ("Texture", 2D) = "white" { }
    16. }
    17.  
    18. SubShader
    19. {
    20.     Pass
    21.     {
    22.        CULL Off
    23.        
    24.       CGPROGRAM
    25.       #pragma vertex vert
    26.       #pragma fragment frag
    27.       #include "UnityCG.cginc"
    28.       #include "AutoLight.cginc"
    29.        
    30.       float4 _Color;
    31.       sampler2D _MainTex;
    32.  
    33.       // vertex input: position, normal
    34.       struct appdata {
    35.           float4 vertex : POSITION;
    36.           float4 texcoord : TEXCOORD0;
    37.       };
    38.        
    39.       struct v2f {
    40.           float4 pos : POSITION;
    41.           float2 uv: TEXCOORD0;
    42.       };
    43.        
    44.       v2f vert (appdata v) {
    45.           v2f o;
    46.  
    47.           float sinOff=v.vertex.x+v.vertex.y+v.vertex.z;
    48.           float t=-_Time*50;
    49.           float fx=v.texcoord.x;
    50.           float fy=v.texcoord.x*v.texcoord.y;
    51.  
    52.           v.vertex.x+=sin(t*1.45+sinOff)*fx*0.5;
    53.           v.vertex.y=sin(t*3.12+sinOff)*fx*0.5-fy*0.9;
    54.           v.vertex.z-=sin(t*2.2+sinOff)*fx*0.2;
    55.    
    56.           o.pos = mul( glstate.matrix.mvp, v.vertex );
    57.           o.uv = v.texcoord;
    58.  
    59.          return o;
    60.       }
    61.        
    62.       float4 frag (v2f i) : COLOR
    63.       {
    64.          half4 color = tex2D(_MainTex, i.uv);
    65.          return color;
    66.       }
    67.  
    68.       ENDCG
    69.  
    70.       SetTexture [_MainTex] {combine texture}
    71.     }
    72. }
    73.     Fallback "VertexLit"
    74. }
    Seon :: Main Code Monkey
    3 Sprockets :: http://www.3sprockets.com :: @3sprockets
    http://cubementd.com :: TD meets RTS in a whole new way!


  16. Posts
    42
    I've used this shader in my Virtual Egyptian Temple (http://www.medievalist.net/unityworl...tiantemple.htm). I've been searching for a way to make the flags wave for ages, so I was very glad to find this shader here. Ideally, I'd like to have each of the four flags be a little out-of-synch with the others. If anyone can advise me how I might do that, I'd appreciate it (I am new to Unity). Thanks again.

  17. Volunteer Moderator
    Posts
    17,426
    I made the small addition of a WaveSpeed variable, so you can make different materials which have slightly different speeds:

    Code:  
    1. // Original shader by cboe - Mar, 23, 2009
    2. // Enhanced to 3 axis movement by Seon - Jan, 21, 2010
    3. // Added _WaveSpeed - Jan, 26, 2010
    4. //
    5. // Requirements: assumes you are using a subdivided plane created with X (width) * Z (height) where Y is flat.
    6. // Requirements: assumes UV as: left X (U0) is attatched to pole, and Top Z (V1) is at top of pole.  
    7. //
    8. // Enjoy!
    9.  
    10. Shader "Selfmade/FlagWave"
    11. {
    12.  
    13. Properties
    14. {
    15.     _Color ("Main Color", Color) = (1,1,1,1)
    16.     _MainTex ("Texture", 2D) = "white" { }
    17.     _WaveSpeed ("Wave Speed", Range(0.0, 150.0)) = 50.0
    18. }
    19.  
    20. SubShader
    21. {
    22.     Pass
    23.     {
    24.        CULL Off
    25.        
    26.       CGPROGRAM
    27.       #pragma vertex vert
    28.       #pragma fragment frag
    29.       #include "UnityCG.cginc"
    30.       #include "AutoLight.cginc"
    31.        
    32.       float4 _Color;
    33.       sampler2D _MainTex;
    34.       float _WaveSpeed;
    35.  
    36.       // vertex input: position, normal
    37.       struct appdata {
    38.           float4 vertex : POSITION;
    39.           float4 texcoord : TEXCOORD0;
    40.       };
    41.        
    42.       struct v2f {
    43.           float4 pos : POSITION;
    44.           float2 uv: TEXCOORD0;
    45.       };
    46.        
    47.       v2f vert (appdata v) {
    48.           v2f o;
    49.  
    50.         float sinOff=v.vertex.x+v.vertex.y+v.vertex.z;
    51.         float t=-_Time*_WaveSpeed;
    52.         float fx=v.texcoord.x;
    53.         float fy=v.texcoord.x*v.texcoord.y;
    54.  
    55.         v.vertex.x+=sin(t*1.45+sinOff)*fx*0.5;
    56.         v.vertex.y=sin(t*3.12+sinOff)*fx*0.5-fy*0.9;
    57.         v.vertex.z-=sin(t*2.2+sinOff)*fx*0.2;
    58.    
    59.           o.pos = mul( glstate.matrix.mvp, v.vertex );
    60.           o.uv = v.texcoord;
    61.  
    62.          return o;
    63.       }
    64.        
    65.       float4 frag (v2f i) : COLOR
    66.       {
    67.          half4 color = tex2D(_MainTex, i.uv);
    68.          return color;
    69.       }
    70.  
    71.       ENDCG
    72.  
    73.       SetTexture [_MainTex] {combine texture}
    74.     }
    75. }
    76.    Fallback "VertexLit"
    77. }

    --Eric


  18. Posts
    42
    Thanks for that update Eric. It's just what I was hoping for. Unfortunately, it is not working perfectly for me. Here's what happens:
    - I change the material of one of the flags and change the speed of its waving. That works. I end up with one of my four flags waving at a different rate from the others, both in the editor and in the web player.
    - I change the material on another flag and change the speed of its waving. That works when I play the scene in the editor. I see two of the flags waving at different rates from each other and from the original setting (so, three different rates of flag waving). However, I only see this when playing the scene in the editor. If I build the scene to view it in the web player, the last flag I set does not wave independently. Its rate is identical to the rate of the first flag I changed. In other words, there are only two rates of flag waving (the original rate, and the first one I changed). When I return from the web player to the editor, I find that the level I set for the last flag has changed to be equal to the setting for the first flag I changed. Hope that makes sense.
    Perhaps I'm doing something wrong, or maybe there's a bug somewhere.

  19. Volunteer Moderator
    Posts
    17,426
    The wave speed is part of the material, so you need a different material for each flag that should have a different speed. If you change the speed in one material, it changes for every object using that material.

    --Eric


  20. Posts
    42
    My previous message must not have been clear. I HAVE assigned three different materials, and set three different wave speeds. What I'm saying is that the third wave speed, though it starts out different, re-sets to match the speed I assigned to one of the other materials. I end up with two of my materials having exactly the same wave speed, though I had originally set them at different speeds. This happens after I build the web-player version. Do you not get this odd result? I've tried it over and over again and the result is always the same for me.