当前位置:   article > 正文

three.js加vue2实现.glb渲染,旋转元素,放大缩小,以及元素点击事件_three.js vue2 特效扩散特效

three.js vue2 特效扩散特效
  1. <template>
  2. <div ref="container" id="container"></div>
  3. </template>
  4. <script>
  5. import * as THREE from "three";
  6. import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";
  7. import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
  8. export default {
  9. data() {
  10. return {
  11. camera: {},
  12. renderer: {},
  13. controls: null,
  14. };
  15. },
  16. mounted() {
  17. let that = this;
  18. var fov = 75; // 拍摄距离,视野角值越大,场景中的物体越小
  19. let near = 1; // 最小范围
  20. var far = 1000;
  21. // 创建场景
  22. const scene = new THREE.Scene();
  23. // 创建相机
  24. const camera = new THREE.PerspectiveCamera(
  25. fov,
  26. window.innerWidth / window.innerHeight,
  27. 0.1,
  28. far
  29. );
  30. this.camera = camera;
  31. // camera.position.set(0, 0, 10); // 设置相机在 z 轴上的位置
  32. // 创建渲染器
  33. const renderer = new THREE.WebGLRenderer({ antialias: true });
  34. renderer.setSize(window.innerWidth, window.innerHeight);
  35. this.renderer = renderer;
  36. let dom = this.$refs.container;
  37. this.$refs.container.appendChild(renderer.domElement);
  38. var cub = new THREE.CubeTextureLoader().load(
  39. Array(6).fill(require("@/static/3d/img/backb.jpg"))
  40. );
  41. cub.encoding = THREE.sRGBEncoding;
  42. cub.flipY = false;
  43. scene.background = cub;
  44. // 添加光源
  45. const directionalLight = new THREE.DirectionalLight(0xffffff, 1); // 方向光颜色和强度
  46. directionalLight.position.set(1, 1, 1); // 光源位置
  47. scene.add(directionalLight);
  48. // 创建控制器
  49. const controls = new OrbitControls(camera, renderer.domElement);
  50. this.controls = controls;
  51. controls.enableDamping = true; // 允许阻尼效果,使动画更平滑
  52. controls.dampingFactor = 0.05; // 阻尼系数
  53. controls.rotateSpeed = 0.1; // 旋转速度
  54. // 加载3D模型
  55. const loader = new GLTFLoader();
  56. loader.load("/3D/house2.glb", (gltf) => {
  57. scene.add(gltf.scene);
  58. function animate() {
  59. requestAnimationFrame(animate);
  60. controls.update(); // 更新控制器状态
  61. renderer.render(scene, camera);
  62. }
  63. const object = gltf.scene; // 获取加载的3D模型对象
  64. // 将点击事件绑定到立方体上
  65. object.traverse((child) => {
  66. console.log(child.name);
  67. //child.name可以在three editor中找到
  68. if (child instanceof THREE.Mesh && child.name === "node_id209") {
  69. child.userData.onClick = () => {
  70. // 在这里处理点击事件的逻辑
  71. alert("您好!");
  72. };
  73. child.userData.clickable = true;//将元素设置为可点击
  74. }
  75. if (child instanceof THREE.Mesh && child.name === "node_id183") {
  76. child.userData.onClick = () => {
  77. // 在这里处理点击事件的逻辑
  78. alert("别点我的头!");
  79. };
  80. child.userData.clickable = true;
  81. }
  82. });
  83. scene.add(object);
  84. // 添加点击事件监听器
  85. dom.addEventListener("mousedown", onMouseDown, false);
  86. function onMouseDown(event) {
  87. const mouse = new THREE.Vector2();
  88. mouse.x = (event.clientX / window.innerWidth) * 2 - 1;
  89. mouse.y = -(event.clientY / window.innerHeight) * 2 + 1;
  90. // 创建射线投射器
  91. const raycaster = new THREE.Raycaster();
  92. raycaster.setFromCamera(mouse, camera);
  93. // 检测射线与可点击对象的交互
  94. const intersects = raycaster.intersectObjects(object.children, true);
  95. intersects.forEach((intersect) => {
  96. const { object } = intersect;
  97. // 检查是否为可点击对象
  98. if (
  99. object.userData.clickable &&
  100. typeof object.userData.onClick === "function"
  101. ) {
  102. // 调用点击事件处理函数
  103. object.userData.onClick();
  104. }
  105. });
  106. }
  107. animate();
  108. });
  109. // 滚轮事件
  110. dom.addEventListener("mousewheel", mousewheel, false);
  111. function mousewheel(e) {
  112. e.preventDefault();
  113. if (e.deltaY < 0) {
  114. // 当滚轮向上滚动时
  115. camera.position.z -= 1; // 向前移动相机
  116. } else {
  117. // 当滚轮向下滚动时
  118. camera.position.z += 1; // 向后移动相机
  119. }
  120. renderer.render(scene, camera);
  121. }
  122. // 窗口大小改变事件
  123. window.addEventListener("resize", this.handleResize);
  124. },
  125. methods: {
  126. handleResize() {
  127. const width = window.innerWidth;
  128. const height = window.innerHeight;
  129. this.camera.aspect = width / height;
  130. this.camera.updateProjectionMatrix();
  131. this.renderer.setSize(width, height);
  132. },
  133. beforeDestroy() {
  134. window.removeEventListener("resize", this.handleResize);
  135. },
  136. },
  137. };
  138. </script>
  139. <style>
  140. /* 可以自定义样式 */
  141. #container {
  142. margin: 0;
  143. padding: 0;
  144. }
  145. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/121581
推荐阅读
相关标签
  

闽ICP备14008679号