经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » JS/JS库/框架 » three.js » 查看文章
threeJs实现波纹扩散及光标浮动效果详解
来源:jb51  时间:2023/3/17 8:57:04  对本文有异议

版本介绍

threejs版本

  1. "version": "0.142.0",

vue版本

  1. "version": "^3.0.0"

node版本

"version": "14.18.2"

正文

思路主要是:

  • 建立圆柱模型,记得把上下两个面去除
  • 建立立标模型
  • 模型放在public文件下的model文件夹中
  • 在动画里面做一个position.x,y,z的操作

效果如下

核心代码

  1. // 扩散动画
  2. this.group2.scale.x = this.group2.scale.x + 0.1
  3. this.group2.scale.y = this.group2.scale.y - 0.01
  4. this.group2.scale.z = this.group2.scale.z + 0.1
  5. if(this.group2.scale.x > 10){
  6. this.group2.scale.x = 1
  7. this.group2.scale.y = 1
  8. this.group2.scale.z = 1
  9. }
  1. // 上下抖动
  2. const time = Date.now() * 0.005
  3. this.group4.position.y = Math.cos( time ) * 1.75 + 2.25

导入即用

1. 新建一个ts文件

  1. import * as THREE from "three";
  2. import { OBJLoader } from "three/examples/jsm/loaders/OBJLoader.js";
  3. import { MTLLoader } from "three/examples/jsm/loaders/MTLLoader.js";
  4. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls.js";
  5. import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader.js";
  6. import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader.js";
  7. export default class ThreeD {
  8. private cylinderRadius: any; // 圆柱体半径
  9. private cylinderOpacity: any; // 圆柱体透明度
  10. private cylinderMesh: any; // 圆柱网格
  11. private scene: any; // 场景
  12. private camera: any; // 相机
  13. private renderer: any; // 渲染器
  14. private group: any; // 新的组对象,控制模型
  15. private group2: any; // 圆柱体模组
  16. private group3: any; // 圆柱体模组-普通点
  17. private group4: any; // 点位模型
  18. private controls: any; // 创建控件对象
  19. private path: any; // 路径
  20. private objName: any; // 模型
  21. private mtlName: any; // 材质
  22. private cameraX: Number; // 相机x
  23. private cameraY: Number; // 相机y
  24. private cameraZ: Number; // 相机z
  25. private objSize: Number; // 模型倍数
  26. private modelSpeed: Number; // 旋转速度
  27. private screenWidth: Number; // 旋转速度
  28. private screenHeight: Number; // 旋转速度
  29. constructor(
  30. cameraX: Number,
  31. cameraY: Number,
  32. cameraZ: Number,
  33. objSize: Number,
  34. modelSpeed: number
  35. ) {
  36. // this.path = path;
  37. // this.objName = objName;
  38. // this.mtlName = mtlName || null;
  39. this.cameraX = cameraX;
  40. this.cameraY = cameraY;
  41. this.cameraZ = cameraZ;
  42. this.objSize = objSize;
  43. this.screenWidth = 0
  44. this.screenHeight = 0
  45. }
  46. /**
  47. * 初始化
  48. * @param instance 容器dom
  49. */
  50. initThree(instance: HTMLElement | null) {
  51. // 场景宽高
  52. const width: any = instance && instance.clientWidth;
  53. const height: any = instance && instance.clientHeight;
  54. this.screenWidth = width;
  55. this.screenHeight = height;
  56. // 1. 创建场景对象Scene
  57. this.scene = new THREE.Scene();
  58. // 2. 创建相机对象fov 代表视角\aspect 宽高比\near 最近看到\far 最远看到
  59. this.camera = new THREE.PerspectiveCamera(50, width / height, 0.1, 200000);
  60. // 设置相机位置(眼睛位置或者说相机篇拍照位置x,y,z)
  61. this.camera.position.set(600, 300, 100);
  62. // 设置相机视角的角度
  63. this.camera.lookAt(0, 0, 0);
  64. // 3.创建组和模型
  65. this.group2 = new THREE.Group() // 组-总光圈
  66. this.group4 = new THREE.Group() // 组-光标
  67. // 创建光圈-总的
  68. this.loadGlbCylinder('Cylinder2.glb', '0', true,10,0,0,0);
  69. // 标注点
  70. this.loadGlbPoint('biaozhi.glb', '0', true,20);
  71. // 把group对象添加到场景中
  72. this.scene.add(this.group);
  73. this.scene.add(this.group2);
  74. this.scene.add(this.group3);
  75. this.scene.add(this.group4);
  76. // 4. 创建光源
  77. this.createPoint();
  78. // 5. 创建渲染器对象
  79. this.renderer = new THREE.WebGLRenderer();
  80. // 设置渲染器的大小
  81. this.renderer.setSize(Number(width), Number(height));
  82. // 增加背景颜色
  83. this.renderer.setClearColor(0xeeeeee, 0);
  84. // 将渲染器添加到div中
  85. instance && instance.append(this.renderer.domElement);
  86. // 7. 动画旋转
  87. this.animate();
  88. }
  89. // 创建glb模型-圆柱体
  90. /**
  91. *
  92. * @param obj 文件名字
  93. * @param name 模型名字
  94. * @param showFlag 是否展示
  95. * @param scale 放大倍数
  96. * @param x
  97. * @param y
  98. * @param z
  99. */
  100. loadGlbCylinder(obj:string,
  101. name:string,
  102. showFlag:any,
  103. scale:number,
  104. x:number,
  105. y:number,
  106. z:number) {
  107. const dracoLoader = new DRACOLoader();
  108. dracoLoader.setDecoderPath("three/js/libs/draco/gltf/");
  109. const loader = new GLTFLoader();
  110. loader.setDRACOLoader(dracoLoader);
  111. loader.load(
  112. `model/${obj}`,
  113. (gltf) => {
  114. const model = gltf.scene;
  115. model.position.set(x, y, z); // 模型坐标偏移量xyz
  116. model.scale.set(scale, scale, scale);
  117. model.name = name;
  118. model.visible = showFlag;
  119. model.traverse((object:any) => {
  120. if (object.isMesh) { // 开启透明度
  121. object.material.transparent = true;//开启透明
  122. object.material.opacity = 0.3;//设置透明度
  123. }
  124. })
  125. this.group2.add(model);
  126. },
  127. undefined,
  128. function (e) {
  129. console.error(e);
  130. }
  131. );
  132. }
  133. /**
  134. * 创建glb模型-圆柱体-普通
  135. * @param obj 文件名字
  136. * @param name 模型名字
  137. * @param showFlag 是否展示
  138. * @param scale 放大倍数
  139. * @param x
  140. * @param y
  141. * @param z
  142. */
  143. loadGlbPoint(obj:string,
  144. name:string,
  145. showFlag:any,
  146. scale:number) {
  147. const dracoLoader = new DRACOLoader();
  148. dracoLoader.setDecoderPath("three/js/libs/draco/gltf/");
  149. const loader = new GLTFLoader();
  150. loader.setDRACOLoader(dracoLoader);
  151. loader.load(
  152. `model/${obj}`,
  153. (gltf) => {
  154. const model = gltf.scene;
  155. model.position.set(0, 0, 0); // 模型坐标偏移量xyz
  156. model.scale.set(scale, scale, scale);
  157. model.name = name;
  158. model.visible = showFlag;
  159. model.traverse((object:any) => {
  160. if (object.isMesh) { // 开启透明度
  161. object.material.transparent = true;//开启透明
  162. object.material.opacity = 1;//设置透明度
  163. }
  164. })
  165. this.group4.add(model);
  166. },
  167. undefined,
  168. function (e) {
  169. console.error(e);
  170. }
  171. );
  172. }
  173. // 创建光源
  174. createPoint() {
  175. //环境光
  176. const ambientLight = new THREE.AmbientLight(0xffffff, 1);
  177. // ambientLight.position.set(400, 200, 300);
  178. this.scene.add(ambientLight);
  179. }
  180. // 动画效果
  181. animate() {
  182. const clock = new THREE.Clock();
  183. // 渲染
  184. const renderEvent = () => {
  185. // const spt = clock.getDelta() * 1000; // 毫秒
  186. // console.log("一帧的时间:毫秒", spt);
  187. // console.log("帧率FPS", 1000 / spt);
  188. //循环调用函数,请求再次执行渲染函数render,渲染下一帧
  189. requestAnimationFrame(renderEvent);
  190. // 将场景和摄像机传入到渲染器中
  191. this.renderer.render(this.scene, this.camera);
  192. // 围绕物体y轴自转
  193. // this.group.rotation.y -= 0.002;
  194. // 圆柱体扩散动画
  195. this.group2.scale.x = this.group2.scale.x + 0.5
  196. this.group2.scale.y = this.group2.scale.y - 0.01
  197. this.group2.scale.z = this.group2.scale.z + 0.5
  198. if(this.group2.scale.x > 50){
  199. this.group2.scale.x = 1
  200. this.group2.scale.y = 1
  201. this.group2.scale.z = 1
  202. }
  203. // 上下移动
  204. const time = Date.now() * 0.005
  205. this.group4.position.y = Math.cos( time ) * 1.75 + 2.25
  206. };
  207. renderEvent();
  208. }
  209. }

2. 在要用的页面导入

  • 在页面建立dom
  1. <div class="zong-model" ref="dom"></div>
  • 导入js
  1. import ThreeD from "@/utils/threeD_fixed";
  • 在onmounted中使用
  1. threeObj = new ThreeD(8, 50, 60, 1, 2);
  2. dom.value && threeObj.initThree(dom.value);

以上就是threeJs实现波纹扩散及光标浮动效果详解的详细内容,更多关于threeJs波纹扩散光标浮动的资料请关注w3xue其它相关文章!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号