Exemplo n.º 1
0
        // static public UnityEngine.Mesh ToHost(this Brep brep)
        // {
        //     var meshes = MeshCompute.CreateFromBrep(brep);
        //     var mesh = new Rhino.Geometry.Mesh();
        //
        //     foreach (var m in meshes)
        //     {
        //         mesh.Append(m);
        //     }
        //
        //     return mesh.ToHost();
        // }

        static public List <Vector2> ToHost(this MeshTextureCoordinateList coordinate)
        {
            var coors = new List <Vector2>();

            foreach (var c in coordinate)
            {
                coors.Add(c.ToHost());
            }

            return(coors);
        }
Exemplo n.º 2
0
        public static void ParseRhinoTextures(MeshTextureCoordinateList textures, out double[] parametricCoord)
        {
            parametricCoord = new double[textures.Count * 2];
            Point2f uv;

            for (int i = 0; i < textures.Count; i++)
            {
                uv = textures[i];
                parametricCoord[i * 2] = uv.X;
                parametricCoord[i * 2] = uv.Y;
            }
        }
Exemplo n.º 3
0
        int GetTextureCoordinatesBuffer(MeshTextureCoordinateList textureCoordinates, out Point2f min, out Point2f max, out int byteLength)
        {
            byte[] bytes = GetTextureCoordinatesBytes(textureCoordinates, out min, out max);

            glTFLoader.Schema.Buffer textureCoordinatesBuffer = new glTFLoader.Schema.Buffer()
            {
                Uri        = Constants.TextBufferHeader + Convert.ToBase64String(bytes),
                ByteLength = bytes.Length,
            };

            byteLength = bytes.Length;

            return(dummy.Buffers.AddAndReturnIndex(textureCoordinatesBuffer));
        }
Exemplo n.º 4
0
        protected override Result RunCommand(RhinoDoc doc, RunMode mode)
        {
            // TODO: complete command.
            RhinoApp.WriteLine("The {0} command is under construction.", EnglishName);

            Rhino.Input.Custom.GetObject gmesh = new Rhino.Input.Custom.GetObject();
            gmesh.SetCommandPrompt("Get the Mesh");
            gmesh.GeometryFilter  = Rhino.DocObjects.ObjectType.Mesh;
            gmesh.SubObjectSelect = true;
            gmesh.Get();
            if (gmesh.CommandResult() != Rhino.Commands.Result.Success)
            {
                return(gmesh.CommandResult());
            }
            Rhino.DocObjects.ObjRef      objref = gmesh.Object(0);
            Rhino.DocObjects.RhinoObject obj    = objref.Object();
            if (obj == null)
            {
                return(Rhino.Commands.Result.Failure);
            }
            Rhino.Geometry.Mesh mesh = objref.Mesh();
            if (mesh == null)
            {
                return(Rhino.Commands.Result.Failure);
            }
            obj.Select(false);

            MeshTextureCoordinateList texture_list = mesh.TextureCoordinates;

            for (int i = 0; i < texture_list.Count - 1; i++)
            {
                Point2f f1 = texture_list[i];
                Point2f f2 = texture_list[i + 1];
                Point3d t1 = new Point3d(f1.X, f1.Y, 0);
                Point3d t2 = new Point3d(f2.X, f2.Y, 0);
                Line    l  = new Line(t1, t2);
                doc.Objects.AddLine(l);
                RhinoApp.WriteLine("Line added");
            }
            doc.Views.Redraw();
            MyRhino5rs11project8PlugIn p = MyRhino5rs11project8PlugIn.Instance;

            p.painted_object_        = mesh;
            p.if_painted_object_set_ = true;
            mesh.UserDictionary.Set("name", "myMesh");
            mesh.UserDictionary.Set("isMovable", false);
            p.graph = new DijkstraGraph(10);
            RhinoApp.WriteLine("Mesh Got");
            return(Result.Success);
        }
