webgl_loader_obj2_ww.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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">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.smoothShading = true;
  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. autoClear: true
  122. } );
  123. this.renderer.setClearColor( 0x050505 );
  124. this.scene = new THREE.Scene();
  125. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  126. this.resetCamera();
  127. this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );
  128. var ambientLight = new THREE.AmbientLight( 0x404040 );
  129. var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  130. var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  131. directionalLight1.position.set( -100, -50, 100 );
  132. directionalLight2.position.set( 100, 50, -100 );
  133. this.scene.add( directionalLight1 );
  134. this.scene.add( directionalLight2 );
  135. this.scene.add( ambientLight );
  136. var helper = new THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  137. this.scene.add( helper );
  138. var geometry = new THREE.BoxGeometry( 10, 10, 10 );
  139. var material = new THREE.MeshNormalMaterial();
  140. this.cube = new THREE.Mesh( geometry, material );
  141. this.cube.position.set( 0, 0, 0 );
  142. this.scene.add( this.cube );
  143. this.createPivot();
  144. };
  145. WWOBJLoader2Example.prototype.createPivot = function () {
  146. this.pivot = new THREE.Object3D();
  147. this.pivot.name = 'Pivot';
  148. this.scene.add( this.pivot );
  149. };
  150. WWOBJLoader2Example.prototype.initPostGL = function () {
  151. var reportProgress = function ( content ) {
  152. console.log( 'Progress: ' + content );
  153. };
  154. var materialsLoaded = function ( materials ) {
  155. var count = Validator.isValid( materials ) ? materials.length : 0;
  156. console.log( 'Loaded #' + count + ' materials.' );
  157. };
  158. var meshLoaded = function ( name, bufferGeometry, material ) {
  159. console.log( 'Loaded mesh: ' + name + ' Material name: ' + material.name );
  160. };
  161. var completedLoading = function () {
  162. console.log( 'Loading complete!' );
  163. };
  164. this.wwObjLoader2.registerCallbackProgress( reportProgress );
  165. this.wwObjLoader2.registerCallbackCompletedLoading( completedLoading );
  166. this.wwObjLoader2.registerCallbackMaterialsLoaded( materialsLoaded );
  167. this.wwObjLoader2.registerCallbackMeshLoaded( meshLoaded );
  168. return true;
  169. };
  170. WWOBJLoader2Example.prototype.loadFiles = function ( prepData ) {
  171. prepData.setSceneGraphBaseNode( this.pivot );
  172. prepData.setStreamMeshes( this.streamMeshes );
  173. this.wwObjLoader2.prepareRun( prepData );
  174. this.wwObjLoader2.run();
  175. };
  176. WWOBJLoader2Example.prototype._handleFileSelect = function ( event, pathTexture ) {
  177. var fileObj = null;
  178. var fileMtl = null;
  179. var files = event.target.files;
  180. for ( var i = 0, file; file = files[ i ]; i++) {
  181. if ( file.name.indexOf( '\.obj' ) > 0 && fileObj === null ) {
  182. fileObj = file;
  183. }
  184. if ( file.name.indexOf( '\.mtl' ) > 0 && fileMtl === null ) {
  185. fileMtl = file;
  186. }
  187. }
  188. if ( ! Validator.isValid( fileObj ) ) {
  189. alert( 'Unable to load OBJ file from given files.' );
  190. }
  191. var fileReader = new FileReader();
  192. fileReader.onload = function( fileDataObj ) {
  193. var uint8Array = new Uint8Array( fileDataObj.target.result );
  194. if ( fileMtl === null ) {
  195. app.loadFilesUser({
  196. name: 'userObj',
  197. objAsArrayBuffer: uint8Array,
  198. pathTexture: pathTexture,
  199. mtlAsString: null
  200. })
  201. } else {
  202. fileReader.onload = function( fileDataMtl ) {
  203. app.loadFilesUser({
  204. name: 'userObj',
  205. objAsArrayBuffer: uint8Array,
  206. pathTexture: pathTexture,
  207. mtlAsString: fileDataMtl.target.result
  208. })
  209. };
  210. fileReader.readAsText( fileMtl );
  211. }
  212. };
  213. fileReader.readAsArrayBuffer( fileObj );
  214. };
  215. WWOBJLoader2Example.prototype.loadFilesUser = function ( objDef ) {
  216. var prepData = new THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer(
  217. objDef.name, objDef.objAsArrayBuffer, objDef.pathTexture, objDef.mtlAsString
  218. );
  219. prepData.setSceneGraphBaseNode( this.pivot );
  220. prepData.setStreamMeshes( this.streamMeshes );
  221. this.wwObjLoader2.prepareRun( prepData );
  222. this.wwObjLoader2.run();
  223. };
  224. WWOBJLoader2Example.prototype.resizeDisplayGL = function () {
  225. this.controls.handleResize();
  226. this.recalcAspectRatio();
  227. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  228. this.updateCamera();
  229. };
  230. WWOBJLoader2Example.prototype.recalcAspectRatio = function () {
  231. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  232. };
  233. WWOBJLoader2Example.prototype.resetCamera = function () {
  234. this.camera.position.copy( this.cameraDefaults.posCamera );
  235. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  236. this.updateCamera();
  237. };
  238. WWOBJLoader2Example.prototype.updateCamera = function () {
  239. this.camera.aspect = this.aspectRatio;
  240. this.camera.lookAt( this.cameraTarget );
  241. this.camera.updateProjectionMatrix();
  242. };
  243. WWOBJLoader2Example.prototype.render = function () {
  244. if ( ! this.renderer.autoClear ) this.renderer.clear();
  245. this.controls.update();
  246. this.cube.rotation.x += 0.05;
  247. this.cube.rotation.y += 0.05;
  248. this.renderer.render( this.scene, this.camera );
  249. };
  250. WWOBJLoader2Example.prototype.alterSmoothShading = function () {
  251. var scope = this;
  252. scope.smoothShading = ! scope.smoothShading;
  253. console.log( scope.smoothShading ? 'Enabling SmoothShading' : 'Enabling FlatShading');
  254. scope.traversalFunction = function ( material ) {
  255. material.shading = scope.smoothShading ? THREE.SmoothShading : THREE.FlatShading;
  256. material.needsUpdate = true;
  257. };
  258. var scopeTraverse = function ( object3d ) {
  259. scope.traverseScene( object3d );
  260. };
  261. scope.pivot.traverse( scopeTraverse );
  262. };
  263. WWOBJLoader2Example.prototype.alterDouble = function () {
  264. var scope = this;
  265. scope.doubleSide = ! scope.doubleSide;
  266. console.log( scope.doubleSide ? 'Enabling DoubleSide materials' : 'Enabling FrontSide materials');
  267. scope.traversalFunction = function ( material ) {
  268. material.side = scope.doubleSide ? THREE.DoubleSide : THREE.FrontSide;
  269. };
  270. var scopeTraverse = function ( object3d ) {
  271. scope.traverseScene( object3d );
  272. };
  273. scope.pivot.traverse( scopeTraverse );
  274. };
  275. WWOBJLoader2Example.prototype.traverseScene = function ( object3d ) {
  276. if ( object3d.material instanceof THREE.MultiMaterial ) {
  277. var materials = object3d.material.materials;
  278. for ( var name in materials ) {
  279. if ( materials.hasOwnProperty( name ) ) this.traversalFunction( materials[ name ] );
  280. }
  281. } else if ( object3d.material ) {
  282. this.traversalFunction( object3d.material );
  283. }
  284. };
  285. WWOBJLoader2Example.prototype.clearAllAssests = function () {
  286. var scope = this;
  287. var remover = function ( object3d ) {
  288. if ( object3d === scope.pivot ) {
  289. return;
  290. }
  291. console.log( 'Removing: ' + object3d.name );
  292. scope.scene.remove( object3d );
  293. if ( object3d.hasOwnProperty( 'geometry' ) ) {
  294. object3d.geometry.dispose();
  295. }
  296. if ( object3d.hasOwnProperty( 'material' ) ) {
  297. var mat = object3d.material;
  298. if ( mat.hasOwnProperty( 'materials' ) ) {
  299. var materials = mat.materials;
  300. for ( var name in materials ) {
  301. if ( materials.hasOwnProperty( name ) ) materials[ name ].dispose();
  302. }
  303. }
  304. }
  305. if ( object3d.hasOwnProperty( 'texture' ) ) {
  306. object3d.texture.dispose();
  307. }
  308. };
  309. scope.scene.remove( scope.pivot );
  310. scope.pivot.traverse( remover );
  311. scope.createPivot();
  312. };
  313. return WWOBJLoader2Example;
  314. })();
  315. var app = new WWOBJLoader2Example( document.getElementById( 'example' ) );
  316. // Init dat.gui and controls
  317. var elemFileInput = document.getElementById( 'fileUploadInput' );
  318. var WWOBJLoader2Control = function() {
  319. this.smoothShading = app.smoothShading;
  320. this.doubleSide = app.doubleSide;
  321. this.streamMeshes = app.streamMeshes;
  322. };
  323. var wwObjLoader2Control = new WWOBJLoader2Control();
  324. var gui = new dat.GUI( {
  325. autoPlace: false,
  326. width: 320
  327. } );
  328. var menuDiv = document.getElementById( 'dat' );
  329. menuDiv.appendChild(gui.domElement);
  330. var folderOptions = gui.addFolder( 'WWOBJLoader2 Options' );
  331. var controlSmooth = folderOptions.add( wwObjLoader2Control, 'smoothShading' ).name( 'Smooth Shading' );
  332. controlSmooth.onChange( function( value ) {
  333. console.log( 'Setting smoothShading to: ' + value );
  334. app.alterSmoothShading();
  335. });
  336. var controlDouble = folderOptions.add( wwObjLoader2Control, 'doubleSide' ).name( 'Double Side Materials' );
  337. controlDouble.onChange( function( value ) {
  338. console.log( 'Setting doubleSide to: ' + value );
  339. app.alterDouble();
  340. });
  341. var controlStreamMeshes = folderOptions.add( wwObjLoader2Control, 'streamMeshes' ).name( 'Stream Meshes' );
  342. controlStreamMeshes.onChange( function( value ) {
  343. console.log( 'Setting streamMeshes to: ' + value );
  344. app.streamMeshes = value;
  345. });
  346. if ( app.fileApiAvailable ) {
  347. wwObjLoader2Control.pathTexture = 'obj/female02/';
  348. var controlPathTexture = folderOptions.add( wwObjLoader2Control, 'pathTexture' ).name( 'Relative path to textures' );
  349. controlPathTexture.onChange( function( value ) {
  350. console.log( 'Setting pathTexture to: ' + value );
  351. app.pathTexture = value + '/';
  352. });
  353. wwObjLoader2Control.loadObjFile = function () {
  354. elemFileInput.click();
  355. };
  356. folderOptions.add( wwObjLoader2Control, 'loadObjFile' ).name( 'Load OBJ/MTL Files' );
  357. var handleFileSelect = function ( object3d ) {
  358. app._handleFileSelect( object3d, wwObjLoader2Control.pathTexture );
  359. };
  360. elemFileInput.addEventListener( 'change' , handleFileSelect, false );
  361. wwObjLoader2Control.clearAllAssests = function () {
  362. app.clearAllAssests();
  363. };
  364. folderOptions.add( wwObjLoader2Control, 'clearAllAssests' ).name( 'Clear Scene' );
  365. }
  366. folderOptions.open();
  367. // init three.js example application
  368. var resizeWindow = function () {
  369. app.resizeDisplayGL();
  370. };
  371. var render = function () {
  372. requestAnimationFrame( render );
  373. app.render();
  374. };
  375. window.addEventListener( 'resize', resizeWindow, false );
  376. console.log( 'Starting initialisation phase...' );
  377. app.initGL();
  378. app.resizeDisplayGL();
  379. app.initPostGL();
  380. var prepData = new THREE.OBJLoader2.WWOBJLoader2.PrepDataFile(
  381. 'male02',
  382. 'obj/male02/',
  383. 'male02.obj',
  384. 'obj/male02/',
  385. 'male02.mtl'
  386. );
  387. app.loadFiles( prepData );
  388. // kick render loop
  389. render();
  390. </script>
  391. </body>
  392. </html>