Beispiel #1
0
        public override void SimulateMaterial(ref Rhino.DocObjects.Material simulatedMaterial, bool forDataOnly)
        {
            var boolrc = false;

            boolrc = HandleTexturedValue(Pbr.BaseColor, Base);
            simulatedMaterial.DiffuseColor = Base.Value.AsSystemColor();
            if (Base.Texture != null && Base.On)
            {
                SimulatedTexture simtex = Base.Texture.SimulatedTexture(RenderTexture.TextureGeneration.Allow);
                simulatedMaterial.SetBitmapTexture(simtex.Texture());
            }

            boolrc = HandleTexturedValue(Pbr.Metallic, Metallic);
            simulatedMaterial.Reflectivity = Metallic.Value;
            if (Metallic.Value > 0.5f)
            {
                simulatedMaterial.DiffuseColor    = System.Drawing.Color.Black;
                simulatedMaterial.ReflectionColor = Base.Value.AsSystemColor();
            }

            boolrc = HandleTexturedValue(Pbr.Roughness, Roughness);
            simulatedMaterial.ReflectionGlossiness = 1.0f - Roughness.Value;

            boolrc = HandleTexturedValue(Pbr.Opacity, Transmission);
            simulatedMaterial.Transparency = 1.0f - Transmission.Value;
            if (Transmission.Value > 0.5f)
            {
                simulatedMaterial.TransparentColor = Base.Value.AsSystemColor();
            }

            boolrc = HandleTexturedValue(Pbr.OpacityRoughness, TransmissionRoughness);
            simulatedMaterial.RefractionGlossiness = 1.0f - TransmissionRoughness.Value;
            boolrc = HandleTexturedValue(Pbr.OpacityIor, Ior);
            simulatedMaterial.IndexOfRefraction = Ior.Value;

            boolrc = HandleTexturedValue(Pbr.Bump, Bump);
            if (Bump.On && Bump.Texture != null)
            {
                SimulatedTexture simtex = Bump.Texture.SimulatedTexture(RenderTexture.TextureGeneration.Allow);
                simulatedMaterial.SetBumpTexture(simtex.Texture());
            }
        }
