css3d_sprites.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>three.js css3d - 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. <style>
  9. body {
  10. background-color: #fff;
  11. color: #000;
  12. }
  13. a {
  14. color: #48f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> css3d - sprites</div>
  20. <div id="container"></div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.module.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three';
  31. import TWEEN from 'three/addons/libs/tween.module.js';
  32. import { TrackballControls } from 'three/addons/controls/TrackballControls.js';
  33. import { CSS3DRenderer, CSS3DSprite } from 'three/addons/renderers/CSS3DRenderer.js';
  34. let camera, scene, renderer;
  35. let controls;
  36. const particlesTotal = 512;
  37. const positions = [];
  38. const objects = [];
  39. let current = 0;
  40. init();
  41. animate();
  42. function init() {
  43. camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 5000 );
  44. camera.position.set( 600, 400, 1500 );
  45. camera.lookAt( 0, 0, 0 );
  46. scene = new THREE.Scene();
  47. const image = document.createElement( 'img' );
  48. image.addEventListener( 'load', function () {
  49. for ( let i = 0; i < particlesTotal; i ++ ) {
  50. const object = new CSS3DSprite( image.cloneNode() );
  51. object.position.x = Math.random() * 4000 - 2000,
  52. object.position.y = Math.random() * 4000 - 2000,
  53. object.position.z = Math.random() * 4000 - 2000;
  54. scene.add( object );
  55. objects.push( object );
  56. }
  57. transition();
  58. } );
  59. image.src = 'textures/sprite.png';
  60. // Plane
  61. const amountX = 16;
  62. const amountZ = 32;
  63. const separationPlane = 150;
  64. const offsetX = ( ( amountX - 1 ) * separationPlane ) / 2;
  65. const offsetZ = ( ( amountZ - 1 ) * separationPlane ) / 2;
  66. for ( let i = 0; i < particlesTotal; i ++ ) {
  67. const x = ( i % amountX ) * separationPlane;
  68. const z = Math.floor( i / amountX ) * separationPlane;
  69. const y = ( Math.sin( x * 0.5 ) + Math.sin( z * 0.5 ) ) * 200;
  70. positions.push( x - offsetX, y, z - offsetZ );
  71. }
  72. // Cube
  73. const amount = 8;
  74. const separationCube = 150;
  75. const offset = ( ( amount - 1 ) * separationCube ) / 2;
  76. for ( let i = 0; i < particlesTotal; i ++ ) {
  77. const x = ( i % amount ) * separationCube;
  78. const y = Math.floor( ( i / amount ) % amount ) * separationCube;
  79. const z = Math.floor( i / ( amount * amount ) ) * separationCube;
  80. positions.push( x - offset, y - offset, z - offset );
  81. }
  82. // Random
  83. for ( let i = 0; i < particlesTotal; i ++ ) {
  84. positions.push(
  85. Math.random() * 4000 - 2000,
  86. Math.random() * 4000 - 2000,
  87. Math.random() * 4000 - 2000
  88. );
  89. }
  90. // Sphere
  91. const radius = 750;
  92. for ( let i = 0; i < particlesTotal; i ++ ) {
  93. const phi = Math.acos( - 1 + ( 2 * i ) / particlesTotal );
  94. const theta = Math.sqrt( particlesTotal * Math.PI ) * phi;
  95. positions.push(
  96. radius * Math.cos( theta ) * Math.sin( phi ),
  97. radius * Math.sin( theta ) * Math.sin( phi ),
  98. radius * Math.cos( phi )
  99. );
  100. }
  101. //
  102. renderer = new CSS3DRenderer();
  103. renderer.setSize( window.innerWidth, window.innerHeight );
  104. document.getElementById( 'container' ).appendChild( renderer.domElement );
  105. //
  106. controls = new TrackballControls( camera, renderer.domElement );
  107. //
  108. window.addEventListener( 'resize', onWindowResize );
  109. }
  110. function onWindowResize() {
  111. camera.aspect = window.innerWidth / window.innerHeight;
  112. camera.updateProjectionMatrix();
  113. renderer.setSize( window.innerWidth, window.innerHeight );
  114. }
  115. function transition() {
  116. const offset = current * particlesTotal * 3;
  117. const duration = 2000;
  118. for ( let i = 0, j = offset; i < particlesTotal; i ++, j += 3 ) {
  119. const object = objects[ i ];
  120. new TWEEN.Tween( object.position )
  121. .to( {
  122. x: positions[ j ],
  123. y: positions[ j + 1 ],
  124. z: positions[ j + 2 ]
  125. }, Math.random() * duration + duration )
  126. .easing( TWEEN.Easing.Exponential.InOut )
  127. .start();
  128. }
  129. new TWEEN.Tween( this )
  130. .to( {}, duration * 3 )
  131. .onComplete( transition )
  132. .start();
  133. current = ( current + 1 ) % 4;
  134. }
  135. function animate() {
  136. requestAnimationFrame( animate );
  137. TWEEN.update();
  138. controls.update();
  139. const time = performance.now();
  140. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  141. const object = objects[ i ];
  142. const scale = Math.sin( ( Math.floor( object.position.x ) + time ) * 0.002 ) * 0.3 + 1;
  143. object.scale.set( scale, scale, scale );
  144. }
  145. renderer.render( scene, camera );
  146. }
  147. </script>
  148. </body>
  149. </html>