Example #1
0
        void BuildLayer(VoxBuildLayer layer)
        {
            int3 localPosition;

            for (int i = 0; i < layer.voxes.Count; i++)
            {
                var vox       = layer.voxes[i];
                var position  = layer.positions[i];
                var operation = layer.operations[i];
                //Debug.LogError("Building Vox: " + vox.size + " at position " + position);
                for (localPosition.x = 0; localPosition.x < vox.size.x; localPosition.x++)
                {
                    for (localPosition.y = 0; localPosition.y < vox.size.y; localPosition.y++)
                    {
                        for (localPosition.z = 0; localPosition.z < vox.size.z; localPosition.z++)
                        {
                            int partIndex = VoxelRaycastSystem.GetVoxelArrayIndex(localPosition, vox.size);
                            if (operation == VoxOperation.FlipX)
                            {
                                partIndex = VoxelRaycastSystem.GetVoxelArrayIndex(new int3(vox.size.x - 1 - localPosition.x, localPosition.y, localPosition.z), vox.size);
                            }
                            var newPosition = localPosition + position;
                            int newIndex    = VoxelRaycastSystem.GetVoxelArrayIndex(newPosition, size);
                            if (vox.data[partIndex] != 0)
                            {
                                data[newIndex] = vox.data[partIndex];
                            }
                        }
                    }
                }
            }
        }
Example #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);
                         }
                     }
                 }
             }
         }
     }
 }
Example #3
0
        private VoxDatam ProcessVoxFile(VoxDatam model)
        {
            var voxel = VoxImport.VoxImportMethods.Load(voxAssetPath);

            filename = Path.GetFileNameWithoutExtension(voxAssetPath);
            if (model == null)
            {
                model = ScriptableObject.CreateInstance <VoxDatam>();
            }
            model.name = filename + " Vox";
            var priorID = model.data.id;

            if (priorID == 0)
            {
                priorID = Bootstrap.GenerateUniqueID();
            }
            model.data = new VoxData {
                id = priorID
            };
            uint[]    pallete = voxel.palette.values;
            Color32[] colors  = VoxImport.VoxImportMethods.CreateColor32FromPelatte(pallete);
            model.data.InitializeColors(colors.Length);
            for (int i = 0; i < colors.Length; i++)
            {
                model.data.colorsR[i] = colors[i].r;
                model.data.colorsG[i] = colors[i].g;
                model.data.colorsB[i] = colors[i].b;
            }
            var  voxData = voxel.chunkChild[0].xyzi.voxels;
            byte voxelValue;

            model.data.size = new int3(voxData.x, voxData.y, voxData.z);
            model.data.InitializeData();
            int3 localPosition;

            for (localPosition.x = 0; localPosition.x < model.data.size.x; localPosition.x++)
            {
                for (localPosition.y = 0; localPosition.y < model.data.size.y; localPosition.y++)
                {
                    for (localPosition.z = 0; localPosition.z < model.data.size.z; localPosition.z++)
                    {
                        int modelVoxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(localPosition, model.data.size);
                        voxelValue = (byte)voxData.voxels[(int)localPosition.x, (int)localPosition.y, (int)localPosition.z];
                        if (voxelValue != 255)
                        {
                            model.data.data[modelVoxelIndex] = (byte)(voxelValue + 1);
                        }
                        else
                        {
                            model.data.data[modelVoxelIndex] = (byte)0;
                        }
                    }
                }
            }
            model.data.scale = scale;
            return(model);
        }
