/// <summary> /// Computes the texture map resolution for the current submesh. /// </summary> private void ComputeTextureMapResolution() { int textureMapResolutionInt = _textureMapResolutionMinMax.x; if (textureMapResolutionInt != _textureMapResolutionMinMax.y) { // Compute the submesh's bounds (quick approximation). float boundsRelativeMagnitude = 1f; Mesh globalMesh = PMGlobalMeshEF.globalMesh; if (globalMesh.subMeshCount > 1) { Bounds bounds = new Bounds(); int maxTriangleCount = 100; int[] submeshTriangles = globalMesh.GetTriangles(_submeshIndex); int submeshTriangleCount = submeshTriangles.Length / 3; int skipFactor = Mathf.CeilToInt(submeshTriangleCount * 1f / maxTriangleCount); for (int triangleIndex = 0; triangleIndex < submeshTriangleCount; triangleIndex += 3 * skipFactor) { bounds.Encapsulate(globalMesh.vertices[submeshTriangles[3 * triangleIndex]]); bounds.Encapsulate(globalMesh.vertices[submeshTriangles[3 * triangleIndex + 1]]); bounds.Encapsulate(globalMesh.vertices[submeshTriangles[3 * triangleIndex + 2]]); } boundsRelativeMagnitude = bounds.size.magnitude / globalMesh.bounds.size.magnitude; } // Determine the texture map's resolution. _textureMapResolution = ColorTextureArray.GetCeilPowerOfTwoForImages(cameraSetup.cameraModels) * 4; float textureMapResolutionFloat = Mathf.Max(_textureMapResolution.x, _textureMapResolution.y); textureMapResolutionFloat *= Mathf.Sqrt(boundsRelativeMagnitude); textureMapResolutionInt = Mathf.NextPowerOfTwo(Mathf.RoundToInt(textureMapResolutionFloat)); // Submit to the min and max bounds. textureMapResolutionInt = Mathf.Max(Mathf.Min(textureMapResolutionInt, _textureMapResolutionMinMax.y), _textureMapResolutionMinMax.x); } _textureMapResolution = new Vector2Int(textureMapResolutionInt, textureMapResolutionInt); }
/// <summary> /// Resets the object's properties. /// </summary> public void Reset() { // Check if there is a rendering caller. renderingCaller = GetComponent <Rendering.Rendering>(); // Reset the key child components. _cameraSetup = CameraSetup.CreateOrResetCameraSetup(transform); _dataHandler = DataHandler.CreateOrResetDataHandler(transform); // Initialize the processing methods. _processingMethods = ProcessingMethod.CreateOrResetProcessingMethods(transform); for (int iter = 0; iter < _processingMethods.Length; iter++) { _processingMethods[iter].InitializeLinks(); } // Initialize the external tools. _externalHelpers = ExternalHelper.CreateOrResetExternalHelpers(transform); for (int iter = 0; iter < _externalHelpers.Length; iter++) { _externalHelpers[iter].InitializeLinks(); } // Get the method defining the container for the input images. _previewSourceImagesLoader = _processingMethods[0].PMColorTextureArray; // Reads the acquisition information from the source data directory. ReadAcquisitionInformation(); // Destroys all created method components. foreach (MonoBehaviour component in GetComponents <MonoBehaviour>()) { if (component is IMethodGUI) { DestroyImmediate(component); } } }
/// <summary> /// Coroutine that renders depth maps for each view using a given global mesh, and stores this information as a depth texture array. /// </summary> /// <returns></returns> private IEnumerator StoreDepthMapTextureArrayCoroutine() { // Get the processed asset's name and path in the bundle. string bundledAssetName = GetBundledAssetName(depthMapsAssetName); string depthDataPathRelative = GetAssetPathRelative(bundledAssetName); // Check if the asset has already been processed. if (!dataHandler.IsAssetAlreadyProcessed(depthDataPathRelative)) { // Reset the progress bar. GeneralToolkit.ResetCancelableProgressBar(true, false); // Create and initialize a temporary preview camera manager aimed at storing depth data. PreviewCameraManager previewCameraManager = new GameObject("TempPreviewCameraManager").AddComponent <PreviewCameraManager>(); Transform previewCameraTransform = new GameObject("TempPreviewCamera").transform; GeneralToolkit.CreateRenderTexture(ref previewCameraManager.targetTexture, Vector2Int.one, 24, RenderTextureFormat.RFloat, true, FilterMode.Point, TextureWrapMode.Clamp); previewCameraManager.CreatePreviewCamera(previewCameraManager.gameObject, previewCameraTransform, cameraSetup.cameraModels[0]); // Instantiate the mesh as a set of submeshes, provided with the default material. Material defaultMat = new Material(GeneralToolkit.shaderStandard); GameObject[] submeshGOs = new GameObject[PMGlobalMeshEF.globalMesh.subMeshCount]; for (int i = 0; i < submeshGOs.Length; i++) { submeshGOs[i] = new GameObject("TempMesh_" + i); submeshGOs[i].transform.parent = previewCameraManager.transform; submeshGOs[i].AddComponent <MeshFilter>().sharedMesh = PMGlobalMeshEF.globalMesh; Material[] materials = new Material[submeshGOs.Length]; materials[i] = defaultMat; submeshGOs[i].AddComponent <MeshRenderer>().materials = materials; } // Create an empty texture array in which to store the depth data. Vector2Int arrayResolution; int arrayDepth; ColorTextureArray.GetCorrectedPowerOfTwoForImages(cameraSetup.cameraModels, out arrayResolution, out arrayDepth); depthData = new Texture2DArray(1, 1, 1, TextureFormat.RGB24, false); GeneralToolkit.CreateTexture2DArray(ref depthData, arrayResolution, arrayDepth, TextureFormat.RGB24, false, FilterMode.Point, TextureWrapMode.Clamp, false); // Create a render texture in which to store RFloat depth data, with the array's resolution. RenderTexture arraySliceRFloatRenderTex = new RenderTexture(1, 1, 0); GeneralToolkit.CreateRenderTexture(ref arraySliceRFloatRenderTex, arrayResolution, 24, RenderTextureFormat.RFloat, true, FilterMode.Point, TextureWrapMode.Clamp); // Create a material and render texture to encode the RFloat distance as a RGB color. Material distanceToColorMat = new Material(GeneralToolkit.shaderAcquisitionConvert01ToColor); RenderTexture distanceAsColorTexture = new RenderTexture(1, 1, 0); GeneralToolkit.CreateRenderTexture(ref distanceAsColorTexture, arrayResolution, 0, RenderTextureFormat.ARGB32, true, FilterMode.Point, TextureWrapMode.Clamp); // Create a texture in which to store the RGB-encoded distance. Texture2D arraySliceRGBTex = new Texture2D(1, 1); GeneralToolkit.CreateTexture2D(ref arraySliceRGBTex, arrayResolution, TextureFormat.RGB24, true, FilterMode.Point, TextureWrapMode.Clamp, false); // Create a depth map in each layer of the texture array, corresponding to each source camera. 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; } // Set the preview camera manager's camera model to the current source camera. previewCameraManager.UpdateCameraModel(cameraSetup.cameraModels[i]); // Render the depth data seen by this camera as an RFloat texture. previewCameraManager.RenderPreviewToTarget(ref previewCameraManager.targetTexture, true); // Resize the rendered texture to the output array's resolution. Graphics.Blit(previewCameraManager.targetTexture, arraySliceRFloatRenderTex); // Convert the resized RFloat texture to an RGB encoding. Graphics.Blit(arraySliceRFloatRenderTex, distanceAsColorTexture, distanceToColorMat); // Store the RGB color texture into the texture array. GeneralToolkit.CopyRenderTextureToTexture2D(distanceAsColorTexture, ref arraySliceRGBTex); depthData.SetPixels(arraySliceRGBTex.GetPixels(), i); depthData.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(depthData, depthDataPathRelative); AssetDatabase.Refresh(); } // Destroy the created textures and conversion material. DestroyImmediate(arraySliceRGBTex); DestroyImmediate(distanceAsColorTexture); DestroyImmediate(distanceToColorMat); DestroyImmediate(arraySliceRFloatRenderTex); // Destroy the created meshes and default material. DestroyImmediate(defaultMat); foreach (GameObject submeshGO in submeshGOs) { DestroyImmediate(submeshGO); } // Destroy the preview camera manager. previewCameraManager.DestroyPreviewCamera(); DestroyImmediate(previewCameraManager.gameObject); // Reset the progress bar. GeneralToolkit.ResetCancelableProgressBar(true, false); } Texture2DArray depthDataAsset = AssetDatabase.LoadAssetAtPath <Texture2DArray>(depthDataPathRelative); depthData = (Texture2DArray)Instantiate(depthDataAsset); }