You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

153 lines
3.9 KiB

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>ship_triangle OBJloader</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. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0px;
  13. overflow: hidden;
  14. }
  15. #info {
  16. color: #fff;
  17. position: absolute;
  18. top: 10px;
  19. width: 100%;
  20. text-align: center;
  21. z-index: 100;
  22. display:block;
  23. }
  24. #info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="info">
  29. OBJ loader
  30. </div>
  31. <script src="http://threejs.org/build/three.min.js"></script>
  32. <script src="http://threejs.org/examples/js/loaders/OBJLoader.js"></script>
  33. <script src="http://threejs.org/examples/js/Detector.js"></script>
  34. <script src="http://threejs.org/examples/js/libs/stats.min.js"></script>
  35. <script>
  36. var clock = new THREE.Clock();
  37. var delta = clock.getDelta(); // seconds.
  38. var rotateAngle = Math.PI / 2 * delta; // pi/2 radians (90 degrees) per second
  39. var container, stats;
  40. var camera, scene, renderer;
  41. var mouseX = 0, mouseY = 0;
  42. var windowHalfX = window.innerWidth / 2;
  43. var windowHalfY = window.innerHeight / 2;
  44. init();
  45. animate();
  46. function init() {
  47. container = document.createElement( 'div' );
  48. document.body.appendChild( container );
  49. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
  50. camera.position.z = 600;
  51. // scene
  52. scene = new THREE.Scene();
  53. var ambient = new THREE.AmbientLight( 0x101030 );
  54. scene.add( ambient );
  55. var directionalLight = new THREE.DirectionalLight( 0xffeedd );
  56. directionalLight.position.set( 0, 0, 1 );
  57. scene.add( directionalLight );
  58. // texture
  59. var manager = new THREE.LoadingManager();
  60. manager.onProgress = function ( item, loaded, total ) {
  61. console.log( item, loaded, total );
  62. };
  63. // model
  64. var loader = new THREE.OBJLoader( manager );
  65. loader.load( 'triangle.obj', function ( object ) {
  66. object.traverse( function ( child ) {
  67. if ( child instanceof THREE.Mesh ) {
  68. //child.material.map = texture;
  69. }
  70. } );
  71. object.position.x = - 60;
  72. object.rotation.x = 20* Math.PI / 180;
  73. object.rotation.z = 20* Math.PI / 180;
  74. object.scale.x = 30;
  75. object.scale.y = 30;
  76. object.scale.z = 30;
  77. obj = object
  78. scene.add( obj );
  79. } );
  80. renderer = new THREE.WebGLRenderer();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. container.appendChild( renderer.domElement );
  83. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  84. window.addEventListener( 'resize', onWindowResize, false );
  85. }
  86. function onWindowResize() {
  87. windowHalfX = window.innerWidth / 2;
  88. windowHalfY = window.innerHeight / 2;
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. }
  93. function onDocumentMouseMove( event ) {
  94. mouseX = ( event.clientX - windowHalfX ) / 2;
  95. mouseY = ( event.clientY - windowHalfY ) / 2;
  96. }
  97. function animate() {
  98. requestAnimationFrame( animate );
  99. render();
  100. }
  101. function render() {
  102. obj.rotation.x += (0.2*(Math.PI / 180));
  103. obj.rotation.x %=360;
  104. camera.position.x += ( mouseX - camera.position.x ) * .05;
  105. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  106. camera.lookAt( scene.position );
  107. renderer.render( scene, camera );
  108. }
  109. </script>
  110. </body>
  111. </html>