webgl_loader_obj2_ww_parallels.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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> - 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. var Validator = THREE.OBJLoader2.prototype._getValidator();
  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.wwDirector = new THREE.OBJLoader2.WWOBJLoader2Director();
  104. this.wwDirector.setCrossOrigin( 'anonymous' );
  105. this.controls = null;
  106. this.cube = null;
  107. this.allAssets = [];
  108. this.feedbackArray = null;
  109. this.running = false;
  110. }
  111. WWParallels.prototype.initGL = function () {
  112. this.renderer = new THREE.WebGLRenderer( {
  113. canvas: this.canvas,
  114. antialias: true,
  115. autoClear: true
  116. } );
  117. this.renderer.setClearColor( 0x050505 );
  118. this.scene = new THREE.Scene();
  119. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  120. this.resetCamera();
  121. this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );
  122. var ambientLight = new THREE.AmbientLight( 0x404040 );
  123. var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  124. var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  125. directionalLight1.position.set( -100, -50, 100 );
  126. directionalLight2.position.set( 100, 50, -100 );
  127. this.scene.add( directionalLight1 );
  128. this.scene.add( directionalLight2 );
  129. this.scene.add( ambientLight );
  130. var geometry = new THREE.BoxGeometry( 10, 10, 10 );
  131. var material = new THREE.MeshNormalMaterial();
  132. this.cube = new THREE.Mesh( geometry, material );
  133. this.cube.position.set( 0, 0, 0 );
  134. this.scene.add( this.cube );
  135. };
  136. WWParallels.prototype.resizeDisplayGL = function () {
  137. this.controls.handleResize();
  138. this.recalcAspectRatio();
  139. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  140. this.updateCamera();
  141. };
  142. WWParallels.prototype.recalcAspectRatio = function () {
  143. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  144. };
  145. WWParallels.prototype.resetCamera = function () {
  146. this.camera.position.copy( this.cameraDefaults.posCamera );
  147. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  148. this.updateCamera();
  149. };
  150. WWParallels.prototype.updateCamera = function () {
  151. this.camera.aspect = this.aspectRatio;
  152. this.camera.lookAt( this.cameraTarget );
  153. this.camera.updateProjectionMatrix();
  154. };
  155. WWParallels.prototype.render = function () {
  156. if ( ! this.renderer.autoClear ) this.renderer.clear();
  157. this.controls.update();
  158. this.cube.rotation.x += 0.05;
  159. this.cube.rotation.y += 0.05;
  160. this.renderer.render( this.scene, this.camera );
  161. };
  162. WWParallels.prototype.reportProgress = function( text ) {
  163. document.getElementById( 'feedback' ).innerHTML = text;
  164. };
  165. WWParallels.prototype.enqueueAllAssests = function ( maxQueueSize, maxWebWorkers, streamMeshes ) {
  166. if ( this.running ) {
  167. return;
  168. } else {
  169. this.running = true;
  170. }
  171. var scope = this;
  172. scope.wwDirector.objectsCompleted = 0;
  173. scope.feedbackArray = new Array( maxWebWorkers );
  174. var i;
  175. for ( i = 0; i < maxWebWorkers; i++ ) {
  176. scope.feedbackArray[ i ] = 'Worker #' + i + ': Awaiting feedback';
  177. }
  178. scope.reportProgress( scope.feedbackArray.join( '\<br\>' ) );
  179. var callbackCompletedLoading = function ( modelName, instanceNo ) {
  180. var msg = 'Worker #' + instanceNo + ': Completed loading: ' + modelName + ' (#' + scope.wwDirector.objectsCompleted + ')';
  181. console.log( msg );
  182. scope.feedbackArray[ instanceNo ] = msg;
  183. scope.reportProgress( scope.feedbackArray.join( '\<br\>' ) );
  184. if ( scope.wwDirector.objectsCompleted + 1 === maxQueueSize ) scope.running = false;
  185. };
  186. var callbackMeshLoaded = function ( name, bufferGeometry, material ) {
  187. var materialOverride;
  188. if ( Validator.isValid( material ) && material.name === 'defaultMaterial' || name === 'Mesh_Mesh_head_geo.001' ) {
  189. materialOverride = material;
  190. materialOverride.color = new THREE.Color( Math.random(), Math.random(), Math.random() );
  191. }
  192. return new THREE.OBJLoader2.WWOBJLoader2.LoadedMeshUserOverride( false, undefined, materialOverride );
  193. };
  194. var globalCallbacks = new THREE.OBJLoader2.WWOBJLoader2.PrepDataCallbacks();
  195. globalCallbacks.registerCallbackCompletedLoading( callbackCompletedLoading );
  196. globalCallbacks.registerCallbackMeshLoaded( callbackMeshLoaded );
  197. this.wwDirector.prepareWorkers( globalCallbacks, maxQueueSize, maxWebWorkers );
  198. console.log( 'Configuring WWManager with queue size ' + this.wwDirector.getMaxQueueSize() + ' and ' + this.wwDirector.getMaxWebWorkers() + ' workers.' );
  199. var callbackCompletedLoadingWalt = function () {
  200. console.log( 'Callback check: WALT was loaded (#' + scope.wwDirector.objectsCompleted + ')' );
  201. };
  202. var models = [];
  203. models.push( {
  204. modelName: 'male02',
  205. dataAvailable: false,
  206. pathObj: 'obj/male02/',
  207. fileObj: 'male02.obj',
  208. pathTexture: 'obj/male02/',
  209. fileMtl: 'male02.mtl'
  210. } );
  211. models.push( {
  212. modelName: 'female02',
  213. dataAvailable: false,
  214. pathObj: 'obj/female02/',
  215. fileObj: 'female02.obj',
  216. pathTexture: 'obj/female02/',
  217. fileMtl: 'female02.mtl'
  218. } );
  219. models.push( {
  220. modelName: 'viveController',
  221. dataAvailable: false,
  222. pathObj: 'models/obj/vive-controller/',
  223. fileObj: 'vr_controller_vive_1_5.obj',
  224. scale: 400.0
  225. } );
  226. models.push( {
  227. modelName: 'cerberus',
  228. dataAvailable: false,
  229. pathObj: 'models/obj/cerberus/',
  230. fileObj: 'Cerberus.obj',
  231. scale: 50.0
  232. } );
  233. models.push( {
  234. modelName: 'WaltHead',
  235. dataAvailable: false,
  236. pathObj: 'obj/walt/',
  237. fileObj: 'WaltHead.obj',
  238. pathTexture: 'obj/walt/',
  239. fileMtl: 'WaltHead.mtl'
  240. } );
  241. var pivot;
  242. var distributionBase = -500;
  243. var distributionMax = 1000;
  244. var modelIndex = 0;
  245. var model;
  246. var runParams;
  247. for ( i = 0; i < maxQueueSize; i++ ) {
  248. modelIndex = Math.floor( Math.random() * models.length );
  249. model = models[ modelIndex ];
  250. pivot = new THREE.Object3D();
  251. pivot.position.set(
  252. distributionBase + distributionMax * Math.random(),
  253. distributionBase + distributionMax * Math.random(),
  254. distributionBase + distributionMax * Math.random()
  255. );
  256. if ( Validator.isValid( model.scale ) ) pivot.scale.set( model.scale, model.scale, model.scale );
  257. this.scene.add( pivot );
  258. model.sceneGraphBaseNode = pivot;
  259. runParams = new THREE.OBJLoader2.WWOBJLoader2.PrepDataFile(
  260. model.modelName, model.pathObj, model.fileObj, model.pathTexture, model.fileMtl
  261. );
  262. runParams.setSceneGraphBaseNode( model.sceneGraphBaseNode );
  263. runParams.setStreamMeshes( streamMeshes );
  264. if ( model.modelName === 'WaltHead' ) {
  265. runParams.getCallbacks().registerCallbackCompletedLoading( callbackCompletedLoadingWalt );
  266. }
  267. this.wwDirector.enqueueForRun( runParams );
  268. this.allAssets.push( runParams );
  269. }
  270. this.wwDirector.processQueue();
  271. };
  272. WWParallels.prototype.clearAllAssests = function () {
  273. var ref;
  274. var scope = this;
  275. for ( var asset in this.allAssets ) {
  276. ref = this.allAssets[ asset ];
  277. var remover = function ( object3d ) {
  278. if ( object3d === ref.sceneGraphBaseNode ) return;
  279. console.log( 'Removing ' + object3d.name );
  280. scope.scene.remove( object3d );
  281. if ( object3d.hasOwnProperty( 'geometry' ) ) object3d.geometry.dispose();
  282. if ( object3d.hasOwnProperty( 'material' ) ) {
  283. var mat = object3d.material;
  284. if ( mat.hasOwnProperty( 'materials' ) ) {
  285. var materials = mat.materials;
  286. for ( var name in materials ) {
  287. if ( materials.hasOwnProperty( name ) ) materials[ name ].dispose();
  288. }
  289. }
  290. }
  291. if ( object3d.hasOwnProperty( 'texture' ) ) object3d.texture.dispose();
  292. };
  293. scope.scene.remove( ref.sceneGraphBaseNode );
  294. ref.sceneGraphBaseNode.traverse( remover );
  295. ref.sceneGraphBaseNode = null;
  296. }
  297. this.allAssets = [];
  298. };
  299. WWParallels.prototype.terminateManager = function () {
  300. this.wwDirector.deregister();
  301. };
  302. return WWParallels;
  303. })();
  304. var app = new WWParallels( document.getElementById( 'example' ) );
  305. var WWParallelsControl = function() {
  306. this.queueLength = 128;
  307. this.workerCount = 4;
  308. this.streamMeshes = true;
  309. this.run = function () {
  310. app.enqueueAllAssests( this.queueLength, this.workerCount, this.streamMeshes );
  311. };
  312. this.terminate = function () {
  313. app.terminateManager();
  314. };
  315. this.clearAllAssests = function () {
  316. app.terminateManager();
  317. app.clearAllAssests();
  318. };
  319. };
  320. var wwParallelsControl = new WWParallelsControl();
  321. var gui = new dat.GUI( {
  322. autoPlace: false,
  323. width: 320
  324. } );
  325. var menuDiv = document.getElementById( 'dat' );
  326. menuDiv.appendChild(gui.domElement);
  327. var folderQueue = gui.addFolder( 'Web Worker Director Queue Control' );
  328. folderQueue.add( wwParallelsControl, 'queueLength' ).min( 1 ).max( 1024 ).step( 1 );
  329. folderQueue.add( wwParallelsControl, 'workerCount' ).min( 1 ).max( 16 ).step( 1 );
  330. folderQueue.add( wwParallelsControl, 'streamMeshes' );
  331. folderQueue.add( wwParallelsControl, 'run' ).name( 'Run Queue' );
  332. folderQueue.open();
  333. var folderWWControl = gui.addFolder( 'Resource Management' );
  334. folderWWControl.add( wwParallelsControl, 'terminate' ).name( 'Terminate WWManager' );
  335. folderWWControl.add( wwParallelsControl, 'clearAllAssests' ).name( 'Clear Scene' );
  336. var resizeWindow = function () {
  337. app.resizeDisplayGL();
  338. };
  339. var render = function () {
  340. requestAnimationFrame( render );
  341. app.render();
  342. };
  343. window.addEventListener( 'resize', resizeWindow, false );
  344. console.log( 'Starting initialisation phase...' );
  345. app.initGL();
  346. app.resizeDisplayGL();
  347. render();
  348. </script>
  349. </body>
  350. </html>