load-obj-materials.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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</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 = 40;
  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 / 2;
  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. {
  85. const mtlLoader = new MTLLoader();
  86. mtlLoader.load( 'resources/models/windmill/windmill.mtl', ( mtl ) => {
  87. mtl.preload();
  88. const objLoader = new OBJLoader();
  89. objLoader.setMaterials( mtl );
  90. objLoader.load( 'resources/models/windmill/windmill.obj', ( root ) => {
  91. scene.add( root );
  92. } );
  93. } );
  94. }
  95. function resizeRendererToDisplaySize( renderer ) {
  96. const canvas = renderer.domElement;
  97. const width = canvas.clientWidth;
  98. const height = canvas.clientHeight;
  99. const needResize = canvas.width !== width || canvas.height !== height;
  100. if ( needResize ) {
  101. renderer.setSize( width, height, false );
  102. }
  103. return needResize;
  104. }
  105. function render() {
  106. if ( resizeRendererToDisplaySize( renderer ) ) {
  107. const canvas = renderer.domElement;
  108. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  109. camera.updateProjectionMatrix();
  110. }
  111. renderer.render( scene, camera );
  112. requestAnimationFrame( render );
  113. }
  114. requestAnimationFrame( render );
  115. }
  116. main();
  117. </script>
  118. </html>