i don’t know if anyone else has a similar problem but i’m trying to make a kinect like motion controller script that uses a webcam such as the one on your laptop and the way i’m doing it is taking a picture of the background and the current camera texture2d. the only problem is i can’t subtract the same pixels from both of them. i looked online and there was almost no talk about comparing 2 texture2d and none included a green screen effect. here is my script
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class facerecognition : MonoBehaviour {
void Start ()
{
WebCamTexture CamTex = new WebCamTexture();
Renderer renderer = GetComponent<Renderer>();
renderer.material.mainTexture = CamTex;
CamTex.Play();
}
// Update is called once per frame
void Update ()
{
//sets the background when s is pressed
if (Input.GetKeyUp("s"))
{
StartCoroutine (setBackground());
//lets you know s key has registerd
Debug.Log ("s has been pressed");
}
StartCoroutine(backgroundisolation());
}
//isolates background
IEnumerator backgroundisolation()
{
yield return WaitForEndOfFrame();
//gets current webcamtexture
Texture2D tex2 = null;
tex2.SetPixels((GetComponent<Renderer>().material.mainTexture as WebCamTexture).GetPixels());
//variables for background
Texture2D tex = null;
byte[] fileData;
//loads background
fileData = File.ReadAllBytes(Application.persistentDataPath + "background.png");
tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
//checks if it's the same (helps for lag and can program default animation
if (tex2 != tex)
{
//texture2d the green screen effect is applying to
Texture2D changes = new Texture2D(tex.width, tex.height);
//heres my problem \|/
changes.SetPixels(tex2 - tex);
}
yield break;
}
//sets the background
public IEnumerator setBackground()
{
yield return new WaitForEndOfFrame();
//gets current webcamtexture
Texture2D snap = new Texture2D(GetComponent<Renderer>().material.mainTexture.width,
GetComponent<Renderer>().material.mainTexture.height);
snap.SetPixels((GetComponent<Renderer>().material.mainTexture as WebCamTexture).GetPixels());
snap.Apply();
byte[] bytes = snap.EncodeToPNG();
//writes a png
File.WriteAllBytes(Application.persistentDataPath + "background.png", bytes);
//to know it was called
Debug.Log ("background saved");
}
}
if there’s any operators i don’t know of or something like SamePixels that would really help thank you.