Esempio n. 1
0
    public void Update()
    {
        var position = new NativeArray <Vector3>(500, Allocator.Persistent);

        var velocity = new NativeArray <Vector3>(500, Allocator.Persistent);

        for (var i = 0; i < velocity.Length; i++)
        {
            velocity[i] = new Vector3(0, 10, 0);
        }


        // Initialize the job data
        var job = new VelocityJob()
        {
            deltaTime = Time.deltaTime,
            position  = position,
            velocity  = velocity
        };

        // Schedule the job, returns the JobHandle which can be waited upon later on
        JobHandle jobHandle = job.Schedule();

        // Ensure the job has completed
        // It is not recommended to Complete a job immediately,
        // since that gives you no actual parallelism.
        // You optimally want to schedule a job early in a frame and then wait for it later in the frame.
        jobHandle.Complete();

        Debug.Log(job.position[0]);

        // Native arrays must be disposed manually
        position.Dispose();
        velocity.Dispose();
    }
Esempio n. 2
0
    void Update()
    {
        // 1.准备数据
        NativeArray <Vector3> tmpPositions = new NativeArray <Vector3>(gameCount, Allocator.TempJob);
        NativeArray <Vector3> tmpVelocitys = new NativeArray <Vector3>(gameCount, Allocator.TempJob);

        for (int i = 0; i < gameCount; i++)
        {
            tmpVelocitys[i] = new Vector3(0, 1, 0);
            //tmpPositions[i] = tmpPositions[i] + tmpVelocitys[i] * Time.deltaTime;
            tmpPositions[i] = gameObjs[i].transform.position;
        }
        VelocityJob job = new VelocityJob()
        {
            positions = tmpPositions,
            delaTime  = Time.deltaTime,
            velocitys = tmpVelocitys
        };
        // 2.执行
        //信号量 主线程如何知道子线程执行完毕
        JobHandle jobHandle = job.Schedule();

        // 3.同步
        jobHandle.Complete();

        for (int i = 0; i < gameCount; i++)
        {
            gameObjs[i].transform.position = tmpPositions[i];
        }


        tmpPositions.Dispose();
        tmpVelocitys.Dispose();
    }
    void ScheduleParallelTestJobs()
    {
        var positions = new NativeArray <Vector3>(500, Allocator.Persistent);

        var velocities = new NativeArray <Vector3>(500, Allocator.Persistent);

        for (var i = 0; i < velocities.Length; i++)
        {
            velocities[i] = new Vector3(0, 10, 0);
        }

        var job = new VelocityJob()
        {
            deltaTime = Time.deltaTime,
            position  = positions,
            velocity  = velocities
        };

        JobHandle jobHandle = job.Schedule(positions.Length, 64);

        StartCoroutine(WaitForJobToBeFinished(jobHandle,
                                              () =>
        {
            jobHandle.Complete();

            Debug.Log(job.position[0]);

            positions.Dispose();
            velocities.Dispose();
        }
                                              ));
    }
Esempio n. 4
0
    public void Test02()
    {
        Camera camera = this.GetComponent <Camera>();
        NativeArray <Matrix4x4> matrix = new NativeArray <Matrix4x4>(1, Allocator.TempJob);

        matrix[0] = camera.cullingMatrix;
        NativeArray <Bounds> bounds = new NativeArray <Bounds>(TESTCOUNT, Allocator.TempJob);

        for (int i = 0; i < TESTCOUNT; i++)
        {
            bounds[i] = new Bounds();
        }


        stopwatch.Reset();
        stopwatch.Start();
        NativeArray <bool> results  = new NativeArray <bool>(TESTCOUNT, Allocator.TempJob);
        VelocityJob        velocity = new VelocityJob()
        {
            matrix = matrix, bounds = bounds, results = results
        };
        JobHandle jobHandle = velocity.Schedule(TESTCOUNT, 1);

        jobHandle.Complete();

        stopwatch.Stop();
        UnityEngine.Debug.Log($"task {stopwatch.ElapsedMilliseconds}");

        matrix.Dispose();
        bounds.Dispose();
        results.Dispose();
    }
        protected override JobHandle OnUpdate(JobHandle inputDependencies)
        {
            var job = new VelocityJob()
            {
                DeltaTime = Time.deltaTime
            };

            return(job.Schedule(this, inputDependencies));
        }
