void SetTargetVoxel(BlockType ty, bool adjacent = false) { Ray ray; if (Cursor.visible) { ray = Camera.main.ScreenPointToRay(Input.mousePosition); } else { ray = cam.ViewportPointToRay(new Vector3(0.5f, 0.5f, 0)); } if (Physics.Raycast(ray, out RaycastHit hit)) { Vector3 p = hit.point + hit.normal * (adjacent ? 0.5f : -0.5f); Vector3Int xyz = chunk.WorldToXYZ(p), size = chunk.settings.size; if (xyz.x < 0 || xyz.y < 0 || xyz.z < 0 || xyz.x >= size.x || xyz.y >= size.y || xyz.z >= size.z) { return; } if (!destroyBedrock && chunk.voxels[chunk.XYZtoIndex(xyz)].ty == BlockType.Bedrock) { return; } chunk.voxels[chunk.XYZtoIndex(xyz)].ty = ty; chunk.Regenerate(); } }
IEnumerator InspectorRegen() { List <CombinedVoxelMesh> insts = CombinedVoxelMesh.instances; Debug.Log($"Regenerate {insts.Count} Chunks"); for (int i = 0; i < insts.Count; i++) { CombinedVoxelMesh chunk = insts[i]; for (int j = 0; j < chunk.voxels.Length; j++) { chunk.voxels[j].ty = BlockType.Air; } chunk.FillVoxels(); yield return(null); chunk.Regenerate(); } Debug.Log($"{insts.Count} Chunks Regenerated"); }
IEnumerator UpdateChunks(Vector2Int vpos, int msLimit = 5) { viewPos = vpos; if (coro != null) { StopCoroutine(coro); coro = null; } sw.Restart(); for (float y = 0.5f - radius, rsq = (radius + bias) * (radius + bias); y < radius; y++) { for (float x = 0.5f - radius; x < radius; x++) { if (x * x + y * y <= rsq) { Vector2Int p = vpos + new Vector2Int((int)(x - 0.5f), (int)(y - 0.5f)); if (!instMap.ContainsKey(p)) { posStack.Push(p); } } } } for (int i = 0; i < chunks.Length; i++) { CVM_Chunk c = chunks[i]; Vector2 dif = c.pos + new Vector2(0.5f, 0.5f) - vpos; if (dif.x * dif.x + dif.y * dif.y > (radius + bias) * (radius + bias)) { Vector2Int nPos = posStack.Pop(); if (instMap.ContainsKey(nPos)) { print("taken!"); } CombinedVoxelMesh cvm = c.CVM; instMap.Remove(c.pos); instMap[nPos] = cvm; chunks[i].pos = nPos; cvm.gameObject.transform.position = new Vector3(nPos.x * csx, 0f, nPos.y * csz); if (voxelMap.ContainsKey(nPos)) { cvm.voxels = voxelMap[nPos]; } else { cvm.voxels = voxelMap[nPos] = new Voxel[cvm.voxels.Length]; cvm.FillVoxels(); } cvm.Regenerate(); if (sw.ElapsedMilliseconds >= msLimit) { sw.Restart(); yield return(null); } } chunks[i].center = vpos; } posStack.Clear(); /*for (int i = 0; i < chunks.Length; i++) { * CVM_Chunk c = chunks[i]; * Vector2 dif = c.pos + new Vector2(0.5f, 0.5f) - vpos; * * if (dif.x * dif.x + dif.y * dif.y > (radius + bias) * (radius + bias)) { * Vector2Int nPos = c.center + vpos - c.pos - new Vector2Int(1, 1); * * if (instMap.ContainsKey(nPos)) print("taken!"); * * CombinedVoxelMesh cvm = c.CVM; * instMap.Remove(c.pos); * instMap[nPos] = cvm; * chunks[i].pos = nPos; * * cvm.gameObject.transform.position = new Vector3(nPos.x * csx, 0f, nPos.y * csz); * * if (voxelMap.ContainsKey(nPos)) * cvm.voxels = voxelMap[nPos]; * else { * cvm.voxels = voxelMap[nPos] = new Voxel[cvm.voxels.Length]; * cvm.FillVoxels(); * } * cvm.Regenerate(); * * if (sw.ElapsedMilliseconds >= msLimit) { * sw.Restart(); * yield return null; * } * } * * chunks[i].center = vpos; * }*/ }