babylon.math.ts 73 KB

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