gpt4 book ai didi

javascript - 更改 gltf 对象颜色的问题

转载 作者:行者123 更新时间:2023-12-01 15:39:58 25 4
gpt4 key购买 nike

有了这个答案作为引用,我已经成功改变了gltf模型的颜色。
( Change the color of an object (.dae or gltf) in "AR.JS" )
但是,模型纹理会消失。
为什么?

<!-- A FRAME -->
<script src="https://aframe.io/releases/1.0.0/aframe.min.js"></script>
<script src="https://unpkg.com/aframe-orbit-controls@1.2.0/dist/aframe-orbit-controls.min.js"></script>

<script>
AFRAME.registerComponent('carman', {
init: function(){
let el = this.el;
let self = this;
self.cars = [];
el.addEventListener("model-loaded", e => {
let car3D = el.getObject3D('mesh');
if (!car3D){return;}
 //console.log('car', car3D);
car3D.traverse(function(node){
if (node.isMesh){
// console.log(node);
self.cars.push(node);
if(node.name == "meta02t"){
self.carMat = node.material;
}
node.material.map = null;
}
});
});
el.addEventListener('changecolor', e => {
let colorp = e.detail.color;
let colorHex = Number(colorp.replace('#', '0x'));
let color3D = new THREE.Color(colorHex);
self.carMat.color = color3D;
});
}
});
AFRAME.registerComponent('click-listener', {
init: function () {
// Listen for click event
let self = this;
let el = this.el;
this.el.addEventListener('click', function (evt) {
// Call the Octahedron and trigger it's scalewave animation
let car = document.querySelector('#car');
let color = el.getAttribute('material', 'color');
car.emit('changecolor', color);
});
}
});
</script>


我的故障元素 https://glitch.com/~0421

最佳答案

没有纹理,因为一旦加载模型,您就会遍历节点并将其删除:

node.material.map = null; // removes the texture

此外 - 即使颜色改变,汽车也看起来是黑色的。 “金属”部分是粗糙的而不是金属的(粗糙度:0.5,金属度:0)。如果你改变它,汽车会改变它的颜色(视觉上):
el.addEventListener('changecolor', e =>{             
// set the new color
let colorp = e.detail.color;
let colorHex = Number(colorp.replace('#', '0x'));
let color3D = new THREE.Color(colorHex);
// apply for the "metal" materials
for (var i = 0; i < self.cars.length; i++) {
if (!self.cars[i].material) return
if (self.cars[i].name.includes("meta")) {
self.cars[i].material.metalness = 0.7
self.cars[i].material.roughness = 0.2
self.cars[i].material.color = color3D;
}
}
});

查看 this毛刺。

关于javascript - 更改 gltf 对象颜色的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61358144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com