RaytracingRenderer.js 5.3 KB

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