babylon.math.ts 99 KB

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