Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SpectrumSystem : JobComponentSystem
- {
- Settings settings;
- Channel[] channels;
- public int Init(Settings inSettings)
- {
- settings = inSettings;
- int spectrumSize = settings.spectrumSize;
- channels = new Channel[1];
- channels[0] = new Channel();
- channels[0].Init(0, spectrumSize);
- int result = channels.Length;
- return result;
- }
- struct SpectrumGroup
- {
- public int Length;
- public ComponentDataArray<Position> positions;
- public ComponentDataArray<Origin> origins;
- public ComponentDataArray<Scale> scales;
- }
- [Inject]
- SpectrumGroup group;
- [ComputeJobOptimization]
- public struct SpectrumJob : IJobParallelFor
- {
- public float maxScale;
- public float dynamics;
- public float epsilon;
- public NativeArray<float> currentSpectrums;
- public NativeArray<float> prevSpectrums;
- public ComponentDataArray<Position> positions;
- public ComponentDataArray<Origin> origins;
- public ComponentDataArray<Scale> scales;
- public void Execute(int index)
- {
- float current = currentSpectrums[index];
- float prev = prevSpectrums[index];
- float val = (dynamics*prev + (1 - dynamics)*current);
- prevSpectrums[index] = val;
- float valAdjusted = val*maxScale;
- float halfHeight = valAdjusted*0.5f;
- Position position = positions[index];
- Origin origin = origins[index];
- Scale scale = scales[index];
- if(val >= epsilon)
- {
- position.Value = new float3(origin.Value.x, halfHeight, origin.Value.z);
- scale.Value = new float3(1, valAdjusted, 1);
- }
- else
- {
- position.Value = new float3(0, 0, 0);
- scale.Value = new float3(0, 0, 0);
- }
- scales[index] = scale;
- positions[index] = position;
- }
- }
- protected override JobHandle OnUpdate(JobHandle inputDeps)
- {
- JobHandle jobHandle = CreateJob(true, channel, inputDeps);
- return jobHandle;
- }
- JobHandle CreateJob(bool left, Channel channel, JobHandle inputDeps)
- {
- AudioListener.GetSpectrumData(channel.spectrumBuff,
- channel.channelNumber,
- FFTWindow.BlackmanHarris);
- channel.currentSpectrums.CopyFrom(channel.spectrumBuff);
- SpectrumJob spectrumJob = new SpectrumJob()
- {
- maxScale = settings.maxScale,
- dynamics = settings.dynamics,
- epsilon = settings.epsilon,
- currentSpectrums = channel.currentSpectrums,
- prevSpectrums = channel.prevSpectrums,
- positions = group.positions,
- origins = group.origins,
- scales = group.Length,
- };
- JobHandle handle = spectrumJob.Schedule(group.Length, 1, inputDeps);
- return handle;
- }
- }
Add Comment
Please, Sign In to add comment