Example #1
0
        private void RenderReflection(Camera camera, Camera reflectionCamera)
        {
            reflectionCamera.cullingMask = this._Data.ReflectionMask;
            this.SetCameraSettings(camera, reflectionCamera);
            this._CurrentTarget            = this.GetRenderTexture(camera.pixelWidth, camera.pixelHeight, reflectionCamera);
            this._TemporaryTargets[camera] = this._CurrentTarget;
            TemporaryRenderTexture temporary = RenderTexturesCache.GetTemporary(this._CurrentTarget.Texture.width, this._CurrentTarget.Texture.height, 16, this._CurrentTarget.Texture.format, true, false, false);

            reflectionCamera.targetTexture         = temporary;
            reflectionCamera.transform.eulerAngles = PlanarReflection.CalculateReflectionAngles(camera);
            reflectionCamera.transform.position    = this.CalculateReflectionPosition(camera);
            float     w        = -this._Water.transform.position.y - 0.07f;
            Vector4   plane    = new Vector4(0f, 1f, 0f, w);
            Matrix4x4 matrix4x = Matrix4x4.zero;

            matrix4x = Reflection.CalculateReflectionMatrix(matrix4x, plane);
            Vector3 position = matrix4x.MultiplyPoint(camera.transform.position);

            reflectionCamera.worldToCameraMatrix = camera.worldToCameraMatrix * matrix4x;
            Vector4   clipPlane = Reflection.CameraSpacePlane(reflectionCamera, this._Water.transform.position, new Vector3(0f, 1f, 0f), 0.07f, 1f);
            Matrix4x4 matrix4x2 = camera.projectionMatrix;

            matrix4x2 = Reflection.CalculateObliqueMatrix(matrix4x2, clipPlane);
            reflectionCamera.projectionMatrix   = matrix4x2;
            reflectionCamera.transform.position = position;
            Vector3 eulerAngles = camera.transform.eulerAngles;

            reflectionCamera.transform.eulerAngles = new Vector3(-eulerAngles.x, eulerAngles.y, eulerAngles.z);
            reflectionCamera.clearFlags            = ((!this._Data.ReflectSkybox) ? CameraClearFlags.Color : CameraClearFlags.Skybox);
            if (this._Data.RenderShadows)
            {
                GL.invertCulling = true;
                reflectionCamera.Render();
                GL.invertCulling = false;
            }
            else
            {
                ShadowQuality shadows = QualitySettings.shadows;
                QualitySettings.shadows = ShadowQuality.Disable;
                GL.invertCulling        = true;
                reflectionCamera.Render();
                GL.invertCulling        = false;
                QualitySettings.shadows = shadows;
            }
            reflectionCamera.targetTexture = null;
            if (this._UtilitiesMaterial == null)
            {
                this._UtilitiesMaterial = new Material(this._UtilitiesShader)
                {
                    hideFlags = HideFlags.DontSave
                };
            }
            Graphics.Blit(temporary, this._CurrentTarget, this._UtilitiesMaterial, 0);
            temporary.Dispose();
        }
Example #2
0
        private TemporaryRenderTexture GetRenderTexture(int width, int height, Camera reflectionCamera)
        {
            int  width2   = Mathf.ClosestPowerOfTwo(Mathf.RoundToInt((float)width * this._FinalResolutionMultiplier));
            int  height2  = Mathf.ClosestPowerOfTwo(Mathf.RoundToInt((float)height * this._FinalResolutionMultiplier));
            bool allowHDR = reflectionCamera.allowHDR;
            TemporaryRenderTexture temporary = RenderTexturesCache.GetTemporary(width2, height2, 0, (!allowHDR || !this._SystemSupportsHdr || !WaterProjectSettings.Instance.AllowFloatingPointMipMaps) ? RenderTextureFormat.ARGB32 : RenderTextureFormat.ARGBHalf, true, false, true);

            temporary.Texture.filterMode = FilterMode.Trilinear;
            temporary.Texture.wrapMode   = TextureWrapMode.Clamp;
            return(temporary);
        }
