load-obj-materials-windmill2.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Load .OBJ and .MTL file - Windmill2</title>
  8. <style>
  9. html, body {
  10. margin: 0;
  11. height: 100%;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <script type="importmap">
  24. {
  25. "imports": {
  26. "three": "../../build/three.module.js",
  27. "three/addons/": "../../examples/jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
  35. import { MTLLoader } from 'three/addons/loaders/MTLLoader.js';
  36. function main() {
  37. const canvas = document.querySelector( '#c' );
  38. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  39. const fov = 45;
  40. const aspect = 2; // the canvas default
  41. const near = 0.1;
  42. const far = 100;
  43. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  44. camera.position.set( 0, 10, 20 );
  45. const controls = new OrbitControls( camera, canvas );
  46. controls.target.set( 0, 5, 0 );
  47. controls.update();
  48. const scene = new THREE.Scene();
  49. scene.background = new THREE.Color( 'black' );
  50. {
  51. const planeSize = 4000;
  52. const loader = new THREE.TextureLoader();
  53. const texture = loader.load( 'resources/images/checker.png' );
  54. texture.colorSpace = THREE.SRGBColorSpace;
  55. texture.wrapS = THREE.RepeatWrapping;
  56. texture.wrapT = THREE.RepeatWrapping;
  57. texture.magFilter = THREE.NearestFilter;
  58. const repeats = planeSize / 200;
  59. texture.repeat.set( repeats, repeats );
  60. const planeGeo = new THREE.PlaneGeometry( planeSize, planeSize );
  61. const planeMat = new THREE.MeshPhongMaterial( {
  62. map: texture,
  63. side: THREE.DoubleSide,
  64. } );
  65. const mesh = new THREE.Mesh( planeGeo, planeMat );
  66. mesh.rotation.x = Math.PI * - .5;
  67. scene.add( mesh );
  68. }
  69. {
  70. const skyColor = 0xB1E1FF; // light blue
  71. const groundColor = 0xB97A20; // brownish orange
  72. const intensity = 3;
  73. const light = new THREE.HemisphereLight( skyColor, groundColor, intensity );
  74. scene.add( light );
  75. }
  76. {
  77. const color = 0xFFFFFF;
  78. const intensity = 3;
  79. const light = new THREE.DirectionalLight( color, intensity );
  80. light.position.set( 5, 10, 2 );
  81. scene.add( light );
  82. scene.add( light.target );
  83. }
  84. function frameArea( sizeToFitOnScreen, boxSize, boxCenter, camera ) {
  85. const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
  86. const halfFovY = THREE.MathUtils.degToRad( camera.fov * .5 );
  87. const distance = halfSizeToFitOnScreen / Math.tan( halfFovY );
  88. // compute a unit vector that points in the direction the camera is now
  89. // in the xz plane from the center of the box
  90. const direction = ( new THREE.Vector3() )
  91. .subVectors( camera.position, boxCenter )
  92. .multiply( new THREE.Vector3( 1, 0, 1 ) )
  93. .normalize();
  94. // move the camera to a position distance units way from the center
  95. // in whatever direction the camera was from the center already
  96. camera.position.copy( direction.multiplyScalar( distance ).add( boxCenter ) );
  97. // pick some near and far values for the frustum that
  98. // will contain the box.
  99. camera.near = boxSize / 100;
  100. camera.far = boxSize * 100;
  101. camera.updateProjectionMatrix();
  102. // point the camera to look at the center of the box
  103. camera.lookAt( boxCenter.x, boxCenter.y, boxCenter.z );
  104. }
  105. {
  106. const mtlLoader = new MTLLoader();
  107. mtlLoader.load( 'resources/models/windmill_2/windmill-fixed.mtl', ( mtl ) => {
  108. mtl.preload();
  109. const objLoader = new OBJLoader();
  110. objLoader.setMaterials( mtl );
  111. objLoader.load( 'resources/models/windmill_2/windmill.obj', ( root ) => {
  112. scene.add( root );
  113. // compute the box that contains all the stuff
  114. // from root and below
  115. const box = new THREE.Box3().setFromObject( root );
  116. const boxSize = box.getSize( new THREE.Vector3() ).length();
  117. const boxCenter = box.getCenter( new THREE.Vector3() );
  118. // set the camera to frame the box
  119. frameArea( boxSize * 1.2, boxSize, boxCenter, camera );
  120. // update the Trackball controls to handle the new size
  121. controls.maxDistance = boxSize * 10;
  122. controls.target.copy( boxCenter );
  123. controls.update();
  124. } );
  125. } );
  126. }
  127. function resizeRendererToDisplaySize( renderer ) {
  128. const canvas = renderer.domElement;
  129. const width = canvas.clientWidth;
  130. const height = canvas.clientHeight;
  131. const needResize = canvas.width !== width || canvas.height !== height;
  132. if ( needResize ) {
  133. renderer.setSize( width, height, false );
  134. }
  135. return needResize;
  136. }
  137. function render() {
  138. if ( resizeRendererToDisplaySize( renderer ) ) {
  139. const canvas = renderer.domElement;
  140. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  141. camera.updateProjectionMatrix();
  142. }
  143. renderer.render( scene, camera );
  144. requestAnimationFrame( render );
  145. }
  146. requestAnimationFrame( render );
  147. }
  148. main();
  149. </script>
  150. </html>