public void Sphere(int _radius) { int radiusSquared = _radius * _radius; Coord3D offset = new Coord3D(getBlockSizeX() / 2, getBlockSizeY() / 2, getBlockSizeZ() / 2); // Distance filled sphere for (int x = -_radius; x < _radius; x++) { for (int y = -_radius; y < _radius; y++) { for (int z = -_radius; z < _radius; z++) { Coord3D coord = new Coord3D(x, y, z); //float distance = Util.DistanceUnsquared(coord.toVector3(), UnityEngine.Vector3.zero); float distance = getDistance(x, y, z); if (distance < radiusSquared) { Voxel voxel = Voxel.ASTEROID_ROCK; if (distance > radiusSquared - (_radius * 3)) { voxel = Voxel.GRASS_BLOCK; } else if (distance > radiusSquared - (_radius * 9)) { voxel = Voxel.DIRT; } this.setBlockAt(coord + offset, voxel, false); } } } } }
public override void FinishLoad() { base.FinishLoad(); this.setBlockAt(getBlockSizeX() / 2, getBlockSizeY() / 2, getBlockSizeZ() / 2, Voxel.VOID); int radius = (int)((getBlockSizeX() * 0.5f) * (getRandom().NextDouble() * (1.0f - 0.65f) + 0.65f)); Coord3D[] points = GameMaster.Instance.voxelMaster.asteroidPresets[getRandom().Next(0, GameMaster.Instance.voxelMaster.asteroidPresets.Count)]; //Sphere( radius, 16 ); Voxel voxel = Voxel.ASTEROID_ROCK; if (getRandom().NextDouble() <= 0.5d) { voxel = Voxel.ASTEROID_ICE; } Coord3D offset = new Coord3D(getBlockSizeX() / 2, getBlockSizeY() / 2, getBlockSizeZ() / 2); for (int i = 0; i < points.Length; i++) { Coord3D point = points[i] + offset; this.setBlockAt(point, voxel, false); } component.transform.Rotate(getRandom().Next(360), getRandom().Next(360), getRandom().Next(360)); }
public override bool Equals(System.Object obj) { Coord3D coord = obj as Coord3D; if (coord == null) { return(false); } return(this.x.Equals(coord.x) && this.y.Equals(coord.y) && this.z.Equals(coord.z)); }
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 void Queue(Coord3D _chunk) { // Make certain this chunk isn't already queued /*for (int i = 0; i < queue.Count; i++) * { * if (ReferenceEquals(queue[i], _chunk) == true) * { * return; * } * }*/ queue.Enqueue(_chunk); // if (_chunk != null)// && _chunk.isVoid() == false && _chunk.isCulled() == false) // { // queue.Enqueue(_chunk); // } }
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 static UnityEngine.Vector3 toVector3(Coord3D _coord) { return(new UnityEngine.Vector3(_coord.x, _coord.y, _coord.z)); }
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; }
public Block setBlockAt(Coord3D _coord, Voxel _voxel, bool _update = true) { return(setBlockAt(_coord.x, _coord.y, _coord.z, _voxel.toBlock(), _update)); }
public Block setBlockAt(Coord3D _coord, Block _block, bool _update = true) { return(setBlockAt(_coord.x, _coord.y, _coord.z, _block, _update)); }
public Block getHighestBlockAt(Coord3D _coord) { return(getHighestBlockAt(_coord.x, _coord.y, _coord.z)); }
public void setLocation(Coord3D _coord) { this.setLocation(_coord.x, _coord.y, _coord.z); }
public Coord3D[] Sphere(int _radius, int _cutouts = 0) { List <Coord3D> coordinates = new List <Coord3D>(); int radiusSquared = _radius * _radius; Coord3D[] cutoutOrigins = new Coord3D[_cutouts]; int[] cutoutRadiuses = new int[_cutouts]; for (int i = 0; i < _cutouts; i++) { int xPos = getRandom().Next((int)(_radius * 2)) - _radius; int yPos = getRandom().Next((int)(_radius * 2)) - _radius; int zPos = getRandom().Next((int)(_radius * 2)) - _radius; Coord3D cutoutOrigin = new Coord3D(xPos, yPos, zPos); int cutoutRadius = getRandom().Next(4, (int)(_radius * 0.6f)); cutoutRadius *= cutoutRadius; cutoutOrigins[i] = cutoutOrigin; cutoutRadiuses[i] = cutoutRadius; } Coord3D[] addOrigins = new Coord3D[_cutouts]; int[] addRadiuses = new int[_cutouts]; for (int i = 0; i < _cutouts; i++) { int xPos = getRandom().Next((int)(_radius * 2)) - _radius; int yPos = getRandom().Next((int)(_radius * 2)) - _radius; int zPos = getRandom().Next((int)(_radius * 2)) - _radius; Coord3D cutoutOrigin = new Coord3D(xPos, yPos, zPos); int cutoutRadius = getRandom().Next(4, (int)(_radius * 0.6f)); cutoutRadius *= cutoutRadius; addOrigins[i] = cutoutOrigin; addRadiuses[i] = cutoutRadius; } float xScale = ((float)getRandom().Next(30, 100)) / 100; float yScale = ((float)getRandom().Next(30, 100)) / 100; float zScale = ((float)getRandom().Next(30, 100)) / 100; // Distance filled sphere for (int x = -_radius; x < _radius; x++) { for (int y = -_radius; y < _radius; y++) { for (int z = -_radius; z < _radius; z++) { Coord3D coord = new Coord3D(x * xScale, y * yScale, z * zScale); float distance = getDistance(x, y, z); if (distance < radiusSquared) { bool isCut = false; for (int i = 0; i < _cutouts; i++) { float cutoutDistance = getDistance(x - cutoutOrigins[i].x, y - cutoutOrigins[i].y, z - cutoutOrigins[i].z); if (cutoutDistance < cutoutRadiuses[i]) { isCut = true; break; } } if (isCut == false) { for (int i = 0; i < _cutouts; i++) { float cutoutDistance = getDistance(x - addOrigins[i].x, y - addOrigins[i].y, z - addOrigins[i].z); if (cutoutDistance < addRadiuses[i]) { coordinates.Add(coord); break; } } } } } } } return(coordinates.ToArray()); }
public Coord3D[] Sphere(int _radius, int _cutouts = 0) { List <Coord3D> coordinates = new List <Coord3D>(); int radiusSquared = _radius * _radius; Coord3D[] cutoutOrigins = new Coord3D[_cutouts]; int[] cutoutRadiuses = new int[_cutouts]; for (int i = 0; i < _cutouts; i++) { int xPos = getRandom().Next((int)(_radius * 2)) - _radius; int yPos = getRandom().Next((int)(_radius * 2)) - _radius; int zPos = getRandom().Next((int)(_radius * 2)) - _radius; Coord3D cutoutOrigin = new Coord3D(xPos, yPos, zPos); int cutoutRadius = getRandom().Next(4, (int)(_radius * 0.6f)); cutoutRadius *= cutoutRadius; cutoutOrigins[i] = cutoutOrigin; cutoutRadiuses[i] = cutoutRadius; } Coord3D[] addOrigins = new Coord3D[_cutouts]; int[] addRadiuses = new int[_cutouts]; for (int i = 0; i < _cutouts; i++) { int xPos = getRandom().Next((int)(_radius * 2)) - _radius; int yPos = getRandom().Next((int)(_radius * 2)) - _radius; int zPos = getRandom().Next((int)(_radius * 2)) - _radius; Coord3D cutoutOrigin = new Coord3D(xPos, yPos, zPos); int cutoutRadius = getRandom().Next(4, (int)(_radius * 0.6f)); cutoutRadius *= cutoutRadius; addOrigins[i] = cutoutOrigin; addRadiuses[i] = cutoutRadius; } float xScale = ((float)getRandom().Next(30, 100)) / 100; float yScale = ((float)getRandom().Next(30, 100)) / 100; float zScale = ((float)getRandom().Next(30, 100)) / 100; // Distance filled sphere for (int x = -_radius; x < _radius; x++) { for (int y = -_radius; y < _radius; y++) { for (int z = -_radius; z < _radius; z++) { Coord3D coord = new Coord3D(x * xScale, y * yScale, z * zScale); float distance = getDistance(x, y, z); if (distance < radiusSquared) { bool isCut = false; for (int i = 0; i < _cutouts; i++) { float cutoutDistance = getDistance(x - cutoutOrigins[i].x, y - cutoutOrigins[i].y, z - cutoutOrigins[i].z); if (cutoutDistance < cutoutRadiuses[i]) { isCut = true; break; } } if (isCut == false) { for (int i = 0; i < _cutouts; i++) { float cutoutDistance = getDistance(x - addOrigins[i].x, y - addOrigins[i].y, z - addOrigins[i].z); if (cutoutDistance < addRadiuses[i]) { coordinates.Add(coord); break; } } } // if (isCut == false) { coordinates.Add(coord); } } } } } // Hallow sphere // double step1 = (2*Math.PI) / (_radius * 10); // double step2 = 360 / (_radius * 10); // for (double i = 0; i <= 2*Math.PI; i += step1) // { // for (double n = 0; n <= 360; n += step2) // { // for (int radius = 0; radius < _radius; radius++) // { // int x = (int)( radius * Math.Cos(n) * Math.Sin(i) ); // int y = (int)( radius * Math.Cos(i)); // int z = (int)( radius * Math.Sin(n) * Math.Sin(i) ); // coordinates.Add(new Coord3D(x, y, z)); // } // } // } // Filled circle // for (int i = 0; i < _radius; i++) // { // for (double n = 0.0d; n < 360.0d; n += 0.1d) // { // double angle = n * Math.PI / 180.0d; // int x = (int)( i * Math.Cos(angle) ); // int y = (int)( i * Math.Sin(angle) ); // coordinates.Add(new Coord3D(x, y, z)); // } // } return(coordinates.ToArray()); }