Search Unity

Vertex shader creates gaps in models

Discussion in 'Shaders' started by proggerwolf, Feb 4, 2016.

  1. proggerwolf

    proggerwolf

    Joined:
    Feb 1, 2015
    Posts:
    4
    I am using a shader to make trees (modeled and vertex painted in blender) look like they wave in the wind. The 3d-model of my tree has no holes and I have already checked for vertices occupying the same position in blender. For some reason my models get "disassembled" (see in the image attached) in unity when the shader is applied. I have no idea why this occurs. Does anyone know what i am doing wrong or how to fix it? Thx in advance

    tree_problem.jpeg

    Code (csharp):
    1.    
    2. Shader "WavingDiffuse" {
    3. Properties {
    4.  _Color ("MainColor", Color) = (1,1,1,1)
    5.  _Emission ("Emission", Color) = (0,0,0,1)
    6.  _MainTex ("Base(RGB)", 2D) = "white" {}
    7.  _Stiffness ("Stiffness" , Range( 1 , 2 ))= 1.0
    8.  _WaveDirection ("WaveDirection" , Range(0 , 1)) = 1.0
    9.  _WaveStrength ("WaveStrength" , Range( 0 , 0.1 ))= 0.02
    10.  _WaveSpeed ("WaveSpeed" , Range( 0 , 20 ))= 6.0
    11.  _WaveSize ("WaveSize" , Range( 0.1 , 5 ))= 1.0
    12. }
    13. SubShader {
    14.  Tags { "RenderType"="Opaque"}
    15.  LOD 200
    16.  
    17. CGPROGRAM
    18. #pragma surface surf Lambert vertex:vert addshadow
    19.  
    20. sampler2D _MainTex;
    21. fixed4 _Color;
    22. fixed4 _Emission;
    23. float _Stiffness;
    24. float _WaveDirection;
    25. float _WaveStrength;
    26. float _WaveSpeed;
    27. float _WaveSize;
    28.  
    29. struct Input {
    30.  float2 uv_MainTex;
    31. };
    32.  
    33. void vert (inout appdata_full v) {
    34.  float multX = sin(_WaveDirection * 2 * 3.141592);
    35.  float multZ = cos(_WaveDirection * 2 * 3.141592);
    36.  float vp = ((multX * -v.vertex.x) + (multZ * -v.vertex.z))/ _WaveSize;
    37.  float offset = _WaveStrength * pow(v.color.r, _Stiffness) *(
    38.  (1 - sin(vp + _Time * 6 * _WaveSpeed)) +
    39.  (1 - sin(vp + _Time * 9 * _WaveSpeed)) +
    40.  (1 - sin(vp + _Time * 10 * _WaveSpeed))
    41.  );
    42.  v.vertex.x += multX * offset;
    43.  v.vertex.z += multZ * offset;
    44.  v.vertex.y -= sin(offset / 4);
    45. }
    46.  
    47. void surf (Input IN, inout SurfaceOutput o) {
    48.  fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    49.  o.Albedo = c.rgb;
    50.  o.Alpha = c.a;
    51.  o.Emission = _Emission;
    52. }
    53. ENDCG
    54. }
    55.  
    56. Fallback "VertexLit"
    57. }
    58. }
     
  2. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    It seems that the vertex color must be to blame here. They might not be the same for all faces.

    As a general note, even if something is 1 vertex in a modelling tool, it does not mean it's 1 vertex in a realtime application. Vertices are only shared if all properties are the same. (Position, normal, uvs, color.) Because of the flat shading, the normals are not the same and the vertices are split in this case. Every triangle will pretty much have 3 unique vertices in this model.
     
  3. proggerwolf

    proggerwolf

    Joined:
    Feb 1, 2015
    Posts:
    4
    okay... So it doesn't seem like there is an easy soulution to the problem but thanks for your answer
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    The solution is to make sure the color you're using in the vertex shader looks "smooth" when unlit and not faceted.
     
  5. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    Try to paint the vertex colors in unity.