CSS3DRenderer.js 6.3 KB

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