webgl_loader_obj2_run_director.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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: 0;
  61. top: 0;
  62. z-Index: 200;
  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" rel="noopener">three.js</a> - Web Worker LoaderDirector 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/LoaderSupport.js"></script>
  83. <script src="js/loaders/OBJLoader2.js"></script>
  84. <script>
  85. 'use strict';
  86. var WWParallels = (function () {
  87. var Validator = THREE.LoaderSupport.Validator;
  88. function WWParallels( elementToBindTo ) {
  89. this.renderer = null;
  90. this.canvas = elementToBindTo;
  91. this.aspectRatio = 1;
  92. this.recalcAspectRatio();
  93. this.scene = null;
  94. this.cameraDefaults = {
  95. posCamera: new THREE.Vector3( 0.0, 175.0, 500.0 ),
  96. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  97. near: 0.1,
  98. far: 10000,
  99. fov: 45
  100. };
  101. this.camera = null;
  102. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  103. this.workerDirector = new THREE.LoaderSupport.WorkerDirector( THREE.OBJLoader2 );
  104. this.logging = {
  105. enabled: false,
  106. debug: false
  107. };
  108. this.workerDirector.setLogging( this.logging.enabled, this.logging.debug );
  109. this.workerDirector.setCrossOrigin( 'anonymous' );
  110. this.workerDirector.setForceWorkerDataCopy( true );
  111. this.controls = null;
  112. this.cube = null;
  113. this.allAssets = [];
  114. this.feedbackArray = null;
  115. this.running = false;
  116. }
  117. WWParallels.prototype.initGL = function () {
  118. this.renderer = new THREE.WebGLRenderer( {
  119. canvas: this.canvas,
  120. antialias: true,
  121. autoClear: true
  122. } );
  123. this.renderer.setClearColor( 0x050505 );
  124. this.scene = new THREE.Scene();
  125. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  126. this.resetCamera();
  127. this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );
  128. var ambientLight = new THREE.AmbientLight( 0x404040 );
  129. var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  130. var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  131. directionalLight1.position.set( -100, -50, 100 );
  132. directionalLight2.position.set( 100, 50, -100 );
  133. this.scene.add( directionalLight1 );
  134. this.scene.add( directionalLight2 );
  135. this.scene.add( ambientLight );
  136. var geometry = new THREE.BoxBufferGeometry( 10, 10, 10 );
  137. var material = new THREE.MeshNormalMaterial();
  138. this.cube = new THREE.Mesh( geometry, material );
  139. this.cube.position.set( 0, 0, 0 );
  140. this.scene.add( this.cube );
  141. };
  142. WWParallels.prototype.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. WWParallels.prototype.recalcAspectRatio = function () {
  149. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  150. };
  151. WWParallels.prototype.resetCamera = function () {
  152. this.camera.position.copy( this.cameraDefaults.posCamera );
  153. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  154. this.updateCamera();
  155. };
  156. WWParallels.prototype.updateCamera = function () {
  157. this.camera.aspect = this.aspectRatio;
  158. this.camera.lookAt( this.cameraTarget );
  159. this.camera.updateProjectionMatrix();
  160. };
  161. WWParallels.prototype.render = function () {
  162. if ( ! this.renderer.autoClear ) this.renderer.clear();
  163. this.controls.update();
  164. this.cube.rotation.x += 0.05;
  165. this.cube.rotation.y += 0.05;
  166. this.renderer.render( this.scene, this.camera );
  167. };
  168. WWParallels.prototype._reportProgress = function( content ) {
  169. var output = content;
  170. if ( Validator.isValid( content ) && Validator.isValid( content.detail ) ) output = content.detail.text;
  171. output = Validator.verifyInput( output, '' );
  172. if ( this.logging.enabled ) console.info( 'Progress:\n\t' + output.replace(/\<br\>/g, '\n\t' ) );
  173. document.getElementById( 'feedback' ).innerHTML = output;
  174. };
  175. WWParallels.prototype.enqueueAllAssests = function ( maxQueueSize, maxWebWorkers, streamMeshes ) {
  176. if ( this.running ) {
  177. return;
  178. } else {
  179. this.running = true;
  180. }
  181. var scope = this;
  182. scope.workerDirector.objectsCompleted = 0;
  183. scope.feedbackArray = [];
  184. scope.reportDonwload = [];
  185. var i;
  186. for ( i = 0; i < maxWebWorkers; i++ ) {
  187. scope.feedbackArray[ i ] = 'Worker #' + i + ': Awaiting feedback';
  188. scope.reportDonwload[ i ] = true;
  189. }
  190. scope._reportProgress( scope.feedbackArray.join( '\<br\>' ) );
  191. var callbackOnLoad = function ( event ) {
  192. var instanceNo = event.detail.instanceNo;
  193. scope.reportDonwload[ instanceNo ] = false;
  194. scope.allAssets.push( event.detail.loaderRootNode );
  195. var msg = 'Worker #' + instanceNo + ': Completed loading: ' + event.detail.modelName + ' (#' + scope.workerDirector.objectsCompleted + ')';
  196. if ( scope.logging.enabled ) console.info( msg );
  197. scope.feedbackArray[ instanceNo ] = msg;
  198. scope._reportProgress( scope.feedbackArray.join( '\<br\>' ) );
  199. if ( scope.workerDirector.objectsCompleted + 1 === maxQueueSize ) scope.running = false;
  200. };
  201. var callbackReportProgress = function ( event ) {
  202. var instanceNo = event.detail.instanceNo;
  203. var text = event.detail.text;
  204. if ( scope.reportDonwload[ instanceNo ] ) {
  205. var msg = 'Worker #' + instanceNo + ': ' + text;
  206. if ( scope.logging.enabled ) console.info( msg );
  207. scope.feedbackArray[ instanceNo ] = msg;
  208. scope._reportProgress( scope.feedbackArray.join( '\<br\>' ) );
  209. }
  210. };
  211. var callbackMeshAlter = function ( event, override ) {
  212. if ( ! Validator.isValid( override ) ) override = new THREE.LoaderSupport.LoadedMeshUserOverride( false, false );
  213. var material = event.detail.material;
  214. var meshName = event.detail.meshName;
  215. if ( Validator.isValid( material ) && material.name === 'defaultMaterial' || meshName === 'Mesh_Mesh_head_geo.001_lambert2SG.001' ) {
  216. var materialOverride = material;
  217. materialOverride.color = new THREE.Color( Math.random(), Math.random(), Math.random() );
  218. var mesh = new THREE.Mesh( event.detail.bufferGeometry, material );
  219. mesh.name = meshName;
  220. override.addMesh( mesh );
  221. override.alteredMesh = true;
  222. }
  223. return override;
  224. };
  225. var callbackOnLoadMaterials = function ( materials ) {
  226. console.log( 'Materials loaded' );
  227. return materials;
  228. };
  229. var callbacks = new THREE.LoaderSupport.Callbacks();
  230. callbacks.setCallbackOnProgress( callbackReportProgress );
  231. callbacks.setCallbackOnLoad( callbackOnLoad );
  232. callbacks.setCallbackOnMeshAlter( callbackMeshAlter );
  233. callbacks.setCallbackOnLoadMaterials( callbackOnLoadMaterials );
  234. this.workerDirector.prepareWorkers( callbacks, maxQueueSize, maxWebWorkers );
  235. if ( this.logging.enabled ) console.info( 'Configuring WWManager with queue size ' + this.workerDirector.getMaxQueueSize() + ' and ' + this.workerDirector.getMaxWebWorkers() + ' workers.' );
  236. var modelPrepDatas = [];
  237. var prepData;
  238. prepData = new THREE.LoaderSupport.PrepData( 'male02' );
  239. prepData.addResource( new THREE.LoaderSupport.ResourceDescriptor( 'models/obj/male02/male02.obj', 'OBJ ') );
  240. prepData.addResource( new THREE.LoaderSupport.ResourceDescriptor( 'models/obj/male02/male02.mtl', 'MTL' ) );
  241. prepData.setLogging( false, false );
  242. modelPrepDatas.push( prepData );
  243. prepData = new THREE.LoaderSupport.PrepData( 'female02' );
  244. prepData.addResource( new THREE.LoaderSupport.ResourceDescriptor( 'models/obj/female02/female02.obj', 'OBJ' ) );
  245. prepData.addResource( new THREE.LoaderSupport.ResourceDescriptor( 'models/obj/female02/female02.mtl', 'MTL' ) );
  246. prepData.setLogging( false, false );
  247. modelPrepDatas.push( prepData );
  248. prepData = new THREE.LoaderSupport.PrepData( 'viveController' );
  249. prepData.addResource( new THREE.LoaderSupport.ResourceDescriptor( 'models/obj/vive-controller/vr_controller_vive_1_5.obj', 'OBJ' ) );
  250. prepData.setLogging( false, false );
  251. prepData.scale = 400.0;
  252. modelPrepDatas.push( prepData );
  253. prepData = new THREE.LoaderSupport.PrepData( 'cerberus' );
  254. prepData.addResource( new THREE.LoaderSupport.ResourceDescriptor( 'models/obj/cerberus/Cerberus.obj', 'OBJ' ) );
  255. prepData.setLogging( false, false );
  256. prepData.scale = 50.0;
  257. modelPrepDatas.push( prepData );
  258. prepData = new THREE.LoaderSupport.PrepData( 'WaltHead' );
  259. prepData.addResource( new THREE.LoaderSupport.ResourceDescriptor( 'models/obj/walt/WaltHead.obj', 'OBJ' ) );
  260. prepData.addResource( new THREE.LoaderSupport.ResourceDescriptor( 'models/obj/walt/WaltHead.mtl', 'MTL' ) );
  261. prepData.setLogging( false, false );
  262. modelPrepDatas.push( prepData );
  263. var pivot;
  264. var distributionBase = -500;
  265. var distributionMax = 1000;
  266. var modelPrepDataIndex = 0;
  267. var modelPrepData;
  268. var scale;
  269. for ( i = 0; i < maxQueueSize; i++ ) {
  270. modelPrepDataIndex = Math.floor( Math.random() * modelPrepDatas.length );
  271. modelPrepData = modelPrepDatas[ modelPrepDataIndex ];
  272. modelPrepData.useAsync = true;
  273. scale = Validator.verifyInput( modelPrepData.scale, 0 );
  274. modelPrepData = modelPrepData.clone();
  275. pivot = new THREE.Object3D();
  276. pivot.position.set(
  277. distributionBase + distributionMax * Math.random(),
  278. distributionBase + distributionMax * Math.random(),
  279. distributionBase + distributionMax * Math.random()
  280. );
  281. if ( scale > 0 ) pivot.scale.set( scale, scale, scale );
  282. this.scene.add( pivot );
  283. modelPrepData.streamMeshesTo = pivot;
  284. this.workerDirector.enqueueForRun( modelPrepData );
  285. }
  286. this.workerDirector.processQueue();
  287. };
  288. WWParallels.prototype.clearAllAssests = function () {
  289. var storedObject3d;
  290. for ( var asset in this.allAssets ) {
  291. storedObject3d = this.allAssets[ asset ];
  292. var scope = this;
  293. var remover = function ( object3d ) {
  294. if ( storedObject3d === object3d ) return;
  295. if ( scope.logging.enabled ) console.info( 'Removing ' + object3d.name );
  296. scope.scene.remove( object3d );
  297. if ( object3d.hasOwnProperty( 'geometry' ) ) object3d.geometry.dispose();
  298. if ( object3d.hasOwnProperty( 'material' ) ) {
  299. var mat = object3d.material;
  300. if ( mat.hasOwnProperty( 'materials' ) ) {
  301. var materials = mat.materials;
  302. for ( var name in materials ) {
  303. if ( materials.hasOwnProperty( name ) ) materials[ name ].dispose();
  304. }
  305. }
  306. }
  307. if ( object3d.hasOwnProperty( 'texture' ) ) object3d.texture.dispose();
  308. };
  309. if ( Validator.isValid( storedObject3d ) ) {
  310. if ( this.pivot !== storedObject3d ) scope.scene.remove( storedObject3d );
  311. storedObject3d.traverse( remover );
  312. storedObject3d = null;
  313. }
  314. }
  315. this.allAssets = [];
  316. };
  317. WWParallels.prototype.terminateManager = function () {
  318. this.workerDirector.tearDown();
  319. this.running = false;
  320. };
  321. WWParallels.prototype.terminateManagerAndClearScene = function () {
  322. var scope = this;
  323. var scopedClearAllAssests = function (){
  324. scope.clearAllAssests();
  325. };
  326. if ( this.workerDirector.isRunning() ) {
  327. this.workerDirector.tearDown( scopedClearAllAssests );
  328. } else {
  329. scopedClearAllAssests();
  330. }
  331. this.running = false;
  332. };
  333. return WWParallels;
  334. })();
  335. var app = new WWParallels( document.getElementById( 'example' ) );
  336. var wwParallelsControl = {
  337. queueLength: 128,
  338. workerCount: 4,
  339. streamMeshes: true,
  340. run: function () {
  341. app.enqueueAllAssests( this.queueLength, this.workerCount, this.streamMeshes );
  342. },
  343. terminate: function () {
  344. app.terminateManager();
  345. },
  346. clearAllAssests: function () {
  347. app.terminateManagerAndClearScene();
  348. }
  349. };
  350. var gui = new dat.GUI( {
  351. autoPlace: false,
  352. width: 320
  353. } );
  354. var menuDiv = document.getElementById( 'dat' );
  355. menuDiv.appendChild(gui.domElement);
  356. var folderQueue = gui.addFolder( 'Web Worker Director Queue Control' );
  357. folderQueue.add( wwParallelsControl, 'queueLength' ).min( 1 ).max( 1024 ).step( 1 );
  358. folderQueue.add( wwParallelsControl, 'workerCount' ).min( 1 ).max( 16 ).step( 1 );
  359. folderQueue.add( wwParallelsControl, 'streamMeshes' );
  360. folderQueue.add( wwParallelsControl, 'run' ).name( 'Run Queue' );
  361. folderQueue.open();
  362. var folderWWControl = gui.addFolder( 'Resource Management' );
  363. folderWWControl.add( wwParallelsControl, 'terminate' ).name( 'Terminate WWManager' );
  364. folderWWControl.add( wwParallelsControl, 'clearAllAssests' ).name( 'Clear Scene' );
  365. var resizeWindow = function () {
  366. app.resizeDisplayGL();
  367. };
  368. var render = function () {
  369. requestAnimationFrame( render );
  370. app.render();
  371. };
  372. window.addEventListener( 'resize', resizeWindow, false );
  373. console.log( 'Starting initialisation phase...' );
  374. app.initGL();
  375. app.resizeDisplayGL();
  376. render();
  377. </script>
  378. </body>
  379. </html>