Last active
March 18, 2018 09:18
-
-
Save FleshMobProductions/d1f1e8e0aaaa834b741da7d162b15da4 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using Unity.Jobs; | |
using System; | |
using Unity.Collections; | |
using TrueSync; | |
using TerriFighter; | |
//TS and FP values are from the TrueSync library. If anyone wants to test this functions with datatypes available in C# and Unity.. | |
//.. replace the datatype FP with float, TSQuaternion with Quaternion and TSVector with Vector3 | |
namespace TF2018 | |
{ | |
//Editor tool to run the test functions: | |
//Place this in any folder called "Editor" so that the script is treated as Editor script: https://docs.unity3d.com/Manual/SpecialFolders.html | |
public class EditorJobTools : MonoBehaviour { | |
[MenuItem("TerriFighter/Job Tests")] | |
public static void RunJobTests() | |
{ | |
JobTests.TestAllJobs(); | |
} | |
} | |
[Serializable] | |
public struct HitboxCollisionInfo | |
{ | |
public int hitboxIndex; | |
public int hitboxType; | |
public TSVector collisionPoint; | |
} | |
[Serializable] | |
public struct HitboxCollisionInfoS | |
{ | |
public int hitboxIndex; | |
public int hitboxType; | |
public FP collisionPointX; | |
public FP collisionPointY; | |
public FP collisionPointZ; | |
} | |
[Serializable] | |
public struct HitboxInfoStruct | |
{ | |
public TSVector position; | |
public FP radius; | |
public int idIndex; //for identifying the reference of the source hitbox | |
public int hitboxType; | |
public int assignHitboxToBodypart; //boolean is non blitable!!! use int/byte instead. 0 means false, everything else means true. | |
public int bodyPart; | |
} | |
[Serializable] | |
public struct HitboxInfoStructS | |
{ | |
public int idIndex; //for identifying the reference of the source hitbox | |
public FP positionX; | |
public FP positionY; | |
public FP positionZ; | |
public FP radius; | |
public int hitboxType; | |
public bool assignHitboxToBodypart; | |
public int bodyPart; | |
} | |
public struct JobTestFP : IJobParallelFor | |
{ | |
public NativeArray<FP> fpValues; | |
public NativeArray<FP> result; | |
public TSQuaternion rotation; | |
public void Execute(int index) | |
{ | |
result[index] = fpValues[index] * rotation.x; | |
} | |
} | |
public struct JobTestTSVector : IJobParallelFor | |
{ | |
public NativeArray<TSVector> fpValues; | |
public NativeArray<TSVector> result; | |
public TSQuaternion rotation; | |
public void Execute(int index) | |
{ | |
result[index] = rotation * fpValues[index]; | |
} | |
} | |
public struct JobTestHitboxCollisionInfo : IJobParallelFor | |
{ | |
public NativeArray<HitboxCollisionInfo> fpValues; | |
public NativeArray<HitboxCollisionInfo> result; | |
public TSQuaternion rotation; | |
public void Execute(int index) | |
{ | |
TSVector val = rotation * (new TSVector(4,2,3) * fpValues[index].hitboxIndex); | |
HitboxCollisionInfo info = new HitboxCollisionInfo(); | |
info.collisionPoint = val; | |
info.hitboxIndex = 3; | |
result[index] = info; //cannot assign a vector: we cannot possibly use vectors that way in | |
} | |
} | |
public struct JobTestHitboxCollisionInfoS : IJobParallelFor | |
{ | |
public NativeArray<HitboxCollisionInfoS> fpValues; | |
public NativeArray<HitboxCollisionInfoS> result; | |
public TSQuaternion rotation; | |
public void Execute(int index) | |
{ | |
TSVector val = rotation * (new TSVector(4, 2, 3) * fpValues[index].hitboxIndex); | |
HitboxCollisionInfoS info = new HitboxCollisionInfoS(); | |
info.collisionPointX = val.x; | |
info.collisionPointY = val.y; | |
info.hitboxIndex = 3; | |
result[index] = info; //cannot assign a vector: we cannot possibly use vectors that way in | |
} | |
} | |
public struct JobTestHitboxInfoStruct : IJobParallelFor | |
{ | |
public NativeArray<HitboxInfoStruct> fpValues; | |
public NativeArray<HitboxInfoStruct> result; | |
public TSQuaternion rotation; | |
public void Execute(int index) | |
{ | |
TSVector val = rotation * fpValues[index].position; | |
HitboxInfoStruct info = new HitboxInfoStruct(); | |
info.position = val; | |
info.radius = 3; | |
result[index] = info; //cannot assign a vector: we cannot possibly use vectors that way in | |
} | |
} | |
public struct JobTestHitboxInfoStructS : IJobParallelFor | |
{ | |
public NativeArray<HitboxInfoStructS> fpValues; | |
public NativeArray<HitboxInfoStructS> result; | |
public TSQuaternion rotation; | |
public void Execute(int index) | |
{ | |
TSVector val = new TSVector(fpValues[index].positionX, fpValues[index].positionY, fpValues[index].positionZ); | |
val = rotation * val; | |
HitboxInfoStructS info = new HitboxInfoStructS(); | |
info.positionX = val.x; | |
info.positionY = val.y; | |
info.positionZ = val.z; | |
info.radius = 3; | |
result[index] = info; //cannot assign a vector: we cannot possibly use vectors that way in | |
} | |
} | |
public class JobTests | |
{ | |
private static HitboxInfoStruct CreateTestCollisionInfo() | |
{ | |
HitboxInfoStruct collisionInfo = new HitboxInfoStruct(); | |
return collisionInfo; | |
} | |
public static void TestAllJobs() | |
{ | |
JobTests.ExecuteFPJob(); | |
JobTests.ExecuteTSVectorJob(); | |
JobTests.ExecuteHitboxCollisionInfoJob(); | |
JobTests.ExecuteHitboxCollisionInfoJobS(); | |
JobTests.ExecuteHitboxInfoStructJob(); | |
JobTests.ExecuteHitboxInfoStructJobS(); | |
} | |
//Not blitable - maybe the size of the struct is too much...? | |
public static void ExecuteHitboxInfoStructJobS() | |
{ | |
functionCheck("ExecuteHitboxInfoStructJobS", ExecuteHitboxInfoStructJobSInternal); | |
} | |
private static void ExecuteHitboxInfoStructJobSInternal() | |
{ | |
HitboxInfoStructS[] vals = new HitboxInfoStructS[] { new HitboxInfoStructS(), new HitboxInfoStructS(), new HitboxInfoStructS() }; | |
HitboxInfoStructS[] results = new HitboxInfoStructS[vals.Length]; | |
Debug.Log(StaticFunctions.GetArrayAsString(vals)); | |
JobTestHitboxInfoStructS job = new JobTestHitboxInfoStructS() | |
{ | |
fpValues = new NativeArray<HitboxInfoStructS>(vals, Allocator.TempJob), | |
result = new NativeArray<HitboxInfoStructS>(results.Length, Allocator.TempJob, NativeArrayOptions.ClearMemory), | |
rotation = TSQuaternion.Euler(0, 45, 90) | |
}; | |
var jobHandle = job.Schedule(vals.Length, vals.Length); | |
jobHandle.Complete(); | |
job.result.CopyTo(results); | |
job.fpValues.Dispose(); | |
job.result.Dispose(); | |
Debug.Log(StaticFunctions.GetArrayAsString(results)); | |
} | |
//Error, not blibable.. | |
public static void ExecuteHitboxInfoStructJob() | |
{ | |
functionCheck("ExecuteHitboxInfoStructJob", ExecuteHitboxInfoStructJobInternal); | |
} | |
private static void ExecuteHitboxInfoStructJobInternal() | |
{ | |
HitboxInfoStruct[] vals = new HitboxInfoStruct[] { new HitboxInfoStruct(), new HitboxInfoStruct(), new HitboxInfoStruct() }; | |
HitboxInfoStruct[] results = new HitboxInfoStruct[vals.Length]; | |
Debug.Log(StaticFunctions.GetArrayAsString(vals)); | |
JobTestHitboxInfoStruct job = new JobTestHitboxInfoStruct() | |
{ | |
fpValues = new NativeArray<HitboxInfoStruct>(vals, Allocator.TempJob), | |
result = new NativeArray<HitboxInfoStruct>(results.Length, Allocator.TempJob, NativeArrayOptions.ClearMemory), | |
rotation = TSQuaternion.Euler(0, 45, 90) | |
}; | |
var jobHandle = job.Schedule(vals.Length, vals.Length); | |
jobHandle.Complete(); | |
job.result.CopyTo(results); | |
job.fpValues.Dispose(); | |
job.result.Dispose(); | |
Debug.Log(StaticFunctions.GetArrayAsString(results)); | |
} | |
public static void ExecuteHitboxCollisionInfoJob() | |
{ | |
functionCheck("ExecuteHitboxCollisionInfoJob", ExecuteHitboxCollisionInfoJobInternal); | |
} | |
private static void ExecuteHitboxCollisionInfoJobInternal() | |
{ | |
HitboxCollisionInfo[] vals = new HitboxCollisionInfo[] { new HitboxCollisionInfo(), new HitboxCollisionInfo(), new HitboxCollisionInfo() }; | |
HitboxCollisionInfo[] results = new HitboxCollisionInfo[vals.Length]; | |
Debug.Log(StaticFunctions.GetArrayAsString(vals)); | |
JobTestHitboxCollisionInfo job = new JobTestHitboxCollisionInfo() | |
{ | |
fpValues = new NativeArray<HitboxCollisionInfo>(vals, Allocator.TempJob), | |
result = new NativeArray<HitboxCollisionInfo>(results.Length, Allocator.TempJob, NativeArrayOptions.ClearMemory), | |
rotation = TSQuaternion.Euler(0, 45, 90) | |
}; | |
var jobHandle = job.Schedule(vals.Length, vals.Length); | |
jobHandle.Complete(); | |
job.result.CopyTo(results); | |
job.fpValues.Dispose(); | |
job.result.Dispose(); | |
Debug.Log(StaticFunctions.GetArrayAsString(results)); | |
} | |
public static void ExecuteHitboxCollisionInfoJobS() | |
{ | |
functionCheck("ExecuteHitboxCollisionInfoJobS", ExecuteHitboxCollisionInfoJobInternalS); | |
} | |
private static void ExecuteHitboxCollisionInfoJobInternalS() | |
{ | |
HitboxCollisionInfoS[] vals = new HitboxCollisionInfoS[] { new HitboxCollisionInfoS(), new HitboxCollisionInfoS(), new HitboxCollisionInfoS() }; | |
HitboxCollisionInfoS[] results = new HitboxCollisionInfoS[vals.Length]; | |
Debug.Log(StaticFunctions.GetArrayAsString(vals)); | |
JobTestHitboxCollisionInfoS job = new JobTestHitboxCollisionInfoS() | |
{ | |
fpValues = new NativeArray<HitboxCollisionInfoS>(vals, Allocator.TempJob), | |
result = new NativeArray<HitboxCollisionInfoS>(results.Length, Allocator.TempJob, NativeArrayOptions.ClearMemory), | |
rotation = TSQuaternion.Euler(0, 45, 90) | |
}; | |
var jobHandle = job.Schedule(vals.Length, vals.Length); | |
jobHandle.Complete(); | |
job.result.CopyTo(results); | |
job.fpValues.Dispose(); | |
job.result.Dispose(); | |
Debug.Log(StaticFunctions.GetArrayAsString(results)); | |
} | |
public static void ExecuteFPJob() | |
{ | |
functionCheck("ExecuteFPJob", ExecuteFPJobInternal); | |
} | |
private static void ExecuteFPJobInternal() | |
{ | |
FP[] vals = new FP[] { 1.1, 1.2, 45.34 }; | |
FP[] results = new FP[vals.Length]; | |
Debug.Log(StaticFunctions.GetArrayAsString(vals)); | |
JobTestFP job = new JobTestFP() | |
{ | |
fpValues = new NativeArray<FP>(vals, Allocator.TempJob), | |
result = new NativeArray<FP>(results.Length, Allocator.TempJob, NativeArrayOptions.ClearMemory), | |
rotation = TSQuaternion.Euler(0, 45, 90) | |
}; | |
var jobHandle = job.Schedule(vals.Length, vals.Length); | |
jobHandle.Complete(); | |
job.result.CopyTo(results); | |
job.fpValues.Dispose(); | |
job.result.Dispose(); | |
Debug.Log(StaticFunctions.GetArrayAsString(results)); | |
} | |
public static Action<string, Action> functionCheck = (string functionName, Action actionTeExecute) => { | |
try | |
{ | |
actionTeExecute(); | |
Debug.Log(string.Format("Function {0} executed successfully", functionName)); | |
} catch (Exception e) | |
{ | |
Debug.LogError(string.Format("Function {0} failed: {1}", functionName, e.ToString())); | |
} | |
}; | |
public static void ExecuteTSVectorJob() | |
{ | |
functionCheck("ExecuteTSVectorJob", ExecuteTSVectorJobInternal); | |
} | |
private static void ExecuteTSVectorJobInternal() | |
{ | |
TSVector[] vals = new TSVector[] { new TSVector(1.1, 0.3, 4), new TSVector(1.3, 3.3, 4), new TSVector(1.2, 0.13, 4.5) }; | |
TSVector[] results = new TSVector[vals.Length]; | |
Debug.Log(StaticFunctions.GetArrayAsString(vals)); | |
JobTestTSVector job = new JobTestTSVector() | |
{ | |
fpValues = new NativeArray<TSVector>(vals, Allocator.TempJob), | |
result = new NativeArray<TSVector>(results.Length, Allocator.TempJob, NativeArrayOptions.ClearMemory), | |
rotation = TSQuaternion.Euler(0, 45, 90) | |
}; | |
var jobHandle = job.Schedule(vals.Length, vals.Length); | |
jobHandle.Complete(); | |
job.result.CopyTo(results); | |
job.fpValues.Dispose(); | |
job.result.Dispose(); | |
Debug.Log(StaticFunctions.GetArrayAsString(results)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment