webgl_custom_attributes_points2.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. <style>
  8. body {
  9. color: #ffffff;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. font-weight: bold;
  14. background-color: #000000;
  15. margin: 0px;
  16. overflow: hidden;
  17. }
  18. #info {
  19. color: #fff;
  20. position: absolute;
  21. top: 0px; width: 100%;
  22. padding: 5px;
  23. z-index:100;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - custom attributes example - particles - billboards</div>
  29. <div id="container"></div>
  30. <script src="../build/three.js"></script>
  31. <script src="js/WebGL.js"></script>
  32. <script src="js/libs/stats.min.js"></script>
  33. <script type="x-shader/x-vertex" id="vertexshader">
  34. attribute float size;
  35. attribute vec3 ca;
  36. varying vec3 vColor;
  37. void main() {
  38. vColor = ca;
  39. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  40. gl_PointSize = size * ( 300.0 / -mvPosition.z );
  41. gl_Position = projectionMatrix * mvPosition;
  42. }
  43. </script>
  44. <script type="x-shader/x-fragment" id="fragmentshader">
  45. uniform vec3 color;
  46. uniform sampler2D texture;
  47. varying vec3 vColor;
  48. void main() {
  49. vec4 color = vec4( color * vColor, 1.0 ) * texture2D( texture, gl_PointCoord );
  50. gl_FragColor = color;
  51. }
  52. </script>
  53. <script>
  54. if ( WEBGL.isWebGLAvailable() === false ) {
  55. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  56. }
  57. var renderer, scene, camera, stats;
  58. var sphere, length1;
  59. var WIDTH = window.innerWidth;
  60. var HEIGHT = window.innerHeight;
  61. init();
  62. animate();
  63. function init() {
  64. camera = new THREE.PerspectiveCamera( 45, WIDTH / HEIGHT, 1, 10000 );
  65. camera.position.z = 300;
  66. scene = new THREE.Scene();
  67. var radius = 100, segments = 68, rings = 38;
  68. var vertices1 = new THREE.SphereGeometry( radius, segments, rings ).vertices;
  69. var vertices2 = new THREE.BoxGeometry( 0.8 * radius, 0.8 * radius, 0.8 * radius, 10, 10, 10 ).vertices;
  70. length1 = vertices1.length;
  71. var vertices = vertices1.concat( vertices2 );
  72. var positions = new Float32Array( vertices.length * 3 );
  73. var colors = new Float32Array( vertices.length * 3 );
  74. var sizes = new Float32Array( vertices.length );
  75. var vertex;
  76. var color = new THREE.Color();
  77. for ( var i = 0, l = vertices.length; i < l; i ++ ) {
  78. vertex = vertices[ i ];
  79. vertex.toArray( positions, i * 3 );
  80. if ( i < length1 ) {
  81. color.setHSL( 0.01 + 0.1 * ( i / length1 ), 0.99, ( vertex.y + radius ) / ( 4 * radius ) );
  82. } else {
  83. color.setHSL( 0.6, 0.75, 0.25 + vertex.y / ( 2 * radius ) );
  84. }
  85. color.toArray( colors, i * 3 );
  86. sizes[ i ] = i < length1 ? 10 : 40;
  87. }
  88. var geometry = new THREE.BufferGeometry();
  89. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  90. geometry.addAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) );
  91. geometry.addAttribute( 'ca', new THREE.BufferAttribute( colors, 3 ) );
  92. //
  93. var texture = new THREE.TextureLoader().load( "textures/sprites/disc.png" );
  94. texture.wrapS = THREE.RepeatWrapping;
  95. texture.wrapT = THREE.RepeatWrapping;
  96. var material = new THREE.ShaderMaterial( {
  97. uniforms: {
  98. color: { value: new THREE.Color( 0xffffff ) },
  99. texture: { value: texture }
  100. },
  101. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  102. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  103. transparent: true
  104. } );
  105. //
  106. sphere = new THREE.Points( geometry, material );
  107. scene.add( sphere );
  108. //
  109. renderer = new THREE.WebGLRenderer();
  110. renderer.setPixelRatio( window.devicePixelRatio );
  111. renderer.setSize( WIDTH, HEIGHT );
  112. var container = document.getElementById( 'container' );
  113. container.appendChild( renderer.domElement );
  114. stats = new Stats();
  115. container.appendChild( stats.dom );
  116. //
  117. window.addEventListener( 'resize', onWindowResize, false );
  118. }
  119. function onWindowResize() {
  120. camera.aspect = window.innerWidth / window.innerHeight;
  121. camera.updateProjectionMatrix();
  122. renderer.setSize( window.innerWidth, window.innerHeight );
  123. }
  124. function sortPoints() {
  125. var vector = new THREE.Vector3();
  126. // Model View Projection matrix
  127. var matrix = new THREE.Matrix4();
  128. matrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  129. matrix.multiply( sphere.matrixWorld );
  130. //
  131. var geometry = sphere.geometry;
  132. var index = geometry.getIndex();
  133. var positions = geometry.getAttribute( 'position' ).array;
  134. var length = positions.length / 3;
  135. if ( index === null ) {
  136. var array = new Uint16Array( length );
  137. for ( var i = 0; i < length; i ++ ) {
  138. array[ i ] = i;
  139. }
  140. index = new THREE.BufferAttribute( array, 1 );
  141. geometry.setIndex( index );
  142. }
  143. var sortArray = [];
  144. for ( var i = 0; i < length; i ++ ) {
  145. vector.fromArray( positions, i * 3 );
  146. vector.applyMatrix4( matrix );
  147. sortArray.push( [ vector.z, i ] );
  148. }
  149. function numericalSort( a, b ) {
  150. return b[ 0 ] - a[ 0 ];
  151. }
  152. sortArray.sort( numericalSort );
  153. var indices = index.array;
  154. for ( var i = 0; i < length; i ++ ) {
  155. indices[ i ] = sortArray[ i ][ 1 ];
  156. }
  157. geometry.index.needsUpdate = true;
  158. }
  159. function animate() {
  160. requestAnimationFrame( animate );
  161. render();
  162. stats.update();
  163. }
  164. function render() {
  165. var time = Date.now() * 0.005;
  166. sphere.rotation.y = 0.02 * time;
  167. sphere.rotation.z = 0.02 * time;
  168. var geometry = sphere.geometry;
  169. var attributes = geometry.attributes;
  170. for ( var i = 0; i < attributes.size.array.length; i ++ ) {
  171. if ( i < length1 ) {
  172. attributes.size.array[ i ] = 16 + 12 * Math.sin( 0.1 * i + time );
  173. }
  174. }
  175. attributes.size.needsUpdate = true;
  176. sortPoints();
  177. renderer.render( scene, camera );
  178. }
  179. </script>
  180. </body>
  181. </html>