webgl_loader_obj2_options.html 14 KB

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