Skip to content

Commit f63b5b6

Browse files
committed
Attachment node component for auto generating attach nodes easier :3
1 parent 6d528b6 commit f63b5b6

File tree

5 files changed

+207
-47
lines changed

5 files changed

+207
-47
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System;
2+
using KSP.Sim;
3+
using KSP.Sim.Definitions;
4+
using UnityEngine;
5+
using UnityEngine.Serialization;
6+
7+
namespace KSP2UT.KSP2UnityTools
8+
{
9+
public class AttachmentNode : MonoBehaviour
10+
{
11+
[Tooltip("Optional field that can be used to group nodes together, eg. 2 downward facing nodes grouped into a 'bottom' group. The group ID would be the same on both nodes. Empty means no group, which is default behavior.")]
12+
public string nodeSymmetryGroupID;
13+
public AttachNodeType nodeType;
14+
public AttachNodeMethod attachMethod;
15+
public bool isMultiJoint;
16+
public int multiJointMaxJoint;
17+
public float multiJointRadiusOffset;
18+
public int size;
19+
public float visualSize;
20+
public bool isResourceCrossFeed;
21+
public bool isRigid;
22+
public float angularStrengthMultiplier;
23+
public float contactArea;
24+
public float overrideDragArea;
25+
public bool isCompoundJoint;
26+
27+
28+
}
29+
}

Assets/KSP2UnityTools/AttachmentNode.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/KSP2UnityTools/Editor/PartEditor.cs

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,11 @@
1-

2-
using System;
3-
using System.Collections.Generic;
4-
using System.IO;
5-
using System.Linq;
1+
using System.IO;
62
using System.Reflection;
73
using KSP;
84
using UnityEditor;
9-
using Cheese.Extensions;
105
using KSP.IO;
116
using KSP.Modules;
127
using KSP.Sim.Definitions;
13-
using KSP.Sim.impl;
14-
using Newtonsoft.Json;
15-
using Newtonsoft.Json.UnityConverters;
16-
using Newtonsoft.Json.UnityConverters.Configuration;
17-
using UnityEditor.VersionControl;
8+
using KSP2UT.KSP2UnityTools;
189
using UnityEngine;
1910

2011
[CustomEditor(typeof(CorePartData))]
@@ -51,6 +42,40 @@ private void OnSceneGUI()
5142
public override void OnInspectorGUI()
5243
{
5344
base.OnInspectorGUI();
45+
GUILayout.Label("Attach Node Settings");
46+
if (GUILayout.Button("Auto Generate AttachNodes"))
47+
{
48+
49+
TargetCore.data.attachNodes.Clear();
50+
// Attach node naming scheme
51+
foreach (var attachmentNode in TargetObject.GetComponentsInChildren<AttachmentNode>())
52+
{
53+
var obj = attachmentNode.gameObject;
54+
var pos = TargetObject.transform.InverseTransformPoint(obj.transform.position);
55+
var dir = Quaternion.Euler(TargetObject.transform.InverseTransformDirection(obj.transform.rotation.eulerAngles)) * Vector3.forward;
56+
var newDefinition = new AttachNodeDefinition
57+
{
58+
nodeID = obj.name,
59+
NodeSymmetryGroupID = attachmentNode.nodeSymmetryGroupID,
60+
nodeType = attachmentNode.nodeType,
61+
attachMethod = attachmentNode.attachMethod,
62+
IsMultiJoint = attachmentNode.isMultiJoint,
63+
MultiJointMaxJoint = attachmentNode.multiJointMaxJoint,
64+
MultiJointRadiusOffset = attachmentNode.multiJointRadiusOffset,
65+
position = pos,
66+
orientation = dir,
67+
size = attachmentNode.size,
68+
visualSize = attachmentNode.visualSize,
69+
angularStrengthMultiplier = attachmentNode.angularStrengthMultiplier,
70+
contactArea = attachmentNode.contactArea,
71+
overrideDragArea = attachmentNode.overrideDragArea,
72+
isCompoundJoint = attachmentNode.isCompoundJoint
73+
};
74+
TargetCore.data.attachNodes.Add(newDefinition);
75+
}
76+
EditorUtility.SetDirty(target);
77+
}
78+
5479
GUILayout.Label("Gizmo Settings", EditorStyles.boldLabel);
5580
EditorGUI.BeginChangeCheck();
5681
_centerOfMassGizmos = EditorGUILayout.Toggle("CoM gizmos", _centerOfMassGizmos);
@@ -117,4 +142,14 @@ public static void DrawGizmoForPartCoreData(CorePartData data, GizmoType gizmoTy
117142
Gizmos.DrawSphere(pos,0.05f);
118143
}
119144
}
145+
146+
[DrawGizmo(GizmoType.Active | GizmoType.Selected)]
147+
public static void DrawGizmoForAttachmentNode(AttachmentNode node, GizmoType gizmoType)
148+
{
149+
if (!_attachNodeGizmos) return;
150+
Gizmos.color = new Color(Color.green.r, Color.green.g, Color.green.b, 0.5f);
151+
var pos = node.transform.position;
152+
Gizmos.DrawRay(pos, node.transform.rotation * Vector3.forward * 0.25f);
153+
Gizmos.DrawSphere(pos,0.05f);
154+
}
120155
}

