CSS3DRenderer.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /**
  2. * Based on http://www.emagix.net/academic/mscs-project/item/camera-sync-with-css3-and-webgl-threejs
  3. * @author mrdoob / http://mrdoob.com/
  4. * @author yomotsu / https://yomotsu.net/
  5. */
  6. import {
  7. Matrix4,
  8. Object3D,
  9. Vector3
  10. } from "../../../build/three.module.js";
  11. var CSS3DObject = function ( element ) {
  12. Object3D.call( this );
  13. this.element = element;
  14. this.element.style.position = 'absolute';
  15. this.element.style.pointerEvents = 'auto';
  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. CSS3DObject.prototype = Object.create( Object3D.prototype );
  25. CSS3DObject.prototype.constructor = CSS3DObject;
  26. var CSS3DSprite = function ( element ) {
  27. CSS3DObject.call( this, element );
  28. };
  29. CSS3DSprite.prototype = Object.create( CSS3DObject.prototype );
  30. CSS3DSprite.prototype.constructor = CSS3DSprite;
  31. //
  32. var CSS3DRenderer = function () {
  33. var _this = this
  34. var _width, _height;
  35. var _widthHalf, _heightHalf;
  36. var matrix = new Matrix4();
  37. var cache = {
  38. camera: { fov: 0, style: '' },
  39. objects: new WeakMap()
  40. };
  41. var domElement = document.createElement( 'div' );
  42. domElement.style.overflow = 'hidden';
  43. this.domElement = domElement;
  44. var cameraElement = document.createElement( 'div' );
  45. cameraElement.style.WebkitTransformStyle = 'preserve-3d';
  46. cameraElement.style.transformStyle = 'preserve-3d';
  47. cameraElement.style.pointerEvents = 'none';
  48. domElement.appendChild( cameraElement );
  49. var isIE = /Trident/i.test( navigator.userAgent );
  50. this.getSize = function () {
  51. return {
  52. width: _width,
  53. height: _height
  54. };
  55. };
  56. this.setSize = function ( width, height ) {
  57. _width = width;
  58. _height = height;
  59. _widthHalf = _width / 2;
  60. _heightHalf = _height / 2;
  61. domElement.style.width = width + 'px';
  62. domElement.style.height = height + 'px';
  63. cameraElement.style.width = width + 'px';
  64. cameraElement.style.height = height + 'px';
  65. };
  66. function epsilon( value ) {
  67. return Math.abs( value ) < 1e-10 ? 0 : value;
  68. }
  69. function getCameraCSSMatrix( matrix ) {
  70. var elements = matrix.elements;
  71. return 'matrix3d(' +
  72. epsilon( elements[ 0 ] ) + ',' +
  73. epsilon( - elements[ 1 ] ) + ',' +
  74. epsilon( elements[ 2 ] ) + ',' +
  75. epsilon( elements[ 3 ] ) + ',' +
  76. epsilon( elements[ 4 ] ) + ',' +
  77. epsilon( - elements[ 5 ] ) + ',' +
  78. epsilon( elements[ 6 ] ) + ',' +
  79. epsilon( elements[ 7 ] ) + ',' +
  80. epsilon( elements[ 8 ] ) + ',' +
  81. epsilon( - elements[ 9 ] ) + ',' +
  82. epsilon( elements[ 10 ] ) + ',' +
  83. epsilon( elements[ 11 ] ) + ',' +
  84. epsilon( elements[ 12 ] ) + ',' +
  85. epsilon( - elements[ 13 ] ) + ',' +
  86. epsilon( elements[ 14 ] ) + ',' +
  87. epsilon( elements[ 15 ] ) +
  88. ')';
  89. }
  90. function getObjectCSSMatrix( matrix, cameraCSSMatrix ) {
  91. var elements = matrix.elements;
  92. var matrix3d = 'matrix3d(' +
  93. epsilon( elements[ 0 ] ) + ',' +
  94. epsilon( elements[ 1 ] ) + ',' +
  95. epsilon( elements[ 2 ] ) + ',' +
  96. epsilon( elements[ 3 ] ) + ',' +
  97. epsilon( - elements[ 4 ] ) + ',' +
  98. epsilon( - elements[ 5 ] ) + ',' +
  99. epsilon( - elements[ 6 ] ) + ',' +
  100. epsilon( - elements[ 7 ] ) + ',' +
  101. epsilon( elements[ 8 ] ) + ',' +
  102. epsilon( elements[ 9 ] ) + ',' +
  103. epsilon( elements[ 10 ] ) + ',' +
  104. epsilon( elements[ 11 ] ) + ',' +
  105. epsilon( elements[ 12 ] ) + ',' +
  106. epsilon( elements[ 13 ] ) + ',' +
  107. epsilon( elements[ 14 ] ) + ',' +
  108. epsilon( elements[ 15 ] ) +
  109. ')';
  110. if ( isIE ) {
  111. return 'translate(-50%,-50%)' +
  112. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)' +
  113. cameraCSSMatrix +
  114. matrix3d;
  115. }
  116. return 'translate(-50%,-50%)' + matrix3d;
  117. }
  118. function renderObject( object, scene, camera, cameraCSSMatrix ) {
  119. if ( object instanceof CSS3DObject ) {
  120. object.onBeforeRender( _this, scene, camera );
  121. var style;
  122. if ( object instanceof CSS3DSprite ) {
  123. // http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/
  124. matrix.copy( camera.matrixWorldInverse );
  125. matrix.transpose();
  126. matrix.copyPosition( object.matrixWorld );
  127. matrix.scale( object.scale );
  128. matrix.elements[ 3 ] = 0;
  129. matrix.elements[ 7 ] = 0;
  130. matrix.elements[ 11 ] = 0;
  131. matrix.elements[ 15 ] = 1;
  132. style = getObjectCSSMatrix( matrix, cameraCSSMatrix );
  133. } else {
  134. style = getObjectCSSMatrix( object.matrixWorld, cameraCSSMatrix );
  135. }
  136. var element = object.element;
  137. var cachedObject = cache.objects.get( object );
  138. if ( cachedObject === undefined || cachedObject.style !== style ) {
  139. element.style.WebkitTransform = style;
  140. element.style.transform = style;
  141. var objectData = { style: style };
  142. if ( isIE ) {
  143. objectData.distanceToCameraSquared = getDistanceToSquared( camera, object );
  144. }
  145. cache.objects.set( object, objectData );
  146. }
  147. if ( element.parentNode !== cameraElement ) {
  148. cameraElement.appendChild( element );
  149. }
  150. object.onAfterRender( _this, scene, camera );
  151. }
  152. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  153. renderObject( object.children[ i ], scene, camera, cameraCSSMatrix );
  154. }
  155. }
  156. var getDistanceToSquared = function () {
  157. var a = new Vector3();
  158. var b = new Vector3();
  159. return function ( object1, object2 ) {
  160. a.setFromMatrixPosition( object1.matrixWorld );
  161. b.setFromMatrixPosition( object2.matrixWorld );
  162. return a.distanceToSquared( b );
  163. };
  164. }();
  165. function filterAndFlatten( scene ) {
  166. var result = [];
  167. scene.traverse( function ( object ) {
  168. if ( object instanceof CSS3DObject ) result.push( object );
  169. } );
  170. return result;
  171. }
  172. function zOrder( scene ) {
  173. var sorted = filterAndFlatten( scene ).sort( function ( a, b ) {
  174. var distanceA = cache.objects.get( a ).distanceToCameraSquared;
  175. var distanceB = cache.objects.get( b ).distanceToCameraSquared;
  176. return distanceA - distanceB;
  177. } );
  178. var zMax = sorted.length;
  179. for ( var i = 0, l = sorted.length; i < l; i ++ ) {
  180. sorted[ i ].element.style.zIndex = zMax - i;
  181. }
  182. }
  183. this.render = function ( scene, camera ) {
  184. var fov = camera.projectionMatrix.elements[ 5 ] * _heightHalf;
  185. if ( cache.camera.fov !== fov ) {
  186. if ( camera.isPerspectiveCamera ) {
  187. domElement.style.WebkitPerspective = fov + 'px';
  188. domElement.style.perspective = fov + 'px';
  189. } else {
  190. domElement.style.WebkitPerspective = '';
  191. domElement.style.perspective = '';
  192. }
  193. cache.camera.fov = fov;
  194. }
  195. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  196. if ( camera.parent === null ) camera.updateMatrixWorld();
  197. if ( camera.isOrthographicCamera ) {
  198. var tx = - ( camera.right + camera.left ) / 2;
  199. var ty = ( camera.top + camera.bottom ) / 2;
  200. }
  201. var cameraCSSMatrix = camera.isOrthographicCamera ?
  202. 'scale(' + fov + ')' + 'translate(' + epsilon( tx ) + 'px,' + epsilon( ty ) + 'px)' + getCameraCSSMatrix( camera.matrixWorldInverse ) :
  203. 'translateZ(' + fov + 'px)' + getCameraCSSMatrix( camera.matrixWorldInverse );
  204. var style = cameraCSSMatrix +
  205. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)';
  206. if ( cache.camera.style !== style && ! isIE ) {
  207. cameraElement.style.WebkitTransform = style;
  208. cameraElement.style.transform = style;
  209. cache.camera.style = style;
  210. }
  211. renderObject( scene, scene, camera, cameraCSSMatrix );
  212. if ( isIE ) {
  213. // IE10 and 11 does not support 'preserve-3d'.
  214. // Thus, z-order in 3D will not work.
  215. // We have to calc z-order manually and set CSS z-index for IE.
  216. // FYI: z-index can't handle object intersection
  217. zOrder( scene );
  218. }
  219. };
  220. };
  221. export { CSS3DObject, CSS3DSprite, CSS3DRenderer };