Search Unity

Shaders behaves differently on 2D mesh and differently on quad

Discussion in 'Shaders' started by milanhenkrich, Jul 23, 2019.

  1. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    11
    Hey guys, I have problem with Water shader for my 2D project - using GrabPass with a bit of distortion added.

    When the shader is applied to a simple quad- it behaves as I want:
    This is brick underwater using quad as water:
    . 1.PNG

    However, in order to simulate water, I am using multiple vertices. So water is not a quad but more complex mesh. When I merge this meshes and I apply the material to it then the output is:

    2.PNG

    I can see that material/shader is behaving differently. It has repetitive patter for every vertex.

    Is there any way how can I avoid this?
     
  2. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    152
    Could you post actual code that shows how you distort the grab pass? It would appear as though the distortion texture is being tiled more over the full mesh.
     
    milanhenkrich likes this.
  3. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    11
    Thank you very much for reply Namey5, please see the code below:


    Code (CSharp):
    1. Shader "Custom/Water2D Surface" {
    2. Properties {
    3.   _Color("Color", Color) = (1,1,1,1)
    4.     _MainTex ("Normalmap", 2D) = "bump" {}
    5.   _Magnitude("Magnitude", Range(0,1)) = 0.05
    6. }
    7.  
    8. Category {
    9.  
    10.     Tags { "Queue"="Transparent" }
    11.  
    12.     SubShader {
    13.  
    14.         // This pass grabs the screen behind the object into a texture.
    15.         // We can access the result in the next pass as _BackgroundTex
    16.         GrabPass { "_BackgroundTex" }
    17.        
    18.         // Main pass: Take the texture grabbed above and use the bumpmap to perturb it
    19.         // on to the screen
    20.         Pass {
    21.             Name "BASE"
    22.            
    23.       CGPROGRAM
    24.       #pragma vertex vert
    25.       #pragma fragment frag
    26.       #include "UnityCG.cginc"
    27.  
    28.       struct vertexInput {
    29.           float4 vertex : POSITION;
    30.           float2 texcoord: TEXCOORD0;
    31.       };
    32.  
    33.       struct vertexOutput {
    34.           float4 vertex : SV_POSITION;
    35.           float4 uvgrab : TEXCOORD0;
    36.           float2 uvmain : TEXCOORD2;
    37.           UNITY_FOG_COORDS(3)
    38.       };
    39.  
    40.       fixed4 _Color;
    41.       sampler2D _MainTex;
    42.       float4 _MainTex_ST;
    43.       float _BumpAmt;
    44.       float4 _BumpMap_ST;
    45.       float  _Magnitude;
    46.  
    47.       vertexOutput vert (vertexInput v)
    48.       {
    49.           vertexOutput o;
    50.           o.vertex = UnityObjectToClipPos(v.vertex);
    51.           #if UNITY_UV_STARTS_AT_TOP
    52.           float scale = -1.0;
    53.           #else
    54.           float scale = 1.0;
    55.           #endif
    56.          o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    57.           o.uvgrab.zw = o.vertex.zw;
    58.           o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
    59.          UNITY_TRANSFER_FOG(o,o.vertex);
    60.           return o;
    61.       }
    62.  
    63.       sampler2D _BackgroundTex;
    64.       float4 _GrabTexture_TexelSize;
    65.       sampler2D _BumpMap;
    66.  
    67.       half4 frag (vertexOutput i) : SV_Target
    68.       {
    69.           // Calculate perturbed coordinates
    70.         half4 bump = tex2D(_MainTex, i.uvmain);
    71.  
    72.         half2 distortion = UnpackNormal(bump).rg;
    73.         i.uvgrab.xy += distortion * _Magnitude;
    74.    
    75.         // Get pixel in GrabTexture, rendered in previous pass
    76.  
    77.           half4 col = tex2Dproj(_BackgroundTex, (i.uvgrab));
    78.  
    79.         // Apply color
    80.           col *= _Color;
    81.  
    82.           return col;
    83.       }
    84.       ENDCG
    85.         }
    86.     }
    87. }
    88.  
    89. }
     
  4. Namey5

    Namey5

    Joined:
    Jul 5, 2013
    Posts:
    152
    Considering you're transforming the normal map, are you sure you haven't changed the tiling on the material? Also make sure the UVs on the new mesh line up with those of the quad, i.e. 0 on one side of the mesh, 1 on the other.
     
    Last edited: Jul 25, 2019
    milanhenkrich likes this.
  5. milanhenkrich

    milanhenkrich

    Joined:
    Jul 26, 2018
    Posts:
    11
    Hey Namey5, thanks for guiding me. You were right, I just had to adjust UVs.