Assets/KSP2UnityTools/game.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
"rootNamespace": "",
44
"references": [],
55
"includePlatforms": [
6-
"Editor"
6+
"Editor",
7+
"WindowsStandalone64"
78
],
89
"excludePlatforms": [],
910
"allowUnsafeCode": false,

Assets/Scenes/SampleScene.unity

Lines changed: 127 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,63 @@ Transform:
349349
m_Father: {fileID: 0}
350350
m_RootOrder: 1
351351
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
352+
--- !u!1 &759798002
353+
GameObject:
354+
m_ObjectHideFlags: 0
355+
m_CorrespondingSourceObject: {fileID: 0}
356+
m_PrefabInstance: {fileID: 0}
357+
m_PrefabAsset: {fileID: 0}
358+
serializedVersion: 6
359+
m_Component:
360+
- component: {fileID: 759798003}
361+
- component: {fileID: 759798004}
362+
m_Layer: 0
363+
m_Name: top
364+
m_TagString: Untagged
365+
m_Icon: {fileID: 0}
366+
m_NavMeshLayer: 0
367+
m_StaticEditorFlags: 0
368+
m_IsActive: 1
369+
--- !u!4 &759798003
370+
Transform:
371+
m_ObjectHideFlags: 0
372+
m_CorrespondingSourceObject: {fileID: 0}
373+
m_PrefabInstance: {fileID: 0}
374+
m_PrefabAsset: {fileID: 0}
375+
m_GameObject: {fileID: 759798002}
376+
m_LocalRotation: {x: -0.7071068, y: 0, z: 0, w: 0.7071068}
377+
m_LocalPosition: {x: 0, y: 0.212, z: 0}
378+
m_LocalScale: {x: 1, y: 1, z: 1}
379+
m_Children: []
380+
m_Father: {fileID: 2045490028}
381+
m_RootOrder: 0
382+
m_LocalEulerAnglesHint: {x: -90, y: 0, z: 0}
383+
--- !u!114 &759798004
384+
MonoBehaviour:
385+
m_ObjectHideFlags: 0
386+
m_CorrespondingSourceObject: {fileID: 0}
387+
m_PrefabInstance: {fileID: 0}
388+
m_PrefabAsset: {fileID: 0}
389+
m_GameObject: {fileID: 759798002}
390+
m_Enabled: 1
391+
m_EditorHideFlags: 0
392+
m_Script: {fileID: 11500000, guid: bbdaa90d5d8f4373a3aea2f46737d90a, type: 3}
393+
m_Name:
394+
m_EditorClassIdentifier:
395+
nodeSymmetryGroupID:
396+
nodeType: 0
397+
attachMethod: 0
398+
isMultiJoint: 0
399+
multiJointMaxJoint: 0
400+
multiJointRadiusOffset: 0
401+
size: 0
402+
visualSize: 0
403+
isResourceCrossFeed: 1
404+
isRigid: 0
405+
angularStrengthMultiplier: 0
406+
contactArea: 0
407+
overrideDragArea: 0
408+
isCompoundJoint: 0
352409
--- !u!1 &963194225
353410
GameObject:
354411
m_ObjectHideFlags: 0
@@ -531,6 +588,63 @@ Transform:
531588
m_Father: {fileID: 0}
532589
m_RootOrder: 2
533590
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
591+
--- !u!1 &1502141082
592+
GameObject:
593+
m_ObjectHideFlags: 0
594+
m_CorrespondingSourceObject: {fileID: 0}
595+
m_PrefabInstance: {fileID: 0}
596+
m_PrefabAsset: {fileID: 0}
597+
serializedVersion: 6
598+
m_Component:
599+
- component: {fileID: 1502141083}
600+
- component: {fileID: 1502141084}
601+
m_Layer: 0
602+
m_Name: bottom
603+
m_TagString: Untagged
604+
m_Icon: {fileID: 0}
605+
m_NavMeshLayer: 0
606+
m_StaticEditorFlags: 0
607+
m_IsActive: 1
608+
--- !u!4 &1502141083
609+
Transform:
610+
m_ObjectHideFlags: 0
611+
m_CorrespondingSourceObject: {fileID: 0}
612+
m_PrefabInstance: {fileID: 0}
613+
m_PrefabAsset: {fileID: 0}
614+
m_GameObject: {fileID: 1502141082}
615+
m_LocalRotation: {x: 0.7071068, y: 0, z: 0, w: 0.7071068}
616+
m_LocalPosition: {x: 0, y: -0.212, z: 0}
617+
m_LocalScale: {x: 1, y: 1, z: 1}
618+
m_Children: []
619+
m_Father: {fileID: 2045490028}
620+
m_RootOrder: 1
621+
m_LocalEulerAnglesHint: {x: 90, y: 0, z: 0}
622+
--- !u!114 &1502141084
623+
MonoBehaviour:
624+
m_ObjectHideFlags: 0
625+
m_CorrespondingSourceObject: {fileID: 0}
626+
m_PrefabInstance: {fileID: 0}
627+
m_PrefabAsset: {fileID: 0}
628+
m_GameObject: {fileID: 1502141082}
629+
m_Enabled: 1
630+
m_EditorHideFlags: 0
631+
m_Script: {fileID: 11500000, guid: bbdaa90d5d8f4373a3aea2f46737d90a, type: 3}
632+
m_Name:
633+
m_EditorClassIdentifier:
634+
nodeSymmetryGroupID:
635+
nodeType: 0
636+
attachMethod: 0
637+
isMultiJoint: 0
638+
multiJointMaxJoint: 0
639+
multiJointRadiusOffset: 0
640+
size: 0
641+
visualSize: 0
642+
isResourceCrossFeed: 1
643+
isRigid: 0
644+
angularStrengthMultiplier: 0
645+
contactArea: 0
646+
overrideDragArea: 0
647+
isCompoundJoint: 0
534648
--- !u!1 &1726311524
535649
GameObject:
536650
m_ObjectHideFlags: 0
@@ -659,7 +773,9 @@ Transform:
659773
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
660774
m_LocalPosition: {x: 0, y: 0, z: 0}
661775
m_LocalScale: {x: 1, y: 1, z: 1}
662-
m_Children: []
776+
m_Children:
777+
- {fileID: 759798003}
778+
- {fileID: 1502141083}
663779
m_Father: {fileID: 0}
664780
m_RootOrder: 4
665781
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
@@ -1125,8 +1241,7 @@ MonoBehaviour:
11251241
_engineModeChangeAnimationTriggerParamName: tChangeEngineMode
11261242
_engineModeIndexAnimationParamName: iEngineMode
11271243
FlameoutVFX: {fileID: 0}
1128-
ThrustTransforms:
1129-
- {fileID: 0}
1244+
ThrustTransforms: []
11301245
ThrustTransformMultipliers: []
11311246
--- !u!114 &2045490033
11321247
MonoBehaviour:
@@ -1230,13 +1345,13 @@ MonoBehaviour:
12301345
z: 0
12311346
orientation:
12321347
x: 0
1233-
y: 1
1234-
z: 0
1348+
y: 0.9999999403953552
1349+
z: 0.00000005960464477539063
12351350
size: 0
12361351
visualSize: 0
1237-
isResourceCrossfeed: 1
1352+
isResourceCrossfeed: 0
12381353
isRigid: 0
1239-
angularStrengthMultiplier: 1
1354+
angularStrengthMultiplier: 0
12401355
contactArea: 0
12411356
overrideDragArea: 0
12421357
isCompoundJoint: 0
@@ -1249,40 +1364,17 @@ MonoBehaviour:
12491364
MultiJointRadiusOffset: 0
12501365
position:
12511366
x: 0
1252-
y: -0.18199999630451202
1253-
z: 0
1254-
orientation:
1255-
x: 0
1256-
y: -1
1257-
z: 0
1258-
size: 0
1259-
visualSize: 0
1260-
isResourceCrossfeed: 1
1261-
isRigid: 0
1262-
angularStrengthMultiplier: 1
1263-
contactArea: 0
1264-
overrideDragArea: 0
1265-
isCompoundJoint: 0
1266-
- nodeID: srfAttach
1267-
NodeSymmetryGroupID:
1268-
nodeType: 1
1269-
attachMethod: 1
1270-
IsMultiJoint: 0
1271-
MultiJointMaxJoint: 0
1272-
MultiJointRadiusOffset: 0
1273-
position:
1274-
x: 0
1275-
y: 0.211999997496605
1367+
y: -0.21199999749660492
12761368
z: 0
12771369
orientation:
12781370
x: 0
1279-
y: 1
1280-
z: 0
1371+
y: -0.9999999403953552
1372+
z: 0.00000005960464477539063
12811373
size: 0
12821374
visualSize: 0
1283-
isResourceCrossfeed: 1
1375+
isResourceCrossfeed: 0
12841376
isRigid: 0
1285-
angularStrengthMultiplier: 1
1377+
angularStrengthMultiplier: 0
12861378
contactArea: 0
12871379
overrideDragArea: 0
12881380
isCompoundJoint: 0

0 commit comments

Comments
 (0)