/// <summary>
        /// Stores an inside-out sphere mesh in the processed assets.
        /// </summary>
        /// <param name="dataHandler"></param> The data handler with which to store the asset.
        public void StoreInsideOutSphereMesh(DataHandler dataHandler)
        {
            // Check if the asset has already been processed.
            string bundledAssetName = dataHandler.GetBundledAssetName(this, sphereFocalSurfaceName);
            string meshRelativePath = Path.Combine(GeneralToolkit.tempDirectoryRelativePath, bundledAssetName + ".asset");

            if (dataHandler.IsAssetAlreadyProcessed(meshRelativePath))
            {
                return;
            }
            // Get the sphere mesh from a sphere primitive.
            Transform  primitiveTransform = GameObject.CreatePrimitive(PrimitiveType.Sphere).transform;
            MeshFilter sphereMeshFilter   = primitiveTransform.GetComponent <MeshFilter>();
            Mesh       sphereMesh         = (Mesh)Instantiate(sphereMeshFilter.sharedMesh);

            // Correct for rotation.
            primitiveTransform.rotation = Quaternion.AngleAxis(-90f, Vector3.up);
            GeneralToolkit.TransformMeshByTransform(ref sphereMesh, primitiveTransform);
            // Make the sphere inside-out.
            int[] triangles = sphereMesh.triangles;
            for (int i = 0; i < triangles.Length; i += 3)
            {
                int t = triangles[i];
                triangles[i]     = triangles[i + 2];
                triangles[i + 2] = t;
            }
            Vector3[] normals = sphereMesh.normals;
            for (int i = 0; i < normals.Length; i++)
            {
                normals[i] = -normals[i];
            }
            Vector2[] UVs = sphereMesh.uv;
            for (int i = 0; i < UVs.Length; i++)
            {
                UVs[i] = new Vector2(1f - UVs[i].x, UVs[i].y);
            }
            sphereMesh.triangles = triangles;
            sphereMesh.normals   = normals;
            sphereMesh.uv        = UVs;
            // Save the mesh as an asset.
            GeneralToolkit.CreateAndUnloadAsset(sphereMesh, meshRelativePath);
            // Destroy the created primitive.
            DestroyImmediate(primitiveTransform.gameObject);
        }
Example #2
0
 /// <summary>
 /// Coroutine that renders per-view meshes from the given depth texture array.
 /// </summary>
 /// <returns></returns>
 private IEnumerator StorePerViewMeshesCoroutine()
 {
     // Reset the progress bar.
     GeneralToolkit.ResetCancelableProgressBar(true, false);
     // Initialize the compute shader's properties.
     PMPerViewMeshesQSTR.InitializePerCall();
     // Create a mesh for each source depth map.
     for (int sourceCamIndex = 0; sourceCamIndex < cameraSetup.cameraModels.Length; sourceCamIndex++)
     {
         // Check if the asset has already been processed.
         string bundledAssetName = dataHandler.GetBundledAssetName(PMPerViewMeshesQSTR, PerViewMeshesQSTR.perViewMeshAssetPrefix + sourceCamIndex);
         string meshRelativePath = Path.Combine(GeneralToolkit.tempDirectoryRelativePath, bundledAssetName + ".asset");
         if (dataHandler.IsAssetAlreadyProcessed(meshRelativePath))
         {
             continue;
         }
         // Update the progress bar, and enable the user to cancel the process.
         PMPerViewMeshesQSTR.DisplayAndUpdateCancelableProgressBar();
         if (GeneralToolkit.progressBarCanceled)
         {
             processingCaller.processingCanceled = true;
             break;
         }
         // Update the camera model.
         PMPerViewMeshesQSTR.cameraModel = cameraSetup.cameraModels[sourceCamIndex];
         // Initialize the distance map texture, and load the depth data into it.
         PMPerViewMeshesQSTR.InitializeDistanceMap();
         Vector2Int    distanceMapResolution  = new Vector2Int(PMPerViewMeshesQSTR.distanceMap.width, PMPerViewMeshesQSTR.distanceMap.height);
         RenderTexture depthTextureArraySlice = new RenderTexture(1, 1, 0);
         GeneralToolkit.CreateRenderTexture(ref depthTextureArraySlice, distanceMapResolution, 0, RenderTextureFormat.ARGB32, true, FilterMode.Point);
         Graphics.Blit(PMDepthTextureArray.depthData, depthTextureArraySlice, sourceCamIndex, 0);
         GeneralToolkit.CopyRenderTextureToTexture2D(depthTextureArraySlice, ref PMPerViewMeshesQSTR.distanceMap);
         DestroyImmediate(depthTextureArraySlice);
         // Compute a mesh from the distance map.
         Mesh meshAsset;
         PMPerViewMeshesQSTR.ComputeMesh(out meshAsset);
         // Save this mesh as an asset.
         GeneralToolkit.CreateAndUnloadAsset(meshAsset, meshRelativePath);
         yield return(null);
     }
     // Reset the progress bar.
     GeneralToolkit.ResetCancelableProgressBar(true, false);
 }
        /// <summary>
        /// Stores a quad mesh in the processed assets.
        /// </summary>
        /// <param name="dataHandler"></param> The data handler with which to store the asset.
        public void StoreQuadMesh(DataHandler dataHandler)
        {
            // Check if the asset has already been processed.
            string bundledAssetName = dataHandler.GetBundledAssetName(this, quadFocalSurfaceName);
            string meshRelativePath = Path.Combine(GeneralToolkit.tempDirectoryRelativePath, bundledAssetName + ".asset");

            if (dataHandler.IsAssetAlreadyProcessed(meshRelativePath))
            {
                return;
            }
            // Get the quad mesh from a quad primitive.
            Transform  primitiveTransform = GameObject.CreatePrimitive(PrimitiveType.Quad).transform;
            MeshFilter quadMeshFilter     = primitiveTransform.GetComponent <MeshFilter>();
            Mesh       quadMesh           = (Mesh)Instantiate(quadMeshFilter.sharedMesh);

            // Save the mesh as an asset.
            GeneralToolkit.CreateAndUnloadAsset(quadMesh, meshRelativePath);
            // Destroy the created primitive.
            DestroyImmediate(primitiveTransform.gameObject);
        }
        /// <summary>
        /// Saves the meshes in the scene as a global mesh asset in the data directory.
        /// </summary>
        public void SaveGlobalMesh()
        {
            // Combines the different meshes in the scene into a single asset.
            Mesh outputMesh = new Mesh();

            MeshFilter[]      meshFilters = FindObjectsOfType <MeshFilter>();
            CombineInstance[] combine     = new CombineInstance[meshFilters.Length];
            for (int i = 0; i < meshFilters.Length; i++)
            {
                combine[i].mesh      = meshFilters[i].sharedMesh;
                combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
            }
            outputMesh.CombineMeshes(combine, false);
            // Saves the asset in the data directory.
            string assetName          = "GlobalMesh.asset";
            string relativeAssetPath  = Path.Combine("Assets", assetName);
            string globalAssetPath    = Path.Combine(Path.GetFullPath(Application.dataPath), assetName);
            string newGlobalAssetPath = Path.Combine(dataHandler.dataDirectory, assetName);

            GeneralToolkit.CreateAndUnloadAsset(outputMesh, relativeAssetPath);
            GeneralToolkit.Replace(PathType.File, globalAssetPath, newGlobalAssetPath);
            GeneralToolkit.Delete(globalAssetPath);
            AssetDatabase.Refresh();
        }