WebGL2RenderingContext: vertexAttribIPointer() 方法

Baseline 已廣泛支援

此功能已成熟,並可在多種裝置和瀏覽器版本上執行。自 2021 年 9 月起,所有瀏覽器均已支援此功能。

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

WebGL2RenderingContext.vertexAttribIPointer() 方法是 WebGL 2 API 的一部分,用於指定頂點屬性陣列中的整數資料格式和位置。

語法

js
vertexAttribIPointer(index, size, type, stride, offset)

引數

index

一個 GLuint,指定要修改的頂點屬性的索引。

size

一個 GLint,指定每個頂點屬性的元件數。必須是 1、2、3 或 4。

type

一個 GLenum,指定陣列中每個元件的資料型別。必須是以下之一:gl.BYTEgl.UNSIGNED_BYTEgl.SHORTgl.UNSIGNED_SHORTgl.INTgl.UNSIGNED_INT

stride(步幅)

一個 GLsizei,指定連續頂點屬性開始之間的位元組偏移量。

offset

一個 GLintptr,指定頂點屬性陣列中第一個元件的位元組偏移量。必須是 type 的倍數。

返回值

無(undefined)。

描述

WebGLRenderingContext.vertexAttribPointer() 非常相似。主要區別在於,雖然 vertexAttribPointer 指定的值在著色器中始終被解釋為浮點值(即使它們最初在緩衝區中被指定為整數),但此方法允許指定在著色器中被解釋為整數的值。

示例

線性混合蒙皮

js
// Describe the layout of the buffer:
// 1. position
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 20, 0);
gl.enableVertexAttribArray(0);
// 2. bone weights, normalized to [0, 1]
gl.vertexAttribPointer(1, 4, gl.UNSIGNED_BYTE, true, 20, 12);
gl.enableVertexAttribArray(1);
// 3. bone indices, interpreted as integer
gl.vertexAttribIPointer(2, 4, gl.UNSIGNED_BYTE, 20, 16);
gl.enableVertexAttribArray(2);

// Connect to attributes from the vertex shader
gl.bindAttribLocation(shaderProgram, 0, "position");
gl.bindAttribLocation(shaderProgram, 1, "boneWeights");
gl.bindAttribLocation(shaderProgram, 2, "boneIndices");
html
<script id="shader-vs" type="x-shader/x-vertex">
  #version 300 es

  uniform mat4 mvMatrix;
  uniform mat4 bones[120];

  in vec3 position;
  in vec4 boneWeights;
  in uvec4 boneIndices;//read as 4-component unsigned integer

  void main() {
      vec4 skinnedPosition =
          bones[boneIndices.s] * vec4(position, 1.0) * boneWeights.s +
          bones[boneIndices.t] * vec4(position, 1.0) * boneWeights.t +
          bones[boneIndices.p] * vec4(position, 1.0) * boneWeights.p +
          bones[boneIndices.q] * vec4(position, 1.0) * boneWeights.q;
      gl_Position = mvMatrix * skinnedPosition;
  }
</script>

規範

規範
WebGL 2.0 規範
# 3.7.8

瀏覽器相容性

另見