Commit 38c2e903 authored by 16이상민's avatar 16이상민

Assets/Script/CoordinateMapperView.cs:

Refactoring code because the code that sets the buffer is structurally redundant.
Refactoring code because the Release () function is called unnecessarily in the constructor and the internal implementation of the OnDisable () function calls the Release () function when the call syntax is removed.

Assets/Script/CoordinateMapperManager.cs:
Extract method because Update () function of 'CoordinateMapperManager' class is big.
Extract module because 'CoordinateMapperManager' class is big.
parent 44937dcd
using UnityEngine; using UnityEngine;
using System.Collections;
using Windows.Kinect; using Windows.Kinect;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System;
public class CoordinateMapperManager : MonoBehaviour public class CoordinateMapperManager : MonoBehaviour
{ {
private KinectSensor m_pKinectSensor; private KinectSensor _Sensor;
private CoordinateMapper m_pCoordinateMapper; private CoordinateMapper _CoordinateMapper;
private MultiSourceFrameReader m_pMultiSourceFrameReader; private MultiSourceFrameReader _SourceFrameReader;
private DepthSpacePoint[] m_pDepthCoordinates; private DepthSpacePoint[] _DepthCoordinates;
private byte[] pColorBuffer; const int ColorWidth = 1920;
private byte[] pBodyIndexBuffer; const int ColorHeight = 1080;
private ushort[] pDepthBuffer;
private Body[] pBodyBuffer;
const int cDepthWidth = 512;
const int cDepthHeight = 424;
const int cColorWidth = 1920;
const int cColorHeight = 1080;
long frameCount = 0; long frameCount = 0;
double elapsedCounter = 0.0; double elapsedCounter = 0.0;
double fps = 0.0; double fps = 0.0;
Texture2D m_pColorRGBX; Texture2D _ColorRGBX;
bool nullFrame = false; bool nullFrame = false;
public Texture2D ColorTexture
{ get { return _ColorRGBX; } }
public byte[] BodyIndexBuffer
{ get { return SourceBuffer.BodyIndexBuffer; } }
public DepthSpacePoint[] DepthCoordinates
{ get { return _DepthCoordinates; } }
public Body[] BodyBuffer
{ get { return SourceBuffer.BodyBuffer; } }
void Awake() void Awake()
{ {
pColorBuffer = new byte[cColorWidth * cColorHeight * 4]; _ColorRGBX = new Texture2D (ColorWidth, ColorHeight, TextureFormat.RGBA32, false);
pBodyIndexBuffer = new byte[cDepthWidth * cDepthHeight];
pDepthBuffer = new ushort[cDepthWidth * cDepthHeight];
m_pColorRGBX = new Texture2D (cColorWidth, cColorHeight, TextureFormat.RGBA32, false); _DepthCoordinates = new DepthSpacePoint[ColorWidth * ColorHeight];
m_pDepthCoordinates = new DepthSpacePoint[cColorWidth * cColorHeight];
InitializeDefaultSensor (); InitializeDefaultSensor ();
} }
Rect fpsRect = new Rect(10, 10, 200, 30);
Rect nullFrameRect = new Rect(10, 50, 200, 30);
void OnGUI () void OnGUI ()
{ {
GUI.Box (fpsRect, "FPS: " + fps.ToString("0.00")); GUI.Box (new Rect(10, 10, 200, 30), "FPS: " + fps.ToString("0.00"));
if (nullFrame) if (nullFrame)
{ GUI.Box (new Rect(10, 50, 200, 30), "NULL MSFR Frame");
GUI.Box (nullFrameRect, "NULL MSFR Frame");
}
}
public Texture2D GetColorTexture()
{
return m_pColorRGBX;
}
public byte[] GetBodyIndexBuffer()
{
return pBodyIndexBuffer;
}
public DepthSpacePoint[] GetDepthCoordinates()
{
return m_pDepthCoordinates;
}
public Body[] GetBodyBuffer()
{
return pBodyBuffer;
} }
void InitializeDefaultSensor() void InitializeDefaultSensor()
{ {
m_pKinectSensor = KinectSensor.GetDefault(); _Sensor = KinectSensor.GetDefault();
if (m_pKinectSensor == null) if (_Sensor == null)
{ {
UnityEngine.Debug.LogError("No ready Kinect found!"); //TODO: Popup Modal Window
return; Debug.LogError("No ready Kinect found!");
Application.Quit();
} }
// Initialize the Kinect and get coordinate mapper and the frame reader _CoordinateMapper = _Sensor.CoordinateMapper;
m_pCoordinateMapper = m_pKinectSensor.CoordinateMapper;
m_pKinectSensor.Open(); _Sensor.Open();
if (!m_pKinectSensor.IsOpen) if (!_Sensor.IsOpen)
{ {
UnityEngine.Debug.LogError("Kinect sensor has problem!"); //TODO: Popup Modal Window
return; Debug.LogError("Kinect sensor has problem!");
Application.Quit();
} }
m_pMultiSourceFrameReader = m_pKinectSensor.OpenMultiSourceFrameReader(
_SourceFrameReader = _Sensor.OpenMultiSourceFrameReader(
FrameSourceTypes.Color | FrameSourceTypes.Color |
FrameSourceTypes.Depth | FrameSourceTypes.Depth |
FrameSourceTypes.BodyIndex | FrameSourceTypes.BodyIndex |
FrameSourceTypes.Body); FrameSourceTypes.Body);
pBodyBuffer = new Body[m_pKinectSensor.BodyFrameSource.BodyCount];
} }
void ProcessFrame() void ProcessFrame()
{ {
var pDepthData = GCHandle.Alloc(pDepthBuffer, GCHandleType.Pinned); var DepthData = GCHandle.Alloc(SourceBuffer.DepthBuffer, GCHandleType.Pinned);
var pDepthCoordinatesData = GCHandle.Alloc(m_pDepthCoordinates, GCHandleType.Pinned); var pDepthCoordinatesData = GCHandle.Alloc(_DepthCoordinates, GCHandleType.Pinned);
m_pCoordinateMapper.MapColorFrameToDepthSpaceUsingIntPtr( _CoordinateMapper.MapColorFrameToDepthSpaceUsingIntPtr(
pDepthData.AddrOfPinnedObject(), DepthData.AddrOfPinnedObject(),
(uint)pDepthBuffer.Length * sizeof(ushort), (uint)SourceBuffer.DepthBuffer.Length * sizeof(ushort),
pDepthCoordinatesData.AddrOfPinnedObject(), pDepthCoordinatesData.AddrOfPinnedObject(),
(uint)m_pDepthCoordinates.Length); (uint)_DepthCoordinates.Length);
pDepthCoordinatesData.Free(); pDepthCoordinatesData.Free();
pDepthData.Free(); DepthData.Free();
m_pColorRGBX.LoadRawTextureData(pColorBuffer); _ColorRGBX.LoadRawTextureData(SourceBuffer.ColorBuffer);
m_pColorRGBX.Apply (); _ColorRGBX.Apply();
} }
void Update() void GetFPS()
{ {
// Get FPS elapsedCounter += Time.deltaTime;
elapsedCounter+=Time.deltaTime; if (elapsedCounter > 1.0)
if(elapsedCounter > 1.0)
{ {
fps = frameCount / elapsedCounter; fps = frameCount / elapsedCounter;
frameCount = 0; frameCount = 0;
elapsedCounter = 0.0; elapsedCounter = 0.0;
} }
frameCount++;
}
void Update()
{
GetFPS();
if (m_pMultiSourceFrameReader == null) if (_SourceFrameReader == null)
return; return;
var pMultiSourceFrame = m_pMultiSourceFrameReader.AcquireLatestFrame(); var sourceFrame = _SourceFrameReader.AcquireLatestFrame();
if (pMultiSourceFrame == null) if ((nullFrame = sourceFrame == null))
{
nullFrame = true;
return; return;
}
frameCount++; SourceBuffer.UpdateBuffers(sourceFrame);
nullFrame = false;
// Get Depth Frame Data. ProcessFrame();
using(var pDepthFrame = pMultiSourceFrame.DepthFrameReference.AcquireFrame())
if (pDepthFrame != null)
{
var pDepthData = GCHandle.Alloc (pDepthBuffer, GCHandleType.Pinned);
pDepthFrame.CopyFrameDataToIntPtr(pDepthData.AddrOfPinnedObject(), (uint)pDepthBuffer.Length * sizeof(ushort));
pDepthData.Free();
} }
// Get Color Frame Data void OnApplicationQuit()
using(var pColorFrame = pMultiSourceFrame.ColorFrameReference.AcquireFrame()) {
if (pColorFrame != null) _DepthCoordinates = null;
if (_SourceFrameReader != null)
{ {
var pColorData = GCHandle.Alloc (pColorBuffer, GCHandleType.Pinned); _SourceFrameReader.Dispose();
pColorFrame.CopyConvertedFrameDataToIntPtr(pColorData.AddrOfPinnedObject(), (uint)pColorBuffer.Length, ColorImageFormat.Rgba); _SourceFrameReader = null;
pColorData.Free();
} }
// Get BodyIndex Frame Data. if (_Sensor != null)
using(var pBodyIndexFrame = pMultiSourceFrame.BodyIndexFrameReference.AcquireFrame())
if (pBodyIndexFrame != null)
{ {
var pBodyIndexData = GCHandle.Alloc (pBodyIndexBuffer, GCHandleType.Pinned); _Sensor.Close();
pBodyIndexFrame.CopyFrameDataToIntPtr(pBodyIndexData.AddrOfPinnedObject(), (uint)pBodyIndexBuffer.Length); _Sensor = null;
pBodyIndexData.Free(); }
} }
}
// Get Body Frame Data. static class SourceBuffer
using(var pBodyFrame = pMultiSourceFrame.BodyFrameReference.AcquireFrame()) {
if (pBodyFrame != null) private const int DepthWidth = 512;
pBodyFrame.GetAndRefreshBodyData(pBodyBuffer); private const int DepthHeight = 424;
public const int ColorWidth = 1920;
public const int ColorHeight = 1080;
ProcessFrame(); static private byte[] _ColorBuffer = new byte[ColorWidth * ColorHeight * 4],
} _BodyIndexBuffer = new byte[DepthWidth * DepthHeight];
static private ushort[] _DepthBuffer = new ushort[DepthWidth * DepthHeight];
private static Body[] _BodyBuffer = new Body[6];
void OnApplicationQuit() public static byte[] ColorBuffer
{ get { return _ColorBuffer; } }
public static byte[] BodyIndexBuffer
{ get { return _BodyIndexBuffer; } }
public static ushort[] DepthBuffer
{ get { return _DepthBuffer; } }
public static Body[] BodyBuffer
{ get { return _BodyBuffer; } }
public static void UpdateBuffers(MultiSourceFrame sourceFrame)
{ {
pDepthBuffer = null; UpdateColorBuffer (sourceFrame);
pColorBuffer = null; UpdateBodyIndexBuffer(sourceFrame);
pBodyIndexBuffer = null; UpdateDepthBuffer (sourceFrame);
pBodyBuffer = null; UpdateBodyBuffer (sourceFrame);
}
if (m_pDepthCoordinates != null) private static void UpdateColorBuffer(MultiSourceFrame sourceFrame)
m_pDepthCoordinates = null; {
using (var colorFrame = sourceFrame.ColorFrameReference.AcquireFrame())
if (colorFrame != null)
{
var ColorData = GCHandle.Alloc(_ColorBuffer, GCHandleType.Pinned);
colorFrame.CopyConvertedFrameDataToIntPtr(ColorData.AddrOfPinnedObject(), (uint)_ColorBuffer.Length, ColorImageFormat.Rgba);
ColorData.Free();
}
}
if (m_pMultiSourceFrameReader != null) private static void UpdateBodyIndexBuffer(MultiSourceFrame sourceFrame)
{
using (var bodyIndexFrame = sourceFrame.BodyIndexFrameReference.AcquireFrame())
if (bodyIndexFrame != null)
{ {
m_pMultiSourceFrameReader.Dispose(); var BodyIndexData = GCHandle.Alloc(_BodyIndexBuffer, GCHandleType.Pinned);
m_pMultiSourceFrameReader = null; bodyIndexFrame.CopyFrameDataToIntPtr(BodyIndexData.AddrOfPinnedObject(), (uint)_BodyIndexBuffer.Length);
BodyIndexData.Free();
}
} }
if (m_pKinectSensor != null) private static void UpdateDepthBuffer(MultiSourceFrame sourceFrame)
{
using (var depthFrame = sourceFrame.DepthFrameReference.AcquireFrame())
if (depthFrame != null)
{ {
m_pKinectSensor.Close(); var DepthData = GCHandle.Alloc(_DepthBuffer, GCHandleType.Pinned);
m_pKinectSensor = null; depthFrame.CopyFrameDataToIntPtr(DepthData.AddrOfPinnedObject(), (uint)_DepthBuffer.Length * sizeof(ushort));
DepthData.Free();
} }
} }
}
private static void UpdateBodyBuffer(MultiSourceFrame sourceFrame)
{
using (var bodyFrame = sourceFrame.BodyFrameReference.AcquireFrame())
if (bodyFrame != null)
bodyFrame.GetAndRefreshBodyData(_BodyBuffer);
}
}
\ No newline at end of file
...@@ -12,11 +12,9 @@ public class CoordinateMapperView : MonoBehaviour ...@@ -12,11 +12,9 @@ public class CoordinateMapperView : MonoBehaviour
void Start () void Start ()
{ {
Release();
CoordinateMapperManager CoordinateMapperManager = GameObject.Find("Managers").GetComponent<CoordinateMapperManager>(); CoordinateMapperManager CoordinateMapperManager = GameObject.Find("Managers").GetComponent<CoordinateMapperManager>();
GetComponent<Renderer>().material.SetTexture("_MainTex", CoordinateMapperManager.GetColorTexture()); GetComponent<Renderer>().material.SetTexture("_MainTex", CoordinateMapperManager.ColorTexture);
Func<Array, Array, Action<string, ComputeBuffer, int>> setBuffer = (srcPoints, dstPoints) => Func<Array, Array, Action<string, ComputeBuffer, int>> setBuffer = (srcPoints, dstPoints) =>
{ {
...@@ -28,10 +26,10 @@ public class CoordinateMapperView : MonoBehaviour ...@@ -28,10 +26,10 @@ public class CoordinateMapperView : MonoBehaviour
}; };
}; };
setBuffer(CoordinateMapperManager.GetDepthCoordinates(), DepthPoints) setBuffer(CoordinateMapperManager.DepthCoordinates, DepthPoints)
("depthCoordinates", DepthBuffer, sizeof(float) * 2); ("depthCoordinates", DepthBuffer, sizeof(float) * 2);
setBuffer(CoordinateMapperManager.GetBodyIndexBuffer(), BodyIndexPoints) setBuffer(CoordinateMapperManager.BodyIndexBuffer, BodyIndexPoints)
("BodyIndexBuffer", BodyIndexBuffer, sizeof(float)); ("BodyIndexBuffer", BodyIndexBuffer, sizeof(float));
} }
...@@ -43,20 +41,12 @@ public class CoordinateMapperView : MonoBehaviour ...@@ -43,20 +41,12 @@ public class CoordinateMapperView : MonoBehaviour
BodyIndexBuffer.SetData(System.Array.ConvertAll(BodyIndexPoints, System.Convert.ToSingle)); BodyIndexBuffer.SetData(System.Array.ConvertAll(BodyIndexPoints, System.Convert.ToSingle));
} }
private void Release() void OnDisable()
{ {
if (DepthBuffer != null)
DepthBuffer.Release(); DepthBuffer.Release();
if (BodyIndexBuffer != null)
BodyIndexBuffer.Release(); BodyIndexBuffer.Release();
DepthPoints = null; DepthPoints = null;
BodyIndexPoints = null; BodyIndexPoints = null;
} }
void OnDisable()
{
Release();
}
} }
...@@ -34,7 +34,7 @@ public class MotionView : MonoBehaviour { ...@@ -34,7 +34,7 @@ public class MotionView : MonoBehaviour {
_coordinateMapperManager _coordinateMapperManager
= CoordinateMapperManager.GetComponent<CoordinateMapperManager>(); = CoordinateMapperManager.GetComponent<CoordinateMapperManager>();
body = _coordinateMapperManager.GetBodyBuffer(); body = _coordinateMapperManager.BodyBuffer;
IsInitialized = false; IsInitialized = false;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment