webgl_loader_obj2_ww.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - WWOBJLoader2</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. #fileUploadInput {
  59. display: none;
  60. }
  61. </style>
  62. </head>
  63. <body>
  64. <div id="glFullscreen">
  65. <canvas id="example"></canvas>
  66. </div>
  67. <div id="dat">
  68. </div>
  69. <div id="info">
  70. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - OBJLoader2 direct loader test
  71. <div id="feedback"></div>
  72. </div>
  73. <input id="fileUploadInput" type="file" name="files[]" multiple accept=".obj,.mtl" />
  74. <script src="js/Detector.js"></script>
  75. <script src="../build/three.js"></script>
  76. <script src="js/controls/TrackballControls.js"></script>
  77. <script src="js/loaders/MTLLoader.js"></script>
  78. <script src="js/libs/dat.gui.min.js"></script>
  79. <script src="js/loaders/OBJLoader2.js"></script>
  80. <script src="js/loaders/WWOBJLoader2.js"></script>
  81. <script>
  82. 'use strict';
  83. var WWOBJLoader2Example = (function () {
  84. var Validator = THREE.OBJLoader2.prototype._getValidator();
  85. function WWOBJLoader2Example( elementToBindTo ) {
  86. this.renderer = null;
  87. this.canvas = elementToBindTo;
  88. this.aspectRatio = 1;
  89. this.recalcAspectRatio();
  90. this.scene = null;
  91. this.cameraDefaults = {
  92. posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
  93. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  94. near: 0.1,
  95. far: 10000,
  96. fov: 45
  97. };
  98. this.camera = null;
  99. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  100. this.controls = null;
  101. this.flatShading = false;
  102. this.doubleSide = false;
  103. this.streamMeshes = true;
  104. this.cube = null;
  105. this.pivot = null;
  106. this.wwObjLoader2 = new THREE.OBJLoader2.WWOBJLoader2();
  107. this.wwObjLoader2.setCrossOrigin( 'anonymous' );
  108. // Check for the various File API support.
  109. this.fileApiAvailable = true;
  110. if ( window.File && window.FileReader && window.FileList && window.Blob ) {
  111. console.log( 'File API is supported! Enabling all features.' );
  112. } else {
  113. this.fileApiAvailable = false;
  114. console.warn( 'File API is not supported! Disabling file loading.' );
  115. }
  116. }
  117. WWOBJLoader2Example.prototype.initGL = function () {
  118. this.renderer = new THREE.WebGLRenderer( {
  119. canvas: this.canvas,
  120. antialias: true
  121. } );
  122. this.scene = new THREE.Scene();
  123. this.scene.background = new THREE.Color( 0x050505 );
  124. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  125. this.resetCamera();
  126. this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );
  127. var ambientLight = new THREE.AmbientLight( 0x404040 );
  128. var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  129. var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  130. directionalLight1.position.set( -100, -50, 100 );
  131. directionalLight2.position.set( 100, 50, -100 );
  132. this.scene.add( directionalLight1 );
  133. this.scene.add( directionalLight2 );
  134. this.scene.add( ambientLight );
  135. var helper = new THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  136. this.scene.add( helper );
  137. var geometry = new THREE.BoxGeometry( 10, 10, 10 );
  138. var material = new THREE.MeshNormalMaterial();
  139. this.cube = new THREE.Mesh( geometry, material );
  140. this.cube.position.set( 0, 0, 0 );
  141. this.scene.add( this.cube );
  142. this.createPivot();
  143. };
  144. WWOBJLoader2Example.prototype.createPivot = function () {
  145. this.pivot = new THREE.Object3D();
  146. this.pivot.name = 'Pivot';
  147. this.scene.add( this.pivot );
  148. };
  149. WWOBJLoader2Example.prototype.initPostGL = function () {
  150. var scope = this;
  151. var materialsLoaded = function ( materials ) {
  152. var count = Validator.isValid( materials ) ? materials.length : 0;
  153. console.log( 'Loaded #' + count + ' materials.' );
  154. };
  155. var meshLoaded = function ( name, bufferGeometry, material ) {
  156. console.log( 'Loaded mesh: ' + name + ' Material name: ' + material.name );
  157. };
  158. var completedLoading = function () {
  159. console.log( 'Loading complete!' );
  160. scope._reportProgress( '' );
  161. };
  162. this.wwObjLoader2.registerCallbackProgress( this._reportProgress );
  163. this.wwObjLoader2.registerCallbackCompletedLoading( completedLoading );
  164. this.wwObjLoader2.registerCallbackMaterialsLoaded( materialsLoaded );
  165. this.wwObjLoader2.registerCallbackMeshLoaded( meshLoaded );
  166. return true;
  167. };
  168. WWOBJLoader2Example.prototype._reportProgress = function( text ) {
  169. console.log( 'Progress: ' + text );
  170. document.getElementById( 'feedback' ).innerHTML = Validator.isValid( text ) ? text : '';
  171. };
  172. WWOBJLoader2Example.prototype.loadFiles = function ( prepData ) {
  173. prepData.setSceneGraphBaseNode( this.pivot );
  174. prepData.setStreamMeshes( this.streamMeshes );
  175. this.wwObjLoader2.prepareRun( prepData );
  176. this.wwObjLoader2.run();
  177. };
  178. WWOBJLoader2Example.prototype._handleFileSelect = function ( event, pathTexture ) {
  179. var fileObj = null;
  180. var fileMtl = null;
  181. var files = event.target.files;
  182. for ( var i = 0, file; file = files[ i ]; i++) {
  183. if ( file.name.indexOf( '\.obj' ) > 0 && fileObj === null ) {
  184. fileObj = file;
  185. }
  186. if ( file.name.indexOf( '\.mtl' ) > 0 && fileMtl === null ) {
  187. fileMtl = file;
  188. }
  189. }
  190. if ( ! Validator.isValid( fileObj ) ) {
  191. alert( 'Unable to load OBJ file from given files.' );
  192. }
  193. var fileReader = new FileReader();
  194. fileReader.onload = function( fileDataObj ) {
  195. var uint8Array = new Uint8Array( fileDataObj.target.result );
  196. if ( fileMtl === null ) {
  197. app.loadFilesUser({
  198. name: 'userObj',
  199. objAsArrayBuffer: uint8Array,
  200. pathTexture: pathTexture,
  201. mtlAsString: null
  202. })
  203. } else {
  204. fileReader.onload = function( fileDataMtl ) {
  205. app.loadFilesUser({
  206. name: 'userObj',
  207. objAsArrayBuffer: uint8Array,
  208. pathTexture: pathTexture,
  209. mtlAsString: fileDataMtl.target.result
  210. })
  211. };
  212. fileReader.readAsText( fileMtl );
  213. }
  214. };
  215. fileReader.readAsArrayBuffer( fileObj );
  216. };
  217. WWOBJLoader2Example.prototype.loadFilesUser = function ( objDef ) {
  218. var prepData = new THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer(
  219. objDef.name, objDef.objAsArrayBuffer, objDef.pathTexture, objDef.mtlAsString
  220. );
  221. prepData.setSceneGraphBaseNode( this.pivot );
  222. prepData.setStreamMeshes( this.streamMeshes );
  223. this.wwObjLoader2.prepareRun( prepData );
  224. this.wwObjLoader2.run();
  225. };
  226. WWOBJLoader2Example.prototype.resizeDisplayGL = function () {
  227. this.controls.handleResize();
  228. this.recalcAspectRatio();
  229. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  230. this.updateCamera();
  231. };
  232. WWOBJLoader2Example.prototype.recalcAspectRatio = function () {
  233. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  234. };
  235. WWOBJLoader2Example.prototype.resetCamera = function () {
  236. this.camera.position.copy( this.cameraDefaults.posCamera );
  237. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  238. this.updateCamera();
  239. };
  240. WWOBJLoader2Example.prototype.updateCamera = function () {
  241. this.camera.aspect = this.aspectRatio;
  242. this.camera.lookAt( this.cameraTarget );
  243. this.camera.updateProjectionMatrix();
  244. };
  245. WWOBJLoader2Example.prototype.render = function () {
  246. if ( ! this.renderer.autoClear ) this.renderer.clear();
  247. this.controls.update();
  248. this.cube.rotation.x += 0.05;
  249. this.cube.rotation.y += 0.05;
  250. this.renderer.render( this.scene, this.camera );
  251. };
  252. WWOBJLoader2Example.prototype.alterShading = function () {
  253. var scope = this;
  254. scope.flatShading = ! scope.flatShading;
  255. console.log( scope.flatShading ? 'Enabling flat shading' : 'Enabling smooth shading');
  256. scope.traversalFunction = function ( material ) {
  257. material.flatShading = scope.flatShading;
  258. material.needsUpdate = true;
  259. };
  260. var scopeTraverse = function ( object3d ) {
  261. scope.traverseScene( object3d );
  262. };
  263. scope.pivot.traverse( scopeTraverse );
  264. };
  265. WWOBJLoader2Example.prototype.alterDouble = function () {
  266. var scope = this;
  267. scope.doubleSide = ! scope.doubleSide;
  268. console.log( scope.doubleSide ? 'Enabling DoubleSide materials' : 'Enabling FrontSide materials');
  269. scope.traversalFunction = function ( material ) {
  270. material.side = scope.doubleSide ? THREE.DoubleSide : THREE.FrontSide;
  271. };
  272. var scopeTraverse = function ( object3d ) {
  273. scope.traverseScene( object3d );
  274. };
  275. scope.pivot.traverse( scopeTraverse );
  276. };
  277. WWOBJLoader2Example.prototype.traverseScene = function ( object3d ) {
  278. if ( object3d.material instanceof THREE.MultiMaterial ) {
  279. var materials = object3d.material.materials;
  280. for ( var name in materials ) {
  281. if ( materials.hasOwnProperty( name ) ) this.traversalFunction( materials[ name ] );
  282. }
  283. } else if ( object3d.material ) {
  284. this.traversalFunction( object3d.material );
  285. }
  286. };
  287. WWOBJLoader2Example.prototype.clearAllAssests = function () {
  288. var scope = this;
  289. var remover = function ( object3d ) {
  290. if ( object3d === scope.pivot ) {
  291. return;
  292. }
  293. console.log( 'Removing: ' + object3d.name );
  294. scope.scene.remove( object3d );
  295. if ( object3d.hasOwnProperty( 'geometry' ) ) {
  296. object3d.geometry.dispose();
  297. }
  298. if ( object3d.hasOwnProperty( 'material' ) ) {
  299. var mat = object3d.material;
  300. if ( mat.hasOwnProperty( 'materials' ) ) {
  301. var materials = mat.materials;
  302. for ( var name in materials ) {
  303. if ( materials.hasOwnProperty( name ) ) materials[ name ].dispose();
  304. }
  305. }
  306. }
  307. if ( object3d.hasOwnProperty( 'texture' ) ) {
  308. object3d.texture.dispose();
  309. }
  310. };
  311. scope.scene.remove( scope.pivot );
  312. scope.pivot.traverse( remover );
  313. scope.createPivot();
  314. };
  315. return WWOBJLoader2Example;
  316. })();
  317. var app = new WWOBJLoader2Example( document.getElementById( 'example' ) );
  318. // Init dat.gui and controls
  319. var elemFileInput = document.getElementById( 'fileUploadInput' );
  320. var WWOBJLoader2Control = function() {
  321. this.flatShading = app.flatShading;
  322. this.doubleSide = app.doubleSide;
  323. this.streamMeshes = app.streamMeshes;
  324. };
  325. var wwObjLoader2Control = new WWOBJLoader2Control();
  326. var gui = new dat.GUI( {
  327. autoPlace: false,
  328. width: 320
  329. } );
  330. var menuDiv = document.getElementById( 'dat' );
  331. menuDiv.appendChild(gui.domElement);
  332. var folderOptions = gui.addFolder( 'WWOBJLoader2 Options' );
  333. var controlSmooth = folderOptions.add( wwObjLoader2Control, 'flatShading' ).name( 'Flat Shading' );
  334. controlSmooth.onChange( function( value ) {
  335. console.log( 'Setting flatShading to: ' + value );
  336. app.alterShading();
  337. });
  338. var controlDouble = folderOptions.add( wwObjLoader2Control, 'doubleSide' ).name( 'Double Side Materials' );
  339. controlDouble.onChange( function( value ) {
  340. console.log( 'Setting doubleSide to: ' + value );
  341. app.alterDouble();
  342. });
  343. var controlStreamMeshes = folderOptions.add( wwObjLoader2Control, 'streamMeshes' ).name( 'Stream Meshes' );
  344. controlStreamMeshes.onChange( function( value ) {
  345. console.log( 'Setting streamMeshes to: ' + value );
  346. app.streamMeshes = value;
  347. });
  348. if ( app.fileApiAvailable ) {
  349. wwObjLoader2Control.pathTexture = 'obj/female02/';
  350. var controlPathTexture = folderOptions.add( wwObjLoader2Control, 'pathTexture' ).name( 'Relative path to textures' );
  351. controlPathTexture.onChange( function( value ) {
  352. console.log( 'Setting pathTexture to: ' + value );
  353. app.pathTexture = value + '/';
  354. });
  355. wwObjLoader2Control.loadObjFile = function () {
  356. elemFileInput.click();
  357. };
  358. folderOptions.add( wwObjLoader2Control, 'loadObjFile' ).name( 'Load OBJ/MTL Files' );
  359. var handleFileSelect = function ( object3d ) {
  360. app._handleFileSelect( object3d, wwObjLoader2Control.pathTexture );
  361. };
  362. elemFileInput.addEventListener( 'change' , handleFileSelect, false );
  363. wwObjLoader2Control.clearAllAssests = function () {
  364. app.clearAllAssests();
  365. };
  366. folderOptions.add( wwObjLoader2Control, 'clearAllAssests' ).name( 'Clear Scene' );
  367. }
  368. folderOptions.open();
  369. // init three.js example application
  370. var resizeWindow = function () {
  371. app.resizeDisplayGL();
  372. };
  373. var render = function () {
  374. requestAnimationFrame( render );
  375. app.render();
  376. };
  377. window.addEventListener( 'resize', resizeWindow, false );
  378. console.log( 'Starting initialisation phase...' );
  379. app.initGL();
  380. app.resizeDisplayGL();
  381. app.initPostGL();
  382. var prepData = new THREE.OBJLoader2.WWOBJLoader2.PrepDataFile(
  383. 'male02',
  384. 'obj/male02/',
  385. 'male02.obj',
  386. 'obj/male02/',
  387. 'male02.mtl'
  388. );
  389. app.loadFiles( prepData );
  390. // kick render loop
  391. render();
  392. </script>
  393. </body>
  394. </html>