Beispiel #1
0
            public VoxData GetRealOne()
            {
                VoxData voxData = new VoxData();

                voxData.id    = id;
                voxData.scale = scale;
                voxData.size  = size;
                voxData.InitializeColors(colorsR.Length);
                if (voxData.colorsR.Length != colorsR.Length)
                {
                    return(voxData);
                }
                for (int i = 0; i < colorsR.Length; i++)
                {
                    voxData.colorsR[i] = colorsR[i];
                    voxData.colorsG[i] = colorsG[i];
                    voxData.colorsB[i] = colorsB[i];
                }
                voxData.InitializeData(); // data.Length
                if (voxData.data.Length != data.Length)
                {
                    return(voxData);
                }
                for (int i = 0; i < data.Length; i++)
                {
                    voxData.data[i] = data[i];
                }
                return(voxData);
            }
Beispiel #2
0
 private void UpdateChunkWithModel(Entity chunkEntity, ref Chunk chunk, VoxData model)
 {
     chunk.isWeights = 1;
     // set chunk data depending on voxModel data
     //if (model.Value.size.x == 16 && model.Value.size.y == 16 && model.Value.size.z == 16)
     {
         //chunk.isDirty = 1;
         if (model.data.Length == 0)
         {
             Debug.LogError("Model Data is 0.");
             return;
         }
         World.EntityManager.AddComponentData(chunkEntity, new ChunkBuilder {
         });
         int3 localPosition; // = float3.zero;
         for (localPosition.x = 0; localPosition.x < chunk.Value.voxelDimensions.x; localPosition.x++)
         {
             for (localPosition.y = 0; localPosition.y < chunk.Value.voxelDimensions.y; localPosition.y++)
             {
                 for (localPosition.z = 0; localPosition.z < chunk.Value.voxelDimensions.z; localPosition.z++)
                 {
                     int voxelIndex      = VoxelRaycastSystem.GetVoxelArrayIndex(localPosition, chunk.Value.voxelDimensions);
                     int modelVoxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(localPosition + chunk.GetVoxelPosition(), model.size);
                     int newVoxelType    = model.data[modelVoxelIndex];
                     if (newVoxelType == 0)
                     {
                         chunk.Value.voxels[voxelIndex] = (byte)(newVoxelType);
                     }
                     else
                     {
                         // mutations
                         if (Bootstrap.instance.isMutateVoxes)
                         {
                             if (UnityEngine.Random.Range(0, 100) >= 90)
                             {
                                 chunk.Value.voxels[voxelIndex] = (byte)(newVoxelType + 1);
                             }
                             else
                             {
                                 if (UnityEngine.Random.Range(0, 100) >= 93)
                                 {
                                     chunk.Value.voxels[voxelIndex] = (byte)(0);
                                 }
                                 else
                                 {
                                     chunk.Value.voxels[voxelIndex] = (byte)(newVoxelType);
                                 }
                             }
                         }
                         else
                         {
                             chunk.Value.voxels[voxelIndex] = (byte)(newVoxelType);
                         }
                     }
                 }
             }
         }
     }
 }
Beispiel #3
0
        public void SpawnChunkRenders(SpawnChunkRendersCommand command)
        {
            World world = World.EntityManager.GetComponentData <World>(command.world);

            if (Bootstrap.instance.isUseModels == false)
            {
                if (world.modelID != 0)
                {
                    return;
                }
            }
            if (World.EntityManager.HasComponent <ZoxID>(command.world) == false)
            {
                Debug.LogError("World does not have ZOXID but has modelID: " + world.modelID);
                return;
            }
            int worldID   = World.EntityManager.GetComponentData <ZoxID>(command.world).id;
            var materials = GetWorldMaterials(worldID);
            //int chunksCount = world.chunks.Length;
            //MapDatam map = null;
            VoxData model = new VoxData();
            NativeArray <Entity> renderEntities = new NativeArray <Entity>(command.renderEntitiesCount * command.materialsCount, Allocator.Temp);

            if (world.modelID == 0)
            {
                //map = worldSpawnSystem.maps[worldID];
                World.EntityManager.Instantiate(worldChunkRenderPrefab, renderEntities);
            }
            else //if (worldSpawnSystem.models.ContainsKey(worldID))
            {
                model = worldSpawnSystem.models[worldID];
                World.EntityManager.Instantiate(modelChunkRenderPrefab, renderEntities);
            }
            var chunkEntities     = world.chunks.ToArray();
            int renderEntityCount = 0;

            for (int i = 0; i < chunkEntities.Length; i++)
            {
                // need to bulk these too
                if (command.isRender[i] == 1)
                {
                    Chunk    chunk = World.EntityManager.GetComponentData <Chunk>(chunkEntities[i]);
                    Entity[] renderEntitiesSmall = new Entity[command.materialsCount];
                    for (int j = 0; j < renderEntitiesSmall.Length; j++)
                    {
                        renderEntitiesSmall[j] = renderEntities[renderEntityCount * command.materialsCount + j];
                    }
                    AddRenderEntitiesToChunk(chunkEntities[i], ref chunk, world, materials, chunk.GetVoxelPosition().ToFloat3(), renderEntitiesSmall, model);
                    World.EntityManager.SetComponentData(chunkEntities[i], chunk);
                    renderEntityCount++;
                }
            }
            renderEntities.Dispose();
        }
