using System.Collections.Generic; using UnityEngine; namespace CTS { public class CTSTerrainManager : CTSSingleton { private HashSet m_shaderSet = new HashSet(); private HashSet m_controllerSet = new HashSet(); protected CTSTerrainManager() { } public void RegisterShader(CompleteTerrainShader shader) { m_shaderSet.Add(shader); } public void UnregisterShader(CompleteTerrainShader shader) { m_shaderSet.Remove(shader); } public void RegisterWeatherController(CTSWeatherController weatherController) { m_controllerSet.Add(weatherController); } public void UnregisterWeatherController(CTSWeatherController weatherController) { m_controllerSet.Remove(weatherController); } public void AddCTSToAllTerrains() { Terrain[] activeTerrains = Terrain.activeTerrains; foreach (Terrain terrain in activeTerrains) { if (terrain.gameObject.GetComponent() == null) { terrain.gameObject.AddComponent(); CompleteTerrainShader.SetDirty(terrain, recordUndo: false, isPlayingAllowed: false); } } } public void AddCTSToTerrain(Terrain terrain) { if (!(terrain == null) && terrain.gameObject.GetComponent() == null) { terrain.gameObject.AddComponent(); CompleteTerrainShader.SetDirty(terrain, recordUndo: false, isPlayingAllowed: false); } } public bool ProfileIsActive(CTSProfile profile) { if (profile == null) { return false; } foreach (CompleteTerrainShader item in m_shaderSet) { if (item.Profile != null && item.Profile.GetInstanceID() == profile.GetInstanceID()) { return true; } } return false; } public void BroadcastProfileSelect(CTSProfile profile) { if (profile == null) { return; } profile.terrainLayerAssetRebuild = true; foreach (CompleteTerrainShader item in m_shaderSet) { item.Profile = profile; } profile.m_currentRenderPipelineType = CompleteTerrainShader.GetRenderPipeline(); } public void BroadcastProfileSelect(CTSProfile profile, Terrain terrain) { if (!(profile == null) && !(terrain == null)) { profile.terrainLayerAssetRebuild = true; CompleteTerrainShader completeTerrainShader = terrain.gameObject.GetComponent(); if (completeTerrainShader == null) { completeTerrainShader = terrain.gameObject.AddComponent(); } completeTerrainShader.IsProfileConnected = true; completeTerrainShader.Profile = profile; } } public void BroadcastProfileUpdate(CTSProfile profile) { if (profile == null) { Debug.LogWarning("Cannot update shader on null profile."); return; } profile.terrainLayerAssetRebuild = true; foreach (CompleteTerrainShader item in m_shaderSet) { if (item.Profile != null && item.Profile.name == profile.name) { item.UpdateShader(); } } profile.m_currentRenderPipelineType = CompleteTerrainShader.GetRenderPipeline(); } public void BroadcastShaderSetup(CTSProfile profile) { profile.terrainLayerAssetRebuild = true; if (Terrain.activeTerrain != null) { CompleteTerrainShader component = Terrain.activeTerrain.GetComponent(); if (component != null && component.Profile != null && component.Profile.name == profile.name) { component.UpdateProfileFromTerrainForced(); BroadcastProfileUpdate(profile); return; } } foreach (CompleteTerrainShader item in m_shaderSet) { if (item.Profile != null) { if (profile == null) { item.UpdateProfileFromTerrainForced(); } else if (item.Profile.name == profile.name) { item.UpdateProfileFromTerrainForced(); BroadcastProfileUpdate(profile); break; } } } } public void BroadcastBakeTerrains() { foreach (CompleteTerrainShader item in m_shaderSet) { if (item.AutoBakeNormalMap) { item.BakeTerrainNormals(); } if (item.AutoBakeColorMap) { if (!item.AutoBakeGrassIntoColorMap) { item.BakeTerrainBaseMap(); } else { item.BakeTerrainBaseMapWithGrass(); } } } } public void BroadcastAlbedoTextureSwitch(CTSProfile profile, Texture2D texture, int textureIdx, float tiling) { profile.terrainLayerAssetRebuild = true; foreach (CompleteTerrainShader item in m_shaderSet) { if (item.Profile != null) { if (profile == null) { item.ReplaceAlbedoInTerrain(texture, textureIdx, tiling); } else if (item.Profile.name == profile.name) { item.ReplaceAlbedoInTerrain(texture, textureIdx, tiling); } } } } public void BroadcastNormalTextureSwitch(CTSProfile profile, Texture2D texture, int textureIdx, float tiling) { profile.terrainLayerAssetRebuild = true; foreach (CompleteTerrainShader item in m_shaderSet) { if (item.Profile != null) { if (profile == null) { item.ReplaceNormalInTerrain(texture, textureIdx, tiling); } else if (item.Profile.name == profile.name) { item.ReplaceNormalInTerrain(texture, textureIdx, tiling); } } } } public void BroadcastWeatherUpdate(CTSWeatherManager manager) { foreach (CTSWeatherController item in m_controllerSet) { item.ProcessWeatherUpdate(manager); } } public void RemoveWorldSeams() { if (m_shaderSet.Count <= 0) { return; } using HashSet.Enumerator enumerator = m_shaderSet.GetEnumerator(); if (enumerator.MoveNext()) { enumerator.Current.RemoveWorldSeams(); } } } }