Ejemplo n.º 1
0
        void RenderWorldSingle()
        {
            lock (ChunkEventManager.viewHashSet)
            {
                foreach (var manager in ChunkEventManager.viewHashSet)
                {
                    while (manager.GetBucket(out var bucket))
                    {
                        viewThread.Enqueue(bucket);
                    }
                }
            }
            foreach (var manager in ChunkEventManager.coroutineHashSet)
            {
                while (manager.GetBucket(out var bucket))
                {
                    coroutineQueue.Enqueue(bucket);
                }
            }

            int minX = 0;
            int maxX = 0;
            int minY = 0;
            int maxY = 0;

            // Ordering: [0] = Left, [1] = Right, [2] = Down, [3] = Up, [4] = Near, [5] = Far
            GeometryUtility.CalculateFrustumPlanes(View.setup._camera, planes);
            float enter;

            UnityEngine.Profiling.Profiler.BeginSample("Calc planes");
            var depth = View.setup.viewSize.z * 16;

            RectInt[] zz = new RectInt[depth];
            for (int z = 0; z < depth; z++)
            {
                var calcPos = new Vector3(View.setup._camera.transform.position.x, View.setup._camera.transform.position.y, z);
                // left
                var ray = new Ray(calcPos, Vector3.left);
                if (planes[0].Raycast(ray, out enter))
                {
                    var point = ray.GetPoint(enter).Floor();
                    if (planes[0].GetSide(point + new Vector3(0.5f, 0, -0.5f)))
                    {
                        minX = point.x - 1;
                    }
                    else
                    {
                        minX = point.x - 1;
                    }
                }
                // right
                ray = new Ray(calcPos, Vector3.right);
                if (planes[1].Raycast(ray, out enter))
                {
                    var point = ray.GetPoint(enter).Floor();
                    if (planes[1].GetSide(point + new Vector3(-0.5f, 0, -0.5f)))
                    {
                        maxX = point.x + 1;
                    }
                    else
                    {
                        maxX = point.x + 1;
                    }
                }
                // bottom
                ray = new Ray(calcPos, Vector3.down);
                if (planes[2].Raycast(ray, out enter))
                {
                    var point = ray.GetPoint(enter).Floor();
                    if (planes[2].GetSide(point + new Vector3(0, 0.5f, -0.5f)))
                    {
                        minY = point.y - 1;
                    }
                    else
                    {
                        minY = point.y - 1;
                    }
                }
                // top
                ray = new Ray(calcPos, Vector3.up);
                if (planes[3].Raycast(ray, out enter))
                {
                    var point = ray.GetPoint(enter).Floor();
                    if (planes[3].GetSide(point + new Vector3(0, -0.5f, -0.5f)))
                    {
                        maxY = point.y + 1;
                    }
                    else
                    {
                        maxY = point.y + 1;
                    }
                }
                zz[z] = new RectInt(minX, minY, maxX - minX, maxY - minY);
            }
            UnityEngine.Profiling.Profiler.EndSample();

            var hp = new Plane(Vector3.up, View.setup._camera.transform.position);
            var vp = new Plane(Vector3.right, View.setup._camera.transform.position);

            var playerPos     = new Vector3(View.setup._camera.transform.position.x, View.setup._camera.transform.position.y, 0);
            var queue         = new Queue <RenderChunk>();
            var startChunkPos = BlockPos.From(Vector3Int.FloorToInt(playerPos));

            minX = (startChunkPos.x >> 4) - View.setup.viewSize.x;
            maxX = (startChunkPos.x >> 4) + View.setup.viewSize.x;
            minY = (startChunkPos.y >> 4) - View.setup.viewSize.y;
            maxY = (startChunkPos.y >> 4) + View.setup.viewSize.y;
            var chunkPos = new BlockPos();

            for (int x = minX; x <= maxX; x++)
            {
                for (int y = minY; y <= maxY; y++)
                {
                    chunkPos.SetChunk(x, y, 0);
                    var renderChunk = renderProvider.GetChunk(chunkPos);
                    if (GeometryUtility.TestPlanesAABB(planes, renderChunk.bounds))
                    {
                        renderChunk.SetFrameIndex(Time.frameCount);
                        queue.Enqueue(renderChunk);
                    }
                    else
                    {
                        renderChunk.Update();
                    }
                }
            }

            while (queue.Count > 0 && queue.Count < 100)
            {
                var renderChunk = queue.Dequeue();
                renderChunk.Update();
                if (renderChunk.empty == 4096)
                {
                    DrawBounds(renderChunk.bounds, Color.white);
                }
                else
                {
                    DrawBounds(renderChunk.bounds, Color.red);
                }
                Graphics.DrawMesh(renderChunk.mesh, Matrix4x4.identity, material2, 0);

                for (Facing facing = Facing.First; facing <= Facing.Last; facing++)
                {
                    chunkPos.Set(renderChunk.chunk.position);
                    chunkPos.AddChunk(facing.GetVector());
                    var renderChunkOffset = renderProvider.GetChunk(startChunkPos, chunkPos);
                    if (renderChunkOffset != null)
                    {
                        renderChunkOffset.Update();
                        if (renderChunkOffset != null && GeometryUtility.TestPlanesAABB(planes, renderChunkOffset.bounds) && renderChunk.IsVisible(facing) && renderChunkOffset.SetFrameIndex(Time.frameCount))
                        {
                            queue.Enqueue(renderChunkOffset);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public override void Execute()
        {
            vertices.Clear();
            triangles.Clear();
            uv.Clear();

            var min = chunk.position.min;
            var max = chunk.position.max;

            for (int z = min.z; z <= max.z; z++)
            {
                for (int x = min.x; x <= max.x; x++)
                {
                    for (int y = min.y; y <= max.y; y++)
                    {
                        pos.Set(x, y, z);
                        var index = pos.GetIndex();
                        if (render.data[index])
                        {
                            byte set = 0;
                            for (Facing facing = Facing.First; facing <= Facing.Last; facing++)
                            {
                                if (facing == Facing.North)
                                {
                                    continue;
                                }

                                offset.Set(pos);
                                offset.Add(facing.GetVector());

                                if (offset.x < min.x || offset.x > max.x || offset.y < min.y || offset.y > max.y || offset.z < min.z || offset.z > max.z)
                                {
                                    if (offset.z < 0)
                                    {
                                        set |= (byte)(1 << (int)facing);
                                    }
                                    else
                                    {
                                        if (world.GetBlockData(offset).IsEmpty())
                                        {
                                            set |= (byte)(1 << (int)facing);
                                        }
                                    }
                                }
                                else
                                {
                                    if (!render.data[index + DF[(int)facing]])
                                    {
                                        set |= (byte)(1 << (int)facing);
                                    }
                                }
                            }
                            AddBlock(pos, set);
                        }
                    }
                }
            }

            var e = render.calcMeshProvider.Create();

            e.render = render;

            var v = e.vertices;

            e.vertices = vertices;
            vertices   = v;

            var t = e.triangles;

            e.triangles = triangles;
            triangles   = t;

            var u = e.uv;

            e.uv = uv;
            uv   = u;

            e.Publish();
        }