Beispiel #4
0
        public void AddRenderEntitiesToChunk(World world, Entity chunkEntity, ref Chunk chunk)
        {
            int             worldID       = World.EntityManager.GetComponentData <ZoxID>(chunk.world).id;
            List <Material> materials     = GetWorldMaterials(worldID);
            float3          spawnPosition = World.EntityManager.GetComponentData <Translation>(chunkEntity).Value;
            VoxData         model         = new VoxData();
            var             prefab        = worldChunkRenderPrefab;

            if (worldSpawnSystem.models.ContainsKey(worldID))
            {
                model  = worldSpawnSystem.models[worldID];
                prefab = modelChunkRenderPrefab;
            }
            Entity[] entities = new Entity[materials.Count];
            for (int i = 0; i < entities.Length; i++)
            {
                entities[i] = World.EntityManager.Instantiate(prefab);
            }
            AddRenderEntitiesToChunk(chunkEntity, ref chunk, world, materials, spawnPosition, entities, model);
        }
Beispiel #5
0
 public void OnAfterDeserialize()
 {
     data = clone.GetRealOne();
 }
Beispiel #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);
        }
Beispiel #7
0
        public static void QueueUpdateModel(EntityManager EntityManager, Entity entity, int spawnID, VoxData model, int skeletonID = 0)
        {
            Entity e = EntityManager.CreateEntity();

            EntityManager.AddComponentData(e, new UpdateModelCommand
            {
                entity     = entity,
                spawnID    = spawnID,
                model      = model,
                skeletonID = skeletonID
            });
        }
Beispiel #8
0
 public void AddRenderEntitiesToChunk(Entity chunkEntity, ref Chunk chunk, World world, List <Material> materials, float3 spawnPosition, Entity[] renderEntities, VoxData model)
 {
     if (ChunkSpawnSystem.isDebugLog)
     {
         Debug.LogError("Adding " + materials.Count + " chunk renders to chunk: " + chunk.Value.chunkPosition + "::" + chunk.Value.voxelDimensions);
     }
     chunk.chunkRenders = new BlitableArray <Entity>(materials.Count, Allocator.Persistent);
     for (int j = 0; j < materials.Count; j++)
     {
         //Entity renderEntity = renderEntities[i * materials.Count + j];
         Entity renderEntity = renderEntities[j];
         SetChunkRender(
             renderEntity,
             chunkEntity,
             ref chunk,
             materials[j],
             j,
             spawnPosition,
             model.id != 0,
             world.skeletonID);
         if (model.id != 0)
         {
             ChunkRenderer chunkRender = World.EntityManager.GetComponentData <ChunkRenderer>(renderEntity);
             chunkRender.isCenter = 1;
             List <Color> colors = model.GetColors(); //  new List<Color>(); //
             chunkRender.voxelColors = new BlitableArray <float3>(colors.Count, Allocator.Persistent);
             for (int a = 0; a < colors.Count; a++)
             {
                 chunkRender.voxelColors[a] = new float3(colors[a].r, colors[a].b, colors[a].g);
             }
             World.EntityManager.SetComponentData(renderEntity, chunkRender);
         }
         chunk.chunkRenders[j] = renderEntity;
     }
 }
