ThreeJS 获取 运行 material 的统一值

ThreeJS get uniform values for running material

我在我的场景中添加了一个带有 MeshPhongMaterial 的对象。我想窥探三号给物体穿的制服。我认为它会像 obj.uniforms.uniformName 一样简单,但实际上并不存在。有没有简单的方法来获得运行时制服Three is setting?

我要的是统一名称及其值。

我想出的方法需要双循环。我在 运行 material 上看到的唯一有用的东西是 material.uniformsList,它是 WebGLUniformLocation 和值的列表,但没有名称。还有 material.program.uniforms,它是 WebGLUniformLocations 的名称的键值存储。看起来你可以将两者压缩在一起以获得统一的列表,但是有更好的方法吗?

let builtUniforms = {};

// For every uniform in the array, look for its location in the program
// uniforms to get the name
for( x = 0; uniform = object.material.uniformsList[ x++ ]; ) {

    // uniform is [ { type, value }, WebGLUniformLocation ] but no name :(

    for( uniformName in object.material.program.uniforms ) {

        // If the WebGLUniformLocations match up, we've found the name
        if( object.material.program.uniforms[ uniformName ] === uniform[ 1 ] ) {

            builtUniforms[ uniformName ] = {
                value: uniform[ 0 ].value,
                name: uniformName
            };
            break;

        }
    }
}
  1. 是否有更直接的方法从 Three.Material 获取统一名称和值?
  2. uniformsListprogram.uniforms 会改变吗?我是否需要在每个渲染循环中构建一次此对象,或者我可以构建一次并重新使用它吗?

三个存储你要找的键值存储在material.__webglShader.uniforms

另见THREE.UniformsLib

还有 THREE.ShaderLib - 例如THREE.ShaderLib.phong

这些包含内置制服的模板以及使用它们的着色器,例如 MeshPhongMaterial