Skip to content

Commit 8d4631d

Browse files
committed
- Source formatting
- Added feature contributor name
1 parent 7cbf24e commit 8d4631d

File tree

2 files changed

+81
-66
lines changed

2 files changed

+81
-66
lines changed
Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,58 @@
1-
/*
2-
This sample code is for demonstrating and testing the functionality
3-
of Unity Capture, and is placed in the public domain.
4-
5-
This code generates a scrolling color texture simply for the purposes of demonstration.
6-
Other uses may include sending a video, another webcam feed or a static image to the output.
7-
*/
8-
9-
using UnityEngine;
10-
11-
public class CaptureTexture : MonoBehaviour {
12-
public int width = 320;
13-
public int height = 240;
14-
public MeshRenderer outputRenderer;
15-
Texture2D activeTex;
16-
UnityCapture.Interface captureInterface;
17-
int y = 0;
18-
Color color = Color.red;
19-
20-
21-
void Start () {
22-
// Create textures
23-
activeTex = new Texture2D(width, height, TextureFormat.ARGB32, false);
24-
captureInterface = new UnityCapture.Interface(UnityCapture.ECaptureDevice.CaptureDevice1);
25-
26-
if (outputRenderer != null) outputRenderer.material.mainTexture = activeTex;
27-
}
28-
29-
void OnDestroy() {
30-
captureInterface.Close();
31-
}
32-
33-
void Update() {
34-
// Draw next line on texture
35-
for (int x = 0; x < width; x++) {
36-
activeTex.SetPixel(x, y, color);
37-
}
38-
39-
y += 1;
40-
if (y > height) {
41-
y = 0;
42-
color = new Color(color.g, color.b, color.r);
43-
}
44-
45-
activeTex.Apply();
46-
47-
// Update the capture texture
48-
UnityCapture.ECaptureSendResult result = captureInterface.SendTexture(activeTex, false, UnityCapture.EResizeMode.LinearResize, UnityCapture.EMirrorMode.Disabled);
49-
50-
if (result != UnityCapture.ECaptureSendResult.SUCCESS) {
51-
Debug.Log(System.Enum.GetName(typeof(UnityCapture.ECaptureSendResult), result));
52-
}
53-
}
54-
}
1+
/*
2+
This sample code is for demonstrating and testing the functionality
3+
of Unity Capture, and is placed in the public domain.
4+
5+
This code generates a scrolling color texture simply for the purposes of demonstration.
6+
Other uses may include sending a video, another webcam feed or a static image to the output.
7+
*/
8+
9+
using UnityEngine;
10+
11+
public class CaptureTexture : MonoBehaviour
12+
{
13+
public int width = 320;
14+
public int height = 240;
15+
public MeshRenderer outputRenderer;
16+
Texture2D activeTex;
17+
UnityCapture.Interface captureInterface;
18+
int y = 0;
19+
Color color = Color.red;
20+
21+
void Start()
22+
{
23+
// Create texture and capture interface
24+
activeTex = new Texture2D(width, height, TextureFormat.ARGB32, false);
25+
captureInterface = new UnityCapture.Interface(UnityCapture.ECaptureDevice.CaptureDevice1);
26+
27+
if (outputRenderer != null) outputRenderer.material.mainTexture = activeTex;
28+
}
29+
30+
void OnDestroy()
31+
{
32+
//Cleanup capture interface
33+
captureInterface.Close();
34+
}
35+
36+
void Update()
37+
{
38+
// Draw next line on texture
39+
for (int x = 0; x < width; x++)
40+
{
41+
activeTex.SetPixel(x, y, color);
42+
}
43+
44+
y += 1;
45+
if (y > height)
46+
{
47+
y = 0;
48+
color = new Color(color.g, color.b, color.r);
49+
}
50+
51+
activeTex.Apply();
52+
53+
// Update the capture texture
54+
UnityCapture.ECaptureSendResult result = captureInterface.SendTexture(activeTex);
55+
if (result != UnityCapture.ECaptureSendResult.SUCCESS)
56+
Debug.Log("SendTexture failed: " + result);
57+
}
58+
}

