// The command buffer populates the LODs with ocean depth data. It submits any objects with a RenderOceanDepth component attached.
        // It's stateless - the textures don't have to be managed across frames/scale changes
        void UpdateCmdBufOceanFloorDepth()
        {
            // if there is nothing in the scene tagged up for depth rendering then there is no depth rendering required
            if (_depthRenderers.Count < 1)
            {
                if (_bufOceanDepth != null)
                {
                    _bufOceanDepth.Clear();
                }

                return;
            }

            if (_bufOceanDepth == null)
            {
                _bufOceanDepth = new CommandBuffer();
                Cam.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, _bufOceanDepth);
                _bufOceanDepth.name = "Ocean Depth";
            }

            _bufOceanDepth.Clear();

            _bufOceanDepth.SetRenderTarget(_rtOceanDepth);
            _bufOceanDepth.ClearRenderTarget(false, true, Color.black);

            foreach (var entry in _depthRenderers)
            {
                _bufOceanDepth.DrawRenderer(entry.Key, entry.Value);
            }
        }
        // The command buffer populates the LODs with ocean depth data. It submits any objects with a RenderOceanDepth component attached.
        // It's stateless - the textures don't have to be managed across frames/scale changes
        void UpdateCmdBufOceanFloorDepth()
        {
            var objs = FindObjectsOfType <RenderOceanDepth>();

            // if there is nothing in the scene tagged up for depth rendering then there is no depth rendering required
            if (objs.Length < 1)
            {
                if (_bufOceanDepth != null)
                {
                    _bufOceanDepth.Clear();
                }

                return;
            }

            if (_bufOceanDepth == null)
            {
                _bufOceanDepth = new CommandBuffer();
                Cam.AddCommandBuffer(CameraEvent.BeforeForwardOpaque, _bufOceanDepth);
                _bufOceanDepth.name = "Ocean Depth";
            }

            _bufOceanDepth.Clear();

            _bufOceanDepth.SetRenderTarget(_rtOceanDepth);
            _bufOceanDepth.ClearRenderTarget(false, true, Color.red * 10000f);

            foreach (var obj in objs)
            {
                if (!obj.enabled)
                {
                    continue;
                }

                var r = obj.GetComponent <Renderer>();
                if (r == null)
                {
                    Debug.LogError("GameObject '" + obj.gameObject.name + "' must have a renderer component attached. Unity Terrain objects are not supported - these must be captured by an Ocean Depth Cache.", obj);
                }
                else if (obj.transform.parent != null && obj.transform.parent.GetComponent <OceanDepthCache>() != null)
                {
                    _bufOceanDepth.DrawRenderer(r, r.material);
                }
                else
                {
                    _bufOceanDepth.DrawRenderer(r, _matOceanDepth);
                }
            }
        }
        protected override void LateUpdate()
        {
            base.LateUpdate();

            if (_advanceSimCmdBuf == null)
            {
                _advanceSimCmdBuf      = new CommandBuffer();
                _advanceSimCmdBuf.name = "AdvanceSim_" + SimName;
                Cam.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, _advanceSimCmdBuf);
                _advanceSimCmdBuf.DrawRenderer(GetComponentInChildren <MeshRenderer>(), _renderSimMaterial);
            }

            float dt = SimDeltaTime;

            _renderSimMaterial.SetFloat("_SimDeltaTime", dt);
            _renderSimMaterial.SetFloat("_SimDeltaTimePrev", _simDeltaTimePrev);
            _simDeltaTimePrev = dt;

            // compute which lod data we are sampling source data from. if a scale change has happened this can be any lod up or down the chain.
            float oceanLocalScale = OceanRenderer.Instance.transform.localScale.x;

            if (_oceanLocalScalePrev == -1f)
            {
                _oceanLocalScalePrev = oceanLocalScale;
            }
            float ratio = oceanLocalScale / _oceanLocalScalePrev;

            _oceanLocalScalePrev = oceanLocalScale;
            float ratio_l2 = Mathf.Log(ratio) / Mathf.Log(2f);
            int   delta    = Mathf.RoundToInt(ratio_l2);

            int srcDataIdx = LodTransform.LodIndex + delta;

            if (srcDataIdx >= 0 && srcDataIdx < SimCameras.Length)
            {
                // bind data to slot 0 - previous frame data
                SimCameras[srcDataIdx].GetComponent <LodDataPersistent>().BindSourceData(0, _renderSimMaterial, false);
            }
            else
            {
                // no source data - bind params only
                BindSourceData(0, _renderSimMaterial, true);
            }

            SetAdditionalSimParams(_renderSimMaterial);

            LateUpdateInternal();
        }
