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

瀏覽器相容性

另見