void Start()
        {
            ///<summary>
            ///Get the container of the waterplanes and give warning if not found
            ///Assign waterplanes and static boundaries to the gameobject arrays respectively
            /// </summary>
            container = GameObject.Find("AQUAS Container");

            if (container == null)
            {
#if UNITY_EDITOR
                Debug.LogWarning("AQUAS couldn't find the object called 'AQUAS Container', that should hold all waterplane objects as children. Underwater effects cannot be rendered. Parent all waterplanes to an object called 'AQUAS Container' to enable underwater effects.");
#endif
                this.enabled = false;
                return;
            }

            waterObjects     = new GameObject[container.transform.childCount];
            staticBoundaries = new GameObject[container.transform.childCount];

            for (int i = 0; i < container.transform.childCount; i++)
            {
                waterObjects[i] = container.transform.GetChild(i).gameObject;

                if (waterObjects[i].transform.Find("Static Boundary") != null)
                {
                    staticBoundaries[i] = waterObjects[i].transform.Find("Static Boundary").gameObject;
                }
                else
                {
                    staticBoundaries[i] = dynamicBoundary;
                }
            }

            ///<summary>
            ///Instantiate objects for creating the final mask and the capturing of the background
            ///Add all required components to each mask object
            ///Set up the layer settings for the mask objects
            /// </summary>
            boundaryMask  = new GameObject("Boundary Mask");
            volumeMask    = new GameObject("Volume Mask");
            frontFaceMask = new GameObject("Front Face Mask");
            background    = new GameObject("Background");

            boundaryMask.hideFlags  = HideFlags.HideAndDontSave;
            volumeMask.hideFlags    = HideFlags.HideAndDontSave;
            frontFaceMask.hideFlags = HideFlags.HideAndDontSave;
            background.hideFlags    = HideFlags.HideAndDontSave;

            boundaryMask.transform.SetParent(transform);
            boundaryMask.AddComponent <Camera>().CopyFrom(GetComponent <Camera>());
            boundaryMask.GetComponent <Camera>().cullingMask &= ~1 << LayerMask.NameToLayer("Everything");
            boundaryMask.GetComponent <Camera>().cullingMask ^= 1 << LayerMask.NameToLayer("Water");
            boundaryMask.GetComponent <Camera>().depth        = GetComponent <Camera>().depth - 4;
            boundaryMask.AddComponent <AQUAS_BoundaryMask>();
            boundaryMask.GetComponent <AQUAS_BoundaryMask>().nextCam = volumeMask;
            boundaryMask.AddComponent <AQUAS_ReflectNot>();

            volumeMask.transform.SetParent(transform);
            volumeMask.AddComponent <Camera>().CopyFrom(GetComponent <Camera>());
            volumeMask.GetComponent <Camera>().cullingMask &= ~1 << LayerMask.NameToLayer("Everything");
            volumeMask.GetComponent <Camera>().cullingMask ^= 1 << LayerMask.NameToLayer("Water");
            volumeMask.GetComponent <Camera>().depth        = GetComponent <Camera>().depth - 3;
            volumeMask.AddComponent <AQUAS_VolumeMask>();
            volumeMask.GetComponent <AQUAS_VolumeMask>().nextCam = frontFaceMask;
            volumeMask.AddComponent <AQUAS_ReflectNot>();

            frontFaceMask.transform.SetParent(transform);
            frontFaceMask.AddComponent <Camera>().CopyFrom(GetComponent <Camera>());
            frontFaceMask.GetComponent <Camera>().cullingMask &= ~1 << LayerMask.NameToLayer("Everything");
            frontFaceMask.GetComponent <Camera>().cullingMask ^= 1 << LayerMask.NameToLayer("Water");
            frontFaceMask.GetComponent <Camera>().depth        = GetComponent <Camera>().depth - 2;
            frontFaceMask.AddComponent <AQUAS_FrontFaceMask>();
            frontFaceMask.GetComponent <AQUAS_FrontFaceMask>().nextCam = gameObject;
            frontFaceMask.AddComponent <AQUAS_ReflectNot>();

            background.transform.SetParent(transform);
            background.AddComponent <Camera>().CopyFrom(GetComponent <Camera>());
            background.GetComponent <Camera>().cullingMask  = cullingMask;
            background.GetComponent <Camera>().cullingMask &= ~(1 << LayerMask.NameToLayer("Water"));
            background.GetComponent <Camera>().depth        = GetComponent <Camera>().depth - 1;
            background.AddComponent <AQUAS_CaptureBackground>();
            background.AddComponent <AQUAS_ReflectNot>();

            switch (backFaceQuality)
            {
            case QUALITY.low:
                background.GetComponent <AQUAS_CaptureBackground>().quality = 3;
                break;

            case QUALITY.medium:
                background.GetComponent <AQUAS_CaptureBackground>().quality = 2;
                break;

            case QUALITY.high:
                background.GetComponent <AQUAS_CaptureBackground>().quality = 0;
                break;
            }


            ///<summary>
            ///Set up the materials
            /// </summary>
            fogMat         = new Material(Shader.Find("Hidden/AQUAS/Underwater/Fog"));
            blurMat        = new Material(Shader.Find("Hidden/AQUAS/Underwater/Blur"));
            dropletMaskMat = new Material(Shader.Find("Hidden/AQUAS/Underwater/Droplet Mask"));

            ///<summary>
            ///Get the screen ratio for the scale of the droplet textures
            /// </summary>
            float ratio = (float)Screen.width / (float)Screen.height;

            ///<summary>
            ///Preset some of the fog material's required texture properties
            /// </summary>
            fogMat.SetTexture("_DistortionLens", (Texture2D)Resources.Load("distortion_ellipse", typeof(Texture2D)));
            fogMat.SetTexture("_DropletNormals", (Texture2D)Resources.Load("wet_lens_normal", typeof(Texture2D)));
            fogMat.SetTextureScale("_DropletNormals", new Vector2(ratio, 1));
            fogMat.SetTexture("_DropletCutout", (Texture2D)Resources.Load("wet_lens_cutout", typeof(Texture2D)));
            fogMat.SetTextureScale("_DropletCutout", new Vector2(ratio, 1));

            ///<summary>
            ///Set up all required render textures
            /// </summary>
            mask = new RenderTexture(Screen.height, Screen.width, 8, RenderTextureFormat.ARGB32);

            buffer1       = new RenderTexture(Screen.width, Screen.height, 32, RenderTextureFormat.ARGB32);
            buffer2       = new RenderTexture(Screen.width, Screen.height, 32, RenderTextureFormat.ARGB32);
            fogBuffer     = new RenderTexture(Screen.width, Screen.height, 32, RenderTextureFormat.ARGB32);
            dropletBuffer = new RenderTexture(Screen.width, Screen.height, 32, RenderTextureFormat.ARGB32);
            dropletMask   = new RenderTexture(Screen.width, Screen.height, 32, RenderTextureFormat.ARGB32);

            ///<summary>
            ///Set up the underwater audio:
            ///- Instantiate audio object as empty gameobject
            ///- Assign clips from the resources if they're null
            ///- Add Aadio aource components to the audio object
            ///- Assign clips to audio sources
            ///- Set looing of splash sounds to false
            /// </summary>
            audioObject = new GameObject("Underwater Audio");
            audioObject.transform.parent = transform;
            audioObject.hideFlags        = HideFlags.HideAndDontSave;

            if (underwaterAmbientSound == null)
            {
                underwaterAmbientSound = (AudioClip)Resources.Load("underwater");
            }
            if (diveSplashSound == null)
            {
                diveSplashSound = (AudioClip)Resources.Load("dive-splash");
            }
            if (surfaceSplashSound == null)
            {
                surfaceSplashSound = (AudioClip)Resources.Load("surfacing-splash");
            }

            underwaterAmbient = audioObject.AddComponent <AudioSource>();
            diveSplash        = audioObject.AddComponent <AudioSource>();
            surfaceSplash     = audioObject.AddComponent <AudioSource>();

            underwaterAmbient.clip = underwaterAmbientSound;
            diveSplash.clip        = diveSplashSound;
            surfaceSplash.clip     = surfaceSplashSound;

            diveSplash.loop    = false;
            surfaceSplash.loop = false;

            ///<summary>
            ///Get prefab and behaviour component for the bubble spawning
            /// </summary>
            bubble          = (GameObject)Resources.Load("Bubble", typeof(GameObject));
            bubbleBehaviour = bubble.GetComponent <AQUAS_BubbleBehaviour>();

            if (spawnBubbles)
            {
                bubbleContainer           = new GameObject("Bubble Container");
                bubbleContainer.hideFlags = HideFlags.HideAndDontSave;
            }
        }
