gl-matrix.js 198 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711
  1. /*!
  2. @fileoverview gl-matrix - High performance matrix and vector operations
  3. @author Brandon Jones
  4. @author Colin MacKenzie IV
  5. @version 3.4.3
  6. Copyright (c) 2015-2021, Brandon Jones, Colin MacKenzie IV.
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. */
  23. (function (global, factory) {
  24. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  25. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  26. (global = global || self, factory(global.glMatrix = {}));
  27. }(this, (function (exports) { 'use strict';
  28. /**
  29. * Common utilities
  30. * @module glMatrix
  31. */
  32. // Configuration Constants
  33. var EPSILON = 0.000001;
  34. var ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array;
  35. var RANDOM = Math.random;
  36. /**
  37. * Sets the type of array used when creating new vectors and matrices
  38. *
  39. * @param {Float32ArrayConstructor | ArrayConstructor} type Array type, such as Float32Array or Array
  40. */
  41. function setMatrixArrayType(type) {
  42. ARRAY_TYPE = type;
  43. }
  44. var degree = Math.PI / 180;
  45. /**
  46. * Convert Degree To Radian
  47. *
  48. * @param {Number} a Angle in Degrees
  49. */
  50. function toRadian(a) {
  51. return a * degree;
  52. }
  53. /**
  54. * Tests whether or not the arguments have approximately the same value, within an absolute
  55. * or relative tolerance of glMatrix.EPSILON (an absolute tolerance is used for values less
  56. * than or equal to 1.0, and a relative tolerance is used for larger values)
  57. *
  58. * @param {Number} a The first number to test.
  59. * @param {Number} b The second number to test.
  60. * @returns {Boolean} True if the numbers are approximately equal, false otherwise.
  61. */
  62. function equals(a, b) {
  63. return Math.abs(a - b) <= EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b));
  64. }
  65. if (!Math.hypot) Math.hypot = function () {
  66. var y = 0,
  67. i = arguments.length;
  68. while (i--) {
  69. y += arguments[i] * arguments[i];
  70. }
  71. return Math.sqrt(y);
  72. };
  73. var common = /*#__PURE__*/Object.freeze({
  74. __proto__: null,
  75. EPSILON: EPSILON,
  76. get ARRAY_TYPE () { return ARRAY_TYPE; },
  77. RANDOM: RANDOM,
  78. setMatrixArrayType: setMatrixArrayType,
  79. toRadian: toRadian,
  80. equals: equals
  81. });
  82. /**
  83. * 2x2 Matrix
  84. * @module mat2
  85. */
  86. /**
  87. * Creates a new identity mat2
  88. *
  89. * @returns {mat2} a new 2x2 matrix
  90. */
  91. function create() {
  92. var out = new ARRAY_TYPE(4);
  93. if (ARRAY_TYPE != Float32Array) {
  94. out[1] = 0;
  95. out[2] = 0;
  96. }
  97. out[0] = 1;
  98. out[3] = 1;
  99. return out;
  100. }
  101. /**
  102. * Creates a new mat2 initialized with values from an existing matrix
  103. *
  104. * @param {ReadonlyMat2} a matrix to clone
  105. * @returns {mat2} a new 2x2 matrix
  106. */
  107. function clone(a) {
  108. var out = new ARRAY_TYPE(4);
  109. out[0] = a[0];
  110. out[1] = a[1];
  111. out[2] = a[2];
  112. out[3] = a[3];
  113. return out;
  114. }
  115. /**
  116. * Copy the values from one mat2 to another
  117. *
  118. * @param {mat2} out the receiving matrix
  119. * @param {ReadonlyMat2} a the source matrix
  120. * @returns {mat2} out
  121. */
  122. function copy(out, a) {
  123. out[0] = a[0];
  124. out[1] = a[1];
  125. out[2] = a[2];
  126. out[3] = a[3];
  127. return out;
  128. }
  129. /**
  130. * Set a mat2 to the identity matrix
  131. *
  132. * @param {mat2} out the receiving matrix
  133. * @returns {mat2} out
  134. */
  135. function identity(out) {
  136. out[0] = 1;
  137. out[1] = 0;
  138. out[2] = 0;
  139. out[3] = 1;
  140. return out;
  141. }
  142. /**
  143. * Create a new mat2 with the given values
  144. *
  145. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  146. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  147. * @param {Number} m10 Component in column 1, row 0 position (index 2)
  148. * @param {Number} m11 Component in column 1, row 1 position (index 3)
  149. * @returns {mat2} out A new 2x2 matrix
  150. */
  151. function fromValues(m00, m01, m10, m11) {
  152. var out = new ARRAY_TYPE(4);
  153. out[0] = m00;
  154. out[1] = m01;
  155. out[2] = m10;
  156. out[3] = m11;
  157. return out;
  158. }
  159. /**
  160. * Set the components of a mat2 to the given values
  161. *
  162. * @param {mat2} out the receiving matrix
  163. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  164. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  165. * @param {Number} m10 Component in column 1, row 0 position (index 2)
  166. * @param {Number} m11 Component in column 1, row 1 position (index 3)
  167. * @returns {mat2} out
  168. */
  169. function set(out, m00, m01, m10, m11) {
  170. out[0] = m00;
  171. out[1] = m01;
  172. out[2] = m10;
  173. out[3] = m11;
  174. return out;
  175. }
  176. /**
  177. * Transpose the values of a mat2
  178. *
  179. * @param {mat2} out the receiving matrix
  180. * @param {ReadonlyMat2} a the source matrix
  181. * @returns {mat2} out
  182. */
  183. function transpose(out, a) {
  184. // If we are transposing ourselves we can skip a few steps but have to cache
  185. // some values
  186. if (out === a) {
  187. var a1 = a[1];
  188. out[1] = a[2];
  189. out[2] = a1;
  190. } else {
  191. out[0] = a[0];
  192. out[1] = a[2];
  193. out[2] = a[1];
  194. out[3] = a[3];
  195. }
  196. return out;
  197. }
  198. /**
  199. * Inverts a mat2
  200. *
  201. * @param {mat2} out the receiving matrix
  202. * @param {ReadonlyMat2} a the source matrix
  203. * @returns {mat2} out
  204. */
  205. function invert(out, a) {
  206. var a0 = a[0],
  207. a1 = a[1],
  208. a2 = a[2],
  209. a3 = a[3]; // Calculate the determinant
  210. var det = a0 * a3 - a2 * a1;
  211. if (!det) {
  212. return null;
  213. }
  214. det = 1.0 / det;
  215. out[0] = a3 * det;
  216. out[1] = -a1 * det;
  217. out[2] = -a2 * det;
  218. out[3] = a0 * det;
  219. return out;
  220. }
  221. /**
  222. * Calculates the adjugate of a mat2
  223. *
  224. * @param {mat2} out the receiving matrix
  225. * @param {ReadonlyMat2} a the source matrix
  226. * @returns {mat2} out
  227. */
  228. function adjoint(out, a) {
  229. // Caching this value is nessecary if out == a
  230. var a0 = a[0];
  231. out[0] = a[3];
  232. out[1] = -a[1];
  233. out[2] = -a[2];
  234. out[3] = a0;
  235. return out;
  236. }
  237. /**
  238. * Calculates the determinant of a mat2
  239. *
  240. * @param {ReadonlyMat2} a the source matrix
  241. * @returns {Number} determinant of a
  242. */
  243. function determinant(a) {
  244. return a[0] * a[3] - a[2] * a[1];
  245. }
  246. /**
  247. * Multiplies two mat2's
  248. *
  249. * @param {mat2} out the receiving matrix
  250. * @param {ReadonlyMat2} a the first operand
  251. * @param {ReadonlyMat2} b the second operand
  252. * @returns {mat2} out
  253. */
  254. function multiply(out, a, b) {
  255. var a0 = a[0],
  256. a1 = a[1],
  257. a2 = a[2],
  258. a3 = a[3];
  259. var b0 = b[0],
  260. b1 = b[1],
  261. b2 = b[2],
  262. b3 = b[3];
  263. out[0] = a0 * b0 + a2 * b1;
  264. out[1] = a1 * b0 + a3 * b1;
  265. out[2] = a0 * b2 + a2 * b3;
  266. out[3] = a1 * b2 + a3 * b3;
  267. return out;
  268. }
  269. /**
  270. * Rotates a mat2 by the given angle
  271. *
  272. * @param {mat2} out the receiving matrix
  273. * @param {ReadonlyMat2} a the matrix to rotate
  274. * @param {Number} rad the angle to rotate the matrix by
  275. * @returns {mat2} out
  276. */
  277. function rotate(out, a, rad) {
  278. var a0 = a[0],
  279. a1 = a[1],
  280. a2 = a[2],
  281. a3 = a[3];
  282. var s = Math.sin(rad);
  283. var c = Math.cos(rad);
  284. out[0] = a0 * c + a2 * s;
  285. out[1] = a1 * c + a3 * s;
  286. out[2] = a0 * -s + a2 * c;
  287. out[3] = a1 * -s + a3 * c;
  288. return out;
  289. }
  290. /**
  291. * Scales the mat2 by the dimensions in the given vec2
  292. *
  293. * @param {mat2} out the receiving matrix
  294. * @param {ReadonlyMat2} a the matrix to rotate
  295. * @param {ReadonlyVec2} v the vec2 to scale the matrix by
  296. * @returns {mat2} out
  297. **/
  298. function scale(out, a, v) {
  299. var a0 = a[0],
  300. a1 = a[1],
  301. a2 = a[2],
  302. a3 = a[3];
  303. var v0 = v[0],
  304. v1 = v[1];
  305. out[0] = a0 * v0;
  306. out[1] = a1 * v0;
  307. out[2] = a2 * v1;
  308. out[3] = a3 * v1;
  309. return out;
  310. }
  311. /**
  312. * Creates a matrix from a given angle
  313. * This is equivalent to (but much faster than):
  314. *
  315. * mat2.identity(dest);
  316. * mat2.rotate(dest, dest, rad);
  317. *
  318. * @param {mat2} out mat2 receiving operation result
  319. * @param {Number} rad the angle to rotate the matrix by
  320. * @returns {mat2} out
  321. */
  322. function fromRotation(out, rad) {
  323. var s = Math.sin(rad);
  324. var c = Math.cos(rad);
  325. out[0] = c;
  326. out[1] = s;
  327. out[2] = -s;
  328. out[3] = c;
  329. return out;
  330. }
  331. /**
  332. * Creates a matrix from a vector scaling
  333. * This is equivalent to (but much faster than):
  334. *
  335. * mat2.identity(dest);
  336. * mat2.scale(dest, dest, vec);
  337. *
  338. * @param {mat2} out mat2 receiving operation result
  339. * @param {ReadonlyVec2} v Scaling vector
  340. * @returns {mat2} out
  341. */
  342. function fromScaling(out, v) {
  343. out[0] = v[0];
  344. out[1] = 0;
  345. out[2] = 0;
  346. out[3] = v[1];
  347. return out;
  348. }
  349. /**
  350. * Returns a string representation of a mat2
  351. *
  352. * @param {ReadonlyMat2} a matrix to represent as a string
  353. * @returns {String} string representation of the matrix
  354. */
  355. function str(a) {
  356. return "mat2(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ")";
  357. }
  358. /**
  359. * Returns Frobenius norm of a mat2
  360. *
  361. * @param {ReadonlyMat2} a the matrix to calculate Frobenius norm of
  362. * @returns {Number} Frobenius norm
  363. */
  364. function frob(a) {
  365. return Math.hypot(a[0], a[1], a[2], a[3]);
  366. }
  367. /**
  368. * Returns L, D and U matrices (Lower triangular, Diagonal and Upper triangular) by factorizing the input matrix
  369. * @param {ReadonlyMat2} L the lower triangular matrix
  370. * @param {ReadonlyMat2} D the diagonal matrix
  371. * @param {ReadonlyMat2} U the upper triangular matrix
  372. * @param {ReadonlyMat2} a the input matrix to factorize
  373. */
  374. function LDU(L, D, U, a) {
  375. L[2] = a[2] / a[0];
  376. U[0] = a[0];
  377. U[1] = a[1];
  378. U[3] = a[3] - L[2] * U[1];
  379. return [L, D, U];
  380. }
  381. /**
  382. * Adds two mat2's
  383. *
  384. * @param {mat2} out the receiving matrix
  385. * @param {ReadonlyMat2} a the first operand
  386. * @param {ReadonlyMat2} b the second operand
  387. * @returns {mat2} out
  388. */
  389. function add(out, a, b) {
  390. out[0] = a[0] + b[0];
  391. out[1] = a[1] + b[1];
  392. out[2] = a[2] + b[2];
  393. out[3] = a[3] + b[3];
  394. return out;
  395. }
  396. /**
  397. * Subtracts matrix b from matrix a
  398. *
  399. * @param {mat2} out the receiving matrix
  400. * @param {ReadonlyMat2} a the first operand
  401. * @param {ReadonlyMat2} b the second operand
  402. * @returns {mat2} out
  403. */
  404. function subtract(out, a, b) {
  405. out[0] = a[0] - b[0];
  406. out[1] = a[1] - b[1];
  407. out[2] = a[2] - b[2];
  408. out[3] = a[3] - b[3];
  409. return out;
  410. }
  411. /**
  412. * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
  413. *
  414. * @param {ReadonlyMat2} a The first matrix.
  415. * @param {ReadonlyMat2} b The second matrix.
  416. * @returns {Boolean} True if the matrices are equal, false otherwise.
  417. */
  418. function exactEquals(a, b) {
  419. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
  420. }
  421. /**
  422. * Returns whether or not the matrices have approximately the same elements in the same position.
  423. *
  424. * @param {ReadonlyMat2} a The first matrix.
  425. * @param {ReadonlyMat2} b The second matrix.
  426. * @returns {Boolean} True if the matrices are equal, false otherwise.
  427. */
  428. function equals$1(a, b) {
  429. var a0 = a[0],
  430. a1 = a[1],
  431. a2 = a[2],
  432. a3 = a[3];
  433. var b0 = b[0],
  434. b1 = b[1],
  435. b2 = b[2],
  436. b3 = b[3];
  437. return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3));
  438. }
  439. /**
  440. * Multiply each element of the matrix by a scalar.
  441. *
  442. * @param {mat2} out the receiving matrix
  443. * @param {ReadonlyMat2} a the matrix to scale
  444. * @param {Number} b amount to scale the matrix's elements by
  445. * @returns {mat2} out
  446. */
  447. function multiplyScalar(out, a, b) {
  448. out[0] = a[0] * b;
  449. out[1] = a[1] * b;
  450. out[2] = a[2] * b;
  451. out[3] = a[3] * b;
  452. return out;
  453. }
  454. /**
  455. * Adds two mat2's after multiplying each element of the second operand by a scalar value.
  456. *
  457. * @param {mat2} out the receiving vector
  458. * @param {ReadonlyMat2} a the first operand
  459. * @param {ReadonlyMat2} b the second operand
  460. * @param {Number} scale the amount to scale b's elements by before adding
  461. * @returns {mat2} out
  462. */
  463. function multiplyScalarAndAdd(out, a, b, scale) {
  464. out[0] = a[0] + b[0] * scale;
  465. out[1] = a[1] + b[1] * scale;
  466. out[2] = a[2] + b[2] * scale;
  467. out[3] = a[3] + b[3] * scale;
  468. return out;
  469. }
  470. /**
  471. * Alias for {@link mat2.multiply}
  472. * @function
  473. */
  474. var mul = multiply;
  475. /**
  476. * Alias for {@link mat2.subtract}
  477. * @function
  478. */
  479. var sub = subtract;
  480. var mat2 = /*#__PURE__*/Object.freeze({
  481. __proto__: null,
  482. create: create,
  483. clone: clone,
  484. copy: copy,
  485. identity: identity,
  486. fromValues: fromValues,
  487. set: set,
  488. transpose: transpose,
  489. invert: invert,
  490. adjoint: adjoint,
  491. determinant: determinant,
  492. multiply: multiply,
  493. rotate: rotate,
  494. scale: scale,
  495. fromRotation: fromRotation,
  496. fromScaling: fromScaling,
  497. str: str,
  498. frob: frob,
  499. LDU: LDU,
  500. add: add,
  501. subtract: subtract,
  502. exactEquals: exactEquals,
  503. equals: equals$1,
  504. multiplyScalar: multiplyScalar,
  505. multiplyScalarAndAdd: multiplyScalarAndAdd,
  506. mul: mul,
  507. sub: sub
  508. });
  509. /**
  510. * 2x3 Matrix
  511. * @module mat2d
  512. * @description
  513. * A mat2d contains six elements defined as:
  514. * <pre>
  515. * [a, b,
  516. * c, d,
  517. * tx, ty]
  518. * </pre>
  519. * This is a short form for the 3x3 matrix:
  520. * <pre>
  521. * [a, b, 0,
  522. * c, d, 0,
  523. * tx, ty, 1]
  524. * </pre>
  525. * The last column is ignored so the array is shorter and operations are faster.
  526. */
  527. /**
  528. * Creates a new identity mat2d
  529. *
  530. * @returns {mat2d} a new 2x3 matrix
  531. */
  532. function create$1() {
  533. var out = new ARRAY_TYPE(6);
  534. if (ARRAY_TYPE != Float32Array) {
  535. out[1] = 0;
  536. out[2] = 0;
  537. out[4] = 0;
  538. out[5] = 0;
  539. }
  540. out[0] = 1;
  541. out[3] = 1;
  542. return out;
  543. }
  544. /**
  545. * Creates a new mat2d initialized with values from an existing matrix
  546. *
  547. * @param {ReadonlyMat2d} a matrix to clone
  548. * @returns {mat2d} a new 2x3 matrix
  549. */
  550. function clone$1(a) {
  551. var out = new ARRAY_TYPE(6);
  552. out[0] = a[0];
  553. out[1] = a[1];
  554. out[2] = a[2];
  555. out[3] = a[3];
  556. out[4] = a[4];
  557. out[5] = a[5];
  558. return out;
  559. }
  560. /**
  561. * Copy the values from one mat2d to another
  562. *
  563. * @param {mat2d} out the receiving matrix
  564. * @param {ReadonlyMat2d} a the source matrix
  565. * @returns {mat2d} out
  566. */
  567. function copy$1(out, a) {
  568. out[0] = a[0];
  569. out[1] = a[1];
  570. out[2] = a[2];
  571. out[3] = a[3];
  572. out[4] = a[4];
  573. out[5] = a[5];
  574. return out;
  575. }
  576. /**
  577. * Set a mat2d to the identity matrix
  578. *
  579. * @param {mat2d} out the receiving matrix
  580. * @returns {mat2d} out
  581. */
  582. function identity$1(out) {
  583. out[0] = 1;
  584. out[1] = 0;
  585. out[2] = 0;
  586. out[3] = 1;
  587. out[4] = 0;
  588. out[5] = 0;
  589. return out;
  590. }
  591. /**
  592. * Create a new mat2d with the given values
  593. *
  594. * @param {Number} a Component A (index 0)
  595. * @param {Number} b Component B (index 1)
  596. * @param {Number} c Component C (index 2)
  597. * @param {Number} d Component D (index 3)
  598. * @param {Number} tx Component TX (index 4)
  599. * @param {Number} ty Component TY (index 5)
  600. * @returns {mat2d} A new mat2d
  601. */
  602. function fromValues$1(a, b, c, d, tx, ty) {
  603. var out = new ARRAY_TYPE(6);
  604. out[0] = a;
  605. out[1] = b;
  606. out[2] = c;
  607. out[3] = d;
  608. out[4] = tx;
  609. out[5] = ty;
  610. return out;
  611. }
  612. /**
  613. * Set the components of a mat2d to the given values
  614. *
  615. * @param {mat2d} out the receiving matrix
  616. * @param {Number} a Component A (index 0)
  617. * @param {Number} b Component B (index 1)
  618. * @param {Number} c Component C (index 2)
  619. * @param {Number} d Component D (index 3)
  620. * @param {Number} tx Component TX (index 4)
  621. * @param {Number} ty Component TY (index 5)
  622. * @returns {mat2d} out
  623. */
  624. function set$1(out, a, b, c, d, tx, ty) {
  625. out[0] = a;
  626. out[1] = b;
  627. out[2] = c;
  628. out[3] = d;
  629. out[4] = tx;
  630. out[5] = ty;
  631. return out;
  632. }
  633. /**
  634. * Inverts a mat2d
  635. *
  636. * @param {mat2d} out the receiving matrix
  637. * @param {ReadonlyMat2d} a the source matrix
  638. * @returns {mat2d} out
  639. */
  640. function invert$1(out, a) {
  641. var aa = a[0],
  642. ab = a[1],
  643. ac = a[2],
  644. ad = a[3];
  645. var atx = a[4],
  646. aty = a[5];
  647. var det = aa * ad - ab * ac;
  648. if (!det) {
  649. return null;
  650. }
  651. det = 1.0 / det;
  652. out[0] = ad * det;
  653. out[1] = -ab * det;
  654. out[2] = -ac * det;
  655. out[3] = aa * det;
  656. out[4] = (ac * aty - ad * atx) * det;
  657. out[5] = (ab * atx - aa * aty) * det;
  658. return out;
  659. }
  660. /**
  661. * Calculates the determinant of a mat2d
  662. *
  663. * @param {ReadonlyMat2d} a the source matrix
  664. * @returns {Number} determinant of a
  665. */
  666. function determinant$1(a) {
  667. return a[0] * a[3] - a[1] * a[2];
  668. }
  669. /**
  670. * Multiplies two mat2d's
  671. *
  672. * @param {mat2d} out the receiving matrix
  673. * @param {ReadonlyMat2d} a the first operand
  674. * @param {ReadonlyMat2d} b the second operand
  675. * @returns {mat2d} out
  676. */
  677. function multiply$1(out, a, b) {
  678. var a0 = a[0],
  679. a1 = a[1],
  680. a2 = a[2],
  681. a3 = a[3],
  682. a4 = a[4],
  683. a5 = a[5];
  684. var b0 = b[0],
  685. b1 = b[1],
  686. b2 = b[2],
  687. b3 = b[3],
  688. b4 = b[4],
  689. b5 = b[5];
  690. out[0] = a0 * b0 + a2 * b1;
  691. out[1] = a1 * b0 + a3 * b1;
  692. out[2] = a0 * b2 + a2 * b3;
  693. out[3] = a1 * b2 + a3 * b3;
  694. out[4] = a0 * b4 + a2 * b5 + a4;
  695. out[5] = a1 * b4 + a3 * b5 + a5;
  696. return out;
  697. }
  698. /**
  699. * Rotates a mat2d by the given angle
  700. *
  701. * @param {mat2d} out the receiving matrix
  702. * @param {ReadonlyMat2d} a the matrix to rotate
  703. * @param {Number} rad the angle to rotate the matrix by
  704. * @returns {mat2d} out
  705. */
  706. function rotate$1(out, a, rad) {
  707. var a0 = a[0],
  708. a1 = a[1],
  709. a2 = a[2],
  710. a3 = a[3],
  711. a4 = a[4],
  712. a5 = a[5];
  713. var s = Math.sin(rad);
  714. var c = Math.cos(rad);
  715. out[0] = a0 * c + a2 * s;
  716. out[1] = a1 * c + a3 * s;
  717. out[2] = a0 * -s + a2 * c;
  718. out[3] = a1 * -s + a3 * c;
  719. out[4] = a4;
  720. out[5] = a5;
  721. return out;
  722. }
  723. /**
  724. * Scales the mat2d by the dimensions in the given vec2
  725. *
  726. * @param {mat2d} out the receiving matrix
  727. * @param {ReadonlyMat2d} a the matrix to translate
  728. * @param {ReadonlyVec2} v the vec2 to scale the matrix by
  729. * @returns {mat2d} out
  730. **/
  731. function scale$1(out, a, v) {
  732. var a0 = a[0],
  733. a1 = a[1],
  734. a2 = a[2],
  735. a3 = a[3],
  736. a4 = a[4],
  737. a5 = a[5];
  738. var v0 = v[0],
  739. v1 = v[1];
  740. out[0] = a0 * v0;
  741. out[1] = a1 * v0;
  742. out[2] = a2 * v1;
  743. out[3] = a3 * v1;
  744. out[4] = a4;
  745. out[5] = a5;
  746. return out;
  747. }
  748. /**
  749. * Translates the mat2d by the dimensions in the given vec2
  750. *
  751. * @param {mat2d} out the receiving matrix
  752. * @param {ReadonlyMat2d} a the matrix to translate
  753. * @param {ReadonlyVec2} v the vec2 to translate the matrix by
  754. * @returns {mat2d} out
  755. **/
  756. function translate(out, a, v) {
  757. var a0 = a[0],
  758. a1 = a[1],
  759. a2 = a[2],
  760. a3 = a[3],
  761. a4 = a[4],
  762. a5 = a[5];
  763. var v0 = v[0],
  764. v1 = v[1];
  765. out[0] = a0;
  766. out[1] = a1;
  767. out[2] = a2;
  768. out[3] = a3;
  769. out[4] = a0 * v0 + a2 * v1 + a4;
  770. out[5] = a1 * v0 + a3 * v1 + a5;
  771. return out;
  772. }
  773. /**
  774. * Creates a matrix from a given angle
  775. * This is equivalent to (but much faster than):
  776. *
  777. * mat2d.identity(dest);
  778. * mat2d.rotate(dest, dest, rad);
  779. *
  780. * @param {mat2d} out mat2d receiving operation result
  781. * @param {Number} rad the angle to rotate the matrix by
  782. * @returns {mat2d} out
  783. */
  784. function fromRotation$1(out, rad) {
  785. var s = Math.sin(rad),
  786. c = Math.cos(rad);
  787. out[0] = c;
  788. out[1] = s;
  789. out[2] = -s;
  790. out[3] = c;
  791. out[4] = 0;
  792. out[5] = 0;
  793. return out;
  794. }
  795. /**
  796. * Creates a matrix from a vector scaling
  797. * This is equivalent to (but much faster than):
  798. *
  799. * mat2d.identity(dest);
  800. * mat2d.scale(dest, dest, vec);
  801. *
  802. * @param {mat2d} out mat2d receiving operation result
  803. * @param {ReadonlyVec2} v Scaling vector
  804. * @returns {mat2d} out
  805. */
  806. function fromScaling$1(out, v) {
  807. out[0] = v[0];
  808. out[1] = 0;
  809. out[2] = 0;
  810. out[3] = v[1];
  811. out[4] = 0;
  812. out[5] = 0;
  813. return out;
  814. }
  815. /**
  816. * Creates a matrix from a vector translation
  817. * This is equivalent to (but much faster than):
  818. *
  819. * mat2d.identity(dest);
  820. * mat2d.translate(dest, dest, vec);
  821. *
  822. * @param {mat2d} out mat2d receiving operation result
  823. * @param {ReadonlyVec2} v Translation vector
  824. * @returns {mat2d} out
  825. */
  826. function fromTranslation(out, v) {
  827. out[0] = 1;
  828. out[1] = 0;
  829. out[2] = 0;
  830. out[3] = 1;
  831. out[4] = v[0];
  832. out[5] = v[1];
  833. return out;
  834. }
  835. /**
  836. * Returns a string representation of a mat2d
  837. *
  838. * @param {ReadonlyMat2d} a matrix to represent as a string
  839. * @returns {String} string representation of the matrix
  840. */
  841. function str$1(a) {
  842. return "mat2d(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ")";
  843. }
  844. /**
  845. * Returns Frobenius norm of a mat2d
  846. *
  847. * @param {ReadonlyMat2d} a the matrix to calculate Frobenius norm of
  848. * @returns {Number} Frobenius norm
  849. */
  850. function frob$1(a) {
  851. return Math.hypot(a[0], a[1], a[2], a[3], a[4], a[5], 1);
  852. }
  853. /**
  854. * Adds two mat2d's
  855. *
  856. * @param {mat2d} out the receiving matrix
  857. * @param {ReadonlyMat2d} a the first operand
  858. * @param {ReadonlyMat2d} b the second operand
  859. * @returns {mat2d} out
  860. */
  861. function add$1(out, a, b) {
  862. out[0] = a[0] + b[0];
  863. out[1] = a[1] + b[1];
  864. out[2] = a[2] + b[2];
  865. out[3] = a[3] + b[3];
  866. out[4] = a[4] + b[4];
  867. out[5] = a[5] + b[5];
  868. return out;
  869. }
  870. /**
  871. * Subtracts matrix b from matrix a
  872. *
  873. * @param {mat2d} out the receiving matrix
  874. * @param {ReadonlyMat2d} a the first operand
  875. * @param {ReadonlyMat2d} b the second operand
  876. * @returns {mat2d} out
  877. */
  878. function subtract$1(out, a, b) {
  879. out[0] = a[0] - b[0];
  880. out[1] = a[1] - b[1];
  881. out[2] = a[2] - b[2];
  882. out[3] = a[3] - b[3];
  883. out[4] = a[4] - b[4];
  884. out[5] = a[5] - b[5];
  885. return out;
  886. }
  887. /**
  888. * Multiply each element of the matrix by a scalar.
  889. *
  890. * @param {mat2d} out the receiving matrix
  891. * @param {ReadonlyMat2d} a the matrix to scale
  892. * @param {Number} b amount to scale the matrix's elements by
  893. * @returns {mat2d} out
  894. */
  895. function multiplyScalar$1(out, a, b) {
  896. out[0] = a[0] * b;
  897. out[1] = a[1] * b;
  898. out[2] = a[2] * b;
  899. out[3] = a[3] * b;
  900. out[4] = a[4] * b;
  901. out[5] = a[5] * b;
  902. return out;
  903. }
  904. /**
  905. * Adds two mat2d's after multiplying each element of the second operand by a scalar value.
  906. *
  907. * @param {mat2d} out the receiving vector
  908. * @param {ReadonlyMat2d} a the first operand
  909. * @param {ReadonlyMat2d} b the second operand
  910. * @param {Number} scale the amount to scale b's elements by before adding
  911. * @returns {mat2d} out
  912. */
  913. function multiplyScalarAndAdd$1(out, a, b, scale) {
  914. out[0] = a[0] + b[0] * scale;
  915. out[1] = a[1] + b[1] * scale;
  916. out[2] = a[2] + b[2] * scale;
  917. out[3] = a[3] + b[3] * scale;
  918. out[4] = a[4] + b[4] * scale;
  919. out[5] = a[5] + b[5] * scale;
  920. return out;
  921. }
  922. /**
  923. * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
  924. *
  925. * @param {ReadonlyMat2d} a The first matrix.
  926. * @param {ReadonlyMat2d} b The second matrix.
  927. * @returns {Boolean} True if the matrices are equal, false otherwise.
  928. */
  929. function exactEquals$1(a, b) {
  930. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5];
  931. }
  932. /**
  933. * Returns whether or not the matrices have approximately the same elements in the same position.
  934. *
  935. * @param {ReadonlyMat2d} a The first matrix.
  936. * @param {ReadonlyMat2d} b The second matrix.
  937. * @returns {Boolean} True if the matrices are equal, false otherwise.
  938. */
  939. function equals$2(a, b) {
  940. var a0 = a[0],
  941. a1 = a[1],
  942. a2 = a[2],
  943. a3 = a[3],
  944. a4 = a[4],
  945. a5 = a[5];
  946. var b0 = b[0],
  947. b1 = b[1],
  948. b2 = b[2],
  949. b3 = b[3],
  950. b4 = b[4],
  951. b5 = b[5];
  952. return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5));
  953. }
  954. /**
  955. * Alias for {@link mat2d.multiply}
  956. * @function
  957. */
  958. var mul$1 = multiply$1;
  959. /**
  960. * Alias for {@link mat2d.subtract}
  961. * @function
  962. */
  963. var sub$1 = subtract$1;
  964. var mat2d = /*#__PURE__*/Object.freeze({
  965. __proto__: null,
  966. create: create$1,
  967. clone: clone$1,
  968. copy: copy$1,
  969. identity: identity$1,
  970. fromValues: fromValues$1,
  971. set: set$1,
  972. invert: invert$1,
  973. determinant: determinant$1,
  974. multiply: multiply$1,
  975. rotate: rotate$1,
  976. scale: scale$1,
  977. translate: translate,
  978. fromRotation: fromRotation$1,
  979. fromScaling: fromScaling$1,
  980. fromTranslation: fromTranslation,
  981. str: str$1,
  982. frob: frob$1,
  983. add: add$1,
  984. subtract: subtract$1,
  985. multiplyScalar: multiplyScalar$1,
  986. multiplyScalarAndAdd: multiplyScalarAndAdd$1,
  987. exactEquals: exactEquals$1,
  988. equals: equals$2,
  989. mul: mul$1,
  990. sub: sub$1
  991. });
  992. /**
  993. * 3x3 Matrix
  994. * @module mat3
  995. */
  996. /**
  997. * Creates a new identity mat3
  998. *
  999. * @returns {mat3} a new 3x3 matrix
  1000. */
  1001. function create$2() {
  1002. var out = new ARRAY_TYPE(9);
  1003. if (ARRAY_TYPE != Float32Array) {
  1004. out[1] = 0;
  1005. out[2] = 0;
  1006. out[3] = 0;
  1007. out[5] = 0;
  1008. out[6] = 0;
  1009. out[7] = 0;
  1010. }
  1011. out[0] = 1;
  1012. out[4] = 1;
  1013. out[8] = 1;
  1014. return out;
  1015. }
  1016. /**
  1017. * Copies the upper-left 3x3 values into the given mat3.
  1018. *
  1019. * @param {mat3} out the receiving 3x3 matrix
  1020. * @param {ReadonlyMat4} a the source 4x4 matrix
  1021. * @returns {mat3} out
  1022. */
  1023. function fromMat4(out, a) {
  1024. out[0] = a[0];
  1025. out[1] = a[1];
  1026. out[2] = a[2];
  1027. out[3] = a[4];
  1028. out[4] = a[5];
  1029. out[5] = a[6];
  1030. out[6] = a[8];
  1031. out[7] = a[9];
  1032. out[8] = a[10];
  1033. return out;
  1034. }
  1035. /**
  1036. * Creates a new mat3 initialized with values from an existing matrix
  1037. *
  1038. * @param {ReadonlyMat3} a matrix to clone
  1039. * @returns {mat3} a new 3x3 matrix
  1040. */
  1041. function clone$2(a) {
  1042. var out = new ARRAY_TYPE(9);
  1043. out[0] = a[0];
  1044. out[1] = a[1];
  1045. out[2] = a[2];
  1046. out[3] = a[3];
  1047. out[4] = a[4];
  1048. out[5] = a[5];
  1049. out[6] = a[6];
  1050. out[7] = a[7];
  1051. out[8] = a[8];
  1052. return out;
  1053. }
  1054. /**
  1055. * Copy the values from one mat3 to another
  1056. *
  1057. * @param {mat3} out the receiving matrix
  1058. * @param {ReadonlyMat3} a the source matrix
  1059. * @returns {mat3} out
  1060. */
  1061. function copy$2(out, a) {
  1062. out[0] = a[0];
  1063. out[1] = a[1];
  1064. out[2] = a[2];
  1065. out[3] = a[3];
  1066. out[4] = a[4];
  1067. out[5] = a[5];
  1068. out[6] = a[6];
  1069. out[7] = a[7];
  1070. out[8] = a[8];
  1071. return out;
  1072. }
  1073. /**
  1074. * Create a new mat3 with the given values
  1075. *
  1076. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  1077. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  1078. * @param {Number} m02 Component in column 0, row 2 position (index 2)
  1079. * @param {Number} m10 Component in column 1, row 0 position (index 3)
  1080. * @param {Number} m11 Component in column 1, row 1 position (index 4)
  1081. * @param {Number} m12 Component in column 1, row 2 position (index 5)
  1082. * @param {Number} m20 Component in column 2, row 0 position (index 6)
  1083. * @param {Number} m21 Component in column 2, row 1 position (index 7)
  1084. * @param {Number} m22 Component in column 2, row 2 position (index 8)
  1085. * @returns {mat3} A new mat3
  1086. */
  1087. function fromValues$2(m00, m01, m02, m10, m11, m12, m20, m21, m22) {
  1088. var out = new ARRAY_TYPE(9);
  1089. out[0] = m00;
  1090. out[1] = m01;
  1091. out[2] = m02;
  1092. out[3] = m10;
  1093. out[4] = m11;
  1094. out[5] = m12;
  1095. out[6] = m20;
  1096. out[7] = m21;
  1097. out[8] = m22;
  1098. return out;
  1099. }
  1100. /**
  1101. * Set the components of a mat3 to the given values
  1102. *
  1103. * @param {mat3} out the receiving matrix
  1104. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  1105. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  1106. * @param {Number} m02 Component in column 0, row 2 position (index 2)
  1107. * @param {Number} m10 Component in column 1, row 0 position (index 3)
  1108. * @param {Number} m11 Component in column 1, row 1 position (index 4)
  1109. * @param {Number} m12 Component in column 1, row 2 position (index 5)
  1110. * @param {Number} m20 Component in column 2, row 0 position (index 6)
  1111. * @param {Number} m21 Component in column 2, row 1 position (index 7)
  1112. * @param {Number} m22 Component in column 2, row 2 position (index 8)
  1113. * @returns {mat3} out
  1114. */
  1115. function set$2(out, m00, m01, m02, m10, m11, m12, m20, m21, m22) {
  1116. out[0] = m00;
  1117. out[1] = m01;
  1118. out[2] = m02;
  1119. out[3] = m10;
  1120. out[4] = m11;
  1121. out[5] = m12;
  1122. out[6] = m20;
  1123. out[7] = m21;
  1124. out[8] = m22;
  1125. return out;
  1126. }
  1127. /**
  1128. * Set a mat3 to the identity matrix
  1129. *
  1130. * @param {mat3} out the receiving matrix
  1131. * @returns {mat3} out
  1132. */
  1133. function identity$2(out) {
  1134. out[0] = 1;
  1135. out[1] = 0;
  1136. out[2] = 0;
  1137. out[3] = 0;
  1138. out[4] = 1;
  1139. out[5] = 0;
  1140. out[6] = 0;
  1141. out[7] = 0;
  1142. out[8] = 1;
  1143. return out;
  1144. }
  1145. /**
  1146. * Transpose the values of a mat3
  1147. *
  1148. * @param {mat3} out the receiving matrix
  1149. * @param {ReadonlyMat3} a the source matrix
  1150. * @returns {mat3} out
  1151. */
  1152. function transpose$1(out, a) {
  1153. // If we are transposing ourselves we can skip a few steps but have to cache some values
  1154. if (out === a) {
  1155. var a01 = a[1],
  1156. a02 = a[2],
  1157. a12 = a[5];
  1158. out[1] = a[3];
  1159. out[2] = a[6];
  1160. out[3] = a01;
  1161. out[5] = a[7];
  1162. out[6] = a02;
  1163. out[7] = a12;
  1164. } else {
  1165. out[0] = a[0];
  1166. out[1] = a[3];
  1167. out[2] = a[6];
  1168. out[3] = a[1];
  1169. out[4] = a[4];
  1170. out[5] = a[7];
  1171. out[6] = a[2];
  1172. out[7] = a[5];
  1173. out[8] = a[8];
  1174. }
  1175. return out;
  1176. }
  1177. /**
  1178. * Inverts a mat3
  1179. *
  1180. * @param {mat3} out the receiving matrix
  1181. * @param {ReadonlyMat3} a the source matrix
  1182. * @returns {mat3} out
  1183. */
  1184. function invert$2(out, a) {
  1185. var a00 = a[0],
  1186. a01 = a[1],
  1187. a02 = a[2];
  1188. var a10 = a[3],
  1189. a11 = a[4],
  1190. a12 = a[5];
  1191. var a20 = a[6],
  1192. a21 = a[7],
  1193. a22 = a[8];
  1194. var b01 = a22 * a11 - a12 * a21;
  1195. var b11 = -a22 * a10 + a12 * a20;
  1196. var b21 = a21 * a10 - a11 * a20; // Calculate the determinant
  1197. var det = a00 * b01 + a01 * b11 + a02 * b21;
  1198. if (!det) {
  1199. return null;
  1200. }
  1201. det = 1.0 / det;
  1202. out[0] = b01 * det;
  1203. out[1] = (-a22 * a01 + a02 * a21) * det;
  1204. out[2] = (a12 * a01 - a02 * a11) * det;
  1205. out[3] = b11 * det;
  1206. out[4] = (a22 * a00 - a02 * a20) * det;
  1207. out[5] = (-a12 * a00 + a02 * a10) * det;
  1208. out[6] = b21 * det;
  1209. out[7] = (-a21 * a00 + a01 * a20) * det;
  1210. out[8] = (a11 * a00 - a01 * a10) * det;
  1211. return out;
  1212. }
  1213. /**
  1214. * Calculates the adjugate of a mat3
  1215. *
  1216. * @param {mat3} out the receiving matrix
  1217. * @param {ReadonlyMat3} a the source matrix
  1218. * @returns {mat3} out
  1219. */
  1220. function adjoint$1(out, a) {
  1221. var a00 = a[0],
  1222. a01 = a[1],
  1223. a02 = a[2];
  1224. var a10 = a[3],
  1225. a11 = a[4],
  1226. a12 = a[5];
  1227. var a20 = a[6],
  1228. a21 = a[7],
  1229. a22 = a[8];
  1230. out[0] = a11 * a22 - a12 * a21;
  1231. out[1] = a02 * a21 - a01 * a22;
  1232. out[2] = a01 * a12 - a02 * a11;
  1233. out[3] = a12 * a20 - a10 * a22;
  1234. out[4] = a00 * a22 - a02 * a20;
  1235. out[5] = a02 * a10 - a00 * a12;
  1236. out[6] = a10 * a21 - a11 * a20;
  1237. out[7] = a01 * a20 - a00 * a21;
  1238. out[8] = a00 * a11 - a01 * a10;
  1239. return out;
  1240. }
  1241. /**
  1242. * Calculates the determinant of a mat3
  1243. *
  1244. * @param {ReadonlyMat3} a the source matrix
  1245. * @returns {Number} determinant of a
  1246. */
  1247. function determinant$2(a) {
  1248. var a00 = a[0],
  1249. a01 = a[1],
  1250. a02 = a[2];
  1251. var a10 = a[3],
  1252. a11 = a[4],
  1253. a12 = a[5];
  1254. var a20 = a[6],
  1255. a21 = a[7],
  1256. a22 = a[8];
  1257. return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20);
  1258. }
  1259. /**
  1260. * Multiplies two mat3's
  1261. *
  1262. * @param {mat3} out the receiving matrix
  1263. * @param {ReadonlyMat3} a the first operand
  1264. * @param {ReadonlyMat3} b the second operand
  1265. * @returns {mat3} out
  1266. */
  1267. function multiply$2(out, a, b) {
  1268. var a00 = a[0],
  1269. a01 = a[1],
  1270. a02 = a[2];
  1271. var a10 = a[3],
  1272. a11 = a[4],
  1273. a12 = a[5];
  1274. var a20 = a[6],
  1275. a21 = a[7],
  1276. a22 = a[8];
  1277. var b00 = b[0],
  1278. b01 = b[1],
  1279. b02 = b[2];
  1280. var b10 = b[3],
  1281. b11 = b[4],
  1282. b12 = b[5];
  1283. var b20 = b[6],
  1284. b21 = b[7],
  1285. b22 = b[8];
  1286. out[0] = b00 * a00 + b01 * a10 + b02 * a20;
  1287. out[1] = b00 * a01 + b01 * a11 + b02 * a21;
  1288. out[2] = b00 * a02 + b01 * a12 + b02 * a22;
  1289. out[3] = b10 * a00 + b11 * a10 + b12 * a20;
  1290. out[4] = b10 * a01 + b11 * a11 + b12 * a21;
  1291. out[5] = b10 * a02 + b11 * a12 + b12 * a22;
  1292. out[6] = b20 * a00 + b21 * a10 + b22 * a20;
  1293. out[7] = b20 * a01 + b21 * a11 + b22 * a21;
  1294. out[8] = b20 * a02 + b21 * a12 + b22 * a22;
  1295. return out;
  1296. }
  1297. /**
  1298. * Translate a mat3 by the given vector
  1299. *
  1300. * @param {mat3} out the receiving matrix
  1301. * @param {ReadonlyMat3} a the matrix to translate
  1302. * @param {ReadonlyVec2} v vector to translate by
  1303. * @returns {mat3} out
  1304. */
  1305. function translate$1(out, a, v) {
  1306. var a00 = a[0],
  1307. a01 = a[1],
  1308. a02 = a[2],
  1309. a10 = a[3],
  1310. a11 = a[4],
  1311. a12 = a[5],
  1312. a20 = a[6],
  1313. a21 = a[7],
  1314. a22 = a[8],
  1315. x = v[0],
  1316. y = v[1];
  1317. out[0] = a00;
  1318. out[1] = a01;
  1319. out[2] = a02;
  1320. out[3] = a10;
  1321. out[4] = a11;
  1322. out[5] = a12;
  1323. out[6] = x * a00 + y * a10 + a20;
  1324. out[7] = x * a01 + y * a11 + a21;
  1325. out[8] = x * a02 + y * a12 + a22;
  1326. return out;
  1327. }
  1328. /**
  1329. * Rotates a mat3 by the given angle
  1330. *
  1331. * @param {mat3} out the receiving matrix
  1332. * @param {ReadonlyMat3} a the matrix to rotate
  1333. * @param {Number} rad the angle to rotate the matrix by
  1334. * @returns {mat3} out
  1335. */
  1336. function rotate$2(out, a, rad) {
  1337. var a00 = a[0],
  1338. a01 = a[1],
  1339. a02 = a[2],
  1340. a10 = a[3],
  1341. a11 = a[4],
  1342. a12 = a[5],
  1343. a20 = a[6],
  1344. a21 = a[7],
  1345. a22 = a[8],
  1346. s = Math.sin(rad),
  1347. c = Math.cos(rad);
  1348. out[0] = c * a00 + s * a10;
  1349. out[1] = c * a01 + s * a11;
  1350. out[2] = c * a02 + s * a12;
  1351. out[3] = c * a10 - s * a00;
  1352. out[4] = c * a11 - s * a01;
  1353. out[5] = c * a12 - s * a02;
  1354. out[6] = a20;
  1355. out[7] = a21;
  1356. out[8] = a22;
  1357. return out;
  1358. }
  1359. /**
  1360. * Scales the mat3 by the dimensions in the given vec2
  1361. *
  1362. * @param {mat3} out the receiving matrix
  1363. * @param {ReadonlyMat3} a the matrix to rotate
  1364. * @param {ReadonlyVec2} v the vec2 to scale the matrix by
  1365. * @returns {mat3} out
  1366. **/
  1367. function scale$2(out, a, v) {
  1368. var x = v[0],
  1369. y = v[1];
  1370. out[0] = x * a[0];
  1371. out[1] = x * a[1];
  1372. out[2] = x * a[2];
  1373. out[3] = y * a[3];
  1374. out[4] = y * a[4];
  1375. out[5] = y * a[5];
  1376. out[6] = a[6];
  1377. out[7] = a[7];
  1378. out[8] = a[8];
  1379. return out;
  1380. }
  1381. /**
  1382. * Creates a matrix from a vector translation
  1383. * This is equivalent to (but much faster than):
  1384. *
  1385. * mat3.identity(dest);
  1386. * mat3.translate(dest, dest, vec);
  1387. *
  1388. * @param {mat3} out mat3 receiving operation result
  1389. * @param {ReadonlyVec2} v Translation vector
  1390. * @returns {mat3} out
  1391. */
  1392. function fromTranslation$1(out, v) {
  1393. out[0] = 1;
  1394. out[1] = 0;
  1395. out[2] = 0;
  1396. out[3] = 0;
  1397. out[4] = 1;
  1398. out[5] = 0;
  1399. out[6] = v[0];
  1400. out[7] = v[1];
  1401. out[8] = 1;
  1402. return out;
  1403. }
  1404. /**
  1405. * Creates a matrix from a given angle
  1406. * This is equivalent to (but much faster than):
  1407. *
  1408. * mat3.identity(dest);
  1409. * mat3.rotate(dest, dest, rad);
  1410. *
  1411. * @param {mat3} out mat3 receiving operation result
  1412. * @param {Number} rad the angle to rotate the matrix by
  1413. * @returns {mat3} out
  1414. */
  1415. function fromRotation$2(out, rad) {
  1416. var s = Math.sin(rad),
  1417. c = Math.cos(rad);
  1418. out[0] = c;
  1419. out[1] = s;
  1420. out[2] = 0;
  1421. out[3] = -s;
  1422. out[4] = c;
  1423. out[5] = 0;
  1424. out[6] = 0;
  1425. out[7] = 0;
  1426. out[8] = 1;
  1427. return out;
  1428. }
  1429. /**
  1430. * Creates a matrix from a vector scaling
  1431. * This is equivalent to (but much faster than):
  1432. *
  1433. * mat3.identity(dest);
  1434. * mat3.scale(dest, dest, vec);
  1435. *
  1436. * @param {mat3} out mat3 receiving operation result
  1437. * @param {ReadonlyVec2} v Scaling vector
  1438. * @returns {mat3} out
  1439. */
  1440. function fromScaling$2(out, v) {
  1441. out[0] = v[0];
  1442. out[1] = 0;
  1443. out[2] = 0;
  1444. out[3] = 0;
  1445. out[4] = v[1];
  1446. out[5] = 0;
  1447. out[6] = 0;
  1448. out[7] = 0;
  1449. out[8] = 1;
  1450. return out;
  1451. }
  1452. /**
  1453. * Copies the values from a mat2d into a mat3
  1454. *
  1455. * @param {mat3} out the receiving matrix
  1456. * @param {ReadonlyMat2d} a the matrix to copy
  1457. * @returns {mat3} out
  1458. **/
  1459. function fromMat2d(out, a) {
  1460. out[0] = a[0];
  1461. out[1] = a[1];
  1462. out[2] = 0;
  1463. out[3] = a[2];
  1464. out[4] = a[3];
  1465. out[5] = 0;
  1466. out[6] = a[4];
  1467. out[7] = a[5];
  1468. out[8] = 1;
  1469. return out;
  1470. }
  1471. /**
  1472. * Calculates a 3x3 matrix from the given quaternion
  1473. *
  1474. * @param {mat3} out mat3 receiving operation result
  1475. * @param {ReadonlyQuat} q Quaternion to create matrix from
  1476. *
  1477. * @returns {mat3} out
  1478. */
  1479. function fromQuat(out, q) {
  1480. var x = q[0],
  1481. y = q[1],
  1482. z = q[2],
  1483. w = q[3];
  1484. var x2 = x + x;
  1485. var y2 = y + y;
  1486. var z2 = z + z;
  1487. var xx = x * x2;
  1488. var yx = y * x2;
  1489. var yy = y * y2;
  1490. var zx = z * x2;
  1491. var zy = z * y2;
  1492. var zz = z * z2;
  1493. var wx = w * x2;
  1494. var wy = w * y2;
  1495. var wz = w * z2;
  1496. out[0] = 1 - yy - zz;
  1497. out[3] = yx - wz;
  1498. out[6] = zx + wy;
  1499. out[1] = yx + wz;
  1500. out[4] = 1 - xx - zz;
  1501. out[7] = zy - wx;
  1502. out[2] = zx - wy;
  1503. out[5] = zy + wx;
  1504. out[8] = 1 - xx - yy;
  1505. return out;
  1506. }
  1507. /**
  1508. * Calculates a 3x3 normal matrix (transpose inverse) from the 4x4 matrix
  1509. *
  1510. * @param {mat3} out mat3 receiving operation result
  1511. * @param {ReadonlyMat4} a Mat4 to derive the normal matrix from
  1512. *
  1513. * @returns {mat3} out
  1514. */
  1515. function normalFromMat4(out, a) {
  1516. var a00 = a[0],
  1517. a01 = a[1],
  1518. a02 = a[2],
  1519. a03 = a[3];
  1520. var a10 = a[4],
  1521. a11 = a[5],
  1522. a12 = a[6],
  1523. a13 = a[7];
  1524. var a20 = a[8],
  1525. a21 = a[9],
  1526. a22 = a[10],
  1527. a23 = a[11];
  1528. var a30 = a[12],
  1529. a31 = a[13],
  1530. a32 = a[14],
  1531. a33 = a[15];
  1532. var b00 = a00 * a11 - a01 * a10;
  1533. var b01 = a00 * a12 - a02 * a10;
  1534. var b02 = a00 * a13 - a03 * a10;
  1535. var b03 = a01 * a12 - a02 * a11;
  1536. var b04 = a01 * a13 - a03 * a11;
  1537. var b05 = a02 * a13 - a03 * a12;
  1538. var b06 = a20 * a31 - a21 * a30;
  1539. var b07 = a20 * a32 - a22 * a30;
  1540. var b08 = a20 * a33 - a23 * a30;
  1541. var b09 = a21 * a32 - a22 * a31;
  1542. var b10 = a21 * a33 - a23 * a31;
  1543. var b11 = a22 * a33 - a23 * a32; // Calculate the determinant
  1544. var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  1545. if (!det) {
  1546. return null;
  1547. }
  1548. det = 1.0 / det;
  1549. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  1550. out[1] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  1551. out[2] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  1552. out[3] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  1553. out[4] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  1554. out[5] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  1555. out[6] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  1556. out[7] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  1557. out[8] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  1558. return out;
  1559. }
  1560. /**
  1561. * Generates a 2D projection matrix with the given bounds
  1562. *
  1563. * @param {mat3} out mat3 frustum matrix will be written into
  1564. * @param {number} width Width of your gl context
  1565. * @param {number} height Height of gl context
  1566. * @returns {mat3} out
  1567. */
  1568. function projection(out, width, height) {
  1569. out[0] = 2 / width;
  1570. out[1] = 0;
  1571. out[2] = 0;
  1572. out[3] = 0;
  1573. out[4] = -2 / height;
  1574. out[5] = 0;
  1575. out[6] = -1;
  1576. out[7] = 1;
  1577. out[8] = 1;
  1578. return out;
  1579. }
  1580. /**
  1581. * Returns a string representation of a mat3
  1582. *
  1583. * @param {ReadonlyMat3} a matrix to represent as a string
  1584. * @returns {String} string representation of the matrix
  1585. */
  1586. function str$2(a) {
  1587. return "mat3(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ", " + a[6] + ", " + a[7] + ", " + a[8] + ")";
  1588. }
  1589. /**
  1590. * Returns Frobenius norm of a mat3
  1591. *
  1592. * @param {ReadonlyMat3} a the matrix to calculate Frobenius norm of
  1593. * @returns {Number} Frobenius norm
  1594. */
  1595. function frob$2(a) {
  1596. return Math.hypot(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8]);
  1597. }
  1598. /**
  1599. * Adds two mat3's
  1600. *
  1601. * @param {mat3} out the receiving matrix
  1602. * @param {ReadonlyMat3} a the first operand
  1603. * @param {ReadonlyMat3} b the second operand
  1604. * @returns {mat3} out
  1605. */
  1606. function add$2(out, a, b) {
  1607. out[0] = a[0] + b[0];
  1608. out[1] = a[1] + b[1];
  1609. out[2] = a[2] + b[2];
  1610. out[3] = a[3] + b[3];
  1611. out[4] = a[4] + b[4];
  1612. out[5] = a[5] + b[5];
  1613. out[6] = a[6] + b[6];
  1614. out[7] = a[7] + b[7];
  1615. out[8] = a[8] + b[8];
  1616. return out;
  1617. }
  1618. /**
  1619. * Subtracts matrix b from matrix a
  1620. *
  1621. * @param {mat3} out the receiving matrix
  1622. * @param {ReadonlyMat3} a the first operand
  1623. * @param {ReadonlyMat3} b the second operand
  1624. * @returns {mat3} out
  1625. */
  1626. function subtract$2(out, a, b) {
  1627. out[0] = a[0] - b[0];
  1628. out[1] = a[1] - b[1];
  1629. out[2] = a[2] - b[2];
  1630. out[3] = a[3] - b[3];
  1631. out[4] = a[4] - b[4];
  1632. out[5] = a[5] - b[5];
  1633. out[6] = a[6] - b[6];
  1634. out[7] = a[7] - b[7];
  1635. out[8] = a[8] - b[8];
  1636. return out;
  1637. }
  1638. /**
  1639. * Multiply each element of the matrix by a scalar.
  1640. *
  1641. * @param {mat3} out the receiving matrix
  1642. * @param {ReadonlyMat3} a the matrix to scale
  1643. * @param {Number} b amount to scale the matrix's elements by
  1644. * @returns {mat3} out
  1645. */
  1646. function multiplyScalar$2(out, a, b) {
  1647. out[0] = a[0] * b;
  1648. out[1] = a[1] * b;
  1649. out[2] = a[2] * b;
  1650. out[3] = a[3] * b;
  1651. out[4] = a[4] * b;
  1652. out[5] = a[5] * b;
  1653. out[6] = a[6] * b;
  1654. out[7] = a[7] * b;
  1655. out[8] = a[8] * b;
  1656. return out;
  1657. }
  1658. /**
  1659. * Adds two mat3's after multiplying each element of the second operand by a scalar value.
  1660. *
  1661. * @param {mat3} out the receiving vector
  1662. * @param {ReadonlyMat3} a the first operand
  1663. * @param {ReadonlyMat3} b the second operand
  1664. * @param {Number} scale the amount to scale b's elements by before adding
  1665. * @returns {mat3} out
  1666. */
  1667. function multiplyScalarAndAdd$2(out, a, b, scale) {
  1668. out[0] = a[0] + b[0] * scale;
  1669. out[1] = a[1] + b[1] * scale;
  1670. out[2] = a[2] + b[2] * scale;
  1671. out[3] = a[3] + b[3] * scale;
  1672. out[4] = a[4] + b[4] * scale;
  1673. out[5] = a[5] + b[5] * scale;
  1674. out[6] = a[6] + b[6] * scale;
  1675. out[7] = a[7] + b[7] * scale;
  1676. out[8] = a[8] + b[8] * scale;
  1677. return out;
  1678. }
  1679. /**
  1680. * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
  1681. *
  1682. * @param {ReadonlyMat3} a The first matrix.
  1683. * @param {ReadonlyMat3} b The second matrix.
  1684. * @returns {Boolean} True if the matrices are equal, false otherwise.
  1685. */
  1686. function exactEquals$2(a, b) {
  1687. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && a[8] === b[8];
  1688. }
  1689. /**
  1690. * Returns whether or not the matrices have approximately the same elements in the same position.
  1691. *
  1692. * @param {ReadonlyMat3} a The first matrix.
  1693. * @param {ReadonlyMat3} b The second matrix.
  1694. * @returns {Boolean} True if the matrices are equal, false otherwise.
  1695. */
  1696. function equals$3(a, b) {
  1697. var a0 = a[0],
  1698. a1 = a[1],
  1699. a2 = a[2],
  1700. a3 = a[3],
  1701. a4 = a[4],
  1702. a5 = a[5],
  1703. a6 = a[6],
  1704. a7 = a[7],
  1705. a8 = a[8];
  1706. var b0 = b[0],
  1707. b1 = b[1],
  1708. b2 = b[2],
  1709. b3 = b[3],
  1710. b4 = b[4],
  1711. b5 = b[5],
  1712. b6 = b[6],
  1713. b7 = b[7],
  1714. b8 = b[8];
  1715. return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8));
  1716. }
  1717. /**
  1718. * Alias for {@link mat3.multiply}
  1719. * @function
  1720. */
  1721. var mul$2 = multiply$2;
  1722. /**
  1723. * Alias for {@link mat3.subtract}
  1724. * @function
  1725. */
  1726. var sub$2 = subtract$2;
  1727. var mat3 = /*#__PURE__*/Object.freeze({
  1728. __proto__: null,
  1729. create: create$2,
  1730. fromMat4: fromMat4,
  1731. clone: clone$2,
  1732. copy: copy$2,
  1733. fromValues: fromValues$2,
  1734. set: set$2,
  1735. identity: identity$2,
  1736. transpose: transpose$1,
  1737. invert: invert$2,
  1738. adjoint: adjoint$1,
  1739. determinant: determinant$2,
  1740. multiply: multiply$2,
  1741. translate: translate$1,
  1742. rotate: rotate$2,
  1743. scale: scale$2,
  1744. fromTranslation: fromTranslation$1,
  1745. fromRotation: fromRotation$2,
  1746. fromScaling: fromScaling$2,
  1747. fromMat2d: fromMat2d,
  1748. fromQuat: fromQuat,
  1749. normalFromMat4: normalFromMat4,
  1750. projection: projection,
  1751. str: str$2,
  1752. frob: frob$2,
  1753. add: add$2,
  1754. subtract: subtract$2,
  1755. multiplyScalar: multiplyScalar$2,
  1756. multiplyScalarAndAdd: multiplyScalarAndAdd$2,
  1757. exactEquals: exactEquals$2,
  1758. equals: equals$3,
  1759. mul: mul$2,
  1760. sub: sub$2
  1761. });
  1762. /**
  1763. * 4x4 Matrix<br>Format: column-major, when typed out it looks like row-major<br>The matrices are being post multiplied.
  1764. * @module mat4
  1765. */
  1766. /**
  1767. * Creates a new identity mat4
  1768. *
  1769. * @returns {mat4} a new 4x4 matrix
  1770. */
  1771. function create$3() {
  1772. var out = new ARRAY_TYPE(16);
  1773. if (ARRAY_TYPE != Float32Array) {
  1774. out[1] = 0;
  1775. out[2] = 0;
  1776. out[3] = 0;
  1777. out[4] = 0;
  1778. out[6] = 0;
  1779. out[7] = 0;
  1780. out[8] = 0;
  1781. out[9] = 0;
  1782. out[11] = 0;
  1783. out[12] = 0;
  1784. out[13] = 0;
  1785. out[14] = 0;
  1786. }
  1787. out[0] = 1;
  1788. out[5] = 1;
  1789. out[10] = 1;
  1790. out[15] = 1;
  1791. return out;
  1792. }
  1793. /**
  1794. * Creates a new mat4 initialized with values from an existing matrix
  1795. *
  1796. * @param {ReadonlyMat4} a matrix to clone
  1797. * @returns {mat4} a new 4x4 matrix
  1798. */
  1799. function clone$3(a) {
  1800. var out = new ARRAY_TYPE(16);
  1801. out[0] = a[0];
  1802. out[1] = a[1];
  1803. out[2] = a[2];
  1804. out[3] = a[3];
  1805. out[4] = a[4];
  1806. out[5] = a[5];
  1807. out[6] = a[6];
  1808. out[7] = a[7];
  1809. out[8] = a[8];
  1810. out[9] = a[9];
  1811. out[10] = a[10];
  1812. out[11] = a[11];
  1813. out[12] = a[12];
  1814. out[13] = a[13];
  1815. out[14] = a[14];
  1816. out[15] = a[15];
  1817. return out;
  1818. }
  1819. /**
  1820. * Copy the values from one mat4 to another
  1821. *
  1822. * @param {mat4} out the receiving matrix
  1823. * @param {ReadonlyMat4} a the source matrix
  1824. * @returns {mat4} out
  1825. */
  1826. function copy$3(out, a) {
  1827. out[0] = a[0];
  1828. out[1] = a[1];
  1829. out[2] = a[2];
  1830. out[3] = a[3];
  1831. out[4] = a[4];
  1832. out[5] = a[5];
  1833. out[6] = a[6];
  1834. out[7] = a[7];
  1835. out[8] = a[8];
  1836. out[9] = a[9];
  1837. out[10] = a[10];
  1838. out[11] = a[11];
  1839. out[12] = a[12];
  1840. out[13] = a[13];
  1841. out[14] = a[14];
  1842. out[15] = a[15];
  1843. return out;
  1844. }
  1845. /**
  1846. * Create a new mat4 with the given values
  1847. *
  1848. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  1849. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  1850. * @param {Number} m02 Component in column 0, row 2 position (index 2)
  1851. * @param {Number} m03 Component in column 0, row 3 position (index 3)
  1852. * @param {Number} m10 Component in column 1, row 0 position (index 4)
  1853. * @param {Number} m11 Component in column 1, row 1 position (index 5)
  1854. * @param {Number} m12 Component in column 1, row 2 position (index 6)
  1855. * @param {Number} m13 Component in column 1, row 3 position (index 7)
  1856. * @param {Number} m20 Component in column 2, row 0 position (index 8)
  1857. * @param {Number} m21 Component in column 2, row 1 position (index 9)
  1858. * @param {Number} m22 Component in column 2, row 2 position (index 10)
  1859. * @param {Number} m23 Component in column 2, row 3 position (index 11)
  1860. * @param {Number} m30 Component in column 3, row 0 position (index 12)
  1861. * @param {Number} m31 Component in column 3, row 1 position (index 13)
  1862. * @param {Number} m32 Component in column 3, row 2 position (index 14)
  1863. * @param {Number} m33 Component in column 3, row 3 position (index 15)
  1864. * @returns {mat4} A new mat4
  1865. */
  1866. function fromValues$3(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
  1867. var out = new ARRAY_TYPE(16);
  1868. out[0] = m00;
  1869. out[1] = m01;
  1870. out[2] = m02;
  1871. out[3] = m03;
  1872. out[4] = m10;
  1873. out[5] = m11;
  1874. out[6] = m12;
  1875. out[7] = m13;
  1876. out[8] = m20;
  1877. out[9] = m21;
  1878. out[10] = m22;
  1879. out[11] = m23;
  1880. out[12] = m30;
  1881. out[13] = m31;
  1882. out[14] = m32;
  1883. out[15] = m33;
  1884. return out;
  1885. }
  1886. /**
  1887. * Set the components of a mat4 to the given values
  1888. *
  1889. * @param {mat4} out the receiving matrix
  1890. * @param {Number} m00 Component in column 0, row 0 position (index 0)
  1891. * @param {Number} m01 Component in column 0, row 1 position (index 1)
  1892. * @param {Number} m02 Component in column 0, row 2 position (index 2)
  1893. * @param {Number} m03 Component in column 0, row 3 position (index 3)
  1894. * @param {Number} m10 Component in column 1, row 0 position (index 4)
  1895. * @param {Number} m11 Component in column 1, row 1 position (index 5)
  1896. * @param {Number} m12 Component in column 1, row 2 position (index 6)
  1897. * @param {Number} m13 Component in column 1, row 3 position (index 7)
  1898. * @param {Number} m20 Component in column 2, row 0 position (index 8)
  1899. * @param {Number} m21 Component in column 2, row 1 position (index 9)
  1900. * @param {Number} m22 Component in column 2, row 2 position (index 10)
  1901. * @param {Number} m23 Component in column 2, row 3 position (index 11)
  1902. * @param {Number} m30 Component in column 3, row 0 position (index 12)
  1903. * @param {Number} m31 Component in column 3, row 1 position (index 13)
  1904. * @param {Number} m32 Component in column 3, row 2 position (index 14)
  1905. * @param {Number} m33 Component in column 3, row 3 position (index 15)
  1906. * @returns {mat4} out
  1907. */
  1908. function set$3(out, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) {
  1909. out[0] = m00;
  1910. out[1] = m01;
  1911. out[2] = m02;
  1912. out[3] = m03;
  1913. out[4] = m10;
  1914. out[5] = m11;
  1915. out[6] = m12;
  1916. out[7] = m13;
  1917. out[8] = m20;
  1918. out[9] = m21;
  1919. out[10] = m22;
  1920. out[11] = m23;
  1921. out[12] = m30;
  1922. out[13] = m31;
  1923. out[14] = m32;
  1924. out[15] = m33;
  1925. return out;
  1926. }
  1927. /**
  1928. * Set a mat4 to the identity matrix
  1929. *
  1930. * @param {mat4} out the receiving matrix
  1931. * @returns {mat4} out
  1932. */
  1933. function identity$3(out) {
  1934. out[0] = 1;
  1935. out[1] = 0;
  1936. out[2] = 0;
  1937. out[3] = 0;
  1938. out[4] = 0;
  1939. out[5] = 1;
  1940. out[6] = 0;
  1941. out[7] = 0;
  1942. out[8] = 0;
  1943. out[9] = 0;
  1944. out[10] = 1;
  1945. out[11] = 0;
  1946. out[12] = 0;
  1947. out[13] = 0;
  1948. out[14] = 0;
  1949. out[15] = 1;
  1950. return out;
  1951. }
  1952. /**
  1953. * Transpose the values of a mat4
  1954. *
  1955. * @param {mat4} out the receiving matrix
  1956. * @param {ReadonlyMat4} a the source matrix
  1957. * @returns {mat4} out
  1958. */
  1959. function transpose$2(out, a) {
  1960. // If we are transposing ourselves we can skip a few steps but have to cache some values
  1961. if (out === a) {
  1962. var a01 = a[1],
  1963. a02 = a[2],
  1964. a03 = a[3];
  1965. var a12 = a[6],
  1966. a13 = a[7];
  1967. var a23 = a[11];
  1968. out[1] = a[4];
  1969. out[2] = a[8];
  1970. out[3] = a[12];
  1971. out[4] = a01;
  1972. out[6] = a[9];
  1973. out[7] = a[13];
  1974. out[8] = a02;
  1975. out[9] = a12;
  1976. out[11] = a[14];
  1977. out[12] = a03;
  1978. out[13] = a13;
  1979. out[14] = a23;
  1980. } else {
  1981. out[0] = a[0];
  1982. out[1] = a[4];
  1983. out[2] = a[8];
  1984. out[3] = a[12];
  1985. out[4] = a[1];
  1986. out[5] = a[5];
  1987. out[6] = a[9];
  1988. out[7] = a[13];
  1989. out[8] = a[2];
  1990. out[9] = a[6];
  1991. out[10] = a[10];
  1992. out[11] = a[14];
  1993. out[12] = a[3];
  1994. out[13] = a[7];
  1995. out[14] = a[11];
  1996. out[15] = a[15];
  1997. }
  1998. return out;
  1999. }
  2000. /**
  2001. * Inverts a mat4
  2002. *
  2003. * @param {mat4} out the receiving matrix
  2004. * @param {ReadonlyMat4} a the source matrix
  2005. * @returns {mat4} out
  2006. */
  2007. function invert$3(out, a) {
  2008. var a00 = a[0],
  2009. a01 = a[1],
  2010. a02 = a[2],
  2011. a03 = a[3];
  2012. var a10 = a[4],
  2013. a11 = a[5],
  2014. a12 = a[6],
  2015. a13 = a[7];
  2016. var a20 = a[8],
  2017. a21 = a[9],
  2018. a22 = a[10],
  2019. a23 = a[11];
  2020. var a30 = a[12],
  2021. a31 = a[13],
  2022. a32 = a[14],
  2023. a33 = a[15];
  2024. var b00 = a00 * a11 - a01 * a10;
  2025. var b01 = a00 * a12 - a02 * a10;
  2026. var b02 = a00 * a13 - a03 * a10;
  2027. var b03 = a01 * a12 - a02 * a11;
  2028. var b04 = a01 * a13 - a03 * a11;
  2029. var b05 = a02 * a13 - a03 * a12;
  2030. var b06 = a20 * a31 - a21 * a30;
  2031. var b07 = a20 * a32 - a22 * a30;
  2032. var b08 = a20 * a33 - a23 * a30;
  2033. var b09 = a21 * a32 - a22 * a31;
  2034. var b10 = a21 * a33 - a23 * a31;
  2035. var b11 = a22 * a33 - a23 * a32; // Calculate the determinant
  2036. var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  2037. if (!det) {
  2038. return null;
  2039. }
  2040. det = 1.0 / det;
  2041. out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det;
  2042. out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det;
  2043. out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det;
  2044. out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det;
  2045. out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det;
  2046. out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det;
  2047. out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det;
  2048. out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det;
  2049. out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det;
  2050. out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det;
  2051. out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det;
  2052. out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det;
  2053. out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det;
  2054. out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det;
  2055. out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det;
  2056. out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det;
  2057. return out;
  2058. }
  2059. /**
  2060. * Calculates the adjugate of a mat4
  2061. *
  2062. * @param {mat4} out the receiving matrix
  2063. * @param {ReadonlyMat4} a the source matrix
  2064. * @returns {mat4} out
  2065. */
  2066. function adjoint$2(out, a) {
  2067. var a00 = a[0],
  2068. a01 = a[1],
  2069. a02 = a[2],
  2070. a03 = a[3];
  2071. var a10 = a[4],
  2072. a11 = a[5],
  2073. a12 = a[6],
  2074. a13 = a[7];
  2075. var a20 = a[8],
  2076. a21 = a[9],
  2077. a22 = a[10],
  2078. a23 = a[11];
  2079. var a30 = a[12],
  2080. a31 = a[13],
  2081. a32 = a[14],
  2082. a33 = a[15];
  2083. out[0] = a11 * (a22 * a33 - a23 * a32) - a21 * (a12 * a33 - a13 * a32) + a31 * (a12 * a23 - a13 * a22);
  2084. out[1] = -(a01 * (a22 * a33 - a23 * a32) - a21 * (a02 * a33 - a03 * a32) + a31 * (a02 * a23 - a03 * a22));
  2085. out[2] = a01 * (a12 * a33 - a13 * a32) - a11 * (a02 * a33 - a03 * a32) + a31 * (a02 * a13 - a03 * a12);
  2086. out[3] = -(a01 * (a12 * a23 - a13 * a22) - a11 * (a02 * a23 - a03 * a22) + a21 * (a02 * a13 - a03 * a12));
  2087. out[4] = -(a10 * (a22 * a33 - a23 * a32) - a20 * (a12 * a33 - a13 * a32) + a30 * (a12 * a23 - a13 * a22));
  2088. out[5] = a00 * (a22 * a33 - a23 * a32) - a20 * (a02 * a33 - a03 * a32) + a30 * (a02 * a23 - a03 * a22);
  2089. out[6] = -(a00 * (a12 * a33 - a13 * a32) - a10 * (a02 * a33 - a03 * a32) + a30 * (a02 * a13 - a03 * a12));
  2090. out[7] = a00 * (a12 * a23 - a13 * a22) - a10 * (a02 * a23 - a03 * a22) + a20 * (a02 * a13 - a03 * a12);
  2091. out[8] = a10 * (a21 * a33 - a23 * a31) - a20 * (a11 * a33 - a13 * a31) + a30 * (a11 * a23 - a13 * a21);
  2092. out[9] = -(a00 * (a21 * a33 - a23 * a31) - a20 * (a01 * a33 - a03 * a31) + a30 * (a01 * a23 - a03 * a21));
  2093. out[10] = a00 * (a11 * a33 - a13 * a31) - a10 * (a01 * a33 - a03 * a31) + a30 * (a01 * a13 - a03 * a11);
  2094. out[11] = -(a00 * (a11 * a23 - a13 * a21) - a10 * (a01 * a23 - a03 * a21) + a20 * (a01 * a13 - a03 * a11));
  2095. out[12] = -(a10 * (a21 * a32 - a22 * a31) - a20 * (a11 * a32 - a12 * a31) + a30 * (a11 * a22 - a12 * a21));
  2096. out[13] = a00 * (a21 * a32 - a22 * a31) - a20 * (a01 * a32 - a02 * a31) + a30 * (a01 * a22 - a02 * a21);
  2097. out[14] = -(a00 * (a11 * a32 - a12 * a31) - a10 * (a01 * a32 - a02 * a31) + a30 * (a01 * a12 - a02 * a11));
  2098. out[15] = a00 * (a11 * a22 - a12 * a21) - a10 * (a01 * a22 - a02 * a21) + a20 * (a01 * a12 - a02 * a11);
  2099. return out;
  2100. }
  2101. /**
  2102. * Calculates the determinant of a mat4
  2103. *
  2104. * @param {ReadonlyMat4} a the source matrix
  2105. * @returns {Number} determinant of a
  2106. */
  2107. function determinant$3(a) {
  2108. var a00 = a[0],
  2109. a01 = a[1],
  2110. a02 = a[2],
  2111. a03 = a[3];
  2112. var a10 = a[4],
  2113. a11 = a[5],
  2114. a12 = a[6],
  2115. a13 = a[7];
  2116. var a20 = a[8],
  2117. a21 = a[9],
  2118. a22 = a[10],
  2119. a23 = a[11];
  2120. var a30 = a[12],
  2121. a31 = a[13],
  2122. a32 = a[14],
  2123. a33 = a[15];
  2124. var b00 = a00 * a11 - a01 * a10;
  2125. var b01 = a00 * a12 - a02 * a10;
  2126. var b02 = a00 * a13 - a03 * a10;
  2127. var b03 = a01 * a12 - a02 * a11;
  2128. var b04 = a01 * a13 - a03 * a11;
  2129. var b05 = a02 * a13 - a03 * a12;
  2130. var b06 = a20 * a31 - a21 * a30;
  2131. var b07 = a20 * a32 - a22 * a30;
  2132. var b08 = a20 * a33 - a23 * a30;
  2133. var b09 = a21 * a32 - a22 * a31;
  2134. var b10 = a21 * a33 - a23 * a31;
  2135. var b11 = a22 * a33 - a23 * a32; // Calculate the determinant
  2136. return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06;
  2137. }
  2138. /**
  2139. * Multiplies two mat4s
  2140. *
  2141. * @param {mat4} out the receiving matrix
  2142. * @param {ReadonlyMat4} a the first operand
  2143. * @param {ReadonlyMat4} b the second operand
  2144. * @returns {mat4} out
  2145. */
  2146. function multiply$3(out, a, b) {
  2147. var a00 = a[0],
  2148. a01 = a[1],
  2149. a02 = a[2],
  2150. a03 = a[3];
  2151. var a10 = a[4],
  2152. a11 = a[5],
  2153. a12 = a[6],
  2154. a13 = a[7];
  2155. var a20 = a[8],
  2156. a21 = a[9],
  2157. a22 = a[10],
  2158. a23 = a[11];
  2159. var a30 = a[12],
  2160. a31 = a[13],
  2161. a32 = a[14],
  2162. a33 = a[15]; // Cache only the current line of the second matrix
  2163. var b0 = b[0],
  2164. b1 = b[1],
  2165. b2 = b[2],
  2166. b3 = b[3];
  2167. out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  2168. out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  2169. out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  2170. out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  2171. b0 = b[4];
  2172. b1 = b[5];
  2173. b2 = b[6];
  2174. b3 = b[7];
  2175. out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  2176. out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  2177. out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  2178. out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  2179. b0 = b[8];
  2180. b1 = b[9];
  2181. b2 = b[10];
  2182. b3 = b[11];
  2183. out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  2184. out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  2185. out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  2186. out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  2187. b0 = b[12];
  2188. b1 = b[13];
  2189. b2 = b[14];
  2190. b3 = b[15];
  2191. out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30;
  2192. out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31;
  2193. out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32;
  2194. out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33;
  2195. return out;
  2196. }
  2197. /**
  2198. * Translate a mat4 by the given vector
  2199. *
  2200. * @param {mat4} out the receiving matrix
  2201. * @param {ReadonlyMat4} a the matrix to translate
  2202. * @param {ReadonlyVec3} v vector to translate by
  2203. * @returns {mat4} out
  2204. */
  2205. function translate$2(out, a, v) {
  2206. var x = v[0],
  2207. y = v[1],
  2208. z = v[2];
  2209. var a00, a01, a02, a03;
  2210. var a10, a11, a12, a13;
  2211. var a20, a21, a22, a23;
  2212. if (a === out) {
  2213. out[12] = a[0] * x + a[4] * y + a[8] * z + a[12];
  2214. out[13] = a[1] * x + a[5] * y + a[9] * z + a[13];
  2215. out[14] = a[2] * x + a[6] * y + a[10] * z + a[14];
  2216. out[15] = a[3] * x + a[7] * y + a[11] * z + a[15];
  2217. } else {
  2218. a00 = a[0];
  2219. a01 = a[1];
  2220. a02 = a[2];
  2221. a03 = a[3];
  2222. a10 = a[4];
  2223. a11 = a[5];
  2224. a12 = a[6];
  2225. a13 = a[7];
  2226. a20 = a[8];
  2227. a21 = a[9];
  2228. a22 = a[10];
  2229. a23 = a[11];
  2230. out[0] = a00;
  2231. out[1] = a01;
  2232. out[2] = a02;
  2233. out[3] = a03;
  2234. out[4] = a10;
  2235. out[5] = a11;
  2236. out[6] = a12;
  2237. out[7] = a13;
  2238. out[8] = a20;
  2239. out[9] = a21;
  2240. out[10] = a22;
  2241. out[11] = a23;
  2242. out[12] = a00 * x + a10 * y + a20 * z + a[12];
  2243. out[13] = a01 * x + a11 * y + a21 * z + a[13];
  2244. out[14] = a02 * x + a12 * y + a22 * z + a[14];
  2245. out[15] = a03 * x + a13 * y + a23 * z + a[15];
  2246. }
  2247. return out;
  2248. }
  2249. /**
  2250. * Scales the mat4 by the dimensions in the given vec3 not using vectorization
  2251. *
  2252. * @param {mat4} out the receiving matrix
  2253. * @param {ReadonlyMat4} a the matrix to scale
  2254. * @param {ReadonlyVec3} v the vec3 to scale the matrix by
  2255. * @returns {mat4} out
  2256. **/
  2257. function scale$3(out, a, v) {
  2258. var x = v[0],
  2259. y = v[1],
  2260. z = v[2];
  2261. out[0] = a[0] * x;
  2262. out[1] = a[1] * x;
  2263. out[2] = a[2] * x;
  2264. out[3] = a[3] * x;
  2265. out[4] = a[4] * y;
  2266. out[5] = a[5] * y;
  2267. out[6] = a[6] * y;
  2268. out[7] = a[7] * y;
  2269. out[8] = a[8] * z;
  2270. out[9] = a[9] * z;
  2271. out[10] = a[10] * z;
  2272. out[11] = a[11] * z;
  2273. out[12] = a[12];
  2274. out[13] = a[13];
  2275. out[14] = a[14];
  2276. out[15] = a[15];
  2277. return out;
  2278. }
  2279. /**
  2280. * Rotates a mat4 by the given angle around the given axis
  2281. *
  2282. * @param {mat4} out the receiving matrix
  2283. * @param {ReadonlyMat4} a the matrix to rotate
  2284. * @param {Number} rad the angle to rotate the matrix by
  2285. * @param {ReadonlyVec3} axis the axis to rotate around
  2286. * @returns {mat4} out
  2287. */
  2288. function rotate$3(out, a, rad, axis) {
  2289. var x = axis[0],
  2290. y = axis[1],
  2291. z = axis[2];
  2292. var len = Math.hypot(x, y, z);
  2293. var s, c, t;
  2294. var a00, a01, a02, a03;
  2295. var a10, a11, a12, a13;
  2296. var a20, a21, a22, a23;
  2297. var b00, b01, b02;
  2298. var b10, b11, b12;
  2299. var b20, b21, b22;
  2300. if (len < EPSILON) {
  2301. return null;
  2302. }
  2303. len = 1 / len;
  2304. x *= len;
  2305. y *= len;
  2306. z *= len;
  2307. s = Math.sin(rad);
  2308. c = Math.cos(rad);
  2309. t = 1 - c;
  2310. a00 = a[0];
  2311. a01 = a[1];
  2312. a02 = a[2];
  2313. a03 = a[3];
  2314. a10 = a[4];
  2315. a11 = a[5];
  2316. a12 = a[6];
  2317. a13 = a[7];
  2318. a20 = a[8];
  2319. a21 = a[9];
  2320. a22 = a[10];
  2321. a23 = a[11]; // Construct the elements of the rotation matrix
  2322. b00 = x * x * t + c;
  2323. b01 = y * x * t + z * s;
  2324. b02 = z * x * t - y * s;
  2325. b10 = x * y * t - z * s;
  2326. b11 = y * y * t + c;
  2327. b12 = z * y * t + x * s;
  2328. b20 = x * z * t + y * s;
  2329. b21 = y * z * t - x * s;
  2330. b22 = z * z * t + c; // Perform rotation-specific matrix multiplication
  2331. out[0] = a00 * b00 + a10 * b01 + a20 * b02;
  2332. out[1] = a01 * b00 + a11 * b01 + a21 * b02;
  2333. out[2] = a02 * b00 + a12 * b01 + a22 * b02;
  2334. out[3] = a03 * b00 + a13 * b01 + a23 * b02;
  2335. out[4] = a00 * b10 + a10 * b11 + a20 * b12;
  2336. out[5] = a01 * b10 + a11 * b11 + a21 * b12;
  2337. out[6] = a02 * b10 + a12 * b11 + a22 * b12;
  2338. out[7] = a03 * b10 + a13 * b11 + a23 * b12;
  2339. out[8] = a00 * b20 + a10 * b21 + a20 * b22;
  2340. out[9] = a01 * b20 + a11 * b21 + a21 * b22;
  2341. out[10] = a02 * b20 + a12 * b21 + a22 * b22;
  2342. out[11] = a03 * b20 + a13 * b21 + a23 * b22;
  2343. if (a !== out) {
  2344. // If the source and destination differ, copy the unchanged last row
  2345. out[12] = a[12];
  2346. out[13] = a[13];
  2347. out[14] = a[14];
  2348. out[15] = a[15];
  2349. }
  2350. return out;
  2351. }
  2352. /**
  2353. * Rotates a matrix by the given angle around the X axis
  2354. *
  2355. * @param {mat4} out the receiving matrix
  2356. * @param {ReadonlyMat4} a the matrix to rotate
  2357. * @param {Number} rad the angle to rotate the matrix by
  2358. * @returns {mat4} out
  2359. */
  2360. function rotateX(out, a, rad) {
  2361. var s = Math.sin(rad);
  2362. var c = Math.cos(rad);
  2363. var a10 = a[4];
  2364. var a11 = a[5];
  2365. var a12 = a[6];
  2366. var a13 = a[7];
  2367. var a20 = a[8];
  2368. var a21 = a[9];
  2369. var a22 = a[10];
  2370. var a23 = a[11];
  2371. if (a !== out) {
  2372. // If the source and destination differ, copy the unchanged rows
  2373. out[0] = a[0];
  2374. out[1] = a[1];
  2375. out[2] = a[2];
  2376. out[3] = a[3];
  2377. out[12] = a[12];
  2378. out[13] = a[13];
  2379. out[14] = a[14];
  2380. out[15] = a[15];
  2381. } // Perform axis-specific matrix multiplication
  2382. out[4] = a10 * c + a20 * s;
  2383. out[5] = a11 * c + a21 * s;
  2384. out[6] = a12 * c + a22 * s;
  2385. out[7] = a13 * c + a23 * s;
  2386. out[8] = a20 * c - a10 * s;
  2387. out[9] = a21 * c - a11 * s;
  2388. out[10] = a22 * c - a12 * s;
  2389. out[11] = a23 * c - a13 * s;
  2390. return out;
  2391. }
  2392. /**
  2393. * Rotates a matrix by the given angle around the Y axis
  2394. *
  2395. * @param {mat4} out the receiving matrix
  2396. * @param {ReadonlyMat4} a the matrix to rotate
  2397. * @param {Number} rad the angle to rotate the matrix by
  2398. * @returns {mat4} out
  2399. */
  2400. function rotateY(out, a, rad) {
  2401. var s = Math.sin(rad);
  2402. var c = Math.cos(rad);
  2403. var a00 = a[0];
  2404. var a01 = a[1];
  2405. var a02 = a[2];
  2406. var a03 = a[3];
  2407. var a20 = a[8];
  2408. var a21 = a[9];
  2409. var a22 = a[10];
  2410. var a23 = a[11];
  2411. if (a !== out) {
  2412. // If the source and destination differ, copy the unchanged rows
  2413. out[4] = a[4];
  2414. out[5] = a[5];
  2415. out[6] = a[6];
  2416. out[7] = a[7];
  2417. out[12] = a[12];
  2418. out[13] = a[13];
  2419. out[14] = a[14];
  2420. out[15] = a[15];
  2421. } // Perform axis-specific matrix multiplication
  2422. out[0] = a00 * c - a20 * s;
  2423. out[1] = a01 * c - a21 * s;
  2424. out[2] = a02 * c - a22 * s;
  2425. out[3] = a03 * c - a23 * s;
  2426. out[8] = a00 * s + a20 * c;
  2427. out[9] = a01 * s + a21 * c;
  2428. out[10] = a02 * s + a22 * c;
  2429. out[11] = a03 * s + a23 * c;
  2430. return out;
  2431. }
  2432. /**
  2433. * Rotates a matrix by the given angle around the Z axis
  2434. *
  2435. * @param {mat4} out the receiving matrix
  2436. * @param {ReadonlyMat4} a the matrix to rotate
  2437. * @param {Number} rad the angle to rotate the matrix by
  2438. * @returns {mat4} out
  2439. */
  2440. function rotateZ(out, a, rad) {
  2441. var s = Math.sin(rad);
  2442. var c = Math.cos(rad);
  2443. var a00 = a[0];
  2444. var a01 = a[1];
  2445. var a02 = a[2];
  2446. var a03 = a[3];
  2447. var a10 = a[4];
  2448. var a11 = a[5];
  2449. var a12 = a[6];
  2450. var a13 = a[7];
  2451. if (a !== out) {
  2452. // If the source and destination differ, copy the unchanged last row
  2453. out[8] = a[8];
  2454. out[9] = a[9];
  2455. out[10] = a[10];
  2456. out[11] = a[11];
  2457. out[12] = a[12];
  2458. out[13] = a[13];
  2459. out[14] = a[14];
  2460. out[15] = a[15];
  2461. } // Perform axis-specific matrix multiplication
  2462. out[0] = a00 * c + a10 * s;
  2463. out[1] = a01 * c + a11 * s;
  2464. out[2] = a02 * c + a12 * s;
  2465. out[3] = a03 * c + a13 * s;
  2466. out[4] = a10 * c - a00 * s;
  2467. out[5] = a11 * c - a01 * s;
  2468. out[6] = a12 * c - a02 * s;
  2469. out[7] = a13 * c - a03 * s;
  2470. return out;
  2471. }
  2472. /**
  2473. * Creates a matrix from a vector translation
  2474. * This is equivalent to (but much faster than):
  2475. *
  2476. * mat4.identity(dest);
  2477. * mat4.translate(dest, dest, vec);
  2478. *
  2479. * @param {mat4} out mat4 receiving operation result
  2480. * @param {ReadonlyVec3} v Translation vector
  2481. * @returns {mat4} out
  2482. */
  2483. function fromTranslation$2(out, v) {
  2484. out[0] = 1;
  2485. out[1] = 0;
  2486. out[2] = 0;
  2487. out[3] = 0;
  2488. out[4] = 0;
  2489. out[5] = 1;
  2490. out[6] = 0;
  2491. out[7] = 0;
  2492. out[8] = 0;
  2493. out[9] = 0;
  2494. out[10] = 1;
  2495. out[11] = 0;
  2496. out[12] = v[0];
  2497. out[13] = v[1];
  2498. out[14] = v[2];
  2499. out[15] = 1;
  2500. return out;
  2501. }
  2502. /**
  2503. * Creates a matrix from a vector scaling
  2504. * This is equivalent to (but much faster than):
  2505. *
  2506. * mat4.identity(dest);
  2507. * mat4.scale(dest, dest, vec);
  2508. *
  2509. * @param {mat4} out mat4 receiving operation result
  2510. * @param {ReadonlyVec3} v Scaling vector
  2511. * @returns {mat4} out
  2512. */
  2513. function fromScaling$3(out, v) {
  2514. out[0] = v[0];
  2515. out[1] = 0;
  2516. out[2] = 0;
  2517. out[3] = 0;
  2518. out[4] = 0;
  2519. out[5] = v[1];
  2520. out[6] = 0;
  2521. out[7] = 0;
  2522. out[8] = 0;
  2523. out[9] = 0;
  2524. out[10] = v[2];
  2525. out[11] = 0;
  2526. out[12] = 0;
  2527. out[13] = 0;
  2528. out[14] = 0;
  2529. out[15] = 1;
  2530. return out;
  2531. }
  2532. /**
  2533. * Creates a matrix from a given angle around a given axis
  2534. * This is equivalent to (but much faster than):
  2535. *
  2536. * mat4.identity(dest);
  2537. * mat4.rotate(dest, dest, rad, axis);
  2538. *
  2539. * @param {mat4} out mat4 receiving operation result
  2540. * @param {Number} rad the angle to rotate the matrix by
  2541. * @param {ReadonlyVec3} axis the axis to rotate around
  2542. * @returns {mat4} out
  2543. */
  2544. function fromRotation$3(out, rad, axis) {
  2545. var x = axis[0],
  2546. y = axis[1],
  2547. z = axis[2];
  2548. var len = Math.hypot(x, y, z);
  2549. var s, c, t;
  2550. if (len < EPSILON) {
  2551. return null;
  2552. }
  2553. len = 1 / len;
  2554. x *= len;
  2555. y *= len;
  2556. z *= len;
  2557. s = Math.sin(rad);
  2558. c = Math.cos(rad);
  2559. t = 1 - c; // Perform rotation-specific matrix multiplication
  2560. out[0] = x * x * t + c;
  2561. out[1] = y * x * t + z * s;
  2562. out[2] = z * x * t - y * s;
  2563. out[3] = 0;
  2564. out[4] = x * y * t - z * s;
  2565. out[5] = y * y * t + c;
  2566. out[6] = z * y * t + x * s;
  2567. out[7] = 0;
  2568. out[8] = x * z * t + y * s;
  2569. out[9] = y * z * t - x * s;
  2570. out[10] = z * z * t + c;
  2571. out[11] = 0;
  2572. out[12] = 0;
  2573. out[13] = 0;
  2574. out[14] = 0;
  2575. out[15] = 1;
  2576. return out;
  2577. }
  2578. /**
  2579. * Creates a matrix from the given angle around the X axis
  2580. * This is equivalent to (but much faster than):
  2581. *
  2582. * mat4.identity(dest);
  2583. * mat4.rotateX(dest, dest, rad);
  2584. *
  2585. * @param {mat4} out mat4 receiving operation result
  2586. * @param {Number} rad the angle to rotate the matrix by
  2587. * @returns {mat4} out
  2588. */
  2589. function fromXRotation(out, rad) {
  2590. var s = Math.sin(rad);
  2591. var c = Math.cos(rad); // Perform axis-specific matrix multiplication
  2592. out[0] = 1;
  2593. out[1] = 0;
  2594. out[2] = 0;
  2595. out[3] = 0;
  2596. out[4] = 0;
  2597. out[5] = c;
  2598. out[6] = s;
  2599. out[7] = 0;
  2600. out[8] = 0;
  2601. out[9] = -s;
  2602. out[10] = c;
  2603. out[11] = 0;
  2604. out[12] = 0;
  2605. out[13] = 0;
  2606. out[14] = 0;
  2607. out[15] = 1;
  2608. return out;
  2609. }
  2610. /**
  2611. * Creates a matrix from the given angle around the Y axis
  2612. * This is equivalent to (but much faster than):
  2613. *
  2614. * mat4.identity(dest);
  2615. * mat4.rotateY(dest, dest, rad);
  2616. *
  2617. * @param {mat4} out mat4 receiving operation result
  2618. * @param {Number} rad the angle to rotate the matrix by
  2619. * @returns {mat4} out
  2620. */
  2621. function fromYRotation(out, rad) {
  2622. var s = Math.sin(rad);
  2623. var c = Math.cos(rad); // Perform axis-specific matrix multiplication
  2624. out[0] = c;
  2625. out[1] = 0;
  2626. out[2] = -s;
  2627. out[3] = 0;
  2628. out[4] = 0;
  2629. out[5] = 1;
  2630. out[6] = 0;
  2631. out[7] = 0;
  2632. out[8] = s;
  2633. out[9] = 0;
  2634. out[10] = c;
  2635. out[11] = 0;
  2636. out[12] = 0;
  2637. out[13] = 0;
  2638. out[14] = 0;
  2639. out[15] = 1;
  2640. return out;
  2641. }
  2642. /**
  2643. * Creates a matrix from the given angle around the Z axis
  2644. * This is equivalent to (but much faster than):
  2645. *
  2646. * mat4.identity(dest);
  2647. * mat4.rotateZ(dest, dest, rad);
  2648. *
  2649. * @param {mat4} out mat4 receiving operation result
  2650. * @param {Number} rad the angle to rotate the matrix by
  2651. * @returns {mat4} out
  2652. */
  2653. function fromZRotation(out, rad) {
  2654. var s = Math.sin(rad);
  2655. var c = Math.cos(rad); // Perform axis-specific matrix multiplication
  2656. out[0] = c;
  2657. out[1] = s;
  2658. out[2] = 0;
  2659. out[3] = 0;
  2660. out[4] = -s;
  2661. out[5] = c;
  2662. out[6] = 0;
  2663. out[7] = 0;
  2664. out[8] = 0;
  2665. out[9] = 0;
  2666. out[10] = 1;
  2667. out[11] = 0;
  2668. out[12] = 0;
  2669. out[13] = 0;
  2670. out[14] = 0;
  2671. out[15] = 1;
  2672. return out;
  2673. }
  2674. /**
  2675. * Creates a matrix from a quaternion rotation and vector translation
  2676. * This is equivalent to (but much faster than):
  2677. *
  2678. * mat4.identity(dest);
  2679. * mat4.translate(dest, vec);
  2680. * let quatMat = mat4.create();
  2681. * quat4.toMat4(quat, quatMat);
  2682. * mat4.multiply(dest, quatMat);
  2683. *
  2684. * @param {mat4} out mat4 receiving operation result
  2685. * @param {quat4} q Rotation quaternion
  2686. * @param {ReadonlyVec3} v Translation vector
  2687. * @returns {mat4} out
  2688. */
  2689. function fromRotationTranslation(out, q, v) {
  2690. // Quaternion math
  2691. var x = q[0],
  2692. y = q[1],
  2693. z = q[2],
  2694. w = q[3];
  2695. var x2 = x + x;
  2696. var y2 = y + y;
  2697. var z2 = z + z;
  2698. var xx = x * x2;
  2699. var xy = x * y2;
  2700. var xz = x * z2;
  2701. var yy = y * y2;
  2702. var yz = y * z2;
  2703. var zz = z * z2;
  2704. var wx = w * x2;
  2705. var wy = w * y2;
  2706. var wz = w * z2;
  2707. out[0] = 1 - (yy + zz);
  2708. out[1] = xy + wz;
  2709. out[2] = xz - wy;
  2710. out[3] = 0;
  2711. out[4] = xy - wz;
  2712. out[5] = 1 - (xx + zz);
  2713. out[6] = yz + wx;
  2714. out[7] = 0;
  2715. out[8] = xz + wy;
  2716. out[9] = yz - wx;
  2717. out[10] = 1 - (xx + yy);
  2718. out[11] = 0;
  2719. out[12] = v[0];
  2720. out[13] = v[1];
  2721. out[14] = v[2];
  2722. out[15] = 1;
  2723. return out;
  2724. }
  2725. /**
  2726. * Creates a new mat4 from a dual quat.
  2727. *
  2728. * @param {mat4} out Matrix
  2729. * @param {ReadonlyQuat2} a Dual Quaternion
  2730. * @returns {mat4} mat4 receiving operation result
  2731. */
  2732. function fromQuat2(out, a) {
  2733. var translation = new ARRAY_TYPE(3);
  2734. var bx = -a[0],
  2735. by = -a[1],
  2736. bz = -a[2],
  2737. bw = a[3],
  2738. ax = a[4],
  2739. ay = a[5],
  2740. az = a[6],
  2741. aw = a[7];
  2742. var magnitude = bx * bx + by * by + bz * bz + bw * bw; //Only scale if it makes sense
  2743. if (magnitude > 0) {
  2744. translation[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2 / magnitude;
  2745. translation[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2 / magnitude;
  2746. translation[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2 / magnitude;
  2747. } else {
  2748. translation[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
  2749. translation[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
  2750. translation[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
  2751. }
  2752. fromRotationTranslation(out, a, translation);
  2753. return out;
  2754. }
  2755. /**
  2756. * Returns the translation vector component of a transformation
  2757. * matrix. If a matrix is built with fromRotationTranslation,
  2758. * the returned vector will be the same as the translation vector
  2759. * originally supplied.
  2760. * @param {vec3} out Vector to receive translation component
  2761. * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
  2762. * @return {vec3} out
  2763. */
  2764. function getTranslation(out, mat) {
  2765. out[0] = mat[12];
  2766. out[1] = mat[13];
  2767. out[2] = mat[14];
  2768. return out;
  2769. }
  2770. /**
  2771. * Returns the scaling factor component of a transformation
  2772. * matrix. If a matrix is built with fromRotationTranslationScale
  2773. * with a normalized Quaternion paramter, the returned vector will be
  2774. * the same as the scaling vector
  2775. * originally supplied.
  2776. * @param {vec3} out Vector to receive scaling factor component
  2777. * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
  2778. * @return {vec3} out
  2779. */
  2780. function getScaling(out, mat) {
  2781. var m11 = mat[0];
  2782. var m12 = mat[1];
  2783. var m13 = mat[2];
  2784. var m21 = mat[4];
  2785. var m22 = mat[5];
  2786. var m23 = mat[6];
  2787. var m31 = mat[8];
  2788. var m32 = mat[9];
  2789. var m33 = mat[10];
  2790. out[0] = Math.hypot(m11, m12, m13);
  2791. out[1] = Math.hypot(m21, m22, m23);
  2792. out[2] = Math.hypot(m31, m32, m33);
  2793. return out;
  2794. }
  2795. /**
  2796. * Returns a quaternion representing the rotational component
  2797. * of a transformation matrix. If a matrix is built with
  2798. * fromRotationTranslation, the returned quaternion will be the
  2799. * same as the quaternion originally supplied.
  2800. * @param {quat} out Quaternion to receive the rotation component
  2801. * @param {ReadonlyMat4} mat Matrix to be decomposed (input)
  2802. * @return {quat} out
  2803. */
  2804. function getRotation(out, mat) {
  2805. var scaling = new ARRAY_TYPE(3);
  2806. getScaling(scaling, mat);
  2807. var is1 = 1 / scaling[0];
  2808. var is2 = 1 / scaling[1];
  2809. var is3 = 1 / scaling[2];
  2810. var sm11 = mat[0] * is1;
  2811. var sm12 = mat[1] * is2;
  2812. var sm13 = mat[2] * is3;
  2813. var sm21 = mat[4] * is1;
  2814. var sm22 = mat[5] * is2;
  2815. var sm23 = mat[6] * is3;
  2816. var sm31 = mat[8] * is1;
  2817. var sm32 = mat[9] * is2;
  2818. var sm33 = mat[10] * is3;
  2819. var trace = sm11 + sm22 + sm33;
  2820. var S = 0;
  2821. if (trace > 0) {
  2822. S = Math.sqrt(trace + 1.0) * 2;
  2823. out[3] = 0.25 * S;
  2824. out[0] = (sm23 - sm32) / S;
  2825. out[1] = (sm31 - sm13) / S;
  2826. out[2] = (sm12 - sm21) / S;
  2827. } else if (sm11 > sm22 && sm11 > sm33) {
  2828. S = Math.sqrt(1.0 + sm11 - sm22 - sm33) * 2;
  2829. out[3] = (sm23 - sm32) / S;
  2830. out[0] = 0.25 * S;
  2831. out[1] = (sm12 + sm21) / S;
  2832. out[2] = (sm31 + sm13) / S;
  2833. } else if (sm22 > sm33) {
  2834. S = Math.sqrt(1.0 + sm22 - sm11 - sm33) * 2;
  2835. out[3] = (sm31 - sm13) / S;
  2836. out[0] = (sm12 + sm21) / S;
  2837. out[1] = 0.25 * S;
  2838. out[2] = (sm23 + sm32) / S;
  2839. } else {
  2840. S = Math.sqrt(1.0 + sm33 - sm11 - sm22) * 2;
  2841. out[3] = (sm12 - sm21) / S;
  2842. out[0] = (sm31 + sm13) / S;
  2843. out[1] = (sm23 + sm32) / S;
  2844. out[2] = 0.25 * S;
  2845. }
  2846. return out;
  2847. }
  2848. /**
  2849. * Creates a matrix from a quaternion rotation, vector translation and vector scale
  2850. * This is equivalent to (but much faster than):
  2851. *
  2852. * mat4.identity(dest);
  2853. * mat4.translate(dest, vec);
  2854. * let quatMat = mat4.create();
  2855. * quat4.toMat4(quat, quatMat);
  2856. * mat4.multiply(dest, quatMat);
  2857. * mat4.scale(dest, scale)
  2858. *
  2859. * @param {mat4} out mat4 receiving operation result
  2860. * @param {quat4} q Rotation quaternion
  2861. * @param {ReadonlyVec3} v Translation vector
  2862. * @param {ReadonlyVec3} s Scaling vector
  2863. * @returns {mat4} out
  2864. */
  2865. function fromRotationTranslationScale(out, q, v, s) {
  2866. // Quaternion math
  2867. var x = q[0],
  2868. y = q[1],
  2869. z = q[2],
  2870. w = q[3];
  2871. var x2 = x + x;
  2872. var y2 = y + y;
  2873. var z2 = z + z;
  2874. var xx = x * x2;
  2875. var xy = x * y2;
  2876. var xz = x * z2;
  2877. var yy = y * y2;
  2878. var yz = y * z2;
  2879. var zz = z * z2;
  2880. var wx = w * x2;
  2881. var wy = w * y2;
  2882. var wz = w * z2;
  2883. var sx = s[0];
  2884. var sy = s[1];
  2885. var sz = s[2];
  2886. out[0] = (1 - (yy + zz)) * sx;
  2887. out[1] = (xy + wz) * sx;
  2888. out[2] = (xz - wy) * sx;
  2889. out[3] = 0;
  2890. out[4] = (xy - wz) * sy;
  2891. out[5] = (1 - (xx + zz)) * sy;
  2892. out[6] = (yz + wx) * sy;
  2893. out[7] = 0;
  2894. out[8] = (xz + wy) * sz;
  2895. out[9] = (yz - wx) * sz;
  2896. out[10] = (1 - (xx + yy)) * sz;
  2897. out[11] = 0;
  2898. out[12] = v[0];
  2899. out[13] = v[1];
  2900. out[14] = v[2];
  2901. out[15] = 1;
  2902. return out;
  2903. }
  2904. /**
  2905. * Creates a matrix from a quaternion rotation, vector translation and vector scale, rotating and scaling around the given origin
  2906. * This is equivalent to (but much faster than):
  2907. *
  2908. * mat4.identity(dest);
  2909. * mat4.translate(dest, vec);
  2910. * mat4.translate(dest, origin);
  2911. * let quatMat = mat4.create();
  2912. * quat4.toMat4(quat, quatMat);
  2913. * mat4.multiply(dest, quatMat);
  2914. * mat4.scale(dest, scale)
  2915. * mat4.translate(dest, negativeOrigin);
  2916. *
  2917. * @param {mat4} out mat4 receiving operation result
  2918. * @param {quat4} q Rotation quaternion
  2919. * @param {ReadonlyVec3} v Translation vector
  2920. * @param {ReadonlyVec3} s Scaling vector
  2921. * @param {ReadonlyVec3} o The origin vector around which to scale and rotate
  2922. * @returns {mat4} out
  2923. */
  2924. function fromRotationTranslationScaleOrigin(out, q, v, s, o) {
  2925. // Quaternion math
  2926. var x = q[0],
  2927. y = q[1],
  2928. z = q[2],
  2929. w = q[3];
  2930. var x2 = x + x;
  2931. var y2 = y + y;
  2932. var z2 = z + z;
  2933. var xx = x * x2;
  2934. var xy = x * y2;
  2935. var xz = x * z2;
  2936. var yy = y * y2;
  2937. var yz = y * z2;
  2938. var zz = z * z2;
  2939. var wx = w * x2;
  2940. var wy = w * y2;
  2941. var wz = w * z2;
  2942. var sx = s[0];
  2943. var sy = s[1];
  2944. var sz = s[2];
  2945. var ox = o[0];
  2946. var oy = o[1];
  2947. var oz = o[2];
  2948. var out0 = (1 - (yy + zz)) * sx;
  2949. var out1 = (xy + wz) * sx;
  2950. var out2 = (xz - wy) * sx;
  2951. var out4 = (xy - wz) * sy;
  2952. var out5 = (1 - (xx + zz)) * sy;
  2953. var out6 = (yz + wx) * sy;
  2954. var out8 = (xz + wy) * sz;
  2955. var out9 = (yz - wx) * sz;
  2956. var out10 = (1 - (xx + yy)) * sz;
  2957. out[0] = out0;
  2958. out[1] = out1;
  2959. out[2] = out2;
  2960. out[3] = 0;
  2961. out[4] = out4;
  2962. out[5] = out5;
  2963. out[6] = out6;
  2964. out[7] = 0;
  2965. out[8] = out8;
  2966. out[9] = out9;
  2967. out[10] = out10;
  2968. out[11] = 0;
  2969. out[12] = v[0] + ox - (out0 * ox + out4 * oy + out8 * oz);
  2970. out[13] = v[1] + oy - (out1 * ox + out5 * oy + out9 * oz);
  2971. out[14] = v[2] + oz - (out2 * ox + out6 * oy + out10 * oz);
  2972. out[15] = 1;
  2973. return out;
  2974. }
  2975. /**
  2976. * Calculates a 4x4 matrix from the given quaternion
  2977. *
  2978. * @param {mat4} out mat4 receiving operation result
  2979. * @param {ReadonlyQuat} q Quaternion to create matrix from
  2980. *
  2981. * @returns {mat4} out
  2982. */
  2983. function fromQuat$1(out, q) {
  2984. var x = q[0],
  2985. y = q[1],
  2986. z = q[2],
  2987. w = q[3];
  2988. var x2 = x + x;
  2989. var y2 = y + y;
  2990. var z2 = z + z;
  2991. var xx = x * x2;
  2992. var yx = y * x2;
  2993. var yy = y * y2;
  2994. var zx = z * x2;
  2995. var zy = z * y2;
  2996. var zz = z * z2;
  2997. var wx = w * x2;
  2998. var wy = w * y2;
  2999. var wz = w * z2;
  3000. out[0] = 1 - yy - zz;
  3001. out[1] = yx + wz;
  3002. out[2] = zx - wy;
  3003. out[3] = 0;
  3004. out[4] = yx - wz;
  3005. out[5] = 1 - xx - zz;
  3006. out[6] = zy + wx;
  3007. out[7] = 0;
  3008. out[8] = zx + wy;
  3009. out[9] = zy - wx;
  3010. out[10] = 1 - xx - yy;
  3011. out[11] = 0;
  3012. out[12] = 0;
  3013. out[13] = 0;
  3014. out[14] = 0;
  3015. out[15] = 1;
  3016. return out;
  3017. }
  3018. /**
  3019. * Generates a frustum matrix with the given bounds
  3020. *
  3021. * @param {mat4} out mat4 frustum matrix will be written into
  3022. * @param {Number} left Left bound of the frustum
  3023. * @param {Number} right Right bound of the frustum
  3024. * @param {Number} bottom Bottom bound of the frustum
  3025. * @param {Number} top Top bound of the frustum
  3026. * @param {Number} near Near bound of the frustum
  3027. * @param {Number} far Far bound of the frustum
  3028. * @returns {mat4} out
  3029. */
  3030. function frustum(out, left, right, bottom, top, near, far) {
  3031. var rl = 1 / (right - left);
  3032. var tb = 1 / (top - bottom);
  3033. var nf = 1 / (near - far);
  3034. out[0] = near * 2 * rl;
  3035. out[1] = 0;
  3036. out[2] = 0;
  3037. out[3] = 0;
  3038. out[4] = 0;
  3039. out[5] = near * 2 * tb;
  3040. out[6] = 0;
  3041. out[7] = 0;
  3042. out[8] = (right + left) * rl;
  3043. out[9] = (top + bottom) * tb;
  3044. out[10] = (far + near) * nf;
  3045. out[11] = -1;
  3046. out[12] = 0;
  3047. out[13] = 0;
  3048. out[14] = far * near * 2 * nf;
  3049. out[15] = 0;
  3050. return out;
  3051. }
  3052. /**
  3053. * Generates a perspective projection matrix with the given bounds.
  3054. * The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1],
  3055. * which matches WebGL/OpenGL's clip volume.
  3056. * Passing null/undefined/no value for far will generate infinite projection matrix.
  3057. *
  3058. * @param {mat4} out mat4 frustum matrix will be written into
  3059. * @param {number} fovy Vertical field of view in radians
  3060. * @param {number} aspect Aspect ratio. typically viewport width/height
  3061. * @param {number} near Near bound of the frustum
  3062. * @param {number} far Far bound of the frustum, can be null or Infinity
  3063. * @returns {mat4} out
  3064. */
  3065. function perspectiveNO(out, fovy, aspect, near, far) {
  3066. var f = 1.0 / Math.tan(fovy / 2),
  3067. nf;
  3068. out[0] = f / aspect;
  3069. out[1] = 0;
  3070. out[2] = 0;
  3071. out[3] = 0;
  3072. out[4] = 0;
  3073. out[5] = f;
  3074. out[6] = 0;
  3075. out[7] = 0;
  3076. out[8] = 0;
  3077. out[9] = 0;
  3078. out[11] = -1;
  3079. out[12] = 0;
  3080. out[13] = 0;
  3081. out[15] = 0;
  3082. if (far != null && far !== Infinity) {
  3083. nf = 1 / (near - far);
  3084. out[10] = (far + near) * nf;
  3085. out[14] = 2 * far * near * nf;
  3086. } else {
  3087. out[10] = -1;
  3088. out[14] = -2 * near;
  3089. }
  3090. return out;
  3091. }
  3092. /**
  3093. * Alias for {@link mat4.perspectiveNO}
  3094. * @function
  3095. */
  3096. var perspective = perspectiveNO;
  3097. /**
  3098. * Generates a perspective projection matrix suitable for WebGPU with the given bounds.
  3099. * The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1],
  3100. * which matches WebGPU/Vulkan/DirectX/Metal's clip volume.
  3101. * Passing null/undefined/no value for far will generate infinite projection matrix.
  3102. *
  3103. * @param {mat4} out mat4 frustum matrix will be written into
  3104. * @param {number} fovy Vertical field of view in radians
  3105. * @param {number} aspect Aspect ratio. typically viewport width/height
  3106. * @param {number} near Near bound of the frustum
  3107. * @param {number} far Far bound of the frustum, can be null or Infinity
  3108. * @returns {mat4} out
  3109. */
  3110. function perspectiveZO(out, fovy, aspect, near, far) {
  3111. var f = 1.0 / Math.tan(fovy / 2),
  3112. nf;
  3113. out[0] = f / aspect;
  3114. out[1] = 0;
  3115. out[2] = 0;
  3116. out[3] = 0;
  3117. out[4] = 0;
  3118. out[5] = f;
  3119. out[6] = 0;
  3120. out[7] = 0;
  3121. out[8] = 0;
  3122. out[9] = 0;
  3123. out[11] = -1;
  3124. out[12] = 0;
  3125. out[13] = 0;
  3126. out[15] = 0;
  3127. if (far != null && far !== Infinity) {
  3128. nf = 1 / (near - far);
  3129. out[10] = far * nf;
  3130. out[14] = far * near * nf;
  3131. } else {
  3132. out[10] = -1;
  3133. out[14] = -near;
  3134. }
  3135. return out;
  3136. }
  3137. /**
  3138. * Generates a perspective projection matrix with the given field of view.
  3139. * This is primarily useful for generating projection matrices to be used
  3140. * with the still experiemental WebVR API.
  3141. *
  3142. * @param {mat4} out mat4 frustum matrix will be written into
  3143. * @param {Object} fov Object containing the following values: upDegrees, downDegrees, leftDegrees, rightDegrees
  3144. * @param {number} near Near bound of the frustum
  3145. * @param {number} far Far bound of the frustum
  3146. * @returns {mat4} out
  3147. */
  3148. function perspectiveFromFieldOfView(out, fov, near, far) {
  3149. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  3150. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  3151. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  3152. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  3153. var xScale = 2.0 / (leftTan + rightTan);
  3154. var yScale = 2.0 / (upTan + downTan);
  3155. out[0] = xScale;
  3156. out[1] = 0.0;
  3157. out[2] = 0.0;
  3158. out[3] = 0.0;
  3159. out[4] = 0.0;
  3160. out[5] = yScale;
  3161. out[6] = 0.0;
  3162. out[7] = 0.0;
  3163. out[8] = -((leftTan - rightTan) * xScale * 0.5);
  3164. out[9] = (upTan - downTan) * yScale * 0.5;
  3165. out[10] = far / (near - far);
  3166. out[11] = -1.0;
  3167. out[12] = 0.0;
  3168. out[13] = 0.0;
  3169. out[14] = far * near / (near - far);
  3170. out[15] = 0.0;
  3171. return out;
  3172. }
  3173. /**
  3174. * Generates a orthogonal projection matrix with the given bounds.
  3175. * The near/far clip planes correspond to a normalized device coordinate Z range of [-1, 1],
  3176. * which matches WebGL/OpenGL's clip volume.
  3177. *
  3178. * @param {mat4} out mat4 frustum matrix will be written into
  3179. * @param {number} left Left bound of the frustum
  3180. * @param {number} right Right bound of the frustum
  3181. * @param {number} bottom Bottom bound of the frustum
  3182. * @param {number} top Top bound of the frustum
  3183. * @param {number} near Near bound of the frustum
  3184. * @param {number} far Far bound of the frustum
  3185. * @returns {mat4} out
  3186. */
  3187. function orthoNO(out, left, right, bottom, top, near, far) {
  3188. var lr = 1 / (left - right);
  3189. var bt = 1 / (bottom - top);
  3190. var nf = 1 / (near - far);
  3191. out[0] = -2 * lr;
  3192. out[1] = 0;
  3193. out[2] = 0;
  3194. out[3] = 0;
  3195. out[4] = 0;
  3196. out[5] = -2 * bt;
  3197. out[6] = 0;
  3198. out[7] = 0;
  3199. out[8] = 0;
  3200. out[9] = 0;
  3201. out[10] = 2 * nf;
  3202. out[11] = 0;
  3203. out[12] = (left + right) * lr;
  3204. out[13] = (top + bottom) * bt;
  3205. out[14] = (far + near) * nf;
  3206. out[15] = 1;
  3207. return out;
  3208. }
  3209. /**
  3210. * Alias for {@link mat4.orthoNO}
  3211. * @function
  3212. */
  3213. var ortho = orthoNO;
  3214. /**
  3215. * Generates a orthogonal projection matrix with the given bounds.
  3216. * The near/far clip planes correspond to a normalized device coordinate Z range of [0, 1],
  3217. * which matches WebGPU/Vulkan/DirectX/Metal's clip volume.
  3218. *
  3219. * @param {mat4} out mat4 frustum matrix will be written into
  3220. * @param {number} left Left bound of the frustum
  3221. * @param {number} right Right bound of the frustum
  3222. * @param {number} bottom Bottom bound of the frustum
  3223. * @param {number} top Top bound of the frustum
  3224. * @param {number} near Near bound of the frustum
  3225. * @param {number} far Far bound of the frustum
  3226. * @returns {mat4} out
  3227. */
  3228. function orthoZO(out, left, right, bottom, top, near, far) {
  3229. var lr = 1 / (left - right);
  3230. var bt = 1 / (bottom - top);
  3231. var nf = 1 / (near - far);
  3232. out[0] = -2 * lr;
  3233. out[1] = 0;
  3234. out[2] = 0;
  3235. out[3] = 0;
  3236. out[4] = 0;
  3237. out[5] = -2 * bt;
  3238. out[6] = 0;
  3239. out[7] = 0;
  3240. out[8] = 0;
  3241. out[9] = 0;
  3242. out[10] = nf;
  3243. out[11] = 0;
  3244. out[12] = (left + right) * lr;
  3245. out[13] = (top + bottom) * bt;
  3246. out[14] = near * nf;
  3247. out[15] = 1;
  3248. return out;
  3249. }
  3250. /**
  3251. * Generates a look-at matrix with the given eye position, focal point, and up axis.
  3252. * If you want a matrix that actually makes an object look at another object, you should use targetTo instead.
  3253. *
  3254. * @param {mat4} out mat4 frustum matrix will be written into
  3255. * @param {ReadonlyVec3} eye Position of the viewer
  3256. * @param {ReadonlyVec3} center Point the viewer is looking at
  3257. * @param {ReadonlyVec3} up vec3 pointing up
  3258. * @returns {mat4} out
  3259. */
  3260. function lookAt(out, eye, center, up) {
  3261. var x0, x1, x2, y0, y1, y2, z0, z1, z2, len;
  3262. var eyex = eye[0];
  3263. var eyey = eye[1];
  3264. var eyez = eye[2];
  3265. var upx = up[0];
  3266. var upy = up[1];
  3267. var upz = up[2];
  3268. var centerx = center[0];
  3269. var centery = center[1];
  3270. var centerz = center[2];
  3271. if (Math.abs(eyex - centerx) < EPSILON && Math.abs(eyey - centery) < EPSILON && Math.abs(eyez - centerz) < EPSILON) {
  3272. return identity$3(out);
  3273. }
  3274. z0 = eyex - centerx;
  3275. z1 = eyey - centery;
  3276. z2 = eyez - centerz;
  3277. len = 1 / Math.hypot(z0, z1, z2);
  3278. z0 *= len;
  3279. z1 *= len;
  3280. z2 *= len;
  3281. x0 = upy * z2 - upz * z1;
  3282. x1 = upz * z0 - upx * z2;
  3283. x2 = upx * z1 - upy * z0;
  3284. len = Math.hypot(x0, x1, x2);
  3285. if (!len) {
  3286. x0 = 0;
  3287. x1 = 0;
  3288. x2 = 0;
  3289. } else {
  3290. len = 1 / len;
  3291. x0 *= len;
  3292. x1 *= len;
  3293. x2 *= len;
  3294. }
  3295. y0 = z1 * x2 - z2 * x1;
  3296. y1 = z2 * x0 - z0 * x2;
  3297. y2 = z0 * x1 - z1 * x0;
  3298. len = Math.hypot(y0, y1, y2);
  3299. if (!len) {
  3300. y0 = 0;
  3301. y1 = 0;
  3302. y2 = 0;
  3303. } else {
  3304. len = 1 / len;
  3305. y0 *= len;
  3306. y1 *= len;
  3307. y2 *= len;
  3308. }
  3309. out[0] = x0;
  3310. out[1] = y0;
  3311. out[2] = z0;
  3312. out[3] = 0;
  3313. out[4] = x1;
  3314. out[5] = y1;
  3315. out[6] = z1;
  3316. out[7] = 0;
  3317. out[8] = x2;
  3318. out[9] = y2;
  3319. out[10] = z2;
  3320. out[11] = 0;
  3321. out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez);
  3322. out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez);
  3323. out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez);
  3324. out[15] = 1;
  3325. return out;
  3326. }
  3327. /**
  3328. * Generates a matrix that makes something look at something else.
  3329. *
  3330. * @param {mat4} out mat4 frustum matrix will be written into
  3331. * @param {ReadonlyVec3} eye Position of the viewer
  3332. * @param {ReadonlyVec3} center Point the viewer is looking at
  3333. * @param {ReadonlyVec3} up vec3 pointing up
  3334. * @returns {mat4} out
  3335. */
  3336. function targetTo(out, eye, target, up) {
  3337. var eyex = eye[0],
  3338. eyey = eye[1],
  3339. eyez = eye[2],
  3340. upx = up[0],
  3341. upy = up[1],
  3342. upz = up[2];
  3343. var z0 = eyex - target[0],
  3344. z1 = eyey - target[1],
  3345. z2 = eyez - target[2];
  3346. var len = z0 * z0 + z1 * z1 + z2 * z2;
  3347. if (len > 0) {
  3348. len = 1 / Math.sqrt(len);
  3349. z0 *= len;
  3350. z1 *= len;
  3351. z2 *= len;
  3352. }
  3353. var x0 = upy * z2 - upz * z1,
  3354. x1 = upz * z0 - upx * z2,
  3355. x2 = upx * z1 - upy * z0;
  3356. len = x0 * x0 + x1 * x1 + x2 * x2;
  3357. if (len > 0) {
  3358. len = 1 / Math.sqrt(len);
  3359. x0 *= len;
  3360. x1 *= len;
  3361. x2 *= len;
  3362. }
  3363. out[0] = x0;
  3364. out[1] = x1;
  3365. out[2] = x2;
  3366. out[3] = 0;
  3367. out[4] = z1 * x2 - z2 * x1;
  3368. out[5] = z2 * x0 - z0 * x2;
  3369. out[6] = z0 * x1 - z1 * x0;
  3370. out[7] = 0;
  3371. out[8] = z0;
  3372. out[9] = z1;
  3373. out[10] = z2;
  3374. out[11] = 0;
  3375. out[12] = eyex;
  3376. out[13] = eyey;
  3377. out[14] = eyez;
  3378. out[15] = 1;
  3379. return out;
  3380. }
  3381. /**
  3382. * Returns a string representation of a mat4
  3383. *
  3384. * @param {ReadonlyMat4} a matrix to represent as a string
  3385. * @returns {String} string representation of the matrix
  3386. */
  3387. function str$3(a) {
  3388. return "mat4(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ", " + a[6] + ", " + a[7] + ", " + a[8] + ", " + a[9] + ", " + a[10] + ", " + a[11] + ", " + a[12] + ", " + a[13] + ", " + a[14] + ", " + a[15] + ")";
  3389. }
  3390. /**
  3391. * Returns Frobenius norm of a mat4
  3392. *
  3393. * @param {ReadonlyMat4} a the matrix to calculate Frobenius norm of
  3394. * @returns {Number} Frobenius norm
  3395. */
  3396. function frob$3(a) {
  3397. return Math.hypot(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9], a[10], a[11], a[12], a[13], a[14], a[15]);
  3398. }
  3399. /**
  3400. * Adds two mat4's
  3401. *
  3402. * @param {mat4} out the receiving matrix
  3403. * @param {ReadonlyMat4} a the first operand
  3404. * @param {ReadonlyMat4} b the second operand
  3405. * @returns {mat4} out
  3406. */
  3407. function add$3(out, a, b) {
  3408. out[0] = a[0] + b[0];
  3409. out[1] = a[1] + b[1];
  3410. out[2] = a[2] + b[2];
  3411. out[3] = a[3] + b[3];
  3412. out[4] = a[4] + b[4];
  3413. out[5] = a[5] + b[5];
  3414. out[6] = a[6] + b[6];
  3415. out[7] = a[7] + b[7];
  3416. out[8] = a[8] + b[8];
  3417. out[9] = a[9] + b[9];
  3418. out[10] = a[10] + b[10];
  3419. out[11] = a[11] + b[11];
  3420. out[12] = a[12] + b[12];
  3421. out[13] = a[13] + b[13];
  3422. out[14] = a[14] + b[14];
  3423. out[15] = a[15] + b[15];
  3424. return out;
  3425. }
  3426. /**
  3427. * Subtracts matrix b from matrix a
  3428. *
  3429. * @param {mat4} out the receiving matrix
  3430. * @param {ReadonlyMat4} a the first operand
  3431. * @param {ReadonlyMat4} b the second operand
  3432. * @returns {mat4} out
  3433. */
  3434. function subtract$3(out, a, b) {
  3435. out[0] = a[0] - b[0];
  3436. out[1] = a[1] - b[1];
  3437. out[2] = a[2] - b[2];
  3438. out[3] = a[3] - b[3];
  3439. out[4] = a[4] - b[4];
  3440. out[5] = a[5] - b[5];
  3441. out[6] = a[6] - b[6];
  3442. out[7] = a[7] - b[7];
  3443. out[8] = a[8] - b[8];
  3444. out[9] = a[9] - b[9];
  3445. out[10] = a[10] - b[10];
  3446. out[11] = a[11] - b[11];
  3447. out[12] = a[12] - b[12];
  3448. out[13] = a[13] - b[13];
  3449. out[14] = a[14] - b[14];
  3450. out[15] = a[15] - b[15];
  3451. return out;
  3452. }
  3453. /**
  3454. * Multiply each element of the matrix by a scalar.
  3455. *
  3456. * @param {mat4} out the receiving matrix
  3457. * @param {ReadonlyMat4} a the matrix to scale
  3458. * @param {Number} b amount to scale the matrix's elements by
  3459. * @returns {mat4} out
  3460. */
  3461. function multiplyScalar$3(out, a, b) {
  3462. out[0] = a[0] * b;
  3463. out[1] = a[1] * b;
  3464. out[2] = a[2] * b;
  3465. out[3] = a[3] * b;
  3466. out[4] = a[4] * b;
  3467. out[5] = a[5] * b;
  3468. out[6] = a[6] * b;
  3469. out[7] = a[7] * b;
  3470. out[8] = a[8] * b;
  3471. out[9] = a[9] * b;
  3472. out[10] = a[10] * b;
  3473. out[11] = a[11] * b;
  3474. out[12] = a[12] * b;
  3475. out[13] = a[13] * b;
  3476. out[14] = a[14] * b;
  3477. out[15] = a[15] * b;
  3478. return out;
  3479. }
  3480. /**
  3481. * Adds two mat4's after multiplying each element of the second operand by a scalar value.
  3482. *
  3483. * @param {mat4} out the receiving vector
  3484. * @param {ReadonlyMat4} a the first operand
  3485. * @param {ReadonlyMat4} b the second operand
  3486. * @param {Number} scale the amount to scale b's elements by before adding
  3487. * @returns {mat4} out
  3488. */
  3489. function multiplyScalarAndAdd$3(out, a, b, scale) {
  3490. out[0] = a[0] + b[0] * scale;
  3491. out[1] = a[1] + b[1] * scale;
  3492. out[2] = a[2] + b[2] * scale;
  3493. out[3] = a[3] + b[3] * scale;
  3494. out[4] = a[4] + b[4] * scale;
  3495. out[5] = a[5] + b[5] * scale;
  3496. out[6] = a[6] + b[6] * scale;
  3497. out[7] = a[7] + b[7] * scale;
  3498. out[8] = a[8] + b[8] * scale;
  3499. out[9] = a[9] + b[9] * scale;
  3500. out[10] = a[10] + b[10] * scale;
  3501. out[11] = a[11] + b[11] * scale;
  3502. out[12] = a[12] + b[12] * scale;
  3503. out[13] = a[13] + b[13] * scale;
  3504. out[14] = a[14] + b[14] * scale;
  3505. out[15] = a[15] + b[15] * scale;
  3506. return out;
  3507. }
  3508. /**
  3509. * Returns whether or not the matrices have exactly the same elements in the same position (when compared with ===)
  3510. *
  3511. * @param {ReadonlyMat4} a The first matrix.
  3512. * @param {ReadonlyMat4} b The second matrix.
  3513. * @returns {Boolean} True if the matrices are equal, false otherwise.
  3514. */
  3515. function exactEquals$3(a, b) {
  3516. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7] && a[8] === b[8] && a[9] === b[9] && a[10] === b[10] && a[11] === b[11] && a[12] === b[12] && a[13] === b[13] && a[14] === b[14] && a[15] === b[15];
  3517. }
  3518. /**
  3519. * Returns whether or not the matrices have approximately the same elements in the same position.
  3520. *
  3521. * @param {ReadonlyMat4} a The first matrix.
  3522. * @param {ReadonlyMat4} b The second matrix.
  3523. * @returns {Boolean} True if the matrices are equal, false otherwise.
  3524. */
  3525. function equals$4(a, b) {
  3526. var a0 = a[0],
  3527. a1 = a[1],
  3528. a2 = a[2],
  3529. a3 = a[3];
  3530. var a4 = a[4],
  3531. a5 = a[5],
  3532. a6 = a[6],
  3533. a7 = a[7];
  3534. var a8 = a[8],
  3535. a9 = a[9],
  3536. a10 = a[10],
  3537. a11 = a[11];
  3538. var a12 = a[12],
  3539. a13 = a[13],
  3540. a14 = a[14],
  3541. a15 = a[15];
  3542. var b0 = b[0],
  3543. b1 = b[1],
  3544. b2 = b[2],
  3545. b3 = b[3];
  3546. var b4 = b[4],
  3547. b5 = b[5],
  3548. b6 = b[6],
  3549. b7 = b[7];
  3550. var b8 = b[8],
  3551. b9 = b[9],
  3552. b10 = b[10],
  3553. b11 = b[11];
  3554. var b12 = b[12],
  3555. b13 = b[13],
  3556. b14 = b[14],
  3557. b15 = b[15];
  3558. return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7)) && Math.abs(a8 - b8) <= EPSILON * Math.max(1.0, Math.abs(a8), Math.abs(b8)) && Math.abs(a9 - b9) <= EPSILON * Math.max(1.0, Math.abs(a9), Math.abs(b9)) && Math.abs(a10 - b10) <= EPSILON * Math.max(1.0, Math.abs(a10), Math.abs(b10)) && Math.abs(a11 - b11) <= EPSILON * Math.max(1.0, Math.abs(a11), Math.abs(b11)) && Math.abs(a12 - b12) <= EPSILON * Math.max(1.0, Math.abs(a12), Math.abs(b12)) && Math.abs(a13 - b13) <= EPSILON * Math.max(1.0, Math.abs(a13), Math.abs(b13)) && Math.abs(a14 - b14) <= EPSILON * Math.max(1.0, Math.abs(a14), Math.abs(b14)) && Math.abs(a15 - b15) <= EPSILON * Math.max(1.0, Math.abs(a15), Math.abs(b15));
  3559. }
  3560. /**
  3561. * Alias for {@link mat4.multiply}
  3562. * @function
  3563. */
  3564. var mul$3 = multiply$3;
  3565. /**
  3566. * Alias for {@link mat4.subtract}
  3567. * @function
  3568. */
  3569. var sub$3 = subtract$3;
  3570. var mat4 = /*#__PURE__*/Object.freeze({
  3571. __proto__: null,
  3572. create: create$3,
  3573. clone: clone$3,
  3574. copy: copy$3,
  3575. fromValues: fromValues$3,
  3576. set: set$3,
  3577. identity: identity$3,
  3578. transpose: transpose$2,
  3579. invert: invert$3,
  3580. adjoint: adjoint$2,
  3581. determinant: determinant$3,
  3582. multiply: multiply$3,
  3583. translate: translate$2,
  3584. scale: scale$3,
  3585. rotate: rotate$3,
  3586. rotateX: rotateX,
  3587. rotateY: rotateY,
  3588. rotateZ: rotateZ,
  3589. fromTranslation: fromTranslation$2,
  3590. fromScaling: fromScaling$3,
  3591. fromRotation: fromRotation$3,
  3592. fromXRotation: fromXRotation,
  3593. fromYRotation: fromYRotation,
  3594. fromZRotation: fromZRotation,
  3595. fromRotationTranslation: fromRotationTranslation,
  3596. fromQuat2: fromQuat2,
  3597. getTranslation: getTranslation,
  3598. getScaling: getScaling,
  3599. getRotation: getRotation,
  3600. fromRotationTranslationScale: fromRotationTranslationScale,
  3601. fromRotationTranslationScaleOrigin: fromRotationTranslationScaleOrigin,
  3602. fromQuat: fromQuat$1,
  3603. frustum: frustum,
  3604. perspectiveNO: perspectiveNO,
  3605. perspective: perspective,
  3606. perspectiveZO: perspectiveZO,
  3607. perspectiveFromFieldOfView: perspectiveFromFieldOfView,
  3608. orthoNO: orthoNO,
  3609. ortho: ortho,
  3610. orthoZO: orthoZO,
  3611. lookAt: lookAt,
  3612. targetTo: targetTo,
  3613. str: str$3,
  3614. frob: frob$3,
  3615. add: add$3,
  3616. subtract: subtract$3,
  3617. multiplyScalar: multiplyScalar$3,
  3618. multiplyScalarAndAdd: multiplyScalarAndAdd$3,
  3619. exactEquals: exactEquals$3,
  3620. equals: equals$4,
  3621. mul: mul$3,
  3622. sub: sub$3
  3623. });
  3624. /**
  3625. * 3 Dimensional Vector
  3626. * @module vec3
  3627. */
  3628. /**
  3629. * Creates a new, empty vec3
  3630. *
  3631. * @returns {vec3} a new 3D vector
  3632. */
  3633. function create$4() {
  3634. var out = new ARRAY_TYPE(3);
  3635. if (ARRAY_TYPE != Float32Array) {
  3636. out[0] = 0;
  3637. out[1] = 0;
  3638. out[2] = 0;
  3639. }
  3640. return out;
  3641. }
  3642. /**
  3643. * Creates a new vec3 initialized with values from an existing vector
  3644. *
  3645. * @param {ReadonlyVec3} a vector to clone
  3646. * @returns {vec3} a new 3D vector
  3647. */
  3648. function clone$4(a) {
  3649. var out = new ARRAY_TYPE(3);
  3650. out[0] = a[0];
  3651. out[1] = a[1];
  3652. out[2] = a[2];
  3653. return out;
  3654. }
  3655. /**
  3656. * Calculates the length of a vec3
  3657. *
  3658. * @param {ReadonlyVec3} a vector to calculate length of
  3659. * @returns {Number} length of a
  3660. */
  3661. function length(a) {
  3662. var x = a[0];
  3663. var y = a[1];
  3664. var z = a[2];
  3665. return Math.hypot(x, y, z);
  3666. }
  3667. /**
  3668. * Creates a new vec3 initialized with the given values
  3669. *
  3670. * @param {Number} x X component
  3671. * @param {Number} y Y component
  3672. * @param {Number} z Z component
  3673. * @returns {vec3} a new 3D vector
  3674. */
  3675. function fromValues$4(x, y, z) {
  3676. var out = new ARRAY_TYPE(3);
  3677. out[0] = x;
  3678. out[1] = y;
  3679. out[2] = z;
  3680. return out;
  3681. }
  3682. /**
  3683. * Copy the values from one vec3 to another
  3684. *
  3685. * @param {vec3} out the receiving vector
  3686. * @param {ReadonlyVec3} a the source vector
  3687. * @returns {vec3} out
  3688. */
  3689. function copy$4(out, a) {
  3690. out[0] = a[0];
  3691. out[1] = a[1];
  3692. out[2] = a[2];
  3693. return out;
  3694. }
  3695. /**
  3696. * Set the components of a vec3 to the given values
  3697. *
  3698. * @param {vec3} out the receiving vector
  3699. * @param {Number} x X component
  3700. * @param {Number} y Y component
  3701. * @param {Number} z Z component
  3702. * @returns {vec3} out
  3703. */
  3704. function set$4(out, x, y, z) {
  3705. out[0] = x;
  3706. out[1] = y;
  3707. out[2] = z;
  3708. return out;
  3709. }
  3710. /**
  3711. * Adds two vec3's
  3712. *
  3713. * @param {vec3} out the receiving vector
  3714. * @param {ReadonlyVec3} a the first operand
  3715. * @param {ReadonlyVec3} b the second operand
  3716. * @returns {vec3} out
  3717. */
  3718. function add$4(out, a, b) {
  3719. out[0] = a[0] + b[0];
  3720. out[1] = a[1] + b[1];
  3721. out[2] = a[2] + b[2];
  3722. return out;
  3723. }
  3724. /**
  3725. * Subtracts vector b from vector a
  3726. *
  3727. * @param {vec3} out the receiving vector
  3728. * @param {ReadonlyVec3} a the first operand
  3729. * @param {ReadonlyVec3} b the second operand
  3730. * @returns {vec3} out
  3731. */
  3732. function subtract$4(out, a, b) {
  3733. out[0] = a[0] - b[0];
  3734. out[1] = a[1] - b[1];
  3735. out[2] = a[2] - b[2];
  3736. return out;
  3737. }
  3738. /**
  3739. * Multiplies two vec3's
  3740. *
  3741. * @param {vec3} out the receiving vector
  3742. * @param {ReadonlyVec3} a the first operand
  3743. * @param {ReadonlyVec3} b the second operand
  3744. * @returns {vec3} out
  3745. */
  3746. function multiply$4(out, a, b) {
  3747. out[0] = a[0] * b[0];
  3748. out[1] = a[1] * b[1];
  3749. out[2] = a[2] * b[2];
  3750. return out;
  3751. }
  3752. /**
  3753. * Divides two vec3's
  3754. *
  3755. * @param {vec3} out the receiving vector
  3756. * @param {ReadonlyVec3} a the first operand
  3757. * @param {ReadonlyVec3} b the second operand
  3758. * @returns {vec3} out
  3759. */
  3760. function divide(out, a, b) {
  3761. out[0] = a[0] / b[0];
  3762. out[1] = a[1] / b[1];
  3763. out[2] = a[2] / b[2];
  3764. return out;
  3765. }
  3766. /**
  3767. * Math.ceil the components of a vec3
  3768. *
  3769. * @param {vec3} out the receiving vector
  3770. * @param {ReadonlyVec3} a vector to ceil
  3771. * @returns {vec3} out
  3772. */
  3773. function ceil(out, a) {
  3774. out[0] = Math.ceil(a[0]);
  3775. out[1] = Math.ceil(a[1]);
  3776. out[2] = Math.ceil(a[2]);
  3777. return out;
  3778. }
  3779. /**
  3780. * Math.floor the components of a vec3
  3781. *
  3782. * @param {vec3} out the receiving vector
  3783. * @param {ReadonlyVec3} a vector to floor
  3784. * @returns {vec3} out
  3785. */
  3786. function floor(out, a) {
  3787. out[0] = Math.floor(a[0]);
  3788. out[1] = Math.floor(a[1]);
  3789. out[2] = Math.floor(a[2]);
  3790. return out;
  3791. }
  3792. /**
  3793. * Returns the minimum of two vec3's
  3794. *
  3795. * @param {vec3} out the receiving vector
  3796. * @param {ReadonlyVec3} a the first operand
  3797. * @param {ReadonlyVec3} b the second operand
  3798. * @returns {vec3} out
  3799. */
  3800. function min(out, a, b) {
  3801. out[0] = Math.min(a[0], b[0]);
  3802. out[1] = Math.min(a[1], b[1]);
  3803. out[2] = Math.min(a[2], b[2]);
  3804. return out;
  3805. }
  3806. /**
  3807. * Returns the maximum of two vec3's
  3808. *
  3809. * @param {vec3} out the receiving vector
  3810. * @param {ReadonlyVec3} a the first operand
  3811. * @param {ReadonlyVec3} b the second operand
  3812. * @returns {vec3} out
  3813. */
  3814. function max(out, a, b) {
  3815. out[0] = Math.max(a[0], b[0]);
  3816. out[1] = Math.max(a[1], b[1]);
  3817. out[2] = Math.max(a[2], b[2]);
  3818. return out;
  3819. }
  3820. /**
  3821. * Math.round the components of a vec3
  3822. *
  3823. * @param {vec3} out the receiving vector
  3824. * @param {ReadonlyVec3} a vector to round
  3825. * @returns {vec3} out
  3826. */
  3827. function round(out, a) {
  3828. out[0] = Math.round(a[0]);
  3829. out[1] = Math.round(a[1]);
  3830. out[2] = Math.round(a[2]);
  3831. return out;
  3832. }
  3833. /**
  3834. * Scales a vec3 by a scalar number
  3835. *
  3836. * @param {vec3} out the receiving vector
  3837. * @param {ReadonlyVec3} a the vector to scale
  3838. * @param {Number} b amount to scale the vector by
  3839. * @returns {vec3} out
  3840. */
  3841. function scale$4(out, a, b) {
  3842. out[0] = a[0] * b;
  3843. out[1] = a[1] * b;
  3844. out[2] = a[2] * b;
  3845. return out;
  3846. }
  3847. /**
  3848. * Adds two vec3's after scaling the second operand by a scalar value
  3849. *
  3850. * @param {vec3} out the receiving vector
  3851. * @param {ReadonlyVec3} a the first operand
  3852. * @param {ReadonlyVec3} b the second operand
  3853. * @param {Number} scale the amount to scale b by before adding
  3854. * @returns {vec3} out
  3855. */
  3856. function scaleAndAdd(out, a, b, scale) {
  3857. out[0] = a[0] + b[0] * scale;
  3858. out[1] = a[1] + b[1] * scale;
  3859. out[2] = a[2] + b[2] * scale;
  3860. return out;
  3861. }
  3862. /**
  3863. * Calculates the euclidian distance between two vec3's
  3864. *
  3865. * @param {ReadonlyVec3} a the first operand
  3866. * @param {ReadonlyVec3} b the second operand
  3867. * @returns {Number} distance between a and b
  3868. */
  3869. function distance(a, b) {
  3870. var x = b[0] - a[0];
  3871. var y = b[1] - a[1];
  3872. var z = b[2] - a[2];
  3873. return Math.hypot(x, y, z);
  3874. }
  3875. /**
  3876. * Calculates the squared euclidian distance between two vec3's
  3877. *
  3878. * @param {ReadonlyVec3} a the first operand
  3879. * @param {ReadonlyVec3} b the second operand
  3880. * @returns {Number} squared distance between a and b
  3881. */
  3882. function squaredDistance(a, b) {
  3883. var x = b[0] - a[0];
  3884. var y = b[1] - a[1];
  3885. var z = b[2] - a[2];
  3886. return x * x + y * y + z * z;
  3887. }
  3888. /**
  3889. * Calculates the squared length of a vec3
  3890. *
  3891. * @param {ReadonlyVec3} a vector to calculate squared length of
  3892. * @returns {Number} squared length of a
  3893. */
  3894. function squaredLength(a) {
  3895. var x = a[0];
  3896. var y = a[1];
  3897. var z = a[2];
  3898. return x * x + y * y + z * z;
  3899. }
  3900. /**
  3901. * Negates the components of a vec3
  3902. *
  3903. * @param {vec3} out the receiving vector
  3904. * @param {ReadonlyVec3} a vector to negate
  3905. * @returns {vec3} out
  3906. */
  3907. function negate(out, a) {
  3908. out[0] = -a[0];
  3909. out[1] = -a[1];
  3910. out[2] = -a[2];
  3911. return out;
  3912. }
  3913. /**
  3914. * Returns the inverse of the components of a vec3
  3915. *
  3916. * @param {vec3} out the receiving vector
  3917. * @param {ReadonlyVec3} a vector to invert
  3918. * @returns {vec3} out
  3919. */
  3920. function inverse(out, a) {
  3921. out[0] = 1.0 / a[0];
  3922. out[1] = 1.0 / a[1];
  3923. out[2] = 1.0 / a[2];
  3924. return out;
  3925. }
  3926. /**
  3927. * Normalize a vec3
  3928. *
  3929. * @param {vec3} out the receiving vector
  3930. * @param {ReadonlyVec3} a vector to normalize
  3931. * @returns {vec3} out
  3932. */
  3933. function normalize(out, a) {
  3934. var x = a[0];
  3935. var y = a[1];
  3936. var z = a[2];
  3937. var len = x * x + y * y + z * z;
  3938. if (len > 0) {
  3939. //TODO: evaluate use of glm_invsqrt here?
  3940. len = 1 / Math.sqrt(len);
  3941. }
  3942. out[0] = a[0] * len;
  3943. out[1] = a[1] * len;
  3944. out[2] = a[2] * len;
  3945. return out;
  3946. }
  3947. /**
  3948. * Calculates the dot product of two vec3's
  3949. *
  3950. * @param {ReadonlyVec3} a the first operand
  3951. * @param {ReadonlyVec3} b the second operand
  3952. * @returns {Number} dot product of a and b
  3953. */
  3954. function dot(a, b) {
  3955. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
  3956. }
  3957. /**
  3958. * Computes the cross product of two vec3's
  3959. *
  3960. * @param {vec3} out the receiving vector
  3961. * @param {ReadonlyVec3} a the first operand
  3962. * @param {ReadonlyVec3} b the second operand
  3963. * @returns {vec3} out
  3964. */
  3965. function cross(out, a, b) {
  3966. var ax = a[0],
  3967. ay = a[1],
  3968. az = a[2];
  3969. var bx = b[0],
  3970. by = b[1],
  3971. bz = b[2];
  3972. out[0] = ay * bz - az * by;
  3973. out[1] = az * bx - ax * bz;
  3974. out[2] = ax * by - ay * bx;
  3975. return out;
  3976. }
  3977. /**
  3978. * Performs a linear interpolation between two vec3's
  3979. *
  3980. * @param {vec3} out the receiving vector
  3981. * @param {ReadonlyVec3} a the first operand
  3982. * @param {ReadonlyVec3} b the second operand
  3983. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  3984. * @returns {vec3} out
  3985. */
  3986. function lerp(out, a, b, t) {
  3987. var ax = a[0];
  3988. var ay = a[1];
  3989. var az = a[2];
  3990. out[0] = ax + t * (b[0] - ax);
  3991. out[1] = ay + t * (b[1] - ay);
  3992. out[2] = az + t * (b[2] - az);
  3993. return out;
  3994. }
  3995. /**
  3996. * Performs a hermite interpolation with two control points
  3997. *
  3998. * @param {vec3} out the receiving vector
  3999. * @param {ReadonlyVec3} a the first operand
  4000. * @param {ReadonlyVec3} b the second operand
  4001. * @param {ReadonlyVec3} c the third operand
  4002. * @param {ReadonlyVec3} d the fourth operand
  4003. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  4004. * @returns {vec3} out
  4005. */
  4006. function hermite(out, a, b, c, d, t) {
  4007. var factorTimes2 = t * t;
  4008. var factor1 = factorTimes2 * (2 * t - 3) + 1;
  4009. var factor2 = factorTimes2 * (t - 2) + t;
  4010. var factor3 = factorTimes2 * (t - 1);
  4011. var factor4 = factorTimes2 * (3 - 2 * t);
  4012. out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
  4013. out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
  4014. out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
  4015. return out;
  4016. }
  4017. /**
  4018. * Performs a bezier interpolation with two control points
  4019. *
  4020. * @param {vec3} out the receiving vector
  4021. * @param {ReadonlyVec3} a the first operand
  4022. * @param {ReadonlyVec3} b the second operand
  4023. * @param {ReadonlyVec3} c the third operand
  4024. * @param {ReadonlyVec3} d the fourth operand
  4025. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  4026. * @returns {vec3} out
  4027. */
  4028. function bezier(out, a, b, c, d, t) {
  4029. var inverseFactor = 1 - t;
  4030. var inverseFactorTimesTwo = inverseFactor * inverseFactor;
  4031. var factorTimes2 = t * t;
  4032. var factor1 = inverseFactorTimesTwo * inverseFactor;
  4033. var factor2 = 3 * t * inverseFactorTimesTwo;
  4034. var factor3 = 3 * factorTimes2 * inverseFactor;
  4035. var factor4 = factorTimes2 * t;
  4036. out[0] = a[0] * factor1 + b[0] * factor2 + c[0] * factor3 + d[0] * factor4;
  4037. out[1] = a[1] * factor1 + b[1] * factor2 + c[1] * factor3 + d[1] * factor4;
  4038. out[2] = a[2] * factor1 + b[2] * factor2 + c[2] * factor3 + d[2] * factor4;
  4039. return out;
  4040. }
  4041. /**
  4042. * Generates a random vector with the given scale
  4043. *
  4044. * @param {vec3} out the receiving vector
  4045. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  4046. * @returns {vec3} out
  4047. */
  4048. function random(out, scale) {
  4049. scale = scale || 1.0;
  4050. var r = RANDOM() * 2.0 * Math.PI;
  4051. var z = RANDOM() * 2.0 - 1.0;
  4052. var zScale = Math.sqrt(1.0 - z * z) * scale;
  4053. out[0] = Math.cos(r) * zScale;
  4054. out[1] = Math.sin(r) * zScale;
  4055. out[2] = z * scale;
  4056. return out;
  4057. }
  4058. /**
  4059. * Transforms the vec3 with a mat4.
  4060. * 4th vector component is implicitly '1'
  4061. *
  4062. * @param {vec3} out the receiving vector
  4063. * @param {ReadonlyVec3} a the vector to transform
  4064. * @param {ReadonlyMat4} m matrix to transform with
  4065. * @returns {vec3} out
  4066. */
  4067. function transformMat4(out, a, m) {
  4068. var x = a[0],
  4069. y = a[1],
  4070. z = a[2];
  4071. var w = m[3] * x + m[7] * y + m[11] * z + m[15];
  4072. w = w || 1.0;
  4073. out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w;
  4074. out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w;
  4075. out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w;
  4076. return out;
  4077. }
  4078. /**
  4079. * Transforms the vec3 with a mat3.
  4080. *
  4081. * @param {vec3} out the receiving vector
  4082. * @param {ReadonlyVec3} a the vector to transform
  4083. * @param {ReadonlyMat3} m the 3x3 matrix to transform with
  4084. * @returns {vec3} out
  4085. */
  4086. function transformMat3(out, a, m) {
  4087. var x = a[0],
  4088. y = a[1],
  4089. z = a[2];
  4090. out[0] = x * m[0] + y * m[3] + z * m[6];
  4091. out[1] = x * m[1] + y * m[4] + z * m[7];
  4092. out[2] = x * m[2] + y * m[5] + z * m[8];
  4093. return out;
  4094. }
  4095. /**
  4096. * Transforms the vec3 with a quat
  4097. * Can also be used for dual quaternions. (Multiply it with the real part)
  4098. *
  4099. * @param {vec3} out the receiving vector
  4100. * @param {ReadonlyVec3} a the vector to transform
  4101. * @param {ReadonlyQuat} q quaternion to transform with
  4102. * @returns {vec3} out
  4103. */
  4104. function transformQuat(out, a, q) {
  4105. // benchmarks: https://jsperf.com/quaternion-transform-vec3-implementations-fixed
  4106. var qx = q[0],
  4107. qy = q[1],
  4108. qz = q[2],
  4109. qw = q[3];
  4110. var x = a[0],
  4111. y = a[1],
  4112. z = a[2]; // var qvec = [qx, qy, qz];
  4113. // var uv = vec3.cross([], qvec, a);
  4114. var uvx = qy * z - qz * y,
  4115. uvy = qz * x - qx * z,
  4116. uvz = qx * y - qy * x; // var uuv = vec3.cross([], qvec, uv);
  4117. var uuvx = qy * uvz - qz * uvy,
  4118. uuvy = qz * uvx - qx * uvz,
  4119. uuvz = qx * uvy - qy * uvx; // vec3.scale(uv, uv, 2 * w);
  4120. var w2 = qw * 2;
  4121. uvx *= w2;
  4122. uvy *= w2;
  4123. uvz *= w2; // vec3.scale(uuv, uuv, 2);
  4124. uuvx *= 2;
  4125. uuvy *= 2;
  4126. uuvz *= 2; // return vec3.add(out, a, vec3.add(out, uv, uuv));
  4127. out[0] = x + uvx + uuvx;
  4128. out[1] = y + uvy + uuvy;
  4129. out[2] = z + uvz + uuvz;
  4130. return out;
  4131. }
  4132. /**
  4133. * Rotate a 3D vector around the x-axis
  4134. * @param {vec3} out The receiving vec3
  4135. * @param {ReadonlyVec3} a The vec3 point to rotate
  4136. * @param {ReadonlyVec3} b The origin of the rotation
  4137. * @param {Number} rad The angle of rotation in radians
  4138. * @returns {vec3} out
  4139. */
  4140. function rotateX$1(out, a, b, rad) {
  4141. var p = [],
  4142. r = []; //Translate point to the origin
  4143. p[0] = a[0] - b[0];
  4144. p[1] = a[1] - b[1];
  4145. p[2] = a[2] - b[2]; //perform rotation
  4146. r[0] = p[0];
  4147. r[1] = p[1] * Math.cos(rad) - p[2] * Math.sin(rad);
  4148. r[2] = p[1] * Math.sin(rad) + p[2] * Math.cos(rad); //translate to correct position
  4149. out[0] = r[0] + b[0];
  4150. out[1] = r[1] + b[1];
  4151. out[2] = r[2] + b[2];
  4152. return out;
  4153. }
  4154. /**
  4155. * Rotate a 3D vector around the y-axis
  4156. * @param {vec3} out The receiving vec3
  4157. * @param {ReadonlyVec3} a The vec3 point to rotate
  4158. * @param {ReadonlyVec3} b The origin of the rotation
  4159. * @param {Number} rad The angle of rotation in radians
  4160. * @returns {vec3} out
  4161. */
  4162. function rotateY$1(out, a, b, rad) {
  4163. var p = [],
  4164. r = []; //Translate point to the origin
  4165. p[0] = a[0] - b[0];
  4166. p[1] = a[1] - b[1];
  4167. p[2] = a[2] - b[2]; //perform rotation
  4168. r[0] = p[2] * Math.sin(rad) + p[0] * Math.cos(rad);
  4169. r[1] = p[1];
  4170. r[2] = p[2] * Math.cos(rad) - p[0] * Math.sin(rad); //translate to correct position
  4171. out[0] = r[0] + b[0];
  4172. out[1] = r[1] + b[1];
  4173. out[2] = r[2] + b[2];
  4174. return out;
  4175. }
  4176. /**
  4177. * Rotate a 3D vector around the z-axis
  4178. * @param {vec3} out The receiving vec3
  4179. * @param {ReadonlyVec3} a The vec3 point to rotate
  4180. * @param {ReadonlyVec3} b The origin of the rotation
  4181. * @param {Number} rad The angle of rotation in radians
  4182. * @returns {vec3} out
  4183. */
  4184. function rotateZ$1(out, a, b, rad) {
  4185. var p = [],
  4186. r = []; //Translate point to the origin
  4187. p[0] = a[0] - b[0];
  4188. p[1] = a[1] - b[1];
  4189. p[2] = a[2] - b[2]; //perform rotation
  4190. r[0] = p[0] * Math.cos(rad) - p[1] * Math.sin(rad);
  4191. r[1] = p[0] * Math.sin(rad) + p[1] * Math.cos(rad);
  4192. r[2] = p[2]; //translate to correct position
  4193. out[0] = r[0] + b[0];
  4194. out[1] = r[1] + b[1];
  4195. out[2] = r[2] + b[2];
  4196. return out;
  4197. }
  4198. /**
  4199. * Get the angle between two 3D vectors
  4200. * @param {ReadonlyVec3} a The first operand
  4201. * @param {ReadonlyVec3} b The second operand
  4202. * @returns {Number} The angle in radians
  4203. */
  4204. function angle(a, b) {
  4205. var ax = a[0],
  4206. ay = a[1],
  4207. az = a[2],
  4208. bx = b[0],
  4209. by = b[1],
  4210. bz = b[2],
  4211. mag1 = Math.sqrt(ax * ax + ay * ay + az * az),
  4212. mag2 = Math.sqrt(bx * bx + by * by + bz * bz),
  4213. mag = mag1 * mag2,
  4214. cosine = mag && dot(a, b) / mag;
  4215. return Math.acos(Math.min(Math.max(cosine, -1), 1));
  4216. }
  4217. /**
  4218. * Set the components of a vec3 to zero
  4219. *
  4220. * @param {vec3} out the receiving vector
  4221. * @returns {vec3} out
  4222. */
  4223. function zero(out) {
  4224. out[0] = 0.0;
  4225. out[1] = 0.0;
  4226. out[2] = 0.0;
  4227. return out;
  4228. }
  4229. /**
  4230. * Returns a string representation of a vector
  4231. *
  4232. * @param {ReadonlyVec3} a vector to represent as a string
  4233. * @returns {String} string representation of the vector
  4234. */
  4235. function str$4(a) {
  4236. return "vec3(" + a[0] + ", " + a[1] + ", " + a[2] + ")";
  4237. }
  4238. /**
  4239. * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)
  4240. *
  4241. * @param {ReadonlyVec3} a The first vector.
  4242. * @param {ReadonlyVec3} b The second vector.
  4243. * @returns {Boolean} True if the vectors are equal, false otherwise.
  4244. */
  4245. function exactEquals$4(a, b) {
  4246. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2];
  4247. }
  4248. /**
  4249. * Returns whether or not the vectors have approximately the same elements in the same position.
  4250. *
  4251. * @param {ReadonlyVec3} a The first vector.
  4252. * @param {ReadonlyVec3} b The second vector.
  4253. * @returns {Boolean} True if the vectors are equal, false otherwise.
  4254. */
  4255. function equals$5(a, b) {
  4256. var a0 = a[0],
  4257. a1 = a[1],
  4258. a2 = a[2];
  4259. var b0 = b[0],
  4260. b1 = b[1],
  4261. b2 = b[2];
  4262. return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2));
  4263. }
  4264. /**
  4265. * Alias for {@link vec3.subtract}
  4266. * @function
  4267. */
  4268. var sub$4 = subtract$4;
  4269. /**
  4270. * Alias for {@link vec3.multiply}
  4271. * @function
  4272. */
  4273. var mul$4 = multiply$4;
  4274. /**
  4275. * Alias for {@link vec3.divide}
  4276. * @function
  4277. */
  4278. var div = divide;
  4279. /**
  4280. * Alias for {@link vec3.distance}
  4281. * @function
  4282. */
  4283. var dist = distance;
  4284. /**
  4285. * Alias for {@link vec3.squaredDistance}
  4286. * @function
  4287. */
  4288. var sqrDist = squaredDistance;
  4289. /**
  4290. * Alias for {@link vec3.length}
  4291. * @function
  4292. */
  4293. var len = length;
  4294. /**
  4295. * Alias for {@link vec3.squaredLength}
  4296. * @function
  4297. */
  4298. var sqrLen = squaredLength;
  4299. /**
  4300. * Perform some operation over an array of vec3s.
  4301. *
  4302. * @param {Array} a the array of vectors to iterate over
  4303. * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed
  4304. * @param {Number} offset Number of elements to skip at the beginning of the array
  4305. * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array
  4306. * @param {Function} fn Function to call for each vector in the array
  4307. * @param {Object} [arg] additional argument to pass to fn
  4308. * @returns {Array} a
  4309. * @function
  4310. */
  4311. var forEach = function () {
  4312. var vec = create$4();
  4313. return function (a, stride, offset, count, fn, arg) {
  4314. var i, l;
  4315. if (!stride) {
  4316. stride = 3;
  4317. }
  4318. if (!offset) {
  4319. offset = 0;
  4320. }
  4321. if (count) {
  4322. l = Math.min(count * stride + offset, a.length);
  4323. } else {
  4324. l = a.length;
  4325. }
  4326. for (i = offset; i < l; i += stride) {
  4327. vec[0] = a[i];
  4328. vec[1] = a[i + 1];
  4329. vec[2] = a[i + 2];
  4330. fn(vec, vec, arg);
  4331. a[i] = vec[0];
  4332. a[i + 1] = vec[1];
  4333. a[i + 2] = vec[2];
  4334. }
  4335. return a;
  4336. };
  4337. }();
  4338. var vec3 = /*#__PURE__*/Object.freeze({
  4339. __proto__: null,
  4340. create: create$4,
  4341. clone: clone$4,
  4342. length: length,
  4343. fromValues: fromValues$4,
  4344. copy: copy$4,
  4345. set: set$4,
  4346. add: add$4,
  4347. subtract: subtract$4,
  4348. multiply: multiply$4,
  4349. divide: divide,
  4350. ceil: ceil,
  4351. floor: floor,
  4352. min: min,
  4353. max: max,
  4354. round: round,
  4355. scale: scale$4,
  4356. scaleAndAdd: scaleAndAdd,
  4357. distance: distance,
  4358. squaredDistance: squaredDistance,
  4359. squaredLength: squaredLength,
  4360. negate: negate,
  4361. inverse: inverse,
  4362. normalize: normalize,
  4363. dot: dot,
  4364. cross: cross,
  4365. lerp: lerp,
  4366. hermite: hermite,
  4367. bezier: bezier,
  4368. random: random,
  4369. transformMat4: transformMat4,
  4370. transformMat3: transformMat3,
  4371. transformQuat: transformQuat,
  4372. rotateX: rotateX$1,
  4373. rotateY: rotateY$1,
  4374. rotateZ: rotateZ$1,
  4375. angle: angle,
  4376. zero: zero,
  4377. str: str$4,
  4378. exactEquals: exactEquals$4,
  4379. equals: equals$5,
  4380. sub: sub$4,
  4381. mul: mul$4,
  4382. div: div,
  4383. dist: dist,
  4384. sqrDist: sqrDist,
  4385. len: len,
  4386. sqrLen: sqrLen,
  4387. forEach: forEach
  4388. });
  4389. /**
  4390. * 4 Dimensional Vector
  4391. * @module vec4
  4392. */
  4393. /**
  4394. * Creates a new, empty vec4
  4395. *
  4396. * @returns {vec4} a new 4D vector
  4397. */
  4398. function create$5() {
  4399. var out = new ARRAY_TYPE(4);
  4400. if (ARRAY_TYPE != Float32Array) {
  4401. out[0] = 0;
  4402. out[1] = 0;
  4403. out[2] = 0;
  4404. out[3] = 0;
  4405. }
  4406. return out;
  4407. }
  4408. /**
  4409. * Creates a new vec4 initialized with values from an existing vector
  4410. *
  4411. * @param {ReadonlyVec4} a vector to clone
  4412. * @returns {vec4} a new 4D vector
  4413. */
  4414. function clone$5(a) {
  4415. var out = new ARRAY_TYPE(4);
  4416. out[0] = a[0];
  4417. out[1] = a[1];
  4418. out[2] = a[2];
  4419. out[3] = a[3];
  4420. return out;
  4421. }
  4422. /**
  4423. * Creates a new vec4 initialized with the given values
  4424. *
  4425. * @param {Number} x X component
  4426. * @param {Number} y Y component
  4427. * @param {Number} z Z component
  4428. * @param {Number} w W component
  4429. * @returns {vec4} a new 4D vector
  4430. */
  4431. function fromValues$5(x, y, z, w) {
  4432. var out = new ARRAY_TYPE(4);
  4433. out[0] = x;
  4434. out[1] = y;
  4435. out[2] = z;
  4436. out[3] = w;
  4437. return out;
  4438. }
  4439. /**
  4440. * Copy the values from one vec4 to another
  4441. *
  4442. * @param {vec4} out the receiving vector
  4443. * @param {ReadonlyVec4} a the source vector
  4444. * @returns {vec4} out
  4445. */
  4446. function copy$5(out, a) {
  4447. out[0] = a[0];
  4448. out[1] = a[1];
  4449. out[2] = a[2];
  4450. out[3] = a[3];
  4451. return out;
  4452. }
  4453. /**
  4454. * Set the components of a vec4 to the given values
  4455. *
  4456. * @param {vec4} out the receiving vector
  4457. * @param {Number} x X component
  4458. * @param {Number} y Y component
  4459. * @param {Number} z Z component
  4460. * @param {Number} w W component
  4461. * @returns {vec4} out
  4462. */
  4463. function set$5(out, x, y, z, w) {
  4464. out[0] = x;
  4465. out[1] = y;
  4466. out[2] = z;
  4467. out[3] = w;
  4468. return out;
  4469. }
  4470. /**
  4471. * Adds two vec4's
  4472. *
  4473. * @param {vec4} out the receiving vector
  4474. * @param {ReadonlyVec4} a the first operand
  4475. * @param {ReadonlyVec4} b the second operand
  4476. * @returns {vec4} out
  4477. */
  4478. function add$5(out, a, b) {
  4479. out[0] = a[0] + b[0];
  4480. out[1] = a[1] + b[1];
  4481. out[2] = a[2] + b[2];
  4482. out[3] = a[3] + b[3];
  4483. return out;
  4484. }
  4485. /**
  4486. * Subtracts vector b from vector a
  4487. *
  4488. * @param {vec4} out the receiving vector
  4489. * @param {ReadonlyVec4} a the first operand
  4490. * @param {ReadonlyVec4} b the second operand
  4491. * @returns {vec4} out
  4492. */
  4493. function subtract$5(out, a, b) {
  4494. out[0] = a[0] - b[0];
  4495. out[1] = a[1] - b[1];
  4496. out[2] = a[2] - b[2];
  4497. out[3] = a[3] - b[3];
  4498. return out;
  4499. }
  4500. /**
  4501. * Multiplies two vec4's
  4502. *
  4503. * @param {vec4} out the receiving vector
  4504. * @param {ReadonlyVec4} a the first operand
  4505. * @param {ReadonlyVec4} b the second operand
  4506. * @returns {vec4} out
  4507. */
  4508. function multiply$5(out, a, b) {
  4509. out[0] = a[0] * b[0];
  4510. out[1] = a[1] * b[1];
  4511. out[2] = a[2] * b[2];
  4512. out[3] = a[3] * b[3];
  4513. return out;
  4514. }
  4515. /**
  4516. * Divides two vec4's
  4517. *
  4518. * @param {vec4} out the receiving vector
  4519. * @param {ReadonlyVec4} a the first operand
  4520. * @param {ReadonlyVec4} b the second operand
  4521. * @returns {vec4} out
  4522. */
  4523. function divide$1(out, a, b) {
  4524. out[0] = a[0] / b[0];
  4525. out[1] = a[1] / b[1];
  4526. out[2] = a[2] / b[2];
  4527. out[3] = a[3] / b[3];
  4528. return out;
  4529. }
  4530. /**
  4531. * Math.ceil the components of a vec4
  4532. *
  4533. * @param {vec4} out the receiving vector
  4534. * @param {ReadonlyVec4} a vector to ceil
  4535. * @returns {vec4} out
  4536. */
  4537. function ceil$1(out, a) {
  4538. out[0] = Math.ceil(a[0]);
  4539. out[1] = Math.ceil(a[1]);
  4540. out[2] = Math.ceil(a[2]);
  4541. out[3] = Math.ceil(a[3]);
  4542. return out;
  4543. }
  4544. /**
  4545. * Math.floor the components of a vec4
  4546. *
  4547. * @param {vec4} out the receiving vector
  4548. * @param {ReadonlyVec4} a vector to floor
  4549. * @returns {vec4} out
  4550. */
  4551. function floor$1(out, a) {
  4552. out[0] = Math.floor(a[0]);
  4553. out[1] = Math.floor(a[1]);
  4554. out[2] = Math.floor(a[2]);
  4555. out[3] = Math.floor(a[3]);
  4556. return out;
  4557. }
  4558. /**
  4559. * Returns the minimum of two vec4's
  4560. *
  4561. * @param {vec4} out the receiving vector
  4562. * @param {ReadonlyVec4} a the first operand
  4563. * @param {ReadonlyVec4} b the second operand
  4564. * @returns {vec4} out
  4565. */
  4566. function min$1(out, a, b) {
  4567. out[0] = Math.min(a[0], b[0]);
  4568. out[1] = Math.min(a[1], b[1]);
  4569. out[2] = Math.min(a[2], b[2]);
  4570. out[3] = Math.min(a[3], b[3]);
  4571. return out;
  4572. }
  4573. /**
  4574. * Returns the maximum of two vec4's
  4575. *
  4576. * @param {vec4} out the receiving vector
  4577. * @param {ReadonlyVec4} a the first operand
  4578. * @param {ReadonlyVec4} b the second operand
  4579. * @returns {vec4} out
  4580. */
  4581. function max$1(out, a, b) {
  4582. out[0] = Math.max(a[0], b[0]);
  4583. out[1] = Math.max(a[1], b[1]);
  4584. out[2] = Math.max(a[2], b[2]);
  4585. out[3] = Math.max(a[3], b[3]);
  4586. return out;
  4587. }
  4588. /**
  4589. * Math.round the components of a vec4
  4590. *
  4591. * @param {vec4} out the receiving vector
  4592. * @param {ReadonlyVec4} a vector to round
  4593. * @returns {vec4} out
  4594. */
  4595. function round$1(out, a) {
  4596. out[0] = Math.round(a[0]);
  4597. out[1] = Math.round(a[1]);
  4598. out[2] = Math.round(a[2]);
  4599. out[3] = Math.round(a[3]);
  4600. return out;
  4601. }
  4602. /**
  4603. * Scales a vec4 by a scalar number
  4604. *
  4605. * @param {vec4} out the receiving vector
  4606. * @param {ReadonlyVec4} a the vector to scale
  4607. * @param {Number} b amount to scale the vector by
  4608. * @returns {vec4} out
  4609. */
  4610. function scale$5(out, a, b) {
  4611. out[0] = a[0] * b;
  4612. out[1] = a[1] * b;
  4613. out[2] = a[2] * b;
  4614. out[3] = a[3] * b;
  4615. return out;
  4616. }
  4617. /**
  4618. * Adds two vec4's after scaling the second operand by a scalar value
  4619. *
  4620. * @param {vec4} out the receiving vector
  4621. * @param {ReadonlyVec4} a the first operand
  4622. * @param {ReadonlyVec4} b the second operand
  4623. * @param {Number} scale the amount to scale b by before adding
  4624. * @returns {vec4} out
  4625. */
  4626. function scaleAndAdd$1(out, a, b, scale) {
  4627. out[0] = a[0] + b[0] * scale;
  4628. out[1] = a[1] + b[1] * scale;
  4629. out[2] = a[2] + b[2] * scale;
  4630. out[3] = a[3] + b[3] * scale;
  4631. return out;
  4632. }
  4633. /**
  4634. * Calculates the euclidian distance between two vec4's
  4635. *
  4636. * @param {ReadonlyVec4} a the first operand
  4637. * @param {ReadonlyVec4} b the second operand
  4638. * @returns {Number} distance between a and b
  4639. */
  4640. function distance$1(a, b) {
  4641. var x = b[0] - a[0];
  4642. var y = b[1] - a[1];
  4643. var z = b[2] - a[2];
  4644. var w = b[3] - a[3];
  4645. return Math.hypot(x, y, z, w);
  4646. }
  4647. /**
  4648. * Calculates the squared euclidian distance between two vec4's
  4649. *
  4650. * @param {ReadonlyVec4} a the first operand
  4651. * @param {ReadonlyVec4} b the second operand
  4652. * @returns {Number} squared distance between a and b
  4653. */
  4654. function squaredDistance$1(a, b) {
  4655. var x = b[0] - a[0];
  4656. var y = b[1] - a[1];
  4657. var z = b[2] - a[2];
  4658. var w = b[3] - a[3];
  4659. return x * x + y * y + z * z + w * w;
  4660. }
  4661. /**
  4662. * Calculates the length of a vec4
  4663. *
  4664. * @param {ReadonlyVec4} a vector to calculate length of
  4665. * @returns {Number} length of a
  4666. */
  4667. function length$1(a) {
  4668. var x = a[0];
  4669. var y = a[1];
  4670. var z = a[2];
  4671. var w = a[3];
  4672. return Math.hypot(x, y, z, w);
  4673. }
  4674. /**
  4675. * Calculates the squared length of a vec4
  4676. *
  4677. * @param {ReadonlyVec4} a vector to calculate squared length of
  4678. * @returns {Number} squared length of a
  4679. */
  4680. function squaredLength$1(a) {
  4681. var x = a[0];
  4682. var y = a[1];
  4683. var z = a[2];
  4684. var w = a[3];
  4685. return x * x + y * y + z * z + w * w;
  4686. }
  4687. /**
  4688. * Negates the components of a vec4
  4689. *
  4690. * @param {vec4} out the receiving vector
  4691. * @param {ReadonlyVec4} a vector to negate
  4692. * @returns {vec4} out
  4693. */
  4694. function negate$1(out, a) {
  4695. out[0] = -a[0];
  4696. out[1] = -a[1];
  4697. out[2] = -a[2];
  4698. out[3] = -a[3];
  4699. return out;
  4700. }
  4701. /**
  4702. * Returns the inverse of the components of a vec4
  4703. *
  4704. * @param {vec4} out the receiving vector
  4705. * @param {ReadonlyVec4} a vector to invert
  4706. * @returns {vec4} out
  4707. */
  4708. function inverse$1(out, a) {
  4709. out[0] = 1.0 / a[0];
  4710. out[1] = 1.0 / a[1];
  4711. out[2] = 1.0 / a[2];
  4712. out[3] = 1.0 / a[3];
  4713. return out;
  4714. }
  4715. /**
  4716. * Normalize a vec4
  4717. *
  4718. * @param {vec4} out the receiving vector
  4719. * @param {ReadonlyVec4} a vector to normalize
  4720. * @returns {vec4} out
  4721. */
  4722. function normalize$1(out, a) {
  4723. var x = a[0];
  4724. var y = a[1];
  4725. var z = a[2];
  4726. var w = a[3];
  4727. var len = x * x + y * y + z * z + w * w;
  4728. if (len > 0) {
  4729. len = 1 / Math.sqrt(len);
  4730. }
  4731. out[0] = x * len;
  4732. out[1] = y * len;
  4733. out[2] = z * len;
  4734. out[3] = w * len;
  4735. return out;
  4736. }
  4737. /**
  4738. * Calculates the dot product of two vec4's
  4739. *
  4740. * @param {ReadonlyVec4} a the first operand
  4741. * @param {ReadonlyVec4} b the second operand
  4742. * @returns {Number} dot product of a and b
  4743. */
  4744. function dot$1(a, b) {
  4745. return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3];
  4746. }
  4747. /**
  4748. * Returns the cross-product of three vectors in a 4-dimensional space
  4749. *
  4750. * @param {ReadonlyVec4} result the receiving vector
  4751. * @param {ReadonlyVec4} U the first vector
  4752. * @param {ReadonlyVec4} V the second vector
  4753. * @param {ReadonlyVec4} W the third vector
  4754. * @returns {vec4} result
  4755. */
  4756. function cross$1(out, u, v, w) {
  4757. var A = v[0] * w[1] - v[1] * w[0],
  4758. B = v[0] * w[2] - v[2] * w[0],
  4759. C = v[0] * w[3] - v[3] * w[0],
  4760. D = v[1] * w[2] - v[2] * w[1],
  4761. E = v[1] * w[3] - v[3] * w[1],
  4762. F = v[2] * w[3] - v[3] * w[2];
  4763. var G = u[0];
  4764. var H = u[1];
  4765. var I = u[2];
  4766. var J = u[3];
  4767. out[0] = H * F - I * E + J * D;
  4768. out[1] = -(G * F) + I * C - J * B;
  4769. out[2] = G * E - H * C + J * A;
  4770. out[3] = -(G * D) + H * B - I * A;
  4771. return out;
  4772. }
  4773. /**
  4774. * Performs a linear interpolation between two vec4's
  4775. *
  4776. * @param {vec4} out the receiving vector
  4777. * @param {ReadonlyVec4} a the first operand
  4778. * @param {ReadonlyVec4} b the second operand
  4779. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  4780. * @returns {vec4} out
  4781. */
  4782. function lerp$1(out, a, b, t) {
  4783. var ax = a[0];
  4784. var ay = a[1];
  4785. var az = a[2];
  4786. var aw = a[3];
  4787. out[0] = ax + t * (b[0] - ax);
  4788. out[1] = ay + t * (b[1] - ay);
  4789. out[2] = az + t * (b[2] - az);
  4790. out[3] = aw + t * (b[3] - aw);
  4791. return out;
  4792. }
  4793. /**
  4794. * Generates a random vector with the given scale
  4795. *
  4796. * @param {vec4} out the receiving vector
  4797. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  4798. * @returns {vec4} out
  4799. */
  4800. function random$1(out, scale) {
  4801. scale = scale || 1.0; // Marsaglia, George. Choosing a Point from the Surface of a
  4802. // Sphere. Ann. Math. Statist. 43 (1972), no. 2, 645--646.
  4803. // http://projecteuclid.org/euclid.aoms/1177692644;
  4804. var v1, v2, v3, v4;
  4805. var s1, s2;
  4806. do {
  4807. v1 = RANDOM() * 2 - 1;
  4808. v2 = RANDOM() * 2 - 1;
  4809. s1 = v1 * v1 + v2 * v2;
  4810. } while (s1 >= 1);
  4811. do {
  4812. v3 = RANDOM() * 2 - 1;
  4813. v4 = RANDOM() * 2 - 1;
  4814. s2 = v3 * v3 + v4 * v4;
  4815. } while (s2 >= 1);
  4816. var d = Math.sqrt((1 - s1) / s2);
  4817. out[0] = scale * v1;
  4818. out[1] = scale * v2;
  4819. out[2] = scale * v3 * d;
  4820. out[3] = scale * v4 * d;
  4821. return out;
  4822. }
  4823. /**
  4824. * Transforms the vec4 with a mat4.
  4825. *
  4826. * @param {vec4} out the receiving vector
  4827. * @param {ReadonlyVec4} a the vector to transform
  4828. * @param {ReadonlyMat4} m matrix to transform with
  4829. * @returns {vec4} out
  4830. */
  4831. function transformMat4$1(out, a, m) {
  4832. var x = a[0],
  4833. y = a[1],
  4834. z = a[2],
  4835. w = a[3];
  4836. out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w;
  4837. out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w;
  4838. out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w;
  4839. out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w;
  4840. return out;
  4841. }
  4842. /**
  4843. * Transforms the vec4 with a quat
  4844. *
  4845. * @param {vec4} out the receiving vector
  4846. * @param {ReadonlyVec4} a the vector to transform
  4847. * @param {ReadonlyQuat} q quaternion to transform with
  4848. * @returns {vec4} out
  4849. */
  4850. function transformQuat$1(out, a, q) {
  4851. var x = a[0],
  4852. y = a[1],
  4853. z = a[2];
  4854. var qx = q[0],
  4855. qy = q[1],
  4856. qz = q[2],
  4857. qw = q[3]; // calculate quat * vec
  4858. var ix = qw * x + qy * z - qz * y;
  4859. var iy = qw * y + qz * x - qx * z;
  4860. var iz = qw * z + qx * y - qy * x;
  4861. var iw = -qx * x - qy * y - qz * z; // calculate result * inverse quat
  4862. out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  4863. out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  4864. out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  4865. out[3] = a[3];
  4866. return out;
  4867. }
  4868. /**
  4869. * Set the components of a vec4 to zero
  4870. *
  4871. * @param {vec4} out the receiving vector
  4872. * @returns {vec4} out
  4873. */
  4874. function zero$1(out) {
  4875. out[0] = 0.0;
  4876. out[1] = 0.0;
  4877. out[2] = 0.0;
  4878. out[3] = 0.0;
  4879. return out;
  4880. }
  4881. /**
  4882. * Returns a string representation of a vector
  4883. *
  4884. * @param {ReadonlyVec4} a vector to represent as a string
  4885. * @returns {String} string representation of the vector
  4886. */
  4887. function str$5(a) {
  4888. return "vec4(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ")";
  4889. }
  4890. /**
  4891. * Returns whether or not the vectors have exactly the same elements in the same position (when compared with ===)
  4892. *
  4893. * @param {ReadonlyVec4} a The first vector.
  4894. * @param {ReadonlyVec4} b The second vector.
  4895. * @returns {Boolean} True if the vectors are equal, false otherwise.
  4896. */
  4897. function exactEquals$5(a, b) {
  4898. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];
  4899. }
  4900. /**
  4901. * Returns whether or not the vectors have approximately the same elements in the same position.
  4902. *
  4903. * @param {ReadonlyVec4} a The first vector.
  4904. * @param {ReadonlyVec4} b The second vector.
  4905. * @returns {Boolean} True if the vectors are equal, false otherwise.
  4906. */
  4907. function equals$6(a, b) {
  4908. var a0 = a[0],
  4909. a1 = a[1],
  4910. a2 = a[2],
  4911. a3 = a[3];
  4912. var b0 = b[0],
  4913. b1 = b[1],
  4914. b2 = b[2],
  4915. b3 = b[3];
  4916. return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3));
  4917. }
  4918. /**
  4919. * Alias for {@link vec4.subtract}
  4920. * @function
  4921. */
  4922. var sub$5 = subtract$5;
  4923. /**
  4924. * Alias for {@link vec4.multiply}
  4925. * @function
  4926. */
  4927. var mul$5 = multiply$5;
  4928. /**
  4929. * Alias for {@link vec4.divide}
  4930. * @function
  4931. */
  4932. var div$1 = divide$1;
  4933. /**
  4934. * Alias for {@link vec4.distance}
  4935. * @function
  4936. */
  4937. var dist$1 = distance$1;
  4938. /**
  4939. * Alias for {@link vec4.squaredDistance}
  4940. * @function
  4941. */
  4942. var sqrDist$1 = squaredDistance$1;
  4943. /**
  4944. * Alias for {@link vec4.length}
  4945. * @function
  4946. */
  4947. var len$1 = length$1;
  4948. /**
  4949. * Alias for {@link vec4.squaredLength}
  4950. * @function
  4951. */
  4952. var sqrLen$1 = squaredLength$1;
  4953. /**
  4954. * Perform some operation over an array of vec4s.
  4955. *
  4956. * @param {Array} a the array of vectors to iterate over
  4957. * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed
  4958. * @param {Number} offset Number of elements to skip at the beginning of the array
  4959. * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array
  4960. * @param {Function} fn Function to call for each vector in the array
  4961. * @param {Object} [arg] additional argument to pass to fn
  4962. * @returns {Array} a
  4963. * @function
  4964. */
  4965. var forEach$1 = function () {
  4966. var vec = create$5();
  4967. return function (a, stride, offset, count, fn, arg) {
  4968. var i, l;
  4969. if (!stride) {
  4970. stride = 4;
  4971. }
  4972. if (!offset) {
  4973. offset = 0;
  4974. }
  4975. if (count) {
  4976. l = Math.min(count * stride + offset, a.length);
  4977. } else {
  4978. l = a.length;
  4979. }
  4980. for (i = offset; i < l; i += stride) {
  4981. vec[0] = a[i];
  4982. vec[1] = a[i + 1];
  4983. vec[2] = a[i + 2];
  4984. vec[3] = a[i + 3];
  4985. fn(vec, vec, arg);
  4986. a[i] = vec[0];
  4987. a[i + 1] = vec[1];
  4988. a[i + 2] = vec[2];
  4989. a[i + 3] = vec[3];
  4990. }
  4991. return a;
  4992. };
  4993. }();
  4994. var vec4 = /*#__PURE__*/Object.freeze({
  4995. __proto__: null,
  4996. create: create$5,
  4997. clone: clone$5,
  4998. fromValues: fromValues$5,
  4999. copy: copy$5,
  5000. set: set$5,
  5001. add: add$5,
  5002. subtract: subtract$5,
  5003. multiply: multiply$5,
  5004. divide: divide$1,
  5005. ceil: ceil$1,
  5006. floor: floor$1,
  5007. min: min$1,
  5008. max: max$1,
  5009. round: round$1,
  5010. scale: scale$5,
  5011. scaleAndAdd: scaleAndAdd$1,
  5012. distance: distance$1,
  5013. squaredDistance: squaredDistance$1,
  5014. length: length$1,
  5015. squaredLength: squaredLength$1,
  5016. negate: negate$1,
  5017. inverse: inverse$1,
  5018. normalize: normalize$1,
  5019. dot: dot$1,
  5020. cross: cross$1,
  5021. lerp: lerp$1,
  5022. random: random$1,
  5023. transformMat4: transformMat4$1,
  5024. transformQuat: transformQuat$1,
  5025. zero: zero$1,
  5026. str: str$5,
  5027. exactEquals: exactEquals$5,
  5028. equals: equals$6,
  5029. sub: sub$5,
  5030. mul: mul$5,
  5031. div: div$1,
  5032. dist: dist$1,
  5033. sqrDist: sqrDist$1,
  5034. len: len$1,
  5035. sqrLen: sqrLen$1,
  5036. forEach: forEach$1
  5037. });
  5038. /**
  5039. * Quaternion
  5040. * @module quat
  5041. */
  5042. /**
  5043. * Creates a new identity quat
  5044. *
  5045. * @returns {quat} a new quaternion
  5046. */
  5047. function create$6() {
  5048. var out = new ARRAY_TYPE(4);
  5049. if (ARRAY_TYPE != Float32Array) {
  5050. out[0] = 0;
  5051. out[1] = 0;
  5052. out[2] = 0;
  5053. }
  5054. out[3] = 1;
  5055. return out;
  5056. }
  5057. /**
  5058. * Set a quat to the identity quaternion
  5059. *
  5060. * @param {quat} out the receiving quaternion
  5061. * @returns {quat} out
  5062. */
  5063. function identity$4(out) {
  5064. out[0] = 0;
  5065. out[1] = 0;
  5066. out[2] = 0;
  5067. out[3] = 1;
  5068. return out;
  5069. }
  5070. /**
  5071. * Sets a quat from the given angle and rotation axis,
  5072. * then returns it.
  5073. *
  5074. * @param {quat} out the receiving quaternion
  5075. * @param {ReadonlyVec3} axis the axis around which to rotate
  5076. * @param {Number} rad the angle in radians
  5077. * @returns {quat} out
  5078. **/
  5079. function setAxisAngle(out, axis, rad) {
  5080. rad = rad * 0.5;
  5081. var s = Math.sin(rad);
  5082. out[0] = s * axis[0];
  5083. out[1] = s * axis[1];
  5084. out[2] = s * axis[2];
  5085. out[3] = Math.cos(rad);
  5086. return out;
  5087. }
  5088. /**
  5089. * Gets the rotation axis and angle for a given
  5090. * quaternion. If a quaternion is created with
  5091. * setAxisAngle, this method will return the same
  5092. * values as providied in the original parameter list
  5093. * OR functionally equivalent values.
  5094. * Example: The quaternion formed by axis [0, 0, 1] and
  5095. * angle -90 is the same as the quaternion formed by
  5096. * [0, 0, 1] and 270. This method favors the latter.
  5097. * @param {vec3} out_axis Vector receiving the axis of rotation
  5098. * @param {ReadonlyQuat} q Quaternion to be decomposed
  5099. * @return {Number} Angle, in radians, of the rotation
  5100. */
  5101. function getAxisAngle(out_axis, q) {
  5102. var rad = Math.acos(q[3]) * 2.0;
  5103. var s = Math.sin(rad / 2.0);
  5104. if (s > EPSILON) {
  5105. out_axis[0] = q[0] / s;
  5106. out_axis[1] = q[1] / s;
  5107. out_axis[2] = q[2] / s;
  5108. } else {
  5109. // If s is zero, return any axis (no rotation - axis does not matter)
  5110. out_axis[0] = 1;
  5111. out_axis[1] = 0;
  5112. out_axis[2] = 0;
  5113. }
  5114. return rad;
  5115. }
  5116. /**
  5117. * Gets the angular distance between two unit quaternions
  5118. *
  5119. * @param {ReadonlyQuat} a Origin unit quaternion
  5120. * @param {ReadonlyQuat} b Destination unit quaternion
  5121. * @return {Number} Angle, in radians, between the two quaternions
  5122. */
  5123. function getAngle(a, b) {
  5124. var dotproduct = dot$2(a, b);
  5125. return Math.acos(2 * dotproduct * dotproduct - 1);
  5126. }
  5127. /**
  5128. * Multiplies two quat's
  5129. *
  5130. * @param {quat} out the receiving quaternion
  5131. * @param {ReadonlyQuat} a the first operand
  5132. * @param {ReadonlyQuat} b the second operand
  5133. * @returns {quat} out
  5134. */
  5135. function multiply$6(out, a, b) {
  5136. var ax = a[0],
  5137. ay = a[1],
  5138. az = a[2],
  5139. aw = a[3];
  5140. var bx = b[0],
  5141. by = b[1],
  5142. bz = b[2],
  5143. bw = b[3];
  5144. out[0] = ax * bw + aw * bx + ay * bz - az * by;
  5145. out[1] = ay * bw + aw * by + az * bx - ax * bz;
  5146. out[2] = az * bw + aw * bz + ax * by - ay * bx;
  5147. out[3] = aw * bw - ax * bx - ay * by - az * bz;
  5148. return out;
  5149. }
  5150. /**
  5151. * Rotates a quaternion by the given angle about the X axis
  5152. *
  5153. * @param {quat} out quat receiving operation result
  5154. * @param {ReadonlyQuat} a quat to rotate
  5155. * @param {number} rad angle (in radians) to rotate
  5156. * @returns {quat} out
  5157. */
  5158. function rotateX$2(out, a, rad) {
  5159. rad *= 0.5;
  5160. var ax = a[0],
  5161. ay = a[1],
  5162. az = a[2],
  5163. aw = a[3];
  5164. var bx = Math.sin(rad),
  5165. bw = Math.cos(rad);
  5166. out[0] = ax * bw + aw * bx;
  5167. out[1] = ay * bw + az * bx;
  5168. out[2] = az * bw - ay * bx;
  5169. out[3] = aw * bw - ax * bx;
  5170. return out;
  5171. }
  5172. /**
  5173. * Rotates a quaternion by the given angle about the Y axis
  5174. *
  5175. * @param {quat} out quat receiving operation result
  5176. * @param {ReadonlyQuat} a quat to rotate
  5177. * @param {number} rad angle (in radians) to rotate
  5178. * @returns {quat} out
  5179. */
  5180. function rotateY$2(out, a, rad) {
  5181. rad *= 0.5;
  5182. var ax = a[0],
  5183. ay = a[1],
  5184. az = a[2],
  5185. aw = a[3];
  5186. var by = Math.sin(rad),
  5187. bw = Math.cos(rad);
  5188. out[0] = ax * bw - az * by;
  5189. out[1] = ay * bw + aw * by;
  5190. out[2] = az * bw + ax * by;
  5191. out[3] = aw * bw - ay * by;
  5192. return out;
  5193. }
  5194. /**
  5195. * Rotates a quaternion by the given angle about the Z axis
  5196. *
  5197. * @param {quat} out quat receiving operation result
  5198. * @param {ReadonlyQuat} a quat to rotate
  5199. * @param {number} rad angle (in radians) to rotate
  5200. * @returns {quat} out
  5201. */
  5202. function rotateZ$2(out, a, rad) {
  5203. rad *= 0.5;
  5204. var ax = a[0],
  5205. ay = a[1],
  5206. az = a[2],
  5207. aw = a[3];
  5208. var bz = Math.sin(rad),
  5209. bw = Math.cos(rad);
  5210. out[0] = ax * bw + ay * bz;
  5211. out[1] = ay * bw - ax * bz;
  5212. out[2] = az * bw + aw * bz;
  5213. out[3] = aw * bw - az * bz;
  5214. return out;
  5215. }
  5216. /**
  5217. * Calculates the W component of a quat from the X, Y, and Z components.
  5218. * Assumes that quaternion is 1 unit in length.
  5219. * Any existing W component will be ignored.
  5220. *
  5221. * @param {quat} out the receiving quaternion
  5222. * @param {ReadonlyQuat} a quat to calculate W component of
  5223. * @returns {quat} out
  5224. */
  5225. function calculateW(out, a) {
  5226. var x = a[0],
  5227. y = a[1],
  5228. z = a[2];
  5229. out[0] = x;
  5230. out[1] = y;
  5231. out[2] = z;
  5232. out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z));
  5233. return out;
  5234. }
  5235. /**
  5236. * Calculate the exponential of a unit quaternion.
  5237. *
  5238. * @param {quat} out the receiving quaternion
  5239. * @param {ReadonlyQuat} a quat to calculate the exponential of
  5240. * @returns {quat} out
  5241. */
  5242. function exp(out, a) {
  5243. var x = a[0],
  5244. y = a[1],
  5245. z = a[2],
  5246. w = a[3];
  5247. var r = Math.sqrt(x * x + y * y + z * z);
  5248. var et = Math.exp(w);
  5249. var s = r > 0 ? et * Math.sin(r) / r : 0;
  5250. out[0] = x * s;
  5251. out[1] = y * s;
  5252. out[2] = z * s;
  5253. out[3] = et * Math.cos(r);
  5254. return out;
  5255. }
  5256. /**
  5257. * Calculate the natural logarithm of a unit quaternion.
  5258. *
  5259. * @param {quat} out the receiving quaternion
  5260. * @param {ReadonlyQuat} a quat to calculate the exponential of
  5261. * @returns {quat} out
  5262. */
  5263. function ln(out, a) {
  5264. var x = a[0],
  5265. y = a[1],
  5266. z = a[2],
  5267. w = a[3];
  5268. var r = Math.sqrt(x * x + y * y + z * z);
  5269. var t = r > 0 ? Math.atan2(r, w) / r : 0;
  5270. out[0] = x * t;
  5271. out[1] = y * t;
  5272. out[2] = z * t;
  5273. out[3] = 0.5 * Math.log(x * x + y * y + z * z + w * w);
  5274. return out;
  5275. }
  5276. /**
  5277. * Calculate the scalar power of a unit quaternion.
  5278. *
  5279. * @param {quat} out the receiving quaternion
  5280. * @param {ReadonlyQuat} a quat to calculate the exponential of
  5281. * @param {Number} b amount to scale the quaternion by
  5282. * @returns {quat} out
  5283. */
  5284. function pow(out, a, b) {
  5285. ln(out, a);
  5286. scale$6(out, out, b);
  5287. exp(out, out);
  5288. return out;
  5289. }
  5290. /**
  5291. * Performs a spherical linear interpolation between two quat
  5292. *
  5293. * @param {quat} out the receiving quaternion
  5294. * @param {ReadonlyQuat} a the first operand
  5295. * @param {ReadonlyQuat} b the second operand
  5296. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  5297. * @returns {quat} out
  5298. */
  5299. function slerp(out, a, b, t) {
  5300. // benchmarks:
  5301. // http://jsperf.com/quaternion-slerp-implementations
  5302. var ax = a[0],
  5303. ay = a[1],
  5304. az = a[2],
  5305. aw = a[3];
  5306. var bx = b[0],
  5307. by = b[1],
  5308. bz = b[2],
  5309. bw = b[3];
  5310. var omega, cosom, sinom, scale0, scale1; // calc cosine
  5311. cosom = ax * bx + ay * by + az * bz + aw * bw; // adjust signs (if necessary)
  5312. if (cosom < 0.0) {
  5313. cosom = -cosom;
  5314. bx = -bx;
  5315. by = -by;
  5316. bz = -bz;
  5317. bw = -bw;
  5318. } // calculate coefficients
  5319. if (1.0 - cosom > EPSILON) {
  5320. // standard case (slerp)
  5321. omega = Math.acos(cosom);
  5322. sinom = Math.sin(omega);
  5323. scale0 = Math.sin((1.0 - t) * omega) / sinom;
  5324. scale1 = Math.sin(t * omega) / sinom;
  5325. } else {
  5326. // "from" and "to" quaternions are very close
  5327. // ... so we can do a linear interpolation
  5328. scale0 = 1.0 - t;
  5329. scale1 = t;
  5330. } // calculate final values
  5331. out[0] = scale0 * ax + scale1 * bx;
  5332. out[1] = scale0 * ay + scale1 * by;
  5333. out[2] = scale0 * az + scale1 * bz;
  5334. out[3] = scale0 * aw + scale1 * bw;
  5335. return out;
  5336. }
  5337. /**
  5338. * Generates a random unit quaternion
  5339. *
  5340. * @param {quat} out the receiving quaternion
  5341. * @returns {quat} out
  5342. */
  5343. function random$2(out) {
  5344. // Implementation of http://planning.cs.uiuc.edu/node198.html
  5345. // TODO: Calling random 3 times is probably not the fastest solution
  5346. var u1 = RANDOM();
  5347. var u2 = RANDOM();
  5348. var u3 = RANDOM();
  5349. var sqrt1MinusU1 = Math.sqrt(1 - u1);
  5350. var sqrtU1 = Math.sqrt(u1);
  5351. out[0] = sqrt1MinusU1 * Math.sin(2.0 * Math.PI * u2);
  5352. out[1] = sqrt1MinusU1 * Math.cos(2.0 * Math.PI * u2);
  5353. out[2] = sqrtU1 * Math.sin(2.0 * Math.PI * u3);
  5354. out[3] = sqrtU1 * Math.cos(2.0 * Math.PI * u3);
  5355. return out;
  5356. }
  5357. /**
  5358. * Calculates the inverse of a quat
  5359. *
  5360. * @param {quat} out the receiving quaternion
  5361. * @param {ReadonlyQuat} a quat to calculate inverse of
  5362. * @returns {quat} out
  5363. */
  5364. function invert$4(out, a) {
  5365. var a0 = a[0],
  5366. a1 = a[1],
  5367. a2 = a[2],
  5368. a3 = a[3];
  5369. var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3;
  5370. var invDot = dot ? 1.0 / dot : 0; // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0
  5371. out[0] = -a0 * invDot;
  5372. out[1] = -a1 * invDot;
  5373. out[2] = -a2 * invDot;
  5374. out[3] = a3 * invDot;
  5375. return out;
  5376. }
  5377. /**
  5378. * Calculates the conjugate of a quat
  5379. * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result.
  5380. *
  5381. * @param {quat} out the receiving quaternion
  5382. * @param {ReadonlyQuat} a quat to calculate conjugate of
  5383. * @returns {quat} out
  5384. */
  5385. function conjugate(out, a) {
  5386. out[0] = -a[0];
  5387. out[1] = -a[1];
  5388. out[2] = -a[2];
  5389. out[3] = a[3];
  5390. return out;
  5391. }
  5392. /**
  5393. * Creates a quaternion from the given 3x3 rotation matrix.
  5394. *
  5395. * NOTE: The resultant quaternion is not normalized, so you should be sure
  5396. * to renormalize the quaternion yourself where necessary.
  5397. *
  5398. * @param {quat} out the receiving quaternion
  5399. * @param {ReadonlyMat3} m rotation matrix
  5400. * @returns {quat} out
  5401. * @function
  5402. */
  5403. function fromMat3(out, m) {
  5404. // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes
  5405. // article "Quaternion Calculus and Fast Animation".
  5406. var fTrace = m[0] + m[4] + m[8];
  5407. var fRoot;
  5408. if (fTrace > 0.0) {
  5409. // |w| > 1/2, may as well choose w > 1/2
  5410. fRoot = Math.sqrt(fTrace + 1.0); // 2w
  5411. out[3] = 0.5 * fRoot;
  5412. fRoot = 0.5 / fRoot; // 1/(4w)
  5413. out[0] = (m[5] - m[7]) * fRoot;
  5414. out[1] = (m[6] - m[2]) * fRoot;
  5415. out[2] = (m[1] - m[3]) * fRoot;
  5416. } else {
  5417. // |w| <= 1/2
  5418. var i = 0;
  5419. if (m[4] > m[0]) i = 1;
  5420. if (m[8] > m[i * 3 + i]) i = 2;
  5421. var j = (i + 1) % 3;
  5422. var k = (i + 2) % 3;
  5423. fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0);
  5424. out[i] = 0.5 * fRoot;
  5425. fRoot = 0.5 / fRoot;
  5426. out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot;
  5427. out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot;
  5428. out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot;
  5429. }
  5430. return out;
  5431. }
  5432. /**
  5433. * Creates a quaternion from the given euler angle x, y, z.
  5434. *
  5435. * @param {quat} out the receiving quaternion
  5436. * @param {x} Angle to rotate around X axis in degrees.
  5437. * @param {y} Angle to rotate around Y axis in degrees.
  5438. * @param {z} Angle to rotate around Z axis in degrees.
  5439. * @returns {quat} out
  5440. * @function
  5441. */
  5442. function fromEuler(out, x, y, z) {
  5443. var halfToRad = 0.5 * Math.PI / 180.0;
  5444. x *= halfToRad;
  5445. y *= halfToRad;
  5446. z *= halfToRad;
  5447. var sx = Math.sin(x);
  5448. var cx = Math.cos(x);
  5449. var sy = Math.sin(y);
  5450. var cy = Math.cos(y);
  5451. var sz = Math.sin(z);
  5452. var cz = Math.cos(z);
  5453. out[0] = sx * cy * cz - cx * sy * sz;
  5454. out[1] = cx * sy * cz + sx * cy * sz;
  5455. out[2] = cx * cy * sz - sx * sy * cz;
  5456. out[3] = cx * cy * cz + sx * sy * sz;
  5457. return out;
  5458. }
  5459. /**
  5460. * Returns a string representation of a quatenion
  5461. *
  5462. * @param {ReadonlyQuat} a vector to represent as a string
  5463. * @returns {String} string representation of the vector
  5464. */
  5465. function str$6(a) {
  5466. return "quat(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ")";
  5467. }
  5468. /**
  5469. * Creates a new quat initialized with values from an existing quaternion
  5470. *
  5471. * @param {ReadonlyQuat} a quaternion to clone
  5472. * @returns {quat} a new quaternion
  5473. * @function
  5474. */
  5475. var clone$6 = clone$5;
  5476. /**
  5477. * Creates a new quat initialized with the given values
  5478. *
  5479. * @param {Number} x X component
  5480. * @param {Number} y Y component
  5481. * @param {Number} z Z component
  5482. * @param {Number} w W component
  5483. * @returns {quat} a new quaternion
  5484. * @function
  5485. */
  5486. var fromValues$6 = fromValues$5;
  5487. /**
  5488. * Copy the values from one quat to another
  5489. *
  5490. * @param {quat} out the receiving quaternion
  5491. * @param {ReadonlyQuat} a the source quaternion
  5492. * @returns {quat} out
  5493. * @function
  5494. */
  5495. var copy$6 = copy$5;
  5496. /**
  5497. * Set the components of a quat to the given values
  5498. *
  5499. * @param {quat} out the receiving quaternion
  5500. * @param {Number} x X component
  5501. * @param {Number} y Y component
  5502. * @param {Number} z Z component
  5503. * @param {Number} w W component
  5504. * @returns {quat} out
  5505. * @function
  5506. */
  5507. var set$6 = set$5;
  5508. /**
  5509. * Adds two quat's
  5510. *
  5511. * @param {quat} out the receiving quaternion
  5512. * @param {ReadonlyQuat} a the first operand
  5513. * @param {ReadonlyQuat} b the second operand
  5514. * @returns {quat} out
  5515. * @function
  5516. */
  5517. var add$6 = add$5;
  5518. /**
  5519. * Alias for {@link quat.multiply}
  5520. * @function
  5521. */
  5522. var mul$6 = multiply$6;
  5523. /**
  5524. * Scales a quat by a scalar number
  5525. *
  5526. * @param {quat} out the receiving vector
  5527. * @param {ReadonlyQuat} a the vector to scale
  5528. * @param {Number} b amount to scale the vector by
  5529. * @returns {quat} out
  5530. * @function
  5531. */
  5532. var scale$6 = scale$5;
  5533. /**
  5534. * Calculates the dot product of two quat's
  5535. *
  5536. * @param {ReadonlyQuat} a the first operand
  5537. * @param {ReadonlyQuat} b the second operand
  5538. * @returns {Number} dot product of a and b
  5539. * @function
  5540. */
  5541. var dot$2 = dot$1;
  5542. /**
  5543. * Performs a linear interpolation between two quat's
  5544. *
  5545. * @param {quat} out the receiving quaternion
  5546. * @param {ReadonlyQuat} a the first operand
  5547. * @param {ReadonlyQuat} b the second operand
  5548. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  5549. * @returns {quat} out
  5550. * @function
  5551. */
  5552. var lerp$2 = lerp$1;
  5553. /**
  5554. * Calculates the length of a quat
  5555. *
  5556. * @param {ReadonlyQuat} a vector to calculate length of
  5557. * @returns {Number} length of a
  5558. */
  5559. var length$2 = length$1;
  5560. /**
  5561. * Alias for {@link quat.length}
  5562. * @function
  5563. */
  5564. var len$2 = length$2;
  5565. /**
  5566. * Calculates the squared length of a quat
  5567. *
  5568. * @param {ReadonlyQuat} a vector to calculate squared length of
  5569. * @returns {Number} squared length of a
  5570. * @function
  5571. */
  5572. var squaredLength$2 = squaredLength$1;
  5573. /**
  5574. * Alias for {@link quat.squaredLength}
  5575. * @function
  5576. */
  5577. var sqrLen$2 = squaredLength$2;
  5578. /**
  5579. * Normalize a quat
  5580. *
  5581. * @param {quat} out the receiving quaternion
  5582. * @param {ReadonlyQuat} a quaternion to normalize
  5583. * @returns {quat} out
  5584. * @function
  5585. */
  5586. var normalize$2 = normalize$1;
  5587. /**
  5588. * Returns whether or not the quaternions have exactly the same elements in the same position (when compared with ===)
  5589. *
  5590. * @param {ReadonlyQuat} a The first quaternion.
  5591. * @param {ReadonlyQuat} b The second quaternion.
  5592. * @returns {Boolean} True if the vectors are equal, false otherwise.
  5593. */
  5594. var exactEquals$6 = exactEquals$5;
  5595. /**
  5596. * Returns whether or not the quaternions have approximately the same elements in the same position.
  5597. *
  5598. * @param {ReadonlyQuat} a The first vector.
  5599. * @param {ReadonlyQuat} b The second vector.
  5600. * @returns {Boolean} True if the vectors are equal, false otherwise.
  5601. */
  5602. var equals$7 = equals$6;
  5603. /**
  5604. * Sets a quaternion to represent the shortest rotation from one
  5605. * vector to another.
  5606. *
  5607. * Both vectors are assumed to be unit length.
  5608. *
  5609. * @param {quat} out the receiving quaternion.
  5610. * @param {ReadonlyVec3} a the initial vector
  5611. * @param {ReadonlyVec3} b the destination vector
  5612. * @returns {quat} out
  5613. */
  5614. var rotationTo = function () {
  5615. var tmpvec3 = create$4();
  5616. var xUnitVec3 = fromValues$4(1, 0, 0);
  5617. var yUnitVec3 = fromValues$4(0, 1, 0);
  5618. return function (out, a, b) {
  5619. var dot$1 = dot(a, b);
  5620. if (dot$1 < -0.999999) {
  5621. cross(tmpvec3, xUnitVec3, a);
  5622. if (len(tmpvec3) < 0.000001) cross(tmpvec3, yUnitVec3, a);
  5623. normalize(tmpvec3, tmpvec3);
  5624. setAxisAngle(out, tmpvec3, Math.PI);
  5625. return out;
  5626. } else if (dot$1 > 0.999999) {
  5627. out[0] = 0;
  5628. out[1] = 0;
  5629. out[2] = 0;
  5630. out[3] = 1;
  5631. return out;
  5632. } else {
  5633. cross(tmpvec3, a, b);
  5634. out[0] = tmpvec3[0];
  5635. out[1] = tmpvec3[1];
  5636. out[2] = tmpvec3[2];
  5637. out[3] = 1 + dot$1;
  5638. return normalize$2(out, out);
  5639. }
  5640. };
  5641. }();
  5642. /**
  5643. * Performs a spherical linear interpolation with two control points
  5644. *
  5645. * @param {quat} out the receiving quaternion
  5646. * @param {ReadonlyQuat} a the first operand
  5647. * @param {ReadonlyQuat} b the second operand
  5648. * @param {ReadonlyQuat} c the third operand
  5649. * @param {ReadonlyQuat} d the fourth operand
  5650. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  5651. * @returns {quat} out
  5652. */
  5653. var sqlerp = function () {
  5654. var temp1 = create$6();
  5655. var temp2 = create$6();
  5656. return function (out, a, b, c, d, t) {
  5657. slerp(temp1, a, d, t);
  5658. slerp(temp2, b, c, t);
  5659. slerp(out, temp1, temp2, 2 * t * (1 - t));
  5660. return out;
  5661. };
  5662. }();
  5663. /**
  5664. * Sets the specified quaternion with values corresponding to the given
  5665. * axes. Each axis is a vec3 and is expected to be unit length and
  5666. * perpendicular to all other specified axes.
  5667. *
  5668. * @param {ReadonlyVec3} view the vector representing the viewing direction
  5669. * @param {ReadonlyVec3} right the vector representing the local "right" direction
  5670. * @param {ReadonlyVec3} up the vector representing the local "up" direction
  5671. * @returns {quat} out
  5672. */
  5673. var setAxes = function () {
  5674. var matr = create$2();
  5675. return function (out, view, right, up) {
  5676. matr[0] = right[0];
  5677. matr[3] = right[1];
  5678. matr[6] = right[2];
  5679. matr[1] = up[0];
  5680. matr[4] = up[1];
  5681. matr[7] = up[2];
  5682. matr[2] = -view[0];
  5683. matr[5] = -view[1];
  5684. matr[8] = -view[2];
  5685. return normalize$2(out, fromMat3(out, matr));
  5686. };
  5687. }();
  5688. var quat = /*#__PURE__*/Object.freeze({
  5689. __proto__: null,
  5690. create: create$6,
  5691. identity: identity$4,
  5692. setAxisAngle: setAxisAngle,
  5693. getAxisAngle: getAxisAngle,
  5694. getAngle: getAngle,
  5695. multiply: multiply$6,
  5696. rotateX: rotateX$2,
  5697. rotateY: rotateY$2,
  5698. rotateZ: rotateZ$2,
  5699. calculateW: calculateW,
  5700. exp: exp,
  5701. ln: ln,
  5702. pow: pow,
  5703. slerp: slerp,
  5704. random: random$2,
  5705. invert: invert$4,
  5706. conjugate: conjugate,
  5707. fromMat3: fromMat3,
  5708. fromEuler: fromEuler,
  5709. str: str$6,
  5710. clone: clone$6,
  5711. fromValues: fromValues$6,
  5712. copy: copy$6,
  5713. set: set$6,
  5714. add: add$6,
  5715. mul: mul$6,
  5716. scale: scale$6,
  5717. dot: dot$2,
  5718. lerp: lerp$2,
  5719. length: length$2,
  5720. len: len$2,
  5721. squaredLength: squaredLength$2,
  5722. sqrLen: sqrLen$2,
  5723. normalize: normalize$2,
  5724. exactEquals: exactEquals$6,
  5725. equals: equals$7,
  5726. rotationTo: rotationTo,
  5727. sqlerp: sqlerp,
  5728. setAxes: setAxes
  5729. });
  5730. /**
  5731. * Dual Quaternion<br>
  5732. * Format: [real, dual]<br>
  5733. * Quaternion format: XYZW<br>
  5734. * Make sure to have normalized dual quaternions, otherwise the functions may not work as intended.<br>
  5735. * @module quat2
  5736. */
  5737. /**
  5738. * Creates a new identity dual quat
  5739. *
  5740. * @returns {quat2} a new dual quaternion [real -> rotation, dual -> translation]
  5741. */
  5742. function create$7() {
  5743. var dq = new ARRAY_TYPE(8);
  5744. if (ARRAY_TYPE != Float32Array) {
  5745. dq[0] = 0;
  5746. dq[1] = 0;
  5747. dq[2] = 0;
  5748. dq[4] = 0;
  5749. dq[5] = 0;
  5750. dq[6] = 0;
  5751. dq[7] = 0;
  5752. }
  5753. dq[3] = 1;
  5754. return dq;
  5755. }
  5756. /**
  5757. * Creates a new quat initialized with values from an existing quaternion
  5758. *
  5759. * @param {ReadonlyQuat2} a dual quaternion to clone
  5760. * @returns {quat2} new dual quaternion
  5761. * @function
  5762. */
  5763. function clone$7(a) {
  5764. var dq = new ARRAY_TYPE(8);
  5765. dq[0] = a[0];
  5766. dq[1] = a[1];
  5767. dq[2] = a[2];
  5768. dq[3] = a[3];
  5769. dq[4] = a[4];
  5770. dq[5] = a[5];
  5771. dq[6] = a[6];
  5772. dq[7] = a[7];
  5773. return dq;
  5774. }
  5775. /**
  5776. * Creates a new dual quat initialized with the given values
  5777. *
  5778. * @param {Number} x1 X component
  5779. * @param {Number} y1 Y component
  5780. * @param {Number} z1 Z component
  5781. * @param {Number} w1 W component
  5782. * @param {Number} x2 X component
  5783. * @param {Number} y2 Y component
  5784. * @param {Number} z2 Z component
  5785. * @param {Number} w2 W component
  5786. * @returns {quat2} new dual quaternion
  5787. * @function
  5788. */
  5789. function fromValues$7(x1, y1, z1, w1, x2, y2, z2, w2) {
  5790. var dq = new ARRAY_TYPE(8);
  5791. dq[0] = x1;
  5792. dq[1] = y1;
  5793. dq[2] = z1;
  5794. dq[3] = w1;
  5795. dq[4] = x2;
  5796. dq[5] = y2;
  5797. dq[6] = z2;
  5798. dq[7] = w2;
  5799. return dq;
  5800. }
  5801. /**
  5802. * Creates a new dual quat from the given values (quat and translation)
  5803. *
  5804. * @param {Number} x1 X component
  5805. * @param {Number} y1 Y component
  5806. * @param {Number} z1 Z component
  5807. * @param {Number} w1 W component
  5808. * @param {Number} x2 X component (translation)
  5809. * @param {Number} y2 Y component (translation)
  5810. * @param {Number} z2 Z component (translation)
  5811. * @returns {quat2} new dual quaternion
  5812. * @function
  5813. */
  5814. function fromRotationTranslationValues(x1, y1, z1, w1, x2, y2, z2) {
  5815. var dq = new ARRAY_TYPE(8);
  5816. dq[0] = x1;
  5817. dq[1] = y1;
  5818. dq[2] = z1;
  5819. dq[3] = w1;
  5820. var ax = x2 * 0.5,
  5821. ay = y2 * 0.5,
  5822. az = z2 * 0.5;
  5823. dq[4] = ax * w1 + ay * z1 - az * y1;
  5824. dq[5] = ay * w1 + az * x1 - ax * z1;
  5825. dq[6] = az * w1 + ax * y1 - ay * x1;
  5826. dq[7] = -ax * x1 - ay * y1 - az * z1;
  5827. return dq;
  5828. }
  5829. /**
  5830. * Creates a dual quat from a quaternion and a translation
  5831. *
  5832. * @param {ReadonlyQuat2} dual quaternion receiving operation result
  5833. * @param {ReadonlyQuat} q a normalized quaternion
  5834. * @param {ReadonlyVec3} t tranlation vector
  5835. * @returns {quat2} dual quaternion receiving operation result
  5836. * @function
  5837. */
  5838. function fromRotationTranslation$1(out, q, t) {
  5839. var ax = t[0] * 0.5,
  5840. ay = t[1] * 0.5,
  5841. az = t[2] * 0.5,
  5842. bx = q[0],
  5843. by = q[1],
  5844. bz = q[2],
  5845. bw = q[3];
  5846. out[0] = bx;
  5847. out[1] = by;
  5848. out[2] = bz;
  5849. out[3] = bw;
  5850. out[4] = ax * bw + ay * bz - az * by;
  5851. out[5] = ay * bw + az * bx - ax * bz;
  5852. out[6] = az * bw + ax * by - ay * bx;
  5853. out[7] = -ax * bx - ay * by - az * bz;
  5854. return out;
  5855. }
  5856. /**
  5857. * Creates a dual quat from a translation
  5858. *
  5859. * @param {ReadonlyQuat2} dual quaternion receiving operation result
  5860. * @param {ReadonlyVec3} t translation vector
  5861. * @returns {quat2} dual quaternion receiving operation result
  5862. * @function
  5863. */
  5864. function fromTranslation$3(out, t) {
  5865. out[0] = 0;
  5866. out[1] = 0;
  5867. out[2] = 0;
  5868. out[3] = 1;
  5869. out[4] = t[0] * 0.5;
  5870. out[5] = t[1] * 0.5;
  5871. out[6] = t[2] * 0.5;
  5872. out[7] = 0;
  5873. return out;
  5874. }
  5875. /**
  5876. * Creates a dual quat from a quaternion
  5877. *
  5878. * @param {ReadonlyQuat2} dual quaternion receiving operation result
  5879. * @param {ReadonlyQuat} q the quaternion
  5880. * @returns {quat2} dual quaternion receiving operation result
  5881. * @function
  5882. */
  5883. function fromRotation$4(out, q) {
  5884. out[0] = q[0];
  5885. out[1] = q[1];
  5886. out[2] = q[2];
  5887. out[3] = q[3];
  5888. out[4] = 0;
  5889. out[5] = 0;
  5890. out[6] = 0;
  5891. out[7] = 0;
  5892. return out;
  5893. }
  5894. /**
  5895. * Creates a new dual quat from a matrix (4x4)
  5896. *
  5897. * @param {quat2} out the dual quaternion
  5898. * @param {ReadonlyMat4} a the matrix
  5899. * @returns {quat2} dual quat receiving operation result
  5900. * @function
  5901. */
  5902. function fromMat4$1(out, a) {
  5903. //TODO Optimize this
  5904. var outer = create$6();
  5905. getRotation(outer, a);
  5906. var t = new ARRAY_TYPE(3);
  5907. getTranslation(t, a);
  5908. fromRotationTranslation$1(out, outer, t);
  5909. return out;
  5910. }
  5911. /**
  5912. * Copy the values from one dual quat to another
  5913. *
  5914. * @param {quat2} out the receiving dual quaternion
  5915. * @param {ReadonlyQuat2} a the source dual quaternion
  5916. * @returns {quat2} out
  5917. * @function
  5918. */
  5919. function copy$7(out, a) {
  5920. out[0] = a[0];
  5921. out[1] = a[1];
  5922. out[2] = a[2];
  5923. out[3] = a[3];
  5924. out[4] = a[4];
  5925. out[5] = a[5];
  5926. out[6] = a[6];
  5927. out[7] = a[7];
  5928. return out;
  5929. }
  5930. /**
  5931. * Set a dual quat to the identity dual quaternion
  5932. *
  5933. * @param {quat2} out the receiving quaternion
  5934. * @returns {quat2} out
  5935. */
  5936. function identity$5(out) {
  5937. out[0] = 0;
  5938. out[1] = 0;
  5939. out[2] = 0;
  5940. out[3] = 1;
  5941. out[4] = 0;
  5942. out[5] = 0;
  5943. out[6] = 0;
  5944. out[7] = 0;
  5945. return out;
  5946. }
  5947. /**
  5948. * Set the components of a dual quat to the given values
  5949. *
  5950. * @param {quat2} out the receiving quaternion
  5951. * @param {Number} x1 X component
  5952. * @param {Number} y1 Y component
  5953. * @param {Number} z1 Z component
  5954. * @param {Number} w1 W component
  5955. * @param {Number} x2 X component
  5956. * @param {Number} y2 Y component
  5957. * @param {Number} z2 Z component
  5958. * @param {Number} w2 W component
  5959. * @returns {quat2} out
  5960. * @function
  5961. */
  5962. function set$7(out, x1, y1, z1, w1, x2, y2, z2, w2) {
  5963. out[0] = x1;
  5964. out[1] = y1;
  5965. out[2] = z1;
  5966. out[3] = w1;
  5967. out[4] = x2;
  5968. out[5] = y2;
  5969. out[6] = z2;
  5970. out[7] = w2;
  5971. return out;
  5972. }
  5973. /**
  5974. * Gets the real part of a dual quat
  5975. * @param {quat} out real part
  5976. * @param {ReadonlyQuat2} a Dual Quaternion
  5977. * @return {quat} real part
  5978. */
  5979. var getReal = copy$6;
  5980. /**
  5981. * Gets the dual part of a dual quat
  5982. * @param {quat} out dual part
  5983. * @param {ReadonlyQuat2} a Dual Quaternion
  5984. * @return {quat} dual part
  5985. */
  5986. function getDual(out, a) {
  5987. out[0] = a[4];
  5988. out[1] = a[5];
  5989. out[2] = a[6];
  5990. out[3] = a[7];
  5991. return out;
  5992. }
  5993. /**
  5994. * Set the real component of a dual quat to the given quaternion
  5995. *
  5996. * @param {quat2} out the receiving quaternion
  5997. * @param {ReadonlyQuat} q a quaternion representing the real part
  5998. * @returns {quat2} out
  5999. * @function
  6000. */
  6001. var setReal = copy$6;
  6002. /**
  6003. * Set the dual component of a dual quat to the given quaternion
  6004. *
  6005. * @param {quat2} out the receiving quaternion
  6006. * @param {ReadonlyQuat} q a quaternion representing the dual part
  6007. * @returns {quat2} out
  6008. * @function
  6009. */
  6010. function setDual(out, q) {
  6011. out[4] = q[0];
  6012. out[5] = q[1];
  6013. out[6] = q[2];
  6014. out[7] = q[3];
  6015. return out;
  6016. }
  6017. /**
  6018. * Gets the translation of a normalized dual quat
  6019. * @param {vec3} out translation
  6020. * @param {ReadonlyQuat2} a Dual Quaternion to be decomposed
  6021. * @return {vec3} translation
  6022. */
  6023. function getTranslation$1(out, a) {
  6024. var ax = a[4],
  6025. ay = a[5],
  6026. az = a[6],
  6027. aw = a[7],
  6028. bx = -a[0],
  6029. by = -a[1],
  6030. bz = -a[2],
  6031. bw = a[3];
  6032. out[0] = (ax * bw + aw * bx + ay * bz - az * by) * 2;
  6033. out[1] = (ay * bw + aw * by + az * bx - ax * bz) * 2;
  6034. out[2] = (az * bw + aw * bz + ax * by - ay * bx) * 2;
  6035. return out;
  6036. }
  6037. /**
  6038. * Translates a dual quat by the given vector
  6039. *
  6040. * @param {quat2} out the receiving dual quaternion
  6041. * @param {ReadonlyQuat2} a the dual quaternion to translate
  6042. * @param {ReadonlyVec3} v vector to translate by
  6043. * @returns {quat2} out
  6044. */
  6045. function translate$3(out, a, v) {
  6046. var ax1 = a[0],
  6047. ay1 = a[1],
  6048. az1 = a[2],
  6049. aw1 = a[3],
  6050. bx1 = v[0] * 0.5,
  6051. by1 = v[1] * 0.5,
  6052. bz1 = v[2] * 0.5,
  6053. ax2 = a[4],
  6054. ay2 = a[5],
  6055. az2 = a[6],
  6056. aw2 = a[7];
  6057. out[0] = ax1;
  6058. out[1] = ay1;
  6059. out[2] = az1;
  6060. out[3] = aw1;
  6061. out[4] = aw1 * bx1 + ay1 * bz1 - az1 * by1 + ax2;
  6062. out[5] = aw1 * by1 + az1 * bx1 - ax1 * bz1 + ay2;
  6063. out[6] = aw1 * bz1 + ax1 * by1 - ay1 * bx1 + az2;
  6064. out[7] = -ax1 * bx1 - ay1 * by1 - az1 * bz1 + aw2;
  6065. return out;
  6066. }
  6067. /**
  6068. * Rotates a dual quat around the X axis
  6069. *
  6070. * @param {quat2} out the receiving dual quaternion
  6071. * @param {ReadonlyQuat2} a the dual quaternion to rotate
  6072. * @param {number} rad how far should the rotation be
  6073. * @returns {quat2} out
  6074. */
  6075. function rotateX$3(out, a, rad) {
  6076. var bx = -a[0],
  6077. by = -a[1],
  6078. bz = -a[2],
  6079. bw = a[3],
  6080. ax = a[4],
  6081. ay = a[5],
  6082. az = a[6],
  6083. aw = a[7],
  6084. ax1 = ax * bw + aw * bx + ay * bz - az * by,
  6085. ay1 = ay * bw + aw * by + az * bx - ax * bz,
  6086. az1 = az * bw + aw * bz + ax * by - ay * bx,
  6087. aw1 = aw * bw - ax * bx - ay * by - az * bz;
  6088. rotateX$2(out, a, rad);
  6089. bx = out[0];
  6090. by = out[1];
  6091. bz = out[2];
  6092. bw = out[3];
  6093. out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  6094. out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  6095. out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  6096. out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  6097. return out;
  6098. }
  6099. /**
  6100. * Rotates a dual quat around the Y axis
  6101. *
  6102. * @param {quat2} out the receiving dual quaternion
  6103. * @param {ReadonlyQuat2} a the dual quaternion to rotate
  6104. * @param {number} rad how far should the rotation be
  6105. * @returns {quat2} out
  6106. */
  6107. function rotateY$3(out, a, rad) {
  6108. var bx = -a[0],
  6109. by = -a[1],
  6110. bz = -a[2],
  6111. bw = a[3],
  6112. ax = a[4],
  6113. ay = a[5],
  6114. az = a[6],
  6115. aw = a[7],
  6116. ax1 = ax * bw + aw * bx + ay * bz - az * by,
  6117. ay1 = ay * bw + aw * by + az * bx - ax * bz,
  6118. az1 = az * bw + aw * bz + ax * by - ay * bx,
  6119. aw1 = aw * bw - ax * bx - ay * by - az * bz;
  6120. rotateY$2(out, a, rad);
  6121. bx = out[0];
  6122. by = out[1];
  6123. bz = out[2];
  6124. bw = out[3];
  6125. out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  6126. out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  6127. out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  6128. out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  6129. return out;
  6130. }
  6131. /**
  6132. * Rotates a dual quat around the Z axis
  6133. *
  6134. * @param {quat2} out the receiving dual quaternion
  6135. * @param {ReadonlyQuat2} a the dual quaternion to rotate
  6136. * @param {number} rad how far should the rotation be
  6137. * @returns {quat2} out
  6138. */
  6139. function rotateZ$3(out, a, rad) {
  6140. var bx = -a[0],
  6141. by = -a[1],
  6142. bz = -a[2],
  6143. bw = a[3],
  6144. ax = a[4],
  6145. ay = a[5],
  6146. az = a[6],
  6147. aw = a[7],
  6148. ax1 = ax * bw + aw * bx + ay * bz - az * by,
  6149. ay1 = ay * bw + aw * by + az * bx - ax * bz,
  6150. az1 = az * bw + aw * bz + ax * by - ay * bx,
  6151. aw1 = aw * bw - ax * bx - ay * by - az * bz;
  6152. rotateZ$2(out, a, rad);
  6153. bx = out[0];
  6154. by = out[1];
  6155. bz = out[2];
  6156. bw = out[3];
  6157. out[4] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  6158. out[5] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  6159. out[6] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  6160. out[7] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  6161. return out;
  6162. }
  6163. /**
  6164. * Rotates a dual quat by a given quaternion (a * q)
  6165. *
  6166. * @param {quat2} out the receiving dual quaternion
  6167. * @param {ReadonlyQuat2} a the dual quaternion to rotate
  6168. * @param {ReadonlyQuat} q quaternion to rotate by
  6169. * @returns {quat2} out
  6170. */
  6171. function rotateByQuatAppend(out, a, q) {
  6172. var qx = q[0],
  6173. qy = q[1],
  6174. qz = q[2],
  6175. qw = q[3],
  6176. ax = a[0],
  6177. ay = a[1],
  6178. az = a[2],
  6179. aw = a[3];
  6180. out[0] = ax * qw + aw * qx + ay * qz - az * qy;
  6181. out[1] = ay * qw + aw * qy + az * qx - ax * qz;
  6182. out[2] = az * qw + aw * qz + ax * qy - ay * qx;
  6183. out[3] = aw * qw - ax * qx - ay * qy - az * qz;
  6184. ax = a[4];
  6185. ay = a[5];
  6186. az = a[6];
  6187. aw = a[7];
  6188. out[4] = ax * qw + aw * qx + ay * qz - az * qy;
  6189. out[5] = ay * qw + aw * qy + az * qx - ax * qz;
  6190. out[6] = az * qw + aw * qz + ax * qy - ay * qx;
  6191. out[7] = aw * qw - ax * qx - ay * qy - az * qz;
  6192. return out;
  6193. }
  6194. /**
  6195. * Rotates a dual quat by a given quaternion (q * a)
  6196. *
  6197. * @param {quat2} out the receiving dual quaternion
  6198. * @param {ReadonlyQuat} q quaternion to rotate by
  6199. * @param {ReadonlyQuat2} a the dual quaternion to rotate
  6200. * @returns {quat2} out
  6201. */
  6202. function rotateByQuatPrepend(out, q, a) {
  6203. var qx = q[0],
  6204. qy = q[1],
  6205. qz = q[2],
  6206. qw = q[3],
  6207. bx = a[0],
  6208. by = a[1],
  6209. bz = a[2],
  6210. bw = a[3];
  6211. out[0] = qx * bw + qw * bx + qy * bz - qz * by;
  6212. out[1] = qy * bw + qw * by + qz * bx - qx * bz;
  6213. out[2] = qz * bw + qw * bz + qx * by - qy * bx;
  6214. out[3] = qw * bw - qx * bx - qy * by - qz * bz;
  6215. bx = a[4];
  6216. by = a[5];
  6217. bz = a[6];
  6218. bw = a[7];
  6219. out[4] = qx * bw + qw * bx + qy * bz - qz * by;
  6220. out[5] = qy * bw + qw * by + qz * bx - qx * bz;
  6221. out[6] = qz * bw + qw * bz + qx * by - qy * bx;
  6222. out[7] = qw * bw - qx * bx - qy * by - qz * bz;
  6223. return out;
  6224. }
  6225. /**
  6226. * Rotates a dual quat around a given axis. Does the normalisation automatically
  6227. *
  6228. * @param {quat2} out the receiving dual quaternion
  6229. * @param {ReadonlyQuat2} a the dual quaternion to rotate
  6230. * @param {ReadonlyVec3} axis the axis to rotate around
  6231. * @param {Number} rad how far the rotation should be
  6232. * @returns {quat2} out
  6233. */
  6234. function rotateAroundAxis(out, a, axis, rad) {
  6235. //Special case for rad = 0
  6236. if (Math.abs(rad) < EPSILON) {
  6237. return copy$7(out, a);
  6238. }
  6239. var axisLength = Math.hypot(axis[0], axis[1], axis[2]);
  6240. rad = rad * 0.5;
  6241. var s = Math.sin(rad);
  6242. var bx = s * axis[0] / axisLength;
  6243. var by = s * axis[1] / axisLength;
  6244. var bz = s * axis[2] / axisLength;
  6245. var bw = Math.cos(rad);
  6246. var ax1 = a[0],
  6247. ay1 = a[1],
  6248. az1 = a[2],
  6249. aw1 = a[3];
  6250. out[0] = ax1 * bw + aw1 * bx + ay1 * bz - az1 * by;
  6251. out[1] = ay1 * bw + aw1 * by + az1 * bx - ax1 * bz;
  6252. out[2] = az1 * bw + aw1 * bz + ax1 * by - ay1 * bx;
  6253. out[3] = aw1 * bw - ax1 * bx - ay1 * by - az1 * bz;
  6254. var ax = a[4],
  6255. ay = a[5],
  6256. az = a[6],
  6257. aw = a[7];
  6258. out[4] = ax * bw + aw * bx + ay * bz - az * by;
  6259. out[5] = ay * bw + aw * by + az * bx - ax * bz;
  6260. out[6] = az * bw + aw * bz + ax * by - ay * bx;
  6261. out[7] = aw * bw - ax * bx - ay * by - az * bz;
  6262. return out;
  6263. }
  6264. /**
  6265. * Adds two dual quat's
  6266. *
  6267. * @param {quat2} out the receiving dual quaternion
  6268. * @param {ReadonlyQuat2} a the first operand
  6269. * @param {ReadonlyQuat2} b the second operand
  6270. * @returns {quat2} out
  6271. * @function
  6272. */
  6273. function add$7(out, a, b) {
  6274. out[0] = a[0] + b[0];
  6275. out[1] = a[1] + b[1];
  6276. out[2] = a[2] + b[2];
  6277. out[3] = a[3] + b[3];
  6278. out[4] = a[4] + b[4];
  6279. out[5] = a[5] + b[5];
  6280. out[6] = a[6] + b[6];
  6281. out[7] = a[7] + b[7];
  6282. return out;
  6283. }
  6284. /**
  6285. * Multiplies two dual quat's
  6286. *
  6287. * @param {quat2} out the receiving dual quaternion
  6288. * @param {ReadonlyQuat2} a the first operand
  6289. * @param {ReadonlyQuat2} b the second operand
  6290. * @returns {quat2} out
  6291. */
  6292. function multiply$7(out, a, b) {
  6293. var ax0 = a[0],
  6294. ay0 = a[1],
  6295. az0 = a[2],
  6296. aw0 = a[3],
  6297. bx1 = b[4],
  6298. by1 = b[5],
  6299. bz1 = b[6],
  6300. bw1 = b[7],
  6301. ax1 = a[4],
  6302. ay1 = a[5],
  6303. az1 = a[6],
  6304. aw1 = a[7],
  6305. bx0 = b[0],
  6306. by0 = b[1],
  6307. bz0 = b[2],
  6308. bw0 = b[3];
  6309. out[0] = ax0 * bw0 + aw0 * bx0 + ay0 * bz0 - az0 * by0;
  6310. out[1] = ay0 * bw0 + aw0 * by0 + az0 * bx0 - ax0 * bz0;
  6311. out[2] = az0 * bw0 + aw0 * bz0 + ax0 * by0 - ay0 * bx0;
  6312. out[3] = aw0 * bw0 - ax0 * bx0 - ay0 * by0 - az0 * bz0;
  6313. out[4] = ax0 * bw1 + aw0 * bx1 + ay0 * bz1 - az0 * by1 + ax1 * bw0 + aw1 * bx0 + ay1 * bz0 - az1 * by0;
  6314. out[5] = ay0 * bw1 + aw0 * by1 + az0 * bx1 - ax0 * bz1 + ay1 * bw0 + aw1 * by0 + az1 * bx0 - ax1 * bz0;
  6315. out[6] = az0 * bw1 + aw0 * bz1 + ax0 * by1 - ay0 * bx1 + az1 * bw0 + aw1 * bz0 + ax1 * by0 - ay1 * bx0;
  6316. out[7] = aw0 * bw1 - ax0 * bx1 - ay0 * by1 - az0 * bz1 + aw1 * bw0 - ax1 * bx0 - ay1 * by0 - az1 * bz0;
  6317. return out;
  6318. }
  6319. /**
  6320. * Alias for {@link quat2.multiply}
  6321. * @function
  6322. */
  6323. var mul$7 = multiply$7;
  6324. /**
  6325. * Scales a dual quat by a scalar number
  6326. *
  6327. * @param {quat2} out the receiving dual quat
  6328. * @param {ReadonlyQuat2} a the dual quat to scale
  6329. * @param {Number} b amount to scale the dual quat by
  6330. * @returns {quat2} out
  6331. * @function
  6332. */
  6333. function scale$7(out, a, b) {
  6334. out[0] = a[0] * b;
  6335. out[1] = a[1] * b;
  6336. out[2] = a[2] * b;
  6337. out[3] = a[3] * b;
  6338. out[4] = a[4] * b;
  6339. out[5] = a[5] * b;
  6340. out[6] = a[6] * b;
  6341. out[7] = a[7] * b;
  6342. return out;
  6343. }
  6344. /**
  6345. * Calculates the dot product of two dual quat's (The dot product of the real parts)
  6346. *
  6347. * @param {ReadonlyQuat2} a the first operand
  6348. * @param {ReadonlyQuat2} b the second operand
  6349. * @returns {Number} dot product of a and b
  6350. * @function
  6351. */
  6352. var dot$3 = dot$2;
  6353. /**
  6354. * Performs a linear interpolation between two dual quats's
  6355. * NOTE: The resulting dual quaternions won't always be normalized (The error is most noticeable when t = 0.5)
  6356. *
  6357. * @param {quat2} out the receiving dual quat
  6358. * @param {ReadonlyQuat2} a the first operand
  6359. * @param {ReadonlyQuat2} b the second operand
  6360. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  6361. * @returns {quat2} out
  6362. */
  6363. function lerp$3(out, a, b, t) {
  6364. var mt = 1 - t;
  6365. if (dot$3(a, b) < 0) t = -t;
  6366. out[0] = a[0] * mt + b[0] * t;
  6367. out[1] = a[1] * mt + b[1] * t;
  6368. out[2] = a[2] * mt + b[2] * t;
  6369. out[3] = a[3] * mt + b[3] * t;
  6370. out[4] = a[4] * mt + b[4] * t;
  6371. out[5] = a[5] * mt + b[5] * t;
  6372. out[6] = a[6] * mt + b[6] * t;
  6373. out[7] = a[7] * mt + b[7] * t;
  6374. return out;
  6375. }
  6376. /**
  6377. * Calculates the inverse of a dual quat. If they are normalized, conjugate is cheaper
  6378. *
  6379. * @param {quat2} out the receiving dual quaternion
  6380. * @param {ReadonlyQuat2} a dual quat to calculate inverse of
  6381. * @returns {quat2} out
  6382. */
  6383. function invert$5(out, a) {
  6384. var sqlen = squaredLength$3(a);
  6385. out[0] = -a[0] / sqlen;
  6386. out[1] = -a[1] / sqlen;
  6387. out[2] = -a[2] / sqlen;
  6388. out[3] = a[3] / sqlen;
  6389. out[4] = -a[4] / sqlen;
  6390. out[5] = -a[5] / sqlen;
  6391. out[6] = -a[6] / sqlen;
  6392. out[7] = a[7] / sqlen;
  6393. return out;
  6394. }
  6395. /**
  6396. * Calculates the conjugate of a dual quat
  6397. * If the dual quaternion is normalized, this function is faster than quat2.inverse and produces the same result.
  6398. *
  6399. * @param {quat2} out the receiving quaternion
  6400. * @param {ReadonlyQuat2} a quat to calculate conjugate of
  6401. * @returns {quat2} out
  6402. */
  6403. function conjugate$1(out, a) {
  6404. out[0] = -a[0];
  6405. out[1] = -a[1];
  6406. out[2] = -a[2];
  6407. out[3] = a[3];
  6408. out[4] = -a[4];
  6409. out[5] = -a[5];
  6410. out[6] = -a[6];
  6411. out[7] = a[7];
  6412. return out;
  6413. }
  6414. /**
  6415. * Calculates the length of a dual quat
  6416. *
  6417. * @param {ReadonlyQuat2} a dual quat to calculate length of
  6418. * @returns {Number} length of a
  6419. * @function
  6420. */
  6421. var length$3 = length$2;
  6422. /**
  6423. * Alias for {@link quat2.length}
  6424. * @function
  6425. */
  6426. var len$3 = length$3;
  6427. /**
  6428. * Calculates the squared length of a dual quat
  6429. *
  6430. * @param {ReadonlyQuat2} a dual quat to calculate squared length of
  6431. * @returns {Number} squared length of a
  6432. * @function
  6433. */
  6434. var squaredLength$3 = squaredLength$2;
  6435. /**
  6436. * Alias for {@link quat2.squaredLength}
  6437. * @function
  6438. */
  6439. var sqrLen$3 = squaredLength$3;
  6440. /**
  6441. * Normalize a dual quat
  6442. *
  6443. * @param {quat2} out the receiving dual quaternion
  6444. * @param {ReadonlyQuat2} a dual quaternion to normalize
  6445. * @returns {quat2} out
  6446. * @function
  6447. */
  6448. function normalize$3(out, a) {
  6449. var magnitude = squaredLength$3(a);
  6450. if (magnitude > 0) {
  6451. magnitude = Math.sqrt(magnitude);
  6452. var a0 = a[0] / magnitude;
  6453. var a1 = a[1] / magnitude;
  6454. var a2 = a[2] / magnitude;
  6455. var a3 = a[3] / magnitude;
  6456. var b0 = a[4];
  6457. var b1 = a[5];
  6458. var b2 = a[6];
  6459. var b3 = a[7];
  6460. var a_dot_b = a0 * b0 + a1 * b1 + a2 * b2 + a3 * b3;
  6461. out[0] = a0;
  6462. out[1] = a1;
  6463. out[2] = a2;
  6464. out[3] = a3;
  6465. out[4] = (b0 - a0 * a_dot_b) / magnitude;
  6466. out[5] = (b1 - a1 * a_dot_b) / magnitude;
  6467. out[6] = (b2 - a2 * a_dot_b) / magnitude;
  6468. out[7] = (b3 - a3 * a_dot_b) / magnitude;
  6469. }
  6470. return out;
  6471. }
  6472. /**
  6473. * Returns a string representation of a dual quatenion
  6474. *
  6475. * @param {ReadonlyQuat2} a dual quaternion to represent as a string
  6476. * @returns {String} string representation of the dual quat
  6477. */
  6478. function str$7(a) {
  6479. return "quat2(" + a[0] + ", " + a[1] + ", " + a[2] + ", " + a[3] + ", " + a[4] + ", " + a[5] + ", " + a[6] + ", " + a[7] + ")";
  6480. }
  6481. /**
  6482. * Returns whether or not the dual quaternions have exactly the same elements in the same position (when compared with ===)
  6483. *
  6484. * @param {ReadonlyQuat2} a the first dual quaternion.
  6485. * @param {ReadonlyQuat2} b the second dual quaternion.
  6486. * @returns {Boolean} true if the dual quaternions are equal, false otherwise.
  6487. */
  6488. function exactEquals$7(a, b) {
  6489. return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3] && a[4] === b[4] && a[5] === b[5] && a[6] === b[6] && a[7] === b[7];
  6490. }
  6491. /**
  6492. * Returns whether or not the dual quaternions have approximately the same elements in the same position.
  6493. *
  6494. * @param {ReadonlyQuat2} a the first dual quat.
  6495. * @param {ReadonlyQuat2} b the second dual quat.
  6496. * @returns {Boolean} true if the dual quats are equal, false otherwise.
  6497. */
  6498. function equals$8(a, b) {
  6499. var a0 = a[0],
  6500. a1 = a[1],
  6501. a2 = a[2],
  6502. a3 = a[3],
  6503. a4 = a[4],
  6504. a5 = a[5],
  6505. a6 = a[6],
  6506. a7 = a[7];
  6507. var b0 = b[0],
  6508. b1 = b[1],
  6509. b2 = b[2],
  6510. b3 = b[3],
  6511. b4 = b[4],
  6512. b5 = b[5],
  6513. b6 = b[6],
  6514. b7 = b[7];
  6515. return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1)) && Math.abs(a2 - b2) <= EPSILON * Math.max(1.0, Math.abs(a2), Math.abs(b2)) && Math.abs(a3 - b3) <= EPSILON * Math.max(1.0, Math.abs(a3), Math.abs(b3)) && Math.abs(a4 - b4) <= EPSILON * Math.max(1.0, Math.abs(a4), Math.abs(b4)) && Math.abs(a5 - b5) <= EPSILON * Math.max(1.0, Math.abs(a5), Math.abs(b5)) && Math.abs(a6 - b6) <= EPSILON * Math.max(1.0, Math.abs(a6), Math.abs(b6)) && Math.abs(a7 - b7) <= EPSILON * Math.max(1.0, Math.abs(a7), Math.abs(b7));
  6516. }
  6517. var quat2 = /*#__PURE__*/Object.freeze({
  6518. __proto__: null,
  6519. create: create$7,
  6520. clone: clone$7,
  6521. fromValues: fromValues$7,
  6522. fromRotationTranslationValues: fromRotationTranslationValues,
  6523. fromRotationTranslation: fromRotationTranslation$1,
  6524. fromTranslation: fromTranslation$3,
  6525. fromRotation: fromRotation$4,
  6526. fromMat4: fromMat4$1,
  6527. copy: copy$7,
  6528. identity: identity$5,
  6529. set: set$7,
  6530. getReal: getReal,
  6531. getDual: getDual,
  6532. setReal: setReal,
  6533. setDual: setDual,
  6534. getTranslation: getTranslation$1,
  6535. translate: translate$3,
  6536. rotateX: rotateX$3,
  6537. rotateY: rotateY$3,
  6538. rotateZ: rotateZ$3,
  6539. rotateByQuatAppend: rotateByQuatAppend,
  6540. rotateByQuatPrepend: rotateByQuatPrepend,
  6541. rotateAroundAxis: rotateAroundAxis,
  6542. add: add$7,
  6543. multiply: multiply$7,
  6544. mul: mul$7,
  6545. scale: scale$7,
  6546. dot: dot$3,
  6547. lerp: lerp$3,
  6548. invert: invert$5,
  6549. conjugate: conjugate$1,
  6550. length: length$3,
  6551. len: len$3,
  6552. squaredLength: squaredLength$3,
  6553. sqrLen: sqrLen$3,
  6554. normalize: normalize$3,
  6555. str: str$7,
  6556. exactEquals: exactEquals$7,
  6557. equals: equals$8
  6558. });
  6559. /**
  6560. * 2 Dimensional Vector
  6561. * @module vec2
  6562. */
  6563. /**
  6564. * Creates a new, empty vec2
  6565. *
  6566. * @returns {vec2} a new 2D vector
  6567. */
  6568. function create$8() {
  6569. var out = new ARRAY_TYPE(2);
  6570. if (ARRAY_TYPE != Float32Array) {
  6571. out[0] = 0;
  6572. out[1] = 0;
  6573. }
  6574. return out;
  6575. }
  6576. /**
  6577. * Creates a new vec2 initialized with values from an existing vector
  6578. *
  6579. * @param {ReadonlyVec2} a vector to clone
  6580. * @returns {vec2} a new 2D vector
  6581. */
  6582. function clone$8(a) {
  6583. var out = new ARRAY_TYPE(2);
  6584. out[0] = a[0];
  6585. out[1] = a[1];
  6586. return out;
  6587. }
  6588. /**
  6589. * Creates a new vec2 initialized with the given values
  6590. *
  6591. * @param {Number} x X component
  6592. * @param {Number} y Y component
  6593. * @returns {vec2} a new 2D vector
  6594. */
  6595. function fromValues$8(x, y) {
  6596. var out = new ARRAY_TYPE(2);
  6597. out[0] = x;
  6598. out[1] = y;
  6599. return out;
  6600. }
  6601. /**
  6602. * Copy the values from one vec2 to another
  6603. *
  6604. * @param {vec2} out the receiving vector
  6605. * @param {ReadonlyVec2} a the source vector
  6606. * @returns {vec2} out
  6607. */
  6608. function copy$8(out, a) {
  6609. out[0] = a[0];
  6610. out[1] = a[1];
  6611. return out;
  6612. }
  6613. /**
  6614. * Set the components of a vec2 to the given values
  6615. *
  6616. * @param {vec2} out the receiving vector
  6617. * @param {Number} x X component
  6618. * @param {Number} y Y component
  6619. * @returns {vec2} out
  6620. */
  6621. function set$8(out, x, y) {
  6622. out[0] = x;
  6623. out[1] = y;
  6624. return out;
  6625. }
  6626. /**
  6627. * Adds two vec2's
  6628. *
  6629. * @param {vec2} out the receiving vector
  6630. * @param {ReadonlyVec2} a the first operand
  6631. * @param {ReadonlyVec2} b the second operand
  6632. * @returns {vec2} out
  6633. */
  6634. function add$8(out, a, b) {
  6635. out[0] = a[0] + b[0];
  6636. out[1] = a[1] + b[1];
  6637. return out;
  6638. }
  6639. /**
  6640. * Subtracts vector b from vector a
  6641. *
  6642. * @param {vec2} out the receiving vector
  6643. * @param {ReadonlyVec2} a the first operand
  6644. * @param {ReadonlyVec2} b the second operand
  6645. * @returns {vec2} out
  6646. */
  6647. function subtract$6(out, a, b) {
  6648. out[0] = a[0] - b[0];
  6649. out[1] = a[1] - b[1];
  6650. return out;
  6651. }
  6652. /**
  6653. * Multiplies two vec2's
  6654. *
  6655. * @param {vec2} out the receiving vector
  6656. * @param {ReadonlyVec2} a the first operand
  6657. * @param {ReadonlyVec2} b the second operand
  6658. * @returns {vec2} out
  6659. */
  6660. function multiply$8(out, a, b) {
  6661. out[0] = a[0] * b[0];
  6662. out[1] = a[1] * b[1];
  6663. return out;
  6664. }
  6665. /**
  6666. * Divides two vec2's
  6667. *
  6668. * @param {vec2} out the receiving vector
  6669. * @param {ReadonlyVec2} a the first operand
  6670. * @param {ReadonlyVec2} b the second operand
  6671. * @returns {vec2} out
  6672. */
  6673. function divide$2(out, a, b) {
  6674. out[0] = a[0] / b[0];
  6675. out[1] = a[1] / b[1];
  6676. return out;
  6677. }
  6678. /**
  6679. * Math.ceil the components of a vec2
  6680. *
  6681. * @param {vec2} out the receiving vector
  6682. * @param {ReadonlyVec2} a vector to ceil
  6683. * @returns {vec2} out
  6684. */
  6685. function ceil$2(out, a) {
  6686. out[0] = Math.ceil(a[0]);
  6687. out[1] = Math.ceil(a[1]);
  6688. return out;
  6689. }
  6690. /**
  6691. * Math.floor the components of a vec2
  6692. *
  6693. * @param {vec2} out the receiving vector
  6694. * @param {ReadonlyVec2} a vector to floor
  6695. * @returns {vec2} out
  6696. */
  6697. function floor$2(out, a) {
  6698. out[0] = Math.floor(a[0]);
  6699. out[1] = Math.floor(a[1]);
  6700. return out;
  6701. }
  6702. /**
  6703. * Returns the minimum of two vec2's
  6704. *
  6705. * @param {vec2} out the receiving vector
  6706. * @param {ReadonlyVec2} a the first operand
  6707. * @param {ReadonlyVec2} b the second operand
  6708. * @returns {vec2} out
  6709. */
  6710. function min$2(out, a, b) {
  6711. out[0] = Math.min(a[0], b[0]);
  6712. out[1] = Math.min(a[1], b[1]);
  6713. return out;
  6714. }
  6715. /**
  6716. * Returns the maximum of two vec2's
  6717. *
  6718. * @param {vec2} out the receiving vector
  6719. * @param {ReadonlyVec2} a the first operand
  6720. * @param {ReadonlyVec2} b the second operand
  6721. * @returns {vec2} out
  6722. */
  6723. function max$2(out, a, b) {
  6724. out[0] = Math.max(a[0], b[0]);
  6725. out[1] = Math.max(a[1], b[1]);
  6726. return out;
  6727. }
  6728. /**
  6729. * Math.round the components of a vec2
  6730. *
  6731. * @param {vec2} out the receiving vector
  6732. * @param {ReadonlyVec2} a vector to round
  6733. * @returns {vec2} out
  6734. */
  6735. function round$2(out, a) {
  6736. out[0] = Math.round(a[0]);
  6737. out[1] = Math.round(a[1]);
  6738. return out;
  6739. }
  6740. /**
  6741. * Scales a vec2 by a scalar number
  6742. *
  6743. * @param {vec2} out the receiving vector
  6744. * @param {ReadonlyVec2} a the vector to scale
  6745. * @param {Number} b amount to scale the vector by
  6746. * @returns {vec2} out
  6747. */
  6748. function scale$8(out, a, b) {
  6749. out[0] = a[0] * b;
  6750. out[1] = a[1] * b;
  6751. return out;
  6752. }
  6753. /**
  6754. * Adds two vec2's after scaling the second operand by a scalar value
  6755. *
  6756. * @param {vec2} out the receiving vector
  6757. * @param {ReadonlyVec2} a the first operand
  6758. * @param {ReadonlyVec2} b the second operand
  6759. * @param {Number} scale the amount to scale b by before adding
  6760. * @returns {vec2} out
  6761. */
  6762. function scaleAndAdd$2(out, a, b, scale) {
  6763. out[0] = a[0] + b[0] * scale;
  6764. out[1] = a[1] + b[1] * scale;
  6765. return out;
  6766. }
  6767. /**
  6768. * Calculates the euclidian distance between two vec2's
  6769. *
  6770. * @param {ReadonlyVec2} a the first operand
  6771. * @param {ReadonlyVec2} b the second operand
  6772. * @returns {Number} distance between a and b
  6773. */
  6774. function distance$2(a, b) {
  6775. var x = b[0] - a[0],
  6776. y = b[1] - a[1];
  6777. return Math.hypot(x, y);
  6778. }
  6779. /**
  6780. * Calculates the squared euclidian distance between two vec2's
  6781. *
  6782. * @param {ReadonlyVec2} a the first operand
  6783. * @param {ReadonlyVec2} b the second operand
  6784. * @returns {Number} squared distance between a and b
  6785. */
  6786. function squaredDistance$2(a, b) {
  6787. var x = b[0] - a[0],
  6788. y = b[1] - a[1];
  6789. return x * x + y * y;
  6790. }
  6791. /**
  6792. * Calculates the length of a vec2
  6793. *
  6794. * @param {ReadonlyVec2} a vector to calculate length of
  6795. * @returns {Number} length of a
  6796. */
  6797. function length$4(a) {
  6798. var x = a[0],
  6799. y = a[1];
  6800. return Math.hypot(x, y);
  6801. }
  6802. /**
  6803. * Calculates the squared length of a vec2
  6804. *
  6805. * @param {ReadonlyVec2} a vector to calculate squared length of
  6806. * @returns {Number} squared length of a
  6807. */
  6808. function squaredLength$4(a) {
  6809. var x = a[0],
  6810. y = a[1];
  6811. return x * x + y * y;
  6812. }
  6813. /**
  6814. * Negates the components of a vec2
  6815. *
  6816. * @param {vec2} out the receiving vector
  6817. * @param {ReadonlyVec2} a vector to negate
  6818. * @returns {vec2} out
  6819. */
  6820. function negate$2(out, a) {
  6821. out[0] = -a[0];
  6822. out[1] = -a[1];
  6823. return out;
  6824. }
  6825. /**
  6826. * Returns the inverse of the components of a vec2
  6827. *
  6828. * @param {vec2} out the receiving vector
  6829. * @param {ReadonlyVec2} a vector to invert
  6830. * @returns {vec2} out
  6831. */
  6832. function inverse$2(out, a) {
  6833. out[0] = 1.0 / a[0];
  6834. out[1] = 1.0 / a[1];
  6835. return out;
  6836. }
  6837. /**
  6838. * Normalize a vec2
  6839. *
  6840. * @param {vec2} out the receiving vector
  6841. * @param {ReadonlyVec2} a vector to normalize
  6842. * @returns {vec2} out
  6843. */
  6844. function normalize$4(out, a) {
  6845. var x = a[0],
  6846. y = a[1];
  6847. var len = x * x + y * y;
  6848. if (len > 0) {
  6849. //TODO: evaluate use of glm_invsqrt here?
  6850. len = 1 / Math.sqrt(len);
  6851. }
  6852. out[0] = a[0] * len;
  6853. out[1] = a[1] * len;
  6854. return out;
  6855. }
  6856. /**
  6857. * Calculates the dot product of two vec2's
  6858. *
  6859. * @param {ReadonlyVec2} a the first operand
  6860. * @param {ReadonlyVec2} b the second operand
  6861. * @returns {Number} dot product of a and b
  6862. */
  6863. function dot$4(a, b) {
  6864. return a[0] * b[0] + a[1] * b[1];
  6865. }
  6866. /**
  6867. * Computes the cross product of two vec2's
  6868. * Note that the cross product must by definition produce a 3D vector
  6869. *
  6870. * @param {vec3} out the receiving vector
  6871. * @param {ReadonlyVec2} a the first operand
  6872. * @param {ReadonlyVec2} b the second operand
  6873. * @returns {vec3} out
  6874. */
  6875. function cross$2(out, a, b) {
  6876. var z = a[0] * b[1] - a[1] * b[0];
  6877. out[0] = out[1] = 0;
  6878. out[2] = z;
  6879. return out;
  6880. }
  6881. /**
  6882. * Performs a linear interpolation between two vec2's
  6883. *
  6884. * @param {vec2} out the receiving vector
  6885. * @param {ReadonlyVec2} a the first operand
  6886. * @param {ReadonlyVec2} b the second operand
  6887. * @param {Number} t interpolation amount, in the range [0-1], between the two inputs
  6888. * @returns {vec2} out
  6889. */
  6890. function lerp$4(out, a, b, t) {
  6891. var ax = a[0],
  6892. ay = a[1];
  6893. out[0] = ax + t * (b[0] - ax);
  6894. out[1] = ay + t * (b[1] - ay);
  6895. return out;
  6896. }
  6897. /**
  6898. * Generates a random vector with the given scale
  6899. *
  6900. * @param {vec2} out the receiving vector
  6901. * @param {Number} [scale] Length of the resulting vector. If ommitted, a unit vector will be returned
  6902. * @returns {vec2} out
  6903. */
  6904. function random$3(out, scale) {
  6905. scale = scale || 1.0;
  6906. var r = RANDOM() * 2.0 * Math.PI;
  6907. out[0] = Math.cos(r) * scale;
  6908. out[1] = Math.sin(r) * scale;
  6909. return out;
  6910. }
  6911. /**
  6912. * Transforms the vec2 with a mat2
  6913. *
  6914. * @param {vec2} out the receiving vector
  6915. * @param {ReadonlyVec2} a the vector to transform
  6916. * @param {ReadonlyMat2} m matrix to transform with
  6917. * @returns {vec2} out
  6918. */
  6919. function transformMat2(out, a, m) {
  6920. var x = a[0],
  6921. y = a[1];
  6922. out[0] = m[0] * x + m[2] * y;
  6923. out[1] = m[1] * x + m[3] * y;
  6924. return out;
  6925. }
  6926. /**
  6927. * Transforms the vec2 with a mat2d
  6928. *
  6929. * @param {vec2} out the receiving vector
  6930. * @param {ReadonlyVec2} a the vector to transform
  6931. * @param {ReadonlyMat2d} m matrix to transform with
  6932. * @returns {vec2} out
  6933. */
  6934. function transformMat2d(out, a, m) {
  6935. var x = a[0],
  6936. y = a[1];
  6937. out[0] = m[0] * x + m[2] * y + m[4];
  6938. out[1] = m[1] * x + m[3] * y + m[5];
  6939. return out;
  6940. }
  6941. /**
  6942. * Transforms the vec2 with a mat3
  6943. * 3rd vector component is implicitly '1'
  6944. *
  6945. * @param {vec2} out the receiving vector
  6946. * @param {ReadonlyVec2} a the vector to transform
  6947. * @param {ReadonlyMat3} m matrix to transform with
  6948. * @returns {vec2} out
  6949. */
  6950. function transformMat3$1(out, a, m) {
  6951. var x = a[0],
  6952. y = a[1];
  6953. out[0] = m[0] * x + m[3] * y + m[6];
  6954. out[1] = m[1] * x + m[4] * y + m[7];
  6955. return out;
  6956. }
  6957. /**
  6958. * Transforms the vec2 with a mat4
  6959. * 3rd vector component is implicitly '0'
  6960. * 4th vector component is implicitly '1'
  6961. *
  6962. * @param {vec2} out the receiving vector
  6963. * @param {ReadonlyVec2} a the vector to transform
  6964. * @param {ReadonlyMat4} m matrix to transform with
  6965. * @returns {vec2} out
  6966. */
  6967. function transformMat4$2(out, a, m) {
  6968. var x = a[0];
  6969. var y = a[1];
  6970. out[0] = m[0] * x + m[4] * y + m[12];
  6971. out[1] = m[1] * x + m[5] * y + m[13];
  6972. return out;
  6973. }
  6974. /**
  6975. * Rotate a 2D vector
  6976. * @param {vec2} out The receiving vec2
  6977. * @param {ReadonlyVec2} a The vec2 point to rotate
  6978. * @param {ReadonlyVec2} b The origin of the rotation
  6979. * @param {Number} rad The angle of rotation in radians
  6980. * @returns {vec2} out
  6981. */
  6982. function rotate$4(out, a, b, rad) {
  6983. //Translate point to the origin
  6984. var p0 = a[0] - b[0],
  6985. p1 = a[1] - b[1],
  6986. sinC = Math.sin(rad),
  6987. cosC = Math.cos(rad); //perform rotation and translate to correct position
  6988. out[0] = p0 * cosC - p1 * sinC + b[0];
  6989. out[1] = p0 * sinC + p1 * cosC + b[1];
  6990. return out;
  6991. }
  6992. /**
  6993. * Get the angle between two 2D vectors
  6994. * @param {ReadonlyVec2} a The first operand
  6995. * @param {ReadonlyVec2} b The second operand
  6996. * @returns {Number} The angle in radians
  6997. */
  6998. function angle$1(a, b) {
  6999. var x1 = a[0],
  7000. y1 = a[1],
  7001. x2 = b[0],
  7002. y2 = b[1],
  7003. // mag is the product of the magnitudes of a and b
  7004. mag = Math.sqrt(x1 * x1 + y1 * y1) * Math.sqrt(x2 * x2 + y2 * y2),
  7005. // mag &&.. short circuits if mag == 0
  7006. cosine = mag && (x1 * x2 + y1 * y2) / mag; // Math.min(Math.max(cosine, -1), 1) clamps the cosine between -1 and 1
  7007. return Math.acos(Math.min(Math.max(cosine, -1), 1));
  7008. }
  7009. /**
  7010. * Set the components of a vec2 to zero
  7011. *
  7012. * @param {vec2} out the receiving vector
  7013. * @returns {vec2} out
  7014. */
  7015. function zero$2(out) {
  7016. out[0] = 0.0;
  7017. out[1] = 0.0;
  7018. return out;
  7019. }
  7020. /**
  7021. * Returns a string representation of a vector
  7022. *
  7023. * @param {ReadonlyVec2} a vector to represent as a string
  7024. * @returns {String} string representation of the vector
  7025. */
  7026. function str$8(a) {
  7027. return "vec2(" + a[0] + ", " + a[1] + ")";
  7028. }
  7029. /**
  7030. * Returns whether or not the vectors exactly have the same elements in the same position (when compared with ===)
  7031. *
  7032. * @param {ReadonlyVec2} a The first vector.
  7033. * @param {ReadonlyVec2} b The second vector.
  7034. * @returns {Boolean} True if the vectors are equal, false otherwise.
  7035. */
  7036. function exactEquals$8(a, b) {
  7037. return a[0] === b[0] && a[1] === b[1];
  7038. }
  7039. /**
  7040. * Returns whether or not the vectors have approximately the same elements in the same position.
  7041. *
  7042. * @param {ReadonlyVec2} a The first vector.
  7043. * @param {ReadonlyVec2} b The second vector.
  7044. * @returns {Boolean} True if the vectors are equal, false otherwise.
  7045. */
  7046. function equals$9(a, b) {
  7047. var a0 = a[0],
  7048. a1 = a[1];
  7049. var b0 = b[0],
  7050. b1 = b[1];
  7051. return Math.abs(a0 - b0) <= EPSILON * Math.max(1.0, Math.abs(a0), Math.abs(b0)) && Math.abs(a1 - b1) <= EPSILON * Math.max(1.0, Math.abs(a1), Math.abs(b1));
  7052. }
  7053. /**
  7054. * Alias for {@link vec2.length}
  7055. * @function
  7056. */
  7057. var len$4 = length$4;
  7058. /**
  7059. * Alias for {@link vec2.subtract}
  7060. * @function
  7061. */
  7062. var sub$6 = subtract$6;
  7063. /**
  7064. * Alias for {@link vec2.multiply}
  7065. * @function
  7066. */
  7067. var mul$8 = multiply$8;
  7068. /**
  7069. * Alias for {@link vec2.divide}
  7070. * @function
  7071. */
  7072. var div$2 = divide$2;
  7073. /**
  7074. * Alias for {@link vec2.distance}
  7075. * @function
  7076. */
  7077. var dist$2 = distance$2;
  7078. /**
  7079. * Alias for {@link vec2.squaredDistance}
  7080. * @function
  7081. */
  7082. var sqrDist$2 = squaredDistance$2;
  7083. /**
  7084. * Alias for {@link vec2.squaredLength}
  7085. * @function
  7086. */
  7087. var sqrLen$4 = squaredLength$4;
  7088. /**
  7089. * Perform some operation over an array of vec2s.
  7090. *
  7091. * @param {Array} a the array of vectors to iterate over
  7092. * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed
  7093. * @param {Number} offset Number of elements to skip at the beginning of the array
  7094. * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array
  7095. * @param {Function} fn Function to call for each vector in the array
  7096. * @param {Object} [arg] additional argument to pass to fn
  7097. * @returns {Array} a
  7098. * @function
  7099. */
  7100. var forEach$2 = function () {
  7101. var vec = create$8();
  7102. return function (a, stride, offset, count, fn, arg) {
  7103. var i, l;
  7104. if (!stride) {
  7105. stride = 2;
  7106. }
  7107. if (!offset) {
  7108. offset = 0;
  7109. }
  7110. if (count) {
  7111. l = Math.min(count * stride + offset, a.length);
  7112. } else {
  7113. l = a.length;
  7114. }
  7115. for (i = offset; i < l; i += stride) {
  7116. vec[0] = a[i];
  7117. vec[1] = a[i + 1];
  7118. fn(vec, vec, arg);
  7119. a[i] = vec[0];
  7120. a[i + 1] = vec[1];
  7121. }
  7122. return a;
  7123. };
  7124. }();
  7125. var vec2 = /*#__PURE__*/Object.freeze({
  7126. __proto__: null,
  7127. create: create$8,
  7128. clone: clone$8,
  7129. fromValues: fromValues$8,
  7130. copy: copy$8,
  7131. set: set$8,
  7132. add: add$8,
  7133. subtract: subtract$6,
  7134. multiply: multiply$8,
  7135. divide: divide$2,
  7136. ceil: ceil$2,
  7137. floor: floor$2,
  7138. min: min$2,
  7139. max: max$2,
  7140. round: round$2,
  7141. scale: scale$8,
  7142. scaleAndAdd: scaleAndAdd$2,
  7143. distance: distance$2,
  7144. squaredDistance: squaredDistance$2,
  7145. length: length$4,
  7146. squaredLength: squaredLength$4,
  7147. negate: negate$2,
  7148. inverse: inverse$2,
  7149. normalize: normalize$4,
  7150. dot: dot$4,
  7151. cross: cross$2,
  7152. lerp: lerp$4,
  7153. random: random$3,
  7154. transformMat2: transformMat2,
  7155. transformMat2d: transformMat2d,
  7156. transformMat3: transformMat3$1,
  7157. transformMat4: transformMat4$2,
  7158. rotate: rotate$4,
  7159. angle: angle$1,
  7160. zero: zero$2,
  7161. str: str$8,
  7162. exactEquals: exactEquals$8,
  7163. equals: equals$9,
  7164. len: len$4,
  7165. sub: sub$6,
  7166. mul: mul$8,
  7167. div: div$2,
  7168. dist: dist$2,
  7169. sqrDist: sqrDist$2,
  7170. sqrLen: sqrLen$4,
  7171. forEach: forEach$2
  7172. });
  7173. exports.glMatrix = common;
  7174. exports.mat2 = mat2;
  7175. exports.mat2d = mat2d;
  7176. exports.mat3 = mat3;
  7177. exports.mat4 = mat4;
  7178. exports.quat = quat;
  7179. exports.quat2 = quat2;
  7180. exports.vec2 = vec2;
  7181. exports.vec3 = vec3;
  7182. exports.vec4 = vec4;
  7183. Object.defineProperty(exports, '__esModule', { value: true });
  7184. })));