/// <summary> /// Creates mesh assets for the depth image specified by the given index. /// </summary> /// <param name="acquisitionIndex"></param> The index of the depth image. /// <returns></returns> private void ProcessDepthImage(int acquisitionIndex) { // Check if the asset has already been processed. string bundledAssetName = dataHandler.GetBundledAssetName(this, perViewMeshAssetPrefix + GeneralToolkit.ToString(acquisitionIndex)); string meshRelativePath = Path.Combine(GeneralToolkit.tempDirectoryRelativePath, bundledAssetName + ".asset"); if (dataHandler.IsAssetAlreadyProcessed(meshRelativePath)) { return; } // Update the camera model. cameraModel = cameraSetup.cameraModels[acquisitionIndex]; // Initialize the distance map texture, and load the depth data into it. InitializeDistanceMap(); string imageName = cameraModel.imageName; string imagePath = Path.Combine(dataHandler.depthDirectory, imageName); GeneralToolkit.LoadTexture(imagePath, ref distanceMap); // Compute a mesh from the distance map. Mesh outMesh; ComputeMesh(out outMesh); // Save this mesh as an asset. AssetDatabase.CreateAsset(outMesh, meshRelativePath); AssetDatabase.Refresh(); // Store the per-view mesh into the final array. Mesh meshAsset = AssetDatabase.LoadAssetAtPath <Mesh>(meshRelativePath); perViewMeshes[acquisitionIndex] = (Mesh)Instantiate(meshAsset); }
/// <summary> /// Parses the camera setup from a directory containing an "images" folder with an image dataset from the Stanford Light Field Archive, and saves the parsed setup in this directory. /// </summary> public void ParseCameraSetup() { // Inform of process start. Debug.Log(GeneralToolkit.FormatScriptMessage(this.GetType(), "Started parsing camera setup for an image dataset from the Stanford Light Field Archive located at: " + dataHandler.colorDirectory + ".")); // Get the files in the "images" folder. FileInfo[] fileInfos = GeneralToolkit.GetFilesByExtension(dataHandler.colorDirectory, ".png"); // Determine the pixel resolution of the images. Texture2D tempTex = new Texture2D(1, 1); GeneralToolkit.LoadTexture(fileInfos[0].FullName, ref tempTex); Vector2Int pixelResolution = new Vector2Int(tempTex.width, tempTex.height); DestroyImmediate(tempTex); // Prepare repositioning around center if it is selected. Vector3 meanPos = Vector3.zero; // Reset the camera models to fit the color count. _cameraSetup.ResetCameraModels(); _cameraSetup.cameraModels = new CameraModel[dataHandler.sourceColorCount]; // Iteratively add each camera model to the setup. for (int iter = 0; iter < dataHandler.sourceColorCount; iter++) { CameraModel cameraModel = _cameraSetup.AddCameraModel(iter); // Store the image's pixel resolution in the camera model. cameraModel.pixelResolution = pixelResolution; // Store the image's name in the camera model. FileInfo fileInfo = fileInfos[iter]; cameraModel.SetCameraReferenceIndexAndImageName(cameraModel.cameraReferenceIndex, fileInfo.Name); // Store the image's position in the model. string[] split = fileInfo.Name.Split('_'); float positionY = -GeneralToolkit.ParseFloat(split[split.Length - 3]); float positionX = GeneralToolkit.ParseFloat(split[split.Length - 2]); Vector3 pos = scaleFactor * new Vector3(positionX, positionY, 0); cameraModel.transform.position = pos; meanPos += pos; } // If it is selected, reposition the camera setup around its center position. if (repositionAroundCenter) { meanPos /= dataHandler.sourceColorCount; for (int iter = 0; iter < dataHandler.sourceColorCount; iter++) { CameraModel cameraModel = _cameraSetup.cameraModels[iter]; cameraModel.transform.position = cameraModel.transform.position - meanPos; } } // Temporarily move the color images to a safe location. string tempDirectoryPath = Path.Combine(GeneralToolkit.GetDirectoryBefore(dataHandler.dataDirectory), "temp"); GeneralToolkit.Move(PathType.Directory, dataHandler.colorDirectory, tempDirectoryPath); // Save the camera setup information (this would also have cleared the "images" folder if it was still there). Acquisition.Acquisition.SaveAcquisitionInformation(dataHandler, cameraSetup); // Move the color images back into their original location. GeneralToolkit.Delete(dataHandler.colorDirectory); GeneralToolkit.Move(PathType.Directory, tempDirectoryPath, dataHandler.colorDirectory); // Update the camera models of the setup object. _cameraSetup.FindCameraModels(); // Inform of end of process. Debug.Log(GeneralToolkit.FormatScriptMessage(this.GetType(), "Finished parsing camera setup. Result can be previewed in the Scene view.")); }
/// <inheritdoc/> protected override void ProvideDepthTextureToGeometryProcessingMethod() { // Load the depth map and provide it to the geometry processing method. string imagePath = Path.Combine(processing.dataHandler.depthDirectory, GetCameraModel().imageName); GeneralToolkit.LoadTexture(imagePath, ref _geometryProcessingMethod.distanceMap); // Convert the precise depth map encoding to one more fitted for visualization. Material preciseToVizMat = new Material(GeneralToolkit.shaderDebuggingConvertPreciseToVisualization); Vector2 minMax01 = GetMinMax01FromDepthMap(); preciseToVizMat.SetVector(_shaderNameMinMax01, minMax01); Graphics.Blit(_geometryProcessingMethod.distanceMap, _visualizationTexture, preciseToVizMat); DestroyImmediate(preciseToVizMat); }
/// <inheritdoc/> protected override IEnumerator ExecuteMethodCoroutine() { // Get the processed asset's name and path in the bundle. string bundledAssetName = GetBundledAssetName(colorDataAssetName); string colorDataPathRelative = GetAssetPathRelative(bundledAssetName); // Check if the asset has already been processed. if (!dataHandler.IsAssetAlreadyProcessed(colorDataPathRelative)) { // Reset the progress bar. GeneralToolkit.ResetCancelableProgressBar(true, false); // Determine the resolution and depth that should be given to the texture array. Vector2Int arrayResolution; int arrayDepth; GetCorrectedPowerOfTwoForImages(cameraSetup.cameraModels, out arrayResolution, out arrayDepth); // Create an empty texture array. colorData = new Texture2DArray(1, 1, 1, TextureFormat.RGBA32, useMipMaps); GeneralToolkit.CreateTexture2DArray(ref colorData, arrayResolution, arrayDepth, TextureFormat.RGB24, false, FilterMode.Point, TextureWrapMode.Clamp, useMipMaps); // Create an empty texture, with the array's resolution. Texture2D arraySlice = new Texture2D(1, 1); GeneralToolkit.CreateTexture2D(ref arraySlice, arrayResolution, TextureFormat.RGB24, false, FilterMode.Point, TextureWrapMode.Clamp, useMipMaps); // Create an empty texture, in which we will load the set of source images one-by-one. Texture2D loadTex = new Texture2D(1, 1); // Process as many images as possible from the set of source images. for (int i = 0; i < arrayDepth; i++) { // Update the progress bar, and enable the user to cancel the process. DisplayAndUpdateCancelableProgressBar(); if (GeneralToolkit.progressBarCanceled) { processingCaller.processingCanceled = true; break; } // Load the camera model. CameraModel cameraModel = cameraSetup.cameraModels[i]; // Load the image into a texture object. string imagePath = Path.Combine(dataHandler.colorDirectory, cameraModel.imageName); GeneralToolkit.CreateTexture2D(ref loadTex, cameraModel.pixelResolution, TextureFormat.RGB24, false, FilterMode.Point, TextureWrapMode.Clamp, useMipMaps); GeneralToolkit.LoadTexture(imagePath, ref loadTex); // Resize the texture so that it fits the array's resolution. GeneralToolkit.ResizeTexture2D(loadTex, ref arraySlice); // Add the texture to the texture array. colorData.SetPixels(arraySlice.GetPixels(), i); colorData.Apply(); yield return(null); } // If the user has not canceled the process, continue. if (!GeneralToolkit.progressBarCanceled) { // Create an asset from this texture array. AssetDatabase.CreateAsset(colorData, colorDataPathRelative); AssetDatabase.Refresh(); } // Destroy created objects. DestroyImmediate(loadTex); DestroyImmediate(arraySlice); // Reset the progress bar. GeneralToolkit.ResetCancelableProgressBar(true, false); } Texture2DArray colorDataAsset = AssetDatabase.LoadAssetAtPath <Texture2DArray>(colorDataPathRelative); colorData = (Texture2DArray)Instantiate(colorDataAsset); }