babylon.math.ts 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098
  1. module BABYLON {
  2. export class Color3 {
  3. constructor(public r: number = 0, public g: number = 0, public b: number = 0) {
  4. }
  5. public toString(): string {
  6. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  7. }
  8. // Operators
  9. public toArray(array: number[], index?: number): void {
  10. if (index === undefined) {
  11. index = 0;
  12. }
  13. array[index] = this.r;
  14. array[index + 1] = this.g;
  15. array[index + 2] = this.b;
  16. }
  17. public asArray(): number[] {
  18. var result = [];
  19. this.toArray(result, 0);
  20. return result;
  21. }
  22. public multiply(otherColor: Color3): Color3 {
  23. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  24. }
  25. public multiplyToRef(otherColor: Color3, result: Color3): void {
  26. result.r = this.r * otherColor.r;
  27. result.g = this.g * otherColor.g;
  28. result.b = this.b * otherColor.b;
  29. }
  30. public equals(otherColor: Color3): boolean {
  31. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  32. }
  33. public scale(scale: number): Color3 {
  34. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  35. }
  36. public scaleToRef(scale: number, result: Color3): void {
  37. result.r = this.r * scale;
  38. result.g = this.g * scale;
  39. result.b = this.b * scale;
  40. }
  41. public add(otherColor: Color3): Color3 {
  42. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  43. }
  44. public addToRef(otherColor: Color3, result: Color3): void {
  45. result.r = this.r + otherColor.r;
  46. result.g = this.g + otherColor.g;
  47. result.b = this.b + otherColor.b;
  48. }
  49. public subtract(otherColor: Color3): Color3 {
  50. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  51. }
  52. public subtractToRef(otherColor: Color3, result: Color3): void {
  53. result.r = this.r - otherColor.r;
  54. result.g = this.g - otherColor.g;
  55. result.b = this.b - otherColor.b;
  56. }
  57. public clone(): Color3 {
  58. return new Color3(this.r, this.g, this.b);
  59. }
  60. public copyFrom(source: Color3): void {
  61. this.r = source.r;
  62. this.g = source.g;
  63. this.b = source.b;
  64. }
  65. public copyFromFloats(r: number, g: number, b: number): void {
  66. this.r = r;
  67. this.g = g;
  68. this.b = b;
  69. }
  70. // Statics
  71. public static FromArray(array: number[]): Color3 {
  72. return new Color3(array[0], array[1], array[2]);
  73. }
  74. public static FromInts(r: number, g: number, b: number): Color3 {
  75. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  76. }
  77. public static Lerp(start: Color3, end: Color3, amount: number): Color3 {
  78. var r = start.r + ((end.r - start.r) * amount);
  79. var g = start.g + ((end.g - start.g) * amount);
  80. var b = start.b + ((end.b - start.b) * amount);
  81. return new Color3(r, g, b);
  82. }
  83. public static Red(): Color3 { return new Color3(1, 0, 0); }
  84. public static Green(): Color3 { return new Color3(0, 1, 0); }
  85. public static Blue(): Color3 { return new Color3(0, 0, 1); }
  86. public static Black(): Color3 { return new Color3(0, 0, 0); }
  87. public static White(): Color3 { return new Color3(1, 1, 1); }
  88. public static Purple(): Color3 { return new Color3(0.5, 0, 0.5); }
  89. public static Magenta(): Color3 { return new Color3(1, 0, 1); }
  90. public static Yellow(): Color3 { return new Color3(1, 1, 0); }
  91. public static Gray(): Color3 { return new Color3(0.5, 0.5, 0.5); }
  92. }
  93. export class Color4 {
  94. constructor(public r: number, public g: number, public b: number, public a: number) {
  95. }
  96. // Operators
  97. public addInPlace(right) {
  98. this.r += right.r;
  99. this.g += right.g;
  100. this.b += right.b;
  101. this.a += right.a;
  102. }
  103. public asArray(): number[] {
  104. var result = [];
  105. this.toArray(result, 0);
  106. return result;
  107. }
  108. public toArray(array: number[], index?: number): void {
  109. if (index === undefined) {
  110. index = 0;
  111. }
  112. array[index] = this.r;
  113. array[index + 1] = this.g;
  114. array[index + 2] = this.b;
  115. array[index + 3] = this.a;
  116. }
  117. public add(right: Color4): Color4 {
  118. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  119. }
  120. public subtract(right: Color4): Color4 {
  121. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  122. }
  123. public subtractToRef(right: Color4, result: Color4): void {
  124. result.r = this.r - right.r;
  125. result.g = this.g - right.g;
  126. result.b = this.b - right.b;
  127. result.a = this.a - right.a;
  128. }
  129. public scale(scale: number): Color4 {
  130. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  131. }
  132. public scaleToRef(scale: number, result: Color4): void {
  133. result.r = this.r * scale;
  134. result.g = this.g * scale;
  135. result.b = this.b * scale;
  136. result.a = this.a * scale;
  137. }
  138. public toString(): string {
  139. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  140. }
  141. public clone(): Color4 {
  142. return new Color4(this.r, this.g, this.b, this.a);
  143. }
  144. // Statics
  145. public static Lerp(left: Color4, right: Color4, amount: number): Color4 {
  146. var result = new Color4(0, 0, 0, 0);
  147. BABYLON.Color4.LerpToRef(left, right, amount, result);
  148. return result;
  149. }
  150. public static LerpToRef(left: Color4, right: Color4, amount: number, result: Color4): void {
  151. result.r = left.r + (right.r - left.r) * amount;
  152. result.g = left.g + (right.g - left.g) * amount;
  153. result.b = left.b + (right.b - left.b) * amount;
  154. result.a = left.a + (right.a - left.a) * amount;
  155. }
  156. public static FromArray(array: number[], offset: number = 0): Color4 {
  157. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  158. }
  159. public static FromInts(r: number, g: number, b: number, a: number): Color4 {
  160. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  161. }
  162. }
  163. export class Vector2 {
  164. constructor(public x: number, public y: number) {
  165. }
  166. public toString(): string {
  167. return "{X: " + this.x + " Y:" + this.y + "}";
  168. }
  169. // Operators
  170. public toArray(array: number[], index?: number): void {
  171. if (index === undefined) {
  172. index = 0;
  173. }
  174. array[index] = this.x;
  175. array[index + 1] = this.y;
  176. }
  177. public asArray(): number[] {
  178. var result = [];
  179. this.toArray(result, 0);
  180. return result;
  181. }
  182. public copyFrom(source: Vector2): void {
  183. this.x = source.x;
  184. this.y = source.y;
  185. }
  186. public copyFromFloats(x: number, y: number): void {
  187. this.x = x;
  188. this.y = y;
  189. }
  190. public add(otherVector: Vector2): Vector2 {
  191. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  192. }
  193. public subtract(otherVector: Vector2): Vector2 {
  194. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  195. }
  196. public negate(): Vector2 {
  197. return new Vector2(-this.x, -this.y);
  198. }
  199. public scaleInPlace(scale: number): void {
  200. this.x *= scale;
  201. this.y *= scale;
  202. }
  203. public scale(scale: number): Vector2 {
  204. return new Vector2(this.x * scale, this.y * scale);
  205. }
  206. public equals(otherVector: Vector2): boolean {
  207. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  208. }
  209. // Properties
  210. public length(): number {
  211. return Math.sqrt(this.x * this.x + this.y * this.y);
  212. }
  213. public lengthSquared(): number {
  214. return (this.x * this.x + this.y * this.y);
  215. }
  216. // Methods
  217. public normalize(): void {
  218. var len = this.length();
  219. if (len === 0)
  220. return;
  221. var num = 1.0 / len;
  222. this.x *= num;
  223. this.y *= num;
  224. }
  225. public clone(): Vector2 {
  226. return new Vector2(this.x, this.y);
  227. }
  228. // Statics
  229. public static Zero(): Vector2 {
  230. return new Vector2(0, 0);
  231. }
  232. public static FromArray(array: number[], offset?: number): Vector2 {
  233. if (!offset) {
  234. offset = 0;
  235. }
  236. return new Vector2(array[offset], array[offset + 1]);
  237. }
  238. public static FromArrayToRef(array: number[], offset: number, result: Vector2): void {
  239. result.x = array[offset];
  240. result.y = array[offset + 1];
  241. }
  242. public static CatmullRom(value1: Vector2, value2: Vector2, value3: Vector2, value4: Vector2, amount: number): Vector2 {
  243. var squared = amount * amount;
  244. var cubed = amount * squared;
  245. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  246. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  247. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  248. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  249. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  250. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  251. return new Vector2(x, y);
  252. }
  253. public static Clamp(value: Vector2, min: Vector2, max: Vector2): Vector2 {
  254. var x = value.x;
  255. x = (x > max.x) ? max.x : x;
  256. x = (x < min.x) ? min.x : x;
  257. var y = value.y;
  258. y = (y > max.y) ? max.y : y;
  259. y = (y < min.y) ? min.y : y;
  260. return new Vector2(x, y);
  261. }
  262. public static Hermite(value1: Vector2, tangent1: Vector2, value2: Vector2, tangent2: Vector2, amount: number): Vector2 {
  263. var squared = amount * amount;
  264. var cubed = amount * squared;
  265. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  266. var part2 = (-2.0 * cubed) + (3.0 * squared);
  267. var part3 = (cubed - (2.0 * squared)) + amount;
  268. var part4 = cubed - squared;
  269. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  270. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  271. return new Vector2(x, y);
  272. }
  273. public static Lerp(start: Vector2, end: Vector2, amount: number): Vector2 {
  274. var x = start.x + ((end.x - start.x) * amount);
  275. var y = start.y + ((end.y - start.y) * amount);
  276. return new Vector2(x, y);
  277. }
  278. public static Dot(left: Vector2, right: Vector2): number {
  279. return left.x * right.x + left.y * right.y;
  280. }
  281. public static Normalize(vector: Vector2): Vector2 {
  282. var newVector = vector.clone();
  283. newVector.normalize();
  284. return newVector;
  285. }
  286. public static Minimize(left: Vector2, right: Vector2): Vector2 {
  287. var x = (left.x < right.x) ? left.x : right.x;
  288. var y = (left.y < right.y) ? left.y : right.y;
  289. return new Vector2(x, y);
  290. }
  291. public static Maximize(left: Vector2, right: Vector2): Vector2 {
  292. var x = (left.x > right.x) ? left.x : right.x;
  293. var y = (left.y > right.y) ? left.y : right.y;
  294. return new Vector2(x, y);
  295. }
  296. public static Transform(vector: Vector2, transformation: Matrix): Vector2 {
  297. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  298. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  299. return new Vector2(x, y);
  300. }
  301. public static Distance(value1: Vector2, value2: Vector2): number {
  302. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  303. }
  304. public static DistanceSquared(value1: Vector2, value2: Vector2): number {
  305. var x = value1.x - value2.x;
  306. var y = value1.y - value2.y;
  307. return (x * x) + (y * y);
  308. }
  309. }
  310. export class Vector3 {
  311. constructor(public x: number, public y: number, public z: number) {
  312. }
  313. public toString(): string {
  314. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  315. }
  316. // Operators
  317. public asArray(): number[] {
  318. var result = [];
  319. this.toArray(result, 0);
  320. return result;
  321. }
  322. public toArray(array: number[], index?: number): void {
  323. if (index === undefined) {
  324. index = 0;
  325. }
  326. array[index] = this.x;
  327. array[index + 1] = this.y;
  328. array[index + 2] = this.z;
  329. }
  330. public addInPlace(otherVector: Vector3): void {
  331. this.x += otherVector.x;
  332. this.y += otherVector.y;
  333. this.z += otherVector.z;
  334. }
  335. public add(otherVector: Vector3): Vector3 {
  336. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  337. }
  338. public addToRef(otherVector: Vector3, result: Vector3): void {
  339. result.x = this.x + otherVector.x;
  340. result.y = this.y + otherVector.y;
  341. result.z = this.z + otherVector.z;
  342. }
  343. public subtractInPlace(otherVector: Vector3): void {
  344. this.x -= otherVector.x;
  345. this.y -= otherVector.y;
  346. this.z -= otherVector.z;
  347. }
  348. public subtract(otherVector: Vector3): Vector3 {
  349. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  350. }
  351. public subtractToRef(otherVector: Vector3, result: Vector3): void {
  352. result.x = this.x - otherVector.x;
  353. result.y = this.y - otherVector.y;
  354. result.z = this.z - otherVector.z;
  355. }
  356. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  357. return new Vector3(this.x - x, this.y - y, this.z - z);
  358. }
  359. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  360. result.x = this.x - x;
  361. result.y = this.y - y;
  362. result.z = this.z - z;
  363. }
  364. public negate(): Vector3 {
  365. return new Vector3(-this.x, -this.y, -this.z);
  366. }
  367. public scaleInPlace(scale: number): void {
  368. this.x *= scale;
  369. this.y *= scale;
  370. this.z *= scale;
  371. }
  372. public scale(scale: number): Vector3 {
  373. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  374. }
  375. public scaleToRef(scale: number, result: Vector3) {
  376. result.x = this.x * scale;
  377. result.y = this.y * scale;
  378. result.z = this.z * scale;
  379. }
  380. public equals(otherVector: Vector3): boolean {
  381. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  382. }
  383. public equalsWithEpsilon(otherVector: Vector3): boolean {
  384. return Math.abs(this.x - otherVector.x) < Engine.Epsilon &&
  385. Math.abs(this.y - otherVector.y) < Engine.Epsilon &&
  386. Math.abs(this.z - otherVector.z) < Engine.Epsilon;
  387. }
  388. public equalsToFloats(x: number, y: number, z: number): boolean {
  389. return this.x === x && this.y === y && this.z === z;
  390. }
  391. public multiplyInPlace(otherVector: Vector3): void {
  392. this.x *= otherVector.x;
  393. this.y *= otherVector.y;
  394. this.z *= otherVector.z;
  395. }
  396. public multiply(otherVector: Vector3): Vector3 {
  397. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  398. }
  399. public multiplyToRef(otherVector: Vector3, result: Vector3): void {
  400. result.x = this.x * otherVector.x;
  401. result.y = this.y * otherVector.y;
  402. result.z = this.z * otherVector.z;
  403. }
  404. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  405. return new Vector3(this.x * x, this.y * y, this.z * z);
  406. }
  407. public divide(otherVector: Vector3): Vector3 {
  408. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  409. }
  410. public divideToRef(otherVector: Vector3, result: Vector3): void {
  411. result.x = this.x / otherVector.x;
  412. result.y = this.y / otherVector.y;
  413. result.z = this.z / otherVector.z;
  414. }
  415. public MinimizeInPlace(other: Vector3): void {
  416. if (other.x < this.x) this.x = other.x;
  417. if (other.y < this.y) this.y = other.y;
  418. if (other.z < this.z) this.z = other.z;
  419. }
  420. public MaximizeInPlace(other: Vector3): void {
  421. if (other.x > this.x) this.x = other.x;
  422. if (other.y > this.y) this.y = other.y;
  423. if (other.z > this.z) this.z = other.z;
  424. }
  425. // Properties
  426. public length(): number {
  427. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  428. }
  429. public lengthSquared(): number {
  430. return (this.x * this.x + this.y * this.y + this.z * this.z);
  431. }
  432. // Methods
  433. public normalize(): void {
  434. var len = this.length();
  435. if (len === 0)
  436. return;
  437. var num = 1.0 / len;
  438. this.x *= num;
  439. this.y *= num;
  440. this.z *= num;
  441. }
  442. public clone(): Vector3 {
  443. return new Vector3(this.x, this.y, this.z);
  444. }
  445. public copyFrom(source: Vector3): void {
  446. this.x = source.x;
  447. this.y = source.y;
  448. this.z = source.z;
  449. }
  450. public copyFromFloats(x: number, y: number, z: number): void {
  451. this.x = x;
  452. this.y = y;
  453. this.z = z;
  454. }
  455. // Statics
  456. public static FromArray(array: number[], offset?: number): Vector3 {
  457. if (!offset) {
  458. offset = 0;
  459. }
  460. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  461. }
  462. public static FromArrayToRef(array: number[], offset: number, result: Vector3): void {
  463. result.x = array[offset];
  464. result.y = array[offset + 1];
  465. result.z = array[offset + 2];
  466. }
  467. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector3): void {
  468. result.x = array[offset];
  469. result.y = array[offset + 1];
  470. result.z = array[offset + 2];
  471. }
  472. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  473. result.x = x;
  474. result.y = y;
  475. result.z = z;
  476. }
  477. public static Zero(): Vector3 {
  478. return new Vector3(0, 0, 0);
  479. }
  480. public static Up(): Vector3 {
  481. return new Vector3(0, 1.0, 0);
  482. }
  483. public static TransformCoordinates(vector: Vector3, transformation: Matrix): Vector3 {
  484. var result = Vector3.Zero();
  485. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  486. return result;
  487. }
  488. public static TransformCoordinatesToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  489. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  490. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  491. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  492. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  493. result.x = x / w;
  494. result.y = y / w;
  495. result.z = z / w;
  496. }
  497. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  498. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  499. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  500. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  501. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  502. result.x = rx / rw;
  503. result.y = ry / rw;
  504. result.z = rz / rw;
  505. }
  506. public static TransformNormal(vector: Vector3, transformation: Matrix): Vector3 {
  507. var result = Vector3.Zero();
  508. Vector3.TransformNormalToRef(vector, transformation, result);
  509. return result;
  510. }
  511. public static TransformNormalToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  512. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  513. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  514. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  515. }
  516. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  517. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  518. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  519. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  520. }
  521. public static CatmullRom(value1: Vector3, value2: Vector3, value3: Vector3, value4: Vector3, amount: number): Vector3 {
  522. var squared = amount * amount;
  523. var cubed = amount * squared;
  524. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  525. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  526. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  527. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  528. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  529. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  530. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  531. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  532. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  533. return new Vector3(x, y, z);
  534. }
  535. public static Clamp(value: Vector3, min: Vector3, max: Vector3): Vector3 {
  536. var x = value.x;
  537. x = (x > max.x) ? max.x : x;
  538. x = (x < min.x) ? min.x : x;
  539. var y = value.y;
  540. y = (y > max.y) ? max.y : y;
  541. y = (y < min.y) ? min.y : y;
  542. var z = value.z;
  543. z = (z > max.z) ? max.z : z;
  544. z = (z < min.z) ? min.z : z;
  545. return new Vector3(x, y, z);
  546. }
  547. public static Hermite(value1: Vector3, tangent1: Vector3, value2: Vector3, tangent2: Vector3, amount: number): Vector3 {
  548. var squared = amount * amount;
  549. var cubed = amount * squared;
  550. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  551. var part2 = (-2.0 * cubed) + (3.0 * squared);
  552. var part3 = (cubed - (2.0 * squared)) + amount;
  553. var part4 = cubed - squared;
  554. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  555. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  556. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  557. return new Vector3(x, y, z);
  558. }
  559. public static Lerp(start: Vector3, end: Vector3, amount: number): Vector3 {
  560. var x = start.x + ((end.x - start.x) * amount);
  561. var y = start.y + ((end.y - start.y) * amount);
  562. var z = start.z + ((end.z - start.z) * amount);
  563. return new Vector3(x, y, z);
  564. }
  565. public static Dot(left: Vector3, right: Vector3): number {
  566. return (left.x * right.x + left.y * right.y + left.z * right.z);
  567. }
  568. public static Cross(left: Vector3, right: Vector3): Vector3 {
  569. var result = Vector3.Zero();
  570. Vector3.CrossToRef(left, right, result);
  571. return result;
  572. }
  573. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  574. result.x = left.y * right.z - left.z * right.y;
  575. result.y = left.z * right.x - left.x * right.z;
  576. result.z = left.x * right.y - left.y * right.x;
  577. }
  578. public static Normalize(vector: Vector3): Vector3 {
  579. var result = Vector3.Zero();
  580. Vector3.NormalizeToRef(vector, result);
  581. return result;
  582. }
  583. public static NormalizeToRef(vector: Vector3, result: Vector3): void {
  584. result.copyFrom(vector);
  585. result.normalize();
  586. }
  587. public static Project(vector: Vector3, world: Matrix, transform: Matrix, viewport: Viewport): Vector3 {
  588. var cw = viewport.width;
  589. var ch = viewport.height;
  590. var cx = viewport.x;
  591. var cy = viewport.y;
  592. var viewportMatrix = BABYLON.Matrix.FromValues(
  593. cw / 2.0, 0, 0, 0,
  594. 0, -ch / 2.0, 0, 0,
  595. 0, 0, 1, 0,
  596. cx + cw / 2.0, ch / 2.0 + cy, 0, 1);
  597. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  598. return Vector3.TransformCoordinates(vector, finalMatrix);
  599. }
  600. public static Unproject(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Vector3 {
  601. var matrix = world.multiply(view).multiply(projection);
  602. matrix.invert();
  603. source.x = source.x / viewportWidth * 2 - 1;
  604. source.y = -(source.y / viewportHeight * 2 - 1);
  605. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  606. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  607. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  608. vector = vector.scale(1.0 / num);
  609. }
  610. return vector;
  611. }
  612. public static Minimize(left: Vector3, right: Vector3): Vector3 {
  613. var min = left.clone();
  614. min.MinimizeInPlace(right);
  615. return min;
  616. }
  617. public static Maximize(left: Vector3, right: Vector3): Vector3 {
  618. var max = left.clone();
  619. max.MaximizeInPlace(right);
  620. return max;
  621. }
  622. public static Distance(value1: Vector3, value2: Vector3): number {
  623. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  624. }
  625. public static DistanceSquared(value1: Vector3, value2: Vector3): number {
  626. var x = value1.x - value2.x;
  627. var y = value1.y - value2.y;
  628. var z = value1.z - value2.z;
  629. return (x * x) + (y * y) + (z * z);
  630. }
  631. public static Center(value1: Vector3, value2: Vector3): Vector3 {
  632. var center = value1.add(value2);
  633. center.scaleInPlace(0.5);
  634. return center;
  635. }
  636. }
  637. export class Quaternion {
  638. constructor(public x: number = 0, public y: number = 0, public z: number = 0, public w: number = 1) {
  639. }
  640. public toString(): string {
  641. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  642. }
  643. public asArray(): number[] {
  644. return [this.x, this.y, this.z, this.w];
  645. }
  646. public equals(otherQuaternion: Quaternion): boolean {
  647. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  648. }
  649. public clone(): Quaternion {
  650. return new Quaternion(this.x, this.y, this.z, this.w);
  651. }
  652. public copyFrom(other: Quaternion): void {
  653. this.x = other.x;
  654. this.y = other.y;
  655. this.z = other.z;
  656. this.w = other.w;
  657. }
  658. public add(other: Quaternion): Quaternion {
  659. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  660. }
  661. public subtract(other: Quaternion): Quaternion {
  662. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  663. }
  664. public scale(value: number): Quaternion {
  665. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  666. }
  667. public multiply(q1: Quaternion): Quaternion {
  668. var result = new Quaternion(0, 0, 0, 1.0);
  669. this.multiplyToRef(q1, result);
  670. return result;
  671. }
  672. public multiplyToRef(q1: Quaternion, result: Quaternion): void {
  673. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  674. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  675. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  676. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  677. }
  678. public length(): number {
  679. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  680. }
  681. public normalize(): void {
  682. var length = 1.0 / this.length();
  683. this.x *= length;
  684. this.y *= length;
  685. this.z *= length;
  686. this.w *= length;
  687. }
  688. public toEulerAngles(): Vector3 {
  689. var qx = this.x;
  690. var qy = this.y;
  691. var qz = this.z;
  692. var qw = this.w;
  693. var sqx = qx * qx;
  694. var sqy = qy * qy;
  695. var sqz = qz * qz;
  696. var yaw = Math.atan2(2.0 * (qy * qw - qx * qz), 1.0 - 2.0 * (sqy + sqz));
  697. var pitch = Math.asin(2.0 * (qx * qy + qz * qw));
  698. var roll = Math.atan2(2.0 * (qx * qw - qy * qz), 1.0 - 2.0 * (sqx + sqz));
  699. var gimbaLockTest = qx * qy + qz * qw;
  700. if (gimbaLockTest > 0.499) {
  701. yaw = 2.0 * Math.atan2(qx, qw);
  702. roll = 0;
  703. } else if (gimbaLockTest < -0.499) {
  704. yaw = -2.0 * Math.atan2(qx, qw);
  705. roll = 0;
  706. }
  707. return new Vector3(pitch, yaw, roll);
  708. }
  709. public toRotationMatrix(result: Matrix): void {
  710. var xx = this.x * this.x;
  711. var yy = this.y * this.y;
  712. var zz = this.z * this.z;
  713. var xy = this.x * this.y;
  714. var zw = this.z * this.w;
  715. var zx = this.z * this.x;
  716. var yw = this.y * this.w;
  717. var yz = this.y * this.z;
  718. var xw = this.x * this.w;
  719. result.m[0] = 1.0 - (2.0 * (yy + zz));
  720. result.m[1] = 2.0 * (xy + zw);
  721. result.m[2] = 2.0 * (zx - yw);
  722. result.m[3] = 0;
  723. result.m[4] = 2.0 * (xy - zw);
  724. result.m[5] = 1.0 - (2.0 * (zz + xx));
  725. result.m[6] = 2.0 * (yz + xw);
  726. result.m[7] = 0;
  727. result.m[8] = 2.0 * (zx + yw);
  728. result.m[9] = 2.0 * (yz - xw);
  729. result.m[10] = 1.0 - (2.0 * (yy + xx));
  730. result.m[11] = 0;
  731. result.m[12] = 0;
  732. result.m[13] = 0;
  733. result.m[14] = 0;
  734. result.m[15] = 1.0;
  735. }
  736. public fromRotationMatrix(matrix: Matrix): void {
  737. var data = matrix.m;
  738. var m11 = data[0], m12 = data[4], m13 = data[8];
  739. var m21 = data[1], m22 = data[5], m23 = data[9];
  740. var m31 = data[2], m32 = data[6], m33 = data[10];
  741. var trace = m11 + m22 + m33;
  742. var s;
  743. if (trace > 0) {
  744. s = 0.5 / Math.sqrt(trace + 1.0);
  745. this.w = 0.25 / s;
  746. this.x = (m32 - m23) * s;
  747. this.y = (m13 - m31) * s;
  748. this.z = (m21 - m12) * s;
  749. return;
  750. }
  751. if (m11 > m22 && m11 > m33) {
  752. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  753. this.w = (m32 - m23) / s;
  754. this.x = 0.25 * s;
  755. this.y = (m12 + m21) / s;
  756. this.z = (m13 + m31) / s;
  757. return;
  758. }
  759. if (m22 > m33) {
  760. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  761. this.w = (m13 - m31) / s;
  762. this.x = (m12 + m21) / s;
  763. this.y = 0.25 * s;
  764. this.z = (m23 + m32) / s;
  765. return;
  766. }
  767. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  768. this.w = (m21 - m12) / s;
  769. this.x = (m13 + m31) / s;
  770. this.y = (m23 + m32) / s;
  771. this.z = 0.25 * s;
  772. }
  773. // Statics
  774. public static RotationAxis(axis: Vector3, angle: number): Quaternion {
  775. var result = new Quaternion();
  776. var sin = Math.sin(angle / 2);
  777. result.w = Math.cos(angle / 2);
  778. result.x = axis.x * sin;
  779. result.y = axis.y * sin;
  780. result.z = axis.z * sin;
  781. return result;
  782. }
  783. public static FromArray(array: number[], offset?: number): Quaternion {
  784. if (!offset) {
  785. offset = 0;
  786. }
  787. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  788. }
  789. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  790. var result = new Quaternion();
  791. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  792. return result;
  793. }
  794. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  795. var halfRoll = roll * 0.5;
  796. var halfPitch = pitch * 0.5;
  797. var halfYaw = yaw * 0.5;
  798. var sinRoll = Math.sin(halfRoll);
  799. var cosRoll = Math.cos(halfRoll);
  800. var sinPitch = Math.sin(halfPitch);
  801. var cosPitch = Math.cos(halfPitch);
  802. var sinYaw = Math.sin(halfYaw);
  803. var cosYaw = Math.cos(halfYaw);
  804. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  805. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  806. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  807. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  808. }
  809. public static Slerp(left: Quaternion, right: Quaternion, amount: number): Quaternion {
  810. var num2;
  811. var num3;
  812. var num = amount;
  813. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  814. var flag = false;
  815. if (num4 < 0) {
  816. flag = true;
  817. num4 = -num4;
  818. }
  819. if (num4 > 0.999999) {
  820. num3 = 1 - num;
  821. num2 = flag ? -num : num;
  822. }
  823. else {
  824. var num5 = Math.acos(num4);
  825. var num6 = (1.0 / Math.sin(num5));
  826. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  827. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  828. }
  829. return new Quaternion((num3 * left.x) + (num2 * right.x), (num3 * left.y) + (num2 * right.y), (num3 * left.z) + (num2 * right.z), (num3 * left.w) + (num2 * right.w));
  830. }
  831. }
  832. export class Matrix {
  833. private static _tempQuaternion: Quaternion = new Quaternion();
  834. private static _xAxis: Vector3 = Vector3.Zero();
  835. private static _yAxis: Vector3 = Vector3.Zero();
  836. private static _zAxis: Vector3 = Vector3.Zero();
  837. public m: Float32Array = new Float32Array(16);
  838. // Properties
  839. public isIdentity(): boolean {
  840. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  841. return false;
  842. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  843. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  844. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  845. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  846. return false;
  847. return true;
  848. }
  849. public determinant(): number {
  850. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  851. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  852. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  853. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  854. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  855. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  856. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  857. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  858. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  859. }
  860. // Methods
  861. public toArray(): Float32Array {
  862. return this.m;
  863. }
  864. public asArray(): Float32Array {
  865. return this.toArray();
  866. }
  867. public invert(): void {
  868. this.invertToRef(this);
  869. }
  870. public invertToRef(other: Matrix) {
  871. var l1 = this.m[0];
  872. var l2 = this.m[1];
  873. var l3 = this.m[2];
  874. var l4 = this.m[3];
  875. var l5 = this.m[4];
  876. var l6 = this.m[5];
  877. var l7 = this.m[6];
  878. var l8 = this.m[7];
  879. var l9 = this.m[8];
  880. var l10 = this.m[9];
  881. var l11 = this.m[10];
  882. var l12 = this.m[11];
  883. var l13 = this.m[12];
  884. var l14 = this.m[13];
  885. var l15 = this.m[14];
  886. var l16 = this.m[15];
  887. var l17 = (l11 * l16) - (l12 * l15);
  888. var l18 = (l10 * l16) - (l12 * l14);
  889. var l19 = (l10 * l15) - (l11 * l14);
  890. var l20 = (l9 * l16) - (l12 * l13);
  891. var l21 = (l9 * l15) - (l11 * l13);
  892. var l22 = (l9 * l14) - (l10 * l13);
  893. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  894. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  895. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  896. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  897. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  898. var l28 = (l7 * l16) - (l8 * l15);
  899. var l29 = (l6 * l16) - (l8 * l14);
  900. var l30 = (l6 * l15) - (l7 * l14);
  901. var l31 = (l5 * l16) - (l8 * l13);
  902. var l32 = (l5 * l15) - (l7 * l13);
  903. var l33 = (l5 * l14) - (l6 * l13);
  904. var l34 = (l7 * l12) - (l8 * l11);
  905. var l35 = (l6 * l12) - (l8 * l10);
  906. var l36 = (l6 * l11) - (l7 * l10);
  907. var l37 = (l5 * l12) - (l8 * l9);
  908. var l38 = (l5 * l11) - (l7 * l9);
  909. var l39 = (l5 * l10) - (l6 * l9);
  910. other.m[0] = l23 * l27;
  911. other.m[4] = l24 * l27;
  912. other.m[8] = l25 * l27;
  913. other.m[12] = l26 * l27;
  914. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  915. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  916. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  917. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  918. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  919. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  920. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  921. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  922. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  923. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  924. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  925. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  926. }
  927. public setTranslation(vector3: Vector3): void {
  928. this.m[12] = vector3.x;
  929. this.m[13] = vector3.y;
  930. this.m[14] = vector3.z;
  931. }
  932. public multiply(other: Matrix): Matrix {
  933. var result = new Matrix();
  934. this.multiplyToRef(other, result);
  935. return result;
  936. }
  937. public copyFrom(other: Matrix): void {
  938. for (var index = 0; index < 16; index++) {
  939. this.m[index] = other.m[index];
  940. }
  941. }
  942. public copyToArray(array: Float32Array, offset: number = 0): void {
  943. for (var index = 0; index < 16; index++) {
  944. array[offset + index] = this.m[index];
  945. }
  946. }
  947. public multiplyToRef(other: Matrix, result: Matrix): void {
  948. this.multiplyToArray(other, result.m, 0);
  949. }
  950. public multiplyToArray(other: Matrix, result: Float32Array, offset: number): void {
  951. var tm0 = this.m[0];
  952. var tm1 = this.m[1];
  953. var tm2 = this.m[2];
  954. var tm3 = this.m[3];
  955. var tm4 = this.m[4];
  956. var tm5 = this.m[5];
  957. var tm6 = this.m[6];
  958. var tm7 = this.m[7];
  959. var tm8 = this.m[8];
  960. var tm9 = this.m[9];
  961. var tm10 = this.m[10];
  962. var tm11 = this.m[11];
  963. var tm12 = this.m[12];
  964. var tm13 = this.m[13];
  965. var tm14 = this.m[14];
  966. var tm15 = this.m[15];
  967. var om0 = other.m[0];
  968. var om1 = other.m[1];
  969. var om2 = other.m[2];
  970. var om3 = other.m[3];
  971. var om4 = other.m[4];
  972. var om5 = other.m[5];
  973. var om6 = other.m[6];
  974. var om7 = other.m[7];
  975. var om8 = other.m[8];
  976. var om9 = other.m[9];
  977. var om10 = other.m[10];
  978. var om11 = other.m[11];
  979. var om12 = other.m[12];
  980. var om13 = other.m[13];
  981. var om14 = other.m[14];
  982. var om15 = other.m[15];
  983. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  984. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  985. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  986. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  987. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  988. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  989. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  990. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  991. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  992. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  993. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  994. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  995. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  996. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  997. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  998. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  999. }
  1000. public equals(value: Matrix): boolean {
  1001. return value &&
  1002. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  1003. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  1004. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  1005. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  1006. }
  1007. public clone(): Matrix {
  1008. return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  1009. this.m[4], this.m[5], this.m[6], this.m[7],
  1010. this.m[8], this.m[9], this.m[10], this.m[11],
  1011. this.m[12], this.m[13], this.m[14], this.m[15]);
  1012. }
  1013. // Statics
  1014. public static FromArray(array: number[], offset?: number): Matrix {
  1015. var result = new Matrix();
  1016. if (!offset) {
  1017. offset = 0;
  1018. }
  1019. Matrix.FromArrayToRef(array, offset, result);
  1020. return result;
  1021. }
  1022. public static FromArrayToRef(array: number[], offset: number, result: Matrix) {
  1023. for (var index = 0; index < 16; index++) {
  1024. result.m[index] = array[index + offset];
  1025. }
  1026. }
  1027. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  1028. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  1029. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  1030. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  1031. result.m[0] = initialM11;
  1032. result.m[1] = initialM12;
  1033. result.m[2] = initialM13;
  1034. result.m[3] = initialM14;
  1035. result.m[4] = initialM21;
  1036. result.m[5] = initialM22;
  1037. result.m[6] = initialM23;
  1038. result.m[7] = initialM24;
  1039. result.m[8] = initialM31;
  1040. result.m[9] = initialM32;
  1041. result.m[10] = initialM33;
  1042. result.m[11] = initialM34;
  1043. result.m[12] = initialM41;
  1044. result.m[13] = initialM42;
  1045. result.m[14] = initialM43;
  1046. result.m[15] = initialM44;
  1047. }
  1048. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  1049. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  1050. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  1051. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  1052. var result = new Matrix();
  1053. result.m[0] = initialM11;
  1054. result.m[1] = initialM12;
  1055. result.m[2] = initialM13;
  1056. result.m[3] = initialM14;
  1057. result.m[4] = initialM21;
  1058. result.m[5] = initialM22;
  1059. result.m[6] = initialM23;
  1060. result.m[7] = initialM24;
  1061. result.m[8] = initialM31;
  1062. result.m[9] = initialM32;
  1063. result.m[10] = initialM33;
  1064. result.m[11] = initialM34;
  1065. result.m[12] = initialM41;
  1066. result.m[13] = initialM42;
  1067. result.m[14] = initialM43;
  1068. result.m[15] = initialM44;
  1069. return result;
  1070. }
  1071. public static Identity(): Matrix {
  1072. return Matrix.FromValues(1.0, 0, 0, 0,
  1073. 0, 1.0, 0, 0,
  1074. 0, 0, 1.0, 0,
  1075. 0, 0, 0, 1.0);
  1076. }
  1077. public static IdentityToRef(result: Matrix): void {
  1078. Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1079. 0, 1.0, 0, 0,
  1080. 0, 0, 1.0, 0,
  1081. 0, 0, 0, 1.0, result);
  1082. }
  1083. public static Zero(): Matrix {
  1084. return Matrix.FromValues(0, 0, 0, 0,
  1085. 0, 0, 0, 0,
  1086. 0, 0, 0, 0,
  1087. 0, 0, 0, 0);
  1088. }
  1089. public static RotationX(angle: number): Matrix {
  1090. var result = new Matrix();
  1091. Matrix.RotationXToRef(angle, result);
  1092. return result;
  1093. }
  1094. public static RotationXToRef(angle: number, result: Matrix): void {
  1095. var s = Math.sin(angle);
  1096. var c = Math.cos(angle);
  1097. result.m[0] = 1.0;
  1098. result.m[15] = 1.0;
  1099. result.m[5] = c;
  1100. result.m[10] = c;
  1101. result.m[9] = -s;
  1102. result.m[6] = s;
  1103. result.m[1] = 0;
  1104. result.m[2] = 0;
  1105. result.m[3] = 0;
  1106. result.m[4] = 0;
  1107. result.m[7] = 0;
  1108. result.m[8] = 0;
  1109. result.m[11] = 0;
  1110. result.m[12] = 0;
  1111. result.m[13] = 0;
  1112. result.m[14] = 0;
  1113. }
  1114. public static RotationY(angle: number): Matrix {
  1115. var result = new Matrix();
  1116. Matrix.RotationYToRef(angle, result);
  1117. return result;
  1118. }
  1119. public static RotationYToRef(angle: number, result: Matrix): void {
  1120. var s = Math.sin(angle);
  1121. var c = Math.cos(angle);
  1122. result.m[5] = 1.0;
  1123. result.m[15] = 1.0;
  1124. result.m[0] = c;
  1125. result.m[2] = -s;
  1126. result.m[8] = s;
  1127. result.m[10] = c;
  1128. result.m[1] = 0;
  1129. result.m[3] = 0;
  1130. result.m[4] = 0;
  1131. result.m[6] = 0;
  1132. result.m[7] = 0;
  1133. result.m[9] = 0;
  1134. result.m[11] = 0;
  1135. result.m[12] = 0;
  1136. result.m[13] = 0;
  1137. result.m[14] = 0;
  1138. }
  1139. public static RotationZ(angle: number): Matrix {
  1140. var result = new Matrix();
  1141. Matrix.RotationZToRef(angle, result);
  1142. return result;
  1143. }
  1144. public static RotationZToRef(angle: number, result: Matrix): void {
  1145. var s = Math.sin(angle);
  1146. var c = Math.cos(angle);
  1147. result.m[10] = 1.0;
  1148. result.m[15] = 1.0;
  1149. result.m[0] = c;
  1150. result.m[1] = s;
  1151. result.m[4] = -s;
  1152. result.m[5] = c;
  1153. result.m[2] = 0;
  1154. result.m[3] = 0;
  1155. result.m[6] = 0;
  1156. result.m[7] = 0;
  1157. result.m[8] = 0;
  1158. result.m[9] = 0;
  1159. result.m[11] = 0;
  1160. result.m[12] = 0;
  1161. result.m[13] = 0;
  1162. result.m[14] = 0;
  1163. }
  1164. public static RotationAxis(axis: Vector3, angle: number): Matrix {
  1165. var s = Math.sin(-angle);
  1166. var c = Math.cos(-angle);
  1167. var c1 = 1 - c;
  1168. axis.normalize();
  1169. var result = Matrix.Zero();
  1170. result.m[0] = (axis.x * axis.x) * c1 + c;
  1171. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1172. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1173. result.m[3] = 0.0;
  1174. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1175. result.m[5] = (axis.y * axis.y) * c1 + c;
  1176. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1177. result.m[7] = 0.0;
  1178. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1179. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1180. result.m[10] = (axis.z * axis.z) * c1 + c;
  1181. result.m[11] = 0.0;
  1182. result.m[15] = 1.0;
  1183. return result;
  1184. }
  1185. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  1186. var result = new Matrix();
  1187. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1188. return result;
  1189. }
  1190. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  1191. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1192. this._tempQuaternion.toRotationMatrix(result);
  1193. }
  1194. public static Scaling(x: number, y: number, z: number): Matrix {
  1195. var result = Matrix.Zero();
  1196. Matrix.ScalingToRef(x, y, z, result);
  1197. return result;
  1198. }
  1199. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  1200. result.m[0] = x;
  1201. result.m[1] = 0;
  1202. result.m[2] = 0;
  1203. result.m[3] = 0;
  1204. result.m[4] = 0;
  1205. result.m[5] = y;
  1206. result.m[6] = 0;
  1207. result.m[7] = 0;
  1208. result.m[8] = 0;
  1209. result.m[9] = 0;
  1210. result.m[10] = z;
  1211. result.m[11] = 0;
  1212. result.m[12] = 0;
  1213. result.m[13] = 0;
  1214. result.m[14] = 0;
  1215. result.m[15] = 1.0;
  1216. }
  1217. public static Translation(x: number, y: number, z: number): Matrix {
  1218. var result = Matrix.Identity();
  1219. Matrix.TranslationToRef(x, y, z, result);
  1220. return result;
  1221. }
  1222. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  1223. Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1224. 0, 1.0, 0, 0,
  1225. 0, 0, 1.0, 0,
  1226. x, y, z, 1.0, result);
  1227. }
  1228. public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  1229. var result = Matrix.Zero();
  1230. Matrix.LookAtLHToRef(eye, target, up, result);
  1231. return result;
  1232. }
  1233. public static LookAtLHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  1234. // Z axis
  1235. target.subtractToRef(eye, this._zAxis);
  1236. this._zAxis.normalize();
  1237. // X axis
  1238. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1239. this._xAxis.normalize();
  1240. // Y axis
  1241. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1242. this._yAxis.normalize();
  1243. // Eye angles
  1244. var ex = -Vector3.Dot(this._xAxis, eye);
  1245. var ey = -Vector3.Dot(this._yAxis, eye);
  1246. var ez = -Vector3.Dot(this._zAxis, eye);
  1247. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
  1248. this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
  1249. this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
  1250. ex, ey, ez, 1, result);
  1251. }
  1252. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  1253. var hw = 2.0 / width;
  1254. var hh = 2.0 / height;
  1255. var id = 1.0 / (zfar - znear);
  1256. var nid = znear / (znear - zfar);
  1257. return Matrix.FromValues(hw, 0, 0, 0,
  1258. 0, hh, 0, 0,
  1259. 0, 0, id, 0,
  1260. 0, 0, nid, 1);
  1261. }
  1262. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  1263. var matrix = Matrix.Zero();
  1264. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1265. return matrix;
  1266. }
  1267. public static OrthoOffCenterLHToRef(left: number, right, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  1268. result.m[0] = 2.0 / (right - left);
  1269. result.m[1] = result.m[2] = result.m[3] = 0;
  1270. result.m[5] = 2.0 / (top - bottom);
  1271. result.m[4] = result.m[6] = result.m[7] = 0;
  1272. result.m[10] = -1.0 / (znear - zfar);
  1273. result.m[8] = result.m[9] = result.m[11] = 0;
  1274. result.m[12] = (left + right) / (left - right);
  1275. result.m[13] = (top + bottom) / (bottom - top);
  1276. result.m[14] = znear / (znear - zfar);
  1277. result.m[15] = 1.0;
  1278. }
  1279. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  1280. var matrix = Matrix.Zero();
  1281. matrix.m[0] = (2.0 * znear) / width;
  1282. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1283. matrix.m[5] = (2.0 * znear) / height;
  1284. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1285. matrix.m[10] = -zfar / (znear - zfar);
  1286. matrix.m[8] = matrix.m[9] = 0.0;
  1287. matrix.m[11] = 1.0;
  1288. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1289. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1290. return matrix;
  1291. }
  1292. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  1293. var matrix = Matrix.Zero();
  1294. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1295. return matrix;
  1296. }
  1297. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix): void {
  1298. var tan = 1.0 / (Math.tan(fov * 0.5));
  1299. result.m[0] = tan / aspect;
  1300. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1301. result.m[5] = tan;
  1302. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1303. result.m[8] = result.m[9] = 0.0;
  1304. result.m[10] = -zfar / (znear - zfar);
  1305. result.m[11] = 1.0;
  1306. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1307. result.m[14] = (znear * zfar) / (znear - zfar);
  1308. }
  1309. public static GetFinalMatrix(viewport: Viewport, world: Matrix, view: Matrix, projection: Matrix, zmin: number, zmax: number): Matrix {
  1310. var cw = viewport.width;
  1311. var ch = viewport.height;
  1312. var cx = viewport.x;
  1313. var cy = viewport.y;
  1314. var viewportMatrix = Matrix.FromValues(cw / 2.0, 0, 0, 0,
  1315. 0, -ch / 2.0, 0, 0,
  1316. 0, 0, zmax - zmin, 0,
  1317. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  1318. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1319. }
  1320. public static Transpose(matrix: Matrix): Matrix {
  1321. var result = new Matrix();
  1322. result.m[0] = matrix.m[0];
  1323. result.m[1] = matrix.m[4];
  1324. result.m[2] = matrix.m[8];
  1325. result.m[3] = matrix.m[12];
  1326. result.m[4] = matrix.m[1];
  1327. result.m[5] = matrix.m[5];
  1328. result.m[6] = matrix.m[9];
  1329. result.m[7] = matrix.m[13];
  1330. result.m[8] = matrix.m[2];
  1331. result.m[9] = matrix.m[6];
  1332. result.m[10] = matrix.m[10];
  1333. result.m[11] = matrix.m[14];
  1334. result.m[12] = matrix.m[3];
  1335. result.m[13] = matrix.m[7];
  1336. result.m[14] = matrix.m[11];
  1337. result.m[15] = matrix.m[15];
  1338. return result;
  1339. }
  1340. public static Reflection(plane: Plane): Matrix {
  1341. var matrix = new Matrix();
  1342. Matrix.ReflectionToRef(plane, matrix);
  1343. return matrix;
  1344. }
  1345. public static ReflectionToRef(plane: Plane, result: Matrix): void {
  1346. plane.normalize();
  1347. var x = plane.normal.x;
  1348. var y = plane.normal.y;
  1349. var z = plane.normal.z;
  1350. var temp = -2 * x;
  1351. var temp2 = -2 * y;
  1352. var temp3 = -2 * z;
  1353. result.m[0] = (temp * x) + 1;
  1354. result.m[1] = temp2 * x;
  1355. result.m[2] = temp3 * x;
  1356. result.m[3] = 0.0;
  1357. result.m[4] = temp * y;
  1358. result.m[5] = (temp2 * y) + 1;
  1359. result.m[6] = temp3 * y;
  1360. result.m[7] = 0.0;
  1361. result.m[8] = temp * z;
  1362. result.m[9] = temp2 * z;
  1363. result.m[10] = (temp3 * z) + 1;
  1364. result.m[11] = 0.0;
  1365. result.m[12] = temp * plane.d;
  1366. result.m[13] = temp2 * plane.d;
  1367. result.m[14] = temp3 * plane.d;
  1368. result.m[15] = 1.0;
  1369. }
  1370. }
  1371. export class Plane {
  1372. public normal: Vector3;
  1373. public d: number;
  1374. constructor(a: number, b: number, c: number, d: number) {
  1375. this.normal = new Vector3(a, b, c);
  1376. this.d = d;
  1377. }
  1378. public asArray(): number[] {
  1379. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  1380. }
  1381. // Methods
  1382. public clone(): Plane {
  1383. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  1384. }
  1385. public normalize(): void {
  1386. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1387. var magnitude = 0;
  1388. if (norm != 0) {
  1389. magnitude = 1.0 / norm;
  1390. }
  1391. this.normal.x *= magnitude;
  1392. this.normal.y *= magnitude;
  1393. this.normal.z *= magnitude;
  1394. this.d *= magnitude;
  1395. }
  1396. public transform(transformation: Matrix): Plane {
  1397. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  1398. var x = this.normal.x;
  1399. var y = this.normal.y;
  1400. var z = this.normal.z;
  1401. var d = this.d;
  1402. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1403. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1404. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1405. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1406. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  1407. }
  1408. public dotCoordinate(point): number {
  1409. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1410. }
  1411. public copyFromPoints(point1: Vector3, point2: Vector3, point3: Vector3): void {
  1412. var x1 = point2.x - point1.x;
  1413. var y1 = point2.y - point1.y;
  1414. var z1 = point2.z - point1.z;
  1415. var x2 = point3.x - point1.x;
  1416. var y2 = point3.y - point1.y;
  1417. var z2 = point3.z - point1.z;
  1418. var yz = (y1 * z2) - (z1 * y2);
  1419. var xz = (z1 * x2) - (x1 * z2);
  1420. var xy = (x1 * y2) - (y1 * x2);
  1421. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1422. var invPyth;
  1423. if (pyth != 0) {
  1424. invPyth = 1.0 / pyth;
  1425. }
  1426. else {
  1427. invPyth = 0;
  1428. }
  1429. this.normal.x = yz * invPyth;
  1430. this.normal.y = xz * invPyth;
  1431. this.normal.z = xy * invPyth;
  1432. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1433. }
  1434. public isFrontFacingTo(direction: Vector3, epsilon: number): boolean {
  1435. var dot = Vector3.Dot(this.normal, direction);
  1436. return (dot <= epsilon);
  1437. }
  1438. public signedDistanceTo(point: Vector3): number {
  1439. return Vector3.Dot(point, this.normal) + this.d;
  1440. }
  1441. // Statics
  1442. static FromArray(array: number[]): Plane {
  1443. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  1444. }
  1445. static FromPoints(point1, point2, point3): Plane {
  1446. var result = new BABYLON.Plane(0, 0, 0, 0);
  1447. result.copyFromPoints(point1, point2, point3);
  1448. return result;
  1449. }
  1450. static FromPositionAndNormal(origin: Vector3, normal: Vector3): Plane {
  1451. var result = new BABYLON.Plane(0, 0, 0, 0);
  1452. normal.normalize();
  1453. result.normal = normal;
  1454. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1455. return result;
  1456. }
  1457. static SignedDistanceToPlaneFromPositionAndNormal(origin: Vector3, normal: Vector3, point: Vector3): number {
  1458. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1459. return Vector3.Dot(point, normal) + d;
  1460. }
  1461. }
  1462. export class Viewport {
  1463. constructor(public x: number, public y: number, public width: number, public height: number) {
  1464. }
  1465. public toGlobal(engine) {
  1466. var width = engine.getRenderWidth();
  1467. var height = engine.getRenderHeight();
  1468. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  1469. }
  1470. }
  1471. export class Frustum {
  1472. public static GetPlanes(transform: Matrix): Plane[] {
  1473. var frustumPlanes = [];
  1474. for (var index = 0; index < 6; index++) {
  1475. frustumPlanes.push(new Plane(0, 0, 0, 0));
  1476. }
  1477. Frustum.GetPlanesToRef(transform, frustumPlanes);
  1478. return frustumPlanes;
  1479. }
  1480. public static GetPlanesToRef(transform: Matrix, frustumPlanes: Plane[]): void {
  1481. // Near
  1482. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1483. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1484. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1485. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1486. frustumPlanes[0].normalize();
  1487. // Far
  1488. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1489. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1490. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1491. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1492. frustumPlanes[1].normalize();
  1493. // Left
  1494. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1495. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1496. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1497. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1498. frustumPlanes[2].normalize();
  1499. // Right
  1500. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1501. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1502. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1503. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1504. frustumPlanes[3].normalize();
  1505. // Top
  1506. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1507. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1508. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1509. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1510. frustumPlanes[4].normalize();
  1511. // Bottom
  1512. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1513. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1514. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1515. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1516. frustumPlanes[5].normalize();
  1517. }
  1518. }
  1519. export class Ray {
  1520. private _edge1: Vector3;
  1521. private _edge2: Vector3;
  1522. private _pvec: Vector3;
  1523. private _tvec: Vector3;
  1524. private _qvec: Vector3;
  1525. constructor(public origin: Vector3, public direction: Vector3) {
  1526. }
  1527. // Methods
  1528. public intersectsBoxMinMax(minimum: Vector3, maximum: Vector3): boolean {
  1529. var d = 0.0;
  1530. var maxValue = Number.MAX_VALUE;
  1531. if (Math.abs(this.direction.x) < 0.0000001) {
  1532. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  1533. return false;
  1534. }
  1535. }
  1536. else {
  1537. var inv = 1.0 / this.direction.x;
  1538. var min = (minimum.x - this.origin.x) * inv;
  1539. var max = (maximum.x - this.origin.x) * inv;
  1540. if (min > max) {
  1541. var temp = min;
  1542. min = max;
  1543. max = temp;
  1544. }
  1545. d = Math.max(min, d);
  1546. maxValue = Math.min(max, maxValue);
  1547. if (d > maxValue) {
  1548. return false;
  1549. }
  1550. }
  1551. if (Math.abs(this.direction.y) < 0.0000001) {
  1552. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  1553. return false;
  1554. }
  1555. }
  1556. else {
  1557. inv = 1.0 / this.direction.y;
  1558. min = (minimum.y - this.origin.y) * inv;
  1559. max = (maximum.y - this.origin.y) * inv;
  1560. if (min > max) {
  1561. temp = min;
  1562. min = max;
  1563. max = temp;
  1564. }
  1565. d = Math.max(min, d);
  1566. maxValue = Math.min(max, maxValue);
  1567. if (d > maxValue) {
  1568. return false;
  1569. }
  1570. }
  1571. if (Math.abs(this.direction.z) < 0.0000001) {
  1572. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  1573. return false;
  1574. }
  1575. }
  1576. else {
  1577. inv = 1.0 / this.direction.z;
  1578. min = (minimum.z - this.origin.z) * inv;
  1579. max = (maximum.z - this.origin.z) * inv;
  1580. if (min > max) {
  1581. temp = min;
  1582. min = max;
  1583. max = temp;
  1584. }
  1585. d = Math.max(min, d);
  1586. maxValue = Math.min(max, maxValue);
  1587. if (d > maxValue) {
  1588. return false;
  1589. }
  1590. }
  1591. return true;
  1592. }
  1593. public intersectsBox(box: BoundingBox): boolean {
  1594. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  1595. }
  1596. public intersectsSphere(sphere): boolean {
  1597. var x = sphere.center.x - this.origin.x;
  1598. var y = sphere.center.y - this.origin.y;
  1599. var z = sphere.center.z - this.origin.z;
  1600. var pyth = (x * x) + (y * y) + (z * z);
  1601. var rr = sphere.radius * sphere.radius;
  1602. if (pyth <= rr) {
  1603. return true;
  1604. }
  1605. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  1606. if (dot < 0.0) {
  1607. return false;
  1608. }
  1609. var temp = pyth - (dot * dot);
  1610. return temp <= rr;
  1611. }
  1612. public intersectsTriangle(vertex0: Vector3, vertex1: Vector3, vertex2: Vector3): IntersectionInfo {
  1613. if (!this._edge1) {
  1614. this._edge1 = BABYLON.Vector3.Zero();
  1615. this._edge2 = BABYLON.Vector3.Zero();
  1616. this._pvec = BABYLON.Vector3.Zero();
  1617. this._tvec = BABYLON.Vector3.Zero();
  1618. this._qvec = BABYLON.Vector3.Zero();
  1619. }
  1620. vertex1.subtractToRef(vertex0, this._edge1);
  1621. vertex2.subtractToRef(vertex0, this._edge2);
  1622. BABYLON.Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  1623. var det = Vector3.Dot(this._edge1, this._pvec);
  1624. if (det === 0) {
  1625. return null;
  1626. }
  1627. var invdet = 1 / det;
  1628. this.origin.subtractToRef(vertex0, this._tvec);
  1629. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  1630. if (bu < 0 || bu > 1.0) {
  1631. return null;
  1632. }
  1633. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  1634. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  1635. if (bv < 0 || bu + bv > 1.0) {
  1636. return null;
  1637. }
  1638. return new IntersectionInfo(bu, bv, Vector3.Dot(this._edge2, this._qvec) * invdet);
  1639. }
  1640. // Statics
  1641. public static CreateNew(x: number, y: number, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Ray {
  1642. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  1643. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  1644. var direction = end.subtract(start);
  1645. direction.normalize();
  1646. return new Ray(start, direction);
  1647. }
  1648. public static Transform(ray: Ray, matrix: Matrix): Ray {
  1649. var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
  1650. var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
  1651. return new Ray(newOrigin, newDirection);
  1652. }
  1653. }
  1654. export enum Space {
  1655. LOCAL = 0,
  1656. WORLD = 1
  1657. }
  1658. export class Axis {
  1659. public static X: Vector3 = new BABYLON.Vector3(1, 0, 0);
  1660. public static Y: Vector3 = new BABYLON.Vector3(0, 1, 0);
  1661. public static Z: Vector3 = new BABYLON.Vector3(0, 0, 1);
  1662. };
  1663. }