Ejemplo n.º 1
0
    public void ChangeBlock(Vector3 globalPosition, byte type)
    {
        Vector3Int voxelPos = VoxelConversions.WorldToVoxel(globalPosition);

        //Debug.LogFormat("globalPostion: {0}", globalPosition);
        ChangeBlock(voxelPos, type);
    }
Ejemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        if (player != null)
        {
            newPlayerVoxelPos = VoxelConversions.WorldToVoxel(player.transform.position);
            newPlayerChunkPos = VoxelConversions.VoxelToChunk(newPlayerVoxelPos);
            //Debug.Log(generateArroundChunk + ", " + newPlayerChunkPos + ", " + Vector3.Distance(generateArroundChunk, newPlayerChunkPos));
            if (Vector3.Distance(_generateArroundChunk, newPlayerChunkPos) > 0 && !_generating)
            {
                // generate around point.
                //Debug.Log("Debug filling " + newPlayerChunkPos + ".");
                _generateArroundChunk = newPlayerChunkPos;
                GenerateSpherical(_generateArroundChunk, null);
            }

            /*if (Vector3.Distance(_oldPlayerVoxelPos, newPlayerVoxelPos) > 2)
             * {
             *  _oldPlayerVoxelPos = newPlayerVoxelPos;
             *  DeleteGrass();
             *  SpawnGrass();
             * }*/
        }

        if (chunksInQueue > 0)
        {
            GameManager.Status = string.Format("Generating chunk {0}/{1}.", chunksGenerated, chunksInQueue);
        }
    }
Ejemplo n.º 3
0
    /*public void SetSurfacePoints(Vector3Int[] points)
     * {
     *  for (int i = 0; i < points.Length; i++)
     *      if (!surfacePoints.Contains(points[i]))
     *          surfacePoints.Add(points[i]);
     * }*/

    public void SetBlockAtLocation(Vector3 position, byte type)
    {
        Vector3Int voxelPos   = VoxelConversions.WorldToVoxel(position);
        Vector3Int chunk      = VoxelConversions.VoxelToChunk(voxelPos);
        Vector3Int localVoxel = VoxelConversions.GlobalVoxToLocalChunkVoxCoord(chunk, voxelPos);

        if (BuilderExists(chunk.x, chunk.y, chunk.z))
        {
            Chunks[new Vector3Int(chunk.x, chunk.y, chunk.z)].EditNextFrame(new Chunk.BlockChange[] { new Chunk.BlockChange(position, type) });
        }
    }
Ejemplo n.º 4
0
    public void Generate(int height)
    {
        int chunkSizeX = SmoothVoxelSettings.ChunkSizeX;
        int chunkSizeY = SmoothVoxelSettings.ChunkSizeY;
        int chunkSizeZ = SmoothVoxelSettings.ChunkSizeZ;

        int meterSizeX = SmoothVoxelSettings.MeterSizeX;
        int meterSizeY = SmoothVoxelSettings.MeterSizeY;
        int meterSizeZ = SmoothVoxelSettings.MeterSizeZ;

        Sampler.SetChunkSettings(SmoothVoxelSettings.voxelsPerMeter,
                                 new Vector3Int(chunkSizeX, chunkSizeY, chunkSizeZ),
                                 new Vector3Int(meterSizeX, meterSizeY, meterSizeZ),
                                 Mathf.RoundToInt(1 / (float)SmoothVoxelSettings.voxelsPerMeter),
                                 ((1.0f / (float)SmoothVoxelSettings.voxelsPerMeter) / 2.0f),
                                 new Vector3(meterSizeX / (float)chunkSizeX, meterSizeY / (float)chunkSizeY, meterSizeZ / (float)chunkSizeZ));

        Vector3Int topVoxel    = VoxelConversions.WorldToVoxel(new Vector3(0, (float)Sampler.GetMax(), 0));
        Vector3Int bottomVoxel = VoxelConversions.WorldToVoxel(new Vector3(0, (float)Sampler.GetMin(), 0));

        int topChunk    = VoxelConversions.VoxelToChunk(topVoxel).y;
        int bottomChunk = VoxelConversions.VoxelToChunk(bottomVoxel).y;

        if (NetworkMode)
        {
            for (int y = 0; y <= topChunk; y++)
            {
                SmoothChunk.CreateChunk(new Vector3Int(Location.x, y, Location.y), Sampler, Controller);
            }
        }
        else
        {
            Vector2Int bottomLeft = new Vector2(Location.x * chunkSizeX, Location.y * chunkSizeZ);
            Vector2Int topRight   = new Vector2(Location.x * chunkSizeX + chunkSizeX, Location.y * chunkSizeZ + chunkSizeZ);
            Sampler.SetSurfaceData(bottomLeft, topRight);

            for (int y = 0; y <= topChunk; y++)
            {
                SmoothChunk.CreateChunk(new Vector3Int(Location.x, y, Location.y), Sampler, Controller);
            }
            Loom.QueueOnMainThread(() =>
            {
                Debug.Log("Spawning grass...");
                SpawnGrass();
            });
        }
    }
