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


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

    首先是安装:

    npm install three

    相关代码如下:

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

  • 相关阅读:
    北京卫视《为你喝彩》——星辰天合 CEO 胥昕,他专攻 SDS 让“数据常青”
    【Node.js】深度解析模块化的那些事
    Redis的安装和使用
    【RabbitMQ实战】01 3分钟在Linux上安装RabbitMQ
    dolphinscheduler试用(一)(边用边修bug。。。。create tenant error)
    李沐《动手学习深度学习》d2lbook环境搭建
    C++学习之路-类模板之泛型动态数组的实现
    四种IP地址的区别
    Python_面向对象
    含文档+PPT+源码等]精品基于PHP实现的计算机信息管理学院网站[包运行成功]计算机PHP毕业设计项目源码
  • 原文地址:https://blog.csdn.net/qq_53986004/article/details/132831617