webgl_loader_obj2_ww_parallels.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - Web Worker Parallel Demo</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. position: absolute;
  50. color: darkorange;
  51. text-align: left;
  52. bottom: 0%;
  53. left: 0%;
  54. width: auto;
  55. padding: 0px 0px 4px 4px;
  56. }
  57. #dat {
  58. user-select: none;
  59. position: absolute;
  60. left: 0px;
  61. top: 0px;
  62. z-Index: 2;
  63. }
  64. </style>
  65. </head>
  66. <body>
  67. <div id="glFullscreen">
  68. <canvas id="example"></canvas>
  69. </div>
  70. <div id="dat">
  71. </div>
  72. <div id="info">
  73. <a href="http://threejs.org" target="_blank">three.js</a> - WWOBJLoader2Director Parallels Demo
  74. </div>
  75. <div id="feedback">
  76. </div>
  77. <script src="js/Detector.js"></script>
  78. <script src="../build/three.js"></script>
  79. <script src="js/controls/TrackballControls.js"></script>
  80. <script src="js/loaders/MTLLoader.js"></script>
  81. <script src="js/libs/dat.gui.min.js"></script>
  82. <script src="js/loaders/OBJLoader2.js"></script>
  83. <script src="js/loaders/WWOBJLoader2.js"></script>
  84. <script>
  85. 'use strict';
  86. var WWParallels = (function () {
  87. function WWParallels( elementToBindTo ) {
  88. this.renderer = null;
  89. this.canvas = elementToBindTo;
  90. this.aspectRatio = 1;
  91. this.recalcAspectRatio();
  92. this.scene = null;
  93. this.cameraDefaults = {
  94. posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
  95. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  96. near: 0.1,
  97. far: 10000,
  98. fov: 45
  99. };
  100. this.camera = null;
  101. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  102. this.wwDirector = new THREE.OBJLoader2.WWOBJLoader2Director();
  103. this.wwDirector.setCrossOrigin( 'anonymous' );
  104. this.controls = null;
  105. this.cube = null;
  106. this.allAssets = [];
  107. this.feedbackArray = null;
  108. }
  109. WWParallels.prototype.initGL = function () {
  110. this.renderer = new THREE.WebGLRenderer( {
  111. canvas: this.canvas,
  112. antialias: true,
  113. autoClear: true
  114. } );
  115. this.renderer.setClearColor( 0x050505 );
  116. this.scene = new THREE.Scene();
  117. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  118. this.resetCamera();
  119. this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );
  120. var ambientLight = new THREE.AmbientLight( 0x404040 );
  121. var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  122. var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  123. directionalLight1.position.set( -100, -50, 100 );
  124. directionalLight2.position.set( 100, 50, -100 );
  125. this.scene.add( directionalLight1 );
  126. this.scene.add( directionalLight2 );
  127. this.scene.add( ambientLight );
  128. var geometry = new THREE.BoxGeometry( 10, 10, 10 );
  129. var material = new THREE.MeshNormalMaterial();
  130. this.cube = new THREE.Mesh( geometry, material );
  131. this.cube.position.set( 0, 0, 0 );
  132. this.scene.add( this.cube );
  133. };
  134. WWParallels.prototype.resizeDisplayGL = function () {
  135. this.controls.handleResize();
  136. this.recalcAspectRatio();
  137. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  138. this.updateCamera();
  139. };
  140. WWParallels.prototype.recalcAspectRatio = function () {
  141. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  142. };
  143. WWParallels.prototype.resetCamera = function () {
  144. this.camera.position.copy( this.cameraDefaults.posCamera );
  145. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  146. this.updateCamera();
  147. };
  148. WWParallels.prototype.updateCamera = function () {
  149. this.camera.aspect = this.aspectRatio;
  150. this.camera.lookAt( this.cameraTarget );
  151. this.camera.updateProjectionMatrix();
  152. };
  153. WWParallels.prototype.render = function () {
  154. if ( ! this.renderer.autoClear ) this.renderer.clear();
  155. this.controls.update();
  156. this.cube.rotation.x += 0.05;
  157. this.cube.rotation.y += 0.05;
  158. this.renderer.render( this.scene, this.camera );
  159. };
  160. WWParallels.prototype.reportProgress = function( text ) {
  161. document.getElementById( 'feedback' ).innerHTML = text;
  162. };
  163. WWParallels.prototype.enqueueAllAssests = function ( maxQueueSize, maxWebWorkers, streamMeshes ) {
  164. var scope = this;
  165. scope.wwDirector.objectsCompleted = 0;
  166. scope.feedbackArray = new Array( maxWebWorkers );
  167. for ( var i = 0; i < maxWebWorkers; i++ ) {
  168. scope.feedbackArray[ i ] = 'Worker #' + i + ': Awaiting feedback';
  169. }
  170. scope.reportProgress( scope.feedbackArray.join( '\<br\>' ) );
  171. var callbackCompletedLoading = function ( modelName, instanceNo ) {
  172. var msg = 'Worker #' + instanceNo + ': Completed loading: ' + modelName + ' (#' + scope.wwDirector.objectsCompleted + ')';
  173. console.log( msg );
  174. scope.feedbackArray[ instanceNo ] = msg;
  175. scope.reportProgress( scope.feedbackArray.join( '\<br\>' ) );
  176. };
  177. var callbackMeshLoaded = function ( meshName, material ) {
  178. var replacedMaterial = null;
  179. if ( material != null && material.name === 'defaultMaterial' || meshName === 'Mesh_Mesh_head_geo.001' ) {
  180. replacedMaterial = material;
  181. replacedMaterial.color = new THREE.Color( Math.random(), Math.random(), Math.random() );
  182. }
  183. return replacedMaterial;
  184. };
  185. this.wwDirector.prepareWorkers(
  186. {
  187. completedLoading: callbackCompletedLoading,
  188. meshLoaded: callbackMeshLoaded
  189. },
  190. maxQueueSize,
  191. maxWebWorkers
  192. );
  193. console.log( 'Configuring WWManager with queue size ' + this.wwDirector.getMaxQueueSize() + ' and ' + this.wwDirector.getMaxWebWorkers() + ' workers.' );
  194. var models = [];
  195. models.push( {
  196. modelName: 'male02',
  197. dataAvailable: false,
  198. pathObj: 'obj/male02/',
  199. fileObj: 'male02.obj',
  200. pathTexture: 'obj/male02/',
  201. fileMtl: 'male02.mtl'
  202. } );
  203. models.push( {
  204. modelName: 'female02',
  205. dataAvailable: false,
  206. pathObj: 'obj/female02/',
  207. fileObj: 'female02.obj',
  208. pathTexture: 'obj/female02/',
  209. fileMtl: 'female02.mtl'
  210. } );
  211. models.push( {
  212. modelName: 'viveController',
  213. dataAvailable: false,
  214. pathObj: 'models/obj/vive-controller/',
  215. fileObj: 'vr_controller_vive_1_5.obj',
  216. scale: 400.0
  217. } );
  218. models.push( {
  219. modelName: 'cerberus',
  220. dataAvailable: false,
  221. pathObj: 'models/obj/cerberus/',
  222. fileObj: 'Cerberus.obj',
  223. scale: 50.0
  224. } );
  225. models.push( {
  226. modelName:'WaltHead',
  227. dataAvailable: false,
  228. pathObj: 'obj/walt/',
  229. fileObj: 'WaltHead.obj',
  230. pathTexture: 'obj/walt/',
  231. fileMtl: 'WaltHead.mtl'
  232. } );
  233. var pivot;
  234. var distributionBase = -500;
  235. var distributionMax = 1000;
  236. var modelIndex = 0;
  237. var model;
  238. var runParams;
  239. for ( var i = 0; i < maxQueueSize; i++ ) {
  240. modelIndex = Math.floor( Math.random() * 5 );
  241. model = models[ modelIndex ];
  242. pivot = new THREE.Object3D();
  243. pivot.position.set(
  244. distributionBase + distributionMax * Math.random(),
  245. distributionBase + distributionMax * Math.random(),
  246. distributionBase + distributionMax * Math.random()
  247. );
  248. if ( model.scale != null ) pivot.scale.set( model.scale, model.scale, model.scale );
  249. this.scene.add( pivot );
  250. model.sceneGraphBaseNode = pivot;
  251. runParams = new THREE.OBJLoader2.WWOBJLoader2.PrepDataFile(
  252. model.modelName, model.pathObj, model.fileObj, model.pathTexture, model.fileMtl, model.sceneGraphBaseNode, streamMeshes
  253. );
  254. this.wwDirector.enqueueForRun( runParams );
  255. this.allAssets.push( runParams );
  256. }
  257. this.wwDirector.processQueue();
  258. };
  259. WWParallels.prototype.clearAllAssests = function () {
  260. var ref;
  261. var scope = this;
  262. for ( var asset in this.allAssets ) {
  263. ref = this.allAssets[asset];
  264. var remover = function ( object3d ) {
  265. if ( object3d === ref.sceneGraphBaseNode ) {
  266. return;
  267. }
  268. console.log( 'Removing ' + object3d.name );
  269. scope.scene.remove( object3d );
  270. if ( object3d.hasOwnProperty( 'geometry' ) ) {
  271. object3d.geometry.dispose();
  272. }
  273. if ( object3d.hasOwnProperty( 'material' ) ) {
  274. var mat = object3d.material;
  275. if ( mat.hasOwnProperty( 'materials' ) ) {
  276. for ( var mmat in mat.materials ) {
  277. mat.materials[mmat].dispose();
  278. }
  279. }
  280. }
  281. if ( object3d.hasOwnProperty( 'texture' ) ) {
  282. object3d.texture.dispose();
  283. }
  284. };
  285. scope.scene.remove( ref.sceneGraphBaseNode );
  286. ref.sceneGraphBaseNode.traverse( remover );
  287. ref.sceneGraphBaseNode = null;
  288. }
  289. this.allAssets = [];
  290. };
  291. WWParallels.prototype.terminateManager = function () {
  292. this.wwDirector.deregister();
  293. };
  294. return WWParallels;
  295. })();
  296. var app = new WWParallels( document.getElementById( 'example' ) );
  297. var WWParallelsControl = function() {
  298. this.queueLength = 128;
  299. this.workerCount = 4;
  300. this.streamMeshes = true;
  301. this.run = function () {
  302. app.enqueueAllAssests( this.queueLength, this.workerCount, this.streamMeshes );
  303. };
  304. this.terminate = function () {
  305. app.terminateManager();
  306. };
  307. this.clearAllAssests = function () {
  308. app.terminateManager();
  309. app.clearAllAssests();
  310. };
  311. };
  312. var wwParallelsControl = new WWParallelsControl();
  313. var gui = new dat.GUI( {
  314. autoPlace: false,
  315. width: 320
  316. } );
  317. var menuDiv = document.getElementById( 'dat' );
  318. menuDiv.appendChild(gui.domElement);
  319. var folderQueue = gui.addFolder( 'Web Worker Director Queue Control' );
  320. folderQueue.add( wwParallelsControl, 'queueLength' ).min( 1 ).max( 1024 ).step( 1 );
  321. folderQueue.add( wwParallelsControl, 'workerCount' ).min( 1 ).max( 16 ).step( 1 );
  322. folderQueue.add( wwParallelsControl, 'streamMeshes' );
  323. folderQueue.add( wwParallelsControl, 'run' ).name( 'Run Queue' );
  324. folderQueue.open();
  325. var folderWWControl = gui.addFolder( 'Resource Management' );
  326. folderWWControl.add( wwParallelsControl, 'terminate' ).name( 'Terminate WWManager' );
  327. folderWWControl.add( wwParallelsControl, 'clearAllAssests' ).name( 'Clear Scene' );
  328. var resizeWindow = function () {
  329. app.resizeDisplayGL();
  330. };
  331. var render = function () {
  332. requestAnimationFrame( render );
  333. app.render();
  334. };
  335. window.addEventListener( 'resize', resizeWindow, false );
  336. console.log( 'Starting initialisation phase...' );
  337. app.initGL();
  338. app.resizeDisplayGL();
  339. render();
  340. </script>
  341. </body>
  342. </html>