コード例 #1
0
ファイル: VoxelRenderer.cs プロジェクト: xenrik/blocks
    private IEnumerator BuildMesh()
    {
        Stopwatch timer = new Stopwatch();

        timer.Start();

        Stopwatch yieldTimer = new Stopwatch();

        yieldTimer.Start();

        Debug.Log("Building Mesh...");

        // Work our which faces we need. We do this by checking to see if a two voxels share and adjacent
        // side.

        ProgressMonitor.Begin("Building Mesh", VoxelMap.Size);

        VoxelMesh voxelMesh = new VoxelMesh();

        voxelMaterials = new Dictionary <int, Material>();
        foreach (var voxel in VoxelMap)
        {
            ProgressMonitor.Worked(1);
            if (NeedsYield(yieldTimer))
            {
                yield return(null);
            }

            if (voxel.Value == 0)
            {
                continue;
            }

            Material material = GetMaterial(voxel.Value);
            foreach (FaceDirection dir in directions)
            {
                if (GetAdjacentVoxel(VoxelMap, voxel.Key, dir) == 0)
                {
                    // Add the face
                    VoxelFace face = new VoxelFace(voxel.Key, dir, halfScale);
                    voxelMesh.Add(face, material);
                } // else don't need a face here
            }
        }

        // Update and parent the meshes
        voxelMesh.Update();
        foreach (var meshGO in voxelMesh.GetGameObjects())
        {
            meshGO.transform.parent        = gameObject.transform;
            meshGO.transform.localPosition = VoxelMap.Offset; // Not sure this is right...should it be scaled?
            meshGO.transform.localRotation = Quaternion.identity;
        }

        // We're done!
        ProgressMonitor.Finished();
        VoxelMesh = voxelMesh;

        Debug.Log($"Completed in {timer.ElapsedMilliseconds / 1000.0f:F2}s");
    }