Ejemplo n.º 5
0
 public void GenerateExplosion(Vector3 postion, int radius)
 {
     System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
     watch.Start();
     Loom.AddAsyncThread("Explosion");
     Loom.QueueAsyncTask("Explosion", () =>
     {
         Dictionary <Vector3Int, List <Chunk.BlockChange> > changes = new Dictionary <Vector3Int, List <Chunk.BlockChange> >();
         Vector3Int voxelPos = VoxelConversions.WorldToVoxel(postion);
         for (int x = voxelPos.x - radius; x <= voxelPos.x + radius; x++)
         {
             for (int y = voxelPos.y - radius; y <= voxelPos.y + radius; y++)
             {
                 for (int z = voxelPos.z - radius; z <= voxelPos.z + radius; z++)
                 {
                     Vector3Int voxel = new Vector3Int(x, y, z);
                     Vector3Int chunk = VoxelConversions.VoxelToChunk(voxel);
                     if (IsInSphere(voxelPos, radius, voxel))
                     {
                         if (!changes.ContainsKey(chunk))
                         {
                             changes.Add(chunk, new List <Chunk.BlockChange>());
                         }
                         changes[chunk].Add(new Chunk.BlockChange(VoxelConversions.GlobalVoxToLocalChunkVoxCoord(chunk, voxel), 0));
                         //ChangeBlock(new Chunk.BlockChange(voxel, 0));
                     }
                 }
             }
         }
         //Debug.Log("Iterated through exploded blocks: " + watch.Elapsed.ToString());
         Loom.QueueOnMainThread(() =>
         {
             foreach (Vector3Int chunkPos in changes.Keys)
             {
                 ChangeBlock(chunkPos, changes[chunkPos].ToArray());
             }
             watch.Stop();
             //Debug.Log("Blocks changes sent to chunk: " + watch.Elapsed.ToString());
         });
     });
 }
    // Update is called once per frame
    void Update()
    {
        if (cameraObj != null)
        {
            newCameraVoxelPos = VoxelConversions.WorldToVoxel(cameraObj.transform.position);
            newCameraChunkPos = VoxelConversions.VoxelToChunk(newCameraVoxelPos);
            //Debug.Log(generateArroundChunk + ", " + newPlayerChunkPos + ", " + Vector3.Distance(generateArroundChunk, newPlayerChunkPos));

            /*if (Vector3.Distance(_oldPlayerVoxelPos, newPlayerVoxelPos) > 2)
             * {
             *  _oldPlayerVoxelPos = newPlayerVoxelPos;
             *  DeleteGrass();
             *  SpawnGrass();
             * }*/
        }

        if (chunksInQueue > 0)
        {
            GameManager.Status = string.Format("Generating chunk {0}/{1}.", chunksGenerated, chunksInQueue);
        }
    }
Ejemplo n.º 7
0
 // Update is called once per frame
 void Update()
 {
     if (player != null)
     {
         _playerCreated    = true;
         _playerPosition   = player.transform.position;
         LODtarget         = _playerPosition;
         newPlayerVoxelPos = VoxelConversions.WorldToVoxel(_playerPosition);
         newPlayerChunkPos = VoxelConversions.VoxelToChunk(newPlayerVoxelPos);
         //Debug.Log(generateArroundChunk + ", " + newPlayerChunkPos + ", " + Vector3.Distance(generateArroundChunk, newPlayerChunkPos));
         if (_oldPlayerChunkPos != newPlayerChunkPos && !_generating)
         {
             // generate around point.
             //Debug.Log("Debug filling " + newPlayerChunkPos + ".");
             _generateArroundChunk = _oldPlayerChunkPos = newPlayerChunkPos;
             GenerateSpherical(newPlayerChunkPos);
         }
         foreach (SmoothChunk chk in Chunks.Values.ToArray())
         {
             chk.ChunkUpdate();
         }
     }
 }
