webgl_particles_sprites.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - particles - sprites</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. background-color: #000000;
  10. margin: 0px;
  11. overflow: hidden;
  12. font-family:Monospace;
  13. font-size:13px;
  14. text-align:center;
  15. font-weight: bold;
  16. text-align:center;
  17. }
  18. a {
  19. color:#0078ff;
  20. }
  21. #info {
  22. color:#fff;
  23. position: absolute;
  24. top: 0px; width: 100%;
  25. padding: 5px;
  26. z-index:100;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - webgl particle sprites example -
  33. snowflakes by <a href="http://en.wikipedia.org/wiki/File:Sketch_of_snow_crystal_by_Ren%C3%A9_Descartes.jpg">Ren&eacute; Descartes</a>
  34. </div>
  35. <script src="../build/Three.js"></script>
  36. <script src="js/Detector.js"></script>
  37. <script src="js/RequestAnimationFrame.js"></script>
  38. <script src="js/Stats.js"></script>
  39. <script>
  40. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  41. var container, stats;
  42. var camera, scene, renderer, particles, geometry, materials = [], parameters, i, h, color, sprite, size;
  43. var mouseX = 0, mouseY = 0;
  44. var windowHalfX = window.innerWidth / 2;
  45. var windowHalfY = window.innerHeight / 2;
  46. init();
  47. animate();
  48. function init() {
  49. container = document.createElement( 'div' );
  50. document.body.appendChild( container );
  51. camera = new THREE.Camera( 75, window.innerWidth / window.innerHeight, 1, 3000 );
  52. camera.position.z = 1000;
  53. camera.useTarget = true;
  54. scene = new THREE.Scene();
  55. scene.fog = new THREE.FogExp2( 0x000000, 0.0008 );
  56. geometry = new THREE.Geometry();
  57. sprite1 = THREE.ImageUtils.loadTexture( "textures/sprites/snowflake1.png" );
  58. sprite2 = THREE.ImageUtils.loadTexture( "textures/sprites/snowflake2.png" );
  59. sprite3 = THREE.ImageUtils.loadTexture( "textures/sprites/snowflake3.png" );
  60. sprite4 = THREE.ImageUtils.loadTexture( "textures/sprites/snowflake4.png" );
  61. sprite5 = THREE.ImageUtils.loadTexture( "textures/sprites/snowflake5.png" );
  62. for ( i = 0; i < 2000; i ++ ) {
  63. vector = new THREE.Vector3( Math.random() * 2000 - 1000, Math.random() * 2000 - 1000, Math.random() * 2000 - 1000 );
  64. geometry.vertices.push( new THREE.Vertex( vector ) );
  65. }
  66. parameters = [ [ [1.0, 0.2, 1.0], sprite2, 20 ], [ [0.95, 0.1, 1], sprite3, 15 ], [ [0.90, 0.05, 1], sprite1, 10 ], [ [0.85, 0, 0.8], sprite5, 8 ], [ [0.80, 0, 0.7], sprite4, 5 ],
  67. ];
  68. for ( i = 0; i < parameters.length; i++ ) {
  69. color = parameters[i][0];
  70. sprite = parameters[i][1];
  71. size = parameters[i][2];
  72. materials[i] = new THREE.ParticleBasicMaterial( { size: size, map: sprite, blending: THREE.AdditiveBlending, depthTest: false, transparent : true } );
  73. materials[i].color.setHSV( color[0], color[1], color[2] );
  74. particles = new THREE.ParticleSystem( geometry, materials[i] );
  75. particles.rotation.x = Math.random() * 6;
  76. particles.rotation.y = Math.random() * 6;
  77. particles.rotation.z = Math.random() * 6;
  78. scene.add( particles );
  79. }
  80. renderer = new THREE.WebGLRenderer( { clearAlpha: 1 });
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. container.appendChild( renderer.domElement );
  83. stats = new Stats();
  84. stats.domElement.style.position = 'absolute';
  85. stats.domElement.style.top = '0px';
  86. container.appendChild( stats.domElement );
  87. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  88. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  89. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  90. }
  91. function onDocumentMouseMove( event ) {
  92. mouseX = event.clientX - windowHalfX;
  93. mouseY = event.clientY - windowHalfY;
  94. }
  95. function onDocumentTouchStart( event ) {
  96. if ( event.touches.length == 1 ) {
  97. event.preventDefault();
  98. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  99. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  100. }
  101. }
  102. function onDocumentTouchMove( event ) {
  103. if ( event.touches.length == 1 ) {
  104. event.preventDefault();
  105. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  106. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  107. }
  108. }
  109. //
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. render();
  113. stats.update();
  114. }
  115. function render() {
  116. var time = new Date().getTime() * 0.00005;
  117. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  118. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  119. for( i = 0; i < scene.objects.length; i++ ) {
  120. scene.objects[i].rotation.y = time * ( i < 4 ? i+1 : - (i+1) );
  121. }
  122. for( i = 0; i < materials.length; i++ ) {
  123. color = parameters[i][0];
  124. h = ( 360 * ( color[0] + time ) % 360 ) / 360;
  125. materials[i].color.setHSV( h, color[1], color[2] );
  126. }
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>