Structs.cs 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using System.Linq;
  5. using System.Threading;
  6. using System.Runtime.InteropServices;
  7. using Urho.Physics;
  8. using Urho.Gui;
  9. using Urho.Urho2D;
  10. using Urho.Resources;
  11. namespace Urho {
  12. [StructLayout (LayoutKind.Sequential)]
  13. public struct Ray {
  14. public Vector3 Origin;
  15. public Vector3 Direction;
  16. public Ray(Vector3 origin, Vector3 direction)
  17. {
  18. Origin = origin;
  19. Direction = Vector3.Normalize(direction);
  20. }
  21. public override bool Equals (object obj)
  22. {
  23. if (!(obj is Ray))
  24. return false;
  25. return (this == (Ray) obj);
  26. }
  27. public static bool operator == (Ray left, Ray right)
  28. {
  29. return ((left.Origin == right.Origin) && (left.Direction == right.Direction));
  30. }
  31. public static bool operator != (Ray left, Ray right)
  32. {
  33. return ((left.Origin != right.Origin) || (left.Direction != right.Direction));
  34. }
  35. public Vector3 Project (Vector3 point)
  36. {
  37. var offset = point - Origin;
  38. return Origin + Vector3.Dot (offset, Direction) * Direction;
  39. }
  40. public override int GetHashCode ()
  41. {
  42. return Origin.GetHashCode () + Direction.GetHashCode ();
  43. }
  44. public float Distance (Vector3 point)
  45. {
  46. var projected = Project (point);
  47. return (point - projected).Length;
  48. }
  49. public Vector3 ClosestPoint (Ray otherRay)
  50. {
  51. var p13 = Origin - otherRay.Origin;
  52. var p43 = otherRay.Direction;
  53. Vector3 p21 = Direction;
  54. float d1343 = Vector3.Dot (p13, p43);
  55. float d4321 = Vector3.Dot (p43, p21);
  56. float d1321 = Vector3.Dot (p13, p21);
  57. float d4343 = Vector3.Dot (p43, p43);
  58. float d2121 = Vector3.Dot (p21, p21);
  59. float d = d2121 * d4343 - d4321 * d4321;
  60. if (Math.Abs(d) < float.Epsilon)
  61. return Origin;
  62. float n = d1343 * d4321 - d1321 * d4343;
  63. float a = n / d;
  64. return Origin + a * Direction;
  65. }
  66. public float HitDistance (Plane plane)
  67. {
  68. float d = Vector3.Dot (plane.Normal, Direction);
  69. if (Math.Abs (d) >= float.Epsilon) {
  70. float t = -(Vector3.Dot (plane.Normal, Origin) + plane.D) / d;
  71. if (t >= 0.0f)
  72. return t;
  73. else
  74. return float.PositiveInfinity;
  75. } else
  76. return float.PositiveInfinity;
  77. }
  78. }
  79. [StructLayout (LayoutKind.Sequential)]
  80. public struct IntRect : IEquatable<IntRect> {
  81. public int Left, Top, Right, Bottom;
  82. public IntRect (int left, int top, int right, int bottom)
  83. {
  84. Left = left;
  85. Top = top;
  86. Right = right;
  87. Bottom = bottom;
  88. }
  89. public static bool operator ==(IntRect left, IntRect right)
  90. {
  91. return left.Equals(right);
  92. }
  93. public static bool operator !=(IntRect left, IntRect right)
  94. {
  95. return !left.Equals(right);
  96. }
  97. public bool Equals(IntRect other)
  98. {
  99. return Left == other.Left
  100. && Top == other.Top
  101. && Right == other.Right
  102. && Bottom == other.Bottom;
  103. }
  104. public override bool Equals(object obj)
  105. {
  106. if (ReferenceEquals(null, obj)) return false;
  107. return obj is IntRect && Equals((IntRect)obj);
  108. }
  109. public override int GetHashCode()
  110. {
  111. int hash = 37;
  112. hash = hash * 43 + Left;
  113. hash = hash * 43 + Top;
  114. hash = hash * 43 + Bottom;
  115. hash = hash * 43 + Right;
  116. return hash;
  117. }
  118. }
  119. [StructLayout (LayoutKind.Sequential)]
  120. public unsafe struct TypeInfo {
  121. public StringHash Type;
  122. public UrhoString TypeName;
  123. public TypeInfo* BaseTypeInfo;
  124. }
  125. [StructLayout (LayoutKind.Sequential)]
  126. public struct Rect : IEquatable<Rect> {
  127. public Vector2 Min, Max;
  128. public Rect (int left, int top, int right, int bottom)
  129. {
  130. Min = new Vector2 (left, top);
  131. Max = new Vector2 (right, bottom);
  132. }
  133. public Rect (Vector2 min, Vector2 max)
  134. {
  135. Min = min;
  136. Max = max;
  137. }
  138. public static bool operator ==(Rect left, Rect right)
  139. {
  140. return left.Equals(right);
  141. }
  142. public static bool operator !=(Rect left, Rect right)
  143. {
  144. return !left.Equals(right);
  145. }
  146. public bool Equals(Rect other)
  147. {
  148. return Min.Equals(other.Min) && Max.Equals(other.Max);
  149. }
  150. public override bool Equals(object obj)
  151. {
  152. if (ReferenceEquals(null, obj)) return false;
  153. return obj is Rect && Equals((Rect)obj);
  154. }
  155. public override int GetHashCode()
  156. {
  157. int hash = 37;
  158. hash = hash * 43 + Min.GetHashCode();
  159. hash = hash * 43 + Max.GetHashCode();
  160. return hash;
  161. }
  162. }
  163. [StructLayout (LayoutKind.Sequential)]
  164. public struct ResourceRef {
  165. public StringHash Type;
  166. public UrhoString Name;
  167. }
  168. [StructLayout (LayoutKind.Sequential)]
  169. public struct HashIteratorBase {
  170. }
  171. [StructLayout (LayoutKind.Sequential)]
  172. public struct Iterator {
  173. }
  174. [StructLayout (LayoutKind.Sequential)]
  175. public struct ResourceRefList {
  176. }
  177. [StructLayout (LayoutKind.Sequential)]
  178. public struct BoundingBox {
  179. public Vector3 Min;
  180. public float DummyMin;
  181. public Vector3 Max;
  182. public float DummyMax;
  183. internal BoundingBox (bool x)
  184. {
  185. DummyMax = 0;
  186. DummyMin = 0;
  187. Min.X = float.PositiveInfinity;
  188. Min.Y = float.PositiveInfinity;
  189. Min.Z = float.PositiveInfinity;
  190. Max.X = float.NegativeInfinity;
  191. Max.Y = float.NegativeInfinity;
  192. Max.Z = float.NegativeInfinity;
  193. }
  194. public BoundingBox (float min, float max)
  195. {
  196. DummyMax = 0;
  197. DummyMin = 0;
  198. Min = new Vector3 (min, min, min);
  199. Max = new Vector3 (max, max, max);
  200. }
  201. public BoundingBox (Vector3 min, Vector3 max)
  202. {
  203. DummyMax = 0;
  204. DummyMin = 0;
  205. Min = min;
  206. Max = max;
  207. }
  208. public void Merge (Vector3 point)
  209. {
  210. if (point.X < Min.X)
  211. Min.X = point.X;
  212. if (point.Y < Min.Y)
  213. Min.Y = point.Y;
  214. if (point.Z < Min.Z)
  215. Min.Z = point.Z;
  216. if (point.X > Max.X)
  217. Max.X = point.X;
  218. if (point.Y > Max.Y)
  219. Max.Y = point.Y;
  220. if (point.Z > Max.Z)
  221. Max.Z = point.Z;
  222. }
  223. public void Merge (BoundingBox box)
  224. {
  225. if (box.Min.X < Min.X)
  226. Min.X = box.Min.X;
  227. if (box.Min.Y < Min.Y)
  228. Min.Y = box.Min.Y;
  229. if (box.Min.Z < Min.Z)
  230. Min.Z = box.Min.Z;
  231. if (box.Max.X > Max.X)
  232. Max.X = box.Max.X;
  233. if (box.Max.Y > Max.Y)
  234. Max.Y = box.Max.Y;
  235. if (box.Max.Z > Max.Z)
  236. Max.Z = box.Max.Z;
  237. }
  238. public void Merge (Vector3 [] points)
  239. {
  240. if (points == null)
  241. throw new ArgumentNullException (nameof (points));
  242. foreach (var p in points)
  243. Merge (p);
  244. }
  245. public void Merge (Frustum frustum)
  246. {
  247. if (frustum == null)
  248. throw new ArgumentNullException (nameof (frustum));
  249. foreach (var p in frustum.Vertices)
  250. Merge (p);
  251. }
  252. public BoundingBox (Vector3 [] points) : this (true)
  253. {
  254. Merge (points);
  255. }
  256. public BoundingBox (Rect rect) : this (new Vector3 (rect.Min.X, rect.Min.Y, 0), new Vector3 (rect.Max.X, rect.Max.Y, 0))
  257. {
  258. }
  259. public BoundingBox (Frustum frustum) : this (true)
  260. {
  261. Merge (frustum);
  262. }
  263. public bool Defined ()
  264. {
  265. return Min.X != float.PositiveInfinity;
  266. }
  267. public Vector3 Center {
  268. get {
  269. return (Max + Min) * 0.5f;
  270. }
  271. }
  272. public Vector3 Size {
  273. get {
  274. return Max - Min;
  275. }
  276. }
  277. public Vector3 HalfSize {
  278. get {
  279. return (Max - Min)*0.5f;
  280. }
  281. }
  282. public Intersection IsInside (Vector3 point)
  283. {
  284. if (point.X < Min.X || point.X > Max.X ||
  285. point.Y < Min.Y || point.Y > Max.Y ||
  286. point.Z < Min.Z || point.Z > Max.Z)
  287. return Intersection.Outside;
  288. return Intersection.Inside;
  289. }
  290. public Intersection IsInside (BoundingBox box)
  291. {
  292. if (box.Max.X < Min.X || box.Min.X > Max.X ||
  293. box.Max.Y < Min.Y || box.Min.Y > Max.Y ||
  294. box.Max.Z < Min.Z || box.Min.Z > Max.Z)
  295. return Intersection.Outside;
  296. else if (box.Min.X < Min.X || box.Max.X > Max.X ||
  297. box.Min.Y < Min.Y || box.Max.Y > Max.Y ||
  298. box.Min.Z < Min.Z || box.Max.Z > Max.Z)
  299. return Intersection.Intersects;
  300. else
  301. return Intersection.Inside;
  302. }
  303. public Intersection IsInsideFast (BoundingBox box)
  304. {
  305. if (box.Max.X < Min.X || box.Min.X > Max.X ||
  306. box.Max.Y < Min.Y || box.Min.Y > Max.Y ||
  307. box.Max.Z < Min.Z || box.Min.Z > Max.Z)
  308. return Intersection.Outside;
  309. else
  310. return Intersection.Inside;
  311. }
  312. public BoundingBox Clip (BoundingBox box)
  313. {
  314. return new BoundingBox (new Vector3 (Math.Max (box.Min.X, Min.X),
  315. Math.Max (box.Min.Y, Min.Y),
  316. Math.Max (box.Min.Z, Min.Z)),
  317. new Vector3 (Math.Min (box.Max.X, Max.X),
  318. Math.Min (box.Max.Y, Max.Y),
  319. Math.Min (box.Max.Z, Max.Z)));
  320. }
  321. }
  322. [StructLayout(LayoutKind.Explicit)]
  323. public struct AnimationTriggerPoint {
  324. [FieldOffset(0)]
  325. public float Time;
  326. [FieldOffset(8)]
  327. public Variant Variant;
  328. }
  329. [StructLayout(LayoutKind.Sequential)]
  330. public unsafe struct VariantValue
  331. {
  332. IntPtr Storage1;
  333. IntPtr Storage2;
  334. IntPtr Storage3;
  335. IntPtr Storage4;
  336. }
  337. [StructLayout(LayoutKind.Explicit)]
  338. public struct Variant {
  339. [FieldOffset(0)]
  340. public VariantType Type;
  341. [FieldOffset(8)]
  342. public VariantValue Value;
  343. }
  344. [StructLayout (LayoutKind.Sequential)]
  345. public struct Matrix3x4 {
  346. public float m00;
  347. public float m01;
  348. public float m02;
  349. public float m03;
  350. public float m10;
  351. public float m11;
  352. public float m12;
  353. public float m13;
  354. public float m20;
  355. public float m21;
  356. public float m22;
  357. public float m23;
  358. public static readonly Matrix3x4 Identity = new Matrix3x4 {m00 = 1, m11 = 1, m22 = 1};
  359. public static readonly Matrix3x4 Zero = new Matrix3x4 ();
  360. public Matrix3x4(
  361. float v00, float v01, float v02, float v03,
  362. float v10, float v11, float v12, float v13,
  363. float v20, float v21, float v22, float v23)
  364. {
  365. m00 = v00; m01 = v01; m02 = v02; m03 = v03;
  366. m10 = v10; m11 = v11; m12 = v12; m13 = v13;
  367. m20 = v20; m21 = v21; m22 = v22; m23 = v23;
  368. }
  369. public Matrix3x4(Vector3 row0, Vector3 row1, Vector3 row2, Vector3 row3)
  370. {
  371. m00 = row0.X; m10 = row0.Y; m20 = row0.Z;
  372. m01 = row1.X; m11 = row1.Y; m21 = row1.Z;
  373. m02 = row2.X; m12 = row2.Y; m22 = row2.Z;
  374. m03 = row3.X; m13 = row3.Y; m23 = row3.Z;
  375. }
  376. public Matrix3x4 Inverse()
  377. {
  378. float det = m00 * m11 * m22 +
  379. m10 * m21 * m02 +
  380. m20 * m01 * m12 -
  381. m20 * m11 * m02 -
  382. m10 * m01 * m22 -
  383. m00 * m21 * m12;
  384. float invDet = 1.0f / det;
  385. Matrix3x4 ret;
  386. ret.m00 = (m11 * m22 - m21 * m12) * invDet;
  387. ret.m01 = -(m01 * m22 - m21 * m02) * invDet;
  388. ret.m02 = (m01 * m12 - m11 * m02) * invDet;
  389. ret.m03 = -(m03 * ret.m00 + m13 * ret.m01 + m23 * ret.m02);
  390. ret.m10 = -(m10 * m22 - m20 * m12) * invDet;
  391. ret.m11 = (m00 * m22 - m20 * m02) * invDet;
  392. ret.m12 = -(m00 * m12 - m10 * m02) * invDet;
  393. ret.m13 = -(m03 * ret.m10 + m13 * ret.m11 + m23 * ret.m12);
  394. ret.m20 = (m10 * m21 - m20 * m11) * invDet;
  395. ret.m21 = -(m00 * m21 - m20 * m01) * invDet;
  396. ret.m22 = (m00 * m11 - m10 * m01) * invDet;
  397. ret.m23 = -(m03 * ret.m20 + m13 * ret.m21 + m23 * ret.m22);
  398. return ret;
  399. }
  400. public static Matrix4 operator *(Matrix4 left, Matrix3x4 rhs)
  401. {
  402. return new Matrix4(
  403. left.M11 * rhs.m00 + left.M12 * rhs.m10 + left.M13 * rhs.m20,
  404. left.M11 * rhs.m01 + left.M12 * rhs.m11 + left.M13 * rhs.m21,
  405. left.M11 * rhs.m02 + left.M12 * rhs.m12 + left.M13 * rhs.m22,
  406. left.M11 * rhs.m03 + left.M12 * rhs.m13 + left.M13 * rhs.m23 + left.M14,
  407. left.M21 * rhs.m00 + left.M22 * rhs.m10 + left.M23 * rhs.m20,
  408. left.M21 * rhs.m01 + left.M22 * rhs.m11 + left.M23 * rhs.m21,
  409. left.M21 * rhs.m02 + left.M22 * rhs.m12 + left.M23 * rhs.m22,
  410. left.M21 * rhs.m03 + left.M22 * rhs.m13 + left.M23 * rhs.m23 + left.M24,
  411. left.M31 * rhs.m00 + left.M32 * rhs.m10 + left.M33 * rhs.m20,
  412. left.M31 * rhs.m01 + left.M32 * rhs.m11 + left.M33 * rhs.m21,
  413. left.M31 * rhs.m02 + left.M32 * rhs.m12 + left.M33 * rhs.m22,
  414. left.M31 * rhs.m03 + left.M32 * rhs.m13 + left.M33 * rhs.m23 + left.M34,
  415. left.M41 * rhs.m00 + left.M42 * rhs.m10 + left.M43 * rhs.m20,
  416. left.M41 * rhs.m01 + left.M42 * rhs.m11 + left.M43 * rhs.m21,
  417. left.M41 * rhs.m02 + left.M42 * rhs.m12 + left.M43 * rhs.m22,
  418. left.M41 * rhs.m03 + left.M42 * rhs.m13 + left.M43 * rhs.m23 + left.M44
  419. );
  420. }
  421. public override string ToString()
  422. {
  423. const int cellSize = 6, prec = 2;
  424. return $"| {m00.ToFixedSizeString(cellSize, prec)} | {m01.ToFixedSizeString(cellSize, prec)} | {m02.ToFixedSizeString(cellSize, prec)} | {m03.ToFixedSizeString(cellSize, prec)} |\n" +
  425. $"| {m10.ToFixedSizeString(cellSize, prec)} | {m11.ToFixedSizeString(cellSize, prec)} | {m12.ToFixedSizeString(cellSize, prec)} | {m13.ToFixedSizeString(cellSize, prec)} |\n" +
  426. $"| {m20.ToFixedSizeString(cellSize, prec)} | {m21.ToFixedSizeString(cellSize, prec)} | {m22.ToFixedSizeString(cellSize, prec)} | {m23.ToFixedSizeString(cellSize, prec)} |";
  427. }
  428. }
  429. [StructLayout (LayoutKind.Sequential)]
  430. public struct Color {
  431. public float R, G, B, A;
  432. public Color (float r = 1f, float g = 1f, float b = 1f, float a = 1f)
  433. {
  434. R = r;
  435. G = g;
  436. B = b;
  437. A = a;
  438. }
  439. public Color (Color source)
  440. {
  441. R = source.R;
  442. G = source.G;
  443. B = source.B;
  444. A = source.A;
  445. }
  446. public Color (Color source, float alpha)
  447. {
  448. R = source.R;
  449. G = source.G;
  450. B = source.B;
  451. A = alpha;
  452. }
  453. public static Color White = new Color (1, 1, 1);
  454. public static Color Gray = new Color (0.5f, 0.5f, 0.5f);
  455. public static Color Black = new Color (0.0f, 0.0f, 0.0f);
  456. public static Color Red = new Color (1.0f, 0.0f, 0.0f);
  457. public static Color Green = new Color (0.0f, 1.0f, 0.0f);
  458. public static Color Blue = new Color (0.0f, 0.0f, 1.0f);
  459. public static Color Cyan = new Color (0.0f, 1.0f, 1.0f);
  460. public static Color Magenta = new Color (1.0f, 0.0f, 1.0f);
  461. public static Color Yellow = new Color (1.0f, 1.0f, 0.0f);
  462. public static Color Transparent = new Color (0.0f, 0.0f, 0.0f, 0.0f);
  463. public static Color FromByteFormat(byte r, byte g, byte b, byte a)
  464. {
  465. return new Color(r / 255f, g / 255f, b / 255f, a / 255f);
  466. }
  467. public static Color FromHex(string hex)
  468. {
  469. hex = hex.Replace("#", string.Empty);
  470. if (hex.Length == 6)
  471. hex = "FF" + hex;
  472. byte a = (byte)(Convert.ToUInt32(hex.Substring(0, 2), 16));
  473. byte r = (byte)(Convert.ToUInt32(hex.Substring(2, 2), 16));
  474. byte g = (byte)(Convert.ToUInt32(hex.Substring(4, 2), 16));
  475. byte b = (byte)(Convert.ToUInt32(hex.Substring(6, 2), 16));
  476. return FromByteFormat(r, g, b, a);
  477. }
  478. public uint ToUInt()
  479. {
  480. uint r = (uint)MathHelper.Clamp(((int)(R * 255.0f)), 0, 255);
  481. uint g = (uint)MathHelper.Clamp(((int)(G * 255.0f)), 0, 255);
  482. uint b = (uint)MathHelper.Clamp(((int)(B * 255.0f)), 0, 255);
  483. uint a = (uint)MathHelper.Clamp(((int)(A * 255.0f)), 0, 255);
  484. return (a << 24) | (b << 16) | (g << 8) | r;
  485. }
  486. public static Color operator +(Color left, Color right)
  487. {
  488. left.A = MathHelper.Clamp(left.A + right.A, 0, 1);
  489. left.R = MathHelper.Clamp(left.R + right.R, 0, 1);
  490. left.G = MathHelper.Clamp(left.G + right.G, 0, 1);
  491. left.B = MathHelper.Clamp(left.B + right.B, 0, 1);
  492. return left;
  493. }
  494. public static Color operator -(Color left, Color right)
  495. {
  496. left.A = MathHelper.Clamp(left.A - right.A, 0, 1);
  497. left.R = MathHelper.Clamp(left.R - right.R, 0, 1);
  498. left.G = MathHelper.Clamp(left.G - right.G, 0, 1);
  499. left.B = MathHelper.Clamp(left.B - right.B, 0, 1);
  500. return left;
  501. }
  502. public static Color operator *(Color left, float value)
  503. {
  504. left.A = MathHelper.Clamp(left.A * value, 0, 1);
  505. left.R = MathHelper.Clamp(left.R * value, 0, 1);
  506. left.G = MathHelper.Clamp(left.G * value, 0, 1);
  507. left.B = MathHelper.Clamp(left.B * value, 0, 1);
  508. return left;
  509. }
  510. public static bool operator ==(Color left, Color right)
  511. {
  512. return left.Equals(right);
  513. }
  514. public static bool operator !=(Color left, Color right)
  515. {
  516. return !left.Equals(right);
  517. }
  518. public bool Equals(Color other)
  519. {
  520. return R.Equals(other.R) && G.Equals(other.G) && B.Equals(other.B) && A.Equals(other.A);
  521. }
  522. public override bool Equals(object obj)
  523. {
  524. if (ReferenceEquals(null, obj)) return false;
  525. return obj is Color && Equals((Color)obj);
  526. }
  527. public override int GetHashCode()
  528. {
  529. unchecked
  530. {
  531. var hashCode = R.GetHashCode();
  532. hashCode = (hashCode * 397) ^ G.GetHashCode();
  533. hashCode = (hashCode * 397) ^ B.GetHashCode();
  534. hashCode = (hashCode * 397) ^ A.GetHashCode();
  535. return hashCode;
  536. }
  537. }
  538. public override string ToString()
  539. {
  540. return $"r:{R}, g:{G}, b:{B}, a:{A}";
  541. }
  542. public static explicit operator Color(Vector3 vector) => new Color(vector.X, vector.Y, vector.Z);
  543. public static explicit operator Vector3(Color color) => new Vector3(color.R, color.G, color.B);
  544. public static explicit operator Color(Vector4 vector) => new Color(vector.X, vector.Y, vector.Z, vector.W);
  545. public static explicit operator Vector4(Color color) => new Vector4(color.R, color.G, color.B, color.A);
  546. }
  547. [StructLayout (LayoutKind.Sequential)]
  548. public struct WeakPtr {
  549. IntPtr ptr;
  550. IntPtr refCountPtr;
  551. public T GetUrhoObject<T>() where T : UrhoObject => Runtime.LookupObject<T>(ptr);
  552. public T GetRefCounted<T>() where T : RefCounted => Runtime.LookupRefCounted<T>(ptr);
  553. }
  554. [StructLayout (LayoutKind.Sequential)]
  555. public struct TouchState {
  556. public int TouchID;
  557. public IntVector2 Position, LastPosition, Delta;
  558. public float Pressure;
  559. WeakPtr touchedElementPtr;
  560. public UIElement TouchedElement => touchedElementPtr.GetUrhoObject<UIElement>();
  561. }
  562. [StructLayout (LayoutKind.Sequential)]
  563. public struct ColorFrame {
  564. public Color Color;
  565. public float Time;
  566. }
  567. [StructLayout (LayoutKind.Sequential)]
  568. public struct JoystickState {
  569. public IntPtr JoystickPtr;
  570. public IntPtr JoystickIdPtr;
  571. public IntPtr ControllerPtr;
  572. IntPtr screenJoystickPtr;
  573. public UIElement ScreenJoystick => Runtime.LookupObject<UIElement>(screenJoystickPtr);
  574. public UrhoString Name;
  575. public VectorBase Buttons;
  576. public VectorBase ButtonPress;
  577. public VectorBase Axes;
  578. public VectorBase Hats;
  579. public float GetAxisPosition(int position)
  580. {
  581. if (position >= Axes.Size)
  582. return 0;
  583. return Axes.Buffer.ReadSingle(position * sizeof(float));
  584. }
  585. public byte GetButtonDown(int position)
  586. {
  587. if (position >= Buttons.Size)
  588. return 0;
  589. return Marshal.ReadByte(Buttons.Buffer, position * sizeof(byte));
  590. }
  591. public byte GetButtonPress(int position)
  592. {
  593. if (position >= ButtonPress.Size)
  594. return 0;
  595. return Marshal.ReadByte(ButtonPress.Buffer, position * sizeof(byte));
  596. }
  597. public int GetHatPosition(int position)
  598. {
  599. if (position >= Hats.Size)
  600. return 0;
  601. return Marshal.ReadInt32(Hats.Buffer, position * sizeof(int));
  602. }
  603. }
  604. [StructLayout(LayoutKind.Sequential)]
  605. public struct VectorBase {
  606. public uint Size;
  607. public uint Capacity;
  608. public IntPtr Buffer;
  609. }
  610. [StructLayout(LayoutKind.Sequential)]
  611. public struct SDL_Event {
  612. }
  613. [StructLayout (LayoutKind.Sequential)]
  614. public struct TextureFrame {
  615. }
  616. [StructLayout (LayoutKind.Sequential)]
  617. public struct LightBatchQueue {
  618. }
  619. [StructLayout (LayoutKind.Sequential)]
  620. public struct Bone {
  621. public UrhoString Name;
  622. public int NameHash;
  623. public uint ParentIndex;
  624. public Vector3 InitialPosition;
  625. public Quaternion InitialRotation;
  626. public Vector3 InitialScale;
  627. public Matrix3x4 OffsetMatrix;
  628. byte animated; //bool is not blittable.
  629. public bool Animated { get { return animated != 0; } set { animated = (byte)(value ? 1 : 0); } }
  630. public byte CollisionMask;
  631. public float Radius;
  632. public BoundingBox BoundingBox;
  633. WeakPtr Node;
  634. }
  635. public unsafe class BoneWrapper
  636. {
  637. readonly object objHolder;
  638. readonly Bone* b;
  639. public BoneWrapper(object objHolder, Bone* bone)
  640. {
  641. this.objHolder = objHolder;
  642. this.b = bone;
  643. }
  644. public UrhoString Name { get { return b->Name; } set { b->Name = value; } }
  645. public int NameHash { get { return b->NameHash; } set { b->NameHash = value; } }
  646. public StringHash NameStringHash { get { return new StringHash(b->NameHash); } set { b->NameHash = value.Code; } }
  647. public uint ParentIndex { get { return b->ParentIndex; } set { b->ParentIndex = value; } }
  648. public Vector3 InitialPosition { get { return b->InitialPosition; } set { b->InitialPosition = value; } }
  649. public Quaternion InitialRotation { get { return b->InitialRotation; } set { b->InitialRotation = value; } }
  650. public Vector3 InitialScale { get { return b->InitialScale; } set { b->InitialScale = value; } }
  651. public Matrix3x4 OffsetMatrix { get { return b->OffsetMatrix; } set { b->OffsetMatrix = value; } }
  652. public bool Animated { get { return b->Animated; } set { b->Animated = value; } }
  653. public byte CollisionMask { get { return b->CollisionMask; } set { b->CollisionMask = value; } }
  654. public float Radius { get { return b->Radius; } set { b->Radius = value; } }
  655. public BoundingBox BoundingBox { get { return b->BoundingBox; } set { b->BoundingBox = value; } }
  656. }
  657. // DEBATABLE: maybe we should let the binder handle it?
  658. [StructLayout(LayoutKind.Sequential)]
  659. public struct RayQueryResult {
  660. public Vector3 Position;
  661. public Vector3 Normal;
  662. public Vector2 TextureUV;
  663. public float Distance;
  664. IntPtr drawablePtr;
  665. public Drawable Drawable => Runtime.LookupObject<Drawable>(drawablePtr);
  666. IntPtr nodePtr;
  667. public Node Node => Runtime.LookupObject<Node>(nodePtr);
  668. public uint SubObject;
  669. }
  670. [StructLayout (LayoutKind.Sequential)]
  671. public unsafe struct RenderPathCommand {
  672. public UrhoString Tag;
  673. public RenderCommandType Type;
  674. public RenderCommandSortMode SortMode;
  675. public UrhoString Pass;
  676. public uint PassIndex;
  677. public UrhoString Metadata;
  678. public UrhoString VertexShaderName;
  679. public UrhoString PixelShaderName;
  680. public UrhoString VertexShaderDefines;
  681. public UrhoString PixelShaderDefines;
  682. //Marshalling String textureNames_[16]
  683. public UrhoString TextureName0;
  684. public UrhoString TextureName1;
  685. public UrhoString TextureName2;
  686. public UrhoString TextureName3;
  687. public UrhoString TextureName4;
  688. public UrhoString TextureName5;
  689. public UrhoString TextureName6;
  690. public UrhoString TextureName7;
  691. #if !__IOS__ && !__ANDROID__
  692. public UrhoString TextureName8;
  693. public UrhoString TextureName9;
  694. public UrhoString TextureName10;
  695. public UrhoString TextureName11;
  696. public UrhoString TextureName12;
  697. public UrhoString TextureName13;
  698. public UrhoString TextureName14;
  699. public UrhoString TextureName15;
  700. #endif
  701. public RenderPathCommand(RenderCommandType type)
  702. {
  703. BlendMode = BlendMode.Replace;
  704. Enabled = 1;
  705. UseLitBase = 1;
  706. Type = type;
  707. //default values:
  708. Tag = default(UrhoString);
  709. SortMode = RenderCommandSortMode.Fronttoback;
  710. Pass = default(UrhoString);
  711. PassIndex = 0;
  712. Metadata = default(UrhoString);
  713. PixelShaderName = default(UrhoString);
  714. VertexShaderName = default(UrhoString);
  715. PixelShaderDefines = default(UrhoString);
  716. VertexShaderDefines = default(UrhoString);
  717. TextureName0 = default(UrhoString);
  718. TextureName1 = default(UrhoString);
  719. TextureName2 = default(UrhoString);
  720. TextureName3 = default(UrhoString);
  721. TextureName4 = default(UrhoString);
  722. TextureName5 = default(UrhoString);
  723. TextureName6 = default(UrhoString);
  724. TextureName7 = default(UrhoString);
  725. #if !__IOS__ && !__ANDROID__
  726. TextureName8 = default(UrhoString);
  727. TextureName9 = default(UrhoString);
  728. TextureName10 = default(UrhoString);
  729. TextureName11 = default(UrhoString);
  730. TextureName12 = default(UrhoString);
  731. TextureName13 = default(UrhoString);
  732. TextureName14 = default(UrhoString);
  733. TextureName15 = default(UrhoString);
  734. #endif
  735. ShaderParameters = default(HashBase);
  736. Outputs = default(VectorBase);
  737. DepthStencilName = default(UrhoString);
  738. ClearFlags = 0;
  739. ClearColor = default(Color);
  740. ClearDepth = 0;
  741. ClearStencil = 0;
  742. UseFogColor = 0;
  743. MarkToStencil = 0;
  744. VertexLights = 0;
  745. EventName = default(UrhoString);
  746. }
  747. public void SetTextureName(TextureUnit unit, string name)
  748. {
  749. UrhoString nameStr = new UrhoString(name);
  750. switch (unit)
  751. {
  752. case TextureUnit.Diffuse:
  753. TextureName0 = nameStr;
  754. break;
  755. case TextureUnit.Normal:
  756. TextureName1 = nameStr;
  757. break;
  758. case TextureUnit.Specular:
  759. TextureName2 = nameStr;
  760. break;
  761. case TextureUnit.Emissive:
  762. TextureName3 = nameStr;
  763. break;
  764. case TextureUnit.Environment:
  765. TextureName4 = nameStr;
  766. break;
  767. case TextureUnit.Volumemap:
  768. TextureName5 = nameStr;
  769. break;
  770. case TextureUnit.Custom1:
  771. TextureName6 = nameStr;
  772. break;
  773. case TextureUnit.Custom2:
  774. TextureName7 = nameStr;
  775. break;
  776. #if !__IOS__ && !__ANDROID__
  777. case TextureUnit.Lightramp:
  778. TextureName8 = nameStr;
  779. break;
  780. case TextureUnit.Lightshape:
  781. TextureName9 = nameStr;
  782. break;
  783. case TextureUnit.Shadowmap:
  784. TextureName10 = nameStr;
  785. break;
  786. case TextureUnit.Faceselect:
  787. TextureName11 = nameStr;
  788. break;
  789. case TextureUnit.Indirection:
  790. TextureName12 = nameStr;
  791. break;
  792. case TextureUnit.Depthbuffer:
  793. TextureName13 = nameStr;
  794. break;
  795. case TextureUnit.Lightbuffer:
  796. TextureName14 = nameStr;
  797. break;
  798. case TextureUnit.Zone:
  799. TextureName15 = nameStr;
  800. break;
  801. #endif
  802. default:
  803. throw new InvalidOperationException("Invalid texture unit");
  804. }
  805. }
  806. // TODO: Surface more members via SharpieBinder
  807. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  808. static extern void RenderPathCommand_SetShaderParameter_float(ref RenderPathCommand rpc, string parameter, float value);
  809. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  810. static extern void RenderPathCommand_SetShaderParameter_Matrix4(ref RenderPathCommand rpc, string parameter, ref Matrix4 value);
  811. [DllImport(Consts.NativeImport, CallingConvention = CallingConvention.Cdecl)]
  812. static extern void RenderPathCommand_SetOutput(ref RenderPathCommand rpc, int index, string name);
  813. public void SetShaderParameter(string parameter, float value) =>
  814. RenderPathCommand_SetShaderParameter_float(ref this, parameter, value);
  815. public void SetShaderParameter(string parameter, Matrix4 value) =>
  816. RenderPathCommand_SetShaderParameter_Matrix4(ref this, parameter, ref value);
  817. public void SetOutput(int index, string name) =>
  818. RenderPathCommand_SetOutput(ref this, index, name);
  819. public HashBase ShaderParameters;
  820. public VectorBase Outputs;
  821. public UrhoString DepthStencilName;
  822. public uint ClearFlags;
  823. public Color ClearColor;
  824. public float ClearDepth;
  825. public uint ClearStencil;
  826. public BlendMode BlendMode;
  827. public byte Enabled;
  828. public byte UseFogColor;
  829. public byte MarkToStencil;
  830. public byte UseLitBase;
  831. public byte VertexLights;
  832. public UrhoString EventName;
  833. }
  834. [StructLayout(LayoutKind.Sequential)]
  835. public struct VertexElement
  836. {
  837. public VertexElementType Type;
  838. /// <summary>
  839. /// Semantic of element.
  840. /// </summary>
  841. VertexElementSemantic Semantic;
  842. /// <summary>
  843. /// Semantic index of element, for example multi-texcoords.
  844. /// </summary>
  845. public byte Index;
  846. /// <summary>
  847. /// Per-instance flag.
  848. /// </summary>
  849. public byte PerInstance;
  850. /// <summary>
  851. /// Offset of element from vertex start. Filled by VertexBuffer once the vertex declaration is built.
  852. /// </summary>
  853. public uint Offset;
  854. }
  855. [StructLayout(LayoutKind.Sequential)]
  856. public struct HashBase {
  857. public IntPtr Head;
  858. public IntPtr Tail;
  859. public IntPtr Ptrs;
  860. public IntPtr Allocator;
  861. }
  862. // DEBATABLE: maybe we should let the binder handle it?
  863. [StructLayout (LayoutKind.Sequential)]
  864. public struct GraphicsImpl {
  865. }
  866. [StructLayout (LayoutKind.Sequential)]
  867. public struct FontGlyph {
  868. public short X, Y, TexWidth, TexHeight;
  869. public float Width, Height, OffsetX, OffsetY, AdvanceX;
  870. public int Page;
  871. byte used;
  872. public bool Used { get { return used != 0; } set { used = (byte) (value ? 1 : 0); }}
  873. }
  874. // DEBATABLE: maybe we should let the binder handle it?
  875. [StructLayout (LayoutKind.Sequential)]
  876. public struct RandomAccessIterator {
  877. }
  878. // DEBATABLE: maybe we should let the binder handle it?
  879. [StructLayout (LayoutKind.Sequential)]
  880. public struct ModelMorph {
  881. }
  882. // DEBATABLE: maybe we should let the binder handle it?
  883. [StructLayout (LayoutKind.Sequential)]
  884. public struct Octant {
  885. }
  886. // DEBATABLE: maybe we should let the binder handle it?
  887. [StructLayout (LayoutKind.Sequential)]
  888. public struct CompressedLevel {
  889. public IntPtr ImageData;
  890. public CompressedFormat Format;
  891. public int Width, Height, Depth;
  892. public uint BlockSize, DataSize, RowSize, RowCount;
  893. }
  894. // DEBATABLE: maybe we should let the binder handle it?
  895. [StructLayout (LayoutKind.Sequential)]
  896. public struct Billboard {
  897. public Vector3 Position;
  898. public Vector2 Size;
  899. public Rect Uv;
  900. public Color Color;
  901. public float Rotation;
  902. public Vector3 Direction;
  903. byte enabled; //bool is not blittable.
  904. public bool Enabled { get { return enabled != 0; } set { enabled = (byte)(value ? 1 : 0); } }
  905. public float SortDistance;
  906. public float ScreenScaleFactor;
  907. }
  908. public unsafe class BillboardWrapper
  909. {
  910. readonly object bbHolder;
  911. readonly Billboard* bb;
  912. public BillboardWrapper(object bbHolder, Billboard* bb)
  913. {
  914. this.bbHolder = bbHolder;
  915. this.bb = bb;
  916. }
  917. public Vector3 Position { get { return bb->Position; } set { bb->Position = value; } }
  918. public Vector2 Size { get { return bb->Size; } set { bb->Size = value; } }
  919. public Rect Uv { get { return bb->Uv; } set { bb->Uv = value; } }
  920. public Color Color { get { return bb->Color; } set { bb->Color = value; } }
  921. public float Rotation { get { return bb->Rotation; } set { bb->Rotation = value; } }
  922. public bool Enabled { get { return bb->Enabled; } set { bb->Enabled = value; } }
  923. public float SortDistance { get { return bb->SortDistance; } set { bb->SortDistance = value; } }
  924. }
  925. // DEBATABLE: maybe we should let the binder handle it?
  926. [StructLayout (LayoutKind.Sequential)]
  927. public struct AnimationTrack {
  928. }
  929. // DEBATABLE: maybe we should let the binder handle it?
  930. [StructLayout (LayoutKind.Sequential)]
  931. public struct CustomGeometryVertex
  932. {
  933. public Vector3 Position;
  934. public Vector3 Normal;
  935. public uint Color;
  936. public Vector2 TexCoord;
  937. public Vector4 Tangent;
  938. }
  939. // DEBATABLE: maybe we should let the binder handle it?
  940. [StructLayout (LayoutKind.Sequential)]
  941. public struct NetworkState {
  942. }
  943. // DEBATABLE: maybe we should let the binder handle it?
  944. [StructLayout (LayoutKind.Sequential)]
  945. public struct ComponentReplicationState {
  946. }
  947. // DEBATABLE: maybe we should let the binder handle it?
  948. [StructLayout (LayoutKind.Sequential)]
  949. public struct ShaderParameter {
  950. }
  951. // DEBATABLE: maybe we should let the binder handle it?
  952. [StructLayout (LayoutKind.Sequential)]
  953. public struct UrhoString
  954. {
  955. public uint Length;
  956. public uint Capacity;
  957. public IntPtr Buffer;
  958. public UrhoString(string str)
  959. {
  960. Buffer = Marshal.StringToHGlobalAnsi(str);
  961. Length = (uint)str.Length;
  962. Capacity = (uint)Math.Max(8, str.Length) + 1;
  963. }
  964. public override string ToString()
  965. {
  966. if (Length > 0 && Buffer != IntPtr.Zero)
  967. return Marshal.PtrToStringAnsi(Buffer, (int)Length);
  968. return string.Empty;
  969. }
  970. public static implicit operator string(UrhoString s) // implicit digit to byte conversion operator
  971. {
  972. return s.ToString();
  973. }
  974. public static explicit operator UrhoString(string s) // explicit byte to digit conversion operator
  975. {
  976. return new UrhoString(s);
  977. }
  978. }
  979. [StructLayout (LayoutKind.Sequential)]
  980. public struct BiasParameters {
  981. public float ConstantBias;
  982. public float SlopeScaleBias;
  983. public float NormalOffset;
  984. public BiasParameters (float constantBias, float slopeScaleBias, float normalOffset = 0.0f)
  985. {
  986. ConstantBias = constantBias;
  987. SlopeScaleBias = slopeScaleBias;
  988. NormalOffset = normalOffset;
  989. }
  990. }
  991. [StructLayout (LayoutKind.Sequential)]
  992. public struct CascadeParameters {
  993. public float Split1, Split2, Split3, Split4;
  994. public float FadeStart;
  995. public float BiasAutoAdjust;
  996. public CascadeParameters (float split1, float split2, float split3, float split4, float fadeStart, float biasAutoAdjust = 1f)
  997. {
  998. Split1 = split1;
  999. Split2 = split2;
  1000. Split3 = split3;
  1001. Split4 = split4;
  1002. FadeStart = fadeStart;
  1003. BiasAutoAdjust = biasAutoAdjust;
  1004. }
  1005. }
  1006. [StructLayout (LayoutKind.Sequential)]
  1007. public struct FocusParameters {
  1008. public byte Focus;
  1009. public byte NonUniform;
  1010. public byte AutoSize;
  1011. public float Quantize;
  1012. public float MinView;
  1013. }
  1014. }
  1015. namespace Urho.IO {
  1016. [StructLayout (LayoutKind.Sequential)]
  1017. public struct PackageEntry {
  1018. public int Offset, Size, Checksum;
  1019. }
  1020. }
  1021. namespace Urho.Urho2D {
  1022. // DEBATABLE: maybe we should let the binder handle it?
  1023. [StructLayout(LayoutKind.Sequential)]
  1024. public struct TileMapInfo2D {
  1025. public Orientation2D Orientation;
  1026. public int Width;
  1027. public int Height;
  1028. public float TileWidth;
  1029. public float TileHeight;
  1030. //calculated properties:
  1031. public float MapWidth => Width * TileWidth;
  1032. public float MapHeight
  1033. {
  1034. get
  1035. {
  1036. if (Orientation == Orientation2D.Staggered)
  1037. return (Height + 1) * 0.5f * TileHeight;
  1038. return Height * TileHeight;
  1039. }
  1040. }
  1041. }
  1042. }
  1043. namespace Urho.Navigation {
  1044. [StructLayout(LayoutKind.Sequential)]
  1045. public struct dtQueryFilter {
  1046. //public float[] AreaCost; // Cost per area type. (Used by default implementation.)
  1047. //public ushort IncludeFlags; // Flags for polygons that can be visited. (Used by default implementation.)
  1048. //public ushort ExcludeFlags; // Flags for polygons that should not be visted. (Used by default implementation.)
  1049. }
  1050. [StructLayout(LayoutKind.Sequential)]
  1051. public struct CrowdObstacleAvoidanceParams {
  1052. public float VelBias;
  1053. public float WeightDesVel;
  1054. public float WeightCurVel;
  1055. public float WeightSide;
  1056. public float WeightToi;
  1057. public float HorizTime;
  1058. public byte GridSize;
  1059. public byte AdaptiveDivs;
  1060. public byte AdaptiveRings;
  1061. public byte AdaptiveDepth;
  1062. };
  1063. }
  1064. namespace Urho.Physics {
  1065. [StructLayout(LayoutKind.Sequential)]
  1066. public struct PhysicsRaycastResult {
  1067. public Vector3 Position;
  1068. public Vector3 Normal;
  1069. public float Distance;
  1070. public float HitFraction;
  1071. IntPtr bodyPtr;
  1072. public RigidBody Body => Runtime.LookupObject<RigidBody>(bodyPtr);
  1073. }
  1074. }
  1075. namespace Urho.Resources {
  1076. [StructLayout (LayoutKind.Sequential)]
  1077. public struct XPathResultSet {
  1078. }
  1079. }
  1080. namespace Urho.Network {
  1081. [StructLayout (LayoutKind.Sequential)]
  1082. public struct ReplicationState {
  1083. IntPtr connection;
  1084. public Connection Connection => Runtime.LookupObject<Connection> (connection);
  1085. }
  1086. [StructLayout (LayoutKind.Sequential)]
  1087. public unsafe struct DirtyBits {
  1088. public fixed byte Data [8];
  1089. public byte Count;
  1090. }
  1091. [StructLayout (LayoutKind.Sequential)]
  1092. public struct NodeReplicationState {
  1093. }
  1094. }