Example #3
0
        private void RenderMaps(float time, RenderTexture[] displacementMaps, RenderTexture[] normalMaps)
        {
            Texture tex;
            Texture tex2;
            Texture tex3;

            this.RenderSpectra(time, out tex, out tex2, out tex3);
            if ((this._RenderedMaps & WavesRendererFFT.MapType.Displacement) != (WavesRendererFFT.MapType) 0)
            {
                TemporaryRenderTexture temporary  = this._SingleTargetCache.GetTemporary();
                TemporaryRenderTexture temporary2 = this._DoubleTargetCache.GetTemporary();
                this._HeightFFT.ComputeFFT(tex, temporary);
                this._DisplacementFFT.ComputeFFT(tex3, temporary2);
                this._FFTUtilitiesMaterial.SetTexture(ShaderVariables.HeightTex, temporary);
                this._FFTUtilitiesMaterial.SetTexture(ShaderVariables.DisplacementTex, temporary2);
                this._FFTUtilitiesMaterial.SetFloat(ShaderVariables.HorizontalDisplacementScale, this._Water.Materials.HorizontalDisplacementScale);
                for (int i = 0; i < 4; i++)
                {
                    this._FFTUtilitiesMaterial.SetFloat(ShaderVariables.JacobianScale, this._Water.Materials.HorizontalDisplacementScale * 0.1f * (float)displacementMaps[i].width / this._WindWaves.TileSizes[i]);
                    this._FFTUtilitiesMaterial.SetVector(ShaderVariables.Offset, WavesRendererFFT._Offsets[i]);
                    Graphics.Blit(null, displacementMaps[i], this._FFTUtilitiesMaterial, 1);
                }
                temporary.Dispose();
                temporary2.Dispose();
            }
            if ((this._RenderedMaps & WavesRendererFFT.MapType.Normal) != (WavesRendererFFT.MapType) 0)
            {
                if (!this._FinalHighQualityNormalMaps)
                {
                    for (int j = 0; j < 2; j++)
                    {
                        int finalResolution = this._WindWaves.FinalResolution;
                        this._FFTUtilitiesMaterial.SetFloat("_Intensity1", 0.58f * (float)finalResolution / this._WindWaves.TileSizes[j * 2]);
                        this._FFTUtilitiesMaterial.SetFloat("_Intensity2", 0.58f * (float)finalResolution / this._WindWaves.TileSizes[j * 2 + 1]);
                        this._FFTUtilitiesMaterial.SetTexture("_MainTex", displacementMaps[j << 1]);
                        this._FFTUtilitiesMaterial.SetTexture("_SecondTex", displacementMaps[(j << 1) + 1]);
                        this._FFTUtilitiesMaterial.SetFloat("_MainTex_Texel_Size", 1f / (float)displacementMaps[j << 1].width);
                        Graphics.Blit(null, normalMaps[j], this._FFTUtilitiesMaterial, 0);
                    }
                }
                else
                {
                    TemporaryRenderTexture temporary3 = this._DoubleTargetCache.GetTemporary();
                    this._NormalFFT.ComputeFFT(tex2, temporary3);
                    for (int k = 0; k < 2; k++)
                    {
                        this._FFTUtilitiesMaterial.SetVector(ShaderVariables.Offset, WavesRendererFFT._OffsetsDual[k]);
                        Graphics.Blit(temporary3, normalMaps[k], this._FFTUtilitiesMaterial, 3);
                    }
                    temporary3.Dispose();
                }
            }
        }
Example #4
0
        internal void Smooth()
        {
            if (this.Profile.Sigma <= 0.1f)
            {
                return;
            }
            TemporaryRenderTexture temporary = RenderTexturesCache.GetTemporary(this._Width, this._Height, 0, WaterQualitySettings.Instance.Ripples.SimulationFormat, true, true, false);

            temporary.Verify(true);
            GaussianShader.VerticalInput  = this._Buffers[1];
            GaussianShader.VerticalOutput = temporary;
            GaussianShader.Dispatch(GaussianShader.KernelType.Vertical, this._Width, this._Height);
            GaussianShader.HorizontalInput  = temporary;
            GaussianShader.HorizontalOutput = this._Buffers[1];
            GaussianShader.Dispatch(GaussianShader.KernelType.Horizontal, this._Width, this._Height);
            temporary.Dispose();
        }