UnityCaptureSample/Assets/UnityCapture/UnityCapture.cs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
Unity Capture
33
Copyright (c) 2018 Bernhard Schelling
44
5+
Feature contributors:
6+
Brandon J Matthews (low-level interface for custom texture capture)
7+
58
Based on UnityCam
69
https://github.com/mrayy/UnityCam
710
Copyright (c) 2016 MHD Yamen Saraiji
@@ -31,8 +34,8 @@ public class UnityCapture : MonoBehaviour
3134
public enum ECaptureDevice { CaptureDevice1 = 0, CaptureDevice2 = 1, CaptureDevice3 = 2, CaptureDevice4 = 3, CaptureDevice5 = 4, CaptureDevice6 = 5, CaptureDevice7 = 6, CaptureDevice8 = 7, CaptureDevice9 = 8, CaptureDevice10 = 9 }
3235
public enum EResizeMode { Disabled = 0, LinearResize = 1 }
3336
public enum EMirrorMode { Disabled = 0, MirrorHorizontally = 1 }
34-
public enum ECaptureSendResult { SUCCESS = 0, WARNING_FRAMESKIP = 1, WARNING_CAPTUREINACTIVE = 2, ERROR_UNSUPPORTEDGRAPHICSDEVICE = 100, ERROR_PARAMETER = 101, ERROR_TOOLARGERESOLUTION = 102, ERROR_TEXTUREFORMAT = 103, ERROR_READTEXTURE = 104, ERROR_INVALIDCAPTUREINSTANCEPTR = 200};
35-
37+
public enum ECaptureSendResult { SUCCESS = 0, WARNING_FRAMESKIP = 1, WARNING_CAPTUREINACTIVE = 2, ERROR_UNSUPPORTEDGRAPHICSDEVICE = 100, ERROR_PARAMETER = 101, ERROR_TOOLARGERESOLUTION = 102, ERROR_TEXTUREFORMAT = 103, ERROR_READTEXTURE = 104, ERROR_INVALIDCAPTUREINSTANCEPTR = 200 };
38+
3639
[SerializeField] [Tooltip("Capture device index")] public ECaptureDevice CaptureDevice = ECaptureDevice.CaptureDevice1;
3740
[SerializeField] [Tooltip("Scale image if Unity and capture resolution don't match (can introduce frame dropping, not recommended)")] public EResizeMode ResizeMode = EResizeMode.Disabled;
3841
[SerializeField] [Tooltip("Mirror captured output image")] public EMirrorMode MirrorMode = EMirrorMode.Disabled;
@@ -82,25 +85,33 @@ void OnRenderImage(RenderTexture source, RenderTexture destination)
8285
}
8386
}
8487

85-
86-
public class Interface {
88+
public class Interface
89+
{
8790
[System.Runtime.InteropServices.DllImport("UnityCapturePlugin")] extern static System.IntPtr CaptureCreateInstance(int CapNum);
8891
[System.Runtime.InteropServices.DllImport("UnityCapturePlugin")] extern static void CaptureDeleteInstance(System.IntPtr instance);
8992
[System.Runtime.InteropServices.DllImport("UnityCapturePlugin")] extern static ECaptureSendResult CaptureSendTexture(System.IntPtr instance, System.IntPtr nativetexture, bool UseDoubleBuffering, EResizeMode ResizeMode, EMirrorMode MirrorMode, bool IsLinearColorSpace);
9093
System.IntPtr CaptureInstance;
91-
92-
public Interface(ECaptureDevice captureDevice) {
93-
CaptureInstance = CaptureCreateInstance((int)captureDevice);
94+
95+
public Interface(ECaptureDevice CaptureDevice)
96+
{
97+
CaptureInstance = CaptureCreateInstance((int)CaptureDevice);
9498
}
9599

96-
public void Close() {
97-
CaptureDeleteInstance(CaptureInstance);
100+
~Interface()
101+
{
102+
Close();
98103
}
99104

100-
public ECaptureSendResult SendTexture(Texture source, bool DoubleBuffering, EResizeMode ResizeMode, EMirrorMode MirrorMode) {
105+
public void Close()
106+
{
107+
if (CaptureInstance != System.IntPtr.Zero) CaptureDeleteInstance(CaptureInstance);
108+
CaptureInstance = System.IntPtr.Zero;
109+
}
110+
111+
public ECaptureSendResult SendTexture(Texture Source, bool DoubleBuffering = false, EResizeMode ResizeMode = EResizeMode.Disabled, EMirrorMode MirrorMode = EMirrorMode.Disabled)
112+
{
101113
if (CaptureInstance == System.IntPtr.Zero) return ECaptureSendResult.ERROR_INVALIDCAPTUREINSTANCEPTR;
102-
ECaptureSendResult result = CaptureSendTexture(CaptureInstance, source.GetNativeTexturePtr(), DoubleBuffering, ResizeMode, MirrorMode, QualitySettings.activeColorSpace == ColorSpace.Linear);
103-
return result;
114+
return CaptureSendTexture(CaptureInstance, Source.GetNativeTexturePtr(), DoubleBuffering, ResizeMode, MirrorMode, QualitySettings.activeColorSpace == ColorSpace.Linear);
104115
}
105116
}
106117
}

0 commit comments

Comments
 (0)