webgl_loader_obj2.html 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. #glFullscreen {
  10. width: 100%;
  11. height: 100vh;
  12. min-width: 640px;
  13. min-height: 360px;
  14. position: relative;
  15. overflow: hidden;
  16. z-index: 0;
  17. }
  18. #example {
  19. width: 100%;
  20. height: 100%;
  21. top: 0;
  22. left: 0;
  23. background-color: #000000;
  24. }
  25. #feedback {
  26. color: darkorange;
  27. }
  28. #dat {
  29. user-select: none;
  30. position: absolute;
  31. left: 0;
  32. top: 0;
  33. z-Index: 200;
  34. }
  35. </style>
  36. </head>
  37. <body>
  38. <div id="glFullscreen">
  39. <canvas id="example"></canvas>
  40. </div>
  41. <div id="dat">
  42. </div>
  43. <div id="info">
  44. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - OBJLoader2 direct loader test
  45. <div id="feedback"></div>
  46. </div>
  47. <script src="../build/three.js"></script>
  48. <script src="js/controls/TrackballControls.js"></script>
  49. <script src="js/loaders/MTLLoader.js"></script>
  50. <script src="js/libs/dat.gui.min.js"></script>
  51. <script src="js/loaders/LoaderSupport.js"></script>
  52. <script src="js/loaders/OBJLoader2.js"></script>
  53. <script>
  54. 'use strict';
  55. var OBJLoader2Example = function ( elementToBindTo ) {
  56. this.renderer = null;
  57. this.canvas = elementToBindTo;
  58. this.aspectRatio = 1;
  59. this.recalcAspectRatio();
  60. this.scene = null;
  61. this.cameraDefaults = {
  62. posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
  63. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  64. near: 0.1,
  65. far: 10000,
  66. fov: 45
  67. };
  68. this.camera = null;
  69. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  70. this.controls = null;
  71. };
  72. OBJLoader2Example.prototype = {
  73. constructor: OBJLoader2Example,
  74. initGL: function () {
  75. this.renderer = new THREE.WebGLRenderer( {
  76. canvas: this.canvas,
  77. antialias: true,
  78. autoClear: true
  79. } );
  80. this.renderer.setClearColor( 0x050505 );
  81. this.scene = new THREE.Scene();
  82. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  83. this.resetCamera();
  84. this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );
  85. var ambientLight = new THREE.AmbientLight( 0x404040 );
  86. var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  87. var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  88. directionalLight1.position.set( -100, -50, 100 );
  89. directionalLight2.position.set( 100, 50, -100 );
  90. this.scene.add( directionalLight1 );
  91. this.scene.add( directionalLight2 );
  92. this.scene.add( ambientLight );
  93. var helper = new THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  94. this.scene.add( helper );
  95. },
  96. initContent: function () {
  97. var modelName = 'female02';
  98. this._reportProgress( { detail: { text: 'Loading: ' + modelName } } );
  99. var scope = this;
  100. var objLoader = new THREE.OBJLoader2();
  101. var callbackOnLoad = function ( event ) {
  102. scope.scene.add( event.detail.loaderRootNode );
  103. console.log( 'Loading complete: ' + event.detail.modelName );
  104. scope._reportProgress( { detail: { text: '' } } );
  105. };
  106. var onLoadMtl = function ( materials ) {
  107. objLoader.setModelName( modelName );
  108. objLoader.setMaterials( materials );
  109. objLoader.setLogging( true, true );
  110. objLoader.load( 'models/obj/female02/female02.obj', callbackOnLoad, null, null, null, false );
  111. };
  112. objLoader.loadMtl( 'models/obj/female02/female02.mtl', null, onLoadMtl );
  113. },
  114. _reportProgress: function( event ) {
  115. var output = THREE.LoaderSupport.Validator.verifyInput( event.detail.text, '' );
  116. console.log( 'Progress: ' + output );
  117. document.getElementById( 'feedback' ).innerHTML = output;
  118. },
  119. resizeDisplayGL: function () {
  120. this.controls.handleResize();
  121. this.recalcAspectRatio();
  122. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  123. this.updateCamera();
  124. },
  125. recalcAspectRatio: function () {
  126. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  127. },
  128. resetCamera: function () {
  129. this.camera.position.copy( this.cameraDefaults.posCamera );
  130. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  131. this.updateCamera();
  132. },
  133. updateCamera: function () {
  134. this.camera.aspect = this.aspectRatio;
  135. this.camera.lookAt( this.cameraTarget );
  136. this.camera.updateProjectionMatrix();
  137. },
  138. render: function () {
  139. if ( ! this.renderer.autoClear ) this.renderer.clear();
  140. this.controls.update();
  141. this.renderer.render( this.scene, this.camera );
  142. }
  143. };
  144. var app = new OBJLoader2Example( document.getElementById( 'example' ) );
  145. var resizeWindow = function () {
  146. app.resizeDisplayGL();
  147. };
  148. var render = function () {
  149. requestAnimationFrame( render );
  150. app.render();
  151. };
  152. window.addEventListener( 'resize', resizeWindow, false );
  153. console.log( 'Starting initialisation phase...' );
  154. app.initGL();
  155. app.resizeDisplayGL();
  156. app.initContent();
  157. render();
  158. </script>
  159. </body>
  160. </html>