URP着色器,几乎相同的代码影响不同

发布于 2025-01-29 05:04:35 字数 2177 浏览 3 评论 0原文

我正在研究URP着色器Gerstner Wave。问题在于,并没有持续两侧海洋飞机波。因此,我觉得如果顶点位置是世界位置,问题可能会解决。它起作用。但是我真的不明白了。

该代码工作

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

“在此处输入图像描述”

一个使奇怪的事情

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

”在此处输入图像描述”

所有顶点着色器在这里:

            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))
    );
}

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))
    );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文