【发布时间】:2022-03-23 17:04:38
【问题描述】:
我正在尝试将模型加载到我的场景中。不幸的是到目前为止没有成功。我看到的是一个空旷的场景,而不是一个模型。当我插入 boxgeometry 时,也可以看到它,所以 three.js 可以正常工作。我尝试从示例文件“Stork.glb”中加载模型。我已将所有内容减少到最低限度。我看不出现在会是什么。我是否必须以不同方式集成 GLTFLoader?
index.js
import Main from "./main";
new Main();
main.js
import * as THREE from "../lib/three/build/three.module.js";
import { OrbitControls } from '../lib/three/examples/jsm/controls/OrbitControls.js';
import { GLTFLoader } from '../lib/three/examples/jsm/loaders/GLTFLoader.js';
class Main {
constructor(){
this.init();
this.animate();
}
init(){
this.renderer = new THREE.WebGLRenderer( { antialias: true } );
this.renderer.setPixelRatio( window.devicePixelRatio );
this.renderer.shadowMap.enabled = true;
this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
this.renderer.autoClear = false;
this.container = document.getElementById('container');
this.renderer.setSize(this.container.clientWidth, this.container.clientHeight);
this.container.appendChild( this.renderer.domElement );
this.aspect = this.container.clientWidth / this.container.clientHeight;
this.scene = new THREE.Scene();
this.scene.background = new THREE.Color( 0x555555 );
this.camera = new THREE.PerspectiveCamera( 45, this.aspect, .1, 10000 );
this.camera.position.set(0, 0, 300);
this.controls = new OrbitControls( this.camera, this.renderer.domElement );
this.controls.enableZoom = true;
this.controls.enabled = true;
this.controls.target.set(0, 0, 0);
//-------------------------------------------------------------------------------------------------
var light = new THREE.AmbientLight(0xffffff);
this.scene.add(light);
const loader = new GLTFLoader();
loader.load("models/Stork.glb", function(gltf){
this.scene.add(gltf.scene);
});
}//end init
animate(){
requestAnimationFrame( this.animate.bind(this) );
this.render();
}//end animate
render(){
this.controls.update();
this.camera.updateMatrixWorld();
this.camera.updateProjectionMatrix();
this.renderer.render(this.scene, this.camera);
}//end render
}//end class
export default Main;
文件夹布局:
mainfolder
|
|-index.html
|-webpack.config.js
|-package.json
|-build
|-src
| |-index.js
| |-main.js
|
|-lib
| |-three
|
|-models
|
Stork.glb
webpack.config.js
const path = require('path');
module.exports = {
mode: 'development',
//devtool: "none",
//mode: 'production',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'bundle.js' },
//plugins: [new HtmlWebpackPlugin()],
module: {
rules: [
{
test: /\.(glb)$/i,
use: [ { loader: 'file-loader', options: { outputPath: 'models/' } } ]
},
],
},
};//end module.exports
【问题讨论】:
-
错误是什么?
-
我在平板电脑上编程,很遗憾我没有看到任何错误信息
-
好的,现在我有一个错误日志:未捕获的参考错误:未定义三个 GLTFLoader.js:3 Fetch API 无法加载文件:///storage/emulated/0/Atests/ModelLoader/models/Stork .glb URL 方案“文件”不受支持。 webpack://OPm/./lib/three/build/three.module.js?:39056 TypeError: Failed to fetch webpack://OPm/./lib/three/examples/jsm/loaders/GLTFLoader.js?: 115
标签: javascript three.js