public override Vector3 FindNearestVertex(Vector3 fromPoint, bool doDebug = false) { Vector3 face = FindNearestFace(fromPoint, gridPlane); //find the nearest face in the world // calculate the offset of the point from the centre of the hex (local 3D coordinates) Vector3 facePoint = _transform.GFInverseTransformPointFixed(fromPoint) - _transform.GFInverseTransformPointFixed(face); // Vector3 -> Vector2 //inside the tile (hex) there are six possibilities, we need to find the right one HexVertex vert = GetHexVert(facePoint); Vector3 toPoint = face; // use the centre of the hex as a starting point // now move to the proper vertex if (vert == HexVertex.NE || vert == HexVertex.NW) // move up { toPoint += unitVectors[idx[1]] * 0.5f * height; } else if (vert == HexVertex.SE || vert == HexVertex.SW) // move down { toPoint -= unitVectors[idx[1]] * 0.5f * height; } if (vert == HexVertex.NE || vert == HexVertex.SE) // move right { toPoint += unitVectors[idx[0]] * 0.5f * radius; } else if (vert == HexVertex.NW || vert == HexVertex.SW) // move left { toPoint -= unitVectors[idx[0]] * 0.5f * radius; } if (vert == HexVertex.W) //move left { toPoint -= unitVectors[idx[0]] * radius; } else if (vert == HexVertex.E) //move right { toPoint += unitVectors[idx[0]] * radius; } if (doDebug) { Gizmos.DrawSphere(toPoint, 0.3f); } return(toPoint); }
// I think this returns the vertex in a vertex-based coordinate system (as opposed to the default face-based one) protected Vector3 GetVertexCoordinatesSomething(Vector3 world) { Vector3 face = FindNearestFace(world, gridPlane); //find the nearest face in the world Vector3 localFace = GetFaceCoordinates(world, gridPlane); //the same face but with local coordinates // calculate the offset of the point from the centre of the hex (local 3D coordinates) Vector3 facePoint = _transform.GFInverseTransformPointFixed(world) - _transform.GFInverseTransformPointFixed(face); // Vector3 -> Vector2 //inside the tile (hex) there are six possibilities, we need to find the right one HexVertex vert = GetHexVert(facePoint); Vector3 vertex = localFace; // use the centre of the hex as a starting point // Debug.Log("Face Point: " + facePoint + ", vertex: " + vert); vertex[idx[1]] = 2 * localFace[idx[1]]; // the quasi-Y of a vertex is at least twice the quasi-Y of the face if (vert == HexVertex.W || vert == HexVertex.E) // move one step up { vertex[idx[1]]++; } else if (vert == HexVertex.NW || vert == HexVertex.NE) // move two steps up { vertex[idx[1]]++; vertex[idx[1]]++; } if (vert == HexVertex.NE || vert == HexVertex.E || vert == HexVertex.SE) // move one step right { vertex[idx[0]]++; } if (Mathf.Abs(localFace[idx[0]] % 2) > 0.1) // upwards shifted hexes add one step by default { vertex[idx[1]]++; } //Debug.Log(vert); //Debug.Log(tile); // Debug.Log("vertex: " + vertex + ", face: " + localFace); return(vertex); }
// returns the direction from a face to the specified vertex (world space only!) protected Vector3 VertexToDirection(HexVertex vert, bool worldSpace = true) { Vector3 dir = new Vector3(); if (vert == HexVertex.E || vert == HexVertex.W) { dir = unitVectors[idx[0]] * radius; if (vert == HexVertex.W) { dir = -dir; } } else if (vert == HexVertex.N || vert == HexVertex.S) { dir = unitVectors[idx[1]] * 0.5f * height; if (vert == HexVertex.S) { dir = -dir; } } else if (vert == HexVertex.NE || vert == HexVertex.SW) { dir = 0.5f * radius * unitVectors[idx[0]] + 0.5f * height * unitVectors[idx[1]]; if (vert == HexVertex.SW) { dir = -dir; } } else if (vert == HexVertex.NW || vert == HexVertex.SE) { dir = -0.5f * radius * unitVectors[idx[0]] + 0.5f * height * unitVectors[idx[1]]; if (vert == HexVertex.SE) { dir = -dir; } } return(dir); }