webgl_loader_obj2.html 10 KB

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