private void Redraw(Vector3Int pos) { Vector3Int chunkPosition, blockPosition; Chunk.SplitPosition(pos, out chunkPosition, out blockPosition); RenderedChunk renderedChunk = BlockMap.Instance.GetLoadedRenderedChunk(chunkPosition); Block block = BlockMap.Instance.GetLoadedBlock(pos); if (renderedChunk != null) { renderedChunk.Draw(blockPosition, block); } }
public void StartTerrain() { //Toggle fog. var p = MapUtil.PlayerArea(mapAreaSize, centerAreaSize, PlayerBlockPosition()); CurrentRendererMapPositionG = new Vector3i(p.X, p.Y, 0); if (RendererMap != null) { throw new NotImplementedException(); } RendererMap = new RenderedChunk[(mapAreaSize / chunksize) * (mapAreaSize / chunksize) * (MapSizeZ / chunksize)]; for (int i = 0; i < RendererMap.Length; i++) { RendererMap[i] = new RenderedChunk(); } }
void DoRedraw(TerrainRendererRedraw r) { #if !CITO unchecked { #endif idsCount = 0; RenderedChunk c = r.c.rendered; if (c.ids != null) { for (int i = 0; i < c.idsCount; i++) { int loadedSubmesh = c.ids[i]; game.d_Batcher.Remove(loadedSubmesh); } } for (int i = 0; i < r.dataCount; i++) { VerticesIndicesToLoad submesh = r.data[i]; if (submesh.modelData.GetIndicesCount() != 0) { float centerVecX = submesh.positionX + chunksize * 0.5f; float centerVecY = submesh.positionZ + chunksize * 0.5f; float centerVecZ = submesh.positionY + chunksize * 0.5f; float radius = sqrt3half * chunksize; ids[idsCount++] = game.d_Batcher.Add(submesh.modelData, submesh.transparent, submesh.texture, centerVecX, centerVecY, centerVecZ, radius); } } int[] idsarr = new int[idsCount]; for (int i = 0; i < idsCount; i++) { idsarr[i] = ids[i]; } c.ids = idsarr; c.idsCount = idsCount; #if !CITO } #endif }
private void Start() { loadQueue = new Queue <Vector3Int>(); gridMap = new GridMap(); gridMap.RegisterCreateOnMissChunkMethod(CreateOnMissChunk); gridMap.RegisterOnSetValueMethod(OnGridmapSetValue); StartCoroutine(ChunkLoaderCoroutine()); Texture2D minimapTexture = new Texture2D(144, 144); minimap.sprite = Sprite.Create(minimapTexture, new Rect(0, 0, 144, 144), new Vector2(0.5f, 0.5f)); Color[] emptyColors = new Color[spriteSize * Chunk.chunkSize * spriteSize * Chunk.chunkSize]; for (int i = 0; i < spriteSize * Chunk.chunkSize * spriteSize * Chunk.chunkSize; i++) { emptyColors[i] = Color.clear; } RenderedChunk[] renderedChunks = new RenderedChunk[chunkCapacity]; for (int i = 0; i < chunkCapacity; i++) { GameObject go = new GameObject("Chunk" + i); go.transform.parent = transform; SpriteRenderer renderer = go.AddComponent <SpriteRenderer>(); Texture2D texture = new Texture2D(spriteSize * Chunk.chunkSize, spriteSize * Chunk.chunkSize); texture.SetPixels(emptyColors); texture.Apply(); texture.filterMode = FilterMode.Point; renderer.sprite = Sprite.Create(texture, new Rect(0, 0, spriteSize * Chunk.chunkSize, spriteSize * Chunk.chunkSize), new Vector2(0, 0), spriteSize); renderedChunks[i] = go.AddComponent <RenderedChunk>(); renderer.gameObject.SetActive(false); } chunkCache = new LRUCache <Vector3Int, RenderedChunk>(chunkCapacity, true, renderedChunks); chunkCache.registerOnRemoveMethod(OnReleaseChunk); Load(SceneData.Instance.isNewMap); }
public override void Run() { if (unloadChunkPos != -1) { #if !CITO unchecked { #endif RenderedChunk c = game.map.chunks[unloadChunkPos].rendered; for (int k = 0; k < c.idsCount; k++) { int loadedSubmesh = c.ids[k]; game.d_Batcher.Remove(loadedSubmesh); } c.ids = null; c.dirty = true; c.light = null; #if !CITO } #endif unloadChunkPos = -1; } }
//todo: use this for chunk byte[], not just for terrain renderer meshes. public void Remap(Vector3i newMapPosition) { //make a list of old chunks var newRendererMap = new RenderedChunk[RendererMap.Length]; Dictionary<Vector3i, RenderedChunk> oldChunks = new Dictionary<Vector3i, RenderedChunk>(); for (int x = 0; x < mapAreaSize / chunksize; x++) { for (int y = 0; y < mapAreaSize / chunksize; y++) { for (int z = 0; z < MapSizeZ / chunksize; z++) { int pos = MapUtil.Index3d(x, y, z, mapAreaSize / chunksize, mapAreaSize / chunksize); int chunkx = x + CurrentRendererMapPositionG.x / chunksize; int chunky = y + CurrentRendererMapPositionG.y / chunksize; int chunkz = z + CurrentRendererMapPositionG.z / chunksize; Vector3i pos2 = new Vector3i(chunkx, chunky, chunkz); oldChunks[pos2] = RendererMap[pos]; } } } for (int x = 0; x < mapAreaSize / chunksize; x++) { for (int y = 0; y < mapAreaSize / chunksize; y++) { for (int z = 0; z < MapSizeZ / chunksize; z++) { int pos = MapUtil.Index3d(x, y, z, mapAreaSize / chunksize, mapAreaSize / chunksize); int newchunkx = x + newMapPosition.x / chunksize; int newchunky = y + newMapPosition.y / chunksize; int newchunkz = z + newMapPosition.z / chunksize; Vector3i pos2 = new Vector3i(newchunkx, newchunky, newchunkz); if (oldChunks.ContainsKey(pos2)) { //if already loaded newRendererMap[pos] = oldChunks[pos2]; oldChunks[pos2] = null; } else { //if needs loading newRendererMap[pos] = new RenderedChunk(); } } } } foreach (var k in oldChunks) { //wasn't used in new area. if (k.Value != null && k.Value.ids != null) { foreach (var subMeshId in k.Value.ids) { d_Batcher.Remove(subMeshId); } } //todo: save to disk } for (int i = 0; i < newRendererMap.Length; i++) { RendererMap[i] = newRendererMap[i]; } }
void CalculateShadows(int cx, int cy, int cz) { #if !CITO unchecked { #endif for (int i = 0; i < GlobalVar.MAX_BLOCKTYPES; i++) { if (game.blocktypes[i] == null) { continue; } CalculateShadowslightRadius[i] = game.blocktypes[i].LightRadius; CalculateShadowsisTransparentForLight[i] = IsTransparentForLight(i); } for (int xx = 0; xx < 3; xx++) { int cx1 = cx + xx - 1; int cx1Chunked = cx1 * chunksize; for (int yy = 0; yy < 3; yy++) { int cy1 = cy + yy - 1; int cy1Chunked = cy1 * chunksize; for (int zz = 0; zz < 3; zz++) { int cz1 = cz + zz - 1; if (!game.map.IsValidChunkPos(cx1, cy1, cz1)) { continue; } Chunk c = game.map.GetChunk(cx1Chunked, cy1Chunked, cz1 * chunksize); if (c.baseLightDirty) { lightBase.CalculateChunkBaseLight(game, cx1, cy1, cz1, CalculateShadowslightRadius, CalculateShadowsisTransparentForLight); c.baseLightDirty = false; } } } } RenderedChunk chunk = game.map.GetChunk(cx * chunksize, cy * chunksize, cz * chunksize).rendered; if (chunk.light == null) { chunk.light = new byte[18 * 18 * 18]; for (int i = 0; i < 18 * 18 * 18; i++) { chunk.light[i] = 15; } } lightBetweenChunks.CalculateLightBetweenChunks(game, cx, cy, cz, CalculateShadowslightRadius, CalculateShadowsisTransparentForLight); for (int i = 0; i < 18 * 18 * 18; i++) { currentChunkShadows[i] = chunk.light[i]; } #if !CITO } #endif }
private void OnReleaseChunk(Vector3Int pos, RenderedChunk renderedChunk) { renderedChunk.Clear(); //Debug.Log("Release " + pos + ", " + renderedChunk); }
//todo: use this for chunk byte[], not just for terrain renderer meshes. public void Remap(Vector3i newMapPosition) { //make a list of old chunks var newRendererMap = new RenderedChunk[RendererMap.Length]; Dictionary <Vector3i, RenderedChunk> oldChunks = new Dictionary <Vector3i, RenderedChunk>(); for (int x = 0; x < mapAreaSize / chunksize; x++) { for (int y = 0; y < mapAreaSize / chunksize; y++) { for (int z = 0; z < MapSizeZ / chunksize; z++) { int pos = MapUtil.Index3d(x, y, z, mapAreaSize / chunksize, mapAreaSize / chunksize); int chunkx = x + CurrentRendererMapPositionG.x / chunksize; int chunky = y + CurrentRendererMapPositionG.y / chunksize; int chunkz = z + CurrentRendererMapPositionG.z / chunksize; Vector3i pos2 = new Vector3i(chunkx, chunky, chunkz); oldChunks[pos2] = RendererMap[pos]; } } } for (int x = 0; x < mapAreaSize / chunksize; x++) { for (int y = 0; y < mapAreaSize / chunksize; y++) { for (int z = 0; z < MapSizeZ / chunksize; z++) { int pos = MapUtil.Index3d(x, y, z, mapAreaSize / chunksize, mapAreaSize / chunksize); int newchunkx = x + newMapPosition.x / chunksize; int newchunky = y + newMapPosition.y / chunksize; int newchunkz = z + newMapPosition.z / chunksize; Vector3i pos2 = new Vector3i(newchunkx, newchunky, newchunkz); if (oldChunks.ContainsKey(pos2)) { //if already loaded newRendererMap[pos] = oldChunks[pos2]; oldChunks[pos2] = null; } else { //if needs loading newRendererMap[pos] = new RenderedChunk(); } } } } foreach (var k in oldChunks) { //wasn't used in new area. if (k.Value != null && k.Value.ids != null) { foreach (var subMeshId in k.Value.ids) { d_Batcher.Remove(subMeshId); } } //todo: save to disk } for (int i = 0; i < newRendererMap.Length; i++) { RendererMap[i] = newRendererMap[i]; } }