@scenoco-three/unity 0.9.0 → 0.9.6

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/README.md CHANGED
@@ -20,7 +20,7 @@ Unity builds them the next time it compiles (or from **SceNoCo ▸ Import**):
20
20
  - **Behaviours** as MonoBehaviours. A game behaviour gets a C# stub in `Assets/Scripts/` with the
21
21
  fields its elements write, already set on every prefab and scene; its logic is yours to port. The
22
22
  stub is written once and never overwritten. Rapier's and Box2D's components become Unity's own
23
- rigidbodies and colliders.
23
+ rigidbodies and colliders, and Box2D's joints Unity's 2D joints.
24
24
 
25
25
  The game is read in its own page, through its own Vite dev server and a headless Chrome: every
26
26
  module but the entry is imported, so nothing starts, and each prefab is built with its behaviours
package/dist/convert.d.ts CHANGED
@@ -31,6 +31,13 @@ export type UnityValue = null | number | boolean | string | {
31
31
  node: string;
32
32
  } | {
33
33
  asset: string;
34
+ }
35
+ /** A plain settings object — a physics material — which the importer reads as it is. */
36
+ | {
37
+ object: {
38
+ type: string;
39
+ fields: Record<string, number | boolean | string>;
40
+ };
34
41
  };
35
42
  export interface UnityComponent {
36
43
  type: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@scenoco-three/unity",
3
- "version": "0.9.0",
3
+ "version": "0.9.6",
4
4
  "description": "Export a SceNoCo game's prefabs, scenes, meshes and materials into a Unity project: nested prefabs, shared materials, behaviours as MonoBehaviours with their fields wired.",
5
5
  "keywords": [
6
6
  "three",
@@ -53,7 +53,7 @@
53
53
  "playwright-core": "^1.62.1"
54
54
  },
55
55
  "peerDependencies": {
56
- "@scenoco-three/core": "^0.9.0",
56
+ "@scenoco-three/core": "^0.9.6",
57
57
  "vite": ">=6"
58
58
  }
59
59
  }
@@ -374,6 +374,7 @@ namespace SceNoCo
374
374
  }
375
375
  so.ApplyModifiedPropertiesWithoutUndo();
376
376
  }
377
+ Physics.WireJoints(n, go, root, warnings.Add);
377
378
  if (n.ContainsKey("prefab")) return; // a nested prefab is wired in its own asset
378
379
  var children = List(n["children"]).Select(Obj).ToList();
379
380
  for (var i = 0; i < children.Count && i < go.transform.childCount; i++) Wire(children[i], go.transform.GetChild(i).gameObject, root);
@@ -532,20 +533,23 @@ namespace SceNoCo
532
533
  }
533
534
  case "Rigidbody2D":
534
535
  {
536
+ // SceNoCo's enums carry Unity's own values.
535
537
  var rb = go.AddComponent<Rigidbody2D>();
536
- var body = f.TryGetValue("bodyType", out var b) ? (string)b : "dynamic";
537
- rb.bodyType = body == "static" ? RigidbodyType2D.Static : body == "kinematic" ? RigidbodyType2D.Kinematic : RigidbodyType2D.Dynamic;
538
- rb.linearDamping = Num(f, "linearDamping", 0);
539
- rb.angularDamping = Num(f, "angularDamping", 0);
540
- rb.freezeRotation = Bool(f, "fixedRotation", false);
538
+ rb.bodyType = (RigidbodyType2D)(int)Num(f, "bodyType", 0);
539
+ rb.useAutoMass = Bool(f, "useAutoMass", false);
540
+ if (!rb.useAutoMass) rb.mass = Num(f, "mass", 1);
541
541
  rb.gravityScale = Num(f, "gravityScale", 1);
542
- if (Bool(f, "bullet", false)) rb.collisionDetectionMode = CollisionDetectionMode2D.Continuous;
542
+ rb.linearDamping = Num(f, "linearDamping", 0);
543
+ rb.angularDamping = Num(f, "angularDamping", 0.05f);
544
+ rb.constraints = (RigidbodyConstraints2D)(int)Num(f, "constraints", 0);
545
+ if (Bool(f, "freezeRotation", false)) rb.freezeRotation = true;
546
+ rb.collisionDetectionMode = (CollisionDetectionMode2D)(int)Num(f, "collisionDetectionMode", 0);
543
547
  return true;
544
548
  }
