Three 学习日志(十)—— 常见几何体(长方体、球体、圆柱、矩形平面、圆形平面)
一、构建常用几何体
const geometry_list = []
const geometry_box = new THREE.BoxGeometry(100, 100, 100);
geometry_list.push(geometry_box);
const geometry_sphere = new THREE.SphereGeometry(50);
geometry_list.push(geometry_sphere);
const geometry_cylinder = new THREE.CylinderGeometry(50, 50, 100);
geometry_list.push(geometry_cylinder);
const geometry_plane = new THREE.PlaneGeometry(100, 50);
geometry_list.push(geometry_plane);
const geometry_circle = new THREE.CircleGeometry(50);
geometry_list.push(geometry_circle);
const material = new THREE.MeshLambertMaterial({
color: 0x00ffff,
transparent: true,
opacity: 0.5,
side: THREE.DoubleSide,
});
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
二、 遍历加入场景中
for (const gIdx in geometry_list) {
const mesh = new THREE.Mesh(geometry_list[gIdx], material);
mesh.position.set(gIdx * 150, 0, 0);
scene.add(mesh);
}
三、效果展示
四、完整代码
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Learn Threetitle>
<script src="../build/three.js">script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "../examples/jsm/"
}
}
script>
head>
<body>
<script type="module">
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
const scene = new THREE.Scene();
const axesHelper = new THREE.AxesHelper(150);
scene.add(axesHelper);
const geometry_list = []
const geometry_box = new THREE.BoxGeometry(100, 100, 100);
geometry_list.push(geometry_box);
const geometry_sphere = new THREE.SphereGeometry(50);
geometry_list.push(geometry_sphere);
const geometry_cylinder = new THREE.CylinderGeometry(50, 50, 100);
geometry_list.push(geometry_cylinder);
const geometry_plane = new THREE.PlaneGeometry(100, 50);
geometry_list.push(geometry_plane);
const geometry_circle = new THREE.CircleGeometry(50);
geometry_list.push(geometry_circle);
const material = new THREE.MeshLambertMaterial({
color: 0x00ffff,
transparent: true,
opacity: 0.5,
side: THREE.DoubleSide,
});
for (const gIdx in geometry_list) {
const mesh = new THREE.Mesh(geometry_list[gIdx], material);
mesh.position.set(gIdx * 150, 0, 0);
scene.add(mesh);
}
const ambient = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambient);
const camera = new THREE.PerspectiveCamera();
camera.position.set(800, 800, 800);
camera.lookAt(200, 0, 0);
const width = window.innerWidth;
const height = window.innerHeight;
const renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
renderer.render(scene, camera);
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', function () {
renderer.render(scene, camera);
});
script>
body>
<style>
body {
overflow: hidden;
margin: 0px;
}
style>
html>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90