webgl_loader_texture_lottie.html 4.3 KB

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