using LibMMD.Material; using UnityEngine; using UnityEngine.Rendering; namespace LibMMD.Unity3D { public sealed class MmdBuiltInMaterialAdapter : MmdMaterialAdapterBase { public override string Id => MmdMaterialAdapterIds.Mmd; public override string DisplayName => "MMD"; public override Shader ResolveShader(MmdMaterial source, MmdMaterialContext context) { return FindFirst(context.IsTransparent ? "MMD/Runtime/Transparent/PMDMaterial" : "MMD/Runtime/PMDMaterial"); } public override void Apply(UnityEngine.Material target, MmdMaterial source, MmdMaterialContext context) { base.Apply(target, source, context); target.SetFloat("_CullMode", source.DrawDoubleFace ? (float)CullMode.Off : (float)CullMode.Back); target.SetFloat("_OutlineEnabled", context.EnableOutline ? 1.0f : 0.0f); target.SetFloat("_ReceiveShadows", context.ReceiveShadows ? 1.0f : 0.0f); target.SetShaderPassEnabled("Outline", context.EnableOutline); target.SetShaderPassEnabled("ShadowCaster", context.CastShadows); target.DisableKeyword("_ALPHABLEND_ON"); target.DisableKeyword("_SURFACE_TYPE_TRANSPARENT"); target.DisableKeyword("_RECEIVE_SHADOWS_OFF"); target.renderQueue = -1; SetFloat(target, "_Opacity", source.DiffuseColor.a); SetColor(target, "_AmbColor", source.AmbientColor); SetColor(target, "_SpecularColor", source.SpecularColor); SetFloat(target, "_Shininess", source.Shiness); SetTexture(target, "_ToonTex", context.ToonTexture); if (context.SphereTextureType == MmdMaterial.SubTextureTypeEnum.MatSubTexSpa) SetTexture(target, "_SphereAddTex", context.SphereTexture); else if (context.SphereTextureType == MmdMaterial.SubTextureTypeEnum.MatSubTexSph) SetTexture(target, "_SphereMulTex", context.SphereTexture); SetKeyword(target, "SELFSHADOW_ON", context.EnableSelfShadow); SetKeyword(target, "SELFSHADOW_OFF", !context.EnableSelfShadow); SetFloat(target, "_SelfShadowFlat", context.ReceiveShadows ? 0.0f : 1.0f); } } }