9

我在 three.js 中的网格上设置纹理,当它加载时,它看起来也是我想要的:

        texture = THREE.ImageUtils.loadTexture("textures/hash.png");

        texture.needsUpdate = true;

        uniforms = {
            color: { type: "c", value: new THREE.Color( 0xffffff ) },
            texture: { type: "t", value: texture },
        },  

        vertexShader = "varying vec2 vUv; void main() {vUv = uv;gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );}",

        fragmentShader = "uniform vec3 color; uniform sampler2D texture; varying vec2 vUv; void main() { vec4 tColor = texture2D( texture, vUv ); gl_FragColor = vec4( mix( color, tColor.rgb, tColor.a ), 1.0 );}",

        material = new THREE.ShaderMaterial({
            uniforms : uniforms,
            vertexShader    : vertexShader,
            fragmentShader  : fragmentShader
        });

但我想稍后改变这个网格上的纹理,我试过这个:

obj.mesh.material.uniforms.texture = THREE.ImageUtils.loadTexture("textures/1.png");
obj.mesh.material.uniforms.texture.needsUpdate = true;

但这不会改变网格上显示的纹理,如何更改 THREE.ShaderMaterial 上的纹理?

4

1 回答 1

11

将纹理分配给obj.mesh.material.uniforms.texture.value。还可以考虑needsUpdate在纹理成功加载后设置标志(通过订阅加载事件)。

于 2012-11-27T14:27:34.987 回答