/// <summary> /// Render stero mask for VR. /// The mask needs to be rendered twice (once for each eye). /// </summary> void RenderSteroOceanMask(MaskData data, Camera cam, Shader sdr) { #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); RenderOceanMask(data, data.target0, eyePos, eyeRot, projection, sdr); Shader.SetGlobalTexture(Ocean.OCEAN_MASK_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); RenderOceanMask(data, data.target1, eyePos, eyeRot, projection, sdr); Shader.SetGlobalTexture(Ocean.OCEAN_MASK_TEXTURE_NAME1, data.target1); } } else { Shader.DisableKeyword("CETO_STERO_CAMERA"); RenderOceanMask(data, data.target0, cam.transform.position, cam.transform.rotation, cam.projectionMatrix, sdr); Shader.SetGlobalTexture(Ocean.OCEAN_MASK_TEXTURE_NAME0, data.target0); } #else Shader.DisableKeyword("CETO_STERO_CAMERA"); RenderOceanMask(data, data.target0, cam.transform.position, cam.transform.rotation, cam.projectionMatrix, sdr); Shader.SetGlobalTexture(Ocean.OCEAN_MASK_TEXTURE_NAME0, data.target0); #endif }
private void CreateMaskCameraFor(Camera cam, MaskData data) { if (data.cam == null) { GameObject gameObject = new GameObject("Ceto Mask 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.black; data.cam.cullingMask = 1 << LayerMask.NameToLayer(Ocean.OCEAN_LAYER); 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); if (data.cam.farClipPlane < this.OCEAN_BOTTOM_DEPTH * 2f) { data.cam.farClipPlane = this.OCEAN_BOTTOM_DEPTH * 2f; data.cam.ResetProjectionMatrix(); } 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 = 32; RenderTextureFormat 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.Point; data.cam.targetTexture.hideFlags = HideFlags.DontSave; data.cam.targetTexture.name = "Ceto Mask Render Target: " + cam.name; } }
/// <summary> /// Render the ocean mask pass into the provided target /// at the provided camera position. /// </summary> void RenderOceanMask(MaskData data, RenderTexture target, Vector3 position, Quaternion rotation, Matrix4x4 projection, Shader sdr) { 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(sdr, "OceanMask"); data.cam.targetTexture = null; NotifyOnEvent.Disable = false; }
/// <summary> /// Create the camera used for the mask. /// </summary> void CreateMaskCameraFor(Camera cam, MaskData data) { if (data.cam == null) { GameObject go = new GameObject("Ceto Mask 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.black; data.cam.cullingMask = 1 << LayerMask.NameToLayer(Ocean.OCEAN_LAYER); data.cam.enabled = false; data.cam.renderingPath = RenderingPath.Forward; data.cam.targetTexture = null; data.cam.useOcclusionCulling = false; //data.cam.opaqueSortMode = OpaqueSortMode.NoDistanceSort; 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); //If mask far plane is less than double the ocean bottom depth //it will contain a semicircle artefact. if (data.cam.farClipPlane < OCEAN_BOTTOM_DEPTH * 2) { data.cam.farClipPlane = OCEAN_BOTTOM_DEPTH * 2; data.cam.ResetProjectionMatrix(); } 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 = 32; data.cam.targetTexture = new RenderTexture(width, height, depth, RenderTextureFormat.RGHalf, RenderTextureReadWrite.Linear); data.cam.targetTexture.filterMode = FilterMode.Point; data.cam.targetTexture.hideFlags = HideFlags.DontSave; data.cam.targetTexture.name = "Ceto Mask Render Target: " + cam.name; } }
/// <summary> /// Create the camera used for the mask. /// If it has already been created then just update it. /// Also create the render targets for the camera. /// </summary> void CreateMaskCameraFor(Camera cam, MaskData data) { if (data.cam == null) { GameObject go = new GameObject("Ceto Mask 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 = new Color(0, 1, 0, 0); //Need to clear r to 0 and g to 1 data.cam.cullingMask = 1 << LayerMask.NameToLayer(Ocean.OCEAN_LAYER); 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); 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 = 32; RenderTextureFormat 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 Mask 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 Mask Render Target1: " + cam.name; } } }
/// <summary> /// Create the camera used for the mask. /// </summary> void CreateMaskCameraFor(Camera cam, MaskData data) { if (data.cam == null) { GameObject go = new GameObject("Ceto Mask 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.black; data.cam.cullingMask = 1 << LayerMask.NameToLayer(Ocean.OCEAN_LAYER); data.cam.enabled = false; data.cam.renderingPath = RenderingPath.Forward; data.cam.targetTexture = null; data.cam.useOcclusionCulling = false; //data.cam.opaqueSortMode = OpaqueSortMode.NoDistanceSort; 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); //If mask far plane is less than double the ocean bottom depth //it will contain a semicircle artefact. if (data.cam.farClipPlane < OCEAN_BOTTOM_DEPTH * 2) { data.cam.farClipPlane = OCEAN_BOTTOM_DEPTH * 2; data.cam.ResetProjectionMatrix(); } 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 = 32; data.cam.targetTexture = new RenderTexture(width, height, depth, RenderTextureFormat.RGHalf, RenderTextureReadWrite.Linear); data.cam.targetTexture.filterMode = FilterMode.Point; data.cam.targetTexture.hideFlags = HideFlags.DontSave; data.cam.targetTexture.name = "Ceto Mask Render Target: " + cam.name; } }