1
0

webgl_loader_gltf_animation_pointer.html 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation - keyframes</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. <style>
  9. body {
  10. background-color: #bfe3dd;
  11. color: #000;
  12. }
  13. a {
  14. color: #2983ff;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="container"></div>
  20. <div id="info">
  21. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> + <a href="https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_animation_pointer" target="_blank" rel="noopener">KHR_animation_pointer</a> using <a href="https://www.npmjs.com/package/@needle-tools/three-animation-pointer" target="_blank" rel="noopener">@needle-tools/three-animation-pointer</a><br/>
  22. DragonDispersion from <a href="https://github.com/KhronosGroup/glTF-Sample-Assets/tree/main/Models/DragonDispersion" target="_blank" rel="noopener">glTF-Sample-Assets</a>
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.module.js",
  28. "three/addons/": "./jsm/",
  29. "@needle-tools/three-animation-pointer": "https://cdn.jsdelivr.net/npm/@needle-tools/three-animation-pointer@1.0.1"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. import Stats from 'three/addons/libs/stats.module.js';
  36. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  37. import { RoomEnvironment } from 'three/addons/environments/RoomEnvironment.js';
  38. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  39. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  40. import { KTX2Loader } from 'three/addons/loaders/KTX2Loader.js';
  41. import { GLTFAnimationPointerExtension } from '@needle-tools/three-animation-pointer';
  42. let mixer;
  43. const timer = new THREE.Timer();
  44. timer.connect( document );
  45. const container = document.getElementById( 'container' );
  46. const stats = new Stats();
  47. container.appendChild( stats.dom );
  48. const renderer = new THREE.WebGLRenderer( { antialias: true } );
  49. renderer.setPixelRatio( window.devicePixelRatio );
  50. renderer.setSize( window.innerWidth, window.innerHeight );
  51. container.appendChild( renderer.domElement );
  52. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  53. const scene = new THREE.Scene();
  54. scene.background = new THREE.Color( 0xbfe3dd );
  55. scene.environment = pmremGenerator.fromScene( new RoomEnvironment(), 0.04 ).texture;
  56. const camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, .2, 100 );
  57. camera.position.set( - 3, 2, 6 );
  58. const controls = new OrbitControls( camera, renderer.domElement );
  59. controls.target.set( 0, 0.5, 0 );
  60. controls.update();
  61. controls.enablePan = false;
  62. controls.enableDamping = true;
  63. const dracoLoader = new DRACOLoader();
  64. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  65. const ktx2Loader = new KTX2Loader()
  66. .setTranscoderPath( 'jsm/libs/basis/' )
  67. .detectSupport( renderer );
  68. const loader = new GLTFLoader();
  69. loader.setDRACOLoader( dracoLoader );
  70. loader.setKTX2Loader( ktx2Loader );
  71. loader.register( p => {
  72. return new GLTFAnimationPointerExtension( p );
  73. } );
  74. loader.load( 'https://cloud.needle.tools/-/assets/Z23hmXB27L6Db-optimized/file', function ( gltf ) {
  75. const model = gltf.scene;
  76. scene.add( model );
  77. mixer = new THREE.AnimationMixer( model );
  78. mixer.clipAction( gltf.animations[ 0 ] ).play();
  79. renderer.setAnimationLoop( animate );
  80. }, undefined, function ( e ) {
  81. console.error( e );
  82. } );
  83. window.onresize = function () {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. };
  88. function animate() {
  89. timer.update();
  90. const delta = timer.getDelta();
  91. mixer.update( delta );
  92. controls.update();
  93. stats.update();
  94. renderer.render( scene, camera );
  95. }
  96. </script>
  97. </body>
  98. </html>
粤ICP备19079148号