示例#1
0
        public Rhino.Render.RenderTexture GetRenderTexture(int textureIndex, Rhino.Display.Color4f factor)
        {
            System.Drawing.Bitmap bmp = GetTextureBitmap(textureIndex, out string name);

            if (bmp == null)
            {
                return(null);
            }

            int width  = bmp.Width;
            int height = bmp.Height;


            System.Drawing.Bitmap resolvedBmp = new System.Drawing.Bitmap(width, height);

            for (int i = 0; i < width; i++)
            {
                for (int j = 0; j < height; j++)
                {
                    Rhino.Display.Color4f colorAt = new Rhino.Display.Color4f(bmp.GetPixel(i, j));

                    float r = GltfUtils.Clamp(colorAt.R * factor.R, 0.0f, 1.0f);
                    float g = GltfUtils.Clamp(colorAt.G * factor.G, 0.0f, 1.0f);
                    float b = GltfUtils.Clamp(colorAt.B * factor.B, 0.0f, 1.0f);
                    float a = GltfUtils.Clamp(colorAt.A * factor.A, 0.0f, 1.0f);

                    Rhino.Display.Color4f colorFinal = new Rhino.Display.Color4f(r, g, b, a);

                    resolvedBmp.SetPixel(i, j, colorFinal.AsSystemColor());
                }
            }

            Rhino.Render.RenderTexture renderTexture = Rhino.Render.RenderTexture.NewBitmapTexture(resolvedBmp, doc);

            renderTexture.BeginChange(Rhino.Render.RenderContent.ChangeContexts.Program);

            renderTexture.Name = name;

            renderTexture.EndChange();

            return(renderTexture);
        }
        public Rhino.Render.RenderMaterial Convert()
        {
            RenderMaterial pbr = RenderContentType.NewContentFromTypeId(ContentUuids.PhysicallyBasedMaterialType, doc) as RenderMaterial;

            pbr.BeginChange(RenderContent.ChangeContexts.Program);

            pbr.Name = converter.GetUniqueName(material.Name);

            if (material.PbrMetallicRoughness != null)
            {
                Rhino.Display.Color4f baseColor = material.PbrMetallicRoughness.BaseColorFactor.ToColor4f();

                if (material.PbrMetallicRoughness.BaseColorTexture != null)
                {
                    int index = material.PbrMetallicRoughness.BaseColorTexture.Index;

                    RenderTexture texture = converter.GetRenderTexture(index, baseColor);

                    pbr.SetChild(texture, Rhino.Render.ParameterNames.PhysicallyBased.BaseColor);
                    pbr.SetChildSlotOn(Rhino.Render.ParameterNames.PhysicallyBased.BaseColor, true, RenderContent.ChangeContexts.Program);
                }

                baseColor = GltfUtils.UnapplyGamma(baseColor);

                pbr.SetParameter(PhysicallyBased.BaseColor, baseColor);

                double roughness = material.PbrMetallicRoughness.RoughnessFactor;

                double metalness = material.PbrMetallicRoughness.MetallicFactor;

                if (material.PbrMetallicRoughness.MetallicRoughnessTexture != null)
                {
                    int index = material.PbrMetallicRoughness.MetallicRoughnessTexture.Index;

                    RhinoGltfMetallicRoughnessConverter metallicRoughness = converter.GetMetallicRoughnessTexture(index);

                    pbr.SetChild(metallicRoughness.MetallicTexture, PhysicallyBased.Metallic);
                    pbr.SetChildSlotOn(PhysicallyBased.Metallic, true, RenderContent.ChangeContexts.Program);
                    pbr.SetChildSlotAmount(PhysicallyBased.Metallic, metalness * 100.0, RenderContent.ChangeContexts.Program);

                    pbr.SetChild(metallicRoughness.RoughnessTexture, PhysicallyBased.Roughness);
                    pbr.SetChildSlotOn(PhysicallyBased.Roughness, true, RenderContent.ChangeContexts.Program);
                    pbr.SetChildSlotAmount(PhysicallyBased.Roughness, roughness * 100.0, RenderContent.ChangeContexts.Program);
                }
                else
                {
                    pbr.SetParameter(PhysicallyBased.Roughness, roughness);

                    pbr.SetParameter(PhysicallyBased.Metallic, metalness);
                }
            }

            Rhino.Display.Color4f emissionColor = material.EmissiveFactor.ToColor4f();

            emissionColor = GltfUtils.UnapplyGamma(emissionColor);

            pbr.SetParameter(PhysicallyBased.Emission, emissionColor);

            if (material.EmissiveTexture != null)
            {
                RenderTexture emissiveTexture = converter.GetRenderTexture(material.EmissiveTexture.Index);

                pbr.SetChild(emissiveTexture, PhysicallyBased.Emission);
                pbr.SetChildSlotOn(PhysicallyBased.Emission, true, RenderContent.ChangeContexts.Program);
            }

            if (material.OcclusionTexture != null)
            {
                RenderTexture occlusionTexture = converter.GetRenderTexture(material.OcclusionTexture.Index);

                pbr.SetChild(occlusionTexture, PhysicallyBased.AmbientOcclusion);
                pbr.SetChildSlotOn(PhysicallyBased.AmbientOcclusion, true, RenderContent.ChangeContexts.Program);
                pbr.SetChildSlotAmount(PhysicallyBased.AmbientOcclusion, material.OcclusionTexture.Strength * 100.0, RenderContent.ChangeContexts.Program);
            }

            if (material.NormalTexture != null)
            {
                RenderTexture normalTexture = converter.GetRenderTexture(material.NormalTexture.Index);

                pbr.SetChild(normalTexture, PhysicallyBased.Bump);
                pbr.SetChildSlotOn(PhysicallyBased.Bump, true, RenderContent.ChangeContexts.Program);
            }

            string clearcoatText    = "";
            string transmissionText = "";
            string iorText          = "";
            string specularText     = "";

            if (material.Extensions != null)
            {
                if (material.Extensions.TryGetValue(glTFExtensions.KHR_materials_clearcoat.Tag, out object clearcoatValue))
                {
                    clearcoatText = clearcoatValue.ToString();
                }

                if (material.Extensions.TryGetValue(glTFExtensions.KHR_materials_transmission.Tag, out object transmissionValue))
                {
                    transmissionText = transmissionValue.ToString();
                }

                if (material.Extensions.TryGetValue(glTFExtensions.KHR_materials_ior.Tag, out object iorValue))
                {
                    iorText = iorValue.ToString();
                }

                if (material.Extensions.TryGetValue(glTFExtensions.KHR_materials_specular.Tag, out object specularValue))
                {
                    specularText = specularValue.ToString();
                }
            }

            HandleClearcoat(clearcoatText, pbr);

            HandleTransmission(transmissionText, pbr);

            HandleIor(iorText, pbr);

            HandleSpecular(specularText, pbr);

            pbr.EndChange();

            doc.RenderMaterials.BeginChange(RenderContent.ChangeContexts.Program);

            doc.RenderMaterials.Add(pbr);

            doc.RenderMaterials.EndChange();

            return(pbr);
        }
