Advertisement
stefanp_gd

Untitled

Feb 18th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.51 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TextureGenerator : MonoBehaviour
  6. {
  7.     public float NoiseStrength;
  8.     [SerializeField] private Renderer outputRenderer;
  9.     private Texture2D targetTexture;
  10.     private WebCamTexture webCamTexture;
  11.  
  12.     private Color[] previousFrame;
  13.  
  14.     private void Start()
  15.     {
  16.        
  17.         webCamTexture = new WebCamTexture();
  18.         webCamTexture.Play();
  19.         targetTexture = new Texture2D(webCamTexture.width, webCamTexture.height);
  20.         outputRenderer.material.mainTexture = targetTexture;
  21.     }
  22.  
  23.     private void Update()
  24.     {
  25.         Color[] pixels = webCamTexture.GetPixels();
  26.         Color[] output = new Color[pixels.Length];
  27.  
  28.         if(previousFrame != null)
  29.         {
  30.             int totalMotion = 0;
  31.             int index = 0;
  32.             for (int pixelY = 0; pixelY < webCamTexture.height; pixelY++)
  33.             {
  34.                 for (int pixelX = 0; pixelX < webCamTexture.width; pixelX++)
  35.                 {
  36.                     //int readX = Mathf.FloorToInt(pixelX + Mathf.Sin(pixelY * 0.1f) * NoiseStrength);
  37.                     //Color color = pixels[index] - previousFrame[index]; Geke zwarte
  38.                     float motion = Mathf.Abs(pixels[index].maxColorComponent- previousFrame[index].maxColorComponent);
  39.                     //targetTexture.SetPixel(pixelX, pixelY, color);
  40.                     output[index] = (motion > 0.04f)?Color.black:Color.white;
  41.                     if((motion > 0.02f))
  42.                     {
  43.                         totalMotion++;
  44.                     }
  45.                     index++;
  46.                 }
  47.             }
  48.             targetTexture.SetPixels(output);
  49.             targetTexture.Apply();
  50.             Debug.Log("Total motion = " + totalMotion);
  51.         }
  52.        
  53.         previousFrame = pixels;
  54.        
  55.     }
  56.  
  57.     private Texture2D GenerateTexture()
  58.     {
  59.         Texture2D texture = new Texture2D(512, 512);
  60.         for (int pixelY = 0; pixelY < texture.height; pixelY++)
  61.         {
  62.             for (int pixelX = 0; pixelX < texture.width; pixelX++)
  63.             {
  64.                 float v = pixelY / (float)texture.height;
  65.                 float u = pixelX / (float)texture.width;
  66.  
  67.                 float c = (Mathf.Floor(v * 5) + Mathf.Floor(u * 5)) % 2;
  68.  
  69.                 Color color = new Color(c, c, c);
  70.                 texture.SetPixel(pixelX, pixelY, color);
  71.             }
  72.         }
  73.         texture.Apply();
  74.         return texture;
  75.  
  76.     }
  77.  
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement