-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
86 changes: 86 additions & 0 deletions
86
Assets/uWindowCapture/Examples/Buffer/UwcGetBufferExample.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using UnityEngine; | ||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace uWindowCapture | ||
{ | ||
|
||
public class UwcGetBufferExample : MonoBehaviour | ||
{ | ||
[SerializeField] | ||
UwcWindowTexture uwcTexture; | ||
|
||
Texture2D texture_; | ||
Color32[] pixels_; | ||
GCHandle handle_; | ||
IntPtr ptr_ = IntPtr.Zero; | ||
|
||
[DllImport("msvcrt.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] | ||
public static extern IntPtr memcpy(IntPtr dest, IntPtr src, int count); | ||
|
||
bool isValid | ||
{ | ||
get | ||
{ | ||
if (!uwcTexture) return false; | ||
|
||
var window = uwcTexture.window; | ||
return window != null && window.buffer != IntPtr.Zero; | ||
} | ||
} | ||
|
||
void OnDestroy() | ||
{ | ||
if (ptr_ != IntPtr.Zero) { | ||
handle_.Free(); | ||
} | ||
} | ||
|
||
void Update() | ||
{ | ||
if (!isValid) return; | ||
|
||
var window = uwcTexture.window; | ||
if (texture_ == null || | ||
window.bufferWidth != texture_.width || | ||
window.bufferHeight != texture_.height) { | ||
UpdateTexture(); | ||
} | ||
|
||
CopyTexture(); | ||
} | ||
|
||
void UpdateTexture() | ||
{ | ||
if (!isValid) return; | ||
|
||
var window = uwcTexture.window; | ||
var width = window.bufferWidth; | ||
var height = window.bufferHeight; | ||
|
||
texture_ = new Texture2D(width, height, TextureFormat.RGBA32, false); | ||
texture_.filterMode = FilterMode.Bilinear; | ||
pixels_ = texture_.GetPixels32(); | ||
handle_ = GCHandle.Alloc(pixels_, GCHandleType.Pinned); | ||
ptr_ = handle_.AddrOfPinnedObject(); | ||
|
||
GetComponent<Renderer>().material.mainTexture = texture_; | ||
} | ||
|
||
void CopyTexture() | ||
{ | ||
if (!isValid) return; | ||
|
||
var window = uwcTexture.window; | ||
var width = window.bufferWidth; | ||
var height = window.bufferHeight; | ||
var buffer = window.buffer; | ||
|
||
memcpy(ptr_, buffer, width * height * sizeof(Byte) * 4); | ||
|
||
texture_.SetPixels32(pixels_); | ||
texture_.Apply(); | ||
} | ||
} | ||
|
||
} |
11 changes: 11 additions & 0 deletions
11
Assets/uWindowCapture/Examples/Buffer/UwcGetBufferExample.cs.meta
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters