Rise
The Vieneo Province
Loading...
Searching...
No Matches
VehicleSoundSpatial.h
Go to the documentation of this file.
1#pragma once
2
4#include <algorithm>
5#include <cmath>
6
8{
9 struct Mix
10 {
11 float gain = 1.f;
12 float pan = 0.f;
13 };
14
15 // Camera mounts are in km and use the same signed translation as matrixView.
16 // Keep close mounted cameras at full level; halve amplitude each time the
17 // distance doubles beyond the source's reference distance. Zoom does not move the ears.
18 inline Mix Calculate(const RiseMath::Vector3& cameraFromVehicle,
19 float yaw, float pitch, float roll, float referenceDistanceKm = .005f)
20 {
21 const float distance = RiseMath::Vec3Length(&cameraFromVehicle);
22 if (!std::isfinite(distance) || !std::isfinite(yaw) ||
23 !std::isfinite(pitch) || !std::isfinite(roll) ||
24 !std::isfinite(referenceDistanceKm) || referenceDistanceKm <= 0.f)
25 return {0.f, 0.f};
26 if (distance <= .000001f)
27 return {};
28
30 RiseMath::MatrixRotationYawPitchRoll(&look, yaw, pitch, roll);
31 RiseMath::MatrixInverse(&look, nullptr, &look);
32 RiseMath::Vector3 sourceInView;
33 RiseMath::Vec3TransformNormal(&sourceInView, &cameraFromVehicle, &look);
34
35 // Rise's projection rotates the view 180 degrees about Z, so negative
36 // view X is screen right. Include elevation to keep overhead sounds central.
37 const float pan = std::clamp(-sourceInView.x / distance, -1.f, 1.f);
38 return {std::min(1.f, referenceDistanceKm / distance), pan};
39 }
40}
float Vec3Length(const Vector3 *a)
Definition ClientMath.h:95
Vector3 * Vec3TransformNormal(Vector3 *out, const Vector3 *a, const Matrix *m)
Definition ClientMath.h:107
Matrix * MatrixRotationYawPitchRoll(Matrix *out, float yaw, float pitch, float roll)
Definition ClientMath.h:137
Matrix * MatrixInverse(Matrix *out, float *determinant, const Matrix *a)
Definition ClientMath.h:118
Mix Calculate(const RiseMath::Vector3 &cameraFromVehicle, float yaw, float pitch, float roll, float referenceDistanceKm=.005f)