Ejemplo n.º 1
0
    public void CreateChunk()
    {
        Debug.Log("Creating chunks...");

        //CreateSampler(Samplers.Sphere)

        SmoothChunk.CreateChunk(new Vector3Int(0, 0, 0), CreateSampler(Samplers.Terrain), this);
        //SmoothChunk.CreateChunk(new Vector3Int(1, 0, 0), CreateSampler(Samplers.Sphere), this);
    }
Ejemplo n.º 2
0
 public void DestroyChunk(Vector3Int chunk)
 {
     try {
         if (BuilderExists(chunk.x, chunk.y, chunk.z))
         {
             SmoothChunk chunkInst = Chunks[chunk];
             chunkInst.Close();
             Chunks.Remove(chunk);
             Loom.QueueOnMainThread(() => Destroy(chunkInst.gameObject));
         }
     }
     catch (Exception e) {
         SafeDebug.LogError(string.Format("{0}\n", e.Message), e);
     }
 }
Ejemplo n.º 3
0
 public void GenerateChunk(SmoothChunk chunk)
 {
     try
     {
         if (chunk != null && !chunk.Generated)
         {
             _generating = true;
             //chunk.Generate();
         }
     }
     catch (Exception e)
     {
         SafeDebug.LogError(e.Message + "\nFunction: GenerateChunks: " + chunk.chunkPosition.x + "," + chunk.chunkPosition.y + "," + chunk.chunkPosition.z);
         SafeDebug.LogError(e.StackTrace);
     }
 }
Ejemplo n.º 4
0
    public static void SpawnChunk(Vector3Int chunkPos, ISampler sampler, IPageController controller)
    {
        Vector3 worldPos       = VoxelConversions.ChunkCoordToWorld(chunkPos);
        double  voxelsPerMeter = SmoothVoxelSettings.voxelsPerMeter;

        SmoothVoxelBuilder builder = new SmoothVoxelBuilder(controller, chunkPos);

        builder.SetBlockTypes(controller.BlockTypes, null);
        builder.CalculateVariables(voxelsPerMeter, SmoothVoxelSettings.MeterSizeX, SmoothVoxelSettings.MeterSizeY, SmoothVoxelSettings.MeterSizeZ);
        builder.Generate(sampler);

        SmoothChunk chunk = GameObject.Instantiate(controller.ChunkPrefab).GetComponent <SmoothChunk>();

        chunk.name = string.Format("Chunk_{0}.{1}.{2}", chunkPos.x, chunkPos.y, chunkPos.z);
        chunk.Init(chunkPos, worldPos, null, controller, 1, builder);
    }
Ejemplo n.º 5
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.º 6
0
    public static void CreateChunk(Vector3Int chunkPos, ISampler sampler, IPageController controller)
    {
        Vector3 worldPos       = VoxelConversions.ChunkCoordToWorld(chunkPos);
        double  voxelsPerMeter = SmoothVoxelSettings.voxelsPerMeter;

        SmoothVoxelBuilder builder = new SmoothVoxelBuilder(controller, chunkPos);

        builder.SetBlockTypes(controller.BlockTypes, null);
        builder.CalculateVariables(voxelsPerMeter, SmoothVoxelSettings.MeterSizeX, SmoothVoxelSettings.MeterSizeY, SmoothVoxelSettings.MeterSizeZ);
        builder.Generate(sampler);

        System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch();
        watch.Start();

        MeshData meshData = builder.Render(false);

        watch.Stop();
        Debug.Log("Chunk Generated at " + watch.Elapsed);
        //if (meshData.vertices.Length == 0)
        //    return;

        Loom.QueueOnMainThread(() => {
            SmoothChunk chunk = GameObject.Instantiate(controller.ChunkPrefab).GetComponent <SmoothChunk>();
            chunk.name        = string.Format("Chunk_{0}.{1}.{2}", chunkPos.x, chunkPos.y, chunkPos.z);
            chunk.Init(chunkPos, worldPos, null, controller, 1, builder);
            chunk.Render(meshData, true);
            Loom.QueueAsyncTask("gen2", () => controller.AddChunk(chunkPos, chunk));

            /*if (!TerrainController.Instance.Chunks.ContainsKey(chunkPos))
             * {
             *  SmoothChunk chunk = GameObject.Instantiate(TerrainController.Instance.chunkPrefab).GetComponent<SmoothChunk>();
             *  chunk.name = string.Format("Chunk_{0}.{1}.{2}", chunkPos.x, chunkPos.y, chunkPos.z);
             *  chunk.Init(chunkPos, worldPos, null, controller, 1, builder);
             *  chunk.Render(meshData);
             *  TerrainController.Instance.AddChunk(chunkPos, chunk);
             * }*/
        });
    }
Ejemplo n.º 7
0
    void Update()
    {
        Ray        ray = gameCam.ScreenPointToRay(new Vector3(Screen.width / 2, Screen.height / 2, 0));
        RaycastHit hit;

        if (focused)
        {
            if (axes == RotationAxes.MouseXAndY)
            {
                float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;

                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationY  = Mathf.Clamp(rotationY, minimumY, maximumY);

                transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
            }
            else if (axes == RotationAxes.MouseX)
            {
                transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
            }
            else
            {
                rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
                rotationY  = Mathf.Clamp(rotationY, minimumY, maximumY);

                transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);

                if (Physics.Raycast(ray, out hit, 100))
                {
                    origin = hit.point;
                    point  = origin;
                    point += (new Vector3(hit.normal.x, hit.normal.y, hit.normal.z) * -((1f / 3f) / 4f));
                    Vector3 localPos = transform.InverseTransformPoint(point);
                    if (Input.GetKeyDown(KeyCode.Mouse0))
                    {
                        SmoothChunk chunk = hit.collider.GetComponent <SmoothChunk>();
                        if (chunk)
                        {
                            //chunk.RemoveBlock(hit);
                            Debug.Log("Attempting to remove block");
                        }
                    }
                    if (Input.GetKeyDown(KeyCode.Mouse1))
                    {
                        SmoothChunk chunk = hit.collider.GetComponent <SmoothChunk>();
                        if (chunk)
                        {
                            //chunk.AddBlock(hit, 5);
                            Debug.Log("Attempting to add block");
                        }
                    }
                }
                else
                {
                    origin = Vector3.zero;
                }
            }
        }

        if (origin != Vector3.zero)
        {
            Debug.DrawLine(ray.origin, origin, Color.red);
            Debug.DrawLine(origin, point, Color.blue);
        }

        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            focused = true;
        }
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            focused = false;
        }

        Cursor.visible    = !focused;
        Screen.lockCursor = focused;
    }