Beispiel #9
0
        public void SpawnChunks(SpawnChunkCommand command)
        {
            var worldEntity = command.world;
            int worldID     = World.EntityManager.GetComponentData <ZoxID>(worldEntity).id;
            var world       = World.EntityManager.GetComponentData <World>(worldEntity);

            if (ChunkSpawnSystem.isDebugLog)
            {
                Debug.LogError("Spawning World's Chunks [" + command.chunkIDs.Length + "] with dimensions: " + world.voxelDimensions + ", id: " + worldID);
            }
            Translation          worldTranslation = World.EntityManager.GetComponentData <Translation>(worldEntity);
            NativeArray <Entity> entities         = new NativeArray <Entity>(command.chunkPositions.Length, Allocator.Temp);
            // materials
            int renderEntitiesCount = 0;

            for (int i = 0; i < command.isRender.Length; i++)
            {
                if (command.isRender[i] == 1)
                {
                    renderEntitiesCount++;
                }
            }
            List <Material> materials = GetWorldMaterials(worldID);
            MapDatam        map       = null;
            VoxData         model     = new VoxData();

            //NativeArray<Entity> renderEntities = new NativeArray<Entity>(renderEntitiesCount * materials.Count, Allocator.Temp);
            if (worldSpawnSystem.maps.ContainsKey(worldID))
            {
                map = worldSpawnSystem.maps[worldID];
                World.EntityManager.Instantiate(chunkPrefab, entities);
                //World.EntityManager.Instantiate(worldChunkRenderPrefab, renderEntities);
            }
            else if (worldSpawnSystem.models.ContainsKey(worldID))
            {
                model = worldSpawnSystem.models[worldID];
                World.EntityManager.Instantiate(modelChunkPrefab, entities);
                //World.EntityManager.Instantiate(modelChunkRenderPrefab, renderEntities);
            }
            // for all bullets, set custom data using indexes entity
            int renderEntityCount = 0;
            var chunkIDs          = command.chunkIDs.ToArray();
            var chunkPositions    = command.chunkPositions.ToArray();

            for (int i = 0; i < entities.Length; i++)
            {
                Entity chunkEntity = entities[i];
                if (chunks.ContainsKey(chunkIDs[i]))
                {
                    World.EntityManager.DestroyEntity(chunkEntity);
                    continue;
                }
                chunks.Add(chunkIDs[i], entities[i]);
                var lookupTable = worldSpawnSystem.worldLookups[worldID];
                if (lookupTable.chunks.ContainsKey(chunkPositions[i]))
                {
                    lookupTable.chunks[chunkPositions[i]] = chunkEntity;
                }
                else
                {
                    lookupTable.chunks.Add(chunkPositions[i], chunkEntity);
                }
                worldSpawnSystem.worldLookups[worldID] = lookupTable;
                World.EntityManager.SetComponentData(entities[i], new ZoxID {
                    id        = chunkIDs[i],
                    creatorID = worldID
                });
                Chunk chunk = World.EntityManager.GetComponentData <Chunk>(chunkEntity);
                chunk.world = worldEntity;
                chunk.Value.chunkPosition   = chunkPositions[i];
                chunk.Value.worldScale      = world.scale;
                chunk.Value.voxelDimensions = world.voxelDimensions;
                chunk.Init(world.voxelDimensions);
                if (model.id != 0)
                {
                    UpdateChunkWithModel(chunkEntity, ref chunk, model);
                }
                SetChunkSurroundingIndexes(chunkEntity, ref chunk);
                float3 spawnPosition = new float3( // worldOffset +
                    chunk.Value.chunkPosition.x * chunk.Value.worldScale.x * chunk.Value.voxelDimensions.x,
                    chunk.Value.chunkPosition.y * chunk.Value.worldScale.y * chunk.Value.voxelDimensions.y,
                    chunk.Value.chunkPosition.z * chunk.Value.worldScale.z * chunk.Value.voxelDimensions.z);
                World.EntityManager.SetComponentData(chunkEntity, new Translation {
                    Value = spawnPosition
                });
                World.EntityManager.SetComponentData(chunkEntity, new NonUniformScale {
                    Value = new float3(1, 1, 1)
                });
                World.EntityManager.SetComponentData(chunkEntity, new Rotation {
                    Value = quaternion.identity
                });
                World.EntityManager.SetComponentData(chunkEntity, new Parent {
                    Value = chunk.world
                });
                // set depending on biome type - biomeDatam has CharacterDatam linked
                //World.EntityManager.SetComponentData(entities[i], chunk);
                World.EntityManager.SetComponentData(entities[i], chunk);
            }
            var entitiesArray = entities.ToArray();;

            world.chunks = new BlitableArray <Entity>(entitiesArray.Length, Allocator.Persistent);
            for (int i = 0; i < world.chunks.Length; i++)
            {
                world.chunks[i] = entitiesArray[i];
            }
            World.EntityManager.SetComponentData(worldEntity, world);
            ChunkRenderSystem.SpawnChunkRenders(World.EntityManager, worldEntity, renderEntitiesCount, materials.Count, command.isRender.ToArray());
            entities.Dispose();
        }
