-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathaudio-debug-system.ts
More file actions
207 lines (194 loc) · 7.87 KB
/
audio-debug-system.ts
File metadata and controls
207 lines (194 loc) · 7.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import audioDebugVert from "../systems/audio-debug.vert";
import audioDebugFrag from "../systems/audio-debug.frag";
import { defineQuery, enterQuery, exitQuery } from "bitecs";
import { getScene, HubsWorld } from "../app";
import { NavMesh } from "../bit-components";
import { DistanceModelType } from "../components/audio-params";
import { getWebGLVersion } from "../utils/webgl";
import { getAudioOrientation, getAudioPosition, isPositionalAudio } from "./audio-emitter-system";
import { Mesh, Material, Vector3, ShaderMaterial } from "three";
import { disposeMaterial } from "../utils/three-utils";
import { ElOrEid } from "../utils/bit-utils";
const fakePanner = {
distanceModel: DistanceModelType.Inverse,
maxDistance: 0,
refDistance: 0,
rolloffFactor: 0,
coneInnerAngle: 0,
coneOuterAngle: 0
};
interface DebugUniforms {
sourcePositions: Array<Vector3>;
sourceOrientations: Array<Vector3>;
distanceModels: Array<number>;
maxDistances: Array<number>;
refDistances: Array<number>;
rolloffFactors: Array<number>;
coneInnerAngles: Array<number>;
coneOuterAngles: Array<number>;
gains: Array<number>;
clipped: Array<number>;
}
let isEnabled = false;
let unsupported = false;
let maxDebugEmitters = 64;
let uniforms: DebugUniforms;
let debugMaterial: ShaderMaterial | undefined | null;
const nav2mat = new Map<number, Material | Material[]>();
const createMaterial = () => {
if (isEnabled) {
if (!uniforms) {
uniforms = {
sourcePositions: new Array<Vector3>(maxDebugEmitters).fill(new Vector3()),
sourceOrientations: new Array<Vector3>(maxDebugEmitters).fill(new Vector3()),
distanceModels: new Array<number>(maxDebugEmitters).fill(0),
maxDistances: new Array<number>(maxDebugEmitters).fill(0),
refDistances: new Array<number>(maxDebugEmitters).fill(0),
rolloffFactors: new Array<number>(maxDebugEmitters).fill(0),
coneInnerAngles: new Array<number>(maxDebugEmitters).fill(0),
coneOuterAngles: new Array<number>(maxDebugEmitters).fill(0),
gains: new Array<number>(maxDebugEmitters).fill(0),
clipped: new Array<number>(maxDebugEmitters).fill(0)
} as DebugUniforms;
}
if (!debugMaterial) {
debugMaterial = new THREE.ShaderMaterial({
uniforms: {
time: { value: 0.0 },
colorInner: { value: new THREE.Color("#7AFF59") },
colorOuter: { value: new THREE.Color("#FF6340") },
colorGain: { value: new THREE.Color("#70DBFF") },
count: { value: 0 },
maxDistance: { value: [] },
refDistance: { value: [] },
rolloffFactor: { value: [] },
distanceModel: { value: [] },
sourcePosition: { value: [] },
sourceOrientation: { value: [] },
coneInnerAngle: { value: [] },
coneOuterAngle: { value: [] },
gain: { value: [] },
clipped: { value: [] }
},
vertexShader: audioDebugVert,
fragmentShader: audioDebugFrag
});
debugMaterial.side = THREE.DoubleSide;
debugMaterial.transparent = true;
debugMaterial.uniforms.count.value = 0;
debugMaterial.defines.MAX_DEBUG_SOURCES = maxDebugEmitters;
}
}
};
getScene().then(() => {
const webGLVersion = getWebGLVersion(APP.scene!.renderer);
if (webGLVersion < "2.0") {
unsupported = true;
} else {
const gl = APP.scene!.renderer.getContext();
const maxUniformVectors = gl.getParameter(gl.MAX_FRAGMENT_UNIFORM_VECTORS);
// 10 is the number of uniform vectors in the shader. If we update that, this number must be updated accordingly.
maxDebugEmitters = Math.min(Math.floor(maxUniformVectors / 10), maxDebugEmitters);
}
if (unsupported) return;
(APP.store as any).addEventListener("statechanged", () => {
isEnabled = APP.store.state.preferences.showAudioDebugPanel;
isEnabled && createMaterial();
defineQuery([NavMesh])(APP.world).forEach(navEid => {
if (unsupported) return;
if (isEnabled) {
addDebugMaterial(APP.world, navEid);
} else {
removeDebugMaterial(APP.world, navEid);
}
});
if (!isEnabled && debugMaterial) {
disposeMaterial(debugMaterial);
debugMaterial = null;
}
});
isEnabled = APP.store.state.preferences.showAudioDebugPanel;
isEnabled && createMaterial();
});
const addDebugMaterial = (world: HubsWorld, navEid: number) => {
if (nav2mat.has(navEid)) return;
const obj = world.eid2obj.get(navEid);
if (obj) {
const navMesh = obj as Mesh;
navMesh.visible = isEnabled;
nav2mat.set(navEid, navMesh.material);
navMesh.material = debugMaterial!;
navMesh.material.needsUpdate = true;
navMesh.geometry.computeVertexNormals();
}
};
const removeDebugMaterial = (world: HubsWorld, navEid: number) => {
if (!nav2mat.has(navEid)) return;
const obj = world.eid2obj.get(navEid);
if (obj) {
const navMesh = obj as Mesh;
navMesh.visible = false;
navMesh.material = nav2mat.get(navEid)!;
nav2mat.delete(navEid);
(navMesh.material as Material).needsUpdate = true;
navMesh.geometry.computeVertexNormals();
}
};
export const cleanupAudioDebugNavMesh = (navEid: number) => removeDebugMaterial(APP.world, navEid);
const emitterPos = new THREE.Vector3();
const emitterDir = new THREE.Vector3();
const navMeshQuery = defineQuery([NavMesh]);
const navMeshEnterQuery = enterQuery(navMeshQuery);
const navMeshExitQuery = exitQuery(navMeshQuery);
export function audioDebugSystem(world: HubsWorld) {
if (unsupported) return;
navMeshExitQuery(world).forEach(navEid => {
removeDebugMaterial(world, navEid);
});
if (isEnabled && uniforms) {
navMeshEnterQuery(world).forEach(navEid => {
isEnabled && addDebugMaterial(world, navEid);
});
let idx = 0;
APP.audios.forEach((audio: AudioNode, audioEmitterId: ElOrEid) => {
if (APP.isAudioPaused.has(audioEmitterId) || APP.mutedState.has(audioEmitterId)) {
return;
}
if (idx >= maxDebugEmitters) return;
const panner = isPositionalAudio(audio) ? audio : fakePanner;
const gain = APP.gains.get(audioEmitterId)!;
getAudioPosition(audioEmitterId, emitterPos);
getAudioOrientation(audioEmitterId, emitterDir);
uniforms.sourcePositions[idx] = emitterPos.clone();
uniforms.sourceOrientations[idx] = emitterDir.clone();
uniforms.distanceModels[idx] = 0;
if (panner.distanceModel === DistanceModelType.Linear) {
uniforms.distanceModels[idx] = 0;
} else if (panner.distanceModel === DistanceModelType.Inverse) {
uniforms.distanceModels[idx] = 1;
} else if (panner.distanceModel === DistanceModelType.Exponential) {
uniforms.distanceModels[idx] = 2;
}
uniforms.maxDistances[idx] = panner.maxDistance;
uniforms.refDistances[idx] = panner.refDistance;
uniforms.rolloffFactors[idx] = panner.rolloffFactor;
uniforms.coneInnerAngles[idx] = panner.coneInnerAngle;
uniforms.coneOuterAngles[idx] = panner.coneOuterAngle;
uniforms.gains[idx] = gain.gain.value;
uniforms.clipped[idx] = APP.clippingState.has(audioEmitterId) ? 1 : 0;
idx++;
});
debugMaterial!.uniforms.time.value = world.time.elapsed;
debugMaterial!.uniforms.distanceModel.value = uniforms.distanceModels;
debugMaterial!.uniforms.maxDistance.value = uniforms.maxDistances;
debugMaterial!.uniforms.refDistance.value = uniforms.refDistances;
debugMaterial!.uniforms.rolloffFactor.value = uniforms.rolloffFactors;
debugMaterial!.uniforms.sourcePosition.value = uniforms.sourcePositions;
debugMaterial!.uniforms.sourceOrientation.value = uniforms.sourceOrientations;
debugMaterial!.uniforms.count.value = idx;
debugMaterial!.uniforms.coneInnerAngle.value = uniforms.coneInnerAngles;
debugMaterial!.uniforms.coneOuterAngle.value = uniforms.coneOuterAngles;
debugMaterial!.uniforms.gain.value = uniforms.gains;
debugMaterial!.uniforms.clipped.value = uniforms.clipped;
}
}