Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by revird · Apr 06, 2019 at 12:01 PM · quaddatastructure

Looking for some help with a QuadTree implementation.

I will preface this by saying I know there are hundreds of tutorials out there on quadtrees etc but I wanted to try to create my own as I am an amateur looking to learn random things and struggle to learn without attempting my own implementations then using references to improve then.

Anyway, I am trying to implement a quadtree that starts with a triangle: ![alt text

the main reason is for LOD based updating. However, with my current implementation I have some issues. Predominately with removal of subtriangles.

Here are the two parts of the code necessary for it to work:

Testing of nodes and the node definitions

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 //Monobeviour testing Nodes.
 public class QuadTreeMWE : MonoBehaviour
 {
     NodeMWE treeRoot;
     Mesh mesh;
     Vector3 currentPoint;
     int zDepth = 10;
     void Start()
     {
         mesh = GetComponent<MeshFilter>().mesh = new Mesh();
 
         Vector3 testa = new Vector3(5, 5, 0);
         Vector3 testb = new Vector3(0, 5, 0);
         Vector3 testc = new Vector3(5, 0, 0);
 
         treeRoot = new NodeMWE(testa, testb, testc);
         mesh.vertices = treeRoot.GetVertices().ToArray();
         mesh.triangles = treeRoot.GetTris();  
     }
 
     int tempVertLength = 0;
     void Update()
     {
         Vector3 mousePos = Input.mousePosition;
         currentPoint = Camera.main.ScreenToWorldPoint(new Vector3(mousePos.x, mousePos.y, 10));
         currentPoint.z = zDepth;
         treeRoot.UpdateDistance(currentPoint);
         if(Input.GetKeyDown(KeyCode.Minus))
         {
             zDepth--;
         }
         if(Input.GetKeyDown(KeyCode.Plus))
         {
             zDepth++;
         }
         mesh.vertices = treeRoot.GetVertices().ToArray();
         mesh.triangles = treeRoot.GetTris();  
     }
 }
 
 
 public class NodeMWE
 {
     int depth;
     List<int> subTris;
     //Holds Boundary/distance(ish) data for node
     public TriangleMWE tri;
     //Recuresively holds all vertices
     private List<Vector3> vertices;
     //Children
     NodeMWE[] leaves;
     bool isEndNode;
 
     //Base Leaf Declaration
     public NodeMWE(Vector3 a, Vector3 b, Vector3 c)
     {
         vertices = new List<Vector3>{a, b, c};
         tri = new TriangleMWE(new Vector3[]{vertices[0], vertices[1], vertices[2]}, new int[] {2, 1, 0});
         isEndNode = true;
         depth = 0;
     }
 
     //Non-Base Leafs
     public NodeMWE(int a, int b, int c, List<Vector3> verts, int dpth)
     {
         depth = dpth;
         vertices = verts;
         tri = new TriangleMWE(new Vector3[]{vertices[a], vertices[b], vertices[c]}, new int[] {a, b, c});
         isEndNode = true;
     }
 
     //Add or remove nodes based on distance from a point
     public void UpdateDistance(Vector3 point)
     {
         float camDistance = tri.GetDistanceFromPointFactor(point);           
         if(camDistance > 5 || depth >= MeshParameters.maxDepth) { 
             if(isEndNode)
             {
                 //Remove();
             }
             else
             {
                 foreach(NodeMWE leaf in leaves)
                 {
                     leaf.UpdateDistance(point);
                 }
             }
             return;
         }
         if(isEndNode)
         {
             Add();
         }
         else
         {
             foreach(NodeMWE leaf in leaves)
             {
                 leaf.UpdateDistance(point);
             }
         }
     }
 
     //Subdivides Node/creates Children
     public void Add()
     {
         isEndNode = false;
         subTris = new List<int>();
         leaves = new NodeMWE[4];
         int nextDepth = depth+1;
             
         int vOffset = vertices.Count;
         int[] tris =  tri.GetTris();
         Vector3 a = MeshHelpersMWE.FindCenterPoint(vertices[tris[0]], vertices[tris[1]]);
         Vector3 b = MeshHelpersMWE.FindCenterPoint(vertices[tris[1]], vertices[tris[2]]);
         Vector3 c = MeshHelpersMWE.FindCenterPoint(vertices[tris[2]], vertices[tris[0]]);
         vertices.Add(a);
         vertices.Add(b);
         vertices.Add(c);
         leaves[0] = new NodeMWE(tris[0], vOffset, vOffset+2, vertices, nextDepth);
         leaves[1] = new NodeMWE(vOffset, vOffset+1, vOffset+2, vertices, nextDepth);
         leaves[2] = new NodeMWE(vOffset+2, vOffset+1, tris[2], vertices, nextDepth);              
         leaves[3] = new NodeMWE(tris[1], vOffset+1, vOffset, vertices,nextDepth);
     }
 
     //UnSubs Node/removes Children
     public bool Remove()
     {
         if(isEndNode)
         {
             return true;
         }
         else {
             foreach(NodeMWE leaf in leaves)
             {
                 if(leaf.Remove())
                 {
                     leaves = null;
                     isEndNode = true;
                     int[] verticesToRemove = leaf.tri.GetTris();
                     //vertices.RemoveAt(verticesToRemove[0]);
                     break;
                 }
             }
             return false;
         }
     }
 
     //Get triangles to draw
     public int[] GetTris()
     {
         if(isEndNode) { return tri.GetTris(); }
         else{
             subTris.Clear();
             foreach(NodeMWE leaf in leaves)
             {
                 subTris.AddRange(leaf.GetTris());
             }
         }
         return subTris.ToArray();
     }
 
     //Get Vertices to draw
     public List<Vector3> GetVertices()
     {
         return vertices;
     }
 }
 
 public static class MeshParametersMWE
 {
     public static readonly int maxDepth = 3;
 }
 
 
 public static class MeshHelpersMWE
 {
     public static Vector3 FindCenterPoint(Vector3 a, Vector3 b)
     {
         float aPoint = (a.x + b.x)/2;
         float bPoint = (a.y + b.y)/2;
         float cPoint = (a.z + b.z)/2;
         return new Vector3(aPoint, bPoint, cPoint);
     }
 }
 
 

Triangles used within Nodes: using System.Collections; using System.Collections.Generic; using UnityEngine;

 public class TriangleMWE
 {
     Vector3[] vPoints;
     Vector3 normal;
     int[] tPoints;
     Vector3 centerPoint;
     float longestDistance;
 
     public TriangleMWE(Vector3[] vecPoints, int[] triPoints)
     {
         vPoints = vecPoints;
         tPoints = triPoints;
         longestDistance = 0;
         normal = Vector3.Cross(vPoints[1]-vPoints[0], vPoints[0]-vPoints[2]);
         centerPoint = (vPoints[0] + vPoints[1] + vPoints[2])/3;
         normal = Vector3.Normalize(normal);
         foreach(Vector3 vec in vecPoints)
         {
             float vecDis = GetVecDistances(vec);
             if(vecDis > longestDistance) { longestDistance = vecDis; }
         }
     }
 
     public float GetVecDistances(Vector3 refPoint){
         Vector3 s0 = refPoint - centerPoint;
         s0.x = Mathf.Pow(s0.x, 2);
         s0.y = Mathf.Pow(s0.y, 2);
         s0.z = Mathf.Pow(s0.z, 2);
         return Mathf.Sqrt(s0.x + s0.y + s0.z);
    }
    
    public float GetDistanceFromPointFactor(Vector3 refPoint){
         Vector3 s0 = refPoint - centerPoint;
         s0.x = Mathf.Pow(s0.x, 2);
         s0.y = Mathf.Pow(s0.y, 2);
         s0.z = Mathf.Pow(s0.z, 2);
         return Mathf.Sqrt(s0.x + s0.y + s0.z)/longestDistance;
    }
 
    public int[] GetTris()
    {
        return tPoints;
    }
 }
 

Apologies for poor variable naming etc, hopefully someone can make sense of what I am trying to do here and tell me if it is as entirely idiotic as I think it is.

Now, trying to remove children causes issues because if I remove the vertices and the triangles - some vertices are shared and the triangles arrays all mess up because they are currently pointing at certain vertice indexes. However, if vertices are removed low down in the vertex array then all the other triangle arrays point to the wrong vertices. Any advice on how to perform this remove would be greatly appreciated as I have spent hours trying to work it out myself.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

102 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity & QuadBuffer 3 Answers

What`s more expensive GUI Texures or Quads for displaying units health?(RTS) 1 Answer

Stretch a Quad from one object to another 2 Answers

Unable to apply texture to Quad 0 Answers

Vertex ordering problem with GLSL Tessellation when using quads in Tessellation Evaluation Shader stage. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges