three.js改变材质上的纹理

时间:2012-11-27 11:21:31

标签: javascript textures three.js

我在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上的纹理?

1 个答案:

答案 0 :(得分:11)

将纹理指定为obj.mesh.material.uniforms.texture.value。还要考虑在纹理成功加载后设置needsUpdate标志(通过订阅加载事件)。