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 System.Collections;
using Windows.Kinect;
using System.Runtime.InteropServices;
using System;
public class CoordinateMapperManager : MonoBehaviour
{
private KinectSensor m_pKinectSensor;
private CoordinateMapper m_pCoordinateMapper;
private MultiSourceFrameReader m_pMultiSourceFrameReader;
private DepthSpacePoint[] m_pDepthCoordinates;
private byte[] pColorBuffer;
private byte[] pBodyIndexBuffer;
private ushort[] pDepthBuffer;
private Body[] pBodyBuffer;
const int cDepthWidth = 512;
const int cDepthHeight = 424;
const int cColorWidth = 1920;
const int cColorHeight = 1080;
private KinectSensor _Sensor;
private CoordinateMapper _CoordinateMapper;
private MultiSourceFrameReader _SourceFrameReader;
private DepthSpacePoint[] _DepthCoordinates;
const int ColorWidth = 1920;
const int ColorHeight = 1080;
long frameCount = 0;
double elapsedCounter = 0.0;
double fps = 0.0;
Texture2D m_pColorRGBX;
Texture2D _ColorRGBX;
bool nullFrame = false;
void Awake()
{
pColorBuffer = new byte[cColorWidth * cColorHeight * 4];
pBodyIndexBuffer = new byte[cDepthWidth * cDepthHeight];
pDepthBuffer = new ushort[cDepthWidth * cDepthHeight];
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; } }
m_pColorRGBX = new Texture2D (cColorWidth, cColorHeight, TextureFormat.RGBA32, false);
void Awake()
{
_ColorRGBX = new Texture2D (ColorWidth, ColorHeight, TextureFormat.RGBA32, false);
m_pDepthCoordinates = new DepthSpacePoint[cColorWidth * cColorHeight];
_DepthCoordinates = new DepthSpacePoint[ColorWidth * ColorHeight];
InitializeDefaultSensor ();
}
Rect fpsRect = new Rect(10, 10, 200, 30);
Rect nullFrameRect = new Rect(10, 50, 200, 30);
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)
{
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;
GUI.Box (new Rect(10, 50, 200, 30), "NULL MSFR Frame");
}
void InitializeDefaultSensor()
{
m_pKinectSensor = KinectSensor.GetDefault();
if (m_pKinectSensor == null)
_Sensor = KinectSensor.GetDefault();
if (_Sensor == null)
{
UnityEngine.Debug.LogError("No ready Kinect found!");
return;
//TODO: Popup Modal Window
Debug.LogError("No ready Kinect found!");
Application.Quit();
}
_CoordinateMapper = _Sensor.CoordinateMapper;
// Initialize the Kinect and get coordinate mapper and the frame reader
m_pCoordinateMapper = m_pKinectSensor.CoordinateMapper;
m_pKinectSensor.Open();
if (!m_pKinectSensor.IsOpen)
_Sensor.Open();
if (!_Sensor.IsOpen)
{
UnityEngine.Debug.LogError("Kinect sensor has problem!");
return;
}
m_pMultiSourceFrameReader = m_pKinectSensor.OpenMultiSourceFrameReader(
//TODO: Popup Modal Window
Debug.LogError("Kinect sensor has problem!");
Application.Quit();
}
_SourceFrameReader = _Sensor.OpenMultiSourceFrameReader(
FrameSourceTypes.Color |
FrameSourceTypes.Depth |
FrameSourceTypes.BodyIndex |
FrameSourceTypes.Body);
pBodyBuffer = new Body[m_pKinectSensor.BodyFrameSource.BodyCount];
}
void ProcessFrame()
{
var pDepthData = GCHandle.Alloc(pDepthBuffer, GCHandleType.Pinned);
var pDepthCoordinatesData = GCHandle.Alloc(m_pDepthCoordinates, GCHandleType.Pinned);
var DepthData = GCHandle.Alloc(SourceBuffer.DepthBuffer, GCHandleType.Pinned);
var pDepthCoordinatesData = GCHandle.Alloc(_DepthCoordinates, GCHandleType.Pinned);
m_pCoordinateMapper.MapColorFrameToDepthSpaceUsingIntPtr(
pDepthData.AddrOfPinnedObject(),
(uint)pDepthBuffer.Length * sizeof(ushort),
_CoordinateMapper.MapColorFrameToDepthSpaceUsingIntPtr(
DepthData.AddrOfPinnedObject(),
(uint)SourceBuffer.DepthBuffer.Length * sizeof(ushort),
pDepthCoordinatesData.AddrOfPinnedObject(),
(uint)m_pDepthCoordinates.Length);
(uint)_DepthCoordinates.Length);
pDepthCoordinatesData.Free();
pDepthData.Free();
DepthData.Free();
m_pColorRGBX.LoadRawTextureData(pColorBuffer);
m_pColorRGBX.Apply ();
_ColorRGBX.LoadRawTextureData(SourceBuffer.ColorBuffer);
_ColorRGBX.Apply();
}
void GetFPS()
{
elapsedCounter += Time.deltaTime;
if (elapsedCounter > 1.0)
{
fps = frameCount / elapsedCounter;
frameCount = 0;
elapsedCounter = 0.0;
}
frameCount++;
}
void Update()
{
// Get FPS
elapsedCounter+=Time.deltaTime;
if(elapsedCounter > 1.0)
{
fps = frameCount / elapsedCounter;
frameCount = 0;
elapsedCounter = 0.0;
}
GetFPS();
if (m_pMultiSourceFrameReader == null)
if (_SourceFrameReader == null)
return;
var pMultiSourceFrame = m_pMultiSourceFrameReader.AcquireLatestFrame();
if (pMultiSourceFrame == null)
{
nullFrame = true;
var sourceFrame = _SourceFrameReader.AcquireLatestFrame();
if ((nullFrame = sourceFrame == null))
return;
}
frameCount++;
nullFrame = false;
// Get Depth Frame Data.
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
using(var pColorFrame = pMultiSourceFrame.ColorFrameReference.AcquireFrame())
if (pColorFrame != null)
{
var pColorData = GCHandle.Alloc (pColorBuffer, GCHandleType.Pinned);
pColorFrame.CopyConvertedFrameDataToIntPtr(pColorData.AddrOfPinnedObject(), (uint)pColorBuffer.Length, ColorImageFormat.Rgba);
pColorData.Free();
}
// Get BodyIndex Frame Data.
using(var pBodyIndexFrame = pMultiSourceFrame.BodyIndexFrameReference.AcquireFrame())
if (pBodyIndexFrame != null)
{
var pBodyIndexData = GCHandle.Alloc (pBodyIndexBuffer, GCHandleType.Pinned);
pBodyIndexFrame.CopyFrameDataToIntPtr(pBodyIndexData.AddrOfPinnedObject(), (uint)pBodyIndexBuffer.Length);
pBodyIndexData.Free();
}
// Get Body Frame Data.
using(var pBodyFrame = pMultiSourceFrame.BodyFrameReference.AcquireFrame())
if (pBodyFrame != null)
pBodyFrame.GetAndRefreshBodyData(pBodyBuffer);
SourceBuffer.UpdateBuffers(sourceFrame);
ProcessFrame();
ProcessFrame();
}
void OnApplicationQuit()
{
pDepthBuffer = null;
pColorBuffer = null;
pBodyIndexBuffer = null;
pBodyBuffer = null;
if (m_pDepthCoordinates != null)
m_pDepthCoordinates = null;
_DepthCoordinates = null;
if (m_pMultiSourceFrameReader != null)
if (_SourceFrameReader != null)
{
m_pMultiSourceFrameReader.Dispose();
m_pMultiSourceFrameReader = null;
_SourceFrameReader.Dispose();
_SourceFrameReader = null;
}
if (m_pKinectSensor != null)
if (_Sensor != null)
{
m_pKinectSensor.Close();
m_pKinectSensor = null;
_Sensor.Close();
_Sensor = null;
}
}
}
static class SourceBuffer
{
private const int DepthWidth = 512;
private const int DepthHeight = 424;
public const int ColorWidth = 1920;
public const int ColorHeight = 1080;
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];
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)
{
UpdateColorBuffer (sourceFrame);
UpdateBodyIndexBuffer(sourceFrame);
UpdateDepthBuffer (sourceFrame);
UpdateBodyBuffer (sourceFrame);
}
private static void UpdateColorBuffer(MultiSourceFrame sourceFrame)
{
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();
}
}
private static void UpdateBodyIndexBuffer(MultiSourceFrame sourceFrame)
{
using (var bodyIndexFrame = sourceFrame.BodyIndexFrameReference.AcquireFrame())
if (bodyIndexFrame != null)
{
var BodyIndexData = GCHandle.Alloc(_BodyIndexBuffer, GCHandleType.Pinned);
bodyIndexFrame.CopyFrameDataToIntPtr(BodyIndexData.AddrOfPinnedObject(), (uint)_BodyIndexBuffer.Length);
BodyIndexData.Free();
}
}
private static void UpdateDepthBuffer(MultiSourceFrame sourceFrame)
{
using (var depthFrame = sourceFrame.DepthFrameReference.AcquireFrame())
if (depthFrame != null)
{
var DepthData = GCHandle.Alloc(_DepthBuffer, GCHandleType.Pinned);
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
void Start ()
{
Release();
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) =>
{
......@@ -28,10 +26,10 @@ public class CoordinateMapperView : MonoBehaviour
};
};
setBuffer(CoordinateMapperManager.GetDepthCoordinates(), DepthPoints)
setBuffer(CoordinateMapperManager.DepthCoordinates, DepthPoints)
("depthCoordinates", DepthBuffer, sizeof(float) * 2);
setBuffer(CoordinateMapperManager.GetBodyIndexBuffer(), BodyIndexPoints)
setBuffer(CoordinateMapperManager.BodyIndexBuffer, BodyIndexPoints)
("BodyIndexBuffer", BodyIndexBuffer, sizeof(float));
}
......@@ -43,20 +41,12 @@ public class CoordinateMapperView : MonoBehaviour
BodyIndexBuffer.SetData(System.Array.ConvertAll(BodyIndexPoints, System.Convert.ToSingle));
}
private void Release()
{
if (DepthBuffer != null)
DepthBuffer.Release();
if (BodyIndexBuffer != null)
BodyIndexBuffer.Release();
DepthPoints = null;
BodyIndexPoints = null;
}
void OnDisable()
{
Release();
}
DepthBuffer.Release();
BodyIndexBuffer.Release();
DepthPoints = null;
BodyIndexPoints = null;
}
}
......@@ -34,7 +34,7 @@ public class MotionView : MonoBehaviour {
_coordinateMapperManager
= CoordinateMapperManager.GetComponent<CoordinateMapperManager>();
body = _coordinateMapperManager.GetBodyBuffer();
body = _coordinateMapperManager.BodyBuffer;
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