threejs-load-obj-materials-windmill2.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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="module">
  24. import * as THREE from './resources/threejs/r114/build/three.module.js';
  25. import {OrbitControls} from './resources/threejs/r114/examples/jsm/controls/OrbitControls.js';
  26. import {OBJLoader2} from './resources/threejs/r114/examples/jsm/loaders/OBJLoader2.js';
  27. import {MTLLoader} from './resources/threejs/r114/examples/jsm/loaders/MTLLoader.js';
  28. import {MtlObjBridge} from './resources/threejs/r114/examples/jsm/loaders/obj2/bridge/MtlObjBridge.js';
  29. function main() {
  30. const canvas = document.querySelector('#c');
  31. const renderer = new THREE.WebGLRenderer({canvas});
  32. const fov = 45;
  33. const aspect = 2; // the canvas default
  34. const near = 0.1;
  35. const far = 100;
  36. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  37. camera.position.set(0, 10, 20);
  38. const controls = new OrbitControls(camera, canvas);
  39. controls.target.set(0, 5, 0);
  40. controls.update();
  41. const scene = new THREE.Scene();
  42. scene.background = new THREE.Color('black');
  43. {
  44. const planeSize = 4000;
  45. const loader = new THREE.TextureLoader();
  46. const texture = loader.load('resources/images/checker.png');
  47. texture.wrapS = THREE.RepeatWrapping;
  48. texture.wrapT = THREE.RepeatWrapping;
  49. texture.magFilter = THREE.NearestFilter;
  50. const repeats = planeSize / 200;
  51. texture.repeat.set(repeats, repeats);
  52. const planeGeo = new THREE.PlaneBufferGeometry(planeSize, planeSize);
  53. const planeMat = new THREE.MeshPhongMaterial({
  54. map: texture,
  55. side: THREE.DoubleSide,
  56. });
  57. const mesh = new THREE.Mesh(planeGeo, planeMat);
  58. mesh.rotation.x = Math.PI * -.5;
  59. scene.add(mesh);
  60. }
  61. {
  62. const skyColor = 0xB1E1FF; // light blue
  63. const groundColor = 0xB97A20; // brownish orange
  64. const intensity = 1;
  65. const light = new THREE.HemisphereLight(skyColor, groundColor, intensity);
  66. scene.add(light);
  67. }
  68. {
  69. const color = 0xFFFFFF;
  70. const intensity = 1;
  71. const light = new THREE.DirectionalLight(color, intensity);
  72. light.position.set(5, 10, 2);
  73. scene.add(light);
  74. scene.add(light.target);
  75. }
  76. function frameArea(sizeToFitOnScreen, boxSize, boxCenter, camera) {
  77. const halfSizeToFitOnScreen = sizeToFitOnScreen * 0.5;
  78. const halfFovY = THREE.MathUtils.degToRad(camera.fov * .5);
  79. const distance = halfSizeToFitOnScreen / Math.tan(halfFovY);
  80. // compute a unit vector that points in the direction the camera is now
  81. // in the xz plane from the center of the box
  82. const direction = (new THREE.Vector3())
  83. .subVectors(camera.position, boxCenter)
  84. .multiply(new THREE.Vector3(1, 0, 1))
  85. .normalize();
  86. // move the camera to a position distance units way from the center
  87. // in whatever direction the camera was from the center already
  88. camera.position.copy(direction.multiplyScalar(distance).add(boxCenter));
  89. // pick some near and far values for the frustum that
  90. // will contain the box.
  91. camera.near = boxSize / 100;
  92. camera.far = boxSize * 100;
  93. camera.updateProjectionMatrix();
  94. // point the camera to look at the center of the box
  95. camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z);
  96. }
  97. {
  98. const mtlLoader = new MTLLoader();
  99. mtlLoader.load('resources/models/windmill_2/windmill-fixed.mtl', (mtlParseResult) => {
  100. const objLoader = new OBJLoader2();
  101. const materials = MtlObjBridge.addMaterialsFromMtlLoader(mtlParseResult);
  102. objLoader.addMaterials(materials);
  103. objLoader.load('resources/models/windmill_2/windmill.obj', (root) => {
  104. scene.add(root);
  105. // compute the box that contains all the stuff
  106. // from root and below
  107. const box = new THREE.Box3().setFromObject(root);
  108. const boxSize = box.getSize(new THREE.Vector3()).length();
  109. const boxCenter = box.getCenter(new THREE.Vector3());
  110. // set the camera to frame the box
  111. frameArea(boxSize * 1.2, boxSize, boxCenter, camera);
  112. // update the Trackball controls to handle the new size
  113. controls.maxDistance = boxSize * 10;
  114. controls.target.copy(boxCenter);
  115. controls.update();
  116. });
  117. });
  118. }
  119. function resizeRendererToDisplaySize(renderer) {
  120. const canvas = renderer.domElement;
  121. const width = canvas.clientWidth;
  122. const height = canvas.clientHeight;
  123. const needResize = canvas.width !== width || canvas.height !== height;
  124. if (needResize) {
  125. renderer.setSize(width, height, false);
  126. }
  127. return needResize;
  128. }
  129. function render() {
  130. if (resizeRendererToDisplaySize(renderer)) {
  131. const canvas = renderer.domElement;
  132. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  133. camera.updateProjectionMatrix();
  134. }
  135. renderer.render(scene, camera);
  136. requestAnimationFrame(render);
  137. }
  138. requestAnimationFrame(render);
  139. }
  140. main();
  141. </script>
  142. </html>