コード例 #1
0
        public void SetBlock(Vector3Int worldPosition, IBlock block)
        {
            Vector3Int chunkIndex    = WorldChunksData.WorldPostionToChunkIndex(worldPosition);
            Vector3Int chunkIndexRUF = WorldChunksData.WorldPostionToChunkIndex(worldPosition + Vector3Int.one);
            Vector3Int chunkIndexLDB = WorldChunksData.WorldPostionToChunkIndex(worldPosition - Vector3Int.one);
            Dictionary <Vector3Int, ChunkLoadType> chunksToRefresh =
                new Dictionary <Vector3Int, ChunkLoadType>()
            {
                { chunkIndex, ChunkLoadType.dynamicEnabled }
            };

            if (chunkIndex.x < chunkIndexRUF.x)
            {
                RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(1, 0, 0));
            }
            if (chunkIndex.y < chunkIndexRUF.y)
            {
                RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(0, 1, 0));
            }
            if (chunkIndex.z < chunkIndexRUF.z)
            {
                RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(0, 0, 1));
            }
            if (chunkIndex.x > chunkIndexLDB.x)
            {
                RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(-1, 0, 0));
            }
            if (chunkIndex.y > chunkIndexLDB.y)
            {
                RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(0, -1, 0));
            }
            if (chunkIndex.z > chunkIndexLDB.z)
            {
                RefreshChunkIfNeighbourBlockNotEmpty(new Vector3Int(0, 0, -1));
            }

            WorldChunksData.SetBlock(worldPosition, block);
            ActualizeDisplayedChunks(chunksToRefresh);

            void RefreshChunkIfNeighbourBlockNotEmpty(Vector3Int offet)
            {
                if (WorldChunksData.GetBlock(worldPosition + offet) != null)
                {
                    chunksToRefresh.Add(chunkIndex + offet, ChunkLoadType.dynamicEnabled);
                }
            }
        }
コード例 #2
0
ファイル: UTests.cs プロジェクト: TED-inc/UniBlocks
        public static void WorldChunksDataTest()
        {
            Debug.Log("Test of: " + nameof(WorldChunksData.WorldPostionToChunkIndex));
            Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(-12, -11, -10)) == -Vector3Int.one);
            Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(0, 0, 0)) == Vector3Int.zero);
            Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(15, 12, 10)) == Vector3Int.zero);
            Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(15, 16, 10)) == Vector3Int.up);
            Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(15, -16, 10)) == Vector3Int.down);
            Debug.Log(WorldChunksData.WorldPostionToChunkIndex(new Vector3Int(15, -17, 10)) == Vector3Int.down * 2);

            Debug.Log("Test of: " + nameof(WorldChunksData.WorldToLocalPostion));
            Vector3Int chunkIndex = new Vector3Int(-10, 15, 0);
            Vector3Int position   = WorldChunksData.WorldToLocalPostion(new Vector3Int(15, -17, 10), ref chunkIndex);

            Debug.Log(chunkIndex == new Vector3Int(-10, 13, 0));
            Debug.Log(position == new Vector3Int(15, 15, 10));

            Debug.Log("Test of: " + nameof(WorldChunksData.SetBlock) + " & " + nameof(WorldChunksData.GetBlock));
            WorldChunksData.SetBlock(Vector3Int.zero, null);
            WorldChunksData.SetBlock(Vector3Int.zero, new StoneBlock());
            Debug.Log(WorldChunksData.GetBlock(Vector3Int.zero) is StoneBlock);
        }
コード例 #3
0
        public static Mesh GenerateMesh(Vector3Int chunkIndex)
        {
            #region SetupMesh
            Mesh           mesh      = new Mesh();
            List <Vector3> vericies  = new List <Vector3>();
            List <int>     triangles = new List <int>();
            List <Vector2> uv        = new List <Vector2>();
            List <Vector3> normals   = new List <Vector3>();
            #endregion

            Fill();
            AssignMesh();

            return(mesh);



            void Fill()
            {
                for (int x = 0; x < ChunckData.chunkSize; x++)
                {
                    for (int z = 0; z < ChunckData.chunkSize; z++)
                    {
                        for (int y = 0; y < ChunckData.chunkSize; y++)
                        {
                            Vector3Int position = new Vector3Int(x, y, z);
                            SetBlock(
                                position,
                                GetVisibleSidesOfBlock(position));
                        }
                    }
                }


                BlockSides GetVisibleSidesOfBlock(Vector3Int position)
                {
                    BlockSides drawSides = 0;

                    if (WorldChunksData.GetBlock(chunkIndex, position) is SimpleBlockBase)
                    {
                        foreach (Enum value in Enum.GetValues(typeof(BlockSides)))
                        {
                            if ((BlockSides)value != BlockSides.None)
                            {
                                if (!(WorldChunksData.GetBlock(chunkIndex, position + BlockSidesUtils.GetNormal((BlockSides)value)) is SimpleBlockBase))
                                {
                                    drawSides += (int)(BlockSides)value;
                                }
                            }
                        }
                    }

                    return(drawSides);
                }

                void SetBlock(Vector3Int position, BlockSides drawSides)
                {
                    foreach (Enum value in Enum.GetValues(typeof(BlockSides)))
                    {
                        if ((BlockSides)value != BlockSides.None)
                        {
                            if (drawSides.HasFlag(value))
                            {
                                SetSide((BlockSides)value);
                            }
                        }
                    }



                    void SetSide(BlockSides drawSide)
                    {
                        SetUVandNormals();
                        SetTriangles();
                        SetVerticies();



                        void SetUVandNormals()
                        {
                            uv.Add(Vector2.zero);
                            uv.Add(Vector2.up);
                            uv.Add(Vector2.right);
                            uv.Add(Vector2.one);


                            for (int i = 0; i < 4; i++)
                            {
                                normals.Add(BlockSidesUtils.GetNormal(drawSide));
                            }
                        }

                        void SetTriangles()
                        {
                            //first triangle
                            triangles.Add(vericies.Count);
                            triangles.Add(vericies.Count + 1);
                            triangles.Add(vericies.Count + 2);
                            //second triangle
                            triangles.Add(vericies.Count + 2);
                            triangles.Add(vericies.Count + 1);
                            triangles.Add(vericies.Count + 3);
                        }

                        void SetVerticies()
                        {
                            switch (drawSide)
                            {
                            case BlockSides.Right:
                                vericies.Add(position + new Vector3(1, 0, 0));
                                vericies.Add(position + new Vector3(1, 1, 0));
                                vericies.Add(position + new Vector3(1, 0, 1));
                                vericies.Add(position + new Vector3(1, 1, 1));
                                break;

                            case BlockSides.Left:
                                vericies.Add(position + new Vector3(0, 0, 1));
                                vericies.Add(position + new Vector3(0, 1, 1));
                                vericies.Add(position + new Vector3(0, 0, 0));
                                vericies.Add(position + new Vector3(0, 1, 0));
                                break;

                            case BlockSides.Up:
                                vericies.Add(position + new Vector3(0, 1, 1));
                                vericies.Add(position + new Vector3(1, 1, 1));
                                vericies.Add(position + new Vector3(0, 1, 0));
                                vericies.Add(position + new Vector3(1, 1, 0));
                                break;

                            case BlockSides.Down:
                                vericies.Add(position + new Vector3(0, 0, 0));
                                vericies.Add(position + new Vector3(1, 0, 0));
                                vericies.Add(position + new Vector3(0, 0, 1));
                                vericies.Add(position + new Vector3(1, 0, 1));
                                break;

                            case BlockSides.Front:
                                vericies.Add(position + new Vector3(1, 0, 1));
                                vericies.Add(position + new Vector3(1, 1, 1));
                                vericies.Add(position + new Vector3(0, 0, 1));
                                vericies.Add(position + new Vector3(0, 1, 1));
                                break;

                            case BlockSides.Back:
                                vericies.Add(position + new Vector3(0, 0, 0));
                                vericies.Add(position + new Vector3(0, 1, 0));
                                vericies.Add(position + new Vector3(1, 0, 0));
                                vericies.Add(position + new Vector3(1, 1, 0));
                                break;
                            }
                        }
                    }
                }
            }

            void AssignMesh()
            {
                mesh.vertices  = vericies.ToArray();
                mesh.triangles = triangles.ToArray();
                mesh.uv        = uv.ToArray();
                mesh.normals   = normals.ToArray();
            }
        }
コード例 #4
0
 private void RefreshChunks()
 {
     WorldChunksData.ClearChunks();
     Draw();
 }