Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load GLB model with Webpack - Three.js

I'm trying to use Webpack for the first time and I have trouble to add my glb model. My model is ok, used many times and I put in public folder. I dont' understand console error, any help will be appreciate, thanks.

I'm using three.js r116 and Firefox. Safari tell me same error, can't found the model.

Here a part of my JS code :

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);
    });

My 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
    }
};

And finally console error :

enter image description here

like image 472
MlleBz Avatar asked Jun 10 '26 20:06

MlleBz


1 Answers

I just find the problem, add this lines to webpack.config

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

And don't need to add assets in public folder, they are in my src folder with scripts.

like image 173
MlleBz Avatar answered Jun 17 '26 14:06

MlleBz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!