【问题标题】:Problems with changing color of gltf object更改 gltf 对象颜色的问题
【发布时间】:2020-08-05 01:29:02
【问题描述】:

有了这个答案作为参考,我已经成功的改变了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

【问题讨论】:

    标签: javascript html css aframe ar.js


    【解决方案1】:

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

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

    thisglitch 中查看。

    【讨论】:

    • 感谢您的回复!我能够安全地显示对象的纹理。调整金属材质对我很有帮助。我学到了很多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多