webgl_custom_attributes_points2.html 6.2 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">three.js</a> - custom attributes example - particles - billboards</div>
  29. <div id="container"></div>
  30. <script src="../build/three.min.js"></script>
  31. <script src="js/Detector.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;
  41. gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) );
  42. gl_Position = projectionMatrix * mvPosition;
  43. }
  44. </script>
  45. <script type="x-shader/x-fragment" id="fragmentshader">
  46. uniform vec3 color;
  47. uniform sampler2D texture;
  48. varying vec3 vColor;
  49. void main() {
  50. vec4 color = vec4( color * vColor, 1.0 ) * texture2D( texture, gl_PointCoord );
  51. gl_FragColor = color;
  52. }
  53. </script>
  54. <script>
  55. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  56. var renderer, scene, camera, stats;
  57. var sphere, vertices1;
  58. var WIDTH = window.innerWidth;
  59. var HEIGHT = window.innerHeight;
  60. init();
  61. animate();
  62. function init() {
  63. camera = new THREE.PerspectiveCamera( 45, WIDTH / HEIGHT, 1, 10000 );
  64. camera.position.z = 300;
  65. scene = new THREE.Scene();
  66. var radius = 100, segments = 68, rings = 38;
  67. var geometry1 = new THREE.SphereGeometry( radius, segments, rings );
  68. var geometry2 = new THREE.BoxGeometry( 0.8 * radius, 0.8 * radius, 0.8 * radius, 10, 10, 10 );
  69. vertices1 = geometry1.vertices.length;
  70. var vertices = geometry1.vertices.concat( geometry2.vertices );
  71. var positions = new Float32Array( vertices.length * 3 );
  72. var colors = new Float32Array( vertices.length * 3 );
  73. var sizes = new Float32Array( vertices.length );
  74. var vertex;
  75. var color = new THREE.Color();
  76. for ( var i = 0, l = vertices.length; i < l; i ++ ) {
  77. vertex = vertices[ i ];
  78. vertex.toArray( positions, i * 3 );
  79. if ( i < vertices1 ) {
  80. color.setHSL( 0.01 + 0.1 * ( i / vertices1 ), 0.99, ( vertex.y + radius ) / ( 4 * radius ) );
  81. } else {
  82. color.setHSL( 0.6, 0.75, 0.25 + vertex.y / ( 2 * radius ) );
  83. }
  84. color.toArray( colors, i * 3 );
  85. sizes[ i ] = i < vertices1 ? 10 : 40;
  86. }
  87. var geometry = new THREE.BufferGeometry();
  88. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  89. geometry.addAttribute( 'size', new THREE.BufferAttribute( sizes, 1 ) );
  90. geometry.addAttribute( 'ca', new THREE.BufferAttribute( colors, 3 ) );
  91. //
  92. var texture = THREE.ImageUtils.loadTexture( "textures/sprites/disc.png" );
  93. texture.wrapS = THREE.RepeatWrapping;
  94. texture.wrapT = THREE.RepeatWrapping;
  95. var material = new THREE.ShaderMaterial( {
  96. uniforms: {
  97. amplitude: { type: "f", value: 1.0 },
  98. color: { type: "c", value: new THREE.Color( 0xffffff ) },
  99. texture: { type: "t", 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. stats.domElement.style.position = 'absolute';
  116. stats.domElement.style.top = '0px';
  117. container.appendChild( stats.domElement );
  118. //
  119. window.addEventListener( 'resize', onWindowResize, false );
  120. }
  121. function onWindowResize() {
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. }
  126. function sortPoints() {
  127. var vector = new THREE.Vector3();
  128. // Model View Projection matrix
  129. var matrix = new THREE.Matrix4();
  130. matrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  131. matrix.multiply( sphere.matrixWorld );
  132. //
  133. var geometry = sphere.geometry;
  134. var index = geometry.getIndex();
  135. var positions = geometry.getAttribute( 'position' ).array;
  136. var length = positions.length / 3;
  137. if ( index === null ) {
  138. var array = new Uint16Array( length );
  139. for ( var i = 0; i < length; i ++ ) {
  140. array[ i ] = i;
  141. }
  142. index = new THREE.BufferAttribute( array, 1 );
  143. geometry.setIndex( index );
  144. }
  145. var sortArray = [];
  146. for ( var i = 0; i < length; i ++ ) {
  147. vector.fromArray( positions, i * 3 );
  148. vector.applyProjection( matrix );
  149. sortArray.push( [ vector.z, i ] );
  150. }
  151. function numericalSort( a, b ) {
  152. return b[ 0 ] - a[ 0 ];
  153. }
  154. sortArray.sort( numericalSort );
  155. var indices = index.array;
  156. for ( var i = 0; i < length; i ++ ) {
  157. indices[ i ] = sortArray[ i ][ 1 ];
  158. }
  159. geometry.index.needsUpdate = true;
  160. }
  161. function animate() {
  162. requestAnimationFrame( animate );
  163. render();
  164. stats.update();
  165. }
  166. function render() {
  167. var time = Date.now() * 0.005;
  168. sphere.rotation.y = 0.02 * time;
  169. sphere.rotation.z = 0.02 * time;
  170. var geometry = sphere.geometry;
  171. var attributes = geometry.attributes;
  172. for ( var i = 0; i < attributes.size.array.length; i ++ ) {
  173. if ( i < vertices1 ) {
  174. attributes.size.array[ i ] = 16 + 12 * Math.sin( 0.1 * i + time );
  175. }
  176. }
  177. attributes.size.needsUpdate = true;
  178. sortPoints();
  179. renderer.render( scene, camera );
  180. }
  181. </script>
  182. </body>
  183. </html>