RaytracingWorkerRenderer.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. * The way to use RaytracingWorkerRenderer is similar to RaytracingRenderer
  3. * except that it is simply a coordinator for workers. The workers compute
  4. * the pixel values and this renderer simply paints it to the Canvas. As such,
  5. * it is simply a renderer.
  6. *
  7. * TODO
  8. * - serialize scene and hand it to workers
  9. * - pass worker path as option
  10. *
  11. * @author zz85 / http://github.com/zz85
  12. */
  13. THREE.RaytracingWorkerRenderer = function ( parameters ) {
  14. console.log( 'THREE.RaytracingWorkerRenderer', THREE.REVISION );
  15. parameters = parameters || {};
  16. var scope = this;
  17. var pool = [];
  18. var renderering = false;
  19. var canvas = document.createElement( 'canvas' );
  20. var context = canvas.getContext( '2d', {
  21. alpha: parameters.alpha === true
  22. } );
  23. var maxRecursionDepth = 3;
  24. var canvasWidth, canvasHeight;
  25. var canvasWidthHalf, canvasHeightHalf;
  26. var clearColor = new THREE.Color( 0x000000 );
  27. this.domElement = canvas;
  28. this.autoClear = true;
  29. var workers = parameters.workers;
  30. var blockSize = parameters.blockSize || 64;
  31. console.log( '%cSpinning off ' + workers + ' Workers ', 'font-size: 20px; background: black; color: white; font-family: monospace;' );
  32. this.setWorkers = function( w ) {
  33. workers = w || navigator.hardwareConcurrency || 4;
  34. while ( pool.length < workers ) {
  35. var i = pool.length;
  36. var worker = new Worker( 'js/renderers/RaytracingWorker.js' );
  37. worker.onmessage = function( e ) {
  38. var data = e.data;
  39. if ( ! data ) return;
  40. if ( data.blockSize ) {
  41. var d = data.data;
  42. var imagedata = new ImageData( new Uint8ClampedArray( d ), data.blockSize, data.blockSize );
  43. context.putImageData( imagedata, data.blockX, data.blockY );
  44. } else if ( data.type == 'complete' ) {
  45. console.log( 'Worker ' + data.worker, data.time / 1000, ( Date.now() - reallyThen ) / 1000 + ' s' );
  46. if ( pool.length > workers ) {
  47. pool.splice( pool.indexOf( this ), 1 )
  48. return this.terminate();
  49. }
  50. renderNext( this );
  51. }
  52. }
  53. worker.color = new THREE.Color().setHSL( i / workers, 0.8, 0.8 ).getHexString();
  54. pool.push( worker );
  55. if ( renderering ) {
  56. worker.postMessage( {
  57. init: [ canvasWidth, canvasHeight ],
  58. worker: i,
  59. workers: pool.length,
  60. blockSize: blockSize,
  61. initScene: initScene.toString()
  62. } );
  63. renderNext( worker );
  64. }
  65. }
  66. if ( ! renderering ) {
  67. while ( pool.length > workers ) {
  68. pool.pop().terminate();
  69. }
  70. }
  71. };
  72. this.setWorkers( workers );
  73. this.setClearColor = function ( color, alpha ) {
  74. clearColor.set( color );
  75. };
  76. this.setPixelRatio = function () {};
  77. this.setSize = function ( width, height ) {
  78. canvas.width = width;
  79. canvas.height = height;
  80. canvasWidth = canvas.width;
  81. canvasHeight = canvas.height;
  82. canvasWidthHalf = Math.floor( canvasWidth / 2 );
  83. canvasHeightHalf = Math.floor( canvasHeight / 2 );
  84. context.fillStyle = 'white';
  85. pool.forEach( function( p, i ) {
  86. p.postMessage( {
  87. init: [ width, height ],
  88. worker: i,
  89. workers: pool.length,
  90. blockSize: blockSize
  91. } );
  92. } );
  93. };
  94. this.setSize( canvas.width, canvas.height );
  95. this.clear = function () {
  96. };
  97. //
  98. var nextBlock, totalBlocks, xblocks, yblocks;
  99. function renderNext( worker ) {
  100. var current = nextBlock ++;
  101. if ( nextBlock > totalBlocks ) {
  102. renderering = false;
  103. return scope.dispatchEvent( { type: "complete" } );
  104. }
  105. var blockX = ( current % xblocks ) * blockSize;
  106. var blockY = ( current / xblocks | 0 ) * blockSize;
  107. worker.postMessage( {
  108. render: true,
  109. x: blockX,
  110. y: blockY
  111. } );
  112. context.fillStyle = '#' + worker.color;
  113. context.fillRect( blockX, blockY, blockSize, blockSize );
  114. }
  115. var materials = {};
  116. var _annex = {
  117. mirror: 1,
  118. reflectivity: 1,
  119. refractionRatio: 1,
  120. glass: 1,
  121. vertexColors: 1,
  122. shading: 1
  123. };
  124. function serializeObject( o ) {
  125. var mat = o.material;
  126. if ( ! mat || mat.uuid in materials ) return;
  127. console.log(mat);
  128. var props = {};
  129. for (var m in _annex) {
  130. if ( mat[m] !== undefined ) {
  131. props[ m ] = mat[ m ];
  132. }
  133. }
  134. materials[ mat.uuid ] = props;
  135. }
  136. this.render = function ( scene, camera ) {
  137. renderering = true;
  138. // update scene graph
  139. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  140. // update camera matrices
  141. if ( camera.parent === null ) camera.updateMatrixWorld();
  142. var sceneJSON = scene.toJSON();
  143. var cameraJSON = camera.toJSON();
  144. scene.traverse( serializeObject );
  145. pool.forEach(function(worker) {
  146. worker.postMessage({
  147. scene: sceneJSON,
  148. camera: cameraJSON,
  149. annex: materials
  150. });
  151. });
  152. context.clearRect( 0, 0, canvasWidth, canvasHeight );
  153. reallyThen = Date.now();
  154. xblocks = Math.ceil( canvasWidth / blockSize );
  155. yblocks = Math.ceil( canvasHeight / blockSize );
  156. nextBlock = 0;
  157. totalBlocks = xblocks * yblocks;
  158. pool.forEach( renderNext );
  159. };
  160. };
  161. THREE.EventDispatcher.prototype.apply( THREE.RaytracingWorkerRenderer.prototype );