Skip to content

Commit b5336bf

Browse files
committed
interactivity: added new math/quatFromUpForward node, incl. Transform.lookAt and Quat.LookRotation exporter
1 parent 215729e commit b5336bf

5 files changed

Lines changed: 129 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using Unity.VisualScripting;
3+
using UnityEditor;
4+
using UnityEngine;
5+
using UnityGLTF.Interactivity.Schema;
6+
7+
namespace UnityGLTF.Interactivity.VisualScripting.Export
8+
{
9+
public class QuaternionLookRotationUnitExporter : IUnitExporter
10+
{
11+
public Type unitType { get; }
12+
13+
[InitializeOnLoadMethod]
14+
private static void Register()
15+
{
16+
InvokeUnitExport.RegisterInvokeExporter(typeof(Quaternion), nameof(Quaternion.LookRotation), new QuaternionLookRotationUnitExporter());
17+
}
18+
19+
public bool InitializeInteractivityNodes(UnitExporter unitExporter)
20+
{
21+
var unit = unitExporter.unit as InvokeMember;
22+
23+
var node = unitExporter.CreateNode<Math_QuatFromUpForwardNode>();
24+
node.ValueIn(Math_QuatFromUpForwardNode.IdForward).MapToInputPort(unit.valueInputs[0]);
25+
if (unit.valueInputs.Count > 1)
26+
node.ValueIn(Math_QuatFromUpForwardNode.IdUp).MapToInputPort(unit.valueInputs[1]);
27+
else
28+
node.ValueIn(Math_QuatFromUpForwardNode.IdUp).SetValue(Vector3.up);
29+
node.ValueOut(Math_QuatFromUpForwardNode.IdOutValue).MapToPort(unit.result);
30+
31+
return true;
32+
}
33+
}
34+
}

Editor/Scripts/Interactivity/VisualScriptingExport/UnitExporters/Math/QuaternionLookRotationUnitExporter.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.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using UnityEditor;
3+
using UnityEngine;
4+
using UnityGLTF.Interactivity.Export;
5+
using UnityGLTF.Interactivity.Schema;
6+
7+
namespace UnityGLTF.Interactivity.VisualScripting.Export
8+
{
9+
public class Transform_LookAtUnitExport : IUnitExporter
10+
{
11+
public Type unitType { get; }
12+
13+
14+
[InitializeOnLoadMethod]
15+
private static void Register()
16+
{
17+
InvokeUnitExport.RegisterInvokeExporter(typeof(Transform), nameof(Transform.LookAt), new Transform_LookAtUnitExport());
18+
}
19+
20+
public bool InitializeInteractivityNodes(UnitExporter unitExporter)
21+
{
22+
var unit = unitExporter.unit as Unity.VisualScripting.InvokeMember;
23+
24+
/*
25+
* LookAt Overloads:
26+
* (Transform target)
27+
* (Transform target, Vector3 worldUp)
28+
* (Vector3 worldPosition)
29+
* (Vector3 worldPosition, Vector3 worldUp)
30+
*/
31+
32+
ValueInRef worldUpRef = null;
33+
34+
TransformHelpers.GetWorldPosition(unitExporter, out var worldPosSelfTargetRef, out var worldPosSelfRef);
35+
worldPosSelfTargetRef.MapToInputPort(unit.target);
36+
37+
var subNode = unitExporter.CreateNode<Math_SubNode>();
38+
var worldPosInput= subNode.ValueIn("a");
39+
subNode.ValueIn("a").ConnectToSource(worldPosSelfRef);
40+
41+
42+
if (unit.valueInputs.Contains("%target"))
43+
{
44+
// Using target Transform variant
45+
TransformHelpers.GetWorldPosition(unitExporter, out var worldPosTargetRef, out var worldPosRef);
46+
worldPosTargetRef.MapToInputPort(unit.valueInputs["%target"]);
47+
worldPosInput.ConnectToSource(worldPosRef);
48+
}
49+
else
50+
{
51+
// Using world position variant
52+
worldPosInput.MapToInputPort(unit.valueInputs["%worldPosition"]);
53+
}
54+
55+
var normalizeNode = unitExporter.CreateNode<Math_NormalizeNode>();
56+
normalizeNode.FirstValueIn().ConnectToSource(subNode.FirstValueOut());
57+
58+
var lookRotNode = unitExporter.CreateNode<Math_QuatFromUpForwardNode>();
59+
worldUpRef = lookRotNode.ValueIn(Math_QuatFromUpForwardNode.IdUp);
60+
61+
if (unit.valueInputs.Contains("%worldUp"))
62+
worldUpRef.MapToInputPort(unit.valueInputs["%worldUp"]);
63+
else
64+
worldUpRef.SetValue(Vector3.up);
65+
66+
TransformHelpers.SetWorldRotation(unitExporter, out var targetRef, out var setRotationRef, out var setRotationFlowIn, out var setRotationFlowOut);
67+
targetRef.MapToInputPort(unit.target);
68+
setRotationRef.ConnectToSource(lookRotNode.FirstValueOut());
69+
setRotationFlowIn.MapToControlInput(unit.enter);
70+
setRotationFlowOut.MapToControlOutput(unit.exit);
71+
72+
return true;
73+
}
74+
}
75+
}

Editor/Scripts/Interactivity/VisualScriptingExport/UnitExporters/Transform/Transform_LookAtUnitExport.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.

Runtime/Scripts/Interactivity/Schema/Nodes/Math/MathNodes.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,6 +1035,20 @@ public class Math_QuatConjugateNode : GltfInteractivityNodeSchema
10351035
public const string IdOutValue = "value";
10361036
}
10371037

1038+
public class Math_QuatFromUpForwardNode : GltfInteractivityNodeSchema
1039+
{
1040+
public override string Op { get; set; } = "math/quatFromUpForward";
1041+
1042+
[InputSocketDescription(GltfTypes.Float3)]
1043+
public const string IdUp = "up";
1044+
1045+
[InputSocketDescription(GltfTypes.Float3)]
1046+
public const string IdForward = "forward";
1047+
1048+
[OutputSocketDescription(GltfTypes.Float4)]
1049+
public const string IdOutValue = "value";
1050+
}
1051+
10381052
public class Math_QuatMulNode : GltfInteractivityNodeSchema
10391053
{
10401054
public override string Op { get; set; } = "math/quatMul";

0 commit comments

Comments
 (0)