Beispiel #2
0
        public static void OptToRhino(OptFile opt, string rhinoPath, bool scale)
        {
            if (opt == null)
            {
                throw new ArgumentNullException("opt");
            }

            string rhinoDirectory = Path.GetDirectoryName(rhinoPath);
            string rhinoName      = Path.GetFileNameWithoutExtension(rhinoPath);

            var distances = opt.Meshes
                            .SelectMany(t => t.Lods)
                            .Select(t => t.Distance)
                            .Distinct()
                            .OrderByDescending(t => t)
                            .ToArray();

            for (int distance = 0; distance < distances.Length; distance++)
            {
                using (var file = new Rhino.FileIO.File3dm())
                {
                    file.Settings.ModelUnitSystem = Rhino.UnitSystem.Meters;

                    int           objectsIndex = 0;
                    List <string> textureNames = new List <string>();

                    foreach (var mesh in opt.Meshes)
                    {
                        var lod = mesh.Lods.FirstOrDefault(t => t.Distance <= distances[distance]);

                        if (lod == null)
                        {
                            continue;
                        }

                        foreach (var textureName in lod.FaceGroups
                                 .Where(t => t.Textures.Count > 0)
                                 .Select(t => t.Textures[0]))
                        {
                            if (!textureNames.Contains(textureName))
                            {
                                textureNames.Add(textureName);
                            }
                        }

                        string meshName = string.Format(CultureInfo.InvariantCulture, "{0}.{1:D3}", mesh.Descriptor.MeshType, objectsIndex);

                        using (var layer = new Rhino.DocObjects.Layer())
                        {
                            layer.Name = meshName;

                            file.Layers.Add(layer);
                        }

                        foreach (var faceGroup in lod.FaceGroups)
                        {
                            using (var rhinoMesh = new Rhino.Geometry.Mesh())
                                using (var rhinoAttributes = new Rhino.DocObjects.ObjectAttributes())
                                {
                                    rhinoAttributes.Name       = meshName;
                                    rhinoAttributes.LayerIndex = objectsIndex;

                                    if (faceGroup.Textures.Count > 0)
                                    {
                                        rhinoAttributes.MaterialIndex = textureNames.IndexOf(faceGroup.Textures[0]);
                                    }

                                    Action <Vector> addVertex;

                                    if (scale)
                                    {
                                        addVertex = vertex => rhinoMesh.Vertices.Add(vertex.X * OptFile.ScaleFactor, vertex.Y * OptFile.ScaleFactor, vertex.Z * OptFile.ScaleFactor);
                                    }
                                    else
                                    {
                                        addVertex = vertex => rhinoMesh.Vertices.Add(vertex.X, vertex.Y, vertex.Z);
                                    }

                                    Action <TextureCoordinates> addTexCoords = texCoords => rhinoMesh.TextureCoordinates.Add(texCoords.U, -texCoords.V);

                                    Action <Vector> addNormal = normal => rhinoMesh.Normals.Add(normal.X, normal.Y, normal.Z);

                                    int facesIndex = 0;

                                    foreach (var face in faceGroup.Faces)
                                    {
                                        var verticesIndex  = face.VerticesIndex;
                                        var texCoordsIndex = face.TextureCoordinatesIndex;
                                        var normalsIndex   = face.VertexNormalsIndex;

                                        addVertex(mesh.Vertices[verticesIndex.A]);
                                        addTexCoords(mesh.TextureCoordinates[texCoordsIndex.A]);
                                        addNormal(mesh.VertexNormals[normalsIndex.A]);
                                        facesIndex++;

                                        addVertex(mesh.Vertices[verticesIndex.B]);
                                        addTexCoords(mesh.TextureCoordinates[texCoordsIndex.B]);
                                        addNormal(mesh.VertexNormals[normalsIndex.B]);
                                        facesIndex++;

                                        addVertex(mesh.Vertices[verticesIndex.C]);
                                        addTexCoords(mesh.TextureCoordinates[texCoordsIndex.C]);
                                        addNormal(mesh.VertexNormals[normalsIndex.C]);
                                        facesIndex++;

                                        if (verticesIndex.D >= 0)
                                        {
                                            addVertex(mesh.Vertices[verticesIndex.D]);
                                            addTexCoords(mesh.TextureCoordinates[texCoordsIndex.D]);
                                            addNormal(mesh.VertexNormals[normalsIndex.D]);
                                            facesIndex++;
                                        }

                                        if (verticesIndex.D < 0)
                                        {
                                            rhinoMesh.Faces.AddFace(facesIndex - 1, facesIndex - 2, facesIndex - 3);
                                        }
                                        else
                                        {
                                            rhinoMesh.Faces.AddFace(facesIndex - 1, facesIndex - 2, facesIndex - 3, facesIndex - 4);
                                        }
                                    }

                                    rhinoMesh.Compact();

                                    file.Objects.AddMesh(rhinoMesh, rhinoAttributes);
                                }
                        }

                        objectsIndex++;
                    }

                    foreach (var textureName in textureNames)
                    {
                        Texture texture;
                        opt.Textures.TryGetValue(textureName, out texture);

                        using (var material = new Rhino.DocObjects.Material())
                        {
                            material.Name = textureName;

                            if (texture == null)
                            {
                                material.DiffuseColor = System.Drawing.Color.White;
                            }
                            else
                            {
                                texture.Save(Path.Combine(rhinoDirectory, textureName + ".png"));
                                material.SetBitmapTexture(textureName + ".png");

                                if (texture.HasAlpha)
                                {
                                    texture.SaveAlphaMap(Path.Combine(rhinoDirectory, textureName + "_alpha.png"));
                                    material.SetTransparencyTexture(textureName + "_alpha.png");
                                }
                            }

                            file.Materials.Add(material);
                        }
                    }

                    file.Write(Path.Combine(rhinoDirectory, string.Format(CultureInfo.InvariantCulture, "{0}_{1}.3dm", rhinoName, distance)), 4);
                }
            }
        }
