babylon.math.ts 90 KB

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