webgl_custom_attributes_points2.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. function init() {
  50. camera = new THREE.PerspectiveCamera( 45, WIDTH / HEIGHT, 1, 10000 );
  51. camera.position.z = 300;
  52. scene = new THREE.Scene();
  53. const radius = 100, segments = 68, rings = 38;
  54. let sphereGeometry = new THREE.SphereGeometry( radius, segments, rings );
  55. let boxGeometry = new THREE.BoxGeometry( 0.8 * radius, 0.8 * radius, 0.8 * radius, 10, 10, 10 );
  56. // if normal and uv attributes are not removed, mergeVertices() can't consolidate identical vertices with different normal/uv data
  57. sphereGeometry.deleteAttribute( 'normal' );
  58. sphereGeometry.deleteAttribute( 'uv' );
  59. boxGeometry.deleteAttribute( 'normal' );
  60. boxGeometry.deleteAttribute( 'uv' );
  61. sphereGeometry = BufferGeometryUtils.mergeVertices( sphereGeometry );
  62. boxGeometry = BufferGeometryUtils.mergeVertices( boxGeometry );
  63. const combinedGeometry = BufferGeometryUtils.mergeGeometries( [ sphereGeometry, boxGeometry ] );
  64. const positionAttribute = combinedGeometry.getAttribute( 'position' );
  65. const colors = [];
  66. const sizes = [];
  67. const color = new THREE.Color();
  68. const vertex = new THREE.Vector3();
  69. length1 = sphereGeometry.getAttribute( 'position' ).count;
  70. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  71. vertex.fromBufferAttribute( positionAttribute, i );
  72. if ( i < length1 ) {
  73. color.setHSL( 0.01 + 0.1 * ( i / length1 ), 0.99, ( vertex.y + radius ) / ( 4 * radius ) );
  74. } else {
  75. color.setHSL( 0.6, 0.75, 0.25 + vertex.y / ( 2 * radius ) );
  76. }
  77. color.toArray( colors, i * 3 );
  78. sizes[ i ] = i < length1 ? 10 : 40;
  79. }
  80. const geometry = new THREE.BufferGeometry();
  81. geometry.setAttribute( 'position', positionAttribute );
  82. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ) );
  83. geometry.setAttribute( 'ca', new THREE.Float32BufferAttribute( colors, 3 ) );
  84. //
  85. const texture = new THREE.TextureLoader().load( 'textures/sprites/disc.png' );
  86. texture.wrapS = THREE.RepeatWrapping;
  87. texture.wrapT = THREE.RepeatWrapping;
  88. const material = new THREE.ShaderMaterial( {
  89. uniforms: {
  90. color: { value: new THREE.Color( 0xffffff ) },
  91. pointTexture: { value: texture }
  92. },
  93. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  94. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  95. transparent: true
  96. } );
  97. //
  98. sphere = new THREE.Points( geometry, material );
  99. scene.add( sphere );
  100. //
  101. renderer = new THREE.WebGLRenderer();
  102. renderer.setPixelRatio( window.devicePixelRatio );
  103. renderer.setSize( WIDTH, HEIGHT );
  104. renderer.setAnimationLoop( animate );
  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. render();
  154. stats.update();
  155. }
  156. function render() {
  157. const time = Date.now() * 0.005;
  158. sphere.rotation.y = 0.02 * time;
  159. sphere.rotation.z = 0.02 * time;
  160. const geometry = sphere.geometry;
  161. const attributes = geometry.attributes;
  162. for ( let i = 0; i < attributes.size.array.length; i ++ ) {
  163. if ( i < length1 ) {
  164. attributes.size.array[ i ] = 16 + 12 * Math.sin( 0.1 * i + time );
  165. }
  166. }
  167. attributes.size.needsUpdate = true;
  168. sortPoints();
  169. renderer.render( scene, camera );
  170. }
  171. </script>
  172. </body>
  173. </html>