代码之家  ›  专栏  ›  技术社区  ›  ioan

在“三”中渲染网格3D顶点。js(面)

  •  0
  • ioan  · 技术社区  · 9 年前

    给定AutoCad API以向量数组{X:,Y:,Z:}的形式生成的200多个顶点,我尝试将它们渲染为三个。js没有运气。

    目前,我正在为数字200进行所有可能的排列,并将所有顶点连接在一起- 我不认为是这样做的,因为它提供了超过20万张脸。

    编辑: 我的AutoCAD代码将获取所有顶点,然后尝试获取其连接顶点的ID(vertex1和vertex2)。但GetHashCode()不起作用。问题并不是它返回了巨大的id号,如148335760和682610240。问题是这些id不是唯一的,它们碰巧被定义了,并且没有连接到任何其他顶点。

    AutoCAD代码:

    //data structures for serialisation
    public class EdgeMe
    {
        public int vertex1;
        public int vertex2;
    }
    public class VertexMe
    {
        public int id;
        public Point3d Point;
        public List<EdgeMe> Edges = new List<EdgeMe>();
    }
    
    public class DataMe{
        public Extents3d extents;
        public string layer;
        public List<VertexMe> points = new List<VertexMe>();
    }
    
    
    //...
    
    
    // Get each Solid3d in modelspace and add its extents
    // to the list
    foreach (var id in ms)
    {
        var obj = tr.GetObject(id, OpenMode.ForRead);
        var sol = obj as Solid3d;
    
        DataMe dataMe = new DataMe();
        if (sol != null)
        {
            dataMe.extents = sol.GeometricExtents;
            dataMe.layer = sol.Layer;
            using (var brep = new Autodesk.AutoCAD.BoundaryRepresentation.Brep(sol))
            {
                foreach (var vertex in brep.Vertices)
                {
                    VertexMe vertexMe = new VertexMe();
                    vertexMe.Point = vertex.Point;
                    vertexMe.id = vertex.Brep.GetHashCode();
                    foreach(var edge in vertex.Edges)
                    {
                        EdgeMe edgeMe = new EdgeMe();
                        edgeMe.vertex1 = edge.Vertex1.Brep.GetHashCode();
                        edgeMe.vertex2 = edge.Vertex2.Brep.GetHashCode();
                        vertexMe.Edges.Add(edgeMe);
                    }
    
                    dataMe.points.Add(vertexMe);
                }
            }
        }
        sols.Add(dataMe);
    }
    

    Java脚本代码:

    var faces = function(vertices) {
        var results = [];
    
        var vertex = [0, 1, 2];
        results.push(vertex.slice());
    
        while(true) {
            vertex[2]++;
            if(vertex[2] > vertices) {
    
                vertex[1]++;
                if(vertex[1] >= vertices) {
                    vertex[0]++;
                    vertex[1] = vertex[0] + 1;
    
                    if(vertex[0] > vertices - 2)
                        return results;
                }
    
                vertex[2] = vertex[1] + 1;
            }
    
            results.push(vertex.slice());
        }
    
    };
    
    var generate = function( ... ) {
        // Process each box, adding it to the scene
        for (var sol in sols) { 
            var s = sols[sol];
            var vertices = [];
    
            for(var vertix in s.points) {// deserialize
                vertix = s.points[vertix];
    
                vertices.push(new THREE.Vector3(vertix.X, vertix.Y, vertix.Z));
            }
    
            var holes = [];
            var triangles, mesh;
            var geometry = new THREE.Geometry();
            geometry.vertices = vertices;
            var xfaces = faces(vertices.length);
    
            for(var i = 0; i < xfaces.length; i++) {
                geometry.faces.push( new THREE.Face3( xfaces[i][0], xfaces[i][1], xfaces[i][2] ));
            }
            geometry.computeFaceNormals();
            geometry.computeVertexNormals();
    
            mesh = new THREE.Mesh( geometry, customimg );
    
            mesh.rotation.set( Math.PI/2, 0, 0);
    
            root.add(mesh);
        }
    }
    

    当做

    伊昂

    2 回复  |  直到 9 年前
        1
  •  0
  •   gaitat    9 年前

        2
  •  0
  •   Augusto Goncalves    9 年前

    相反,我建议你使用 Forge Model Derivative API 从文件中提取可视内容。翻译完文件后,可以使用 Viewer (基于Three.js,但针对工程文件进行了优化)。您可以在以下位置找到几个示例: this Github .