Ejemplo n.º 8
0
        public void Generate()
        {
            if (empty)
            {
                return;
            }

            if (!Initialized)
            {
                return;
            }

            Stopwatch watch = new Stopwatch();

            watch.Start();

            //surfaceBlocksCount = 0;
            GeneratedBlocks = 0;

            int cx = Location.x;
            int cy = Location.y;
            int cz = Location.z;

            int xStart = cx * ChunkSizeX;
            int xEnd   = cx * ChunkSizeX + ChunkSizeX;

            int yStart = cy * ChunkSizeY;
            int yEnd   = cy * ChunkSizeY + ChunkSizeY;

            int zStart = cz * ChunkSizeZ;
            int zEnd   = cz * ChunkSizeZ + ChunkSizeZ;

            int globalLocX = 0;
            int globalLocY = 0;
            int globalLocZ = 0;

            int x = 0;
            int y = 0;
            int z = 0;

            int Y_Min = 0;
            int Y_Max = ChunkSizeY;

            int heightmapMin = VoxelConversions.WorldToVoxel(new Vector3(0, Math.Max(0, (float)Sampler.GetMin() - Depth), 0)).y;

            Logger.Log("heightmapMin: {0}", heightmapMin);

            int heightmapMin_local = VoxelConversions.GlobalToLocalChunkCoord(new Vector3Int(0, heightmapMin, 0)).y;

            Logger.Log("heightmapMin_local: {0}", heightmapMin_local);

            int heightmapMax = VoxelConversions.WorldToVoxel(new Vector3(0, Math.Min(SmoothVoxelSettings.MeterSizeY, (float)Sampler.GetMax()), 0)).y;

            Logger.Log("heightmapMax: {0}", heightmapMax);

            int heightmapMax_local = VoxelConversions.GlobalToLocalChunkCoord(new Vector3Int(0, heightmapMax, 0)).y;

            Logger.Log("heightmapMax_local: {0}", heightmapMax_local);

            Logger.Log("Generating with updated mode {0}: {1}", updateMode.ToString(), DebugTimer.Elapsed());

            if (updateMode == UpdateMode.EmptyToHeightmap)
            {
                return;
            }
            else if (updateMode == UpdateMode.EmptyToReduced || updateMode == UpdateMode.EmptyToFull ||
                     updateMode == UpdateMode.HeightmapToReduced || updateMode == UpdateMode.HeightmapToFull)
            {
                //Min = int.MaxValue;
                //Max = int.MinValue;
                switch (updateMode)
                {
                case UpdateMode.EmptyToReduced:
                case UpdateMode.HeightmapToReduced:
                    Y_Min   = heightmapMin_local;
                    yStart += Y_Min;
                    Y_Max   = heightmapMax_local;
                    break;

                case UpdateMode.EmptyToFull:
                case UpdateMode.HeightmapToFull:
                    Y_Max = heightmapMax_local;
                    break;
                }
            }
            else if (updateMode == UpdateMode.ReducedToFull)
            {
                Y_Max = heightmapMin_local;
                Logger.Log("ReducedToFull: " + heightmapMin_local);
            }

            //else if(updateMode == UpdateMode.)

            //if (LoadedFromDisk && !ReduceDepth && Max_Mode == LOD_Mode.Full)
            //    Y_Max = VoxelConversions.GlobalToLocalChunkCoord(VoxelConversions.WorldToVoxel(new Vector3(0, (float)Sampler.GetMin() - Depth, 0))).y;

            UnityGameServer.Logger.Log("Column: Y_Min: {0}, Y_Max: {1}", Y_Min, Y_Max);

            col_data.Allocate(ChunkSizeX, ChunkSizeY, ChunkSizeZ);
            builder.Generate(Y_Min, Y_Max, xStart, zStart);


            /*for (globalLocY = yStart, y = Y_Min; y < Y_Max; globalLocY++, y++)
             * {
             *
             *  Max = Mathf.Max(y, Max);
             *  Min = Mathf.Min(y, Min);
             *
             *  for (globalLocZ = zStart, z = 0; z < ChunkSizeZ; globalLocZ++, z++)
             *  {
             *      for (globalLocX = xStart, x = 0; x < ChunkSizeX; globalLocX++, x++)
             *      {
             *          if (deactivated)
             *          {
             *              break;
             *          }
             *
             *          Vector3 worldPos = new Vector3(x * xSideLength, y * ySideLength, z * zSideLength);
             *          Vector3Int globalPos = new Vector3Int(globalLocX * skipDist, globalLocY * skipDist, globalLocZ * skipDist);
             *          Vector3Int localPos = new Vector3Int(x, y, z);
             *          GridPoint[] grid = new GridPoint[8];
             *          for (int i = 0; i < grid.Length; i++)
             *              grid[i] = GetGridPoint(worldPos + locOffset[i], localPos + directionOffsets[i], globalPos + globalOffsets[i]);
             *          ProcessBlock(grid, 0);
             *          GeneratedBlocks++;
             *      }
             *  }
             * }*/

            watch.Stop();
            Logger.Log("Generated from {0} to {1}: " + DebugTimer.Elapsed(), Y_Min, Y_Max);
            Logger.Log("Generated surface blocks: {0}", col_data.surfaceBlocksCount);
        }