// Monogame's BasicEffect uses Phong's shading, while glTF uses PBR shading, so // given monogame's limitations, we try to guess the most appropiate values // to have a reasonably good looking renders. public Effect UseRigidEffect(Schema2.Material srcMaterial) { if (_Device == null) { throw new InvalidOperationException(); } if (srcMaterial == null) { if (_DefaultRigid == null) { _DefaultRigid = new BasicEffect(_Device); _Disposables.AddDisposable(_DefaultRigid); } return(_DefaultRigid); } if (_RigidEffects.TryGetValue(srcMaterial, out Effect dstMaterial)) { return(dstMaterial); } dstMaterial = srcMaterial.Alpha == Schema2.AlphaMode.MASK ? CreateAlphaTestEffect(srcMaterial) : CreateBasicEffect(srcMaterial); _RigidEffects[srcMaterial] = dstMaterial; return(dstMaterial); }
private Effect CreateBasicEffect(Schema2.Material srcMaterial) { var dstMaterial = new BasicEffect(_Device); _Disposables.AddDisposable(dstMaterial); dstMaterial.Name = srcMaterial.Name; dstMaterial.Alpha = GetAlphaLevel(srcMaterial); dstMaterial.DiffuseColor = GetDiffuseColor(srcMaterial); dstMaterial.SpecularColor = GetSpecularColor(srcMaterial); dstMaterial.SpecularPower = GetSpecularPower(srcMaterial); dstMaterial.EmissiveColor = GeEmissiveColor(srcMaterial); dstMaterial.Texture = GetDiffuseTexture(srcMaterial); if (srcMaterial.Unlit) { dstMaterial.EmissiveColor = dstMaterial.DiffuseColor; dstMaterial.SpecularColor = Vector3.Zero; dstMaterial.SpecularPower = 16; } dstMaterial.PreferPerPixelLighting = true; dstMaterial.TextureEnabled = dstMaterial.Texture != null; return(dstMaterial); }
private static Vector3 GeEmissiveColor(Schema2.Material srcMaterial) { var emissive = srcMaterial.FindChannel("Emissive"); if (emissive == null) return Vector3.Zero; return new Vector3(emissive.Value.Parameter.X, emissive.Value.Parameter.Y, emissive.Value.Parameter.Z); }
private static Vector3 GetDiffuseColor(Schema2.Material srcMaterial) { var diffuse = srcMaterial.FindChannel("Diffuse"); if (diffuse == null) diffuse = srcMaterial.FindChannel("BaseColor"); if (diffuse == null) return Vector3.One; return new Vector3(diffuse.Value.Parameter.X, diffuse.Value.Parameter.Y, diffuse.Value.Parameter.Z); }
private static float GetAlphaLevel(Schema2.Material srcMaterial) { if (srcMaterial.Alpha == Schema2.AlphaMode.OPAQUE) return 1; var baseColor = srcMaterial.FindChannel("BaseColor"); if (baseColor == null) return 1; return baseColor.Value.Parameter.W; }
private static float GetSpecularPower(Schema2.Material srcMaterial) { var mr = srcMaterial.FindChannel("MetallicRoughness"); if (mr == null) return 16; // default value = 16 var metallic = mr.Value.Parameter.X; var roughness = mr.Value.Parameter.Y; return 4 + 16 * metallic; }
private Texture2D GetDiffuseTexture(Schema2.Material srcMaterial) { var diffuse = srcMaterial.FindChannel("Diffuse"); if (diffuse == null) diffuse = srcMaterial.FindChannel("BaseColor"); if (diffuse == null) return null; var name = srcMaterial.Name; if (name == null) name = "null"; name += "-Diffuse"; return _TexFactory.UseTexture(diffuse.Value.Texture?.PrimaryImage?.GetImageContent() ?? default, name); }
public Effect UseSkinnedEffect(Schema2.Material srcMaterial) { if (_Device == null) { throw new InvalidOperationException(); } if (srcMaterial == null) { if (_DefaultSkinned == null) { _DefaultSkinned = new SkinnedEffect(_Device); _Disposables.AddDisposable(_DefaultRigid); } return(_DefaultSkinned); } if (_SkinnedEffects.TryGetValue(srcMaterial, out SkinnedEffect dstMaterial)) { return(dstMaterial); } dstMaterial = new SkinnedEffect(_Device); _SkinnedEffects[srcMaterial] = dstMaterial; _Disposables.AddDisposable(dstMaterial); dstMaterial.Name = srcMaterial.Name; dstMaterial.Alpha = GetAlphaLevel(srcMaterial); dstMaterial.DiffuseColor = GetDiffuseColor(srcMaterial); dstMaterial.SpecularColor = GetSpecularColor(srcMaterial); dstMaterial.SpecularPower = GetSpecularPower(srcMaterial); dstMaterial.EmissiveColor = GeEmissiveColor(srcMaterial); dstMaterial.Texture = GetDiffuseTexture(srcMaterial); dstMaterial.WeightsPerVertex = 4; dstMaterial.PreferPerPixelLighting = true; // apparently, SkinnedEffect does not support disabling textures, so we set a white texture here. if (dstMaterial.Texture == null) { dstMaterial.Texture = _TexFactory.UseWhiteImage(); } return(dstMaterial); }
private static Vector3 GetSpecularColor(Schema2.Material srcMaterial) { var mr = srcMaterial.FindChannel("MetallicRoughness"); if (mr == null) return Vector3.One; // default value 16 var diffuse = GetDiffuseColor(srcMaterial); 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 k; }
private Effect CreateAlphaTestEffect(Schema2.Material srcMaterial) { var dstMaterial = new AlphaTestEffect(_Device); _Disposables.AddDisposable(dstMaterial); dstMaterial.Name = srcMaterial.Name; dstMaterial.Alpha = GetAlphaLevel(srcMaterial); //dstMaterial.AlphaFunction = CompareFunction.GreaterEqual; dstMaterial.ReferenceAlpha = (int)(srcMaterial.AlphaCutoff * 255); dstMaterial.DiffuseColor = GetDiffuseColor(srcMaterial); dstMaterial.Texture = GetDiffuseTexture(srcMaterial); return dstMaterial; }
public Effect GetMaterial(Schema2.Material srcMaterial, bool isSkinned) { if (isSkinned) { if (_SkinnedEffects.TryGetValue(srcMaterial, out SkinnedEffect dstMaterial)) { return(dstMaterial); } } else { if (_RigidEffects.TryGetValue(srcMaterial, out Effect dstMaterial)) { return(dstMaterial); } } return(null); }
private Texture2D GetDiffuseTexture(Schema2.Material srcMaterial) { var diffuse = srcMaterial.FindChannel("Diffuse"); if (diffuse == null) { diffuse = srcMaterial.FindChannel("BaseColor"); } if (diffuse == null) { return(null); } var name = srcMaterial.Name; if (name == null) { name = "null"; } name += "-Diffuse"; return(_TexFactory.UseTexture(diffuse.Value.Texture?.PrimaryImage?.Content ?? default, name)); }
private static Material GetMaterialFromTriangle(Schema2.Material srcMaterial) { if (srcMaterial == null) { return(default);