想知道是否可以在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
、vec2
、vec3
、vec4
、mat3
还是mat4
,这意味着数据将从你告诉属性提取到浮点数中转换。例如,如果您保持提取类型 =gl.BYTE
,规范化 = false,则属性中的值将为 -127.0 到 128.0 如果您说提取类型gl.UNSIGNED_BYTE
,规范化 = true,则值将为 0.0 到 1.0
WebGL2 添加了整数属性int
、ivec2
、ivec3
、ivec4
、uint
、uvec2
、uvec3
、uvec4
。
设置调用的整数属性gl.vertexAttribIPointer
我建议一些关于WebGL的教程