Example #1
0
        private TemporaryRenderTexture GetRenderTexture(int width, int height)
        {
            int adaptedWidth  = Mathf.ClosestPowerOfTwo(width / finalDivider);
            int adaptedHeight = Mathf.ClosestPowerOfTwo(height / finalDivider);

            var renderTexture = RenderTexturesCache.GetTemporary(adaptedWidth, adaptedHeight, 0, reflectionCamera.hdr && systemSupportsHDR ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32, true, false, true);

            renderTexture.Texture.filterMode = FilterMode.Trilinear;
            renderTexture.Texture.wrapMode   = TextureWrapMode.Clamp;

            return(renderTexture);
        }
Example #2
0
        private void RenderDistortions(RenderTexture source, RenderTexture target)
        {
            float distortionIntensity = localWaterCamera.ContainingWater.UnderwaterDistortionsIntensity;

            if (distortionIntensity > 0.0f)
            {
                int w             = Camera.current.pixelWidth >> 2;
                int h             = Camera.current.pixelHeight >> 2;
                var distortionTex = RenderTexturesCache.GetTemporary(w, h, 0, RenderTextureFormat.ARGB32, true, false, false);
                RenderDistortionMap(distortionTex);

                imeMaterial.SetTexture("_DistortionTex", distortionTex);
                imeMaterial.SetFloat("_DistortionIntensity", distortionIntensity);
                Graphics.Blit(source, target, imeMaterial, 3);

                distortionTex.Dispose();
            }
            else
            {
                Graphics.Blit(source, target);
            }
        }
Example #3
0
        void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            if (!localWaterCamera.enabled || localWaterCamera.ContainingWater == null)
            {
                Graphics.Blit(source, destination);
                return;
            }

            source.filterMode = FilterMode.Bilinear;

            var temp1 = RenderTexturesCache.GetTemporary(source.width, source.height, 0, destination != null ? destination.format : source.format, true, false);

            temp1.Texture.filterMode = FilterMode.Bilinear;
            temp1.Texture.wrapMode   = TextureWrapMode.Clamp;

            RenderDepthScatter(source, temp1);

            blur.TotalSize = localWaterCamera.ContainingWater.UnderwaterBlurSize * cameraBlurScale;
            blur.Apply(temp1);

            RenderDistortions(temp1, destination);
            temp1.Dispose();
        }
Example #4
0
        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();
        }
Example #5
0
        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();
                }
            }
        }