babylon.math.ts 75 KB

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