CSS2DRenderer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. }
  12. } );
  13. };
  14. THREE.CSS2DObject.prototype = Object.create( THREE.Object3D.prototype );
  15. THREE.CSS2DObject.prototype.constructor = THREE.CSS2DObject;
  16. //
  17. THREE.CSS2DRenderer = function () {
  18. console.log( 'THREE.CSS2DRenderer', THREE.REVISION );
  19. var _width, _height;
  20. var _widthHalf, _heightHalf;
  21. var vector = new THREE.Vector3();
  22. var viewMatrix = new THREE.Matrix4();
  23. var viewProjectionMatrix = new THREE.Matrix4();
  24. var cache = {
  25. objects: new WeakMap()
  26. };
  27. var domElement = document.createElement( 'div' );
  28. domElement.style.overflow = 'hidden';
  29. this.domElement = domElement;
  30. this.getSize = function () {
  31. return {
  32. width: _width,
  33. height: _height
  34. };
  35. };
  36. this.setSize = function ( width, height ) {
  37. _width = width;
  38. _height = height;
  39. _widthHalf = _width / 2;
  40. _heightHalf = _height / 2;
  41. domElement.style.width = width + 'px';
  42. domElement.style.height = height + 'px';
  43. };
  44. var renderObject = function ( object, camera ) {
  45. if ( object instanceof THREE.CSS2DObject ) {
  46. vector.setFromMatrixPosition( object.matrixWorld );
  47. vector.applyMatrix4( viewProjectionMatrix );
  48. var element = object.element;
  49. var style = 'translate(-50%,-50%) translate(' + ( vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - vector.y * _heightHalf + _heightHalf ) + 'px)';
  50. element.style.WebkitTransform = style;
  51. element.style.MozTransform = style;
  52. element.style.oTransform = style;
  53. element.style.transform = style;
  54. var objectData = {
  55. distanceToCameraSquared: getDistanceToSquared( camera, object )
  56. };
  57. cache.objects.set( object, objectData );
  58. if ( element.parentNode !== domElement ) {
  59. domElement.appendChild( element );
  60. }
  61. }
  62. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  63. renderObject( object.children[ i ], camera );
  64. }
  65. };
  66. var getDistanceToSquared = function () {
  67. var a = new THREE.Vector3();
  68. var b = new THREE.Vector3();
  69. return function ( object1, object2 ) {
  70. a.setFromMatrixPosition( object1.matrixWorld );
  71. b.setFromMatrixPosition( object2.matrixWorld );
  72. return a.distanceToSquared( b );
  73. };
  74. }();
  75. var filterAndFlatten = function ( scene ) {
  76. var result = [];
  77. scene.traverse( function ( object ) {
  78. if ( object instanceof THREE.CSS2DObject ) result.push( object );
  79. } );
  80. return result;
  81. };
  82. var zOrder = function ( scene ) {
  83. var sorted = filterAndFlatten( scene ).sort( function ( a, b ) {
  84. var distanceA = cache.objects.get( a ).distanceToCameraSquared;
  85. var distanceB = cache.objects.get( b ).distanceToCameraSquared;
  86. return distanceA - distanceB;
  87. } );
  88. var zMax = sorted.length;
  89. for ( var i = 0, l = sorted.length; i < l; i ++ ) {
  90. sorted[ i ].element.style.zIndex = zMax - i;
  91. }
  92. };
  93. this.render = function ( scene, camera ) {
  94. scene.updateMatrixWorld();
  95. if ( camera.parent === null ) camera.updateMatrixWorld();
  96. viewMatrix.copy( camera.matrixWorldInverse );
  97. viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, viewMatrix );
  98. renderObject( scene, camera );
  99. zOrder( scene );
  100. };
  101. };