MegaBezPatch.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. [System.Serializable]
  4. public class Warp
  5. {
  6. public string name = "None";
  7. public Vector3[] points = new Vector3[16];
  8. public void GetWarp(MegaBezPatch mod)
  9. {
  10. mod.p11 = points[0];
  11. mod.p12 = points[1];
  12. mod.p13 = points[2];
  13. mod.p14 = points[3];
  14. mod.p21 = points[4];
  15. mod.p22 = points[5];
  16. mod.p23 = points[6];
  17. mod.p24 = points[7];
  18. mod.p31 = points[8];
  19. mod.p32 = points[9];
  20. mod.p33 = points[10];
  21. mod.p34 = points[11];
  22. mod.p41 = points[12];
  23. mod.p42 = points[13];
  24. mod.p43 = points[14];
  25. mod.p44 = points[15];
  26. }
  27. public void SetWarp(MegaBezPatch mod)
  28. {
  29. points[0] = mod.p11;
  30. points[1] = mod.p12;
  31. points[2] = mod.p13;
  32. points[3] = mod.p14;
  33. points[4] = mod.p21;
  34. points[5] = mod.p22;
  35. points[6] = mod.p23;
  36. points[7] = mod.p24;
  37. points[8] = mod.p31;
  38. points[9] = mod.p32;
  39. points[10] = mod.p33;
  40. points[11] = mod.p34;
  41. points[12] = mod.p41;
  42. points[13] = mod.p42;
  43. points[14] = mod.p43;
  44. points[15] = mod.p44;
  45. }
  46. public void AdjustLattice(float wr, float hr)
  47. {
  48. Vector3 r = new Vector3(wr, hr, 1.0f);
  49. points[0] = Vector3.Scale(points[0], r);
  50. points[1] = Vector3.Scale(points[1], r);
  51. points[2] = Vector3.Scale(points[2], r);
  52. points[3] = Vector3.Scale(points[3], r);
  53. points[4] = Vector3.Scale(points[4], r);
  54. points[5] = Vector3.Scale(points[5], r);
  55. points[6] = Vector3.Scale(points[6], r);
  56. points[7] = Vector3.Scale(points[7], r);
  57. points[8] = Vector3.Scale(points[8], r);
  58. points[9] = Vector3.Scale(points[9], r);
  59. points[10] = Vector3.Scale(points[10], r);
  60. points[11] = Vector3.Scale(points[11], r);
  61. points[12] = Vector3.Scale(points[12], r);
  62. points[13] = Vector3.Scale(points[13], r);
  63. points[14] = Vector3.Scale(points[14], r);
  64. points[15] = Vector3.Scale(points[15], r);
  65. }
  66. }
  67. [ExecuteInEditMode]
  68. public class MegaBezPatch : MonoBehaviour
  69. {
  70. public float Width = 1.0f;
  71. public float Height = 1.0f;
  72. public int WidthSegs = 20;
  73. public int HeightSegs = 20;
  74. public bool GenUVs = true;
  75. public bool recalcBounds = false;
  76. public bool recalcTangents = true;
  77. public bool recalcCollider = false;
  78. public bool showgizmos = true;
  79. public bool showlatticepoints = false;
  80. public Color latticecol = Color.white;
  81. public float handlesize = 0.075f;
  82. public bool positionhandles = false;
  83. public bool showlabels = true;
  84. public Vector2 snap = new Vector2(0.25f, 0.25f);
  85. public List<Warp> warps = new List<Warp>();
  86. public int srcwarp;
  87. public int destwarp;
  88. [HideInInspector]
  89. public Vector3[] verts;
  90. [HideInInspector]
  91. public Vector2[] uvs;
  92. [HideInInspector]
  93. public int[] tris;
  94. [HideInInspector]
  95. public Vector3[] norms;
  96. [HideInInspector]
  97. public bool rebuild = true;
  98. public Vector2 UVOffset = Vector2.zero;
  99. public Vector2 UVScale = Vector2.one;
  100. public int currentwarp = 0;
  101. [HideInInspector]
  102. public Mesh mesh;
  103. public float switchtime = 1.0f;
  104. public float time = 1000.0f;
  105. public Vector3 p11;
  106. public Vector3 p21;
  107. public Vector3 p31;
  108. public Vector3 p41;
  109. public Vector3 p12;
  110. public Vector3 p22;
  111. public Vector3 p32;
  112. public Vector3 p42;
  113. public Vector3 p13;
  114. public Vector3 p23;
  115. public Vector3 p33;
  116. public Vector3 p43;
  117. public Vector3 p14;
  118. public Vector3 p24;
  119. public Vector3 p34;
  120. public Vector3 p44;
  121. public bool animateWarps = true;
  122. //public bool useWarpValue = false;
  123. //public float warpValue = 0.0f;
  124. public bool align = false;
  125. public float scale = 1.0f;
  126. public Vector2 offset = Vector2.zero;
  127. public void AddWarp()
  128. {
  129. Warp warp = new Warp();
  130. warp.SetWarp(this);
  131. warps.Add(warp);
  132. }
  133. public void UpdateWarp(int i)
  134. {
  135. Warp warp = warps[i];
  136. warp.SetWarp(this);
  137. }
  138. public void SetDestWarp(int i)
  139. {
  140. destwarp = i;
  141. }
  142. public void SetWarp(int i)
  143. {
  144. if ( Application.isPlaying )
  145. {
  146. time = 0.0f;
  147. srcwarp = currentwarp;
  148. destwarp = i;
  149. }
  150. else
  151. {
  152. time = 100.0f;
  153. currentwarp = i;
  154. warps[i].GetWarp(this);
  155. }
  156. }
  157. void Start()
  158. {
  159. time = 0.0f;
  160. }
  161. public void Reset()
  162. {
  163. InitLattice();
  164. Rebuild();
  165. }
  166. public void Rebuild()
  167. {
  168. MeshFilter mf = GetComponent<MeshFilter>();
  169. if ( mf != null )
  170. {
  171. Mesh mesh1 = mf.sharedMesh;
  172. if ( mesh1 == null )
  173. {
  174. mesh1 = new Mesh();
  175. mf.sharedMesh = mesh1;
  176. }
  177. mesh = mesh1;
  178. }
  179. }
  180. void Update()
  181. {
  182. if ( animateWarps )
  183. ChangeWarp(srcwarp, destwarp);
  184. else
  185. {
  186. CheckForChange();
  187. }
  188. if ( mesh == null )
  189. Rebuild();
  190. if ( rebuild )
  191. BuildMesh(mesh);
  192. }
  193. void MakeQuad1(int f, int a, int b, int c, int d)
  194. {
  195. tris[f++] = a;
  196. tris[f++] = b;
  197. tris[f++] = c;
  198. tris[f++] = c;
  199. tris[f++] = d;
  200. tris[f++] = a;
  201. }
  202. // Put in utils
  203. int MaxComponent(Vector3 v)
  204. {
  205. if ( Mathf.Abs(v.x) > Mathf.Abs(v.y) )
  206. {
  207. if ( Mathf.Abs(v.x) > Mathf.Abs(v.z) )
  208. return 0;
  209. else
  210. return 2;
  211. }
  212. else
  213. {
  214. if ( Mathf.Abs(v.y) > Mathf.Abs(v.z) )
  215. return 1;
  216. else
  217. return 2;
  218. }
  219. }
  220. void UpdateSurface()
  221. {
  222. }
  223. // Only call this on size or seg change
  224. void BuildMesh(Mesh mesh)
  225. {
  226. if ( align )
  227. {
  228. AlignPoints();
  229. }
  230. if ( WidthSegs < 1 )
  231. WidthSegs = 1;
  232. if ( HeightSegs < 1 )
  233. HeightSegs = 1;
  234. Vector3 p = Vector3.zero;
  235. int numverts = (WidthSegs + 1) * (HeightSegs + 1);
  236. if ( verts == null )
  237. {
  238. InitLattice();
  239. }
  240. if ( verts == null || verts.Length != numverts )
  241. {
  242. verts = new Vector3[numverts];
  243. uvs = new Vector2[numverts];
  244. tris = new int[HeightSegs * WidthSegs * 2 * 3];
  245. norms = new Vector3[numverts];
  246. for ( int i = 0; i < norms.Length; i++ )
  247. norms[i] = Vector3.back;
  248. }
  249. Vector2 uv = Vector2.zero;
  250. int index = 0;
  251. p = Vector3.zero;
  252. for ( int i = 0; i <= HeightSegs; i++ )
  253. {
  254. index = i * (WidthSegs + 1);
  255. for ( int j = 0; j <= WidthSegs; j++ )
  256. {
  257. float xIndex = (float)j / (float)WidthSegs;
  258. float yIndex = (float)i / (float)HeightSegs;
  259. float omx = 1.0f - xIndex;
  260. float omy = 1.0f - yIndex;
  261. float x1 = omx * omx * omx;
  262. float x2 = (3.0f * omx) * omx * xIndex;
  263. float x3 = (3.0f * omx) * xIndex * xIndex;
  264. float x4 = xIndex * xIndex * xIndex;
  265. float y1 = omy * omy * omy;
  266. float y2 = (3.0f * omy) * omy * yIndex;
  267. float y3 = (3.0f * omy) * yIndex * yIndex;
  268. float y4 = yIndex * yIndex * yIndex;
  269. p.x = (x1 * p11.x * y1) + (x2 * p12.x * y1) + (x3 * p13.x * y1) + (x4 * p14.x * y1)
  270. + (x1 * p21.x * y2) + (x2 * p22.x * y2) + (x3 * p23.x * y2) + (x4 * p24.x * y2)
  271. + (x1 * p31.x * y3) + (x2 * p32.x * y3) + (x3 * p33.x * y3) + (x4 * p34.x * y3)
  272. + (x1 * p41.x * y4) + (x2 * p42.x * y4) + (x3 * p43.x * y4) + (x4 * p44.x * y4);
  273. p.y = (x1 * p11.y * y1) + (x2 * p12.y * y1) + (x3 * p13.y * y1) + (x4 * p14.y * y1)
  274. + (x1 * p21.y * y2) + (x2 * p22.y * y2) + (x3 * p23.y * y2) + (x4 * p24.y * y2)
  275. + (x1 * p31.y * y3) + (x2 * p32.y * y3) + (x3 * p33.y * y3) + (x4 * p34.y * y3)
  276. + (x1 * p41.y * y4) + (x2 * p42.y * y4) + (x3 * p43.y * y4) + (x4 * p44.y * y4);
  277. verts[index + j] = p;
  278. if ( GenUVs )
  279. {
  280. uv.x = (xIndex + UVOffset.x) * UVScale.x;
  281. uv.y = (yIndex + UVOffset.y) * UVScale.y;
  282. uvs[index + j] = uv;
  283. }
  284. }
  285. }
  286. int f = 0;
  287. for ( int iz = 0; iz < HeightSegs; iz++ )
  288. {
  289. int kv = iz * (WidthSegs + 1);
  290. for ( int ix = 0; ix < WidthSegs; ix++ )
  291. {
  292. tris[f++] = kv;
  293. tris[f++] = kv + WidthSegs + 1;
  294. tris[f++] = kv + WidthSegs + 2;
  295. tris[f++] = kv + WidthSegs + 2;
  296. tris[f++] = kv + 1;
  297. tris[f++] = kv;
  298. kv++;
  299. }
  300. }
  301. mesh.Clear();
  302. mesh.subMeshCount = 1;
  303. mesh.vertices = verts;
  304. mesh.uv = uvs;
  305. mesh.SetTriangles(tris, 0);
  306. mesh.normals = norms;
  307. mesh.RecalculateBounds();
  308. if ( recalcTangents )
  309. BuildTangents(mesh, verts, norms, tris, uvs);
  310. }
  311. [ContextMenu("Init Lattice")]
  312. public void InitLattice()
  313. {
  314. float w = Width;
  315. float h = Height;
  316. p11 = new Vector3(-1.5f * w, -1.5f * h, 0.0f);
  317. p12 = new Vector3(-0.5f * w, -1.5f * h, 0.0f);
  318. p13 = new Vector3(0.5f * w, -1.5f * h, 0.0f);
  319. p14 = new Vector3(1.5f * w, -1.5f * h, 0.0f);
  320. p21 = new Vector3(-1.5f * w, -0.5f * h, 0.0f);
  321. p22 = new Vector3(-0.5f * w, -0.5f * h, 0.0f);
  322. p23 = new Vector3(0.5f * w, -0.5f * h, 0.0f);
  323. p24 = new Vector3(1.5f * w, -0.5f * h, 0.0f);
  324. p31 = new Vector3(-1.5f * w, 0.5f * h, 0.0f);
  325. p32 = new Vector3(-0.5f * w, 0.5f * h, 0.0f);
  326. p33 = new Vector3(0.5f * w, 0.5f * h, 0.0f);
  327. p34 = new Vector3(1.5f * w, 0.5f * h, 0.0f);
  328. p41 = new Vector3(-1.5f * w, 1.5f * h, 0.0f);
  329. p42 = new Vector3(-0.5f * w, 1.5f * h, 0.0f);
  330. p43 = new Vector3(0.5f * w, 1.5f * h, 0.0f);
  331. p44 = new Vector3(1.5f * w, 1.5f * h, 0.0f);
  332. }
  333. public Vector3[] lpoints;
  334. public void AdjustLattice(float w, float h)
  335. {
  336. float wr = w / Width;
  337. float hr = h / Height;
  338. Vector3 r = new Vector3(wr, hr, 1.0f);
  339. p11 = Vector3.Scale(p11, r);
  340. p12 = Vector3.Scale(p12, r);
  341. p13 = Vector3.Scale(p13, r);
  342. p14 = Vector3.Scale(p14, r);
  343. p21 = Vector3.Scale(p21, r);
  344. p22 = Vector3.Scale(p22, r);
  345. p23 = Vector3.Scale(p23, r);
  346. p24 = Vector3.Scale(p24, r);
  347. p31 = Vector3.Scale(p31, r);
  348. p32 = Vector3.Scale(p32, r);
  349. p33 = Vector3.Scale(p33, r);
  350. p34 = Vector3.Scale(p34, r);
  351. p41 = Vector3.Scale(p41, r);
  352. p42 = Vector3.Scale(p42, r);
  353. p43 = Vector3.Scale(p43, r);
  354. p44 = Vector3.Scale(p44, r);
  355. for ( int i = 0; i < warps.Count; i++ )
  356. {
  357. warps[i].AdjustLattice(wr, hr);
  358. }
  359. Height = h;
  360. Width = w;
  361. }
  362. static public void BuildTangents(Mesh mesh, Vector3[] verts, Vector3[] norms, int[] tris, Vector2[] uvs)
  363. {
  364. int vertexCount = mesh.vertices.Length;
  365. Vector3[] tan1 = new Vector3[vertexCount];
  366. Vector3[] tan2 = new Vector3[vertexCount];
  367. Vector4[] tangents = new Vector4[vertexCount];
  368. for ( int a = 0; a < tris.Length; a += 3 )
  369. {
  370. long i1 = tris[a];
  371. long i2 = tris[a + 1];
  372. long i3 = tris[a + 2];
  373. Vector3 v1 = verts[i1];
  374. Vector3 v2 = verts[i2];
  375. Vector3 v3 = verts[i3];
  376. Vector2 w1 = uvs[i1];
  377. Vector2 w2 = uvs[i2];
  378. Vector2 w3 = uvs[i3];
  379. float x1 = v2.x - v1.x;
  380. float x2 = v3.x - v1.x;
  381. float y1 = v2.y - v1.y;
  382. float y2 = v3.y - v1.y;
  383. float z1 = v2.z - v1.z;
  384. float z2 = v3.z - v1.z;
  385. float s1 = w2.x - w1.x;
  386. float s2 = w3.x - w1.x;
  387. float t1 = w2.y - w1.y;
  388. float t2 = w3.y - w1.y;
  389. float r = 1.0f / (s1 * t2 - s2 * t1);
  390. Vector3 sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
  391. Vector3 tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
  392. tan1[i1] += sdir;
  393. tan1[i2] += sdir;
  394. tan1[i3] += sdir;
  395. tan2[i1] += tdir;
  396. tan2[i2] += tdir;
  397. tan2[i3] += tdir;
  398. }
  399. for ( int a = 0; a < vertexCount; a++ )
  400. {
  401. Vector3 n = norms[a];
  402. Vector3 t = tan1[a];
  403. Vector3.OrthoNormalize(ref n, ref t);
  404. tangents[a].x = t.x;
  405. tangents[a].y = t.y;
  406. tangents[a].z = t.z;
  407. tangents[a].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
  408. }
  409. mesh.tangents = tangents;
  410. }
  411. Vector3 bounce(Vector3 start, Vector3 end, float value)
  412. {
  413. value /= 1.0f;
  414. end -= start;
  415. if ( value < (1.0f / 2.75f) )
  416. {
  417. return end * (7.5625f * value * value) + start;
  418. }
  419. else
  420. {
  421. if ( value < (2.0f / 2.75f) )
  422. {
  423. value -= (1.5f / 2.75f);
  424. return end * (7.5625f * (value) * value + 0.75f) + start;
  425. }
  426. else
  427. {
  428. if ( value < (2.5f / 2.75f) )
  429. {
  430. value -= (2.25f / 2.75f);
  431. return end * (7.5625f * (value) * value + .9375f) + start;
  432. }
  433. else
  434. {
  435. value -= (2.625f / 2.75f);
  436. return end * (7.5625f * (value) * value + .984375f) + start;
  437. }
  438. }
  439. }
  440. }
  441. Vector3 easeInOutSine(Vector3 start, Vector3 end, float value)
  442. {
  443. end -= start;
  444. return -end / 2.0f * (Mathf.Cos(Mathf.PI * value / 1.0f) - 1.0f) + start;
  445. }
  446. float delay = -1.0f;
  447. int currentDest;
  448. public void CheckForChange()
  449. {
  450. if ( !Application.isPlaying )
  451. return;
  452. if ( destwarp < 0 || destwarp >= warps.Count )
  453. return;
  454. if ( destwarp != srcwarp )
  455. {
  456. if ( destwarp != currentDest )
  457. {
  458. time = 0.0f;
  459. currentDest = destwarp;
  460. }
  461. time += Time.deltaTime;
  462. Warp from = warps[srcwarp];
  463. Warp to = warps[destwarp];
  464. float a = time / switchtime;
  465. if ( a >= 1.0f )
  466. {
  467. a = 1.0f;
  468. time = 0.0f;
  469. srcwarp = destwarp;
  470. }
  471. p11 = easeInOutSine(from.points[0], to.points[0], a);
  472. p12 = easeInOutSine(from.points[1], to.points[1], a);
  473. p13 = easeInOutSine(from.points[2], to.points[2], a);
  474. p14 = easeInOutSine(from.points[3], to.points[3], a);
  475. p21 = easeInOutSine(from.points[4], to.points[4], a);
  476. p22 = easeInOutSine(from.points[5], to.points[5], a);
  477. p23 = easeInOutSine(from.points[6], to.points[6], a);
  478. p24 = easeInOutSine(from.points[7], to.points[7], a);
  479. p31 = easeInOutSine(from.points[8], to.points[8], a);
  480. p32 = easeInOutSine(from.points[9], to.points[9], a);
  481. p33 = easeInOutSine(from.points[10], to.points[10], a);
  482. p34 = easeInOutSine(from.points[11], to.points[11], a);
  483. p41 = easeInOutSine(from.points[12], to.points[12], a);
  484. p42 = easeInOutSine(from.points[13], to.points[13], a);
  485. p43 = easeInOutSine(from.points[14], to.points[14], a);
  486. p44 = easeInOutSine(from.points[15], to.points[15], a);
  487. }
  488. else
  489. time = 0.0f;
  490. }
  491. public void ChangeWarp(int f, int t)
  492. {
  493. if ( !Application.isPlaying )
  494. return;
  495. if ( delay > 0.0f )
  496. {
  497. delay -= Time.deltaTime;
  498. return;
  499. }
  500. if ( time <= switchtime )
  501. {
  502. time += Time.deltaTime;
  503. Warp from = warps[f];
  504. Warp to = warps[t];
  505. float a = time / switchtime;
  506. if ( a > 1.0f )
  507. {
  508. a = 1.0f;
  509. currentwarp = t;
  510. t++;
  511. destwarp = t;
  512. if ( destwarp >= warps.Count )
  513. destwarp = 0;
  514. srcwarp = currentwarp;
  515. time = 0.0f;
  516. delay = 1.0f;
  517. }
  518. p11 = easeInOutSine(from.points[0], to.points[0], a);
  519. p12 = easeInOutSine(from.points[1], to.points[1], a);
  520. p13 = easeInOutSine(from.points[2], to.points[2], a);
  521. p14 = easeInOutSine(from.points[3], to.points[3], a);
  522. p21 = easeInOutSine(from.points[4], to.points[4], a);
  523. p22 = easeInOutSine(from.points[5], to.points[5], a);
  524. p23 = easeInOutSine(from.points[6], to.points[6], a);
  525. p24 = easeInOutSine(from.points[7], to.points[7], a);
  526. p31 = easeInOutSine(from.points[8], to.points[8], a);
  527. p32 = easeInOutSine(from.points[9], to.points[9], a);
  528. p33 = easeInOutSine(from.points[10], to.points[10], a);
  529. p34 = easeInOutSine(from.points[11], to.points[11], a);
  530. p41 = easeInOutSine(from.points[12], to.points[12], a);
  531. p42 = easeInOutSine(from.points[13], to.points[13], a);
  532. p43 = easeInOutSine(from.points[14], to.points[14], a);
  533. p44 = easeInOutSine(from.points[15], to.points[15], a);
  534. }
  535. }
  536. [ContextMenu("Align")]
  537. public void AlignPoints()
  538. {
  539. Vector3 d = p14 - p11;
  540. p12 = p11 + (d * 0.333333f);
  541. p13 = p11 + (d * 0.666666f);
  542. d = p44 - p41;
  543. p42 = p41 + (d * 0.333333f);
  544. p43 = p41 + (d * 0.666666f);
  545. d = p41 - p11;
  546. p21 = p11 + (d * 0.333333f);
  547. p31 = p11 + (d * 0.666666f);
  548. d = p44 - p14;
  549. p24 = p14 + (d * 0.333333f);
  550. p34 = p14 + (d * 0.666666f);
  551. // Need to do line insterect
  552. p22 = LineIntersectionPoint(p21, p24, p12, p42);
  553. p23 = LineIntersectionPoint(p21, p24, p13, p43);
  554. p32 = LineIntersectionPoint(p31, p34, p12, p42);
  555. p33 = LineIntersectionPoint(p31, p34, p13, p43);
  556. }
  557. Vector3 LineIntersectionPoint(Vector3 ps1, Vector3 pe1, Vector3 ps2, Vector3 pe2)
  558. {
  559. // Get A,B,C of first line - points : ps1 to pe1
  560. float A1 = pe1.y - ps1.y;
  561. float B1 = ps1.x - pe1.x;
  562. float C1 = A1 * ps1.x + B1 * ps1.y;
  563. // Get A,B,C of second line - points : ps2 to pe2
  564. float A2 = pe2.y - ps2.y;
  565. float B2 = ps2.x - pe2.x;
  566. float C2 = A2 * ps2.x + B2 * ps2.y;
  567. // Get delta and check if the lines are parallel
  568. float delta = A1 * B2 - A2 * B1;
  569. if ( delta == 0 )
  570. throw new System.Exception("Lines are parallel");
  571. // now return the Vector2 intersection point
  572. return new Vector3((B2 * C1 - B1 * C2) / delta, (A1 * C2 - A2 * C1) / delta, 0.0f);
  573. }
  574. }