Search Unity

Vertex Shader problem

Discussion in 'Shaders' started by Frieza, Jun 22, 2012.

  1. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    Hi all,

    I'm trying to shift some vertices in a vertex shader and I noticed some slightly erratic behavior whenever I moved the camera (vertices would literally "jump" to a different position in world space). I decided to scale back my shader and ended up with the following:

    Code (csharp):
    1.  
    2. Shader "Custom/Foliage/SUPERBASIC" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" {}       
    5.     }
    6.     SubShader {
    7.         Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
    8.         Blend SrcAlpha OneMinusSrcAlpha
    9.         Cull Off ZWrite Off
    10.        
    11.         CGINCLUDE  
    12.         #include "UnityCG.cginc"   
    13.  
    14.         sampler2D _MainTex;
    15.  
    16.         struct v2f {
    17.             float4  pos : SV_POSITION;
    18.             float2  uv      : TEXCOORD0;
    19.             fixed4  color   : COLOR;
    20.         };
    21.        
    22.        
    23.         v2f vert (appdata_full v)
    24.         {
    25.             v2f o;
    26.  
    27.             // so this should just be X + 1?
    28.             float4 newPos = v.vertex + float4(1,0,0,0);
    29.          
    30.             o.pos = mul( UNITY_MATRIX_MVP, newPos );
    31.                     o.uv = float4( v.texcoord.xy, 0, 0 );
    32.             return o;
    33.            
    34.         }      
    35.         ENDCG
    36.        
    37.         Pass {
    38.             CGPROGRAM
    39.             #pragma vertex vert
    40.             #pragma fragment frag      
    41.             fixed4 frag (v2f i) : COLOR
    42.             {      
    43.                 return tex2D (_MainTex, i.uv.xy);
    44.             }
    45.             ENDCG
    46.         }          
    47.     }
    48.     FallBack "Diffuse"
    49. }
    50.  
    I thought this would simply move all my vertices +1 in the X, but no! Move the camera around and you get all kinds of random placement of the objects!

    I don't get this issue at all if I use unity cubes, only with meshes which indicates some sort of issue with the mesh itself (which is just a plane) so I'm rather baffled as to what might be causing this.

    Could anyone offer any insight as to why my vertices end up in totally bizarre locations?
    Thanks!
     
    Last edited: Jun 23, 2012
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I'm unfortunately not understanding the problem. You spell vertices wrong, though.

    Edit: I hoped that you wouldn't change it in the main post. Other people who make the same mistake may not realize it if they come to this thread, now. I considered sending a PM, but hoped it would help more than just you by mentioning it here.

    http://forum.unity3d.com/threads/31385-Frustrum-is-spelled-frustum!!!-O
     
    Last edited: Jun 23, 2012
  3. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    The issue can be more clearly illustrated via this screen capture: http://www.rhythm-station.com/problem.mp4
    However, if we instead want to be unhelpful and anal about literacy then I think you're misusing the comma.
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    If you think that fixing people's mistakes for them is unhelpful, then I'm not going to help you. And that's too bad, given that I know what the problem is.

    What problem do you have with my comma? Why did you react that way?
     
    Last edited: Jun 23, 2012
  5. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    If you're a genuine person then I think you should re-read what you wrote, it actually comes across pretty rude.
     
  6. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,878
    Just remove the Cull Off directive.
     
  7. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    Thanks Aubergine,
    I tried your suggestion but unfortunately it did not work, the issue still persists.
     
  8. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    That's not part of it, though I could see how somebody could come to suggesting "ZWrite Off", based on that video.

    I think interpreting things that way is useless. I just want us both to get better at important things. Answer my questions and I'll answer yours – seems mutually beneficial to me. I'm interested in investing in people that I think can do good things for the world, instead of wasting time on tact. We've got better things to do.
     
    Last edited: Jun 23, 2012
  9. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    To answer your questions -

    What problem do you have with my comma?
    Very likely, the same problem you have with my spelling. Except I wouldn't have made a huge issue out of it had you not made a big issue out of my spelling in the first place. It's all totally unnecessary don't you agree? Even you yourself say "instead of wasting time on tact". You are as guilty of this as I.

    If my comment caused you upset, then I'm sorry. It has the equal amount of politeness, relevancy and "tact" as your original comment did. If you do feel annoyed by my comment, then you will have experienced the exact same reaction that I did. Please consider that. My comment can be interpreted just as much as yours can be.

    If you're looking for a more blunt and literal answer you can find more information here: http://grammar.ccc.commnet.edu/grammar/commas.htm (Not intending to cause offense by the way, your question can be interpreted a number of ways).

    Why did you react that way?
    I would go into depth if you really want to understand the psychology of why people would react the way they do when you post snide sounding comments (even if unintentional) but I won't here. If you really want such detail, I'll happily send you a private message on the issue rather than clog the forums with further banter.
     
  10. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    I think you need to learn the definition of tact. I don't recall making any effort not to offend, here. :-D

    I'm happy to have my grammatical errors pointed out, so I can improve myself. The offense I took from what you said was about being "unhelpful".

    I didn't find the rules there, but I had a feeling that this might be due to your location...
    http://english.stackexchange.com/qu...ma-before-a-subordinating-conjunction-like-if

    As suggested here, I used a vocative comma. The American Heritage dictionary does that too, so I think I'm fine with it, but thanks for opening my eyes to this.

    That's okay. If you only think it will be helpful to me, don't worry about it.

    Now! As for the problem! :-D
    Dynamic batching is not consistent; it depends on the camera transform. When your objects are not batched, the shader does what you expect. When they are, then your offset is in world space. So, if the objects have any rotation or scaling, the offset will visibly jump between spaces.

    There are a couple solutions. The best one is probably to alter the mesh to not be so many separate objects. Is there a reason you did not do that? The other solution is to script the offset in world space, and apply that to the world position of the vertex, instead of the local one. Then you'd use a "VP" matrix instead of "MVP".

    You could also turn off dynamic batching!
     
    Last edited: Jun 23, 2012
  11. Frieza

    Frieza

    Joined:
    Aug 29, 2008
    Posts:
    68
    Thanks for this,

    I did try a whole mesh earlier and noticed an improvement, but your explanation makes a lot of sense.
    The reason I don't have the object as one big mesh is because I want to keep file size of the project as low as possible, as well as have the ability to create cheap mesh variation on trees for much lower cost than pumping out a lot of different meshes and bundling them all. I'm not using static batching at all, I instead combine everything at the start of a scene load at runtime. I should have tested combination of the leaf meshes sooner as this solves the issue quite easily. All in hindsight I suppose.

    As an aside, I also wasn't aware of the differences between American and British comma usage. At least we all learned something from this.

    Thanks again.