webgl_sprites.html 5.6 KB

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