Ejemplo n.º 1
0
        public static Material LoadMat(string matPath, int renderQueue = -1)
        {
            Material material = (Material)Resources.Load("Materials/" + matPath, typeof(Material));

            if (material == null)
            {
                Log.Warning("Could not load material " + matPath);
            }
            Request request = default(Request);

            request.path        = matPath;
            request.renderQueue = renderQueue;
            Request key = request;

            if (!dict.TryGetValue(key, out Material value))
            {
                value = MaterialAllocator.Create(material);
                if (renderQueue != -1)
                {
                    value.renderQueue = renderQueue;
                }
                dict.Add(key, value);
            }
            return(value);
        }
Ejemplo n.º 2
0
        public static Material FadedVersionOf(Material sourceMat, float alpha)
        {
            int      num = FadedMaterialPool.IndexFromAlpha(alpha);
            Material result;

            if (num == 0)
            {
                result = BaseContent.ClearMat;
            }
            else if (num == 29)
            {
                result = sourceMat;
            }
            else
            {
                FadedMaterialPool.FadedMatRequest key = new FadedMaterialPool.FadedMatRequest(sourceMat, num);
                Material material;
                if (!FadedMaterialPool.cachedMats.TryGetValue(key, out material))
                {
                    material       = MaterialAllocator.Create(sourceMat);
                    material.color = new Color(1f, 1f, 1f, (float)FadedMaterialPool.IndexFromAlpha(alpha) / 30f);
                    FadedMaterialPool.cachedMats.Add(key, material);
                }
                result = material;
            }
            return(result);
        }
        public static Material FadedVersionOf(Material sourceMat, float alpha)
        {
            int num = IndexFromAlpha(alpha);

            switch (num)
            {
            case 0:
                return(BaseContent.ClearMat);

            case 29:
                return(sourceMat);

            default:
            {
                FadedMatRequest key = new FadedMatRequest(sourceMat, num);
                if (!cachedMats.TryGetValue(key, out var value))
                {
                    value       = MaterialAllocator.Create(sourceMat);
                    value.color = new Color(1f, 1f, 1f, (float)IndexFromAlpha(alpha) / 30f);
                    cachedMats.Add(key, value);
                }
                return(value);
            }
            }
        }
Ejemplo n.º 4
0
        public static Material LoadMat(string matPath, int renderQueue = -1)
        {
            Material material = (Material)Resources.Load("Materials/" + matPath, typeof(Material));

            if (material == null)
            {
                Log.Warning("Could not load material " + matPath, false);
            }
            MatLoader.Request key = new MatLoader.Request
            {
                path        = matPath,
                renderQueue = renderQueue
            };
            Material material2;

            if (!MatLoader.dict.TryGetValue(key, out material2))
            {
                material2 = MaterialAllocator.Create(material);
                if (renderQueue != -1)
                {
                    material2.renderQueue = renderQueue;
                }
                MatLoader.dict.Add(key, material2);
            }
            return(material2);
        }
Ejemplo n.º 5
0
        public static Material MatFrom(MaterialRequest req)
        {
            if (!UnityData.IsInMainThread)
            {
                Log.Error("Tried to get a material from a different thread.", false);
                return(null);
            }
            if (req.mainTex == null)
            {
                Log.Error("MatFrom with null sourceTex.", false);
                return(BaseContent.BadMat);
            }
            if (req.shader == null)
            {
                Log.Warning("Matfrom with null shader.", false);
                return(BaseContent.BadMat);
            }
            if (req.maskTex != null && !req.shader.SupportsMaskTex())
            {
                Log.Error("MaterialRequest has maskTex but shader does not support it. req=" + req.ToString(), false);
                req.maskTex = null;
            }
            Material material;

            if (!MaterialPool.matDictionary.TryGetValue(req, out material))
            {
                material             = MaterialAllocator.Create(req.shader);
                material.name        = req.shader.name + "_" + req.mainTex.name;
                material.mainTexture = req.mainTex;
                material.color       = req.color;
                if (req.maskTex != null)
                {
                    material.SetTexture(ShaderPropertyIDs.MaskTex, req.maskTex);
                    material.SetColor(ShaderPropertyIDs.ColorTwo, req.colorTwo);
                }
                if (req.renderQueue != 0)
                {
                    material.renderQueue = req.renderQueue;
                }
                if (!req.shaderParameters.NullOrEmpty <ShaderParameter>())
                {
                    for (int i = 0; i < req.shaderParameters.Count; i++)
                    {
                        req.shaderParameters[i].Apply(material);
                    }
                }
                MaterialPool.matDictionary.Add(req, material);
                if (!MaterialPool.matDictionary.ContainsKey(req))
                {
                    Log.Error("MaterialRequest is not present in the dictionary even though we've just added it there. The equality operators are most likely defined incorrectly.", false);
                }
                if (req.shader == ShaderDatabase.CutoutPlant || req.shader == ShaderDatabase.TransparentPlant)
                {
                    WindManager.Notify_PlantMaterialCreated(material);
                }
            }
            return(material);
        }
        public static Material Create(Shader shader)
        {
            Material material = new Material(shader);

            MaterialAllocator.references[material] = new MaterialAllocator.MaterialInfo
            {
                stackTrace = ((!Prefs.DevMode) ? "(unavailable)" : Environment.StackTrace)
            };
            MaterialAllocator.TryReport();
            return(material);
        }
Ejemplo n.º 7
0
 public static Material MatFrom(MaterialRequest req)
 {
     if (!UnityData.IsInMainThread)
     {
         Log.Error("Tried to get a material from a different thread.");
         return(null);
     }
     if (req.mainTex == null)
     {
         Log.Error("MatFrom with null sourceTex.");
         return(BaseContent.BadMat);
     }
     if (req.shader == null)
     {
         Log.Warning("Matfrom with null shader.");
         return(BaseContent.BadMat);
     }
     if (req.maskTex != null && !req.shader.SupportsMaskTex())
     {
         Log.Error("MaterialRequest has maskTex but shader does not support it. req=" + req.ToString());
         req.maskTex = null;
     }
     req.color    = (Color32)req.color;
     req.colorTwo = (Color32)req.colorTwo;
     if (!matDictionary.TryGetValue(req, out var value))
     {
         value             = MaterialAllocator.Create(req.shader);
         value.name        = req.shader.name + "_" + req.mainTex.name;
         value.mainTexture = req.mainTex;
         value.color       = req.color;
         if (req.maskTex != null)
         {
             value.SetTexture(ShaderPropertyIDs.MaskTex, req.maskTex);
             value.SetColor(ShaderPropertyIDs.ColorTwo, req.colorTwo);
         }
         if (req.renderQueue != 0)
         {
             value.renderQueue = req.renderQueue;
         }
         if (!req.shaderParameters.NullOrEmpty())
         {
             for (int i = 0; i < req.shaderParameters.Count; i++)
             {
                 req.shaderParameters[i].Apply(value);
             }
         }
         matDictionary.Add(req, value);
         if (req.shader == ShaderDatabase.CutoutPlant || req.shader == ShaderDatabase.TransparentPlant)
         {
             WindManager.Notify_PlantMaterialCreated(value);
         }
     }
     return(value);
 }
Ejemplo n.º 8
0
 public static Material GetInvisibleMat(Material baseMat)
 {
     if (!materials.TryGetValue(baseMat, out Material value))
     {
         value        = MaterialAllocator.Create(baseMat);
         value.shader = ShaderDatabase.Invisible;
         value.SetTexture(NoiseTex, TexGame.RippleTex);
         value.color = color;
         materials.Add(baseMat, value);
     }
     return(value);
 }
Ejemplo n.º 9
0
        public override void PostLoad()
        {
            placingDraggableDimensions = 2;
            LongEventHandler.ExecuteWhenFinished(delegate
            {
                Shader shader = null;
                switch (edgeType)
                {
                case TerrainEdgeType.Hard:
                    shader = ShaderDatabase.TerrainHard;
                    break;

                case TerrainEdgeType.Fade:
                    shader = ShaderDatabase.TerrainFade;
                    break;

                case TerrainEdgeType.FadeRough:
                    shader = ShaderDatabase.TerrainFadeRough;
                    break;

                case TerrainEdgeType.Water:
                    shader = ShaderDatabase.TerrainWater;
                    break;
                }
                graphic = GraphicDatabase.Get <Graphic_Terrain>(texturePath, shader, Vector2.one, color, 2000 + renderPrecedence);
                if (shader == ShaderDatabase.TerrainFadeRough || shader == ShaderDatabase.TerrainWater)
                {
                    graphic.MatSingle.SetTexture("_AlphaAddTex", TexGame.AlphaAddTex);
                }
                if (!waterDepthShader.NullOrEmpty())
                {
                    waterDepthMaterial             = MaterialAllocator.Create(ShaderDatabase.LoadShader(waterDepthShader));
                    waterDepthMaterial.renderQueue = 2000 + renderPrecedence;
                    waterDepthMaterial.SetTexture("_AlphaAddTex", TexGame.AlphaAddTex);
                    if (waterDepthShaderParameters != null)
                    {
                        for (int j = 0; j < waterDepthShaderParameters.Count; j++)
                        {
                            waterDepthShaderParameters[j].Apply(waterDepthMaterial);
                        }
                    }
                }
            });
            if (tools != null)
            {
                for (int i = 0; i < tools.Count; i++)
                {
                    tools[i].id = i.ToString();
                }
            }
            base.PostLoad();
        }
Ejemplo n.º 10
0
        public static Material NewSolidColorMaterial(Color col, Shader shader)
        {
            if (!UnityData.IsInMainThread)
            {
                Log.Error("Tried to create a material from a different thread.");
                return(null);
            }
            Material material = MaterialAllocator.Create(shader);

            material.color = col;
            material.name  = "SolidColorMat-" + shader.name + "-" + col;
            return(material);
        }
Ejemplo n.º 11
0
        public static Material GetDamageFlashMat(Material baseMat, float damPct)
        {
            if (damPct < 0.01f)
            {
                return(baseMat);
            }
            if (!damagedMats.TryGetValue(baseMat, out var value))
            {
                value = MaterialAllocator.Create(baseMat);
                damagedMats.Add(baseMat, value);
            }
            Color color2 = (value.color = Color.Lerp(baseMat.color, DamagedMatStartingColor, damPct));

            return(value);
        }
            public MaterialAtlas(Material newRootMat)
            {
                Vector2 mainTextureScale = new Vector2(0.1875f, 0.1875f);

                for (int i = 0; i < 16; i++)
                {
                    float    x = (float)(i % 4) * 0.25f + 0.03125f;
                    float    y = (float)(i / 4) * 0.25f + 0.03125f;
                    Vector2  mainTextureOffset = new Vector2(x, y);
                    Material material          = MaterialAllocator.Create(newRootMat);
                    material.name              = newRootMat.name + "_ASM" + i;
                    material.mainTextureScale  = mainTextureScale;
                    material.mainTextureOffset = mainTextureOffset;
                    this.subMats[i]            = material;
                }
            }
 public static void TryReport()
 {
     if (MaterialAllocator.MaterialWarningThreshold() > MaterialAllocator.nextWarningThreshold)
     {
         MaterialAllocator.nextWarningThreshold = MaterialAllocator.MaterialWarningThreshold();
     }
     if (MaterialAllocator.references.Count > MaterialAllocator.nextWarningThreshold)
     {
         Log.Error(string.Format("Material allocator has allocated {0} materials; this may be a sign of a material leak", MaterialAllocator.references.Count), false);
         if (Prefs.DevMode)
         {
             MaterialAllocator.MaterialReport();
         }
         MaterialAllocator.nextWarningThreshold *= 2;
     }
 }
        public static Material GetDamageFlashMat(Material baseMat, float damPct)
        {
            if (damPct < 0.01f)
            {
                return(baseMat);
            }
            Material material;

            if (!DamagedMatPool.damagedMats.TryGetValue(baseMat, out material))
            {
                material = MaterialAllocator.Create(baseMat);
                DamagedMatPool.damagedMats.Add(baseMat, material);
            }
            Color color = Color.Lerp(baseMat.color, DamagedMatPool.DamagedMatStartingColor, damPct);

            material.color = color;
            return(material);
        }
        public static Material NewSolidColorMaterial(Color col, Shader shader)
        {
            if (!UnityData.IsInMainThread)
            {
                Log.Error("Tried to create a material from a different thread.", false);
                return(null);
            }
            Material material = MaterialAllocator.Create(shader);

            material.color = col;
            material.name  = string.Concat(new object[]
            {
                "SolidColorMat-",
                shader.name,
                "-",
                col
            });
            return(material);
        }
Ejemplo n.º 16
0
        private void <PostLoad> m__0()
        {
            Shader shader = null;

            switch (this.edgeType)
            {
            case TerrainDef.TerrainEdgeType.Hard:
                shader = ShaderDatabase.TerrainHard;
                break;

            case TerrainDef.TerrainEdgeType.Fade:
                shader = ShaderDatabase.TerrainFade;
                break;

            case TerrainDef.TerrainEdgeType.FadeRough:
                shader = ShaderDatabase.TerrainFadeRough;
                break;

            case TerrainDef.TerrainEdgeType.Water:
                shader = ShaderDatabase.TerrainWater;
                break;
            }
            this.graphic = GraphicDatabase.Get <Graphic_Terrain>(this.texturePath, shader, Vector2.one, this.color, 2000 + this.renderPrecedence);
            if (shader == ShaderDatabase.TerrainFadeRough || shader == ShaderDatabase.TerrainWater)
            {
                this.graphic.MatSingle.SetTexture("_AlphaAddTex", TexGame.AlphaAddTex);
            }
            if (!this.waterDepthShader.NullOrEmpty())
            {
                this.waterDepthMaterial             = MaterialAllocator.Create(ShaderDatabase.LoadShader(this.waterDepthShader));
                this.waterDepthMaterial.renderQueue = 2000 + this.renderPrecedence;
                this.waterDepthMaterial.SetTexture("_AlphaAddTex", TexGame.AlphaAddTex);
                if (this.waterDepthShaderParameters != null)
                {
                    for (int i = 0; i < this.waterDepthShaderParameters.Count; i++)
                    {
                        this.waterDepthShaderParameters[i].Apply(this.waterDepthMaterial);
                    }
                }
            }
        }