2
0

webgl_custom_attributes_points2.html 6.0 KB

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