public void SetBounds(int nodeId, NodeBounds bounds)
        {
            Node node = m_Nodes[nodeId];

            node.Bounds     = bounds;
            m_Nodes[nodeId] = node;
        }
        public static BuildTerrainJob Create(float extents, float3 cameraPosition, NativeArray <float4> planes, NativeList <RenderNode> renderNodes)
        {
            float2 camPosition = new float2(cameraPosition.x, cameraPosition.z);

            float2 center = default;

            center.x = FloorToMultiple(camPosition.x, 32f);
            center.y = FloorToMultiple(camPosition.y, 32f);

            NodeBounds bounds = new NodeBounds
            {
                min = new float2(center.x - extents, center.y - extents),
                max = new float2(center.x + extents, center.y + extents)
            };

            BuildTerrainJob job = new BuildTerrainJob
            {
                CameraPosition = camPosition,
                Bounds         = bounds,
                Planes         = planes,
                RenderNodes    = renderNodes
            };

            return(job);
        }
Beispiel #3
0
        public bool Intersects(NodeBounds other)
        {
            if (min.x > other.max.x || other.min.x > max.x)
            {
                return(false);
            }

            if (min.y > other.max.y || other.min.y > max.y)
            {
                return(false);
            }

            return(true);
        }
        public int AddNode(NodeBounds bounds)
        {
            Node node = Node.Default;

            node.Bounds = bounds;
            int id = NextNodeId();

            if (id > m_Nodes.Length - 1)
            {
                //Debug.LogFormat("AddNode {0} out of nodes", bounds);
                throw new Exception();
            }
            m_Nodes[id] = node;
            return(id);
        }
        private void FindOverlapping(int nodeId, NodeBounds bounds, NativeList <int> results)
        {
            Node node = m_Nodes[nodeId];

            if (node.Bounds.Intersects(bounds))
            {
                if (node.IsLeaf)
                {
                    results.Add(nodeId);
                }
                else
                {
                    for (int i = 0; i < 4; i++)
                    {
                        FindOverlapping(node.Data[i], bounds, results);
                    }
                }
            }
        }
Beispiel #6
0
        public bool IsRight(NodeBounds bounds, NodeBounds other)
        {
            float2 point = new float2(bounds.max.x + 1f, bounds.Center.y);

            return(other.Contains(point));
        }
Beispiel #7
0
        public bool IsLeft(NodeBounds bounds, NodeBounds other)
        {
            float2 point = new float2(bounds.min.x - 1f, bounds.Center.y);

            return(other.Contains(point));
        }
Beispiel #8
0
        public void GetRenderNodes(QuadTree tree, NativeList <RenderNode> renderNodes, NativeArray <float4> planes)
        {
            NativeList <int> results = new NativeList <int>(Allocator.Temp);

            var nodes = tree.Nodes;

            for (int i = 0; i < nodes.Length; i++)
            {
                Node node = nodes[i];
                if (!node.IsLeaf)
                {
                    continue;
                }

                NodeBounds bounds = node.Bounds;
                bounds.Expand(2f);

                float2 center        = bounds.Center;
                float2 extents       = bounds.Extents;
                Bounds frustumBounds = new Bounds();
                frustumBounds.center  = new Vector3(center.x, 0f, center.y);
                frustumBounds.extents = new Vector3(extents.x, 1000f, extents.y);

                var intersectResult = FrustumPlanes2.Intersect(planes, frustumBounds);
                if (intersectResult == FrustumPlanes2.IntersectResult.Out)
                {
                    continue;
                }


                tree.FindOverlapping(bounds, results);
                NodeSides sides = default;

                for (int j = 0; j < results.Length; j++)
                {
                    int otherId = results[j];
                    if (otherId == i)
                    {
                        continue;
                    }
                    Node other = nodes[otherId];

                    // only smaller neighbors set different edges
                    if (!(node.Bounds.Size.x + 1 < other.Bounds.Size.x))
                    {
                        continue;
                    }


                    if (IsLeft(node.Bounds, other.Bounds))
                    {
                        sides.Left = true;
                    }
                    else if (IsRight(node.Bounds, other.Bounds))
                    {
                        sides.Right = true;
                    }
                    else if (IsForward(node.Bounds, other.Bounds))
                    {
                        sides.Forward = true;
                    }
                    else if (IsBack(node.Bounds, other.Bounds))
                    {
                        sides.Back = true;
                    }
                }
                results.Clear();

                RenderNode renderNode = new RenderNode
                {
                    Node     = node,
                    MeshType = sides.GetMeshType()
                };
                renderNodes.Add(renderNode);
            }
        }
Beispiel #9
0
        public bool IsForward(NodeBounds bounds, NodeBounds other)
        {
            float2 point = new float2(bounds.Center.x, bounds.max.y + 1f);

            return(other.Contains(point));
        }
Beispiel #10
0
        public bool IsBack(NodeBounds bounds, NodeBounds other)
        {
            float2 point = new float2(bounds.Center.x, bounds.min.y - 1f);

            return(other.Contains(point));
        }
 public void FindOverlapping(NodeBounds bounds, NativeList <int> results)
 {
     FindOverlapping(0, bounds, results);
 }
        public void Construct(NodeBounds bounds)
        {
            int rootId = AddNode(bounds);

            BuildNode(rootId);
        }