本篇我们来介绍 CircleGeometry 几何体,其常用属性如下表所示:
属性 | 描述 |
---|---|
radius (半径) | 必选。此属性指定圆的半径 |
segments (分段) | 可选。此属性定义创建圆所用的面数量。最少3个,默认为8个。值越大,圆就越光滑 |
thetaStart (起始角) | 可选。此属性定义从哪儿开始画圆。取值范围 0~2*π |
thetaLength (角度) | 可选。此属性定义圆画多大。默认为 2*π(整圆)。配合 thetaStart 一起使用。取值范围 0~2*π |
<!DOCTYPE html>
<html>
<head>
<title>示例 05.02 - CircleGeometry</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 cube;
//var sphere;
var plane;
var meshMaterial;
var ambientLight;
var spotLight;
//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, 30, 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);
ambientLight = new THREE.AmbientLight(0x0c0c0c);
scene.add(ambientLight);
spotLight = new THREE.SpotLight(0xffffff);
spotLight.position.set(0, 30, 60);
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.MeshBasicMaterial({color: 0x777777});
ground = new THREE.Mesh(groundGeometry, groundMaterial);
ground.rotation.set(-0.5 * Math.PI, 0, 0); // 沿着 X轴旋转-90°
scene.add(ground);
// 定义 circle
var circleGeometry;
// 材质
meshMaterial = [
new THREE.MeshNormalMaterial({side: THREE.DoubleSide}),
new THREE.MeshBasicMaterial({wireframe: true})
];
/** 用来保存那些需要修改的变量 */
guiParams = new function() {
this.rotationSpeed = 0.02;
this.radius = 17.8;
this.segments = 11;
this.thetaStart = 1.1;
this.thetaLength = 4.7;
this.update = function() {
scene.remove(plane);
circleGeometry = new THREE.CircleGeometry(this.radius, this.segments, this.thetaStart, this.thetaLength);
plane = new THREE.SceneUtils.createMultiMaterialObject(circleGeometry, meshMaterial);
plane.castShadow = true;
plane.position.set(0, 25, 0);
scene.add(plane);
}
}
/** 定义 dat.GUI 对象,并绑定 guiParams 的几个属性 */
var gui = new dat.GUI();
var folder = gui.addFolder('Mesh');
folder.open();
folder.add(guiParams, 'radius', 1, 50).onChange(function(e) {
guiParams.update();
});
folder.add(guiParams, 'segments', 1, 50).onChange(function(e) {
guiParams.update();
});
folder.add(guiParams, 'thetaStart', 0, 2 * Math.PI).onChange(function(e) {
guiParams.update();
});
folder.add(guiParams, 'thetaLength', 0, 2 * Math.PI).onChange(function(e) {
guiParams.update();
});
guiParams.update();
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 instanceof THREE.Mesh && mesh != ground) {
//mesh.rotation.x = step;
mesh.rotation.y = step;
//mesh.rotation.z = step;
}
});
}
</script>
</body>
</html>