@polarfront-lab/ionian 1.3.0 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/ionian.js CHANGED
@@ -142,6 +142,9 @@ class AssetService {
142
142
  getTextures() {
143
143
  return Array.from(this.textures.values());
144
144
  }
145
+ hasMatcap(id) {
146
+ return this.textures.has(id);
147
+ }
145
148
  /**
146
149
  * Loads a mesh asynchronously.
147
150
  * @param id - The ID of the mesh.
@@ -723,6 +726,7 @@ class IntersectionService {
723
726
  * @param destinationGeometry The destination geometry.
724
727
  */
725
728
  constructor(eventEmitter, camera, originGeometry, destinationGeometry) {
729
+ __publicField(this, "active", true);
726
730
  __publicField(this, "raycaster", new THREE.Raycaster());
727
731
  __publicField(this, "mousePosition", new THREE.Vector2());
728
732
  __publicField(this, "camera");
@@ -742,6 +746,9 @@ class IntersectionService {
742
746
  this.destinationGeometry = destinationGeometry;
743
747
  this.geometryNeedsUpdate = true;
744
748
  }
749
+ setActive(active) {
750
+ this.active = active;
751
+ }
745
752
  getIntersectionMesh() {
746
753
  return this.intersectionMesh;
747
754
  }
@@ -788,7 +795,7 @@ class IntersectionService {
788
795
  * Set the mouse position.
789
796
  * @param mousePosition
790
797
  */
791
- setMousePosition(mousePosition) {
798
+ setPointerPosition(mousePosition) {
792
799
  if (mousePosition) this.mousePosition.copy(mousePosition);
793
800
  }
794
801
  /**
@@ -796,6 +803,7 @@ class IntersectionService {
796
803
  * @returns The intersection point or undefined if no intersection was found.
797
804
  */
798
805
  calculate(instancedMesh) {
806
+ if (!this.active) return;
799
807
  this.updateIntersectionMesh(instancedMesh);
800
808
  if (!this.camera) return;
801
809
  if (this.geometryNeedsUpdate) {
@@ -1498,20 +1506,22 @@ class ParticlesEngine {
1498
1506
  __publicField(this, "transitionService");
1499
1507
  __publicField(this, "engineState");
1500
1508
  __publicField(this, "intersectionService");
1509
+ const { scene, renderer, camera, textureSize, useIntersection = true } = params;
1501
1510
  this.eventEmitter = new DefaultEventEmitter();
1502
- this.serviceStates = this.initialServiceStates();
1511
+ this.serviceStates = this.getInitialServiceStates();
1503
1512
  this.eventEmitter.on("serviceStateUpdated", this.handleServiceStateUpdated.bind(this));
1504
- this.scene = params.scene;
1505
- this.renderer = params.renderer;
1506
- this.engineState = this.initialEngineState(params.textureSize);
1513
+ this.scene = scene;
1514
+ this.renderer = renderer;
1515
+ this.engineState = this.initialEngineState(params);
1507
1516
  this.assetService = new AssetService(this.eventEmitter);
1508
1517
  this.transitionService = new TransitionService(this.eventEmitter);
1509
- this.dataTextureManager = new DataTextureService(this.eventEmitter, params.textureSize);
1510
- this.simulationRendererService = new SimulationRendererService(this.eventEmitter, params.textureSize, this.renderer);
1511
- this.instancedMeshManager = new InstancedMeshManager(params.textureSize);
1518
+ this.dataTextureManager = new DataTextureService(this.eventEmitter, textureSize);
1519
+ this.simulationRendererService = new SimulationRendererService(this.eventEmitter, textureSize, this.renderer);
1520
+ this.instancedMeshManager = new InstancedMeshManager(textureSize);
1512
1521
  this.instancedMeshManager.useMatcapMaterial();
1513
1522
  this.scene.add(this.instancedMeshManager.getMesh());
1514
- this.intersectionService = new IntersectionService(this.eventEmitter, params.camera);
1523
+ this.intersectionService = new IntersectionService(this.eventEmitter, camera);
1524
+ if (!useIntersection) this.intersectionService.setActive(false);
1515
1525
  this.eventEmitter.on("transitionProgressed", this.handleTransitionProgress.bind(this));
1516
1526
  this.eventEmitter.on("interactionPositionUpdated", this.handleInteractionPositionUpdated.bind(this));
1517
1527
  }
@@ -1572,6 +1582,30 @@ class ParticlesEngine {
1572
1582
  this.engineState.destinationMatcapID = matcapID;
1573
1583
  this.instancedMeshManager.setDestinationMatcap(this.assetService.getMatcap(matcapID));
1574
1584
  }
1585
+ setOriginColor(color, override = false) {
1586
+ if (override) this.eventEmitter.emit("transitionCancelled", { type: "matcap" });
1587
+ this.instancedMeshManager.setOriginColor(color);
1588
+ }
1589
+ setDestinationColor(color, override = false) {
1590
+ if (override) this.eventEmitter.emit("transitionCancelled", { type: "matcap" });
1591
+ this.instancedMeshManager.setDestinationColor(color);
1592
+ }
1593
+ setOriginTexture(id, override = false) {
1594
+ if (override) this.eventEmitter.emit("transitionCancelled", { type: "matcap" });
1595
+ if (typeof id === "string" && this.assetService.hasMatcap(id)) {
1596
+ this.setOriginMatcap(id);
1597
+ } else {
1598
+ this.setOriginColor(id);
1599
+ }
1600
+ }
1601
+ setDestinationTexture(id, override = false) {
1602
+ if (override) this.eventEmitter.emit("transitionCancelled", { type: "matcap" });
1603
+ if (typeof id === "string" && this.assetService.hasMatcap(id)) {
1604
+ this.setDestinationMatcap(id);
1605
+ } else {
1606
+ this.setDestinationColor(id);
1607
+ }
1608
+ }
1575
1609
  setMatcapProgress(progress, override = false) {
1576
1610
  if (override) this.eventEmitter.emit("transitionCancelled", { type: "matcap" });
1577
1611
  this.engineState.matcapTransitionProgress = progress;
@@ -1592,7 +1626,12 @@ class ParticlesEngine {
1592
1626
  this.eventEmitter.emit("invalidRequest", { message: `Mesh with id "${this.engineState.destinationMeshID}" does not exist` });
1593
1627
  return;
1594
1628
  }
1595
- this.dataTextureManager.getDataTexture(originMesh).then((texture) => this.simulationRendererService.setOriginDataTexture({ dataTexture: texture, textureSize: size }));
1629
+ this.dataTextureManager.getDataTexture(originMesh).then(
1630
+ (texture) => this.simulationRendererService.setOriginDataTexture({
1631
+ dataTexture: texture,
1632
+ textureSize: size
1633
+ })
1634
+ );
1596
1635
  this.dataTextureManager.getDataTexture(destinationMesh).then(
1597
1636
  (texture) => this.simulationRendererService.setDestinationDataTexture({
1598
1637
  dataTexture: texture,
@@ -1619,9 +1658,18 @@ class ParticlesEngine {
1619
1658
  async fetchAndRegisterMatcap(id, url) {
1620
1659
  return await this.assetService.loadTextureAsync(id, url);
1621
1660
  }
1661
+ useIntersect(use) {
1662
+ this.intersectionService.setActive(use);
1663
+ this.engineState.useIntersect = use;
1664
+ if (!use) {
1665
+ this.engineState.pointerPosition = { x: -99999999, y: -99999999 };
1666
+ this.intersectionService.setPointerPosition(this.engineState.pointerPosition);
1667
+ }
1668
+ }
1622
1669
  setPointerPosition(position) {
1670
+ if (!this.engineState.useIntersect) return;
1623
1671
  this.engineState.pointerPosition = position;
1624
- this.intersectionService.setMousePosition(position);
1672
+ this.intersectionService.setPointerPosition(position);
1625
1673
  }
1626
1674
  setGeometrySize(geometrySize) {
1627
1675
  this.engineState.instanceGeometryScale = geometrySize;
@@ -1665,6 +1713,24 @@ class ParticlesEngine {
1665
1713
  }
1666
1714
  );
1667
1715
  }
1716
+ scheduleTextureTransition(origin, destination, options) {
1717
+ const easing = (options == null ? void 0 : options.easing) ?? linear;
1718
+ const duration = (options == null ? void 0 : options.duration) ?? 1e3;
1719
+ if (options == null ? void 0 : options.override) {
1720
+ this.eventEmitter.emit("transitionCancelled", { type: "matcap" });
1721
+ }
1722
+ this.transitionService.enqueue(
1723
+ "matcap",
1724
+ { easing, duration },
1725
+ {
1726
+ onTransitionBegin: () => {
1727
+ this.setOriginTexture(origin);
1728
+ this.setDestinationTexture(destination);
1729
+ this.setMatcapProgress(0);
1730
+ }
1731
+ }
1732
+ );
1733
+ }
1668
1734
  handleServiceStateUpdated({ type, state }) {
1669
1735
  this.serviceStates[type] = state;
1670
1736
  }
@@ -1688,9 +1754,9 @@ class ParticlesEngine {
1688
1754
  this.assetService.dispose();
1689
1755
  this.dataTextureManager.dispose();
1690
1756
  }
1691
- initialEngineState(textureSize) {
1757
+ initialEngineState(params) {
1692
1758
  return {
1693
- textureSize,
1759
+ textureSize: params.textureSize,
1694
1760
  originMeshID: "",
1695
1761
  destinationMeshID: "",
1696
1762
  dataTextureTransitionProgress: 0,
@@ -1701,10 +1767,11 @@ class ParticlesEngine {
1701
1767
  positionalTractionForce: 0.1,
1702
1768
  maxRepelDistance: 0.3,
1703
1769
  pointerPosition: { x: 0, y: 0 },
1704
- instanceGeometryScale: { x: 1, y: 1, z: 1 }
1770
+ instanceGeometryScale: { x: 1, y: 1, z: 1 },
1771
+ useIntersect: params.useIntersection ?? true
1705
1772
  };
1706
1773
  }
1707
- initialServiceStates() {
1774
+ getInitialServiceStates() {
1708
1775
  return {
1709
1776
  "data-texture": "created",
1710
1777
  "instanced-mesh": "created",
@@ -1726,12 +1793,6 @@ class ParticlesEngine {
1726
1793
  handleInteractionPositionUpdated({ position }) {
1727
1794
  this.simulationRendererService.setInteractionPosition(position);
1728
1795
  }
1729
- setOriginColor(color) {
1730
- this.instancedMeshManager.setOriginColor(color);
1731
- }
1732
- setDestinationColor(color) {
1733
- this.instancedMeshManager.setDestinationColor(color);
1734
- }
1735
1796
  }
1736
1797
  export {
1737
1798
  ParticlesEngine