babylon.math.ts 289 KB

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