Beispiel #1
0
 /// <summary>
 /// Creates a new empty <see cref="RenderMaterial"/> Instance
 /// </summary>
 /// <returns></returns>
 private static RenderMaterial CreateEmptyMaterial()
 {
     return(RenderContentType
            .NewContentFromTypeId(
                ContentUuids.PhysicallyBasedMaterialType)
            as RenderMaterial);
 }
        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);
        }