webgl_loader_obj2_options.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - OBJLoader2 usage options</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 usage options<br>Use module workers with Chromium based browser (80+)
  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 { GUI } from './jsm/libs/dat.gui.module.js';
  51. import { TrackballControls } from "./jsm/controls/TrackballControls.js";
  52. import { VertexNormalsHelper } from "./jsm/helpers/VertexNormalsHelper.js";
  53. import { MTLLoader } from "./jsm/loaders/MTLLoader.js";
  54. import { MtlObjBridge } from "./jsm/loaders/obj2/bridge/MtlObjBridge.js";
  55. import { OBJLoader2 } from "./jsm/loaders/OBJLoader2.js";
  56. import { OBJLoader2Parallel } from "./jsm/loaders/OBJLoader2Parallel.js";
  57. import { LoadedMeshUserOverride } from "./jsm/loaders/obj2/shared/MeshReceiver.js";
  58. const WWOBJLoader2Example = function ( elementToBindTo ) {
  59. this.renderer = null;
  60. this.canvas = elementToBindTo;
  61. this.aspectRatio = 1;
  62. this.recalcAspectRatio();
  63. this.scene = null;
  64. this.cameraDefaults = {
  65. posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
  66. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  67. near: 0.1,
  68. far: 10000,
  69. fov: 45
  70. };
  71. this.camera = null;
  72. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  73. this.controls = null;
  74. this.flatShading = false;
  75. this.doubleSide = false;
  76. this.useJsmWorker = false;
  77. this.loadCount = 6;
  78. this.cube = null;
  79. this.pivot = null;
  80. };
  81. WWOBJLoader2Example.prototype = {
  82. constructor: WWOBJLoader2Example,
  83. initGL: function () {
  84. this.renderer = new THREE.WebGLRenderer( {
  85. canvas: this.canvas,
  86. antialias: true,
  87. autoClear: true
  88. } );
  89. this.renderer.setClearColor( 0x050505 );
  90. this.scene = new THREE.Scene();
  91. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  92. this.resetCamera();
  93. this.controls = new TrackballControls( this.camera, this.renderer.domElement );
  94. let ambientLight = new THREE.AmbientLight( 0x404040 );
  95. let directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  96. let directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  97. directionalLight1.position.set( -100, -50, 100 );
  98. directionalLight2.position.set( 100, 50, -100 );
  99. this.scene.add( directionalLight1 );
  100. this.scene.add( directionalLight2 );
  101. this.scene.add( ambientLight );
  102. let helper = new THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  103. this.scene.add( helper );
  104. let geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  105. let material = new THREE.MeshNormalMaterial();
  106. this.cube = new THREE.Mesh( geometry, material );
  107. this.cube.position.set( 0, 0, 0 );
  108. this.scene.add( this.cube );
  109. this.pivot = new THREE.Object3D();
  110. this.pivot.name = 'Pivot';
  111. this.scene.add( this.pivot );
  112. },
  113. useParseMain: function () {
  114. let modelName = 'female02';
  115. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  116. let objLoader2 = new OBJLoader2()
  117. .setModelName( modelName );
  118. let scope = this;
  119. function onLoadMtl ( mtlParseResult ) {
  120. objLoader2.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ), true );
  121. let fileLoader = new THREE.FileLoader();
  122. fileLoader.setPath( '' );
  123. fileLoader.setResponseType( 'arraybuffer' );
  124. fileLoader.load( 'models/obj/female02/female02.obj',
  125. function ( content, message ) {
  126. let local = new THREE.Object3D();
  127. local.name = 'Pivot_female02';
  128. local.position.set( 75, 0, 0 );
  129. scope.pivot.add( local );
  130. local.add( objLoader2.parse( content ) );
  131. scope._reportProgress( { detail: { text: 'Loading of ' + modelName + ' completed: OBJLoader2#pase: Parsing completed' } } );
  132. scope.finalize();
  133. }
  134. );
  135. }
  136. let mtlLoader = new MTLLoader();
  137. mtlLoader.load( 'models/obj/female02/female02.mtl', onLoadMtl );
  138. },
  139. useParseParallel: function () {
  140. let modelName = 'female02_vertex' ;
  141. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  142. let local = new THREE.Object3D();
  143. local.name = 'Pivot_female02_vertex';
  144. local.position.set( -75, 0, 0 );
  145. this.pivot.add( local );
  146. let scope = this;
  147. function callbackOnLoad( object3d, message ) {
  148. local.add( object3d );
  149. scope._reportProgress( { detail: { text: 'Loading of ' + modelName + ' completed: ' + message } } );
  150. scope.finalize();
  151. }
  152. let materials = {
  153. tester: new THREE.MeshStandardMaterial()
  154. };
  155. let objLoader2Parallel = new OBJLoader2Parallel()
  156. .setModelName( modelName )
  157. .setJsmWorker( this.useJsmWorker, new URL( OBJLoader2Parallel.DEFAULT_JSM_WORKER_PATH, window.location.href ) )
  158. .setCallbackOnLoad( callbackOnLoad )
  159. .addMaterials( materials, true );
  160. let fileLoader = new THREE.FileLoader();
  161. fileLoader.setPath( '' );
  162. fileLoader.setResponseType( 'arraybuffer' );
  163. fileLoader.load( 'models/obj/female02/female02_vertex_colors.obj',
  164. function ( content ) {
  165. objLoader2Parallel.parse( content );
  166. }
  167. );
  168. },
  169. useLoadMain: function () {
  170. let modelName = 'male02';
  171. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  172. let objLoader2 = new OBJLoader2()
  173. .setModelName( modelName )
  174. .setUseIndices( true );
  175. let scope = this;
  176. function callbackOnLoad ( object3d, message ) {
  177. let local = new THREE.Object3D();
  178. local.name = 'Pivot_male02';
  179. local.position.set( 0, 0, -75 );
  180. scope.pivot.add( local );
  181. local.add( object3d );
  182. scope._reportProgress( { detail: { text: 'Loading of ' + modelName + ' completed: ' + message } } );
  183. scope.finalize();
  184. }
  185. function onLoadMtl ( mtlParseResult ) {
  186. objLoader2.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ), true );
  187. objLoader2.load( 'models/obj/male02/male02.obj', callbackOnLoad, null, null, null );
  188. }
  189. let mtlLoader = new MTLLoader();
  190. mtlLoader.load( 'models/obj/male02/male02.mtl', onLoadMtl );
  191. },
  192. useLoadParallel: function () {
  193. let modelName = 'WaltHead';
  194. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  195. let local = new THREE.Object3D();
  196. local.name = 'Pivot_WaltHead';
  197. local.position.set( -175, 50, 0 );
  198. let scale = 0.5;
  199. local.scale.set( scale, scale, scale );
  200. this.pivot.add( local );
  201. let objLoader2Parallel = new OBJLoader2Parallel()
  202. .setModelName( modelName )
  203. .setJsmWorker( this.useJsmWorker, new URL( OBJLoader2Parallel.DEFAULT_JSM_WORKER_PATH, window.location.href ) );
  204. let scope = this;
  205. function callbackOnLoad ( object3d, message ) {
  206. local.add( object3d );
  207. scope._reportProgress( { detail: { text: 'Loading of ' + modelName + ' completed: ' + message } } );
  208. scope.finalize();
  209. }
  210. function onLoadMtl ( mtlParseResult ) {
  211. objLoader2Parallel.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ), true );
  212. objLoader2Parallel.load( 'models/obj/walt/WaltHead.obj', callbackOnLoad );
  213. }
  214. let mtlLoader = new MTLLoader();
  215. mtlLoader.load( 'models/obj/walt/WaltHead.mtl', onLoadMtl );
  216. },
  217. useLoadMainFallback: function () {
  218. let local = new THREE.Object3D();
  219. local.name = 'Pivot_Cerberus';
  220. local.position.set( 0, 0, 100 );
  221. let scale = 50;
  222. local.scale.set( scale, scale, scale );
  223. this.pivot.add( local );
  224. let objLoader2Parallel = new OBJLoader2Parallel()
  225. .setModelName( local.name )
  226. .setExecuteParallel( false );
  227. let scope = this;
  228. function callbackOnLoad ( object3d, message ) {
  229. local.add( object3d );
  230. scope._reportProgress( { detail: { text: 'Loading of ' + objLoader2Parallel.modelName + ' completed: ' + message } } );
  231. scope.finalize();
  232. }
  233. objLoader2Parallel.load( 'models/obj/cerberus/Cerberus.obj', callbackOnLoad );
  234. },
  235. useLoadParallelMeshAlter: function () {
  236. let local = new THREE.Object3D();
  237. local.position.set( 175, -100, 0 );
  238. local.name = 'Pivot_ninjaHead';
  239. this.pivot.add( local );
  240. let objLoader2Parallel = new OBJLoader2Parallel()
  241. .setModelName( local.name )
  242. .setJsmWorker( this.useJsmWorker, new URL( OBJLoader2Parallel.DEFAULT_JSM_WORKER_PATH, window.location.href ) )
  243. .setBaseObject3d( local );
  244. // Configure WorkerExecutionSupport to not disregard worker after execution
  245. objLoader2Parallel.getWorkerExecutionSupport().setTerminateWorkerOnLoad( false );
  246. function callbackMeshAlter ( event ) {
  247. let override = new LoadedMeshUserOverride( false, true );
  248. let mesh = new THREE.Mesh( event.detail.bufferGeometry, event.detail.material );
  249. mesh.name = event.detail.meshName;
  250. let helper = new VertexNormalsHelper( mesh, 2, 0x00ff00 );
  251. helper.name = 'VertexNormalsHelper';
  252. override.addMesh( mesh );
  253. override.addMesh( helper );
  254. return override;
  255. }
  256. objLoader2Parallel.setCallbackOnMeshAlter( callbackMeshAlter );
  257. let scope = this;
  258. function callbackOnLoad ( object3d, message ) {
  259. scope._reportProgress( { detail: { text: 'Loading of ' + objLoader2Parallel.modelName + ' completed: ' + message } } );
  260. scope.finalize();
  261. }
  262. objLoader2Parallel.load( 'models/obj/ninja/ninjaHead_Low.obj', callbackOnLoad );
  263. },
  264. finalize: function () {
  265. this.loadCount--;
  266. if ( this.loadCount === 0 ) {
  267. this._reportProgress( { detail: { text: '' } } );
  268. }
  269. },
  270. _reportProgress: function( event ) {
  271. let output = '';
  272. if ( event.detail !== null && event.detail !== undefined && event.detail.text ) {
  273. output = event.detail.text;
  274. }
  275. console.log( 'Progress: ' + output );
  276. document.getElementById( 'feedback' ).innerHTML = output;
  277. },
  278. resizeDisplayGL: function () {
  279. this.controls.handleResize();
  280. this.recalcAspectRatio();
  281. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  282. this.updateCamera();
  283. },
  284. recalcAspectRatio: function () {
  285. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  286. },
  287. resetCamera: function () {
  288. this.camera.position.copy( this.cameraDefaults.posCamera );
  289. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  290. this.updateCamera();
  291. },
  292. updateCamera: function () {
  293. this.camera.aspect = this.aspectRatio;
  294. this.camera.lookAt( this.cameraTarget );
  295. this.camera.updateProjectionMatrix();
  296. },
  297. render: function () {
  298. if ( ! this.renderer.autoClear ) this.renderer.clear();
  299. this.controls.update();
  300. this.cube.rotation.x += 0.05;
  301. this.cube.rotation.y += 0.05;
  302. this.renderer.render( this.scene, this.camera );
  303. },
  304. alterShading: function () {
  305. let scope = this;
  306. scope.flatShading = ! scope.flatShading;
  307. console.log( scope.flatShading ? 'Enabling flat shading' : 'Enabling smooth shading');
  308. scope.traversalFunction = function ( material ) {
  309. material.flatShading = scope.flatShading;
  310. material.needsUpdate = true;
  311. };
  312. function scopeTraverse ( object3d ) {
  313. scope.traverseScene( object3d );
  314. }
  315. scope.pivot.traverse( scopeTraverse );
  316. },
  317. alterDouble: function () {
  318. let scope = this;
  319. scope.doubleSide = ! scope.doubleSide;
  320. console.log( scope.doubleSide ? 'Enabling THREE.DoubleSide materials' : 'Enabling THREE.FrontSide materials');
  321. scope.traversalFunction = function ( material ) {
  322. material.side = scope.doubleSide ? THREE.DoubleSide : THREE.FrontSide;
  323. };
  324. function scopeTraverse ( object3d ) {
  325. scope.traverseScene( object3d );
  326. }
  327. scope.pivot.traverse( scopeTraverse );
  328. },
  329. traverseScene: function ( object3d ) {
  330. if ( Array.isArray( object3d.material ) ) {
  331. let materials = object3d.material.materials;
  332. for ( let name in materials ) {
  333. if ( materials.hasOwnProperty( name ) ) this.traversalFunction( materials[ name ] );
  334. }
  335. } else if ( object3d.material ) {
  336. this.traversalFunction( object3d.material );
  337. }
  338. },
  339. executeLoading: function () {
  340. // Load a file with OBJLoader2.parse on main
  341. this.useParseMain();
  342. // Load a file with OBJLoader2Parallel.parse in parallel in worker
  343. this.useParseParallel();
  344. // Load a file with OBJLoader.load on main
  345. this.useLoadMain();
  346. // Load a file with OBJLoader2Parallel.load in parallel in worker
  347. this.useLoadParallel();
  348. // Load a file with OBJLoader2Parallel.load on main with fallback to OBJLoader2.parse
  349. this.useLoadMainFallback();
  350. // Load a file with OBJLoader2Parallel.load in parallel in worker and add normals during onMeshAlter
  351. this.useLoadParallelMeshAlter();
  352. }
  353. };
  354. let app = new WWOBJLoader2Example( document.getElementById( 'example' ) );
  355. let handleExecuteLoading;
  356. let wwObjLoader2Control = {
  357. flatShading: app.flatShading,
  358. doubleSide: app.doubleSide,
  359. useJsmWorker: app.useJsmWorker,
  360. blockEvent: function ( event ) {
  361. event.stopPropagation();
  362. },
  363. disableElement: function ( elementHandle ) {
  364. elementHandle.domElement.addEventListener( 'click', this.blockEvent, true );
  365. elementHandle.domElement.parentElement.style.pointerEvents = 'none';
  366. elementHandle.domElement.parentElement.style.opacity = 0.5;
  367. },
  368. executeLoading: function() {
  369. app.executeLoading();
  370. this.disableElement( handleExecuteLoading );
  371. },
  372. };
  373. let menuDiv = document.getElementById( 'dat' );
  374. let gui = new GUI( {
  375. autoPlace: false,
  376. width: 320
  377. } );
  378. menuDiv.appendChild( gui.domElement );
  379. let folderRenderingOptions = gui.addFolder( 'Rendering Options' );
  380. let controlFlat = folderRenderingOptions.add( wwObjLoader2Control, 'flatShading' ).name( 'Flat Shading' );
  381. controlFlat.onChange( function( value ) {
  382. console.log( 'Setting flatShading to: ' + value );
  383. app.alterShading();
  384. });
  385. let controlDouble = folderRenderingOptions.add( wwObjLoader2Control, 'doubleSide' ).name( 'Double Side Materials' );
  386. controlDouble.onChange( function( value ) {
  387. console.log( 'Setting doubleSide to: ' + value );
  388. app.alterDouble();
  389. });
  390. let folderObjLoader2ParallelOptions = gui.addFolder( 'OBJLoader2Parallel Options' );
  391. let controlJsmWorker = folderObjLoader2ParallelOptions.add( wwObjLoader2Control, 'useJsmWorker' ).name( 'Use Module Workers' );
  392. controlJsmWorker.onChange( function( value ) {
  393. console.log( 'Setting useJsmWorker to: ' + value );
  394. app.useJsmWorker = value;
  395. });
  396. let folderExecution = gui.addFolder( 'Execution' );
  397. handleExecuteLoading = folderExecution.add( wwObjLoader2Control, 'executeLoading' ).name( 'Run' );
  398. handleExecuteLoading.domElement.id = 'startButton';
  399. folderRenderingOptions.open();
  400. folderObjLoader2ParallelOptions.open();
  401. folderExecution.open();
  402. // init three.js example application
  403. function resizeWindow () {
  404. app.resizeDisplayGL();
  405. }
  406. function render () {
  407. requestAnimationFrame( render );
  408. app.render();
  409. }
  410. window.addEventListener( 'resize', resizeWindow, false );
  411. console.log( 'Starting initialisation phase...' );
  412. app.initGL();
  413. app.resizeDisplayGL();
  414. // kick render loop
  415. render();
  416. </script>
  417. </body>
  418. </html>