RaytracingRenderer.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.RaytracingRenderer = function ( parameters ) {
  5. console.log( 'THREE.RaytracingRenderer', THREE.REVISION );
  6. parameters = parameters || {};
  7. var canvas = document.createElement( 'canvas' );
  8. var context = canvas.getContext( '2d', {
  9. alpha: parameters.alpha === true
  10. } );
  11. var canvasWidth, canvasHeight;
  12. var canvasWidthHalf, canvasHeightHalf;
  13. var clearColor = new THREE.Color( 0x000000 );
  14. var blockX, blockY;
  15. var blockSize = 64;
  16. var canvasBlock = document.createElement( 'canvas' );
  17. canvasBlock.width = blockSize;
  18. canvasBlock.height = blockSize;
  19. var contextBlock = canvasBlock.getContext( '2d', {
  20. alpha: parameters.alpha === true
  21. } );
  22. var imagedata = contextBlock.getImageData( 0, 0, blockSize, blockSize );
  23. var data = imagedata.data;
  24. var color = new THREE.Color();
  25. var lightColor = new THREE.Color();
  26. var origin = new THREE.Vector3();
  27. var direction = new THREE.Vector3();
  28. var raycaster = new THREE.Raycaster( origin, direction );
  29. var raycasterLight = new THREE.Raycaster();
  30. var objects;
  31. var lights = [];
  32. this.domElement = canvas;
  33. this.autoClear = true;
  34. this.setClearColor = function ( color, alpha ) {
  35. clearColor.set( color );
  36. };
  37. this.setSize = function ( width, height ) {
  38. canvas.width = width;
  39. canvas.height = height;
  40. canvasWidth = canvas.width;
  41. canvasHeight = canvas.height;
  42. canvasWidthHalf = Math.floor( canvasWidth / 2 );
  43. canvasHeightHalf = Math.floor( canvasHeight / 2 );
  44. context.fillStyle = 'white';
  45. };
  46. this.setSize( canvas.width, canvas.height );
  47. this.clear = function () {
  48. };
  49. var renderBlock = function () {
  50. for ( var i = 0, l = data.length; i < l; i += 4 ) {
  51. data[ i ] = 0;
  52. data[ i + 1 ] = 0;
  53. data[ i + 2 ] = 0;
  54. }
  55. var index = 0;
  56. for ( var y = 0; y < blockSize; y ++ ) {
  57. for ( var x = 0; x < blockSize; x ++, index += 4 ) {
  58. direction.set( x + blockX - canvasWidthHalf, y + blockY - canvasHeightHalf, - 500 );
  59. direction.normalize();
  60. var intersections = raycaster.intersectObjects( objects, true );
  61. if ( intersections.length > 0 ) {
  62. var intersection = intersections[ 0 ];
  63. var point = intersection.point;
  64. var object = intersection.object;
  65. var material = object.material;
  66. var face = intersection.face;
  67. if ( material.vertexColors === THREE.NoColors ) {
  68. color.copy( material.color );
  69. } else if ( material.vertexColors === THREE.FaceColors ) {
  70. color.copy( face.color );
  71. }
  72. if ( material instanceof THREE.MeshLambertMaterial ) {
  73. lightColor.set( 0, 0, 0 );
  74. raycasterLight.ray.origin.copy( point );
  75. for ( var i = 0, l = lights.length; i < l; i ++ ) {
  76. var light = lights[ i ];
  77. raycasterLight.ray.direction.copy( light.position ).sub( point ).normalize();
  78. var intersections = raycasterLight.intersectObjects( objects, true );
  79. if ( intersections.length === 0 ) {
  80. var dot = 1.0 - Math.max( face.normal.dot( raycasterLight.ray.direction ), 0 );
  81. var intensity = dot * light.intensity;
  82. lightColor.r += light.color.r * intensity;
  83. lightColor.g += light.color.g * intensity;
  84. lightColor.b += light.color.b * intensity;
  85. }
  86. }
  87. color.multiply( lightColor );
  88. }
  89. data[ index ] = color.r * 255;
  90. data[ index + 1 ] = color.g * 255;
  91. data[ index + 2 ] = color.b * 255;
  92. }
  93. }
  94. }
  95. context.putImageData( imagedata, blockX, blockY );
  96. blockX += blockSize;
  97. if ( blockX >= canvasWidth ) {
  98. blockX = 0;
  99. blockY += blockSize;
  100. if ( blockY >= canvasHeight ) return;
  101. }
  102. context.fillRect( blockX, blockY, blockSize, blockSize );
  103. requestAnimationFrame( renderBlock );
  104. };
  105. this.render = function ( scene, camera ) {
  106. if ( this.autoClear === true ) this.clear();
  107. blockX = 0;
  108. blockY = 0;
  109. origin.copy( camera.position );
  110. objects = scene.children;
  111. // collect lights
  112. lights.length = 0;
  113. scene.traverse( function ( object ) {
  114. if ( object instanceof THREE.Light ) {
  115. lights.push( object );
  116. }
  117. } )
  118. renderBlock();
  119. }
  120. };