tnblog
首页
视频
资源
登录

Three js 绘制星空(二)

3243人阅读 2023/5/31 16:43 总访问:3467522 评论:0 收藏:0 手机
分类: threejs

Three js 绘制星空


package.json

  1. {
  2. "name": "01-three_basic",
  3. "version": "1.0.0",
  4. "description": "",
  5. "main": "index.js",
  6. "scripts": {
  7. "dev": "parcel src/index.html",
  8. "build": "parcel build src/index.html"
  9. },
  10. "author": "",
  11. "license": "ISC",
  12. "devDependencies": {
  13. "parcel-bundler": "^1.12.5"
  14. },
  15. "dependencies": {
  16. "dat.gui": "^0.7.9",
  17. "gsap": "^3.11.5",
  18. "three": "^0.152.2"
  19. }
  20. }


index.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <link rel="stylesheet" href="./assets/css/style.css">
  9. </head>
  10. <body>
  11. <script src="./main/main.js" type="module"></script>
  12. </body>
  13. </html>


main.js

  1. import * as THREE from 'three';
  2. // 导入轨道控制器
  3. import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
  4. // 导入动画库
  5. // import { gsap } from 'gsap';
  6. // 导入dat.gui
  7. // import * as dat from 'dat.gui'
  8. // 创建平面
  9. // 创建场景
  10. const scene = new THREE.Scene()
  11. // 创建相机
  12. // PerspectiveCamera 透视相机
  13. const camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000)
  14. // 设置位置
  15. // x,y,z
  16. camera.position.set(0, 0, 40);
  17. // 添加相机
  18. scene.add(camera)
  19. // 添加辅助线
  20. // const axesHelper = new THREE.AxesHelper( 5 );
  21. // scene.add( axesHelper );
  22. function createPoints(url,size = 0.5){
  23. // 创建球几何体
  24. const sphereGeometry = new THREE.BufferGeometry();
  25. const count = 5000;
  26. // 设置缓冲区数组
  27. const positions = new Float32Array(count * 3);
  28. // 设置顶点
  29. const colors = new Float32Array(count * 3)
  30. for (let i = 0; i < count * 3; i++) {
  31. positions[i] = (Math.random() - 0.5) * 100;
  32. colors[i] = Math.random();
  33. }
  34. sphereGeometry.setAttribute('position',new THREE.BufferAttribute(positions,3))
  35. sphereGeometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
  36. // 设置点材质
  37. const pointMaterial = new THREE.PointsMaterial()
  38. // 设置点大小
  39. pointMaterial.size = 0.5;
  40. // 设置点颜色
  41. pointMaterial.color.set(0xfff000)
  42. // 添加受光影响
  43. pointMaterial.sizeAttenuation = true
  44. // 载入纹理
  45. const textureLoader = new THREE.TextureLoader()
  46. const texture = textureLoader.load(`./textures/particles/${url}.png`)
  47. // 设置纹理
  48. pointMaterial.map = texture;
  49. pointMaterial.alphaMap = texture;
  50. pointMaterial.transparent = true;
  51. pointMaterial.depthWrite = false;
  52. pointMaterial.blending = THREE.AdditiveBlending;
  53. // 启用顶点颜色
  54. pointMaterial.vertexColors = true;
  55. // 创建点
  56. const points = new THREE.Points(sphereGeometry, pointMaterial)
  57. // 场景添加点
  58. scene.add( points );
  59. return points
  60. }
  61. const points = createPoints("zs2",1.5)
  62. const points2 = createPoints("xh",1)
  63. const points3 = createPoints("6",2)
  64. // 初始化添加渲染器
  65. const renderer = new THREE.WebGLRenderer();
  66. // 设置渲染的尺寸大小
  67. renderer.setSize(window.innerWidth,window.innerHeight)
  68. // 开启场景中的阴影贴图
  69. renderer.shadowMap.enabled = true;
  70. renderer.useLegacyLights = true;
  71. // 将webgl渲染的canvas内容添加到body
  72. document.body.appendChild(renderer.domElement)
  73. // 创建控制器
  74. const controls = new OrbitControls(camera, renderer.domElement)
  75. // 设置控制器阻力,让控制器更有真实效果,必须在你的动画循环里调用.update()。
  76. controls.enableDamping = true;
  77. // 设置时钟
  78. const clock = new THREE.Clock();
  79. function render() {
  80. let time = clock.getElapsedTime();
  81. // points.material.map.needsUpdate = true;
  82. // texture.needsUpdate = true
  83. // 自动旋转
  84. points.rotation.x = time * 0.3
  85. points2.rotation.x = time * 0.5
  86. points2.rotation.y = time * 0.4
  87. points3.rotation.z = time * 0.4
  88. // 对应设置控制器阻力
  89. controls.update();
  90. // 渲染render
  91. renderer.render(scene,camera);
  92. // 渲染下一帧的时候就会调用render函数
  93. requestAnimationFrame( render );
  94. }
  95. render()
  96. // 监听画面变化,更新渲染画面
  97. window.addEventListener("resize", () => {
  98. // 更新摄像头
  99. camera.aspect = window.innerWidth / window.innerHeight;
  100. // 更新摄像机的投影矩阵
  101. camera.updateProjectionMatrix();
  102. // 更新渲染器
  103. renderer.setSize(window.innerWidth, window.innerHeight);
  104. // 设置渲染器的像素比
  105. renderer.setPixelRatio(window.devicePixelRatio);
  106. });


这里有个图。


欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739

评价

小可爱

2019/12/24 16:53:39

[兔子][兔子]小坑小坑

这一世以无限游戏为使命!
排名
2
文章
634
粉丝
44
评论
93
docker中Sware集群与service
尘叶心繁 : 想学呀!我教你呀
一个bug让程序员走上法庭 索赔金额达400亿日元
叼着奶瓶逛酒吧 : 所以说做程序员也要懂点法律知识
.net core 塑形资源
剑轩 : 收藏收藏
映射AutoMapper
剑轩 : 好是好,这个对效率影响大不大哇,效率高不高
ASP.NET Core 服务注册生命周期
剑轩 : http://www.tnblog.net/aojiancc2/article/details/167
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术