RaytracingRenderer.js 5.3 KB

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