public void Update() { Chunk chunk = Engine.PositionToChunk(transform.position); if (chunk == null) { return; } Index voxelIndex = chunk.PositionToVoxelIndex(transform.position); VoxelInfo voxelInfo = new VoxelInfo(voxelIndex, chunk); // create a local copy of the collision voxel so we can call functions on it GameObject voxelObject = Instantiate(Engine.GetVoxelGameObject(voxelInfo.GetVoxel())) as GameObject; VoxelEvents events = voxelObject.GetComponent <VoxelEvents>(); if (events != null) { // OnEnter if (chunk != LastChunk || voxelIndex.IsEqual(LastIndex) == false) { events.OnBlockEnter(gameObject, voxelInfo); } else // OnStay { events.OnBlockStay(gameObject, voxelInfo); } } LastChunk = chunk; LastIndex = voxelIndex; Destroy(voxelObject); }
// a raycast which returns the index of the hit voxel and the gameobject of the hit chunk public static VoxelInfo VoxelRaycast(Vector3 origin, Vector3 direction, float range, bool ignoreTransparent) { RaycastHit hit = new RaycastHit(); if (!Physics.Raycast(origin, direction, out hit, range)) { return(null); } GameObject g = hit.collider.gameObject; if (g.GetComponent <Chunk>() == null && g.GetComponent <ChunkExtension>() == null) { return(null); } if (g.GetComponent <ChunkExtension>() != null) { // if we hit a mesh container instead of a chunk g = g.transform.parent.gameObject; // swap the mesh container for the actual chunk object } // check if we're actually hitting a chunk Chunk ch = g.GetComponent <Chunk>(); Index idx = ch.PositionToVoxelIndex(hit.point, hit.normal, false); if (ignoreTransparent) { // punch through transparent voxels by raycasting again when a transparent voxel is hit ushort hitVoxel = ch.GetVoxel(idx.x, idx.y, idx.z); if (GetVoxelType(hitVoxel).VTransparency != Transparency.solid) { Vector3 newOrigin = hit.point; newOrigin.y -= 0.5f; // push the new raycast down a bit return(VoxelRaycast(newOrigin, Vector3.down, range - hit.distance, true)); } } return(new VoxelInfo(ch.PositionToVoxelIndex(hit.point, hit.normal, false), // get hit voxel index ch.PositionToVoxelIndex(hit.point, hit.normal, true), // get adjacent voxel index ch)); // get chunk }
public static VoxelInfo PositionToVoxelInfo ( Vector3 position ) { GameObject chunkObject = Engine.PositionToChunk (position); if (chunkObject != null) { Chunk chunk = chunkObject.GetComponent<Chunk>(); Index voxelIndex = chunk.PositionToVoxelIndex (position); return new VoxelInfo (voxelIndex, chunk); } else { return null; } }
public static VoxelInfo PositionToVoxelInfo(Vector3 position) { Chunk chunk = PositionToChunk(position); if (chunk == null) { return(null); } Index vIdx = chunk.PositionToVoxelIndex(position); return(new VoxelInfo(vIdx, chunk)); }
public void Update() { // check if chunk is not null GameObject chunkObject = Engine.PositionToChunk(transform.position); if (chunkObject == null) { return; } // get the voxelInfo from the transform's position Chunk chunk = chunkObject.GetComponent <Chunk>(); Index voxelIndex = chunk.PositionToVoxelIndex(transform.position); voxelInfo = new VoxelInfo(voxelIndex, chunk); // create a local copy of the collision voxel so we can call functions on it GameObject voxelObject = Instantiate(Engine.GetVoxelGameObject(voxelInfo.GetVoxel())) as GameObject; VoxelEvents events = voxelObject.GetComponent <VoxelEvents>(); if (events != null) { // OnEnter if (chunk != lastChunk || voxelIndex.IsEqual(voxelIndex) == false) { events.OnBlockEnter(gameObject, voxelInfo); } // OnStay else { events.OnBlockStay(gameObject, voxelInfo); } } lastChunk = chunk; Destroy(voxelObject); }