Exemplo n.º 5
0
        int GetTextureCoordinatesAccessor(MeshTextureCoordinateList textureCoordinates)
        {
            int?textureCoordinatesBufferViewIdx = GetTextureCoordinatesBufferView(textureCoordinates, out Point2f min, out Point2f max, out int countCoordinates);

            glTFLoader.Schema.Accessor textureCoordinatesAccessor = new glTFLoader.Schema.Accessor()
            {
                BufferView    = textureCoordinatesBufferViewIdx,
                ByteOffset    = 0,
                ComponentType = glTFLoader.Schema.Accessor.ComponentTypeEnum.FLOAT,
                Count         = countCoordinates,
                Min           = min.ToFloatArray(),
                Max           = max.ToFloatArray(),
                Type          = glTFLoader.Schema.Accessor.TypeEnum.VEC2,
            };

            return(dummy.Accessors.AddAndReturnIndex(textureCoordinatesAccessor));
        }
Exemplo n.º 6
0
        private byte[] GetTextureCoordinatesBytes(MeshTextureCoordinateList textureCoordinates, out Point2f min, out Point2f max)
        {
            min = new Point2f(float.PositiveInfinity, float.PositiveInfinity);
            max = new Point2f(float.NegativeInfinity, float.NegativeInfinity);

            List <float> coordinates = new List <float>(textureCoordinates.Count * 2);

            foreach (Point2f coordinate in textureCoordinates)
            {
                coordinates.AddRange(new float[] { coordinate.X, coordinate.Y });

                min.X = Math.Min(min.X, coordinate.X);
                max.X = Math.Max(max.X, coordinate.X);

                min.Y = Math.Min(min.Y, coordinate.Y);
                max.Y = Math.Max(max.Y, coordinate.Y);
            }

            IEnumerable <byte> bytesEnumerable = coordinates.SelectMany(value => BitConverter.GetBytes(value));

            return(bytesEnumerable.ToArray());
        }
Exemplo n.º 7
0
        int?GetTextureCoordinatesBufferView(MeshTextureCoordinateList textureCoordinates, out Point2f min, out Point2f max, out int countCoordinates)
        {
            if (options.UseDracoCompression)
            {
                min = currentGeometryInfo.TexCoordsMin;
                max = currentGeometryInfo.TexCoordsMax;
                countCoordinates = currentGeometryInfo.TexCoordsCount;
                return(null);
            }

            int buffer     = 0;
            int byteLength = 0;
            int byteOffset = 0;

            if (binary)
            {
                byte[] bytes = GetTextureCoordinatesBytes(textureCoordinates, out min, out max);
                byteLength = bytes.Length;
                byteOffset = binaryBuffer.Count;
                binaryBuffer.AddRange(bytes);
            }
            else
            {
                buffer = GetTextureCoordinatesBuffer(textureCoordinates, out min, out max, out byteLength);
            }

            glTFLoader.Schema.BufferView textureCoordinatesBufferView = new glTFLoader.Schema.BufferView()
            {
                Buffer     = buffer,
                ByteLength = byteLength,
                ByteOffset = byteOffset,
                Target     = glTFLoader.Schema.BufferView.TargetEnum.ARRAY_BUFFER,
            };

            countCoordinates = textureCoordinates.Count;

            return(dummy.BufferViews.AddAndReturnIndex(textureCoordinatesBufferView));
        }
Exemplo n.º 8
0
        public int StoreSerialized(Mesh mesh, string name)
        {
            MeshFaceList              faces     = mesh.Faces;
            MeshVertexList            vertices  = mesh.Vertices;
            MeshVertexNormalList      normals   = mesh.Normals;
            MeshTextureCoordinateList texCoords = mesh.TextureCoordinates;

            faces.ConvertQuadsToTriangles();

            int vertexCount   = vertices.Count;
            int triangleCount = faces.TriangleCount;

            Log("MeshStore[" + meshCount + "]: adding mesh with " + vertexCount + " vertices, " + triangleCount + " triangles"
                + (name != null && name.Length > 0 ? (" (\"" + name + "\")") : ""));

            meshDict.Add((ulong)output.Position);
            Serialize(output, MTS_FILEFORMAT_HEADER);
            Serialize(output, MTS_FILEFORMAT_VERSION_V4);

            Stream zStream = new ZlibStream(output, CompressionMode.Compress,
                                            CompressionLevel.BestCompression, true);

            uint flags = MTS_SINGLE_PRECISION;

            if (texCoords.Count > 0)
            {
                flags |= MTS_HAS_TEXCOORDS;
            }
            if (normals.Count > 0)
            {
                flags |= MTS_HAS_NORMALS;
            }

            Serialize(zStream, flags);
            Serialize(zStream, (name == null) ? "" : name);
            Serialize(zStream, (ulong)vertexCount);
            Serialize(zStream, (ulong)triangleCount);

            for (int i = 0; i < vertexCount; ++i)
            {
                Point3f p = vertices[i];
                Serialize(zStream, p.X); Serialize(zStream, p.Y); Serialize(zStream, p.Z);
            }

            if (normals.Count > 0)
            {
                for (int i = 0; i < vertexCount; ++i)
                {
                    Vector3f n = normals[i];
                    Serialize(zStream, n.X); Serialize(zStream, n.Y); Serialize(zStream, n.Z);
                }
            }

            if (texCoords.Count > 0)
            {
                for (int i = 0; i < vertexCount; ++i)
                {
                    Point2f uv = texCoords[i];
                    Serialize(zStream, uv.X); Serialize(zStream, uv.Y);
                }
            }

            for (int i = 0; i < triangleCount; ++i)
            {
                MeshFace face = faces[i];
                if (!face.IsTriangle)
                {
                    throw new Exception("Internal error: expected a triangle face!");
                }
                Serialize(zStream, face.A); Serialize(zStream, face.B); Serialize(zStream, face.C);
            }
            zStream.Close();

            return(meshCount++);
        }