示例#4
0
        void LateUpdate()
        {
            if (_advanceSimCmdBuf == null)
            {
                _advanceSimCmdBuf      = new CommandBuffer();
                _advanceSimCmdBuf.name = "AdvanceSim_" + SimName;
                Cam.AddCommandBuffer(CameraEvent.BeforeForwardAlpha, _advanceSimCmdBuf);
                _advanceSimCmdBuf.DrawRenderer(GetComponentInChildren <MeshRenderer>(), _renderSimMaterial);
            }


            if (_copySimResultsCmdBuf == null)
            {
                _copySimResultsCmdBuf      = new CommandBuffer();
                _copySimResultsCmdBuf.name = "CopySimResults_" + SimName;
            }

            int lodIndex = OceanRenderer.Instance.GetLodIndex(_resolution);

            // is the lod for the sim target resolution currently rendering?
            if (lodIndex == -1)
            {
                // no - clear the copy sim results command buffer
                if (_copySimResultsCmdBuf != null)
                {
                    _copySimResultsCmdBuf.Clear();
                }

                // unassign from any camera if it is assigned
                if (_bufAssignedCamIdx != -1)
                {
                    OceanRenderer.Instance.Builder._shapeCameras[_bufAssignedCamIdx].RemoveCommandBuffer(CameraEvent.AfterForwardAlpha, _copySimResultsCmdBuf);
                    _bufAssignedCamIdx = -1;
                }

                // clear the simulation data - so that it doesnt suddenly pop in later
                Graphics.Blit(Texture2D.blackTexture, PPRTs.Source, _matClearSim);
                Graphics.Blit(Texture2D.blackTexture, PPRTs.Target, _matClearSim);

                return;
            }

            // now make sure the command buffer is assigned to the correct camera
            if (_bufAssignedCamIdx != lodIndex)
            {
                if (_bufAssignedCamIdx != -1)
                {
                    OceanRenderer.Instance.Builder._shapeCameras[_bufAssignedCamIdx].RemoveCommandBuffer(CameraEvent.AfterForwardAlpha, _copySimResultsCmdBuf);
                }

                OceanRenderer.Instance.Builder._shapeCameras[lodIndex].AddCommandBuffer(CameraEvent.AfterForwardAlpha, _copySimResultsCmdBuf);
                _bufAssignedCamIdx = lodIndex;
            }

            var lodCam = OceanRenderer.Instance.Builder._shapeCameras[lodIndex];
            var wdc    = OceanRenderer.Instance.Builder._shapeWDCs[lodIndex];

            transform.position = lodCam.transform.position;

            Cam.orthographicSize = lodCam.orthographicSize;
            transform.localScale = (Vector3.right + Vector3.up) * Cam.orthographicSize * 2f + Vector3.forward;

            Cam.projectionMatrix = lodCam.projectionMatrix;

            Vector3 posDelta = wdc._renderData._posSnapped - _camPosSnappedLast;

            _renderSimMaterial.SetVector("_CameraPositionDelta", posDelta);
            _camPosSnappedLast = wdc._renderData._posSnapped;

            float dt = SimDeltaTime;

            _renderSimMaterial.SetFloat("_SimDeltaTime", dt);
            _renderSimMaterial.SetFloat("_SimDeltaTimePrev", _simDeltaTimePrev);
            if (!OceanRenderer.Instance._freezeTime)
            {
                _simDeltaTimePrev = dt;
            }

            _renderSimMaterial.SetTexture("_SimDataLastFrame", PPRTs.Source);
            wdc.ApplyMaterialParams(0, new PropertyWrapperMaterial(_renderSimMaterial));

            SetAdditionalSimParams(_renderSimMaterial);

            if (_copySimMaterial)
            {
                _copySimMaterial.mainTexture = PPRTs.Target;

                _copySimResultsCmdBuf.Clear();
                _copySimResultsCmdBuf.Blit(PPRTs.Target, lodCam.targetTexture, _copySimMaterial);
            }

            AddPostRenderCommands(_copySimResultsCmdBuf);
        }