public SCNGeometry ImportOBJ(string path)
    {
        string[] fileContents = File.ReadLines(path).ToArray();

        GetLines(fileContents);
        polygonCount = indexLines.Length;

        int[] vertexIndexPrefix = SplitIndices(indexLines).Item1;
        int[] vertexIndices     = SplitIndices(indexLines).Item2;
        int[] normalIndices     = SplitIndices(indexLines).Item3;

        SCNVector3[] vertexPoints = LinesToPoints(vertexLines);
        SCNVector3[] normalPoints = LinesToPoints(normalLines);

        SCNVector3[] vertices = OrderPoints(vertexPoints, vertexIndices);
        SCNVector3[] normals  = OrderPoints(normalPoints, normalIndices);

        vertexIndices = ReorderIndices(vertexIndexPrefix, vertexIndices);

        NSData vertexIndexData = IndexData(vertexIndices);

        SCNGeometrySource  vertexSource  = SCNGeometrySource.FromVertices(vertices);
        SCNGeometrySource  normalSource  = SCNGeometrySource.FromNormals(normals);
        SCNGeometryElement vertexElement = SCNGeometryElement.FromData(vertexIndexData, SCNGeometryPrimitiveType.Polygon, polygonCount, sizeof(int));

        return(SCNGeometry.Create(new[] { vertexSource, normalSource }, new[] { vertexElement }));
    }
Esempio n. 2
0
        private SCNGeometry CreateTrailMesh(List <SCNVector3> positions, List <SCNVector4> colors)
        {
            SCNGeometry result = null;

            if (positions.Count >= 4)
            {
                var array = new byte[positions.Count];
                for (byte i = 0; i < positions.Count; i++)
                {
                    array[i] = i;
                }

                var positionSource = SCNGeometrySource.FromVertices(positions.ToArray());
                var colorSource    = SCNGeometrySourceExtensions.Create(colors);
                var element        = SCNGeometryElement.FromData(NSData.FromArray(array), SCNGeometryPrimitiveType.TriangleStrip, positions.Count - 2, 2);
                result = SCNGeometry.Create(new SCNGeometrySource[] { positionSource, colorSource }, new SCNGeometryElement[] { element });

                var material = result.FirstMaterial;
                if (material == null)
                {
                    throw new Exception("created geometry without material");
                }

                material.DoubleSided         = true;
                material.LightingModelName   = SCNLightingModel.Constant;
                material.BlendMode           = SCNBlendMode.Add;
                material.WritesToDepthBuffer = false;
            }

            return(result);
        }
Esempio n. 3
0
        public SCNGeometry MakeMeshGeometry(SCNVector3[] vertices, SCNVector3[] normals, List <int> faces)
        {
            var geometrySources = new[] { SCNGeometrySource.FromVertices(vertices), SCNGeometrySource.FromNormals(normals) };
            var array           = faces.SelectMany(v => BitConverter.GetBytes(v)).ToArray();
            var indexData       = NSData.FromArray(array);
            var elementSource   = SCNGeometryElement.FromData(indexData, SCNGeometryPrimitiveType.Triangles, (nint)(faces.Count / 3), (nint)sizeof(int));
            var geometry        = SCNGeometry.Create(geometrySources, new[] { elementSource });

            geometry.WantsAdaptiveSubdivision = false;

            return(geometry);
        }
Esempio n. 4
0
        public static SCNGeometry ToSCNGeometry(this Solid csg)
        {
            if (csg.Polygons.Count == 0)
            {
                return(SCNGeometry.Create());
            }
            else
            {
                var verts =
                    csg.
                    Polygons.
                    SelectMany(x => x.Vertices).
                    Select(ToSCNVector3).
                    ToArray();
                var norms =
                    csg.
                    Polygons.
                    SelectMany(x =>
                {
                    var n = x.Plane.Normal.ToSCNVector3();
                    return(x.Vertices.Select(_ => n));
                }).
                    ToArray();
                var vertsSource = SCNGeometrySource.FromVertices(verts);
                var normsSource = SCNGeometrySource.FromNormals(norms);
                var sources     = new[] { vertsSource, normsSource };
                var triStream   = new System.IO.MemoryStream();
                var triWriter   = new System.IO.BinaryWriter(triStream);
                var triCount    = 0;
                var vi          = 0;
                foreach (var p in csg.Polygons)
                {
                    for (var i = 2; i < p.Vertices.Count; i++)
                    {
                        triWriter.Write(vi);
                        triWriter.Write(vi + i - 1);
                        triWriter.Write(vi + i);
                    }
                    triCount += p.Vertices.Count - 2;
                    vi       += p.Vertices.Count;
                }
                triWriter.Flush();
                var triData  = NSData.FromArray(triStream.ToArray());
                var elem     = SCNGeometryElement.FromData(triData, SCNGeometryPrimitiveType.Triangles, triCount, 4);
                var elements = new[] { elem };

                var g = SCNGeometry.Create(sources, elements);
                g.FirstMaterial.Diffuse.ContentColor = NSColor.LightGray;
                return(g);
            }
        }
Esempio n. 5
0
 static internal SCNGeometrySource [] CreateGeometrySources(nfloat width, nfloat height)
 {
     return(new [] {
         SCNGeometrySource.FromVertices(new [] {
             new SCNVector3(0, 0, 0),
             new SCNVector3(0, height, 0),
             new SCNVector3(width, height, 0),
             new SCNVector3(width, 0, 0)
         }),
         SCNGeometrySource.FromNormals(new [] {
             new SCNVector3(0, 0, 1),
             new SCNVector3(0, 0, 1),
             new SCNVector3(0, 0, 1),
             new SCNVector3(0, 0, 1)
         }),
         SCNGeometrySource.FromTextureCoordinates(new [] {
             new CGPoint(0, 0),
             new CGPoint(0, 1),
             new CGPoint(1, 1),
             new CGPoint(1, 0)
         })
     });
 }