RaytracingRenderer.js 5.3 KB

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