//Removes new gaia underwater FX from your scene public static void GX_Water_RemoveUnderwaterFx() { if (!Application.isPlaying) { GameObject underwaterFX = GameObject.Find("Underwater PostFX"); if (underwaterFX != null) { DestroyImmediate(underwaterFX); } GaiaUnderWaterEffects underwaterFXScript = FindObjectOfType <GaiaUnderWaterEffects>(); if (underwaterFXScript != null) { DestroyImmediate(underwaterFXScript); } GameObject underwaterAudioFX = GameObject.Find("Underwater SoundFX"); if (underwaterAudioFX != null) { DestroyImmediate(underwaterAudioFX); } GameObject underwaterTransactionPostFX = GameObject.Find("Underwater Transition PostFX"); if (underwaterTransactionPostFX != null) { DestroyImmediate(underwaterTransactionPostFX); } GaiaReflectionProbeUpdate reflectionProbeUpdater = FindObjectOfType <GaiaReflectionProbeUpdate>(); if (reflectionProbeUpdater != null) { DestroyImmediate(reflectionProbeUpdater); } } }
//Adds new gaia water to your scene public static void GX_Water_AddWater() { if (Application.isPlaying) { Debug.LogWarning("Can only add water when application is not playing!"); return; } //Get relevant information GaiaSceneInfo sceneInfo = GaiaSceneInfo.GetSceneInfo(); GameObject parentObject = GetOrCreateEnvironmentParent(); Terrain activeTerrain = GetActiveTerrain(); Material waterMaterial = GetWaterMaterial("Ambient Water Sample Material"); //Get main directional light and make sure its set up properly var lightObject = GetOrCreateDirectionalLight(); var effectsSettings = lightObject.GetComponent <GaiaUnderWaterEffects>(); if (lightObject.GetComponent <GaiaUnderWaterEffects>() == null) { effectsSettings = lightObject.AddComponent <GaiaUnderWaterEffects>(); } effectsSettings.m_causticsSize = 5; effectsSettings.m_followPlayer = false; effectsSettings.m_framesPerSecond = 25f; effectsSettings.m_sealevel = sceneInfo.m_seaLevel; effectsSettings.m_underWaterFogColor = new Color32(76, 112, 142, 255); effectsSettings.m_underWaterFogDistance = 65f; effectsSettings.LoadCaustics(); #if UNITY_EDITOR effectsSettings.player = effectsSettings.GetThePlayer(); #endif #if UNITY_POST_PROCESSING_STACK_V2 var underwaterPostFxObject = GameObject.Find("Underwater PostFX"); if (underwaterPostFxObject == null) { underwaterPostFxObject = new GameObject("Underwater PostFX"); underwaterPostFxObject.transform.position = new Vector3(0f, sceneInfo.m_seaLevel - 506.5f, 0f); underwaterPostFxObject.transform.parent = parentObject.transform; underwaterPostFxObject.layer = LayerMask.NameToLayer("TransparentFX"); //Add the pp volume var ppVol = underwaterPostFxObject.AddComponent <PostProcessVolume>(); ppVol.sharedProfile = AssetDatabase.LoadAssetAtPath <PostProcessProfile>(GetAssetPath("Ambient Sample Underwater Post Processing")); ppVol.blendDistance = 4f; ppVol.priority = 1; var colliderSettings = underwaterPostFxObject.AddComponent <BoxCollider>(); if (activeTerrain != null) { colliderSettings.size = new Vector3(activeTerrain.terrainData.size.x * 1.5f, 1000f, activeTerrain.terrainData.size.z * 1.5f); } else { colliderSettings.size = new Vector3(2560f, 1000f, 2560f); } colliderSettings.isTrigger = true; } var underwaterTransitionFXObject = GameObject.Find("Underwater Transition PostFX"); if (underwaterTransitionFXObject == null) { underwaterTransitionFXObject = new GameObject("Underwater Transition PostFX"); underwaterTransitionFXObject.transform.position = new Vector3(0f, sceneInfo.m_seaLevel, 0f); underwaterTransitionFXObject.transform.parent = parentObject.transform; underwaterTransitionFXObject.layer = LayerMask.NameToLayer("TransparentFX"); var ppVol = underwaterTransitionFXObject.AddComponent <PostProcessVolume>(); ppVol.sharedProfile = AssetDatabase.LoadAssetAtPath <PostProcessProfile>(GetAssetPath("Ambient Sample Underwater Transaction Post Processing")); ppVol.blendDistance = 0.15f; ppVol.priority = 2; var colliderSettings = underwaterTransitionFXObject.AddComponent <BoxCollider>(); if (activeTerrain != null) { colliderSettings.size = new Vector3(activeTerrain.terrainData.size.x * 1.5f, 0.1f, activeTerrain.terrainData.size.z * 1.5f); } else { colliderSettings.size = new Vector3(2560f, 0.1f, 2560f); } colliderSettings.isTrigger = true; } #endif var underwaterAudioFXObject = GameObject.Find("Underwater SoundFX"); Terrain terrain = GetActiveTerrain(); if (underwaterAudioFXObject == null) { underwaterAudioFXObject = new GameObject("Underwater SoundFX"); underwaterAudioFXObject.transform.parent = parentObject.transform; var audio = underwaterAudioFXObject.AddComponent <AudioSource>(); audio.clip = AssetDatabase.LoadAssetAtPath <AudioClip>(GetAssetPath("Gaia Ambient Underwater Sound Effect")); audio.volume = 0f; audio.loop = true; if (terrain != null) { audio.maxDistance = terrain.terrainData.size.x * 1.5f; } else { audio.maxDistance = 3000f; } } //Grab or create the water GameObject theWaterObject = GameObject.Find("Ambient Water Sample"); if (theWaterObject == null) { theWaterObject = Instantiate(GetAssetPrefab("Ambient Water Sample")); theWaterObject.name = "Ambient Water Sample"; theWaterObject.transform.parent = parentObject.transform; } //And update it Vector3 waterPosition = sceneInfo.m_centrePointOnTerrain; waterPosition.y = sceneInfo.m_seaLevel; theWaterObject.transform.position = waterPosition; if (activeTerrain != null) { theWaterObject.transform.localScale = new Vector3(sceneInfo.m_sceneBounds.size.x, 1f, sceneInfo.m_sceneBounds.size.z); } else { theWaterObject.transform.localScale = new Vector3(256f, 1f, 256f); } //Update water material if (waterMaterial != null) { if (activeTerrain != null) { waterMaterial.SetFloat("_GlobalTiling", sceneInfo.m_sceneBounds.size.x); } else { waterMaterial.SetFloat("_GlobalTiling", 128f); } } //Adds reflection probe updater script if missing GetOrCreateReflectionProbe("Camera Reflection Probe"); GameObject theReflectionProbeObject = GameObject.Find("Camera Reflection Probe"); if (theReflectionProbeObject != null) { if (theReflectionProbeObject.GetComponent <GaiaReflectionProbeUpdate>() == null) { GaiaReflectionProbeUpdate probeObject = theReflectionProbeObject.AddComponent <GaiaReflectionProbeUpdate>(); probeObject.m_followCamera = true; probeObject.SetProbeSettings(); } } }