webgl_loader_obj2.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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="http://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 {
  50. AmbientLight,
  51. DirectionalLight,
  52. GridHelper,
  53. PerspectiveCamera,
  54. Scene,
  55. Vector3,
  56. WebGLRenderer
  57. } from "../build/three.module.js";
  58. import { TrackballControls } from "./jsm/controls/TrackballControls.js";
  59. import { MTLLoader } from "./jsm/loaders/MTLLoader.js";
  60. import { OBJLoader2 } from "./jsm/loaders/OBJLoader2.js";
  61. import { MtlObjBridge } from "./jsm/loaders/obj2/bridge/MtlObjBridge.js";
  62. const OBJLoader2Example = function ( elementToBindTo ) {
  63. this.renderer = null;
  64. this.canvas = elementToBindTo;
  65. this.aspectRatio = 1;
  66. this.recalcAspectRatio();
  67. this.scene = null;
  68. this.cameraDefaults = {
  69. posCamera: new Vector3( 0.0, 175.0, 500.0 ),
  70. posCameraTarget: new Vector3( 0, 0, 0 ),
  71. near: 0.1,
  72. far: 10000,
  73. fov: 45
  74. };
  75. this.camera = null;
  76. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  77. this.controls = null;
  78. };
  79. OBJLoader2Example.prototype = {
  80. constructor: OBJLoader2Example,
  81. initGL: function () {
  82. this.renderer = new WebGLRenderer( {
  83. canvas: this.canvas,
  84. antialias: true,
  85. autoClear: true
  86. } );
  87. this.renderer.setClearColor( 0x050505 );
  88. this.scene = new Scene();
  89. this.camera = new PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  90. this.resetCamera();
  91. this.controls = new TrackballControls( this.camera, this.renderer.domElement );
  92. let ambientLight = new AmbientLight( 0x404040 );
  93. let directionalLight1 = new DirectionalLight( 0xC0C090 );
  94. let directionalLight2 = new DirectionalLight( 0xC0C090 );
  95. directionalLight1.position.set( - 100, - 50, 100 );
  96. directionalLight2.position.set( 100, 50, - 100 );
  97. this.scene.add( directionalLight1 );
  98. this.scene.add( directionalLight2 );
  99. this.scene.add( ambientLight );
  100. let helper = new GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  101. this.scene.add( helper );
  102. },
  103. initContent: function () {
  104. let modelName = 'female02';
  105. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  106. let scope = this;
  107. let objLoader2 = new OBJLoader2();
  108. let callbackOnLoad = function ( object3d ) {
  109. scope.scene.add( object3d );
  110. console.log( 'Loading complete: ' + modelName );
  111. scope._reportProgress( { detail: { text: '' } } );
  112. };
  113. let onLoadMtl = function ( mtlParseResult ) {
  114. objLoader2.setModelName( modelName );
  115. objLoader2.setLogging( true, true );
  116. objLoader2.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ) );
  117. objLoader2.load( 'models/obj/female02/female02.obj', callbackOnLoad, null, null, null );
  118. };
  119. let mtlLoader = new MTLLoader();
  120. mtlLoader.load( 'models/obj/female02/female02.mtl', onLoadMtl );
  121. },
  122. _reportProgress: function ( event ) {
  123. let output = '';
  124. if ( event.detail !== null && event.detail !== undefined && event.detail.text ) {
  125. output = event.detail.text;
  126. }
  127. console.log( 'Progress: ' + output );
  128. document.getElementById( 'feedback' ).innerHTML = output;
  129. },
  130. resizeDisplayGL: function () {
  131. this.controls.handleResize();
  132. this.recalcAspectRatio();
  133. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  134. this.updateCamera();
  135. },
  136. recalcAspectRatio: function () {
  137. this.aspectRatio = (this.canvas.offsetHeight === 0) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  138. },
  139. resetCamera: function () {
  140. this.camera.position.copy( this.cameraDefaults.posCamera );
  141. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  142. this.updateCamera();
  143. },
  144. updateCamera: function () {
  145. this.camera.aspect = this.aspectRatio;
  146. this.camera.lookAt( this.cameraTarget );
  147. this.camera.updateProjectionMatrix();
  148. },
  149. render: function () {
  150. if ( !this.renderer.autoClear ) this.renderer.clear();
  151. this.controls.update();
  152. this.renderer.render( this.scene, this.camera );
  153. }
  154. };
  155. let app = new OBJLoader2Example( document.getElementById( 'example' ) );
  156. let resizeWindow = function () {
  157. app.resizeDisplayGL();
  158. };
  159. let render = function () {
  160. requestAnimationFrame( render );
  161. app.render();
  162. };
  163. window.addEventListener( 'resize', resizeWindow, false );
  164. console.log( 'Starting initialisation phase...' );
  165. app.initGL();
  166. app.resizeDisplayGL();
  167. app.initContent();
  168. render();
  169. </script>
  170. </body>
  171. </html>