有关WebGL2通话声明,请参见本文结尾
调用函数将数据加载到纹理中时出现此错误。我正在使用Chrome 68浏览器。该代码在WebGL1
中有效,但在WebGL2
中无效。
我的WebGL参数(internalFormat,format,channelType)如下:
internalFormat: number = WebGLRenderingContext.RGBA;
format: number = WebGLRenderingContext.RGBA;
channelType: number = WebGLRenderingContext.FLOAT;
channelSize = 4;
internalFormat: number = WebGL2RenderingContext.R32F;
format: number = WebGL2RenderingContext.RED;
channelType: number = WebGLRenderingContext.FLOAT;
channelSize = 1;
在WebGL 1和2中,我启用了此扩展名:
this.textureFloatExtension = this.gl.getExtension('OES_texture_float');
在WebGL 2中,我另外启用了此功能:
this.colorBufferFloatExtension = this.gl.getExtension('EXT_color_buffer_float');
以及函数定义:
updateTexture(texture: WebGLTexture, width: number, height: number, data: Float32Array): void {
const gl = this.gl;
gl.bindTexture(gl.TEXTURE_2D, texture);
let buffer: ValidArrayTypes = data;
if (data) {
buffer = this.encoder.encode(data, width * height);
}
gl.texSubImage2D(
gl.TEXTURE_2D,
0, // level
0, // xoffset
0, // yoffset
width, height, this.encoder.format, this.encoder.channelType, buffer);
this.checkError();
gl.bindTexture(gl.TEXTURE_2D, null);
}
在调用之前使用以下内容创建纹理:
allocateTexture(width: number, height: number): WebGLTexture {
const gl = this.gl;
// Create the texture
const texture = gl.createTexture();
// Bind the texture so the following methods effect this texture.
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
gl.texImage2D(
gl.TEXTURE_2D,
0, // Level of detail.
this.encoder.internalFormat, width, height,
0, // Always 0 in OpenGL ES.
this.encoder.format, this.encoder.channelType, null);
this.checkError();
gl.bindTexture(gl.TEXTURE_2D, null);
return texture as WebGLTexture;
}
texSubImage2D(target: number, level: number, xoffset: number, yoffset: number,
width: number, height: number,
format: number, type: number, pixels?: ArrayBufferView | null): void;
texSubImage2D(target: number, level: number, xoffset: number, yoffset: number,
format: number, type: number, source: ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement): void; // May throw DOMException
texSubImage2D(target: number, level: number, xoffset: number, yoffset: number,
format: number, type: number, source: ImageBitmap | ImageData | HTMLImageElement | HTMLCanvasElement | HTMLVideoElement): void; // May throw DOMException