123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- /**
- * RaytracingRenderer renders by raytracing it's scene. However, it does not
- * compute the pixels itself but it hands off and coordinates the tasks for workers.
- * The workers compute the pixel values and this renderer simply paints it to the Canvas.
- *
- * @author zz85 / http://github.com/zz85
- */
- THREE.RaytracingRenderer = function ( parameters ) {
- console.log( 'THREE.RaytracingRenderer', THREE.REVISION );
- parameters = parameters || {};
- var scope = this;
- var pool = [];
- var renderering = false;
- var canvas = document.createElement( 'canvas' );
- var context = canvas.getContext( '2d', {
- alpha: parameters.alpha === true
- } );
- var canvasWidth, canvasHeight;
- var clearColor = new THREE.Color( 0x000000 );
- this.domElement = canvas;
- this.autoClear = true;
- var workers = parameters.workers;
- var blockSize = parameters.blockSize || 64;
- this.randomize = parameters.randomize;
- var toRender = [], workerId = 0, sceneId = 0;
- console.log( '%cSpinning off ' + workers + ' Workers ', 'font-size: 20px; background: black; color: white; font-family: monospace;' );
- this.setWorkers = function ( w ) {
- workers = w || navigator.hardwareConcurrency || 4;
- while ( pool.length < workers ) {
- var worker = new Worker( parameters.workerPath );
- worker.id = workerId ++;
- worker.onmessage = function ( e ) {
- var data = e.data;
- if ( ! data ) return;
- if ( data.blockSize && sceneId == data.sceneId ) { // we match sceneId here to be sure
- var imagedata = new ImageData( new Uint8ClampedArray( data.data ), data.blockSize, data.blockSize );
- context.putImageData( imagedata, data.blockX, data.blockY );
- // completed
- console.log( 'Worker ' + this.id, data.time / 1000, ( Date.now() - reallyThen ) / 1000 + ' s' );
- if ( pool.length > workers ) {
- pool.splice( pool.indexOf( this ), 1 );
- return this.terminate();
- }
- renderNext( this );
- }
- };
- worker.color = new THREE.Color().setHSL( Math.random(), 0.8, 0.8 ).getHexString();
- pool.push( worker );
- updateSettings( worker );
- if ( renderering ) {
- worker.postMessage( {
- scene: sceneJSON,
- camera: cameraJSON,
- annex: materials,
- sceneId: sceneId
- } );
- renderNext( worker );
- }
- }
- if ( ! renderering ) {
- while ( pool.length > workers ) {
- pool.pop().terminate();
- }
- }
- };
- this.setWorkers( workers );
- this.setClearColor = function ( color /*, alpha */ ) {
- clearColor.set( color );
- };
- this.setPixelRatio = function () {};
- this.setSize = function ( width, height ) {
- canvas.width = width;
- canvas.height = height;
- canvasWidth = canvas.width;
- canvasHeight = canvas.height;
- context.fillStyle = 'white';
- pool.forEach( updateSettings );
- };
- this.setSize( canvas.width, canvas.height );
- this.clear = function () {
- };
- //
- var totalBlocks, xblocks, yblocks;
- function updateSettings( worker ) {
- worker.postMessage( {
- init: [ canvasWidth, canvasHeight ],
- worker: worker.id,
- // workers: pool.length,
- blockSize: blockSize
- } );
- }
- function renderNext( worker ) {
- if ( ! toRender.length ) {
- renderering = false;
- return scope.dispatchEvent( { type: "complete" } );
- }
- var current = toRender.pop();
- var blockX = ( current % xblocks ) * blockSize;
- var blockY = ( current / xblocks | 0 ) * blockSize;
- worker.postMessage( {
- render: true,
- x: blockX,
- y: blockY,
- sceneId: sceneId
- } );
- context.fillStyle = '#' + worker.color;
- context.fillRect( blockX, blockY, blockSize, blockSize );
- }
- var materials = {};
- var sceneJSON, cameraJSON, reallyThen;
- // additional properties that were not serialize automatically
- var _annex = {
- mirror: 1,
- reflectivity: 1,
- refractionRatio: 1,
- glass: 1
- };
- function serializeObject( o ) {
- var mat = o.material;
- if ( ! mat || mat.uuid in materials ) return;
- var props = {};
- for ( var m in _annex ) {
- if ( mat[ m ] !== undefined ) {
- props[ m ] = mat[ m ];
- }
- }
- materials[ mat.uuid ] = props;
- }
- this.render = function ( scene, camera ) {
- renderering = true;
- // update scene graph
- if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
- // update camera matrices
- if ( camera.parent === null ) camera.updateMatrixWorld();
- sceneJSON = scene.toJSON();
- cameraJSON = camera.toJSON();
- ++ sceneId;
- scene.traverse( serializeObject );
- pool.forEach( function ( worker ) {
- worker.postMessage( {
- scene: sceneJSON,
- camera: cameraJSON,
- annex: materials,
- sceneId: sceneId
- } );
- } );
- context.clearRect( 0, 0, canvasWidth, canvasHeight );
- reallyThen = Date.now();
- xblocks = Math.ceil( canvasWidth / blockSize );
- yblocks = Math.ceil( canvasHeight / blockSize );
- totalBlocks = xblocks * yblocks;
- toRender = [];
- for ( var i = 0; i < totalBlocks; i ++ ) {
- toRender.push( i );
- }
- // Randomize painting :)
- if ( scope.randomize ) {
- for ( var i = 0; i < totalBlocks; i ++ ) {
- var swap = Math.random() * totalBlocks | 0;
- var tmp = toRender[ swap ];
- toRender[ swap ] = toRender[ i ];
- toRender[ i ] = tmp;
- }
- }
- pool.forEach( renderNext );
- };
- };
- Object.assign( THREE.RaytracingRenderer.prototype, THREE.EventDispatcher.prototype );
|