babylon.math.ts 76 KB

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