private void SwapRT() { var t = rt1; rt1 = rt2; rt2 = t; }
override public void ComputeFFT(Texture tex, RenderTexture target) { using (rt1 = renderTexturesSet.GetTemporary()) using (rt2 = renderTexturesSet.GetTemporary()) { ComputeFFT(tex, null, twoChannels ? 2 : 0); ComputeFFT(rt1, target, twoChannels ? 3 : 1); } }
private void RenderReflection(Camera camera) { if (!enabled) { return; } if (reflectionCamera == null) { var reflectionCameraGo = new GameObject(name + " Reflection Camera"); reflectionCameraGo.transform.parent = transform; reflectionCamera = reflectionCameraGo.AddComponent <Camera>(); ValidateReflectionCamera(); } reflectionCamera.hdr = systemSupportsHDR && camera.hdr; reflectionCamera.backgroundColor = new Color(0.0f, 0.0f, 0.0f, 0.0f); currentTarget = GetRenderTexture(camera.pixelWidth, camera.pixelHeight); temporaryTargets[camera] = currentTarget; var target = RenderTexturesCache.GetTemporary(currentTarget.Texture.width, currentTarget.Texture.height, 16, currentTarget.Texture.format, true, false, false); reflectionCamera.targetTexture = target; reflectionCamera.aspect = camera.aspect; Vector3 cameraEuler = camera.transform.eulerAngles; reflectionCamera.transform.eulerAngles = new Vector3(-cameraEuler.x, cameraEuler.y, cameraEuler.z); reflectionCamera.transform.position = camera.transform.position; Vector3 cameraPosition = camera.transform.position; cameraPosition.y = transform.position.y - cameraPosition.y; reflectionCamera.transform.position = cameraPosition; float d = -transform.position.y - clipPlaneOffset; Vector4 reflectionPlane = new Vector4(0, 1, 0, d); Matrix4x4 reflection = Matrix4x4.zero; reflection = CalculateReflectionMatrix(reflection, reflectionPlane); Vector3 newpos = reflection.MultiplyPoint(camera.transform.position); reflectionCamera.worldToCameraMatrix = camera.worldToCameraMatrix * reflection; Vector4 clipPlane = CameraSpacePlane(reflectionCamera, transform.position, new Vector3(0, 1, 0), 1.0f); var matrix = camera.projectionMatrix; matrix = CalculateObliqueMatrix(matrix, clipPlane); reflectionCamera.projectionMatrix = matrix; reflectionCamera.transform.position = newpos; Vector3 cameraEulerB = camera.transform.eulerAngles; reflectionCamera.transform.eulerAngles = new Vector3(-cameraEulerB.x, cameraEulerB.y, cameraEulerB.z); reflectionCamera.clearFlags = reflectSkybox ? CameraClearFlags.Skybox : CameraClearFlags.SolidColor; GL.invertCulling = true; reflectionCamera.Render(); GL.invertCulling = false; reflectionCamera.targetTexture = null; if (utilitiesMaterial == null) { utilitiesMaterial = new Material(utilitiesShader); utilitiesMaterial.hideFlags = HideFlags.DontSave; } Graphics.Blit(target, currentTarget, utilitiesMaterial, 0); target.Dispose(); }
private void ValidateWaveMaps() { int frameCount = Time.frameCount; if (waveMapsFrame == frameCount || !Application.isPlaying) { return; // it's already done } if (lastCopyFrom != null) { if (copyModeDirty) { copyModeDirty = false; ValidateResources(); } return; } waveMapsFrame = frameCount; // render needed spectra Texture heightSpectrum, slopeSpectrum, displacementSpectrum; RenderSpectra(out heightSpectrum, out slopeSpectrum, out displacementSpectrum); // displacement if ((renderedMaps & MapType.Displacement) != 0) { ClearDisplacedHeightMaps(); TemporaryRenderTexture displacedHeightMapTemp = displacedHeightMapsCache.GetTemporary(); TemporaryRenderTexture packedHeightMaps = singleTargetCache.GetTemporary(); TemporaryRenderTexture packedHorizontalDisplacementMaps = doubleTargetCache.GetTemporary(); heightFFT.ComputeFFT(heightSpectrum, packedHeightMaps); displacementFFT.ComputeFFT(displacementSpectrum, packedHorizontalDisplacementMaps); fftUtilitiesMaterial.SetTexture("_HeightTex", packedHeightMaps); fftUtilitiesMaterial.SetTexture("_DisplacementTex", packedHorizontalDisplacementMaps); fftUtilitiesMaterial.SetFloat("_HorizontalDisplacementScale", water.HorizontalDisplacementScale); for (int scaleIndex = 0; scaleIndex < 4; ++scaleIndex) { fftUtilitiesMaterial.SetFloat("_JacobianScale", water.HorizontalDisplacementScale * 0.1f * displacementMaps[scaleIndex].width / windWaves.TileSizes[scaleIndex]); // * 220.0f * displacementMaps[scaleIndex].width / (2048.0f * water.SpectraRenderer.TileSizes[scaleIndex]) fftUtilitiesMaterial.SetVector("_Offset", offsets[scaleIndex]); Graphics.Blit(null, displacementMaps[scaleIndex], fftUtilitiesMaterial, 1); RenderDisplacedHeightMaps(displacementMaps[scaleIndex], displacedHeightMapTemp, scaleIndex); } packedHeightMaps.Dispose(); packedHorizontalDisplacementMaps.Dispose(); // copy and generate mip maps Graphics.Blit(displacedHeightMapTemp, displacedHeightMap); displacedHeightMapTemp.Dispose(); } // slope if ((renderedMaps & MapType.Slope) != 0) { if (!finalHighQualitySlopeMaps) { for (int scalesIndex = 0; scalesIndex < 2; ++scalesIndex) { int resolution = windWaves.FinalResolution; fftUtilitiesMaterial.SetFloat("_Intensity1", 0.58f * resolution / windWaves.TileSizes[scalesIndex * 2]); fftUtilitiesMaterial.SetFloat("_Intensity2", 0.58f * resolution / windWaves.TileSizes[scalesIndex * 2 + 1]); fftUtilitiesMaterial.SetTexture("_MainTex", displacementMaps[scalesIndex * 2]); fftUtilitiesMaterial.SetTexture("_SecondTex", displacementMaps[scalesIndex * 2 + 1]); Graphics.Blit(null, slopeMaps[scalesIndex], fftUtilitiesMaterial, 0); } } else { TemporaryRenderTexture packedSlopeMaps = doubleTargetCache.GetTemporary(); slopeFFT.ComputeFFT(slopeSpectrum, packedSlopeMaps); for (int scalesIndex = 0; scalesIndex < 2; ++scalesIndex) { fftUtilitiesMaterial.SetVector("_Offset", offsetsDual[scalesIndex]); Graphics.Blit(packedSlopeMaps, slopeMaps[scalesIndex], fftUtilitiesMaterial, 3); } packedSlopeMaps.Dispose(); } } }