/// <summary> /// Render the reflections for this objects position /// and the current camera. /// </summary> public void RenderReflection(GameObject go) { try { if (!enabled) { return; } Camera cam = Camera.current; if (cam == null) { return; } CameraData data = m_ocean.FindCameraData(cam); //Create the data needed if not already created. if (data.reflection == null) { data.reflection = new ReflectionData(); } if (data.reflection.IsViewUpdated(cam)) { return; } //If this camera has disable the reflection turn it off in the shader and return. if (GetDisableReflections(data.settings)) { Shader.DisableKeyword("CETO_REFLECTION_ON"); data.reflection.SetViewAsUpdated(cam); return; } else { Shader.EnableKeyword("CETO_REFLECTION_ON"); } RenderTexture reflections0 = null; RenderTexture reflections1 = null; if (data.reflection.cam != null) { DisableFog disableFog = data.reflection.cam.GetComponent <DisableFog>(); if (disableFog != null) { disableFog.enabled = !fogInReflection; } } if (RenderReflectionCustom != null) { //If using a custom method //Destroy the camera if already created as its no longer needed. if (data.reflection.cam != null) { data.reflection.DestroyTargets(); } CreateRenderTarget(data.reflection, cam.pixelWidth, cam.pixelHeight, cam.allowHDR, false, data.settings); //Create the dummy object if null if (m_dummy == null) { m_dummy = new GameObject("Ceto Reflection Dummy Gameobject"); m_dummy.hideFlags = HideFlags.HideAndDontSave; } //Set the position of the reflection plane. m_dummy.transform.position = new Vector3(0.0f, m_ocean.level, 0.0f); //Copy returned texture in target. Graphics.Blit(RenderReflectionCustom(m_dummy), data.reflection.target0); reflections0 = data.reflection.target0; reflections1 = null; //Custom stero not supported. } else { //Else use normal method. CreateReflectionCameraFor(cam, data.reflection); CreateRenderTarget(data.reflection, cam.pixelWidth, cam.pixelHeight, cam.allowHDR, cam.stereoEnabled, data.settings); if (cam.stereoEnabled) { RenderSteroReflection(data.reflection, cam, data.settings); reflections0 = data.reflection.target0; reflections1 = data.reflection.target1; } else { Shader.DisableKeyword("CETO_STERO_CAMERA"); RenderReflection(data.reflection.cam, data.reflection.target0, cam.transform.position, cam.transform.rotation, cam.projectionMatrix, data.settings); reflections0 = data.reflection.target0; reflections1 = null; } } //The reflections texture should now contain the rendered //reflections for the current cameras view. if (reflections0 != null) { m_imageBlur.BlurIterations = blurIterations; m_imageBlur.BlurMode = blurMode; m_imageBlur.BlurSpread = blurSpread; m_imageBlur.Blur(reflections0); Shader.SetGlobalTexture(Ocean.REFLECTION_TEXTURE_NAME0, reflections0); } if (reflections1 != null) { m_imageBlur.BlurIterations = blurIterations; m_imageBlur.BlurMode = blurMode; m_imageBlur.BlurSpread = blurSpread; m_imageBlur.Blur(reflections1); Shader.SetGlobalTexture(Ocean.REFLECTION_TEXTURE_NAME1, reflections1); } data.reflection.SetViewAsUpdated(cam); } catch (Exception e) { Ocean.LogError(e.ToString()); WasError = true; enabled = false; } }
/// <summary> /// Render the reflections for this objects position /// and the current camera. /// </summary> public override void RenderReflection(GameObject go) { try { if (!enabled) { return; } Camera cam = Camera.current; if (cam == null) { return; } CameraData data = m_ocean.FindCameraData(cam); //Create the data needed if not already created. if (data.reflection == null) { data.reflection = new ReflectionData(); } if (data.reflection.updated) { return; } //If this camera has disable the reflection turn it off in the shader and return. if (GetDisableReflections(data.settings)) { Shader.DisableKeyword("CETO_REFLECTION_ON"); data.reflection.updated = true; return; } else { Shader.EnableKeyword("CETO_REFLECTION_ON"); } RenderTexture reflections = null; if (data.reflection.cam != null) { DisableFog disableFog = data.reflection.cam.GetComponent <DisableFog>(); if (disableFog != null) { disableFog.enabled = !fogInReflection; } } if (RenderReflectionCustom != null) { //If using a custom method //Destroy the camera if already created as its no longer needed. if (data.reflection.cam != null) { RTUtility.ReleaseAndDestroy(data.reflection.cam.targetTexture); data.reflection.cam.targetTexture = null; Destroy(data.reflection.cam.gameObject); Destroy(data.reflection.cam); data.reflection.cam = null; } CreateRenderTarget(data.reflection, cam.name, cam.pixelWidth, cam.pixelHeight, cam.allowHDR, data.settings); //Create the dummy object if null if (m_dummy == null) { m_dummy = new GameObject("Ceto Reflection Dummy Gameobject"); m_dummy.hideFlags = HideFlags.HideAndDontSave; } //Set the position of the reflection plane. m_dummy.transform.position = new Vector3(0.0f, m_ocean.level, 0.0f); reflections = RenderReflectionCustom(m_dummy); } else { //Else use normal method. CreateReflectionCameraFor(cam, data.reflection); CreateRenderTarget(data.reflection, cam.name, cam.pixelWidth, cam.pixelHeight, cam.allowHDR, data.settings); NotifyOnEvent.Disable = true; RenderReflectionFor(cam, data.reflection.cam, data.settings); NotifyOnEvent.Disable = false; reflections = data.reflection.cam.targetTexture; } //The reflections texture should now contain the rendered //reflections for the current cameras view. if (reflections != null) { //Blit into another texture to take a copy. Graphics.Blit(reflections, data.reflection.tex); m_imageBlur.BlurIterations = blurIterations; m_imageBlur.BlurMode = blurMode; m_imageBlur.BlurSpread = blurSpread; m_imageBlur.Blur(data.reflection.tex); Shader.SetGlobalTexture(Ocean.REFLECTION_TEXTURE_NAME, data.reflection.tex); } data.reflection.updated = true; } catch (Exception e) { Ocean.LogError(e.ToString()); WasError = true; enabled = false; } }
//[ImageEffectOpaque] void OnRenderImage(RenderTexture source, RenderTexture destination) { if (!ShouldRenderEffect()) { Graphics.Blit(source, destination); return; } Camera cam = GetComponent <Camera>(); float CAMERA_NEAR = cam.nearClipPlane; float CAMERA_FAR = cam.farClipPlane; float CAMERA_FOV = cam.fieldOfView; float CAMERA_ASPECT_RATIO = cam.aspect; Matrix4x4 frustumCorners = Matrix4x4.identity; float fovWHalf = CAMERA_FOV * 0.5f; Vector3 toRight = cam.transform.right * CAMERA_NEAR * Mathf.Tan(fovWHalf * Mathf.Deg2Rad) * CAMERA_ASPECT_RATIO; Vector3 toTop = cam.transform.up * CAMERA_NEAR * Mathf.Tan(fovWHalf * Mathf.Deg2Rad); Vector3 topLeft = (cam.transform.forward * CAMERA_NEAR - toRight + toTop); float CAMERA_SCALE = topLeft.magnitude * CAMERA_FAR / CAMERA_NEAR; topLeft.Normalize(); topLeft *= CAMERA_SCALE; Vector3 topRight = (cam.transform.forward * CAMERA_NEAR + toRight + toTop); topRight.Normalize(); topRight *= CAMERA_SCALE; Vector3 bottomRight = (cam.transform.forward * CAMERA_NEAR + toRight - toTop); bottomRight.Normalize(); bottomRight *= CAMERA_SCALE; Vector3 bottomLeft = (cam.transform.forward * CAMERA_NEAR - toRight - toTop); bottomLeft.Normalize(); bottomLeft *= CAMERA_SCALE; frustumCorners.SetRow(0, topLeft); frustumCorners.SetRow(1, topRight); frustumCorners.SetRow(2, bottomRight); frustumCorners.SetRow(3, bottomLeft); m_material.SetMatrix("_FrustumCorners", frustumCorners); Color mulCol = Ocean.Instance.SunColor() * Mathf.Max(0.0f, Vector3.Dot(Vector3.up, Ocean.Instance.SunDir())); mulCol = Color.Lerp(Color.white, mulCol, attenuationBySun); m_material.SetColor("_MultiplyCol", mulCol); RenderTexture belowTex = RenderTexture.GetTemporary(source.width, source.height, 0); CustomGraphicsBlit(source, belowTex, m_material, 0); m_imageBlur.BlurIterations = blurIterations; m_imageBlur.BlurMode = blurMode; m_imageBlur.BlurSpread = blurSpread; m_imageBlur.Blur(belowTex); m_material.SetTexture("_BelowTex", belowTex); Graphics.Blit(source, destination, m_material, 1); RenderTexture.ReleaseTemporary(belowTex); }
//[ImageEffectOpaque] void OnRenderImage(RenderTexture source, RenderTexture destination) { if (underWaterPostEffectSdr == null || m_material == null || SystemInfo.graphicsShaderLevel < 30) { Graphics.Blit(source, destination); //Debug.Log(Time.frameCount + " shader or material is null"); return; } if (Ocean.Instance == null || Ocean.Instance.UnderWater == null) { Graphics.Blit(source, destination); //Debug.Log(Time.frameCount + " instance or underwater is null"); return; } if (!Ocean.Instance.gameObject.activeInHierarchy) { Graphics.Blit(source, destination); //Debug.Log(Time.frameCount + " ocean not enabled"); return; } if (Ocean.Instance.UnderWater.Mode != UNDERWATER_MODE.ABOVE_AND_BELOW) { Graphics.Blit(source, destination); //Debug.Log(Time.frameCount + " underwater mode not ABOVE_AND_BELOW"); return; } if (!m_underWaterIsVisible) { Graphics.Blit(source, destination); //Debug.Log(Time.frameCount + " underwater is not visible"); return; } Camera cam = GetComponent <Camera>(); float CAMERA_NEAR = cam.nearClipPlane; float CAMERA_FAR = cam.farClipPlane; float CAMERA_FOV = cam.fieldOfView; float CAMERA_ASPECT_RATIO = cam.aspect; Matrix4x4 frustumCorners = Matrix4x4.identity; float fovWHalf = CAMERA_FOV * 0.5f; Vector3 toRight = cam.transform.right * CAMERA_NEAR * Mathf.Tan(fovWHalf * Mathf.Deg2Rad) * CAMERA_ASPECT_RATIO; Vector3 toTop = cam.transform.up * CAMERA_NEAR * Mathf.Tan(fovWHalf * Mathf.Deg2Rad); Vector3 topLeft = (cam.transform.forward * CAMERA_NEAR - toRight + toTop); float CAMERA_SCALE = topLeft.magnitude * CAMERA_FAR / CAMERA_NEAR; topLeft.Normalize(); topLeft *= CAMERA_SCALE; Vector3 topRight = (cam.transform.forward * CAMERA_NEAR + toRight + toTop); topRight.Normalize(); topRight *= CAMERA_SCALE; Vector3 bottomRight = (cam.transform.forward * CAMERA_NEAR + toRight - toTop); bottomRight.Normalize(); bottomRight *= CAMERA_SCALE; Vector3 bottomLeft = (cam.transform.forward * CAMERA_NEAR - toRight - toTop); bottomLeft.Normalize(); bottomLeft *= CAMERA_SCALE; frustumCorners.SetRow(0, topLeft); frustumCorners.SetRow(1, topRight); frustumCorners.SetRow(2, bottomRight); frustumCorners.SetRow(3, bottomLeft); m_material.SetMatrix("_FrustumCorners", frustumCorners); Color col = Color.white; if (attenuateBySun) { col = Ocean.Instance.SunColor() * Mathf.Max(0.0f, Vector3.Dot(Vector3.up, Ocean.Instance.SunDir())); } m_material.SetColor("_MultiplyCol", col); RenderTexture belowTex = RenderTexture.GetTemporary(source.width, source.height, 0); CustomGraphicsBlit(source, belowTex, m_material, 0); m_imageBlur.BlurIterations = blurIterations; m_imageBlur.BlurMode = blurMode; m_imageBlur.BlurSpread = blurSpread; m_imageBlur.Blur(belowTex); m_material.SetTexture("_BelowTex", belowTex); Graphics.Blit(source, destination, m_material, 1); RenderTexture.ReleaseTemporary(belowTex); }