使用 Webpack 加载 GLB 模型 - Three.js

Mll*_*eBz 3 three.js webpack gltf

我是第一次尝试使用 Webpack,但在添加我的 glb 模型时遇到了麻烦。我的模型没问题,使用了很多次,我放在公共文件夹中。我不明白控制台错误,任何帮助将不胜感激,谢谢。

我正在使用three.js r116 和Firefox。Safari 告诉我同样的错误,找不到模型。

这是我的 JS 代码的一部分:

import * as THREE from 'three';
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader';

const loader = new GLTFLoader();
    loader.load('/assets/models/street_car.glb', (gltf) => {
        scene.add(gltf.scene);
    });
Run Code Online (Sandbox Code Playgroud)

我的 webpack.config :

const path = require('path');
const CopyWebpackPlugin = require('copy-webpack-plugin');

module.exports = {
    entry: './src/scripts/main.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        filename: 'dist/main.js',
    },
    performance: {
        hints: false
    },
    plugins: [
        new CopyWebpackPlugin([{ from: '**/*', to: '' }], {
            context: 'src',
            writeToDisk: true,
        }),
    ],
    devServer: {
        contentBase: path.resolve(__dirname, 'dist'),
        port: 9000, 
        historyApiFallback: true
    }
};
Run Code Online (Sandbox Code Playgroud)

最后控制台错误:

在此处输入图片说明

Mll*_*eBz 7

我刚找到问题,把这行加到 webpack.config

module:
    {
        rules:
        [
            {
                test: /\.(glb|gltf)$/,
                use:
                [
                    {
                        loader: 'file-loader',
                        options:
                        {
                            outputPath: 'assets/models/'
                        }
                    }
                ]
            },
        ]
    }
Run Code Online (Sandbox Code Playgroud)

并且不需要在公共文件夹中添加资产,它们在我的 src 文件夹中带有脚本。