CSS3DRenderer.js 6.6 KB

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