CSS3DRenderer.js 6.0 KB

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