CSS3DRenderer.js 6.4 KB

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