Ryle Radio 1.0.0
An open-source "radio" system for Unity, allowing tracks, tuning, broadcasters, and more!
Loading...
Searching...
No Matches
RadioBroadcaster.cs
1using NaughtyAttributes;
4using UnityEngine;
5
7{
8
9 /// <summary>
10 /// A "broadcaster" for a \ref RadioTrackWrapper - the closer the \ref RadioOutput that's playing the track is to a broadcaster (taking into consideration broadcast radius, power, etc), the louder the track is in the Output.
11 ///
12 /// Somewhat similar to real world broadcasters- at least, that was the original intention :)
13 ///
14 /// This has a custom editor in \ref RadioBroadcasterEditor
15 /// </summary>
16 [AddComponentMenu("Ryle Radio/Radio Broadcaster")]
18 {
19 /// <summary>
20 /// The inner and outer radii of this broadcaster.
21 ///
22 /// <i>radiusProg is the x-value in the \ref distanceFalloff that we use to evaluate broadcast power</i>
23 /// If the Output is within the inner radius, the radiusProg is 1.
24 /// If the Output is between the inner and outer radii, the radiusProg is between 0 and 1.
25 /// If the Output is outside both radii, the radiusProg is 0.
26 /// </summary>
27 [Space(8)]
28 public Vector2 broadcastRadius;
29
30 /// <summary>
31 /// The power of this broadcaster at the maximum and minimum radii.
32 ///
33 /// The x-value is the broadcast power right on the outer radius, and the y-value is the broadcast power within the inner radius.
34 /// Broadcast power between the inner and outer radii will be somewhere in this range, defined by the \ref distanceFalloff
35 ///
36 /// If the x-value is greater than 0, you might want to enable \ref applyToAllOutputsOutside so that all Outputs are affected by this broadcaster.
37 /// </summary>
38 [Space(8), MinMaxSlider(0, 1), SerializeField]
39 protected Vector2 broadcastPowers = new(0, 1);
40
41 /// <summary>
42 /// The falloff curve between the inner and outer broadcast radii- the x-value is how far between the radii the Output is, and the y-value is how far between \ref broadcastPowers x and y the track's broadcast power is
43 /// </summary>
44 [SerializeField, CurveRange(0, 0, 1, 1)]
45 protected AnimationCurve distanceFalloff = new(new Keyframe[2] {
46 new(0, 1, 0, 0),
47 new(1, 0, 0, 0)
48 });
49
50 /// <summary>
51 /// If the x-value of \ref broadcastPowers should be applied to all Outputs outside of this broadcaster's radii- effectively makes it global
52 /// </summary>
53 [SerializeField, AllowNesting, ShowIf("ShowApplyToAllOutputs")]
54 private bool applyToAllOutputsOutside = false;
55
56 /// <summary>
57 /// The position of the broadcaster in the previous frame- we can't access `transform.position` in the audio thread, so we need to cache it in update
58 /// </summary>
59 private Vector3 cachedPos;
60
61 /// <summary>
62 /// Shows \ref applyToAllOutputsOutside when the x-value of \ref broadcastPowers is greater than 0
63 /// </summary>
64 private bool ShowApplyToAllOutputs => broadcastPowers.x > 0;
65
66
67 /// <summary>
68 /// Updates the position of this broadcaster
69 /// </summary>
70 private void Update()
71 {
72 // cache the position
73 cachedPos = transform.position;
74 }
75
76 /// <summary>
77 /// Links this broadcaster to a track
78 /// </summary>
79 /// <param name="_track">The track to link to</param>
80 protected override void AssignToTrack(RadioTrackWrapper _track)
81 {
82 _track.broadcasters.Add(this);
83 _track.OnAddBroadcaster(this, _track);
84 }
85
86 /// <summary>
87 /// Unlinks this broadcaster from a track
88 /// </summary>
89 /// <param name="_track">The track to unlink from</param>
90 protected override void RemoveFromTrack(RadioTrackWrapper _track)
91 {
92 _track.broadcasters.Remove(this);
93 _track.OnRemoveBroadcaster(this, _track);
94 }
95
96 /// <summary>
97 /// Gets the broadcast power of this particular broadcaster using the Output's position
98 /// </summary>
99 /// <remarks></remarks>
100 /// <param name="_receiverPos">The position of the Output</param>
101 /// <returns>A value between 0 and 1, used as a multiplier to the loudness of a track</returns>
102 public float GetPower(Vector3 _receiverPos)
103 {
104 // the distance between the output and this broadcaster
105 float distance = Vector3.Distance(cachedPos, _receiverPos);
106
107 // how far between the inner and outer radii the distance is
108 float radiusProg = Mathf.Clamp01(Mathf.InverseLerp(broadcastRadius.x, broadcastRadius.y, distance));
109
110 // get the adjusted power according to the distance falloff
111 float adjustedT = distanceFalloff.Evaluate(radiusProg);
112
113 // if the output is outside of the range of this broadcaster and it's not global when outside, return a power of 0
114 if (adjustedT <= 0 && (ShowApplyToAllOutputs && !applyToAllOutputsOutside))
115 return 0;
116
117 // return the actual broadcast power according to values supplied in the broadcastPowers variable
118 return Mathf.Lerp(broadcastPowers.x, broadcastPowers.y, adjustedT);
119 }
120 }
121
122}
An extension of RadioComponent that accesses specific tracks on the stored RadioData.
A "broadcaster" for a RadioTrackWrapper - the closer the RadioOutput that's playing the track is to a...
Vector3 cachedPos
The position of the broadcaster in the previous frame- we can't access transform.position in the audi...
bool ShowApplyToAllOutputs
Shows applyToAllOutputsOutside when the x-value of broadcastPowers is greater than 0.
float GetPower(Vector3 _receiverPos)
Gets the broadcast power of this particular broadcaster using the Output's position.
Vector2 broadcastRadius
The inner and outer radii of this broadcaster.
void Update()
Updates the position of this broadcaster.
bool applyToAllOutputsOutside
If the x-value of broadcastPowers should be applied to all Outputs outside of this broadcaster's radi...
Vector2 broadcastPowers
The power of this broadcaster at the maximum and minimum radii.
override void RemoveFromTrack(RadioTrackWrapper _track)
Unlinks this broadcaster from a track.
AnimationCurve distanceFalloff
The falloff curve between the inner and outer broadcast radii- the x-value is how far between the rad...
override void AssignToTrack(RadioTrackWrapper _track)
Links this broadcaster to a track.
A wrapper class for RadioTrack so that track types can be switched between in the inspector!...
Action< RadioBroadcaster, RadioTrackWrapper > OnAddBroadcaster
An event called when a broadcaster is added to the track.
Action< RadioBroadcaster, RadioTrackWrapper > OnRemoveBroadcaster
An event called when a broadcaster is removed from this track.
List< RadioBroadcaster > broadcasters
The broadcasters in the scene that have this track selected.
Base interfaces and classes for components, e.g: track accessors, output accessors.
Components to be placed on scene objects, e.g: Outputs, Broadcasters, Observers.
Tracks to be used on a radio- includes base classes.
Definition RadioUtils.cs:20