CSS3DRenderer.js 11 KB

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