Skip to content

Commit 9cb5663

Browse files
committed
return span instead of using callback (hidden area mesh)
1 parent 8114721 commit 9cb5663

File tree

5 files changed

+37
-64
lines changed

5 files changed

+37
-64
lines changed

OpenVR.NET/Devices/DeviceModel.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,7 @@ public enum ComponentType {
142142
}
143143

144144
/// <summary>
145-
/// Loads the model asynchronously (on a thread pool).
146-
/// If a callback is not registered, the associated resource will not be loaded,
147-
/// otherwise it is your responsibility to dispose of any received disposable resources.
145+
/// Loads the model asynchronously (on a thread pool)
148146
/// </summary>
149147
public async Task LoadAsync (
150148
Func<ComponentType, bool>? begin = null,

OpenVR.NET/DrawContext.cs

+13-37
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Numerics;
2-
using System.Runtime.InteropServices;
32
using Valve.VR;
43

54
namespace OpenVR.NET;
@@ -24,21 +23,16 @@ public interface IVRDrawContext {
2423
void SubmitFrame ( EVREye eye, Texture_t texture, EVRSubmitFlags flags = EVRSubmitFlags.Submit_Default );
2524

2625
/// <summary>
27-
/// Gets a mesh which will cover the area of the render texture that is not visible.
26+
/// Gets a mesh which will cover the area of the render texture that is not visible ( or visible if <paramref name="inverse"/> is <see langword="true"/> ).
2827
/// The mesh is in UV coordinates.
2928
/// </summary>
30-
void GetHiddenAreaMesh ( EVREye eye, Action<Vector2, Vector2, Vector2> addTriangle );
29+
ReadOnlySpan<(Vector2, Vector2, Vector2)> GetHiddenAreaMesh ( EVREye eye, bool inverse = false );
3130

3231
/// <summary>
33-
/// Gets a mesh which will cover the area of the render texture that is visible.
32+
/// Gets a vertex loop around the area of the render texture which is visible.
3433
/// The mesh is in UV coordinates.
3534
/// </summary>
36-
void GetInverseHiddenAreaMesh ( EVREye eye, Action<Vector2, Vector2, Vector2> addTriangle );
37-
38-
/// <summary>
39-
/// idk
40-
/// </summary>
41-
void GetLoopHiddenAreaMesh ( EVREye eye, Action<Vector2> addVertex );
35+
ReadOnlySpan<Vector2> GetLoopHiddenAreaMesh ( EVREye eye );
4236

4337
/// <summary>
4438
/// Whether OpenVR is rendering controllers on its own - you should consider not drawing user hands/controllers if this is <see langword="true"/>
@@ -71,37 +65,19 @@ public void SubmitFrame ( EVREye eye, Texture_t texture, EVRSubmitFlags flags =
7165

7266
public Vector2 Resolution { get; }
7367

74-
public void GetHiddenAreaMesh ( EVREye eye, Action<Vector2, Vector2, Vector2> addTriangle ) {
75-
var mesh = VR.CVR.GetHiddenAreaMesh( eye, EHiddenAreaMeshType.k_eHiddenAreaMesh_Standard );
76-
var size = Marshal.SizeOf<HmdVector2_t>();
77-
for ( int i = 0; i < mesh.unTriangleCount; i++ ) {
78-
var ptr = mesh.pVertexData + size * i * 3;
79-
var a = Marshal.PtrToStructure<HmdVector2_t>( ptr );
80-
var b = Marshal.PtrToStructure<HmdVector2_t>( ptr + size );
81-
var c = Marshal.PtrToStructure<HmdVector2_t>( ptr + size + size );
82-
addTriangle( new( a.v0, a.v1 ), new( b.v0, b.v1 ), new( c.v0, c.v1 ) );
68+
public ReadOnlySpan<(Vector2, Vector2, Vector2)> GetHiddenAreaMesh ( EVREye eye, bool inverse = false ) {
69+
var mesh = VR.CVR.GetHiddenAreaMesh( eye, inverse ? EHiddenAreaMeshType.k_eHiddenAreaMesh_Inverse : EHiddenAreaMeshType.k_eHiddenAreaMesh_Standard );
70+
71+
unsafe {
72+
return new ReadOnlySpan<(Vector2, Vector2, Vector2)>( mesh.pVertexData.ToPointer(), (int)mesh.unTriangleCount );
8373
}
8474
}
8575

86-
public void GetInverseHiddenAreaMesh ( EVREye eye, Action<Vector2, Vector2, Vector2> addTriangle ) {
87-
var mesh = VR.CVR.GetHiddenAreaMesh( eye, EHiddenAreaMeshType.k_eHiddenAreaMesh_Inverse );
88-
var size = Marshal.SizeOf<HmdVector2_t>();
89-
for ( int i = 0; i < mesh.unTriangleCount; i++ ) {
90-
var ptr = mesh.pVertexData + size * i * 3;
91-
var a = Marshal.PtrToStructure<HmdVector2_t>( ptr );
92-
var b = Marshal.PtrToStructure<HmdVector2_t>( ptr + size );
93-
var c = Marshal.PtrToStructure<HmdVector2_t>( ptr + size + size );
94-
addTriangle( new( a.v0, a.v1 ), new( b.v0, b.v1 ), new( c.v0, c.v1 ) );
95-
}
96-
}
97-
98-
public void GetLoopHiddenAreaMesh ( EVREye eye, Action<Vector2> addVertex ) {
76+
public ReadOnlySpan<Vector2> GetLoopHiddenAreaMesh ( EVREye eye ) {
9977
var mesh = VR.CVR.GetHiddenAreaMesh( eye, EHiddenAreaMeshType.k_eHiddenAreaMesh_LineLoop );
100-
var size = Marshal.SizeOf<HmdVector2_t>();
101-
for ( int i = 0; i < mesh.unTriangleCount; i++ ) {
102-
var ptr = mesh.pVertexData + size * i;
103-
var a = Marshal.PtrToStructure<HmdVector2_t>( ptr );
104-
addVertex( new( a.v0, a.v1 ) );
78+
79+
unsafe {
80+
return new ReadOnlySpan<Vector2>( mesh.pVertexData.ToPointer(), (int)mesh.unTriangleCount );
10581
}
10682
}
10783

OpenVR.NET/OpenVR.NET.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
77
<RootNamespace>OpenVR.NET</RootNamespace>
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
89
</PropertyGroup>
910

1011
<ItemGroup>

Tests/Program.cs

+12-14
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,21 @@ void Log ( object? msg, Thread? expectedThread = null ) {
7575
hiddenMeshSaved = true;
7676
StringBuilder left = new();
7777
int i = 1;
78-
ctx.GetHiddenAreaMesh( EVREye.Eye_Left, ( a, b, c ) => {
78+
foreach ( var (a, b, c) in ctx.GetHiddenAreaMesh( EVREye.Eye_Left ) ) {
7979
left.AppendLine( $"v {a.X} {a.Y} 0" );
8080
left.AppendLine( $"v {b.X} {b.Y} 0" );
8181
left.AppendLine( $"v {c.X} {c.Y} 0" );
8282
left.AppendLine( $"f {i++} {i++} {i++}" );
83-
} );
83+
}
8484

8585
StringBuilder right = new();
8686
i = 1;
87-
ctx.GetHiddenAreaMesh( EVREye.Eye_Right, ( a, b, c ) => {
87+
foreach ( var (a, b, c) in ctx.GetHiddenAreaMesh( EVREye.Eye_Right ) ) {
8888
right.AppendLine( $"v {a.X} {a.Y} 0" );
8989
right.AppendLine( $"v {b.X} {b.Y} 0" );
9090
right.AppendLine( $"v {c.X} {c.Y} 0" );
9191
right.AppendLine( $"f {i++} {i++} {i++}" );
92-
} );
92+
}
9393

9494
Task.Run( async () => {
9595
await File.WriteAllTextAsync( "./leftHiddenMesh.obj", left.ToString() );
@@ -102,21 +102,21 @@ void Log ( object? msg, Thread? expectedThread = null ) {
102102

103103
StringBuilder leftinv = new();
104104
i = 1;
105-
ctx.GetInverseHiddenAreaMesh( EVREye.Eye_Left, ( a, b, c ) => {
105+
foreach ( var (a, b, c) in ctx.GetHiddenAreaMesh( EVREye.Eye_Left, inverse: true ) ) {
106106
leftinv.AppendLine( $"v {a.X} {a.Y} 0" );
107107
leftinv.AppendLine( $"v {b.X} {b.Y} 0" );
108108
leftinv.AppendLine( $"v {c.X} {c.Y} 0" );
109109
leftinv.AppendLine( $"f {i++} {i++} {i++}" );
110-
} );
110+
}
111111

112112
StringBuilder rightinv = new();
113113
i = 1;
114-
ctx.GetInverseHiddenAreaMesh( EVREye.Eye_Right, ( a, b, c ) => {
114+
foreach ( var (a, b, c) in ctx.GetHiddenAreaMesh( EVREye.Eye_Right, inverse: true ) ) {
115115
rightinv.AppendLine( $"v {a.X} {a.Y} 0" );
116116
rightinv.AppendLine( $"v {b.X} {b.Y} 0" );
117117
rightinv.AppendLine( $"v {c.X} {c.Y} 0" );
118118
rightinv.AppendLine( $"f {i++} {i++} {i++}" );
119-
} );
119+
}
120120

121121
Task.Run( async () => {
122122
await File.WriteAllTextAsync( "./leftInverseHiddenMesh.obj", leftinv.ToString() );
@@ -128,16 +128,14 @@ void Log ( object? msg, Thread? expectedThread = null ) {
128128
} );
129129

130130
StringBuilder leftloop = new();
131-
i = 1;
132-
ctx.GetLoopHiddenAreaMesh( EVREye.Eye_Left, a => {
131+
foreach ( var a in ctx.GetLoopHiddenAreaMesh( EVREye.Eye_Left ) ) {
133132
leftloop.AppendLine( $"v {a.X} {a.Y} 0" );
134-
} );
133+
}
135134

136135
StringBuilder rightloop = new();
137-
i = 1;
138-
ctx.GetLoopHiddenAreaMesh( EVREye.Eye_Right, a => {
136+
foreach ( var a in ctx.GetLoopHiddenAreaMesh( EVREye.Eye_Right ) ) {
139137
rightloop.AppendLine( $"v {a.X} {a.Y} 0" );
140-
} );
138+
}
141139

142140
Task.Run( async () => {
143141
await File.WriteAllTextAsync( "./leftLoopHiddenMesh.obj", leftloop.ToString() );

VisualTests/TestWindow.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -278,27 +278,27 @@ void loadHiddenMeshes ( IVRDrawContext ctx ) {
278278
leftHiddenMesh = new( v => PositionVertex.Upload( v, v.Length ) );
279279
leftHiddenMesh.Link( basicShader, s => PositionVertex.Link( position: s.GetAttrib( "aPos" ) ) );
280280
uint i = 0;
281-
ctx.GetHiddenAreaMesh( EVREye.Eye_Left, ( a, b, c ) => {
282-
leftHiddenMesh.Vertices.Add( new( (a.X - 0.5f) * 2, (a.Y - 0.5f) * 2, 0 ) );
283-
leftHiddenMesh.Vertices.Add( new( (b.X - 0.5f) * 2, (b.Y - 0.5f) * 2, 0 ) );
284-
leftHiddenMesh.Vertices.Add( new( (c.X - 0.5f) * 2, (c.Y - 0.5f) * 2, 0 ) );
281+
foreach ( var (a, b, c) in ctx.GetHiddenAreaMesh( EVREye.Eye_Left ) ) {
282+
leftHiddenMesh.Vertices.Add( new( ( a.X - 0.5f ) * 2, ( a.Y - 0.5f ) * 2, 0 ) );
283+
leftHiddenMesh.Vertices.Add( new( ( b.X - 0.5f ) * 2, ( b.Y - 0.5f ) * 2, 0 ) );
284+
leftHiddenMesh.Vertices.Add( new( ( c.X - 0.5f ) * 2, ( c.Y - 0.5f ) * 2, 0 ) );
285285
leftHiddenMesh.Indices.Add( i++ );
286286
leftHiddenMesh.Indices.Add( i++ );
287287
leftHiddenMesh.Indices.Add( i++ );
288-
} );
288+
}
289289
leftHiddenMesh.Upload();
290290

291291
rightHiddenMesh = new( v => PositionVertex.Upload( v, v.Length ) );
292292
rightHiddenMesh.Link( basicShader, s => PositionVertex.Link( position: s.GetAttrib( "aPos" ) ) );
293293
i = 0;
294-
ctx.GetHiddenAreaMesh( EVREye.Eye_Right, ( a, b, c ) => {
295-
rightHiddenMesh.Vertices.Add( new( (a.X - 0.5f) * 2, (a.Y - 0.5f) * 2, 0 ) );
296-
rightHiddenMesh.Vertices.Add( new( (b.X - 0.5f) * 2, (b.Y - 0.5f) * 2, 0 ) );
297-
rightHiddenMesh.Vertices.Add( new( (c.X - 0.5f) * 2, (c.Y - 0.5f) * 2, 0 ) );
294+
foreach ( var (a, b, c) in ctx.GetHiddenAreaMesh( EVREye.Eye_Right ) ) {
295+
rightHiddenMesh.Vertices.Add( new( ( a.X - 0.5f ) * 2, ( a.Y - 0.5f ) * 2, 0 ) );
296+
rightHiddenMesh.Vertices.Add( new( ( b.X - 0.5f ) * 2, ( b.Y - 0.5f ) * 2, 0 ) );
297+
rightHiddenMesh.Vertices.Add( new( ( c.X - 0.5f ) * 2, ( c.Y - 0.5f ) * 2, 0 ) );
298298
rightHiddenMesh.Indices.Add( i++ );
299299
rightHiddenMesh.Indices.Add( i++ );
300300
rightHiddenMesh.Indices.Add( i++ );
301-
} );
301+
}
302302
rightHiddenMesh.Upload();
303303
}
304304

0 commit comments

Comments
 (0)