使用 'gl.PointCoord' 时是否可以更改每个片段的深度?
Is it possible to change the depth of each fragment when using 'gl.PointCoord'?
我想知道如何调整每个片段的深度值(使用gl_FragDepth
)。我用 gl_PointSize=5.0
渲染点基元。
我知道在片段着色器中,我可以通过 gl.PointCoord.xy 访问每个片段,但我未能为每个片段重置不同的深度值?
如有任何帮助,我们将不胜感激。
在 WebGL 1.0 中,必须启用扩展 EXT_frag_depth
才能访问 gl_FragDepth
:
- WebGL EXT_frag_depth Khronos Ratified Extension Specification
- Whosebug 问题Using gl_FragDepth in WebGL
您可以写入片段着色器中的内置统一 out float gl_FragDepth;
。 gl_FragDepth
的值设置为深度缓冲区。由于深度是在片段着色器中设置的,因此它会影响单个片段。
深度的有效范围由 glDepthRange
设置。默认情况下,深度范围是从 0 到 1。0 是近的,1 是远的。
如果未设置gl_FragDepth
,则gl_FragCoord
的z
组件将被设置为深度缓冲区。
通过在顶点着色器中设置 gl_Position
的 z
组件来指定图元顶点位置的深度(在您的例子中是一个点)。如果 -gl_Position.w < gl_Position.z
和 gl_Position.z < gl_Position.w
则深度(z
分量)在剪辑 space 中。
剪辑 space 坐标通过除以 gl_Position
的 w
分量转换为规范化设备 space 坐标(透视划分,因为 gl_Position
是一个齐次坐标)。因此,标准化设备 space 的近平面和远平面之间的 z
分量在 [-1, 1].
范围内
最后将标准化设备 space 的 z
分量(范围 [-1, 1])映射到深度范围,默认情况下在 [0, 1] 中。
注意,in mediump vec2 gl_PointCoord
是片段着色器的二维输入变量。你不能写信给它。
参见OpenGL ES Shading Language 1.00 Specification, 7.2 Fragment Shader Special Variables, page 60:
The fragment shader has access to the read-only built-in variable gl_PointCoord. The values in gl_PointCoord
are two-dimensional coordinates indicating where within a point primitive the current fragment is located. They range from 0.0 to 1.0 across the point.
我想知道如何调整每个片段的深度值(使用gl_FragDepth
)。我用 gl_PointSize=5.0
渲染点基元。
我知道在片段着色器中,我可以通过 gl.PointCoord.xy 访问每个片段,但我未能为每个片段重置不同的深度值?
如有任何帮助,我们将不胜感激。
在 WebGL 1.0 中,必须启用扩展 EXT_frag_depth
才能访问 gl_FragDepth
:
- WebGL EXT_frag_depth Khronos Ratified Extension Specification
- Whosebug 问题Using gl_FragDepth in WebGL
您可以写入片段着色器中的内置统一 out float gl_FragDepth;
。 gl_FragDepth
的值设置为深度缓冲区。由于深度是在片段着色器中设置的,因此它会影响单个片段。
深度的有效范围由 glDepthRange
设置。默认情况下,深度范围是从 0 到 1。0 是近的,1 是远的。
如果未设置gl_FragDepth
,则gl_FragCoord
的z
组件将被设置为深度缓冲区。
通过在顶点着色器中设置 gl_Position
的 z
组件来指定图元顶点位置的深度(在您的例子中是一个点)。如果 -gl_Position.w < gl_Position.z
和 gl_Position.z < gl_Position.w
则深度(z
分量)在剪辑 space 中。
剪辑 space 坐标通过除以 gl_Position
的 w
分量转换为规范化设备 space 坐标(透视划分,因为 gl_Position
是一个齐次坐标)。因此,标准化设备 space 的近平面和远平面之间的 z
分量在 [-1, 1].
最后将标准化设备 space 的 z
分量(范围 [-1, 1])映射到深度范围,默认情况下在 [0, 1] 中。
注意,in mediump vec2 gl_PointCoord
是片段着色器的二维输入变量。你不能写信给它。
参见OpenGL ES Shading Language 1.00 Specification, 7.2 Fragment Shader Special Variables, page 60:
The fragment shader has access to the read-only built-in variable gl_PointCoord. The values in
gl_PointCoord
are two-dimensional coordinates indicating where within a point primitive the current fragment is located. They range from 0.0 to 1.0 across the point.