math.vector.ts 210 KB

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