Guest User

Untitled

a guest
Jul 23rd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.00 KB | None | 0 0
  1. class SpectrumSystem : JobComponentSystem
  2. {
  3. Settings settings;
  4.  
  5. Channel[] channels;
  6.  
  7. public int Init(Settings inSettings)
  8. {
  9. settings = inSettings;
  10.  
  11. int spectrumSize = settings.spectrumSize;
  12. channels = new Channel[1];
  13. channels[0] = new Channel();
  14. channels[0].Init(0, spectrumSize);
  15.  
  16. int result = channels.Length;
  17. return result;
  18. }
  19.  
  20. struct SpectrumGroup
  21. {
  22. public int Length;
  23. public ComponentDataArray<Position> positions;
  24. public ComponentDataArray<Origin> origins;
  25. public ComponentDataArray<Scale> scales;
  26. }
  27.  
  28. [Inject]
  29. SpectrumGroup group;
  30.  
  31. [ComputeJobOptimization]
  32. public struct SpectrumJob : IJobParallelFor
  33. {
  34. public float maxScale;
  35. public float dynamics;
  36. public float epsilon;
  37.  
  38. public NativeArray<float> currentSpectrums;
  39. public NativeArray<float> prevSpectrums;
  40.  
  41. public ComponentDataArray<Position> positions;
  42. public ComponentDataArray<Origin> origins;
  43. public ComponentDataArray<Scale> scales;
  44.  
  45. public void Execute(int index)
  46. {
  47. float current = currentSpectrums[index];
  48. float prev = prevSpectrums[index];
  49.  
  50. float val = (dynamics*prev + (1 - dynamics)*current);
  51. prevSpectrums[index] = val;
  52.  
  53. float valAdjusted = val*maxScale;
  54. float halfHeight = valAdjusted*0.5f;
  55.  
  56. Position position = positions[index];
  57. Origin origin = origins[index];
  58. Scale scale = scales[index];
  59.  
  60. if(val >= epsilon)
  61. {
  62. position.Value = new float3(origin.Value.x, halfHeight, origin.Value.z);
  63. scale.Value = new float3(1, valAdjusted, 1);
  64. }
  65. else
  66. {
  67. position.Value = new float3(0, 0, 0);
  68. scale.Value = new float3(0, 0, 0);
  69. }
  70.  
  71. scales[index] = scale;
  72. positions[index] = position;
  73. }
  74. }
  75.  
  76. protected override JobHandle OnUpdate(JobHandle inputDeps)
  77. {
  78. JobHandle jobHandle = CreateJob(true, channel, inputDeps);
  79. return jobHandle;
  80. }
  81.  
  82. JobHandle CreateJob(bool left, Channel channel, JobHandle inputDeps)
  83. {
  84. AudioListener.GetSpectrumData(channel.spectrumBuff,
  85. channel.channelNumber,
  86. FFTWindow.BlackmanHarris);
  87. channel.currentSpectrums.CopyFrom(channel.spectrumBuff);
  88.  
  89. SpectrumJob spectrumJob = new SpectrumJob()
  90. {
  91. maxScale = settings.maxScale,
  92. dynamics = settings.dynamics,
  93. epsilon = settings.epsilon,
  94. currentSpectrums = channel.currentSpectrums,
  95. prevSpectrums = channel.prevSpectrums,
  96.  
  97. positions = group.positions,
  98. origins = group.origins,
  99. scales = group.Length,
  100. };
  101.  
  102. JobHandle handle = spectrumJob.Schedule(group.Length, 1, inputDeps);
  103. return handle;
  104. }
  105. }
Add Comment
Please, Sign In to add comment