public void SplitTriangle(int count, int splitAmount, ref int vertexCount) { if (count < splitAmount) { /// replace triangle by 4 triangles int a = GetMiddlePoint(tri.x, tri.y, ref vertexCount); int b = GetMiddlePoint(tri.y, tri.z, ref vertexCount); int c = GetMiddlePoint(tri.z, tri.x, ref vertexCount); PlanetTriangle t1 = new PlanetTriangle(planet, new Vector3Int(tri.x, a, c), vertices, depth + 1, middlePointIndexTable); t1.SplitTriangle(count + 1, splitAmount, ref vertexCount); PlanetTriangle t2 = new PlanetTriangle(planet, new Vector3Int(tri.y, b, a), vertices, depth + 1, middlePointIndexTable); t2.SplitTriangle(count + 1, splitAmount, ref vertexCount); PlanetTriangle t3 = new PlanetTriangle(planet, new Vector3Int(tri.z, c, b), vertices, depth + 1, middlePointIndexTable); t3.SplitTriangle(count + 1, splitAmount, ref vertexCount); PlanetTriangle t4 = new PlanetTriangle(planet, new Vector3Int(a, b, c), vertices, depth + 1, middlePointIndexTable); t4.SplitTriangle(count + 1, splitAmount, ref vertexCount); t4.AddNeighborsTwoWay(t1, t2, t3); t1.AddNeighborsTwoWay(t2, t3); t2.AddNeighborsTwoWay(t3); AddChildren(t1, t2, t3, t4); } }
public void AddNeighbor(PlanetTriangle neighbor) { //neighbors[neighborCount] = Neighbor; neighbors.Add(neighbor); neighborCount++; totalNeighborCount++; }
public float DistanceToTarget(PlanetTriangle from, TriangleInfo to) { float distance = Vector3.Distance(from.RotatedMiddlePointOfTriangle + transform.position, to.position); float angle3 = Mathf.Acos(1 - Mathf.Pow(distance, 2) / (2 * Mathf.Pow(radius, 2))); float factor2 = angle3 / (Mathf.PI * 2); float circumference = 2 * Mathf.PI * radius; return(circumference * factor2); }
public bool CanEnterTriangleFrom(PlanetTriangle t) { IList <int> entrances = GetNeighborVertices(t); bool found = false; for (int i = 0; i < entrances.Count && !found; i++) { found = vertices[entrances[i]].surface.canMoveOnTerrain; } return(found); }
public bool GetPlanetTriangleFor(Vector3 from, out PlanetTriangle tri) { RaycastHit hit; tri = null; Vector3 dir = transform.position - from; //Debug.DrawRay(from + (-dir.normalized * 5), dir, Color.red, 1); if (Physics.Raycast(from + (-dir.normalized * 5), dir, out hit, dir.magnitude, 1 << 12)) { tri = GetTriangleWithData(new TriangleInfo(hit)); } return(tri != null); }
public bool IsNeighborWith(PlanetTriangle t) { int[] tris = t.TriIndizes; int count = 0; for (int i = 0; i < tris.Length && count < SAME_INDEX_COUNT_EXPECTED_FOR_NEIGHBORS; i++) { if (tris[i] == tri.x || tris[i] == tri.y || tris[i] == tri.z) { count++; } } return(count >= SAME_INDEX_COUNT_EXPECTED_FOR_NEIGHBORS); }
public PlanetTriangle ClosestFromNeighboursAt(Vector3 point) { PlanetTriangle result = this; float distance = (RotatedMiddlePointOfTriangle.normalized - point.normalized).sqrMagnitude; foreach (PlanetTriangle tri in neighbors) { float newDistance = (tri.RotatedMiddlePointOfTriangle.normalized - point.normalized).sqrMagnitude; if (newDistance < distance) { distance = newDistance; result = tri; } } return(result); }
public void SetChildNeighbors() { if (!IsLeaf) { List <PlanetTriangle> children = NeighborRelevantChildren.ToList(); for (int i = 0; i < children.Count; i++) { PlanetTriangle t = children[i]; for (int x = 0; x < neighbors.Count /* && currentNeighbor == null*/; x++) { neighbors[x].SetChildNeighborsWith(t); } } foreach (PlanetTriangle t in Children) { t.SetChildNeighbors(); } } }
public PlanetTriangle ClosestTriangleAtSphericalPosition(IEnumerable <PlanetTriangle> possibilities, Vector3 globalPoint) { Vector3 sphericalPoint = globalPoint = globalPoint.normalized * radius; PlanetTriangle result = null; float minDistance = float.MaxValue; foreach (PlanetTriangle tri in possibilities) { float distance = (tri.GetSphericalMiddlePointOfTriangle(radius) - sphericalPoint).sqrMagnitude; if (distance < minDistance) { minDistance = distance; result = tri; } } return(result); }
private void Update() { if (HasPath) { float distance = speed * Time.deltaTime; do { bool isCurrentNull = !entity.HasTriangleAssigned; PlanetTriangle newTri = CurrentTriangle.ClosestFromNeighboursAt(transform.position - Planet.transform.position); if (isCurrentNull || newTri != CurrentTriangle) { CurrentTriangle = newTri; entity.UpdateToTriangle(CurrentTarget - transform.position); } if (distance * distance >= missingSqrDistance) { distance -= Mathf.Sqrt(missingSqrDistance); missingSqrDistance = 0; transform.position = CurrentTarget; if (HasPath) { SetNextPathPart(); } else { ReachedDestination(); } } else { Vector3 nextPosition = transform.position + transform.forward * distance; transform.position = nextPosition; missingSqrDistance = (CurrentTarget - transform.position).sqrMagnitude; distance = 0; } } while (distance > 0 && HasPath); } }
public void SetChildNeighborsWith(PlanetTriangle target) { if (!IsLeaf) { List <PlanetTriangle> children = Children.ToList(); children = children.Where(c => c.IsNeighborWith(target)).ToList(); foreach (PlanetTriangle child in children) { if (!child.neighbors.Contains(target)) { child.AddNeighborsTwoWay(target); } } //NeighborRelevantChildren.Where(c => c.IsNeighborWith(target)).ToList().ForEach(c => c.AddNeighborTwoWay(target)); } else { Debug.LogError("trianlge has higher lod then neighbor. Holes in mesh needs to be fixed"); } }
public PlanetTriangle GetTriangleWithData(TriangleInfo triInfo) { Vector3 localPosition = triInfo.position - transform.position; PlanetTriangle result; if (levelOfDetail == 1) { result = mainTriangles.Where(t => t.RotatedNormal == triInfo.normal).First(); } else { IEnumerable <PlanetTriangle> tris = mainTriangles; do { result = ClosestTriangleAtSphericalPosition(tris, localPosition); tris = result.Children; } while (!result.IsLastParent); result = result.Children.Where(t => t.RotatedNormal == triInfo.normal).FirstOrDefault(); if (result == null) { // Debug.LogWarning("Couldnt find triangle with same normal. Searched in neighbors of closest."); result = ClosestTriangleAtSphericalPosition(tris, localPosition); PlanetTriangle normalInNeighbor = result.neighbors.Where(t => t.RotatedNormal == triInfo.normal).FirstOrDefault(); if (normalInNeighbor == null) { throw new System.Exception("Couldnt find triangle with hit normal!"); } else { result = normalInNeighbor; } } } return(result); }
public void AddNeighborTwoWay(PlanetTriangle Neighbor) { Neighbor.AddNeighbor(this); AddNeighbor(Neighbor); }
public void AddChild(PlanetTriangle child) { Children[childCount] = child; child.parent = this; childCount++; }
public IList <int> GetNeighborVertices(PlanetTriangle t) { return(GetNeighborVertices(t.TriIndizes)); }
public PlanetTriangle[] GetCircumjacent(PlanetTriangle field) { return(field.GetAccessableCircumjacent()); }
public float DistanceToField(PlanetTriangle from, PlanetTriangle to) { return(Vector3.Distance(from.RotatedMiddlePointOfTriangle, to.RotatedMiddlePointOfTriangle)); }
public bool ReachedTarget(PlanetTriangle current, TriangleInfo destination) { return(current.RotatedNormal == destination.normal); }
public bool IsEqual(PlanetTriangle t1, PlanetTriangle t2) { return(t1 == t2); }
protected override void OnTriangleSet(PlanetTriangle t) { //Planet.DiscoverTriangle(t); }
protected virtual void OnTriangleSet(PlanetTriangle t) { }