日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Three.js中引入dat.gui库实现界面组件控制动画速度变量

發布時間:2025/3/19 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Three.js中引入dat.gui库实现界面组件控制动画速度变量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

Three.js中使用requestAnimationFrame方法實現立方體轉動和小球跳動的動畫:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/119390804

在上面的基礎上,引入dat.GUI的庫,能很容易就創建出一個簡單的界面組件

用以修改代碼中的變量。

比如我們用其控制動畫的速度,實現效果如下

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

首先下載庫dat-gui.js

https://download.csdn.net/download/BADAO_LIUMANG_QIZHI/20706490

將其引入到html中

??? <script type="text/javascript" src="./js/dat.gui.js"></script>

然后新建界面并設置兩個變量

??????? var controls = new function () {this.rotationSpeed = 0.02;this.bouncingSpeed = 0.03;};var gui = new dat.GUI();gui.add(controls, 'rotationSpeed', 0, 0.5);gui.add(controls, 'bouncingSpeed', 0, 0.5);

然后再實現動畫的速度控制的變量改為這兩個變量

??????????? cube.rotation.x += controls.rotationSpeed;cube.rotation.y += controls.rotationSpeed;cube.rotation.z += controls.rotationSpeed;

以及

??????????? step += controls.bouncingSpeed;sphere.position.x = 20 + ( 10 * (Math.cos(step)));sphere.position.y = 2 + ( 10 * Math.abs(Math.sin(step)));

完整示例代碼

<!DOCTYPE html><html><head><title>dat.gui的使用</title><script type="text/javascript" src="./js/three.js"></script><script type="text/javascript" src="./js/dat.gui.js"></script><style>body {/* 將邊距設置為0,溢出設置為隱藏,以實現全屏顯示 */margin: 0;overflow: hidden;}</style> </head> <body><!-- 顯示的div --> <div id="WebGL-output"> </div><script type="text/javascript">// 初始化的方法function init() {// 創建一個場景,它將包含我們所有的元素,如物體,攝像機和燈光var scene = new THREE.Scene();// 創建一個相機,它定義了我們正在看的地方var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);// 創建渲染器并設置大小var renderer = new THREE.WebGLRenderer();//將renderer的背景色設置為接近白色renderer.setClearColorHex();renderer.setClearColor(new THREE.Color(0xEEEEEE));//設置大小renderer.setSize(window.innerWidth, window.innerHeight);// 創建平面,并定義平面的尺寸var planeGeometry = new THREE.PlaneGeometry(60, 20);//創建一個基本材質,并設置顏色var planeMaterial = new THREE.MeshBasicMaterial({color: 0xcccccc});//把兩個對象合并到Mesh網格對象var plane = new THREE.Mesh(planeGeometry, planeMaterial);// 設置平面繞x軸旋轉90度plane.rotation.x = -0.5 * Math.PI;// 設置平面的坐標位置plane.position.x = 15;plane.position.y = 0;plane.position.z = 0;// 將平面添加進場景scene.add(plane);// 創建一個立方體var cubeGeometry = new THREE.BoxGeometry(4, 4, 4);//設置材質//把線框屬性wireframe設置為truevar cubeMaterial = new THREE.MeshBasicMaterial({color: 0xff0000, wireframe: true});//合并var cube = new THREE.Mesh(cubeGeometry, cubeMaterial);// 設置立方體坐標cube.position.x = 20;cube.position.y = 12;cube.position.z = 2;// 將立方體添加進場景scene.add(cube);// 創建一個球體var sphereGeometry = new THREE.SphereGeometry(4, 20, 20);//把線框屬性wireframe設置為truevar sphereMaterial = new THREE.MeshBasicMaterial({color: 0x7777ff, wireframe: true});var sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);// 設置球體的坐標sphere.position.x = 20;sphere.position.y = 4;sphere.position.z = 2;// 添加進場景scene.add(sphere);// 定義相機的坐標,即懸掛在場景的上方camera.position.x = -30;camera.position.y = 40;camera.position.z = 30;//為了確保相機能夠拍攝到這些物體,使用lookat函數指向場景的中心camera.lookAt(scene.position);// 將renderer的輸出掛接到HTML終點div元素document.getElementById("WebGL-output").appendChild(renderer.domElement);// 渲染場景var step = 0;var controls = new function () {this.rotationSpeed = 0.02;this.bouncingSpeed = 0.03;};var gui = new dat.GUI();gui.add(controls, 'rotationSpeed', 0, 0.5);gui.add(controls, 'bouncingSpeed', 0, 0.5);renderScene();function renderScene() {// 方塊繞坐標軸旋轉//在每個坐標軸的rotation 屬性上增加0.02,其效果就是//使得這個方塊繞所有的軸旋轉cube.rotation.x += controls.rotationSpeed;cube.rotation.y += controls.rotationSpeed;cube.rotation.z += controls.rotationSpeed;// 修改球體的坐標使其按照一條曲線跳躍//要修改在x軸上的position 和y 軸上的positon//定義彈跳的速度step += controls.bouncingSpeed;sphere.position.x = 20 + ( 10 * (Math.cos(step)));sphere.position.y = 2 + ( 10 * Math.abs(Math.sin(step)));// 使用requestAnimationFrame定時渲染場景requestAnimationFrame(renderScene);renderer.render(scene, camera);}}window.onload = init;</script> </body> </html>

總結

以上是生活随笔為你收集整理的Three.js中引入dat.gui库实现界面组件控制动画速度变量的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。