math.vector.ts 205 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430
  1. import { Scalar } from "./math.scalar";
  2. import { Epsilon } from './math.constants';
  3. import { Viewport } from './math.viewport';
  4. import { DeepImmutable, Nullable, FloatArray, float } from "../types";
  5. import { ArrayTools } from '../Misc/arrayTools';
  6. import { IPlaneLike } from './math.like';
  7. import { _TypeStore } from '../Misc/typeStore';
  8. /**
  9. * Class representing a vector containing 2 coordinates
  10. */
  11. export class Vector2 {
  12. /**
  13. * Creates a new Vector2 from the given x and y coordinates
  14. * @param x defines the first coordinate
  15. * @param y defines the second coordinate
  16. */
  17. constructor(
  18. /** defines the first coordinate */
  19. public x: number = 0,
  20. /** defines the second coordinate */
  21. public y: number = 0) {
  22. }
  23. /**
  24. * Gets a string with the Vector2 coordinates
  25. * @returns a string with the Vector2 coordinates
  26. */
  27. public toString(): string {
  28. return "{X: " + this.x + " Y:" + this.y + "}";
  29. }
  30. /**
  31. * Gets class name
  32. * @returns the string "Vector2"
  33. */
  34. public getClassName(): string {
  35. return "Vector2";
  36. }
  37. /**
  38. * Gets current vector hash code
  39. * @returns the Vector2 hash code as a number
  40. */
  41. public getHashCode(): number {
  42. let hash = this.x | 0;
  43. hash = (hash * 397) ^ (this.y | 0);
  44. return hash;
  45. }
  46. // Operators
  47. /**
  48. * Sets the Vector2 coordinates in the given array or Float32Array from the given index.
  49. * @param array defines the source array
  50. * @param index defines the offset in source array
  51. * @returns the current Vector2
  52. */
  53. public toArray(array: FloatArray, index: number = 0): Vector2 {
  54. array[index] = this.x;
  55. array[index + 1] = this.y;
  56. return this;
  57. }
  58. /**
  59. * Copy the current vector to an array
  60. * @returns a new array with 2 elements: the Vector2 coordinates.
  61. */
  62. public asArray(): number[] {
  63. var result = new Array<number>();
  64. this.toArray(result, 0);
  65. return result;
  66. }
  67. /**
  68. * Sets the Vector2 coordinates with the given Vector2 coordinates
  69. * @param source defines the source Vector2
  70. * @returns the current updated Vector2
  71. */
  72. public copyFrom(source: DeepImmutable<Vector2>): Vector2 {
  73. this.x = source.x;
  74. this.y = source.y;
  75. return this;
  76. }
  77. /**
  78. * Sets the Vector2 coordinates with the given floats
  79. * @param x defines the first coordinate
  80. * @param y defines the second coordinate
  81. * @returns the current updated Vector2
  82. */
  83. public copyFromFloats(x: number, y: number): Vector2 {
  84. this.x = x;
  85. this.y = y;
  86. return this;
  87. }
  88. /**
  89. * Sets the Vector2 coordinates with the given floats
  90. * @param x defines the first coordinate
  91. * @param y defines the second coordinate
  92. * @returns the current updated Vector2
  93. */
  94. public set(x: number, y: number): Vector2 {
  95. return this.copyFromFloats(x, y);
  96. }
  97. /**
  98. * Add another vector with the current one
  99. * @param otherVector defines the other vector
  100. * @returns a new Vector2 set with the addition of the current Vector2 and the given one coordinates
  101. */
  102. public add(otherVector: DeepImmutable<Vector2>): Vector2 {
  103. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  104. }
  105. /**
  106. * Sets the "result" coordinates with the addition of the current Vector2 and the given one coordinates
  107. * @param otherVector defines the other vector
  108. * @param result defines the target vector
  109. * @returns the unmodified current Vector2
  110. */
  111. public addToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  112. result.x = this.x + otherVector.x;
  113. result.y = this.y + otherVector.y;
  114. return this;
  115. }
  116. /**
  117. * Set the Vector2 coordinates by adding the given Vector2 coordinates
  118. * @param otherVector defines the other vector
  119. * @returns the current updated Vector2
  120. */
  121. public addInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  122. this.x += otherVector.x;
  123. this.y += otherVector.y;
  124. return this;
  125. }
  126. /**
  127. * Gets a new Vector2 by adding the current Vector2 coordinates to the given Vector3 x, y coordinates
  128. * @param otherVector defines the other vector
  129. * @returns a new Vector2
  130. */
  131. public addVector3(otherVector: Vector3): Vector2 {
  132. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  133. }
  134. /**
  135. * Gets a new Vector2 set with the subtracted coordinates of the given one from the current Vector2
  136. * @param otherVector defines the other vector
  137. * @returns a new Vector2
  138. */
  139. public subtract(otherVector: Vector2): Vector2 {
  140. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  141. }
  142. /**
  143. * Sets the "result" coordinates with the subtraction of the given one from the current Vector2 coordinates.
  144. * @param otherVector defines the other vector
  145. * @param result defines the target vector
  146. * @returns the unmodified current Vector2
  147. */
  148. public subtractToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  149. result.x = this.x - otherVector.x;
  150. result.y = this.y - otherVector.y;
  151. return this;
  152. }
  153. /**
  154. * Sets the current Vector2 coordinates by subtracting from it the given one coordinates
  155. * @param otherVector defines the other vector
  156. * @returns the current updated Vector2
  157. */
  158. public subtractInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  159. this.x -= otherVector.x;
  160. this.y -= otherVector.y;
  161. return this;
  162. }
  163. /**
  164. * Multiplies in place the current Vector2 coordinates by the given ones
  165. * @param otherVector defines the other vector
  166. * @returns the current updated Vector2
  167. */
  168. public multiplyInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  169. this.x *= otherVector.x;
  170. this.y *= otherVector.y;
  171. return this;
  172. }
  173. /**
  174. * Returns a new Vector2 set with the multiplication of the current Vector2 and the given one coordinates
  175. * @param otherVector defines the other vector
  176. * @returns a new Vector2
  177. */
  178. public multiply(otherVector: DeepImmutable<Vector2>): Vector2 {
  179. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  180. }
  181. /**
  182. * Sets "result" coordinates with the multiplication of the current Vector2 and the given one coordinates
  183. * @param otherVector defines the other vector
  184. * @param result defines the target vector
  185. * @returns the unmodified current Vector2
  186. */
  187. public multiplyToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  188. result.x = this.x * otherVector.x;
  189. result.y = this.y * otherVector.y;
  190. return this;
  191. }
  192. /**
  193. * Gets a new Vector2 set with the Vector2 coordinates multiplied by the given floats
  194. * @param x defines the first coordinate
  195. * @param y defines the second coordinate
  196. * @returns a new Vector2
  197. */
  198. public multiplyByFloats(x: number, y: number): Vector2 {
  199. return new Vector2(this.x * x, this.y * y);
  200. }
  201. /**
  202. * Returns a new Vector2 set with the Vector2 coordinates divided by the given one coordinates
  203. * @param otherVector defines the other vector
  204. * @returns a new Vector2
  205. */
  206. public divide(otherVector: Vector2): Vector2 {
  207. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  208. }
  209. /**
  210. * Sets the "result" coordinates with the Vector2 divided by the given one coordinates
  211. * @param otherVector defines the other vector
  212. * @param result defines the target vector
  213. * @returns the unmodified current Vector2
  214. */
  215. public divideToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  216. result.x = this.x / otherVector.x;
  217. result.y = this.y / otherVector.y;
  218. return this;
  219. }
  220. /**
  221. * Divides the current Vector2 coordinates by the given ones
  222. * @param otherVector defines the other vector
  223. * @returns the current updated Vector2
  224. */
  225. public divideInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  226. return this.divideToRef(otherVector, this);
  227. }
  228. /**
  229. * Gets a new Vector2 with current Vector2 negated coordinates
  230. * @returns a new Vector2
  231. */
  232. public negate(): Vector2 {
  233. return new Vector2(-this.x, -this.y);
  234. }
  235. /**
  236. * Negate this vector in place
  237. * @returns this
  238. */
  239. public negateInPlace(): Vector2 {
  240. this.x *= -1;
  241. this.y *= -1;
  242. return this;
  243. }
  244. /**
  245. * Negate the current Vector2 and stores the result in the given vector "result" coordinates
  246. * @param result defines the Vector3 object where to store the result
  247. * @returns the current Vector2
  248. */
  249. public negateToRef(result: Vector2): Vector2 {
  250. return result.copyFromFloats(this.x * -1, this.y * -1);
  251. }
  252. /**
  253. * Multiply the Vector2 coordinates by scale
  254. * @param scale defines the scaling factor
  255. * @returns the current updated Vector2
  256. */
  257. public scaleInPlace(scale: number): Vector2 {
  258. this.x *= scale;
  259. this.y *= scale;
  260. return this;
  261. }
  262. /**
  263. * Returns a new Vector2 scaled by "scale" from the current Vector2
  264. * @param scale defines the scaling factor
  265. * @returns a new Vector2
  266. */
  267. public scale(scale: number): Vector2 {
  268. let result = new Vector2(0, 0);
  269. this.scaleToRef(scale, result);
  270. return result;
  271. }
  272. /**
  273. * Scale the current Vector2 values by a factor to a given Vector2
  274. * @param scale defines the scale factor
  275. * @param result defines the Vector2 object where to store the result
  276. * @returns the unmodified current Vector2
  277. */
  278. public scaleToRef(scale: number, result: Vector2): Vector2 {
  279. result.x = this.x * scale;
  280. result.y = this.y * scale;
  281. return this;
  282. }
  283. /**
  284. * Scale the current Vector2 values by a factor and add the result to a given Vector2
  285. * @param scale defines the scale factor
  286. * @param result defines the Vector2 object where to store the result
  287. * @returns the unmodified current Vector2
  288. */
  289. public scaleAndAddToRef(scale: number, result: Vector2): Vector2 {
  290. result.x += this.x * scale;
  291. result.y += this.y * scale;
  292. return this;
  293. }
  294. /**
  295. * Gets a boolean if two vectors are equals
  296. * @param otherVector defines the other vector
  297. * @returns true if the given vector coordinates strictly equal the current Vector2 ones
  298. */
  299. public equals(otherVector: DeepImmutable<Vector2>): boolean {
  300. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  301. }
  302. /**
  303. * Gets a boolean if two vectors are equals (using an epsilon value)
  304. * @param otherVector defines the other vector
  305. * @param epsilon defines the minimal distance to consider equality
  306. * @returns true if the given vector coordinates are close to the current ones by a distance of epsilon.
  307. */
  308. public equalsWithEpsilon(otherVector: DeepImmutable<Vector2>, epsilon: number = Epsilon): boolean {
  309. return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon);
  310. }
  311. /**
  312. * Gets a new Vector2 from current Vector2 floored values
  313. * @returns a new Vector2
  314. */
  315. public floor(): Vector2 {
  316. return new Vector2(Math.floor(this.x), Math.floor(this.y));
  317. }
  318. /**
  319. * Gets a new Vector2 from current Vector2 floored values
  320. * @returns a new Vector2
  321. */
  322. public fract(): Vector2 {
  323. return new Vector2(this.x - Math.floor(this.x), this.y - Math.floor(this.y));
  324. }
  325. // Properties
  326. /**
  327. * Gets the length of the vector
  328. * @returns the vector length (float)
  329. */
  330. public length(): number {
  331. return Math.sqrt(this.x * this.x + this.y * this.y);
  332. }
  333. /**
  334. * Gets the vector squared length
  335. * @returns the vector squared length (float)
  336. */
  337. public lengthSquared(): number {
  338. return (this.x * this.x + this.y * this.y);
  339. }
  340. // Methods
  341. /**
  342. * Normalize the vector
  343. * @returns the current updated Vector2
  344. */
  345. public normalize(): Vector2 {
  346. var len = this.length();
  347. if (len === 0) {
  348. return this;
  349. }
  350. this.x /= len;
  351. this.y /= len;
  352. return this;
  353. }
  354. /**
  355. * Gets a new Vector2 copied from the Vector2
  356. * @returns a new Vector2
  357. */
  358. public clone(): Vector2 {
  359. return new Vector2(this.x, this.y);
  360. }
  361. // Statics
  362. /**
  363. * Gets a new Vector2(0, 0)
  364. * @returns a new Vector2
  365. */
  366. public static Zero(): Vector2 {
  367. return new Vector2(0, 0);
  368. }
  369. /**
  370. * Gets a new Vector2(1, 1)
  371. * @returns a new Vector2
  372. */
  373. public static One(): Vector2 {
  374. return new Vector2(1, 1);
  375. }
  376. /**
  377. * Gets a new Vector2 set from the given index element of the given array
  378. * @param array defines the data source
  379. * @param offset defines the offset in the data source
  380. * @returns a new Vector2
  381. */
  382. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Vector2 {
  383. return new Vector2(array[offset], array[offset + 1]);
  384. }
  385. /**
  386. * Sets "result" from the given index element of the given array
  387. * @param array defines the data source
  388. * @param offset defines the offset in the data source
  389. * @param result defines the target vector
  390. */
  391. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector2): void {
  392. result.x = array[offset];
  393. result.y = array[offset + 1];
  394. }
  395. /**
  396. * Gets a new Vector2 located for "amount" (float) on the CatmullRom spline defined by the given four Vector2
  397. * @param value1 defines 1st point of control
  398. * @param value2 defines 2nd point of control
  399. * @param value3 defines 3rd point of control
  400. * @param value4 defines 4th point of control
  401. * @param amount defines the interpolation factor
  402. * @returns a new Vector2
  403. */
  404. public static CatmullRom(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>, value3: DeepImmutable<Vector2>, value4: DeepImmutable<Vector2>, amount: number): Vector2 {
  405. var squared = amount * amount;
  406. var cubed = amount * squared;
  407. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  408. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  409. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  410. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  411. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  412. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  413. return new Vector2(x, y);
  414. }
  415. /**
  416. * Returns a new Vector2 set with same the coordinates than "value" ones if the vector "value" is in the square defined by "min" and "max".
  417. * If a coordinate of "value" is lower than "min" coordinates, the returned Vector2 is given this "min" coordinate.
  418. * If a coordinate of "value" is greater than "max" coordinates, the returned Vector2 is given this "max" coordinate
  419. * @param value defines the value to clamp
  420. * @param min defines the lower limit
  421. * @param max defines the upper limit
  422. * @returns a new Vector2
  423. */
  424. public static Clamp(value: DeepImmutable<Vector2>, min: DeepImmutable<Vector2>, max: DeepImmutable<Vector2>): Vector2 {
  425. var x = value.x;
  426. x = (x > max.x) ? max.x : x;
  427. x = (x < min.x) ? min.x : x;
  428. var y = value.y;
  429. y = (y > max.y) ? max.y : y;
  430. y = (y < min.y) ? min.y : y;
  431. return new Vector2(x, y);
  432. }
  433. /**
  434. * Returns a new Vector2 located for "amount" (float) on the Hermite spline defined by the vectors "value1", "value3", "tangent1", "tangent2"
  435. * @param value1 defines the 1st control point
  436. * @param tangent1 defines the outgoing tangent
  437. * @param value2 defines the 2nd control point
  438. * @param tangent2 defines the incoming tangent
  439. * @param amount defines the interpolation factor
  440. * @returns a new Vector2
  441. */
  442. public static Hermite(value1: DeepImmutable<Vector2>, tangent1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>, tangent2: DeepImmutable<Vector2>, amount: number): Vector2 {
  443. var squared = amount * amount;
  444. var cubed = amount * squared;
  445. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  446. var part2 = (-2.0 * cubed) + (3.0 * squared);
  447. var part3 = (cubed - (2.0 * squared)) + amount;
  448. var part4 = cubed - squared;
  449. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  450. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  451. return new Vector2(x, y);
  452. }
  453. /**
  454. * Returns a new Vector2 located for "amount" (float) on the linear interpolation between the vector "start" adn the vector "end".
  455. * @param start defines the start vector
  456. * @param end defines the end vector
  457. * @param amount defines the interpolation factor
  458. * @returns a new Vector2
  459. */
  460. public static Lerp(start: DeepImmutable<Vector2>, end: DeepImmutable<Vector2>, amount: number): Vector2 {
  461. var x = start.x + ((end.x - start.x) * amount);
  462. var y = start.y + ((end.y - start.y) * amount);
  463. return new Vector2(x, y);
  464. }
  465. /**
  466. * Gets the dot product of the vector "left" and the vector "right"
  467. * @param left defines first vector
  468. * @param right defines second vector
  469. * @returns the dot product (float)
  470. */
  471. public static Dot(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): number {
  472. return left.x * right.x + left.y * right.y;
  473. }
  474. /**
  475. * Returns a new Vector2 equal to the normalized given vector
  476. * @param vector defines the vector to normalize
  477. * @returns a new Vector2
  478. */
  479. public static Normalize(vector: DeepImmutable<Vector2>): Vector2 {
  480. var newVector = vector.clone();
  481. newVector.normalize();
  482. return newVector;
  483. }
  484. /**
  485. * Gets a new Vector2 set with the minimal coordinate values from the "left" and "right" vectors
  486. * @param left defines 1st vector
  487. * @param right defines 2nd vector
  488. * @returns a new Vector2
  489. */
  490. public static Minimize(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): Vector2 {
  491. var x = (left.x < right.x) ? left.x : right.x;
  492. var y = (left.y < right.y) ? left.y : right.y;
  493. return new Vector2(x, y);
  494. }
  495. /**
  496. * Gets a new Vecto2 set with the maximal coordinate values from the "left" and "right" vectors
  497. * @param left defines 1st vector
  498. * @param right defines 2nd vector
  499. * @returns a new Vector2
  500. */
  501. public static Maximize(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): Vector2 {
  502. var x = (left.x > right.x) ? left.x : right.x;
  503. var y = (left.y > right.y) ? left.y : right.y;
  504. return new Vector2(x, y);
  505. }
  506. /**
  507. * Gets a new Vector2 set with the transformed coordinates of the given vector by the given transformation matrix
  508. * @param vector defines the vector to transform
  509. * @param transformation defines the matrix to apply
  510. * @returns a new Vector2
  511. */
  512. public static Transform(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Matrix>): Vector2 {
  513. let r = Vector2.Zero();
  514. Vector2.TransformToRef(vector, transformation, r);
  515. return r;
  516. }
  517. /**
  518. * Transforms the given vector coordinates by the given transformation matrix and stores the result in the vector "result" coordinates
  519. * @param vector defines the vector to transform
  520. * @param transformation defines the matrix to apply
  521. * @param result defines the target vector
  522. */
  523. public static TransformToRef(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Matrix>, result: Vector2) {
  524. const m = transformation.m;
  525. var x = (vector.x * m[0]) + (vector.y * m[4]) + m[12];
  526. var y = (vector.x * m[1]) + (vector.y * m[5]) + m[13];
  527. result.x = x;
  528. result.y = y;
  529. }
  530. /**
  531. * Determines if a given vector is included in a triangle
  532. * @param p defines the vector to test
  533. * @param p0 defines 1st triangle point
  534. * @param p1 defines 2nd triangle point
  535. * @param p2 defines 3rd triangle point
  536. * @returns true if the point "p" is in the triangle defined by the vertors "p0", "p1", "p2"
  537. */
  538. public static PointInTriangle(p: DeepImmutable<Vector2>, p0: DeepImmutable<Vector2>, p1: DeepImmutable<Vector2>, p2: DeepImmutable<Vector2>) {
  539. let a = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  540. let sign = a < 0 ? -1 : 1;
  541. let s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
  542. let t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
  543. return s > 0 && t > 0 && (s + t) < 2 * a * sign;
  544. }
  545. /**
  546. * Gets the distance between the vectors "value1" and "value2"
  547. * @param value1 defines first vector
  548. * @param value2 defines second vector
  549. * @returns the distance between vectors
  550. */
  551. public static Distance(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): number {
  552. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  553. }
  554. /**
  555. * Returns the squared distance between the vectors "value1" and "value2"
  556. * @param value1 defines first vector
  557. * @param value2 defines second vector
  558. * @returns the squared distance between vectors
  559. */
  560. public static DistanceSquared(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): number {
  561. var x = value1.x - value2.x;
  562. var y = value1.y - value2.y;
  563. return (x * x) + (y * y);
  564. }
  565. /**
  566. * Gets a new Vector2 located at the center of the vectors "value1" and "value2"
  567. * @param value1 defines first vector
  568. * @param value2 defines second vector
  569. * @returns a new Vector2
  570. */
  571. public static Center(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): Vector2 {
  572. var center = value1.add(value2);
  573. center.scaleInPlace(0.5);
  574. return center;
  575. }
  576. /**
  577. * Gets the shortest distance (float) between the point "p" and the segment defined by the two points "segA" and "segB".
  578. * @param p defines the middle point
  579. * @param segA defines one point of the segment
  580. * @param segB defines the other point of the segment
  581. * @returns the shortest distance
  582. */
  583. public static DistanceOfPointFromSegment(p: DeepImmutable<Vector2>, segA: DeepImmutable<Vector2>, segB: DeepImmutable<Vector2>): number {
  584. let l2 = Vector2.DistanceSquared(segA, segB);
  585. if (l2 === 0.0) {
  586. return Vector2.Distance(p, segA);
  587. }
  588. let v = segB.subtract(segA);
  589. let t = Math.max(0, Math.min(1, Vector2.Dot(p.subtract(segA), v) / l2));
  590. let proj = segA.add(v.multiplyByFloats(t, t));
  591. return Vector2.Distance(p, proj);
  592. }
  593. }
  594. /**
  595. * Class used to store (x,y,z) vector representation
  596. * A Vector3 is the main object used in 3D geometry
  597. * It can represent etiher the coordinates of a point the space, either a direction
  598. * Reminder: js uses a left handed forward facing system
  599. */
  600. export class Vector3 {
  601. private static _UpReadOnly = Vector3.Up() as DeepImmutable<Vector3>;
  602. private static _ZeroReadOnly = Vector3.Zero() as DeepImmutable<Vector3>;
  603. /**
  604. * Creates a new Vector3 object from the given x, y, z (floats) coordinates.
  605. * @param x defines the first coordinates (on X axis)
  606. * @param y defines the second coordinates (on Y axis)
  607. * @param z defines the third coordinates (on Z axis)
  608. */
  609. constructor(
  610. /**
  611. * Defines the first coordinates (on X axis)
  612. */
  613. public x: number = 0,
  614. /**
  615. * Defines the second coordinates (on Y axis)
  616. */
  617. public y: number = 0,
  618. /**
  619. * Defines the third coordinates (on Z axis)
  620. */
  621. public z: number = 0
  622. ) {
  623. }
  624. /**
  625. * Creates a string representation of the Vector3
  626. * @returns a string with the Vector3 coordinates.
  627. */
  628. public toString(): string {
  629. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  630. }
  631. /**
  632. * Gets the class name
  633. * @returns the string "Vector3"
  634. */
  635. public getClassName(): string {
  636. return "Vector3";
  637. }
  638. /**
  639. * Creates the Vector3 hash code
  640. * @returns a number which tends to be unique between Vector3 instances
  641. */
  642. public getHashCode(): number {
  643. let hash = this.x | 0;
  644. hash = (hash * 397) ^ (this.y | 0);
  645. hash = (hash * 397) ^ (this.z | 0);
  646. return hash;
  647. }
  648. // Operators
  649. /**
  650. * Creates an array containing three elements : the coordinates of the Vector3
  651. * @returns a new array of numbers
  652. */
  653. public asArray(): number[] {
  654. var result: number[] = [];
  655. this.toArray(result, 0);
  656. return result;
  657. }
  658. /**
  659. * Populates the given array or Float32Array from the given index with the successive coordinates of the Vector3
  660. * @param array defines the destination array
  661. * @param index defines the offset in the destination array
  662. * @returns the current Vector3
  663. */
  664. public toArray(array: FloatArray, index: number = 0): Vector3 {
  665. array[index] = this.x;
  666. array[index + 1] = this.y;
  667. array[index + 2] = this.z;
  668. return this;
  669. }
  670. /**
  671. * Converts the current Vector3 into a quaternion (considering that the Vector3 contains Euler angles representation of a rotation)
  672. * @returns a new Quaternion object, computed from the Vector3 coordinates
  673. */
  674. public toQuaternion(): Quaternion {
  675. return Quaternion.RotationYawPitchRoll(this.y, this.x, this.z);
  676. }
  677. /**
  678. * Adds the given vector to the current Vector3
  679. * @param otherVector defines the second operand
  680. * @returns the current updated Vector3
  681. */
  682. public addInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  683. return this.addInPlaceFromFloats(otherVector.x, otherVector.y, otherVector.z);
  684. }
  685. /**
  686. * Adds the given coordinates to the current Vector3
  687. * @param x defines the x coordinate of the operand
  688. * @param y defines the y coordinate of the operand
  689. * @param z defines the z coordinate of the operand
  690. * @returns the current updated Vector3
  691. */
  692. public addInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  693. this.x += x;
  694. this.y += y;
  695. this.z += z;
  696. return this;
  697. }
  698. /**
  699. * Gets a new Vector3, result of the addition the current Vector3 and the given vector
  700. * @param otherVector defines the second operand
  701. * @returns the resulting Vector3
  702. */
  703. public add(otherVector: DeepImmutable<Vector3>): Vector3 {
  704. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  705. }
  706. /**
  707. * Adds the current Vector3 to the given one and stores the result in the vector "result"
  708. * @param otherVector defines the second operand
  709. * @param result defines the Vector3 object where to store the result
  710. * @returns the current Vector3
  711. */
  712. public addToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  713. return result.copyFromFloats(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  714. }
  715. /**
  716. * Subtract the given vector from the current Vector3
  717. * @param otherVector defines the second operand
  718. * @returns the current updated Vector3
  719. */
  720. public subtractInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  721. this.x -= otherVector.x;
  722. this.y -= otherVector.y;
  723. this.z -= otherVector.z;
  724. return this;
  725. }
  726. /**
  727. * Returns a new Vector3, result of the subtraction of the given vector from the current Vector3
  728. * @param otherVector defines the second operand
  729. * @returns the resulting Vector3
  730. */
  731. public subtract(otherVector: DeepImmutable<Vector3>): Vector3 {
  732. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  733. }
  734. /**
  735. * Subtracts the given vector from the current Vector3 and stores the result in the vector "result".
  736. * @param otherVector defines the second operand
  737. * @param result defines the Vector3 object where to store the result
  738. * @returns the current Vector3
  739. */
  740. public subtractToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  741. return this.subtractFromFloatsToRef(otherVector.x, otherVector.y, otherVector.z, result);
  742. }
  743. /**
  744. * Returns a new Vector3 set with the subtraction of the given floats from the current Vector3 coordinates
  745. * @param x defines the x coordinate of the operand
  746. * @param y defines the y coordinate of the operand
  747. * @param z defines the z coordinate of the operand
  748. * @returns the resulting Vector3
  749. */
  750. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  751. return new Vector3(this.x - x, this.y - y, this.z - z);
  752. }
  753. /**
  754. * Subtracts the given floats from the current Vector3 coordinates and set the given vector "result" with this result
  755. * @param x defines the x coordinate of the operand
  756. * @param y defines the y coordinate of the operand
  757. * @param z defines the z coordinate of the operand
  758. * @param result defines the Vector3 object where to store the result
  759. * @returns the current Vector3
  760. */
  761. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
  762. return result.copyFromFloats(this.x - x, this.y - y, this.z - z);
  763. }
  764. /**
  765. * Gets a new Vector3 set with the current Vector3 negated coordinates
  766. * @returns a new Vector3
  767. */
  768. public negate(): Vector3 {
  769. return new Vector3(-this.x, -this.y, -this.z);
  770. }
  771. /**
  772. * Negate this vector in place
  773. * @returns this
  774. */
  775. public negateInPlace(): Vector3 {
  776. this.x *= -1;
  777. this.y *= -1;
  778. this.z *= -1;
  779. return this;
  780. }
  781. /**
  782. * Negate the current Vector3 and stores the result in the given vector "result" coordinates
  783. * @param result defines the Vector3 object where to store the result
  784. * @returns the current Vector3
  785. */
  786. public negateToRef(result: Vector3): Vector3 {
  787. return result.copyFromFloats(this.x * -1, this.y * -1, this.z * -1);
  788. }
  789. /**
  790. * Multiplies the Vector3 coordinates by the float "scale"
  791. * @param scale defines the multiplier factor
  792. * @returns the current updated Vector3
  793. */
  794. public scaleInPlace(scale: number): Vector3 {
  795. this.x *= scale;
  796. this.y *= scale;
  797. this.z *= scale;
  798. return this;
  799. }
  800. /**
  801. * Returns a new Vector3 set with the current Vector3 coordinates multiplied by the float "scale"
  802. * @param scale defines the multiplier factor
  803. * @returns a new Vector3
  804. */
  805. public scale(scale: number): Vector3 {
  806. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  807. }
  808. /**
  809. * Multiplies the current Vector3 coordinates by the float "scale" and stores the result in the given vector "result" coordinates
  810. * @param scale defines the multiplier factor
  811. * @param result defines the Vector3 object where to store the result
  812. * @returns the current Vector3
  813. */
  814. public scaleToRef(scale: number, result: Vector3): Vector3 {
  815. return result.copyFromFloats(this.x * scale, this.y * scale, this.z * scale);
  816. }
  817. /**
  818. * Scale the current Vector3 values by a factor and add the result to a given Vector3
  819. * @param scale defines the scale factor
  820. * @param result defines the Vector3 object where to store the result
  821. * @returns the unmodified current Vector3
  822. */
  823. public scaleAndAddToRef(scale: number, result: Vector3): Vector3 {
  824. return result.addInPlaceFromFloats(this.x * scale, this.y * scale, this.z * scale);
  825. }
  826. /**
  827. * Returns true if the current Vector3 and the given vector coordinates are strictly equal
  828. * @param otherVector defines the second operand
  829. * @returns true if both vectors are equals
  830. */
  831. public equals(otherVector: DeepImmutable<Vector3>): boolean {
  832. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  833. }
  834. /**
  835. * Returns true if the current Vector3 and the given vector coordinates are distant less than epsilon
  836. * @param otherVector defines the second operand
  837. * @param epsilon defines the minimal distance to define values as equals
  838. * @returns true if both vectors are distant less than epsilon
  839. */
  840. public equalsWithEpsilon(otherVector: DeepImmutable<Vector3>, epsilon: number = Epsilon): boolean {
  841. return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon) && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon);
  842. }
  843. /**
  844. * Returns true if the current Vector3 coordinates equals the given floats
  845. * @param x defines the x coordinate of the operand
  846. * @param y defines the y coordinate of the operand
  847. * @param z defines the z coordinate of the operand
  848. * @returns true if both vectors are equals
  849. */
  850. public equalsToFloats(x: number, y: number, z: number): boolean {
  851. return this.x === x && this.y === y && this.z === z;
  852. }
  853. /**
  854. * Multiplies the current Vector3 coordinates by the given ones
  855. * @param otherVector defines the second operand
  856. * @returns the current updated Vector3
  857. */
  858. public multiplyInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  859. this.x *= otherVector.x;
  860. this.y *= otherVector.y;
  861. this.z *= otherVector.z;
  862. return this;
  863. }
  864. /**
  865. * Returns a new Vector3, result of the multiplication of the current Vector3 by the given vector
  866. * @param otherVector defines the second operand
  867. * @returns the new Vector3
  868. */
  869. public multiply(otherVector: DeepImmutable<Vector3>): Vector3 {
  870. return this.multiplyByFloats(otherVector.x, otherVector.y, otherVector.z);
  871. }
  872. /**
  873. * Multiplies the current Vector3 by the given one and stores the result in the given vector "result"
  874. * @param otherVector defines the second operand
  875. * @param result defines the Vector3 object where to store the result
  876. * @returns the current Vector3
  877. */
  878. public multiplyToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  879. return result.copyFromFloats(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  880. }
  881. /**
  882. * Returns a new Vector3 set with the result of the mulliplication of the current Vector3 coordinates by the given floats
  883. * @param x defines the x coordinate of the operand
  884. * @param y defines the y coordinate of the operand
  885. * @param z defines the z coordinate of the operand
  886. * @returns the new Vector3
  887. */
  888. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  889. return new Vector3(this.x * x, this.y * y, this.z * z);
  890. }
  891. /**
  892. * Returns a new Vector3 set with the result of the division of the current Vector3 coordinates by the given ones
  893. * @param otherVector defines the second operand
  894. * @returns the new Vector3
  895. */
  896. public divide(otherVector: DeepImmutable<Vector3>): Vector3 {
  897. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  898. }
  899. /**
  900. * Divides the current Vector3 coordinates by the given ones and stores the result in the given vector "result"
  901. * @param otherVector defines the second operand
  902. * @param result defines the Vector3 object where to store the result
  903. * @returns the current Vector3
  904. */
  905. public divideToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  906. return result.copyFromFloats(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  907. }
  908. /**
  909. * Divides the current Vector3 coordinates by the given ones.
  910. * @param otherVector defines the second operand
  911. * @returns the current updated Vector3
  912. */
  913. public divideInPlace(otherVector: Vector3): Vector3 {
  914. return this.divideToRef(otherVector, this);
  915. }
  916. /**
  917. * Updates the current Vector3 with the minimal coordinate values between its and the given vector ones
  918. * @param other defines the second operand
  919. * @returns the current updated Vector3
  920. */
  921. public minimizeInPlace(other: DeepImmutable<Vector3>): Vector3 {
  922. return this.minimizeInPlaceFromFloats(other.x, other.y, other.z);
  923. }
  924. /**
  925. * Updates the current Vector3 with the maximal coordinate values between its and the given vector ones.
  926. * @param other defines the second operand
  927. * @returns the current updated Vector3
  928. */
  929. public maximizeInPlace(other: DeepImmutable<Vector3>): Vector3 {
  930. return this.maximizeInPlaceFromFloats(other.x, other.y, other.z);
  931. }
  932. /**
  933. * Updates the current Vector3 with the minimal coordinate values between its and the given coordinates
  934. * @param x defines the x coordinate of the operand
  935. * @param y defines the y coordinate of the operand
  936. * @param z defines the z coordinate of the operand
  937. * @returns the current updated Vector3
  938. */
  939. public minimizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  940. if (x < this.x) { this.x = x; }
  941. if (y < this.y) { this.y = y; }
  942. if (z < this.z) { this.z = z; }
  943. return this;
  944. }
  945. /**
  946. * Updates the current Vector3 with the maximal coordinate values between its and the given coordinates.
  947. * @param x defines the x coordinate of the operand
  948. * @param y defines the y coordinate of the operand
  949. * @param z defines the z coordinate of the operand
  950. * @returns the current updated Vector3
  951. */
  952. public maximizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  953. if (x > this.x) { this.x = x; }
  954. if (y > this.y) { this.y = y; }
  955. if (z > this.z) { this.z = z; }
  956. return this;
  957. }
  958. /**
  959. * Due to float precision, scale of a mesh could be uniform but float values are off by a small fraction
  960. * Check if is non uniform within a certain amount of decimal places to account for this
  961. * @param epsilon the amount the values can differ
  962. * @returns if the the vector is non uniform to a certain number of decimal places
  963. */
  964. public isNonUniformWithinEpsilon(epsilon: number) {
  965. let absX = Math.abs(this.x);
  966. let absY = Math.abs(this.y);
  967. if (!Scalar.WithinEpsilon(absX, absY, epsilon)) {
  968. return true;
  969. }
  970. let absZ = Math.abs(this.z);
  971. if (!Scalar.WithinEpsilon(absX, absZ, epsilon)) {
  972. return true;
  973. }
  974. if (!Scalar.WithinEpsilon(absY, absZ, epsilon)) {
  975. return true;
  976. }
  977. return false;
  978. }
  979. /**
  980. * Gets a boolean indicating that the vector is non uniform meaning x, y or z are not all the same
  981. */
  982. public get isNonUniform(): boolean {
  983. let absX = Math.abs(this.x);
  984. let absY = Math.abs(this.y);
  985. if (absX !== absY) {
  986. return true;
  987. }
  988. let absZ = Math.abs(this.z);
  989. if (absX !== absZ) {
  990. return true;
  991. }
  992. if (absY !== absZ) {
  993. return true;
  994. }
  995. return false;
  996. }
  997. /**
  998. * Gets a new Vector3 from current Vector3 floored values
  999. * @returns a new Vector3
  1000. */
  1001. public floor(): Vector3 {
  1002. return new Vector3(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z));
  1003. }
  1004. /**
  1005. * Gets a new Vector3 from current Vector3 floored values
  1006. * @returns a new Vector3
  1007. */
  1008. public fract(): Vector3 {
  1009. return new Vector3(this.x - Math.floor(this.x), this.y - Math.floor(this.y), this.z - Math.floor(this.z));
  1010. }
  1011. // Properties
  1012. /**
  1013. * Gets the length of the Vector3
  1014. * @returns the length of the Vector3
  1015. */
  1016. public length(): number {
  1017. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  1018. }
  1019. /**
  1020. * Gets the squared length of the Vector3
  1021. * @returns squared length of the Vector3
  1022. */
  1023. public lengthSquared(): number {
  1024. return (this.x * this.x + this.y * this.y + this.z * this.z);
  1025. }
  1026. /**
  1027. * Normalize the current Vector3.
  1028. * Please note that this is an in place operation.
  1029. * @returns the current updated Vector3
  1030. */
  1031. public normalize(): Vector3 {
  1032. return this.normalizeFromLength(this.length());
  1033. }
  1034. /**
  1035. * Reorders the x y z properties of the vector in place
  1036. * @param order new ordering of the properties (eg. for vector 1,2,3 with "ZYX" will produce 3,2,1)
  1037. * @returns the current updated vector
  1038. */
  1039. public reorderInPlace(order: string) {
  1040. order = order.toLowerCase();
  1041. if (order === "xyz") {
  1042. return this;
  1043. }
  1044. MathTmp.Vector3[0].copyFrom(this);
  1045. ["x", "y", "z"].forEach((val, i) => {
  1046. (<any>this)[val] = (<any>MathTmp.Vector3[0])[order[i]];
  1047. });
  1048. return this;
  1049. }
  1050. /**
  1051. * Rotates the vector around 0,0,0 by a quaternion
  1052. * @param quaternion the rotation quaternion
  1053. * @param result vector to store the result
  1054. * @returns the resulting vector
  1055. */
  1056. public rotateByQuaternionToRef(quaternion: Quaternion, result: Vector3) {
  1057. quaternion.toRotationMatrix(MathTmp.Matrix[0]);
  1058. Vector3.TransformCoordinatesToRef(this, MathTmp.Matrix[0], result);
  1059. return result;
  1060. }
  1061. /**
  1062. * Rotates a vector around a given point
  1063. * @param quaternion the rotation quaternion
  1064. * @param point the point to rotate around
  1065. * @param result vector to store the result
  1066. * @returns the resulting vector
  1067. */
  1068. public rotateByQuaternionAroundPointToRef(quaternion: Quaternion, point: Vector3, result: Vector3) {
  1069. this.subtractToRef(point, MathTmp.Vector3[0]);
  1070. MathTmp.Vector3[0].rotateByQuaternionToRef(quaternion, MathTmp.Vector3[0]);
  1071. point.addToRef(MathTmp.Vector3[0], result);
  1072. return result;
  1073. }
  1074. /**
  1075. * Returns a new Vector3 as the cross product of the current vector and the "other" one
  1076. * The cross product is then orthogonal to both current and "other"
  1077. * @param other defines the right operand
  1078. * @returns the cross product
  1079. */
  1080. public cross(other: Vector3) {
  1081. return Vector3.Cross(this, other);
  1082. }
  1083. /**
  1084. * Normalize the current Vector3 with the given input length.
  1085. * Please note that this is an in place operation.
  1086. * @param len the length of the vector
  1087. * @returns the current updated Vector3
  1088. */
  1089. public normalizeFromLength(len: number): Vector3 {
  1090. if (len === 0 || len === 1.0) {
  1091. return this;
  1092. }
  1093. return this.scaleInPlace(1.0 / len);
  1094. }
  1095. /**
  1096. * Normalize the current Vector3 to a new vector
  1097. * @returns the new Vector3
  1098. */
  1099. public normalizeToNew(): Vector3 {
  1100. const normalized = new Vector3(0, 0, 0);
  1101. this.normalizeToRef(normalized);
  1102. return normalized;
  1103. }
  1104. /**
  1105. * Normalize the current Vector3 to the reference
  1106. * @param reference define the Vector3 to update
  1107. * @returns the updated Vector3
  1108. */
  1109. public normalizeToRef(reference: DeepImmutable<Vector3>): Vector3 {
  1110. var len = this.length();
  1111. if (len === 0 || len === 1.0) {
  1112. return reference.copyFromFloats(this.x, this.y, this.z);
  1113. }
  1114. return this.scaleToRef(1.0 / len, reference);
  1115. }
  1116. /**
  1117. * Creates a new Vector3 copied from the current Vector3
  1118. * @returns the new Vector3
  1119. */
  1120. public clone(): Vector3 {
  1121. return new Vector3(this.x, this.y, this.z);
  1122. }
  1123. /**
  1124. * Copies the given vector coordinates to the current Vector3 ones
  1125. * @param source defines the source Vector3
  1126. * @returns the current updated Vector3
  1127. */
  1128. public copyFrom(source: DeepImmutable<Vector3>): Vector3 {
  1129. return this.copyFromFloats(source.x, source.y, source.z);
  1130. }
  1131. /**
  1132. * Copies the given floats to the current Vector3 coordinates
  1133. * @param x defines the x coordinate of the operand
  1134. * @param y defines the y coordinate of the operand
  1135. * @param z defines the z coordinate of the operand
  1136. * @returns the current updated Vector3
  1137. */
  1138. public copyFromFloats(x: number, y: number, z: number): Vector3 {
  1139. this.x = x;
  1140. this.y = y;
  1141. this.z = z;
  1142. return this;
  1143. }
  1144. /**
  1145. * Copies the given floats to the current Vector3 coordinates
  1146. * @param x defines the x coordinate of the operand
  1147. * @param y defines the y coordinate of the operand
  1148. * @param z defines the z coordinate of the operand
  1149. * @returns the current updated Vector3
  1150. */
  1151. public set(x: number, y: number, z: number): Vector3 {
  1152. return this.copyFromFloats(x, y, z);
  1153. }
  1154. /**
  1155. * Copies the given float to the current Vector3 coordinates
  1156. * @param v defines the x, y and z coordinates of the operand
  1157. * @returns the current updated Vector3
  1158. */
  1159. public setAll(v: number): Vector3 {
  1160. this.x = this.y = this.z = v;
  1161. return this;
  1162. }
  1163. // Statics
  1164. /**
  1165. * Get the clip factor between two vectors
  1166. * @param vector0 defines the first operand
  1167. * @param vector1 defines the second operand
  1168. * @param axis defines the axis to use
  1169. * @param size defines the size along the axis
  1170. * @returns the clip factor
  1171. */
  1172. public static GetClipFactor(vector0: DeepImmutable<Vector3>, vector1: DeepImmutable<Vector3>, axis: DeepImmutable<Vector3>, size: number) {
  1173. var d0 = Vector3.Dot(vector0, axis) - size;
  1174. var d1 = Vector3.Dot(vector1, axis) - size;
  1175. var s = d0 / (d0 - d1);
  1176. return s;
  1177. }
  1178. /**
  1179. * Get angle between two vectors
  1180. * @param vector0 angle between vector0 and vector1
  1181. * @param vector1 angle between vector0 and vector1
  1182. * @param normal direction of the normal
  1183. * @return the angle between vector0 and vector1
  1184. */
  1185. public static GetAngleBetweenVectors(vector0: DeepImmutable<Vector3>, vector1: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>): number {
  1186. const v0: Vector3 = vector0.normalizeToRef(MathTmp.Vector3[1]);
  1187. const v1: Vector3 = vector1.normalizeToRef(MathTmp.Vector3[2]);
  1188. const dot: number = Vector3.Dot(v0, v1);
  1189. const n = MathTmp.Vector3[3];
  1190. Vector3.CrossToRef(v0, v1, n);
  1191. if (Vector3.Dot(n, normal) > 0) {
  1192. return Math.acos(dot);
  1193. }
  1194. return -Math.acos(dot);
  1195. }
  1196. /**
  1197. * Returns a new Vector3 set from the index "offset" of the given array
  1198. * @param array defines the source array
  1199. * @param offset defines the offset in the source array
  1200. * @returns the new Vector3
  1201. */
  1202. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Vector3 {
  1203. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  1204. }
  1205. /**
  1206. * Returns a new Vector3 set from the index "offset" of the given Float32Array
  1207. * @param array defines the source array
  1208. * @param offset defines the offset in the source array
  1209. * @returns the new Vector3
  1210. * @deprecated Please use FromArray instead.
  1211. */
  1212. public static FromFloatArray(array: DeepImmutable<Float32Array>, offset?: number): Vector3 {
  1213. return Vector3.FromArray(array, offset);
  1214. }
  1215. /**
  1216. * Sets the given vector "result" with the element values from the index "offset" of the given array
  1217. * @param array defines the source array
  1218. * @param offset defines the offset in the source array
  1219. * @param result defines the Vector3 where to store the result
  1220. */
  1221. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector3): void {
  1222. result.x = array[offset];
  1223. result.y = array[offset + 1];
  1224. result.z = array[offset + 2];
  1225. }
  1226. /**
  1227. * Sets the given vector "result" with the element values from the index "offset" of the given Float32Array
  1228. * @param array defines the source array
  1229. * @param offset defines the offset in the source array
  1230. * @param result defines the Vector3 where to store the result
  1231. * @deprecated Please use FromArrayToRef instead.
  1232. */
  1233. public static FromFloatArrayToRef(array: DeepImmutable<Float32Array>, offset: number, result: Vector3): void {
  1234. return Vector3.FromArrayToRef(array, offset, result);
  1235. }
  1236. /**
  1237. * Sets the given vector "result" with the given floats.
  1238. * @param x defines the x coordinate of the source
  1239. * @param y defines the y coordinate of the source
  1240. * @param z defines the z coordinate of the source
  1241. * @param result defines the Vector3 where to store the result
  1242. */
  1243. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  1244. result.copyFromFloats(x, y, z);
  1245. }
  1246. /**
  1247. * Returns a new Vector3 set to (0.0, 0.0, 0.0)
  1248. * @returns a new empty Vector3
  1249. */
  1250. public static Zero(): Vector3 {
  1251. return new Vector3(0.0, 0.0, 0.0);
  1252. }
  1253. /**
  1254. * Returns a new Vector3 set to (1.0, 1.0, 1.0)
  1255. * @returns a new unit Vector3
  1256. */
  1257. public static One(): Vector3 {
  1258. return new Vector3(1.0, 1.0, 1.0);
  1259. }
  1260. /**
  1261. * Returns a new Vector3 set to (0.0, 1.0, 0.0)
  1262. * @returns a new up Vector3
  1263. */
  1264. public static Up(): Vector3 {
  1265. return new Vector3(0.0, 1.0, 0.0);
  1266. }
  1267. /**
  1268. * Gets a up Vector3 that must not be updated
  1269. */
  1270. public static get UpReadOnly(): DeepImmutable<Vector3> {
  1271. return Vector3._UpReadOnly;
  1272. }
  1273. /**
  1274. * Gets a zero Vector3 that must not be updated
  1275. */
  1276. public static get ZeroReadOnly(): DeepImmutable<Vector3> {
  1277. return Vector3._ZeroReadOnly;
  1278. }
  1279. /**
  1280. * Returns a new Vector3 set to (0.0, -1.0, 0.0)
  1281. * @returns a new down Vector3
  1282. */
  1283. public static Down(): Vector3 {
  1284. return new Vector3(0.0, -1.0, 0.0);
  1285. }
  1286. /**
  1287. * Returns a new Vector3 set to (0.0, 0.0, 1.0)
  1288. * @param rightHandedSystem is the scene right-handed (negative z)
  1289. * @returns a new forward Vector3
  1290. */
  1291. public static Forward(rightHandedSystem: boolean = false): Vector3 {
  1292. return new Vector3(0.0, 0.0, (rightHandedSystem ? -1.0 : 1.0));
  1293. }
  1294. /**
  1295. * Returns a new Vector3 set to (0.0, 0.0, -1.0)
  1296. * @param rightHandedSystem is the scene right-handed (negative-z)
  1297. * @returns a new forward Vector3
  1298. */
  1299. public static Backward(rightHandedSystem: boolean = false): Vector3 {
  1300. return new Vector3(0.0, 0.0, (rightHandedSystem ? 1.0 : -1.0));
  1301. }
  1302. /**
  1303. * Returns a new Vector3 set to (1.0, 0.0, 0.0)
  1304. * @returns a new right Vector3
  1305. */
  1306. public static Right(): Vector3 {
  1307. return new Vector3(1.0, 0.0, 0.0);
  1308. }
  1309. /**
  1310. * Returns a new Vector3 set to (-1.0, 0.0, 0.0)
  1311. * @returns a new left Vector3
  1312. */
  1313. public static Left(): Vector3 {
  1314. return new Vector3(-1.0, 0.0, 0.0);
  1315. }
  1316. /**
  1317. * Returns a new Vector3 set with the result of the transformation by the given matrix of the given vector.
  1318. * This method computes tranformed coordinates only, not transformed direction vectors (ie. it takes translation in account)
  1319. * @param vector defines the Vector3 to transform
  1320. * @param transformation defines the transformation matrix
  1321. * @returns the transformed Vector3
  1322. */
  1323. public static TransformCoordinates(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>): Vector3 {
  1324. var result = Vector3.Zero();
  1325. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  1326. return result;
  1327. }
  1328. /**
  1329. * Sets the given vector "result" coordinates with the result of the transformation by the given matrix of the given vector
  1330. * This method computes tranformed coordinates only, not transformed direction vectors (ie. it takes translation in account)
  1331. * @param vector defines the Vector3 to transform
  1332. * @param transformation defines the transformation matrix
  1333. * @param result defines the Vector3 where to store the result
  1334. */
  1335. public static TransformCoordinatesToRef(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  1336. Vector3.TransformCoordinatesFromFloatsToRef(vector.x, vector.y, vector.z, transformation, result);
  1337. }
  1338. /**
  1339. * Sets the given vector "result" coordinates with the result of the transformation by the given matrix of the given floats (x, y, z)
  1340. * This method computes tranformed coordinates only, not transformed direction vectors
  1341. * @param x define the x coordinate of the source vector
  1342. * @param y define the y coordinate of the source vector
  1343. * @param z define the z coordinate of the source vector
  1344. * @param transformation defines the transformation matrix
  1345. * @param result defines the Vector3 where to store the result
  1346. */
  1347. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  1348. const m = transformation.m;
  1349. var rx = x * m[0] + y * m[4] + z * m[8] + m[12];
  1350. var ry = x * m[1] + y * m[5] + z * m[9] + m[13];
  1351. var rz = x * m[2] + y * m[6] + z * m[10] + m[14];
  1352. var rw = 1 / (x * m[3] + y * m[7] + z * m[11] + m[15]);
  1353. result.x = rx * rw;
  1354. result.y = ry * rw;
  1355. result.z = rz * rw;
  1356. }
  1357. /**
  1358. * Returns a new Vector3 set with the result of the normal transformation by the given matrix of the given vector
  1359. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  1360. * @param vector defines the Vector3 to transform
  1361. * @param transformation defines the transformation matrix
  1362. * @returns the new Vector3
  1363. */
  1364. public static TransformNormal(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>): Vector3 {
  1365. var result = Vector3.Zero();
  1366. Vector3.TransformNormalToRef(vector, transformation, result);
  1367. return result;
  1368. }
  1369. /**
  1370. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given vector
  1371. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  1372. * @param vector defines the Vector3 to transform
  1373. * @param transformation defines the transformation matrix
  1374. * @param result defines the Vector3 where to store the result
  1375. */
  1376. public static TransformNormalToRef(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  1377. this.TransformNormalFromFloatsToRef(vector.x, vector.y, vector.z, transformation, result);
  1378. }
  1379. /**
  1380. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given floats (x, y, z)
  1381. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  1382. * @param x define the x coordinate of the source vector
  1383. * @param y define the y coordinate of the source vector
  1384. * @param z define the z coordinate of the source vector
  1385. * @param transformation defines the transformation matrix
  1386. * @param result defines the Vector3 where to store the result
  1387. */
  1388. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  1389. const m = transformation.m;
  1390. result.x = x * m[0] + y * m[4] + z * m[8];
  1391. result.y = x * m[1] + y * m[5] + z * m[9];
  1392. result.z = x * m[2] + y * m[6] + z * m[10];
  1393. }
  1394. /**
  1395. * Returns a new Vector3 located for "amount" on the CatmullRom interpolation spline defined by the vectors "value1", "value2", "value3", "value4"
  1396. * @param value1 defines the first control point
  1397. * @param value2 defines the second control point
  1398. * @param value3 defines the third control point
  1399. * @param value4 defines the fourth control point
  1400. * @param amount defines the amount on the spline to use
  1401. * @returns the new Vector3
  1402. */
  1403. public static CatmullRom(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>, value3: DeepImmutable<Vector3>, value4: DeepImmutable<Vector3>, amount: number): Vector3 {
  1404. var squared = amount * amount;
  1405. var cubed = amount * squared;
  1406. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  1407. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  1408. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  1409. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  1410. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  1411. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  1412. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  1413. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  1414. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  1415. return new Vector3(x, y, z);
  1416. }
  1417. /**
  1418. * Returns a new Vector3 set with the coordinates of "value", if the vector "value" is in the cube defined by the vectors "min" and "max"
  1419. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one
  1420. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one
  1421. * @param value defines the current value
  1422. * @param min defines the lower range value
  1423. * @param max defines the upper range value
  1424. * @returns the new Vector3
  1425. */
  1426. public static Clamp(value: DeepImmutable<Vector3>, min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>): Vector3 {
  1427. const v = new Vector3();
  1428. Vector3.ClampToRef(value, min, max, v);
  1429. return v;
  1430. }
  1431. /**
  1432. * Sets the given vector "result" with the coordinates of "value", if the vector "value" is in the cube defined by the vectors "min" and "max"
  1433. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one
  1434. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one
  1435. * @param value defines the current value
  1436. * @param min defines the lower range value
  1437. * @param max defines the upper range value
  1438. * @param result defines the Vector3 where to store the result
  1439. */
  1440. public static ClampToRef(value: DeepImmutable<Vector3>, min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>, result: Vector3): void {
  1441. var x = value.x;
  1442. x = (x > max.x) ? max.x : x;
  1443. x = (x < min.x) ? min.x : x;
  1444. var y = value.y;
  1445. y = (y > max.y) ? max.y : y;
  1446. y = (y < min.y) ? min.y : y;
  1447. var z = value.z;
  1448. z = (z > max.z) ? max.z : z;
  1449. z = (z < min.z) ? min.z : z;
  1450. result.copyFromFloats(x, y, z);
  1451. }
  1452. /**
  1453. * Checks if a given vector is inside a specific range
  1454. * @param v defines the vector to test
  1455. * @param min defines the minimum range
  1456. * @param max defines the maximum range
  1457. */
  1458. public static CheckExtends(v: Vector3, min: Vector3, max: Vector3): void {
  1459. min.minimizeInPlace(v);
  1460. max.maximizeInPlace(v);
  1461. }
  1462. /**
  1463. * Returns a new Vector3 located for "amount" (float) on the Hermite interpolation spline defined by the vectors "value1", "tangent1", "value2", "tangent2"
  1464. * @param value1 defines the first control point
  1465. * @param tangent1 defines the first tangent vector
  1466. * @param value2 defines the second control point
  1467. * @param tangent2 defines the second tangent vector
  1468. * @param amount defines the amount on the interpolation spline (between 0 and 1)
  1469. * @returns the new Vector3
  1470. */
  1471. public static Hermite(value1: DeepImmutable<Vector3>, tangent1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>, tangent2: DeepImmutable<Vector3>, amount: number): Vector3 {
  1472. var squared = amount * amount;
  1473. var cubed = amount * squared;
  1474. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  1475. var part2 = (-2.0 * cubed) + (3.0 * squared);
  1476. var part3 = (cubed - (2.0 * squared)) + amount;
  1477. var part4 = cubed - squared;
  1478. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  1479. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  1480. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  1481. return new Vector3(x, y, z);
  1482. }
  1483. /**
  1484. * Returns a new Vector3 located for "amount" (float) on the linear interpolation between the vectors "start" and "end"
  1485. * @param start defines the start value
  1486. * @param end defines the end value
  1487. * @param amount max defines amount between both (between 0 and 1)
  1488. * @returns the new Vector3
  1489. */
  1490. public static Lerp(start: DeepImmutable<Vector3>, end: DeepImmutable<Vector3>, amount: number): Vector3 {
  1491. var result = new Vector3(0, 0, 0);
  1492. Vector3.LerpToRef(start, end, amount, result);
  1493. return result;
  1494. }
  1495. /**
  1496. * Sets the given vector "result" with the result of the linear interpolation from the vector "start" for "amount" to the vector "end"
  1497. * @param start defines the start value
  1498. * @param end defines the end value
  1499. * @param amount max defines amount between both (between 0 and 1)
  1500. * @param result defines the Vector3 where to store the result
  1501. */
  1502. public static LerpToRef(start: DeepImmutable<Vector3>, end: DeepImmutable<Vector3>, amount: number, result: Vector3): void {
  1503. result.x = start.x + ((end.x - start.x) * amount);
  1504. result.y = start.y + ((end.y - start.y) * amount);
  1505. result.z = start.z + ((end.z - start.z) * amount);
  1506. }
  1507. /**
  1508. * Returns the dot product (float) between the vectors "left" and "right"
  1509. * @param left defines the left operand
  1510. * @param right defines the right operand
  1511. * @returns the dot product
  1512. */
  1513. public static Dot(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): number {
  1514. return (left.x * right.x + left.y * right.y + left.z * right.z);
  1515. }
  1516. /**
  1517. * Returns a new Vector3 as the cross product of the vectors "left" and "right"
  1518. * The cross product is then orthogonal to both "left" and "right"
  1519. * @param left defines the left operand
  1520. * @param right defines the right operand
  1521. * @returns the cross product
  1522. */
  1523. public static Cross(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  1524. var result = Vector3.Zero();
  1525. Vector3.CrossToRef(left, right, result);
  1526. return result;
  1527. }
  1528. /**
  1529. * Sets the given vector "result" with the cross product of "left" and "right"
  1530. * The cross product is then orthogonal to both "left" and "right"
  1531. * @param left defines the left operand
  1532. * @param right defines the right operand
  1533. * @param result defines the Vector3 where to store the result
  1534. */
  1535. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  1536. const x = left.y * right.z - left.z * right.y;
  1537. const y = left.z * right.x - left.x * right.z;
  1538. const z = left.x * right.y - left.y * right.x;
  1539. result.copyFromFloats(x, y, z);
  1540. }
  1541. /**
  1542. * Returns a new Vector3 as the normalization of the given vector
  1543. * @param vector defines the Vector3 to normalize
  1544. * @returns the new Vector3
  1545. */
  1546. public static Normalize(vector: DeepImmutable<Vector3>): Vector3 {
  1547. var result = Vector3.Zero();
  1548. Vector3.NormalizeToRef(vector, result);
  1549. return result;
  1550. }
  1551. /**
  1552. * Sets the given vector "result" with the normalization of the given first vector
  1553. * @param vector defines the Vector3 to normalize
  1554. * @param result defines the Vector3 where to store the result
  1555. */
  1556. public static NormalizeToRef(vector: DeepImmutable<Vector3>, result: Vector3): void {
  1557. vector.normalizeToRef(result);
  1558. }
  1559. /**
  1560. * Project a Vector3 onto screen space
  1561. * @param vector defines the Vector3 to project
  1562. * @param world defines the world matrix to use
  1563. * @param transform defines the transform (view x projection) matrix to use
  1564. * @param viewport defines the screen viewport to use
  1565. * @returns the new Vector3
  1566. */
  1567. public static Project(vector: DeepImmutable<Vector3>, world: DeepImmutable<Matrix>, transform: DeepImmutable<Matrix>, viewport: DeepImmutable<Viewport>): Vector3 {
  1568. var cw = viewport.width;
  1569. var ch = viewport.height;
  1570. var cx = viewport.x;
  1571. var cy = viewport.y;
  1572. var viewportMatrix = MathTmp.Matrix[1];
  1573. Matrix.FromValuesToRef(
  1574. cw / 2.0, 0, 0, 0,
  1575. 0, -ch / 2.0, 0, 0,
  1576. 0, 0, 0.5, 0,
  1577. cx + cw / 2.0, ch / 2.0 + cy, 0.5, 1, viewportMatrix);
  1578. var matrix = MathTmp.Matrix[0];
  1579. world.multiplyToRef(transform, matrix);
  1580. matrix.multiplyToRef(viewportMatrix, matrix);
  1581. return Vector3.TransformCoordinates(vector, matrix);
  1582. }
  1583. /** @hidden */
  1584. public static _UnprojectFromInvertedMatrixToRef(source: DeepImmutable<Vector3>, matrix: DeepImmutable<Matrix>, result: Vector3) {
  1585. Vector3.TransformCoordinatesToRef(source, matrix, result);
  1586. const m = matrix.m;
  1587. var num = source.x * m[3] + source.y * m[7] + source.z * m[11] + m[15];
  1588. if (Scalar.WithinEpsilon(num, 1.0)) {
  1589. result.scaleInPlace(1.0 / num);
  1590. }
  1591. }
  1592. /**
  1593. * Unproject from screen space to object space
  1594. * @param source defines the screen space Vector3 to use
  1595. * @param viewportWidth defines the current width of the viewport
  1596. * @param viewportHeight defines the current height of the viewport
  1597. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  1598. * @param transform defines the transform (view x projection) matrix to use
  1599. * @returns the new Vector3
  1600. */
  1601. public static UnprojectFromTransform(source: Vector3, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, transform: DeepImmutable<Matrix>): Vector3 {
  1602. var matrix = MathTmp.Matrix[0];
  1603. world.multiplyToRef(transform, matrix);
  1604. matrix.invert();
  1605. source.x = source.x / viewportWidth * 2 - 1;
  1606. source.y = -(source.y / viewportHeight * 2 - 1);
  1607. const vector = new Vector3();
  1608. Vector3._UnprojectFromInvertedMatrixToRef(source, matrix, vector);
  1609. return vector;
  1610. }
  1611. /**
  1612. * Unproject from screen space to object space
  1613. * @param source defines the screen space Vector3 to use
  1614. * @param viewportWidth defines the current width of the viewport
  1615. * @param viewportHeight defines the current height of the viewport
  1616. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  1617. * @param view defines the view matrix to use
  1618. * @param projection defines the projection matrix to use
  1619. * @returns the new Vector3
  1620. */
  1621. public static Unproject(source: DeepImmutable<Vector3>, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>): Vector3 {
  1622. let result = Vector3.Zero();
  1623. Vector3.UnprojectToRef(source, viewportWidth, viewportHeight, world, view, projection, result);
  1624. return result;
  1625. }
  1626. /**
  1627. * Unproject from screen space to object space
  1628. * @param source defines the screen space Vector3 to use
  1629. * @param viewportWidth defines the current width of the viewport
  1630. * @param viewportHeight defines the current height of the viewport
  1631. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  1632. * @param view defines the view matrix to use
  1633. * @param projection defines the projection matrix to use
  1634. * @param result defines the Vector3 where to store the result
  1635. */
  1636. public static UnprojectToRef(source: DeepImmutable<Vector3>, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, result: Vector3): void {
  1637. Vector3.UnprojectFloatsToRef(source.x, source.y, source.z, viewportWidth, viewportHeight, world, view, projection, result);
  1638. }
  1639. /**
  1640. * Unproject from screen space to object space
  1641. * @param sourceX defines the screen space x coordinate to use
  1642. * @param sourceY defines the screen space y coordinate to use
  1643. * @param sourceZ defines the screen space z coordinate to use
  1644. * @param viewportWidth defines the current width of the viewport
  1645. * @param viewportHeight defines the current height of the viewport
  1646. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  1647. * @param view defines the view matrix to use
  1648. * @param projection defines the projection matrix to use
  1649. * @param result defines the Vector3 where to store the result
  1650. */
  1651. public static UnprojectFloatsToRef(sourceX: float, sourceY: float, sourceZ: float, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, result: Vector3): void {
  1652. var matrix = MathTmp.Matrix[0];
  1653. world.multiplyToRef(view, matrix);
  1654. matrix.multiplyToRef(projection, matrix);
  1655. matrix.invert();
  1656. var screenSource = MathTmp.Vector3[0];
  1657. screenSource.x = sourceX / viewportWidth * 2 - 1;
  1658. screenSource.y = -(sourceY / viewportHeight * 2 - 1);
  1659. screenSource.z = 2 * sourceZ - 1.0;
  1660. Vector3._UnprojectFromInvertedMatrixToRef(screenSource, matrix, result);
  1661. }
  1662. /**
  1663. * Gets the minimal coordinate values between two Vector3
  1664. * @param left defines the first operand
  1665. * @param right defines the second operand
  1666. * @returns the new Vector3
  1667. */
  1668. public static Minimize(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  1669. var min = left.clone();
  1670. min.minimizeInPlace(right);
  1671. return min;
  1672. }
  1673. /**
  1674. * Gets the maximal coordinate values between two Vector3
  1675. * @param left defines the first operand
  1676. * @param right defines the second operand
  1677. * @returns the new Vector3
  1678. */
  1679. public static Maximize(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  1680. var max = left.clone();
  1681. max.maximizeInPlace(right);
  1682. return max;
  1683. }
  1684. /**
  1685. * Returns the distance between the vectors "value1" and "value2"
  1686. * @param value1 defines the first operand
  1687. * @param value2 defines the second operand
  1688. * @returns the distance
  1689. */
  1690. public static Distance(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): number {
  1691. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  1692. }
  1693. /**
  1694. * Returns the squared distance between the vectors "value1" and "value2"
  1695. * @param value1 defines the first operand
  1696. * @param value2 defines the second operand
  1697. * @returns the squared distance
  1698. */
  1699. public static DistanceSquared(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): number {
  1700. var x = value1.x - value2.x;
  1701. var y = value1.y - value2.y;
  1702. var z = value1.z - value2.z;
  1703. return (x * x) + (y * y) + (z * z);
  1704. }
  1705. /**
  1706. * Returns a new Vector3 located at the center between "value1" and "value2"
  1707. * @param value1 defines the first operand
  1708. * @param value2 defines the second operand
  1709. * @returns the new Vector3
  1710. */
  1711. public static Center(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): Vector3 {
  1712. var center = value1.add(value2);
  1713. center.scaleInPlace(0.5);
  1714. return center;
  1715. }
  1716. /**
  1717. * Given three orthogonal normalized left-handed oriented Vector3 axis in space (target system),
  1718. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  1719. * to something in order to rotate it from its local system to the given target system
  1720. * Note: axis1, axis2 and axis3 are normalized during this operation
  1721. * @param axis1 defines the first axis
  1722. * @param axis2 defines the second axis
  1723. * @param axis3 defines the third axis
  1724. * @returns a new Vector3
  1725. */
  1726. public static RotationFromAxis(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>): Vector3 {
  1727. var rotation = Vector3.Zero();
  1728. Vector3.RotationFromAxisToRef(axis1, axis2, axis3, rotation);
  1729. return rotation;
  1730. }
  1731. /**
  1732. * The same than RotationFromAxis but updates the given ref Vector3 parameter instead of returning a new Vector3
  1733. * @param axis1 defines the first axis
  1734. * @param axis2 defines the second axis
  1735. * @param axis3 defines the third axis
  1736. * @param ref defines the Vector3 where to store the result
  1737. */
  1738. public static RotationFromAxisToRef(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>, ref: Vector3): void {
  1739. var quat = MathTmp.Quaternion[0];
  1740. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  1741. quat.toEulerAnglesToRef(ref);
  1742. }
  1743. }
  1744. /**
  1745. * Vector4 class created for EulerAngle class conversion to Quaternion
  1746. */
  1747. export class Vector4 {
  1748. /**
  1749. * Creates a Vector4 object from the given floats.
  1750. * @param x x value of the vector
  1751. * @param y y value of the vector
  1752. * @param z z value of the vector
  1753. * @param w w value of the vector
  1754. */
  1755. constructor(
  1756. /** x value of the vector */
  1757. public x: number,
  1758. /** y value of the vector */
  1759. public y: number,
  1760. /** z value of the vector */
  1761. public z: number,
  1762. /** w value of the vector */
  1763. public w: number
  1764. ) { }
  1765. /**
  1766. * Returns the string with the Vector4 coordinates.
  1767. * @returns a string containing all the vector values
  1768. */
  1769. public toString(): string {
  1770. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1771. }
  1772. /**
  1773. * Returns the string "Vector4".
  1774. * @returns "Vector4"
  1775. */
  1776. public getClassName(): string {
  1777. return "Vector4";
  1778. }
  1779. /**
  1780. * Returns the Vector4 hash code.
  1781. * @returns a unique hash code
  1782. */
  1783. public getHashCode(): number {
  1784. let hash = this.x | 0;
  1785. hash = (hash * 397) ^ (this.y | 0);
  1786. hash = (hash * 397) ^ (this.z | 0);
  1787. hash = (hash * 397) ^ (this.w | 0);
  1788. return hash;
  1789. }
  1790. // Operators
  1791. /**
  1792. * Returns a new array populated with 4 elements : the Vector4 coordinates.
  1793. * @returns the resulting array
  1794. */
  1795. public asArray(): number[] {
  1796. var result = new Array<number>();
  1797. this.toArray(result, 0);
  1798. return result;
  1799. }
  1800. /**
  1801. * Populates the given array from the given index with the Vector4 coordinates.
  1802. * @param array array to populate
  1803. * @param index index of the array to start at (default: 0)
  1804. * @returns the Vector4.
  1805. */
  1806. public toArray(array: FloatArray, index?: number): Vector4 {
  1807. if (index === undefined) {
  1808. index = 0;
  1809. }
  1810. array[index] = this.x;
  1811. array[index + 1] = this.y;
  1812. array[index + 2] = this.z;
  1813. array[index + 3] = this.w;
  1814. return this;
  1815. }
  1816. /**
  1817. * Adds the given vector to the current Vector4.
  1818. * @param otherVector the vector to add
  1819. * @returns the updated Vector4.
  1820. */
  1821. public addInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  1822. this.x += otherVector.x;
  1823. this.y += otherVector.y;
  1824. this.z += otherVector.z;
  1825. this.w += otherVector.w;
  1826. return this;
  1827. }
  1828. /**
  1829. * Returns a new Vector4 as the result of the addition of the current Vector4 and the given one.
  1830. * @param otherVector the vector to add
  1831. * @returns the resulting vector
  1832. */
  1833. public add(otherVector: DeepImmutable<Vector4>): Vector4 {
  1834. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  1835. }
  1836. /**
  1837. * Updates the given vector "result" with the result of the addition of the current Vector4 and the given one.
  1838. * @param otherVector the vector to add
  1839. * @param result the vector to store the result
  1840. * @returns the current Vector4.
  1841. */
  1842. public addToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  1843. result.x = this.x + otherVector.x;
  1844. result.y = this.y + otherVector.y;
  1845. result.z = this.z + otherVector.z;
  1846. result.w = this.w + otherVector.w;
  1847. return this;
  1848. }
  1849. /**
  1850. * Subtract in place the given vector from the current Vector4.
  1851. * @param otherVector the vector to subtract
  1852. * @returns the updated Vector4.
  1853. */
  1854. public subtractInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  1855. this.x -= otherVector.x;
  1856. this.y -= otherVector.y;
  1857. this.z -= otherVector.z;
  1858. this.w -= otherVector.w;
  1859. return this;
  1860. }
  1861. /**
  1862. * Returns a new Vector4 with the result of the subtraction of the given vector from the current Vector4.
  1863. * @param otherVector the vector to add
  1864. * @returns the new vector with the result
  1865. */
  1866. public subtract(otherVector: DeepImmutable<Vector4>): Vector4 {
  1867. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  1868. }
  1869. /**
  1870. * Sets the given vector "result" with the result of the subtraction of the given vector from the current Vector4.
  1871. * @param otherVector the vector to subtract
  1872. * @param result the vector to store the result
  1873. * @returns the current Vector4.
  1874. */
  1875. public subtractToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  1876. result.x = this.x - otherVector.x;
  1877. result.y = this.y - otherVector.y;
  1878. result.z = this.z - otherVector.z;
  1879. result.w = this.w - otherVector.w;
  1880. return this;
  1881. }
  1882. /**
  1883. * Returns a new Vector4 set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  1884. */
  1885. /**
  1886. * Returns a new Vector4 set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  1887. * @param x value to subtract
  1888. * @param y value to subtract
  1889. * @param z value to subtract
  1890. * @param w value to subtract
  1891. * @returns new vector containing the result
  1892. */
  1893. public subtractFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  1894. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  1895. }
  1896. /**
  1897. * Sets the given vector "result" set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  1898. * @param x value to subtract
  1899. * @param y value to subtract
  1900. * @param z value to subtract
  1901. * @param w value to subtract
  1902. * @param result the vector to store the result in
  1903. * @returns the current Vector4.
  1904. */
  1905. public subtractFromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): Vector4 {
  1906. result.x = this.x - x;
  1907. result.y = this.y - y;
  1908. result.z = this.z - z;
  1909. result.w = this.w - w;
  1910. return this;
  1911. }
  1912. /**
  1913. * Returns a new Vector4 set with the current Vector4 negated coordinates.
  1914. * @returns a new vector with the negated values
  1915. */
  1916. public negate(): Vector4 {
  1917. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  1918. }
  1919. /**
  1920. * Negate this vector in place
  1921. * @returns this
  1922. */
  1923. public negateInPlace(): Vector4 {
  1924. this.x *= -1;
  1925. this.y *= -1;
  1926. this.z *= -1;
  1927. this.w *= -1;
  1928. return this;
  1929. }
  1930. /**
  1931. * Negate the current Vector4 and stores the result in the given vector "result" coordinates
  1932. * @param result defines the Vector3 object where to store the result
  1933. * @returns the current Vector4
  1934. */
  1935. public negateToRef(result: Vector4): Vector4 {
  1936. return result.copyFromFloats(this.x * -1, this.y * -1, this.z * -1, this.w * -1);
  1937. }
  1938. /**
  1939. * Multiplies the current Vector4 coordinates by scale (float).
  1940. * @param scale the number to scale with
  1941. * @returns the updated Vector4.
  1942. */
  1943. public scaleInPlace(scale: number): Vector4 {
  1944. this.x *= scale;
  1945. this.y *= scale;
  1946. this.z *= scale;
  1947. this.w *= scale;
  1948. return this;
  1949. }
  1950. /**
  1951. * Returns a new Vector4 set with the current Vector4 coordinates multiplied by scale (float).
  1952. * @param scale the number to scale with
  1953. * @returns a new vector with the result
  1954. */
  1955. public scale(scale: number): Vector4 {
  1956. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  1957. }
  1958. /**
  1959. * Sets the given vector "result" with the current Vector4 coordinates multiplied by scale (float).
  1960. * @param scale the number to scale with
  1961. * @param result a vector to store the result in
  1962. * @returns the current Vector4.
  1963. */
  1964. public scaleToRef(scale: number, result: Vector4): Vector4 {
  1965. result.x = this.x * scale;
  1966. result.y = this.y * scale;
  1967. result.z = this.z * scale;
  1968. result.w = this.w * scale;
  1969. return this;
  1970. }
  1971. /**
  1972. * Scale the current Vector4 values by a factor and add the result to a given Vector4
  1973. * @param scale defines the scale factor
  1974. * @param result defines the Vector4 object where to store the result
  1975. * @returns the unmodified current Vector4
  1976. */
  1977. public scaleAndAddToRef(scale: number, result: Vector4): Vector4 {
  1978. result.x += this.x * scale;
  1979. result.y += this.y * scale;
  1980. result.z += this.z * scale;
  1981. result.w += this.w * scale;
  1982. return this;
  1983. }
  1984. /**
  1985. * Boolean : True if the current Vector4 coordinates are stricly equal to the given ones.
  1986. * @param otherVector the vector to compare against
  1987. * @returns true if they are equal
  1988. */
  1989. public equals(otherVector: DeepImmutable<Vector4>): boolean {
  1990. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  1991. }
  1992. /**
  1993. * Boolean : True if the current Vector4 coordinates are each beneath the distance "epsilon" from the given vector ones.
  1994. * @param otherVector vector to compare against
  1995. * @param epsilon (Default: very small number)
  1996. * @returns true if they are equal
  1997. */
  1998. public equalsWithEpsilon(otherVector: DeepImmutable<Vector4>, epsilon: number = Epsilon): boolean {
  1999. return otherVector
  2000. && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon)
  2001. && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon)
  2002. && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon)
  2003. && Scalar.WithinEpsilon(this.w, otherVector.w, epsilon);
  2004. }
  2005. /**
  2006. * Boolean : True if the given floats are strictly equal to the current Vector4 coordinates.
  2007. * @param x x value to compare against
  2008. * @param y y value to compare against
  2009. * @param z z value to compare against
  2010. * @param w w value to compare against
  2011. * @returns true if equal
  2012. */
  2013. public equalsToFloats(x: number, y: number, z: number, w: number): boolean {
  2014. return this.x === x && this.y === y && this.z === z && this.w === w;
  2015. }
  2016. /**
  2017. * Multiplies in place the current Vector4 by the given one.
  2018. * @param otherVector vector to multiple with
  2019. * @returns the updated Vector4.
  2020. */
  2021. public multiplyInPlace(otherVector: Vector4): Vector4 {
  2022. this.x *= otherVector.x;
  2023. this.y *= otherVector.y;
  2024. this.z *= otherVector.z;
  2025. this.w *= otherVector.w;
  2026. return this;
  2027. }
  2028. /**
  2029. * Returns a new Vector4 set with the multiplication result of the current Vector4 and the given one.
  2030. * @param otherVector vector to multiple with
  2031. * @returns resulting new vector
  2032. */
  2033. public multiply(otherVector: DeepImmutable<Vector4>): Vector4 {
  2034. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  2035. }
  2036. /**
  2037. * Updates the given vector "result" with the multiplication result of the current Vector4 and the given one.
  2038. * @param otherVector vector to multiple with
  2039. * @param result vector to store the result
  2040. * @returns the current Vector4.
  2041. */
  2042. public multiplyToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2043. result.x = this.x * otherVector.x;
  2044. result.y = this.y * otherVector.y;
  2045. result.z = this.z * otherVector.z;
  2046. result.w = this.w * otherVector.w;
  2047. return this;
  2048. }
  2049. /**
  2050. * Returns a new Vector4 set with the multiplication result of the given floats and the current Vector4 coordinates.
  2051. * @param x x value multiply with
  2052. * @param y y value multiply with
  2053. * @param z z value multiply with
  2054. * @param w w value multiply with
  2055. * @returns resulting new vector
  2056. */
  2057. public multiplyByFloats(x: number, y: number, z: number, w: number): Vector4 {
  2058. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  2059. }
  2060. /**
  2061. * Returns a new Vector4 set with the division result of the current Vector4 by the given one.
  2062. * @param otherVector vector to devide with
  2063. * @returns resulting new vector
  2064. */
  2065. public divide(otherVector: DeepImmutable<Vector4>): Vector4 {
  2066. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  2067. }
  2068. /**
  2069. * Updates the given vector "result" with the division result of the current Vector4 by the given one.
  2070. * @param otherVector vector to devide with
  2071. * @param result vector to store the result
  2072. * @returns the current Vector4.
  2073. */
  2074. public divideToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2075. result.x = this.x / otherVector.x;
  2076. result.y = this.y / otherVector.y;
  2077. result.z = this.z / otherVector.z;
  2078. result.w = this.w / otherVector.w;
  2079. return this;
  2080. }
  2081. /**
  2082. * Divides the current Vector3 coordinates by the given ones.
  2083. * @param otherVector vector to devide with
  2084. * @returns the updated Vector3.
  2085. */
  2086. public divideInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  2087. return this.divideToRef(otherVector, this);
  2088. }
  2089. /**
  2090. * Updates the Vector4 coordinates with the minimum values between its own and the given vector ones
  2091. * @param other defines the second operand
  2092. * @returns the current updated Vector4
  2093. */
  2094. public minimizeInPlace(other: DeepImmutable<Vector4>): Vector4 {
  2095. if (other.x < this.x) { this.x = other.x; }
  2096. if (other.y < this.y) { this.y = other.y; }
  2097. if (other.z < this.z) { this.z = other.z; }
  2098. if (other.w < this.w) { this.w = other.w; }
  2099. return this;
  2100. }
  2101. /**
  2102. * Updates the Vector4 coordinates with the maximum values between its own and the given vector ones
  2103. * @param other defines the second operand
  2104. * @returns the current updated Vector4
  2105. */
  2106. public maximizeInPlace(other: DeepImmutable<Vector4>): Vector4 {
  2107. if (other.x > this.x) { this.x = other.x; }
  2108. if (other.y > this.y) { this.y = other.y; }
  2109. if (other.z > this.z) { this.z = other.z; }
  2110. if (other.w > this.w) { this.w = other.w; }
  2111. return this;
  2112. }
  2113. /**
  2114. * Gets a new Vector4 from current Vector4 floored values
  2115. * @returns a new Vector4
  2116. */
  2117. public floor(): Vector4 {
  2118. return new Vector4(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z), Math.floor(this.w));
  2119. }
  2120. /**
  2121. * Gets a new Vector4 from current Vector3 floored values
  2122. * @returns a new Vector4
  2123. */
  2124. public fract(): Vector4 {
  2125. return new Vector4(this.x - Math.floor(this.x), this.y - Math.floor(this.y), this.z - Math.floor(this.z), this.w - Math.floor(this.w));
  2126. }
  2127. // Properties
  2128. /**
  2129. * Returns the Vector4 length (float).
  2130. * @returns the length
  2131. */
  2132. public length(): number {
  2133. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  2134. }
  2135. /**
  2136. * Returns the Vector4 squared length (float).
  2137. * @returns the length squared
  2138. */
  2139. public lengthSquared(): number {
  2140. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  2141. }
  2142. // Methods
  2143. /**
  2144. * Normalizes in place the Vector4.
  2145. * @returns the updated Vector4.
  2146. */
  2147. public normalize(): Vector4 {
  2148. var len = this.length();
  2149. if (len === 0) {
  2150. return this;
  2151. }
  2152. return this.scaleInPlace(1.0 / len);
  2153. }
  2154. /**
  2155. * Returns a new Vector3 from the Vector4 (x, y, z) coordinates.
  2156. * @returns this converted to a new vector3
  2157. */
  2158. public toVector3(): Vector3 {
  2159. return new Vector3(this.x, this.y, this.z);
  2160. }
  2161. /**
  2162. * Returns a new Vector4 copied from the current one.
  2163. * @returns the new cloned vector
  2164. */
  2165. public clone(): Vector4 {
  2166. return new Vector4(this.x, this.y, this.z, this.w);
  2167. }
  2168. /**
  2169. * Updates the current Vector4 with the given one coordinates.
  2170. * @param source the source vector to copy from
  2171. * @returns the updated Vector4.
  2172. */
  2173. public copyFrom(source: DeepImmutable<Vector4>): Vector4 {
  2174. this.x = source.x;
  2175. this.y = source.y;
  2176. this.z = source.z;
  2177. this.w = source.w;
  2178. return this;
  2179. }
  2180. /**
  2181. * Updates the current Vector4 coordinates with the given floats.
  2182. * @param x float to copy from
  2183. * @param y float to copy from
  2184. * @param z float to copy from
  2185. * @param w float to copy from
  2186. * @returns the updated Vector4.
  2187. */
  2188. public copyFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  2189. this.x = x;
  2190. this.y = y;
  2191. this.z = z;
  2192. this.w = w;
  2193. return this;
  2194. }
  2195. /**
  2196. * Updates the current Vector4 coordinates with the given floats.
  2197. * @param x float to set from
  2198. * @param y float to set from
  2199. * @param z float to set from
  2200. * @param w float to set from
  2201. * @returns the updated Vector4.
  2202. */
  2203. public set(x: number, y: number, z: number, w: number): Vector4 {
  2204. return this.copyFromFloats(x, y, z, w);
  2205. }
  2206. /**
  2207. * Copies the given float to the current Vector3 coordinates
  2208. * @param v defines the x, y, z and w coordinates of the operand
  2209. * @returns the current updated Vector3
  2210. */
  2211. public setAll(v: number): Vector4 {
  2212. this.x = this.y = this.z = this.w = v;
  2213. return this;
  2214. }
  2215. // Statics
  2216. /**
  2217. * Returns a new Vector4 set from the starting index of the given array.
  2218. * @param array the array to pull values from
  2219. * @param offset the offset into the array to start at
  2220. * @returns the new vector
  2221. */
  2222. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset?: number): Vector4 {
  2223. if (!offset) {
  2224. offset = 0;
  2225. }
  2226. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  2227. }
  2228. /**
  2229. * Updates the given vector "result" from the starting index of the given array.
  2230. * @param array the array to pull values from
  2231. * @param offset the offset into the array to start at
  2232. * @param result the vector to store the result in
  2233. */
  2234. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector4): void {
  2235. result.x = array[offset];
  2236. result.y = array[offset + 1];
  2237. result.z = array[offset + 2];
  2238. result.w = array[offset + 3];
  2239. }
  2240. /**
  2241. * Updates the given vector "result" from the starting index of the given Float32Array.
  2242. * @param array the array to pull values from
  2243. * @param offset the offset into the array to start at
  2244. * @param result the vector to store the result in
  2245. */
  2246. public static FromFloatArrayToRef(array: DeepImmutable<Float32Array>, offset: number, result: Vector4): void {
  2247. Vector4.FromArrayToRef(array, offset, result);
  2248. }
  2249. /**
  2250. * Updates the given vector "result" coordinates from the given floats.
  2251. * @param x float to set from
  2252. * @param y float to set from
  2253. * @param z float to set from
  2254. * @param w float to set from
  2255. * @param result the vector to the floats in
  2256. */
  2257. public static FromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): void {
  2258. result.x = x;
  2259. result.y = y;
  2260. result.z = z;
  2261. result.w = w;
  2262. }
  2263. /**
  2264. * Returns a new Vector4 set to (0.0, 0.0, 0.0, 0.0)
  2265. * @returns the new vector
  2266. */
  2267. public static Zero(): Vector4 {
  2268. return new Vector4(0.0, 0.0, 0.0, 0.0);
  2269. }
  2270. /**
  2271. * Returns a new Vector4 set to (1.0, 1.0, 1.0, 1.0)
  2272. * @returns the new vector
  2273. */
  2274. public static One(): Vector4 {
  2275. return new Vector4(1.0, 1.0, 1.0, 1.0);
  2276. }
  2277. /**
  2278. * Returns a new normalized Vector4 from the given one.
  2279. * @param vector the vector to normalize
  2280. * @returns the vector
  2281. */
  2282. public static Normalize(vector: DeepImmutable<Vector4>): Vector4 {
  2283. var result = Vector4.Zero();
  2284. Vector4.NormalizeToRef(vector, result);
  2285. return result;
  2286. }
  2287. /**
  2288. * Updates the given vector "result" from the normalization of the given one.
  2289. * @param vector the vector to normalize
  2290. * @param result the vector to store the result in
  2291. */
  2292. public static NormalizeToRef(vector: DeepImmutable<Vector4>, result: Vector4): void {
  2293. result.copyFrom(vector);
  2294. result.normalize();
  2295. }
  2296. /**
  2297. * Returns a vector with the minimum values from the left and right vectors
  2298. * @param left left vector to minimize
  2299. * @param right right vector to minimize
  2300. * @returns a new vector with the minimum of the left and right vector values
  2301. */
  2302. public static Minimize(left: DeepImmutable<Vector4>, right: DeepImmutable<Vector4>): Vector4 {
  2303. var min = left.clone();
  2304. min.minimizeInPlace(right);
  2305. return min;
  2306. }
  2307. /**
  2308. * Returns a vector with the maximum values from the left and right vectors
  2309. * @param left left vector to maximize
  2310. * @param right right vector to maximize
  2311. * @returns a new vector with the maximum of the left and right vector values
  2312. */
  2313. public static Maximize(left: DeepImmutable<Vector4>, right: DeepImmutable<Vector4>): Vector4 {
  2314. var max = left.clone();
  2315. max.maximizeInPlace(right);
  2316. return max;
  2317. }
  2318. /**
  2319. * Returns the distance (float) between the vectors "value1" and "value2".
  2320. * @param value1 value to calulate the distance between
  2321. * @param value2 value to calulate the distance between
  2322. * @return the distance between the two vectors
  2323. */
  2324. public static Distance(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): number {
  2325. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  2326. }
  2327. /**
  2328. * Returns the squared distance (float) between the vectors "value1" and "value2".
  2329. * @param value1 value to calulate the distance between
  2330. * @param value2 value to calulate the distance between
  2331. * @return the distance between the two vectors squared
  2332. */
  2333. public static DistanceSquared(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): number {
  2334. var x = value1.x - value2.x;
  2335. var y = value1.y - value2.y;
  2336. var z = value1.z - value2.z;
  2337. var w = value1.w - value2.w;
  2338. return (x * x) + (y * y) + (z * z) + (w * w);
  2339. }
  2340. /**
  2341. * Returns a new Vector4 located at the center between the vectors "value1" and "value2".
  2342. * @param value1 value to calulate the center between
  2343. * @param value2 value to calulate the center between
  2344. * @return the center between the two vectors
  2345. */
  2346. public static Center(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): Vector4 {
  2347. var center = value1.add(value2);
  2348. center.scaleInPlace(0.5);
  2349. return center;
  2350. }
  2351. /**
  2352. * Returns a new Vector4 set with the result of the normal transformation by the given matrix of the given vector.
  2353. * This methods computes transformed normalized direction vectors only.
  2354. * @param vector the vector to transform
  2355. * @param transformation the transformation matrix to apply
  2356. * @returns the new vector
  2357. */
  2358. public static TransformNormal(vector: DeepImmutable<Vector4>, transformation: DeepImmutable<Matrix>): Vector4 {
  2359. var result = Vector4.Zero();
  2360. Vector4.TransformNormalToRef(vector, transformation, result);
  2361. return result;
  2362. }
  2363. /**
  2364. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given vector.
  2365. * This methods computes transformed normalized direction vectors only.
  2366. * @param vector the vector to transform
  2367. * @param transformation the transformation matrix to apply
  2368. * @param result the vector to store the result in
  2369. */
  2370. public static TransformNormalToRef(vector: DeepImmutable<Vector4>, transformation: DeepImmutable<Matrix>, result: Vector4): void {
  2371. const m = transformation.m;
  2372. var x = (vector.x * m[0]) + (vector.y * m[4]) + (vector.z * m[8]);
  2373. var y = (vector.x * m[1]) + (vector.y * m[5]) + (vector.z * m[9]);
  2374. var z = (vector.x * m[2]) + (vector.y * m[6]) + (vector.z * m[10]);
  2375. result.x = x;
  2376. result.y = y;
  2377. result.z = z;
  2378. result.w = vector.w;
  2379. }
  2380. /**
  2381. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given floats (x, y, z, w).
  2382. * This methods computes transformed normalized direction vectors only.
  2383. * @param x value to transform
  2384. * @param y value to transform
  2385. * @param z value to transform
  2386. * @param w value to transform
  2387. * @param transformation the transformation matrix to apply
  2388. * @param result the vector to store the results in
  2389. */
  2390. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, w: number, transformation: DeepImmutable<Matrix>, result: Vector4): void {
  2391. const m = transformation.m;
  2392. result.x = (x * m[0]) + (y * m[4]) + (z * m[8]);
  2393. result.y = (x * m[1]) + (y * m[5]) + (z * m[9]);
  2394. result.z = (x * m[2]) + (y * m[6]) + (z * m[10]);
  2395. result.w = w;
  2396. }
  2397. /**
  2398. * Creates a new Vector4 from a Vector3
  2399. * @param source defines the source data
  2400. * @param w defines the 4th component (default is 0)
  2401. * @returns a new Vector4
  2402. */
  2403. public static FromVector3(source: Vector3, w: number = 0) {
  2404. return new Vector4(source.x, source.y, source.z, w);
  2405. }
  2406. }
  2407. /**
  2408. * Class used to store quaternion data
  2409. * @see https://en.wikipedia.org/wiki/Quaternion
  2410. * @see http://doc.babylonjs.com/features/position,_rotation,_scaling
  2411. */
  2412. export class Quaternion {
  2413. /**
  2414. * Creates a new Quaternion from the given floats
  2415. * @param x defines the first component (0 by default)
  2416. * @param y defines the second component (0 by default)
  2417. * @param z defines the third component (0 by default)
  2418. * @param w defines the fourth component (1.0 by default)
  2419. */
  2420. constructor(
  2421. /** defines the first component (0 by default) */
  2422. public x: number = 0.0,
  2423. /** defines the second component (0 by default) */
  2424. public y: number = 0.0,
  2425. /** defines the third component (0 by default) */
  2426. public z: number = 0.0,
  2427. /** defines the fourth component (1.0 by default) */
  2428. public w: number = 1.0) {
  2429. }
  2430. /**
  2431. * Gets a string representation for the current quaternion
  2432. * @returns a string with the Quaternion coordinates
  2433. */
  2434. public toString(): string {
  2435. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  2436. }
  2437. /**
  2438. * Gets the class name of the quaternion
  2439. * @returns the string "Quaternion"
  2440. */
  2441. public getClassName(): string {
  2442. return "Quaternion";
  2443. }
  2444. /**
  2445. * Gets a hash code for this quaternion
  2446. * @returns the quaternion hash code
  2447. */
  2448. public getHashCode(): number {
  2449. let hash = this.x | 0;
  2450. hash = (hash * 397) ^ (this.y | 0);
  2451. hash = (hash * 397) ^ (this.z | 0);
  2452. hash = (hash * 397) ^ (this.w | 0);
  2453. return hash;
  2454. }
  2455. /**
  2456. * Copy the quaternion to an array
  2457. * @returns a new array populated with 4 elements from the quaternion coordinates
  2458. */
  2459. public asArray(): number[] {
  2460. return [this.x, this.y, this.z, this.w];
  2461. }
  2462. /**
  2463. * Check if two quaternions are equals
  2464. * @param otherQuaternion defines the second operand
  2465. * @return true if the current quaternion and the given one coordinates are strictly equals
  2466. */
  2467. public equals(otherQuaternion: DeepImmutable<Quaternion>): boolean {
  2468. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  2469. }
  2470. /**
  2471. * Gets a boolean if two quaternions are equals (using an epsilon value)
  2472. * @param otherQuaternion defines the other quaternion
  2473. * @param epsilon defines the minimal distance to consider equality
  2474. * @returns true if the given quaternion coordinates are close to the current ones by a distance of epsilon.
  2475. */
  2476. public equalsWithEpsilon(otherQuaternion: DeepImmutable<Quaternion>, epsilon: number = Epsilon): boolean {
  2477. return otherQuaternion
  2478. && Scalar.WithinEpsilon(this.x, otherQuaternion.x, epsilon)
  2479. && Scalar.WithinEpsilon(this.y, otherQuaternion.y, epsilon)
  2480. && Scalar.WithinEpsilon(this.z, otherQuaternion.z, epsilon)
  2481. && Scalar.WithinEpsilon(this.w, otherQuaternion.w, epsilon);
  2482. }
  2483. /**
  2484. * Clone the current quaternion
  2485. * @returns a new quaternion copied from the current one
  2486. */
  2487. public clone(): Quaternion {
  2488. return new Quaternion(this.x, this.y, this.z, this.w);
  2489. }
  2490. /**
  2491. * Copy a quaternion to the current one
  2492. * @param other defines the other quaternion
  2493. * @returns the updated current quaternion
  2494. */
  2495. public copyFrom(other: DeepImmutable<Quaternion>): Quaternion {
  2496. this.x = other.x;
  2497. this.y = other.y;
  2498. this.z = other.z;
  2499. this.w = other.w;
  2500. return this;
  2501. }
  2502. /**
  2503. * Updates the current quaternion with the given float coordinates
  2504. * @param x defines the x coordinate
  2505. * @param y defines the y coordinate
  2506. * @param z defines the z coordinate
  2507. * @param w defines the w coordinate
  2508. * @returns the updated current quaternion
  2509. */
  2510. public copyFromFloats(x: number, y: number, z: number, w: number): Quaternion {
  2511. this.x = x;
  2512. this.y = y;
  2513. this.z = z;
  2514. this.w = w;
  2515. return this;
  2516. }
  2517. /**
  2518. * Updates the current quaternion from the given float coordinates
  2519. * @param x defines the x coordinate
  2520. * @param y defines the y coordinate
  2521. * @param z defines the z coordinate
  2522. * @param w defines the w coordinate
  2523. * @returns the updated current quaternion
  2524. */
  2525. public set(x: number, y: number, z: number, w: number): Quaternion {
  2526. return this.copyFromFloats(x, y, z, w);
  2527. }
  2528. /**
  2529. * Adds two quaternions
  2530. * @param other defines the second operand
  2531. * @returns a new quaternion as the addition result of the given one and the current quaternion
  2532. */
  2533. public add(other: DeepImmutable<Quaternion>): Quaternion {
  2534. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  2535. }
  2536. /**
  2537. * Add a quaternion to the current one
  2538. * @param other defines the quaternion to add
  2539. * @returns the current quaternion
  2540. */
  2541. public addInPlace(other: DeepImmutable<Quaternion>): Quaternion {
  2542. this.x += other.x;
  2543. this.y += other.y;
  2544. this.z += other.z;
  2545. this.w += other.w;
  2546. return this;
  2547. }
  2548. /**
  2549. * Subtract two quaternions
  2550. * @param other defines the second operand
  2551. * @returns a new quaternion as the subtraction result of the given one from the current one
  2552. */
  2553. public subtract(other: Quaternion): Quaternion {
  2554. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  2555. }
  2556. /**
  2557. * Multiplies the current quaternion by a scale factor
  2558. * @param value defines the scale factor
  2559. * @returns a new quaternion set by multiplying the current quaternion coordinates by the float "scale"
  2560. */
  2561. public scale(value: number): Quaternion {
  2562. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  2563. }
  2564. /**
  2565. * Scale the current quaternion values by a factor and stores the result to a given quaternion
  2566. * @param scale defines the scale factor
  2567. * @param result defines the Quaternion object where to store the result
  2568. * @returns the unmodified current quaternion
  2569. */
  2570. public scaleToRef(scale: number, result: Quaternion): Quaternion {
  2571. result.x = this.x * scale;
  2572. result.y = this.y * scale;
  2573. result.z = this.z * scale;
  2574. result.w = this.w * scale;
  2575. return this;
  2576. }
  2577. /**
  2578. * Multiplies in place the current quaternion by a scale factor
  2579. * @param value defines the scale factor
  2580. * @returns the current modified quaternion
  2581. */
  2582. public scaleInPlace(value: number): Quaternion {
  2583. this.x *= value;
  2584. this.y *= value;
  2585. this.z *= value;
  2586. this.w *= value;
  2587. return this;
  2588. }
  2589. /**
  2590. * Scale the current quaternion values by a factor and add the result to a given quaternion
  2591. * @param scale defines the scale factor
  2592. * @param result defines the Quaternion object where to store the result
  2593. * @returns the unmodified current quaternion
  2594. */
  2595. public scaleAndAddToRef(scale: number, result: Quaternion): Quaternion {
  2596. result.x += this.x * scale;
  2597. result.y += this.y * scale;
  2598. result.z += this.z * scale;
  2599. result.w += this.w * scale;
  2600. return this;
  2601. }
  2602. /**
  2603. * Multiplies two quaternions
  2604. * @param q1 defines the second operand
  2605. * @returns a new quaternion set as the multiplication result of the current one with the given one "q1"
  2606. */
  2607. public multiply(q1: DeepImmutable<Quaternion>): Quaternion {
  2608. var result = new Quaternion(0, 0, 0, 1.0);
  2609. this.multiplyToRef(q1, result);
  2610. return result;
  2611. }
  2612. /**
  2613. * Sets the given "result" as the the multiplication result of the current one with the given one "q1"
  2614. * @param q1 defines the second operand
  2615. * @param result defines the target quaternion
  2616. * @returns the current quaternion
  2617. */
  2618. public multiplyToRef(q1: DeepImmutable<Quaternion>, result: Quaternion): Quaternion {
  2619. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  2620. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  2621. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  2622. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  2623. result.copyFromFloats(x, y, z, w);
  2624. return this;
  2625. }
  2626. /**
  2627. * Updates the current quaternion with the multiplication of itself with the given one "q1"
  2628. * @param q1 defines the second operand
  2629. * @returns the currentupdated quaternion
  2630. */
  2631. public multiplyInPlace(q1: DeepImmutable<Quaternion>): Quaternion {
  2632. this.multiplyToRef(q1, this);
  2633. return this;
  2634. }
  2635. /**
  2636. * Conjugates (1-q) the current quaternion and stores the result in the given quaternion
  2637. * @param ref defines the target quaternion
  2638. * @returns the current quaternion
  2639. */
  2640. public conjugateToRef(ref: Quaternion): Quaternion {
  2641. ref.copyFromFloats(-this.x, -this.y, -this.z, this.w);
  2642. return this;
  2643. }
  2644. /**
  2645. * Conjugates in place (1-q) the current quaternion
  2646. * @returns the current updated quaternion
  2647. */
  2648. public conjugateInPlace(): Quaternion {
  2649. this.x *= -1;
  2650. this.y *= -1;
  2651. this.z *= -1;
  2652. return this;
  2653. }
  2654. /**
  2655. * Conjugates in place (1-q) the current quaternion
  2656. * @returns a new quaternion
  2657. */
  2658. public conjugate(): Quaternion {
  2659. var result = new Quaternion(-this.x, -this.y, -this.z, this.w);
  2660. return result;
  2661. }
  2662. /**
  2663. * Gets length of current quaternion
  2664. * @returns the quaternion length (float)
  2665. */
  2666. public length(): number {
  2667. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  2668. }
  2669. /**
  2670. * Normalize in place the current quaternion
  2671. * @returns the current updated quaternion
  2672. */
  2673. public normalize(): Quaternion {
  2674. var len = this.length();
  2675. if (len === 0) {
  2676. return this;
  2677. }
  2678. var inv = 1.0 / len;
  2679. this.x *= inv;
  2680. this.y *= inv;
  2681. this.z *= inv;
  2682. this.w *= inv;
  2683. return this;
  2684. }
  2685. /**
  2686. * Returns a new Vector3 set with the Euler angles translated from the current quaternion
  2687. * @param order is a reserved parameter and is ignore for now
  2688. * @returns a new Vector3 containing the Euler angles
  2689. */
  2690. public toEulerAngles(order = "YZX"): Vector3 {
  2691. var result = Vector3.Zero();
  2692. this.toEulerAnglesToRef(result);
  2693. return result;
  2694. }
  2695. /**
  2696. * Sets the given vector3 "result" with the Euler angles translated from the current quaternion
  2697. * @param result defines the vector which will be filled with the Euler angles
  2698. * @param order is a reserved parameter and is ignore for now
  2699. * @returns the current unchanged quaternion
  2700. */
  2701. public toEulerAnglesToRef(result: Vector3): Quaternion {
  2702. var qz = this.z;
  2703. var qx = this.x;
  2704. var qy = this.y;
  2705. var qw = this.w;
  2706. var sqw = qw * qw;
  2707. var sqz = qz * qz;
  2708. var sqx = qx * qx;
  2709. var sqy = qy * qy;
  2710. var zAxisY = qy * qz - qx * qw;
  2711. var limit = .4999999;
  2712. if (zAxisY < -limit) {
  2713. result.y = 2 * Math.atan2(qy, qw);
  2714. result.x = Math.PI / 2;
  2715. result.z = 0;
  2716. } else if (zAxisY > limit) {
  2717. result.y = 2 * Math.atan2(qy, qw);
  2718. result.x = -Math.PI / 2;
  2719. result.z = 0;
  2720. } else {
  2721. result.z = Math.atan2(2.0 * (qx * qy + qz * qw), (-sqz - sqx + sqy + sqw));
  2722. result.x = Math.asin(-2.0 * (qz * qy - qx * qw));
  2723. result.y = Math.atan2(2.0 * (qz * qx + qy * qw), (sqz - sqx - sqy + sqw));
  2724. }
  2725. return this;
  2726. }
  2727. /**
  2728. * Updates the given rotation matrix with the current quaternion values
  2729. * @param result defines the target matrix
  2730. * @returns the current unchanged quaternion
  2731. */
  2732. public toRotationMatrix(result: Matrix): Quaternion {
  2733. Matrix.FromQuaternionToRef(this, result);
  2734. return this;
  2735. }
  2736. /**
  2737. * Updates the current quaternion from the given rotation matrix values
  2738. * @param matrix defines the source matrix
  2739. * @returns the current updated quaternion
  2740. */
  2741. public fromRotationMatrix(matrix: DeepImmutable<Matrix>): Quaternion {
  2742. Quaternion.FromRotationMatrixToRef(matrix, this);
  2743. return this;
  2744. }
  2745. // Statics
  2746. /**
  2747. * Creates a new quaternion from a rotation matrix
  2748. * @param matrix defines the source matrix
  2749. * @returns a new quaternion created from the given rotation matrix values
  2750. */
  2751. public static FromRotationMatrix(matrix: DeepImmutable<Matrix>): Quaternion {
  2752. var result = new Quaternion();
  2753. Quaternion.FromRotationMatrixToRef(matrix, result);
  2754. return result;
  2755. }
  2756. /**
  2757. * Updates the given quaternion with the given rotation matrix values
  2758. * @param matrix defines the source matrix
  2759. * @param result defines the target quaternion
  2760. */
  2761. public static FromRotationMatrixToRef(matrix: DeepImmutable<Matrix>, result: Quaternion): void {
  2762. var data = matrix.m;
  2763. var m11 = data[0], m12 = data[4], m13 = data[8];
  2764. var m21 = data[1], m22 = data[5], m23 = data[9];
  2765. var m31 = data[2], m32 = data[6], m33 = data[10];
  2766. var trace = m11 + m22 + m33;
  2767. var s;
  2768. if (trace > 0) {
  2769. s = 0.5 / Math.sqrt(trace + 1.0);
  2770. result.w = 0.25 / s;
  2771. result.x = (m32 - m23) * s;
  2772. result.y = (m13 - m31) * s;
  2773. result.z = (m21 - m12) * s;
  2774. } else if (m11 > m22 && m11 > m33) {
  2775. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  2776. result.w = (m32 - m23) / s;
  2777. result.x = 0.25 * s;
  2778. result.y = (m12 + m21) / s;
  2779. result.z = (m13 + m31) / s;
  2780. } else if (m22 > m33) {
  2781. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  2782. result.w = (m13 - m31) / s;
  2783. result.x = (m12 + m21) / s;
  2784. result.y = 0.25 * s;
  2785. result.z = (m23 + m32) / s;
  2786. } else {
  2787. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  2788. result.w = (m21 - m12) / s;
  2789. result.x = (m13 + m31) / s;
  2790. result.y = (m23 + m32) / s;
  2791. result.z = 0.25 * s;
  2792. }
  2793. }
  2794. /**
  2795. * Returns the dot product (float) between the quaternions "left" and "right"
  2796. * @param left defines the left operand
  2797. * @param right defines the right operand
  2798. * @returns the dot product
  2799. */
  2800. public static Dot(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>): number {
  2801. return (left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w);
  2802. }
  2803. /**
  2804. * Checks if the two quaternions are close to each other
  2805. * @param quat0 defines the first quaternion to check
  2806. * @param quat1 defines the second quaternion to check
  2807. * @returns true if the two quaternions are close to each other
  2808. */
  2809. public static AreClose(quat0: DeepImmutable<Quaternion>, quat1: DeepImmutable<Quaternion>): boolean {
  2810. let dot = Quaternion.Dot(quat0, quat1);
  2811. return dot >= 0;
  2812. }
  2813. /**
  2814. * Creates an empty quaternion
  2815. * @returns a new quaternion set to (0.0, 0.0, 0.0)
  2816. */
  2817. public static Zero(): Quaternion {
  2818. return new Quaternion(0.0, 0.0, 0.0, 0.0);
  2819. }
  2820. /**
  2821. * Inverse a given quaternion
  2822. * @param q defines the source quaternion
  2823. * @returns a new quaternion as the inverted current quaternion
  2824. */
  2825. public static Inverse(q: DeepImmutable<Quaternion>): Quaternion {
  2826. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  2827. }
  2828. /**
  2829. * Inverse a given quaternion
  2830. * @param q defines the source quaternion
  2831. * @param result the quaternion the result will be stored in
  2832. * @returns the result quaternion
  2833. */
  2834. public static InverseToRef(q: Quaternion, result: Quaternion): Quaternion {
  2835. result.set(-q.x, -q.y, -q.z, q.w);
  2836. return result;
  2837. }
  2838. /**
  2839. * Creates an identity quaternion
  2840. * @returns the identity quaternion
  2841. */
  2842. public static Identity(): Quaternion {
  2843. return new Quaternion(0.0, 0.0, 0.0, 1.0);
  2844. }
  2845. /**
  2846. * Gets a boolean indicating if the given quaternion is identity
  2847. * @param quaternion defines the quaternion to check
  2848. * @returns true if the quaternion is identity
  2849. */
  2850. public static IsIdentity(quaternion: DeepImmutable<Quaternion>): boolean {
  2851. return quaternion && quaternion.x === 0 && quaternion.y === 0 && quaternion.z === 0 && quaternion.w === 1;
  2852. }
  2853. /**
  2854. * Creates a quaternion from a rotation around an axis
  2855. * @param axis defines the axis to use
  2856. * @param angle defines the angle to use
  2857. * @returns a new quaternion created from the given axis (Vector3) and angle in radians (float)
  2858. */
  2859. public static RotationAxis(axis: DeepImmutable<Vector3>, angle: number): Quaternion {
  2860. return Quaternion.RotationAxisToRef(axis, angle, new Quaternion());
  2861. }
  2862. /**
  2863. * Creates a rotation around an axis and stores it into the given quaternion
  2864. * @param axis defines the axis to use
  2865. * @param angle defines the angle to use
  2866. * @param result defines the target quaternion
  2867. * @returns the target quaternion
  2868. */
  2869. public static RotationAxisToRef(axis: DeepImmutable<Vector3>, angle: number, result: Quaternion): Quaternion {
  2870. var sin = Math.sin(angle / 2);
  2871. axis.normalize();
  2872. result.w = Math.cos(angle / 2);
  2873. result.x = axis.x * sin;
  2874. result.y = axis.y * sin;
  2875. result.z = axis.z * sin;
  2876. return result;
  2877. }
  2878. /**
  2879. * Creates a new quaternion from data stored into an array
  2880. * @param array defines the data source
  2881. * @param offset defines the offset in the source array where the data starts
  2882. * @returns a new quaternion
  2883. */
  2884. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset?: number): Quaternion {
  2885. if (!offset) {
  2886. offset = 0;
  2887. }
  2888. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  2889. }
  2890. /**
  2891. * Updates the given quaternion "result" from the starting index of the given array.
  2892. * @param array the array to pull values from
  2893. * @param offset the offset into the array to start at
  2894. * @param result the quaternion to store the result in
  2895. */
  2896. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Quaternion): void {
  2897. result.x = array[offset];
  2898. result.y = array[offset + 1];
  2899. result.z = array[offset + 2];
  2900. result.w = array[offset + 3];
  2901. }
  2902. /**
  2903. * Create a quaternion from Euler rotation angles
  2904. * @param x Pitch
  2905. * @param y Yaw
  2906. * @param z Roll
  2907. * @returns the new Quaternion
  2908. */
  2909. public static FromEulerAngles(x: number, y: number, z: number): Quaternion {
  2910. var q = new Quaternion();
  2911. Quaternion.RotationYawPitchRollToRef(y, x, z, q);
  2912. return q;
  2913. }
  2914. /**
  2915. * Updates a quaternion from Euler rotation angles
  2916. * @param x Pitch
  2917. * @param y Yaw
  2918. * @param z Roll
  2919. * @param result the quaternion to store the result
  2920. * @returns the updated quaternion
  2921. */
  2922. public static FromEulerAnglesToRef(x: number, y: number, z: number, result: Quaternion): Quaternion {
  2923. Quaternion.RotationYawPitchRollToRef(y, x, z, result);
  2924. return result;
  2925. }
  2926. /**
  2927. * Create a quaternion from Euler rotation vector
  2928. * @param vec the Euler vector (x Pitch, y Yaw, z Roll)
  2929. * @returns the new Quaternion
  2930. */
  2931. public static FromEulerVector(vec: DeepImmutable<Vector3>): Quaternion {
  2932. var q = new Quaternion();
  2933. Quaternion.RotationYawPitchRollToRef(vec.y, vec.x, vec.z, q);
  2934. return q;
  2935. }
  2936. /**
  2937. * Updates a quaternion from Euler rotation vector
  2938. * @param vec the Euler vector (x Pitch, y Yaw, z Roll)
  2939. * @param result the quaternion to store the result
  2940. * @returns the updated quaternion
  2941. */
  2942. public static FromEulerVectorToRef(vec: DeepImmutable<Vector3>, result: Quaternion): Quaternion {
  2943. Quaternion.RotationYawPitchRollToRef(vec.y, vec.x, vec.z, result);
  2944. return result;
  2945. }
  2946. /**
  2947. * Creates a new quaternion from the given Euler float angles (y, x, z)
  2948. * @param yaw defines the rotation around Y axis
  2949. * @param pitch defines the rotation around X axis
  2950. * @param roll defines the rotation around Z axis
  2951. * @returns the new quaternion
  2952. */
  2953. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  2954. var q = new Quaternion();
  2955. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, q);
  2956. return q;
  2957. }
  2958. /**
  2959. * Creates a new rotation from the given Euler float angles (y, x, z) and stores it in the target quaternion
  2960. * @param yaw defines the rotation around Y axis
  2961. * @param pitch defines the rotation around X axis
  2962. * @param roll defines the rotation around Z axis
  2963. * @param result defines the target quaternion
  2964. */
  2965. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  2966. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  2967. var halfRoll = roll * 0.5;
  2968. var halfPitch = pitch * 0.5;
  2969. var halfYaw = yaw * 0.5;
  2970. var sinRoll = Math.sin(halfRoll);
  2971. var cosRoll = Math.cos(halfRoll);
  2972. var sinPitch = Math.sin(halfPitch);
  2973. var cosPitch = Math.cos(halfPitch);
  2974. var sinYaw = Math.sin(halfYaw);
  2975. var cosYaw = Math.cos(halfYaw);
  2976. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  2977. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  2978. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  2979. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  2980. }
  2981. /**
  2982. * Creates a new quaternion from the given Euler float angles expressed in z-x-z orientation
  2983. * @param alpha defines the rotation around first axis
  2984. * @param beta defines the rotation around second axis
  2985. * @param gamma defines the rotation around third axis
  2986. * @returns the new quaternion
  2987. */
  2988. public static RotationAlphaBetaGamma(alpha: number, beta: number, gamma: number): Quaternion {
  2989. var result = new Quaternion();
  2990. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  2991. return result;
  2992. }
  2993. /**
  2994. * Creates a new quaternion from the given Euler float angles expressed in z-x-z orientation and stores it in the target quaternion
  2995. * @param alpha defines the rotation around first axis
  2996. * @param beta defines the rotation around second axis
  2997. * @param gamma defines the rotation around third axis
  2998. * @param result defines the target quaternion
  2999. */
  3000. public static RotationAlphaBetaGammaToRef(alpha: number, beta: number, gamma: number, result: Quaternion): void {
  3001. // Produces a quaternion from Euler angles in the z-x-z orientation
  3002. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  3003. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  3004. var halfBeta = beta * 0.5;
  3005. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  3006. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  3007. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  3008. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  3009. }
  3010. /**
  3011. * Creates a new quaternion containing the rotation value to reach the target (axis1, axis2, axis3) orientation as a rotated XYZ system (axis1, axis2 and axis3 are normalized during this operation)
  3012. * @param axis1 defines the first axis
  3013. * @param axis2 defines the second axis
  3014. * @param axis3 defines the third axis
  3015. * @returns the new quaternion
  3016. */
  3017. public static RotationQuaternionFromAxis(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>): Quaternion {
  3018. var quat = new Quaternion(0.0, 0.0, 0.0, 0.0);
  3019. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  3020. return quat;
  3021. }
  3022. /**
  3023. * Creates a rotation value to reach the target (axis1, axis2, axis3) orientation as a rotated XYZ system (axis1, axis2 and axis3 are normalized during this operation) and stores it in the target quaternion
  3024. * @param axis1 defines the first axis
  3025. * @param axis2 defines the second axis
  3026. * @param axis3 defines the third axis
  3027. * @param ref defines the target quaternion
  3028. */
  3029. public static RotationQuaternionFromAxisToRef(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>, ref: Quaternion): void {
  3030. var rotMat = MathTmp.Matrix[0];
  3031. Matrix.FromXYZAxesToRef(axis1.normalize(), axis2.normalize(), axis3.normalize(), rotMat);
  3032. Quaternion.FromRotationMatrixToRef(rotMat, ref);
  3033. }
  3034. /**
  3035. * Interpolates between two quaternions
  3036. * @param left defines first quaternion
  3037. * @param right defines second quaternion
  3038. * @param amount defines the gradient to use
  3039. * @returns the new interpolated quaternion
  3040. */
  3041. public static Slerp(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>, amount: number): Quaternion {
  3042. var result = Quaternion.Identity();
  3043. Quaternion.SlerpToRef(left, right, amount, result);
  3044. return result;
  3045. }
  3046. /**
  3047. * Interpolates between two quaternions and stores it into a target quaternion
  3048. * @param left defines first quaternion
  3049. * @param right defines second quaternion
  3050. * @param amount defines the gradient to use
  3051. * @param result defines the target quaternion
  3052. */
  3053. public static SlerpToRef(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>, amount: number, result: Quaternion): void {
  3054. var num2;
  3055. var num3;
  3056. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  3057. var flag = false;
  3058. if (num4 < 0) {
  3059. flag = true;
  3060. num4 = -num4;
  3061. }
  3062. if (num4 > 0.999999) {
  3063. num3 = 1 - amount;
  3064. num2 = flag ? -amount : amount;
  3065. }
  3066. else {
  3067. var num5 = Math.acos(num4);
  3068. var num6 = (1.0 / Math.sin(num5));
  3069. num3 = (Math.sin((1.0 - amount) * num5)) * num6;
  3070. num2 = flag ? ((-Math.sin(amount * num5)) * num6) : ((Math.sin(amount * num5)) * num6);
  3071. }
  3072. result.x = (num3 * left.x) + (num2 * right.x);
  3073. result.y = (num3 * left.y) + (num2 * right.y);
  3074. result.z = (num3 * left.z) + (num2 * right.z);
  3075. result.w = (num3 * left.w) + (num2 * right.w);
  3076. }
  3077. /**
  3078. * Interpolate between two quaternions using Hermite interpolation
  3079. * @param value1 defines first quaternion
  3080. * @param tangent1 defines the incoming tangent
  3081. * @param value2 defines second quaternion
  3082. * @param tangent2 defines the outgoing tangent
  3083. * @param amount defines the target quaternion
  3084. * @returns the new interpolated quaternion
  3085. */
  3086. public static Hermite(value1: DeepImmutable<Quaternion>, tangent1: DeepImmutable<Quaternion>, value2: DeepImmutable<Quaternion>, tangent2: DeepImmutable<Quaternion>, amount: number): Quaternion {
  3087. var squared = amount * amount;
  3088. var cubed = amount * squared;
  3089. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  3090. var part2 = (-2.0 * cubed) + (3.0 * squared);
  3091. var part3 = (cubed - (2.0 * squared)) + amount;
  3092. var part4 = cubed - squared;
  3093. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  3094. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  3095. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  3096. var w = (((value1.w * part1) + (value2.w * part2)) + (tangent1.w * part3)) + (tangent2.w * part4);
  3097. return new Quaternion(x, y, z, w);
  3098. }
  3099. }
  3100. /**
  3101. * Class used to store matrix data (4x4)
  3102. */
  3103. export class Matrix {
  3104. private static _updateFlagSeed = 0;
  3105. private static _identityReadOnly = Matrix.Identity() as DeepImmutable<Matrix>;
  3106. private _isIdentity = false;
  3107. private _isIdentityDirty = true;
  3108. private _isIdentity3x2 = true;
  3109. private _isIdentity3x2Dirty = true;
  3110. /**
  3111. * Gets the update flag of the matrix which is an unique number for the matrix.
  3112. * It will be incremented every time the matrix data change.
  3113. * You can use it to speed the comparison between two versions of the same matrix.
  3114. */
  3115. public updateFlag: number = -1;
  3116. private readonly _m: Float32Array = new Float32Array(16);
  3117. /**
  3118. * Gets the internal data of the matrix
  3119. */
  3120. public get m(): DeepImmutable<Float32Array> { return this._m; }
  3121. /** @hidden */
  3122. public _markAsUpdated() {
  3123. this.updateFlag = Matrix._updateFlagSeed++;
  3124. this._isIdentity = false;
  3125. this._isIdentity3x2 = false;
  3126. this._isIdentityDirty = true;
  3127. this._isIdentity3x2Dirty = true;
  3128. }
  3129. /** @hidden */
  3130. private _updateIdentityStatus(isIdentity: boolean, isIdentityDirty: boolean = false, isIdentity3x2: boolean = false, isIdentity3x2Dirty: boolean = true) {
  3131. this.updateFlag = Matrix._updateFlagSeed++;
  3132. this._isIdentity = isIdentity;
  3133. this._isIdentity3x2 = isIdentity || isIdentity3x2;
  3134. this._isIdentityDirty = this._isIdentity ? false : isIdentityDirty;
  3135. this._isIdentity3x2Dirty = this._isIdentity3x2 ? false : isIdentity3x2Dirty;
  3136. }
  3137. /**
  3138. * Creates an empty matrix (filled with zeros)
  3139. */
  3140. public constructor() {
  3141. this._updateIdentityStatus(false);
  3142. }
  3143. // Properties
  3144. /**
  3145. * Check if the current matrix is identity
  3146. * @returns true is the matrix is the identity matrix
  3147. */
  3148. public isIdentity(): boolean {
  3149. if (this._isIdentityDirty) {
  3150. this._isIdentityDirty = false;
  3151. const m = this._m;
  3152. this._isIdentity = (
  3153. m[0] === 1.0 && m[1] === 0.0 && m[2] === 0.0 && m[3] === 0.0 &&
  3154. m[4] === 0.0 && m[5] === 1.0 && m[6] === 0.0 && m[7] === 0.0 &&
  3155. m[8] === 0.0 && m[9] === 0.0 && m[10] === 1.0 && m[11] === 0.0 &&
  3156. m[12] === 0.0 && m[13] === 0.0 && m[14] === 0.0 && m[15] === 1.0
  3157. );
  3158. }
  3159. return this._isIdentity;
  3160. }
  3161. /**
  3162. * Check if the current matrix is identity as a texture matrix (3x2 store in 4x4)
  3163. * @returns true is the matrix is the identity matrix
  3164. */
  3165. public isIdentityAs3x2(): boolean {
  3166. if (this._isIdentity3x2Dirty) {
  3167. this._isIdentity3x2Dirty = false;
  3168. if (this._m[0] !== 1.0 || this._m[5] !== 1.0 || this._m[15] !== 1.0) {
  3169. this._isIdentity3x2 = false;
  3170. } else if (this._m[1] !== 0.0 || this._m[2] !== 0.0 || this._m[3] !== 0.0 ||
  3171. this._m[4] !== 0.0 || this._m[6] !== 0.0 || this._m[7] !== 0.0 ||
  3172. this._m[8] !== 0.0 || this._m[9] !== 0.0 || this._m[10] !== 0.0 || this._m[11] !== 0.0 ||
  3173. this._m[12] !== 0.0 || this._m[13] !== 0.0 || this._m[14] !== 0.0) {
  3174. this._isIdentity3x2 = false;
  3175. } else {
  3176. this._isIdentity3x2 = true;
  3177. }
  3178. }
  3179. return this._isIdentity3x2;
  3180. }
  3181. /**
  3182. * Gets the determinant of the matrix
  3183. * @returns the matrix determinant
  3184. */
  3185. public determinant(): number {
  3186. if (this._isIdentity === true) {
  3187. return 1;
  3188. }
  3189. const m = this._m;
  3190. const m00 = m[0], m01 = m[1], m02 = m[2], m03 = m[3];
  3191. const m10 = m[4], m11 = m[5], m12 = m[6], m13 = m[7];
  3192. const m20 = m[8], m21 = m[9], m22 = m[10], m23 = m[11];
  3193. const m30 = m[12], m31 = m[13], m32 = m[14], m33 = m[15];
  3194. // https://en.wikipedia.org/wiki/Laplace_expansion
  3195. // to compute the deterrminant of a 4x4 Matrix we compute the cofactors of any row or column,
  3196. // then we multiply each Cofactor by its corresponding matrix value and sum them all to get the determinant
  3197. // Cofactor(i, j) = sign(i,j) * det(Minor(i, j))
  3198. // where
  3199. // - sign(i,j) = (i+j) % 2 === 0 ? 1 : -1
  3200. // - Minor(i, j) is the 3x3 matrix we get by removing row i and column j from current Matrix
  3201. //
  3202. // Here we do that for the 1st row.
  3203. const det_22_33 = m22 * m33 - m32 * m23;
  3204. const det_21_33 = m21 * m33 - m31 * m23;
  3205. const det_21_32 = m21 * m32 - m31 * m22;
  3206. const det_20_33 = m20 * m33 - m30 * m23;
  3207. const det_20_32 = m20 * m32 - m22 * m30;
  3208. const det_20_31 = m20 * m31 - m30 * m21;
  3209. const cofact_00 = +(m11 * det_22_33 - m12 * det_21_33 + m13 * det_21_32);
  3210. const cofact_01 = -(m10 * det_22_33 - m12 * det_20_33 + m13 * det_20_32);
  3211. const cofact_02 = +(m10 * det_21_33 - m11 * det_20_33 + m13 * det_20_31);
  3212. const cofact_03 = -(m10 * det_21_32 - m11 * det_20_32 + m12 * det_20_31);
  3213. return m00 * cofact_00 + m01 * cofact_01 + m02 * cofact_02 + m03 * cofact_03;
  3214. }
  3215. // Methods
  3216. /**
  3217. * Returns the matrix as a Float32Array
  3218. * @returns the matrix underlying array
  3219. */
  3220. public toArray(): DeepImmutable<Float32Array> {
  3221. return this._m;
  3222. }
  3223. /**
  3224. * Returns the matrix as a Float32Array
  3225. * @returns the matrix underlying array.
  3226. */
  3227. public asArray(): DeepImmutable<Float32Array> {
  3228. return this._m;
  3229. }
  3230. /**
  3231. * Inverts the current matrix in place
  3232. * @returns the current inverted matrix
  3233. */
  3234. public invert(): Matrix {
  3235. this.invertToRef(this);
  3236. return this;
  3237. }
  3238. /**
  3239. * Sets all the matrix elements to zero
  3240. * @returns the current matrix
  3241. */
  3242. public reset(): Matrix {
  3243. Matrix.FromValuesToRef(
  3244. 0.0, 0.0, 0.0, 0.0,
  3245. 0.0, 0.0, 0.0, 0.0,
  3246. 0.0, 0.0, 0.0, 0.0,
  3247. 0.0, 0.0, 0.0, 0.0,
  3248. this
  3249. );
  3250. this._updateIdentityStatus(false);
  3251. return this;
  3252. }
  3253. /**
  3254. * Adds the current matrix with a second one
  3255. * @param other defines the matrix to add
  3256. * @returns a new matrix as the addition of the current matrix and the given one
  3257. */
  3258. public add(other: DeepImmutable<Matrix>): Matrix {
  3259. var result = new Matrix();
  3260. this.addToRef(other, result);
  3261. return result;
  3262. }
  3263. /**
  3264. * Sets the given matrix "result" to the addition of the current matrix and the given one
  3265. * @param other defines the matrix to add
  3266. * @param result defines the target matrix
  3267. * @returns the current matrix
  3268. */
  3269. public addToRef(other: DeepImmutable<Matrix>, result: Matrix): Matrix {
  3270. const m = this._m;
  3271. const resultM = result._m;
  3272. const otherM = other.m;
  3273. for (var index = 0; index < 16; index++) {
  3274. resultM[index] = m[index] + otherM[index];
  3275. }
  3276. result._markAsUpdated();
  3277. return this;
  3278. }
  3279. /**
  3280. * Adds in place the given matrix to the current matrix
  3281. * @param other defines the second operand
  3282. * @returns the current updated matrix
  3283. */
  3284. public addToSelf(other: DeepImmutable<Matrix>): Matrix {
  3285. const m = this._m;
  3286. const otherM = other.m;
  3287. for (var index = 0; index < 16; index++) {
  3288. m[index] += otherM[index];
  3289. }
  3290. this._markAsUpdated();
  3291. return this;
  3292. }
  3293. /**
  3294. * Sets the given matrix to the current inverted Matrix
  3295. * @param other defines the target matrix
  3296. * @returns the unmodified current matrix
  3297. */
  3298. public invertToRef(other: Matrix): Matrix {
  3299. if (this._isIdentity === true) {
  3300. Matrix.IdentityToRef(other);
  3301. return this;
  3302. }
  3303. // the inverse of a Matrix is the transpose of cofactor matrix divided by the determinant
  3304. const m = this._m;
  3305. const m00 = m[0], m01 = m[1], m02 = m[2], m03 = m[3];
  3306. const m10 = m[4], m11 = m[5], m12 = m[6], m13 = m[7];
  3307. const m20 = m[8], m21 = m[9], m22 = m[10], m23 = m[11];
  3308. const m30 = m[12], m31 = m[13], m32 = m[14], m33 = m[15];
  3309. const det_22_33 = m22 * m33 - m32 * m23;
  3310. const det_21_33 = m21 * m33 - m31 * m23;
  3311. const det_21_32 = m21 * m32 - m31 * m22;
  3312. const det_20_33 = m20 * m33 - m30 * m23;
  3313. const det_20_32 = m20 * m32 - m22 * m30;
  3314. const det_20_31 = m20 * m31 - m30 * m21;
  3315. const cofact_00 = +(m11 * det_22_33 - m12 * det_21_33 + m13 * det_21_32);
  3316. const cofact_01 = -(m10 * det_22_33 - m12 * det_20_33 + m13 * det_20_32);
  3317. const cofact_02 = +(m10 * det_21_33 - m11 * det_20_33 + m13 * det_20_31);
  3318. const cofact_03 = -(m10 * det_21_32 - m11 * det_20_32 + m12 * det_20_31);
  3319. const det = m00 * cofact_00 + m01 * cofact_01 + m02 * cofact_02 + m03 * cofact_03;
  3320. if (det === 0) {
  3321. // not invertible
  3322. other.copyFrom(this);
  3323. return this;
  3324. }
  3325. const detInv = 1 / det;
  3326. const det_12_33 = m12 * m33 - m32 * m13;
  3327. const det_11_33 = m11 * m33 - m31 * m13;
  3328. const det_11_32 = m11 * m32 - m31 * m12;
  3329. const det_10_33 = m10 * m33 - m30 * m13;
  3330. const det_10_32 = m10 * m32 - m30 * m12;
  3331. const det_10_31 = m10 * m31 - m30 * m11;
  3332. const det_12_23 = m12 * m23 - m22 * m13;
  3333. const det_11_23 = m11 * m23 - m21 * m13;
  3334. const det_11_22 = m11 * m22 - m21 * m12;
  3335. const det_10_23 = m10 * m23 - m20 * m13;
  3336. const det_10_22 = m10 * m22 - m20 * m12;
  3337. const det_10_21 = m10 * m21 - m20 * m11;
  3338. const cofact_10 = -(m01 * det_22_33 - m02 * det_21_33 + m03 * det_21_32);
  3339. const cofact_11 = +(m00 * det_22_33 - m02 * det_20_33 + m03 * det_20_32);
  3340. const cofact_12 = -(m00 * det_21_33 - m01 * det_20_33 + m03 * det_20_31);
  3341. const cofact_13 = +(m00 * det_21_32 - m01 * det_20_32 + m02 * det_20_31);
  3342. const cofact_20 = +(m01 * det_12_33 - m02 * det_11_33 + m03 * det_11_32);
  3343. const cofact_21 = -(m00 * det_12_33 - m02 * det_10_33 + m03 * det_10_32);
  3344. const cofact_22 = +(m00 * det_11_33 - m01 * det_10_33 + m03 * det_10_31);
  3345. const cofact_23 = -(m00 * det_11_32 - m01 * det_10_32 + m02 * det_10_31);
  3346. const cofact_30 = -(m01 * det_12_23 - m02 * det_11_23 + m03 * det_11_22);
  3347. const cofact_31 = +(m00 * det_12_23 - m02 * det_10_23 + m03 * det_10_22);
  3348. const cofact_32 = -(m00 * det_11_23 - m01 * det_10_23 + m03 * det_10_21);
  3349. const cofact_33 = +(m00 * det_11_22 - m01 * det_10_22 + m02 * det_10_21);
  3350. Matrix.FromValuesToRef(
  3351. cofact_00 * detInv, cofact_10 * detInv, cofact_20 * detInv, cofact_30 * detInv,
  3352. cofact_01 * detInv, cofact_11 * detInv, cofact_21 * detInv, cofact_31 * detInv,
  3353. cofact_02 * detInv, cofact_12 * detInv, cofact_22 * detInv, cofact_32 * detInv,
  3354. cofact_03 * detInv, cofact_13 * detInv, cofact_23 * detInv, cofact_33 * detInv,
  3355. other
  3356. );
  3357. return this;
  3358. }
  3359. /**
  3360. * add a value at the specified position in the current Matrix
  3361. * @param index the index of the value within the matrix. between 0 and 15.
  3362. * @param value the value to be added
  3363. * @returns the current updated matrix
  3364. */
  3365. public addAtIndex(index: number, value: number): Matrix {
  3366. this._m[index] += value;
  3367. this._markAsUpdated();
  3368. return this;
  3369. }
  3370. /**
  3371. * mutiply the specified position in the current Matrix by a value
  3372. * @param index the index of the value within the matrix. between 0 and 15.
  3373. * @param value the value to be added
  3374. * @returns the current updated matrix
  3375. */
  3376. public multiplyAtIndex(index: number, value: number): Matrix {
  3377. this._m[index] *= value;
  3378. this._markAsUpdated();
  3379. return this;
  3380. }
  3381. /**
  3382. * Inserts the translation vector (using 3 floats) in the current matrix
  3383. * @param x defines the 1st component of the translation
  3384. * @param y defines the 2nd component of the translation
  3385. * @param z defines the 3rd component of the translation
  3386. * @returns the current updated matrix
  3387. */
  3388. public setTranslationFromFloats(x: number, y: number, z: number): Matrix {
  3389. this._m[12] = x;
  3390. this._m[13] = y;
  3391. this._m[14] = z;
  3392. this._markAsUpdated();
  3393. return this;
  3394. }
  3395. /**
  3396. * Adds the translation vector (using 3 floats) in the current matrix
  3397. * @param x defines the 1st component of the translation
  3398. * @param y defines the 2nd component of the translation
  3399. * @param z defines the 3rd component of the translation
  3400. * @returns the current updated matrix
  3401. */
  3402. public addTranslationFromFloats(x: number, y: number, z: number): Matrix {
  3403. this._m[12] += x;
  3404. this._m[13] += y;
  3405. this._m[14] += z;
  3406. this._markAsUpdated();
  3407. return this;
  3408. }
  3409. /**
  3410. * Inserts the translation vector in the current matrix
  3411. * @param vector3 defines the translation to insert
  3412. * @returns the current updated matrix
  3413. */
  3414. public setTranslation(vector3: DeepImmutable<Vector3>): Matrix {
  3415. return this.setTranslationFromFloats(vector3.x, vector3.y, vector3.z);
  3416. }
  3417. /**
  3418. * Gets the translation value of the current matrix
  3419. * @returns a new Vector3 as the extracted translation from the matrix
  3420. */
  3421. public getTranslation(): Vector3 {
  3422. return new Vector3(this._m[12], this._m[13], this._m[14]);
  3423. }
  3424. /**
  3425. * Fill a Vector3 with the extracted translation from the matrix
  3426. * @param result defines the Vector3 where to store the translation
  3427. * @returns the current matrix
  3428. */
  3429. public getTranslationToRef(result: Vector3): Matrix {
  3430. result.x = this._m[12];
  3431. result.y = this._m[13];
  3432. result.z = this._m[14];
  3433. return this;
  3434. }
  3435. /**
  3436. * Remove rotation and scaling part from the matrix
  3437. * @returns the updated matrix
  3438. */
  3439. public removeRotationAndScaling(): Matrix {
  3440. const m = this.m;
  3441. Matrix.FromValuesToRef(
  3442. 1.0, 0.0, 0.0, 0.0,
  3443. 0.0, 1.0, 0.0, 0.0,
  3444. 0.0, 0.0, 1.0, 0.0,
  3445. m[12], m[13], m[14], m[15],
  3446. this
  3447. );
  3448. this._updateIdentityStatus(m[12] === 0 && m[13] === 0 && m[14] === 0 && m[15] === 1);
  3449. return this;
  3450. }
  3451. /**
  3452. * Multiply two matrices
  3453. * @param other defines the second operand
  3454. * @returns a new matrix set with the multiplication result of the current Matrix and the given one
  3455. */
  3456. public multiply(other: DeepImmutable<Matrix>): Matrix {
  3457. var result = new Matrix();
  3458. this.multiplyToRef(other, result);
  3459. return result;
  3460. }
  3461. /**
  3462. * Copy the current matrix from the given one
  3463. * @param other defines the source matrix
  3464. * @returns the current updated matrix
  3465. */
  3466. public copyFrom(other: DeepImmutable<Matrix>): Matrix {
  3467. other.copyToArray(this._m);
  3468. const o = (other as Matrix);
  3469. this._updateIdentityStatus(o._isIdentity, o._isIdentityDirty, o._isIdentity3x2, o._isIdentity3x2Dirty);
  3470. return this;
  3471. }
  3472. /**
  3473. * Populates the given array from the starting index with the current matrix values
  3474. * @param array defines the target array
  3475. * @param offset defines the offset in the target array where to start storing values
  3476. * @returns the current matrix
  3477. */
  3478. public copyToArray(array: Float32Array, offset: number = 0): Matrix {
  3479. let source = this._m;
  3480. array[offset] = source[0];
  3481. array[offset + 1] = source[1];
  3482. array[offset + 2] = source[2];
  3483. array[offset + 3] = source[3];
  3484. array[offset + 4] = source[4];
  3485. array[offset + 5] = source[5];
  3486. array[offset + 6] = source[6];
  3487. array[offset + 7] = source[7];
  3488. array[offset + 8] = source[8];
  3489. array[offset + 9] = source[9];
  3490. array[offset + 10] = source[10];
  3491. array[offset + 11] = source[11];
  3492. array[offset + 12] = source[12];
  3493. array[offset + 13] = source[13];
  3494. array[offset + 14] = source[14];
  3495. array[offset + 15] = source[15];
  3496. return this;
  3497. }
  3498. /**
  3499. * Sets the given matrix "result" with the multiplication result of the current Matrix and the given one
  3500. * @param other defines the second operand
  3501. * @param result defines the matrix where to store the multiplication
  3502. * @returns the current matrix
  3503. */
  3504. public multiplyToRef(other: DeepImmutable<Matrix>, result: Matrix): Matrix {
  3505. if (this._isIdentity) {
  3506. result.copyFrom(other);
  3507. return this;
  3508. }
  3509. if ((other as Matrix)._isIdentity) {
  3510. result.copyFrom(this);
  3511. return this;
  3512. }
  3513. this.multiplyToArray(other, result._m, 0);
  3514. result._markAsUpdated();
  3515. return this;
  3516. }
  3517. /**
  3518. * Sets the Float32Array "result" from the given index "offset" with the multiplication of the current matrix and the given one
  3519. * @param other defines the second operand
  3520. * @param result defines the array where to store the multiplication
  3521. * @param offset defines the offset in the target array where to start storing values
  3522. * @returns the current matrix
  3523. */
  3524. public multiplyToArray(other: DeepImmutable<Matrix>, result: Float32Array, offset: number): Matrix {
  3525. const m = this._m;
  3526. const otherM = other.m;
  3527. var tm0 = m[0], tm1 = m[1], tm2 = m[2], tm3 = m[3];
  3528. var tm4 = m[4], tm5 = m[5], tm6 = m[6], tm7 = m[7];
  3529. var tm8 = m[8], tm9 = m[9], tm10 = m[10], tm11 = m[11];
  3530. var tm12 = m[12], tm13 = m[13], tm14 = m[14], tm15 = m[15];
  3531. var om0 = otherM[0], om1 = otherM[1], om2 = otherM[2], om3 = otherM[3];
  3532. var om4 = otherM[4], om5 = otherM[5], om6 = otherM[6], om7 = otherM[7];
  3533. var om8 = otherM[8], om9 = otherM[9], om10 = otherM[10], om11 = otherM[11];
  3534. var om12 = otherM[12], om13 = otherM[13], om14 = otherM[14], om15 = otherM[15];
  3535. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  3536. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  3537. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  3538. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  3539. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  3540. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  3541. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  3542. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  3543. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  3544. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  3545. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  3546. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  3547. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  3548. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  3549. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  3550. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  3551. return this;
  3552. }
  3553. /**
  3554. * Check equality between this matrix and a second one
  3555. * @param value defines the second matrix to compare
  3556. * @returns true is the current matrix and the given one values are strictly equal
  3557. */
  3558. public equals(value: DeepImmutable<Matrix>): boolean {
  3559. const other = (value as Matrix);
  3560. if (!other) {
  3561. return false;
  3562. }
  3563. if (this._isIdentity || other._isIdentity) {
  3564. if (!this._isIdentityDirty && !other._isIdentityDirty) {
  3565. return this._isIdentity && other._isIdentity;
  3566. }
  3567. }
  3568. const m = this.m;
  3569. const om = other.m;
  3570. return (
  3571. m[0] === om[0] && m[1] === om[1] && m[2] === om[2] && m[3] === om[3] &&
  3572. m[4] === om[4] && m[5] === om[5] && m[6] === om[6] && m[7] === om[7] &&
  3573. m[8] === om[8] && m[9] === om[9] && m[10] === om[10] && m[11] === om[11] &&
  3574. m[12] === om[12] && m[13] === om[13] && m[14] === om[14] && m[15] === om[15]
  3575. );
  3576. }
  3577. /**
  3578. * Clone the current matrix
  3579. * @returns a new matrix from the current matrix
  3580. */
  3581. public clone(): Matrix {
  3582. const matrix = new Matrix();
  3583. matrix.copyFrom(this);
  3584. return matrix;
  3585. }
  3586. /**
  3587. * Returns the name of the current matrix class
  3588. * @returns the string "Matrix"
  3589. */
  3590. public getClassName(): string {
  3591. return "Matrix";
  3592. }
  3593. /**
  3594. * Gets the hash code of the current matrix
  3595. * @returns the hash code
  3596. */
  3597. public getHashCode(): number {
  3598. let hash = this._m[0] | 0;
  3599. for (let i = 1; i < 16; i++) {
  3600. hash = (hash * 397) ^ (this._m[i] | 0);
  3601. }
  3602. return hash;
  3603. }
  3604. /**
  3605. * Decomposes the current Matrix into a translation, rotation and scaling components
  3606. * @param scale defines the scale vector3 given as a reference to update
  3607. * @param rotation defines the rotation quaternion given as a reference to update
  3608. * @param translation defines the translation vector3 given as a reference to update
  3609. * @returns true if operation was successful
  3610. */
  3611. public decompose(scale?: Vector3, rotation?: Quaternion, translation?: Vector3): boolean {
  3612. if (this._isIdentity) {
  3613. if (translation) {
  3614. translation.setAll(0);
  3615. }
  3616. if (scale) {
  3617. scale.setAll(1);
  3618. }
  3619. if (rotation) {
  3620. rotation.copyFromFloats(0, 0, 0, 1);
  3621. }
  3622. return true;
  3623. }
  3624. const m = this._m;
  3625. if (translation) {
  3626. translation.copyFromFloats(m[12], m[13], m[14]);
  3627. }
  3628. scale = scale || MathTmp.Vector3[0];
  3629. scale.x = Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
  3630. scale.y = Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);
  3631. scale.z = Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]);
  3632. if (this.determinant() <= 0) {
  3633. scale.y *= -1;
  3634. }
  3635. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  3636. if (rotation) {
  3637. rotation.copyFromFloats(0.0, 0.0, 0.0, 1.0);
  3638. }
  3639. return false;
  3640. }
  3641. if (rotation) {
  3642. const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
  3643. Matrix.FromValuesToRef(
  3644. m[0] * sx, m[1] * sx, m[2] * sx, 0.0,
  3645. m[4] * sy, m[5] * sy, m[6] * sy, 0.0,
  3646. m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
  3647. 0.0, 0.0, 0.0, 1.0,
  3648. MathTmp.Matrix[0]
  3649. );
  3650. Quaternion.FromRotationMatrixToRef(MathTmp.Matrix[0], rotation);
  3651. }
  3652. return true;
  3653. }
  3654. /**
  3655. * Gets specific row of the matrix
  3656. * @param index defines the number of the row to get
  3657. * @returns the index-th row of the current matrix as a new Vector4
  3658. */
  3659. public getRow(index: number): Nullable<Vector4> {
  3660. if (index < 0 || index > 3) {
  3661. return null;
  3662. }
  3663. var i = index * 4;
  3664. return new Vector4(this._m[i + 0], this._m[i + 1], this._m[i + 2], this._m[i + 3]);
  3665. }
  3666. /**
  3667. * Sets the index-th row of the current matrix to the vector4 values
  3668. * @param index defines the number of the row to set
  3669. * @param row defines the target vector4
  3670. * @returns the updated current matrix
  3671. */
  3672. public setRow(index: number, row: Vector4): Matrix {
  3673. return this.setRowFromFloats(index, row.x, row.y, row.z, row.w);
  3674. }
  3675. /**
  3676. * Compute the transpose of the matrix
  3677. * @returns the new transposed matrix
  3678. */
  3679. public transpose(): Matrix {
  3680. return Matrix.Transpose(this);
  3681. }
  3682. /**
  3683. * Compute the transpose of the matrix and store it in a given matrix
  3684. * @param result defines the target matrix
  3685. * @returns the current matrix
  3686. */
  3687. public transposeToRef(result: Matrix): Matrix {
  3688. Matrix.TransposeToRef(this, result);
  3689. return this;
  3690. }
  3691. /**
  3692. * Sets the index-th row of the current matrix with the given 4 x float values
  3693. * @param index defines the row index
  3694. * @param x defines the x component to set
  3695. * @param y defines the y component to set
  3696. * @param z defines the z component to set
  3697. * @param w defines the w component to set
  3698. * @returns the updated current matrix
  3699. */
  3700. public setRowFromFloats(index: number, x: number, y: number, z: number, w: number): Matrix {
  3701. if (index < 0 || index > 3) {
  3702. return this;
  3703. }
  3704. var i = index * 4;
  3705. this._m[i + 0] = x;
  3706. this._m[i + 1] = y;
  3707. this._m[i + 2] = z;
  3708. this._m[i + 3] = w;
  3709. this._markAsUpdated();
  3710. return this;
  3711. }
  3712. /**
  3713. * Compute a new matrix set with the current matrix values multiplied by scale (float)
  3714. * @param scale defines the scale factor
  3715. * @returns a new matrix
  3716. */
  3717. public scale(scale: number): Matrix {
  3718. var result = new Matrix();
  3719. this.scaleToRef(scale, result);
  3720. return result;
  3721. }
  3722. /**
  3723. * Scale the current matrix values by a factor to a given result matrix
  3724. * @param scale defines the scale factor
  3725. * @param result defines the matrix to store the result
  3726. * @returns the current matrix
  3727. */
  3728. public scaleToRef(scale: number, result: Matrix): Matrix {
  3729. for (var index = 0; index < 16; index++) {
  3730. result._m[index] = this._m[index] * scale;
  3731. }
  3732. result._markAsUpdated();
  3733. return this;
  3734. }
  3735. /**
  3736. * Scale the current matrix values by a factor and add the result to a given matrix
  3737. * @param scale defines the scale factor
  3738. * @param result defines the Matrix to store the result
  3739. * @returns the current matrix
  3740. */
  3741. public scaleAndAddToRef(scale: number, result: Matrix): Matrix {
  3742. for (var index = 0; index < 16; index++) {
  3743. result._m[index] += this._m[index] * scale;
  3744. }
  3745. result._markAsUpdated();
  3746. return this;
  3747. }
  3748. /**
  3749. * Writes to the given matrix a normal matrix, computed from this one (using values from identity matrix for fourth row and column).
  3750. * @param ref matrix to store the result
  3751. */
  3752. public toNormalMatrix(ref: Matrix): void {
  3753. const tmp = MathTmp.Matrix[0];
  3754. this.invertToRef(tmp);
  3755. tmp.transposeToRef(ref);
  3756. var m = ref._m;
  3757. Matrix.FromValuesToRef(
  3758. m[0], m[1], m[2], 0.0,
  3759. m[4], m[5], m[6], 0.0,
  3760. m[8], m[9], m[10], 0.0,
  3761. 0.0, 0.0, 0.0, 1.0,
  3762. ref
  3763. );
  3764. }
  3765. /**
  3766. * Gets only rotation part of the current matrix
  3767. * @returns a new matrix sets to the extracted rotation matrix from the current one
  3768. */
  3769. public getRotationMatrix(): Matrix {
  3770. var result = new Matrix();
  3771. this.getRotationMatrixToRef(result);
  3772. return result;
  3773. }
  3774. /**
  3775. * Extracts the rotation matrix from the current one and sets it as the given "result"
  3776. * @param result defines the target matrix to store data to
  3777. * @returns the current matrix
  3778. */
  3779. public getRotationMatrixToRef(result: Matrix): Matrix {
  3780. const scale = MathTmp.Vector3[0];
  3781. if (!this.decompose(scale)) {
  3782. Matrix.IdentityToRef(result);
  3783. return this;
  3784. }
  3785. const m = this._m;
  3786. const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
  3787. Matrix.FromValuesToRef(
  3788. m[0] * sx, m[1] * sx, m[2] * sx, 0.0,
  3789. m[4] * sy, m[5] * sy, m[6] * sy, 0.0,
  3790. m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
  3791. 0.0, 0.0, 0.0, 1.0,
  3792. result
  3793. );
  3794. return this;
  3795. }
  3796. /**
  3797. * Toggles model matrix from being right handed to left handed in place and vice versa
  3798. */
  3799. public toggleModelMatrixHandInPlace() {
  3800. const m = this._m;
  3801. m[2] *= -1;
  3802. m[6] *= -1;
  3803. m[8] *= -1;
  3804. m[9] *= -1;
  3805. m[14] *= -1;
  3806. this._markAsUpdated();
  3807. }
  3808. /**
  3809. * Toggles projection matrix from being right handed to left handed in place and vice versa
  3810. */
  3811. public toggleProjectionMatrixHandInPlace() {
  3812. var m = this._m;
  3813. m[8] *= -1;
  3814. m[9] *= -1;
  3815. m[10] *= -1;
  3816. m[11] *= -1;
  3817. this._markAsUpdated();
  3818. }
  3819. // Statics
  3820. /**
  3821. * Creates a matrix from an array
  3822. * @param array defines the source array
  3823. * @param offset defines an offset in the source array
  3824. * @returns a new Matrix set from the starting index of the given array
  3825. */
  3826. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Matrix {
  3827. var result = new Matrix();
  3828. Matrix.FromArrayToRef(array, offset, result);
  3829. return result;
  3830. }
  3831. /**
  3832. * Copy the content of an array into a given matrix
  3833. * @param array defines the source array
  3834. * @param offset defines an offset in the source array
  3835. * @param result defines the target matrix
  3836. */
  3837. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Matrix) {
  3838. for (var index = 0; index < 16; index++) {
  3839. result._m[index] = array[index + offset];
  3840. }
  3841. result._markAsUpdated();
  3842. }
  3843. /**
  3844. * Stores an array into a matrix after having multiplied each component by a given factor
  3845. * @param array defines the source array
  3846. * @param offset defines the offset in the source array
  3847. * @param scale defines the scaling factor
  3848. * @param result defines the target matrix
  3849. */
  3850. public static FromFloat32ArrayToRefScaled(array: DeepImmutable<Float32Array>, offset: number, scale: number, result: Matrix) {
  3851. for (var index = 0; index < 16; index++) {
  3852. result._m[index] = array[index + offset] * scale;
  3853. }
  3854. result._markAsUpdated();
  3855. }
  3856. /**
  3857. * Gets an identity matrix that must not be updated
  3858. */
  3859. public static get IdentityReadOnly(): DeepImmutable<Matrix> {
  3860. return Matrix._identityReadOnly;
  3861. }
  3862. /**
  3863. * Stores a list of values (16) inside a given matrix
  3864. * @param initialM11 defines 1st value of 1st row
  3865. * @param initialM12 defines 2nd value of 1st row
  3866. * @param initialM13 defines 3rd value of 1st row
  3867. * @param initialM14 defines 4th value of 1st row
  3868. * @param initialM21 defines 1st value of 2nd row
  3869. * @param initialM22 defines 2nd value of 2nd row
  3870. * @param initialM23 defines 3rd value of 2nd row
  3871. * @param initialM24 defines 4th value of 2nd row
  3872. * @param initialM31 defines 1st value of 3rd row
  3873. * @param initialM32 defines 2nd value of 3rd row
  3874. * @param initialM33 defines 3rd value of 3rd row
  3875. * @param initialM34 defines 4th value of 3rd row
  3876. * @param initialM41 defines 1st value of 4th row
  3877. * @param initialM42 defines 2nd value of 4th row
  3878. * @param initialM43 defines 3rd value of 4th row
  3879. * @param initialM44 defines 4th value of 4th row
  3880. * @param result defines the target matrix
  3881. */
  3882. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  3883. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  3884. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  3885. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  3886. const m = result._m;
  3887. m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
  3888. m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
  3889. m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
  3890. m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
  3891. result._markAsUpdated();
  3892. }
  3893. /**
  3894. * Creates new matrix from a list of values (16)
  3895. * @param initialM11 defines 1st value of 1st row
  3896. * @param initialM12 defines 2nd value of 1st row
  3897. * @param initialM13 defines 3rd value of 1st row
  3898. * @param initialM14 defines 4th value of 1st row
  3899. * @param initialM21 defines 1st value of 2nd row
  3900. * @param initialM22 defines 2nd value of 2nd row
  3901. * @param initialM23 defines 3rd value of 2nd row
  3902. * @param initialM24 defines 4th value of 2nd row
  3903. * @param initialM31 defines 1st value of 3rd row
  3904. * @param initialM32 defines 2nd value of 3rd row
  3905. * @param initialM33 defines 3rd value of 3rd row
  3906. * @param initialM34 defines 4th value of 3rd row
  3907. * @param initialM41 defines 1st value of 4th row
  3908. * @param initialM42 defines 2nd value of 4th row
  3909. * @param initialM43 defines 3rd value of 4th row
  3910. * @param initialM44 defines 4th value of 4th row
  3911. * @returns the new matrix
  3912. */
  3913. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  3914. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  3915. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  3916. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  3917. var result = new Matrix();
  3918. const m = result._m;
  3919. m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
  3920. m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
  3921. m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
  3922. m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
  3923. result._markAsUpdated();
  3924. return result;
  3925. }
  3926. /**
  3927. * Creates a new matrix composed by merging scale (vector3), rotation (quaternion) and translation (vector3)
  3928. * @param scale defines the scale vector3
  3929. * @param rotation defines the rotation quaternion
  3930. * @param translation defines the translation vector3
  3931. * @returns a new matrix
  3932. */
  3933. public static Compose(scale: DeepImmutable<Vector3>, rotation: DeepImmutable<Quaternion>, translation: DeepImmutable<Vector3>): Matrix {
  3934. var result = new Matrix();
  3935. Matrix.ComposeToRef(scale, rotation, translation, result);
  3936. return result;
  3937. }
  3938. /**
  3939. * Sets a matrix to a value composed by merging scale (vector3), rotation (quaternion) and translation (vector3)
  3940. * @param scale defines the scale vector3
  3941. * @param rotation defines the rotation quaternion
  3942. * @param translation defines the translation vector3
  3943. * @param result defines the target matrix
  3944. */
  3945. public static ComposeToRef(scale: DeepImmutable<Vector3>, rotation: DeepImmutable<Quaternion>, translation: DeepImmutable<Vector3>, result: Matrix): void {
  3946. let m = result._m;
  3947. var x = rotation.x, y = rotation.y, z = rotation.z, w = rotation.w;
  3948. var x2 = x + x, y2 = y + y, z2 = z + z;
  3949. var xx = x * x2, xy = x * y2, xz = x * z2;
  3950. var yy = y * y2, yz = y * z2, zz = z * z2;
  3951. var wx = w * x2, wy = w * y2, wz = w * z2;
  3952. var sx = scale.x, sy = scale.y, sz = scale.z;
  3953. m[0] = (1 - (yy + zz)) * sx;
  3954. m[1] = (xy + wz) * sx;
  3955. m[2] = (xz - wy) * sx;
  3956. m[3] = 0;
  3957. m[4] = (xy - wz) * sy;
  3958. m[5] = (1 - (xx + zz)) * sy;
  3959. m[6] = (yz + wx) * sy;
  3960. m[7] = 0;
  3961. m[8] = (xz + wy) * sz;
  3962. m[9] = (yz - wx) * sz;
  3963. m[10] = (1 - (xx + yy)) * sz;
  3964. m[11] = 0;
  3965. m[12] = translation.x;
  3966. m[13] = translation.y;
  3967. m[14] = translation.z;
  3968. m[15] = 1;
  3969. result._markAsUpdated();
  3970. }
  3971. /**
  3972. * Creates a new identity matrix
  3973. * @returns a new identity matrix
  3974. */
  3975. public static Identity(): Matrix {
  3976. const identity = Matrix.FromValues(
  3977. 1.0, 0.0, 0.0, 0.0,
  3978. 0.0, 1.0, 0.0, 0.0,
  3979. 0.0, 0.0, 1.0, 0.0,
  3980. 0.0, 0.0, 0.0, 1.0);
  3981. identity._updateIdentityStatus(true);
  3982. return identity;
  3983. }
  3984. /**
  3985. * Creates a new identity matrix and stores the result in a given matrix
  3986. * @param result defines the target matrix
  3987. */
  3988. public static IdentityToRef(result: Matrix): void {
  3989. Matrix.FromValuesToRef(
  3990. 1.0, 0.0, 0.0, 0.0,
  3991. 0.0, 1.0, 0.0, 0.0,
  3992. 0.0, 0.0, 1.0, 0.0,
  3993. 0.0, 0.0, 0.0, 1.0,
  3994. result
  3995. );
  3996. result._updateIdentityStatus(true);
  3997. }
  3998. /**
  3999. * Creates a new zero matrix
  4000. * @returns a new zero matrix
  4001. */
  4002. public static Zero(): Matrix {
  4003. const zero = Matrix.FromValues(
  4004. 0.0, 0.0, 0.0, 0.0,
  4005. 0.0, 0.0, 0.0, 0.0,
  4006. 0.0, 0.0, 0.0, 0.0,
  4007. 0.0, 0.0, 0.0, 0.0);
  4008. zero._updateIdentityStatus(false);
  4009. return zero;
  4010. }
  4011. /**
  4012. * Creates a new rotation matrix for "angle" radians around the X axis
  4013. * @param angle defines the angle (in radians) to use
  4014. * @return the new matrix
  4015. */
  4016. public static RotationX(angle: number): Matrix {
  4017. var result = new Matrix();
  4018. Matrix.RotationXToRef(angle, result);
  4019. return result;
  4020. }
  4021. /**
  4022. * Creates a new matrix as the invert of a given matrix
  4023. * @param source defines the source matrix
  4024. * @returns the new matrix
  4025. */
  4026. public static Invert(source: DeepImmutable<Matrix>): Matrix {
  4027. var result = new Matrix();
  4028. source.invertToRef(result);
  4029. return result;
  4030. }
  4031. /**
  4032. * Creates a new rotation matrix for "angle" radians around the X axis and stores it in a given matrix
  4033. * @param angle defines the angle (in radians) to use
  4034. * @param result defines the target matrix
  4035. */
  4036. public static RotationXToRef(angle: number, result: Matrix): void {
  4037. var s = Math.sin(angle);
  4038. var c = Math.cos(angle);
  4039. Matrix.FromValuesToRef(
  4040. 1.0, 0.0, 0.0, 0.0,
  4041. 0.0, c, s, 0.0,
  4042. 0.0, -s, c, 0.0,
  4043. 0.0, 0.0, 0.0, 1.0,
  4044. result
  4045. );
  4046. result._updateIdentityStatus(c === 1 && s === 0);
  4047. }
  4048. /**
  4049. * Creates a new rotation matrix for "angle" radians around the Y axis
  4050. * @param angle defines the angle (in radians) to use
  4051. * @return the new matrix
  4052. */
  4053. public static RotationY(angle: number): Matrix {
  4054. var result = new Matrix();
  4055. Matrix.RotationYToRef(angle, result);
  4056. return result;
  4057. }
  4058. /**
  4059. * Creates a new rotation matrix for "angle" radians around the Y axis and stores it in a given matrix
  4060. * @param angle defines the angle (in radians) to use
  4061. * @param result defines the target matrix
  4062. */
  4063. public static RotationYToRef(angle: number, result: Matrix): void {
  4064. var s = Math.sin(angle);
  4065. var c = Math.cos(angle);
  4066. Matrix.FromValuesToRef(
  4067. c, 0.0, -s, 0.0,
  4068. 0.0, 1.0, 0.0, 0.0,
  4069. s, 0.0, c, 0.0,
  4070. 0.0, 0.0, 0.0, 1.0,
  4071. result
  4072. );
  4073. result._updateIdentityStatus(c === 1 && s === 0);
  4074. }
  4075. /**
  4076. * Creates a new rotation matrix for "angle" radians around the Z axis
  4077. * @param angle defines the angle (in radians) to use
  4078. * @return the new matrix
  4079. */
  4080. public static RotationZ(angle: number): Matrix {
  4081. var result = new Matrix();
  4082. Matrix.RotationZToRef(angle, result);
  4083. return result;
  4084. }
  4085. /**
  4086. * Creates a new rotation matrix for "angle" radians around the Z axis and stores it in a given matrix
  4087. * @param angle defines the angle (in radians) to use
  4088. * @param result defines the target matrix
  4089. */
  4090. public static RotationZToRef(angle: number, result: Matrix): void {
  4091. var s = Math.sin(angle);
  4092. var c = Math.cos(angle);
  4093. Matrix.FromValuesToRef(
  4094. c, s, 0.0, 0.0,
  4095. -s, c, 0.0, 0.0,
  4096. 0.0, 0.0, 1.0, 0.0,
  4097. 0.0, 0.0, 0.0, 1.0,
  4098. result
  4099. );
  4100. result._updateIdentityStatus(c === 1 && s === 0);
  4101. }
  4102. /**
  4103. * Creates a new rotation matrix for "angle" radians around the given axis
  4104. * @param axis defines the axis to use
  4105. * @param angle defines the angle (in radians) to use
  4106. * @return the new matrix
  4107. */
  4108. public static RotationAxis(axis: DeepImmutable<Vector3>, angle: number): Matrix {
  4109. var result = new Matrix();
  4110. Matrix.RotationAxisToRef(axis, angle, result);
  4111. return result;
  4112. }
  4113. /**
  4114. * Creates a new rotation matrix for "angle" radians around the given axis and stores it in a given matrix
  4115. * @param axis defines the axis to use
  4116. * @param angle defines the angle (in radians) to use
  4117. * @param result defines the target matrix
  4118. */
  4119. public static RotationAxisToRef(axis: DeepImmutable<Vector3>, angle: number, result: Matrix): void {
  4120. var s = Math.sin(-angle);
  4121. var c = Math.cos(-angle);
  4122. var c1 = 1 - c;
  4123. axis.normalize();
  4124. const m = result._m;
  4125. m[0] = (axis.x * axis.x) * c1 + c;
  4126. m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  4127. m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  4128. m[3] = 0.0;
  4129. m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  4130. m[5] = (axis.y * axis.y) * c1 + c;
  4131. m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  4132. m[7] = 0.0;
  4133. m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  4134. m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  4135. m[10] = (axis.z * axis.z) * c1 + c;
  4136. m[11] = 0.0;
  4137. m[12] = 0.0;
  4138. m[13] = 0.0;
  4139. m[14] = 0.0;
  4140. m[15] = 1.0;
  4141. result._markAsUpdated();
  4142. }
  4143. /**
  4144. * Takes normalised vectors and returns a rotation matrix to align "from" with "to".
  4145. * Taken from http://www.iquilezles.org/www/articles/noacos/noacos.htm
  4146. * @param from defines the vector to align
  4147. * @param to defines the vector to align to
  4148. * @param result defines the target matrix
  4149. */
  4150. public static RotationAlignToRef(from: DeepImmutable<Vector3>, to: DeepImmutable<Vector3>, result: Matrix): void {
  4151. const v = Vector3.Cross(to, from);
  4152. const c = Vector3.Dot(to, from);
  4153. const k = 1 / (1 + c);
  4154. const m = result._m;
  4155. m[0] = v.x * v.x * k + c; m[1] = v.y * v.x * k - v.z; m[2] = v.z * v.x * k + v.y; m[3] = 0;
  4156. m[4] = v.x * v.y * k + v.z; m[5] = v.y * v.y * k + c; m[6] = v.z * v.y * k - v.x; m[7] = 0;
  4157. m[8] = v.x * v.z * k - v.y; m[9] = v.y * v.z * k + v.x; m[10] = v.z * v.z * k + c; m[11] = 0;
  4158. m[12] = 0; m[13] = 0; m[14] = 0; m[15] = 1;
  4159. result._markAsUpdated();
  4160. }
  4161. /**
  4162. * Creates a rotation matrix
  4163. * @param yaw defines the yaw angle in radians (Y axis)
  4164. * @param pitch defines the pitch angle in radians (X axis)
  4165. * @param roll defines the roll angle in radians (X axis)
  4166. * @returns the new rotation matrix
  4167. */
  4168. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  4169. var result = new Matrix();
  4170. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  4171. return result;
  4172. }
  4173. /**
  4174. * Creates a rotation matrix and stores it in a given matrix
  4175. * @param yaw defines the yaw angle in radians (Y axis)
  4176. * @param pitch defines the pitch angle in radians (X axis)
  4177. * @param roll defines the roll angle in radians (X axis)
  4178. * @param result defines the target matrix
  4179. */
  4180. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  4181. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, MathTmp.Quaternion[0]);
  4182. MathTmp.Quaternion[0].toRotationMatrix(result);
  4183. }
  4184. /**
  4185. * Creates a scaling matrix
  4186. * @param x defines the scale factor on X axis
  4187. * @param y defines the scale factor on Y axis
  4188. * @param z defines the scale factor on Z axis
  4189. * @returns the new matrix
  4190. */
  4191. public static Scaling(x: number, y: number, z: number): Matrix {
  4192. var result = new Matrix();
  4193. Matrix.ScalingToRef(x, y, z, result);
  4194. return result;
  4195. }
  4196. /**
  4197. * Creates a scaling matrix and stores it in a given matrix
  4198. * @param x defines the scale factor on X axis
  4199. * @param y defines the scale factor on Y axis
  4200. * @param z defines the scale factor on Z axis
  4201. * @param result defines the target matrix
  4202. */
  4203. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  4204. Matrix.FromValuesToRef(
  4205. x, 0.0, 0.0, 0.0,
  4206. 0.0, y, 0.0, 0.0,
  4207. 0.0, 0.0, z, 0.0,
  4208. 0.0, 0.0, 0.0, 1.0,
  4209. result
  4210. );
  4211. result._updateIdentityStatus(x === 1 && y === 1 && z === 1);
  4212. }
  4213. /**
  4214. * Creates a translation matrix
  4215. * @param x defines the translation on X axis
  4216. * @param y defines the translation on Y axis
  4217. * @param z defines the translationon Z axis
  4218. * @returns the new matrix
  4219. */
  4220. public static Translation(x: number, y: number, z: number): Matrix {
  4221. var result = new Matrix();
  4222. Matrix.TranslationToRef(x, y, z, result);
  4223. return result;
  4224. }
  4225. /**
  4226. * Creates a translation matrix and stores it in a given matrix
  4227. * @param x defines the translation on X axis
  4228. * @param y defines the translation on Y axis
  4229. * @param z defines the translationon Z axis
  4230. * @param result defines the target matrix
  4231. */
  4232. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  4233. Matrix.FromValuesToRef(
  4234. 1.0, 0.0, 0.0, 0.0,
  4235. 0.0, 1.0, 0.0, 0.0,
  4236. 0.0, 0.0, 1.0, 0.0,
  4237. x, y, z, 1.0,
  4238. result
  4239. );
  4240. result._updateIdentityStatus(x === 0 && y === 0 && z === 0);
  4241. }
  4242. /**
  4243. * Returns a new Matrix whose values are the interpolated values for "gradient" (float) between the ones of the matrices "startValue" and "endValue".
  4244. * @param startValue defines the start value
  4245. * @param endValue defines the end value
  4246. * @param gradient defines the gradient factor
  4247. * @returns the new matrix
  4248. */
  4249. public static Lerp(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number): Matrix {
  4250. var result = new Matrix();
  4251. Matrix.LerpToRef(startValue, endValue, gradient, result);
  4252. return result;
  4253. }
  4254. /**
  4255. * Set the given matrix "result" as the interpolated values for "gradient" (float) between the ones of the matrices "startValue" and "endValue".
  4256. * @param startValue defines the start value
  4257. * @param endValue defines the end value
  4258. * @param gradient defines the gradient factor
  4259. * @param result defines the Matrix object where to store data
  4260. */
  4261. public static LerpToRef(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number, result: Matrix): void {
  4262. const resultM = result._m;
  4263. const startM = startValue.m;
  4264. const endM = endValue.m;
  4265. for (var index = 0; index < 16; index++) {
  4266. resultM[index] = startM[index] * (1.0 - gradient) + endM[index] * gradient;
  4267. }
  4268. result._markAsUpdated();
  4269. }
  4270. /**
  4271. * Builds a new matrix whose values are computed by:
  4272. * * decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices
  4273. * * interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end
  4274. * * recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices
  4275. * @param startValue defines the first matrix
  4276. * @param endValue defines the second matrix
  4277. * @param gradient defines the gradient between the two matrices
  4278. * @returns the new matrix
  4279. */
  4280. public static DecomposeLerp(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number): Matrix {
  4281. var result = new Matrix();
  4282. Matrix.DecomposeLerpToRef(startValue, endValue, gradient, result);
  4283. return result;
  4284. }
  4285. /**
  4286. * Update a matrix to values which are computed by:
  4287. * * decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices
  4288. * * interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end
  4289. * * recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices
  4290. * @param startValue defines the first matrix
  4291. * @param endValue defines the second matrix
  4292. * @param gradient defines the gradient between the two matrices
  4293. * @param result defines the target matrix
  4294. */
  4295. public static DecomposeLerpToRef(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number, result: Matrix) {
  4296. var startScale = MathTmp.Vector3[0];
  4297. var startRotation = MathTmp.Quaternion[0];
  4298. var startTranslation = MathTmp.Vector3[1];
  4299. startValue.decompose(startScale, startRotation, startTranslation);
  4300. var endScale = MathTmp.Vector3[2];
  4301. var endRotation = MathTmp.Quaternion[1];
  4302. var endTranslation = MathTmp.Vector3[3];
  4303. endValue.decompose(endScale, endRotation, endTranslation);
  4304. var resultScale = MathTmp.Vector3[4];
  4305. Vector3.LerpToRef(startScale, endScale, gradient, resultScale);
  4306. var resultRotation = MathTmp.Quaternion[2];
  4307. Quaternion.SlerpToRef(startRotation, endRotation, gradient, resultRotation);
  4308. var resultTranslation = MathTmp.Vector3[5];
  4309. Vector3.LerpToRef(startTranslation, endTranslation, gradient, resultTranslation);
  4310. Matrix.ComposeToRef(resultScale, resultRotation, resultTranslation, result);
  4311. }
  4312. /**
  4313. * Gets a new rotation matrix used to rotate an entity so as it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up"
  4314. * This function works in left handed mode
  4315. * @param eye defines the final position of the entity
  4316. * @param target defines where the entity should look at
  4317. * @param up defines the up vector for the entity
  4318. * @returns the new matrix
  4319. */
  4320. public static LookAtLH(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>): Matrix {
  4321. var result = new Matrix();
  4322. Matrix.LookAtLHToRef(eye, target, up, result);
  4323. return result;
  4324. }
  4325. /**
  4326. * Sets the given "result" Matrix to a rotation matrix used to rotate an entity so that it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up".
  4327. * This function works in left handed mode
  4328. * @param eye defines the final position of the entity
  4329. * @param target defines where the entity should look at
  4330. * @param up defines the up vector for the entity
  4331. * @param result defines the target matrix
  4332. */
  4333. public static LookAtLHToRef(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: Matrix): void {
  4334. const xAxis = MathTmp.Vector3[0];
  4335. const yAxis = MathTmp.Vector3[1];
  4336. const zAxis = MathTmp.Vector3[2];
  4337. // Z axis
  4338. target.subtractToRef(eye, zAxis);
  4339. zAxis.normalize();
  4340. // X axis
  4341. Vector3.CrossToRef(up, zAxis, xAxis);
  4342. const xSquareLength = xAxis.lengthSquared();
  4343. if (xSquareLength === 0) {
  4344. xAxis.x = 1.0;
  4345. } else {
  4346. xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
  4347. }
  4348. // Y axis
  4349. Vector3.CrossToRef(zAxis, xAxis, yAxis);
  4350. yAxis.normalize();
  4351. // Eye angles
  4352. var ex = -Vector3.Dot(xAxis, eye);
  4353. var ey = -Vector3.Dot(yAxis, eye);
  4354. var ez = -Vector3.Dot(zAxis, eye);
  4355. Matrix.FromValuesToRef(
  4356. xAxis.x, yAxis.x, zAxis.x, 0.0,
  4357. xAxis.y, yAxis.y, zAxis.y, 0.0,
  4358. xAxis.z, yAxis.z, zAxis.z, 0.0,
  4359. ex, ey, ez, 1.0,
  4360. result
  4361. );
  4362. }
  4363. /**
  4364. * Gets a new rotation matrix used to rotate an entity so as it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up"
  4365. * This function works in right handed mode
  4366. * @param eye defines the final position of the entity
  4367. * @param target defines where the entity should look at
  4368. * @param up defines the up vector for the entity
  4369. * @returns the new matrix
  4370. */
  4371. public static LookAtRH(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>): Matrix {
  4372. var result = new Matrix();
  4373. Matrix.LookAtRHToRef(eye, target, up, result);
  4374. return result;
  4375. }
  4376. /**
  4377. * Sets the given "result" Matrix to a rotation matrix used to rotate an entity so that it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up".
  4378. * This function works in right handed mode
  4379. * @param eye defines the final position of the entity
  4380. * @param target defines where the entity should look at
  4381. * @param up defines the up vector for the entity
  4382. * @param result defines the target matrix
  4383. */
  4384. public static LookAtRHToRef(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: Matrix): void {
  4385. const xAxis = MathTmp.Vector3[0];
  4386. const yAxis = MathTmp.Vector3[1];
  4387. const zAxis = MathTmp.Vector3[2];
  4388. // Z axis
  4389. eye.subtractToRef(target, zAxis);
  4390. zAxis.normalize();
  4391. // X axis
  4392. Vector3.CrossToRef(up, zAxis, xAxis);
  4393. const xSquareLength = xAxis.lengthSquared();
  4394. if (xSquareLength === 0) {
  4395. xAxis.x = 1.0;
  4396. } else {
  4397. xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
  4398. }
  4399. // Y axis
  4400. Vector3.CrossToRef(zAxis, xAxis, yAxis);
  4401. yAxis.normalize();
  4402. // Eye angles
  4403. var ex = -Vector3.Dot(xAxis, eye);
  4404. var ey = -Vector3.Dot(yAxis, eye);
  4405. var ez = -Vector3.Dot(zAxis, eye);
  4406. Matrix.FromValuesToRef(
  4407. xAxis.x, yAxis.x, zAxis.x, 0.0,
  4408. xAxis.y, yAxis.y, zAxis.y, 0.0,
  4409. xAxis.z, yAxis.z, zAxis.z, 0.0,
  4410. ex, ey, ez, 1.0,
  4411. result
  4412. );
  4413. }
  4414. /**
  4415. * Create a left-handed orthographic projection matrix
  4416. * @param width defines the viewport width
  4417. * @param height defines the viewport height
  4418. * @param znear defines the near clip plane
  4419. * @param zfar defines the far clip plane
  4420. * @returns a new matrix as a left-handed orthographic projection matrix
  4421. */
  4422. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  4423. var matrix = new Matrix();
  4424. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  4425. return matrix;
  4426. }
  4427. /**
  4428. * Store a left-handed orthographic projection to a given matrix
  4429. * @param width defines the viewport width
  4430. * @param height defines the viewport height
  4431. * @param znear defines the near clip plane
  4432. * @param zfar defines the far clip plane
  4433. * @param result defines the target matrix
  4434. */
  4435. public static OrthoLHToRef(width: number, height: number, znear: number, zfar: number, result: Matrix): void {
  4436. let n = znear;
  4437. let f = zfar;
  4438. let a = 2.0 / width;
  4439. let b = 2.0 / height;
  4440. let c = 2.0 / (f - n);
  4441. let d = -(f + n) / (f - n);
  4442. Matrix.FromValuesToRef(
  4443. a, 0.0, 0.0, 0.0,
  4444. 0.0, b, 0.0, 0.0,
  4445. 0.0, 0.0, c, 0.0,
  4446. 0.0, 0.0, d, 1.0,
  4447. result
  4448. );
  4449. result._updateIdentityStatus(a === 1 && b === 1 && c === 1 && d === 0);
  4450. }
  4451. /**
  4452. * Create a left-handed orthographic projection matrix
  4453. * @param left defines the viewport left coordinate
  4454. * @param right defines the viewport right coordinate
  4455. * @param bottom defines the viewport bottom coordinate
  4456. * @param top defines the viewport top coordinate
  4457. * @param znear defines the near clip plane
  4458. * @param zfar defines the far clip plane
  4459. * @returns a new matrix as a left-handed orthographic projection matrix
  4460. */
  4461. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  4462. var matrix = new Matrix();
  4463. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  4464. return matrix;
  4465. }
  4466. /**
  4467. * Stores a left-handed orthographic projection into a given matrix
  4468. * @param left defines the viewport left coordinate
  4469. * @param right defines the viewport right coordinate
  4470. * @param bottom defines the viewport bottom coordinate
  4471. * @param top defines the viewport top coordinate
  4472. * @param znear defines the near clip plane
  4473. * @param zfar defines the far clip plane
  4474. * @param result defines the target matrix
  4475. */
  4476. public static OrthoOffCenterLHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  4477. let n = znear;
  4478. let f = zfar;
  4479. let a = 2.0 / (right - left);
  4480. let b = 2.0 / (top - bottom);
  4481. let c = 2.0 / (f - n);
  4482. let d = -(f + n) / (f - n);
  4483. let i0 = (left + right) / (left - right);
  4484. let i1 = (top + bottom) / (bottom - top);
  4485. Matrix.FromValuesToRef(
  4486. a, 0.0, 0.0, 0.0,
  4487. 0.0, b, 0.0, 0.0,
  4488. 0.0, 0.0, c, 0.0,
  4489. i0, i1, d, 1.0,
  4490. result
  4491. );
  4492. result._markAsUpdated();
  4493. }
  4494. /**
  4495. * Creates a right-handed orthographic projection matrix
  4496. * @param left defines the viewport left coordinate
  4497. * @param right defines the viewport right coordinate
  4498. * @param bottom defines the viewport bottom coordinate
  4499. * @param top defines the viewport top coordinate
  4500. * @param znear defines the near clip plane
  4501. * @param zfar defines the far clip plane
  4502. * @returns a new matrix as a right-handed orthographic projection matrix
  4503. */
  4504. public static OrthoOffCenterRH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  4505. var matrix = new Matrix();
  4506. Matrix.OrthoOffCenterRHToRef(left, right, bottom, top, znear, zfar, matrix);
  4507. return matrix;
  4508. }
  4509. /**
  4510. * Stores a right-handed orthographic projection into a given matrix
  4511. * @param left defines the viewport left coordinate
  4512. * @param right defines the viewport right coordinate
  4513. * @param bottom defines the viewport bottom coordinate
  4514. * @param top defines the viewport top coordinate
  4515. * @param znear defines the near clip plane
  4516. * @param zfar defines the far clip plane
  4517. * @param result defines the target matrix
  4518. */
  4519. public static OrthoOffCenterRHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  4520. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, result);
  4521. result._m[10] *= -1; // No need to call _markAsUpdated as previous function already called it and let _isIdentityDirty to true
  4522. }
  4523. /**
  4524. * Creates a left-handed perspective projection matrix
  4525. * @param width defines the viewport width
  4526. * @param height defines the viewport height
  4527. * @param znear defines the near clip plane
  4528. * @param zfar defines the far clip plane
  4529. * @returns a new matrix as a left-handed perspective projection matrix
  4530. */
  4531. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  4532. var matrix = new Matrix();
  4533. let n = znear;
  4534. let f = zfar;
  4535. let a = 2.0 * n / width;
  4536. let b = 2.0 * n / height;
  4537. let c = (f + n) / (f - n);
  4538. let d = -2.0 * f * n / (f - n);
  4539. Matrix.FromValuesToRef(
  4540. a, 0.0, 0.0, 0.0,
  4541. 0.0, b, 0.0, 0.0,
  4542. 0.0, 0.0, c, 1.0,
  4543. 0.0, 0.0, d, 0.0,
  4544. matrix
  4545. );
  4546. matrix._updateIdentityStatus(false);
  4547. return matrix;
  4548. }
  4549. /**
  4550. * Creates a left-handed perspective projection matrix
  4551. * @param fov defines the horizontal field of view
  4552. * @param aspect defines the aspect ratio
  4553. * @param znear defines the near clip plane
  4554. * @param zfar defines the far clip plane
  4555. * @returns a new matrix as a left-handed perspective projection matrix
  4556. */
  4557. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  4558. var matrix = new Matrix();
  4559. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  4560. return matrix;
  4561. }
  4562. /**
  4563. * Stores a left-handed perspective projection into a given matrix
  4564. * @param fov defines the horizontal field of view
  4565. * @param aspect defines the aspect ratio
  4566. * @param znear defines the near clip plane
  4567. * @param zfar defines the far clip plane
  4568. * @param result defines the target matrix
  4569. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  4570. */
  4571. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  4572. let n = znear;
  4573. let f = zfar;
  4574. let t = 1.0 / (Math.tan(fov * 0.5));
  4575. let a = isVerticalFovFixed ? (t / aspect) : t;
  4576. let b = isVerticalFovFixed ? t : (t * aspect);
  4577. let c = (f + n) / (f - n);
  4578. let d = -2.0 * f * n / (f - n);
  4579. Matrix.FromValuesToRef(
  4580. a, 0.0, 0.0, 0.0,
  4581. 0.0, b, 0.0, 0.0,
  4582. 0.0, 0.0, c, 1.0,
  4583. 0.0, 0.0, d, 0.0,
  4584. result
  4585. );
  4586. result._updateIdentityStatus(false);
  4587. }
  4588. /**
  4589. * Stores a left-handed perspective projection into a given matrix with depth reversed
  4590. * @param fov defines the horizontal field of view
  4591. * @param aspect defines the aspect ratio
  4592. * @param znear defines the near clip plane
  4593. * @param zfar not used as infinity is used as far clip
  4594. * @param result defines the target matrix
  4595. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  4596. */
  4597. public static PerspectiveFovReverseLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  4598. let t = 1.0 / (Math.tan(fov * 0.5));
  4599. let a = isVerticalFovFixed ? (t / aspect) : t;
  4600. let b = isVerticalFovFixed ? t : (t * aspect);
  4601. Matrix.FromValuesToRef(
  4602. a, 0.0, 0.0, 0.0,
  4603. 0.0, b, 0.0, 0.0,
  4604. 0.0, 0.0, -znear, 1.0,
  4605. 0.0, 0.0, 1.0, 0.0,
  4606. result
  4607. );
  4608. result._updateIdentityStatus(false);
  4609. }
  4610. /**
  4611. * Creates a right-handed perspective projection matrix
  4612. * @param fov defines the horizontal field of view
  4613. * @param aspect defines the aspect ratio
  4614. * @param znear defines the near clip plane
  4615. * @param zfar defines the far clip plane
  4616. * @returns a new matrix as a right-handed perspective projection matrix
  4617. */
  4618. public static PerspectiveFovRH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  4619. var matrix = new Matrix();
  4620. Matrix.PerspectiveFovRHToRef(fov, aspect, znear, zfar, matrix);
  4621. return matrix;
  4622. }
  4623. /**
  4624. * Stores a right-handed perspective projection into a given matrix
  4625. * @param fov defines the horizontal field of view
  4626. * @param aspect defines the aspect ratio
  4627. * @param znear defines the near clip plane
  4628. * @param zfar defines the far clip plane
  4629. * @param result defines the target matrix
  4630. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  4631. */
  4632. public static PerspectiveFovRHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  4633. //alternatively this could be expressed as:
  4634. // m = PerspectiveFovLHToRef
  4635. // m[10] *= -1.0;
  4636. // m[11] *= -1.0;
  4637. let n = znear;
  4638. let f = zfar;
  4639. let t = 1.0 / (Math.tan(fov * 0.5));
  4640. let a = isVerticalFovFixed ? (t / aspect) : t;
  4641. let b = isVerticalFovFixed ? t : (t * aspect);
  4642. let c = -(f + n) / (f - n);
  4643. let d = -2 * f * n / (f - n);
  4644. Matrix.FromValuesToRef(
  4645. a, 0.0, 0.0, 0.0,
  4646. 0.0, b, 0.0, 0.0,
  4647. 0.0, 0.0, c, -1.0,
  4648. 0.0, 0.0, d, 0.0,
  4649. result
  4650. );
  4651. result._updateIdentityStatus(false);
  4652. }
  4653. /**
  4654. * Stores a right-handed perspective projection into a given matrix
  4655. * @param fov defines the horizontal field of view
  4656. * @param aspect defines the aspect ratio
  4657. * @param znear defines the near clip plane
  4658. * @param zfar not used as infinity is used as far clip
  4659. * @param result defines the target matrix
  4660. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  4661. */
  4662. public static PerspectiveFovReverseRHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  4663. //alternatively this could be expressed as:
  4664. // m = PerspectiveFovLHToRef
  4665. // m[10] *= -1.0;
  4666. // m[11] *= -1.0;
  4667. let t = 1.0 / (Math.tan(fov * 0.5));
  4668. let a = isVerticalFovFixed ? (t / aspect) : t;
  4669. let b = isVerticalFovFixed ? t : (t * aspect);
  4670. Matrix.FromValuesToRef(
  4671. a, 0.0, 0.0, 0.0,
  4672. 0.0, b, 0.0, 0.0,
  4673. 0.0, 0.0, -znear, -1.0,
  4674. 0.0, 0.0, -1.0, 0.0,
  4675. result
  4676. );
  4677. result._updateIdentityStatus(false);
  4678. }
  4679. /**
  4680. * Stores a perspective projection for WebVR info a given matrix
  4681. * @param fov defines the field of view
  4682. * @param znear defines the near clip plane
  4683. * @param zfar defines the far clip plane
  4684. * @param result defines the target matrix
  4685. * @param rightHanded defines if the matrix must be in right-handed mode (false by default)
  4686. */
  4687. public static PerspectiveFovWebVRToRef(fov: { upDegrees: number, downDegrees: number, leftDegrees: number, rightDegrees: number }, znear: number, zfar: number, result: Matrix, rightHanded = false): void {
  4688. var rightHandedFactor = rightHanded ? -1 : 1;
  4689. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  4690. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  4691. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  4692. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  4693. var xScale = 2.0 / (leftTan + rightTan);
  4694. var yScale = 2.0 / (upTan + downTan);
  4695. const m = result._m;
  4696. m[0] = xScale;
  4697. m[1] = m[2] = m[3] = m[4] = 0.0;
  4698. m[5] = yScale;
  4699. m[6] = m[7] = 0.0;
  4700. m[8] = ((leftTan - rightTan) * xScale * 0.5);
  4701. m[9] = -((upTan - downTan) * yScale * 0.5);
  4702. m[10] = -zfar / (znear - zfar);
  4703. m[11] = 1.0 * rightHandedFactor;
  4704. m[12] = m[13] = m[15] = 0.0;
  4705. m[14] = -(2.0 * zfar * znear) / (zfar - znear);
  4706. result._markAsUpdated();
  4707. }
  4708. /**
  4709. * Computes a complete transformation matrix
  4710. * @param viewport defines the viewport to use
  4711. * @param world defines the world matrix
  4712. * @param view defines the view matrix
  4713. * @param projection defines the projection matrix
  4714. * @param zmin defines the near clip plane
  4715. * @param zmax defines the far clip plane
  4716. * @returns the transformation matrix
  4717. */
  4718. public static GetFinalMatrix(viewport: DeepImmutable<Viewport>, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, zmin: number, zmax: number): Matrix {
  4719. var cw = viewport.width;
  4720. var ch = viewport.height;
  4721. var cx = viewport.x;
  4722. var cy = viewport.y;
  4723. var viewportMatrix = Matrix.FromValues(
  4724. cw / 2.0, 0.0, 0.0, 0.0,
  4725. 0.0, -ch / 2.0, 0.0, 0.0,
  4726. 0.0, 0.0, zmax - zmin, 0.0,
  4727. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1.0);
  4728. var matrix = MathTmp.Matrix[0];
  4729. world.multiplyToRef(view, matrix);
  4730. matrix.multiplyToRef(projection, matrix);
  4731. return matrix.multiply(viewportMatrix);
  4732. }
  4733. /**
  4734. * Extracts a 2x2 matrix from a given matrix and store the result in a Float32Array
  4735. * @param matrix defines the matrix to use
  4736. * @returns a new Float32Array array with 4 elements : the 2x2 matrix extracted from the given matrix
  4737. */
  4738. public static GetAsMatrix2x2(matrix: DeepImmutable<Matrix>): Float32Array {
  4739. const m = matrix.m;
  4740. return new Float32Array([m[0], m[1], m[4], m[5]]);
  4741. }
  4742. /**
  4743. * Extracts a 3x3 matrix from a given matrix and store the result in a Float32Array
  4744. * @param matrix defines the matrix to use
  4745. * @returns a new Float32Array array with 9 elements : the 3x3 matrix extracted from the given matrix
  4746. */
  4747. public static GetAsMatrix3x3(matrix: DeepImmutable<Matrix>): Float32Array {
  4748. const m = matrix.m;
  4749. return new Float32Array([
  4750. m[0], m[1], m[2],
  4751. m[4], m[5], m[6],
  4752. m[8], m[9], m[10]
  4753. ]);
  4754. }
  4755. /**
  4756. * Compute the transpose of a given matrix
  4757. * @param matrix defines the matrix to transpose
  4758. * @returns the new matrix
  4759. */
  4760. public static Transpose(matrix: DeepImmutable<Matrix>): Matrix {
  4761. var result = new Matrix();
  4762. Matrix.TransposeToRef(matrix, result);
  4763. return result;
  4764. }
  4765. /**
  4766. * Compute the transpose of a matrix and store it in a target matrix
  4767. * @param matrix defines the matrix to transpose
  4768. * @param result defines the target matrix
  4769. */
  4770. public static TransposeToRef(matrix: DeepImmutable<Matrix>, result: Matrix): void {
  4771. const rm = result._m;
  4772. const mm = matrix.m;
  4773. rm[0] = mm[0];
  4774. rm[1] = mm[4];
  4775. rm[2] = mm[8];
  4776. rm[3] = mm[12];
  4777. rm[4] = mm[1];
  4778. rm[5] = mm[5];
  4779. rm[6] = mm[9];
  4780. rm[7] = mm[13];
  4781. rm[8] = mm[2];
  4782. rm[9] = mm[6];
  4783. rm[10] = mm[10];
  4784. rm[11] = mm[14];
  4785. rm[12] = mm[3];
  4786. rm[13] = mm[7];
  4787. rm[14] = mm[11];
  4788. rm[15] = mm[15];
  4789. // identity-ness does not change when transposing
  4790. result._updateIdentityStatus((matrix as Matrix)._isIdentity, (matrix as Matrix)._isIdentityDirty);
  4791. }
  4792. /**
  4793. * Computes a reflection matrix from a plane
  4794. * @param plane defines the reflection plane
  4795. * @returns a new matrix
  4796. */
  4797. public static Reflection(plane: DeepImmutable<IPlaneLike>): Matrix {
  4798. var matrix = new Matrix();
  4799. Matrix.ReflectionToRef(plane, matrix);
  4800. return matrix;
  4801. }
  4802. /**
  4803. * Computes a reflection matrix from a plane
  4804. * @param plane defines the reflection plane
  4805. * @param result defines the target matrix
  4806. */
  4807. public static ReflectionToRef(plane: DeepImmutable<IPlaneLike>, result: Matrix): void {
  4808. plane.normalize();
  4809. var x = plane.normal.x;
  4810. var y = plane.normal.y;
  4811. var z = plane.normal.z;
  4812. var temp = -2 * x;
  4813. var temp2 = -2 * y;
  4814. var temp3 = -2 * z;
  4815. Matrix.FromValuesToRef(
  4816. temp * x + 1, temp2 * x, temp3 * x, 0.0,
  4817. temp * y, temp2 * y + 1, temp3 * y, 0.0,
  4818. temp * z, temp2 * z, temp3 * z + 1, 0.0,
  4819. temp * plane.d, temp2 * plane.d, temp3 * plane.d, 1.0,
  4820. result
  4821. );
  4822. }
  4823. /**
  4824. * Sets the given matrix as a rotation matrix composed from the 3 left handed axes
  4825. * @param xaxis defines the value of the 1st axis
  4826. * @param yaxis defines the value of the 2nd axis
  4827. * @param zaxis defines the value of the 3rd axis
  4828. * @param result defines the target matrix
  4829. */
  4830. public static FromXYZAxesToRef(xaxis: DeepImmutable<Vector3>, yaxis: DeepImmutable<Vector3>, zaxis: DeepImmutable<Vector3>, result: Matrix) {
  4831. Matrix.FromValuesToRef(
  4832. xaxis.x, xaxis.y, xaxis.z, 0.0,
  4833. yaxis.x, yaxis.y, yaxis.z, 0.0,
  4834. zaxis.x, zaxis.y, zaxis.z, 0.0,
  4835. 0.0, 0.0, 0.0, 1.0,
  4836. result
  4837. );
  4838. }
  4839. /**
  4840. * Creates a rotation matrix from a quaternion and stores it in a target matrix
  4841. * @param quat defines the quaternion to use
  4842. * @param result defines the target matrix
  4843. */
  4844. public static FromQuaternionToRef(quat: DeepImmutable<Quaternion>, result: Matrix) {
  4845. var xx = quat.x * quat.x;
  4846. var yy = quat.y * quat.y;
  4847. var zz = quat.z * quat.z;
  4848. var xy = quat.x * quat.y;
  4849. var zw = quat.z * quat.w;
  4850. var zx = quat.z * quat.x;
  4851. var yw = quat.y * quat.w;
  4852. var yz = quat.y * quat.z;
  4853. var xw = quat.x * quat.w;
  4854. result._m[0] = 1.0 - (2.0 * (yy + zz));
  4855. result._m[1] = 2.0 * (xy + zw);
  4856. result._m[2] = 2.0 * (zx - yw);
  4857. result._m[3] = 0.0;
  4858. result._m[4] = 2.0 * (xy - zw);
  4859. result._m[5] = 1.0 - (2.0 * (zz + xx));
  4860. result._m[6] = 2.0 * (yz + xw);
  4861. result._m[7] = 0.0;
  4862. result._m[8] = 2.0 * (zx + yw);
  4863. result._m[9] = 2.0 * (yz - xw);
  4864. result._m[10] = 1.0 - (2.0 * (yy + xx));
  4865. result._m[11] = 0.0;
  4866. result._m[12] = 0.0;
  4867. result._m[13] = 0.0;
  4868. result._m[14] = 0.0;
  4869. result._m[15] = 1.0;
  4870. result._markAsUpdated();
  4871. }
  4872. }
  4873. /**
  4874. * @hidden
  4875. * Same as Tmp but not exported to keep it only for math functions to avoid conflicts
  4876. */
  4877. class MathTmp {
  4878. public static Vector3: Vector3[] = ArrayTools.BuildArray(6, Vector3.Zero);
  4879. public static Matrix: Matrix[] = ArrayTools.BuildArray(2, Matrix.Identity);
  4880. public static Quaternion: Quaternion[] = ArrayTools.BuildArray(3, Quaternion.Zero);
  4881. }
  4882. /**
  4883. * @hidden
  4884. */
  4885. export class TmpVectors {
  4886. public static Vector2: Vector2[] = ArrayTools.BuildArray(3, Vector2.Zero); // 3 temp Vector2 at once should be enough
  4887. public static Vector3: Vector3[] = ArrayTools.BuildArray(13, Vector3.Zero); // 13 temp Vector3 at once should be enough
  4888. public static Vector4: Vector4[] = ArrayTools.BuildArray(3, Vector4.Zero); // 3 temp Vector4 at once should be enough
  4889. public static Quaternion: Quaternion[] = ArrayTools.BuildArray(2, Quaternion.Zero); // 2 temp Quaternion at once should be enough
  4890. public static Matrix: Matrix[] = ArrayTools.BuildArray(8, Matrix.Identity); // 8 temp Matrices at once should be enough
  4891. }
  4892. _TypeStore.RegisteredTypes["BABYLON.Vector2"] = Vector2;
  4893. _TypeStore.RegisteredTypes["BABYLON.Vector3"] = Vector3;
  4894. _TypeStore.RegisteredTypes["BABYLON.Vector4"] = Vector4;
  4895. _TypeStore.RegisteredTypes["BABYLON.Matrix"] = Matrix;