Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Why my circle wave has a hole in the center ?

Discussion in 'Shaders' started by fang1994, Nov 15, 2019.

  1. fang1994

    fang1994

    Joined:
    Mar 20, 2019
    Posts:
    12
    This is simplified version without light, the wave center is (0,0)
    Code (CSharp):
    1. Shader "Water/CircleWaveHasHole"
    2. {
    3.     Properties
    4.     {
    5.         _Color("Color", Color) = (1,1,1,1)
    6.         _MainTex("Texture", 2D) = "white" {}
    7.         [Gamma]_Metallic("Metallic", Range(0,1)) = 0
    8.         _Smoothness("Smoothness", Range(0,1)) = 0
    9.  
    10.         _Wave1("Wave1  (ALQP)", Vector) = (0.2,5,0.2,1)//A:amplitude, L:wavelength, Q:steepness P:Initial phase
    11.         _Center1("Wave1 Center", Vector) = (1,1,1,1)//
    12.     }
    13.     SubShader
    14.     {
    15.         Tags { "RenderType"="Opaque" }
    16.         LOD 100
    17.  
    18.         Pass
    19.         {
    20.             CGPROGRAM
    21.             #pragma vertex vert
    22.             #pragma fragment frag
    23.             // make fog work
    24.             #pragma multi_compile_fog
    25.  
    26.             #include "UnityCG.cginc"
    27.  
    28.             #define TAU (2*UNITY_PI)
    29.             #define TIME (_Time.y)
    30.             struct GerstnerWave {
    31.                 float amplitude;
    32.                 float wavelength;
    33.                 float speed;
    34.                 float3 dir;
    35.                 float4 center;
    36.                 float steepness;
    37.                 float w;
    38.             };
    39.  
    40.             struct appdata
    41.             {
    42.                 float4 vertex : POSITION;
    43.                 float2 uv : TEXCOORD0;
    44.             };
    45.  
    46.             struct v2f
    47.             {
    48.                 float2 uv : TEXCOORD0;
    49.                 float4 vertex : SV_POSITION;
    50.             };
    51.  
    52.             sampler2D _MainTex;
    53.             float4 _MainTex_ST;
    54.             float4 _Color;
    55.             float _Metallic;
    56.             float _Smoothness;
    57.  
    58.             float4 _Wave1;
    59.             float4 _Center1;
    60.  
    61.             void GerstnerPositionSum(float3 originalPoint, GerstnerWave wave, inout float3 positionOffset) {
    62.                 float phi = dot(normalize(originalPoint.xz - float2(0, 0)), originalPoint.xz);
    63.                 positionOffset.y += wave.amplitude*sin(phi);
    64.             }
    65.  
    66.             v2f vert (appdata v)
    67.             {
    68.                 v2f o;
    69.  
    70.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    71.  
    72.                 GerstnerWave wave;
    73.  
    74.                 wave.amplitude = _Wave1[0];
    75.                 wave.wavelength = _Wave1[1];
    76.                 wave.steepness = _Wave1[2];
    77.                 wave.speed = _Wave1[3];
    78.                 wave.center = _Center1;
    79.                 wave.w = TAU / wave.wavelength;
    80.                 //float2 dir = abs(normalize(v.vertex.xz - _Center1.xz));
    81.                 //waves[0].dir = (dir.x, 0, dir.y);
    82.                 float3 positionOffset = float3(0, 0, 0);
    83.                 GerstnerPositionSum(v.vertex, wave, positionOffset);
    84.  
    85.                 v.vertex.y += positionOffset.y;
    86.                 o.vertex = UnityObjectToClipPos(v.vertex);
    87.                 return o;
    88.             }
    89.  
    90.             fixed4 frag (v2f i) : SV_Target
    91.             {
    92.                 return fixed4(0,0,0,0);
    93.             }
    94.             ENDCG
    95.         }
    96.     }
    97. }
    98.  
    I don't know why has a hole in center, how can the vertex be deleted?
    upload_2019-11-15_15-16-3.png
     

    Attached Files:

  2. Tartiflette

    Tartiflette

    Joined:
    Apr 10, 2015
    Posts:
    84
    I suspect the problem is here: normalize(originalPoint.xz - float2(0, 0)). I bet your missing vertex is exactly at position 0, which means your are trying to normalize 0 which returns something that is either infinite or simply not a number. Either way, not something you want to use as a vertex position :)
     
  3. fang1994

    fang1994

    Joined:
    Mar 20, 2019
    Posts:
    12
    Recently I have been quite busy, sorry, I forget to reply. I modify the code like this:
    Code (CSharp):
    1. float phi = 0;
    2. if(originalPoint.x!=0 || originalPoint.z!=0 ){
    3.     phi = dot(normalize(originalPoint.xz - float2(0, 0)), originalPoint.xz);
    4. }
    And I think if the wave is circle wave, use length(originalPoint.xz - float2(0, 0) is better.