RaytracingWorkerRenderer.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * The way to use RaytracingWorkerRenderer is similar to RaytracingRenderer
  3. * except that it is simply a coordinator for workers. The workers compute
  4. * the pixel values and this renderer simply paints it to the Canvas. As such,
  5. * it is simply a renderer.
  6. *
  7. * TODO
  8. * - serialize scene and hand it to workers
  9. * - renderer thread to hand block jobs to workers
  10. * - pass worker path as option
  11. *
  12. * @author zz85 / http://github.com/zz85
  13. */
  14. THREE.RaytracingWorkerRenderer = function ( parameters ) {
  15. console.log( 'THREE.RaytracingWorkerRenderer', THREE.REVISION );
  16. parameters = parameters || {};
  17. var scope = this;
  18. var pool = [];
  19. var canvas = document.createElement( 'canvas' );
  20. var context = canvas.getContext( '2d', {
  21. alpha: parameters.alpha === true
  22. } );
  23. var maxRecursionDepth = 3;
  24. var canvasWidth, canvasHeight;
  25. var canvasWidthHalf, canvasHeightHalf;
  26. var clearColor = new THREE.Color( 0x000000 );
  27. this.domElement = canvas;
  28. this.autoClear = true;
  29. var workers = parameters.workers || navigator.hardwareConcurrency || 4;
  30. console.log('%cSpinning off ' + workers + ' Workers ', 'font-size: 20px; background: black; color: white; font-family: monospace;');
  31. for (var i = 0; i < workers; i++) {
  32. var worker = new Worker('js/renderers/RaytracingWorker.js');
  33. worker.onmessage = function(e) {
  34. var data = e.data;
  35. if (!data) return;
  36. if (data.blockSize) {
  37. var d = data.data;
  38. var imagedata = new ImageData(new Uint8ClampedArray(d), data.blockSize, data.blockSize);
  39. renderer.ctx.putImageData( imagedata, data.blockX, data.blockY );
  40. } else if (data.type == 'complete') {
  41. // TODO can terminate worker here or schedule more other jobs...
  42. }
  43. }
  44. pool.push(worker);
  45. }
  46. this.setClearColor = function ( color, alpha ) {
  47. clearColor.set( color );
  48. };
  49. this.setPixelRatio = function () {};
  50. this.setSize = function ( width, height ) {
  51. canvas.width = width;
  52. canvas.height = height;
  53. canvasWidth = canvas.width;
  54. canvasHeight = canvas.height;
  55. canvasWidthHalf = Math.floor( canvasWidth / 2 );
  56. canvasHeightHalf = Math.floor( canvasHeight / 2 );
  57. context.fillStyle = 'white';
  58. pool.forEach(function(p, i) {
  59. p.postMessage({
  60. init: [ width, height ],
  61. worker: i,
  62. workers: pool.length,
  63. blockSize: 64
  64. })
  65. })
  66. };
  67. this.setSize( canvas.width, canvas.height );
  68. this.clear = function () {
  69. };
  70. //
  71. this.render = function ( scene, camera ) {
  72. reallyThen = Date.now()
  73. pool.forEach(function(p) {
  74. p.postMessage({
  75. render: true
  76. })
  77. });
  78. };
  79. this.ctx = context;
  80. };
  81. THREE.EventDispatcher.prototype.apply( THREE.RaytracingWorkerRenderer.prototype );