URP着色器,几乎相同的代码影响不同
我正在研究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);
that one made weird things
float worldPos = mul(unity_ObjectToWorld, v.positionOS);
v.positionOS = worldPos;
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论