webgl_loader_obj2_options.html 14 KB

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