math.ts 281 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580
  1. import { DeepImmutable, Nullable, FloatArray, float } from "../types";
  2. import { ArrayTools } from "../Misc/arrayTools";
  3. import { Scalar } from "./math.scalar";
  4. /**
  5. * Constant used to convert a value to gamma space
  6. * @ignorenaming
  7. */
  8. export const ToGammaSpace = 1 / 2.2;
  9. /**
  10. * Constant used to convert a value to linear space
  11. * @ignorenaming
  12. */
  13. export const ToLinearSpace = 2.2;
  14. /**
  15. * Constant used to define the minimal number value in Babylon.js
  16. * @ignorenaming
  17. */
  18. let Epsilon = 0.001;
  19. export { Epsilon };
  20. /**
  21. * Class used to hold a RBG color
  22. */
  23. export class Color3 {
  24. /**
  25. * Creates a new Color3 object from red, green, blue values, all between 0 and 1
  26. * @param r defines the red component (between 0 and 1, default is 0)
  27. * @param g defines the green component (between 0 and 1, default is 0)
  28. * @param b defines the blue component (between 0 and 1, default is 0)
  29. */
  30. constructor(
  31. /**
  32. * Defines the red component (between 0 and 1, default is 0)
  33. */
  34. public r: number = 0,
  35. /**
  36. * Defines the green component (between 0 and 1, default is 0)
  37. */
  38. public g: number = 0,
  39. /**
  40. * Defines the blue component (between 0 and 1, default is 0)
  41. */
  42. public b: number = 0) {
  43. }
  44. /**
  45. * Creates a string with the Color3 current values
  46. * @returns the string representation of the Color3 object
  47. */
  48. public toString(): string {
  49. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  50. }
  51. /**
  52. * Returns the string "Color3"
  53. * @returns "Color3"
  54. */
  55. public getClassName(): string {
  56. return "Color3";
  57. }
  58. /**
  59. * Compute the Color3 hash code
  60. * @returns an unique number that can be used to hash Color3 objects
  61. */
  62. public getHashCode(): number {
  63. let hash = (this.r * 255) | 0;
  64. hash = (hash * 397) ^ ((this.g * 255) | 0);
  65. hash = (hash * 397) ^ ((this.b * 255) | 0);
  66. return hash;
  67. }
  68. // Operators
  69. /**
  70. * Stores in the given array from the given starting index the red, green, blue values as successive elements
  71. * @param array defines the array where to store the r,g,b components
  72. * @param index defines an optional index in the target array to define where to start storing values
  73. * @returns the current Color3 object
  74. */
  75. public toArray(array: FloatArray, index: number = 0): Color3 {
  76. array[index] = this.r;
  77. array[index + 1] = this.g;
  78. array[index + 2] = this.b;
  79. return this;
  80. }
  81. /**
  82. * Returns a new Color4 object from the current Color3 and the given alpha
  83. * @param alpha defines the alpha component on the new Color4 object (default is 1)
  84. * @returns a new Color4 object
  85. */
  86. public toColor4(alpha: number = 1): Color4 {
  87. return new Color4(this.r, this.g, this.b, alpha);
  88. }
  89. /**
  90. * Returns a new array populated with 3 numeric elements : red, green and blue values
  91. * @returns the new array
  92. */
  93. public asArray(): number[] {
  94. var result = new Array<number>();
  95. this.toArray(result, 0);
  96. return result;
  97. }
  98. /**
  99. * Returns the luminance value
  100. * @returns a float value
  101. */
  102. public toLuminance(): number {
  103. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  104. }
  105. /**
  106. * Multiply each Color3 rgb values by the given Color3 rgb values in a new Color3 object
  107. * @param otherColor defines the second operand
  108. * @returns the new Color3 object
  109. */
  110. public multiply(otherColor: DeepImmutable<Color3>): Color3 {
  111. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  112. }
  113. /**
  114. * Multiply the rgb values of the Color3 and the given Color3 and stores the result in the object "result"
  115. * @param otherColor defines the second operand
  116. * @param result defines the Color3 object where to store the result
  117. * @returns the current Color3
  118. */
  119. public multiplyToRef(otherColor: DeepImmutable<Color3>, result: Color3): Color3 {
  120. result.r = this.r * otherColor.r;
  121. result.g = this.g * otherColor.g;
  122. result.b = this.b * otherColor.b;
  123. return this;
  124. }
  125. /**
  126. * Determines equality between Color3 objects
  127. * @param otherColor defines the second operand
  128. * @returns true if the rgb values are equal to the given ones
  129. */
  130. public equals(otherColor: DeepImmutable<Color3>): boolean {
  131. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  132. }
  133. /**
  134. * Determines equality between the current Color3 object and a set of r,b,g values
  135. * @param r defines the red component to check
  136. * @param g defines the green component to check
  137. * @param b defines the blue component to check
  138. * @returns true if the rgb values are equal to the given ones
  139. */
  140. public equalsFloats(r: number, g: number, b: number): boolean {
  141. return this.r === r && this.g === g && this.b === b;
  142. }
  143. /**
  144. * Multiplies in place each rgb value by scale
  145. * @param scale defines the scaling factor
  146. * @returns the updated Color3
  147. */
  148. public scale(scale: number): Color3 {
  149. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  150. }
  151. /**
  152. * Multiplies the rgb values by scale and stores the result into "result"
  153. * @param scale defines the scaling factor
  154. * @param result defines the Color3 object where to store the result
  155. * @returns the unmodified current Color3
  156. */
  157. public scaleToRef(scale: number, result: Color3): Color3 {
  158. result.r = this.r * scale;
  159. result.g = this.g * scale;
  160. result.b = this.b * scale;
  161. return this;
  162. }
  163. /**
  164. * Scale the current Color3 values by a factor and add the result to a given Color3
  165. * @param scale defines the scale factor
  166. * @param result defines color to store the result into
  167. * @returns the unmodified current Color3
  168. */
  169. public scaleAndAddToRef(scale: number, result: Color3): Color3 {
  170. result.r += this.r * scale;
  171. result.g += this.g * scale;
  172. result.b += this.b * scale;
  173. return this;
  174. }
  175. /**
  176. * Clamps the rgb values by the min and max values and stores the result into "result"
  177. * @param min defines minimum clamping value (default is 0)
  178. * @param max defines maximum clamping value (default is 1)
  179. * @param result defines color to store the result into
  180. * @returns the original Color3
  181. */
  182. public clampToRef(min: number = 0, max: number = 1, result: Color3): Color3 {
  183. result.r = Scalar.Clamp(this.r, min, max);
  184. result.g = Scalar.Clamp(this.g, min, max);
  185. result.b = Scalar.Clamp(this.b, min, max);
  186. return this;
  187. }
  188. /**
  189. * Creates a new Color3 set with the added values of the current Color3 and of the given one
  190. * @param otherColor defines the second operand
  191. * @returns the new Color3
  192. */
  193. public add(otherColor: DeepImmutable<Color3>): Color3 {
  194. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  195. }
  196. /**
  197. * Stores the result of the addition of the current Color3 and given one rgb values into "result"
  198. * @param otherColor defines the second operand
  199. * @param result defines Color3 object to store the result into
  200. * @returns the unmodified current Color3
  201. */
  202. public addToRef(otherColor: DeepImmutable<Color3>, result: Color3): Color3 {
  203. result.r = this.r + otherColor.r;
  204. result.g = this.g + otherColor.g;
  205. result.b = this.b + otherColor.b;
  206. return this;
  207. }
  208. /**
  209. * Returns a new Color3 set with the subtracted values of the given one from the current Color3
  210. * @param otherColor defines the second operand
  211. * @returns the new Color3
  212. */
  213. public subtract(otherColor: DeepImmutable<Color3>): Color3 {
  214. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  215. }
  216. /**
  217. * Stores the result of the subtraction of given one from the current Color3 rgb values into "result"
  218. * @param otherColor defines the second operand
  219. * @param result defines Color3 object to store the result into
  220. * @returns the unmodified current Color3
  221. */
  222. public subtractToRef(otherColor: DeepImmutable<Color3>, result: Color3): Color3 {
  223. result.r = this.r - otherColor.r;
  224. result.g = this.g - otherColor.g;
  225. result.b = this.b - otherColor.b;
  226. return this;
  227. }
  228. /**
  229. * Copy the current object
  230. * @returns a new Color3 copied the current one
  231. */
  232. public clone(): Color3 {
  233. return new Color3(this.r, this.g, this.b);
  234. }
  235. /**
  236. * Copies the rgb values from the source in the current Color3
  237. * @param source defines the source Color3 object
  238. * @returns the updated Color3 object
  239. */
  240. public copyFrom(source: DeepImmutable<Color3>): Color3 {
  241. this.r = source.r;
  242. this.g = source.g;
  243. this.b = source.b;
  244. return this;
  245. }
  246. /**
  247. * Updates the Color3 rgb values from the given floats
  248. * @param r defines the red component to read from
  249. * @param g defines the green component to read from
  250. * @param b defines the blue component to read from
  251. * @returns the current Color3 object
  252. */
  253. public copyFromFloats(r: number, g: number, b: number): Color3 {
  254. this.r = r;
  255. this.g = g;
  256. this.b = b;
  257. return this;
  258. }
  259. /**
  260. * Updates the Color3 rgb values from the given floats
  261. * @param r defines the red component to read from
  262. * @param g defines the green component to read from
  263. * @param b defines the blue component to read from
  264. * @returns the current Color3 object
  265. */
  266. public set(r: number, g: number, b: number): Color3 {
  267. return this.copyFromFloats(r, g, b);
  268. }
  269. /**
  270. * Compute the Color3 hexadecimal code as a string
  271. * @returns a string containing the hexadecimal representation of the Color3 object
  272. */
  273. public toHexString(): string {
  274. var intR = (this.r * 255) | 0;
  275. var intG = (this.g * 255) | 0;
  276. var intB = (this.b * 255) | 0;
  277. return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB);
  278. }
  279. /**
  280. * Computes a new Color3 converted from the current one to linear space
  281. * @returns a new Color3 object
  282. */
  283. public toLinearSpace(): Color3 {
  284. var convertedColor = new Color3();
  285. this.toLinearSpaceToRef(convertedColor);
  286. return convertedColor;
  287. }
  288. /**
  289. * Converts current color in rgb space to HSV values
  290. * @returns a new color3 representing the HSV values
  291. */
  292. public toHSV(): Color3 {
  293. let result = new Color3();
  294. this.toHSVToRef(result);
  295. return result;
  296. }
  297. /**
  298. * Converts current color in rgb space to HSV values
  299. * @param result defines the Color3 where to store the HSV values
  300. */
  301. public toHSVToRef(result: Color3) {
  302. var r = this.r;
  303. var g = this.g;
  304. var b = this.b;
  305. var max = Math.max(r, g, b);
  306. var min = Math.min(r, g, b);
  307. var h = 0;
  308. var s = 0;
  309. var v = max;
  310. var dm = max - min;
  311. if (max !== 0) {
  312. s = dm / max;
  313. }
  314. if (max != min) {
  315. if (max == r) {
  316. h = (g - b) / dm;
  317. if (g < b) {
  318. h += 6;
  319. }
  320. } else if (max == g) {
  321. h = (b - r) / dm + 2;
  322. } else if (max == b) {
  323. h = (r - g) / dm + 4;
  324. }
  325. h *= 60;
  326. }
  327. result.r = h;
  328. result.g = s;
  329. result.b = v;
  330. }
  331. /**
  332. * Converts the Color3 values to linear space and stores the result in "convertedColor"
  333. * @param convertedColor defines the Color3 object where to store the linear space version
  334. * @returns the unmodified Color3
  335. */
  336. public toLinearSpaceToRef(convertedColor: Color3): Color3 {
  337. convertedColor.r = Math.pow(this.r, ToLinearSpace);
  338. convertedColor.g = Math.pow(this.g, ToLinearSpace);
  339. convertedColor.b = Math.pow(this.b, ToLinearSpace);
  340. return this;
  341. }
  342. /**
  343. * Computes a new Color3 converted from the current one to gamma space
  344. * @returns a new Color3 object
  345. */
  346. public toGammaSpace(): Color3 {
  347. var convertedColor = new Color3();
  348. this.toGammaSpaceToRef(convertedColor);
  349. return convertedColor;
  350. }
  351. /**
  352. * Converts the Color3 values to gamma space and stores the result in "convertedColor"
  353. * @param convertedColor defines the Color3 object where to store the gamma space version
  354. * @returns the unmodified Color3
  355. */
  356. public toGammaSpaceToRef(convertedColor: Color3): Color3 {
  357. convertedColor.r = Math.pow(this.r, ToGammaSpace);
  358. convertedColor.g = Math.pow(this.g, ToGammaSpace);
  359. convertedColor.b = Math.pow(this.b, ToGammaSpace);
  360. return this;
  361. }
  362. // Statics
  363. private static _BlackReadOnly = Color3.Black() as DeepImmutable<Color3>;
  364. /**
  365. * Convert Hue, saturation and value to a Color3 (RGB)
  366. * @param hue defines the hue
  367. * @param saturation defines the saturation
  368. * @param value defines the value
  369. * @param result defines the Color3 where to store the RGB values
  370. */
  371. public static HSVtoRGBToRef(hue: number, saturation: number, value: number, result: Color3) {
  372. var chroma = value * saturation;
  373. var h = hue / 60;
  374. var x = chroma * (1 - Math.abs((h % 2) - 1));
  375. var r = 0;
  376. var g = 0;
  377. var b = 0;
  378. if (h >= 0 && h <= 1) {
  379. r = chroma;
  380. g = x;
  381. } else if (h >= 1 && h <= 2) {
  382. r = x;
  383. g = chroma;
  384. } else if (h >= 2 && h <= 3) {
  385. g = chroma;
  386. b = x;
  387. } else if (h >= 3 && h <= 4) {
  388. g = x;
  389. b = chroma;
  390. } else if (h >= 4 && h <= 5) {
  391. r = x;
  392. b = chroma;
  393. } else if (h >= 5 && h <= 6) {
  394. r = chroma;
  395. b = x;
  396. }
  397. var m = value - chroma;
  398. result.set((r + m), (g + m), (b + m));
  399. }
  400. /**
  401. * Creates a new Color3 from the string containing valid hexadecimal values
  402. * @param hex defines a string containing valid hexadecimal values
  403. * @returns a new Color3 object
  404. */
  405. public static FromHexString(hex: string): Color3 {
  406. if (hex.substring(0, 1) !== "#" || hex.length !== 7) {
  407. return new Color3(0, 0, 0);
  408. }
  409. var r = parseInt(hex.substring(1, 3), 16);
  410. var g = parseInt(hex.substring(3, 5), 16);
  411. var b = parseInt(hex.substring(5, 7), 16);
  412. return Color3.FromInts(r, g, b);
  413. }
  414. /**
  415. * Creates a new Color3 from the starting index of the given array
  416. * @param array defines the source array
  417. * @param offset defines an offset in the source array
  418. * @returns a new Color3 object
  419. */
  420. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Color3 {
  421. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  422. }
  423. /**
  424. * Creates a new Color3 from integer values (< 256)
  425. * @param r defines the red component to read from (value between 0 and 255)
  426. * @param g defines the green component to read from (value between 0 and 255)
  427. * @param b defines the blue component to read from (value between 0 and 255)
  428. * @returns a new Color3 object
  429. */
  430. public static FromInts(r: number, g: number, b: number): Color3 {
  431. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  432. }
  433. /**
  434. * Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3
  435. * @param start defines the start Color3 value
  436. * @param end defines the end Color3 value
  437. * @param amount defines the gradient value between start and end
  438. * @returns a new Color3 object
  439. */
  440. public static Lerp(start: DeepImmutable<Color3>, end: DeepImmutable<Color3>, amount: number): Color3 {
  441. var result = new Color3(0.0, 0.0, 0.0);
  442. Color3.LerpToRef(start, end, amount, result);
  443. return result;
  444. }
  445. /**
  446. * Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3
  447. * @param left defines the start value
  448. * @param right defines the end value
  449. * @param amount defines the gradient factor
  450. * @param result defines the Color3 object where to store the result
  451. */
  452. public static LerpToRef(left: DeepImmutable<Color3>, right: DeepImmutable<Color3>, amount: number, result: Color3): void {
  453. result.r = left.r + ((right.r - left.r) * amount);
  454. result.g = left.g + ((right.g - left.g) * amount);
  455. result.b = left.b + ((right.b - left.b) * amount);
  456. }
  457. /**
  458. * Returns a Color3 value containing a red color
  459. * @returns a new Color3 object
  460. */
  461. public static Red(): Color3 { return new Color3(1, 0, 0); }
  462. /**
  463. * Returns a Color3 value containing a green color
  464. * @returns a new Color3 object
  465. */
  466. public static Green(): Color3 { return new Color3(0, 1, 0); }
  467. /**
  468. * Returns a Color3 value containing a blue color
  469. * @returns a new Color3 object
  470. */
  471. public static Blue(): Color3 { return new Color3(0, 0, 1); }
  472. /**
  473. * Returns a Color3 value containing a black color
  474. * @returns a new Color3 object
  475. */
  476. public static Black(): Color3 { return new Color3(0, 0, 0); }
  477. /**
  478. * Gets a Color3 value containing a black color that must not be updated
  479. */
  480. public static get BlackReadOnly(): DeepImmutable<Color3> {
  481. return Color3._BlackReadOnly;
  482. }
  483. /**
  484. * Returns a Color3 value containing a white color
  485. * @returns a new Color3 object
  486. */
  487. public static White(): Color3 { return new Color3(1, 1, 1); }
  488. /**
  489. * Returns a Color3 value containing a purple color
  490. * @returns a new Color3 object
  491. */
  492. public static Purple(): Color3 { return new Color3(0.5, 0, 0.5); }
  493. /**
  494. * Returns a Color3 value containing a magenta color
  495. * @returns a new Color3 object
  496. */
  497. public static Magenta(): Color3 { return new Color3(1, 0, 1); }
  498. /**
  499. * Returns a Color3 value containing a yellow color
  500. * @returns a new Color3 object
  501. */
  502. public static Yellow(): Color3 { return new Color3(1, 1, 0); }
  503. /**
  504. * Returns a Color3 value containing a gray color
  505. * @returns a new Color3 object
  506. */
  507. public static Gray(): Color3 { return new Color3(0.5, 0.5, 0.5); }
  508. /**
  509. * Returns a Color3 value containing a teal color
  510. * @returns a new Color3 object
  511. */
  512. public static Teal(): Color3 { return new Color3(0, 1.0, 1.0); }
  513. /**
  514. * Returns a Color3 value containing a random color
  515. * @returns a new Color3 object
  516. */
  517. public static Random(): Color3 { return new Color3(Math.random(), Math.random(), Math.random()); }
  518. }
  519. /**
  520. * Class used to hold a RBGA color
  521. */
  522. export class Color4 {
  523. /**
  524. * Creates a new Color4 object from red, green, blue values, all between 0 and 1
  525. * @param r defines the red component (between 0 and 1, default is 0)
  526. * @param g defines the green component (between 0 and 1, default is 0)
  527. * @param b defines the blue component (between 0 and 1, default is 0)
  528. * @param a defines the alpha component (between 0 and 1, default is 1)
  529. */
  530. constructor(
  531. /**
  532. * Defines the red component (between 0 and 1, default is 0)
  533. */
  534. public r: number = 0,
  535. /**
  536. * Defines the green component (between 0 and 1, default is 0)
  537. */
  538. public g: number = 0,
  539. /**
  540. * Defines the blue component (between 0 and 1, default is 0)
  541. */
  542. public b: number = 0,
  543. /**
  544. * Defines the alpha component (between 0 and 1, default is 1)
  545. */
  546. public a: number = 1) {
  547. }
  548. // Operators
  549. /**
  550. * Adds in place the given Color4 values to the current Color4 object
  551. * @param right defines the second operand
  552. * @returns the current updated Color4 object
  553. */
  554. public addInPlace(right: DeepImmutable<Color4>): Color4 {
  555. this.r += right.r;
  556. this.g += right.g;
  557. this.b += right.b;
  558. this.a += right.a;
  559. return this;
  560. }
  561. /**
  562. * Creates a new array populated with 4 numeric elements : red, green, blue, alpha values
  563. * @returns the new array
  564. */
  565. public asArray(): number[] {
  566. var result = new Array<number>();
  567. this.toArray(result, 0);
  568. return result;
  569. }
  570. /**
  571. * Stores from the starting index in the given array the Color4 successive values
  572. * @param array defines the array where to store the r,g,b components
  573. * @param index defines an optional index in the target array to define where to start storing values
  574. * @returns the current Color4 object
  575. */
  576. public toArray(array: number[], index: number = 0): Color4 {
  577. array[index] = this.r;
  578. array[index + 1] = this.g;
  579. array[index + 2] = this.b;
  580. array[index + 3] = this.a;
  581. return this;
  582. }
  583. /**
  584. * Determines equality between Color4 objects
  585. * @param otherColor defines the second operand
  586. * @returns true if the rgba values are equal to the given ones
  587. */
  588. public equals(otherColor: DeepImmutable<Color4>): boolean {
  589. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b && this.a === otherColor.a;
  590. }
  591. /**
  592. * Creates a new Color4 set with the added values of the current Color4 and of the given one
  593. * @param right defines the second operand
  594. * @returns a new Color4 object
  595. */
  596. public add(right: DeepImmutable<Color4>): Color4 {
  597. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  598. }
  599. /**
  600. * Creates a new Color4 set with the subtracted values of the given one from the current Color4
  601. * @param right defines the second operand
  602. * @returns a new Color4 object
  603. */
  604. public subtract(right: DeepImmutable<Color4>): Color4 {
  605. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  606. }
  607. /**
  608. * Subtracts the given ones from the current Color4 values and stores the results in "result"
  609. * @param right defines the second operand
  610. * @param result defines the Color4 object where to store the result
  611. * @returns the current Color4 object
  612. */
  613. public subtractToRef(right: DeepImmutable<Color4>, result: Color4): Color4 {
  614. result.r = this.r - right.r;
  615. result.g = this.g - right.g;
  616. result.b = this.b - right.b;
  617. result.a = this.a - right.a;
  618. return this;
  619. }
  620. /**
  621. * Creates a new Color4 with the current Color4 values multiplied by scale
  622. * @param scale defines the scaling factor to apply
  623. * @returns a new Color4 object
  624. */
  625. public scale(scale: number): Color4 {
  626. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  627. }
  628. /**
  629. * Multiplies the current Color4 values by scale and stores the result in "result"
  630. * @param scale defines the scaling factor to apply
  631. * @param result defines the Color4 object where to store the result
  632. * @returns the current unmodified Color4
  633. */
  634. public scaleToRef(scale: number, result: Color4): Color4 {
  635. result.r = this.r * scale;
  636. result.g = this.g * scale;
  637. result.b = this.b * scale;
  638. result.a = this.a * scale;
  639. return this;
  640. }
  641. /**
  642. * Scale the current Color4 values by a factor and add the result to a given Color4
  643. * @param scale defines the scale factor
  644. * @param result defines the Color4 object where to store the result
  645. * @returns the unmodified current Color4
  646. */
  647. public scaleAndAddToRef(scale: number, result: Color4): Color4 {
  648. result.r += this.r * scale;
  649. result.g += this.g * scale;
  650. result.b += this.b * scale;
  651. result.a += this.a * scale;
  652. return this;
  653. }
  654. /**
  655. * Clamps the rgb values by the min and max values and stores the result into "result"
  656. * @param min defines minimum clamping value (default is 0)
  657. * @param max defines maximum clamping value (default is 1)
  658. * @param result defines color to store the result into.
  659. * @returns the cuurent Color4
  660. */
  661. public clampToRef(min: number = 0, max: number = 1, result: Color4): Color4 {
  662. result.r = Scalar.Clamp(this.r, min, max);
  663. result.g = Scalar.Clamp(this.g, min, max);
  664. result.b = Scalar.Clamp(this.b, min, max);
  665. result.a = Scalar.Clamp(this.a, min, max);
  666. return this;
  667. }
  668. /**
  669. * Multipy an Color4 value by another and return a new Color4 object
  670. * @param color defines the Color4 value to multiply by
  671. * @returns a new Color4 object
  672. */
  673. public multiply(color: Color4): Color4 {
  674. return new Color4(this.r * color.r, this.g * color.g, this.b * color.b, this.a * color.a);
  675. }
  676. /**
  677. * Multipy a Color4 value by another and push the result in a reference value
  678. * @param color defines the Color4 value to multiply by
  679. * @param result defines the Color4 to fill the result in
  680. * @returns the result Color4
  681. */
  682. public multiplyToRef(color: Color4, result: Color4): Color4 {
  683. result.r = this.r * color.r;
  684. result.g = this.g * color.g;
  685. result.b = this.b * color.b;
  686. result.a = this.a * color.a;
  687. return result;
  688. }
  689. /**
  690. * Creates a string with the Color4 current values
  691. * @returns the string representation of the Color4 object
  692. */
  693. public toString(): string {
  694. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  695. }
  696. /**
  697. * Returns the string "Color4"
  698. * @returns "Color4"
  699. */
  700. public getClassName(): string {
  701. return "Color4";
  702. }
  703. /**
  704. * Compute the Color4 hash code
  705. * @returns an unique number that can be used to hash Color4 objects
  706. */
  707. public getHashCode(): number {
  708. let hash = (this.r * 255) | 0;
  709. hash = (hash * 397) ^ ((this.g * 255) | 0);
  710. hash = (hash * 397) ^ ((this.b * 255) | 0);
  711. hash = (hash * 397) ^ ((this.a * 255) | 0);
  712. return hash;
  713. }
  714. /**
  715. * Creates a new Color4 copied from the current one
  716. * @returns a new Color4 object
  717. */
  718. public clone(): Color4 {
  719. return new Color4(this.r, this.g, this.b, this.a);
  720. }
  721. /**
  722. * Copies the given Color4 values into the current one
  723. * @param source defines the source Color4 object
  724. * @returns the current updated Color4 object
  725. */
  726. public copyFrom(source: Color4): Color4 {
  727. this.r = source.r;
  728. this.g = source.g;
  729. this.b = source.b;
  730. this.a = source.a;
  731. return this;
  732. }
  733. /**
  734. * Copies the given float values into the current one
  735. * @param r defines the red component to read from
  736. * @param g defines the green component to read from
  737. * @param b defines the blue component to read from
  738. * @param a defines the alpha component to read from
  739. * @returns the current updated Color4 object
  740. */
  741. public copyFromFloats(r: number, g: number, b: number, a: number): Color4 {
  742. this.r = r;
  743. this.g = g;
  744. this.b = b;
  745. this.a = a;
  746. return this;
  747. }
  748. /**
  749. * Copies the given float values into the current one
  750. * @param r defines the red component to read from
  751. * @param g defines the green component to read from
  752. * @param b defines the blue component to read from
  753. * @param a defines the alpha component to read from
  754. * @returns the current updated Color4 object
  755. */
  756. public set(r: number, g: number, b: number, a: number): Color4 {
  757. return this.copyFromFloats(r, g, b, a);
  758. }
  759. /**
  760. * Compute the Color4 hexadecimal code as a string
  761. * @returns a string containing the hexadecimal representation of the Color4 object
  762. */
  763. public toHexString(): string {
  764. var intR = (this.r * 255) | 0;
  765. var intG = (this.g * 255) | 0;
  766. var intB = (this.b * 255) | 0;
  767. var intA = (this.a * 255) | 0;
  768. return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB) + Scalar.ToHex(intA);
  769. }
  770. /**
  771. * Computes a new Color4 converted from the current one to linear space
  772. * @returns a new Color4 object
  773. */
  774. public toLinearSpace(): Color4 {
  775. var convertedColor = new Color4();
  776. this.toLinearSpaceToRef(convertedColor);
  777. return convertedColor;
  778. }
  779. /**
  780. * Converts the Color4 values to linear space and stores the result in "convertedColor"
  781. * @param convertedColor defines the Color4 object where to store the linear space version
  782. * @returns the unmodified Color4
  783. */
  784. public toLinearSpaceToRef(convertedColor: Color4): Color4 {
  785. convertedColor.r = Math.pow(this.r, ToLinearSpace);
  786. convertedColor.g = Math.pow(this.g, ToLinearSpace);
  787. convertedColor.b = Math.pow(this.b, ToLinearSpace);
  788. convertedColor.a = this.a;
  789. return this;
  790. }
  791. /**
  792. * Computes a new Color4 converted from the current one to gamma space
  793. * @returns a new Color4 object
  794. */
  795. public toGammaSpace(): Color4 {
  796. var convertedColor = new Color4();
  797. this.toGammaSpaceToRef(convertedColor);
  798. return convertedColor;
  799. }
  800. /**
  801. * Converts the Color4 values to gamma space and stores the result in "convertedColor"
  802. * @param convertedColor defines the Color4 object where to store the gamma space version
  803. * @returns the unmodified Color4
  804. */
  805. public toGammaSpaceToRef(convertedColor: Color4): Color4 {
  806. convertedColor.r = Math.pow(this.r, ToGammaSpace);
  807. convertedColor.g = Math.pow(this.g, ToGammaSpace);
  808. convertedColor.b = Math.pow(this.b, ToGammaSpace);
  809. convertedColor.a = this.a;
  810. return this;
  811. }
  812. // Statics
  813. /**
  814. * Creates a new Color4 from the string containing valid hexadecimal values
  815. * @param hex defines a string containing valid hexadecimal values
  816. * @returns a new Color4 object
  817. */
  818. public static FromHexString(hex: string): Color4 {
  819. if (hex.substring(0, 1) !== "#" || hex.length !== 9) {
  820. return new Color4(0.0, 0.0, 0.0, 0.0);
  821. }
  822. var r = parseInt(hex.substring(1, 3), 16);
  823. var g = parseInt(hex.substring(3, 5), 16);
  824. var b = parseInt(hex.substring(5, 7), 16);
  825. var a = parseInt(hex.substring(7, 9), 16);
  826. return Color4.FromInts(r, g, b, a);
  827. }
  828. /**
  829. * Creates a new Color4 object set with the linearly interpolated values of "amount" between the left Color4 object and the right Color4 object
  830. * @param left defines the start value
  831. * @param right defines the end value
  832. * @param amount defines the gradient factor
  833. * @returns a new Color4 object
  834. */
  835. public static Lerp(left: DeepImmutable<Color4>, right: DeepImmutable<Color4>, amount: number): Color4 {
  836. var result = new Color4(0.0, 0.0, 0.0, 0.0);
  837. Color4.LerpToRef(left, right, amount, result);
  838. return result;
  839. }
  840. /**
  841. * Set the given "result" with the linearly interpolated values of "amount" between the left Color4 object and the right Color4 object
  842. * @param left defines the start value
  843. * @param right defines the end value
  844. * @param amount defines the gradient factor
  845. * @param result defines the Color4 object where to store data
  846. */
  847. public static LerpToRef(left: DeepImmutable<Color4>, right: DeepImmutable<Color4>, amount: number, result: Color4): void {
  848. result.r = left.r + (right.r - left.r) * amount;
  849. result.g = left.g + (right.g - left.g) * amount;
  850. result.b = left.b + (right.b - left.b) * amount;
  851. result.a = left.a + (right.a - left.a) * amount;
  852. }
  853. /**
  854. * Creates a new Color4 from a Color3 and an alpha value
  855. * @param color3 defines the source Color3 to read from
  856. * @param alpha defines the alpha component (1.0 by default)
  857. * @returns a new Color4 object
  858. */
  859. public static FromColor3(color3: DeepImmutable<Color3>, alpha: number = 1.0): Color4 {
  860. return new Color4(color3.r, color3.g, color3.b, alpha);
  861. }
  862. /**
  863. * Creates a new Color4 from the starting index element of the given array
  864. * @param array defines the source array to read from
  865. * @param offset defines the offset in the source array
  866. * @returns a new Color4 object
  867. */
  868. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Color4 {
  869. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  870. }
  871. /**
  872. * Creates a new Color3 from integer values (< 256)
  873. * @param r defines the red component to read from (value between 0 and 255)
  874. * @param g defines the green component to read from (value between 0 and 255)
  875. * @param b defines the blue component to read from (value between 0 and 255)
  876. * @param a defines the alpha component to read from (value between 0 and 255)
  877. * @returns a new Color3 object
  878. */
  879. public static FromInts(r: number, g: number, b: number, a: number): Color4 {
  880. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  881. }
  882. /**
  883. * Check the content of a given array and convert it to an array containing RGBA data
  884. * If the original array was already containing count * 4 values then it is returned directly
  885. * @param colors defines the array to check
  886. * @param count defines the number of RGBA data to expect
  887. * @returns an array containing count * 4 values (RGBA)
  888. */
  889. public static CheckColors4(colors: number[], count: number): number[] {
  890. // Check if color3 was used
  891. if (colors.length === count * 3) {
  892. var colors4 = [];
  893. for (var index = 0; index < colors.length; index += 3) {
  894. var newIndex = (index / 3) * 4;
  895. colors4[newIndex] = colors[index];
  896. colors4[newIndex + 1] = colors[index + 1];
  897. colors4[newIndex + 2] = colors[index + 2];
  898. colors4[newIndex + 3] = 1.0;
  899. }
  900. return colors4;
  901. }
  902. return colors;
  903. }
  904. }
  905. /**
  906. * Class representing a vector containing 2 coordinates
  907. */
  908. export class Vector2 {
  909. /**
  910. * Creates a new Vector2 from the given x and y coordinates
  911. * @param x defines the first coordinate
  912. * @param y defines the second coordinate
  913. */
  914. constructor(
  915. /** defines the first coordinate */
  916. public x: number = 0,
  917. /** defines the second coordinate */
  918. public y: number = 0) {
  919. }
  920. /**
  921. * Gets a string with the Vector2 coordinates
  922. * @returns a string with the Vector2 coordinates
  923. */
  924. public toString(): string {
  925. return "{X: " + this.x + " Y:" + this.y + "}";
  926. }
  927. /**
  928. * Gets class name
  929. * @returns the string "Vector2"
  930. */
  931. public getClassName(): string {
  932. return "Vector2";
  933. }
  934. /**
  935. * Gets current vector hash code
  936. * @returns the Vector2 hash code as a number
  937. */
  938. public getHashCode(): number {
  939. let hash = this.x | 0;
  940. hash = (hash * 397) ^ (this.y | 0);
  941. return hash;
  942. }
  943. // Operators
  944. /**
  945. * Sets the Vector2 coordinates in the given array or Float32Array from the given index.
  946. * @param array defines the source array
  947. * @param index defines the offset in source array
  948. * @returns the current Vector2
  949. */
  950. public toArray(array: FloatArray, index: number = 0): Vector2 {
  951. array[index] = this.x;
  952. array[index + 1] = this.y;
  953. return this;
  954. }
  955. /**
  956. * Copy the current vector to an array
  957. * @returns a new array with 2 elements: the Vector2 coordinates.
  958. */
  959. public asArray(): number[] {
  960. var result = new Array<number>();
  961. this.toArray(result, 0);
  962. return result;
  963. }
  964. /**
  965. * Sets the Vector2 coordinates with the given Vector2 coordinates
  966. * @param source defines the source Vector2
  967. * @returns the current updated Vector2
  968. */
  969. public copyFrom(source: DeepImmutable<Vector2>): Vector2 {
  970. this.x = source.x;
  971. this.y = source.y;
  972. return this;
  973. }
  974. /**
  975. * Sets the Vector2 coordinates with the given floats
  976. * @param x defines the first coordinate
  977. * @param y defines the second coordinate
  978. * @returns the current updated Vector2
  979. */
  980. public copyFromFloats(x: number, y: number): Vector2 {
  981. this.x = x;
  982. this.y = y;
  983. return this;
  984. }
  985. /**
  986. * Sets the Vector2 coordinates with the given floats
  987. * @param x defines the first coordinate
  988. * @param y defines the second coordinate
  989. * @returns the current updated Vector2
  990. */
  991. public set(x: number, y: number): Vector2 {
  992. return this.copyFromFloats(x, y);
  993. }
  994. /**
  995. * Add another vector with the current one
  996. * @param otherVector defines the other vector
  997. * @returns a new Vector2 set with the addition of the current Vector2 and the given one coordinates
  998. */
  999. public add(otherVector: DeepImmutable<Vector2>): Vector2 {
  1000. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  1001. }
  1002. /**
  1003. * Sets the "result" coordinates with the addition of the current Vector2 and the given one coordinates
  1004. * @param otherVector defines the other vector
  1005. * @param result defines the target vector
  1006. * @returns the unmodified current Vector2
  1007. */
  1008. public addToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  1009. result.x = this.x + otherVector.x;
  1010. result.y = this.y + otherVector.y;
  1011. return this;
  1012. }
  1013. /**
  1014. * Set the Vector2 coordinates by adding the given Vector2 coordinates
  1015. * @param otherVector defines the other vector
  1016. * @returns the current updated Vector2
  1017. */
  1018. public addInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  1019. this.x += otherVector.x;
  1020. this.y += otherVector.y;
  1021. return this;
  1022. }
  1023. /**
  1024. * Gets a new Vector2 by adding the current Vector2 coordinates to the given Vector3 x, y coordinates
  1025. * @param otherVector defines the other vector
  1026. * @returns a new Vector2
  1027. */
  1028. public addVector3(otherVector: Vector3): Vector2 {
  1029. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  1030. }
  1031. /**
  1032. * Gets a new Vector2 set with the subtracted coordinates of the given one from the current Vector2
  1033. * @param otherVector defines the other vector
  1034. * @returns a new Vector2
  1035. */
  1036. public subtract(otherVector: Vector2): Vector2 {
  1037. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  1038. }
  1039. /**
  1040. * Sets the "result" coordinates with the subtraction of the given one from the current Vector2 coordinates.
  1041. * @param otherVector defines the other vector
  1042. * @param result defines the target vector
  1043. * @returns the unmodified current Vector2
  1044. */
  1045. public subtractToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  1046. result.x = this.x - otherVector.x;
  1047. result.y = this.y - otherVector.y;
  1048. return this;
  1049. }
  1050. /**
  1051. * Sets the current Vector2 coordinates by subtracting from it the given one coordinates
  1052. * @param otherVector defines the other vector
  1053. * @returns the current updated Vector2
  1054. */
  1055. public subtractInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  1056. this.x -= otherVector.x;
  1057. this.y -= otherVector.y;
  1058. return this;
  1059. }
  1060. /**
  1061. * Multiplies in place the current Vector2 coordinates by the given ones
  1062. * @param otherVector defines the other vector
  1063. * @returns the current updated Vector2
  1064. */
  1065. public multiplyInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  1066. this.x *= otherVector.x;
  1067. this.y *= otherVector.y;
  1068. return this;
  1069. }
  1070. /**
  1071. * Returns a new Vector2 set with the multiplication of the current Vector2 and the given one coordinates
  1072. * @param otherVector defines the other vector
  1073. * @returns a new Vector2
  1074. */
  1075. public multiply(otherVector: DeepImmutable<Vector2>): Vector2 {
  1076. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  1077. }
  1078. /**
  1079. * Sets "result" coordinates with the multiplication of the current Vector2 and the given one coordinates
  1080. * @param otherVector defines the other vector
  1081. * @param result defines the target vector
  1082. * @returns the unmodified current Vector2
  1083. */
  1084. public multiplyToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  1085. result.x = this.x * otherVector.x;
  1086. result.y = this.y * otherVector.y;
  1087. return this;
  1088. }
  1089. /**
  1090. * Gets a new Vector2 set with the Vector2 coordinates multiplied by the given floats
  1091. * @param x defines the first coordinate
  1092. * @param y defines the second coordinate
  1093. * @returns a new Vector2
  1094. */
  1095. public multiplyByFloats(x: number, y: number): Vector2 {
  1096. return new Vector2(this.x * x, this.y * y);
  1097. }
  1098. /**
  1099. * Returns a new Vector2 set with the Vector2 coordinates divided by the given one coordinates
  1100. * @param otherVector defines the other vector
  1101. * @returns a new Vector2
  1102. */
  1103. public divide(otherVector: Vector2): Vector2 {
  1104. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  1105. }
  1106. /**
  1107. * Sets the "result" coordinates with the Vector2 divided by the given one coordinates
  1108. * @param otherVector defines the other vector
  1109. * @param result defines the target vector
  1110. * @returns the unmodified current Vector2
  1111. */
  1112. public divideToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  1113. result.x = this.x / otherVector.x;
  1114. result.y = this.y / otherVector.y;
  1115. return this;
  1116. }
  1117. /**
  1118. * Divides the current Vector2 coordinates by the given ones
  1119. * @param otherVector defines the other vector
  1120. * @returns the current updated Vector2
  1121. */
  1122. public divideInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  1123. return this.divideToRef(otherVector, this);
  1124. }
  1125. /**
  1126. * Gets a new Vector2 with current Vector2 negated coordinates
  1127. * @returns a new Vector2
  1128. */
  1129. public negate(): Vector2 {
  1130. return new Vector2(-this.x, -this.y);
  1131. }
  1132. /**
  1133. * Multiply the Vector2 coordinates by scale
  1134. * @param scale defines the scaling factor
  1135. * @returns the current updated Vector2
  1136. */
  1137. public scaleInPlace(scale: number): Vector2 {
  1138. this.x *= scale;
  1139. this.y *= scale;
  1140. return this;
  1141. }
  1142. /**
  1143. * Returns a new Vector2 scaled by "scale" from the current Vector2
  1144. * @param scale defines the scaling factor
  1145. * @returns a new Vector2
  1146. */
  1147. public scale(scale: number): Vector2 {
  1148. let result = new Vector2(0, 0);
  1149. this.scaleToRef(scale, result);
  1150. return result;
  1151. }
  1152. /**
  1153. * Scale the current Vector2 values by a factor to a given Vector2
  1154. * @param scale defines the scale factor
  1155. * @param result defines the Vector2 object where to store the result
  1156. * @returns the unmodified current Vector2
  1157. */
  1158. public scaleToRef(scale: number, result: Vector2): Vector2 {
  1159. result.x = this.x * scale;
  1160. result.y = this.y * scale;
  1161. return this;
  1162. }
  1163. /**
  1164. * Scale the current Vector2 values by a factor and add the result to a given Vector2
  1165. * @param scale defines the scale factor
  1166. * @param result defines the Vector2 object where to store the result
  1167. * @returns the unmodified current Vector2
  1168. */
  1169. public scaleAndAddToRef(scale: number, result: Vector2): Vector2 {
  1170. result.x += this.x * scale;
  1171. result.y += this.y * scale;
  1172. return this;
  1173. }
  1174. /**
  1175. * Gets a boolean if two vectors are equals
  1176. * @param otherVector defines the other vector
  1177. * @returns true if the given vector coordinates strictly equal the current Vector2 ones
  1178. */
  1179. public equals(otherVector: DeepImmutable<Vector2>): boolean {
  1180. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  1181. }
  1182. /**
  1183. * Gets a boolean if two vectors are equals (using an epsilon value)
  1184. * @param otherVector defines the other vector
  1185. * @param epsilon defines the minimal distance to consider equality
  1186. * @returns true if the given vector coordinates are close to the current ones by a distance of epsilon.
  1187. */
  1188. public equalsWithEpsilon(otherVector: DeepImmutable<Vector2>, epsilon: number = Epsilon): boolean {
  1189. return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon);
  1190. }
  1191. /**
  1192. * Gets a new Vector2 from current Vector2 floored values
  1193. * @returns a new Vector2
  1194. */
  1195. public floor(): Vector2 {
  1196. return new Vector2(Math.floor(this.x), Math.floor(this.y));
  1197. }
  1198. /**
  1199. * Gets a new Vector2 from current Vector2 floored values
  1200. * @returns a new Vector2
  1201. */
  1202. public fract(): Vector2 {
  1203. return new Vector2(this.x - Math.floor(this.x), this.y - Math.floor(this.y));
  1204. }
  1205. // Properties
  1206. /**
  1207. * Gets the length of the vector
  1208. * @returns the vector length (float)
  1209. */
  1210. public length(): number {
  1211. return Math.sqrt(this.x * this.x + this.y * this.y);
  1212. }
  1213. /**
  1214. * Gets the vector squared length
  1215. * @returns the vector squared length (float)
  1216. */
  1217. public lengthSquared(): number {
  1218. return (this.x * this.x + this.y * this.y);
  1219. }
  1220. // Methods
  1221. /**
  1222. * Normalize the vector
  1223. * @returns the current updated Vector2
  1224. */
  1225. public normalize(): Vector2 {
  1226. var len = this.length();
  1227. if (len === 0) {
  1228. return this;
  1229. }
  1230. var num = 1.0 / len;
  1231. this.x *= num;
  1232. this.y *= num;
  1233. return this;
  1234. }
  1235. /**
  1236. * Gets a new Vector2 copied from the Vector2
  1237. * @returns a new Vector2
  1238. */
  1239. public clone(): Vector2 {
  1240. return new Vector2(this.x, this.y);
  1241. }
  1242. // Statics
  1243. /**
  1244. * Gets a new Vector2(0, 0)
  1245. * @returns a new Vector2
  1246. */
  1247. public static Zero(): Vector2 {
  1248. return new Vector2(0, 0);
  1249. }
  1250. /**
  1251. * Gets a new Vector2(1, 1)
  1252. * @returns a new Vector2
  1253. */
  1254. public static One(): Vector2 {
  1255. return new Vector2(1, 1);
  1256. }
  1257. /**
  1258. * Gets a new Vector2 set from the given index element of the given array
  1259. * @param array defines the data source
  1260. * @param offset defines the offset in the data source
  1261. * @returns a new Vector2
  1262. */
  1263. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Vector2 {
  1264. return new Vector2(array[offset], array[offset + 1]);
  1265. }
  1266. /**
  1267. * Sets "result" from the given index element of the given array
  1268. * @param array defines the data source
  1269. * @param offset defines the offset in the data source
  1270. * @param result defines the target vector
  1271. */
  1272. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector2): void {
  1273. result.x = array[offset];
  1274. result.y = array[offset + 1];
  1275. }
  1276. /**
  1277. * Gets a new Vector2 located for "amount" (float) on the CatmullRom spline defined by the given four Vector2
  1278. * @param value1 defines 1st point of control
  1279. * @param value2 defines 2nd point of control
  1280. * @param value3 defines 3rd point of control
  1281. * @param value4 defines 4th point of control
  1282. * @param amount defines the interpolation factor
  1283. * @returns a new Vector2
  1284. */
  1285. public static CatmullRom(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>, value3: DeepImmutable<Vector2>, value4: DeepImmutable<Vector2>, amount: number): Vector2 {
  1286. var squared = amount * amount;
  1287. var cubed = amount * squared;
  1288. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  1289. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  1290. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  1291. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  1292. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  1293. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  1294. return new Vector2(x, y);
  1295. }
  1296. /**
  1297. * 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".
  1298. * If a coordinate of "value" is lower than "min" coordinates, the returned Vector2 is given this "min" coordinate.
  1299. * If a coordinate of "value" is greater than "max" coordinates, the returned Vector2 is given this "max" coordinate
  1300. * @param value defines the value to clamp
  1301. * @param min defines the lower limit
  1302. * @param max defines the upper limit
  1303. * @returns a new Vector2
  1304. */
  1305. public static Clamp(value: DeepImmutable<Vector2>, min: DeepImmutable<Vector2>, max: DeepImmutable<Vector2>): Vector2 {
  1306. var x = value.x;
  1307. x = (x > max.x) ? max.x : x;
  1308. x = (x < min.x) ? min.x : x;
  1309. var y = value.y;
  1310. y = (y > max.y) ? max.y : y;
  1311. y = (y < min.y) ? min.y : y;
  1312. return new Vector2(x, y);
  1313. }
  1314. /**
  1315. * Returns a new Vector2 located for "amount" (float) on the Hermite spline defined by the vectors "value1", "value3", "tangent1", "tangent2"
  1316. * @param value1 defines the 1st control point
  1317. * @param tangent1 defines the outgoing tangent
  1318. * @param value2 defines the 2nd control point
  1319. * @param tangent2 defines the incoming tangent
  1320. * @param amount defines the interpolation factor
  1321. * @returns a new Vector2
  1322. */
  1323. public static Hermite(value1: DeepImmutable<Vector2>, tangent1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>, tangent2: DeepImmutable<Vector2>, amount: number): Vector2 {
  1324. var squared = amount * amount;
  1325. var cubed = amount * squared;
  1326. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  1327. var part2 = (-2.0 * cubed) + (3.0 * squared);
  1328. var part3 = (cubed - (2.0 * squared)) + amount;
  1329. var part4 = cubed - squared;
  1330. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  1331. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  1332. return new Vector2(x, y);
  1333. }
  1334. /**
  1335. * Returns a new Vector2 located for "amount" (float) on the linear interpolation between the vector "start" adn the vector "end".
  1336. * @param start defines the start vector
  1337. * @param end defines the end vector
  1338. * @param amount defines the interpolation factor
  1339. * @returns a new Vector2
  1340. */
  1341. public static Lerp(start: DeepImmutable<Vector2>, end: DeepImmutable<Vector2>, amount: number): Vector2 {
  1342. var x = start.x + ((end.x - start.x) * amount);
  1343. var y = start.y + ((end.y - start.y) * amount);
  1344. return new Vector2(x, y);
  1345. }
  1346. /**
  1347. * Gets the dot product of the vector "left" and the vector "right"
  1348. * @param left defines first vector
  1349. * @param right defines second vector
  1350. * @returns the dot product (float)
  1351. */
  1352. public static Dot(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): number {
  1353. return left.x * right.x + left.y * right.y;
  1354. }
  1355. /**
  1356. * Returns a new Vector2 equal to the normalized given vector
  1357. * @param vector defines the vector to normalize
  1358. * @returns a new Vector2
  1359. */
  1360. public static Normalize(vector: DeepImmutable<Vector2>): Vector2 {
  1361. var newVector = vector.clone();
  1362. newVector.normalize();
  1363. return newVector;
  1364. }
  1365. /**
  1366. * Gets a new Vector2 set with the minimal coordinate values from the "left" and "right" vectors
  1367. * @param left defines 1st vector
  1368. * @param right defines 2nd vector
  1369. * @returns a new Vector2
  1370. */
  1371. public static Minimize(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): Vector2 {
  1372. var x = (left.x < right.x) ? left.x : right.x;
  1373. var y = (left.y < right.y) ? left.y : right.y;
  1374. return new Vector2(x, y);
  1375. }
  1376. /**
  1377. * Gets a new Vecto2 set with the maximal coordinate values from the "left" and "right" vectors
  1378. * @param left defines 1st vector
  1379. * @param right defines 2nd vector
  1380. * @returns a new Vector2
  1381. */
  1382. public static Maximize(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): Vector2 {
  1383. var x = (left.x > right.x) ? left.x : right.x;
  1384. var y = (left.y > right.y) ? left.y : right.y;
  1385. return new Vector2(x, y);
  1386. }
  1387. /**
  1388. * Gets a new Vector2 set with the transformed coordinates of the given vector by the given transformation matrix
  1389. * @param vector defines the vector to transform
  1390. * @param transformation defines the matrix to apply
  1391. * @returns a new Vector2
  1392. */
  1393. public static Transform(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Matrix>): Vector2 {
  1394. let r = Vector2.Zero();
  1395. Vector2.TransformToRef(vector, transformation, r);
  1396. return r;
  1397. }
  1398. /**
  1399. * Transforms the given vector coordinates by the given transformation matrix and stores the result in the vector "result" coordinates
  1400. * @param vector defines the vector to transform
  1401. * @param transformation defines the matrix to apply
  1402. * @param result defines the target vector
  1403. */
  1404. public static TransformToRef(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Matrix>, result: Vector2) {
  1405. const m = transformation.m;
  1406. var x = (vector.x * m[0]) + (vector.y * m[4]) + m[12];
  1407. var y = (vector.x * m[1]) + (vector.y * m[5]) + m[13];
  1408. result.x = x;
  1409. result.y = y;
  1410. }
  1411. /**
  1412. * Determines if a given vector is included in a triangle
  1413. * @param p defines the vector to test
  1414. * @param p0 defines 1st triangle point
  1415. * @param p1 defines 2nd triangle point
  1416. * @param p2 defines 3rd triangle point
  1417. * @returns true if the point "p" is in the triangle defined by the vertors "p0", "p1", "p2"
  1418. */
  1419. public static PointInTriangle(p: DeepImmutable<Vector2>, p0: DeepImmutable<Vector2>, p1: DeepImmutable<Vector2>, p2: DeepImmutable<Vector2>) {
  1420. let a = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  1421. let sign = a < 0 ? -1 : 1;
  1422. let s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
  1423. let t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
  1424. return s > 0 && t > 0 && (s + t) < 2 * a * sign;
  1425. }
  1426. /**
  1427. * Gets the distance between the vectors "value1" and "value2"
  1428. * @param value1 defines first vector
  1429. * @param value2 defines second vector
  1430. * @returns the distance between vectors
  1431. */
  1432. public static Distance(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): number {
  1433. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  1434. }
  1435. /**
  1436. * Returns the squared distance between the vectors "value1" and "value2"
  1437. * @param value1 defines first vector
  1438. * @param value2 defines second vector
  1439. * @returns the squared distance between vectors
  1440. */
  1441. public static DistanceSquared(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): number {
  1442. var x = value1.x - value2.x;
  1443. var y = value1.y - value2.y;
  1444. return (x * x) + (y * y);
  1445. }
  1446. /**
  1447. * Gets a new Vector2 located at the center of the vectors "value1" and "value2"
  1448. * @param value1 defines first vector
  1449. * @param value2 defines second vector
  1450. * @returns a new Vector2
  1451. */
  1452. public static Center(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): Vector2 {
  1453. var center = value1.add(value2);
  1454. center.scaleInPlace(0.5);
  1455. return center;
  1456. }
  1457. /**
  1458. * Gets the shortest distance (float) between the point "p" and the segment defined by the two points "segA" and "segB".
  1459. * @param p defines the middle point
  1460. * @param segA defines one point of the segment
  1461. * @param segB defines the other point of the segment
  1462. * @returns the shortest distance
  1463. */
  1464. public static DistanceOfPointFromSegment(p: DeepImmutable<Vector2>, segA: DeepImmutable<Vector2>, segB: DeepImmutable<Vector2>): number {
  1465. let l2 = Vector2.DistanceSquared(segA, segB);
  1466. if (l2 === 0.0) {
  1467. return Vector2.Distance(p, segA);
  1468. }
  1469. let v = segB.subtract(segA);
  1470. let t = Math.max(0, Math.min(1, Vector2.Dot(p.subtract(segA), v) / l2));
  1471. let proj = segA.add(v.multiplyByFloats(t, t));
  1472. return Vector2.Distance(p, proj);
  1473. }
  1474. }
  1475. /**
  1476. * Classed used to store (x,y,z) vector representation
  1477. * A Vector3 is the main object used in 3D geometry
  1478. * It can represent etiher the coordinates of a point the space, either a direction
  1479. * Reminder: js uses a left handed forward facing system
  1480. */
  1481. export class Vector3 {
  1482. private static _UpReadOnly = Vector3.Up() as DeepImmutable<Vector3>;
  1483. private static _ZeroReadOnly = Vector3.Zero() as DeepImmutable<Vector3>;
  1484. /**
  1485. * Creates a new Vector3 object from the given x, y, z (floats) coordinates.
  1486. * @param x defines the first coordinates (on X axis)
  1487. * @param y defines the second coordinates (on Y axis)
  1488. * @param z defines the third coordinates (on Z axis)
  1489. */
  1490. constructor(
  1491. /**
  1492. * Defines the first coordinates (on X axis)
  1493. */
  1494. public x: number = 0,
  1495. /**
  1496. * Defines the second coordinates (on Y axis)
  1497. */
  1498. public y: number = 0,
  1499. /**
  1500. * Defines the third coordinates (on Z axis)
  1501. */
  1502. public z: number = 0
  1503. ) {
  1504. }
  1505. /**
  1506. * Creates a string representation of the Vector3
  1507. * @returns a string with the Vector3 coordinates.
  1508. */
  1509. public toString(): string {
  1510. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  1511. }
  1512. /**
  1513. * Gets the class name
  1514. * @returns the string "Vector3"
  1515. */
  1516. public getClassName(): string {
  1517. return "Vector3";
  1518. }
  1519. /**
  1520. * Creates the Vector3 hash code
  1521. * @returns a number which tends to be unique between Vector3 instances
  1522. */
  1523. public getHashCode(): number {
  1524. let hash = this.x | 0;
  1525. hash = (hash * 397) ^ (this.y | 0);
  1526. hash = (hash * 397) ^ (this.z | 0);
  1527. return hash;
  1528. }
  1529. // Operators
  1530. /**
  1531. * Creates an array containing three elements : the coordinates of the Vector3
  1532. * @returns a new array of numbers
  1533. */
  1534. public asArray(): number[] {
  1535. var result: number[] = [];
  1536. this.toArray(result, 0);
  1537. return result;
  1538. }
  1539. /**
  1540. * Populates the given array or Float32Array from the given index with the successive coordinates of the Vector3
  1541. * @param array defines the destination array
  1542. * @param index defines the offset in the destination array
  1543. * @returns the current Vector3
  1544. */
  1545. public toArray(array: FloatArray, index: number = 0): Vector3 {
  1546. array[index] = this.x;
  1547. array[index + 1] = this.y;
  1548. array[index + 2] = this.z;
  1549. return this;
  1550. }
  1551. /**
  1552. * Converts the current Vector3 into a quaternion (considering that the Vector3 contains Euler angles representation of a rotation)
  1553. * @returns a new Quaternion object, computed from the Vector3 coordinates
  1554. */
  1555. public toQuaternion(): Quaternion {
  1556. return Quaternion.RotationYawPitchRoll(this.y, this.x, this.z);
  1557. }
  1558. /**
  1559. * Adds the given vector to the current Vector3
  1560. * @param otherVector defines the second operand
  1561. * @returns the current updated Vector3
  1562. */
  1563. public addInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  1564. return this.addInPlaceFromFloats(otherVector.x, otherVector.y, otherVector.z);
  1565. }
  1566. /**
  1567. * Adds the given coordinates to the current Vector3
  1568. * @param x defines the x coordinate of the operand
  1569. * @param y defines the y coordinate of the operand
  1570. * @param z defines the z coordinate of the operand
  1571. * @returns the current updated Vector3
  1572. */
  1573. public addInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  1574. this.x += x;
  1575. this.y += y;
  1576. this.z += z;
  1577. return this;
  1578. }
  1579. /**
  1580. * Gets a new Vector3, result of the addition the current Vector3 and the given vector
  1581. * @param otherVector defines the second operand
  1582. * @returns the resulting Vector3
  1583. */
  1584. public add(otherVector: DeepImmutable<Vector3>): Vector3 {
  1585. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  1586. }
  1587. /**
  1588. * Adds the current Vector3 to the given one and stores the result in the vector "result"
  1589. * @param otherVector defines the second operand
  1590. * @param result defines the Vector3 object where to store the result
  1591. * @returns the current Vector3
  1592. */
  1593. public addToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  1594. return result.copyFromFloats(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  1595. }
  1596. /**
  1597. * Subtract the given vector from the current Vector3
  1598. * @param otherVector defines the second operand
  1599. * @returns the current updated Vector3
  1600. */
  1601. public subtractInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  1602. this.x -= otherVector.x;
  1603. this.y -= otherVector.y;
  1604. this.z -= otherVector.z;
  1605. return this;
  1606. }
  1607. /**
  1608. * Returns a new Vector3, result of the subtraction of the given vector from the current Vector3
  1609. * @param otherVector defines the second operand
  1610. * @returns the resulting Vector3
  1611. */
  1612. public subtract(otherVector: DeepImmutable<Vector3>): Vector3 {
  1613. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  1614. }
  1615. /**
  1616. * Subtracts the given vector from the current Vector3 and stores the result in the vector "result".
  1617. * @param otherVector defines the second operand
  1618. * @param result defines the Vector3 object where to store the result
  1619. * @returns the current Vector3
  1620. */
  1621. public subtractToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  1622. return this.subtractFromFloatsToRef(otherVector.x, otherVector.y, otherVector.z, result);
  1623. }
  1624. /**
  1625. * Returns a new Vector3 set with the subtraction of the given floats from the current Vector3 coordinates
  1626. * @param x defines the x coordinate of the operand
  1627. * @param y defines the y coordinate of the operand
  1628. * @param z defines the z coordinate of the operand
  1629. * @returns the resulting Vector3
  1630. */
  1631. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  1632. return new Vector3(this.x - x, this.y - y, this.z - z);
  1633. }
  1634. /**
  1635. * Subtracts the given floats from the current Vector3 coordinates and set the given vector "result" with this result
  1636. * @param x defines the x coordinate of the operand
  1637. * @param y defines the y coordinate of the operand
  1638. * @param z defines the z coordinate of the operand
  1639. * @param result defines the Vector3 object where to store the result
  1640. * @returns the current Vector3
  1641. */
  1642. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
  1643. return result.copyFromFloats(this.x - x, this.y - y, this.z - z);
  1644. }
  1645. /**
  1646. * Gets a new Vector3 set with the current Vector3 negated coordinates
  1647. * @returns a new Vector3
  1648. */
  1649. public negate(): Vector3 {
  1650. return new Vector3(-this.x, -this.y, -this.z);
  1651. }
  1652. /**
  1653. * Multiplies the Vector3 coordinates by the float "scale"
  1654. * @param scale defines the multiplier factor
  1655. * @returns the current updated Vector3
  1656. */
  1657. public scaleInPlace(scale: number): Vector3 {
  1658. this.x *= scale;
  1659. this.y *= scale;
  1660. this.z *= scale;
  1661. return this;
  1662. }
  1663. /**
  1664. * Returns a new Vector3 set with the current Vector3 coordinates multiplied by the float "scale"
  1665. * @param scale defines the multiplier factor
  1666. * @returns a new Vector3
  1667. */
  1668. public scale(scale: number): Vector3 {
  1669. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  1670. }
  1671. /**
  1672. * Multiplies the current Vector3 coordinates by the float "scale" and stores the result in the given vector "result" coordinates
  1673. * @param scale defines the multiplier factor
  1674. * @param result defines the Vector3 object where to store the result
  1675. * @returns the current Vector3
  1676. */
  1677. public scaleToRef(scale: number, result: Vector3): Vector3 {
  1678. return result.copyFromFloats(this.x * scale, this.y * scale, this.z * scale);
  1679. }
  1680. /**
  1681. * Scale the current Vector3 values by a factor and add the result to a given Vector3
  1682. * @param scale defines the scale factor
  1683. * @param result defines the Vector3 object where to store the result
  1684. * @returns the unmodified current Vector3
  1685. */
  1686. public scaleAndAddToRef(scale: number, result: Vector3): Vector3 {
  1687. return result.addInPlaceFromFloats(this.x * scale, this.y * scale, this.z * scale);
  1688. }
  1689. /**
  1690. * Returns true if the current Vector3 and the given vector coordinates are strictly equal
  1691. * @param otherVector defines the second operand
  1692. * @returns true if both vectors are equals
  1693. */
  1694. public equals(otherVector: DeepImmutable<Vector3>): boolean {
  1695. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  1696. }
  1697. /**
  1698. * Returns true if the current Vector3 and the given vector coordinates are distant less than epsilon
  1699. * @param otherVector defines the second operand
  1700. * @param epsilon defines the minimal distance to define values as equals
  1701. * @returns true if both vectors are distant less than epsilon
  1702. */
  1703. public equalsWithEpsilon(otherVector: DeepImmutable<Vector3>, epsilon: number = Epsilon): boolean {
  1704. return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon) && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon);
  1705. }
  1706. /**
  1707. * Returns true if the current Vector3 coordinates equals the given floats
  1708. * @param x defines the x coordinate of the operand
  1709. * @param y defines the y coordinate of the operand
  1710. * @param z defines the z coordinate of the operand
  1711. * @returns true if both vectors are equals
  1712. */
  1713. public equalsToFloats(x: number, y: number, z: number): boolean {
  1714. return this.x === x && this.y === y && this.z === z;
  1715. }
  1716. /**
  1717. * Multiplies the current Vector3 coordinates by the given ones
  1718. * @param otherVector defines the second operand
  1719. * @returns the current updated Vector3
  1720. */
  1721. public multiplyInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  1722. this.x *= otherVector.x;
  1723. this.y *= otherVector.y;
  1724. this.z *= otherVector.z;
  1725. return this;
  1726. }
  1727. /**
  1728. * Returns a new Vector3, result of the multiplication of the current Vector3 by the given vector
  1729. * @param otherVector defines the second operand
  1730. * @returns the new Vector3
  1731. */
  1732. public multiply(otherVector: DeepImmutable<Vector3>): Vector3 {
  1733. return this.multiplyByFloats(otherVector.x, otherVector.y, otherVector.z);
  1734. }
  1735. /**
  1736. * Multiplies the current Vector3 by the given one and stores the result in the given vector "result"
  1737. * @param otherVector defines the second operand
  1738. * @param result defines the Vector3 object where to store the result
  1739. * @returns the current Vector3
  1740. */
  1741. public multiplyToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  1742. return result.copyFromFloats(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  1743. }
  1744. /**
  1745. * Returns a new Vector3 set with the result of the mulliplication of the current Vector3 coordinates by the given floats
  1746. * @param x defines the x coordinate of the operand
  1747. * @param y defines the y coordinate of the operand
  1748. * @param z defines the z coordinate of the operand
  1749. * @returns the new Vector3
  1750. */
  1751. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  1752. return new Vector3(this.x * x, this.y * y, this.z * z);
  1753. }
  1754. /**
  1755. * Returns a new Vector3 set with the result of the division of the current Vector3 coordinates by the given ones
  1756. * @param otherVector defines the second operand
  1757. * @returns the new Vector3
  1758. */
  1759. public divide(otherVector: DeepImmutable<Vector3>): Vector3 {
  1760. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  1761. }
  1762. /**
  1763. * Divides the current Vector3 coordinates by the given ones and stores the result in the given vector "result"
  1764. * @param otherVector defines the second operand
  1765. * @param result defines the Vector3 object where to store the result
  1766. * @returns the current Vector3
  1767. */
  1768. public divideToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  1769. return result.copyFromFloats(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  1770. }
  1771. /**
  1772. * Divides the current Vector3 coordinates by the given ones.
  1773. * @param otherVector defines the second operand
  1774. * @returns the current updated Vector3
  1775. */
  1776. public divideInPlace(otherVector: Vector3): Vector3 {
  1777. return this.divideToRef(otherVector, this);
  1778. }
  1779. /**
  1780. * Updates the current Vector3 with the minimal coordinate values between its and the given vector ones
  1781. * @param other defines the second operand
  1782. * @returns the current updated Vector3
  1783. */
  1784. public minimizeInPlace(other: DeepImmutable<Vector3>): Vector3 {
  1785. return this.minimizeInPlaceFromFloats(other.x, other.y, other.z);
  1786. }
  1787. /**
  1788. * Updates the current Vector3 with the maximal coordinate values between its and the given vector ones.
  1789. * @param other defines the second operand
  1790. * @returns the current updated Vector3
  1791. */
  1792. public maximizeInPlace(other: DeepImmutable<Vector3>): Vector3 {
  1793. return this.maximizeInPlaceFromFloats(other.x, other.y, other.z);
  1794. }
  1795. /**
  1796. * Updates the current Vector3 with the minimal coordinate values between its and the given coordinates
  1797. * @param x defines the x coordinate of the operand
  1798. * @param y defines the y coordinate of the operand
  1799. * @param z defines the z coordinate of the operand
  1800. * @returns the current updated Vector3
  1801. */
  1802. public minimizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  1803. if (x < this.x) { this.x = x; }
  1804. if (y < this.y) { this.y = y; }
  1805. if (z < this.z) { this.z = z; }
  1806. return this;
  1807. }
  1808. /**
  1809. * Updates the current Vector3 with the maximal coordinate values between its and the given coordinates.
  1810. * @param x defines the x coordinate of the operand
  1811. * @param y defines the y coordinate of the operand
  1812. * @param z defines the z coordinate of the operand
  1813. * @returns the current updated Vector3
  1814. */
  1815. public maximizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  1816. if (x > this.x) { this.x = x; }
  1817. if (y > this.y) { this.y = y; }
  1818. if (z > this.z) { this.z = z; }
  1819. return this;
  1820. }
  1821. /**
  1822. * Due to float precision, scale of a mesh could be uniform but float values are off by a small fraction
  1823. * Check if is non uniform within a certain amount of decimal places to account for this
  1824. * @param epsilon the amount the values can differ
  1825. * @returns if the the vector is non uniform to a certain number of decimal places
  1826. */
  1827. public isNonUniformWithinEpsilon(epsilon: number) {
  1828. let absX = Math.abs(this.x);
  1829. let absY = Math.abs(this.y);
  1830. if (!Scalar.WithinEpsilon(absX, absY, epsilon)) {
  1831. return true;
  1832. }
  1833. let absZ = Math.abs(this.z);
  1834. if (!Scalar.WithinEpsilon(absX, absZ, epsilon)) {
  1835. return true;
  1836. }
  1837. if (!Scalar.WithinEpsilon(absY, absZ, epsilon)) {
  1838. return true;
  1839. }
  1840. return false;
  1841. }
  1842. /**
  1843. * Gets a boolean indicating that the vector is non uniform meaning x, y or z are not all the same
  1844. */
  1845. public get isNonUniform(): boolean {
  1846. let absX = Math.abs(this.x);
  1847. let absY = Math.abs(this.y);
  1848. if (absX !== absY) {
  1849. return true;
  1850. }
  1851. let absZ = Math.abs(this.z);
  1852. if (absX !== absZ) {
  1853. return true;
  1854. }
  1855. if (absY !== absZ) {
  1856. return true;
  1857. }
  1858. return false;
  1859. }
  1860. /**
  1861. * Gets a new Vector3 from current Vector3 floored values
  1862. * @returns a new Vector3
  1863. */
  1864. public floor(): Vector3 {
  1865. return new Vector3(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z));
  1866. }
  1867. /**
  1868. * Gets a new Vector3 from current Vector3 floored values
  1869. * @returns a new Vector3
  1870. */
  1871. public fract(): Vector3 {
  1872. return new Vector3(this.x - Math.floor(this.x), this.y - Math.floor(this.y), this.z - Math.floor(this.z));
  1873. }
  1874. // Properties
  1875. /**
  1876. * Gets the length of the Vector3
  1877. * @returns the length of the Vector3
  1878. */
  1879. public length(): number {
  1880. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  1881. }
  1882. /**
  1883. * Gets the squared length of the Vector3
  1884. * @returns squared length of the Vector3
  1885. */
  1886. public lengthSquared(): number {
  1887. return (this.x * this.x + this.y * this.y + this.z * this.z);
  1888. }
  1889. /**
  1890. * Normalize the current Vector3.
  1891. * Please note that this is an in place operation.
  1892. * @returns the current updated Vector3
  1893. */
  1894. public normalize(): Vector3 {
  1895. return this.normalizeFromLength(this.length());
  1896. }
  1897. /**
  1898. * Reorders the x y z properties of the vector in place
  1899. * @param order new ordering of the properties (eg. for vector 1,2,3 with "ZYX" will produce 3,2,1)
  1900. * @returns the current updated vector
  1901. */
  1902. public reorderInPlace(order: string) {
  1903. order = order.toLowerCase();
  1904. if (order === "xyz") {
  1905. return this;
  1906. }
  1907. MathTmp.Vector3[0].copyFrom(this);
  1908. ["x", "y", "z"].forEach((val, i) => {
  1909. (<any>this)[val] = (<any>MathTmp.Vector3[0])[order[i]];
  1910. });
  1911. return this;
  1912. }
  1913. /**
  1914. * Rotates the vector around 0,0,0 by a quaternion
  1915. * @param quaternion the rotation quaternion
  1916. * @param result vector to store the result
  1917. * @returns the resulting vector
  1918. */
  1919. public rotateByQuaternionToRef(quaternion: Quaternion, result: Vector3) {
  1920. quaternion.toRotationMatrix(MathTmp.Matrix[0]);
  1921. Vector3.TransformCoordinatesToRef(this, MathTmp.Matrix[0], result);
  1922. return result;
  1923. }
  1924. /**
  1925. * Rotates a vector around a given point
  1926. * @param quaternion the rotation quaternion
  1927. * @param point the point to rotate around
  1928. * @param result vector to store the result
  1929. * @returns the resulting vector
  1930. */
  1931. public rotateByQuaternionAroundPointToRef(quaternion: Quaternion, point: Vector3, result: Vector3) {
  1932. this.subtractToRef(point, MathTmp.Vector3[0]);
  1933. MathTmp.Vector3[0].rotateByQuaternionToRef(quaternion, MathTmp.Vector3[0]);
  1934. point.addToRef(MathTmp.Vector3[0], result);
  1935. return result;
  1936. }
  1937. /**
  1938. * Normalize the current Vector3 with the given input length.
  1939. * Please note that this is an in place operation.
  1940. * @param len the length of the vector
  1941. * @returns the current updated Vector3
  1942. */
  1943. public normalizeFromLength(len: number): Vector3 {
  1944. if (len === 0 || len === 1.0) {
  1945. return this;
  1946. }
  1947. return this.scaleInPlace(1.0 / len);
  1948. }
  1949. /**
  1950. * Normalize the current Vector3 to a new vector
  1951. * @returns the new Vector3
  1952. */
  1953. public normalizeToNew(): Vector3 {
  1954. const normalized = new Vector3(0, 0, 0);
  1955. this.normalizeToRef(normalized);
  1956. return normalized;
  1957. }
  1958. /**
  1959. * Normalize the current Vector3 to the reference
  1960. * @param reference define the Vector3 to update
  1961. * @returns the updated Vector3
  1962. */
  1963. public normalizeToRef(reference: DeepImmutable<Vector3>): Vector3 {
  1964. var len = this.length();
  1965. if (len === 0 || len === 1.0) {
  1966. return reference.copyFromFloats(this.x, this.y, this.z);
  1967. }
  1968. return this.scaleToRef(1.0 / len, reference);
  1969. }
  1970. /**
  1971. * Creates a new Vector3 copied from the current Vector3
  1972. * @returns the new Vector3
  1973. */
  1974. public clone(): Vector3 {
  1975. return new Vector3(this.x, this.y, this.z);
  1976. }
  1977. /**
  1978. * Copies the given vector coordinates to the current Vector3 ones
  1979. * @param source defines the source Vector3
  1980. * @returns the current updated Vector3
  1981. */
  1982. public copyFrom(source: DeepImmutable<Vector3>): Vector3 {
  1983. return this.copyFromFloats(source.x, source.y, source.z);
  1984. }
  1985. /**
  1986. * Copies the given floats to the current Vector3 coordinates
  1987. * @param x defines the x coordinate of the operand
  1988. * @param y defines the y coordinate of the operand
  1989. * @param z defines the z coordinate of the operand
  1990. * @returns the current updated Vector3
  1991. */
  1992. public copyFromFloats(x: number, y: number, z: number): Vector3 {
  1993. this.x = x;
  1994. this.y = y;
  1995. this.z = z;
  1996. return this;
  1997. }
  1998. /**
  1999. * Copies the given floats to the current Vector3 coordinates
  2000. * @param x defines the x coordinate of the operand
  2001. * @param y defines the y coordinate of the operand
  2002. * @param z defines the z coordinate of the operand
  2003. * @returns the current updated Vector3
  2004. */
  2005. public set(x: number, y: number, z: number): Vector3 {
  2006. return this.copyFromFloats(x, y, z);
  2007. }
  2008. /**
  2009. * Copies the given float to the current Vector3 coordinates
  2010. * @param v defines the x, y and z coordinates of the operand
  2011. * @returns the current updated Vector3
  2012. */
  2013. public setAll(v: number): Vector3 {
  2014. this.x = this.y = this.z = v;
  2015. return this;
  2016. }
  2017. // Statics
  2018. /**
  2019. * Get the clip factor between two vectors
  2020. * @param vector0 defines the first operand
  2021. * @param vector1 defines the second operand
  2022. * @param axis defines the axis to use
  2023. * @param size defines the size along the axis
  2024. * @returns the clip factor
  2025. */
  2026. public static GetClipFactor(vector0: DeepImmutable<Vector3>, vector1: DeepImmutable<Vector3>, axis: DeepImmutable<Vector3>, size: number) {
  2027. var d0 = Vector3.Dot(vector0, axis) - size;
  2028. var d1 = Vector3.Dot(vector1, axis) - size;
  2029. var s = d0 / (d0 - d1);
  2030. return s;
  2031. }
  2032. /**
  2033. * Get angle between two vectors
  2034. * @param vector0 angle between vector0 and vector1
  2035. * @param vector1 angle between vector0 and vector1
  2036. * @param normal direction of the normal
  2037. * @return the angle between vector0 and vector1
  2038. */
  2039. public static GetAngleBetweenVectors(vector0: DeepImmutable<Vector3>, vector1: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>): number {
  2040. const v0: Vector3 = vector0.normalizeToRef(MathTmp.Vector3[1]);
  2041. const v1: Vector3 = vector1.normalizeToRef(MathTmp.Vector3[2]);
  2042. const dot: number = Vector3.Dot(v0, v1);
  2043. const n = MathTmp.Vector3[3];
  2044. Vector3.CrossToRef(v0, v1, n);
  2045. if (Vector3.Dot(n, normal) > 0) {
  2046. return Math.acos(dot);
  2047. }
  2048. return -Math.acos(dot);
  2049. }
  2050. /**
  2051. * Returns a new Vector3 set from the index "offset" of the given array
  2052. * @param array defines the source array
  2053. * @param offset defines the offset in the source array
  2054. * @returns the new Vector3
  2055. */
  2056. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Vector3 {
  2057. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  2058. }
  2059. /**
  2060. * Returns a new Vector3 set from the index "offset" of the given Float32Array
  2061. * This function is deprecated. Use FromArray instead
  2062. * @param array defines the source array
  2063. * @param offset defines the offset in the source array
  2064. * @returns the new Vector3
  2065. */
  2066. public static FromFloatArray(array: DeepImmutable<Float32Array>, offset?: number): Vector3 {
  2067. return Vector3.FromArray(array, offset);
  2068. }
  2069. /**
  2070. * Sets the given vector "result" with the element values from the index "offset" of the given array
  2071. * @param array defines the source array
  2072. * @param offset defines the offset in the source array
  2073. * @param result defines the Vector3 where to store the result
  2074. */
  2075. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector3): void {
  2076. result.x = array[offset];
  2077. result.y = array[offset + 1];
  2078. result.z = array[offset + 2];
  2079. }
  2080. /**
  2081. * Sets the given vector "result" with the element values from the index "offset" of the given Float32Array
  2082. * This function is deprecated. Use FromArrayToRef instead.
  2083. * @param array defines the source array
  2084. * @param offset defines the offset in the source array
  2085. * @param result defines the Vector3 where to store the result
  2086. */
  2087. public static FromFloatArrayToRef(array: DeepImmutable<Float32Array>, offset: number, result: Vector3): void {
  2088. return Vector3.FromArrayToRef(array, offset, result);
  2089. }
  2090. /**
  2091. * Sets the given vector "result" with the given floats.
  2092. * @param x defines the x coordinate of the source
  2093. * @param y defines the y coordinate of the source
  2094. * @param z defines the z coordinate of the source
  2095. * @param result defines the Vector3 where to store the result
  2096. */
  2097. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  2098. result.copyFromFloats(x, y, z);
  2099. }
  2100. /**
  2101. * Returns a new Vector3 set to (0.0, 0.0, 0.0)
  2102. * @returns a new empty Vector3
  2103. */
  2104. public static Zero(): Vector3 {
  2105. return new Vector3(0.0, 0.0, 0.0);
  2106. }
  2107. /**
  2108. * Returns a new Vector3 set to (1.0, 1.0, 1.0)
  2109. * @returns a new unit Vector3
  2110. */
  2111. public static One(): Vector3 {
  2112. return new Vector3(1.0, 1.0, 1.0);
  2113. }
  2114. /**
  2115. * Returns a new Vector3 set to (0.0, 1.0, 0.0)
  2116. * @returns a new up Vector3
  2117. */
  2118. public static Up(): Vector3 {
  2119. return new Vector3(0.0, 1.0, 0.0);
  2120. }
  2121. /**
  2122. * Gets a up Vector3 that must not be updated
  2123. */
  2124. public static get UpReadOnly(): DeepImmutable<Vector3> {
  2125. return Vector3._UpReadOnly;
  2126. }
  2127. /**
  2128. * Gets a zero Vector3 that must not be updated
  2129. */
  2130. public static get ZeroReadOnly(): DeepImmutable<Vector3> {
  2131. return Vector3._ZeroReadOnly;
  2132. }
  2133. /**
  2134. * Returns a new Vector3 set to (0.0, -1.0, 0.0)
  2135. * @returns a new down Vector3
  2136. */
  2137. public static Down(): Vector3 {
  2138. return new Vector3(0.0, -1.0, 0.0);
  2139. }
  2140. /**
  2141. * Returns a new Vector3 set to (0.0, 0.0, 1.0)
  2142. * @returns a new forward Vector3
  2143. */
  2144. public static Forward(): Vector3 {
  2145. return new Vector3(0.0, 0.0, 1.0);
  2146. }
  2147. /**
  2148. * Returns a new Vector3 set to (0.0, 0.0, -1.0)
  2149. * @returns a new forward Vector3
  2150. */
  2151. public static Backward(): Vector3 {
  2152. return new Vector3(0.0, 0.0, -1.0);
  2153. }
  2154. /**
  2155. * Returns a new Vector3 set to (1.0, 0.0, 0.0)
  2156. * @returns a new right Vector3
  2157. */
  2158. public static Right(): Vector3 {
  2159. return new Vector3(1.0, 0.0, 0.0);
  2160. }
  2161. /**
  2162. * Returns a new Vector3 set to (-1.0, 0.0, 0.0)
  2163. * @returns a new left Vector3
  2164. */
  2165. public static Left(): Vector3 {
  2166. return new Vector3(-1.0, 0.0, 0.0);
  2167. }
  2168. /**
  2169. * Returns a new Vector3 set with the result of the transformation by the given matrix of the given vector.
  2170. * This method computes tranformed coordinates only, not transformed direction vectors (ie. it takes translation in account)
  2171. * @param vector defines the Vector3 to transform
  2172. * @param transformation defines the transformation matrix
  2173. * @returns the transformed Vector3
  2174. */
  2175. public static TransformCoordinates(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>): Vector3 {
  2176. var result = Vector3.Zero();
  2177. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  2178. return result;
  2179. }
  2180. /**
  2181. * Sets the given vector "result" coordinates with the result of the transformation by the given matrix of the given vector
  2182. * This method computes tranformed coordinates only, not transformed direction vectors (ie. it takes translation in account)
  2183. * @param vector defines the Vector3 to transform
  2184. * @param transformation defines the transformation matrix
  2185. * @param result defines the Vector3 where to store the result
  2186. */
  2187. public static TransformCoordinatesToRef(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  2188. Vector3.TransformCoordinatesFromFloatsToRef(vector.x, vector.y, vector.z, transformation, result);
  2189. }
  2190. /**
  2191. * Sets the given vector "result" coordinates with the result of the transformation by the given matrix of the given floats (x, y, z)
  2192. * This method computes tranformed coordinates only, not transformed direction vectors
  2193. * @param x define the x coordinate of the source vector
  2194. * @param y define the y coordinate of the source vector
  2195. * @param z define the z coordinate of the source vector
  2196. * @param transformation defines the transformation matrix
  2197. * @param result defines the Vector3 where to store the result
  2198. */
  2199. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  2200. const m = transformation.m;
  2201. var rx = x * m[0] + y * m[4] + z * m[8] + m[12];
  2202. var ry = x * m[1] + y * m[5] + z * m[9] + m[13];
  2203. var rz = x * m[2] + y * m[6] + z * m[10] + m[14];
  2204. var rw = 1 / (x * m[3] + y * m[7] + z * m[11] + m[15]);
  2205. result.x = rx * rw;
  2206. result.y = ry * rw;
  2207. result.z = rz * rw;
  2208. }
  2209. /**
  2210. * Returns a new Vector3 set with the result of the normal transformation by the given matrix of the given vector
  2211. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  2212. * @param vector defines the Vector3 to transform
  2213. * @param transformation defines the transformation matrix
  2214. * @returns the new Vector3
  2215. */
  2216. public static TransformNormal(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>): Vector3 {
  2217. var result = Vector3.Zero();
  2218. Vector3.TransformNormalToRef(vector, transformation, result);
  2219. return result;
  2220. }
  2221. /**
  2222. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given vector
  2223. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  2224. * @param vector defines the Vector3 to transform
  2225. * @param transformation defines the transformation matrix
  2226. * @param result defines the Vector3 where to store the result
  2227. */
  2228. public static TransformNormalToRef(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  2229. this.TransformNormalFromFloatsToRef(vector.x, vector.y, vector.z, transformation, result);
  2230. }
  2231. /**
  2232. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given floats (x, y, z)
  2233. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  2234. * @param x define the x coordinate of the source vector
  2235. * @param y define the y coordinate of the source vector
  2236. * @param z define the z coordinate of the source vector
  2237. * @param transformation defines the transformation matrix
  2238. * @param result defines the Vector3 where to store the result
  2239. */
  2240. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  2241. const m = transformation.m;
  2242. result.x = x * m[0] + y * m[4] + z * m[8];
  2243. result.y = x * m[1] + y * m[5] + z * m[9];
  2244. result.z = x * m[2] + y * m[6] + z * m[10];
  2245. }
  2246. /**
  2247. * Returns a new Vector3 located for "amount" on the CatmullRom interpolation spline defined by the vectors "value1", "value2", "value3", "value4"
  2248. * @param value1 defines the first control point
  2249. * @param value2 defines the second control point
  2250. * @param value3 defines the third control point
  2251. * @param value4 defines the fourth control point
  2252. * @param amount defines the amount on the spline to use
  2253. * @returns the new Vector3
  2254. */
  2255. public static CatmullRom(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>, value3: DeepImmutable<Vector3>, value4: DeepImmutable<Vector3>, amount: number): Vector3 {
  2256. var squared = amount * amount;
  2257. var cubed = amount * squared;
  2258. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  2259. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  2260. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  2261. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  2262. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  2263. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  2264. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  2265. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  2266. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  2267. return new Vector3(x, y, z);
  2268. }
  2269. /**
  2270. * 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"
  2271. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one
  2272. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one
  2273. * @param value defines the current value
  2274. * @param min defines the lower range value
  2275. * @param max defines the upper range value
  2276. * @returns the new Vector3
  2277. */
  2278. public static Clamp(value: DeepImmutable<Vector3>, min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>): Vector3 {
  2279. const v = new Vector3();
  2280. Vector3.ClampToRef(value, min, max, v);
  2281. return v;
  2282. }
  2283. /**
  2284. * 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"
  2285. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one
  2286. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one
  2287. * @param value defines the current value
  2288. * @param min defines the lower range value
  2289. * @param max defines the upper range value
  2290. * @param result defines the Vector3 where to store the result
  2291. */
  2292. public static ClampToRef(value: DeepImmutable<Vector3>, min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>, result: Vector3): void {
  2293. var x = value.x;
  2294. x = (x > max.x) ? max.x : x;
  2295. x = (x < min.x) ? min.x : x;
  2296. var y = value.y;
  2297. y = (y > max.y) ? max.y : y;
  2298. y = (y < min.y) ? min.y : y;
  2299. var z = value.z;
  2300. z = (z > max.z) ? max.z : z;
  2301. z = (z < min.z) ? min.z : z;
  2302. result.copyFromFloats(x, y, z);
  2303. }
  2304. /**
  2305. * Checks if a given vector is inside a specific range
  2306. * @param v defines the vector to test
  2307. * @param min defines the minimum range
  2308. * @param max defines the maximum range
  2309. */
  2310. public static CheckExtends(v: Vector3, min: Vector3, max: Vector3): void {
  2311. min.minimizeInPlace(v);
  2312. max.maximizeInPlace(v);
  2313. }
  2314. /**
  2315. * Returns a new Vector3 located for "amount" (float) on the Hermite interpolation spline defined by the vectors "value1", "tangent1", "value2", "tangent2"
  2316. * @param value1 defines the first control point
  2317. * @param tangent1 defines the first tangent vector
  2318. * @param value2 defines the second control point
  2319. * @param tangent2 defines the second tangent vector
  2320. * @param amount defines the amount on the interpolation spline (between 0 and 1)
  2321. * @returns the new Vector3
  2322. */
  2323. public static Hermite(value1: DeepImmutable<Vector3>, tangent1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>, tangent2: DeepImmutable<Vector3>, amount: number): Vector3 {
  2324. var squared = amount * amount;
  2325. var cubed = amount * squared;
  2326. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  2327. var part2 = (-2.0 * cubed) + (3.0 * squared);
  2328. var part3 = (cubed - (2.0 * squared)) + amount;
  2329. var part4 = cubed - squared;
  2330. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  2331. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  2332. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  2333. return new Vector3(x, y, z);
  2334. }
  2335. /**
  2336. * Returns a new Vector3 located for "amount" (float) on the linear interpolation between the vectors "start" and "end"
  2337. * @param start defines the start value
  2338. * @param end defines the end value
  2339. * @param amount max defines amount between both (between 0 and 1)
  2340. * @returns the new Vector3
  2341. */
  2342. public static Lerp(start: DeepImmutable<Vector3>, end: DeepImmutable<Vector3>, amount: number): Vector3 {
  2343. var result = new Vector3(0, 0, 0);
  2344. Vector3.LerpToRef(start, end, amount, result);
  2345. return result;
  2346. }
  2347. /**
  2348. * Sets the given vector "result" with the result of the linear interpolation from the vector "start" for "amount" to the vector "end"
  2349. * @param start defines the start value
  2350. * @param end defines the end value
  2351. * @param amount max defines amount between both (between 0 and 1)
  2352. * @param result defines the Vector3 where to store the result
  2353. */
  2354. public static LerpToRef(start: DeepImmutable<Vector3>, end: DeepImmutable<Vector3>, amount: number, result: Vector3): void {
  2355. result.x = start.x + ((end.x - start.x) * amount);
  2356. result.y = start.y + ((end.y - start.y) * amount);
  2357. result.z = start.z + ((end.z - start.z) * amount);
  2358. }
  2359. /**
  2360. * Returns the dot product (float) between the vectors "left" and "right"
  2361. * @param left defines the left operand
  2362. * @param right defines the right operand
  2363. * @returns the dot product
  2364. */
  2365. public static Dot(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): number {
  2366. return (left.x * right.x + left.y * right.y + left.z * right.z);
  2367. }
  2368. /**
  2369. * Returns a new Vector3 as the cross product of the vectors "left" and "right"
  2370. * The cross product is then orthogonal to both "left" and "right"
  2371. * @param left defines the left operand
  2372. * @param right defines the right operand
  2373. * @returns the cross product
  2374. */
  2375. public static Cross(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  2376. var result = Vector3.Zero();
  2377. Vector3.CrossToRef(left, right, result);
  2378. return result;
  2379. }
  2380. /**
  2381. * Sets the given vector "result" with the cross product of "left" and "right"
  2382. * The cross product is then orthogonal to both "left" and "right"
  2383. * @param left defines the left operand
  2384. * @param right defines the right operand
  2385. * @param result defines the Vector3 where to store the result
  2386. */
  2387. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  2388. const x = left.y * right.z - left.z * right.y;
  2389. const y = left.z * right.x - left.x * right.z;
  2390. const z = left.x * right.y - left.y * right.x;
  2391. result.copyFromFloats(x, y, z);
  2392. }
  2393. /**
  2394. * Returns a new Vector3 as the normalization of the given vector
  2395. * @param vector defines the Vector3 to normalize
  2396. * @returns the new Vector3
  2397. */
  2398. public static Normalize(vector: DeepImmutable<Vector3>): Vector3 {
  2399. var result = Vector3.Zero();
  2400. Vector3.NormalizeToRef(vector, result);
  2401. return result;
  2402. }
  2403. /**
  2404. * Sets the given vector "result" with the normalization of the given first vector
  2405. * @param vector defines the Vector3 to normalize
  2406. * @param result defines the Vector3 where to store the result
  2407. */
  2408. public static NormalizeToRef(vector: DeepImmutable<Vector3>, result: Vector3): void {
  2409. vector.normalizeToRef(result);
  2410. }
  2411. /**
  2412. * Project a Vector3 onto screen space
  2413. * @param vector defines the Vector3 to project
  2414. * @param world defines the world matrix to use
  2415. * @param transform defines the transform (view x projection) matrix to use
  2416. * @param viewport defines the screen viewport to use
  2417. * @returns the new Vector3
  2418. */
  2419. public static Project(vector: DeepImmutable<Vector3>, world: DeepImmutable<Matrix>, transform: DeepImmutable<Matrix>, viewport: DeepImmutable<Viewport>): Vector3 {
  2420. var cw = viewport.width;
  2421. var ch = viewport.height;
  2422. var cx = viewport.x;
  2423. var cy = viewport.y;
  2424. var viewportMatrix = MathTmp.Matrix[1];
  2425. Matrix.FromValuesToRef(
  2426. cw / 2.0, 0, 0, 0,
  2427. 0, -ch / 2.0, 0, 0,
  2428. 0, 0, 0.5, 0,
  2429. cx + cw / 2.0, ch / 2.0 + cy, 0.5, 1, viewportMatrix);
  2430. var matrix = MathTmp.Matrix[0];
  2431. world.multiplyToRef(transform, matrix);
  2432. matrix.multiplyToRef(viewportMatrix, matrix);
  2433. return Vector3.TransformCoordinates(vector, matrix);
  2434. }
  2435. /** @hidden */
  2436. public static _UnprojectFromInvertedMatrixToRef(source: DeepImmutable<Vector3>, matrix: DeepImmutable<Matrix>, result: Vector3) {
  2437. Vector3.TransformCoordinatesToRef(source, matrix, result);
  2438. const m = matrix.m;
  2439. var num = source.x * m[3] + source.y * m[7] + source.z * m[11] + m[15];
  2440. if (Scalar.WithinEpsilon(num, 1.0)) {
  2441. result.scaleInPlace(1.0 / num);
  2442. }
  2443. }
  2444. /**
  2445. * Unproject from screen space to object space
  2446. * @param source defines the screen space Vector3 to use
  2447. * @param viewportWidth defines the current width of the viewport
  2448. * @param viewportHeight defines the current height of the viewport
  2449. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2450. * @param transform defines the transform (view x projection) matrix to use
  2451. * @returns the new Vector3
  2452. */
  2453. public static UnprojectFromTransform(source: Vector3, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, transform: DeepImmutable<Matrix>): Vector3 {
  2454. var matrix = MathTmp.Matrix[0];
  2455. world.multiplyToRef(transform, matrix);
  2456. matrix.invert();
  2457. source.x = source.x / viewportWidth * 2 - 1;
  2458. source.y = -(source.y / viewportHeight * 2 - 1);
  2459. const vector = new Vector3();
  2460. Vector3._UnprojectFromInvertedMatrixToRef(source, matrix, vector);
  2461. return vector;
  2462. }
  2463. /**
  2464. * Unproject from screen space to object space
  2465. * @param source defines the screen space Vector3 to use
  2466. * @param viewportWidth defines the current width of the viewport
  2467. * @param viewportHeight defines the current height of the viewport
  2468. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2469. * @param view defines the view matrix to use
  2470. * @param projection defines the projection matrix to use
  2471. * @returns the new Vector3
  2472. */
  2473. public static Unproject(source: DeepImmutable<Vector3>, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>): Vector3 {
  2474. let result = Vector3.Zero();
  2475. Vector3.UnprojectToRef(source, viewportWidth, viewportHeight, world, view, projection, result);
  2476. return result;
  2477. }
  2478. /**
  2479. * Unproject from screen space to object space
  2480. * @param source defines the screen space Vector3 to use
  2481. * @param viewportWidth defines the current width of the viewport
  2482. * @param viewportHeight defines the current height of the viewport
  2483. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2484. * @param view defines the view matrix to use
  2485. * @param projection defines the projection matrix to use
  2486. * @param result defines the Vector3 where to store the result
  2487. */
  2488. public static UnprojectToRef(source: DeepImmutable<Vector3>, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, result: Vector3): void {
  2489. Vector3.UnprojectFloatsToRef(source.x, source.y, source.z, viewportWidth, viewportHeight, world, view, projection, result);
  2490. }
  2491. /**
  2492. * Unproject from screen space to object space
  2493. * @param sourceX defines the screen space x coordinate to use
  2494. * @param sourceY defines the screen space y coordinate to use
  2495. * @param sourceZ defines the screen space z coordinate to use
  2496. * @param viewportWidth defines the current width of the viewport
  2497. * @param viewportHeight defines the current height of the viewport
  2498. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2499. * @param view defines the view matrix to use
  2500. * @param projection defines the projection matrix to use
  2501. * @param result defines the Vector3 where to store the result
  2502. */
  2503. 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 {
  2504. var matrix = MathTmp.Matrix[0];
  2505. world.multiplyToRef(view, matrix);
  2506. matrix.multiplyToRef(projection, matrix);
  2507. matrix.invert();
  2508. var screenSource = MathTmp.Vector3[0];
  2509. screenSource.x = sourceX / viewportWidth * 2 - 1;
  2510. screenSource.y = -(sourceY / viewportHeight * 2 - 1);
  2511. screenSource.z = 2 * sourceZ - 1.0;
  2512. Vector3._UnprojectFromInvertedMatrixToRef(screenSource, matrix, result);
  2513. }
  2514. /**
  2515. * Gets the minimal coordinate values between two Vector3
  2516. * @param left defines the first operand
  2517. * @param right defines the second operand
  2518. * @returns the new Vector3
  2519. */
  2520. public static Minimize(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  2521. var min = left.clone();
  2522. min.minimizeInPlace(right);
  2523. return min;
  2524. }
  2525. /**
  2526. * Gets the maximal coordinate values between two Vector3
  2527. * @param left defines the first operand
  2528. * @param right defines the second operand
  2529. * @returns the new Vector3
  2530. */
  2531. public static Maximize(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  2532. var max = left.clone();
  2533. max.maximizeInPlace(right);
  2534. return max;
  2535. }
  2536. /**
  2537. * Returns the distance between the vectors "value1" and "value2"
  2538. * @param value1 defines the first operand
  2539. * @param value2 defines the second operand
  2540. * @returns the distance
  2541. */
  2542. public static Distance(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): number {
  2543. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  2544. }
  2545. /**
  2546. * Returns the squared distance between the vectors "value1" and "value2"
  2547. * @param value1 defines the first operand
  2548. * @param value2 defines the second operand
  2549. * @returns the squared distance
  2550. */
  2551. public static DistanceSquared(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): number {
  2552. var x = value1.x - value2.x;
  2553. var y = value1.y - value2.y;
  2554. var z = value1.z - value2.z;
  2555. return (x * x) + (y * y) + (z * z);
  2556. }
  2557. /**
  2558. * Returns a new Vector3 located at the center between "value1" and "value2"
  2559. * @param value1 defines the first operand
  2560. * @param value2 defines the second operand
  2561. * @returns the new Vector3
  2562. */
  2563. public static Center(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): Vector3 {
  2564. var center = value1.add(value2);
  2565. center.scaleInPlace(0.5);
  2566. return center;
  2567. }
  2568. /**
  2569. * Given three orthogonal normalized left-handed oriented Vector3 axis in space (target system),
  2570. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  2571. * to something in order to rotate it from its local system to the given target system
  2572. * Note: axis1, axis2 and axis3 are normalized during this operation
  2573. * @param axis1 defines the first axis
  2574. * @param axis2 defines the second axis
  2575. * @param axis3 defines the third axis
  2576. * @returns a new Vector3
  2577. */
  2578. public static RotationFromAxis(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>): Vector3 {
  2579. var rotation = Vector3.Zero();
  2580. Vector3.RotationFromAxisToRef(axis1, axis2, axis3, rotation);
  2581. return rotation;
  2582. }
  2583. /**
  2584. * The same than RotationFromAxis but updates the given ref Vector3 parameter instead of returning a new Vector3
  2585. * @param axis1 defines the first axis
  2586. * @param axis2 defines the second axis
  2587. * @param axis3 defines the third axis
  2588. * @param ref defines the Vector3 where to store the result
  2589. */
  2590. public static RotationFromAxisToRef(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>, ref: Vector3): void {
  2591. var quat = MathTmp.Quaternion[0];
  2592. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  2593. quat.toEulerAnglesToRef(ref);
  2594. }
  2595. }
  2596. /**
  2597. * Vector4 class created for EulerAngle class conversion to Quaternion
  2598. */
  2599. export class Vector4 {
  2600. /**
  2601. * Creates a Vector4 object from the given floats.
  2602. * @param x x value of the vector
  2603. * @param y y value of the vector
  2604. * @param z z value of the vector
  2605. * @param w w value of the vector
  2606. */
  2607. constructor(
  2608. /** x value of the vector */
  2609. public x: number,
  2610. /** y value of the vector */
  2611. public y: number,
  2612. /** z value of the vector */
  2613. public z: number,
  2614. /** w value of the vector */
  2615. public w: number
  2616. ) { }
  2617. /**
  2618. * Returns the string with the Vector4 coordinates.
  2619. * @returns a string containing all the vector values
  2620. */
  2621. public toString(): string {
  2622. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  2623. }
  2624. /**
  2625. * Returns the string "Vector4".
  2626. * @returns "Vector4"
  2627. */
  2628. public getClassName(): string {
  2629. return "Vector4";
  2630. }
  2631. /**
  2632. * Returns the Vector4 hash code.
  2633. * @returns a unique hash code
  2634. */
  2635. public getHashCode(): number {
  2636. let hash = this.x | 0;
  2637. hash = (hash * 397) ^ (this.y | 0);
  2638. hash = (hash * 397) ^ (this.z | 0);
  2639. hash = (hash * 397) ^ (this.w | 0);
  2640. return hash;
  2641. }
  2642. // Operators
  2643. /**
  2644. * Returns a new array populated with 4 elements : the Vector4 coordinates.
  2645. * @returns the resulting array
  2646. */
  2647. public asArray(): number[] {
  2648. var result = new Array<number>();
  2649. this.toArray(result, 0);
  2650. return result;
  2651. }
  2652. /**
  2653. * Populates the given array from the given index with the Vector4 coordinates.
  2654. * @param array array to populate
  2655. * @param index index of the array to start at (default: 0)
  2656. * @returns the Vector4.
  2657. */
  2658. public toArray(array: FloatArray, index?: number): Vector4 {
  2659. if (index === undefined) {
  2660. index = 0;
  2661. }
  2662. array[index] = this.x;
  2663. array[index + 1] = this.y;
  2664. array[index + 2] = this.z;
  2665. array[index + 3] = this.w;
  2666. return this;
  2667. }
  2668. /**
  2669. * Adds the given vector to the current Vector4.
  2670. * @param otherVector the vector to add
  2671. * @returns the updated Vector4.
  2672. */
  2673. public addInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  2674. this.x += otherVector.x;
  2675. this.y += otherVector.y;
  2676. this.z += otherVector.z;
  2677. this.w += otherVector.w;
  2678. return this;
  2679. }
  2680. /**
  2681. * Returns a new Vector4 as the result of the addition of the current Vector4 and the given one.
  2682. * @param otherVector the vector to add
  2683. * @returns the resulting vector
  2684. */
  2685. public add(otherVector: DeepImmutable<Vector4>): Vector4 {
  2686. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  2687. }
  2688. /**
  2689. * Updates the given vector "result" with the result of the addition of the current Vector4 and the given one.
  2690. * @param otherVector the vector to add
  2691. * @param result the vector to store the result
  2692. * @returns the current Vector4.
  2693. */
  2694. public addToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2695. result.x = this.x + otherVector.x;
  2696. result.y = this.y + otherVector.y;
  2697. result.z = this.z + otherVector.z;
  2698. result.w = this.w + otherVector.w;
  2699. return this;
  2700. }
  2701. /**
  2702. * Subtract in place the given vector from the current Vector4.
  2703. * @param otherVector the vector to subtract
  2704. * @returns the updated Vector4.
  2705. */
  2706. public subtractInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  2707. this.x -= otherVector.x;
  2708. this.y -= otherVector.y;
  2709. this.z -= otherVector.z;
  2710. this.w -= otherVector.w;
  2711. return this;
  2712. }
  2713. /**
  2714. * Returns a new Vector4 with the result of the subtraction of the given vector from the current Vector4.
  2715. * @param otherVector the vector to add
  2716. * @returns the new vector with the result
  2717. */
  2718. public subtract(otherVector: DeepImmutable<Vector4>): Vector4 {
  2719. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  2720. }
  2721. /**
  2722. * Sets the given vector "result" with the result of the subtraction of the given vector from the current Vector4.
  2723. * @param otherVector the vector to subtract
  2724. * @param result the vector to store the result
  2725. * @returns the current Vector4.
  2726. */
  2727. public subtractToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2728. result.x = this.x - otherVector.x;
  2729. result.y = this.y - otherVector.y;
  2730. result.z = this.z - otherVector.z;
  2731. result.w = this.w - otherVector.w;
  2732. return this;
  2733. }
  2734. /**
  2735. * Returns a new Vector4 set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  2736. */
  2737. /**
  2738. * Returns a new Vector4 set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  2739. * @param x value to subtract
  2740. * @param y value to subtract
  2741. * @param z value to subtract
  2742. * @param w value to subtract
  2743. * @returns new vector containing the result
  2744. */
  2745. public subtractFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  2746. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  2747. }
  2748. /**
  2749. * Sets the given vector "result" set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  2750. * @param x value to subtract
  2751. * @param y value to subtract
  2752. * @param z value to subtract
  2753. * @param w value to subtract
  2754. * @param result the vector to store the result in
  2755. * @returns the current Vector4.
  2756. */
  2757. public subtractFromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): Vector4 {
  2758. result.x = this.x - x;
  2759. result.y = this.y - y;
  2760. result.z = this.z - z;
  2761. result.w = this.w - w;
  2762. return this;
  2763. }
  2764. /**
  2765. * Returns a new Vector4 set with the current Vector4 negated coordinates.
  2766. * @returns a new vector with the negated values
  2767. */
  2768. public negate(): Vector4 {
  2769. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  2770. }
  2771. /**
  2772. * Multiplies the current Vector4 coordinates by scale (float).
  2773. * @param scale the number to scale with
  2774. * @returns the updated Vector4.
  2775. */
  2776. public scaleInPlace(scale: number): Vector4 {
  2777. this.x *= scale;
  2778. this.y *= scale;
  2779. this.z *= scale;
  2780. this.w *= scale;
  2781. return this;
  2782. }
  2783. /**
  2784. * Returns a new Vector4 set with the current Vector4 coordinates multiplied by scale (float).
  2785. * @param scale the number to scale with
  2786. * @returns a new vector with the result
  2787. */
  2788. public scale(scale: number): Vector4 {
  2789. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  2790. }
  2791. /**
  2792. * Sets the given vector "result" with the current Vector4 coordinates multiplied by scale (float).
  2793. * @param scale the number to scale with
  2794. * @param result a vector to store the result in
  2795. * @returns the current Vector4.
  2796. */
  2797. public scaleToRef(scale: number, result: Vector4): Vector4 {
  2798. result.x = this.x * scale;
  2799. result.y = this.y * scale;
  2800. result.z = this.z * scale;
  2801. result.w = this.w * scale;
  2802. return this;
  2803. }
  2804. /**
  2805. * Scale the current Vector4 values by a factor and add the result to a given Vector4
  2806. * @param scale defines the scale factor
  2807. * @param result defines the Vector4 object where to store the result
  2808. * @returns the unmodified current Vector4
  2809. */
  2810. public scaleAndAddToRef(scale: number, result: Vector4): Vector4 {
  2811. result.x += this.x * scale;
  2812. result.y += this.y * scale;
  2813. result.z += this.z * scale;
  2814. result.w += this.w * scale;
  2815. return this;
  2816. }
  2817. /**
  2818. * Boolean : True if the current Vector4 coordinates are stricly equal to the given ones.
  2819. * @param otherVector the vector to compare against
  2820. * @returns true if they are equal
  2821. */
  2822. public equals(otherVector: DeepImmutable<Vector4>): boolean {
  2823. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  2824. }
  2825. /**
  2826. * Boolean : True if the current Vector4 coordinates are each beneath the distance "epsilon" from the given vector ones.
  2827. * @param otherVector vector to compare against
  2828. * @param epsilon (Default: very small number)
  2829. * @returns true if they are equal
  2830. */
  2831. public equalsWithEpsilon(otherVector: DeepImmutable<Vector4>, epsilon: number = Epsilon): boolean {
  2832. return otherVector
  2833. && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon)
  2834. && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon)
  2835. && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon)
  2836. && Scalar.WithinEpsilon(this.w, otherVector.w, epsilon);
  2837. }
  2838. /**
  2839. * Boolean : True if the given floats are strictly equal to the current Vector4 coordinates.
  2840. * @param x x value to compare against
  2841. * @param y y value to compare against
  2842. * @param z z value to compare against
  2843. * @param w w value to compare against
  2844. * @returns true if equal
  2845. */
  2846. public equalsToFloats(x: number, y: number, z: number, w: number): boolean {
  2847. return this.x === x && this.y === y && this.z === z && this.w === w;
  2848. }
  2849. /**
  2850. * Multiplies in place the current Vector4 by the given one.
  2851. * @param otherVector vector to multiple with
  2852. * @returns the updated Vector4.
  2853. */
  2854. public multiplyInPlace(otherVector: Vector4): Vector4 {
  2855. this.x *= otherVector.x;
  2856. this.y *= otherVector.y;
  2857. this.z *= otherVector.z;
  2858. this.w *= otherVector.w;
  2859. return this;
  2860. }
  2861. /**
  2862. * Returns a new Vector4 set with the multiplication result of the current Vector4 and the given one.
  2863. * @param otherVector vector to multiple with
  2864. * @returns resulting new vector
  2865. */
  2866. public multiply(otherVector: DeepImmutable<Vector4>): Vector4 {
  2867. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  2868. }
  2869. /**
  2870. * Updates the given vector "result" with the multiplication result of the current Vector4 and the given one.
  2871. * @param otherVector vector to multiple with
  2872. * @param result vector to store the result
  2873. * @returns the current Vector4.
  2874. */
  2875. public multiplyToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2876. result.x = this.x * otherVector.x;
  2877. result.y = this.y * otherVector.y;
  2878. result.z = this.z * otherVector.z;
  2879. result.w = this.w * otherVector.w;
  2880. return this;
  2881. }
  2882. /**
  2883. * Returns a new Vector4 set with the multiplication result of the given floats and the current Vector4 coordinates.
  2884. * @param x x value multiply with
  2885. * @param y y value multiply with
  2886. * @param z z value multiply with
  2887. * @param w w value multiply with
  2888. * @returns resulting new vector
  2889. */
  2890. public multiplyByFloats(x: number, y: number, z: number, w: number): Vector4 {
  2891. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  2892. }
  2893. /**
  2894. * Returns a new Vector4 set with the division result of the current Vector4 by the given one.
  2895. * @param otherVector vector to devide with
  2896. * @returns resulting new vector
  2897. */
  2898. public divide(otherVector: DeepImmutable<Vector4>): Vector4 {
  2899. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  2900. }
  2901. /**
  2902. * Updates the given vector "result" with the division result of the current Vector4 by the given one.
  2903. * @param otherVector vector to devide with
  2904. * @param result vector to store the result
  2905. * @returns the current Vector4.
  2906. */
  2907. public divideToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2908. result.x = this.x / otherVector.x;
  2909. result.y = this.y / otherVector.y;
  2910. result.z = this.z / otherVector.z;
  2911. result.w = this.w / otherVector.w;
  2912. return this;
  2913. }
  2914. /**
  2915. * Divides the current Vector3 coordinates by the given ones.
  2916. * @param otherVector vector to devide with
  2917. * @returns the updated Vector3.
  2918. */
  2919. public divideInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  2920. return this.divideToRef(otherVector, this);
  2921. }
  2922. /**
  2923. * Updates the Vector4 coordinates with the minimum values between its own and the given vector ones
  2924. * @param other defines the second operand
  2925. * @returns the current updated Vector4
  2926. */
  2927. public minimizeInPlace(other: DeepImmutable<Vector4>): Vector4 {
  2928. if (other.x < this.x) { this.x = other.x; }
  2929. if (other.y < this.y) { this.y = other.y; }
  2930. if (other.z < this.z) { this.z = other.z; }
  2931. if (other.w < this.w) { this.w = other.w; }
  2932. return this;
  2933. }
  2934. /**
  2935. * Updates the Vector4 coordinates with the maximum values between its own and the given vector ones
  2936. * @param other defines the second operand
  2937. * @returns the current updated Vector4
  2938. */
  2939. public maximizeInPlace(other: DeepImmutable<Vector4>): Vector4 {
  2940. if (other.x > this.x) { this.x = other.x; }
  2941. if (other.y > this.y) { this.y = other.y; }
  2942. if (other.z > this.z) { this.z = other.z; }
  2943. if (other.w > this.w) { this.w = other.w; }
  2944. return this;
  2945. }
  2946. /**
  2947. * Gets a new Vector4 from current Vector4 floored values
  2948. * @returns a new Vector4
  2949. */
  2950. public floor(): Vector4 {
  2951. return new Vector4(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z), Math.floor(this.w));
  2952. }
  2953. /**
  2954. * Gets a new Vector4 from current Vector3 floored values
  2955. * @returns a new Vector4
  2956. */
  2957. public fract(): Vector4 {
  2958. 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));
  2959. }
  2960. // Properties
  2961. /**
  2962. * Returns the Vector4 length (float).
  2963. * @returns the length
  2964. */
  2965. public length(): number {
  2966. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  2967. }
  2968. /**
  2969. * Returns the Vector4 squared length (float).
  2970. * @returns the length squared
  2971. */
  2972. public lengthSquared(): number {
  2973. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  2974. }
  2975. // Methods
  2976. /**
  2977. * Normalizes in place the Vector4.
  2978. * @returns the updated Vector4.
  2979. */
  2980. public normalize(): Vector4 {
  2981. var len = this.length();
  2982. if (len === 0) {
  2983. return this;
  2984. }
  2985. return this.scaleInPlace(1.0 / len);
  2986. }
  2987. /**
  2988. * Returns a new Vector3 from the Vector4 (x, y, z) coordinates.
  2989. * @returns this converted to a new vector3
  2990. */
  2991. public toVector3(): Vector3 {
  2992. return new Vector3(this.x, this.y, this.z);
  2993. }
  2994. /**
  2995. * Returns a new Vector4 copied from the current one.
  2996. * @returns the new cloned vector
  2997. */
  2998. public clone(): Vector4 {
  2999. return new Vector4(this.x, this.y, this.z, this.w);
  3000. }
  3001. /**
  3002. * Updates the current Vector4 with the given one coordinates.
  3003. * @param source the source vector to copy from
  3004. * @returns the updated Vector4.
  3005. */
  3006. public copyFrom(source: DeepImmutable<Vector4>): Vector4 {
  3007. this.x = source.x;
  3008. this.y = source.y;
  3009. this.z = source.z;
  3010. this.w = source.w;
  3011. return this;
  3012. }
  3013. /**
  3014. * Updates the current Vector4 coordinates with the given floats.
  3015. * @param x float to copy from
  3016. * @param y float to copy from
  3017. * @param z float to copy from
  3018. * @param w float to copy from
  3019. * @returns the updated Vector4.
  3020. */
  3021. public copyFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  3022. this.x = x;
  3023. this.y = y;
  3024. this.z = z;
  3025. this.w = w;
  3026. return this;
  3027. }
  3028. /**
  3029. * Updates the current Vector4 coordinates with the given floats.
  3030. * @param x float to set from
  3031. * @param y float to set from
  3032. * @param z float to set from
  3033. * @param w float to set from
  3034. * @returns the updated Vector4.
  3035. */
  3036. public set(x: number, y: number, z: number, w: number): Vector4 {
  3037. return this.copyFromFloats(x, y, z, w);
  3038. }
  3039. /**
  3040. * Copies the given float to the current Vector3 coordinates
  3041. * @param v defines the x, y, z and w coordinates of the operand
  3042. * @returns the current updated Vector3
  3043. */
  3044. public setAll(v: number): Vector4 {
  3045. this.x = this.y = this.z = this.w = v;
  3046. return this;
  3047. }
  3048. // Statics
  3049. /**
  3050. * Returns a new Vector4 set from the starting index of the given array.
  3051. * @param array the array to pull values from
  3052. * @param offset the offset into the array to start at
  3053. * @returns the new vector
  3054. */
  3055. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset?: number): Vector4 {
  3056. if (!offset) {
  3057. offset = 0;
  3058. }
  3059. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  3060. }
  3061. /**
  3062. * Updates the given vector "result" from the starting index of the given array.
  3063. * @param array the array to pull values from
  3064. * @param offset the offset into the array to start at
  3065. * @param result the vector to store the result in
  3066. */
  3067. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector4): void {
  3068. result.x = array[offset];
  3069. result.y = array[offset + 1];
  3070. result.z = array[offset + 2];
  3071. result.w = array[offset + 3];
  3072. }
  3073. /**
  3074. * Updates the given vector "result" from the starting index of the given Float32Array.
  3075. * @param array the array to pull values from
  3076. * @param offset the offset into the array to start at
  3077. * @param result the vector to store the result in
  3078. */
  3079. public static FromFloatArrayToRef(array: DeepImmutable<Float32Array>, offset: number, result: Vector4): void {
  3080. Vector4.FromArrayToRef(array, offset, result);
  3081. }
  3082. /**
  3083. * Updates the given vector "result" coordinates from the given floats.
  3084. * @param x float to set from
  3085. * @param y float to set from
  3086. * @param z float to set from
  3087. * @param w float to set from
  3088. * @param result the vector to the floats in
  3089. */
  3090. public static FromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): void {
  3091. result.x = x;
  3092. result.y = y;
  3093. result.z = z;
  3094. result.w = w;
  3095. }
  3096. /**
  3097. * Returns a new Vector4 set to (0.0, 0.0, 0.0, 0.0)
  3098. * @returns the new vector
  3099. */
  3100. public static Zero(): Vector4 {
  3101. return new Vector4(0.0, 0.0, 0.0, 0.0);
  3102. }
  3103. /**
  3104. * Returns a new Vector4 set to (1.0, 1.0, 1.0, 1.0)
  3105. * @returns the new vector
  3106. */
  3107. public static One(): Vector4 {
  3108. return new Vector4(1.0, 1.0, 1.0, 1.0);
  3109. }
  3110. /**
  3111. * Returns a new normalized Vector4 from the given one.
  3112. * @param vector the vector to normalize
  3113. * @returns the vector
  3114. */
  3115. public static Normalize(vector: DeepImmutable<Vector4>): Vector4 {
  3116. var result = Vector4.Zero();
  3117. Vector4.NormalizeToRef(vector, result);
  3118. return result;
  3119. }
  3120. /**
  3121. * Updates the given vector "result" from the normalization of the given one.
  3122. * @param vector the vector to normalize
  3123. * @param result the vector to store the result in
  3124. */
  3125. public static NormalizeToRef(vector: DeepImmutable<Vector4>, result: Vector4): void {
  3126. result.copyFrom(vector);
  3127. result.normalize();
  3128. }
  3129. /**
  3130. * Returns a vector with the minimum values from the left and right vectors
  3131. * @param left left vector to minimize
  3132. * @param right right vector to minimize
  3133. * @returns a new vector with the minimum of the left and right vector values
  3134. */
  3135. public static Minimize(left: DeepImmutable<Vector4>, right: DeepImmutable<Vector4>): Vector4 {
  3136. var min = left.clone();
  3137. min.minimizeInPlace(right);
  3138. return min;
  3139. }
  3140. /**
  3141. * Returns a vector with the maximum values from the left and right vectors
  3142. * @param left left vector to maximize
  3143. * @param right right vector to maximize
  3144. * @returns a new vector with the maximum of the left and right vector values
  3145. */
  3146. public static Maximize(left: DeepImmutable<Vector4>, right: DeepImmutable<Vector4>): Vector4 {
  3147. var max = left.clone();
  3148. max.maximizeInPlace(right);
  3149. return max;
  3150. }
  3151. /**
  3152. * Returns the distance (float) between the vectors "value1" and "value2".
  3153. * @param value1 value to calulate the distance between
  3154. * @param value2 value to calulate the distance between
  3155. * @return the distance between the two vectors
  3156. */
  3157. public static Distance(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): number {
  3158. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  3159. }
  3160. /**
  3161. * Returns the squared distance (float) between the vectors "value1" and "value2".
  3162. * @param value1 value to calulate the distance between
  3163. * @param value2 value to calulate the distance between
  3164. * @return the distance between the two vectors squared
  3165. */
  3166. public static DistanceSquared(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): number {
  3167. var x = value1.x - value2.x;
  3168. var y = value1.y - value2.y;
  3169. var z = value1.z - value2.z;
  3170. var w = value1.w - value2.w;
  3171. return (x * x) + (y * y) + (z * z) + (w * w);
  3172. }
  3173. /**
  3174. * Returns a new Vector4 located at the center between the vectors "value1" and "value2".
  3175. * @param value1 value to calulate the center between
  3176. * @param value2 value to calulate the center between
  3177. * @return the center between the two vectors
  3178. */
  3179. public static Center(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): Vector4 {
  3180. var center = value1.add(value2);
  3181. center.scaleInPlace(0.5);
  3182. return center;
  3183. }
  3184. /**
  3185. * Returns a new Vector4 set with the result of the normal transformation by the given matrix of the given vector.
  3186. * This methods computes transformed normalized direction vectors only.
  3187. * @param vector the vector to transform
  3188. * @param transformation the transformation matrix to apply
  3189. * @returns the new vector
  3190. */
  3191. public static TransformNormal(vector: DeepImmutable<Vector4>, transformation: DeepImmutable<Matrix>): Vector4 {
  3192. var result = Vector4.Zero();
  3193. Vector4.TransformNormalToRef(vector, transformation, result);
  3194. return result;
  3195. }
  3196. /**
  3197. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given vector.
  3198. * This methods computes transformed normalized direction vectors only.
  3199. * @param vector the vector to transform
  3200. * @param transformation the transformation matrix to apply
  3201. * @param result the vector to store the result in
  3202. */
  3203. public static TransformNormalToRef(vector: DeepImmutable<Vector4>, transformation: DeepImmutable<Matrix>, result: Vector4): void {
  3204. const m = transformation.m;
  3205. var x = (vector.x * m[0]) + (vector.y * m[4]) + (vector.z * m[8]);
  3206. var y = (vector.x * m[1]) + (vector.y * m[5]) + (vector.z * m[9]);
  3207. var z = (vector.x * m[2]) + (vector.y * m[6]) + (vector.z * m[10]);
  3208. result.x = x;
  3209. result.y = y;
  3210. result.z = z;
  3211. result.w = vector.w;
  3212. }
  3213. /**
  3214. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given floats (x, y, z, w).
  3215. * This methods computes transformed normalized direction vectors only.
  3216. * @param x value to transform
  3217. * @param y value to transform
  3218. * @param z value to transform
  3219. * @param w value to transform
  3220. * @param transformation the transformation matrix to apply
  3221. * @param result the vector to store the results in
  3222. */
  3223. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, w: number, transformation: DeepImmutable<Matrix>, result: Vector4): void {
  3224. const m = transformation.m;
  3225. result.x = (x * m[0]) + (y * m[4]) + (z * m[8]);
  3226. result.y = (x * m[1]) + (y * m[5]) + (z * m[9]);
  3227. result.z = (x * m[2]) + (y * m[6]) + (z * m[10]);
  3228. result.w = w;
  3229. }
  3230. /**
  3231. * Creates a new Vector4 from a Vector3
  3232. * @param source defines the source data
  3233. * @param w defines the 4th component (default is 0)
  3234. * @returns a new Vector4
  3235. */
  3236. public static FromVector3(source: Vector3, w: number = 0) {
  3237. return new Vector4(source.x, source.y, source.z, w);
  3238. }
  3239. }
  3240. /**
  3241. * Interface for the size containing width and height
  3242. */
  3243. export interface ISize {
  3244. /**
  3245. * Width
  3246. */
  3247. width: number;
  3248. /**
  3249. * Heighht
  3250. */
  3251. height: number;
  3252. }
  3253. /**
  3254. * Size containing widht and height
  3255. */
  3256. export class Size implements ISize {
  3257. /**
  3258. * Width
  3259. */
  3260. public width: number;
  3261. /**
  3262. * Height
  3263. */
  3264. public height: number;
  3265. /**
  3266. * Creates a Size object from the given width and height (floats).
  3267. * @param width width of the new size
  3268. * @param height height of the new size
  3269. */
  3270. public constructor(width: number, height: number) {
  3271. this.width = width;
  3272. this.height = height;
  3273. }
  3274. /**
  3275. * Returns a string with the Size width and height
  3276. * @returns a string with the Size width and height
  3277. */
  3278. public toString(): string {
  3279. return `{W: ${this.width}, H: ${this.height}}`;
  3280. }
  3281. /**
  3282. * "Size"
  3283. * @returns the string "Size"
  3284. */
  3285. public getClassName(): string {
  3286. return "Size";
  3287. }
  3288. /**
  3289. * Returns the Size hash code.
  3290. * @returns a hash code for a unique width and height
  3291. */
  3292. public getHashCode(): number {
  3293. let hash = this.width | 0;
  3294. hash = (hash * 397) ^ (this.height | 0);
  3295. return hash;
  3296. }
  3297. /**
  3298. * Updates the current size from the given one.
  3299. * @param src the given size
  3300. */
  3301. public copyFrom(src: Size) {
  3302. this.width = src.width;
  3303. this.height = src.height;
  3304. }
  3305. /**
  3306. * Updates in place the current Size from the given floats.
  3307. * @param width width of the new size
  3308. * @param height height of the new size
  3309. * @returns the updated Size.
  3310. */
  3311. public copyFromFloats(width: number, height: number): Size {
  3312. this.width = width;
  3313. this.height = height;
  3314. return this;
  3315. }
  3316. /**
  3317. * Updates in place the current Size from the given floats.
  3318. * @param width width to set
  3319. * @param height height to set
  3320. * @returns the updated Size.
  3321. */
  3322. public set(width: number, height: number): Size {
  3323. return this.copyFromFloats(width, height);
  3324. }
  3325. /**
  3326. * Multiplies the width and height by numbers
  3327. * @param w factor to multiple the width by
  3328. * @param h factor to multiple the height by
  3329. * @returns a new Size set with the multiplication result of the current Size and the given floats.
  3330. */
  3331. public multiplyByFloats(w: number, h: number): Size {
  3332. return new Size(this.width * w, this.height * h);
  3333. }
  3334. /**
  3335. * Clones the size
  3336. * @returns a new Size copied from the given one.
  3337. */
  3338. public clone(): Size {
  3339. return new Size(this.width, this.height);
  3340. }
  3341. /**
  3342. * True if the current Size and the given one width and height are strictly equal.
  3343. * @param other the other size to compare against
  3344. * @returns True if the current Size and the given one width and height are strictly equal.
  3345. */
  3346. public equals(other: Size): boolean {
  3347. if (!other) {
  3348. return false;
  3349. }
  3350. return (this.width === other.width) && (this.height === other.height);
  3351. }
  3352. /**
  3353. * The surface of the Size : width * height (float).
  3354. */
  3355. public get surface(): number {
  3356. return this.width * this.height;
  3357. }
  3358. /**
  3359. * Create a new size of zero
  3360. * @returns a new Size set to (0.0, 0.0)
  3361. */
  3362. public static Zero(): Size {
  3363. return new Size(0.0, 0.0);
  3364. }
  3365. /**
  3366. * Sums the width and height of two sizes
  3367. * @param otherSize size to add to this size
  3368. * @returns a new Size set as the addition result of the current Size and the given one.
  3369. */
  3370. public add(otherSize: Size): Size {
  3371. let r = new Size(this.width + otherSize.width, this.height + otherSize.height);
  3372. return r;
  3373. }
  3374. /**
  3375. * Subtracts the width and height of two
  3376. * @param otherSize size to subtract to this size
  3377. * @returns a new Size set as the subtraction result of the given one from the current Size.
  3378. */
  3379. public subtract(otherSize: Size): Size {
  3380. let r = new Size(this.width - otherSize.width, this.height - otherSize.height);
  3381. return r;
  3382. }
  3383. /**
  3384. * Creates a new Size set at the linear interpolation "amount" between "start" and "end"
  3385. * @param start starting size to lerp between
  3386. * @param end end size to lerp between
  3387. * @param amount amount to lerp between the start and end values
  3388. * @returns a new Size set at the linear interpolation "amount" between "start" and "end"
  3389. */
  3390. public static Lerp(start: Size, end: Size, amount: number): Size {
  3391. var w = start.width + ((end.width - start.width) * amount);
  3392. var h = start.height + ((end.height - start.height) * amount);
  3393. return new Size(w, h);
  3394. }
  3395. }
  3396. /**
  3397. * Class used to store quaternion data
  3398. * @see https://en.wikipedia.org/wiki/Quaternion
  3399. * @see http://doc.babylonjs.com/features/position,_rotation,_scaling
  3400. */
  3401. export class Quaternion {
  3402. /**
  3403. * Creates a new Quaternion from the given floats
  3404. * @param x defines the first component (0 by default)
  3405. * @param y defines the second component (0 by default)
  3406. * @param z defines the third component (0 by default)
  3407. * @param w defines the fourth component (1.0 by default)
  3408. */
  3409. constructor(
  3410. /** defines the first component (0 by default) */
  3411. public x: number = 0.0,
  3412. /** defines the second component (0 by default) */
  3413. public y: number = 0.0,
  3414. /** defines the third component (0 by default) */
  3415. public z: number = 0.0,
  3416. /** defines the fourth component (1.0 by default) */
  3417. public w: number = 1.0) {
  3418. }
  3419. /**
  3420. * Gets a string representation for the current quaternion
  3421. * @returns a string with the Quaternion coordinates
  3422. */
  3423. public toString(): string {
  3424. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  3425. }
  3426. /**
  3427. * Gets the class name of the quaternion
  3428. * @returns the string "Quaternion"
  3429. */
  3430. public getClassName(): string {
  3431. return "Quaternion";
  3432. }
  3433. /**
  3434. * Gets a hash code for this quaternion
  3435. * @returns the quaternion hash code
  3436. */
  3437. public getHashCode(): number {
  3438. let hash = this.x | 0;
  3439. hash = (hash * 397) ^ (this.y | 0);
  3440. hash = (hash * 397) ^ (this.z | 0);
  3441. hash = (hash * 397) ^ (this.w | 0);
  3442. return hash;
  3443. }
  3444. /**
  3445. * Copy the quaternion to an array
  3446. * @returns a new array populated with 4 elements from the quaternion coordinates
  3447. */
  3448. public asArray(): number[] {
  3449. return [this.x, this.y, this.z, this.w];
  3450. }
  3451. /**
  3452. * Check if two quaternions are equals
  3453. * @param otherQuaternion defines the second operand
  3454. * @return true if the current quaternion and the given one coordinates are strictly equals
  3455. */
  3456. public equals(otherQuaternion: DeepImmutable<Quaternion>): boolean {
  3457. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  3458. }
  3459. /**
  3460. * Clone the current quaternion
  3461. * @returns a new quaternion copied from the current one
  3462. */
  3463. public clone(): Quaternion {
  3464. return new Quaternion(this.x, this.y, this.z, this.w);
  3465. }
  3466. /**
  3467. * Copy a quaternion to the current one
  3468. * @param other defines the other quaternion
  3469. * @returns the updated current quaternion
  3470. */
  3471. public copyFrom(other: DeepImmutable<Quaternion>): Quaternion {
  3472. this.x = other.x;
  3473. this.y = other.y;
  3474. this.z = other.z;
  3475. this.w = other.w;
  3476. return this;
  3477. }
  3478. /**
  3479. * Updates the current quaternion with the given float coordinates
  3480. * @param x defines the x coordinate
  3481. * @param y defines the y coordinate
  3482. * @param z defines the z coordinate
  3483. * @param w defines the w coordinate
  3484. * @returns the updated current quaternion
  3485. */
  3486. public copyFromFloats(x: number, y: number, z: number, w: number): Quaternion {
  3487. this.x = x;
  3488. this.y = y;
  3489. this.z = z;
  3490. this.w = w;
  3491. return this;
  3492. }
  3493. /**
  3494. * Updates the current quaternion from the given float coordinates
  3495. * @param x defines the x coordinate
  3496. * @param y defines the y coordinate
  3497. * @param z defines the z coordinate
  3498. * @param w defines the w coordinate
  3499. * @returns the updated current quaternion
  3500. */
  3501. public set(x: number, y: number, z: number, w: number): Quaternion {
  3502. return this.copyFromFloats(x, y, z, w);
  3503. }
  3504. /**
  3505. * Adds two quaternions
  3506. * @param other defines the second operand
  3507. * @returns a new quaternion as the addition result of the given one and the current quaternion
  3508. */
  3509. public add(other: DeepImmutable<Quaternion>): Quaternion {
  3510. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  3511. }
  3512. /**
  3513. * Add a quaternion to the current one
  3514. * @param other defines the quaternion to add
  3515. * @returns the current quaternion
  3516. */
  3517. public addInPlace(other: DeepImmutable<Quaternion>): Quaternion {
  3518. this.x += other.x;
  3519. this.y += other.y;
  3520. this.z += other.z;
  3521. this.w += other.w;
  3522. return this;
  3523. }
  3524. /**
  3525. * Subtract two quaternions
  3526. * @param other defines the second operand
  3527. * @returns a new quaternion as the subtraction result of the given one from the current one
  3528. */
  3529. public subtract(other: Quaternion): Quaternion {
  3530. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  3531. }
  3532. /**
  3533. * Multiplies the current quaternion by a scale factor
  3534. * @param value defines the scale factor
  3535. * @returns a new quaternion set by multiplying the current quaternion coordinates by the float "scale"
  3536. */
  3537. public scale(value: number): Quaternion {
  3538. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  3539. }
  3540. /**
  3541. * Scale the current quaternion values by a factor and stores the result to a given quaternion
  3542. * @param scale defines the scale factor
  3543. * @param result defines the Quaternion object where to store the result
  3544. * @returns the unmodified current quaternion
  3545. */
  3546. public scaleToRef(scale: number, result: Quaternion): Quaternion {
  3547. result.x = this.x * scale;
  3548. result.y = this.y * scale;
  3549. result.z = this.z * scale;
  3550. result.w = this.w * scale;
  3551. return this;
  3552. }
  3553. /**
  3554. * Multiplies in place the current quaternion by a scale factor
  3555. * @param value defines the scale factor
  3556. * @returns the current modified quaternion
  3557. */
  3558. public scaleInPlace(value: number): Quaternion {
  3559. this.x *= value;
  3560. this.y *= value;
  3561. this.z *= value;
  3562. this.w *= value;
  3563. return this;
  3564. }
  3565. /**
  3566. * Scale the current quaternion values by a factor and add the result to a given quaternion
  3567. * @param scale defines the scale factor
  3568. * @param result defines the Quaternion object where to store the result
  3569. * @returns the unmodified current quaternion
  3570. */
  3571. public scaleAndAddToRef(scale: number, result: Quaternion): Quaternion {
  3572. result.x += this.x * scale;
  3573. result.y += this.y * scale;
  3574. result.z += this.z * scale;
  3575. result.w += this.w * scale;
  3576. return this;
  3577. }
  3578. /**
  3579. * Multiplies two quaternions
  3580. * @param q1 defines the second operand
  3581. * @returns a new quaternion set as the multiplication result of the current one with the given one "q1"
  3582. */
  3583. public multiply(q1: DeepImmutable<Quaternion>): Quaternion {
  3584. var result = new Quaternion(0, 0, 0, 1.0);
  3585. this.multiplyToRef(q1, result);
  3586. return result;
  3587. }
  3588. /**
  3589. * Sets the given "result" as the the multiplication result of the current one with the given one "q1"
  3590. * @param q1 defines the second operand
  3591. * @param result defines the target quaternion
  3592. * @returns the current quaternion
  3593. */
  3594. public multiplyToRef(q1: DeepImmutable<Quaternion>, result: Quaternion): Quaternion {
  3595. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  3596. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  3597. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  3598. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  3599. result.copyFromFloats(x, y, z, w);
  3600. return this;
  3601. }
  3602. /**
  3603. * Updates the current quaternion with the multiplication of itself with the given one "q1"
  3604. * @param q1 defines the second operand
  3605. * @returns the currentupdated quaternion
  3606. */
  3607. public multiplyInPlace(q1: DeepImmutable<Quaternion>): Quaternion {
  3608. this.multiplyToRef(q1, this);
  3609. return this;
  3610. }
  3611. /**
  3612. * Conjugates (1-q) the current quaternion and stores the result in the given quaternion
  3613. * @param ref defines the target quaternion
  3614. * @returns the current quaternion
  3615. */
  3616. public conjugateToRef(ref: Quaternion): Quaternion {
  3617. ref.copyFromFloats(-this.x, -this.y, -this.z, this.w);
  3618. return this;
  3619. }
  3620. /**
  3621. * Conjugates in place (1-q) the current quaternion
  3622. * @returns the current updated quaternion
  3623. */
  3624. public conjugateInPlace(): Quaternion {
  3625. this.x *= -1;
  3626. this.y *= -1;
  3627. this.z *= -1;
  3628. return this;
  3629. }
  3630. /**
  3631. * Conjugates in place (1-q) the current quaternion
  3632. * @returns a new quaternion
  3633. */
  3634. public conjugate(): Quaternion {
  3635. var result = new Quaternion(-this.x, -this.y, -this.z, this.w);
  3636. return result;
  3637. }
  3638. /**
  3639. * Gets length of current quaternion
  3640. * @returns the quaternion length (float)
  3641. */
  3642. public length(): number {
  3643. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  3644. }
  3645. /**
  3646. * Normalize in place the current quaternion
  3647. * @returns the current updated quaternion
  3648. */
  3649. public normalize(): Quaternion {
  3650. var len = this.length();
  3651. if (len === 0) {
  3652. return this;
  3653. }
  3654. var inv = 1.0 / len;
  3655. this.x *= inv;
  3656. this.y *= inv;
  3657. this.z *= inv;
  3658. this.w *= inv;
  3659. return this;
  3660. }
  3661. /**
  3662. * Returns a new Vector3 set with the Euler angles translated from the current quaternion
  3663. * @param order is a reserved parameter and is ignore for now
  3664. * @returns a new Vector3 containing the Euler angles
  3665. */
  3666. public toEulerAngles(order = "YZX"): Vector3 {
  3667. var result = Vector3.Zero();
  3668. this.toEulerAnglesToRef(result);
  3669. return result;
  3670. }
  3671. /**
  3672. * Sets the given vector3 "result" with the Euler angles translated from the current quaternion
  3673. * @param result defines the vector which will be filled with the Euler angles
  3674. * @param order is a reserved parameter and is ignore for now
  3675. * @returns the current unchanged quaternion
  3676. */
  3677. public toEulerAnglesToRef(result: Vector3): Quaternion {
  3678. var qz = this.z;
  3679. var qx = this.x;
  3680. var qy = this.y;
  3681. var qw = this.w;
  3682. var sqw = qw * qw;
  3683. var sqz = qz * qz;
  3684. var sqx = qx * qx;
  3685. var sqy = qy * qy;
  3686. var zAxisY = qy * qz - qx * qw;
  3687. var limit = .4999999;
  3688. if (zAxisY < -limit) {
  3689. result.y = 2 * Math.atan2(qy, qw);
  3690. result.x = Math.PI / 2;
  3691. result.z = 0;
  3692. } else if (zAxisY > limit) {
  3693. result.y = 2 * Math.atan2(qy, qw);
  3694. result.x = -Math.PI / 2;
  3695. result.z = 0;
  3696. } else {
  3697. result.z = Math.atan2(2.0 * (qx * qy + qz * qw), (-sqz - sqx + sqy + sqw));
  3698. result.x = Math.asin(-2.0 * (qz * qy - qx * qw));
  3699. result.y = Math.atan2(2.0 * (qz * qx + qy * qw), (sqz - sqx - sqy + sqw));
  3700. }
  3701. return this;
  3702. }
  3703. /**
  3704. * Updates the given rotation matrix with the current quaternion values
  3705. * @param result defines the target matrix
  3706. * @returns the current unchanged quaternion
  3707. */
  3708. public toRotationMatrix(result: Matrix): Quaternion {
  3709. Matrix.FromQuaternionToRef(this, result);
  3710. return this;
  3711. }
  3712. /**
  3713. * Updates the current quaternion from the given rotation matrix values
  3714. * @param matrix defines the source matrix
  3715. * @returns the current updated quaternion
  3716. */
  3717. public fromRotationMatrix(matrix: DeepImmutable<Matrix>): Quaternion {
  3718. Quaternion.FromRotationMatrixToRef(matrix, this);
  3719. return this;
  3720. }
  3721. // Statics
  3722. /**
  3723. * Creates a new quaternion from a rotation matrix
  3724. * @param matrix defines the source matrix
  3725. * @returns a new quaternion created from the given rotation matrix values
  3726. */
  3727. public static FromRotationMatrix(matrix: DeepImmutable<Matrix>): Quaternion {
  3728. var result = new Quaternion();
  3729. Quaternion.FromRotationMatrixToRef(matrix, result);
  3730. return result;
  3731. }
  3732. /**
  3733. * Updates the given quaternion with the given rotation matrix values
  3734. * @param matrix defines the source matrix
  3735. * @param result defines the target quaternion
  3736. */
  3737. public static FromRotationMatrixToRef(matrix: DeepImmutable<Matrix>, result: Quaternion): void {
  3738. var data = matrix.m;
  3739. var m11 = data[0], m12 = data[4], m13 = data[8];
  3740. var m21 = data[1], m22 = data[5], m23 = data[9];
  3741. var m31 = data[2], m32 = data[6], m33 = data[10];
  3742. var trace = m11 + m22 + m33;
  3743. var s;
  3744. if (trace > 0) {
  3745. s = 0.5 / Math.sqrt(trace + 1.0);
  3746. result.w = 0.25 / s;
  3747. result.x = (m32 - m23) * s;
  3748. result.y = (m13 - m31) * s;
  3749. result.z = (m21 - m12) * s;
  3750. } else if (m11 > m22 && m11 > m33) {
  3751. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  3752. result.w = (m32 - m23) / s;
  3753. result.x = 0.25 * s;
  3754. result.y = (m12 + m21) / s;
  3755. result.z = (m13 + m31) / s;
  3756. } else if (m22 > m33) {
  3757. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  3758. result.w = (m13 - m31) / s;
  3759. result.x = (m12 + m21) / s;
  3760. result.y = 0.25 * s;
  3761. result.z = (m23 + m32) / s;
  3762. } else {
  3763. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  3764. result.w = (m21 - m12) / s;
  3765. result.x = (m13 + m31) / s;
  3766. result.y = (m23 + m32) / s;
  3767. result.z = 0.25 * s;
  3768. }
  3769. }
  3770. /**
  3771. * Returns the dot product (float) between the quaternions "left" and "right"
  3772. * @param left defines the left operand
  3773. * @param right defines the right operand
  3774. * @returns the dot product
  3775. */
  3776. public static Dot(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>): number {
  3777. return (left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w);
  3778. }
  3779. /**
  3780. * Checks if the two quaternions are close to each other
  3781. * @param quat0 defines the first quaternion to check
  3782. * @param quat1 defines the second quaternion to check
  3783. * @returns true if the two quaternions are close to each other
  3784. */
  3785. public static AreClose(quat0: DeepImmutable<Quaternion>, quat1: DeepImmutable<Quaternion>): boolean {
  3786. let dot = Quaternion.Dot(quat0, quat1);
  3787. return dot >= 0;
  3788. }
  3789. /**
  3790. * Creates an empty quaternion
  3791. * @returns a new quaternion set to (0.0, 0.0, 0.0)
  3792. */
  3793. public static Zero(): Quaternion {
  3794. return new Quaternion(0.0, 0.0, 0.0, 0.0);
  3795. }
  3796. /**
  3797. * Inverse a given quaternion
  3798. * @param q defines the source quaternion
  3799. * @returns a new quaternion as the inverted current quaternion
  3800. */
  3801. public static Inverse(q: DeepImmutable<Quaternion>): Quaternion {
  3802. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  3803. }
  3804. /**
  3805. * Inverse a given quaternion
  3806. * @param q defines the source quaternion
  3807. * @param result the quaternion the result will be stored in
  3808. * @returns the result quaternion
  3809. */
  3810. public static InverseToRef(q: Quaternion, result: Quaternion): Quaternion {
  3811. result.set(-q.x, -q.y, -q.z, q.w);
  3812. return result;
  3813. }
  3814. /**
  3815. * Creates an identity quaternion
  3816. * @returns the identity quaternion
  3817. */
  3818. public static Identity(): Quaternion {
  3819. return new Quaternion(0.0, 0.0, 0.0, 1.0);
  3820. }
  3821. /**
  3822. * Gets a boolean indicating if the given quaternion is identity
  3823. * @param quaternion defines the quaternion to check
  3824. * @returns true if the quaternion is identity
  3825. */
  3826. public static IsIdentity(quaternion: DeepImmutable<Quaternion>): boolean {
  3827. return quaternion && quaternion.x === 0 && quaternion.y === 0 && quaternion.z === 0 && quaternion.w === 1;
  3828. }
  3829. /**
  3830. * Creates a quaternion from a rotation around an axis
  3831. * @param axis defines the axis to use
  3832. * @param angle defines the angle to use
  3833. * @returns a new quaternion created from the given axis (Vector3) and angle in radians (float)
  3834. */
  3835. public static RotationAxis(axis: DeepImmutable<Vector3>, angle: number): Quaternion {
  3836. return Quaternion.RotationAxisToRef(axis, angle, new Quaternion());
  3837. }
  3838. /**
  3839. * Creates a rotation around an axis and stores it into the given quaternion
  3840. * @param axis defines the axis to use
  3841. * @param angle defines the angle to use
  3842. * @param result defines the target quaternion
  3843. * @returns the target quaternion
  3844. */
  3845. public static RotationAxisToRef(axis: DeepImmutable<Vector3>, angle: number, result: Quaternion): Quaternion {
  3846. var sin = Math.sin(angle / 2);
  3847. axis.normalize();
  3848. result.w = Math.cos(angle / 2);
  3849. result.x = axis.x * sin;
  3850. result.y = axis.y * sin;
  3851. result.z = axis.z * sin;
  3852. return result;
  3853. }
  3854. /**
  3855. * Creates a new quaternion from data stored into an array
  3856. * @param array defines the data source
  3857. * @param offset defines the offset in the source array where the data starts
  3858. * @returns a new quaternion
  3859. */
  3860. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset?: number): Quaternion {
  3861. if (!offset) {
  3862. offset = 0;
  3863. }
  3864. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  3865. }
  3866. /**
  3867. * Create a quaternion from Euler rotation angles
  3868. * @param x Pitch
  3869. * @param y Yaw
  3870. * @param z Roll
  3871. * @returns the new Quaternion
  3872. */
  3873. public static FromEulerAngles(x: number, y: number, z: number): Quaternion {
  3874. var q = new Quaternion();
  3875. Quaternion.RotationYawPitchRollToRef(y, x, z, q);
  3876. return q;
  3877. }
  3878. /**
  3879. * Updates a quaternion from Euler rotation angles
  3880. * @param x Pitch
  3881. * @param y Yaw
  3882. * @param z Roll
  3883. * @param result the quaternion to store the result
  3884. * @returns the updated quaternion
  3885. */
  3886. public static FromEulerAnglesToRef(x: number, y: number, z: number, result: Quaternion): Quaternion {
  3887. Quaternion.RotationYawPitchRollToRef(y, x, z, result);
  3888. return result;
  3889. }
  3890. /**
  3891. * Create a quaternion from Euler rotation vector
  3892. * @param vec the Euler vector (x Pitch, y Yaw, z Roll)
  3893. * @returns the new Quaternion
  3894. */
  3895. public static FromEulerVector(vec: DeepImmutable<Vector3>): Quaternion {
  3896. var q = new Quaternion();
  3897. Quaternion.RotationYawPitchRollToRef(vec.y, vec.x, vec.z, q);
  3898. return q;
  3899. }
  3900. /**
  3901. * Updates a quaternion from Euler rotation vector
  3902. * @param vec the Euler vector (x Pitch, y Yaw, z Roll)
  3903. * @param result the quaternion to store the result
  3904. * @returns the updated quaternion
  3905. */
  3906. public static FromEulerVectorToRef(vec: DeepImmutable<Vector3>, result: Quaternion): Quaternion {
  3907. Quaternion.RotationYawPitchRollToRef(vec.y, vec.x, vec.z, result);
  3908. return result;
  3909. }
  3910. /**
  3911. * Creates a new quaternion from the given Euler float angles (y, x, z)
  3912. * @param yaw defines the rotation around Y axis
  3913. * @param pitch defines the rotation around X axis
  3914. * @param roll defines the rotation around Z axis
  3915. * @returns the new quaternion
  3916. */
  3917. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  3918. var q = new Quaternion();
  3919. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, q);
  3920. return q;
  3921. }
  3922. /**
  3923. * Creates a new rotation from the given Euler float angles (y, x, z) and stores it in the target quaternion
  3924. * @param yaw defines the rotation around Y axis
  3925. * @param pitch defines the rotation around X axis
  3926. * @param roll defines the rotation around Z axis
  3927. * @param result defines the target quaternion
  3928. */
  3929. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  3930. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  3931. var halfRoll = roll * 0.5;
  3932. var halfPitch = pitch * 0.5;
  3933. var halfYaw = yaw * 0.5;
  3934. var sinRoll = Math.sin(halfRoll);
  3935. var cosRoll = Math.cos(halfRoll);
  3936. var sinPitch = Math.sin(halfPitch);
  3937. var cosPitch = Math.cos(halfPitch);
  3938. var sinYaw = Math.sin(halfYaw);
  3939. var cosYaw = Math.cos(halfYaw);
  3940. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  3941. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  3942. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  3943. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  3944. }
  3945. /**
  3946. * Creates a new quaternion from the given Euler float angles expressed in z-x-z orientation
  3947. * @param alpha defines the rotation around first axis
  3948. * @param beta defines the rotation around second axis
  3949. * @param gamma defines the rotation around third axis
  3950. * @returns the new quaternion
  3951. */
  3952. public static RotationAlphaBetaGamma(alpha: number, beta: number, gamma: number): Quaternion {
  3953. var result = new Quaternion();
  3954. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  3955. return result;
  3956. }
  3957. /**
  3958. * Creates a new quaternion from the given Euler float angles expressed in z-x-z orientation and stores it in the target quaternion
  3959. * @param alpha defines the rotation around first axis
  3960. * @param beta defines the rotation around second axis
  3961. * @param gamma defines the rotation around third axis
  3962. * @param result defines the target quaternion
  3963. */
  3964. public static RotationAlphaBetaGammaToRef(alpha: number, beta: number, gamma: number, result: Quaternion): void {
  3965. // Produces a quaternion from Euler angles in the z-x-z orientation
  3966. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  3967. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  3968. var halfBeta = beta * 0.5;
  3969. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  3970. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  3971. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  3972. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  3973. }
  3974. /**
  3975. * 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)
  3976. * @param axis1 defines the first axis
  3977. * @param axis2 defines the second axis
  3978. * @param axis3 defines the third axis
  3979. * @returns the new quaternion
  3980. */
  3981. public static RotationQuaternionFromAxis(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>): Quaternion {
  3982. var quat = new Quaternion(0.0, 0.0, 0.0, 0.0);
  3983. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  3984. return quat;
  3985. }
  3986. /**
  3987. * 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
  3988. * @param axis1 defines the first axis
  3989. * @param axis2 defines the second axis
  3990. * @param axis3 defines the third axis
  3991. * @param ref defines the target quaternion
  3992. */
  3993. public static RotationQuaternionFromAxisToRef(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>, ref: Quaternion): void {
  3994. var rotMat = MathTmp.Matrix[0];
  3995. Matrix.FromXYZAxesToRef(axis1.normalize(), axis2.normalize(), axis3.normalize(), rotMat);
  3996. Quaternion.FromRotationMatrixToRef(rotMat, ref);
  3997. }
  3998. /**
  3999. * Interpolates between two quaternions
  4000. * @param left defines first quaternion
  4001. * @param right defines second quaternion
  4002. * @param amount defines the gradient to use
  4003. * @returns the new interpolated quaternion
  4004. */
  4005. public static Slerp(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>, amount: number): Quaternion {
  4006. var result = Quaternion.Identity();
  4007. Quaternion.SlerpToRef(left, right, amount, result);
  4008. return result;
  4009. }
  4010. /**
  4011. * Interpolates between two quaternions and stores it into a target quaternion
  4012. * @param left defines first quaternion
  4013. * @param right defines second quaternion
  4014. * @param amount defines the gradient to use
  4015. * @param result defines the target quaternion
  4016. */
  4017. public static SlerpToRef(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>, amount: number, result: Quaternion): void {
  4018. var num2;
  4019. var num3;
  4020. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  4021. var flag = false;
  4022. if (num4 < 0) {
  4023. flag = true;
  4024. num4 = -num4;
  4025. }
  4026. if (num4 > 0.999999) {
  4027. num3 = 1 - amount;
  4028. num2 = flag ? -amount : amount;
  4029. }
  4030. else {
  4031. var num5 = Math.acos(num4);
  4032. var num6 = (1.0 / Math.sin(num5));
  4033. num3 = (Math.sin((1.0 - amount) * num5)) * num6;
  4034. num2 = flag ? ((-Math.sin(amount * num5)) * num6) : ((Math.sin(amount * num5)) * num6);
  4035. }
  4036. result.x = (num3 * left.x) + (num2 * right.x);
  4037. result.y = (num3 * left.y) + (num2 * right.y);
  4038. result.z = (num3 * left.z) + (num2 * right.z);
  4039. result.w = (num3 * left.w) + (num2 * right.w);
  4040. }
  4041. /**
  4042. * Interpolate between two quaternions using Hermite interpolation
  4043. * @param value1 defines first quaternion
  4044. * @param tangent1 defines the incoming tangent
  4045. * @param value2 defines second quaternion
  4046. * @param tangent2 defines the outgoing tangent
  4047. * @param amount defines the target quaternion
  4048. * @returns the new interpolated quaternion
  4049. */
  4050. public static Hermite(value1: DeepImmutable<Quaternion>, tangent1: DeepImmutable<Quaternion>, value2: DeepImmutable<Quaternion>, tangent2: DeepImmutable<Quaternion>, amount: number): Quaternion {
  4051. var squared = amount * amount;
  4052. var cubed = amount * squared;
  4053. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  4054. var part2 = (-2.0 * cubed) + (3.0 * squared);
  4055. var part3 = (cubed - (2.0 * squared)) + amount;
  4056. var part4 = cubed - squared;
  4057. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  4058. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  4059. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  4060. var w = (((value1.w * part1) + (value2.w * part2)) + (tangent1.w * part3)) + (tangent2.w * part4);
  4061. return new Quaternion(x, y, z, w);
  4062. }
  4063. }
  4064. /**
  4065. * Class used to store matrix data (4x4)
  4066. */
  4067. export class Matrix {
  4068. private static _updateFlagSeed = 0;
  4069. private static _identityReadOnly = Matrix.Identity() as DeepImmutable<Matrix>;
  4070. private _isIdentity = false;
  4071. private _isIdentityDirty = true;
  4072. private _isIdentity3x2 = true;
  4073. private _isIdentity3x2Dirty = true;
  4074. /**
  4075. * Gets the update flag of the matrix which is an unique number for the matrix.
  4076. * It will be incremented every time the matrix data change.
  4077. * You can use it to speed the comparison between two versions of the same matrix.
  4078. */
  4079. public updateFlag: number = -1;
  4080. private readonly _m: Float32Array = new Float32Array(16);
  4081. /**
  4082. * Gets the internal data of the matrix
  4083. */
  4084. public get m(): DeepImmutable<Float32Array> { return this._m; }
  4085. /** @hidden */
  4086. public _markAsUpdated() {
  4087. this.updateFlag = Matrix._updateFlagSeed++;
  4088. this._isIdentity = false;
  4089. this._isIdentity3x2 = false;
  4090. this._isIdentityDirty = true;
  4091. this._isIdentity3x2Dirty = true;
  4092. }
  4093. /** @hidden */
  4094. private _updateIdentityStatus(isIdentity: boolean, isIdentityDirty: boolean = false, isIdentity3x2: boolean = false, isIdentity3x2Dirty: boolean = true) {
  4095. this.updateFlag = Matrix._updateFlagSeed++;
  4096. this._isIdentity = isIdentity;
  4097. this._isIdentity3x2 = isIdentity || isIdentity3x2;
  4098. this._isIdentityDirty = this._isIdentity ? false : isIdentityDirty;
  4099. this._isIdentity3x2Dirty = this._isIdentity3x2 ? false : isIdentity3x2Dirty;
  4100. }
  4101. /**
  4102. * Creates an empty matrix (filled with zeros)
  4103. */
  4104. public constructor() {
  4105. this._updateIdentityStatus(false);
  4106. }
  4107. // Properties
  4108. /**
  4109. * Check if the current matrix is identity
  4110. * @returns true is the matrix is the identity matrix
  4111. */
  4112. public isIdentity(): boolean {
  4113. if (this._isIdentityDirty) {
  4114. this._isIdentityDirty = false;
  4115. const m = this._m;
  4116. this._isIdentity = (
  4117. m[0] === 1.0 && m[1] === 0.0 && m[2] === 0.0 && m[3] === 0.0 &&
  4118. m[4] === 0.0 && m[5] === 1.0 && m[6] === 0.0 && m[7] === 0.0 &&
  4119. m[8] === 0.0 && m[9] === 0.0 && m[10] === 1.0 && m[11] === 0.0 &&
  4120. m[12] === 0.0 && m[13] === 0.0 && m[14] === 0.0 && m[15] === 1.0
  4121. );
  4122. }
  4123. return this._isIdentity;
  4124. }
  4125. /**
  4126. * Check if the current matrix is identity as a texture matrix (3x2 store in 4x4)
  4127. * @returns true is the matrix is the identity matrix
  4128. */
  4129. public isIdentityAs3x2(): boolean {
  4130. if (this._isIdentity3x2Dirty) {
  4131. this._isIdentity3x2Dirty = false;
  4132. if (this._m[0] !== 1.0 || this._m[5] !== 1.0 || this._m[15] !== 1.0) {
  4133. this._isIdentity3x2 = false;
  4134. } else if (this._m[1] !== 0.0 || this._m[2] !== 0.0 || this._m[3] !== 0.0 ||
  4135. this._m[4] !== 0.0 || this._m[6] !== 0.0 || this._m[7] !== 0.0 ||
  4136. this._m[8] !== 0.0 || this._m[9] !== 0.0 || this._m[10] !== 0.0 || this._m[11] !== 0.0 ||
  4137. this._m[12] !== 0.0 || this._m[13] !== 0.0 || this._m[14] !== 0.0) {
  4138. this._isIdentity3x2 = false;
  4139. } else {
  4140. this._isIdentity3x2 = true;
  4141. }
  4142. }
  4143. return this._isIdentity3x2;
  4144. }
  4145. /**
  4146. * Gets the determinant of the matrix
  4147. * @returns the matrix determinant
  4148. */
  4149. public determinant(): number {
  4150. if (this._isIdentity === true) {
  4151. return 1;
  4152. }
  4153. const m = this._m;
  4154. const m00 = m[0], m01 = m[1], m02 = m[2], m03 = m[3];
  4155. const m10 = m[4], m11 = m[5], m12 = m[6], m13 = m[7];
  4156. const m20 = m[8], m21 = m[9], m22 = m[10], m23 = m[11];
  4157. const m30 = m[12], m31 = m[13], m32 = m[14], m33 = m[15];
  4158. // https://en.wikipedia.org/wiki/Laplace_expansion
  4159. // to compute the deterrminant of a 4x4 Matrix we compute the cofactors of any row or column,
  4160. // then we multiply each Cofactor by its corresponding matrix value and sum them all to get the determinant
  4161. // Cofactor(i, j) = sign(i,j) * det(Minor(i, j))
  4162. // where
  4163. // - sign(i,j) = (i+j) % 2 === 0 ? 1 : -1
  4164. // - Minor(i, j) is the 3x3 matrix we get by removing row i and column j from current Matrix
  4165. //
  4166. // Here we do that for the 1st row.
  4167. const det_22_33 = m22 * m33 - m32 * m23;
  4168. const det_21_33 = m21 * m33 - m31 * m23;
  4169. const det_21_32 = m21 * m32 - m31 * m22;
  4170. const det_20_33 = m20 * m33 - m30 * m23;
  4171. const det_20_32 = m20 * m32 - m22 * m30;
  4172. const det_20_31 = m20 * m31 - m30 * m21;
  4173. const cofact_00 = +(m11 * det_22_33 - m12 * det_21_33 + m13 * det_21_32);
  4174. const cofact_01 = -(m10 * det_22_33 - m12 * det_20_33 + m13 * det_20_32);
  4175. const cofact_02 = +(m10 * det_21_33 - m11 * det_20_33 + m13 * det_20_31);
  4176. const cofact_03 = -(m10 * det_21_32 - m11 * det_20_32 + m12 * det_20_31);
  4177. return m00 * cofact_00 + m01 * cofact_01 + m02 * cofact_02 + m03 * cofact_03;
  4178. }
  4179. // Methods
  4180. /**
  4181. * Returns the matrix as a Float32Array
  4182. * @returns the matrix underlying array
  4183. */
  4184. public toArray(): DeepImmutable<Float32Array> {
  4185. return this._m;
  4186. }
  4187. /**
  4188. * Returns the matrix as a Float32Array
  4189. * @returns the matrix underlying array.
  4190. */
  4191. public asArray(): DeepImmutable<Float32Array> {
  4192. return this._m;
  4193. }
  4194. /**
  4195. * Inverts the current matrix in place
  4196. * @returns the current inverted matrix
  4197. */
  4198. public invert(): Matrix {
  4199. this.invertToRef(this);
  4200. return this;
  4201. }
  4202. /**
  4203. * Sets all the matrix elements to zero
  4204. * @returns the current matrix
  4205. */
  4206. public reset(): Matrix {
  4207. Matrix.FromValuesToRef(
  4208. 0.0, 0.0, 0.0, 0.0,
  4209. 0.0, 0.0, 0.0, 0.0,
  4210. 0.0, 0.0, 0.0, 0.0,
  4211. 0.0, 0.0, 0.0, 0.0,
  4212. this
  4213. );
  4214. this._updateIdentityStatus(false);
  4215. return this;
  4216. }
  4217. /**
  4218. * Adds the current matrix with a second one
  4219. * @param other defines the matrix to add
  4220. * @returns a new matrix as the addition of the current matrix and the given one
  4221. */
  4222. public add(other: DeepImmutable<Matrix>): Matrix {
  4223. var result = new Matrix();
  4224. this.addToRef(other, result);
  4225. return result;
  4226. }
  4227. /**
  4228. * Sets the given matrix "result" to the addition of the current matrix and the given one
  4229. * @param other defines the matrix to add
  4230. * @param result defines the target matrix
  4231. * @returns the current matrix
  4232. */
  4233. public addToRef(other: DeepImmutable<Matrix>, result: Matrix): Matrix {
  4234. const m = this._m;
  4235. const resultM = result._m;
  4236. const otherM = other.m;
  4237. for (var index = 0; index < 16; index++) {
  4238. resultM[index] = m[index] + otherM[index];
  4239. }
  4240. result._markAsUpdated();
  4241. return this;
  4242. }
  4243. /**
  4244. * Adds in place the given matrix to the current matrix
  4245. * @param other defines the second operand
  4246. * @returns the current updated matrix
  4247. */
  4248. public addToSelf(other: DeepImmutable<Matrix>): Matrix {
  4249. const m = this._m;
  4250. const otherM = other.m;
  4251. for (var index = 0; index < 16; index++) {
  4252. m[index] += otherM[index];
  4253. }
  4254. this._markAsUpdated();
  4255. return this;
  4256. }
  4257. /**
  4258. * Sets the given matrix to the current inverted Matrix
  4259. * @param other defines the target matrix
  4260. * @returns the unmodified current matrix
  4261. */
  4262. public invertToRef(other: Matrix): Matrix {
  4263. if (this._isIdentity === true) {
  4264. Matrix.IdentityToRef(other);
  4265. return this;
  4266. }
  4267. // the inverse of a Matrix is the transpose of cofactor matrix divided by the determinant
  4268. const m = this._m;
  4269. const m00 = m[0], m01 = m[1], m02 = m[2], m03 = m[3];
  4270. const m10 = m[4], m11 = m[5], m12 = m[6], m13 = m[7];
  4271. const m20 = m[8], m21 = m[9], m22 = m[10], m23 = m[11];
  4272. const m30 = m[12], m31 = m[13], m32 = m[14], m33 = m[15];
  4273. const det_22_33 = m22 * m33 - m32 * m23;
  4274. const det_21_33 = m21 * m33 - m31 * m23;
  4275. const det_21_32 = m21 * m32 - m31 * m22;
  4276. const det_20_33 = m20 * m33 - m30 * m23;
  4277. const det_20_32 = m20 * m32 - m22 * m30;
  4278. const det_20_31 = m20 * m31 - m30 * m21;
  4279. const cofact_00 = +(m11 * det_22_33 - m12 * det_21_33 + m13 * det_21_32);
  4280. const cofact_01 = -(m10 * det_22_33 - m12 * det_20_33 + m13 * det_20_32);
  4281. const cofact_02 = +(m10 * det_21_33 - m11 * det_20_33 + m13 * det_20_31);
  4282. const cofact_03 = -(m10 * det_21_32 - m11 * det_20_32 + m12 * det_20_31);
  4283. const det = m00 * cofact_00 + m01 * cofact_01 + m02 * cofact_02 + m03 * cofact_03;
  4284. if (det === 0) {
  4285. // not invertible
  4286. other.copyFrom(this);
  4287. return this;
  4288. }
  4289. const detInv = 1 / det;
  4290. const det_12_33 = m12 * m33 - m32 * m13;
  4291. const det_11_33 = m11 * m33 - m31 * m13;
  4292. const det_11_32 = m11 * m32 - m31 * m12;
  4293. const det_10_33 = m10 * m33 - m30 * m13;
  4294. const det_10_32 = m10 * m32 - m30 * m12;
  4295. const det_10_31 = m10 * m31 - m30 * m11;
  4296. const det_12_23 = m12 * m23 - m22 * m13;
  4297. const det_11_23 = m11 * m23 - m21 * m13;
  4298. const det_11_22 = m11 * m22 - m21 * m12;
  4299. const det_10_23 = m10 * m23 - m20 * m13;
  4300. const det_10_22 = m10 * m22 - m20 * m12;
  4301. const det_10_21 = m10 * m21 - m20 * m11;
  4302. const cofact_10 = -(m01 * det_22_33 - m02 * det_21_33 + m03 * det_21_32);
  4303. const cofact_11 = +(m00 * det_22_33 - m02 * det_20_33 + m03 * det_20_32);
  4304. const cofact_12 = -(m00 * det_21_33 - m01 * det_20_33 + m03 * det_20_31);
  4305. const cofact_13 = +(m00 * det_21_32 - m01 * det_20_32 + m02 * det_20_31);
  4306. const cofact_20 = +(m01 * det_12_33 - m02 * det_11_33 + m03 * det_11_32);
  4307. const cofact_21 = -(m00 * det_12_33 - m02 * det_10_33 + m03 * det_10_32);
  4308. const cofact_22 = +(m00 * det_11_33 - m01 * det_10_33 + m03 * det_10_31);
  4309. const cofact_23 = -(m00 * det_11_32 - m01 * det_10_32 + m02 * det_10_31);
  4310. const cofact_30 = -(m01 * det_12_23 - m02 * det_11_23 + m03 * det_11_22);
  4311. const cofact_31 = +(m00 * det_12_23 - m02 * det_10_23 + m03 * det_10_22);
  4312. const cofact_32 = -(m00 * det_11_23 - m01 * det_10_23 + m03 * det_10_21);
  4313. const cofact_33 = +(m00 * det_11_22 - m01 * det_10_22 + m02 * det_10_21);
  4314. Matrix.FromValuesToRef(
  4315. cofact_00 * detInv, cofact_10 * detInv, cofact_20 * detInv, cofact_30 * detInv,
  4316. cofact_01 * detInv, cofact_11 * detInv, cofact_21 * detInv, cofact_31 * detInv,
  4317. cofact_02 * detInv, cofact_12 * detInv, cofact_22 * detInv, cofact_32 * detInv,
  4318. cofact_03 * detInv, cofact_13 * detInv, cofact_23 * detInv, cofact_33 * detInv,
  4319. other
  4320. );
  4321. return this;
  4322. }
  4323. /**
  4324. * add a value at the specified position in the current Matrix
  4325. * @param index the index of the value within the matrix. between 0 and 15.
  4326. * @param value the value to be added
  4327. * @returns the current updated matrix
  4328. */
  4329. public addAtIndex(index: number, value: number): Matrix {
  4330. this._m[index] += value;
  4331. this._markAsUpdated();
  4332. return this;
  4333. }
  4334. /**
  4335. * mutiply the specified position in the current Matrix by a value
  4336. * @param index the index of the value within the matrix. between 0 and 15.
  4337. * @param value the value to be added
  4338. * @returns the current updated matrix
  4339. */
  4340. public multiplyAtIndex(index: number, value: number): Matrix {
  4341. this._m[index] *= value;
  4342. this._markAsUpdated();
  4343. return this;
  4344. }
  4345. /**
  4346. * Inserts the translation vector (using 3 floats) in the current matrix
  4347. * @param x defines the 1st component of the translation
  4348. * @param y defines the 2nd component of the translation
  4349. * @param z defines the 3rd component of the translation
  4350. * @returns the current updated matrix
  4351. */
  4352. public setTranslationFromFloats(x: number, y: number, z: number): Matrix {
  4353. this._m[12] = x;
  4354. this._m[13] = y;
  4355. this._m[14] = z;
  4356. this._markAsUpdated();
  4357. return this;
  4358. }
  4359. /**
  4360. * Adds the translation vector (using 3 floats) in the current matrix
  4361. * @param x defines the 1st component of the translation
  4362. * @param y defines the 2nd component of the translation
  4363. * @param z defines the 3rd component of the translation
  4364. * @returns the current updated matrix
  4365. */
  4366. public addTranslationFromFloats(x: number, y: number, z: number): Matrix {
  4367. this._m[12] += x;
  4368. this._m[13] += y;
  4369. this._m[14] += z;
  4370. this._markAsUpdated();
  4371. return this;
  4372. }
  4373. /**
  4374. * Inserts the translation vector in the current matrix
  4375. * @param vector3 defines the translation to insert
  4376. * @returns the current updated matrix
  4377. */
  4378. public setTranslation(vector3: DeepImmutable<Vector3>): Matrix {
  4379. return this.setTranslationFromFloats(vector3.x, vector3.y, vector3.z);
  4380. }
  4381. /**
  4382. * Gets the translation value of the current matrix
  4383. * @returns a new Vector3 as the extracted translation from the matrix
  4384. */
  4385. public getTranslation(): Vector3 {
  4386. return new Vector3(this._m[12], this._m[13], this._m[14]);
  4387. }
  4388. /**
  4389. * Fill a Vector3 with the extracted translation from the matrix
  4390. * @param result defines the Vector3 where to store the translation
  4391. * @returns the current matrix
  4392. */
  4393. public getTranslationToRef(result: Vector3): Matrix {
  4394. result.x = this._m[12];
  4395. result.y = this._m[13];
  4396. result.z = this._m[14];
  4397. return this;
  4398. }
  4399. /**
  4400. * Remove rotation and scaling part from the matrix
  4401. * @returns the updated matrix
  4402. */
  4403. public removeRotationAndScaling(): Matrix {
  4404. const m = this.m;
  4405. Matrix.FromValuesToRef(
  4406. 1.0, 0.0, 0.0, 0.0,
  4407. 0.0, 1.0, 0.0, 0.0,
  4408. 0.0, 0.0, 1.0, 0.0,
  4409. m[12], m[13], m[14], m[15],
  4410. this
  4411. );
  4412. this._updateIdentityStatus(m[12] === 0 && m[13] === 0 && m[14] === 0 && m[15] === 1);
  4413. return this;
  4414. }
  4415. /**
  4416. * Multiply two matrices
  4417. * @param other defines the second operand
  4418. * @returns a new matrix set with the multiplication result of the current Matrix and the given one
  4419. */
  4420. public multiply(other: DeepImmutable<Matrix>): Matrix {
  4421. var result = new Matrix();
  4422. this.multiplyToRef(other, result);
  4423. return result;
  4424. }
  4425. /**
  4426. * Copy the current matrix from the given one
  4427. * @param other defines the source matrix
  4428. * @returns the current updated matrix
  4429. */
  4430. public copyFrom(other: DeepImmutable<Matrix>): Matrix {
  4431. other.copyToArray(this._m);
  4432. const o = (other as Matrix);
  4433. this._updateIdentityStatus(o._isIdentity, o._isIdentityDirty, o._isIdentity3x2, o._isIdentity3x2Dirty);
  4434. return this;
  4435. }
  4436. /**
  4437. * Populates the given array from the starting index with the current matrix values
  4438. * @param array defines the target array
  4439. * @param offset defines the offset in the target array where to start storing values
  4440. * @returns the current matrix
  4441. */
  4442. public copyToArray(array: Float32Array, offset: number = 0): Matrix {
  4443. for (var index = 0; index < 16; index++) {
  4444. array[offset + index] = this._m[index];
  4445. }
  4446. return this;
  4447. }
  4448. /**
  4449. * Sets the given matrix "result" with the multiplication result of the current Matrix and the given one
  4450. * @param other defines the second operand
  4451. * @param result defines the matrix where to store the multiplication
  4452. * @returns the current matrix
  4453. */
  4454. public multiplyToRef(other: DeepImmutable<Matrix>, result: Matrix): Matrix {
  4455. if (this._isIdentity) {
  4456. result.copyFrom(other);
  4457. return this;
  4458. }
  4459. if ((other as Matrix)._isIdentity) {
  4460. result.copyFrom(this);
  4461. return this;
  4462. }
  4463. this.multiplyToArray(other, result._m, 0);
  4464. result._markAsUpdated();
  4465. return this;
  4466. }
  4467. /**
  4468. * Sets the Float32Array "result" from the given index "offset" with the multiplication of the current matrix and the given one
  4469. * @param other defines the second operand
  4470. * @param result defines the array where to store the multiplication
  4471. * @param offset defines the offset in the target array where to start storing values
  4472. * @returns the current matrix
  4473. */
  4474. public multiplyToArray(other: DeepImmutable<Matrix>, result: Float32Array, offset: number): Matrix {
  4475. const m = this._m;
  4476. const otherM = other.m;
  4477. var tm0 = m[0], tm1 = m[1], tm2 = m[2], tm3 = m[3];
  4478. var tm4 = m[4], tm5 = m[5], tm6 = m[6], tm7 = m[7];
  4479. var tm8 = m[8], tm9 = m[9], tm10 = m[10], tm11 = m[11];
  4480. var tm12 = m[12], tm13 = m[13], tm14 = m[14], tm15 = m[15];
  4481. var om0 = otherM[0], om1 = otherM[1], om2 = otherM[2], om3 = otherM[3];
  4482. var om4 = otherM[4], om5 = otherM[5], om6 = otherM[6], om7 = otherM[7];
  4483. var om8 = otherM[8], om9 = otherM[9], om10 = otherM[10], om11 = otherM[11];
  4484. var om12 = otherM[12], om13 = otherM[13], om14 = otherM[14], om15 = otherM[15];
  4485. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  4486. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  4487. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  4488. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  4489. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  4490. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  4491. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  4492. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  4493. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  4494. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  4495. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  4496. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  4497. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  4498. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  4499. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  4500. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  4501. return this;
  4502. }
  4503. /**
  4504. * Check equality between this matrix and a second one
  4505. * @param value defines the second matrix to compare
  4506. * @returns true is the current matrix and the given one values are strictly equal
  4507. */
  4508. public equals(value: DeepImmutable<Matrix>): boolean {
  4509. const other = (value as Matrix);
  4510. if (!other) {
  4511. return false;
  4512. }
  4513. if (this._isIdentity || other._isIdentity) {
  4514. if (!this._isIdentityDirty && !other._isIdentityDirty) {
  4515. return this._isIdentity && other._isIdentity;
  4516. }
  4517. }
  4518. const m = this.m;
  4519. const om = other.m;
  4520. return (
  4521. m[0] === om[0] && m[1] === om[1] && m[2] === om[2] && m[3] === om[3] &&
  4522. m[4] === om[4] && m[5] === om[5] && m[6] === om[6] && m[7] === om[7] &&
  4523. m[8] === om[8] && m[9] === om[9] && m[10] === om[10] && m[11] === om[11] &&
  4524. m[12] === om[12] && m[13] === om[13] && m[14] === om[14] && m[15] === om[15]
  4525. );
  4526. }
  4527. /**
  4528. * Clone the current matrix
  4529. * @returns a new matrix from the current matrix
  4530. */
  4531. public clone(): Matrix {
  4532. const matrix = new Matrix();
  4533. matrix.copyFrom(this);
  4534. return matrix;
  4535. }
  4536. /**
  4537. * Returns the name of the current matrix class
  4538. * @returns the string "Matrix"
  4539. */
  4540. public getClassName(): string {
  4541. return "Matrix";
  4542. }
  4543. /**
  4544. * Gets the hash code of the current matrix
  4545. * @returns the hash code
  4546. */
  4547. public getHashCode(): number {
  4548. let hash = this._m[0] | 0;
  4549. for (let i = 1; i < 16; i++) {
  4550. hash = (hash * 397) ^ (this._m[i] | 0);
  4551. }
  4552. return hash;
  4553. }
  4554. /**
  4555. * Decomposes the current Matrix into a translation, rotation and scaling components
  4556. * @param scale defines the scale vector3 given as a reference to update
  4557. * @param rotation defines the rotation quaternion given as a reference to update
  4558. * @param translation defines the translation vector3 given as a reference to update
  4559. * @returns true if operation was successful
  4560. */
  4561. public decompose(scale?: Vector3, rotation?: Quaternion, translation?: Vector3): boolean {
  4562. if (this._isIdentity) {
  4563. if (translation) {
  4564. translation.setAll(0);
  4565. }
  4566. if (scale) {
  4567. scale.setAll(1);
  4568. }
  4569. if (rotation) {
  4570. rotation.copyFromFloats(0, 0, 0, 1);
  4571. }
  4572. return true;
  4573. }
  4574. const m = this._m;
  4575. if (translation) {
  4576. translation.copyFromFloats(m[12], m[13], m[14]);
  4577. }
  4578. scale = scale || MathTmp.Vector3[0];
  4579. scale.x = Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
  4580. scale.y = Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);
  4581. scale.z = Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]);
  4582. if (this.determinant() <= 0) {
  4583. scale.y *= -1;
  4584. }
  4585. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  4586. if (rotation) {
  4587. rotation.copyFromFloats(0.0, 0.0, 0.0, 1.0);
  4588. }
  4589. return false;
  4590. }
  4591. if (rotation) {
  4592. const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
  4593. Matrix.FromValuesToRef(
  4594. m[0] * sx, m[1] * sx, m[2] * sx, 0.0,
  4595. m[4] * sy, m[5] * sy, m[6] * sy, 0.0,
  4596. m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
  4597. 0.0, 0.0, 0.0, 1.0,
  4598. MathTmp.Matrix[0]
  4599. );
  4600. Quaternion.FromRotationMatrixToRef(MathTmp.Matrix[0], rotation);
  4601. }
  4602. return true;
  4603. }
  4604. /**
  4605. * Gets specific row of the matrix
  4606. * @param index defines the number of the row to get
  4607. * @returns the index-th row of the current matrix as a new Vector4
  4608. */
  4609. public getRow(index: number): Nullable<Vector4> {
  4610. if (index < 0 || index > 3) {
  4611. return null;
  4612. }
  4613. var i = index * 4;
  4614. return new Vector4(this._m[i + 0], this._m[i + 1], this._m[i + 2], this._m[i + 3]);
  4615. }
  4616. /**
  4617. * Sets the index-th row of the current matrix to the vector4 values
  4618. * @param index defines the number of the row to set
  4619. * @param row defines the target vector4
  4620. * @returns the updated current matrix
  4621. */
  4622. public setRow(index: number, row: Vector4): Matrix {
  4623. return this.setRowFromFloats(index, row.x, row.y, row.z, row.w);
  4624. }
  4625. /**
  4626. * Compute the transpose of the matrix
  4627. * @returns the new transposed matrix
  4628. */
  4629. public transpose(): Matrix {
  4630. return Matrix.Transpose(this);
  4631. }
  4632. /**
  4633. * Compute the transpose of the matrix and store it in a given matrix
  4634. * @param result defines the target matrix
  4635. * @returns the current matrix
  4636. */
  4637. public transposeToRef(result: Matrix): Matrix {
  4638. Matrix.TransposeToRef(this, result);
  4639. return this;
  4640. }
  4641. /**
  4642. * Sets the index-th row of the current matrix with the given 4 x float values
  4643. * @param index defines the row index
  4644. * @param x defines the x component to set
  4645. * @param y defines the y component to set
  4646. * @param z defines the z component to set
  4647. * @param w defines the w component to set
  4648. * @returns the updated current matrix
  4649. */
  4650. public setRowFromFloats(index: number, x: number, y: number, z: number, w: number): Matrix {
  4651. if (index < 0 || index > 3) {
  4652. return this;
  4653. }
  4654. var i = index * 4;
  4655. this._m[i + 0] = x;
  4656. this._m[i + 1] = y;
  4657. this._m[i + 2] = z;
  4658. this._m[i + 3] = w;
  4659. this._markAsUpdated();
  4660. return this;
  4661. }
  4662. /**
  4663. * Compute a new matrix set with the current matrix values multiplied by scale (float)
  4664. * @param scale defines the scale factor
  4665. * @returns a new matrix
  4666. */
  4667. public scale(scale: number): Matrix {
  4668. var result = new Matrix();
  4669. this.scaleToRef(scale, result);
  4670. return result;
  4671. }
  4672. /**
  4673. * Scale the current matrix values by a factor to a given result matrix
  4674. * @param scale defines the scale factor
  4675. * @param result defines the matrix to store the result
  4676. * @returns the current matrix
  4677. */
  4678. public scaleToRef(scale: number, result: Matrix): Matrix {
  4679. for (var index = 0; index < 16; index++) {
  4680. result._m[index] = this._m[index] * scale;
  4681. }
  4682. result._markAsUpdated();
  4683. return this;
  4684. }
  4685. /**
  4686. * Scale the current matrix values by a factor and add the result to a given matrix
  4687. * @param scale defines the scale factor
  4688. * @param result defines the Matrix to store the result
  4689. * @returns the current matrix
  4690. */
  4691. public scaleAndAddToRef(scale: number, result: Matrix): Matrix {
  4692. for (var index = 0; index < 16; index++) {
  4693. result._m[index] += this._m[index] * scale;
  4694. }
  4695. result._markAsUpdated();
  4696. return this;
  4697. }
  4698. /**
  4699. * Writes to the given matrix a normal matrix, computed from this one (using values from identity matrix for fourth row and column).
  4700. * @param ref matrix to store the result
  4701. */
  4702. public toNormalMatrix(ref: Matrix): void {
  4703. const tmp = MathTmp.Matrix[0];
  4704. this.invertToRef(tmp);
  4705. tmp.transposeToRef(ref);
  4706. var m = ref._m;
  4707. Matrix.FromValuesToRef(
  4708. m[0], m[1], m[2], 0.0,
  4709. m[4], m[5], m[6], 0.0,
  4710. m[8], m[9], m[10], 0.0,
  4711. 0.0, 0.0, 0.0, 1.0,
  4712. ref
  4713. );
  4714. }
  4715. /**
  4716. * Gets only rotation part of the current matrix
  4717. * @returns a new matrix sets to the extracted rotation matrix from the current one
  4718. */
  4719. public getRotationMatrix(): Matrix {
  4720. var result = new Matrix();
  4721. this.getRotationMatrixToRef(result);
  4722. return result;
  4723. }
  4724. /**
  4725. * Extracts the rotation matrix from the current one and sets it as the given "result"
  4726. * @param result defines the target matrix to store data to
  4727. * @returns the current matrix
  4728. */
  4729. public getRotationMatrixToRef(result: Matrix): Matrix {
  4730. const scale = MathTmp.Vector3[0];
  4731. if (!this.decompose(scale)) {
  4732. Matrix.IdentityToRef(result);
  4733. return this;
  4734. }
  4735. const m = this._m;
  4736. const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
  4737. Matrix.FromValuesToRef(
  4738. m[0] * sx, m[1] * sx, m[2] * sx, 0.0,
  4739. m[4] * sy, m[5] * sy, m[6] * sy, 0.0,
  4740. m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
  4741. 0.0, 0.0, 0.0, 1.0,
  4742. result
  4743. );
  4744. return this;
  4745. }
  4746. /**
  4747. * Toggles model matrix from being right handed to left handed in place and vice versa
  4748. */
  4749. public toggleModelMatrixHandInPlace() {
  4750. const m = this._m;
  4751. m[2] *= -1;
  4752. m[6] *= -1;
  4753. m[8] *= -1;
  4754. m[9] *= -1;
  4755. m[14] *= -1;
  4756. this._markAsUpdated();
  4757. }
  4758. /**
  4759. * Toggles projection matrix from being right handed to left handed in place and vice versa
  4760. */
  4761. public toggleProjectionMatrixHandInPlace() {
  4762. var m = this._m;
  4763. m[8] *= -1;
  4764. m[9] *= -1;
  4765. m[10] *= -1;
  4766. m[11] *= -1;
  4767. this._markAsUpdated();
  4768. }
  4769. // Statics
  4770. /**
  4771. * Creates a matrix from an array
  4772. * @param array defines the source array
  4773. * @param offset defines an offset in the source array
  4774. * @returns a new Matrix set from the starting index of the given array
  4775. */
  4776. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Matrix {
  4777. var result = new Matrix();
  4778. Matrix.FromArrayToRef(array, offset, result);
  4779. return result;
  4780. }
  4781. /**
  4782. * Copy the content of an array into a given matrix
  4783. * @param array defines the source array
  4784. * @param offset defines an offset in the source array
  4785. * @param result defines the target matrix
  4786. */
  4787. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Matrix) {
  4788. for (var index = 0; index < 16; index++) {
  4789. result._m[index] = array[index + offset];
  4790. }
  4791. result._markAsUpdated();
  4792. }
  4793. /**
  4794. * Stores an array into a matrix after having multiplied each component by a given factor
  4795. * @param array defines the source array
  4796. * @param offset defines the offset in the source array
  4797. * @param scale defines the scaling factor
  4798. * @param result defines the target matrix
  4799. */
  4800. public static FromFloat32ArrayToRefScaled(array: DeepImmutable<Float32Array>, offset: number, scale: number, result: Matrix) {
  4801. for (var index = 0; index < 16; index++) {
  4802. result._m[index] = array[index + offset] * scale;
  4803. }
  4804. result._markAsUpdated();
  4805. }
  4806. /**
  4807. * Gets an identity matrix that must not be updated
  4808. */
  4809. public static get IdentityReadOnly(): DeepImmutable<Matrix> {
  4810. return Matrix._identityReadOnly;
  4811. }
  4812. /**
  4813. * Stores a list of values (16) inside a given matrix
  4814. * @param initialM11 defines 1st value of 1st row
  4815. * @param initialM12 defines 2nd value of 1st row
  4816. * @param initialM13 defines 3rd value of 1st row
  4817. * @param initialM14 defines 4th value of 1st row
  4818. * @param initialM21 defines 1st value of 2nd row
  4819. * @param initialM22 defines 2nd value of 2nd row
  4820. * @param initialM23 defines 3rd value of 2nd row
  4821. * @param initialM24 defines 4th value of 2nd row
  4822. * @param initialM31 defines 1st value of 3rd row
  4823. * @param initialM32 defines 2nd value of 3rd row
  4824. * @param initialM33 defines 3rd value of 3rd row
  4825. * @param initialM34 defines 4th value of 3rd row
  4826. * @param initialM41 defines 1st value of 4th row
  4827. * @param initialM42 defines 2nd value of 4th row
  4828. * @param initialM43 defines 3rd value of 4th row
  4829. * @param initialM44 defines 4th value of 4th row
  4830. * @param result defines the target matrix
  4831. */
  4832. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  4833. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  4834. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  4835. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  4836. const m = result._m;
  4837. m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
  4838. m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
  4839. m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
  4840. m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
  4841. result._markAsUpdated();
  4842. }
  4843. /**
  4844. * Creates new matrix from a list of values (16)
  4845. * @param initialM11 defines 1st value of 1st row
  4846. * @param initialM12 defines 2nd value of 1st row
  4847. * @param initialM13 defines 3rd value of 1st row
  4848. * @param initialM14 defines 4th value of 1st row
  4849. * @param initialM21 defines 1st value of 2nd row
  4850. * @param initialM22 defines 2nd value of 2nd row
  4851. * @param initialM23 defines 3rd value of 2nd row
  4852. * @param initialM24 defines 4th value of 2nd row
  4853. * @param initialM31 defines 1st value of 3rd row
  4854. * @param initialM32 defines 2nd value of 3rd row
  4855. * @param initialM33 defines 3rd value of 3rd row
  4856. * @param initialM34 defines 4th value of 3rd row
  4857. * @param initialM41 defines 1st value of 4th row
  4858. * @param initialM42 defines 2nd value of 4th row
  4859. * @param initialM43 defines 3rd value of 4th row
  4860. * @param initialM44 defines 4th value of 4th row
  4861. * @returns the new matrix
  4862. */
  4863. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  4864. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  4865. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  4866. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  4867. var result = new Matrix();
  4868. const m = result._m;
  4869. m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
  4870. m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
  4871. m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
  4872. m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
  4873. result._markAsUpdated();
  4874. return result;
  4875. }
  4876. /**
  4877. * Creates a new matrix composed by merging scale (vector3), rotation (quaternion) and translation (vector3)
  4878. * @param scale defines the scale vector3
  4879. * @param rotation defines the rotation quaternion
  4880. * @param translation defines the translation vector3
  4881. * @returns a new matrix
  4882. */
  4883. public static Compose(scale: DeepImmutable<Vector3>, rotation: DeepImmutable<Quaternion>, translation: DeepImmutable<Vector3>): Matrix {
  4884. var result = new Matrix();
  4885. Matrix.ComposeToRef(scale, rotation, translation, result);
  4886. return result;
  4887. }
  4888. /**
  4889. * Sets a matrix to a value composed by merging scale (vector3), rotation (quaternion) and translation (vector3)
  4890. * @param scale defines the scale vector3
  4891. * @param rotation defines the rotation quaternion
  4892. * @param translation defines the translation vector3
  4893. * @param result defines the target matrix
  4894. */
  4895. public static ComposeToRef(scale: DeepImmutable<Vector3>, rotation: DeepImmutable<Quaternion>, translation: DeepImmutable<Vector3>, result: Matrix): void {
  4896. let m = result._m;
  4897. var x = rotation.x, y = rotation.y, z = rotation.z, w = rotation.w;
  4898. var x2 = x + x, y2 = y + y, z2 = z + z;
  4899. var xx = x * x2, xy = x * y2, xz = x * z2;
  4900. var yy = y * y2, yz = y * z2, zz = z * z2;
  4901. var wx = w * x2, wy = w * y2, wz = w * z2;
  4902. var sx = scale.x, sy = scale.y, sz = scale.z;
  4903. m[0] = (1 - (yy + zz)) * sx;
  4904. m[1] = (xy + wz) * sx;
  4905. m[2] = (xz - wy) * sx;
  4906. m[3] = 0;
  4907. m[4] = (xy - wz) * sy;
  4908. m[5] = (1 - (xx + zz)) * sy;
  4909. m[6] = (yz + wx) * sy;
  4910. m[7] = 0;
  4911. m[8] = (xz + wy) * sz;
  4912. m[9] = (yz - wx) * sz;
  4913. m[10] = (1 - (xx + yy)) * sz;
  4914. m[11] = 0;
  4915. m[12] = translation.x;
  4916. m[13] = translation.y;
  4917. m[14] = translation.z;
  4918. m[15] = 1;
  4919. result._markAsUpdated();
  4920. }
  4921. /**
  4922. * Creates a new identity matrix
  4923. * @returns a new identity matrix
  4924. */
  4925. public static Identity(): Matrix {
  4926. const identity = Matrix.FromValues(
  4927. 1.0, 0.0, 0.0, 0.0,
  4928. 0.0, 1.0, 0.0, 0.0,
  4929. 0.0, 0.0, 1.0, 0.0,
  4930. 0.0, 0.0, 0.0, 1.0);
  4931. identity._updateIdentityStatus(true);
  4932. return identity;
  4933. }
  4934. /**
  4935. * Creates a new identity matrix and stores the result in a given matrix
  4936. * @param result defines the target matrix
  4937. */
  4938. public static IdentityToRef(result: Matrix): void {
  4939. Matrix.FromValuesToRef(
  4940. 1.0, 0.0, 0.0, 0.0,
  4941. 0.0, 1.0, 0.0, 0.0,
  4942. 0.0, 0.0, 1.0, 0.0,
  4943. 0.0, 0.0, 0.0, 1.0,
  4944. result
  4945. );
  4946. result._updateIdentityStatus(true);
  4947. }
  4948. /**
  4949. * Creates a new zero matrix
  4950. * @returns a new zero matrix
  4951. */
  4952. public static Zero(): Matrix {
  4953. const zero = Matrix.FromValues(
  4954. 0.0, 0.0, 0.0, 0.0,
  4955. 0.0, 0.0, 0.0, 0.0,
  4956. 0.0, 0.0, 0.0, 0.0,
  4957. 0.0, 0.0, 0.0, 0.0);
  4958. zero._updateIdentityStatus(false);
  4959. return zero;
  4960. }
  4961. /**
  4962. * Creates a new rotation matrix for "angle" radians around the X axis
  4963. * @param angle defines the angle (in radians) to use
  4964. * @return the new matrix
  4965. */
  4966. public static RotationX(angle: number): Matrix {
  4967. var result = new Matrix();
  4968. Matrix.RotationXToRef(angle, result);
  4969. return result;
  4970. }
  4971. /**
  4972. * Creates a new matrix as the invert of a given matrix
  4973. * @param source defines the source matrix
  4974. * @returns the new matrix
  4975. */
  4976. public static Invert(source: DeepImmutable<Matrix>): Matrix {
  4977. var result = new Matrix();
  4978. source.invertToRef(result);
  4979. return result;
  4980. }
  4981. /**
  4982. * Creates a new rotation matrix for "angle" radians around the X axis and stores it in a given matrix
  4983. * @param angle defines the angle (in radians) to use
  4984. * @param result defines the target matrix
  4985. */
  4986. public static RotationXToRef(angle: number, result: Matrix): void {
  4987. var s = Math.sin(angle);
  4988. var c = Math.cos(angle);
  4989. Matrix.FromValuesToRef(
  4990. 1.0, 0.0, 0.0, 0.0,
  4991. 0.0, c, s, 0.0,
  4992. 0.0, -s, c, 0.0,
  4993. 0.0, 0.0, 0.0, 1.0,
  4994. result
  4995. );
  4996. result._updateIdentityStatus(c === 1 && s === 0);
  4997. }
  4998. /**
  4999. * Creates a new rotation matrix for "angle" radians around the Y axis
  5000. * @param angle defines the angle (in radians) to use
  5001. * @return the new matrix
  5002. */
  5003. public static RotationY(angle: number): Matrix {
  5004. var result = new Matrix();
  5005. Matrix.RotationYToRef(angle, result);
  5006. return result;
  5007. }
  5008. /**
  5009. * Creates a new rotation matrix for "angle" radians around the Y axis and stores it in a given matrix
  5010. * @param angle defines the angle (in radians) to use
  5011. * @param result defines the target matrix
  5012. */
  5013. public static RotationYToRef(angle: number, result: Matrix): void {
  5014. var s = Math.sin(angle);
  5015. var c = Math.cos(angle);
  5016. Matrix.FromValuesToRef(
  5017. c, 0.0, -s, 0.0,
  5018. 0.0, 1.0, 0.0, 0.0,
  5019. s, 0.0, c, 0.0,
  5020. 0.0, 0.0, 0.0, 1.0,
  5021. result
  5022. );
  5023. result._updateIdentityStatus(c === 1 && s === 0);
  5024. }
  5025. /**
  5026. * Creates a new rotation matrix for "angle" radians around the Z axis
  5027. * @param angle defines the angle (in radians) to use
  5028. * @return the new matrix
  5029. */
  5030. public static RotationZ(angle: number): Matrix {
  5031. var result = new Matrix();
  5032. Matrix.RotationZToRef(angle, result);
  5033. return result;
  5034. }
  5035. /**
  5036. * Creates a new rotation matrix for "angle" radians around the Z axis and stores it in a given matrix
  5037. * @param angle defines the angle (in radians) to use
  5038. * @param result defines the target matrix
  5039. */
  5040. public static RotationZToRef(angle: number, result: Matrix): void {
  5041. var s = Math.sin(angle);
  5042. var c = Math.cos(angle);
  5043. Matrix.FromValuesToRef(
  5044. c, s, 0.0, 0.0,
  5045. -s, c, 0.0, 0.0,
  5046. 0.0, 0.0, 1.0, 0.0,
  5047. 0.0, 0.0, 0.0, 1.0,
  5048. result
  5049. );
  5050. result._updateIdentityStatus(c === 1 && s === 0);
  5051. }
  5052. /**
  5053. * Creates a new rotation matrix for "angle" radians around the given axis
  5054. * @param axis defines the axis to use
  5055. * @param angle defines the angle (in radians) to use
  5056. * @return the new matrix
  5057. */
  5058. public static RotationAxis(axis: DeepImmutable<Vector3>, angle: number): Matrix {
  5059. var result = new Matrix();
  5060. Matrix.RotationAxisToRef(axis, angle, result);
  5061. return result;
  5062. }
  5063. /**
  5064. * Creates a new rotation matrix for "angle" radians around the given axis and stores it in a given matrix
  5065. * @param axis defines the axis to use
  5066. * @param angle defines the angle (in radians) to use
  5067. * @param result defines the target matrix
  5068. */
  5069. public static RotationAxisToRef(axis: DeepImmutable<Vector3>, angle: number, result: Matrix): void {
  5070. var s = Math.sin(-angle);
  5071. var c = Math.cos(-angle);
  5072. var c1 = 1 - c;
  5073. axis.normalize();
  5074. const m = result._m;
  5075. m[0] = (axis.x * axis.x) * c1 + c;
  5076. m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  5077. m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  5078. m[3] = 0.0;
  5079. m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  5080. m[5] = (axis.y * axis.y) * c1 + c;
  5081. m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  5082. m[7] = 0.0;
  5083. m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  5084. m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  5085. m[10] = (axis.z * axis.z) * c1 + c;
  5086. m[11] = 0.0;
  5087. m[12] = 0.0;
  5088. m[13] = 0.0;
  5089. m[14] = 0.0;
  5090. m[15] = 1.0;
  5091. result._markAsUpdated();
  5092. }
  5093. /**
  5094. * Takes normalised vectors and returns a rotation matrix to align "from" with "to".
  5095. * Taken from http://www.iquilezles.org/www/articles/noacos/noacos.htm
  5096. * @param from defines the vector to align
  5097. * @param to defines the vector to align to
  5098. * @param result defines the target matrix
  5099. */
  5100. public static RotationAlignToRef(from: DeepImmutable<Vector3>, to: DeepImmutable<Vector3>, result: Matrix): void {
  5101. const v = Vector3.Cross(to, from);
  5102. const c = Vector3.Dot(to, from);
  5103. const k = 1 / (1 + c);
  5104. const m = result._m;
  5105. 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;
  5106. 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;
  5107. 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;
  5108. m[12] = 0; m[13] = 0; m[14] = 0; m[15] = 1;
  5109. result._markAsUpdated();
  5110. }
  5111. /**
  5112. * Creates a rotation matrix
  5113. * @param yaw defines the yaw angle in radians (Y axis)
  5114. * @param pitch defines the pitch angle in radians (X axis)
  5115. * @param roll defines the roll angle in radians (X axis)
  5116. * @returns the new rotation matrix
  5117. */
  5118. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  5119. var result = new Matrix();
  5120. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  5121. return result;
  5122. }
  5123. /**
  5124. * Creates a rotation matrix and stores it in a given matrix
  5125. * @param yaw defines the yaw angle in radians (Y axis)
  5126. * @param pitch defines the pitch angle in radians (X axis)
  5127. * @param roll defines the roll angle in radians (X axis)
  5128. * @param result defines the target matrix
  5129. */
  5130. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  5131. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, MathTmp.Quaternion[0]);
  5132. MathTmp.Quaternion[0].toRotationMatrix(result);
  5133. }
  5134. /**
  5135. * Creates a scaling matrix
  5136. * @param x defines the scale factor on X axis
  5137. * @param y defines the scale factor on Y axis
  5138. * @param z defines the scale factor on Z axis
  5139. * @returns the new matrix
  5140. */
  5141. public static Scaling(x: number, y: number, z: number): Matrix {
  5142. var result = new Matrix();
  5143. Matrix.ScalingToRef(x, y, z, result);
  5144. return result;
  5145. }
  5146. /**
  5147. * Creates a scaling matrix and stores it in a given matrix
  5148. * @param x defines the scale factor on X axis
  5149. * @param y defines the scale factor on Y axis
  5150. * @param z defines the scale factor on Z axis
  5151. * @param result defines the target matrix
  5152. */
  5153. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  5154. Matrix.FromValuesToRef(
  5155. x, 0.0, 0.0, 0.0,
  5156. 0.0, y, 0.0, 0.0,
  5157. 0.0, 0.0, z, 0.0,
  5158. 0.0, 0.0, 0.0, 1.0,
  5159. result
  5160. );
  5161. result._updateIdentityStatus(x === 1 && y === 1 && z === 1);
  5162. }
  5163. /**
  5164. * Creates a translation matrix
  5165. * @param x defines the translation on X axis
  5166. * @param y defines the translation on Y axis
  5167. * @param z defines the translationon Z axis
  5168. * @returns the new matrix
  5169. */
  5170. public static Translation(x: number, y: number, z: number): Matrix {
  5171. var result = new Matrix();
  5172. Matrix.TranslationToRef(x, y, z, result);
  5173. return result;
  5174. }
  5175. /**
  5176. * Creates a translation matrix and stores it in a given matrix
  5177. * @param x defines the translation on X axis
  5178. * @param y defines the translation on Y axis
  5179. * @param z defines the translationon Z axis
  5180. * @param result defines the target matrix
  5181. */
  5182. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  5183. Matrix.FromValuesToRef(
  5184. 1.0, 0.0, 0.0, 0.0,
  5185. 0.0, 1.0, 0.0, 0.0,
  5186. 0.0, 0.0, 1.0, 0.0,
  5187. x, y, z, 1.0,
  5188. result
  5189. );
  5190. result._updateIdentityStatus(x === 0 && y === 0 && z === 0);
  5191. }
  5192. /**
  5193. * Returns a new Matrix whose values are the interpolated values for "gradient" (float) between the ones of the matrices "startValue" and "endValue".
  5194. * @param startValue defines the start value
  5195. * @param endValue defines the end value
  5196. * @param gradient defines the gradient factor
  5197. * @returns the new matrix
  5198. */
  5199. public static Lerp(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number): Matrix {
  5200. var result = new Matrix();
  5201. Matrix.LerpToRef(startValue, endValue, gradient, result);
  5202. return result;
  5203. }
  5204. /**
  5205. * Set the given matrix "result" as the interpolated values for "gradient" (float) between the ones of the matrices "startValue" and "endValue".
  5206. * @param startValue defines the start value
  5207. * @param endValue defines the end value
  5208. * @param gradient defines the gradient factor
  5209. * @param result defines the Matrix object where to store data
  5210. */
  5211. public static LerpToRef(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number, result: Matrix): void {
  5212. const resultM = result._m;
  5213. const startM = startValue.m;
  5214. const endM = endValue.m;
  5215. for (var index = 0; index < 16; index++) {
  5216. resultM[index] = startM[index] * (1.0 - gradient) + endM[index] * gradient;
  5217. }
  5218. result._markAsUpdated();
  5219. }
  5220. /**
  5221. * Builds a new matrix whose values are computed by:
  5222. * * decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices
  5223. * * interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end
  5224. * * recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices
  5225. * @param startValue defines the first matrix
  5226. * @param endValue defines the second matrix
  5227. * @param gradient defines the gradient between the two matrices
  5228. * @returns the new matrix
  5229. */
  5230. public static DecomposeLerp(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number): Matrix {
  5231. var result = new Matrix();
  5232. Matrix.DecomposeLerpToRef(startValue, endValue, gradient, result);
  5233. return result;
  5234. }
  5235. /**
  5236. * Update a matrix to values which are computed by:
  5237. * * decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices
  5238. * * interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end
  5239. * * recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices
  5240. * @param startValue defines the first matrix
  5241. * @param endValue defines the second matrix
  5242. * @param gradient defines the gradient between the two matrices
  5243. * @param result defines the target matrix
  5244. */
  5245. public static DecomposeLerpToRef(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number, result: Matrix) {
  5246. var startScale = MathTmp.Vector3[0];
  5247. var startRotation = MathTmp.Quaternion[0];
  5248. var startTranslation = MathTmp.Vector3[1];
  5249. startValue.decompose(startScale, startRotation, startTranslation);
  5250. var endScale = MathTmp.Vector3[2];
  5251. var endRotation = MathTmp.Quaternion[1];
  5252. var endTranslation = MathTmp.Vector3[3];
  5253. endValue.decompose(endScale, endRotation, endTranslation);
  5254. var resultScale = MathTmp.Vector3[4];
  5255. Vector3.LerpToRef(startScale, endScale, gradient, resultScale);
  5256. var resultRotation = MathTmp.Quaternion[2];
  5257. Quaternion.SlerpToRef(startRotation, endRotation, gradient, resultRotation);
  5258. var resultTranslation = MathTmp.Vector3[5];
  5259. Vector3.LerpToRef(startTranslation, endTranslation, gradient, resultTranslation);
  5260. Matrix.ComposeToRef(resultScale, resultRotation, resultTranslation, result);
  5261. }
  5262. /**
  5263. * 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"
  5264. * This function works in left handed mode
  5265. * @param eye defines the final position of the entity
  5266. * @param target defines where the entity should look at
  5267. * @param up defines the up vector for the entity
  5268. * @returns the new matrix
  5269. */
  5270. public static LookAtLH(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>): Matrix {
  5271. var result = new Matrix();
  5272. Matrix.LookAtLHToRef(eye, target, up, result);
  5273. return result;
  5274. }
  5275. /**
  5276. * 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".
  5277. * This function works in left handed mode
  5278. * @param eye defines the final position of the entity
  5279. * @param target defines where the entity should look at
  5280. * @param up defines the up vector for the entity
  5281. * @param result defines the target matrix
  5282. */
  5283. public static LookAtLHToRef(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: Matrix): void {
  5284. const xAxis = MathTmp.Vector3[0];
  5285. const yAxis = MathTmp.Vector3[1];
  5286. const zAxis = MathTmp.Vector3[2];
  5287. // Z axis
  5288. target.subtractToRef(eye, zAxis);
  5289. zAxis.normalize();
  5290. // X axis
  5291. Vector3.CrossToRef(up, zAxis, xAxis);
  5292. const xSquareLength = xAxis.lengthSquared();
  5293. if (xSquareLength === 0) {
  5294. xAxis.x = 1.0;
  5295. } else {
  5296. xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
  5297. }
  5298. // Y axis
  5299. Vector3.CrossToRef(zAxis, xAxis, yAxis);
  5300. yAxis.normalize();
  5301. // Eye angles
  5302. var ex = -Vector3.Dot(xAxis, eye);
  5303. var ey = -Vector3.Dot(yAxis, eye);
  5304. var ez = -Vector3.Dot(zAxis, eye);
  5305. Matrix.FromValuesToRef(
  5306. xAxis.x, yAxis.x, zAxis.x, 0.0,
  5307. xAxis.y, yAxis.y, zAxis.y, 0.0,
  5308. xAxis.z, yAxis.z, zAxis.z, 0.0,
  5309. ex, ey, ez, 1.0,
  5310. result
  5311. );
  5312. }
  5313. /**
  5314. * 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"
  5315. * This function works in right handed mode
  5316. * @param eye defines the final position of the entity
  5317. * @param target defines where the entity should look at
  5318. * @param up defines the up vector for the entity
  5319. * @returns the new matrix
  5320. */
  5321. public static LookAtRH(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>): Matrix {
  5322. var result = new Matrix();
  5323. Matrix.LookAtRHToRef(eye, target, up, result);
  5324. return result;
  5325. }
  5326. /**
  5327. * 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".
  5328. * This function works in right handed mode
  5329. * @param eye defines the final position of the entity
  5330. * @param target defines where the entity should look at
  5331. * @param up defines the up vector for the entity
  5332. * @param result defines the target matrix
  5333. */
  5334. public static LookAtRHToRef(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: Matrix): void {
  5335. const xAxis = MathTmp.Vector3[0];
  5336. const yAxis = MathTmp.Vector3[1];
  5337. const zAxis = MathTmp.Vector3[2];
  5338. // Z axis
  5339. eye.subtractToRef(target, zAxis);
  5340. zAxis.normalize();
  5341. // X axis
  5342. Vector3.CrossToRef(up, zAxis, xAxis);
  5343. const xSquareLength = xAxis.lengthSquared();
  5344. if (xSquareLength === 0) {
  5345. xAxis.x = 1.0;
  5346. } else {
  5347. xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
  5348. }
  5349. // Y axis
  5350. Vector3.CrossToRef(zAxis, xAxis, yAxis);
  5351. yAxis.normalize();
  5352. // Eye angles
  5353. var ex = -Vector3.Dot(xAxis, eye);
  5354. var ey = -Vector3.Dot(yAxis, eye);
  5355. var ez = -Vector3.Dot(zAxis, eye);
  5356. Matrix.FromValuesToRef(
  5357. xAxis.x, yAxis.x, zAxis.x, 0.0,
  5358. xAxis.y, yAxis.y, zAxis.y, 0.0,
  5359. xAxis.z, yAxis.z, zAxis.z, 0.0,
  5360. ex, ey, ez, 1.0,
  5361. result
  5362. );
  5363. }
  5364. /**
  5365. * Create a left-handed orthographic projection matrix
  5366. * @param width defines the viewport width
  5367. * @param height defines the viewport height
  5368. * @param znear defines the near clip plane
  5369. * @param zfar defines the far clip plane
  5370. * @returns a new matrix as a left-handed orthographic projection matrix
  5371. */
  5372. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  5373. var matrix = new Matrix();
  5374. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  5375. return matrix;
  5376. }
  5377. /**
  5378. * Store a left-handed orthographic projection to a given matrix
  5379. * @param width defines the viewport width
  5380. * @param height defines the viewport height
  5381. * @param znear defines the near clip plane
  5382. * @param zfar defines the far clip plane
  5383. * @param result defines the target matrix
  5384. */
  5385. public static OrthoLHToRef(width: number, height: number, znear: number, zfar: number, result: Matrix): void {
  5386. let n = znear;
  5387. let f = zfar;
  5388. let a = 2.0 / width;
  5389. let b = 2.0 / height;
  5390. let c = 2.0 / (f - n);
  5391. let d = -(f + n) / (f - n);
  5392. Matrix.FromValuesToRef(
  5393. a, 0.0, 0.0, 0.0,
  5394. 0.0, b, 0.0, 0.0,
  5395. 0.0, 0.0, c, 0.0,
  5396. 0.0, 0.0, d, 1.0,
  5397. result
  5398. );
  5399. result._updateIdentityStatus(a === 1 && b === 1 && c === 1 && d === 0);
  5400. }
  5401. /**
  5402. * Create a left-handed orthographic projection matrix
  5403. * @param left defines the viewport left coordinate
  5404. * @param right defines the viewport right coordinate
  5405. * @param bottom defines the viewport bottom coordinate
  5406. * @param top defines the viewport top coordinate
  5407. * @param znear defines the near clip plane
  5408. * @param zfar defines the far clip plane
  5409. * @returns a new matrix as a left-handed orthographic projection matrix
  5410. */
  5411. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  5412. var matrix = new Matrix();
  5413. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  5414. return matrix;
  5415. }
  5416. /**
  5417. * Stores a left-handed orthographic projection into a given matrix
  5418. * @param left defines the viewport left coordinate
  5419. * @param right defines the viewport right coordinate
  5420. * @param bottom defines the viewport bottom coordinate
  5421. * @param top defines the viewport top coordinate
  5422. * @param znear defines the near clip plane
  5423. * @param zfar defines the far clip plane
  5424. * @param result defines the target matrix
  5425. */
  5426. public static OrthoOffCenterLHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  5427. let n = znear;
  5428. let f = zfar;
  5429. let a = 2.0 / (right - left);
  5430. let b = 2.0 / (top - bottom);
  5431. let c = 2.0 / (f - n);
  5432. let d = -(f + n) / (f - n);
  5433. let i0 = (left + right) / (left - right);
  5434. let i1 = (top + bottom) / (bottom - top);
  5435. Matrix.FromValuesToRef(
  5436. a, 0.0, 0.0, 0.0,
  5437. 0.0, b, 0.0, 0.0,
  5438. 0.0, 0.0, c, 0.0,
  5439. i0, i1, d, 1.0,
  5440. result
  5441. );
  5442. result._markAsUpdated();
  5443. }
  5444. /**
  5445. * Creates a right-handed orthographic projection matrix
  5446. * @param left defines the viewport left coordinate
  5447. * @param right defines the viewport right coordinate
  5448. * @param bottom defines the viewport bottom coordinate
  5449. * @param top defines the viewport top coordinate
  5450. * @param znear defines the near clip plane
  5451. * @param zfar defines the far clip plane
  5452. * @returns a new matrix as a right-handed orthographic projection matrix
  5453. */
  5454. public static OrthoOffCenterRH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  5455. var matrix = new Matrix();
  5456. Matrix.OrthoOffCenterRHToRef(left, right, bottom, top, znear, zfar, matrix);
  5457. return matrix;
  5458. }
  5459. /**
  5460. * Stores a right-handed orthographic projection into a given matrix
  5461. * @param left defines the viewport left coordinate
  5462. * @param right defines the viewport right coordinate
  5463. * @param bottom defines the viewport bottom coordinate
  5464. * @param top defines the viewport top coordinate
  5465. * @param znear defines the near clip plane
  5466. * @param zfar defines the far clip plane
  5467. * @param result defines the target matrix
  5468. */
  5469. public static OrthoOffCenterRHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  5470. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, result);
  5471. result._m[10] *= -1; // No need to call _markAsUpdated as previous function already called it and let _isIdentityDirty to true
  5472. }
  5473. /**
  5474. * Creates a left-handed perspective projection matrix
  5475. * @param width defines the viewport width
  5476. * @param height defines the viewport height
  5477. * @param znear defines the near clip plane
  5478. * @param zfar defines the far clip plane
  5479. * @returns a new matrix as a left-handed perspective projection matrix
  5480. */
  5481. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  5482. var matrix = new Matrix();
  5483. let n = znear;
  5484. let f = zfar;
  5485. let a = 2.0 * n / width;
  5486. let b = 2.0 * n / height;
  5487. let c = (f + n) / (f - n);
  5488. let d = -2.0 * f * n / (f - n);
  5489. Matrix.FromValuesToRef(
  5490. a, 0.0, 0.0, 0.0,
  5491. 0.0, b, 0.0, 0.0,
  5492. 0.0, 0.0, c, 1.0,
  5493. 0.0, 0.0, d, 0.0,
  5494. matrix
  5495. );
  5496. matrix._updateIdentityStatus(false);
  5497. return matrix;
  5498. }
  5499. /**
  5500. * Creates a left-handed perspective projection matrix
  5501. * @param fov defines the horizontal field of view
  5502. * @param aspect defines the aspect ratio
  5503. * @param znear defines the near clip plane
  5504. * @param zfar defines the far clip plane
  5505. * @returns a new matrix as a left-handed perspective projection matrix
  5506. */
  5507. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  5508. var matrix = new Matrix();
  5509. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  5510. return matrix;
  5511. }
  5512. /**
  5513. * Stores a left-handed perspective projection into a given matrix
  5514. * @param fov defines the horizontal field of view
  5515. * @param aspect defines the aspect ratio
  5516. * @param znear defines the near clip plane
  5517. * @param zfar defines the far clip plane
  5518. * @param result defines the target matrix
  5519. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  5520. */
  5521. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  5522. let n = znear;
  5523. let f = zfar;
  5524. let t = 1.0 / (Math.tan(fov * 0.5));
  5525. let a = isVerticalFovFixed ? (t / aspect) : t;
  5526. let b = isVerticalFovFixed ? t : (t * aspect);
  5527. let c = (f + n) / (f - n);
  5528. let d = -2.0 * f * n / (f - n);
  5529. Matrix.FromValuesToRef(
  5530. a, 0.0, 0.0, 0.0,
  5531. 0.0, b, 0.0, 0.0,
  5532. 0.0, 0.0, c, 1.0,
  5533. 0.0, 0.0, d, 0.0,
  5534. result
  5535. );
  5536. result._updateIdentityStatus(false);
  5537. }
  5538. /**
  5539. * Creates a right-handed perspective projection matrix
  5540. * @param fov defines the horizontal field of view
  5541. * @param aspect defines the aspect ratio
  5542. * @param znear defines the near clip plane
  5543. * @param zfar defines the far clip plane
  5544. * @returns a new matrix as a right-handed perspective projection matrix
  5545. */
  5546. public static PerspectiveFovRH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  5547. var matrix = new Matrix();
  5548. Matrix.PerspectiveFovRHToRef(fov, aspect, znear, zfar, matrix);
  5549. return matrix;
  5550. }
  5551. /**
  5552. * Stores a right-handed perspective projection into a given matrix
  5553. * @param fov defines the horizontal field of view
  5554. * @param aspect defines the aspect ratio
  5555. * @param znear defines the near clip plane
  5556. * @param zfar defines the far clip plane
  5557. * @param result defines the target matrix
  5558. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  5559. */
  5560. public static PerspectiveFovRHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  5561. //alternatively this could be expressed as:
  5562. // m = PerspectiveFovLHToRef
  5563. // m[10] *= -1.0;
  5564. // m[11] *= -1.0;
  5565. let n = znear;
  5566. let f = zfar;
  5567. let t = 1.0 / (Math.tan(fov * 0.5));
  5568. let a = isVerticalFovFixed ? (t / aspect) : t;
  5569. let b = isVerticalFovFixed ? t : (t * aspect);
  5570. let c = -(f + n) / (f - n);
  5571. let d = -2 * f * n / (f - n);
  5572. Matrix.FromValuesToRef(
  5573. a, 0.0, 0.0, 0.0,
  5574. 0.0, b, 0.0, 0.0,
  5575. 0.0, 0.0, c, -1.0,
  5576. 0.0, 0.0, d, 0.0,
  5577. result
  5578. );
  5579. result._updateIdentityStatus(false);
  5580. }
  5581. /**
  5582. * Stores a perspective projection for WebVR info a given matrix
  5583. * @param fov defines the field of view
  5584. * @param znear defines the near clip plane
  5585. * @param zfar defines the far clip plane
  5586. * @param result defines the target matrix
  5587. * @param rightHanded defines if the matrix must be in right-handed mode (false by default)
  5588. */
  5589. public static PerspectiveFovWebVRToRef(fov: { upDegrees: number, downDegrees: number, leftDegrees: number, rightDegrees: number }, znear: number, zfar: number, result: Matrix, rightHanded = false): void {
  5590. var rightHandedFactor = rightHanded ? -1 : 1;
  5591. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  5592. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  5593. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  5594. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  5595. var xScale = 2.0 / (leftTan + rightTan);
  5596. var yScale = 2.0 / (upTan + downTan);
  5597. const m = result._m;
  5598. m[0] = xScale;
  5599. m[1] = m[2] = m[3] = m[4] = 0.0;
  5600. m[5] = yScale;
  5601. m[6] = m[7] = 0.0;
  5602. m[8] = ((leftTan - rightTan) * xScale * 0.5);
  5603. m[9] = -((upTan - downTan) * yScale * 0.5);
  5604. m[10] = -zfar / (znear - zfar);
  5605. m[11] = 1.0 * rightHandedFactor;
  5606. m[12] = m[13] = m[15] = 0.0;
  5607. m[14] = -(2.0 * zfar * znear) / (zfar - znear);
  5608. result._markAsUpdated();
  5609. }
  5610. /**
  5611. * Computes a complete transformation matrix
  5612. * @param viewport defines the viewport to use
  5613. * @param world defines the world matrix
  5614. * @param view defines the view matrix
  5615. * @param projection defines the projection matrix
  5616. * @param zmin defines the near clip plane
  5617. * @param zmax defines the far clip plane
  5618. * @returns the transformation matrix
  5619. */
  5620. public static GetFinalMatrix(viewport: DeepImmutable<Viewport>, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, zmin: number, zmax: number): Matrix {
  5621. var cw = viewport.width;
  5622. var ch = viewport.height;
  5623. var cx = viewport.x;
  5624. var cy = viewport.y;
  5625. var viewportMatrix = Matrix.FromValues(
  5626. cw / 2.0, 0.0, 0.0, 0.0,
  5627. 0.0, -ch / 2.0, 0.0, 0.0,
  5628. 0.0, 0.0, zmax - zmin, 0.0,
  5629. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1.0);
  5630. var matrix = MathTmp.Matrix[0];
  5631. world.multiplyToRef(view, matrix);
  5632. matrix.multiplyToRef(projection, matrix);
  5633. return matrix.multiply(viewportMatrix);
  5634. }
  5635. /**
  5636. * Extracts a 2x2 matrix from a given matrix and store the result in a Float32Array
  5637. * @param matrix defines the matrix to use
  5638. * @returns a new Float32Array array with 4 elements : the 2x2 matrix extracted from the given matrix
  5639. */
  5640. public static GetAsMatrix2x2(matrix: DeepImmutable<Matrix>): Float32Array {
  5641. const m = matrix.m;
  5642. return new Float32Array([m[0], m[1], m[4], m[5]]);
  5643. }
  5644. /**
  5645. * Extracts a 3x3 matrix from a given matrix and store the result in a Float32Array
  5646. * @param matrix defines the matrix to use
  5647. * @returns a new Float32Array array with 9 elements : the 3x3 matrix extracted from the given matrix
  5648. */
  5649. public static GetAsMatrix3x3(matrix: DeepImmutable<Matrix>): Float32Array {
  5650. const m = matrix.m;
  5651. return new Float32Array([
  5652. m[0], m[1], m[2],
  5653. m[4], m[5], m[6],
  5654. m[8], m[9], m[10]
  5655. ]);
  5656. }
  5657. /**
  5658. * Compute the transpose of a given matrix
  5659. * @param matrix defines the matrix to transpose
  5660. * @returns the new matrix
  5661. */
  5662. public static Transpose(matrix: DeepImmutable<Matrix>): Matrix {
  5663. var result = new Matrix();
  5664. Matrix.TransposeToRef(matrix, result);
  5665. return result;
  5666. }
  5667. /**
  5668. * Compute the transpose of a matrix and store it in a target matrix
  5669. * @param matrix defines the matrix to transpose
  5670. * @param result defines the target matrix
  5671. */
  5672. public static TransposeToRef(matrix: DeepImmutable<Matrix>, result: Matrix): void {
  5673. const rm = result._m;
  5674. const mm = matrix.m;
  5675. rm[0] = mm[0];
  5676. rm[1] = mm[4];
  5677. rm[2] = mm[8];
  5678. rm[3] = mm[12];
  5679. rm[4] = mm[1];
  5680. rm[5] = mm[5];
  5681. rm[6] = mm[9];
  5682. rm[7] = mm[13];
  5683. rm[8] = mm[2];
  5684. rm[9] = mm[6];
  5685. rm[10] = mm[10];
  5686. rm[11] = mm[14];
  5687. rm[12] = mm[3];
  5688. rm[13] = mm[7];
  5689. rm[14] = mm[11];
  5690. rm[15] = mm[15];
  5691. // identity-ness does not change when transposing
  5692. result._updateIdentityStatus((matrix as Matrix)._isIdentity, (matrix as Matrix)._isIdentityDirty);
  5693. }
  5694. /**
  5695. * Computes a reflection matrix from a plane
  5696. * @param plane defines the reflection plane
  5697. * @returns a new matrix
  5698. */
  5699. public static Reflection(plane: DeepImmutable<Plane>): Matrix {
  5700. var matrix = new Matrix();
  5701. Matrix.ReflectionToRef(plane, matrix);
  5702. return matrix;
  5703. }
  5704. /**
  5705. * Computes a reflection matrix from a plane
  5706. * @param plane defines the reflection plane
  5707. * @param result defines the target matrix
  5708. */
  5709. public static ReflectionToRef(plane: DeepImmutable<Plane>, result: Matrix): void {
  5710. plane.normalize();
  5711. var x = plane.normal.x;
  5712. var y = plane.normal.y;
  5713. var z = plane.normal.z;
  5714. var temp = -2 * x;
  5715. var temp2 = -2 * y;
  5716. var temp3 = -2 * z;
  5717. Matrix.FromValuesToRef(
  5718. temp * x + 1, temp2 * x, temp3 * x, 0.0,
  5719. temp * y, temp2 * y + 1, temp3 * y, 0.0,
  5720. temp * z, temp2 * z, temp3 * z + 1, 0.0,
  5721. temp * plane.d, temp2 * plane.d, temp3 * plane.d, 1.0,
  5722. result
  5723. );
  5724. }
  5725. /**
  5726. * Sets the given matrix as a rotation matrix composed from the 3 left handed axes
  5727. * @param xaxis defines the value of the 1st axis
  5728. * @param yaxis defines the value of the 2nd axis
  5729. * @param zaxis defines the value of the 3rd axis
  5730. * @param result defines the target matrix
  5731. */
  5732. public static FromXYZAxesToRef(xaxis: DeepImmutable<Vector3>, yaxis: DeepImmutable<Vector3>, zaxis: DeepImmutable<Vector3>, result: Matrix) {
  5733. Matrix.FromValuesToRef(
  5734. xaxis.x, xaxis.y, xaxis.z, 0.0,
  5735. yaxis.x, yaxis.y, yaxis.z, 0.0,
  5736. zaxis.x, zaxis.y, zaxis.z, 0.0,
  5737. 0.0, 0.0, 0.0, 1.0,
  5738. result
  5739. );
  5740. }
  5741. /**
  5742. * Creates a rotation matrix from a quaternion and stores it in a target matrix
  5743. * @param quat defines the quaternion to use
  5744. * @param result defines the target matrix
  5745. */
  5746. public static FromQuaternionToRef(quat: DeepImmutable<Quaternion>, result: Matrix) {
  5747. var xx = quat.x * quat.x;
  5748. var yy = quat.y * quat.y;
  5749. var zz = quat.z * quat.z;
  5750. var xy = quat.x * quat.y;
  5751. var zw = quat.z * quat.w;
  5752. var zx = quat.z * quat.x;
  5753. var yw = quat.y * quat.w;
  5754. var yz = quat.y * quat.z;
  5755. var xw = quat.x * quat.w;
  5756. result._m[0] = 1.0 - (2.0 * (yy + zz));
  5757. result._m[1] = 2.0 * (xy + zw);
  5758. result._m[2] = 2.0 * (zx - yw);
  5759. result._m[3] = 0.0;
  5760. result._m[4] = 2.0 * (xy - zw);
  5761. result._m[5] = 1.0 - (2.0 * (zz + xx));
  5762. result._m[6] = 2.0 * (yz + xw);
  5763. result._m[7] = 0.0;
  5764. result._m[8] = 2.0 * (zx + yw);
  5765. result._m[9] = 2.0 * (yz - xw);
  5766. result._m[10] = 1.0 - (2.0 * (yy + xx));
  5767. result._m[11] = 0.0;
  5768. result._m[12] = 0.0;
  5769. result._m[13] = 0.0;
  5770. result._m[14] = 0.0;
  5771. result._m[15] = 1.0;
  5772. result._markAsUpdated();
  5773. }
  5774. }
  5775. /**
  5776. * Represens a plane by the equation ax + by + cz + d = 0
  5777. */
  5778. export class Plane {
  5779. /**
  5780. * Normal of the plane (a,b,c)
  5781. */
  5782. public normal: Vector3;
  5783. /**
  5784. * d component of the plane
  5785. */
  5786. public d: number;
  5787. /**
  5788. * Creates a Plane object according to the given floats a, b, c, d and the plane equation : ax + by + cz + d = 0
  5789. * @param a a component of the plane
  5790. * @param b b component of the plane
  5791. * @param c c component of the plane
  5792. * @param d d component of the plane
  5793. */
  5794. constructor(a: number, b: number, c: number, d: number) {
  5795. this.normal = new Vector3(a, b, c);
  5796. this.d = d;
  5797. }
  5798. /**
  5799. * @returns the plane coordinates as a new array of 4 elements [a, b, c, d].
  5800. */
  5801. public asArray(): number[] {
  5802. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  5803. }
  5804. // Methods
  5805. /**
  5806. * @returns a new plane copied from the current Plane.
  5807. */
  5808. public clone(): Plane {
  5809. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  5810. }
  5811. /**
  5812. * @returns the string "Plane".
  5813. */
  5814. public getClassName(): string {
  5815. return "Plane";
  5816. }
  5817. /**
  5818. * @returns the Plane hash code.
  5819. */
  5820. public getHashCode(): number {
  5821. let hash = this.normal.getHashCode();
  5822. hash = (hash * 397) ^ (this.d | 0);
  5823. return hash;
  5824. }
  5825. /**
  5826. * Normalize the current Plane in place.
  5827. * @returns the updated Plane.
  5828. */
  5829. public normalize(): Plane {
  5830. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  5831. var magnitude = 0.0;
  5832. if (norm !== 0) {
  5833. magnitude = 1.0 / norm;
  5834. }
  5835. this.normal.x *= magnitude;
  5836. this.normal.y *= magnitude;
  5837. this.normal.z *= magnitude;
  5838. this.d *= magnitude;
  5839. return this;
  5840. }
  5841. /**
  5842. * Applies a transformation the plane and returns the result
  5843. * @param transformation the transformation matrix to be applied to the plane
  5844. * @returns a new Plane as the result of the transformation of the current Plane by the given matrix.
  5845. */
  5846. public transform(transformation: DeepImmutable<Matrix>): Plane {
  5847. const transposedMatrix = MathTmp.Matrix[0];
  5848. Matrix.TransposeToRef(transformation, transposedMatrix);
  5849. const m = transposedMatrix.m;
  5850. var x = this.normal.x;
  5851. var y = this.normal.y;
  5852. var z = this.normal.z;
  5853. var d = this.d;
  5854. var normalX = x * m[0] + y * m[1] + z * m[2] + d * m[3];
  5855. var normalY = x * m[4] + y * m[5] + z * m[6] + d * m[7];
  5856. var normalZ = x * m[8] + y * m[9] + z * m[10] + d * m[11];
  5857. var finalD = x * m[12] + y * m[13] + z * m[14] + d * m[15];
  5858. return new Plane(normalX, normalY, normalZ, finalD);
  5859. }
  5860. /**
  5861. * Calcualtte the dot product between the point and the plane normal
  5862. * @param point point to calculate the dot product with
  5863. * @returns the dot product (float) of the point coordinates and the plane normal.
  5864. */
  5865. public dotCoordinate(point: DeepImmutable<Vector3>): number {
  5866. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  5867. }
  5868. /**
  5869. * Updates the current Plane from the plane defined by the three given points.
  5870. * @param point1 one of the points used to contruct the plane
  5871. * @param point2 one of the points used to contruct the plane
  5872. * @param point3 one of the points used to contruct the plane
  5873. * @returns the updated Plane.
  5874. */
  5875. public copyFromPoints(point1: DeepImmutable<Vector3>, point2: DeepImmutable<Vector3>, point3: DeepImmutable<Vector3>): Plane {
  5876. var x1 = point2.x - point1.x;
  5877. var y1 = point2.y - point1.y;
  5878. var z1 = point2.z - point1.z;
  5879. var x2 = point3.x - point1.x;
  5880. var y2 = point3.y - point1.y;
  5881. var z2 = point3.z - point1.z;
  5882. var yz = (y1 * z2) - (z1 * y2);
  5883. var xz = (z1 * x2) - (x1 * z2);
  5884. var xy = (x1 * y2) - (y1 * x2);
  5885. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  5886. var invPyth;
  5887. if (pyth !== 0) {
  5888. invPyth = 1.0 / pyth;
  5889. }
  5890. else {
  5891. invPyth = 0.0;
  5892. }
  5893. this.normal.x = yz * invPyth;
  5894. this.normal.y = xz * invPyth;
  5895. this.normal.z = xy * invPyth;
  5896. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  5897. return this;
  5898. }
  5899. /**
  5900. * Checks if the plane is facing a given direction
  5901. * @param direction the direction to check if the plane is facing
  5902. * @param epsilon value the dot product is compared against (returns true if dot <= epsilon)
  5903. * @returns True is the vector "direction" is the same side than the plane normal.
  5904. */
  5905. public isFrontFacingTo(direction: DeepImmutable<Vector3>, epsilon: number): boolean {
  5906. var dot = Vector3.Dot(this.normal, direction);
  5907. return (dot <= epsilon);
  5908. }
  5909. /**
  5910. * Calculates the distance to a point
  5911. * @param point point to calculate distance to
  5912. * @returns the signed distance (float) from the given point to the Plane.
  5913. */
  5914. public signedDistanceTo(point: DeepImmutable<Vector3>): number {
  5915. return Vector3.Dot(point, this.normal) + this.d;
  5916. }
  5917. // Statics
  5918. /**
  5919. * Creates a plane from an array
  5920. * @param array the array to create a plane from
  5921. * @returns a new Plane from the given array.
  5922. */
  5923. static FromArray(array: DeepImmutable<ArrayLike<number>>): Plane {
  5924. return new Plane(array[0], array[1], array[2], array[3]);
  5925. }
  5926. /**
  5927. * Creates a plane from three points
  5928. * @param point1 point used to create the plane
  5929. * @param point2 point used to create the plane
  5930. * @param point3 point used to create the plane
  5931. * @returns a new Plane defined by the three given points.
  5932. */
  5933. static FromPoints(point1: DeepImmutable<Vector3>, point2: DeepImmutable<Vector3>, point3: DeepImmutable<Vector3>): Plane {
  5934. var result = new Plane(0.0, 0.0, 0.0, 0.0);
  5935. result.copyFromPoints(point1, point2, point3);
  5936. return result;
  5937. }
  5938. /**
  5939. * Creates a plane from an origin point and a normal
  5940. * @param origin origin of the plane to be constructed
  5941. * @param normal normal of the plane to be constructed
  5942. * @returns a new Plane the normal vector to this plane at the given origin point.
  5943. * Note : the vector "normal" is updated because normalized.
  5944. */
  5945. static FromPositionAndNormal(origin: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>): Plane {
  5946. var result = new Plane(0.0, 0.0, 0.0, 0.0);
  5947. normal.normalize();
  5948. result.normal = normal;
  5949. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  5950. return result;
  5951. }
  5952. /**
  5953. * Calculates the distance from a plane and a point
  5954. * @param origin origin of the plane to be constructed
  5955. * @param normal normal of the plane to be constructed
  5956. * @param point point to calculate distance to
  5957. * @returns the signed distance between the plane defined by the normal vector at the "origin"" point and the given other point.
  5958. */
  5959. static SignedDistanceToPlaneFromPositionAndNormal(origin: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>, point: DeepImmutable<Vector3>): number {
  5960. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  5961. return Vector3.Dot(point, normal) + d;
  5962. }
  5963. }
  5964. /**
  5965. * Class used to represent a viewport on screen
  5966. */
  5967. export class Viewport {
  5968. /**
  5969. * Creates a Viewport object located at (x, y) and sized (width, height)
  5970. * @param x defines viewport left coordinate
  5971. * @param y defines viewport top coordinate
  5972. * @param width defines the viewport width
  5973. * @param height defines the viewport height
  5974. */
  5975. constructor(
  5976. /** viewport left coordinate */
  5977. public x: number,
  5978. /** viewport top coordinate */
  5979. public y: number,
  5980. /**viewport width */
  5981. public width: number,
  5982. /** viewport height */
  5983. public height: number) {
  5984. }
  5985. /**
  5986. * Creates a new viewport using absolute sizing (from 0-> width, 0-> height instead of 0->1)
  5987. * @param renderWidth defines the rendering width
  5988. * @param renderHeight defines the rendering height
  5989. * @returns a new Viewport
  5990. */
  5991. public toGlobal(renderWidth: number, renderHeight: number): Viewport {
  5992. return new Viewport(this.x * renderWidth, this.y * renderHeight, this.width * renderWidth, this.height * renderHeight);
  5993. }
  5994. /**
  5995. * Stores absolute viewport value into a target viewport (from 0-> width, 0-> height instead of 0->1)
  5996. * @param renderWidth defines the rendering width
  5997. * @param renderHeight defines the rendering height
  5998. * @param ref defines the target viewport
  5999. * @returns the current viewport
  6000. */
  6001. public toGlobalToRef(renderWidth: number, renderHeight: number, ref: Viewport): Viewport {
  6002. ref.x = this.x * renderWidth;
  6003. ref.y = this.y * renderHeight;
  6004. ref.width = this.width * renderWidth;
  6005. ref.height = this.height * renderHeight;
  6006. return this;
  6007. }
  6008. /**
  6009. * Returns a new Viewport copied from the current one
  6010. * @returns a new Viewport
  6011. */
  6012. public clone(): Viewport {
  6013. return new Viewport(this.x, this.y, this.width, this.height);
  6014. }
  6015. }
  6016. /**
  6017. * Reprasents a camera frustum
  6018. */
  6019. export class Frustum {
  6020. /**
  6021. * Gets the planes representing the frustum
  6022. * @param transform matrix to be applied to the returned planes
  6023. * @returns a new array of 6 Frustum planes computed by the given transformation matrix.
  6024. */
  6025. public static GetPlanes(transform: DeepImmutable<Matrix>): Plane[] {
  6026. var frustumPlanes = [];
  6027. for (var index = 0; index < 6; index++) {
  6028. frustumPlanes.push(new Plane(0.0, 0.0, 0.0, 0.0));
  6029. }
  6030. Frustum.GetPlanesToRef(transform, frustumPlanes);
  6031. return frustumPlanes;
  6032. }
  6033. /**
  6034. * Gets the near frustum plane transformed by the transform matrix
  6035. * @param transform transformation matrix to be applied to the resulting frustum plane
  6036. * @param frustumPlane the resuling frustum plane
  6037. */
  6038. public static GetNearPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  6039. const m = transform.m;
  6040. frustumPlane.normal.x = m[3] + m[2];
  6041. frustumPlane.normal.y = m[7] + m[6];
  6042. frustumPlane.normal.z = m[11] + m[10];
  6043. frustumPlane.d = m[15] + m[14];
  6044. frustumPlane.normalize();
  6045. }
  6046. /**
  6047. * Gets the far frustum plane transformed by the transform matrix
  6048. * @param transform transformation matrix to be applied to the resulting frustum plane
  6049. * @param frustumPlane the resuling frustum plane
  6050. */
  6051. public static GetFarPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  6052. const m = transform.m;
  6053. frustumPlane.normal.x = m[3] - m[2];
  6054. frustumPlane.normal.y = m[7] - m[6];
  6055. frustumPlane.normal.z = m[11] - m[10];
  6056. frustumPlane.d = m[15] - m[14];
  6057. frustumPlane.normalize();
  6058. }
  6059. /**
  6060. * Gets the left frustum plane transformed by the transform matrix
  6061. * @param transform transformation matrix to be applied to the resulting frustum plane
  6062. * @param frustumPlane the resuling frustum plane
  6063. */
  6064. public static GetLeftPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  6065. const m = transform.m;
  6066. frustumPlane.normal.x = m[3] + m[0];
  6067. frustumPlane.normal.y = m[7] + m[4];
  6068. frustumPlane.normal.z = m[11] + m[8];
  6069. frustumPlane.d = m[15] + m[12];
  6070. frustumPlane.normalize();
  6071. }
  6072. /**
  6073. * Gets the right frustum plane transformed by the transform matrix
  6074. * @param transform transformation matrix to be applied to the resulting frustum plane
  6075. * @param frustumPlane the resuling frustum plane
  6076. */
  6077. public static GetRightPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  6078. const m = transform.m;
  6079. frustumPlane.normal.x = m[3] - m[0];
  6080. frustumPlane.normal.y = m[7] - m[4];
  6081. frustumPlane.normal.z = m[11] - m[8];
  6082. frustumPlane.d = m[15] - m[12];
  6083. frustumPlane.normalize();
  6084. }
  6085. /**
  6086. * Gets the top frustum plane transformed by the transform matrix
  6087. * @param transform transformation matrix to be applied to the resulting frustum plane
  6088. * @param frustumPlane the resuling frustum plane
  6089. */
  6090. public static GetTopPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  6091. const m = transform.m;
  6092. frustumPlane.normal.x = m[3] - m[1];
  6093. frustumPlane.normal.y = m[7] - m[5];
  6094. frustumPlane.normal.z = m[11] - m[9];
  6095. frustumPlane.d = m[15] - m[13];
  6096. frustumPlane.normalize();
  6097. }
  6098. /**
  6099. * Gets the bottom frustum plane transformed by the transform matrix
  6100. * @param transform transformation matrix to be applied to the resulting frustum plane
  6101. * @param frustumPlane the resuling frustum plane
  6102. */
  6103. public static GetBottomPlaneToRef(transform: DeepImmutable<Matrix>, frustumPlane: Plane): void {
  6104. const m = transform.m;
  6105. frustumPlane.normal.x = m[3] + m[1];
  6106. frustumPlane.normal.y = m[7] + m[5];
  6107. frustumPlane.normal.z = m[11] + m[9];
  6108. frustumPlane.d = m[15] + m[13];
  6109. frustumPlane.normalize();
  6110. }
  6111. /**
  6112. * Sets the given array "frustumPlanes" with the 6 Frustum planes computed by the given transformation matrix.
  6113. * @param transform transformation matrix to be applied to the resulting frustum planes
  6114. * @param frustumPlanes the resuling frustum planes
  6115. */
  6116. public static GetPlanesToRef(transform: DeepImmutable<Matrix>, frustumPlanes: Plane[]): void {
  6117. // Near
  6118. Frustum.GetNearPlaneToRef(transform, frustumPlanes[0]);
  6119. // Far
  6120. Frustum.GetFarPlaneToRef(transform, frustumPlanes[1]);
  6121. // Left
  6122. Frustum.GetLeftPlaneToRef(transform, frustumPlanes[2]);
  6123. // Right
  6124. Frustum.GetRightPlaneToRef(transform, frustumPlanes[3]);
  6125. // Top
  6126. Frustum.GetTopPlaneToRef(transform, frustumPlanes[4]);
  6127. // Bottom
  6128. Frustum.GetBottomPlaneToRef(transform, frustumPlanes[5]);
  6129. }
  6130. }
  6131. /** Defines supported spaces */
  6132. export enum Space {
  6133. /** Local (object) space */
  6134. LOCAL = 0,
  6135. /** World space */
  6136. WORLD = 1,
  6137. /** Bone space */
  6138. BONE = 2
  6139. }
  6140. /** Defines the 3 main axes */
  6141. export class Axis {
  6142. /** X axis */
  6143. public static X: Vector3 = new Vector3(1.0, 0.0, 0.0);
  6144. /** Y axis */
  6145. public static Y: Vector3 = new Vector3(0.0, 1.0, 0.0);
  6146. /** Z axis */
  6147. public static Z: Vector3 = new Vector3(0.0, 0.0, 1.0);
  6148. }
  6149. /** Class used to represent a Bezier curve */
  6150. export class BezierCurve {
  6151. /**
  6152. * Returns the cubic Bezier interpolated value (float) at "t" (float) from the given x1, y1, x2, y2 floats
  6153. * @param t defines the time
  6154. * @param x1 defines the left coordinate on X axis
  6155. * @param y1 defines the left coordinate on Y axis
  6156. * @param x2 defines the right coordinate on X axis
  6157. * @param y2 defines the right coordinate on Y axis
  6158. * @returns the interpolated value
  6159. */
  6160. public static Interpolate(t: number, x1: number, y1: number, x2: number, y2: number): number {
  6161. // Extract X (which is equal to time here)
  6162. var f0 = 1 - 3 * x2 + 3 * x1;
  6163. var f1 = 3 * x2 - 6 * x1;
  6164. var f2 = 3 * x1;
  6165. var refinedT = t;
  6166. for (var i = 0; i < 5; i++) {
  6167. var refinedT2 = refinedT * refinedT;
  6168. var refinedT3 = refinedT2 * refinedT;
  6169. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  6170. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  6171. refinedT -= (x - t) * slope;
  6172. refinedT = Math.min(1, Math.max(0, refinedT));
  6173. }
  6174. // Resolve cubic bezier for the given x
  6175. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 +
  6176. 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 +
  6177. Math.pow(refinedT, 3);
  6178. }
  6179. }
  6180. /**
  6181. * Defines potential orientation for back face culling
  6182. */
  6183. export enum Orientation {
  6184. /**
  6185. * Clockwise
  6186. */
  6187. CW = 0,
  6188. /** Counter clockwise */
  6189. CCW = 1
  6190. }
  6191. /**
  6192. * Defines angle representation
  6193. */
  6194. export class Angle {
  6195. private _radians: number;
  6196. /**
  6197. * Creates an Angle object of "radians" radians (float).
  6198. * @param radians the angle in radians
  6199. */
  6200. constructor(radians: number) {
  6201. this._radians = radians;
  6202. if (this._radians < 0.0) { this._radians += (2.0 * Math.PI); }
  6203. }
  6204. /**
  6205. * Get value in degrees
  6206. * @returns the Angle value in degrees (float)
  6207. */
  6208. public degrees() {
  6209. return this._radians * 180.0 / Math.PI;
  6210. }
  6211. /**
  6212. * Get value in radians
  6213. * @returns the Angle value in radians (float)
  6214. */
  6215. public radians() {
  6216. return this._radians;
  6217. }
  6218. /**
  6219. * Gets a new Angle object valued with the angle value in radians between the two given vectors
  6220. * @param a defines first vector
  6221. * @param b defines second vector
  6222. * @returns a new Angle
  6223. */
  6224. public static BetweenTwoPoints(a: DeepImmutable<Vector2>, b: DeepImmutable<Vector2>): Angle {
  6225. var delta = b.subtract(a);
  6226. var theta = Math.atan2(delta.y, delta.x);
  6227. return new Angle(theta);
  6228. }
  6229. /**
  6230. * Gets a new Angle object from the given float in radians
  6231. * @param radians defines the angle value in radians
  6232. * @returns a new Angle
  6233. */
  6234. public static FromRadians(radians: number): Angle {
  6235. return new Angle(radians);
  6236. }
  6237. /**
  6238. * Gets a new Angle object from the given float in degrees
  6239. * @param degrees defines the angle value in degrees
  6240. * @returns a new Angle
  6241. */
  6242. public static FromDegrees(degrees: number): Angle {
  6243. return new Angle(degrees * Math.PI / 180.0);
  6244. }
  6245. }
  6246. /**
  6247. * This represents an arc in a 2d space.
  6248. */
  6249. export class Arc2 {
  6250. /**
  6251. * Defines the center point of the arc.
  6252. */
  6253. public centerPoint: Vector2;
  6254. /**
  6255. * Defines the radius of the arc.
  6256. */
  6257. public radius: number;
  6258. /**
  6259. * Defines the angle of the arc (from mid point to end point).
  6260. */
  6261. public angle: Angle;
  6262. /**
  6263. * Defines the start angle of the arc (from start point to middle point).
  6264. */
  6265. public startAngle: Angle;
  6266. /**
  6267. * Defines the orientation of the arc (clock wise/counter clock wise).
  6268. */
  6269. public orientation: Orientation;
  6270. /**
  6271. * Creates an Arc object from the three given points : start, middle and end.
  6272. * @param startPoint Defines the start point of the arc
  6273. * @param midPoint Defines the midlle point of the arc
  6274. * @param endPoint Defines the end point of the arc
  6275. */
  6276. constructor(
  6277. /** Defines the start point of the arc */
  6278. public startPoint: Vector2,
  6279. /** Defines the mid point of the arc */
  6280. public midPoint: Vector2,
  6281. /** Defines the end point of the arc */
  6282. public endPoint: Vector2) {
  6283. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  6284. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  6285. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  6286. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  6287. this.centerPoint = new Vector2(
  6288. (startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det,
  6289. ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det
  6290. );
  6291. this.radius = this.centerPoint.subtract(this.startPoint).length();
  6292. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  6293. var a1 = this.startAngle.degrees();
  6294. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  6295. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  6296. // angles correction
  6297. if (a2 - a1 > +180.0) { a2 -= 360.0; }
  6298. if (a2 - a1 < -180.0) { a2 += 360.0; }
  6299. if (a3 - a2 > +180.0) { a3 -= 360.0; }
  6300. if (a3 - a2 < -180.0) { a3 += 360.0; }
  6301. this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
  6302. this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
  6303. }
  6304. }
  6305. /**
  6306. * Represents a 2D path made up of multiple 2D points
  6307. */
  6308. export class Path2 {
  6309. private _points = new Array<Vector2>();
  6310. private _length = 0.0;
  6311. /**
  6312. * If the path start and end point are the same
  6313. */
  6314. public closed = false;
  6315. /**
  6316. * Creates a Path2 object from the starting 2D coordinates x and y.
  6317. * @param x the starting points x value
  6318. * @param y the starting points y value
  6319. */
  6320. constructor(x: number, y: number) {
  6321. this._points.push(new Vector2(x, y));
  6322. }
  6323. /**
  6324. * Adds a new segment until the given coordinates (x, y) to the current Path2.
  6325. * @param x the added points x value
  6326. * @param y the added points y value
  6327. * @returns the updated Path2.
  6328. */
  6329. public addLineTo(x: number, y: number): Path2 {
  6330. if (this.closed) {
  6331. return this;
  6332. }
  6333. var newPoint = new Vector2(x, y);
  6334. var previousPoint = this._points[this._points.length - 1];
  6335. this._points.push(newPoint);
  6336. this._length += newPoint.subtract(previousPoint).length();
  6337. return this;
  6338. }
  6339. /**
  6340. * Adds _numberOfSegments_ segments according to the arc definition (middle point coordinates, end point coordinates, the arc start point being the current Path2 last point) to the current Path2.
  6341. * @param midX middle point x value
  6342. * @param midY middle point y value
  6343. * @param endX end point x value
  6344. * @param endY end point y value
  6345. * @param numberOfSegments (default: 36)
  6346. * @returns the updated Path2.
  6347. */
  6348. public addArcTo(midX: number, midY: number, endX: number, endY: number, numberOfSegments = 36): Path2 {
  6349. if (this.closed) {
  6350. return this;
  6351. }
  6352. var startPoint = this._points[this._points.length - 1];
  6353. var midPoint = new Vector2(midX, midY);
  6354. var endPoint = new Vector2(endX, endY);
  6355. var arc = new Arc2(startPoint, midPoint, endPoint);
  6356. var increment = arc.angle.radians() / numberOfSegments;
  6357. if (arc.orientation === Orientation.CW) { increment *= -1; }
  6358. var currentAngle = arc.startAngle.radians() + increment;
  6359. for (var i = 0; i < numberOfSegments; i++) {
  6360. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  6361. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  6362. this.addLineTo(x, y);
  6363. currentAngle += increment;
  6364. }
  6365. return this;
  6366. }
  6367. /**
  6368. * Closes the Path2.
  6369. * @returns the Path2.
  6370. */
  6371. public close(): Path2 {
  6372. this.closed = true;
  6373. return this;
  6374. }
  6375. /**
  6376. * Gets the sum of the distance between each sequential point in the path
  6377. * @returns the Path2 total length (float).
  6378. */
  6379. public length(): number {
  6380. var result = this._length;
  6381. if (!this.closed) {
  6382. var lastPoint = this._points[this._points.length - 1];
  6383. var firstPoint = this._points[0];
  6384. result += (firstPoint.subtract(lastPoint).length());
  6385. }
  6386. return result;
  6387. }
  6388. /**
  6389. * Gets the points which construct the path
  6390. * @returns the Path2 internal array of points.
  6391. */
  6392. public getPoints(): Vector2[] {
  6393. return this._points;
  6394. }
  6395. /**
  6396. * Retreives the point at the distance aways from the starting point
  6397. * @param normalizedLengthPosition the length along the path to retreive the point from
  6398. * @returns a new Vector2 located at a percentage of the Path2 total length on this path.
  6399. */
  6400. public getPointAtLengthPosition(normalizedLengthPosition: number): Vector2 {
  6401. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  6402. return Vector2.Zero();
  6403. }
  6404. var lengthPosition = normalizedLengthPosition * this.length();
  6405. var previousOffset = 0;
  6406. for (var i = 0; i < this._points.length; i++) {
  6407. var j = (i + 1) % this._points.length;
  6408. var a = this._points[i];
  6409. var b = this._points[j];
  6410. var bToA = b.subtract(a);
  6411. var nextOffset = (bToA.length() + previousOffset);
  6412. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  6413. var dir = bToA.normalize();
  6414. var localOffset = lengthPosition - previousOffset;
  6415. return new Vector2(
  6416. a.x + (dir.x * localOffset),
  6417. a.y + (dir.y * localOffset)
  6418. );
  6419. }
  6420. previousOffset = nextOffset;
  6421. }
  6422. return Vector2.Zero();
  6423. }
  6424. /**
  6425. * Creates a new path starting from an x and y position
  6426. * @param x starting x value
  6427. * @param y starting y value
  6428. * @returns a new Path2 starting at the coordinates (x, y).
  6429. */
  6430. public static StartingAt(x: number, y: number): Path2 {
  6431. return new Path2(x, y);
  6432. }
  6433. }
  6434. /**
  6435. * Represents a 3D path made up of multiple 3D points
  6436. */
  6437. export class Path3D {
  6438. private _curve = new Array<Vector3>();
  6439. private _distances = new Array<number>();
  6440. private _tangents = new Array<Vector3>();
  6441. private _normals = new Array<Vector3>();
  6442. private _binormals = new Array<Vector3>();
  6443. private _raw: boolean;
  6444. /**
  6445. * new Path3D(path, normal, raw)
  6446. * Creates a Path3D. A Path3D is a logical math object, so not a mesh.
  6447. * please read the description in the tutorial : https://doc.babylonjs.com/how_to/how_to_use_path3d
  6448. * @param path an array of Vector3, the curve axis of the Path3D
  6449. * @param firstNormal (options) Vector3, the first wanted normal to the curve. Ex (0, 1, 0) for a vertical normal.
  6450. * @param raw (optional, default false) : boolean, if true the returned Path3D isn't normalized. Useful to depict path acceleration or speed.
  6451. */
  6452. constructor(
  6453. /**
  6454. * an array of Vector3, the curve axis of the Path3D
  6455. */
  6456. public path: Vector3[],
  6457. firstNormal: Nullable<Vector3> = null,
  6458. raw?: boolean
  6459. ) {
  6460. for (var p = 0; p < path.length; p++) {
  6461. this._curve[p] = path[p].clone(); // hard copy
  6462. }
  6463. this._raw = raw || false;
  6464. this._compute(firstNormal);
  6465. }
  6466. /**
  6467. * Returns the Path3D array of successive Vector3 designing its curve.
  6468. * @returns the Path3D array of successive Vector3 designing its curve.
  6469. */
  6470. public getCurve(): Vector3[] {
  6471. return this._curve;
  6472. }
  6473. /**
  6474. * Returns an array populated with tangent vectors on each Path3D curve point.
  6475. * @returns an array populated with tangent vectors on each Path3D curve point.
  6476. */
  6477. public getTangents(): Vector3[] {
  6478. return this._tangents;
  6479. }
  6480. /**
  6481. * Returns an array populated with normal vectors on each Path3D curve point.
  6482. * @returns an array populated with normal vectors on each Path3D curve point.
  6483. */
  6484. public getNormals(): Vector3[] {
  6485. return this._normals;
  6486. }
  6487. /**
  6488. * Returns an array populated with binormal vectors on each Path3D curve point.
  6489. * @returns an array populated with binormal vectors on each Path3D curve point.
  6490. */
  6491. public getBinormals(): Vector3[] {
  6492. return this._binormals;
  6493. }
  6494. /**
  6495. * Returns an array populated with distances (float) of the i-th point from the first curve point.
  6496. * @returns an array populated with distances (float) of the i-th point from the first curve point.
  6497. */
  6498. public getDistances(): number[] {
  6499. return this._distances;
  6500. }
  6501. /**
  6502. * Forces the Path3D tangent, normal, binormal and distance recomputation.
  6503. * @param path path which all values are copied into the curves points
  6504. * @param firstNormal which should be projected onto the curve
  6505. * @returns the same object updated.
  6506. */
  6507. public update(path: Vector3[], firstNormal: Nullable<Vector3> = null): Path3D {
  6508. for (var p = 0; p < path.length; p++) {
  6509. this._curve[p].x = path[p].x;
  6510. this._curve[p].y = path[p].y;
  6511. this._curve[p].z = path[p].z;
  6512. }
  6513. this._compute(firstNormal);
  6514. return this;
  6515. }
  6516. // private function compute() : computes tangents, normals and binormals
  6517. private _compute(firstNormal: Nullable<Vector3>): void {
  6518. var l = this._curve.length;
  6519. // first and last tangents
  6520. this._tangents[0] = this._getFirstNonNullVector(0);
  6521. if (!this._raw) {
  6522. this._tangents[0].normalize();
  6523. }
  6524. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  6525. if (!this._raw) {
  6526. this._tangents[l - 1].normalize();
  6527. }
  6528. // normals and binormals at first point : arbitrary vector with _normalVector()
  6529. var tg0 = this._tangents[0];
  6530. var pp0 = this._normalVector(tg0, firstNormal);
  6531. this._normals[0] = pp0;
  6532. if (!this._raw) {
  6533. this._normals[0].normalize();
  6534. }
  6535. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  6536. if (!this._raw) {
  6537. this._binormals[0].normalize();
  6538. }
  6539. this._distances[0] = 0.0;
  6540. // normals and binormals : next points
  6541. var prev: Vector3; // previous vector (segment)
  6542. var cur: Vector3; // current vector (segment)
  6543. var curTang: Vector3; // current tangent
  6544. // previous normal
  6545. var prevBinor: Vector3; // previous binormal
  6546. for (var i = 1; i < l; i++) {
  6547. // tangents
  6548. prev = this._getLastNonNullVector(i);
  6549. if (i < l - 1) {
  6550. cur = this._getFirstNonNullVector(i);
  6551. this._tangents[i] = prev.add(cur);
  6552. this._tangents[i].normalize();
  6553. }
  6554. this._distances[i] = this._distances[i - 1] + prev.length();
  6555. // normals and binormals
  6556. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  6557. curTang = this._tangents[i];
  6558. prevBinor = this._binormals[i - 1];
  6559. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  6560. if (!this._raw) {
  6561. this._normals[i].normalize();
  6562. }
  6563. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  6564. if (!this._raw) {
  6565. this._binormals[i].normalize();
  6566. }
  6567. }
  6568. }
  6569. // private function getFirstNonNullVector(index)
  6570. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  6571. private _getFirstNonNullVector(index: number): Vector3 {
  6572. var i = 1;
  6573. var nNVector: Vector3 = this._curve[index + i].subtract(this._curve[index]);
  6574. while (nNVector.length() === 0 && index + i + 1 < this._curve.length) {
  6575. i++;
  6576. nNVector = this._curve[index + i].subtract(this._curve[index]);
  6577. }
  6578. return nNVector;
  6579. }
  6580. // private function getLastNonNullVector(index)
  6581. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  6582. private _getLastNonNullVector(index: number): Vector3 {
  6583. var i = 1;
  6584. var nLVector: Vector3 = this._curve[index].subtract(this._curve[index - i]);
  6585. while (nLVector.length() === 0 && index > i + 1) {
  6586. i++;
  6587. nLVector = this._curve[index].subtract(this._curve[index - i]);
  6588. }
  6589. return nLVector;
  6590. }
  6591. // private function normalVector(v0, vt, va) :
  6592. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  6593. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  6594. private _normalVector(vt: Vector3, va: Nullable<Vector3>): Vector3 {
  6595. var normal0: Vector3;
  6596. var tgl = vt.length();
  6597. if (tgl === 0.0) {
  6598. tgl = 1.0;
  6599. }
  6600. if (va === undefined || va === null) {
  6601. var point: Vector3;
  6602. if (!Scalar.WithinEpsilon(Math.abs(vt.y) / tgl, 1.0, Epsilon)) { // search for a point in the plane
  6603. point = new Vector3(0.0, -1.0, 0.0);
  6604. }
  6605. else if (!Scalar.WithinEpsilon(Math.abs(vt.x) / tgl, 1.0, Epsilon)) {
  6606. point = new Vector3(1.0, 0.0, 0.0);
  6607. }
  6608. else if (!Scalar.WithinEpsilon(Math.abs(vt.z) / tgl, 1.0, Epsilon)) {
  6609. point = new Vector3(0.0, 0.0, 1.0);
  6610. }
  6611. else {
  6612. point = Vector3.Zero();
  6613. }
  6614. normal0 = Vector3.Cross(vt, point);
  6615. }
  6616. else {
  6617. normal0 = Vector3.Cross(vt, va);
  6618. Vector3.CrossToRef(normal0, vt, normal0);
  6619. }
  6620. normal0.normalize();
  6621. return normal0;
  6622. }
  6623. }
  6624. /**
  6625. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  6626. * A Curve3 is designed from a series of successive Vector3.
  6627. * @see https://doc.babylonjs.com/how_to/how_to_use_curve3
  6628. */
  6629. export class Curve3 {
  6630. private _points: Vector3[];
  6631. private _length: number = 0.0;
  6632. /**
  6633. * Returns a Curve3 object along a Quadratic Bezier curve : https://doc.babylonjs.com/how_to/how_to_use_curve3#quadratic-bezier-curve
  6634. * @param v0 (Vector3) the origin point of the Quadratic Bezier
  6635. * @param v1 (Vector3) the control point
  6636. * @param v2 (Vector3) the end point of the Quadratic Bezier
  6637. * @param nbPoints (integer) the wanted number of points in the curve
  6638. * @returns the created Curve3
  6639. */
  6640. public static CreateQuadraticBezier(v0: DeepImmutable<Vector3>, v1: DeepImmutable<Vector3>, v2: DeepImmutable<Vector3>, nbPoints: number): Curve3 {
  6641. nbPoints = nbPoints > 2 ? nbPoints : 3;
  6642. var bez = new Array<Vector3>();
  6643. var equation = (t: number, val0: number, val1: number, val2: number) => {
  6644. var res = (1.0 - t) * (1.0 - t) * val0 + 2.0 * t * (1.0 - t) * val1 + t * t * val2;
  6645. return res;
  6646. };
  6647. for (var i = 0; i <= nbPoints; i++) {
  6648. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x), equation(i / nbPoints, v0.y, v1.y, v2.y), equation(i / nbPoints, v0.z, v1.z, v2.z)));
  6649. }
  6650. return new Curve3(bez);
  6651. }
  6652. /**
  6653. * Returns a Curve3 object along a Cubic Bezier curve : https://doc.babylonjs.com/how_to/how_to_use_curve3#cubic-bezier-curve
  6654. * @param v0 (Vector3) the origin point of the Cubic Bezier
  6655. * @param v1 (Vector3) the first control point
  6656. * @param v2 (Vector3) the second control point
  6657. * @param v3 (Vector3) the end point of the Cubic Bezier
  6658. * @param nbPoints (integer) the wanted number of points in the curve
  6659. * @returns the created Curve3
  6660. */
  6661. public static CreateCubicBezier(v0: DeepImmutable<Vector3>, v1: DeepImmutable<Vector3>, v2: DeepImmutable<Vector3>, v3: DeepImmutable<Vector3>, nbPoints: number): Curve3 {
  6662. nbPoints = nbPoints > 3 ? nbPoints : 4;
  6663. var bez = new Array<Vector3>();
  6664. var equation = (t: number, val0: number, val1: number, val2: number, val3: number) => {
  6665. var res = (1.0 - t) * (1.0 - t) * (1.0 - t) * val0 + 3.0 * t * (1.0 - t) * (1.0 - t) * val1 + 3.0 * t * t * (1.0 - t) * val2 + t * t * t * val3;
  6666. return res;
  6667. };
  6668. for (var i = 0; i <= nbPoints; i++) {
  6669. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x, v3.x), equation(i / nbPoints, v0.y, v1.y, v2.y, v3.y), equation(i / nbPoints, v0.z, v1.z, v2.z, v3.z)));
  6670. }
  6671. return new Curve3(bez);
  6672. }
  6673. /**
  6674. * Returns a Curve3 object along a Hermite Spline curve : https://doc.babylonjs.com/how_to/how_to_use_curve3#hermite-spline
  6675. * @param p1 (Vector3) the origin point of the Hermite Spline
  6676. * @param t1 (Vector3) the tangent vector at the origin point
  6677. * @param p2 (Vector3) the end point of the Hermite Spline
  6678. * @param t2 (Vector3) the tangent vector at the end point
  6679. * @param nbPoints (integer) the wanted number of points in the curve
  6680. * @returns the created Curve3
  6681. */
  6682. public static CreateHermiteSpline(p1: DeepImmutable<Vector3>, t1: DeepImmutable<Vector3>, p2: DeepImmutable<Vector3>, t2: DeepImmutable<Vector3>, nbPoints: number): Curve3 {
  6683. var hermite = new Array<Vector3>();
  6684. var step = 1.0 / nbPoints;
  6685. for (var i = 0; i <= nbPoints; i++) {
  6686. hermite.push(Vector3.Hermite(p1, t1, p2, t2, i * step));
  6687. }
  6688. return new Curve3(hermite);
  6689. }
  6690. /**
  6691. * Returns a Curve3 object along a CatmullRom Spline curve :
  6692. * @param points (array of Vector3) the points the spline must pass through. At least, four points required
  6693. * @param nbPoints (integer) the wanted number of points between each curve control points
  6694. * @param closed (boolean) optional with default false, when true forms a closed loop from the points
  6695. * @returns the created Curve3
  6696. */
  6697. public static CreateCatmullRomSpline(points: DeepImmutable<Vector3[]>, nbPoints: number, closed?: boolean): Curve3 {
  6698. var catmullRom = new Array<Vector3>();
  6699. var step = 1.0 / nbPoints;
  6700. var amount = 0.0;
  6701. if (closed) {
  6702. var pointsCount = points.length;
  6703. for (var i = 0; i < pointsCount; i++) {
  6704. amount = 0;
  6705. for (var c = 0; c < nbPoints; c++) {
  6706. catmullRom.push(Vector3.CatmullRom(points[i % pointsCount], points[(i + 1) % pointsCount], points[(i + 2) % pointsCount], points[(i + 3) % pointsCount], amount));
  6707. amount += step;
  6708. }
  6709. }
  6710. catmullRom.push(catmullRom[0]);
  6711. }
  6712. else {
  6713. var totalPoints = new Array<Vector3>();
  6714. totalPoints.push(points[0].clone());
  6715. Array.prototype.push.apply(totalPoints, points);
  6716. totalPoints.push(points[points.length - 1].clone());
  6717. for (var i = 0; i < totalPoints.length - 3; i++) {
  6718. amount = 0;
  6719. for (var c = 0; c < nbPoints; c++) {
  6720. catmullRom.push(Vector3.CatmullRom(totalPoints[i], totalPoints[i + 1], totalPoints[i + 2], totalPoints[i + 3], amount));
  6721. amount += step;
  6722. }
  6723. }
  6724. i--;
  6725. catmullRom.push(Vector3.CatmullRom(totalPoints[i], totalPoints[i + 1], totalPoints[i + 2], totalPoints[i + 3], amount));
  6726. }
  6727. return new Curve3(catmullRom);
  6728. }
  6729. /**
  6730. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  6731. * A Curve3 is designed from a series of successive Vector3.
  6732. * Tuto : https://doc.babylonjs.com/how_to/how_to_use_curve3#curve3-object
  6733. * @param points points which make up the curve
  6734. */
  6735. constructor(points: Vector3[]) {
  6736. this._points = points;
  6737. this._length = this._computeLength(points);
  6738. }
  6739. /**
  6740. * @returns the Curve3 stored array of successive Vector3
  6741. */
  6742. public getPoints() {
  6743. return this._points;
  6744. }
  6745. /**
  6746. * @returns the computed length (float) of the curve.
  6747. */
  6748. public length() {
  6749. return this._length;
  6750. }
  6751. /**
  6752. * Returns a new instance of Curve3 object : var curve = curveA.continue(curveB);
  6753. * This new Curve3 is built by translating and sticking the curveB at the end of the curveA.
  6754. * curveA and curveB keep unchanged.
  6755. * @param curve the curve to continue from this curve
  6756. * @returns the newly constructed curve
  6757. */
  6758. public continue(curve: DeepImmutable<Curve3>): Curve3 {
  6759. var lastPoint = this._points[this._points.length - 1];
  6760. var continuedPoints = this._points.slice();
  6761. var curvePoints = curve.getPoints();
  6762. for (var i = 1; i < curvePoints.length; i++) {
  6763. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  6764. }
  6765. var continuedCurve = new Curve3(continuedPoints);
  6766. return continuedCurve;
  6767. }
  6768. private _computeLength(path: DeepImmutable<Vector3[]>): number {
  6769. var l = 0;
  6770. for (var i = 1; i < path.length; i++) {
  6771. l += (path[i].subtract(path[i - 1])).length();
  6772. }
  6773. return l;
  6774. }
  6775. }
  6776. // Vertex formats
  6777. /**
  6778. * Contains position and normal vectors for a vertex
  6779. */
  6780. export class PositionNormalVertex {
  6781. /**
  6782. * Creates a PositionNormalVertex
  6783. * @param position the position of the vertex (defaut: 0,0,0)
  6784. * @param normal the normal of the vertex (defaut: 0,1,0)
  6785. */
  6786. constructor(
  6787. /** the position of the vertex (defaut: 0,0,0) */
  6788. public position: Vector3 = Vector3.Zero(),
  6789. /** the normal of the vertex (defaut: 0,1,0) */
  6790. public normal: Vector3 = Vector3.Up()
  6791. ) {
  6792. }
  6793. /**
  6794. * Clones the PositionNormalVertex
  6795. * @returns the cloned PositionNormalVertex
  6796. */
  6797. public clone(): PositionNormalVertex {
  6798. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  6799. }
  6800. }
  6801. /**
  6802. * Contains position, normal and uv vectors for a vertex
  6803. */
  6804. export class PositionNormalTextureVertex {
  6805. /**
  6806. * Creates a PositionNormalTextureVertex
  6807. * @param position the position of the vertex (defaut: 0,0,0)
  6808. * @param normal the normal of the vertex (defaut: 0,1,0)
  6809. * @param uv the uv of the vertex (default: 0,0)
  6810. */
  6811. constructor(
  6812. /** the position of the vertex (defaut: 0,0,0) */
  6813. public position: Vector3 = Vector3.Zero(),
  6814. /** the normal of the vertex (defaut: 0,1,0) */
  6815. public normal: Vector3 = Vector3.Up(),
  6816. /** the uv of the vertex (default: 0,0) */
  6817. public uv: Vector2 = Vector2.Zero()
  6818. ) {
  6819. }
  6820. /**
  6821. * Clones the PositionNormalTextureVertex
  6822. * @returns the cloned PositionNormalTextureVertex
  6823. */
  6824. public clone(): PositionNormalTextureVertex {
  6825. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  6826. }
  6827. }
  6828. // Temporary pre-allocated objects for engine internal use
  6829. // usage in any internal function :
  6830. // var tmp = Tmp.Vector3[0]; <= gets access to the first pre-created Vector3
  6831. // There's a Tmp array per object type : int, float, Vector2, Vector3, Vector4, Quaternion, Matrix
  6832. /**
  6833. * @hidden
  6834. */
  6835. export class Tmp {
  6836. public static Color3: Color3[] = ArrayTools.BuildArray(3, Color3.Black);
  6837. public static Color4: Color4[] = ArrayTools.BuildArray(3, () => new Color4(0, 0, 0, 0));
  6838. public static Vector2: Vector2[] = ArrayTools.BuildArray(3, Vector2.Zero); // 3 temp Vector2 at once should be enough
  6839. public static Vector3: Vector3[] = ArrayTools.BuildArray(13, Vector3.Zero); // 13 temp Vector3 at once should be enough
  6840. public static Vector4: Vector4[] = ArrayTools.BuildArray(3, Vector4.Zero); // 3 temp Vector4 at once should be enough
  6841. public static Quaternion: Quaternion[] = ArrayTools.BuildArray(2, Quaternion.Zero); // 2 temp Quaternion at once should be enough
  6842. public static Matrix: Matrix[] = ArrayTools.BuildArray(8, Matrix.Identity); // 8 temp Matrices at once should be enough
  6843. }
  6844. /**
  6845. * @hidden
  6846. * Same as Tmp but not exported to keep it only for math functions to avoid conflicts
  6847. */
  6848. class MathTmp {
  6849. public static Vector3: Vector3[] = ArrayTools.BuildArray(6, Vector3.Zero);
  6850. public static Matrix: Matrix[] = ArrayTools.BuildArray(2, Matrix.Identity);
  6851. public static Quaternion: Quaternion[] = ArrayTools.BuildArray(3, Quaternion.Zero);
  6852. }