private Material CreateMaterial(SharpGLTF.Schema2.Material mat, string ddsPath, int id) { if (mat != null) { return(new Material() { Id = (uint)id, Shader = DecodeShaderName(mat.Name), Textures = new[] { GetBaseColorTexture(mat, ddsPath), GetRoughnessTexture(mat, ddsPath), mat.FindChannel(KnownChannel.Normal.ToString())?.Texture?.PrimaryImage?.Name, } }); } else { return(new Material() { Id = (uint)id, Shader = GmdUtils.PBR_MAT, Textures = new string[] { } }); } }
private static int GetMaterialColor(Schema2.Material material) { var mb = new MaterialBuilder(); material.CopyTo(mb); return(GetMaterialColor(mb)); }
/// <summary> /// Gets the specular color for the specified material. /// </summary> protected Color GetMaterialSpecularColor(SharpGLTF.Schema2.Material material) { if (material == null) { return(Color.White); } var mr = material.FindChannel("MetallicRoughness"); if (mr == null) { return(Color.White); } var diffuse = GetMaterialDiffuseColor(material).ToVector3(); var metallic = mr.Value.Parameter.X; var roughness = mr.Value.Parameter.Y; var k = Vector3.Zero; k += Vector3.Lerp(diffuse, Vector3.Zero, roughness); k += Vector3.Lerp(diffuse, Vector3.One, metallic); k *= 0.5f; return(new Color(k)); }
private string GetRoughnessTexture(SharpGLTF.Schema2.Material mat, string ddsPath) { var channel = mat.FindChannel(KnownChannel.MetallicRoughness.ToString()); if (channel?.Texture?.PrimaryImage?.Name != null) { return(channel.Value.Texture.PrimaryImage.Name); } if (ddsPath != null) { var filename = $"{Guid.NewGuid().ToString().Substring(0, 8)}.dds"; var converter = new ImageConverter(ddsPath); if (channel.HasValue) { converter.GenerateMetallicRoughnessOcclusion(filename, channel.Value.Parameter.X, channel.Value.Parameter.Y, 1); } else { converter.GenerateMetallicRoughnessOcclusion(filename, 0, 0.8f, 1); } return(Path.GetFileNameWithoutExtension(filename)); } return(null); }
/// <summary> /// Gets the diffuse color for the specified material. /// </summary> protected Color GetMaterialDiffuseColor(SharpGLTF.Schema2.Material material) { if (material == null) { return(Color.White); } var diffuse = material.FindChannel("Diffuse") ?? material.FindChannel("BaseColor"); if (diffuse == null) { return(Color.White); } return(new Color(diffuse.Value.Parameter.X, diffuse.Value.Parameter.Y, diffuse.Value.Parameter.Z)); }
/// <summary> /// Gets the emissive color for the specified material. /// </summary> protected Color GetMaterialEmissiveColor(SharpGLTF.Schema2.Material material) { if (material == null) { return(Color.Black); } var emissive = material.FindChannel("Emissive"); if (emissive == null) { return(Color.Black); } return(new Color(emissive.Value.Parameter.X, emissive.Value.Parameter.Y, emissive.Value.Parameter.Z)); }
private void _WriteMeshPrimitive(MeshPrimitiveReader srcPrim, SRCMATERIAL srcMaterial) { if (srcMaterial == null) { srcMaterial = GetDefaultMaterial(); } var effect = _MatFactory.GetMaterial(srcMaterial, srcPrim.IsSkinned); if (effect == null) { effect = CreateEffect(srcMaterial, srcPrim.IsSkinned); _MatFactory.Register(srcMaterial, srcPrim.IsSkinned, effect); } WriteMeshPrimitive(srcPrim, effect); }
/// <summary> /// Gets the specular power for the specified material. /// </summary> protected Single GetMaterialSpecularPower(SharpGLTF.Schema2.Material material) { if (material == null) { return(16f); } var mr = material.FindChannel("MetallicRoughness"); if (mr == null) { return(16f); } var metallic = mr.Value.Parameter.X; return(4f + 16f * metallic); }
private string GetBaseColorTexture(SharpGLTF.Schema2.Material mat, string ddsPath) { var channel = mat.FindChannel(KnownChannel.BaseColor.ToString()); if (channel?.Texture?.PrimaryImage?.Name != null) { return(channel.Value.Texture.PrimaryImage.Name); } if (ddsPath != null && channel.HasValue) { var filename = $"{Guid.NewGuid().ToString().Substring(0, 8)}.dds"; var converter = new ImageConverter(ddsPath); converter.GenerateColorTexture(filename, channel.Value.Parameter.X, channel.Value.Parameter.Y, channel.Value.Parameter.Z, channel.Value.Parameter.W); return(Path.GetFileNameWithoutExtension(filename)); } return(null); }
/// <summary> /// Gets the alpha for the specified material. /// </summary> protected Single GetMaterialAlpha(SharpGLTF.Schema2.Material material) { if (material == null) { return(1f); } if (material.Alpha == AlphaMode.OPAQUE) { return(1f); } var baseColor = material.FindChannel("BaseColor"); if (baseColor == null) { return(1f); } return(baseColor.Value.Parameter.W); }
/// <summary> /// Gets the texture for the specified material. /// </summary> protected Texture2D GetMaterialTexture(ContentManager contentManager, SharpGLTF.Schema2.Material material) { if (material == null) { return(null); } var diffuse = material.FindChannel("Diffuse") ?? material.FindChannel("BaseColor"); if (diffuse == null) { return(null); } var texture = diffuse.Value.Texture; if (texture != null) { var textureName = $"{diffuse.Value.LogicalParent.Name ?? "null"}-{diffuse.Value.Key}"; var textureContent = texture.PrimaryImage?.Content; if (textureContent != null && textureContent.Value.IsValid) { if (!textureCache.TryGetValue(textureName, out var textureResource)) { using (var textureStream = textureContent.Value.Open()) { textureResource = contentManager.LoadFromStream <Texture2D>(textureStream, textureContent.Value.FileExtension); } textureCache[textureName] = textureResource; } return(textureResource); } } return(null); }