Beispiel #3
0
        public static void OptToRhino(OptFile opt, string rhinoPath, bool scale)
        {
            if (opt == null)
            {
                throw new ArgumentNullException("opt");
            }

            string rhinoDirectory = Path.GetDirectoryName(rhinoPath);
            string rhinoName = Path.GetFileNameWithoutExtension(rhinoPath);

            var distances = opt.Meshes
                .SelectMany(t => t.Lods)
                .Select(t => t.Distance)
                .Distinct()
                .OrderByDescending(t => t)
                .ToArray();

            for (int distance = 0; distance < distances.Length; distance++)
            {
                using (var file = new Rhino.FileIO.File3dm())
                {
                    file.Settings.ModelUnitSystem = Rhino.UnitSystem.Meters;

                    int objectsIndex = 0;
                    List<string> textureNames = new List<string>();

                    foreach (var mesh in opt.Meshes)
                    {
                        var lod = mesh.Lods.FirstOrDefault(t => t.Distance <= distances[distance]);

                        if (lod == null)
                        {
                            continue;
                        }

                        foreach (var textureName in lod.FaceGroups
                            .Where(t => t.Textures.Count > 0)
                            .Select(t => t.Textures[0]))
                        {
                            if (!textureNames.Contains(textureName))
                            {
                                textureNames.Add(textureName);
                            }
                        }

                        string meshName = string.Format(CultureInfo.InvariantCulture, "{0}.{1:D3}", mesh.Descriptor.MeshType, objectsIndex);

                        using (var layer = new Rhino.DocObjects.Layer())
                        {
                            layer.Name = meshName;

                            file.Layers.Add(layer);
                        }

                        foreach (var faceGroup in lod.FaceGroups)
                        {
                            using (var rhinoMesh = new Rhino.Geometry.Mesh())
                            using (var rhinoAttributes = new Rhino.DocObjects.ObjectAttributes())
                            {
                                rhinoAttributes.Name = meshName;
                                rhinoAttributes.LayerIndex = objectsIndex;

                                if (faceGroup.Textures.Count > 0)
                                {
                                    rhinoAttributes.MaterialIndex = textureNames.IndexOf(faceGroup.Textures[0]);
                                }

                                Action<Vector> addVertex;

                                if (scale)
                                {
                                    addVertex = vertex => rhinoMesh.Vertices.Add(vertex.X * OptFile.ScaleFactor, vertex.Y * OptFile.ScaleFactor, vertex.Z * OptFile.ScaleFactor);
                                }
                                else
                                {
                                    addVertex = vertex => rhinoMesh.Vertices.Add(vertex.X, vertex.Y, vertex.Z);
                                }

                                Action<TextureCoordinates> addTexCoords = texCoords => rhinoMesh.TextureCoordinates.Add(texCoords.U, -texCoords.V);

                                Action<Vector> addNormal = normal => rhinoMesh.Normals.Add(normal.X, normal.Y, normal.Z);

                                int facesIndex = 0;

                                foreach (var face in faceGroup.Faces)
                                {
                                    var verticesIndex = face.VerticesIndex;
                                    var texCoordsIndex = face.TextureCoordinatesIndex;
                                    var normalsIndex = face.VertexNormalsIndex;

                                    addVertex(mesh.Vertices[verticesIndex.A]);
                                    addTexCoords(mesh.TextureCoordinates[texCoordsIndex.A]);
                                    addNormal(mesh.VertexNormals[normalsIndex.A]);
                                    facesIndex++;

                                    addVertex(mesh.Vertices[verticesIndex.B]);
                                    addTexCoords(mesh.TextureCoordinates[texCoordsIndex.B]);
                                    addNormal(mesh.VertexNormals[normalsIndex.B]);
                                    facesIndex++;

                                    addVertex(mesh.Vertices[verticesIndex.C]);
                                    addTexCoords(mesh.TextureCoordinates[texCoordsIndex.C]);
                                    addNormal(mesh.VertexNormals[normalsIndex.C]);
                                    facesIndex++;

                                    if (verticesIndex.D >= 0)
                                    {
                                        addVertex(mesh.Vertices[verticesIndex.D]);
                                        addTexCoords(mesh.TextureCoordinates[texCoordsIndex.D]);
                                        addNormal(mesh.VertexNormals[normalsIndex.D]);
                                        facesIndex++;
                                    }

                                    if (verticesIndex.D < 0)
                                    {
                                        rhinoMesh.Faces.AddFace(facesIndex - 1, facesIndex - 2, facesIndex - 3);
                                    }
                                    else
                                    {
                                        rhinoMesh.Faces.AddFace(facesIndex - 1, facesIndex - 2, facesIndex - 3, facesIndex - 4);
                                    }
                                }

                                rhinoMesh.Compact();

                                file.Objects.AddMesh(rhinoMesh, rhinoAttributes);
                            }
                        }

                        objectsIndex++;
                    }

                    foreach (var textureName in textureNames)
                    {
                        Texture texture;
                        opt.Textures.TryGetValue(textureName, out texture);

                        using (var material = new Rhino.DocObjects.Material())
                        {
                            material.Name = textureName;

                            if (texture == null)
                            {
                                material.DiffuseColor = System.Drawing.Color.White;
                            }
                            else
                            {
                                texture.Save(Path.Combine(rhinoDirectory, textureName + ".png"));
                                material.SetBitmapTexture(textureName + ".png");

                                if (texture.HasAlpha)
                                {
                                    texture.SaveAlphaMap(Path.Combine(rhinoDirectory, textureName + "_alpha.png"));
                                    material.SetTransparencyTexture(textureName + "_alpha.png");
                                }
                            }

                            file.Materials.Add(material);
                        }
                    }

                    file.Write(Path.Combine(rhinoDirectory, string.Format(CultureInfo.InvariantCulture, "{0}_{1}.3dm", rhinoName, distance)), 4);
                }
            }
        }