Using WebGL extensions
WebGL,像它的姐妹API (OpenGL and OpenGL ES),支持使用扩展(extensions)。一份完整的扩展列表可在 khronos webgl extension registry。
规范扩展名,供应商前缀和首选项
扩展(extensions)在未被官方正式制定为标准前,某些浏览器厂商可能会支持WebGL扩展 (but only when they are in draft stage)。这样的话,扩展的名字应该加上相应的厂商前缀 (MOZ_
, WEBKIT_
, etc.),否则这个扩展只能在浏览器切换了偏好设置的前提下生效。
If you wish to work with the bleeding edge of extensions, and want to keep working on upon ratification (assuming, of course, that the extension doesn't change in incompatible ways), that you query the canonical extension name as well as the vendor extension name. For instance:
var ext = (
gl.getExtension('OES_vertex_array_object') ||
gl.getExtension('MOZ_OES_vertex_array_object') ||
gl.getExtension('WEBKIT_OES_vertex_array_object')
);
Note that, vendor prefix have been discouraged more and more and thus most browser implement experimental extensions behind a feature flag rather than vendor prefix.
The feature flags are:
webgl.enable-draft-extensions
in Firefoxchrome://flags/#enable-webgl-draft-extensions
in Chromium based browsers (Chrome, Opera).
命名约定
WebGL extensions are prefixed with "ANGLE", "OES", "EXT" or "WEBGL". These prefixes reflect origin and intent:
- ANGLE_: Extensions that are written by the ANGLE library authors.
- OES_: Extensions that mirror functionality from OpenGL ES or OpenGL API extensions approved by the respective architecture review boards.
- EXT_: Extensions that mirror other OpenGL ES or OpenGL API extensions.
- WEBGL_: Extensions that are WebGL-specific and intended to be compatible with multiple web browsers. It should also be used for extensions which originated with the OpenGL ES or OpenGL APIs, but whose behavior has been significantly altered.
查询可用的扩展程序
The WebGL context supports querying what extensions are available.
var available_extensions = gl.getSupportedExtensions();
The WebGLRenderingContext.getSupportedExtensions()
method returns an array of strings, one for each supported extension.
扩展列表
The current extensions are:
ANGLE_instanced_arrays
EXT_blend_minmax
EXT_color_buffer_float
EXT_color_buffer_half_float
EXT_disjoint_timer_query
EXT_float_blend
ExperimentalEXT_frag_depth
EXT_shader_texture_lod
EXT_sRGB
EXT_texture_compression_bptc
EXT_texture_compression_rgtc
EXT_texture_filter_anisotropic
EXT_texture_norm16
KHR_parallel_shader_compile
OES_element_index_uint
OES_fbo_render_mipmap
OES_standard_derivatives
OES_texture_float
OES_texture_float_linear
OES_texture_half_float
OES_texture_half_float_linear
OES_vertex_array_object
OVR_multiview2
WEBGL_color_buffer_float
WEBGL_compressed_texture_astc
WEBGL_compressed_texture_etc
WEBGL_compressed_texture_etc1
WEBGL_compressed_texture_pvrtc
WEBGL_compressed_texture_s3tc
WEBGL_compressed_texture_s3tc_srgb
WEBGL_debug_renderer_info
WEBGL_debug_shaders
WEBGL_depth_texture
WEBGL_draw_buffers
WEBGL_lose_context
WEBGL_multi_draw
启用一个扩展
Before an extension can be used it has to be enabled using WebGLRenderingContext.getExtension()
. For example:
var float_texture_ext = gl.getExtension('OES_texture_float');
The return value is null
if the extension is not supported, or an extension object otherwise.
扩展对象
If an extension defines specific symbols or functions that are not available in the core specification of WebGL, they will be available on the extension object returned by the call to gl.getExtension()
.