実現したいこと
Unityにおいてサンドボックスのゲームを作りたいが
地形の生成時に処理の負荷がかかるため高速化したい
大規模なサンドボックスゲームを作る場合は
ブロックのデータはどのように管理するのが高速なのかご教授願いたい
前提
ワールドはチャンクを縦横に敷き詰められており
チャンクには16*16(*32)のブロックのデータがある
それらのチャンクのデータはCChunkにまとめられており
ゲッターであるGetBlock()にてブロックデータを取得する
しかしメッシュ生成の都合上、周囲のブロックを参照することが多く
チャンクを跨いでデータを取得する必要が出てきた
そのため新たにGetBlockModを作成したが
これは引数として周辺のチャンクを渡していて
必要に応じて隣のチャンクからデータを参照している
該当のソースコード
C#
1public class CChunk 2{3 //ブロックの配列データ4 public int[][][] mBlock;5 ・・・その他色々なチャンクに必要な雑多な情報 6 7 8 //ブロックデータのゲッター9 public int GetBlock(int z, int y, int x)10 {11 12 if (x >= Define.CHUNK_SIZE_X) return 0;13 if (y >= Define.CHUNK_SIZE_Y) return 0;14 if (z >= Define.CHUNK_SIZE_Z) return 0;15 16 if (x < 0) return 0;17 if (y < 0) return 0;18 if (z < 0) return 0;19 20 return mBlock[z][y][x];21 }22 23 public int GetBlockMod(int z, int y, int x, 24 in CChunk p_x, in CChunk m_x,25 in CChunk p_z, in CChunk m_z)26 {27 //----------28 //プラス方向29 //----------30 if (x >= Define.CHUNK_SIZE_X)31 {32 if ( p_x != null )33 {34 return p_x.GetBlock(z, y, x - Define.CHUNK_SIZE_X);35 }36 else 37 {38 return (int)BLOCK_ID.VOID;39 }40 }41 if (y >= Define.CHUNK_SIZE_Y)42 {43 if (p_z != null)44 {45 return p_z.GetBlock(z, y - Define.CHUNK_SIZE_Y, x);46 }47 else48 {49 return (int)BLOCK_ID.VOID;50 }51 }52 if (z >= Define.CHUNK_SIZE_Z)53 {54 return (int)BLOCK_ID.VOID;55 }56 //----------57 //マイナス方向58 //----------59 if (x < 0)60 {61 if (m_x != null)62 {63 return m_x.GetBlock( z, y, Define.CHUNK_SIZE_X + x );64 }65 else66 {67 return (int)BLOCK_ID.VOID;68 }69 }70 if (y < 0)71 {72 if (m_z != null)73 {74 return m_z.GetBlock(z, Define.CHUNK_SIZE_Y + y, x);75 }76 else77 {78 return (int)BLOCK_ID.VOID;79 }80 }81 if (z < 0)82 {83 //下方向だけブロックにする84 return (int)BLOCK_ID.VOID;85 }86 return mBlock[z][y][x];87 }88}

0 コメント