Exemplo n.º 1
0
        public override void BuildCommandBuffer(OceanRenderer ocean, CommandBuffer buf)
        {
            base.BuildCommandBuffer(ocean, buf);

            // if there is nothing in the scene tagged up for depth rendering, and we have cleared the RTs, then we can early out
            var drawList = RegisterLodDataInputBase.GetRegistrar(GetType());

            if (drawList.Count == 0 && _targetsClear)
            {
                return;
            }

            for (int lodIdx = OceanRenderer.Instance.CurrentLodCount - 1; lodIdx >= 0; lodIdx--)
            {
                buf.SetRenderTarget(_targets, 0, CubemapFace.Unknown, lodIdx);
                buf.ClearRenderTarget(false, true, Color.black);
                buf.SetGlobalInt(sp_LD_SliceIndex, lodIdx);
                SubmitDraws(lodIdx, buf);
            }

            // targets have now been cleared, we can early out next time around
            if (drawList.Count == 0)
            {
                _targetsClear = true;
            }
        }
Exemplo n.º 2
0
        void InitBatches()
        {
            if (_waveShader == null)
            {
                _waveShader = Shader.Find("Crest/Inputs/Animated Waves/Gerstner Batch");
                Debug.Assert(_waveShader, "Could not load Gerstner wave shader, make sure it is packaged in the build.");
                if (_waveShader == null)
                {
                    return;
                }
            }

            _batches = new GerstnerBatch[LodDataMgr.MAX_LOD_COUNT];
            for (int i = 0; i < _batches.Length; i++)
            {
                _batches[i] = new GerstnerBatch(_waveShader, _directTowardsPoint);
            }

            // Submit draws to create the Gerstner waves. LODs from 0 to N-2 render the Gerstner waves from their lod. Additionally, any waves
            // in the biggest lod, or too big for the biggest lod, are rendered into both of the last two LODs N-1 and N-2, as this allows us to
            // move these waves between LODs without pops when the camera changes heights and the LODs need to change scale.

            var registered = RegisterLodDataInputBase.GetRegistrar(typeof(LodDataMgrAnimWaves));

            foreach (var batch in _batches)
            {
                registered.Add(batch);
            }
        }
Exemplo n.º 3
0
        protected void SubmitDrawsFiltered(int lodIdx, CommandBuffer buf, IDrawFilter filter)
        {
            var lt = OceanRenderer.Instance._lodTransform;

            lt._renderData[lodIdx].Validate(0, this);

            lt.SetViewProjectionMatrices(lodIdx, buf);

            var drawList = RegisterLodDataInputBase.GetRegistrar(GetType());

            foreach (var draw in drawList)
            {
                if (!draw.Enabled)
                {
                    continue;
                }

                int   isTransition;
                float weight = filter.Filter(draw, out isTransition);
                if (weight > 0f)
                {
                    draw.Draw(buf, weight, isTransition);
                }
            }
        }
Exemplo n.º 4
0
        public void RemoveDraw(RegisterLodDataInputBase data)
        {
            if (OceanRenderer.Instance == null)
            {
                // Ocean has unloaded, clear out
                _drawList.Clear();
                return;
            }

            _drawList.Remove(data);
        }
Exemplo n.º 5
0
        public override void BuildCommandBuffer(OceanRenderer ocean, CommandBuffer buf)
        {
            base.BuildCommandBuffer(ocean, buf);

            // if there is nothing in the scene tagged up for depth rendering, and we have cleared the RTs, then we can early out
            var drawList = RegisterLodDataInputBase.GetRegistrar(GetType());

            if (drawList.Count == 0 && _targetsClear)
            {
                return;
            }

            if (UseGeometryShader)
            {
                buf.SetRenderTarget(_targets, 0, CubemapFace.Unknown, -1);
                buf.ClearRenderTarget(false, true, Color.white * 1000f);

                Matrix4x4[] matrixArray = new Matrix4x4[MAX_LOD_COUNT];

                var lt = OceanRenderer.Instance._lodTransform;
                for (int lodIdx = OceanRenderer.Instance.CurrentLodCount - 1; lodIdx >= 0; lodIdx--)
                {
                    lt._renderData[lodIdx].Validate(0, this);
                    Matrix4x4 platformProjectionMatrix = GL.GetGPUProjectionMatrix(lt.GetProjectionMatrix(lodIdx), true);
                    Matrix4x4 worldToClipPos           = platformProjectionMatrix * lt.GetWorldToCameraMatrix(lodIdx);
                    matrixArray[lodIdx] = worldToClipPos;
                }

                buf.SetGlobalMatrixArray(sp_SliceViewProjMatrices, matrixArray);
                buf.SetGlobalInt(sp_CurrentLodCount, OceanRenderer.Instance.CurrentLodCount);

                foreach (var draw in drawList)
                {
                    draw.Draw(buf, 1f, 0);
                }
            }
            else
            {
                for (int lodIdx = OceanRenderer.Instance.CurrentLodCount - 1; lodIdx >= 0; lodIdx--)
                {
                    buf.SetRenderTarget(_targets, 0, CubemapFace.Unknown, lodIdx);
                    buf.ClearRenderTarget(false, true, Color.white * 1000f);
                    buf.SetGlobalFloat(OceanRenderer.sp_LD_SliceIndex, lodIdx);
                    SubmitDraws(lodIdx, buf);
                }
            }

            // targets have now been cleared, we can early out next time around
            if (drawList.Count == 0)
            {
                _targetsClear = true;
            }
        }
