CSS3DRenderer.js 6.8 KB

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