CSS3DRenderer.js 6.2 KB

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