babylon.math.ts 289 KB

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