CSS3DRenderer.js 6.4 KB

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