예제 #1
0
        void ScheduleLateralProfileCreationJobs(JobHandle inputDeps, out JobHandle outputDeps)
        {
            m_SurfaceEntities     = new NativeList <Entity>(Allocator.TempJob);
            m_RoadMarkingEntities = new NativeList <Entity>(Allocator.TempJob);

            var profileEntities         = new NativeList <Entity>(Allocator.TempJob);
            var numLanesArray           = new NativeList <int>(Allocator.TempJob);
            var surfaceStartIndices     = new NativeList <int>(Allocator.TempJob);
            var roadMarkingStartIndices = new NativeList <int>(Allocator.TempJob);

            var resizeNumExtraLanesArrayJob = new ResizeNumExtraLanesArrayJob
            {
                RoadEntities  = m_RoadEntities.AsDeferredJobArray(),
                NumExtraLanes = numLanesArray
            }.Schedule(inputDeps);

            JobHandle calculateNumOfExtraLanesJobHandle;

            if (Parameters.lateralProfileParameters.randomNumLanes)
            {
                var randomSeed = RandomUtility.CombineSeedWithBaseSeed(
                    Parameters.baseRandomSeed, Parameters.lateralProfileParameters.randomSeed);
                var setNumOfExtraLanesJob = new SetRandomNumOfLanes
                {
                    MaxNumLanes   = Parameters.lateralProfileParameters.numLanes,
                    RoadEntities  = m_RoadEntities.AsDeferredJobArray(),
                    RandomSeed    = randomSeed,
                    NumLanesArray = numLanesArray.AsDeferredJobArray(),
                    RoadCenterLineDataComponents = GetComponentDataFromEntity <RoadCenterLineData>()
                }.Schedule(numLanesArray, 2, resizeNumExtraLanesArrayJob);
                calculateNumOfExtraLanesJobHandle = setNumOfExtraLanesJob;
            }
            else
            {
                var setNumOfExtraLanesJob = new SetNumOfLanes
                {
                    NumLanes      = Parameters.lateralProfileParameters.numLanes,
                    NumLanesArray = numLanesArray.AsDeferredJobArray()
                }.Schedule(numLanesArray, 2, resizeNumExtraLanesArrayJob);
                calculateNumOfExtraLanesJobHandle = setNumOfExtraLanesJob;
            }

            var resizeLateralProfileEntityArrays = new ResizeLateralProfileEntityArraysJob
            {
                NumLanes                = numLanesArray.AsDeferredJobArray(),
                SurfaceStartIndices     = surfaceStartIndices,
                RoadMarkingStartIndices = roadMarkingStartIndices,
                ProfileEntities         = profileEntities,
                SurfaceEntities         = m_SurfaceEntities,
                RoadMarkingEntities     = m_RoadMarkingEntities,
            }.Schedule(calculateNumOfExtraLanesJobHandle);

            var createRoadMarkingEntities = new CreateRoadMarkingEntities
            {
                Transaction          = m_Transaction,
                ProfileArchetype     = m_ProfileArchetype,
                SurfaceArchetype     = m_SurfaceArchetype,
                RoadMarkingArchetype = m_RoadMarkingArchetype,
                ProfileEntities      = profileEntities.AsDeferredJobArray(),
                SurfaceEntities      = m_SurfaceEntities.AsDeferredJobArray(),
                RoadMarkingEntities  = m_RoadMarkingEntities.AsDeferredJobArray(),
            }.Schedule(resizeLateralProfileEntityArrays);

            var addDefaultLateralProfileToRoadsJob = new AddDefaultLateralProfileToRoadsJob
            {
                ProfileParams           = Parameters.lateralProfileParameters.GetParams(),
                MarkingParams           = Parameters.roadMarkingParameters.GetParams(),
                RoadEntities            = m_RoadEntities.AsDeferredJobArray(),
                ProfileEntities         = profileEntities.AsDeferredJobArray(),
                SurfaceEntities         = m_SurfaceEntities.AsDeferredJobArray(),
                RoadMarkingEntities     = m_RoadMarkingEntities.AsDeferredJobArray(),
                NumLanes                = numLanesArray.AsDeferredJobArray(),
                SurfaceStartIndices     = surfaceStartIndices.AsDeferredJobArray(),
                RoadMarkingStartIndices = roadMarkingStartIndices.AsDeferredJobArray(),
                Profiles                = GetComponentDataFromEntity <LateralProfile>(),
                Surfaces                = GetComponentDataFromEntity <LateralProfileSurface>(),
                RoadMarkings            = GetComponentDataFromEntity <RoadMarking>(),
                ProfileBuffers          = GetBufferFromEntity <LateralProfileEntityRef>(),
                SurfaceBuffers          = GetBufferFromEntity <LateralProfileSurfaceEntityRef>(),
                SampleBuffers           = GetBufferFromEntity <LateralProfileSample>()
            }.Schedule(numLanesArray, 1, createRoadMarkingEntities);

            profileEntities.Dispose(addDefaultLateralProfileToRoadsJob);
            numLanesArray.Dispose(addDefaultLateralProfileToRoadsJob);
            surfaceStartIndices.Dispose(addDefaultLateralProfileToRoadsJob);
            roadMarkingStartIndices.Dispose(addDefaultLateralProfileToRoadsJob);

            // NOTE: We call the ScheduleBatchedJobs API here to force the jobs scheduled before this point to start
            //     executing, otherwise the scheduled jobs will continue to wait until the final job is scheduled in
            //     this system.
            JobHandle.ScheduleBatchedJobs();
            outputDeps = addDefaultLateralProfileToRoadsJob;
        }
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            if (Utilities.GeometryUtility.ApproximatelyEqual(Parameters.decalDensity, 0f))
            {
                return(inputDeps);
            }

            var roadEntities = GetEntityQuery(typeof(RoadCenterLineData))
                               .ToEntityArrayAsync(Allocator.TempJob, out var queryHandle);
            var roadSampleBuffers = GetBufferFromEntity <RoadCenterLineSample>();
            var numDecalsPerRoad  = new NativeList <int>(Allocator.TempJob);
            var decalStartIndices = new NativeList <int>(Allocator.TempJob);
            var decalPoses        = new NativeList <RigidTransform>(Allocator.TempJob);

            var resizeNumDecalsArrayJob = new ResizeNumDecalsArrayJob
            {
                RoadEntities     = roadEntities,
                NumDecalsPerRoad = numDecalsPerRoad
            }.Schedule(queryHandle);

            var calculateNumDecalsPerRoadJob = new CalculateNumDecalsPerRoadJob
            {
                Density           = Parameters.decalDensity,
                NumDecalsPerRoad  = numDecalsPerRoad.AsDeferredJobArray(),
                RoadEntities      = roadEntities,
                RoadSampleBuffers = roadSampleBuffers
            }.Schedule(numDecalsPerRoad, 1, resizeNumDecalsArrayJob);

            var initializeDecalPositionsArrayJob = new InitializeDecalPositionsArrayJob
            {
                NumDecalsPerRoad  = numDecalsPerRoad.AsDeferredJobArray(),
                DecalStartIndices = decalStartIndices,
                DecalPoses        = decalPoses
            }.Schedule(calculateNumDecalsPerRoadJob);

            initializeDecalPositionsArrayJob.Complete();

            var spreadDecalsThroughoutRoadsJob = new SpreadDecalsThroughoutRoadsJob
            {
                RandomSeed        = RandomUtility.CombineSeedWithBaseSeed(Parameters.baseRandomSeed, Parameters.randomSeed),
                DecalPoses        = decalPoses.AsDeferredJobArray(),
                DecalStartIndices = decalStartIndices.AsDeferredJobArray(),
                RoadEntities      = roadEntities,
                RoadSampleBuffers = roadSampleBuffers,
                Profiles          = GetComponentDataFromEntity <LateralProfile>(),
                ProfileBuffers    = GetBufferFromEntity <LateralProfileEntityRef>(),
                NumDecalsPerRoad  = numDecalsPerRoad.AsDeferredJobArray(),
            }.Schedule(decalStartIndices, 1, initializeDecalPositionsArrayJob);

            spreadDecalsThroughoutRoadsJob.Complete();

            foreach (var pose in decalPoses)
            {
                var decalObj = new GameObject("ManholeCoverDecal");
                decalObj.transform.parent    = Parameters.parentObject.transform;
                decalObj.transform.position  = pose.pos + new float3(0, 0.25f, 0);
                decalObj.transform.rotation *= pose.rot;
                var projector = decalObj.AddComponent <DecalProjector>();
                projector.material = Parameters.material;
            }

            roadEntities.Dispose(spreadDecalsThroughoutRoadsJob);
            numDecalsPerRoad.Dispose(spreadDecalsThroughoutRoadsJob);
            decalStartIndices.Dispose(spreadDecalsThroughoutRoadsJob);
            decalPoses.Dispose(spreadDecalsThroughoutRoadsJob);

            return(spreadDecalsThroughoutRoadsJob);
        }