webgl_loader_obj2_options.html 15 KB

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