Пример #1
0
 public void SetWorldPosition(int streamerID, Entity worldEntity, int3 newCentralPosition)
 {
     if (World.EntityManager.Exists(worldEntity))
     {
         World world = World.EntityManager.GetComponentData <World>(worldEntity);
         WorldStreamSystem.StreamChunksIn(World.EntityManager, chunkSpawnSystem, world.modelID != 0,
                                          worldEntity, ref world, newCentralPosition, Bootstrap.GetRenderDistance(), Bootstrap.GetLoadDistance());
         World.EntityManager.SetComponentData(worldEntity, world);
     }
 }
Пример #2
0
        public void OnAddedStreamer(Entity player, Entity worldEntity)
        {
            Translation position      = World.EntityManager.GetComponentData <Translation>(player);
            World       world         = World.EntityManager.GetComponentData <World>(worldEntity);
            int3        chunkPosition = VoxelRaycastSystem.GetChunkPosition(
                VoxelRaycastSystem.WorldPositionToVoxelPosition(position.Value),
                world.voxelDimensions);

            chunkPosition.y = 0;
            WorldStreamSystem.StreamChunksIn(World.EntityManager, chunkSpawnSystem, world.modelID != 0,
                                             worldEntity, ref world, chunkPosition, Bootstrap.GetRenderDistance(), Bootstrap.GetLoadDistance());
            World.EntityManager.SetComponentData(worldEntity, world);
        }
Пример #3
0
        private void UpdateMap(Entity worldEntity, MapDatam map, int id)
        {
            if (maps.ContainsKey(id))
            {
                maps[id] = map;
            }
            else
            {
                maps.Add(id, map);
            }
            // initiate the biomes with tilemap values
            for (int i = 0; i < map.biomes.Count; i++)
            {
                map.biomes[i].InitializeIDs(map.tilemap);
            }
            World.EntityManager.SetComponentData(worldEntity, new Rotation {
                Value = Quaternion.Euler(map.worldRotation)
            });
            World.EntityManager.SetComponentData(worldEntity, new NonUniformScale {
                Value = map.worldScale
            });
            World world = new World
            {
                chunkIDs        = new BlitableArray <int>(0, Unity.Collections.Allocator.Persistent),
                chunkPositions  = new BlitableArray <int3>(0, Unity.Collections.Allocator.Persistent),
                scale           = map.worldScale,
                voxelDimensions = map.voxelDimensions,
                bounds          = 32
            };

            World.EntityManager.SetComponentData(worldEntity, world);
            World.EntityManager.SetComponentData(worldEntity, new ZoxID {
                id = id
            });
            if (Application.isPlaying == false)
            {
                WorldStreamSystem.StreamChunksIn(World.EntityManager, chunkSpawnSystem, world.modelID != 0,
                                                 worldEntity, ref world, int3.Zero(), Bootstrap.GetRenderDistance(), Bootstrap.GetLoadDistance());
            }
        }
