Beispiel #1
0
        // On-demand create any objects we need for water
        void CreateWaterObjects(Camera currentCamera)
        {
            // Reflection render texture
            if (!_reflectionTexture || _reflectionTexture.width != _textureSize)
            {
                if (_reflectionTexture)
                {
                    DestroyImmediate(_reflectionTexture);
                }

                var format = _hdr ? RenderTextureFormat.ARGBHalf : RenderTextureFormat.ARGB32;
                Debug.Assert(SystemInfo.SupportsRenderTextureFormat(format), "The graphics device does not support the render texture format " + format.ToString());
                _reflectionTexture = new RenderTexture(_textureSize, _textureSize, _stencil ? 24 : 16, format)
                {
                    name         = "__WaterReflection" + GetHashCode(),
                    isPowerOfTwo = true,
                    hideFlags    = HideFlags.DontSave
                };
                _reflectionTexture.Create();
                PreparedReflections.Register(currentCamera.GetHashCode(), _reflectionTexture);
            }

            // Camera for reflection
            if (!_camReflections)
            {
                GameObject go = new GameObject("Water Refl Cam");
                _camReflections                    = go.AddComponent <Camera>();
                _camReflections.enabled            = false;
                _camReflections.transform.position = transform.position;
                _camReflections.transform.rotation = transform.rotation;
                _camReflections.cullingMask        = _reflectionLayers;
                _camReflectionsSkybox              = _camReflections.gameObject.AddComponent <Skybox>();
                _camReflections.gameObject.AddComponent <FlareLayer>();
                _camReflections.cameraType = CameraType.Reflection;
                var additionalCameraData = _camReflections.gameObject.AddComponent <LWRPAdditionalCameraData>();
                additionalCameraData.renderShadows        = !_disableShadows;
                additionalCameraData.requiresColorTexture = false;
                additionalCameraData.requiresDepthTexture = false;

                if (_hideCameraGameobject)
                {
                    go.hideFlags = HideFlags.HideAndDontSave;
                }
            }
        }
Beispiel #2
0
        // Called when visible to a camera
        void OnWillRenderObject()
        {
            if (OceanRenderer.Instance == null || Rend == null)
            {
                return;
            }

            // check if built-in pipeline being used
            if (Camera.current != null)
            {
                _currentCamera = Camera.current;
            }

            // Depth texture is used by ocean shader for transparency/depth fog, and for fading out foam at shoreline.
            _currentCamera.depthTextureMode |= DepthTextureMode.Depth;

            if (Rend.sharedMaterial != OceanRenderer.Instance.OceanMaterial)
            {
                Rend.sharedMaterial = OceanRenderer.Instance.OceanMaterial;
            }

            // per instance data

            if (_mpb == null)
            {
                _mpb = new PropertyWrapperMPB();
            }
            Rend.GetPropertyBlock(_mpb.materialPropertyBlock);

            // Only done here because current camera is defined. This could be done just once, probably on the OnRender function
            // or similar on the OceanPlanarReflection script?
            var reflTex = PreparedReflections.GetRenderTexture(_currentCamera.GetHashCode());

            if (reflTex)
            {
                _mpb.SetTexture(sp_ReflectionTex, reflTex);
            }
            else
            {
                _mpb.SetTexture(sp_ReflectionTex, Texture2D.blackTexture);
            }

            Rend.SetPropertyBlock(_mpb.materialPropertyBlock);
        }
Beispiel #3
0
        private void OnDisable()
        {
            if (_camViewpoint != null)
            {
                PreparedReflections.Remove(_camViewpoint.GetHashCode());
            }

            // Cleanup all the objects we possibly have created
            if (_reflectionTexture)
            {
                Destroy(_reflectionTexture);
                _reflectionTexture = null;
            }
            if (_camReflections)
            {
                Destroy(_camReflections.gameObject);
                _camReflections = null;
            }
        }
Beispiel #4
0
        private void OnDisable()
        {
            if (_camViewpoint != null)
            {
                PreparedReflections.Remove(_camViewpoint.GetHashCode());
            }

            // Cleanup all the objects we possibly have created
            if (_reflectionTexture)
            {
                Destroy(_reflectionTexture);
                _reflectionTexture = null;
            }
            if (_camReflections)
            {
                Destroy(_camReflections.gameObject);
                _camReflections = null;
            }

            WeatherMakerCommandBufferManagerScript.Instance.UnregisterPreCull(this);
        }
Beispiel #5
0
        private void OnDisable()
        {
            if (_camViewpoint != null)
            {
                PreparedReflections.Remove(_camViewpoint.GetHashCode());
            }

            // Cleanup all the objects we possibly have created
            if (_reflectionTexture)
            {
                Destroy(_reflectionTexture);
                _reflectionTexture = null;
            }
            if (_camReflections)
            {
                Destroy(_camReflections.gameObject);
                _camReflections = null;
            }

            RenderPipelineManager.beginCameraRendering -= BeginCameraRendering;
        }
Beispiel #6
0
        // Used by the ocean mask system if we need to render the ocean mask in situations
        // where the ocean itself doesn't need to be rendered or has otherwise been disabled
        internal void BindOceanData(Camera camera)
        {
            _oceanDataHasBeenBound = true;
            if (OceanRenderer.Instance == null || Rend == null)
            {
                return;
            }

            if (Rend.sharedMaterial != OceanRenderer.Instance.OceanMaterial)
            {
                Rend.sharedMaterial = OceanRenderer.Instance.OceanMaterial;
            }

            // per instance data

            if (_mpb == null)
            {
                _mpb = new PropertyWrapperMPB();
            }
            Rend.GetPropertyBlock(_mpb.materialPropertyBlock);

            // Only done here because current camera is defined. This could be done just once, probably on the OnRender function
            // or similar on the OceanPlanarReflection script?
            var reflTex = PreparedReflections.GetRenderTexture(camera.GetHashCode());

            if (reflTex)
            {
                _mpb.SetTexture(sp_ReflectionTex, reflTex);
            }
            else
            {
                _mpb.SetTexture(sp_ReflectionTex, Texture2D.blackTexture);
            }

            Rend.SetPropertyBlock(_mpb.materialPropertyBlock);
        }
