2
0

CSS2DRenderer.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. THREE.CSS2DObject = function ( element ) {
  5. THREE.Object3D.call( this );
  6. this.element = element;
  7. this.element.style.position = 'absolute';
  8. this.addEventListener( 'removed', function ( event ) {
  9. if ( this.element.parentNode !== null ) {
  10. this.element.parentNode.removeChild( this.element );
  11. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  12. this.children[ i ].dispatchEvent( event );
  13. }
  14. }
  15. } );
  16. };
  17. THREE.CSS2DObject.prototype = Object.create( THREE.Object3D.prototype );
  18. //
  19. THREE.CSS2DRenderer = function () {
  20. console.log( 'THREE.CSS3DRenderer', THREE.REVISION );
  21. var _width, _height;
  22. var _widthHalf, _heightHalf;
  23. var vector = new THREE.Vector3();
  24. var viewMatrix = new THREE.Matrix4();
  25. var viewProjectionMatrix = new THREE.Matrix4();
  26. var domElement = document.createElement( 'div' );
  27. domElement.style.overflow = 'hidden';
  28. this.domElement = domElement;
  29. this.setSize = function ( width, height ) {
  30. _width = width;
  31. _height = height;
  32. _widthHalf = _width / 2;
  33. _heightHalf = _height / 2;
  34. domElement.style.width = width + 'px';
  35. domElement.style.height = height + 'px';
  36. };
  37. var renderObject = function ( object, camera ) {
  38. if ( object instanceof THREE.CSS2DObject ) {
  39. vector.getPositionFromMatrix( object.matrixWorld );
  40. vector.applyProjection( viewProjectionMatrix );
  41. var element = object.element;
  42. var style = 'translate(-50%,-50%) translate(' + ( vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - vector.y * _heightHalf + _heightHalf ) + 'px)';
  43. element.style.WebkitTransform = style;
  44. element.style.MozTransform = style;
  45. element.style.oTransform = style;
  46. element.style.transform = style;
  47. if ( element.parentNode !== domElement ) {
  48. domElement.appendChild( element );
  49. }
  50. }
  51. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  52. renderObject( object.children[ i ], camera );
  53. }
  54. };
  55. this.render = function ( scene, camera ) {
  56. scene.updateMatrixWorld();
  57. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  58. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  59. viewMatrix.copy( camera.matrixWorldInverse.getInverse( camera.matrixWorld ) );
  60. viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, viewMatrix );
  61. renderObject( scene, camera );
  62. };
  63. };