I’m making a game which requires using a voxel engine to build structures, except I plan to include more models than just simple cubes, and some of these are quite small. So I need to the voxels to be smaller than 1 unity unit each (3 per unit). The problem is I’m having trouble converting between world space and voxel coordinates due to the unequal sizes (if they were the same size it wouldn’t be a problem). This is what I’ve tried so far:
public Vector3 GetIndexFromWorld(Vector3 worldPos, Vector3 normals) {
float half = ((1f / (float)VoxelsPerMeter) / 2f); // VoxelsPerMeter equals 3
Vector3 localPos = transform.InverseTransformPoint(worldPos);
Vector3 point = localPos + normals * -half;
return new Vector3(Mathf.RoundToInt(point.x) * VoxelsPerMeter, Mathf.RoundToInt(point.y) * VoxelsPerMeter, Mathf.RoundToInt(point.z) * VoxelsPerMeter);
}
Thanks in advance for any help.
What's InverseTransformPoint for -- Do your objects run diagonally?
– Owen-ReynoldsI have the InverseTransformPoint to convert from world space to the chunks local space, because the chunk contains the 3D array of voxels (and yea, to give the option of rotating the chunks).
– nug700