webgl_loader_json_claraio.html 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - loaders - Clara.io JSON 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> - <a href="https://clara.io" target="_blank" rel="noopener">Clara.io</a> JSON THREE.Scene/Object Load Test<br/>
  12. More Information: <a href="https://clara.io/learn/user-guide/data_exchange/threejs_export" target="_blank" rel="noopener">Clara.io ThreeJS Export Documentation</a>
  13. </div>
  14. <script type="module">
  15. import * as THREE from '../build/three.module.js';
  16. var container;
  17. var camera, scene, renderer;
  18. var mouseX = 0, mouseY = 0;
  19. var windowHalfX = window.innerWidth / 2;
  20. var windowHalfY = window.innerHeight / 2;
  21. init();
  22. animate();
  23. function init() {
  24. container = document.createElement( 'div' );
  25. document.body.appendChild( container );
  26. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  27. camera.position.z = 4;
  28. // scene
  29. scene = new THREE.Scene();
  30. var ambient = new THREE.AmbientLight( 0x444444 );
  31. scene.add( ambient );
  32. var directionalLight = new THREE.DirectionalLight( 0xffeedd );
  33. directionalLight.position.set( 0, 0, 1 ).normalize();
  34. scene.add( directionalLight );
  35. // BEGIN Clara.io JSON loader code
  36. var objectLoader = new THREE.ObjectLoader();
  37. objectLoader.load( "models/json/teapot-claraio.json", function ( obj ) {
  38. scene.add( obj );
  39. } );
  40. // END Clara.io JSON loader code
  41. renderer = new THREE.WebGLRenderer();
  42. renderer.setPixelRatio( window.devicePixelRatio );
  43. renderer.setSize( window.innerWidth, window.innerHeight );
  44. container.appendChild( renderer.domElement );
  45. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  46. //
  47. window.addEventListener( 'resize', onWindowResize, false );
  48. }
  49. function onWindowResize() {
  50. windowHalfX = window.innerWidth / 2;
  51. windowHalfY = window.innerHeight / 2;
  52. camera.aspect = window.innerWidth / window.innerHeight;
  53. camera.updateProjectionMatrix();
  54. renderer.setSize( window.innerWidth, window.innerHeight );
  55. }
  56. function onDocumentMouseMove( event ) {
  57. mouseX = ( event.clientX - windowHalfX ) / 2;
  58. mouseY = ( event.clientY - windowHalfY ) / 2;
  59. }
  60. //
  61. function animate() {
  62. requestAnimationFrame( animate );
  63. render();
  64. }
  65. function render() {
  66. camera.position.x += ( mouseX - camera.position.x ) * .05;
  67. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  68. camera.lookAt( scene.position );
  69. renderer.render( scene, camera );
  70. }
  71. </script>
  72. </body>
  73. </html>