【发布时间】:2018-06-28 22:57:16
【问题描述】:
我正在尝试使用 React 服务器端渲染使 Post-processing Outline Thee.js example 工作。我的主要问题是这些行(47 和 280)(来自示例):
<script src="js/postprocessing/OutlinePass.js"></script>
outlinePass = new THREE.OutlinePass( new THREE.Vector2( window.innerWidth, window.innerHeight ), scene, camera );
composer.addPass( outlinePass );
因为我不确定如何在 React 中包含 OutlinePass。我安装了包 postprocessing (npm profile) 但它没有 OutlinePass。
我还尝试像这样在ComponentDidMount 和ComponentWillMount 中导入脚本:
const script = document.createElement("script");
script.src = require('!!url-loader!../../../assets/webgl/js/OutlinePass.js');
script.async = true;
document.body.appendChild(script);
但它向我显示了错误:Uncaught ReferenceError: THREE is not defined。
我也试过(一次一个):
import OutlinePass from '../../../assets/webgl/js/OutlinePass.js';
const OutlinePass = require('../../../assets/webgl/js/OutlinePass.js');
const OutlinePass = require('../../../assets/webgl/js/OutlinePass.js')(THREE);
那也没用。
我的 jsx(部分):
import * as THREE from 'three';
import * as OBJLoader from 'three-obj-loader';
componentWillMount() {
const width = window.innerWidth;
const height = window.innerHeight;
this.setState({
windowW: width,
windowH: height
});
// LOAD SCRIPTS
const script = document.createElement("script");
script.src = require('!!url-loader!../../../assets/webgl/js/OutlinePass.js');
script.async = true;
document.body.appendChild(script);
}
componentDidMount() {
const { windowH, windowW } = this.state;
const { THREE } = this;
const TrackballControls = require('three-trackballcontrols');
OBJLoader(THREE);
// WINDOW RESIZE
// window.addEventListener( 'resize', onWindowResize, false );
// POST-PROCESSING
const outlinePass = new THREE.OutlinePass( new THREE.Vector2( window.innerWidth, window.innerHeight ), scene, camera );
// Code for this not included to clear space
this.camera = camera;
this.controls = controls;
this.scene = scene;
this.renderer = renderer;
this.mount.appendChild(this.renderer.domElement);
this.start();
}
componentWillUnmount() {
this.stop();
this.mount.removeChild(this.renderer.domElement);
}
start() {
if (!this.frameId) {
this.frameId = requestAnimationFrame(this.animate);
}
}
stop() {
cancelAnimationFrame(this.frameId);
}
animate() {
this.renderScene();
this.frameId = window.requestAnimationFrame(this.animate);
this.controls.update();
}
renderScene() {
this.renderer.render(this.scene, this.camera);
}
【问题讨论】:
标签: javascript node.js reactjs three.js