webgl_loader_obj2_options.html 14 KB

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