前端3D·Three.js一学就会系列:第二 画线

各位前端伙伴们,大家好,我是阿峰。最近开始入坑前端3D建站,跟大家一起慢慢深入three.js做网站3D。

今天给大家讲下three.js 画线


一、省略部分

官网,介绍 ,以及引入库,参看文章片头系列文章:01 第一个3D网站

二、使用方法

创建一个场景

csharp 复制代码
const scene = new THREE.Scene();

创建一个透视摄像机

csharp 复制代码
const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
camera.position.set( 0, 0, 100 );
camera.lookAt( 0, 0, 0 );

知识点: camera.position.set():三个参数固定透视摄像机的位置 camera.lookAt():三个参数固定透视摄像机的拍摄方向

将渲染器添加到页面上

csharp 复制代码
const renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

创建一个线条

csharp 复制代码
const points = [];
points.push(new THREE.Vector3( - 10, 0, 0 ) );
points.push( new THREE.Vector3( 0, 10, 0 ) );
points.push( new THREE.Vector3( 10, 0, 0 ) );
const geometry = new THREE.BufferGeometry().setFromPoints( points );
const material = new THREE.LineBasicMaterial( { color: 0x0000ff } );

const line = new THREE.Line( geometry, material );
scene.add( line );

知识点: Vector3:三维向量x、y和z 代表位置 BufferGeometry: 是面片、线或点几何体的有效表述 setFromPoints:设置数据来源 LineBasicMaterial:线条材质:可定义属性 color颜色,linewidth线宽等参考LineBasicMaterial 【扩展】 LineDashedMaterial:与LineBasicMaterial同样是线条材质:可定义属性 color颜色,linewidth线宽等参考LineDashedMaterial

渲染场景

csharp 复制代码
function animate() {
	requestAnimationFrame( animate );
	renderer.render( scene, camera );
}
animate();

requestAnimationFrame有很多的优点。最重要的一点或许就是当用户切换到其它的标签页时,它会暂停,因此不会浪费用户宝贵的处理器资源,也不会损耗电池的使用寿命。

线条动起来

csharp 复制代码
function animate() {
	requestAnimationFrame( animate );
	// 旋转方向,及大小
	cube.rotation.x += 0.01;
	cube.rotation.y += 0.01;

	renderer.render( scene, camera );
};

animate();

完整代码(实例)

javascript 复制代码
<html>
	<head>
		<meta charset="utf-8">
		<title>My first three.js app</title>
		<style>
			body { margin: 0; }
		</style>
	</head>
	<body>
		<script src="./three.js"></script>
		<!-- <script src="https://threejs.org/build/three.js"></script> -->
		<script>
			// 创建一个场景
			const scene = new THREE.Scene();
			const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 );
			camera.position.set( 0, 0, 100 );
			camera.lookAt( 0, 0, 0 );

			// 展示
			const renderer = new THREE.WebGLRenderer();
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );
			// 创建一条线
			const points = [];
			points.push( new THREE.Vector3( - 10, 0, 0 ) );
			points.push( new THREE.Vector3( 0, 10, 0 ) );
			points.push( new THREE.Vector3( 10, 0, 0 ) );
			const geometry = new THREE.BufferGeometry().setFromPoints( points );
			const material = new THREE.LineBasicMaterial( { color: 0x0000ff } );

			const line = new THREE.Line( geometry, material );
			scene.add( line );

			function animate() {
				requestAnimationFrame( animate );

				line.rotation.x += 0.01;
				line.rotation.y += 0.01;

				renderer.render( scene, camera );
			};

			animate();
		</script>
	</body>
</html>

效果

总结

以上就是今天要讲的内容,本文仅仅简单介绍了three.js的使用,而three.js提供了非常多的3D显示功能,后续文章,我将带大家慢慢深入了解。


如果这篇这篇文章对您有帮助?关注、点赞、收藏 ,三连支持一下。 有疑问或想法?评论区见。 我们下期再见。

相关推荐
M ? A5 小时前
你的 Vue 3 响应式状态,VuReact 如何生成 React Hooks 依赖数组?
前端·javascript·vue.js·经验分享·react.js·面试·vureact
FlyWIHTSKY5 小时前
HTML 中 `<span>` 和 `<div>` 详细对比
前端·html
competes5 小时前
React.js JavaScript前端技术脚本运行框架。程序员进行研发组项目现场工作落地的一瞬之间适应性恒强说明可塑性强度达到应用架构师的考核标准
前端·javascript·人工智能·react.js·java-ee·ecmascript
2401_832635585 小时前
踩坑分享IntelliJ IDEA 打包 Web 项目 WAR 包(含 Tomcat 部署 + 常见问题解决)
前端·tomcat·intellij-idea
Evavava啊6 小时前
Android WebView 中 React useState 更新失效问题
android·前端·react.js·渲染
恋恋风尘hhh6 小时前
Web 端请求签名机制分析:以小红书(XiaoHongShu)x-s 参数为例
前端
包子源6 小时前
React-PDF 与 Web 预览「像素级」对齐实践
前端·react.js·pdf
jiayong236 小时前
第 25 课:给学习笔记页加上搜索、标签筛选和 URL 同步
开发语言·前端·javascript·vue.js·学习
UXbot6 小时前
如何用 AI 快速生成完整的移动端 UI 界面:从描述到交付的实操教程
前端·ui·交互·ai编程·原型模式
南囝coding6 小时前
零成本打造专业域名邮箱:Cloudflare + Gmail 终极配置保姆级全攻略
前端·后端