I’m working on a project and I want to utilize WebGPU on the newly released 113 version of Google Chrome. However, I need to ensure that this project can still run WebGL on devices or browsers that don’t support WebGPU.
Therefore, I have set up an if statement to check for WebGPU support. Here is the code, including my imports:
import WebGPURenderer from 'three/examples/jsm/renderers/webgpu/WebGPURenderer.js'
console.log(navigator.gpu);
var renderer;
if (navigator.gpu) {
renderer = new WebGPURenderer({ canvas: canvas,antialias: true } );
} else if (window.WebGLRenderingContext) {
renderer = new THREE.WebGLRenderer({ canvas: canvas,antialias: true} );
}else {
throw new Error("Your browser does not support WebGL or WebGPU");
}
It runs quite well on the local environment, and here are my results. However, there seems to be a discrepancy in the color space for fog.fogColor
and renderer.setClearColor
between WebGPURenderer
and WebGLRenderer
, as shown in the image. But this is not a major issue.
When I tried to test the project on my other devices, including Windows and mobile devices (where Chrome on Windows supports WebGPU, but WebGPU is not supported on mobile devices), using Chrome v113 browser on the same local network, I encountered an error and the canvas couldn’t be rendered. The error message was: “Uncaught ReferenceError: GPUShaderStage is not defined” at line 18 in WebGPUNodeBuilder.js.
I tried to use dynamic imports to load the WebGPURenderer module only when WebGPU is supported. It turns out that the WebGPU Renderer cannot be loaded correctly. Here’s the code
var renderer;
if (typeof navigator.gpu !== 'undefined' && navigator.gpu.requestAdapter) {
import('three/examples/jsm/renderers/webgpu/WebGPURenderer.js')
.then(({ default: WebGPURenderer }) => {
renderer = new WebGPURenderer({ canvas: canvas, antialias: true });
// Initialize your scene with the WebGPURenderer here
})
.catch((error) => {
console.error('Failed to load WebGPURenderer:', error);
// Fallback to WebGLRenderer if you wish or show an error message
});
} else if (window.WebGLRenderingContext) {
renderer = new THREE.WebGLRenderer({ canvas: canvas, antialias: true });
// Initialize your scene with the WebGLRenderer here
} else {
throw new Error("Your browser does not support WebGL or WebGPU");
}
the error is script.js:322 Uncaught TypeError: Cannot set properties of undefined (setting ‘outputColorSpace’)
I’m not sure if there is an error in my code or if something is missing, or if the mechanism of switching renderers is currently not feasible. Please provide guidance on this matter.Thanks!