webgl_sprites.html 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. background:#fff;
  10. padding:0;
  11. margin:0;
  12. font-weight: bold;
  13. overflow:hidden;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <script src="../build/three.min.js"></script>
  19. <script src="js/libs/stats.min.js"></script>
  20. <script>
  21. var stats;
  22. var camera, scene, renderer;
  23. var mapC;
  24. var group;
  25. init();
  26. animate();
  27. function init() {
  28. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 2100 );
  29. camera.position.z = 1500;
  30. scene = new THREE.Scene();
  31. scene.fog = new THREE.Fog( 0x000000, 1500, 2100 );
  32. // create sprites
  33. var amount = 200;
  34. var radius = 500;
  35. var mapA = THREE.ImageUtils.loadTexture( "textures/sprite0.png", undefined, function() { createHUDSprites() } );
  36. var mapB = THREE.ImageUtils.loadTexture( "textures/sprite1.png" );
  37. mapC = THREE.ImageUtils.loadTexture( "textures/sprite2.png" );
  38. group = new THREE.Object3D();
  39. var materialC = new THREE.SpriteMaterial( { map: mapC, color: 0xffffff, fog: true } );
  40. var materialB = new THREE.SpriteMaterial( { map: mapB, color: 0xffffff, fog: true } );
  41. for ( var a = 0; a < amount; a ++ ) {
  42. var x = Math.random() - 0.5;
  43. var y = Math.random() - 0.5;
  44. var z = Math.random() - 0.5;
  45. if ( z < 0 ) {
  46. material = materialB.clone();
  47. } else {
  48. material = materialC.clone();
  49. material.color.setHSL( 0.5 * Math.random(), 0.75, 0.5 );
  50. material.uvScale.set( 2, 2 );
  51. material.uvOffset.set( -0.5, -0.5 );
  52. }
  53. var sprite = new THREE.Sprite( material );
  54. sprite.position.set( x, y, z );
  55. sprite.position.normalize();
  56. sprite.position.multiplyScalar( radius );
  57. group.add( sprite );
  58. }
  59. scene.add( group );
  60. // add 2d-sprites
  61. var createHUDSprites = function() {
  62. var scaleX = mapA.image.width;
  63. var scaleY = mapA.image.height;
  64. var materialA1 = new THREE.SpriteMaterial( { map: mapA, opacity: 0.25 } );
  65. var materialA2 = new THREE.SpriteMaterial( { map: mapA, opacity: 0.5 } );
  66. var materialA3 = new THREE.SpriteMaterial( { map: mapA, opacity: 1 } );
  67. /*
  68. sprite = new THREE.Sprite( materialA1 );
  69. sprite.position.set( 100, 100, 0 );
  70. sprite.scale.set( scaleX, scaleY, 1 );
  71. scene.add( sprite );
  72. sprite = new THREE.Sprite( materialA2 );
  73. sprite.position.set( 150, 150, 2 );
  74. sprite.scale.set( scaleX, scaleY, 1 );
  75. scene.add( sprite );
  76. sprite = new THREE.Sprite( materialA3 );
  77. sprite.position.set( 200, 200, 3 );
  78. sprite.scale.set( scaleX, scaleY, 1 );
  79. scene.add( sprite );
  80. */
  81. }
  82. // renderer
  83. renderer = new THREE.WebGLRenderer();
  84. renderer.setClearColor( 0x000000, 1 );
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. document.body.appendChild( renderer.domElement );
  87. // stats
  88. stats = new Stats();
  89. stats.domElement.style.position = 'absolute';
  90. stats.domElement.style.top = '0px';
  91. stats.domElement.style.zIndex = 100;
  92. document.body.appendChild( stats.domElement );
  93. //
  94. window.addEventListener( 'resize', onWindowResize, false );
  95. }
  96. function onWindowResize() {
  97. camera.aspect = window.innerWidth / window.innerHeight;
  98. camera.updateProjectionMatrix();
  99. renderer.setSize( window.innerWidth, window.innerHeight );
  100. }
  101. function animate() {
  102. requestAnimationFrame( animate );
  103. render();
  104. stats.update();
  105. }
  106. function render() {
  107. var time = Date.now() / 1000;
  108. for ( var i = 0, l = group.children.length; i < l; i ++ ) {
  109. var sprite = group.children[ i ];
  110. var material = sprite.material;
  111. var scale = Math.sin( time + sprite.position.x * 0.01 ) * 0.3 + 1.0;
  112. var imageWidth = 1;
  113. var imageHeight = 1;
  114. if ( material.map && material.map.image && material.map.image.width ) {
  115. imageWidth = material.map.image.width;
  116. imageHeight = material.map.image.height;
  117. }
  118. sprite.material.rotation += 0.1 * ( i / l );
  119. sprite.scale.set( scale * imageWidth, scale * imageHeight, 1.0 );
  120. if ( material.map !== mapC ) {
  121. material.opacity = Math.sin( time + sprite.position.x * 0.01 ) * 0.4 + 0.6;
  122. }
  123. }
  124. group.rotation.x = time * 0.5;
  125. group.rotation.y = time * 0.75;
  126. group.rotation.z = time * 1.0;
  127. time += 0.02;
  128. renderer.render( scene, camera );
  129. }
  130. </script>
  131. </body>
  132. </html>