public static void SetTransformValues(Transform transform, CameraLocation camLoc) { Vector3 position; Quaternion rotation; Vector3 localScale; GetTransformValues(camLoc.WorldToCameraTransform, out position, out rotation, out localScale); transform.position = position; transform.rotation = rotation; transform.localScale = localScale; }
public static void UpdateDisplay(Texture2D tex, CameraLocation camLoc) { Texture = tex; Texture.name = tex.name; Container = new GameObject(); SampleProjector = new Projector(); CameraLocation.SetTransformValues(SampleProjector.transform, camLoc); CameraLocation.SetTransformValues(Container.transform, camLoc); SampleProjector.transform.parent = Container.transform; }
public static CameraLocation Load(string filepath) { CameraLocation camLoc = null; if (File.Exists(filepath)) { camLoc = new CameraLocation(); string[] fileLines = File.ReadAllLines(filepath); if (fileLines.Length != 17) { return(null); } // Read in CameraToWorldTransform matrix Matrix4x4 camToWorldTransform = new Matrix4x4(); for (int i = 0; i < 4; i++) { camToWorldTransform.SetRow(i, ReadMatrixRow(fileLines[i + 1])); } // Read in ProjectionTransform matrix Matrix4x4 projectionTransform = new Matrix4x4(); for (int i = 0; i < 4; i++) { projectionTransform.SetRow(i, ReadMatrixRow(fileLines[i + 8])); } // Read in Near Clip Plane float nearClipPlane = float.Parse(fileLines[14]); // Read in Far Clip Plane float farClipPlane = float.Parse(fileLines[16]); // Set CameraLocation data camLoc.HasLocationData = true; camLoc.CameraToWorldTransform = camToWorldTransform; camLoc.ProjectionTransform = projectionTransform; camLoc.WorldToCameraTransform = camToWorldTransform.inverse; camLoc.NearClipPlane = nearClipPlane; camLoc.FarClipPlane = farClipPlane; camLoc.name = GetFileNameWithoutExtension(filepath); } return(camLoc); }
// Save a photo and its location information to a file public static void OnCapturedPhotoToMemory(PhotoCapture.PhotoCaptureResult result, PhotoCaptureFrame frame) { // If photo was taken... if (result.success) { Resolution camRes = Constants.Camera.CameraResolution(); // Generate blank texture canvas Texture2D targetTexture = new Texture2D( camRes.width, // Width of the texture camRes.height, // Height of the texture TextureFormat.BGRA32, // Format of the texture (Hololens image format) false // Don't mipmap the texture ); // Copy the raw image data into our target texture frame.UploadImageDataToTexture(targetTexture); // The image is flipped and MUST BE REORIENTED in a custom shader targetTexture = FlipTexture.FlipHorizontal(targetTexture); // Save the texture to a file targetTexture.name = Constants.Names.TextureAutoName + Constants.Names.TextureAutoSuffixInt; //SaveTexture.Save(targetTexture, RoomTexture.FileSuffixTypes.PNG, RoomTexture.RoomTextureFolderPath); SaveTexture.Save(targetTexture, Constants.Suffixes.ImageSuffixTypes.PNG, Constants.Folders.RoomTextureFolderPath); Destroy(targetTexture); // Save the camera location information to a file CameraLocation camLoc = new CameraLocation(frame); CameraLocation.Save(camLoc, Constants.Camera.CameraLocationAutoName + Constants.Names.TextureAutoSuffixInt); // Adjust the automatic tracker number for the texture/cameraLocation file Constants.Names.TextureAutoSuffixInt++; // Display to DisplayManager most recently saved photo and location DisplayManager.UpdateDisplay(targetTexture, camLoc); } }
/// <summary> /// Save a file representing the stored matrices with the given filename. Assumes that the filename doesn't have an extension (will automatically assign it). /// </summary> /// <param name="cameraLocation"></param> /// <param name="filename"></param> public static void Save(CameraLocation cameraLocation, string filename) { string camToWorldString = "{"; string projectionString = "{"; string nearClipPlaneString = cameraLocation.NearClipPlane.ToString(); string farClipPlaneString = cameraLocation.FarClipPlane.ToString(); if (cameraLocation.hasLocationData) { // Record the Camera To World Transform for (int y = 0; y < 4; y++) { Vector4 rowData = cameraLocation.CameraToWorldTransform.GetRow(y); camToWorldString += Environment.NewLine + rowData.x + Constants.Camera.CameraLocation_MatrixCellSeparator + rowData.y + Constants.Camera.CameraLocation_MatrixCellSeparator + rowData.z + Constants.Camera.CameraLocation_MatrixCellSeparator + rowData.w; } camToWorldString += Environment.NewLine + "}"; // Record the Projection Transform for (int y = 0; y < 4; y++) { Vector4 rowData = cameraLocation.ProjectionTransform.GetRow(y); projectionString += Environment.NewLine + rowData.x + Constants.Camera.CameraLocation_MatrixCellSeparator + rowData.y + Constants.Camera.CameraLocation_MatrixCellSeparator + rowData.z + Constants.Camera.CameraLocation_MatrixCellSeparator + rowData.w; } projectionString += Environment.NewLine + "}"; } else { for (int y = 0; y < 4; y++) { camToWorldString += Environment.NewLine; projectionString += Environment.NewLine; for (int x = 0; x < 3; x++) { camToWorldString += "0" + Constants.Camera.CameraLocation_MatrixCellSeparator; projectionString += "0" + Constants.Camera.CameraLocation_MatrixCellSeparator; } camToWorldString += '0'; projectionString += '0'; } camToWorldString += Environment.NewLine + "}"; projectionString += Environment.NewLine + "}"; } // Create a file or open one for overwriting File.WriteAllText( // Camera location info folder path + (name & file suffix) Constants.Folders.CameraLocationFolderPath + filename + Constants.Suffixes.FileSuffix_CameraLocation, // Contents to write to file camToWorldString + Constants.Camera.CameraLocation_MatrixSeparator + projectionString + Constants.Camera.CameraLocation_MatrixSeparator + nearClipPlaneString + Constants.Camera.CameraLocation_MatrixSeparator + farClipPlaneString ); #if UNITY_EDITOR // Immediately update folders / files shown AssetDatabase.Refresh(); #endif }
// This assumes that all information found on // https://developer.microsoft.com/en-us/windows/holographic/locatable_camera // is correct. // // This version of the method adds the projector to the container since you can't add manipulated components to a GameObject dynamically public static Projector GenerateProjector(GameObject container, string CameraLocationFilePath, string TextureFilePath) { Projector newProjector = null; CameraLocation camLoc = CameraLocation.Load(CameraLocationFilePath); if (camLoc.HasLocationData && container != null) { newProjector = container.AddComponent <Projector>(); // Set the main aspects of the projector int horizontalFOV = CameraLocation.GetHorizontalFOV(Constants.Camera.CameraResolution()); // in degrees newProjector.aspectRatio = 16.0f / 9.0f; newProjector.fieldOfView = horizontalFOV / newProjector.aspectRatio; newProjector.orthographic = false; newProjector.nearClipPlane = camLoc.NearClipPlane; newProjector.farClipPlane = camLoc.FarClipPlane; CameraLocation.SetTransformValues(newProjector.transform, camLoc); // ERROR TESTING REMOVE /* * // Set the position, rotation, and scale of the projector * // This information was found on a forum post at http://answers.unity3d.com/questions/402280/how-to-decompose-a-trs-matrix.html * newProjector.transform.position = camLoc.WorldToCameraTransform.GetColumn(3); * newProjector.transform.rotation = Quaternion.LookRotation( * camLoc.WorldToCameraTransform.GetColumn(2), * camLoc.WorldToCameraTransform.GetColumn(1) * ); * newProjector.transform.localScale = new Vector3( * camLoc.WorldToCameraTransform.GetColumn(0).magnitude, * camLoc.WorldToCameraTransform.GetColumn(1).magnitude, * camLoc.WorldToCameraTransform.GetColumn(2).magnitude * ); */ // Set the name newProjector.name = Constants.Names.Projector_AutoName + camLoc.name; // Generate and set the material Texture2D tex = LoadTexture.Load(TextureFilePath); //Material mat = MaterialMaker.GenerateRoomMaterial(tex, RoomTexture.Projector_MaterialAutoName + camLoc.name); Material mat = MaterialMaker.GenerateRoomMaterial(tex); #if UNITY_EDITOR // ERROR TESTING - DOESN'T EVEN APPLY TO FINAL BUILD SaveMaterial.Save(mat); #endif // AssetDatabase.CreateAsset(mat, RoomTexture.MaterialFolderPath + mat.name); // AssetDatabase.Refresh(); newProjector.material = mat; // Set it to ignore all layers except for the one it needs to project onto int layerID = LayerMask.NameToLayer(Constants.Names.LayerName); int ignoreLayer = 1 << layerID; ignoreLayer = ~ignoreLayer; newProjector.ignoreLayers = ignoreLayer; // ERROR TESTING REMOVE //AssetDatabase.CreateAsset(newProjector, Constants.Folders.ProjectorFolderPath + FileNameTranslator.ClippedTextureToProjector(tex.name)); //AssetDatabase.SaveAssets(); } return(newProjector); }
public static void GetShaderArrays(out Texture2DArray texArray, out Matrix4x4[] worldToCameraMatrixArray, out Matrix4x4[] projectionMatrixArray) { string[] clippedTexFiles = Directory.GetFiles(Constants.Folders.ClippedRoomTextureFolderPath); string[] cameraLocFiles = Directory.GetFiles(Constants.Folders.CameraLocationFolderPath); Resolution camRes = Constants.Camera.CameraResolution(); Resolution downgradedRes = GetDowngradedResolution(camRes); int numLegitimateTextureFiles = 0; foreach (string texFile in clippedTexFiles) { string fileExtension = Path.GetExtension(texFile); if (!fileExtension.Equals(Constants.Suffixes.FileSuffix_PNG) && !fileExtension.Equals(Constants.Suffixes.FileSuffix_JPG)) { continue; } ++numLegitimateTextureFiles; } if (clippedTexFiles.Length > 0) { texArray = new Texture2DArray(downgradedRes.width, downgradedRes.height, numLegitimateTextureFiles, Constants.Camera.Format, false); } else { texArray = null; } worldToCameraMatrixArray = new Matrix4x4[cameraLocFiles.Length]; projectionMatrixArray = new Matrix4x4[cameraLocFiles.Length]; int arrayIndex = 0; foreach (string texFile in clippedTexFiles) { string fileExtension = Path.GetExtension(texFile); if (!fileExtension.Equals(Constants.Suffixes.FileSuffix_PNG) && !fileExtension.Equals(Constants.Suffixes.FileSuffix_JPG)) { continue; } // ERROR TESTING REMOVE // Adjust the texture filepath to ready it for loading by Resources.Load, which requires a relative path with NO file extension //string correctFilepath = Constants.Folders.ClippedRoomTextureFolderPath_Load + Path.GetFileNameWithoutExtension(texFile); Texture2D tex = LoadTexture.Load(texFile); Texture2D downgradedTex = DowngradeTexture(tex, downgradedRes); // copy texture into texture array texArray.SetPixels32(downgradedTex.GetPixels32(), arrayIndex); // ERROR TESTING REMOVE //Graphics.CopyTexture(downgradedTex, 0, 0, texArray, arrayIndex, 0); CameraLocation camLoc = CameraLocation.Load(Constants.Folders.CameraLocationFolderPath + FileNameTranslator.ClippedTextureToCameraLocation(Path.GetFileNameWithoutExtension(texFile)) + Constants.Suffixes.FileSuffix_CameraLocation); if (camLoc != null) { worldToCameraMatrixArray[arrayIndex] = camLoc.WorldToCameraTransform; projectionMatrixArray[arrayIndex] = camLoc.ProjectionTransform; } ++arrayIndex; } }