예제 #1
0
        public void Generate()
        {
            System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            timer.Start();

            MeshData meshData = MarchingCubes.Poligonyze(
                new Bounds(center, terrainMaster.chunkSize),
                Sample,
                terrainMaster.resolution,
                (1 - terrainMaster.soilToAirVolumetricRatio) * 2 - 1);

            timer.Stop();
            Debug.Log("Chunk " + center + ": MarchingCubes.Poligonyze in " + timer.ElapsedMilliseconds + " milliseconds.");

            Matrix4x4 matrix = Matrix4x4.Scale(Vector3.one * terrainMaster.postProcessingScale) * Matrix4x4.Translate(-center);

            meshData.Transform(matrix);

            ThreadWorkManager.RequestMainThreadWork(() =>
            {
                gameObject       = new GameObject();
                gameObject.name  = center.ToString();
                gameObject.layer = LayerMask.NameToLayer("Terrain");

                Mesh mesh = meshData.ToMesh();

                gameObject.AddComponent <MeshFilter>().sharedMesh       = mesh;
                gameObject.AddComponent <MeshRenderer>().sharedMaterial = terrainMaster.chunkMaterial;
                gameObject.AddComponent <MeshCollider>().sharedMesh     = mesh;

                gameObject.transform.parent   = terrainMaster.transform;
                gameObject.transform.position = center * terrainMaster.postProcessingScale;
                gameObject.SetActive(isVisible);
            });
        }
예제 #2
0
        public TerrainChunk(Vector3 center, TerrainMaster terrainMaster)
        {
            this.center        = center;
            this.terrainMaster = terrainMaster;

            ThreadWorkManager.RequestWork(Generate);
        }
예제 #3
0
    void Start()
    {
        voxelMap = new VoxelMap(width, height);

        ThreadWorkManager.RequestWork(() =>
        {
            GenerateMap();

            SpawnMap();
            SpawnMarchingMap();
        });
    }
예제 #4
0
    void SpawnMarchingMapChunk(Vector3Int center, Vector3Int size)
    {
        MeshData meshData = MarchingCubes.Poligonyze(new PolygonizableVoxelMap(voxelMap), new Bounds(center, size), Vector3.one * 2, 0.5f);

        //Debug.Log("Finished MarchingCubes.Poligonyze at " + center);

        ThreadWorkManager.RequestMainThreadWork(() =>
        {
            GameObject go = new GameObject();
            go.AddComponent <MeshFilter>().mesh             = meshData.ToMesh();
            go.AddComponent <MeshRenderer>().sharedMaterial = voxelMaterial;
            go.transform.rotation = Quaternion.Euler(-90, 0, 0);
        });
    }
예제 #5
0
    void Start()
    {
        Vector3 playerPosition = playerTransform.position;

        ThreadWorkManager.RequestWork(() => UpdateChunks(playerPosition, visibleChunks));

        chunkMaterial.SetFloat("_SimplexNoiseFrequency", TessellationSimplexNoiseFrequency);
        chunkMaterial.SetFloat("_SimplexNoiseAmplitude", TessellationSimplexNoiseAmplitude);
        foreach (var item in FindObjectsOfType <ColliderGenerator>())
        {
            item.TessellationSimplexNoiseFrequency = TessellationSimplexNoiseFrequency;
            item.TessellationSimplexNoiseAmplitude = TessellationSimplexNoiseAmplitude;
        }
    }
예제 #6
0
    void SpawnMarchingMap()
    {
        Vector3Int size = new Vector3Int(Mathf.Min(32, voxelMap.Width),
                                         Mathf.Min(32, voxelMap.Height),
                                         Mathf.Min(32, voxelMap.Depth));

        for (int x = size.x / 2; x <= voxelMap.Width - size.x / 2; x += size.x)
        {
            for (int y = size.y / 2; y <= voxelMap.Height - size.y / 2; y += size.y)
            {
                for (int z = size.z / 2; z <= voxelMap.Depth - size.z / 2; z += size.z)
                {
                    Vector3Int center = new Vector3Int(x, y, z);
                    ThreadWorkManager.RequestWork(() => SpawnMarchingMapChunk(center, size));
                }
            }
        }
    }
예제 #7
0
 private void OnDestroy()
 {
     ThreadWorkManager.Destroy();
 }
예제 #8
0
 private void Update()
 {
     ThreadWorkManager.Update();
 }
