2
0

webgl_custom_attributes_points2.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - custom attributes [particles][billboards]</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - custom attributes example - particles - billboards</div>
  11. <div id="container"></div>
  12. <script type="x-shader/x-vertex" id="vertexshader">
  13. attribute float size;
  14. attribute vec3 ca;
  15. varying vec3 vColor;
  16. void main() {
  17. vColor = ca;
  18. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  19. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  20. gl_Position = projectionMatrix * mvPosition;
  21. }
  22. </script>
  23. <script type="x-shader/x-fragment" id="fragmentshader">
  24. uniform vec3 color;
  25. uniform sampler2D pointTexture;
  26. varying vec3 vColor;
  27. void main() {
  28. vec4 color = vec4( color * vColor, 1.0 ) * texture2D( pointTexture, gl_PointCoord );
  29. gl_FragColor = color;
  30. }
  31. </script>
  32. <script type="importmap">
  33. {
  34. "imports": {
  35. "three": "../build/three.module.js",
  36. "three/addons/": "./jsm/"
  37. }
  38. }
  39. </script>
  40. <script type="module">
  41. import * as THREE from 'three';
  42. import Stats from 'three/addons/libs/stats.module.js';
  43. import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
  44. let renderer, scene, camera, stats;
  45. let sphere, length1;
  46. const WIDTH = window.innerWidth;
  47. const HEIGHT = window.innerHeight;
  48. init();
  49. animate();
  50. function init() {
  51. camera = new THREE.PerspectiveCamera( 45, WIDTH / HEIGHT, 1, 10000 );
  52. camera.position.z = 300;
  53. scene = new THREE.Scene();
  54. const radius = 100, segments = 68, rings = 38;
  55. let sphereGeometry = new THREE.SphereGeometry( radius, segments, rings );
  56. let boxGeometry = new THREE.BoxGeometry( 0.8 * radius, 0.8 * radius, 0.8 * radius, 10, 10, 10 );
  57. // if normal and uv attributes are not removed, mergeVertices() can't consolidate identical vertices with different normal/uv data
  58. sphereGeometry.deleteAttribute( 'normal' );
  59. sphereGeometry.deleteAttribute( 'uv' );
  60. boxGeometry.deleteAttribute( 'normal' );
  61. boxGeometry.deleteAttribute( 'uv' );
  62. sphereGeometry = BufferGeometryUtils.mergeVertices( sphereGeometry );
  63. boxGeometry = BufferGeometryUtils.mergeVertices( boxGeometry );
  64. const combinedGeometry = BufferGeometryUtils.mergeGeometries( [ sphereGeometry, boxGeometry ] );
  65. const positionAttribute = combinedGeometry.getAttribute( 'position' );
  66. const colors = [];
  67. const sizes = [];
  68. const color = new THREE.Color();
  69. const vertex = new THREE.Vector3();
  70. length1 = sphereGeometry.getAttribute( 'position' ).count;
  71. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  72. vertex.fromBufferAttribute( positionAttribute, i );
  73. if ( i < length1 ) {
  74. color.setHSL( 0.01 + 0.1 * ( i / length1 ), 0.99, ( vertex.y + radius ) / ( 4 * radius ) );
  75. } else {
  76. color.setHSL( 0.6, 0.75, 0.25 + vertex.y / ( 2 * radius ) );
  77. }
  78. color.toArray( colors, i * 3 );
  79. sizes[ i ] = i < length1 ? 10 : 40;
  80. }
  81. const geometry = new THREE.BufferGeometry();
  82. geometry.setAttribute( 'position', positionAttribute );
  83. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ) );
  84. geometry.setAttribute( 'ca', new THREE.Float32BufferAttribute( colors, 3 ) );
  85. //
  86. const texture = new THREE.TextureLoader().load( 'textures/sprites/disc.png' );
  87. texture.wrapS = THREE.RepeatWrapping;
  88. texture.wrapT = THREE.RepeatWrapping;
  89. const material = new THREE.ShaderMaterial( {
  90. uniforms: {
  91. color: { value: new THREE.Color( 0xffffff ) },
  92. pointTexture: { value: texture }
  93. },
  94. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  95. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  96. transparent: true
  97. } );
  98. //
  99. sphere = new THREE.Points( geometry, material );
  100. scene.add( sphere );
  101. //
  102. renderer = new THREE.WebGLRenderer();
  103. renderer.setPixelRatio( window.devicePixelRatio );
  104. renderer.setSize( WIDTH, HEIGHT );
  105. const container = document.getElementById( 'container' );
  106. container.appendChild( renderer.domElement );
  107. stats = new Stats();
  108. container.appendChild( stats.dom );
  109. //
  110. window.addEventListener( 'resize', onWindowResize );
  111. }
  112. function onWindowResize() {
  113. camera.aspect = window.innerWidth / window.innerHeight;
  114. camera.updateProjectionMatrix();
  115. renderer.setSize( window.innerWidth, window.innerHeight );
  116. }
  117. function sortPoints() {
  118. const vector = new THREE.Vector3();
  119. // Model View Projection matrix
  120. const matrix = new THREE.Matrix4();
  121. matrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  122. matrix.multiply( sphere.matrixWorld );
  123. //
  124. const geometry = sphere.geometry;
  125. let index = geometry.getIndex();
  126. const positions = geometry.getAttribute( 'position' ).array;
  127. const length = positions.length / 3;
  128. if ( index === null ) {
  129. const array = new Uint16Array( length );
  130. for ( let i = 0; i < length; i ++ ) {
  131. array[ i ] = i;
  132. }
  133. index = new THREE.BufferAttribute( array, 1 );
  134. geometry.setIndex( index );
  135. }
  136. const sortArray = [];
  137. for ( let i = 0; i < length; i ++ ) {
  138. vector.fromArray( positions, i * 3 );
  139. vector.applyMatrix4( matrix );
  140. sortArray.push( [ vector.z, i ] );
  141. }
  142. function numericalSort( a, b ) {
  143. return b[ 0 ] - a[ 0 ];
  144. }
  145. sortArray.sort( numericalSort );
  146. const indices = index.array;
  147. for ( let i = 0; i < length; i ++ ) {
  148. indices[ i ] = sortArray[ i ][ 1 ];
  149. }
  150. geometry.index.needsUpdate = true;
  151. }
  152. function animate() {
  153. requestAnimationFrame( animate );
  154. render();
  155. stats.update();
  156. }
  157. function render() {
  158. const time = Date.now() * 0.005;
  159. sphere.rotation.y = 0.02 * time;
  160. sphere.rotation.z = 0.02 * time;
  161. const geometry = sphere.geometry;
  162. const attributes = geometry.attributes;
  163. for ( let i = 0; i < attributes.size.array.length; i ++ ) {
  164. if ( i < length1 ) {
  165. attributes.size.array[ i ] = 16 + 12 * Math.sin( 0.1 * i + time );
  166. }
  167. }
  168. attributes.size.needsUpdate = true;
  169. sortPoints();
  170. renderer.render( scene, camera );
  171. }
  172. </script>
  173. </body>
  174. </html>