Beispiel #1
0
        private void GetTexturesAndSettingsAtCurrentLocation()
        {
            this.m_textureList.Clear();
            CTSProfile ctsProfile = (CTSProfile)null;

            SplatPrototype[] splatPrototypeArray = new SplatPrototype[0];
            Vector3          position            = ((Component)this).get_transform().get_position();
            Vector3          localScale          = ((Component)this).get_transform().get_localScale();
            Vector3          eulerAngles         = ((Component)this).get_transform().get_eulerAngles();

            for (int index1 = this.m_filters.Length - 1; index1 >= 0; --index1)
            {
                Mesh sharedMesh = this.m_filters[index1].get_sharedMesh();
                if (Object.op_Inequality((Object)sharedMesh, (Object)null))
                {
                    Vector3[] vertices = sharedMesh.get_vertices();
                    for (int index2 = vertices.Length - 1; index2 >= 0; --index2)
                    {
                        Vector3 locationWU = Vector3.op_Addition(position, Quaternion.op_Multiply(Quaternion.Euler(eulerAngles), Vector3.Scale(vertices[index2], localScale)));
                        Terrain terrain    = this.GetTerrain(locationWU);
                        if (Object.op_Inequality((Object)terrain, (Object)null))
                        {
                            if (Object.op_Equality((Object)ctsProfile, (Object)null))
                            {
                                CompleteTerrainShader component = (CompleteTerrainShader)((Component)terrain).get_gameObject().GetComponent <CompleteTerrainShader>();
                                if (Object.op_Inequality((Object)component, (Object)null))
                                {
                                    ctsProfile = component.Profile;
                                }
                            }
                            if (splatPrototypeArray.Length == 0)
                            {
                                splatPrototypeArray = terrain.get_terrainData().get_splatPrototypes();
                            }
                            Vector3 localPosition = this.GetLocalPosition(terrain, locationWU);
                            float[,,] texturesAtLocation = this.GetTexturesAtLocation(terrain, localPosition);
                            for (int index3 = 0; index3 < texturesAtLocation.GetLength(2); ++index3)
                            {
                                if (index3 == this.m_textureList.Count)
                                {
                                    this.m_textureList.Add(new CTSMeshBlender.TextureData()
                                    {
                                        m_terrainIdx             = index3,
                                        m_terrainTextureStrength = texturesAtLocation[0, 0, index3]
                                    });
                                }
                                else
                                {
                                    this.m_textureList[index3].m_terrainTextureStrength += texturesAtLocation[0, 0, index3];
                                }
                            }
                        }
                    }
                }
            }
            List <CTSMeshBlender.TextureData> list = this.m_textureList.OrderByDescending <CTSMeshBlender.TextureData, float>((Func <CTSMeshBlender.TextureData, float>)(x => x.m_terrainTextureStrength)).ToList <CTSMeshBlender.TextureData>();

            while (list.Count > 3)
            {
                list.RemoveAt(list.Count - 1);
            }
            this.m_textureList = list.OrderBy <CTSMeshBlender.TextureData, int>((Func <CTSMeshBlender.TextureData, int>)(x => x.m_terrainIdx)).ToList <CTSMeshBlender.TextureData>();
            if (Object.op_Inequality((Object)ctsProfile, (Object)null))
            {
                this.m_geoMap            = ctsProfile.GeoAlbedo;
                this.m_geoMapClosePower  = ctsProfile.m_geoMapClosePower;
                this.m_geoMapOffsetClose = ctsProfile.m_geoMapCloseOffset;
                this.m_geoTilingClose    = ctsProfile.m_geoMapTilingClose;
                this.m_smoothness        = ctsProfile.m_globalTerrainSmoothness;
                this.m_specular          = ctsProfile.m_globalTerrainSpecular;
                switch (ctsProfile.m_globalAOType)
                {
                case CTSConstants.AOType.None:
                    this.m_useAO        = false;
                    this.m_useAOTexture = false;
                    break;

                case CTSConstants.AOType.NormalMapBased:
                    this.m_useAO        = true;
                    this.m_useAOTexture = false;
                    break;

                case CTSConstants.AOType.TextureBased:
                    this.m_useAO        = true;
                    this.m_useAOTexture = true;
                    break;
                }
            }
            else
            {
                this.m_geoMap            = (Texture2D)null;
                this.m_geoMapClosePower  = 0.0f;
                this.m_geoMapOffsetClose = 0.0f;
                this.m_geoTilingClose    = 0.0f;
                this.m_smoothness        = 1f;
                this.m_specular          = 1f;
                this.m_useAO             = true;
                this.m_useAOTexture      = false;
            }
            byte minHeight = 0;
            byte maxHeight = 0;

            for (int index = 0; index < this.m_textureList.Count; ++index)
            {
                CTSMeshBlender.TextureData texture = this.m_textureList[index];
                if (Object.op_Inequality((Object)ctsProfile, (Object)null) && texture.m_terrainIdx < ctsProfile.TerrainTextures.Count)
                {
                    CTSTerrainTextureDetails terrainTexture = ctsProfile.TerrainTextures[texture.m_terrainIdx];
                    texture.m_albedo           = terrainTexture.Albedo;
                    texture.m_normal           = terrainTexture.Normal;
                    texture.m_hao_in_GA        = ctsProfile.BakeHAOTexture(((Object)terrainTexture.Albedo).get_name(), terrainTexture.Height, terrainTexture.AmbientOcclusion, out minHeight, out maxHeight);
                    texture.m_aoPower          = terrainTexture.m_aoPower;
                    texture.m_color            = new Vector4((float)terrainTexture.m_tint.r * terrainTexture.m_tintBrightness, (float)terrainTexture.m_tint.g * terrainTexture.m_tintBrightness, (float)terrainTexture.m_tint.b * terrainTexture.m_tintBrightness, terrainTexture.m_smoothness);
                    texture.m_geoPower         = terrainTexture.m_geologicalPower;
                    texture.m_normalPower      = terrainTexture.m_normalStrength;
                    texture.m_tiling           = terrainTexture.m_albedoTilingClose;
                    texture.m_heightContrast   = terrainTexture.m_heightContrast;
                    texture.m_heightDepth      = terrainTexture.m_heightDepth;
                    texture.m_heightBlendClose = terrainTexture.m_heightBlendClose;
                }
                else if (texture.m_terrainIdx < splatPrototypeArray.Length)
                {
                    SplatPrototype splatPrototype = splatPrototypeArray[texture.m_terrainIdx];
                    texture.m_albedo           = splatPrototype.get_texture();
                    texture.m_normal           = splatPrototype.get_normalMap();
                    texture.m_hao_in_GA        = (Texture2D)null;
                    texture.m_aoPower          = 0.0f;
                    texture.m_color            = Vector4.get_one();
                    texture.m_geoPower         = 0.0f;
                    texture.m_normalPower      = 1f;
                    texture.m_tiling           = (float)splatPrototype.get_tileSize().x;
                    texture.m_heightContrast   = 1f;
                    texture.m_heightDepth      = 1f;
                    texture.m_heightBlendClose = 1f;
                }
            }
        }
    private static void ExportTerrain()
    {
        if (!Directory.Exists(m_saveLocation))
        {
            Directory.CreateDirectory(m_saveLocation);
        }
        JSONObject node = new JSONObject(JSONObject.Type.OBJECT);

        node.AddField("version", "1.0");
        node.AddField("cameraCoordinateInverse", cameraCoordinateInverse);
        float val = m_terrain.get_terrainData().get_size().x / ((float)(m_terrain.get_terrainData().get_heightmapWidth() - 1));

        node.AddField("gridSize", val);
        if (((m_terrain.get_terrainData().get_heightmapWidth() - 1) % CHUNK_GRID_NUM) != 0)
        {
            Debug.LogError("高度图的宽减去一必须是" + CHUNK_GRID_NUM + "的倍数");
        }
        else if (((m_terrain.get_terrainData().get_heightmapHeight() - 1) % CHUNK_GRID_NUM) != 0)
        {
            Debug.LogError("高度图的高减去一必须是" + CHUNK_GRID_NUM + "的倍数");
        }
        else
        {
            int num2 = (m_terrain.get_terrainData().get_heightmapWidth() - 1) / CHUNK_GRID_NUM;
            int num3 = (m_terrain.get_terrainData().get_heightmapHeight() - 1) / CHUNK_GRID_NUM;
            node.AddField("chunkNumX", num2);
            node.AddField("chunkNumZ", num3);
            JSONObject obj3 = new JSONObject(JSONObject.Type.OBJECT);
            obj3.AddField("numX", m_terrain.get_terrainData().get_heightmapWidth());
            obj3.AddField("numZ", m_terrain.get_terrainData().get_heightmapHeight());
            obj3.AddField("bitType", 0x10);
            obj3.AddField("value", m_terrain.get_terrainData().get_size().y);
            obj3.AddField("url", m_terrain.get_name().ToLower() + "_heightmap.thdata");
            node.AddField("heightData", obj3);
            JSONObject obj4 = new JSONObject(JSONObject.Type.OBJECT);
            node.AddField("material", obj4);
            JSONObject obj5 = new JSONObject(JSONObject.Type.ARRAY);
            obj5.Add((float)0f);
            obj5.Add((float)0f);
            obj5.Add((float)0f);
            obj4.AddField("ambient", obj5);
            JSONObject obj6 = new JSONObject(JSONObject.Type.ARRAY);
            obj6.Add((float)1f);
            obj6.Add((float)1f);
            obj6.Add((float)1f);
            obj4.AddField("diffuse", obj6);
            JSONObject obj7 = new JSONObject(JSONObject.Type.ARRAY);
            obj7.Add((float)0.2f);
            obj7.Add((float)0.2f);
            obj7.Add((float)0.2f);
            obj7.Add((float)32f);
            obj4.AddField("specular", obj7);
            JSONObject obj8 = new JSONObject(JSONObject.Type.ARRAY);
            node.AddField("detailTexture", obj8);
            int length = m_terrain.get_terrainData().get_splatPrototypes().Length;
            for (int i = 0; i < length; i++)
            {
                JSONObject     obj11     = new JSONObject(JSONObject.Type.OBJECT);
                SplatPrototype prototype = m_terrain.get_terrainData().get_splatPrototypes()[i];
                obj11.AddField("diffuse", prototype.get_texture().get_name().ToLower() + ".jpg");
                if (prototype.get_normalMap() != null)
                {
                    obj11.AddField("normal", prototype.get_normalMap().get_name().ToLower() + ".jpg");
                }
                JSONObject obj12 = new JSONObject(JSONObject.Type.ARRAY);
                obj12.Add((float)(prototype.get_tileSize().x / val));
                obj12.Add((float)(prototype.get_tileSize().y / val));
                obj11.AddField("scale", obj12);
                JSONObject obj13 = new JSONObject(JSONObject.Type.ARRAY);
                obj13.Add(prototype.get_tileOffset().x);
                obj13.Add(prototype.get_tileOffset().y);
                obj11.AddField("offset", obj13);
                obj8.Add(obj11);
            }
            m_chunkInfoNode = new JSONObject(JSONObject.Type.ARRAY);
            node.AddField("chunkInfo", m_chunkInfoNode);
            float[,] heightsData = GetHeightsData();
            Texture2D  textured = ExportNormal(calcNormalOfTriangle(heightsData, (((num2 * LEAF_GRID_NUM) * num3) * LEAF_GRID_NUM) * 2, val));
            JSONObject obj9     = new JSONObject(JSONObject.Type.ARRAY);
            obj9.Add(textured.get_name().ToLower() + ".png");
            node.AddField("normalMap", obj9);
            int           num5   = 0;
            int           num6   = 0;
            int           index  = 0;
            int           num8   = 0;
            int           num9   = 0;
            int           width  = 0;
            int           height = 0;
            Color[]       color  = null;
            TextureFormat format = 0;
            for (num5 = 0; num5 < num3; num5++)
            {
                for (num6 = 0; num6 < num2; num6++)
                {
                    length              = m_terrain.get_terrainData().get_alphamapTextures().Length;
                    m_chunkNode         = new JSONObject(JSONObject.Type.OBJECT);
                    m_alphamapArrayNode = new JSONObject(JSONObject.Type.ARRAY);
                    m_detailIDArrayNode = new JSONObject(JSONObject.Type.ARRAY);
                    m_splatIndex        = 0;
                    for (index = 0; index < length; index++)
                    {
                        Texture2D textured2 = m_terrain.get_terrainData().get_alphamapTextures()[index];
                        if ((textured2.get_width() % num2) != 0)
                        {
                            Debug.LogError("Control Texture(alpha map) 的宽必须是" + num2 + "的倍数");
                            return;
                        }
                        if ((textured2.get_height() % num3) != 0)
                        {
                            Debug.LogError("Control Texture(alpha map) 的高必须是" + num3 + "的倍数");
                            return;
                        }
                        width  = textured2.get_width() / num2;
                        height = textured2.get_height() / num3;
                        num8   = num6 * width;
                        num9   = textured2.get_height() - ((num5 + 1) * height);
                        color  = textured2.GetPixels(num8, num9, width, height);
                        format = textured2.get_format();
                        MergeAlphaMap(string.Concat(new object[] { m_terrain.get_name().ToLower(), "_splatalpha{0}_", num6, "_", num5, m_debug ? ".jpg" : ".png" }), format, width, height, color);
                    }
                    AfterMergeAlphaMap(string.Concat(new object[] { m_terrain.get_name().ToLower(), "_splatalpha{0}_", num6, "_", num5, m_debug ? ".jpg" : ".png" }), format, width, height);
                    m_chunkNode.AddField("normalMap", 0);
                }
                ExportSplat();
                ExportHeightmap16(heightsData);
                ExportAlphamap();
            }
            JSONObject obj10 = new JSONObject(JSONObject.Type.ARRAY);
            for (int j = 0; j < m_alphamapDataList.Count; j++)
            {
                KeyValuePair <string, Color[]> pair = m_alphamapDataList[j];
                obj10.Add(pair.Key.ToLower());
            }
            node.AddField("alphaMap", obj10);
            saveData(node);
        }
    }