CSS3DRenderer.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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.CSS3DSprite = function ( element ) {
  17. THREE.CSS3DObject.call( this, element );
  18. };
  19. THREE.CSS3DSprite.prototype = Object.create( THREE.CSS3DObject.prototype );
  20. //
  21. THREE.CSS3DRenderer = function () {
  22. console.log( 'THREE.CSS3DRenderer', THREE.REVISION );
  23. var _width, _height;
  24. var _widthHalf, _heightHalf;
  25. var matrix = new THREE.Matrix4();
  26. var cache = {
  27. camera: { fov: 0, style: '' },
  28. objects: {}
  29. };
  30. var domElement = document.createElement( 'div' );
  31. domElement.style.overflow = 'hidden';
  32. domElement.style.WebkitTransformStyle = 'preserve-3d';
  33. domElement.style.MozTransformStyle = 'preserve-3d';
  34. domElement.style.oTransformStyle = 'preserve-3d';
  35. domElement.style.transformStyle = 'preserve-3d';
  36. this.domElement = domElement;
  37. var cameraElement = document.createElement( 'div' );
  38. cameraElement.style.WebkitTransformStyle = 'preserve-3d';
  39. cameraElement.style.MozTransformStyle = 'preserve-3d';
  40. cameraElement.style.oTransformStyle = 'preserve-3d';
  41. cameraElement.style.transformStyle = 'preserve-3d';
  42. domElement.appendChild( cameraElement );
  43. this.setClearColor = function () {
  44. };
  45. this.setSize = function ( width, height ) {
  46. _width = width;
  47. _height = height;
  48. _widthHalf = _width / 2;
  49. _heightHalf = _height / 2;
  50. domElement.style.width = width + 'px';
  51. domElement.style.height = height + 'px';
  52. cameraElement.style.width = width + 'px';
  53. cameraElement.style.height = height + 'px';
  54. };
  55. var epsilon = function ( value ) {
  56. return Math.abs( value ) < 0.000001 ? 0 : value;
  57. };
  58. var getCameraCSSMatrix = function ( matrix ) {
  59. var elements = matrix.elements;
  60. return 'matrix3d(' +
  61. epsilon( elements[ 0 ] ) + ',' +
  62. epsilon( - elements[ 1 ] ) + ',' +
  63. epsilon( elements[ 2 ] ) + ',' +
  64. epsilon( elements[ 3 ] ) + ',' +
  65. epsilon( elements[ 4 ] ) + ',' +
  66. epsilon( - elements[ 5 ] ) + ',' +
  67. epsilon( elements[ 6 ] ) + ',' +
  68. epsilon( elements[ 7 ] ) + ',' +
  69. epsilon( elements[ 8 ] ) + ',' +
  70. epsilon( - elements[ 9 ] ) + ',' +
  71. epsilon( elements[ 10 ] ) + ',' +
  72. epsilon( elements[ 11 ] ) + ',' +
  73. epsilon( elements[ 12 ] ) + ',' +
  74. epsilon( - elements[ 13 ] ) + ',' +
  75. epsilon( elements[ 14 ] ) + ',' +
  76. epsilon( elements[ 15 ] ) +
  77. ')';
  78. };
  79. var getObjectCSSMatrix = function ( matrix ) {
  80. var elements = matrix.elements;
  81. return 'translate3d(-50%,-50%,0) matrix3d(' +
  82. epsilon( elements[ 0 ] ) + ',' +
  83. epsilon( elements[ 1 ] ) + ',' +
  84. epsilon( elements[ 2 ] ) + ',' +
  85. epsilon( elements[ 3 ] ) + ',' +
  86. epsilon( - elements[ 4 ] ) + ',' +
  87. epsilon( - elements[ 5 ] ) + ',' +
  88. epsilon( - elements[ 6 ] ) + ',' +
  89. epsilon( - elements[ 7 ] ) + ',' +
  90. epsilon( elements[ 8 ] ) + ',' +
  91. epsilon( elements[ 9 ] ) + ',' +
  92. epsilon( elements[ 10 ] ) + ',' +
  93. epsilon( elements[ 11 ] ) + ',' +
  94. epsilon( elements[ 12 ] ) + ',' +
  95. epsilon( elements[ 13 ] ) + ',' +
  96. epsilon( elements[ 14 ] ) + ',' +
  97. epsilon( elements[ 15 ] ) +
  98. ')';
  99. };
  100. var renderObject = function ( object, camera ) {
  101. if ( object instanceof THREE.CSS3DObject ) {
  102. var style;
  103. if ( object instanceof THREE.CSS3DSprite ) {
  104. // http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/
  105. matrix.copy( camera.matrixWorldInverse );
  106. matrix.transpose();
  107. matrix.copyPosition( object.matrixWorld );
  108. matrix.scale( object.scale );
  109. matrix.elements[ 3 ] = 0;
  110. matrix.elements[ 7 ] = 0;
  111. matrix.elements[ 11 ] = 0;
  112. matrix.elements[ 15 ] = 1;
  113. style = getObjectCSSMatrix( matrix );
  114. } else {
  115. style = getObjectCSSMatrix( object.matrixWorld );
  116. }
  117. var element = object.element;
  118. var cachedStyle = cache.objects[ object.id ];
  119. if ( cachedStyle === undefined || cachedStyle !== style ) {
  120. element.style.WebkitTransform = style;
  121. element.style.MozTransform = style;
  122. element.style.oTransform = style;
  123. element.style.transform = style;
  124. cache.objects[ object.id ] = style;
  125. }
  126. if ( element.parentNode !== cameraElement ) {
  127. cameraElement.appendChild( element );
  128. }
  129. }
  130. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  131. renderObject( object.children[ i ], camera );
  132. }
  133. };
  134. this.render = function ( scene, camera ) {
  135. var fov = 0.5 / Math.tan( THREE.Math.degToRad( camera.fov * 0.5 ) ) * _height;
  136. if ( cache.camera.fov !== fov ) {
  137. domElement.style.WebkitPerspective = fov + "px";
  138. domElement.style.MozPerspective = fov + "px";
  139. domElement.style.oPerspective = fov + "px";
  140. domElement.style.perspective = fov + "px";
  141. cache.camera.fov = fov;
  142. }
  143. scene.updateMatrixWorld();
  144. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  145. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  146. var style = "translate3d(0,0," + fov + "px)" + getCameraCSSMatrix( camera.matrixWorldInverse ) +
  147. " translate3d(" + _widthHalf + "px," + _heightHalf + "px, 0)";
  148. if ( cache.camera.style !== style ) {
  149. cameraElement.style.WebkitTransform = style;
  150. cameraElement.style.MozTransform = style;
  151. cameraElement.style.oTransform = style;
  152. cameraElement.style.transform = style;
  153. cache.camera.style = style;
  154. }
  155. renderObject( scene, camera );
  156. };
  157. };