webgl_custom_attributes_points2.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. <!-- Import maps polyfill -->
  33. <!-- Remove this when import maps will be widely supported -->
  34. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  35. <script type="importmap">
  36. {
  37. "imports": {
  38. "three": "../build/three.module.js",
  39. "three/addons/": "./jsm/"
  40. }
  41. }
  42. </script>
  43. <script type="module">
  44. import * as THREE from 'three';
  45. import Stats from 'three/addons/libs/stats.module.js';
  46. import * as BufferGeometryUtils from 'three/addons/utils/BufferGeometryUtils.js';
  47. let renderer, scene, camera, stats;
  48. let sphere, length1;
  49. const WIDTH = window.innerWidth;
  50. const HEIGHT = window.innerHeight;
  51. init();
  52. animate();
  53. function init() {
  54. camera = new THREE.PerspectiveCamera( 45, WIDTH / HEIGHT, 1, 10000 );
  55. camera.position.z = 300;
  56. scene = new THREE.Scene();
  57. const radius = 100, segments = 68, rings = 38;
  58. let sphereGeometry = new THREE.SphereGeometry( radius, segments, rings );
  59. let boxGeometry = new THREE.BoxGeometry( 0.8 * radius, 0.8 * radius, 0.8 * radius, 10, 10, 10 );
  60. // if normal and uv attributes are not removed, mergeVertices() can't consolidate identical vertices with different normal/uv data
  61. sphereGeometry.deleteAttribute( 'normal' );
  62. sphereGeometry.deleteAttribute( 'uv' );
  63. boxGeometry.deleteAttribute( 'normal' );
  64. boxGeometry.deleteAttribute( 'uv' );
  65. sphereGeometry = BufferGeometryUtils.mergeVertices( sphereGeometry );
  66. boxGeometry = BufferGeometryUtils.mergeVertices( boxGeometry );
  67. const combinedGeometry = BufferGeometryUtils.mergeGeometries( [ sphereGeometry, boxGeometry ] );
  68. const positionAttribute = combinedGeometry.getAttribute( 'position' );
  69. const colors = [];
  70. const sizes = [];
  71. const color = new THREE.Color();
  72. const vertex = new THREE.Vector3();
  73. length1 = sphereGeometry.getAttribute( 'position' ).count;
  74. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  75. vertex.fromBufferAttribute( positionAttribute, i );
  76. if ( i < length1 ) {
  77. color.setHSL( 0.01 + 0.1 * ( i / length1 ), 0.99, ( vertex.y + radius ) / ( 4 * radius ) );
  78. } else {
  79. color.setHSL( 0.6, 0.75, 0.25 + vertex.y / ( 2 * radius ) );
  80. }
  81. color.toArray( colors, i * 3 );
  82. sizes[ i ] = i < length1 ? 10 : 40;
  83. }
  84. const geometry = new THREE.BufferGeometry();
  85. geometry.setAttribute( 'position', positionAttribute );
  86. geometry.setAttribute( 'size', new THREE.Float32BufferAttribute( sizes, 1 ) );
  87. geometry.setAttribute( 'ca', new THREE.Float32BufferAttribute( colors, 3 ) );
  88. //
  89. const texture = new THREE.TextureLoader().load( 'textures/sprites/disc.png' );
  90. texture.wrapS = THREE.RepeatWrapping;
  91. texture.wrapT = THREE.RepeatWrapping;
  92. const material = new THREE.ShaderMaterial( {
  93. uniforms: {
  94. color: { value: new THREE.Color( 0xffffff ) },
  95. pointTexture: { value: texture }
  96. },
  97. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  98. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  99. transparent: true
  100. } );
  101. //
  102. sphere = new THREE.Points( geometry, material );
  103. scene.add( sphere );
  104. //
  105. renderer = new THREE.WebGLRenderer();
  106. renderer.setPixelRatio( window.devicePixelRatio );
  107. renderer.setSize( WIDTH, HEIGHT );
  108. renderer.useLegacyLights = false;
  109. const container = document.getElementById( 'container' );
  110. container.appendChild( renderer.domElement );
  111. stats = new Stats();
  112. container.appendChild( stats.dom );
  113. //
  114. window.addEventListener( 'resize', onWindowResize );
  115. }
  116. function onWindowResize() {
  117. camera.aspect = window.innerWidth / window.innerHeight;
  118. camera.updateProjectionMatrix();
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. }
  121. function sortPoints() {
  122. const vector = new THREE.Vector3();
  123. // Model View Projection matrix
  124. const matrix = new THREE.Matrix4();
  125. matrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  126. matrix.multiply( sphere.matrixWorld );
  127. //
  128. const geometry = sphere.geometry;
  129. let index = geometry.getIndex();
  130. const positions = geometry.getAttribute( 'position' ).array;
  131. const length = positions.length / 3;
  132. if ( index === null ) {
  133. const array = new Uint16Array( length );
  134. for ( let i = 0; i < length; i ++ ) {
  135. array[ i ] = i;
  136. }
  137. index = new THREE.BufferAttribute( array, 1 );
  138. geometry.setIndex( index );
  139. }
  140. const sortArray = [];
  141. for ( let i = 0; i < length; i ++ ) {
  142. vector.fromArray( positions, i * 3 );
  143. vector.applyMatrix4( matrix );
  144. sortArray.push( [ vector.z, i ] );
  145. }
  146. function numericalSort( a, b ) {
  147. return b[ 0 ] - a[ 0 ];
  148. }
  149. sortArray.sort( numericalSort );
  150. const indices = index.array;
  151. for ( let i = 0; i < length; i ++ ) {
  152. indices[ i ] = sortArray[ i ][ 1 ];
  153. }
  154. geometry.index.needsUpdate = true;
  155. }
  156. function animate() {
  157. requestAnimationFrame( animate );
  158. render();
  159. stats.update();
  160. }
  161. function render() {
  162. const time = Date.now() * 0.005;
  163. sphere.rotation.y = 0.02 * time;
  164. sphere.rotation.z = 0.02 * time;
  165. const geometry = sphere.geometry;
  166. const attributes = geometry.attributes;
  167. for ( let i = 0; i < attributes.size.array.length; i ++ ) {
  168. if ( i < length1 ) {
  169. attributes.size.array[ i ] = 16 + 12 * Math.sin( 0.1 * i + time );
  170. }
  171. }
  172. attributes.size.needsUpdate = true;
  173. sortPoints();
  174. renderer.render( scene, camera );
  175. }
  176. </script>
  177. </body>
  178. </html>