webgl_loader_obj2_options.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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="http://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 {
  50. AmbientLight,
  51. DirectionalLight,
  52. GridHelper,
  53. PerspectiveCamera,
  54. Scene,
  55. Vector3,
  56. BoxBufferGeometry,
  57. WebGLRenderer,
  58. Object3D,
  59. MeshNormalMaterial,
  60. Mesh,
  61. VertexNormalsHelper,
  62. FileLoader,
  63. DoubleSide,
  64. FrontSide
  65. } from "../build/three.module.js";
  66. import { GUI } from './jsm/libs/dat.gui.module.js';
  67. import { TrackballControls } from "./jsm/controls/TrackballControls.js";
  68. import { MTLLoader } from "./jsm/loaders/MTLLoader.js";
  69. import { MtlObjBridge } from "./jsm/loaders/obj2/bridge/MtlObjBridge.js";
  70. import { OBJLoader2 } from "./jsm/loaders/OBJLoader2.js";
  71. import { OBJLoader2Parallel } from "./jsm/loaders/OBJLoader2Parallel.js";
  72. import { LoadedMeshUserOverride } from "./jsm/loaders/obj2/shared/MeshReceiver.js";
  73. const WWOBJLoader2Example = function ( elementToBindTo ) {
  74. this.renderer = null;
  75. this.canvas = elementToBindTo;
  76. this.aspectRatio = 1;
  77. this.recalcAspectRatio();
  78. this.scene = null;
  79. this.cameraDefaults = {
  80. posCamera: new Vector3( 0.0, 175.0, 500.0 ),
  81. posCameraTarget: new Vector3( 0, 0, 0 ),
  82. near: 0.1,
  83. far: 10000,
  84. fov: 45
  85. };
  86. this.camera = null;
  87. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  88. this.controls = null;
  89. this.flatShading = false;
  90. this.doubleSide = false;
  91. this.cube = null;
  92. this.pivot = null;
  93. };
  94. WWOBJLoader2Example.prototype = {
  95. constructor: WWOBJLoader2Example,
  96. initGL: function () {
  97. this.renderer = new WebGLRenderer( {
  98. canvas: this.canvas,
  99. antialias: true,
  100. autoClear: true
  101. } );
  102. this.renderer.setClearColor( 0x050505 );
  103. this.scene = new Scene();
  104. this.camera = new PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  105. this.resetCamera();
  106. this.controls = new TrackballControls( this.camera, this.renderer.domElement );
  107. let ambientLight = new AmbientLight( 0x404040 );
  108. let directionalLight1 = new DirectionalLight( 0xC0C090 );
  109. let directionalLight2 = new DirectionalLight( 0xC0C090 );
  110. directionalLight1.position.set( -100, -50, 100 );
  111. directionalLight2.position.set( 100, 50, -100 );
  112. this.scene.add( directionalLight1 );
  113. this.scene.add( directionalLight2 );
  114. this.scene.add( ambientLight );
  115. let helper = new GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  116. this.scene.add( helper );
  117. let geometry = new BoxBufferGeometry( 10, 10, 10 );
  118. let material = new MeshNormalMaterial();
  119. this.cube = new Mesh( geometry, material );
  120. this.cube.position.set( 0, 0, 0 );
  121. this.scene.add( this.cube );
  122. this.pivot = new Object3D();
  123. this.pivot.name = 'Pivot';
  124. this.scene.add( this.pivot );
  125. },
  126. useParseMain: function () {
  127. let modelName = 'female02';
  128. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  129. let objLoader2 = new OBJLoader2()
  130. .setModelName( modelName );
  131. let scope = this;
  132. function onLoadMtl ( mtlParseResult ) {
  133. objLoader2.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ) );
  134. let fileLoader = new FileLoader();
  135. fileLoader.setPath( '' );
  136. fileLoader.setResponseType( 'arraybuffer' );
  137. fileLoader.load( 'models/obj/female02/female02.obj',
  138. function ( content ) {
  139. let local = new Object3D();
  140. local.name = 'Pivot_female02';
  141. local.position.set( 75, 0, 0 );
  142. scope.pivot.add( local );
  143. local.add( objLoader2.parse( content ) );
  144. scope._reportProgress( { detail: { text: 'Loading complete: ' + modelName } } );
  145. }
  146. );
  147. }
  148. let mtlLoader = new MTLLoader();
  149. mtlLoader.load( 'models/obj/female02/female02.mtl', onLoadMtl );
  150. },
  151. useParseParallel: function () {
  152. let modelName = 'female02_vertex' ;
  153. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  154. let scope = this;
  155. let objLoader2Parallel = new OBJLoader2Parallel();
  156. objLoader2Parallel.setModelName( modelName );
  157. function callbackOnLoad ( message ) {
  158. scope.scene.add( objLoader2Parallel.baseObject3d );
  159. scope._reportProgress( { detail: { text: 'Loading complete: ' + message } } );
  160. }
  161. let filename = 'models/obj/female02/female02_vertex_colors.obj';
  162. objLoader2Parallel.load( filename, callbackOnLoad );
  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 ) {
  172. let local = new 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 complete: ' + objLoader2.modelName } } );
  178. }
  179. function onLoadMtl ( mtlParseResult ) {
  180. objLoader2.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ) );
  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 Object3D();
  190. local.name = 'Pivot_WaltHead';
  191. local.position.set( -125, 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. .setBaseObject3d( local );
  198. let scope = this;
  199. function callbackOnLoad ( message ) {
  200. scope._reportProgress( { detail: { text: 'Loading complete: ' + message } } );
  201. }
  202. function onLoadMtl ( mtlParseResult ) {
  203. objLoader2Parallel.addMaterials( MtlObjBridge.addMaterialsFromMtlLoader( mtlParseResult ) );
  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 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 ) {
  221. local.add( object3d );
  222. scope._reportProgress( { detail: { text: 'Loading complete: ' + local.name } } );
  223. }
  224. objLoader2Parallel.load( 'models/obj/cerberus/Cerberus.obj', callbackOnLoad );
  225. },
  226. useLoadParallelMeshAlter: function () {
  227. let local = new Object3D();
  228. local.position.set( 125, 50, 0 );
  229. local.name = 'Pivot_vive-controller';
  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 Mesh( event.detail.bufferGeometry, event.detail.material );
  239. let scale = 200.0;
  240. mesh.scale.set( scale, scale, scale );
  241. mesh.name = event.detail.meshName;
  242. let helper = new VertexNormalsHelper( mesh, 2, 0x00ff00, 1 );
  243. helper.name = 'VertexNormalsHelper';
  244. override.addMesh( mesh );
  245. override.addMesh( helper );
  246. return override;
  247. }
  248. objLoader2Parallel._setCallbacks( null, callbackMeshAlter );
  249. let scope = this;
  250. function callbackOnLoad ( message ) {
  251. scope._reportProgress( { detail: { text: 'Loading complete: ' + message } } );
  252. }
  253. objLoader2Parallel.load( 'models/obj/vive-controller/vr_controller_vive_1_5.obj', callbackOnLoad );
  254. },
  255. finalize: function () {
  256. this._reportProgress( { detail: { text: '' } } );
  257. },
  258. _reportProgress: function( event ) {
  259. let output = '';
  260. if ( event.detail !== null && event.detail !== undefined && event.detail.text ) {
  261. output = event.detail.text;
  262. }
  263. console.log( 'Progress: ' + output );
  264. document.getElementById( 'feedback' ).innerHTML = output;
  265. },
  266. resizeDisplayGL: function () {
  267. this.controls.handleResize();
  268. this.recalcAspectRatio();
  269. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  270. this.updateCamera();
  271. },
  272. recalcAspectRatio: function () {
  273. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  274. },
  275. resetCamera: function () {
  276. this.camera.position.copy( this.cameraDefaults.posCamera );
  277. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  278. this.updateCamera();
  279. },
  280. updateCamera: function () {
  281. this.camera.aspect = this.aspectRatio;
  282. this.camera.lookAt( this.cameraTarget );
  283. this.camera.updateProjectionMatrix();
  284. },
  285. render: function () {
  286. if ( ! this.renderer.autoClear ) this.renderer.clear();
  287. this.controls.update();
  288. this.cube.rotation.x += 0.05;
  289. this.cube.rotation.y += 0.05;
  290. this.renderer.render( this.scene, this.camera );
  291. },
  292. alterShading: function () {
  293. let scope = this;
  294. scope.flatShading = ! scope.flatShading;
  295. console.log( scope.flatShading ? 'Enabling flat shading' : 'Enabling smooth shading');
  296. scope.traversalFunction = function ( material ) {
  297. material.flatShading = scope.flatShading;
  298. material.needsUpdate = true;
  299. };
  300. function scopeTraverse ( object3d ) {
  301. scope.traverseScene( object3d );
  302. }
  303. scope.pivot.traverse( scopeTraverse );
  304. },
  305. alterDouble: function () {
  306. let scope = this;
  307. scope.doubleSide = ! scope.doubleSide;
  308. console.log( scope.doubleSide ? 'Enabling DoubleSide materials' : 'Enabling FrontSide materials');
  309. scope.traversalFunction = function ( material ) {
  310. material.side = scope.doubleSide ? DoubleSide : FrontSide;
  311. };
  312. function scopeTraverse ( object3d ) {
  313. scope.traverseScene( object3d );
  314. }
  315. scope.pivot.traverse( scopeTraverse );
  316. },
  317. traverseScene: function ( object3d ) {
  318. if ( Array.isArray( object3d.material ) ) {
  319. let materials = object3d.material.materials;
  320. for ( let name in materials ) {
  321. if ( materials.hasOwnProperty( name ) ) this.traversalFunction( materials[ name ] );
  322. }
  323. } else if ( object3d.material ) {
  324. this.traversalFunction( object3d.material );
  325. }
  326. }
  327. };
  328. let app = new WWOBJLoader2Example( document.getElementById( 'example' ) );
  329. let wwObjLoader2Control = {
  330. flatShading: app.flatShading,
  331. doubleSide: app.doubleSide
  332. };
  333. let menuDiv = document.getElementById( 'dat' );
  334. let gui = new GUI( {
  335. autoPlace: false,
  336. width: 320
  337. } );
  338. menuDiv.appendChild( gui.domElement );
  339. let folderOptions = gui.addFolder( 'WWOBJLoader2 Options' );
  340. let controlFlat = folderOptions.add( wwObjLoader2Control, 'flatShading' ).name( 'Flat Shading' );
  341. controlFlat.onChange( function( value ) {
  342. console.log( 'Setting flatShading to: ' + value );
  343. app.alterShading();
  344. });
  345. let controlDouble = folderOptions.add( wwObjLoader2Control, 'doubleSide' ).name( 'Double Side Materials' );
  346. controlDouble.onChange( function( value ) {
  347. console.log( 'Setting doubleSide to: ' + value );
  348. app.alterDouble();
  349. });
  350. folderOptions.open();
  351. // init three.js example application
  352. function resizeWindow () {
  353. app.resizeDisplayGL();
  354. }
  355. function render () {
  356. requestAnimationFrame( render );
  357. app.render();
  358. }
  359. window.addEventListener( 'resize', resizeWindow, false );
  360. console.log( 'Starting initialisation phase...' );
  361. app.initGL();
  362. app.resizeDisplayGL();
  363. // kick render loop
  364. render();
  365. // Load a file with OBJLoader2.parse on main
  366. app.useParseMain();
  367. // Load a file with OBJLoader2Parallel.parse in parallel in worker
  368. app.useParseParallel();
  369. // Load a file with OBJLoader.load on main
  370. app.useLoadMain();
  371. // Load a file with OBJLoader2Parallel.load in parallel in worker
  372. app.useLoadParallel();
  373. // Load a file with OBJLoader2Parallel.load on main with fallback to OBJLoader2.parse
  374. app.useLoadMainFallback();
  375. // Load a file with OBJLoader2Parallel.load in parallel in worker and add normals during onMeshAlter
  376. app.useLoadParallelMeshAlter();
  377. app.finalize();
  378. </script>
  379. </body>
  380. </html>