0

I'm working on urp shader gerstner wave. Problem was that two side by side ocean plane waves were not continuesly. So, I tought if vertex positions would be world position, problem could fixed. it worked. However there was something I really don't understand.

this code worked

v.positionOS = mul(unity_ObjectToWorld, v.positionOS);

enter image description here

that one made weird things

float worldPos = mul(unity_ObjectToWorld, v.positionOS);
v.positionOS = worldPos;

enter image description here

all vertex shader here:

            v2f vert(a2v v)
        {
            v2f o;
            float3 tangent = float3(1, 0, 0);
            float3 binormal = float3(0, 0, 1);
            float3 p = v.positionOS;
            

  /*float worldPos = mul(unity_ObjectToWorld, v.positionOS);
v.positionOS = worldPos;*/

            v.positionOS = mul(unity_ObjectToWorld, v.positionOS);

            p += GerstnerWave(_WaveA, v.positionOS.xyz, tangent, binormal);

            o.heightOS = p.y;

            VertexPositionInputs positionInputs = GetVertexPositionInputs(p);
            o.positionCS = positionInputs.positionCS;
            o.positionWS = positionInputs.positionWS;

            return o;
        }
       
            float3 GerstnerWave(
    float4 wave, float3 p, inout float3 tangent, inout float3 binormal
)
{
    float steepness = wave.z;
    float wavelength = wave.w;
    float k = 2 * UNITY_PI / wavelength;
    float c = sqrt(9.8 / k);
    float2 d = normalize(wave.xy) * _Frequency;
    float f = k * (dot(d, p.xz) - c * _Time.y * _Speed);
    float a = steepness / k;

    tangent += float3(
        - d.x * d.x * (steepness * sin(f)),
        d.x * (steepness * cos(f)),
        - d.x * d.y * (steepness * sin(f))
    );
    binormal += float3(
        - d.x * d.y * (steepness * sin(f)),
        d.y * (steepness * cos(f)),
        - d.y * d.y * (steepness * sin(f))
    );

    return float3(
        d.x * (a * cos(f)),
        a * sin(f),
        d.y * (a * cos(f))
    );
}
guvncnyldz
  • 13
  • 4
  • directions are not working properly I guess – guvncnyldz May 16 '22 at 10:32
  • Are you converting it to an incorrect type? I would think you're getting an error if you weren't, but you didn't mention that. I tried to look up `positionOS` and it doesn't look like a stock variable name, but every single example I've seen online that uses `positionOS` has it as a `float4` and not a `float`. – Chuck May 16 '22 at 13:45
  • 1
    @Chuck right, of course, it had to be float4. simple problem just missed from me because of focusing so long time I guess. Thank you – guvncnyldz May 16 '22 at 15:09

0 Answers0