Replace volumetric audio obstacles with flat rectangular walls

This commit is contained in:
2026-10-05 20:09:43 +03:00
parent d88a4b11fd
commit fa8be77559
8 changed files with 62 additions and 44 deletions
+28 -28
View File
@@ -17,13 +17,14 @@ type OcclusionConfig struct {
MaxObjects int `yaml:"max_objects,omitempty"`
}
// Sizes are full dimensions in metres; rotation is a unit quaternion xyzw.
// Size is [width, height] in metres along local X/Z; local Y is the
// normal of a zero-thickness rectangle. Rotation is a unit quaternion xyzw.
// Transmission is an amplitude multiplier, not a power ratio or dB value.
type Obstacle struct {
Priority *int `json:"priority,omitempty"`
ID string `json:"id"`
Center [3]float64 `json:"center"`
Size [3]float64 `json:"size"`
Size []float64 `json:"size"`
Rotation [4]float64 `json:"rotation"`
Transmission *float64 `json:"transmission"`
}
@@ -39,7 +40,7 @@ type Scene struct {
type occluder struct {
priority int
center Vec3
half [3]float64
half [2]float64
axes [3]Vec3
transmission float64
}
@@ -79,11 +80,19 @@ func ParseScene(payload []byte, maxObjects int) (*CompiledScene, error) {
return nil, fmt.Errorf("empty or duplicate obstacle id")
}
ids[object.ID] = true
if len(object.Size) != 2 {
return nil, fmt.Errorf("obstacle %q: size must contain width and height; volumetric walls are unsupported", object.ID)
}
for i := 0; i < 3; i++ {
if !finite(object.Center[i]) || math.Abs(object.Center[i]) >= 1e7 || !finite(object.Size[i]) || object.Size[i] < 1e-4 || object.Size[i] > 1e6 {
if !finite(object.Center[i]) || math.Abs(object.Center[i]) >= 1e7 {
return nil, fmt.Errorf("invalid bounds for obstacle %q", object.ID)
}
}
for _, size := range object.Size {
if !finite(size) || size < 1e-4 || size > 1e6 {
return nil, fmt.Errorf("invalid size for obstacle %q", object.ID)
}
}
if object.Transmission == nil || !finite(*object.Transmission) || *object.Transmission < 0 || *object.Transmission > 1 {
return nil, fmt.Errorf("invalid transmission for obstacle %q", object.ID)
}
@@ -99,7 +108,7 @@ func ParseScene(payload []byte, maxObjects int) (*CompiledScene, error) {
compiled.Objects = append(compiled.Objects, occluder{
priority: priority,
center: Vec3{object.Center[0], object.Center[1], object.Center[2]},
half: [3]float64{object.Size[0] / 2, object.Size[1] / 2, object.Size[2] / 2},
half: [2]float64{object.Size[0] / 2, object.Size[1] / 2},
axes: [3]Vec3{
{1 - 2*(q.Y*q.Y+q.Z*q.Z), 2 * (q.X*q.Y + q.Z*q.W), 2 * (q.X*q.Z - q.Y*q.W)},
q.Right(),
@@ -110,33 +119,24 @@ func ParseScene(payload []byte, maxObjects int) (*CompiledScene, error) {
return compiled, nil
}
// The segment/OBB slab test clips t to [0,1], not to an infinite ray. Each
// object attenuates once, including when an endpoint is inside the volume.
// Boundary contact counts as an intersection; no thickness-based absorption.
// Intersect a finite segment with a two-sided plane, then check the rectangle.
// Parallel/coplanar segments do not cross the wall. Endpoint and edge contact
// count when the segment is not parallel; no volume or thickness is involved.
func (o occluder) intersects(a, b Vec3) bool {
start := Vec3{a.X - o.center.X, a.Y - o.center.Y, a.Z - o.center.Z}
delta := Vec3{b.X - a.X, b.Y - a.Y, b.Z - a.Z}
lo, hi := 0.0, 1.0
for i, axis := range o.axes {
p := start.X*axis.X + start.Y*axis.Y + start.Z*axis.Z
d := delta.X*axis.X + delta.Y*axis.Y + delta.Z*axis.Z
if math.Abs(d) < 1e-10 {
if p < -o.half[i] || p > o.half[i] {
return false
}
continue
}
t0, t1 := (-o.half[i]-p)/d, (o.half[i]-p)/d
if t0 > t1 {
t0, t1 = t1, t0
}
lo = math.Max(lo, t0)
hi = math.Min(hi, t1)
if lo > hi {
return false
}
dot := func(v, axis Vec3) float64 { return v.X*axis.X + v.Y*axis.Y + v.Z*axis.Z }
distance := dot(start, o.axes[1])
denominator := dot(delta, o.axes[1])
if math.Abs(denominator) < 1e-10 {
return false
}
return true
t := -distance / denominator
if t < 0 || t > 1 {
return false
}
point := Vec3{start.X + t*delta.X, start.Y + t*delta.Y, start.Z + t*delta.Z}
return math.Abs(dot(point, o.axes[0])) <= o.half[0]+1e-8 && math.Abs(dot(point, o.axes[2])) <= o.half[1]+1e-8
}
func (s *CompiledScene) Transmission(a, b Vec3) float64 {
return s.transmission(a, b, 0)