在我的three.js项目中,我尝试通过单击图像来更改颜色纹理。
这是我的代码:
...
var col;
document.getElementById('image3').onclick = function() {
col=("textures/synleder/synleder_COL.png");
};
var textures = {
color: THREE.ImageUtils.loadTexture(col),
normal: THREE.ImageUtils.loadTexture('textures/synleder/synleder_NRM.jpg'),
specular: THREE.ImageUtils.loadTexture('textures/synleder/synleder_SPEC.jpg'),
};
textures.color.wrapS = textures.color.wrapT = THREE.RepeatWrapping;
textures.color.repeat.set( 2, 2);
textures.normal.wrapS = textures.normal.wrapT = THREE.RepeatWrapping;
textures.normal.repeat.set( 2, 2);
textures.specular.wrapS = textures.specular.wrapT = THREE.RepeatWrapping;
textures.specular.repeat.set( 2, 2);
var shader = THREE.ShaderLib[ "normalmap" ];
var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
uniforms[ "tNormal" ].value = textures.normal;
uniforms[ "uNormalScale" ].value.y = 2;
var material2 = new THREE.MeshPhongMaterial( {
map: textures.color,
specularMap: textures.specular,
normalMap: uniforms[ "tNormal" ].value,
normalScale: uniforms[ "uNormalScale" ].value,
} );
loader = new THREE.JSONLoader( true );
document.body.appendChild( loader.statusDomElement );
loader.load( "geo/kugel5.js", function( geometry ) { createScene( geometry, scale, material2 ) } );
...
我到现在为止尝试的是我定义了一个变量col,它应该包含我的颜色纹理的路径。通过单击image3,我的几何体上应该可以看到新的颜色纹理。不幸的是它不起作用。 经过一些研究,我找到了这个帖子:Three.js texture / image update at runtime
我认为我必须添加一些内容来更新纹理:textures.color.needsUpdate=true;
但是当我把它添加到我的代码中时:
document.getElementById('image3').onclick = function() {
col=("textures/synleder/synleder_COL.png");
textures.color.needsUpdate=true;
};
我的几何图形消失了。有没有人知道我做错了什么?
非常感谢!
答案 0 :(得分:1)
document.getElementById('image3').onclick = function() {
textures.color = THREE.ImageUtils.loadTexture("textures/synleder/synleder_COL.png",function(){
material2.color = textures.color;
textures.color.wrapS = textures.color.wrapT = THREE.RepeatWrapping;
textures.color.repeat.set( 2, 2);
textures.color.needsUpdate=true;
});
};
这应该这样做