forked from homuler/MediaPipeUnityPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextureFrame.cs
More file actions
67 lines (54 loc) · 1.67 KB
/
Copy pathTextureFrame.cs
File metadata and controls
67 lines (54 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Mediapipe;
using System;
using System.Runtime.InteropServices;
using Unity.Collections;
using UnityEngine;
public class TextureFrame {
private Texture2D texture;
private IntPtr nativeTexturePtr = IntPtr.Zero;
public int width { get; private set; }
public int height { get; private set; }
public GlTextureBuffer.DeletionCallback OnRelease;
private GCHandle deletionCallbackHandle;
public TextureFrame(int width, int height, GlTextureBuffer.DeletionCallback OnRelease) {
texture = new Texture2D(width, height, TextureFormat.RGBA32, false);
this.width = width;
this.height = height;
this.OnRelease = OnRelease;
deletionCallbackHandle = GCHandle.Alloc(this.OnRelease, GCHandleType.Pinned);
}
~TextureFrame() {
if (deletionCallbackHandle.IsAllocated) {
deletionCallbackHandle.Free();
}
}
public void CopyTexture(Texture dst) {
Graphics.CopyTexture(texture, dst);
}
public void CopyTextureFrom(WebCamTexture src) {
// TODO: Convert format on GPU
texture.SetPixels32(src.GetPixels32());
texture.Apply();
}
public Color32[] GetPixels32() {
return texture.GetPixels32();
}
// TODO: implement generic method
public NativeArray<byte> GetRawNativeByteArray() {
return texture.GetRawTextureData<byte>();
}
public IntPtr GetNativeTexturePtr(bool update = true) {
if (update || nativeTexturePtr == IntPtr.Zero) {
nativeTexturePtr = texture.GetNativeTexturePtr();
}
return nativeTexturePtr;
}
public GpuBufferFormat gpuBufferformat {
get {
return GpuBufferFormat.kBGRA32;
}
}
public void Release() {
OnRelease((UInt64)GetNativeTexturePtr(false), IntPtr.Zero);
}
}