webgl_interactive_particles.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - interactive particles</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. background-color: #000000;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. #info {
  15. position: absolute;
  16. top: 0px;
  17. width: 100%;
  18. padding: 5px;
  19. font-family: Monospace;
  20. font-size: 13px;
  21. text-align: center;
  22. font-weight: bold;
  23. }
  24. a {
  25. color: #fff;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <div id="container"></div>
  31. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> webgl - interactive - particles</div>
  32. <script src="../build/three.min.js"></script>
  33. <script src="js/Detector.js"></script>
  34. <script src="js/libs/stats.min.js"></script>
  35. <script type="x-shader/x-vertex" id="vertexshader">
  36. attribute float size;
  37. attribute vec3 customColor;
  38. varying vec3 vColor;
  39. void main() {
  40. vColor = customColor;
  41. vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );
  42. gl_PointSize = size * ( 300.0 / length( mvPosition.xyz ) );
  43. gl_Position = projectionMatrix * mvPosition;
  44. }
  45. </script>
  46. <script type="x-shader/x-fragment" id="fragmentshader">
  47. uniform vec3 color;
  48. uniform sampler2D texture;
  49. varying vec3 vColor;
  50. void main() {
  51. gl_FragColor = vec4( color * vColor, 1.0 );
  52. gl_FragColor = gl_FragColor * texture2D( texture, gl_PointCoord );
  53. if ( gl_FragColor.a < ALPHATEST ) discard;
  54. }
  55. </script>
  56. <script>
  57. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  58. var renderer, scene, camera, stats;
  59. var particles, uniforms, attributes;
  60. var PARTICLE_SIZE = 20;
  61. var raycaster, intersects;
  62. var mouse = { x: 1, y: 1 }, INTERSECTED;
  63. var vector = new THREE.Vector3();
  64. init();
  65. animate();
  66. function init() {
  67. container = document.getElementById( 'container' );
  68. scene = new THREE.Scene();
  69. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  70. camera.position.z = 250;
  71. //
  72. attributes = {
  73. size: { type: 'f', value: [] },
  74. customColor: { type: 'c', value: [] }
  75. };
  76. uniforms = {
  77. color: { type: "c", value: new THREE.Color( 0xffffff ) },
  78. texture: { type: "t", value: THREE.ImageUtils.loadTexture( "textures/sprites/disc.png" ) }
  79. };
  80. var shaderMaterial = new THREE.ShaderMaterial( {
  81. uniforms: uniforms,
  82. attributes: attributes,
  83. vertexShader: document.getElementById( 'vertexshader' ).textContent,
  84. fragmentShader: document.getElementById( 'fragmentshader' ).textContent,
  85. alphaTest: 0.9,
  86. } );
  87. var geometry = new THREE.BoxGeometry( 200, 200, 200, 16, 16, 16 );
  88. particles = new THREE.PointCloud( geometry, shaderMaterial );
  89. var values_size = attributes.size.value;
  90. var values_color = attributes.customColor.value;
  91. var vertices = particles.geometry.vertices;
  92. for( var v = 0, vl = vertices.length; v < vl; v++ ) {
  93. values_size[ v ] = PARTICLE_SIZE * 0.5;
  94. values_color[ v ] = new THREE.Color().setHSL( 0.01 + 0.1 * ( v / vl ), 1.0, 0.5 );
  95. }
  96. scene.add( particles );
  97. //
  98. renderer = new THREE.WebGLRenderer();
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. container.appendChild( renderer.domElement );
  101. //
  102. raycaster = new THREE.Raycaster();
  103. //
  104. stats = new Stats();
  105. stats.domElement.style.position = 'absolute';
  106. stats.domElement.style.top = '0px';
  107. container.appendChild( stats.domElement );
  108. //
  109. window.addEventListener( 'resize', onWindowResize, false );
  110. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  111. }
  112. function onDocumentMouseMove( event ) {
  113. event.preventDefault();
  114. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  115. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  116. }
  117. function onWindowResize() {
  118. camera.aspect = window.innerWidth / window.innerHeight;
  119. camera.updateProjectionMatrix();
  120. renderer.setSize( window.innerWidth, window.innerHeight );
  121. }
  122. function animate() {
  123. requestAnimationFrame( animate );
  124. render();
  125. stats.update();
  126. }
  127. function render() { ref: http://dl.dropboxusercontent.com/u/4253186/three/examples/webgl_interactive_particles.html
  128. particles.rotation.x += 0.0005;
  129. particles.rotation.y += 0.001;
  130. vector = new THREE.Vector3( mouse.x, mouse.y, 0.5 ).unproject( camera );
  131. raycaster.ray.set( camera.position, vector.sub( camera.position ).normalize() );
  132. intersects = raycaster.intersectObject( particles );
  133. if ( intersects.length > 0 ) {
  134. if ( INTERSECTED != intersects[ 0 ].index ) {
  135. attributes.size.value[ INTERSECTED ] = PARTICLE_SIZE;
  136. INTERSECTED = intersects[ 0 ].index;
  137. attributes.size.value[ INTERSECTED ] = PARTICLE_SIZE * 1.25;
  138. attributes.size.needsUpdate = true;
  139. }
  140. } else if ( INTERSECTED !== null ) {
  141. attributes.size.value[ INTERSECTED ] = PARTICLE_SIZE;
  142. attributes.size.needsUpdate = true;
  143. INTERSECTED = null;
  144. }
  145. renderer.render( scene, camera );
  146. }
  147. </script>
  148. </body>
  149. </html>