Exemplo n.º 9
0
        public String StoreOBJ(Mesh mesh, string name)
        {
            MeshFaceList              faces     = mesh.Faces;
            MeshVertexList            vertices  = mesh.Vertices;
            MeshVertexNormalList      normals   = mesh.Normals;
            MeshTextureCoordinateList texCoords = mesh.TextureCoordinates;

            faces.ConvertQuadsToTriangles();

            if (name == null || name.Length == 0)
            {
                name = "mesh" + meshCount.ToString();
            }

            int vertexCount   = vertices.Count;
            int triangleCount = faces.TriangleCount;

            Log("MeshStore[" + meshCount + "]: saving OBJ mesh with " + vertexCount + " vertices, "
                + triangleCount + " triangles" + "\"" + name + "\")");


            //meshDict.Add((ulong)output.Position);
            //Serialize(output, MTS_FILEFORMAT_HEADER);
            //Serialize(output, MTS_FILEFORMAT_VERSION_V4);

            String       filename = name + ".obj";
            FileStream   output   = new FileStream(Path.Combine(basePath, filename), FileMode.Create);
            StreamWriter stream   = new StreamWriter(output);

            stream.WriteLine("# Object " + name);
            stream.WriteLine("# Generated by the Mitsuba Rhino plugin.");
            stream.WriteLine("# Vertices: " + vertexCount.ToString());
            stream.WriteLine("# Faces: " + triangleCount.ToString());

            for (int i = 0; i < vertexCount; ++i)
            {
                Point3f p = vertices[i];
                stream.WriteLine("v " + p.X.ToString() + " " + p.Y.ToString() + " " + p.Z.ToString());
            }

            if (normals.Count > 0)
            {
                for (int i = 0; i < vertexCount; ++i)
                {
                    Vector3f n = normals[i];
                    stream.WriteLine("vn " + n.X.ToString() + " " + n.Y.ToString() + " " + n.Z.ToString());
                }
            }

            if (texCoords.Count > 0)
            {
                for (int i = 0; i < vertexCount; ++i)
                {
                    Point2f uv = texCoords[i];
                    stream.WriteLine("vt " + uv.X.ToString() + " " + uv.Y.ToString());
                }
            }

            for (int i = 0; i < triangleCount; ++i)
            {
                MeshFace face = faces[i];
                if (!face.IsTriangle)
                {
                    throw new Exception("Internal error: expected a triangle face!");
                }
                String a = (face.A + 1).ToString();
                String b = (face.B + 1).ToString();
                String c = (face.C + 1).ToString();
                if (texCoords.Count > 0)
                {
                    stream.WriteLine("f " + a + "/" + a + "/" + a + " "
                                     + b + "/" + b + "/" + b + " "
                                     + c + "/" + c + "/" + c);
                }
                else
                {
                    stream.WriteLine("f " + a + "//" + a + " "
                                     + b + "//" + b + " "
                                     + c + "//" + c);
                }
            }

            stream.Close();
            meshCount++;
            return(filename);
        }