Beispiel #7
0
        // Called when visible to a camera
        void OnWillRenderObject()
        {
            if (OceanRenderer.Instance == null || Rend == null)
            {
                return;
            }

            // check if built-in pipeline being used
            if (Camera.current != null)
            {
                _currentCamera = Camera.current;
            }

            // Depth texture is used by ocean shader for transparency/depth fog, and for fading out foam at shoreline.
            _currentCamera.depthTextureMode |= DepthTextureMode.Depth;

            if (Rend.sharedMaterial != OceanRenderer.Instance.OceanMaterial)
            {
                Rend.sharedMaterial = OceanRenderer.Instance.OceanMaterial;
            }

            // per instance data

            if (_mpb == null)
            {
                _mpb = new PropertyWrapperMPB();
            }
            Rend.GetPropertyBlock(_mpb.materialPropertyBlock);

            // blend LOD 0 shape in/out to avoid pop, if the ocean might scale up later (it is smaller than its maximum scale)
            var needToBlendOutShape = _lodIndex == 0 && OceanRenderer.Instance.ScaleCouldIncrease;
            var meshScaleLerp       = needToBlendOutShape ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 0f;

            // blend furthest normals scale in/out to avoid pop, if scale could reduce
            var needToBlendOutNormals = _lodIndex == _totalLodCount - 1 && OceanRenderer.Instance.ScaleCouldDecrease;
            var farNormalsWeight      = needToBlendOutNormals ? OceanRenderer.Instance.ViewerAltitudeLevelAlpha : 1f;

            _mpb.SetVector(sp_InstanceData, new Vector3(meshScaleLerp, farNormalsWeight, _lodIndex));

            // geometry data
            // compute grid size of geometry. take the long way to get there - make sure we land exactly on a power of two
            // and not inherit any of the lossy-ness from lossyScale.
            var scale_pow_2        = OceanRenderer.Instance.CalcLodScale(_lodIndex);
            var gridSizeGeo        = scale_pow_2 / (0.25f * _lodDataResolution / _geoDownSampleFactor);
            var gridSizeLodData    = gridSizeGeo / _geoDownSampleFactor;
            var mul                = 1.875f; // fudge 1
            var pow                = 1.4f;   // fudge 2
            var normalScrollSpeed0 = Mathf.Pow(Mathf.Log(1f + 2f * gridSizeLodData) * mul, pow);
            var normalScrollSpeed1 = Mathf.Pow(Mathf.Log(1f + 4f * gridSizeLodData) * mul, pow);

            _mpb.SetVector(sp_GeomData, new Vector4(gridSizeLodData, gridSizeGeo, normalScrollSpeed0, normalScrollSpeed1));

            // Assign LOD data to ocean shader
            var ldaws     = OceanRenderer.Instance._lodDataAnimWaves;
            var ldsds     = OceanRenderer.Instance._lodDataSeaDepths;
            var ldclip    = OceanRenderer.Instance._lodDataClipSurface;
            var ldfoam    = OceanRenderer.Instance._lodDataFoam;
            var ldflow    = OceanRenderer.Instance._lodDataFlow;
            var ldshadows = OceanRenderer.Instance._lodDataShadow;

            _mpb.SetInt(LodDataMgr.sp_LD_SliceIndex, _lodIndex);
            if (ldaws != null)
            {
                ldaws.BindResultData(_mpb);
            }
            if (ldflow != null)
            {
                ldflow.BindResultData(_mpb);
            }
            else
            {
                LodDataMgrFlow.BindNull(_mpb);
            }
            if (ldfoam != null)
            {
                ldfoam.BindResultData(_mpb);
            }
            else
            {
                LodDataMgrFoam.BindNull(_mpb);
            }
            if (ldsds != null)
            {
                ldsds.BindResultData(_mpb);
            }
            else
            {
                LodDataMgrSeaFloorDepth.BindNull(_mpb);
            }
            if (ldclip != null)
            {
                ldclip.BindResultData(_mpb);
            }
            else
            {
                LodDataMgrClipSurface.BindNull(_mpb);
            }
            if (ldshadows != null)
            {
                ldshadows.BindResultData(_mpb);
            }
            else
            {
                LodDataMgrShadow.BindNull(_mpb);
            }

            var reflTex = PreparedReflections.GetRenderTexture(_currentCamera.GetHashCode());

            if (reflTex)
            {
                _mpb.SetTexture(sp_ReflectionTex, reflTex);
            }
            else
            {
                _mpb.SetTexture(sp_ReflectionTex, Texture2D.blackTexture);
            }

            // Hack - due to SV_IsFrontFace occasionally coming through as true for back faces,
            // add a param here that forces ocean to be in underwater state. I think the root
            // cause here might be imprecision or numerical issues at ocean tile boundaries, although
            // i'm not sure why cracks are not visible in this case.
            var heightOffset = OceanRenderer.Instance.ViewerHeightAboveWater;

            _mpb.SetFloat(sp_ForceUnderwater, heightOffset < -2f ? 1f : 0f);

            Rend.SetPropertyBlock(_mpb.materialPropertyBlock);
        }