CSS3DRenderer.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import {
  2. Matrix4,
  3. Object3D
  4. } from '../../../build/three.module.js';
  5. /**
  6. * Based on http://www.emagix.net/academic/mscs-project/item/camera-sync-with-css3-and-webgl-threejs
  7. */
  8. class CSS3DObject extends Object3D {
  9. constructor( element ) {
  10. super();
  11. this.element = element || document.createElement( 'div' );
  12. this.element.style.position = 'absolute';
  13. this.element.style.pointerEvents = 'auto';
  14. this.addEventListener( 'removed', function () {
  15. this.traverse( function ( object ) {
  16. if ( object.element instanceof Element && object.element.parentNode !== null ) {
  17. object.element.parentNode.removeChild( object.element );
  18. }
  19. } );
  20. } );
  21. }
  22. copy( source, recursive ) {
  23. super.copy( source, recursive );
  24. this.element = source.element.cloneNode( true );
  25. return this;
  26. }
  27. }
  28. CSS3DObject.prototype.isCSS3DObject = true;
  29. class CSS3DSprite extends CSS3DObject {
  30. constructor( element ) {
  31. super( element );
  32. }
  33. }
  34. CSS3DSprite.prototype.isCSS3DSprite = true;
  35. //
  36. const _matrix = new Matrix4();
  37. class CSS3DRenderer {
  38. constructor() {
  39. const _this = this;
  40. let _width, _height;
  41. let _widthHalf, _heightHalf;
  42. const cache = {
  43. camera: { fov: 0, style: '' },
  44. objects: new WeakMap()
  45. };
  46. const domElement = document.createElement( 'div' );
  47. domElement.style.overflow = 'hidden';
  48. this.domElement = domElement;
  49. const cameraElement = document.createElement( 'div' );
  50. cameraElement.style.transformStyle = 'preserve-3d';
  51. cameraElement.style.pointerEvents = 'none';
  52. domElement.appendChild( cameraElement );
  53. this.getSize = function () {
  54. return {
  55. width: _width,
  56. height: _height
  57. };
  58. };
  59. this.render = function ( scene, camera ) {
  60. const fov = camera.projectionMatrix.elements[ 5 ] * _heightHalf;
  61. if ( cache.camera.fov !== fov ) {
  62. domElement.style.perspective = camera.isPerspectiveCamera ? fov + 'px' : '';
  63. cache.camera.fov = fov;
  64. }
  65. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  66. if ( camera.parent === null ) camera.updateMatrixWorld();
  67. let tx, ty;
  68. if ( camera.isOrthographicCamera ) {
  69. tx = - ( camera.right + camera.left ) / 2;
  70. ty = ( camera.top + camera.bottom ) / 2;
  71. }
  72. const cameraCSSMatrix = camera.isOrthographicCamera ?
  73. 'scale(' + fov + ')' + 'translate(' + epsilon( tx ) + 'px,' + epsilon( ty ) + 'px)' + getCameraCSSMatrix( camera.matrixWorldInverse ) :
  74. 'translateZ(' + fov + 'px)' + getCameraCSSMatrix( camera.matrixWorldInverse );
  75. const style = cameraCSSMatrix +
  76. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)';
  77. if ( cache.camera.style !== style ) {
  78. cameraElement.style.transform = style;
  79. cache.camera.style = style;
  80. }
  81. renderObject( scene, scene, camera, cameraCSSMatrix );
  82. };
  83. this.setSize = function ( width, height ) {
  84. _width = width;
  85. _height = height;
  86. _widthHalf = _width / 2;
  87. _heightHalf = _height / 2;
  88. domElement.style.width = width + 'px';
  89. domElement.style.height = height + 'px';
  90. cameraElement.style.width = width + 'px';
  91. cameraElement.style.height = height + 'px';
  92. };
  93. function epsilon( value ) {
  94. return Math.abs( value ) < 1e-10 ? 0 : value;
  95. }
  96. function getCameraCSSMatrix( matrix ) {
  97. const elements = matrix.elements;
  98. return 'matrix3d(' +
  99. epsilon( elements[ 0 ] ) + ',' +
  100. epsilon( - elements[ 1 ] ) + ',' +
  101. epsilon( elements[ 2 ] ) + ',' +
  102. epsilon( elements[ 3 ] ) + ',' +
  103. epsilon( elements[ 4 ] ) + ',' +
  104. epsilon( - elements[ 5 ] ) + ',' +
  105. epsilon( elements[ 6 ] ) + ',' +
  106. epsilon( elements[ 7 ] ) + ',' +
  107. epsilon( elements[ 8 ] ) + ',' +
  108. epsilon( - elements[ 9 ] ) + ',' +
  109. epsilon( elements[ 10 ] ) + ',' +
  110. epsilon( elements[ 11 ] ) + ',' +
  111. epsilon( elements[ 12 ] ) + ',' +
  112. epsilon( - elements[ 13 ] ) + ',' +
  113. epsilon( elements[ 14 ] ) + ',' +
  114. epsilon( elements[ 15 ] ) +
  115. ')';
  116. }
  117. function getObjectCSSMatrix( matrix ) {
  118. const elements = matrix.elements;
  119. const matrix3d = 'matrix3d(' +
  120. epsilon( elements[ 0 ] ) + ',' +
  121. epsilon( elements[ 1 ] ) + ',' +
  122. epsilon( elements[ 2 ] ) + ',' +
  123. epsilon( elements[ 3 ] ) + ',' +
  124. epsilon( - elements[ 4 ] ) + ',' +
  125. epsilon( - elements[ 5 ] ) + ',' +
  126. epsilon( - elements[ 6 ] ) + ',' +
  127. epsilon( - elements[ 7 ] ) + ',' +
  128. epsilon( elements[ 8 ] ) + ',' +
  129. epsilon( elements[ 9 ] ) + ',' +
  130. epsilon( elements[ 10 ] ) + ',' +
  131. epsilon( elements[ 11 ] ) + ',' +
  132. epsilon( elements[ 12 ] ) + ',' +
  133. epsilon( elements[ 13 ] ) + ',' +
  134. epsilon( elements[ 14 ] ) + ',' +
  135. epsilon( elements[ 15 ] ) +
  136. ')';
  137. return 'translate(-50%,-50%)' + matrix3d;
  138. }
  139. function renderObject( object, scene, camera, cameraCSSMatrix ) {
  140. if ( object.isCSS3DObject ) {
  141. object.onBeforeRender( _this, scene, camera );
  142. let style;
  143. if ( object.isCSS3DSprite ) {
  144. // http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/
  145. _matrix.copy( camera.matrixWorldInverse );
  146. _matrix.transpose();
  147. _matrix.copyPosition( object.matrixWorld );
  148. _matrix.scale( object.scale );
  149. _matrix.elements[ 3 ] = 0;
  150. _matrix.elements[ 7 ] = 0;
  151. _matrix.elements[ 11 ] = 0;
  152. _matrix.elements[ 15 ] = 1;
  153. style = getObjectCSSMatrix( _matrix );
  154. } else {
  155. style = getObjectCSSMatrix( object.matrixWorld );
  156. }
  157. const element = object.element;
  158. const cachedObject = cache.objects.get( object );
  159. if ( cachedObject === undefined || cachedObject.style !== style ) {
  160. element.style.transform = style;
  161. const objectData = { style: style };
  162. cache.objects.set( object, objectData );
  163. }
  164. element.style.display = object.visible ? '' : 'none';
  165. if ( element.parentNode !== cameraElement ) {
  166. cameraElement.appendChild( element );
  167. }
  168. object.onAfterRender( _this, scene, camera );
  169. }
  170. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  171. renderObject( object.children[ i ], scene, camera, cameraCSSMatrix );
  172. }
  173. }
  174. }
  175. }
  176. export { CSS3DObject, CSS3DSprite, CSS3DRenderer };