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 TextureGen : MonoBehaviour
- {
- [SerializeField] private Renderer _OutPutRenderer;
- [SerializeField] private Material _OutputMaterial;
- [SerializeField] private float _DetectMovement;
- private WebCamTexture _webCamTexture;
- private Texture2D _TargetTexture;
- private Color _Color;
- private void Start()
- {
- _webCamTexture = new WebCamTexture();
- _webCamTexture.Play();
- _TargetTexture = new Texture2D(_webCamTexture.width, _webCamTexture.height);
- _OutputMaterial.mainTexture = _TargetTexture;
- }
- private Color[] _PreviousFrame;
- 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++)
- {
- float _motion = Mathf.Abs(_pixels[_index].maxColorComponent - _PreviousFrame[_index].maxColorComponent);
- _output[_index] = (_motion < _DetectMovement) ? _Color:Color.red;
- if ((_motion > _DetectMovement)) _totalmotion++;
- _index++;
- }
- }
- _TargetTexture.SetPixels(_output);
- _TargetTexture.Apply();
- int _totalPixels = _webCamTexture.width * _webCamTexture.height;
- print(_totalmotion * 100 / _totalPixels);
- }
- _PreviousFrame = _pixels;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement