示例#1
0
        // ==== maintenance ===========================================================================================

        public void Awake()
        {
            chunkIndex = new Index(transform.position);

            Size = Engine.ChunkSize;

            neighborChunks = new Chunk[6]; // 0 = up, 1 = down, 2 = right, 3 = left, 4 = forward, 5 = back
            MeshCreator    = GetComponent <ChunkMeshCreator>();
            fresh          = true;

            // Register chunk
            ChunkManager.RegisterChunk(this);

            // Clear the voxel data
            voxelData = new ushort[sizeX * sizeY * sizeZ];

            // Set actual position
            Vector3 idx = chunkIndex.ToVector3();
            Vector3 s   = transform.localScale;

            transform.position = new Vector3(
                idx.x * sizeX * s.x,
                idx.y * sizeY * s.y,
                idx.z * sizeZ * s.z);

            // Grab voxel data
            if (Engine.EnableMultiplayer && !Network.isServer)
            {
                StartCoroutine(RequestVoxelData()); // if multiplayer, get data from server
            }
            else if (Engine.SaveVoxelData && TryLoadVoxelData())
            {
                // data is loaded through TryLoadVoxelData()
            }
            else
            {
                GenerateVoxelData();
            }
        }
示例#2
0
        // ==== maintenance ===========================================================================================

        public void Awake()
        { // chunk initialization (load/generate data, set position, etc.)
            // Set variables
            ChunkIndex        = new Index(transform.position);
            SideLength        = Engine.ChunkSideLength;
            SquaredSideLength = SideLength * SideLength;
            NeighborChunks    = new Chunk[6]; // 0 = up, 1 = down, 2 = right, 3 = left, 4 = forward, 5 = back
            MeshCreator       = GetComponent <ChunkMeshCreator>();
            Fresh             = true;

            // Register chunk
            ChunkManager.RegisterChunk(this);

            // Clear the voxel data
            VoxelData = new ushort[SideLength * SideLength * SideLength];

            // Set actual position
            transform.position = ChunkIndex.ToVector3() * SideLength;

            // multiply by scale
            transform.position = new Vector3(transform.position.x * transform.localScale.x, transform.position.y * transform.localScale.y, transform.position.z * transform.localScale.z);

            // Grab voxel data

            /*if (Engine.EnableMultiplayer && !Network.isServer) {
             *  StartCoroutine (RequestVoxelData());	// if multiplayer, get data from server
             * }
             * else */
            if (Engine.SaveVoxelData && TryLoadVoxelData() == true)
            {
                // data is loaded through TryLoadVoxelData()
            }
            else
            {
                GenerateVoxelData();
            }
        }
示例#3
0
        public void Update()
        {
            // don't load chunks if engine isn't initialized yet
            if (!Engine.Initialized || !ChunkManager.Initialized)
            {
                return;
            }

            // don't load chunks if multiplayer is enabled but the connection isn't established yet
            if (Engine.EnableMultiplayer)
            {
                if (!Network.isClient && !Network.isServer)
                {
                    return;
                }
            }



            // track which chunk we're currently in. If it's different from previous frame, spawn chunks at current position.

            currentPos = Engine.PositionToChunkIndex(transform.position);

            if (currentPos.IsEqual(LastPos) == false)
            {
                ChunkManager.SpawnChunks(currentPos.x, currentPos.y, currentPos.z);

                // (Multiplayer) update server position
                if (Engine.EnableMultiplayer && Engine.MultiplayerTrackPosition && Engine.UniblocksNetwork != null)
                {
                    UniblocksClient.UpdatePlayerPosition(currentPos);
                }
            }

            LastPos = currentPos;
        }
示例#4
0
 private void OnDestroy()
 {
     ChunkManager.UnregisterChunk(this);
 }
示例#5
0
        private IEnumerator SpawnMissingChunks(int originX, int originY, int originZ, int range)
        {
            int heightRange = Engine.HeightRange;

            ChunkUpdateQueue = new List <Chunk>();            // clear update queue - it will be repopulated again in the correct order in the following loop

            // flag chunks not in range for removal
            ChunksToDestroy = new List <Chunk>();
            foreach (Chunk chunk in Chunks.Values)
            {
                if (Vector2.Distance(new Vector2(chunk.ChunkIndex.x, chunk.ChunkIndex.z), new Vector2(originX, originZ)) > range + Engine.ChunkDespawnDistance)
                {
                    ChunksToDestroy.Add(chunk);
                }

                else if (Mathf.Abs(chunk.ChunkIndex.y - originY) > range + Engine.ChunkDespawnDistance)
                {                 // destroy chunks outside of vertical range
                    ChunksToDestroy.Add(chunk);
                }
            }


            // main loop
            for (int currentLoop = 0; currentLoop <= range; currentLoop++)
            {
                for (var x = originX - currentLoop; x <= originX + currentLoop; x++)
                {                 // iterate through all potential chunk indexes within range
                    for (var y = originY - currentLoop; y <= originY + currentLoop; y++)
                    {
                        for (var z = originZ - currentLoop; z <= originZ + currentLoop; z++)
                        {
                            if (Mathf.Abs(y) <= heightRange)
                            {                             // skip chunks outside of height range
                                if (Mathf.Abs(originX - x) + Mathf.Abs(originZ - z) < range * 1.3f)
                                {                         // skip corners
                                    // pause loop while the queue is not empty
                                    while (ChunkUpdateQueue.Count > 0)
                                    {
                                        ProcessChunkQueue();
                                        if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration)
                                        {
                                            yield return(new WaitForEndOfFrame());
                                        }
                                    }

                                    Chunk currentChunk = ChunkManager.GetChunkComponent(x, y, z);


                                    // chunks that already exist but haven't had their mesh built yet should be added to the update queue
                                    if (currentChunk != null)
                                    {
                                        // chunks without meshes spawned by server should be changed to regular chunks
                                        if (currentChunk.DisableMesh || currentChunk.EnableTimeout)
                                        {
                                            currentChunk.DisableMesh   = false;
                                            currentChunk.EnableTimeout = false;
                                            currentChunk.Fresh         = true;
                                        }

                                        if (currentChunk.Fresh)
                                        {
                                            // spawn neighbor chunks
                                            for (int d = 0; d < 6; d++)
                                            {
                                                Index      neighborIndex = currentChunk.ChunkIndex.GetAdjacentIndex((Direction)d);
                                                GameObject neighborChunk = GetChunk(neighborIndex);
                                                if (neighborChunk == null)
                                                {
                                                    neighborChunk = Instantiate(ChunkObject, neighborIndex.ToVector3(), transform.rotation) as GameObject;
                                                }
                                                currentChunk.NeighborChunks[d] = neighborChunk.GetComponent <Chunk>();                                                // always add the neighbor to NeighborChunks, in case it's not there already

                                                // continue loop in next frame if the current frame time is exceeded
                                                if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration)
                                                {
                                                    yield return(new WaitForEndOfFrame());
                                                }
                                                if (StopSpawning)
                                                {
                                                    EndSequence();
                                                    yield break;
                                                }
                                            }

                                            if (currentChunk != null)
                                            {
                                                currentChunk.AddToQueueWhenReady();
                                            }
                                        }
                                    }

                                    else                                       // if chunk doesn't exist, create new chunk (it adds itself to the update queue when its data is ready)

                                    // spawn chunk
                                    {
                                        GameObject newChunk = Instantiate(ChunkObject, new Vector3(x, y, z), transform.rotation) as GameObject;                                         // Spawn a new chunk.
                                        currentChunk = newChunk.GetComponent <Chunk>();

                                        // spawn neighbor chunks if they're not spawned yet
                                        for (int d = 0; d < 6; d++)
                                        {
                                            Index      neighborIndex = currentChunk.ChunkIndex.GetAdjacentIndex((Direction)d);
                                            GameObject neighborChunk = GetChunk(neighborIndex);
                                            if (neighborChunk == null)
                                            {
                                                neighborChunk = Instantiate(ChunkObject, neighborIndex.ToVector3(), transform.rotation) as GameObject;
                                            }
                                            currentChunk.NeighborChunks[d] = neighborChunk.GetComponent <Chunk>();                                            // always add the neighbor to NeighborChunks, in case it's not there already

                                            // continue loop in next frame if the current frame time is exceeded
                                            if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration)
                                            {
                                                yield return(new WaitForEndOfFrame());
                                            }
                                            if (StopSpawning)
                                            {
                                                EndSequence();
                                                yield break;
                                            }
                                        }

                                        if (currentChunk != null)
                                        {
                                            currentChunk.AddToQueueWhenReady();
                                        }
                                    }
                                }
                            }



                            // continue loop in next frame if the current frame time is exceeded
                            if (frameStopwatch.Elapsed.TotalSeconds >= targetFrameDuration)
                            {
                                yield return(new WaitForEndOfFrame());
                            }
                            if (StopSpawning)
                            {
                                EndSequence();
                                yield break;
                            }
                        }
                    }
                }
            }

            yield return(new WaitForEndOfFrame());

            EndSequence();
        }
示例#6
0
        void Awake()
        {
            EngineInstance       = this;
            ChunkManagerInstance = GetComponent <ChunkManager>();

            WorldName = lWorldName;
            UpdateWorldPath();

            BlocksPath = lBlocksPath;
            Blocks     = lBlocks;

            TargetFPS            = lTargetFPS;
            MaxChunkSaves        = lMaxChunkSaves;
            MaxChunkDataRequests = lMaxChunkDataRequests;

            TextureUnit              = lTextureUnit;
            TexturePadding           = lTexturePadding;
            GenerateColliders        = lGenerateColliders;
            ShowBorderFaces          = lShowBorderFaces;
            EnableMultiplayer        = lEnableMultiplayer;
            MultiplayerTrackPosition = lMultiplayerTrackPosition;
            SaveVoxelData            = lSaveVoxelData;
            GenerateMeshes           = lGenerateMeshes;

            ChunkSpawnDistance   = lChunkSpawnDistance;
            HeightRange          = lHeightRange;
            ChunkDespawnDistance = lChunkDespawnDistance;

            SendCameraLookEvents = lSendCameraLookEvents;
            SendCursorEvents     = lSendCursorEvents;

            ChunkSize = new Index3(lChunkSizeX, lChunkSizeY, lChunkSizeZ);

            ChunkDataFiles.LoadedRegions = new Dictionary <string, string[]>();
            ChunkDataFiles.TempChunkData = new Dictionary <string, string>();

            if (lChunkTimeout <= 0.00001f)
            {
                EnableChunkTimeout = false;
            }
            else
            {
                EnableChunkTimeout = true;
                ChunkTimeout       = lChunkTimeout;
            }

            /*if (Application.isWebPlayer)
             * {
             *  lSaveVoxelData = SaveVoxelData = false;
             * }*/


            // set layer collision
            if (LayerMask.LayerToName(NO_COLLIDE_LAYER) != string.Empty)
            {
                Debug.LogWarning("Uniblocks: " + NO_COLLIDE_LAYER + " is reserved for Uniblocks; it is automatically set to ignore collision with all layers.");
            }
            for (int i = 0; i < 31; i++)
            {
                Physics.IgnoreLayerCollision(i, NO_COLLIDE_LAYER);
            }


            PerformValidationChecks();

            Initialized = true;
        }
