位置向量的z和w分量在WGSL中好像被交换了。

huangapple go评论66阅读模式
英文:

z and w components of position vector act like they are switched in WGSL

问题

以下是您要翻译的代码部分:

struct Vertex {
	@builtin(position) position : vec4<f32>,
	@location(0) color : vec4<f32>,
}

@vertex
fn vMain(@builtin(vertex_index) vertexIndex: u32) -> Vertex {
	var positions = array<vec4<f32>, 3> (
		vec4<f32>(0, 0.5, 0, 1),
		vec4<f32>(-0.5, -0.5, 0, 1),
		vec4<f32>(0.5, -0.5, 0, 1),
	);

	var output: Vertex;
	output.position = positions[vertexIndex];
	output.color = vec4<f32>(1, 1, 1, 1);

	return output;
}

@fragment
fn fMain(@location(0) color: vec4<f32>) -> @location(0) vec4<f32> {
	return color;
}

希望这对您有所帮助。如果您有其他问题,请随时提问。

英文:

I have the following WGSL shader that creates a triangle:

struct Vertex {
	@builtin(position) position : vec4&lt;f32&gt;,
	@location(0) color : vec4&lt;f32&gt;,
}

@vertex
fn vMain(@builtin(vertex_index) vertexIndex: u32) -&gt; Vertex {
	var positions = array&lt;vec4&lt;f32&gt;, 3&gt; (
		vec4&lt;f32&gt;(0, 0.5, 0, 1),
		vec4&lt;f32&gt;(-0.5, -0.5, 0, 1),
		vec4&lt;f32&gt;(0.5, -0.5, 0, 1),
	);

	var output: Vertex;
	output.position = positions[vertexIndex];
	output.color = vec4&lt;f32&gt;(1, 1, 1, 1);

	return output;
}

@fragment
fn fMain(@location(0) color: vec4&lt;f32&gt;) -&gt; @location(0) vec4&lt;f32&gt; {
	return color;
}

According to everything I've found the 4 component vectors should be treated as x, y, z, and w in that order. The problem is that when I change the w component, it acts as if I am changing the z; the vertices of the triangle look as if they are getting farther or nearer. And when I change the z, the triangle never changes shape but instead will be partially clipped sometimes.

Changing z of top vertex to 2:
位置向量的z和w分量在WGSL中好像被交换了。

Changing w of left vertex to 2:位置向量的z和w分量在WGSL中好像被交换了。

I do not understand why this effect is happening.

答案1

得分: 0

W 在正常的 3D 变换流程中有特殊的含义。它用于将透视应用于模型。虽然你从顶点着色器返回了一个 4D 向量,但它经过了所谓的透视除法,因此你应该考虑返回的是 3D 向量 [x/w, y/w, z/w]。请注意,这个问题不仅适用于 WebGPU,而且在所有 3D 库中都是相同的。

你可以在不同的地方阅读更多关于这个主题的信息:

英文:

W has a special meaning in the normal 3d transform pipeline. It is used to apply perspective to a model. While you are returning a 4d vector from the vertex shader, that is put through what is called a perspective divide, so in stead you should think that you are returning the 3d vector [x/w, y/w, z/w]. Note that this question is not webgpu specific and is the same through all 3d libraries.

You can read more about this in various places:

huangapple
  • 本文由 发表于 2023年5月21日 05:27:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76297413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定