Example #4
0
        void SpawnVoxel(int3 spawnPosition, int voxelID, Entity world)
        {
            var voxelDimensions = World.EntityManager.GetComponentData <World>(world).voxelDimensions;
            var chunkPosition   = VoxelRaycastSystem.GetChunkPosition(spawnPosition, voxelDimensions);// new float3(spawnPosition.x / 16, spawnPosition.y / 16, spawnPosition.z / 16);
            var localPosition   = VoxelRaycastSystem.GetLocalPosition(spawnPosition, chunkPosition, voxelDimensions);
            //Debug.LogError("Spawning voxel of Type: " + spawnType + " M: " + spawnPosition.ToString() + " C: " + chunkPosition.ToString() + " L:" + localPosition);
            // get chunk that position is within
            // get chunk
            // todo: store chunks in UniqueKey<(worldID, chunkPosition)> as keys) - 4 numbers to generate a unique key?
            Entity foundChunk   = new Entity();
            Chunk  writeToChunk = new Chunk();
            bool   didFindChunk = false;

            foreach (Entity e in chunkSpawnSystem.chunks.Values)
            {
                Chunk chunk = World.EntityManager.GetComponentData <Chunk>(e);
                if (chunk.Value.chunkPosition.x == chunkPosition.x && chunk.Value.chunkPosition.y == chunkPosition.y && chunk.Value.chunkPosition.z == chunkPosition.z)
                {
                    foundChunk   = e;
                    writeToChunk = chunk;
                    didFindChunk = true;
                    //return chunk.Value.voxels[GetVoxelArrayIndex(localChunkPosition)];
                }
            }
            if (!didFindChunk)
            {
                //Debug.LogError("Could not find chunk: " + chunkPosition.ToString());
                return;
            }
            // get index out of spawnPosition
            int index = VoxelRaycastSystem.GetVoxelArrayIndex(localPosition, voxelDimensions);
            //if (didFindChunk)
            {
                //Debug.LogError("Found Chunk: " + chunkPosition.ToString() + " with voxelIndex: " + index);
            }
            //Debug.LogError("    chunkPosition: " + chunkPosition.ToString() + " index at " + index);
            // get index from voxel list
            int voxelIndex = voxelIDs.IndexOf(voxelID) + 1; // add one for air

            writeToChunk.Value.voxels[index] = (byte)(voxelIndex);
            //writeToChunk.isDirty = 1;
            World.EntityManager.SetComponentData(foundChunk, writeToChunk);


            if (World.EntityManager.HasComponent <ChunkBuilder>(foundChunk))
            {
                World.EntityManager.SetComponentData(foundChunk, new ChunkBuilder {
                });
            }
            else
            {
                World.EntityManager.AddComponentData(foundChunk, new ChunkBuilder {
                });
            }
        }
