misc_animation_authoring.html 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - animation authoring</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. "W" translate | "E" rotate | "R" scale | "+" increase size | "-" decrease size<br />
  12. Press "Q" to toggle world/local space, hold down "Ctrl" to snap to grid
  13. </div>
  14. <script src="js/libs/timeliner_gui.min.js"></script>
  15. <script type="module">
  16. import * as THREE from '../build/three.module.js';
  17. import { TransformControls } from './jsm/controls/TransformControls.js';
  18. import { TimelinerController } from './jsm/animation/TimelinerController.js';
  19. var camera, scene, renderer, control;
  20. init();
  21. render();
  22. function init() {
  23. renderer = new THREE.WebGLRenderer( { antialias: true } );
  24. renderer.setPixelRatio( window.devicePixelRatio );
  25. renderer.setSize( window.innerWidth, window.innerHeight );
  26. document.body.appendChild( renderer.domElement );
  27. //
  28. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 3000 );
  29. camera.position.set( 1000, 500, 1000 );
  30. camera.lookAt( 0, 200, 0 );
  31. scene = new THREE.Scene();
  32. scene.add( new THREE.GridHelper( 1000, 10 ) );
  33. var light = new THREE.DirectionalLight( 0xffffff, 2 );
  34. light.position.set( 1, 1, 1 );
  35. scene.add( light );
  36. var texture = new THREE.TextureLoader().load( 'textures/crate.gif', render );
  37. texture.mapping = THREE.UVMapping;
  38. texture.anisotropy = renderer.capabilities.getMaxAnisotropy();
  39. var geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
  40. var material = new THREE.MeshLambertMaterial( { map: texture } );
  41. control = new TransformControls( camera, renderer.domElement );
  42. control.addEventListener( 'change', render );
  43. var mesh = new THREE.Mesh( geometry, material );
  44. mesh.name = "MyBox";
  45. scene.add( mesh );
  46. control.attach( mesh );
  47. scene.add( control );
  48. window.addEventListener( 'resize', onWindowResize, false );
  49. window.addEventListener( 'keydown', function ( event ) {
  50. switch ( event.keyCode ) {
  51. case 81: // Q
  52. control.setSpace( control.space === "local" ? "world" : "local" );
  53. break;
  54. case 17: // Ctrl
  55. control.setTranslationSnap( 100 );
  56. control.setRotationSnap( THREE.MathUtils.degToRad( 15 ) );
  57. break;
  58. case 87: // W
  59. control.setMode( "translate" );
  60. break;
  61. case 69: // E
  62. control.setMode( "rotate" );
  63. break;
  64. case 82: // R
  65. control.setMode( "scale" );
  66. break;
  67. case 187:
  68. case 107: // +, =, num+
  69. control.setSize( control.size + 0.1 );
  70. break;
  71. case 189:
  72. case 109: // -, _, num-
  73. control.setSize( Math.max( control.size - 0.1, 0.1 ) );
  74. break;
  75. }
  76. } );
  77. window.addEventListener( 'keyup', function ( event ) {
  78. switch ( event.keyCode ) {
  79. case 17: // Ctrl
  80. control.setTranslationSnap( null );
  81. control.setRotationSnap( null );
  82. break;
  83. }
  84. } );
  85. var trackInfo = [
  86. {
  87. type: THREE.VectorKeyframeTrack,
  88. propertyPath: 'MyBox.position',
  89. initialValue: [ 0, 0, 0 ],
  90. interpolation: THREE.InterpolateSmooth
  91. },
  92. {
  93. type: THREE.QuaternionKeyframeTrack,
  94. propertyPath: 'MyBox.quaternion',
  95. initialValue: [ 0, 0, 0, 1 ],
  96. interpolation: THREE.InterpolateLinear
  97. }
  98. ];
  99. new Timeliner( new TimelinerController( scene, trackInfo, render ) );
  100. }
  101. function onWindowResize() {
  102. camera.aspect = window.innerWidth / window.innerHeight;
  103. camera.updateProjectionMatrix();
  104. renderer.setSize( window.innerWidth, window.innerHeight );
  105. render();
  106. }
  107. function render() {
  108. renderer.render( scene, camera );
  109. }
  110. </script>
  111. </body>
  112. </html>