webgl_custom_attributes_points2.html 5.8 KB

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