CSS2DRenderer.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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 () {
  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. element.style.display = ( object.visible && vector.z >= - 1 && vector.z <= 1 ) ? '' : 'none';
  55. var objectData = {
  56. distanceToCameraSquared: getDistanceToSquared( camera, object )
  57. };
  58. cache.objects.set( object, objectData );
  59. if ( element.parentNode !== domElement ) {
  60. domElement.appendChild( element );
  61. }
  62. }
  63. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  64. renderObject( object.children[ i ], camera );
  65. }
  66. };
  67. var getDistanceToSquared = function () {
  68. var a = new THREE.Vector3();
  69. var b = new THREE.Vector3();
  70. return function ( object1, object2 ) {
  71. a.setFromMatrixPosition( object1.matrixWorld );
  72. b.setFromMatrixPosition( object2.matrixWorld );
  73. return a.distanceToSquared( b );
  74. };
  75. }();
  76. var filterAndFlatten = function ( scene ) {
  77. var result = [];
  78. scene.traverse( function ( object ) {
  79. if ( object instanceof THREE.CSS2DObject ) result.push( object );
  80. } );
  81. return result;
  82. };
  83. var zOrder = function ( scene ) {
  84. var sorted = filterAndFlatten( scene ).sort( function ( a, b ) {
  85. var distanceA = cache.objects.get( a ).distanceToCameraSquared;
  86. var distanceB = cache.objects.get( b ).distanceToCameraSquared;
  87. return distanceA - distanceB;
  88. } );
  89. var zMax = sorted.length;
  90. for ( var i = 0, l = sorted.length; i < l; i ++ ) {
  91. sorted[ i ].element.style.zIndex = zMax - i;
  92. }
  93. };
  94. this.render = function ( scene, camera ) {
  95. scene.updateMatrixWorld();
  96. if ( camera.parent === null ) camera.updateMatrixWorld();
  97. viewMatrix.copy( camera.matrixWorldInverse );
  98. viewProjectionMatrix.multiplyMatrices( camera.projectionMatrix, viewMatrix );
  99. renderObject( scene, camera );
  100. zOrder( scene );
  101. };
  102. };