public void assignData(int _x, int _y, int _z, VoxelObject _voxelObject = null) { x = _x; y = _y; z = _z; voxelObject = _voxelObject; }
public Location(float _x, float _y, float _z, VoxelObject _voxelObject) { x = _x; y = _y; z = _z; voxelObject = _voxelObject; }
// Convert int to float public Location(int _x, int _y, int _z, VoxelObject _voxelObject) { x = (float)_x; y = (float)_y; z = (float)_z; voxelObject = _voxelObject; }
private CollisionState collisionState = CollisionState.Waiting; // The collision state of this chunk public Chunk(int _x, int _y, int _z, VoxelObject _voxelObject) { x = _x; y = _y; z = _z; voxelObject = _voxelObject; blocks = new Block[(Constants.CHUNK_SIZE * Constants.CHUNK_SIZE * Constants.CHUNK_SIZE)]; behaviorBlocks = new List <BehaviorBlock>(); voidBlockCount = blocks.Length; voidState = true; }
public void Render() { foreach (KeyValuePair <Guid, VoxelObject> entry in voxelObjects) { VoxelObject thisVoxelObject = entry.Value; if (thisVoxelObject != null && thisVoxelObject.loaded == true) { thisVoxelObject.Render(); } } }
public static void SaveVoxelObject(VoxelObject _voxelObject, string _fileName) { String path = @"external/saves/voxel objects/" + _fileName + ".svo"; if (File.Exists(path) == false) { Directory.CreateDirectory(Path.GetDirectoryName(path)); } StreamWriter file = new StreamWriter(path); file.WriteLine(_voxelObject.getName()); file.WriteLine(_voxelObject.chunkSizeX + "," + _voxelObject.chunkSizeY + "," + _voxelObject.chunkSizeZ); for (int x = 0; x < _voxelObject.getLoadedChunks().Count; x++) { Chunk thisChunk = _voxelObject.getLoadedChunks()[x]; Block[] blocks = thisChunk.getRawData(); for (int i = 0; i < blocks.Length; i++) { Block thisBlock = blocks[i]; if (thisBlock != null) { String line = ""; line += "v:" + thisBlock.getType(); line += "/p:" + thisBlock.getWorldX() + "," + thisBlock.getWorldY() + "," + thisBlock.getWorldZ(); if (thisBlock.getBlockData() is Rotatable) { line += "/r:" + ((Rotatable)thisBlock.getBlockData()).getDirection(); } if (thisBlock.getBlockData() is Flippable && ((Flippable)thisBlock.getBlockData()).canFlip()) { line += "/f:" + ((Flippable)thisBlock.getBlockData()).isFlipped(); } if (thisBlock.getBlockData() is Orientated) { line += "/o:" + ((Orientated)thisBlock.getBlockData()).getOrientation(); } file.WriteLine(line); } } } file.Close(); }
public void Update() { foreach (KeyValuePair <Guid, VoxelObject> entry in voxelObjects) { VoxelObject thisVoxelObject = entry.Value; if (thisVoxelObject != null) { thisVoxelObject.Update(); } if (thisVoxelObject.component != null) { thisVoxelObject.component.TryBuildChunk(); } } }
public void Tick() { foreach (KeyValuePair <Guid, VoxelObject> entry in voxelObjects) { VoxelObject thisVoxelObject = entry.Value; if (thisVoxelObject != null && thisVoxelObject.isStatic == false) { thisVoxelObject.Tick(); } if (thisVoxelObject.component != null) { thisVoxelObject.component.Tick(); } } }
public VoxelHitDataBuiltin(RaycastHit _hit) { rayInfo = _hit; component = _hit.transform.GetComponent <VoxelComponent>(); if (component == null) { return; } voxelObject = component.voxelObject; worldNormal = _hit.normal; worldNormal.Normalize(); localNormal = _hit.transform.InverseTransformDirection(_hit.normal); localNormal.Normalize(); _hit.point += (_hit.normal * 0.1f); // Pad the ray to penetrate into the voxel's space transformPosition = _hit.transform.position + ((BoxCollider)_hit.collider).center; transformPosition -= (Vector3.one * 0.5f); // Reverse block centering //localPosition = _hit.transform.InverseTransformPoint(_hit.point); // USE CLICK SPACE INSTEAD OF COLLIDER SPACE localPosition = _hit.collider.transform.localPosition + ((BoxCollider)_hit.collider).center; localPosition -= component.pivotPoint; // Offset by the pivot localPosition -= (Vector3.one * 0.5f); // Reverse block centering //localPosition += localNormal; // The hit voxel is offset by the normal of the face we hit // USE CLICK SPACE INSTEAD OF COLLIDER SPACE localPosition = new Vector3( (float)Math.Round(localPosition.x, 0, MidpointRounding.ToEven), (float)Math.Round(localPosition.y, 0, MidpointRounding.ToEven), (float)Math.Round(localPosition.z, 0, MidpointRounding.ToEven)); localFacePosition = (localPosition + localNormal); worldPosition = _hit.transform.InverseTransformDirection(_hit.point); worldPosition -= worldNormal; // The hit voxel is offset by the normal of the face we hit worldPosition = new Vector3( (float)Math.Round(worldPosition.x, 0, MidpointRounding.ToEven), (float)Math.Round(worldPosition.y, 0, MidpointRounding.ToEven), (float)Math.Round(worldPosition.z, 0, MidpointRounding.ToEven)); worldFacePosition = (worldPosition + worldNormal); atHit = Coord3D.fromVector3(localPosition); // The coord of the voxel that was hit atFace = Coord3D.fromVector3(localFacePosition); // The coord of the voxel at the face that was hit }
public VoxelHitData(Unity.Physics.RaycastHit _hit, Unity.Entities.Entity _entity) { rayInfo = _hit; hitTransform = GameMaster.Instance.entitiesToObjectMap[_entity]; component = hitTransform.GetComponent <VoxelComponent>(); if (component == null) { return; } voxelObject = component.voxelObject; _hit.Position += (_hit.SurfaceNormal * 0.1f); // Pad the ray to penetrate into the voxel's space worldNormal = _hit.SurfaceNormal; worldNormal.Normalize(); localNormal = hitTransform.InverseTransformDirection(_hit.SurfaceNormal); localNormal.Normalize(); voxelPosition = hitTransform.InverseTransformPoint(_hit.Position); voxelPosition -= (component.pivotPoint + (Vector3.one * 0.5f)); // Offset to match voxel coordinates voxelPosition.x = (float)Math.Round(voxelPosition.x); voxelPosition.y = (float)Math.Round(voxelPosition.y); voxelPosition.z = (float)Math.Round(voxelPosition.z); voxelPosition -= localNormal; // Reverse centering localPosition = voxelPosition + component.pivotPoint + (Vector3.one * 0.5f); worldPosition = hitTransform.position + (hitTransform.rotation * localPosition); localFacePosition = (localPosition + localNormal); voxelFacePosition = (voxelPosition + localNormal); worldFacePosition = (worldPosition + worldNormal); atHit = Coord3D.fromVector3(voxelPosition); // The coord of the voxel that was hit atFace = Coord3D.fromVector3(voxelFacePosition); // The coord of the voxel at the face that was hit }
public Coord3D(double _x, double _y, double _z, VoxelObject _voxelObject = null) { assignData(Mathf.RoundToInt((float)_x), Mathf.RoundToInt((float)_y), Mathf.RoundToInt((float)_z), _voxelObject); }
public Coord3D(float _x, float _y, float _z, VoxelObject _voxelObject = null) { assignData(Mathf.RoundToInt(_x), Mathf.RoundToInt(_y), Mathf.RoundToInt(_z), _voxelObject); }
public Coord3D(int _x, int _y, int _z, VoxelObject _voxelObject = null) { assignData(_x, _y, _z, _voxelObject); }
public static void LoadVoxelObject(VoxelObject _voxelObject, string _fileName) { String path = @"external/saves/voxel objects/" + _fileName + ".svo"; if (File.Exists(path) == true) { StreamReader file = new StreamReader(path); string name = file.ReadLine(); string size = file.ReadLine(); string[] parts = size.Split(','); int sizeX = int.Parse(parts[0]); int sizeY = int.Parse(parts[1]); int sizeZ = int.Parse(parts[2]); _voxelObject.setName(name); _voxelObject.chunkSizeX = sizeX; _voxelObject.chunkSizeY = sizeY; _voxelObject.chunkSizeZ = sizeZ; _voxelObject.Reset(); while (file.EndOfStream == false) { Voxel voxel = Voxel.VOID; Block thisBlock = null; int posX = 0; int posY = 0; int posZ = 0; string entry = file.ReadLine(); string[] sections = entry.Split('/'); for (int i = 0; i < sections.Length; i++) { string[] section = sections[i].Split(':'); string tag = section[0]; string value = section[1]; if (tag == "v") { voxel = (Voxel)Enum.Parse(typeof(Voxel), value); thisBlock = voxel.toBlock(); } else if (tag == "p") { parts = value.Split(','); posX = int.Parse(parts[0]); posY = int.Parse(parts[1]); posZ = int.Parse(parts[2]); } else if (tag == "r") { Direction facing = (Direction)Enum.Parse(typeof(Direction), value); Rotatable data = (Rotatable)thisBlock.getBlockData(); data.setDirection(facing); thisBlock.setBlockData(data); } else if (tag == "f") { bool flipped = bool.Parse(value); Flippable data = (Flippable)thisBlock.getBlockData(); data.setFlipped(flipped); thisBlock.setBlockData(data); } else if (tag == "o") { Direction orientation = (Direction)Enum.Parse(typeof(Direction), value); Orientated data = (Orientated)thisBlock.getBlockData(); data.setOrientation(orientation); thisBlock.setBlockData(data); } } _voxelObject.setBlockAt(posX, posY, posZ, thisBlock); } file.Close(); } }
protected override void ThreadFunction() { while (stop != true) { if (state == ChunkState.Stopped) { continue; } else if (state == ChunkState.Stopping) { state = ChunkState.Stopped; continue; } if (queue.Count == 0) { state = ChunkState.Waiting; } else { state = ChunkState.Loading; ChunkComponent thisComponent; bool dequeued = queue.TryDequeue(out thisComponent); if (dequeued == false || thisComponent == null) { continue; } Coord3D thisPosition = thisComponent.position; if (thisPosition == null) { continue; } VoxelObject voxelObject = thisComponent.position.voxelObject; if (voxelObject == null) { continue; } if (thisComponent.chunk == null) { Chunk thisChunk = voxelObject.GenerateChunk(thisPosition.x, thisPosition.y, thisPosition.z); thisChunk.component = thisComponent; thisComponent.chunk = thisChunk; thisChunk.PrepareLoad(); thisComponent.BuildCollision(); } else { thisComponent.BuildCollision(); } if (state != ChunkState.Stopped) { state = ChunkState.Waiting; } } updates++; // Increment update count } IsDone = true; }