-
-
Notifications
You must be signed in to change notification settings - Fork 307
StandAlone Quality of Life Improvements #310
StandAlone Quality of Life Improvements #310
Conversation
ForgeUnity/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float2.cs
Show resolved
Hide resolved
|
These changes seem related to issue #96 |
Crazy8ball
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolve conflicts please, and @phalasz if you are able to test and verify these changes don't break anything then we can go ahead and merge this. Because it is standalone, we can't use the Vector3,4 etc because that is unity and Standalone should have no reference to unity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Really good work here but there are so many file changes (some of which are unrelated). In the future, I would consider breaking this up into multiple PRs because it would make each improvement easier to test and review.
|
@notjuanortiz > Really good work here but there are so many file changes (some of which are unrelated). In the future, I would consider breaking this up into multiple PRs because it would make each improvement easier to test and review. The file changes were kept to a minimum as all of the files changed were required in order to fully add Float2-4 and the code generation to better support use of standalone, all of the changes go hand in hand and wouldn't make much sense on their own which is why this is a single pr to improve StandAlone. Also, which file changes were unrelated? I looked back through the commits and none of the files changed seemed to be unrelated to my additions and changes listed. |
|
@Crazy8ball |
|
|
||
| namespace BeardedManStudios.Forge.Networking.StandAlone | ||
| { | ||
| public class StandAloneObjectMapper : ObjectMapper |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if it would be good to roll the mapping of these new types into the ObjectMapper itself instead. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see many people needing the use for it as most people will just use unity headless servers which is why I put it in its own object mapper that inherits from the base
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The objectmapper not the unityobject mapper that extends the object mapper. This should be fine too though, I was just wondering if it would make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could it be that if you use Float3 in RPCs, you will get errors whilst trying to send them from a Unity client?
BaseNetworkException: The type BeardedManStudios.Float3 is not allowed to be sent over the Network (yet)
BeardedManStudios.Forge.Networking.ObjectMapper.GetBytesArray (System.Object o, System.Type type) (at Assets/BeardedManStudios/Scripts/Networking/Forge/Networking/ObjectMapper.cs:455)
BeardedManStudios.Forge.Networking.Unity.UnityObjectMapper.GetBytesArray (System.Object o, System.Type type) (at Assets/BeardedManStudios/Scripts/UnityObjectMapper.cs:160)
BeardedManStudios.Forge.Networking.ObjectMapper.MapBytes (BeardedManStudios.BMSByte bytes, System.Object[] args) (at Assets/BeardedManStudios/Scripts/Networking/Forge/Networking/ObjectMapper.cs:217)
BeardedManStudios.Forge.Networking.NetworkObject.SendRpc (BeardedManStudios.Forge.Networking.NetworkingPlayer targetPlayer, System.Byte methodId, System.Boolean replacePrevious, System.Boolean reliable, BeardedManStudios.Forge.Networking.Receivers receivers, BeardedManStudios.Forge.Networking.NetworkingPlayer sender, System.Object[] args) (at Assets/BeardedManStudios/Scripts/Networking/Forge/Networking/Objects/NetworkObject.cs:1245)
BeardedManStudios.Forge.Networking.NetworkObject.SendRpc (System.Byte methodId, BeardedManStudios.Forge.Networking.Receivers receivers, System.Object[] args) (at Assets/BeardedManStudios/Scripts/Networking/Forge/Networking/Objects/NetworkObject.cs:1030)
MyNetworkTest.Update () (at Assets/Scripts/Network/MyNetworkTest.cs:96)
This seems to happen because SendRpc uses ObjectMapper.Instance, which is not an instance of StandAloneObjectMapper. Though I'm wondering how this does work for Vector3, as those are only in the UnityObjectMapper, is the instance perhaps set to an instance of the child class somewhere?
EDIT: Found it, it's UnityObjectMapper.Instance.UseAsDefault();, I think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup you found it, that is how it is in unity land.
|
If this is merged, how would one go about choosing versions of the generated code that work stand-alone? Just select the non-Unity fields and avoid using the Unity ones? Or will multiple versions be generated? Some other things I noticed whilst trying to use the code generated after testing these changes:
EDIT: I figured it out, you have to uncheck "Generate MonoBehavior", or this new code will not be used. Though it appears that you then lose the I'm also wondering at this point if the By introducing "StandAlone" it looks like a user needs to pick between the two, it may not be clear to new users, and there seems little benefit in keeping the Unity types around in the first place. |
| @@ -0,0 +1,37 @@ | |||
| namespace BeardedManStudios | |||
| { | |||
| public partial struct Float2 | |||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A minor nitpick, but it may be interesting to add ToString methods to these new types for easy logging and printing, just like Unity's Vector3 and friends have.
| return ForgeAcceptableFieldTypes.VECTOR3; | ||
| case "vector4": | ||
| return ForgeAcceptableFieldTypes.VECTOR4; | ||
| case "float2": |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't these fields also be marked in IsInterpolatable above as interpolatable, just like Unity's Vector types?
I'm not too familiar with the internals, but an implementation for these in BMSExtensions and BeardedMath may be required, e.g.:
public static bool Near(this Float4 target, Float4 other, float distance)
{
return
Near(target.x, other.x, distance) &&
Near(target.y, other.y, distance) &&
Near(target.z, other.z, distance) &&
Near(target.w, other.w, distance);
}public static Float4 Lerp(Float4 a0, Float4 a1, float t)
{
return new Float4(
Lerp(a0.x, a1.x, t),
Lerp(a0.y, a1.y, t),
Lerp(a0.z, a1.z, t),
Lerp(a0.w, a1.w, t)
);
}Note also that there appears to be a bug (#384) currently that causes interpolation of these new types to break. In any case, I think we can support interpolation here, too. Forge appears to already do this for Vector3 and friends in e.g. InterpolateVector3.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fixed #384 in another pull request, this makes interpolation "work" as in "not break any updates entirely anymore", but not pretty, as there is no actual interpolation happening as is the case for e.g. Vector3. I took a quick stab at this and implementing interpolation seemed to be fairly straightforward.
@DrPotatoNet I can't update this pull request's branch, so could you implement this patch on top of the changes, if this is okay for the maintainers of Forge?
From 21a5f83ca3e275ea88d42e5e50c51edb04cc0d2b Mon Sep 17 00:00:00 2001
From: MrWatts-Anorak <[email protected]>
Date: Mon, 30 Nov 2020 12:55:37 +0100
Subject: [PATCH] Fix interpolation not working for new Float types
There were multiple bugs here: interpolation was simply not implemented
for the new FloatX types, and falling back to a generic version. This
generic version also didn't work [1].
See also https://github.com/BeardedManStudios/ForgeNetworkingRemastered/pull/310#discussion_r532443679
[1] https://github.com/BeardedManStudios/ForgeNetworkingRemastered/issues/384
---
.../Editor/ForgeClassFieldValue.cs | 12 ++++++
.../Scripts/Networking/BMSExtensions.cs | 15 +++++++
.../Scripts/StandAlone/Objects/Float2.cs | 20 +++++++++-
.../Scripts/StandAlone/Objects/Float3.cs | 22 +++++++++-
.../Scripts/StandAlone/Objects/Float4.cs | 24 ++++++++++-
.../StandAlone/Objects/InterpolateFloat2.cs | 40 +++++++++++++++++++
.../Objects/InterpolateFloat2.cs.meta | 11 +++++
.../StandAlone/Objects/InterpolateFloat3.cs | 19 +++++++++
.../Objects/InterpolateFloat3.cs.meta | 11 +++++
.../StandAlone/Objects/InterpolateFloat4.cs | 19 +++++++++
.../Objects/InterpolateFloat4.cs.meta | 11 +++++
11 files changed, 201 insertions(+), 3 deletions(-)
create mode 100644 unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat2.cs
create mode 100644 unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat2.cs.meta
create mode 100644 unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat3.cs
create mode 100644 unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat3.cs.meta
create mode 100644 unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat4.cs
create mode 100644 unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat4.cs.meta
diff --git a/unity-wade/Assets/BeardedManStudios/Editor/ForgeClassFieldValue.cs b/unity-wade/Assets/BeardedManStudios/Editor/ForgeClassFieldValue.cs
index b5230f0..7375ed8 100644
--- a/unity-wade/Assets/BeardedManStudios/Editor/ForgeClassFieldValue.cs
+++ b/unity-wade/Assets/BeardedManStudios/Editor/ForgeClassFieldValue.cs
@@ -170,6 +170,15 @@ namespace BeardedManStudios.Forge.Networking.UnityEditor
case ForgeAcceptableFieldTypes.QUATERNION:
returnValue = "InterpolateQuaternion";
break;
+ case ForgeAcceptableFieldTypes.FLOAT2:
+ returnValue = "InterpolateFloat2";
+ break;
+ case ForgeAcceptableFieldTypes.FLOAT3:
+ returnValue = "InterpolateFloat3";
+ break;
+ case ForgeAcceptableFieldTypes.FLOAT4:
+ returnValue = "InterpolateFloat4";
+ break;
default:
returnValue = "Interpolated<" + baseTypeString + ">";
break;
@@ -188,6 +197,9 @@ namespace BeardedManStudios.Forge.Networking.UnityEditor
case ForgeAcceptableFieldTypes.VECTOR2:
case ForgeAcceptableFieldTypes.VECTOR3:
case ForgeAcceptableFieldTypes.VECTOR4:
+ case ForgeAcceptableFieldTypes.FLOAT2:
+ case ForgeAcceptableFieldTypes.FLOAT3:
+ case ForgeAcceptableFieldTypes.FLOAT4:
case ForgeAcceptableFieldTypes.QUATERNION:
returnValue = true;
break;
diff --git a/unity-wade/Assets/BeardedManStudios/Scripts/Networking/BMSExtensions.cs b/unity-wade/Assets/BeardedManStudios/Scripts/Networking/BMSExtensions.cs
index efc7b74..560e818 100644
--- a/unity-wade/Assets/BeardedManStudios/Scripts/Networking/BMSExtensions.cs
+++ b/unity-wade/Assets/BeardedManStudios/Scripts/Networking/BMSExtensions.cs
@@ -57,6 +57,21 @@
return target.Between(other - distance, other + distance);
}
+ public static bool Near(this Float2 target, Float2 other, float threshold = 0.0012f)
+ {
+ return Float2.Distance(target, other) <= threshold;
+ }
+
+ public static bool Near(this Float3 target, Float3 other, float threshold = 0.0012f)
+ {
+ return Float3.Distance(target, other) <= threshold;
+ }
+
+ public static bool Near(this Float4 target, Float4 other, float threshold = 0.0012f)
+ {
+ return Float4.Distance(target, other) <= threshold;
+ }
+
public static bool Near<T>(this T target, T other, float distance)
{
return target.Equals(other);
diff --git a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float2.cs b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float2.cs
index 36ca8c2..bd04eeb 100644
--- a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float2.cs
+++ b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float2.cs
@@ -1,4 +1,6 @@
-namespace BeardedManStudios
+using System;
+
+namespace BeardedManStudios
{
public partial struct Float2
{
@@ -33,5 +35,21 @@
{
return base.GetHashCode();
}
+
+ public static Float2 Lerp(Float2 a0, Float2 a1, float t)
+ {
+ return new Float2(
+ BeardedMath.Lerp(a0.x, a1.x, t),
+ BeardedMath.Lerp(a0.y, a1.y, t)
+ );
+ }
+
+ public static float Distance(Float2 a, Float2 b)
+ {
+ return (float)Math.Sqrt((double)(
+ Math.Pow(a.x - b.x, 2) +
+ Math.Pow(a.y - b.y, 2)
+ ));
+ }
}
}
diff --git a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float3.cs b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float3.cs
index 315f7b6..1670ab2 100644
--- a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float3.cs
+++ b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float3.cs
@@ -1,4 +1,6 @@
-namespace BeardedManStudios
+using System;
+
+namespace BeardedManStudios
{
public partial struct Float3
{
@@ -34,5 +36,23 @@
{
return base.GetHashCode();
}
+
+ public static Float3 Lerp(Float3 a0, Float3 a1, float t)
+ {
+ return new Float3(
+ BeardedMath.Lerp(a0.x, a1.x, t),
+ BeardedMath.Lerp(a0.y, a1.y, t),
+ BeardedMath.Lerp(a0.z, a1.z, t)
+ );
+ }
+
+ public static float Distance(Float3 a, Float3 b)
+ {
+ return (float)Math.Sqrt((double)(
+ Math.Pow(a.x - b.x, 2) +
+ Math.Pow(a.y - b.y, 2) +
+ Math.Pow(a.z - b.z, 2)
+ ));
+ }
}
}
diff --git a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float4.cs b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float4.cs
index 2fe927e..d9bb279 100644
--- a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float4.cs
+++ b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/Float4.cs
@@ -1,4 +1,6 @@
-namespace BeardedManStudios
+using System;
+
+namespace BeardedManStudios
{
public partial struct Float4
{
@@ -35,5 +37,25 @@
{
return base.GetHashCode();
}
+
+ public static Float4 Lerp(Float4 a0, Float4 a1, float t)
+ {
+ return new Float4(
+ BeardedMath.Lerp(a0.x, a1.x, t),
+ BeardedMath.Lerp(a0.y, a1.y, t),
+ BeardedMath.Lerp(a0.z, a1.z, t),
+ BeardedMath.Lerp(a0.w, a1.w, t)
+ );
+ }
+
+ public static float Distance(Float4 a, Float4 b)
+ {
+ return (float)Math.Sqrt((double)(
+ Math.Pow(a.x - b.x, 2) +
+ Math.Pow(a.y - b.y, 2) +
+ Math.Pow(a.z - b.z, 2) +
+ Math.Pow(a.w - b.w, 2)
+ ));
+ }
}
}
diff --git a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat2.cs b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat2.cs
new file mode 100644
index 0000000..5155500
--- /dev/null
+++ b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat2.cs
@@ -0,0 +1,40 @@
+/*-----------------------------+------------------------------\
+| |
+| !!!NOTICE!!! |
+| |
+| These libraries are under heavy development so they are |
+| subject to make many changes as development continues. |
+| For this reason, the libraries may not be well commented. |
+| THANK YOU for supporting forge with all your feedback |
+| suggestions, bug reports and comments! |
+| |
+| - The Forge Team |
+| Bearded Man Studios, Inc. |
+| |
+| This source code, project files, and associated files are |
+| copyrighted by Bearded Man Studios, Inc. (2012-2015) and |
+| may not be redistributed without written permission. |
+| |
+\------------------------------+-----------------------------*/
+
+using BeardedManStudios.Forge.Networking;
+
+namespace BeardedManStudios
+{
+ public struct InterpolateFloat2 : IInterpolator<Float2>
+ {
+ public Float2 current;
+ public Float2 target;
+ public float LerpT { get; set; }
+ public bool Enabled { get; set; }
+ public ulong Timestep { get; set; }
+
+ public Float2 Interpolate()
+ {
+ if (!Enabled) return target;
+
+ current = Float2.Lerp(current, target, LerpT);
+ return current;
+ }
+ }
+}
\ No newline at end of file
diff --git a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat2.cs.meta b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat2.cs.meta
new file mode 100644
index 0000000..f17feab
--- /dev/null
+++ b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat2.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 5d8f29f9be70c3a4e9c81a969eca280c
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat3.cs b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat3.cs
new file mode 100644
index 0000000..6599612
--- /dev/null
+++ b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat3.cs
@@ -0,0 +1,21 @@
+using BeardedManStudios.Forge.Networking;
+
+namespace BeardedManStudios
+{
+ public struct InterpolateFloat3 : IInterpolator<Float3>
+ {
+ public Float3 current;
+ public Float3 target;
+ public float LerpT { get; set; }
+ public bool Enabled { get; set; }
+ public ulong Timestep { get; set; }
+
+ public Float3 Interpolate()
+ {
+ if (!Enabled) return target;
+
+ current = Float3.Lerp(current, target, LerpT);
+ return current;
+ }
+ }
+}
\ No newline at end of file
diff --git a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat3.cs.meta b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat3.cs.meta
new file mode 100644
index 0000000..0ad8ee4
--- /dev/null
+++ b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat3.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 57d00189c59006d48a08c6c9ed915b29
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat4.cs b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat4.cs
new file mode 100644
index 0000000..28622e0
--- /dev/null
+++ b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat4.cs
@@ -0,0 +1,21 @@
+using BeardedManStudios.Forge.Networking;
+
+namespace BeardedManStudios
+{
+ public struct InterpolateFloat4 : IInterpolator<Float4>
+ {
+ public Float4 current;
+ public Float4 target;
+ public float LerpT { get; set; }
+ public bool Enabled { get; set; }
+ public ulong Timestep { get; set; }
+
+ public Float4 Interpolate()
+ {
+ if (!Enabled) return target;
+
+ current = Float4.Lerp(current, target, LerpT);
+ return current;
+ }
+ }
+}
\ No newline at end of file
diff --git a/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat4.cs.meta b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat4.cs.meta
new file mode 100644
index 0000000..d6b9aee
--- /dev/null
+++ b/unity-wade/Assets/BeardedManStudios/Scripts/StandAlone/Objects/InterpolateFloat4.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 77b17d7f89db5bb4bb8038dfaa3b6da9
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
--
2.28.0.windows.1|
I am no longer interested in perusing this as I no longer use Forge. |
|
@DrPotatoNet That's fine, thanks for letting us know and your efforts. I may look into applying your patches and mine on a new branch and creating a separate pull request. EDIT: Done in #393. |
These are more of a quality of life changes for those who use the StandAlone Server.
Changes as follows:
-Added StandAloneNetworkObjectFactory for use with StandAlone Server
-Added Float2, Float3, and Float4 as substitutes for Unity's Vector2, Vector3, and Vector4 (Color & Quaternion) for use with the StandAlone Server with operators for converting between them
-Added code generation for Non-Monobehavior NetworkObjects