2
0

RaytracingRenderer.js 5.1 KB

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