CSS3DRenderer.js 6.3 KB

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