webgl_loader_texture_lottie.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  25. import { RoundedBoxGeometry } from 'three/addons/geometries/RoundedBoxGeometry.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. import lottie from 'https://cdn.jsdelivr.net/npm/lottie-web@5.13.0/+esm';
  28. let renderer, scene, camera, controls;
  29. let mesh;
  30. init();
  31. function init() {
  32. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.1, 10 );
  33. camera.position.z = 2.5;
  34. scene = new THREE.Scene();
  35. scene.background = new THREE.Color( 0x111111 );
  36. // lottie
  37. const loader = new THREE.FileLoader();
  38. loader.setResponseType( 'json' );
  39. loader.load( 'textures/lottie/24017-lottie-logo-animation.json', function ( data ) {
  40. const container = document.createElement( 'div' );
  41. const dpr = window.devicePixelRatio;
  42. container.style.width = data.w * dpr + 'px';
  43. container.style.height = data.h * dpr + 'px';
  44. document.body.appendChild( container );
  45. const animation = lottie.loadAnimation( {
  46. container: container,
  47. animType: 'canvas',
  48. loop: true,
  49. autoplay: true,
  50. animationData: data,
  51. rendererSettings: { dpr: dpr }
  52. } );
  53. const texture = new THREE.CanvasTexture( animation.container );
  54. texture.minFilter = THREE.NearestFilter;
  55. texture.generateMipmaps = false;
  56. texture.colorSpace = THREE.SRGBColorSpace;
  57. animation.addEventListener( 'enterFrame', function () {
  58. texture.needsUpdate = true;
  59. } );
  60. container.style.display = 'none'; // must be done after loadAnimation() otherwise canvas has 0 dimensions
  61. setupControls( animation );
  62. // texture = new THREE.TextureLoader().load( 'textures/uv_grid_directx.jpg' );
  63. // texture.colorSpace = THREE.SRGBColorSpace;
  64. const geometry = new RoundedBoxGeometry( 1, 1, 1, 7, 0.1 );
  65. const material = new THREE.MeshStandardMaterial( { roughness: 0, map: texture } );
  66. mesh = new THREE.Mesh( geometry, material );
  67. scene.add( mesh );
  68. } );
  69. renderer = new THREE.WebGLRenderer();
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.setAnimationLoop( animate );
  73. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  74. document.body.appendChild( renderer.domElement );
  75. const environment = new RoomEnvironment();
  76. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  77. scene.environment = pmremGenerator.fromScene( environment, 0.04 ).texture;
  78. controls = new OrbitControls( camera, renderer.domElement );
  79. controls.autoRotate = true;
  80. //
  81. window.addEventListener( 'resize', onWindowResize );
  82. }
  83. function setupControls( animation ) {
  84. // Lottie animation API
  85. // https://airbnb.io/lottie/#/web
  86. // There are a few undocumented properties:
  87. // console.log( animation );
  88. const scrubber = document.getElementById( 'scrubber' );
  89. scrubber.max = animation.totalFrames;
  90. scrubber.addEventListener( 'pointerdown', function () {
  91. animation.pause();
  92. } );
  93. scrubber.addEventListener( 'pointerup', function () {
  94. animation.play();
  95. } );
  96. scrubber.addEventListener( 'input', function () {
  97. animation.goToAndStop( parseFloat( scrubber.value ), true );
  98. } );
  99. animation.addEventListener( 'enterFrame', function () {
  100. scrubber.value = animation.currentFrame;
  101. } );
  102. }
  103. function onWindowResize() {
  104. camera.aspect = window.innerWidth / window.innerHeight;
  105. camera.updateProjectionMatrix();
  106. renderer.setSize( window.innerWidth, window.innerHeight );
  107. }
  108. //
  109. function animate() {
  110. controls.update();
  111. renderer.render( scene, camera );
  112. }
  113. </script>
  114. </body>
  115. </html>
粤ICP备19079148号