如何在 a-frame 中创建自定义方块

How to create a custom square in a-frame

如何创建自定义方块以在 a-frame 中注册新组件?

我正在尝试更新此处找到的示例 https://github.com/aframevr/aframe/blob/master/docs/components/geometry.md 在 "Register a Custom Geometry"

标题下

以下是我尝试过的 - 首先是 js

AFRAME.registerGeometry('example', {
      schema: {
        vertices: {
          default: ['-10 10 0', '-10 -10 0', '10 -10 0', '10 -10 0'], //added a 4th set of dimensions here
        }
      },

      init: function (data) {
        var geometry = new THREE.Geometry();
        geometry.vertices = data.vertices.map(function (vertex) {
            var points = vertex.split(' ').map(function(x){return parseInt(x);});
            return new THREE.Vector3(points[0], points[1], points[2], points[3]); //added a 4th 'points' here
        });
        geometry.computeBoundingBox();
        geometry.faces.push(new THREE.Face3(0, 1, 2, 3)); //added a 4th number here
        geometry.mergeVertices();
        geometry.computeFaceNormals();
        geometry.computeVertexNormals();
        this.geometry = geometry;
      }
    });

我在更改的地方做了评论,基本上只是在以前有三个元素的地方添加了 4 个元素

然后我将其添加到 html

<a-entity geometry="primitive: example; vertices: 1 1 -3, 3 1 -3, 2 2 -3, 1 2 -3"></a-entity>

但它似乎只是忽略了我的新顶点而只是创建了一个三角形。我怀疑 three.js 代码有一些我不理解的地方,而且我似乎无法通过查看他们的文档来解决它 - 任何帮助将不胜感激。

这似乎有效;

AFRAME.registerGeometry('example', {
      schema: {
        vertices: {
          default: ['-10 10 0', '-10 -10 0', '10 -10 0', '10 -10 0'],
        }
      },

      init: function (data) {
        var geometry = new THREE.Geometry();
        geometry.vertices = data.vertices.map(function (vertex) {
            var points = vertex.split(' ').map(function(x){return parseInt(x);});
            return new THREE.Vector3(points[0], points[1], points[2]);
        });
        geometry.computeBoundingBox();
        geometry.faces.push(new THREE.Face3(0, 1, 2));
        geometry.faces.push(new THREE.Face3(0, 2, 3));
        geometry.mergeVertices();
        geometry.computeFaceNormals();
        geometry.computeVertexNormals();
        this.geometry = geometry;
      }
    });

在 html

中再次跟进
<a-entity geometry="primitive: example; vertices: 1 1 -3, 3 1 -3, 2 2 -3, 1 2 -3"></a-entity>