three.js 01-05 之使用 dat.GUI 库简化试验

来源:互联网 发布:后拱辰享雪面霜知乎 编辑:程序博客网 时间:2024/05/22 00:23

    在上一篇文中,我们已经学习了如何利用 Three.js 实现简单的动画,譬如旋转我们的立方体、让我们的球体弹跳等。但是,如果我想控制一下立方体的旋转速度,或者调整一下球体的弹跳速度,该怎么做呢?这个时候,相信大部分学者都会很自然的想到传统的方式,即添加几个 html 元素,然后编写相关的 JavaScript 以达到试验目的···

    当然,那样做自然是没什么问题。但是,本文将介绍一种更便捷的方法来达到更好的试验目的。即通过引入一个叫 dat.GUI 的库,据说这个库是由 Google 的几个牛人创建的,具体文档可以访问 http://code.google.com/p/dat-gui/ 进一步的了解。本文将通过使用 dat.GUI 这个库添加一个用户界面,使得我们可以:

  • 控制立方体的旋转速度
  • 控制球体的弹跳速度

    首先,我们把上一篇的试验文件另存为“chapter01-05-data-gui.html”,并在 <header> 头部引入 dat.gui.min.js,这个文件位于 build/js/libs/dat.gui.min.js 中,接下来我们要定义一个 JavaScript 对象,用来保存哪些我们想要通过 dat.GUI 库来修改的变量,如下所示:

/** 用来保存那些需要修改的变量 */var guiControls = new function() {    this.rotationSpeed = 0.02;    this.bouncingSpeed = 0.04;}
在这个 guiControls 对象里,我们定义了两个属性 this.rotationSpeed 和 this.bouncingSpeed 以及他们的默认值。接下来,我们把这个对象传递给 data.GUI 对象,并定义这两个属性的取值范围,代码如下:
/** 定义 dat.GUI 对象,并绑定 guiControls 的两个属性 */var gui = new dat.GUI();gui.add(guiControls, 'rotationSpeed', 0, 0.5);gui.add(guiControls, 'bouncingSpeed', 0, 0.5);
此处,我们规定了 rotationSpeed 和 bouncingSpeed 两个属性的取值范围都是从 0 到 0.5,现在我们要做的就是将 rotateCube() 方法中的常量 0.02 替换成 guiControls.rotationSpeed,并将 bounceSphere() 方法中的常量 0.04 替换成 guiControls.bouncingSpeed。

    此时,我们再通过浏览器观察我们的场景,你将在右上角看到一个简单的用户界面,通过它就可以控制物体的旋转速度和弹跳速度了。最后完成的代码如下:

<!DOCTYPE html><html><head>    <title>示例 01.05 - 使用 data.GUI 库简化试验</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 controls;var stats;var cube;var sphere;    // 当所有元素加载完毕后,就执行我们 Three.js 相关的东西    $(function() {stats = initStats();scene = new THREE.Scene();camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000); // 2147483647camera.position.set(-30, 40, 30);camera.lookAt(scene.position);render = new THREE.WebGLRenderer( {antialias: true} ); // antialias 抗锯齿render.setSize(window.innerWidth, window.innerHeight);render.setClearColor(0xEEEEEE);render.shadowMap.enabled = true; // 允许阴影投射controls = new THREE.OrbitControls(camera, render.domElement);scene.add(new THREE.AxisHelper(20));// 加入坐标轴// 加入一个平面var planeGeometry = new THREE.PlaneGeometry(60, 20, 1, 1);var planeMaterial = new THREE.MeshLambertMaterial( {color: 0xFFFFFF} );var plane = new THREE.Mesh(planeGeometry, planeMaterial);plane.rotation.x = -0.5 * Math.PI; // 沿着 X轴旋转-90°plane.position.x = 15; // 沿着 x轴右移 15个单位plane.position.y = 0; // y轴为 0plane.position.z = 0; // z轴为 0plane.receiveShadow = true; // 几何平面接收阴影scene.add(plane);// 加入一个立方体var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);var cubeMaterial = new THREE.MeshLambertMaterial( {color: 0xFF0000} );cube = new THREE.Mesh(cubeGeometry, cubeMaterial);cube.position.x = -4;cube.position.y = 3;cube.position.z = 0;cube.castShadow = true; // 立方体投射阴影scene.add(cube);// 加入一个球体var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);var sphereMaterial = new THREE.MeshLambertMaterial( {color: 0x7777FF} );sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);sphere.position.x = 20;sphere.position.y = 4;sphere.position.z = 2;sphere.castShadow = true; // 球体投射阴影scene.add(sphere);// 加入一个光源// 注:基础材质 MeshBasicMaterial 不会对光源产生反应,因此要改用 MeshLambertMaterial 或 MeshPhongMaterial 材质才有效果var spotLight = new THREE.SpotLight( 0xFFFFFF);spotLight.position.set(-40, 60, -10);spotLight.castShadow = true; // 光源产生阴影spotLight.shadow.mapSize.width = 1024; // 必须是 2的幂,默认值为 512spotLight.shadow.mapSize.height = 1024; // 必须是 2的幂,默认值为 512scene.add(spotLight);$('#WebGL-output')[0].appendChild(render.domElement);renderScene();    });/** 渲染场景 */function renderScene() {stats.update();rotateCube(); // 旋转立方体bounceSphere() // 弹跳球体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 rotateCube() {cube.rotation.x += guiControls.rotationSpeed;cube.rotation.y += guiControls.rotationSpeed;cube.rotation.z += guiControls.rotationSpeed;}/** 弹跳球体 */var step = 0;function bounceSphere() {step += guiControls.bouncingSpeed;sphere.position.x = 20 + (10 * Math.cos(step));sphere.position.y = 2 + (10 * Math.abs(Math.sin(step)));console.log('step=' + step + ', x=' + 10 * Math.cos(step) + ', y=' + 10 * Math.abs(Math.sin(step)));}/** 用来保存那些需要修改的变量 */var guiControls = new function() {this.rotationSpeed = 0.02;this.bouncingSpeed = 0.04;}/** 定义 dat.GUI 对象,并绑定 guiControls 的两个属性 */var gui = new dat.GUI();gui.add(guiControls, 'rotationSpeed', 0, 0.5);gui.add(guiControls, 'bouncingSpeed', 0, 0.5);</script></body></html>
未完待续···

原创粉丝点击