如果可以在 WebGL/WebGL2 中使用大于 1 的整数比例
If you can use an integer scale greater than 1 in WebGL/WebGL2
想知道是否可以在 bufferData
中使用 Uint32Array
,所以不要这样:
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(bufferData), gl.STATIC_DRAW);
应该是这样的:
gl.bufferData(gl.ARRAY_BUFFER, new Uint32Array(bufferData), gl.STATIC_DRAW);
此外,我还看到了所有顶点示例,但它们都在 0 到 1 的范围内,例如 0.5
等。我想知道您是否可以改用更大的值,如 500 或 100000,并将比例设置成这样。所以在这种情况下,要么使用大浮点数,要么使用整数。
您可以将任何您想要的数据放入缓冲区。 WebGL 不关心。它可以是浮点数、字节、整数、无符号字节、无符号整数、短裤、无符号短裤。也可以混用。
您如何使用这些数据以及将其用于什么由您决定。该数据不必是位置数据。它可以是法线,可以是颜色,可以是粒子的速度,可以是国家的 ID,可以是任何东西。
将数据放入缓冲区后,您可以使用 gl.vertexAttribPointer
告诉 WebGL 如何取出数据。
const location = specifies the attribute to set (looked up with gl.getAttribLocation)
const size = number of elements to pull out per vertex shader iteration (1 to 4)
const type = the type of data. gl.FLOAT, gl.BYTE, gl.UNSIGNED_BYTE, gl.SHORT, etc..
const normalize = true/false. True means the value represents 0 to 1
of unsigned types or -1 to 1 for signed types
const stride = number of bytes to skip per vertex shader iteration to get the next
data piece of data. 0 = use size * sizeof(type)
const offset = number of bytes to start into the buffer
gl.vertexAttribPointer(location, size, type, normalize, stride, offset);
注意WebGL1中的所有属性都是float类型。 float
、vec2
、vec3
、vec4
、mat3
或 mat4
,这意味着数据将从您告诉要提取的属性进行转换变成一个漂浮物。例如,如果您保持 extract type = gl.BYTE
,normalize = false,则属性中的值将为 -127.0 到 128.0 如果您说 extract type gl.UNSIGNED_BYTE
,normalize = true,则属性值将为 0.0 到 1.0
WebGL2添加整数属性int
、ivec2
、ivec3
、ivec4
、uint
、uvec2
、uvec3
, uvec4
.
要设置整数属性,您调用 gl.vertexAttribIPointer
想知道是否可以在 bufferData
中使用 Uint32Array
,所以不要这样:
gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(bufferData), gl.STATIC_DRAW);
应该是这样的:
gl.bufferData(gl.ARRAY_BUFFER, new Uint32Array(bufferData), gl.STATIC_DRAW);
此外,我还看到了所有顶点示例,但它们都在 0 到 1 的范围内,例如 0.5
等。我想知道您是否可以改用更大的值,如 500 或 100000,并将比例设置成这样。所以在这种情况下,要么使用大浮点数,要么使用整数。
您可以将任何您想要的数据放入缓冲区。 WebGL 不关心。它可以是浮点数、字节、整数、无符号字节、无符号整数、短裤、无符号短裤。也可以混用。
您如何使用这些数据以及将其用于什么由您决定。该数据不必是位置数据。它可以是法线,可以是颜色,可以是粒子的速度,可以是国家的 ID,可以是任何东西。
将数据放入缓冲区后,您可以使用 gl.vertexAttribPointer
告诉 WebGL 如何取出数据。
const location = specifies the attribute to set (looked up with gl.getAttribLocation)
const size = number of elements to pull out per vertex shader iteration (1 to 4)
const type = the type of data. gl.FLOAT, gl.BYTE, gl.UNSIGNED_BYTE, gl.SHORT, etc..
const normalize = true/false. True means the value represents 0 to 1
of unsigned types or -1 to 1 for signed types
const stride = number of bytes to skip per vertex shader iteration to get the next
data piece of data. 0 = use size * sizeof(type)
const offset = number of bytes to start into the buffer
gl.vertexAttribPointer(location, size, type, normalize, stride, offset);
注意WebGL1中的所有属性都是float类型。 float
、vec2
、vec3
、vec4
、mat3
或 mat4
,这意味着数据将从您告诉要提取的属性进行转换变成一个漂浮物。例如,如果您保持 extract type = gl.BYTE
,normalize = false,则属性中的值将为 -127.0 到 128.0 如果您说 extract type gl.UNSIGNED_BYTE
,normalize = true,则属性值将为 0.0 到 1.0
WebGL2添加整数属性int
、ivec2
、ivec3
、ivec4
、uint
、uvec2
、uvec3
, uvec4
.
要设置整数属性,您调用 gl.vertexAttribIPointer