Esempio n. 6
0
    void Update()
    {
        // 1.准备数据

        for (int i = 0; i < gameCount; i++)
        {
            tmpVelocitys[i] = new Vector3(0, 1, 0);
            //tmpPositions[i] = tmpPositions[i] + tmpVelocitys[i] * Time.deltaTime;
            tmpPositions[i]  = gameObjs[i].transform.position;
            tmpQuaternion[i] = gameObjs[i].transform.rotation;
        }
        VelocityJob job = new VelocityJob()
        {
            positions = tmpPositions,
            delaTime  = Time.deltaTime,
            velocitys = tmpVelocitys
        };
        RotateJob rotateJob = new RotateJob()
        {
            deltaTime   = Time.deltaTime,
            quaternions = tmpQuaternion
        };

        //依赖按照速度计算的到的位置数组
        ApplyTransform applyTransform = new ApplyTransform()
        {
            positions   = tmpPositions,
            quaternions = tmpQuaternion
        };

        // 2.执行
        //信号量 主线程如何知道子线程执行完毕    gameCount 指定总共子线程执行数据数量 10:每个子线程以下处理多少次
        JobHandle jobHandle = job.Schedule(gameCount, 10);                                // 移动 Job

        JobHandle rotateHandle = rotateJob.Schedule(gameCount, 10);                       // 旋转 Job

        JobHandle combineHandle = JobHandle.CombineDependencies(jobHandle, rotateHandle); // 赋值job 共同依赖联合的句柄~~

        JobHandle tranHandle = applyTransform.Schedule(tranAccessArray, combineHandle);   // 对 obj 赋值 job

        // 3.同步
        jobHandle.Complete();
        rotateHandle.Complete();
        tranHandle.Complete();

        //4。更新位置
        //for (int i = 0; i < gameCount; i++)
        //{
        //    gameObjs[i].transform.position = tmpPositions[i];
        //}
    }
Esempio n. 7
0
    private IEnumerator raiseMap(float duration)
    {
        float endTime = Time.time + duration;

        Color[] colors = tileDamageMap.GetPixels();
        // Ensure pixels array is destroyed to prevent memory leaks
        if (pixels != null && pixels.IsCreated)
        {
            pixels.Dispose();
        }
        pixels = new NativeArray <Color>(colors.Length, Allocator.Persistent);
        VelocityJob job;
        JobHandle   jobHandle;

        while (endTime > Time.time)
        {
            colors = tileDamageMap.GetPixels();
            pixels.CopyFrom(colors);

            // Initialize the job data
            job = new VelocityJob()
            {
                deltaTime = Time.deltaTime / duration,
                pixels    = pixels
            };

            jobHandle = job.Schedule(pixels.Length, 64);
            jobHandle.Complete();
            job.pixels.CopyTo(colors);

            tileDamageMap.SetPixels(colors);
            yield return(null);
        }
        colors = tileDamageMap.GetPixels();
        pixels.CopyFrom(colors);

        // Initialize the job data
        job = new VelocityJob()
        {
            deltaTime = 1,
            pixels    = pixels
        };

        jobHandle = job.Schedule(pixels.Length, 64);
        jobHandle.Complete();
        job.pixels.CopyTo(colors);

        tileDamageMap.SetPixels(colors);
        pixels.Dispose();
    }
    void Update1()
    {
        var job = new VelocityJob()
        {
            deltaTime = Time.deltaTime,
            position  = position1,
            velocity  = velocity1
        };

        JobHandle jobHandle = job.Schedule();

        jobHandle.Complete();

//         Debug.Log(job.position[0]);
    }
    public void Update()
    {
        // バッファ生成
        var position = new NativeArray <Vector3>(_count, Allocator.Persistent);
        var velocity = new NativeArray <Vector3>(_count, Allocator.Persistent);

        for (var i = 0; i < velocity.Length; i++)
        {
            // 入力バッファの中身を詰める
            velocity[i] = new Vector3(0, 10, 0);
        }

        if (useJob)
        {
            // ジョブ生成して、必要情報を渡す
            var job = new VelocityJob()
            {
                deltaTime = Time.deltaTime,
                position  = position,
                velocity  = velocity
            };

            // ジョブを実行
            JobHandle jobHandle = job.Schedule(_count, 0);

            // ジョブ完了の待機
            jobHandle.Complete();
        }
        else
        {
            for (int i = 0; i < _count; i++)
            {
                Utility.Execute(i, position, velocity, Time.deltaTime);
            }
        }

        for (int i = 0; i < _count; i++)
        {
            // 更新後のデータを取得してほげほげする
            var pos = position[i];
            // Do something...
        }


        // バッファの破棄
        position.Dispose();
        velocity.Dispose();
    }
