Skip to content

Commit

Permalink
[VM] 3D audio panning (riperiperi#243)
Browse files Browse the repository at this point in the history
* [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.
  • Loading branch information
LazyDuchess authored Jul 30, 2022
1 parent 2a48c75 commit 024637e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
13 changes: 8 additions & 5 deletions TSOClient/tso.client/Rendering/City/CityGeometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -468,11 +468,14 @@ public void RegenMeshVerts(GraphicsDevice gd, Rectangle? range)

RoadIndices?.Dispose();
RoadVertices?.Dispose();
RoadIndices = new IndexBuffer(gd, IndexElementSize.ThirtyTwoBits, roadIndices.Count, BufferUsage.None);
RoadIndices.SetData(roadIndices.ToArray());
RoadVertices = new VertexBuffer(gd, typeof(TLayerVertex), roadVertices.Count, BufferUsage.None);
RoadVertices.SetData(roadVertices.ToArray());
RoadPrims = roadIndices.Count / 3;
if (roadVertices.Count > 0)
{
RoadIndices = new IndexBuffer(gd, IndexElementSize.ThirtyTwoBits, roadIndices.Count, BufferUsage.None);
RoadIndices.SetData(roadIndices.ToArray());
RoadVertices = new VertexBuffer(gd, typeof(TLayerVertex), roadVertices.Count, BufferUsage.None);
RoadVertices.SetData(roadVertices.ToArray());
RoadPrims = roadIndices.Count / 3;
}
}

private int O(int x, int y, int minx, int maxx)
Expand Down
1 change: 1 addition & 0 deletions TSOClient/tso.simantics/Entities/VMAvatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ private void HandleTimePropsEvent(TimePropertyListItem tp)
Zoom = true,
};
owner.SoundThreads.Add(entry);
owner.Thread.Context.VM.SoundEntities.Add(this);
owner.TickSounds();
}
}
Expand Down
24 changes: 23 additions & 1 deletion TSOClient/tso.simantics/Entities/VMEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,6 @@ public void TickSounds()
float pan = (SoundThreads[i].Pan) ? Math.Max(-1.0f, Math.Min(1.0f, scrPos.X / worldSpace.WorldPxWidth)) : 0;
pan = pan * pan * ((pan > 0)?1:-1);


float volume = 1f;

var rcs = worldState.Cameras.ActiveCamera as CameraController3D;
Expand All @@ -382,6 +381,29 @@ public void TickSounds()
volume = 1.5f - delta.Length() / 40f;
volume *= (10 / ((rcs.Zoom3D * rcs.Zoom3D) + 10));
volume *= worldState.PreciseZoom;

//Calculate 3D sound
if (SoundThreads[i].Pan)
{
//Calculate camera vectors and heading relative to sound source
var sourcePos = new Vector3(vp.X, vp.Z, vp.Y);

var lookAt = rcs.Camera.Target - rcs.Camera.Position;
lookAt.Normalize();

var lookUp = Vector3.Up;

var lookSide = Vector3.Cross(lookAt, lookUp);
lookSide.Normalize();

var heading = sourcePos - rcs.Camera.Position;
heading.Normalize();

pan = Vector3.Dot(lookSide, heading);

//Tweak exponent so that sounds don't go off one ear too soon
pan = (float)Math.Pow(Math.Abs(pan), 2.25f) * Math.Sign(pan);
}
}
else
{
Expand Down

0 comments on commit 024637e

Please sign in to comment.