我目前正在构建一个利用THREE.js.
我想从THREE.js库中导入一些代码,该库不属于官方发行版。在 的官方存储库下THREE.js,examples 文件夹下有一些模块,官方文档THREE.js利用这些模块来展示示例。
我如何利用这些模块并在我自己的代码中使用它们?
在我的应用程序中,我想使用该THREE.STLExporter模块。(https://github.com/mrdoob/ Three.js/blob/ master/examples/js/exporters/STLExporter.js )
因为我已经安装three为我的应用程序的依赖项,所以我首先尝试简单地执行import * as THREE from 'three',但这似乎没有成功。
然后我尝试examples直接访问该文件夹并通过执行 `require(' Three/examples/js/exporters/STLExporter') 手动获取模块,但这也不起作用。
我检查了官方文档的源代码,注意到示例直接在标签中包含必要的模块,但我不想这样做,因为我正在构建一个 React 应用程序。我希望能够通过 NPM 包含模块或在我的应用程序中存储模块的代码。
请帮助这里的菜鸟。谢谢你!
您可以创建一个单独的 Three.js 文件,该文件将导入“三”,向其中添加任何示例代码并导出为对象,以下是与 THREE.OrbitControls 配合使用的代码片段:
import * as THREE from 'three';
window.THREE = THREE; // THREE.OrbitControls expects THREE to be a global object
require('three/examples/js/controls/OrbitControls');
export default window.THREE;
Run Code Online (Sandbox Code Playgroud)
然后在您的应用程序中使用three.js文件,如下所示:
import React, { Component } from "react";
import ReactDOM from "react-dom";
import THREE from "./three";
class App extends Component {
componentDidMount() {
// BASIC THREE.JS THINGS: SCENE, CAMERA, RENDERER
// Three.js Creating a scene tutorial
// https://threejs.org/docs/index.html#manual/en/introduction/Creating-a-scene
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.z = 5;
var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
// MOUNT INSIDE OF REACT
this.mount.appendChild(renderer.domElement); // mount a scene inside of React using a ref
// CAMERA CONTROLS
// https://threejs.org/docs/index.html#examples/controls/OrbitControls
this.controls = new THREE.OrbitControls(camera);
// ADD CUBE AND LIGHTS
// https://threejs.org/docs/index.html#api/en/geometries/BoxGeometry
// https://threejs.org/docs/scenes/geometry-browser.html#BoxGeometry
var geometry = new THREE.BoxGeometry(2, 2, 2);
var material = new THREE.MeshPhongMaterial( {
color: 0x156289,
emissive: 0x072534,
side: THREE.DoubleSide,
flatShading: true
} );
var cube = new THREE.Mesh(geometry, material);
scene.add(cube);
var lights = [];
lights[ 0 ] = new THREE.PointLight( 0xffffff, 1, 0 );
lights[ 1 ] = new THREE.PointLight( 0xffffff, 1, 0 );
lights[ 2 ] = new THREE.PointLight( 0xffffff, 1, 0 );
lights[ 0 ].position.set( 0, 200, 0 );
lights[ 1 ].position.set( 100, 200, 100 );
lights[ 2 ].position.set( - 100, - 200, - 100 );
scene.add( lights[ 0 ] );
scene.add( lights[ 1 ] );
scene.add( lights[ 2 ] );
// SCALE ON RESIZE
// Check "How can scene scale be preserved on resize?" section of Three.js FAQ
// https://threejs.org/docs/index.html#manual/en/introduction/FAQ
// code below is taken from Three.js fiddle
// http://jsfiddle.net/Q4Jpu/
// remember these initial values
var tanFOV = Math.tan( ( ( Math.PI / 180 ) * camera.fov / 2 ) );
var windowHeight = window.innerHeight;
window.addEventListener( 'resize', onWindowResize, false );
function onWindowResize( event ) {
camera.aspect = window.innerWidth / window.innerHeight;
// adjust the FOV
camera.fov = ( 360 / Math.PI ) * Math.atan( tanFOV * ( window.innerHeight / windowHeight ) );
camera.updateProjectionMatrix();
camera.lookAt( scene.position );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.render( scene, camera );
}
// ANIMATE THE SCENE
var animate = function() {
requestAnimationFrame(animate);
cube.rotation.x += 0.01;
cube.rotation.y += 0.01;
renderer.render(scene, camera);
};
animate();
}
render() {
return <div ref={ref => (this.mount = ref)} />;
}
}
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);
Run Code Online (Sandbox Code Playgroud)
stackblitz 上的工作演示https://stackblitz.com/edit/react-two-example-import?file=index.js
| 归档时间: |
|
| 查看次数: |
2862 次 |
| 最近记录: |