Пример #4
0
        public static void StreamChunksIn(EntityManager EntityManager, ChunkSpawnSystem chunkSpawnSystem,
                                          bool isModel, Entity worldEntity, ref World world, int3 centerWorldPosition, float renderSize, float loadSize, bool isCentredWorld = true)
        {
            world.centralPosition = centerWorldPosition;
            int boundsSize = (int)math.max(renderSize, loadSize);
            // Calculate Bounds
            int3 lowerBounds = new int3(0, 0, 0);

            if (isCentredWorld)
            {
                lowerBounds = new int3(-boundsSize, 0, -boundsSize);
            }
            int3 upperBounds = new int3(boundsSize, 0, boundsSize);

            lowerBounds += centerWorldPosition;
            upperBounds += centerWorldPosition;
            int3                   spawnChunkPosition;
            HashSet <int3>         oldChunkPositions   = new HashSet <int3>(world.chunkPositions.ToArray());
            HashSet <int3>         onlyPositions       = new HashSet <int3>();
            HashSet <int3>         worldChunkPositions = new HashSet <int3>(world.chunkPositions.ToArray()); // total positions!
            Dictionary <int, bool> allRenders          = new Dictionary <int, bool>();
            Dictionary <int3, int> chunkPositions      = new Dictionary <int3, int>();
            List <int>             chunkDistances      = new List <int>();

            for (int i = 0; i < world.chunkIDs.Length; i++)
            {
                Entity chunkEntity = chunkSpawnSystem.chunks[world.chunkIDs[i]];
                chunkPositions.Add(EntityManager.GetComponentData <Chunk>(chunkEntity).Value.chunkPosition, world.chunkIDs[i]);
            }
            lowerBounds.x = (int)lowerBounds.x;
            lowerBounds.y = (int)lowerBounds.y;
            lowerBounds.z = (int)lowerBounds.z;

            for (spawnChunkPosition.x = lowerBounds.x; spawnChunkPosition.x <= upperBounds.x; spawnChunkPosition.x++)
            {
                for (spawnChunkPosition.y = lowerBounds.y; spawnChunkPosition.y <= upperBounds.y; spawnChunkPosition.y++)
                {
                    for (spawnChunkPosition.z = lowerBounds.z; spawnChunkPosition.z <= upperBounds.z; spawnChunkPosition.z++)
                    {
                        if (world.bounds == 0 ||
                            (spawnChunkPosition.x >= -world.bounds && spawnChunkPosition.x <= world.bounds &&
                             spawnChunkPosition.z >= -world.bounds && spawnChunkPosition.z <= world.bounds))
                        {
                            // if doesnt have chunk, add it to list
                            if (!worldChunkPositions.Contains(spawnChunkPosition))
                            {
                                worldChunkPositions.Add(spawnChunkPosition);    // new position added here
                            }
                            else
                            {
                                int3 distance = centerWorldPosition - spawnChunkPosition;
                                distance.x = math.abs(distance.x);
                                distance.z = math.abs(distance.z);
                                int maxDistance = (int)math.max(distance.x, distance.z);
                                chunkDistances.Add(maxDistance);
                                bool isRender = (spawnChunkPosition.x - centerWorldPosition.x) >= -renderSize && (spawnChunkPosition.x - centerWorldPosition.x) <= renderSize &&
                                                (spawnChunkPosition.z - centerWorldPosition.z) >= -renderSize && (spawnChunkPosition.z - centerWorldPosition.z) <= renderSize;
                                allRenders.Add(chunkPositions[spawnChunkPosition],
                                               (isModel || isRender)); //maxDistance != renderSize));
                            }
                            // in another list add all chunk positions needed - this should be done per stream position
                            onlyPositions.Add(spawnChunkPosition);
                        }
                    }
                }
            }
            // in another system - spawn in chunks
            // Adding New Chunks!
            List <int> worldsChunkIDs = new List <int>(); // total ids // world.chunkIDs.ToArray()

            for (int i = 0; i < world.chunkIDs.Length; i++)
            {
                worldsChunkIDs.Add(world.chunkIDs[i]);
            }
            List <int3> newPositions = new List <int3>();
            List <bool> newRenders   = new List <bool>();

            // only for updates, so not for first chunks - basically not for models
            //for (int i = 0; i < worldChunkPositions.Count; i++)
            foreach (var worldChunkPosition in worldChunkPositions)
            {
                // if doesn't have chunk position already existing
                if (!oldChunkPositions.Contains(worldChunkPosition))
                {
                    newPositions.Add(worldChunkPosition);
                    bool isRender = (worldChunkPosition.x - centerWorldPosition.x) >= -renderSize && (worldChunkPosition.x - centerWorldPosition.x) <= renderSize &&
                                    (worldChunkPosition.z - centerWorldPosition.z) >= -renderSize && (worldChunkPosition.z - centerWorldPosition.z) <= renderSize;
                    newRenders.Add(isModel || isRender);
                }
            }
            // finally, spawn the chunks
            int[] newIDs = chunkSpawnSystem.SpawnChunks(worldEntity, newPositions.ToArray(), newRenders.ToArray());
            worldsChunkIDs.AddRange(newIDs); // add new ids here for added chunk positions
            // Remove chunks that arn't in onlyPositions list
            if (worldsChunkIDs.Count != worldChunkPositions.Count)
            {
                Debug.LogError("worldsChunkIDs count: " + worldsChunkIDs.Count + " not equal to worldCHunkPositions Count: " + worldChunkPositions.Count);
                return;
            }
            List <int>  removingChunkIDs  = new List <int>();
            List <int3> removingPositions = new List <int3>();
            //for (int i = 0; i < worldChunkPositions.Count; i++)
            int h = 0;

            foreach (var worldChunkPosition in worldChunkPositions)
            {
                // if not in main location, remove the chunk
                if (!onlyPositions.Contains(worldChunkPosition))
                {
                    //Debug.LogError("Removing Chunk: " + worldChunkPositions[i].ToString() + ":::" + worldsChunkIDs[i]);
                    removingChunkIDs.Add(worldsChunkIDs[h]);
                    removingPositions.Add(worldChunkPosition);
                }
                h++;
            }
            chunkSpawnSystem.RemoveChunks(ref world, removingChunkIDs.ToArray(), removingPositions.ToArray());
            // now remove the ids from list
            for (int i = 0; i < removingChunkIDs.Count; i++)
            {
                if (worldsChunkIDs.Contains(removingChunkIDs[i]))
                {
                    int index = worldsChunkIDs.IndexOf(removingChunkIDs[i]);
                    worldsChunkIDs.RemoveAt(index);
                    int j = 0;
                    foreach (var positoin in worldChunkPositions)
                    {
                        if (index == j)
                        {
                            worldChunkPositions.Remove(positoin);
                            break;
                        }
                        j++;
                    }
                    //worldChunkPositions.RemoveAt(index);
                }
            }
            // create new arrays for world
            List <int> oldChunkIDs = new List <int>(world.chunkIDs.ToArray());

            world.chunkIDs = new BlitableArray <int>(worldsChunkIDs.Count, Unity.Collections.Allocator.Persistent);
            for (int i = 0; i < world.chunkIDs.Length; i++)
            {
                world.chunkIDs[i] = worldsChunkIDs[i];
            }
            world.chunkPositions = new BlitableArray <int3>(worldChunkPositions.Count, Unity.Collections.Allocator.Persistent);
            //for (int i = 0; i < world.chunkPositions.Length; i++)
            int a = 0;

            foreach (var position in worldChunkPositions)
            {
                world.chunkPositions[a] = position; //worldChunkPositions[i];
                a++;
            }
            WorldStreamSystem.CreateWorldUpdate(EntityManager, worldEntity, worldsChunkIDs, oldChunkIDs, allRenders);
        }
