CSS3DRenderer.js 11 KB

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