簡體   English   中英

three.js中的紋理更新

[英]Texture update in three.js

在我的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紋理/圖像在運行時更新

而且我認為我必須添加一些東西來更新紋理: textures.color.needsUpdate=true;

但是當我將其添加到我的代碼中時:

 document.getElementById('image3').onclick = function() { 
    col=("textures/synleder/synleder_COL.png");
    textures.color.needsUpdate=true;
    };

我的幾何消失了。 有人知道我做錯了什么嗎?

非常感謝!

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;
  });
};

這應該做

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM