CSS3DRenderer.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 ( event ) {
  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. domElement.style.WebkitTransformStyle = 'preserve-3d';
  35. domElement.style.MozTransformStyle = 'preserve-3d';
  36. domElement.style.oTransformStyle = 'preserve-3d';
  37. domElement.style.transformStyle = 'preserve-3d';
  38. this.domElement = domElement;
  39. var cameraElement = document.createElement( 'div' );
  40. cameraElement.style.WebkitTransformStyle = 'preserve-3d';
  41. cameraElement.style.MozTransformStyle = 'preserve-3d';
  42. cameraElement.style.oTransformStyle = 'preserve-3d';
  43. cameraElement.style.transformStyle = 'preserve-3d';
  44. domElement.appendChild( cameraElement );
  45. this.setClearColor = function () {};
  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. var epsilon = function ( value ) {
  63. return Math.abs( value ) < Number.EPSILON ? 0 : value;
  64. };
  65. var getCameraCSSMatrix = function ( 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. var getObjectCSSMatrix = function ( matrix ) {
  87. var elements = matrix.elements;
  88. return 'translate3d(-50%,-50%,0) 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. };
  107. var renderObject = function ( object, camera ) {
  108. if ( object instanceof THREE.CSS3DObject ) {
  109. var style;
  110. if ( object instanceof THREE.CSS3DSprite ) {
  111. // http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/
  112. matrix.copy( camera.matrixWorldInverse );
  113. matrix.transpose();
  114. matrix.copyPosition( object.matrixWorld );
  115. matrix.scale( object.scale );
  116. matrix.elements[ 3 ] = 0;
  117. matrix.elements[ 7 ] = 0;
  118. matrix.elements[ 11 ] = 0;
  119. matrix.elements[ 15 ] = 1;
  120. style = getObjectCSSMatrix( matrix );
  121. } else {
  122. style = getObjectCSSMatrix( object.matrixWorld );
  123. }
  124. var element = object.element;
  125. var cachedStyle = cache.objects[ object.id ];
  126. if ( cachedStyle === undefined || cachedStyle !== style ) {
  127. element.style.WebkitTransform = style;
  128. element.style.MozTransform = style;
  129. element.style.oTransform = style;
  130. element.style.transform = style;
  131. cache.objects[ object.id ] = style;
  132. }
  133. if ( element.parentNode !== cameraElement ) {
  134. cameraElement.appendChild( element );
  135. }
  136. }
  137. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  138. renderObject( object.children[ i ], camera );
  139. }
  140. };
  141. this.render = function ( scene, camera ) {
  142. var fov = 0.5 / Math.tan( THREE.Math.degToRad( camera.getEffectiveFOV() * 0.5 ) ) * _height;
  143. if ( cache.camera.fov !== fov ) {
  144. domElement.style.WebkitPerspective = fov + "px";
  145. domElement.style.MozPerspective = fov + "px";
  146. domElement.style.oPerspective = fov + "px";
  147. domElement.style.perspective = fov + "px";
  148. cache.camera.fov = fov;
  149. }
  150. scene.updateMatrixWorld();
  151. if ( camera.parent === null ) camera.updateMatrixWorld();
  152. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  153. var style = "translate3d(0,0," + fov + "px)" + getCameraCSSMatrix( camera.matrixWorldInverse ) +
  154. " translate3d(" + _widthHalf + "px," + _heightHalf + "px, 0)";
  155. if ( cache.camera.style !== style ) {
  156. cameraElement.style.WebkitTransform = style;
  157. cameraElement.style.MozTransform = style;
  158. cameraElement.style.oTransform = style;
  159. cameraElement.style.transform = style;
  160. cache.camera.style = style;
  161. }
  162. renderObject( scene, camera );
  163. };
  164. };