babylon.math.ts 140 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828
  1. module BABYLON {
  2. declare var SIMD;
  3. export class Color3 {
  4. constructor(public r: number = 0, public g: number = 0, public b: number = 0) {
  5. }
  6. public toString(): string {
  7. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  8. }
  9. // Operators
  10. public toArray(array: number[], index?: number): Color3 {
  11. if (index === undefined) {
  12. index = 0;
  13. }
  14. array[index] = this.r;
  15. array[index + 1] = this.g;
  16. array[index + 2] = this.b;
  17. return this;
  18. }
  19. public toColor4(alpha = 1): Color4 {
  20. return new Color4(this.r, this.g, this.b, alpha);
  21. }
  22. public asArray(): number[] {
  23. var result = [];
  24. this.toArray(result, 0);
  25. return result;
  26. }
  27. public toLuminance(): number {
  28. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  29. }
  30. public multiply(otherColor: Color3): Color3 {
  31. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  32. }
  33. public multiplyToRef(otherColor: Color3, result: Color3): Color3 {
  34. result.r = this.r * otherColor.r;
  35. result.g = this.g * otherColor.g;
  36. result.b = this.b * otherColor.b;
  37. return this;
  38. }
  39. public equals(otherColor: Color3): boolean {
  40. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  41. }
  42. public equalsFloats(r: number, g: number, b: number): boolean {
  43. return this.r === r && this.g === g && this.b === b;
  44. }
  45. public scale(scale: number): Color3 {
  46. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  47. }
  48. public scaleToRef(scale: number, result: Color3): Color3 {
  49. result.r = this.r * scale;
  50. result.g = this.g * scale;
  51. result.b = this.b * scale;
  52. return this;
  53. }
  54. public add(otherColor: Color3): Color3 {
  55. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  56. }
  57. public addToRef(otherColor: Color3, result: Color3): Color3 {
  58. result.r = this.r + otherColor.r;
  59. result.g = this.g + otherColor.g;
  60. result.b = this.b + otherColor.b;
  61. return this;
  62. }
  63. public subtract(otherColor: Color3): Color3 {
  64. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  65. }
  66. public subtractToRef(otherColor: Color3, result: Color3): Color3 {
  67. result.r = this.r - otherColor.r;
  68. result.g = this.g - otherColor.g;
  69. result.b = this.b - otherColor.b;
  70. return this;
  71. }
  72. public clone(): Color3 {
  73. return new Color3(this.r, this.g, this.b);
  74. }
  75. public copyFrom(source: Color3): Color3 {
  76. this.r = source.r;
  77. this.g = source.g;
  78. this.b = source.b;
  79. return this;
  80. }
  81. public copyFromFloats(r: number, g: number, b: number): Color3 {
  82. this.r = r;
  83. this.g = g;
  84. this.b = b;
  85. return this;
  86. }
  87. public toHexString(): string {
  88. var intR = (this.r * 255) | 0;
  89. var intG = (this.g * 255) | 0;
  90. var intB = (this.b * 255) | 0;
  91. return "#" + Tools.ToHex(intR) + Tools.ToHex(intG) + Tools.ToHex(intB);
  92. }
  93. // Statics
  94. public static FromHexString(hex: string): Color3 {
  95. if (hex.substring(0, 1) !== "#" || hex.length !== 7) {
  96. Tools.Warn("Color3.FromHexString must be called with a string like #FFFFFF");
  97. return new Color3(0, 0, 0);
  98. }
  99. var r = parseInt(hex.substring(1, 3), 16);
  100. var g = parseInt(hex.substring(3, 5), 16);
  101. var b = parseInt(hex.substring(5, 7), 16);
  102. return Color3.FromInts(r, g, b);
  103. }
  104. public static FromArray(array: number[], offset: number = 0): Color3 {
  105. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  106. }
  107. public static FromInts(r: number, g: number, b: number): Color3 {
  108. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  109. }
  110. public static Lerp(start: Color3, end: Color3, amount: number): Color3 {
  111. var r = start.r + ((end.r - start.r) * amount);
  112. var g = start.g + ((end.g - start.g) * amount);
  113. var b = start.b + ((end.b - start.b) * amount);
  114. return new Color3(r, g, b);
  115. }
  116. public static Red(): Color3 { return new Color3(1, 0, 0); }
  117. public static Green(): Color3 { return new Color3(0, 1, 0); }
  118. public static Blue(): Color3 { return new Color3(0, 0, 1); }
  119. public static Black(): Color3 { return new Color3(0, 0, 0); }
  120. public static White(): Color3 { return new Color3(1, 1, 1); }
  121. public static Purple(): Color3 { return new Color3(0.5, 0, 0.5); }
  122. public static Magenta(): Color3 { return new Color3(1, 0, 1); }
  123. public static Yellow(): Color3 { return new Color3(1, 1, 0); }
  124. public static Gray(): Color3 { return new Color3(0.5, 0.5, 0.5); }
  125. }
  126. export class Color4 {
  127. constructor(public r: number, public g: number, public b: number, public a: number) {
  128. }
  129. // Operators
  130. public addInPlace(right): Color4 {
  131. this.r += right.r;
  132. this.g += right.g;
  133. this.b += right.b;
  134. this.a += right.a;
  135. return this;
  136. }
  137. public asArray(): number[] {
  138. var result = [];
  139. this.toArray(result, 0);
  140. return result;
  141. }
  142. public toArray(array: number[], index?: number): Color4 {
  143. if (index === undefined) {
  144. index = 0;
  145. }
  146. array[index] = this.r;
  147. array[index + 1] = this.g;
  148. array[index + 2] = this.b;
  149. array[index + 3] = this.a;
  150. return this;
  151. }
  152. public add(right: Color4): Color4 {
  153. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  154. }
  155. public subtract(right: Color4): Color4 {
  156. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  157. }
  158. public subtractToRef(right: Color4, result: Color4): Color4 {
  159. result.r = this.r - right.r;
  160. result.g = this.g - right.g;
  161. result.b = this.b - right.b;
  162. result.a = this.a - right.a;
  163. return this;
  164. }
  165. public scale(scale: number): Color4 {
  166. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  167. }
  168. public scaleToRef(scale: number, result: Color4): Color4 {
  169. result.r = this.r * scale;
  170. result.g = this.g * scale;
  171. result.b = this.b * scale;
  172. result.a = this.a * scale;
  173. return this;
  174. }
  175. public toString(): string {
  176. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  177. }
  178. public clone(): Color4 {
  179. return new Color4(this.r, this.g, this.b, this.a);
  180. }
  181. public copyFrom(source: Color4): Color4 {
  182. this.r = source.r;
  183. this.g = source.g;
  184. this.b = source.b;
  185. this.a = source.a;
  186. return this;
  187. }
  188. public toHexString(): string {
  189. var intR = (this.r * 255) | 0;
  190. var intG = (this.g * 255) | 0;
  191. var intB = (this.b * 255) | 0;
  192. var intA = (this.a * 255) | 0;
  193. return "#" + Tools.ToHex(intR) + Tools.ToHex(intG) + Tools.ToHex(intB) + Tools.ToHex(intA);
  194. }
  195. // Statics
  196. public static FromHexString(hex: string): Color4 {
  197. if (hex.substring(0, 1) !== "#" || hex.length !== 9) {
  198. Tools.Warn("Color4.FromHexString must be called with a string like #FFFFFFFF");
  199. return new Color4(0, 0, 0, 0);
  200. }
  201. var r = parseInt(hex.substring(1, 3), 16);
  202. var g = parseInt(hex.substring(3, 5), 16);
  203. var b = parseInt(hex.substring(5, 7), 16);
  204. var a = parseInt(hex.substring(7, 9), 16);
  205. return Color4.FromInts(r, g, b, a);
  206. }
  207. public static Lerp(left: Color4, right: Color4, amount: number): Color4 {
  208. var result = new Color4(0, 0, 0, 0);
  209. Color4.LerpToRef(left, right, amount, result);
  210. return result;
  211. }
  212. public static LerpToRef(left: Color4, right: Color4, amount: number, result: Color4): void {
  213. result.r = left.r + (right.r - left.r) * amount;
  214. result.g = left.g + (right.g - left.g) * amount;
  215. result.b = left.b + (right.b - left.b) * amount;
  216. result.a = left.a + (right.a - left.a) * amount;
  217. }
  218. public static FromArray(array: number[], offset: number = 0): Color4 {
  219. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  220. }
  221. public static FromInts(r: number, g: number, b: number, a: number): Color4 {
  222. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  223. }
  224. }
  225. export class Vector2 {
  226. constructor(public x: number, public y: number) {
  227. }
  228. public toString(): string {
  229. return "{X: " + this.x + " Y:" + this.y + "}";
  230. }
  231. // Operators
  232. public toArray(array: number[], index: number = 0): Vector2 {
  233. array[index] = this.x;
  234. array[index + 1] = this.y;
  235. return this;
  236. }
  237. public asArray(): number[] {
  238. var result = [];
  239. this.toArray(result, 0);
  240. return result;
  241. }
  242. public copyFrom(source: Vector2): Vector2 {
  243. this.x = source.x;
  244. this.y = source.y;
  245. return this;
  246. }
  247. public copyFromFloats(x: number, y: number): Vector2 {
  248. this.x = x;
  249. this.y = y;
  250. return this;
  251. }
  252. public add(otherVector: Vector2): Vector2 {
  253. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  254. }
  255. public addVector3(otherVector: Vector3): Vector2 {
  256. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  257. }
  258. public subtract(otherVector: Vector2): Vector2 {
  259. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  260. }
  261. public subtractInPlace(otherVector: Vector2): Vector2 {
  262. this.x -= otherVector.x;
  263. this.y -= otherVector.y;
  264. return this;
  265. }
  266. public multiplyInPlace(otherVector: Vector2): Vector2 {
  267. this.x *= otherVector.x;
  268. this.y *= otherVector.y;
  269. return this;
  270. }
  271. public multiply(otherVector: Vector2): Vector2 {
  272. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  273. }
  274. public multiplyToRef(otherVector: Vector2, result: Vector2): Vector2 {
  275. result.x = this.x * otherVector.x;
  276. result.y = this.y * otherVector.y;
  277. return this;
  278. }
  279. public multiplyByFloats(x: number, y: number): Vector2 {
  280. return new Vector2(this.x * x, this.y * y);
  281. }
  282. public divide(otherVector: Vector2): Vector2 {
  283. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  284. }
  285. public divideToRef(otherVector: Vector2, result: Vector2): Vector2 {
  286. result.x = this.x / otherVector.x;
  287. result.y = this.y / otherVector.y;
  288. return this;
  289. }
  290. public negate(): Vector2 {
  291. return new Vector2(-this.x, -this.y);
  292. }
  293. public scaleInPlace(scale: number): Vector2 {
  294. this.x *= scale;
  295. this.y *= scale;
  296. return this;
  297. }
  298. public scale(scale: number): Vector2 {
  299. return new Vector2(this.x * scale, this.y * scale);
  300. }
  301. public equals(otherVector: Vector2): boolean {
  302. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  303. }
  304. public equalsWithEpsilon(otherVector: Vector2, epsilon: number = Engine.Epsilon): boolean {
  305. return otherVector && Tools.WithinEpsilon(this.x, otherVector.x, epsilon) && Tools.WithinEpsilon(this.y, otherVector.y, epsilon);
  306. }
  307. // Properties
  308. public length(): number {
  309. return Math.sqrt(this.x * this.x + this.y * this.y);
  310. }
  311. public lengthSquared(): number {
  312. return (this.x * this.x + this.y * this.y);
  313. }
  314. // Methods
  315. public normalize(): Vector2 {
  316. var len = this.length();
  317. if (len === 0)
  318. return this;
  319. var num = 1.0 / len;
  320. this.x *= num;
  321. this.y *= num;
  322. return this;
  323. }
  324. public clone(): Vector2 {
  325. return new Vector2(this.x, this.y);
  326. }
  327. // Statics
  328. public static Zero(): Vector2 {
  329. return new Vector2(0, 0);
  330. }
  331. public static FromArray(array: number[], offset: number = 0): Vector2 {
  332. return new Vector2(array[offset], array[offset + 1]);
  333. }
  334. public static FromArrayToRef(array: number[], offset: number, result: Vector2): void {
  335. result.x = array[offset];
  336. result.y = array[offset + 1];
  337. }
  338. public static CatmullRom(value1: Vector2, value2: Vector2, value3: Vector2, value4: Vector2, amount: number): Vector2 {
  339. var squared = amount * amount;
  340. var cubed = amount * squared;
  341. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  342. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  343. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  344. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  345. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  346. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  347. return new Vector2(x, y);
  348. }
  349. public static Clamp(value: Vector2, min: Vector2, max: Vector2): Vector2 {
  350. var x = value.x;
  351. x = (x > max.x) ? max.x : x;
  352. x = (x < min.x) ? min.x : x;
  353. var y = value.y;
  354. y = (y > max.y) ? max.y : y;
  355. y = (y < min.y) ? min.y : y;
  356. return new Vector2(x, y);
  357. }
  358. public static Hermite(value1: Vector2, tangent1: Vector2, value2: Vector2, tangent2: Vector2, amount: number): Vector2 {
  359. var squared = amount * amount;
  360. var cubed = amount * squared;
  361. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  362. var part2 = (-2.0 * cubed) + (3.0 * squared);
  363. var part3 = (cubed - (2.0 * squared)) + amount;
  364. var part4 = cubed - squared;
  365. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  366. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  367. return new Vector2(x, y);
  368. }
  369. public static Lerp(start: Vector2, end: Vector2, amount: number): Vector2 {
  370. var x = start.x + ((end.x - start.x) * amount);
  371. var y = start.y + ((end.y - start.y) * amount);
  372. return new Vector2(x, y);
  373. }
  374. public static Dot(left: Vector2, right: Vector2): number {
  375. return left.x * right.x + left.y * right.y;
  376. }
  377. public static Normalize(vector: Vector2): Vector2 {
  378. var newVector = vector.clone();
  379. newVector.normalize();
  380. return newVector;
  381. }
  382. public static Minimize(left: Vector2, right: Vector2): Vector2 {
  383. var x = (left.x < right.x) ? left.x : right.x;
  384. var y = (left.y < right.y) ? left.y : right.y;
  385. return new Vector2(x, y);
  386. }
  387. public static Maximize(left: Vector2, right: Vector2): Vector2 {
  388. var x = (left.x > right.x) ? left.x : right.x;
  389. var y = (left.y > right.y) ? left.y : right.y;
  390. return new Vector2(x, y);
  391. }
  392. public static Transform(vector: Vector2, transformation: Matrix): Vector2 {
  393. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  394. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  395. return new Vector2(x, y);
  396. }
  397. public static Distance(value1: Vector2, value2: Vector2): number {
  398. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  399. }
  400. public static DistanceSquared(value1: Vector2, value2: Vector2): number {
  401. var x = value1.x - value2.x;
  402. var y = value1.y - value2.y;
  403. return (x * x) + (y * y);
  404. }
  405. }
  406. export class Vector3 {
  407. constructor(public x: number, public y: number, public z: number) {
  408. }
  409. public toString(): string {
  410. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  411. }
  412. // Operators
  413. public asArray(): number[] {
  414. var result = [];
  415. this.toArray(result, 0);
  416. return result;
  417. }
  418. public toArray(array: number[], index: number = 0): Vector3 {
  419. array[index] = this.x;
  420. array[index + 1] = this.y;
  421. array[index + 2] = this.z;
  422. return this;
  423. }
  424. public toQuaternion(): Quaternion {
  425. var result = new Quaternion(0, 0, 0, 1);
  426. var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
  427. var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
  428. var coszMinusx = Math.cos((this.z - this.x) * 0.5);
  429. var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
  430. var cosy = Math.cos(this.y * 0.5);
  431. var siny = Math.sin(this.y * 0.5);
  432. result.x = coszMinusx * siny;
  433. result.y = -sinzMinusx * siny;
  434. result.z = sinxPlusz * cosy;
  435. result.w = cosxPlusz * cosy;
  436. return result;
  437. }
  438. public addInPlace(otherVector: Vector3): Vector3 {
  439. this.x += otherVector.x;
  440. this.y += otherVector.y;
  441. this.z += otherVector.z;
  442. return this;
  443. }
  444. public add(otherVector: Vector3): Vector3 {
  445. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  446. }
  447. public addToRef(otherVector: Vector3, result: Vector3): Vector3 {
  448. result.x = this.x + otherVector.x;
  449. result.y = this.y + otherVector.y;
  450. result.z = this.z + otherVector.z;
  451. return this;
  452. }
  453. public subtractInPlace(otherVector: Vector3): Vector3 {
  454. this.x -= otherVector.x;
  455. this.y -= otherVector.y;
  456. this.z -= otherVector.z;
  457. return this;
  458. }
  459. public subtract(otherVector: Vector3): Vector3 {
  460. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  461. }
  462. public subtractToRef(otherVector: Vector3, result: Vector3): Vector3 {
  463. result.x = this.x - otherVector.x;
  464. result.y = this.y - otherVector.y;
  465. result.z = this.z - otherVector.z;
  466. return this;
  467. }
  468. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  469. return new Vector3(this.x - x, this.y - y, this.z - z);
  470. }
  471. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
  472. result.x = this.x - x;
  473. result.y = this.y - y;
  474. result.z = this.z - z;
  475. return this;
  476. }
  477. public negate(): Vector3 {
  478. return new Vector3(-this.x, -this.y, -this.z);
  479. }
  480. public scaleInPlace(scale: number): Vector3 {
  481. this.x *= scale;
  482. this.y *= scale;
  483. this.z *= scale;
  484. return this;
  485. }
  486. public scale(scale: number): Vector3 {
  487. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  488. }
  489. public scaleToRef(scale: number, result: Vector3) {
  490. result.x = this.x * scale;
  491. result.y = this.y * scale;
  492. result.z = this.z * scale;
  493. }
  494. public equals(otherVector: Vector3): boolean {
  495. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  496. }
  497. public equalsWithEpsilon(otherVector: Vector3, epsilon: number = Engine.Epsilon): boolean {
  498. return otherVector && Tools.WithinEpsilon(this.x, otherVector.x, epsilon) && Tools.WithinEpsilon(this.y, otherVector.y, epsilon) && Tools.WithinEpsilon(this.z, otherVector.z, epsilon);
  499. }
  500. public equalsToFloats(x: number, y: number, z: number): boolean {
  501. return this.x === x && this.y === y && this.z === z;
  502. }
  503. public multiplyInPlace(otherVector: Vector3): Vector3 {
  504. this.x *= otherVector.x;
  505. this.y *= otherVector.y;
  506. this.z *= otherVector.z;
  507. return this;
  508. }
  509. public multiply(otherVector: Vector3): Vector3 {
  510. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  511. }
  512. public multiplyToRef(otherVector: Vector3, result: Vector3): Vector3 {
  513. result.x = this.x * otherVector.x;
  514. result.y = this.y * otherVector.y;
  515. result.z = this.z * otherVector.z;
  516. return this;
  517. }
  518. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  519. return new Vector3(this.x * x, this.y * y, this.z * z);
  520. }
  521. public divide(otherVector: Vector3): Vector3 {
  522. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  523. }
  524. public divideToRef(otherVector: Vector3, result: Vector3): Vector3 {
  525. result.x = this.x / otherVector.x;
  526. result.y = this.y / otherVector.y;
  527. result.z = this.z / otherVector.z;
  528. return this;
  529. }
  530. public MinimizeInPlace(other: Vector3): Vector3 {
  531. if (other.x < this.x) this.x = other.x;
  532. if (other.y < this.y) this.y = other.y;
  533. if (other.z < this.z) this.z = other.z;
  534. return this;
  535. }
  536. public MaximizeInPlace(other: Vector3): Vector3 {
  537. if (other.x > this.x) this.x = other.x;
  538. if (other.y > this.y) this.y = other.y;
  539. if (other.z > this.z) this.z = other.z;
  540. return this;
  541. }
  542. // Properties
  543. public length(): number {
  544. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  545. }
  546. public lengthSquared(): number {
  547. return (this.x * this.x + this.y * this.y + this.z * this.z);
  548. }
  549. // Methods
  550. public normalize(): Vector3 {
  551. var len = this.length();
  552. if (len === 0 || len === 1.0)
  553. return this;
  554. var num = 1.0 / len;
  555. this.x *= num;
  556. this.y *= num;
  557. this.z *= num;
  558. return this;
  559. }
  560. public clone(): Vector3 {
  561. return new Vector3(this.x, this.y, this.z);
  562. }
  563. public copyFrom(source: Vector3): Vector3 {
  564. this.x = source.x;
  565. this.y = source.y;
  566. this.z = source.z;
  567. return this;
  568. }
  569. public copyFromFloats(x: number, y: number, z: number): Vector3 {
  570. this.x = x;
  571. this.y = y;
  572. this.z = z;
  573. return this;
  574. }
  575. // Statics
  576. public static GetClipFactor(vector0: Vector3, vector1: Vector3, axis: Vector3, size) {
  577. var d0 = Vector3.Dot(vector0, axis) - size;
  578. var d1 = Vector3.Dot(vector1, axis) - size;
  579. var s = d0 / (d0 - d1);
  580. return s;
  581. }
  582. public static FromArray(array: number[], offset?: number): Vector3 {
  583. if (!offset) {
  584. offset = 0;
  585. }
  586. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  587. }
  588. public static FromFloatArray(array: Float32Array, offset?: number): Vector3 {
  589. if (!offset) {
  590. offset = 0;
  591. }
  592. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  593. }
  594. public static FromArrayToRef(array: number[], offset: number, result: Vector3): void {
  595. result.x = array[offset];
  596. result.y = array[offset + 1];
  597. result.z = array[offset + 2];
  598. }
  599. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector3): void {
  600. result.x = array[offset];
  601. result.y = array[offset + 1];
  602. result.z = array[offset + 2];
  603. }
  604. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  605. result.x = x;
  606. result.y = y;
  607. result.z = z;
  608. }
  609. public static Zero(): Vector3 {
  610. return new Vector3(0, 0, 0);
  611. }
  612. public static Up(): Vector3 {
  613. return new Vector3(0, 1.0, 0);
  614. }
  615. public static TransformCoordinates(vector: Vector3, transformation: Matrix): Vector3 {
  616. var result = Vector3.Zero();
  617. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  618. return result;
  619. }
  620. public static TransformCoordinatesToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  621. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  622. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  623. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  624. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  625. result.x = x / w;
  626. result.y = y / w;
  627. result.z = z / w;
  628. }
  629. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  630. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  631. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  632. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  633. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  634. result.x = rx / rw;
  635. result.y = ry / rw;
  636. result.z = rz / rw;
  637. }
  638. public static TransformCoordinatesToRefSIMD(vector: Vector3, transformation: Matrix, result: Vector3): void {
  639. var v = SIMD.float32x4.loadXYZ((<any>vector)._data, 0);
  640. var m0 = SIMD.float32x4.load(transformation.m, 0);
  641. var m1 = SIMD.float32x4.load(transformation.m, 4);
  642. var m2 = SIMD.float32x4.load(transformation.m, 8);
  643. var m3 = SIMD.float32x4.load(transformation.m, 12);
  644. var r = SIMD.float32x4.add(SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 0, 0, 0, 0), m0), SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 1, 1, 1, 1), m1)), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 2, 2, 2, 2), m2), m3));
  645. r = SIMD.float32x4.div(r, SIMD.float32x4.swizzle(r, 3, 3, 3, 3));
  646. SIMD.float32x4.storeXYZ((<any>result)._data, 0, r);
  647. }
  648. public static TransformCoordinatesFromFloatsToRefSIMD(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  649. var v0 = SIMD.float32x4.splat(x);
  650. var v1 = SIMD.float32x4.splat(y);
  651. var v2 = SIMD.float32x4.splat(z);
  652. var m0 = SIMD.float32x4.load(transformation.m, 0);
  653. var m1 = SIMD.float32x4.load(transformation.m, 4);
  654. var m2 = SIMD.float32x4.load(transformation.m, 8);
  655. var m3 = SIMD.float32x4.load(transformation.m, 12);
  656. var r = SIMD.float32x4.add(SIMD.float32x4.add(SIMD.float32x4.mul(v0, m0), SIMD.float32x4.mul(v1, m1)), SIMD.float32x4.add(SIMD.float32x4.mul(v2, m2), m3));
  657. r = SIMD.float32x4.div(r, SIMD.float32x4.swizzle(r, 3, 3, 3, 3));
  658. SIMD.float32x4.storeXYZ((<any>result)._data, 0, r);
  659. }
  660. public static TransformNormal(vector: Vector3, transformation: Matrix): Vector3 {
  661. var result = Vector3.Zero();
  662. Vector3.TransformNormalToRef(vector, transformation, result);
  663. return result;
  664. }
  665. public static TransformNormalToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  666. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  667. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  668. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  669. }
  670. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  671. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  672. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  673. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  674. }
  675. public static CatmullRom(value1: Vector3, value2: Vector3, value3: Vector3, value4: Vector3, amount: number): Vector3 {
  676. var squared = amount * amount;
  677. var cubed = amount * squared;
  678. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  679. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  680. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  681. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  682. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  683. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  684. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  685. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  686. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  687. return new Vector3(x, y, z);
  688. }
  689. public static Clamp(value: Vector3, min: Vector3, max: Vector3): Vector3 {
  690. var x = value.x;
  691. x = (x > max.x) ? max.x : x;
  692. x = (x < min.x) ? min.x : x;
  693. var y = value.y;
  694. y = (y > max.y) ? max.y : y;
  695. y = (y < min.y) ? min.y : y;
  696. var z = value.z;
  697. z = (z > max.z) ? max.z : z;
  698. z = (z < min.z) ? min.z : z;
  699. return new Vector3(x, y, z);
  700. }
  701. public static Hermite(value1: Vector3, tangent1: Vector3, value2: Vector3, tangent2: Vector3, amount: number): Vector3 {
  702. var squared = amount * amount;
  703. var cubed = amount * squared;
  704. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  705. var part2 = (-2.0 * cubed) + (3.0 * squared);
  706. var part3 = (cubed - (2.0 * squared)) + amount;
  707. var part4 = cubed - squared;
  708. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  709. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  710. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  711. return new Vector3(x, y, z);
  712. }
  713. public static Lerp(start: Vector3, end: Vector3, amount: number): Vector3 {
  714. var x = start.x + ((end.x - start.x) * amount);
  715. var y = start.y + ((end.y - start.y) * amount);
  716. var z = start.z + ((end.z - start.z) * amount);
  717. return new Vector3(x, y, z);
  718. }
  719. public static Dot(left: Vector3, right: Vector3): number {
  720. return (left.x * right.x + left.y * right.y + left.z * right.z);
  721. }
  722. public static Cross(left: Vector3, right: Vector3): Vector3 {
  723. var result = Vector3.Zero();
  724. Vector3.CrossToRef(left, right, result);
  725. return result;
  726. }
  727. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  728. result.x = left.y * right.z - left.z * right.y;
  729. result.y = left.z * right.x - left.x * right.z;
  730. result.z = left.x * right.y - left.y * right.x;
  731. }
  732. public static Normalize(vector: Vector3): Vector3 {
  733. var result = Vector3.Zero();
  734. Vector3.NormalizeToRef(vector, result);
  735. return result;
  736. }
  737. public static NormalizeToRef(vector: Vector3, result: Vector3): void {
  738. result.copyFrom(vector);
  739. result.normalize();
  740. }
  741. public static Project(vector: Vector3, world: Matrix, transform: Matrix, viewport: Viewport): Vector3 {
  742. var cw = viewport.width;
  743. var ch = viewport.height;
  744. var cx = viewport.x;
  745. var cy = viewport.y;
  746. var viewportMatrix = Matrix.FromValues(
  747. cw / 2.0, 0, 0, 0,
  748. 0, -ch / 2.0, 0, 0,
  749. 0, 0, 1, 0,
  750. cx + cw / 2.0, ch / 2.0 + cy, 0, 1);
  751. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  752. return Vector3.TransformCoordinates(vector, finalMatrix);
  753. }
  754. public static UnprojectFromTransform(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, transform: Matrix): Vector3 {
  755. var matrix = world.multiply(transform);
  756. matrix.invert();
  757. source.x = source.x / viewportWidth * 2 - 1;
  758. source.y = -(source.y / viewportHeight * 2 - 1);
  759. var vector = Vector3.TransformCoordinates(source, matrix);
  760. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  761. if (Tools.WithinEpsilon(num, 1.0)) {
  762. vector = vector.scale(1.0 / num);
  763. }
  764. return vector;
  765. }
  766. public static Unproject(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Vector3 {
  767. var matrix = world.multiply(view).multiply(projection);
  768. matrix.invert();
  769. var screenSource = new Vector3(source.x / viewportWidth * 2 - 1, -(source.y / viewportHeight * 2 - 1), source.z);
  770. var vector = Vector3.TransformCoordinates(screenSource, matrix);
  771. var num = screenSource.x * matrix.m[3] + screenSource.y * matrix.m[7] + screenSource.z * matrix.m[11] + matrix.m[15];
  772. if (Tools.WithinEpsilon(num, 1.0)) {
  773. vector = vector.scale(1.0 / num);
  774. }
  775. return vector;
  776. }
  777. public static Minimize(left: Vector3, right: Vector3): Vector3 {
  778. var min = left.clone();
  779. min.MinimizeInPlace(right);
  780. return min;
  781. }
  782. public static Maximize(left: Vector3, right: Vector3): Vector3 {
  783. var max = left.clone();
  784. max.MaximizeInPlace(right);
  785. return max;
  786. }
  787. public static Distance(value1: Vector3, value2: Vector3): number {
  788. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  789. }
  790. public static DistanceSquared(value1: Vector3, value2: Vector3): number {
  791. var x = value1.x - value2.x;
  792. var y = value1.y - value2.y;
  793. var z = value1.z - value2.z;
  794. return (x * x) + (y * y) + (z * z);
  795. }
  796. public static Center(value1: Vector3, value2: Vector3): Vector3 {
  797. var center = value1.add(value2);
  798. center.scaleInPlace(0.5);
  799. return center;
  800. }
  801. /**
  802. * Given three orthogonal left-handed oriented Vector3 axis in space (target system),
  803. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  804. * to something in order to rotate it from its local system to the given target system.
  805. */
  806. public static RotationFromAxis(axis1: Vector3, axis2: Vector3, axis3: Vector3): Vector3 {
  807. var u = Vector3.Normalize(axis1);
  808. var w = Vector3.Normalize(axis3);
  809. // world axis
  810. var X = Axis.X;
  811. var Y = Axis.Y;
  812. // equation unknowns and vars
  813. var yaw = 0.0;
  814. var pitch = 0.0;
  815. var roll = 0.0;
  816. var x = 0.0;
  817. var y = 0.0;
  818. var z = 0.0;
  819. var t = 0.0;
  820. var sign = -1.0;
  821. var nbRevert = 0;
  822. var cross: Vector3;
  823. var dot = 0.0;
  824. // step 1 : rotation around w
  825. // Rv3(u) = u1, and u1 belongs to plane xOz
  826. // Rv3(w) = w1 = w invariant
  827. var u1: Vector3;
  828. var v1: Vector3;
  829. if (Tools.WithinEpsilon(w.z, 0, Engine.Epsilon)) {
  830. z = 1.0;
  831. }
  832. else if (Tools.WithinEpsilon(w.x, 0, Engine.Epsilon)) {
  833. x = 1.0;
  834. }
  835. else {
  836. t = w.z / w.x;
  837. x = - t * Math.sqrt(1 / (1 + t * t));
  838. z = Math.sqrt(1 / (1 + t * t));
  839. }
  840. u1 = new Vector3(x, y, z);
  841. u1.normalize();
  842. v1 = Vector3.Cross(w, u1); // v1 image of v through rotation around w
  843. v1.normalize();
  844. cross = Vector3.Cross(u, u1); // returns same direction as w (=local z) if positive angle : cross(source, image)
  845. cross.normalize();
  846. if (Vector3.Dot(w, cross) < 0) {
  847. sign = 1.0;
  848. }
  849. dot = Vector3.Dot(u, u1);
  850. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  851. roll = Math.acos(dot) * sign;
  852. if (Vector3.Dot(u1, X) < 0) { // checks X orientation
  853. roll = Math.PI + roll;
  854. u1 = u1.scaleInPlace(-1);
  855. v1 = v1.scaleInPlace(-1);
  856. nbRevert++;
  857. }
  858. // step 2 : rotate around u1
  859. // Ru1(w1) = Ru1(w) = w2, and w2 belongs to plane xOz
  860. // u1 is yet in xOz and invariant by Ru1, so after this step u1 and w2 will be in xOz
  861. var w2: Vector3;
  862. var v2: Vector3;
  863. x = 0.0;
  864. y = 0.0;
  865. z = 0.0;
  866. sign = -1;
  867. if (Tools.WithinEpsilon(w.z, 0, Engine.Epsilon)) {
  868. x = 1.0;
  869. }
  870. else {
  871. t = u1.z / u1.x;
  872. x = - t * Math.sqrt(1 / (1 + t * t));
  873. z = Math.sqrt(1 / (1 + t * t));
  874. }
  875. w2 = new Vector3(x, y, z);
  876. w2.normalize();
  877. v2 = Vector3.Cross(w2, u1); // v2 image of v1 through rotation around u1
  878. v2.normalize();
  879. cross = Vector3.Cross(w, w2); // returns same direction as u1 (=local x) if positive angle : cross(source, image)
  880. cross.normalize();
  881. if (Vector3.Dot(u1, cross) < 0) {
  882. sign = 1.0;
  883. }
  884. dot = Vector3.Dot(w, w2);
  885. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  886. pitch = Math.acos(dot) * sign;
  887. if (Vector3.Dot(v2, Y) < 0) { // checks for Y orientation
  888. pitch = Math.PI + pitch;
  889. v2 = v2.scaleInPlace(-1);
  890. w2 = w2.scaleInPlace(-1);
  891. nbRevert++;
  892. }
  893. // step 3 : rotate around v2
  894. // Rv2(u1) = X, same as Rv2(w2) = Z, with X=(1,0,0) and Z=(0,0,1)
  895. sign = -1;
  896. cross = Vector3.Cross(X, u1); // returns same direction as Y if positive angle : cross(source, image)
  897. cross.normalize();
  898. if (Vector3.Dot(cross, Y) < 0) {
  899. sign = 1.0;
  900. }
  901. dot = Vector3.Dot(u1, X);
  902. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  903. yaw = - Math.acos(dot) * sign; // negative : plane zOx oriented clockwise
  904. if (dot < 0 && nbRevert < 2) {
  905. yaw = Math.PI + yaw;
  906. }
  907. return new Vector3(pitch, yaw, roll);
  908. }
  909. }
  910. //Vector4 class created for EulerAngle class conversion to Quaternion
  911. export class Vector4 {
  912. constructor(public x: number, public y: number, public z: number, public w: number) { }
  913. public toString(): string {
  914. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "W:" + this.w + "}";
  915. }
  916. // Operators
  917. public asArray(): number[] {
  918. var result = [];
  919. this.toArray(result, 0);
  920. return result;
  921. }
  922. public toArray(array: number[], index?: number): Vector4 {
  923. if (index === undefined) {
  924. index = 0;
  925. }
  926. array[index] = this.x;
  927. array[index + 1] = this.y;
  928. array[index + 2] = this.z;
  929. array[index + 3] = this.w;
  930. return this;
  931. }
  932. public addInPlace(otherVector: Vector4): Vector4 {
  933. this.x += otherVector.x;
  934. this.y += otherVector.y;
  935. this.z += otherVector.z;
  936. this.w += otherVector.w;
  937. return this;
  938. }
  939. public add(otherVector: Vector4): Vector4 {
  940. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  941. }
  942. public addToRef(otherVector: Vector4, result: Vector4): Vector4 {
  943. result.x = this.x + otherVector.x;
  944. result.y = this.y + otherVector.y;
  945. result.z = this.z + otherVector.z;
  946. result.w = this.w + otherVector.w;
  947. return this;
  948. }
  949. public subtractInPlace(otherVector: Vector4): Vector4 {
  950. this.x -= otherVector.x;
  951. this.y -= otherVector.y;
  952. this.z -= otherVector.z;
  953. this.w -= otherVector.w;
  954. return this;
  955. }
  956. public subtract(otherVector: Vector4): Vector4 {
  957. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  958. }
  959. public subtractToRef(otherVector: Vector4, result: Vector4): Vector4 {
  960. result.x = this.x - otherVector.x;
  961. result.y = this.y - otherVector.y;
  962. result.z = this.z - otherVector.z;
  963. result.w = this.w - otherVector.w;
  964. return this;
  965. }
  966. public subtractFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  967. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  968. }
  969. public subtractFromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): Vector4 {
  970. result.x = this.x - x;
  971. result.y = this.y - y;
  972. result.z = this.z - z;
  973. result.w = this.w - w;
  974. return this;
  975. }
  976. public negate(): Vector4 {
  977. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  978. }
  979. public scaleInPlace(scale: number): Vector4 {
  980. this.x *= scale;
  981. this.y *= scale;
  982. this.z *= scale;
  983. this.w *= scale;
  984. return this;
  985. }
  986. public scale(scale: number): Vector4 {
  987. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  988. }
  989. public scaleToRef(scale: number, result: Vector4) {
  990. result.x = this.x * scale;
  991. result.y = this.y * scale;
  992. result.z = this.z * scale;
  993. result.w = this.w * scale;
  994. }
  995. public equals(otherVector: Vector4): boolean {
  996. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  997. }
  998. public equalsWithEpsilon(otherVector: Vector4, epsilon: number = Engine.Epsilon): boolean {
  999. return otherVector
  1000. && Tools.WithinEpsilon(this.x, otherVector.x, epsilon)
  1001. && Tools.WithinEpsilon(this.y, otherVector.y, epsilon)
  1002. && Tools.WithinEpsilon(this.z, otherVector.z, epsilon)
  1003. && Tools.WithinEpsilon(this.w, otherVector.w, epsilon);
  1004. }
  1005. public equalsToFloats(x: number, y: number, z: number, w: number): boolean {
  1006. return this.x === x && this.y === y && this.z === z && this.w === w;
  1007. }
  1008. public multiplyInPlace(otherVector: Vector4): Vector4 {
  1009. this.x *= otherVector.x;
  1010. this.y *= otherVector.y;
  1011. this.z *= otherVector.z;
  1012. this.w *= otherVector.w;
  1013. return this;
  1014. }
  1015. public multiply(otherVector: Vector4): Vector4 {
  1016. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  1017. }
  1018. public multiplyToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1019. result.x = this.x * otherVector.x;
  1020. result.y = this.y * otherVector.y;
  1021. result.z = this.z * otherVector.z;
  1022. result.w = this.w * otherVector.w;
  1023. return this;
  1024. }
  1025. public multiplyByFloats(x: number, y: number, z: number, w: number): Vector4 {
  1026. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  1027. }
  1028. public divide(otherVector: Vector4): Vector4 {
  1029. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  1030. }
  1031. public divideToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1032. result.x = this.x / otherVector.x;
  1033. result.y = this.y / otherVector.y;
  1034. result.z = this.z / otherVector.z;
  1035. result.w = this.w / otherVector.w;
  1036. return this;
  1037. }
  1038. public MinimizeInPlace(other: Vector4): Vector4 {
  1039. if (other.x < this.x) this.x = other.x;
  1040. if (other.y < this.y) this.y = other.y;
  1041. if (other.z < this.z) this.z = other.z;
  1042. if (other.w < this.w) this.w = other.w;
  1043. return this;
  1044. }
  1045. public MaximizeInPlace(other: Vector4): Vector4 {
  1046. if (other.x > this.x) this.x = other.x;
  1047. if (other.y > this.y) this.y = other.y;
  1048. if (other.z > this.z) this.z = other.z;
  1049. if (other.w > this.w) this.w = other.w;
  1050. return this;
  1051. }
  1052. // Properties
  1053. public length(): number {
  1054. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1055. }
  1056. public lengthSquared(): number {
  1057. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1058. }
  1059. // Methods
  1060. public normalize(): Vector4 {
  1061. var len = this.length();
  1062. if (len === 0)
  1063. return this;
  1064. var num = 1.0 / len;
  1065. this.x *= num;
  1066. this.y *= num;
  1067. this.z *= num;
  1068. this.w *= num;
  1069. return this;
  1070. }
  1071. public clone(): Vector4 {
  1072. return new Vector4(this.x, this.y, this.z, this.w);
  1073. }
  1074. public copyFrom(source: Vector4): Vector4 {
  1075. this.x = source.x;
  1076. this.y = source.y;
  1077. this.z = source.z;
  1078. this.w = source.w;
  1079. return this;
  1080. }
  1081. public copyFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  1082. this.x = x;
  1083. this.y = y;
  1084. this.z = z;
  1085. this.w = w;
  1086. return this;
  1087. }
  1088. // Statics
  1089. public static FromArray(array: number[], offset?: number): Vector4 {
  1090. if (!offset) {
  1091. offset = 0;
  1092. }
  1093. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1094. }
  1095. public static FromArrayToRef(array: number[], offset: number, result: Vector4): void {
  1096. result.x = array[offset];
  1097. result.y = array[offset + 1];
  1098. result.z = array[offset + 2];
  1099. result.w = array[offset + 3];
  1100. }
  1101. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector4): void {
  1102. result.x = array[offset];
  1103. result.y = array[offset + 1];
  1104. result.z = array[offset + 2];
  1105. result.w = array[offset + 3];
  1106. }
  1107. public static FromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): void {
  1108. result.x = x;
  1109. result.y = y;
  1110. result.z = z;
  1111. result.w = w;
  1112. }
  1113. public static Zero(): Vector4 {
  1114. return new Vector4(0, 0, 0, 0);
  1115. }
  1116. public static Normalize(vector: Vector4): Vector4 {
  1117. var result = Vector4.Zero();
  1118. Vector4.NormalizeToRef(vector, result);
  1119. return result;
  1120. }
  1121. public static NormalizeToRef(vector: Vector4, result: Vector4): void {
  1122. result.copyFrom(vector);
  1123. result.normalize();
  1124. }
  1125. public static Minimize(left: Vector4, right: Vector4): Vector4 {
  1126. var min = left.clone();
  1127. min.MinimizeInPlace(right);
  1128. return min;
  1129. }
  1130. public static Maximize(left: Vector4, right: Vector4): Vector4 {
  1131. var max = left.clone();
  1132. max.MaximizeInPlace(right);
  1133. return max;
  1134. }
  1135. public static Distance(value1: Vector4, value2: Vector4): number {
  1136. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  1137. }
  1138. public static DistanceSquared(value1: Vector4, value2: Vector4): number {
  1139. var x = value1.x - value2.x;
  1140. var y = value1.y - value2.y;
  1141. var z = value1.z - value2.z;
  1142. var w = value1.w - value2.w;
  1143. return (x * x) + (y * y) + (z * z) + (w * w);
  1144. }
  1145. public static Center(value1: Vector4, value2: Vector4): Vector4 {
  1146. var center = value1.add(value2);
  1147. center.scaleInPlace(0.5);
  1148. return center;
  1149. }
  1150. }
  1151. export class Quaternion {
  1152. constructor(public x: number = 0, public y: number = 0, public z: number = 0, public w: number = 1) {
  1153. }
  1154. public toString(): string {
  1155. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1156. }
  1157. public asArray(): number[] {
  1158. return [this.x, this.y, this.z, this.w];
  1159. }
  1160. public equals(otherQuaternion: Quaternion): boolean {
  1161. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  1162. }
  1163. public clone(): Quaternion {
  1164. return new Quaternion(this.x, this.y, this.z, this.w);
  1165. }
  1166. public copyFrom(other: Quaternion): Quaternion {
  1167. this.x = other.x;
  1168. this.y = other.y;
  1169. this.z = other.z;
  1170. this.w = other.w;
  1171. return this;
  1172. }
  1173. public copyFromFloats(x: number, y: number, z: number, w: number): Quaternion {
  1174. this.x = x;
  1175. this.y = y;
  1176. this.z = z;
  1177. this.w = w;
  1178. return this;
  1179. }
  1180. public add(other: Quaternion): Quaternion {
  1181. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  1182. }
  1183. public subtract(other: Quaternion): Quaternion {
  1184. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  1185. }
  1186. public scale(value: number): Quaternion {
  1187. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  1188. }
  1189. public multiply(q1: Quaternion): Quaternion {
  1190. var result = new Quaternion(0, 0, 0, 1.0);
  1191. this.multiplyToRef(q1, result);
  1192. return result;
  1193. }
  1194. public multiplyToRef(q1: Quaternion, result: Quaternion): Quaternion {
  1195. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  1196. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  1197. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  1198. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  1199. result.copyFromFloats(x, y, z, w);
  1200. return this;
  1201. }
  1202. public length(): number {
  1203. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  1204. }
  1205. public normalize(): Quaternion {
  1206. var length = 1.0 / this.length();
  1207. this.x *= length;
  1208. this.y *= length;
  1209. this.z *= length;
  1210. this.w *= length;
  1211. return this;
  1212. }
  1213. public toEulerAngles(): Vector3 {
  1214. var result = Vector3.Zero();
  1215. this.toEulerAnglesToRef(result);
  1216. return result;
  1217. }
  1218. public toEulerAnglesToRef(result: Vector3): Quaternion {
  1219. //result is an EulerAngles in the in the z-x-z convention
  1220. var qx = this.x;
  1221. var qy = this.y;
  1222. var qz = this.z;
  1223. var qw = this.w;
  1224. var qxy = qx * qy;
  1225. var qxz = qx * qz;
  1226. var qwy = qw * qy;
  1227. var qwz = qw * qz;
  1228. var qwx = qw * qx;
  1229. var qyz = qy * qz;
  1230. var sqx = qx * qx;
  1231. var sqy = qy * qy;
  1232. var determinant = sqx + sqy;
  1233. if (determinant !== 0.000 && determinant !== 1.000) {
  1234. result.x = Math.atan2(qxz + qwy, qwx - qyz);
  1235. result.y = Math.acos(1 - 2 * determinant);
  1236. result.z = Math.atan2(qxz - qwy, qwx + qyz);
  1237. } else {
  1238. if (determinant === 0.0) {
  1239. result.x = 0.0;
  1240. result.y = 0.0;
  1241. 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)
  1242. } else //determinant == 1.000
  1243. {
  1244. 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)
  1245. result.y = Math.PI;
  1246. result.z = 0.0;
  1247. }
  1248. }
  1249. return this;
  1250. }
  1251. public toRotationMatrix(result: Matrix): Quaternion {
  1252. var xx = this.x * this.x;
  1253. var yy = this.y * this.y;
  1254. var zz = this.z * this.z;
  1255. var xy = this.x * this.y;
  1256. var zw = this.z * this.w;
  1257. var zx = this.z * this.x;
  1258. var yw = this.y * this.w;
  1259. var yz = this.y * this.z;
  1260. var xw = this.x * this.w;
  1261. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1262. result.m[1] = 2.0 * (xy + zw);
  1263. result.m[2] = 2.0 * (zx - yw);
  1264. result.m[3] = 0;
  1265. result.m[4] = 2.0 * (xy - zw);
  1266. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1267. result.m[6] = 2.0 * (yz + xw);
  1268. result.m[7] = 0;
  1269. result.m[8] = 2.0 * (zx + yw);
  1270. result.m[9] = 2.0 * (yz - xw);
  1271. result.m[10] = 1.0 - (2.0 * (yy + xx));
  1272. result.m[11] = 0;
  1273. result.m[12] = 0;
  1274. result.m[13] = 0;
  1275. result.m[14] = 0;
  1276. result.m[15] = 1.0;
  1277. return this;
  1278. }
  1279. public fromRotationMatrix(matrix: Matrix): Quaternion {
  1280. Quaternion.FromRotationMatrixToRef(matrix, this);
  1281. return this;
  1282. }
  1283. // Statics
  1284. public static FromRotationMatrix(matrix: Matrix): Quaternion {
  1285. var result = new Quaternion();
  1286. Quaternion.FromRotationMatrixToRef(matrix, result);
  1287. return result;
  1288. }
  1289. public static FromRotationMatrixToRef(matrix: Matrix, result: Quaternion): void {
  1290. var data = matrix.m;
  1291. var m11 = data[0], m12 = data[4], m13 = data[8];
  1292. var m21 = data[1], m22 = data[5], m23 = data[9];
  1293. var m31 = data[2], m32 = data[6], m33 = data[10];
  1294. var trace = m11 + m22 + m33;
  1295. var s;
  1296. if (trace > 0) {
  1297. s = 0.5 / Math.sqrt(trace + 1.0);
  1298. result.w = 0.25 / s;
  1299. result.x = (m32 - m23) * s;
  1300. result.y = (m13 - m31) * s;
  1301. result.z = (m21 - m12) * s;
  1302. } else if (m11 > m22 && m11 > m33) {
  1303. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1304. result.w = (m32 - m23) / s;
  1305. result.x = 0.25 * s;
  1306. result.y = (m12 + m21) / s;
  1307. result.z = (m13 + m31) / s;
  1308. } else if (m22 > m33) {
  1309. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1310. result.w = (m13 - m31) / s;
  1311. result.x = (m12 + m21) / s;
  1312. result.y = 0.25 * s;
  1313. result.z = (m23 + m32) / s;
  1314. } else {
  1315. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1316. result.w = (m21 - m12) / s;
  1317. result.x = (m13 + m31) / s;
  1318. result.y = (m23 + m32) / s;
  1319. result.z = 0.25 * s;
  1320. }
  1321. }
  1322. public static Inverse(q: Quaternion): Quaternion {
  1323. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1324. }
  1325. public static Identity(): Quaternion {
  1326. return new Quaternion(0, 0, 0, 1);
  1327. }
  1328. public static RotationAxis(axis: Vector3, angle: number): Quaternion {
  1329. var result = new Quaternion();
  1330. var sin = Math.sin(angle / 2);
  1331. result.w = Math.cos(angle / 2);
  1332. result.x = axis.x * sin;
  1333. result.y = axis.y * sin;
  1334. result.z = axis.z * sin;
  1335. return result;
  1336. }
  1337. public static FromArray(array: number[], offset?: number): Quaternion {
  1338. if (!offset) {
  1339. offset = 0;
  1340. }
  1341. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1342. }
  1343. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  1344. var result = new Quaternion();
  1345. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1346. return result;
  1347. }
  1348. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  1349. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  1350. var halfRoll = roll * 0.5;
  1351. var halfPitch = pitch * 0.5;
  1352. var halfYaw = yaw * 0.5;
  1353. var sinRoll = Math.sin(halfRoll);
  1354. var cosRoll = Math.cos(halfRoll);
  1355. var sinPitch = Math.sin(halfPitch);
  1356. var cosPitch = Math.cos(halfPitch);
  1357. var sinYaw = Math.sin(halfYaw);
  1358. var cosYaw = Math.cos(halfYaw);
  1359. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1360. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1361. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1362. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1363. }
  1364. public static RotationAlphaBetaGamma(alpha: number, beta: number, gamma: number): Quaternion {
  1365. var result = new Quaternion();
  1366. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  1367. return result;
  1368. }
  1369. public static RotationAlphaBetaGammaToRef(alpha: number, beta: number, gamma: number, result: Quaternion): void {
  1370. // Produces a quaternion from Euler angles in the z-x-z orientation
  1371. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  1372. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  1373. var halfBeta = beta * 0.5;
  1374. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1375. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1376. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1377. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1378. }
  1379. public static Slerp(left: Quaternion, right: Quaternion, amount: number): Quaternion {
  1380. var num2;
  1381. var num3;
  1382. var num = amount;
  1383. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1384. var flag = false;
  1385. if (num4 < 0) {
  1386. flag = true;
  1387. num4 = -num4;
  1388. }
  1389. if (num4 > 0.999999) {
  1390. num3 = 1 - num;
  1391. num2 = flag ? -num : num;
  1392. }
  1393. else {
  1394. var num5 = Math.acos(num4);
  1395. var num6 = (1.0 / Math.sin(num5));
  1396. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1397. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1398. }
  1399. 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));
  1400. }
  1401. }
  1402. export class Matrix {
  1403. private static _tempQuaternion: Quaternion = new Quaternion();
  1404. private static _xAxis: Vector3 = Vector3.Zero();
  1405. private static _yAxis: Vector3 = Vector3.Zero();
  1406. private static _zAxis: Vector3 = Vector3.Zero();
  1407. public m: Float32Array = new Float32Array(16);
  1408. // Properties
  1409. public isIdentity(): boolean {
  1410. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  1411. return false;
  1412. if (this.m[1] !== 0.0 || this.m[2] !== 0.0 || this.m[3] !== 0.0 ||
  1413. this.m[4] !== 0.0 || this.m[6] !== 0.0 || this.m[7] !== 0.0 ||
  1414. this.m[8] !== 0.0 || this.m[9] !== 0.0 || this.m[11] !== 0.0 ||
  1415. this.m[12] !== 0.0 || this.m[13] !== 0.0 || this.m[14] !== 0.0)
  1416. return false;
  1417. return true;
  1418. }
  1419. public determinant(): number {
  1420. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1421. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1422. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1423. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1424. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1425. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1426. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  1427. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  1428. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  1429. }
  1430. // Methods
  1431. public toArray(): Float32Array {
  1432. return this.m;
  1433. }
  1434. public asArray(): Float32Array {
  1435. return this.toArray();
  1436. }
  1437. public invert(): Matrix {
  1438. this.invertToRef(this);
  1439. return this;
  1440. }
  1441. public reset(): Matrix {
  1442. for (var index = 0; index < 16; index++) {
  1443. this.m[index] = 0;
  1444. }
  1445. return this;
  1446. }
  1447. public add(other: Matrix): Matrix {
  1448. var result = new Matrix();
  1449. this.addToRef(other, result);
  1450. return result;
  1451. }
  1452. public addToRef(other: Matrix, result: Matrix): Matrix {
  1453. for (var index = 0; index < 16; index++) {
  1454. result.m[index] = this.m[index] + other.m[index];
  1455. }
  1456. return this;
  1457. }
  1458. public addToSelf(other: Matrix): Matrix {
  1459. for (var index = 0; index < 16; index++) {
  1460. this.m[index] += other.m[index];
  1461. }
  1462. return this;
  1463. }
  1464. public invertToRef(other: Matrix): Matrix {
  1465. var l1 = this.m[0];
  1466. var l2 = this.m[1];
  1467. var l3 = this.m[2];
  1468. var l4 = this.m[3];
  1469. var l5 = this.m[4];
  1470. var l6 = this.m[5];
  1471. var l7 = this.m[6];
  1472. var l8 = this.m[7];
  1473. var l9 = this.m[8];
  1474. var l10 = this.m[9];
  1475. var l11 = this.m[10];
  1476. var l12 = this.m[11];
  1477. var l13 = this.m[12];
  1478. var l14 = this.m[13];
  1479. var l15 = this.m[14];
  1480. var l16 = this.m[15];
  1481. var l17 = (l11 * l16) - (l12 * l15);
  1482. var l18 = (l10 * l16) - (l12 * l14);
  1483. var l19 = (l10 * l15) - (l11 * l14);
  1484. var l20 = (l9 * l16) - (l12 * l13);
  1485. var l21 = (l9 * l15) - (l11 * l13);
  1486. var l22 = (l9 * l14) - (l10 * l13);
  1487. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1488. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1489. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1490. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1491. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1492. var l28 = (l7 * l16) - (l8 * l15);
  1493. var l29 = (l6 * l16) - (l8 * l14);
  1494. var l30 = (l6 * l15) - (l7 * l14);
  1495. var l31 = (l5 * l16) - (l8 * l13);
  1496. var l32 = (l5 * l15) - (l7 * l13);
  1497. var l33 = (l5 * l14) - (l6 * l13);
  1498. var l34 = (l7 * l12) - (l8 * l11);
  1499. var l35 = (l6 * l12) - (l8 * l10);
  1500. var l36 = (l6 * l11) - (l7 * l10);
  1501. var l37 = (l5 * l12) - (l8 * l9);
  1502. var l38 = (l5 * l11) - (l7 * l9);
  1503. var l39 = (l5 * l10) - (l6 * l9);
  1504. other.m[0] = l23 * l27;
  1505. other.m[4] = l24 * l27;
  1506. other.m[8] = l25 * l27;
  1507. other.m[12] = l26 * l27;
  1508. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1509. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1510. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1511. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1512. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1513. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1514. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1515. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1516. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1517. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1518. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1519. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1520. return this;
  1521. }
  1522. public invertToRefSIMD(other: Matrix): Matrix {
  1523. var src = this.m;
  1524. var dest = other.m;
  1525. var row0, row1, row2, row3;
  1526. var tmp1;
  1527. var minor0, minor1, minor2, minor3;
  1528. var det;
  1529. // Load the 4 rows
  1530. var src0 = SIMD.float32x4.load(src, 0);
  1531. var src1 = SIMD.float32x4.load(src, 4);
  1532. var src2 = SIMD.float32x4.load(src, 8);
  1533. var src3 = SIMD.float32x4.load(src, 12);
  1534. // Transpose the source matrix. Sort of. Not a true transpose operation
  1535. tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
  1536. row1 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
  1537. row0 = SIMD.float32x4.shuffle(tmp1, row1, 0, 2, 4, 6);
  1538. row1 = SIMD.float32x4.shuffle(row1, tmp1, 1, 3, 5, 7);
  1539. tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
  1540. row3 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
  1541. row2 = SIMD.float32x4.shuffle(tmp1, row3, 0, 2, 4, 6);
  1542. row3 = SIMD.float32x4.shuffle(row3, tmp1, 1, 3, 5, 7);
  1543. // This is a true transposition, but it will lead to an incorrect result
  1544. //tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
  1545. //tmp2 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
  1546. //row0 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
  1547. //row1 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
  1548. //tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
  1549. //tmp2 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
  1550. //row2 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
  1551. //row3 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
  1552. // ----
  1553. tmp1 = SIMD.float32x4.mul(row2, row3);
  1554. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1555. minor0 = SIMD.float32x4.mul(row1, tmp1);
  1556. minor1 = SIMD.float32x4.mul(row0, tmp1);
  1557. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1558. minor0 = SIMD.float32x4.sub(SIMD.float32x4.mul(row1, tmp1), minor0);
  1559. minor1 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor1);
  1560. minor1 = SIMD.float32x4.swizzle(minor1, 2, 3, 0, 1); // 0x4E = 01001110
  1561. // ----
  1562. tmp1 = SIMD.float32x4.mul(row1, row2);
  1563. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1564. minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor0);
  1565. minor3 = SIMD.float32x4.mul(row0, tmp1);
  1566. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1567. minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row3, tmp1));
  1568. minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor3);
  1569. minor3 = SIMD.float32x4.swizzle(minor3, 2, 3, 0, 1); // 0x4E = 01001110
  1570. // ----
  1571. tmp1 = SIMD.float32x4.mul(SIMD.float32x4.swizzle(row1, 2, 3, 0, 1), row3); // 0x4E = 01001110
  1572. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1573. row2 = SIMD.float32x4.swizzle(row2, 2, 3, 0, 1); // 0x4E = 01001110
  1574. minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor0);
  1575. minor2 = SIMD.float32x4.mul(row0, tmp1);
  1576. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1577. minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row2, tmp1));
  1578. minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor2);
  1579. minor2 = SIMD.float32x4.swizzle(minor2, 2, 3, 0, 1); // 0x4E = 01001110
  1580. // ----
  1581. tmp1 = SIMD.float32x4.mul(row0, row1);
  1582. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1583. minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor2);
  1584. minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row2, tmp1), minor3);
  1585. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1586. minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row3, tmp1), minor2);
  1587. minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row2, tmp1));
  1588. // ----
  1589. tmp1 = SIMD.float32x4.mul(row0, row3);
  1590. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1591. minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row2, tmp1));
  1592. minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor2);
  1593. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1594. minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor1);
  1595. minor2 = SIMD.float32x4.sub(minor2, SIMD.float32x4.mul(row1, tmp1));
  1596. // ----
  1597. tmp1 = SIMD.float32x4.mul(row0, row2);
  1598. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1599. minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor1);
  1600. minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row1, tmp1));
  1601. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1602. minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row3, tmp1));
  1603. minor3 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor3);
  1604. // Compute determinant
  1605. det = SIMD.float32x4.mul(row0, minor0);
  1606. det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 2, 3, 0, 1), det); // 0x4E = 01001110
  1607. det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 1, 0, 3, 2), det); // 0xB1 = 10110001
  1608. tmp1 = SIMD.float32x4.reciprocalApproximation(det);
  1609. det = SIMD.float32x4.sub(SIMD.float32x4.add(tmp1, tmp1), SIMD.float32x4.mul(det, SIMD.float32x4.mul(tmp1, tmp1)));
  1610. det = SIMD.float32x4.swizzle(det, 0, 0, 0, 0);
  1611. // These shuffles aren't necessary if the faulty transposition is done
  1612. // up at the top of this function.
  1613. //minor0 = SIMD.float32x4.swizzle(minor0, 2, 1, 0, 3);
  1614. //minor1 = SIMD.float32x4.swizzle(minor1, 2, 1, 0, 3);
  1615. //minor2 = SIMD.float32x4.swizzle(minor2, 2, 1, 0, 3);
  1616. //minor3 = SIMD.float32x4.swizzle(minor3, 2, 1, 0, 3);
  1617. // Compute final values by multiplying with 1/det
  1618. minor0 = SIMD.float32x4.mul(det, minor0);
  1619. minor1 = SIMD.float32x4.mul(det, minor1);
  1620. minor2 = SIMD.float32x4.mul(det, minor2);
  1621. minor3 = SIMD.float32x4.mul(det, minor3);
  1622. SIMD.float32x4.store(dest, 0, minor0);
  1623. SIMD.float32x4.store(dest, 4, minor1);
  1624. SIMD.float32x4.store(dest, 8, minor2);
  1625. SIMD.float32x4.store(dest, 12, minor3);
  1626. return this;
  1627. }
  1628. public setTranslation(vector3: Vector3): Matrix {
  1629. this.m[12] = vector3.x;
  1630. this.m[13] = vector3.y;
  1631. this.m[14] = vector3.z;
  1632. return this;
  1633. }
  1634. public multiply(other: Matrix): Matrix {
  1635. var result = new Matrix();
  1636. this.multiplyToRef(other, result);
  1637. return result;
  1638. }
  1639. public copyFrom(other: Matrix): Matrix {
  1640. for (var index = 0; index < 16; index++) {
  1641. this.m[index] = other.m[index];
  1642. }
  1643. return this;
  1644. }
  1645. public copyToArray(array: Float32Array, offset: number = 0): Matrix {
  1646. for (var index = 0; index < 16; index++) {
  1647. array[offset + index] = this.m[index];
  1648. }
  1649. return this;
  1650. }
  1651. public multiplyToRef(other: Matrix, result: Matrix): Matrix {
  1652. this.multiplyToArray(other, result.m, 0);
  1653. return this;
  1654. }
  1655. public multiplyToArray(other: Matrix, result: Float32Array, offset: number): Matrix {
  1656. var tm0 = this.m[0];
  1657. var tm1 = this.m[1];
  1658. var tm2 = this.m[2];
  1659. var tm3 = this.m[3];
  1660. var tm4 = this.m[4];
  1661. var tm5 = this.m[5];
  1662. var tm6 = this.m[6];
  1663. var tm7 = this.m[7];
  1664. var tm8 = this.m[8];
  1665. var tm9 = this.m[9];
  1666. var tm10 = this.m[10];
  1667. var tm11 = this.m[11];
  1668. var tm12 = this.m[12];
  1669. var tm13 = this.m[13];
  1670. var tm14 = this.m[14];
  1671. var tm15 = this.m[15];
  1672. var om0 = other.m[0];
  1673. var om1 = other.m[1];
  1674. var om2 = other.m[2];
  1675. var om3 = other.m[3];
  1676. var om4 = other.m[4];
  1677. var om5 = other.m[5];
  1678. var om6 = other.m[6];
  1679. var om7 = other.m[7];
  1680. var om8 = other.m[8];
  1681. var om9 = other.m[9];
  1682. var om10 = other.m[10];
  1683. var om11 = other.m[11];
  1684. var om12 = other.m[12];
  1685. var om13 = other.m[13];
  1686. var om14 = other.m[14];
  1687. var om15 = other.m[15];
  1688. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1689. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1690. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1691. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1692. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1693. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1694. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1695. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1696. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1697. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1698. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1699. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1700. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1701. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1702. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1703. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1704. return this;
  1705. }
  1706. public multiplyToArraySIMD(other: Matrix, result: Matrix, offset = 0): void {
  1707. var tm = this.m;
  1708. var om = other.m;
  1709. var om0 = SIMD.float32x4.load(om, 0);
  1710. var om1 = SIMD.float32x4.load(om, 4);
  1711. var om2 = SIMD.float32x4.load(om, 8);
  1712. var om3 = SIMD.float32x4.load(om, 12);
  1713. var tm0 = SIMD.float32x4.load(tm, 0);
  1714. SIMD.float32x4.store(result, offset + 0, SIMD.float32x4.add(
  1715. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 0, 0, 0, 0), om0),
  1716. SIMD.float32x4.add(
  1717. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 1, 1, 1, 1), om1),
  1718. SIMD.float32x4.add(
  1719. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 2, 2, 2, 2), om2),
  1720. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 3, 3, 3, 3), om3)))));
  1721. var tm1 = SIMD.float32x4.load(tm, 4);
  1722. SIMD.float32x4.store(result, offset + 4, SIMD.float32x4.add(
  1723. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 0, 0, 0, 0), om0),
  1724. SIMD.float32x4.add(
  1725. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 1, 1, 1, 1), om1),
  1726. SIMD.float32x4.add(
  1727. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 2, 2, 2, 2), om2),
  1728. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 3, 3, 3, 3), om3)))));
  1729. var tm2 = SIMD.float32x4.load(tm, 8);
  1730. SIMD.float32x4.store(result, offset + 8, SIMD.float32x4.add(
  1731. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 0, 0, 0, 0), om0),
  1732. SIMD.float32x4.add(
  1733. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 1, 1, 1, 1), om1),
  1734. SIMD.float32x4.add(
  1735. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 2, 2, 2, 2), om2),
  1736. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 3, 3, 3, 3), om3)))));
  1737. var tm3 = SIMD.float32x4.load(tm, 12);
  1738. SIMD.float32x4.store(result, offset + 12, SIMD.float32x4.add(
  1739. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 0, 0, 0, 0), om0),
  1740. SIMD.float32x4.add(
  1741. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 1, 1, 1, 1), om1),
  1742. SIMD.float32x4.add(
  1743. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 2, 2, 2, 2), om2),
  1744. SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 3, 3, 3, 3), om3)))));
  1745. }
  1746. public equals(value: Matrix): boolean {
  1747. return value &&
  1748. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  1749. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  1750. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  1751. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  1752. }
  1753. public clone(): Matrix {
  1754. return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  1755. this.m[4], this.m[5], this.m[6], this.m[7],
  1756. this.m[8], this.m[9], this.m[10], this.m[11],
  1757. this.m[12], this.m[13], this.m[14], this.m[15]);
  1758. }
  1759. public decompose(scale: Vector3, rotation: Quaternion, translation: Vector3): boolean {
  1760. translation.x = this.m[12];
  1761. translation.y = this.m[13];
  1762. translation.z = this.m[14];
  1763. var xs = Tools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  1764. var ys = Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  1765. var zs = Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  1766. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  1767. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  1768. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  1769. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  1770. rotation.x = 0;
  1771. rotation.y = 0;
  1772. rotation.z = 0;
  1773. rotation.w = 1;
  1774. return false;
  1775. }
  1776. var rotationMatrix = Matrix.FromValues(
  1777. this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0,
  1778. this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0,
  1779. this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0,
  1780. 0, 0, 0, 1);
  1781. Quaternion.FromRotationMatrixToRef(rotationMatrix, rotation);
  1782. return true;
  1783. }
  1784. // Statics
  1785. public static FromArray(array: number[], offset?: number): Matrix {
  1786. var result = new Matrix();
  1787. if (!offset) {
  1788. offset = 0;
  1789. }
  1790. Matrix.FromArrayToRef(array, offset, result);
  1791. return result;
  1792. }
  1793. public static FromArrayToRef(array: number[], offset: number, result: Matrix) {
  1794. for (var index = 0; index < 16; index++) {
  1795. result.m[index] = array[index + offset];
  1796. }
  1797. }
  1798. public static FromFloat32ArrayToRefScaled(array: Float32Array, offset: number, scale: number, result: Matrix) {
  1799. for (var index = 0; index < 16; index++) {
  1800. result.m[index] = array[index + offset] * scale;
  1801. }
  1802. }
  1803. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  1804. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  1805. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  1806. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  1807. result.m[0] = initialM11;
  1808. result.m[1] = initialM12;
  1809. result.m[2] = initialM13;
  1810. result.m[3] = initialM14;
  1811. result.m[4] = initialM21;
  1812. result.m[5] = initialM22;
  1813. result.m[6] = initialM23;
  1814. result.m[7] = initialM24;
  1815. result.m[8] = initialM31;
  1816. result.m[9] = initialM32;
  1817. result.m[10] = initialM33;
  1818. result.m[11] = initialM34;
  1819. result.m[12] = initialM41;
  1820. result.m[13] = initialM42;
  1821. result.m[14] = initialM43;
  1822. result.m[15] = initialM44;
  1823. }
  1824. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  1825. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  1826. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  1827. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  1828. var result = new Matrix();
  1829. result.m[0] = initialM11;
  1830. result.m[1] = initialM12;
  1831. result.m[2] = initialM13;
  1832. result.m[3] = initialM14;
  1833. result.m[4] = initialM21;
  1834. result.m[5] = initialM22;
  1835. result.m[6] = initialM23;
  1836. result.m[7] = initialM24;
  1837. result.m[8] = initialM31;
  1838. result.m[9] = initialM32;
  1839. result.m[10] = initialM33;
  1840. result.m[11] = initialM34;
  1841. result.m[12] = initialM41;
  1842. result.m[13] = initialM42;
  1843. result.m[14] = initialM43;
  1844. result.m[15] = initialM44;
  1845. return result;
  1846. }
  1847. public static Compose(scale: Vector3, rotation: Quaternion, translation: Vector3): Matrix {
  1848. var result = Matrix.FromValues(scale.x, 0, 0, 0,
  1849. 0, scale.y, 0, 0,
  1850. 0, 0, scale.z, 0,
  1851. 0, 0, 0, 1);
  1852. var rotationMatrix = Matrix.Identity();
  1853. rotation.toRotationMatrix(rotationMatrix);
  1854. result = result.multiply(rotationMatrix);
  1855. result.setTranslation(translation);
  1856. return result;
  1857. }
  1858. public static Identity(): Matrix {
  1859. return Matrix.FromValues(1.0, 0, 0, 0,
  1860. 0, 1.0, 0, 0,
  1861. 0, 0, 1.0, 0,
  1862. 0, 0, 0, 1.0);
  1863. }
  1864. public static IdentityToRef(result: Matrix): void {
  1865. Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1866. 0, 1.0, 0, 0,
  1867. 0, 0, 1.0, 0,
  1868. 0, 0, 0, 1.0, result);
  1869. }
  1870. public static Zero(): Matrix {
  1871. return Matrix.FromValues(0, 0, 0, 0,
  1872. 0, 0, 0, 0,
  1873. 0, 0, 0, 0,
  1874. 0, 0, 0, 0);
  1875. }
  1876. public static RotationX(angle: number): Matrix {
  1877. var result = new Matrix();
  1878. Matrix.RotationXToRef(angle, result);
  1879. return result;
  1880. }
  1881. public static Invert(source: Matrix): Matrix {
  1882. var result = new Matrix();
  1883. source.invertToRef(result);
  1884. return result;
  1885. }
  1886. public static RotationXToRef(angle: number, result: Matrix): void {
  1887. var s = Math.sin(angle);
  1888. var c = Math.cos(angle);
  1889. result.m[0] = 1.0;
  1890. result.m[15] = 1.0;
  1891. result.m[5] = c;
  1892. result.m[10] = c;
  1893. result.m[9] = -s;
  1894. result.m[6] = s;
  1895. result.m[1] = 0;
  1896. result.m[2] = 0;
  1897. result.m[3] = 0;
  1898. result.m[4] = 0;
  1899. result.m[7] = 0;
  1900. result.m[8] = 0;
  1901. result.m[11] = 0;
  1902. result.m[12] = 0;
  1903. result.m[13] = 0;
  1904. result.m[14] = 0;
  1905. }
  1906. public static RotationY(angle: number): Matrix {
  1907. var result = new Matrix();
  1908. Matrix.RotationYToRef(angle, result);
  1909. return result;
  1910. }
  1911. public static RotationYToRef(angle: number, result: Matrix): void {
  1912. var s = Math.sin(angle);
  1913. var c = Math.cos(angle);
  1914. result.m[5] = 1.0;
  1915. result.m[15] = 1.0;
  1916. result.m[0] = c;
  1917. result.m[2] = -s;
  1918. result.m[8] = s;
  1919. result.m[10] = c;
  1920. result.m[1] = 0;
  1921. result.m[3] = 0;
  1922. result.m[4] = 0;
  1923. result.m[6] = 0;
  1924. result.m[7] = 0;
  1925. result.m[9] = 0;
  1926. result.m[11] = 0;
  1927. result.m[12] = 0;
  1928. result.m[13] = 0;
  1929. result.m[14] = 0;
  1930. }
  1931. public static RotationZ(angle: number): Matrix {
  1932. var result = new Matrix();
  1933. Matrix.RotationZToRef(angle, result);
  1934. return result;
  1935. }
  1936. public static RotationZToRef(angle: number, result: Matrix): void {
  1937. var s = Math.sin(angle);
  1938. var c = Math.cos(angle);
  1939. result.m[10] = 1.0;
  1940. result.m[15] = 1.0;
  1941. result.m[0] = c;
  1942. result.m[1] = s;
  1943. result.m[4] = -s;
  1944. result.m[5] = c;
  1945. result.m[2] = 0;
  1946. result.m[3] = 0;
  1947. result.m[6] = 0;
  1948. result.m[7] = 0;
  1949. result.m[8] = 0;
  1950. result.m[9] = 0;
  1951. result.m[11] = 0;
  1952. result.m[12] = 0;
  1953. result.m[13] = 0;
  1954. result.m[14] = 0;
  1955. }
  1956. public static RotationAxis(axis: Vector3, angle: number): Matrix {
  1957. var s = Math.sin(-angle);
  1958. var c = Math.cos(-angle);
  1959. var c1 = 1 - c;
  1960. axis.normalize();
  1961. var result = Matrix.Zero();
  1962. result.m[0] = (axis.x * axis.x) * c1 + c;
  1963. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1964. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1965. result.m[3] = 0.0;
  1966. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1967. result.m[5] = (axis.y * axis.y) * c1 + c;
  1968. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1969. result.m[7] = 0.0;
  1970. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1971. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1972. result.m[10] = (axis.z * axis.z) * c1 + c;
  1973. result.m[11] = 0.0;
  1974. result.m[15] = 1.0;
  1975. return result;
  1976. }
  1977. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  1978. var result = new Matrix();
  1979. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1980. return result;
  1981. }
  1982. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  1983. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1984. this._tempQuaternion.toRotationMatrix(result);
  1985. }
  1986. public static Scaling(x: number, y: number, z: number): Matrix {
  1987. var result = Matrix.Zero();
  1988. Matrix.ScalingToRef(x, y, z, result);
  1989. return result;
  1990. }
  1991. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  1992. result.m[0] = x;
  1993. result.m[1] = 0;
  1994. result.m[2] = 0;
  1995. result.m[3] = 0;
  1996. result.m[4] = 0;
  1997. result.m[5] = y;
  1998. result.m[6] = 0;
  1999. result.m[7] = 0;
  2000. result.m[8] = 0;
  2001. result.m[9] = 0;
  2002. result.m[10] = z;
  2003. result.m[11] = 0;
  2004. result.m[12] = 0;
  2005. result.m[13] = 0;
  2006. result.m[14] = 0;
  2007. result.m[15] = 1.0;
  2008. }
  2009. public static Translation(x: number, y: number, z: number): Matrix {
  2010. var result = Matrix.Identity();
  2011. Matrix.TranslationToRef(x, y, z, result);
  2012. return result;
  2013. }
  2014. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  2015. Matrix.FromValuesToRef(1.0, 0, 0, 0,
  2016. 0, 1.0, 0, 0,
  2017. 0, 0, 1.0, 0,
  2018. x, y, z, 1.0, result);
  2019. }
  2020. public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  2021. var result = Matrix.Zero();
  2022. Matrix.LookAtLHToRef(eye, target, up, result);
  2023. return result;
  2024. }
  2025. public static LookAtLHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  2026. // Z axis
  2027. target.subtractToRef(eye, this._zAxis);
  2028. this._zAxis.normalize();
  2029. // X axis
  2030. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  2031. this._xAxis.normalize();
  2032. // Y axis
  2033. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  2034. this._yAxis.normalize();
  2035. // Eye angles
  2036. var ex = -Vector3.Dot(this._xAxis, eye);
  2037. var ey = -Vector3.Dot(this._yAxis, eye);
  2038. var ez = -Vector3.Dot(this._zAxis, eye);
  2039. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
  2040. this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
  2041. this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
  2042. ex, ey, ez, 1, result);
  2043. }
  2044. public static LookAtLHToRefSIMD(eyeRef: Vector3, targetRef: Vector3, upRef: Vector3, result: Matrix): void {
  2045. var out = result.m;
  2046. var center = SIMD.float32x4(targetRef.x, targetRef.y, targetRef.z, 0);
  2047. var eye = SIMD.float32x4(eyeRef.x, eyeRef.y, eyeRef.z, 0);
  2048. var up = SIMD.float32x4(upRef.x, upRef.y, upRef.z, 0);
  2049. // cc.kmVec3Subtract(f, pCenter, pEye);
  2050. var f = SIMD.float32x4.sub(center, eye);
  2051. // cc.kmVec3Normalize(f, f);
  2052. var tmp = SIMD.float32x4.mul(f, f);
  2053. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  2054. f = SIMD.float32x4.mul(f, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  2055. // cc.kmVec3Assign(up, pUp);
  2056. // cc.kmVec3Normalize(up, up);
  2057. tmp = SIMD.float32x4.mul(up, up);
  2058. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  2059. up = SIMD.float32x4.mul(up, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  2060. // cc.kmVec3Cross(s, f, up);
  2061. var s = SIMD.float32x4.sub(SIMD.float32x4.mul(SIMD.float32x4.swizzle(f, 1, 2, 0, 3), SIMD.float32x4.swizzle(up, 2, 0, 1, 3)), SIMD.float32x4.mul(SIMD.float32x4.swizzle(f, 2, 0, 1, 3), SIMD.float32x4.swizzle(up, 1, 2, 0, 3)));
  2062. // cc.kmVec3Normalize(s, s);
  2063. tmp = SIMD.float32x4.mul(s, s);
  2064. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  2065. s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  2066. // cc.kmVec3Cross(u, s, f);
  2067. var u = SIMD.float32x4.sub(SIMD.float32x4.mul(SIMD.float32x4.swizzle(s, 1, 2, 0, 3), SIMD.float32x4.swizzle(f, 2, 0, 1, 3)), SIMD.float32x4.mul(SIMD.float32x4.swizzle(s, 2, 0, 1, 3), SIMD.float32x4.swizzle(f, 1, 2, 0, 3)));
  2068. // cc.kmVec3Normalize(s, s);
  2069. tmp = SIMD.float32x4.mul(s, s);
  2070. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  2071. s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrtApproximation(tmp));
  2072. var zero = SIMD.float32x4.splat(0.0);
  2073. s = SIMD.float32x4.neg(s);
  2074. var tmp01 = SIMD.float32x4.shuffle(s, u, 0, 1, 4, 5);
  2075. var tmp23 = SIMD.float32x4.shuffle(f, zero, 0, 1, 4, 5);
  2076. var a0 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
  2077. var a1 = SIMD.float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7);
  2078. tmp01 = SIMD.float32x4.shuffle(s, u, 2, 3, 6, 7);
  2079. tmp23 = SIMD.float32x4.shuffle(f, zero, 2, 3, 6, 7);
  2080. var a2 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
  2081. var a3 = SIMD.float32x4(0.0, 0.0, 0.0, 1.0);
  2082. var b0 = SIMD.float32x4(1.0, 0.0, 0.0, 0.0);
  2083. var b1 = SIMD.float32x4(0.0, 1.0, 0.0, 0.0);
  2084. var b2 = SIMD.float32x4(0.0, 0.0, 1.0, 0.0);
  2085. var b3 = SIMD.float32x4.neg(eye);
  2086. b3 = SIMD.float32x4.withW(b3, 1.0);
  2087. SIMD.float32x4.store(out, 0, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 3, 3, 3, 3), a3)))));
  2088. SIMD.float32x4.store(out, 4, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 3, 3, 3, 3), a3)))));
  2089. SIMD.float32x4.store(out, 8, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 3, 3, 3, 3), a3)))));
  2090. SIMD.float32x4.store(out, 12, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 3, 3, 3, 3), a3)))));
  2091. }
  2092. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  2093. var matrix = Matrix.Zero();
  2094. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  2095. return matrix;
  2096. }
  2097. public static OrthoLHToRef(width: number, height: number, znear: number, zfar: number, result: Matrix): void {
  2098. var hw = 2.0 / width;
  2099. var hh = 2.0 / height;
  2100. var id = 1.0 / (zfar - znear);
  2101. var nid = znear / (znear - zfar);
  2102. Matrix.FromValuesToRef(hw, 0, 0, 0,
  2103. 0, hh, 0, 0,
  2104. 0, 0, id, 0,
  2105. 0, 0, nid, 1, result);
  2106. }
  2107. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  2108. var matrix = Matrix.Zero();
  2109. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  2110. return matrix;
  2111. }
  2112. public static OrthoOffCenterLHToRef(left: number, right, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  2113. result.m[0] = 2.0 / (right - left);
  2114. result.m[1] = result.m[2] = result.m[3] = 0;
  2115. result.m[5] = 2.0 / (top - bottom);
  2116. result.m[4] = result.m[6] = result.m[7] = 0;
  2117. result.m[10] = -1.0 / (znear - zfar);
  2118. result.m[8] = result.m[9] = result.m[11] = 0;
  2119. result.m[12] = (left + right) / (left - right);
  2120. result.m[13] = (top + bottom) / (bottom - top);
  2121. result.m[14] = znear / (znear - zfar);
  2122. result.m[15] = 1.0;
  2123. }
  2124. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  2125. var matrix = Matrix.Zero();
  2126. matrix.m[0] = (2.0 * znear) / width;
  2127. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  2128. matrix.m[5] = (2.0 * znear) / height;
  2129. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  2130. matrix.m[10] = -zfar / (znear - zfar);
  2131. matrix.m[8] = matrix.m[9] = 0.0;
  2132. matrix.m[11] = 1.0;
  2133. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  2134. matrix.m[14] = (znear * zfar) / (znear - zfar);
  2135. return matrix;
  2136. }
  2137. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  2138. var matrix = Matrix.Zero();
  2139. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  2140. return matrix;
  2141. }
  2142. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, fovMode = Camera.FOVMODE_VERTICAL_FIXED): void {
  2143. var tan = 1.0 / (Math.tan(fov * 0.5));
  2144. var v_fixed = (fovMode === Camera.FOVMODE_VERTICAL_FIXED);
  2145. if (v_fixed) {
  2146. result.m[0] = tan / aspect;
  2147. }
  2148. else {
  2149. result.m[0] = tan;
  2150. }
  2151. result.m[1] = result.m[2] = result.m[3] = 0.0;
  2152. if (v_fixed) {
  2153. result.m[5] = tan;
  2154. }
  2155. else {
  2156. result.m[5] = tan * aspect;
  2157. }
  2158. result.m[4] = result.m[6] = result.m[7] = 0.0;
  2159. result.m[8] = result.m[9] = 0.0;
  2160. result.m[10] = -zfar / (znear - zfar);
  2161. result.m[11] = 1.0;
  2162. result.m[12] = result.m[13] = result.m[15] = 0.0;
  2163. result.m[14] = (znear * zfar) / (znear - zfar);
  2164. }
  2165. public static GetFinalMatrix(viewport: Viewport, world: Matrix, view: Matrix, projection: Matrix, zmin: number, zmax: number): Matrix {
  2166. var cw = viewport.width;
  2167. var ch = viewport.height;
  2168. var cx = viewport.x;
  2169. var cy = viewport.y;
  2170. var viewportMatrix = Matrix.FromValues(cw / 2.0, 0, 0, 0,
  2171. 0, -ch / 2.0, 0, 0,
  2172. 0, 0, zmax - zmin, 0,
  2173. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  2174. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  2175. }
  2176. public static GetAsMatrix2x2(matrix: Matrix): Float32Array {
  2177. return new Float32Array([
  2178. matrix.m[0], matrix.m[1],
  2179. matrix.m[4], matrix.m[5]
  2180. ]);
  2181. }
  2182. public static GetAsMatrix3x3(matrix: Matrix): Float32Array {
  2183. return new Float32Array([
  2184. matrix.m[0], matrix.m[1], matrix.m[2],
  2185. matrix.m[4], matrix.m[5], matrix.m[6],
  2186. matrix.m[8], matrix.m[9], matrix.m[10]
  2187. ]);
  2188. }
  2189. public static Transpose(matrix: Matrix): Matrix {
  2190. var result = new Matrix();
  2191. result.m[0] = matrix.m[0];
  2192. result.m[1] = matrix.m[4];
  2193. result.m[2] = matrix.m[8];
  2194. result.m[3] = matrix.m[12];
  2195. result.m[4] = matrix.m[1];
  2196. result.m[5] = matrix.m[5];
  2197. result.m[6] = matrix.m[9];
  2198. result.m[7] = matrix.m[13];
  2199. result.m[8] = matrix.m[2];
  2200. result.m[9] = matrix.m[6];
  2201. result.m[10] = matrix.m[10];
  2202. result.m[11] = matrix.m[14];
  2203. result.m[12] = matrix.m[3];
  2204. result.m[13] = matrix.m[7];
  2205. result.m[14] = matrix.m[11];
  2206. result.m[15] = matrix.m[15];
  2207. return result;
  2208. }
  2209. public static Reflection(plane: Plane): Matrix {
  2210. var matrix = new Matrix();
  2211. Matrix.ReflectionToRef(plane, matrix);
  2212. return matrix;
  2213. }
  2214. public static ReflectionToRef(plane: Plane, result: Matrix): void {
  2215. plane.normalize();
  2216. var x = plane.normal.x;
  2217. var y = plane.normal.y;
  2218. var z = plane.normal.z;
  2219. var temp = -2 * x;
  2220. var temp2 = -2 * y;
  2221. var temp3 = -2 * z;
  2222. result.m[0] = (temp * x) + 1;
  2223. result.m[1] = temp2 * x;
  2224. result.m[2] = temp3 * x;
  2225. result.m[3] = 0.0;
  2226. result.m[4] = temp * y;
  2227. result.m[5] = (temp2 * y) + 1;
  2228. result.m[6] = temp3 * y;
  2229. result.m[7] = 0.0;
  2230. result.m[8] = temp * z;
  2231. result.m[9] = temp2 * z;
  2232. result.m[10] = (temp3 * z) + 1;
  2233. result.m[11] = 0.0;
  2234. result.m[12] = temp * plane.d;
  2235. result.m[13] = temp2 * plane.d;
  2236. result.m[14] = temp3 * plane.d;
  2237. result.m[15] = 1.0;
  2238. }
  2239. }
  2240. export class Plane {
  2241. public normal: Vector3;
  2242. public d: number;
  2243. constructor(a: number, b: number, c: number, d: number) {
  2244. this.normal = new Vector3(a, b, c);
  2245. this.d = d;
  2246. }
  2247. public asArray(): number[] {
  2248. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  2249. }
  2250. // Methods
  2251. public clone(): Plane {
  2252. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  2253. }
  2254. public normalize(): Plane {
  2255. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  2256. var magnitude = 0;
  2257. if (norm !== 0) {
  2258. magnitude = 1.0 / norm;
  2259. }
  2260. this.normal.x *= magnitude;
  2261. this.normal.y *= magnitude;
  2262. this.normal.z *= magnitude;
  2263. this.d *= magnitude;
  2264. return this;
  2265. }
  2266. public transform(transformation: Matrix): Plane {
  2267. var transposedMatrix = Matrix.Transpose(transformation);
  2268. var x = this.normal.x;
  2269. var y = this.normal.y;
  2270. var z = this.normal.z;
  2271. var d = this.d;
  2272. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  2273. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  2274. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  2275. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  2276. return new Plane(normalX, normalY, normalZ, finalD);
  2277. }
  2278. public dotCoordinate(point): number {
  2279. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  2280. }
  2281. public copyFromPoints(point1: Vector3, point2: Vector3, point3: Vector3): Plane {
  2282. var x1 = point2.x - point1.x;
  2283. var y1 = point2.y - point1.y;
  2284. var z1 = point2.z - point1.z;
  2285. var x2 = point3.x - point1.x;
  2286. var y2 = point3.y - point1.y;
  2287. var z2 = point3.z - point1.z;
  2288. var yz = (y1 * z2) - (z1 * y2);
  2289. var xz = (z1 * x2) - (x1 * z2);
  2290. var xy = (x1 * y2) - (y1 * x2);
  2291. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  2292. var invPyth;
  2293. if (pyth !== 0) {
  2294. invPyth = 1.0 / pyth;
  2295. }
  2296. else {
  2297. invPyth = 0;
  2298. }
  2299. this.normal.x = yz * invPyth;
  2300. this.normal.y = xz * invPyth;
  2301. this.normal.z = xy * invPyth;
  2302. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  2303. return this;
  2304. }
  2305. public isFrontFacingTo(direction: Vector3, epsilon: number): boolean {
  2306. var dot = Vector3.Dot(this.normal, direction);
  2307. return (dot <= epsilon);
  2308. }
  2309. public signedDistanceTo(point: Vector3): number {
  2310. return Vector3.Dot(point, this.normal) + this.d;
  2311. }
  2312. // Statics
  2313. static FromArray(array: number[]): Plane {
  2314. return new Plane(array[0], array[1], array[2], array[3]);
  2315. }
  2316. static FromPoints(point1, point2, point3): Plane {
  2317. var result = new Plane(0, 0, 0, 0);
  2318. result.copyFromPoints(point1, point2, point3);
  2319. return result;
  2320. }
  2321. static FromPositionAndNormal(origin: Vector3, normal: Vector3): Plane {
  2322. var result = new Plane(0, 0, 0, 0);
  2323. normal.normalize();
  2324. result.normal = normal;
  2325. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2326. return result;
  2327. }
  2328. static SignedDistanceToPlaneFromPositionAndNormal(origin: Vector3, normal: Vector3, point: Vector3): number {
  2329. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2330. return Vector3.Dot(point, normal) + d;
  2331. }
  2332. }
  2333. export class Viewport {
  2334. constructor(public x: number, public y: number, public width: number, public height: number) {
  2335. }
  2336. public toGlobal(engine): Viewport {
  2337. var width = engine.getRenderWidth();
  2338. var height = engine.getRenderHeight();
  2339. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  2340. }
  2341. }
  2342. export class Frustum {
  2343. public static GetPlanes(transform: Matrix): Plane[] {
  2344. var frustumPlanes = [];
  2345. for (var index = 0; index < 6; index++) {
  2346. frustumPlanes.push(new Plane(0, 0, 0, 0));
  2347. }
  2348. Frustum.GetPlanesToRef(transform, frustumPlanes);
  2349. return frustumPlanes;
  2350. }
  2351. public static GetPlanesToRef(transform: Matrix, frustumPlanes: Plane[]): void {
  2352. // Near
  2353. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  2354. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  2355. frustumPlanes[0].normal.z = transform.m[11] + transform.m[10];
  2356. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  2357. frustumPlanes[0].normalize();
  2358. // Far
  2359. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  2360. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  2361. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  2362. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  2363. frustumPlanes[1].normalize();
  2364. // Left
  2365. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  2366. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  2367. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  2368. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  2369. frustumPlanes[2].normalize();
  2370. // Right
  2371. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  2372. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  2373. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  2374. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  2375. frustumPlanes[3].normalize();
  2376. // Top
  2377. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  2378. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  2379. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  2380. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  2381. frustumPlanes[4].normalize();
  2382. // Bottom
  2383. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  2384. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  2385. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  2386. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  2387. frustumPlanes[5].normalize();
  2388. }
  2389. }
  2390. export class Ray {
  2391. private _edge1: Vector3;
  2392. private _edge2: Vector3;
  2393. private _pvec: Vector3;
  2394. private _tvec: Vector3;
  2395. private _qvec: Vector3;
  2396. constructor(public origin: Vector3, public direction: Vector3, public length: number = Number.MAX_VALUE) {
  2397. }
  2398. // Methods
  2399. public intersectsBoxMinMax(minimum: Vector3, maximum: Vector3): boolean {
  2400. var d = 0.0;
  2401. var maxValue = Number.MAX_VALUE;
  2402. var inv: number;
  2403. var min: number;
  2404. var max: number;
  2405. var temp: number;
  2406. if (Math.abs(this.direction.x) < 0.0000001) {
  2407. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  2408. return false;
  2409. }
  2410. }
  2411. else {
  2412. inv = 1.0 / this.direction.x;
  2413. min = (minimum.x - this.origin.x) * inv;
  2414. max = (maximum.x - this.origin.x) * inv;
  2415. if (max === -Infinity) {
  2416. max = Infinity;
  2417. }
  2418. if (min > max) {
  2419. temp = min;
  2420. min = max;
  2421. max = temp;
  2422. }
  2423. d = Math.max(min, d);
  2424. maxValue = Math.min(max, maxValue);
  2425. if (d > maxValue) {
  2426. return false;
  2427. }
  2428. }
  2429. if (Math.abs(this.direction.y) < 0.0000001) {
  2430. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  2431. return false;
  2432. }
  2433. }
  2434. else {
  2435. inv = 1.0 / this.direction.y;
  2436. min = (minimum.y - this.origin.y) * inv;
  2437. max = (maximum.y - this.origin.y) * inv;
  2438. if (max === -Infinity) {
  2439. max = Infinity;
  2440. }
  2441. if (min > max) {
  2442. temp = min;
  2443. min = max;
  2444. max = temp;
  2445. }
  2446. d = Math.max(min, d);
  2447. maxValue = Math.min(max, maxValue);
  2448. if (d > maxValue) {
  2449. return false;
  2450. }
  2451. }
  2452. if (Math.abs(this.direction.z) < 0.0000001) {
  2453. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  2454. return false;
  2455. }
  2456. }
  2457. else {
  2458. inv = 1.0 / this.direction.z;
  2459. min = (minimum.z - this.origin.z) * inv;
  2460. max = (maximum.z - this.origin.z) * inv;
  2461. if (max === -Infinity) {
  2462. max = Infinity;
  2463. }
  2464. if (min > max) {
  2465. temp = min;
  2466. min = max;
  2467. max = temp;
  2468. }
  2469. d = Math.max(min, d);
  2470. maxValue = Math.min(max, maxValue);
  2471. if (d > maxValue) {
  2472. return false;
  2473. }
  2474. }
  2475. return true;
  2476. }
  2477. public intersectsBox(box: BoundingBox): boolean {
  2478. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  2479. }
  2480. public intersectsSphere(sphere): boolean {
  2481. var x = sphere.center.x - this.origin.x;
  2482. var y = sphere.center.y - this.origin.y;
  2483. var z = sphere.center.z - this.origin.z;
  2484. var pyth = (x * x) + (y * y) + (z * z);
  2485. var rr = sphere.radius * sphere.radius;
  2486. if (pyth <= rr) {
  2487. return true;
  2488. }
  2489. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  2490. if (dot < 0.0) {
  2491. return false;
  2492. }
  2493. var temp = pyth - (dot * dot);
  2494. return temp <= rr;
  2495. }
  2496. public intersectsTriangle(vertex0: Vector3, vertex1: Vector3, vertex2: Vector3): IntersectionInfo {
  2497. if (!this._edge1) {
  2498. this._edge1 = Vector3.Zero();
  2499. this._edge2 = Vector3.Zero();
  2500. this._pvec = Vector3.Zero();
  2501. this._tvec = Vector3.Zero();
  2502. this._qvec = Vector3.Zero();
  2503. }
  2504. vertex1.subtractToRef(vertex0, this._edge1);
  2505. vertex2.subtractToRef(vertex0, this._edge2);
  2506. Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  2507. var det = Vector3.Dot(this._edge1, this._pvec);
  2508. if (det === 0) {
  2509. return null;
  2510. }
  2511. var invdet = 1 / det;
  2512. this.origin.subtractToRef(vertex0, this._tvec);
  2513. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  2514. if (bu < 0 || bu > 1.0) {
  2515. return null;
  2516. }
  2517. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  2518. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  2519. if (bv < 0 || bu + bv > 1.0) {
  2520. return null;
  2521. }
  2522. //check if the distance is longer than the predefined length.
  2523. var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
  2524. if (distance > this.length) {
  2525. return null;
  2526. }
  2527. return new IntersectionInfo(bu, bv, distance);
  2528. }
  2529. // Statics
  2530. public static CreateNew(x: number, y: number, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Ray {
  2531. var start = Vector3.Unproject(new Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  2532. var end = Vector3.Unproject(new Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  2533. var direction = end.subtract(start);
  2534. direction.normalize();
  2535. return new Ray(start, direction);
  2536. }
  2537. /**
  2538. * 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
  2539. * transformed to the given world matrix.
  2540. * @param origin The origin point
  2541. * @param end The end point
  2542. * @param world a matrix to transform the ray to. Default is the identity matrix.
  2543. */
  2544. public static CreateNewFromTo(origin: Vector3, end: Vector3, world: Matrix = Matrix.Identity()): Ray {
  2545. var direction = end.subtract(origin);
  2546. var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
  2547. direction.normalize();
  2548. return Ray.Transform(new Ray(origin, direction, length), world);
  2549. }
  2550. public static Transform(ray: Ray, matrix: Matrix): Ray {
  2551. var newOrigin = Vector3.TransformCoordinates(ray.origin, matrix);
  2552. var newDirection = Vector3.TransformNormal(ray.direction, matrix);
  2553. return new Ray(newOrigin, newDirection, ray.length);
  2554. }
  2555. }
  2556. export enum Space {
  2557. LOCAL = 0,
  2558. WORLD = 1
  2559. }
  2560. export class Axis {
  2561. public static X: Vector3 = new Vector3(1, 0, 0);
  2562. public static Y: Vector3 = new Vector3(0, 1, 0);
  2563. public static Z: Vector3 = new Vector3(0, 0, 1);
  2564. };
  2565. export class BezierCurve {
  2566. public static interpolate(t: number, x1: number, y1: number, x2: number, y2: number): number {
  2567. // Extract X (which is equal to time here)
  2568. var f0 = 1 - 3 * x2 + 3 * x1;
  2569. var f1 = 3 * x2 - 6 * x1;
  2570. var f2 = 3 * x1;
  2571. var refinedT = t;
  2572. for (var i = 0; i < 5; i++) {
  2573. var refinedT2 = refinedT * refinedT;
  2574. var refinedT3 = refinedT2 * refinedT;
  2575. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  2576. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  2577. refinedT -= (x - t) * slope;
  2578. refinedT = Math.min(1, Math.max(0, refinedT));
  2579. }
  2580. // Resolve cubic bezier for the given x
  2581. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 +
  2582. 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 +
  2583. Math.pow(refinedT, 3);
  2584. }
  2585. }
  2586. export enum Orientation {
  2587. CW = 0,
  2588. CCW = 1
  2589. }
  2590. export class Angle {
  2591. private _radians: number;
  2592. constructor(radians: number) {
  2593. this._radians = radians;
  2594. if (this._radians < 0) this._radians += (2 * Math.PI);
  2595. }
  2596. public degrees = () => this._radians * 180 / Math.PI;
  2597. public radians = () => this._radians;
  2598. public static BetweenTwoPoints(a: Vector2, b: Vector2): Angle {
  2599. var delta = b.subtract(a);
  2600. var theta = Math.atan2(delta.y, delta.x);
  2601. return new Angle(theta);
  2602. }
  2603. public static FromRadians(radians: number): Angle {
  2604. return new Angle(radians);
  2605. }
  2606. public static FromDegrees(degrees: number): Angle {
  2607. return new Angle(degrees * Math.PI / 180);
  2608. }
  2609. }
  2610. export class Arc2 {
  2611. centerPoint: Vector2;
  2612. radius: number;
  2613. angle: Angle;
  2614. startAngle: Angle;
  2615. orientation: Orientation;
  2616. constructor(public startPoint: Vector2, public midPoint: Vector2, public endPoint: Vector2) {
  2617. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  2618. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  2619. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  2620. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  2621. this.centerPoint = new Vector2(
  2622. (startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det,
  2623. ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det
  2624. );
  2625. this.radius = this.centerPoint.subtract(this.startPoint).length();
  2626. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  2627. var a1 = this.startAngle.degrees();
  2628. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  2629. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  2630. // angles correction
  2631. if (a2 - a1 > +180.0) a2 -= 360.0;
  2632. if (a2 - a1 < -180.0) a2 += 360.0;
  2633. if (a3 - a2 > +180.0) a3 -= 360.0;
  2634. if (a3 - a2 < -180.0) a3 += 360.0;
  2635. this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
  2636. this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
  2637. }
  2638. }
  2639. export class PathCursor {
  2640. private _onchange = new Array<(cursor: PathCursor) => void>();
  2641. value: number = 0;
  2642. animations = new Array<Animation>();
  2643. constructor(private path: Path2) {
  2644. }
  2645. public getPoint(): Vector3 {
  2646. var point = this.path.getPointAtLengthPosition(this.value);
  2647. return new Vector3(point.x, 0, point.y);
  2648. }
  2649. public moveAhead(step: number = 0.002): PathCursor {
  2650. this.move(step);
  2651. return this;
  2652. }
  2653. public moveBack(step: number = 0.002): PathCursor {
  2654. this.move(-step);
  2655. return this;
  2656. }
  2657. public move(step: number): PathCursor {
  2658. if (Math.abs(step) > 1) {
  2659. throw "step size should be less than 1.";
  2660. }
  2661. this.value += step;
  2662. this.ensureLimits();
  2663. this.raiseOnChange();
  2664. return this;
  2665. }
  2666. private ensureLimits(): PathCursor {
  2667. while (this.value > 1) {
  2668. this.value -= 1;
  2669. }
  2670. while (this.value < 0) {
  2671. this.value += 1;
  2672. }
  2673. return this;
  2674. }
  2675. // used by animation engine
  2676. private markAsDirty(propertyName: string): PathCursor {
  2677. this.ensureLimits();
  2678. this.raiseOnChange();
  2679. return this;
  2680. }
  2681. private raiseOnChange(): PathCursor {
  2682. this._onchange.forEach(f => f(this));
  2683. return this;
  2684. }
  2685. public onchange(f: (cursor: PathCursor) => void): PathCursor {
  2686. this._onchange.push(f);
  2687. return this;
  2688. }
  2689. }
  2690. export class Path2 {
  2691. private _points = new Array<Vector2>();
  2692. private _length = 0;
  2693. public closed = false;
  2694. constructor(x: number, y: number) {
  2695. this._points.push(new Vector2(x, y));
  2696. }
  2697. public addLineTo(x: number, y: number): Path2 {
  2698. if (closed) {
  2699. Tools.Error("cannot add lines to closed paths");
  2700. return this;
  2701. }
  2702. var newPoint = new Vector2(x, y);
  2703. var previousPoint = this._points[this._points.length - 1];
  2704. this._points.push(newPoint);
  2705. this._length += newPoint.subtract(previousPoint).length();
  2706. return this;
  2707. }
  2708. public addArcTo(midX: number, midY: number, endX: number, endY: number, numberOfSegments = 36): Path2 {
  2709. if (closed) {
  2710. Tools.Error("cannot add arcs to closed paths");
  2711. return this;
  2712. }
  2713. var startPoint = this._points[this._points.length - 1];
  2714. var midPoint = new Vector2(midX, midY);
  2715. var endPoint = new Vector2(endX, endY);
  2716. var arc = new Arc2(startPoint, midPoint, endPoint);
  2717. var increment = arc.angle.radians() / numberOfSegments;
  2718. if (arc.orientation === Orientation.CW) increment *= -1;
  2719. var currentAngle = arc.startAngle.radians() + increment;
  2720. for (var i = 0; i < numberOfSegments; i++) {
  2721. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  2722. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  2723. this.addLineTo(x, y);
  2724. currentAngle += increment;
  2725. }
  2726. return this;
  2727. }
  2728. public close(): Path2 {
  2729. this.closed = true;
  2730. return this;
  2731. }
  2732. public length(): number {
  2733. var result = this._length;
  2734. if (!this.closed) {
  2735. var lastPoint = this._points[this._points.length - 1];
  2736. var firstPoint = this._points[0];
  2737. result += (firstPoint.subtract(lastPoint).length());
  2738. }
  2739. return result;
  2740. }
  2741. public getPoints(): Vector2[] {
  2742. return this._points;
  2743. }
  2744. public getPointAtLengthPosition(normalizedLengthPosition: number): Vector2 {
  2745. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  2746. Tools.Error("normalized length position should be between 0 and 1.");
  2747. return Vector2.Zero();
  2748. }
  2749. var lengthPosition = normalizedLengthPosition * this.length();
  2750. var previousOffset = 0;
  2751. for (var i = 0; i < this._points.length; i++) {
  2752. var j = (i + 1) % this._points.length;
  2753. var a = this._points[i];
  2754. var b = this._points[j];
  2755. var bToA = b.subtract(a);
  2756. var nextOffset = (bToA.length() + previousOffset);
  2757. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  2758. var dir = bToA.normalize();
  2759. var localOffset = lengthPosition - previousOffset;
  2760. return new Vector2(
  2761. a.x + (dir.x * localOffset),
  2762. a.y + (dir.y * localOffset)
  2763. );
  2764. }
  2765. previousOffset = nextOffset;
  2766. }
  2767. Tools.Error("internal error");
  2768. return Vector2.Zero();
  2769. }
  2770. public static StartingAt(x: number, y: number): Path2 {
  2771. return new Path2(x, y);
  2772. }
  2773. }
  2774. export class Path3D {
  2775. private _curve = new Array<Vector3>();
  2776. private _distances = new Array<number>();
  2777. private _tangents = new Array<Vector3>();
  2778. private _normals = new Array<Vector3>();
  2779. private _binormals = new Array<Vector3>();
  2780. private _raw: boolean;
  2781. constructor(public path: Vector3[], firstNormal?: Vector3, raw?: boolean) {
  2782. for (var p = 0; p < path.length; p++) {
  2783. this._curve[p] = path[p].clone(); // hard copy
  2784. }
  2785. this._raw = raw || false;
  2786. this._compute(firstNormal);
  2787. }
  2788. public getCurve(): Vector3[] {
  2789. return this._curve;
  2790. }
  2791. public getTangents(): Vector3[] {
  2792. return this._tangents;
  2793. }
  2794. public getNormals(): Vector3[] {
  2795. return this._normals;
  2796. }
  2797. public getBinormals(): Vector3[] {
  2798. return this._binormals;
  2799. }
  2800. public getDistances(): number[] {
  2801. return this._distances;
  2802. }
  2803. public update(path: Vector3[], firstNormal?: Vector3): Path3D {
  2804. for (var p = 0; p < path.length; p++) {
  2805. this._curve[p].x = path[p].x;
  2806. this._curve[p].y = path[p].y;
  2807. this._curve[p].z = path[p].z;
  2808. }
  2809. this._compute(firstNormal);
  2810. return this;
  2811. }
  2812. // private function compute() : computes tangents, normals and binormals
  2813. private _compute(firstNormal) {
  2814. var l = this._curve.length;
  2815. // first and last tangents
  2816. this._tangents[0] = this._getFirstNonNullVector(0);
  2817. if (!this._raw) {
  2818. this._tangents[0].normalize();
  2819. }
  2820. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  2821. if (!this._raw) {
  2822. this._tangents[l - 1].normalize();
  2823. }
  2824. // normals and binormals at first point : arbitrary vector with _normalVector()
  2825. var tg0 = this._tangents[0];
  2826. var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
  2827. this._normals[0] = pp0;
  2828. if (!this._raw) {
  2829. this._normals[0].normalize();
  2830. }
  2831. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  2832. if (!this._raw) {
  2833. this._binormals[0].normalize();
  2834. }
  2835. this._distances[0] = 0;
  2836. // normals and binormals : next points
  2837. var prev: Vector3; // previous vector (segment)
  2838. var cur: Vector3; // current vector (segment)
  2839. var curTang: Vector3; // current tangent
  2840. // previous normal
  2841. var prevBinor: Vector3; // previous binormal
  2842. for (var i = 1; i < l; i++) {
  2843. // tangents
  2844. prev = this._getLastNonNullVector(i);
  2845. if (i < l - 1) {
  2846. cur = this._getFirstNonNullVector(i);
  2847. this._tangents[i] = prev.add(cur);
  2848. this._tangents[i].normalize();
  2849. }
  2850. this._distances[i] = this._distances[i - 1] + prev.length();
  2851. // normals and binormals
  2852. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  2853. curTang = this._tangents[i];
  2854. prevBinor = this._binormals[i - 1];
  2855. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  2856. if (!this._raw) {
  2857. this._normals[i].normalize();
  2858. }
  2859. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  2860. if (!this._raw) {
  2861. this._binormals[i].normalize();
  2862. }
  2863. }
  2864. }
  2865. // private function getFirstNonNullVector(index)
  2866. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  2867. private _getFirstNonNullVector(index: number): Vector3 {
  2868. var i = 1;
  2869. var nNVector: Vector3 = this._curve[index + i].subtract(this._curve[index]);
  2870. while (nNVector.length() === 0 && index + i + 1 < this._curve.length) {
  2871. i++;
  2872. nNVector = this._curve[index + i].subtract(this._curve[index]);
  2873. }
  2874. return nNVector;
  2875. }
  2876. // private function getLastNonNullVector(index)
  2877. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  2878. private _getLastNonNullVector(index: number): Vector3 {
  2879. var i = 1;
  2880. var nLVector: Vector3 = this._curve[index].subtract(this._curve[index - i]);
  2881. while (nLVector.length() === 0 && index > i + 1) {
  2882. i++;
  2883. nLVector = this._curve[index].subtract(this._curve[index - i]);
  2884. }
  2885. return nLVector;
  2886. }
  2887. // private function normalVector(v0, vt, va) :
  2888. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  2889. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  2890. private _normalVector(v0: Vector3, vt: Vector3, va: Vector3): Vector3 {
  2891. var normal0: Vector3;
  2892. if (va === undefined || va === null) {
  2893. var point: Vector3;
  2894. if (!Tools.WithinEpsilon(vt.y, 1, Engine.Epsilon)) { // search for a point in the plane
  2895. point = new Vector3(0, -1, 0);
  2896. }
  2897. else if (!Tools.WithinEpsilon(vt.x, 1, Engine.Epsilon)) {
  2898. point = new Vector3(1, 0, 0);
  2899. }
  2900. else if (!Tools.WithinEpsilon(vt.z, 1, Engine.Epsilon)) {
  2901. point = new Vector3(0, 0, 1);
  2902. }
  2903. normal0 = Vector3.Cross(vt, point);
  2904. }
  2905. else {
  2906. normal0 = Vector3.Cross(vt, va);
  2907. Vector3.CrossToRef(normal0, vt, normal0);
  2908. //normal0 = Vector3.Cross(normal0, vt);
  2909. }
  2910. normal0.normalize();
  2911. return normal0;
  2912. }
  2913. }
  2914. export class Curve3 {
  2915. private _points: Vector3[];
  2916. private _length: number = 0;
  2917. // QuadraticBezier(origin_V3, control_V3, destination_V3, nbPoints)
  2918. public static CreateQuadraticBezier(v0: Vector3, v1: Vector3, v2: Vector3, nbPoints: number): Curve3 {
  2919. nbPoints = nbPoints > 2 ? nbPoints : 3;
  2920. var bez = new Array<Vector3>();
  2921. var equation = (t: number, val0: number, val1: number, val2: number) => {
  2922. var res = (1 - t) * (1 - t) * val0 + 2 * t * (1 - t) * val1 + t * t * val2;
  2923. return res;
  2924. }
  2925. for (var i = 0; i <= nbPoints; i++) {
  2926. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x), equation(i / nbPoints, v0.y, v1.y, v2.y), equation(i / nbPoints, v0.z, v1.z, v2.z)));
  2927. }
  2928. return new Curve3(bez);
  2929. }
  2930. // CubicBezier(origin_V3, control1_V3, control2_V3, destination_V3, nbPoints)
  2931. public static CreateCubicBezier(v0: Vector3, v1: Vector3, v2: Vector3, v3: Vector3, nbPoints: number): Curve3 {
  2932. nbPoints = nbPoints > 3 ? nbPoints : 4;
  2933. var bez = new Array<Vector3>();
  2934. var equation = (t: number, val0: number, val1: number, val2: number, val3: number) => {
  2935. var res = (1 - t) * (1 - t) * (1 - t) * val0 + 3 * t * (1 - t) * (1 - t) * val1 + 3 * t * t * (1 - t) * val2 + t * t * t * val3;
  2936. return res;
  2937. }
  2938. for (var i = 0; i <= nbPoints; i++) {
  2939. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x, v3.x), equation(i / nbPoints, v0.y, v1.y, v2.y, v3.y), equation(i / nbPoints, v0.z, v1.z, v2.z, v3.z)));
  2940. }
  2941. return new Curve3(bez);
  2942. }
  2943. // HermiteSpline(origin_V3, originTangent_V3, destination_V3, destinationTangent_V3, nbPoints)
  2944. public static CreateHermiteSpline(p1: Vector3, t1: Vector3, p2: Vector3, t2: Vector3, nbPoints: number): Curve3 {
  2945. var hermite = new Array<Vector3>();
  2946. var step = 1 / nbPoints;
  2947. for (var i = 0; i <= nbPoints; i++) {
  2948. hermite.push(Vector3.Hermite(p1, t1, p2, t2, i * step));
  2949. }
  2950. return new Curve3(hermite);
  2951. }
  2952. constructor(points: Vector3[]) {
  2953. this._points = points;
  2954. this._length = this._computeLength(points);
  2955. }
  2956. public getPoints() {
  2957. return this._points;
  2958. }
  2959. public length() {
  2960. return this._length;
  2961. }
  2962. public continue(curve: Curve3): Curve3 {
  2963. var lastPoint = this._points[this._points.length - 1];
  2964. var continuedPoints = this._points.slice();
  2965. var curvePoints = curve.getPoints();
  2966. for (var i = 1; i < curvePoints.length; i++) {
  2967. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  2968. }
  2969. var continuedCurve = new Curve3(continuedPoints);
  2970. return continuedCurve;
  2971. }
  2972. private _computeLength(path: Vector3[]): number {
  2973. var l = 0;
  2974. for (var i = 1; i < path.length; i++) {
  2975. l += (path[i].subtract(path[i - 1])).length();
  2976. }
  2977. return l;
  2978. }
  2979. }
  2980. // Vertex formats
  2981. export class PositionNormalVertex {
  2982. constructor(public position: Vector3 = Vector3.Zero(), public normal: Vector3 = Vector3.Up()) {
  2983. }
  2984. public clone(): PositionNormalVertex {
  2985. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  2986. }
  2987. }
  2988. export class PositionNormalTextureVertex {
  2989. constructor(public position: Vector3 = Vector3.Zero(), public normal: Vector3 = Vector3.Up(), public uv: Vector2 = Vector2.Zero()) {
  2990. }
  2991. public clone(): PositionNormalTextureVertex {
  2992. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  2993. }
  2994. }
  2995. // SIMD
  2996. var previousMultiplyToArray = Matrix.prototype.multiplyToArray;
  2997. var previousInvertToRef = Matrix.prototype.invertToRef;
  2998. var previousLookAtLHToRef = Matrix.LookAtLHToRef;
  2999. var previousTransformCoordinatesToRef = Vector3.TransformCoordinatesToRef;
  3000. var previousTransformCoordinatesFromFloatsToRef = Vector3.TransformCoordinatesFromFloatsToRef;
  3001. export class SIMDHelper {
  3002. private static _isEnabled = false;
  3003. public static get IsEnabled(): boolean {
  3004. return SIMDHelper._isEnabled;
  3005. }
  3006. public static DisableSIMD(): void {
  3007. // Replace functions
  3008. Matrix.prototype.multiplyToArray = <any>previousMultiplyToArray;
  3009. Matrix.prototype.invertToRef = <any>previousInvertToRef;
  3010. Matrix.LookAtLHToRef = <any>previousLookAtLHToRef;
  3011. Vector3.TransformCoordinatesToRef = <any>previousTransformCoordinatesToRef;
  3012. Vector3.TransformCoordinatesFromFloatsToRef = <any>previousTransformCoordinatesFromFloatsToRef;
  3013. SIMDHelper._isEnabled = false;
  3014. }
  3015. public static EnableSIMD(): void {
  3016. if (window.SIMD === undefined) {
  3017. return;
  3018. }
  3019. // Replace functions
  3020. Matrix.prototype.multiplyToArray = <any>Matrix.prototype.multiplyToArraySIMD;
  3021. Matrix.prototype.invertToRef = <any>Matrix.prototype.invertToRefSIMD;
  3022. Matrix.LookAtLHToRef = <any>Matrix.LookAtLHToRefSIMD;
  3023. Vector3.TransformCoordinatesToRef = <any>Vector3.TransformCoordinatesToRefSIMD;
  3024. Vector3.TransformCoordinatesFromFloatsToRef = <any>Vector3.TransformCoordinatesFromFloatsToRefSIMD;
  3025. Object.defineProperty(Vector3.prototype, "x", {
  3026. get() { return this._data[0]; },
  3027. set(value: number) {
  3028. if (!this._data) {
  3029. this._data = new Float32Array(3);
  3030. }
  3031. this._data[0] = value;
  3032. }
  3033. });
  3034. Object.defineProperty(Vector3.prototype, "y", {
  3035. get() { return this._data[1]; },
  3036. set(value: number) {
  3037. this._data[1] = value;
  3038. }
  3039. });
  3040. Object.defineProperty(Vector3.prototype, "z", {
  3041. get() { return this._data[2]; },
  3042. set(value: number) {
  3043. this._data[2] = value;
  3044. }
  3045. });
  3046. SIMDHelper._isEnabled = true;
  3047. }
  3048. }
  3049. }