2
0

css3d_sprites.html 5.1 KB

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