RaytracingRenderer.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. 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.fillStyle = clearColor.getStyle();
  152. context.fillRect( 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 };