webgl_loader_texture_lottie.html 4.2 KB

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