css3d_sprites.html 4.7 KB

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