CSS3DRenderer.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. // Should we replace to feature detection?
  41. // https://github.com/Modernizr/Modernizr/blob/master/feature-detects/css/transformstylepreserve3d.js
  42. // So far, we use `document.documentMode` to detect IE
  43. var isFlatTransform = !! document.documentMode;
  44. this.setClearColor = function () {};
  45. this.getSize = function () {
  46. return {
  47. width: _width,
  48. height: _height
  49. };
  50. };
  51. this.setSize = function ( width, height ) {
  52. _width = width;
  53. _height = height;
  54. _widthHalf = _width / 2;
  55. _heightHalf = _height / 2;
  56. domElement.style.width = width + 'px';
  57. domElement.style.height = height + 'px';
  58. cameraElement.style.width = width + 'px';
  59. cameraElement.style.height = height + 'px';
  60. };
  61. function epsilon( value ) {
  62. return Math.abs( value ) < 1e-10 ? 0 : value;
  63. }
  64. function getCameraCSSMatrix( matrix ) {
  65. var elements = matrix.elements;
  66. return 'matrix3d(' +
  67. epsilon( elements[ 0 ] ) + ',' +
  68. epsilon( - elements[ 1 ] ) + ',' +
  69. epsilon( elements[ 2 ] ) + ',' +
  70. epsilon( elements[ 3 ] ) + ',' +
  71. epsilon( elements[ 4 ] ) + ',' +
  72. epsilon( - elements[ 5 ] ) + ',' +
  73. epsilon( elements[ 6 ] ) + ',' +
  74. epsilon( elements[ 7 ] ) + ',' +
  75. epsilon( elements[ 8 ] ) + ',' +
  76. epsilon( - elements[ 9 ] ) + ',' +
  77. epsilon( elements[ 10 ] ) + ',' +
  78. epsilon( elements[ 11 ] ) + ',' +
  79. epsilon( elements[ 12 ] ) + ',' +
  80. epsilon( - elements[ 13 ] ) + ',' +
  81. epsilon( elements[ 14 ] ) + ',' +
  82. epsilon( elements[ 15 ] ) +
  83. ')';
  84. }
  85. function getObjectCSSMatrix( matrix, cameraCSSMatrix ) {
  86. var elements = matrix.elements;
  87. var matrix3d = 'matrix3d(' +
  88. epsilon( elements[ 0 ] ) + ',' +
  89. epsilon( elements[ 1 ] ) + ',' +
  90. epsilon( elements[ 2 ] ) + ',' +
  91. epsilon( elements[ 3 ] ) + ',' +
  92. epsilon( - elements[ 4 ] ) + ',' +
  93. epsilon( - elements[ 5 ] ) + ',' +
  94. epsilon( - elements[ 6 ] ) + ',' +
  95. epsilon( - elements[ 7 ] ) + ',' +
  96. epsilon( elements[ 8 ] ) + ',' +
  97. epsilon( elements[ 9 ] ) + ',' +
  98. epsilon( elements[ 10 ] ) + ',' +
  99. epsilon( elements[ 11 ] ) + ',' +
  100. epsilon( elements[ 12 ] ) + ',' +
  101. epsilon( elements[ 13 ] ) + ',' +
  102. epsilon( elements[ 14 ] ) + ',' +
  103. epsilon( elements[ 15 ] ) +
  104. ')';
  105. if ( ! isFlatTransform ) {
  106. return 'translate(-50%,-50%)' + matrix3d;
  107. }
  108. return 'translate(-50%,-50%)' +
  109. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)' +
  110. cameraCSSMatrix +
  111. matrix3d;
  112. }
  113. function renderObject( object, camera, cameraCSSMatrix ) {
  114. if ( object instanceof THREE.CSS3DObject ) {
  115. var style;
  116. if ( object instanceof THREE.CSS3DSprite ) {
  117. // http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/
  118. matrix.copy( camera.matrixWorldInverse );
  119. matrix.transpose();
  120. matrix.copyPosition( object.matrixWorld );
  121. matrix.scale( object.scale );
  122. matrix.elements[ 3 ] = 0;
  123. matrix.elements[ 7 ] = 0;
  124. matrix.elements[ 11 ] = 0;
  125. matrix.elements[ 15 ] = 1;
  126. style = getObjectCSSMatrix( matrix, cameraCSSMatrix );
  127. } else {
  128. style = getObjectCSSMatrix( object.matrixWorld, cameraCSSMatrix );
  129. }
  130. var element = object.element;
  131. var cachedStyle = cache.objects[ object.id ] && cache.objects[ object.id ].style;
  132. if ( cachedStyle === undefined || cachedStyle !== style ) {
  133. element.style.WebkitTransform = style;
  134. element.style.MozTransform = style;
  135. element.style.transform = style;
  136. cache.objects[ object.id ] = { style: style };
  137. if ( isFlatTransform ) {
  138. cache.objects[ object.id ].distanceToCameraSquared = getDistanceToSquared( camera, object );
  139. }
  140. }
  141. if ( element.parentNode !== cameraElement ) {
  142. cameraElement.appendChild( element );
  143. }
  144. }
  145. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  146. renderObject( object.children[ i ], camera, cameraCSSMatrix );
  147. }
  148. }
  149. var getDistanceToSquared = function () {
  150. var a = new THREE.Vector3();
  151. var b = new THREE.Vector3();
  152. return function ( object1, object2 ) {
  153. a.x = object1.matrixWorld.elements[ 12 ];
  154. a.y = object1.matrixWorld.elements[ 13 ];
  155. a.z = object1.matrixWorld.elements[ 14 ];
  156. b.x = object2.matrixWorld.elements[ 12 ];
  157. b.y = object2.matrixWorld.elements[ 13 ];
  158. b.z = object2.matrixWorld.elements[ 14 ];
  159. return a.distanceToSquared( b );
  160. };
  161. }();
  162. function zOrder( scene ) {
  163. var order = Object.keys( cache.objects ).sort( function ( a, b ) {
  164. return cache.objects[ a ].distanceToCameraSquared - cache.objects[ b ].distanceToCameraSquared;
  165. } );
  166. var zMax = order.length;
  167. scene.traverse( function ( object ) {
  168. var index = order.indexOf( object.id + '' );
  169. if ( index !== - 1 ) {
  170. object.element.style.zIndex = zMax - index;
  171. }
  172. } );
  173. }
  174. this.render = function ( scene, camera ) {
  175. var fov = 0.5 / Math.tan( THREE.Math.degToRad( camera.getEffectiveFOV() * 0.5 ) ) * _height;
  176. if ( cache.camera.fov !== fov ) {
  177. domElement.style.WebkitPerspective = fov + 'px';
  178. domElement.style.MozPerspective = fov + 'px';
  179. domElement.style.perspective = fov + 'px';
  180. cache.camera.fov = fov;
  181. }
  182. scene.updateMatrixWorld();
  183. if ( camera.parent === null ) camera.updateMatrixWorld();
  184. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  185. var cameraCSSMatrix = 'translateZ(' + fov + 'px)' +
  186. getCameraCSSMatrix( camera.matrixWorldInverse );
  187. var style = cameraCSSMatrix +
  188. 'translate(' + _widthHalf + 'px,' + _heightHalf + 'px)';
  189. if ( ! isFlatTransform && cache.camera.style !== style ) {
  190. cameraElement.style.WebkitTransform = style;
  191. cameraElement.style.MozTransform = style;
  192. cameraElement.style.transform = style;
  193. cache.camera.style = style;
  194. }
  195. renderObject( scene, camera, cameraCSSMatrix );
  196. if ( isFlatTransform ) {
  197. // IE10 and 11 does not support 'preserve-3d'.
  198. // Thus, z-order in 3D will not work.
  199. // We have to calc z-order manually and set CSS z-index for IE.
  200. // FYI: z-index can't handle object intersection
  201. zOrder( scene );
  202. }
  203. };
  204. };