I’ve been having some difficulties while trying to integrate Babylon.js WebGPU engine into my React project, which is built with TypeScript and Vite. While the setup works perfectly fine with Babylon.js WebGL engine, I’m consistently getting the following errors when switching to the WebGPUEngine:
wasm streaming compile failed: TypeError: Failed to execute 'compile' on 'WebAssembly': Incorrect response MIME type. Expected 'application/wasm'.
This appears to be caused by the cdn downloaded scripts, for glslang.js, twgsl.js
Additionally, I’m seeing warnings like:
[TextureView "TextureView_SwapChain_ResolveTarget"] is associated with [Device "BabylonWebGPUDevice0"], and cannot be used with [Device "BabylonWebGPUDevice1"].
heres an example of some code i am trying to run with the WebGPUEngine when getting these types of warnings:
import { useEffect, useRef, useState } from 'react';
import { Engine, WebGPUEngine, Scene, Color4, MeshBuilder, StandardMaterial, Color3, ArcRotateCamera, Vector3, HemisphericLight } from '@babylonjs/core';
const App: React.FC = () => {
const canvasRef = useRef<HTMLCanvasElement>(null);
const [sceneKey, setSceneKey] = useState<'sceneone' | 'scenetwo' | 'scenethree'>('sceneone');
const scenes: Record<'sceneone' | 'scenetwo' | 'scenethree', { background: Color4; boxColor: Color3 }> = {
sceneone: {
background: new Color4(1, 1, 1, 1),
boxColor: new Color3(0, 1, 0),
},
scenetwo: {
background: new Color4(1, 1, 1, 1),
boxColor: new Color3(1, 0, 0),
},
scenethree: {
background: new Color4(1, 1, 1, 1),
boxColor: new Color3(0, 0, 1),
},
};
useEffect(() => {
if (!canvasRef.current) return;
const initializeEngine = async () => {
console.log('Initializing engine');
// const engine = n ew Engine(canvasRef.current!);
const engine = new WebGPUEngine(canvasRef.current!);
await engine.initAsync();
const scene = new Scene(engine);
scene.clearColor = scenes[sceneKey].background;
const box = MeshBuilder.CreateBox('box', { size: 2 }, scene);
const boxMaterial = new StandardMaterial('boxMaterial', scene);
boxMaterial.diffuseColor = scenes[sceneKey].boxColor;
box.material = boxMaterial;
new HemisphericLight('light', new Vector3(0, 1, 0), scene);
const camera = new ArcRotateCamera('camera', -Math.PI / 2, Math.PI / 2, 5, Vector3.Zero(), scene);
camera.attachControl(canvasRef.current, true);
engine.runRenderLoop(() => {
scene.render();
});
return () => {
scene.dispose();
engine.dispose();
};
};
initializeEngine();
}, [sceneKey]);
const handleSceneChange = () => {
switch (sceneKey) {
case 'sceneone':
console.log('Switching to scenetwo');
setSceneKey('scenetwo');
break;
case 'scenetwo':
console.log('Switching to scenethree');
setSceneKey('scenethree');
break;
case 'scenethree':
console.log('Switching to sceneone');
setSceneKey('sceneone');
break;
default:
break;
}
};
return (
<div>
<canvas ref={canvasRef} style={{ width: '100%', height: '400px' }} />
<button onClick={handleSceneChange}>Switch Scene</button>
</div>
);
};
export default App;