RaytracingRenderer.js 5.2 KB

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