Skip to content

Commit 024637e

Browse files
authored
[VM] 3D audio panning (riperiperi#243)
* [VM[ 3D audio panning Calculates panning for entity sounds and adds avatars to sound entities on sound events. * Fix crashing when rendering a city with no roads.
1 parent 2a48c75 commit 024637e

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

TSOClient/tso.client/Rendering/City/CityGeometry.cs

+8-5
Original file line numberDiff line numberDiff line change
@@ -468,11 +468,14 @@ public void RegenMeshVerts(GraphicsDevice gd, Rectangle? range)
468468

469469
RoadIndices?.Dispose();
470470
RoadVertices?.Dispose();
471-
RoadIndices = new IndexBuffer(gd, IndexElementSize.ThirtyTwoBits, roadIndices.Count, BufferUsage.None);
472-
RoadIndices.SetData(roadIndices.ToArray());
473-
RoadVertices = new VertexBuffer(gd, typeof(TLayerVertex), roadVertices.Count, BufferUsage.None);
474-
RoadVertices.SetData(roadVertices.ToArray());
475-
RoadPrims = roadIndices.Count / 3;
471+
if (roadVertices.Count > 0)
472+
{
473+
RoadIndices = new IndexBuffer(gd, IndexElementSize.ThirtyTwoBits, roadIndices.Count, BufferUsage.None);
474+
RoadIndices.SetData(roadIndices.ToArray());
475+
RoadVertices = new VertexBuffer(gd, typeof(TLayerVertex), roadVertices.Count, BufferUsage.None);
476+
RoadVertices.SetData(roadVertices.ToArray());
477+
RoadPrims = roadIndices.Count / 3;
478+
}
476479
}
477480

478481
private int O(int x, int y, int minx, int maxx)

TSOClient/tso.simantics/Entities/VMAvatar.cs

+1
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ private void HandleTimePropsEvent(TimePropertyListItem tp)
490490
Zoom = true,
491491
};
492492
owner.SoundThreads.Add(entry);
493+
owner.Thread.Context.VM.SoundEntities.Add(this);
493494
owner.TickSounds();
494495
}
495496
}

TSOClient/tso.simantics/Entities/VMEntity.cs

+23-1
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,6 @@ public void TickSounds()
369369
float pan = (SoundThreads[i].Pan) ? Math.Max(-1.0f, Math.Min(1.0f, scrPos.X / worldSpace.WorldPxWidth)) : 0;
370370
pan = pan * pan * ((pan > 0)?1:-1);
371371

372-
373372
float volume = 1f;
374373

375374
var rcs = worldState.Cameras.ActiveCamera as CameraController3D;
@@ -382,6 +381,29 @@ public void TickSounds()
382381
volume = 1.5f - delta.Length() / 40f;
383382
volume *= (10 / ((rcs.Zoom3D * rcs.Zoom3D) + 10));
384383
volume *= worldState.PreciseZoom;
384+
385+
//Calculate 3D sound
386+
if (SoundThreads[i].Pan)
387+
{
388+
//Calculate camera vectors and heading relative to sound source
389+
var sourcePos = new Vector3(vp.X, vp.Z, vp.Y);
390+
391+
var lookAt = rcs.Camera.Target - rcs.Camera.Position;
392+
lookAt.Normalize();
393+
394+
var lookUp = Vector3.Up;
395+
396+
var lookSide = Vector3.Cross(lookAt, lookUp);
397+
lookSide.Normalize();
398+
399+
var heading = sourcePos - rcs.Camera.Position;
400+
heading.Normalize();
401+
402+
pan = Vector3.Dot(lookSide, heading);
403+
404+
//Tweak exponent so that sounds don't go off one ear too soon
405+
pan = (float)Math.Pow(Math.Abs(pan), 2.25f) * Math.Sign(pan);
406+
}
385407
}
386408
else
387409
{

0 commit comments

Comments
 (0)