545
549
  case "BoxCollider2D":
546
550
  {
547
551
  var c = go.AddComponent<BoxCollider2D>();
548
- if (f.ContainsKey("halfExtents")) { var h = Floats(Obj(f["halfExtents"])["v2"]); c.size = new Vector2(h[0] * 2, h[1] * 2); }
552
+ if (f.ContainsKey("size")) c.size = V2(f["size"]);
549
553
  SetCollider2D(c, f);
550
554
  return true;
551
555
  }
@@ -559,8 +563,8 @@ namespace SceNoCo
559
563
  case "CapsuleCollider2D":
560
564
  {
561
565
  var c = go.AddComponent<CapsuleCollider2D>();
562
- if (f.ContainsKey("size")) { var s = Floats(Obj(f["size"])["v2"]); c.size = new Vector2(s[0], s[1]); }
563
- c.direction = f.TryGetValue("direction", out var d) && (string)d == "horizontal" ? CapsuleDirection2D.Horizontal : CapsuleDirection2D.Vertical;
566
+ if (f.ContainsKey("size")) c.size = V2(f["size"]);
567
+ c.direction = (CapsuleDirection2D)(int)Num(f, "direction", 0);
564
568
  SetCollider2D(c, f);
565
569
  return true;
566
570
  }
@@ -572,28 +576,154 @@ namespace SceNoCo
572
576
  return true;
573
577
  }
574
578
  case "EdgeCollider2D":
