webgl_loader_obj2.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - OBJLoader2 basic usage</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. #glFullscreen {
  10. width: 100%;
  11. height: 100vh;
  12. min-width: 640px;
  13. min-height: 360px;
  14. position: relative;
  15. overflow: hidden;
  16. z-index: 0;
  17. }
  18. #example {
  19. width: 100%;
  20. height: 100%;
  21. top: 0;
  22. left: 0;
  23. background-color: #000000;
  24. }
  25. #feedback {
  26. color: darkorange;
  27. }
  28. #dat {
  29. user-select: none;
  30. position: absolute;
  31. left: 0;
  32. top: 0;
  33. z-Index: 200;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="glFullscreen">
  39. <canvas id="example"></canvas>
  40. </div>
  41. <div id="dat">
  42. </div>
  43. <div id="info">
  44. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - OBJLoader2 direct loader test
  45. <div id="feedback"></div>
  46. </div>
  47. <script type="module">
  48. 'use strict';
  49. import * as THREE from '../build/three.module.js';
  50. import { TrackballControls } from "./jsm/controls/TrackballControls.js";
  51. import { MTLLoader } from "./jsm/loaders/MTLLoader.js";
  52. import { OBJLoader2 } from "./jsm/loaders/OBJLoader2.js";
  53. import { MtlObjBridge } from "./jsm/loaders/obj2/bridge/MtlObjBridge.js";
  54. const OBJLoader2Example = function ( elementToBindTo ) {
  55. this.renderer = null;
  56. this.canvas = elementToBindTo;
  57. this.aspectRatio = 1;
  58. this.recalcAspectRatio();
  59. this.scene = null;
  60. this.cameraDefaults = {
  61. posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
  62. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  63. near: 0.1,
  64. far: 10000,
  65. fov: 45
  66. };
  67. this.camera = null;
  68. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  69. this.controls = null;
  70. };
  71. OBJLoader2Example.prototype = {
  72. constructor: OBJLoader2Example,
  73. initGL: function () {
  74. this.renderer = new THREE.WebGLRenderer( {
  75. canvas: this.canvas,
  76. antialias: true,
  77. autoClear: true
  78. } );
  79. this.renderer.setClearColor( 0x050505 );
  80. this.scene = new THREE.Scene();
  81. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  82. this.resetCamera();
  83. this.controls = new TrackballControls( this.camera, this.renderer.domElement );
  84. let ambientLight = new THREE.AmbientLight( 0x404040 );
  85. let directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  86. let directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  87. directionalLight1.position.set( - 100, - 50, 100 );
  88. directionalLight2.position.set( 100, 50, - 100 );
  89. this.scene.add( directionalLight1 );
  90. this.scene.add( directionalLight2 );
  91. this.scene.add( ambientLight );
  92. let helper = new THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  93. this.scene.add( helper );
  94. },
  95. initContent: function () {
  96. let modelName = 'female02';
  97. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  98. let scope = this;
  99. let objLoader2 = new OBJLoader2();
  100. let callbackOnLoad = function ( object3d ) {
  101. scope.scene.add( object3d );
  102. console.log( 'Loading complete: ' + modelName );
  103. scope._reportProgress( { detail: { text: '' } } );
  104. };
  105. let onLoadMtl = function ( mtlParseResult ) {
  106. objLoader2.setModelName( modelName );
  107. objLoader2.setLogging( true, true );
  108. objLoader2.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ), true );
  109. objLoader2.load( 'models/obj/female02/female02.obj', callbackOnLoad, null, null, null );
  110. };
  111. let mtlLoader = new MTLLoader();
  112. mtlLoader.load( 'models/obj/female02/female02.mtl', onLoadMtl );
  113. },
  114. _reportProgress: function ( event ) {
  115. let output = '';
  116. if ( event.detail !== null && event.detail !== undefined && event.detail.text ) {
  117. output = event.detail.text;
  118. }
  119. console.log( 'Progress: ' + output );
  120. document.getElementById( 'feedback' ).innerHTML = output;
  121. },
  122. resizeDisplayGL: function () {
  123. this.controls.handleResize();
  124. this.recalcAspectRatio();
  125. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  126. this.updateCamera();
  127. },
  128. recalcAspectRatio: function () {
  129. this.aspectRatio = (this.canvas.offsetHeight === 0) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  130. },
  131. resetCamera: function () {
  132. this.camera.position.copy( this.cameraDefaults.posCamera );
  133. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  134. this.updateCamera();
  135. },
  136. updateCamera: function () {
  137. this.camera.aspect = this.aspectRatio;
  138. this.camera.lookAt( this.cameraTarget );
  139. this.camera.updateProjectionMatrix();
  140. },
  141. render: function () {
  142. if ( !this.renderer.autoClear ) this.renderer.clear();
  143. this.controls.update();
  144. this.renderer.render( this.scene, this.camera );
  145. }
  146. };
  147. let app = new OBJLoader2Example( document.getElementById( 'example' ) );
  148. let resizeWindow = function () {
  149. app.resizeDisplayGL();
  150. };
  151. let render = function () {
  152. requestAnimationFrame( render );
  153. app.render();
  154. };
  155. window.addEventListener( 'resize', resizeWindow, false );
  156. console.log( 'Starting initialisation phase...' );
  157. app.initGL();
  158. app.resizeDisplayGL();
  159. app.initContent();
  160. render();
  161. </script>
  162. </body>
  163. </html>