webgl_loader_obj2_options.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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
  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.cube = null;
  77. this.pivot = null;
  78. };
  79. WWOBJLoader2Example.prototype = {
  80. constructor: WWOBJLoader2Example,
  81. initGL: function () {
  82. this.renderer = new THREE.WebGLRenderer( {
  83. canvas: this.canvas,
  84. antialias: true,
  85. autoClear: true
  86. } );
  87. this.renderer.setClearColor( 0x050505 );
  88. this.scene = new THREE.Scene();
  89. this.camera = new THREE.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 THREE.AmbientLight( 0x404040 );
  93. let directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  94. let directionalLight2 = new THREE.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 THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  101. this.scene.add( helper );
  102. let geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  103. let material = new THREE.MeshNormalMaterial();
  104. this.cube = new THREE.Mesh( geometry, material );
  105. this.cube.position.set( 0, 0, 0 );
  106. this.scene.add( this.cube );
  107. this.pivot = new THREE.Object3D();
  108. this.pivot.name = 'Pivot';
  109. this.scene.add( this.pivot );
  110. },
  111. useParseMain: function () {
  112. let modelName = 'female02';
  113. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  114. let objLoader2 = new OBJLoader2()
  115. .setModelName( modelName );
  116. let scope = this;
  117. function onLoadMtl ( mtlParseResult ) {
  118. objLoader2.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ), true );
  119. let fileLoader = new THREE.FileLoader();
  120. fileLoader.setPath( '' );
  121. fileLoader.setResponseType( 'arraybuffer' );
  122. fileLoader.load( 'models/obj/female02/female02.obj',
  123. function ( content, message ) {
  124. let local = new THREE.Object3D();
  125. local.name = 'Pivot_female02';
  126. local.position.set( 75, 0, 0 );
  127. scope.pivot.add( local );
  128. local.add( objLoader2.parse( content ) );
  129. scope._reportProgress( { detail: { text: 'Loading of ' + modelName + 'completed: ' + message } } );
  130. }
  131. );
  132. }
  133. let mtlLoader = new MTLLoader();
  134. mtlLoader.load( 'models/obj/female02/female02.mtl', onLoadMtl );
  135. },
  136. useParseParallel: function () {
  137. let modelName = 'female02_vertex' ;
  138. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  139. let local = new THREE.Object3D();
  140. local.name = 'Pivot_female02_vertex';
  141. local.position.set( -75, 0, 0 );
  142. this.pivot.add( local );
  143. let scope = this;
  144. function callbackOnLoad( object3d, message ) {
  145. local.add( object3d );
  146. scope._reportProgress( { detail: { text: 'Loading of ' + modelName + 'completed: ' + message } } );
  147. }
  148. let materials = {
  149. tester: new THREE.MeshStandardMaterial()
  150. };
  151. let objLoader2Parallel = new OBJLoader2Parallel()
  152. .setModelName( modelName )
  153. .setCallbackOnLoad( callbackOnLoad )
  154. .addMaterials( materials, true );
  155. let fileLoader = new THREE.FileLoader();
  156. fileLoader.setPath( '' );
  157. fileLoader.setResponseType( 'arraybuffer' );
  158. fileLoader.load( 'models/obj/female02/female02_vertex_colors.obj',
  159. function ( content ) {
  160. objLoader2Parallel.parse( content );
  161. }
  162. );
  163. },
  164. useLoadMain: function () {
  165. let modelName = 'male02';
  166. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  167. let objLoader2 = new OBJLoader2()
  168. .setModelName( modelName )
  169. .setUseIndices( true );
  170. let scope = this;
  171. function callbackOnLoad ( object3d, message ) {
  172. let local = new THREE.Object3D();
  173. local.name = 'Pivot_male02';
  174. local.position.set( 0, 0, -75 );
  175. scope.pivot.add( local );
  176. local.add( object3d );
  177. scope._reportProgress( { detail: { text: 'Loading of ' + modelName + 'completed: ' + message } } );
  178. }
  179. function onLoadMtl ( mtlParseResult ) {
  180. objLoader2.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ), true );
  181. objLoader2.load( 'models/obj/male02/male02.obj', callbackOnLoad, null, null, null );
  182. }
  183. let mtlLoader = new MTLLoader();
  184. mtlLoader.load( 'models/obj/male02/male02.mtl', onLoadMtl );
  185. },
  186. useLoadParallel: function () {
  187. let modelName = 'WaltHead';
  188. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  189. let local = new THREE.Object3D();
  190. local.name = 'Pivot_WaltHead';
  191. local.position.set( -175, 50, 0 );
  192. let scale = 0.5;
  193. local.scale.set( scale, scale, scale );
  194. this.pivot.add( local );
  195. let objLoader2Parallel = new OBJLoader2Parallel()
  196. .setModelName( modelName );
  197. let scope = this;
  198. function callbackOnLoad ( object3d, message ) {
  199. local.add( object3d );
  200. scope._reportProgress( { detail: { text: 'Loading of ' + modelName + 'completed: ' + message } } );
  201. }
  202. function onLoadMtl ( mtlParseResult ) {
  203. objLoader2Parallel.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ), true );
  204. objLoader2Parallel.load( 'models/obj/walt/WaltHead.obj', callbackOnLoad );
  205. }
  206. let mtlLoader = new MTLLoader();
  207. mtlLoader.load( 'models/obj/walt/WaltHead.mtl', onLoadMtl );
  208. },
  209. useLoadMainFallback: function () {
  210. let local = new THREE.Object3D();
  211. local.name = 'Pivot_Cerberus';
  212. local.position.set( 0, 0, 100 );
  213. let scale = 50;
  214. local.scale.set( scale, scale, scale );
  215. this.pivot.add( local );
  216. let objLoader2Parallel = new OBJLoader2Parallel()
  217. .setModelName( local.name )
  218. .setExecuteParallel( false );
  219. let scope = this;
  220. function callbackOnLoad ( object3d, message ) {
  221. local.add( object3d );
  222. scope._reportProgress( { detail: { text: 'Loading of ' + objLoader2Parallel.modelName + 'completed: ' + message } } );
  223. }
  224. objLoader2Parallel.load( 'models/obj/cerberus/Cerberus.obj', callbackOnLoad );
  225. },
  226. useLoadParallelMeshAlter: function () {
  227. let local = new THREE.Object3D();
  228. local.position.set( 175, -100, 0 );
  229. local.name = 'Pivot_ninjaHead';
  230. this.pivot.add( local );
  231. let objLoader2Parallel = new OBJLoader2Parallel()
  232. .setModelName( local.name )
  233. .setBaseObject3d( local );
  234. // Configure WorkerExecutionSupport to not disregard worker after execution
  235. objLoader2Parallel.getWorkerExecutionSupport().setTerminateWorkerOnLoad( false );
  236. function callbackMeshAlter ( event ) {
  237. let override = new LoadedMeshUserOverride( false, true );
  238. let mesh = new THREE.Mesh( event.detail.bufferGeometry, event.detail.material );
  239. mesh.name = event.detail.meshName;
  240. let helper = new VertexNormalsHelper( mesh, 2, 0x00ff00, 1 );
  241. helper.name = 'VertexNormalsHelper';
  242. override.addMesh( mesh );
  243. override.addMesh( helper );
  244. return override;
  245. }
  246. objLoader2Parallel.setCallbackOnMeshAlter( callbackMeshAlter );
  247. let scope = this;
  248. function callbackOnLoad ( object3d, message ) {
  249. scope._reportProgress( { detail: { text: 'Loading of ' + objLoader2Parallel.modelName + 'completed: ' + message } } );
  250. }
  251. objLoader2Parallel.load( 'models/obj/ninja/ninjaHead_Low.obj', callbackOnLoad );
  252. },
  253. finalize: function () {
  254. this._reportProgress( { detail: { text: '' } } );
  255. },
  256. _reportProgress: function( event ) {
  257. let output = '';
  258. if ( event.detail !== null && event.detail !== undefined && event.detail.text ) {
  259. output = event.detail.text;
  260. }
  261. console.log( 'Progress: ' + output );
  262. document.getElementById( 'feedback' ).innerHTML = output;
  263. },
  264. resizeDisplayGL: function () {
  265. this.controls.handleResize();
  266. this.recalcAspectRatio();
  267. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  268. this.updateCamera();
  269. },
  270. recalcAspectRatio: function () {
  271. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  272. },
  273. resetCamera: function () {
  274. this.camera.position.copy( this.cameraDefaults.posCamera );
  275. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  276. this.updateCamera();
  277. },
  278. updateCamera: function () {
  279. this.camera.aspect = this.aspectRatio;
  280. this.camera.lookAt( this.cameraTarget );
  281. this.camera.updateProjectionMatrix();
  282. },
  283. render: function () {
  284. if ( ! this.renderer.autoClear ) this.renderer.clear();
  285. this.controls.update();
  286. this.cube.rotation.x += 0.05;
  287. this.cube.rotation.y += 0.05;
  288. this.renderer.render( this.scene, this.camera );
  289. },
  290. alterShading: function () {
  291. let scope = this;
  292. scope.flatShading = ! scope.flatShading;
  293. console.log( scope.flatShading ? 'Enabling flat shading' : 'Enabling smooth shading');
  294. scope.traversalFunction = function ( material ) {
  295. material.flatShading = scope.flatShading;
  296. material.needsUpdate = true;
  297. };
  298. function scopeTraverse ( object3d ) {
  299. scope.traverseScene( object3d );
  300. }
  301. scope.pivot.traverse( scopeTraverse );
  302. },
  303. alterDouble: function () {
  304. let scope = this;
  305. scope.doubleSide = ! scope.doubleSide;
  306. console.log( scope.doubleSide ? 'Enabling THREE.DoubleSide materials' : 'Enabling THREE.FrontSide materials');
  307. scope.traversalFunction = function ( material ) {
  308. material.side = scope.doubleSide ? THREE.DoubleSide : THREE.FrontSide;
  309. };
  310. function scopeTraverse ( object3d ) {
  311. scope.traverseScene( object3d );
  312. }
  313. scope.pivot.traverse( scopeTraverse );
  314. },
  315. traverseScene: function ( object3d ) {
  316. if ( Array.isArray( object3d.material ) ) {
  317. let materials = object3d.material.materials;
  318. for ( let name in materials ) {
  319. if ( materials.hasOwnProperty( name ) ) this.traversalFunction( materials[ name ] );
  320. }
  321. } else if ( object3d.material ) {
  322. this.traversalFunction( object3d.material );
  323. }
  324. }
  325. };
  326. let app = new WWOBJLoader2Example( document.getElementById( 'example' ) );
  327. let wwObjLoader2Control = {
  328. flatShading: app.flatShading,
  329. doubleSide: app.doubleSide
  330. };
  331. let menuDiv = document.getElementById( 'dat' );
  332. let gui = new GUI( {
  333. autoPlace: false,
  334. width: 320
  335. } );
  336. menuDiv.appendChild( gui.domElement );
  337. let folderOptions = gui.addFolder( 'WWOBJLoader2 Options' );
  338. let controlFlat = folderOptions.add( wwObjLoader2Control, 'flatShading' ).name( 'Flat Shading' );
  339. controlFlat.onChange( function( value ) {
  340. console.log( 'Setting flatShading to: ' + value );
  341. app.alterShading();
  342. });
  343. let controlDouble = folderOptions.add( wwObjLoader2Control, 'doubleSide' ).name( 'Double Side Materials' );
  344. controlDouble.onChange( function( value ) {
  345. console.log( 'Setting doubleSide to: ' + value );
  346. app.alterDouble();
  347. });
  348. folderOptions.open();
  349. // init three.js example application
  350. function resizeWindow () {
  351. app.resizeDisplayGL();
  352. }
  353. function render () {
  354. requestAnimationFrame( render );
  355. app.render();
  356. }
  357. window.addEventListener( 'resize', resizeWindow, false );
  358. console.log( 'Starting initialisation phase...' );
  359. app.initGL();
  360. app.resizeDisplayGL();
  361. // kick render loop
  362. render();
  363. // Load a file with OBJLoader2.parse on main
  364. app.useParseMain();
  365. // Load a file with OBJLoader2Parallel.parse in parallel in worker
  366. app.useParseParallel();
  367. // Load a file with OBJLoader.load on main
  368. app.useLoadMain();
  369. // Load a file with OBJLoader2Parallel.load in parallel in worker
  370. app.useLoadParallel();
  371. // Load a file with OBJLoader2Parallel.load on main with fallback to OBJLoader2.parse
  372. app.useLoadMainFallback();
  373. // Load a file with OBJLoader2Parallel.load in parallel in worker and add normals during onMeshAlter
  374. app.useLoadParallelMeshAlter();
  375. app.finalize();
  376. </script>
  377. </body>
  378. </html>