2
0

ClippingContext.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. import { Matrix3, Plane, Vector4 } from 'three';
  2. const _plane = new Plane();
  3. const _viewNormalMatrix = new Matrix3();
  4. let _clippingContextVersion = 0;
  5. class ClippingContext {
  6. constructor() {
  7. this.version = ++ _clippingContextVersion;
  8. this.globalClippingCount = 0;
  9. this.localClippingCount = 0;
  10. this.localClippingEnabled = false;
  11. this.localClipIntersection = false;
  12. this.planes = [];
  13. this.parentVersion = 0;
  14. }
  15. projectPlanes( source, offset ) {
  16. const l = source.length;
  17. const planes = this.planes;
  18. for ( let i = 0; i < l; i ++ ) {
  19. _plane.copy( source[ i ] ).applyMatrix4( this.viewMatrix, _viewNormalMatrix );
  20. const v = planes[ offset + i ];
  21. const normal = _plane.normal;
  22. v.x = - normal.x;
  23. v.y = - normal.y;
  24. v.z = - normal.z;
  25. v.w = _plane.constant;
  26. }
  27. }
  28. updateGlobal( renderer, camera ) {
  29. const rendererClippingPlanes = renderer.clippingPlanes;
  30. this.viewMatrix = camera.matrixWorldInverse;
  31. _viewNormalMatrix.getNormalMatrix( this.viewMatrix );
  32. let update = false;
  33. if ( Array.isArray( rendererClippingPlanes ) && rendererClippingPlanes.length !== 0 ) {
  34. const l = rendererClippingPlanes.length;
  35. if ( l !== this.globalClippingCount ) {
  36. const planes = [];
  37. for ( let i = 0; i < l; i ++ ) {
  38. planes.push( new Vector4() );
  39. }
  40. this.globalClippingCount = l;
  41. this.planes = planes;
  42. update = true;
  43. }
  44. this.projectPlanes( rendererClippingPlanes, 0 );
  45. } else if ( this.globalClippingCount !== 0 ) {
  46. this.globalClippingCount = 0;
  47. this.planes = [];
  48. update = true;
  49. }
  50. if ( renderer.localClippingEnabled !== this.localClippingEnabled ) {
  51. this.localClippingEnabled = renderer.localClippingEnabled;
  52. update = true;
  53. }
  54. if ( update ) this.version = _clippingContextVersion ++;
  55. }
  56. update( parent, material ) {
  57. let update = false;
  58. if ( this !== parent && parent.version !== this.parentVersion ) {
  59. this.globalClippingCount = material.isShadowNodeMaterial ? 0 : parent.globalClippingCount;
  60. this.localClippingEnabled = parent.localClippingEnabled;
  61. this.planes = Array.from( parent.planes );
  62. this.parentVersion = parent.version;
  63. this.viewMatrix = parent.viewMatrix;
  64. update = true;
  65. }
  66. if ( this.localClippingEnabled ) {
  67. const localClippingPlanes = material.clippingPlanes;
  68. if ( ( Array.isArray( localClippingPlanes ) && localClippingPlanes.length !== 0 ) ) {
  69. const l = localClippingPlanes.length;
  70. const planes = this.planes;
  71. const offset = this.globalClippingCount;
  72. if ( update || l !== this.localClippingCount ) {
  73. planes.length = offset + l;
  74. for ( let i = 0; i < l; i ++ ) {
  75. planes[ offset + i ] = new Vector4();
  76. }
  77. this.localClippingCount = l;
  78. update = true;
  79. }
  80. this.projectPlanes( localClippingPlanes, offset );
  81. } else if ( this.localClippingCount !== 0 ) {
  82. this.localClippingCount = 0;
  83. update = true;
  84. }
  85. if ( this.localClipIntersection !== material.clipIntersection ) {
  86. this.localClipIntersection = material.clipIntersection;
  87. update = true;
  88. }
  89. }
  90. if ( update ) this.version = _clippingContextVersion ++;
  91. }
  92. }
  93. export default ClippingContext;