Пример #1
0
    // Update is called once per frame
    void Update()
    {
        //reset counting variables
        totalEnergy1 = 0;
        totalEnergy2 = 0;
        totalEnergy3 = 0;

        totalRotationalMomentum1 = 0;
        totalRotationalMomentum2 = 0;
        totalRotationalMomentum3 = 0;

        netVelocity1 = Vector3.zero;
        netVelocity2 = Vector3.zero;
        netVelocity3 = Vector3.zero;

        foreach (Transform child in transform)
        {
            if (Vector3.SqrMagnitude(child.position - basinAxis1.position) <= maxCalcDistanceSqr)                                                        //if it's in the first basin
            {
                Rigidbody body = child.GetComponent <Rigidbody>();                                                                                       //get rigidbody component
                if (body != null)                                                                                                                        //verify it's not null
                {
                    totalEnergy1             += Vector3.SqrMagnitude(body.velocity) * body.mass;                                                         //calculate total particle energy where E = m * |v|^2
                    totalRotationalMomentum1 += Vector3.Dot(Vector3.Cross(child.position - basinAxis1.position, body.mass * body.velocity), Vector3.up); //calculate total angular momentum where L = R x mV
                    netVelocity1             += body.velocity;                                                                                           //calculate net velocity in x,y,z
                }
            }
            else if (Vector3.SqrMagnitude(child.position - basinAxis2.position) <= maxCalcDistanceSqr)                                                   //if it's in the second basin
            {
                Rigidbody body = child.GetComponent <Rigidbody>();                                                                                       //get rigidbody component
                if (body != null)                                                                                                                        //verify it's not null
                {
                    totalEnergy2             += Vector3.SqrMagnitude(body.velocity) * body.mass;                                                         //calculate total particle energy where E = m * |v|^2
                    totalRotationalMomentum2 += Vector3.Dot(Vector3.Cross(child.position - basinAxis2.position, body.mass * body.velocity), Vector3.up); //calculate total angular momentum where L = R x mV
                    netVelocity2             += body.velocity;                                                                                           //calculate net velocity in x,y,z
                }
            }
            else if (Vector3.SqrMagnitude(child.position - basinAxis3.position) <= maxCalcDistanceSqr)                                                   //if it's in the third basin
            {
                Rigidbody body = child.GetComponent <Rigidbody>();                                                                                       //get rigidbody component
                if (body != null)                                                                                                                        //verify it's not null
                {
                    totalEnergy3             += Vector3.SqrMagnitude(body.velocity) * body.mass;                                                         //calculate total particle energy where E = m * |v|^2
                    totalRotationalMomentum3 += Vector3.Dot(Vector3.Cross(child.position - basinAxis3.position, body.mass * body.velocity), Vector3.up); //calculate total angular momentum where L = R x mV
                    netVelocity3             += body.velocity;                                                                                           //calculate net velocity in x,y,z
                }
            }
        }

        //update parameters on synthesizers based on inputs
        Hv_simple_sin_synth_AudioLib sinSynth = basinAxis1.GetComponent <Hv_simple_sin_synth_AudioLib>();

        sinSynth.SetFloatParameter(Hv_simple_sin_synth_AudioLib.Parameter.Gain, Mathf.Lerp(gainMin, gainMax, totalEnergy1 / energyMax));
        sinSynth.SetFloatParameter(Hv_simple_sin_synth_AudioLib.Parameter.Oscfreq, Mathf.Lerp(oscMin, oscMax, Mathf.Abs(totalRotationalMomentum1) / momentumMax));
        sinSynth.SetFloatParameter(Hv_simple_sin_synth_AudioLib.Parameter.Freqcutoff, Mathf.Lerp(frequencyMin, frequencyMax, Mathf.Abs(netVelocity1.x) / netVelocityMax));
        sinSynth.SetFloatParameter(Hv_simple_sin_synth_AudioLib.Parameter.Ringmodfreq, Mathf.Lerp(ringFreqMin, ringFreqMax, Mathf.Abs(netVelocity1.y) / netVelocityMax));
        sinSynth.SetFloatParameter(Hv_simple_sin_synth_AudioLib.Parameter.Ringmodmultiplier, Mathf.Lerp(ringMultMin, ringMultMax, Mathf.Abs(netVelocity1.z) / netVelocityMax));

        Hv_simple_saw_synth_AudioLib sawSynth = basinAxis2.GetComponent <Hv_simple_saw_synth_AudioLib>();

        sawSynth.SetFloatParameter(Hv_simple_saw_synth_AudioLib.Parameter.Gain, Mathf.Lerp(gainMin, gainMax, totalEnergy2 / energyMax));
        sawSynth.SetFloatParameter(Hv_simple_saw_synth_AudioLib.Parameter.Oscfreq, Mathf.Lerp(oscMin, oscMax, Mathf.Abs(totalRotationalMomentum2) / momentumMax));
        sawSynth.SetFloatParameter(Hv_simple_saw_synth_AudioLib.Parameter.Freqcutoff, Mathf.Lerp(frequencyMin, frequencyMax, Mathf.Abs(netVelocity2.x) / netVelocityMax));
        sawSynth.SetFloatParameter(Hv_simple_saw_synth_AudioLib.Parameter.Ringmodfreq, Mathf.Lerp(ringFreqMin, ringFreqMax, Mathf.Abs(netVelocity2.y) / netVelocityMax));
        sawSynth.SetFloatParameter(Hv_simple_saw_synth_AudioLib.Parameter.Ringmodmultiplier, Mathf.Lerp(ringMultMin, ringMultMax, Mathf.Abs(netVelocity2.z) / netVelocityMax));

        Hv_simple_square_synth_AudioLib squareSynth = basinAxis3.GetComponent <Hv_simple_square_synth_AudioLib>();

        squareSynth.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Gain, Mathf.Lerp(gainMin, gainMax, totalEnergy3 / energyMax));
        squareSynth.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Oscfreq, Mathf.Lerp(oscMin, oscMax, Mathf.Abs(totalRotationalMomentum3) / momentumMax));
        squareSynth.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Freqcutoff, Mathf.Lerp(frequencyMin, frequencyMax, Mathf.Abs(netVelocity3.x) / netVelocityMax));
        squareSynth.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Ringmodfreq, Mathf.Lerp(ringFreqMin, ringFreqMax, Mathf.Abs(netVelocity3.y) / netVelocityMax));
        squareSynth.SetFloatParameter(Hv_simple_square_synth_AudioLib.Parameter.Ringmodmultiplier, Mathf.Lerp(ringMultMin, ringMultMax, Mathf.Abs(netVelocity3.z) / netVelocityMax));
    }
 private void OnEnable()
 {
     _dsp = target as Hv_simple_square_synth_AudioLib;
 }