CSS3DRenderer.min.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 ){THREE.Object3D.call( this );this.element = element;this.element.style.position = 'absolute';this.addEventListener( 'removed',function ( event ){if ( this.element.parentNode !== null ){this.element.parentNode.removeChild( this.element );for ( var i = 0,l = this.children.length;i < l;i ++ ){this.children[ i ].dispatchEvent( event )}
  6. }})}
  7. ;THREE.CSS3DObject.prototype = Object.create( THREE.Object3D.prototype );THREE.CSS3DSprite = function ( element ){THREE.CSS3DObject.call( this,element )}
  8. ;THREE.CSS3DSprite.prototype = Object.create( THREE.CSS3DObject.prototype );//
  9. THREE.CSS3DRenderer = function (){console.log( 'THREE.CSS3DRenderer',THREE.REVISION );var _width,_height;var _widthHalf,_heightHalf;var matrix = new THREE.Matrix4();var domElement = document.createElement( 'div' );domElement.style.overflow = 'hidden';domElement.style.WebkitTransformStyle = 'preserve-3d';domElement.style.MozTransformStyle = 'preserve-3d';domElement.style.oTransformStyle = 'preserve-3d';domElement.style.transformStyle = 'preserve-3d';this.domElement = domElement;var cameraElement = document.createElement( 'div' );cameraElement.style.WebkitTransformStyle = 'preserve-3d';cameraElement.style.MozTransformStyle = 'preserve-3d';cameraElement.style.oTransformStyle = 'preserve-3d';cameraElement.style.transformStyle = 'preserve-3d';domElement.appendChild( cameraElement );this.setClearColor = function (){}
  10. ;this.setSize = function ( width,height ){_width = width;_height = height;_widthHalf = _width / 2;_heightHalf = _height / 2;domElement.style.width = width + 'px';domElement.style.height = height + 'px';cameraElement.style.width = width + 'px';cameraElement.style.height = height + 'px'}
  11. ;var epsilon = function ( value ){return Math.abs( value ) < 0.000001 ? 0:value}
  12. ;var getCameraCSSMatrix = function ( matrix ){var elements = matrix.elements;return 'matrix3d(' +
  13. epsilon( elements[ 0 ] ) + ',' +
  14. epsilon( - elements[ 1 ] ) + ',' +
  15. epsilon( elements[ 2 ] ) + ',' +
  16. epsilon( elements[ 3 ] ) + ',' +
  17. epsilon( elements[ 4 ] ) + ',' +
  18. epsilon( - elements[ 5 ] ) + ',' +
  19. epsilon( elements[ 6 ] ) + ',' +
  20. epsilon( elements[ 7 ] ) + ',' +
  21. epsilon( elements[ 8 ] ) + ',' +
  22. epsilon( - elements[ 9 ] ) + ',' +
  23. epsilon( elements[ 10 ] ) + ',' +
  24. epsilon( elements[ 11 ] ) + ',' +
  25. epsilon( elements[ 12 ] ) + ',' +
  26. epsilon( - elements[ 13 ] ) + ',' +
  27. epsilon( elements[ 14 ] ) + ',' +
  28. epsilon( elements[ 15 ] ) +
  29. ')'}
  30. ;var getObjectCSSMatrix = function ( matrix ){var elements = matrix.elements;return 'translate3d(-50%,-50%,0) matrix3d(' +
  31. epsilon( elements[ 0 ] ) + ',' +
  32. epsilon( elements[ 1 ] ) + ',' +
  33. epsilon( elements[ 2 ] ) + ',' +
  34. epsilon( elements[ 3 ] ) + ',' +
  35. epsilon( - elements[ 4 ] ) + ',' +
  36. epsilon( - elements[ 5 ] ) + ',' +
  37. epsilon( - elements[ 6 ] ) + ',' +
  38. epsilon( - elements[ 7 ] ) + ',' +
  39. epsilon( elements[ 8 ] ) + ',' +
  40. epsilon( elements[ 9 ] ) + ',' +
  41. epsilon( elements[ 10 ] ) + ',' +
  42. epsilon( elements[ 11 ] ) + ',' +
  43. epsilon( elements[ 12 ] ) + ',' +
  44. epsilon( elements[ 13 ] ) + ',' +
  45. epsilon( elements[ 14 ] ) + ',' +
  46. epsilon( elements[ 15 ] ) +
  47. ')'}
  48. ;var renderObject = function ( object,camera ){if ( object instanceof THREE.CSS3DObject ){var style;if ( object instanceof THREE.CSS3DSprite ){// http://swiftcoder.wordpress.com/2008/11/25/constructing-a-billboard-matrix/
  49. matrix.copy( camera.matrixWorldInverse );matrix.transpose();matrix.copyPosition( object.matrixWorld );matrix.scale( object.scale );matrix.elements[ 3 ] = 0;matrix.elements[ 7 ] = 0;matrix.elements[ 11 ] = 0;matrix.elements[ 15 ] = 1;style = getObjectCSSMatrix( matrix )}
  50. else{style = getObjectCSSMatrix( object.matrixWorld )}
  51. var element = object.element;element.style.WebkitTransform = style;element.style.MozTransform = style;element.style.oTransform = style;element.style.transform = style;if ( element.parentNode !== cameraElement ){cameraElement.appendChild( element )}
  52. }for ( var i = 0,l = object.children.length;i < l;i ++ ){renderObject( object.children[ i ],camera )}
  53. };this.render = function ( scene,camera ){var fov = 0.5 / Math.tan( THREE.Math.degToRad( camera.fov * 0.5 ) ) * _height;domElement.style.WebkitPerspective = fov + "px";domElement.style.MozPerspective = fov + "px";domElement.style.oPerspective = fov + "px";domElement.style.perspective = fov + "px";scene.updateMatrixWorld();if ( camera.parent === undefined ) camera.updateMatrixWorld();camera.matrixWorldInverse.getInverse( camera.matrixWorld );var style = "translate3d(0,0," + fov + "px)" + getCameraCSSMatrix( camera.matrixWorldInverse ) +
  54. " translate3d(" + _widthHalf + "px," + _heightHalf + "px,0)";cameraElement.style.WebkitTransform = style;cameraElement.style.MozTransform = style;cameraElement.style.oTransform = style;cameraElement.style.transform = style;renderObject( scene,camera )}
  55. };