UnivoxRenderingJobs.RenderResult[] GenerateBoxelMeshes(Entity chunk, JobHandle handle = default)
        {
            var materialLookup    = GetBufferFromEntity <BlockMaterialIdentityComponent>(true);
            var subMaterialLookup = GetBufferFromEntity <BlockSubMaterialIdentityComponent>(true);
            var blockShapeLookup  = GetBufferFromEntity <BlockShapeComponent>(true);
            var culledFaceLookup  = GetBufferFromEntity <BlockCulledFacesComponent>(true);


            var materials    = materialLookup[chunk].AsNativeArray();
            var blockShapes  = blockShapeLookup[chunk].AsNativeArray();
            var subMaterials = subMaterialLookup[chunk].AsNativeArray();
            var culledFaces  = culledFaceLookup[chunk].AsNativeArray();

            const int maxBatchSize = Byte.MaxValue;

            handle.Complete();

            Profiler.BeginSample("Create Batches");
            var uniqueBatchData = UnivoxRenderingJobs.GatherUnique(materials);

            Profiler.EndSample();

            var meshes = new UnivoxRenderingJobs.RenderResult[uniqueBatchData.Length];

            Profiler.BeginSample("Process Batches");
            for (var i = 0; i < uniqueBatchData.Length; i++)
            {
                var materialId = uniqueBatchData[i];
                Profiler.BeginSample($"Process Batch {i}");
                var gatherPlanerJob = GatherPlanarJobV3.Create(blockShapes, culledFaces, subMaterials, materials,
                                                               uniqueBatchData[i], out var queue);
                var gatherPlanerJobHandle = gatherPlanerJob.Schedule(GatherPlanarJobV3.JobLength, maxBatchSize);

                var writerToReaderJob = new NativeQueueToNativeListJob <PlanarData>()
                {
                    OutList = new NativeList <PlanarData>(Allocator.TempJob),
                    Queue   = queue
                };
                writerToReaderJob.Schedule(gatherPlanerJobHandle).Complete();
                queue.Dispose();
                var planarBatch = writerToReaderJob.OutList;

                //Calculate the Size Each Voxel Will Use
//                var cubeSizeJob = CreateCalculateCubeSizeJob(batch, chunk);
                var cubeSizeJob = UnivoxRenderingJobs.CreateCalculateCubeSizeJobV2(planarBatch);

                //Calculate the Size of the Mesh and the position to write to per voxel
                var indexAndSizeJob = UnivoxRenderingJobs.CreateCalculateIndexAndTotalSizeJob(cubeSizeJob);
                //Schedule the jobs
                var cubeSizeJobHandle     = cubeSizeJob.Schedule(planarBatch.Length, maxBatchSize);
                var indexAndSizeJobHandle = indexAndSizeJob.Schedule(cubeSizeJobHandle);
                //Complete these jobs
                indexAndSizeJobHandle.Complete();

                //GEnerate the mesh
//                var genMeshJob = CreateGenerateCubeBoxelMeshV2(planarBatch, offsets, indexAndSizeJob);
                var genMeshJob = UnivoxRenderingJobs.CreateGenerateCubeBoxelMeshV2(planarBatch, indexAndSizeJob);
                //Dispose unneccessary native arrays
                indexAndSizeJob.TriangleTotalSize.Dispose();
                indexAndSizeJob.VertexTotalSize.Dispose();
                //Schedule the generation
                var genMeshHandle =
                    genMeshJob.Schedule(planarBatch.Length, maxBatchSize, indexAndSizeJobHandle);

                //Finish and Create the Mesh
                genMeshHandle.Complete();
                planarBatch.Dispose();
                meshes[i] = new UnivoxRenderingJobs.RenderResult()
                {
                    Mesh     = UnivoxRenderingJobs.CreateMesh(genMeshJob),
                    Material = materialId
                };
                Profiler.EndSample();
            }

            Profiler.EndSample();

//            offsets.Dispose();
            uniqueBatchData.Dispose();
            return(meshes);
        }
Beispiel #2
0
    protected override JobHandle OnUpdate(JobHandle inputDeps)
    {
        if (!initialized)
        {
            Initialize();
            initialized = true;
        }

        if (uninitializedPlayfieldGroup.CalculateLength() == 0)
        {
            return(inputDeps);
        }

        resultPositions.Clear();
        playfieldIndex.Clear();

        SharedComponentDataArray <Playfield> playfieldArray = uninitializedPlayfieldGroup.GetSharedComponentDataArray <Playfield>();
        EntityArray entityArray = uninitializedPlayfieldGroup.GetEntityArray();
        Playfield   playfield   = entityManager.GetSharedComponentData <Playfield>(entityArray[0]);

        for (int i = 0; i < playfieldArray.Length; i++)
        {
            playfieldIndex.TryAdd(playfieldArray[i].index, i);
        }

        JobHandle combinedHandle = new JobHandle();
        int       totalNumCells  = 0;

        for (int i = 0; i < playfieldArray.Length; i++)
        {
            int size = playfieldArray[i].width * playfieldArray[i].height;
            totalNumCells += size;
            JobHandle pHandle = new PlayfieldCellPositionJob {
                width          = playfieldArray[i].width,
                height         = playfieldArray[i].height,
                playfieldIndex = i,
                cellDatas      = positions.ToConcurrent(),
            }.Schedule(size, 64, inputDeps);

            if (i == 0)
            {
                combinedHandle = pHandle;
            }
            else
            {
                combinedHandle = JobHandle.CombineDependencies(combinedHandle, pHandle);
            }
        }

        JobHandle qlHandle = new NativeQueueToNativeListJob <CellData> {
            queue    = positions,
            out_list = resultPositions,
        }.Schedule(combinedHandle);

        JobHandle pijHandle = new PlayfieldCellCreationJob {
            commandBuffer          = initBarrier.CreateCommandBuffer().ToConcurrent(),
            positions              = resultPositions,
            playfieldCellArchetype = playfieldCellArchetype,
            playfieldArray         = playfieldArray,
            playfieldIndex         = playfieldIndex,
        }.Schedule(totalNumCells, 64, qlHandle);

        JobHandle pfjHandle = new PlayfieldFinalizeJob {
            commandBuffer = finalizeBarrier.CreateCommandBuffer().ToConcurrent(),
            playfields    = entityArray,
        }.Schedule(entityArray.Length, 64, pijHandle);

        return(pfjHandle);
    }