private static void ExportSplat() { int length = m_terrain.get_terrainData().get_splatPrototypes().Length; for (int i = 0; i < length; i++) { SplatPrototype prototype = m_terrain.get_terrainData().get_splatPrototypes()[i]; Texture2D textured = prototype.get_texture(); string assetPath = UnityEditor.AssetDatabase.GetAssetPath(textured.GetInstanceID()); UnityEditor.TextureImporter atPath = UnityEditor.AssetImporter.GetAtPath(assetPath) as UnityEditor.TextureImporter; atPath.isReadable = true; atPath.textureCompression = UnityEditor.TextureImporterCompression.Uncompressed; UnityEditor.AssetDatabase.ImportAsset(assetPath); FileStream output = File.Open(m_saveLocation + "/" + textured.get_name().ToLower() + ".jpg", System.IO.FileMode.Create); new BinaryWriter(output).Write(ImageConversion.EncodeToJPG(textured)); output.Close(); if (prototype.get_normalMap() != null) { textured = prototype.get_normalMap(); string path = UnityEditor.AssetDatabase.GetAssetPath(textured.GetInstanceID()); UnityEditor.TextureImporter importer2 = UnityEditor.AssetImporter.GetAtPath(path) as UnityEditor.TextureImporter; importer2.isReadable = true; importer2.textureCompression = UnityEditor.TextureImporterCompression.Uncompressed; UnityEditor.AssetDatabase.ImportAsset(path); FileStream stream2 = File.Open(m_saveLocation + "/" + textured.get_name().ToLower() + ".jpg", System.IO.FileMode.Create); new BinaryWriter(stream2).Write(ImageConversion.EncodeToJPG(textured)); stream2.Close(); } } }
public void UpdateSettingsFromTerrain(Terrain terrain, bool forceUpdate) { if (Object.op_Equality((Object)terrain, (Object)null) || Object.op_Equality((Object)terrain.get_terrainData(), (Object)null)) { return; } if (forceUpdate) { this.m_needsAlbedosArrayUpdate = true; this.m_needsNormalsArrayUpdate = true; } while (this.m_terrainTextures.Count > terrain.get_terrainData().get_splatPrototypes().Length) { this.m_terrainTextures.RemoveAt(this.m_terrainTextures.Count - 1); this.m_needsAlbedosArrayUpdate = true; this.m_needsNormalsArrayUpdate = true; } SplatPrototype[] splatPrototypes = terrain.get_terrainData().get_splatPrototypes(); for (int index = 0; index < splatPrototypes.Length; ++index) { SplatPrototype splatPrototype = splatPrototypes[index]; if (index < this.m_terrainTextures.Count) { CTSTerrainTextureDetails terrainTexture = this.m_terrainTextures[index]; terrainTexture.Albedo = splatPrototype.get_texture(); terrainTexture.m_albedoTilingClose = (float)terrain.get_terrainData().get_splatPrototypes()[index].get_tileSize().x; terrainTexture.Normal = splatPrototype.get_normalMap(); } else { this.m_terrainTextures.Add(new CTSTerrainTextureDetails() { m_textureIdx = index, Albedo = terrain.get_terrainData().get_splatPrototypes()[index].get_texture(), m_albedoTilingClose = (float)terrain.get_terrainData().get_splatPrototypes()[index].get_tileSize().x, Normal = terrain.get_terrainData().get_splatPrototypes()[index].get_normalMap() }); } } this.RegenerateArraysIfNecessary(); }
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); } }