Example #1
0
        private void RetrieveRenderTexturesSet()
        {
            var format = twoChannels ?
                         (highPrecision ? RenderTextureFormat.ARGBFloat : RenderTextureFormat.ARGBHalf) :
                         (highPrecision ? RenderTextureFormat.RGFloat : RenderTextureFormat.RGHalf);

            renderTexturesSet = RenderTexturesCache.GetCache(resolution << 1, resolution << 1, 0, format, true, usesUAV);
        }
Example #2
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 #3
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 #4
0
        static public RenderTexturesCache GetCache(int width, int height, int depthBuffer, RenderTextureFormat format, bool linear, bool uav, bool mipMaps = false)
        {
            RenderTexturesUpdater.EnsureInstance();

            ulong hash = 0;

            hash |= (uint)width;
            hash |= ((uint)height << 16);
            hash |= ((ulong)depthBuffer << 29);                    // >> 3 << 32
            hash |= ((linear ? 1UL : 0UL) << 34);
            hash |= ((uav ? 1UL : 0UL) << 35);
            hash |= ((mipMaps ? 1UL : 0UL) << 36);
            hash |= ((ulong)format << 37);

            RenderTexturesCache renderTexturesCache;

            if (!cache.TryGetValue(hash, out renderTexturesCache))
            {
                cache[hash] = renderTexturesCache = new RenderTexturesCache(hash, (int)width, (int)height, (int)depthBuffer, format, linear, uav, mipMaps);
            }

            return(renderTexturesCache);
        }
Example #5
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 #6
0
 internal TemporaryRenderTexture(RenderTexturesCache renderTexturesCache)
 {
     this.renderTexturesCache = renderTexturesCache;
     this.renderTexture       = renderTexturesCache.GetTemporaryDirect();
 }
Example #7
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 #8
0
        private void ValidateResources()
        {
            if (windWaves.CopyFrom == null)
            {
                ValidateFFT(ref heightFFT, (renderedMaps & MapType.Displacement) != 0, false);
                ValidateFFT(ref displacementFFT, (renderedMaps & MapType.Displacement) != 0, true);
                ValidateFFT(ref slopeFFT, (renderedMaps & MapType.Slope) != 0, true);
            }

            if (displacementMaps == null || slopeMaps == null || displacedHeightMap == null)
            {
                bool flatten = (!water.Volume.Boundless && flattenMode == FlattenMode.Auto) || flattenMode == FlattenMode.ForcedOn;

                if (this.flatten != flatten)
                {
                    this.flatten = flatten;

                    if (displacedHeightMap != null)
                    {
                        displacedHeightMap.Destroy();
                        displacedHeightMap = null;
                    }
                }

                RenderTexture[] usedDisplacementMaps, usedSlopeMaps;
                RenderTexture   usedDisplacedHeightMap;

                if (windWaves.CopyFrom == null)
                {
                    int resolution = windWaves.FinalResolution;
                    int displacedHeightMapResolution = flatten ? resolution : (resolution >> 2);
                    int packResolution = resolution << 1;
                    singleTargetCache        = RenderTexturesCache.GetCache(packResolution, packResolution, 0, RenderTextureFormat.RHalf, true, heightFFT is Dx11FFT);
                    doubleTargetCache        = RenderTexturesCache.GetCache(packResolution, packResolution, 0, RenderTextureFormat.RGHalf, true, displacementFFT is Dx11FFT);
                    displacedHeightMapsCache = RenderTexturesCache.GetCache(displacedHeightMapResolution, displacedHeightMapResolution, 0, RenderTextureFormat.ARGBHalf, true, false, false);

                    if (displacementMaps == null && (renderedMaps & MapType.Displacement) != 0)
                    {
                        CreateRenderTextures(ref displacementMaps, RenderTextureFormat.ARGBHalf, 4, true);
                    }

                    if (slopeMaps == null && (renderedMaps & MapType.Slope) != 0)
                    {
                        CreateRenderTextures(ref slopeMaps, RenderTextureFormat.ARGBHalf, 2, true);
                    }

                    if (displacedHeightMap == null)
                    {
                        displacedHeightMap           = new RenderTexture(displacedHeightMapResolution, displacedHeightMapResolution, 0, RenderTextureFormat.ARGBHalf, RenderTextureReadWrite.Linear);
                        displacedHeightMap.hideFlags = HideFlags.DontSave;
                        displacedHeightMap.wrapMode  = TextureWrapMode.Repeat;

                        if (FloatingPointMipMapsSupport)
                        {
                            displacedHeightMap.filterMode       = FilterMode.Trilinear;
                            displacedHeightMap.useMipMap        = true;
                            displacedHeightMap.autoGenerateMips = true;
                        }
                        else
                        {
                            displacedHeightMap.filterMode = FilterMode.Bilinear;
                        }
                    }

                    usedDisplacementMaps   = displacementMaps;
                    usedSlopeMaps          = slopeMaps;
                    usedDisplacedHeightMap = displacedHeightMap;
                }
                else
                {
                    var copyFrom = windWaves.CopyFrom;

                    if (copyFrom.WaterWavesFFT.windWaves == null)
                    {
                        copyFrom.ResolveFinalSettings(WaterQualitySettings.Instance.CurrentQualityLevel);
                    }

                    copyFrom.WaterWavesFFT.ValidateResources();

                    usedDisplacementMaps   = copyFrom.WaterWavesFFT.displacementMaps;
                    usedSlopeMaps          = copyFrom.WaterWavesFFT.slopeMaps;
                    usedDisplacedHeightMap = copyFrom.WaterWavesFFT.displacedHeightMap;
                }

                for (int scaleIndex = 0; scaleIndex < 4; ++scaleIndex)
                {
                    string suffix = scaleIndex != 0 ? scaleIndex.ToString() : "";

                    if (usedDisplacementMaps != null)
                    {
                        water.WaterMaterial.SetTexture("_GlobalDisplacementMap" + suffix, usedDisplacementMaps[scaleIndex]);
                        water.WaterBackMaterial.SetTexture("_GlobalDisplacementMap" + suffix, usedDisplacementMaps[scaleIndex]);
                    }

                    if (scaleIndex < 2 && usedSlopeMaps != null)
                    {
                        water.WaterMaterial.SetTexture("_GlobalNormalMap" + suffix, usedSlopeMaps[scaleIndex]);
                        water.WaterBackMaterial.SetTexture("_GlobalNormalMap" + suffix, usedSlopeMaps[scaleIndex]);
                    }
                }

                water.WaterMaterial.SetTexture("_DisplacedHeightMaps", usedDisplacedHeightMap);
                water.WaterBackMaterial.SetTexture("_DisplacedHeightMaps", usedDisplacedHeightMap);
                water.WaterVolumeMaterial.SetTexture("_DisplacedHeightMaps", usedDisplacedHeightMap);

                if (ResourcesChanged != null)
                {
                    ResourcesChanged();
                }
            }
        }