コード例 #1
0
        public UnityEngine.Material LoadMaterial(MMDMaterial mmdMaterial, MMDUnityConfig config)
        {
            var mainTexture   = _textureLoader.LoadTexture(mmdMaterial.Texture);
            var isTransparent = mmdMaterial.DiffuseColor.a < 0.9999f || mmdMaterial.EdgeColor.a < 0.9999f ||
                                IsTextireTransparent(mainTexture);
            var material = new UnityEngine.Material(GetShader(mmdMaterial, config, isTransparent));

            ConfigMaterial(mmdMaterial, config, material, mainTexture);
            return(material);
        }
コード例 #2
0
        private static Shader GetShader(MMDMaterial mmdMaterial, MMDUnityConfig config, bool isTransparent)
        {
            //"MMD/Transparent/PMDMaterial-with-Outline-CullBack-NoCastShadow"
            var shaderName = BuildShaderName(mmdMaterial, config, isTransparent);
            var ret        = Shader.Find(shaderName);

            if (ret == null)
            {
                Debug.LogWarning("Can't find shader " + shaderName);
            }
            return(ret);
        }
コード例 #3
0
        public void RefreshMaterialConfig(MMDMaterial mmdMaterial, MMDUnityConfig config, UnityEngine.Material material)
        {
            var mainTexture   = _textureLoader.LoadTexture(mmdMaterial.Texture);
            var isTransparent = mmdMaterial.DiffuseColor.a < 0.9999f || mmdMaterial.EdgeColor.a < 0.9999f ||
                                IsTextireTransparent(mainTexture);
            var shaderName = BuildShaderName(mmdMaterial, config, isTransparent);

            if (!material.shader.name.Equals(shaderName))
            {
                material.shader = Shader.Find(shaderName);
            }
            ConfigMaterial(mmdMaterial, config, material, mainTexture);
        }
コード例 #4
0
        private static MMDMaterial ReadMaterial(BinaryReader reader, ModelReadConfig config, Encoding encoding,
                                                int textureIndexSize, MMDTexture[] textureList)
        {
            var material = new MMDMaterial();

            material.Name          = MMDReaderUtil.ReadSizedString(reader, encoding);
            material.NameEn        = MMDReaderUtil.ReadSizedString(reader, encoding);
            material.DiffuseColor  = MMDReaderUtil.ReadColor(reader, true);
            material.SpecularColor = MMDReaderUtil.ReadColor(reader, false);
            material.Shiness       = reader.ReadSingle();
            material.AmbientColor  = MMDReaderUtil.ReadColor(reader, false);
            var drawFlag = reader.ReadByte();

            material.DrawDoubleFace   = (drawFlag & PmxMaterialDrawFlags.PmxMaterialDrawDoubleFace) != 0;
            material.DrawGroundShadow = (drawFlag & PmxMaterialDrawFlags.PmxMaterialDrawGroundShadow) != 0;
            material.CastSelfShadow   = (drawFlag & PmxMaterialDrawFlags.PmxMaterialCastSelfShadow) != 0;
            material.DrawSelfShadow   = (drawFlag & PmxMaterialDrawFlags.PmxMaterialDrawSelfShadow) != 0;
            material.DrawEdge         = (drawFlag & PmxMaterialDrawFlags.PmxMaterialDrawEdge) != 0;
            material.EdgeColor        = MMDReaderUtil.ReadColor(reader, true);
            material.EdgeSize         = reader.ReadSingle();
            var textureIndex = MMDReaderUtil.ReadIndex(reader, textureIndexSize);

            if (textureIndex < textureList.Length && textureIndex >= 0)
            {
                material.Texture = textureList[textureIndex];
            }
            var subTextureIndex = MMDReaderUtil.ReadIndex(reader, textureIndexSize);

            if (subTextureIndex < textureList.Length && subTextureIndex >= 0)
            {
                material.SubTexture = textureList[subTextureIndex];
            }
            material.SubTextureType = (MMDMaterial.SubTextureTypeEnum)reader.ReadByte();
            var useGlobalToon = reader.ReadByte() != 0;

            if (useGlobalToon)
            {
                int globalToonIndex = reader.ReadByte();
                material.Toon = MMDTextureUtil.GetGlobalToon(globalToonIndex, config.GlobalToonPath);
            }
            else
            {
                var toonIndex = MMDReaderUtil.ReadIndex(reader, textureIndexSize);
                if (toonIndex < textureList.Length && toonIndex >= 0)
                {
                    material.Toon = textureList[toonIndex];
                }
            }
            material.MetaInfo = MMDReaderUtil.ReadSizedString(reader, encoding);
            return(material);
        }
