three.js几何体之八面体

位置: 首页 > 前端三套件
[发布: 2025.5.16  作者: 马黑  阅读: 6]

八面缓冲几何体(OctahedronGeometry)是一个用于生成八面体的类。构造器:

OctahedronGeometry(radius : Float, detail : Integer)

其中:

radius — 八面体的半径,默认值为 1
detail — 默认值为0,将这个值设为一个大于0的数将会为它增加一些顶点,使其不再是一个八面体

以下代码演示各面颜色为道奇蓝的八面体在光源作用下呈现出各面明暗不同的效果(平行光光源从xyz轴的右上前出发):

<script type="module">
	/* 八面缓冲几何体 OctahedronGeometry */
	import * as THREE from 'https://esm.sh/three';

	const scene = new THREE.Scene;
	const camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
	camera.position.set(0, 0, 10);
	const renderer = new THREE.WebGLRenderer({ antialias: true });
	renderer.setSize(window.innerWidth, window.innerHeight);
	document.body.appendChild(renderer.domElement);

	const geometry = new THREE.OctahedronGeometry(1.2, 0); // 八面体
	const material = new THREE.MeshLambertMaterial({ color: 0x1e90ff }); // 朗伯特材质
	const ico = new THREE.Mesh(geometry, material); // 创建网格Mess对象(图像=几何体+材质)
	scene.add(ico); // 图像添加到场景成形
	
	const ambientLight = new THREE.AmbientLight(0xffeeee, 1); // 环境光 : 改善场景视觉
	scene.add(ambientLight);
	
	const dirLight = new THREE.DirectionalLight(0xffd700, 2.5); // 平行光(方向光) : 角度照亮
	dirLight.position.set(20,20,20); // 光来自右上靠近观者
	dirLight.target = ico; // 光作用的对象 : 八面体图像
	scene.add(dirLight);

	const animate = () => {
		requestAnimationFrame(animate);
		ico.rotation.x += 0.01;
		ico.rotation.y += 0.01;
		renderer.render(scene, camera);
	};

	window.onresize = () => renderer.setSize(window.innerWidth, window.innerHeight);

	animate();

</script>

前一篇: three.js几何体之二十面体
下一篇: three.js几何体之四面体

发表评论:

  
 

评论列表 [0条]

Copyright © 2023 All Right Reserved 马黑PHP文章管理整站系统v1.8
联系我们: gxblk@163.com