verify.html 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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="http://threejs.org" target="_blank" rel="noopener">three.js</a> - OBJLoader2/OBJLoader verification
  68. <div id="feedback"></div>
  69. </div>
  70. <script src="../../../js/WebGL.js"></script>
  71. <script src="../../../js/libs/dat.gui.min.js"></script>
  72. <script type="module">
  73. 'use strict';
  74. import {
  75. AmbientLight,
  76. DirectionalLight,
  77. GridHelper,
  78. PerspectiveCamera,
  79. Scene,
  80. Vector3,
  81. WebGLRenderer
  82. } from "../../../../build/three.module.js";
  83. import { TrackballControls } from "../../../jsm/controls/TrackballControls.js";
  84. import { MTLLoader } from "../../../jsm/loaders/MTLLoader.js";
  85. import { MtlObjBridge } from "../../../jsm/loaders/bridge/MtlObjBridge.js";
  86. import { OBJLoader } from "../../../jsm/loaders/OBJLoader.js";
  87. import { OBJLoader2 } from "../../../jsm/loaders/OBJLoader2.js";
  88. const OBJLoaderVerify = function ( elementToBindTo ) {
  89. this.renderer = null;
  90. this.canvas = elementToBindTo;
  91. this.aspectRatio = 1;
  92. this.recalcAspectRatio();
  93. this.scene = null;
  94. this.cameraDefaults = {
  95. posCamera: new Vector3( 0.0, 175.0, 500.0 ),
  96. posCameraTarget: new Vector3( 0, 0, 0 ),
  97. near: 0.1,
  98. far: 10000,
  99. fov: 45
  100. };
  101. this.camera = null;
  102. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  103. this.controls = null;
  104. };
  105. OBJLoaderVerify.prototype = {
  106. constructor: OBJLoaderVerify,
  107. initGL: function () {
  108. this.renderer = new WebGLRenderer( {
  109. canvas: this.canvas,
  110. antialias: true,
  111. autoClear: true
  112. } );
  113. this.renderer.setClearColor( 0x050505 );
  114. this.scene = new Scene();
  115. this.camera = new PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  116. this.resetCamera();
  117. this.controls = new TrackballControls( this.camera, this.renderer.domElement );
  118. let ambientLight = new AmbientLight( 0x404040 );
  119. let directionalLight1 = new DirectionalLight( 0xC0C090 );
  120. let directionalLight2 = new DirectionalLight( 0xC0C090 );
  121. directionalLight1.position.set( -100, -50, 100 );
  122. directionalLight2.position.set( 100, 50, -100 );
  123. this.scene.add( directionalLight1 );
  124. this.scene.add( directionalLight2 );
  125. this.scene.add( ambientLight );
  126. let helper = new GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  127. this.scene.add( helper );
  128. },
  129. initContent: function () {
  130. let modelName = 'verificationCubes';
  131. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  132. let objLoader = new OBJLoader();
  133. let objLoader2 = new OBJLoader2();
  134. objLoader2.setModelName( modelName );
  135. objLoader2.setLogging( true, false );
  136. objLoader2.setUseOAsMesh( true );
  137. let scope = this;
  138. let callbackOnLoad = function ( object3d ) {
  139. scope.scene.add( object3d );
  140. console.log( 'Loading complete: ' + modelName );
  141. scope._reportProgress( { detail: { text: '' } } );
  142. };
  143. let onLoadMtl = function ( mtlParseResult ) {
  144. objLoader.setMaterials( mtlParseResult );
  145. objLoader.load( './verify.obj', function ( object ) {
  146. object.position.y = -100;
  147. scope.scene.add( object );
  148. } );
  149. objLoader2.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ) );
  150. objLoader2.load( './verify.obj', callbackOnLoad, null, null, null );
  151. };
  152. let mtlLoader = new MTLLoader();
  153. mtlLoader.load( './verify.mtl', onLoadMtl );
  154. },
  155. _reportProgress: function( event ) {
  156. let output = ( event.detail !== undefined && event.detail !== null && event.detail.text !== undefined && event.detail.text !== null ) ? event.detail.text : '';
  157. console.log( 'Progress: ' + output );
  158. document.getElementById( 'feedback' ).innerHTML = output;
  159. },
  160. resizeDisplayGL: function () {
  161. this.controls.handleResize();
  162. this.recalcAspectRatio();
  163. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  164. this.updateCamera();
  165. },
  166. recalcAspectRatio: function () {
  167. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  168. },
  169. resetCamera: function () {
  170. this.camera.position.copy( this.cameraDefaults.posCamera );
  171. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  172. this.updateCamera();
  173. },
  174. updateCamera: function () {
  175. this.camera.aspect = this.aspectRatio;
  176. this.camera.lookAt( this.cameraTarget );
  177. this.camera.updateProjectionMatrix();
  178. },
  179. render: function () {
  180. if ( ! this.renderer.autoClear ) this.renderer.clear();
  181. this.controls.update();
  182. this.renderer.render( this.scene, this.camera );
  183. }
  184. };
  185. let app = new OBJLoaderVerify( document.getElementById( 'example' ) );
  186. let resizeWindow = function () {
  187. app.resizeDisplayGL();
  188. };
  189. let render = function () {
  190. requestAnimationFrame( render );
  191. app.render();
  192. };
  193. window.addEventListener( 'resize', resizeWindow, false );
  194. console.log( 'Starting initialisation phase...' );
  195. app.initGL();
  196. app.resizeDisplayGL();
  197. app.initContent();
  198. render();
  199. </script>
  200. </body>
  201. </html>