css3d_sprites.html 5.2 KB

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