예제 #9
0
    void SpawnMap()
    {
        MeshData meshData = new MeshData();

        Vector3Int[] directions = new Vector3Int[]
        {
            new Vector3Int(1, 0, 0),
            new Vector3Int(-1, 0, 0),
            new Vector3Int(0, 1, 0),
            new Vector3Int(0, -1, 0),
            new Vector3Int(0, 0, 1),
            new Vector3Int(0, 0, -1)
        };

        // Invert y and z for visualization purposes
        voxelMap.Foreach((x, y, z, voxelPresent) =>
        {
            if (voxelPresent)
            {
                //Instantiate(voxelPrefab, new Vector3(x, y, z), Quaternion.identity, transform);

                for (int i = 0; i < directions.Length; i++)
                {
                    if (voxelMap.CheckVoxelAt(x + directions[i].x, y + directions[i].y, z + directions[i].z) == false)
                    {
                        CreateSquare(new Vector3Int(x, y, z), meshData, directions[i]);
                    }
                }
            }
        });

        meshData.CalculateNormals();

        ThreadWorkManager.RequestMainThreadWork(() =>
        {
            GameObject go = new GameObject();
            go.AddComponent <MeshFilter>().mesh             = meshData.ToMesh();
            go.AddComponent <MeshRenderer>().sharedMaterial = voxelMaterial;
            go.transform.rotation = Quaternion.Euler(-90, 0, 0);

            /*MeshData m = new MeshData();
             *
             * //CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(1, 0, 0));
             * CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(-1, 0, 0));
             * CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(0, 1, 0));
             * CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(0, -1, 0));
             * //CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(0, 0, 1));
             * CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(0, 0, -1));
             *
             * CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(1, 0, 0));
             * //CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(-1, 0, 0));
             * //CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(0, 1, 0));
             * CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(0, -1, 0));
             * //CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(0, 0, 1));
             * CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(0, 0, -1));
             *
             * CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(1, 0, 0));
             * CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(-1, 0, 0));
             * CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(0, 1, 0));
             * //CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(0, -1, 0));
             * //CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(0, 0, 1));
             * CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(0, 0, -1));
             *
             * //CreateSquare(new Vector3Int(100, 100, 100), m, new Vector3Int(1, 0, 0));
             * CreateSquare(new Vector3Int(100, 100, 101), m, new Vector3Int(-1, 0, 0));
             * CreateSquare(new Vector3Int(100, 100, 101), m, new Vector3Int(0, 1, 0));
             * CreateSquare(new Vector3Int(100, 100, 101), m, new Vector3Int(0, -1, 0));
             * CreateSquare(new Vector3Int(100, 100, 101), m, new Vector3Int(0, 0, 1));
             * //CreateSquare(new Vector3Int(100, 100, 101), m, new Vector3Int(0, 0, -1));
             *
             * CreateSquare(new Vector3Int(101, 100, 101), m, new Vector3Int(1, 0, 0));
             * //CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(-1, 0, 0));
             * //CreateSquare(new Vector3Int(101, 100, 100), m, new Vector3Int(0, 1, 0));
             * CreateSquare(new Vector3Int(101, 100, 101), m, new Vector3Int(0, -1, 0));
             * CreateSquare(new Vector3Int(101, 100, 101), m, new Vector3Int(0, 0, 1));
             * //CreateSquare(new Vector3Int(101, 100, 101), m, new Vector3Int(0, 0, -1));
             *
             * CreateSquare(new Vector3Int(101, 101, 101), m, new Vector3Int(1, 0, 0));
             * CreateSquare(new Vector3Int(101, 101, 101), m, new Vector3Int(-1, 0, 0));
             * CreateSquare(new Vector3Int(101, 101, 101), m, new Vector3Int(0, 1, 0));
             * //CreateSquare(new Vector3Int(101, 101, 100), m, new Vector3Int(0, -1, 0));
             * CreateSquare(new Vector3Int(101, 101, 101), m, new Vector3Int(0, 0, 1));
             * //CreateSquare(new Vector3Int(101, 101, 101), m, new Vector3Int(0, 0, -1));
             *
             * m.Smooth();
             * go = new GameObject();
             * go.AddComponent<MeshFilter>().mesh = m.ToMesh();
             * go.AddComponent<MeshRenderer>().sharedMaterial = voxelMaterial;*/
        });
    }