/// <summary> /// Render stero depth pass for VR. /// The depths need to be rendered twice (once for each eye). /// </summary> void RenderSteroOceanDepth(DepthData data, Camera cam) { #if UNITY_540_OR_HIGHER && CETO_USE_STEAM_VR if (OceanVR.OpenVRInUse) { Shader.EnableKeyword("CETO_STERO_CAMERA"); if (cam.stereoTargetEye == StereoTargetEyeMask.Both || cam.stereoTargetEye == StereoTargetEyeMask.Left) { Vector3 eyePos; Quaternion eyeRot; Matrix4x4 projection; OceanVR.GetSteamVRLeftEye(cam, out eyePos, out eyeRot, out projection); RenderOceanDepth(data, data.target0, eyePos, eyeRot, projection); Shader.SetGlobalTexture(Ocean.OCEAN_DEPTH_TEXTURE_NAME0, data.target0); } if (cam.stereoTargetEye == StereoTargetEyeMask.Both || cam.stereoTargetEye == StereoTargetEyeMask.Right) { Vector3 eyePos; Quaternion eyeRot; Matrix4x4 projection; OceanVR.GetSteamVRRightEye(cam, out eyePos, out eyeRot, out projection); RenderOceanDepth(data, data.target1, eyePos, eyeRot, projection); Shader.SetGlobalTexture(Ocean.OCEAN_DEPTH_TEXTURE_NAME1, data.target1); } } else { Shader.DisableKeyword("CETO_STERO_CAMERA"); RenderOceanDepth(data, data.target0, cam.transform.position, cam.transform.rotation, cam.projectionMatrix); Shader.SetGlobalTexture(Ocean.OCEAN_DEPTH_TEXTURE_NAME0, data.target0); } #else Shader.DisableKeyword("CETO_STERO_CAMERA"); RenderOceanDepth(data, data.target0, cam.transform.position, cam.transform.rotation, cam.projectionMatrix); Shader.SetGlobalTexture(Ocean.OCEAN_DEPTH_TEXTURE_NAME0, data.target0); #endif }
private void CreateRefractionCommand(Camera cam, DepthData data) { if (this.depthMode == DEPTH_MODE.USE_DEPTH_BUFFER) { if (data.refractionCommand == null) { data.refractionCommand = new RefractionCommand(cam, this.copyDepthSdr, this.normalFadeSdr); } if (!data.refractionCommand.DisableCopyDepthCmd && base.DisableCopyDepthCmd) { Shader.SetGlobalTexture(Ocean.DEPTH_GRAB_TEXTURE_NAME, Texture2D.whiteTexture); } if (!data.refractionCommand.DisableNormalFadeCmd && base.DisableNormalFadeCmd) { Shader.SetGlobalTexture(Ocean.NORMAL_FADE_TEXTURE_NAME, Texture2D.blackTexture); } data.refractionCommand.DisableCopyDepthCmd = base.DisableCopyDepthCmd; data.refractionCommand.DisableNormalFadeCmd = base.DisableNormalFadeCmd; data.refractionCommand.UpdateCommands(); } else if (data.refractionCommand != null) { data.refractionCommand.ClearCommands(); data.refractionCommand = null; } }
private void CreateDepthCameraFor(Camera cam, DepthData data) { if (data.cam == null) { GameObject gameObject = new GameObject("Ceto Depth Camera: " + cam.name); gameObject.hideFlags = HideFlags.HideAndDontSave; gameObject.AddComponent <IgnoreOceanEvents>(); gameObject.AddComponent <DisableFog>(); gameObject.AddComponent <DisableShadows>(); data.cam = gameObject.AddComponent <Camera>(); data.cam.clearFlags = CameraClearFlags.Color; data.cam.backgroundColor = Color.white; data.cam.enabled = false; data.cam.renderingPath = RenderingPath.Forward; data.cam.targetTexture = null; data.cam.useOcclusionCulling = false; data.cam.RemoveAllCommandBuffers(); data.cam.targetTexture = null; } data.cam.fieldOfView = cam.fieldOfView; data.cam.nearClipPlane = cam.nearClipPlane; data.cam.farClipPlane = cam.farClipPlane; data.cam.transform.position = cam.transform.position; data.cam.transform.rotation = cam.transform.rotation; data.cam.worldToCameraMatrix = cam.worldToCameraMatrix; data.cam.projectionMatrix = cam.projectionMatrix; data.cam.orthographic = cam.orthographic; data.cam.aspect = cam.aspect; data.cam.orthographicSize = cam.orthographicSize; data.cam.rect = new Rect(0f, 0f, 1f, 1f); data.cam.layerCullDistances = cam.layerCullDistances; data.cam.layerCullSpherical = cam.layerCullSpherical; RenderTexture targetTexture = data.cam.targetTexture; if (targetTexture == null || targetTexture.width != cam.pixelWidth || targetTexture.height != cam.pixelHeight) { if (targetTexture != null) { RTUtility.ReleaseAndDestroy(targetTexture); } int pixelWidth = cam.pixelWidth; int pixelHeight = cam.pixelHeight; int depth = 24; RenderTextureFormat format = RenderTextureFormat.RGFloat; if (!SystemInfo.SupportsRenderTextureFormat(format)) { format = RenderTextureFormat.RGHalf; } if (!SystemInfo.SupportsRenderTextureFormat(format)) { format = RenderTextureFormat.ARGBHalf; } data.cam.targetTexture = new RenderTexture(pixelWidth, pixelHeight, depth, format, RenderTextureReadWrite.Linear); data.cam.targetTexture.filterMode = FilterMode.Bilinear; data.cam.targetTexture.hideFlags = HideFlags.DontSave; data.cam.targetTexture.name = "Ceto Ocean Depths Render Target: " + cam.name; } }
/// <summary> /// Render the ocean depth pass into the provided target /// at the provided camera position. /// </summary> void RenderOceanDepth(DepthData data, RenderTexture target, Vector3 position, Quaternion rotation, Matrix4x4 projection) { NotifyOnEvent.Disable = true; data.cam.ResetWorldToCameraMatrix(); data.cam.transform.position = position; data.cam.transform.rotation = rotation; data.cam.projectionMatrix = projection; data.cam.targetTexture = target; data.cam.RenderWithShader(oceanDepthSdr, "RenderType"); data.cam.targetTexture = null; NotifyOnEvent.Disable = false; }
/// <summary> /// Create the refraction command buffer. /// </summary> void CreateRefractionGrab(Camera cam, DepthData data) { IRefractionCommand cmd = GetRefractionCommand(); CameraEvent cmdEvent = GetCommandEvent(cam); //The command has already been created. if (data.grabCmd != null) { if (depthMode != DEPTH_MODE.USE_DEPTH_BUFFER || refractionResolution != cmd.Resolution || cmdEvent != data.cmdEvent || !cmd.Matches(cam)) { //The current cmd settings no longer match camera //or the depth mode is no longer DEPTH_BUFFER //Remove command and release. cmd.Remove(cam); data.grabCmd = null; } else { //The settings have not changed. return; } } //If the depth mode is USE_DEPTH_BUFFER or useCmdBufferForGrab //is true then a command buffer is needed. if (depthMode == DEPTH_MODE.USE_DEPTH_BUFFER) { cmd.Resolution = refractionResolution; cmd.Event = cmdEvent; data.grabCmd = cmd.Create(cam); data.cmdEvent = cmdEvent; } }
/// <summary> /// Create the refraction command buffer. /// </summary> void CreateRefractionCommand(Camera cam, DepthData data) { if (depthMode == DEPTH_MODE.USE_DEPTH_BUFFER) { //Need refraction command. Create and update. if (data.refractionCommand == null) { data.refractionCommand = new RefractionCommand(cam, copyDepthSdr, normalFadeSdr); } //If commands has been disabled this frame then zeo texture. if (!data.refractionCommand.DisableCopyDepthCmd && DisableCopyDepthCmd) { Shader.SetGlobalTexture(Ocean.DEPTH_GRAB_TEXTURE_NAME, Texture2D.whiteTexture); } if (!data.refractionCommand.DisableNormalFadeCmd && DisableNormalFadeCmd) { Shader.SetGlobalTexture(Ocean.NORMAL_FADE_TEXTURE_NAME, Texture2D.blackTexture); } data.refractionCommand.DisableCopyDepthCmd = DisableCopyDepthCmd; data.refractionCommand.DisableNormalFadeCmd = DisableNormalFadeCmd; data.refractionCommand.UpdateCommands(); } else { //Dont need the refraction command for this mode if (data.refractionCommand != null) { data.refractionCommand.ClearCommands(); data.refractionCommand = null; } } }
/// <summary> /// Create the camera used for the ocean depths. /// </summary> void CreateDepthCameraFor(Camera cam, DepthData data) { if (data.cam == null) { GameObject go = new GameObject("Ceto Depth Camera: " + cam.name); go.hideFlags = HideFlags.HideAndDontSave; go.AddComponent<IgnoreOceanEvents>(); go.AddComponent<DisableFog>(); go.AddComponent<DisableShadows>(); data.cam = go.AddComponent<Camera>(); //data.cam.CopyFrom(cam); //This will cause a recursive culling error in Unity >= 5.4 data.cam.clearFlags = CameraClearFlags.SolidColor; data.cam.backgroundColor = Color.white; data.cam.enabled = false; data.cam.renderingPath = RenderingPath.Forward; data.cam.targetTexture = null; data.cam.useOcclusionCulling = false; data.cam.RemoveAllCommandBuffers(); data.cam.targetTexture = null; } data.cam.fieldOfView = cam.fieldOfView; data.cam.nearClipPlane = cam.nearClipPlane; data.cam.farClipPlane = cam.farClipPlane; data.cam.transform.position = cam.transform.position; data.cam.transform.rotation = cam.transform.rotation; data.cam.worldToCameraMatrix = cam.worldToCameraMatrix; data.cam.projectionMatrix = cam.projectionMatrix; data.cam.orthographic = cam.orthographic; data.cam.aspect = cam.aspect; data.cam.orthographicSize = cam.orthographicSize; data.cam.rect = new Rect(0, 0, 1, 1); data.cam.layerCullDistances = cam.layerCullDistances; data.cam.layerCullSpherical = cam.layerCullSpherical; RenderTexture tex = data.cam.targetTexture; if (tex == null || tex.width != cam.pixelWidth || tex.height != cam.pixelHeight) { if (tex != null) RTUtility.ReleaseAndDestroy(tex); int width = cam.pixelWidth; int height = cam.pixelHeight; int depth = 24; RenderTextureFormat format; if (SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RGFloat)) format = RenderTextureFormat.RGFloat; else format = RenderTextureFormat.RGHalf; data.cam.targetTexture = new RenderTexture(width, height, depth, format, RenderTextureReadWrite.Linear); data.cam.targetTexture.filterMode = FilterMode.Bilinear; data.cam.targetTexture.hideFlags = HideFlags.DontSave; data.cam.targetTexture.name = "Ceto Ocean Depths Render Target: " + cam.name; } }
/// <summary> /// Create the camera used for the ocean depths /// If it has already been created then just update it. /// Also create the render targets for the camera. /// </summary> void CreateDepthCameraFor(Camera cam, DepthData data) { if (data.cam == null) { GameObject go = new GameObject("Ceto Depth Camera: " + cam.name); go.hideFlags = HideFlags.HideAndDontSave; go.AddComponent <IgnoreOceanEvents>(); go.AddComponent <DisableFog>(); go.AddComponent <DisableShadows>(); data.cam = go.AddComponent <Camera>(); data.cam.clearFlags = CameraClearFlags.SolidColor; data.cam.backgroundColor = Color.white; data.cam.enabled = false; data.cam.renderingPath = RenderingPath.Forward; data.cam.targetTexture = null; data.cam.useOcclusionCulling = false; data.cam.RemoveAllCommandBuffers(); data.cam.targetTexture = null; } //Note - position rotation and projection set before rendering. //Update other settings here. data.cam.fieldOfView = cam.fieldOfView; data.cam.nearClipPlane = cam.nearClipPlane; data.cam.farClipPlane = cam.farClipPlane; data.cam.orthographic = cam.orthographic; data.cam.aspect = cam.aspect; data.cam.orthographicSize = cam.orthographicSize; data.cam.rect = new Rect(0, 0, 1, 1); data.cam.layerCullDistances = cam.layerCullDistances; data.cam.layerCullSpherical = cam.layerCullSpherical; if (data.target0 == null || data.target0.width != cam.pixelWidth || data.target0.height != cam.pixelHeight) { data.DestroyTargets(); int width = cam.pixelWidth; int height = cam.pixelHeight; int depth = 24; RenderTextureFormat format = RenderTextureFormat.RGFloat; if (!SystemInfo.SupportsRenderTextureFormat(format)) { format = RenderTextureFormat.RGHalf; } if (!SystemInfo.SupportsRenderTextureFormat(format)) { format = RenderTextureFormat.ARGBHalf; } data.target0 = new RenderTexture(width, height, depth, format, RenderTextureReadWrite.Linear); data.target0.filterMode = FilterMode.Point; data.target0.hideFlags = HideFlags.DontSave; data.target0.name = "Ceto Ocean Depths Render Target0: " + cam.name; if (cam.stereoEnabled) { data.target1 = new RenderTexture(width, height, depth, format, RenderTextureReadWrite.Linear); data.target1.filterMode = FilterMode.Point; data.target1.hideFlags = HideFlags.DontSave; data.target1.name = "Ceto Ocean Depths Render Target1: " + cam.name; } } }