RaytracingRenderer.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 clearColor = new THREE.Color( 0x000000 );
  21. this.domElement = canvas;
  22. this.autoClear = true;
  23. var workers = parameters.workers;
  24. var blockSize = parameters.blockSize || 64;
  25. this.randomize = parameters.randomize;
  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. updateSettings( worker );
  51. if ( renderering ) {
  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. context.fillStyle = 'white';
  78. pool.forEach( updateSettings );
  79. };
  80. this.setSize( canvas.width, canvas.height );
  81. this.clear = function () {
  82. };
  83. //
  84. var totalBlocks, xblocks, yblocks;
  85. function updateSettings( worker ) {
  86. worker.postMessage( {
  87. init: [ canvasWidth, canvasHeight ],
  88. worker: worker.id,
  89. // workers: pool.length,
  90. blockSize: blockSize
  91. } );
  92. }
  93. function renderNext( worker ) {
  94. if ( ! toRender.length ) {
  95. renderering = false;
  96. return scope.dispatchEvent( { type: "complete" } );
  97. }
  98. var current = toRender.pop();
  99. var blockX = ( current % xblocks ) * blockSize;
  100. var blockY = ( current / xblocks | 0 ) * blockSize;
  101. worker.postMessage( {
  102. render: true,
  103. x: blockX,
  104. y: blockY,
  105. sceneId: sceneId
  106. } );
  107. context.fillStyle = '#' + worker.color;
  108. context.fillRect( blockX, blockY, blockSize, blockSize );
  109. }
  110. var materials = {};
  111. var sceneJSON, cameraJSON, reallyThen;
  112. // additional properties that were not serialize automatically
  113. var _annex = {
  114. mirror: 1,
  115. reflectivity: 1,
  116. refractionRatio: 1,
  117. glass: 1
  118. };
  119. function serializeObject( o ) {
  120. var mat = o.material;
  121. if ( ! mat || mat.uuid in materials ) return;
  122. var props = {};
  123. for ( var m in _annex ) {
  124. if ( mat[ m ] !== undefined ) {
  125. props[ m ] = mat[ m ];
  126. }
  127. }
  128. materials[ mat.uuid ] = props;
  129. }
  130. this.render = function ( scene, camera ) {
  131. renderering = true;
  132. // update scene graph
  133. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  134. // update camera matrices
  135. if ( camera.parent === null ) camera.updateMatrixWorld();
  136. sceneJSON = scene.toJSON();
  137. cameraJSON = camera.toJSON();
  138. ++ sceneId;
  139. scene.traverse( serializeObject );
  140. pool.forEach( function( worker ) {
  141. worker.postMessage( {
  142. scene: sceneJSON,
  143. camera: cameraJSON,
  144. annex: materials,
  145. sceneId: sceneId
  146. } );
  147. } );
  148. context.clearRect( 0, 0, canvasWidth, canvasHeight );
  149. reallyThen = Date.now();
  150. xblocks = Math.ceil( canvasWidth / blockSize );
  151. yblocks = Math.ceil( canvasHeight / blockSize );
  152. totalBlocks = xblocks * yblocks;
  153. toRender = [];
  154. for ( var i = 0; i < totalBlocks; i ++ ) {
  155. toRender.push( i );
  156. }
  157. // Randomize painting :)
  158. if ( scope.randomize ) {
  159. for ( var i = 0; i < totalBlocks; i ++ ) {
  160. var swap = Math.random() * totalBlocks | 0;
  161. var tmp = toRender[ swap ];
  162. toRender[ swap ] = toRender[ i ];
  163. toRender[ i ] = tmp;
  164. }
  165. }
  166. pool.forEach( renderNext );
  167. };
  168. };
  169. Object.assign( THREE.RaytracingRenderer.prototype, THREE.EventDispatcher.prototype );