CSS3DRenderer.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. import {
  2. Matrix4,
  3. Object3D,
  4. Quaternion,
  5. Vector3
  6. } from 'three';
  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.isCSS3DObject = true;
  17. this.element = element;
  18. this.element.style.position = 'absolute';
  19. this.element.style.pointerEvents = 'auto';
  20. this.element.style.userSelect = 'none';
  21. this.element.setAttribute( 'draggable', false );
  22. this.addEventListener( 'removed', function () {
  23. this.traverse( function ( object ) {
  24. if ( object.element instanceof Element && object.element.parentNode !== null ) {
  25. object.element.parentNode.removeChild( object.element );
  26. }
  27. } );
  28. } );
  29. }
  30. copy( source, recursive ) {
  31. super.copy( source, recursive );
  32. this.element = source.element.cloneNode( true );
  33. return this;
  34. }
  35. }
  36. class CSS3DSprite extends CSS3DObject {
  37. constructor( element ) {
  38. super( element );
  39. this.isCSS3DSprite = true;
  40. this.rotation2D = 0;
  41. }
  42. copy( source, recursive ) {
  43. super.copy( source, recursive );
  44. this.rotation2D = source.rotation2D;
  45. return this;
  46. }
  47. }
  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: { 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 viewElement = document.createElement( 'div' );
  64. viewElement.style.transformOrigin = '0 0';
  65. viewElement.style.pointerEvents = 'none';
  66. domElement.appendChild( viewElement );
  67. const cameraElement = document.createElement( 'div' );
  68. cameraElement.style.transformStyle = 'preserve-3d';
  69. viewElement.appendChild( cameraElement );
  70. this.getSize = function () {
  71. return {
  72. width: _width,
  73. height: _height
  74. };
  75. };
  76. this.render = function ( scene, camera ) {
  77. const fov = camera.projectionMatrix.elements[ 5 ] * _heightHalf;
  78. if ( camera.view && camera.view.enabled ) {
  79. // view offset
  80. viewElement.style.transform = `translate( ${ - camera.view.offsetX * ( _width / camera.view.width ) }px, ${ - camera.view.offsetY * ( _height / camera.view.height ) }px )`;
  81. // view fullWidth and fullHeight, view width and height
  82. viewElement.style.transform += `scale( ${ camera.view.fullWidth / camera.view.width }, ${ camera.view.fullHeight / camera.view.height } )`;
  83. } else {
  84. viewElement.style.transform = '';
  85. }
  86. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  87. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  88. let tx, ty;
  89. if ( camera.isOrthographicCamera ) {
  90. tx = - ( camera.right + camera.left ) / 2;
  91. ty = ( camera.top + camera.bottom ) / 2;
  92. }
  93. const scaleByViewOffset = camera.view && camera.view.enabled ? camera.view.height / camera.view.fullHeight : 1;
  94. const cameraCSSMatrix = camera.isOrthographicCamera ?
  95. `scale( ${ scaleByViewOffset } )` + 'scale(' + fov + ')' + 'translate(' + epsilon( tx ) + 'px,' + epsilon( ty ) + 'px)' + getCameraCSSMatrix( camera.matrixWorldInverse ) :
  96. `scale( ${ scaleByViewOffset } )` + 'translateZ(' + fov + 'px)' + getCameraCSSMatrix( camera.matrixWorldInverse );
  97. const perspective = camera.isPerspectiveCamera ? 'perspective(' + fov + 'px) ' : '';
  98. const style = perspective + cameraCSSMatrix +
  99. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)';
  100. if ( cache.camera.style !== style ) {
  101. cameraElement.style.transform = style;
  102. cache.camera.style = style;
  103. }
  104. renderObject( scene, scene, camera, cameraCSSMatrix );
  105. };
  106. this.setSize = function ( width, height ) {
  107. _width = width;
  108. _height = height;
  109. _widthHalf = _width / 2;
  110. _heightHalf = _height / 2;
  111. domElement.style.width = width + 'px';
  112. domElement.style.height = height + 'px';
  113. viewElement.style.width = width + 'px';
  114. viewElement.style.height = height + 'px';
  115. cameraElement.style.width = width + 'px';
  116. cameraElement.style.height = height + 'px';
  117. };
  118. function epsilon( value ) {
  119. return Math.abs( value ) < 1e-10 ? 0 : value;
  120. }
  121. function getCameraCSSMatrix( matrix ) {
  122. const elements = matrix.elements;
  123. return 'matrix3d(' +
  124. epsilon( elements[ 0 ] ) + ',' +
  125. epsilon( - elements[ 1 ] ) + ',' +
  126. epsilon( elements[ 2 ] ) + ',' +
  127. epsilon( elements[ 3 ] ) + ',' +
  128. epsilon( elements[ 4 ] ) + ',' +
  129. epsilon( - elements[ 5 ] ) + ',' +
  130. epsilon( elements[ 6 ] ) + ',' +
  131. epsilon( elements[ 7 ] ) + ',' +
  132. epsilon( elements[ 8 ] ) + ',' +
  133. epsilon( - elements[ 9 ] ) + ',' +
  134. epsilon( elements[ 10 ] ) + ',' +
  135. epsilon( elements[ 11 ] ) + ',' +
  136. epsilon( elements[ 12 ] ) + ',' +
  137. epsilon( - elements[ 13 ] ) + ',' +
  138. epsilon( elements[ 14 ] ) + ',' +
  139. epsilon( elements[ 15 ] ) +
  140. ')';
  141. }
  142. function getObjectCSSMatrix( matrix ) {
  143. const elements = matrix.elements;
  144. const matrix3d = 'matrix3d(' +
  145. epsilon( elements[ 0 ] ) + ',' +
  146. epsilon( elements[ 1 ] ) + ',' +
  147. epsilon( elements[ 2 ] ) + ',' +
  148. epsilon( elements[ 3 ] ) + ',' +
  149. epsilon( - elements[ 4 ] ) + ',' +
  150. epsilon( - elements[ 5 ] ) + ',' +
  151. epsilon( - elements[ 6 ] ) + ',' +
  152. epsilon( - elements[ 7 ] ) + ',' +
  153. epsilon( elements[ 8 ] ) + ',' +
  154. epsilon( elements[ 9 ] ) + ',' +
  155. epsilon( elements[ 10 ] ) + ',' +
  156. epsilon( elements[ 11 ] ) + ',' +
  157. epsilon( elements[ 12 ] ) + ',' +
  158. epsilon( elements[ 13 ] ) + ',' +
  159. epsilon( elements[ 14 ] ) + ',' +
  160. epsilon( elements[ 15 ] ) +
  161. ')';
  162. return 'translate(-50%,-50%)' + matrix3d;
  163. }
  164. function hideObject( object ) {
  165. if ( object.isCSS3DObject ) object.element.style.display = 'none';
  166. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  167. hideObject( object.children[ i ] );
  168. }
  169. }
  170. function renderObject( object, scene, camera, cameraCSSMatrix ) {
  171. if ( object.visible === false ) {
  172. hideObject( object );
  173. return;
  174. }
  175. if ( object.isCSS3DObject ) {
  176. const visible = ( object.layers.test( camera.layers ) === true );
  177. const element = object.element;
  178. element.style.display = visible === true ? '' : 'none';
  179. if ( visible === true ) {
  180. object.onBeforeRender( _this, scene, camera );
  181. let style;
  182. if ( object.isCSS3DSprite ) {
  183. // http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/
  184. _matrix.copy( camera.matrixWorldInverse );
  185. _matrix.transpose();
  186. if ( object.rotation2D !== 0 ) _matrix.multiply( _matrix2.makeRotationZ( object.rotation2D ) );
  187. object.matrixWorld.decompose( _position, _quaternion, _scale );
  188. _matrix.setPosition( _position );
  189. _matrix.scale( _scale );
  190. _matrix.elements[ 3 ] = 0;
  191. _matrix.elements[ 7 ] = 0;
  192. _matrix.elements[ 11 ] = 0;
  193. _matrix.elements[ 15 ] = 1;
  194. style = getObjectCSSMatrix( _matrix );
  195. } else {
  196. style = getObjectCSSMatrix( object.matrixWorld );
  197. }
  198. const cachedObject = cache.objects.get( object );
  199. if ( cachedObject === undefined || cachedObject.style !== style ) {
  200. element.style.transform = style;
  201. const objectData = { style: style };
  202. cache.objects.set( object, objectData );
  203. }
  204. if ( element.parentNode !== cameraElement ) {
  205. cameraElement.appendChild( element );
  206. }
  207. object.onAfterRender( _this, scene, camera );
  208. }
  209. }
  210. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  211. renderObject( object.children[ i ], scene, camera, cameraCSSMatrix );
  212. }
  213. }
  214. }
  215. }
  216. export { CSS3DObject, CSS3DSprite, CSS3DRenderer };