2
0

webgl_custom_attributes_points2.html 6.3 KB

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