CSS3DRenderer.js 6.8 KB

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