属于 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]()
Sets the uniform's value to the specified value, which may be a single floating point or integer number, or a 2-4 component vector specified either as a list of values or as a Float32Array or Int32Array .
uniformMatrix[234][fv]()
Sets the uniform's value to the specified matrix, possibly with transposition. The value is represented as a sequence of 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.

The possible values correspond to the uniform names returned by getActiveUniform ; see that function for specifics on how declared uniforms map to uniform location names.

Additionally, for uniforms declared as arrays, the following names are also valid:
  • The uniform name without the [0] suffix. E.g. the location returned for arrayUniform is equivalent to the one for arrayUniform[0] .
  • The uniform name indexed with an integer. E.g. the location returned for 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]);
					
This code snippet is taken from the function 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 .
  • The uniform uRotationVector is set to the contents of the variable currentRotation . This, too, is a 2-component vertex.
  • Finally, the uniform 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.

浏览器兼容性

更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
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

图例

完整支持

完整支持

另请参阅

元数据

  • 最后修改:
  1. WebGL API
  2. WebGLRenderingContext
  3. 特性
    1. canvas
    2. drawingBufferHeight
    3. drawingBufferWidth
  4. 方法
    1. activeTexture()
    2. attachShader()
    3. bindAttribLocation()
    4. bindBuffer()
    5. bindFramebuffer()
    6. bindRenderbuffer()
    7. bindTexture()
    8. blendColor()
    9. blendEquation()
    10. blendEquationSeparate()
    11. blendFunc()
    12. blendFuncSeparate()
    13. bufferData()
    14. bufferSubData()
    15. checkFramebufferStatus()
    16. clear()
    17. clearColor()
    18. clearDepth()
    19. clearStencil()
    20. colorMask()
    21. commit()
    22. compileShader()
    23. compressedTexImage[23]D()
    24. compressedTexSubImage2D()
    25. copyTexImage2D()
    26. copyTexSubImage2D()
    27. createBuffer()
    28. createFramebuffer()
    29. createProgram()
    30. createRenderbuffer()
    31. createShader()
    32. createTexture()
    33. cullFace()
    34. deleteBuffer()
    35. deleteFramebuffer()
    36. deleteProgram()
    37. deleteRenderbuffer()
    38. deleteShader()
    39. deleteTexture()
    40. depthFunc()
    41. depthMask()
    42. depthRange()
    43. detachShader()
    44. disable()
    45. disableVertexAttribArray()
    46. drawArrays()
    47. drawElements()
    48. enable()
    49. enableVertexAttribArray()
    50. finish()
    51. flush()
    52. framebufferRenderbuffer()
    53. framebufferTexture2D()
    54. frontFace()
    55. generateMipmap()
    56. getActiveAttrib()
    57. getActiveUniform()
    58. getAttachedShaders()
    59. getAttribLocation()
    60. getBufferParameter()
    61. getContextAttributes()
    62. getError()
    63. getExtension()
    64. getFramebufferAttachmentParameter()
    65. getParameter()
    66. getProgramInfoLog()
    67. getProgramParameter()
    68. getRenderbufferParameter()
    69. getShaderInfoLog()
    70. getShaderParameter()
    71. getShaderPrecisionFormat()
    72. getShaderSource()
    73. getSupportedExtensions()
    74. getTexParameter()
    75. getUniform()
    76. getUniformLocation()
    77. getVertexAttrib()
    78. getVertexAttribOffset()
    79. hint()
    80. isBuffer()
    81. isContextLost()
    82. isEnabled()
    83. isFramebuffer()
    84. isProgram()
    85. isRenderbuffer()
    86. isShader()
    87. isTexture()
    88. lineWidth()
    89. linkProgram()
    90. pixelStorei()
    91. polygonOffset()
    92. readPixels()
    93. renderbufferStorage()
    94. sampleCoverage()
    95. scissor()
    96. shaderSource()
    97. stencilFunc()
    98. stencilFuncSeparate()
    99. stencilMask()
    100. stencilMaskSeparate()
    101. stencilOp()
    102. stencilOpSeparate()
    103. texImage2D()
    104. texParameter[fi]()
    105. texSubImage2D()
    106. uniform[1234][fi][v]()
    107. uniformMatrix[234]fv()
    108. useProgram()
    109. validateProgram()
    110. vertexAttrib[1234]f[v]()
    111. vertexAttribPointer()
    112. viewport()
  5. WebGL 相关页面
    1. ANGLE_instanced_arrays
    2. EXT_blend_minmax
    3. EXT_color_buffer_half_float
    4. EXT_disjoint_timer_query
    5. EXT_frag_depth
    6. EXT_sRGB
    7. EXT_shader_texture_lod
    8. EXT_texture_filter_anisotropic
    9. OES_element_index_uint
    10. OES_standard_derivatives
    11. OES_texture_float
    12. OES_texture_float_linear
    13. OES_texture_half_float
    14. OES_texture_half_float_linear
    15. OES_vertex_array_object
    16. WEBGL_color_buffer_float
    17. WEBGL_compressed_texture_atc
    18. WEBGL_compressed_texture_etc1
    19. WEBGL_compressed_texture_pvrtc
    20. WEBGL_compressed_texture_s3tc
    21. WEBGL_compressed_texture_s3tc_srgb
    22. WEBGL_debug_renderer_info
    23. WEBGL_debug_shaders
    24. WEBGL_depth_texture
    25. WEBGL_draw_buffers
    26. WEBGL_lose_context
    27. WebGL2RenderingContext
    28. WebGLActiveInfo
    29. WebGLBuffer
    30. WebGLContextEvent
    31. WebGLFramebuffer
    32. WebGLObject
    33. WebGLProgram
    34. WebGLQuery
    35. WebGLRenderbuffer
    36. WebGLSampler
    37. WebGLShader
    38. WebGLShaderPrecisionFormat
    39. WebGLSync
    40. WebGLTexture
    41. WebGLTransformFeedback
    42. WebGLUniformLocation
    43. WebGLVertexArrayObject