示例#3
0
        private bool AttemptConvertVertexColors(glTFLoader.Schema.MeshPrimitive primitive, Rhino.Geometry.Mesh rhinoMesh)
        {
            if (!primitive.Attributes.TryGetValue(VertexColorAttributeTag, out int vertexColorAccessorIndex))
            {
                return(false);
            }

            glTFLoader.Schema.Accessor vertexColorAccessor = converter.GetAccessor(vertexColorAccessorIndex);

            if (vertexColorAccessor == null)
            {
                return(false);
            }

            glTFLoader.Schema.BufferView vertexColorBufferView = converter.GetBufferView(vertexColorAccessor.BufferView);

            if (vertexColorBufferView == null)
            {
                return(false);
            }

            byte[] vertexColorBuffer = converter.GetBuffer(vertexColorBufferView.Buffer);

            if (vertexColorBuffer == null)
            {
                return(false);
            }

            int vertexColorOffset = vertexColorAccessor.ByteOffset + vertexColorBufferView.ByteOffset;

            int vertexColorStride = vertexColorBufferView.ByteStride.HasValue ? vertexColorBufferView.ByteStride.Value : TotalStride(vertexColorAccessor.ComponentType, vertexColorAccessor.Type);

            int vertexColorComponentCount = ComponentsCount(vertexColorAccessor.Type);

            int vertexColorComponentSize = ComponentSize(vertexColorAccessor.ComponentType);

            List <float> vertexColors = new List <float>();

            for (int i = 0; i < vertexColorAccessor.Count; i++)
            {
                int vertexColorIndex = vertexColorOffset + i * vertexColorStride;

                for (int j = 0; j < vertexColorComponentCount; j++)
                {
                    int location = vertexColorIndex + j * vertexColorComponentSize;

                    float channelColor = 0.0f;

                    if (vertexColorAccessor.ComponentType == glTFLoader.Schema.Accessor.ComponentTypeEnum.FLOAT)
                    {
                        channelColor = BitConverter.ToSingle(vertexColorBuffer, location);
                    }
                    else if (vertexColorAccessor.ComponentType == glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_SHORT)
                    {
                        ushort value = BitConverter.ToUInt16(vertexColorBuffer, location);
                        channelColor = (float)value / (float)ushort.MaxValue;
                    }
                    else if (vertexColorAccessor.ComponentType == glTFLoader.Schema.Accessor.ComponentTypeEnum.UNSIGNED_BYTE)
                    {
                        byte value = vertexColorBuffer[location];
                        channelColor = (float)value / (float)byte.MaxValue;
                    }

                    vertexColors.Add(channelColor);
                }
            }

            int countVertexColors = vertexColors.Count / vertexColorComponentCount;

            for (int i = 0; i < countVertexColors; i++)
            {
                int index = i * vertexColorComponentCount;

                if (vertexColorAccessor.Type == glTFLoader.Schema.Accessor.TypeEnum.VEC3)
                {
                    float r = GltfUtils.Clamp(vertexColors[index + 0], 0.0f, 1.0f);
                    float g = GltfUtils.Clamp(vertexColors[index + 1], 0.0f, 1.0f);
                    float b = GltfUtils.Clamp(vertexColors[index + 2], 0.0f, 1.0f);

                    Rhino.Display.Color4f color = new Rhino.Display.Color4f(r, g, b, 1.0f);

                    rhinoMesh.VertexColors.Add(color.AsSystemColor());
                }
                else if (vertexColorAccessor.Type == glTFLoader.Schema.Accessor.TypeEnum.VEC4)
                {
                    float r = GltfUtils.Clamp(vertexColors[index + 0], 0.0f, 1.0f);
                    float g = GltfUtils.Clamp(vertexColors[index + 1], 0.0f, 1.0f);
                    float b = GltfUtils.Clamp(vertexColors[index + 2], 0.0f, 1.0f);
                    float a = GltfUtils.Clamp(vertexColors[index + 3], 0.0f, 1.0f);

                    Rhino.Display.Color4f color = new Rhino.Display.Color4f(r, g, b, a);

                    rhinoMesh.VertexColors.Add(color.AsSystemColor());
                }
            }

            return(true);
        }