Aufgrund einer Wartung wird GitLab am 28.09. zwischen 10:00 und 11:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 28.09. between 10:00 and 11:00 am.

VAUListener.cs 3.63 KB
Newer Older
1
using UnityEngine;
2
using System.Linq;
3 4 5 6 7 8
using System.Collections;
using VA;


public class VAUListener : MonoBehaviour {

9
    public string listenername = "Listener";
10 11
    public int maxreverbZones = 2;
    public string VAAudioRenderer = "MyBinauralArtificialReverb";
12 13
    private int Listener;
    private VANet _VA = null;
14 15
    private Quaternion q;
    private Transform t;
16
    private AudioReverbZone[] reverbZones;
17 18
    private float currentArtificialReverberationTime = 0f;
    private float shadowReverbTime;
19
    
20 21 22 23 24 25 26 27
    public int ID
    {
        get
        {
            return Listener;
        }
    }

28 29 30
    // Use this for initialization
    void Start()
    {
31
        _VA = VAUAdapter.VA;
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
32
        Listener = _VA.CreateListener(listenername);
33
        listenername += " " + Listener;
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
34
        _VA.SetListenerAuralizationMode(Listener, "all");
Maurice Tayeb Andreas's avatar
[VAU]  
Maurice Tayeb Andreas committed
35
        Debug.Log("[VAU]" + listenername + " created");
36
        _VA.SetListenerHRIRDataset(Listener, VAUAdapter.DefaultHRIR);
37
        SetListenerPositionOrientation();
38 39
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, 2.0f);
        reverbZones = FindObjectsOfType(typeof(AudioReverbZone)) as AudioReverbZone[];
40 41 42 43
    }

    void OnEnable()
    {
44 45
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnListenerAuralizationModeChanged;
46 47 48 49
    }
	
	// Update is called once per frame
	void Update ()
50 51
    {
        SetListenerPositionOrientation();
52
        SetActiveReverbZones();
53
    }
54

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
55
    //uses the View- and Up-Vector to transmit the position of the listener to VA
56
    void SetListenerPositionOrientation()
57
    {
58 59
        t = GetComponent<Transform>();
        q = t.rotation;
60 61
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
62 63
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
64
        _VA.SetListenerPosition(Listener, t.transform.position.x, t.transform.position.y, -t.transform.position.z);
65
        _VA.SetListenerOrientationVU(Listener, view_ogl.x, view_ogl.y, view_ogl.z, up_ogl.x, up_ogl.y, up_ogl.z);
66
    }
67

68 69 70 71 72
    void SetActiveReverbZones()
    {
        if (reverbZones == null)
            return;

73
        float actReverbTime = 0f;
74 75 76
        int i = 0;
        foreach (AudioReverbZone reverbZone in reverbZones)
        {
77 78 79 80
            if (i >= maxreverbZones)
                break;
            float actDistance = Vector3.Distance(reverbZone.transform.position, gameObject.transform.position);
            if ((actDistance < reverbZone.maxDistance))
81
            {
82
                if (actDistance > reverbZone.minDistance)
83
                {
84
                    actReverbTime += reverbZone.decayTime * (actDistance - reverbZone.minDistance) / (reverbZone.maxDistance - reverbZone.minDistance);
85
                }
86
                else
87
                {
88
                    actReverbTime += reverbZone.decayTime;
89
                }
90
                i++;
91 92
            }
        }
93 94 95 96
        if (shadowReverbTime == actReverbTime)
            return;

        shadowReverbTime = actReverbTime;
97

98
        if (i > 0)
99
            actReverbTime /= i;
100 101
        
        _VA.SetArtificalReverbarationTime(VAAudioRenderer, actReverbTime);
102 103 104

    }

105 106 107 108
    void OnListenerAuralizationModeChanged(string AuraMode)
    {
        _VA.SetListenerAuralizationMode(Listener, AuraMode);
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
109

110 111 112 113 114
    void OnDisable()
    {
        if (GetComponent<VAUAuralizationMode>() != null)
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnListenerAuralizationModeChanged;
    }
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
115

Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
116 117 118 119 120 121 122
    void OnDestroy()
    {
        if (_VA.IsConnected())
        {
            _VA.DeleteListener(Listener);
        }
    }
123
}