Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class TextureGenerator : MonoBehaviour
- {
- public float NoiseStrength;
- [SerializeField] private Renderer outputRenderer;
- private Texture2D targetTexture;
- private WebCamTexture webCamTexture;
- private Color[] previousFrame;
- private void Start()
- {
- webCamTexture = new WebCamTexture();
- webCamTexture.Play();
- targetTexture = new Texture2D(webCamTexture.width, webCamTexture.height);
- outputRenderer.material.mainTexture = targetTexture;
- }
- private void Update()
- {
- Color[] pixels = webCamTexture.GetPixels();
- Color[] output = new Color[pixels.Length];
- if(previousFrame != null)
- {
- int totalMotion = 0;
- int index = 0;
- for (int pixelY = 0; pixelY < webCamTexture.height; pixelY++)
- {
- for (int pixelX = 0; pixelX < webCamTexture.width; pixelX++)
- {
- //int readX = Mathf.FloorToInt(pixelX + Mathf.Sin(pixelY * 0.1f) * NoiseStrength);
- //Color color = pixels[index] - previousFrame[index]; Geke zwarte
- float motion = Mathf.Abs(pixels[index].maxColorComponent- previousFrame[index].maxColorComponent);
- //targetTexture.SetPixel(pixelX, pixelY, color);
- output[index] = (motion > 0.04f)?Color.black:Color.white;
- if((motion > 0.02f))
- {
- totalMotion++;
- }
- index++;
- }
- }
- targetTexture.SetPixels(output);
- targetTexture.Apply();
- Debug.Log("Total motion = " + totalMotion);
- }
- previousFrame = pixels;
- }
- private Texture2D GenerateTexture()
- {
- Texture2D texture = new Texture2D(512, 512);
- for (int pixelY = 0; pixelY < texture.height; pixelY++)
- {
- for (int pixelX = 0; pixelX < texture.width; pixelX++)
- {
- float v = pixelY / (float)texture.height;
- float u = pixelX / (float)texture.width;
- float c = (Mathf.Floor(v * 5) + Mathf.Floor(u * 5)) % 2;
- Color color = new Color(c, c, c);
- texture.SetPixel(pixelX, pixelY, color);
- }
- }
- texture.Apply();
- return texture;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement