| | 1 | | using System; |
| | 2 | | using System.IO; |
| | 3 | | using UnityEngine; |
| | 4 | |
|
| | 5 | | namespace DCL.Components |
| | 6 | | { |
| | 7 | | public static class DCLTransformUtils |
| | 8 | | { |
| | 9 | | public static unsafe void DecodeTransform(string payload, ref DCLTransform.Model model) |
| | 10 | | { |
| 130246 | 11 | | byte[] bytes = Convert.FromBase64String(payload); |
| 130246 | 12 | | fixed (byte* numPtr = &bytes[0]) |
| | 13 | | { |
| 130246 | 14 | | float* arr = (float*)numPtr; |
| 130246 | 15 | | model.position.x = arr[0]; |
| 130246 | 16 | | model.position.y = arr[1]; |
| 130246 | 17 | | model.position.z = arr[2]; |
| 130246 | 18 | | model.rotation.x = arr[3]; |
| 130246 | 19 | | model.rotation.y = arr[4]; |
| 130246 | 20 | | model.rotation.z = arr[5]; |
| 130246 | 21 | | model.rotation.w = arr[6]; |
| 130246 | 22 | | model.scale.x = arr[7]; |
| 130246 | 23 | | model.scale.y = arr[8]; |
| 130246 | 24 | | model.scale.z = arr[9]; |
| | 25 | | } |
| 130246 | 26 | | } |
| | 27 | |
|
| | 28 | | public static unsafe string EncodeTransform(Vector3 position, Quaternion rotation, Vector3 scale) |
| | 29 | | { |
| | 30 | | unsafe void WriteSingleToStream(float value, Stream stream) |
| | 31 | | { |
| 2460 | 32 | | byte* ptr = (byte*)&value; |
| 2460 | 33 | | stream.WriteByte(ptr[0]); |
| 2460 | 34 | | stream.WriteByte(ptr[1]); |
| 2460 | 35 | | stream.WriteByte(ptr[2]); |
| 2460 | 36 | | stream.WriteByte(ptr[3]); |
| 2460 | 37 | | } |
| | 38 | |
|
| 246 | 39 | | using (var memoryStream = new MemoryStream(40)) |
| | 40 | | { |
| 246 | 41 | | WriteSingleToStream(position.x, memoryStream); |
| 246 | 42 | | WriteSingleToStream(position.y, memoryStream); |
| 246 | 43 | | WriteSingleToStream(position.z, memoryStream); |
| 246 | 44 | | WriteSingleToStream(rotation.x, memoryStream); |
| 246 | 45 | | WriteSingleToStream(rotation.y, memoryStream); |
| 246 | 46 | | WriteSingleToStream(rotation.z, memoryStream); |
| 246 | 47 | | WriteSingleToStream(rotation.w, memoryStream); |
| 246 | 48 | | WriteSingleToStream(scale.x, memoryStream); |
| 246 | 49 | | WriteSingleToStream(scale.y, memoryStream); |
| 246 | 50 | | WriteSingleToStream(scale.z, memoryStream); |
| | 51 | |
|
| 246 | 52 | | return Convert.ToBase64String(memoryStream.GetBuffer()); |
| | 53 | | } |
| 246 | 54 | | } |
| | 55 | | } |
| | 56 | | } |