public void DestroyTargets() { RTUtility.ReleaseAndDestroy(target0); RTUtility.ReleaseAndDestroy(target1); target0 = null; target1 = null; }
/// <summary> /// Create the render targets for the reflection camera and the reflection texture. /// </summary> void CreateRenderTarget(ReflectionData data, string camName, int width, int height, bool isHdr, OceanCameraSettings settings) { int scale = ResolutionToNumber(GetReflectionResolution(settings)); width /= scale; height /= scale; //If the texture has been created and settings have not changed //just update the ansio settings and return. if (data.tex != null && data.tex.width == width && data.tex.height == height) { data.tex.anisoLevel = ansio; return; } //Settings have changed or textures not created. if (data.tex != null) { RTUtility.ReleaseAndDestroy(data.tex); } RenderTextureFormat format; if (isHdr || QualitySettings.activeColorSpace == ColorSpace.Linear) { format = RenderTextureFormat.ARGBHalf; } else { format = RenderTextureFormat.ARGB32; } //This is the actual texture that will be sampled from for the reflections data.tex = new RenderTexture(width, height, 0, format, RenderTextureReadWrite.Default); data.tex.filterMode = FilterMode.Bilinear; data.tex.wrapMode = TextureWrapMode.Clamp; data.tex.useMipMap = false; data.tex.anisoLevel = ansio; data.tex.hideFlags = HideFlags.HideAndDontSave; data.tex.name = "Ceto Reflection Texture: " + camName; //This is the camera that will render the reflections. //Maybe null if a custom reflection method is being used. if (data.cam != null) { if (data.cam.targetTexture != null) { RTUtility.ReleaseAndDestroy(data.cam.targetTexture); } data.cam.targetTexture = new RenderTexture(width, height, 16, format, RenderTextureReadWrite.Default); data.cam.targetTexture.filterMode = FilterMode.Bilinear; data.cam.targetTexture.wrapMode = TextureWrapMode.Clamp; //data.cam.targetTexture.useMipMap = false; data.cam.targetTexture.anisoLevel = 0; data.cam.targetTexture.hideFlags = HideFlags.HideAndDontSave; data.cam.targetTexture.name = "Ceto Reflection Render Target: " + camName; } }
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; } }
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> /// Remove and release this camera /// and its data from the ocean. /// </summary> public void RemoveCameraData(Camera cam) { if (!m_cameraData.ContainsKey(cam)) { return; } CameraData data = m_cameraData[cam]; if (data.overlay != null) { OverlayManager.DestroyBuffers(data.overlay); } if (data.reflection != null) { RTUtility.ReleaseAndDestroy(data.reflection.tex); data.reflection.tex = null; 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; } } if (data.depth != null) { if (data.depth.cam != null) { RTUtility.ReleaseAndDestroy(data.depth.cam.targetTexture); data.depth.cam.targetTexture = null; Destroy(data.depth.cam.gameObject); Destroy(data.depth.cam); data.depth.cam = null; } } if (data.mask != null) { if (data.mask.cam != null) { RTUtility.ReleaseAndDestroy(data.mask.cam.targetTexture); data.mask.cam.targetTexture = null; Destroy(data.mask.cam.gameObject); Destroy(data.mask.cam); data.mask.cam = null; } } m_cameraData.Remove(cam); }
public void DestroyBuffers(WaveOverlayData data) { RTUtility.ReleaseAndDestroy(data.normal); RTUtility.ReleaseAndDestroy(data.height); RTUtility.ReleaseAndDestroy(data.foam); RTUtility.ReleaseAndDestroy(data.clip); data.normal = null; data.height = null; data.foam = null; data.clip = null; }
public override void Release() { this.m_tmpList.Clear(); this.m_fourier.Release(); int num = this.m_buffers.Length; for (int i = 0; i < num; i++) { RTUtility.ReleaseAndDestroy(this.m_buffers[i].data); this.m_buffers[i].data[0] = null; this.m_buffers[i].data[1] = null; } }
/// <summary> /// Release the buffers. /// </summary> public override void Release() { m_tmpList.Clear(); m_fourier.Release(); int count = m_buffers.Length; for (int i = 0; i < count; i++) { RTUtility.ReleaseAndDestroy(m_buffers[i].data); m_buffers[i].data[0] = null; m_buffers[i].data[1] = null; } }
private void CreateRenderTarget(ReflectionData data, string camName, int width, int height, bool isHdr, OceanCameraSettings settings) { int num = this.ResolutionToNumber(this.GetReflectionResolution(settings)); width /= num; height /= num; if (data.tex != null && data.tex.width == width && data.tex.height == height) { data.tex.anisoLevel = this.ansio; return; } if (data.tex != null) { RTUtility.ReleaseAndDestroy(data.tex); } RenderTextureFormat format; if ((isHdr || QualitySettings.activeColorSpace == ColorSpace.Linear) && SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBHalf)) { format = RenderTextureFormat.ARGBHalf; } else { format = RenderTextureFormat.ARGB32; } data.tex = new RenderTexture(width, height, 0, format, RenderTextureReadWrite.Default); data.tex.filterMode = FilterMode.Bilinear; data.tex.wrapMode = TextureWrapMode.Clamp; data.tex.useMipMap = false; data.tex.anisoLevel = this.ansio; data.tex.hideFlags = HideFlags.HideAndDontSave; data.tex.name = "Ceto Reflection Texture: " + camName; if (data.cam != null) { if (data.cam.targetTexture != null) { RTUtility.ReleaseAndDestroy(data.cam.targetTexture); } data.cam.targetTexture = new RenderTexture(width, height, 16, format, RenderTextureReadWrite.Default); data.cam.targetTexture.filterMode = FilterMode.Bilinear; data.cam.targetTexture.wrapMode = TextureWrapMode.Clamp; data.cam.targetTexture.useMipMap = false; data.cam.targetTexture.anisoLevel = 0; data.cam.targetTexture.hideFlags = HideFlags.HideAndDontSave; data.cam.targetTexture.name = "Ceto Reflection Render Target: " + camName; } }
private void Release() { if (this.m_displacementBuffer != null) { this.m_displacementBuffer.Release(); this.m_displacementBuffer = null; } if (this.m_slopeBuffer != null) { this.m_slopeBuffer.Release(); this.m_slopeBuffer = null; } if (this.m_jacobianBuffer != null) { this.m_jacobianBuffer.Release(); this.m_jacobianBuffer = null; } if (this.m_readBuffer != null) { this.m_readBuffer.Release(); this.m_readBuffer = null; } if (this.m_conditions != null && this.m_conditions[0] != null && this.m_conditions[0].Done) { this.CacheCondition(this.m_conditions[0]); if (this.m_conditionCache == null || !this.m_conditionCache.ContainsKey(this.m_conditions[0].Key)) { this.m_conditions[0].Release(); this.m_conditions[0] = null; } } if (this.m_conditions != null && this.m_conditions[1] != null && this.m_conditions[1].Done && (this.m_conditionCache == null || !this.m_conditionCache.ContainsKey(this.m_conditions[1].Key))) { this.m_conditions[1].Release(); this.m_conditions[1] = null; } this.m_conditions = null; this.m_findRangeTask = null; RTUtility.ReleaseAndDestroy(this.m_displacementMaps); this.m_displacementMaps = null; RTUtility.ReleaseAndDestroy(this.m_slopeMaps); this.m_slopeMaps = null; RTUtility.ReleaseAndDestroy(this.m_foamMaps); this.m_foamMaps = null; }
public void RemoveCameraData(Camera cam) { if (!this.m_cameraData.ContainsKey(cam)) { return; } CameraData cameraData = this.m_cameraData[cam]; if (cameraData.overlay != null) { this.OverlayManager.DestroyBuffers(cameraData.overlay); } if (cameraData.reflection != null) { RTUtility.ReleaseAndDestroy(cameraData.reflection.tex); cameraData.reflection.tex = null; if (cameraData.reflection.cam != null) { RTUtility.ReleaseAndDestroy(cameraData.reflection.cam.targetTexture); cameraData.reflection.cam.targetTexture = null; UnityEngine.Object.Destroy(cameraData.reflection.cam.gameObject); UnityEngine.Object.Destroy(cameraData.reflection.cam); cameraData.reflection.cam = null; } } if (cameraData.depth != null && cameraData.depth.cam != null) { RTUtility.ReleaseAndDestroy(cameraData.depth.cam.targetTexture); cameraData.depth.cam.targetTexture = null; UnityEngine.Object.Destroy(cameraData.depth.cam.gameObject); UnityEngine.Object.Destroy(cameraData.depth.cam); cameraData.depth.cam = null; } if (cameraData.mask != null && cameraData.mask.cam != null) { RTUtility.ReleaseAndDestroy(cameraData.mask.cam.targetTexture); cameraData.mask.cam.targetTexture = null; UnityEngine.Object.Destroy(cameraData.mask.cam.gameObject); UnityEngine.Object.Destroy(cameraData.mask.cam); cameraData.mask.cam = null; } this.m_cameraData.Remove(cam); }
public void CreateBuffer(string name, Camera cam, OVERLAY_MAP_SIZE size, RenderTextureFormat format, bool isLinear, ref RenderTexture map) { float num = this.SizeToValue(size); int num2 = Mathf.Min(4096, (int)((float)cam.pixelWidth * num)); int num3 = Mathf.Min(4096, (int)((float)cam.pixelHeight * num)); if (map == null || map.width != num2 || map.height != num3) { if (map != null) { RTUtility.ReleaseAndDestroy(map); } RenderTextureReadWrite readWrite = (!isLinear) ? RenderTextureReadWrite.Default : RenderTextureReadWrite.Linear; map = new RenderTexture(num2, num3, 0, format, readWrite); map.useMipMap = false; map.filterMode = FilterMode.Bilinear; map.hideFlags = HideFlags.DontSave; map.name = "Ceto Overlay " + name + " Buffer: " + cam.name; } }
public override void RenderReflection(GameObject go) { try { if (base.enabled) { Camera current = Camera.current; if (!(current == null)) { CameraData cameraData = this.m_ocean.FindCameraData(current); if (cameraData.reflection == null) { cameraData.reflection = new ReflectionData(); } if (!cameraData.reflection.updated) { if (this.GetDisableReflections(cameraData.settings)) { Shader.DisableKeyword("CETO_REFLECTION_ON"); cameraData.reflection.updated = true; } else { Shader.EnableKeyword("CETO_REFLECTION_ON"); if (cameraData.reflection.cam != null) { DisableFog component = cameraData.reflection.cam.GetComponent <DisableFog>(); if (component != null) { component.enabled = !this.fogInReflection; } } RenderTexture renderTexture; if (this.RenderReflectionCustom != null) { if (cameraData.reflection.cam != null) { RTUtility.ReleaseAndDestroy(cameraData.reflection.cam.targetTexture); cameraData.reflection.cam.targetTexture = null; UnityEngine.Object.Destroy(cameraData.reflection.cam.gameObject); UnityEngine.Object.Destroy(cameraData.reflection.cam); cameraData.reflection.cam = null; } this.CreateRenderTarget(cameraData.reflection, current.name, current.pixelWidth, current.pixelHeight, current.hdr, cameraData.settings); if (this.m_dummy == null) { this.m_dummy = new GameObject("Ceto Reflection Dummy Gameobject"); this.m_dummy.hideFlags = HideFlags.HideAndDontSave; } this.m_dummy.transform.position = new Vector3(0f, this.m_ocean.level, 0f); renderTexture = this.RenderReflectionCustom(this.m_dummy); } else { this.CreateReflectionCameraFor(current, cameraData.reflection); this.CreateRenderTarget(cameraData.reflection, current.name, current.pixelWidth, current.pixelHeight, current.hdr, cameraData.settings); if (current.stereoEnabled) { } NotifyOnEvent.Disable = true; this.RenderReflectionFor(current, cameraData.reflection.cam, cameraData.settings); NotifyOnEvent.Disable = false; renderTexture = cameraData.reflection.cam.targetTexture; } if (renderTexture != null) { if (this.blitmaterial == null) { this.blitmaterial = new Material(Shader.Find("Hidden/TheForestBlitCopyFullscreen")); } Graphics.Blit(renderTexture, cameraData.reflection.tex, this.blitmaterial); this.m_imageBlur.BlurIterations = this.blurIterations; this.m_imageBlur.BlurMode = this.blurMode; this.m_imageBlur.BlurSpread = this.blurSpread; this.m_imageBlur.Blur(cameraData.reflection.tex); Shader.SetGlobalTexture(Ocean.REFLECTION_TEXTURE_NAME, cameraData.reflection.tex); } cameraData.reflection.updated = true; } } } } } catch (Exception ex) { Ocean.LogError(ex.ToString()); base.WasError = true; base.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; } }
/// <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; } }