webgl_loader_obj2.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - OBJLoader2</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">three.js</a> - OBJLoader2 direct loader test
  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/OBJLoader2.js"></script>
  76. <script>
  77. 'use strict';
  78. var OBJLoader2Example = (function () {
  79. function OBJLoader2Example( elementToBindTo ) {
  80. this.renderer = null;
  81. this.canvas = elementToBindTo;
  82. this.aspectRatio = 1;
  83. this.recalcAspectRatio();
  84. this.scene = null;
  85. this.cameraDefaults = {
  86. posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
  87. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  88. near: 0.1,
  89. far: 10000,
  90. fov: 45
  91. };
  92. this.camera = null;
  93. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  94. this.controls = null;
  95. this.smoothShading = true;
  96. this.doubleSide = false;
  97. this.cube = null;
  98. this.pivot = null;
  99. }
  100. OBJLoader2Example.prototype.initGL = function () {
  101. this.renderer = new THREE.WebGLRenderer( {
  102. canvas: this.canvas,
  103. antialias: true,
  104. autoClear: true
  105. } );
  106. this.renderer.setClearColor( 0x050505 );
  107. this.scene = new THREE.Scene();
  108. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  109. this.resetCamera();
  110. this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );
  111. var ambientLight = new THREE.AmbientLight( 0x404040 );
  112. var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  113. var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  114. directionalLight1.position.set( -100, -50, 100 );
  115. directionalLight2.position.set( 100, 50, -100 );
  116. this.scene.add( directionalLight1 );
  117. this.scene.add( directionalLight2 );
  118. this.scene.add( ambientLight );
  119. var helper = new THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  120. this.scene.add( helper );
  121. var geometry = new THREE.BoxGeometry( 10, 10, 10 );
  122. var material = new THREE.MeshNormalMaterial();
  123. this.cube = new THREE.Mesh( geometry, material );
  124. this.cube.position.set( 0, 0, 0 );
  125. this.scene.add( this.cube );
  126. this.pivot = new THREE.Object3D();
  127. this.pivot.name = 'Pivot';
  128. this.scene.add( this.pivot );
  129. };
  130. OBJLoader2Example.prototype.initPostGL = function ( objDef ) {
  131. var scope = this;
  132. var mtlLoader = new THREE.MTLLoader();
  133. mtlLoader.setPath( objDef.texturePath );
  134. mtlLoader.setCrossOrigin( 'anonymous' );
  135. mtlLoader.load( objDef.fileMtl, function( materials ) {
  136. materials.preload();
  137. var objLoader = new THREE.OBJLoader2();
  138. objLoader.setSceneGraphBaseNode( scope.pivot );
  139. objLoader.setMaterials( materials.materials );
  140. objLoader.setPath( objDef.path );
  141. objLoader.setDebug( false, false );
  142. var onSuccess = function ( object3d ) {
  143. console.log( 'Loading complete. Meshes were attached to: ' + object3d.name );
  144. };
  145. var onProgress = function ( event ) {
  146. if ( event.lengthComputable ) {
  147. var percentComplete = event.loaded / event.total * 100;
  148. var output = 'Download of "' + objDef.fileObj + '": ' + Math.round( percentComplete ) + '%';
  149. console.log(output);
  150. }
  151. };
  152. var onError = function ( event ) {
  153. console.error( 'Error of type "' + event.type + '" occurred when trying to load: ' + event.src );
  154. };
  155. objLoader.load( objDef.fileObj, onSuccess, onProgress, onError );
  156. });
  157. return true;
  158. };
  159. OBJLoader2Example.prototype.resizeDisplayGL = function () {
  160. this.controls.handleResize();
  161. this.recalcAspectRatio();
  162. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  163. this.updateCamera();
  164. };
  165. OBJLoader2Example.prototype.recalcAspectRatio = function () {
  166. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  167. };
  168. OBJLoader2Example.prototype.resetCamera = function () {
  169. this.camera.position.copy( this.cameraDefaults.posCamera );
  170. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  171. this.updateCamera();
  172. };
  173. OBJLoader2Example.prototype.updateCamera = function () {
  174. this.camera.aspect = this.aspectRatio;
  175. this.camera.lookAt( this.cameraTarget );
  176. this.camera.updateProjectionMatrix();
  177. };
  178. OBJLoader2Example.prototype.render = function () {
  179. if ( ! this.renderer.autoClear ) this.renderer.clear();
  180. this.controls.update();
  181. this.cube.rotation.x += 0.05;
  182. this.cube.rotation.y += 0.05;
  183. this.renderer.render( this.scene, this.camera );
  184. };
  185. OBJLoader2Example.prototype.alterSmoothShading = function () {
  186. var scope = this;
  187. scope.smoothShading = ! scope.smoothShading;
  188. console.log( scope.smoothShading ? 'Enabling SmoothShading' : 'Enabling FlatShading');
  189. scope.traversalFunction = function ( material ) {
  190. material.shading = scope.smoothShading ? THREE.SmoothShading : THREE.FlatShading;
  191. material.needsUpdate = true;
  192. };
  193. var scopeTraverse = function ( object3d ) {
  194. scope.traverseScene( object3d );
  195. };
  196. scope.pivot.traverse( scopeTraverse );
  197. };
  198. OBJLoader2Example.prototype.alterDouble = function () {
  199. var scope = this;
  200. scope.doubleSide = ! scope.doubleSide;
  201. console.log( scope.doubleSide ? 'Enabling DoubleSide materials' : 'Enabling FrontSide materials');
  202. scope.traversalFunction = function ( material ) {
  203. material.side = scope.doubleSide ? THREE.DoubleSide : THREE.FrontSide;
  204. };
  205. var scopeTraverse = function ( object3d ) {
  206. scope.traverseScene( object3d );
  207. };
  208. scope.pivot.traverse( scopeTraverse );
  209. };
  210. OBJLoader2Example.prototype.traverseScene = function ( object3d ) {
  211. if ( object3d.material instanceof THREE.MultiMaterial ) {
  212. var materials = object3d.material.materials;
  213. for ( var name in materials ) {
  214. if ( materials.hasOwnProperty( name ) ) this.traversalFunction( materials[ name ] );
  215. }
  216. } else if ( object3d.material ) {
  217. this.traversalFunction( object3d.material );
  218. }
  219. };
  220. return OBJLoader2Example;
  221. })();
  222. var app = new OBJLoader2Example( document.getElementById( 'example' ) );
  223. // Init dat.gui and controls
  224. var OBJLoader2Control = function() {
  225. this.smoothShading = app.smoothShading;
  226. this.doubleSide = app.doubleSide;
  227. };
  228. var objLoader2Control = new OBJLoader2Control();
  229. var gui = new dat.GUI( {
  230. autoPlace: false,
  231. width: 320
  232. } );
  233. var menuDiv = document.getElementById( 'dat' );
  234. menuDiv.appendChild(gui.domElement);
  235. var folderQueue = gui.addFolder( 'OBJLoader2 Options' );
  236. var controlSmooth = folderQueue.add( objLoader2Control, 'smoothShading' ).name( 'Smooth Shading' );
  237. controlSmooth.onChange( function( value ) {
  238. console.log( 'Setting smoothShading to: ' + value );
  239. app.alterSmoothShading();
  240. });
  241. var controlDouble = folderQueue.add( objLoader2Control, 'doubleSide' ).name( 'Double Side Materials' );
  242. controlDouble.onChange( function( value ) {
  243. console.log( 'Setting doubleSide to: ' + value );
  244. app.alterDouble();
  245. });
  246. folderQueue.open();
  247. // init three.js example application
  248. var resizeWindow = function () {
  249. app.resizeDisplayGL();
  250. };
  251. var render = function () {
  252. requestAnimationFrame( render );
  253. app.render();
  254. };
  255. window.addEventListener( 'resize', resizeWindow, false );
  256. console.log( 'Starting initialisation phase...' );
  257. app.initGL();
  258. app.resizeDisplayGL();
  259. app.initPostGL( {
  260. path: 'obj/female02/',
  261. fileObj: 'female02.obj',
  262. texturePath: 'obj/female02/',
  263. fileMtl: 'female02.mtl'
  264. } );
  265. render();
  266. </script>
  267. </body>
  268. </html>