factor.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import * as three from "three-js";
  2. class factor {
  3. #mesh;
  4. constructor(mesh) {
  5. if (!(mesh instanceof three.Mesh)) {
  6. throw new TypeError("Must initialize with Mesh.");
  7. }
  8. this.#mesh = mesh;
  9. }
  10. get mesh() {
  11. if (!(this.#mesh instanceof three.Mesh)) {
  12. throw new TypeError("Mesh is not initialized.");
  13. }
  14. return this.#mesh;
  15. }
  16. loop() {
  17. return null;
  18. }
  19. }
  20. class functional_factor extends factor {
  21. #animation
  22. constructor (init, animation = null) {
  23. if (typeof(init) !== "function") {
  24. throw new TypeError("Init must be an function.");
  25. }
  26. if (animation !== null && typeof(animation) !== "function") {
  27. throw new TypeError("Animation could only be null or function.");
  28. }
  29. const mesh = init(this);
  30. if (!(mesh instanceof three.Mesh)) {
  31. throw new TypeError("Factor initializer must return mesh.");
  32. }
  33. super(mesh);
  34. this.#animation = animation;
  35. }
  36. loop() {
  37. if (this.#animation !== null) {
  38. this.#animation(this);
  39. }
  40. }
  41. }
  42. export { factor, functional_factor }