webgl_loader_obj2.html 10 KB

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