首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在react nextjs中没有显示gltf文件

在react nextjs中没有显示gltf文件
EN

Stack Overflow用户
提问于 2020-09-15 13:44:27
回答 1查看 585关注 0票数 1

我正在尝试使用threejs.But在nextjs应用程序中加载一个gltf文件当我尝试在react上使用nextjs应用程序运行它时,它不工作project.This是我如何与webpack配置next.js的:

代码语言:javascript
复制
const withCSS = require('@zeit/next-css');
const withImages = require('next-images');

const withPlugins = require('next-compose-plugins');
module.exports = withPlugins([
    [withCSS, { cssModules: true  }],
    [withImages],
], {
    serverRuntimeConfig: {   serverRuntimeConfigValue: 'test server'  },
    publicRuntimeConfig: {   publicRuntimeConfigValue: {apiUrl:process.env.apiUrl.trim()} },
    webpack: (config, options) => {
        config.module.rules.push({
            test: /\.(glb|gltf)$/,
            use: {
                loader: 'file-loader',
            }
        })
        return config;   },exportTrailingSlash: true
});

我像这样导入文件:

代码语言:javascript
复制
import React from 'react';
import * as THREE from 'three';
import GLTFLoader from 'three-gltf-loader';
import  TransformControls  from './TransformControls.js'
import test2 from "../../../static/images/villa.gltf";

我在componentDidmount中编写了这个函数来加载gltf:

代码语言:javascript
复制
   this.loader.load(test2, gltf => {
            this.gltf = gltf.scene

            // ADD MODEL TO THE SCENE
            this.scene.add(gltf.scene);
          

         });

这是渲染gltf文件时的网络选项卡

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-15 15:25:35

为了使用file-loader正确服务资产,您必须配置_next静态目录的正确位置,如下所示:

代码语言:javascript
复制
{
  loader: 'file-loader',
  options: {
    publicPath: "/_next/static/images", // the path access the assets via url
    outputPath: "static/images/", // where to store on disk
  }
}

但看起来您可能还需要设置加载.bin文件并保留原始名称,因为它将在调用.load函数时加载:

代码语言:javascript
复制
webpack: (config) => {
  config.module.rules.push({
    test: /\.(glb|gltf)$/,
    use: {
      loader: 'file-loader',
      options: {
        publicPath: "/_next/static/images",
        outputPath: "static/images/",
      }
    },
  });
  
  // For bin file
  config.module.rules.push({
    test: /\.(bin)$/,
    use: {
      loader: 'file-loader',
      options: {
        publicPath: "/_next/static/images",
        outputPath: "static/images/",
        name: '[name].[ext]' // keep the original name
      }
    },
  });
}

还可以在组件中导入bin文件:

代码语言:javascript
复制
import "../../../static/images/villa.bin";
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63895811

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档