2019-11-04 21:39:02 +00:00
|
|
|
|
using System;
|
|
|
|
|
using UnityEngine;
|
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
|
|
|
|
namespace cylvester
|
|
|
|
|
{
|
|
|
|
|
[Serializable]
|
2019-11-09 15:51:15 +00:00
|
|
|
|
class UnityMovementTextureEvent : UnityEvent<Texture2D> { }
|
2019-11-04 21:39:02 +00:00
|
|
|
|
|
|
|
|
|
public class MovementBaker : MonoBehaviour
|
|
|
|
|
{
|
|
|
|
|
[SerializeField] private UnityMovementTextureEvent DifferenceTextureReceived;
|
|
|
|
|
[SerializeField] private ComputeShader computeShader;
|
|
|
|
|
[SerializeField] private float factor = 1;
|
|
|
|
|
|
|
|
|
|
private RenderTexture resultTexture_;
|
|
|
|
|
private RenderTexture previousFrameTexture_;
|
2019-11-09 15:51:15 +00:00
|
|
|
|
private Texture2D texture_;
|
2019-11-04 21:39:02 +00:00
|
|
|
|
private int kernelHandle_;
|
|
|
|
|
|
|
|
|
|
private void Start()
|
|
|
|
|
{
|
|
|
|
|
resultTexture_ = new RenderTexture(512, 424, 0, RenderTextureFormat.R16) {enableRandomWrite = true};
|
|
|
|
|
resultTexture_.Create();
|
|
|
|
|
|
2019-11-09 15:51:15 +00:00
|
|
|
|
texture_ = new Texture2D(512, 424, TextureFormat.R16, false);
|
|
|
|
|
|
2019-11-04 21:39:02 +00:00
|
|
|
|
previousFrameTexture_ = new RenderTexture(512, 424, 0, RenderTextureFormat.R16) {enableRandomWrite = true};
|
|
|
|
|
previousFrameTexture_.Create();
|
|
|
|
|
|
|
|
|
|
kernelHandle_ = computeShader.FindKernel("CSMain");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void OnInfraredFrameReceived(Texture2D texture)
|
|
|
|
|
{
|
|
|
|
|
computeShader.SetTexture(kernelHandle_, "Input", texture);
|
|
|
|
|
computeShader.SetTexture(kernelHandle_, "Previous", previousFrameTexture_);
|
|
|
|
|
computeShader.SetTexture(kernelHandle_, "Result", resultTexture_);
|
|
|
|
|
computeShader.SetFloat("Factor", factor);
|
|
|
|
|
|
|
|
|
|
computeShader.Dispatch(kernelHandle_, 1, 1, 1);
|
2019-11-09 15:51:15 +00:00
|
|
|
|
|
|
|
|
|
RenderTexture.active = resultTexture_;
|
|
|
|
|
texture_.ReadPixels(new Rect(0, 0, resultTexture_.width, resultTexture_.height), 0 ,0 );
|
|
|
|
|
texture_.Apply();
|
2019-11-04 21:39:02 +00:00
|
|
|
|
|
2019-11-09 15:51:15 +00:00
|
|
|
|
DifferenceTextureReceived.Invoke(texture_);
|
2019-11-04 21:39:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|