TilesGroup.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Group, Matrix4 } from 'three';
  2. // Specialization of "Group" that only updates world matrices of children if
  3. // the transform has changed since the last update and ignores the "force"
  4. // parameter under the assumption that the children tiles will not move.
  5. const tempMat = new Matrix4();
  6. export class TilesGroup extends Group {
  7. constructor( tilesRenderer ) {
  8. super();
  9. this.tilesRenderer = tilesRenderer;
  10. }
  11. raycast( raycaster, intersects ) {
  12. this.tilesRenderer.raycast( raycaster, intersects );
  13. }
  14. updateMatrixWorld( force ) {
  15. if ( this.matrixAutoUpdate ) {
  16. this.updateMatrix();
  17. }
  18. if ( this.matrixWorldNeedsUpdate || force ) {
  19. if ( this.parent === null ) {
  20. tempMat.copy( this.matrix );
  21. } else {
  22. tempMat.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  23. }
  24. this.matrixWorldNeedsUpdate = false;
  25. // check if the matrix changed relative to what it was.
  26. const elA = tempMat.elements;
  27. const elB = this.matrixWorld.elements;
  28. let isDifferent = false;
  29. for ( let i = 0; i < 16; i ++ ) {
  30. const itemA = elA[ i ];
  31. const itemB = elB[ i ];
  32. const diff = Math.abs( itemA - itemB );
  33. if ( diff > Number.EPSILON ) {
  34. isDifferent = true;
  35. break;
  36. }
  37. }
  38. if ( isDifferent ) {
  39. this.matrixWorld.copy( tempMat );
  40. // update children
  41. // the children will not have to change unless the parent group has updated
  42. const children = this.children;
  43. for ( let i = 0, l = children.length; i < l; i ++ ) {
  44. children[ i ].updateMatrixWorld();
  45. }
  46. }
  47. }
  48. }
  49. }