简体   繁体   English

three.js改变材质上的纹理

[英]three.js change texture on material

I'm setting up a texture on a mesh in three.js and when it loads it looks how I want it too: 我在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
        });

but I want to change the texture that is on this mesh later on, I have tried this: 但是我想稍后改变这个网格上的纹理,我试过这个:

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

but this doesn't change the texture being displayed on the mesh, how can I change a texture on a THREE.ShaderMaterial ? 但这不会改变网格上显示的纹理,如何更改THREE.ShaderMaterial上的纹理?

Assign the texture to obj.mesh.material.uniforms.texture.value instead. 将纹理分配给obj.mesh.material.uniforms.texture.value Also consider setting the needsUpdate flag after the texture has successfully loaded (by subscribing to the load event). 还要考虑在纹理成功加载后设置needsUpdate标志(通过订阅加载事件)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM