webgl_loader_obj2.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - OBJLoader2 basic usage</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="../build/three.js"></script>
  71. <script src="js/controls/TrackballControls.js"></script>
  72. <script src="js/loaders/MTLLoader.js"></script>
  73. <script src="js/libs/dat.gui.min.js"></script>
  74. <script src="js/loaders/LoaderSupport.js"></script>
  75. <script src="js/loaders/OBJLoader2.js"></script>
  76. <script>
  77. 'use strict';
  78. var OBJLoader2Example = function ( elementToBindTo ) {
  79. this.renderer = null;
  80. this.canvas = elementToBindTo;
  81. this.aspectRatio = 1;
  82. this.recalcAspectRatio();
  83. this.scene = null;
  84. this.cameraDefaults = {
  85. posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
  86. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  87. near: 0.1,
  88. far: 10000,
  89. fov: 45
  90. };
  91. this.camera = null;
  92. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  93. this.controls = null;
  94. };
  95. OBJLoader2Example.prototype = {
  96. constructor: OBJLoader2Example,
  97. initGL: function () {
  98. this.renderer = new THREE.WebGLRenderer( {
  99. canvas: this.canvas,
  100. antialias: true,
  101. autoClear: true
  102. } );
  103. this.renderer.setClearColor( 0x050505 );
  104. this.scene = new THREE.Scene();
  105. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  106. this.resetCamera();
  107. this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );
  108. var ambientLight = new THREE.AmbientLight( 0x404040 );
  109. var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  110. var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  111. directionalLight1.position.set( -100, -50, 100 );
  112. directionalLight2.position.set( 100, 50, -100 );
  113. this.scene.add( directionalLight1 );
  114. this.scene.add( directionalLight2 );
  115. this.scene.add( ambientLight );
  116. var helper = new THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  117. this.scene.add( helper );
  118. },
  119. initContent: function () {
  120. var modelName = 'female02';
  121. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  122. var scope = this;
  123. var objLoader = new THREE.OBJLoader2();
  124. var callbackOnLoad = function ( event ) {
  125. scope.scene.add( event.detail.loaderRootNode );
  126. console.log( 'Loading complete: ' + event.detail.modelName );
  127. scope._reportProgress( { detail: { text: '' } } );
  128. };
  129. var onLoadMtl = function ( materials ) {
  130. objLoader.setModelName( modelName );
  131. objLoader.setMaterials( materials );
  132. objLoader.setLogging( true, true );
  133. objLoader.load( 'models/obj/female02/female02.obj', callbackOnLoad, null, null, null, false );
  134. };
  135. objLoader.loadMtl( 'models/obj/female02/female02.mtl', null, onLoadMtl );
  136. },
  137. _reportProgress: function( event ) {
  138. var output = THREE.LoaderSupport.Validator.verifyInput( event.detail.text, '' );
  139. console.log( 'Progress: ' + output );
  140. document.getElementById( 'feedback' ).innerHTML = output;
  141. },
  142. resizeDisplayGL: function () {
  143. this.controls.handleResize();
  144. this.recalcAspectRatio();
  145. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  146. this.updateCamera();
  147. },
  148. recalcAspectRatio: function () {
  149. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  150. },
  151. resetCamera: function () {
  152. this.camera.position.copy( this.cameraDefaults.posCamera );
  153. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  154. this.updateCamera();
  155. },
  156. updateCamera: function () {
  157. this.camera.aspect = this.aspectRatio;
  158. this.camera.lookAt( this.cameraTarget );
  159. this.camera.updateProjectionMatrix();
  160. },
  161. render: function () {
  162. if ( ! this.renderer.autoClear ) this.renderer.clear();
  163. this.controls.update();
  164. this.renderer.render( this.scene, this.camera );
  165. }
  166. };
  167. var app = new OBJLoader2Example( document.getElementById( 'example' ) );
  168. var resizeWindow = function () {
  169. app.resizeDisplayGL();
  170. };
  171. var render = function () {
  172. requestAnimationFrame( render );
  173. app.render();
  174. };
  175. window.addEventListener( 'resize', resizeWindow, false );
  176. console.log( 'Starting initialisation phase...' );
  177. app.initGL();
  178. app.resizeDisplayGL();
  179. app.initContent();
  180. render();
  181. </script>
  182. </body>
  183. </html>