load-obj-wat.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 - ?</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. function main() {
  36. const canvas = document.querySelector( '#c' );
  37. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  38. const fov = 45;
  39. const aspect = 2; // the canvas default
  40. const near = 0.1;
  41. const far = 100;
  42. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  43. camera.position.set( 0, 10, 20 );
  44. const controls = new OrbitControls( camera, canvas );
  45. controls.target.set( 0, 5, 0 );
  46. controls.update();
  47. const scene = new THREE.Scene();
  48. scene.background = new THREE.Color( 'black' );
  49. {
  50. const planeSize = 40;
  51. const loader = new THREE.TextureLoader();
  52. const texture = loader.load( 'resources/images/checker.png' );
  53. texture.colorSpace = THREE.SRGBColorSpace;
  54. texture.wrapS = THREE.RepeatWrapping;
  55. texture.wrapT = THREE.RepeatWrapping;
  56. texture.magFilter = THREE.NearestFilter;
  57. const repeats = planeSize / 2;
  58. texture.repeat.set( repeats, repeats );
  59. const planeGeo = new THREE.PlaneGeometry( planeSize, planeSize );
  60. const planeMat = new THREE.MeshPhongMaterial( {
  61. map: texture,
  62. side: THREE.DoubleSide,
  63. } );
  64. const mesh = new THREE.Mesh( planeGeo, planeMat );
  65. mesh.rotation.x = Math.PI * - .5;
  66. scene.add( mesh );
  67. }
  68. {
  69. const skyColor = 0xB1E1FF; // light blue
  70. const groundColor = 0xB97A20; // brownish orange
  71. const intensity = 3;
  72. const light = new THREE.HemisphereLight( skyColor, groundColor, intensity );
  73. scene.add( light );
  74. }
  75. {
  76. const color = 0xFFFFFF;
  77. const intensity = 1;
  78. const light = new THREE.DirectionalLight( color, intensity );
  79. light.position.set( 0, 10, 0 );
  80. light.target.position.set( - 5, 0, 0 );
  81. scene.add( light );
  82. scene.add( light.target );
  83. }
  84. {
  85. const objLoader = new OBJLoader();
  86. objLoader.load( 'resources/models/windmill_2/windmill.obj', ( root ) => {
  87. scene.add( root );
  88. } );
  89. }
  90. function resizeRendererToDisplaySize( renderer ) {
  91. const canvas = renderer.domElement;
  92. const width = canvas.clientWidth;
  93. const height = canvas.clientHeight;
  94. const needResize = canvas.width !== width || canvas.height !== height;
  95. if ( needResize ) {
  96. renderer.setSize( width, height, false );
  97. }
  98. return needResize;
  99. }
  100. function render() {
  101. if ( resizeRendererToDisplaySize( renderer ) ) {
  102. const canvas = renderer.domElement;
  103. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  104. camera.updateProjectionMatrix();
  105. }
  106. renderer.render( scene, camera );
  107. requestAnimationFrame( render );
  108. }
  109. requestAnimationFrame( render );
  110. }
  111. main();
  112. </script>
  113. </html>