Skip to content

Commit

Permalink
refactor and imunode based on component
Browse files Browse the repository at this point in the history
  • Loading branch information
Xlinka committed May 4, 2024
1 parent f429843 commit f8e497b
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 34 deletions.
29 changes: 0 additions & 29 deletions ProjectObsidian/ProtoFlux/Avatar/AvatarRootSlot.cs

This file was deleted.

112 changes: 112 additions & 0 deletions ProjectObsidian/ProtoFlux/Devices/ImuDataNode .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
using System;
using System.Runtime.InteropServices;
using FrooxEngine;
using FrooxEngine.ProtoFlux;
using ProtoFlux.Core;
using Valve.VR;
using ProtoFlux.Runtimes.Execution;
using Elements.Core;
using System.Threading.Tasks;



namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Devices
{
[NodeCategory("Obsidian/Devices")]
public class ImuDataNode : AsyncActionNode<FrooxEngineContext>
{
public ObjectInput<User> SimulatingUser;
public ObjectInput<string> Path;
public ValueInput<bool> Simulate;

public ValueOutput<double> SampleTime;
public ValueOutput<double3> VAccel;
public ValueOutput<double3> VGyro;
public ValueOutput<Imu_OffScaleFlags> OffScaleFlags;

public AsyncCall OnSimulationStart;
public Continuation OnDataUpdated;
public Continuation OnError;

private ulong _buffer;
private string _previousPath;

public ImuDataNode()
{
SampleTime = new ValueOutput<double>(this);
VAccel = new ValueOutput<double3>(this);
VGyro = new ValueOutput<double3>(this);
OffScaleFlags = new ValueOutput<Imu_OffScaleFlags>(this);
}

protected override async Task<IOperation> RunAsync(FrooxEngineContext context)
{
User user = SimulatingUser.Evaluate(context);
bool simulate = Simulate.Evaluate(context);
string path = Path.Evaluate(context);

if (!simulate || user != context.LocalUser || string.IsNullOrEmpty(path))
{
CloseBuffer();
return OnError.Target;
}

await OnSimulationStart.ExecuteAsync(context);

if (_buffer == 0 || _previousPath != path)
{
CloseBuffer();
if (!OpenBuffer(path))
{
return OnError.Target;
}
}

ReadImuData(context);
return OnDataUpdated.Target;
}

private bool OpenBuffer(string path)
{
unsafe
{
ulong buffer = default;
var errorCode = OpenVR.IOBuffer.Open(path, EIOBufferMode.Read, (uint)sizeof(ImuSample_t), 0u, ref buffer);
if (errorCode == EIOBufferError.IOBuffer_Success)
{
_buffer = buffer;
_previousPath = path;
return true;
}
return false;
}
}

private void ReadImuData(FrooxEngineContext context)
{
unsafe
{
var punRead = 0u;
var imuSample_t = default(ImuSample_t);
var error = OpenVR.IOBuffer.Read(_buffer, (IntPtr)(&imuSample_t), (uint)sizeof(ImuSample_t), ref punRead);

if (error == EIOBufferError.IOBuffer_Success && punRead == sizeof(ImuSample_t))
{
// Writing each IMU data point with context to ensure correct synchronization and data handling
SampleTime.Write(imuSample_t.fSampleTime, context);
VAccel.Write(new double3(imuSample_t.vAccel.v0, imuSample_t.vAccel.v1, imuSample_t.vAccel.v2), context);
VGyro.Write(new double3(imuSample_t.vGyro.v0, imuSample_t.vGyro.v1, imuSample_t.vGyro.v2), context);
OffScaleFlags.Write((Imu_OffScaleFlags)imuSample_t.unOffScaleFlags, context);
}
}
}

private void CloseBuffer()
{
if (_buffer == 0) return;
OpenVR.IOBuffer.Close(_buffer);
_buffer = 0;
}
}

}
17 changes: 12 additions & 5 deletions ProjectObsidian/ProtoFlux/Users/Avatar/AvatarRootSlot.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,31 @@
using System.Collections.Generic;
using System.Linq;
using Elements.Core;
using FrooxEngine;
using FrooxEngine.CommonAvatar;
using ProtoFlux.Core;
using ProtoFlux.Runtimes.Execution;

namespace ProtoFlux.Users.Avatar
namespace ProtoFlux.Runtimes.Execution.Nodes.Obsidian.Users.Avatar
{
[ContinuouslyChanging]
[NodeCategory("ProtoFlux/Obsidian/Avatar")]
[NodeCategory("Obsidian/Avatar")]
public class AvatarRootSlot : ObjectFunctionNode<ExecutionContext, Slot>
{
public readonly ObjectInput<User> User;

protected override Slot Compute(ExecutionContext context)
{
User user = User.Evaluate(context);
if (user == null) return null;

Slot slot = user.Root.Slot;
List<AvatarRoot> list = new List<AvatarRoot>();
List<AvatarRoot> list = Pool.BorrowList<AvatarRoot>();
slot.GetFirstDirectComponentsInChildren(list);
return user == null || list.Count == 0 ? null : list[0].Slot;
Slot avatarRoot = list.FirstOrDefault()?.Slot;
Pool.Return(ref list);

return avatarRoot;
}
}
}
}

0 comments on commit f8e497b

Please sign in to comment.