| | 1 | | using DCL.CameraTool; |
| | 2 | | using DCL.Configuration; |
| | 3 | | using DCL.Helpers; |
| | 4 | | using DCL.UIElements.Structures; |
| | 5 | | using Google.Protobuf.Collections; |
| | 6 | | using JetBrains.Annotations; |
| | 7 | | using Decentraland.Common; |
| | 8 | | using UnityEngine; |
| | 9 | | using Quaternion = Decentraland.Common.Quaternion; |
| | 10 | | using Vector3 = Decentraland.Common.Vector3; |
| | 11 | |
|
| | 12 | | namespace DCL.ECSComponents |
| | 13 | | { |
| | 14 | | public static class ProtoConvertUtils |
| | 15 | | { |
| | 16 | | public static RaycastHit ToPBRaycasHit(long entityId, string meshName, Ray ray, HitInfo rawHit) |
| | 17 | | { |
| 0 | 18 | | var hit = new RaycastHit(); |
| 0 | 19 | | hit.Length = rawHit.distance; |
| 0 | 20 | | hit.GlobalOrigin = UnityVectorToPBVector(ray.origin); |
| 0 | 21 | | hit.EntityId = (uint)entityId; |
| 0 | 22 | | hit.MeshName = meshName; |
| 0 | 23 | | hit.Position = UnityVectorToPBVector(rawHit.point); |
| 0 | 24 | | hit.NormalHit = UnityVectorToPBVector(rawHit.normal); |
| 0 | 25 | | hit.Direction = UnityVectorToPBVector(ray.direction); |
| | 26 | |
|
| 0 | 27 | | return hit; |
| | 28 | | } |
| | 29 | |
|
| | 30 | | public static RaycastHit ToPBRaycasHit(long entityId, string meshName, Ray ray, |
| | 31 | | float hitDistance, UnityEngine.Vector3 hitPoint, UnityEngine.Vector3 hitNormal) |
| | 32 | | { |
| 15 | 33 | | var ret = new RaycastHit |
| | 34 | | { |
| | 35 | | Length = hitDistance, |
| | 36 | | GlobalOrigin = UnityVectorToPBVector(ray.origin), |
| | 37 | | Position = UnityVectorToPBVector(hitPoint), |
| | 38 | | NormalHit = UnityVectorToPBVector(hitNormal), |
| | 39 | | Direction = UnityVectorToPBVector(ray.direction), |
| | 40 | | EntityId = (uint)entityId |
| | 41 | | }; |
| | 42 | |
|
| 15 | 43 | | if (!string.IsNullOrEmpty(meshName)) |
| | 44 | | { |
| 0 | 45 | | ret.MeshName = meshName; |
| | 46 | | } |
| | 47 | |
|
| 15 | 48 | | return ret; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | public static Vector3 UnityVectorToPBVector(UnityEngine.Vector3 original) => |
| 135 | 52 | | new() |
| | 53 | | { |
| | 54 | | X = original.x, |
| | 55 | | Y = original.y, |
| | 56 | | Z = original.z |
| | 57 | | }; |
| | 58 | |
|
| | 59 | | public static UnityEngine.Vector3 PBVectorToUnityVector(Vector3 original) => |
| 87 | 60 | | new() |
| | 61 | | { |
| | 62 | | x = original.X, |
| | 63 | | y = original.Y, |
| | 64 | | z = original.Z |
| | 65 | | }; |
| | 66 | |
|
| | 67 | | public static UnityEngine.Quaternion PBQuaternionToUnityQuaternion(Quaternion original) => |
| 6 | 68 | | new() |
| | 69 | | { |
| | 70 | | x = original.X, |
| | 71 | | y = original.Y, |
| | 72 | | z = original.Z, |
| | 73 | | w = original.W |
| | 74 | | }; |
| | 75 | |
|
| | 76 | | public static CameraMode.ModeId PBCameraEnumToUnityEnum(CameraType mode) |
| | 77 | | { |
| | 78 | | switch (mode) |
| | 79 | | { |
| | 80 | | case CameraType.CtFirstPerson: |
| 4 | 81 | | return CameraMode.ModeId.FirstPerson; |
| | 82 | | case CameraType.CtThirdPerson: |
| 0 | 83 | | return CameraMode.ModeId.ThirdPerson; |
| | 84 | | default: |
| 0 | 85 | | return CommonScriptableObjects.cameraMode.Get(); |
| | 86 | | } |
| | 87 | | } |
| | 88 | |
|
| | 89 | | public static CameraType UnityEnumToPBCameraEnum(CameraMode.ModeId mode) |
| | 90 | | { |
| | 91 | | switch (mode) |
| | 92 | | { |
| | 93 | | case CameraMode.ModeId.FirstPerson: |
| 1 | 94 | | return CameraType.CtFirstPerson; |
| | 95 | | case CameraMode.ModeId.ThirdPerson: |
| | 96 | | default: |
| 7 | 97 | | return CameraType.CtThirdPerson; |
| | 98 | | } |
| | 99 | | } |
| | 100 | |
|
| | 101 | | public static Color ToUnityColor(this Color3 color) |
| | 102 | | { |
| 39 | 103 | | return new Color(color.R, color.G, color.B); |
| | 104 | | } |
| | 105 | |
|
| | 106 | | public static Color ToUnityColor(this Color4 color) |
| | 107 | | { |
| 56 | 108 | | return new Color(color.R, color.G, color.B, color.A); |
| | 109 | | } |
| | 110 | |
|
| | 111 | | public static string ToFontName(this Font font) |
| | 112 | | { |
| | 113 | | // TODO: add support for the rest of the fonts and discuss old font deprecation |
| | 114 | | const string SANS_SERIF = "SansSerif"; |
| | 115 | |
|
| | 116 | | switch (font) |
| | 117 | | { |
| | 118 | | case Font.FSansSerif: |
| 31 | 119 | | return SANS_SERIF; |
| | 120 | | default: |
| | 121 | | return SANS_SERIF; |
| | 122 | | } |
| | 123 | | } |
| | 124 | |
|
| | 125 | | public static TextAnchor ToUnityTextAlign(this TextAlignMode align) |
| | 126 | | { |
| | 127 | | switch (align) |
| | 128 | | { |
| | 129 | | case TextAlignMode.TamTopCenter: |
| 0 | 130 | | return TextAnchor.UpperCenter; |
| | 131 | | case TextAlignMode.TamTopLeft: |
| 0 | 132 | | return TextAnchor.UpperLeft; |
| | 133 | | case TextAlignMode.TamTopRight: |
| 0 | 134 | | return TextAnchor.UpperRight; |
| | 135 | |
|
| | 136 | | case TextAlignMode.TamBottomCenter: |
| 5 | 137 | | return TextAnchor.LowerCenter; |
| | 138 | | case TextAlignMode.TamBottomLeft: |
| 0 | 139 | | return TextAnchor.LowerLeft; |
| | 140 | | case TextAlignMode.TamBottomRight: |
| 0 | 141 | | return TextAnchor.LowerRight; |
| | 142 | |
|
| | 143 | | case TextAlignMode.TamMiddleCenter: |
| 0 | 144 | | return TextAnchor.MiddleCenter; |
| | 145 | | case TextAlignMode.TamMiddleLeft: |
| 4 | 146 | | return TextAnchor.MiddleLeft; |
| | 147 | | case TextAlignMode.TamMiddleRight: |
| 1 | 148 | | return TextAnchor.MiddleRight; |
| | 149 | |
|
| | 150 | | default: |
| 0 | 151 | | return TextAnchor.MiddleCenter; |
| | 152 | | } |
| | 153 | | } |
| | 154 | |
|
| | 155 | | public static Vector4 ToUnityBorder([CanBeNull] this BorderRect rect) => |
| 1 | 156 | | rect == null ? Vector4.zero : new Vector4(rect.Left, rect.Top, rect.Right, rect.Bottom); |
| | 157 | |
|
| | 158 | | public static DCLUVs ToDCLUVs([CanBeNull] this RepeatedField<float> uvs) => |
| 1 | 159 | | uvs is not { Count: 8 } |
| | 160 | | ? DCLUVs.Default |
| | 161 | | : new DCLUVs( |
| | 162 | | new UnityEngine.Vector2(uvs[0], uvs[1]), |
| | 163 | | new UnityEngine.Vector2(uvs[2], uvs[3]), |
| | 164 | | new UnityEngine.Vector2(uvs[4], uvs[5]), |
| | 165 | | new UnityEngine.Vector2(uvs[6], uvs[7])); |
| | 166 | |
|
| | 167 | | public static DCLImageScaleMode ToDCLImageScaleMode(this BackgroundTextureMode textureMode) |
| | 168 | | { |
| 1 | 169 | | return textureMode switch |
| | 170 | | { |
| 0 | 171 | | BackgroundTextureMode.Center => DCLImageScaleMode.CENTER, |
| 0 | 172 | | BackgroundTextureMode.Stretch => DCLImageScaleMode.STRETCH, |
| 1 | 173 | | BackgroundTextureMode.NineSlices => DCLImageScaleMode.NINE_SLICES, |
| 0 | 174 | | _ => DCLImageScaleMode.STRETCH |
| | 175 | | }; |
| | 176 | | } |
| | 177 | | } |
| | 178 | | } |