webgl_loader_obj2_run_director.html 15 KB

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