verify.html 6.2 KB

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