Insect rig improvements

This commit is contained in:
Pascal Serrarens 2026-05-05 08:48:41 +02:00
parent 9eda1cdf06
commit 36c414ae02
15 changed files with 6946 additions and 2318 deletions

View File

@ -10,13 +10,15 @@ namespace CreatureControl {
protected Insect insect;
public override void OnEnable() {
base.OnEnable();
insect = target as Insect;
//bool anythingChanged = false;
bool anythingChanged = false;
anythingChanged |= insect.CheckTargetRig();
base.OnEnable();
//anythingChanged |= insect.CheckTargetRig("InsectRig");
// We already do this in Creature_Editor...
//this.creature.targetRig.MatchTo(this.creature, ref anythingChanged);
//insect.insectRig.MatchTo(insect, ref anythingChanged);
@ -64,10 +66,10 @@ namespace CreatureControl {
// }
*/
// if (anythingChanged) {
// EditorUtility.SetDirty(creature);
// AssetDatabase.SaveAssets();
// }
if (anythingChanged) {
EditorUtility.SetDirty(creature);
AssetDatabase.SaveAssets();
}
}
static void CollectFromStateMachine(AnimatorStateMachine sm, List<AnimationClip> outList) {
@ -111,6 +113,9 @@ namespace CreatureControl {
static bool showTargets;
private void TargetsInspector() {
if (insect.insectRig == null)
return;
bool configurationIncomplete = false;
if (!insect.leftFrontLeg.isConfigured ||
!insect.leftMiddleLeg.isConfigured ||
@ -153,9 +158,11 @@ namespace CreatureControl {
if (somethingChanged) {
Debug.Log("seomthin has changed");
// don't know if this apply/update is really needed....
serializedObject.ApplyModifiedProperties();
insect.insectRig.MatchTo(insect, ref somethingChanged);
serializedObject.Update();
// serializedObject.ApplyModifiedProperties();
insect.insectRig.MatchTo(insect, ref somethingChanged);
EditorUtility.SetDirty(this.insect);
AssetDatabase.SaveAssets();
}
}
}

View File

@ -8,7 +8,7 @@ namespace CreatureControl {
private static bool showfield = false;
public static bool Inspector(SerializedProperty legProp, TargetLeg targetLeg, Leg leg) {
bool anythingChanged = false;
bool somethingChanged = false;
GUIStyle foldoutStyle = new(EditorStyles.foldout) {
margin = EditorStyles.objectField.margin
@ -17,28 +17,49 @@ namespace CreatureControl {
EditorGUILayout.BeginHorizontal();
string legName = ConvertCamelCase(legProp.name);
showfield = EditorGUILayout.Foldout(showfield, legName, true, foldoutStyle);
SerializedProperty femurProp = legProp.FindPropertyRelative("_femur");//nameof(Leg._femur));
SerializedProperty tibiaProp = legProp.FindPropertyRelative("_tibia"); //nameof(Leg._tibia));
SerializedProperty tarsusProp = legProp.FindPropertyRelative("_tarsus"); //nameof(Leg._tarsus));
Transform newFemur = (Transform)EditorGUILayout.ObjectField(femurProp.objectReferenceValue, typeof(Transform), true);
if (newFemur != femurProp.objectReferenceValue) {
leg.DetectBones(newFemur);
anythingChanged = true;
bool femurChanged = FemurInspector(leg);
if (femurChanged) {
Debug.Log("Check");
leg.DetectBones(leg.femur);
}
somethingChanged |= femurChanged;
EditorGUILayout.EndHorizontal();
if (femurProp.objectReferenceValue != null && tibiaProp.objectReferenceValue == null)
if (leg.femur != null && leg.tibia == null)
showfield = true;
if (showfield) {
EditorGUI.indentLevel++;
tibiaProp.objectReferenceValue = (Transform)EditorGUILayout.ObjectField("Lower Leg", tibiaProp.objectReferenceValue, typeof(Transform), true);
tarsusProp.objectReferenceValue = (Transform)EditorGUILayout.ObjectField("Foot", tarsusProp.objectReferenceValue, typeof(Transform), true);
// Need to check if anythingChanged and update the projection if it did
somethingChanged |= TibiaInspector(leg);
somethingChanged |= TarsusInspector(leg);
somethingChanged |= PhalangesInspector(leg);
EditorGUI.indentLevel--;
}
return anythingChanged;
return somethingChanged;
}
protected static bool FemurInspector(Leg leg) {
Transform oldFemur = leg.femur;
leg.femur = (Transform)EditorGUILayout.ObjectField(leg.femur, typeof(Transform), true);
return leg.femur != oldFemur;
}
protected static bool TibiaInspector(Leg leg) {
Transform oldTibia = leg.tibia;
leg.tibia = (Transform)EditorGUILayout.ObjectField("Lower leg", leg.tibia, typeof(Transform), true);
return leg.tibia != oldTibia;
}
protected static bool TarsusInspector(Leg leg) {
Transform oldTarsus = leg.tarsus;
leg.tarsus = (Transform)EditorGUILayout.ObjectField("Foot", leg.tarsus, typeof(Transform), true);
return leg.tarsus != oldTarsus;
}
protected static bool PhalangesInspector(Leg leg) {
Transform oldPhalanges = leg.phalanges;
leg.phalanges = (Transform)EditorGUILayout.ObjectField("Toes", leg.phalanges, typeof(Transform), true);
return leg.phalanges != oldPhalanges;
}
private static string ConvertCamelCase(string text) {

View File

@ -159,6 +159,9 @@ namespace CreatureControl {
/// Update the bones of the creature's rig from the target rig pose
/// </summary>
public virtual void UpdateModel() {
if (this.model == null)
return;
Vector3 newPosition = this.targetRig.transform.position + this.targetToModelTranslation;
Quaternion newOrientation = this.targetRig.transform.rotation * this.targetToModelRotation;
this.model.SetPositionAndRotation(newPosition, newOrientation);

View File

@ -22,7 +22,7 @@ namespace CreatureControl {
public Leg rightFrontLeg;
public Leg rightMiddleLeg;
public Leg rightHindLeg;
public Leg rightHindLeg;
public bool updateAnimations = false;
@ -45,6 +45,9 @@ namespace CreatureControl {
public override void UpdateModel() {
base.UpdateModel();
if (this.insectRig == null)
return;
if (this.insectRig.leftFrontLeg != null)
this.insectRig.leftFrontLeg.UpdateBones(this.leftFrontLeg);
if (this.insectRig.leftMiddleLeg != null)

View File

@ -21,6 +21,7 @@ namespace CreatureControl {
}
}
public readonly static string nameOfFemur = nameof(_femur);
[SerializeField]
private Transform _femur;
/// <summary>
@ -34,6 +35,7 @@ namespace CreatureControl {
}
}
public readonly static string nameOfTibia = nameof(_tibia);
[SerializeField]
private Transform _tibia;
/// <summary>
@ -48,6 +50,7 @@ namespace CreatureControl {
}
}
public readonly static string nameOfTarsus = nameof(_tarsus);
[SerializeField]
private Transform _tarsus;
/// <summary>
@ -62,6 +65,7 @@ namespace CreatureControl {
}
}
public readonly static string nameOfPhalanges = nameof(_phalanges);
[SerializeField]
private Transform _phalanges;
/// <summary>
@ -169,7 +173,6 @@ namespace CreatureControl {
public void DetectBones(Transform root) {
if (root == null)
return;
coxa = femur = tibia = tarsus = phalanges = end = null;
// gather a straight chain following single-child links
List<Transform> chain = new();
@ -180,40 +183,39 @@ namespace CreatureControl {
chain.Add(current);
}
// the detected end bone is the last element in the collected chain
end = chain[^1];
// map chain length to bone roles according to rules:
// 1 => femur
// 2 => femur, tibia
// 3 => femur, tibia, tarsus
// 4 => femur, tibia, tarsus, phalanges
// 5+ => coxa, femur, tibia, tarsus, phalanges (first 5)
int count = chain.Count;
if (count <= 1)
// With one bone (and no end bone) it is not possible to compute the IK
return;
// the detected end bone is the last element in the collected chain
// this.end = chain[^1];
// count--;
if (count == 1)
femur = chain[0];
this.femur = chain[0];
else if (count == 2) {
femur = chain[0];
tibia = chain[1];
this.femur = chain[0];
this.tibia = chain[1];
}
else if (count == 3) {
femur = chain[0];
tibia = chain[1];
tarsus = chain[2];
this.femur = chain[0];
this.tibia = chain[1];
this.tarsus = chain[2];
}
else if (count == 4) {
femur = chain[0];
tibia = chain[1];
tarsus = chain[2];
phalanges = chain[3];
this.femur = chain[0];
this.tibia = chain[1];
this.tarsus = chain[2];
this.phalanges = chain[3];
}
else // count >= 5
{
coxa = chain[0];
femur = chain[1];
tibia = chain[2];
tarsus = chain[3];
phalanges = chain[4];
this.coxa = chain[0];
this.femur = chain[1];
this.tibia = chain[2];
this.tarsus = chain[3];
this.phalanges = chain[4];
}
}
@ -226,9 +228,9 @@ namespace CreatureControl {
public bool isConfigured =>
this.femur != null &&
this.tibia != null &&
this.tarsus != null &&
this.phalanges != null &&
this.end != null;
this.tarsus != null;
// && this.phalanges != null
// && this.end != null;
public Side side;
public bool isLeft => side == Side.Left;

View File

@ -27,6 +27,9 @@ namespace CreatureControl {
/// <param name="creature">The creature to align to</param>
/// <param name="anythingChanged">True when any property of the creature has changed</param>
public virtual void MatchTo(Creature creature, ref bool anythingChanged) {
if (creature == null || creature.model == null)
return;
Vector3 targetToModelTranslation = creature.model.position - this.transform.position;
bool changed = targetToModelTranslation != creature.targetToModelTranslation;
if (changed) {

View File

@ -99,7 +99,9 @@ namespace NanoBrain {
public void SetGraph(GameObject gameObject) {
this.gameObject = gameObject;
if (this.currentCluster == null)
return;
if (Application.isPlaying == false)
this.serializedBrain = new SerializedObject(this.currentCluster.prefab);
this.selectedOutput = this.currentCluster.outputs[0];
@ -121,7 +123,7 @@ namespace NanoBrain {
}
public void OnIMGUI() {
if (Application.isPlaying == false)
if (Application.isPlaying == false && serializedBrain != null)
serializedBrain.Update();
Handles.BeginGUI();

View File

@ -78,10 +78,17 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
bones:
femur: {fileID: 2087731612450819825}
tibia: {fileID: 7566832081299524197}
tarsus: {fileID: 7011186323915432702}
_coxa: {fileID: 0}
_femur: {fileID: 2087731612450819825}
_tibia: {fileID: 7566832081299524197}
_tarsus: {fileID: 7011186323915432702}
_phalanges: {fileID: 0}
_end: {fileID: 0}
_femurLength: 0.004816547
_tibiaLength: 0.008353032
_tarsusLength: 0
_phalangesLength: 0
side: 0
target: {fileID: 8947192729555298471}
targetToBoneFemur: {x: 0, y: 0, z: 0, w: 0}
targetToBoneTibia: {x: 0, y: 0, z: 0, w: 0}
@ -144,7 +151,7 @@ MonoBehaviour:
rightMiddleLeg: {fileID: 4196034226083389076}
rightBackLeg: {fileID: 8323677930838830493}
render: 1
legLength: 0.009770508
legLength: 0.0031432603
--- !u!95 &5843436816833865534
Animator:
serializedVersion: 5
@ -190,7 +197,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 816003872898039836}
serializedVersion: 2
m_LocalRotation: {x: -0.47292894, y: 0.17234407, z: 0.038568486, w: -0.86321974}
m_LocalRotation: {x: -0.47292906, y: 0.17234407, z: 0.038568486, w: -0.8632196}
m_LocalPosition: {x: 0, y: 0, z: 0.008212241}
m_LocalScale: {x: 0.9999999, y: 0.9999999, z: 0.9999999}
m_ConstrainProportionsScale: 0
@ -243,10 +250,17 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
bones:
femur: {fileID: 976560727556484423}
tibia: {fileID: 3873135409852464941}
tarsus: {fileID: 3355004770557811387}
_coxa: {fileID: 0}
_femur: {fileID: 976560727556484423}
_tibia: {fileID: 3873135409852464941}
_tarsus: {fileID: 3355004770557811387}
_phalanges: {fileID: 0}
_end: {fileID: 0}
_femurLength: 0.003806679
_tibiaLength: 0.0060429377
_tarsusLength: 0
_phalangesLength: 0
side: 0
target: {fileID: 2041764967651498327}
targetToBoneFemur: {x: 0, y: 0, z: 0, w: 0}
targetToBoneTibia: {x: 0, y: 0, z: 0, w: 0}
@ -511,10 +525,17 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
bones:
femur: {fileID: 7879963364189984297}
tibia: {fileID: 8557148899078362646}
tarsus: {fileID: 2520372565419969361}
_coxa: {fileID: 0}
_femur: {fileID: 7879963364189984297}
_tibia: {fileID: 8557148899078362646}
_tarsus: {fileID: 2520372565419969361}
_phalanges: {fileID: 0}
_end: {fileID: 0}
_femurLength: 0.006008031
_tibiaLength: 0.008482452
_tarsusLength: 0
_phalangesLength: 0
side: 0
target: {fileID: 5692380185316106944}
targetToBoneFemur: {x: 0, y: 0, z: 0, w: 0}
targetToBoneTibia: {x: 0, y: 0, z: 0, w: 0}
@ -1175,7 +1196,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 5930454242050132924}
serializedVersion: 2
m_LocalRotation: {x: 0.07720134, y: 0.9225222, z: 0.25537333, w: -0.27888584}
m_LocalRotation: {x: 0.07720129, y: 0.92252225, z: 0.25537315, w: -0.27888587}
m_LocalPosition: {x: -0.002, y: 0.004, z: -0.006}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
@ -1196,10 +1217,17 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
bones:
femur: {fileID: 1138231466888029713}
tibia: {fileID: 6229249450306020558}
tarsus: {fileID: 3473835942814004862}
_coxa: {fileID: 0}
_femur: {fileID: 1138231466888029713}
_tibia: {fileID: 6229249450306020558}
_tarsus: {fileID: 3473835942814004862}
_phalanges: {fileID: 0}
_end: {fileID: 0}
_femurLength: 0.0031432603
_tibiaLength: 0.008212241
_tarsusLength: 0
_phalangesLength: 0
side: 0
target: {fileID: 2714894253296331867}
targetToBoneFemur: {x: 0, y: 0, z: 0, w: 0}
targetToBoneTibia: {x: 0, y: 0, z: 0, w: 0}
@ -1313,10 +1341,17 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
bones:
femur: {fileID: 5158943779127512027}
tibia: {fileID: 5102382635668602733}
tarsus: {fileID: 5832494694235077857}
_coxa: {fileID: 0}
_femur: {fileID: 5158943779127512027}
_tibia: {fileID: 5102382635668602733}
_tarsus: {fileID: 5832494694235077857}
_phalanges: {fileID: 0}
_end: {fileID: 0}
_femurLength: 0.004816545
_tibiaLength: 0.008259119
_tarsusLength: 0
_phalangesLength: 0
side: 0
target: {fileID: 8660658904332009209}
targetToBoneFemur: {x: 0, y: 0, z: 0, w: 0}
targetToBoneTibia: {x: 0, y: 0, z: 0, w: 0}
@ -1722,7 +1757,7 @@ Transform:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 8534431202702769095}
serializedVersion: 2
m_LocalRotation: {x: 0.6024823, y: 0.00000008940697, z: 0.000000052154064, w: 0.79813224}
m_LocalRotation: {x: 0.6024822, y: 0.000000074505806, z: 0.000000044703484, w: 0.7981324}
m_LocalPosition: {x: 0, y: 0, z: 0.00314326}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
@ -1881,10 +1916,17 @@ MonoBehaviour:
m_Name:
m_EditorClassIdentifier:
bones:
femur: {fileID: 7827425323228862897}
tibia: {fileID: 8607306932134183899}
tarsus: {fileID: 5350519918537018444}
_coxa: {fileID: 0}
_femur: {fileID: 7827425323228862897}
_tibia: {fileID: 8607306932134183899}
_tarsus: {fileID: 5350519918537018444}
_phalanges: {fileID: 0}
_end: {fileID: 0}
_femurLength: 0.00380668
_tibiaLength: 0.005963828
_tarsusLength: 0
_phalangesLength: 0
side: 0
target: {fileID: 5063380583403966759}
targetToBoneFemur: {x: 0, y: 0, z: 0, w: 0}
targetToBoneTibia: {x: 0, y: 0, z: 0, w: 0}

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 49b4f3c47b71346f2be821320af94de8
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,59 @@
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!1001 &7296245778777971897
PrefabInstance:
m_ObjectHideFlags: 0
serializedVersion: 2
m_Modification:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: -8679921383154817045, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_LocalPosition.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_LocalPosition.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_LocalPosition.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_LocalRotation.w
value: 1
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_LocalRotation.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_LocalRotation.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_LocalRotation.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_LocalEulerAnglesHint.x
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_LocalEulerAnglesHint.y
value: 0
objectReference: {fileID: 0}
- target: {fileID: -8679921383154817045, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_LocalEulerAnglesHint.z
value: 0
objectReference: {fileID: 0}
- target: {fileID: 919132149155446097, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}
propertyPath: m_Name
value: LowPolyAntRigged
objectReference: {fileID: 0}
m_RemovedComponents: []
m_RemovedGameObjects: []
m_AddedGameObjects: []
m_AddedComponents: []
m_SourcePrefab: {fileID: 100100000, guid: 0de805ada00d79cef8575c1c8977d331, type: 3}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: c5354e27a3702dfa6838172d55d2ea6f
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant: