SHARE
TWEET

motion detection

a guest Feb 18th, 2019 83 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TextureGen : MonoBehaviour
  6. {
  7.     [SerializeField] private Renderer _OutPutRenderer;
  8.    
  9.     [SerializeField] private Material _OutputMaterial;
  10.  
  11.     [SerializeField] private float _DetectMovement;
  12.  
  13.     private WebCamTexture _webCamTexture;
  14.     private Texture2D _TargetTexture;
  15.     private Color _Color;
  16.    
  17.     private void Start()
  18.     {
  19.         _webCamTexture = new WebCamTexture();
  20.         _webCamTexture.Play();
  21.  
  22.         _TargetTexture = new Texture2D(_webCamTexture.width, _webCamTexture.height);
  23.  
  24.         _OutputMaterial.mainTexture = _TargetTexture;
  25.     }
  26.  
  27.     private Color[] _PreviousFrame;
  28.     private void Update()
  29.     {
  30.         Color[] _pixels = _webCamTexture.GetPixels();
  31.         Color[] _output = new Color[_pixels.Length];
  32.  
  33.         if(_PreviousFrame != null)
  34.         {
  35.            
  36.  
  37.             int _totalmotion = 0;
  38.             int _index = 0;
  39.             for (int PixelY = 0; PixelY < _webCamTexture.height; PixelY++)
  40.             {
  41.                 for (int PixelX = 0; PixelX < _webCamTexture.width; PixelX++)
  42.                 {
  43.                     float _motion = Mathf.Abs(_pixels[_index].maxColorComponent - _PreviousFrame[_index].maxColorComponent);
  44.                     _output[_index] = (_motion < _DetectMovement) ? _Color:Color.red;
  45.                     if ((_motion > _DetectMovement)) _totalmotion++;
  46.                     _index++;
  47.                 }
  48.             }
  49.             _TargetTexture.SetPixels(_output);
  50.             _TargetTexture.Apply();
  51.             int _totalPixels = _webCamTexture.width * _webCamTexture.height;
  52.             print(_totalmotion * 100 / _totalPixels);
  53.         }
  54.         _PreviousFrame = _pixels;
  55.     }
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top