Beispiel #10
0
        public void Merge(VoxData newPart, int3 offset)
        {
            var oldData = data.ToArray();
            var oldSize = size;

            // i should also change bytes depending on colours, using a colour lookup or something
            if (colorsR.Length == 0)
            {
                InitializeColors(newPart.colorsR.Length);
                for (int i = 0; i < newPart.colorsR.Length; i++)
                {
                    colorsR[i] = newPart.colorsR[i];
                    colorsG[i] = newPart.colorsG[i];
                    colorsB[i] = newPart.colorsB[i];
                }
            }
            // get new size first
            size = new int3(
                math.max(size.x, newPart.size.x + math.abs(offset.x)),
                math.max(size.y, newPart.size.y + math.abs(offset.y)),
                math.max(size.z, newPart.size.z + math.abs(offset.z)));
            scale = new float3(
                math.max(scale.x, newPart.scale.x),
                math.max(scale.y, newPart.scale.y),
                math.max(scale.z, newPart.scale.z));
            UnityEngine.Debug.LogError("New size is set to: " + size + " from " + oldSize);
            // create new data
            InitializeData();
            int3 localPosition;

            for (localPosition.x = 0; localPosition.x < size.x; localPosition.x++)
            {
                for (localPosition.y = 0; localPosition.y < size.y; localPosition.y++)
                {
                    for (localPosition.z = 0; localPosition.z < size.z; localPosition.z++)
                    {
                        int newIndex = VoxelRaycastSystem.GetVoxelArrayIndex(localPosition, size);
                        data[newIndex] = 0;
                    }
                }
            }

            // first add in old parts
            for (localPosition.x = 0; localPosition.x < oldSize.x; localPosition.x++)
            {
                for (localPosition.y = 0; localPosition.y < oldSize.y; localPosition.y++)
                {
                    for (localPosition.z = 0; localPosition.z < oldSize.z; localPosition.z++)
                    {
                        int oldIndex    = VoxelRaycastSystem.GetVoxelArrayIndex(localPosition, oldSize);
                        var newPosition = localPosition; // + position offset?
                        if (offset.x < 0)
                        {
                            newPosition.x += -offset.x;
                        }
                        if (offset.y < 0)
                        {
                            newPosition.y += -offset.y;
                        }
                        if (offset.z < 0)
                        {
                            newPosition.z += -offset.z;
                        }
                        int newIndex = VoxelRaycastSystem.GetVoxelArrayIndex(newPosition, size);
                        if (oldData[oldIndex] != 0)
                        {
                            data[newIndex] = oldData[oldIndex];
                        }
                    }
                }
            }
            if (offset.x < 0)
            {
                offset.x = 0;
            }
            if (offset.y < 0)
            {
                offset.y = 0;
            }
            if (offset.z < 0)
            {
                offset.z = 0;
            }

            // now add in new part
            for (localPosition.x = 0; localPosition.x < newPart.size.x; localPosition.x++)
            {
                for (localPosition.y = 0; localPosition.y < newPart.size.y; localPosition.y++)
                {
                    for (localPosition.z = 0; localPosition.z < newPart.size.z; localPosition.z++)
                    {
                        int partIndex   = VoxelRaycastSystem.GetVoxelArrayIndex(localPosition, newPart.size);
                        var newPosition = localPosition + offset;  // + position offset?
                        int newIndex    = VoxelRaycastSystem.GetVoxelArrayIndex(newPosition, size);
                        if (newPart.data[partIndex] != 0)
                        {
                            data[newIndex] = newPart.data[partIndex];
                        }
                    }
                }
            }
        }