无法在javascript worker中使用three.js纹理加载程序

无法在javascript worker中使用three.js纹理加载程序,javascript,three.js,web-worker,Javascript,Three.js,Web Worker,我正在尝试使用three.js加载纹理 main.js: const worker = new Worker('../workers/worker.js', { type: 'module' }); 这是simple worker.js: import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.121.1/build/three.module.js'; const textureLoader = new T

我正在尝试使用three.js加载纹理

main.js:

const worker = new Worker('../workers/worker.js', {
    type: 'module'
  });
这是simple worker.js:

    import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.121.1/build/three.module.js';
 const textureLoader = new THREE.TextureLoader();
    textureLoader.load(
      // resource URL
      'images/texture.jpg',
    
      // onLoad callback
      function ( texture ) {
        // in this example we create the material when the texture is loaded
        const material = new THREE.MeshBasicMaterial( {
          map: texture
         } );
      },
    
      // onProgress callback currently not supported
      undefined,
    
      // onError callback
      function ( err ) {
        console.error( 'An error happened.' );
      }
    );
但我得到以下错误:

Uncaught ReferenceError: document is not defined

在我的搜索中,我发现无法访问DOM,但我不知道如何修复,谢谢。

您可以使用而不是
THREE.TextureLoader来解决此问题。这种新类型的加载程序对DOM没有依赖性

还有一个示例演示加载程序。基本用法是:

const-loader=new-THREE.ImageBitmapLoader();
load('path/to/my/texture.png',函数(imageBitmap){
const texture=new THREE.CanvasTexture(图像位图);
const material=new THREE.MeshBasicMaterial({map:texture});
} );