webgl_animation_scene.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - scene animation</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. <style>
  8. body {
  9. color: #000;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #fff;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. a {
  23. color: #0af;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container"></div>
  29. <div id="info">
  30. <a href="http://threejs.org" target="_blank">three.js</a> webgl - scene animation - <a href="https://clara.io/view/96106133-2e99-40cf-8abd-64defd153e61">Three Gears Scene</a> courtesy of David Sarno
  31. <br><br>camera orbit/zoom/pan with left/middle/right mouse button</div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/controls/OrbitControls.js"></script>
  34. <script src="js/Detector.js"></script>
  35. <script src="js/libs/stats.min.js"></script>
  36. <script>
  37. var scene, camera, controls, stats;
  38. var renderer, mixer;
  39. var clock = new THREE.Clock();
  40. var url = 'models/json/scene-animation.json';
  41. var SCREEN_WIDTH = window.innerWidth;
  42. var SCREEN_HEIGHT = window.innerHeight;
  43. var container = document.getElementById( 'container' );
  44. stats = new Stats();
  45. container.appendChild( stats.dom );
  46. renderer = new THREE.WebGLRenderer( { antialias: true } );
  47. renderer.setClearColor( 0xffffff );
  48. renderer.setPixelRatio( window.devicePixelRatio );
  49. renderer.setSize( SCREEN_WIDTH, SCREEN_HEIGHT );
  50. container.appendChild( renderer.domElement );
  51. // Load a scene with objects, lights and camera from a JSON file
  52. new THREE.ObjectLoader().load( url, function ( loadedScene ) {
  53. scene = loadedScene;
  54. // If the loaded file contains a perspective camera, use it with adjusted aspect ratio...
  55. scene.traverse( function ( sceneChild ) {
  56. if ( sceneChild.type === 'PerspectiveCamera' ) {
  57. camera = sceneChild;
  58. camera.aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
  59. camera.updateProjectionMatrix();
  60. }
  61. } );
  62. // ... else create a new camera and use it in the loaded scene
  63. if ( camera === undefined ) {
  64. camera = new THREE.PerspectiveCamera( 30, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 10000 );
  65. camera.position.set( - 200, 0, 200 );
  66. }
  67. controls = new THREE.OrbitControls( camera );
  68. // Ground plane and fog: examples for applying additional children and new property values to the loaded scene
  69. var geometry = new THREE.PlaneBufferGeometry( 20000, 20000 );
  70. var material = new THREE.MeshPhongMaterial( { shininess: 0.1 } );
  71. var ground = new THREE.Mesh( geometry, material );
  72. ground.position.set( 0, - 250, 0 );
  73. ground.rotation.x = - Math.PI / 2;
  74. scene.add( ground );
  75. scene.fog = new THREE.Fog( 0xffffff, 1000, 10000 );
  76. // Initialization of the loaded animations
  77. var animationClip = scene.animations[ 0 ];
  78. mixer = new THREE.AnimationMixer( scene );
  79. mixer.clipAction( animationClip ).play();
  80. animate();
  81. } );
  82. window.onresize = function () {
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. renderer.setSize( window.innerWidth, window.innerHeight );
  86. };
  87. function animate() {
  88. requestAnimationFrame( animate );
  89. render();
  90. }
  91. function render() {
  92. var delta = 0.75 * clock.getDelta();
  93. mixer.update( delta );
  94. stats.update();
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>