webgl_particles_billboards.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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. 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 billboards example
  33. </div>
  34. <script src="../build/Three.js"></script>
  35. <script src="js/Detector.js"></script>
  36. <script src="js/RequestAnimationFrame.js"></script>
  37. <script src="js/Stats.js"></script>
  38. <script>
  39. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  40. var container, stats;
  41. var camera, scene, renderer, particles, geometry, material, i, h, color, sprite, size, x, y, z;
  42. var mouseX = 0, mouseY = 0;
  43. var windowHalfX = window.innerWidth / 2;
  44. var windowHalfY = window.innerHeight / 2;
  45. init();
  46. animate();
  47. function init() {
  48. container = document.createElement( 'div' );
  49. document.body.appendChild( container );
  50. camera = new THREE.Camera( 55, window.innerWidth / window.innerHeight, 1, 3000 );
  51. camera.position.z = 1000;
  52. camera.useTarget = true;
  53. scene = new THREE.Scene();
  54. scene.fog = new THREE.FogExp2( 0x000000, 0.001 );
  55. geometry = new THREE.Geometry();
  56. sprite = THREE.ImageUtils.loadTexture( "textures/sprites/circle.png" );
  57. for ( i = 0; i < 10000; i++ ) {
  58. x = 2000 * Math.random() - 1000;
  59. y = 2000 * Math.random() - 1000;
  60. z = 2000 * Math.random() - 1000;
  61. vector = new THREE.Vector3( x, y, z );
  62. geometry.vertices.push( new THREE.Vertex( vector ) );
  63. }
  64. material = new THREE.ParticleBasicMaterial( { size: 35, sizeAttenuation: false, map: sprite } );
  65. material.color.setHSV( 1.0, 0.2, 0.8 );
  66. particles = new THREE.ParticleSystem( geometry, material );
  67. particles.sortParticles = true;
  68. particles.updateMatrix();
  69. scene.add( particles );
  70. var light = new THREE.DirectionalLight( 0xffffff );
  71. light.position.x = 0;
  72. light.position.y = 0;
  73. light.position.z = 1;
  74. scene.add( light );
  75. renderer = new THREE.WebGLRenderer( { clearAlpha: 1 });
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. container.appendChild( renderer.domElement );
  78. stats = new Stats();
  79. stats.domElement.style.position = 'absolute';
  80. stats.domElement.style.top = '0px';
  81. container.appendChild( stats.domElement );
  82. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  83. document.addEventListener( 'touchstart', onDocumentTouchStart, false );
  84. document.addEventListener( 'touchmove', onDocumentTouchMove, false );
  85. }
  86. function onDocumentMouseMove( event ) {
  87. mouseX = event.clientX - windowHalfX;
  88. mouseY = event.clientY - windowHalfY;
  89. }
  90. function onDocumentTouchStart( event ) {
  91. if ( event.touches.length == 1 ) {
  92. event.preventDefault();
  93. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  94. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  95. }
  96. }
  97. function onDocumentTouchMove( event ) {
  98. if ( event.touches.length == 1 ) {
  99. event.preventDefault();
  100. mouseX = event.touches[ 0 ].pageX - windowHalfX;
  101. mouseY = event.touches[ 0 ].pageY - windowHalfY;
  102. }
  103. }
  104. //
  105. function animate() {
  106. requestAnimationFrame( animate );
  107. render();
  108. stats.update();
  109. }
  110. function render() {
  111. var time = new Date().getTime() * 0.00005;
  112. camera.position.x += ( mouseX - camera.position.x ) * 0.05;
  113. camera.position.y += ( - mouseY - camera.position.y ) * 0.05;
  114. /*
  115. for( i = 0; i < scene.objects.length; i++ ) {
  116. scene.objects[i].rotation.y = 5*time * ( i < 4 ? i+1 : - (i+1) );
  117. }
  118. */
  119. h = ( 360 * ( 1.0 + time ) % 360 ) / 360;
  120. material.color.setHSV( h, 0.75, 0.8 );
  121. renderer.render( scene, camera );
  122. }
  123. </script>
  124. </body>
  125. </html>