Example #5
0
        private void OnRenderImage(RenderTexture source, RenderTexture destination)
        {
            Water water = (!this._HasWaterOverride) ? this._LocalWaterCamera.ContainingWater : this._WaterOverride;

            if (!this._LocalWaterCamera.enabled || water == null)
            {
                Graphics.Blit(source, destination);
                return;
            }
            source.filterMode = FilterMode.Bilinear;
            TemporaryRenderTexture temporary = RenderTexturesCache.GetTemporary(source.width, source.height, 0, (!(destination != null)) ? source.format : destination.format, true, false, false);

            temporary.Texture.filterMode = FilterMode.Bilinear;
            temporary.Texture.wrapMode   = TextureWrapMode.Clamp;
            this.RenderDepthScatter(source, temporary);
            this._Blur.TotalSize = water.Materials.UnderwaterBlurSize * this._CameraBlurScale;
            this._Blur.Apply(temporary);
            this.RenderDistortions(temporary, destination);
            temporary.Dispose();
        }
Example #6
0
        private void RenderDistortions(Texture source, RenderTexture target)
        {
            Water water = (!this._HasWaterOverride) ? this._LocalWaterCamera.ContainingWater : this._WaterOverride;
            float underwaterDistortionsIntensity = water.Materials.UnderwaterDistortionsIntensity;

            if (underwaterDistortionsIntensity > 0f)
            {
                int width  = Camera.current.pixelWidth >> 2;
                int height = Camera.current.pixelHeight >> 2;
                TemporaryRenderTexture temporary = RenderTexturesCache.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32, true, false, false);
                this.RenderDistortionMap(temporary);
                temporary.Texture.filterMode = FilterMode.Bilinear;
                this._ImeMaterial.SetTexture("_DistortionTex", temporary);
                this._ImeMaterial.SetFloat("_DistortionIntensity", underwaterDistortionsIntensity);
                GraphicsUtilities.Blit(source, target, this._ImeMaterial, 2, water.Renderer.PropertyBlock);
                temporary.Dispose();
            }
            else
            {
                Graphics.Blit(source, target);
            }
        }
