ExtractOptimizedShaders() private static method

private static ExtractOptimizedShaders ( List materials, Shader &optimizedSolidShader, Shader &optimizedCutoutShader ) : void
materials List
optimizedSolidShader UnityEngine.Shader
optimizedCutoutShader UnityEngine.Shader
return void
コード例 #1
0
ファイル: TreeData.cs プロジェクト: zhkuang/UnityDecompiled
        public bool OptimizeMaterial(List <TreeMaterial> materials, List <TreeVertex> vertices, List <TreeTriangle> triangles)
        {
            if (!this.optimizedSolidMaterial || !this.optimizedCutoutMaterial)
            {
                Debug.LogError("Optimized materials haven't been assigned");
                return(false);
            }
            Shader shader;
            Shader shader2;

            TreeData.ExtractOptimizedShaders(materials, out shader, out shader2);
            this.optimizedSolidMaterial.shader  = shader;
            this.optimizedCutoutMaterial.shader = shader2;
            int num     = 1024;
            int num2    = 1024;
            int padding = 32;

            Profiler.BeginSample("OptimizeMaterial");
            float[] array = new float[materials.Count];
            float   num3  = 0f;
            float   num4  = 0f;

            for (int i = 0; i < materials.Count; i++)
            {
                if (!materials[i].tileV)
                {
                    num4 += 1f;
                }
                else
                {
                    num3 += 1f;
                }
            }
            for (int j = 0; j < materials.Count; j++)
            {
                if (materials[j].tileV)
                {
                    array[j] = 1f;
                }
                else
                {
                    array[j] = 1f / num4;
                }
            }
            TextureAtlas textureAtlas = new TextureAtlas();

            for (int k = 0; k < materials.Count; k++)
            {
                Texture2D texture2D       = null;
                Texture2D normal          = null;
                Texture2D gloss           = null;
                Texture2D transtex        = null;
                Texture2D shadowOffsetTex = null;
                Color     color           = new Color(1f, 1f, 1f, 1f);
                float     num5            = 0.03f;
                Vector2   textureScale    = new Vector2(1f, 1f);
                Material  material        = materials[k].material;
                if (material)
                {
                    if (material.HasProperty("_Color"))
                    {
                        color = material.GetColor("_Color");
                    }
                    if (material.HasProperty("_MainTex"))
                    {
                        texture2D    = (material.mainTexture as Texture2D);
                        textureScale = material.GetTextureScale("_MainTex");
                    }
                    if (material.HasProperty("_BumpMap"))
                    {
                        normal = (material.GetTexture("_BumpMap") as Texture2D);
                    }
                    if (material.HasProperty("_GlossMap"))
                    {
                        gloss = (material.GetTexture("_GlossMap") as Texture2D);
                    }
                    if (material.HasProperty("_TranslucencyMap"))
                    {
                        transtex = (material.GetTexture("_TranslucencyMap") as Texture2D);
                    }
                    if (material.HasProperty("_Shininess"))
                    {
                        num5 = material.GetFloat("_Shininess");
                    }
                    if (material.HasProperty("_ShadowOffset"))
                    {
                        shadowOffsetTex = (material.GetTexture("_ShadowOffset") as Texture2D);
                    }
                }
                num5 = Mathf.Clamp(num5, 0.03f, 1f);
                Vector2 scale = new Vector2(array[k], array[k]);
                if (texture2D)
                {
                    scale.x *= (float)num / (float)texture2D.width;
                    scale.y *= (float)num2 / (float)texture2D.height;
                }
                bool tileV = materials[k].tileV;
                if (!tileV)
                {
                    textureScale = new Vector2(1f, 1f);
                }
                textureAtlas.AddTexture("tex" + k, texture2D, color, normal, gloss, transtex, shadowOffsetTex, num5, scale, tileV, textureScale);
            }
            textureAtlas.Pack(ref num, num2, padding, true);
            this.UpdateTextures(textureAtlas, materials);
            Rect    rect      = default(Rect);
            Vector2 texTiling = new Vector2(1f, 1f);
            int     num6      = -1;

            for (int l = 0; l < triangles.Count; l++)
            {
                TreeTriangle treeTriangle = triangles[l];
                if (treeTriangle.materialIndex != num6)
                {
                    num6      = treeTriangle.materialIndex;
                    rect      = textureAtlas.GetUVRect("tex" + treeTriangle.materialIndex);
                    texTiling = textureAtlas.GetTexTiling("tex" + treeTriangle.materialIndex);
                }
                for (int m = 0; m < 3; m++)
                {
                    TreeVertex treeVertex = vertices[treeTriangle.v[m]];
                    if (!treeVertex.flag)
                    {
                        treeVertex.uv0.x = rect.x + treeVertex.uv0.x * rect.width;
                        treeVertex.uv0.y = (rect.y + treeVertex.uv0.y * rect.height) * texTiling.y;
                        treeVertex.flag  = true;
                    }
                }
                if (treeTriangle.isCutout)
                {
                    treeTriangle.materialIndex = 1;
                }
                else
                {
                    treeTriangle.materialIndex = 0;
                }
            }
            Profiler.EndSample();
            return(true);
        }