<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script src="./main.js"></script>
</body>
</html>
index.html
main.js
package.json
现在支持上传本地图片了!
import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';
// 引入gltf模型加载库GLTFLoader.js
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
// 三维向量Vector3创建一组顶点坐标
const arr = [
new THREE.Vector3(-50, 20, 90),
new THREE.Vector3(-10, 40, 40),
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(60, -60, 0),
new THREE.Vector3(70, 0, 80)
]
const curve = new THREE.CatmullRomCurve3(arr); // 三维样条曲线
const pointsArr = curve.getPoints(100); // 从曲线上获取点
const arr2 = [
new THREE.Vector2(-100, 0),
new THREE.Vector2(0, 30),
new THREE.Vector2(100, 0),
]
const curve2 = new THREE.SplineCurve(arr2); // 二维样条曲线
const pointsArr2 = curve2.getPoints(60);
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 3000);
const renderer = new THREE.WebGLRenderer({ antialias: true });
const geometry = new THREE.BufferGeometry();
const material = new THREE.LineBasicMaterial({
color: 0xff0000
});
const line = new THREE.LineLoop(geometry, material);
// 点模型可视化曲线经过的点
const geometry2 = new THREE.BufferGeometry();
const material2 = new THREE.PointsMaterial({
color: 0xff00ff,
size: 10
})
const point = new THREE.Points(geometry2, material2);
// 点模型可视化曲线经过的点
const geometry3 = new THREE.BufferGeometry();
const material3 = new THREE.PointsMaterial({
color: 0x1ad4f5,
size: 3
})
const line2 = new THREE.Points(geometry3, material3);
const axesHelper = new THREE.AxesHelper(150);
const ambientLight = new THREE.AmbientLight(0x404040, 2); // 添加环境光
const directionalLight = new THREE.DirectionalLight(0xffffff, 1); // 添加方向光
const pointLight = new THREE.PointLight(0xff0000, 2, 10); // 添加点光源,红色的点光源
const controls = new OrbitControls(camera, renderer.domElement); // 添加轨道控制器
geometry.setFromPoints(pointsArr); // 读取坐标数据赋值给几何体顶点
geometry2.setFromPoints(arr); // 读取样条曲线经过的顶点位置
geometry3.setFromPoints(pointsArr2);
directionalLight.position.set(5, 5, 5).normalize();
directionalLight.castShadow = true; // 方向光投射阴影
pointLight.position.set(2, 2, 2);
pointLight.castShadow = true; // 点光源投射阴影
controls.enableDamping = true; // 启用阻尼效果
controls.dampingFactor = 0.05; // 阻尼系数
scene.add(line2);
scene.add(point);
scene.add(line);
scene.add(axesHelper);
scene.add(directionalLight);
scene.add(ambientLight);
scene.add(pointLight);
// 设置相机位置
camera.position.set(300, 100, 600);
camera.lookAt(line.position);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.shadowMap.enabled = true; // 启用阴影
renderer.setClearColor(0x444444, 1);
document.body.appendChild(renderer.domElement);
// 动画循环
const animate = () => {
requestAnimationFrame(animate);
// 渲染场景
renderer.render(scene, camera);
};
// 启动动画
animate();
// 窗口大小调整时更新相机和渲染器
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
{
"dependencies": {
"three": "0.173.0"
}
}
预览