Exemplo n.º 6
0
        void OnDisable()
        {
            if (_batches != null)
            {
                var registered = RegisterLodDataInputBase.GetRegistrar(typeof(LodDataMgrAnimWaves));
                foreach (var batch in _batches)
                {
                    registered.Remove(batch);
                }

                _batches = null;
            }
        }
Exemplo n.º 7
0
        protected void SubmitDraws(int lodIdx, CommandBuffer buf)
        {
            var lt = OceanRenderer.Instance._lodTransform;

            lt._renderData[lodIdx].Validate(0, this);

            lt.SetViewProjectionMatrices(lodIdx, buf);

            var drawList = RegisterLodDataInputBase.GetRegistrar(GetType());

            foreach (var draw in drawList)
            {
                draw.Draw(buf, 1f, 0);
            }
        }
Exemplo n.º 8
0
        public override void BuildCommandBuffer(OceanRenderer ocean, CommandBuffer buf)
        {
            base.BuildCommandBuffer(ocean, buf);

            // If there is nothing in the scene tagged up for depth rendering, and we have cleared the RTs, then we can early out
            var drawList = RegisterLodDataInputBase.GetRegistrar(GetType());

            if (drawList.Count == 0 && _targetsClear)
            {
                return;
            }

            for (int lodIdx = OceanRenderer.Instance.CurrentLodCount - 1; lodIdx >= 0; lodIdx--)
            {
                buf.SetRenderTarget(_targets, 0, CubemapFace.Unknown, lodIdx);
                var defaultToClip = OceanRenderer.Instance._defaultClippingState == OceanRenderer.DefaultClippingState.EverythingClipped;
                buf.ClearRenderTarget(false, true, defaultToClip ? Color.white : Color.black);
                buf.SetGlobalInt(sp_LD_SliceIndex, lodIdx);
                SubmitDraws(lodIdx, buf);
            }

            // Targets are only clear if nothing was drawn
            _targetsClear = drawList.Count == 0;
        }
 public bool Filter(RegisterLodDataInputBase data)
 {
     return((data as RegisterAnimWavesInput).OctaveWavelength == 0f);
 }
            public bool Filter(RegisterLodDataInputBase data)
            {
                var drawOctaveWavelength = (data as RegisterAnimWavesInput).OctaveWavelength;

                return((_lodMinWavelength <= drawOctaveWavelength) && (drawOctaveWavelength < _lodMaxWavelength || _lodIdx == _lodCount - 1));
            }