Пример #2
0
        void Start()
        {
            ///<summary>
            ///Cache default fog settings and restore whenever not underwater
            /// </summary>
            defaultFog        = RenderSettings.fog;
            defaultFogColor   = RenderSettings.fogColor;
            defaultFogMode    = RenderSettings.fogMode;
            defaultFogDensity = RenderSettings.fogDensity;

            ///<summary>
            ///Set up the materials
            /// </summary>
            underwaterMat = new Material(Shader.Find("Hidden/AQUAS/Underwater/Fog Simple"));
            blurMat       = new Material(Shader.Find("Hidden/AQUAS/Underwater/Blur Simple"));

            ///<summary>
            ///Get the screen ratio for the scale of the droplet textures
            /// </summary>
            float ratio = (float)Screen.width / (float)Screen.height;

            ///<summary>
            ///Preset some of the fog material's required texture properties
            /// </summary>
            underwaterMat.SetTexture("_DistortionLens", (Texture2D)Resources.Load("distortion_ellipse", typeof(Texture2D)));
            underwaterMat.SetTexture("_DropletNormals", (Texture2D)Resources.Load("wet_lens_normal", typeof(Texture2D)));
            underwaterMat.SetTextureScale("_DropletNormals", new Vector2(ratio, 1));
            underwaterMat.SetTexture("_DropletCutout", (Texture2D)Resources.Load("wet_lens_cutout", typeof(Texture2D)));
            underwaterMat.SetTextureScale("_DropletCutout", new Vector2(ratio, 1));

            ///<summary>
            ///Set up all required render textures
            /// </summary>
            underwaterBuffer = new RenderTexture(Screen.width, Screen.height, 32, RenderTextureFormat.ARGB32);
            buffer1          = new RenderTexture(Screen.width, Screen.height, 32, RenderTextureFormat.ARGB32);
            buffer2          = new RenderTexture(Screen.width, Screen.height, 32, RenderTextureFormat.ARGB32);

            ///<summary>
            ///Set up the underwater audio:
            ///- Instantiate audio object as empty gameobject
            ///- Assign clips from the resources if they're null
            ///- Add Aadio aource components to the audio object
            ///- Assign clips to audio sources
            ///- Set looing of splash sounds to false
            /// </summary>
            audioObject = new GameObject("Underwater Audio");
            audioObject.transform.parent = transform;
            audioObject.hideFlags        = HideFlags.HideAndDontSave;

            if (underwaterAmbientSound == null)
            {
                underwaterAmbientSound = (AudioClip)Resources.Load("underwater");
            }
            if (diveSplashSound == null)
            {
                diveSplashSound = (AudioClip)Resources.Load("dive-splash");
            }
            if (surfaceSplashSound == null)
            {
                surfaceSplashSound = (AudioClip)Resources.Load("surfacing-splash");
            }

            underwaterAmbient = audioObject.AddComponent <AudioSource>();
            diveSplash        = audioObject.AddComponent <AudioSource>();
            surfaceSplash     = audioObject.AddComponent <AudioSource>();

            underwaterAmbient.clip = underwaterAmbientSound;
            diveSplash.clip        = diveSplashSound;
            surfaceSplash.clip     = surfaceSplashSound;

            diveSplash.loop    = false;
            surfaceSplash.loop = false;

            ///<summary>
            ///Get prefab and behaviour component for the bubble spawning
            /// </summary>
            bubble          = (GameObject)Resources.Load("Bubble", typeof(GameObject));
            bubbleBehaviour = bubble.GetComponent <AQUAS_BubbleBehaviour>();

            if (spawnBubbles)
            {
                bubbleContainer           = new GameObject("Bubble Container");
                bubbleContainer.hideFlags = HideFlags.HideAndDontSave;
            }
        }