575
- case "ChainCollider2D":
576
579
  {
577
580
  var c = go.AddComponent<EdgeCollider2D>();
578
- if (type == "EdgeCollider2D" && f.ContainsKey("from") && f.ContainsKey("to"))
579
- {
580
- var a = Floats(Obj(f["from"])["v2"]);
581
- var b2 = Floats(Obj(f["to"])["v2"]);
582
- c.points = new[] { new Vector2(a[0], a[1]), new Vector2(b2[0], b2[1]) };
583
- }
584
- else if (f.ContainsKey("points"))
581
+ if (f.ContainsKey("points")) c.points = Points(f["points"]);
582
+ SetCollider2D(c, f);
583
+ return true;
584
+ }
585
+ // Joints: `connectedBody` is another node's, set by `WireJoints` once every node is built.
586
+ case "FixedJoint2D":
587
+ {
588
+ var j = go.AddComponent<FixedJoint2D>();
589
+ SetAnchored(j, f, true);
590
+ j.frequency = Num(f, "frequency", 0);
591
+ j.dampingRatio = Num(f, "dampingRatio", 0);
592
+ return true;
593
+ }
594
+ case "HingeJoint2D":
595
+ {
596
+ var j = go.AddComponent<HingeJoint2D>();
597
+ SetAnchored(j, f, true);
598
+ j.useLimits = Bool(f, "useLimits", false);
599
+ if (f.ContainsKey("limits")) { var l = Fields(f["limits"]); j.limits = new JointAngleLimits2D { min = Num(l, "min", 0), max = Num(l, "max", 359) }; }
600
+ j.useMotor = Bool(f, "useMotor", false);
601
+ if (f.ContainsKey("motor")) j.motor = Motor(f["motor"]);
602
+ return true;
603
+ }
604
+ case "DistanceJoint2D":
605
+ {
606
+ var j = go.AddComponent<DistanceJoint2D>();
607
+ SetAnchored(j, f, false);
608
+ j.autoConfigureDistance = Bool(f, "autoConfigureDistance", true);
609
+ if (f.ContainsKey("distance")) j.distance = Num(f, "distance", 1);
610
+ j.maxDistanceOnly = Bool(f, "maxDistanceOnly", false);
611
+ return true;
612
+ }
613
+ case "SpringJoint2D":
614
+ {
615
+ var j = go.AddComponent<SpringJoint2D>();
616
+ SetAnchored(j, f, false);
617
+ j.autoConfigureDistance = Bool(f, "autoConfigureDistance", true);
618
+ if (f.ContainsKey("distance")) j.distance = Num(f, "distance", 1);
619
+ j.dampingRatio = Num(f, "dampingRatio", 0);
620
+ j.frequency = Num(f, "frequency", 1);
621
+ return true;
622
+ }
623
+ case "SliderJoint2D":
624
+ {
625
+ var j = go.AddComponent<SliderJoint2D>();
626
+ SetAnchored(j, f, true);
627
+ j.autoConfigureAngle = Bool(f, "autoConfigureAngle", false);
628
+ j.angle = Num(f, "angle", 0);
629
+ j.useLimits = Bool(f, "useLimits", false);
630
+ if (f.ContainsKey("limits")) { var l = Fields(f["limits"]); j.limits = new JointTranslationLimits2D { min = Num(l, "min", 0), max = Num(l, "max", 0) }; }
631
+ j.useMotor = Bool(f, "useMotor", false);
632
+ if (f.ContainsKey("motor")) j.motor = Motor(f["motor"]);
633
+ return true;
634
+ }
635
+ case "WheelJoint2D":
636
+ {
637
+ var j = go.AddComponent<WheelJoint2D>();
638
+ SetAnchored(j, f, true);
639
+ if (f.ContainsKey("suspension"))
585
640
  {
586
- var pts = Points(f["points"]).ToList();
587
- if (Bool(f, "loop", false) && pts.Count > 0) pts.Add(pts[0]);
588
- c.points = pts.ToArray();
641
+ var s = Fields(f["suspension"]);
642
+ j.suspension = new JointSuspension2D { dampingRatio = Num(s, "dampingRatio", 0.7f), frequency = Num(s, "frequency", 2), angle = Num(s, "angle", 90) };
589
643
  }
590
- SetCollider2D(c, f);
644
+ j.useMotor = Bool(f, "useMotor", false);
645
+ if (f.ContainsKey("motor")) j.motor = Motor(f["motor"]);
646
+ return true;
647
+ }
648
+ case "FrictionJoint2D":
649
+ {
650
+ var j = go.AddComponent<FrictionJoint2D>();
651
+ SetAnchored(j, f, true);
652
+ j.maxForce = Num(f, "maxForce", 1);
653
+ j.maxTorque = Num(f, "maxTorque", 1);
654
+ return true;
655
+ }
656
+ case "RelativeJoint2D":
657
+ {
658
+ var j = go.AddComponent<RelativeJoint2D>();
659
+ SetJoint(j, f);
660
+ j.maxForce = Num(f, "maxForce", 10000);
661
+ j.maxTorque = Num(f, "maxTorque", 10000);
662
+ j.correctionScale = Num(f, "correctionScale", 0.3f);
663
+ j.autoConfigureOffset = Bool(f, "autoConfigureOffset", true);
664
+ if (f.ContainsKey("linearOffset")) j.linearOffset = V2(f["linearOffset"]);
665
+ if (f.ContainsKey("angularOffset")) j.angularOffset = Num(f, "angularOffset", 0);
666
+ return true;
667
+ }
668
+ case "TargetJoint2D":
669
+ {
670
+ var j = go.AddComponent<TargetJoint2D>();
671
+ SetJoint(j, f);
672
+ if (f.ContainsKey("anchor")) j.anchor = V2(f["anchor"]);
673
+ j.autoConfigureTarget = Bool(f, "autoConfigureTarget", true);
674
+ if (f.ContainsKey("target")) j.target = V2(f["target"]);
675
+ j.maxForce = Num(f, "maxForce", 1000);
676
+ j.dampingRatio = Num(f, "dampingRatio", 1);
677
+ j.frequency = Num(f, "frequency", 5);
591
678
  return true;
592
679
  }
593
680
  }
594
681
  return false;
595
682
  }
596
683
 
