gpt4 book ai didi

javascript - 如何将 Three.js 与 React 结合使用

转载 作者:行者123 更新时间:2023-12-03 00:28:24 24 4
gpt4 key购买 nike

我尝试在 React 应用程序中使用 Three.js,但是即使是 Three.js 文档中的基本示例也无法在 Canvas 上加载。

我首先做了一个vanilla.js sandbox使用运行良好的示例进行实现。然后我将它移植过来创建一个react+two.js 最小sandbox实现失败。

任何人都可以看一下它并为我指出正确的方向吗?

class Viewer extends Component {
state = {};

scene = null;

camera = null;

renderer = new WebGLRenderer();

inst = 0;

viewerRef = React.createRef();

componentDidMount() {
const { domElement } = this.renderer;
this.scene = new Scene();
this.scene.background = new Color("#ccc");
this.camera = new Camera(
75,
domElement.innerWidth / domElement.innerHeight,
0.1,
1000
);
this.renderer.setSize(domElement.innerWidth, domElement.innerHeight);
this.viewerRef.current.appendChild(this.renderer.domElement);
const geometry = new BoxGeometry(1, 1, 1);
const material = new MeshBasicMaterial({ color: 0x05ff00 });
const cube = new Mesh(geometry, material);

this.scene.add(cube);

this.camera.position.z = 5;
this.display();
}

display = () => {
this.renderer.render(this.scene, this.camera);
requestAnimationFrame(this.display);
};

render = () => <div className="viewer" ref={this.viewerRef} />;
}

最佳答案

这是 CodeSandBox,它与 Three.js 的基本 React 包装器代码一起使用。它还具有 THREE.OrbitControls 集成和缩放调整代码:

https://codesandbox.io/s/github/supromikali/react-three-demo

现场演示:https://31yp61zxq6.codesandbox.io/

以下是完整的代码片段,以防上面列出的链接不适合您:

index.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);

two.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 {...THREE, OrbitControls: window.THREE.OrbitControls};

结果应如下所示:

enter image description here

关于javascript - 如何将 Three.js 与 React 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53979627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com