private void updatePhaserFilter(float percentageToEdge = -1)
        {
            if (percentageToEdge < 0)
            {
                percentageToEdge = percentToEdge();
            }

            if (hotspot.phaserLevel > 0.1f)
            {
                filterPhaser.setPhaserPercent(percentageToEdge);
            }
        }
Example #2
0
        private void Update()
        {
            if (userIsInsideTriggerRange && _audioSrc.spatialBlend > 0)
            {
                bool atLeast1DistBasedEffect = hotspot.distortion > 0.1f ||
                                               Mathf.Abs(hotspot.pitchBend) > 0.1f ||
                                               Mathf.Abs(hotspot.freqCutoff) > 0.1f ||
                                               Mathf.Abs(hotspot.phaserLevel) > 0.1f || true;

                if (Mathf.Abs(hotspot.pitchBend) < 0.1f)
                {
                    _audioSrc.pitch = 1f;
                }
                if (!atLeast1DistBasedEffect)
                {
                    return;
                }

                // TODO: CHECK IF SPATIAL BLEND is 2D
                float adjMax = soundMaxDist;
                float adjMin = soundMinDist * 2f;

                float distFromCam       = Vector3.Distance(transform.position, Camera.main.transform.position);
                float preClampedPercent = (distFromCam - adjMin) / (adjMax - adjMin);
                float percentageToEdge  = Mathf.Clamp(preClampedPercent, 0, 1);
                // Debug.Log ("percentageToEdge: " + percentageToEdge);

                // =============================
                // Optimise these checks so they don't occur on each Update()
                if (hotspot.distortion > 0.1f)
                {
                    filterDistortion.distortionLevel = Mathf.Pow(percentageToEdge, 0.5f) * hotspot.distortion;
                }
                if (Mathf.Abs(hotspot.pitchBend) > 0.1f)
                {
                    float pitchBendcoefficient = hotspot.pitchBend > 0
                                    ? Mathf.Lerp(0, highestPitch - 1.0f, hotspot.pitchBend)
                                    : Mathf.Lerp(0, lowestPitch - 1.0f, Mathf.Abs(hotspot.pitchBend));
                    _audioSrc.pitch = percentageToEdge * pitchBendcoefficient + 1.0f;
                }

                if (hotspot.phaserLevel > 0.1f)
                {
                    filterPhaser.setPhaserPercent(percentageToEdge);
                }

                // =============================
                // FREQUENCY CUTOFF
                // TODO: Should think about making this a logarithmic relationship
                if (hotspot.freqCutoff > 0.1f)
                {
                    // float coefficient = Mathf.Pow(percentageToEdge * 1.1f, 2f);
                    //   10 + ([0-1] x 1 * 4000)
                    // =
                    filterHighPass.cutoffFrequency = 80f + (percentageToEdge * hotspot.freqCutoff * HPFMultiplier);
                }
                else if (hotspot.freqCutoff < -0.1f)
                {
                    // Minus sign because the freqCutoff value will be negative
                    // Mathf.Log10(10f); == 1
                    // float coefficient = (1f - percentageToEdge);
                    // float coefficient = Mathf.Log10((1f - percentageToEdge) * 10f);
                    // float coefficient = (1f - Mathf.Pow(percentageToEdge, 3));
                    float coefficient = Mathf.Pow((1f - (percentageToEdge * 1.1f)), 3f);
                    filterLowPass.cutoffFrequency = 150f - (coefficient * hotspot.freqCutoff * LPFMultiplier); // 21990f
                }
            }
        }