private Color32[] GetTextureColors(
            Texture2D source,
            TextureFormat format,
            int dimensions)
        {
            Texture2D texture2D = CTSProfile.ResizeTexture(source, format, this.m_albedoAniso, dimensions, dimensions, false, false, false);

            Color32[] pixels32 = texture2D.GetPixels32();
            if (!Application.get_isPlaying())
            {
                Object.DestroyImmediate((Object)texture2D);
            }
            else
            {
                Object.Destroy((Object)texture2D);
            }
            return(pixels32);
        }
        private Texture2D BakeNormal(Texture2D normalTexture)
        {
            int num = this.m_normalTextureSizePx * this.m_normalTextureSizePx;

            if (num == 0 || Object.op_Equality((Object)normalTexture, (Object)null))
            {
                return((Texture2D)null);
            }
            Texture2D texture2D1 = new Texture2D(this.m_normalTextureSizePx, this.m_normalTextureSizePx, this.m_normalFormat, true, true);

            ((Object)texture2D1).set_name("CTS_" + ((Object)this).get_name() + "_Normal");
            ((Texture)texture2D1).set_anisoLevel(this.m_normalAniso);
            ((Texture)texture2D1).set_filterMode(this.m_normalFilterMode);
            Color32[] pixels32_1 = texture2D1.GetPixels32();
            Texture2D texture2D2 = CTSProfile.ResizeTexture(normalTexture, this.m_normalFormat, this.m_normalAniso, this.m_normalTextureSizePx, this.m_normalTextureSizePx, false, true, false);

            Color32[] pixels32_2 = texture2D2.GetPixels32();
            for (int index = 0; index < num; ++index)
            {
                pixels32_1[index].r = (__Null)128;
                pixels32_1[index].g = pixels32_2[index].g;
                pixels32_1[index].b = (__Null)128;
                pixels32_1[index].a = pixels32_2[index].a;
            }
            if (Application.get_isPlaying())
            {
                Object.Destroy((Object)texture2D2);
            }
            else
            {
                Object.DestroyImmediate((Object)texture2D2);
            }
            texture2D1.SetPixels32(pixels32_1);
            texture2D1.Apply(true);
            if (this.m_normalCompress)
            {
                texture2D1.Compress(true);
                texture2D1.Apply(true);
            }
            return(texture2D1);
        }
        public void ConstructTerrainReplacementNormals()
        {
            if (Application.get_isPlaying())
            {
                return;
            }
            while (this.m_replacementTerrainNormals.Count > this.m_terrainTextures.Count)
            {
                this.m_replacementTerrainNormals.RemoveAt(this.m_replacementTerrainNormals.Count - 1);
            }
            while (this.m_replacementTerrainNormals.Count < this.m_terrainTextures.Count)
            {
                this.m_replacementTerrainNormals.Add((Texture2D)null);
            }
            string path1 = this.m_ctsDirectory + "Terrains/ReplacementTextures/";

            Directory.CreateDirectory(path1);
            for (int index = 0; index < this.m_terrainTextures.Count; ++index)
            {
                CTSTerrainTextureDetails terrainTexture = this.m_terrainTextures[index];
                if (Object.op_Inequality((Object)terrainTexture.Normal, (Object)null))
                {
                    string path2 = path1 + ((Object)terrainTexture.Normal).get_name() + "_nrm_cts.png";
                    if (!File.Exists(path2))
                    {
                        Texture2D texture2D = CTSProfile.ResizeTexture(terrainTexture.Normal, this.m_normalFormat, this.m_normalAniso, 64, 64, false, true, false);
                        ((Object)texture2D).set_name(((Object)terrainTexture.Normal).get_name() + "_nrm_cts");
                        this.m_replacementTerrainNormals[index] = texture2D;
                        byte[] png = ImageConversion.EncodeToPNG(this.m_replacementTerrainNormals[index]);
                        File.WriteAllBytes(path2, png);
                    }
                }
                else
                {
                    this.m_replacementTerrainNormals[index] = (Texture2D)null;
                }
            }
            CompleteTerrainShader.SetDirty((Object)this, false, true);
        }
        private Texture2D BakeAlbedo(
            Texture2D albedoTexture,
            Texture2D smoothnessTexture,
            Texture2D roughnessTexture)
        {
            int num = this.m_normalTextureSizePx * this.m_normalTextureSizePx;

            if (num == 0)
            {
                return((Texture2D)null);
            }
            Texture2D texture2D1 = new Texture2D(this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, this.m_albedoFormat, true, false);

            ((Object)texture2D1).set_name("CTS_" + ((Object)this).get_name() + "_ASm");
            ((Texture)texture2D1).set_anisoLevel(this.m_albedoAniso);
            ((Texture)texture2D1).set_filterMode(this.m_albedoFilterMode);
            Color32[] pixels32_1 = texture2D1.GetPixels32();
            if (Object.op_Inequality((Object)albedoTexture, (Object)null))
            {
                Texture2D texture2D2 = CTSProfile.ResizeTexture(albedoTexture, this.m_albedoFormat, this.m_albedoAniso, this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, false, false, false);
                Color32[] pixels32_2 = texture2D2.GetPixels32();
                for (int index = 0; index < num; ++index)
                {
                    pixels32_1[index].r = pixels32_2[index].r;
                    pixels32_1[index].g = pixels32_2[index].g;
                    pixels32_1[index].b = pixels32_2[index].b;
                }
                if (Application.get_isPlaying())
                {
                    Object.Destroy((Object)texture2D2);
                }
                else
                {
                    Object.DestroyImmediate((Object)texture2D2);
                }
            }
            if (Object.op_Inequality((Object)roughnessTexture, (Object)null) && Object.op_Equality((Object)smoothnessTexture, (Object)null))
            {
                Texture2D texture2D2 = CTSProfile.ResizeTexture(roughnessTexture, this.m_albedoFormat, this.m_albedoAniso, this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, false, false, false);
                Color32[] pixels32_2 = texture2D2.GetPixels32();
                for (int index = 0; index < num; ++index)
                {
                    pixels32_1[index].a = (__Null)(int)(byte)((int)byte.MaxValue - pixels32_2[index].g);
                }
                if (Application.get_isPlaying())
                {
                    Object.Destroy((Object)texture2D2);
                }
                else
                {
                    Object.DestroyImmediate((Object)texture2D2);
                }
            }
            if (Object.op_Inequality((Object)smoothnessTexture, (Object)null))
            {
                Texture2D texture2D2 = CTSProfile.ResizeTexture(smoothnessTexture, this.m_albedoFormat, this.m_albedoAniso, this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, false, false, false);
                Color32[] pixels32_2 = texture2D2.GetPixels32();
                for (int index = 0; index < num; ++index)
                {
                    pixels32_1[index].a = pixels32_2[index].g;
                }
                if (Application.get_isPlaying())
                {
                    Object.Destroy((Object)texture2D2);
                }
                else
                {
                    Object.DestroyImmediate((Object)texture2D2);
                }
            }
            texture2D1.SetPixels32(pixels32_1);
            texture2D1.Apply(true);
            if (this.m_albedoCompress)
            {
                texture2D1.Compress(true);
                texture2D1.Apply(true);
            }
            return(texture2D1);
        }
        public Texture2D BakeHAOTexture(
            string name,
            Texture2D hTexture,
            Texture2D aoTexture,
            out byte minHeight,
            out byte maxHeight)
        {
            minHeight = (byte)0;
            maxHeight = byte.MaxValue;
            int num = this.m_albedoTextureSizePx * this.m_albedoTextureSizePx;

            if (num == 0)
            {
                return((Texture2D)null);
            }
            Texture2D texture2D1 = new Texture2D(this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, this.m_albedoFormat, true, false);

            ((Object)texture2D1).set_name("CTS_" + name + "_HAO");
            ((Texture)texture2D1).set_anisoLevel(this.m_albedoAniso);
            ((Texture)texture2D1).set_filterMode(this.m_albedoFilterMode);
            Color32[] pixels32_1 = texture2D1.GetPixels32();
            if (Object.op_Inequality((Object)hTexture, (Object)null))
            {
                minHeight = byte.MaxValue;
                maxHeight = (byte)0;
                Texture2D texture2D2 = CTSProfile.ResizeTexture(hTexture, this.m_albedoFormat, this.m_albedoAniso, this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, false, false, false);
                Color32[] pixels32_2 = texture2D2.GetPixels32();
                for (int index = 0; index < num; ++index)
                {
                    byte g = (byte)pixels32_2[index].g;
                    if ((int)g < (int)minHeight)
                    {
                        minHeight = g;
                    }
                    if ((int)g > (int)maxHeight)
                    {
                        maxHeight = g;
                    }
                    pixels32_1[index].r = (__Null)(int)(pixels32_1[index].g = pixels32_1[index].b = (__Null)g);
                }
                if (Application.get_isPlaying())
                {
                    Object.Destroy((Object)texture2D2);
                }
                else
                {
                    Object.DestroyImmediate((Object)texture2D2);
                }
            }
            if (Object.op_Inequality((Object)aoTexture, (Object)null))
            {
                Texture2D texture2D2 = CTSProfile.ResizeTexture(aoTexture, this.m_albedoFormat, this.m_albedoAniso, this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, false, false, false);
                Color32[] pixels32_2 = texture2D2.GetPixels32();
                for (int index = 0; index < num; ++index)
                {
                    pixels32_1[index].a = pixels32_2[index].g;
                }
                if (Application.get_isPlaying())
                {
                    Object.Destroy((Object)texture2D2);
                }
                else
                {
                    Object.DestroyImmediate((Object)texture2D2);
                }
            }
            else
            {
                for (int index = 0; index < num; ++index)
                {
                    pixels32_1[index].a = (__Null)(int)byte.MaxValue;
                }
            }
            texture2D1.SetPixels32(pixels32_1);
            texture2D1.Apply(true);
            if (this.m_albedoCompress)
            {
                texture2D1.Compress(true);
                texture2D1.Apply(true);
            }
            return(texture2D1);
        }
        private void ConstructAlbedosTextureArray()
        {
            this.m_needsAlbedosArrayUpdate = false;
            List <Texture2D> sourceTextures = new List <Texture2D>();
            int  num1 = 0;
            byte minHeight;
            byte maxHeight;

            for (int index = 0; index < this.m_terrainTextures.Count; ++index)
            {
                CTSTerrainTextureDetails terrainTexture = this.m_terrainTextures[index];
                if (Object.op_Inequality((Object)terrainTexture.Albedo, (Object)null))
                {
                    Texture2D texture2D = !Object.op_Equality((Object)terrainTexture.Smoothness, (Object)null) || !Object.op_Equality((Object)terrainTexture.Roughness, (Object)null) ? this.BakeAlbedo(terrainTexture.Albedo, terrainTexture.Smoothness, terrainTexture.Roughness) : (!this.m_albedoCompress ? CTSProfile.ResizeTexture(terrainTexture.Albedo, this.m_albedoFormat, this.m_albedoAniso, this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, true, false, false) : CTSProfile.ResizeTexture(terrainTexture.Albedo, this.m_albedoFormat, this.m_albedoAniso, this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, true, false, true));
                    sourceTextures.Add(texture2D);
                    Color linear = ((Color) ref texture2D.GetPixels(texture2D.get_mipmapCount() - 1)[0]).get_linear();
                    terrainTexture.m_albedoAverage = new Vector4((float)linear.r, (float)linear.g, (float)linear.b, (float)linear.a);
                    terrainTexture.m_albedoIdx     = num1++;
                    if ((this.m_shaderType == CTSConstants.ShaderType.Advanced || this.m_shaderType == CTSConstants.ShaderType.Tesselation) && (Object.op_Inequality((Object)terrainTexture.Height, (Object)null) || Object.op_Inequality((Object)terrainTexture.AmbientOcclusion, (Object)null)))
                    {
                        sourceTextures.Add(this.BakeHAOTexture(terrainTexture.m_name, terrainTexture.Height, terrainTexture.AmbientOcclusion, out minHeight, out maxHeight));
                        if (Object.op_Inequality((Object)terrainTexture.Height, (Object)null))
                        {
                            terrainTexture.m_heightIdx = num1;
                            terrainTexture.m_heightMin = (float)minHeight / (float)byte.MaxValue;
                            terrainTexture.m_heightMax = (float)maxHeight / (float)byte.MaxValue;
                        }
                        else
                        {
                            terrainTexture.m_heightIdx = -1;
                        }
                        terrainTexture.m_aoIdx = !Object.op_Inequality((Object)terrainTexture.AmbientOcclusion, (Object)null) ? -1 : num1;
                        ++num1;
                    }
                    else
                    {
                        terrainTexture.m_aoIdx     = -1;
                        terrainTexture.m_heightIdx = -1;
                    }
                }
                else
                {
                    terrainTexture.m_albedoIdx = -1;
                }
                terrainTexture.m_albedoWasChanged = false;
            }
            if (Object.op_Inequality((Object)this.m_snowAlbedoTexture, (Object)null))
            {
                Texture2D texture2D = !this.m_albedoCompress ? CTSProfile.ResizeTexture(this.m_snowAlbedoTexture, this.m_albedoFormat, this.m_normalAniso, this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, true, false, false) : CTSProfile.ResizeTexture(this.m_snowAlbedoTexture, this.m_albedoFormat, this.m_normalAniso, this.m_albedoTextureSizePx, this.m_albedoTextureSizePx, true, false, true);
                Color     linear    = ((Color) ref texture2D.GetPixels(texture2D.get_mipmapCount() - 1)[0]).get_linear();
                this.m_snowAverage = new Vector4((float)linear.r, (float)linear.g, (float)linear.b, (float)linear.a);
                sourceTextures.Add(texture2D);
                this.m_snowAlbedoTextureIdx = num1++;
            }
            else
            {
                this.m_snowAlbedoTextureIdx = -1;
            }
            if (Object.op_Inequality((Object)this.m_snowHeightTexture, (Object)null) || Object.op_Inequality((Object)this.m_snowAOTexture, (Object)null))
            {
                sourceTextures.Add(this.BakeHAOTexture("CTS_SnowHAO", this.m_snowHeightTexture, this.m_snowAOTexture, out minHeight, out maxHeight));
                if (Object.op_Inequality((Object)this.m_snowHeightTexture, (Object)null))
                {
                    this.m_snowHeightTextureIdx  = num1;
                    this.m_snowHeightmapMinValue = (float)minHeight / (float)byte.MaxValue;
                    this.m_snowHeightmapMaxValue = (float)maxHeight / (float)byte.MaxValue;
                }
                else
                {
                    this.m_snowHeightTextureIdx  = -1;
                    this.m_snowHeightmapMinValue = 0.0f;
                    this.m_snowHeightmapMaxValue = 1f;
                }
                this.m_snowAOTextureIdx = !Object.op_Inequality((Object)this.m_snowAOTexture, (Object)null) ? -1 : num1;
                int num2 = num1 + 1;
            }
            else
            {
                this.m_snowAOTextureIdx     = -1;
                this.m_snowHeightTextureIdx = -1;
            }
            this.AlbedosTextureArray = this.GetTextureArray(sourceTextures, CTSConstants.TextureType.Albedo, this.m_albedoAniso);
        }