改变 gltf 对象颜色的问题
Problems with changing color of gltf object
参考这个答案,我已经成功更改了gltf模型的颜色。
()
但是,模型纹理将消失。
为什么?
<!-- 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
此外-即使换了颜色,这辆车看起来也是黑色的。 "metal" 部分是粗糙的而不是金属的(粗糙度: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 故障中查看。
参考这个答案,我已经成功更改了gltf模型的颜色。
(
<!-- 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
此外-即使换了颜色,这辆车看起来也是黑色的。 "metal" 部分是粗糙的而不是金属的(粗糙度: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 故障中查看。