Пример #5
0
        public void Initialize(Unity.Entities.World space)
        {
            // entity spawn
            worldSpawnSystem = space.GetOrCreateSystem <WorldSpawnSystem>();
            AddSystemToUpdateList(worldSpawnSystem);
            chunkSpawnSystem = space.GetOrCreateSystem <ChunkSpawnSystem>();
            AddSystemToUpdateList(chunkSpawnSystem);
            chunkRenderSystem = space.GetOrCreateSystem <ChunkRenderSystem>();
            AddSystemToUpdateList(chunkRenderSystem);
            voxelSpawnSystem = space.GetOrCreateSystem <VoxelSpawnSystem>();
            AddSystemToUpdateList(voxelSpawnSystem);

            // mesh gen
            chunkSideSystem = space.GetOrCreateSystem <ChunkSidesSystem>();
            AddSystemToUpdateList(chunkSideSystem);
            chunkSideCullSystem = space.GetOrCreateSystem <ChunkSideCullingSystem>();
            AddSystemToUpdateList(chunkSideCullSystem);
            chunkToRendererSystem = space.GetOrCreateSystem <ChunkToRendererSystem>();
            AddSystemToUpdateList(chunkToRendererSystem);
            chunkMeshBuildSystem = space.GetOrCreateSystem <ChunkMeshBuilderSystem>();
            AddSystemToUpdateList(chunkMeshBuildSystem);
            chunkMeshEndSystem = space.GetOrCreateSystem <ChunkMeshEndingSystem>();
            AddSystemToUpdateList(chunkMeshEndSystem);

            chunkWeightBuilder = space.GetOrCreateSystem <ChunkWeightBuilder>();
            AddSystemToUpdateList(chunkWeightBuilder);

            // maps
            chunkMapStarterSystem = space.GetOrCreateSystem <ChunkMapStarterSystem>();
            AddSystemToUpdateList(chunkMapStarterSystem);
            chunkMapBuilderSystem = space.GetOrCreateSystem <ChunkMapBuilderSystem>();
            AddSystemToUpdateList(chunkMapBuilderSystem);
            chunkMapCompleterSystem = space.GetOrCreateSystem <ChunkMapCompleterSystem>();
            AddSystemToUpdateList(chunkMapCompleterSystem);

            // player streaming
            worldStreamSystem = space.GetOrCreateSystem <WorldStreamSystem>();
            AddSystemToUpdateList(worldStreamSystem);
            chunkStreamSystem = space.GetOrCreateSystem <ChunkStreamSystem>();
            AddSystemToUpdateList(chunkStreamSystem);
            chunkStreamEndSystem = space.GetOrCreateSystem <ChunkStreamEndSystem>();
            AddSystemToUpdateList(chunkStreamEndSystem);

            // interact
            voxelRaycastSystem = space.GetOrCreateSystem <VoxelRaycastSystem>();
            AddSystemToUpdateList(voxelRaycastSystem);
            voxelPreviewSystem = space.GetOrCreateSystem <VoxelPreviewSystem>();
            AddSystemToUpdateList(voxelPreviewSystem);
            characterRaycastSystem = space.GetOrCreateSystem <CharacterRaycastSystem>();
            AddSystemToUpdateList(characterRaycastSystem);

            renderSystem = space.GetOrCreateSystem <RenderSystem>();
            AddSystemToUpdateList(renderSystem);

            if (Bootstrap.instance.isAnimateRenders)
            {
                chunkRendererAnimationSystem = space.GetOrCreateSystem <ChunkRendererAnimationSystem>();
                AddSystemToUpdateList(chunkRendererAnimationSystem);
            }

            if (Bootstrap.DebugChunks)
            {
                debugChunkSystem = space.GetOrCreateSystem <DebugChunkSystem>();
                AddSystemToUpdateList(debugChunkSystem);
            }
            if (!Bootstrap.isRenderChunks)
            {
                chunkSideSystem.Enabled = false;
            }
            SetLinks();
        }
