WebGLMultiview.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /**
  2. * @author fernandojsg / http://fernandojsg.com
  3. * @author Takahiro https://github.com/takahirox
  4. */
  5. import { WebGLMultiviewRenderTarget } from '../WebGLMultiviewRenderTarget.js';
  6. import { Matrix3 } from '../../math/Matrix3.js';
  7. import { Matrix4 } from '../../math/Matrix4.js';
  8. function WebGLMultiview( renderer, requested, options ) {
  9. options = Object.assign( {}, { debug: false }, options );
  10. var DEFAULT_NUMVIEWS = 2;
  11. var gl = renderer.context;
  12. var canvas = renderer.domElement;
  13. var capabilities = renderer.capabilities;
  14. var properties = renderer.properties;
  15. var renderTarget, currentRenderTarget;
  16. var mat3, mat4, cameraArray;
  17. this.getMaxViews = function () {
  18. return capabilities.maxMultiviewViews;
  19. };
  20. this.getNumViews = function () {
  21. if ( renderTarget && renderer.getRenderTarget() === renderTarget ) {
  22. return renderTarget.numViews;
  23. }
  24. return 0;
  25. };
  26. function getCameraArray( camera ) {
  27. if ( camera.isArrayCamera ) return camera.cameras;
  28. cameraArray[ 0 ] = camera;
  29. return cameraArray;
  30. }
  31. //
  32. this.isAvailable = function () {
  33. return capabilities.multiview;
  34. };
  35. this.isEnabled = function () {
  36. return requested && this.isAvailable();
  37. };
  38. if ( options.debug ) {
  39. if ( requested && ! this.isAvailable() ) {
  40. console.warn( 'WebGLRenderer: Multiview requested but not supported by the browser' );
  41. } else if ( requested !== false && this.isAvailable() ) {
  42. console.info( 'WebGLRenderer: Multiview enabled' );
  43. }
  44. }
  45. this.updateCameraProjectionMatrices = function ( camera, uniforms ) {
  46. var cameras = getCameraArray( camera );
  47. for ( var i = 0; i < cameras.length; i ++ ) {
  48. mat4[ i ].copy( cameras[ i ].projectionMatrix );
  49. }
  50. uniforms.setValue( gl, 'projectionMatrices', mat4 );
  51. };
  52. this.updateCameraViewMatrices = function ( camera, uniforms ) {
  53. var cameras = getCameraArray( camera );
  54. for ( var i = 0; i < cameras.length; i ++ ) {
  55. mat4[ i ].copy( cameras[ i ].matrixWorldInverse );
  56. }
  57. uniforms.setValue( gl, 'viewMatrices', mat4 );
  58. };
  59. this.updateObjectMatrices = function ( object, camera, uniforms ) {
  60. var cameras = getCameraArray( camera );
  61. for ( var i = 0; i < cameras.length; i ++ ) {
  62. mat4[ i ].multiplyMatrices( cameras[ i ].matrixWorldInverse, object.matrixWorld );
  63. mat3[ i ].getNormalMatrix( mat4[ i ] );
  64. }
  65. uniforms.setValue( gl, 'modelViewMatrices', mat4 );
  66. uniforms.setValue( gl, 'normalMatrices', mat3 );
  67. };
  68. this.attachRenderTarget = function ( camera ) {
  69. currentRenderTarget = renderer.getRenderTarget();
  70. // Resize if needed
  71. var width = canvas.width;
  72. var height = canvas.height;
  73. if ( camera.isArrayCamera ) {
  74. // Every camera must have the same size, so we just get the size from the first one
  75. var bounds = camera.cameras[ 0 ].bounds;
  76. width *= bounds.z;
  77. height *= bounds.w;
  78. renderTarget.setNumViews( camera.cameras.length );
  79. } else {
  80. renderTarget.setNumViews( DEFAULT_NUMVIEWS );
  81. }
  82. renderTarget.setSize( width, height );
  83. renderer.setRenderTarget( renderTarget );
  84. };
  85. this.detachRenderTarget = function ( camera ) {
  86. var viewFramebuffers = properties.get( renderTarget ).__webglViewFramebuffers;
  87. // @todo Use actual framebuffer
  88. gl.bindFramebuffer( gl.FRAMEBUFFER, null );
  89. if ( camera.isArrayCamera ) {
  90. for ( var i = 0; i < camera.cameras.length; i ++ ) {
  91. var bounds = camera.cameras[ i ].bounds;
  92. var x = bounds.x * canvas.width;
  93. var y = bounds.y * canvas.height;
  94. var width = bounds.z * canvas.width;
  95. var height = bounds.w * canvas.height;
  96. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, viewFramebuffers[ i ] );
  97. gl.blitFramebuffer( 0, 0, width, height, x, y, x + width, y + height, gl.COLOR_BUFFER_BIT, gl.NEAREST );
  98. }
  99. } else {
  100. // If no array camera, blit just one view
  101. gl.bindFramebuffer( gl.READ_FRAMEBUFFER, viewFramebuffers[ 0 ] );
  102. gl.blitFramebuffer( 0, 0, canvas.width, canvas.height, 0, 0, canvas.width, canvas.height, gl.COLOR_BUFFER_BIT, gl.NEAREST );
  103. }
  104. renderer.setRenderTarget( currentRenderTarget );
  105. };
  106. if ( this.isEnabled() ) {
  107. renderTarget = new WebGLMultiviewRenderTarget( canvas.width, canvas.height, this.numViews );
  108. // Auxiliary matrices to be used when updating arrays of uniforms
  109. mat4 = [];
  110. mat3 = [];
  111. cameraArray = [];
  112. for ( var i = 0; i < this.getMaxViews(); i ++ ) {
  113. mat4[ i ] = new Matrix4();
  114. mat3[ i ] = new Matrix3();
  115. }
  116. }
  117. }
  118. export { WebGLMultiview };