コード例 #1
0
ファイル: DebugMaterial.cs プロジェクト: DeadZoneLuna/uSource
        public void Init(VMTFile VMT)
        {
            if (VMT != null && VMT.KeyValues != null)
            {
                if (builder == null)
                {
                    builder = new StringBuilder();
                }

                foreach (var a in (VMT.Include != null ? VMT.Include : VMT).KeyValues)
                {
                    builder.AppendLine(VMT.FileName);
                    builder.AppendLine(a.Key);
                    builder.AppendLine("{");

                    foreach (var b in a.Value)
                    {
                        builder.AppendLine($"    \"{b.Key}\"    \"{b.Value}\"");
                    }

                    builder.AppendLine("}");
                }

                Data += builder.ToString();
            }
        }
コード例 #2
0
ファイル: VMTFile.cs プロジェクト: DeadZoneLuna/uSource
        public void CreateMaterial()
        {
            #region Patch "Shader"
            if (ContainsParam("replace"))
            {
                this[ShaderType].MergeFrom(this[ShaderType]["replace"], true);
            }

            if (ContainsParam("include"))
            {
                Include = uResourceManager.LoadMaterial(GetParam("include"));
                this[ShaderType].MergeFrom(Include[Include.ShaderType], false);
            }

            if (ContainsParam("insert"))
            {
                this[ShaderType].MergeFrom(this[ShaderType]["insert"], false);
            }
            #endregion

            if (ContainsParam("$fallbackmaterial"))
            {
                Include = uResourceManager.LoadMaterial(GetParam("$fallbackmaterial"));
                this[ShaderType].MergeFrom(Include[Include.ShaderType], true);
            }

            //TODO
            //HasAnimation = ContainsParma("animatedtexture") && GetParma("animatedtexturevar") == "$basetexture";

#if UNITY_EDITOR
            //Try load asset from project (if exist)
            if (uLoader.SaveAssetsToUnity)
            {
                Material = uResourceManager.LoadAsset <Material>(FileName, uResourceManager.MaterialsExtension[0], ".mat");
                if (Material != null)
                {
                    return;
                }
            }
#endif

            String    TextureName;
            String    PropertyName;
            Texture2D BaseTexture = null;
            Boolean   HasAlpha    = false;
            if (ContainsParam("$basetexture") || ContainsParam("$envmapmask"))
            {
                TextureName = GetParam("$basetexture");
                if (TextureName == null || TextureName.Length == 0)
                {
                    TextureName = GetParam("$envmapmask");
                }

                BaseTexture = uResourceManager.LoadTexture(TextureName, ExportData: new String[, ] {
                    { FileName, "_MainTex" }
                })[0, 0];

                //To avoid "NullReferenceException"
                if (BaseTexture != null)
                {
                    HasAlpha = BaseTexture.alphaIsTransparency;
                }
            }

            Material       = new Material(GetShader(Include == null ? ShaderType : Include.ShaderType, HasAlpha));
            Material.name  = FileName;
            Material.color = GetColor();

            if (BaseTexture != null)
            {
                Material.mainTexture = BaseTexture;
            }

            if (ContainsParam("$basetexture2"))
            {
                TextureName  = GetParam("$basetexture2");
                PropertyName = "_SecondTex";
                if (Material.HasProperty(PropertyName))
                {
                    Material.SetTexture(PropertyName, uResourceManager.LoadTexture(TextureName, ExportData: new String[, ] {
                        { FileName, PropertyName }
                    })[0, 0]);
                }
            }

            if (ContainsParam("$envmapmask"))
            {
                PropertyName = "_AlphaMask";
                if (Material.HasProperty(PropertyName))
                {
                    if (BaseTexture != null && !HasAlpha)
                    {
                        TextureName = GetParam("$envmapmask");
                        Material.SetInt(PropertyName, 1);
                        Material.SetTexture("_MaskTex", uResourceManager.LoadTexture(TextureName, ExportData: new String[, ] {
                            { FileName, "_MaskTex" }
                        })[0, 0]);
                    }
                }
            }

            //Transparent
            if (IsTrue("$translucent"))
            {
                Material.renderQueue = TransparentQueue++;
            }

            //Cutout Transparent
            if (IsTrue("$alphatest"))
            {
                Material.renderQueue = (int)UnityEngine.Rendering.RenderQueue.AlphaTest;
            }

            //Cull mode state
            //0 - Off (Double Sided)
            //1 - Front (Front Sided)
            //2 - Back (Back Sided)
            if (IsTrue("$nocull"))
            {
                PropertyName = "_Cull";
                if (Material.HasProperty(PropertyName))
                {
                    Material.SetInt(PropertyName, 0);
                }
            }

            // Detail texture blend
            if (ContainsParam("$detail"))
            {
                PropertyName = "_Detail";
                if (Material.HasProperty(PropertyName))
                {
                    TextureName = GetParam("$detail");
                    Material.SetTexture(PropertyName, uResourceManager.LoadTexture(TextureName, ExportData: new String[, ] {
                        { FileName, PropertyName }
                    })[0, 0]);

                    if (ContainsParam("$detailscale"))
                    {
                        Material.SetTextureScale("_Detail", GetVector2("$detailscale"));
                    }

                    if (Material.HasProperty("_DetailFactor"))
                    {
                        if (ContainsParam("$detailblendfactor"))
                        {
                            Material.SetFloat("_DetailFactor", GetSingle("$detailblendfactor") / 2);
                        }
                        else
                        {
                            Material.SetFloat("_DetailFactor", 0.5f);
                        }

                        if (ContainsParam("$detailblendmode"))
                        {
                            Material.SetInt("_DetailBlendMode", GetInteger("$detailblendmode"));
                        }
                    }
                }
            }

            if (ContainsParam("$surfaceprop"))
            {
                SurfaceProp = GetParam("$surfaceprop");
            }
        }