Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.00 KB | None | 0 0
  1. using UnityEngine;
  2. using Unity.Jobs;
  3. using Unity.Collections;
  4. using UnityEngine.Jobs;
  5.  
  6. public class JobRender : MonoBehaviour
  7. {
  8.     static public JobRender Instance;
  9.  
  10.     struct ComputeJob : IJobParallelFor
  11.     {
  12.         public void Execute(int i)
  13.         {
  14.             var step = Instance.m_VelocityArray * ((float)Instance.Watch.ElapsedMilliseconds/1000f);
  15.  
  16.             Instance.matrices.SetTRS(new Vector3(Instance.matrices[0, 3], Instance.matrices[1, 3],
  17.                 Instance.matrices[2, 3]) + step,
  18.                 Instance.matrices.rotation * Quaternion.Euler(step * 100), Vector3.one * Instance.scale);
  19.         }
  20.     }
  21.  
  22.     public Mesh mesh;
  23.     public int computeSize = 1023, batchSize = 100;
  24.     public float scale = 3;
  25.     public Material mat;
  26.     public System.Diagnostics.Stopwatch Watch = new System.Diagnostics.Stopwatch();
  27.  
  28.     JobHandle handleCalculate;
  29.  
  30.     Matrix4x4[] matrices;
  31.     NativeArray<Vector3> m_VelocityArray;
  32.  
  33.     void OnEnable()
  34.     {
  35.         Instance = this;
  36.         matrices = new Matrix4x4[computeSize];
  37.  
  38.         m_VelocityArray = new NativeArray<Vector3>(computeSize, Allocator.Persistent, NativeArrayOptions.None);
  39.         for (var i = 0; i < m_VelocityArray.Length; i++)
  40.         {
  41.             m_VelocityArray = UnityEngine.Random.insideUnitSphere;
  42.             matrices.SetTRS(UnityEngine.Random.insideUnitSphere * 100, Quaternion.identity, Vector3.one);
  43.         }
  44.     }
  45.  
  46.     void OnDisable()
  47.     {
  48.         handleCalculate.Complete();
  49.         m_VelocityArray.Dispose();
  50.     }
  51.  
  52.     void Update()
  53.     {
  54.         Instance = this;
  55.  
  56.         if (handleCalculate.IsCompleted)
  57.         {
  58.             Watch.Stop();
  59.             Watch.Reset();
  60.             Watch.Start();
  61.  
  62.             var jobA = new ComputeJob()
  63.             {
  64.             };
  65.  
  66.             handleCalculate = jobA.Schedule(computeSize, batchSize);
  67.         }
  68.     }
  69.  
  70.     private void LateUpdate()
  71.     {
  72.         Graphics.DrawMeshInstanced(mesh, 0, mat, matrices, 1023);
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement