属于
WebGL API
,
WebGLRenderingContext
方法
getUniformLocation()
returns the location of a specific
uniform
variable which is part of a given
WebGLProgram
.
The uniform variable is returned as a
WebGLUniformLocation
object, which is an opaque identifier used to specify where in the GPU's memory that uniform variable is located.
Once you have the uniform's location, you can access the uniform itself using one of the other uniform access methods, passing in the uniform location as one of the inputs:
getUniform()
Returns the value of the uniform at the given location.
uniform[1234][fi][v]()
Float32Array
or
Int32Array
.
uniformMatrix[234][fv]()
GLfloat
values or as a
Float32Array
.
The uniform itself is declared in the shader program using GLSL.
WebGLUniformLocation = WebGLRenderingContext.getUniformLocation(program, name);
program
WebGLProgram
in which to locate the specified uniform variable.
名称
DOMString
specifying the name of the uniform variable whose location is to be returned. The name can't have any whitespace in it, and you can't use this function to get the location of any uniforms starting with the reserved string
"gl_"
, since those are internal to the WebGL layer.
getActiveUniform
; see that function for specifics on how declared uniforms map to uniform location names.
[0]
suffix. E.g. the location returned for
arrayUniform
is equivalent to the one for
arrayUniform[0]
.
arrayUniform[2]
would point directly to the third entry of the
arrayUniform
uniform.
A
WebGLUniformLocation
value indicating the location of the named variable, if it exists. If the specified variable doesn't exist,
null
被返回取而代之。
WebGLUniformLocation
is an opaque value used to uniquely identify the location in the GPU's memory at which the uniform variable is located. With this value in hand, you can call other WebGL methods to access the value of the uniform variable.
WebGLUniformLocation
type is compatible with the
GLint
type when specifying the index or location of a uniform attribute.
The following errors may occur; to check for errors after
getUniformLocation()
returns, call
getError()
.
GL_INVALID_VALUE
program
parameter is not a value or object generated by WebGL.
GL_INVALID_OPERATION
program
parameter doesn't correspond to a GLSL program generated by WebGL, or the specified program hasn't been linked successfully.
In this example, taken from the
animateScene()
method in the article
A basic 2D WebGL animation example
, obtains the locations of three uniforms from the shading program, then sets the value of each of the three uniforms.
gl.useProgram(shaderProgram);
uScalingFactor =
gl.getUniformLocation(shaderProgram, "uScalingFactor");
uGlobalColor =
gl.getUniformLocation(shaderProgram, "uGlobalColor");
uRotationVector =
gl.getUniformLocation(shaderProgram, "uRotationVector")
gl.uniform2fv(uScalingFactor, currentScale);
gl.uniform2fv(uRotationVector, currentRotation);
gl.uniform4fv(uGlobalColor, [0.1, 0.7, 0.2, 1.0]);
animateScene()
in "A basic 2D WebGL animation example." See that article for the full sample and to see the resulting animation in action.
After setting the current shading program to
shaderProgram
, this code fetches the three uniforms
"uScalingFactor"
,
"uGlobalColor"
,和
"uRotationVector"
,调用
getUniformLocation()
once for each uniform.
Then the three uniforms' values are set:
uScalingFactor
uniform — a 2-component vertex — receives the horizontal and vertical scaling factors from the variable
currentScale
.
uRotationVector
is set to the contents of the variable
currentRotation
. This, too, is a 2-component vertex.
uGlobalColor
is set to the color
[0.1, 0.7, 0.2, 1.0]
, the components in this 4-component vector represent the values of red, green, blue, and alpha, respectively.
Having done this, the next time the shading functions are called, their own variables named
uScalingFactor
,
uGlobalColor
,和
uRotationVector
will all have the values provided by the JavaScript code.
| 规范 | 状态 | 注释 |
|---|---|---|
|
WebGL 1.0
The definition of 'getUniformLocation' in that specification. |
推荐 | 初始定义。 |
|
OpenGL ES 2.0
The definition of 'glGetUniformLocation' in that specification. |
标准 | Man page of the OpenGL API. |
The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
更新 GitHub 上的兼容性数据| 桌面 | 移动 | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
getUniformLocation
|
Chrome 9 | Edge 12 | Firefox 4 | IE 11 | Opera 12 | Safari 5.1 | WebView Android Yes | Chrome Android 25 | Firefox Android Yes | Opera Android 12 | Safari iOS 8 | Samsung Internet Android 1.5 |
完整支持
WebGLRenderingContext.getAttribLocation()
WebGLRenderingContext.getActiveUniform()
WebGLUniformLocation
WebGLRenderingContext
activeTexture()
attachShader()
bindAttribLocation()
bindBuffer()
bindFramebuffer()
bindRenderbuffer()
bindTexture()
blendColor()
blendEquation()
blendEquationSeparate()
blendFunc()
blendFuncSeparate()
bufferData()
bufferSubData()
checkFramebufferStatus()
clear()
clearColor()
clearDepth()
clearStencil()
colorMask()
commit()
compileShader()
compressedTexImage[23]D()
compressedTexSubImage2D()
copyTexImage2D()
copyTexSubImage2D()
createBuffer()
createFramebuffer()
createProgram()
createRenderbuffer()
createShader()
createTexture()
cullFace()
deleteBuffer()
deleteFramebuffer()
deleteProgram()
deleteRenderbuffer()
deleteShader()
deleteTexture()
depthFunc()
depthMask()
depthRange()
detachShader()
disable()
disableVertexAttribArray()
drawArrays()
drawElements()
enable()
enableVertexAttribArray()
finish()
flush()
framebufferRenderbuffer()
framebufferTexture2D()
frontFace()
generateMipmap()
getActiveAttrib()
getActiveUniform()
getAttachedShaders()
getAttribLocation()
getBufferParameter()
getContextAttributes()
getError()
getExtension()
getFramebufferAttachmentParameter()
getParameter()
getProgramInfoLog()
getProgramParameter()
getRenderbufferParameter()
getShaderInfoLog()
getShaderParameter()
getShaderPrecisionFormat()
getShaderSource()
getSupportedExtensions()
getTexParameter()
getUniform()
getUniformLocation()
getVertexAttrib()
getVertexAttribOffset()
hint()
isBuffer()
isContextLost()
isEnabled()
isFramebuffer()
isProgram()
isRenderbuffer()
isShader()
isTexture()
lineWidth()
linkProgram()
pixelStorei()
polygonOffset()
readPixels()
renderbufferStorage()
sampleCoverage()
scissor()
shaderSource()
stencilFunc()
stencilFuncSeparate()
stencilMask()
stencilMaskSeparate()
stencilOp()
stencilOpSeparate()
texImage2D()
texParameter[fi]()
texSubImage2D()
uniform[1234][fi][v]()
uniformMatrix[234]fv()
useProgram()
validateProgram()
vertexAttrib[1234]f[v]()
vertexAttribPointer()
viewport()
ANGLE_instanced_arrays
EXT_blend_minmax
EXT_color_buffer_half_float
EXT_disjoint_timer_query
EXT_frag_depth
EXT_sRGB
EXT_shader_texture_lod
EXT_texture_filter_anisotropic
OES_element_index_uint
OES_standard_derivatives
OES_texture_float
OES_texture_float_linear
OES_texture_half_float
OES_texture_half_float_linear
OES_vertex_array_object
WEBGL_color_buffer_float
WEBGL_compressed_texture_atc
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
WebGL2RenderingContext
WebGLActiveInfo
WebGLBuffer
WebGLContextEvent
WebGLFramebuffer
WebGLObject
WebGLProgram
WebGLQuery
WebGLRenderbuffer
WebGLSampler
WebGLShader
WebGLShaderPrecisionFormat
WebGLSync
WebGLTexture
WebGLTransformFeedback
WebGLUniformLocation
WebGLVertexArrayObject