Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using Unity.Jobs;
- using Unity.Collections;
- using UnityEngine.Jobs;
- public class JobRender : MonoBehaviour
- {
- static public JobRender Instance;
- struct ComputeJob : IJobParallelFor
- {
- public void Execute(int i)
- {
- var step = Instance.m_VelocityArray * ((float)Instance.Watch.ElapsedMilliseconds/1000f);
- Instance.matrices.SetTRS(new Vector3(Instance.matrices[0, 3], Instance.matrices[1, 3],
- Instance.matrices[2, 3]) + step,
- Instance.matrices.rotation * Quaternion.Euler(step * 100), Vector3.one * Instance.scale);
- }
- }
- public Mesh mesh;
- public int computeSize = 1023, batchSize = 100;
- public float scale = 3;
- public Material mat;
- public System.Diagnostics.Stopwatch Watch = new System.Diagnostics.Stopwatch();
- JobHandle handleCalculate;
- Matrix4x4[] matrices;
- NativeArray<Vector3> m_VelocityArray;
- void OnEnable()
- {
- Instance = this;
- matrices = new Matrix4x4[computeSize];
- m_VelocityArray = new NativeArray<Vector3>(computeSize, Allocator.Persistent, NativeArrayOptions.None);
- for (var i = 0; i < m_VelocityArray.Length; i++)
- {
- m_VelocityArray = UnityEngine.Random.insideUnitSphere;
- matrices.SetTRS(UnityEngine.Random.insideUnitSphere * 100, Quaternion.identity, Vector3.one);
- }
- }
- void OnDisable()
- {
- handleCalculate.Complete();
- m_VelocityArray.Dispose();
- }
- void Update()
- {
- Instance = this;
- if (handleCalculate.IsCompleted)
- {
- Watch.Stop();
- Watch.Reset();
- Watch.Start();
- var jobA = new ComputeJob()
- {
- };
- handleCalculate = jobA.Schedule(computeSize, batchSize);
- }
- }
- private void LateUpdate()
- {
- Graphics.DrawMeshInstanced(mesh, 0, mat, matrices, 1023);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement