sa2kit 1.0.7 → 1.0.9

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/mmd/index.js CHANGED
@@ -317,6 +317,80 @@ var MMDPlayerEnhanced = ({
317
317
  onAnimationEnded
318
318
  }) => {
319
319
  console.log("\u{1F3A8} [MMDPlayerEnhanced] \u7EC4\u4EF6\u521D\u59CB\u5316");
320
+ React2.useEffect(() => {
321
+ return () => {
322
+ console.log("\u{1F9F9} [MMDPlayerEnhanced] \u7EC4\u4EF6\u5378\u8F7D\uFF0C\u6267\u884C\u5B8C\u6574\u6E05\u7406");
323
+ if (animationIdRef.current) {
324
+ cancelAnimationFrame(animationIdRef.current);
325
+ animationIdRef.current = null;
326
+ }
327
+ if (audioRef.current) {
328
+ audioRef.current.pause();
329
+ audioRef.current.src = "";
330
+ audioRef.current.load();
331
+ audioRef.current = null;
332
+ }
333
+ if (helperRef.current) {
334
+ try {
335
+ helperRef.current.enable("animation", false);
336
+ helperRef.current.enable("ik", false);
337
+ helperRef.current.enable("grant", false);
338
+ helperRef.current.enable("physics", false);
339
+ const helperObjects = helperRef.current.objects;
340
+ if (helperObjects && Array.isArray(helperObjects)) {
341
+ const physicsWorldsToDestroy = /* @__PURE__ */ new Set();
342
+ helperObjects.forEach((obj) => {
343
+ if (obj.physics) {
344
+ const physics = obj.physics;
345
+ if (physics.world) physicsWorldsToDestroy.add(physics.world);
346
+ if (physics.bodies) physics.bodies.length = 0;
347
+ if (physics.constraints) physics.constraints.length = 0;
348
+ obj.physics = null;
349
+ }
350
+ });
351
+ physicsWorldsToDestroy.forEach((world) => {
352
+ try {
353
+ while (world.getNumCollisionObjects() > 0) {
354
+ const obj = world.getCollisionObjectArray().at(0);
355
+ world.removeCollisionObject(obj);
356
+ if (obj && obj.destroy) obj.destroy();
357
+ }
358
+ if (world.destroy) world.destroy();
359
+ } catch (e) {
360
+ }
361
+ });
362
+ helperObjects.length = 0;
363
+ }
364
+ } catch (e) {
365
+ }
366
+ helperRef.current = null;
367
+ }
368
+ if (sceneRef.current) {
369
+ sceneRef.current.clear();
370
+ sceneRef.current = null;
371
+ }
372
+ if (rendererRef.current) {
373
+ rendererRef.current.dispose();
374
+ rendererRef.current = null;
375
+ }
376
+ if (controlsRef.current) {
377
+ controlsRef.current.dispose();
378
+ controlsRef.current = null;
379
+ }
380
+ cameraRef.current = null;
381
+ if (clockRef.current) {
382
+ clockRef.current = new THREE2__namespace.Clock();
383
+ }
384
+ vmdDataRef.current = null;
385
+ if (window.gc) {
386
+ try {
387
+ window.gc();
388
+ } catch (e) {
389
+ }
390
+ }
391
+ console.log("\u2705 [MMDPlayerEnhanced] \u7EC4\u4EF6\u5378\u8F7D\u6E05\u7406\u5B8C\u6210");
392
+ };
393
+ }, []);
320
394
  const [selectedResourceId, setSelectedResourceId] = React2.useState(
321
395
  defaultResourceId || resourcesList?.[0]?.id || ""
322
396
  );
@@ -461,7 +535,18 @@ var MMDPlayerEnhanced = ({
461
535
  animationIdRef.current = requestAnimationFrame(animate);
462
536
  if (helperRef.current && isPlayingRef.current) {
463
537
  const delta = clockRef.current.getDelta();
464
- helperRef.current.update(delta);
538
+ try {
539
+ helperRef.current.update(delta);
540
+ } catch (error2) {
541
+ if (error2.message && error2.message.includes("OOM")) {
542
+ console.error("\u274C \u7269\u7406\u5F15\u64CE\u5185\u5B58\u6EA2\u51FA\uFF0C\u505C\u6B62\u64AD\u653E");
543
+ isPlayingRef.current = false;
544
+ setIsPlaying(false);
545
+ onError?.(new Error("\u7269\u7406\u5F15\u64CE\u5185\u5B58\u6EA2\u51FA"));
546
+ return;
547
+ }
548
+ throw error2;
549
+ }
465
550
  if (!hasAudioRef.current && !loop && !animationEndedFiredRef.current) {
466
551
  const currentTime = clockRef.current.getElapsedTime();
467
552
  if (animationDurationRef.current > 0) {
@@ -537,40 +622,68 @@ var MMDPlayerEnhanced = ({
537
622
  helperRef.current.enable("physics", false);
538
623
  const helperObjects = helperRef.current.objects;
539
624
  if (helperObjects && Array.isArray(helperObjects)) {
625
+ const physicsWorldsToDestroy = /* @__PURE__ */ new Set();
540
626
  helperObjects.forEach((obj) => {
541
627
  if (obj.physics) {
542
628
  try {
543
629
  const physics = obj.physics;
630
+ if (physics.world) {
631
+ physicsWorldsToDestroy.add(physics.world);
632
+ }
544
633
  if (physics.bodies && Array.isArray(physics.bodies)) {
545
634
  physics.bodies.forEach((body) => {
546
635
  if (physics.world && body) {
547
- physics.world.removeRigidBody(body);
636
+ try {
637
+ physics.world.removeRigidBody(body);
638
+ if (window.Ammo && body.destroy) {
639
+ body.destroy();
640
+ }
641
+ } catch (e) {
642
+ }
548
643
  }
549
644
  });
550
645
  physics.bodies.length = 0;
646
+ physics.bodies = null;
551
647
  }
552
648
  if (physics.constraints && Array.isArray(physics.constraints)) {
553
649
  physics.constraints.forEach((constraint) => {
554
650
  if (physics.world && constraint) {
555
- physics.world.removeConstraint(constraint);
651
+ try {
652
+ physics.world.removeConstraint(constraint);
653
+ if (window.Ammo && constraint.destroy) {
654
+ constraint.destroy();
655
+ }
656
+ } catch (e) {
657
+ }
556
658
  }
557
659
  });
558
660
  physics.constraints.length = 0;
559
- }
560
- if (physics.world) {
561
- const world = physics.world;
562
- while (world.getNumCollisionObjects() > 0) {
563
- const body = world.getCollisionObjectArray().at(0);
564
- world.removeCollisionObject(body);
565
- }
661
+ physics.constraints = null;
566
662
  }
567
663
  if (physics.reset) physics.reset();
664
+ physics.world = null;
568
665
  obj.physics = null;
569
666
  } catch (e) {
570
667
  console.warn("\u6E05\u7406\u7269\u7406\u7CFB\u7EDF\u5931\u8D25:", e);
571
668
  }
572
669
  }
573
670
  });
671
+ physicsWorldsToDestroy.forEach((world) => {
672
+ try {
673
+ while (world.getNumCollisionObjects() > 0) {
674
+ const obj = world.getCollisionObjectArray().at(0);
675
+ world.removeCollisionObject(obj);
676
+ if (obj && obj.destroy) {
677
+ obj.destroy();
678
+ }
679
+ }
680
+ if (world.destroy) {
681
+ world.destroy();
682
+ }
683
+ } catch (e) {
684
+ console.warn("\u9500\u6BC1\u7269\u7406\u4E16\u754C\u5931\u8D25:", e);
685
+ }
686
+ });
574
687
  helperObjects.length = 0;
575
688
  }
576
689
  } catch (error2) {
@@ -634,6 +747,7 @@ var MMDPlayerEnhanced = ({
634
747
  if (window.gc) {
635
748
  try {
636
749
  window.gc();
750
+ console.log("\u267B\uFE0F \u5DF2\u89E6\u53D1\u5783\u573E\u56DE\u6536");
637
751
  } catch (e) {
638
752
  }
639
753
  }
@@ -802,13 +916,75 @@ var MMDPlayerEnhanced = ({
802
916
  loadMMD();
803
917
  }, [currentResources, stage?.enablePhysics, autoPlay, loop, onLoad, onError, reloadTrigger]);
804
918
  const play = () => {
805
- if (!helperRef.current && !needReset) return;
806
919
  if (needReset && vmdDataRef.current && sceneRef.current && cameraRef.current) {
807
- const { mesh, vmd, cameraVmd } = vmdDataRef.current;
920
+ console.log("\u{1F504} \u68C0\u6D4B\u5230\u9700\u8981\u91CD\u7F6E\uFF0C\u5F00\u59CB\u5F3A\u5236\u6E05\u7406...");
921
+ if (animationIdRef.current) {
922
+ cancelAnimationFrame(animationIdRef.current);
923
+ animationIdRef.current = null;
924
+ }
808
925
  if (helperRef.current) {
809
926
  try {
927
+ helperRef.current.enable("animation", false);
928
+ helperRef.current.enable("ik", false);
929
+ helperRef.current.enable("grant", false);
930
+ helperRef.current.enable("physics", false);
810
931
  const helperObjects = helperRef.current.objects;
811
932
  if (helperObjects && Array.isArray(helperObjects)) {
933
+ const physicsWorldsToDestroy = /* @__PURE__ */ new Set();
934
+ helperObjects.forEach((obj) => {
935
+ if (obj.physics) {
936
+ try {
937
+ const physics = obj.physics;
938
+ if (physics.world) {
939
+ physicsWorldsToDestroy.add(physics.world);
940
+ }
941
+ if (physics.bodies && Array.isArray(physics.bodies)) {
942
+ physics.bodies.forEach((body) => {
943
+ if (physics.world && body) {
944
+ try {
945
+ physics.world.removeRigidBody(body);
946
+ if (window.Ammo && body.destroy) {
947
+ body.destroy();
948
+ }
949
+ } catch (e) {
950
+ }
951
+ }
952
+ });
953
+ physics.bodies.length = 0;
954
+ physics.bodies = null;
955
+ }
956
+ if (physics.constraints && Array.isArray(physics.constraints)) {
957
+ physics.constraints.forEach((constraint) => {
958
+ if (physics.world && constraint) {
959
+ try {
960
+ physics.world.removeConstraint(constraint);
961
+ if (window.Ammo && constraint.destroy) {
962
+ constraint.destroy();
963
+ }
964
+ } catch (e) {
965
+ }
966
+ }
967
+ });
968
+ physics.constraints.length = 0;
969
+ physics.constraints = null;
970
+ }
971
+ physics.world = null;
972
+ obj.physics = null;
973
+ } catch (e) {
974
+ }
975
+ }
976
+ });
977
+ physicsWorldsToDestroy.forEach((world) => {
978
+ try {
979
+ while (world.getNumCollisionObjects() > 0) {
980
+ const obj = world.getCollisionObjectArray().at(0);
981
+ world.removeCollisionObject(obj);
982
+ if (obj && obj.destroy) obj.destroy();
983
+ }
984
+ if (world.destroy) world.destroy();
985
+ } catch (e) {
986
+ }
987
+ });
812
988
  helperObjects.length = 0;
813
989
  }
814
990
  } catch (error2) {
@@ -817,6 +993,7 @@ var MMDPlayerEnhanced = ({
817
993
  const newHelper = new threeStdlib.MMDAnimationHelper();
818
994
  helperRef.current = newHelper;
819
995
  clockRef.current = new THREE2__namespace.Clock();
996
+ const { mesh, vmd, cameraVmd } = vmdDataRef.current;
820
997
  if (vmd && typeof vmd === "object") {
821
998
  try {
822
999
  newHelper.add(mesh, {
@@ -845,6 +1022,7 @@ var MMDPlayerEnhanced = ({
845
1022
  audioRef.current.currentTime = 0;
846
1023
  }
847
1024
  setNeedReset(false);
1025
+ console.log("\u2705 \u5F3A\u5236\u6E05\u7406\u5B8C\u6210\uFF0C\u5F00\u59CB\u64AD\u653E");
848
1026
  }
849
1027
  if (!helperRef.current) {
850
1028
  console.error("\u274C [play] helper \u4E0D\u5B58\u5728\uFF0C\u65E0\u6CD5\u64AD\u653E");
@@ -879,6 +1057,7 @@ var MMDPlayerEnhanced = ({
879
1057
  };
880
1058
  const stop = () => {
881
1059
  if (!helperRef.current || !sceneRef.current) return;
1060
+ console.log("\u23F9\uFE0F \u505C\u6B62\u64AD\u653E\uFF0C\u5F00\u59CB\u6E05\u7406\u7269\u7406\u7CFB\u7EDF...");
882
1061
  isPlayingRef.current = false;
883
1062
  setIsPlaying(false);
884
1063
  if (audioRef.current) {
@@ -887,6 +1066,73 @@ var MMDPlayerEnhanced = ({
887
1066
  }
888
1067
  clockRef.current.stop();
889
1068
  clockRef.current = new THREE2__namespace.Clock();
1069
+ try {
1070
+ helperRef.current.enable("animation", false);
1071
+ helperRef.current.enable("ik", false);
1072
+ helperRef.current.enable("grant", false);
1073
+ helperRef.current.enable("physics", false);
1074
+ const helperObjects = helperRef.current.objects;
1075
+ if (helperObjects && Array.isArray(helperObjects)) {
1076
+ const physicsWorldsToDestroy = /* @__PURE__ */ new Set();
1077
+ helperObjects.forEach((obj) => {
1078
+ if (obj.physics) {
1079
+ try {
1080
+ const physics = obj.physics;
1081
+ if (physics.world) {
1082
+ physicsWorldsToDestroy.add(physics.world);
1083
+ }
1084
+ if (physics.bodies && Array.isArray(physics.bodies)) {
1085
+ physics.bodies.forEach((body) => {
1086
+ if (physics.world && body) {
1087
+ try {
1088
+ physics.world.removeRigidBody(body);
1089
+ if (window.Ammo && body.destroy) {
1090
+ body.destroy();
1091
+ }
1092
+ } catch (e) {
1093
+ }
1094
+ }
1095
+ });
1096
+ physics.bodies.length = 0;
1097
+ physics.bodies = null;
1098
+ }
1099
+ if (physics.constraints && Array.isArray(physics.constraints)) {
1100
+ physics.constraints.forEach((constraint) => {
1101
+ if (physics.world && constraint) {
1102
+ try {
1103
+ physics.world.removeConstraint(constraint);
1104
+ if (window.Ammo && constraint.destroy) {
1105
+ constraint.destroy();
1106
+ }
1107
+ } catch (e) {
1108
+ }
1109
+ }
1110
+ });
1111
+ physics.constraints.length = 0;
1112
+ physics.constraints = null;
1113
+ }
1114
+ physics.world = null;
1115
+ obj.physics = null;
1116
+ } catch (e) {
1117
+ }
1118
+ }
1119
+ });
1120
+ physicsWorldsToDestroy.forEach((world) => {
1121
+ try {
1122
+ while (world.getNumCollisionObjects() > 0) {
1123
+ const obj = world.getCollisionObjectArray().at(0);
1124
+ world.removeCollisionObject(obj);
1125
+ if (obj && obj.destroy) obj.destroy();
1126
+ }
1127
+ if (world.destroy) world.destroy();
1128
+ } catch (e) {
1129
+ }
1130
+ });
1131
+ helperObjects.length = 0;
1132
+ }
1133
+ } catch (error2) {
1134
+ console.warn("\u505C\u6B62\u65F6\u6E05\u7406\u7269\u7406\u7CFB\u7EDF\u5931\u8D25:", error2);
1135
+ }
890
1136
  const mesh = sceneRef.current.children.find(
891
1137
  (child) => child.type === "SkinnedMesh" || child.isSkinnedMesh
892
1138
  );
@@ -905,7 +1151,7 @@ var MMDPlayerEnhanced = ({
905
1151
  }
906
1152
  }
907
1153
  setNeedReset(true);
908
- console.log("\u23F9\uFE0F \u505C\u6B62\u64AD\u653E\u5E76\u91CD\u7F6E\u5230\u521D\u59CB\u72B6\u6001\uFF0CneedReset = true");
1154
+ console.log("\u2705 \u505C\u6B62\u64AD\u653E\u5E76\u6E05\u7406\u5B8C\u6210\uFF0CneedReset = true");
909
1155
  };
910
1156
  const handleResourceChange = (resourceId) => {
911
1157
  console.log("\u{1F504} [MMDPlayerEnhanced] \u5207\u6362\u8D44\u6E90:", resourceId);