CSS2DRenderer.js 4.2 KB

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