Skip to content

Commit

Permalink
3D audio can now be toggled
Browse files Browse the repository at this point in the history
update dependencies
code cleanup
  • Loading branch information
Thiago Zimmermann committed May 2, 2020
1 parent 491e0a7 commit 57a31ad
Show file tree
Hide file tree
Showing 29 changed files with 1,770 additions and 677 deletions.
19 changes: 15 additions & 4 deletions src/Hypnonema.Client/ClientScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ private CallbackDelegate OnCreateScreen(IDictionary<string, object> args, Callba
args.FirstOrDefault(arg => arg.Key == "soundMaxDistance").Value?.ToString(),
out var soundMaxDistance)) soundMaxDistance = 100.0f;

if (!bool.TryParse(
args.FirstOrDefault(arg => arg.Key == "is3DAudioEnabled").Value?.ToString(),
out var is3DAudioEnabled)) is3DAudioEnabled = false;

var screen = new Screen
{
AlwaysOn = alwaysOn,
Expand All @@ -302,7 +306,8 @@ private CallbackDelegate OnCreateScreen(IDictionary<string, object> args, Callba
GlobalVolume = globalVolume,
SoundMaxDistance = soundMaxDistance,
SoundMinDistance = soundMinDistance,
SoundAttenuation = soundAttenuation
SoundAttenuation = soundAttenuation,
Is3DAudioEnabled = is3DAudioEnabled,
},
PositionalSettings = is3DRendered
? new PositionalSettings
Expand Down Expand Up @@ -461,6 +466,11 @@ private CallbackDelegate OnEditScreen(IDictionary<string, object> args, Callback
return callback;
}

if (!bool.TryParse(args.FirstOrDefault(arg => arg.Key == "is3DAudioEnabled").Value?.ToString(), out var is3DAudioEnabled))
{
is3DAudioEnabled = false;
}

var screen = new Screen
{
Id = id,
Expand All @@ -473,7 +483,8 @@ private CallbackDelegate OnEditScreen(IDictionary<string, object> args, Callback
GlobalVolume = globalVolume,
SoundMaxDistance = soundMaxDistance,
SoundAttenuation = soundAttenuation,
SoundMinDistance = soundMinDistance
SoundMinDistance = soundMinDistance,
Is3DAudioEnabled = is3DAudioEnabled,
},
TargetSettings =
is3DRendered
Expand Down Expand Up @@ -579,7 +590,7 @@ private async Task<CallbackDelegate> OnRequestState(IDictionary<string, object>

foreach (var player in this.playerPool.VideoPlayers)
{
player.Browser.SendGetState();
player.Browser.GetState();
var state = await BrowserStateHelperScript.GetLastState();
if (state == null)
{
Expand Down Expand Up @@ -671,7 +682,7 @@ private async Task OnStateTick()
{
foreach (var player in videoPlayers)
{
player.Browser.SendGetState();
player.Browser.GetState();
var duiState = await BrowserStateHelperScript.GetLastState();
if (duiState == null)
{
Expand Down
44 changes: 24 additions & 20 deletions src/Hypnonema.Client/Graphics/DuiBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,47 +65,52 @@ public void Dispose()
GC.SuppressFinalize(this);
}

public void SendGetState()
public void GetState()
{
this.SendMessage(new { type = "getState" });
}

public void SendInit(string screenName, string posterUrl)
public void Init(string screenName, string posterUrl)
{
this.SendMessage(new { type = "init", screenName, posterUrl });
}

public void SendMessage(object obj)
{
API.SendDuiMessage(this.NativeValue, JsonConvert.SerializeObject(obj));
}

public void SendPause()
public void Pause()
{
this.SendMessage(new { type = "pause" });
}

public void SendPlay(string url)
public void Play(string url)
{
this.SendMessage(new { type = "play", src = new { url } });
}

public void SendResume()
public void Resume()
{
this.SendMessage(new { type = "resume" });
}

public void SendSeek(float time)
public void Seek(float time)
{
this.SendMessage(new { type = "seek", time });
}

public void SendStop()
public void SendMessage(object obj)
{
API.SendDuiMessage(this.NativeValue, JsonConvert.SerializeObject(obj));
}

public void SetVolume(float volume)
{
this.SendMessage(new { type = "volume", volume = volume / 100 });
}

public void Stop()
{
this.SendMessage(new { type = "stop" });
}

public void SendTick(AudioTickData tickData)
public void Tick(AudioTickData tickData)
{
this.SendMessage(
new
Expand Down Expand Up @@ -136,20 +141,19 @@ public void SendTick(AudioTickData tickData)
});
}

public void SendToggleReplay(bool toggle)
public void Toggle3DAudio(bool value)
{
this.SendMessage(new { type = "toggleReplay", value = toggle });
this.SendMessage(new { type = "toggle3DAudio", enabled = value });
}

public void SendUpdate(bool paused, float currentTime, string currentSource)
public void ToggleReplay(bool toggle)
{
this.SendMessage(
new { type = "update", paused, currentTime, src = currentSource });
this.SendMessage(new { type = "toggleReplay", value = toggle });
}

public void SendVolume(float volume)
public void Update(bool paused, float currentTime, string currentSource)
{
this.SendMessage(new { type = "volume", volume = volume / 100 });
this.SendMessage(new { type = "update", paused, currentTime, src = currentSource });
}
}
}
6 changes: 3 additions & 3 deletions src/Hypnonema.Client/Hypnonema.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="CitizenFX.Core.Client, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\..\packages\CitizenFX.Core.Client.1.0.2328\lib\net45\CitizenFX.Core.Client.dll</HintPath>
<HintPath>..\..\packages\CitizenFX.Core.Client.1.0.2432\lib\net45\CitizenFX.Core.Client.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
Expand Down Expand Up @@ -76,11 +76,11 @@
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\..\packages\Nerdbank.GitVersioning.3.1.74\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\..\packages\Nerdbank.GitVersioning.3.1.74\build\Nerdbank.GitVersioning.targets')" />
<Import Project="..\..\packages\Nerdbank.GitVersioning.3.1.91\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\..\packages\Nerdbank.GitVersioning.3.1.91\build\Nerdbank.GitVersioning.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}".</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\..\packages\Nerdbank.GitVersioning.3.1.74\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Nerdbank.GitVersioning.3.1.74\build\Nerdbank.GitVersioning.targets'))" />
<Error Condition="!Exists('..\..\packages\Nerdbank.GitVersioning.3.1.91\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\..\packages\Nerdbank.GitVersioning.3.1.91\build\Nerdbank.GitVersioning.targets'))" />
</Target>
</Project>
2 changes: 2 additions & 0 deletions src/Hypnonema.Client/Players/IVideoPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public interface IVideoPlayer : IDisposable

float SoundMinDistance { get; set; }

void CalculateVolume();

void Draw();

Task OnTick();
Expand Down
100 changes: 60 additions & 40 deletions src/Hypnonema.Client/Players/VideoPlayer2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public VideoPlayer2D(DuiBrowser browser, RenderTarget renderTarget, string scree

public float GlobalVolume { get; set; } = 100f;

public bool Is3DAudioEnabled { get; set; } = true;

public RenderTarget RenderTarget { get; }

public string ScreenName { get; }
Expand All @@ -36,96 +38,108 @@ public VideoPlayer2D(DuiBrowser browser, RenderTarget renderTarget, string scree

public float SoundMinDistance { get; set; } = 10f;

public void Dispose()
{
if (this.Browser.IsValid) this.Browser.Dispose();

if (this.RenderTarget.IsValid) this.RenderTarget.Dispose();

GC.SuppressFinalize(this);
}

public void Draw()
public void CalculateVolume()
{
this.RenderTarget.Draw(this.Browser.TxdName, this.Browser.TxnName);
}

public async Task OnTick()
{
this.Draw();

var entity = GetClosestObjectOfType(this.SoundMaxDistance, (uint)this.RenderTarget.Hash);
if (entity == null)
{
this.Browser.SendVolume(0f);
this.Browser.SetVolume(0f);
return;
}

var distance = World.GetDistance(Game.PlayerPed.Position, entity.Position);

if (distance >= this.SoundMaxDistance)
{
this.Browser.SendVolume(0f);
this.Browser.SetVolume(0f);
}
else
{
if (entity.IsOccluded)
if (entity.IsOccluded) this.Browser.SetVolume(this.GlobalVolume / 2);
else this.Browser.SetVolume(this.GlobalVolume);

if (this.Is3DAudioEnabled)
{
this.Browser.SendVolume(this.GlobalVolume / 2);
var tickData = new AudioTickData
{
ListenerForward = Game.PlayerPed.ForwardVector,
ListenerUp = Game.PlayerPed.UpVector,
PositionListener = Game.PlayerPed.Position,
PositionPanner =
entity.Position - Game.PlayerPed.Position, // relative to player
OrientationPanner = entity.ForwardVector
};

this.Browser.Tick(tickData);
}
else
{
this.Browser.SendVolume(this.GlobalVolume);
this.Browser.SetVolume(this.GetSoundFactor(distance) * this.GlobalVolume);
}

var tickData = new AudioTickData
{
ListenerForward = Game.PlayerPed.ForwardVector,
ListenerUp = Game.PlayerPed.UpVector,
PositionListener = Game.PlayerPed.Position,
PositionPanner = entity.Position - Game.PlayerPed.Position, // relative to player
OrientationPanner = entity.ForwardVector
};

this.Browser.SendTick(tickData);
}
}

public void Dispose()
{
if (this.Browser.IsValid) this.Browser.Dispose();

if (this.RenderTarget.IsValid) this.RenderTarget.Dispose();

GC.SuppressFinalize(this);
}

public void Draw()
{
this.RenderTarget.Draw(this.Browser.TxdName, this.Browser.TxnName);
}

public async Task OnTick()
{
this.Draw();

this.CalculateVolume();

await Task.FromResult(0);
}

public void Pause()
{
this.Browser.SendPause();
this.Browser.Pause();
}

public void Play(string url)
{
this.Browser.SendPlay(url);
this.Browser.Play(url);
}

public void Resume()
{
this.Browser.SendResume();
this.Browser.Resume();
}

public void Seek(float time)
{
this.Browser.SendSeek(time);
this.Browser.Seek(time);
}

public void Stop()
{
this.Browser.SendStop();
this.Browser.Stop();
}

public void SynchronizeState(bool paused, float currentTime, string currentSource)
{
this.Browser.SendUpdate(paused, currentTime, currentSource);
this.Browser.Update(paused, currentTime, currentSource);
}

public void Toggle3DAudio(bool value)
{
this.Browser.Toggle3DAudio(value);
}

public void ToggleReplay(bool replay)
{
this.Browser.SendToggleReplay(replay);
this.Browser.ToggleReplay(replay);
}

private static Prop GetClosestObjectOfType(float radius, uint modelHash)
Expand All @@ -143,5 +157,11 @@ private static Prop GetClosestObjectOfType(float radius, uint modelHash)

return entity == 0 ? null : new Prop(entity);
}

private float GetSoundFactor(float distance)
{
return this.SoundMinDistance / (this.SoundMinDistance + this.SoundAttenuation
* (Math.Max(distance, this.SoundMinDistance) - this.SoundMinDistance));
}
}
}
Loading

0 comments on commit 57a31ad

Please sign in to comment.