CSS3DRenderer.js 7.1 KB

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