CSS3DRenderer.js 7.9 KB

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