684
+ static void SetJoint(Joint2D j, Dictionary<string, object> f)
685
+ {
686
+ j.enableCollision = Bool(f, "enableCollision", false);
687
+ if (f.ContainsKey("breakForce")) j.breakForce = Num(f, "breakForce", float.PositiveInfinity);
688
+ if (f.ContainsKey("breakTorque")) j.breakTorque = Num(f, "breakTorque", float.PositiveInfinity);
689
+ }
690
+
691
+ /** `autoConfigureConnectedAnchor` before `connectedAnchor`: Unity overwrites the anchor while it is on. */
692
+ static void SetAnchored(AnchoredJoint2D j, Dictionary<string, object> f, bool autoByDefault)
693
+ {
694
+ SetJoint(j, f);
695
+ if (f.ContainsKey("anchor")) j.anchor = V2(f["anchor"]);
696
+ j.autoConfigureConnectedAnchor = Bool(f, "autoConfigureConnectedAnchor", autoByDefault);
697
+ if (f.ContainsKey("connectedAnchor")) j.connectedAnchor = V2(f["connectedAnchor"]);
698
+ }
699
+
700
+ /** A settings object's fields: `{ object: { fields } }`. */
701
+ static Dictionary<string, object> Fields(object v) => Obj(Obj(Obj(v)["object"])["fields"]);
702
+
703
+ static JointMotor2D Motor(object v)
704
+ {
705
+ var m = Fields(v);
706
+ return new JointMotor2D { motorSpeed = Num(m, "motorSpeed", 0), maxMotorTorque = Num(m, "maxMotorTorque", 10000) };
707
+ }
708
+
709
+ /** Each joint's `connectedBody`: the Rigidbody2D on the node it names, joints matched by type in order. */
710
+ public static void WireJoints(Dictionary<string, object> n, GameObject go, GameObject root, Action<string> warn)
711
+ {
712
+ var seen = new Dictionary<string, int>();
713
+ foreach (var comp in List(n["components"]).Select(Obj).Where(c => !(bool)c["game"]))
714
+ {
715
+ var type = Str(comp, "type");
716
+ var t = typeof(Joint2D).Assembly.GetType("UnityEngine." + type);
717
+ if (t == null || !typeof(Joint2D).IsAssignableFrom(t)) continue;
718
+ seen[type] = seen.TryGetValue(type, out var i) ? i + 1 : 0;
719
+ if (!Obj(comp["fields"]).TryGetValue("connectedBody", out var link) || !(link is Dictionary<string, object> l) || !l.TryGetValue("node", out var path)) continue;
720
+ var joint = (Joint2D)go.GetComponents(t)[seen[type]];
721
+ var target = Find(root, (string)path);
722
+ joint.connectedBody = target != null ? target.GetComponent<Rigidbody2D>() : null;
723
+ if (joint.connectedBody == null) warn($"{type} on {go.name}: its connectedBody was not found");
724
+ }
725
+ }
726
+
597
727
  static void SetCollider3D(Collider c, Dictionary<string, object> f)
598
728
  {
599
729
  c.isTrigger = Bool(f, "isTrigger", false);
@@ -613,10 +743,16 @@ namespace SceNoCo
613
743
 
614
744
  static void SetCollider2D(Collider2D c, Dictionary<string, object> f)
615
745
  {
616
- c.isTrigger = Bool(f, "isSensor", false);
617
- if (!f.ContainsKey("friction") && !f.ContainsKey("restitution")) return;
618
- var friction = Num(f, "friction", 0.3f);
619
- var bounce = Num(f, "restitution", 0);
746
+ c.isTrigger = Bool(f, "isTrigger", false);
747
+ if (f.ContainsKey("offset")) c.offset = V2(f["offset"]);
748
+ if (f.ContainsKey("density")) c.density = Num(f, "density", 1);
749
+ // A shared PhysicsMaterial2D, or the collider's own friction and bounciness: one asset per pair.
750
+ var surface = f.TryGetValue("material", out var mv) && mv is Dictionary<string, object> mo && mo.TryGetValue("object", out var ov)
751
+ ? Obj(Obj(ov)["fields"])
752
+ : f;
753
+ if (!surface.ContainsKey("friction") && !surface.ContainsKey("bounciness")) return;
754
+ var friction = Num(surface, "friction", 0.4f);
755
+ var bounce = Num(surface, "bounciness", 0);
620
756
  var path = $"Assets/Materials/Physics/Friction{friction:0.##}_Bounce{bounce:0.##}_2D.asset".Replace(',', '.');
621
757
  var m = AssetDatabase.LoadAssetAtPath<PhysicsMaterial2D>(path);
622
758
  if (m == null)
@@ -628,6 +764,12 @@ namespace SceNoCo
628
764
  c.sharedMaterial = m;
629
765
  }
630
766
 
767
+ static Vector2 V2(object v)
768
+ {
769
+ var p = Floats(Obj(v)["v2"]);
770
+ return new Vector2(p[0], p[1]);
771
+ }
772
+
631
773
  static Vector2[] Points(object v) =>
632
774
  List(Obj(v)["array"]).Select(p => Floats(Obj(p)["v2"])).Select(p => new Vector2(p[0], p[1])).ToArray();
633
775
  }