コード例 #1
0
    public BlockTypes[] CalculateBlockTypes(HeightData[] heights)
    {
        var inputSize = _totalBlockNumberX * _totalBlockNumberY * _totalBlockNumberZ;

        // output data
        var types = new BlockTypes[inputSize];

        var typeJob = new BlockTypeJob()
        {
            // input
            TotalBlockNumberX = _totalBlockNumberX,
            TotalBlockNumberY = _totalBlockNumberY,
            TotalBlockNumberZ = _totalBlockNumberZ,
            Heights           = new NativeArray <HeightData>(heights, Allocator.TempJob),

            // output
            Result = new NativeArray <BlockTypes>(types, Allocator.TempJob)
        };

        var typeJobHandle = typeJob.Schedule(inputSize, 8);

        typeJobHandle.Complete();
        typeJob.Result.CopyTo(types);

        // cleanup
        typeJob.Result.Dispose();
        typeJob.Heights.Dispose();

        return(types);
    }
コード例 #2
0
        internal static BlockType[] CalculateBlockTypes(ReadonlyVector3Int[] heights)
        {
            var inputSize = TotalBlockNumberX * TotalBlockNumberY * TotalBlockNumberZ;

            // output data
            var types = new BlockType[inputSize];

            var typeJob = new BlockTypeJob()
            {
                // input
                TotalBlockNumberX = TotalBlockNumberX,
                TotalBlockNumberY = TotalBlockNumberY,
                TotalBlockNumberZ = TotalBlockNumberZ,
                Heights           = new NativeArray <ReadonlyVector3Int>(heights, Allocator.TempJob),

                // output
                Result = new NativeArray <BlockType>(types, Allocator.TempJob)
            };

            var typeJobHandle = typeJob.Schedule(inputSize, 8);

            typeJobHandle.Complete();
            typeJob.Result.CopyTo(types);

            // cleanup
            typeJob.Result.Dispose();
            typeJob.Heights.Dispose();

            return(types);
        }
コード例 #3
0
        protected override void OnUpdate()
        {
            if (flag)
            {
                return;
            }

            var blockTypesType = GetArchetypeChunkComponentType <BlockTypesComponent>();      // read-write access
            var coordinateType = GetArchetypeChunkComponentType <CoordinatesComponent>(true); // true means it is read only
            var seedType       = GetArchetypeChunkComponentType <SeedComponent>(true);        // true means it is read only

            var job = new BlockTypeJob()
            {
                BlockTypes  = blockTypesType,
                Coordinates = coordinateType,
                Seed        = seedType
            };

            JobHandle jobHandle = job.Schedule(_query);

            jobHandle.Complete(); // wait until completed

            // all calculations are done in one frame
            EntityManager entityManager = World.DefaultGameObjectInjectionWorld.EntityManager;

            // there is a bug here - throws ECS-related null reference exception
            Entities
            // allows us to modify or delete entities
            //.WithStructuralChanges()
            // this serves as a signature as well
            .ForEach((Entity entity, in CoordinatesComponent coordinates, in BlockTypesComponent blockTypes) =>
            {
                // copy data to the main array
                //TerrainGenerator.CreateBlock(
                //    ref GlobalVariables.Blocks[coordinates.Coordinates.x, coordinates.Coordinates.y, coordinates.Coordinates.z],
                //    blockTypes.BlockType);

                // clean up and prevent further calculations
                //entityManager.DestroyEntity(entity);
            }).Run();     // "Run" means run on the main thread