|
1 | 1 | import React, { useEffect, useRef } from 'react'; |
2 | | -import { Scene as BabylonScene, SceneOptions, WebXRDefaultExperienceOptions, HavokPlugin, Vector3, Nullable, Camera } from '@babylonjs/core'; |
| 2 | +import { Scene as BabylonScene, SceneOptions, WebXRDefaultExperienceOptions, HavokPlugin, Vector3, Nullable, Camera, Engine } from '@babylonjs/core'; |
3 | 3 | import { GUI3DManager } from '@babylonjs/gui'; |
4 | 4 | import HavokPhysics from '@babylonjs/havok'; |
5 | 5 | import { SceneContext, Store, createBabylonStore } from './store'; |
@@ -85,12 +85,34 @@ export const Scene: React.FC<SceneProps> = ({ children, sceneOptions, onSceneRea |
85 | 85 | engine.registerView(canvas, scene.activeCamera as Camera); |
86 | 86 | } |
87 | 87 | scene.detachControl(); |
88 | | - |
89 | 88 | canvas.onclick = () => { |
| 89 | + if (!activeScene) { |
| 90 | + // disable audio on all scenes but no current |
| 91 | + engine.scenes.forEach(_scene => { |
| 92 | + if (_scene !== scene) { |
| 93 | + if (_scene.audioEnabled) { |
| 94 | + _scene.audioEnabled = false; |
| 95 | + } |
| 96 | + } |
| 97 | + }); |
| 98 | + } |
90 | 99 | if (activeScene !== scene) { |
| 100 | + if (activeScene?.audioEnabled) { |
| 101 | + // disabled audio on previous scene |
| 102 | + activeScene.audioEnabled = false; |
| 103 | + } |
91 | 104 | activeScene?.detachControl(); |
92 | 105 | engine.inputElement = canvas; |
93 | 106 | scene.attachControl(); |
| 107 | + // set listener to camera position of current scene |
| 108 | + if (Engine.audioEngine) { |
| 109 | + scene.audioEnabled = true; |
| 110 | + const listener = Engine.audioEngine.audioContext?.listener; |
| 111 | + if (listener) { |
| 112 | + const camPos = scene.activeCamera!.position; |
| 113 | + listener.setPosition(camPos.x, camPos.y, camPos.z); |
| 114 | + } |
| 115 | + } |
94 | 116 | activeScene = scene; |
95 | 117 | } |
96 | 118 | }; |
@@ -128,7 +150,10 @@ export const Scene: React.FC<SceneProps> = ({ children, sceneOptions, onSceneRea |
128 | 150 | })(); |
129 | 151 |
|
130 | 152 | return () => { |
131 | | - Reactylon.unmount(rootContainer.current!, disposeEngine); |
| 153 | + Reactylon.unmount(rootContainer.current!, () => { |
| 154 | + activeScene = null; |
| 155 | + disposeEngine(); |
| 156 | + }); |
132 | 157 | rootContainer.current = null; |
133 | 158 | }; |
134 | 159 | }, []); |
|
0 commit comments