--- title: 'WebGLRenderingContext.compressedTexImage[23]D()' slug: Web/API/WebGLRenderingContext/compressedTexImage2D translation_of: Web/API/WebGLRenderingContext/compressedTexImage2D ---
{{APIRef("WebGL")}}

下面这两个function:

 WebGLRenderingContext.compressedTexImage2D()  and WebGL2RenderingContext.compressedTexImage3D()WebGL API 中特指压缩二维或三维纹理图像的格式。

在使用这些方法之前,必须通过 WebGL extensions, 也就是 WebGL扩展启用压缩图像格式。

Syntax

// WebGL 1:
void gl.compressedTexImage2D(target, level, internalformat, width, height, border, ArrayBufferView? pixels);

// Additionally available in WebGL 2:
// read from buffer bound to gl.PIXEL_UNPACK_BUFFER
void gl.compressedTexImage2D(target, level, internalformat, width, height, border, GLsizei imageSize, GLintptr offset);
void gl.compressedTexImage2D(target, level, internalformat, width, height, border,
                             ArrayBufferView srcData, optional srcOffset, optional srcLengthOverride);

 // read from buffer bound to gl.PIXEL_UNPACK_BUFFER
void gl.compressedTexImage3D(target, level, internalformat, width, height, depth, border, GLsizei imageSize, GLintptr offset);
void gl.compressedTexImage3D(target, level, internalformat, width, height, depth, border,
                             ArrayBufferView srcData, optional srcOffset, optional srcLengthOverride);

Parameters

target
A {{domxref("GLenum")}} specifying the binding point (target) of the active texture. Possible values for compressedTexImage2D: Possible values for compressedTexImage3D:
level
A {{domxref("GLint")}} specifying the level of detail. Level 0 is the base image level and level n is the nth mipmap reduction level.
internalformat
A {{domxref("GLenum")}} specifying the compressed image format. Compressed image formats must be enabled by WebGL extensions before using this method. All values are possible for compressedTexImage2D. See compressed texture formats for which are valid for compressedTexImage3D. Possible values:
width
A {{domxref("GLsizei")}} specifying the width of the texture.
height
A {{domxref("GLsizei")}} specifying the height of the texture.
depth
A {{domxref("GLsizei")}} specifying the depth of the texture/the number of textures in a TEXTURE_2D_ARRAY.
border
A {{domxref("GLint")}} specifying the width of the border. Must be 0.
imageSize
A {{domxref("GLsizei")}} specifying the number of bytes to read from the buffer bound to gl.PIXEL_UNPACK_BUFFER.
offset
A {{domxref("GLintptr")}} specifying the offset in bytes from which to read from the buffer bound to gl.PIXEL_UNPACK_BUFFER.
pixels
An {{domxref("ArrayBufferView")}} that be used as a data store for the compressed image data in memory.

Return value

None.

Examples

var ext = (
  gl.getExtension('WEBGL_compressed_texture_s3tc') ||
  gl.getExtension('MOZ_WEBGL_compressed_texture_s3tc') ||
  gl.getExtension('WEBKIT_WEBGL_compressed_texture_s3tc')
);

var texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.compressedTexImage2D(gl.TEXTURE_2D, 0, ext.COMPRESSED_RGBA_S3TC_DXT5_EXT, 512, 512, 0, textureData);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);

Specifications

Specification Status Comment
{{SpecName('WebGL', "#COMPRESSEDTEXIMAGE2D", "compressedTexImage2D")}} {{Spec2('WebGL')}} Initial definition for WebGL.
{{SpecName('OpenGL ES 2.0', "glCompressedTexImage2D.xml", "glCompressedTexImage2D")}} {{Spec2('OpenGL ES 2.0')}} Man page of the (similar) OpenGL ES 2.0 API.
{{SpecName('OpenGL ES 3.0', "glCompressedTexImage2D.xhtml", "glCompressedTexImage2D")}} {{Spec2('OpenGL ES 3.0')}} Man page of the (similar) OpenGL ES 3.0 API.

Browser compatibility

compressedTexImage2D

{{Compat("api.WebGLRenderingContext.compressedTexImage2D")}}

compressedTexImage3D

{{Compat("api.WebGL2RenderingContext.compressedTexImage3D")}}

See also