/// <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);
        }
Esempio n. 2
0
        /// <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);
        }