babylon.math.ts 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031
  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 equalsToFloats(x: number, y: number, z: number): boolean {
  376. return this.x === x && this.y === y && this.z === z;
  377. }
  378. public multiplyInPlace(otherVector: Vector3): void {
  379. this.x *= otherVector.x;
  380. this.y *= otherVector.y;
  381. this.z *= otherVector.z;
  382. }
  383. public multiply(otherVector: Vector3): Vector3 {
  384. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  385. }
  386. public multiplyToRef(otherVector: Vector3, result: Vector3): void {
  387. result.x = this.x * otherVector.x;
  388. result.y = this.y * otherVector.y;
  389. result.z = this.z * otherVector.z;
  390. }
  391. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  392. return new Vector3(this.x * x, this.y * y, this.z * z);
  393. }
  394. public divide(otherVector: Vector3): Vector3 {
  395. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  396. }
  397. public divideToRef(otherVector: Vector3, result: Vector3): void {
  398. result.x = this.x / otherVector.x;
  399. result.y = this.y / otherVector.y;
  400. result.z = this.z / otherVector.z;
  401. }
  402. public MinimizeInPlace(other: Vector3): void {
  403. if (other.x < this.x) this.x = other.x;
  404. if (other.y < this.y) this.y = other.y;
  405. if (other.z < this.z) this.z = other.z;
  406. }
  407. public MaximizeInPlace(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. // Properties
  413. public length(): number {
  414. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  415. }
  416. public lengthSquared(): number {
  417. return (this.x * this.x + this.y * this.y + this.z * this.z);
  418. }
  419. // Methods
  420. public normalize(): void {
  421. var len = this.length();
  422. if (len === 0)
  423. return;
  424. var num = 1.0 / len;
  425. this.x *= num;
  426. this.y *= num;
  427. this.z *= num;
  428. }
  429. public clone(): Vector3 {
  430. return new Vector3(this.x, this.y, this.z);
  431. }
  432. public copyFrom(source: Vector3): void {
  433. this.x = source.x;
  434. this.y = source.y;
  435. this.z = source.z;
  436. }
  437. public copyFromFloats(x: number, y: number, z: number): void {
  438. this.x = x;
  439. this.y = y;
  440. this.z = z;
  441. }
  442. // Statics
  443. public static FromArray(array: number[], offset?: number): Vector3 {
  444. if (!offset) {
  445. offset = 0;
  446. }
  447. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  448. }
  449. public static FromArrayToRef(array: number[], offset: number, result: Vector3): void {
  450. result.x = array[offset];
  451. result.y = array[offset + 1];
  452. result.z = array[offset + 2];
  453. }
  454. public static FromFloatArrayToRef(array: Float32Array, 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 FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  460. result.x = x;
  461. result.y = y;
  462. result.z = z;
  463. }
  464. public static Zero(): Vector3 {
  465. return new Vector3(0, 0, 0);
  466. }
  467. public static Up(): Vector3 {
  468. return new Vector3(0, 1.0, 0);
  469. }
  470. public static TransformCoordinates(vector: Vector3, transformation: Matrix): Vector3 {
  471. var result = Vector3.Zero();
  472. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  473. return result;
  474. }
  475. public static TransformCoordinatesToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  476. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  477. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  478. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  479. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  480. result.x = x / w;
  481. result.y = y / w;
  482. result.z = z / w;
  483. }
  484. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  485. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  486. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  487. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  488. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  489. result.x = rx / rw;
  490. result.y = ry / rw;
  491. result.z = rz / rw;
  492. }
  493. public static TransformNormal(vector: Vector3, transformation: Matrix): Vector3 {
  494. var result = Vector3.Zero();
  495. Vector3.TransformNormalToRef(vector, transformation, result);
  496. return result;
  497. }
  498. public static TransformNormalToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  499. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  500. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  501. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  502. }
  503. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  504. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  505. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  506. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  507. }
  508. public static CatmullRom(value1: Vector3, value2: Vector3, value3: Vector3, value4: Vector3, amount: number): Vector3 {
  509. var squared = amount * amount;
  510. var cubed = amount * squared;
  511. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  512. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  513. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  514. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  515. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  516. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  517. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  518. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  519. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  520. return new Vector3(x, y, z);
  521. }
  522. public static Clamp(value: Vector3, min: Vector3, max: Vector3): Vector3 {
  523. var x = value.x;
  524. x = (x > max.x) ? max.x : x;
  525. x = (x < min.x) ? min.x : x;
  526. var y = value.y;
  527. y = (y > max.y) ? max.y : y;
  528. y = (y < min.y) ? min.y : y;
  529. var z = value.z;
  530. z = (z > max.z) ? max.z : z;
  531. z = (z < min.z) ? min.z : z;
  532. return new Vector3(x, y, z);
  533. }
  534. public static Hermite(value1: Vector3, tangent1: Vector3, value2: Vector3, tangent2: Vector3, amount: number): Vector3 {
  535. var squared = amount * amount;
  536. var cubed = amount * squared;
  537. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  538. var part2 = (-2.0 * cubed) + (3.0 * squared);
  539. var part3 = (cubed - (2.0 * squared)) + amount;
  540. var part4 = cubed - squared;
  541. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  542. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  543. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  544. return new Vector3(x, y, z);
  545. }
  546. public static Lerp(start: Vector3, end: Vector3, amount: number): Vector3 {
  547. var x = start.x + ((end.x - start.x) * amount);
  548. var y = start.y + ((end.y - start.y) * amount);
  549. var z = start.z + ((end.z - start.z) * amount);
  550. return new Vector3(x, y, z);
  551. }
  552. public static Dot(left: Vector3, right: Vector3): number {
  553. return (left.x * right.x + left.y * right.y + left.z * right.z);
  554. }
  555. public static Cross(left: Vector3, right: Vector3): Vector3 {
  556. var result = Vector3.Zero();
  557. Vector3.CrossToRef(left, right, result);
  558. return result;
  559. }
  560. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  561. result.x = left.y * right.z - left.z * right.y;
  562. result.y = left.z * right.x - left.x * right.z;
  563. result.z = left.x * right.y - left.y * right.x;
  564. }
  565. public static Normalize(vector: Vector3): Vector3 {
  566. var result = Vector3.Zero();
  567. Vector3.NormalizeToRef(vector, result);
  568. return result;
  569. }
  570. public static NormalizeToRef(vector: Vector3, result: Vector3): void {
  571. result.copyFrom(vector);
  572. result.normalize();
  573. }
  574. public static Project(vector: Vector3, world: Matrix, transform: Matrix, viewport: Viewport): Vector3 {
  575. var cw = viewport.width;
  576. var ch = viewport.height;
  577. var cx = viewport.x;
  578. var cy = viewport.y;
  579. var viewportMatrix = BABYLON.Matrix.FromValues(
  580. cw / 2.0, 0, 0, 0,
  581. 0, -ch / 2.0, 0, 0,
  582. 0, 0, 1, 0,
  583. cx + cw / 2.0, ch / 2.0 + cy, 0, 1);
  584. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  585. return Vector3.TransformCoordinates(vector, finalMatrix);
  586. }
  587. public static Unproject(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Vector3 {
  588. var matrix = world.multiply(view).multiply(projection);
  589. matrix.invert();
  590. source.x = source.x / viewportWidth * 2 - 1;
  591. source.y = -(source.y / viewportHeight * 2 - 1);
  592. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  593. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  594. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  595. vector = vector.scale(1.0 / num);
  596. }
  597. return vector;
  598. }
  599. public static Minimize(left: Vector3, right: Vector3): Vector3 {
  600. var min = left.clone();
  601. min.MinimizeInPlace(right);
  602. return min;
  603. }
  604. public static Maximize(left: Vector3, right: Vector3): Vector3 {
  605. var max = left.clone();
  606. max.MaximizeInPlace(right);
  607. return max;
  608. }
  609. public static Distance(value1: Vector3, value2: Vector3): number {
  610. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  611. }
  612. public static DistanceSquared(value1: Vector3, value2: Vector3): number {
  613. var x = value1.x - value2.x;
  614. var y = value1.y - value2.y;
  615. var z = value1.z - value2.z;
  616. return (x * x) + (y * y) + (z * z);
  617. }
  618. public static Center(value1: Vector3, value2: Vector3): Vector3 {
  619. var center = value1.add(value2);
  620. center.scaleInPlace(0.5);
  621. return center;
  622. }
  623. }
  624. export class Quaternion {
  625. constructor(public x: number = 0, public y: number = 0, public z: number = 0, public w: number = 0) {
  626. }
  627. public toString(): string {
  628. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  629. }
  630. public asArray(): number[] {
  631. return [this.x, this.y, this.z, this.w];
  632. }
  633. public equals(otherQuaternion: Quaternion): boolean {
  634. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  635. }
  636. public clone(): Quaternion {
  637. return new Quaternion(this.x, this.y, this.z, this.w);
  638. }
  639. public copyFrom(other: Quaternion): void {
  640. this.x = other.x;
  641. this.y = other.y;
  642. this.z = other.z;
  643. this.w = other.w;
  644. }
  645. public add(other: Quaternion): Quaternion {
  646. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  647. }
  648. public subtract(other: Quaternion): Quaternion {
  649. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  650. }
  651. public scale(value: number): Quaternion {
  652. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  653. }
  654. public multiply(q1: Quaternion): Quaternion {
  655. var result = new Quaternion(0, 0, 0, 1.0);
  656. this.multiplyToRef(q1, result);
  657. return result;
  658. }
  659. public multiplyToRef(q1: Quaternion, result: Quaternion): void {
  660. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  661. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  662. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  663. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  664. }
  665. public length(): number {
  666. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  667. }
  668. public normalize(): void {
  669. var length = 1.0 / this.length();
  670. this.x *= length;
  671. this.y *= length;
  672. this.z *= length;
  673. this.w *= length;
  674. }
  675. public toEulerAngles(): Vector3 {
  676. var qx = this.x;
  677. var qy = this.y;
  678. var qz = this.z;
  679. var qw = this.w;
  680. var sqx = qx * qx;
  681. var sqy = qy * qy;
  682. var sqz = qz * qz;
  683. var yaw = Math.atan2(2.0 * (qy * qw - qx * qz), 1.0 - 2.0 * (sqy + sqz));
  684. var pitch = Math.asin(2.0 * (qx * qy + qz * qw));
  685. var roll = Math.atan2(2.0 * (qx * qw - qy * qz), 1.0 - 2.0 * (sqx + sqz));
  686. var gimbaLockTest = qx * qy + qz * qw;
  687. if (gimbaLockTest > 0.499) {
  688. yaw = 2.0 * Math.atan2(qx, qw);
  689. roll = 0;
  690. } else if (gimbaLockTest < -0.499) {
  691. yaw = -2.0 * Math.atan2(qx, qw);
  692. roll = 0;
  693. }
  694. return new Vector3(pitch, yaw, roll);
  695. }
  696. public toRotationMatrix(result: Matrix): void {
  697. var xx = this.x * this.x;
  698. var yy = this.y * this.y;
  699. var zz = this.z * this.z;
  700. var xy = this.x * this.y;
  701. var zw = this.z * this.w;
  702. var zx = this.z * this.x;
  703. var yw = this.y * this.w;
  704. var yz = this.y * this.z;
  705. var xw = this.x * this.w;
  706. result.m[0] = 1.0 - (2.0 * (yy + zz));
  707. result.m[1] = 2.0 * (xy + zw);
  708. result.m[2] = 2.0 * (zx - yw);
  709. result.m[3] = 0;
  710. result.m[4] = 2.0 * (xy - zw);
  711. result.m[5] = 1.0 - (2.0 * (zz + xx));
  712. result.m[6] = 2.0 * (yz + xw);
  713. result.m[7] = 0;
  714. result.m[8] = 2.0 * (zx + yw);
  715. result.m[9] = 2.0 * (yz - xw);
  716. result.m[10] = 1.0 - (2.0 * (yy + xx));
  717. result.m[11] = 0;
  718. result.m[12] = 0;
  719. result.m[13] = 0;
  720. result.m[14] = 0;
  721. result.m[15] = 1.0;
  722. }
  723. // Statics
  724. public static RotationAxis(axis: Vector3, angle: number): Quaternion {
  725. var result = new Quaternion();
  726. var sin = Math.sin(angle / 2);
  727. result.w = Math.cos(angle / 2);
  728. result.x = axis.x * sin;
  729. result.y = axis.y * sin;
  730. result.z = axis.z * sin;
  731. return result;
  732. }
  733. public static FromArray(array: number[], offset?: number): Quaternion {
  734. if (!offset) {
  735. offset = 0;
  736. }
  737. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  738. }
  739. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  740. var result = new Quaternion();
  741. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  742. return result;
  743. }
  744. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  745. var halfRoll = roll * 0.5;
  746. var halfPitch = pitch * 0.5;
  747. var halfYaw = yaw * 0.5;
  748. var sinRoll = Math.sin(halfRoll);
  749. var cosRoll = Math.cos(halfRoll);
  750. var sinPitch = Math.sin(halfPitch);
  751. var cosPitch = Math.cos(halfPitch);
  752. var sinYaw = Math.sin(halfYaw);
  753. var cosYaw = Math.cos(halfYaw);
  754. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  755. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  756. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  757. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  758. }
  759. public static Slerp(left: Quaternion, right: Quaternion, amount: number): Quaternion {
  760. var num2;
  761. var num3;
  762. var num = amount;
  763. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  764. var flag = false;
  765. if (num4 < 0) {
  766. flag = true;
  767. num4 = -num4;
  768. }
  769. if (num4 > 0.999999) {
  770. num3 = 1 - num;
  771. num2 = flag ? -num : num;
  772. }
  773. else {
  774. var num5 = Math.acos(num4);
  775. var num6 = (1.0 / Math.sin(num5));
  776. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  777. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  778. }
  779. 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));
  780. }
  781. }
  782. export class Matrix {
  783. private static _tempQuaternion: Quaternion = new Quaternion();
  784. private static _xAxis: Vector3 = Vector3.Zero();
  785. private static _yAxis: Vector3 = Vector3.Zero();
  786. private static _zAxis: Vector3 = Vector3.Zero();
  787. public m: Float32Array = new Float32Array(16);
  788. // Properties
  789. public isIdentity(): boolean {
  790. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  791. return false;
  792. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  793. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  794. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  795. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  796. return false;
  797. return true;
  798. }
  799. public determinant(): number {
  800. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  801. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  802. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  803. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  804. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  805. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  806. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  807. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  808. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  809. }
  810. // Methods
  811. public toArray(): Float32Array {
  812. return this.m;
  813. }
  814. public asArray(): Float32Array {
  815. return this.toArray();
  816. }
  817. public invert(): void {
  818. this.invertToRef(this);
  819. }
  820. public invertToRef(other: Matrix) {
  821. var l1 = this.m[0];
  822. var l2 = this.m[1];
  823. var l3 = this.m[2];
  824. var l4 = this.m[3];
  825. var l5 = this.m[4];
  826. var l6 = this.m[5];
  827. var l7 = this.m[6];
  828. var l8 = this.m[7];
  829. var l9 = this.m[8];
  830. var l10 = this.m[9];
  831. var l11 = this.m[10];
  832. var l12 = this.m[11];
  833. var l13 = this.m[12];
  834. var l14 = this.m[13];
  835. var l15 = this.m[14];
  836. var l16 = this.m[15];
  837. var l17 = (l11 * l16) - (l12 * l15);
  838. var l18 = (l10 * l16) - (l12 * l14);
  839. var l19 = (l10 * l15) - (l11 * l14);
  840. var l20 = (l9 * l16) - (l12 * l13);
  841. var l21 = (l9 * l15) - (l11 * l13);
  842. var l22 = (l9 * l14) - (l10 * l13);
  843. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  844. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  845. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  846. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  847. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  848. var l28 = (l7 * l16) - (l8 * l15);
  849. var l29 = (l6 * l16) - (l8 * l14);
  850. var l30 = (l6 * l15) - (l7 * l14);
  851. var l31 = (l5 * l16) - (l8 * l13);
  852. var l32 = (l5 * l15) - (l7 * l13);
  853. var l33 = (l5 * l14) - (l6 * l13);
  854. var l34 = (l7 * l12) - (l8 * l11);
  855. var l35 = (l6 * l12) - (l8 * l10);
  856. var l36 = (l6 * l11) - (l7 * l10);
  857. var l37 = (l5 * l12) - (l8 * l9);
  858. var l38 = (l5 * l11) - (l7 * l9);
  859. var l39 = (l5 * l10) - (l6 * l9);
  860. other.m[0] = l23 * l27;
  861. other.m[4] = l24 * l27;
  862. other.m[8] = l25 * l27;
  863. other.m[12] = l26 * l27;
  864. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  865. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  866. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  867. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  868. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  869. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  870. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  871. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  872. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  873. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  874. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  875. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  876. }
  877. public setTranslation(vector3: Vector3): void {
  878. this.m[12] = vector3.x;
  879. this.m[13] = vector3.y;
  880. this.m[14] = vector3.z;
  881. }
  882. public multiply(other: Matrix): Matrix {
  883. var result = new Matrix();
  884. this.multiplyToRef(other, result);
  885. return result;
  886. }
  887. public copyFrom(other: Matrix): void {
  888. for (var index = 0; index < 16; index++) {
  889. this.m[index] = other.m[index];
  890. }
  891. }
  892. public copyToArray(array: Float32Array, offset: number = 0): void {
  893. for (var index = 0; index < 16; index++) {
  894. array[offset + index] = this.m[index];
  895. }
  896. }
  897. public multiplyToRef(other: Matrix, result: Matrix): void {
  898. this.multiplyToArray(other, result.m, 0);
  899. }
  900. public multiplyToArray(other: Matrix, result: Float32Array, offset: number): void {
  901. var tm0 = this.m[0];
  902. var tm1 = this.m[1];
  903. var tm2 = this.m[2];
  904. var tm3 = this.m[3];
  905. var tm4 = this.m[4];
  906. var tm5 = this.m[5];
  907. var tm6 = this.m[6];
  908. var tm7 = this.m[7];
  909. var tm8 = this.m[8];
  910. var tm9 = this.m[9];
  911. var tm10 = this.m[10];
  912. var tm11 = this.m[11];
  913. var tm12 = this.m[12];
  914. var tm13 = this.m[13];
  915. var tm14 = this.m[14];
  916. var tm15 = this.m[15];
  917. var om0 = other.m[0];
  918. var om1 = other.m[1];
  919. var om2 = other.m[2];
  920. var om3 = other.m[3];
  921. var om4 = other.m[4];
  922. var om5 = other.m[5];
  923. var om6 = other.m[6];
  924. var om7 = other.m[7];
  925. var om8 = other.m[8];
  926. var om9 = other.m[9];
  927. var om10 = other.m[10];
  928. var om11 = other.m[11];
  929. var om12 = other.m[12];
  930. var om13 = other.m[13];
  931. var om14 = other.m[14];
  932. var om15 = other.m[15];
  933. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  934. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  935. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  936. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  937. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  938. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  939. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  940. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  941. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  942. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  943. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  944. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  945. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  946. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  947. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  948. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  949. }
  950. public equals(value: Matrix): boolean {
  951. return value &&
  952. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  953. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  954. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  955. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  956. }
  957. public clone(): Matrix {
  958. return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  959. this.m[4], this.m[5], this.m[6], this.m[7],
  960. this.m[8], this.m[9], this.m[10], this.m[11],
  961. this.m[12], this.m[13], this.m[14], this.m[15]);
  962. }
  963. // Statics
  964. public static FromArray(array: number[], offset?: number): Matrix {
  965. var result = new Matrix();
  966. if (!offset) {
  967. offset = 0;
  968. }
  969. Matrix.FromArrayToRef(array, offset, result);
  970. return result;
  971. }
  972. public static FromArrayToRef(array: number[], offset: number, result: Matrix) {
  973. for (var index = 0; index < 16; index++) {
  974. result.m[index] = array[index + offset];
  975. }
  976. }
  977. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  978. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  979. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  980. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  981. result.m[0] = initialM11;
  982. result.m[1] = initialM12;
  983. result.m[2] = initialM13;
  984. result.m[3] = initialM14;
  985. result.m[4] = initialM21;
  986. result.m[5] = initialM22;
  987. result.m[6] = initialM23;
  988. result.m[7] = initialM24;
  989. result.m[8] = initialM31;
  990. result.m[9] = initialM32;
  991. result.m[10] = initialM33;
  992. result.m[11] = initialM34;
  993. result.m[12] = initialM41;
  994. result.m[13] = initialM42;
  995. result.m[14] = initialM43;
  996. result.m[15] = initialM44;
  997. }
  998. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  999. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  1000. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  1001. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  1002. var result = new Matrix();
  1003. result.m[0] = initialM11;
  1004. result.m[1] = initialM12;
  1005. result.m[2] = initialM13;
  1006. result.m[3] = initialM14;
  1007. result.m[4] = initialM21;
  1008. result.m[5] = initialM22;
  1009. result.m[6] = initialM23;
  1010. result.m[7] = initialM24;
  1011. result.m[8] = initialM31;
  1012. result.m[9] = initialM32;
  1013. result.m[10] = initialM33;
  1014. result.m[11] = initialM34;
  1015. result.m[12] = initialM41;
  1016. result.m[13] = initialM42;
  1017. result.m[14] = initialM43;
  1018. result.m[15] = initialM44;
  1019. return result;
  1020. }
  1021. public static Identity(): Matrix {
  1022. return Matrix.FromValues(1.0, 0, 0, 0,
  1023. 0, 1.0, 0, 0,
  1024. 0, 0, 1.0, 0,
  1025. 0, 0, 0, 1.0);
  1026. }
  1027. public static IdentityToRef(result: Matrix): void {
  1028. Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1029. 0, 1.0, 0, 0,
  1030. 0, 0, 1.0, 0,
  1031. 0, 0, 0, 1.0, result);
  1032. }
  1033. public static Zero(): Matrix {
  1034. return Matrix.FromValues(0, 0, 0, 0,
  1035. 0, 0, 0, 0,
  1036. 0, 0, 0, 0,
  1037. 0, 0, 0, 0);
  1038. }
  1039. public static RotationX(angle: number): Matrix {
  1040. var result = new Matrix();
  1041. Matrix.RotationXToRef(angle, result);
  1042. return result;
  1043. }
  1044. public static RotationXToRef(angle: number, result: Matrix): void {
  1045. var s = Math.sin(angle);
  1046. var c = Math.cos(angle);
  1047. result.m[0] = 1.0;
  1048. result.m[15] = 1.0;
  1049. result.m[5] = c;
  1050. result.m[10] = c;
  1051. result.m[9] = -s;
  1052. result.m[6] = s;
  1053. result.m[1] = 0;
  1054. result.m[2] = 0;
  1055. result.m[3] = 0;
  1056. result.m[4] = 0;
  1057. result.m[7] = 0;
  1058. result.m[8] = 0;
  1059. result.m[11] = 0;
  1060. result.m[12] = 0;
  1061. result.m[13] = 0;
  1062. result.m[14] = 0;
  1063. }
  1064. public static RotationY(angle: number): Matrix {
  1065. var result = new Matrix();
  1066. Matrix.RotationYToRef(angle, result);
  1067. return result;
  1068. }
  1069. public static RotationYToRef(angle: number, result: Matrix): void {
  1070. var s = Math.sin(angle);
  1071. var c = Math.cos(angle);
  1072. result.m[5] = 1.0;
  1073. result.m[15] = 1.0;
  1074. result.m[0] = c;
  1075. result.m[2] = -s;
  1076. result.m[8] = s;
  1077. result.m[10] = c;
  1078. result.m[1] = 0;
  1079. result.m[3] = 0;
  1080. result.m[4] = 0;
  1081. result.m[6] = 0;
  1082. result.m[7] = 0;
  1083. result.m[9] = 0;
  1084. result.m[11] = 0;
  1085. result.m[12] = 0;
  1086. result.m[13] = 0;
  1087. result.m[14] = 0;
  1088. }
  1089. public static RotationZ(angle: number): Matrix {
  1090. var result = new Matrix();
  1091. Matrix.RotationZToRef(angle, result);
  1092. return result;
  1093. }
  1094. public static RotationZToRef(angle: number, result: Matrix): void {
  1095. var s = Math.sin(angle);
  1096. var c = Math.cos(angle);
  1097. result.m[10] = 1.0;
  1098. result.m[15] = 1.0;
  1099. result.m[0] = c;
  1100. result.m[1] = s;
  1101. result.m[4] = -s;
  1102. result.m[5] = c;
  1103. result.m[2] = 0;
  1104. result.m[3] = 0;
  1105. result.m[6] = 0;
  1106. result.m[7] = 0;
  1107. result.m[8] = 0;
  1108. result.m[9] = 0;
  1109. result.m[11] = 0;
  1110. result.m[12] = 0;
  1111. result.m[13] = 0;
  1112. result.m[14] = 0;
  1113. }
  1114. public static RotationAxis(axis: Vector3, angle: number): Matrix {
  1115. var s = Math.sin(-angle);
  1116. var c = Math.cos(-angle);
  1117. var c1 = 1 - c;
  1118. axis.normalize();
  1119. var result = Matrix.Zero();
  1120. result.m[0] = (axis.x * axis.x) * c1 + c;
  1121. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1122. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1123. result.m[3] = 0.0;
  1124. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1125. result.m[5] = (axis.y * axis.y) * c1 + c;
  1126. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1127. result.m[7] = 0.0;
  1128. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1129. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1130. result.m[10] = (axis.z * axis.z) * c1 + c;
  1131. result.m[11] = 0.0;
  1132. result.m[15] = 1.0;
  1133. return result;
  1134. }
  1135. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  1136. var result = new Matrix();
  1137. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1138. return result;
  1139. }
  1140. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  1141. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1142. this._tempQuaternion.toRotationMatrix(result);
  1143. }
  1144. public static Scaling(x: number, y: number, z: number): Matrix {
  1145. var result = Matrix.Zero();
  1146. Matrix.ScalingToRef(x, y, z, result);
  1147. return result;
  1148. }
  1149. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  1150. result.m[0] = x;
  1151. result.m[1] = 0;
  1152. result.m[2] = 0;
  1153. result.m[3] = 0;
  1154. result.m[4] = 0;
  1155. result.m[5] = y;
  1156. result.m[6] = 0;
  1157. result.m[7] = 0;
  1158. result.m[8] = 0;
  1159. result.m[9] = 0;
  1160. result.m[10] = z;
  1161. result.m[11] = 0;
  1162. result.m[12] = 0;
  1163. result.m[13] = 0;
  1164. result.m[14] = 0;
  1165. result.m[15] = 1.0;
  1166. }
  1167. public static Translation(x: number, y: number, z: number): Matrix {
  1168. var result = Matrix.Identity();
  1169. Matrix.TranslationToRef(x, y, z, result);
  1170. return result;
  1171. }
  1172. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  1173. Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1174. 0, 1.0, 0, 0,
  1175. 0, 0, 1.0, 0,
  1176. x, y, z, 1.0, result);
  1177. }
  1178. public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  1179. var result = Matrix.Zero();
  1180. Matrix.LookAtLHToRef(eye, target, up, result);
  1181. return result;
  1182. }
  1183. public static LookAtLHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  1184. // Z axis
  1185. target.subtractToRef(eye, this._zAxis);
  1186. this._zAxis.normalize();
  1187. // X axis
  1188. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1189. this._xAxis.normalize();
  1190. // Y axis
  1191. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1192. this._yAxis.normalize();
  1193. // Eye angles
  1194. var ex = -Vector3.Dot(this._xAxis, eye);
  1195. var ey = -Vector3.Dot(this._yAxis, eye);
  1196. var ez = -Vector3.Dot(this._zAxis, eye);
  1197. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
  1198. this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
  1199. this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
  1200. ex, ey, ez, 1, result);
  1201. }
  1202. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  1203. var hw = 2.0 / width;
  1204. var hh = 2.0 / height;
  1205. var id = 1.0 / (zfar - znear);
  1206. var nid = znear / (znear - zfar);
  1207. return Matrix.FromValues(hw, 0, 0, 0,
  1208. 0, hh, 0, 0,
  1209. 0, 0, id, 0,
  1210. 0, 0, nid, 1);
  1211. }
  1212. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  1213. var matrix = Matrix.Zero();
  1214. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1215. return matrix;
  1216. }
  1217. public static OrthoOffCenterLHToRef(left: number, right, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  1218. result.m[0] = 2.0 / (right - left);
  1219. result.m[1] = result.m[2] = result.m[3] = 0;
  1220. result.m[5] = 2.0 / (top - bottom);
  1221. result.m[4] = result.m[6] = result.m[7] = 0;
  1222. result.m[10] = -1.0 / (znear - zfar);
  1223. result.m[8] = result.m[9] = result.m[11] = 0;
  1224. result.m[12] = (left + right) / (left - right);
  1225. result.m[13] = (top + bottom) / (bottom - top);
  1226. result.m[14] = znear / (znear - zfar);
  1227. result.m[15] = 1.0;
  1228. }
  1229. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  1230. var matrix = Matrix.Zero();
  1231. matrix.m[0] = (2.0 * znear) / width;
  1232. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1233. matrix.m[5] = (2.0 * znear) / height;
  1234. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1235. matrix.m[10] = -zfar / (znear - zfar);
  1236. matrix.m[8] = matrix.m[9] = 0.0;
  1237. matrix.m[11] = 1.0;
  1238. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1239. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1240. return matrix;
  1241. }
  1242. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  1243. var matrix = Matrix.Zero();
  1244. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1245. return matrix;
  1246. }
  1247. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix): void {
  1248. var tan = 1.0 / (Math.tan(fov * 0.5));
  1249. result.m[0] = tan / aspect;
  1250. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1251. result.m[5] = tan;
  1252. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1253. result.m[8] = result.m[9] = 0.0;
  1254. result.m[10] = -zfar / (znear - zfar);
  1255. result.m[11] = 1.0;
  1256. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1257. result.m[14] = (znear * zfar) / (znear - zfar);
  1258. }
  1259. public static GetFinalMatrix(viewport: Viewport, world: Matrix, view: Matrix, projection: Matrix, zmin: number, zmax: number): Matrix {
  1260. var cw = viewport.width;
  1261. var ch = viewport.height;
  1262. var cx = viewport.x;
  1263. var cy = viewport.y;
  1264. var viewportMatrix = Matrix.FromValues(cw / 2.0, 0, 0, 0,
  1265. 0, -ch / 2.0, 0, 0,
  1266. 0, 0, zmax - zmin, 0,
  1267. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  1268. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1269. }
  1270. public static Transpose(matrix: Matrix): Matrix {
  1271. var result = new Matrix();
  1272. result.m[0] = matrix.m[0];
  1273. result.m[1] = matrix.m[4];
  1274. result.m[2] = matrix.m[8];
  1275. result.m[3] = matrix.m[12];
  1276. result.m[4] = matrix.m[1];
  1277. result.m[5] = matrix.m[5];
  1278. result.m[6] = matrix.m[9];
  1279. result.m[7] = matrix.m[13];
  1280. result.m[8] = matrix.m[2];
  1281. result.m[9] = matrix.m[6];
  1282. result.m[10] = matrix.m[10];
  1283. result.m[11] = matrix.m[14];
  1284. result.m[12] = matrix.m[3];
  1285. result.m[13] = matrix.m[7];
  1286. result.m[14] = matrix.m[11];
  1287. result.m[15] = matrix.m[15];
  1288. return result;
  1289. }
  1290. public static Reflection(plane: Plane): Matrix {
  1291. var matrix = new Matrix();
  1292. Matrix.ReflectionToRef(plane, matrix);
  1293. return matrix;
  1294. }
  1295. public static ReflectionToRef(plane: Plane, result: Matrix): void {
  1296. plane.normalize();
  1297. var x = plane.normal.x;
  1298. var y = plane.normal.y;
  1299. var z = plane.normal.z;
  1300. var temp = -2 * x;
  1301. var temp2 = -2 * y;
  1302. var temp3 = -2 * z;
  1303. result.m[0] = (temp * x) + 1;
  1304. result.m[1] = temp2 * x;
  1305. result.m[2] = temp3 * x;
  1306. result.m[3] = 0.0;
  1307. result.m[4] = temp * y;
  1308. result.m[5] = (temp2 * y) + 1;
  1309. result.m[6] = temp3 * y;
  1310. result.m[7] = 0.0;
  1311. result.m[8] = temp * z;
  1312. result.m[9] = temp2 * z;
  1313. result.m[10] = (temp3 * z) + 1;
  1314. result.m[11] = 0.0;
  1315. result.m[12] = temp * plane.d;
  1316. result.m[13] = temp2 * plane.d;
  1317. result.m[14] = temp3 * plane.d;
  1318. result.m[15] = 1.0;
  1319. }
  1320. }
  1321. export class Plane {
  1322. public normal: Vector3;
  1323. public d: number;
  1324. constructor(a: number, b: number, c: number, d: number) {
  1325. this.normal = new Vector3(a, b, c);
  1326. this.d = d;
  1327. }
  1328. public asArray(): number[] {
  1329. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  1330. }
  1331. // Methods
  1332. public clone(): Plane {
  1333. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  1334. }
  1335. public normalize(): void {
  1336. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1337. var magnitude = 0;
  1338. if (norm != 0) {
  1339. magnitude = 1.0 / norm;
  1340. }
  1341. this.normal.x *= magnitude;
  1342. this.normal.y *= magnitude;
  1343. this.normal.z *= magnitude;
  1344. this.d *= magnitude;
  1345. }
  1346. public transform(transformation: Matrix): Plane {
  1347. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  1348. var x = this.normal.x;
  1349. var y = this.normal.y;
  1350. var z = this.normal.z;
  1351. var d = this.d;
  1352. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1353. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1354. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1355. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1356. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  1357. }
  1358. public dotCoordinate(point): number {
  1359. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1360. }
  1361. public copyFromPoints(point1: Vector3, point2: Vector3, point3: Vector3): void {
  1362. var x1 = point2.x - point1.x;
  1363. var y1 = point2.y - point1.y;
  1364. var z1 = point2.z - point1.z;
  1365. var x2 = point3.x - point1.x;
  1366. var y2 = point3.y - point1.y;
  1367. var z2 = point3.z - point1.z;
  1368. var yz = (y1 * z2) - (z1 * y2);
  1369. var xz = (z1 * x2) - (x1 * z2);
  1370. var xy = (x1 * y2) - (y1 * x2);
  1371. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1372. var invPyth;
  1373. if (pyth != 0) {
  1374. invPyth = 1.0 / pyth;
  1375. }
  1376. else {
  1377. invPyth = 0;
  1378. }
  1379. this.normal.x = yz * invPyth;
  1380. this.normal.y = xz * invPyth;
  1381. this.normal.z = xy * invPyth;
  1382. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1383. }
  1384. public isFrontFacingTo(direction: Vector3, epsilon: number): boolean {
  1385. var dot = Vector3.Dot(this.normal, direction);
  1386. return (dot <= epsilon);
  1387. }
  1388. public signedDistanceTo(point: Vector3): number {
  1389. return Vector3.Dot(point, this.normal) + this.d;
  1390. }
  1391. // Statics
  1392. static FromArray(array: number[]): Plane {
  1393. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  1394. }
  1395. static FromPoints(point1, point2, point3): Plane {
  1396. var result = new BABYLON.Plane(0, 0, 0, 0);
  1397. result.copyFromPoints(point1, point2, point3);
  1398. return result;
  1399. }
  1400. static FromPositionAndNormal(origin: Vector3, normal: Vector3): Plane {
  1401. var result = new BABYLON.Plane(0, 0, 0, 0);
  1402. normal.normalize();
  1403. result.normal = normal;
  1404. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1405. return result;
  1406. }
  1407. static SignedDistanceToPlaneFromPositionAndNormal(origin: Vector3, normal: Vector3, point: Vector3): number {
  1408. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1409. return Vector3.Dot(point, normal) + d;
  1410. }
  1411. }
  1412. export class Viewport {
  1413. constructor(public x: number, public y: number, public width: number, public height: number) {
  1414. }
  1415. public toGlobal(engine) {
  1416. var width = engine.getRenderWidth();
  1417. var height = engine.getRenderHeight();
  1418. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  1419. }
  1420. }
  1421. export class Frustum {
  1422. public static GetPlanes(transform: Matrix): Plane[] {
  1423. var frustumPlanes = [];
  1424. for (var index = 0; index < 6; index++) {
  1425. frustumPlanes.push(new Plane(0, 0, 0, 0));
  1426. }
  1427. Frustum.GetPlanesToRef(transform, frustumPlanes);
  1428. return frustumPlanes;
  1429. }
  1430. public static GetPlanesToRef(transform: Matrix, frustumPlanes: Plane[]): void {
  1431. // Near
  1432. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1433. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1434. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1435. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1436. frustumPlanes[0].normalize();
  1437. // Far
  1438. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1439. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1440. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1441. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1442. frustumPlanes[1].normalize();
  1443. // Left
  1444. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1445. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1446. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1447. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1448. frustumPlanes[2].normalize();
  1449. // Right
  1450. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1451. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1452. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1453. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1454. frustumPlanes[3].normalize();
  1455. // Top
  1456. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1457. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1458. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1459. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1460. frustumPlanes[4].normalize();
  1461. // Bottom
  1462. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1463. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1464. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1465. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1466. frustumPlanes[5].normalize();
  1467. }
  1468. }
  1469. export class Ray {
  1470. private _edge1: Vector3;
  1471. private _edge2: Vector3;
  1472. private _pvec: Vector3;
  1473. private _tvec: Vector3;
  1474. private _qvec: Vector3;
  1475. constructor(public origin: Vector3, public direction: Vector3) {
  1476. }
  1477. // Methods
  1478. public intersectsBoxMinMax(minimum: Vector3, maximum: Vector3): boolean {
  1479. var d = 0.0;
  1480. var maxValue = Number.MAX_VALUE;
  1481. if (Math.abs(this.direction.x) < 0.0000001) {
  1482. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  1483. return false;
  1484. }
  1485. }
  1486. else {
  1487. var inv = 1.0 / this.direction.x;
  1488. var min = (minimum.x - this.origin.x) * inv;
  1489. var max = (maximum.x - this.origin.x) * inv;
  1490. if (min > max) {
  1491. var temp = min;
  1492. min = max;
  1493. max = temp;
  1494. }
  1495. d = Math.max(min, d);
  1496. maxValue = Math.min(max, maxValue);
  1497. if (d > maxValue) {
  1498. return false;
  1499. }
  1500. }
  1501. if (Math.abs(this.direction.y) < 0.0000001) {
  1502. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  1503. return false;
  1504. }
  1505. }
  1506. else {
  1507. inv = 1.0 / this.direction.y;
  1508. min = (minimum.y - this.origin.y) * inv;
  1509. max = (maximum.y - this.origin.y) * inv;
  1510. if (min > max) {
  1511. temp = min;
  1512. min = max;
  1513. max = temp;
  1514. }
  1515. d = Math.max(min, d);
  1516. maxValue = Math.min(max, maxValue);
  1517. if (d > maxValue) {
  1518. return false;
  1519. }
  1520. }
  1521. if (Math.abs(this.direction.z) < 0.0000001) {
  1522. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  1523. return false;
  1524. }
  1525. }
  1526. else {
  1527. inv = 1.0 / this.direction.z;
  1528. min = (minimum.z - this.origin.z) * inv;
  1529. max = (maximum.z - this.origin.z) * inv;
  1530. if (min > max) {
  1531. temp = min;
  1532. min = max;
  1533. max = temp;
  1534. }
  1535. d = Math.max(min, d);
  1536. maxValue = Math.min(max, maxValue);
  1537. if (d > maxValue) {
  1538. return false;
  1539. }
  1540. }
  1541. return true;
  1542. }
  1543. public intersectsBox(box: BoundingBox): boolean {
  1544. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  1545. }
  1546. public intersectsSphere(sphere): boolean {
  1547. var x = sphere.center.x - this.origin.x;
  1548. var y = sphere.center.y - this.origin.y;
  1549. var z = sphere.center.z - this.origin.z;
  1550. var pyth = (x * x) + (y * y) + (z * z);
  1551. var rr = sphere.radius * sphere.radius;
  1552. if (pyth <= rr) {
  1553. return true;
  1554. }
  1555. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  1556. if (dot < 0.0) {
  1557. return false;
  1558. }
  1559. var temp = pyth - (dot * dot);
  1560. return temp <= rr;
  1561. }
  1562. public intersectsTriangle(vertex0: Vector3, vertex1: Vector3, vertex2: Vector3): IntersectionInfo {
  1563. if (!this._edge1) {
  1564. this._edge1 = BABYLON.Vector3.Zero();
  1565. this._edge2 = BABYLON.Vector3.Zero();
  1566. this._pvec = BABYLON.Vector3.Zero();
  1567. this._tvec = BABYLON.Vector3.Zero();
  1568. this._qvec = BABYLON.Vector3.Zero();
  1569. }
  1570. vertex1.subtractToRef(vertex0, this._edge1);
  1571. vertex2.subtractToRef(vertex0, this._edge2);
  1572. BABYLON.Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  1573. var det = Vector3.Dot(this._edge1, this._pvec);
  1574. if (det === 0) {
  1575. return null;
  1576. }
  1577. var invdet = 1 / det;
  1578. this.origin.subtractToRef(vertex0, this._tvec);
  1579. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  1580. if (bu < 0 || bu > 1.0) {
  1581. return null;
  1582. }
  1583. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  1584. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  1585. if (bv < 0 || bu + bv > 1.0) {
  1586. return null;
  1587. }
  1588. return new IntersectionInfo(bu, bv, Vector3.Dot(this._edge2, this._qvec) * invdet);
  1589. }
  1590. // Statics
  1591. public static CreateNew(x: number, y: number, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Ray {
  1592. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  1593. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  1594. var direction = end.subtract(start);
  1595. direction.normalize();
  1596. return new Ray(start, direction);
  1597. }
  1598. public static Transform(ray: Ray, matrix: Matrix): Ray {
  1599. var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
  1600. var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
  1601. return new Ray(newOrigin, newDirection);
  1602. }
  1603. }
  1604. export enum Space {
  1605. LOCAL = 0,
  1606. WORLD = 1
  1607. }
  1608. export class Axis {
  1609. public static X: Vector3 = new BABYLON.Vector3(1, 0, 0);
  1610. public static Y: Vector3 = new BABYLON.Vector3(0, 1, 0);
  1611. public static Z: Vector3 =new BABYLON.Vector3(0, 0, 1);
  1612. };
  1613. }