CSS3DRenderer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import {
  2. Matrix4,
  3. Object3D,
  4. Quaternion,
  5. Vector3
  6. } from 'three';
  7. // Based on http://www.emagix.net/academic/mscs-project/item/camera-sync-with-css3-and-webgl-threejs
  8. const _position = new Vector3();
  9. const _quaternion = new Quaternion();
  10. const _scale = new Vector3();
  11. /**
  12. * The base 3D object that is supported by {@link CSS3DRenderer}.
  13. *
  14. * @augments Object3D
  15. */
  16. class CSS3DObject extends Object3D {
  17. /**
  18. * Constructs a new CSS3D object.
  19. *
  20. * @param {DOMElement} [element] - The DOM element.
  21. */
  22. constructor( element = document.createElement( 'div' ) ) {
  23. super();
  24. /**
  25. * This flag can be used for type testing.
  26. *
  27. * @type {boolean}
  28. * @readonly
  29. * @default true
  30. */
  31. this.isCSS3DObject = true;
  32. /**
  33. * The DOM element which defines the appearance of this 3D object.
  34. *
  35. * @type {DOMElement}
  36. * @readonly
  37. * @default true
  38. */
  39. this.element = element;
  40. this.element.style.position = 'absolute';
  41. this.element.style.pointerEvents = 'auto';
  42. this.element.style.userSelect = 'none';
  43. this.element.setAttribute( 'draggable', false );
  44. this.addEventListener( 'removed', function () {
  45. this.traverse( function ( object ) {
  46. if (
  47. object.element instanceof object.element.ownerDocument.defaultView.Element &&
  48. object.element.parentNode !== null
  49. ) {
  50. object.element.remove();
  51. }
  52. } );
  53. } );
  54. }
  55. copy( source, recursive ) {
  56. super.copy( source, recursive );
  57. this.element = source.element.cloneNode( true );
  58. return this;
  59. }
  60. }
  61. /**
  62. * A specialized version of {@link CSS3DObject} that represents
  63. * DOM elements as sprites.
  64. *
  65. * @augments CSS3DObject
  66. */
  67. class CSS3DSprite extends CSS3DObject {
  68. /**
  69. * Constructs a new CSS3D sprite object.
  70. *
  71. * @param {DOMElement} [element] - The DOM element.
  72. */
  73. constructor( element ) {
  74. super( element );
  75. /**
  76. * This flag can be used for type testing.
  77. *
  78. * @type {boolean}
  79. * @readonly
  80. * @default true
  81. */
  82. this.isCSS3DSprite = true;
  83. /**
  84. * The sprite's rotation in radians.
  85. *
  86. * @type {number}
  87. * @default 0
  88. */
  89. this.rotation2D = 0;
  90. }
  91. copy( source, recursive ) {
  92. super.copy( source, recursive );
  93. this.rotation2D = source.rotation2D;
  94. return this;
  95. }
  96. }
  97. //
  98. const _matrix = new Matrix4();
  99. const _matrix2 = new Matrix4();
  100. /**
  101. * This renderer can be used to apply hierarchical 3D transformations to DOM elements
  102. * via the CSS3 [transform]{@link https://www.w3schools.com/cssref/css3_pr_transform.asp} property.
  103. * `CSS3DRenderer` is particularly interesting if you want to apply 3D effects to a website without
  104. * canvas based rendering. It can also be used in order to combine DOM elements with WebGLcontent.
  105. *
  106. * There are, however, some important limitations:
  107. *
  108. * - It's not possible to use the material system of *three.js*.
  109. * - It's also not possible to use geometries.
  110. * - The renderer only supports 100% browser and display zoom.
  111. *
  112. * So `CSS3DRenderer` is just focused on ordinary DOM elements. These elements are wrapped into special
  113. * 3D objects ({@link CSS3DObject} or {@link CSS3DSprite}) and then added to the scene graph.
  114. */
  115. class CSS3DRenderer {
  116. /**
  117. * Constructs a new CSS3D renderer.
  118. *
  119. * @param {CSS3DRenderer~Parameters} [parameters] - The parameters.
  120. */
  121. constructor( parameters = {} ) {
  122. const _this = this;
  123. let _width, _height;
  124. let _widthHalf, _heightHalf;
  125. const cache = {
  126. camera: { style: '' },
  127. objects: new WeakMap()
  128. };
  129. const domElement = parameters.element !== undefined ? parameters.element : document.createElement( 'div' );
  130. domElement.style.overflow = 'hidden';
  131. /**
  132. * The DOM where the renderer appends its child-elements.
  133. *
  134. * @type {DOMElement}
  135. */
  136. this.domElement = domElement;
  137. const viewElement = document.createElement( 'div' );
  138. viewElement.style.transformOrigin = '0 0';
  139. viewElement.style.pointerEvents = 'none';
  140. domElement.appendChild( viewElement );
  141. const cameraElement = document.createElement( 'div' );
  142. cameraElement.style.transformStyle = 'preserve-3d';
  143. viewElement.appendChild( cameraElement );
  144. /**
  145. * Returns an object containing the width and height of the renderer.
  146. *
  147. * @return {{width:number,height:number}} The size of the renderer.
  148. */
  149. this.getSize = function () {
  150. return {
  151. width: _width,
  152. height: _height
  153. };
  154. };
  155. /**
  156. * Renders the given scene using the given camera.
  157. *
  158. * @param {Object3D} scene - A scene or any other type of 3D object.
  159. * @param {Camera} camera - The camera.
  160. */
  161. this.render = function ( scene, camera ) {
  162. const fov = camera.projectionMatrix.elements[ 5 ] * _heightHalf;
  163. if ( camera.view && camera.view.enabled ) {
  164. // view offset
  165. viewElement.style.transform = `translate( ${ - camera.view.offsetX * ( _width / camera.view.width ) }px, ${ - camera.view.offsetY * ( _height / camera.view.height ) }px )`;
  166. // view fullWidth and fullHeight, view width and height
  167. viewElement.style.transform += `scale( ${ camera.view.fullWidth / camera.view.width }, ${ camera.view.fullHeight / camera.view.height } )`;
  168. } else {
  169. viewElement.style.transform = '';
  170. }
  171. if ( scene.matrixWorldAutoUpdate === true ) scene.updateMatrixWorld();
  172. if ( camera.parent === null && camera.matrixWorldAutoUpdate === true ) camera.updateMatrixWorld();
  173. let tx, ty;
  174. if ( camera.isOrthographicCamera ) {
  175. tx = - ( camera.right + camera.left ) / 2;
  176. ty = ( camera.top + camera.bottom ) / 2;
  177. }
  178. const scaleByViewOffset = camera.view && camera.view.enabled ? camera.view.height / camera.view.fullHeight : 1;
  179. const cameraCSSMatrix = camera.isOrthographicCamera ?
  180. `scale( ${ scaleByViewOffset } )` + 'scale(' + fov + ')' + 'translate(' + epsilon( tx ) + 'px,' + epsilon( ty ) + 'px)' + getCameraCSSMatrix( camera.matrixWorldInverse ) :
  181. `scale( ${ scaleByViewOffset } )` + 'translateZ(' + fov + 'px)' + getCameraCSSMatrix( camera.matrixWorldInverse );
  182. const perspective = camera.isPerspectiveCamera ? 'perspective(' + fov + 'px) ' : '';
  183. const style = perspective + cameraCSSMatrix +
  184. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)';
  185. if ( cache.camera.style !== style ) {
  186. cameraElement.style.transform = style;
  187. cache.camera.style = style;
  188. }
  189. renderObject( scene, scene, camera, cameraCSSMatrix );
  190. };
  191. /**
  192. * Resizes the renderer to the given width and height.
  193. *
  194. * @param {number} width - The width of the renderer.
  195. * @param {number} height - The height of the renderer.
  196. */
  197. this.setSize = function ( width, height ) {
  198. _width = width;
  199. _height = height;
  200. _widthHalf = _width / 2;
  201. _heightHalf = _height / 2;
  202. domElement.style.width = width + 'px';
  203. domElement.style.height = height + 'px';
  204. viewElement.style.width = width + 'px';
  205. viewElement.style.height = height + 'px';
  206. cameraElement.style.width = width + 'px';
  207. cameraElement.style.height = height + 'px';
  208. };
  209. function epsilon( value ) {
  210. return Math.abs( value ) < 1e-10 ? 0 : value;
  211. }
  212. function getCameraCSSMatrix( matrix ) {
  213. const elements = matrix.elements;
  214. return 'matrix3d(' +
  215. epsilon( elements[ 0 ] ) + ',' +
  216. epsilon( - elements[ 1 ] ) + ',' +
  217. epsilon( elements[ 2 ] ) + ',' +
  218. epsilon( elements[ 3 ] ) + ',' +
  219. epsilon( elements[ 4 ] ) + ',' +
  220. epsilon( - elements[ 5 ] ) + ',' +
  221. epsilon( elements[ 6 ] ) + ',' +
  222. epsilon( elements[ 7 ] ) + ',' +
  223. epsilon( elements[ 8 ] ) + ',' +
  224. epsilon( - elements[ 9 ] ) + ',' +
  225. epsilon( elements[ 10 ] ) + ',' +
  226. epsilon( elements[ 11 ] ) + ',' +
  227. epsilon( elements[ 12 ] ) + ',' +
  228. epsilon( - elements[ 13 ] ) + ',' +
  229. epsilon( elements[ 14 ] ) + ',' +
  230. epsilon( elements[ 15 ] ) +
  231. ')';
  232. }
  233. function getObjectCSSMatrix( matrix ) {
  234. const elements = matrix.elements;
  235. const matrix3d = 'matrix3d(' +
  236. epsilon( elements[ 0 ] ) + ',' +
  237. epsilon( elements[ 1 ] ) + ',' +
  238. epsilon( elements[ 2 ] ) + ',' +
  239. epsilon( elements[ 3 ] ) + ',' +
  240. epsilon( - elements[ 4 ] ) + ',' +
  241. epsilon( - elements[ 5 ] ) + ',' +
  242. epsilon( - elements[ 6 ] ) + ',' +
  243. epsilon( - elements[ 7 ] ) + ',' +
  244. epsilon( elements[ 8 ] ) + ',' +
  245. epsilon( elements[ 9 ] ) + ',' +
  246. epsilon( elements[ 10 ] ) + ',' +
  247. epsilon( elements[ 11 ] ) + ',' +
  248. epsilon( elements[ 12 ] ) + ',' +
  249. epsilon( elements[ 13 ] ) + ',' +
  250. epsilon( elements[ 14 ] ) + ',' +
  251. epsilon( elements[ 15 ] ) +
  252. ')';
  253. return 'translate(-50%,-50%)' + matrix3d;
  254. }
  255. function hideObject( object ) {
  256. if ( object.isCSS3DObject ) object.element.style.display = 'none';
  257. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  258. hideObject( object.children[ i ] );
  259. }
  260. }
  261. function renderObject( object, scene, camera, cameraCSSMatrix ) {
  262. if ( object.visible === false ) {
  263. hideObject( object );
  264. return;
  265. }
  266. if ( object.isCSS3DObject ) {
  267. const visible = ( object.layers.test( camera.layers ) === true );
  268. const element = object.element;
  269. element.style.display = visible === true ? '' : 'none';
  270. if ( visible === true ) {
  271. object.onBeforeRender( _this, scene, camera );
  272. let style;
  273. if ( object.isCSS3DSprite ) {
  274. // http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/
  275. _matrix.copy( camera.matrixWorldInverse );
  276. _matrix.transpose();
  277. if ( object.rotation2D !== 0 ) _matrix.multiply( _matrix2.makeRotationZ( object.rotation2D ) );
  278. object.matrixWorld.decompose( _position, _quaternion, _scale );
  279. _matrix.setPosition( _position );
  280. _matrix.scale( _scale );
  281. _matrix.elements[ 3 ] = 0;
  282. _matrix.elements[ 7 ] = 0;
  283. _matrix.elements[ 11 ] = 0;
  284. _matrix.elements[ 15 ] = 1;
  285. style = getObjectCSSMatrix( _matrix );
  286. } else {
  287. style = getObjectCSSMatrix( object.matrixWorld );
  288. }
  289. const cachedObject = cache.objects.get( object );
  290. if ( cachedObject === undefined || cachedObject.style !== style ) {
  291. element.style.transform = style;
  292. const objectData = { style: style };
  293. cache.objects.set( object, objectData );
  294. }
  295. if ( element.parentNode !== cameraElement ) {
  296. cameraElement.appendChild( element );
  297. }
  298. object.onAfterRender( _this, scene, camera );
  299. }
  300. }
  301. for ( let i = 0, l = object.children.length; i < l; i ++ ) {
  302. renderObject( object.children[ i ], scene, camera, cameraCSSMatrix );
  303. }
  304. }
  305. }
  306. }
  307. /**
  308. * Constructor parameters of `CSS3DRenderer`.
  309. *
  310. * @typedef {Object} CSS3DRenderer~Parameters
  311. * @property {DOMElement} [element] - A DOM element where the renderer appends its child-elements.
  312. * If not passed in here, a new div element will be created.
  313. **/
  314. export { CSS3DObject, CSS3DSprite, CSS3DRenderer };
粤ICP备19079148号