webgl_loader_obj2_meshspray.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>Mesh Spray - Worker based mesh spray</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. color: darkorange;
  50. }
  51. #dat {
  52. user-select: none;
  53. position: absolute;
  54. left: 0;
  55. top: 0;
  56. z-Index: 200;
  57. }
  58. </style>
  59. </head>
  60. <body>
  61. <div id="glFullscreen">
  62. <canvas id="example"></canvas>
  63. </div>
  64. <div id="dat">
  65. </div>
  66. <div id="info">
  67. <a href="http://threejs.org" target="_blank">three.js</a> - Mesh Spray
  68. <div id="feedback"></div>
  69. </div>
  70. <script src="js/Detector.js"></script>
  71. <script src="../build/three.js"></script>
  72. <script src="js/controls/TrackballControls.js"></script>
  73. <script src="js/libs/dat.gui.min.js"></script>
  74. <script src="js/loaders/LoaderSupport.js"></script>
  75. <script>
  76. 'use strict';
  77. var MeshSpray = (function () {
  78. var Validator = THREE.LoaderSupport.Validator;
  79. var ConsoleLogger = THREE.LoaderSupport.ConsoleLogger;
  80. MeshSpray.prototype = Object.create( THREE.LoaderSupport.Commons.prototype );
  81. MeshSpray.prototype.constructor = MeshSpray;
  82. function MeshSpray( manager ) {
  83. THREE.LoaderSupport.Commons.call( this, manager );
  84. this.workerSupport = null;
  85. this.logger = new ConsoleLogger();
  86. };
  87. MeshSpray.prototype.run = function ( prepData, workerSupportExternal ) {
  88. if ( Validator.isValid( workerSupportExternal ) ) {
  89. this.workerSupport = workerSupportExternal;
  90. this.logger = workerSupportExternal.logger;
  91. } else {
  92. this.workerSupport = Validator.verifyInput( this.workerSupport, new THREE.LoaderSupport.WorkerSupport() );
  93. }
  94. this.logger.logTimeStart( 'MeshSpray' );
  95. this._applyPrepData( prepData );
  96. var scope = this;
  97. var scopeBuilderFunc = function ( payload ) {
  98. var meshes = scope.builder.buildMeshes( payload );
  99. var mesh;
  100. for ( var i in meshes ) {
  101. mesh = meshes[ i ];
  102. scope.loaderRootNode.add( mesh );
  103. }
  104. };
  105. var scopeFuncComplete = function ( message ) {
  106. var callback = scope.callbacks.onLoad;
  107. if ( Validator.isValid( callback ) ) callback(
  108. {
  109. detail: {
  110. loaderRootNode: scope.loaderRootNode,
  111. modelName: scope.modelName,
  112. instanceNo: scope.instanceNo
  113. }
  114. }
  115. );
  116. scope.logger.logTimeEnd( 'MeshSpray' );
  117. };
  118. var buildCode = function ( funcBuildObject, funcBuildSingelton ) {
  119. var workerCode = '';
  120. workerCode += '/**\n';
  121. workerCode += ' * This code was constructed by MeshSpray buildWorkerCode.\n';
  122. workerCode += ' */\n\n';
  123. workerCode += funcBuildObject( 'Validator', Validator );
  124. workerCode += funcBuildSingelton( 'ConsoleLogger', 'ConsoleLogger', ConsoleLogger );
  125. workerCode += funcBuildSingelton( 'Parser', 'Parser', Parser );
  126. return workerCode;
  127. };
  128. this.workerSupport.validate( buildCode, false );
  129. this.workerSupport.setCallbacks( scopeBuilderFunc, scopeFuncComplete );
  130. this.workerSupport.run(
  131. {
  132. cmd: 'run',
  133. params: {
  134. debug: this.debug,
  135. enableLogging: this.logger.enabled,
  136. dimension: prepData.dimension,
  137. quantity: prepData.quantity,
  138. globalObjectCount: prepData.globalObjectCount
  139. },
  140. logger: {
  141. debug: this.logger.debug,
  142. enabled: this.logger.enabled
  143. },
  144. materials: {
  145. materialNames: this.builder.materialNames
  146. },
  147. buffers: {
  148. input: null
  149. }
  150. }
  151. );
  152. };
  153. var Parser = ( function () {
  154. function Parser( logger ) {
  155. this.sizeFactor = 0.5;
  156. this.localOffsetFactor = 1.0;
  157. this.globalObjectCount = 0;
  158. this.debug = false;
  159. this.dimension = 200;
  160. this.quantity = 1;
  161. this.callbackBuilder = null;
  162. this.logger = logger;
  163. };
  164. Parser.prototype.parse = function () {
  165. var materialDescription;
  166. var materialDescriptions = [];
  167. var materialGroups = [];
  168. materialDescription = {
  169. name: 'Gen',
  170. flat: false,
  171. vertexColors: true,
  172. default: true
  173. };
  174. materialDescriptions.push( materialDescription );
  175. var baseTriangle = [ 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 0.0, -1.0, 1.0 ];
  176. var vertices = [];
  177. var colors = [];
  178. var normals = [];
  179. var uvs = [];
  180. var dimensionHalf = this.dimension / 2;
  181. var fixedOffsetX;
  182. var fixedOffsetY;
  183. var fixedOffsetZ;
  184. var s, t;
  185. // complete triagle
  186. var sizeVaring = this.sizeFactor * Math.random();
  187. // local coords offset
  188. var localOffsetFactor = this.localOffsetFactor;
  189. for ( var i = 0; i < this.quantity; i++ ) {
  190. sizeVaring = this.sizeFactor * Math.random();
  191. s = 2 * Math.PI * Math.random();
  192. t = Math.PI * Math.random();
  193. fixedOffsetX = dimensionHalf * Math.random() * Math.cos( s ) * Math.sin( t );
  194. fixedOffsetY = dimensionHalf * Math.random() * Math.sin( s ) * Math.sin( t );
  195. fixedOffsetZ = dimensionHalf * Math.random() * Math.cos( t );
  196. for ( var j = 0; j < baseTriangle.length; j += 3 ) {
  197. vertices.push( baseTriangle[ j ] * sizeVaring + localOffsetFactor * Math.random() + fixedOffsetX );
  198. vertices.push( baseTriangle[ j + 1 ] * sizeVaring + localOffsetFactor * Math.random() + fixedOffsetY );
  199. vertices.push( baseTriangle[ j + 2 ] * sizeVaring + localOffsetFactor * Math.random() + fixedOffsetZ );
  200. colors.push( Math.random() );
  201. colors.push( Math.random() );
  202. colors.push( Math.random() );
  203. }
  204. }
  205. var absoluteVertexCount = vertices.length;
  206. var absoluteColorCount = colors.length;
  207. var absoluteNormalCount = 0;
  208. var absoluteUvCount = 0;
  209. var vertexFA = new Float32Array( absoluteVertexCount );
  210. var colorFA = ( absoluteColorCount > 0 ) ? new Float32Array( absoluteColorCount ) : null;
  211. var normalFA = ( absoluteNormalCount > 0 ) ? new Float32Array( absoluteNormalCount ) : null;
  212. var uvFA = ( absoluteUvCount > 0 ) ? new Float32Array( absoluteUvCount ) : null;
  213. vertexFA.set( vertices, 0 );
  214. if ( colorFA ) {
  215. colorFA.set( colors, 0 );
  216. }
  217. if ( normalFA ) {
  218. normalFA.set( normals, 0 );
  219. }
  220. if ( uvFA ) {
  221. uvFA.set( uvs, 0 );
  222. }
  223. this.globalObjectCount++;
  224. this.callbackBuilder(
  225. {
  226. cmd: 'meshData',
  227. progress: {
  228. numericalValue: 1.0
  229. },
  230. params: {
  231. meshName: 'Gen' + this.globalObjectCount
  232. },
  233. materials: {
  234. multiMaterial: false,
  235. materialDescriptions: materialDescriptions,
  236. materialGroups: materialGroups
  237. },
  238. buffers: {
  239. vertices: vertexFA,
  240. colors: colorFA,
  241. normals: normalFA,
  242. uvs: uvFA
  243. }
  244. },
  245. [ vertexFA.buffer ],
  246. colorFA !== null ? [ colorFA.buffer ] : null,
  247. normalFA !== null ? [ normalFA.buffer ] : null,
  248. uvFA !== null ? [ uvFA.buffer ] : null
  249. );
  250. this.logger.logInfo( 'Global output object count: ' + this.globalObjectCount );
  251. };
  252. return Parser;
  253. })();
  254. return MeshSpray;
  255. })();
  256. var MeshSprayApp = (function () {
  257. var Validator = THREE.LoaderSupport.Validator;
  258. function MeshSprayApp( elementToBindTo ) {
  259. this.renderer = null;
  260. this.canvas = elementToBindTo;
  261. this.aspectRatio = 1;
  262. this.recalcAspectRatio();
  263. this.scene = null;
  264. this.cameraDefaults = {
  265. posCamera: new THREE.Vector3( 500.0, 500.0, 1000.0 ),
  266. posCameraTarget: new THREE.Vector3( 0, 0, 0 ),
  267. near: 0.1,
  268. far: 10000,
  269. fov: 45
  270. };
  271. this.camera = null;
  272. this.cameraTarget = this.cameraDefaults.posCameraTarget;
  273. this.controls = null;
  274. this.cube = null;
  275. this.pivot = null;
  276. }
  277. MeshSprayApp.prototype.initGL = function () {
  278. this.renderer = new THREE.WebGLRenderer( {
  279. canvas: this.canvas,
  280. antialias: true,
  281. autoClear: true
  282. } );
  283. this.renderer.setClearColor( 0x050505 );
  284. this.scene = new THREE.Scene();
  285. this.camera = new THREE.PerspectiveCamera( this.cameraDefaults.fov, this.aspectRatio, this.cameraDefaults.near, this.cameraDefaults.far );
  286. this.resetCamera();
  287. this.controls = new THREE.TrackballControls( this.camera, this.renderer.domElement );
  288. var ambientLight = new THREE.AmbientLight( 0x404040 );
  289. var directionalLight1 = new THREE.DirectionalLight( 0xC0C090 );
  290. var directionalLight2 = new THREE.DirectionalLight( 0xC0C090 );
  291. directionalLight1.position.set( -100, -50, 100 );
  292. directionalLight2.position.set( 100, 50, -100 );
  293. this.scene.add( directionalLight1 );
  294. this.scene.add( directionalLight2 );
  295. this.scene.add( ambientLight );
  296. var helper = new THREE.GridHelper( 1200, 60, 0xFF4444, 0x404040 );
  297. this.scene.add( helper );
  298. var geometry = new THREE.BoxGeometry( 10, 10, 10 );
  299. var material = new THREE.MeshNormalMaterial();
  300. this.cube = new THREE.Mesh( geometry, material );
  301. this.cube.position.set( 0, 0, 0 );
  302. this.scene.add( this.cube );
  303. this.pivot = new THREE.Object3D();
  304. this.pivot.name = 'Pivot';
  305. this.scene.add( this.pivot );
  306. };
  307. MeshSprayApp.prototype.initContent = function () {
  308. var maxQueueSize = 1024;
  309. var maxWebWorkers = 4;
  310. var radius = 640;
  311. var logger = new THREE.LoaderSupport.ConsoleLogger( false );
  312. this.workerDirector = new THREE.LoaderSupport.WorkerDirector( MeshSpray, logger );
  313. this.workerDirector.setCrossOrigin( 'anonymous' );
  314. var scope = this;
  315. var callbackOnLoad = function ( event ) {
  316. logger.logInfo( 'Worker #' + event.detail.instanceNo + ': Completed loading. (#' + scope.workerDirector.objectsCompleted + ')' );
  317. };
  318. var reportProgress = function( event ) {
  319. document.getElementById( 'feedback' ).innerHTML = event.detail.text;
  320. logger.logInfo( event.detail.text );
  321. };
  322. var callbackMeshAlter = function ( event ) {
  323. var override = new THREE.LoaderSupport.LoadedMeshUserOverride( false, true );
  324. event.detail.side = THREE.DoubleSide;
  325. var mesh = new THREE.Mesh( event.detail.bufferGeometry, event.detail.material );
  326. mesh.name = event.detail.meshName;
  327. override.addMesh( mesh );
  328. return override;
  329. };
  330. var callbacks = new THREE.LoaderSupport.Callbacks();
  331. callbacks.setCallbackOnMeshAlter( callbackMeshAlter );
  332. callbacks.setCallbackOnLoad( callbackOnLoad );
  333. callbacks.setCallbackOnProgress( reportProgress );
  334. this.workerDirector.prepareWorkers( callbacks, maxQueueSize, maxWebWorkers );
  335. var prepData;
  336. var pivot;
  337. var s, t, r, x, y, z;
  338. var globalObjectCount = 0;
  339. for ( var i = 0; i < maxQueueSize; i++ ) {
  340. prepData = new THREE.LoaderSupport.PrepData( 'Triangles_' + i );
  341. pivot = new THREE.Object3D();
  342. s = 2 * Math.PI * Math.random();
  343. t = Math.PI * Math.random();
  344. r = radius * Math.random();
  345. x = r * Math.cos( s ) * Math.sin( t );
  346. y = r * Math.sin( s ) * Math.sin( t );
  347. z = r * Math.cos( t );
  348. pivot.position.set( x, y, z );
  349. this.scene.add( pivot );
  350. prepData.setStreamMeshesTo( pivot );
  351. prepData.quantity = 8192;
  352. prepData.dimension = Math.max( Math.random() * 500, 100 );
  353. prepData.globalObjectCount = globalObjectCount++;
  354. this.workerDirector.enqueueForRun( prepData );
  355. }
  356. this.workerDirector.processQueue();
  357. };
  358. MeshSprayApp.prototype.resizeDisplayGL = function () {
  359. this.controls.handleResize();
  360. this.recalcAspectRatio();
  361. this.renderer.setSize( this.canvas.offsetWidth, this.canvas.offsetHeight, false );
  362. this.updateCamera();
  363. };
  364. MeshSprayApp.prototype.recalcAspectRatio = function () {
  365. this.aspectRatio = ( this.canvas.offsetHeight === 0 ) ? 1 : this.canvas.offsetWidth / this.canvas.offsetHeight;
  366. };
  367. MeshSprayApp.prototype.resetCamera = function () {
  368. this.camera.position.copy( this.cameraDefaults.posCamera );
  369. this.cameraTarget.copy( this.cameraDefaults.posCameraTarget );
  370. this.updateCamera();
  371. };
  372. MeshSprayApp.prototype.updateCamera = function () {
  373. this.camera.aspect = this.aspectRatio;
  374. this.camera.lookAt( this.cameraTarget );
  375. this.camera.updateProjectionMatrix();
  376. };
  377. MeshSprayApp.prototype.render = function () {
  378. if ( ! this.renderer.autoClear ) this.renderer.clear();
  379. this.controls.update();
  380. this.cube.rotation.x += 0.05;
  381. this.cube.rotation.y += 0.05;
  382. this.renderer.render( this.scene, this.camera );
  383. };
  384. return MeshSprayApp;
  385. })();
  386. var app = new MeshSprayApp( document.getElementById( 'example' ) );
  387. // init three.js example application
  388. var resizeWindow = function () {
  389. app.resizeDisplayGL();
  390. };
  391. var render = function () {
  392. requestAnimationFrame( render );
  393. app.render();
  394. };
  395. window.addEventListener( 'resize', resizeWindow, false );
  396. console.log( 'Starting initialisation phase...' );
  397. app.initGL();
  398. app.resizeDisplayGL();
  399. app.initContent();
  400. render();
  401. </script>
  402. </body>
  403. </html>