Esempio n. 10
0
        public void Update()
        {
            // NativeArrayはNativeContainer属性が付加されているので
            // MainThreadとWorkerThreadでデータを安全に共有することができます。
            // また、使い終えたらDisposeする必要があります。
            var position = new NativeArray <Vector3>(100000, Allocator.Persistent);
            var velocity = new NativeArray <Vector3>(100000, Allocator.Persistent);

            for (var i = 0; i < velocity.Length; i++)
            {
                velocity[i] = new Vector3(0, 10, 0);
            }

            // Jobの初期化処理です。
            var job = new VelocityJob()
            {
                deltaTime = Time.deltaTime,
                position  = position,
                velocity  = velocity
            };

            // Jobをスケジューリングし、後でJobの完了を待つことができるJobHandleを返します。
            JobHandle jobHandle = job.Schedule();

            // メインスレッドで何か計算している最中にJobを動かしておきたい場合は以下のメソッドを呼ぶ
            JobHandle.ScheduleBatchedJobs();

            // ......
            // 何かMainThreadで行っておきたい処理
            // MainThreadで10[ms]かかる重い処理を想定
            // ......
            System.Threading.Thread.Sleep(10);

            // Jobが完了したことを確認します(完了してなければ完了まで待ちます)
            // Schedule実行後、すぐにCompleteを呼び出すことはお勧めできません。
            // 並列処理の恩恵を受けることがほぼできなくなるためです。
            // フレームの早い段階でJobをScheduleし、他の処理を行った後でCompleteを呼び出すのが最適です
            jobHandle.Complete();

            Debug.Log(job.position[0]);

            position.Dispose();
            velocity.Dispose();
        }
Esempio n. 11
0
        protected override JobHandle OnUpdate(JobHandle inputDeps)
        {
            translationMapData.Clear();
            velocityMapData.Clear();
            for (int i = 0; i < entities.Length; i++)
            {
                translationMapData.Add(entities[i], EntityManager.GetComponentData <Translation>(entities[i]));
                velocityMapData.Add(entities[i], EntityManager.GetComponentData <VelocityComponent>(entities[i]));
            }

            job = new VelocityJob
            {
                DeltaTime          = Time.DeltaTime,
                CollisionWorld     = buildPhysicsWorld.PhysicsWorld.CollisionWorld,
                CollisionFilter    = collisionFilter,
                TranslationMapData = translationMapData,
                VelocityMapData    = velocityMapData
            };

            return(job.Schedule(this, inputDeps));
        }
Esempio n. 12
0
        public void Update()
        {
            var position = new NativeArray <Vector3>(100000, Allocator.Persistent);

            var velocity = new NativeArray <Vector3>(100000, Allocator.Persistent);

            for (var i = 0; i < velocity.Length; i++)
            {
                velocity[i] = new Vector3(0, 10, 0);
            }

            var job = new VelocityJob()
            {
                deltaTime = Time.deltaTime,
                position  = position,
                velocity  = velocity
            };

            // 並列実行のJobをスケジュールします。
            // 最初のパラメータは、各反復が何回実行されるかです。
            // 2番目のパラメータは、内部でのループ分割数(バッチ数)です。
            JobHandle jobHandle = job.Schedule(position.Length, 128);

            // メインスレッドで何か計算している最中にJobを動かしておきたい場合は以下のメソッドを呼ぶ
            JobHandle.ScheduleBatchedJobs();

            // ......
            // 何かMainThreadで行っておきたい処理
            // MainThreadで10[ms]かかる重い処理を想定
            // ......
            System.Threading.Thread.Sleep(10);

            jobHandle.Complete();

            Debug.Log(job.position[0]);

            position.Dispose();
            velocity.Dispose();
        }