2
0

webgl_loader_obj2_ww.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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. function WWOBJLoader2Example( elementToBindTo ) {
  85. this.renderer = null;
  86. this.canvas = elementToBindTo;
  87. this.aspectRatio = 1;
  88. this.recalcAspectRatio();
  89. this.scene = null;
  90. this.cameraDefaults = {
  91. posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
  92. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  93. near: 0.1,
  94. far: 10000,
  95. fov: 45
  96. };
  97. this.camera = null;
  98. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  99. this.controls = null;
  100. this.smoothShading = true;
  101. this.doubleSide = false;
  102. this.streamMeshes = true;
  103. this.cube = null;
  104. this.pivot = null;
  105. this.wwObjLoader2 = new THREE.OBJLoader2.WWOBJLoader2();
  106. this.wwObjLoader2.setCrossOrigin( 'anonymous' );
  107. // Check for the various File API support.
  108. this.fileApiAvailable = true;
  109. if ( window.File && window.FileReader && window.FileList && window.Blob ) {
  110. console.log( 'File API is supported! Enabling all features.' );
  111. } else {
  112. this.fileApiAvailable = false;
  113. console.warn( 'File API is not supported! Disabling file loading.' );
  114. }
  115. }
  116. WWOBJLoader2Example.prototype.initGL = function () {
  117. this.renderer = new THREE.WebGLRenderer( {
  118. canvas: this.canvas,
  119. antialias: true,
  120. autoClear: true
  121. } );
  122. this.renderer.setClearColor( 0x050505 );
  123. this.scene = new THREE.Scene();
  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 reportProgress = function ( content ) {
  151. console.log( 'Progress: ' + content );
  152. };
  153. var materialsLoaded = function ( materials ) {
  154. var count = 0;
  155. console.log( 'The following materials have been loaded:' );
  156. for ( var mat in materials ) {
  157. count++;
  158. }
  159. console.log( 'Loaded #' + count + ' materials.' );
  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. return true;
  168. };
  169. WWOBJLoader2Example.prototype.loadFiles = function ( prepData ) {
  170. prepData.sceneGraphBaseNode = this.pivot;
  171. prepData.streamMeshes = this.streamMeshes;
  172. this.wwObjLoader2.prepareRun( prepData );
  173. this.wwObjLoader2.run();
  174. };
  175. WWOBJLoader2Example.prototype._handleFileSelect = function ( event, pathTexture ) {
  176. var fileObj = null;
  177. var fileMtl = null;
  178. var files = event.target.files;
  179. for ( var i = 0, file; file = files[ i ]; i++) {
  180. if ( file.name.indexOf( '\.obj' ) > 0 && fileObj === null ) {
  181. fileObj = file;
  182. }
  183. if ( file.name.indexOf( '\.mtl' ) > 0 && fileMtl === null ) {
  184. fileMtl = file;
  185. }
  186. }
  187. if ( fileObj == null ) {
  188. alert( 'Unable to load OBJ file from given files.' );
  189. }
  190. var fileReader = new FileReader();
  191. fileReader.onload = function( fileDataObj ) {
  192. var uint8Array = new Uint8Array( fileDataObj.target.result );
  193. if ( fileMtl === null ) {
  194. app.loadFilesUser({
  195. name: 'userObj',
  196. objAsArrayBuffer: uint8Array,
  197. pathTexture: pathTexture,
  198. mtlAsString: null
  199. })
  200. } else {
  201. fileReader.onload = function( fileDataMtl ) {
  202. app.loadFilesUser({
  203. name: 'userObj',
  204. objAsArrayBuffer: uint8Array,
  205. pathTexture: pathTexture,
  206. mtlAsString: fileDataMtl.target.result
  207. })
  208. };
  209. fileReader.readAsText( fileMtl );
  210. }
  211. };
  212. fileReader.readAsArrayBuffer( fileObj );
  213. };
  214. WWOBJLoader2Example.prototype.loadFilesUser = function ( objDef ) {
  215. var prepData = new THREE.OBJLoader2.WWOBJLoader2.PrepDataArrayBuffer(
  216. objDef.name, objDef.objAsArrayBuffer, objDef.pathTexture, objDef.mtlAsString, this.pivot, this.streamMeshes
  217. );
  218. this.wwObjLoader2.prepareRun( prepData );
  219. this.wwObjLoader2.run();
  220. };
  221. WWOBJLoader2Example.prototype.resizeDisplayGL = function () {
  222. this.controls.handleResize();
  223. this.recalcAspectRatio();
  224. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  225. this.updateCamera();
  226. };
  227. WWOBJLoader2Example.prototype.recalcAspectRatio = function () {
  228. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  229. };
  230. WWOBJLoader2Example.prototype.resetCamera = function () {
  231. this.camera.position.copy( this.cameraDefaults.posCamera );
  232. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  233. this.updateCamera();
  234. };
  235. WWOBJLoader2Example.prototype.updateCamera = function () {
  236. this.camera.aspect = this.aspectRatio;
  237. this.camera.lookAt( this.cameraTarget );
  238. this.camera.updateProjectionMatrix();
  239. };
  240. WWOBJLoader2Example.prototype.render = function () {
  241. if ( ! this.renderer.autoClear ) this.renderer.clear();
  242. this.controls.update();
  243. this.cube.rotation.x += 0.05;
  244. this.cube.rotation.y += 0.05;
  245. this.renderer.render( this.scene, this.camera );
  246. };
  247. WWOBJLoader2Example.prototype.alterSmoothShading = function () {
  248. var scope = this;
  249. scope.smoothShading = ! scope.smoothShading;
  250. console.log( scope.smoothShading ? 'Enabling SmoothShading' : 'Enabling FlatShading');
  251. scope.traversalFunction = function ( material ) {
  252. material.shading = scope.smoothShading ? THREE.SmoothShading : THREE.FlatShading;
  253. material.needsUpdate = true;
  254. };
  255. var scopeTraverse = function ( object3d ) {
  256. scope.traverseScene( object3d );
  257. };
  258. scope.pivot.traverse( scopeTraverse );
  259. };
  260. WWOBJLoader2Example.prototype.alterDouble = function () {
  261. var scope = this;
  262. scope.doubleSide = ! scope.doubleSide;
  263. console.log( scope.doubleSide ? 'Enabling DoubleSide materials' : 'Enabling FrontSide materials');
  264. scope.traversalFunction = function ( material ) {
  265. material.side = scope.doubleSide ? THREE.DoubleSide : THREE.FrontSide;
  266. };
  267. var scopeTraverse = function ( object3d ) {
  268. scope.traverseScene( object3d );
  269. };
  270. scope.pivot.traverse( scopeTraverse );
  271. };
  272. WWOBJLoader2Example.prototype.traverseScene = function ( object3d ) {
  273. if ( object3d.material instanceof THREE.MultiMaterial ) {
  274. for ( var matName in object3d.material.materials ) {
  275. this.traversalFunction( object3d.material.materials[ matName ] );
  276. }
  277. } else if ( object3d.material ) {
  278. this.traversalFunction( object3d.material );
  279. }
  280. };
  281. WWOBJLoader2Example.prototype.clearAllAssests = function () {
  282. var scope = this;
  283. var remover = function ( object3d ) {
  284. if ( object3d === scope.pivot ) {
  285. return;
  286. }
  287. console.log( 'Removing: ' + object3d.name );
  288. scope.scene.remove( object3d );
  289. if ( object3d.hasOwnProperty( 'geometry' ) ) {
  290. object3d.geometry.dispose();
  291. }
  292. if ( object3d.hasOwnProperty( 'material' ) ) {
  293. var mat = object3d.material;
  294. if ( mat.hasOwnProperty( 'materials' ) ) {
  295. for ( var mmat in mat.materials ) {
  296. mat.materials[ mmat ].dispose();
  297. }
  298. }
  299. }
  300. if ( object3d.hasOwnProperty( 'texture' ) ) {
  301. object3d.texture.dispose();
  302. }
  303. };
  304. scope.scene.remove( scope.pivot );
  305. scope.pivot.traverse( remover );
  306. scope.createPivot();
  307. };
  308. return WWOBJLoader2Example;
  309. })();
  310. var app = new WWOBJLoader2Example( document.getElementById( 'example' ) );
  311. // Init dat.gui and controls
  312. var elemFileInput = document.getElementById( 'fileUploadInput' );
  313. var WWOBJLoader2Control = function() {
  314. this.smoothShading = app.smoothShading;
  315. this.doubleSide = app.doubleSide;
  316. this.streamMeshes = app.streamMeshes;
  317. };
  318. var wwObjLoader2Control = new WWOBJLoader2Control();
  319. var gui = new dat.GUI( {
  320. autoPlace: false,
  321. width: 320
  322. } );
  323. var menuDiv = document.getElementById( 'dat' );
  324. menuDiv.appendChild(gui.domElement);
  325. var folderOptions = gui.addFolder( 'WWOBJLoader2 Options' );
  326. var controlSmooth = folderOptions.add( wwObjLoader2Control, 'smoothShading' ).name( 'Smooth Shading' );
  327. controlSmooth.onChange( function( value ) {
  328. console.log( 'Setting smoothShading to: ' + value );
  329. app.alterSmoothShading();
  330. });
  331. var controlDouble = folderOptions.add( wwObjLoader2Control, 'doubleSide' ).name( 'Double Side Materials' );
  332. controlDouble.onChange( function( value ) {
  333. console.log( 'Setting doubleSide to: ' + value );
  334. app.alterDouble();
  335. });
  336. var controlStreamMeshes = folderOptions.add( wwObjLoader2Control, 'streamMeshes' ).name( 'Stream Meshes' );
  337. controlStreamMeshes.onChange( function( value ) {
  338. console.log( 'Setting streamMeshes to: ' + value );
  339. app.streamMeshes = value;
  340. });
  341. if ( app.fileApiAvailable ) {
  342. wwObjLoader2Control.pathTexture = 'obj/female02/';
  343. var controlPathTexture = folderOptions.add( wwObjLoader2Control, 'pathTexture' ).name( 'Relative path to textures' );
  344. controlPathTexture.onChange( function( value ) {
  345. console.log( 'Setting pathTexture to: ' + value );
  346. app.pathTexture = value + '/';
  347. });
  348. wwObjLoader2Control.loadObjFile = function () {
  349. elemFileInput.click();
  350. };
  351. folderOptions.add( wwObjLoader2Control, 'loadObjFile' ).name( 'Load OBJ/MTL Files' );
  352. var handleFileSelect = function ( object3d ) {
  353. app._handleFileSelect( object3d, wwObjLoader2Control.pathTexture );
  354. };
  355. elemFileInput.addEventListener( 'change' , handleFileSelect, false );
  356. wwObjLoader2Control.clearAllAssests = function () {
  357. app.clearAllAssests();
  358. };
  359. folderOptions.add( wwObjLoader2Control, 'clearAllAssests' ).name( 'Clear Scene' );
  360. }
  361. folderOptions.open();
  362. // init three.js example application
  363. var resizeWindow = function () {
  364. app.resizeDisplayGL();
  365. };
  366. var render = function () {
  367. requestAnimationFrame( render );
  368. app.render();
  369. };
  370. window.addEventListener( 'resize', resizeWindow, false );
  371. console.log( 'Starting initialisation phase...' );
  372. app.initGL();
  373. app.resizeDisplayGL();
  374. app.initPostGL();
  375. var prepData = new THREE.OBJLoader2.WWOBJLoader2.PrepDataFile(
  376. 'male02',
  377. 'obj/male02/',
  378. 'male02.obj',
  379. 'obj/male02/',
  380. 'male02.mtl'
  381. );
  382. app.loadFiles( prepData );
  383. // kick render loop
  384. render();
  385. </script>
  386. </body>
  387. </html>