math.vector.ts 209 KB

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