three.js环境及使用教程

开发环境

bash 复制代码
npm i [email protected]
npm i @types/[email protected]

入门代码

index.html

xml 复制代码
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>Document</title>
		<!-- <link rel="stylesheet" href="demo.css" /> -->
		<style>
			body {
				margin: 0;
			}
		</style>
		<!-- 核心依赖 -->
		<script
			async
			src="https://unpkg.com/[email protected]/dist/es-module-shims.js"
		></script>
		<script type="importmap">
			{
				"imports": {
					"three": "../../node_modules/three/build/three.module.js"
				}
			}
		</script>
	</head>
	<body>
		<script type="module" src="demo.js"></script>
	</body>
</html>

demo.js

js 复制代码
import * as THREE from "three";

//! 场景
const scene = new THREE.Scene();

//! 透视相机(垂直视野角度,长宽比,近端面,远端面)
const camera = new THREE.PerspectiveCamera(
	75,
	window.innerWidth / window.innerHeight,
	0.1,
	1000
);
camera.position.z = 5;

//! 渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
window.onresize = function () {
	renderer.setSize(window.innerWidth, window.innerHeight);
	//
	camera.setViewOffset(
		window.innerWidth,
		window.innerHeight,
		0,
		0,
		window.innerWidth,
		window.innerHeight
	);
};

//! 线
{
	// 定义材质
	const material = new THREE.LineBasicMaterial({ color: 0x00ff00 });
	// 创建几何体
	const points = [];
	points.push(new THREE.Vector3(-3, 0, 0));
	points.push(new THREE.Vector3(0, 3, 0));
	points.push(new THREE.Vector3(3, 0, 0));
	points.push(new THREE.Vector3(0, -3, 0));
	points.push(new THREE.Vector3(-3, 0, 0));
	const geometry = new THREE.BufferGeometry().setFromPoints(points);
	// 创建 Line
	const line = new THREE.Line(geometry, material);
	// 添加到场景
	scene.add(line);
}

//! 立方体
{
	// 定义材质
	const material = new THREE.MeshBasicMaterial({ color: 0x0ffff0 });
	// 几何对象
	const geometry = new THREE.BoxGeometry(1, 1, 1);
	// 创建 Cube
	const cube = new THREE.Mesh(geometry, material);
	// 添加到场景
	scene.add(cube);
	// 动画效果
	function animate() {
		// 向浏览器发起一个执行某函数的请求(一般默认保持60FPS的频率)
		requestAnimationFrame(animate);
		// 旋转 Cube
		cube.rotation.x += 0.01;
		cube.rotation.y += 0.01;
		// 旋转 Camera
		camera.rotation.z += 0.01;
		// 刷新相机
		renderer.render(scene, camera);
	}
	animate();
}

摄像机(Camer)

TODO

材质(Mesh)

TODO

图层(Layers)

TODO

基本几何图形

TODO

加载3D模型

TODO

相关推荐
玩大数据的龙威2 分钟前
【ArcGIS技巧】—村庄规划规划用地规划状态字段生成工具
arcgis
Biomamba生信基地22 分钟前
R语言基础| 下载、安装
开发语言·r语言·生信·医药
姜君竹23 分钟前
QT的工程文件.pro文件
开发语言·c++·qt·系统架构
奇树谦28 分钟前
使用VTK还是OpenGL集成到qt程序里哪个好?
开发语言·qt
VBA633739 分钟前
VBA之Word应用第三章第十节:文档Document对象的方法(三)
开发语言
老胖闲聊1 小时前
Python Rio 【图像处理】库简介
开发语言·图像处理·python
拉不动的猪1 小时前
安卓和ios小程序开发中的兼容性问题举例
前端·javascript·面试
码界奇点1 小时前
Python Flask文件处理与异常处理实战指南
开发语言·python·自然语言处理·flask·python3.11
贩卖纯净水.1 小时前
浏览器兼容-polyfill-本地服务-优化
开发语言·前端·javascript
k要开心1 小时前
C++概念以及基础框架语法
开发语言·c++