npm i three
import React, {
useRef, useEffect } from "react";
import * as THREE from "three";
var camera: THREE.Camera, scene: THREE.Object3D, renderer: THREE.WebGLRenderer;
var geometry, material, mesh: THREE.Object3D;
function animate() {
requestAnimationFrame(animate);
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.02;
renderer.render(scene, camera);
}
function App() {
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (ref.current) {
camera = new THREE.PerspectiveCamera(
70,
window.innerWidth / window.innerHeight,
0.01,
10
);
camera.position.z = 1;
scene = new THREE.Scene();
geometry = new THREE.BoxGeometry(0.2, 0.2, 0.2);
material = new THREE.MeshNormalMaterial();
mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
renderer = new THREE.WebGLRenderer({
antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
ref.current.appendChild(renderer.domElement);
}
animate();
}, []);
return <div ref={
ref}></div>;
}
export default App;
/**
* @param [fov=50] Camera frustum vertical field of view. Default value is 50.
* @param [aspect=1] Camera frustum aspect ratio. Default value is 1.
* @param [near=0.1] Camera frustum near plane. Default value is 0.1.
* @param [far=2000] Camera frustum far plane. Default value is 2000.
*/
第一个属性是视野角度(FOV)。视野角度就是无论在什么时候,你所能在显示器上看到的场景的范围,它的值是一个角度。
第二个值是长宽比(aspect ratio)。 也就是你用一个物体的宽除以它的高的比值。比如说,当你在一个宽屏电视上播放老电影时,可以看到图像仿佛是被压扁的。
接下来的两个值是远剪切面和近剪切面。 也就是说当物体所在的位置比摄像机的远剪切面远或者所在位置比近剪切面近的时候,该物体超出的部分将不会被渲染到场景中。
/**
* @param [width=1] — Width of the sides on the X axis.
* @param [height=1] — Height of the sides on the Y axis.
* @param [depth=1] — Depth of the sides on the Z axis.
* @param [widthSegments=1] — Number of segmented faces along the width of the sides.
* @param [heightSegments=1] — Number of segmented faces along the height of the sides.
* @param [depthSegments=1] — Number of segmented faces along the depth of the sides.
*/
mesh = new THREE.Mesh(geometry, material)
就是把材质应用到几何体上。scene.add(mesh)
,会使得这个几何体渲染到场景000的位置import React, {
useRef, useEffect } from "react";
import * as THREE from "three";
var camera: THREE.Camera, scene: THREE.Object3D, renderer: THREE.WebGLRenderer;
var geometry, material, mesh: THREE.Object3D;
let gPlane;
let axes;
let mGeometry;
let gmesh;
let ball;
let mball;
let meshball;
let light;
function animate() {
requestAnimationFrame(animate);
mesh.rotation.x += 0.01;
mesh.rotation.y += 0.02;
renderer.render(scene, camera);
}
function App() {
const ref = useRef<HTMLDivElement>(null);
useEffect(() => {
if (ref.current) {
//场景,相机渲染器初始化
//------------
renderer = new THREE.WebGLRenderer({
antialias: true });
//设置清屏色
renderer.setClearColor(new THREE.Color(0xeeeeee));
//设置场景大小
renderer.setSize(window.innerWidth, window.innerHeight);
//开启阴影,一般不开
renderer.shadowMapEnabled = true;
scene = new THREE.Scene();
//设置测试工具
axes = new THREE.AxesHelper(255);
scene.add(axes); //测试工具加入
//设置相机
camera = new THREE.PerspectiveCamera(
50,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.set(-30, 40, 30);
camera.lookAt(0, 0, 0); //看向场景中央
//-----------
//设置几何体
gPlane = new THREE.PlaneGeometry(70, 50, 1, 1);
material = new THREE.MeshLambertMaterial({
color: 0xeeeeee });
mesh = new THREE.Mesh(gPlane, material);
mesh.rotation.x = -0.5 * Math.PI;
mesh.position.x = 15;
mesh.position.y = 0;
mesh.position.z = 0;
mesh.receiveShadow = true; //配合render的开启阴影进行渲染
scene.add(mesh);
//设置立方体
geometry = new THREE.BoxGeometry(4, 4, 4);
// mGeometry = new THREE.MeshBasicMaterial({
// color: 0xff0000, //basic材质不对光源有反应 lamber和phong会对光源有反应
// wireframe: true,//开启线框
// });
mGeometry = new THREE.MeshLambertMaterial({
color: 0xff0000,
});
gmesh = new THREE.Mesh(geometry, mGeometry);
gmesh.position.x = -4;
gmesh.position.y = 2;
gmesh.position.z = 0;
gmesh.castShadow = true; //配合render的开启阴影进行渲染
scene.add(gmesh);
//设置球体
ball = new THREE.SphereGeometry(4, 20, 20);
mball = new THREE.MeshPhongMaterial({
//这个phong看起来比lamber有高光
color: 0x7777ff,
});
meshball = new THREE.Mesh(ball, mball);
meshball.position.x = 25;
meshball.position.y = 10;
meshball.position.z = 10;
meshball.castShadow = true; //配合render的开启阴影进行渲染
scene.add(meshball);
//设置光源
light = new THREE.SpotLight(0xffffff);
light.position.set(-40, 60, -10);
light.castShadow = true; //配合render的开启阴影进行渲染
scene.add(light);
ref.current.appendChild(renderer.domElement);
renderer.render(scene, camera);
}
// animate();
}, []);
return <div ref={
ref}></div>;
}
export default App;