예제 #1
0
        private void Awake()
        {
            if (dontDestroyOnLoad)
            {
                DontDestroyOnLoad(gameObject);
            }

            // There's a chance there are multiple worlds active currently.
            // If there is, stop here.
            if (Main != null && Main != this)
            {
                return;
            }

            if (!BlockProvider.IsInitialized)
            {
                BlockProvider.Initialize(blockCollection);
            }

            if (!TextureProvider.IsInitialized)
            {
                TextureProvider.Initialize(blockCollection);
            }

            if (!Serialization.IsInitialized)
            {
                Serialization.Initialize(Application.persistentDataPath + "/HertzVox/");
            }

            Texture2D atlas = TextureProvider.GetAtlas();

            mat = new Material(chunkMaterial)
            {
                mainTexture = atlas
            };

            int xSize = atlas.width / blockCollection.TextureSize;
            int ySize = atlas.height / blockCollection.TextureSize;

            mat.SetInt("_AtlasX", xSize);
            mat.SetInt("_AtlasY", ySize);
            mat.SetVector("_AtlasRec", new Vector4(1.0f / xSize, 1.0f / ySize));

            generateJobs = new NativeHashMap <int3, ChunkJobData>(0, Allocator.Persistent);
            renderJobs   = new NativeHashMap <int3, ChunkJobData>(0, Allocator.Persistent);
            colliderJobs = new NativeHashMap <int3, ChunkJobData>(0, Allocator.Persistent);

            renderChunks   = new NativeList <int3>(Allocator.Persistent);
            chunksToRemove = new NativeList <int3>(Allocator.Persistent);

            generator = GetComponent <IVoxGeneration>();

            if (generator == null)
            {
                Debug.LogWarning("There's no voxel generator (IVoxGenerator) attached to Voxel World " + gameObject.name + ". No chunks will show up.");
            }
        }
예제 #2
0
        public JobHandle ScheduleRenderJob()
        {
            UpdatingRenderer = true;
            HasRender        = true;

            vertices = new NativeList <float3>(Allocator.TempJob);
            indicies = new NativeList <int>(Allocator.TempJob);
            uvs      = new NativeList <float4>(Allocator.TempJob);
            colors   = new NativeList <float4>(Allocator.TempJob);
            normals  = new NativeList <float3>(Allocator.TempJob);

            NativeArray <int> northBlocks = world.TryGetChunk(new int3(position.x, position.y, position.z + CHUNK_SIZE), out Chunk northChunk) ?
                                            northChunk.blocks.GetBlocks(Allocator.TempJob) : BlockProvider.GetEmptyBlocks(Allocator.TempJob);

            NativeArray <int> southBlocks = world.TryGetChunk(new int3(position.x, position.y, position.z - CHUNK_SIZE), out Chunk southChunk) ?
                                            southChunk.blocks.GetBlocks(Allocator.TempJob) : BlockProvider.GetEmptyBlocks(Allocator.TempJob);

            NativeArray <int> eastBlocks = world.TryGetChunk(new int3(position.x + CHUNK_SIZE, position.y, position.z), out Chunk eastChunk) ?
                                           eastChunk.blocks.GetBlocks(Allocator.TempJob) : BlockProvider.GetEmptyBlocks(Allocator.TempJob);

            NativeArray <int> westBlocks = world.TryGetChunk(new int3(position.x - CHUNK_SIZE, position.y, position.z), out Chunk westChunk) ?
                                           westChunk.blocks.GetBlocks(Allocator.TempJob) : BlockProvider.GetEmptyBlocks(Allocator.TempJob);

            NativeArray <int> upBlocks = world.TryGetChunk(new int3(position.x, position.y + CHUNK_SIZE, position.z), out Chunk topChunk) ?
                                         topChunk.blocks.GetBlocks(Allocator.TempJob) : BlockProvider.GetEmptyBlocks(Allocator.TempJob);

            NativeArray <int> downBlocks = world.TryGetChunk(new int3(position.x, position.y - CHUNK_SIZE, position.z), out Chunk downChunk) ?
                                           downChunk.blocks.GetBlocks(Allocator.TempJob) : BlockProvider.GetEmptyBlocks(Allocator.TempJob);

            return(new BuildChunkJob()
            {
                chunkSize = CHUNK_SIZE,
                position = position,
                blocks = blocks.GetBlocks(Allocator.TempJob),
                blockMap = BlockProvider.GetBlockMap(),
                textures = TextureProvider.GetTextureMap(),
                vertices = vertices,
                indicies = indicies,
                uvs = uvs,
                colors = colors,
                normals = normals,
                northBlocks = northBlocks,
                southBlocks = southBlocks,
                eastBlocks = eastBlocks,
                westBlocks = westBlocks,
                upBlocks = upBlocks,
                downBlocks = downBlocks
            }.Schedule());
        }
예제 #3
0
        private void OnDestroy()
        {
            // Make sure only the active world disposes of things.
            if (Main != null && Main != this)
            {
                return;
            }

            if (generatingChunks)
            {
                FinishGeneratingChunks();
            }

            DisposeJobList(generateJobs);
            DisposeJobList(renderJobs);
            DisposeJobList(colliderJobs);

            foreach (Chunk chunk in chunks.Values)
            {
                chunk.Dispose(true);
            }

            TextureProvider.Dispose();
            BlockProvider.Dispose();

            Destroy(mat);

            generateJobs.Dispose();
            renderJobs.Dispose();
            colliderJobs.Dispose();

            renderChunks.Dispose();
            chunksToRemove.Dispose();

            if (clearTempOnDestroy)
            {
                Serialization.ClearTemp();
            }
        }