CSS3DRenderer.js 6.6 KB

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