コード例 #5
0
        private void ConfigMaterial(MMDMaterial mmdMaterial, MMDUnityConfig config, UnityEngine.Material material, Texture mainTexture)
        {
            material.SetColor("_Color", mmdMaterial.DiffuseColor);
            material.SetFloat("_Opacity", mmdMaterial.DiffuseColor.a);
            material.SetColor("_AmbColor", mmdMaterial.AmbientColor);
            material.SetColor("_SpecularColor", mmdMaterial.SpecularColor);
            material.SetFloat("_Shininess", mmdMaterial.Shiness);
            material.SetFloat("_OutlineWidth", mmdMaterial.EdgeSize);
            material.SetColor("_OutlineColor", mmdMaterial.EdgeColor);
            if (mainTexture != null)
            {
                material.mainTexture      = mainTexture;
                material.mainTextureScale = new Vector2(1, 1);
            }


            if (mmdMaterial.SubTextureType != MMDMaterial.SubTextureTypeEnum.MatSubTexOff)
            {
                var additionalTexture =
                    mmdMaterial.SubTexture == null ? null : _textureLoader.LoadTexture(mmdMaterial.SubTexture);
                if (additionalTexture != null)
                {
                    additionalTexture.wrapMode = TextureWrapMode.Clamp;
                    switch (mmdMaterial.SubTextureType)
                    {
                    case MMDMaterial.SubTextureTypeEnum.MatSubTexSpa:
                        material.SetTexture("_SphereAddTex", additionalTexture);
                        material.SetTextureScale("_SphereAddTex", new Vector2(1, 1));
                        break;

                    case MMDMaterial.SubTextureTypeEnum.MatSubTexSph:
                        material.SetTexture("_SphereMulTex", additionalTexture);
                        material.SetTextureScale("_SphereMulTex", new Vector2(1, 1));
                        break;
                    }
                }
            }

            RefreshShaderKeywords(mmdMaterial, config, material);

            var toonTexture = _textureLoader.LoadTexture(mmdMaterial.Toon);

            if (toonTexture != null)
            {
                toonTexture.wrapMode = TextureWrapMode.Clamp;
                material.SetTexture("_ToonTex", toonTexture);
                material.SetTextureScale("_ToonTex", new Vector2(1, 1));
            }
        }
コード例 #6
0
        private static string BuildShaderName(MMDMaterial mmdMaterial, MMDUnityConfig config, bool isTransparent)
        {
            var shaderNameBuilder = new StringBuilder();

            shaderNameBuilder.Append("VRM/");
            if (isTransparent)
            {
                shaderNameBuilder.Append("UnlitTransparentZWrite");
            }
            else
            {
                shaderNameBuilder.Append("UnlitTexture");
            }

            var shaderName = shaderNameBuilder.ToString();

            return(shaderName);
        }
コード例 #7
0
        private static void ReadParts(BinaryReader reader, RawMMDModel model, List <int> tooTextureIds)
        {
            var partNum       = reader.ReadInt32();
            var partBaseShift = 0;

            model.Parts = new Part[partNum];
            for (var i = 0; i < partNum; ++i)
            {
                var material = new MMDMaterial();
                material.DiffuseColor  = MMDReaderUtil.ReadColor(reader, true);
                material.Shiness       = reader.ReadSingle();
                material.SpecularColor = MMDReaderUtil.ReadColor(reader, false);
                material.AmbientColor  = MMDReaderUtil.ReadColor(reader, false);
                var toonId      = reader.ReadByte();
                var edgeFlag    = reader.ReadSByte();
                var vertexNum   = reader.ReadInt32();
                var textureName = MMDReaderUtil.ReadStringFixedLength(reader, 20, Tools.JapaneseEncoding);
                material.DrawDoubleFace   = material.DiffuseColor.a < 0.9999f;
                material.DrawGroundShadow = edgeFlag != 0;
                material.CastSelfShadow   = true;
                material.DrawSelfShadow   = true;
                material.DrawEdge         = edgeFlag != 0;
                material.EdgeColor        = Color.black;
                if (!string.IsNullOrEmpty(textureName))
                {
                    var dlmPos = textureName.IndexOf("*", StringComparison.Ordinal);
                    if (dlmPos >= 0)
                    {
                        var texPath = textureName.Substring(0, dlmPos);
                        var sphPath = textureName.Substring(dlmPos + 1);
                        if (!string.IsNullOrEmpty(texPath))
                        {
                            material.Texture = new MMDTexture(texPath);
                        }
                        if (!string.IsNullOrEmpty(sphPath))
                        {
                            material.SubTexture = new MMDTexture(sphPath);
                        }
                        if (sphPath.EndsWith("a") || sphPath.EndsWith("A"))
                        {
                            material.SubTextureType = MMDMaterial.SubTextureTypeEnum.MatSubTexSpa;
                        }
                        else
                        {
                            material.SubTextureType = MMDMaterial.SubTextureTypeEnum.MatSubTexSph;
                        }
                    }
                    else
                    {
                        var extDlmPos = textureName.LastIndexOf(".", StringComparison.Ordinal);
                        if (extDlmPos >= 0)
                        {
                            var ext = textureName.Substring(extDlmPos + 1);
                            ext = ext.ToLower();
                            if (!ext.Equals("sph") && !ext.Equals("spa"))
                            {
                                material.Texture = new MMDTexture(textureName);
                            }
                            else
                            {
                                material.SubTexture = new MMDTexture(textureName);
                                ;
                                if (ext.EndsWith("a"))
                                {
                                    material.SubTextureType = MMDMaterial.SubTextureTypeEnum.MatSubTexSpa;
                                }
                                else
                                {
                                    material.SubTextureType = MMDMaterial.SubTextureTypeEnum.MatSubTexSph;
                                }
                            }
                        }
                        else
                        {
                            material.Texture = new MMDTexture(textureName);
                        }
                    }
                }
                var part = new Part();
                part.Material = material;
                tooTextureIds.Add(toonId);
                part.BaseShift        = partBaseShift;
                part.TriangleIndexNum = vertexNum;
                partBaseShift        += vertexNum;
                model.Parts[i]        = part;
            }
        }
コード例 #8
0
 private void RefreshShaderKeywords(MMDMaterial mmdMaterial, MMDUnityConfig config, UnityEngine.Material material)
 {
     RefreshDrawSelfShadowKeyword(MMDUnityConfig.DealSwitch(config.EnableDrawSelfShadow, mmdMaterial.DrawSelfShadow),
                                  material);
 }