当前位置:   article > 正文

three.js 流动线shader实现方式(伪)_threejs 流动线

threejs 流动线

 原文来至,在原文的基础上,优化了一些逻辑

https://blog.csdn.net/weixin_43842660/article/details/97940152

 

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <script src="../js/three.min.js"></script>
  7. <script src="../js/OrbitControls.js"></script>
  8. <script src="../js/Init.js"></script>
  9. <style>
  10. #box{
  11. position: absolute;
  12. top: 0;
  13. left: 0;
  14. right: 0;
  15. bottom: 0;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <div class="ys-absolute-container" id="box" style="overflow: hidden"></div>
  21. <script>
  22. let el=document.getElementById('box')
  23. let app=new THREE.inithree(el,{
  24. axes:true,
  25. })
  26. let scene=app.scene
  27. let renderer=app.renderer
  28. let camera=app.camera
  29. let controls=app.controls
  30. camera.position.set(50, 50, 50)
  31. camera.lookAt(new THREE.Vector3(0, 0, 0))
  32. function addGroundPlane() {
  33. // create the ground plane
  34. var planeGeometry = new THREE.PlaneGeometry(60, 20, 120, 120);
  35. var planeMaterial = new THREE.MeshPhongMaterial({
  36. color: 0xffffff
  37. });
  38. var plane = new THREE.Mesh(planeGeometry, planeMaterial);
  39. plane.receiveShadow = true;
  40. // rotate and position the plane
  41. plane.rotation.x = -0.5 * Math.PI;
  42. plane.position.x = 15;
  43. plane.position.y = 0;
  44. plane.position.z = 0;
  45. scene.add(plane);
  46. return plane;
  47. }
  48. addGroundPlane()
  49. var ambientLight = new THREE.AmbientLight(0xffffff,0.9);
  50. scene.add(ambientLight);
  51. // 参数依次为color、强度、距离、角度,penumbra,decay
  52. var spotLight = new THREE.SpotLight(0x00ff00);
  53. spotLight.position.set(30,25,-10)
  54. scene.add(spotLight);
  55. // const spotLightHelper = new THREE.SpotLightHelper( spotLight )
  56. // scene.add( spotLightHelper )
  57. var amount = 1000;
  58. var radius = 200;
  59. var positions = new Float32Array(amount * 3);
  60. var colors = new Float32Array(amount * 3);
  61. var sizes = new Float32Array(amount);
  62. var vertex = new THREE.Vector3();
  63. var color = new THREE.Color(0xffffff);
  64. for (var i = 0; i < amount; i++) {
  65. vertex.x = (Math.random() * 2 - 1) * radius;
  66. vertex.y = (Math.random() * 2 - 1) * radius;
  67. vertex.z = (Math.random() * 2 - 1) * radius;
  68. vertex.toArray(positions, i * 3);
  69. if (vertex.x < 0) {
  70. color.setHSL(0.5 + 0.1 * (i / amount), 0.7, 0.5);
  71. } else {
  72. color.setHSL(0.0 + 0.1 * (i / amount), 0.9, 0.5);
  73. }
  74. color.toArray(colors, i * 3);
  75. sizes[i] = 2; //线粗细
  76. }
  77. var curve = new THREE.CatmullRomCurve3();
  78. function Point(pos) {
  79. this.vector = new THREE.Vector3(pos.x, pos.y, pos.z);
  80. }
  81. var points = [
  82. new Point({ x: -100, y: 0, z: 0 }),
  83. new Point({ x: -50, y: 50, z: 0 }),
  84. new Point({ x: 100.199427036501952, y: 0, z: 0 }),
  85. ]
  86. points.forEach(function (point) {
  87. curve.points.push(point.vector);
  88. });
  89. var geometry = new THREE.Geometry();
  90. geometry.vertices = curve.getPoints(amount - 1);
  91. var pointArr = [];
  92. var colorArr = [];
  93. var objArr = geometry.vertices;
  94. for (var i = 0; i < objArr.length; i++) {
  95. pointArr.push(objArr[i].x);
  96. pointArr.push(objArr[i].y);
  97. pointArr.push(objArr[i].z);
  98. colorArr.push(0);
  99. colorArr.push(0);
  100. colorArr.push(1);
  101. }
  102. pointArr = new Float32Array(pointArr);
  103. var geometry = new THREE.BufferGeometry();
  104. geometry.addAttribute('position', new THREE.BufferAttribute(pointArr, 3));
  105. geometry.addAttribute('customColor', new THREE.BufferAttribute(colors, 3));
  106. geometry.addAttribute('size', new THREE.BufferAttribute(sizes, 1));
  107. let v=`
  108. attribute float size; //顶点大小,由geometry的属性传入
  109. attribute vec3 customColor; //顶点自定义颜色,由geometry的属性传入
  110. varying vec3 vColor; //插值颜色
  111. void main() {
  112. vColor = customColor; //插值颜色,由geometry的属性传入
  113. gl_PointSize = 5.0*size ;
  114. //gl_Position的计算总是固定为 投影矩阵*模型视图矩阵*位置向量
  115. gl_Position = projectionMatrix*modelViewMatrix * vec4( position, 1.0 );
  116. }`
  117. //原文顶点着色器后三句为,个人没发现size的区别,就简化了
  118. // vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  119. //
  120. // gl_PointSize = size * ( 300.0 / -mvPosition.z );
  121. //
  122. // gl_Position = projectionMatrix * mvPosition;
  123. let f=`
  124. uniform vec3 color; //顶点颜色 ,由shader构造材质时引入
  125. uniform sampler2D pointTexture; //顶点纹理采样器
  126. varying vec3 vColor; //顶点插值颜色
  127. void main() {
  128. //原文 gl_FragColor = vec4( color * vColor, 1.0 ) * texture2D( pointTexture, gl_PointCoord );
  129. //改为一下方式更好看,也易于初学者理解一些
  130. gl_FragColor = vec4( color * vColor, 1.0 ) ;
  131. }
  132. `
  133. var material = new THREE.ShaderMaterial({
  134. uniforms: {
  135. color: { value: new THREE.Color(0xf0ffff) },
  136. pointTexture: { value: new THREE.TextureLoader().load("../images/red_line.png") }
  137. },
  138. vertexShader: v,
  139. fragmentShader: f,
  140. // blending: THREE.AdditiveBlending,
  141. depthTest: false,
  142. depthWrite: false,
  143. transparent: true
  144. });
  145. sphere = new THREE.Points(geometry, material);
  146. scene.add(sphere);
  147. let arr,delArray,newArray
  148. function animate() {
  149. controls.update()
  150. //实际上该种方式就是一堆点构线,将点数组的前三个一直取出,在拼接到最后,造成流动的效果
  151. //实际上并不是线,放大之后就可以看得很清楚
  152. arr = sphere.geometry.attributes.position.array.toString().split(",");
  153. delArray = arr.splice(0, 9);
  154. newArray = arr.concat(delArray);
  155. sphere.geometry.attributes.position.array = new Float32Array(newArray);
  156. sphere.geometry.attributes.position.needsUpdate = true;
  157. renderer.render(scene, camera)
  158. requestAnimationFrame(animate)
  159. }
  160. animate()
  161. </script>
  162. </body>
  163. </html>

 

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

闽ICP备14008679号