three.js几何体之二十面体
二十面缓冲几何体(IcosahedronGeometry)是一个用于生成二十面体的类。构造器:
IcosahedronGeometry(radius : Float, detail : Integer)
其中:
radius — 二十面体的半径,默认为 1
detail — 默认值为0。将这个值设为一个大于0的数将会为它增加一些顶点,使其不再是一个二十面体。当这个值大于1的时候,实际上它将变成一个球体
下面的代码,我们创建一个二十面几何体;材质是 MeshLambertMaterial,朗伯特材质,该材质光源才能呈现,我们打了两种光,一是环境光,二是方向光。本示例没有引入相机轨道控制器,只能观看:
<script type="module"> /* 二十面缓冲几何体 IcosahedronGeometry */ 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, -1, 10); const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); const geometry = new THREE.IcosahedronGeometry(1.2, 0); // 创建二十面体 const material = new THREE.MeshLambertMaterial({ color: 0x90ee90 }); // 创建朗伯特材质 const ico = new THREE.Mesh(geometry, material); // 创建网格Mess对象(图像=几何体+材质) scene.add(ico); // 图像添加到场景成形 const ambientLight = new THREE.AmbientLight(0xffeeee, 0.75); // 环境光 : 改善场景视觉 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条]