当前位置:   article > 正文

vue2+three.js实现宇宙(进阶版)_vue2 three.js

vue2 three.js

2023.9.12今天我学习了vue2+three.js实现一个好看的动态效果:

首先是安装:

npm install three

相关代码如下:

  1. <!--3d宇宙效果-->
  2. <template>
  3. <div>
  4. <div id="content" />
  5. </div>
  6. </template>
  7. <script>
  8. import * as THREE from 'three'
  9. export default {
  10. data() {
  11. return {
  12. scene: null,
  13. camera: null,
  14. renderer: null,
  15. mesh: null,
  16. light: null,
  17. stars: null,
  18. mesh3: null
  19. }
  20. },
  21. mounted() {
  22. this.init()
  23. this.animate()
  24. },
  25. methods: {
  26. init() {
  27. // 创建场景
  28. this.createScene()
  29. //创建照相机
  30. this.createCamera()
  31. // 创建渲染器
  32. this.createRenderer()
  33. // 创建物体
  34. this.createMesh()
  35. // 创建星空
  36. this.createStars()
  37. //触发
  38. this.render()
  39. },
  40. // 创建场景
  41. createScene() {
  42. this.scene = new THREE.Scene()
  43. this.light = new THREE.DirectionalLight(0xffffff, 1)
  44. this.light.position.set(100, 100, 100)
  45. this.scene.add(this.light)
  46. },
  47. createStars() {
  48. const geometry = new THREE.BufferGeometry()
  49. const positions = []
  50. for (let i = 0; i < 10000; i++) {
  51. const x = THREE.MathUtils.randFloatSpread(2000)
  52. const y = THREE.MathUtils.randFloatSpread(2000)
  53. const z = THREE.MathUtils.randFloatSpread(2000)
  54. positions.push(x, y, z)
  55. }
  56. geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3))
  57. const material = new THREE.PointsMaterial({color: 0xffffff})
  58. this.stars = new THREE.Points(geometry, material)
  59. this.scene.add(this.stars)
  60. },
  61. // 创建相机
  62. createCamera() {
  63. this.camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000)
  64. this.camera.position.set(200, 200, 200)
  65. this.camera.lookAt(this.scene.position) // 设置相机方向(指向的场景对象)
  66. },
  67. // 创建渲染器
  68. createRenderer() {
  69. this.renderer = new THREE.WebGLRenderer()
  70. this.renderer.setSize(window.innerWidth, window.innerHeight)
  71. this.renderer.setClearColor(new THREE.Color(0x000000))
  72. document.getElementById('content').appendChild(this.renderer.domElement)
  73. },
  74. // 创建物体
  75. createMesh() {
  76. let geometry1 = new THREE.SphereGeometry(40, 40, 40)
  77. let material1 = new THREE.MeshLambertMaterial({
  78. color: 0x00ff00
  79. })
  80. this.mesh = new THREE.Mesh(geometry1, material1)
  81. this.mesh.position.set(0, 0, 0)
  82. this.scene.add(this.mesh)
  83. let geometry2 = new THREE.SphereGeometry(50, 50, 50)
  84. let material2 = new THREE.MeshLambertMaterial({
  85. color: 0xADD8E6
  86. })
  87. let mesh2 = new THREE.Mesh(geometry2, material2)
  88. mesh2.position.set(-40, 0, 0)
  89. this.scene.add(mesh2)
  90. let geometry3 = new THREE.SphereGeometry(30, 50, 50)
  91. let material3 = new THREE.MeshLambertMaterial({
  92. color: 0x800080
  93. })
  94. this.mesh3 = new THREE.Mesh(geometry3, material3)
  95. this.mesh3.position.set(160, 0, 0)
  96. this.scene.add(this.mesh3)
  97. let geometry4 = new THREE.SphereGeometry(35, 50, 50)
  98. let material4 = new THREE.MeshLambertMaterial({
  99. color: 0xFFFF00
  100. })
  101. let mesh4 = new THREE.Mesh(geometry4, material4)
  102. mesh4.position.set(-20, 80, 150)
  103. this.scene.add(mesh4)
  104. let geometry5 = new THREE.SphereGeometry(15, 50, 50)
  105. let material5 = new THREE.MeshLambertMaterial({
  106. color: 0x0000FF
  107. })
  108. let mesh5 = new THREE.Mesh(geometry5, material5)
  109. mesh5.position.set(120, 80, -80)
  110. this.scene.add(mesh5)
  111. this.render()
  112. },
  113. // 触发
  114. render() {
  115. this.renderer.render(this.scene, this.camera)
  116. },
  117. animate() {
  118. // 计算时间差
  119. const time = Date.now() * 0.001
  120. // 根据时间变化更新球体和光源的位置
  121. this.mesh.position.set(100 * Math.cos(time), 100 * Math.sin(time), 0)
  122. this.mesh3.position.set(0, 50 * Math.cos(time), 100 * Math.sin(time))
  123. this.light.position.set(50 * Math.cos(time), 50 * Math.sin(time), 0)
  124. // 触发渲染
  125. this.render()
  126. // 不断循环调用 animate 函数
  127. requestAnimationFrame(this.animate)
  128. },
  129. }
  130. }
  131. </script>

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

闽ICP备14008679号