简体   繁体   English

如何将 .gltf/.glb 模型加载到 Three.js 应用程序中?

[英]How to load .gltf/.glb model into Three.js app?

I went through a few similar topics here on SO but I can't find a solution to my problem.我在这里经历了一些类似的主题,但我找不到解决我的问题的方法。 The issue is that .glb model doesn't load at all.问题是.glb模型根本不加载。 My Vue app uses webpack (I use Quasar framework which has built-in webpack).我的 Vue 应用程序使用 webpack(我使用内置 webpack 的 Quasar 框架)。 I configured Webpack to bundle .glb files (I read that file-loader is sufficient here).我将 Webpack 配置为捆绑.glb文件(我在这里读到file-loader就足够了)。 It doesn't help.它没有帮助。 My model sits in assets folder.我的模型位于assets文件夹中。 Some say that 3D model should be placed in public (or static ) folder.有人说 3D 模型应该放在public (或static )文件夹中。 I've tried it and both solutions don't work in my case.我已经尝试过了,两种解决方案都不适用于我的情况。

I loaded model to main_three.js and updated webpack in quasar.conf.js .我将模型加载到main_three.js并在 quasar.conf.js 中更新了quasar.conf.js Here is a reproducible example: https://codesandbox.io/s/interesting-mendeleev-3e1zoy?file=/src/store/model/main_three.js这是一个可重现的示例: https ://codesandbox.io/s/interesting-mendeleev-3e1zoy?file=/src/store/model/main_three.js

main_three.js main_three.js

import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";

const loader = new GLTFLoader();
loader.load("~assets/Flower.glb", (glb) => {
  state.scene.add(glb.scene);
});

quasar.conf.js quasar.conf.js

chainWebpack(chain) {
  chain.module.rule('glb')
  .test(/\.glb$/)
  .use('file-loader')
  .loader('file-loader')
}

I think that this will work:我认为这会起作用:

 const loader = new GLTFLoader(); loader.load("../assets/Flower.glb", (glb) => { state.scene.add(glb.scene); });

put ../ in front of assets/Flower.glb in the file url.../放在文件 url 中 assets/Flower.glb 的前面。 i think that will work because when we use the ~ operator, it does -(A + 1) where A is the value to the right of the tilde.我认为这会起作用,因为当我们使用 ~ 运算符时,它确实 -(A + 1) 其中 A 是波浪号右侧的值。 For example, we'd get -2 if we did this (since -(1 + 1) = -2):例如,如果我们这样做,我们会得到 -2(因为 -(1 + 1) = -2):

**~**['apples', 'oranges'].indexOf('apples'); // => 0
**~**['apples', 'oranges'].indexOf('oranges'); // => -2 

while the ../ goes back a folder.../返回一个文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM