Skip to content

Commit

Permalink
added oscv3 and pleiades protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
joreg committed Nov 21, 2023
1 parent a6d3d10 commit e8fc6a0
Show file tree
Hide file tree
Showing 10 changed files with 4,118 additions and 907 deletions.
2,207 changes: 1,727 additions & 480 deletions VL.Augmenta.vl

Large diffs are not rendered by default.

906 changes: 906 additions & 0 deletions help/How To Receive Augmenta Pleiades data.vl

Large diffs are not rendered by default.

610 changes: 610 additions & 0 deletions help/HowTo Receive Augmenta data via OSCv3.vl

Large diffs are not rendered by default.

Large diffs are not rendered by default.

55 changes: 55 additions & 0 deletions help/shaders/DrawColoredParticles_DrawFX.sdsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@

shader DrawColoredParticles_DrawFX : VS_PS_Base, ShaderUtils
{
StructuredBuffer<float4> ParticlesBuffer;
StructuredBuffer<float4> ColorBuffer;

cbuffer PerFrame
{
[Color]
float ParticleSize;
};

stream float2 TexCoord;
stream uint VertexID : SV_VertexID;
stream float4 Color;

// VS ==============================================================================

stage override void VSMain()
{
uint id = streams.VertexID;
streams.PositionWS = ParticlesBuffer[id];
streams.Color = ColorBuffer[id];
}

// GS ==============================================================================

stream float Size;

[maxvertexcount(4)]
stage void GSMain(point Input input[1], inout TriangleStream<Output> triangleStream)
{
streams = input[0];

for(int i=0; i<4; i++)
{
streams.TexCoord = QuadUV[i].xy;

float4 posView = mul(streams.PositionWS, WorldView);
posView.xyz += QuadPositions[i].xyz * ParticleSize;
streams.ShadingPosition = mul(posView, Projection);

triangleStream.Append(streams);
}
}


// PS ==============================================================================

stage override void PSMain()
{
CircleSpriteDiscard(streams.TexCoord);
streams.ColorTarget = streams.Color;
}
};
10 changes: 10 additions & 0 deletions src/Augmenta.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<BaseOutputPath>..\lib</BaseOutputPath>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions src/Augmenta.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.7.34031.279
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Augmenta", "Augmenta.csproj", "{BEE32E87-4D44-4F15-B01E-46C5E7C13739}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{BEE32E87-4D44-4F15-B01E-46C5E7C13739}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BEE32E87-4D44-4F15-B01E-46C5E7C13739}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BEE32E87-4D44-4F15-B01E-46C5E7C13739}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BEE32E87-4D44-4F15-B01E-46C5E7C13739}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3B490D63-6A8B-4B11-BD53-B6524964E53D}
EndGlobalSection
EndGlobal
170 changes: 170 additions & 0 deletions src/PObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
using System.Numerics;
using System.Runtime.CompilerServices;

namespace Augmenta
{
public class PObject
{
public int objectID;
private Vector3[] pointsA = new Vector3[0];
private int pointCount;
public ReadOnlyMemory<Vector3> points => new ReadOnlyMemory<Vector3>(pointsA, 0, pointCount);

public Matrix4x4 transform;
internal Matrix4x4 parentTransform = Matrix4x4.Identity;

//cluster
public enum State { Enter = 0, Update = 1, Leave = 2, Ghost = 3 };
public State state;

public Vector3 centroid;
public Vector3 velocity;
public Vector3 minBounds;
public Vector3 maxBounds;
//[Range(0, 1)]
public float weight;

float lastUpdateTime;

public float killDelayTime = 0;

public float timeSinceGhost;
public bool drawDebug;

public enum PositionUpdateMode { None, Centroid, BoxCenter }
public PositionUpdateMode posUpdateMode = PositionUpdateMode.Centroid;

public enum CoordMode { Absolute, Relative }
public CoordMode pointMode = CoordMode.Relative;

public delegate void OnRemoveEvent(PObject obj);
public event OnRemoveEvent onRemove;


// Update is called once per frame
internal void Update(float time)
{
if (time - lastUpdateTime > .5f)
timeSinceGhost = time;
else
timeSinceGhost = -1;
}

public void updateData(float time, Span<byte> data, int offset)
{
var pos = offset + 1 + 2 * sizeof(int); //packet type (1) + packet size (4) + objectID (4)
while (pos < data.Length)
{
var propertyID = Utils.ReadInt(data, pos);
var propertySize = Utils.ReadInt(data, pos + sizeof(int));

if (propertySize < 0)
{
//Debug.LogWarning("Error : property size < 0");
break;
}

switch (propertyID)
{
case 0: updatePointsData(data, pos + 2 * sizeof(int)); break;
case 1: updateClusterData(data, pos + 2 * sizeof(int)); break;
}

pos += propertySize;
}

lastUpdateTime = time;
}

void updatePointsData(Span<byte> data, int offset)
{
pointCount = Utils.ReadInt(data, offset);
var vectors = Utils.ReadVectors(data, offset + sizeof(int), pointCount * Unsafe.SizeOf<Vector3>());

if (pointsA.Length < pointCount)
pointsA = new Vector3[(int)(pointCount * 1.5)];

if (pointMode == CoordMode.Absolute)
vectors.CopyTo(pointsA.AsSpan());
else
{
for (int i = 0; i < vectors.Length; i++)
pointsA[i] = Vector3.Transform(vectors[i], parentTransform);
}
}

void updateClusterData(Span<byte> data, int offset)
{
state = (State)Utils.ReadInt(data, offset);
if (state == State.Leave) //Will leave
{
onRemove(this);
return;
}

var clusterData = new Vector3[4];
for (int i = 0; i < 4; i++)
{
var si = offset + sizeof(int) + i * Unsafe.SizeOf<Vector3>();

var p = Utils.ReadVector(data, si);
if (pointMode == CoordMode.Absolute)
clusterData[i] = clusterData[i] = p;
else
clusterData[i] = Vector3.Transform(p, parentTransform);// parentTransform.InverseTransformPoint(p);
}

centroid = clusterData[0];
velocity = clusterData[1];
minBounds = clusterData[2];
maxBounds = clusterData[3];
weight = Utils.ReadFloat(data, offset + 4 + 4 * Unsafe.SizeOf<Vector3>());

switch (posUpdateMode)
{
case PositionUpdateMode.None:
break;
case PositionUpdateMode.Centroid:
transform.Translation = centroid;
break;
case PositionUpdateMode.BoxCenter:
transform.Translation = (minBounds + maxBounds) / 2;
break;
}
}

public void kill()
{
if (killDelayTime == 0)
{
//Destroy(gameObject);
return;
}

//points = new Vector3[0];
//StartCoroutine(killForReal(killDelayTime));
}

//IEnumerator killForReal(float timeBeforeKill)
//{
// yield return new WaitForSeconds(timeBeforeKill);
// Destroy(gameObject);
//}

//void OnDrawGizmos()
//{
// if (drawDebug)
// {
// Color c = Color.HSVToRGB((objectID * .1f) % 1, 1, 1); //Color.red;// getColor();
// if (state == State.Ghost) c = Color.gray / 2;

// Gizmos.color = c;
// foreach (var p in points) Gizmos.DrawLine(p, p + Vector3.forward * .01f);

// Gizmos.color = c + Color.white * .3f;
// Gizmos.DrawWireSphere(centroid, .03f);
// Gizmos.DrawWireCube((minBounds + maxBounds) / 2, maxBounds - minBounds);
// }
//}
}
}
92 changes: 92 additions & 0 deletions src/PleiadesClient.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System.Numerics;

namespace Augmenta
{
public class PleiadesClient
{
public Dictionary<int, PObject> objects = new Dictionary<int, PObject>();
Matrix4x4 transform;

//Call once per frame
public void Update(Matrix4x4 transform)
{
this.transform = transform;

var objectsToRemove = new List<int>();
foreach (var o in objects.Values)
if (o.timeSinceGhost > 1)
objectsToRemove.Add(o.objectID);

foreach (var oid in objectsToRemove)
{
var o = objects[oid];
objects.Remove(oid);
o.kill();
}
}

public void processData(float time, Span<byte> data, int offset = 0)
{
var type = data[offset];

if (type == 255) //bundle
{
var pos = offset + 1; //offset + sizeof(packettype)
while (pos < data.Length - 5) //-sizeof(packettype) - sizeof(packetsize)
{
var packetSize = Utils.ReadInt(data, pos + 1); //pos + sizeof(packettype)
processData(time, data, pos);
pos += packetSize;
}
}

//Debug.Log("Packet type : " + type);

switch (type)
{
case 0: //Object
{
processObject(time, data, offset);
}
break;

case 1: //Zone
{
processZone(time, data, offset);
}
break;
}
}

private void processObject(float time, Span<byte> data, int offset)
{
var objectID = Utils.ReadInt(data, offset + 1 + sizeof(int)); //offset + sizeof(packettype) + sizeof(packetsize)

PObject o = null;
if (objects.ContainsKey(objectID)) o = objects[objectID];
if (o == null)
{
o = new PObject();
o.objectID = objectID;
o.onRemove += onObjectRemove;
objects.Add(objectID, o);
}

o.Update(time);
o.parentTransform = transform;
o.updateData(time, data, offset);
}

void processZone(float time, Span<byte> data, int offset)
{

}

//events
void onObjectRemove(PObject o)
{
objects.Remove(o.objectID);
o.kill();
}
}
}
32 changes: 32 additions & 0 deletions src/Utils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Augmenta
{
internal static class Utils
{
internal static int ReadInt(Span<byte> data, int offset)
{
return MemoryMarshal.Cast<byte, int>(data.Slice(offset))[0];
}

internal static float ReadFloat(Span<byte> data, int offset)
{
return MemoryMarshal.Cast<byte, float>(data.Slice(offset))[0];
}

internal static Vector3 ReadVector(Span<byte> data, int offset)
{
return MemoryMarshal.Cast<byte, Vector3>(data.Slice(offset))[0];
}
internal static Span<Vector3> ReadVectors(Span<byte> data, int offset, int length)
{
return MemoryMarshal.Cast<byte, Vector3>(data.Slice(offset, length));
}
}
}

0 comments on commit e8fc6a0

Please sign in to comment.