webgl_loader_obj2_options.html 16 KB

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