WebGLRenderingContext: vertexAttrib[1234]f[v]() 方法

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

注意:此功能在 Web Workers 中可用。

WebGLRenderingContext.vertexAttrib[1234]f[v]() 方法是 WebGL API 的一部分,用于为通用顶点属性指定常量值。

语法

js
vertexAttrib1f(index, v0)
vertexAttrib2f(index, v0, v1)
vertexAttrib3f(index, v0, v1, v2)
vertexAttrib4f(index, v0, v1, v2, v3)

vertexAttrib1fv(index, value)
vertexAttrib2fv(index, value)
vertexAttrib3fv(index, value)
vertexAttrib4fv(index, value)

参数

index

一个 GLuint,指定要修改的顶点属性的位置。

v0, v1, v2, v3

一个浮点 Number,表示顶点属性的值。

value

一个 Float32Array,用于浮点向量顶点属性的值。

返回值

无(undefined)。

描述

虽然顶点属性通常用于为每个顶点指定不同的值(使用 vertexAttribPointer),但指定一个常量值也是有用的。例如,如果你有一个着色器,其中有一个 color 顶点属性,但你想让所有内容都以单一颜色绘制,你可以使用 vertexAttrib 来实现这一点,而无需创建仅包含一个值的缓冲区,也无需创建使用 uniform 来表示颜色的单独着色器。

如果绑定的数组缓冲区没有通过 enableVertexAttribArray 启用,则将使用此值。

属性可以是矩阵,在这种情况下,矩阵的列必须加载到连续的顶点属性槽中。

使用 vertexAttrib 设置的值是全局上下文的;也就是说,它们不属于着色器状态(例如,通用顶点属性索引到着色器变量绑定),也不属于顶点数组对象状态(例如,启用的顶点属性数组)。更改这些值的唯一方法是再次调用此函数。

示例

js
const a_foobar = gl.getAttribLocation(shaderProgram, "foobar");
// Either set each component individually:
gl.vertexAttrib3f(a_foobar, 10.0, 5.0, 2.0);
// Or provide a Float32Array:
const floatArray = new Float32Array([10.0, 5.0, 2.0]);
gl.vertexAttrib3fv(a_foobar, floatArray);
js
// We want to load the following 3x3 matrix into attribute named "matrix3x3"
// 0 1 2
// 3 4 5
// 6 7 8
const matrix3x3Location = gl.getAttribLocation(shaderProgram, "matrix3x3");
gl.vertexAttrib3f(matrix3x3Location, 0, 3, 6);
gl.vertexAttrib3f(matrix3x3Location + 1, 1, 4, 7);
gl.vertexAttrib3f(matrix3x3Location + 2, 2, 5, 8);

规范

规范
WebGL 规范
# 5.14.10

浏览器兼容性

另见