Example #7
0
        private void RenderMasks(Camera camera, WaterCamera waterCamera, MaterialPropertyBlock propertyBlock)
        {
            List <WaterVolumeSubtract> subtractiveVolumesDirect = this._Water.Volume.GetSubtractiveVolumesDirect();
            List <WaterVolumeAdd>      volumesDirect            = this._Water.Volume.GetVolumesDirect();

            if (waterCamera == null || !waterCamera.RenderVolumes || (subtractiveVolumesDirect.Count == 0 && volumesDirect.Count == 0 && this._Masks.Count == 0))
            {
                this.ReleaseTemporaryBuffers();
                return;
            }
            int    waterTempLayer      = WaterProjectSettings.Instance.WaterTempLayer;
            int    waterCollidersLayer = WaterProjectSettings.Instance.WaterCollidersLayer;
            Camera effectsCamera       = waterCamera.EffectsCamera;

            if (effectsCamera == null)
            {
                this.ReleaseTemporaryBuffers();
                return;
            }
            bool flag  = false;
            bool flag2 = false;
            bool flag3 = false;

            this.OnSharedSubtractiveMaskRender(ref flag, ref flag2, ref flag3);
            effectsCamera.CopyFrom(camera);
            effectsCamera.enabled = false;
            effectsCamera.GetComponent <WaterCamera>().enabled = false;
            effectsCamera.renderingPath    = RenderingPath.Forward;
            effectsCamera.depthTextureMode = DepthTextureMode.None;
            effectsCamera.cullingMask      = 1 << waterTempLayer;
            if (subtractiveVolumesDirect.Count != 0)
            {
                if (this._SubtractiveMaskTexture == null)
                {
                    this._SubtractiveMaskTexture = RenderTexture.GetTemporary(camera.pixelWidth, camera.pixelHeight, 24, (!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear, 1);
                }
                Graphics.SetRenderTarget(this._SubtractiveMaskTexture);
                int count = subtractiveVolumesDirect.Count;
                for (int i = 0; i < count; i++)
                {
                    subtractiveVolumesDirect[i].SetLayer(waterTempLayer);
                }
                TemporaryRenderTexture temporary = RenderTexturesCache.GetTemporary(camera.pixelWidth, camera.pixelHeight, 24, (!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGBFloat, true, false, false);
                effectsCamera.clearFlags      = CameraClearFlags.Color;
                effectsCamera.backgroundColor = new Color(0f, 0f, 0.5f, 0f);
                effectsCamera.targetTexture   = temporary;
                effectsCamera.RenderWithShader(this._VolumeFrontShader, "CustomType");
                GL.Clear(true, true, new Color(0f, 0f, 0f, 0f), 0f);
                Shader.SetGlobalTexture("_VolumesFrontDepth", temporary);
                effectsCamera.clearFlags    = CameraClearFlags.Nothing;
                effectsCamera.targetTexture = this._SubtractiveMaskTexture;
                effectsCamera.RenderWithShader(this._VolumeBackShader, "CustomType");
                temporary.Dispose();
                for (int j = 0; j < count; j++)
                {
                    subtractiveVolumesDirect[j].SetLayer(waterCollidersLayer);
                }
                propertyBlock.SetTexture(ShaderVariables.SubtractiveMask, this._SubtractiveMaskTexture);
            }
            if (volumesDirect.Count != 0)
            {
                this.OnSharedMaskAdditiveRender();
                if (this._AdditiveMaskTexture == null)
                {
                    this._AdditiveMaskTexture = RenderTexture.GetTemporary(camera.pixelWidth, camera.pixelHeight, 16, (!SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.ARGBFloat)) ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear, 1);
                }
                Graphics.SetRenderTarget(this._AdditiveMaskTexture);
                GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
                int count2 = volumesDirect.Count;
                for (int k = 0; k < count2; k++)
                {
                    volumesDirect[k].SetLayer(waterTempLayer);
                    volumesDirect[k].EnableRenderers(false);
                }
                effectsCamera.clearFlags    = CameraClearFlags.Nothing;
                effectsCamera.targetTexture = this._AdditiveMaskTexture;
                effectsCamera.RenderWithShader((!waterCamera.IsInsideAdditiveVolume) ? this._VolumeFrontFastShader : this._VolumeFrontShader, "CustomType");
                effectsCamera.clearFlags    = CameraClearFlags.Nothing;
                effectsCamera.targetTexture = this._AdditiveMaskTexture;
                effectsCamera.RenderWithShader(this._VolumeBackShader, "CustomType");
                for (int l = 0; l < count2; l++)
                {
                    volumesDirect[l].SetLayer(waterCollidersLayer);
                }
                propertyBlock.SetTexture(ShaderVariables.AdditiveMask, this._AdditiveMaskTexture);
            }
            this.OnSharedMaskPostRender();
            effectsCamera.targetTexture = null;
        }
 /// <summary>
 /// Creates a temporary texture, sets it as active in <see cref="RenderTexture.active"/>, then removes it
 /// and sets the previous active texture back automatically.
 /// </summary>
 /// <param name="width">Width of the temporary texture in pixels.</param>
 /// <param name="height">Height of the temporary texture in pixels.</param>
 /// <param name="depthBuffer">Depth buffer of the temporary texture.</param>
 /// <seealso cref="TemporaryRenderTexture"/>
 /// <example><code>
 /// using (var temporaryActiveTexture = new TemporaryActiveTexture(icon.width, icon.height, 0))
 /// {
 ///     Graphics.Blit(icon, temporary, material);
 /// });
 /// </code></example>
 public TemporaryActiveTexture(int width, int height, int depthBuffer)
 {
     _previousActiveTexture = RenderTexture.active;
     _value = new TemporaryRenderTexture(width, height, depthBuffer);
     RenderTexture.active = _value;
 }