Esempio n. 1
0
        public void Draw(Camera camera)
        {
            if (CanDraw())
            {
                var lodLevelCount = Mathf.Min(MaxLodLevelCount, _renderer.LodLevelCount);
                for (int i = 0; i < lodLevelCount; ++i)
                {
                    var lodRenderer = _renderer.GetLodRenderer(i);
                    var count       = lodRenderer.Renderers.Length;

                    int drawCallOffset = 0;
                    for (int j = 0; j < count; ++j)
                    {
                        var renderer = lodRenderer.Renderers[j];
                        if (renderer == null)
                        {
                            continue;
                        }

                        for (int k = 0; k < renderer.SubMeshCount; ++k)
                        {
                            if (sorted)
                            {
                                _argsData[i].SetData(args);
                            }
                            else
                            {
                                ComputeBuffer.CopyCount(_drawInstanceDataForLod[i], _argsData[i],
                                                        drawCallOffset + Constants.StrideSizeUint);
//                                _argsData[i].GetData(args);
//                                _argsData[i].SetData(args);
                            }

                            _mbp.SetBuffer(Constants.MeshVariable.DrawInstanceData, _drawInstanceDataForLod[i]);
                            SetMaterialPropertyBlock();

                            if (SharedConfig.GrassQueue != -1)
                            {
                                renderer.Materials[k].renderQueue = SharedConfig.GrassQueue;
                            }

                            Graphics.DrawMeshInstancedIndirect(
                                renderer.Mesh,
                                k,
                                renderer.Materials[k],
                                testBounds,
                                _argsData[i],
                                drawCallOffset,
                                _mbp,
                                _castShadow,
                                _receiveShadow,
                                Layer,
                                camera);

                            drawCallOffset += ArgCountPerDraw * sizeof(uint);
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        internal InstancingDraw(InstancingRenderer renderer, ComputeShader visShader, ComputeShader sortShader)
        {
            _renderer   = renderer;
            _visShader  = visShader;
            _sortShader = sortShader;

            SetShadow(renderer.CastShadow, renderer.ReceiveShadow);
            _drawInstanceDataForLod = new ComputeBuffer[MaxLodLevelCount];

            // assume 20 drawcalls at most for each lod level
            var argsArray = new int[ArgCountPerDraw * 20];

            for (int i = 0; i < argsArray.Length; ++i)
            {
                argsArray[i] = 0;
            }

            var lodLevelCount = renderer.LodLevelCount;

            _argsData = new ComputeBuffer[lodLevelCount];

            for (int i = 0; i < lodLevelCount; ++i)
            {
                var lodRenderer = renderer.GetLodRenderer(i);
                var count       = lodRenderer.Renderers.Length;

                int drawCallIndex = 0;
                for (int j = 0; j < count; ++j)
                {
                    if (lodRenderer.Renderers[j] == null)
                    {
                        continue;
                    }

                    for (int k = 0; k < lodRenderer.Renderers[j].SubMeshCount; ++k)
                    {
                        argsArray[drawCallIndex] = lodRenderer.Renderers[j].IndexCount[k];
                        drawCallIndex           += ArgCountPerDraw;
                    }
                }

                if (drawCallIndex != 0)
                {
                    _argsData[i] = new ComputeBuffer(1, drawCallIndex * sizeof(uint), ComputeBufferType.IndirectArguments);
                    _argsData[i].SetData(argsArray);
                }
            }

            if (renderer.LodRatios != null)
            {
                _lodSize   = renderer.LodSize;
                _lodRatios = renderer.LodRatios;
            }
        }
Esempio n. 3
0
        internal InstancingDraw(InstancingRenderer renderer, ComputeShader visShader)
        {
            _renderer  = renderer;
            _visShader = visShader;

            SetShadow(ShadowCastingMode.Off, false);
            _drawInstanceData = new ComputeBuffer[MaxLodLevelCount];

            // index count per instance, instance count, start index location, base vertex location, start instance location.
            var argsArray = new int[ArgCountPerDraw * 20];

            for (int i = 0; i < argsArray.Length; ++i)
            {
                argsArray[i] = 0;
            }

            var lodLevelCount = renderer.LodLevelCount;

            _argsData = new ComputeBuffer[lodLevelCount];

            for (int i = 0; i < lodLevelCount; ++i)
            {
                var lodRenderer = renderer.GetLodRenderer(i);
                var count       = lodRenderer.Renderers.Length;

                int drawCallIndex = 0;
                for (int j = 0; j < count; ++j)
                {
                    for (int k = 0; k < lodRenderer.Renderers[j].SubMeshCount; ++k)
                    {
                        argsArray[drawCallIndex] = lodRenderer.Renderers[j].IndexCount[k];
                        drawCallIndex           += ArgCountPerDraw;
                    }
                }
                _argsData[i] = new ComputeBuffer(1, drawCallIndex * sizeof(uint), ComputeBufferType.IndirectArguments);
                _argsData[i].SetData(argsArray);
            }

            if (_lodRatios != null)
            {
                _lodSize   = renderer.LodSize;
                _lodRatios = renderer.LodRatios;
            }
        }
Esempio n. 4
0
        public void Draw(Camera camera)
        {
            if (CanDraw())
            {
                var lodLevelCount = Mathf.Min(MaxLodLevelCount, _renderer.LodLevelCount);
                for (int i = 0; i < lodLevelCount; ++i)
                {
                    var lodRenderer = _renderer.GetLodRenderer(i);
                    var count       = lodRenderer.Renderers.Length;

                    int drawCallOffset = 0;
                    for (int j = 0; j < count; ++j)
                    {
                        var renderer = lodRenderer.Renderers[j];

                        for (int k = 0; k < renderer.SubMeshCount; ++k)
                        {
                            ComputeBuffer.CopyCount(_drawInstanceData[i], _argsData[i], drawCallOffset + Constants.StrideSizeUint);

                            _mbp.SetBuffer(Constants.MeshVariable.DrawInstanceData, _drawInstanceData[i]);
                            SetMaterialPropertyBlock();

                            Graphics.DrawMeshInstancedIndirect(
                                renderer.Mesh,
                                k,
                                renderer.Materials[k],
                                testBounds,
                                _argsData[0],
                                drawCallOffset,
                                _mbp,
                                _castShadow,
                                _receiveShadow,
                                Layer,
                                camera);

                            drawCallOffset += ArgCountPerDraw * sizeof(uint);
                        }
                    }
                }
            }
        }