VAUSoundReceiver.cs 2.26 KB
Newer Older
1
using UnityEngine;
2
using System.Linq;
3 4 5 6
using System.Collections;
using VA;


7
public class VAUSoundReceiver : MonoBehaviour {
8

9
    [Tooltip("Descriptive name")]
10
    public string Name = "SoundReceiver";
11
    
Dipl.-Ing. Jonas Stienen's avatar
Dipl.-Ing. Jonas Stienen committed
12
    [Tooltip("Set an explicit renderer for this receiver [todo]")]
13
    public VAUAudioRenderer Renderer = null;
14

15 16
    [Tooltip("Connect an directivity to this receiver")]
    public VAUDirectivity Directivity = null;
17

18 19
	[Tooltip("Internal VA identifier")]
    protected int _ID;
20

21 22
    private Quaternion q;
    private Transform t;
23
    
24 25 26 27
    public int ID
    {
        get
        {
28
            return _ID;
29 30
        }
    }
31
    
32 33
    void Start()
    {
34 35 36 37
		_ID = VAUAdapter.VA.CreateSoundReceiver(Name);
        VAUAdapter.VA.SetSoundReceiverAuralizationMode(_ID, "all");
		if (Directivity)
			VAUAdapter.VA.SetSoundReceiverDirectivity(_ID, Directivity.ID);
38
        
39
        SetSoundReceiverPositionOrientation();
40 41 42 43
    }

    void OnEnable()
    {
44
        if (GetComponent<VAUAuralizationMode>() != null)
45
            GetComponent<VAUAuralizationMode>().AuraStringChanged += OnSoundReceiverAuralizationModeChanged;
46
    }
47

48
    void Update()
49
    {
50
        SetSoundReceiverPositionOrientation();
51
    }
52

53
    // Uses the View- and Up-Vector to transmit the position of the listener to VA
54
    protected void SetSoundReceiverPositionOrientation()
55
    {
56 57
        t = GetComponent<Transform>();
        q = t.rotation;
58 59
        Vector3 up = q * Vector3.up;
        Vector3 view = q * Vector3.forward;
60 61
        Vector3 view_ogl = new Vector3(view.x, view.y, -view.z);
        Vector3 up_ogl = new Vector3(up.x, up.y, -up.z);
62 63
        VAUAdapter.VA.SetSoundReceiverPosition(_ID, new VAVec3(t.transform.position.x, t.transform.position.y, -t.transform.position.z));
        VAUAdapter.VA.SetSoundReceiverOrientationVU(_ID, new VAVec3(view_ogl.x, view_ogl.y, view_ogl.z), new VAVec3(up_ogl.x, up_ogl.y, up_ogl.z));
64
    }
65

66
    protected void OnSoundReceiverAuralizationModeChanged(string AuraMode)
67
    {
68
        VAUAdapter.VA.SetSoundReceiverAuralizationMode(_ID, AuraMode);
69
    }
70

71
    private void OnDestroy()
72 73
    {
        if (GetComponent<VAUAuralizationMode>() != null)
74
            GetComponent<VAUAuralizationMode>().AuraStringChanged -= OnSoundReceiverAuralizationModeChanged;
75 76
		
		VAUAdapter.VA.DeleteSoundReceiver(ID);
Maurice Tayeb Andreas's avatar
Maurice Tayeb Andreas committed
77
    }
78
}