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); }
private bool CreateShaderPart(ShaderBody shb, RenderMaterial rm, float gamma) { var crm = rm as ICyclesMaterial; ShaderBody.CyclesMaterial mattype = ShaderBody.CyclesMaterial.No; if (crm == null) { // always simulate material, need to know now myself // what to read out from the simulated material to // populate my own material descriptions. var m = rm.SimulateMaterial(true); // figure out what type of material we are. //var probemat = GuessMaterialFromSmell(rm); var probemat = WhatMaterial(rm, m); rm.BeginChange(RenderContent.ChangeContexts.Ignore); var dcl = m.DiffuseColor; var scl = m.SpecularColor; var rcl = m.ReflectionColor; var rfcl = m.TransparentColor; var emcl = m.EmissionColor; var polish = (float)m.ReflectionGlossiness; var reflectivity = (float)m.Reflectivity; var metalic = 0f; var shine = (float)(m.Shine / Material.MaxShine); switch (probemat) { case ProbableMaterial.Plaster: mattype = ShaderBody.CyclesMaterial.Diffuse; break; case ProbableMaterial.Glass: case ProbableMaterial.Gem: metalic = 0f; mattype = ShaderBody.CyclesMaterial.Glass; break; case ProbableMaterial.Metal: metalic = 1.0f; mattype = ShaderBody.CyclesMaterial.SimpleMetal; break; case ProbableMaterial.Plastic: //polish = reflectivity; //shine = polish; //reflectivity = 0f; metalic = 0f; mattype = ShaderBody.CyclesMaterial.SimplePlastic; break; case ProbableMaterial.Paint: mattype = ShaderBody.CyclesMaterial.Paint; break; case ProbableMaterial.Custom: mattype = ShaderBody.CyclesMaterial.No; break; } var difftexAlpha = m.AlphaTransparency; var col = RenderEngine.CreateFloat4(dcl.R, dcl.G, dcl.B, 255); var spec = RenderEngine.CreateFloat4(scl.R, scl.G, scl.B, 255); var refl = RenderEngine.CreateFloat4(rcl.R, rcl.G, rcl.B, 255); var transp = RenderEngine.CreateFloat4(rfcl.R, rfcl.G, rfcl.B, 255); var refr = RenderEngine.CreateFloat4(rfcl.R, rfcl.G, rfcl.B, 255); var emis = RenderEngine.CreateFloat4(emcl.R, emcl.G, emcl.B, 255); //shb.Type = CyclesShader.Shader.Diffuse, shb.CyclesMaterialType = mattype; shb.Shadeless = m.DisableLighting; shb.DiffuseColor = col; shb.SpecularColor = spec; shb.ReflectionColor = refl; shb.ReflectionRoughness = (float)m.ReflectionGlossiness; // polish; shb.RefractionColor = refr; shb.RefractionRoughness = (float)m.RefractionGlossiness; shb.TransparencyColor = transp; shb.EmissionColor = emis; shb.FresnelIOR = (float)m.FresnelIndexOfRefraction; shb.IOR = (float)m.IndexOfRefraction; shb.Roughness = (float)m.ReflectionGlossiness; shb.Reflectivity = reflectivity; shb.Metalic = metalic; shb.Transparency = (float)m.Transparency; shb.Shine = shine; shb.Gloss = (float)m.ReflectionGlossiness; shb.FresnelReflections = m.FresnelReflections; shb.Gamma = gamma; shb.Name = m.Name ?? ""; shb.DiffuseTexture.Amount = 0.0f; shb.BumpTexture.Amount = 0.0f; shb.TransparencyTexture.Amount = 0.0f; shb.EnvironmentTexture.Amount = 0.0f; if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Diffuse)) { var difftex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Diffuse); BitmapConverter.MaterialBitmapFromEvaluator(ref shb, rm, difftex, RenderMaterial.StandardChildSlots.Diffuse); if (shb.HasDiffuseTexture) { shb.DiffuseTexture.UseAlpha = difftexAlpha; shb.DiffuseTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Diffuse) / 100.0f, 1.0f); } } if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Bump)) { var bumptex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Bump); BitmapConverter.MaterialBitmapFromEvaluator(ref shb, rm, bumptex, RenderMaterial.StandardChildSlots.Bump); if (shb.HasBumpTexture) { shb.BumpTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Bump) / 100.0f, 1.0f); } } if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Transparency)) { var transtex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Transparency); BitmapConverter.MaterialBitmapFromEvaluator(ref shb, rm, transtex, RenderMaterial.StandardChildSlots.Transparency); if (shb.HasTransparencyTexture) { shb.TransparencyTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Transparency) / 100.0f, 1.0f); } } if (rm.GetTextureOnFromUsage(RenderMaterial.StandardChildSlots.Environment)) { var envtex = rm.GetTextureFromUsage(RenderMaterial.StandardChildSlots.Environment); BitmapConverter.MaterialBitmapFromEvaluator(ref shb, rm, envtex, RenderMaterial.StandardChildSlots.Environment); if (shb.HasEnvironmentTexture) { shb.EnvironmentTexture.Amount = (float)Math.Min(rm.GetTextureAmountFromUsage(RenderMaterial.StandardChildSlots.Environment) / 100.0f, 1.0f); } } rm.EndChange(); } else { crm.BakeParameters(); shb.Crm = crm; shb.CyclesMaterialType = ShaderBody.CyclesMaterial.Xml; shb.Gamma = gamma; shb.Name = rm.Name ?? "some cycles material"; } return(true); }