void Update() { // If currently recording, try to add the next frame data to the current temporary // frame store if (isRecording) { // If the file info in the file saver does not have the mesh dimensions set, // set these using the kinect manager if (!fileSaver.IsMeshDimensionsSet()) { fileSaver.SetMeshDimensions( kinectManager.DepthFrameHeight / KinectMeshGenerator.DownSampleSize, kinectManager.DepthFrameWidth / KinectMeshGenerator.DownSampleSize); } // Retrieve all of the mesh data for storage Vector3[] meshVertices = kinectMesh.GetMeshVertices(); Texture2D colourTexture = kinectManager.ColourTexture; Vector2[] uvs = kinectMesh.GetMeshUvs(); // If this is the first frame being added, set the time since the last frame to 0 // and flip the flag so that all subsequent times are then the time between update // frames float timeSinceLastFrame; if (firstFrame) { timeSinceLastFrame = 0.0f; firstFrame = false; } else { timeSinceLastFrame = Time.deltaTime; } try { tempFrameStore.AddFrame(meshVertices, colourTexture, uvs, timeSinceLastFrame); } catch (KinectFramesStore.FrameStoreFullException) { // If an exception is thrown, the temporary store is full, so copy it into // the data queue recordedDataQueue.Enqueue(new KinectFramesStore(tempFrameStore)); // Then, reinitialise the temporary store and add the current frame to the // new object tempFrameStore = new KinectFramesStore(FramesPerStore); tempFrameStore.AddFrame(meshVertices, colourTexture, uvs, timeSinceLastFrame); } } // If the background thread has not yet started and the data queue is not empty, // add the file serialize and save callback to the thread pool work queue, then // flip the file processing flag if (!IsProcessingFile && recordedDataQueue.Count != 0) { ThreadPool.QueueUserWorkItem(SaveChunkCallback, recordedDataQueue.Dequeue()); IsProcessingFile = true; } }
void Start() { // Cache references to the kinect components that will contain the data // that needs to be stored kinectManager = KinectView.GetComponent <KinectManager>(); kinectMesh = KinectView.GetComponent <KinectMeshGenerator>(); // Cache a reference to the file manager and set its frames per chunk info // attribute fileSaver = gameObject.GetComponent <FileSystemSaver>(); fileSaver.SetFramesPerChunk(FramesPerStore); // Add the background save handlers to the finished serialization event // and finished file info event ChunkFinished += ChunkSaveFinishedHandler; FileInfoFinished += FileInfoFinishedHandler; // Initialise the temporary kinect store object and the queue of store objects tempFrameStore = new KinectFramesStore(FramesPerStore); recordedDataQueue = new Queue <KinectFramesStore>(); // Initialise the flags isRecording = false; firstFrame = true; IsProcessingFile = false; }
void Start() { // Retrieve a reference to the mesh renderer and the mesh object in the mesh filter meshRenderer = gameObject.GetComponent <Renderer>(); // Initialise the next chunk as a null object and initialise the last chunk/frame flags nextChunk = null; lastChunk = false; LastFrame = false; // Set the mesh state to the no file state until it is initialised SetMeshState(MeshState.NoFile); }
private void InitialisePlayback(KinectFramesStore firstChunk, FileSystem.FileInfo fileInfo) { // Store the first chunk and ensure that the frame iterator is at the first position currentChunk = firstChunk; currentChunk.Reset(); // Initialise an empty downsampled mesh as a 2D plane with the correct height and width // attributes that match the recording images InitialiseMeshData(fileInfo.MeshWidth, fileInfo.MeshHeight); // Align the centre of the mesh to its parent container origin, which by default is world // space AlignMeshToWorldOrigin(fileInfo.MeshWidth, fileInfo.MeshHeight, fileInfo.DepthScale, fileInfo.MinReliableDistance, fileInfo.MaxReliableDistance); // Load in the first frame to the mesh by moving the iterator up and then reading // the current frame value currentChunk.MoveNext(); RefreshMeshData(fileInfo.MeshWidth, fileInfo.MeshHeight, currentChunk.Current); // Finally, initialise the timer between frames frameTimeDelta = 0.0f; }
private void CheckForMeshUpdate() { // If on the last frame, simply return out if (LastFrame) { return; } // Otherwise, start by adding the time from the last frame onto // the timer variable frameTimeDelta += Time.deltaTime; if (!lastChunk && nextChunk == null) { // If the last chunk flag is false and the next chunk is null, // get a new one from the playback manager nextChunk = PlaybackManager.GetNextChunk(); if (nextChunk == null) { // If it is still null, there are no more chunks left, so set // the last chunk flag if (!lastChunk) { lastChunk = true; } } } // If the current chunk has a next frame, this check will automatically move // up the iterator position, so store a reference to this next frame KinectFramesStore.KinectFrame nextFrame; if (currentChunk.MoveNext()) { nextFrame = currentChunk.Current; } else { // Otherwise, the chunk is at its last frame, so first check if this is // the last chunk if (lastChunk) { // If it is, flip the last frame flag and return. LastFrame = true; return; } else { // Otherwise, a next chunk is available, so switch this out for the // current chunk and dereference the next chunk pointer currentChunk = new KinectFramesStore(nextChunk); nextChunk = null; // Then, ensure that the iterator component it is at the first position, // move the pointer up by one and store a local reference to the next frame currentChunk.Reset(); currentChunk.MoveNext(); nextFrame = currentChunk.Current; } } // If the timer count is greater than or equal to the time between this frame and // the next, load in the next frame's data to the mesh and reset the frame timer if (frameTimeDelta >= nextFrame.FrameDeltaTime) { RefreshMeshData(PlaybackManager.FileInfoOpen.MeshWidth, PlaybackManager.FileInfoOpen.MeshHeight, nextFrame); frameTimeDelta = 0.0f; } else { // Otherwise, the frame can not be loaded yet, so move the iterator pointer // position back by one so that the next update will carry out the check on // the same frame currentChunk.MovePrev(); } }
public KinectFramesStore GetNextChunk() { // First check to see if any chunks are available in the queue, returning // a null reference if there aren't if (loadedChunkQueue.Count == 0) { return(null); } // Otherwise, dequeue the next chunk KinectFramesStore nextChunk = loadedChunkQueue.Dequeue(); // If the file chunk index is not at the last position, there is more data // left in the file if (fileChunkIndex < FileInfoOpen.ChunkSizes.Count - 1) { // If the loaded chunk queue is now equla to or under the buffer size, // stop playback and load in more chunks to refresh the buffer if (loadedChunkQueue.Count <= ChunkBufferSize) { StopPlaying(); // Check that the number of chunks left in the file is greater than the number // of chunks to load in to refill the buffer. If it is not, use the remaining // number of chunks in the file instead int lastChunkIndex = fileChunkIndex; if (RefreshBufferSize > (FileInfoOpen.ChunkSizes.Count - 1 - fileChunkIndex)) { fileChunkIndex = FileInfoOpen.ChunkSizes.Count - 1; } else { fileChunkIndex += RefreshBufferSize; } // If a chunk is still loading as well, simply queue up all of these new chunks if (IsLoading) { for (int i = lastChunkIndex + 1; i < fileChunkIndex + 1; i++) { chunksToLoadQueue.Enqueue(new ChunkLoadData(GetChunkStartFromIndex(i), FileInfoOpen.ChunkSizes[i])); } } else { // Otherwise, if there is more than one chunk to load, skip over the first chunk // in this new set and queue up all of the others, including the chunk that the // global index is currently on if (fileChunkIndex - lastChunkIndex > 1) { for (int i = lastChunkIndex + 2; i < fileChunkIndex + 1; i++) { chunksToLoadQueue.Enqueue(new ChunkLoadData(GetChunkStartFromIndex(i), FileInfoOpen.ChunkSizes[i])); } } // Then, begin loading in the first of these new chunks ThreadPool.QueueUserWorkItem(ChunkLoadCallback, new ChunkLoadData(GetChunkStartFromIndex(lastChunkIndex + 1), FileInfoOpen.ChunkSizes[lastChunkIndex + 1])); } } else { // Otherwise, progress the file chunk index and load in the next chunk to replace // the one just dequeued fileChunkIndex++; ChunkLoadData newChunkToLoad = new ChunkLoadData(GetChunkStartFromIndex(fileChunkIndex), FileInfoOpen.ChunkSizes[fileChunkIndex]); // If another chunk is already loading, add this new chunk into the chunks to load // queue. Otherwise, manually queue the chunk processing task on the background thread if (IsLoading) { chunksToLoadQueue.Enqueue(newChunkToLoad); } else { ThreadPool.QueueUserWorkItem(ChunkLoadCallback, newChunkToLoad); } } } else if (IsLoading) { // Otherwise, if it is at the last position and loading data in, stop playing so that the // final chunks can be added to the buffer before resuming playing StopPlaying(); } return(nextChunk); }