示例#7
0
        // ==== initialization ====
        public void Awake()
        {
            Engine.EngineInstance       = this;
            Engine.ChunkManagerInstance = GetComponent <ChunkManager>();

            WorldName = lWorldName;
            UpdateWorldPath();

            BlocksPath    = lBlocksPath;
            Engine.Blocks = lBlocks;

            TargetFPS            = lTargetFPS;
            MaxChunkSaves        = lMaxChunkSaves;
            MaxChunkDataRequests = lMaxChunkDataRequests;

            TextureUnit              = lTextureUnit;
            TexturePadding           = lTexturePadding;
            GenerateColliders        = lGenerateColliders;
            ShowBorderFaces          = lShowBorderFaces;
            EnableMultiplayer        = lEnableMultiplayer;
            MultiplayerTrackPosition = lMultiplayerTrackPosition;
            EnableInternetplayer     = lEnableInternetplayer;
            SaveVoxelData            = lSaveVoxelData;
            GenerateMeshes           = lGenerateMeshes;

            ChunkSpawnDistance   = lChunkSpawnDistance;
            HeightRange          = lHeightRange;
            ChunkDespawnDistance = lChunkDespawnDistance;

            SendCameraLookEvents = lSendCameraLookEvents;
            SendCursorEvents     = lSendCursorEvents;

            ChunkSideLength   = lChunkSideLength;
            SquaredSideLength = lChunkSideLength * lChunkSideLength;

            ChunkDataFiles.LoadedRegions = new Dictionary <string, string[]>();
            ChunkDataFiles.TempChunkData = new Dictionary <string, string>();

            if (lChunkTimeout <= 0.00001f)
            {
                EnableChunkTimeout = false;
            }
            else
            {
                EnableChunkTimeout = true;
                ChunkTimeout       = lChunkTimeout;
            }

            if (Application.isWebPlayer)
            {
                lSaveVoxelData = false;
                SaveVoxelData  = false;
            }


            // set layer
            if (LayerMask.LayerToName(26) != "" && LayerMask.LayerToName(26) != "UniblocksNoCollide")
            {
                Debug.LogWarning("Uniblocks: Layer 26 is reserved for Uniblocks; it is automatically set to ignore collision with all layers.");
            }
            for (int i = 0; i < 31; i++)
            {
                Physics.IgnoreLayerCollision(i, 26);
            }
            ///不能即是广域网游戏又是局域网游戏
            if (Engine.EnableInternetplayer && EnableInternetplayer == EnableMultiplayer)
            {
                Debug.LogError("Uniblocks: Can not be internetplayer or multiplayer meanwhile.");
                Debug.Break();
            }

            // check block array
            if (Engine.Blocks.Length < 1)
            {
                Debug.LogError("Uniblocks: The blocks array is empty! Use the Block Editor to update the blocks array.");
                Debug.Break();
            }

            if (Engine.Blocks[0] == null)
            {
                Debug.LogError("Uniblocks: Cannot find the empty block prefab (id 0)!");
                Debug.Break();
            }
            else if (Engine.Blocks[0].GetComponent <Voxel>() == null)
            {
                Debug.LogError("Uniblocks: Voxel id 0 does not have the Voxel component attached!");
                Debug.Break();
            }

            // check settings
            if (Engine.ChunkSideLength < 1)
            {
                Debug.LogError("Uniblocks: Chunk side length must be greater than 0!");
                Debug.Break();
            }

            if (Engine.ChunkSpawnDistance < 1)
            {
                Engine.ChunkSpawnDistance = 0;
                Debug.LogWarning("Uniblocks: Chunk spawn distance is 0. No chunks will spawn!");
            }

            if (Engine.HeightRange < 0)
            {
                Engine.HeightRange = 0;
                Debug.LogWarning("Uniblocks: Chunk height range can't be a negative number! Setting chunk height range to 0.");
            }

            if (Engine.MaxChunkDataRequests < 0)
            {
                Engine.MaxChunkDataRequests = 0;
                Debug.LogWarning("Uniblocks: Max chunk data requests can't be a negative number! Setting max chunk data requests to 0.");
            }

            // check materials
            GameObject chunkPrefab   = GetComponent <ChunkManager>().ChunkObject;
            int        materialCount = chunkPrefab.GetComponent <Renderer>().sharedMaterials.Length - 1;

            for (ushort i = 0; i < Engine.Blocks.Length; i++)
            {
                if (Engine.Blocks[i] != null)
                {
                    Voxel voxel = Engine.Blocks[i].GetComponent <Voxel>();

                    if (voxel.VSubmeshIndex < 0)
                    {
                        Debug.LogError("Uniblocks: Voxel " + i + " has a material index lower than 0! Material index must be 0 or greater.");
                        Debug.Break();
                    }

                    if (voxel.VSubmeshIndex > materialCount)
                    {
                        Debug.LogError("Uniblocks: Voxel " + i + " uses material index " + voxel.VSubmeshIndex + ", but the chunk prefab only has " + (materialCount + 1) + " material(s) attached. Set a lower material index or attach more materials to the chunk prefab.");
                        Debug.Break();
                    }
                }
            }

            // check anti-aliasing
            if (QualitySettings.antiAliasing > 0)
            {
                Debug.LogWarning("Uniblocks: Anti-aliasing is enabled. This may cause seam lines to appear between blocks. If you see lines between blocks, try disabling anti-aliasing, switching to deferred rendering path, or adding some texture padding in the engine settings.");
            }


            Engine.Initialized = true;
        }
示例#8
0
        // ==== initialization ====
        public void Awake()
        {
            Engine.EngineInstance = this;
            Engine.ChunkManagerInstance = GetComponent<ChunkManager>();

            WorldName = lWorldName;
            UpdateWorldPath();

            BlocksPath = lBlocksPath;
            Engine.Blocks = lBlocks;

            TargetFPS = lTargetFPS;
            MaxChunkSaves = lMaxChunkSaves;
            MaxChunkDataRequests = lMaxChunkDataRequests;

            TextureUnit = lTextureUnit;
            TexturePadding = lTexturePadding;
            GenerateColliders = lGenerateColliders;
            ShowBorderFaces = lShowBorderFaces;
            EnableMultiplayer = lEnableMultiplayer;
            MultiplayerTrackPosition = lMultiplayerTrackPosition;
            SaveVoxelData = lSaveVoxelData;
            GenerateMeshes = lGenerateMeshes;

            ChunkSpawnDistance = lChunkSpawnDistance;
            HeightRange = lHeightRange;
            ChunkDespawnDistance = lChunkDespawnDistance;

            SendCameraLookEvents = lSendCameraLookEvents;
            SendCursorEvents = lSendCursorEvents;

            ChunkSideLength = lChunkSideLength;
            SquaredSideLength = lChunkSideLength * lChunkSideLength;

            ChunkDataFiles.LoadedRegions = new Dictionary<string, string[]>();
            ChunkDataFiles.TempChunkData = new Dictionary<string, string>();

            if (lChunkTimeout <= 0.00001f)	{
            EnableChunkTimeout = false;
            }
            else {
            EnableChunkTimeout = true;
            ChunkTimeout = lChunkTimeout;
            }

            if (Application.isWebPlayer) {
            lSaveVoxelData = false;
            SaveVoxelData = false;
            }

            // set layer
            if (LayerMask.LayerToName (26) != "" && LayerMask.LayerToName (26) != "UniblocksNoCollide") {
            Debug.LogWarning ("Uniblocks: Layer 26 is reserved for Uniblocks; it is automatically set to ignore collision with all layers.");
            }
            for (int i=0; i<31; i++) {
            Physics.IgnoreLayerCollision (i, 26);
            }

            // check block array
            if (Engine.Blocks.Length < 1) {
            Debug.LogError ("Uniblocks: The blocks array is empty! Use the Block Editor to update the blocks array.");
            Debug.Break();
            }

            if (Engine.Blocks[0] == null) {
            Debug.LogError ("Uniblocks: Cannot find the empty block prefab (id 0)!");
            Debug.Break();
            }
            else if (Engine.Blocks[0].GetComponent<Voxel>() == null) {
            Debug.LogError ("Uniblocks: Voxel id 0 does not have the Voxel component attached!");
            Debug.Break();
            }

            // check settings
            if (Engine.ChunkSideLength < 1) {
            Debug.LogError ("Uniblocks: Chunk side length must be greater than 0!");
            Debug.Break();
            }

            if (Engine.ChunkSpawnDistance < 1) {
            Engine.ChunkSpawnDistance = 0;
            Debug.LogWarning ("Uniblocks: Chunk spawn distance is 0. No chunks will spawn!");
            }

            if (Engine.HeightRange < 0) {
            Engine.HeightRange = 0;
            Debug.LogWarning ("Uniblocks: Chunk height range can't be a negative number! Setting chunk height range to 0.");
            }

            if (Engine.MaxChunkDataRequests < 0) {
            Engine.MaxChunkDataRequests = 0;
            Debug.LogWarning ("Uniblocks: Max chunk data requests can't be a negative number! Setting max chunk data requests to 0.");
            }

            // check materials
            GameObject chunkPrefab = GetComponent<ChunkManager>().ChunkObject;
            int materialCount = chunkPrefab.GetComponent<Renderer>().sharedMaterials.Length-1;

            for (ushort i=0; i<Engine.Blocks.Length; i++) {

            if (Engine.Blocks[i] != null) {
                Voxel voxel = Engine.Blocks[i].GetComponent<Voxel>();

                if (voxel.VSubmeshIndex < 0) {
                    Debug.LogError ("Uniblocks: Voxel "+i+" has a material index lower than 0! Material index must be 0 or greater.");
                    Debug.Break();
                }

                if (voxel.VSubmeshIndex > materialCount) {
                    Debug.LogError ("Uniblocks: Voxel "+i+" uses material index "+voxel.VSubmeshIndex +", but the chunk prefab only has "+(materialCount+1)+ " material(s) attached. Set a lower material index or attach more materials to the chunk prefab.");
                    Debug.Break();
                }
            }
            }

            // check anti-aliasing
            if (QualitySettings.antiAliasing > 0) {
            Debug.LogWarning ("Uniblocks: Anti-aliasing is enabled. This may cause seam lines to appear between blocks. If you see lines between blocks, try disabling anti-aliasing, switching to deferred rendering path, or adding some texture padding in the engine settings.");
            }

            Engine.Initialized = true;
        }