本篇介绍 three.js 中的 LatheGeometry 扫描几何体。它是从一条光滑的曲线开始创建图形,这条曲线是通过指定一些点(也叫节点)来定义的,而这条曲线通常也被称作样条曲线。当这条曲线围绕一个固定点选择时,就构成了一个类似花瓶或铃铛的图形。关于 LatheGeometry 的几个常用属性,如下表所示:
属性 | 描述 |
---|---|
points | 必选。此属性定义构成样条曲线的点,然后基于这条曲线生成类似花瓶或铃铛的图形 |
segments | 可选。此属性定义构成图形时所需的分段数。默认值是 12 |
phiStart | 可选。此属性定义构成图形时从圆的何处开始。取值范围是 0 到 2π。默认值是 0 |
phiLength | 可选。此属性定义构成图形时有多完整。例如四分之一圆 0.5π。默认值是 2π(整圆 360°) |
先来看看完整的示例,代码如下所示:
<!DOCTYPE html>
<html>
<head>
<title>示例 06.02 - LatheGeometry</title>
<script src="../build/three.js"></script>
<script src="../build/js/controls/OrbitControls.js"></script>
<script src="../build/js/libs/stats.min.js"></script>
<script src="../build/js/libs/dat.gui.min.js"></script>
<script src="../jquery/jquery-3.2.1.min.js"></script>
<style>
body {
/* 设置 margin 为 0,并且 overflow 为 hidden,来完成页面样式 */
margin: 0;
overflow: hidden;
}
/* 统计对象的样式 */
#Stats-output {
position: absolute;
left: 0px;
top: 0px;
}
</style>
</head>
<body>
<!-- 用于 WebGL 输出的 Div -->
<div id="webgl-output"></div>
<!-- 用于统计 FPS 输出的 Div -->
<div id="stats-output"></div>
<!-- 运行 Three.js 示例的 Javascript 代码 -->
<script type="text/javascript">
var scene;
var camera;
var render;
var webglRender;
//var canvasRender;
var controls;
var stats;
var guiParams;
var ground;
var lathe;
var spGroup;
var meshMaterial;
var ambientLight;
var spotLight;
var axesHelper;
//var cameraHelper;
$(function() {
stats = initStats();
scene = new THREE.Scene();
webglRender = new THREE.WebGLRenderer( {antialias: true, alpha: true} ); // antialias 抗锯齿
webglRender.setSize(window.innerWidth, window.innerHeight);
webglRender.setClearColor(0xeeeeee, 1.0);
webglRender.shadowMap.enabled = true; // 允许阴影投射
render = webglRender;
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.01, 1000); // 2147483647
camera.position.set(-45.5, 68.2, 90.9);
var target = new THREE.Vector3(10, 0 , 0);
controls = new THREE.OrbitControls(camera, render.domElement);
controls.target = target;
camera.lookAt(target);
$('#webgl-output')[0].appendChild(render.domElement);
window.addEventListener('resize', onWindowResize, false);
// 加入一个坐标轴:X(橙色)、Y(绿色)、Z(蓝色)
axesHelper = new THREE.AxesHelper(60);
scene.add(axesHelper);
ambientLight = new THREE.AmbientLight(0x0c0c0c);
scene.add(ambientLight);
spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(0, 60, 30);
spotLight.shadow.mapSize.width = 5120; // 必须是 2的幂,默认值为 512
spotLight.shadow.mapSize.height = 5120; // 必须是 2的幂,默认值为 512
spotLight.castShadow = true;
scene.add(spotLight);
//cameraHelper = new THREE.CameraHelper(spotLight.shadow.camera);
//scene.add(cameraHelper);
// 加入一个地面
var groundGeometry = new THREE.PlaneGeometry(100, 100, 4, 4);
var groundMaterial = new THREE.MeshLambertMaterial({color: 0x777777}); // MeshBasicMaterial 材质不能接收阴影
ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.set(-0.5 * Math.PI, 0, 0); // 沿着 X轴旋转-90°
ground.receiveShadow = true;
scene.add(ground);
// 材质
meshMaterial = [
new THREE.MeshNormalMaterial({side: THREE.DoubleSide}),
new THREE.MeshBasicMaterial({wireframe: true})
];
/** 用来保存那些需要修改的变量 */
guiParams = new function() {
this.rotationSpeed = 0.02;
this.segments = 20;
this.phiStart = 0;
this.phiLength = 3.3;
}
/** 定义 dat.GUI 对象,并绑定 guiParams 的几个属性 */
var gui = new dat.GUI();
gui.add(guiParams, 'segments', 1, 50, 1).onChange( function(e) {updateMesh()} );
gui.add(guiParams, 'phiStart', 0, 2 * Math.PI, 0.01).onChange( function(e) {updateMesh()} );
gui.add(guiParams, 'phiLength', 0, 2 * Math.PI, 0.01).onChange( function(e) {updateMesh()} );
updateMesh();
renderScene();
});
/** 渲染场景 */
function renderScene() {
stats.update();
rotateMesh(); // 旋转物体
requestAnimationFrame(renderScene);
render.render(scene, camera);
}
/** 初始化 stats 统计对象 */
function initStats() {
stats = new Stats();
stats.setMode(0); // 0 为监测 FPS;1 为监测渲染时间
$('#stats-output').append(stats.domElement);
return stats;
}
/** 当浏览器窗口大小变化时触发 */
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
render.setSize(window.innerWidth, window.innerHeight);
}
/** 旋转物体 */
var step = 0;
function rotateMesh() {
step += guiParams.rotationSpeed;
scene.traverse(function(mesh) {
if (mesh === spGroup) {
//mesh.rotation.x = step;
mesh.rotation.y = step;
//mesh.rotation.z = step;
}
});
}
function updateMesh() {
scene.remove(spGroup);
// 定义 lathe
var points = generatePoints();
var latheGeometry = new THREE.LatheGeometry(points, guiParams.segments, guiParams.phiStart, guiParams.phiLength);
lathe = new THREE.SceneUtils.createMultiMaterialObject(latheGeometry, meshMaterial);
lathe.children[1].castShadow = true; // lathe.children[0] 的材质是 MeshNormalMaterial,不能投射阴影
spGroup.add(lathe);
scene.add(spGroup);
}
/** 随机生成 30 个点 */
function generatePoints() {
var points = [];
var height = 5;
var count = 30;
for (var i = 0; i < count; i++) {
var randomX = (Math.sin(i * 0.2) + Math.cos(i * 0.3)) * height + 12;
var randomY = i + 5;
points.push(new THREE.Vector2(randomX, randomY));
}
spGroup = new THREE.Object3D();
var material = new THREE.MeshBasicMaterial( {color: 0xff0000, transparent: false} );
// 添加 30 个小球体
points.forEach(function(point) {
var spGeometry = new THREE.SphereGeometry(0.2);
var spMesh = new THREE.Mesh(spGeometry, material);
spMesh.position.set(point.x, point.y, 0);
spGroup.add(spMesh);
});
return points;
}
</script>
</body>
</html>
在示例中的 generatePoints() 函数里,我们生成了 30 个点,这些点的 x 坐标是由正弦函数和余弦函数结果的组合算出来的,y 坐标则是基于变量 i 算得。跟上一篇介绍 ConvexGeometry 凸面体示例一样,为了更好的展示这些点,我们添加了相应的红色小球体。