math.vector.ts 211 KB

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