Example #5
0
        // need to use vox layers instead, each layer has a list of voxes and positions
        public void Build(VoxBuildLayer bodyLayer, VoxBuildLayer gearLayer, //List<VoxData> voxes, List<int3> positions, List<VoxOperation> operations,
                          int3 newSize)
        {
            if (bodyLayer.voxes.Count == 0)
            {
                Debug.LogError("0 Voxes..Cannot build vox. Remember to have core on item.");
                return;
            }
            //Debug.LogError("Building Vox with: " + voxes.Count + " voxes. Size is: " + newSize);
            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)
            {
                var newPart = bodyLayer.voxes[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  = newSize;
            scale = new float3(0.5f, 0.5f, 0.5f);
            //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
            BuildLayer(bodyLayer);
            BuildLayer(gearLayer);
        }
Example #6
0
            public void Execute(ref ChunkRendererBuilder chunkRendererBuilder, ref ChunkRenderer chunk, ref ChunkSides chunkSides)
            {
                if (chunkRendererBuilder.state == 1)
                {
                    /*if (ChunkSpawnSystem.isDebugLog)
                     * {
                     *      UnityEngine.Debug.LogError("Processing ChunkSideCullingSystem voxes.");
                     * }*/
                    chunkRendererBuilder.state = 2;
                    int  voxelIndex;
                    byte meta;
                    int3 position;                     //  = new float3(0, 0, 0)
                    int  normalVoxelIndex = -1;
                    for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                    {
                        for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                        {
                            for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                            {
                                normalVoxelIndex++;
                                if (normalVoxelIndex >= chunkSides.sidesUp.Length)
                                {
                                    //UnityEngine.Debug.LogError("normalVoxelIndex is too high at: " + normalVoxelIndex);
                                    return;
                                }
                                //voxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position, chunk.Value.voxelDimensions);
                                meta = chunk.Value.voxels[normalVoxelIndex];
                                if (meta == 0)
                                {
                                    // if air don't draw anything!
                                    chunkSides.sidesUp[normalVoxelIndex]      = 0;
                                    chunkSides.sidesDown[normalVoxelIndex]    = 0;
                                    chunkSides.sidesLeft[normalVoxelIndex]    = 0;
                                    chunkSides.sidesRight[normalVoxelIndex]   = 0;
                                    chunkSides.sidesForward[normalVoxelIndex] = 0;
                                    chunkSides.sidesBack[normalVoxelIndex]    = 0;
                                }
                                // for all solid voxels
                                else
                                {
                                    // Y AXIS - Up - Down
                                    if (position.y < chunk.Value.voxelDimensions.y - 1)
                                    {
                                        // x + HEIGHT * (y + WIDTH* z)
                                        voxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position + int3.Up(), chunk.Value.voxelDimensions);// (int)(sizeY * sizeX * (position.x) + sizeY * (position.y + 1) + (position.z));
                                        if (chunk.Value.voxels[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesUp[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesUp[normalVoxelIndex] = 0;
                                        }
                                    }
                                    else
                                    {
                                        voxelIndex = (int)(chunk.Value.voxelDimensions.z * (position.x) + (position.z));  // get the XZ Index
                                        if (chunk.Value.voxelsUp[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesUp[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesUp[normalVoxelIndex] = 0;
                                        }
                                    }
                                    if (position.y > 0)
                                    {
                                        voxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position + int3.Down(), //new float3(0, -1, 0),
                                                                                           chunk.Value.voxelDimensions);
                                        //voxelIndexDown = (int)(sizeY * sizeX * (position.x) + sizeY * (position.y - 1) + (position.z));
                                        if (chunk.Value.voxels[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesDown[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesDown[normalVoxelIndex] = 0;
                                        }
                                    }
                                    else
                                    {
                                        //chunk.sidesDown[voxelIndex] = 1;
                                        voxelIndex = (int)(chunk.Value.voxelDimensions.z * (position.x) + (position.z));
                                        if (chunk.Value.voxelsDown[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesDown[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesDown[normalVoxelIndex] = 0;
                                        }
                                    }
                                    // Z AXIS - Forward - Back
                                    if (position.z < chunk.Value.voxelDimensions.z - 1)
                                    {
                                        voxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position + int3.Forward(),
                                                                                           //new float3(0, 0, 1),
                                                                                           chunk.Value.voxelDimensions);
                                        //voxelIndexForward = (int)((position.z + 1) + sizeY * (position.y) + sizeY * sizeX * (position.x));
                                        if (chunk.Value.voxels[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesForward[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesForward[normalVoxelIndex] = 0;
                                        }
                                    }
                                    else
                                    {
                                        voxelIndex = (int)(chunk.Value.voxelDimensions.y * (position.x) + (position.y));
                                        if (chunk.Value.voxelsForward[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesForward[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesForward[normalVoxelIndex] = 0;
                                        }
                                        //chunk.sidesForward[voxelIndex] = 1;
                                    }
                                    if (position.z > 0)
                                    {
                                        voxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position + int3.Back(),
                                                                                           //new float3(0, 0, -1),
                                                                                           chunk.Value.voxelDimensions);
                                        //voxelIndexBack = (int)((position.z - 1) + sizeY * (position.y) + sizeY * sizeX * (position.x));
                                        if (chunk.Value.voxels[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesBack[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesBack[normalVoxelIndex] = 0;
                                        }
                                    }
                                    else
                                    {
                                        voxelIndex = (int)(chunk.Value.voxelDimensions.y * (position.x) + (position.y));
                                        if (chunk.Value.voxelsBack[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesBack[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesBack[normalVoxelIndex] = 0;
                                        }
                                        //chunk.sidesBack[voxelIndex] = 1;
                                    }

                                    // X AXIS - Left - Right
                                    #region LeftRight
                                    if (position.x > 0)
                                    {
                                        voxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position + int3.Left(),
                                                                                           //new float3(-1, 0, 0),
                                                                                           chunk.Value.voxelDimensions);
                                        //voxelIndexLeft = (int)(sizeY * sizeX * (position.x - 1) + sizeY * (position.y) +(position.z));
                                        if (chunk.Value.voxels[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesLeft[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesLeft[normalVoxelIndex] = 0;
                                        }
                                    }
                                    else                                     //if (position.x == 0)
                                    {
                                        voxelIndex = (int)(chunk.Value.voxelDimensions.z * (position.y) + (position.z));
                                        if (chunk.Value.voxelsLeft[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesLeft[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesLeft[normalVoxelIndex] = 0;
                                        }
                                    }
                                    if (position.x < chunk.Value.voxelDimensions.x - 1)
                                    {
                                        voxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position + int3.Right(),
                                                                                           //new float3(1, 0, 0),
                                                                                           chunk.Value.voxelDimensions);
                                        //voxelIndexRight = (int)(sizeY * sizeX * (position.x + 1) + sizeY * (position.y) + (position.z));
                                        if (chunk.Value.voxels[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesRight[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesRight[normalVoxelIndex] = 0;
                                        }
                                    }
                                    else                                    // if (position.x == 15)
                                    {
                                        voxelIndex = (int)(chunk.Value.voxelDimensions.z * (position.y) + (position.z));
                                        if (chunk.Value.voxelsRight[voxelIndex] == 0)
                                        {
                                            chunkSides.sidesRight[normalVoxelIndex] = 1;
                                        }
                                        else
                                        {
                                            chunkSides.sidesRight[normalVoxelIndex] = 0;
                                        }
                                    }
                                    #endregion
                                }
                            }
                        }
                    }
                    //chunk.isCullChunkSides = 1;
                    //chunk.isCullSides = 0;
                }
            }
Example #7
0
            public void Execute(ref ChunkBuilder chunkBuilder, ref Chunk chunk, ref ZoxID zoxID)
            {
                if (chunkBuilder.state == 0)
                {
                    byte edgeVoxelType = 0; // for making edges not show

                    // for all chunk sides, get side chunk, set side voxels
                    // if chunk has all sides updated, and is also dirty, it can cull sides
                    int  voxelIndex           = 0;
                    int  otherChunkVoxelIndex = 0;
                    int3 position;
                    bool failed;

                    #region UpDown
                    if (chunk.hasUpdatedUp == 0)
                    {
                        voxelIndex = 0;
                        if (chunk.indexUp != -1)
                        {
                            position.y = 0;
                            Chunk otherChunk = GetChunk(ref chunk, zoxID.creatorID, int3.Up(), out failed);
                            if (failed)
                            {
                                for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                                {
                                    for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                    {
                                        chunk.Value.voxelsUp[voxelIndex] = edgeVoxelType;
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedUp = 1;
                            }
                            else if (otherChunk.isGenerating == 0)
                            {
                                for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                                {
                                    for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                    {
                                        otherChunkVoxelIndex             = VoxelRaycastSystem.GetVoxelArrayIndex(position, chunk.Value.voxelDimensions);
                                        chunk.Value.voxelsUp[voxelIndex] = otherChunk.Value.voxels[otherChunkVoxelIndex];
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedUp = 1;
                            }
                        }
                        else
                        {
                            for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                            {
                                for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                {
                                    chunk.Value.voxelsUp[voxelIndex] = edgeVoxelType;
                                    voxelIndex++;
                                }
                            }
                            chunk.hasUpdatedUp = 1;
                        }
                    }
                    if (chunk.hasUpdatedDown == 0)
                    {
                        voxelIndex = 0;
                        if (chunk.indexDown != -1)
                        {
                            Chunk otherChunk = GetChunk(ref chunk, zoxID.creatorID, int3.Down(), out failed);
                            if (failed)
                            {
                                for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                                {
                                    for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                    {
                                        chunk.Value.voxelsDown[voxelIndex] = edgeVoxelType;                                           // 0
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedDown = 1;
                            }
                            else if (otherChunk.isGenerating == 0)
                            {
                                // For All Top Voxels of Other Chunk
                                position.y = (int)chunk.Value.voxelDimensions.y - 1;
                                for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                                {
                                    for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                    {
                                        otherChunkVoxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position, chunk.Value.voxelDimensions);
                                        chunk.Value.voxelsDown[voxelIndex] = otherChunk.Value.voxels[otherChunkVoxelIndex];
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedDown = 1;
                            }
                        }
                        else
                        {
                            for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                            {
                                for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                {
                                    if (chunk.isWeights == 1)
                                    {
                                        chunk.Value.voxelsDown[voxelIndex] = edgeVoxelType;                                           // 0
                                    }
                                    else
                                    {
                                        chunk.Value.voxelsDown[voxelIndex] = 1;                                        // edgeVoxelType;   // 0
                                    }
                                    voxelIndex++;
                                }
                            }
                            chunk.hasUpdatedDown = 1;
                        }
                    }
                    #endregion

                    #region LeftRight
                    if (chunk.hasUpdatedLeft == 0)
                    {
                        voxelIndex = 0;
                        if (chunk.indexLeft != -1)
                        {
                            Chunk otherChunk = GetChunk(ref chunk, zoxID.creatorID, int3.Left(), out failed);
                            if (failed)
                            {
                                for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                                {
                                    for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                    {
                                        chunk.Value.voxelsLeft[voxelIndex] = edgeVoxelType;
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedLeft = 1;
                            }
                            else if (otherChunk.isGenerating == 0)
                            {
                                // For All Right Voxels of Other Chunk
                                position.x = (int)chunk.Value.voxelDimensions.x - 1;
                                for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                                {
                                    for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                    {
                                        otherChunkVoxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position, chunk.Value.voxelDimensions);
                                        chunk.Value.voxelsLeft[voxelIndex] = otherChunk.Value.voxels[otherChunkVoxelIndex];
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedLeft = 1;
                            }
                        }
                        else
                        {
                            for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                            {
                                for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                {
                                    chunk.Value.voxelsLeft[voxelIndex] = edgeVoxelType;
                                    voxelIndex++;
                                }
                            }
                            chunk.hasUpdatedLeft = 1;
                        }
                    }
                    if (chunk.hasUpdatedRight == 0)
                    {
                        voxelIndex = 0;
                        if (chunk.indexRight != -1)
                        {
                            Chunk otherChunk = GetChunk(ref chunk, zoxID.creatorID, int3.Right(), out failed);
                            if (failed)
                            {
                                for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                                {
                                    for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                    {
                                        chunk.Value.voxelsRight[voxelIndex] = edgeVoxelType;                                           // 0
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedRight = 1;
                            }
                            else if (otherChunk.isGenerating == 0)
                            {
                                // For All Left Voxels of Other Chunk
                                position.x = 0;
                                for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                                {
                                    for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                    {
                                        otherChunkVoxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position, chunk.Value.voxelDimensions);
                                        chunk.Value.voxelsRight[voxelIndex] = otherChunk.Value.voxels[otherChunkVoxelIndex];
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedRight = 1;
                            }
                        }
                        else
                        {
                            for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                            {
                                for (position.z = 0; position.z < chunk.Value.voxelDimensions.z; position.z++)
                                {
                                    chunk.Value.voxelsRight[voxelIndex] = edgeVoxelType;                                       // 0
                                    voxelIndex++;
                                }
                            }
                            chunk.hasUpdatedRight = 1;
                        }
                    }
                    #endregion

                    #region ForwardBack
                    if (chunk.hasUpdatedForward == 0)
                    {
                        voxelIndex = 0;
                        if (chunk.indexForward != -1)
                        {
                            Chunk otherChunk = GetChunk(ref chunk, zoxID.creatorID, int3.Forward(), out failed);
                            if (failed)
                            {
                                for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                                {
                                    for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                                    {
                                        chunk.Value.voxelsForward[voxelIndex] = edgeVoxelType;
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedForward = 1;
                            }
                            else if (otherChunk.isGenerating == 0)
                            {
                                // For All Back Voxels of Other Chunk
                                position.z = 0;
                                for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                                {
                                    for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                                    {
                                        otherChunkVoxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position, chunk.Value.voxelDimensions);
                                        chunk.Value.voxelsForward[voxelIndex] = otherChunk.Value.voxels[otherChunkVoxelIndex];
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedForward = 1;
                            }
                        }
                        else
                        {
                            for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                            {
                                for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                                {
                                    chunk.Value.voxelsForward[voxelIndex] = edgeVoxelType;
                                    voxelIndex++;
                                }
                            }
                            chunk.hasUpdatedForward = 1;
                        }
                    }
                    if (chunk.hasUpdatedBack == 0)
                    {
                        voxelIndex = 0;
                        if (chunk.indexBack != -1)
                        {
                            Chunk otherChunk = GetChunk(ref chunk, zoxID.creatorID, int3.Back(), out failed);
                            if (failed)
                            {
                                for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                                {
                                    for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                                    {
                                        chunk.Value.voxelsBack[voxelIndex] = edgeVoxelType;                                           // 0
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedBack = 1;
                            }
                            else if (otherChunk.isGenerating == 0)
                            {
                                // For All Front Voxels of Other Chunk
                                position.z = (int)chunk.Value.voxelDimensions.z - 1;
                                for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                                {
                                    for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                                    {
                                        otherChunkVoxelIndex = VoxelRaycastSystem.GetVoxelArrayIndex(position, chunk.Value.voxelDimensions);
                                        chunk.Value.voxelsBack[voxelIndex] = otherChunk.Value.voxels[otherChunkVoxelIndex];
                                        voxelIndex++;
                                    }
                                }
                                chunk.hasUpdatedBack = 1;
                            }
                        }
                        else
                        {
                            for (position.x = 0; position.x < chunk.Value.voxelDimensions.x; position.x++)
                            {
                                for (position.y = 0; position.y < chunk.Value.voxelDimensions.y; position.y++)
                                {
                                    chunk.Value.voxelsBack[voxelIndex] = edgeVoxelType;                                       // 0
                                    voxelIndex++;
                                }
                            }
                            chunk.hasUpdatedBack = 1;
                        }
                    }
                    #endregion
                    if (chunk.hasUpdatedLeft == 1 && chunk.hasUpdatedRight == 1 &&
                        chunk.hasUpdatedForward == 1 && chunk.hasUpdatedBack == 1)
                    {
                        chunkBuilder.state = 1;
                    }
                }
            }
Example #8
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];
                        }
                    }
                }
            }
        }