CSS2DRenderer.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. */
  4. import {
  5. Matrix4,
  6. Object3D,
  7. Vector3
  8. } from "../../../build/three.module.js";
  9. var CSS2DObject = function ( element ) {
  10. Object3D.call( this );
  11. this.element = element;
  12. this.element.style.position = 'absolute';
  13. this.addEventListener( 'removed', function () {
  14. this.traverse( function ( object ) {
  15. if ( object.element instanceof Element && object.element.parentNode !== null ) {
  16. object.element.parentNode.removeChild( object.element );
  17. }
  18. } );
  19. } );
  20. };
  21. CSS2DObject.prototype = Object.create( Object3D.prototype );
  22. CSS2DObject.prototype.constructor = CSS2DObject;
  23. //
  24. var CSS2DRenderer = function () {
  25. var _this = this;
  26. var _width, _height;
  27. var _widthHalf, _heightHalf;
  28. var vector = new Vector3();
  29. var viewMatrix = new Matrix4();
  30. var viewProjectionMatrix = new Matrix4();
  31. var cache = {
  32. objects: new WeakMap()
  33. };
  34. var domElement = document.createElement( 'div' );
  35. domElement.style.overflow = 'hidden';
  36. this.domElement = domElement;
  37. this.getSize = function () {
  38. return {
  39. width: _width,
  40. height: _height
  41. };
  42. };
  43. this.setSize = function ( width, height ) {
  44. _width = width;
  45. _height = height;
  46. _widthHalf = _width / 2;
  47. _heightHalf = _height / 2;
  48. domElement.style.width = width + 'px';
  49. domElement.style.height = height + 'px';
  50. };
  51. var renderObject = function ( object, scene, camera ) {
  52. if ( object instanceof CSS2DObject ) {
  53. object.onBeforeRender( _this, scene, camera );
  54. vector.setFromMatrixPosition( object.matrixWorld );
  55. vector.applyMatrix4( viewProjectionMatrix );
  56. var element = object.element;
  57. var style = 'translate(-50%,-50%) translate(' + ( vector.x * _widthHalf + _widthHalf ) + 'px,' + ( - vector.y * _heightHalf + _heightHalf ) + 'px)';
  58. element.style.WebkitTransform = style;
  59. element.style.MozTransform = style;
  60. element.style.oTransform = style;
  61. element.style.transform = style;
  62. element.style.display = ( object.visible && vector.z >= - 1 && vector.z <= 1 ) ? '' : 'none';
  63. var objectData = {
  64. distanceToCameraSquared: getDistanceToSquared( camera, object )
  65. };
  66. cache.objects.set( object, objectData );
  67. if ( element.parentNode !== domElement ) {
  68. domElement.appendChild( element );
  69. }
  70. object.onAfterRender( _this, scene, camera );
  71. }
  72. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  73. renderObject( object.children[ i ], scene, camera );
  74. }
  75. };
  76. var getDistanceToSquared = function () {
  77. var a = new Vector3();
  78. var b = new Vector3();
  79. return function ( object1, object2 ) {
  80. a.setFromMatrixPosition( object1.matrixWorld );
  81. b.setFromMatrixPosition( object2.matrixWorld );
  82. return a.distanceToSquared( b );
  83. };
  84. }();
  85. var filterAndFlatten = function ( scene ) {
  86. var result = [];
  87. scene.traverse( function ( object ) {
  88. if ( object instanceof CSS2DObject ) result.push( object );
  89. } );
  90. return result;
  91. };
  92. var zOrder = function ( scene ) {
  93. var sorted = filterAndFlatten( scene ).sort( function ( a, b ) {
  94. var distanceA = cache.objects.get( a ).distanceToCameraSquared;
  95. var distanceB = cache.objects.get( b ).distanceToCameraSquared;
  96. return distanceA - distanceB;
  97. } );
  98. var zMax = sorted.length;
  99. for ( var i = 0, l = sorted.length; i < l; i ++ ) {
  100. sorted[ i ].element.style.zIndex = zMax - i;
  101. }
  102. };
  103. this.render = function ( scene, camera ) {
  104. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  105. if ( camera.parent === null ) camera.updateMatrixWorld();
  106. viewMatrix.copy( camera.matrixWorldInverse );
  107. viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, viewMatrix );
  108. renderObject( scene, scene, camera );
  109. zOrder( scene );
  110. };
  111. };
  112. export { CSS2DObject, CSS2DRenderer };