three.js改变材质上的纹理 [英] three.js change texture on material

查看:722
本文介绍了three.js改变材质上的纹理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm setting up a texture on a mesh in three.js and when it loads it looks how I want it too:

        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;

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

but this doesn't change the texture being displayed on the mesh, how can I change a texture on a THREE.ShaderMaterial ?

推荐答案

将纹理分配给 obj.mesh.material.uniforms.texture。价值而不是。在纹理成功加载(通过订阅加载事件)后,还要考虑设置 needsUpdate 标志。

Assign the texture to obj.mesh.material.uniforms.texture.value instead. Also consider setting the needsUpdate flag after the texture has successfully loaded (by subscribing to the load event).

这篇关于three.js改变材质上的纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