Exemplo n.º 11
0
        void InitBatches()
        {
            // Get the wave
            MeshRenderer rend = GetComponent <MeshRenderer>();

            if (_mode == GerstnerMode.Geometry)
            {
                rend.enabled = false;
#if UNITY_EDITOR
                // Cleanup render proxy used for global mode after switching.
                if (_renderProxy != null)
                {
                    DestroyImmediate(_renderProxy);
                }
#endif
            }
            else if (_mode == GerstnerMode.Global)
            {
                // Create render proxy only if we don't already have one.
                if (_renderProxy == null)
                {
                    // Create a proxy MeshRenderer to feed the rendering
                    _renderProxy = GameObject.CreatePrimitive(PrimitiveType.Quad);
#if UNITY_EDITOR
                    DestroyImmediate(_renderProxy.GetComponent <Collider>());
#else
                    Destroy(_renderProxy.GetComponent <Collider>());
#endif
                    _renderProxy.hideFlags        = HideFlags.HideAndDontSave;
                    _renderProxy.transform.parent = transform;
                    rend         = _renderProxy.GetComponent <MeshRenderer>();
                    rend.enabled = false;
                    var waveShader = Shader.Find("Hidden/Crest/Inputs/Animated Waves/Gerstner Batch Global");
                    Debug.Assert(waveShader, "Could not load Gerstner wave shader, make sure it is packaged in the build.");
                    if (waveShader == null)
                    {
                        enabled = false;
                        return;
                    }

                    rend.material = new Material(waveShader);
                }
                else
                {
                    rend = _renderProxy.GetComponent <MeshRenderer>();
                }
            }

            var registered = RegisterLodDataInputBase.GetRegistrar(typeof(LodDataMgrAnimWaves));

#if UNITY_EDITOR
            // Unregister after switching modes in the editor.
            if (_batches != null)
            {
                foreach (var batch in _batches)
                {
                    registered.Remove(batch);
                }
            }
#endif

            _batches = new GerstnerBatch[LodDataMgr.MAX_LOD_COUNT];
            for (int i = 0; i < _batches.Length; i++)
            {
                _batches[i] = new GerstnerBatch(this, i, rend, _directTowardsPoint);
            }

            // Submit draws to create the Gerstner waves. LODs from 0 to N-2 render the Gerstner waves from their lod. Additionally, any waves
            // in the biggest lod, or too big for the biggest lod, are rendered into both of the last two LODs N-1 and N-2, as this allows us to
            // move these waves between LODs without pops when the camera changes heights and the LODs need to change scale.

            foreach (var batch in _batches)
            {
                registered.Add(0, batch);
            }
        }
        void InitBatches()
        {
            // Get the wave
            MeshRenderer rend = null;

            if (_mode == GerstnerMode.Geometry)
            {
                rend = GetComponent <MeshRenderer>();

                if (!rend)
                {
                    Debug.LogError($"Gerstner input '{gameObject.name}' has Mode set to Geometry, but no MeshRenderer component is attached. Please attach a MeshRenderer to provide the geometry for rendering the Gerstner waves.", this);
                    enabled = false;
                    return;
                }
                if (!rend.sharedMaterial)
                {
                    Debug.LogError($"Gerstner input '{gameObject.name}' has Mode set to Geometry, but the geometry has no material assigned. Please assign a material that uses one of the Gerstner input shaders.", this);
                    enabled = false;
                    return;
                }

                rend.enabled = false;
            }
            else if (_mode == GerstnerMode.Global)
            {
                if (GetComponent <MeshRenderer>() != null)
                {
                    Debug.LogWarning($"Gerstner input '{gameObject.name}' has MeshRenderer component that will be ignored because the Mode is set to Global.", this);
                }

                // Create a proxy MeshRenderer to feed the rendering
                var renderProxy = GameObject.CreatePrimitive(PrimitiveType.Quad);
                Destroy(renderProxy.GetComponent <Collider>());
                renderProxy.hideFlags        = HideFlags.HideAndDontSave;
                renderProxy.transform.parent = transform;
                rend         = renderProxy.GetComponent <MeshRenderer>();
                rend.enabled = false;

                var waveShader = Shader.Find("Hidden/Crest/Inputs/Animated Waves/Gerstner Batch Global");
                Debug.Assert(waveShader, "Could not load Gerstner wave shader, make sure it is packaged in the build.");
                if (waveShader == null)
                {
                    enabled = false;
                    return;
                }

                rend.material = new Material(waveShader);
            }

            _batches = new GerstnerBatch[LodDataMgr.MAX_LOD_COUNT];
            for (int i = 0; i < _batches.Length; i++)
            {
                _batches[i] = new GerstnerBatch(rend, _directTowardsPoint);
            }

            // Submit draws to create the Gerstner waves. LODs from 0 to N-2 render the Gerstner waves from their lod. Additionally, any waves
            // in the biggest lod, or too big for the biggest lod, are rendered into both of the last two LODs N-1 and N-2, as this allows us to
            // move these waves between LODs without pops when the camera changes heights and the LODs need to change scale.

            var registered = RegisterLodDataInputBase.GetRegistrar(typeof(LodDataMgrAnimWaves));

            foreach (var batch in _batches)
            {
                registered.Add(0, batch);
            }
        }