【问题标题】:Creating a new object in three.js在 three.js 中创建一个新对象
【发布时间】:2018-03-26 11:07:24
【问题描述】:

我目前正在开展一个项目,该项目涉及基于数据库在 Threejs 中制作 3D 对象。

我已经有了连接等,但是当我尝试创建一个新对象时它总是失败。

    this.type = 'CustomObject';
    let shape = new THREE.Shape();
    const maxSize = coords.reduce((prev, current) => (Math.abs(prev.value) > Math.abs(current.value)) ? prev : current); // max Size but Abs
    // console.log("Max size before check : "+ maxSize.value);
    //Check weither maxSize is positive or negative.
    let height = Math.abs(maxSize.value);

    shape.moveTo(0, 0);
    for (let i = 0; i < coords.length; i++) {
        // console.log(coords[i]);
        shape.lineTo(coords[i].id, coords[i].value);
    }

    shape.lineTo(coords[coords.length - 1].id, -height - 3);
    shape.lineTo(0, -height - 3);
    shape.lineTo(0, 0);
    // let geometry = new THREE.ExtrudeGeometry( shape, extrudeSettings );
    let extrudeSettings = {
        steps: 4,
        amount: 20,
        bevelEnabled: false,
        bevelThickness: 1,
        bevelSize: 1,
        bevelSegments: 1
    };
    this.geometry = new THREE.ExtrudeGeometry(shape, extrudeSettings);
    this.material = new THREE.MeshBasicMaterial({color: 0xCbcbcb});
    let object = new THREE.Mesh( this.geometry, this.material );
    this.createdGraph = object;
    console.log(this.createdGraph.Object3D);
    this.scene.add(this.createdGraph);
}

这只是代码的一部分,我知道它不是最好的。但我想在清理它之前先处理它。 它是根据 ES6 编写的。

问题是,如果您执行代码,它确实会创建我想要的图形。

代码创建的对象的屏幕截图。

但是,如果我尝试将它添加到 A-Frame(因为我以前使用过它),它会一直给我一个错误,即我无法将没有 Object3D 的对象添加到场景或“this.updateMorphTargets”不是函数'。

有人有什么想法吗?

PS 我也尝试过这个 https://stackoverflow.com/a/31924233 想法,但结果是“this.updateMorphTargets is not a function”错误。

谢谢:)

【问题讨论】:

    标签: javascript three.js aframe


    【解决方案1】:

    我不确定哪些部分会给您带来错误,
    我已将您的代码复制到小提琴中,添加了 3 个坐标,它是 working

    从您的代码来看,您的场景引用错误,在 a-frame 中是

    this.el.sceneEl
    

    假设this 是任何实体,而this.scene 不涉及场景。
    此外,将three.js 对象称为object3D,而不是Object3D


    我只将 three.js 对象的创建放到了 aframe 组件中:
    AFRAME.registerComponent("foo",{
      init:function(){
      //Your code here
      }
    });
    

    并放置一个实体:

    <a-entity foo> </a-entity>
    


    在添加之前,我还通过获取场景参考来放置对象:
    this.el.sceneEl.object3D.add(object)
    

    【讨论】:

    • 你先生,是一个真正的英雄。我花了一段时间才把它转换成我现有的代码,但它现在可以工作了!我确实必须通过 document.body.innerHTML += 添加 部分手册,以便它可以处理来自 GET 请求的坐标。但是,嘿,它有效!谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 2011-03-13
    • 2011-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多