webgl_sprites.html 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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. padding:0;
  10. margin:0;
  11. font-weight: bold;
  12. overflow:hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.js"></script>
  18. <script>
  19. var camera, scene, renderer;
  20. var cameraOrtho, sceneOrtho;
  21. var spriteTL, spriteTR, spriteBL, spriteBR, spriteC;
  22. var mapC;
  23. var group;
  24. init();
  25. animate();
  26. function init() {
  27. var width = window.innerWidth;
  28. var height = window.innerHeight;
  29. camera = new THREE.PerspectiveCamera( 60, width / height, 1, 2100 );
  30. camera.position.z = 1500;
  31. cameraOrtho = new THREE.OrthographicCamera( - width / 2, width / 2, height / 2, - height / 2, 1, 10 );
  32. cameraOrtho.position.z = 10;
  33. scene = new THREE.Scene();
  34. scene.fog = new THREE.Fog( 0x000000, 1500, 2100 );
  35. sceneOrtho = new THREE.Scene();
  36. // create sprites
  37. var amount = 200;
  38. var radius = 500;
  39. var textureLoader = new THREE.TextureLoader();
  40. var mapA = textureLoader.load( "textures/sprite0.png", createHUDSprites );
  41. var mapB = textureLoader.load( "textures/sprite1.png" );
  42. mapC = textureLoader.load( "textures/sprite2.png" );
  43. group = new THREE.Group();
  44. var materialC = new THREE.SpriteMaterial( { map: mapC, color: 0xffffff, fog: true } );
  45. var materialB = new THREE.SpriteMaterial( { map: mapB, color: 0xffffff, fog: true } );
  46. for ( var a = 0; a < amount; a ++ ) {
  47. var x = Math.random() - 0.5;
  48. var y = Math.random() - 0.5;
  49. var z = Math.random() - 0.5;
  50. if ( z < 0 ) {
  51. material = materialB.clone();
  52. } else {
  53. material = materialC.clone();
  54. material.color.setHSL( 0.5 * Math.random(), 0.75, 0.5 );
  55. material.map.offset.set( -0.5, -0.5 );
  56. material.map.repeat.set( 2, 2 );
  57. }
  58. var sprite = new THREE.Sprite( material );
  59. sprite.position.set( x, y, z );
  60. sprite.position.normalize();
  61. sprite.position.multiplyScalar( radius );
  62. group.add( sprite );
  63. }
  64. scene.add( group );
  65. // renderer
  66. renderer = new THREE.WebGLRenderer();
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.autoClear = false; // To allow render overlay on top of sprited sphere
  70. document.body.appendChild( renderer.domElement );
  71. //
  72. window.addEventListener( 'resize', onWindowResize, false );
  73. }
  74. function createHUDSprites ( texture ) {
  75. var material = new THREE.SpriteMaterial( { map: texture } );
  76. var width = material.map.image.width;
  77. var height = material.map.image.height;
  78. spriteTL = new THREE.Sprite( material );
  79. spriteTL.scale.set( width, height, 1 );
  80. sceneOrtho.add( spriteTL );
  81. spriteTR = new THREE.Sprite( material );
  82. spriteTR.scale.set( width, height, 1 );
  83. sceneOrtho.add( spriteTR );
  84. spriteBL = new THREE.Sprite( material );
  85. spriteBL.scale.set( width, height, 1 );
  86. sceneOrtho.add( spriteBL );
  87. spriteBR = new THREE.Sprite( material );
  88. spriteBR.scale.set( width, height, 1 );
  89. sceneOrtho.add( spriteBR );
  90. spriteC = new THREE.Sprite( material );
  91. spriteC.scale.set( width, height, 1 );
  92. sceneOrtho.add( spriteC );
  93. updateHUDSprites();
  94. }
  95. function updateHUDSprites () {
  96. var width = window.innerWidth / 2;
  97. var height = window.innerHeight / 2;
  98. var material = spriteTL.material;
  99. var imageWidth = material.map.image.width / 2;
  100. var imageHeight = material.map.image.height / 2;
  101. spriteTL.position.set( - width + imageWidth, height - imageHeight, 1 ); // top left
  102. spriteTR.position.set( width - imageWidth, height - imageHeight, 1 ); // top right
  103. spriteBL.position.set( - width + imageWidth, - height + imageHeight, 1 ); // bottom left
  104. spriteBR.position.set( width - imageWidth, - height + imageHeight, 1 ); // bottom right
  105. spriteC.position.set( 0, 0, 1 ); // center
  106. }
  107. function onWindowResize() {
  108. var width = window.innerWidth;
  109. var height = window.innerHeight;
  110. camera.aspect = width / height;
  111. camera.updateProjectionMatrix();
  112. cameraOrtho.left = - width / 2;
  113. cameraOrtho.right = width / 2;
  114. cameraOrtho.top = height / 2;
  115. cameraOrtho.bottom = - height / 2;
  116. cameraOrtho.updateProjectionMatrix();
  117. updateHUDSprites();
  118. renderer.setSize( window.innerWidth, window.innerHeight );
  119. }
  120. function animate() {
  121. requestAnimationFrame( animate );
  122. render();
  123. }
  124. function render() {
  125. var time = Date.now() / 1000;
  126. for ( var i = 0, l = group.children.length; i < l; i ++ ) {
  127. var sprite = group.children[ i ];
  128. var material = sprite.material;
  129. var scale = Math.sin( time + sprite.position.x * 0.01 ) * 0.3 + 1.0;
  130. var imageWidth = 1;
  131. var imageHeight = 1;
  132. if ( material.map && material.map.image && material.map.image.width ) {
  133. imageWidth = material.map.image.width;
  134. imageHeight = material.map.image.height;
  135. }
  136. sprite.material.rotation += 0.1 * ( i / l );
  137. sprite.scale.set( scale * imageWidth, scale * imageHeight, 1.0 );
  138. if ( material.map !== mapC ) {
  139. material.opacity = Math.sin( time + sprite.position.x * 0.01 ) * 0.4 + 0.6;
  140. }
  141. }
  142. group.rotation.x = time * 0.5;
  143. group.rotation.y = time * 0.75;
  144. group.rotation.z = time * 1.0;
  145. renderer.clear();
  146. renderer.render( scene, camera );
  147. renderer.clearDepth();
  148. renderer.render( sceneOrtho, cameraOrtho );
  149. }
  150. </script>
  151. </body>
  152. </html>