// Update is called once per frame void Update() { if (player != null) { newPlayerVoxelPos = VoxelConversions.WorldToVoxel(player.transform.position); newPlayerChunkPos = VoxelConversions.VoxelToChunk(newPlayerVoxelPos); //Debug.Log(generateArroundChunk + ", " + newPlayerChunkPos + ", " + Vector3.Distance(generateArroundChunk, newPlayerChunkPos)); if (Vector3.Distance(_generateArroundChunk, newPlayerChunkPos) > 0 && !_generating) { // generate around point. //Debug.Log("Debug filling " + newPlayerChunkPos + "."); _generateArroundChunk = newPlayerChunkPos; GenerateSpherical(_generateArroundChunk, null); } /*if (Vector3.Distance(_oldPlayerVoxelPos, newPlayerVoxelPos) > 2) * { * _oldPlayerVoxelPos = newPlayerVoxelPos; * DeleteGrass(); * SpawnGrass(); * }*/ } if (chunksInQueue > 0) { GameManager.Status = string.Format("Generating chunk {0}/{1}.", chunksGenerated, chunksInQueue); } }
public float ValueSample(Vector3Int local, Vector3Int global) { uint type; return((float)Sampler.GetIsoValue(local, global, out type)); if (IsInBounds(local.x, local.y, local.z)) { return(GetBlockValue(local.x, local.y, local.z)); } else { Vector3Int chunk = VoxelConversions.VoxelToChunk(global); Vector3Int chunklocalVoxel = VoxelConversions.GlobalToLocalChunkCoord(chunk, global); SmoothVoxelBuilder builder = (SmoothVoxelBuilder)controller.GetBuilder(chunk.x, chunk.y, chunk.z); if (builder != null) { return(builder.GetBlockValue(chunklocalVoxel.x, chunklocalVoxel.y, chunklocalVoxel.z)); } else { return((float)Sampler.GetIsoValue(chunklocalVoxel, global, out type)); } } }
/*public void SetSurfacePoints(Vector3Int[] points) * { * for (int i = 0; i < points.Length; i++) * if (!surfacePoints.Contains(points[i])) * surfacePoints.Add(points[i]); * }*/ public void SetBlockAtLocation(Vector3 position, byte type) { Vector3Int voxelPos = VoxelConversions.WorldToVoxel(position); Vector3Int chunk = VoxelConversions.VoxelToChunk(voxelPos); Vector3Int localVoxel = VoxelConversions.GlobalVoxToLocalChunkVoxCoord(chunk, voxelPos); if (BuilderExists(chunk.x, chunk.y, chunk.z)) { Chunks[new Vector3Int(chunk.x, chunk.y, chunk.z)].EditNextFrame(new Chunk.BlockChange[] { new Chunk.BlockChange(position, type) }); } }
public Block GetBlock(int x, int y, int z) { Vector3Int chunk = VoxelConversions.VoxelToChunk(new Vector3Int(x, y, z)); Vector3Int localVoxel = VoxelConversions.GlobalToLocalChunkCoord(chunk, new Vector3Int(x, y, z)); Block result = default(Block); if (BuilderExists(chunk.x, chunk.y, chunk.z)) { result = Chunks[chunk].GetBlock(localVoxel.x, localVoxel.y, localVoxel.z); } return(result); }
public byte GetBlock(int x, int y, int z) { Vector3Int chunk = VoxelConversions.VoxelToChunk(new Vector3Int(x, y, x)); Vector3Int localVoxel = VoxelConversions.GlobalVoxToLocalChunkVoxCoord(chunk, new Vector3Int(x, y, z)); byte result = 1; if (x >= 0 && y >= 0 && z >= 0 && BuilderExists(chunk.x, chunk.y, chunk.z)) { result = Chunks[new Vector3Int(chunk.x, chunk.y, chunk.z)].GetBlock(x, y, z); } return(result); }
} // needs to be changed if using superchunks. public byte GetBlock(int x, int y, int z) { Vector3Int chunk = VoxelConversions.VoxelToChunk(new Vector3Int(x, y, x)); Vector3Int localVoxel = VoxelConversions.GlobalVoxToLocalChunkVoxCoord(chunk, new Vector3Int(x, y, z)); byte result = 1; if (x >= 0 && y >= 0 && z >= 0 && Chunks.ContainsKey(chunk)) { result = Chunks[chunk].GetBlock(x, y, z); } return(result); }
public void Generate(int height) { int chunkSizeX = SmoothVoxelSettings.ChunkSizeX; int chunkSizeY = SmoothVoxelSettings.ChunkSizeY; int chunkSizeZ = SmoothVoxelSettings.ChunkSizeZ; int meterSizeX = SmoothVoxelSettings.MeterSizeX; int meterSizeY = SmoothVoxelSettings.MeterSizeY; int meterSizeZ = SmoothVoxelSettings.MeterSizeZ; Sampler.SetChunkSettings(SmoothVoxelSettings.voxelsPerMeter, new Vector3Int(chunkSizeX, chunkSizeY, chunkSizeZ), new Vector3Int(meterSizeX, meterSizeY, meterSizeZ), Mathf.RoundToInt(1 / (float)SmoothVoxelSettings.voxelsPerMeter), ((1.0f / (float)SmoothVoxelSettings.voxelsPerMeter) / 2.0f), new Vector3(meterSizeX / (float)chunkSizeX, meterSizeY / (float)chunkSizeY, meterSizeZ / (float)chunkSizeZ)); Vector3Int topVoxel = VoxelConversions.WorldToVoxel(new Vector3(0, (float)Sampler.GetMax(), 0)); Vector3Int bottomVoxel = VoxelConversions.WorldToVoxel(new Vector3(0, (float)Sampler.GetMin(), 0)); int topChunk = VoxelConversions.VoxelToChunk(topVoxel).y; int bottomChunk = VoxelConversions.VoxelToChunk(bottomVoxel).y; if (NetworkMode) { for (int y = 0; y <= topChunk; y++) { SmoothChunk.CreateChunk(new Vector3Int(Location.x, y, Location.y), Sampler, Controller); } } else { Vector2Int bottomLeft = new Vector2(Location.x * chunkSizeX, Location.y * chunkSizeZ); Vector2Int topRight = new Vector2(Location.x * chunkSizeX + chunkSizeX, Location.y * chunkSizeZ + chunkSizeZ); Sampler.SetSurfaceData(bottomLeft, topRight); for (int y = 0; y <= topChunk; y++) { SmoothChunk.CreateChunk(new Vector3Int(Location.x, y, Location.y), Sampler, Controller); } Loom.QueueOnMainThread(() => { Debug.Log("Spawning grass..."); SpawnGrass(); }); } }
public void ChangeBlock(Chunk.BlockChange change) { Vector3Int chunk = VoxelConversions.VoxelToChunk(change.position); Vector3Int localVoxel = VoxelConversions.GlobalVoxToLocalChunkVoxCoord(chunk, change.position); //Debug.LogFormat("voxel: {0}, localVoxel: {1}, chunk: {2}", voxel, localVoxel, chunk); if (BuilderExists(chunk.x, chunk.y, chunk.z)) { if (localVoxel.x >= 0 && localVoxel.x < VoxelSettings.ChunkSizeX && localVoxel.y >= 0 && localVoxel.y < VoxelSettings.ChunkSizeY && localVoxel.z >= 0 && localVoxel.z < VoxelSettings.ChunkSizeZ) { Chunks[chunk].EditNextFrame(new Chunk.BlockChange(localVoxel, change.type)); } else { SafeDebug.LogError(string.Format("Out of Bounds: chunk: {0}, globalVoxel:{1}, localVoxel: {2}, Function: GenerateExplosion", chunk, change.position, localVoxel)); } } }
public void GenerateExplosion(Vector3 postion, int radius) { System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); watch.Start(); Loom.AddAsyncThread("Explosion"); Loom.QueueAsyncTask("Explosion", () => { Dictionary <Vector3Int, List <Chunk.BlockChange> > changes = new Dictionary <Vector3Int, List <Chunk.BlockChange> >(); Vector3Int voxelPos = VoxelConversions.WorldToVoxel(postion); for (int x = voxelPos.x - radius; x <= voxelPos.x + radius; x++) { for (int y = voxelPos.y - radius; y <= voxelPos.y + radius; y++) { for (int z = voxelPos.z - radius; z <= voxelPos.z + radius; z++) { Vector3Int voxel = new Vector3Int(x, y, z); Vector3Int chunk = VoxelConversions.VoxelToChunk(voxel); if (IsInSphere(voxelPos, radius, voxel)) { if (!changes.ContainsKey(chunk)) { changes.Add(chunk, new List <Chunk.BlockChange>()); } changes[chunk].Add(new Chunk.BlockChange(VoxelConversions.GlobalVoxToLocalChunkVoxCoord(chunk, voxel), 0)); //ChangeBlock(new Chunk.BlockChange(voxel, 0)); } } } } //Debug.Log("Iterated through exploded blocks: " + watch.Elapsed.ToString()); Loom.QueueOnMainThread(() => { foreach (Vector3Int chunkPos in changes.Keys) { ChangeBlock(chunkPos, changes[chunkPos].ToArray()); } watch.Stop(); //Debug.Log("Blocks changes sent to chunk: " + watch.Elapsed.ToString()); }); }); }
// Update is called once per frame void Update() { if (cameraObj != null) { newCameraVoxelPos = VoxelConversions.WorldToVoxel(cameraObj.transform.position); newCameraChunkPos = VoxelConversions.VoxelToChunk(newCameraVoxelPos); //Debug.Log(generateArroundChunk + ", " + newPlayerChunkPos + ", " + Vector3.Distance(generateArroundChunk, newPlayerChunkPos)); /*if (Vector3.Distance(_oldPlayerVoxelPos, newPlayerVoxelPos) > 2) * { * _oldPlayerVoxelPos = newPlayerVoxelPos; * DeleteGrass(); * SpawnGrass(); * }*/ } if (chunksInQueue > 0) { GameManager.Status = string.Format("Generating chunk {0}/{1}.", chunksGenerated, chunksInQueue); } }
public void ChangeBlock(Vector3Int[] voxels, byte type) { foreach (Vector3Int block in voxels) { Vector3Int chunk = VoxelConversions.VoxelToChunk(block); Vector3Int localVoxel = VoxelConversions.GlobalToLocalChunkCoord(chunk, block); //Debug.LogFormat("voxel: {0}, localVoxel: {1}, chunk: {2}", voxel, localVoxel, chunk); if (BuilderExists(chunk.x, chunk.y, chunk.z)) { if (localVoxel.x >= 0 && localVoxel.x < SmoothVoxelSettings.ChunkSizeX && localVoxel.y >= 0 && localVoxel.y < SmoothVoxelSettings.ChunkSizeY && localVoxel.z >= 0 && localVoxel.z < SmoothVoxelSettings.ChunkSizeZ) { Chunks[chunk].EditNextFrame(new SmoothChunk.BlockChange(localVoxel, type)); } else { SafeDebug.LogError(string.Format("Out of Bounds: chunk: {0}, globalVoxel:{1}, localVoxel: {2}, Function: GenerateExplosion", chunk, block, localVoxel)); } } } }
public Block GetBlock(int x, int y, int z) { Vector3Int chunk = VoxelConversions.VoxelToChunk(new Vector3Int(x, y, z)); Vector3Int localVoxel = VoxelConversions.GlobalToLocalChunkCoord(chunk, new Vector3Int(x, y, z)); Block result = default(Block); if (BuilderExists(chunk.x, chunk.y, chunk.z)) { try { result = Chunks[new Vector3Int(chunk.x, chunk.y, chunk.z)].GetBlock(localVoxel.x, localVoxel.y, localVoxel.z); }catch (Exception e) { Debug.LogErrorFormat("{0}, globalZ: {1}", e.Message, z); throw; } } else { chunkNoExistError = true; } return(result); }
public void SpawnGrass() { int xMin = newPlayerVoxelPos.x - maxGrassDistance; int xMax = newPlayerVoxelPos.x + maxGrassDistance; int yMin = newPlayerVoxelPos.y - maxGrassDistance; int yMax = newPlayerVoxelPos.y + maxGrassDistance; int zMin = newPlayerVoxelPos.z - maxGrassDistance; int zMax = newPlayerVoxelPos.z + maxGrassDistance; Loom.QueueAsyncTask(GrassThreadName, () => { List <Vector3Int> grass = new List <Vector3Int>(); for (int x = xMin; x < xMax; x++) { for (int z = zMin; z < zMax; z++) { Vector3Int pos = new Vector3Int(x, 0, z); Vector3Int chunk = VoxelConversions.VoxelToChunk(pos); if (Chunks[chunk].surface2D.Contains(new Vector2Int(pos.x, pos.z)) && !grassObj.ContainsKey(new Vector2Int(x, z)) && IsInSphere(newPlayerVoxelPos, maxGrassDistance, new Vector3Int(x, 0, z))) { grass.Add(pos); } } } Loom.QueueOnMainThread(() => { for (int i = 0; i < grass.Count; i++) { if (!grassObj.ContainsKey(new Vector2Int(grass[i].x, grass[i].z))) { Vector3 globalPos = VoxelConversions.VoxelToWorld(grass[i]); grassObj.Add(new Vector2Int(grass[i].x, grass[i].z), (GameObject)Instantiate(grassPrefab, new Vector3(grass[i].x, 0, grass[i].z), Quaternion.identity)); } } }); }); }
// Update is called once per frame void Update() { if (player != null) { _playerCreated = true; _playerPosition = player.transform.position; LODtarget = _playerPosition; newPlayerVoxelPos = VoxelConversions.WorldToVoxel(_playerPosition); newPlayerChunkPos = VoxelConversions.VoxelToChunk(newPlayerVoxelPos); //Debug.Log(generateArroundChunk + ", " + newPlayerChunkPos + ", " + Vector3.Distance(generateArroundChunk, newPlayerChunkPos)); if (_oldPlayerChunkPos != newPlayerChunkPos && !_generating) { // generate around point. //Debug.Log("Debug filling " + newPlayerChunkPos + "."); _generateArroundChunk = _oldPlayerChunkPos = newPlayerChunkPos; GenerateSpherical(newPlayerChunkPos); } foreach (SmoothChunk chk in Chunks.Values.ToArray()) { chk.ChunkUpdate(); } } }
public GridPoint GetVector4(Vector3 world, Vector3Int local, Vector3Int global, bool generate) { //Vector3 origin = new Vector3(ChunkSizeX / 2, ChunkSizeY / 2, ChunkSizeX / 2); //return new Vector4(world.x, world.y, world.z, Vector3.Distance(origin, world)); GridPoint result = default(GridPoint); Vector3Int chunk = VoxelConversions.VoxelToChunk(global); uint type; if (generate) { if (IsInBounds(local.x, local.y, local.z)) { result = new GridPoint(world.x, world.y, world.z, (float)GetIsoValue(local, global, generate, out type), type); } else { result = new GridPoint(world.x, world.y, world.z, (float)Sampler.GetIsoValue(local, global, out type), type); //result = new GridPoint(world.x, world.y, world.z, 100); /*Vector3Int chunklocalVoxel = VoxelConversions.GlobalToLocalChunkCoord(chunk, global); * SmoothVoxelBuilder builder = (SmoothVoxelBuilder)controller.GetBuilder(chunk.x, chunk.y, chunk.z); * if (builder != null) * { * result = new GridPoint(world.x, world.y, world.z, (float)builder.Sampler.GetIsoValue(local, global, out type), type); * } * else * { * //result = new GridPoint(world.x, world.y, world.z, (float)Sampler.GetIsoValue(chunklocalVoxel, global, out type), type); * result = new GridPoint(world.x, world.y, world.z, (float)Sampler.GetIsoValue(local, global, out type), type); * }*/ } } else { if (chunk != location) { Vector3Int chunklocalVoxel = VoxelConversions.GlobalToLocalChunkCoord(chunk, global); IVoxelBuilder builder = controller.GetBuilder(chunk.x, chunk.y, chunk.z); if (builder != null) { Block builderBlock = builder.GetBlock(chunklocalVoxel.x, chunklocalVoxel.y, chunklocalVoxel.z); result = new GridPoint(world.x, world.y, world.z, (float)builderBlock.iso, builderBlock.type); } else { result = new GridPoint(world.x, world.y, world.z, (float)Sampler.GetIsoValue(chunklocalVoxel, global, out type)); } } else { result = new GridPoint(world.x, world.y, world.z, (float)GetIsoValue(local, global, generate, out type), (byte)type); } } result.OriginLocal = local; result.OriginGlobal = global; return(result); }