math.vector.ts 206 KB

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