Пример #6
0
        private void UpdateModel(Entity world, VoxData model, SkeletonDatam skeleton, int id)
        {
            if (worlds.ContainsKey(id) == false)
            {
                //Debug.LogError("Updating Model: " + id);
                worlds.Add(id, world);
                worldLookups.Add(id, new WorldChunkMap {
                    chunks = new Dictionary <int3, Entity>()
                });
            }
            else
            {
                //Debug.LogError("Removing previous world: " + id);
                DestroyWorldWeakly(id);
                worlds.Add(id, world);
                worldLookups.Add(id, new WorldChunkMap {
                    chunks = new Dictionary <int3, Entity>()
                });
            }
            if (models.ContainsKey(id))
            {
                models[id] = model;
            }
            else
            {
                models.Add(id, model);
            }
            float3 scale = new float3(model.scale.x / 16f, model.scale.y / 16f, model.scale.z / 16f);

            World.EntityManager.SetComponentData(world, new ZoxID {
                id = id
            });
            World worldComponent = new World
            {
                //chunkIDs = new BlitableArray<int>(0, Unity.Collections.Allocator.Persistent),
                //chunkPositions = new BlitableArray<int3>(0, Unity.Collections.Allocator.Persistent),
                scale           = scale,
                voxelDimensions = model.size,
                modelID         = model.id
            };

            if (skeleton != null)
            {
                //worldComponent.skeletonID = skeleton.data.id;
            }
            float renderDistance = 0;

            if (World.EntityManager.HasComponent <World>(world) == false)
            {
                World.EntityManager.AddComponentData(world, worldComponent);
            }
            else
            {
                World.EntityManager.SetComponentData(world, worldComponent);
            }
            WorldStreamSystem.StreamChunksIn(
                World.EntityManager,
                chunkSpawnSystem,
                true,
                world,
                ref worldComponent,
                int3.Zero(),
                renderDistance,
                renderDistance,
                false);
            World.EntityManager.SetComponentData(world, worldComponent);
        }