Last active
September 10, 2021 10:32
-
-
Save Ooseykins/7b3e4cd459dc88e69d68319a13c78d7e to your computer and use it in GitHub Desktop.
Unity component to read uDesktopDuplication (or other) texture as single colour
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
using UnityEngine; | |
using System; | |
public class DesktopCapture : MonoBehaviour | |
{ | |
[SerializeField] | |
uDesktopDuplication.Texture uddTexture; | |
public Light spotLight; | |
[Tooltip("Power of 2 to use for the texture size")] | |
[Range(1, 11)] | |
public int size = 5; | |
RenderTexture[] rtArr; | |
Texture2D pxTex; | |
public void InitRT(int rtCount) { | |
rtArr = new RenderTexture[rtCount]; | |
for (int i = 0; i < rtArr.Length; i++) { | |
rtArr[i] = new RenderTexture((int)Math.Pow(2, i), (int)Math.Pow(2, i), 0); | |
rtArr[i].Create(); | |
} | |
pxTex = new Texture2D(1, 1, TextureFormat.ARGB32, false); | |
} | |
public Color CalculateColor(Texture2D tex) { | |
Graphics.Blit(tex, rtArr[size - 1]); | |
for (int i = size - 1; i > 0; i-- ) { | |
Graphics.Blit(rtArr[i], rtArr[i - 1]); | |
} | |
RenderTexture.active = rtArr[0]; | |
pxTex.ReadPixels(new Rect(0, 0, 1, 1), 0, 0); | |
pxTex.Apply(); | |
return pxTex.GetPixel(0, 0); | |
} | |
private void Start() { | |
InitRT(size); | |
} | |
private void Update() { | |
if (uddTexture.monitor != null && uddTexture.monitor.texture != null) { | |
spotLight.color = CalculateColor(uddTexture.monitor.texture); | |
} | |
else { | |
Debug.Log("uDD monitor or texture not set"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment