CSS3DRenderer.js 6.2 KB

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