我是第一次尝试 WebGPU,遇到了一个问题。通常在其他渲染框架中,我会将 vec4 与全 1 的矩阵相乘,输出将是相同的。
这是我对这段代码的期望:
@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
let world_position: vec4f = vec4f(in.position, 0.0, 1.0);
out.position = world_position;
out.color = in.color;
return out;
}
但是当我尝试这个时:
@vertex
fn vs_main(in: VertexInput) -> VertexOutput {
var out: VertexOutput;
let world_position: vec4f = mat4x4f(
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0,
1.0, 1.0, 1.0, 1.0
) * vec4f(in.position, 0.0, 1.0);
out.position = world_position;
out.color = in.color;
return out;
}
vec4f
的结果 mat4x4f * vec4f
中的每个值都是 mat4x4f
中的行和 vec4f
中的列的 点积。
这就像写作:
vec4f(
max4x4f[0,0]*vec4f[0] + max4x4f[1,0]*vec4f[0] + max4x4f[2,0]*vec4f[0] + max4x4f[3,0]*vec4f[0],
max4x4f[0,1]*vec4f[1] + max4x4f[1,1]*vec4f[1] + max4x4f[2,1]*vec4f[1] + max4x4f[3,1]*vec4f[1],
max4x4f[0,2]*vec4f[2] + max4x4f[1,2]*vec4f[2] + max4x4f[2,2]*vec4f[2] + max4x4f[3,2]*vec4f[2],
max4x4f[0,3]*vec4f[3] + max4x4f[1,3]*vec4f[3] + max4x4f[2,3]*vec4f[3] + max4x4f[3,3]*vec4f[3]
)
单位矩阵将是:
let world_position: vec4f = mat4x4f(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0
)