webgl_loader_texture_lottie.html 4.8 KB

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