/// <summary> /// Scan the room and return the scaned room as a RoomMesh (serialized as a byte array) /// This method can ONLY be used by HoloLens /// </summary> /// <returns>Serialized Room Mesh</returns> public byte[] LoadMesh() { SpatialMappingManager mappingManager = GetComponent <SpatialMappingManager>(); List <MeshFilter> meshFilters = mappingManager.GetMeshFilters(); List <Mesh> meshes = new List <Mesh>(); foreach (var meshFilter in meshFilters) { Mesh mesh = meshFilter.sharedMesh; Mesh clone = new Mesh(); List <Vector3> verts = new List <Vector3>(); verts.AddRange(mesh.vertices); for (int i = 0; i < verts.Count; i++) { verts[i] = meshFilter.transform.TransformPoint(verts[i]); } clone.SetVertices(verts); clone.SetTriangles(mesh.triangles, 0); meshes.Add(clone); } return(SimpleMeshSerializer.Serialize(meshes)); }
public static void LoadMesh() { // Master client will load mesh from disk if (Type == DeviceType.MasterClient) { _mesh = SimpleMeshSerializer.Serialize(MeshSaver.Load("RoomMesh")); } // HoloLens will scan the room and creates mesh else if (Type == DeviceType.HoloLens) { // The following code is copying from UWB-ARSandbox/Assets/Scripts/netWorkManager.cs:sendMeshToUnity() List <MeshFilter> meshFilters = MappingManager.GetMeshFilters(); List <Mesh> meshes = new List <Mesh>(); foreach (var meshFilter in meshFilters) { Mesh mesh = meshFilter.sharedMesh; Mesh clone = new Mesh(); List <Vector3> verts = new List <Vector3>(); verts.AddRange(mesh.vertices); for (int i = 0; i < verts.Count; i++) { verts[i] = meshFilter.transform.TransformPoint(verts[i]); } clone.SetVertices(verts); clone.SetTriangles(mesh.triangles, 0); meshes.Add(clone); } _mesh = SimpleMeshSerializer.Serialize(meshes); } }
/// <summary> /// Saves the provided meshes to the specified file. /// </summary> /// <param name="fileName">Name to give the saved mesh file. Exclude path and extension.</param> /// <param name="meshes">The collection of Mesh objects to save.</param> /// <returns>Fully qualified name of the saved mesh file.</returns> /// <remarks>Determines the save path to use and automatically applies the file extension.</remarks> public static string Save(string fileName, IEnumerable <Mesh> meshes) { if (string.IsNullOrEmpty(fileName)) { throw new ArgumentException("Must specify a valid fileName."); } if (meshes == null) { throw new ArgumentNullException("Value of meshes cannot be null."); } // Create the mesh file. String folderName = MeshFolderName; Debug.Log(String.Format("Saving mesh file: {0}", Path.Combine(folderName, fileName + fileExtension))); using (Stream stream = OpenFileForWrite(folderName, fileName + fileExtension)) { // Serialize and write the meshes to the file. byte[] data = SimpleMeshSerializer.Serialize(meshes); stream.Write(data, 0, data.Length); stream.Flush(); } Debug.Log("Mesh file saved."); return(Path.Combine(folderName, fileName + fileExtension)); }
private void SendMesh(Mesh m) { Debug.Log("Sending mesh..."); byte[] serializedMesh = SimpleMeshSerializer.Serialize(new Mesh[] { mesh }); Debug.LogFormat("Serialized mesh size: {0} KB", serializedMesh.LongLength / 1000); byte[] compressedSerializedMesh = CLZF2.Compress(serializedMesh); Debug.LogFormat("Compressed serialized mesh size: {0} KB", compressedSerializedMesh.LongLength / 1000); SendMeshHeader(compressedSerializedMesh); StartCoroutine(SendMeshChunks(compressedSerializedMesh)); }
public void SendSpatialMapping() { List <MeshFilter> meshes = SpatialMappingManager.Instance.GetMeshFilters(); byte[] meshData = SimpleMeshSerializer.Serialize(meshes); //SpatialMappingManager.Instance.DrawVisualMeshes = true; LiveMessageResponseSpatialMapping msg = new LiveMessageResponseSpatialMapping(); msg.mapData = meshData; Debug.Log("Spatial Mapping bytes len=" + meshData.Length); Debug.Log("Send Spatial Mapping!"); syncClient.SendMessage(msg.Serialize()); }
public async void sendMesh(Mesh m, Vector3 location, Quaternion rotation) { if (!connected) { return; } try { //SendHeadsetLocation(); List <Mesh> meshes = new List <Mesh>(); meshes.Add(m); byte[] meshData = SimpleMeshSerializer.Serialize(meshes); byte[] bytes = new byte[36]; // 4 bytes per float System.Buffer.BlockCopy(BitConverter.GetBytes(36 + meshData.Length), 0, bytes, 0, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(1), 0, bytes, 4, 4); //type of packet System.Buffer.BlockCopy(BitConverter.GetBytes(location.x), 0, bytes, 8, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(location.y), 0, bytes, 12, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(location.z), 0, bytes, 16, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.x), 0, bytes, 20, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.y), 0, bytes, 24, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.z), 0, bytes, 28, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.w), 0, bytes, 32, 4); byte[] sendData = Combine(bytes, meshData); if (sendData.Length > 0) { enqueueOutgoing(sendData); } SendHeadsetLocation(); } catch (Exception e) { Debug.Log(e.ToString()); return; } //Debug.Log("Sent: " + sendData.Length + " bytes"); }
public static void UpdateMesh(IEnumerable <Mesh> newMesh) { _mesh = SimpleMeshSerializer.Serialize(newMesh); _lastestMeshVersion++; }
/* * public void captureImageData() * { * * Resolution cameraResolution = UnityEngine.XR.WSA.WebCam.PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First(); * targetTexture = new Texture2D(cameraResolution.width, cameraResolution.height); * * // Create a PhotoCapture object * UnityEngine.XR.WSA.WebCam.PhotoCapture.CreateAsync(false, delegate (UnityEngine.XR.WSA.WebCam.PhotoCapture captureObject) { * photoCaptureObject = captureObject; * UnityEngine.XR.WSA.WebCam.CameraParameters cameraParameters = new UnityEngine.XR.WSA.WebCam.CameraParameters(); * cameraParameters.hologramOpacity = 0.0f; * cameraParameters.cameraResolutionWidth = cameraResolution.width; * cameraParameters.cameraResolutionHeight = cameraResolution.height; * cameraParameters.pixelFormat = UnityEngine.XR.WSA.WebCam.CapturePixelFormat.BGRA32; * * // Activate the camera * photoCaptureObject.StartPhotoModeAsync(cameraParameters, delegate (UnityEngine.XR.WSA.WebCam.PhotoCapture.PhotoCaptureResult result) { * // Take a picture * photoCaptureObject.TakePhotoAsync(OnCapturedPhotoToMemory); * }); * }); * } * * void OnCapturedPhotoToMemory(UnityEngine.XR.WSA.WebCam.PhotoCapture.PhotoCaptureResult result, UnityEngine.XR.WSA.WebCam.PhotoCaptureFrame photoCaptureFrame) * { * // Copy the raw image data into the target texture * photoCaptureFrame.UploadImageDataToTexture(targetTexture); * * // Create a GameObject to which the texture can be applied * GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad); * Renderer quadRenderer = quad.GetComponent<Renderer>() as Renderer; * quadRenderer.material = new Material(Shader.Find("Custom/Unlit/UnlitTexture")); * * quad.transform.parent = this.transform; * quad.transform.localPosition = new Vector3(0.0f, 0.0f, 3.0f); * * quadRenderer.material.SetTexture("_MainTex", targetTexture); * * // Deactivate the camera * photoCaptureObject.StopPhotoModeAsync(OnStoppedPhotoMode); * } * * void OnStoppedPhotoMode(UnityEngine.XR.WSA.WebCam.PhotoCapture.PhotoCaptureResult result) * { * // Shutdown the photo capture resource * photoCaptureObject.Dispose(); * photoCaptureObject = null; * } */ public async void sendMesh(Mesh m, Vector3 location, Quaternion rotation) { if (!connected) { return; } #if !UNITY_EDITOR try { List <Mesh> meshes = new List <Mesh>(); meshes.Add(m); byte[] meshData = SimpleMeshSerializer.Serialize(meshes); //byte[] vectBytes = BitConverter.GetBytes(location); //byte[] quatBytes = BitConverter.GetBytes(rotation); byte[] bytes = new byte[4 + 12 + 16]; // 4 bytes per float System.Buffer.BlockCopy(BitConverter.GetBytes(32 + meshData.Length), 0, bytes, 0, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(location.x), 0, bytes, 4, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(location.y), 0, bytes, 8, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(location.z), 0, bytes, 12, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.x), 0, bytes, 16, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.y), 0, bytes, 20, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.z), 0, bytes, 24, 4); System.Buffer.BlockCopy(BitConverter.GetBytes(rotation.w), 0, bytes, 28, 4); //byte[] sendData = Compress(Combine(bytes, meshData)); byte[] sendData = Combine(bytes, meshData); //byte[] sendData = Compress(Combine(bytes, SimpleMeshSerializer.Serialize(meshes))); //Byte[] sendData = Encoding.ASCII.GetBytes("WEEEEEE!"); //udpClient.Send(sendData, sendData.Length); //safety catch for huge items //temp for testing //byte[] sendData = meshData; if (sendData.Length > 200000) { Debug.Log("Packet of length " + sendData.Length + " waiting to go out... But can't.. Because it is probably too huge..."); return; } DataWriter writer = new DataWriter(outputStream); writer.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8; writer.ByteOrder = Windows.Storage.Streams.ByteOrder.LittleEndian; //StreamWriter writer = new StreamWriter(outputStream); //writer.BaseStream.Write(sendData,0,sendData.Length); writer.WriteBytes(sendData); await writer.StoreAsync(); await writer.FlushAsync(); Debug.Log("Sent " + sendData.Length + " bytes."); writer.DetachStream(); //writer.WriteBytes(sendData); //await writer.FlushAsync(); //await writer.StoreAsync(); } catch (Exception e) { Debug.Log(e.ToString()); return; } //Debug.Log("Sent: " + sendData.Length + " bytes"); #endif }