webgl_loader_obj2_options.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #000;
  11. color: #fff;
  12. margin: 0 0 0 0;
  13. padding: 0 0 0 0;
  14. border: none;
  15. cursor: default;
  16. }
  17. #info {
  18. color: #fff;
  19. position: absolute;
  20. top: 10px;
  21. width: 100%;
  22. text-align: center;
  23. z-index: 100;
  24. display:block;
  25. }
  26. #info a {
  27. color: #f00;
  28. font-weight: bold;
  29. text-decoration: underline;
  30. cursor: pointer
  31. }
  32. #glFullscreen {
  33. width: 100%;
  34. height: 100vh;
  35. min-width: 640px;
  36. min-height: 360px;
  37. position: relative;
  38. overflow: hidden;
  39. z-index: 0;
  40. }
  41. #example {
  42. width: 100%;
  43. height: 100%;
  44. top: 0;
  45. left: 0;
  46. background-color: #000000;
  47. }
  48. #feedback {
  49. color: darkorange;
  50. }
  51. #dat {
  52. user-select: none;
  53. position: absolute;
  54. left: 0;
  55. top: 0;
  56. z-Index: 200;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <div id="glFullscreen">
  62. <canvas id="example"></canvas>
  63. </div>
  64. <div id="dat">
  65. </div>
  66. <div id="info">
  67. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - OBJLoader2 usage options
  68. <div id="feedback"></div>
  69. </div>
  70. <script src="js/Detector.js"></script>
  71. <script src="../build/three.js"></script>
  72. <script src="js/controls/TrackballControls.js"></script>
  73. <script src="js/loaders/MTLLoader.js"></script>
  74. <script src="js/libs/dat.gui.min.js"></script>
  75. <script src="js/loaders/LoaderSupport.js"></script>
  76. <script src="js/loaders/OBJLoader2.js"></script>
  77. <script>
  78. 'use strict';
  79. var WWOBJLoader2Example = (function () {
  80. var Validator = THREE.LoaderSupport.Validator;
  81. function WWOBJLoader2Example( elementToBindTo ) {
  82. this.renderer = null;
  83. this.canvas = elementToBindTo;
  84. this.aspectRatio = 1;
  85. this.recalcAspectRatio();
  86. this.scene = null;
  87. this.cameraDefaults = {
  88. posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
  89. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  90. near: 0.1,
  91. far: 10000,
  92. fov: 45
  93. };
  94. this.camera = null;
  95. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  96. this.controls = null;
  97. this.flatShading = false;
  98. this.doubleSide = false;
  99. this.cube = null;
  100. this.pivot = null;
  101. }
  102. WWOBJLoader2Example.prototype.initGL = function () {
  103. this.renderer = new THREE.WebGLRenderer( {
  104. canvas: this.canvas,
  105. antialias: true,
  106. autoClear: true
  107. } );
  108. this.renderer.setClearColor( 0x050505 );
  109. this.scene = new THREE.Scene();
  110. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  111. this.resetCamera();
  112. this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );
  113. var ambientLight = new THREE.AmbientLight( 0x404040 );
  114. var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  115. var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  116. directionalLight1.position.set( -100, -50, 100 );
  117. directionalLight2.position.set( 100, 50, -100 );
  118. this.scene.add( directionalLight1 );
  119. this.scene.add( directionalLight2 );
  120. this.scene.add( ambientLight );
  121. var helper = new THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  122. this.scene.add( helper );
  123. var geometry = new THREE.BoxGeometry( 10, 10, 10 );
  124. var material = new THREE.MeshNormalMaterial();
  125. this.cube = new THREE.Mesh( geometry, material );
  126. this.cube.position.set( 0, 0, 0 );
  127. this.scene.add( this.cube );
  128. this.pivot = new THREE.Object3D();
  129. this.pivot.name = 'Pivot';
  130. this.scene.add( this.pivot );
  131. };
  132. WWOBJLoader2Example.prototype.useParseSync = function () {
  133. var modelName = 'female02';
  134. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  135. var scope = this;
  136. var objLoader = new THREE.OBJLoader2();
  137. var onLoadMtl = function ( materials ) {
  138. objLoader.setModelName( modelName );
  139. objLoader.setMaterials( materials );
  140. var fileLoader = new THREE.FileLoader();
  141. fileLoader.setPath( '' );
  142. fileLoader.setResponseType( 'arraybuffer' );
  143. fileLoader.load( 'models/obj/female02/female02.obj',
  144. function ( content ) {
  145. var local = new THREE.Object3D();
  146. local.name = 'Pivot_female02';
  147. local.position.set( 75, 0, 0 );
  148. scope.pivot.add( local );
  149. local.add( objLoader.parse( content ) );
  150. scope._reportProgress( { detail: { text: 'Loading complete: ' + modelName } } );
  151. }
  152. );
  153. };
  154. objLoader.loadMtl( 'models/obj/female02/female02.mtl', null, onLoadMtl );
  155. };
  156. WWOBJLoader2Example.prototype.useParseAsync = function () {
  157. var modelName = 'female02_vertex' ;
  158. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  159. var callbackOnLoad = function ( event ) {
  160. var local = new THREE.Object3D();
  161. local.name = 'Pivot_female02_vertex';
  162. local.position.set( -75, 0, 0 );
  163. scope.pivot.add( local );
  164. local.add( event.detail.loaderRootNode );
  165. scope._reportProgress( { detail: { text: 'Loading complete: ' + event.detail.modelName } } );
  166. };
  167. var scope = this;
  168. var objLoader = new THREE.OBJLoader2();
  169. objLoader.setModelName( modelName );
  170. var fileLoader = new THREE.FileLoader();
  171. fileLoader.setPath( '' );
  172. fileLoader.setResponseType( 'arraybuffer' );
  173. var filename = 'models/obj/female02/female02_vertex_colors.obj';
  174. fileLoader.load( filename,
  175. function ( content ) {
  176. objLoader.parseAsync( content, callbackOnLoad );
  177. scope._reportProgress( { detail: { text: 'File loading complete: ' + filename } } );
  178. }
  179. );
  180. };
  181. WWOBJLoader2Example.prototype.useLoadSync = function () {
  182. var modelName = 'male02';
  183. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  184. var scope = this;
  185. var objLoader = new THREE.OBJLoader2();
  186. var callbackOnLoad = function ( event ) {
  187. var local = new THREE.Object3D();
  188. local.name = 'Pivot_male02';
  189. local.position.set( 0, 0, -75 );
  190. scope.pivot.add( local );
  191. local.add( event.detail.loaderRootNode );
  192. scope._reportProgress( { detail: { text: 'Loading complete: ' + event.detail.modelName } } );
  193. };
  194. var onLoadMtl = function ( materials ) {
  195. objLoader.setModelName( modelName );
  196. objLoader.setMaterials( materials );
  197. objLoader.setUseIndices( true );
  198. objLoader.load( 'models/obj/male02/male02.obj', callbackOnLoad, null, null, null, false );
  199. };
  200. objLoader.loadMtl( 'models/obj/male02/male02.mtl', null, onLoadMtl );
  201. };
  202. WWOBJLoader2Example.prototype.useLoadAsync = function () {
  203. var modelName = 'WaltHead';
  204. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  205. var scope = this;
  206. var objLoader = new THREE.OBJLoader2();
  207. var callbackOnLoad = function ( event ) {
  208. objLoader.workerSupport.setTerminateRequested( true );
  209. var local = new THREE.Object3D();
  210. local.name = 'Pivot_WaltHead';
  211. local.position.set( -125, 50, 0 );
  212. var scale = 0.5;
  213. local.scale.set( scale, scale, scale );
  214. scope.pivot.add( local );
  215. local.add( event.detail.loaderRootNode );
  216. scope._reportProgress( { detail: { text: 'Loading complete: ' + event.detail.modelName } } );
  217. };
  218. var onLoadMtl = function ( materials ) {
  219. objLoader.setModelName( modelName );
  220. objLoader.setMaterials( materials );
  221. objLoader.terminateWorkerOnLoad = false;
  222. objLoader.load( 'models/obj/walt/WaltHead.obj', callbackOnLoad, null, null, null, true );
  223. };
  224. objLoader.loadMtl( 'models/obj/walt/WaltHead.mtl', null, onLoadMtl );
  225. };
  226. WWOBJLoader2Example.prototype.useRunSync = function () {
  227. var scope = this;
  228. var callbackOnLoad = function ( event ) {
  229. scope._reportProgress( { detail: { text: 'Loading complete: ' + event.detail.modelName } } );
  230. };
  231. var prepData = new THREE.LoaderSupport.PrepData( 'cerberus' );
  232. var local = new THREE.Object3D();
  233. local.position.set( 0, 0, 100 );
  234. local.scale.set( 50.0, 50.0, 50.0 );
  235. this.pivot.add( local );
  236. prepData.streamMeshesTo = local;
  237. prepData.addResource( new THREE.LoaderSupport.ResourceDescriptor( 'models/obj/cerberus/Cerberus.obj', 'OBJ' ) );
  238. var callbacks = prepData.getCallbacks();
  239. callbacks.setCallbackOnProgress( this._reportProgress );
  240. callbacks.setCallbackOnLoad( callbackOnLoad );
  241. var objLoader = new THREE.OBJLoader2();
  242. objLoader.run( prepData );
  243. };
  244. WWOBJLoader2Example.prototype.useRunAsyncMeshAlter = function () {
  245. var scope = this;
  246. var callbackOnLoad = function ( event ) {
  247. scope._reportProgress( { detail: { text: 'Loading complete: ' + event.detail.modelName } } );
  248. };
  249. var prepData = new THREE.LoaderSupport.PrepData( 'vive-controller' );
  250. var local = new THREE.Object3D();
  251. local.position.set( 125, 50, 0 );
  252. local.name = 'Pivot_vive-controller';
  253. this.pivot.add( local );
  254. prepData.streamMeshesTo = local;
  255. prepData.addResource( new THREE.LoaderSupport.ResourceDescriptor( 'models/obj/vive-controller/vr_controller_vive_1_5.obj', 'OBJ' ) );
  256. prepData.useAsync = true;
  257. var callbacks = prepData.getCallbacks();
  258. var callbackMeshAlter = function ( event ) {
  259. var override = new THREE.LoaderSupport.LoadedMeshUserOverride( false, true );
  260. var mesh = new THREE.Mesh( event.detail.bufferGeometry, event.detail.material );
  261. var scale = 200.0;
  262. mesh.scale.set( scale, scale, scale );
  263. mesh.name = event.detail.meshName;
  264. var helper = new THREE.VertexNormalsHelper( mesh, 2, 0x00ff00, 1 );
  265. helper.name = 'VertexNormalsHelper';
  266. override.addMesh( mesh );
  267. override.addMesh( helper );
  268. return override;
  269. };
  270. callbacks.setCallbackOnMeshAlter( callbackMeshAlter );
  271. callbacks.setCallbackOnProgress( this._reportProgress );
  272. callbacks.setCallbackOnLoad( callbackOnLoad );
  273. var objLoader = new THREE.OBJLoader2();
  274. objLoader.run( prepData );
  275. };
  276. WWOBJLoader2Example.prototype.finalize = function () {
  277. this._reportProgress( { detail: { text: '' } } );
  278. };
  279. WWOBJLoader2Example.prototype._reportProgress = function( event ) {
  280. var output = Validator.verifyInput( event.detail.text, '' );
  281. console.log( 'Progress: ' + output );
  282. document.getElementById( 'feedback' ).innerHTML = output;
  283. };
  284. WWOBJLoader2Example.prototype.resizeDisplayGL = function () {
  285. this.controls.handleResize();
  286. this.recalcAspectRatio();
  287. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  288. this.updateCamera();
  289. };
  290. WWOBJLoader2Example.prototype.recalcAspectRatio = function () {
  291. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  292. };
  293. WWOBJLoader2Example.prototype.resetCamera = function () {
  294. this.camera.position.copy( this.cameraDefaults.posCamera );
  295. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  296. this.updateCamera();
  297. };
  298. WWOBJLoader2Example.prototype.updateCamera = function () {
  299. this.camera.aspect = this.aspectRatio;
  300. this.camera.lookAt( this.cameraTarget );
  301. this.camera.updateProjectionMatrix();
  302. };
  303. WWOBJLoader2Example.prototype.render = function () {
  304. if ( ! this.renderer.autoClear ) this.renderer.clear();
  305. this.controls.update();
  306. this.cube.rotation.x += 0.05;
  307. this.cube.rotation.y += 0.05;
  308. this.renderer.render( this.scene, this.camera );
  309. };
  310. WWOBJLoader2Example.prototype.alterShading = function () {
  311. var scope = this;
  312. scope.flatShading = ! scope.flatShading;
  313. console.log( scope.flatShading ? 'Enabling flat shading' : 'Enabling smooth shading');
  314. scope.traversalFunction = function ( material ) {
  315. material.flatShading = scope.flatShading;
  316. material.needsUpdate = true;
  317. };
  318. var scopeTraverse = function ( object3d ) {
  319. scope.traverseScene( object3d );
  320. };
  321. scope.pivot.traverse( scopeTraverse );
  322. };
  323. WWOBJLoader2Example.prototype.alterDouble = function () {
  324. var scope = this;
  325. scope.doubleSide = ! scope.doubleSide;
  326. console.log( scope.doubleSide ? 'Enabling DoubleSide materials' : 'Enabling FrontSide materials');
  327. scope.traversalFunction = function ( material ) {
  328. material.side = scope.doubleSide ? THREE.DoubleSide : THREE.FrontSide;
  329. };
  330. var scopeTraverse = function ( object3d ) {
  331. scope.traverseScene( object3d );
  332. };
  333. scope.pivot.traverse( scopeTraverse );
  334. };
  335. WWOBJLoader2Example.prototype.traverseScene = function ( object3d ) {
  336. if ( object3d.material instanceof THREE.MultiMaterial ) {
  337. var materials = object3d.material.materials;
  338. for ( var name in materials ) {
  339. if ( materials.hasOwnProperty( name ) ) this.traversalFunction( materials[ name ] );
  340. }
  341. } else if ( object3d.material ) {
  342. this.traversalFunction( object3d.material );
  343. }
  344. };
  345. return WWOBJLoader2Example;
  346. })();
  347. var app = new WWOBJLoader2Example( document.getElementById( 'example' ) );
  348. var wwObjLoader2Control = {
  349. flatShading: app.flatShading,
  350. doubleSide: app.doubleSide
  351. };
  352. var menuDiv = document.getElementById( 'dat' );
  353. var gui = new dat.GUI( {
  354. autoPlace: false,
  355. width: 320
  356. } );
  357. menuDiv.appendChild( gui.domElement );
  358. var folderOptions = gui.addFolder( 'WWOBJLoader2 Options' );
  359. var controlFlat = folderOptions.add( wwObjLoader2Control, 'flatShading' ).name( 'Flat Shading' );
  360. controlFlat.onChange( function( value ) {
  361. console.log( 'Setting flatShading to: ' + value );
  362. app.alterShading();
  363. });
  364. var controlDouble = folderOptions.add( wwObjLoader2Control, 'doubleSide' ).name( 'Double Side Materials' );
  365. controlDouble.onChange( function( value ) {
  366. console.log( 'Setting doubleSide to: ' + value );
  367. app.alterDouble();
  368. });
  369. folderOptions.open();
  370. // init three.js example application
  371. var resizeWindow = function () {
  372. app.resizeDisplayGL();
  373. };
  374. var render = function () {
  375. requestAnimationFrame( render );
  376. app.render();
  377. };
  378. window.addEventListener( 'resize', resizeWindow, false );
  379. console.log( 'Starting initialisation phase...' );
  380. app.initGL();
  381. app.resizeDisplayGL();
  382. // kick render loop
  383. render();
  384. // Load a file with OBJLoader.parse synchronously
  385. app.useParseSync();
  386. // Load a file with OBJLoader.parseAsync asynchronously using a worker
  387. app.useParseAsync();
  388. // Load a file with OBJLoader.load synchronously
  389. app.useLoadSync();
  390. // Load a file with OBJLoader.load asynchronously
  391. app.useLoadAsync();
  392. // Load a file with OBJLoader.run synchronously
  393. app.useRunSync();
  394. // Load a file with OBJLoader.run asynchronously and add normals during onMeshAlter
  395. app.useRunAsyncMeshAlter();
  396. app.finalize();
  397. </script>
  398. </body>
  399. </html>