我一直在努力从three.js导入示例模块,我在项目中使用了 next.js(服务器端React框架),并使用了Express中的自定义服务器。我的服务器代码看起来像这样-
const express = require('express');
const next = require('next');
const favicon = require('serve-favicon');
var path = require('path');
let fs = require('fs')
const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });
nextApp.prepare()
.then(() => {
let server = express(), options = {}, PORT = 3000, app = express()
if (dev) {
// DEVELOPMENT ///
// DEVELOPMENT ///
}
else {
// PRODUCTION ///
options = {
...options
}
// PRODUCTION ///
}
server.use(favicon(path.join(__dirname, "/favicon.ico")))
server.get('/', (req, res) => {
const actualPage = '/';
nextApp.render(req, res, actualPage);
});
server.get('*', (req, res) => {
const actualPage = '/not-found';
nextApp.render(req, res, actualPage);
// return handle(req, res)
});
server.listen((PORT), (err) => {
if (err) throw err
console.log('>> Ready on ' + PORT)
})
})
.catch((ex) => {
console.error(ex.stack)
process.exit(1)
})
我基本上运行了一个npx create-next-app,并在next.js项目中配置了custom express server来进行动态路由,如您在上面的代码中所见。
然后,我使用three.js在home组件中创建了一个场景,该场景已导入并渲染到App.js文件中。我的home.js组件看起来像这样-
import React, { useState, useEffect } from 'react'
import * as THREE from 'three'
import { TrackballControls } from 'three/examples/jsm/controls/TrackballControls'
import "../src/assets/sass/home.scss"
const X = () => {
let parent, renderer, scene, camera, TrackballControls
useEffect(() => {
// renderer
renderer = new THREE.WebGLRenderer()
renderer.setSize( window.innerWidth, window.innerHeight )
document.body.appendChild( renderer.domElement )
// scene
scene = new THREE.Scene()
// camera
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 100 )
camera.position.set( 20, 20, 20 )
// controls
controls = new TrackballControls( camera )
controls.minDistance = 5
controls.maxDistance = 250
controls.enableDamping = true; // an animation loop is required when either damping or auto-rotation are enabled
controls.dampingFactor = 0.05;
// axes
// scene.add(new THREE.AxisHelper( 20 ))
// geometry
let geometry = new THREE.SphereGeometry( 2, 8, 6, 0, 6.3, 0, 3.1)
// material
let material = new THREE.MeshBasicMaterial({
wireframe: true,
wireframeLinewidth: 1
})
let sphere = new THREE.Mesh( geometry, material )
// parent
parent = new THREE.Object3D()
scene.add( parent )
scene.add( sphere )
function animate() {
requestAnimationFrame( animate )
parent.rotation.z += 0.01
controls.update()
renderer.render( scene, camera )
}
animate()
}
,[])
return <div></div>
}
export default X
现在这是我面临的问题-我正在从three.js导入一个名为TrackballControls的示例模块,它不完全位于核心三个模块中,而是位于您外部的文件夹中可以从路径- 'three / examples / jsm / controls / TrackballControls.js' 中查看。您可以在这里看到更多信息-Importing es6 modules separately。但是不知何故,它不起作用。抛出
之类的错误我尝试使用一个简单的create-react-app做同样的事情,导入完全有效!!因此,我知道服务器端代码存在问题,并且我假设这是与Webpack相关的问题。但是我对webpack真的不了解。有人请帮助我,将不胜感激!
如果有帮助,这是我的 next.config.js 文件-
const withSASS = require('@zeit/next-sass')
const { parsed: localEnv } = require('dotenv').config()
const webpack = require('webpack')
// const path = require('path')
function HACK_removeMinimizeOptionFromCssLoaders(config) {
console.warn(
'HACK: Removing `minimize` option from `css-loader` entries in Webpack config',
)
config.module.rules.forEach(rule => {
if (Array.isArray(rule.use)) {
rule.use.forEach(u => {
if (u.loader === 'css-loader' && u.options) {
delete u.options.minimize
}
})
}
})
}
module.exports = withSASS(
{
webpack(config) {
HACK_removeMinimizeOptionFromCssLoaders(config)
config.plugins.push(new webpack.EnvironmentPlugin(localEnv))
return config
}
})
答案 0 :(得分:0)
我知道这里没有人可以看到,但是如果将来有人遇到这个问题,我会发布我的解决方案。所以这就是我想出的-
在我的问题中我没有提到我正在使用一个名为next.js的SSR(服务器端渲染)反应框架。使用纯React应用程序,导入可以完美运行。但是在服务器端框架中,与导入相关的工作应在useEffect(或componentDidMount)内部以及其余threejs工作中完成。所以我像这样动态导入它-
let dynamicallyImportPackage = async () => {
let TrackballControls
await import('three/examples/jsm/controls/TrackballControls')
// you can now use the package in here
.then(module => {
TrackballControls = module.TrackballControls
})
.catch(e => console.log(e))
return TrackballControls
}
然后,我像这样在useEffect中使用它-
let TrackbackControls = await dynamicallyImportPackage()
// controls
controls = new TrackbackControls(camera, container)
controls.minDistance = 5
controls.maxDistance = 250
Arigato Gosaimasu!