webgl_loader_texture_lottie.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lottie loader</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. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - lottie<br/></br>
  12. <input id="scrubber" type="range" value="0" style="width: 300px">
  13. </div>
  14. <!-- Import maps polyfill -->
  15. <!-- Remove this when import maps will be widely supported -->
  16. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  17. <script type="importmap">
  18. {
  19. "imports": {
  20. "three": "../build/three.module.js",
  21. "three/addons/": "./jsm/"
  22. }
  23. }
  24. </script>
  25. <script type="module">
  26. import * as THREE from 'three';
  27. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  28. import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
  29. import { LottieLoader } from 'three/addons/loaders/LottieLoader.js';
  30. let renderer, scene, camera;
  31. let mesh;
  32. init();
  33. animate();
  34. function init() {
  35. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  36. camera.position.z = 2.5;
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0x111111 );
  39. const loader = new LottieLoader();
  40. loader.setQuality( 2 );
  41. loader.load( 'textures/lottie/24017-lottie-logo-animation.json', function ( texture ) {
  42. setupControls( texture.animation );
  43. // texture = new THREE.TextureLoader().load( 'textures/uv_grid_directx.jpg' );
  44. // texture.colorSpace = THREE.SRGBColorSpace;
  45. const geometry = new RoundedBoxGeometry( 1, 1, 1, 7, 0.2 );
  46. const material = new THREE.MeshStandardMaterial( { roughness: 0.1, map: texture } );
  47. mesh = new THREE.Mesh( geometry, material );
  48. scene.add( mesh );
  49. } );
  50. renderer = new THREE.WebGLRenderer();
  51. renderer.setPixelRatio( window.devicePixelRatio );
  52. renderer.setSize( window.innerWidth, window.innerHeight );
  53. document.body.appendChild( renderer.domElement );
  54. const environment = new RoomEnvironment();
  55. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  56. scene.environment = pmremGenerator.fromScene( environment ).texture;
  57. //
  58. window.addEventListener( 'resize', onWindowResize );
  59. }
  60. function setupControls( animation ) {
  61. // Lottie animation API
  62. // https://airbnb.io/lottie/#/web
  63. // There are a few undocumented properties:
  64. // console.log( animation );
  65. const scrubber = document.getElementById( 'scrubber' );
  66. scrubber.max = animation.totalFrames;
  67. scrubber.addEventListener( 'pointerdown', function () {
  68. animation.pause();
  69. } );
  70. scrubber.addEventListener( 'pointerup', function () {
  71. animation.play();
  72. } );
  73. scrubber.addEventListener( 'input', function () {
  74. animation.goToAndStop( parseFloat( scrubber.value ), true );
  75. } );
  76. animation.addEventListener( 'enterFrame', function () {
  77. scrubber.value = animation.currentFrame;
  78. } );
  79. }
  80. function onWindowResize() {
  81. camera.aspect = window.innerWidth / window.innerHeight;
  82. camera.updateProjectionMatrix();
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. }
  85. //
  86. function animate() {
  87. requestAnimationFrame( animate );
  88. if ( mesh ) {
  89. mesh.rotation.y -= 0.001;
  90. }
  91. renderer.render( scene, camera );
  92. }
  93. </script>
  94. </body>
  95. </html>