three.core.js 737 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947369483694936950369513695236953369543695536956369573695836959369603696136962369633696436965369663696736968369693697036971369723697336974369753697636977369783697936980369813698236983369843698536986369873698836989369903699136992369933699436995369963699736998369993700037001370023700337004370053700637007370083700937010370113701237013370143701537016370173701837019370203702137022370233702437025370263702737028370293703037031370323703337034370353703637037370383703937040370413704237043370443704537046370473704837049
  1. /**
  2. * @license
  3. * Copyright 2010-2024 Three.js Authors
  4. * SPDX-License-Identifier: MIT
  5. */
  6. const REVISION = '172';
  7. const MOUSE = { LEFT: 0, MIDDLE: 1, RIGHT: 2, ROTATE: 0, DOLLY: 1, PAN: 2 };
  8. const TOUCH = { ROTATE: 0, PAN: 1, DOLLY_PAN: 2, DOLLY_ROTATE: 3 };
  9. const CullFaceNone = 0;
  10. const CullFaceBack = 1;
  11. const CullFaceFront = 2;
  12. const CullFaceFrontBack = 3;
  13. const BasicShadowMap = 0;
  14. const PCFShadowMap = 1;
  15. const PCFSoftShadowMap = 2;
  16. const VSMShadowMap = 3;
  17. const FrontSide = 0;
  18. const BackSide = 1;
  19. const DoubleSide = 2;
  20. const NoBlending = 0;
  21. const NormalBlending = 1;
  22. const AdditiveBlending = 2;
  23. const SubtractiveBlending = 3;
  24. const MultiplyBlending = 4;
  25. const CustomBlending = 5;
  26. const AddEquation = 100;
  27. const SubtractEquation = 101;
  28. const ReverseSubtractEquation = 102;
  29. const MinEquation = 103;
  30. const MaxEquation = 104;
  31. const ZeroFactor = 200;
  32. const OneFactor = 201;
  33. const SrcColorFactor = 202;
  34. const OneMinusSrcColorFactor = 203;
  35. const SrcAlphaFactor = 204;
  36. const OneMinusSrcAlphaFactor = 205;
  37. const DstAlphaFactor = 206;
  38. const OneMinusDstAlphaFactor = 207;
  39. const DstColorFactor = 208;
  40. const OneMinusDstColorFactor = 209;
  41. const SrcAlphaSaturateFactor = 210;
  42. const ConstantColorFactor = 211;
  43. const OneMinusConstantColorFactor = 212;
  44. const ConstantAlphaFactor = 213;
  45. const OneMinusConstantAlphaFactor = 214;
  46. const NeverDepth = 0;
  47. const AlwaysDepth = 1;
  48. const LessDepth = 2;
  49. const LessEqualDepth = 3;
  50. const EqualDepth = 4;
  51. const GreaterEqualDepth = 5;
  52. const GreaterDepth = 6;
  53. const NotEqualDepth = 7;
  54. const MultiplyOperation = 0;
  55. const MixOperation = 1;
  56. const AddOperation = 2;
  57. const NoToneMapping = 0;
  58. const LinearToneMapping = 1;
  59. const ReinhardToneMapping = 2;
  60. const CineonToneMapping = 3;
  61. const ACESFilmicToneMapping = 4;
  62. const CustomToneMapping = 5;
  63. const AgXToneMapping = 6;
  64. const NeutralToneMapping = 7;
  65. const AttachedBindMode = 'attached';
  66. const DetachedBindMode = 'detached';
  67. const UVMapping = 300;
  68. const CubeReflectionMapping = 301;
  69. const CubeRefractionMapping = 302;
  70. const EquirectangularReflectionMapping = 303;
  71. const EquirectangularRefractionMapping = 304;
  72. const CubeUVReflectionMapping = 306;
  73. const RepeatWrapping = 1000;
  74. const ClampToEdgeWrapping = 1001;
  75. const MirroredRepeatWrapping = 1002;
  76. const NearestFilter = 1003;
  77. const NearestMipmapNearestFilter = 1004;
  78. const NearestMipMapNearestFilter = 1004;
  79. const NearestMipmapLinearFilter = 1005;
  80. const NearestMipMapLinearFilter = 1005;
  81. const LinearFilter = 1006;
  82. const LinearMipmapNearestFilter = 1007;
  83. const LinearMipMapNearestFilter = 1007;
  84. const LinearMipmapLinearFilter = 1008;
  85. const LinearMipMapLinearFilter = 1008;
  86. const UnsignedByteType = 1009;
  87. const ByteType = 1010;
  88. const ShortType = 1011;
  89. const UnsignedShortType = 1012;
  90. const IntType = 1013;
  91. const UnsignedIntType = 1014;
  92. const FloatType = 1015;
  93. const HalfFloatType = 1016;
  94. const UnsignedShort4444Type = 1017;
  95. const UnsignedShort5551Type = 1018;
  96. const UnsignedInt248Type = 1020;
  97. const UnsignedInt5999Type = 35902;
  98. const AlphaFormat = 1021;
  99. const RGBFormat = 1022;
  100. const RGBAFormat = 1023;
  101. const LuminanceFormat = 1024;
  102. const LuminanceAlphaFormat = 1025;
  103. const DepthFormat = 1026;
  104. const DepthStencilFormat = 1027;
  105. const RedFormat = 1028;
  106. const RedIntegerFormat = 1029;
  107. const RGFormat = 1030;
  108. const RGIntegerFormat = 1031;
  109. const RGBIntegerFormat = 1032;
  110. const RGBAIntegerFormat = 1033;
  111. const RGB_S3TC_DXT1_Format = 33776;
  112. const RGBA_S3TC_DXT1_Format = 33777;
  113. const RGBA_S3TC_DXT3_Format = 33778;
  114. const RGBA_S3TC_DXT5_Format = 33779;
  115. const RGB_PVRTC_4BPPV1_Format = 35840;
  116. const RGB_PVRTC_2BPPV1_Format = 35841;
  117. const RGBA_PVRTC_4BPPV1_Format = 35842;
  118. const RGBA_PVRTC_2BPPV1_Format = 35843;
  119. const RGB_ETC1_Format = 36196;
  120. const RGB_ETC2_Format = 37492;
  121. const RGBA_ETC2_EAC_Format = 37496;
  122. const RGBA_ASTC_4x4_Format = 37808;
  123. const RGBA_ASTC_5x4_Format = 37809;
  124. const RGBA_ASTC_5x5_Format = 37810;
  125. const RGBA_ASTC_6x5_Format = 37811;
  126. const RGBA_ASTC_6x6_Format = 37812;
  127. const RGBA_ASTC_8x5_Format = 37813;
  128. const RGBA_ASTC_8x6_Format = 37814;
  129. const RGBA_ASTC_8x8_Format = 37815;
  130. const RGBA_ASTC_10x5_Format = 37816;
  131. const RGBA_ASTC_10x6_Format = 37817;
  132. const RGBA_ASTC_10x8_Format = 37818;
  133. const RGBA_ASTC_10x10_Format = 37819;
  134. const RGBA_ASTC_12x10_Format = 37820;
  135. const RGBA_ASTC_12x12_Format = 37821;
  136. const RGBA_BPTC_Format = 36492;
  137. const RGB_BPTC_SIGNED_Format = 36494;
  138. const RGB_BPTC_UNSIGNED_Format = 36495;
  139. const RED_RGTC1_Format = 36283;
  140. const SIGNED_RED_RGTC1_Format = 36284;
  141. const RED_GREEN_RGTC2_Format = 36285;
  142. const SIGNED_RED_GREEN_RGTC2_Format = 36286;
  143. const LoopOnce = 2200;
  144. const LoopRepeat = 2201;
  145. const LoopPingPong = 2202;
  146. const InterpolateDiscrete = 2300;
  147. const InterpolateLinear = 2301;
  148. const InterpolateSmooth = 2302;
  149. const ZeroCurvatureEnding = 2400;
  150. const ZeroSlopeEnding = 2401;
  151. const WrapAroundEnding = 2402;
  152. const NormalAnimationBlendMode = 2500;
  153. const AdditiveAnimationBlendMode = 2501;
  154. const TrianglesDrawMode = 0;
  155. const TriangleStripDrawMode = 1;
  156. const TriangleFanDrawMode = 2;
  157. const BasicDepthPacking = 3200;
  158. const RGBADepthPacking = 3201;
  159. const RGBDepthPacking = 3202;
  160. const RGDepthPacking = 3203;
  161. const TangentSpaceNormalMap = 0;
  162. const ObjectSpaceNormalMap = 1;
  163. // Color space string identifiers, matching CSS Color Module Level 4 and WebGPU names where available.
  164. const NoColorSpace = '';
  165. const SRGBColorSpace = 'srgb';
  166. const LinearSRGBColorSpace = 'srgb-linear';
  167. const LinearTransfer = 'linear';
  168. const SRGBTransfer = 'srgb';
  169. const ZeroStencilOp = 0;
  170. const KeepStencilOp = 7680;
  171. const ReplaceStencilOp = 7681;
  172. const IncrementStencilOp = 7682;
  173. const DecrementStencilOp = 7683;
  174. const IncrementWrapStencilOp = 34055;
  175. const DecrementWrapStencilOp = 34056;
  176. const InvertStencilOp = 5386;
  177. const NeverStencilFunc = 512;
  178. const LessStencilFunc = 513;
  179. const EqualStencilFunc = 514;
  180. const LessEqualStencilFunc = 515;
  181. const GreaterStencilFunc = 516;
  182. const NotEqualStencilFunc = 517;
  183. const GreaterEqualStencilFunc = 518;
  184. const AlwaysStencilFunc = 519;
  185. const NeverCompare = 512;
  186. const LessCompare = 513;
  187. const EqualCompare = 514;
  188. const LessEqualCompare = 515;
  189. const GreaterCompare = 516;
  190. const NotEqualCompare = 517;
  191. const GreaterEqualCompare = 518;
  192. const AlwaysCompare = 519;
  193. const StaticDrawUsage = 35044;
  194. const DynamicDrawUsage = 35048;
  195. const StreamDrawUsage = 35040;
  196. const StaticReadUsage = 35045;
  197. const DynamicReadUsage = 35049;
  198. const StreamReadUsage = 35041;
  199. const StaticCopyUsage = 35046;
  200. const DynamicCopyUsage = 35050;
  201. const StreamCopyUsage = 35042;
  202. const GLSL1 = '100';
  203. const GLSL3 = '300 es';
  204. const WebGLCoordinateSystem = 2000;
  205. const WebGPUCoordinateSystem = 2001;
  206. /**
  207. * https://github.com/mrdoob/eventdispatcher.js/
  208. */
  209. class EventDispatcher {
  210. addEventListener( type, listener ) {
  211. if ( this._listeners === undefined ) this._listeners = {};
  212. const listeners = this._listeners;
  213. if ( listeners[ type ] === undefined ) {
  214. listeners[ type ] = [];
  215. }
  216. if ( listeners[ type ].indexOf( listener ) === - 1 ) {
  217. listeners[ type ].push( listener );
  218. }
  219. }
  220. hasEventListener( type, listener ) {
  221. if ( this._listeners === undefined ) return false;
  222. const listeners = this._listeners;
  223. return listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1;
  224. }
  225. removeEventListener( type, listener ) {
  226. if ( this._listeners === undefined ) return;
  227. const listeners = this._listeners;
  228. const listenerArray = listeners[ type ];
  229. if ( listenerArray !== undefined ) {
  230. const index = listenerArray.indexOf( listener );
  231. if ( index !== - 1 ) {
  232. listenerArray.splice( index, 1 );
  233. }
  234. }
  235. }
  236. dispatchEvent( event ) {
  237. if ( this._listeners === undefined ) return;
  238. const listeners = this._listeners;
  239. const listenerArray = listeners[ event.type ];
  240. if ( listenerArray !== undefined ) {
  241. event.target = this;
  242. // Make a copy, in case listeners are removed while iterating.
  243. const array = listenerArray.slice( 0 );
  244. for ( let i = 0, l = array.length; i < l; i ++ ) {
  245. array[ i ].call( this, event );
  246. }
  247. event.target = null;
  248. }
  249. }
  250. }
  251. const _lut = [ '00', '01', '02', '03', '04', '05', '06', '07', '08', '09', '0a', '0b', '0c', '0d', '0e', '0f', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '1a', '1b', '1c', '1d', '1e', '1f', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '2a', '2b', '2c', '2d', '2e', '2f', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '3a', '3b', '3c', '3d', '3e', '3f', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '4a', '4b', '4c', '4d', '4e', '4f', '50', '51', '52', '53', '54', '55', '56', '57', '58', '59', '5a', '5b', '5c', '5d', '5e', '5f', '60', '61', '62', '63', '64', '65', '66', '67', '68', '69', '6a', '6b', '6c', '6d', '6e', '6f', '70', '71', '72', '73', '74', '75', '76', '77', '78', '79', '7a', '7b', '7c', '7d', '7e', '7f', '80', '81', '82', '83', '84', '85', '86', '87', '88', '89', '8a', '8b', '8c', '8d', '8e', '8f', '90', '91', '92', '93', '94', '95', '96', '97', '98', '99', '9a', '9b', '9c', '9d', '9e', '9f', 'a0', 'a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'b0', 'b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'ba', 'bb', 'bc', 'bd', 'be', 'bf', 'c0', 'c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'ca', 'cb', 'cc', 'cd', 'ce', 'cf', 'd0', 'd1', 'd2', 'd3', 'd4', 'd5', 'd6', 'd7', 'd8', 'd9', 'da', 'db', 'dc', 'dd', 'de', 'df', 'e0', 'e1', 'e2', 'e3', 'e4', 'e5', 'e6', 'e7', 'e8', 'e9', 'ea', 'eb', 'ec', 'ed', 'ee', 'ef', 'f0', 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'fa', 'fb', 'fc', 'fd', 'fe', 'ff' ];
  252. let _seed = 1234567;
  253. const DEG2RAD = Math.PI / 180;
  254. const RAD2DEG = 180 / Math.PI;
  255. // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
  256. function generateUUID() {
  257. const d0 = Math.random() * 0xffffffff | 0;
  258. const d1 = Math.random() * 0xffffffff | 0;
  259. const d2 = Math.random() * 0xffffffff | 0;
  260. const d3 = Math.random() * 0xffffffff | 0;
  261. const uuid = _lut[ d0 & 0xff ] + _lut[ d0 >> 8 & 0xff ] + _lut[ d0 >> 16 & 0xff ] + _lut[ d0 >> 24 & 0xff ] + '-' +
  262. _lut[ d1 & 0xff ] + _lut[ d1 >> 8 & 0xff ] + '-' + _lut[ d1 >> 16 & 0x0f | 0x40 ] + _lut[ d1 >> 24 & 0xff ] + '-' +
  263. _lut[ d2 & 0x3f | 0x80 ] + _lut[ d2 >> 8 & 0xff ] + '-' + _lut[ d2 >> 16 & 0xff ] + _lut[ d2 >> 24 & 0xff ] +
  264. _lut[ d3 & 0xff ] + _lut[ d3 >> 8 & 0xff ] + _lut[ d3 >> 16 & 0xff ] + _lut[ d3 >> 24 & 0xff ];
  265. // .toLowerCase() here flattens concatenated strings to save heap memory space.
  266. return uuid.toLowerCase();
  267. }
  268. function clamp( value, min, max ) {
  269. return Math.max( min, Math.min( max, value ) );
  270. }
  271. // compute euclidean modulo of m % n
  272. // https://en.wikipedia.org/wiki/Modulo_operation
  273. function euclideanModulo( n, m ) {
  274. return ( ( n % m ) + m ) % m;
  275. }
  276. // Linear mapping from range <a1, a2> to range <b1, b2>
  277. function mapLinear( x, a1, a2, b1, b2 ) {
  278. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  279. }
  280. // https://www.gamedev.net/tutorials/programming/general-and-gameplay-programming/inverse-lerp-a-super-useful-yet-often-overlooked-function-r5230/
  281. function inverseLerp( x, y, value ) {
  282. if ( x !== y ) {
  283. return ( value - x ) / ( y - x );
  284. } else {
  285. return 0;
  286. }
  287. }
  288. // https://en.wikipedia.org/wiki/Linear_interpolation
  289. function lerp( x, y, t ) {
  290. return ( 1 - t ) * x + t * y;
  291. }
  292. // http://www.rorydriscoll.com/2016/03/07/frame-rate-independent-damping-using-lerp/
  293. function damp( x, y, lambda, dt ) {
  294. return lerp( x, y, 1 - Math.exp( - lambda * dt ) );
  295. }
  296. // https://www.desmos.com/calculator/vcsjnyz7x4
  297. function pingpong( x, length = 1 ) {
  298. return length - Math.abs( euclideanModulo( x, length * 2 ) - length );
  299. }
  300. // http://en.wikipedia.org/wiki/Smoothstep
  301. function smoothstep( x, min, max ) {
  302. if ( x <= min ) return 0;
  303. if ( x >= max ) return 1;
  304. x = ( x - min ) / ( max - min );
  305. return x * x * ( 3 - 2 * x );
  306. }
  307. function smootherstep( x, min, max ) {
  308. if ( x <= min ) return 0;
  309. if ( x >= max ) return 1;
  310. x = ( x - min ) / ( max - min );
  311. return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
  312. }
  313. // Random integer from <low, high> interval
  314. function randInt( low, high ) {
  315. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  316. }
  317. // Random float from <low, high> interval
  318. function randFloat( low, high ) {
  319. return low + Math.random() * ( high - low );
  320. }
  321. // Random float from <-range/2, range/2> interval
  322. function randFloatSpread( range ) {
  323. return range * ( 0.5 - Math.random() );
  324. }
  325. // Deterministic pseudo-random float in the interval [ 0, 1 ]
  326. function seededRandom( s ) {
  327. if ( s !== undefined ) _seed = s;
  328. // Mulberry32 generator
  329. let t = _seed += 0x6D2B79F5;
  330. t = Math.imul( t ^ t >>> 15, t | 1 );
  331. t ^= t + Math.imul( t ^ t >>> 7, t | 61 );
  332. return ( ( t ^ t >>> 14 ) >>> 0 ) / 4294967296;
  333. }
  334. function degToRad( degrees ) {
  335. return degrees * DEG2RAD;
  336. }
  337. function radToDeg( radians ) {
  338. return radians * RAD2DEG;
  339. }
  340. function isPowerOfTwo( value ) {
  341. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  342. }
  343. function ceilPowerOfTwo( value ) {
  344. return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) );
  345. }
  346. function floorPowerOfTwo( value ) {
  347. return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) );
  348. }
  349. function setQuaternionFromProperEuler( q, a, b, c, order ) {
  350. // Intrinsic Proper Euler Angles - see https://en.wikipedia.org/wiki/Euler_angles
  351. // rotations are applied to the axes in the order specified by 'order'
  352. // rotation by angle 'a' is applied first, then by angle 'b', then by angle 'c'
  353. // angles are in radians
  354. const cos = Math.cos;
  355. const sin = Math.sin;
  356. const c2 = cos( b / 2 );
  357. const s2 = sin( b / 2 );
  358. const c13 = cos( ( a + c ) / 2 );
  359. const s13 = sin( ( a + c ) / 2 );
  360. const c1_3 = cos( ( a - c ) / 2 );
  361. const s1_3 = sin( ( a - c ) / 2 );
  362. const c3_1 = cos( ( c - a ) / 2 );
  363. const s3_1 = sin( ( c - a ) / 2 );
  364. switch ( order ) {
  365. case 'XYX':
  366. q.set( c2 * s13, s2 * c1_3, s2 * s1_3, c2 * c13 );
  367. break;
  368. case 'YZY':
  369. q.set( s2 * s1_3, c2 * s13, s2 * c1_3, c2 * c13 );
  370. break;
  371. case 'ZXZ':
  372. q.set( s2 * c1_3, s2 * s1_3, c2 * s13, c2 * c13 );
  373. break;
  374. case 'XZX':
  375. q.set( c2 * s13, s2 * s3_1, s2 * c3_1, c2 * c13 );
  376. break;
  377. case 'YXY':
  378. q.set( s2 * c3_1, c2 * s13, s2 * s3_1, c2 * c13 );
  379. break;
  380. case 'ZYZ':
  381. q.set( s2 * s3_1, s2 * c3_1, c2 * s13, c2 * c13 );
  382. break;
  383. default:
  384. console.warn( 'THREE.MathUtils: .setQuaternionFromProperEuler() encountered an unknown order: ' + order );
  385. }
  386. }
  387. function denormalize( value, array ) {
  388. switch ( array.constructor ) {
  389. case Float32Array:
  390. return value;
  391. case Uint32Array:
  392. return value / 4294967295.0;
  393. case Uint16Array:
  394. return value / 65535.0;
  395. case Uint8Array:
  396. return value / 255.0;
  397. case Int32Array:
  398. return Math.max( value / 2147483647.0, - 1.0 );
  399. case Int16Array:
  400. return Math.max( value / 32767.0, - 1.0 );
  401. case Int8Array:
  402. return Math.max( value / 127.0, - 1.0 );
  403. default:
  404. throw new Error( 'Invalid component type.' );
  405. }
  406. }
  407. function normalize( value, array ) {
  408. switch ( array.constructor ) {
  409. case Float32Array:
  410. return value;
  411. case Uint32Array:
  412. return Math.round( value * 4294967295.0 );
  413. case Uint16Array:
  414. return Math.round( value * 65535.0 );
  415. case Uint8Array:
  416. return Math.round( value * 255.0 );
  417. case Int32Array:
  418. return Math.round( value * 2147483647.0 );
  419. case Int16Array:
  420. return Math.round( value * 32767.0 );
  421. case Int8Array:
  422. return Math.round( value * 127.0 );
  423. default:
  424. throw new Error( 'Invalid component type.' );
  425. }
  426. }
  427. const MathUtils = {
  428. DEG2RAD: DEG2RAD,
  429. RAD2DEG: RAD2DEG,
  430. generateUUID: generateUUID,
  431. clamp: clamp,
  432. euclideanModulo: euclideanModulo,
  433. mapLinear: mapLinear,
  434. inverseLerp: inverseLerp,
  435. lerp: lerp,
  436. damp: damp,
  437. pingpong: pingpong,
  438. smoothstep: smoothstep,
  439. smootherstep: smootherstep,
  440. randInt: randInt,
  441. randFloat: randFloat,
  442. randFloatSpread: randFloatSpread,
  443. seededRandom: seededRandom,
  444. degToRad: degToRad,
  445. radToDeg: radToDeg,
  446. isPowerOfTwo: isPowerOfTwo,
  447. ceilPowerOfTwo: ceilPowerOfTwo,
  448. floorPowerOfTwo: floorPowerOfTwo,
  449. setQuaternionFromProperEuler: setQuaternionFromProperEuler,
  450. normalize: normalize,
  451. denormalize: denormalize
  452. };
  453. class Vector2 {
  454. constructor( x = 0, y = 0 ) {
  455. Vector2.prototype.isVector2 = true;
  456. this.x = x;
  457. this.y = y;
  458. }
  459. get width() {
  460. return this.x;
  461. }
  462. set width( value ) {
  463. this.x = value;
  464. }
  465. get height() {
  466. return this.y;
  467. }
  468. set height( value ) {
  469. this.y = value;
  470. }
  471. set( x, y ) {
  472. this.x = x;
  473. this.y = y;
  474. return this;
  475. }
  476. setScalar( scalar ) {
  477. this.x = scalar;
  478. this.y = scalar;
  479. return this;
  480. }
  481. setX( x ) {
  482. this.x = x;
  483. return this;
  484. }
  485. setY( y ) {
  486. this.y = y;
  487. return this;
  488. }
  489. setComponent( index, value ) {
  490. switch ( index ) {
  491. case 0: this.x = value; break;
  492. case 1: this.y = value; break;
  493. default: throw new Error( 'index is out of range: ' + index );
  494. }
  495. return this;
  496. }
  497. getComponent( index ) {
  498. switch ( index ) {
  499. case 0: return this.x;
  500. case 1: return this.y;
  501. default: throw new Error( 'index is out of range: ' + index );
  502. }
  503. }
  504. clone() {
  505. return new this.constructor( this.x, this.y );
  506. }
  507. copy( v ) {
  508. this.x = v.x;
  509. this.y = v.y;
  510. return this;
  511. }
  512. add( v ) {
  513. this.x += v.x;
  514. this.y += v.y;
  515. return this;
  516. }
  517. addScalar( s ) {
  518. this.x += s;
  519. this.y += s;
  520. return this;
  521. }
  522. addVectors( a, b ) {
  523. this.x = a.x + b.x;
  524. this.y = a.y + b.y;
  525. return this;
  526. }
  527. addScaledVector( v, s ) {
  528. this.x += v.x * s;
  529. this.y += v.y * s;
  530. return this;
  531. }
  532. sub( v ) {
  533. this.x -= v.x;
  534. this.y -= v.y;
  535. return this;
  536. }
  537. subScalar( s ) {
  538. this.x -= s;
  539. this.y -= s;
  540. return this;
  541. }
  542. subVectors( a, b ) {
  543. this.x = a.x - b.x;
  544. this.y = a.y - b.y;
  545. return this;
  546. }
  547. multiply( v ) {
  548. this.x *= v.x;
  549. this.y *= v.y;
  550. return this;
  551. }
  552. multiplyScalar( scalar ) {
  553. this.x *= scalar;
  554. this.y *= scalar;
  555. return this;
  556. }
  557. divide( v ) {
  558. this.x /= v.x;
  559. this.y /= v.y;
  560. return this;
  561. }
  562. divideScalar( scalar ) {
  563. return this.multiplyScalar( 1 / scalar );
  564. }
  565. applyMatrix3( m ) {
  566. const x = this.x, y = this.y;
  567. const e = m.elements;
  568. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ];
  569. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ];
  570. return this;
  571. }
  572. min( v ) {
  573. this.x = Math.min( this.x, v.x );
  574. this.y = Math.min( this.y, v.y );
  575. return this;
  576. }
  577. max( v ) {
  578. this.x = Math.max( this.x, v.x );
  579. this.y = Math.max( this.y, v.y );
  580. return this;
  581. }
  582. clamp( min, max ) {
  583. // assumes min < max, componentwise
  584. this.x = clamp( this.x, min.x, max.x );
  585. this.y = clamp( this.y, min.y, max.y );
  586. return this;
  587. }
  588. clampScalar( minVal, maxVal ) {
  589. this.x = clamp( this.x, minVal, maxVal );
  590. this.y = clamp( this.y, minVal, maxVal );
  591. return this;
  592. }
  593. clampLength( min, max ) {
  594. const length = this.length();
  595. return this.divideScalar( length || 1 ).multiplyScalar( clamp( length, min, max ) );
  596. }
  597. floor() {
  598. this.x = Math.floor( this.x );
  599. this.y = Math.floor( this.y );
  600. return this;
  601. }
  602. ceil() {
  603. this.x = Math.ceil( this.x );
  604. this.y = Math.ceil( this.y );
  605. return this;
  606. }
  607. round() {
  608. this.x = Math.round( this.x );
  609. this.y = Math.round( this.y );
  610. return this;
  611. }
  612. roundToZero() {
  613. this.x = Math.trunc( this.x );
  614. this.y = Math.trunc( this.y );
  615. return this;
  616. }
  617. negate() {
  618. this.x = - this.x;
  619. this.y = - this.y;
  620. return this;
  621. }
  622. dot( v ) {
  623. return this.x * v.x + this.y * v.y;
  624. }
  625. cross( v ) {
  626. return this.x * v.y - this.y * v.x;
  627. }
  628. lengthSq() {
  629. return this.x * this.x + this.y * this.y;
  630. }
  631. length() {
  632. return Math.sqrt( this.x * this.x + this.y * this.y );
  633. }
  634. manhattanLength() {
  635. return Math.abs( this.x ) + Math.abs( this.y );
  636. }
  637. normalize() {
  638. return this.divideScalar( this.length() || 1 );
  639. }
  640. angle() {
  641. // computes the angle in radians with respect to the positive x-axis
  642. const angle = Math.atan2( - this.y, - this.x ) + Math.PI;
  643. return angle;
  644. }
  645. angleTo( v ) {
  646. const denominator = Math.sqrt( this.lengthSq() * v.lengthSq() );
  647. if ( denominator === 0 ) return Math.PI / 2;
  648. const theta = this.dot( v ) / denominator;
  649. // clamp, to handle numerical problems
  650. return Math.acos( clamp( theta, - 1, 1 ) );
  651. }
  652. distanceTo( v ) {
  653. return Math.sqrt( this.distanceToSquared( v ) );
  654. }
  655. distanceToSquared( v ) {
  656. const dx = this.x - v.x, dy = this.y - v.y;
  657. return dx * dx + dy * dy;
  658. }
  659. manhattanDistanceTo( v ) {
  660. return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y );
  661. }
  662. setLength( length ) {
  663. return this.normalize().multiplyScalar( length );
  664. }
  665. lerp( v, alpha ) {
  666. this.x += ( v.x - this.x ) * alpha;
  667. this.y += ( v.y - this.y ) * alpha;
  668. return this;
  669. }
  670. lerpVectors( v1, v2, alpha ) {
  671. this.x = v1.x + ( v2.x - v1.x ) * alpha;
  672. this.y = v1.y + ( v2.y - v1.y ) * alpha;
  673. return this;
  674. }
  675. equals( v ) {
  676. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  677. }
  678. fromArray( array, offset = 0 ) {
  679. this.x = array[ offset ];
  680. this.y = array[ offset + 1 ];
  681. return this;
  682. }
  683. toArray( array = [], offset = 0 ) {
  684. array[ offset ] = this.x;
  685. array[ offset + 1 ] = this.y;
  686. return array;
  687. }
  688. fromBufferAttribute( attribute, index ) {
  689. this.x = attribute.getX( index );
  690. this.y = attribute.getY( index );
  691. return this;
  692. }
  693. rotateAround( center, angle ) {
  694. const c = Math.cos( angle ), s = Math.sin( angle );
  695. const x = this.x - center.x;
  696. const y = this.y - center.y;
  697. this.x = x * c - y * s + center.x;
  698. this.y = x * s + y * c + center.y;
  699. return this;
  700. }
  701. random() {
  702. this.x = Math.random();
  703. this.y = Math.random();
  704. return this;
  705. }
  706. *[ Symbol.iterator ]() {
  707. yield this.x;
  708. yield this.y;
  709. }
  710. }
  711. class Matrix3 {
  712. constructor( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) {
  713. Matrix3.prototype.isMatrix3 = true;
  714. this.elements = [
  715. 1, 0, 0,
  716. 0, 1, 0,
  717. 0, 0, 1
  718. ];
  719. if ( n11 !== undefined ) {
  720. this.set( n11, n12, n13, n21, n22, n23, n31, n32, n33 );
  721. }
  722. }
  723. set( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) {
  724. const te = this.elements;
  725. te[ 0 ] = n11; te[ 1 ] = n21; te[ 2 ] = n31;
  726. te[ 3 ] = n12; te[ 4 ] = n22; te[ 5 ] = n32;
  727. te[ 6 ] = n13; te[ 7 ] = n23; te[ 8 ] = n33;
  728. return this;
  729. }
  730. identity() {
  731. this.set(
  732. 1, 0, 0,
  733. 0, 1, 0,
  734. 0, 0, 1
  735. );
  736. return this;
  737. }
  738. copy( m ) {
  739. const te = this.elements;
  740. const me = m.elements;
  741. te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ];
  742. te[ 3 ] = me[ 3 ]; te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ];
  743. te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ]; te[ 8 ] = me[ 8 ];
  744. return this;
  745. }
  746. extractBasis( xAxis, yAxis, zAxis ) {
  747. xAxis.setFromMatrix3Column( this, 0 );
  748. yAxis.setFromMatrix3Column( this, 1 );
  749. zAxis.setFromMatrix3Column( this, 2 );
  750. return this;
  751. }
  752. setFromMatrix4( m ) {
  753. const me = m.elements;
  754. this.set(
  755. me[ 0 ], me[ 4 ], me[ 8 ],
  756. me[ 1 ], me[ 5 ], me[ 9 ],
  757. me[ 2 ], me[ 6 ], me[ 10 ]
  758. );
  759. return this;
  760. }
  761. multiply( m ) {
  762. return this.multiplyMatrices( this, m );
  763. }
  764. premultiply( m ) {
  765. return this.multiplyMatrices( m, this );
  766. }
  767. multiplyMatrices( a, b ) {
  768. const ae = a.elements;
  769. const be = b.elements;
  770. const te = this.elements;
  771. const a11 = ae[ 0 ], a12 = ae[ 3 ], a13 = ae[ 6 ];
  772. const a21 = ae[ 1 ], a22 = ae[ 4 ], a23 = ae[ 7 ];
  773. const a31 = ae[ 2 ], a32 = ae[ 5 ], a33 = ae[ 8 ];
  774. const b11 = be[ 0 ], b12 = be[ 3 ], b13 = be[ 6 ];
  775. const b21 = be[ 1 ], b22 = be[ 4 ], b23 = be[ 7 ];
  776. const b31 = be[ 2 ], b32 = be[ 5 ], b33 = be[ 8 ];
  777. te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31;
  778. te[ 3 ] = a11 * b12 + a12 * b22 + a13 * b32;
  779. te[ 6 ] = a11 * b13 + a12 * b23 + a13 * b33;
  780. te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31;
  781. te[ 4 ] = a21 * b12 + a22 * b22 + a23 * b32;
  782. te[ 7 ] = a21 * b13 + a22 * b23 + a23 * b33;
  783. te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31;
  784. te[ 5 ] = a31 * b12 + a32 * b22 + a33 * b32;
  785. te[ 8 ] = a31 * b13 + a32 * b23 + a33 * b33;
  786. return this;
  787. }
  788. multiplyScalar( s ) {
  789. const te = this.elements;
  790. te[ 0 ] *= s; te[ 3 ] *= s; te[ 6 ] *= s;
  791. te[ 1 ] *= s; te[ 4 ] *= s; te[ 7 ] *= s;
  792. te[ 2 ] *= s; te[ 5 ] *= s; te[ 8 ] *= s;
  793. return this;
  794. }
  795. determinant() {
  796. const te = this.elements;
  797. const a = te[ 0 ], b = te[ 1 ], c = te[ 2 ],
  798. d = te[ 3 ], e = te[ 4 ], f = te[ 5 ],
  799. g = te[ 6 ], h = te[ 7 ], i = te[ 8 ];
  800. return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
  801. }
  802. invert() {
  803. const te = this.elements,
  804. n11 = te[ 0 ], n21 = te[ 1 ], n31 = te[ 2 ],
  805. n12 = te[ 3 ], n22 = te[ 4 ], n32 = te[ 5 ],
  806. n13 = te[ 6 ], n23 = te[ 7 ], n33 = te[ 8 ],
  807. t11 = n33 * n22 - n32 * n23,
  808. t12 = n32 * n13 - n33 * n12,
  809. t13 = n23 * n12 - n22 * n13,
  810. det = n11 * t11 + n21 * t12 + n31 * t13;
  811. if ( det === 0 ) return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  812. const detInv = 1 / det;
  813. te[ 0 ] = t11 * detInv;
  814. te[ 1 ] = ( n31 * n23 - n33 * n21 ) * detInv;
  815. te[ 2 ] = ( n32 * n21 - n31 * n22 ) * detInv;
  816. te[ 3 ] = t12 * detInv;
  817. te[ 4 ] = ( n33 * n11 - n31 * n13 ) * detInv;
  818. te[ 5 ] = ( n31 * n12 - n32 * n11 ) * detInv;
  819. te[ 6 ] = t13 * detInv;
  820. te[ 7 ] = ( n21 * n13 - n23 * n11 ) * detInv;
  821. te[ 8 ] = ( n22 * n11 - n21 * n12 ) * detInv;
  822. return this;
  823. }
  824. transpose() {
  825. let tmp;
  826. const m = this.elements;
  827. tmp = m[ 1 ]; m[ 1 ] = m[ 3 ]; m[ 3 ] = tmp;
  828. tmp = m[ 2 ]; m[ 2 ] = m[ 6 ]; m[ 6 ] = tmp;
  829. tmp = m[ 5 ]; m[ 5 ] = m[ 7 ]; m[ 7 ] = tmp;
  830. return this;
  831. }
  832. getNormalMatrix( matrix4 ) {
  833. return this.setFromMatrix4( matrix4 ).invert().transpose();
  834. }
  835. transposeIntoArray( r ) {
  836. const m = this.elements;
  837. r[ 0 ] = m[ 0 ];
  838. r[ 1 ] = m[ 3 ];
  839. r[ 2 ] = m[ 6 ];
  840. r[ 3 ] = m[ 1 ];
  841. r[ 4 ] = m[ 4 ];
  842. r[ 5 ] = m[ 7 ];
  843. r[ 6 ] = m[ 2 ];
  844. r[ 7 ] = m[ 5 ];
  845. r[ 8 ] = m[ 8 ];
  846. return this;
  847. }
  848. setUvTransform( tx, ty, sx, sy, rotation, cx, cy ) {
  849. const c = Math.cos( rotation );
  850. const s = Math.sin( rotation );
  851. this.set(
  852. sx * c, sx * s, - sx * ( c * cx + s * cy ) + cx + tx,
  853. - sy * s, sy * c, - sy * ( - s * cx + c * cy ) + cy + ty,
  854. 0, 0, 1
  855. );
  856. return this;
  857. }
  858. //
  859. scale( sx, sy ) {
  860. this.premultiply( _m3.makeScale( sx, sy ) );
  861. return this;
  862. }
  863. rotate( theta ) {
  864. this.premultiply( _m3.makeRotation( - theta ) );
  865. return this;
  866. }
  867. translate( tx, ty ) {
  868. this.premultiply( _m3.makeTranslation( tx, ty ) );
  869. return this;
  870. }
  871. // for 2D Transforms
  872. makeTranslation( x, y ) {
  873. if ( x.isVector2 ) {
  874. this.set(
  875. 1, 0, x.x,
  876. 0, 1, x.y,
  877. 0, 0, 1
  878. );
  879. } else {
  880. this.set(
  881. 1, 0, x,
  882. 0, 1, y,
  883. 0, 0, 1
  884. );
  885. }
  886. return this;
  887. }
  888. makeRotation( theta ) {
  889. // counterclockwise
  890. const c = Math.cos( theta );
  891. const s = Math.sin( theta );
  892. this.set(
  893. c, - s, 0,
  894. s, c, 0,
  895. 0, 0, 1
  896. );
  897. return this;
  898. }
  899. makeScale( x, y ) {
  900. this.set(
  901. x, 0, 0,
  902. 0, y, 0,
  903. 0, 0, 1
  904. );
  905. return this;
  906. }
  907. //
  908. equals( matrix ) {
  909. const te = this.elements;
  910. const me = matrix.elements;
  911. for ( let i = 0; i < 9; i ++ ) {
  912. if ( te[ i ] !== me[ i ] ) return false;
  913. }
  914. return true;
  915. }
  916. fromArray( array, offset = 0 ) {
  917. for ( let i = 0; i < 9; i ++ ) {
  918. this.elements[ i ] = array[ i + offset ];
  919. }
  920. return this;
  921. }
  922. toArray( array = [], offset = 0 ) {
  923. const te = this.elements;
  924. array[ offset ] = te[ 0 ];
  925. array[ offset + 1 ] = te[ 1 ];
  926. array[ offset + 2 ] = te[ 2 ];
  927. array[ offset + 3 ] = te[ 3 ];
  928. array[ offset + 4 ] = te[ 4 ];
  929. array[ offset + 5 ] = te[ 5 ];
  930. array[ offset + 6 ] = te[ 6 ];
  931. array[ offset + 7 ] = te[ 7 ];
  932. array[ offset + 8 ] = te[ 8 ];
  933. return array;
  934. }
  935. clone() {
  936. return new this.constructor().fromArray( this.elements );
  937. }
  938. }
  939. const _m3 = /*@__PURE__*/ new Matrix3();
  940. function arrayNeedsUint32( array ) {
  941. // assumes larger values usually on last
  942. for ( let i = array.length - 1; i >= 0; -- i ) {
  943. if ( array[ i ] >= 65535 ) return true; // account for PRIMITIVE_RESTART_FIXED_INDEX, #24565
  944. }
  945. return false;
  946. }
  947. const TYPED_ARRAYS = {
  948. Int8Array: Int8Array,
  949. Uint8Array: Uint8Array,
  950. Uint8ClampedArray: Uint8ClampedArray,
  951. Int16Array: Int16Array,
  952. Uint16Array: Uint16Array,
  953. Int32Array: Int32Array,
  954. Uint32Array: Uint32Array,
  955. Float32Array: Float32Array,
  956. Float64Array: Float64Array
  957. };
  958. function getTypedArray( type, buffer ) {
  959. return new TYPED_ARRAYS[ type ]( buffer );
  960. }
  961. function createElementNS( name ) {
  962. return document.createElementNS( 'http://www.w3.org/1999/xhtml', name );
  963. }
  964. function createCanvasElement() {
  965. const canvas = createElementNS( 'canvas' );
  966. canvas.style.display = 'block';
  967. return canvas;
  968. }
  969. const _cache = {};
  970. function warnOnce( message ) {
  971. if ( message in _cache ) return;
  972. _cache[ message ] = true;
  973. console.warn( message );
  974. }
  975. function probeAsync( gl, sync, interval ) {
  976. return new Promise( function ( resolve, reject ) {
  977. function probe() {
  978. switch ( gl.clientWaitSync( sync, gl.SYNC_FLUSH_COMMANDS_BIT, 0 ) ) {
  979. case gl.WAIT_FAILED:
  980. reject();
  981. break;
  982. case gl.TIMEOUT_EXPIRED:
  983. setTimeout( probe, interval );
  984. break;
  985. default:
  986. resolve();
  987. }
  988. }
  989. setTimeout( probe, interval );
  990. } );
  991. }
  992. function toNormalizedProjectionMatrix( projectionMatrix ) {
  993. const m = projectionMatrix.elements;
  994. // Convert [-1, 1] to [0, 1] projection matrix
  995. m[ 2 ] = 0.5 * m[ 2 ] + 0.5 * m[ 3 ];
  996. m[ 6 ] = 0.5 * m[ 6 ] + 0.5 * m[ 7 ];
  997. m[ 10 ] = 0.5 * m[ 10 ] + 0.5 * m[ 11 ];
  998. m[ 14 ] = 0.5 * m[ 14 ] + 0.5 * m[ 15 ];
  999. }
  1000. function toReversedProjectionMatrix( projectionMatrix ) {
  1001. const m = projectionMatrix.elements;
  1002. const isPerspectiveMatrix = m[ 11 ] === - 1;
  1003. // Reverse [0, 1] projection matrix
  1004. if ( isPerspectiveMatrix ) {
  1005. m[ 10 ] = - m[ 10 ] - 1;
  1006. m[ 14 ] = - m[ 14 ];
  1007. } else {
  1008. m[ 10 ] = - m[ 10 ];
  1009. m[ 14 ] = - m[ 14 ] + 1;
  1010. }
  1011. }
  1012. const LINEAR_REC709_TO_XYZ = /*@__PURE__*/ new Matrix3().set(
  1013. 0.4123908, 0.3575843, 0.1804808,
  1014. 0.2126390, 0.7151687, 0.0721923,
  1015. 0.0193308, 0.1191948, 0.9505322
  1016. );
  1017. const XYZ_TO_LINEAR_REC709 = /*@__PURE__*/ new Matrix3().set(
  1018. 3.2409699, - 1.5373832, - 0.4986108,
  1019. - 0.9692436, 1.8759675, 0.0415551,
  1020. 0.0556301, - 0.2039770, 1.0569715
  1021. );
  1022. function createColorManagement() {
  1023. const ColorManagement = {
  1024. enabled: true,
  1025. workingColorSpace: LinearSRGBColorSpace,
  1026. /**
  1027. * Implementations of supported color spaces.
  1028. *
  1029. * Required:
  1030. * - primaries: chromaticity coordinates [ rx ry gx gy bx by ]
  1031. * - whitePoint: reference white [ x y ]
  1032. * - transfer: transfer function (pre-defined)
  1033. * - toXYZ: Matrix3 RGB to XYZ transform
  1034. * - fromXYZ: Matrix3 XYZ to RGB transform
  1035. * - luminanceCoefficients: RGB luminance coefficients
  1036. *
  1037. * Optional:
  1038. * - outputColorSpaceConfig: { drawingBufferColorSpace: ColorSpace }
  1039. * - workingColorSpaceConfig: { unpackColorSpace: ColorSpace }
  1040. *
  1041. * Reference:
  1042. * - https://www.russellcottrell.com/photo/matrixCalculator.htm
  1043. */
  1044. spaces: {},
  1045. convert: function ( color, sourceColorSpace, targetColorSpace ) {
  1046. if ( this.enabled === false || sourceColorSpace === targetColorSpace || ! sourceColorSpace || ! targetColorSpace ) {
  1047. return color;
  1048. }
  1049. if ( this.spaces[ sourceColorSpace ].transfer === SRGBTransfer ) {
  1050. color.r = SRGBToLinear( color.r );
  1051. color.g = SRGBToLinear( color.g );
  1052. color.b = SRGBToLinear( color.b );
  1053. }
  1054. if ( this.spaces[ sourceColorSpace ].primaries !== this.spaces[ targetColorSpace ].primaries ) {
  1055. color.applyMatrix3( this.spaces[ sourceColorSpace ].toXYZ );
  1056. color.applyMatrix3( this.spaces[ targetColorSpace ].fromXYZ );
  1057. }
  1058. if ( this.spaces[ targetColorSpace ].transfer === SRGBTransfer ) {
  1059. color.r = LinearToSRGB( color.r );
  1060. color.g = LinearToSRGB( color.g );
  1061. color.b = LinearToSRGB( color.b );
  1062. }
  1063. return color;
  1064. },
  1065. fromWorkingColorSpace: function ( color, targetColorSpace ) {
  1066. return this.convert( color, this.workingColorSpace, targetColorSpace );
  1067. },
  1068. toWorkingColorSpace: function ( color, sourceColorSpace ) {
  1069. return this.convert( color, sourceColorSpace, this.workingColorSpace );
  1070. },
  1071. getPrimaries: function ( colorSpace ) {
  1072. return this.spaces[ colorSpace ].primaries;
  1073. },
  1074. getTransfer: function ( colorSpace ) {
  1075. if ( colorSpace === NoColorSpace ) return LinearTransfer;
  1076. return this.spaces[ colorSpace ].transfer;
  1077. },
  1078. getLuminanceCoefficients: function ( target, colorSpace = this.workingColorSpace ) {
  1079. return target.fromArray( this.spaces[ colorSpace ].luminanceCoefficients );
  1080. },
  1081. define: function ( colorSpaces ) {
  1082. Object.assign( this.spaces, colorSpaces );
  1083. },
  1084. // Internal APIs
  1085. _getMatrix: function ( targetMatrix, sourceColorSpace, targetColorSpace ) {
  1086. return targetMatrix
  1087. .copy( this.spaces[ sourceColorSpace ].toXYZ )
  1088. .multiply( this.spaces[ targetColorSpace ].fromXYZ );
  1089. },
  1090. _getDrawingBufferColorSpace: function ( colorSpace ) {
  1091. return this.spaces[ colorSpace ].outputColorSpaceConfig.drawingBufferColorSpace;
  1092. },
  1093. _getUnpackColorSpace: function ( colorSpace = this.workingColorSpace ) {
  1094. return this.spaces[ colorSpace ].workingColorSpaceConfig.unpackColorSpace;
  1095. }
  1096. };
  1097. /******************************************************************************
  1098. * sRGB definitions
  1099. */
  1100. const REC709_PRIMARIES = [ 0.640, 0.330, 0.300, 0.600, 0.150, 0.060 ];
  1101. const REC709_LUMINANCE_COEFFICIENTS = [ 0.2126, 0.7152, 0.0722 ];
  1102. const D65 = [ 0.3127, 0.3290 ];
  1103. ColorManagement.define( {
  1104. [ LinearSRGBColorSpace ]: {
  1105. primaries: REC709_PRIMARIES,
  1106. whitePoint: D65,
  1107. transfer: LinearTransfer,
  1108. toXYZ: LINEAR_REC709_TO_XYZ,
  1109. fromXYZ: XYZ_TO_LINEAR_REC709,
  1110. luminanceCoefficients: REC709_LUMINANCE_COEFFICIENTS,
  1111. workingColorSpaceConfig: { unpackColorSpace: SRGBColorSpace },
  1112. outputColorSpaceConfig: { drawingBufferColorSpace: SRGBColorSpace }
  1113. },
  1114. [ SRGBColorSpace ]: {
  1115. primaries: REC709_PRIMARIES,
  1116. whitePoint: D65,
  1117. transfer: SRGBTransfer,
  1118. toXYZ: LINEAR_REC709_TO_XYZ,
  1119. fromXYZ: XYZ_TO_LINEAR_REC709,
  1120. luminanceCoefficients: REC709_LUMINANCE_COEFFICIENTS,
  1121. outputColorSpaceConfig: { drawingBufferColorSpace: SRGBColorSpace }
  1122. },
  1123. } );
  1124. return ColorManagement;
  1125. }
  1126. const ColorManagement = /*@__PURE__*/ createColorManagement();
  1127. function SRGBToLinear( c ) {
  1128. return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 );
  1129. }
  1130. function LinearToSRGB( c ) {
  1131. return ( c < 0.0031308 ) ? c * 12.92 : 1.055 * ( Math.pow( c, 0.41666 ) ) - 0.055;
  1132. }
  1133. let _canvas;
  1134. class ImageUtils {
  1135. static getDataURL( image ) {
  1136. if ( /^data:/i.test( image.src ) ) {
  1137. return image.src;
  1138. }
  1139. if ( typeof HTMLCanvasElement === 'undefined' ) {
  1140. return image.src;
  1141. }
  1142. let canvas;
  1143. if ( image instanceof HTMLCanvasElement ) {
  1144. canvas = image;
  1145. } else {
  1146. if ( _canvas === undefined ) _canvas = createElementNS( 'canvas' );
  1147. _canvas.width = image.width;
  1148. _canvas.height = image.height;
  1149. const context = _canvas.getContext( '2d' );
  1150. if ( image instanceof ImageData ) {
  1151. context.putImageData( image, 0, 0 );
  1152. } else {
  1153. context.drawImage( image, 0, 0, image.width, image.height );
  1154. }
  1155. canvas = _canvas;
  1156. }
  1157. if ( canvas.width > 2048 || canvas.height > 2048 ) {
  1158. console.warn( 'THREE.ImageUtils.getDataURL: Image converted to jpg for performance reasons', image );
  1159. return canvas.toDataURL( 'image/jpeg', 0.6 );
  1160. } else {
  1161. return canvas.toDataURL( 'image/png' );
  1162. }
  1163. }
  1164. static sRGBToLinear( image ) {
  1165. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  1166. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
  1167. ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
  1168. const canvas = createElementNS( 'canvas' );
  1169. canvas.width = image.width;
  1170. canvas.height = image.height;
  1171. const context = canvas.getContext( '2d' );
  1172. context.drawImage( image, 0, 0, image.width, image.height );
  1173. const imageData = context.getImageData( 0, 0, image.width, image.height );
  1174. const data = imageData.data;
  1175. for ( let i = 0; i < data.length; i ++ ) {
  1176. data[ i ] = SRGBToLinear( data[ i ] / 255 ) * 255;
  1177. }
  1178. context.putImageData( imageData, 0, 0 );
  1179. return canvas;
  1180. } else if ( image.data ) {
  1181. const data = image.data.slice( 0 );
  1182. for ( let i = 0; i < data.length; i ++ ) {
  1183. if ( data instanceof Uint8Array || data instanceof Uint8ClampedArray ) {
  1184. data[ i ] = Math.floor( SRGBToLinear( data[ i ] / 255 ) * 255 );
  1185. } else {
  1186. // assuming float
  1187. data[ i ] = SRGBToLinear( data[ i ] );
  1188. }
  1189. }
  1190. return {
  1191. data: data,
  1192. width: image.width,
  1193. height: image.height
  1194. };
  1195. } else {
  1196. console.warn( 'THREE.ImageUtils.sRGBToLinear(): Unsupported image type. No color space conversion applied.' );
  1197. return image;
  1198. }
  1199. }
  1200. }
  1201. let _sourceId = 0;
  1202. class Source {
  1203. constructor( data = null ) {
  1204. this.isSource = true;
  1205. Object.defineProperty( this, 'id', { value: _sourceId ++ } );
  1206. this.uuid = generateUUID();
  1207. this.data = data;
  1208. this.dataReady = true;
  1209. this.version = 0;
  1210. }
  1211. set needsUpdate( value ) {
  1212. if ( value === true ) this.version ++;
  1213. }
  1214. toJSON( meta ) {
  1215. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  1216. if ( ! isRootObject && meta.images[ this.uuid ] !== undefined ) {
  1217. return meta.images[ this.uuid ];
  1218. }
  1219. const output = {
  1220. uuid: this.uuid,
  1221. url: ''
  1222. };
  1223. const data = this.data;
  1224. if ( data !== null ) {
  1225. let url;
  1226. if ( Array.isArray( data ) ) {
  1227. // cube texture
  1228. url = [];
  1229. for ( let i = 0, l = data.length; i < l; i ++ ) {
  1230. if ( data[ i ].isDataTexture ) {
  1231. url.push( serializeImage( data[ i ].image ) );
  1232. } else {
  1233. url.push( serializeImage( data[ i ] ) );
  1234. }
  1235. }
  1236. } else {
  1237. // texture
  1238. url = serializeImage( data );
  1239. }
  1240. output.url = url;
  1241. }
  1242. if ( ! isRootObject ) {
  1243. meta.images[ this.uuid ] = output;
  1244. }
  1245. return output;
  1246. }
  1247. }
  1248. function serializeImage( image ) {
  1249. if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) ||
  1250. ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) ||
  1251. ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) {
  1252. // default images
  1253. return ImageUtils.getDataURL( image );
  1254. } else {
  1255. if ( image.data ) {
  1256. // images of DataTexture
  1257. return {
  1258. data: Array.from( image.data ),
  1259. width: image.width,
  1260. height: image.height,
  1261. type: image.data.constructor.name
  1262. };
  1263. } else {
  1264. console.warn( 'THREE.Texture: Unable to serialize Texture.' );
  1265. return {};
  1266. }
  1267. }
  1268. }
  1269. let _textureId = 0;
  1270. class Texture extends EventDispatcher {
  1271. constructor( image = Texture.DEFAULT_IMAGE, mapping = Texture.DEFAULT_MAPPING, wrapS = ClampToEdgeWrapping, wrapT = ClampToEdgeWrapping, magFilter = LinearFilter, minFilter = LinearMipmapLinearFilter, format = RGBAFormat, type = UnsignedByteType, anisotropy = Texture.DEFAULT_ANISOTROPY, colorSpace = NoColorSpace ) {
  1272. super();
  1273. this.isTexture = true;
  1274. Object.defineProperty( this, 'id', { value: _textureId ++ } );
  1275. this.uuid = generateUUID();
  1276. this.name = '';
  1277. this.source = new Source( image );
  1278. this.mipmaps = [];
  1279. this.mapping = mapping;
  1280. this.channel = 0;
  1281. this.wrapS = wrapS;
  1282. this.wrapT = wrapT;
  1283. this.magFilter = magFilter;
  1284. this.minFilter = minFilter;
  1285. this.anisotropy = anisotropy;
  1286. this.format = format;
  1287. this.internalFormat = null;
  1288. this.type = type;
  1289. this.offset = new Vector2( 0, 0 );
  1290. this.repeat = new Vector2( 1, 1 );
  1291. this.center = new Vector2( 0, 0 );
  1292. this.rotation = 0;
  1293. this.matrixAutoUpdate = true;
  1294. this.matrix = new Matrix3();
  1295. this.generateMipmaps = true;
  1296. this.premultiplyAlpha = false;
  1297. this.flipY = true;
  1298. this.unpackAlignment = 4; // valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
  1299. this.colorSpace = colorSpace;
  1300. this.userData = {};
  1301. this.version = 0;
  1302. this.onUpdate = null;
  1303. this.renderTarget = null; // assign texture to a render target
  1304. this.isRenderTargetTexture = false; // indicates whether a texture belongs to a render target or not
  1305. this.pmremVersion = 0; // indicates whether this texture should be processed by PMREMGenerator or not (only relevant for render target textures)
  1306. }
  1307. get image() {
  1308. return this.source.data;
  1309. }
  1310. set image( value = null ) {
  1311. this.source.data = value;
  1312. }
  1313. updateMatrix() {
  1314. this.matrix.setUvTransform( this.offset.x, this.offset.y, this.repeat.x, this.repeat.y, this.rotation, this.center.x, this.center.y );
  1315. }
  1316. clone() {
  1317. return new this.constructor().copy( this );
  1318. }
  1319. copy( source ) {
  1320. this.name = source.name;
  1321. this.source = source.source;
  1322. this.mipmaps = source.mipmaps.slice( 0 );
  1323. this.mapping = source.mapping;
  1324. this.channel = source.channel;
  1325. this.wrapS = source.wrapS;
  1326. this.wrapT = source.wrapT;
  1327. this.magFilter = source.magFilter;
  1328. this.minFilter = source.minFilter;
  1329. this.anisotropy = source.anisotropy;
  1330. this.format = source.format;
  1331. this.internalFormat = source.internalFormat;
  1332. this.type = source.type;
  1333. this.offset.copy( source.offset );
  1334. this.repeat.copy( source.repeat );
  1335. this.center.copy( source.center );
  1336. this.rotation = source.rotation;
  1337. this.matrixAutoUpdate = source.matrixAutoUpdate;
  1338. this.matrix.copy( source.matrix );
  1339. this.generateMipmaps = source.generateMipmaps;
  1340. this.premultiplyAlpha = source.premultiplyAlpha;
  1341. this.flipY = source.flipY;
  1342. this.unpackAlignment = source.unpackAlignment;
  1343. this.colorSpace = source.colorSpace;
  1344. this.renderTarget = source.renderTarget;
  1345. this.isRenderTargetTexture = source.isRenderTargetTexture;
  1346. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  1347. this.needsUpdate = true;
  1348. return this;
  1349. }
  1350. toJSON( meta ) {
  1351. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  1352. if ( ! isRootObject && meta.textures[ this.uuid ] !== undefined ) {
  1353. return meta.textures[ this.uuid ];
  1354. }
  1355. const output = {
  1356. metadata: {
  1357. version: 4.6,
  1358. type: 'Texture',
  1359. generator: 'Texture.toJSON'
  1360. },
  1361. uuid: this.uuid,
  1362. name: this.name,
  1363. image: this.source.toJSON( meta ).uuid,
  1364. mapping: this.mapping,
  1365. channel: this.channel,
  1366. repeat: [ this.repeat.x, this.repeat.y ],
  1367. offset: [ this.offset.x, this.offset.y ],
  1368. center: [ this.center.x, this.center.y ],
  1369. rotation: this.rotation,
  1370. wrap: [ this.wrapS, this.wrapT ],
  1371. format: this.format,
  1372. internalFormat: this.internalFormat,
  1373. type: this.type,
  1374. colorSpace: this.colorSpace,
  1375. minFilter: this.minFilter,
  1376. magFilter: this.magFilter,
  1377. anisotropy: this.anisotropy,
  1378. flipY: this.flipY,
  1379. generateMipmaps: this.generateMipmaps,
  1380. premultiplyAlpha: this.premultiplyAlpha,
  1381. unpackAlignment: this.unpackAlignment
  1382. };
  1383. if ( Object.keys( this.userData ).length > 0 ) output.userData = this.userData;
  1384. if ( ! isRootObject ) {
  1385. meta.textures[ this.uuid ] = output;
  1386. }
  1387. return output;
  1388. }
  1389. dispose() {
  1390. this.dispatchEvent( { type: 'dispose' } );
  1391. }
  1392. transformUv( uv ) {
  1393. if ( this.mapping !== UVMapping ) return uv;
  1394. uv.applyMatrix3( this.matrix );
  1395. if ( uv.x < 0 || uv.x > 1 ) {
  1396. switch ( this.wrapS ) {
  1397. case RepeatWrapping:
  1398. uv.x = uv.x - Math.floor( uv.x );
  1399. break;
  1400. case ClampToEdgeWrapping:
  1401. uv.x = uv.x < 0 ? 0 : 1;
  1402. break;
  1403. case MirroredRepeatWrapping:
  1404. if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) {
  1405. uv.x = Math.ceil( uv.x ) - uv.x;
  1406. } else {
  1407. uv.x = uv.x - Math.floor( uv.x );
  1408. }
  1409. break;
  1410. }
  1411. }
  1412. if ( uv.y < 0 || uv.y > 1 ) {
  1413. switch ( this.wrapT ) {
  1414. case RepeatWrapping:
  1415. uv.y = uv.y - Math.floor( uv.y );
  1416. break;
  1417. case ClampToEdgeWrapping:
  1418. uv.y = uv.y < 0 ? 0 : 1;
  1419. break;
  1420. case MirroredRepeatWrapping:
  1421. if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) {
  1422. uv.y = Math.ceil( uv.y ) - uv.y;
  1423. } else {
  1424. uv.y = uv.y - Math.floor( uv.y );
  1425. }
  1426. break;
  1427. }
  1428. }
  1429. if ( this.flipY ) {
  1430. uv.y = 1 - uv.y;
  1431. }
  1432. return uv;
  1433. }
  1434. set needsUpdate( value ) {
  1435. if ( value === true ) {
  1436. this.version ++;
  1437. this.source.needsUpdate = true;
  1438. }
  1439. }
  1440. set needsPMREMUpdate( value ) {
  1441. if ( value === true ) {
  1442. this.pmremVersion ++;
  1443. }
  1444. }
  1445. }
  1446. Texture.DEFAULT_IMAGE = null;
  1447. Texture.DEFAULT_MAPPING = UVMapping;
  1448. Texture.DEFAULT_ANISOTROPY = 1;
  1449. class Vector4 {
  1450. constructor( x = 0, y = 0, z = 0, w = 1 ) {
  1451. Vector4.prototype.isVector4 = true;
  1452. this.x = x;
  1453. this.y = y;
  1454. this.z = z;
  1455. this.w = w;
  1456. }
  1457. get width() {
  1458. return this.z;
  1459. }
  1460. set width( value ) {
  1461. this.z = value;
  1462. }
  1463. get height() {
  1464. return this.w;
  1465. }
  1466. set height( value ) {
  1467. this.w = value;
  1468. }
  1469. set( x, y, z, w ) {
  1470. this.x = x;
  1471. this.y = y;
  1472. this.z = z;
  1473. this.w = w;
  1474. return this;
  1475. }
  1476. setScalar( scalar ) {
  1477. this.x = scalar;
  1478. this.y = scalar;
  1479. this.z = scalar;
  1480. this.w = scalar;
  1481. return this;
  1482. }
  1483. setX( x ) {
  1484. this.x = x;
  1485. return this;
  1486. }
  1487. setY( y ) {
  1488. this.y = y;
  1489. return this;
  1490. }
  1491. setZ( z ) {
  1492. this.z = z;
  1493. return this;
  1494. }
  1495. setW( w ) {
  1496. this.w = w;
  1497. return this;
  1498. }
  1499. setComponent( index, value ) {
  1500. switch ( index ) {
  1501. case 0: this.x = value; break;
  1502. case 1: this.y = value; break;
  1503. case 2: this.z = value; break;
  1504. case 3: this.w = value; break;
  1505. default: throw new Error( 'index is out of range: ' + index );
  1506. }
  1507. return this;
  1508. }
  1509. getComponent( index ) {
  1510. switch ( index ) {
  1511. case 0: return this.x;
  1512. case 1: return this.y;
  1513. case 2: return this.z;
  1514. case 3: return this.w;
  1515. default: throw new Error( 'index is out of range: ' + index );
  1516. }
  1517. }
  1518. clone() {
  1519. return new this.constructor( this.x, this.y, this.z, this.w );
  1520. }
  1521. copy( v ) {
  1522. this.x = v.x;
  1523. this.y = v.y;
  1524. this.z = v.z;
  1525. this.w = ( v.w !== undefined ) ? v.w : 1;
  1526. return this;
  1527. }
  1528. add( v ) {
  1529. this.x += v.x;
  1530. this.y += v.y;
  1531. this.z += v.z;
  1532. this.w += v.w;
  1533. return this;
  1534. }
  1535. addScalar( s ) {
  1536. this.x += s;
  1537. this.y += s;
  1538. this.z += s;
  1539. this.w += s;
  1540. return this;
  1541. }
  1542. addVectors( a, b ) {
  1543. this.x = a.x + b.x;
  1544. this.y = a.y + b.y;
  1545. this.z = a.z + b.z;
  1546. this.w = a.w + b.w;
  1547. return this;
  1548. }
  1549. addScaledVector( v, s ) {
  1550. this.x += v.x * s;
  1551. this.y += v.y * s;
  1552. this.z += v.z * s;
  1553. this.w += v.w * s;
  1554. return this;
  1555. }
  1556. sub( v ) {
  1557. this.x -= v.x;
  1558. this.y -= v.y;
  1559. this.z -= v.z;
  1560. this.w -= v.w;
  1561. return this;
  1562. }
  1563. subScalar( s ) {
  1564. this.x -= s;
  1565. this.y -= s;
  1566. this.z -= s;
  1567. this.w -= s;
  1568. return this;
  1569. }
  1570. subVectors( a, b ) {
  1571. this.x = a.x - b.x;
  1572. this.y = a.y - b.y;
  1573. this.z = a.z - b.z;
  1574. this.w = a.w - b.w;
  1575. return this;
  1576. }
  1577. multiply( v ) {
  1578. this.x *= v.x;
  1579. this.y *= v.y;
  1580. this.z *= v.z;
  1581. this.w *= v.w;
  1582. return this;
  1583. }
  1584. multiplyScalar( scalar ) {
  1585. this.x *= scalar;
  1586. this.y *= scalar;
  1587. this.z *= scalar;
  1588. this.w *= scalar;
  1589. return this;
  1590. }
  1591. applyMatrix4( m ) {
  1592. const x = this.x, y = this.y, z = this.z, w = this.w;
  1593. const e = m.elements;
  1594. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w;
  1595. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w;
  1596. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w;
  1597. this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
  1598. return this;
  1599. }
  1600. divide( v ) {
  1601. this.x /= v.x;
  1602. this.y /= v.y;
  1603. this.z /= v.z;
  1604. this.w /= v.w;
  1605. return this;
  1606. }
  1607. divideScalar( scalar ) {
  1608. return this.multiplyScalar( 1 / scalar );
  1609. }
  1610. setAxisAngleFromQuaternion( q ) {
  1611. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
  1612. // q is assumed to be normalized
  1613. this.w = 2 * Math.acos( q.w );
  1614. const s = Math.sqrt( 1 - q.w * q.w );
  1615. if ( s < 0.0001 ) {
  1616. this.x = 1;
  1617. this.y = 0;
  1618. this.z = 0;
  1619. } else {
  1620. this.x = q.x / s;
  1621. this.y = q.y / s;
  1622. this.z = q.z / s;
  1623. }
  1624. return this;
  1625. }
  1626. setAxisAngleFromRotationMatrix( m ) {
  1627. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
  1628. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  1629. let angle, x, y, z; // variables for result
  1630. const epsilon = 0.01, // margin to allow for rounding errors
  1631. epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees
  1632. te = m.elements,
  1633. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  1634. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  1635. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  1636. if ( ( Math.abs( m12 - m21 ) < epsilon ) &&
  1637. ( Math.abs( m13 - m31 ) < epsilon ) &&
  1638. ( Math.abs( m23 - m32 ) < epsilon ) ) {
  1639. // singularity found
  1640. // first check for identity matrix which must have +1 for all terms
  1641. // in leading diagonal and zero in other terms
  1642. if ( ( Math.abs( m12 + m21 ) < epsilon2 ) &&
  1643. ( Math.abs( m13 + m31 ) < epsilon2 ) &&
  1644. ( Math.abs( m23 + m32 ) < epsilon2 ) &&
  1645. ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
  1646. // this singularity is identity matrix so angle = 0
  1647. this.set( 1, 0, 0, 0 );
  1648. return this; // zero angle, arbitrary axis
  1649. }
  1650. // otherwise this singularity is angle = 180
  1651. angle = Math.PI;
  1652. const xx = ( m11 + 1 ) / 2;
  1653. const yy = ( m22 + 1 ) / 2;
  1654. const zz = ( m33 + 1 ) / 2;
  1655. const xy = ( m12 + m21 ) / 4;
  1656. const xz = ( m13 + m31 ) / 4;
  1657. const yz = ( m23 + m32 ) / 4;
  1658. if ( ( xx > yy ) && ( xx > zz ) ) {
  1659. // m11 is the largest diagonal term
  1660. if ( xx < epsilon ) {
  1661. x = 0;
  1662. y = 0.707106781;
  1663. z = 0.707106781;
  1664. } else {
  1665. x = Math.sqrt( xx );
  1666. y = xy / x;
  1667. z = xz / x;
  1668. }
  1669. } else if ( yy > zz ) {
  1670. // m22 is the largest diagonal term
  1671. if ( yy < epsilon ) {
  1672. x = 0.707106781;
  1673. y = 0;
  1674. z = 0.707106781;
  1675. } else {
  1676. y = Math.sqrt( yy );
  1677. x = xy / y;
  1678. z = yz / y;
  1679. }
  1680. } else {
  1681. // m33 is the largest diagonal term so base result on this
  1682. if ( zz < epsilon ) {
  1683. x = 0.707106781;
  1684. y = 0.707106781;
  1685. z = 0;
  1686. } else {
  1687. z = Math.sqrt( zz );
  1688. x = xz / z;
  1689. y = yz / z;
  1690. }
  1691. }
  1692. this.set( x, y, z, angle );
  1693. return this; // return 180 deg rotation
  1694. }
  1695. // as we have reached here there are no singularities so we can handle normally
  1696. let s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) +
  1697. ( m13 - m31 ) * ( m13 - m31 ) +
  1698. ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
  1699. if ( Math.abs( s ) < 0.001 ) s = 1;
  1700. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  1701. // caught by singularity test above, but I've left it in just in case
  1702. this.x = ( m32 - m23 ) / s;
  1703. this.y = ( m13 - m31 ) / s;
  1704. this.z = ( m21 - m12 ) / s;
  1705. this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
  1706. return this;
  1707. }
  1708. setFromMatrixPosition( m ) {
  1709. const e = m.elements;
  1710. this.x = e[ 12 ];
  1711. this.y = e[ 13 ];
  1712. this.z = e[ 14 ];
  1713. this.w = e[ 15 ];
  1714. return this;
  1715. }
  1716. min( v ) {
  1717. this.x = Math.min( this.x, v.x );
  1718. this.y = Math.min( this.y, v.y );
  1719. this.z = Math.min( this.z, v.z );
  1720. this.w = Math.min( this.w, v.w );
  1721. return this;
  1722. }
  1723. max( v ) {
  1724. this.x = Math.max( this.x, v.x );
  1725. this.y = Math.max( this.y, v.y );
  1726. this.z = Math.max( this.z, v.z );
  1727. this.w = Math.max( this.w, v.w );
  1728. return this;
  1729. }
  1730. clamp( min, max ) {
  1731. // assumes min < max, componentwise
  1732. this.x = clamp( this.x, min.x, max.x );
  1733. this.y = clamp( this.y, min.y, max.y );
  1734. this.z = clamp( this.z, min.z, max.z );
  1735. this.w = clamp( this.w, min.w, max.w );
  1736. return this;
  1737. }
  1738. clampScalar( minVal, maxVal ) {
  1739. this.x = clamp( this.x, minVal, maxVal );
  1740. this.y = clamp( this.y, minVal, maxVal );
  1741. this.z = clamp( this.z, minVal, maxVal );
  1742. this.w = clamp( this.w, minVal, maxVal );
  1743. return this;
  1744. }
  1745. clampLength( min, max ) {
  1746. const length = this.length();
  1747. return this.divideScalar( length || 1 ).multiplyScalar( clamp( length, min, max ) );
  1748. }
  1749. floor() {
  1750. this.x = Math.floor( this.x );
  1751. this.y = Math.floor( this.y );
  1752. this.z = Math.floor( this.z );
  1753. this.w = Math.floor( this.w );
  1754. return this;
  1755. }
  1756. ceil() {
  1757. this.x = Math.ceil( this.x );
  1758. this.y = Math.ceil( this.y );
  1759. this.z = Math.ceil( this.z );
  1760. this.w = Math.ceil( this.w );
  1761. return this;
  1762. }
  1763. round() {
  1764. this.x = Math.round( this.x );
  1765. this.y = Math.round( this.y );
  1766. this.z = Math.round( this.z );
  1767. this.w = Math.round( this.w );
  1768. return this;
  1769. }
  1770. roundToZero() {
  1771. this.x = Math.trunc( this.x );
  1772. this.y = Math.trunc( this.y );
  1773. this.z = Math.trunc( this.z );
  1774. this.w = Math.trunc( this.w );
  1775. return this;
  1776. }
  1777. negate() {
  1778. this.x = - this.x;
  1779. this.y = - this.y;
  1780. this.z = - this.z;
  1781. this.w = - this.w;
  1782. return this;
  1783. }
  1784. dot( v ) {
  1785. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  1786. }
  1787. lengthSq() {
  1788. return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  1789. }
  1790. length() {
  1791. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  1792. }
  1793. manhattanLength() {
  1794. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
  1795. }
  1796. normalize() {
  1797. return this.divideScalar( this.length() || 1 );
  1798. }
  1799. setLength( length ) {
  1800. return this.normalize().multiplyScalar( length );
  1801. }
  1802. lerp( v, alpha ) {
  1803. this.x += ( v.x - this.x ) * alpha;
  1804. this.y += ( v.y - this.y ) * alpha;
  1805. this.z += ( v.z - this.z ) * alpha;
  1806. this.w += ( v.w - this.w ) * alpha;
  1807. return this;
  1808. }
  1809. lerpVectors( v1, v2, alpha ) {
  1810. this.x = v1.x + ( v2.x - v1.x ) * alpha;
  1811. this.y = v1.y + ( v2.y - v1.y ) * alpha;
  1812. this.z = v1.z + ( v2.z - v1.z ) * alpha;
  1813. this.w = v1.w + ( v2.w - v1.w ) * alpha;
  1814. return this;
  1815. }
  1816. equals( v ) {
  1817. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
  1818. }
  1819. fromArray( array, offset = 0 ) {
  1820. this.x = array[ offset ];
  1821. this.y = array[ offset + 1 ];
  1822. this.z = array[ offset + 2 ];
  1823. this.w = array[ offset + 3 ];
  1824. return this;
  1825. }
  1826. toArray( array = [], offset = 0 ) {
  1827. array[ offset ] = this.x;
  1828. array[ offset + 1 ] = this.y;
  1829. array[ offset + 2 ] = this.z;
  1830. array[ offset + 3 ] = this.w;
  1831. return array;
  1832. }
  1833. fromBufferAttribute( attribute, index ) {
  1834. this.x = attribute.getX( index );
  1835. this.y = attribute.getY( index );
  1836. this.z = attribute.getZ( index );
  1837. this.w = attribute.getW( index );
  1838. return this;
  1839. }
  1840. random() {
  1841. this.x = Math.random();
  1842. this.y = Math.random();
  1843. this.z = Math.random();
  1844. this.w = Math.random();
  1845. return this;
  1846. }
  1847. *[ Symbol.iterator ]() {
  1848. yield this.x;
  1849. yield this.y;
  1850. yield this.z;
  1851. yield this.w;
  1852. }
  1853. }
  1854. /*
  1855. In options, we can specify:
  1856. * Texture parameters for an auto-generated target texture
  1857. * depthBuffer/stencilBuffer: Booleans to indicate if we should generate these buffers
  1858. */
  1859. class RenderTarget extends EventDispatcher {
  1860. constructor( width = 1, height = 1, options = {} ) {
  1861. super();
  1862. this.isRenderTarget = true;
  1863. this.width = width;
  1864. this.height = height;
  1865. this.depth = 1;
  1866. this.scissor = new Vector4( 0, 0, width, height );
  1867. this.scissorTest = false;
  1868. this.viewport = new Vector4( 0, 0, width, height );
  1869. const image = { width: width, height: height, depth: 1 };
  1870. options = Object.assign( {
  1871. generateMipmaps: false,
  1872. internalFormat: null,
  1873. minFilter: LinearFilter,
  1874. depthBuffer: true,
  1875. stencilBuffer: false,
  1876. resolveDepthBuffer: true,
  1877. resolveStencilBuffer: true,
  1878. depthTexture: null,
  1879. samples: 0,
  1880. count: 1
  1881. }, options );
  1882. const texture = new Texture( image, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace );
  1883. texture.flipY = false;
  1884. texture.generateMipmaps = options.generateMipmaps;
  1885. texture.internalFormat = options.internalFormat;
  1886. this.textures = [];
  1887. const count = options.count;
  1888. for ( let i = 0; i < count; i ++ ) {
  1889. this.textures[ i ] = texture.clone();
  1890. this.textures[ i ].isRenderTargetTexture = true;
  1891. this.textures[ i ].renderTarget = this;
  1892. }
  1893. this.depthBuffer = options.depthBuffer;
  1894. this.stencilBuffer = options.stencilBuffer;
  1895. this.resolveDepthBuffer = options.resolveDepthBuffer;
  1896. this.resolveStencilBuffer = options.resolveStencilBuffer;
  1897. this._depthTexture = null;
  1898. this.depthTexture = options.depthTexture;
  1899. this.samples = options.samples;
  1900. }
  1901. get texture() {
  1902. return this.textures[ 0 ];
  1903. }
  1904. set texture( value ) {
  1905. this.textures[ 0 ] = value;
  1906. }
  1907. set depthTexture( current ) {
  1908. if ( this._depthTexture !== null ) this._depthTexture.renderTarget = null;
  1909. if ( current !== null ) current.renderTarget = this;
  1910. this._depthTexture = current;
  1911. }
  1912. get depthTexture() {
  1913. return this._depthTexture;
  1914. }
  1915. setSize( width, height, depth = 1 ) {
  1916. if ( this.width !== width || this.height !== height || this.depth !== depth ) {
  1917. this.width = width;
  1918. this.height = height;
  1919. this.depth = depth;
  1920. for ( let i = 0, il = this.textures.length; i < il; i ++ ) {
  1921. this.textures[ i ].image.width = width;
  1922. this.textures[ i ].image.height = height;
  1923. this.textures[ i ].image.depth = depth;
  1924. }
  1925. this.dispose();
  1926. }
  1927. this.viewport.set( 0, 0, width, height );
  1928. this.scissor.set( 0, 0, width, height );
  1929. }
  1930. clone() {
  1931. return new this.constructor().copy( this );
  1932. }
  1933. copy( source ) {
  1934. this.width = source.width;
  1935. this.height = source.height;
  1936. this.depth = source.depth;
  1937. this.scissor.copy( source.scissor );
  1938. this.scissorTest = source.scissorTest;
  1939. this.viewport.copy( source.viewport );
  1940. this.textures.length = 0;
  1941. for ( let i = 0, il = source.textures.length; i < il; i ++ ) {
  1942. this.textures[ i ] = source.textures[ i ].clone();
  1943. this.textures[ i ].isRenderTargetTexture = true;
  1944. this.textures[ i ].renderTarget = this;
  1945. }
  1946. // ensure image object is not shared, see #20328
  1947. const image = Object.assign( {}, source.texture.image );
  1948. this.texture.source = new Source( image );
  1949. this.depthBuffer = source.depthBuffer;
  1950. this.stencilBuffer = source.stencilBuffer;
  1951. this.resolveDepthBuffer = source.resolveDepthBuffer;
  1952. this.resolveStencilBuffer = source.resolveStencilBuffer;
  1953. if ( source.depthTexture !== null ) this.depthTexture = source.depthTexture.clone();
  1954. this.samples = source.samples;
  1955. return this;
  1956. }
  1957. dispose() {
  1958. this.dispatchEvent( { type: 'dispose' } );
  1959. }
  1960. }
  1961. class WebGLRenderTarget extends RenderTarget {
  1962. constructor( width = 1, height = 1, options = {} ) {
  1963. super( width, height, options );
  1964. this.isWebGLRenderTarget = true;
  1965. }
  1966. }
  1967. class DataArrayTexture extends Texture {
  1968. constructor( data = null, width = 1, height = 1, depth = 1 ) {
  1969. super( null );
  1970. this.isDataArrayTexture = true;
  1971. this.image = { data, width, height, depth };
  1972. this.magFilter = NearestFilter;
  1973. this.minFilter = NearestFilter;
  1974. this.wrapR = ClampToEdgeWrapping;
  1975. this.generateMipmaps = false;
  1976. this.flipY = false;
  1977. this.unpackAlignment = 1;
  1978. this.layerUpdates = new Set();
  1979. }
  1980. addLayerUpdate( layerIndex ) {
  1981. this.layerUpdates.add( layerIndex );
  1982. }
  1983. clearLayerUpdates() {
  1984. this.layerUpdates.clear();
  1985. }
  1986. }
  1987. class WebGLArrayRenderTarget extends WebGLRenderTarget {
  1988. constructor( width = 1, height = 1, depth = 1, options = {} ) {
  1989. super( width, height, options );
  1990. this.isWebGLArrayRenderTarget = true;
  1991. this.depth = depth;
  1992. this.texture = new DataArrayTexture( null, width, height, depth );
  1993. this.texture.isRenderTargetTexture = true;
  1994. }
  1995. }
  1996. class Data3DTexture extends Texture {
  1997. constructor( data = null, width = 1, height = 1, depth = 1 ) {
  1998. // We're going to add .setXXX() methods for setting properties later.
  1999. // Users can still set in Data3DTexture directly.
  2000. //
  2001. // const texture = new THREE.Data3DTexture( data, width, height, depth );
  2002. // texture.anisotropy = 16;
  2003. //
  2004. // See #14839
  2005. super( null );
  2006. this.isData3DTexture = true;
  2007. this.image = { data, width, height, depth };
  2008. this.magFilter = NearestFilter;
  2009. this.minFilter = NearestFilter;
  2010. this.wrapR = ClampToEdgeWrapping;
  2011. this.generateMipmaps = false;
  2012. this.flipY = false;
  2013. this.unpackAlignment = 1;
  2014. }
  2015. }
  2016. class WebGL3DRenderTarget extends WebGLRenderTarget {
  2017. constructor( width = 1, height = 1, depth = 1, options = {} ) {
  2018. super( width, height, options );
  2019. this.isWebGL3DRenderTarget = true;
  2020. this.depth = depth;
  2021. this.texture = new Data3DTexture( null, width, height, depth );
  2022. this.texture.isRenderTargetTexture = true;
  2023. }
  2024. }
  2025. class Quaternion {
  2026. constructor( x = 0, y = 0, z = 0, w = 1 ) {
  2027. this.isQuaternion = true;
  2028. this._x = x;
  2029. this._y = y;
  2030. this._z = z;
  2031. this._w = w;
  2032. }
  2033. static slerpFlat( dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t ) {
  2034. // fuzz-free, array-based Quaternion SLERP operation
  2035. let x0 = src0[ srcOffset0 + 0 ],
  2036. y0 = src0[ srcOffset0 + 1 ],
  2037. z0 = src0[ srcOffset0 + 2 ],
  2038. w0 = src0[ srcOffset0 + 3 ];
  2039. const x1 = src1[ srcOffset1 + 0 ],
  2040. y1 = src1[ srcOffset1 + 1 ],
  2041. z1 = src1[ srcOffset1 + 2 ],
  2042. w1 = src1[ srcOffset1 + 3 ];
  2043. if ( t === 0 ) {
  2044. dst[ dstOffset + 0 ] = x0;
  2045. dst[ dstOffset + 1 ] = y0;
  2046. dst[ dstOffset + 2 ] = z0;
  2047. dst[ dstOffset + 3 ] = w0;
  2048. return;
  2049. }
  2050. if ( t === 1 ) {
  2051. dst[ dstOffset + 0 ] = x1;
  2052. dst[ dstOffset + 1 ] = y1;
  2053. dst[ dstOffset + 2 ] = z1;
  2054. dst[ dstOffset + 3 ] = w1;
  2055. return;
  2056. }
  2057. if ( w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1 ) {
  2058. let s = 1 - t;
  2059. const cos = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1,
  2060. dir = ( cos >= 0 ? 1 : - 1 ),
  2061. sqrSin = 1 - cos * cos;
  2062. // Skip the Slerp for tiny steps to avoid numeric problems:
  2063. if ( sqrSin > Number.EPSILON ) {
  2064. const sin = Math.sqrt( sqrSin ),
  2065. len = Math.atan2( sin, cos * dir );
  2066. s = Math.sin( s * len ) / sin;
  2067. t = Math.sin( t * len ) / sin;
  2068. }
  2069. const tDir = t * dir;
  2070. x0 = x0 * s + x1 * tDir;
  2071. y0 = y0 * s + y1 * tDir;
  2072. z0 = z0 * s + z1 * tDir;
  2073. w0 = w0 * s + w1 * tDir;
  2074. // Normalize in case we just did a lerp:
  2075. if ( s === 1 - t ) {
  2076. const f = 1 / Math.sqrt( x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0 );
  2077. x0 *= f;
  2078. y0 *= f;
  2079. z0 *= f;
  2080. w0 *= f;
  2081. }
  2082. }
  2083. dst[ dstOffset ] = x0;
  2084. dst[ dstOffset + 1 ] = y0;
  2085. dst[ dstOffset + 2 ] = z0;
  2086. dst[ dstOffset + 3 ] = w0;
  2087. }
  2088. static multiplyQuaternionsFlat( dst, dstOffset, src0, srcOffset0, src1, srcOffset1 ) {
  2089. const x0 = src0[ srcOffset0 ];
  2090. const y0 = src0[ srcOffset0 + 1 ];
  2091. const z0 = src0[ srcOffset0 + 2 ];
  2092. const w0 = src0[ srcOffset0 + 3 ];
  2093. const x1 = src1[ srcOffset1 ];
  2094. const y1 = src1[ srcOffset1 + 1 ];
  2095. const z1 = src1[ srcOffset1 + 2 ];
  2096. const w1 = src1[ srcOffset1 + 3 ];
  2097. dst[ dstOffset ] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1;
  2098. dst[ dstOffset + 1 ] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1;
  2099. dst[ dstOffset + 2 ] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1;
  2100. dst[ dstOffset + 3 ] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
  2101. return dst;
  2102. }
  2103. get x() {
  2104. return this._x;
  2105. }
  2106. set x( value ) {
  2107. this._x = value;
  2108. this._onChangeCallback();
  2109. }
  2110. get y() {
  2111. return this._y;
  2112. }
  2113. set y( value ) {
  2114. this._y = value;
  2115. this._onChangeCallback();
  2116. }
  2117. get z() {
  2118. return this._z;
  2119. }
  2120. set z( value ) {
  2121. this._z = value;
  2122. this._onChangeCallback();
  2123. }
  2124. get w() {
  2125. return this._w;
  2126. }
  2127. set w( value ) {
  2128. this._w = value;
  2129. this._onChangeCallback();
  2130. }
  2131. set( x, y, z, w ) {
  2132. this._x = x;
  2133. this._y = y;
  2134. this._z = z;
  2135. this._w = w;
  2136. this._onChangeCallback();
  2137. return this;
  2138. }
  2139. clone() {
  2140. return new this.constructor( this._x, this._y, this._z, this._w );
  2141. }
  2142. copy( quaternion ) {
  2143. this._x = quaternion.x;
  2144. this._y = quaternion.y;
  2145. this._z = quaternion.z;
  2146. this._w = quaternion.w;
  2147. this._onChangeCallback();
  2148. return this;
  2149. }
  2150. setFromEuler( euler, update = true ) {
  2151. const x = euler._x, y = euler._y, z = euler._z, order = euler._order;
  2152. // http://www.mathworks.com/matlabcentral/fileexchange/
  2153. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  2154. // content/SpinCalc.m
  2155. const cos = Math.cos;
  2156. const sin = Math.sin;
  2157. const c1 = cos( x / 2 );
  2158. const c2 = cos( y / 2 );
  2159. const c3 = cos( z / 2 );
  2160. const s1 = sin( x / 2 );
  2161. const s2 = sin( y / 2 );
  2162. const s3 = sin( z / 2 );
  2163. switch ( order ) {
  2164. case 'XYZ':
  2165. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  2166. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  2167. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  2168. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  2169. break;
  2170. case 'YXZ':
  2171. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  2172. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  2173. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  2174. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  2175. break;
  2176. case 'ZXY':
  2177. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  2178. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  2179. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  2180. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  2181. break;
  2182. case 'ZYX':
  2183. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  2184. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  2185. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  2186. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  2187. break;
  2188. case 'YZX':
  2189. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  2190. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  2191. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  2192. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  2193. break;
  2194. case 'XZY':
  2195. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  2196. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  2197. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  2198. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  2199. break;
  2200. default:
  2201. console.warn( 'THREE.Quaternion: .setFromEuler() encountered an unknown order: ' + order );
  2202. }
  2203. if ( update === true ) this._onChangeCallback();
  2204. return this;
  2205. }
  2206. setFromAxisAngle( axis, angle ) {
  2207. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  2208. // assumes axis is normalized
  2209. const halfAngle = angle / 2, s = Math.sin( halfAngle );
  2210. this._x = axis.x * s;
  2211. this._y = axis.y * s;
  2212. this._z = axis.z * s;
  2213. this._w = Math.cos( halfAngle );
  2214. this._onChangeCallback();
  2215. return this;
  2216. }
  2217. setFromRotationMatrix( m ) {
  2218. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  2219. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  2220. const te = m.elements,
  2221. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  2222. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  2223. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
  2224. trace = m11 + m22 + m33;
  2225. if ( trace > 0 ) {
  2226. const s = 0.5 / Math.sqrt( trace + 1.0 );
  2227. this._w = 0.25 / s;
  2228. this._x = ( m32 - m23 ) * s;
  2229. this._y = ( m13 - m31 ) * s;
  2230. this._z = ( m21 - m12 ) * s;
  2231. } else if ( m11 > m22 && m11 > m33 ) {
  2232. const s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  2233. this._w = ( m32 - m23 ) / s;
  2234. this._x = 0.25 * s;
  2235. this._y = ( m12 + m21 ) / s;
  2236. this._z = ( m13 + m31 ) / s;
  2237. } else if ( m22 > m33 ) {
  2238. const s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  2239. this._w = ( m13 - m31 ) / s;
  2240. this._x = ( m12 + m21 ) / s;
  2241. this._y = 0.25 * s;
  2242. this._z = ( m23 + m32 ) / s;
  2243. } else {
  2244. const s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  2245. this._w = ( m21 - m12 ) / s;
  2246. this._x = ( m13 + m31 ) / s;
  2247. this._y = ( m23 + m32 ) / s;
  2248. this._z = 0.25 * s;
  2249. }
  2250. this._onChangeCallback();
  2251. return this;
  2252. }
  2253. setFromUnitVectors( vFrom, vTo ) {
  2254. // assumes direction vectors vFrom and vTo are normalized
  2255. let r = vFrom.dot( vTo ) + 1;
  2256. if ( r < Number.EPSILON ) {
  2257. // vFrom and vTo point in opposite directions
  2258. r = 0;
  2259. if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
  2260. this._x = - vFrom.y;
  2261. this._y = vFrom.x;
  2262. this._z = 0;
  2263. this._w = r;
  2264. } else {
  2265. this._x = 0;
  2266. this._y = - vFrom.z;
  2267. this._z = vFrom.y;
  2268. this._w = r;
  2269. }
  2270. } else {
  2271. // crossVectors( vFrom, vTo ); // inlined to avoid cyclic dependency on Vector3
  2272. this._x = vFrom.y * vTo.z - vFrom.z * vTo.y;
  2273. this._y = vFrom.z * vTo.x - vFrom.x * vTo.z;
  2274. this._z = vFrom.x * vTo.y - vFrom.y * vTo.x;
  2275. this._w = r;
  2276. }
  2277. return this.normalize();
  2278. }
  2279. angleTo( q ) {
  2280. return 2 * Math.acos( Math.abs( clamp( this.dot( q ), - 1, 1 ) ) );
  2281. }
  2282. rotateTowards( q, step ) {
  2283. const angle = this.angleTo( q );
  2284. if ( angle === 0 ) return this;
  2285. const t = Math.min( 1, step / angle );
  2286. this.slerp( q, t );
  2287. return this;
  2288. }
  2289. identity() {
  2290. return this.set( 0, 0, 0, 1 );
  2291. }
  2292. invert() {
  2293. // quaternion is assumed to have unit length
  2294. return this.conjugate();
  2295. }
  2296. conjugate() {
  2297. this._x *= - 1;
  2298. this._y *= - 1;
  2299. this._z *= - 1;
  2300. this._onChangeCallback();
  2301. return this;
  2302. }
  2303. dot( v ) {
  2304. return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
  2305. }
  2306. lengthSq() {
  2307. return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
  2308. }
  2309. length() {
  2310. return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
  2311. }
  2312. normalize() {
  2313. let l = this.length();
  2314. if ( l === 0 ) {
  2315. this._x = 0;
  2316. this._y = 0;
  2317. this._z = 0;
  2318. this._w = 1;
  2319. } else {
  2320. l = 1 / l;
  2321. this._x = this._x * l;
  2322. this._y = this._y * l;
  2323. this._z = this._z * l;
  2324. this._w = this._w * l;
  2325. }
  2326. this._onChangeCallback();
  2327. return this;
  2328. }
  2329. multiply( q ) {
  2330. return this.multiplyQuaternions( this, q );
  2331. }
  2332. premultiply( q ) {
  2333. return this.multiplyQuaternions( q, this );
  2334. }
  2335. multiplyQuaternions( a, b ) {
  2336. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  2337. const qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
  2338. const qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
  2339. this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  2340. this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  2341. this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  2342. this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  2343. this._onChangeCallback();
  2344. return this;
  2345. }
  2346. slerp( qb, t ) {
  2347. if ( t === 0 ) return this;
  2348. if ( t === 1 ) return this.copy( qb );
  2349. const x = this._x, y = this._y, z = this._z, w = this._w;
  2350. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  2351. let cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
  2352. if ( cosHalfTheta < 0 ) {
  2353. this._w = - qb._w;
  2354. this._x = - qb._x;
  2355. this._y = - qb._y;
  2356. this._z = - qb._z;
  2357. cosHalfTheta = - cosHalfTheta;
  2358. } else {
  2359. this.copy( qb );
  2360. }
  2361. if ( cosHalfTheta >= 1.0 ) {
  2362. this._w = w;
  2363. this._x = x;
  2364. this._y = y;
  2365. this._z = z;
  2366. return this;
  2367. }
  2368. const sqrSinHalfTheta = 1.0 - cosHalfTheta * cosHalfTheta;
  2369. if ( sqrSinHalfTheta <= Number.EPSILON ) {
  2370. const s = 1 - t;
  2371. this._w = s * w + t * this._w;
  2372. this._x = s * x + t * this._x;
  2373. this._y = s * y + t * this._y;
  2374. this._z = s * z + t * this._z;
  2375. this.normalize(); // normalize calls _onChangeCallback()
  2376. return this;
  2377. }
  2378. const sinHalfTheta = Math.sqrt( sqrSinHalfTheta );
  2379. const halfTheta = Math.atan2( sinHalfTheta, cosHalfTheta );
  2380. const ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  2381. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  2382. this._w = ( w * ratioA + this._w * ratioB );
  2383. this._x = ( x * ratioA + this._x * ratioB );
  2384. this._y = ( y * ratioA + this._y * ratioB );
  2385. this._z = ( z * ratioA + this._z * ratioB );
  2386. this._onChangeCallback();
  2387. return this;
  2388. }
  2389. slerpQuaternions( qa, qb, t ) {
  2390. return this.copy( qa ).slerp( qb, t );
  2391. }
  2392. random() {
  2393. // sets this quaternion to a uniform random unit quaternnion
  2394. // Ken Shoemake
  2395. // Uniform random rotations
  2396. // D. Kirk, editor, Graphics Gems III, pages 124-132. Academic Press, New York, 1992.
  2397. const theta1 = 2 * Math.PI * Math.random();
  2398. const theta2 = 2 * Math.PI * Math.random();
  2399. const x0 = Math.random();
  2400. const r1 = Math.sqrt( 1 - x0 );
  2401. const r2 = Math.sqrt( x0 );
  2402. return this.set(
  2403. r1 * Math.sin( theta1 ),
  2404. r1 * Math.cos( theta1 ),
  2405. r2 * Math.sin( theta2 ),
  2406. r2 * Math.cos( theta2 ),
  2407. );
  2408. }
  2409. equals( quaternion ) {
  2410. return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
  2411. }
  2412. fromArray( array, offset = 0 ) {
  2413. this._x = array[ offset ];
  2414. this._y = array[ offset + 1 ];
  2415. this._z = array[ offset + 2 ];
  2416. this._w = array[ offset + 3 ];
  2417. this._onChangeCallback();
  2418. return this;
  2419. }
  2420. toArray( array = [], offset = 0 ) {
  2421. array[ offset ] = this._x;
  2422. array[ offset + 1 ] = this._y;
  2423. array[ offset + 2 ] = this._z;
  2424. array[ offset + 3 ] = this._w;
  2425. return array;
  2426. }
  2427. fromBufferAttribute( attribute, index ) {
  2428. this._x = attribute.getX( index );
  2429. this._y = attribute.getY( index );
  2430. this._z = attribute.getZ( index );
  2431. this._w = attribute.getW( index );
  2432. this._onChangeCallback();
  2433. return this;
  2434. }
  2435. toJSON() {
  2436. return this.toArray();
  2437. }
  2438. _onChange( callback ) {
  2439. this._onChangeCallback = callback;
  2440. return this;
  2441. }
  2442. _onChangeCallback() {}
  2443. *[ Symbol.iterator ]() {
  2444. yield this._x;
  2445. yield this._y;
  2446. yield this._z;
  2447. yield this._w;
  2448. }
  2449. }
  2450. class Vector3 {
  2451. constructor( x = 0, y = 0, z = 0 ) {
  2452. Vector3.prototype.isVector3 = true;
  2453. this.x = x;
  2454. this.y = y;
  2455. this.z = z;
  2456. }
  2457. set( x, y, z ) {
  2458. if ( z === undefined ) z = this.z; // sprite.scale.set(x,y)
  2459. this.x = x;
  2460. this.y = y;
  2461. this.z = z;
  2462. return this;
  2463. }
  2464. setScalar( scalar ) {
  2465. this.x = scalar;
  2466. this.y = scalar;
  2467. this.z = scalar;
  2468. return this;
  2469. }
  2470. setX( x ) {
  2471. this.x = x;
  2472. return this;
  2473. }
  2474. setY( y ) {
  2475. this.y = y;
  2476. return this;
  2477. }
  2478. setZ( z ) {
  2479. this.z = z;
  2480. return this;
  2481. }
  2482. setComponent( index, value ) {
  2483. switch ( index ) {
  2484. case 0: this.x = value; break;
  2485. case 1: this.y = value; break;
  2486. case 2: this.z = value; break;
  2487. default: throw new Error( 'index is out of range: ' + index );
  2488. }
  2489. return this;
  2490. }
  2491. getComponent( index ) {
  2492. switch ( index ) {
  2493. case 0: return this.x;
  2494. case 1: return this.y;
  2495. case 2: return this.z;
  2496. default: throw new Error( 'index is out of range: ' + index );
  2497. }
  2498. }
  2499. clone() {
  2500. return new this.constructor( this.x, this.y, this.z );
  2501. }
  2502. copy( v ) {
  2503. this.x = v.x;
  2504. this.y = v.y;
  2505. this.z = v.z;
  2506. return this;
  2507. }
  2508. add( v ) {
  2509. this.x += v.x;
  2510. this.y += v.y;
  2511. this.z += v.z;
  2512. return this;
  2513. }
  2514. addScalar( s ) {
  2515. this.x += s;
  2516. this.y += s;
  2517. this.z += s;
  2518. return this;
  2519. }
  2520. addVectors( a, b ) {
  2521. this.x = a.x + b.x;
  2522. this.y = a.y + b.y;
  2523. this.z = a.z + b.z;
  2524. return this;
  2525. }
  2526. addScaledVector( v, s ) {
  2527. this.x += v.x * s;
  2528. this.y += v.y * s;
  2529. this.z += v.z * s;
  2530. return this;
  2531. }
  2532. sub( v ) {
  2533. this.x -= v.x;
  2534. this.y -= v.y;
  2535. this.z -= v.z;
  2536. return this;
  2537. }
  2538. subScalar( s ) {
  2539. this.x -= s;
  2540. this.y -= s;
  2541. this.z -= s;
  2542. return this;
  2543. }
  2544. subVectors( a, b ) {
  2545. this.x = a.x - b.x;
  2546. this.y = a.y - b.y;
  2547. this.z = a.z - b.z;
  2548. return this;
  2549. }
  2550. multiply( v ) {
  2551. this.x *= v.x;
  2552. this.y *= v.y;
  2553. this.z *= v.z;
  2554. return this;
  2555. }
  2556. multiplyScalar( scalar ) {
  2557. this.x *= scalar;
  2558. this.y *= scalar;
  2559. this.z *= scalar;
  2560. return this;
  2561. }
  2562. multiplyVectors( a, b ) {
  2563. this.x = a.x * b.x;
  2564. this.y = a.y * b.y;
  2565. this.z = a.z * b.z;
  2566. return this;
  2567. }
  2568. applyEuler( euler ) {
  2569. return this.applyQuaternion( _quaternion$4.setFromEuler( euler ) );
  2570. }
  2571. applyAxisAngle( axis, angle ) {
  2572. return this.applyQuaternion( _quaternion$4.setFromAxisAngle( axis, angle ) );
  2573. }
  2574. applyMatrix3( m ) {
  2575. const x = this.x, y = this.y, z = this.z;
  2576. const e = m.elements;
  2577. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
  2578. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
  2579. this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
  2580. return this;
  2581. }
  2582. applyNormalMatrix( m ) {
  2583. return this.applyMatrix3( m ).normalize();
  2584. }
  2585. applyMatrix4( m ) {
  2586. const x = this.x, y = this.y, z = this.z;
  2587. const e = m.elements;
  2588. const w = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] );
  2589. this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * w;
  2590. this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * w;
  2591. this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * w;
  2592. return this;
  2593. }
  2594. applyQuaternion( q ) {
  2595. // quaternion q is assumed to have unit length
  2596. const vx = this.x, vy = this.y, vz = this.z;
  2597. const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
  2598. // t = 2 * cross( q.xyz, v );
  2599. const tx = 2 * ( qy * vz - qz * vy );
  2600. const ty = 2 * ( qz * vx - qx * vz );
  2601. const tz = 2 * ( qx * vy - qy * vx );
  2602. // v + q.w * t + cross( q.xyz, t );
  2603. this.x = vx + qw * tx + qy * tz - qz * ty;
  2604. this.y = vy + qw * ty + qz * tx - qx * tz;
  2605. this.z = vz + qw * tz + qx * ty - qy * tx;
  2606. return this;
  2607. }
  2608. project( camera ) {
  2609. return this.applyMatrix4( camera.matrixWorldInverse ).applyMatrix4( camera.projectionMatrix );
  2610. }
  2611. unproject( camera ) {
  2612. return this.applyMatrix4( camera.projectionMatrixInverse ).applyMatrix4( camera.matrixWorld );
  2613. }
  2614. transformDirection( m ) {
  2615. // input: THREE.Matrix4 affine matrix
  2616. // vector interpreted as a direction
  2617. const x = this.x, y = this.y, z = this.z;
  2618. const e = m.elements;
  2619. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
  2620. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
  2621. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
  2622. return this.normalize();
  2623. }
  2624. divide( v ) {
  2625. this.x /= v.x;
  2626. this.y /= v.y;
  2627. this.z /= v.z;
  2628. return this;
  2629. }
  2630. divideScalar( scalar ) {
  2631. return this.multiplyScalar( 1 / scalar );
  2632. }
  2633. min( v ) {
  2634. this.x = Math.min( this.x, v.x );
  2635. this.y = Math.min( this.y, v.y );
  2636. this.z = Math.min( this.z, v.z );
  2637. return this;
  2638. }
  2639. max( v ) {
  2640. this.x = Math.max( this.x, v.x );
  2641. this.y = Math.max( this.y, v.y );
  2642. this.z = Math.max( this.z, v.z );
  2643. return this;
  2644. }
  2645. clamp( min, max ) {
  2646. // assumes min < max, componentwise
  2647. this.x = clamp( this.x, min.x, max.x );
  2648. this.y = clamp( this.y, min.y, max.y );
  2649. this.z = clamp( this.z, min.z, max.z );
  2650. return this;
  2651. }
  2652. clampScalar( minVal, maxVal ) {
  2653. this.x = clamp( this.x, minVal, maxVal );
  2654. this.y = clamp( this.y, minVal, maxVal );
  2655. this.z = clamp( this.z, minVal, maxVal );
  2656. return this;
  2657. }
  2658. clampLength( min, max ) {
  2659. const length = this.length();
  2660. return this.divideScalar( length || 1 ).multiplyScalar( clamp( length, min, max ) );
  2661. }
  2662. floor() {
  2663. this.x = Math.floor( this.x );
  2664. this.y = Math.floor( this.y );
  2665. this.z = Math.floor( this.z );
  2666. return this;
  2667. }
  2668. ceil() {
  2669. this.x = Math.ceil( this.x );
  2670. this.y = Math.ceil( this.y );
  2671. this.z = Math.ceil( this.z );
  2672. return this;
  2673. }
  2674. round() {
  2675. this.x = Math.round( this.x );
  2676. this.y = Math.round( this.y );
  2677. this.z = Math.round( this.z );
  2678. return this;
  2679. }
  2680. roundToZero() {
  2681. this.x = Math.trunc( this.x );
  2682. this.y = Math.trunc( this.y );
  2683. this.z = Math.trunc( this.z );
  2684. return this;
  2685. }
  2686. negate() {
  2687. this.x = - this.x;
  2688. this.y = - this.y;
  2689. this.z = - this.z;
  2690. return this;
  2691. }
  2692. dot( v ) {
  2693. return this.x * v.x + this.y * v.y + this.z * v.z;
  2694. }
  2695. // TODO lengthSquared?
  2696. lengthSq() {
  2697. return this.x * this.x + this.y * this.y + this.z * this.z;
  2698. }
  2699. length() {
  2700. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  2701. }
  2702. manhattanLength() {
  2703. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  2704. }
  2705. normalize() {
  2706. return this.divideScalar( this.length() || 1 );
  2707. }
  2708. setLength( length ) {
  2709. return this.normalize().multiplyScalar( length );
  2710. }
  2711. lerp( v, alpha ) {
  2712. this.x += ( v.x - this.x ) * alpha;
  2713. this.y += ( v.y - this.y ) * alpha;
  2714. this.z += ( v.z - this.z ) * alpha;
  2715. return this;
  2716. }
  2717. lerpVectors( v1, v2, alpha ) {
  2718. this.x = v1.x + ( v2.x - v1.x ) * alpha;
  2719. this.y = v1.y + ( v2.y - v1.y ) * alpha;
  2720. this.z = v1.z + ( v2.z - v1.z ) * alpha;
  2721. return this;
  2722. }
  2723. cross( v ) {
  2724. return this.crossVectors( this, v );
  2725. }
  2726. crossVectors( a, b ) {
  2727. const ax = a.x, ay = a.y, az = a.z;
  2728. const bx = b.x, by = b.y, bz = b.z;
  2729. this.x = ay * bz - az * by;
  2730. this.y = az * bx - ax * bz;
  2731. this.z = ax * by - ay * bx;
  2732. return this;
  2733. }
  2734. projectOnVector( v ) {
  2735. const denominator = v.lengthSq();
  2736. if ( denominator === 0 ) return this.set( 0, 0, 0 );
  2737. const scalar = v.dot( this ) / denominator;
  2738. return this.copy( v ).multiplyScalar( scalar );
  2739. }
  2740. projectOnPlane( planeNormal ) {
  2741. _vector$c.copy( this ).projectOnVector( planeNormal );
  2742. return this.sub( _vector$c );
  2743. }
  2744. reflect( normal ) {
  2745. // reflect incident vector off plane orthogonal to normal
  2746. // normal is assumed to have unit length
  2747. return this.sub( _vector$c.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
  2748. }
  2749. angleTo( v ) {
  2750. const denominator = Math.sqrt( this.lengthSq() * v.lengthSq() );
  2751. if ( denominator === 0 ) return Math.PI / 2;
  2752. const theta = this.dot( v ) / denominator;
  2753. // clamp, to handle numerical problems
  2754. return Math.acos( clamp( theta, - 1, 1 ) );
  2755. }
  2756. distanceTo( v ) {
  2757. return Math.sqrt( this.distanceToSquared( v ) );
  2758. }
  2759. distanceToSquared( v ) {
  2760. const dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
  2761. return dx * dx + dy * dy + dz * dz;
  2762. }
  2763. manhattanDistanceTo( v ) {
  2764. return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ) + Math.abs( this.z - v.z );
  2765. }
  2766. setFromSpherical( s ) {
  2767. return this.setFromSphericalCoords( s.radius, s.phi, s.theta );
  2768. }
  2769. setFromSphericalCoords( radius, phi, theta ) {
  2770. const sinPhiRadius = Math.sin( phi ) * radius;
  2771. this.x = sinPhiRadius * Math.sin( theta );
  2772. this.y = Math.cos( phi ) * radius;
  2773. this.z = sinPhiRadius * Math.cos( theta );
  2774. return this;
  2775. }
  2776. setFromCylindrical( c ) {
  2777. return this.setFromCylindricalCoords( c.radius, c.theta, c.y );
  2778. }
  2779. setFromCylindricalCoords( radius, theta, y ) {
  2780. this.x = radius * Math.sin( theta );
  2781. this.y = y;
  2782. this.z = radius * Math.cos( theta );
  2783. return this;
  2784. }
  2785. setFromMatrixPosition( m ) {
  2786. const e = m.elements;
  2787. this.x = e[ 12 ];
  2788. this.y = e[ 13 ];
  2789. this.z = e[ 14 ];
  2790. return this;
  2791. }
  2792. setFromMatrixScale( m ) {
  2793. const sx = this.setFromMatrixColumn( m, 0 ).length();
  2794. const sy = this.setFromMatrixColumn( m, 1 ).length();
  2795. const sz = this.setFromMatrixColumn( m, 2 ).length();
  2796. this.x = sx;
  2797. this.y = sy;
  2798. this.z = sz;
  2799. return this;
  2800. }
  2801. setFromMatrixColumn( m, index ) {
  2802. return this.fromArray( m.elements, index * 4 );
  2803. }
  2804. setFromMatrix3Column( m, index ) {
  2805. return this.fromArray( m.elements, index * 3 );
  2806. }
  2807. setFromEuler( e ) {
  2808. this.x = e._x;
  2809. this.y = e._y;
  2810. this.z = e._z;
  2811. return this;
  2812. }
  2813. setFromColor( c ) {
  2814. this.x = c.r;
  2815. this.y = c.g;
  2816. this.z = c.b;
  2817. return this;
  2818. }
  2819. equals( v ) {
  2820. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  2821. }
  2822. fromArray( array, offset = 0 ) {
  2823. this.x = array[ offset ];
  2824. this.y = array[ offset + 1 ];
  2825. this.z = array[ offset + 2 ];
  2826. return this;
  2827. }
  2828. toArray( array = [], offset = 0 ) {
  2829. array[ offset ] = this.x;
  2830. array[ offset + 1 ] = this.y;
  2831. array[ offset + 2 ] = this.z;
  2832. return array;
  2833. }
  2834. fromBufferAttribute( attribute, index ) {
  2835. this.x = attribute.getX( index );
  2836. this.y = attribute.getY( index );
  2837. this.z = attribute.getZ( index );
  2838. return this;
  2839. }
  2840. random() {
  2841. this.x = Math.random();
  2842. this.y = Math.random();
  2843. this.z = Math.random();
  2844. return this;
  2845. }
  2846. randomDirection() {
  2847. // https://mathworld.wolfram.com/SpherePointPicking.html
  2848. const theta = Math.random() * Math.PI * 2;
  2849. const u = Math.random() * 2 - 1;
  2850. const c = Math.sqrt( 1 - u * u );
  2851. this.x = c * Math.cos( theta );
  2852. this.y = u;
  2853. this.z = c * Math.sin( theta );
  2854. return this;
  2855. }
  2856. *[ Symbol.iterator ]() {
  2857. yield this.x;
  2858. yield this.y;
  2859. yield this.z;
  2860. }
  2861. }
  2862. const _vector$c = /*@__PURE__*/ new Vector3();
  2863. const _quaternion$4 = /*@__PURE__*/ new Quaternion();
  2864. class Box3 {
  2865. constructor( min = new Vector3( + Infinity, + Infinity, + Infinity ), max = new Vector3( - Infinity, - Infinity, - Infinity ) ) {
  2866. this.isBox3 = true;
  2867. this.min = min;
  2868. this.max = max;
  2869. }
  2870. set( min, max ) {
  2871. this.min.copy( min );
  2872. this.max.copy( max );
  2873. return this;
  2874. }
  2875. setFromArray( array ) {
  2876. this.makeEmpty();
  2877. for ( let i = 0, il = array.length; i < il; i += 3 ) {
  2878. this.expandByPoint( _vector$b.fromArray( array, i ) );
  2879. }
  2880. return this;
  2881. }
  2882. setFromBufferAttribute( attribute ) {
  2883. this.makeEmpty();
  2884. for ( let i = 0, il = attribute.count; i < il; i ++ ) {
  2885. this.expandByPoint( _vector$b.fromBufferAttribute( attribute, i ) );
  2886. }
  2887. return this;
  2888. }
  2889. setFromPoints( points ) {
  2890. this.makeEmpty();
  2891. for ( let i = 0, il = points.length; i < il; i ++ ) {
  2892. this.expandByPoint( points[ i ] );
  2893. }
  2894. return this;
  2895. }
  2896. setFromCenterAndSize( center, size ) {
  2897. const halfSize = _vector$b.copy( size ).multiplyScalar( 0.5 );
  2898. this.min.copy( center ).sub( halfSize );
  2899. this.max.copy( center ).add( halfSize );
  2900. return this;
  2901. }
  2902. setFromObject( object, precise = false ) {
  2903. this.makeEmpty();
  2904. return this.expandByObject( object, precise );
  2905. }
  2906. clone() {
  2907. return new this.constructor().copy( this );
  2908. }
  2909. copy( box ) {
  2910. this.min.copy( box.min );
  2911. this.max.copy( box.max );
  2912. return this;
  2913. }
  2914. makeEmpty() {
  2915. this.min.x = this.min.y = this.min.z = + Infinity;
  2916. this.max.x = this.max.y = this.max.z = - Infinity;
  2917. return this;
  2918. }
  2919. isEmpty() {
  2920. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  2921. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
  2922. }
  2923. getCenter( target ) {
  2924. return this.isEmpty() ? target.set( 0, 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  2925. }
  2926. getSize( target ) {
  2927. return this.isEmpty() ? target.set( 0, 0, 0 ) : target.subVectors( this.max, this.min );
  2928. }
  2929. expandByPoint( point ) {
  2930. this.min.min( point );
  2931. this.max.max( point );
  2932. return this;
  2933. }
  2934. expandByVector( vector ) {
  2935. this.min.sub( vector );
  2936. this.max.add( vector );
  2937. return this;
  2938. }
  2939. expandByScalar( scalar ) {
  2940. this.min.addScalar( - scalar );
  2941. this.max.addScalar( scalar );
  2942. return this;
  2943. }
  2944. expandByObject( object, precise = false ) {
  2945. // Computes the world-axis-aligned bounding box of an object (including its children),
  2946. // accounting for both the object's, and children's, world transforms
  2947. object.updateWorldMatrix( false, false );
  2948. const geometry = object.geometry;
  2949. if ( geometry !== undefined ) {
  2950. const positionAttribute = geometry.getAttribute( 'position' );
  2951. // precise AABB computation based on vertex data requires at least a position attribute.
  2952. // instancing isn't supported so far and uses the normal (conservative) code path.
  2953. if ( precise === true && positionAttribute !== undefined && object.isInstancedMesh !== true ) {
  2954. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  2955. if ( object.isMesh === true ) {
  2956. object.getVertexPosition( i, _vector$b );
  2957. } else {
  2958. _vector$b.fromBufferAttribute( positionAttribute, i );
  2959. }
  2960. _vector$b.applyMatrix4( object.matrixWorld );
  2961. this.expandByPoint( _vector$b );
  2962. }
  2963. } else {
  2964. if ( object.boundingBox !== undefined ) {
  2965. // object-level bounding box
  2966. if ( object.boundingBox === null ) {
  2967. object.computeBoundingBox();
  2968. }
  2969. _box$4.copy( object.boundingBox );
  2970. } else {
  2971. // geometry-level bounding box
  2972. if ( geometry.boundingBox === null ) {
  2973. geometry.computeBoundingBox();
  2974. }
  2975. _box$4.copy( geometry.boundingBox );
  2976. }
  2977. _box$4.applyMatrix4( object.matrixWorld );
  2978. this.union( _box$4 );
  2979. }
  2980. }
  2981. const children = object.children;
  2982. for ( let i = 0, l = children.length; i < l; i ++ ) {
  2983. this.expandByObject( children[ i ], precise );
  2984. }
  2985. return this;
  2986. }
  2987. containsPoint( point ) {
  2988. return point.x >= this.min.x && point.x <= this.max.x &&
  2989. point.y >= this.min.y && point.y <= this.max.y &&
  2990. point.z >= this.min.z && point.z <= this.max.z;
  2991. }
  2992. containsBox( box ) {
  2993. return this.min.x <= box.min.x && box.max.x <= this.max.x &&
  2994. this.min.y <= box.min.y && box.max.y <= this.max.y &&
  2995. this.min.z <= box.min.z && box.max.z <= this.max.z;
  2996. }
  2997. getParameter( point, target ) {
  2998. // This can potentially have a divide by zero if the box
  2999. // has a size dimension of 0.
  3000. return target.set(
  3001. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  3002. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  3003. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  3004. );
  3005. }
  3006. intersectsBox( box ) {
  3007. // using 6 splitting planes to rule out intersections.
  3008. return box.max.x >= this.min.x && box.min.x <= this.max.x &&
  3009. box.max.y >= this.min.y && box.min.y <= this.max.y &&
  3010. box.max.z >= this.min.z && box.min.z <= this.max.z;
  3011. }
  3012. intersectsSphere( sphere ) {
  3013. // Find the point on the AABB closest to the sphere center.
  3014. this.clampPoint( sphere.center, _vector$b );
  3015. // If that point is inside the sphere, the AABB and sphere intersect.
  3016. return _vector$b.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  3017. }
  3018. intersectsPlane( plane ) {
  3019. // We compute the minimum and maximum dot product values. If those values
  3020. // are on the same side (back or front) of the plane, then there is no intersection.
  3021. let min, max;
  3022. if ( plane.normal.x > 0 ) {
  3023. min = plane.normal.x * this.min.x;
  3024. max = plane.normal.x * this.max.x;
  3025. } else {
  3026. min = plane.normal.x * this.max.x;
  3027. max = plane.normal.x * this.min.x;
  3028. }
  3029. if ( plane.normal.y > 0 ) {
  3030. min += plane.normal.y * this.min.y;
  3031. max += plane.normal.y * this.max.y;
  3032. } else {
  3033. min += plane.normal.y * this.max.y;
  3034. max += plane.normal.y * this.min.y;
  3035. }
  3036. if ( plane.normal.z > 0 ) {
  3037. min += plane.normal.z * this.min.z;
  3038. max += plane.normal.z * this.max.z;
  3039. } else {
  3040. min += plane.normal.z * this.max.z;
  3041. max += plane.normal.z * this.min.z;
  3042. }
  3043. return ( min <= - plane.constant && max >= - plane.constant );
  3044. }
  3045. intersectsTriangle( triangle ) {
  3046. if ( this.isEmpty() ) {
  3047. return false;
  3048. }
  3049. // compute box center and extents
  3050. this.getCenter( _center );
  3051. _extents.subVectors( this.max, _center );
  3052. // translate triangle to aabb origin
  3053. _v0$2.subVectors( triangle.a, _center );
  3054. _v1$7.subVectors( triangle.b, _center );
  3055. _v2$4.subVectors( triangle.c, _center );
  3056. // compute edge vectors for triangle
  3057. _f0.subVectors( _v1$7, _v0$2 );
  3058. _f1.subVectors( _v2$4, _v1$7 );
  3059. _f2.subVectors( _v0$2, _v2$4 );
  3060. // test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb
  3061. // make an axis testing of each of the 3 sides of the aabb against each of the 3 sides of the triangle = 9 axis of separation
  3062. // axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned)
  3063. let axes = [
  3064. 0, - _f0.z, _f0.y, 0, - _f1.z, _f1.y, 0, - _f2.z, _f2.y,
  3065. _f0.z, 0, - _f0.x, _f1.z, 0, - _f1.x, _f2.z, 0, - _f2.x,
  3066. - _f0.y, _f0.x, 0, - _f1.y, _f1.x, 0, - _f2.y, _f2.x, 0
  3067. ];
  3068. if ( ! satForAxes( axes, _v0$2, _v1$7, _v2$4, _extents ) ) {
  3069. return false;
  3070. }
  3071. // test 3 face normals from the aabb
  3072. axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
  3073. if ( ! satForAxes( axes, _v0$2, _v1$7, _v2$4, _extents ) ) {
  3074. return false;
  3075. }
  3076. // finally testing the face normal of the triangle
  3077. // use already existing triangle edge vectors here
  3078. _triangleNormal.crossVectors( _f0, _f1 );
  3079. axes = [ _triangleNormal.x, _triangleNormal.y, _triangleNormal.z ];
  3080. return satForAxes( axes, _v0$2, _v1$7, _v2$4, _extents );
  3081. }
  3082. clampPoint( point, target ) {
  3083. return target.copy( point ).clamp( this.min, this.max );
  3084. }
  3085. distanceToPoint( point ) {
  3086. return this.clampPoint( point, _vector$b ).distanceTo( point );
  3087. }
  3088. getBoundingSphere( target ) {
  3089. if ( this.isEmpty() ) {
  3090. target.makeEmpty();
  3091. } else {
  3092. this.getCenter( target.center );
  3093. target.radius = this.getSize( _vector$b ).length() * 0.5;
  3094. }
  3095. return target;
  3096. }
  3097. intersect( box ) {
  3098. this.min.max( box.min );
  3099. this.max.min( box.max );
  3100. // ensure that if there is no overlap, the result is fully empty, not slightly empty with non-inf/+inf values that will cause subsequence intersects to erroneously return valid values.
  3101. if ( this.isEmpty() ) this.makeEmpty();
  3102. return this;
  3103. }
  3104. union( box ) {
  3105. this.min.min( box.min );
  3106. this.max.max( box.max );
  3107. return this;
  3108. }
  3109. applyMatrix4( matrix ) {
  3110. // transform of empty box is an empty box.
  3111. if ( this.isEmpty() ) return this;
  3112. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  3113. _points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  3114. _points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  3115. _points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  3116. _points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  3117. _points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  3118. _points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  3119. _points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  3120. _points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  3121. this.setFromPoints( _points );
  3122. return this;
  3123. }
  3124. translate( offset ) {
  3125. this.min.add( offset );
  3126. this.max.add( offset );
  3127. return this;
  3128. }
  3129. equals( box ) {
  3130. return box.min.equals( this.min ) && box.max.equals( this.max );
  3131. }
  3132. }
  3133. const _points = [
  3134. /*@__PURE__*/ new Vector3(),
  3135. /*@__PURE__*/ new Vector3(),
  3136. /*@__PURE__*/ new Vector3(),
  3137. /*@__PURE__*/ new Vector3(),
  3138. /*@__PURE__*/ new Vector3(),
  3139. /*@__PURE__*/ new Vector3(),
  3140. /*@__PURE__*/ new Vector3(),
  3141. /*@__PURE__*/ new Vector3()
  3142. ];
  3143. const _vector$b = /*@__PURE__*/ new Vector3();
  3144. const _box$4 = /*@__PURE__*/ new Box3();
  3145. // triangle centered vertices
  3146. const _v0$2 = /*@__PURE__*/ new Vector3();
  3147. const _v1$7 = /*@__PURE__*/ new Vector3();
  3148. const _v2$4 = /*@__PURE__*/ new Vector3();
  3149. // triangle edge vectors
  3150. const _f0 = /*@__PURE__*/ new Vector3();
  3151. const _f1 = /*@__PURE__*/ new Vector3();
  3152. const _f2 = /*@__PURE__*/ new Vector3();
  3153. const _center = /*@__PURE__*/ new Vector3();
  3154. const _extents = /*@__PURE__*/ new Vector3();
  3155. const _triangleNormal = /*@__PURE__*/ new Vector3();
  3156. const _testAxis = /*@__PURE__*/ new Vector3();
  3157. function satForAxes( axes, v0, v1, v2, extents ) {
  3158. for ( let i = 0, j = axes.length - 3; i <= j; i += 3 ) {
  3159. _testAxis.fromArray( axes, i );
  3160. // project the aabb onto the separating axis
  3161. const r = extents.x * Math.abs( _testAxis.x ) + extents.y * Math.abs( _testAxis.y ) + extents.z * Math.abs( _testAxis.z );
  3162. // project all 3 vertices of the triangle onto the separating axis
  3163. const p0 = v0.dot( _testAxis );
  3164. const p1 = v1.dot( _testAxis );
  3165. const p2 = v2.dot( _testAxis );
  3166. // actual test, basically see if either of the most extreme of the triangle points intersects r
  3167. if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) {
  3168. // points of the projected triangle are outside the projected half-length of the aabb
  3169. // the axis is separating and we can exit
  3170. return false;
  3171. }
  3172. }
  3173. return true;
  3174. }
  3175. const _box$3 = /*@__PURE__*/ new Box3();
  3176. const _v1$6 = /*@__PURE__*/ new Vector3();
  3177. const _v2$3 = /*@__PURE__*/ new Vector3();
  3178. class Sphere {
  3179. constructor( center = new Vector3(), radius = - 1 ) {
  3180. this.isSphere = true;
  3181. this.center = center;
  3182. this.radius = radius;
  3183. }
  3184. set( center, radius ) {
  3185. this.center.copy( center );
  3186. this.radius = radius;
  3187. return this;
  3188. }
  3189. setFromPoints( points, optionalCenter ) {
  3190. const center = this.center;
  3191. if ( optionalCenter !== undefined ) {
  3192. center.copy( optionalCenter );
  3193. } else {
  3194. _box$3.setFromPoints( points ).getCenter( center );
  3195. }
  3196. let maxRadiusSq = 0;
  3197. for ( let i = 0, il = points.length; i < il; i ++ ) {
  3198. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
  3199. }
  3200. this.radius = Math.sqrt( maxRadiusSq );
  3201. return this;
  3202. }
  3203. copy( sphere ) {
  3204. this.center.copy( sphere.center );
  3205. this.radius = sphere.radius;
  3206. return this;
  3207. }
  3208. isEmpty() {
  3209. return ( this.radius < 0 );
  3210. }
  3211. makeEmpty() {
  3212. this.center.set( 0, 0, 0 );
  3213. this.radius = - 1;
  3214. return this;
  3215. }
  3216. containsPoint( point ) {
  3217. return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
  3218. }
  3219. distanceToPoint( point ) {
  3220. return ( point.distanceTo( this.center ) - this.radius );
  3221. }
  3222. intersectsSphere( sphere ) {
  3223. const radiusSum = this.radius + sphere.radius;
  3224. return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
  3225. }
  3226. intersectsBox( box ) {
  3227. return box.intersectsSphere( this );
  3228. }
  3229. intersectsPlane( plane ) {
  3230. return Math.abs( plane.distanceToPoint( this.center ) ) <= this.radius;
  3231. }
  3232. clampPoint( point, target ) {
  3233. const deltaLengthSq = this.center.distanceToSquared( point );
  3234. target.copy( point );
  3235. if ( deltaLengthSq > ( this.radius * this.radius ) ) {
  3236. target.sub( this.center ).normalize();
  3237. target.multiplyScalar( this.radius ).add( this.center );
  3238. }
  3239. return target;
  3240. }
  3241. getBoundingBox( target ) {
  3242. if ( this.isEmpty() ) {
  3243. // Empty sphere produces empty bounding box
  3244. target.makeEmpty();
  3245. return target;
  3246. }
  3247. target.set( this.center, this.center );
  3248. target.expandByScalar( this.radius );
  3249. return target;
  3250. }
  3251. applyMatrix4( matrix ) {
  3252. this.center.applyMatrix4( matrix );
  3253. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  3254. return this;
  3255. }
  3256. translate( offset ) {
  3257. this.center.add( offset );
  3258. return this;
  3259. }
  3260. expandByPoint( point ) {
  3261. if ( this.isEmpty() ) {
  3262. this.center.copy( point );
  3263. this.radius = 0;
  3264. return this;
  3265. }
  3266. _v1$6.subVectors( point, this.center );
  3267. const lengthSq = _v1$6.lengthSq();
  3268. if ( lengthSq > ( this.radius * this.radius ) ) {
  3269. // calculate the minimal sphere
  3270. const length = Math.sqrt( lengthSq );
  3271. const delta = ( length - this.radius ) * 0.5;
  3272. this.center.addScaledVector( _v1$6, delta / length );
  3273. this.radius += delta;
  3274. }
  3275. return this;
  3276. }
  3277. union( sphere ) {
  3278. if ( sphere.isEmpty() ) {
  3279. return this;
  3280. }
  3281. if ( this.isEmpty() ) {
  3282. this.copy( sphere );
  3283. return this;
  3284. }
  3285. if ( this.center.equals( sphere.center ) === true ) {
  3286. this.radius = Math.max( this.radius, sphere.radius );
  3287. } else {
  3288. _v2$3.subVectors( sphere.center, this.center ).setLength( sphere.radius );
  3289. this.expandByPoint( _v1$6.copy( sphere.center ).add( _v2$3 ) );
  3290. this.expandByPoint( _v1$6.copy( sphere.center ).sub( _v2$3 ) );
  3291. }
  3292. return this;
  3293. }
  3294. equals( sphere ) {
  3295. return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
  3296. }
  3297. clone() {
  3298. return new this.constructor().copy( this );
  3299. }
  3300. }
  3301. const _vector$a = /*@__PURE__*/ new Vector3();
  3302. const _segCenter = /*@__PURE__*/ new Vector3();
  3303. const _segDir = /*@__PURE__*/ new Vector3();
  3304. const _diff = /*@__PURE__*/ new Vector3();
  3305. const _edge1 = /*@__PURE__*/ new Vector3();
  3306. const _edge2 = /*@__PURE__*/ new Vector3();
  3307. const _normal$1 = /*@__PURE__*/ new Vector3();
  3308. class Ray {
  3309. constructor( origin = new Vector3(), direction = new Vector3( 0, 0, - 1 ) ) {
  3310. this.origin = origin;
  3311. this.direction = direction;
  3312. }
  3313. set( origin, direction ) {
  3314. this.origin.copy( origin );
  3315. this.direction.copy( direction );
  3316. return this;
  3317. }
  3318. copy( ray ) {
  3319. this.origin.copy( ray.origin );
  3320. this.direction.copy( ray.direction );
  3321. return this;
  3322. }
  3323. at( t, target ) {
  3324. return target.copy( this.origin ).addScaledVector( this.direction, t );
  3325. }
  3326. lookAt( v ) {
  3327. this.direction.copy( v ).sub( this.origin ).normalize();
  3328. return this;
  3329. }
  3330. recast( t ) {
  3331. this.origin.copy( this.at( t, _vector$a ) );
  3332. return this;
  3333. }
  3334. closestPointToPoint( point, target ) {
  3335. target.subVectors( point, this.origin );
  3336. const directionDistance = target.dot( this.direction );
  3337. if ( directionDistance < 0 ) {
  3338. return target.copy( this.origin );
  3339. }
  3340. return target.copy( this.origin ).addScaledVector( this.direction, directionDistance );
  3341. }
  3342. distanceToPoint( point ) {
  3343. return Math.sqrt( this.distanceSqToPoint( point ) );
  3344. }
  3345. distanceSqToPoint( point ) {
  3346. const directionDistance = _vector$a.subVectors( point, this.origin ).dot( this.direction );
  3347. // point behind the ray
  3348. if ( directionDistance < 0 ) {
  3349. return this.origin.distanceToSquared( point );
  3350. }
  3351. _vector$a.copy( this.origin ).addScaledVector( this.direction, directionDistance );
  3352. return _vector$a.distanceToSquared( point );
  3353. }
  3354. distanceSqToSegment( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
  3355. // from https://github.com/pmjoniak/GeometricTools/blob/master/GTEngine/Include/Mathematics/GteDistRaySegment.h
  3356. // It returns the min distance between the ray and the segment
  3357. // defined by v0 and v1
  3358. // It can also set two optional targets :
  3359. // - The closest point on the ray
  3360. // - The closest point on the segment
  3361. _segCenter.copy( v0 ).add( v1 ).multiplyScalar( 0.5 );
  3362. _segDir.copy( v1 ).sub( v0 ).normalize();
  3363. _diff.copy( this.origin ).sub( _segCenter );
  3364. const segExtent = v0.distanceTo( v1 ) * 0.5;
  3365. const a01 = - this.direction.dot( _segDir );
  3366. const b0 = _diff.dot( this.direction );
  3367. const b1 = - _diff.dot( _segDir );
  3368. const c = _diff.lengthSq();
  3369. const det = Math.abs( 1 - a01 * a01 );
  3370. let s0, s1, sqrDist, extDet;
  3371. if ( det > 0 ) {
  3372. // The ray and segment are not parallel.
  3373. s0 = a01 * b1 - b0;
  3374. s1 = a01 * b0 - b1;
  3375. extDet = segExtent * det;
  3376. if ( s0 >= 0 ) {
  3377. if ( s1 >= - extDet ) {
  3378. if ( s1 <= extDet ) {
  3379. // region 0
  3380. // Minimum at interior points of ray and segment.
  3381. const invDet = 1 / det;
  3382. s0 *= invDet;
  3383. s1 *= invDet;
  3384. sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c;
  3385. } else {
  3386. // region 1
  3387. s1 = segExtent;
  3388. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3389. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3390. }
  3391. } else {
  3392. // region 5
  3393. s1 = - segExtent;
  3394. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3395. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3396. }
  3397. } else {
  3398. if ( s1 <= - extDet ) {
  3399. // region 4
  3400. s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) );
  3401. s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3402. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3403. } else if ( s1 <= extDet ) {
  3404. // region 3
  3405. s0 = 0;
  3406. s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3407. sqrDist = s1 * ( s1 + 2 * b1 ) + c;
  3408. } else {
  3409. // region 2
  3410. s0 = Math.max( 0, - ( a01 * segExtent + b0 ) );
  3411. s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3412. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3413. }
  3414. }
  3415. } else {
  3416. // Ray and segment are parallel.
  3417. s1 = ( a01 > 0 ) ? - segExtent : segExtent;
  3418. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3419. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3420. }
  3421. if ( optionalPointOnRay ) {
  3422. optionalPointOnRay.copy( this.origin ).addScaledVector( this.direction, s0 );
  3423. }
  3424. if ( optionalPointOnSegment ) {
  3425. optionalPointOnSegment.copy( _segCenter ).addScaledVector( _segDir, s1 );
  3426. }
  3427. return sqrDist;
  3428. }
  3429. intersectSphere( sphere, target ) {
  3430. _vector$a.subVectors( sphere.center, this.origin );
  3431. const tca = _vector$a.dot( this.direction );
  3432. const d2 = _vector$a.dot( _vector$a ) - tca * tca;
  3433. const radius2 = sphere.radius * sphere.radius;
  3434. if ( d2 > radius2 ) return null;
  3435. const thc = Math.sqrt( radius2 - d2 );
  3436. // t0 = first intersect point - entrance on front of sphere
  3437. const t0 = tca - thc;
  3438. // t1 = second intersect point - exit point on back of sphere
  3439. const t1 = tca + thc;
  3440. // test to see if t1 is behind the ray - if so, return null
  3441. if ( t1 < 0 ) return null;
  3442. // test to see if t0 is behind the ray:
  3443. // if it is, the ray is inside the sphere, so return the second exit point scaled by t1,
  3444. // in order to always return an intersect point that is in front of the ray.
  3445. if ( t0 < 0 ) return this.at( t1, target );
  3446. // else t0 is in front of the ray, so return the first collision point scaled by t0
  3447. return this.at( t0, target );
  3448. }
  3449. intersectsSphere( sphere ) {
  3450. return this.distanceSqToPoint( sphere.center ) <= ( sphere.radius * sphere.radius );
  3451. }
  3452. distanceToPlane( plane ) {
  3453. const denominator = plane.normal.dot( this.direction );
  3454. if ( denominator === 0 ) {
  3455. // line is coplanar, return origin
  3456. if ( plane.distanceToPoint( this.origin ) === 0 ) {
  3457. return 0;
  3458. }
  3459. // Null is preferable to undefined since undefined means.... it is undefined
  3460. return null;
  3461. }
  3462. const t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator;
  3463. // Return if the ray never intersects the plane
  3464. return t >= 0 ? t : null;
  3465. }
  3466. intersectPlane( plane, target ) {
  3467. const t = this.distanceToPlane( plane );
  3468. if ( t === null ) {
  3469. return null;
  3470. }
  3471. return this.at( t, target );
  3472. }
  3473. intersectsPlane( plane ) {
  3474. // check if the ray lies on the plane first
  3475. const distToPoint = plane.distanceToPoint( this.origin );
  3476. if ( distToPoint === 0 ) {
  3477. return true;
  3478. }
  3479. const denominator = plane.normal.dot( this.direction );
  3480. if ( denominator * distToPoint < 0 ) {
  3481. return true;
  3482. }
  3483. // ray origin is behind the plane (and is pointing behind it)
  3484. return false;
  3485. }
  3486. intersectBox( box, target ) {
  3487. let tmin, tmax, tymin, tymax, tzmin, tzmax;
  3488. const invdirx = 1 / this.direction.x,
  3489. invdiry = 1 / this.direction.y,
  3490. invdirz = 1 / this.direction.z;
  3491. const origin = this.origin;
  3492. if ( invdirx >= 0 ) {
  3493. tmin = ( box.min.x - origin.x ) * invdirx;
  3494. tmax = ( box.max.x - origin.x ) * invdirx;
  3495. } else {
  3496. tmin = ( box.max.x - origin.x ) * invdirx;
  3497. tmax = ( box.min.x - origin.x ) * invdirx;
  3498. }
  3499. if ( invdiry >= 0 ) {
  3500. tymin = ( box.min.y - origin.y ) * invdiry;
  3501. tymax = ( box.max.y - origin.y ) * invdiry;
  3502. } else {
  3503. tymin = ( box.max.y - origin.y ) * invdiry;
  3504. tymax = ( box.min.y - origin.y ) * invdiry;
  3505. }
  3506. if ( ( tmin > tymax ) || ( tymin > tmax ) ) return null;
  3507. if ( tymin > tmin || isNaN( tmin ) ) tmin = tymin;
  3508. if ( tymax < tmax || isNaN( tmax ) ) tmax = tymax;
  3509. if ( invdirz >= 0 ) {
  3510. tzmin = ( box.min.z - origin.z ) * invdirz;
  3511. tzmax = ( box.max.z - origin.z ) * invdirz;
  3512. } else {
  3513. tzmin = ( box.max.z - origin.z ) * invdirz;
  3514. tzmax = ( box.min.z - origin.z ) * invdirz;
  3515. }
  3516. if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) return null;
  3517. if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin;
  3518. if ( tzmax < tmax || tmax !== tmax ) tmax = tzmax;
  3519. //return point closest to the ray (positive side)
  3520. if ( tmax < 0 ) return null;
  3521. return this.at( tmin >= 0 ? tmin : tmax, target );
  3522. }
  3523. intersectsBox( box ) {
  3524. return this.intersectBox( box, _vector$a ) !== null;
  3525. }
  3526. intersectTriangle( a, b, c, backfaceCulling, target ) {
  3527. // Compute the offset origin, edges, and normal.
  3528. // from https://github.com/pmjoniak/GeometricTools/blob/master/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h
  3529. _edge1.subVectors( b, a );
  3530. _edge2.subVectors( c, a );
  3531. _normal$1.crossVectors( _edge1, _edge2 );
  3532. // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
  3533. // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
  3534. // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
  3535. // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
  3536. // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
  3537. let DdN = this.direction.dot( _normal$1 );
  3538. let sign;
  3539. if ( DdN > 0 ) {
  3540. if ( backfaceCulling ) return null;
  3541. sign = 1;
  3542. } else if ( DdN < 0 ) {
  3543. sign = - 1;
  3544. DdN = - DdN;
  3545. } else {
  3546. return null;
  3547. }
  3548. _diff.subVectors( this.origin, a );
  3549. const DdQxE2 = sign * this.direction.dot( _edge2.crossVectors( _diff, _edge2 ) );
  3550. // b1 < 0, no intersection
  3551. if ( DdQxE2 < 0 ) {
  3552. return null;
  3553. }
  3554. const DdE1xQ = sign * this.direction.dot( _edge1.cross( _diff ) );
  3555. // b2 < 0, no intersection
  3556. if ( DdE1xQ < 0 ) {
  3557. return null;
  3558. }
  3559. // b1+b2 > 1, no intersection
  3560. if ( DdQxE2 + DdE1xQ > DdN ) {
  3561. return null;
  3562. }
  3563. // Line intersects triangle, check if ray does.
  3564. const QdN = - sign * _diff.dot( _normal$1 );
  3565. // t < 0, no intersection
  3566. if ( QdN < 0 ) {
  3567. return null;
  3568. }
  3569. // Ray intersects triangle.
  3570. return this.at( QdN / DdN, target );
  3571. }
  3572. applyMatrix4( matrix4 ) {
  3573. this.origin.applyMatrix4( matrix4 );
  3574. this.direction.transformDirection( matrix4 );
  3575. return this;
  3576. }
  3577. equals( ray ) {
  3578. return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
  3579. }
  3580. clone() {
  3581. return new this.constructor().copy( this );
  3582. }
  3583. }
  3584. class Matrix4 {
  3585. constructor( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  3586. Matrix4.prototype.isMatrix4 = true;
  3587. this.elements = [
  3588. 1, 0, 0, 0,
  3589. 0, 1, 0, 0,
  3590. 0, 0, 1, 0,
  3591. 0, 0, 0, 1
  3592. ];
  3593. if ( n11 !== undefined ) {
  3594. this.set( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 );
  3595. }
  3596. }
  3597. set( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  3598. const te = this.elements;
  3599. te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14;
  3600. te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24;
  3601. te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34;
  3602. te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44;
  3603. return this;
  3604. }
  3605. identity() {
  3606. this.set(
  3607. 1, 0, 0, 0,
  3608. 0, 1, 0, 0,
  3609. 0, 0, 1, 0,
  3610. 0, 0, 0, 1
  3611. );
  3612. return this;
  3613. }
  3614. clone() {
  3615. return new Matrix4().fromArray( this.elements );
  3616. }
  3617. copy( m ) {
  3618. const te = this.elements;
  3619. const me = m.elements;
  3620. te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ]; te[ 3 ] = me[ 3 ];
  3621. te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ]; te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ];
  3622. te[ 8 ] = me[ 8 ]; te[ 9 ] = me[ 9 ]; te[ 10 ] = me[ 10 ]; te[ 11 ] = me[ 11 ];
  3623. te[ 12 ] = me[ 12 ]; te[ 13 ] = me[ 13 ]; te[ 14 ] = me[ 14 ]; te[ 15 ] = me[ 15 ];
  3624. return this;
  3625. }
  3626. copyPosition( m ) {
  3627. const te = this.elements, me = m.elements;
  3628. te[ 12 ] = me[ 12 ];
  3629. te[ 13 ] = me[ 13 ];
  3630. te[ 14 ] = me[ 14 ];
  3631. return this;
  3632. }
  3633. setFromMatrix3( m ) {
  3634. const me = m.elements;
  3635. this.set(
  3636. me[ 0 ], me[ 3 ], me[ 6 ], 0,
  3637. me[ 1 ], me[ 4 ], me[ 7 ], 0,
  3638. me[ 2 ], me[ 5 ], me[ 8 ], 0,
  3639. 0, 0, 0, 1
  3640. );
  3641. return this;
  3642. }
  3643. extractBasis( xAxis, yAxis, zAxis ) {
  3644. xAxis.setFromMatrixColumn( this, 0 );
  3645. yAxis.setFromMatrixColumn( this, 1 );
  3646. zAxis.setFromMatrixColumn( this, 2 );
  3647. return this;
  3648. }
  3649. makeBasis( xAxis, yAxis, zAxis ) {
  3650. this.set(
  3651. xAxis.x, yAxis.x, zAxis.x, 0,
  3652. xAxis.y, yAxis.y, zAxis.y, 0,
  3653. xAxis.z, yAxis.z, zAxis.z, 0,
  3654. 0, 0, 0, 1
  3655. );
  3656. return this;
  3657. }
  3658. extractRotation( m ) {
  3659. // this method does not support reflection matrices
  3660. const te = this.elements;
  3661. const me = m.elements;
  3662. const scaleX = 1 / _v1$5.setFromMatrixColumn( m, 0 ).length();
  3663. const scaleY = 1 / _v1$5.setFromMatrixColumn( m, 1 ).length();
  3664. const scaleZ = 1 / _v1$5.setFromMatrixColumn( m, 2 ).length();
  3665. te[ 0 ] = me[ 0 ] * scaleX;
  3666. te[ 1 ] = me[ 1 ] * scaleX;
  3667. te[ 2 ] = me[ 2 ] * scaleX;
  3668. te[ 3 ] = 0;
  3669. te[ 4 ] = me[ 4 ] * scaleY;
  3670. te[ 5 ] = me[ 5 ] * scaleY;
  3671. te[ 6 ] = me[ 6 ] * scaleY;
  3672. te[ 7 ] = 0;
  3673. te[ 8 ] = me[ 8 ] * scaleZ;
  3674. te[ 9 ] = me[ 9 ] * scaleZ;
  3675. te[ 10 ] = me[ 10 ] * scaleZ;
  3676. te[ 11 ] = 0;
  3677. te[ 12 ] = 0;
  3678. te[ 13 ] = 0;
  3679. te[ 14 ] = 0;
  3680. te[ 15 ] = 1;
  3681. return this;
  3682. }
  3683. makeRotationFromEuler( euler ) {
  3684. const te = this.elements;
  3685. const x = euler.x, y = euler.y, z = euler.z;
  3686. const a = Math.cos( x ), b = Math.sin( x );
  3687. const c = Math.cos( y ), d = Math.sin( y );
  3688. const e = Math.cos( z ), f = Math.sin( z );
  3689. if ( euler.order === 'XYZ' ) {
  3690. const ae = a * e, af = a * f, be = b * e, bf = b * f;
  3691. te[ 0 ] = c * e;
  3692. te[ 4 ] = - c * f;
  3693. te[ 8 ] = d;
  3694. te[ 1 ] = af + be * d;
  3695. te[ 5 ] = ae - bf * d;
  3696. te[ 9 ] = - b * c;
  3697. te[ 2 ] = bf - ae * d;
  3698. te[ 6 ] = be + af * d;
  3699. te[ 10 ] = a * c;
  3700. } else if ( euler.order === 'YXZ' ) {
  3701. const ce = c * e, cf = c * f, de = d * e, df = d * f;
  3702. te[ 0 ] = ce + df * b;
  3703. te[ 4 ] = de * b - cf;
  3704. te[ 8 ] = a * d;
  3705. te[ 1 ] = a * f;
  3706. te[ 5 ] = a * e;
  3707. te[ 9 ] = - b;
  3708. te[ 2 ] = cf * b - de;
  3709. te[ 6 ] = df + ce * b;
  3710. te[ 10 ] = a * c;
  3711. } else if ( euler.order === 'ZXY' ) {
  3712. const ce = c * e, cf = c * f, de = d * e, df = d * f;
  3713. te[ 0 ] = ce - df * b;
  3714. te[ 4 ] = - a * f;
  3715. te[ 8 ] = de + cf * b;
  3716. te[ 1 ] = cf + de * b;
  3717. te[ 5 ] = a * e;
  3718. te[ 9 ] = df - ce * b;
  3719. te[ 2 ] = - a * d;
  3720. te[ 6 ] = b;
  3721. te[ 10 ] = a * c;
  3722. } else if ( euler.order === 'ZYX' ) {
  3723. const ae = a * e, af = a * f, be = b * e, bf = b * f;
  3724. te[ 0 ] = c * e;
  3725. te[ 4 ] = be * d - af;
  3726. te[ 8 ] = ae * d + bf;
  3727. te[ 1 ] = c * f;
  3728. te[ 5 ] = bf * d + ae;
  3729. te[ 9 ] = af * d - be;
  3730. te[ 2 ] = - d;
  3731. te[ 6 ] = b * c;
  3732. te[ 10 ] = a * c;
  3733. } else if ( euler.order === 'YZX' ) {
  3734. const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  3735. te[ 0 ] = c * e;
  3736. te[ 4 ] = bd - ac * f;
  3737. te[ 8 ] = bc * f + ad;
  3738. te[ 1 ] = f;
  3739. te[ 5 ] = a * e;
  3740. te[ 9 ] = - b * e;
  3741. te[ 2 ] = - d * e;
  3742. te[ 6 ] = ad * f + bc;
  3743. te[ 10 ] = ac - bd * f;
  3744. } else if ( euler.order === 'XZY' ) {
  3745. const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  3746. te[ 0 ] = c * e;
  3747. te[ 4 ] = - f;
  3748. te[ 8 ] = d * e;
  3749. te[ 1 ] = ac * f + bd;
  3750. te[ 5 ] = a * e;
  3751. te[ 9 ] = ad * f - bc;
  3752. te[ 2 ] = bc * f - ad;
  3753. te[ 6 ] = b * e;
  3754. te[ 10 ] = bd * f + ac;
  3755. }
  3756. // bottom row
  3757. te[ 3 ] = 0;
  3758. te[ 7 ] = 0;
  3759. te[ 11 ] = 0;
  3760. // last column
  3761. te[ 12 ] = 0;
  3762. te[ 13 ] = 0;
  3763. te[ 14 ] = 0;
  3764. te[ 15 ] = 1;
  3765. return this;
  3766. }
  3767. makeRotationFromQuaternion( q ) {
  3768. return this.compose( _zero, q, _one );
  3769. }
  3770. lookAt( eye, target, up ) {
  3771. const te = this.elements;
  3772. _z.subVectors( eye, target );
  3773. if ( _z.lengthSq() === 0 ) {
  3774. // eye and target are in the same position
  3775. _z.z = 1;
  3776. }
  3777. _z.normalize();
  3778. _x.crossVectors( up, _z );
  3779. if ( _x.lengthSq() === 0 ) {
  3780. // up and z are parallel
  3781. if ( Math.abs( up.z ) === 1 ) {
  3782. _z.x += 0.0001;
  3783. } else {
  3784. _z.z += 0.0001;
  3785. }
  3786. _z.normalize();
  3787. _x.crossVectors( up, _z );
  3788. }
  3789. _x.normalize();
  3790. _y.crossVectors( _z, _x );
  3791. te[ 0 ] = _x.x; te[ 4 ] = _y.x; te[ 8 ] = _z.x;
  3792. te[ 1 ] = _x.y; te[ 5 ] = _y.y; te[ 9 ] = _z.y;
  3793. te[ 2 ] = _x.z; te[ 6 ] = _y.z; te[ 10 ] = _z.z;
  3794. return this;
  3795. }
  3796. multiply( m ) {
  3797. return this.multiplyMatrices( this, m );
  3798. }
  3799. premultiply( m ) {
  3800. return this.multiplyMatrices( m, this );
  3801. }
  3802. multiplyMatrices( a, b ) {
  3803. const ae = a.elements;
  3804. const be = b.elements;
  3805. const te = this.elements;
  3806. const a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ];
  3807. const a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ];
  3808. const a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ];
  3809. const a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ];
  3810. const b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ];
  3811. const b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ];
  3812. const b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ];
  3813. const b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ];
  3814. te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  3815. te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  3816. te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  3817. te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  3818. te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  3819. te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  3820. te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  3821. te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  3822. te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  3823. te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  3824. te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  3825. te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  3826. te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  3827. te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  3828. te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  3829. te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  3830. return this;
  3831. }
  3832. multiplyScalar( s ) {
  3833. const te = this.elements;
  3834. te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s;
  3835. te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s;
  3836. te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s;
  3837. te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s;
  3838. return this;
  3839. }
  3840. determinant() {
  3841. const te = this.elements;
  3842. const n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ];
  3843. const n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ];
  3844. const n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ];
  3845. const n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ];
  3846. //TODO: make this more efficient
  3847. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  3848. return (
  3849. n41 * (
  3850. + n14 * n23 * n32
  3851. - n13 * n24 * n32
  3852. - n14 * n22 * n33
  3853. + n12 * n24 * n33
  3854. + n13 * n22 * n34
  3855. - n12 * n23 * n34
  3856. ) +
  3857. n42 * (
  3858. + n11 * n23 * n34
  3859. - n11 * n24 * n33
  3860. + n14 * n21 * n33
  3861. - n13 * n21 * n34
  3862. + n13 * n24 * n31
  3863. - n14 * n23 * n31
  3864. ) +
  3865. n43 * (
  3866. + n11 * n24 * n32
  3867. - n11 * n22 * n34
  3868. - n14 * n21 * n32
  3869. + n12 * n21 * n34
  3870. + n14 * n22 * n31
  3871. - n12 * n24 * n31
  3872. ) +
  3873. n44 * (
  3874. - n13 * n22 * n31
  3875. - n11 * n23 * n32
  3876. + n11 * n22 * n33
  3877. + n13 * n21 * n32
  3878. - n12 * n21 * n33
  3879. + n12 * n23 * n31
  3880. )
  3881. );
  3882. }
  3883. transpose() {
  3884. const te = this.elements;
  3885. let tmp;
  3886. tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp;
  3887. tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp;
  3888. tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp;
  3889. tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp;
  3890. tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp;
  3891. tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp;
  3892. return this;
  3893. }
  3894. setPosition( x, y, z ) {
  3895. const te = this.elements;
  3896. if ( x.isVector3 ) {
  3897. te[ 12 ] = x.x;
  3898. te[ 13 ] = x.y;
  3899. te[ 14 ] = x.z;
  3900. } else {
  3901. te[ 12 ] = x;
  3902. te[ 13 ] = y;
  3903. te[ 14 ] = z;
  3904. }
  3905. return this;
  3906. }
  3907. invert() {
  3908. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  3909. const te = this.elements,
  3910. n11 = te[ 0 ], n21 = te[ 1 ], n31 = te[ 2 ], n41 = te[ 3 ],
  3911. n12 = te[ 4 ], n22 = te[ 5 ], n32 = te[ 6 ], n42 = te[ 7 ],
  3912. n13 = te[ 8 ], n23 = te[ 9 ], n33 = te[ 10 ], n43 = te[ 11 ],
  3913. n14 = te[ 12 ], n24 = te[ 13 ], n34 = te[ 14 ], n44 = te[ 15 ],
  3914. t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44,
  3915. t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44,
  3916. t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44,
  3917. t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
  3918. const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
  3919. if ( det === 0 ) return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  3920. const detInv = 1 / det;
  3921. te[ 0 ] = t11 * detInv;
  3922. te[ 1 ] = ( n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44 ) * detInv;
  3923. te[ 2 ] = ( n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44 ) * detInv;
  3924. te[ 3 ] = ( n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43 ) * detInv;
  3925. te[ 4 ] = t12 * detInv;
  3926. te[ 5 ] = ( n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44 ) * detInv;
  3927. te[ 6 ] = ( n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44 ) * detInv;
  3928. te[ 7 ] = ( n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43 ) * detInv;
  3929. te[ 8 ] = t13 * detInv;
  3930. te[ 9 ] = ( n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44 ) * detInv;
  3931. te[ 10 ] = ( n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44 ) * detInv;
  3932. te[ 11 ] = ( n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43 ) * detInv;
  3933. te[ 12 ] = t14 * detInv;
  3934. te[ 13 ] = ( n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34 ) * detInv;
  3935. te[ 14 ] = ( n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34 ) * detInv;
  3936. te[ 15 ] = ( n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33 ) * detInv;
  3937. return this;
  3938. }
  3939. scale( v ) {
  3940. const te = this.elements;
  3941. const x = v.x, y = v.y, z = v.z;
  3942. te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z;
  3943. te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z;
  3944. te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z;
  3945. te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z;
  3946. return this;
  3947. }
  3948. getMaxScaleOnAxis() {
  3949. const te = this.elements;
  3950. const scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ];
  3951. const scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ];
  3952. const scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ];
  3953. return Math.sqrt( Math.max( scaleXSq, scaleYSq, scaleZSq ) );
  3954. }
  3955. makeTranslation( x, y, z ) {
  3956. if ( x.isVector3 ) {
  3957. this.set(
  3958. 1, 0, 0, x.x,
  3959. 0, 1, 0, x.y,
  3960. 0, 0, 1, x.z,
  3961. 0, 0, 0, 1
  3962. );
  3963. } else {
  3964. this.set(
  3965. 1, 0, 0, x,
  3966. 0, 1, 0, y,
  3967. 0, 0, 1, z,
  3968. 0, 0, 0, 1
  3969. );
  3970. }
  3971. return this;
  3972. }
  3973. makeRotationX( theta ) {
  3974. const c = Math.cos( theta ), s = Math.sin( theta );
  3975. this.set(
  3976. 1, 0, 0, 0,
  3977. 0, c, - s, 0,
  3978. 0, s, c, 0,
  3979. 0, 0, 0, 1
  3980. );
  3981. return this;
  3982. }
  3983. makeRotationY( theta ) {
  3984. const c = Math.cos( theta ), s = Math.sin( theta );
  3985. this.set(
  3986. c, 0, s, 0,
  3987. 0, 1, 0, 0,
  3988. - s, 0, c, 0,
  3989. 0, 0, 0, 1
  3990. );
  3991. return this;
  3992. }
  3993. makeRotationZ( theta ) {
  3994. const c = Math.cos( theta ), s = Math.sin( theta );
  3995. this.set(
  3996. c, - s, 0, 0,
  3997. s, c, 0, 0,
  3998. 0, 0, 1, 0,
  3999. 0, 0, 0, 1
  4000. );
  4001. return this;
  4002. }
  4003. makeRotationAxis( axis, angle ) {
  4004. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  4005. const c = Math.cos( angle );
  4006. const s = Math.sin( angle );
  4007. const t = 1 - c;
  4008. const x = axis.x, y = axis.y, z = axis.z;
  4009. const tx = t * x, ty = t * y;
  4010. this.set(
  4011. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  4012. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  4013. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  4014. 0, 0, 0, 1
  4015. );
  4016. return this;
  4017. }
  4018. makeScale( x, y, z ) {
  4019. this.set(
  4020. x, 0, 0, 0,
  4021. 0, y, 0, 0,
  4022. 0, 0, z, 0,
  4023. 0, 0, 0, 1
  4024. );
  4025. return this;
  4026. }
  4027. makeShear( xy, xz, yx, yz, zx, zy ) {
  4028. this.set(
  4029. 1, yx, zx, 0,
  4030. xy, 1, zy, 0,
  4031. xz, yz, 1, 0,
  4032. 0, 0, 0, 1
  4033. );
  4034. return this;
  4035. }
  4036. compose( position, quaternion, scale ) {
  4037. const te = this.elements;
  4038. const x = quaternion._x, y = quaternion._y, z = quaternion._z, w = quaternion._w;
  4039. const x2 = x + x, y2 = y + y, z2 = z + z;
  4040. const xx = x * x2, xy = x * y2, xz = x * z2;
  4041. const yy = y * y2, yz = y * z2, zz = z * z2;
  4042. const wx = w * x2, wy = w * y2, wz = w * z2;
  4043. const sx = scale.x, sy = scale.y, sz = scale.z;
  4044. te[ 0 ] = ( 1 - ( yy + zz ) ) * sx;
  4045. te[ 1 ] = ( xy + wz ) * sx;
  4046. te[ 2 ] = ( xz - wy ) * sx;
  4047. te[ 3 ] = 0;
  4048. te[ 4 ] = ( xy - wz ) * sy;
  4049. te[ 5 ] = ( 1 - ( xx + zz ) ) * sy;
  4050. te[ 6 ] = ( yz + wx ) * sy;
  4051. te[ 7 ] = 0;
  4052. te[ 8 ] = ( xz + wy ) * sz;
  4053. te[ 9 ] = ( yz - wx ) * sz;
  4054. te[ 10 ] = ( 1 - ( xx + yy ) ) * sz;
  4055. te[ 11 ] = 0;
  4056. te[ 12 ] = position.x;
  4057. te[ 13 ] = position.y;
  4058. te[ 14 ] = position.z;
  4059. te[ 15 ] = 1;
  4060. return this;
  4061. }
  4062. decompose( position, quaternion, scale ) {
  4063. const te = this.elements;
  4064. let sx = _v1$5.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();
  4065. const sy = _v1$5.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length();
  4066. const sz = _v1$5.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length();
  4067. // if determine is negative, we need to invert one scale
  4068. const det = this.determinant();
  4069. if ( det < 0 ) sx = - sx;
  4070. position.x = te[ 12 ];
  4071. position.y = te[ 13 ];
  4072. position.z = te[ 14 ];
  4073. // scale the rotation part
  4074. _m1$2.copy( this );
  4075. const invSX = 1 / sx;
  4076. const invSY = 1 / sy;
  4077. const invSZ = 1 / sz;
  4078. _m1$2.elements[ 0 ] *= invSX;
  4079. _m1$2.elements[ 1 ] *= invSX;
  4080. _m1$2.elements[ 2 ] *= invSX;
  4081. _m1$2.elements[ 4 ] *= invSY;
  4082. _m1$2.elements[ 5 ] *= invSY;
  4083. _m1$2.elements[ 6 ] *= invSY;
  4084. _m1$2.elements[ 8 ] *= invSZ;
  4085. _m1$2.elements[ 9 ] *= invSZ;
  4086. _m1$2.elements[ 10 ] *= invSZ;
  4087. quaternion.setFromRotationMatrix( _m1$2 );
  4088. scale.x = sx;
  4089. scale.y = sy;
  4090. scale.z = sz;
  4091. return this;
  4092. }
  4093. makePerspective( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem ) {
  4094. const te = this.elements;
  4095. const x = 2 * near / ( right - left );
  4096. const y = 2 * near / ( top - bottom );
  4097. const a = ( right + left ) / ( right - left );
  4098. const b = ( top + bottom ) / ( top - bottom );
  4099. let c, d;
  4100. if ( coordinateSystem === WebGLCoordinateSystem ) {
  4101. c = - ( far + near ) / ( far - near );
  4102. d = ( - 2 * far * near ) / ( far - near );
  4103. } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
  4104. c = - far / ( far - near );
  4105. d = ( - far * near ) / ( far - near );
  4106. } else {
  4107. throw new Error( 'THREE.Matrix4.makePerspective(): Invalid coordinate system: ' + coordinateSystem );
  4108. }
  4109. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0;
  4110. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0;
  4111. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  4112. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0;
  4113. return this;
  4114. }
  4115. makeOrthographic( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem ) {
  4116. const te = this.elements;
  4117. const w = 1.0 / ( right - left );
  4118. const h = 1.0 / ( top - bottom );
  4119. const p = 1.0 / ( far - near );
  4120. const x = ( right + left ) * w;
  4121. const y = ( top + bottom ) * h;
  4122. let z, zInv;
  4123. if ( coordinateSystem === WebGLCoordinateSystem ) {
  4124. z = ( far + near ) * p;
  4125. zInv = - 2 * p;
  4126. } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
  4127. z = near * p;
  4128. zInv = - 1 * p;
  4129. } else {
  4130. throw new Error( 'THREE.Matrix4.makeOrthographic(): Invalid coordinate system: ' + coordinateSystem );
  4131. }
  4132. te[ 0 ] = 2 * w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x;
  4133. te[ 1 ] = 0; te[ 5 ] = 2 * h; te[ 9 ] = 0; te[ 13 ] = - y;
  4134. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = zInv; te[ 14 ] = - z;
  4135. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1;
  4136. return this;
  4137. }
  4138. equals( matrix ) {
  4139. const te = this.elements;
  4140. const me = matrix.elements;
  4141. for ( let i = 0; i < 16; i ++ ) {
  4142. if ( te[ i ] !== me[ i ] ) return false;
  4143. }
  4144. return true;
  4145. }
  4146. fromArray( array, offset = 0 ) {
  4147. for ( let i = 0; i < 16; i ++ ) {
  4148. this.elements[ i ] = array[ i + offset ];
  4149. }
  4150. return this;
  4151. }
  4152. toArray( array = [], offset = 0 ) {
  4153. const te = this.elements;
  4154. array[ offset ] = te[ 0 ];
  4155. array[ offset + 1 ] = te[ 1 ];
  4156. array[ offset + 2 ] = te[ 2 ];
  4157. array[ offset + 3 ] = te[ 3 ];
  4158. array[ offset + 4 ] = te[ 4 ];
  4159. array[ offset + 5 ] = te[ 5 ];
  4160. array[ offset + 6 ] = te[ 6 ];
  4161. array[ offset + 7 ] = te[ 7 ];
  4162. array[ offset + 8 ] = te[ 8 ];
  4163. array[ offset + 9 ] = te[ 9 ];
  4164. array[ offset + 10 ] = te[ 10 ];
  4165. array[ offset + 11 ] = te[ 11 ];
  4166. array[ offset + 12 ] = te[ 12 ];
  4167. array[ offset + 13 ] = te[ 13 ];
  4168. array[ offset + 14 ] = te[ 14 ];
  4169. array[ offset + 15 ] = te[ 15 ];
  4170. return array;
  4171. }
  4172. }
  4173. const _v1$5 = /*@__PURE__*/ new Vector3();
  4174. const _m1$2 = /*@__PURE__*/ new Matrix4();
  4175. const _zero = /*@__PURE__*/ new Vector3( 0, 0, 0 );
  4176. const _one = /*@__PURE__*/ new Vector3( 1, 1, 1 );
  4177. const _x = /*@__PURE__*/ new Vector3();
  4178. const _y = /*@__PURE__*/ new Vector3();
  4179. const _z = /*@__PURE__*/ new Vector3();
  4180. const _matrix$2 = /*@__PURE__*/ new Matrix4();
  4181. const _quaternion$3 = /*@__PURE__*/ new Quaternion();
  4182. class Euler {
  4183. constructor( x = 0, y = 0, z = 0, order = Euler.DEFAULT_ORDER ) {
  4184. this.isEuler = true;
  4185. this._x = x;
  4186. this._y = y;
  4187. this._z = z;
  4188. this._order = order;
  4189. }
  4190. get x() {
  4191. return this._x;
  4192. }
  4193. set x( value ) {
  4194. this._x = value;
  4195. this._onChangeCallback();
  4196. }
  4197. get y() {
  4198. return this._y;
  4199. }
  4200. set y( value ) {
  4201. this._y = value;
  4202. this._onChangeCallback();
  4203. }
  4204. get z() {
  4205. return this._z;
  4206. }
  4207. set z( value ) {
  4208. this._z = value;
  4209. this._onChangeCallback();
  4210. }
  4211. get order() {
  4212. return this._order;
  4213. }
  4214. set order( value ) {
  4215. this._order = value;
  4216. this._onChangeCallback();
  4217. }
  4218. set( x, y, z, order = this._order ) {
  4219. this._x = x;
  4220. this._y = y;
  4221. this._z = z;
  4222. this._order = order;
  4223. this._onChangeCallback();
  4224. return this;
  4225. }
  4226. clone() {
  4227. return new this.constructor( this._x, this._y, this._z, this._order );
  4228. }
  4229. copy( euler ) {
  4230. this._x = euler._x;
  4231. this._y = euler._y;
  4232. this._z = euler._z;
  4233. this._order = euler._order;
  4234. this._onChangeCallback();
  4235. return this;
  4236. }
  4237. setFromRotationMatrix( m, order = this._order, update = true ) {
  4238. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  4239. const te = m.elements;
  4240. const m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
  4241. const m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
  4242. const m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  4243. switch ( order ) {
  4244. case 'XYZ':
  4245. this._y = Math.asin( clamp( m13, - 1, 1 ) );
  4246. if ( Math.abs( m13 ) < 0.9999999 ) {
  4247. this._x = Math.atan2( - m23, m33 );
  4248. this._z = Math.atan2( - m12, m11 );
  4249. } else {
  4250. this._x = Math.atan2( m32, m22 );
  4251. this._z = 0;
  4252. }
  4253. break;
  4254. case 'YXZ':
  4255. this._x = Math.asin( - clamp( m23, - 1, 1 ) );
  4256. if ( Math.abs( m23 ) < 0.9999999 ) {
  4257. this._y = Math.atan2( m13, m33 );
  4258. this._z = Math.atan2( m21, m22 );
  4259. } else {
  4260. this._y = Math.atan2( - m31, m11 );
  4261. this._z = 0;
  4262. }
  4263. break;
  4264. case 'ZXY':
  4265. this._x = Math.asin( clamp( m32, - 1, 1 ) );
  4266. if ( Math.abs( m32 ) < 0.9999999 ) {
  4267. this._y = Math.atan2( - m31, m33 );
  4268. this._z = Math.atan2( - m12, m22 );
  4269. } else {
  4270. this._y = 0;
  4271. this._z = Math.atan2( m21, m11 );
  4272. }
  4273. break;
  4274. case 'ZYX':
  4275. this._y = Math.asin( - clamp( m31, - 1, 1 ) );
  4276. if ( Math.abs( m31 ) < 0.9999999 ) {
  4277. this._x = Math.atan2( m32, m33 );
  4278. this._z = Math.atan2( m21, m11 );
  4279. } else {
  4280. this._x = 0;
  4281. this._z = Math.atan2( - m12, m22 );
  4282. }
  4283. break;
  4284. case 'YZX':
  4285. this._z = Math.asin( clamp( m21, - 1, 1 ) );
  4286. if ( Math.abs( m21 ) < 0.9999999 ) {
  4287. this._x = Math.atan2( - m23, m22 );
  4288. this._y = Math.atan2( - m31, m11 );
  4289. } else {
  4290. this._x = 0;
  4291. this._y = Math.atan2( m13, m33 );
  4292. }
  4293. break;
  4294. case 'XZY':
  4295. this._z = Math.asin( - clamp( m12, - 1, 1 ) );
  4296. if ( Math.abs( m12 ) < 0.9999999 ) {
  4297. this._x = Math.atan2( m32, m22 );
  4298. this._y = Math.atan2( m13, m11 );
  4299. } else {
  4300. this._x = Math.atan2( - m23, m33 );
  4301. this._y = 0;
  4302. }
  4303. break;
  4304. default:
  4305. console.warn( 'THREE.Euler: .setFromRotationMatrix() encountered an unknown order: ' + order );
  4306. }
  4307. this._order = order;
  4308. if ( update === true ) this._onChangeCallback();
  4309. return this;
  4310. }
  4311. setFromQuaternion( q, order, update ) {
  4312. _matrix$2.makeRotationFromQuaternion( q );
  4313. return this.setFromRotationMatrix( _matrix$2, order, update );
  4314. }
  4315. setFromVector3( v, order = this._order ) {
  4316. return this.set( v.x, v.y, v.z, order );
  4317. }
  4318. reorder( newOrder ) {
  4319. // WARNING: this discards revolution information -bhouston
  4320. _quaternion$3.setFromEuler( this );
  4321. return this.setFromQuaternion( _quaternion$3, newOrder );
  4322. }
  4323. equals( euler ) {
  4324. return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
  4325. }
  4326. fromArray( array ) {
  4327. this._x = array[ 0 ];
  4328. this._y = array[ 1 ];
  4329. this._z = array[ 2 ];
  4330. if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
  4331. this._onChangeCallback();
  4332. return this;
  4333. }
  4334. toArray( array = [], offset = 0 ) {
  4335. array[ offset ] = this._x;
  4336. array[ offset + 1 ] = this._y;
  4337. array[ offset + 2 ] = this._z;
  4338. array[ offset + 3 ] = this._order;
  4339. return array;
  4340. }
  4341. _onChange( callback ) {
  4342. this._onChangeCallback = callback;
  4343. return this;
  4344. }
  4345. _onChangeCallback() {}
  4346. *[ Symbol.iterator ]() {
  4347. yield this._x;
  4348. yield this._y;
  4349. yield this._z;
  4350. yield this._order;
  4351. }
  4352. }
  4353. Euler.DEFAULT_ORDER = 'XYZ';
  4354. class Layers {
  4355. constructor() {
  4356. this.mask = 1 | 0;
  4357. }
  4358. set( channel ) {
  4359. this.mask = ( 1 << channel | 0 ) >>> 0;
  4360. }
  4361. enable( channel ) {
  4362. this.mask |= 1 << channel | 0;
  4363. }
  4364. enableAll() {
  4365. this.mask = 0xffffffff | 0;
  4366. }
  4367. toggle( channel ) {
  4368. this.mask ^= 1 << channel | 0;
  4369. }
  4370. disable( channel ) {
  4371. this.mask &= ~ ( 1 << channel | 0 );
  4372. }
  4373. disableAll() {
  4374. this.mask = 0;
  4375. }
  4376. test( layers ) {
  4377. return ( this.mask & layers.mask ) !== 0;
  4378. }
  4379. isEnabled( channel ) {
  4380. return ( this.mask & ( 1 << channel | 0 ) ) !== 0;
  4381. }
  4382. }
  4383. let _object3DId = 0;
  4384. const _v1$4 = /*@__PURE__*/ new Vector3();
  4385. const _q1 = /*@__PURE__*/ new Quaternion();
  4386. const _m1$1 = /*@__PURE__*/ new Matrix4();
  4387. const _target = /*@__PURE__*/ new Vector3();
  4388. const _position$3 = /*@__PURE__*/ new Vector3();
  4389. const _scale$2 = /*@__PURE__*/ new Vector3();
  4390. const _quaternion$2 = /*@__PURE__*/ new Quaternion();
  4391. const _xAxis = /*@__PURE__*/ new Vector3( 1, 0, 0 );
  4392. const _yAxis = /*@__PURE__*/ new Vector3( 0, 1, 0 );
  4393. const _zAxis = /*@__PURE__*/ new Vector3( 0, 0, 1 );
  4394. const _addedEvent = { type: 'added' };
  4395. const _removedEvent = { type: 'removed' };
  4396. const _childaddedEvent = { type: 'childadded', child: null };
  4397. const _childremovedEvent = { type: 'childremoved', child: null };
  4398. class Object3D extends EventDispatcher {
  4399. constructor() {
  4400. super();
  4401. this.isObject3D = true;
  4402. Object.defineProperty( this, 'id', { value: _object3DId ++ } );
  4403. this.uuid = generateUUID();
  4404. this.name = '';
  4405. this.type = 'Object3D';
  4406. this.parent = null;
  4407. this.children = [];
  4408. this.up = Object3D.DEFAULT_UP.clone();
  4409. const position = new Vector3();
  4410. const rotation = new Euler();
  4411. const quaternion = new Quaternion();
  4412. const scale = new Vector3( 1, 1, 1 );
  4413. function onRotationChange() {
  4414. quaternion.setFromEuler( rotation, false );
  4415. }
  4416. function onQuaternionChange() {
  4417. rotation.setFromQuaternion( quaternion, undefined, false );
  4418. }
  4419. rotation._onChange( onRotationChange );
  4420. quaternion._onChange( onQuaternionChange );
  4421. Object.defineProperties( this, {
  4422. position: {
  4423. configurable: true,
  4424. enumerable: true,
  4425. value: position
  4426. },
  4427. rotation: {
  4428. configurable: true,
  4429. enumerable: true,
  4430. value: rotation
  4431. },
  4432. quaternion: {
  4433. configurable: true,
  4434. enumerable: true,
  4435. value: quaternion
  4436. },
  4437. scale: {
  4438. configurable: true,
  4439. enumerable: true,
  4440. value: scale
  4441. },
  4442. modelViewMatrix: {
  4443. value: new Matrix4()
  4444. },
  4445. normalMatrix: {
  4446. value: new Matrix3()
  4447. }
  4448. } );
  4449. this.matrix = new Matrix4();
  4450. this.matrixWorld = new Matrix4();
  4451. this.matrixAutoUpdate = Object3D.DEFAULT_MATRIX_AUTO_UPDATE;
  4452. this.matrixWorldAutoUpdate = Object3D.DEFAULT_MATRIX_WORLD_AUTO_UPDATE; // checked by the renderer
  4453. this.matrixWorldNeedsUpdate = false;
  4454. this.layers = new Layers();
  4455. this.visible = true;
  4456. this.castShadow = false;
  4457. this.receiveShadow = false;
  4458. this.frustumCulled = true;
  4459. this.renderOrder = 0;
  4460. this.animations = [];
  4461. this.userData = {};
  4462. }
  4463. onBeforeShadow( /* renderer, object, camera, shadowCamera, geometry, depthMaterial, group */ ) {}
  4464. onAfterShadow( /* renderer, object, camera, shadowCamera, geometry, depthMaterial, group */ ) {}
  4465. onBeforeRender( /* renderer, scene, camera, geometry, material, group */ ) {}
  4466. onAfterRender( /* renderer, scene, camera, geometry, material, group */ ) {}
  4467. applyMatrix4( matrix ) {
  4468. if ( this.matrixAutoUpdate ) this.updateMatrix();
  4469. this.matrix.premultiply( matrix );
  4470. this.matrix.decompose( this.position, this.quaternion, this.scale );
  4471. }
  4472. applyQuaternion( q ) {
  4473. this.quaternion.premultiply( q );
  4474. return this;
  4475. }
  4476. setRotationFromAxisAngle( axis, angle ) {
  4477. // assumes axis is normalized
  4478. this.quaternion.setFromAxisAngle( axis, angle );
  4479. }
  4480. setRotationFromEuler( euler ) {
  4481. this.quaternion.setFromEuler( euler, true );
  4482. }
  4483. setRotationFromMatrix( m ) {
  4484. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  4485. this.quaternion.setFromRotationMatrix( m );
  4486. }
  4487. setRotationFromQuaternion( q ) {
  4488. // assumes q is normalized
  4489. this.quaternion.copy( q );
  4490. }
  4491. rotateOnAxis( axis, angle ) {
  4492. // rotate object on axis in object space
  4493. // axis is assumed to be normalized
  4494. _q1.setFromAxisAngle( axis, angle );
  4495. this.quaternion.multiply( _q1 );
  4496. return this;
  4497. }
  4498. rotateOnWorldAxis( axis, angle ) {
  4499. // rotate object on axis in world space
  4500. // axis is assumed to be normalized
  4501. // method assumes no rotated parent
  4502. _q1.setFromAxisAngle( axis, angle );
  4503. this.quaternion.premultiply( _q1 );
  4504. return this;
  4505. }
  4506. rotateX( angle ) {
  4507. return this.rotateOnAxis( _xAxis, angle );
  4508. }
  4509. rotateY( angle ) {
  4510. return this.rotateOnAxis( _yAxis, angle );
  4511. }
  4512. rotateZ( angle ) {
  4513. return this.rotateOnAxis( _zAxis, angle );
  4514. }
  4515. translateOnAxis( axis, distance ) {
  4516. // translate object by distance along axis in object space
  4517. // axis is assumed to be normalized
  4518. _v1$4.copy( axis ).applyQuaternion( this.quaternion );
  4519. this.position.add( _v1$4.multiplyScalar( distance ) );
  4520. return this;
  4521. }
  4522. translateX( distance ) {
  4523. return this.translateOnAxis( _xAxis, distance );
  4524. }
  4525. translateY( distance ) {
  4526. return this.translateOnAxis( _yAxis, distance );
  4527. }
  4528. translateZ( distance ) {
  4529. return this.translateOnAxis( _zAxis, distance );
  4530. }
  4531. localToWorld( vector ) {
  4532. this.updateWorldMatrix( true, false );
  4533. return vector.applyMatrix4( this.matrixWorld );
  4534. }
  4535. worldToLocal( vector ) {
  4536. this.updateWorldMatrix( true, false );
  4537. return vector.applyMatrix4( _m1$1.copy( this.matrixWorld ).invert() );
  4538. }
  4539. lookAt( x, y, z ) {
  4540. // This method does not support objects having non-uniformly-scaled parent(s)
  4541. if ( x.isVector3 ) {
  4542. _target.copy( x );
  4543. } else {
  4544. _target.set( x, y, z );
  4545. }
  4546. const parent = this.parent;
  4547. this.updateWorldMatrix( true, false );
  4548. _position$3.setFromMatrixPosition( this.matrixWorld );
  4549. if ( this.isCamera || this.isLight ) {
  4550. _m1$1.lookAt( _position$3, _target, this.up );
  4551. } else {
  4552. _m1$1.lookAt( _target, _position$3, this.up );
  4553. }
  4554. this.quaternion.setFromRotationMatrix( _m1$1 );
  4555. if ( parent ) {
  4556. _m1$1.extractRotation( parent.matrixWorld );
  4557. _q1.setFromRotationMatrix( _m1$1 );
  4558. this.quaternion.premultiply( _q1.invert() );
  4559. }
  4560. }
  4561. add( object ) {
  4562. if ( arguments.length > 1 ) {
  4563. for ( let i = 0; i < arguments.length; i ++ ) {
  4564. this.add( arguments[ i ] );
  4565. }
  4566. return this;
  4567. }
  4568. if ( object === this ) {
  4569. console.error( 'THREE.Object3D.add: object can\'t be added as a child of itself.', object );
  4570. return this;
  4571. }
  4572. if ( object && object.isObject3D ) {
  4573. object.removeFromParent();
  4574. object.parent = this;
  4575. this.children.push( object );
  4576. object.dispatchEvent( _addedEvent );
  4577. _childaddedEvent.child = object;
  4578. this.dispatchEvent( _childaddedEvent );
  4579. _childaddedEvent.child = null;
  4580. } else {
  4581. console.error( 'THREE.Object3D.add: object not an instance of THREE.Object3D.', object );
  4582. }
  4583. return this;
  4584. }
  4585. remove( object ) {
  4586. if ( arguments.length > 1 ) {
  4587. for ( let i = 0; i < arguments.length; i ++ ) {
  4588. this.remove( arguments[ i ] );
  4589. }
  4590. return this;
  4591. }
  4592. const index = this.children.indexOf( object );
  4593. if ( index !== - 1 ) {
  4594. object.parent = null;
  4595. this.children.splice( index, 1 );
  4596. object.dispatchEvent( _removedEvent );
  4597. _childremovedEvent.child = object;
  4598. this.dispatchEvent( _childremovedEvent );
  4599. _childremovedEvent.child = null;
  4600. }
  4601. return this;
  4602. }
  4603. removeFromParent() {
  4604. const parent = this.parent;
  4605. if ( parent !== null ) {
  4606. parent.remove( this );
  4607. }
  4608. return this;
  4609. }
  4610. clear() {
  4611. return this.remove( ... this.children );
  4612. }
  4613. attach( object ) {
  4614. // adds object as a child of this, while maintaining the object's world transform
  4615. // Note: This method does not support scene graphs having non-uniformly-scaled nodes(s)
  4616. this.updateWorldMatrix( true, false );
  4617. _m1$1.copy( this.matrixWorld ).invert();
  4618. if ( object.parent !== null ) {
  4619. object.parent.updateWorldMatrix( true, false );
  4620. _m1$1.multiply( object.parent.matrixWorld );
  4621. }
  4622. object.applyMatrix4( _m1$1 );
  4623. object.removeFromParent();
  4624. object.parent = this;
  4625. this.children.push( object );
  4626. object.updateWorldMatrix( false, true );
  4627. object.dispatchEvent( _addedEvent );
  4628. _childaddedEvent.child = object;
  4629. this.dispatchEvent( _childaddedEvent );
  4630. _childaddedEvent.child = null;
  4631. return this;
  4632. }
  4633. getObjectById( id ) {
  4634. return this.getObjectByProperty( 'id', id );
  4635. }
  4636. getObjectByName( name ) {
  4637. return this.getObjectByProperty( 'name', name );
  4638. }
  4639. getObjectByProperty( name, value ) {
  4640. if ( this[ name ] === value ) return this;
  4641. for ( let i = 0, l = this.children.length; i < l; i ++ ) {
  4642. const child = this.children[ i ];
  4643. const object = child.getObjectByProperty( name, value );
  4644. if ( object !== undefined ) {
  4645. return object;
  4646. }
  4647. }
  4648. return undefined;
  4649. }
  4650. getObjectsByProperty( name, value, result = [] ) {
  4651. if ( this[ name ] === value ) result.push( this );
  4652. const children = this.children;
  4653. for ( let i = 0, l = children.length; i < l; i ++ ) {
  4654. children[ i ].getObjectsByProperty( name, value, result );
  4655. }
  4656. return result;
  4657. }
  4658. getWorldPosition( target ) {
  4659. this.updateWorldMatrix( true, false );
  4660. return target.setFromMatrixPosition( this.matrixWorld );
  4661. }
  4662. getWorldQuaternion( target ) {
  4663. this.updateWorldMatrix( true, false );
  4664. this.matrixWorld.decompose( _position$3, target, _scale$2 );
  4665. return target;
  4666. }
  4667. getWorldScale( target ) {
  4668. this.updateWorldMatrix( true, false );
  4669. this.matrixWorld.decompose( _position$3, _quaternion$2, target );
  4670. return target;
  4671. }
  4672. getWorldDirection( target ) {
  4673. this.updateWorldMatrix( true, false );
  4674. const e = this.matrixWorld.elements;
  4675. return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize();
  4676. }
  4677. raycast( /* raycaster, intersects */ ) {}
  4678. traverse( callback ) {
  4679. callback( this );
  4680. const children = this.children;
  4681. for ( let i = 0, l = children.length; i < l; i ++ ) {
  4682. children[ i ].traverse( callback );
  4683. }
  4684. }
  4685. traverseVisible( callback ) {
  4686. if ( this.visible === false ) return;
  4687. callback( this );
  4688. const children = this.children;
  4689. for ( let i = 0, l = children.length; i < l; i ++ ) {
  4690. children[ i ].traverseVisible( callback );
  4691. }
  4692. }
  4693. traverseAncestors( callback ) {
  4694. const parent = this.parent;
  4695. if ( parent !== null ) {
  4696. callback( parent );
  4697. parent.traverseAncestors( callback );
  4698. }
  4699. }
  4700. updateMatrix() {
  4701. this.matrix.compose( this.position, this.quaternion, this.scale );
  4702. this.matrixWorldNeedsUpdate = true;
  4703. }
  4704. updateMatrixWorld( force ) {
  4705. if ( this.matrixAutoUpdate ) this.updateMatrix();
  4706. if ( this.matrixWorldNeedsUpdate || force ) {
  4707. if ( this.matrixWorldAutoUpdate === true ) {
  4708. if ( this.parent === null ) {
  4709. this.matrixWorld.copy( this.matrix );
  4710. } else {
  4711. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  4712. }
  4713. }
  4714. this.matrixWorldNeedsUpdate = false;
  4715. force = true;
  4716. }
  4717. // make sure descendants are updated if required
  4718. const children = this.children;
  4719. for ( let i = 0, l = children.length; i < l; i ++ ) {
  4720. const child = children[ i ];
  4721. child.updateMatrixWorld( force );
  4722. }
  4723. }
  4724. updateWorldMatrix( updateParents, updateChildren ) {
  4725. const parent = this.parent;
  4726. if ( updateParents === true && parent !== null ) {
  4727. parent.updateWorldMatrix( true, false );
  4728. }
  4729. if ( this.matrixAutoUpdate ) this.updateMatrix();
  4730. if ( this.matrixWorldAutoUpdate === true ) {
  4731. if ( this.parent === null ) {
  4732. this.matrixWorld.copy( this.matrix );
  4733. } else {
  4734. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  4735. }
  4736. }
  4737. // make sure descendants are updated
  4738. if ( updateChildren === true ) {
  4739. const children = this.children;
  4740. for ( let i = 0, l = children.length; i < l; i ++ ) {
  4741. const child = children[ i ];
  4742. child.updateWorldMatrix( false, true );
  4743. }
  4744. }
  4745. }
  4746. toJSON( meta ) {
  4747. // meta is a string when called from JSON.stringify
  4748. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  4749. const output = {};
  4750. // meta is a hash used to collect geometries, materials.
  4751. // not providing it implies that this is the root object
  4752. // being serialized.
  4753. if ( isRootObject ) {
  4754. // initialize meta obj
  4755. meta = {
  4756. geometries: {},
  4757. materials: {},
  4758. textures: {},
  4759. images: {},
  4760. shapes: {},
  4761. skeletons: {},
  4762. animations: {},
  4763. nodes: {}
  4764. };
  4765. output.metadata = {
  4766. version: 4.6,
  4767. type: 'Object',
  4768. generator: 'Object3D.toJSON'
  4769. };
  4770. }
  4771. // standard Object3D serialization
  4772. const object = {};
  4773. object.uuid = this.uuid;
  4774. object.type = this.type;
  4775. if ( this.name !== '' ) object.name = this.name;
  4776. if ( this.castShadow === true ) object.castShadow = true;
  4777. if ( this.receiveShadow === true ) object.receiveShadow = true;
  4778. if ( this.visible === false ) object.visible = false;
  4779. if ( this.frustumCulled === false ) object.frustumCulled = false;
  4780. if ( this.renderOrder !== 0 ) object.renderOrder = this.renderOrder;
  4781. if ( Object.keys( this.userData ).length > 0 ) object.userData = this.userData;
  4782. object.layers = this.layers.mask;
  4783. object.matrix = this.matrix.toArray();
  4784. object.up = this.up.toArray();
  4785. if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false;
  4786. // object specific properties
  4787. if ( this.isInstancedMesh ) {
  4788. object.type = 'InstancedMesh';
  4789. object.count = this.count;
  4790. object.instanceMatrix = this.instanceMatrix.toJSON();
  4791. if ( this.instanceColor !== null ) object.instanceColor = this.instanceColor.toJSON();
  4792. }
  4793. if ( this.isBatchedMesh ) {
  4794. object.type = 'BatchedMesh';
  4795. object.perObjectFrustumCulled = this.perObjectFrustumCulled;
  4796. object.sortObjects = this.sortObjects;
  4797. object.drawRanges = this._drawRanges;
  4798. object.reservedRanges = this._reservedRanges;
  4799. object.visibility = this._visibility;
  4800. object.active = this._active;
  4801. object.bounds = this._bounds.map( bound => ( {
  4802. boxInitialized: bound.boxInitialized,
  4803. boxMin: bound.box.min.toArray(),
  4804. boxMax: bound.box.max.toArray(),
  4805. sphereInitialized: bound.sphereInitialized,
  4806. sphereRadius: bound.sphere.radius,
  4807. sphereCenter: bound.sphere.center.toArray()
  4808. } ) );
  4809. object.maxInstanceCount = this._maxInstanceCount;
  4810. object.maxVertexCount = this._maxVertexCount;
  4811. object.maxIndexCount = this._maxIndexCount;
  4812. object.geometryInitialized = this._geometryInitialized;
  4813. object.geometryCount = this._geometryCount;
  4814. object.matricesTexture = this._matricesTexture.toJSON( meta );
  4815. if ( this._colorsTexture !== null ) object.colorsTexture = this._colorsTexture.toJSON( meta );
  4816. if ( this.boundingSphere !== null ) {
  4817. object.boundingSphere = {
  4818. center: object.boundingSphere.center.toArray(),
  4819. radius: object.boundingSphere.radius
  4820. };
  4821. }
  4822. if ( this.boundingBox !== null ) {
  4823. object.boundingBox = {
  4824. min: object.boundingBox.min.toArray(),
  4825. max: object.boundingBox.max.toArray()
  4826. };
  4827. }
  4828. }
  4829. //
  4830. function serialize( library, element ) {
  4831. if ( library[ element.uuid ] === undefined ) {
  4832. library[ element.uuid ] = element.toJSON( meta );
  4833. }
  4834. return element.uuid;
  4835. }
  4836. if ( this.isScene ) {
  4837. if ( this.background ) {
  4838. if ( this.background.isColor ) {
  4839. object.background = this.background.toJSON();
  4840. } else if ( this.background.isTexture ) {
  4841. object.background = this.background.toJSON( meta ).uuid;
  4842. }
  4843. }
  4844. if ( this.environment && this.environment.isTexture && this.environment.isRenderTargetTexture !== true ) {
  4845. object.environment = this.environment.toJSON( meta ).uuid;
  4846. }
  4847. } else if ( this.isMesh || this.isLine || this.isPoints ) {
  4848. object.geometry = serialize( meta.geometries, this.geometry );
  4849. const parameters = this.geometry.parameters;
  4850. if ( parameters !== undefined && parameters.shapes !== undefined ) {
  4851. const shapes = parameters.shapes;
  4852. if ( Array.isArray( shapes ) ) {
  4853. for ( let i = 0, l = shapes.length; i < l; i ++ ) {
  4854. const shape = shapes[ i ];
  4855. serialize( meta.shapes, shape );
  4856. }
  4857. } else {
  4858. serialize( meta.shapes, shapes );
  4859. }
  4860. }
  4861. }
  4862. if ( this.isSkinnedMesh ) {
  4863. object.bindMode = this.bindMode;
  4864. object.bindMatrix = this.bindMatrix.toArray();
  4865. if ( this.skeleton !== undefined ) {
  4866. serialize( meta.skeletons, this.skeleton );
  4867. object.skeleton = this.skeleton.uuid;
  4868. }
  4869. }
  4870. if ( this.material !== undefined ) {
  4871. if ( Array.isArray( this.material ) ) {
  4872. const uuids = [];
  4873. for ( let i = 0, l = this.material.length; i < l; i ++ ) {
  4874. uuids.push( serialize( meta.materials, this.material[ i ] ) );
  4875. }
  4876. object.material = uuids;
  4877. } else {
  4878. object.material = serialize( meta.materials, this.material );
  4879. }
  4880. }
  4881. //
  4882. if ( this.children.length > 0 ) {
  4883. object.children = [];
  4884. for ( let i = 0; i < this.children.length; i ++ ) {
  4885. object.children.push( this.children[ i ].toJSON( meta ).object );
  4886. }
  4887. }
  4888. //
  4889. if ( this.animations.length > 0 ) {
  4890. object.animations = [];
  4891. for ( let i = 0; i < this.animations.length; i ++ ) {
  4892. const animation = this.animations[ i ];
  4893. object.animations.push( serialize( meta.animations, animation ) );
  4894. }
  4895. }
  4896. if ( isRootObject ) {
  4897. const geometries = extractFromCache( meta.geometries );
  4898. const materials = extractFromCache( meta.materials );
  4899. const textures = extractFromCache( meta.textures );
  4900. const images = extractFromCache( meta.images );
  4901. const shapes = extractFromCache( meta.shapes );
  4902. const skeletons = extractFromCache( meta.skeletons );
  4903. const animations = extractFromCache( meta.animations );
  4904. const nodes = extractFromCache( meta.nodes );
  4905. if ( geometries.length > 0 ) output.geometries = geometries;
  4906. if ( materials.length > 0 ) output.materials = materials;
  4907. if ( textures.length > 0 ) output.textures = textures;
  4908. if ( images.length > 0 ) output.images = images;
  4909. if ( shapes.length > 0 ) output.shapes = shapes;
  4910. if ( skeletons.length > 0 ) output.skeletons = skeletons;
  4911. if ( animations.length > 0 ) output.animations = animations;
  4912. if ( nodes.length > 0 ) output.nodes = nodes;
  4913. }
  4914. output.object = object;
  4915. return output;
  4916. // extract data from the cache hash
  4917. // remove metadata on each item
  4918. // and return as array
  4919. function extractFromCache( cache ) {
  4920. const values = [];
  4921. for ( const key in cache ) {
  4922. const data = cache[ key ];
  4923. delete data.metadata;
  4924. values.push( data );
  4925. }
  4926. return values;
  4927. }
  4928. }
  4929. clone( recursive ) {
  4930. return new this.constructor().copy( this, recursive );
  4931. }
  4932. copy( source, recursive = true ) {
  4933. this.name = source.name;
  4934. this.up.copy( source.up );
  4935. this.position.copy( source.position );
  4936. this.rotation.order = source.rotation.order;
  4937. this.quaternion.copy( source.quaternion );
  4938. this.scale.copy( source.scale );
  4939. this.matrix.copy( source.matrix );
  4940. this.matrixWorld.copy( source.matrixWorld );
  4941. this.matrixAutoUpdate = source.matrixAutoUpdate;
  4942. this.matrixWorldAutoUpdate = source.matrixWorldAutoUpdate;
  4943. this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
  4944. this.layers.mask = source.layers.mask;
  4945. this.visible = source.visible;
  4946. this.castShadow = source.castShadow;
  4947. this.receiveShadow = source.receiveShadow;
  4948. this.frustumCulled = source.frustumCulled;
  4949. this.renderOrder = source.renderOrder;
  4950. this.animations = source.animations.slice();
  4951. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  4952. if ( recursive === true ) {
  4953. for ( let i = 0; i < source.children.length; i ++ ) {
  4954. const child = source.children[ i ];
  4955. this.add( child.clone() );
  4956. }
  4957. }
  4958. return this;
  4959. }
  4960. }
  4961. Object3D.DEFAULT_UP = /*@__PURE__*/ new Vector3( 0, 1, 0 );
  4962. Object3D.DEFAULT_MATRIX_AUTO_UPDATE = true;
  4963. Object3D.DEFAULT_MATRIX_WORLD_AUTO_UPDATE = true;
  4964. const _v0$1 = /*@__PURE__*/ new Vector3();
  4965. const _v1$3 = /*@__PURE__*/ new Vector3();
  4966. const _v2$2 = /*@__PURE__*/ new Vector3();
  4967. const _v3$2 = /*@__PURE__*/ new Vector3();
  4968. const _vab = /*@__PURE__*/ new Vector3();
  4969. const _vac = /*@__PURE__*/ new Vector3();
  4970. const _vbc = /*@__PURE__*/ new Vector3();
  4971. const _vap = /*@__PURE__*/ new Vector3();
  4972. const _vbp = /*@__PURE__*/ new Vector3();
  4973. const _vcp = /*@__PURE__*/ new Vector3();
  4974. const _v40 = /*@__PURE__*/ new Vector4();
  4975. const _v41 = /*@__PURE__*/ new Vector4();
  4976. const _v42 = /*@__PURE__*/ new Vector4();
  4977. class Triangle {
  4978. constructor( a = new Vector3(), b = new Vector3(), c = new Vector3() ) {
  4979. this.a = a;
  4980. this.b = b;
  4981. this.c = c;
  4982. }
  4983. static getNormal( a, b, c, target ) {
  4984. target.subVectors( c, b );
  4985. _v0$1.subVectors( a, b );
  4986. target.cross( _v0$1 );
  4987. const targetLengthSq = target.lengthSq();
  4988. if ( targetLengthSq > 0 ) {
  4989. return target.multiplyScalar( 1 / Math.sqrt( targetLengthSq ) );
  4990. }
  4991. return target.set( 0, 0, 0 );
  4992. }
  4993. // static/instance method to calculate barycentric coordinates
  4994. // based on: http://www.blackpawn.com/texts/pointinpoly/default.html
  4995. static getBarycoord( point, a, b, c, target ) {
  4996. _v0$1.subVectors( c, a );
  4997. _v1$3.subVectors( b, a );
  4998. _v2$2.subVectors( point, a );
  4999. const dot00 = _v0$1.dot( _v0$1 );
  5000. const dot01 = _v0$1.dot( _v1$3 );
  5001. const dot02 = _v0$1.dot( _v2$2 );
  5002. const dot11 = _v1$3.dot( _v1$3 );
  5003. const dot12 = _v1$3.dot( _v2$2 );
  5004. const denom = ( dot00 * dot11 - dot01 * dot01 );
  5005. // collinear or singular triangle
  5006. if ( denom === 0 ) {
  5007. target.set( 0, 0, 0 );
  5008. return null;
  5009. }
  5010. const invDenom = 1 / denom;
  5011. const u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  5012. const v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  5013. // barycentric coordinates must always sum to 1
  5014. return target.set( 1 - u - v, v, u );
  5015. }
  5016. static containsPoint( point, a, b, c ) {
  5017. // if the triangle is degenerate then we can't contain a point
  5018. if ( this.getBarycoord( point, a, b, c, _v3$2 ) === null ) {
  5019. return false;
  5020. }
  5021. return ( _v3$2.x >= 0 ) && ( _v3$2.y >= 0 ) && ( ( _v3$2.x + _v3$2.y ) <= 1 );
  5022. }
  5023. static getInterpolation( point, p1, p2, p3, v1, v2, v3, target ) {
  5024. if ( this.getBarycoord( point, p1, p2, p3, _v3$2 ) === null ) {
  5025. target.x = 0;
  5026. target.y = 0;
  5027. if ( 'z' in target ) target.z = 0;
  5028. if ( 'w' in target ) target.w = 0;
  5029. return null;
  5030. }
  5031. target.setScalar( 0 );
  5032. target.addScaledVector( v1, _v3$2.x );
  5033. target.addScaledVector( v2, _v3$2.y );
  5034. target.addScaledVector( v3, _v3$2.z );
  5035. return target;
  5036. }
  5037. static getInterpolatedAttribute( attr, i1, i2, i3, barycoord, target ) {
  5038. _v40.setScalar( 0 );
  5039. _v41.setScalar( 0 );
  5040. _v42.setScalar( 0 );
  5041. _v40.fromBufferAttribute( attr, i1 );
  5042. _v41.fromBufferAttribute( attr, i2 );
  5043. _v42.fromBufferAttribute( attr, i3 );
  5044. target.setScalar( 0 );
  5045. target.addScaledVector( _v40, barycoord.x );
  5046. target.addScaledVector( _v41, barycoord.y );
  5047. target.addScaledVector( _v42, barycoord.z );
  5048. return target;
  5049. }
  5050. static isFrontFacing( a, b, c, direction ) {
  5051. _v0$1.subVectors( c, b );
  5052. _v1$3.subVectors( a, b );
  5053. // strictly front facing
  5054. return ( _v0$1.cross( _v1$3 ).dot( direction ) < 0 ) ? true : false;
  5055. }
  5056. set( a, b, c ) {
  5057. this.a.copy( a );
  5058. this.b.copy( b );
  5059. this.c.copy( c );
  5060. return this;
  5061. }
  5062. setFromPointsAndIndices( points, i0, i1, i2 ) {
  5063. this.a.copy( points[ i0 ] );
  5064. this.b.copy( points[ i1 ] );
  5065. this.c.copy( points[ i2 ] );
  5066. return this;
  5067. }
  5068. setFromAttributeAndIndices( attribute, i0, i1, i2 ) {
  5069. this.a.fromBufferAttribute( attribute, i0 );
  5070. this.b.fromBufferAttribute( attribute, i1 );
  5071. this.c.fromBufferAttribute( attribute, i2 );
  5072. return this;
  5073. }
  5074. clone() {
  5075. return new this.constructor().copy( this );
  5076. }
  5077. copy( triangle ) {
  5078. this.a.copy( triangle.a );
  5079. this.b.copy( triangle.b );
  5080. this.c.copy( triangle.c );
  5081. return this;
  5082. }
  5083. getArea() {
  5084. _v0$1.subVectors( this.c, this.b );
  5085. _v1$3.subVectors( this.a, this.b );
  5086. return _v0$1.cross( _v1$3 ).length() * 0.5;
  5087. }
  5088. getMidpoint( target ) {
  5089. return target.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
  5090. }
  5091. getNormal( target ) {
  5092. return Triangle.getNormal( this.a, this.b, this.c, target );
  5093. }
  5094. getPlane( target ) {
  5095. return target.setFromCoplanarPoints( this.a, this.b, this.c );
  5096. }
  5097. getBarycoord( point, target ) {
  5098. return Triangle.getBarycoord( point, this.a, this.b, this.c, target );
  5099. }
  5100. getInterpolation( point, v1, v2, v3, target ) {
  5101. return Triangle.getInterpolation( point, this.a, this.b, this.c, v1, v2, v3, target );
  5102. }
  5103. containsPoint( point ) {
  5104. return Triangle.containsPoint( point, this.a, this.b, this.c );
  5105. }
  5106. isFrontFacing( direction ) {
  5107. return Triangle.isFrontFacing( this.a, this.b, this.c, direction );
  5108. }
  5109. intersectsBox( box ) {
  5110. return box.intersectsTriangle( this );
  5111. }
  5112. closestPointToPoint( p, target ) {
  5113. const a = this.a, b = this.b, c = this.c;
  5114. let v, w;
  5115. // algorithm thanks to Real-Time Collision Detection by Christer Ericson,
  5116. // published by Morgan Kaufmann Publishers, (c) 2005 Elsevier Inc.,
  5117. // under the accompanying license; see chapter 5.1.5 for detailed explanation.
  5118. // basically, we're distinguishing which of the voronoi regions of the triangle
  5119. // the point lies in with the minimum amount of redundant computation.
  5120. _vab.subVectors( b, a );
  5121. _vac.subVectors( c, a );
  5122. _vap.subVectors( p, a );
  5123. const d1 = _vab.dot( _vap );
  5124. const d2 = _vac.dot( _vap );
  5125. if ( d1 <= 0 && d2 <= 0 ) {
  5126. // vertex region of A; barycentric coords (1, 0, 0)
  5127. return target.copy( a );
  5128. }
  5129. _vbp.subVectors( p, b );
  5130. const d3 = _vab.dot( _vbp );
  5131. const d4 = _vac.dot( _vbp );
  5132. if ( d3 >= 0 && d4 <= d3 ) {
  5133. // vertex region of B; barycentric coords (0, 1, 0)
  5134. return target.copy( b );
  5135. }
  5136. const vc = d1 * d4 - d3 * d2;
  5137. if ( vc <= 0 && d1 >= 0 && d3 <= 0 ) {
  5138. v = d1 / ( d1 - d3 );
  5139. // edge region of AB; barycentric coords (1-v, v, 0)
  5140. return target.copy( a ).addScaledVector( _vab, v );
  5141. }
  5142. _vcp.subVectors( p, c );
  5143. const d5 = _vab.dot( _vcp );
  5144. const d6 = _vac.dot( _vcp );
  5145. if ( d6 >= 0 && d5 <= d6 ) {
  5146. // vertex region of C; barycentric coords (0, 0, 1)
  5147. return target.copy( c );
  5148. }
  5149. const vb = d5 * d2 - d1 * d6;
  5150. if ( vb <= 0 && d2 >= 0 && d6 <= 0 ) {
  5151. w = d2 / ( d2 - d6 );
  5152. // edge region of AC; barycentric coords (1-w, 0, w)
  5153. return target.copy( a ).addScaledVector( _vac, w );
  5154. }
  5155. const va = d3 * d6 - d5 * d4;
  5156. if ( va <= 0 && ( d4 - d3 ) >= 0 && ( d5 - d6 ) >= 0 ) {
  5157. _vbc.subVectors( c, b );
  5158. w = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) );
  5159. // edge region of BC; barycentric coords (0, 1-w, w)
  5160. return target.copy( b ).addScaledVector( _vbc, w ); // edge region of BC
  5161. }
  5162. // face region
  5163. const denom = 1 / ( va + vb + vc );
  5164. // u = va * denom
  5165. v = vb * denom;
  5166. w = vc * denom;
  5167. return target.copy( a ).addScaledVector( _vab, v ).addScaledVector( _vac, w );
  5168. }
  5169. equals( triangle ) {
  5170. return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
  5171. }
  5172. }
  5173. const _colorKeywords = { 'aliceblue': 0xF0F8FF, 'antiquewhite': 0xFAEBD7, 'aqua': 0x00FFFF, 'aquamarine': 0x7FFFD4, 'azure': 0xF0FFFF,
  5174. 'beige': 0xF5F5DC, 'bisque': 0xFFE4C4, 'black': 0x000000, 'blanchedalmond': 0xFFEBCD, 'blue': 0x0000FF, 'blueviolet': 0x8A2BE2,
  5175. 'brown': 0xA52A2A, 'burlywood': 0xDEB887, 'cadetblue': 0x5F9EA0, 'chartreuse': 0x7FFF00, 'chocolate': 0xD2691E, 'coral': 0xFF7F50,
  5176. 'cornflowerblue': 0x6495ED, 'cornsilk': 0xFFF8DC, 'crimson': 0xDC143C, 'cyan': 0x00FFFF, 'darkblue': 0x00008B, 'darkcyan': 0x008B8B,
  5177. 'darkgoldenrod': 0xB8860B, 'darkgray': 0xA9A9A9, 'darkgreen': 0x006400, 'darkgrey': 0xA9A9A9, 'darkkhaki': 0xBDB76B, 'darkmagenta': 0x8B008B,
  5178. 'darkolivegreen': 0x556B2F, 'darkorange': 0xFF8C00, 'darkorchid': 0x9932CC, 'darkred': 0x8B0000, 'darksalmon': 0xE9967A, 'darkseagreen': 0x8FBC8F,
  5179. 'darkslateblue': 0x483D8B, 'darkslategray': 0x2F4F4F, 'darkslategrey': 0x2F4F4F, 'darkturquoise': 0x00CED1, 'darkviolet': 0x9400D3,
  5180. 'deeppink': 0xFF1493, 'deepskyblue': 0x00BFFF, 'dimgray': 0x696969, 'dimgrey': 0x696969, 'dodgerblue': 0x1E90FF, 'firebrick': 0xB22222,
  5181. 'floralwhite': 0xFFFAF0, 'forestgreen': 0x228B22, 'fuchsia': 0xFF00FF, 'gainsboro': 0xDCDCDC, 'ghostwhite': 0xF8F8FF, 'gold': 0xFFD700,
  5182. 'goldenrod': 0xDAA520, 'gray': 0x808080, 'green': 0x008000, 'greenyellow': 0xADFF2F, 'grey': 0x808080, 'honeydew': 0xF0FFF0, 'hotpink': 0xFF69B4,
  5183. 'indianred': 0xCD5C5C, 'indigo': 0x4B0082, 'ivory': 0xFFFFF0, 'khaki': 0xF0E68C, 'lavender': 0xE6E6FA, 'lavenderblush': 0xFFF0F5, 'lawngreen': 0x7CFC00,
  5184. 'lemonchiffon': 0xFFFACD, 'lightblue': 0xADD8E6, 'lightcoral': 0xF08080, 'lightcyan': 0xE0FFFF, 'lightgoldenrodyellow': 0xFAFAD2, 'lightgray': 0xD3D3D3,
  5185. 'lightgreen': 0x90EE90, 'lightgrey': 0xD3D3D3, 'lightpink': 0xFFB6C1, 'lightsalmon': 0xFFA07A, 'lightseagreen': 0x20B2AA, 'lightskyblue': 0x87CEFA,
  5186. 'lightslategray': 0x778899, 'lightslategrey': 0x778899, 'lightsteelblue': 0xB0C4DE, 'lightyellow': 0xFFFFE0, 'lime': 0x00FF00, 'limegreen': 0x32CD32,
  5187. 'linen': 0xFAF0E6, 'magenta': 0xFF00FF, 'maroon': 0x800000, 'mediumaquamarine': 0x66CDAA, 'mediumblue': 0x0000CD, 'mediumorchid': 0xBA55D3,
  5188. 'mediumpurple': 0x9370DB, 'mediumseagreen': 0x3CB371, 'mediumslateblue': 0x7B68EE, 'mediumspringgreen': 0x00FA9A, 'mediumturquoise': 0x48D1CC,
  5189. 'mediumvioletred': 0xC71585, 'midnightblue': 0x191970, 'mintcream': 0xF5FFFA, 'mistyrose': 0xFFE4E1, 'moccasin': 0xFFE4B5, 'navajowhite': 0xFFDEAD,
  5190. 'navy': 0x000080, 'oldlace': 0xFDF5E6, 'olive': 0x808000, 'olivedrab': 0x6B8E23, 'orange': 0xFFA500, 'orangered': 0xFF4500, 'orchid': 0xDA70D6,
  5191. 'palegoldenrod': 0xEEE8AA, 'palegreen': 0x98FB98, 'paleturquoise': 0xAFEEEE, 'palevioletred': 0xDB7093, 'papayawhip': 0xFFEFD5, 'peachpuff': 0xFFDAB9,
  5192. 'peru': 0xCD853F, 'pink': 0xFFC0CB, 'plum': 0xDDA0DD, 'powderblue': 0xB0E0E6, 'purple': 0x800080, 'rebeccapurple': 0x663399, 'red': 0xFF0000, 'rosybrown': 0xBC8F8F,
  5193. 'royalblue': 0x4169E1, 'saddlebrown': 0x8B4513, 'salmon': 0xFA8072, 'sandybrown': 0xF4A460, 'seagreen': 0x2E8B57, 'seashell': 0xFFF5EE,
  5194. 'sienna': 0xA0522D, 'silver': 0xC0C0C0, 'skyblue': 0x87CEEB, 'slateblue': 0x6A5ACD, 'slategray': 0x708090, 'slategrey': 0x708090, 'snow': 0xFFFAFA,
  5195. 'springgreen': 0x00FF7F, 'steelblue': 0x4682B4, 'tan': 0xD2B48C, 'teal': 0x008080, 'thistle': 0xD8BFD8, 'tomato': 0xFF6347, 'turquoise': 0x40E0D0,
  5196. 'violet': 0xEE82EE, 'wheat': 0xF5DEB3, 'white': 0xFFFFFF, 'whitesmoke': 0xF5F5F5, 'yellow': 0xFFFF00, 'yellowgreen': 0x9ACD32 };
  5197. const _hslA = { h: 0, s: 0, l: 0 };
  5198. const _hslB = { h: 0, s: 0, l: 0 };
  5199. function hue2rgb( p, q, t ) {
  5200. if ( t < 0 ) t += 1;
  5201. if ( t > 1 ) t -= 1;
  5202. if ( t < 1 / 6 ) return p + ( q - p ) * 6 * t;
  5203. if ( t < 1 / 2 ) return q;
  5204. if ( t < 2 / 3 ) return p + ( q - p ) * 6 * ( 2 / 3 - t );
  5205. return p;
  5206. }
  5207. class Color {
  5208. constructor( r, g, b ) {
  5209. this.isColor = true;
  5210. this.r = 1;
  5211. this.g = 1;
  5212. this.b = 1;
  5213. return this.set( r, g, b );
  5214. }
  5215. set( r, g, b ) {
  5216. if ( g === undefined && b === undefined ) {
  5217. // r is THREE.Color, hex or string
  5218. const value = r;
  5219. if ( value && value.isColor ) {
  5220. this.copy( value );
  5221. } else if ( typeof value === 'number' ) {
  5222. this.setHex( value );
  5223. } else if ( typeof value === 'string' ) {
  5224. this.setStyle( value );
  5225. }
  5226. } else {
  5227. this.setRGB( r, g, b );
  5228. }
  5229. return this;
  5230. }
  5231. setScalar( scalar ) {
  5232. this.r = scalar;
  5233. this.g = scalar;
  5234. this.b = scalar;
  5235. return this;
  5236. }
  5237. setHex( hex, colorSpace = SRGBColorSpace ) {
  5238. hex = Math.floor( hex );
  5239. this.r = ( hex >> 16 & 255 ) / 255;
  5240. this.g = ( hex >> 8 & 255 ) / 255;
  5241. this.b = ( hex & 255 ) / 255;
  5242. ColorManagement.toWorkingColorSpace( this, colorSpace );
  5243. return this;
  5244. }
  5245. setRGB( r, g, b, colorSpace = ColorManagement.workingColorSpace ) {
  5246. this.r = r;
  5247. this.g = g;
  5248. this.b = b;
  5249. ColorManagement.toWorkingColorSpace( this, colorSpace );
  5250. return this;
  5251. }
  5252. setHSL( h, s, l, colorSpace = ColorManagement.workingColorSpace ) {
  5253. // h,s,l ranges are in 0.0 - 1.0
  5254. h = euclideanModulo( h, 1 );
  5255. s = clamp( s, 0, 1 );
  5256. l = clamp( l, 0, 1 );
  5257. if ( s === 0 ) {
  5258. this.r = this.g = this.b = l;
  5259. } else {
  5260. const p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s );
  5261. const q = ( 2 * l ) - p;
  5262. this.r = hue2rgb( q, p, h + 1 / 3 );
  5263. this.g = hue2rgb( q, p, h );
  5264. this.b = hue2rgb( q, p, h - 1 / 3 );
  5265. }
  5266. ColorManagement.toWorkingColorSpace( this, colorSpace );
  5267. return this;
  5268. }
  5269. setStyle( style, colorSpace = SRGBColorSpace ) {
  5270. function handleAlpha( string ) {
  5271. if ( string === undefined ) return;
  5272. if ( parseFloat( string ) < 1 ) {
  5273. console.warn( 'THREE.Color: Alpha component of ' + style + ' will be ignored.' );
  5274. }
  5275. }
  5276. let m;
  5277. if ( m = /^(\w+)\(([^\)]*)\)/.exec( style ) ) {
  5278. // rgb / hsl
  5279. let color;
  5280. const name = m[ 1 ];
  5281. const components = m[ 2 ];
  5282. switch ( name ) {
  5283. case 'rgb':
  5284. case 'rgba':
  5285. if ( color = /^\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
  5286. // rgb(255,0,0) rgba(255,0,0,0.5)
  5287. handleAlpha( color[ 4 ] );
  5288. return this.setRGB(
  5289. Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255,
  5290. Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255,
  5291. Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255,
  5292. colorSpace
  5293. );
  5294. }
  5295. if ( color = /^\s*(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
  5296. // rgb(100%,0%,0%) rgba(100%,0%,0%,0.5)
  5297. handleAlpha( color[ 4 ] );
  5298. return this.setRGB(
  5299. Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100,
  5300. Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100,
  5301. Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100,
  5302. colorSpace
  5303. );
  5304. }
  5305. break;
  5306. case 'hsl':
  5307. case 'hsla':
  5308. if ( color = /^\s*(\d*\.?\d+)\s*,\s*(\d*\.?\d+)\%\s*,\s*(\d*\.?\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
  5309. // hsl(120,50%,50%) hsla(120,50%,50%,0.5)
  5310. handleAlpha( color[ 4 ] );
  5311. return this.setHSL(
  5312. parseFloat( color[ 1 ] ) / 360,
  5313. parseFloat( color[ 2 ] ) / 100,
  5314. parseFloat( color[ 3 ] ) / 100,
  5315. colorSpace
  5316. );
  5317. }
  5318. break;
  5319. default:
  5320. console.warn( 'THREE.Color: Unknown color model ' + style );
  5321. }
  5322. } else if ( m = /^\#([A-Fa-f\d]+)$/.exec( style ) ) {
  5323. // hex color
  5324. const hex = m[ 1 ];
  5325. const size = hex.length;
  5326. if ( size === 3 ) {
  5327. // #ff0
  5328. return this.setRGB(
  5329. parseInt( hex.charAt( 0 ), 16 ) / 15,
  5330. parseInt( hex.charAt( 1 ), 16 ) / 15,
  5331. parseInt( hex.charAt( 2 ), 16 ) / 15,
  5332. colorSpace
  5333. );
  5334. } else if ( size === 6 ) {
  5335. // #ff0000
  5336. return this.setHex( parseInt( hex, 16 ), colorSpace );
  5337. } else {
  5338. console.warn( 'THREE.Color: Invalid hex color ' + style );
  5339. }
  5340. } else if ( style && style.length > 0 ) {
  5341. return this.setColorName( style, colorSpace );
  5342. }
  5343. return this;
  5344. }
  5345. setColorName( style, colorSpace = SRGBColorSpace ) {
  5346. // color keywords
  5347. const hex = _colorKeywords[ style.toLowerCase() ];
  5348. if ( hex !== undefined ) {
  5349. // red
  5350. this.setHex( hex, colorSpace );
  5351. } else {
  5352. // unknown color
  5353. console.warn( 'THREE.Color: Unknown color ' + style );
  5354. }
  5355. return this;
  5356. }
  5357. clone() {
  5358. return new this.constructor( this.r, this.g, this.b );
  5359. }
  5360. copy( color ) {
  5361. this.r = color.r;
  5362. this.g = color.g;
  5363. this.b = color.b;
  5364. return this;
  5365. }
  5366. copySRGBToLinear( color ) {
  5367. this.r = SRGBToLinear( color.r );
  5368. this.g = SRGBToLinear( color.g );
  5369. this.b = SRGBToLinear( color.b );
  5370. return this;
  5371. }
  5372. copyLinearToSRGB( color ) {
  5373. this.r = LinearToSRGB( color.r );
  5374. this.g = LinearToSRGB( color.g );
  5375. this.b = LinearToSRGB( color.b );
  5376. return this;
  5377. }
  5378. convertSRGBToLinear() {
  5379. this.copySRGBToLinear( this );
  5380. return this;
  5381. }
  5382. convertLinearToSRGB() {
  5383. this.copyLinearToSRGB( this );
  5384. return this;
  5385. }
  5386. getHex( colorSpace = SRGBColorSpace ) {
  5387. ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace );
  5388. return Math.round( clamp( _color.r * 255, 0, 255 ) ) * 65536 + Math.round( clamp( _color.g * 255, 0, 255 ) ) * 256 + Math.round( clamp( _color.b * 255, 0, 255 ) );
  5389. }
  5390. getHexString( colorSpace = SRGBColorSpace ) {
  5391. return ( '000000' + this.getHex( colorSpace ).toString( 16 ) ).slice( - 6 );
  5392. }
  5393. getHSL( target, colorSpace = ColorManagement.workingColorSpace ) {
  5394. // h,s,l ranges are in 0.0 - 1.0
  5395. ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace );
  5396. const r = _color.r, g = _color.g, b = _color.b;
  5397. const max = Math.max( r, g, b );
  5398. const min = Math.min( r, g, b );
  5399. let hue, saturation;
  5400. const lightness = ( min + max ) / 2.0;
  5401. if ( min === max ) {
  5402. hue = 0;
  5403. saturation = 0;
  5404. } else {
  5405. const delta = max - min;
  5406. saturation = lightness <= 0.5 ? delta / ( max + min ) : delta / ( 2 - max - min );
  5407. switch ( max ) {
  5408. case r: hue = ( g - b ) / delta + ( g < b ? 6 : 0 ); break;
  5409. case g: hue = ( b - r ) / delta + 2; break;
  5410. case b: hue = ( r - g ) / delta + 4; break;
  5411. }
  5412. hue /= 6;
  5413. }
  5414. target.h = hue;
  5415. target.s = saturation;
  5416. target.l = lightness;
  5417. return target;
  5418. }
  5419. getRGB( target, colorSpace = ColorManagement.workingColorSpace ) {
  5420. ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace );
  5421. target.r = _color.r;
  5422. target.g = _color.g;
  5423. target.b = _color.b;
  5424. return target;
  5425. }
  5426. getStyle( colorSpace = SRGBColorSpace ) {
  5427. ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace );
  5428. const r = _color.r, g = _color.g, b = _color.b;
  5429. if ( colorSpace !== SRGBColorSpace ) {
  5430. // Requires CSS Color Module Level 4 (https://www.w3.org/TR/css-color-4/).
  5431. return `color(${ colorSpace } ${ r.toFixed( 3 ) } ${ g.toFixed( 3 ) } ${ b.toFixed( 3 ) })`;
  5432. }
  5433. return `rgb(${ Math.round( r * 255 ) },${ Math.round( g * 255 ) },${ Math.round( b * 255 ) })`;
  5434. }
  5435. offsetHSL( h, s, l ) {
  5436. this.getHSL( _hslA );
  5437. return this.setHSL( _hslA.h + h, _hslA.s + s, _hslA.l + l );
  5438. }
  5439. add( color ) {
  5440. this.r += color.r;
  5441. this.g += color.g;
  5442. this.b += color.b;
  5443. return this;
  5444. }
  5445. addColors( color1, color2 ) {
  5446. this.r = color1.r + color2.r;
  5447. this.g = color1.g + color2.g;
  5448. this.b = color1.b + color2.b;
  5449. return this;
  5450. }
  5451. addScalar( s ) {
  5452. this.r += s;
  5453. this.g += s;
  5454. this.b += s;
  5455. return this;
  5456. }
  5457. sub( color ) {
  5458. this.r = Math.max( 0, this.r - color.r );
  5459. this.g = Math.max( 0, this.g - color.g );
  5460. this.b = Math.max( 0, this.b - color.b );
  5461. return this;
  5462. }
  5463. multiply( color ) {
  5464. this.r *= color.r;
  5465. this.g *= color.g;
  5466. this.b *= color.b;
  5467. return this;
  5468. }
  5469. multiplyScalar( s ) {
  5470. this.r *= s;
  5471. this.g *= s;
  5472. this.b *= s;
  5473. return this;
  5474. }
  5475. lerp( color, alpha ) {
  5476. this.r += ( color.r - this.r ) * alpha;
  5477. this.g += ( color.g - this.g ) * alpha;
  5478. this.b += ( color.b - this.b ) * alpha;
  5479. return this;
  5480. }
  5481. lerpColors( color1, color2, alpha ) {
  5482. this.r = color1.r + ( color2.r - color1.r ) * alpha;
  5483. this.g = color1.g + ( color2.g - color1.g ) * alpha;
  5484. this.b = color1.b + ( color2.b - color1.b ) * alpha;
  5485. return this;
  5486. }
  5487. lerpHSL( color, alpha ) {
  5488. this.getHSL( _hslA );
  5489. color.getHSL( _hslB );
  5490. const h = lerp( _hslA.h, _hslB.h, alpha );
  5491. const s = lerp( _hslA.s, _hslB.s, alpha );
  5492. const l = lerp( _hslA.l, _hslB.l, alpha );
  5493. this.setHSL( h, s, l );
  5494. return this;
  5495. }
  5496. setFromVector3( v ) {
  5497. this.r = v.x;
  5498. this.g = v.y;
  5499. this.b = v.z;
  5500. return this;
  5501. }
  5502. applyMatrix3( m ) {
  5503. const r = this.r, g = this.g, b = this.b;
  5504. const e = m.elements;
  5505. this.r = e[ 0 ] * r + e[ 3 ] * g + e[ 6 ] * b;
  5506. this.g = e[ 1 ] * r + e[ 4 ] * g + e[ 7 ] * b;
  5507. this.b = e[ 2 ] * r + e[ 5 ] * g + e[ 8 ] * b;
  5508. return this;
  5509. }
  5510. equals( c ) {
  5511. return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b );
  5512. }
  5513. fromArray( array, offset = 0 ) {
  5514. this.r = array[ offset ];
  5515. this.g = array[ offset + 1 ];
  5516. this.b = array[ offset + 2 ];
  5517. return this;
  5518. }
  5519. toArray( array = [], offset = 0 ) {
  5520. array[ offset ] = this.r;
  5521. array[ offset + 1 ] = this.g;
  5522. array[ offset + 2 ] = this.b;
  5523. return array;
  5524. }
  5525. fromBufferAttribute( attribute, index ) {
  5526. this.r = attribute.getX( index );
  5527. this.g = attribute.getY( index );
  5528. this.b = attribute.getZ( index );
  5529. return this;
  5530. }
  5531. toJSON() {
  5532. return this.getHex();
  5533. }
  5534. *[ Symbol.iterator ]() {
  5535. yield this.r;
  5536. yield this.g;
  5537. yield this.b;
  5538. }
  5539. }
  5540. const _color = /*@__PURE__*/ new Color();
  5541. Color.NAMES = _colorKeywords;
  5542. let _materialId = 0;
  5543. class Material extends EventDispatcher {
  5544. constructor() {
  5545. super();
  5546. this.isMaterial = true;
  5547. Object.defineProperty( this, 'id', { value: _materialId ++ } );
  5548. this.uuid = generateUUID();
  5549. this.name = '';
  5550. this.type = 'Material';
  5551. this.blending = NormalBlending;
  5552. this.side = FrontSide;
  5553. this.vertexColors = false;
  5554. this.opacity = 1;
  5555. this.transparent = false;
  5556. this.alphaHash = false;
  5557. this.blendSrc = SrcAlphaFactor;
  5558. this.blendDst = OneMinusSrcAlphaFactor;
  5559. this.blendEquation = AddEquation;
  5560. this.blendSrcAlpha = null;
  5561. this.blendDstAlpha = null;
  5562. this.blendEquationAlpha = null;
  5563. this.blendColor = new Color( 0, 0, 0 );
  5564. this.blendAlpha = 0;
  5565. this.depthFunc = LessEqualDepth;
  5566. this.depthTest = true;
  5567. this.depthWrite = true;
  5568. this.stencilWriteMask = 0xff;
  5569. this.stencilFunc = AlwaysStencilFunc;
  5570. this.stencilRef = 0;
  5571. this.stencilFuncMask = 0xff;
  5572. this.stencilFail = KeepStencilOp;
  5573. this.stencilZFail = KeepStencilOp;
  5574. this.stencilZPass = KeepStencilOp;
  5575. this.stencilWrite = false;
  5576. this.clippingPlanes = null;
  5577. this.clipIntersection = false;
  5578. this.clipShadows = false;
  5579. this.shadowSide = null;
  5580. this.colorWrite = true;
  5581. this.precision = null; // override the renderer's default precision for this material
  5582. this.polygonOffset = false;
  5583. this.polygonOffsetFactor = 0;
  5584. this.polygonOffsetUnits = 0;
  5585. this.dithering = false;
  5586. this.alphaToCoverage = false;
  5587. this.premultipliedAlpha = false;
  5588. this.forceSinglePass = false;
  5589. this.visible = true;
  5590. this.toneMapped = true;
  5591. this.userData = {};
  5592. this.version = 0;
  5593. this._alphaTest = 0;
  5594. }
  5595. get alphaTest() {
  5596. return this._alphaTest;
  5597. }
  5598. set alphaTest( value ) {
  5599. if ( this._alphaTest > 0 !== value > 0 ) {
  5600. this.version ++;
  5601. }
  5602. this._alphaTest = value;
  5603. }
  5604. // onBeforeRender and onBeforeCompile only supported in WebGLRenderer
  5605. onBeforeRender( /* renderer, scene, camera, geometry, object, group */ ) {}
  5606. onBeforeCompile( /* shaderobject, renderer */ ) {}
  5607. customProgramCacheKey() {
  5608. return this.onBeforeCompile.toString();
  5609. }
  5610. setValues( values ) {
  5611. if ( values === undefined ) return;
  5612. for ( const key in values ) {
  5613. const newValue = values[ key ];
  5614. if ( newValue === undefined ) {
  5615. console.warn( `THREE.Material: parameter '${ key }' has value of undefined.` );
  5616. continue;
  5617. }
  5618. const currentValue = this[ key ];
  5619. if ( currentValue === undefined ) {
  5620. console.warn( `THREE.Material: '${ key }' is not a property of THREE.${ this.type }.` );
  5621. continue;
  5622. }
  5623. if ( currentValue && currentValue.isColor ) {
  5624. currentValue.set( newValue );
  5625. } else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
  5626. currentValue.copy( newValue );
  5627. } else {
  5628. this[ key ] = newValue;
  5629. }
  5630. }
  5631. }
  5632. toJSON( meta ) {
  5633. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  5634. if ( isRootObject ) {
  5635. meta = {
  5636. textures: {},
  5637. images: {}
  5638. };
  5639. }
  5640. const data = {
  5641. metadata: {
  5642. version: 4.6,
  5643. type: 'Material',
  5644. generator: 'Material.toJSON'
  5645. }
  5646. };
  5647. // standard Material serialization
  5648. data.uuid = this.uuid;
  5649. data.type = this.type;
  5650. if ( this.name !== '' ) data.name = this.name;
  5651. if ( this.color && this.color.isColor ) data.color = this.color.getHex();
  5652. if ( this.roughness !== undefined ) data.roughness = this.roughness;
  5653. if ( this.metalness !== undefined ) data.metalness = this.metalness;
  5654. if ( this.sheen !== undefined ) data.sheen = this.sheen;
  5655. if ( this.sheenColor && this.sheenColor.isColor ) data.sheenColor = this.sheenColor.getHex();
  5656. if ( this.sheenRoughness !== undefined ) data.sheenRoughness = this.sheenRoughness;
  5657. if ( this.emissive && this.emissive.isColor ) data.emissive = this.emissive.getHex();
  5658. if ( this.emissiveIntensity !== undefined && this.emissiveIntensity !== 1 ) data.emissiveIntensity = this.emissiveIntensity;
  5659. if ( this.specular && this.specular.isColor ) data.specular = this.specular.getHex();
  5660. if ( this.specularIntensity !== undefined ) data.specularIntensity = this.specularIntensity;
  5661. if ( this.specularColor && this.specularColor.isColor ) data.specularColor = this.specularColor.getHex();
  5662. if ( this.shininess !== undefined ) data.shininess = this.shininess;
  5663. if ( this.clearcoat !== undefined ) data.clearcoat = this.clearcoat;
  5664. if ( this.clearcoatRoughness !== undefined ) data.clearcoatRoughness = this.clearcoatRoughness;
  5665. if ( this.clearcoatMap && this.clearcoatMap.isTexture ) {
  5666. data.clearcoatMap = this.clearcoatMap.toJSON( meta ).uuid;
  5667. }
  5668. if ( this.clearcoatRoughnessMap && this.clearcoatRoughnessMap.isTexture ) {
  5669. data.clearcoatRoughnessMap = this.clearcoatRoughnessMap.toJSON( meta ).uuid;
  5670. }
  5671. if ( this.clearcoatNormalMap && this.clearcoatNormalMap.isTexture ) {
  5672. data.clearcoatNormalMap = this.clearcoatNormalMap.toJSON( meta ).uuid;
  5673. data.clearcoatNormalScale = this.clearcoatNormalScale.toArray();
  5674. }
  5675. if ( this.dispersion !== undefined ) data.dispersion = this.dispersion;
  5676. if ( this.iridescence !== undefined ) data.iridescence = this.iridescence;
  5677. if ( this.iridescenceIOR !== undefined ) data.iridescenceIOR = this.iridescenceIOR;
  5678. if ( this.iridescenceThicknessRange !== undefined ) data.iridescenceThicknessRange = this.iridescenceThicknessRange;
  5679. if ( this.iridescenceMap && this.iridescenceMap.isTexture ) {
  5680. data.iridescenceMap = this.iridescenceMap.toJSON( meta ).uuid;
  5681. }
  5682. if ( this.iridescenceThicknessMap && this.iridescenceThicknessMap.isTexture ) {
  5683. data.iridescenceThicknessMap = this.iridescenceThicknessMap.toJSON( meta ).uuid;
  5684. }
  5685. if ( this.anisotropy !== undefined ) data.anisotropy = this.anisotropy;
  5686. if ( this.anisotropyRotation !== undefined ) data.anisotropyRotation = this.anisotropyRotation;
  5687. if ( this.anisotropyMap && this.anisotropyMap.isTexture ) {
  5688. data.anisotropyMap = this.anisotropyMap.toJSON( meta ).uuid;
  5689. }
  5690. if ( this.map && this.map.isTexture ) data.map = this.map.toJSON( meta ).uuid;
  5691. if ( this.matcap && this.matcap.isTexture ) data.matcap = this.matcap.toJSON( meta ).uuid;
  5692. if ( this.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  5693. if ( this.lightMap && this.lightMap.isTexture ) {
  5694. data.lightMap = this.lightMap.toJSON( meta ).uuid;
  5695. data.lightMapIntensity = this.lightMapIntensity;
  5696. }
  5697. if ( this.aoMap && this.aoMap.isTexture ) {
  5698. data.aoMap = this.aoMap.toJSON( meta ).uuid;
  5699. data.aoMapIntensity = this.aoMapIntensity;
  5700. }
  5701. if ( this.bumpMap && this.bumpMap.isTexture ) {
  5702. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  5703. data.bumpScale = this.bumpScale;
  5704. }
  5705. if ( this.normalMap && this.normalMap.isTexture ) {
  5706. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  5707. data.normalMapType = this.normalMapType;
  5708. data.normalScale = this.normalScale.toArray();
  5709. }
  5710. if ( this.displacementMap && this.displacementMap.isTexture ) {
  5711. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  5712. data.displacementScale = this.displacementScale;
  5713. data.displacementBias = this.displacementBias;
  5714. }
  5715. if ( this.roughnessMap && this.roughnessMap.isTexture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  5716. if ( this.metalnessMap && this.metalnessMap.isTexture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  5717. if ( this.emissiveMap && this.emissiveMap.isTexture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  5718. if ( this.specularMap && this.specularMap.isTexture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  5719. if ( this.specularIntensityMap && this.specularIntensityMap.isTexture ) data.specularIntensityMap = this.specularIntensityMap.toJSON( meta ).uuid;
  5720. if ( this.specularColorMap && this.specularColorMap.isTexture ) data.specularColorMap = this.specularColorMap.toJSON( meta ).uuid;
  5721. if ( this.envMap && this.envMap.isTexture ) {
  5722. data.envMap = this.envMap.toJSON( meta ).uuid;
  5723. if ( this.combine !== undefined ) data.combine = this.combine;
  5724. }
  5725. if ( this.envMapRotation !== undefined ) data.envMapRotation = this.envMapRotation.toArray();
  5726. if ( this.envMapIntensity !== undefined ) data.envMapIntensity = this.envMapIntensity;
  5727. if ( this.reflectivity !== undefined ) data.reflectivity = this.reflectivity;
  5728. if ( this.refractionRatio !== undefined ) data.refractionRatio = this.refractionRatio;
  5729. if ( this.gradientMap && this.gradientMap.isTexture ) {
  5730. data.gradientMap = this.gradientMap.toJSON( meta ).uuid;
  5731. }
  5732. if ( this.transmission !== undefined ) data.transmission = this.transmission;
  5733. if ( this.transmissionMap && this.transmissionMap.isTexture ) data.transmissionMap = this.transmissionMap.toJSON( meta ).uuid;
  5734. if ( this.thickness !== undefined ) data.thickness = this.thickness;
  5735. if ( this.thicknessMap && this.thicknessMap.isTexture ) data.thicknessMap = this.thicknessMap.toJSON( meta ).uuid;
  5736. if ( this.attenuationDistance !== undefined && this.attenuationDistance !== Infinity ) data.attenuationDistance = this.attenuationDistance;
  5737. if ( this.attenuationColor !== undefined ) data.attenuationColor = this.attenuationColor.getHex();
  5738. if ( this.size !== undefined ) data.size = this.size;
  5739. if ( this.shadowSide !== null ) data.shadowSide = this.shadowSide;
  5740. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  5741. if ( this.blending !== NormalBlending ) data.blending = this.blending;
  5742. if ( this.side !== FrontSide ) data.side = this.side;
  5743. if ( this.vertexColors === true ) data.vertexColors = true;
  5744. if ( this.opacity < 1 ) data.opacity = this.opacity;
  5745. if ( this.transparent === true ) data.transparent = true;
  5746. if ( this.blendSrc !== SrcAlphaFactor ) data.blendSrc = this.blendSrc;
  5747. if ( this.blendDst !== OneMinusSrcAlphaFactor ) data.blendDst = this.blendDst;
  5748. if ( this.blendEquation !== AddEquation ) data.blendEquation = this.blendEquation;
  5749. if ( this.blendSrcAlpha !== null ) data.blendSrcAlpha = this.blendSrcAlpha;
  5750. if ( this.blendDstAlpha !== null ) data.blendDstAlpha = this.blendDstAlpha;
  5751. if ( this.blendEquationAlpha !== null ) data.blendEquationAlpha = this.blendEquationAlpha;
  5752. if ( this.blendColor && this.blendColor.isColor ) data.blendColor = this.blendColor.getHex();
  5753. if ( this.blendAlpha !== 0 ) data.blendAlpha = this.blendAlpha;
  5754. if ( this.depthFunc !== LessEqualDepth ) data.depthFunc = this.depthFunc;
  5755. if ( this.depthTest === false ) data.depthTest = this.depthTest;
  5756. if ( this.depthWrite === false ) data.depthWrite = this.depthWrite;
  5757. if ( this.colorWrite === false ) data.colorWrite = this.colorWrite;
  5758. if ( this.stencilWriteMask !== 0xff ) data.stencilWriteMask = this.stencilWriteMask;
  5759. if ( this.stencilFunc !== AlwaysStencilFunc ) data.stencilFunc = this.stencilFunc;
  5760. if ( this.stencilRef !== 0 ) data.stencilRef = this.stencilRef;
  5761. if ( this.stencilFuncMask !== 0xff ) data.stencilFuncMask = this.stencilFuncMask;
  5762. if ( this.stencilFail !== KeepStencilOp ) data.stencilFail = this.stencilFail;
  5763. if ( this.stencilZFail !== KeepStencilOp ) data.stencilZFail = this.stencilZFail;
  5764. if ( this.stencilZPass !== KeepStencilOp ) data.stencilZPass = this.stencilZPass;
  5765. if ( this.stencilWrite === true ) data.stencilWrite = this.stencilWrite;
  5766. // rotation (SpriteMaterial)
  5767. if ( this.rotation !== undefined && this.rotation !== 0 ) data.rotation = this.rotation;
  5768. if ( this.polygonOffset === true ) data.polygonOffset = true;
  5769. if ( this.polygonOffsetFactor !== 0 ) data.polygonOffsetFactor = this.polygonOffsetFactor;
  5770. if ( this.polygonOffsetUnits !== 0 ) data.polygonOffsetUnits = this.polygonOffsetUnits;
  5771. if ( this.linewidth !== undefined && this.linewidth !== 1 ) data.linewidth = this.linewidth;
  5772. if ( this.dashSize !== undefined ) data.dashSize = this.dashSize;
  5773. if ( this.gapSize !== undefined ) data.gapSize = this.gapSize;
  5774. if ( this.scale !== undefined ) data.scale = this.scale;
  5775. if ( this.dithering === true ) data.dithering = true;
  5776. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  5777. if ( this.alphaHash === true ) data.alphaHash = true;
  5778. if ( this.alphaToCoverage === true ) data.alphaToCoverage = true;
  5779. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = true;
  5780. if ( this.forceSinglePass === true ) data.forceSinglePass = true;
  5781. if ( this.wireframe === true ) data.wireframe = true;
  5782. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  5783. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  5784. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  5785. if ( this.flatShading === true ) data.flatShading = true;
  5786. if ( this.visible === false ) data.visible = false;
  5787. if ( this.toneMapped === false ) data.toneMapped = false;
  5788. if ( this.fog === false ) data.fog = false;
  5789. if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;
  5790. // TODO: Copied from Object3D.toJSON
  5791. function extractFromCache( cache ) {
  5792. const values = [];
  5793. for ( const key in cache ) {
  5794. const data = cache[ key ];
  5795. delete data.metadata;
  5796. values.push( data );
  5797. }
  5798. return values;
  5799. }
  5800. if ( isRootObject ) {
  5801. const textures = extractFromCache( meta.textures );
  5802. const images = extractFromCache( meta.images );
  5803. if ( textures.length > 0 ) data.textures = textures;
  5804. if ( images.length > 0 ) data.images = images;
  5805. }
  5806. return data;
  5807. }
  5808. clone() {
  5809. return new this.constructor().copy( this );
  5810. }
  5811. copy( source ) {
  5812. this.name = source.name;
  5813. this.blending = source.blending;
  5814. this.side = source.side;
  5815. this.vertexColors = source.vertexColors;
  5816. this.opacity = source.opacity;
  5817. this.transparent = source.transparent;
  5818. this.blendSrc = source.blendSrc;
  5819. this.blendDst = source.blendDst;
  5820. this.blendEquation = source.blendEquation;
  5821. this.blendSrcAlpha = source.blendSrcAlpha;
  5822. this.blendDstAlpha = source.blendDstAlpha;
  5823. this.blendEquationAlpha = source.blendEquationAlpha;
  5824. this.blendColor.copy( source.blendColor );
  5825. this.blendAlpha = source.blendAlpha;
  5826. this.depthFunc = source.depthFunc;
  5827. this.depthTest = source.depthTest;
  5828. this.depthWrite = source.depthWrite;
  5829. this.stencilWriteMask = source.stencilWriteMask;
  5830. this.stencilFunc = source.stencilFunc;
  5831. this.stencilRef = source.stencilRef;
  5832. this.stencilFuncMask = source.stencilFuncMask;
  5833. this.stencilFail = source.stencilFail;
  5834. this.stencilZFail = source.stencilZFail;
  5835. this.stencilZPass = source.stencilZPass;
  5836. this.stencilWrite = source.stencilWrite;
  5837. const srcPlanes = source.clippingPlanes;
  5838. let dstPlanes = null;
  5839. if ( srcPlanes !== null ) {
  5840. const n = srcPlanes.length;
  5841. dstPlanes = new Array( n );
  5842. for ( let i = 0; i !== n; ++ i ) {
  5843. dstPlanes[ i ] = srcPlanes[ i ].clone();
  5844. }
  5845. }
  5846. this.clippingPlanes = dstPlanes;
  5847. this.clipIntersection = source.clipIntersection;
  5848. this.clipShadows = source.clipShadows;
  5849. this.shadowSide = source.shadowSide;
  5850. this.colorWrite = source.colorWrite;
  5851. this.precision = source.precision;
  5852. this.polygonOffset = source.polygonOffset;
  5853. this.polygonOffsetFactor = source.polygonOffsetFactor;
  5854. this.polygonOffsetUnits = source.polygonOffsetUnits;
  5855. this.dithering = source.dithering;
  5856. this.alphaTest = source.alphaTest;
  5857. this.alphaHash = source.alphaHash;
  5858. this.alphaToCoverage = source.alphaToCoverage;
  5859. this.premultipliedAlpha = source.premultipliedAlpha;
  5860. this.forceSinglePass = source.forceSinglePass;
  5861. this.visible = source.visible;
  5862. this.toneMapped = source.toneMapped;
  5863. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  5864. return this;
  5865. }
  5866. dispose() {
  5867. this.dispatchEvent( { type: 'dispose' } );
  5868. }
  5869. set needsUpdate( value ) {
  5870. if ( value === true ) this.version ++;
  5871. }
  5872. onBuild( /* shaderobject, renderer */ ) {
  5873. console.warn( 'Material: onBuild() has been removed.' ); // @deprecated, r166
  5874. }
  5875. }
  5876. class MeshBasicMaterial extends Material {
  5877. constructor( parameters ) {
  5878. super();
  5879. this.isMeshBasicMaterial = true;
  5880. this.type = 'MeshBasicMaterial';
  5881. this.color = new Color( 0xffffff ); // emissive
  5882. this.map = null;
  5883. this.lightMap = null;
  5884. this.lightMapIntensity = 1.0;
  5885. this.aoMap = null;
  5886. this.aoMapIntensity = 1.0;
  5887. this.specularMap = null;
  5888. this.alphaMap = null;
  5889. this.envMap = null;
  5890. this.envMapRotation = new Euler();
  5891. this.combine = MultiplyOperation;
  5892. this.reflectivity = 1;
  5893. this.refractionRatio = 0.98;
  5894. this.wireframe = false;
  5895. this.wireframeLinewidth = 1;
  5896. this.wireframeLinecap = 'round';
  5897. this.wireframeLinejoin = 'round';
  5898. this.fog = true;
  5899. this.setValues( parameters );
  5900. }
  5901. copy( source ) {
  5902. super.copy( source );
  5903. this.color.copy( source.color );
  5904. this.map = source.map;
  5905. this.lightMap = source.lightMap;
  5906. this.lightMapIntensity = source.lightMapIntensity;
  5907. this.aoMap = source.aoMap;
  5908. this.aoMapIntensity = source.aoMapIntensity;
  5909. this.specularMap = source.specularMap;
  5910. this.alphaMap = source.alphaMap;
  5911. this.envMap = source.envMap;
  5912. this.envMapRotation.copy( source.envMapRotation );
  5913. this.combine = source.combine;
  5914. this.reflectivity = source.reflectivity;
  5915. this.refractionRatio = source.refractionRatio;
  5916. this.wireframe = source.wireframe;
  5917. this.wireframeLinewidth = source.wireframeLinewidth;
  5918. this.wireframeLinecap = source.wireframeLinecap;
  5919. this.wireframeLinejoin = source.wireframeLinejoin;
  5920. this.fog = source.fog;
  5921. return this;
  5922. }
  5923. }
  5924. // Fast Half Float Conversions, http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf
  5925. const _tables = /*@__PURE__*/ _generateTables();
  5926. function _generateTables() {
  5927. // float32 to float16 helpers
  5928. const buffer = new ArrayBuffer( 4 );
  5929. const floatView = new Float32Array( buffer );
  5930. const uint32View = new Uint32Array( buffer );
  5931. const baseTable = new Uint32Array( 512 );
  5932. const shiftTable = new Uint32Array( 512 );
  5933. for ( let i = 0; i < 256; ++ i ) {
  5934. const e = i - 127;
  5935. // very small number (0, -0)
  5936. if ( e < - 27 ) {
  5937. baseTable[ i ] = 0x0000;
  5938. baseTable[ i | 0x100 ] = 0x8000;
  5939. shiftTable[ i ] = 24;
  5940. shiftTable[ i | 0x100 ] = 24;
  5941. // small number (denorm)
  5942. } else if ( e < - 14 ) {
  5943. baseTable[ i ] = 0x0400 >> ( - e - 14 );
  5944. baseTable[ i | 0x100 ] = ( 0x0400 >> ( - e - 14 ) ) | 0x8000;
  5945. shiftTable[ i ] = - e - 1;
  5946. shiftTable[ i | 0x100 ] = - e - 1;
  5947. // normal number
  5948. } else if ( e <= 15 ) {
  5949. baseTable[ i ] = ( e + 15 ) << 10;
  5950. baseTable[ i | 0x100 ] = ( ( e + 15 ) << 10 ) | 0x8000;
  5951. shiftTable[ i ] = 13;
  5952. shiftTable[ i | 0x100 ] = 13;
  5953. // large number (Infinity, -Infinity)
  5954. } else if ( e < 128 ) {
  5955. baseTable[ i ] = 0x7c00;
  5956. baseTable[ i | 0x100 ] = 0xfc00;
  5957. shiftTable[ i ] = 24;
  5958. shiftTable[ i | 0x100 ] = 24;
  5959. // stay (NaN, Infinity, -Infinity)
  5960. } else {
  5961. baseTable[ i ] = 0x7c00;
  5962. baseTable[ i | 0x100 ] = 0xfc00;
  5963. shiftTable[ i ] = 13;
  5964. shiftTable[ i | 0x100 ] = 13;
  5965. }
  5966. }
  5967. // float16 to float32 helpers
  5968. const mantissaTable = new Uint32Array( 2048 );
  5969. const exponentTable = new Uint32Array( 64 );
  5970. const offsetTable = new Uint32Array( 64 );
  5971. for ( let i = 1; i < 1024; ++ i ) {
  5972. let m = i << 13; // zero pad mantissa bits
  5973. let e = 0; // zero exponent
  5974. // normalized
  5975. while ( ( m & 0x00800000 ) === 0 ) {
  5976. m <<= 1;
  5977. e -= 0x00800000; // decrement exponent
  5978. }
  5979. m &= ~ 0x00800000; // clear leading 1 bit
  5980. e += 0x38800000; // adjust bias
  5981. mantissaTable[ i ] = m | e;
  5982. }
  5983. for ( let i = 1024; i < 2048; ++ i ) {
  5984. mantissaTable[ i ] = 0x38000000 + ( ( i - 1024 ) << 13 );
  5985. }
  5986. for ( let i = 1; i < 31; ++ i ) {
  5987. exponentTable[ i ] = i << 23;
  5988. }
  5989. exponentTable[ 31 ] = 0x47800000;
  5990. exponentTable[ 32 ] = 0x80000000;
  5991. for ( let i = 33; i < 63; ++ i ) {
  5992. exponentTable[ i ] = 0x80000000 + ( ( i - 32 ) << 23 );
  5993. }
  5994. exponentTable[ 63 ] = 0xc7800000;
  5995. for ( let i = 1; i < 64; ++ i ) {
  5996. if ( i !== 32 ) {
  5997. offsetTable[ i ] = 1024;
  5998. }
  5999. }
  6000. return {
  6001. floatView: floatView,
  6002. uint32View: uint32View,
  6003. baseTable: baseTable,
  6004. shiftTable: shiftTable,
  6005. mantissaTable: mantissaTable,
  6006. exponentTable: exponentTable,
  6007. offsetTable: offsetTable
  6008. };
  6009. }
  6010. // float32 to float16
  6011. function toHalfFloat( val ) {
  6012. if ( Math.abs( val ) > 65504 ) console.warn( 'THREE.DataUtils.toHalfFloat(): Value out of range.' );
  6013. val = clamp( val, - 65504, 65504 );
  6014. _tables.floatView[ 0 ] = val;
  6015. const f = _tables.uint32View[ 0 ];
  6016. const e = ( f >> 23 ) & 0x1ff;
  6017. return _tables.baseTable[ e ] + ( ( f & 0x007fffff ) >> _tables.shiftTable[ e ] );
  6018. }
  6019. // float16 to float32
  6020. function fromHalfFloat( val ) {
  6021. const m = val >> 10;
  6022. _tables.uint32View[ 0 ] = _tables.mantissaTable[ _tables.offsetTable[ m ] + ( val & 0x3ff ) ] + _tables.exponentTable[ m ];
  6023. return _tables.floatView[ 0 ];
  6024. }
  6025. const DataUtils = {
  6026. toHalfFloat: toHalfFloat,
  6027. fromHalfFloat: fromHalfFloat,
  6028. };
  6029. const _vector$9 = /*@__PURE__*/ new Vector3();
  6030. const _vector2$1 = /*@__PURE__*/ new Vector2();
  6031. class BufferAttribute {
  6032. constructor( array, itemSize, normalized = false ) {
  6033. if ( Array.isArray( array ) ) {
  6034. throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
  6035. }
  6036. this.isBufferAttribute = true;
  6037. this.name = '';
  6038. this.array = array;
  6039. this.itemSize = itemSize;
  6040. this.count = array !== undefined ? array.length / itemSize : 0;
  6041. this.normalized = normalized;
  6042. this.usage = StaticDrawUsage;
  6043. this.updateRanges = [];
  6044. this.gpuType = FloatType;
  6045. this.version = 0;
  6046. }
  6047. onUploadCallback() {}
  6048. set needsUpdate( value ) {
  6049. if ( value === true ) this.version ++;
  6050. }
  6051. setUsage( value ) {
  6052. this.usage = value;
  6053. return this;
  6054. }
  6055. addUpdateRange( start, count ) {
  6056. this.updateRanges.push( { start, count } );
  6057. }
  6058. clearUpdateRanges() {
  6059. this.updateRanges.length = 0;
  6060. }
  6061. copy( source ) {
  6062. this.name = source.name;
  6063. this.array = new source.array.constructor( source.array );
  6064. this.itemSize = source.itemSize;
  6065. this.count = source.count;
  6066. this.normalized = source.normalized;
  6067. this.usage = source.usage;
  6068. this.gpuType = source.gpuType;
  6069. return this;
  6070. }
  6071. copyAt( index1, attribute, index2 ) {
  6072. index1 *= this.itemSize;
  6073. index2 *= attribute.itemSize;
  6074. for ( let i = 0, l = this.itemSize; i < l; i ++ ) {
  6075. this.array[ index1 + i ] = attribute.array[ index2 + i ];
  6076. }
  6077. return this;
  6078. }
  6079. copyArray( array ) {
  6080. this.array.set( array );
  6081. return this;
  6082. }
  6083. applyMatrix3( m ) {
  6084. if ( this.itemSize === 2 ) {
  6085. for ( let i = 0, l = this.count; i < l; i ++ ) {
  6086. _vector2$1.fromBufferAttribute( this, i );
  6087. _vector2$1.applyMatrix3( m );
  6088. this.setXY( i, _vector2$1.x, _vector2$1.y );
  6089. }
  6090. } else if ( this.itemSize === 3 ) {
  6091. for ( let i = 0, l = this.count; i < l; i ++ ) {
  6092. _vector$9.fromBufferAttribute( this, i );
  6093. _vector$9.applyMatrix3( m );
  6094. this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z );
  6095. }
  6096. }
  6097. return this;
  6098. }
  6099. applyMatrix4( m ) {
  6100. for ( let i = 0, l = this.count; i < l; i ++ ) {
  6101. _vector$9.fromBufferAttribute( this, i );
  6102. _vector$9.applyMatrix4( m );
  6103. this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z );
  6104. }
  6105. return this;
  6106. }
  6107. applyNormalMatrix( m ) {
  6108. for ( let i = 0, l = this.count; i < l; i ++ ) {
  6109. _vector$9.fromBufferAttribute( this, i );
  6110. _vector$9.applyNormalMatrix( m );
  6111. this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z );
  6112. }
  6113. return this;
  6114. }
  6115. transformDirection( m ) {
  6116. for ( let i = 0, l = this.count; i < l; i ++ ) {
  6117. _vector$9.fromBufferAttribute( this, i );
  6118. _vector$9.transformDirection( m );
  6119. this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z );
  6120. }
  6121. return this;
  6122. }
  6123. set( value, offset = 0 ) {
  6124. // Matching BufferAttribute constructor, do not normalize the array.
  6125. this.array.set( value, offset );
  6126. return this;
  6127. }
  6128. getComponent( index, component ) {
  6129. let value = this.array[ index * this.itemSize + component ];
  6130. if ( this.normalized ) value = denormalize( value, this.array );
  6131. return value;
  6132. }
  6133. setComponent( index, component, value ) {
  6134. if ( this.normalized ) value = normalize( value, this.array );
  6135. this.array[ index * this.itemSize + component ] = value;
  6136. return this;
  6137. }
  6138. getX( index ) {
  6139. let x = this.array[ index * this.itemSize ];
  6140. if ( this.normalized ) x = denormalize( x, this.array );
  6141. return x;
  6142. }
  6143. setX( index, x ) {
  6144. if ( this.normalized ) x = normalize( x, this.array );
  6145. this.array[ index * this.itemSize ] = x;
  6146. return this;
  6147. }
  6148. getY( index ) {
  6149. let y = this.array[ index * this.itemSize + 1 ];
  6150. if ( this.normalized ) y = denormalize( y, this.array );
  6151. return y;
  6152. }
  6153. setY( index, y ) {
  6154. if ( this.normalized ) y = normalize( y, this.array );
  6155. this.array[ index * this.itemSize + 1 ] = y;
  6156. return this;
  6157. }
  6158. getZ( index ) {
  6159. let z = this.array[ index * this.itemSize + 2 ];
  6160. if ( this.normalized ) z = denormalize( z, this.array );
  6161. return z;
  6162. }
  6163. setZ( index, z ) {
  6164. if ( this.normalized ) z = normalize( z, this.array );
  6165. this.array[ index * this.itemSize + 2 ] = z;
  6166. return this;
  6167. }
  6168. getW( index ) {
  6169. let w = this.array[ index * this.itemSize + 3 ];
  6170. if ( this.normalized ) w = denormalize( w, this.array );
  6171. return w;
  6172. }
  6173. setW( index, w ) {
  6174. if ( this.normalized ) w = normalize( w, this.array );
  6175. this.array[ index * this.itemSize + 3 ] = w;
  6176. return this;
  6177. }
  6178. setXY( index, x, y ) {
  6179. index *= this.itemSize;
  6180. if ( this.normalized ) {
  6181. x = normalize( x, this.array );
  6182. y = normalize( y, this.array );
  6183. }
  6184. this.array[ index + 0 ] = x;
  6185. this.array[ index + 1 ] = y;
  6186. return this;
  6187. }
  6188. setXYZ( index, x, y, z ) {
  6189. index *= this.itemSize;
  6190. if ( this.normalized ) {
  6191. x = normalize( x, this.array );
  6192. y = normalize( y, this.array );
  6193. z = normalize( z, this.array );
  6194. }
  6195. this.array[ index + 0 ] = x;
  6196. this.array[ index + 1 ] = y;
  6197. this.array[ index + 2 ] = z;
  6198. return this;
  6199. }
  6200. setXYZW( index, x, y, z, w ) {
  6201. index *= this.itemSize;
  6202. if ( this.normalized ) {
  6203. x = normalize( x, this.array );
  6204. y = normalize( y, this.array );
  6205. z = normalize( z, this.array );
  6206. w = normalize( w, this.array );
  6207. }
  6208. this.array[ index + 0 ] = x;
  6209. this.array[ index + 1 ] = y;
  6210. this.array[ index + 2 ] = z;
  6211. this.array[ index + 3 ] = w;
  6212. return this;
  6213. }
  6214. onUpload( callback ) {
  6215. this.onUploadCallback = callback;
  6216. return this;
  6217. }
  6218. clone() {
  6219. return new this.constructor( this.array, this.itemSize ).copy( this );
  6220. }
  6221. toJSON() {
  6222. const data = {
  6223. itemSize: this.itemSize,
  6224. type: this.array.constructor.name,
  6225. array: Array.from( this.array ),
  6226. normalized: this.normalized
  6227. };
  6228. if ( this.name !== '' ) data.name = this.name;
  6229. if ( this.usage !== StaticDrawUsage ) data.usage = this.usage;
  6230. return data;
  6231. }
  6232. }
  6233. //
  6234. class Int8BufferAttribute extends BufferAttribute {
  6235. constructor( array, itemSize, normalized ) {
  6236. super( new Int8Array( array ), itemSize, normalized );
  6237. }
  6238. }
  6239. class Uint8BufferAttribute extends BufferAttribute {
  6240. constructor( array, itemSize, normalized ) {
  6241. super( new Uint8Array( array ), itemSize, normalized );
  6242. }
  6243. }
  6244. class Uint8ClampedBufferAttribute extends BufferAttribute {
  6245. constructor( array, itemSize, normalized ) {
  6246. super( new Uint8ClampedArray( array ), itemSize, normalized );
  6247. }
  6248. }
  6249. class Int16BufferAttribute extends BufferAttribute {
  6250. constructor( array, itemSize, normalized ) {
  6251. super( new Int16Array( array ), itemSize, normalized );
  6252. }
  6253. }
  6254. class Uint16BufferAttribute extends BufferAttribute {
  6255. constructor( array, itemSize, normalized ) {
  6256. super( new Uint16Array( array ), itemSize, normalized );
  6257. }
  6258. }
  6259. class Int32BufferAttribute extends BufferAttribute {
  6260. constructor( array, itemSize, normalized ) {
  6261. super( new Int32Array( array ), itemSize, normalized );
  6262. }
  6263. }
  6264. class Uint32BufferAttribute extends BufferAttribute {
  6265. constructor( array, itemSize, normalized ) {
  6266. super( new Uint32Array( array ), itemSize, normalized );
  6267. }
  6268. }
  6269. class Float16BufferAttribute extends BufferAttribute {
  6270. constructor( array, itemSize, normalized ) {
  6271. super( new Uint16Array( array ), itemSize, normalized );
  6272. this.isFloat16BufferAttribute = true;
  6273. }
  6274. getX( index ) {
  6275. let x = fromHalfFloat( this.array[ index * this.itemSize ] );
  6276. if ( this.normalized ) x = denormalize( x, this.array );
  6277. return x;
  6278. }
  6279. setX( index, x ) {
  6280. if ( this.normalized ) x = normalize( x, this.array );
  6281. this.array[ index * this.itemSize ] = toHalfFloat( x );
  6282. return this;
  6283. }
  6284. getY( index ) {
  6285. let y = fromHalfFloat( this.array[ index * this.itemSize + 1 ] );
  6286. if ( this.normalized ) y = denormalize( y, this.array );
  6287. return y;
  6288. }
  6289. setY( index, y ) {
  6290. if ( this.normalized ) y = normalize( y, this.array );
  6291. this.array[ index * this.itemSize + 1 ] = toHalfFloat( y );
  6292. return this;
  6293. }
  6294. getZ( index ) {
  6295. let z = fromHalfFloat( this.array[ index * this.itemSize + 2 ] );
  6296. if ( this.normalized ) z = denormalize( z, this.array );
  6297. return z;
  6298. }
  6299. setZ( index, z ) {
  6300. if ( this.normalized ) z = normalize( z, this.array );
  6301. this.array[ index * this.itemSize + 2 ] = toHalfFloat( z );
  6302. return this;
  6303. }
  6304. getW( index ) {
  6305. let w = fromHalfFloat( this.array[ index * this.itemSize + 3 ] );
  6306. if ( this.normalized ) w = denormalize( w, this.array );
  6307. return w;
  6308. }
  6309. setW( index, w ) {
  6310. if ( this.normalized ) w = normalize( w, this.array );
  6311. this.array[ index * this.itemSize + 3 ] = toHalfFloat( w );
  6312. return this;
  6313. }
  6314. setXY( index, x, y ) {
  6315. index *= this.itemSize;
  6316. if ( this.normalized ) {
  6317. x = normalize( x, this.array );
  6318. y = normalize( y, this.array );
  6319. }
  6320. this.array[ index + 0 ] = toHalfFloat( x );
  6321. this.array[ index + 1 ] = toHalfFloat( y );
  6322. return this;
  6323. }
  6324. setXYZ( index, x, y, z ) {
  6325. index *= this.itemSize;
  6326. if ( this.normalized ) {
  6327. x = normalize( x, this.array );
  6328. y = normalize( y, this.array );
  6329. z = normalize( z, this.array );
  6330. }
  6331. this.array[ index + 0 ] = toHalfFloat( x );
  6332. this.array[ index + 1 ] = toHalfFloat( y );
  6333. this.array[ index + 2 ] = toHalfFloat( z );
  6334. return this;
  6335. }
  6336. setXYZW( index, x, y, z, w ) {
  6337. index *= this.itemSize;
  6338. if ( this.normalized ) {
  6339. x = normalize( x, this.array );
  6340. y = normalize( y, this.array );
  6341. z = normalize( z, this.array );
  6342. w = normalize( w, this.array );
  6343. }
  6344. this.array[ index + 0 ] = toHalfFloat( x );
  6345. this.array[ index + 1 ] = toHalfFloat( y );
  6346. this.array[ index + 2 ] = toHalfFloat( z );
  6347. this.array[ index + 3 ] = toHalfFloat( w );
  6348. return this;
  6349. }
  6350. }
  6351. class Float32BufferAttribute extends BufferAttribute {
  6352. constructor( array, itemSize, normalized ) {
  6353. super( new Float32Array( array ), itemSize, normalized );
  6354. }
  6355. }
  6356. let _id$1 = 0;
  6357. const _m1 = /*@__PURE__*/ new Matrix4();
  6358. const _obj = /*@__PURE__*/ new Object3D();
  6359. const _offset = /*@__PURE__*/ new Vector3();
  6360. const _box$2 = /*@__PURE__*/ new Box3();
  6361. const _boxMorphTargets = /*@__PURE__*/ new Box3();
  6362. const _vector$8 = /*@__PURE__*/ new Vector3();
  6363. class BufferGeometry extends EventDispatcher {
  6364. constructor() {
  6365. super();
  6366. this.isBufferGeometry = true;
  6367. Object.defineProperty( this, 'id', { value: _id$1 ++ } );
  6368. this.uuid = generateUUID();
  6369. this.name = '';
  6370. this.type = 'BufferGeometry';
  6371. this.index = null;
  6372. this.indirect = null;
  6373. this.attributes = {};
  6374. this.morphAttributes = {};
  6375. this.morphTargetsRelative = false;
  6376. this.groups = [];
  6377. this.boundingBox = null;
  6378. this.boundingSphere = null;
  6379. this.drawRange = { start: 0, count: Infinity };
  6380. this.userData = {};
  6381. }
  6382. getIndex() {
  6383. return this.index;
  6384. }
  6385. setIndex( index ) {
  6386. if ( Array.isArray( index ) ) {
  6387. this.index = new ( arrayNeedsUint32( index ) ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 );
  6388. } else {
  6389. this.index = index;
  6390. }
  6391. return this;
  6392. }
  6393. setIndirect( indirect ) {
  6394. this.indirect = indirect;
  6395. return this;
  6396. }
  6397. getIndirect() {
  6398. return this.indirect;
  6399. }
  6400. getAttribute( name ) {
  6401. return this.attributes[ name ];
  6402. }
  6403. setAttribute( name, attribute ) {
  6404. this.attributes[ name ] = attribute;
  6405. return this;
  6406. }
  6407. deleteAttribute( name ) {
  6408. delete this.attributes[ name ];
  6409. return this;
  6410. }
  6411. hasAttribute( name ) {
  6412. return this.attributes[ name ] !== undefined;
  6413. }
  6414. addGroup( start, count, materialIndex = 0 ) {
  6415. this.groups.push( {
  6416. start: start,
  6417. count: count,
  6418. materialIndex: materialIndex
  6419. } );
  6420. }
  6421. clearGroups() {
  6422. this.groups = [];
  6423. }
  6424. setDrawRange( start, count ) {
  6425. this.drawRange.start = start;
  6426. this.drawRange.count = count;
  6427. }
  6428. applyMatrix4( matrix ) {
  6429. const position = this.attributes.position;
  6430. if ( position !== undefined ) {
  6431. position.applyMatrix4( matrix );
  6432. position.needsUpdate = true;
  6433. }
  6434. const normal = this.attributes.normal;
  6435. if ( normal !== undefined ) {
  6436. const normalMatrix = new Matrix3().getNormalMatrix( matrix );
  6437. normal.applyNormalMatrix( normalMatrix );
  6438. normal.needsUpdate = true;
  6439. }
  6440. const tangent = this.attributes.tangent;
  6441. if ( tangent !== undefined ) {
  6442. tangent.transformDirection( matrix );
  6443. tangent.needsUpdate = true;
  6444. }
  6445. if ( this.boundingBox !== null ) {
  6446. this.computeBoundingBox();
  6447. }
  6448. if ( this.boundingSphere !== null ) {
  6449. this.computeBoundingSphere();
  6450. }
  6451. return this;
  6452. }
  6453. applyQuaternion( q ) {
  6454. _m1.makeRotationFromQuaternion( q );
  6455. this.applyMatrix4( _m1 );
  6456. return this;
  6457. }
  6458. rotateX( angle ) {
  6459. // rotate geometry around world x-axis
  6460. _m1.makeRotationX( angle );
  6461. this.applyMatrix4( _m1 );
  6462. return this;
  6463. }
  6464. rotateY( angle ) {
  6465. // rotate geometry around world y-axis
  6466. _m1.makeRotationY( angle );
  6467. this.applyMatrix4( _m1 );
  6468. return this;
  6469. }
  6470. rotateZ( angle ) {
  6471. // rotate geometry around world z-axis
  6472. _m1.makeRotationZ( angle );
  6473. this.applyMatrix4( _m1 );
  6474. return this;
  6475. }
  6476. translate( x, y, z ) {
  6477. // translate geometry
  6478. _m1.makeTranslation( x, y, z );
  6479. this.applyMatrix4( _m1 );
  6480. return this;
  6481. }
  6482. scale( x, y, z ) {
  6483. // scale geometry
  6484. _m1.makeScale( x, y, z );
  6485. this.applyMatrix4( _m1 );
  6486. return this;
  6487. }
  6488. lookAt( vector ) {
  6489. _obj.lookAt( vector );
  6490. _obj.updateMatrix();
  6491. this.applyMatrix4( _obj.matrix );
  6492. return this;
  6493. }
  6494. center() {
  6495. this.computeBoundingBox();
  6496. this.boundingBox.getCenter( _offset ).negate();
  6497. this.translate( _offset.x, _offset.y, _offset.z );
  6498. return this;
  6499. }
  6500. setFromPoints( points ) {
  6501. const positionAttribute = this.getAttribute( 'position' );
  6502. if ( positionAttribute === undefined ) {
  6503. const position = [];
  6504. for ( let i = 0, l = points.length; i < l; i ++ ) {
  6505. const point = points[ i ];
  6506. position.push( point.x, point.y, point.z || 0 );
  6507. }
  6508. this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
  6509. } else {
  6510. const l = Math.min( points.length, positionAttribute.count ); // make sure data do not exceed buffer size
  6511. for ( let i = 0; i < l; i ++ ) {
  6512. const point = points[ i ];
  6513. positionAttribute.setXYZ( i, point.x, point.y, point.z || 0 );
  6514. }
  6515. if ( points.length > positionAttribute.count ) {
  6516. console.warn( 'THREE.BufferGeometry: Buffer size too small for points data. Use .dispose() and create a new geometry.' );
  6517. }
  6518. positionAttribute.needsUpdate = true;
  6519. }
  6520. return this;
  6521. }
  6522. computeBoundingBox() {
  6523. if ( this.boundingBox === null ) {
  6524. this.boundingBox = new Box3();
  6525. }
  6526. const position = this.attributes.position;
  6527. const morphAttributesPosition = this.morphAttributes.position;
  6528. if ( position && position.isGLBufferAttribute ) {
  6529. console.error( 'THREE.BufferGeometry.computeBoundingBox(): GLBufferAttribute requires a manual bounding box.', this );
  6530. this.boundingBox.set(
  6531. new Vector3( - Infinity, - Infinity, - Infinity ),
  6532. new Vector3( + Infinity, + Infinity, + Infinity )
  6533. );
  6534. return;
  6535. }
  6536. if ( position !== undefined ) {
  6537. this.boundingBox.setFromBufferAttribute( position );
  6538. // process morph attributes if present
  6539. if ( morphAttributesPosition ) {
  6540. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  6541. const morphAttribute = morphAttributesPosition[ i ];
  6542. _box$2.setFromBufferAttribute( morphAttribute );
  6543. if ( this.morphTargetsRelative ) {
  6544. _vector$8.addVectors( this.boundingBox.min, _box$2.min );
  6545. this.boundingBox.expandByPoint( _vector$8 );
  6546. _vector$8.addVectors( this.boundingBox.max, _box$2.max );
  6547. this.boundingBox.expandByPoint( _vector$8 );
  6548. } else {
  6549. this.boundingBox.expandByPoint( _box$2.min );
  6550. this.boundingBox.expandByPoint( _box$2.max );
  6551. }
  6552. }
  6553. }
  6554. } else {
  6555. this.boundingBox.makeEmpty();
  6556. }
  6557. if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
  6558. console.error( 'THREE.BufferGeometry.computeBoundingBox(): Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
  6559. }
  6560. }
  6561. computeBoundingSphere() {
  6562. if ( this.boundingSphere === null ) {
  6563. this.boundingSphere = new Sphere();
  6564. }
  6565. const position = this.attributes.position;
  6566. const morphAttributesPosition = this.morphAttributes.position;
  6567. if ( position && position.isGLBufferAttribute ) {
  6568. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): GLBufferAttribute requires a manual bounding sphere.', this );
  6569. this.boundingSphere.set( new Vector3(), Infinity );
  6570. return;
  6571. }
  6572. if ( position ) {
  6573. // first, find the center of the bounding sphere
  6574. const center = this.boundingSphere.center;
  6575. _box$2.setFromBufferAttribute( position );
  6576. // process morph attributes if present
  6577. if ( morphAttributesPosition ) {
  6578. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  6579. const morphAttribute = morphAttributesPosition[ i ];
  6580. _boxMorphTargets.setFromBufferAttribute( morphAttribute );
  6581. if ( this.morphTargetsRelative ) {
  6582. _vector$8.addVectors( _box$2.min, _boxMorphTargets.min );
  6583. _box$2.expandByPoint( _vector$8 );
  6584. _vector$8.addVectors( _box$2.max, _boxMorphTargets.max );
  6585. _box$2.expandByPoint( _vector$8 );
  6586. } else {
  6587. _box$2.expandByPoint( _boxMorphTargets.min );
  6588. _box$2.expandByPoint( _boxMorphTargets.max );
  6589. }
  6590. }
  6591. }
  6592. _box$2.getCenter( center );
  6593. // second, try to find a boundingSphere with a radius smaller than the
  6594. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  6595. let maxRadiusSq = 0;
  6596. for ( let i = 0, il = position.count; i < il; i ++ ) {
  6597. _vector$8.fromBufferAttribute( position, i );
  6598. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$8 ) );
  6599. }
  6600. // process morph attributes if present
  6601. if ( morphAttributesPosition ) {
  6602. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  6603. const morphAttribute = morphAttributesPosition[ i ];
  6604. const morphTargetsRelative = this.morphTargetsRelative;
  6605. for ( let j = 0, jl = morphAttribute.count; j < jl; j ++ ) {
  6606. _vector$8.fromBufferAttribute( morphAttribute, j );
  6607. if ( morphTargetsRelative ) {
  6608. _offset.fromBufferAttribute( position, j );
  6609. _vector$8.add( _offset );
  6610. }
  6611. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$8 ) );
  6612. }
  6613. }
  6614. }
  6615. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  6616. if ( isNaN( this.boundingSphere.radius ) ) {
  6617. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
  6618. }
  6619. }
  6620. }
  6621. computeTangents() {
  6622. const index = this.index;
  6623. const attributes = this.attributes;
  6624. // based on http://www.terathon.com/code/tangent.html
  6625. // (per vertex tangents)
  6626. if ( index === null ||
  6627. attributes.position === undefined ||
  6628. attributes.normal === undefined ||
  6629. attributes.uv === undefined ) {
  6630. console.error( 'THREE.BufferGeometry: .computeTangents() failed. Missing required attributes (index, position, normal or uv)' );
  6631. return;
  6632. }
  6633. const positionAttribute = attributes.position;
  6634. const normalAttribute = attributes.normal;
  6635. const uvAttribute = attributes.uv;
  6636. if ( this.hasAttribute( 'tangent' ) === false ) {
  6637. this.setAttribute( 'tangent', new BufferAttribute( new Float32Array( 4 * positionAttribute.count ), 4 ) );
  6638. }
  6639. const tangentAttribute = this.getAttribute( 'tangent' );
  6640. const tan1 = [], tan2 = [];
  6641. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  6642. tan1[ i ] = new Vector3();
  6643. tan2[ i ] = new Vector3();
  6644. }
  6645. const vA = new Vector3(),
  6646. vB = new Vector3(),
  6647. vC = new Vector3(),
  6648. uvA = new Vector2(),
  6649. uvB = new Vector2(),
  6650. uvC = new Vector2(),
  6651. sdir = new Vector3(),
  6652. tdir = new Vector3();
  6653. function handleTriangle( a, b, c ) {
  6654. vA.fromBufferAttribute( positionAttribute, a );
  6655. vB.fromBufferAttribute( positionAttribute, b );
  6656. vC.fromBufferAttribute( positionAttribute, c );
  6657. uvA.fromBufferAttribute( uvAttribute, a );
  6658. uvB.fromBufferAttribute( uvAttribute, b );
  6659. uvC.fromBufferAttribute( uvAttribute, c );
  6660. vB.sub( vA );
  6661. vC.sub( vA );
  6662. uvB.sub( uvA );
  6663. uvC.sub( uvA );
  6664. const r = 1.0 / ( uvB.x * uvC.y - uvC.x * uvB.y );
  6665. // silently ignore degenerate uv triangles having coincident or colinear vertices
  6666. if ( ! isFinite( r ) ) return;
  6667. sdir.copy( vB ).multiplyScalar( uvC.y ).addScaledVector( vC, - uvB.y ).multiplyScalar( r );
  6668. tdir.copy( vC ).multiplyScalar( uvB.x ).addScaledVector( vB, - uvC.x ).multiplyScalar( r );
  6669. tan1[ a ].add( sdir );
  6670. tan1[ b ].add( sdir );
  6671. tan1[ c ].add( sdir );
  6672. tan2[ a ].add( tdir );
  6673. tan2[ b ].add( tdir );
  6674. tan2[ c ].add( tdir );
  6675. }
  6676. let groups = this.groups;
  6677. if ( groups.length === 0 ) {
  6678. groups = [ {
  6679. start: 0,
  6680. count: index.count
  6681. } ];
  6682. }
  6683. for ( let i = 0, il = groups.length; i < il; ++ i ) {
  6684. const group = groups[ i ];
  6685. const start = group.start;
  6686. const count = group.count;
  6687. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  6688. handleTriangle(
  6689. index.getX( j + 0 ),
  6690. index.getX( j + 1 ),
  6691. index.getX( j + 2 )
  6692. );
  6693. }
  6694. }
  6695. const tmp = new Vector3(), tmp2 = new Vector3();
  6696. const n = new Vector3(), n2 = new Vector3();
  6697. function handleVertex( v ) {
  6698. n.fromBufferAttribute( normalAttribute, v );
  6699. n2.copy( n );
  6700. const t = tan1[ v ];
  6701. // Gram-Schmidt orthogonalize
  6702. tmp.copy( t );
  6703. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  6704. // Calculate handedness
  6705. tmp2.crossVectors( n2, t );
  6706. const test = tmp2.dot( tan2[ v ] );
  6707. const w = ( test < 0.0 ) ? - 1.0 : 1.0;
  6708. tangentAttribute.setXYZW( v, tmp.x, tmp.y, tmp.z, w );
  6709. }
  6710. for ( let i = 0, il = groups.length; i < il; ++ i ) {
  6711. const group = groups[ i ];
  6712. const start = group.start;
  6713. const count = group.count;
  6714. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  6715. handleVertex( index.getX( j + 0 ) );
  6716. handleVertex( index.getX( j + 1 ) );
  6717. handleVertex( index.getX( j + 2 ) );
  6718. }
  6719. }
  6720. }
  6721. computeVertexNormals() {
  6722. const index = this.index;
  6723. const positionAttribute = this.getAttribute( 'position' );
  6724. if ( positionAttribute !== undefined ) {
  6725. let normalAttribute = this.getAttribute( 'normal' );
  6726. if ( normalAttribute === undefined ) {
  6727. normalAttribute = new BufferAttribute( new Float32Array( positionAttribute.count * 3 ), 3 );
  6728. this.setAttribute( 'normal', normalAttribute );
  6729. } else {
  6730. // reset existing normals to zero
  6731. for ( let i = 0, il = normalAttribute.count; i < il; i ++ ) {
  6732. normalAttribute.setXYZ( i, 0, 0, 0 );
  6733. }
  6734. }
  6735. const pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
  6736. const nA = new Vector3(), nB = new Vector3(), nC = new Vector3();
  6737. const cb = new Vector3(), ab = new Vector3();
  6738. // indexed elements
  6739. if ( index ) {
  6740. for ( let i = 0, il = index.count; i < il; i += 3 ) {
  6741. const vA = index.getX( i + 0 );
  6742. const vB = index.getX( i + 1 );
  6743. const vC = index.getX( i + 2 );
  6744. pA.fromBufferAttribute( positionAttribute, vA );
  6745. pB.fromBufferAttribute( positionAttribute, vB );
  6746. pC.fromBufferAttribute( positionAttribute, vC );
  6747. cb.subVectors( pC, pB );
  6748. ab.subVectors( pA, pB );
  6749. cb.cross( ab );
  6750. nA.fromBufferAttribute( normalAttribute, vA );
  6751. nB.fromBufferAttribute( normalAttribute, vB );
  6752. nC.fromBufferAttribute( normalAttribute, vC );
  6753. nA.add( cb );
  6754. nB.add( cb );
  6755. nC.add( cb );
  6756. normalAttribute.setXYZ( vA, nA.x, nA.y, nA.z );
  6757. normalAttribute.setXYZ( vB, nB.x, nB.y, nB.z );
  6758. normalAttribute.setXYZ( vC, nC.x, nC.y, nC.z );
  6759. }
  6760. } else {
  6761. // non-indexed elements (unconnected triangle soup)
  6762. for ( let i = 0, il = positionAttribute.count; i < il; i += 3 ) {
  6763. pA.fromBufferAttribute( positionAttribute, i + 0 );
  6764. pB.fromBufferAttribute( positionAttribute, i + 1 );
  6765. pC.fromBufferAttribute( positionAttribute, i + 2 );
  6766. cb.subVectors( pC, pB );
  6767. ab.subVectors( pA, pB );
  6768. cb.cross( ab );
  6769. normalAttribute.setXYZ( i + 0, cb.x, cb.y, cb.z );
  6770. normalAttribute.setXYZ( i + 1, cb.x, cb.y, cb.z );
  6771. normalAttribute.setXYZ( i + 2, cb.x, cb.y, cb.z );
  6772. }
  6773. }
  6774. this.normalizeNormals();
  6775. normalAttribute.needsUpdate = true;
  6776. }
  6777. }
  6778. normalizeNormals() {
  6779. const normals = this.attributes.normal;
  6780. for ( let i = 0, il = normals.count; i < il; i ++ ) {
  6781. _vector$8.fromBufferAttribute( normals, i );
  6782. _vector$8.normalize();
  6783. normals.setXYZ( i, _vector$8.x, _vector$8.y, _vector$8.z );
  6784. }
  6785. }
  6786. toNonIndexed() {
  6787. function convertBufferAttribute( attribute, indices ) {
  6788. const array = attribute.array;
  6789. const itemSize = attribute.itemSize;
  6790. const normalized = attribute.normalized;
  6791. const array2 = new array.constructor( indices.length * itemSize );
  6792. let index = 0, index2 = 0;
  6793. for ( let i = 0, l = indices.length; i < l; i ++ ) {
  6794. if ( attribute.isInterleavedBufferAttribute ) {
  6795. index = indices[ i ] * attribute.data.stride + attribute.offset;
  6796. } else {
  6797. index = indices[ i ] * itemSize;
  6798. }
  6799. for ( let j = 0; j < itemSize; j ++ ) {
  6800. array2[ index2 ++ ] = array[ index ++ ];
  6801. }
  6802. }
  6803. return new BufferAttribute( array2, itemSize, normalized );
  6804. }
  6805. //
  6806. if ( this.index === null ) {
  6807. console.warn( 'THREE.BufferGeometry.toNonIndexed(): BufferGeometry is already non-indexed.' );
  6808. return this;
  6809. }
  6810. const geometry2 = new BufferGeometry();
  6811. const indices = this.index.array;
  6812. const attributes = this.attributes;
  6813. // attributes
  6814. for ( const name in attributes ) {
  6815. const attribute = attributes[ name ];
  6816. const newAttribute = convertBufferAttribute( attribute, indices );
  6817. geometry2.setAttribute( name, newAttribute );
  6818. }
  6819. // morph attributes
  6820. const morphAttributes = this.morphAttributes;
  6821. for ( const name in morphAttributes ) {
  6822. const morphArray = [];
  6823. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  6824. for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) {
  6825. const attribute = morphAttribute[ i ];
  6826. const newAttribute = convertBufferAttribute( attribute, indices );
  6827. morphArray.push( newAttribute );
  6828. }
  6829. geometry2.morphAttributes[ name ] = morphArray;
  6830. }
  6831. geometry2.morphTargetsRelative = this.morphTargetsRelative;
  6832. // groups
  6833. const groups = this.groups;
  6834. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  6835. const group = groups[ i ];
  6836. geometry2.addGroup( group.start, group.count, group.materialIndex );
  6837. }
  6838. return geometry2;
  6839. }
  6840. toJSON() {
  6841. const data = {
  6842. metadata: {
  6843. version: 4.6,
  6844. type: 'BufferGeometry',
  6845. generator: 'BufferGeometry.toJSON'
  6846. }
  6847. };
  6848. // standard BufferGeometry serialization
  6849. data.uuid = this.uuid;
  6850. data.type = this.type;
  6851. if ( this.name !== '' ) data.name = this.name;
  6852. if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;
  6853. if ( this.parameters !== undefined ) {
  6854. const parameters = this.parameters;
  6855. for ( const key in parameters ) {
  6856. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  6857. }
  6858. return data;
  6859. }
  6860. // for simplicity the code assumes attributes are not shared across geometries, see #15811
  6861. data.data = { attributes: {} };
  6862. const index = this.index;
  6863. if ( index !== null ) {
  6864. data.data.index = {
  6865. type: index.array.constructor.name,
  6866. array: Array.prototype.slice.call( index.array )
  6867. };
  6868. }
  6869. const attributes = this.attributes;
  6870. for ( const key in attributes ) {
  6871. const attribute = attributes[ key ];
  6872. data.data.attributes[ key ] = attribute.toJSON( data.data );
  6873. }
  6874. const morphAttributes = {};
  6875. let hasMorphAttributes = false;
  6876. for ( const key in this.morphAttributes ) {
  6877. const attributeArray = this.morphAttributes[ key ];
  6878. const array = [];
  6879. for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
  6880. const attribute = attributeArray[ i ];
  6881. array.push( attribute.toJSON( data.data ) );
  6882. }
  6883. if ( array.length > 0 ) {
  6884. morphAttributes[ key ] = array;
  6885. hasMorphAttributes = true;
  6886. }
  6887. }
  6888. if ( hasMorphAttributes ) {
  6889. data.data.morphAttributes = morphAttributes;
  6890. data.data.morphTargetsRelative = this.morphTargetsRelative;
  6891. }
  6892. const groups = this.groups;
  6893. if ( groups.length > 0 ) {
  6894. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  6895. }
  6896. const boundingSphere = this.boundingSphere;
  6897. if ( boundingSphere !== null ) {
  6898. data.data.boundingSphere = {
  6899. center: boundingSphere.center.toArray(),
  6900. radius: boundingSphere.radius
  6901. };
  6902. }
  6903. return data;
  6904. }
  6905. clone() {
  6906. return new this.constructor().copy( this );
  6907. }
  6908. copy( source ) {
  6909. // reset
  6910. this.index = null;
  6911. this.attributes = {};
  6912. this.morphAttributes = {};
  6913. this.groups = [];
  6914. this.boundingBox = null;
  6915. this.boundingSphere = null;
  6916. // used for storing cloned, shared data
  6917. const data = {};
  6918. // name
  6919. this.name = source.name;
  6920. // index
  6921. const index = source.index;
  6922. if ( index !== null ) {
  6923. this.setIndex( index.clone( data ) );
  6924. }
  6925. // attributes
  6926. const attributes = source.attributes;
  6927. for ( const name in attributes ) {
  6928. const attribute = attributes[ name ];
  6929. this.setAttribute( name, attribute.clone( data ) );
  6930. }
  6931. // morph attributes
  6932. const morphAttributes = source.morphAttributes;
  6933. for ( const name in morphAttributes ) {
  6934. const array = [];
  6935. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  6936. for ( let i = 0, l = morphAttribute.length; i < l; i ++ ) {
  6937. array.push( morphAttribute[ i ].clone( data ) );
  6938. }
  6939. this.morphAttributes[ name ] = array;
  6940. }
  6941. this.morphTargetsRelative = source.morphTargetsRelative;
  6942. // groups
  6943. const groups = source.groups;
  6944. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  6945. const group = groups[ i ];
  6946. this.addGroup( group.start, group.count, group.materialIndex );
  6947. }
  6948. // bounding box
  6949. const boundingBox = source.boundingBox;
  6950. if ( boundingBox !== null ) {
  6951. this.boundingBox = boundingBox.clone();
  6952. }
  6953. // bounding sphere
  6954. const boundingSphere = source.boundingSphere;
  6955. if ( boundingSphere !== null ) {
  6956. this.boundingSphere = boundingSphere.clone();
  6957. }
  6958. // draw range
  6959. this.drawRange.start = source.drawRange.start;
  6960. this.drawRange.count = source.drawRange.count;
  6961. // user data
  6962. this.userData = source.userData;
  6963. return this;
  6964. }
  6965. dispose() {
  6966. this.dispatchEvent( { type: 'dispose' } );
  6967. }
  6968. }
  6969. const _inverseMatrix$3 = /*@__PURE__*/ new Matrix4();
  6970. const _ray$3 = /*@__PURE__*/ new Ray();
  6971. const _sphere$6 = /*@__PURE__*/ new Sphere();
  6972. const _sphereHitAt = /*@__PURE__*/ new Vector3();
  6973. const _vA$1 = /*@__PURE__*/ new Vector3();
  6974. const _vB$1 = /*@__PURE__*/ new Vector3();
  6975. const _vC$1 = /*@__PURE__*/ new Vector3();
  6976. const _tempA = /*@__PURE__*/ new Vector3();
  6977. const _morphA = /*@__PURE__*/ new Vector3();
  6978. const _intersectionPoint = /*@__PURE__*/ new Vector3();
  6979. const _intersectionPointWorld = /*@__PURE__*/ new Vector3();
  6980. class Mesh extends Object3D {
  6981. constructor( geometry = new BufferGeometry(), material = new MeshBasicMaterial() ) {
  6982. super();
  6983. this.isMesh = true;
  6984. this.type = 'Mesh';
  6985. this.geometry = geometry;
  6986. this.material = material;
  6987. this.updateMorphTargets();
  6988. }
  6989. copy( source, recursive ) {
  6990. super.copy( source, recursive );
  6991. if ( source.morphTargetInfluences !== undefined ) {
  6992. this.morphTargetInfluences = source.morphTargetInfluences.slice();
  6993. }
  6994. if ( source.morphTargetDictionary !== undefined ) {
  6995. this.morphTargetDictionary = Object.assign( {}, source.morphTargetDictionary );
  6996. }
  6997. this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
  6998. this.geometry = source.geometry;
  6999. return this;
  7000. }
  7001. updateMorphTargets() {
  7002. const geometry = this.geometry;
  7003. const morphAttributes = geometry.morphAttributes;
  7004. const keys = Object.keys( morphAttributes );
  7005. if ( keys.length > 0 ) {
  7006. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  7007. if ( morphAttribute !== undefined ) {
  7008. this.morphTargetInfluences = [];
  7009. this.morphTargetDictionary = {};
  7010. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  7011. const name = morphAttribute[ m ].name || String( m );
  7012. this.morphTargetInfluences.push( 0 );
  7013. this.morphTargetDictionary[ name ] = m;
  7014. }
  7015. }
  7016. }
  7017. }
  7018. getVertexPosition( index, target ) {
  7019. const geometry = this.geometry;
  7020. const position = geometry.attributes.position;
  7021. const morphPosition = geometry.morphAttributes.position;
  7022. const morphTargetsRelative = geometry.morphTargetsRelative;
  7023. target.fromBufferAttribute( position, index );
  7024. const morphInfluences = this.morphTargetInfluences;
  7025. if ( morphPosition && morphInfluences ) {
  7026. _morphA.set( 0, 0, 0 );
  7027. for ( let i = 0, il = morphPosition.length; i < il; i ++ ) {
  7028. const influence = morphInfluences[ i ];
  7029. const morphAttribute = morphPosition[ i ];
  7030. if ( influence === 0 ) continue;
  7031. _tempA.fromBufferAttribute( morphAttribute, index );
  7032. if ( morphTargetsRelative ) {
  7033. _morphA.addScaledVector( _tempA, influence );
  7034. } else {
  7035. _morphA.addScaledVector( _tempA.sub( target ), influence );
  7036. }
  7037. }
  7038. target.add( _morphA );
  7039. }
  7040. return target;
  7041. }
  7042. raycast( raycaster, intersects ) {
  7043. const geometry = this.geometry;
  7044. const material = this.material;
  7045. const matrixWorld = this.matrixWorld;
  7046. if ( material === undefined ) return;
  7047. // test with bounding sphere in world space
  7048. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  7049. _sphere$6.copy( geometry.boundingSphere );
  7050. _sphere$6.applyMatrix4( matrixWorld );
  7051. // check distance from ray origin to bounding sphere
  7052. _ray$3.copy( raycaster.ray ).recast( raycaster.near );
  7053. if ( _sphere$6.containsPoint( _ray$3.origin ) === false ) {
  7054. if ( _ray$3.intersectSphere( _sphere$6, _sphereHitAt ) === null ) return;
  7055. if ( _ray$3.origin.distanceToSquared( _sphereHitAt ) > ( raycaster.far - raycaster.near ) ** 2 ) return;
  7056. }
  7057. // convert ray to local space of mesh
  7058. _inverseMatrix$3.copy( matrixWorld ).invert();
  7059. _ray$3.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$3 );
  7060. // test with bounding box in local space
  7061. if ( geometry.boundingBox !== null ) {
  7062. if ( _ray$3.intersectsBox( geometry.boundingBox ) === false ) return;
  7063. }
  7064. // test for intersections with geometry
  7065. this._computeIntersections( raycaster, intersects, _ray$3 );
  7066. }
  7067. _computeIntersections( raycaster, intersects, rayLocalSpace ) {
  7068. let intersection;
  7069. const geometry = this.geometry;
  7070. const material = this.material;
  7071. const index = geometry.index;
  7072. const position = geometry.attributes.position;
  7073. const uv = geometry.attributes.uv;
  7074. const uv1 = geometry.attributes.uv1;
  7075. const normal = geometry.attributes.normal;
  7076. const groups = geometry.groups;
  7077. const drawRange = geometry.drawRange;
  7078. if ( index !== null ) {
  7079. // indexed buffer geometry
  7080. if ( Array.isArray( material ) ) {
  7081. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  7082. const group = groups[ i ];
  7083. const groupMaterial = material[ group.materialIndex ];
  7084. const start = Math.max( group.start, drawRange.start );
  7085. const end = Math.min( index.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) );
  7086. for ( let j = start, jl = end; j < jl; j += 3 ) {
  7087. const a = index.getX( j );
  7088. const b = index.getX( j + 1 );
  7089. const c = index.getX( j + 2 );
  7090. intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  7091. if ( intersection ) {
  7092. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
  7093. intersection.face.materialIndex = group.materialIndex;
  7094. intersects.push( intersection );
  7095. }
  7096. }
  7097. }
  7098. } else {
  7099. const start = Math.max( 0, drawRange.start );
  7100. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  7101. for ( let i = start, il = end; i < il; i += 3 ) {
  7102. const a = index.getX( i );
  7103. const b = index.getX( i + 1 );
  7104. const c = index.getX( i + 2 );
  7105. intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  7106. if ( intersection ) {
  7107. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
  7108. intersects.push( intersection );
  7109. }
  7110. }
  7111. }
  7112. } else if ( position !== undefined ) {
  7113. // non-indexed buffer geometry
  7114. if ( Array.isArray( material ) ) {
  7115. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  7116. const group = groups[ i ];
  7117. const groupMaterial = material[ group.materialIndex ];
  7118. const start = Math.max( group.start, drawRange.start );
  7119. const end = Math.min( position.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) );
  7120. for ( let j = start, jl = end; j < jl; j += 3 ) {
  7121. const a = j;
  7122. const b = j + 1;
  7123. const c = j + 2;
  7124. intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  7125. if ( intersection ) {
  7126. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
  7127. intersection.face.materialIndex = group.materialIndex;
  7128. intersects.push( intersection );
  7129. }
  7130. }
  7131. }
  7132. } else {
  7133. const start = Math.max( 0, drawRange.start );
  7134. const end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
  7135. for ( let i = start, il = end; i < il; i += 3 ) {
  7136. const a = i;
  7137. const b = i + 1;
  7138. const c = i + 2;
  7139. intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  7140. if ( intersection ) {
  7141. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
  7142. intersects.push( intersection );
  7143. }
  7144. }
  7145. }
  7146. }
  7147. }
  7148. }
  7149. function checkIntersection$1( object, material, raycaster, ray, pA, pB, pC, point ) {
  7150. let intersect;
  7151. if ( material.side === BackSide ) {
  7152. intersect = ray.intersectTriangle( pC, pB, pA, true, point );
  7153. } else {
  7154. intersect = ray.intersectTriangle( pA, pB, pC, ( material.side === FrontSide ), point );
  7155. }
  7156. if ( intersect === null ) return null;
  7157. _intersectionPointWorld.copy( point );
  7158. _intersectionPointWorld.applyMatrix4( object.matrixWorld );
  7159. const distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld );
  7160. if ( distance < raycaster.near || distance > raycaster.far ) return null;
  7161. return {
  7162. distance: distance,
  7163. point: _intersectionPointWorld.clone(),
  7164. object: object
  7165. };
  7166. }
  7167. function checkGeometryIntersection( object, material, raycaster, ray, uv, uv1, normal, a, b, c ) {
  7168. object.getVertexPosition( a, _vA$1 );
  7169. object.getVertexPosition( b, _vB$1 );
  7170. object.getVertexPosition( c, _vC$1 );
  7171. const intersection = checkIntersection$1( object, material, raycaster, ray, _vA$1, _vB$1, _vC$1, _intersectionPoint );
  7172. if ( intersection ) {
  7173. const barycoord = new Vector3();
  7174. Triangle.getBarycoord( _intersectionPoint, _vA$1, _vB$1, _vC$1, barycoord );
  7175. if ( uv ) {
  7176. intersection.uv = Triangle.getInterpolatedAttribute( uv, a, b, c, barycoord, new Vector2() );
  7177. }
  7178. if ( uv1 ) {
  7179. intersection.uv1 = Triangle.getInterpolatedAttribute( uv1, a, b, c, barycoord, new Vector2() );
  7180. }
  7181. if ( normal ) {
  7182. intersection.normal = Triangle.getInterpolatedAttribute( normal, a, b, c, barycoord, new Vector3() );
  7183. if ( intersection.normal.dot( ray.direction ) > 0 ) {
  7184. intersection.normal.multiplyScalar( - 1 );
  7185. }
  7186. }
  7187. const face = {
  7188. a: a,
  7189. b: b,
  7190. c: c,
  7191. normal: new Vector3(),
  7192. materialIndex: 0
  7193. };
  7194. Triangle.getNormal( _vA$1, _vB$1, _vC$1, face.normal );
  7195. intersection.face = face;
  7196. intersection.barycoord = barycoord;
  7197. }
  7198. return intersection;
  7199. }
  7200. class BoxGeometry extends BufferGeometry {
  7201. constructor( width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1 ) {
  7202. super();
  7203. this.type = 'BoxGeometry';
  7204. this.parameters = {
  7205. width: width,
  7206. height: height,
  7207. depth: depth,
  7208. widthSegments: widthSegments,
  7209. heightSegments: heightSegments,
  7210. depthSegments: depthSegments
  7211. };
  7212. const scope = this;
  7213. // segments
  7214. widthSegments = Math.floor( widthSegments );
  7215. heightSegments = Math.floor( heightSegments );
  7216. depthSegments = Math.floor( depthSegments );
  7217. // buffers
  7218. const indices = [];
  7219. const vertices = [];
  7220. const normals = [];
  7221. const uvs = [];
  7222. // helper variables
  7223. let numberOfVertices = 0;
  7224. let groupStart = 0;
  7225. // build each side of the box geometry
  7226. buildPlane( 'z', 'y', 'x', - 1, - 1, depth, height, width, depthSegments, heightSegments, 0 ); // px
  7227. buildPlane( 'z', 'y', 'x', 1, - 1, depth, height, - width, depthSegments, heightSegments, 1 ); // nx
  7228. buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height, widthSegments, depthSegments, 2 ); // py
  7229. buildPlane( 'x', 'z', 'y', 1, - 1, width, depth, - height, widthSegments, depthSegments, 3 ); // ny
  7230. buildPlane( 'x', 'y', 'z', 1, - 1, width, height, depth, widthSegments, heightSegments, 4 ); // pz
  7231. buildPlane( 'x', 'y', 'z', - 1, - 1, width, height, - depth, widthSegments, heightSegments, 5 ); // nz
  7232. // build geometry
  7233. this.setIndex( indices );
  7234. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  7235. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  7236. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  7237. function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) {
  7238. const segmentWidth = width / gridX;
  7239. const segmentHeight = height / gridY;
  7240. const widthHalf = width / 2;
  7241. const heightHalf = height / 2;
  7242. const depthHalf = depth / 2;
  7243. const gridX1 = gridX + 1;
  7244. const gridY1 = gridY + 1;
  7245. let vertexCounter = 0;
  7246. let groupCount = 0;
  7247. const vector = new Vector3();
  7248. // generate vertices, normals and uvs
  7249. for ( let iy = 0; iy < gridY1; iy ++ ) {
  7250. const y = iy * segmentHeight - heightHalf;
  7251. for ( let ix = 0; ix < gridX1; ix ++ ) {
  7252. const x = ix * segmentWidth - widthHalf;
  7253. // set values to correct vector component
  7254. vector[ u ] = x * udir;
  7255. vector[ v ] = y * vdir;
  7256. vector[ w ] = depthHalf;
  7257. // now apply vector to vertex buffer
  7258. vertices.push( vector.x, vector.y, vector.z );
  7259. // set values to correct vector component
  7260. vector[ u ] = 0;
  7261. vector[ v ] = 0;
  7262. vector[ w ] = depth > 0 ? 1 : - 1;
  7263. // now apply vector to normal buffer
  7264. normals.push( vector.x, vector.y, vector.z );
  7265. // uvs
  7266. uvs.push( ix / gridX );
  7267. uvs.push( 1 - ( iy / gridY ) );
  7268. // counters
  7269. vertexCounter += 1;
  7270. }
  7271. }
  7272. // indices
  7273. // 1. you need three indices to draw a single face
  7274. // 2. a single segment consists of two faces
  7275. // 3. so we need to generate six (2*3) indices per segment
  7276. for ( let iy = 0; iy < gridY; iy ++ ) {
  7277. for ( let ix = 0; ix < gridX; ix ++ ) {
  7278. const a = numberOfVertices + ix + gridX1 * iy;
  7279. const b = numberOfVertices + ix + gridX1 * ( iy + 1 );
  7280. const c = numberOfVertices + ( ix + 1 ) + gridX1 * ( iy + 1 );
  7281. const d = numberOfVertices + ( ix + 1 ) + gridX1 * iy;
  7282. // faces
  7283. indices.push( a, b, d );
  7284. indices.push( b, c, d );
  7285. // increase counter
  7286. groupCount += 6;
  7287. }
  7288. }
  7289. // add a group to the geometry. this will ensure multi material support
  7290. scope.addGroup( groupStart, groupCount, materialIndex );
  7291. // calculate new start value for groups
  7292. groupStart += groupCount;
  7293. // update total number of vertices
  7294. numberOfVertices += vertexCounter;
  7295. }
  7296. }
  7297. copy( source ) {
  7298. super.copy( source );
  7299. this.parameters = Object.assign( {}, source.parameters );
  7300. return this;
  7301. }
  7302. static fromJSON( data ) {
  7303. return new BoxGeometry( data.width, data.height, data.depth, data.widthSegments, data.heightSegments, data.depthSegments );
  7304. }
  7305. }
  7306. /**
  7307. * Uniform Utilities
  7308. */
  7309. function cloneUniforms( src ) {
  7310. const dst = {};
  7311. for ( const u in src ) {
  7312. dst[ u ] = {};
  7313. for ( const p in src[ u ] ) {
  7314. const property = src[ u ][ p ];
  7315. if ( property && ( property.isColor ||
  7316. property.isMatrix3 || property.isMatrix4 ||
  7317. property.isVector2 || property.isVector3 || property.isVector4 ||
  7318. property.isTexture || property.isQuaternion ) ) {
  7319. if ( property.isRenderTargetTexture ) {
  7320. console.warn( 'UniformsUtils: Textures of render targets cannot be cloned via cloneUniforms() or mergeUniforms().' );
  7321. dst[ u ][ p ] = null;
  7322. } else {
  7323. dst[ u ][ p ] = property.clone();
  7324. }
  7325. } else if ( Array.isArray( property ) ) {
  7326. dst[ u ][ p ] = property.slice();
  7327. } else {
  7328. dst[ u ][ p ] = property;
  7329. }
  7330. }
  7331. }
  7332. return dst;
  7333. }
  7334. function mergeUniforms( uniforms ) {
  7335. const merged = {};
  7336. for ( let u = 0; u < uniforms.length; u ++ ) {
  7337. const tmp = cloneUniforms( uniforms[ u ] );
  7338. for ( const p in tmp ) {
  7339. merged[ p ] = tmp[ p ];
  7340. }
  7341. }
  7342. return merged;
  7343. }
  7344. function cloneUniformsGroups( src ) {
  7345. const dst = [];
  7346. for ( let u = 0; u < src.length; u ++ ) {
  7347. dst.push( src[ u ].clone() );
  7348. }
  7349. return dst;
  7350. }
  7351. function getUnlitUniformColorSpace( renderer ) {
  7352. const currentRenderTarget = renderer.getRenderTarget();
  7353. if ( currentRenderTarget === null ) {
  7354. // https://github.com/mrdoob/three.js/pull/23937#issuecomment-1111067398
  7355. return renderer.outputColorSpace;
  7356. }
  7357. // https://github.com/mrdoob/three.js/issues/27868
  7358. if ( currentRenderTarget.isXRRenderTarget === true ) {
  7359. return currentRenderTarget.texture.colorSpace;
  7360. }
  7361. return ColorManagement.workingColorSpace;
  7362. }
  7363. // Legacy
  7364. const UniformsUtils = { clone: cloneUniforms, merge: mergeUniforms };
  7365. var default_vertex = "void main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}";
  7366. var default_fragment = "void main() {\n\tgl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n}";
  7367. class ShaderMaterial extends Material {
  7368. constructor( parameters ) {
  7369. super();
  7370. this.isShaderMaterial = true;
  7371. this.type = 'ShaderMaterial';
  7372. this.defines = {};
  7373. this.uniforms = {};
  7374. this.uniformsGroups = [];
  7375. this.vertexShader = default_vertex;
  7376. this.fragmentShader = default_fragment;
  7377. this.linewidth = 1;
  7378. this.wireframe = false;
  7379. this.wireframeLinewidth = 1;
  7380. this.fog = false; // set to use scene fog
  7381. this.lights = false; // set to use scene lights
  7382. this.clipping = false; // set to use user-defined clipping planes
  7383. this.forceSinglePass = true;
  7384. this.extensions = {
  7385. clipCullDistance: false, // set to use vertex shader clipping
  7386. multiDraw: false // set to use vertex shader multi_draw / enable gl_DrawID
  7387. };
  7388. // When rendered geometry doesn't include these attributes but the material does,
  7389. // use these default values in WebGL. This avoids errors when buffer data is missing.
  7390. this.defaultAttributeValues = {
  7391. 'color': [ 1, 1, 1 ],
  7392. 'uv': [ 0, 0 ],
  7393. 'uv1': [ 0, 0 ]
  7394. };
  7395. this.index0AttributeName = undefined;
  7396. this.uniformsNeedUpdate = false;
  7397. this.glslVersion = null;
  7398. if ( parameters !== undefined ) {
  7399. this.setValues( parameters );
  7400. }
  7401. }
  7402. copy( source ) {
  7403. super.copy( source );
  7404. this.fragmentShader = source.fragmentShader;
  7405. this.vertexShader = source.vertexShader;
  7406. this.uniforms = cloneUniforms( source.uniforms );
  7407. this.uniformsGroups = cloneUniformsGroups( source.uniformsGroups );
  7408. this.defines = Object.assign( {}, source.defines );
  7409. this.wireframe = source.wireframe;
  7410. this.wireframeLinewidth = source.wireframeLinewidth;
  7411. this.fog = source.fog;
  7412. this.lights = source.lights;
  7413. this.clipping = source.clipping;
  7414. this.extensions = Object.assign( {}, source.extensions );
  7415. this.glslVersion = source.glslVersion;
  7416. return this;
  7417. }
  7418. toJSON( meta ) {
  7419. const data = super.toJSON( meta );
  7420. data.glslVersion = this.glslVersion;
  7421. data.uniforms = {};
  7422. for ( const name in this.uniforms ) {
  7423. const uniform = this.uniforms[ name ];
  7424. const value = uniform.value;
  7425. if ( value && value.isTexture ) {
  7426. data.uniforms[ name ] = {
  7427. type: 't',
  7428. value: value.toJSON( meta ).uuid
  7429. };
  7430. } else if ( value && value.isColor ) {
  7431. data.uniforms[ name ] = {
  7432. type: 'c',
  7433. value: value.getHex()
  7434. };
  7435. } else if ( value && value.isVector2 ) {
  7436. data.uniforms[ name ] = {
  7437. type: 'v2',
  7438. value: value.toArray()
  7439. };
  7440. } else if ( value && value.isVector3 ) {
  7441. data.uniforms[ name ] = {
  7442. type: 'v3',
  7443. value: value.toArray()
  7444. };
  7445. } else if ( value && value.isVector4 ) {
  7446. data.uniforms[ name ] = {
  7447. type: 'v4',
  7448. value: value.toArray()
  7449. };
  7450. } else if ( value && value.isMatrix3 ) {
  7451. data.uniforms[ name ] = {
  7452. type: 'm3',
  7453. value: value.toArray()
  7454. };
  7455. } else if ( value && value.isMatrix4 ) {
  7456. data.uniforms[ name ] = {
  7457. type: 'm4',
  7458. value: value.toArray()
  7459. };
  7460. } else {
  7461. data.uniforms[ name ] = {
  7462. value: value
  7463. };
  7464. // note: the array variants v2v, v3v, v4v, m4v and tv are not supported so far
  7465. }
  7466. }
  7467. if ( Object.keys( this.defines ).length > 0 ) data.defines = this.defines;
  7468. data.vertexShader = this.vertexShader;
  7469. data.fragmentShader = this.fragmentShader;
  7470. data.lights = this.lights;
  7471. data.clipping = this.clipping;
  7472. const extensions = {};
  7473. for ( const key in this.extensions ) {
  7474. if ( this.extensions[ key ] === true ) extensions[ key ] = true;
  7475. }
  7476. if ( Object.keys( extensions ).length > 0 ) data.extensions = extensions;
  7477. return data;
  7478. }
  7479. }
  7480. class Camera extends Object3D {
  7481. constructor() {
  7482. super();
  7483. this.isCamera = true;
  7484. this.type = 'Camera';
  7485. this.matrixWorldInverse = new Matrix4();
  7486. this.projectionMatrix = new Matrix4();
  7487. this.projectionMatrixInverse = new Matrix4();
  7488. this.coordinateSystem = WebGLCoordinateSystem;
  7489. }
  7490. copy( source, recursive ) {
  7491. super.copy( source, recursive );
  7492. this.matrixWorldInverse.copy( source.matrixWorldInverse );
  7493. this.projectionMatrix.copy( source.projectionMatrix );
  7494. this.projectionMatrixInverse.copy( source.projectionMatrixInverse );
  7495. this.coordinateSystem = source.coordinateSystem;
  7496. return this;
  7497. }
  7498. getWorldDirection( target ) {
  7499. return super.getWorldDirection( target ).negate();
  7500. }
  7501. updateMatrixWorld( force ) {
  7502. super.updateMatrixWorld( force );
  7503. this.matrixWorldInverse.copy( this.matrixWorld ).invert();
  7504. }
  7505. updateWorldMatrix( updateParents, updateChildren ) {
  7506. super.updateWorldMatrix( updateParents, updateChildren );
  7507. this.matrixWorldInverse.copy( this.matrixWorld ).invert();
  7508. }
  7509. clone() {
  7510. return new this.constructor().copy( this );
  7511. }
  7512. }
  7513. const _v3$1 = /*@__PURE__*/ new Vector3();
  7514. const _minTarget = /*@__PURE__*/ new Vector2();
  7515. const _maxTarget = /*@__PURE__*/ new Vector2();
  7516. class PerspectiveCamera extends Camera {
  7517. constructor( fov = 50, aspect = 1, near = 0.1, far = 2000 ) {
  7518. super();
  7519. this.isPerspectiveCamera = true;
  7520. this.type = 'PerspectiveCamera';
  7521. this.fov = fov;
  7522. this.zoom = 1;
  7523. this.near = near;
  7524. this.far = far;
  7525. this.focus = 10;
  7526. this.aspect = aspect;
  7527. this.view = null;
  7528. this.filmGauge = 35; // width of the film (default in millimeters)
  7529. this.filmOffset = 0; // horizontal film offset (same unit as gauge)
  7530. this.updateProjectionMatrix();
  7531. }
  7532. copy( source, recursive ) {
  7533. super.copy( source, recursive );
  7534. this.fov = source.fov;
  7535. this.zoom = source.zoom;
  7536. this.near = source.near;
  7537. this.far = source.far;
  7538. this.focus = source.focus;
  7539. this.aspect = source.aspect;
  7540. this.view = source.view === null ? null : Object.assign( {}, source.view );
  7541. this.filmGauge = source.filmGauge;
  7542. this.filmOffset = source.filmOffset;
  7543. return this;
  7544. }
  7545. /**
  7546. * Sets the FOV by focal length in respect to the current .filmGauge.
  7547. *
  7548. * The default film gauge is 35, so that the focal length can be specified for
  7549. * a 35mm (full frame) camera.
  7550. *
  7551. * @param {number} focalLength - Values for focal length and film gauge must have the same unit.
  7552. */
  7553. setFocalLength( focalLength ) {
  7554. /** see {@link http://www.bobatkins.com/photography/technical/field_of_view.html} */
  7555. const vExtentSlope = 0.5 * this.getFilmHeight() / focalLength;
  7556. this.fov = RAD2DEG * 2 * Math.atan( vExtentSlope );
  7557. this.updateProjectionMatrix();
  7558. }
  7559. /**
  7560. * Calculates the focal length from the current .fov and .filmGauge.
  7561. *
  7562. * @returns {number}
  7563. */
  7564. getFocalLength() {
  7565. const vExtentSlope = Math.tan( DEG2RAD * 0.5 * this.fov );
  7566. return 0.5 * this.getFilmHeight() / vExtentSlope;
  7567. }
  7568. getEffectiveFOV() {
  7569. return RAD2DEG * 2 * Math.atan(
  7570. Math.tan( DEG2RAD * 0.5 * this.fov ) / this.zoom );
  7571. }
  7572. getFilmWidth() {
  7573. // film not completely covered in portrait format (aspect < 1)
  7574. return this.filmGauge * Math.min( this.aspect, 1 );
  7575. }
  7576. getFilmHeight() {
  7577. // film not completely covered in landscape format (aspect > 1)
  7578. return this.filmGauge / Math.max( this.aspect, 1 );
  7579. }
  7580. /**
  7581. * Computes the 2D bounds of the camera's viewable rectangle at a given distance along the viewing direction.
  7582. * Sets minTarget and maxTarget to the coordinates of the lower-left and upper-right corners of the view rectangle.
  7583. *
  7584. * @param {number} distance
  7585. * @param {Vector2} minTarget
  7586. * @param {Vector2} maxTarget
  7587. */
  7588. getViewBounds( distance, minTarget, maxTarget ) {
  7589. _v3$1.set( - 1, - 1, 0.5 ).applyMatrix4( this.projectionMatrixInverse );
  7590. minTarget.set( _v3$1.x, _v3$1.y ).multiplyScalar( - distance / _v3$1.z );
  7591. _v3$1.set( 1, 1, 0.5 ).applyMatrix4( this.projectionMatrixInverse );
  7592. maxTarget.set( _v3$1.x, _v3$1.y ).multiplyScalar( - distance / _v3$1.z );
  7593. }
  7594. /**
  7595. * Computes the width and height of the camera's viewable rectangle at a given distance along the viewing direction.
  7596. *
  7597. * @param {number} distance
  7598. * @param {Vector2} target - Vector2 target used to store result where x is width and y is height.
  7599. * @returns {Vector2}
  7600. */
  7601. getViewSize( distance, target ) {
  7602. this.getViewBounds( distance, _minTarget, _maxTarget );
  7603. return target.subVectors( _maxTarget, _minTarget );
  7604. }
  7605. /**
  7606. * Sets an offset in a larger frustum. This is useful for multi-window or
  7607. * multi-monitor/multi-machine setups.
  7608. *
  7609. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  7610. * the monitors are in grid like this
  7611. *
  7612. * +---+---+---+
  7613. * | A | B | C |
  7614. * +---+---+---+
  7615. * | D | E | F |
  7616. * +---+---+---+
  7617. *
  7618. * then for each monitor you would call it like this
  7619. *
  7620. * const w = 1920;
  7621. * const h = 1080;
  7622. * const fullWidth = w * 3;
  7623. * const fullHeight = h * 2;
  7624. *
  7625. * --A--
  7626. * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  7627. * --B--
  7628. * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  7629. * --C--
  7630. * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  7631. * --D--
  7632. * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  7633. * --E--
  7634. * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  7635. * --F--
  7636. * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  7637. *
  7638. * Note there is no reason monitors have to be the same size or in a grid.
  7639. *
  7640. * @param {number} fullWidth
  7641. * @param {number} fullHeight
  7642. * @param {number} x
  7643. * @param {number} y
  7644. * @param {number} width
  7645. * @param {number} height
  7646. */
  7647. setViewOffset( fullWidth, fullHeight, x, y, width, height ) {
  7648. this.aspect = fullWidth / fullHeight;
  7649. if ( this.view === null ) {
  7650. this.view = {
  7651. enabled: true,
  7652. fullWidth: 1,
  7653. fullHeight: 1,
  7654. offsetX: 0,
  7655. offsetY: 0,
  7656. width: 1,
  7657. height: 1
  7658. };
  7659. }
  7660. this.view.enabled = true;
  7661. this.view.fullWidth = fullWidth;
  7662. this.view.fullHeight = fullHeight;
  7663. this.view.offsetX = x;
  7664. this.view.offsetY = y;
  7665. this.view.width = width;
  7666. this.view.height = height;
  7667. this.updateProjectionMatrix();
  7668. }
  7669. clearViewOffset() {
  7670. if ( this.view !== null ) {
  7671. this.view.enabled = false;
  7672. }
  7673. this.updateProjectionMatrix();
  7674. }
  7675. updateProjectionMatrix() {
  7676. const near = this.near;
  7677. let top = near * Math.tan( DEG2RAD * 0.5 * this.fov ) / this.zoom;
  7678. let height = 2 * top;
  7679. let width = this.aspect * height;
  7680. let left = - 0.5 * width;
  7681. const view = this.view;
  7682. if ( this.view !== null && this.view.enabled ) {
  7683. const fullWidth = view.fullWidth,
  7684. fullHeight = view.fullHeight;
  7685. left += view.offsetX * width / fullWidth;
  7686. top -= view.offsetY * height / fullHeight;
  7687. width *= view.width / fullWidth;
  7688. height *= view.height / fullHeight;
  7689. }
  7690. const skew = this.filmOffset;
  7691. if ( skew !== 0 ) left += near * skew / this.getFilmWidth();
  7692. this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far, this.coordinateSystem );
  7693. this.projectionMatrixInverse.copy( this.projectionMatrix ).invert();
  7694. }
  7695. toJSON( meta ) {
  7696. const data = super.toJSON( meta );
  7697. data.object.fov = this.fov;
  7698. data.object.zoom = this.zoom;
  7699. data.object.near = this.near;
  7700. data.object.far = this.far;
  7701. data.object.focus = this.focus;
  7702. data.object.aspect = this.aspect;
  7703. if ( this.view !== null ) data.object.view = Object.assign( {}, this.view );
  7704. data.object.filmGauge = this.filmGauge;
  7705. data.object.filmOffset = this.filmOffset;
  7706. return data;
  7707. }
  7708. }
  7709. const fov = - 90; // negative fov is not an error
  7710. const aspect = 1;
  7711. class CubeCamera extends Object3D {
  7712. constructor( near, far, renderTarget ) {
  7713. super();
  7714. this.type = 'CubeCamera';
  7715. this.renderTarget = renderTarget;
  7716. this.coordinateSystem = null;
  7717. this.activeMipmapLevel = 0;
  7718. const cameraPX = new PerspectiveCamera( fov, aspect, near, far );
  7719. cameraPX.layers = this.layers;
  7720. this.add( cameraPX );
  7721. const cameraNX = new PerspectiveCamera( fov, aspect, near, far );
  7722. cameraNX.layers = this.layers;
  7723. this.add( cameraNX );
  7724. const cameraPY = new PerspectiveCamera( fov, aspect, near, far );
  7725. cameraPY.layers = this.layers;
  7726. this.add( cameraPY );
  7727. const cameraNY = new PerspectiveCamera( fov, aspect, near, far );
  7728. cameraNY.layers = this.layers;
  7729. this.add( cameraNY );
  7730. const cameraPZ = new PerspectiveCamera( fov, aspect, near, far );
  7731. cameraPZ.layers = this.layers;
  7732. this.add( cameraPZ );
  7733. const cameraNZ = new PerspectiveCamera( fov, aspect, near, far );
  7734. cameraNZ.layers = this.layers;
  7735. this.add( cameraNZ );
  7736. }
  7737. updateCoordinateSystem() {
  7738. const coordinateSystem = this.coordinateSystem;
  7739. const cameras = this.children.concat();
  7740. const [ cameraPX, cameraNX, cameraPY, cameraNY, cameraPZ, cameraNZ ] = cameras;
  7741. for ( const camera of cameras ) this.remove( camera );
  7742. if ( coordinateSystem === WebGLCoordinateSystem ) {
  7743. cameraPX.up.set( 0, 1, 0 );
  7744. cameraPX.lookAt( 1, 0, 0 );
  7745. cameraNX.up.set( 0, 1, 0 );
  7746. cameraNX.lookAt( - 1, 0, 0 );
  7747. cameraPY.up.set( 0, 0, - 1 );
  7748. cameraPY.lookAt( 0, 1, 0 );
  7749. cameraNY.up.set( 0, 0, 1 );
  7750. cameraNY.lookAt( 0, - 1, 0 );
  7751. cameraPZ.up.set( 0, 1, 0 );
  7752. cameraPZ.lookAt( 0, 0, 1 );
  7753. cameraNZ.up.set( 0, 1, 0 );
  7754. cameraNZ.lookAt( 0, 0, - 1 );
  7755. } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
  7756. cameraPX.up.set( 0, - 1, 0 );
  7757. cameraPX.lookAt( - 1, 0, 0 );
  7758. cameraNX.up.set( 0, - 1, 0 );
  7759. cameraNX.lookAt( 1, 0, 0 );
  7760. cameraPY.up.set( 0, 0, 1 );
  7761. cameraPY.lookAt( 0, 1, 0 );
  7762. cameraNY.up.set( 0, 0, - 1 );
  7763. cameraNY.lookAt( 0, - 1, 0 );
  7764. cameraPZ.up.set( 0, - 1, 0 );
  7765. cameraPZ.lookAt( 0, 0, 1 );
  7766. cameraNZ.up.set( 0, - 1, 0 );
  7767. cameraNZ.lookAt( 0, 0, - 1 );
  7768. } else {
  7769. throw new Error( 'THREE.CubeCamera.updateCoordinateSystem(): Invalid coordinate system: ' + coordinateSystem );
  7770. }
  7771. for ( const camera of cameras ) {
  7772. this.add( camera );
  7773. camera.updateMatrixWorld();
  7774. }
  7775. }
  7776. update( renderer, scene ) {
  7777. if ( this.parent === null ) this.updateMatrixWorld();
  7778. const { renderTarget, activeMipmapLevel } = this;
  7779. if ( this.coordinateSystem !== renderer.coordinateSystem ) {
  7780. this.coordinateSystem = renderer.coordinateSystem;
  7781. this.updateCoordinateSystem();
  7782. }
  7783. const [ cameraPX, cameraNX, cameraPY, cameraNY, cameraPZ, cameraNZ ] = this.children;
  7784. const currentRenderTarget = renderer.getRenderTarget();
  7785. const currentActiveCubeFace = renderer.getActiveCubeFace();
  7786. const currentActiveMipmapLevel = renderer.getActiveMipmapLevel();
  7787. const currentXrEnabled = renderer.xr.enabled;
  7788. renderer.xr.enabled = false;
  7789. const generateMipmaps = renderTarget.texture.generateMipmaps;
  7790. renderTarget.texture.generateMipmaps = false;
  7791. renderer.setRenderTarget( renderTarget, 0, activeMipmapLevel );
  7792. renderer.render( scene, cameraPX );
  7793. renderer.setRenderTarget( renderTarget, 1, activeMipmapLevel );
  7794. renderer.render( scene, cameraNX );
  7795. renderer.setRenderTarget( renderTarget, 2, activeMipmapLevel );
  7796. renderer.render( scene, cameraPY );
  7797. renderer.setRenderTarget( renderTarget, 3, activeMipmapLevel );
  7798. renderer.render( scene, cameraNY );
  7799. renderer.setRenderTarget( renderTarget, 4, activeMipmapLevel );
  7800. renderer.render( scene, cameraPZ );
  7801. // mipmaps are generated during the last call of render()
  7802. // at this point, all sides of the cube render target are defined
  7803. renderTarget.texture.generateMipmaps = generateMipmaps;
  7804. renderer.setRenderTarget( renderTarget, 5, activeMipmapLevel );
  7805. renderer.render( scene, cameraNZ );
  7806. renderer.setRenderTarget( currentRenderTarget, currentActiveCubeFace, currentActiveMipmapLevel );
  7807. renderer.xr.enabled = currentXrEnabled;
  7808. renderTarget.texture.needsPMREMUpdate = true;
  7809. }
  7810. }
  7811. class CubeTexture extends Texture {
  7812. constructor( images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace ) {
  7813. images = images !== undefined ? images : [];
  7814. mapping = mapping !== undefined ? mapping : CubeReflectionMapping;
  7815. super( images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace );
  7816. this.isCubeTexture = true;
  7817. this.flipY = false;
  7818. }
  7819. get images() {
  7820. return this.image;
  7821. }
  7822. set images( value ) {
  7823. this.image = value;
  7824. }
  7825. }
  7826. class WebGLCubeRenderTarget extends WebGLRenderTarget {
  7827. constructor( size = 1, options = {} ) {
  7828. super( size, size, options );
  7829. this.isWebGLCubeRenderTarget = true;
  7830. const image = { width: size, height: size, depth: 1 };
  7831. const images = [ image, image, image, image, image, image ];
  7832. this.texture = new CubeTexture( images, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace );
  7833. // By convention -- likely based on the RenderMan spec from the 1990's -- cube maps are specified by WebGL (and three.js)
  7834. // in a coordinate system in which positive-x is to the right when looking up the positive-z axis -- in other words,
  7835. // in a left-handed coordinate system. By continuing this convention, preexisting cube maps continued to render correctly.
  7836. // three.js uses a right-handed coordinate system. So environment maps used in three.js appear to have px and nx swapped
  7837. // and the flag isRenderTargetTexture controls this conversion. The flip is not required when using WebGLCubeRenderTarget.texture
  7838. // as a cube texture (this is detected when isRenderTargetTexture is set to true for cube textures).
  7839. this.texture.isRenderTargetTexture = true;
  7840. this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false;
  7841. this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter;
  7842. }
  7843. fromEquirectangularTexture( renderer, texture ) {
  7844. this.texture.type = texture.type;
  7845. this.texture.colorSpace = texture.colorSpace;
  7846. this.texture.generateMipmaps = texture.generateMipmaps;
  7847. this.texture.minFilter = texture.minFilter;
  7848. this.texture.magFilter = texture.magFilter;
  7849. const shader = {
  7850. uniforms: {
  7851. tEquirect: { value: null },
  7852. },
  7853. vertexShader: /* glsl */`
  7854. varying vec3 vWorldDirection;
  7855. vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
  7856. return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
  7857. }
  7858. void main() {
  7859. vWorldDirection = transformDirection( position, modelMatrix );
  7860. #include <begin_vertex>
  7861. #include <project_vertex>
  7862. }
  7863. `,
  7864. fragmentShader: /* glsl */`
  7865. uniform sampler2D tEquirect;
  7866. varying vec3 vWorldDirection;
  7867. #include <common>
  7868. void main() {
  7869. vec3 direction = normalize( vWorldDirection );
  7870. vec2 sampleUV = equirectUv( direction );
  7871. gl_FragColor = texture2D( tEquirect, sampleUV );
  7872. }
  7873. `
  7874. };
  7875. const geometry = new BoxGeometry( 5, 5, 5 );
  7876. const material = new ShaderMaterial( {
  7877. name: 'CubemapFromEquirect',
  7878. uniforms: cloneUniforms( shader.uniforms ),
  7879. vertexShader: shader.vertexShader,
  7880. fragmentShader: shader.fragmentShader,
  7881. side: BackSide,
  7882. blending: NoBlending
  7883. } );
  7884. material.uniforms.tEquirect.value = texture;
  7885. const mesh = new Mesh( geometry, material );
  7886. const currentMinFilter = texture.minFilter;
  7887. // Avoid blurred poles
  7888. if ( texture.minFilter === LinearMipmapLinearFilter ) texture.minFilter = LinearFilter;
  7889. const camera = new CubeCamera( 1, 10, this );
  7890. camera.update( renderer, mesh );
  7891. texture.minFilter = currentMinFilter;
  7892. mesh.geometry.dispose();
  7893. mesh.material.dispose();
  7894. return this;
  7895. }
  7896. clear( renderer, color, depth, stencil ) {
  7897. const currentRenderTarget = renderer.getRenderTarget();
  7898. for ( let i = 0; i < 6; i ++ ) {
  7899. renderer.setRenderTarget( this, i );
  7900. renderer.clear( color, depth, stencil );
  7901. }
  7902. renderer.setRenderTarget( currentRenderTarget );
  7903. }
  7904. }
  7905. class FogExp2 {
  7906. constructor( color, density = 0.00025 ) {
  7907. this.isFogExp2 = true;
  7908. this.name = '';
  7909. this.color = new Color( color );
  7910. this.density = density;
  7911. }
  7912. clone() {
  7913. return new FogExp2( this.color, this.density );
  7914. }
  7915. toJSON( /* meta */ ) {
  7916. return {
  7917. type: 'FogExp2',
  7918. name: this.name,
  7919. color: this.color.getHex(),
  7920. density: this.density
  7921. };
  7922. }
  7923. }
  7924. class Fog {
  7925. constructor( color, near = 1, far = 1000 ) {
  7926. this.isFog = true;
  7927. this.name = '';
  7928. this.color = new Color( color );
  7929. this.near = near;
  7930. this.far = far;
  7931. }
  7932. clone() {
  7933. return new Fog( this.color, this.near, this.far );
  7934. }
  7935. toJSON( /* meta */ ) {
  7936. return {
  7937. type: 'Fog',
  7938. name: this.name,
  7939. color: this.color.getHex(),
  7940. near: this.near,
  7941. far: this.far
  7942. };
  7943. }
  7944. }
  7945. class Scene extends Object3D {
  7946. constructor() {
  7947. super();
  7948. this.isScene = true;
  7949. this.type = 'Scene';
  7950. this.background = null;
  7951. this.environment = null;
  7952. this.fog = null;
  7953. this.backgroundBlurriness = 0;
  7954. this.backgroundIntensity = 1;
  7955. this.backgroundRotation = new Euler();
  7956. this.environmentIntensity = 1;
  7957. this.environmentRotation = new Euler();
  7958. this.overrideMaterial = null;
  7959. if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
  7960. __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) );
  7961. }
  7962. }
  7963. copy( source, recursive ) {
  7964. super.copy( source, recursive );
  7965. if ( source.background !== null ) this.background = source.background.clone();
  7966. if ( source.environment !== null ) this.environment = source.environment.clone();
  7967. if ( source.fog !== null ) this.fog = source.fog.clone();
  7968. this.backgroundBlurriness = source.backgroundBlurriness;
  7969. this.backgroundIntensity = source.backgroundIntensity;
  7970. this.backgroundRotation.copy( source.backgroundRotation );
  7971. this.environmentIntensity = source.environmentIntensity;
  7972. this.environmentRotation.copy( source.environmentRotation );
  7973. if ( source.overrideMaterial !== null ) this.overrideMaterial = source.overrideMaterial.clone();
  7974. this.matrixAutoUpdate = source.matrixAutoUpdate;
  7975. return this;
  7976. }
  7977. toJSON( meta ) {
  7978. const data = super.toJSON( meta );
  7979. if ( this.fog !== null ) data.object.fog = this.fog.toJSON();
  7980. if ( this.backgroundBlurriness > 0 ) data.object.backgroundBlurriness = this.backgroundBlurriness;
  7981. if ( this.backgroundIntensity !== 1 ) data.object.backgroundIntensity = this.backgroundIntensity;
  7982. data.object.backgroundRotation = this.backgroundRotation.toArray();
  7983. if ( this.environmentIntensity !== 1 ) data.object.environmentIntensity = this.environmentIntensity;
  7984. data.object.environmentRotation = this.environmentRotation.toArray();
  7985. return data;
  7986. }
  7987. }
  7988. class InterleavedBuffer {
  7989. constructor( array, stride ) {
  7990. this.isInterleavedBuffer = true;
  7991. this.array = array;
  7992. this.stride = stride;
  7993. this.count = array !== undefined ? array.length / stride : 0;
  7994. this.usage = StaticDrawUsage;
  7995. this.updateRanges = [];
  7996. this.version = 0;
  7997. this.uuid = generateUUID();
  7998. }
  7999. onUploadCallback() {}
  8000. set needsUpdate( value ) {
  8001. if ( value === true ) this.version ++;
  8002. }
  8003. setUsage( value ) {
  8004. this.usage = value;
  8005. return this;
  8006. }
  8007. addUpdateRange( start, count ) {
  8008. this.updateRanges.push( { start, count } );
  8009. }
  8010. clearUpdateRanges() {
  8011. this.updateRanges.length = 0;
  8012. }
  8013. copy( source ) {
  8014. this.array = new source.array.constructor( source.array );
  8015. this.count = source.count;
  8016. this.stride = source.stride;
  8017. this.usage = source.usage;
  8018. return this;
  8019. }
  8020. copyAt( index1, attribute, index2 ) {
  8021. index1 *= this.stride;
  8022. index2 *= attribute.stride;
  8023. for ( let i = 0, l = this.stride; i < l; i ++ ) {
  8024. this.array[ index1 + i ] = attribute.array[ index2 + i ];
  8025. }
  8026. return this;
  8027. }
  8028. set( value, offset = 0 ) {
  8029. this.array.set( value, offset );
  8030. return this;
  8031. }
  8032. clone( data ) {
  8033. if ( data.arrayBuffers === undefined ) {
  8034. data.arrayBuffers = {};
  8035. }
  8036. if ( this.array.buffer._uuid === undefined ) {
  8037. this.array.buffer._uuid = generateUUID();
  8038. }
  8039. if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) {
  8040. data.arrayBuffers[ this.array.buffer._uuid ] = this.array.slice( 0 ).buffer;
  8041. }
  8042. const array = new this.array.constructor( data.arrayBuffers[ this.array.buffer._uuid ] );
  8043. const ib = new this.constructor( array, this.stride );
  8044. ib.setUsage( this.usage );
  8045. return ib;
  8046. }
  8047. onUpload( callback ) {
  8048. this.onUploadCallback = callback;
  8049. return this;
  8050. }
  8051. toJSON( data ) {
  8052. if ( data.arrayBuffers === undefined ) {
  8053. data.arrayBuffers = {};
  8054. }
  8055. // generate UUID for array buffer if necessary
  8056. if ( this.array.buffer._uuid === undefined ) {
  8057. this.array.buffer._uuid = generateUUID();
  8058. }
  8059. if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) {
  8060. data.arrayBuffers[ this.array.buffer._uuid ] = Array.from( new Uint32Array( this.array.buffer ) );
  8061. }
  8062. //
  8063. return {
  8064. uuid: this.uuid,
  8065. buffer: this.array.buffer._uuid,
  8066. type: this.array.constructor.name,
  8067. stride: this.stride
  8068. };
  8069. }
  8070. }
  8071. const _vector$7 = /*@__PURE__*/ new Vector3();
  8072. class InterleavedBufferAttribute {
  8073. constructor( interleavedBuffer, itemSize, offset, normalized = false ) {
  8074. this.isInterleavedBufferAttribute = true;
  8075. this.name = '';
  8076. this.data = interleavedBuffer;
  8077. this.itemSize = itemSize;
  8078. this.offset = offset;
  8079. this.normalized = normalized;
  8080. }
  8081. get count() {
  8082. return this.data.count;
  8083. }
  8084. get array() {
  8085. return this.data.array;
  8086. }
  8087. set needsUpdate( value ) {
  8088. this.data.needsUpdate = value;
  8089. }
  8090. applyMatrix4( m ) {
  8091. for ( let i = 0, l = this.data.count; i < l; i ++ ) {
  8092. _vector$7.fromBufferAttribute( this, i );
  8093. _vector$7.applyMatrix4( m );
  8094. this.setXYZ( i, _vector$7.x, _vector$7.y, _vector$7.z );
  8095. }
  8096. return this;
  8097. }
  8098. applyNormalMatrix( m ) {
  8099. for ( let i = 0, l = this.count; i < l; i ++ ) {
  8100. _vector$7.fromBufferAttribute( this, i );
  8101. _vector$7.applyNormalMatrix( m );
  8102. this.setXYZ( i, _vector$7.x, _vector$7.y, _vector$7.z );
  8103. }
  8104. return this;
  8105. }
  8106. transformDirection( m ) {
  8107. for ( let i = 0, l = this.count; i < l; i ++ ) {
  8108. _vector$7.fromBufferAttribute( this, i );
  8109. _vector$7.transformDirection( m );
  8110. this.setXYZ( i, _vector$7.x, _vector$7.y, _vector$7.z );
  8111. }
  8112. return this;
  8113. }
  8114. getComponent( index, component ) {
  8115. let value = this.array[ index * this.data.stride + this.offset + component ];
  8116. if ( this.normalized ) value = denormalize( value, this.array );
  8117. return value;
  8118. }
  8119. setComponent( index, component, value ) {
  8120. if ( this.normalized ) value = normalize( value, this.array );
  8121. this.data.array[ index * this.data.stride + this.offset + component ] = value;
  8122. return this;
  8123. }
  8124. setX( index, x ) {
  8125. if ( this.normalized ) x = normalize( x, this.array );
  8126. this.data.array[ index * this.data.stride + this.offset ] = x;
  8127. return this;
  8128. }
  8129. setY( index, y ) {
  8130. if ( this.normalized ) y = normalize( y, this.array );
  8131. this.data.array[ index * this.data.stride + this.offset + 1 ] = y;
  8132. return this;
  8133. }
  8134. setZ( index, z ) {
  8135. if ( this.normalized ) z = normalize( z, this.array );
  8136. this.data.array[ index * this.data.stride + this.offset + 2 ] = z;
  8137. return this;
  8138. }
  8139. setW( index, w ) {
  8140. if ( this.normalized ) w = normalize( w, this.array );
  8141. this.data.array[ index * this.data.stride + this.offset + 3 ] = w;
  8142. return this;
  8143. }
  8144. getX( index ) {
  8145. let x = this.data.array[ index * this.data.stride + this.offset ];
  8146. if ( this.normalized ) x = denormalize( x, this.array );
  8147. return x;
  8148. }
  8149. getY( index ) {
  8150. let y = this.data.array[ index * this.data.stride + this.offset + 1 ];
  8151. if ( this.normalized ) y = denormalize( y, this.array );
  8152. return y;
  8153. }
  8154. getZ( index ) {
  8155. let z = this.data.array[ index * this.data.stride + this.offset + 2 ];
  8156. if ( this.normalized ) z = denormalize( z, this.array );
  8157. return z;
  8158. }
  8159. getW( index ) {
  8160. let w = this.data.array[ index * this.data.stride + this.offset + 3 ];
  8161. if ( this.normalized ) w = denormalize( w, this.array );
  8162. return w;
  8163. }
  8164. setXY( index, x, y ) {
  8165. index = index * this.data.stride + this.offset;
  8166. if ( this.normalized ) {
  8167. x = normalize( x, this.array );
  8168. y = normalize( y, this.array );
  8169. }
  8170. this.data.array[ index + 0 ] = x;
  8171. this.data.array[ index + 1 ] = y;
  8172. return this;
  8173. }
  8174. setXYZ( index, x, y, z ) {
  8175. index = index * this.data.stride + this.offset;
  8176. if ( this.normalized ) {
  8177. x = normalize( x, this.array );
  8178. y = normalize( y, this.array );
  8179. z = normalize( z, this.array );
  8180. }
  8181. this.data.array[ index + 0 ] = x;
  8182. this.data.array[ index + 1 ] = y;
  8183. this.data.array[ index + 2 ] = z;
  8184. return this;
  8185. }
  8186. setXYZW( index, x, y, z, w ) {
  8187. index = index * this.data.stride + this.offset;
  8188. if ( this.normalized ) {
  8189. x = normalize( x, this.array );
  8190. y = normalize( y, this.array );
  8191. z = normalize( z, this.array );
  8192. w = normalize( w, this.array );
  8193. }
  8194. this.data.array[ index + 0 ] = x;
  8195. this.data.array[ index + 1 ] = y;
  8196. this.data.array[ index + 2 ] = z;
  8197. this.data.array[ index + 3 ] = w;
  8198. return this;
  8199. }
  8200. clone( data ) {
  8201. if ( data === undefined ) {
  8202. console.log( 'THREE.InterleavedBufferAttribute.clone(): Cloning an interleaved buffer attribute will de-interleave buffer data.' );
  8203. const array = [];
  8204. for ( let i = 0; i < this.count; i ++ ) {
  8205. const index = i * this.data.stride + this.offset;
  8206. for ( let j = 0; j < this.itemSize; j ++ ) {
  8207. array.push( this.data.array[ index + j ] );
  8208. }
  8209. }
  8210. return new BufferAttribute( new this.array.constructor( array ), this.itemSize, this.normalized );
  8211. } else {
  8212. if ( data.interleavedBuffers === undefined ) {
  8213. data.interleavedBuffers = {};
  8214. }
  8215. if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) {
  8216. data.interleavedBuffers[ this.data.uuid ] = this.data.clone( data );
  8217. }
  8218. return new InterleavedBufferAttribute( data.interleavedBuffers[ this.data.uuid ], this.itemSize, this.offset, this.normalized );
  8219. }
  8220. }
  8221. toJSON( data ) {
  8222. if ( data === undefined ) {
  8223. console.log( 'THREE.InterleavedBufferAttribute.toJSON(): Serializing an interleaved buffer attribute will de-interleave buffer data.' );
  8224. const array = [];
  8225. for ( let i = 0; i < this.count; i ++ ) {
  8226. const index = i * this.data.stride + this.offset;
  8227. for ( let j = 0; j < this.itemSize; j ++ ) {
  8228. array.push( this.data.array[ index + j ] );
  8229. }
  8230. }
  8231. // de-interleave data and save it as an ordinary buffer attribute for now
  8232. return {
  8233. itemSize: this.itemSize,
  8234. type: this.array.constructor.name,
  8235. array: array,
  8236. normalized: this.normalized
  8237. };
  8238. } else {
  8239. // save as true interleaved attribute
  8240. if ( data.interleavedBuffers === undefined ) {
  8241. data.interleavedBuffers = {};
  8242. }
  8243. if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) {
  8244. data.interleavedBuffers[ this.data.uuid ] = this.data.toJSON( data );
  8245. }
  8246. return {
  8247. isInterleavedBufferAttribute: true,
  8248. itemSize: this.itemSize,
  8249. data: this.data.uuid,
  8250. offset: this.offset,
  8251. normalized: this.normalized
  8252. };
  8253. }
  8254. }
  8255. }
  8256. class SpriteMaterial extends Material {
  8257. constructor( parameters ) {
  8258. super();
  8259. this.isSpriteMaterial = true;
  8260. this.type = 'SpriteMaterial';
  8261. this.color = new Color( 0xffffff );
  8262. this.map = null;
  8263. this.alphaMap = null;
  8264. this.rotation = 0;
  8265. this.sizeAttenuation = true;
  8266. this.transparent = true;
  8267. this.fog = true;
  8268. this.setValues( parameters );
  8269. }
  8270. copy( source ) {
  8271. super.copy( source );
  8272. this.color.copy( source.color );
  8273. this.map = source.map;
  8274. this.alphaMap = source.alphaMap;
  8275. this.rotation = source.rotation;
  8276. this.sizeAttenuation = source.sizeAttenuation;
  8277. this.fog = source.fog;
  8278. return this;
  8279. }
  8280. }
  8281. let _geometry;
  8282. const _intersectPoint = /*@__PURE__*/ new Vector3();
  8283. const _worldScale = /*@__PURE__*/ new Vector3();
  8284. const _mvPosition = /*@__PURE__*/ new Vector3();
  8285. const _alignedPosition = /*@__PURE__*/ new Vector2();
  8286. const _rotatedPosition = /*@__PURE__*/ new Vector2();
  8287. const _viewWorldMatrix = /*@__PURE__*/ new Matrix4();
  8288. const _vA = /*@__PURE__*/ new Vector3();
  8289. const _vB = /*@__PURE__*/ new Vector3();
  8290. const _vC = /*@__PURE__*/ new Vector3();
  8291. const _uvA = /*@__PURE__*/ new Vector2();
  8292. const _uvB = /*@__PURE__*/ new Vector2();
  8293. const _uvC = /*@__PURE__*/ new Vector2();
  8294. class Sprite extends Object3D {
  8295. constructor( material = new SpriteMaterial() ) {
  8296. super();
  8297. this.isSprite = true;
  8298. this.type = 'Sprite';
  8299. if ( _geometry === undefined ) {
  8300. _geometry = new BufferGeometry();
  8301. const float32Array = new Float32Array( [
  8302. - 0.5, - 0.5, 0, 0, 0,
  8303. 0.5, - 0.5, 0, 1, 0,
  8304. 0.5, 0.5, 0, 1, 1,
  8305. - 0.5, 0.5, 0, 0, 1
  8306. ] );
  8307. const interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
  8308. _geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] );
  8309. _geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
  8310. _geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
  8311. }
  8312. this.geometry = _geometry;
  8313. this.material = material;
  8314. this.center = new Vector2( 0.5, 0.5 );
  8315. }
  8316. raycast( raycaster, intersects ) {
  8317. if ( raycaster.camera === null ) {
  8318. console.error( 'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.' );
  8319. }
  8320. _worldScale.setFromMatrixScale( this.matrixWorld );
  8321. _viewWorldMatrix.copy( raycaster.camera.matrixWorld );
  8322. this.modelViewMatrix.multiplyMatrices( raycaster.camera.matrixWorldInverse, this.matrixWorld );
  8323. _mvPosition.setFromMatrixPosition( this.modelViewMatrix );
  8324. if ( raycaster.camera.isPerspectiveCamera && this.material.sizeAttenuation === false ) {
  8325. _worldScale.multiplyScalar( - _mvPosition.z );
  8326. }
  8327. const rotation = this.material.rotation;
  8328. let sin, cos;
  8329. if ( rotation !== 0 ) {
  8330. cos = Math.cos( rotation );
  8331. sin = Math.sin( rotation );
  8332. }
  8333. const center = this.center;
  8334. transformVertex( _vA.set( - 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
  8335. transformVertex( _vB.set( 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
  8336. transformVertex( _vC.set( 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
  8337. _uvA.set( 0, 0 );
  8338. _uvB.set( 1, 0 );
  8339. _uvC.set( 1, 1 );
  8340. // check first triangle
  8341. let intersect = raycaster.ray.intersectTriangle( _vA, _vB, _vC, false, _intersectPoint );
  8342. if ( intersect === null ) {
  8343. // check second triangle
  8344. transformVertex( _vB.set( - 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
  8345. _uvB.set( 0, 1 );
  8346. intersect = raycaster.ray.intersectTriangle( _vA, _vC, _vB, false, _intersectPoint );
  8347. if ( intersect === null ) {
  8348. return;
  8349. }
  8350. }
  8351. const distance = raycaster.ray.origin.distanceTo( _intersectPoint );
  8352. if ( distance < raycaster.near || distance > raycaster.far ) return;
  8353. intersects.push( {
  8354. distance: distance,
  8355. point: _intersectPoint.clone(),
  8356. uv: Triangle.getInterpolation( _intersectPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() ),
  8357. face: null,
  8358. object: this
  8359. } );
  8360. }
  8361. copy( source, recursive ) {
  8362. super.copy( source, recursive );
  8363. if ( source.center !== undefined ) this.center.copy( source.center );
  8364. this.material = source.material;
  8365. return this;
  8366. }
  8367. }
  8368. function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) {
  8369. // compute position in camera space
  8370. _alignedPosition.subVectors( vertexPosition, center ).addScalar( 0.5 ).multiply( scale );
  8371. // to check if rotation is not zero
  8372. if ( sin !== undefined ) {
  8373. _rotatedPosition.x = ( cos * _alignedPosition.x ) - ( sin * _alignedPosition.y );
  8374. _rotatedPosition.y = ( sin * _alignedPosition.x ) + ( cos * _alignedPosition.y );
  8375. } else {
  8376. _rotatedPosition.copy( _alignedPosition );
  8377. }
  8378. vertexPosition.copy( mvPosition );
  8379. vertexPosition.x += _rotatedPosition.x;
  8380. vertexPosition.y += _rotatedPosition.y;
  8381. // transform to world space
  8382. vertexPosition.applyMatrix4( _viewWorldMatrix );
  8383. }
  8384. const _v1$2 = /*@__PURE__*/ new Vector3();
  8385. const _v2$1 = /*@__PURE__*/ new Vector3();
  8386. class LOD extends Object3D {
  8387. constructor() {
  8388. super();
  8389. this._currentLevel = 0;
  8390. this.type = 'LOD';
  8391. Object.defineProperties( this, {
  8392. levels: {
  8393. enumerable: true,
  8394. value: []
  8395. },
  8396. isLOD: {
  8397. value: true,
  8398. }
  8399. } );
  8400. this.autoUpdate = true;
  8401. }
  8402. copy( source ) {
  8403. super.copy( source, false );
  8404. const levels = source.levels;
  8405. for ( let i = 0, l = levels.length; i < l; i ++ ) {
  8406. const level = levels[ i ];
  8407. this.addLevel( level.object.clone(), level.distance, level.hysteresis );
  8408. }
  8409. this.autoUpdate = source.autoUpdate;
  8410. return this;
  8411. }
  8412. addLevel( object, distance = 0, hysteresis = 0 ) {
  8413. distance = Math.abs( distance );
  8414. const levels = this.levels;
  8415. let l;
  8416. for ( l = 0; l < levels.length; l ++ ) {
  8417. if ( distance < levels[ l ].distance ) {
  8418. break;
  8419. }
  8420. }
  8421. levels.splice( l, 0, { distance: distance, hysteresis: hysteresis, object: object } );
  8422. this.add( object );
  8423. return this;
  8424. }
  8425. removeLevel( distance ) {
  8426. const levels = this.levels;
  8427. for ( let i = 0; i < levels.length; i ++ ) {
  8428. if ( levels[ i ].distance === distance ) {
  8429. const removedElements = levels.splice( i, 1 );
  8430. this.remove( removedElements[ 0 ].object );
  8431. return true;
  8432. }
  8433. }
  8434. return false;
  8435. }
  8436. getCurrentLevel() {
  8437. return this._currentLevel;
  8438. }
  8439. getObjectForDistance( distance ) {
  8440. const levels = this.levels;
  8441. if ( levels.length > 0 ) {
  8442. let i, l;
  8443. for ( i = 1, l = levels.length; i < l; i ++ ) {
  8444. let levelDistance = levels[ i ].distance;
  8445. if ( levels[ i ].object.visible ) {
  8446. levelDistance -= levelDistance * levels[ i ].hysteresis;
  8447. }
  8448. if ( distance < levelDistance ) {
  8449. break;
  8450. }
  8451. }
  8452. return levels[ i - 1 ].object;
  8453. }
  8454. return null;
  8455. }
  8456. raycast( raycaster, intersects ) {
  8457. const levels = this.levels;
  8458. if ( levels.length > 0 ) {
  8459. _v1$2.setFromMatrixPosition( this.matrixWorld );
  8460. const distance = raycaster.ray.origin.distanceTo( _v1$2 );
  8461. this.getObjectForDistance( distance ).raycast( raycaster, intersects );
  8462. }
  8463. }
  8464. update( camera ) {
  8465. const levels = this.levels;
  8466. if ( levels.length > 1 ) {
  8467. _v1$2.setFromMatrixPosition( camera.matrixWorld );
  8468. _v2$1.setFromMatrixPosition( this.matrixWorld );
  8469. const distance = _v1$2.distanceTo( _v2$1 ) / camera.zoom;
  8470. levels[ 0 ].object.visible = true;
  8471. let i, l;
  8472. for ( i = 1, l = levels.length; i < l; i ++ ) {
  8473. let levelDistance = levels[ i ].distance;
  8474. if ( levels[ i ].object.visible ) {
  8475. levelDistance -= levelDistance * levels[ i ].hysteresis;
  8476. }
  8477. if ( distance >= levelDistance ) {
  8478. levels[ i - 1 ].object.visible = false;
  8479. levels[ i ].object.visible = true;
  8480. } else {
  8481. break;
  8482. }
  8483. }
  8484. this._currentLevel = i - 1;
  8485. for ( ; i < l; i ++ ) {
  8486. levels[ i ].object.visible = false;
  8487. }
  8488. }
  8489. }
  8490. toJSON( meta ) {
  8491. const data = super.toJSON( meta );
  8492. if ( this.autoUpdate === false ) data.object.autoUpdate = false;
  8493. data.object.levels = [];
  8494. const levels = this.levels;
  8495. for ( let i = 0, l = levels.length; i < l; i ++ ) {
  8496. const level = levels[ i ];
  8497. data.object.levels.push( {
  8498. object: level.object.uuid,
  8499. distance: level.distance,
  8500. hysteresis: level.hysteresis
  8501. } );
  8502. }
  8503. return data;
  8504. }
  8505. }
  8506. const _basePosition = /*@__PURE__*/ new Vector3();
  8507. const _skinIndex = /*@__PURE__*/ new Vector4();
  8508. const _skinWeight = /*@__PURE__*/ new Vector4();
  8509. const _vector3 = /*@__PURE__*/ new Vector3();
  8510. const _matrix4 = /*@__PURE__*/ new Matrix4();
  8511. const _vertex = /*@__PURE__*/ new Vector3();
  8512. const _sphere$5 = /*@__PURE__*/ new Sphere();
  8513. const _inverseMatrix$2 = /*@__PURE__*/ new Matrix4();
  8514. const _ray$2 = /*@__PURE__*/ new Ray();
  8515. class SkinnedMesh extends Mesh {
  8516. constructor( geometry, material ) {
  8517. super( geometry, material );
  8518. this.isSkinnedMesh = true;
  8519. this.type = 'SkinnedMesh';
  8520. this.bindMode = AttachedBindMode;
  8521. this.bindMatrix = new Matrix4();
  8522. this.bindMatrixInverse = new Matrix4();
  8523. this.boundingBox = null;
  8524. this.boundingSphere = null;
  8525. }
  8526. computeBoundingBox() {
  8527. const geometry = this.geometry;
  8528. if ( this.boundingBox === null ) {
  8529. this.boundingBox = new Box3();
  8530. }
  8531. this.boundingBox.makeEmpty();
  8532. const positionAttribute = geometry.getAttribute( 'position' );
  8533. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  8534. this.getVertexPosition( i, _vertex );
  8535. this.boundingBox.expandByPoint( _vertex );
  8536. }
  8537. }
  8538. computeBoundingSphere() {
  8539. const geometry = this.geometry;
  8540. if ( this.boundingSphere === null ) {
  8541. this.boundingSphere = new Sphere();
  8542. }
  8543. this.boundingSphere.makeEmpty();
  8544. const positionAttribute = geometry.getAttribute( 'position' );
  8545. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  8546. this.getVertexPosition( i, _vertex );
  8547. this.boundingSphere.expandByPoint( _vertex );
  8548. }
  8549. }
  8550. copy( source, recursive ) {
  8551. super.copy( source, recursive );
  8552. this.bindMode = source.bindMode;
  8553. this.bindMatrix.copy( source.bindMatrix );
  8554. this.bindMatrixInverse.copy( source.bindMatrixInverse );
  8555. this.skeleton = source.skeleton;
  8556. if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone();
  8557. if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone();
  8558. return this;
  8559. }
  8560. raycast( raycaster, intersects ) {
  8561. const material = this.material;
  8562. const matrixWorld = this.matrixWorld;
  8563. if ( material === undefined ) return;
  8564. // test with bounding sphere in world space
  8565. if ( this.boundingSphere === null ) this.computeBoundingSphere();
  8566. _sphere$5.copy( this.boundingSphere );
  8567. _sphere$5.applyMatrix4( matrixWorld );
  8568. if ( raycaster.ray.intersectsSphere( _sphere$5 ) === false ) return;
  8569. // convert ray to local space of skinned mesh
  8570. _inverseMatrix$2.copy( matrixWorld ).invert();
  8571. _ray$2.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$2 );
  8572. // test with bounding box in local space
  8573. if ( this.boundingBox !== null ) {
  8574. if ( _ray$2.intersectsBox( this.boundingBox ) === false ) return;
  8575. }
  8576. // test for intersections with geometry
  8577. this._computeIntersections( raycaster, intersects, _ray$2 );
  8578. }
  8579. getVertexPosition( index, target ) {
  8580. super.getVertexPosition( index, target );
  8581. this.applyBoneTransform( index, target );
  8582. return target;
  8583. }
  8584. bind( skeleton, bindMatrix ) {
  8585. this.skeleton = skeleton;
  8586. if ( bindMatrix === undefined ) {
  8587. this.updateMatrixWorld( true );
  8588. this.skeleton.calculateInverses();
  8589. bindMatrix = this.matrixWorld;
  8590. }
  8591. this.bindMatrix.copy( bindMatrix );
  8592. this.bindMatrixInverse.copy( bindMatrix ).invert();
  8593. }
  8594. pose() {
  8595. this.skeleton.pose();
  8596. }
  8597. normalizeSkinWeights() {
  8598. const vector = new Vector4();
  8599. const skinWeight = this.geometry.attributes.skinWeight;
  8600. for ( let i = 0, l = skinWeight.count; i < l; i ++ ) {
  8601. vector.fromBufferAttribute( skinWeight, i );
  8602. const scale = 1.0 / vector.manhattanLength();
  8603. if ( scale !== Infinity ) {
  8604. vector.multiplyScalar( scale );
  8605. } else {
  8606. vector.set( 1, 0, 0, 0 ); // do something reasonable
  8607. }
  8608. skinWeight.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
  8609. }
  8610. }
  8611. updateMatrixWorld( force ) {
  8612. super.updateMatrixWorld( force );
  8613. if ( this.bindMode === AttachedBindMode ) {
  8614. this.bindMatrixInverse.copy( this.matrixWorld ).invert();
  8615. } else if ( this.bindMode === DetachedBindMode ) {
  8616. this.bindMatrixInverse.copy( this.bindMatrix ).invert();
  8617. } else {
  8618. console.warn( 'THREE.SkinnedMesh: Unrecognized bindMode: ' + this.bindMode );
  8619. }
  8620. }
  8621. applyBoneTransform( index, vector ) {
  8622. const skeleton = this.skeleton;
  8623. const geometry = this.geometry;
  8624. _skinIndex.fromBufferAttribute( geometry.attributes.skinIndex, index );
  8625. _skinWeight.fromBufferAttribute( geometry.attributes.skinWeight, index );
  8626. _basePosition.copy( vector ).applyMatrix4( this.bindMatrix );
  8627. vector.set( 0, 0, 0 );
  8628. for ( let i = 0; i < 4; i ++ ) {
  8629. const weight = _skinWeight.getComponent( i );
  8630. if ( weight !== 0 ) {
  8631. const boneIndex = _skinIndex.getComponent( i );
  8632. _matrix4.multiplyMatrices( skeleton.bones[ boneIndex ].matrixWorld, skeleton.boneInverses[ boneIndex ] );
  8633. vector.addScaledVector( _vector3.copy( _basePosition ).applyMatrix4( _matrix4 ), weight );
  8634. }
  8635. }
  8636. return vector.applyMatrix4( this.bindMatrixInverse );
  8637. }
  8638. }
  8639. class Bone extends Object3D {
  8640. constructor() {
  8641. super();
  8642. this.isBone = true;
  8643. this.type = 'Bone';
  8644. }
  8645. }
  8646. class DataTexture extends Texture {
  8647. constructor( data = null, width = 1, height = 1, format, type, mapping, wrapS, wrapT, magFilter = NearestFilter, minFilter = NearestFilter, anisotropy, colorSpace ) {
  8648. super( null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace );
  8649. this.isDataTexture = true;
  8650. this.image = { data: data, width: width, height: height };
  8651. this.generateMipmaps = false;
  8652. this.flipY = false;
  8653. this.unpackAlignment = 1;
  8654. }
  8655. }
  8656. const _offsetMatrix = /*@__PURE__*/ new Matrix4();
  8657. const _identityMatrix = /*@__PURE__*/ new Matrix4();
  8658. class Skeleton {
  8659. constructor( bones = [], boneInverses = [] ) {
  8660. this.uuid = generateUUID();
  8661. this.bones = bones.slice( 0 );
  8662. this.boneInverses = boneInverses;
  8663. this.boneMatrices = null;
  8664. this.boneTexture = null;
  8665. this.init();
  8666. }
  8667. init() {
  8668. const bones = this.bones;
  8669. const boneInverses = this.boneInverses;
  8670. this.boneMatrices = new Float32Array( bones.length * 16 );
  8671. // calculate inverse bone matrices if necessary
  8672. if ( boneInverses.length === 0 ) {
  8673. this.calculateInverses();
  8674. } else {
  8675. // handle special case
  8676. if ( bones.length !== boneInverses.length ) {
  8677. console.warn( 'THREE.Skeleton: Number of inverse bone matrices does not match amount of bones.' );
  8678. this.boneInverses = [];
  8679. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  8680. this.boneInverses.push( new Matrix4() );
  8681. }
  8682. }
  8683. }
  8684. }
  8685. calculateInverses() {
  8686. this.boneInverses.length = 0;
  8687. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  8688. const inverse = new Matrix4();
  8689. if ( this.bones[ i ] ) {
  8690. inverse.copy( this.bones[ i ].matrixWorld ).invert();
  8691. }
  8692. this.boneInverses.push( inverse );
  8693. }
  8694. }
  8695. pose() {
  8696. // recover the bind-time world matrices
  8697. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  8698. const bone = this.bones[ i ];
  8699. if ( bone ) {
  8700. bone.matrixWorld.copy( this.boneInverses[ i ] ).invert();
  8701. }
  8702. }
  8703. // compute the local matrices, positions, rotations and scales
  8704. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  8705. const bone = this.bones[ i ];
  8706. if ( bone ) {
  8707. if ( bone.parent && bone.parent.isBone ) {
  8708. bone.matrix.copy( bone.parent.matrixWorld ).invert();
  8709. bone.matrix.multiply( bone.matrixWorld );
  8710. } else {
  8711. bone.matrix.copy( bone.matrixWorld );
  8712. }
  8713. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  8714. }
  8715. }
  8716. }
  8717. update() {
  8718. const bones = this.bones;
  8719. const boneInverses = this.boneInverses;
  8720. const boneMatrices = this.boneMatrices;
  8721. const boneTexture = this.boneTexture;
  8722. // flatten bone matrices to array
  8723. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  8724. // compute the offset between the current and the original transform
  8725. const matrix = bones[ i ] ? bones[ i ].matrixWorld : _identityMatrix;
  8726. _offsetMatrix.multiplyMatrices( matrix, boneInverses[ i ] );
  8727. _offsetMatrix.toArray( boneMatrices, i * 16 );
  8728. }
  8729. if ( boneTexture !== null ) {
  8730. boneTexture.needsUpdate = true;
  8731. }
  8732. }
  8733. clone() {
  8734. return new Skeleton( this.bones, this.boneInverses );
  8735. }
  8736. computeBoneTexture() {
  8737. // layout (1 matrix = 4 pixels)
  8738. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  8739. // with 8x8 pixel texture max 16 bones * 4 pixels = (8 * 8)
  8740. // 16x16 pixel texture max 64 bones * 4 pixels = (16 * 16)
  8741. // 32x32 pixel texture max 256 bones * 4 pixels = (32 * 32)
  8742. // 64x64 pixel texture max 1024 bones * 4 pixels = (64 * 64)
  8743. let size = Math.sqrt( this.bones.length * 4 ); // 4 pixels needed for 1 matrix
  8744. size = Math.ceil( size / 4 ) * 4;
  8745. size = Math.max( size, 4 );
  8746. const boneMatrices = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
  8747. boneMatrices.set( this.boneMatrices ); // copy current values
  8748. const boneTexture = new DataTexture( boneMatrices, size, size, RGBAFormat, FloatType );
  8749. boneTexture.needsUpdate = true;
  8750. this.boneMatrices = boneMatrices;
  8751. this.boneTexture = boneTexture;
  8752. return this;
  8753. }
  8754. getBoneByName( name ) {
  8755. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  8756. const bone = this.bones[ i ];
  8757. if ( bone.name === name ) {
  8758. return bone;
  8759. }
  8760. }
  8761. return undefined;
  8762. }
  8763. dispose( ) {
  8764. if ( this.boneTexture !== null ) {
  8765. this.boneTexture.dispose();
  8766. this.boneTexture = null;
  8767. }
  8768. }
  8769. fromJSON( json, bones ) {
  8770. this.uuid = json.uuid;
  8771. for ( let i = 0, l = json.bones.length; i < l; i ++ ) {
  8772. const uuid = json.bones[ i ];
  8773. let bone = bones[ uuid ];
  8774. if ( bone === undefined ) {
  8775. console.warn( 'THREE.Skeleton: No bone found with UUID:', uuid );
  8776. bone = new Bone();
  8777. }
  8778. this.bones.push( bone );
  8779. this.boneInverses.push( new Matrix4().fromArray( json.boneInverses[ i ] ) );
  8780. }
  8781. this.init();
  8782. return this;
  8783. }
  8784. toJSON() {
  8785. const data = {
  8786. metadata: {
  8787. version: 4.6,
  8788. type: 'Skeleton',
  8789. generator: 'Skeleton.toJSON'
  8790. },
  8791. bones: [],
  8792. boneInverses: []
  8793. };
  8794. data.uuid = this.uuid;
  8795. const bones = this.bones;
  8796. const boneInverses = this.boneInverses;
  8797. for ( let i = 0, l = bones.length; i < l; i ++ ) {
  8798. const bone = bones[ i ];
  8799. data.bones.push( bone.uuid );
  8800. const boneInverse = boneInverses[ i ];
  8801. data.boneInverses.push( boneInverse.toArray() );
  8802. }
  8803. return data;
  8804. }
  8805. }
  8806. class InstancedBufferAttribute extends BufferAttribute {
  8807. constructor( array, itemSize, normalized, meshPerAttribute = 1 ) {
  8808. super( array, itemSize, normalized );
  8809. this.isInstancedBufferAttribute = true;
  8810. this.meshPerAttribute = meshPerAttribute;
  8811. }
  8812. copy( source ) {
  8813. super.copy( source );
  8814. this.meshPerAttribute = source.meshPerAttribute;
  8815. return this;
  8816. }
  8817. toJSON() {
  8818. const data = super.toJSON();
  8819. data.meshPerAttribute = this.meshPerAttribute;
  8820. data.isInstancedBufferAttribute = true;
  8821. return data;
  8822. }
  8823. }
  8824. const _instanceLocalMatrix = /*@__PURE__*/ new Matrix4();
  8825. const _instanceWorldMatrix = /*@__PURE__*/ new Matrix4();
  8826. const _instanceIntersects = [];
  8827. const _box3 = /*@__PURE__*/ new Box3();
  8828. const _identity = /*@__PURE__*/ new Matrix4();
  8829. const _mesh$1 = /*@__PURE__*/ new Mesh();
  8830. const _sphere$4 = /*@__PURE__*/ new Sphere();
  8831. class InstancedMesh extends Mesh {
  8832. constructor( geometry, material, count ) {
  8833. super( geometry, material );
  8834. this.isInstancedMesh = true;
  8835. this.instanceMatrix = new InstancedBufferAttribute( new Float32Array( count * 16 ), 16 );
  8836. this.instanceColor = null;
  8837. this.morphTexture = null;
  8838. this.count = count;
  8839. this.boundingBox = null;
  8840. this.boundingSphere = null;
  8841. for ( let i = 0; i < count; i ++ ) {
  8842. this.setMatrixAt( i, _identity );
  8843. }
  8844. }
  8845. computeBoundingBox() {
  8846. const geometry = this.geometry;
  8847. const count = this.count;
  8848. if ( this.boundingBox === null ) {
  8849. this.boundingBox = new Box3();
  8850. }
  8851. if ( geometry.boundingBox === null ) {
  8852. geometry.computeBoundingBox();
  8853. }
  8854. this.boundingBox.makeEmpty();
  8855. for ( let i = 0; i < count; i ++ ) {
  8856. this.getMatrixAt( i, _instanceLocalMatrix );
  8857. _box3.copy( geometry.boundingBox ).applyMatrix4( _instanceLocalMatrix );
  8858. this.boundingBox.union( _box3 );
  8859. }
  8860. }
  8861. computeBoundingSphere() {
  8862. const geometry = this.geometry;
  8863. const count = this.count;
  8864. if ( this.boundingSphere === null ) {
  8865. this.boundingSphere = new Sphere();
  8866. }
  8867. if ( geometry.boundingSphere === null ) {
  8868. geometry.computeBoundingSphere();
  8869. }
  8870. this.boundingSphere.makeEmpty();
  8871. for ( let i = 0; i < count; i ++ ) {
  8872. this.getMatrixAt( i, _instanceLocalMatrix );
  8873. _sphere$4.copy( geometry.boundingSphere ).applyMatrix4( _instanceLocalMatrix );
  8874. this.boundingSphere.union( _sphere$4 );
  8875. }
  8876. }
  8877. copy( source, recursive ) {
  8878. super.copy( source, recursive );
  8879. this.instanceMatrix.copy( source.instanceMatrix );
  8880. if ( source.morphTexture !== null ) this.morphTexture = source.morphTexture.clone();
  8881. if ( source.instanceColor !== null ) this.instanceColor = source.instanceColor.clone();
  8882. this.count = source.count;
  8883. if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone();
  8884. if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone();
  8885. return this;
  8886. }
  8887. getColorAt( index, color ) {
  8888. color.fromArray( this.instanceColor.array, index * 3 );
  8889. }
  8890. getMatrixAt( index, matrix ) {
  8891. matrix.fromArray( this.instanceMatrix.array, index * 16 );
  8892. }
  8893. getMorphAt( index, object ) {
  8894. const objectInfluences = object.morphTargetInfluences;
  8895. const array = this.morphTexture.source.data.data;
  8896. const len = objectInfluences.length + 1; // All influences + the baseInfluenceSum
  8897. const dataIndex = index * len + 1; // Skip the baseInfluenceSum at the beginning
  8898. for ( let i = 0; i < objectInfluences.length; i ++ ) {
  8899. objectInfluences[ i ] = array[ dataIndex + i ];
  8900. }
  8901. }
  8902. raycast( raycaster, intersects ) {
  8903. const matrixWorld = this.matrixWorld;
  8904. const raycastTimes = this.count;
  8905. _mesh$1.geometry = this.geometry;
  8906. _mesh$1.material = this.material;
  8907. if ( _mesh$1.material === undefined ) return;
  8908. // test with bounding sphere first
  8909. if ( this.boundingSphere === null ) this.computeBoundingSphere();
  8910. _sphere$4.copy( this.boundingSphere );
  8911. _sphere$4.applyMatrix4( matrixWorld );
  8912. if ( raycaster.ray.intersectsSphere( _sphere$4 ) === false ) return;
  8913. // now test each instance
  8914. for ( let instanceId = 0; instanceId < raycastTimes; instanceId ++ ) {
  8915. // calculate the world matrix for each instance
  8916. this.getMatrixAt( instanceId, _instanceLocalMatrix );
  8917. _instanceWorldMatrix.multiplyMatrices( matrixWorld, _instanceLocalMatrix );
  8918. // the mesh represents this single instance
  8919. _mesh$1.matrixWorld = _instanceWorldMatrix;
  8920. _mesh$1.raycast( raycaster, _instanceIntersects );
  8921. // process the result of raycast
  8922. for ( let i = 0, l = _instanceIntersects.length; i < l; i ++ ) {
  8923. const intersect = _instanceIntersects[ i ];
  8924. intersect.instanceId = instanceId;
  8925. intersect.object = this;
  8926. intersects.push( intersect );
  8927. }
  8928. _instanceIntersects.length = 0;
  8929. }
  8930. }
  8931. setColorAt( index, color ) {
  8932. if ( this.instanceColor === null ) {
  8933. this.instanceColor = new InstancedBufferAttribute( new Float32Array( this.instanceMatrix.count * 3 ).fill( 1 ), 3 );
  8934. }
  8935. color.toArray( this.instanceColor.array, index * 3 );
  8936. }
  8937. setMatrixAt( index, matrix ) {
  8938. matrix.toArray( this.instanceMatrix.array, index * 16 );
  8939. }
  8940. setMorphAt( index, object ) {
  8941. const objectInfluences = object.morphTargetInfluences;
  8942. const len = objectInfluences.length + 1; // morphBaseInfluence + all influences
  8943. if ( this.morphTexture === null ) {
  8944. this.morphTexture = new DataTexture( new Float32Array( len * this.count ), len, this.count, RedFormat, FloatType );
  8945. }
  8946. const array = this.morphTexture.source.data.data;
  8947. let morphInfluencesSum = 0;
  8948. for ( let i = 0; i < objectInfluences.length; i ++ ) {
  8949. morphInfluencesSum += objectInfluences[ i ];
  8950. }
  8951. const morphBaseInfluence = this.geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
  8952. const dataIndex = len * index;
  8953. array[ dataIndex ] = morphBaseInfluence;
  8954. array.set( objectInfluences, dataIndex + 1 );
  8955. }
  8956. updateMorphTargets() {
  8957. }
  8958. dispose() {
  8959. this.dispatchEvent( { type: 'dispose' } );
  8960. if ( this.morphTexture !== null ) {
  8961. this.morphTexture.dispose();
  8962. this.morphTexture = null;
  8963. }
  8964. return this;
  8965. }
  8966. }
  8967. const _vector1 = /*@__PURE__*/ new Vector3();
  8968. const _vector2 = /*@__PURE__*/ new Vector3();
  8969. const _normalMatrix = /*@__PURE__*/ new Matrix3();
  8970. class Plane {
  8971. constructor( normal = new Vector3( 1, 0, 0 ), constant = 0 ) {
  8972. this.isPlane = true;
  8973. // normal is assumed to be normalized
  8974. this.normal = normal;
  8975. this.constant = constant;
  8976. }
  8977. set( normal, constant ) {
  8978. this.normal.copy( normal );
  8979. this.constant = constant;
  8980. return this;
  8981. }
  8982. setComponents( x, y, z, w ) {
  8983. this.normal.set( x, y, z );
  8984. this.constant = w;
  8985. return this;
  8986. }
  8987. setFromNormalAndCoplanarPoint( normal, point ) {
  8988. this.normal.copy( normal );
  8989. this.constant = - point.dot( this.normal );
  8990. return this;
  8991. }
  8992. setFromCoplanarPoints( a, b, c ) {
  8993. const normal = _vector1.subVectors( c, b ).cross( _vector2.subVectors( a, b ) ).normalize();
  8994. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  8995. this.setFromNormalAndCoplanarPoint( normal, a );
  8996. return this;
  8997. }
  8998. copy( plane ) {
  8999. this.normal.copy( plane.normal );
  9000. this.constant = plane.constant;
  9001. return this;
  9002. }
  9003. normalize() {
  9004. // Note: will lead to a divide by zero if the plane is invalid.
  9005. const inverseNormalLength = 1.0 / this.normal.length();
  9006. this.normal.multiplyScalar( inverseNormalLength );
  9007. this.constant *= inverseNormalLength;
  9008. return this;
  9009. }
  9010. negate() {
  9011. this.constant *= - 1;
  9012. this.normal.negate();
  9013. return this;
  9014. }
  9015. distanceToPoint( point ) {
  9016. return this.normal.dot( point ) + this.constant;
  9017. }
  9018. distanceToSphere( sphere ) {
  9019. return this.distanceToPoint( sphere.center ) - sphere.radius;
  9020. }
  9021. projectPoint( point, target ) {
  9022. return target.copy( point ).addScaledVector( this.normal, - this.distanceToPoint( point ) );
  9023. }
  9024. intersectLine( line, target ) {
  9025. const direction = line.delta( _vector1 );
  9026. const denominator = this.normal.dot( direction );
  9027. if ( denominator === 0 ) {
  9028. // line is coplanar, return origin
  9029. if ( this.distanceToPoint( line.start ) === 0 ) {
  9030. return target.copy( line.start );
  9031. }
  9032. // Unsure if this is the correct method to handle this case.
  9033. return null;
  9034. }
  9035. const t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
  9036. if ( t < 0 || t > 1 ) {
  9037. return null;
  9038. }
  9039. return target.copy( line.start ).addScaledVector( direction, t );
  9040. }
  9041. intersectsLine( line ) {
  9042. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  9043. const startSign = this.distanceToPoint( line.start );
  9044. const endSign = this.distanceToPoint( line.end );
  9045. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  9046. }
  9047. intersectsBox( box ) {
  9048. return box.intersectsPlane( this );
  9049. }
  9050. intersectsSphere( sphere ) {
  9051. return sphere.intersectsPlane( this );
  9052. }
  9053. coplanarPoint( target ) {
  9054. return target.copy( this.normal ).multiplyScalar( - this.constant );
  9055. }
  9056. applyMatrix4( matrix, optionalNormalMatrix ) {
  9057. const normalMatrix = optionalNormalMatrix || _normalMatrix.getNormalMatrix( matrix );
  9058. const referencePoint = this.coplanarPoint( _vector1 ).applyMatrix4( matrix );
  9059. const normal = this.normal.applyMatrix3( normalMatrix ).normalize();
  9060. this.constant = - referencePoint.dot( normal );
  9061. return this;
  9062. }
  9063. translate( offset ) {
  9064. this.constant -= offset.dot( this.normal );
  9065. return this;
  9066. }
  9067. equals( plane ) {
  9068. return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
  9069. }
  9070. clone() {
  9071. return new this.constructor().copy( this );
  9072. }
  9073. }
  9074. const _sphere$3 = /*@__PURE__*/ new Sphere();
  9075. const _vector$6 = /*@__PURE__*/ new Vector3();
  9076. class Frustum {
  9077. constructor( p0 = new Plane(), p1 = new Plane(), p2 = new Plane(), p3 = new Plane(), p4 = new Plane(), p5 = new Plane() ) {
  9078. this.planes = [ p0, p1, p2, p3, p4, p5 ];
  9079. }
  9080. set( p0, p1, p2, p3, p4, p5 ) {
  9081. const planes = this.planes;
  9082. planes[ 0 ].copy( p0 );
  9083. planes[ 1 ].copy( p1 );
  9084. planes[ 2 ].copy( p2 );
  9085. planes[ 3 ].copy( p3 );
  9086. planes[ 4 ].copy( p4 );
  9087. planes[ 5 ].copy( p5 );
  9088. return this;
  9089. }
  9090. copy( frustum ) {
  9091. const planes = this.planes;
  9092. for ( let i = 0; i < 6; i ++ ) {
  9093. planes[ i ].copy( frustum.planes[ i ] );
  9094. }
  9095. return this;
  9096. }
  9097. setFromProjectionMatrix( m, coordinateSystem = WebGLCoordinateSystem ) {
  9098. const planes = this.planes;
  9099. const me = m.elements;
  9100. const me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ];
  9101. const me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ];
  9102. const me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ];
  9103. const me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ];
  9104. planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize();
  9105. planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();
  9106. planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();
  9107. planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();
  9108. planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();
  9109. if ( coordinateSystem === WebGLCoordinateSystem ) {
  9110. planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize();
  9111. } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
  9112. planes[ 5 ].setComponents( me2, me6, me10, me14 ).normalize();
  9113. } else {
  9114. throw new Error( 'THREE.Frustum.setFromProjectionMatrix(): Invalid coordinate system: ' + coordinateSystem );
  9115. }
  9116. return this;
  9117. }
  9118. intersectsObject( object ) {
  9119. if ( object.boundingSphere !== undefined ) {
  9120. if ( object.boundingSphere === null ) object.computeBoundingSphere();
  9121. _sphere$3.copy( object.boundingSphere ).applyMatrix4( object.matrixWorld );
  9122. } else {
  9123. const geometry = object.geometry;
  9124. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  9125. _sphere$3.copy( geometry.boundingSphere ).applyMatrix4( object.matrixWorld );
  9126. }
  9127. return this.intersectsSphere( _sphere$3 );
  9128. }
  9129. intersectsSprite( sprite ) {
  9130. _sphere$3.center.set( 0, 0, 0 );
  9131. _sphere$3.radius = 0.7071067811865476;
  9132. _sphere$3.applyMatrix4( sprite.matrixWorld );
  9133. return this.intersectsSphere( _sphere$3 );
  9134. }
  9135. intersectsSphere( sphere ) {
  9136. const planes = this.planes;
  9137. const center = sphere.center;
  9138. const negRadius = - sphere.radius;
  9139. for ( let i = 0; i < 6; i ++ ) {
  9140. const distance = planes[ i ].distanceToPoint( center );
  9141. if ( distance < negRadius ) {
  9142. return false;
  9143. }
  9144. }
  9145. return true;
  9146. }
  9147. intersectsBox( box ) {
  9148. const planes = this.planes;
  9149. for ( let i = 0; i < 6; i ++ ) {
  9150. const plane = planes[ i ];
  9151. // corner at max distance
  9152. _vector$6.x = plane.normal.x > 0 ? box.max.x : box.min.x;
  9153. _vector$6.y = plane.normal.y > 0 ? box.max.y : box.min.y;
  9154. _vector$6.z = plane.normal.z > 0 ? box.max.z : box.min.z;
  9155. if ( plane.distanceToPoint( _vector$6 ) < 0 ) {
  9156. return false;
  9157. }
  9158. }
  9159. return true;
  9160. }
  9161. containsPoint( point ) {
  9162. const planes = this.planes;
  9163. for ( let i = 0; i < 6; i ++ ) {
  9164. if ( planes[ i ].distanceToPoint( point ) < 0 ) {
  9165. return false;
  9166. }
  9167. }
  9168. return true;
  9169. }
  9170. clone() {
  9171. return new this.constructor().copy( this );
  9172. }
  9173. }
  9174. function ascIdSort( a, b ) {
  9175. return a - b;
  9176. }
  9177. function sortOpaque( a, b ) {
  9178. return a.z - b.z;
  9179. }
  9180. function sortTransparent( a, b ) {
  9181. return b.z - a.z;
  9182. }
  9183. class MultiDrawRenderList {
  9184. constructor() {
  9185. this.index = 0;
  9186. this.pool = [];
  9187. this.list = [];
  9188. }
  9189. push( start, count, z, index ) {
  9190. const pool = this.pool;
  9191. const list = this.list;
  9192. if ( this.index >= pool.length ) {
  9193. pool.push( {
  9194. start: - 1,
  9195. count: - 1,
  9196. z: - 1,
  9197. index: - 1,
  9198. } );
  9199. }
  9200. const item = pool[ this.index ];
  9201. list.push( item );
  9202. this.index ++;
  9203. item.start = start;
  9204. item.count = count;
  9205. item.z = z;
  9206. item.index = index;
  9207. }
  9208. reset() {
  9209. this.list.length = 0;
  9210. this.index = 0;
  9211. }
  9212. }
  9213. const _matrix$1 = /*@__PURE__*/ new Matrix4();
  9214. const _whiteColor = /*@__PURE__*/ new Color( 1, 1, 1 );
  9215. const _frustum = /*@__PURE__*/ new Frustum();
  9216. const _box$1 = /*@__PURE__*/ new Box3();
  9217. const _sphere$2 = /*@__PURE__*/ new Sphere();
  9218. const _vector$5 = /*@__PURE__*/ new Vector3();
  9219. const _forward = /*@__PURE__*/ new Vector3();
  9220. const _temp = /*@__PURE__*/ new Vector3();
  9221. const _renderList = /*@__PURE__*/ new MultiDrawRenderList();
  9222. const _mesh = /*@__PURE__*/ new Mesh();
  9223. const _batchIntersects = [];
  9224. // copies data from attribute "src" into "target" starting at "targetOffset"
  9225. function copyAttributeData( src, target, targetOffset = 0 ) {
  9226. const itemSize = target.itemSize;
  9227. if ( src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor ) {
  9228. // use the component getters and setters if the array data cannot
  9229. // be copied directly
  9230. const vertexCount = src.count;
  9231. for ( let i = 0; i < vertexCount; i ++ ) {
  9232. for ( let c = 0; c < itemSize; c ++ ) {
  9233. target.setComponent( i + targetOffset, c, src.getComponent( i, c ) );
  9234. }
  9235. }
  9236. } else {
  9237. // faster copy approach using typed array set function
  9238. target.array.set( src.array, targetOffset * itemSize );
  9239. }
  9240. target.needsUpdate = true;
  9241. }
  9242. // safely copies array contents to a potentially smaller array
  9243. function copyArrayContents( src, target ) {
  9244. if ( src.constructor !== target.constructor ) {
  9245. // if arrays are of a different type (eg due to index size increasing) then data must be per-element copied
  9246. const len = Math.min( src.length, target.length );
  9247. for ( let i = 0; i < len; i ++ ) {
  9248. target[ i ] = src[ i ];
  9249. }
  9250. } else {
  9251. // if the arrays use the same data layout we can use a fast block copy
  9252. const len = Math.min( src.length, target.length );
  9253. target.set( new src.constructor( src.buffer, 0, len ) );
  9254. }
  9255. }
  9256. class BatchedMesh extends Mesh {
  9257. get maxInstanceCount() {
  9258. return this._maxInstanceCount;
  9259. }
  9260. get instanceCount() {
  9261. return this._instanceInfo.length - this._availableInstanceIds.length;
  9262. }
  9263. get unusedVertexCount() {
  9264. return this._maxVertexCount - this._nextVertexStart;
  9265. }
  9266. get unusedIndexCount() {
  9267. return this._maxIndexCount - this._nextIndexStart;
  9268. }
  9269. constructor( maxInstanceCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material ) {
  9270. super( new BufferGeometry(), material );
  9271. this.isBatchedMesh = true;
  9272. this.perObjectFrustumCulled = true;
  9273. this.sortObjects = true;
  9274. this.boundingBox = null;
  9275. this.boundingSphere = null;
  9276. this.customSort = null;
  9277. // stores visible, active, and geometry id per instance and reserved buffer ranges for geometries
  9278. this._instanceInfo = [];
  9279. this._geometryInfo = [];
  9280. // instance, geometry ids that have been set as inactive, and are available to be overwritten
  9281. this._availableInstanceIds = [];
  9282. this._availableGeometryIds = [];
  9283. // used to track where the next point is that geometry should be inserted
  9284. this._nextIndexStart = 0;
  9285. this._nextVertexStart = 0;
  9286. this._geometryCount = 0;
  9287. // flags
  9288. this._visibilityChanged = true;
  9289. this._geometryInitialized = false;
  9290. // cached user options
  9291. this._maxInstanceCount = maxInstanceCount;
  9292. this._maxVertexCount = maxVertexCount;
  9293. this._maxIndexCount = maxIndexCount;
  9294. // buffers for multi draw
  9295. this._multiDrawCounts = new Int32Array( maxInstanceCount );
  9296. this._multiDrawStarts = new Int32Array( maxInstanceCount );
  9297. this._multiDrawCount = 0;
  9298. this._multiDrawInstances = null;
  9299. // Local matrix per geometry by using data texture
  9300. this._matricesTexture = null;
  9301. this._indirectTexture = null;
  9302. this._colorsTexture = null;
  9303. this._initMatricesTexture();
  9304. this._initIndirectTexture();
  9305. }
  9306. _initMatricesTexture() {
  9307. // layout (1 matrix = 4 pixels)
  9308. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  9309. // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)
  9310. // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)
  9311. // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)
  9312. // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)
  9313. let size = Math.sqrt( this._maxInstanceCount * 4 ); // 4 pixels needed for 1 matrix
  9314. size = Math.ceil( size / 4 ) * 4;
  9315. size = Math.max( size, 4 );
  9316. const matricesArray = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
  9317. const matricesTexture = new DataTexture( matricesArray, size, size, RGBAFormat, FloatType );
  9318. this._matricesTexture = matricesTexture;
  9319. }
  9320. _initIndirectTexture() {
  9321. let size = Math.sqrt( this._maxInstanceCount );
  9322. size = Math.ceil( size );
  9323. const indirectArray = new Uint32Array( size * size );
  9324. const indirectTexture = new DataTexture( indirectArray, size, size, RedIntegerFormat, UnsignedIntType );
  9325. this._indirectTexture = indirectTexture;
  9326. }
  9327. _initColorsTexture() {
  9328. let size = Math.sqrt( this._maxInstanceCount );
  9329. size = Math.ceil( size );
  9330. // 4 floats per RGBA pixel initialized to white
  9331. const colorsArray = new Float32Array( size * size * 4 ).fill( 1 );
  9332. const colorsTexture = new DataTexture( colorsArray, size, size, RGBAFormat, FloatType );
  9333. colorsTexture.colorSpace = ColorManagement.workingColorSpace;
  9334. this._colorsTexture = colorsTexture;
  9335. }
  9336. _initializeGeometry( reference ) {
  9337. const geometry = this.geometry;
  9338. const maxVertexCount = this._maxVertexCount;
  9339. const maxIndexCount = this._maxIndexCount;
  9340. if ( this._geometryInitialized === false ) {
  9341. for ( const attributeName in reference.attributes ) {
  9342. const srcAttribute = reference.getAttribute( attributeName );
  9343. const { array, itemSize, normalized } = srcAttribute;
  9344. const dstArray = new array.constructor( maxVertexCount * itemSize );
  9345. const dstAttribute = new BufferAttribute( dstArray, itemSize, normalized );
  9346. geometry.setAttribute( attributeName, dstAttribute );
  9347. }
  9348. if ( reference.getIndex() !== null ) {
  9349. // Reserve last u16 index for primitive restart.
  9350. const indexArray = maxVertexCount > 65535
  9351. ? new Uint32Array( maxIndexCount )
  9352. : new Uint16Array( maxIndexCount );
  9353. geometry.setIndex( new BufferAttribute( indexArray, 1 ) );
  9354. }
  9355. this._geometryInitialized = true;
  9356. }
  9357. }
  9358. // Make sure the geometry is compatible with the existing combined geometry attributes
  9359. _validateGeometry( geometry ) {
  9360. // check to ensure the geometries are using consistent attributes and indices
  9361. const batchGeometry = this.geometry;
  9362. if ( Boolean( geometry.getIndex() ) !== Boolean( batchGeometry.getIndex() ) ) {
  9363. throw new Error( 'THREE.BatchedMesh: All geometries must consistently have "index".' );
  9364. }
  9365. for ( const attributeName in batchGeometry.attributes ) {
  9366. if ( ! geometry.hasAttribute( attributeName ) ) {
  9367. throw new Error( `THREE.BatchedMesh: Added geometry missing "${ attributeName }". All geometries must have consistent attributes.` );
  9368. }
  9369. const srcAttribute = geometry.getAttribute( attributeName );
  9370. const dstAttribute = batchGeometry.getAttribute( attributeName );
  9371. if ( srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized ) {
  9372. throw new Error( 'THREE.BatchedMesh: All attributes must have a consistent itemSize and normalized value.' );
  9373. }
  9374. }
  9375. }
  9376. validateInstanceId( instanceId ) {
  9377. const instanceInfo = this._instanceInfo;
  9378. if ( instanceId < 0 || instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
  9379. throw new Error( `THREE.BatchedMesh: Invalid instanceId ${instanceId}. Instance is either out of range or has been deleted.` );
  9380. }
  9381. }
  9382. validateGeometryId( geometryId ) {
  9383. const geometryInfoList = this._geometryInfo;
  9384. if ( geometryId < 0 || geometryId >= geometryInfoList.length || geometryInfoList[ geometryId ].active === false ) {
  9385. throw new Error( `THREE.BatchedMesh: Invalid geometryId ${geometryId}. Geometry is either out of range or has been deleted.` );
  9386. }
  9387. }
  9388. setCustomSort( func ) {
  9389. this.customSort = func;
  9390. return this;
  9391. }
  9392. computeBoundingBox() {
  9393. if ( this.boundingBox === null ) {
  9394. this.boundingBox = new Box3();
  9395. }
  9396. const boundingBox = this.boundingBox;
  9397. const instanceInfo = this._instanceInfo;
  9398. boundingBox.makeEmpty();
  9399. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9400. if ( instanceInfo[ i ].active === false ) continue;
  9401. const geometryId = instanceInfo[ i ].geometryIndex;
  9402. this.getMatrixAt( i, _matrix$1 );
  9403. this.getBoundingBoxAt( geometryId, _box$1 ).applyMatrix4( _matrix$1 );
  9404. boundingBox.union( _box$1 );
  9405. }
  9406. }
  9407. computeBoundingSphere() {
  9408. if ( this.boundingSphere === null ) {
  9409. this.boundingSphere = new Sphere();
  9410. }
  9411. const boundingSphere = this.boundingSphere;
  9412. const instanceInfo = this._instanceInfo;
  9413. boundingSphere.makeEmpty();
  9414. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9415. if ( instanceInfo[ i ].active === false ) continue;
  9416. const geometryId = instanceInfo[ i ].geometryIndex;
  9417. this.getMatrixAt( i, _matrix$1 );
  9418. this.getBoundingSphereAt( geometryId, _sphere$2 ).applyMatrix4( _matrix$1 );
  9419. boundingSphere.union( _sphere$2 );
  9420. }
  9421. }
  9422. addInstance( geometryId ) {
  9423. const atCapacity = this._instanceInfo.length >= this.maxInstanceCount;
  9424. // ensure we're not over geometry
  9425. if ( atCapacity && this._availableInstanceIds.length === 0 ) {
  9426. throw new Error( 'THREE.BatchedMesh: Maximum item count reached.' );
  9427. }
  9428. const instanceInfo = {
  9429. visible: true,
  9430. active: true,
  9431. geometryIndex: geometryId,
  9432. };
  9433. let drawId = null;
  9434. // Prioritize using previously freed instance ids
  9435. if ( this._availableInstanceIds.length > 0 ) {
  9436. this._availableInstanceIds.sort( ascIdSort );
  9437. drawId = this._availableInstanceIds.shift();
  9438. this._instanceInfo[ drawId ] = instanceInfo;
  9439. } else {
  9440. drawId = this._instanceInfo.length;
  9441. this._instanceInfo.push( instanceInfo );
  9442. }
  9443. const matricesTexture = this._matricesTexture;
  9444. _matrix$1.identity().toArray( matricesTexture.image.data, drawId * 16 );
  9445. matricesTexture.needsUpdate = true;
  9446. const colorsTexture = this._colorsTexture;
  9447. if ( colorsTexture ) {
  9448. _whiteColor.toArray( colorsTexture.image.data, drawId * 4 );
  9449. colorsTexture.needsUpdate = true;
  9450. }
  9451. this._visibilityChanged = true;
  9452. return drawId;
  9453. }
  9454. addGeometry( geometry, reservedVertexCount = - 1, reservedIndexCount = - 1 ) {
  9455. this._initializeGeometry( geometry );
  9456. this._validateGeometry( geometry );
  9457. const geometryInfo = {
  9458. // geometry information
  9459. vertexStart: - 1,
  9460. vertexCount: - 1,
  9461. reservedVertexCount: - 1,
  9462. indexStart: - 1,
  9463. indexCount: - 1,
  9464. reservedIndexCount: - 1,
  9465. // draw range information
  9466. start: - 1,
  9467. count: - 1,
  9468. // state
  9469. boundingBox: null,
  9470. boundingSphere: null,
  9471. active: true,
  9472. };
  9473. const geometryInfoList = this._geometryInfo;
  9474. geometryInfo.vertexStart = this._nextVertexStart;
  9475. geometryInfo.reservedVertexCount = reservedVertexCount === - 1 ? geometry.getAttribute( 'position' ).count : reservedVertexCount;
  9476. const index = geometry.getIndex();
  9477. const hasIndex = index !== null;
  9478. if ( hasIndex ) {
  9479. geometryInfo.indexStart = this._nextIndexStart;
  9480. geometryInfo.reservedIndexCount = reservedIndexCount === - 1 ? index.count : reservedIndexCount;
  9481. }
  9482. if (
  9483. geometryInfo.indexStart !== - 1 &&
  9484. geometryInfo.indexStart + geometryInfo.reservedIndexCount > this._maxIndexCount ||
  9485. geometryInfo.vertexStart + geometryInfo.reservedVertexCount > this._maxVertexCount
  9486. ) {
  9487. throw new Error( 'THREE.BatchedMesh: Reserved space request exceeds the maximum buffer size.' );
  9488. }
  9489. // update id
  9490. let geometryId;
  9491. if ( this._availableGeometryIds.length > 0 ) {
  9492. this._availableGeometryIds.sort( ascIdSort );
  9493. geometryId = this._availableGeometryIds.shift();
  9494. geometryInfoList[ geometryId ] = geometryInfo;
  9495. } else {
  9496. geometryId = this._geometryCount;
  9497. this._geometryCount ++;
  9498. geometryInfoList.push( geometryInfo );
  9499. }
  9500. // update the geometry
  9501. this.setGeometryAt( geometryId, geometry );
  9502. // increment the next geometry position
  9503. this._nextIndexStart = geometryInfo.indexStart + geometryInfo.reservedIndexCount;
  9504. this._nextVertexStart = geometryInfo.vertexStart + geometryInfo.reservedVertexCount;
  9505. return geometryId;
  9506. }
  9507. setGeometryAt( geometryId, geometry ) {
  9508. if ( geometryId >= this._geometryCount ) {
  9509. throw new Error( 'THREE.BatchedMesh: Maximum geometry count reached.' );
  9510. }
  9511. this._validateGeometry( geometry );
  9512. const batchGeometry = this.geometry;
  9513. const hasIndex = batchGeometry.getIndex() !== null;
  9514. const dstIndex = batchGeometry.getIndex();
  9515. const srcIndex = geometry.getIndex();
  9516. const geometryInfo = this._geometryInfo[ geometryId ];
  9517. if (
  9518. hasIndex &&
  9519. srcIndex.count > geometryInfo.reservedIndexCount ||
  9520. geometry.attributes.position.count > geometryInfo.reservedVertexCount
  9521. ) {
  9522. throw new Error( 'THREE.BatchedMesh: Reserved space not large enough for provided geometry.' );
  9523. }
  9524. // copy geometry buffer data over
  9525. const vertexStart = geometryInfo.vertexStart;
  9526. const reservedVertexCount = geometryInfo.reservedVertexCount;
  9527. geometryInfo.vertexCount = geometry.getAttribute( 'position' ).count;
  9528. for ( const attributeName in batchGeometry.attributes ) {
  9529. // copy attribute data
  9530. const srcAttribute = geometry.getAttribute( attributeName );
  9531. const dstAttribute = batchGeometry.getAttribute( attributeName );
  9532. copyAttributeData( srcAttribute, dstAttribute, vertexStart );
  9533. // fill the rest in with zeroes
  9534. const itemSize = srcAttribute.itemSize;
  9535. for ( let i = srcAttribute.count, l = reservedVertexCount; i < l; i ++ ) {
  9536. const index = vertexStart + i;
  9537. for ( let c = 0; c < itemSize; c ++ ) {
  9538. dstAttribute.setComponent( index, c, 0 );
  9539. }
  9540. }
  9541. dstAttribute.needsUpdate = true;
  9542. dstAttribute.addUpdateRange( vertexStart * itemSize, reservedVertexCount * itemSize );
  9543. }
  9544. // copy index
  9545. if ( hasIndex ) {
  9546. const indexStart = geometryInfo.indexStart;
  9547. const reservedIndexCount = geometryInfo.reservedIndexCount;
  9548. geometryInfo.indexCount = geometry.getIndex().count;
  9549. // copy index data over
  9550. for ( let i = 0; i < srcIndex.count; i ++ ) {
  9551. dstIndex.setX( indexStart + i, vertexStart + srcIndex.getX( i ) );
  9552. }
  9553. // fill the rest in with zeroes
  9554. for ( let i = srcIndex.count, l = reservedIndexCount; i < l; i ++ ) {
  9555. dstIndex.setX( indexStart + i, vertexStart );
  9556. }
  9557. dstIndex.needsUpdate = true;
  9558. dstIndex.addUpdateRange( indexStart, geometryInfo.reservedIndexCount );
  9559. }
  9560. // update the draw range
  9561. geometryInfo.start = hasIndex ? geometryInfo.indexStart : geometryInfo.vertexStart;
  9562. geometryInfo.count = hasIndex ? geometryInfo.indexCount : geometryInfo.vertexCount;
  9563. // store the bounding boxes
  9564. geometryInfo.boundingBox = null;
  9565. if ( geometry.boundingBox !== null ) {
  9566. geometryInfo.boundingBox = geometry.boundingBox.clone();
  9567. }
  9568. geometryInfo.boundingSphere = null;
  9569. if ( geometry.boundingSphere !== null ) {
  9570. geometryInfo.boundingSphere = geometry.boundingSphere.clone();
  9571. }
  9572. this._visibilityChanged = true;
  9573. return geometryId;
  9574. }
  9575. deleteGeometry( geometryId ) {
  9576. const geometryInfoList = this._geometryInfo;
  9577. if ( geometryId >= geometryInfoList.length || geometryInfoList[ geometryId ].active === false ) {
  9578. return this;
  9579. }
  9580. // delete any instances associated with this geometry
  9581. const instanceInfo = this._instanceInfo;
  9582. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9583. if ( instanceInfo[ i ].geometryIndex === geometryId ) {
  9584. this.deleteInstance( i );
  9585. }
  9586. }
  9587. geometryInfoList[ geometryId ].active = false;
  9588. this._availableGeometryIds.push( geometryId );
  9589. this._visibilityChanged = true;
  9590. return this;
  9591. }
  9592. deleteInstance( instanceId ) {
  9593. this.validateInstanceId( instanceId );
  9594. this._instanceInfo[ instanceId ].active = false;
  9595. this._availableInstanceIds.push( instanceId );
  9596. this._visibilityChanged = true;
  9597. return this;
  9598. }
  9599. optimize() {
  9600. // track the next indices to copy data to
  9601. let nextVertexStart = 0;
  9602. let nextIndexStart = 0;
  9603. // Iterate over all geometry ranges in order sorted from earliest in the geometry buffer to latest
  9604. // in the geometry buffer. Because draw range objects can be reused there is no guarantee of their order.
  9605. const geometryInfoList = this._geometryInfo;
  9606. const indices = geometryInfoList
  9607. .map( ( e, i ) => i )
  9608. .sort( ( a, b ) => {
  9609. return geometryInfoList[ a ].vertexStart - geometryInfoList[ b ].vertexStart;
  9610. } );
  9611. const geometry = this.geometry;
  9612. for ( let i = 0, l = geometryInfoList.length; i < l; i ++ ) {
  9613. // if a geometry range is inactive then don't copy anything
  9614. const index = indices[ i ];
  9615. const geometryInfo = geometryInfoList[ index ];
  9616. if ( geometryInfo.active === false ) {
  9617. continue;
  9618. }
  9619. // if a geometry contains an index buffer then shift it, as well
  9620. if ( geometry.index !== null ) {
  9621. if ( geometryInfo.indexStart !== nextIndexStart ) {
  9622. const { indexStart, vertexStart, reservedIndexCount } = geometryInfo;
  9623. const index = geometry.index;
  9624. const array = index.array;
  9625. // shift the index pointers based on how the vertex data will shift
  9626. // adjusting the index must happen first so the original vertex start value is available
  9627. const elementDelta = nextVertexStart - vertexStart;
  9628. for ( let j = indexStart; j < indexStart + reservedIndexCount; j ++ ) {
  9629. array[ j ] = array[ j ] + elementDelta;
  9630. }
  9631. index.array.copyWithin( nextIndexStart, indexStart, indexStart + reservedIndexCount );
  9632. index.addUpdateRange( nextIndexStart, reservedIndexCount );
  9633. geometryInfo.indexStart = nextIndexStart;
  9634. }
  9635. nextIndexStart += geometryInfo.reservedIndexCount;
  9636. }
  9637. // if a geometry needs to be moved then copy attribute data to overwrite unused space
  9638. if ( geometryInfo.vertexStart !== nextVertexStart ) {
  9639. const { vertexStart, reservedVertexCount } = geometryInfo;
  9640. const attributes = geometry.attributes;
  9641. for ( const key in attributes ) {
  9642. const attribute = attributes[ key ];
  9643. const { array, itemSize } = attribute;
  9644. array.copyWithin( nextVertexStart * itemSize, vertexStart * itemSize, ( vertexStart + reservedVertexCount ) * itemSize );
  9645. attribute.addUpdateRange( nextVertexStart * itemSize, reservedVertexCount * itemSize );
  9646. }
  9647. geometryInfo.vertexStart = nextVertexStart;
  9648. }
  9649. nextVertexStart += geometryInfo.reservedVertexCount;
  9650. geometryInfo.start = geometry.index ? geometryInfo.indexStart : geometryInfo.vertexStart;
  9651. // step the next geometry points to the shifted position
  9652. this._nextIndexStart = geometry.index ? geometryInfo.indexStart + geometryInfo.reservedIndexCount : 0;
  9653. this._nextVertexStart = geometryInfo.vertexStart + geometryInfo.reservedVertexCount;
  9654. }
  9655. return this;
  9656. }
  9657. // get bounding box and compute it if it doesn't exist
  9658. getBoundingBoxAt( geometryId, target ) {
  9659. if ( geometryId >= this._geometryCount ) {
  9660. return null;
  9661. }
  9662. // compute bounding box
  9663. const geometry = this.geometry;
  9664. const geometryInfo = this._geometryInfo[ geometryId ];
  9665. if ( geometryInfo.boundingBox === null ) {
  9666. const box = new Box3();
  9667. const index = geometry.index;
  9668. const position = geometry.attributes.position;
  9669. for ( let i = geometryInfo.start, l = geometryInfo.start + geometryInfo.count; i < l; i ++ ) {
  9670. let iv = i;
  9671. if ( index ) {
  9672. iv = index.getX( iv );
  9673. }
  9674. box.expandByPoint( _vector$5.fromBufferAttribute( position, iv ) );
  9675. }
  9676. geometryInfo.boundingBox = box;
  9677. }
  9678. target.copy( geometryInfo.boundingBox );
  9679. return target;
  9680. }
  9681. // get bounding sphere and compute it if it doesn't exist
  9682. getBoundingSphereAt( geometryId, target ) {
  9683. if ( geometryId >= this._geometryCount ) {
  9684. return null;
  9685. }
  9686. // compute bounding sphere
  9687. const geometry = this.geometry;
  9688. const geometryInfo = this._geometryInfo[ geometryId ];
  9689. if ( geometryInfo.boundingSphere === null ) {
  9690. const sphere = new Sphere();
  9691. this.getBoundingBoxAt( geometryId, _box$1 );
  9692. _box$1.getCenter( sphere.center );
  9693. const index = geometry.index;
  9694. const position = geometry.attributes.position;
  9695. let maxRadiusSq = 0;
  9696. for ( let i = geometryInfo.start, l = geometryInfo.start + geometryInfo.count; i < l; i ++ ) {
  9697. let iv = i;
  9698. if ( index ) {
  9699. iv = index.getX( iv );
  9700. }
  9701. _vector$5.fromBufferAttribute( position, iv );
  9702. maxRadiusSq = Math.max( maxRadiusSq, sphere.center.distanceToSquared( _vector$5 ) );
  9703. }
  9704. sphere.radius = Math.sqrt( maxRadiusSq );
  9705. geometryInfo.boundingSphere = sphere;
  9706. }
  9707. target.copy( geometryInfo.boundingSphere );
  9708. return target;
  9709. }
  9710. setMatrixAt( instanceId, matrix ) {
  9711. this.validateInstanceId( instanceId );
  9712. const matricesTexture = this._matricesTexture;
  9713. const matricesArray = this._matricesTexture.image.data;
  9714. matrix.toArray( matricesArray, instanceId * 16 );
  9715. matricesTexture.needsUpdate = true;
  9716. return this;
  9717. }
  9718. getMatrixAt( instanceId, matrix ) {
  9719. this.validateInstanceId( instanceId );
  9720. return matrix.fromArray( this._matricesTexture.image.data, instanceId * 16 );
  9721. }
  9722. setColorAt( instanceId, color ) {
  9723. this.validateInstanceId( instanceId );
  9724. if ( this._colorsTexture === null ) {
  9725. this._initColorsTexture();
  9726. }
  9727. color.toArray( this._colorsTexture.image.data, instanceId * 4 );
  9728. this._colorsTexture.needsUpdate = true;
  9729. return this;
  9730. }
  9731. getColorAt( instanceId, color ) {
  9732. this.validateInstanceId( instanceId );
  9733. return color.fromArray( this._colorsTexture.image.data, instanceId * 4 );
  9734. }
  9735. setVisibleAt( instanceId, value ) {
  9736. this.validateInstanceId( instanceId );
  9737. if ( this._instanceInfo[ instanceId ].visible === value ) {
  9738. return this;
  9739. }
  9740. this._instanceInfo[ instanceId ].visible = value;
  9741. this._visibilityChanged = true;
  9742. return this;
  9743. }
  9744. getVisibleAt( instanceId ) {
  9745. this.validateInstanceId( instanceId );
  9746. return this._instanceInfo[ instanceId ].visible;
  9747. }
  9748. setGeometryIdAt( instanceId, geometryId ) {
  9749. this.validateInstanceId( instanceId );
  9750. this.validateGeometryId( geometryId );
  9751. this._instanceInfo[ instanceId ].geometryIndex = geometryId;
  9752. return this;
  9753. }
  9754. getGeometryIdAt( instanceId ) {
  9755. this.validateInstanceId( instanceId );
  9756. return this._instanceInfo[ instanceId ].geometryIndex;
  9757. }
  9758. getGeometryRangeAt( geometryId, target = {} ) {
  9759. this.validateGeometryId( geometryId );
  9760. const geometryInfo = this._geometryInfo[ geometryId ];
  9761. target.vertexStart = geometryInfo.vertexStart;
  9762. target.vertexCount = geometryInfo.vertexCount;
  9763. target.reservedVertexCount = geometryInfo.reservedVertexCount;
  9764. target.indexStart = geometryInfo.indexStart;
  9765. target.indexCount = geometryInfo.indexCount;
  9766. target.reservedIndexCount = geometryInfo.reservedIndexCount;
  9767. target.start = geometryInfo.start;
  9768. target.count = geometryInfo.count;
  9769. return target;
  9770. }
  9771. setInstanceCount( maxInstanceCount ) {
  9772. // shrink the available instances as much as possible
  9773. const availableInstanceIds = this._availableInstanceIds;
  9774. const instanceInfo = this._instanceInfo;
  9775. availableInstanceIds.sort( ascIdSort );
  9776. while ( availableInstanceIds[ availableInstanceIds.length - 1 ] === instanceInfo.length ) {
  9777. instanceInfo.pop();
  9778. availableInstanceIds.pop();
  9779. }
  9780. // throw an error if it can't be shrunk to the desired size
  9781. if ( maxInstanceCount < instanceInfo.length ) {
  9782. throw new Error( `BatchedMesh: Instance ids outside the range ${ maxInstanceCount } are being used. Cannot shrink instance count.` );
  9783. }
  9784. // copy the multi draw counts
  9785. const multiDrawCounts = new Int32Array( maxInstanceCount );
  9786. const multiDrawStarts = new Int32Array( maxInstanceCount );
  9787. copyArrayContents( this._multiDrawCounts, multiDrawCounts );
  9788. copyArrayContents( this._multiDrawStarts, multiDrawStarts );
  9789. this._multiDrawCounts = multiDrawCounts;
  9790. this._multiDrawStarts = multiDrawStarts;
  9791. this._maxInstanceCount = maxInstanceCount;
  9792. // update texture data for instance sampling
  9793. const indirectTexture = this._indirectTexture;
  9794. const matricesTexture = this._matricesTexture;
  9795. const colorsTexture = this._colorsTexture;
  9796. indirectTexture.dispose();
  9797. this._initIndirectTexture();
  9798. copyArrayContents( indirectTexture.image.data, this._indirectTexture.image.data );
  9799. matricesTexture.dispose();
  9800. this._initMatricesTexture();
  9801. copyArrayContents( matricesTexture.image.data, this._matricesTexture.image.data );
  9802. if ( colorsTexture ) {
  9803. colorsTexture.dispose();
  9804. this._initColorsTexture();
  9805. copyArrayContents( colorsTexture.image.data, this._colorsTexture.image.data );
  9806. }
  9807. }
  9808. setGeometrySize( maxVertexCount, maxIndexCount ) {
  9809. // Check if we can shrink to the requested vertex attribute size
  9810. const validRanges = [ ...this._geometryInfo ].filter( info => info.active );
  9811. const requiredVertexLength = Math.max( ...validRanges.map( range => range.vertexStart + range.reservedVertexCount ) );
  9812. if ( requiredVertexLength > maxVertexCount ) {
  9813. throw new Error( `BatchedMesh: Geometry vertex values are being used outside the range ${ maxIndexCount }. Cannot shrink further.` );
  9814. }
  9815. // Check if we can shrink to the requested index attribute size
  9816. if ( this.geometry.index ) {
  9817. const requiredIndexLength = Math.max( ...validRanges.map( range => range.indexStart + range.reservedIndexCount ) );
  9818. if ( requiredIndexLength > maxIndexCount ) {
  9819. throw new Error( `BatchedMesh: Geometry index values are being used outside the range ${ maxIndexCount }. Cannot shrink further.` );
  9820. }
  9821. }
  9822. //
  9823. // dispose of the previous geometry
  9824. const oldGeometry = this.geometry;
  9825. oldGeometry.dispose();
  9826. // recreate the geometry needed based on the previous variant
  9827. this._maxVertexCount = maxVertexCount;
  9828. this._maxIndexCount = maxIndexCount;
  9829. if ( this._geometryInitialized ) {
  9830. this._geometryInitialized = false;
  9831. this.geometry = new BufferGeometry();
  9832. this._initializeGeometry( oldGeometry );
  9833. }
  9834. // copy data from the previous geometry
  9835. const geometry = this.geometry;
  9836. if ( oldGeometry.index ) {
  9837. copyArrayContents( oldGeometry.index.array, geometry.index.array );
  9838. }
  9839. for ( const key in oldGeometry.attributes ) {
  9840. copyArrayContents( oldGeometry.attributes[ key ].array, geometry.attributes[ key ].array );
  9841. }
  9842. }
  9843. raycast( raycaster, intersects ) {
  9844. const instanceInfo = this._instanceInfo;
  9845. const geometryInfoList = this._geometryInfo;
  9846. const matrixWorld = this.matrixWorld;
  9847. const batchGeometry = this.geometry;
  9848. // iterate over each geometry
  9849. _mesh.material = this.material;
  9850. _mesh.geometry.index = batchGeometry.index;
  9851. _mesh.geometry.attributes = batchGeometry.attributes;
  9852. if ( _mesh.geometry.boundingBox === null ) {
  9853. _mesh.geometry.boundingBox = new Box3();
  9854. }
  9855. if ( _mesh.geometry.boundingSphere === null ) {
  9856. _mesh.geometry.boundingSphere = new Sphere();
  9857. }
  9858. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9859. if ( ! instanceInfo[ i ].visible || ! instanceInfo[ i ].active ) {
  9860. continue;
  9861. }
  9862. const geometryId = instanceInfo[ i ].geometryIndex;
  9863. const geometryInfo = geometryInfoList[ geometryId ];
  9864. _mesh.geometry.setDrawRange( geometryInfo.start, geometryInfo.count );
  9865. // get the intersects
  9866. this.getMatrixAt( i, _mesh.matrixWorld ).premultiply( matrixWorld );
  9867. this.getBoundingBoxAt( geometryId, _mesh.geometry.boundingBox );
  9868. this.getBoundingSphereAt( geometryId, _mesh.geometry.boundingSphere );
  9869. _mesh.raycast( raycaster, _batchIntersects );
  9870. // add batch id to the intersects
  9871. for ( let j = 0, l = _batchIntersects.length; j < l; j ++ ) {
  9872. const intersect = _batchIntersects[ j ];
  9873. intersect.object = this;
  9874. intersect.batchId = i;
  9875. intersects.push( intersect );
  9876. }
  9877. _batchIntersects.length = 0;
  9878. }
  9879. _mesh.material = null;
  9880. _mesh.geometry.index = null;
  9881. _mesh.geometry.attributes = {};
  9882. _mesh.geometry.setDrawRange( 0, Infinity );
  9883. }
  9884. copy( source ) {
  9885. super.copy( source );
  9886. this.geometry = source.geometry.clone();
  9887. this.perObjectFrustumCulled = source.perObjectFrustumCulled;
  9888. this.sortObjects = source.sortObjects;
  9889. this.boundingBox = source.boundingBox !== null ? source.boundingBox.clone() : null;
  9890. this.boundingSphere = source.boundingSphere !== null ? source.boundingSphere.clone() : null;
  9891. this._geometryInfo = source._geometryInfo.map( info => ( {
  9892. ...info,
  9893. boundingBox: info.boundingBox !== null ? info.boundingBox.clone() : null,
  9894. boundingSphere: info.boundingSphere !== null ? info.boundingSphere.clone() : null,
  9895. } ) );
  9896. this._instanceInfo = source._instanceInfo.map( info => ( { ...info } ) );
  9897. this._maxInstanceCount = source._maxInstanceCount;
  9898. this._maxVertexCount = source._maxVertexCount;
  9899. this._maxIndexCount = source._maxIndexCount;
  9900. this._geometryInitialized = source._geometryInitialized;
  9901. this._geometryCount = source._geometryCount;
  9902. this._multiDrawCounts = source._multiDrawCounts.slice();
  9903. this._multiDrawStarts = source._multiDrawStarts.slice();
  9904. this._matricesTexture = source._matricesTexture.clone();
  9905. this._matricesTexture.image.data = this._matricesTexture.image.data.slice();
  9906. if ( this._colorsTexture !== null ) {
  9907. this._colorsTexture = source._colorsTexture.clone();
  9908. this._colorsTexture.image.data = this._colorsTexture.image.data.slice();
  9909. }
  9910. return this;
  9911. }
  9912. dispose() {
  9913. // Assuming the geometry is not shared with other meshes
  9914. this.geometry.dispose();
  9915. this._matricesTexture.dispose();
  9916. this._matricesTexture = null;
  9917. this._indirectTexture.dispose();
  9918. this._indirectTexture = null;
  9919. if ( this._colorsTexture !== null ) {
  9920. this._colorsTexture.dispose();
  9921. this._colorsTexture = null;
  9922. }
  9923. return this;
  9924. }
  9925. onBeforeRender( renderer, scene, camera, geometry, material/*, _group*/ ) {
  9926. // if visibility has not changed and frustum culling and object sorting is not required
  9927. // then skip iterating over all items
  9928. if ( ! this._visibilityChanged && ! this.perObjectFrustumCulled && ! this.sortObjects ) {
  9929. return;
  9930. }
  9931. // the indexed version of the multi draw function requires specifying the start
  9932. // offset in bytes.
  9933. const index = geometry.getIndex();
  9934. const bytesPerElement = index === null ? 1 : index.array.BYTES_PER_ELEMENT;
  9935. const instanceInfo = this._instanceInfo;
  9936. const multiDrawStarts = this._multiDrawStarts;
  9937. const multiDrawCounts = this._multiDrawCounts;
  9938. const geometryInfoList = this._geometryInfo;
  9939. const perObjectFrustumCulled = this.perObjectFrustumCulled;
  9940. const indirectTexture = this._indirectTexture;
  9941. const indirectArray = indirectTexture.image.data;
  9942. // prepare the frustum in the local frame
  9943. if ( perObjectFrustumCulled ) {
  9944. _matrix$1
  9945. .multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse )
  9946. .multiply( this.matrixWorld );
  9947. _frustum.setFromProjectionMatrix(
  9948. _matrix$1,
  9949. renderer.coordinateSystem
  9950. );
  9951. }
  9952. let multiDrawCount = 0;
  9953. if ( this.sortObjects ) {
  9954. // get the camera position in the local frame
  9955. _matrix$1.copy( this.matrixWorld ).invert();
  9956. _vector$5.setFromMatrixPosition( camera.matrixWorld ).applyMatrix4( _matrix$1 );
  9957. _forward.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld ).transformDirection( _matrix$1 );
  9958. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9959. if ( instanceInfo[ i ].visible && instanceInfo[ i ].active ) {
  9960. const geometryId = instanceInfo[ i ].geometryIndex;
  9961. // get the bounds in world space
  9962. this.getMatrixAt( i, _matrix$1 );
  9963. this.getBoundingSphereAt( geometryId, _sphere$2 ).applyMatrix4( _matrix$1 );
  9964. // determine whether the batched geometry is within the frustum
  9965. let culled = false;
  9966. if ( perObjectFrustumCulled ) {
  9967. culled = ! _frustum.intersectsSphere( _sphere$2 );
  9968. }
  9969. if ( ! culled ) {
  9970. // get the distance from camera used for sorting
  9971. const geometryInfo = geometryInfoList[ geometryId ];
  9972. const z = _temp.subVectors( _sphere$2.center, _vector$5 ).dot( _forward );
  9973. _renderList.push( geometryInfo.start, geometryInfo.count, z, i );
  9974. }
  9975. }
  9976. }
  9977. // Sort the draw ranges and prep for rendering
  9978. const list = _renderList.list;
  9979. const customSort = this.customSort;
  9980. if ( customSort === null ) {
  9981. list.sort( material.transparent ? sortTransparent : sortOpaque );
  9982. } else {
  9983. customSort.call( this, list, camera );
  9984. }
  9985. for ( let i = 0, l = list.length; i < l; i ++ ) {
  9986. const item = list[ i ];
  9987. multiDrawStarts[ multiDrawCount ] = item.start * bytesPerElement;
  9988. multiDrawCounts[ multiDrawCount ] = item.count;
  9989. indirectArray[ multiDrawCount ] = item.index;
  9990. multiDrawCount ++;
  9991. }
  9992. _renderList.reset();
  9993. } else {
  9994. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9995. if ( instanceInfo[ i ].visible && instanceInfo[ i ].active ) {
  9996. const geometryId = instanceInfo[ i ].geometryIndex;
  9997. // determine whether the batched geometry is within the frustum
  9998. let culled = false;
  9999. if ( perObjectFrustumCulled ) {
  10000. // get the bounds in world space
  10001. this.getMatrixAt( i, _matrix$1 );
  10002. this.getBoundingSphereAt( geometryId, _sphere$2 ).applyMatrix4( _matrix$1 );
  10003. culled = ! _frustum.intersectsSphere( _sphere$2 );
  10004. }
  10005. if ( ! culled ) {
  10006. const geometryInfo = geometryInfoList[ geometryId ];
  10007. multiDrawStarts[ multiDrawCount ] = geometryInfo.start * bytesPerElement;
  10008. multiDrawCounts[ multiDrawCount ] = geometryInfo.count;
  10009. indirectArray[ multiDrawCount ] = i;
  10010. multiDrawCount ++;
  10011. }
  10012. }
  10013. }
  10014. }
  10015. indirectTexture.needsUpdate = true;
  10016. this._multiDrawCount = multiDrawCount;
  10017. this._visibilityChanged = false;
  10018. }
  10019. onBeforeShadow( renderer, object, camera, shadowCamera, geometry, depthMaterial/* , group */ ) {
  10020. this.onBeforeRender( renderer, null, shadowCamera, geometry, depthMaterial );
  10021. }
  10022. }
  10023. class LineBasicMaterial extends Material {
  10024. constructor( parameters ) {
  10025. super();
  10026. this.isLineBasicMaterial = true;
  10027. this.type = 'LineBasicMaterial';
  10028. this.color = new Color( 0xffffff );
  10029. this.map = null;
  10030. this.linewidth = 1;
  10031. this.linecap = 'round';
  10032. this.linejoin = 'round';
  10033. this.fog = true;
  10034. this.setValues( parameters );
  10035. }
  10036. copy( source ) {
  10037. super.copy( source );
  10038. this.color.copy( source.color );
  10039. this.map = source.map;
  10040. this.linewidth = source.linewidth;
  10041. this.linecap = source.linecap;
  10042. this.linejoin = source.linejoin;
  10043. this.fog = source.fog;
  10044. return this;
  10045. }
  10046. }
  10047. const _vStart = /*@__PURE__*/ new Vector3();
  10048. const _vEnd = /*@__PURE__*/ new Vector3();
  10049. const _inverseMatrix$1 = /*@__PURE__*/ new Matrix4();
  10050. const _ray$1 = /*@__PURE__*/ new Ray();
  10051. const _sphere$1 = /*@__PURE__*/ new Sphere();
  10052. const _intersectPointOnRay = /*@__PURE__*/ new Vector3();
  10053. const _intersectPointOnSegment = /*@__PURE__*/ new Vector3();
  10054. class Line extends Object3D {
  10055. constructor( geometry = new BufferGeometry(), material = new LineBasicMaterial() ) {
  10056. super();
  10057. this.isLine = true;
  10058. this.type = 'Line';
  10059. this.geometry = geometry;
  10060. this.material = material;
  10061. this.updateMorphTargets();
  10062. }
  10063. copy( source, recursive ) {
  10064. super.copy( source, recursive );
  10065. this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
  10066. this.geometry = source.geometry;
  10067. return this;
  10068. }
  10069. computeLineDistances() {
  10070. const geometry = this.geometry;
  10071. // we assume non-indexed geometry
  10072. if ( geometry.index === null ) {
  10073. const positionAttribute = geometry.attributes.position;
  10074. const lineDistances = [ 0 ];
  10075. for ( let i = 1, l = positionAttribute.count; i < l; i ++ ) {
  10076. _vStart.fromBufferAttribute( positionAttribute, i - 1 );
  10077. _vEnd.fromBufferAttribute( positionAttribute, i );
  10078. lineDistances[ i ] = lineDistances[ i - 1 ];
  10079. lineDistances[ i ] += _vStart.distanceTo( _vEnd );
  10080. }
  10081. geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
  10082. } else {
  10083. console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
  10084. }
  10085. return this;
  10086. }
  10087. raycast( raycaster, intersects ) {
  10088. const geometry = this.geometry;
  10089. const matrixWorld = this.matrixWorld;
  10090. const threshold = raycaster.params.Line.threshold;
  10091. const drawRange = geometry.drawRange;
  10092. // Checking boundingSphere distance to ray
  10093. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  10094. _sphere$1.copy( geometry.boundingSphere );
  10095. _sphere$1.applyMatrix4( matrixWorld );
  10096. _sphere$1.radius += threshold;
  10097. if ( raycaster.ray.intersectsSphere( _sphere$1 ) === false ) return;
  10098. //
  10099. _inverseMatrix$1.copy( matrixWorld ).invert();
  10100. _ray$1.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$1 );
  10101. const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  10102. const localThresholdSq = localThreshold * localThreshold;
  10103. const step = this.isLineSegments ? 2 : 1;
  10104. const index = geometry.index;
  10105. const attributes = geometry.attributes;
  10106. const positionAttribute = attributes.position;
  10107. if ( index !== null ) {
  10108. const start = Math.max( 0, drawRange.start );
  10109. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  10110. for ( let i = start, l = end - 1; i < l; i += step ) {
  10111. const a = index.getX( i );
  10112. const b = index.getX( i + 1 );
  10113. const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, a, b );
  10114. if ( intersect ) {
  10115. intersects.push( intersect );
  10116. }
  10117. }
  10118. if ( this.isLineLoop ) {
  10119. const a = index.getX( end - 1 );
  10120. const b = index.getX( start );
  10121. const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, a, b );
  10122. if ( intersect ) {
  10123. intersects.push( intersect );
  10124. }
  10125. }
  10126. } else {
  10127. const start = Math.max( 0, drawRange.start );
  10128. const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
  10129. for ( let i = start, l = end - 1; i < l; i += step ) {
  10130. const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, i, i + 1 );
  10131. if ( intersect ) {
  10132. intersects.push( intersect );
  10133. }
  10134. }
  10135. if ( this.isLineLoop ) {
  10136. const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, end - 1, start );
  10137. if ( intersect ) {
  10138. intersects.push( intersect );
  10139. }
  10140. }
  10141. }
  10142. }
  10143. updateMorphTargets() {
  10144. const geometry = this.geometry;
  10145. const morphAttributes = geometry.morphAttributes;
  10146. const keys = Object.keys( morphAttributes );
  10147. if ( keys.length > 0 ) {
  10148. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  10149. if ( morphAttribute !== undefined ) {
  10150. this.morphTargetInfluences = [];
  10151. this.morphTargetDictionary = {};
  10152. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  10153. const name = morphAttribute[ m ].name || String( m );
  10154. this.morphTargetInfluences.push( 0 );
  10155. this.morphTargetDictionary[ name ] = m;
  10156. }
  10157. }
  10158. }
  10159. }
  10160. }
  10161. function checkIntersection( object, raycaster, ray, thresholdSq, a, b ) {
  10162. const positionAttribute = object.geometry.attributes.position;
  10163. _vStart.fromBufferAttribute( positionAttribute, a );
  10164. _vEnd.fromBufferAttribute( positionAttribute, b );
  10165. const distSq = ray.distanceSqToSegment( _vStart, _vEnd, _intersectPointOnRay, _intersectPointOnSegment );
  10166. if ( distSq > thresholdSq ) return;
  10167. _intersectPointOnRay.applyMatrix4( object.matrixWorld ); // Move back to world space for distance calculation
  10168. const distance = raycaster.ray.origin.distanceTo( _intersectPointOnRay );
  10169. if ( distance < raycaster.near || distance > raycaster.far ) return;
  10170. return {
  10171. distance: distance,
  10172. // What do we want? intersection point on the ray or on the segment??
  10173. // point: raycaster.ray.at( distance ),
  10174. point: _intersectPointOnSegment.clone().applyMatrix4( object.matrixWorld ),
  10175. index: a,
  10176. face: null,
  10177. faceIndex: null,
  10178. barycoord: null,
  10179. object: object
  10180. };
  10181. }
  10182. const _start = /*@__PURE__*/ new Vector3();
  10183. const _end = /*@__PURE__*/ new Vector3();
  10184. class LineSegments extends Line {
  10185. constructor( geometry, material ) {
  10186. super( geometry, material );
  10187. this.isLineSegments = true;
  10188. this.type = 'LineSegments';
  10189. }
  10190. computeLineDistances() {
  10191. const geometry = this.geometry;
  10192. // we assume non-indexed geometry
  10193. if ( geometry.index === null ) {
  10194. const positionAttribute = geometry.attributes.position;
  10195. const lineDistances = [];
  10196. for ( let i = 0, l = positionAttribute.count; i < l; i += 2 ) {
  10197. _start.fromBufferAttribute( positionAttribute, i );
  10198. _end.fromBufferAttribute( positionAttribute, i + 1 );
  10199. lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ];
  10200. lineDistances[ i + 1 ] = lineDistances[ i ] + _start.distanceTo( _end );
  10201. }
  10202. geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
  10203. } else {
  10204. console.warn( 'THREE.LineSegments.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
  10205. }
  10206. return this;
  10207. }
  10208. }
  10209. class LineLoop extends Line {
  10210. constructor( geometry, material ) {
  10211. super( geometry, material );
  10212. this.isLineLoop = true;
  10213. this.type = 'LineLoop';
  10214. }
  10215. }
  10216. class PointsMaterial extends Material {
  10217. constructor( parameters ) {
  10218. super();
  10219. this.isPointsMaterial = true;
  10220. this.type = 'PointsMaterial';
  10221. this.color = new Color( 0xffffff );
  10222. this.map = null;
  10223. this.alphaMap = null;
  10224. this.size = 1;
  10225. this.sizeAttenuation = true;
  10226. this.fog = true;
  10227. this.setValues( parameters );
  10228. }
  10229. copy( source ) {
  10230. super.copy( source );
  10231. this.color.copy( source.color );
  10232. this.map = source.map;
  10233. this.alphaMap = source.alphaMap;
  10234. this.size = source.size;
  10235. this.sizeAttenuation = source.sizeAttenuation;
  10236. this.fog = source.fog;
  10237. return this;
  10238. }
  10239. }
  10240. const _inverseMatrix = /*@__PURE__*/ new Matrix4();
  10241. const _ray = /*@__PURE__*/ new Ray();
  10242. const _sphere = /*@__PURE__*/ new Sphere();
  10243. const _position$2 = /*@__PURE__*/ new Vector3();
  10244. class Points extends Object3D {
  10245. constructor( geometry = new BufferGeometry(), material = new PointsMaterial() ) {
  10246. super();
  10247. this.isPoints = true;
  10248. this.type = 'Points';
  10249. this.geometry = geometry;
  10250. this.material = material;
  10251. this.updateMorphTargets();
  10252. }
  10253. copy( source, recursive ) {
  10254. super.copy( source, recursive );
  10255. this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
  10256. this.geometry = source.geometry;
  10257. return this;
  10258. }
  10259. raycast( raycaster, intersects ) {
  10260. const geometry = this.geometry;
  10261. const matrixWorld = this.matrixWorld;
  10262. const threshold = raycaster.params.Points.threshold;
  10263. const drawRange = geometry.drawRange;
  10264. // Checking boundingSphere distance to ray
  10265. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  10266. _sphere.copy( geometry.boundingSphere );
  10267. _sphere.applyMatrix4( matrixWorld );
  10268. _sphere.radius += threshold;
  10269. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  10270. //
  10271. _inverseMatrix.copy( matrixWorld ).invert();
  10272. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  10273. const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  10274. const localThresholdSq = localThreshold * localThreshold;
  10275. const index = geometry.index;
  10276. const attributes = geometry.attributes;
  10277. const positionAttribute = attributes.position;
  10278. if ( index !== null ) {
  10279. const start = Math.max( 0, drawRange.start );
  10280. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  10281. for ( let i = start, il = end; i < il; i ++ ) {
  10282. const a = index.getX( i );
  10283. _position$2.fromBufferAttribute( positionAttribute, a );
  10284. testPoint( _position$2, a, localThresholdSq, matrixWorld, raycaster, intersects, this );
  10285. }
  10286. } else {
  10287. const start = Math.max( 0, drawRange.start );
  10288. const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
  10289. for ( let i = start, l = end; i < l; i ++ ) {
  10290. _position$2.fromBufferAttribute( positionAttribute, i );
  10291. testPoint( _position$2, i, localThresholdSq, matrixWorld, raycaster, intersects, this );
  10292. }
  10293. }
  10294. }
  10295. updateMorphTargets() {
  10296. const geometry = this.geometry;
  10297. const morphAttributes = geometry.morphAttributes;
  10298. const keys = Object.keys( morphAttributes );
  10299. if ( keys.length > 0 ) {
  10300. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  10301. if ( morphAttribute !== undefined ) {
  10302. this.morphTargetInfluences = [];
  10303. this.morphTargetDictionary = {};
  10304. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  10305. const name = morphAttribute[ m ].name || String( m );
  10306. this.morphTargetInfluences.push( 0 );
  10307. this.morphTargetDictionary[ name ] = m;
  10308. }
  10309. }
  10310. }
  10311. }
  10312. }
  10313. function testPoint( point, index, localThresholdSq, matrixWorld, raycaster, intersects, object ) {
  10314. const rayPointDistanceSq = _ray.distanceSqToPoint( point );
  10315. if ( rayPointDistanceSq < localThresholdSq ) {
  10316. const intersectPoint = new Vector3();
  10317. _ray.closestPointToPoint( point, intersectPoint );
  10318. intersectPoint.applyMatrix4( matrixWorld );
  10319. const distance = raycaster.ray.origin.distanceTo( intersectPoint );
  10320. if ( distance < raycaster.near || distance > raycaster.far ) return;
  10321. intersects.push( {
  10322. distance: distance,
  10323. distanceToRay: Math.sqrt( rayPointDistanceSq ),
  10324. point: intersectPoint,
  10325. index: index,
  10326. face: null,
  10327. faceIndex: null,
  10328. barycoord: null,
  10329. object: object
  10330. } );
  10331. }
  10332. }
  10333. class Group extends Object3D {
  10334. constructor() {
  10335. super();
  10336. this.isGroup = true;
  10337. this.type = 'Group';
  10338. }
  10339. }
  10340. class VideoTexture extends Texture {
  10341. constructor( video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
  10342. super( video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
  10343. this.isVideoTexture = true;
  10344. this.minFilter = minFilter !== undefined ? minFilter : LinearFilter;
  10345. this.magFilter = magFilter !== undefined ? magFilter : LinearFilter;
  10346. this.generateMipmaps = false;
  10347. const scope = this;
  10348. function updateVideo() {
  10349. scope.needsUpdate = true;
  10350. video.requestVideoFrameCallback( updateVideo );
  10351. }
  10352. if ( 'requestVideoFrameCallback' in video ) {
  10353. video.requestVideoFrameCallback( updateVideo );
  10354. }
  10355. }
  10356. clone() {
  10357. return new this.constructor( this.image ).copy( this );
  10358. }
  10359. update() {
  10360. const video = this.image;
  10361. const hasVideoFrameCallback = 'requestVideoFrameCallback' in video;
  10362. if ( hasVideoFrameCallback === false && video.readyState >= video.HAVE_CURRENT_DATA ) {
  10363. this.needsUpdate = true;
  10364. }
  10365. }
  10366. }
  10367. class FramebufferTexture extends Texture {
  10368. constructor( width, height ) {
  10369. super( { width, height } );
  10370. this.isFramebufferTexture = true;
  10371. this.magFilter = NearestFilter;
  10372. this.minFilter = NearestFilter;
  10373. this.generateMipmaps = false;
  10374. this.needsUpdate = true;
  10375. }
  10376. }
  10377. class CompressedTexture extends Texture {
  10378. constructor( mipmaps, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, colorSpace ) {
  10379. super( null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace );
  10380. this.isCompressedTexture = true;
  10381. this.image = { width: width, height: height };
  10382. this.mipmaps = mipmaps;
  10383. // no flipping for cube textures
  10384. // (also flipping doesn't work for compressed textures )
  10385. this.flipY = false;
  10386. // can't generate mipmaps for compressed textures
  10387. // mips must be embedded in DDS files
  10388. this.generateMipmaps = false;
  10389. }
  10390. }
  10391. class CompressedArrayTexture extends CompressedTexture {
  10392. constructor( mipmaps, width, height, depth, format, type ) {
  10393. super( mipmaps, width, height, format, type );
  10394. this.isCompressedArrayTexture = true;
  10395. this.image.depth = depth;
  10396. this.wrapR = ClampToEdgeWrapping;
  10397. this.layerUpdates = new Set();
  10398. }
  10399. addLayerUpdate( layerIndex ) {
  10400. this.layerUpdates.add( layerIndex );
  10401. }
  10402. clearLayerUpdates() {
  10403. this.layerUpdates.clear();
  10404. }
  10405. }
  10406. class CompressedCubeTexture extends CompressedTexture {
  10407. constructor( images, format, type ) {
  10408. super( undefined, images[ 0 ].width, images[ 0 ].height, format, type, CubeReflectionMapping );
  10409. this.isCompressedCubeTexture = true;
  10410. this.isCubeTexture = true;
  10411. this.image = images;
  10412. }
  10413. }
  10414. class CanvasTexture extends Texture {
  10415. constructor( canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
  10416. super( canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
  10417. this.isCanvasTexture = true;
  10418. this.needsUpdate = true;
  10419. }
  10420. }
  10421. class DepthTexture extends Texture {
  10422. constructor( width, height, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, format = DepthFormat ) {
  10423. if ( format !== DepthFormat && format !== DepthStencilFormat ) {
  10424. throw new Error( 'DepthTexture format must be either THREE.DepthFormat or THREE.DepthStencilFormat' );
  10425. }
  10426. if ( type === undefined && format === DepthFormat ) type = UnsignedIntType;
  10427. if ( type === undefined && format === DepthStencilFormat ) type = UnsignedInt248Type;
  10428. super( null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
  10429. this.isDepthTexture = true;
  10430. this.image = { width: width, height: height };
  10431. this.magFilter = magFilter !== undefined ? magFilter : NearestFilter;
  10432. this.minFilter = minFilter !== undefined ? minFilter : NearestFilter;
  10433. this.flipY = false;
  10434. this.generateMipmaps = false;
  10435. this.compareFunction = null;
  10436. }
  10437. copy( source ) {
  10438. super.copy( source );
  10439. this.compareFunction = source.compareFunction;
  10440. return this;
  10441. }
  10442. toJSON( meta ) {
  10443. const data = super.toJSON( meta );
  10444. if ( this.compareFunction !== null ) data.compareFunction = this.compareFunction;
  10445. return data;
  10446. }
  10447. }
  10448. /**
  10449. * Extensible curve object.
  10450. *
  10451. * Some common of curve methods:
  10452. * .getPoint( t, optionalTarget ), .getTangent( t, optionalTarget )
  10453. * .getPointAt( u, optionalTarget ), .getTangentAt( u, optionalTarget )
  10454. * .getPoints(), .getSpacedPoints()
  10455. * .getLength()
  10456. * .updateArcLengths()
  10457. *
  10458. * This following curves inherit from THREE.Curve:
  10459. *
  10460. * -- 2D curves --
  10461. * THREE.ArcCurve
  10462. * THREE.CubicBezierCurve
  10463. * THREE.EllipseCurve
  10464. * THREE.LineCurve
  10465. * THREE.QuadraticBezierCurve
  10466. * THREE.SplineCurve
  10467. *
  10468. * -- 3D curves --
  10469. * THREE.CatmullRomCurve3
  10470. * THREE.CubicBezierCurve3
  10471. * THREE.LineCurve3
  10472. * THREE.QuadraticBezierCurve3
  10473. *
  10474. * A series of curves can be represented as a THREE.CurvePath.
  10475. *
  10476. **/
  10477. class Curve {
  10478. constructor() {
  10479. this.type = 'Curve';
  10480. this.arcLengthDivisions = 200;
  10481. }
  10482. // Virtual base class method to overwrite and implement in subclasses
  10483. // - t [0 .. 1]
  10484. getPoint( /* t, optionalTarget */ ) {
  10485. console.warn( 'THREE.Curve: .getPoint() not implemented.' );
  10486. return null;
  10487. }
  10488. // Get point at relative position in curve according to arc length
  10489. // - u [0 .. 1]
  10490. getPointAt( u, optionalTarget ) {
  10491. const t = this.getUtoTmapping( u );
  10492. return this.getPoint( t, optionalTarget );
  10493. }
  10494. // Get sequence of points using getPoint( t )
  10495. getPoints( divisions = 5 ) {
  10496. const points = [];
  10497. for ( let d = 0; d <= divisions; d ++ ) {
  10498. points.push( this.getPoint( d / divisions ) );
  10499. }
  10500. return points;
  10501. }
  10502. // Get sequence of points using getPointAt( u )
  10503. getSpacedPoints( divisions = 5 ) {
  10504. const points = [];
  10505. for ( let d = 0; d <= divisions; d ++ ) {
  10506. points.push( this.getPointAt( d / divisions ) );
  10507. }
  10508. return points;
  10509. }
  10510. // Get total curve arc length
  10511. getLength() {
  10512. const lengths = this.getLengths();
  10513. return lengths[ lengths.length - 1 ];
  10514. }
  10515. // Get list of cumulative segment lengths
  10516. getLengths( divisions = this.arcLengthDivisions ) {
  10517. if ( this.cacheArcLengths &&
  10518. ( this.cacheArcLengths.length === divisions + 1 ) &&
  10519. ! this.needsUpdate ) {
  10520. return this.cacheArcLengths;
  10521. }
  10522. this.needsUpdate = false;
  10523. const cache = [];
  10524. let current, last = this.getPoint( 0 );
  10525. let sum = 0;
  10526. cache.push( 0 );
  10527. for ( let p = 1; p <= divisions; p ++ ) {
  10528. current = this.getPoint( p / divisions );
  10529. sum += current.distanceTo( last );
  10530. cache.push( sum );
  10531. last = current;
  10532. }
  10533. this.cacheArcLengths = cache;
  10534. return cache; // { sums: cache, sum: sum }; Sum is in the last element.
  10535. }
  10536. updateArcLengths() {
  10537. this.needsUpdate = true;
  10538. this.getLengths();
  10539. }
  10540. // Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant
  10541. getUtoTmapping( u, distance ) {
  10542. const arcLengths = this.getLengths();
  10543. let i = 0;
  10544. const il = arcLengths.length;
  10545. let targetArcLength; // The targeted u distance value to get
  10546. if ( distance ) {
  10547. targetArcLength = distance;
  10548. } else {
  10549. targetArcLength = u * arcLengths[ il - 1 ];
  10550. }
  10551. // binary search for the index with largest value smaller than target u distance
  10552. let low = 0, high = il - 1, comparison;
  10553. while ( low <= high ) {
  10554. i = Math.floor( low + ( high - low ) / 2 ); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats
  10555. comparison = arcLengths[ i ] - targetArcLength;
  10556. if ( comparison < 0 ) {
  10557. low = i + 1;
  10558. } else if ( comparison > 0 ) {
  10559. high = i - 1;
  10560. } else {
  10561. high = i;
  10562. break;
  10563. // DONE
  10564. }
  10565. }
  10566. i = high;
  10567. if ( arcLengths[ i ] === targetArcLength ) {
  10568. return i / ( il - 1 );
  10569. }
  10570. // we could get finer grain at lengths, or use simple interpolation between two points
  10571. const lengthBefore = arcLengths[ i ];
  10572. const lengthAfter = arcLengths[ i + 1 ];
  10573. const segmentLength = lengthAfter - lengthBefore;
  10574. // determine where we are between the 'before' and 'after' points
  10575. const segmentFraction = ( targetArcLength - lengthBefore ) / segmentLength;
  10576. // add that fractional amount to t
  10577. const t = ( i + segmentFraction ) / ( il - 1 );
  10578. return t;
  10579. }
  10580. // Returns a unit vector tangent at t
  10581. // In case any sub curve does not implement its tangent derivation,
  10582. // 2 points a small delta apart will be used to find its gradient
  10583. // which seems to give a reasonable approximation
  10584. getTangent( t, optionalTarget ) {
  10585. const delta = 0.0001;
  10586. let t1 = t - delta;
  10587. let t2 = t + delta;
  10588. // Capping in case of danger
  10589. if ( t1 < 0 ) t1 = 0;
  10590. if ( t2 > 1 ) t2 = 1;
  10591. const pt1 = this.getPoint( t1 );
  10592. const pt2 = this.getPoint( t2 );
  10593. const tangent = optionalTarget || ( ( pt1.isVector2 ) ? new Vector2() : new Vector3() );
  10594. tangent.copy( pt2 ).sub( pt1 ).normalize();
  10595. return tangent;
  10596. }
  10597. getTangentAt( u, optionalTarget ) {
  10598. const t = this.getUtoTmapping( u );
  10599. return this.getTangent( t, optionalTarget );
  10600. }
  10601. computeFrenetFrames( segments, closed ) {
  10602. // see http://www.cs.indiana.edu/pub/techreports/TR425.pdf
  10603. const normal = new Vector3();
  10604. const tangents = [];
  10605. const normals = [];
  10606. const binormals = [];
  10607. const vec = new Vector3();
  10608. const mat = new Matrix4();
  10609. // compute the tangent vectors for each segment on the curve
  10610. for ( let i = 0; i <= segments; i ++ ) {
  10611. const u = i / segments;
  10612. tangents[ i ] = this.getTangentAt( u, new Vector3() );
  10613. }
  10614. // select an initial normal vector perpendicular to the first tangent vector,
  10615. // and in the direction of the minimum tangent xyz component
  10616. normals[ 0 ] = new Vector3();
  10617. binormals[ 0 ] = new Vector3();
  10618. let min = Number.MAX_VALUE;
  10619. const tx = Math.abs( tangents[ 0 ].x );
  10620. const ty = Math.abs( tangents[ 0 ].y );
  10621. const tz = Math.abs( tangents[ 0 ].z );
  10622. if ( tx <= min ) {
  10623. min = tx;
  10624. normal.set( 1, 0, 0 );
  10625. }
  10626. if ( ty <= min ) {
  10627. min = ty;
  10628. normal.set( 0, 1, 0 );
  10629. }
  10630. if ( tz <= min ) {
  10631. normal.set( 0, 0, 1 );
  10632. }
  10633. vec.crossVectors( tangents[ 0 ], normal ).normalize();
  10634. normals[ 0 ].crossVectors( tangents[ 0 ], vec );
  10635. binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] );
  10636. // compute the slowly-varying normal and binormal vectors for each segment on the curve
  10637. for ( let i = 1; i <= segments; i ++ ) {
  10638. normals[ i ] = normals[ i - 1 ].clone();
  10639. binormals[ i ] = binormals[ i - 1 ].clone();
  10640. vec.crossVectors( tangents[ i - 1 ], tangents[ i ] );
  10641. if ( vec.length() > Number.EPSILON ) {
  10642. vec.normalize();
  10643. const theta = Math.acos( clamp( tangents[ i - 1 ].dot( tangents[ i ] ), - 1, 1 ) ); // clamp for floating pt errors
  10644. normals[ i ].applyMatrix4( mat.makeRotationAxis( vec, theta ) );
  10645. }
  10646. binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
  10647. }
  10648. // if the curve is closed, postprocess the vectors so the first and last normal vectors are the same
  10649. if ( closed === true ) {
  10650. let theta = Math.acos( clamp( normals[ 0 ].dot( normals[ segments ] ), - 1, 1 ) );
  10651. theta /= segments;
  10652. if ( tangents[ 0 ].dot( vec.crossVectors( normals[ 0 ], normals[ segments ] ) ) > 0 ) {
  10653. theta = - theta;
  10654. }
  10655. for ( let i = 1; i <= segments; i ++ ) {
  10656. // twist a little...
  10657. normals[ i ].applyMatrix4( mat.makeRotationAxis( tangents[ i ], theta * i ) );
  10658. binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
  10659. }
  10660. }
  10661. return {
  10662. tangents: tangents,
  10663. normals: normals,
  10664. binormals: binormals
  10665. };
  10666. }
  10667. clone() {
  10668. return new this.constructor().copy( this );
  10669. }
  10670. copy( source ) {
  10671. this.arcLengthDivisions = source.arcLengthDivisions;
  10672. return this;
  10673. }
  10674. toJSON() {
  10675. const data = {
  10676. metadata: {
  10677. version: 4.6,
  10678. type: 'Curve',
  10679. generator: 'Curve.toJSON'
  10680. }
  10681. };
  10682. data.arcLengthDivisions = this.arcLengthDivisions;
  10683. data.type = this.type;
  10684. return data;
  10685. }
  10686. fromJSON( json ) {
  10687. this.arcLengthDivisions = json.arcLengthDivisions;
  10688. return this;
  10689. }
  10690. }
  10691. class EllipseCurve extends Curve {
  10692. constructor( aX = 0, aY = 0, xRadius = 1, yRadius = 1, aStartAngle = 0, aEndAngle = Math.PI * 2, aClockwise = false, aRotation = 0 ) {
  10693. super();
  10694. this.isEllipseCurve = true;
  10695. this.type = 'EllipseCurve';
  10696. this.aX = aX;
  10697. this.aY = aY;
  10698. this.xRadius = xRadius;
  10699. this.yRadius = yRadius;
  10700. this.aStartAngle = aStartAngle;
  10701. this.aEndAngle = aEndAngle;
  10702. this.aClockwise = aClockwise;
  10703. this.aRotation = aRotation;
  10704. }
  10705. getPoint( t, optionalTarget = new Vector2() ) {
  10706. const point = optionalTarget;
  10707. const twoPi = Math.PI * 2;
  10708. let deltaAngle = this.aEndAngle - this.aStartAngle;
  10709. const samePoints = Math.abs( deltaAngle ) < Number.EPSILON;
  10710. // ensures that deltaAngle is 0 .. 2 PI
  10711. while ( deltaAngle < 0 ) deltaAngle += twoPi;
  10712. while ( deltaAngle > twoPi ) deltaAngle -= twoPi;
  10713. if ( deltaAngle < Number.EPSILON ) {
  10714. if ( samePoints ) {
  10715. deltaAngle = 0;
  10716. } else {
  10717. deltaAngle = twoPi;
  10718. }
  10719. }
  10720. if ( this.aClockwise === true && ! samePoints ) {
  10721. if ( deltaAngle === twoPi ) {
  10722. deltaAngle = - twoPi;
  10723. } else {
  10724. deltaAngle = deltaAngle - twoPi;
  10725. }
  10726. }
  10727. const angle = this.aStartAngle + t * deltaAngle;
  10728. let x = this.aX + this.xRadius * Math.cos( angle );
  10729. let y = this.aY + this.yRadius * Math.sin( angle );
  10730. if ( this.aRotation !== 0 ) {
  10731. const cos = Math.cos( this.aRotation );
  10732. const sin = Math.sin( this.aRotation );
  10733. const tx = x - this.aX;
  10734. const ty = y - this.aY;
  10735. // Rotate the point about the center of the ellipse.
  10736. x = tx * cos - ty * sin + this.aX;
  10737. y = tx * sin + ty * cos + this.aY;
  10738. }
  10739. return point.set( x, y );
  10740. }
  10741. copy( source ) {
  10742. super.copy( source );
  10743. this.aX = source.aX;
  10744. this.aY = source.aY;
  10745. this.xRadius = source.xRadius;
  10746. this.yRadius = source.yRadius;
  10747. this.aStartAngle = source.aStartAngle;
  10748. this.aEndAngle = source.aEndAngle;
  10749. this.aClockwise = source.aClockwise;
  10750. this.aRotation = source.aRotation;
  10751. return this;
  10752. }
  10753. toJSON() {
  10754. const data = super.toJSON();
  10755. data.aX = this.aX;
  10756. data.aY = this.aY;
  10757. data.xRadius = this.xRadius;
  10758. data.yRadius = this.yRadius;
  10759. data.aStartAngle = this.aStartAngle;
  10760. data.aEndAngle = this.aEndAngle;
  10761. data.aClockwise = this.aClockwise;
  10762. data.aRotation = this.aRotation;
  10763. return data;
  10764. }
  10765. fromJSON( json ) {
  10766. super.fromJSON( json );
  10767. this.aX = json.aX;
  10768. this.aY = json.aY;
  10769. this.xRadius = json.xRadius;
  10770. this.yRadius = json.yRadius;
  10771. this.aStartAngle = json.aStartAngle;
  10772. this.aEndAngle = json.aEndAngle;
  10773. this.aClockwise = json.aClockwise;
  10774. this.aRotation = json.aRotation;
  10775. return this;
  10776. }
  10777. }
  10778. class ArcCurve extends EllipseCurve {
  10779. constructor( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
  10780. super( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
  10781. this.isArcCurve = true;
  10782. this.type = 'ArcCurve';
  10783. }
  10784. }
  10785. /**
  10786. * Centripetal CatmullRom Curve - which is useful for avoiding
  10787. * cusps and self-intersections in non-uniform catmull rom curves.
  10788. * http://www.cemyuksel.com/research/catmullrom_param/catmullrom.pdf
  10789. *
  10790. * curve.type accepts centripetal(default), chordal and catmullrom
  10791. * curve.tension is used for catmullrom which defaults to 0.5
  10792. */
  10793. /*
  10794. Based on an optimized c++ solution in
  10795. - http://stackoverflow.com/questions/9489736/catmull-rom-curve-with-no-cusps-and-no-self-intersections/
  10796. - http://ideone.com/NoEbVM
  10797. This CubicPoly class could be used for reusing some variables and calculations,
  10798. but for three.js curve use, it could be possible inlined and flatten into a single function call
  10799. which can be placed in CurveUtils.
  10800. */
  10801. function CubicPoly() {
  10802. let c0 = 0, c1 = 0, c2 = 0, c3 = 0;
  10803. /*
  10804. * Compute coefficients for a cubic polynomial
  10805. * p(s) = c0 + c1*s + c2*s^2 + c3*s^3
  10806. * such that
  10807. * p(0) = x0, p(1) = x1
  10808. * and
  10809. * p'(0) = t0, p'(1) = t1.
  10810. */
  10811. function init( x0, x1, t0, t1 ) {
  10812. c0 = x0;
  10813. c1 = t0;
  10814. c2 = - 3 * x0 + 3 * x1 - 2 * t0 - t1;
  10815. c3 = 2 * x0 - 2 * x1 + t0 + t1;
  10816. }
  10817. return {
  10818. initCatmullRom: function ( x0, x1, x2, x3, tension ) {
  10819. init( x1, x2, tension * ( x2 - x0 ), tension * ( x3 - x1 ) );
  10820. },
  10821. initNonuniformCatmullRom: function ( x0, x1, x2, x3, dt0, dt1, dt2 ) {
  10822. // compute tangents when parameterized in [t1,t2]
  10823. let t1 = ( x1 - x0 ) / dt0 - ( x2 - x0 ) / ( dt0 + dt1 ) + ( x2 - x1 ) / dt1;
  10824. let t2 = ( x2 - x1 ) / dt1 - ( x3 - x1 ) / ( dt1 + dt2 ) + ( x3 - x2 ) / dt2;
  10825. // rescale tangents for parametrization in [0,1]
  10826. t1 *= dt1;
  10827. t2 *= dt1;
  10828. init( x1, x2, t1, t2 );
  10829. },
  10830. calc: function ( t ) {
  10831. const t2 = t * t;
  10832. const t3 = t2 * t;
  10833. return c0 + c1 * t + c2 * t2 + c3 * t3;
  10834. }
  10835. };
  10836. }
  10837. //
  10838. const tmp = /*@__PURE__*/ new Vector3();
  10839. const px = /*@__PURE__*/ new CubicPoly();
  10840. const py = /*@__PURE__*/ new CubicPoly();
  10841. const pz = /*@__PURE__*/ new CubicPoly();
  10842. class CatmullRomCurve3 extends Curve {
  10843. constructor( points = [], closed = false, curveType = 'centripetal', tension = 0.5 ) {
  10844. super();
  10845. this.isCatmullRomCurve3 = true;
  10846. this.type = 'CatmullRomCurve3';
  10847. this.points = points;
  10848. this.closed = closed;
  10849. this.curveType = curveType;
  10850. this.tension = tension;
  10851. }
  10852. getPoint( t, optionalTarget = new Vector3() ) {
  10853. const point = optionalTarget;
  10854. const points = this.points;
  10855. const l = points.length;
  10856. const p = ( l - ( this.closed ? 0 : 1 ) ) * t;
  10857. let intPoint = Math.floor( p );
  10858. let weight = p - intPoint;
  10859. if ( this.closed ) {
  10860. intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / l ) + 1 ) * l;
  10861. } else if ( weight === 0 && intPoint === l - 1 ) {
  10862. intPoint = l - 2;
  10863. weight = 1;
  10864. }
  10865. let p0, p3; // 4 points (p1 & p2 defined below)
  10866. if ( this.closed || intPoint > 0 ) {
  10867. p0 = points[ ( intPoint - 1 ) % l ];
  10868. } else {
  10869. // extrapolate first point
  10870. tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
  10871. p0 = tmp;
  10872. }
  10873. const p1 = points[ intPoint % l ];
  10874. const p2 = points[ ( intPoint + 1 ) % l ];
  10875. if ( this.closed || intPoint + 2 < l ) {
  10876. p3 = points[ ( intPoint + 2 ) % l ];
  10877. } else {
  10878. // extrapolate last point
  10879. tmp.subVectors( points[ l - 1 ], points[ l - 2 ] ).add( points[ l - 1 ] );
  10880. p3 = tmp;
  10881. }
  10882. if ( this.curveType === 'centripetal' || this.curveType === 'chordal' ) {
  10883. // init Centripetal / Chordal Catmull-Rom
  10884. const pow = this.curveType === 'chordal' ? 0.5 : 0.25;
  10885. let dt0 = Math.pow( p0.distanceToSquared( p1 ), pow );
  10886. let dt1 = Math.pow( p1.distanceToSquared( p2 ), pow );
  10887. let dt2 = Math.pow( p2.distanceToSquared( p3 ), pow );
  10888. // safety check for repeated points
  10889. if ( dt1 < 1e-4 ) dt1 = 1.0;
  10890. if ( dt0 < 1e-4 ) dt0 = dt1;
  10891. if ( dt2 < 1e-4 ) dt2 = dt1;
  10892. px.initNonuniformCatmullRom( p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2 );
  10893. py.initNonuniformCatmullRom( p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2 );
  10894. pz.initNonuniformCatmullRom( p0.z, p1.z, p2.z, p3.z, dt0, dt1, dt2 );
  10895. } else if ( this.curveType === 'catmullrom' ) {
  10896. px.initCatmullRom( p0.x, p1.x, p2.x, p3.x, this.tension );
  10897. py.initCatmullRom( p0.y, p1.y, p2.y, p3.y, this.tension );
  10898. pz.initCatmullRom( p0.z, p1.z, p2.z, p3.z, this.tension );
  10899. }
  10900. point.set(
  10901. px.calc( weight ),
  10902. py.calc( weight ),
  10903. pz.calc( weight )
  10904. );
  10905. return point;
  10906. }
  10907. copy( source ) {
  10908. super.copy( source );
  10909. this.points = [];
  10910. for ( let i = 0, l = source.points.length; i < l; i ++ ) {
  10911. const point = source.points[ i ];
  10912. this.points.push( point.clone() );
  10913. }
  10914. this.closed = source.closed;
  10915. this.curveType = source.curveType;
  10916. this.tension = source.tension;
  10917. return this;
  10918. }
  10919. toJSON() {
  10920. const data = super.toJSON();
  10921. data.points = [];
  10922. for ( let i = 0, l = this.points.length; i < l; i ++ ) {
  10923. const point = this.points[ i ];
  10924. data.points.push( point.toArray() );
  10925. }
  10926. data.closed = this.closed;
  10927. data.curveType = this.curveType;
  10928. data.tension = this.tension;
  10929. return data;
  10930. }
  10931. fromJSON( json ) {
  10932. super.fromJSON( json );
  10933. this.points = [];
  10934. for ( let i = 0, l = json.points.length; i < l; i ++ ) {
  10935. const point = json.points[ i ];
  10936. this.points.push( new Vector3().fromArray( point ) );
  10937. }
  10938. this.closed = json.closed;
  10939. this.curveType = json.curveType;
  10940. this.tension = json.tension;
  10941. return this;
  10942. }
  10943. }
  10944. /**
  10945. * Bezier Curves formulas obtained from
  10946. * https://en.wikipedia.org/wiki/B%C3%A9zier_curve
  10947. */
  10948. function CatmullRom( t, p0, p1, p2, p3 ) {
  10949. const v0 = ( p2 - p0 ) * 0.5;
  10950. const v1 = ( p3 - p1 ) * 0.5;
  10951. const t2 = t * t;
  10952. const t3 = t * t2;
  10953. return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  10954. }
  10955. //
  10956. function QuadraticBezierP0( t, p ) {
  10957. const k = 1 - t;
  10958. return k * k * p;
  10959. }
  10960. function QuadraticBezierP1( t, p ) {
  10961. return 2 * ( 1 - t ) * t * p;
  10962. }
  10963. function QuadraticBezierP2( t, p ) {
  10964. return t * t * p;
  10965. }
  10966. function QuadraticBezier( t, p0, p1, p2 ) {
  10967. return QuadraticBezierP0( t, p0 ) + QuadraticBezierP1( t, p1 ) +
  10968. QuadraticBezierP2( t, p2 );
  10969. }
  10970. //
  10971. function CubicBezierP0( t, p ) {
  10972. const k = 1 - t;
  10973. return k * k * k * p;
  10974. }
  10975. function CubicBezierP1( t, p ) {
  10976. const k = 1 - t;
  10977. return 3 * k * k * t * p;
  10978. }
  10979. function CubicBezierP2( t, p ) {
  10980. return 3 * ( 1 - t ) * t * t * p;
  10981. }
  10982. function CubicBezierP3( t, p ) {
  10983. return t * t * t * p;
  10984. }
  10985. function CubicBezier( t, p0, p1, p2, p3 ) {
  10986. return CubicBezierP0( t, p0 ) + CubicBezierP1( t, p1 ) + CubicBezierP2( t, p2 ) +
  10987. CubicBezierP3( t, p3 );
  10988. }
  10989. class CubicBezierCurve extends Curve {
  10990. constructor( v0 = new Vector2(), v1 = new Vector2(), v2 = new Vector2(), v3 = new Vector2() ) {
  10991. super();
  10992. this.isCubicBezierCurve = true;
  10993. this.type = 'CubicBezierCurve';
  10994. this.v0 = v0;
  10995. this.v1 = v1;
  10996. this.v2 = v2;
  10997. this.v3 = v3;
  10998. }
  10999. getPoint( t, optionalTarget = new Vector2() ) {
  11000. const point = optionalTarget;
  11001. const v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
  11002. point.set(
  11003. CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
  11004. CubicBezier( t, v0.y, v1.y, v2.y, v3.y )
  11005. );
  11006. return point;
  11007. }
  11008. copy( source ) {
  11009. super.copy( source );
  11010. this.v0.copy( source.v0 );
  11011. this.v1.copy( source.v1 );
  11012. this.v2.copy( source.v2 );
  11013. this.v3.copy( source.v3 );
  11014. return this;
  11015. }
  11016. toJSON() {
  11017. const data = super.toJSON();
  11018. data.v0 = this.v0.toArray();
  11019. data.v1 = this.v1.toArray();
  11020. data.v2 = this.v2.toArray();
  11021. data.v3 = this.v3.toArray();
  11022. return data;
  11023. }
  11024. fromJSON( json ) {
  11025. super.fromJSON( json );
  11026. this.v0.fromArray( json.v0 );
  11027. this.v1.fromArray( json.v1 );
  11028. this.v2.fromArray( json.v2 );
  11029. this.v3.fromArray( json.v3 );
  11030. return this;
  11031. }
  11032. }
  11033. class CubicBezierCurve3 extends Curve {
  11034. constructor( v0 = new Vector3(), v1 = new Vector3(), v2 = new Vector3(), v3 = new Vector3() ) {
  11035. super();
  11036. this.isCubicBezierCurve3 = true;
  11037. this.type = 'CubicBezierCurve3';
  11038. this.v0 = v0;
  11039. this.v1 = v1;
  11040. this.v2 = v2;
  11041. this.v3 = v3;
  11042. }
  11043. getPoint( t, optionalTarget = new Vector3() ) {
  11044. const point = optionalTarget;
  11045. const v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
  11046. point.set(
  11047. CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
  11048. CubicBezier( t, v0.y, v1.y, v2.y, v3.y ),
  11049. CubicBezier( t, v0.z, v1.z, v2.z, v3.z )
  11050. );
  11051. return point;
  11052. }
  11053. copy( source ) {
  11054. super.copy( source );
  11055. this.v0.copy( source.v0 );
  11056. this.v1.copy( source.v1 );
  11057. this.v2.copy( source.v2 );
  11058. this.v3.copy( source.v3 );
  11059. return this;
  11060. }
  11061. toJSON() {
  11062. const data = super.toJSON();
  11063. data.v0 = this.v0.toArray();
  11064. data.v1 = this.v1.toArray();
  11065. data.v2 = this.v2.toArray();
  11066. data.v3 = this.v3.toArray();
  11067. return data;
  11068. }
  11069. fromJSON( json ) {
  11070. super.fromJSON( json );
  11071. this.v0.fromArray( json.v0 );
  11072. this.v1.fromArray( json.v1 );
  11073. this.v2.fromArray( json.v2 );
  11074. this.v3.fromArray( json.v3 );
  11075. return this;
  11076. }
  11077. }
  11078. class LineCurve extends Curve {
  11079. constructor( v1 = new Vector2(), v2 = new Vector2() ) {
  11080. super();
  11081. this.isLineCurve = true;
  11082. this.type = 'LineCurve';
  11083. this.v1 = v1;
  11084. this.v2 = v2;
  11085. }
  11086. getPoint( t, optionalTarget = new Vector2() ) {
  11087. const point = optionalTarget;
  11088. if ( t === 1 ) {
  11089. point.copy( this.v2 );
  11090. } else {
  11091. point.copy( this.v2 ).sub( this.v1 );
  11092. point.multiplyScalar( t ).add( this.v1 );
  11093. }
  11094. return point;
  11095. }
  11096. // Line curve is linear, so we can overwrite default getPointAt
  11097. getPointAt( u, optionalTarget ) {
  11098. return this.getPoint( u, optionalTarget );
  11099. }
  11100. getTangent( t, optionalTarget = new Vector2() ) {
  11101. return optionalTarget.subVectors( this.v2, this.v1 ).normalize();
  11102. }
  11103. getTangentAt( u, optionalTarget ) {
  11104. return this.getTangent( u, optionalTarget );
  11105. }
  11106. copy( source ) {
  11107. super.copy( source );
  11108. this.v1.copy( source.v1 );
  11109. this.v2.copy( source.v2 );
  11110. return this;
  11111. }
  11112. toJSON() {
  11113. const data = super.toJSON();
  11114. data.v1 = this.v1.toArray();
  11115. data.v2 = this.v2.toArray();
  11116. return data;
  11117. }
  11118. fromJSON( json ) {
  11119. super.fromJSON( json );
  11120. this.v1.fromArray( json.v1 );
  11121. this.v2.fromArray( json.v2 );
  11122. return this;
  11123. }
  11124. }
  11125. class LineCurve3 extends Curve {
  11126. constructor( v1 = new Vector3(), v2 = new Vector3() ) {
  11127. super();
  11128. this.isLineCurve3 = true;
  11129. this.type = 'LineCurve3';
  11130. this.v1 = v1;
  11131. this.v2 = v2;
  11132. }
  11133. getPoint( t, optionalTarget = new Vector3() ) {
  11134. const point = optionalTarget;
  11135. if ( t === 1 ) {
  11136. point.copy( this.v2 );
  11137. } else {
  11138. point.copy( this.v2 ).sub( this.v1 );
  11139. point.multiplyScalar( t ).add( this.v1 );
  11140. }
  11141. return point;
  11142. }
  11143. // Line curve is linear, so we can overwrite default getPointAt
  11144. getPointAt( u, optionalTarget ) {
  11145. return this.getPoint( u, optionalTarget );
  11146. }
  11147. getTangent( t, optionalTarget = new Vector3() ) {
  11148. return optionalTarget.subVectors( this.v2, this.v1 ).normalize();
  11149. }
  11150. getTangentAt( u, optionalTarget ) {
  11151. return this.getTangent( u, optionalTarget );
  11152. }
  11153. copy( source ) {
  11154. super.copy( source );
  11155. this.v1.copy( source.v1 );
  11156. this.v2.copy( source.v2 );
  11157. return this;
  11158. }
  11159. toJSON() {
  11160. const data = super.toJSON();
  11161. data.v1 = this.v1.toArray();
  11162. data.v2 = this.v2.toArray();
  11163. return data;
  11164. }
  11165. fromJSON( json ) {
  11166. super.fromJSON( json );
  11167. this.v1.fromArray( json.v1 );
  11168. this.v2.fromArray( json.v2 );
  11169. return this;
  11170. }
  11171. }
  11172. class QuadraticBezierCurve extends Curve {
  11173. constructor( v0 = new Vector2(), v1 = new Vector2(), v2 = new Vector2() ) {
  11174. super();
  11175. this.isQuadraticBezierCurve = true;
  11176. this.type = 'QuadraticBezierCurve';
  11177. this.v0 = v0;
  11178. this.v1 = v1;
  11179. this.v2 = v2;
  11180. }
  11181. getPoint( t, optionalTarget = new Vector2() ) {
  11182. const point = optionalTarget;
  11183. const v0 = this.v0, v1 = this.v1, v2 = this.v2;
  11184. point.set(
  11185. QuadraticBezier( t, v0.x, v1.x, v2.x ),
  11186. QuadraticBezier( t, v0.y, v1.y, v2.y )
  11187. );
  11188. return point;
  11189. }
  11190. copy( source ) {
  11191. super.copy( source );
  11192. this.v0.copy( source.v0 );
  11193. this.v1.copy( source.v1 );
  11194. this.v2.copy( source.v2 );
  11195. return this;
  11196. }
  11197. toJSON() {
  11198. const data = super.toJSON();
  11199. data.v0 = this.v0.toArray();
  11200. data.v1 = this.v1.toArray();
  11201. data.v2 = this.v2.toArray();
  11202. return data;
  11203. }
  11204. fromJSON( json ) {
  11205. super.fromJSON( json );
  11206. this.v0.fromArray( json.v0 );
  11207. this.v1.fromArray( json.v1 );
  11208. this.v2.fromArray( json.v2 );
  11209. return this;
  11210. }
  11211. }
  11212. class QuadraticBezierCurve3 extends Curve {
  11213. constructor( v0 = new Vector3(), v1 = new Vector3(), v2 = new Vector3() ) {
  11214. super();
  11215. this.isQuadraticBezierCurve3 = true;
  11216. this.type = 'QuadraticBezierCurve3';
  11217. this.v0 = v0;
  11218. this.v1 = v1;
  11219. this.v2 = v2;
  11220. }
  11221. getPoint( t, optionalTarget = new Vector3() ) {
  11222. const point = optionalTarget;
  11223. const v0 = this.v0, v1 = this.v1, v2 = this.v2;
  11224. point.set(
  11225. QuadraticBezier( t, v0.x, v1.x, v2.x ),
  11226. QuadraticBezier( t, v0.y, v1.y, v2.y ),
  11227. QuadraticBezier( t, v0.z, v1.z, v2.z )
  11228. );
  11229. return point;
  11230. }
  11231. copy( source ) {
  11232. super.copy( source );
  11233. this.v0.copy( source.v0 );
  11234. this.v1.copy( source.v1 );
  11235. this.v2.copy( source.v2 );
  11236. return this;
  11237. }
  11238. toJSON() {
  11239. const data = super.toJSON();
  11240. data.v0 = this.v0.toArray();
  11241. data.v1 = this.v1.toArray();
  11242. data.v2 = this.v2.toArray();
  11243. return data;
  11244. }
  11245. fromJSON( json ) {
  11246. super.fromJSON( json );
  11247. this.v0.fromArray( json.v0 );
  11248. this.v1.fromArray( json.v1 );
  11249. this.v2.fromArray( json.v2 );
  11250. return this;
  11251. }
  11252. }
  11253. class SplineCurve extends Curve {
  11254. constructor( points = [] ) {
  11255. super();
  11256. this.isSplineCurve = true;
  11257. this.type = 'SplineCurve';
  11258. this.points = points;
  11259. }
  11260. getPoint( t, optionalTarget = new Vector2() ) {
  11261. const point = optionalTarget;
  11262. const points = this.points;
  11263. const p = ( points.length - 1 ) * t;
  11264. const intPoint = Math.floor( p );
  11265. const weight = p - intPoint;
  11266. const p0 = points[ intPoint === 0 ? intPoint : intPoint - 1 ];
  11267. const p1 = points[ intPoint ];
  11268. const p2 = points[ intPoint > points.length - 2 ? points.length - 1 : intPoint + 1 ];
  11269. const p3 = points[ intPoint > points.length - 3 ? points.length - 1 : intPoint + 2 ];
  11270. point.set(
  11271. CatmullRom( weight, p0.x, p1.x, p2.x, p3.x ),
  11272. CatmullRom( weight, p0.y, p1.y, p2.y, p3.y )
  11273. );
  11274. return point;
  11275. }
  11276. copy( source ) {
  11277. super.copy( source );
  11278. this.points = [];
  11279. for ( let i = 0, l = source.points.length; i < l; i ++ ) {
  11280. const point = source.points[ i ];
  11281. this.points.push( point.clone() );
  11282. }
  11283. return this;
  11284. }
  11285. toJSON() {
  11286. const data = super.toJSON();
  11287. data.points = [];
  11288. for ( let i = 0, l = this.points.length; i < l; i ++ ) {
  11289. const point = this.points[ i ];
  11290. data.points.push( point.toArray() );
  11291. }
  11292. return data;
  11293. }
  11294. fromJSON( json ) {
  11295. super.fromJSON( json );
  11296. this.points = [];
  11297. for ( let i = 0, l = json.points.length; i < l; i ++ ) {
  11298. const point = json.points[ i ];
  11299. this.points.push( new Vector2().fromArray( point ) );
  11300. }
  11301. return this;
  11302. }
  11303. }
  11304. var Curves = /*#__PURE__*/Object.freeze({
  11305. __proto__: null,
  11306. ArcCurve: ArcCurve,
  11307. CatmullRomCurve3: CatmullRomCurve3,
  11308. CubicBezierCurve: CubicBezierCurve,
  11309. CubicBezierCurve3: CubicBezierCurve3,
  11310. EllipseCurve: EllipseCurve,
  11311. LineCurve: LineCurve,
  11312. LineCurve3: LineCurve3,
  11313. QuadraticBezierCurve: QuadraticBezierCurve,
  11314. QuadraticBezierCurve3: QuadraticBezierCurve3,
  11315. SplineCurve: SplineCurve
  11316. });
  11317. /**************************************************************
  11318. * Curved Path - a curve path is simply a array of connected
  11319. * curves, but retains the api of a curve
  11320. **************************************************************/
  11321. class CurvePath extends Curve {
  11322. constructor() {
  11323. super();
  11324. this.type = 'CurvePath';
  11325. this.curves = [];
  11326. this.autoClose = false; // Automatically closes the path
  11327. }
  11328. add( curve ) {
  11329. this.curves.push( curve );
  11330. }
  11331. closePath() {
  11332. // Add a line curve if start and end of lines are not connected
  11333. const startPoint = this.curves[ 0 ].getPoint( 0 );
  11334. const endPoint = this.curves[ this.curves.length - 1 ].getPoint( 1 );
  11335. if ( ! startPoint.equals( endPoint ) ) {
  11336. const lineType = ( startPoint.isVector2 === true ) ? 'LineCurve' : 'LineCurve3';
  11337. this.curves.push( new Curves[ lineType ]( endPoint, startPoint ) );
  11338. }
  11339. return this;
  11340. }
  11341. // To get accurate point with reference to
  11342. // entire path distance at time t,
  11343. // following has to be done:
  11344. // 1. Length of each sub path have to be known
  11345. // 2. Locate and identify type of curve
  11346. // 3. Get t for the curve
  11347. // 4. Return curve.getPointAt(t')
  11348. getPoint( t, optionalTarget ) {
  11349. const d = t * this.getLength();
  11350. const curveLengths = this.getCurveLengths();
  11351. let i = 0;
  11352. // To think about boundaries points.
  11353. while ( i < curveLengths.length ) {
  11354. if ( curveLengths[ i ] >= d ) {
  11355. const diff = curveLengths[ i ] - d;
  11356. const curve = this.curves[ i ];
  11357. const segmentLength = curve.getLength();
  11358. const u = segmentLength === 0 ? 0 : 1 - diff / segmentLength;
  11359. return curve.getPointAt( u, optionalTarget );
  11360. }
  11361. i ++;
  11362. }
  11363. return null;
  11364. // loop where sum != 0, sum > d , sum+1 <d
  11365. }
  11366. // We cannot use the default THREE.Curve getPoint() with getLength() because in
  11367. // THREE.Curve, getLength() depends on getPoint() but in THREE.CurvePath
  11368. // getPoint() depends on getLength
  11369. getLength() {
  11370. const lens = this.getCurveLengths();
  11371. return lens[ lens.length - 1 ];
  11372. }
  11373. // cacheLengths must be recalculated.
  11374. updateArcLengths() {
  11375. this.needsUpdate = true;
  11376. this.cacheLengths = null;
  11377. this.getCurveLengths();
  11378. }
  11379. // Compute lengths and cache them
  11380. // We cannot overwrite getLengths() because UtoT mapping uses it.
  11381. getCurveLengths() {
  11382. // We use cache values if curves and cache array are same length
  11383. if ( this.cacheLengths && this.cacheLengths.length === this.curves.length ) {
  11384. return this.cacheLengths;
  11385. }
  11386. // Get length of sub-curve
  11387. // Push sums into cached array
  11388. const lengths = [];
  11389. let sums = 0;
  11390. for ( let i = 0, l = this.curves.length; i < l; i ++ ) {
  11391. sums += this.curves[ i ].getLength();
  11392. lengths.push( sums );
  11393. }
  11394. this.cacheLengths = lengths;
  11395. return lengths;
  11396. }
  11397. getSpacedPoints( divisions = 40 ) {
  11398. const points = [];
  11399. for ( let i = 0; i <= divisions; i ++ ) {
  11400. points.push( this.getPoint( i / divisions ) );
  11401. }
  11402. if ( this.autoClose ) {
  11403. points.push( points[ 0 ] );
  11404. }
  11405. return points;
  11406. }
  11407. getPoints( divisions = 12 ) {
  11408. const points = [];
  11409. let last;
  11410. for ( let i = 0, curves = this.curves; i < curves.length; i ++ ) {
  11411. const curve = curves[ i ];
  11412. const resolution = curve.isEllipseCurve ? divisions * 2
  11413. : ( curve.isLineCurve || curve.isLineCurve3 ) ? 1
  11414. : curve.isSplineCurve ? divisions * curve.points.length
  11415. : divisions;
  11416. const pts = curve.getPoints( resolution );
  11417. for ( let j = 0; j < pts.length; j ++ ) {
  11418. const point = pts[ j ];
  11419. if ( last && last.equals( point ) ) continue; // ensures no consecutive points are duplicates
  11420. points.push( point );
  11421. last = point;
  11422. }
  11423. }
  11424. if ( this.autoClose && points.length > 1 && ! points[ points.length - 1 ].equals( points[ 0 ] ) ) {
  11425. points.push( points[ 0 ] );
  11426. }
  11427. return points;
  11428. }
  11429. copy( source ) {
  11430. super.copy( source );
  11431. this.curves = [];
  11432. for ( let i = 0, l = source.curves.length; i < l; i ++ ) {
  11433. const curve = source.curves[ i ];
  11434. this.curves.push( curve.clone() );
  11435. }
  11436. this.autoClose = source.autoClose;
  11437. return this;
  11438. }
  11439. toJSON() {
  11440. const data = super.toJSON();
  11441. data.autoClose = this.autoClose;
  11442. data.curves = [];
  11443. for ( let i = 0, l = this.curves.length; i < l; i ++ ) {
  11444. const curve = this.curves[ i ];
  11445. data.curves.push( curve.toJSON() );
  11446. }
  11447. return data;
  11448. }
  11449. fromJSON( json ) {
  11450. super.fromJSON( json );
  11451. this.autoClose = json.autoClose;
  11452. this.curves = [];
  11453. for ( let i = 0, l = json.curves.length; i < l; i ++ ) {
  11454. const curve = json.curves[ i ];
  11455. this.curves.push( new Curves[ curve.type ]().fromJSON( curve ) );
  11456. }
  11457. return this;
  11458. }
  11459. }
  11460. class Path extends CurvePath {
  11461. constructor( points ) {
  11462. super();
  11463. this.type = 'Path';
  11464. this.currentPoint = new Vector2();
  11465. if ( points ) {
  11466. this.setFromPoints( points );
  11467. }
  11468. }
  11469. setFromPoints( points ) {
  11470. this.moveTo( points[ 0 ].x, points[ 0 ].y );
  11471. for ( let i = 1, l = points.length; i < l; i ++ ) {
  11472. this.lineTo( points[ i ].x, points[ i ].y );
  11473. }
  11474. return this;
  11475. }
  11476. moveTo( x, y ) {
  11477. this.currentPoint.set( x, y ); // TODO consider referencing vectors instead of copying?
  11478. return this;
  11479. }
  11480. lineTo( x, y ) {
  11481. const curve = new LineCurve( this.currentPoint.clone(), new Vector2( x, y ) );
  11482. this.curves.push( curve );
  11483. this.currentPoint.set( x, y );
  11484. return this;
  11485. }
  11486. quadraticCurveTo( aCPx, aCPy, aX, aY ) {
  11487. const curve = new QuadraticBezierCurve(
  11488. this.currentPoint.clone(),
  11489. new Vector2( aCPx, aCPy ),
  11490. new Vector2( aX, aY )
  11491. );
  11492. this.curves.push( curve );
  11493. this.currentPoint.set( aX, aY );
  11494. return this;
  11495. }
  11496. bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
  11497. const curve = new CubicBezierCurve(
  11498. this.currentPoint.clone(),
  11499. new Vector2( aCP1x, aCP1y ),
  11500. new Vector2( aCP2x, aCP2y ),
  11501. new Vector2( aX, aY )
  11502. );
  11503. this.curves.push( curve );
  11504. this.currentPoint.set( aX, aY );
  11505. return this;
  11506. }
  11507. splineThru( pts /*Array of Vector*/ ) {
  11508. const npts = [ this.currentPoint.clone() ].concat( pts );
  11509. const curve = new SplineCurve( npts );
  11510. this.curves.push( curve );
  11511. this.currentPoint.copy( pts[ pts.length - 1 ] );
  11512. return this;
  11513. }
  11514. arc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
  11515. const x0 = this.currentPoint.x;
  11516. const y0 = this.currentPoint.y;
  11517. this.absarc( aX + x0, aY + y0, aRadius,
  11518. aStartAngle, aEndAngle, aClockwise );
  11519. return this;
  11520. }
  11521. absarc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
  11522. this.absellipse( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
  11523. return this;
  11524. }
  11525. ellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
  11526. const x0 = this.currentPoint.x;
  11527. const y0 = this.currentPoint.y;
  11528. this.absellipse( aX + x0, aY + y0, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
  11529. return this;
  11530. }
  11531. absellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
  11532. const curve = new EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
  11533. if ( this.curves.length > 0 ) {
  11534. // if a previous curve is present, attempt to join
  11535. const firstPoint = curve.getPoint( 0 );
  11536. if ( ! firstPoint.equals( this.currentPoint ) ) {
  11537. this.lineTo( firstPoint.x, firstPoint.y );
  11538. }
  11539. }
  11540. this.curves.push( curve );
  11541. const lastPoint = curve.getPoint( 1 );
  11542. this.currentPoint.copy( lastPoint );
  11543. return this;
  11544. }
  11545. copy( source ) {
  11546. super.copy( source );
  11547. this.currentPoint.copy( source.currentPoint );
  11548. return this;
  11549. }
  11550. toJSON() {
  11551. const data = super.toJSON();
  11552. data.currentPoint = this.currentPoint.toArray();
  11553. return data;
  11554. }
  11555. fromJSON( json ) {
  11556. super.fromJSON( json );
  11557. this.currentPoint.fromArray( json.currentPoint );
  11558. return this;
  11559. }
  11560. }
  11561. class LatheGeometry extends BufferGeometry {
  11562. constructor( points = [ new Vector2( 0, - 0.5 ), new Vector2( 0.5, 0 ), new Vector2( 0, 0.5 ) ], segments = 12, phiStart = 0, phiLength = Math.PI * 2 ) {
  11563. super();
  11564. this.type = 'LatheGeometry';
  11565. this.parameters = {
  11566. points: points,
  11567. segments: segments,
  11568. phiStart: phiStart,
  11569. phiLength: phiLength
  11570. };
  11571. segments = Math.floor( segments );
  11572. // clamp phiLength so it's in range of [ 0, 2PI ]
  11573. phiLength = clamp( phiLength, 0, Math.PI * 2 );
  11574. // buffers
  11575. const indices = [];
  11576. const vertices = [];
  11577. const uvs = [];
  11578. const initNormals = [];
  11579. const normals = [];
  11580. // helper variables
  11581. const inverseSegments = 1.0 / segments;
  11582. const vertex = new Vector3();
  11583. const uv = new Vector2();
  11584. const normal = new Vector3();
  11585. const curNormal = new Vector3();
  11586. const prevNormal = new Vector3();
  11587. let dx = 0;
  11588. let dy = 0;
  11589. // pre-compute normals for initial "meridian"
  11590. for ( let j = 0; j <= ( points.length - 1 ); j ++ ) {
  11591. switch ( j ) {
  11592. case 0: // special handling for 1st vertex on path
  11593. dx = points[ j + 1 ].x - points[ j ].x;
  11594. dy = points[ j + 1 ].y - points[ j ].y;
  11595. normal.x = dy * 1.0;
  11596. normal.y = - dx;
  11597. normal.z = dy * 0.0;
  11598. prevNormal.copy( normal );
  11599. normal.normalize();
  11600. initNormals.push( normal.x, normal.y, normal.z );
  11601. break;
  11602. case ( points.length - 1 ): // special handling for last Vertex on path
  11603. initNormals.push( prevNormal.x, prevNormal.y, prevNormal.z );
  11604. break;
  11605. default: // default handling for all vertices in between
  11606. dx = points[ j + 1 ].x - points[ j ].x;
  11607. dy = points[ j + 1 ].y - points[ j ].y;
  11608. normal.x = dy * 1.0;
  11609. normal.y = - dx;
  11610. normal.z = dy * 0.0;
  11611. curNormal.copy( normal );
  11612. normal.x += prevNormal.x;
  11613. normal.y += prevNormal.y;
  11614. normal.z += prevNormal.z;
  11615. normal.normalize();
  11616. initNormals.push( normal.x, normal.y, normal.z );
  11617. prevNormal.copy( curNormal );
  11618. }
  11619. }
  11620. // generate vertices, uvs and normals
  11621. for ( let i = 0; i <= segments; i ++ ) {
  11622. const phi = phiStart + i * inverseSegments * phiLength;
  11623. const sin = Math.sin( phi );
  11624. const cos = Math.cos( phi );
  11625. for ( let j = 0; j <= ( points.length - 1 ); j ++ ) {
  11626. // vertex
  11627. vertex.x = points[ j ].x * sin;
  11628. vertex.y = points[ j ].y;
  11629. vertex.z = points[ j ].x * cos;
  11630. vertices.push( vertex.x, vertex.y, vertex.z );
  11631. // uv
  11632. uv.x = i / segments;
  11633. uv.y = j / ( points.length - 1 );
  11634. uvs.push( uv.x, uv.y );
  11635. // normal
  11636. const x = initNormals[ 3 * j + 0 ] * sin;
  11637. const y = initNormals[ 3 * j + 1 ];
  11638. const z = initNormals[ 3 * j + 0 ] * cos;
  11639. normals.push( x, y, z );
  11640. }
  11641. }
  11642. // indices
  11643. for ( let i = 0; i < segments; i ++ ) {
  11644. for ( let j = 0; j < ( points.length - 1 ); j ++ ) {
  11645. const base = j + i * points.length;
  11646. const a = base;
  11647. const b = base + points.length;
  11648. const c = base + points.length + 1;
  11649. const d = base + 1;
  11650. // faces
  11651. indices.push( a, b, d );
  11652. indices.push( c, d, b );
  11653. }
  11654. }
  11655. // build geometry
  11656. this.setIndex( indices );
  11657. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  11658. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  11659. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  11660. }
  11661. copy( source ) {
  11662. super.copy( source );
  11663. this.parameters = Object.assign( {}, source.parameters );
  11664. return this;
  11665. }
  11666. static fromJSON( data ) {
  11667. return new LatheGeometry( data.points, data.segments, data.phiStart, data.phiLength );
  11668. }
  11669. }
  11670. class CapsuleGeometry extends LatheGeometry {
  11671. constructor( radius = 1, length = 1, capSegments = 4, radialSegments = 8 ) {
  11672. const path = new Path();
  11673. path.absarc( 0, - length / 2, radius, Math.PI * 1.5, 0 );
  11674. path.absarc( 0, length / 2, radius, 0, Math.PI * 0.5 );
  11675. super( path.getPoints( capSegments ), radialSegments );
  11676. this.type = 'CapsuleGeometry';
  11677. this.parameters = {
  11678. radius: radius,
  11679. length: length,
  11680. capSegments: capSegments,
  11681. radialSegments: radialSegments,
  11682. };
  11683. }
  11684. static fromJSON( data ) {
  11685. return new CapsuleGeometry( data.radius, data.length, data.capSegments, data.radialSegments );
  11686. }
  11687. }
  11688. class CircleGeometry extends BufferGeometry {
  11689. constructor( radius = 1, segments = 32, thetaStart = 0, thetaLength = Math.PI * 2 ) {
  11690. super();
  11691. this.type = 'CircleGeometry';
  11692. this.parameters = {
  11693. radius: radius,
  11694. segments: segments,
  11695. thetaStart: thetaStart,
  11696. thetaLength: thetaLength
  11697. };
  11698. segments = Math.max( 3, segments );
  11699. // buffers
  11700. const indices = [];
  11701. const vertices = [];
  11702. const normals = [];
  11703. const uvs = [];
  11704. // helper variables
  11705. const vertex = new Vector3();
  11706. const uv = new Vector2();
  11707. // center point
  11708. vertices.push( 0, 0, 0 );
  11709. normals.push( 0, 0, 1 );
  11710. uvs.push( 0.5, 0.5 );
  11711. for ( let s = 0, i = 3; s <= segments; s ++, i += 3 ) {
  11712. const segment = thetaStart + s / segments * thetaLength;
  11713. // vertex
  11714. vertex.x = radius * Math.cos( segment );
  11715. vertex.y = radius * Math.sin( segment );
  11716. vertices.push( vertex.x, vertex.y, vertex.z );
  11717. // normal
  11718. normals.push( 0, 0, 1 );
  11719. // uvs
  11720. uv.x = ( vertices[ i ] / radius + 1 ) / 2;
  11721. uv.y = ( vertices[ i + 1 ] / radius + 1 ) / 2;
  11722. uvs.push( uv.x, uv.y );
  11723. }
  11724. // indices
  11725. for ( let i = 1; i <= segments; i ++ ) {
  11726. indices.push( i, i + 1, 0 );
  11727. }
  11728. // build geometry
  11729. this.setIndex( indices );
  11730. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  11731. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  11732. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  11733. }
  11734. copy( source ) {
  11735. super.copy( source );
  11736. this.parameters = Object.assign( {}, source.parameters );
  11737. return this;
  11738. }
  11739. static fromJSON( data ) {
  11740. return new CircleGeometry( data.radius, data.segments, data.thetaStart, data.thetaLength );
  11741. }
  11742. }
  11743. class CylinderGeometry extends BufferGeometry {
  11744. constructor( radiusTop = 1, radiusBottom = 1, height = 1, radialSegments = 32, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 ) {
  11745. super();
  11746. this.type = 'CylinderGeometry';
  11747. this.parameters = {
  11748. radiusTop: radiusTop,
  11749. radiusBottom: radiusBottom,
  11750. height: height,
  11751. radialSegments: radialSegments,
  11752. heightSegments: heightSegments,
  11753. openEnded: openEnded,
  11754. thetaStart: thetaStart,
  11755. thetaLength: thetaLength
  11756. };
  11757. const scope = this;
  11758. radialSegments = Math.floor( radialSegments );
  11759. heightSegments = Math.floor( heightSegments );
  11760. // buffers
  11761. const indices = [];
  11762. const vertices = [];
  11763. const normals = [];
  11764. const uvs = [];
  11765. // helper variables
  11766. let index = 0;
  11767. const indexArray = [];
  11768. const halfHeight = height / 2;
  11769. let groupStart = 0;
  11770. // generate geometry
  11771. generateTorso();
  11772. if ( openEnded === false ) {
  11773. if ( radiusTop > 0 ) generateCap( true );
  11774. if ( radiusBottom > 0 ) generateCap( false );
  11775. }
  11776. // build geometry
  11777. this.setIndex( indices );
  11778. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  11779. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  11780. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  11781. function generateTorso() {
  11782. const normal = new Vector3();
  11783. const vertex = new Vector3();
  11784. let groupCount = 0;
  11785. // this will be used to calculate the normal
  11786. const slope = ( radiusBottom - radiusTop ) / height;
  11787. // generate vertices, normals and uvs
  11788. for ( let y = 0; y <= heightSegments; y ++ ) {
  11789. const indexRow = [];
  11790. const v = y / heightSegments;
  11791. // calculate the radius of the current row
  11792. const radius = v * ( radiusBottom - radiusTop ) + radiusTop;
  11793. for ( let x = 0; x <= radialSegments; x ++ ) {
  11794. const u = x / radialSegments;
  11795. const theta = u * thetaLength + thetaStart;
  11796. const sinTheta = Math.sin( theta );
  11797. const cosTheta = Math.cos( theta );
  11798. // vertex
  11799. vertex.x = radius * sinTheta;
  11800. vertex.y = - v * height + halfHeight;
  11801. vertex.z = radius * cosTheta;
  11802. vertices.push( vertex.x, vertex.y, vertex.z );
  11803. // normal
  11804. normal.set( sinTheta, slope, cosTheta ).normalize();
  11805. normals.push( normal.x, normal.y, normal.z );
  11806. // uv
  11807. uvs.push( u, 1 - v );
  11808. // save index of vertex in respective row
  11809. indexRow.push( index ++ );
  11810. }
  11811. // now save vertices of the row in our index array
  11812. indexArray.push( indexRow );
  11813. }
  11814. // generate indices
  11815. for ( let x = 0; x < radialSegments; x ++ ) {
  11816. for ( let y = 0; y < heightSegments; y ++ ) {
  11817. // we use the index array to access the correct indices
  11818. const a = indexArray[ y ][ x ];
  11819. const b = indexArray[ y + 1 ][ x ];
  11820. const c = indexArray[ y + 1 ][ x + 1 ];
  11821. const d = indexArray[ y ][ x + 1 ];
  11822. // faces
  11823. if ( radiusTop > 0 || y !== 0 ) {
  11824. indices.push( a, b, d );
  11825. groupCount += 3;
  11826. }
  11827. if ( radiusBottom > 0 || y !== heightSegments - 1 ) {
  11828. indices.push( b, c, d );
  11829. groupCount += 3;
  11830. }
  11831. }
  11832. }
  11833. // add a group to the geometry. this will ensure multi material support
  11834. scope.addGroup( groupStart, groupCount, 0 );
  11835. // calculate new start value for groups
  11836. groupStart += groupCount;
  11837. }
  11838. function generateCap( top ) {
  11839. // save the index of the first center vertex
  11840. const centerIndexStart = index;
  11841. const uv = new Vector2();
  11842. const vertex = new Vector3();
  11843. let groupCount = 0;
  11844. const radius = ( top === true ) ? radiusTop : radiusBottom;
  11845. const sign = ( top === true ) ? 1 : - 1;
  11846. // first we generate the center vertex data of the cap.
  11847. // because the geometry needs one set of uvs per face,
  11848. // we must generate a center vertex per face/segment
  11849. for ( let x = 1; x <= radialSegments; x ++ ) {
  11850. // vertex
  11851. vertices.push( 0, halfHeight * sign, 0 );
  11852. // normal
  11853. normals.push( 0, sign, 0 );
  11854. // uv
  11855. uvs.push( 0.5, 0.5 );
  11856. // increase index
  11857. index ++;
  11858. }
  11859. // save the index of the last center vertex
  11860. const centerIndexEnd = index;
  11861. // now we generate the surrounding vertices, normals and uvs
  11862. for ( let x = 0; x <= radialSegments; x ++ ) {
  11863. const u = x / radialSegments;
  11864. const theta = u * thetaLength + thetaStart;
  11865. const cosTheta = Math.cos( theta );
  11866. const sinTheta = Math.sin( theta );
  11867. // vertex
  11868. vertex.x = radius * sinTheta;
  11869. vertex.y = halfHeight * sign;
  11870. vertex.z = radius * cosTheta;
  11871. vertices.push( vertex.x, vertex.y, vertex.z );
  11872. // normal
  11873. normals.push( 0, sign, 0 );
  11874. // uv
  11875. uv.x = ( cosTheta * 0.5 ) + 0.5;
  11876. uv.y = ( sinTheta * 0.5 * sign ) + 0.5;
  11877. uvs.push( uv.x, uv.y );
  11878. // increase index
  11879. index ++;
  11880. }
  11881. // generate indices
  11882. for ( let x = 0; x < radialSegments; x ++ ) {
  11883. const c = centerIndexStart + x;
  11884. const i = centerIndexEnd + x;
  11885. if ( top === true ) {
  11886. // face top
  11887. indices.push( i, i + 1, c );
  11888. } else {
  11889. // face bottom
  11890. indices.push( i + 1, i, c );
  11891. }
  11892. groupCount += 3;
  11893. }
  11894. // add a group to the geometry. this will ensure multi material support
  11895. scope.addGroup( groupStart, groupCount, top === true ? 1 : 2 );
  11896. // calculate new start value for groups
  11897. groupStart += groupCount;
  11898. }
  11899. }
  11900. copy( source ) {
  11901. super.copy( source );
  11902. this.parameters = Object.assign( {}, source.parameters );
  11903. return this;
  11904. }
  11905. static fromJSON( data ) {
  11906. return new CylinderGeometry( data.radiusTop, data.radiusBottom, data.height, data.radialSegments, data.heightSegments, data.openEnded, data.thetaStart, data.thetaLength );
  11907. }
  11908. }
  11909. class ConeGeometry extends CylinderGeometry {
  11910. constructor( radius = 1, height = 1, radialSegments = 32, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 ) {
  11911. super( 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength );
  11912. this.type = 'ConeGeometry';
  11913. this.parameters = {
  11914. radius: radius,
  11915. height: height,
  11916. radialSegments: radialSegments,
  11917. heightSegments: heightSegments,
  11918. openEnded: openEnded,
  11919. thetaStart: thetaStart,
  11920. thetaLength: thetaLength
  11921. };
  11922. }
  11923. static fromJSON( data ) {
  11924. return new ConeGeometry( data.radius, data.height, data.radialSegments, data.heightSegments, data.openEnded, data.thetaStart, data.thetaLength );
  11925. }
  11926. }
  11927. class PolyhedronGeometry extends BufferGeometry {
  11928. constructor( vertices = [], indices = [], radius = 1, detail = 0 ) {
  11929. super();
  11930. this.type = 'PolyhedronGeometry';
  11931. this.parameters = {
  11932. vertices: vertices,
  11933. indices: indices,
  11934. radius: radius,
  11935. detail: detail
  11936. };
  11937. // default buffer data
  11938. const vertexBuffer = [];
  11939. const uvBuffer = [];
  11940. // the subdivision creates the vertex buffer data
  11941. subdivide( detail );
  11942. // all vertices should lie on a conceptual sphere with a given radius
  11943. applyRadius( radius );
  11944. // finally, create the uv data
  11945. generateUVs();
  11946. // build non-indexed geometry
  11947. this.setAttribute( 'position', new Float32BufferAttribute( vertexBuffer, 3 ) );
  11948. this.setAttribute( 'normal', new Float32BufferAttribute( vertexBuffer.slice(), 3 ) );
  11949. this.setAttribute( 'uv', new Float32BufferAttribute( uvBuffer, 2 ) );
  11950. if ( detail === 0 ) {
  11951. this.computeVertexNormals(); // flat normals
  11952. } else {
  11953. this.normalizeNormals(); // smooth normals
  11954. }
  11955. // helper functions
  11956. function subdivide( detail ) {
  11957. const a = new Vector3();
  11958. const b = new Vector3();
  11959. const c = new Vector3();
  11960. // iterate over all faces and apply a subdivision with the given detail value
  11961. for ( let i = 0; i < indices.length; i += 3 ) {
  11962. // get the vertices of the face
  11963. getVertexByIndex( indices[ i + 0 ], a );
  11964. getVertexByIndex( indices[ i + 1 ], b );
  11965. getVertexByIndex( indices[ i + 2 ], c );
  11966. // perform subdivision
  11967. subdivideFace( a, b, c, detail );
  11968. }
  11969. }
  11970. function subdivideFace( a, b, c, detail ) {
  11971. const cols = detail + 1;
  11972. // we use this multidimensional array as a data structure for creating the subdivision
  11973. const v = [];
  11974. // construct all of the vertices for this subdivision
  11975. for ( let i = 0; i <= cols; i ++ ) {
  11976. v[ i ] = [];
  11977. const aj = a.clone().lerp( c, i / cols );
  11978. const bj = b.clone().lerp( c, i / cols );
  11979. const rows = cols - i;
  11980. for ( let j = 0; j <= rows; j ++ ) {
  11981. if ( j === 0 && i === cols ) {
  11982. v[ i ][ j ] = aj;
  11983. } else {
  11984. v[ i ][ j ] = aj.clone().lerp( bj, j / rows );
  11985. }
  11986. }
  11987. }
  11988. // construct all of the faces
  11989. for ( let i = 0; i < cols; i ++ ) {
  11990. for ( let j = 0; j < 2 * ( cols - i ) - 1; j ++ ) {
  11991. const k = Math.floor( j / 2 );
  11992. if ( j % 2 === 0 ) {
  11993. pushVertex( v[ i ][ k + 1 ] );
  11994. pushVertex( v[ i + 1 ][ k ] );
  11995. pushVertex( v[ i ][ k ] );
  11996. } else {
  11997. pushVertex( v[ i ][ k + 1 ] );
  11998. pushVertex( v[ i + 1 ][ k + 1 ] );
  11999. pushVertex( v[ i + 1 ][ k ] );
  12000. }
  12001. }
  12002. }
  12003. }
  12004. function applyRadius( radius ) {
  12005. const vertex = new Vector3();
  12006. // iterate over the entire buffer and apply the radius to each vertex
  12007. for ( let i = 0; i < vertexBuffer.length; i += 3 ) {
  12008. vertex.x = vertexBuffer[ i + 0 ];
  12009. vertex.y = vertexBuffer[ i + 1 ];
  12010. vertex.z = vertexBuffer[ i + 2 ];
  12011. vertex.normalize().multiplyScalar( radius );
  12012. vertexBuffer[ i + 0 ] = vertex.x;
  12013. vertexBuffer[ i + 1 ] = vertex.y;
  12014. vertexBuffer[ i + 2 ] = vertex.z;
  12015. }
  12016. }
  12017. function generateUVs() {
  12018. const vertex = new Vector3();
  12019. for ( let i = 0; i < vertexBuffer.length; i += 3 ) {
  12020. vertex.x = vertexBuffer[ i + 0 ];
  12021. vertex.y = vertexBuffer[ i + 1 ];
  12022. vertex.z = vertexBuffer[ i + 2 ];
  12023. const u = azimuth( vertex ) / 2 / Math.PI + 0.5;
  12024. const v = inclination( vertex ) / Math.PI + 0.5;
  12025. uvBuffer.push( u, 1 - v );
  12026. }
  12027. correctUVs();
  12028. correctSeam();
  12029. }
  12030. function correctSeam() {
  12031. // handle case when face straddles the seam, see #3269
  12032. for ( let i = 0; i < uvBuffer.length; i += 6 ) {
  12033. // uv data of a single face
  12034. const x0 = uvBuffer[ i + 0 ];
  12035. const x1 = uvBuffer[ i + 2 ];
  12036. const x2 = uvBuffer[ i + 4 ];
  12037. const max = Math.max( x0, x1, x2 );
  12038. const min = Math.min( x0, x1, x2 );
  12039. // 0.9 is somewhat arbitrary
  12040. if ( max > 0.9 && min < 0.1 ) {
  12041. if ( x0 < 0.2 ) uvBuffer[ i + 0 ] += 1;
  12042. if ( x1 < 0.2 ) uvBuffer[ i + 2 ] += 1;
  12043. if ( x2 < 0.2 ) uvBuffer[ i + 4 ] += 1;
  12044. }
  12045. }
  12046. }
  12047. function pushVertex( vertex ) {
  12048. vertexBuffer.push( vertex.x, vertex.y, vertex.z );
  12049. }
  12050. function getVertexByIndex( index, vertex ) {
  12051. const stride = index * 3;
  12052. vertex.x = vertices[ stride + 0 ];
  12053. vertex.y = vertices[ stride + 1 ];
  12054. vertex.z = vertices[ stride + 2 ];
  12055. }
  12056. function correctUVs() {
  12057. const a = new Vector3();
  12058. const b = new Vector3();
  12059. const c = new Vector3();
  12060. const centroid = new Vector3();
  12061. const uvA = new Vector2();
  12062. const uvB = new Vector2();
  12063. const uvC = new Vector2();
  12064. for ( let i = 0, j = 0; i < vertexBuffer.length; i += 9, j += 6 ) {
  12065. a.set( vertexBuffer[ i + 0 ], vertexBuffer[ i + 1 ], vertexBuffer[ i + 2 ] );
  12066. b.set( vertexBuffer[ i + 3 ], vertexBuffer[ i + 4 ], vertexBuffer[ i + 5 ] );
  12067. c.set( vertexBuffer[ i + 6 ], vertexBuffer[ i + 7 ], vertexBuffer[ i + 8 ] );
  12068. uvA.set( uvBuffer[ j + 0 ], uvBuffer[ j + 1 ] );
  12069. uvB.set( uvBuffer[ j + 2 ], uvBuffer[ j + 3 ] );
  12070. uvC.set( uvBuffer[ j + 4 ], uvBuffer[ j + 5 ] );
  12071. centroid.copy( a ).add( b ).add( c ).divideScalar( 3 );
  12072. const azi = azimuth( centroid );
  12073. correctUV( uvA, j + 0, a, azi );
  12074. correctUV( uvB, j + 2, b, azi );
  12075. correctUV( uvC, j + 4, c, azi );
  12076. }
  12077. }
  12078. function correctUV( uv, stride, vector, azimuth ) {
  12079. if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) {
  12080. uvBuffer[ stride ] = uv.x - 1;
  12081. }
  12082. if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) {
  12083. uvBuffer[ stride ] = azimuth / 2 / Math.PI + 0.5;
  12084. }
  12085. }
  12086. // Angle around the Y axis, counter-clockwise when looking from above.
  12087. function azimuth( vector ) {
  12088. return Math.atan2( vector.z, - vector.x );
  12089. }
  12090. // Angle above the XZ plane.
  12091. function inclination( vector ) {
  12092. return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) );
  12093. }
  12094. }
  12095. copy( source ) {
  12096. super.copy( source );
  12097. this.parameters = Object.assign( {}, source.parameters );
  12098. return this;
  12099. }
  12100. static fromJSON( data ) {
  12101. return new PolyhedronGeometry( data.vertices, data.indices, data.radius, data.details );
  12102. }
  12103. }
  12104. class DodecahedronGeometry extends PolyhedronGeometry {
  12105. constructor( radius = 1, detail = 0 ) {
  12106. const t = ( 1 + Math.sqrt( 5 ) ) / 2;
  12107. const r = 1 / t;
  12108. const vertices = [
  12109. // (±1, ±1, ±1)
  12110. - 1, - 1, - 1, - 1, - 1, 1,
  12111. - 1, 1, - 1, - 1, 1, 1,
  12112. 1, - 1, - 1, 1, - 1, 1,
  12113. 1, 1, - 1, 1, 1, 1,
  12114. // (0, ±1/φ, ±φ)
  12115. 0, - r, - t, 0, - r, t,
  12116. 0, r, - t, 0, r, t,
  12117. // (±1/φ, ±φ, 0)
  12118. - r, - t, 0, - r, t, 0,
  12119. r, - t, 0, r, t, 0,
  12120. // (±φ, 0, ±1/φ)
  12121. - t, 0, - r, t, 0, - r,
  12122. - t, 0, r, t, 0, r
  12123. ];
  12124. const indices = [
  12125. 3, 11, 7, 3, 7, 15, 3, 15, 13,
  12126. 7, 19, 17, 7, 17, 6, 7, 6, 15,
  12127. 17, 4, 8, 17, 8, 10, 17, 10, 6,
  12128. 8, 0, 16, 8, 16, 2, 8, 2, 10,
  12129. 0, 12, 1, 0, 1, 18, 0, 18, 16,
  12130. 6, 10, 2, 6, 2, 13, 6, 13, 15,
  12131. 2, 16, 18, 2, 18, 3, 2, 3, 13,
  12132. 18, 1, 9, 18, 9, 11, 18, 11, 3,
  12133. 4, 14, 12, 4, 12, 0, 4, 0, 8,
  12134. 11, 9, 5, 11, 5, 19, 11, 19, 7,
  12135. 19, 5, 14, 19, 14, 4, 19, 4, 17,
  12136. 1, 12, 14, 1, 14, 5, 1, 5, 9
  12137. ];
  12138. super( vertices, indices, radius, detail );
  12139. this.type = 'DodecahedronGeometry';
  12140. this.parameters = {
  12141. radius: radius,
  12142. detail: detail
  12143. };
  12144. }
  12145. static fromJSON( data ) {
  12146. return new DodecahedronGeometry( data.radius, data.detail );
  12147. }
  12148. }
  12149. const _v0 = /*@__PURE__*/ new Vector3();
  12150. const _v1$1 = /*@__PURE__*/ new Vector3();
  12151. const _normal = /*@__PURE__*/ new Vector3();
  12152. const _triangle = /*@__PURE__*/ new Triangle();
  12153. class EdgesGeometry extends BufferGeometry {
  12154. constructor( geometry = null, thresholdAngle = 1 ) {
  12155. super();
  12156. this.type = 'EdgesGeometry';
  12157. this.parameters = {
  12158. geometry: geometry,
  12159. thresholdAngle: thresholdAngle
  12160. };
  12161. if ( geometry !== null ) {
  12162. const precisionPoints = 4;
  12163. const precision = Math.pow( 10, precisionPoints );
  12164. const thresholdDot = Math.cos( DEG2RAD * thresholdAngle );
  12165. const indexAttr = geometry.getIndex();
  12166. const positionAttr = geometry.getAttribute( 'position' );
  12167. const indexCount = indexAttr ? indexAttr.count : positionAttr.count;
  12168. const indexArr = [ 0, 0, 0 ];
  12169. const vertKeys = [ 'a', 'b', 'c' ];
  12170. const hashes = new Array( 3 );
  12171. const edgeData = {};
  12172. const vertices = [];
  12173. for ( let i = 0; i < indexCount; i += 3 ) {
  12174. if ( indexAttr ) {
  12175. indexArr[ 0 ] = indexAttr.getX( i );
  12176. indexArr[ 1 ] = indexAttr.getX( i + 1 );
  12177. indexArr[ 2 ] = indexAttr.getX( i + 2 );
  12178. } else {
  12179. indexArr[ 0 ] = i;
  12180. indexArr[ 1 ] = i + 1;
  12181. indexArr[ 2 ] = i + 2;
  12182. }
  12183. const { a, b, c } = _triangle;
  12184. a.fromBufferAttribute( positionAttr, indexArr[ 0 ] );
  12185. b.fromBufferAttribute( positionAttr, indexArr[ 1 ] );
  12186. c.fromBufferAttribute( positionAttr, indexArr[ 2 ] );
  12187. _triangle.getNormal( _normal );
  12188. // create hashes for the edge from the vertices
  12189. hashes[ 0 ] = `${ Math.round( a.x * precision ) },${ Math.round( a.y * precision ) },${ Math.round( a.z * precision ) }`;
  12190. hashes[ 1 ] = `${ Math.round( b.x * precision ) },${ Math.round( b.y * precision ) },${ Math.round( b.z * precision ) }`;
  12191. hashes[ 2 ] = `${ Math.round( c.x * precision ) },${ Math.round( c.y * precision ) },${ Math.round( c.z * precision ) }`;
  12192. // skip degenerate triangles
  12193. if ( hashes[ 0 ] === hashes[ 1 ] || hashes[ 1 ] === hashes[ 2 ] || hashes[ 2 ] === hashes[ 0 ] ) {
  12194. continue;
  12195. }
  12196. // iterate over every edge
  12197. for ( let j = 0; j < 3; j ++ ) {
  12198. // get the first and next vertex making up the edge
  12199. const jNext = ( j + 1 ) % 3;
  12200. const vecHash0 = hashes[ j ];
  12201. const vecHash1 = hashes[ jNext ];
  12202. const v0 = _triangle[ vertKeys[ j ] ];
  12203. const v1 = _triangle[ vertKeys[ jNext ] ];
  12204. const hash = `${ vecHash0 }_${ vecHash1 }`;
  12205. const reverseHash = `${ vecHash1 }_${ vecHash0 }`;
  12206. if ( reverseHash in edgeData && edgeData[ reverseHash ] ) {
  12207. // if we found a sibling edge add it into the vertex array if
  12208. // it meets the angle threshold and delete the edge from the map.
  12209. if ( _normal.dot( edgeData[ reverseHash ].normal ) <= thresholdDot ) {
  12210. vertices.push( v0.x, v0.y, v0.z );
  12211. vertices.push( v1.x, v1.y, v1.z );
  12212. }
  12213. edgeData[ reverseHash ] = null;
  12214. } else if ( ! ( hash in edgeData ) ) {
  12215. // if we've already got an edge here then skip adding a new one
  12216. edgeData[ hash ] = {
  12217. index0: indexArr[ j ],
  12218. index1: indexArr[ jNext ],
  12219. normal: _normal.clone(),
  12220. };
  12221. }
  12222. }
  12223. }
  12224. // iterate over all remaining, unmatched edges and add them to the vertex array
  12225. for ( const key in edgeData ) {
  12226. if ( edgeData[ key ] ) {
  12227. const { index0, index1 } = edgeData[ key ];
  12228. _v0.fromBufferAttribute( positionAttr, index0 );
  12229. _v1$1.fromBufferAttribute( positionAttr, index1 );
  12230. vertices.push( _v0.x, _v0.y, _v0.z );
  12231. vertices.push( _v1$1.x, _v1$1.y, _v1$1.z );
  12232. }
  12233. }
  12234. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  12235. }
  12236. }
  12237. copy( source ) {
  12238. super.copy( source );
  12239. this.parameters = Object.assign( {}, source.parameters );
  12240. return this;
  12241. }
  12242. }
  12243. class Shape extends Path {
  12244. constructor( points ) {
  12245. super( points );
  12246. this.uuid = generateUUID();
  12247. this.type = 'Shape';
  12248. this.holes = [];
  12249. }
  12250. getPointsHoles( divisions ) {
  12251. const holesPts = [];
  12252. for ( let i = 0, l = this.holes.length; i < l; i ++ ) {
  12253. holesPts[ i ] = this.holes[ i ].getPoints( divisions );
  12254. }
  12255. return holesPts;
  12256. }
  12257. // get points of shape and holes (keypoints based on segments parameter)
  12258. extractPoints( divisions ) {
  12259. return {
  12260. shape: this.getPoints( divisions ),
  12261. holes: this.getPointsHoles( divisions )
  12262. };
  12263. }
  12264. copy( source ) {
  12265. super.copy( source );
  12266. this.holes = [];
  12267. for ( let i = 0, l = source.holes.length; i < l; i ++ ) {
  12268. const hole = source.holes[ i ];
  12269. this.holes.push( hole.clone() );
  12270. }
  12271. return this;
  12272. }
  12273. toJSON() {
  12274. const data = super.toJSON();
  12275. data.uuid = this.uuid;
  12276. data.holes = [];
  12277. for ( let i = 0, l = this.holes.length; i < l; i ++ ) {
  12278. const hole = this.holes[ i ];
  12279. data.holes.push( hole.toJSON() );
  12280. }
  12281. return data;
  12282. }
  12283. fromJSON( json ) {
  12284. super.fromJSON( json );
  12285. this.uuid = json.uuid;
  12286. this.holes = [];
  12287. for ( let i = 0, l = json.holes.length; i < l; i ++ ) {
  12288. const hole = json.holes[ i ];
  12289. this.holes.push( new Path().fromJSON( hole ) );
  12290. }
  12291. return this;
  12292. }
  12293. }
  12294. /**
  12295. * Port from https://github.com/mapbox/earcut (v2.2.4)
  12296. */
  12297. const Earcut = {
  12298. triangulate: function ( data, holeIndices, dim = 2 ) {
  12299. const hasHoles = holeIndices && holeIndices.length;
  12300. const outerLen = hasHoles ? holeIndices[ 0 ] * dim : data.length;
  12301. let outerNode = linkedList( data, 0, outerLen, dim, true );
  12302. const triangles = [];
  12303. if ( ! outerNode || outerNode.next === outerNode.prev ) return triangles;
  12304. let minX, minY, maxX, maxY, x, y, invSize;
  12305. if ( hasHoles ) outerNode = eliminateHoles( data, holeIndices, outerNode, dim );
  12306. // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
  12307. if ( data.length > 80 * dim ) {
  12308. minX = maxX = data[ 0 ];
  12309. minY = maxY = data[ 1 ];
  12310. for ( let i = dim; i < outerLen; i += dim ) {
  12311. x = data[ i ];
  12312. y = data[ i + 1 ];
  12313. if ( x < minX ) minX = x;
  12314. if ( y < minY ) minY = y;
  12315. if ( x > maxX ) maxX = x;
  12316. if ( y > maxY ) maxY = y;
  12317. }
  12318. // minX, minY and invSize are later used to transform coords into integers for z-order calculation
  12319. invSize = Math.max( maxX - minX, maxY - minY );
  12320. invSize = invSize !== 0 ? 32767 / invSize : 0;
  12321. }
  12322. earcutLinked( outerNode, triangles, dim, minX, minY, invSize, 0 );
  12323. return triangles;
  12324. }
  12325. };
  12326. // create a circular doubly linked list from polygon points in the specified winding order
  12327. function linkedList( data, start, end, dim, clockwise ) {
  12328. let i, last;
  12329. if ( clockwise === ( signedArea( data, start, end, dim ) > 0 ) ) {
  12330. for ( i = start; i < end; i += dim ) last = insertNode( i, data[ i ], data[ i + 1 ], last );
  12331. } else {
  12332. for ( i = end - dim; i >= start; i -= dim ) last = insertNode( i, data[ i ], data[ i + 1 ], last );
  12333. }
  12334. if ( last && equals( last, last.next ) ) {
  12335. removeNode( last );
  12336. last = last.next;
  12337. }
  12338. return last;
  12339. }
  12340. // eliminate colinear or duplicate points
  12341. function filterPoints( start, end ) {
  12342. if ( ! start ) return start;
  12343. if ( ! end ) end = start;
  12344. let p = start,
  12345. again;
  12346. do {
  12347. again = false;
  12348. if ( ! p.steiner && ( equals( p, p.next ) || area( p.prev, p, p.next ) === 0 ) ) {
  12349. removeNode( p );
  12350. p = end = p.prev;
  12351. if ( p === p.next ) break;
  12352. again = true;
  12353. } else {
  12354. p = p.next;
  12355. }
  12356. } while ( again || p !== end );
  12357. return end;
  12358. }
  12359. // main ear slicing loop which triangulates a polygon (given as a linked list)
  12360. function earcutLinked( ear, triangles, dim, minX, minY, invSize, pass ) {
  12361. if ( ! ear ) return;
  12362. // interlink polygon nodes in z-order
  12363. if ( ! pass && invSize ) indexCurve( ear, minX, minY, invSize );
  12364. let stop = ear,
  12365. prev, next;
  12366. // iterate through ears, slicing them one by one
  12367. while ( ear.prev !== ear.next ) {
  12368. prev = ear.prev;
  12369. next = ear.next;
  12370. if ( invSize ? isEarHashed( ear, minX, minY, invSize ) : isEar( ear ) ) {
  12371. // cut off the triangle
  12372. triangles.push( prev.i / dim | 0 );
  12373. triangles.push( ear.i / dim | 0 );
  12374. triangles.push( next.i / dim | 0 );
  12375. removeNode( ear );
  12376. // skipping the next vertex leads to less sliver triangles
  12377. ear = next.next;
  12378. stop = next.next;
  12379. continue;
  12380. }
  12381. ear = next;
  12382. // if we looped through the whole remaining polygon and can't find any more ears
  12383. if ( ear === stop ) {
  12384. // try filtering points and slicing again
  12385. if ( ! pass ) {
  12386. earcutLinked( filterPoints( ear ), triangles, dim, minX, minY, invSize, 1 );
  12387. // if this didn't work, try curing all small self-intersections locally
  12388. } else if ( pass === 1 ) {
  12389. ear = cureLocalIntersections( filterPoints( ear ), triangles, dim );
  12390. earcutLinked( ear, triangles, dim, minX, minY, invSize, 2 );
  12391. // as a last resort, try splitting the remaining polygon into two
  12392. } else if ( pass === 2 ) {
  12393. splitEarcut( ear, triangles, dim, minX, minY, invSize );
  12394. }
  12395. break;
  12396. }
  12397. }
  12398. }
  12399. // check whether a polygon node forms a valid ear with adjacent nodes
  12400. function isEar( ear ) {
  12401. const a = ear.prev,
  12402. b = ear,
  12403. c = ear.next;
  12404. if ( area( a, b, c ) >= 0 ) return false; // reflex, can't be an ear
  12405. // now make sure we don't have other points inside the potential ear
  12406. const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
  12407. // triangle bbox; min & max are calculated like this for speed
  12408. const x0 = ax < bx ? ( ax < cx ? ax : cx ) : ( bx < cx ? bx : cx ),
  12409. y0 = ay < by ? ( ay < cy ? ay : cy ) : ( by < cy ? by : cy ),
  12410. x1 = ax > bx ? ( ax > cx ? ax : cx ) : ( bx > cx ? bx : cx ),
  12411. y1 = ay > by ? ( ay > cy ? ay : cy ) : ( by > cy ? by : cy );
  12412. let p = c.next;
  12413. while ( p !== a ) {
  12414. if ( p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 &&
  12415. pointInTriangle( ax, ay, bx, by, cx, cy, p.x, p.y ) &&
  12416. area( p.prev, p, p.next ) >= 0 ) return false;
  12417. p = p.next;
  12418. }
  12419. return true;
  12420. }
  12421. function isEarHashed( ear, minX, minY, invSize ) {
  12422. const a = ear.prev,
  12423. b = ear,
  12424. c = ear.next;
  12425. if ( area( a, b, c ) >= 0 ) return false; // reflex, can't be an ear
  12426. const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
  12427. // triangle bbox; min & max are calculated like this for speed
  12428. const x0 = ax < bx ? ( ax < cx ? ax : cx ) : ( bx < cx ? bx : cx ),
  12429. y0 = ay < by ? ( ay < cy ? ay : cy ) : ( by < cy ? by : cy ),
  12430. x1 = ax > bx ? ( ax > cx ? ax : cx ) : ( bx > cx ? bx : cx ),
  12431. y1 = ay > by ? ( ay > cy ? ay : cy ) : ( by > cy ? by : cy );
  12432. // z-order range for the current triangle bbox;
  12433. const minZ = zOrder( x0, y0, minX, minY, invSize ),
  12434. maxZ = zOrder( x1, y1, minX, minY, invSize );
  12435. let p = ear.prevZ,
  12436. n = ear.nextZ;
  12437. // look for points inside the triangle in both directions
  12438. while ( p && p.z >= minZ && n && n.z <= maxZ ) {
  12439. if ( p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
  12440. pointInTriangle( ax, ay, bx, by, cx, cy, p.x, p.y ) && area( p.prev, p, p.next ) >= 0 ) return false;
  12441. p = p.prevZ;
  12442. if ( n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
  12443. pointInTriangle( ax, ay, bx, by, cx, cy, n.x, n.y ) && area( n.prev, n, n.next ) >= 0 ) return false;
  12444. n = n.nextZ;
  12445. }
  12446. // look for remaining points in decreasing z-order
  12447. while ( p && p.z >= minZ ) {
  12448. if ( p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
  12449. pointInTriangle( ax, ay, bx, by, cx, cy, p.x, p.y ) && area( p.prev, p, p.next ) >= 0 ) return false;
  12450. p = p.prevZ;
  12451. }
  12452. // look for remaining points in increasing z-order
  12453. while ( n && n.z <= maxZ ) {
  12454. if ( n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
  12455. pointInTriangle( ax, ay, bx, by, cx, cy, n.x, n.y ) && area( n.prev, n, n.next ) >= 0 ) return false;
  12456. n = n.nextZ;
  12457. }
  12458. return true;
  12459. }
  12460. // go through all polygon nodes and cure small local self-intersections
  12461. function cureLocalIntersections( start, triangles, dim ) {
  12462. let p = start;
  12463. do {
  12464. const a = p.prev,
  12465. b = p.next.next;
  12466. if ( ! equals( a, b ) && intersects( a, p, p.next, b ) && locallyInside( a, b ) && locallyInside( b, a ) ) {
  12467. triangles.push( a.i / dim | 0 );
  12468. triangles.push( p.i / dim | 0 );
  12469. triangles.push( b.i / dim | 0 );
  12470. // remove two nodes involved
  12471. removeNode( p );
  12472. removeNode( p.next );
  12473. p = start = b;
  12474. }
  12475. p = p.next;
  12476. } while ( p !== start );
  12477. return filterPoints( p );
  12478. }
  12479. // try splitting polygon into two and triangulate them independently
  12480. function splitEarcut( start, triangles, dim, minX, minY, invSize ) {
  12481. // look for a valid diagonal that divides the polygon into two
  12482. let a = start;
  12483. do {
  12484. let b = a.next.next;
  12485. while ( b !== a.prev ) {
  12486. if ( a.i !== b.i && isValidDiagonal( a, b ) ) {
  12487. // split the polygon in two by the diagonal
  12488. let c = splitPolygon( a, b );
  12489. // filter colinear points around the cuts
  12490. a = filterPoints( a, a.next );
  12491. c = filterPoints( c, c.next );
  12492. // run earcut on each half
  12493. earcutLinked( a, triangles, dim, minX, minY, invSize, 0 );
  12494. earcutLinked( c, triangles, dim, minX, minY, invSize, 0 );
  12495. return;
  12496. }
  12497. b = b.next;
  12498. }
  12499. a = a.next;
  12500. } while ( a !== start );
  12501. }
  12502. // link every hole into the outer loop, producing a single-ring polygon without holes
  12503. function eliminateHoles( data, holeIndices, outerNode, dim ) {
  12504. const queue = [];
  12505. let i, len, start, end, list;
  12506. for ( i = 0, len = holeIndices.length; i < len; i ++ ) {
  12507. start = holeIndices[ i ] * dim;
  12508. end = i < len - 1 ? holeIndices[ i + 1 ] * dim : data.length;
  12509. list = linkedList( data, start, end, dim, false );
  12510. if ( list === list.next ) list.steiner = true;
  12511. queue.push( getLeftmost( list ) );
  12512. }
  12513. queue.sort( compareX );
  12514. // process holes from left to right
  12515. for ( i = 0; i < queue.length; i ++ ) {
  12516. outerNode = eliminateHole( queue[ i ], outerNode );
  12517. }
  12518. return outerNode;
  12519. }
  12520. function compareX( a, b ) {
  12521. return a.x - b.x;
  12522. }
  12523. // find a bridge between vertices that connects hole with an outer ring and link it
  12524. function eliminateHole( hole, outerNode ) {
  12525. const bridge = findHoleBridge( hole, outerNode );
  12526. if ( ! bridge ) {
  12527. return outerNode;
  12528. }
  12529. const bridgeReverse = splitPolygon( bridge, hole );
  12530. // filter collinear points around the cuts
  12531. filterPoints( bridgeReverse, bridgeReverse.next );
  12532. return filterPoints( bridge, bridge.next );
  12533. }
  12534. // David Eberly's algorithm for finding a bridge between hole and outer polygon
  12535. function findHoleBridge( hole, outerNode ) {
  12536. let p = outerNode,
  12537. qx = - Infinity,
  12538. m;
  12539. const hx = hole.x, hy = hole.y;
  12540. // find a segment intersected by a ray from the hole's leftmost point to the left;
  12541. // segment's endpoint with lesser x will be potential connection point
  12542. do {
  12543. if ( hy <= p.y && hy >= p.next.y && p.next.y !== p.y ) {
  12544. const x = p.x + ( hy - p.y ) * ( p.next.x - p.x ) / ( p.next.y - p.y );
  12545. if ( x <= hx && x > qx ) {
  12546. qx = x;
  12547. m = p.x < p.next.x ? p : p.next;
  12548. if ( x === hx ) return m; // hole touches outer segment; pick leftmost endpoint
  12549. }
  12550. }
  12551. p = p.next;
  12552. } while ( p !== outerNode );
  12553. if ( ! m ) return null;
  12554. // look for points inside the triangle of hole point, segment intersection and endpoint;
  12555. // if there are no points found, we have a valid connection;
  12556. // otherwise choose the point of the minimum angle with the ray as connection point
  12557. const stop = m,
  12558. mx = m.x,
  12559. my = m.y;
  12560. let tanMin = Infinity, tan;
  12561. p = m;
  12562. do {
  12563. if ( hx >= p.x && p.x >= mx && hx !== p.x &&
  12564. pointInTriangle( hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y ) ) {
  12565. tan = Math.abs( hy - p.y ) / ( hx - p.x ); // tangential
  12566. if ( locallyInside( p, hole ) && ( tan < tanMin || ( tan === tanMin && ( p.x > m.x || ( p.x === m.x && sectorContainsSector( m, p ) ) ) ) ) ) {
  12567. m = p;
  12568. tanMin = tan;
  12569. }
  12570. }
  12571. p = p.next;
  12572. } while ( p !== stop );
  12573. return m;
  12574. }
  12575. // whether sector in vertex m contains sector in vertex p in the same coordinates
  12576. function sectorContainsSector( m, p ) {
  12577. return area( m.prev, m, p.prev ) < 0 && area( p.next, m, m.next ) < 0;
  12578. }
  12579. // interlink polygon nodes in z-order
  12580. function indexCurve( start, minX, minY, invSize ) {
  12581. let p = start;
  12582. do {
  12583. if ( p.z === 0 ) p.z = zOrder( p.x, p.y, minX, minY, invSize );
  12584. p.prevZ = p.prev;
  12585. p.nextZ = p.next;
  12586. p = p.next;
  12587. } while ( p !== start );
  12588. p.prevZ.nextZ = null;
  12589. p.prevZ = null;
  12590. sortLinked( p );
  12591. }
  12592. // Simon Tatham's linked list merge sort algorithm
  12593. // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
  12594. function sortLinked( list ) {
  12595. let i, p, q, e, tail, numMerges, pSize, qSize,
  12596. inSize = 1;
  12597. do {
  12598. p = list;
  12599. list = null;
  12600. tail = null;
  12601. numMerges = 0;
  12602. while ( p ) {
  12603. numMerges ++;
  12604. q = p;
  12605. pSize = 0;
  12606. for ( i = 0; i < inSize; i ++ ) {
  12607. pSize ++;
  12608. q = q.nextZ;
  12609. if ( ! q ) break;
  12610. }
  12611. qSize = inSize;
  12612. while ( pSize > 0 || ( qSize > 0 && q ) ) {
  12613. if ( pSize !== 0 && ( qSize === 0 || ! q || p.z <= q.z ) ) {
  12614. e = p;
  12615. p = p.nextZ;
  12616. pSize --;
  12617. } else {
  12618. e = q;
  12619. q = q.nextZ;
  12620. qSize --;
  12621. }
  12622. if ( tail ) tail.nextZ = e;
  12623. else list = e;
  12624. e.prevZ = tail;
  12625. tail = e;
  12626. }
  12627. p = q;
  12628. }
  12629. tail.nextZ = null;
  12630. inSize *= 2;
  12631. } while ( numMerges > 1 );
  12632. return list;
  12633. }
  12634. // z-order of a point given coords and inverse of the longer side of data bbox
  12635. function zOrder( x, y, minX, minY, invSize ) {
  12636. // coords are transformed into non-negative 15-bit integer range
  12637. x = ( x - minX ) * invSize | 0;
  12638. y = ( y - minY ) * invSize | 0;
  12639. x = ( x | ( x << 8 ) ) & 0x00FF00FF;
  12640. x = ( x | ( x << 4 ) ) & 0x0F0F0F0F;
  12641. x = ( x | ( x << 2 ) ) & 0x33333333;
  12642. x = ( x | ( x << 1 ) ) & 0x55555555;
  12643. y = ( y | ( y << 8 ) ) & 0x00FF00FF;
  12644. y = ( y | ( y << 4 ) ) & 0x0F0F0F0F;
  12645. y = ( y | ( y << 2 ) ) & 0x33333333;
  12646. y = ( y | ( y << 1 ) ) & 0x55555555;
  12647. return x | ( y << 1 );
  12648. }
  12649. // find the leftmost node of a polygon ring
  12650. function getLeftmost( start ) {
  12651. let p = start,
  12652. leftmost = start;
  12653. do {
  12654. if ( p.x < leftmost.x || ( p.x === leftmost.x && p.y < leftmost.y ) ) leftmost = p;
  12655. p = p.next;
  12656. } while ( p !== start );
  12657. return leftmost;
  12658. }
  12659. // check if a point lies within a convex triangle
  12660. function pointInTriangle( ax, ay, bx, by, cx, cy, px, py ) {
  12661. return ( cx - px ) * ( ay - py ) >= ( ax - px ) * ( cy - py ) &&
  12662. ( ax - px ) * ( by - py ) >= ( bx - px ) * ( ay - py ) &&
  12663. ( bx - px ) * ( cy - py ) >= ( cx - px ) * ( by - py );
  12664. }
  12665. // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
  12666. function isValidDiagonal( a, b ) {
  12667. return a.next.i !== b.i && a.prev.i !== b.i && ! intersectsPolygon( a, b ) && // doesn't intersect other edges
  12668. ( locallyInside( a, b ) && locallyInside( b, a ) && middleInside( a, b ) && // locally visible
  12669. ( area( a.prev, a, b.prev ) || area( a, b.prev, b ) ) || // does not create opposite-facing sectors
  12670. equals( a, b ) && area( a.prev, a, a.next ) > 0 && area( b.prev, b, b.next ) > 0 ); // special zero-length case
  12671. }
  12672. // signed area of a triangle
  12673. function area( p, q, r ) {
  12674. return ( q.y - p.y ) * ( r.x - q.x ) - ( q.x - p.x ) * ( r.y - q.y );
  12675. }
  12676. // check if two points are equal
  12677. function equals( p1, p2 ) {
  12678. return p1.x === p2.x && p1.y === p2.y;
  12679. }
  12680. // check if two segments intersect
  12681. function intersects( p1, q1, p2, q2 ) {
  12682. const o1 = sign( area( p1, q1, p2 ) );
  12683. const o2 = sign( area( p1, q1, q2 ) );
  12684. const o3 = sign( area( p2, q2, p1 ) );
  12685. const o4 = sign( area( p2, q2, q1 ) );
  12686. if ( o1 !== o2 && o3 !== o4 ) return true; // general case
  12687. if ( o1 === 0 && onSegment( p1, p2, q1 ) ) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
  12688. if ( o2 === 0 && onSegment( p1, q2, q1 ) ) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
  12689. if ( o3 === 0 && onSegment( p2, p1, q2 ) ) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
  12690. if ( o4 === 0 && onSegment( p2, q1, q2 ) ) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
  12691. return false;
  12692. }
  12693. // for collinear points p, q, r, check if point q lies on segment pr
  12694. function onSegment( p, q, r ) {
  12695. return q.x <= Math.max( p.x, r.x ) && q.x >= Math.min( p.x, r.x ) && q.y <= Math.max( p.y, r.y ) && q.y >= Math.min( p.y, r.y );
  12696. }
  12697. function sign( num ) {
  12698. return num > 0 ? 1 : num < 0 ? - 1 : 0;
  12699. }
  12700. // check if a polygon diagonal intersects any polygon segments
  12701. function intersectsPolygon( a, b ) {
  12702. let p = a;
  12703. do {
  12704. if ( p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
  12705. intersects( p, p.next, a, b ) ) return true;
  12706. p = p.next;
  12707. } while ( p !== a );
  12708. return false;
  12709. }
  12710. // check if a polygon diagonal is locally inside the polygon
  12711. function locallyInside( a, b ) {
  12712. return area( a.prev, a, a.next ) < 0 ?
  12713. area( a, b, a.next ) >= 0 && area( a, a.prev, b ) >= 0 :
  12714. area( a, b, a.prev ) < 0 || area( a, a.next, b ) < 0;
  12715. }
  12716. // check if the middle point of a polygon diagonal is inside the polygon
  12717. function middleInside( a, b ) {
  12718. let p = a,
  12719. inside = false;
  12720. const px = ( a.x + b.x ) / 2,
  12721. py = ( a.y + b.y ) / 2;
  12722. do {
  12723. if ( ( ( p.y > py ) !== ( p.next.y > py ) ) && p.next.y !== p.y &&
  12724. ( px < ( p.next.x - p.x ) * ( py - p.y ) / ( p.next.y - p.y ) + p.x ) )
  12725. inside = ! inside;
  12726. p = p.next;
  12727. } while ( p !== a );
  12728. return inside;
  12729. }
  12730. // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
  12731. // if one belongs to the outer ring and another to a hole, it merges it into a single ring
  12732. function splitPolygon( a, b ) {
  12733. const a2 = new Node( a.i, a.x, a.y ),
  12734. b2 = new Node( b.i, b.x, b.y ),
  12735. an = a.next,
  12736. bp = b.prev;
  12737. a.next = b;
  12738. b.prev = a;
  12739. a2.next = an;
  12740. an.prev = a2;
  12741. b2.next = a2;
  12742. a2.prev = b2;
  12743. bp.next = b2;
  12744. b2.prev = bp;
  12745. return b2;
  12746. }
  12747. // create a node and optionally link it with previous one (in a circular doubly linked list)
  12748. function insertNode( i, x, y, last ) {
  12749. const p = new Node( i, x, y );
  12750. if ( ! last ) {
  12751. p.prev = p;
  12752. p.next = p;
  12753. } else {
  12754. p.next = last.next;
  12755. p.prev = last;
  12756. last.next.prev = p;
  12757. last.next = p;
  12758. }
  12759. return p;
  12760. }
  12761. function removeNode( p ) {
  12762. p.next.prev = p.prev;
  12763. p.prev.next = p.next;
  12764. if ( p.prevZ ) p.prevZ.nextZ = p.nextZ;
  12765. if ( p.nextZ ) p.nextZ.prevZ = p.prevZ;
  12766. }
  12767. function Node( i, x, y ) {
  12768. // vertex index in coordinates array
  12769. this.i = i;
  12770. // vertex coordinates
  12771. this.x = x;
  12772. this.y = y;
  12773. // previous and next vertex nodes in a polygon ring
  12774. this.prev = null;
  12775. this.next = null;
  12776. // z-order curve value
  12777. this.z = 0;
  12778. // previous and next nodes in z-order
  12779. this.prevZ = null;
  12780. this.nextZ = null;
  12781. // indicates whether this is a steiner point
  12782. this.steiner = false;
  12783. }
  12784. function signedArea( data, start, end, dim ) {
  12785. let sum = 0;
  12786. for ( let i = start, j = end - dim; i < end; i += dim ) {
  12787. sum += ( data[ j ] - data[ i ] ) * ( data[ i + 1 ] + data[ j + 1 ] );
  12788. j = i;
  12789. }
  12790. return sum;
  12791. }
  12792. class ShapeUtils {
  12793. // calculate area of the contour polygon
  12794. static area( contour ) {
  12795. const n = contour.length;
  12796. let a = 0.0;
  12797. for ( let p = n - 1, q = 0; q < n; p = q ++ ) {
  12798. a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
  12799. }
  12800. return a * 0.5;
  12801. }
  12802. static isClockWise( pts ) {
  12803. return ShapeUtils.area( pts ) < 0;
  12804. }
  12805. static triangulateShape( contour, holes ) {
  12806. const vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ]
  12807. const holeIndices = []; // array of hole indices
  12808. const faces = []; // final array of vertex indices like [ [ a,b,d ], [ b,c,d ] ]
  12809. removeDupEndPts( contour );
  12810. addContour( vertices, contour );
  12811. //
  12812. let holeIndex = contour.length;
  12813. holes.forEach( removeDupEndPts );
  12814. for ( let i = 0; i < holes.length; i ++ ) {
  12815. holeIndices.push( holeIndex );
  12816. holeIndex += holes[ i ].length;
  12817. addContour( vertices, holes[ i ] );
  12818. }
  12819. //
  12820. const triangles = Earcut.triangulate( vertices, holeIndices );
  12821. //
  12822. for ( let i = 0; i < triangles.length; i += 3 ) {
  12823. faces.push( triangles.slice( i, i + 3 ) );
  12824. }
  12825. return faces;
  12826. }
  12827. }
  12828. function removeDupEndPts( points ) {
  12829. const l = points.length;
  12830. if ( l > 2 && points[ l - 1 ].equals( points[ 0 ] ) ) {
  12831. points.pop();
  12832. }
  12833. }
  12834. function addContour( vertices, contour ) {
  12835. for ( let i = 0; i < contour.length; i ++ ) {
  12836. vertices.push( contour[ i ].x );
  12837. vertices.push( contour[ i ].y );
  12838. }
  12839. }
  12840. /**
  12841. * Creates extruded geometry from a path shape.
  12842. *
  12843. * parameters = {
  12844. *
  12845. * curveSegments: <int>, // number of points on the curves
  12846. * steps: <int>, // number of points for z-side extrusions / used for subdividing segments of extrude spline too
  12847. * depth: <float>, // Depth to extrude the shape
  12848. *
  12849. * bevelEnabled: <bool>, // turn on bevel
  12850. * bevelThickness: <float>, // how deep into the original shape bevel goes
  12851. * bevelSize: <float>, // how far from shape outline (including bevelOffset) is bevel
  12852. * bevelOffset: <float>, // how far from shape outline does bevel start
  12853. * bevelSegments: <int>, // number of bevel layers
  12854. *
  12855. * extrudePath: <THREE.Curve> // curve to extrude shape along
  12856. *
  12857. * UVGenerator: <Object> // object that provides UV generator functions
  12858. *
  12859. * }
  12860. */
  12861. class ExtrudeGeometry extends BufferGeometry {
  12862. constructor( shapes = new Shape( [ new Vector2( 0.5, 0.5 ), new Vector2( - 0.5, 0.5 ), new Vector2( - 0.5, - 0.5 ), new Vector2( 0.5, - 0.5 ) ] ), options = {} ) {
  12863. super();
  12864. this.type = 'ExtrudeGeometry';
  12865. this.parameters = {
  12866. shapes: shapes,
  12867. options: options
  12868. };
  12869. shapes = Array.isArray( shapes ) ? shapes : [ shapes ];
  12870. const scope = this;
  12871. const verticesArray = [];
  12872. const uvArray = [];
  12873. for ( let i = 0, l = shapes.length; i < l; i ++ ) {
  12874. const shape = shapes[ i ];
  12875. addShape( shape );
  12876. }
  12877. // build geometry
  12878. this.setAttribute( 'position', new Float32BufferAttribute( verticesArray, 3 ) );
  12879. this.setAttribute( 'uv', new Float32BufferAttribute( uvArray, 2 ) );
  12880. this.computeVertexNormals();
  12881. // functions
  12882. function addShape( shape ) {
  12883. const placeholder = [];
  12884. // options
  12885. const curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
  12886. const steps = options.steps !== undefined ? options.steps : 1;
  12887. const depth = options.depth !== undefined ? options.depth : 1;
  12888. let bevelEnabled = options.bevelEnabled !== undefined ? options.bevelEnabled : true;
  12889. let bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 0.2;
  12890. let bevelSize = options.bevelSize !== undefined ? options.bevelSize : bevelThickness - 0.1;
  12891. let bevelOffset = options.bevelOffset !== undefined ? options.bevelOffset : 0;
  12892. let bevelSegments = options.bevelSegments !== undefined ? options.bevelSegments : 3;
  12893. const extrudePath = options.extrudePath;
  12894. const uvgen = options.UVGenerator !== undefined ? options.UVGenerator : WorldUVGenerator;
  12895. //
  12896. let extrudePts, extrudeByPath = false;
  12897. let splineTube, binormal, normal, position2;
  12898. if ( extrudePath ) {
  12899. extrudePts = extrudePath.getSpacedPoints( steps );
  12900. extrudeByPath = true;
  12901. bevelEnabled = false; // bevels not supported for path extrusion
  12902. // SETUP TNB variables
  12903. // TODO1 - have a .isClosed in spline?
  12904. splineTube = extrudePath.computeFrenetFrames( steps, false );
  12905. // console.log(splineTube, 'splineTube', splineTube.normals.length, 'steps', steps, 'extrudePts', extrudePts.length);
  12906. binormal = new Vector3();
  12907. normal = new Vector3();
  12908. position2 = new Vector3();
  12909. }
  12910. // Safeguards if bevels are not enabled
  12911. if ( ! bevelEnabled ) {
  12912. bevelSegments = 0;
  12913. bevelThickness = 0;
  12914. bevelSize = 0;
  12915. bevelOffset = 0;
  12916. }
  12917. // Variables initialization
  12918. const shapePoints = shape.extractPoints( curveSegments );
  12919. let vertices = shapePoints.shape;
  12920. const holes = shapePoints.holes;
  12921. const reverse = ! ShapeUtils.isClockWise( vertices );
  12922. if ( reverse ) {
  12923. vertices = vertices.reverse();
  12924. // Maybe we should also check if holes are in the opposite direction, just to be safe ...
  12925. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  12926. const ahole = holes[ h ];
  12927. if ( ShapeUtils.isClockWise( ahole ) ) {
  12928. holes[ h ] = ahole.reverse();
  12929. }
  12930. }
  12931. }
  12932. const faces = ShapeUtils.triangulateShape( vertices, holes );
  12933. /* Vertices */
  12934. const contour = vertices; // vertices has all points but contour has only points of circumference
  12935. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  12936. const ahole = holes[ h ];
  12937. vertices = vertices.concat( ahole );
  12938. }
  12939. function scalePt2( pt, vec, size ) {
  12940. if ( ! vec ) console.error( 'THREE.ExtrudeGeometry: vec does not exist' );
  12941. return pt.clone().addScaledVector( vec, size );
  12942. }
  12943. const vlen = vertices.length, flen = faces.length;
  12944. // Find directions for point movement
  12945. function getBevelVec( inPt, inPrev, inNext ) {
  12946. // computes for inPt the corresponding point inPt' on a new contour
  12947. // shifted by 1 unit (length of normalized vector) to the left
  12948. // if we walk along contour clockwise, this new contour is outside the old one
  12949. //
  12950. // inPt' is the intersection of the two lines parallel to the two
  12951. // adjacent edges of inPt at a distance of 1 unit on the left side.
  12952. let v_trans_x, v_trans_y, shrink_by; // resulting translation vector for inPt
  12953. // good reading for geometry algorithms (here: line-line intersection)
  12954. // http://geomalgorithms.com/a05-_intersect-1.html
  12955. const v_prev_x = inPt.x - inPrev.x,
  12956. v_prev_y = inPt.y - inPrev.y;
  12957. const v_next_x = inNext.x - inPt.x,
  12958. v_next_y = inNext.y - inPt.y;
  12959. const v_prev_lensq = ( v_prev_x * v_prev_x + v_prev_y * v_prev_y );
  12960. // check for collinear edges
  12961. const collinear0 = ( v_prev_x * v_next_y - v_prev_y * v_next_x );
  12962. if ( Math.abs( collinear0 ) > Number.EPSILON ) {
  12963. // not collinear
  12964. // length of vectors for normalizing
  12965. const v_prev_len = Math.sqrt( v_prev_lensq );
  12966. const v_next_len = Math.sqrt( v_next_x * v_next_x + v_next_y * v_next_y );
  12967. // shift adjacent points by unit vectors to the left
  12968. const ptPrevShift_x = ( inPrev.x - v_prev_y / v_prev_len );
  12969. const ptPrevShift_y = ( inPrev.y + v_prev_x / v_prev_len );
  12970. const ptNextShift_x = ( inNext.x - v_next_y / v_next_len );
  12971. const ptNextShift_y = ( inNext.y + v_next_x / v_next_len );
  12972. // scaling factor for v_prev to intersection point
  12973. const sf = ( ( ptNextShift_x - ptPrevShift_x ) * v_next_y -
  12974. ( ptNextShift_y - ptPrevShift_y ) * v_next_x ) /
  12975. ( v_prev_x * v_next_y - v_prev_y * v_next_x );
  12976. // vector from inPt to intersection point
  12977. v_trans_x = ( ptPrevShift_x + v_prev_x * sf - inPt.x );
  12978. v_trans_y = ( ptPrevShift_y + v_prev_y * sf - inPt.y );
  12979. // Don't normalize!, otherwise sharp corners become ugly
  12980. // but prevent crazy spikes
  12981. const v_trans_lensq = ( v_trans_x * v_trans_x + v_trans_y * v_trans_y );
  12982. if ( v_trans_lensq <= 2 ) {
  12983. return new Vector2( v_trans_x, v_trans_y );
  12984. } else {
  12985. shrink_by = Math.sqrt( v_trans_lensq / 2 );
  12986. }
  12987. } else {
  12988. // handle special case of collinear edges
  12989. let direction_eq = false; // assumes: opposite
  12990. if ( v_prev_x > Number.EPSILON ) {
  12991. if ( v_next_x > Number.EPSILON ) {
  12992. direction_eq = true;
  12993. }
  12994. } else {
  12995. if ( v_prev_x < - Number.EPSILON ) {
  12996. if ( v_next_x < - Number.EPSILON ) {
  12997. direction_eq = true;
  12998. }
  12999. } else {
  13000. if ( Math.sign( v_prev_y ) === Math.sign( v_next_y ) ) {
  13001. direction_eq = true;
  13002. }
  13003. }
  13004. }
  13005. if ( direction_eq ) {
  13006. // console.log("Warning: lines are a straight sequence");
  13007. v_trans_x = - v_prev_y;
  13008. v_trans_y = v_prev_x;
  13009. shrink_by = Math.sqrt( v_prev_lensq );
  13010. } else {
  13011. // console.log("Warning: lines are a straight spike");
  13012. v_trans_x = v_prev_x;
  13013. v_trans_y = v_prev_y;
  13014. shrink_by = Math.sqrt( v_prev_lensq / 2 );
  13015. }
  13016. }
  13017. return new Vector2( v_trans_x / shrink_by, v_trans_y / shrink_by );
  13018. }
  13019. const contourMovements = [];
  13020. for ( let i = 0, il = contour.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
  13021. if ( j === il ) j = 0;
  13022. if ( k === il ) k = 0;
  13023. // (j)---(i)---(k)
  13024. // console.log('i,j,k', i, j , k)
  13025. contourMovements[ i ] = getBevelVec( contour[ i ], contour[ j ], contour[ k ] );
  13026. }
  13027. const holesMovements = [];
  13028. let oneHoleMovements, verticesMovements = contourMovements.concat();
  13029. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  13030. const ahole = holes[ h ];
  13031. oneHoleMovements = [];
  13032. for ( let i = 0, il = ahole.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
  13033. if ( j === il ) j = 0;
  13034. if ( k === il ) k = 0;
  13035. // (j)---(i)---(k)
  13036. oneHoleMovements[ i ] = getBevelVec( ahole[ i ], ahole[ j ], ahole[ k ] );
  13037. }
  13038. holesMovements.push( oneHoleMovements );
  13039. verticesMovements = verticesMovements.concat( oneHoleMovements );
  13040. }
  13041. // Loop bevelSegments, 1 for the front, 1 for the back
  13042. for ( let b = 0; b < bevelSegments; b ++ ) {
  13043. //for ( b = bevelSegments; b > 0; b -- ) {
  13044. const t = b / bevelSegments;
  13045. const z = bevelThickness * Math.cos( t * Math.PI / 2 );
  13046. const bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset;
  13047. // contract shape
  13048. for ( let i = 0, il = contour.length; i < il; i ++ ) {
  13049. const vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
  13050. v( vert.x, vert.y, - z );
  13051. }
  13052. // expand holes
  13053. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  13054. const ahole = holes[ h ];
  13055. oneHoleMovements = holesMovements[ h ];
  13056. for ( let i = 0, il = ahole.length; i < il; i ++ ) {
  13057. const vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
  13058. v( vert.x, vert.y, - z );
  13059. }
  13060. }
  13061. }
  13062. const bs = bevelSize + bevelOffset;
  13063. // Back facing vertices
  13064. for ( let i = 0; i < vlen; i ++ ) {
  13065. const vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
  13066. if ( ! extrudeByPath ) {
  13067. v( vert.x, vert.y, 0 );
  13068. } else {
  13069. // v( vert.x, vert.y + extrudePts[ 0 ].y, extrudePts[ 0 ].x );
  13070. normal.copy( splineTube.normals[ 0 ] ).multiplyScalar( vert.x );
  13071. binormal.copy( splineTube.binormals[ 0 ] ).multiplyScalar( vert.y );
  13072. position2.copy( extrudePts[ 0 ] ).add( normal ).add( binormal );
  13073. v( position2.x, position2.y, position2.z );
  13074. }
  13075. }
  13076. // Add stepped vertices...
  13077. // Including front facing vertices
  13078. for ( let s = 1; s <= steps; s ++ ) {
  13079. for ( let i = 0; i < vlen; i ++ ) {
  13080. const vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
  13081. if ( ! extrudeByPath ) {
  13082. v( vert.x, vert.y, depth / steps * s );
  13083. } else {
  13084. // v( vert.x, vert.y + extrudePts[ s - 1 ].y, extrudePts[ s - 1 ].x );
  13085. normal.copy( splineTube.normals[ s ] ).multiplyScalar( vert.x );
  13086. binormal.copy( splineTube.binormals[ s ] ).multiplyScalar( vert.y );
  13087. position2.copy( extrudePts[ s ] ).add( normal ).add( binormal );
  13088. v( position2.x, position2.y, position2.z );
  13089. }
  13090. }
  13091. }
  13092. // Add bevel segments planes
  13093. //for ( b = 1; b <= bevelSegments; b ++ ) {
  13094. for ( let b = bevelSegments - 1; b >= 0; b -- ) {
  13095. const t = b / bevelSegments;
  13096. const z = bevelThickness * Math.cos( t * Math.PI / 2 );
  13097. const bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset;
  13098. // contract shape
  13099. for ( let i = 0, il = contour.length; i < il; i ++ ) {
  13100. const vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
  13101. v( vert.x, vert.y, depth + z );
  13102. }
  13103. // expand holes
  13104. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  13105. const ahole = holes[ h ];
  13106. oneHoleMovements = holesMovements[ h ];
  13107. for ( let i = 0, il = ahole.length; i < il; i ++ ) {
  13108. const vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
  13109. if ( ! extrudeByPath ) {
  13110. v( vert.x, vert.y, depth + z );
  13111. } else {
  13112. v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x + z );
  13113. }
  13114. }
  13115. }
  13116. }
  13117. /* Faces */
  13118. // Top and bottom faces
  13119. buildLidFaces();
  13120. // Sides faces
  13121. buildSideFaces();
  13122. ///// Internal functions
  13123. function buildLidFaces() {
  13124. const start = verticesArray.length / 3;
  13125. if ( bevelEnabled ) {
  13126. let layer = 0; // steps + 1
  13127. let offset = vlen * layer;
  13128. // Bottom faces
  13129. for ( let i = 0; i < flen; i ++ ) {
  13130. const face = faces[ i ];
  13131. f3( face[ 2 ] + offset, face[ 1 ] + offset, face[ 0 ] + offset );
  13132. }
  13133. layer = steps + bevelSegments * 2;
  13134. offset = vlen * layer;
  13135. // Top faces
  13136. for ( let i = 0; i < flen; i ++ ) {
  13137. const face = faces[ i ];
  13138. f3( face[ 0 ] + offset, face[ 1 ] + offset, face[ 2 ] + offset );
  13139. }
  13140. } else {
  13141. // Bottom faces
  13142. for ( let i = 0; i < flen; i ++ ) {
  13143. const face = faces[ i ];
  13144. f3( face[ 2 ], face[ 1 ], face[ 0 ] );
  13145. }
  13146. // Top faces
  13147. for ( let i = 0; i < flen; i ++ ) {
  13148. const face = faces[ i ];
  13149. f3( face[ 0 ] + vlen * steps, face[ 1 ] + vlen * steps, face[ 2 ] + vlen * steps );
  13150. }
  13151. }
  13152. scope.addGroup( start, verticesArray.length / 3 - start, 0 );
  13153. }
  13154. // Create faces for the z-sides of the shape
  13155. function buildSideFaces() {
  13156. const start = verticesArray.length / 3;
  13157. let layeroffset = 0;
  13158. sidewalls( contour, layeroffset );
  13159. layeroffset += contour.length;
  13160. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  13161. const ahole = holes[ h ];
  13162. sidewalls( ahole, layeroffset );
  13163. //, true
  13164. layeroffset += ahole.length;
  13165. }
  13166. scope.addGroup( start, verticesArray.length / 3 - start, 1 );
  13167. }
  13168. function sidewalls( contour, layeroffset ) {
  13169. let i = contour.length;
  13170. while ( -- i >= 0 ) {
  13171. const j = i;
  13172. let k = i - 1;
  13173. if ( k < 0 ) k = contour.length - 1;
  13174. //console.log('b', i,j, i-1, k,vertices.length);
  13175. for ( let s = 0, sl = ( steps + bevelSegments * 2 ); s < sl; s ++ ) {
  13176. const slen1 = vlen * s;
  13177. const slen2 = vlen * ( s + 1 );
  13178. const a = layeroffset + j + slen1,
  13179. b = layeroffset + k + slen1,
  13180. c = layeroffset + k + slen2,
  13181. d = layeroffset + j + slen2;
  13182. f4( a, b, c, d );
  13183. }
  13184. }
  13185. }
  13186. function v( x, y, z ) {
  13187. placeholder.push( x );
  13188. placeholder.push( y );
  13189. placeholder.push( z );
  13190. }
  13191. function f3( a, b, c ) {
  13192. addVertex( a );
  13193. addVertex( b );
  13194. addVertex( c );
  13195. const nextIndex = verticesArray.length / 3;
  13196. const uvs = uvgen.generateTopUV( scope, verticesArray, nextIndex - 3, nextIndex - 2, nextIndex - 1 );
  13197. addUV( uvs[ 0 ] );
  13198. addUV( uvs[ 1 ] );
  13199. addUV( uvs[ 2 ] );
  13200. }
  13201. function f4( a, b, c, d ) {
  13202. addVertex( a );
  13203. addVertex( b );
  13204. addVertex( d );
  13205. addVertex( b );
  13206. addVertex( c );
  13207. addVertex( d );
  13208. const nextIndex = verticesArray.length / 3;
  13209. const uvs = uvgen.generateSideWallUV( scope, verticesArray, nextIndex - 6, nextIndex - 3, nextIndex - 2, nextIndex - 1 );
  13210. addUV( uvs[ 0 ] );
  13211. addUV( uvs[ 1 ] );
  13212. addUV( uvs[ 3 ] );
  13213. addUV( uvs[ 1 ] );
  13214. addUV( uvs[ 2 ] );
  13215. addUV( uvs[ 3 ] );
  13216. }
  13217. function addVertex( index ) {
  13218. verticesArray.push( placeholder[ index * 3 + 0 ] );
  13219. verticesArray.push( placeholder[ index * 3 + 1 ] );
  13220. verticesArray.push( placeholder[ index * 3 + 2 ] );
  13221. }
  13222. function addUV( vector2 ) {
  13223. uvArray.push( vector2.x );
  13224. uvArray.push( vector2.y );
  13225. }
  13226. }
  13227. }
  13228. copy( source ) {
  13229. super.copy( source );
  13230. this.parameters = Object.assign( {}, source.parameters );
  13231. return this;
  13232. }
  13233. toJSON() {
  13234. const data = super.toJSON();
  13235. const shapes = this.parameters.shapes;
  13236. const options = this.parameters.options;
  13237. return toJSON$1( shapes, options, data );
  13238. }
  13239. static fromJSON( data, shapes ) {
  13240. const geometryShapes = [];
  13241. for ( let j = 0, jl = data.shapes.length; j < jl; j ++ ) {
  13242. const shape = shapes[ data.shapes[ j ] ];
  13243. geometryShapes.push( shape );
  13244. }
  13245. const extrudePath = data.options.extrudePath;
  13246. if ( extrudePath !== undefined ) {
  13247. data.options.extrudePath = new Curves[ extrudePath.type ]().fromJSON( extrudePath );
  13248. }
  13249. return new ExtrudeGeometry( geometryShapes, data.options );
  13250. }
  13251. }
  13252. const WorldUVGenerator = {
  13253. generateTopUV: function ( geometry, vertices, indexA, indexB, indexC ) {
  13254. const a_x = vertices[ indexA * 3 ];
  13255. const a_y = vertices[ indexA * 3 + 1 ];
  13256. const b_x = vertices[ indexB * 3 ];
  13257. const b_y = vertices[ indexB * 3 + 1 ];
  13258. const c_x = vertices[ indexC * 3 ];
  13259. const c_y = vertices[ indexC * 3 + 1 ];
  13260. return [
  13261. new Vector2( a_x, a_y ),
  13262. new Vector2( b_x, b_y ),
  13263. new Vector2( c_x, c_y )
  13264. ];
  13265. },
  13266. generateSideWallUV: function ( geometry, vertices, indexA, indexB, indexC, indexD ) {
  13267. const a_x = vertices[ indexA * 3 ];
  13268. const a_y = vertices[ indexA * 3 + 1 ];
  13269. const a_z = vertices[ indexA * 3 + 2 ];
  13270. const b_x = vertices[ indexB * 3 ];
  13271. const b_y = vertices[ indexB * 3 + 1 ];
  13272. const b_z = vertices[ indexB * 3 + 2 ];
  13273. const c_x = vertices[ indexC * 3 ];
  13274. const c_y = vertices[ indexC * 3 + 1 ];
  13275. const c_z = vertices[ indexC * 3 + 2 ];
  13276. const d_x = vertices[ indexD * 3 ];
  13277. const d_y = vertices[ indexD * 3 + 1 ];
  13278. const d_z = vertices[ indexD * 3 + 2 ];
  13279. if ( Math.abs( a_y - b_y ) < Math.abs( a_x - b_x ) ) {
  13280. return [
  13281. new Vector2( a_x, 1 - a_z ),
  13282. new Vector2( b_x, 1 - b_z ),
  13283. new Vector2( c_x, 1 - c_z ),
  13284. new Vector2( d_x, 1 - d_z )
  13285. ];
  13286. } else {
  13287. return [
  13288. new Vector2( a_y, 1 - a_z ),
  13289. new Vector2( b_y, 1 - b_z ),
  13290. new Vector2( c_y, 1 - c_z ),
  13291. new Vector2( d_y, 1 - d_z )
  13292. ];
  13293. }
  13294. }
  13295. };
  13296. function toJSON$1( shapes, options, data ) {
  13297. data.shapes = [];
  13298. if ( Array.isArray( shapes ) ) {
  13299. for ( let i = 0, l = shapes.length; i < l; i ++ ) {
  13300. const shape = shapes[ i ];
  13301. data.shapes.push( shape.uuid );
  13302. }
  13303. } else {
  13304. data.shapes.push( shapes.uuid );
  13305. }
  13306. data.options = Object.assign( {}, options );
  13307. if ( options.extrudePath !== undefined ) data.options.extrudePath = options.extrudePath.toJSON();
  13308. return data;
  13309. }
  13310. class IcosahedronGeometry extends PolyhedronGeometry {
  13311. constructor( radius = 1, detail = 0 ) {
  13312. const t = ( 1 + Math.sqrt( 5 ) ) / 2;
  13313. const vertices = [
  13314. - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t, 0,
  13315. 0, - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t,
  13316. t, 0, - 1, t, 0, 1, - t, 0, - 1, - t, 0, 1
  13317. ];
  13318. const indices = [
  13319. 0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
  13320. 1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
  13321. 3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
  13322. 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1
  13323. ];
  13324. super( vertices, indices, radius, detail );
  13325. this.type = 'IcosahedronGeometry';
  13326. this.parameters = {
  13327. radius: radius,
  13328. detail: detail
  13329. };
  13330. }
  13331. static fromJSON( data ) {
  13332. return new IcosahedronGeometry( data.radius, data.detail );
  13333. }
  13334. }
  13335. class OctahedronGeometry extends PolyhedronGeometry {
  13336. constructor( radius = 1, detail = 0 ) {
  13337. const vertices = [
  13338. 1, 0, 0, - 1, 0, 0, 0, 1, 0,
  13339. 0, - 1, 0, 0, 0, 1, 0, 0, - 1
  13340. ];
  13341. const indices = [
  13342. 0, 2, 4, 0, 4, 3, 0, 3, 5,
  13343. 0, 5, 2, 1, 2, 5, 1, 5, 3,
  13344. 1, 3, 4, 1, 4, 2
  13345. ];
  13346. super( vertices, indices, radius, detail );
  13347. this.type = 'OctahedronGeometry';
  13348. this.parameters = {
  13349. radius: radius,
  13350. detail: detail
  13351. };
  13352. }
  13353. static fromJSON( data ) {
  13354. return new OctahedronGeometry( data.radius, data.detail );
  13355. }
  13356. }
  13357. class PlaneGeometry extends BufferGeometry {
  13358. constructor( width = 1, height = 1, widthSegments = 1, heightSegments = 1 ) {
  13359. super();
  13360. this.type = 'PlaneGeometry';
  13361. this.parameters = {
  13362. width: width,
  13363. height: height,
  13364. widthSegments: widthSegments,
  13365. heightSegments: heightSegments
  13366. };
  13367. const width_half = width / 2;
  13368. const height_half = height / 2;
  13369. const gridX = Math.floor( widthSegments );
  13370. const gridY = Math.floor( heightSegments );
  13371. const gridX1 = gridX + 1;
  13372. const gridY1 = gridY + 1;
  13373. const segment_width = width / gridX;
  13374. const segment_height = height / gridY;
  13375. //
  13376. const indices = [];
  13377. const vertices = [];
  13378. const normals = [];
  13379. const uvs = [];
  13380. for ( let iy = 0; iy < gridY1; iy ++ ) {
  13381. const y = iy * segment_height - height_half;
  13382. for ( let ix = 0; ix < gridX1; ix ++ ) {
  13383. const x = ix * segment_width - width_half;
  13384. vertices.push( x, - y, 0 );
  13385. normals.push( 0, 0, 1 );
  13386. uvs.push( ix / gridX );
  13387. uvs.push( 1 - ( iy / gridY ) );
  13388. }
  13389. }
  13390. for ( let iy = 0; iy < gridY; iy ++ ) {
  13391. for ( let ix = 0; ix < gridX; ix ++ ) {
  13392. const a = ix + gridX1 * iy;
  13393. const b = ix + gridX1 * ( iy + 1 );
  13394. const c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  13395. const d = ( ix + 1 ) + gridX1 * iy;
  13396. indices.push( a, b, d );
  13397. indices.push( b, c, d );
  13398. }
  13399. }
  13400. this.setIndex( indices );
  13401. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13402. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13403. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13404. }
  13405. copy( source ) {
  13406. super.copy( source );
  13407. this.parameters = Object.assign( {}, source.parameters );
  13408. return this;
  13409. }
  13410. static fromJSON( data ) {
  13411. return new PlaneGeometry( data.width, data.height, data.widthSegments, data.heightSegments );
  13412. }
  13413. }
  13414. class RingGeometry extends BufferGeometry {
  13415. constructor( innerRadius = 0.5, outerRadius = 1, thetaSegments = 32, phiSegments = 1, thetaStart = 0, thetaLength = Math.PI * 2 ) {
  13416. super();
  13417. this.type = 'RingGeometry';
  13418. this.parameters = {
  13419. innerRadius: innerRadius,
  13420. outerRadius: outerRadius,
  13421. thetaSegments: thetaSegments,
  13422. phiSegments: phiSegments,
  13423. thetaStart: thetaStart,
  13424. thetaLength: thetaLength
  13425. };
  13426. thetaSegments = Math.max( 3, thetaSegments );
  13427. phiSegments = Math.max( 1, phiSegments );
  13428. // buffers
  13429. const indices = [];
  13430. const vertices = [];
  13431. const normals = [];
  13432. const uvs = [];
  13433. // some helper variables
  13434. let radius = innerRadius;
  13435. const radiusStep = ( ( outerRadius - innerRadius ) / phiSegments );
  13436. const vertex = new Vector3();
  13437. const uv = new Vector2();
  13438. // generate vertices, normals and uvs
  13439. for ( let j = 0; j <= phiSegments; j ++ ) {
  13440. for ( let i = 0; i <= thetaSegments; i ++ ) {
  13441. // values are generate from the inside of the ring to the outside
  13442. const segment = thetaStart + i / thetaSegments * thetaLength;
  13443. // vertex
  13444. vertex.x = radius * Math.cos( segment );
  13445. vertex.y = radius * Math.sin( segment );
  13446. vertices.push( vertex.x, vertex.y, vertex.z );
  13447. // normal
  13448. normals.push( 0, 0, 1 );
  13449. // uv
  13450. uv.x = ( vertex.x / outerRadius + 1 ) / 2;
  13451. uv.y = ( vertex.y / outerRadius + 1 ) / 2;
  13452. uvs.push( uv.x, uv.y );
  13453. }
  13454. // increase the radius for next row of vertices
  13455. radius += radiusStep;
  13456. }
  13457. // indices
  13458. for ( let j = 0; j < phiSegments; j ++ ) {
  13459. const thetaSegmentLevel = j * ( thetaSegments + 1 );
  13460. for ( let i = 0; i < thetaSegments; i ++ ) {
  13461. const segment = i + thetaSegmentLevel;
  13462. const a = segment;
  13463. const b = segment + thetaSegments + 1;
  13464. const c = segment + thetaSegments + 2;
  13465. const d = segment + 1;
  13466. // faces
  13467. indices.push( a, b, d );
  13468. indices.push( b, c, d );
  13469. }
  13470. }
  13471. // build geometry
  13472. this.setIndex( indices );
  13473. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13474. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13475. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13476. }
  13477. copy( source ) {
  13478. super.copy( source );
  13479. this.parameters = Object.assign( {}, source.parameters );
  13480. return this;
  13481. }
  13482. static fromJSON( data ) {
  13483. return new RingGeometry( data.innerRadius, data.outerRadius, data.thetaSegments, data.phiSegments, data.thetaStart, data.thetaLength );
  13484. }
  13485. }
  13486. class ShapeGeometry extends BufferGeometry {
  13487. constructor( shapes = new Shape( [ new Vector2( 0, 0.5 ), new Vector2( - 0.5, - 0.5 ), new Vector2( 0.5, - 0.5 ) ] ), curveSegments = 12 ) {
  13488. super();
  13489. this.type = 'ShapeGeometry';
  13490. this.parameters = {
  13491. shapes: shapes,
  13492. curveSegments: curveSegments
  13493. };
  13494. // buffers
  13495. const indices = [];
  13496. const vertices = [];
  13497. const normals = [];
  13498. const uvs = [];
  13499. // helper variables
  13500. let groupStart = 0;
  13501. let groupCount = 0;
  13502. // allow single and array values for "shapes" parameter
  13503. if ( Array.isArray( shapes ) === false ) {
  13504. addShape( shapes );
  13505. } else {
  13506. for ( let i = 0; i < shapes.length; i ++ ) {
  13507. addShape( shapes[ i ] );
  13508. this.addGroup( groupStart, groupCount, i ); // enables MultiMaterial support
  13509. groupStart += groupCount;
  13510. groupCount = 0;
  13511. }
  13512. }
  13513. // build geometry
  13514. this.setIndex( indices );
  13515. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13516. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13517. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13518. // helper functions
  13519. function addShape( shape ) {
  13520. const indexOffset = vertices.length / 3;
  13521. const points = shape.extractPoints( curveSegments );
  13522. let shapeVertices = points.shape;
  13523. const shapeHoles = points.holes;
  13524. // check direction of vertices
  13525. if ( ShapeUtils.isClockWise( shapeVertices ) === false ) {
  13526. shapeVertices = shapeVertices.reverse();
  13527. }
  13528. for ( let i = 0, l = shapeHoles.length; i < l; i ++ ) {
  13529. const shapeHole = shapeHoles[ i ];
  13530. if ( ShapeUtils.isClockWise( shapeHole ) === true ) {
  13531. shapeHoles[ i ] = shapeHole.reverse();
  13532. }
  13533. }
  13534. const faces = ShapeUtils.triangulateShape( shapeVertices, shapeHoles );
  13535. // join vertices of inner and outer paths to a single array
  13536. for ( let i = 0, l = shapeHoles.length; i < l; i ++ ) {
  13537. const shapeHole = shapeHoles[ i ];
  13538. shapeVertices = shapeVertices.concat( shapeHole );
  13539. }
  13540. // vertices, normals, uvs
  13541. for ( let i = 0, l = shapeVertices.length; i < l; i ++ ) {
  13542. const vertex = shapeVertices[ i ];
  13543. vertices.push( vertex.x, vertex.y, 0 );
  13544. normals.push( 0, 0, 1 );
  13545. uvs.push( vertex.x, vertex.y ); // world uvs
  13546. }
  13547. // indices
  13548. for ( let i = 0, l = faces.length; i < l; i ++ ) {
  13549. const face = faces[ i ];
  13550. const a = face[ 0 ] + indexOffset;
  13551. const b = face[ 1 ] + indexOffset;
  13552. const c = face[ 2 ] + indexOffset;
  13553. indices.push( a, b, c );
  13554. groupCount += 3;
  13555. }
  13556. }
  13557. }
  13558. copy( source ) {
  13559. super.copy( source );
  13560. this.parameters = Object.assign( {}, source.parameters );
  13561. return this;
  13562. }
  13563. toJSON() {
  13564. const data = super.toJSON();
  13565. const shapes = this.parameters.shapes;
  13566. return toJSON( shapes, data );
  13567. }
  13568. static fromJSON( data, shapes ) {
  13569. const geometryShapes = [];
  13570. for ( let j = 0, jl = data.shapes.length; j < jl; j ++ ) {
  13571. const shape = shapes[ data.shapes[ j ] ];
  13572. geometryShapes.push( shape );
  13573. }
  13574. return new ShapeGeometry( geometryShapes, data.curveSegments );
  13575. }
  13576. }
  13577. function toJSON( shapes, data ) {
  13578. data.shapes = [];
  13579. if ( Array.isArray( shapes ) ) {
  13580. for ( let i = 0, l = shapes.length; i < l; i ++ ) {
  13581. const shape = shapes[ i ];
  13582. data.shapes.push( shape.uuid );
  13583. }
  13584. } else {
  13585. data.shapes.push( shapes.uuid );
  13586. }
  13587. return data;
  13588. }
  13589. class SphereGeometry extends BufferGeometry {
  13590. constructor( radius = 1, widthSegments = 32, heightSegments = 16, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI ) {
  13591. super();
  13592. this.type = 'SphereGeometry';
  13593. this.parameters = {
  13594. radius: radius,
  13595. widthSegments: widthSegments,
  13596. heightSegments: heightSegments,
  13597. phiStart: phiStart,
  13598. phiLength: phiLength,
  13599. thetaStart: thetaStart,
  13600. thetaLength: thetaLength
  13601. };
  13602. widthSegments = Math.max( 3, Math.floor( widthSegments ) );
  13603. heightSegments = Math.max( 2, Math.floor( heightSegments ) );
  13604. const thetaEnd = Math.min( thetaStart + thetaLength, Math.PI );
  13605. let index = 0;
  13606. const grid = [];
  13607. const vertex = new Vector3();
  13608. const normal = new Vector3();
  13609. // buffers
  13610. const indices = [];
  13611. const vertices = [];
  13612. const normals = [];
  13613. const uvs = [];
  13614. // generate vertices, normals and uvs
  13615. for ( let iy = 0; iy <= heightSegments; iy ++ ) {
  13616. const verticesRow = [];
  13617. const v = iy / heightSegments;
  13618. // special case for the poles
  13619. let uOffset = 0;
  13620. if ( iy === 0 && thetaStart === 0 ) {
  13621. uOffset = 0.5 / widthSegments;
  13622. } else if ( iy === heightSegments && thetaEnd === Math.PI ) {
  13623. uOffset = - 0.5 / widthSegments;
  13624. }
  13625. for ( let ix = 0; ix <= widthSegments; ix ++ ) {
  13626. const u = ix / widthSegments;
  13627. // vertex
  13628. vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  13629. vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
  13630. vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  13631. vertices.push( vertex.x, vertex.y, vertex.z );
  13632. // normal
  13633. normal.copy( vertex ).normalize();
  13634. normals.push( normal.x, normal.y, normal.z );
  13635. // uv
  13636. uvs.push( u + uOffset, 1 - v );
  13637. verticesRow.push( index ++ );
  13638. }
  13639. grid.push( verticesRow );
  13640. }
  13641. // indices
  13642. for ( let iy = 0; iy < heightSegments; iy ++ ) {
  13643. for ( let ix = 0; ix < widthSegments; ix ++ ) {
  13644. const a = grid[ iy ][ ix + 1 ];
  13645. const b = grid[ iy ][ ix ];
  13646. const c = grid[ iy + 1 ][ ix ];
  13647. const d = grid[ iy + 1 ][ ix + 1 ];
  13648. if ( iy !== 0 || thetaStart > 0 ) indices.push( a, b, d );
  13649. if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) indices.push( b, c, d );
  13650. }
  13651. }
  13652. // build geometry
  13653. this.setIndex( indices );
  13654. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13655. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13656. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13657. }
  13658. copy( source ) {
  13659. super.copy( source );
  13660. this.parameters = Object.assign( {}, source.parameters );
  13661. return this;
  13662. }
  13663. static fromJSON( data ) {
  13664. return new SphereGeometry( data.radius, data.widthSegments, data.heightSegments, data.phiStart, data.phiLength, data.thetaStart, data.thetaLength );
  13665. }
  13666. }
  13667. class TetrahedronGeometry extends PolyhedronGeometry {
  13668. constructor( radius = 1, detail = 0 ) {
  13669. const vertices = [
  13670. 1, 1, 1, - 1, - 1, 1, - 1, 1, - 1, 1, - 1, - 1
  13671. ];
  13672. const indices = [
  13673. 2, 1, 0, 0, 3, 2, 1, 3, 0, 2, 3, 1
  13674. ];
  13675. super( vertices, indices, radius, detail );
  13676. this.type = 'TetrahedronGeometry';
  13677. this.parameters = {
  13678. radius: radius,
  13679. detail: detail
  13680. };
  13681. }
  13682. static fromJSON( data ) {
  13683. return new TetrahedronGeometry( data.radius, data.detail );
  13684. }
  13685. }
  13686. class TorusGeometry extends BufferGeometry {
  13687. constructor( radius = 1, tube = 0.4, radialSegments = 12, tubularSegments = 48, arc = Math.PI * 2 ) {
  13688. super();
  13689. this.type = 'TorusGeometry';
  13690. this.parameters = {
  13691. radius: radius,
  13692. tube: tube,
  13693. radialSegments: radialSegments,
  13694. tubularSegments: tubularSegments,
  13695. arc: arc
  13696. };
  13697. radialSegments = Math.floor( radialSegments );
  13698. tubularSegments = Math.floor( tubularSegments );
  13699. // buffers
  13700. const indices = [];
  13701. const vertices = [];
  13702. const normals = [];
  13703. const uvs = [];
  13704. // helper variables
  13705. const center = new Vector3();
  13706. const vertex = new Vector3();
  13707. const normal = new Vector3();
  13708. // generate vertices, normals and uvs
  13709. for ( let j = 0; j <= radialSegments; j ++ ) {
  13710. for ( let i = 0; i <= tubularSegments; i ++ ) {
  13711. const u = i / tubularSegments * arc;
  13712. const v = j / radialSegments * Math.PI * 2;
  13713. // vertex
  13714. vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u );
  13715. vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u );
  13716. vertex.z = tube * Math.sin( v );
  13717. vertices.push( vertex.x, vertex.y, vertex.z );
  13718. // normal
  13719. center.x = radius * Math.cos( u );
  13720. center.y = radius * Math.sin( u );
  13721. normal.subVectors( vertex, center ).normalize();
  13722. normals.push( normal.x, normal.y, normal.z );
  13723. // uv
  13724. uvs.push( i / tubularSegments );
  13725. uvs.push( j / radialSegments );
  13726. }
  13727. }
  13728. // generate indices
  13729. for ( let j = 1; j <= radialSegments; j ++ ) {
  13730. for ( let i = 1; i <= tubularSegments; i ++ ) {
  13731. // indices
  13732. const a = ( tubularSegments + 1 ) * j + i - 1;
  13733. const b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1;
  13734. const c = ( tubularSegments + 1 ) * ( j - 1 ) + i;
  13735. const d = ( tubularSegments + 1 ) * j + i;
  13736. // faces
  13737. indices.push( a, b, d );
  13738. indices.push( b, c, d );
  13739. }
  13740. }
  13741. // build geometry
  13742. this.setIndex( indices );
  13743. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13744. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13745. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13746. }
  13747. copy( source ) {
  13748. super.copy( source );
  13749. this.parameters = Object.assign( {}, source.parameters );
  13750. return this;
  13751. }
  13752. static fromJSON( data ) {
  13753. return new TorusGeometry( data.radius, data.tube, data.radialSegments, data.tubularSegments, data.arc );
  13754. }
  13755. }
  13756. class TorusKnotGeometry extends BufferGeometry {
  13757. constructor( radius = 1, tube = 0.4, tubularSegments = 64, radialSegments = 8, p = 2, q = 3 ) {
  13758. super();
  13759. this.type = 'TorusKnotGeometry';
  13760. this.parameters = {
  13761. radius: radius,
  13762. tube: tube,
  13763. tubularSegments: tubularSegments,
  13764. radialSegments: radialSegments,
  13765. p: p,
  13766. q: q
  13767. };
  13768. tubularSegments = Math.floor( tubularSegments );
  13769. radialSegments = Math.floor( radialSegments );
  13770. // buffers
  13771. const indices = [];
  13772. const vertices = [];
  13773. const normals = [];
  13774. const uvs = [];
  13775. // helper variables
  13776. const vertex = new Vector3();
  13777. const normal = new Vector3();
  13778. const P1 = new Vector3();
  13779. const P2 = new Vector3();
  13780. const B = new Vector3();
  13781. const T = new Vector3();
  13782. const N = new Vector3();
  13783. // generate vertices, normals and uvs
  13784. for ( let i = 0; i <= tubularSegments; ++ i ) {
  13785. // the radian "u" is used to calculate the position on the torus curve of the current tubular segment
  13786. const u = i / tubularSegments * p * Math.PI * 2;
  13787. // now we calculate two points. P1 is our current position on the curve, P2 is a little farther ahead.
  13788. // these points are used to create a special "coordinate space", which is necessary to calculate the correct vertex positions
  13789. calculatePositionOnCurve( u, p, q, radius, P1 );
  13790. calculatePositionOnCurve( u + 0.01, p, q, radius, P2 );
  13791. // calculate orthonormal basis
  13792. T.subVectors( P2, P1 );
  13793. N.addVectors( P2, P1 );
  13794. B.crossVectors( T, N );
  13795. N.crossVectors( B, T );
  13796. // normalize B, N. T can be ignored, we don't use it
  13797. B.normalize();
  13798. N.normalize();
  13799. for ( let j = 0; j <= radialSegments; ++ j ) {
  13800. // now calculate the vertices. they are nothing more than an extrusion of the torus curve.
  13801. // because we extrude a shape in the xy-plane, there is no need to calculate a z-value.
  13802. const v = j / radialSegments * Math.PI * 2;
  13803. const cx = - tube * Math.cos( v );
  13804. const cy = tube * Math.sin( v );
  13805. // now calculate the final vertex position.
  13806. // first we orient the extrusion with our basis vectors, then we add it to the current position on the curve
  13807. vertex.x = P1.x + ( cx * N.x + cy * B.x );
  13808. vertex.y = P1.y + ( cx * N.y + cy * B.y );
  13809. vertex.z = P1.z + ( cx * N.z + cy * B.z );
  13810. vertices.push( vertex.x, vertex.y, vertex.z );
  13811. // normal (P1 is always the center/origin of the extrusion, thus we can use it to calculate the normal)
  13812. normal.subVectors( vertex, P1 ).normalize();
  13813. normals.push( normal.x, normal.y, normal.z );
  13814. // uv
  13815. uvs.push( i / tubularSegments );
  13816. uvs.push( j / radialSegments );
  13817. }
  13818. }
  13819. // generate indices
  13820. for ( let j = 1; j <= tubularSegments; j ++ ) {
  13821. for ( let i = 1; i <= radialSegments; i ++ ) {
  13822. // indices
  13823. const a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
  13824. const b = ( radialSegments + 1 ) * j + ( i - 1 );
  13825. const c = ( radialSegments + 1 ) * j + i;
  13826. const d = ( radialSegments + 1 ) * ( j - 1 ) + i;
  13827. // faces
  13828. indices.push( a, b, d );
  13829. indices.push( b, c, d );
  13830. }
  13831. }
  13832. // build geometry
  13833. this.setIndex( indices );
  13834. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13835. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13836. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13837. // this function calculates the current position on the torus curve
  13838. function calculatePositionOnCurve( u, p, q, radius, position ) {
  13839. const cu = Math.cos( u );
  13840. const su = Math.sin( u );
  13841. const quOverP = q / p * u;
  13842. const cs = Math.cos( quOverP );
  13843. position.x = radius * ( 2 + cs ) * 0.5 * cu;
  13844. position.y = radius * ( 2 + cs ) * su * 0.5;
  13845. position.z = radius * Math.sin( quOverP ) * 0.5;
  13846. }
  13847. }
  13848. copy( source ) {
  13849. super.copy( source );
  13850. this.parameters = Object.assign( {}, source.parameters );
  13851. return this;
  13852. }
  13853. static fromJSON( data ) {
  13854. return new TorusKnotGeometry( data.radius, data.tube, data.tubularSegments, data.radialSegments, data.p, data.q );
  13855. }
  13856. }
  13857. class TubeGeometry extends BufferGeometry {
  13858. constructor( path = new QuadraticBezierCurve3( new Vector3( - 1, - 1, 0 ), new Vector3( - 1, 1, 0 ), new Vector3( 1, 1, 0 ) ), tubularSegments = 64, radius = 1, radialSegments = 8, closed = false ) {
  13859. super();
  13860. this.type = 'TubeGeometry';
  13861. this.parameters = {
  13862. path: path,
  13863. tubularSegments: tubularSegments,
  13864. radius: radius,
  13865. radialSegments: radialSegments,
  13866. closed: closed
  13867. };
  13868. const frames = path.computeFrenetFrames( tubularSegments, closed );
  13869. // expose internals
  13870. this.tangents = frames.tangents;
  13871. this.normals = frames.normals;
  13872. this.binormals = frames.binormals;
  13873. // helper variables
  13874. const vertex = new Vector3();
  13875. const normal = new Vector3();
  13876. const uv = new Vector2();
  13877. let P = new Vector3();
  13878. // buffer
  13879. const vertices = [];
  13880. const normals = [];
  13881. const uvs = [];
  13882. const indices = [];
  13883. // create buffer data
  13884. generateBufferData();
  13885. // build geometry
  13886. this.setIndex( indices );
  13887. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13888. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13889. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13890. // functions
  13891. function generateBufferData() {
  13892. for ( let i = 0; i < tubularSegments; i ++ ) {
  13893. generateSegment( i );
  13894. }
  13895. // if the geometry is not closed, generate the last row of vertices and normals
  13896. // at the regular position on the given path
  13897. //
  13898. // if the geometry is closed, duplicate the first row of vertices and normals (uvs will differ)
  13899. generateSegment( ( closed === false ) ? tubularSegments : 0 );
  13900. // uvs are generated in a separate function.
  13901. // this makes it easy compute correct values for closed geometries
  13902. generateUVs();
  13903. // finally create faces
  13904. generateIndices();
  13905. }
  13906. function generateSegment( i ) {
  13907. // we use getPointAt to sample evenly distributed points from the given path
  13908. P = path.getPointAt( i / tubularSegments, P );
  13909. // retrieve corresponding normal and binormal
  13910. const N = frames.normals[ i ];
  13911. const B = frames.binormals[ i ];
  13912. // generate normals and vertices for the current segment
  13913. for ( let j = 0; j <= radialSegments; j ++ ) {
  13914. const v = j / radialSegments * Math.PI * 2;
  13915. const sin = Math.sin( v );
  13916. const cos = - Math.cos( v );
  13917. // normal
  13918. normal.x = ( cos * N.x + sin * B.x );
  13919. normal.y = ( cos * N.y + sin * B.y );
  13920. normal.z = ( cos * N.z + sin * B.z );
  13921. normal.normalize();
  13922. normals.push( normal.x, normal.y, normal.z );
  13923. // vertex
  13924. vertex.x = P.x + radius * normal.x;
  13925. vertex.y = P.y + radius * normal.y;
  13926. vertex.z = P.z + radius * normal.z;
  13927. vertices.push( vertex.x, vertex.y, vertex.z );
  13928. }
  13929. }
  13930. function generateIndices() {
  13931. for ( let j = 1; j <= tubularSegments; j ++ ) {
  13932. for ( let i = 1; i <= radialSegments; i ++ ) {
  13933. const a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
  13934. const b = ( radialSegments + 1 ) * j + ( i - 1 );
  13935. const c = ( radialSegments + 1 ) * j + i;
  13936. const d = ( radialSegments + 1 ) * ( j - 1 ) + i;
  13937. // faces
  13938. indices.push( a, b, d );
  13939. indices.push( b, c, d );
  13940. }
  13941. }
  13942. }
  13943. function generateUVs() {
  13944. for ( let i = 0; i <= tubularSegments; i ++ ) {
  13945. for ( let j = 0; j <= radialSegments; j ++ ) {
  13946. uv.x = i / tubularSegments;
  13947. uv.y = j / radialSegments;
  13948. uvs.push( uv.x, uv.y );
  13949. }
  13950. }
  13951. }
  13952. }
  13953. copy( source ) {
  13954. super.copy( source );
  13955. this.parameters = Object.assign( {}, source.parameters );
  13956. return this;
  13957. }
  13958. toJSON() {
  13959. const data = super.toJSON();
  13960. data.path = this.parameters.path.toJSON();
  13961. return data;
  13962. }
  13963. static fromJSON( data ) {
  13964. // This only works for built-in curves (e.g. CatmullRomCurve3).
  13965. // User defined curves or instances of CurvePath will not be deserialized.
  13966. return new TubeGeometry(
  13967. new Curves[ data.path.type ]().fromJSON( data.path ),
  13968. data.tubularSegments,
  13969. data.radius,
  13970. data.radialSegments,
  13971. data.closed
  13972. );
  13973. }
  13974. }
  13975. class WireframeGeometry extends BufferGeometry {
  13976. constructor( geometry = null ) {
  13977. super();
  13978. this.type = 'WireframeGeometry';
  13979. this.parameters = {
  13980. geometry: geometry
  13981. };
  13982. if ( geometry !== null ) {
  13983. // buffer
  13984. const vertices = [];
  13985. const edges = new Set();
  13986. // helper variables
  13987. const start = new Vector3();
  13988. const end = new Vector3();
  13989. if ( geometry.index !== null ) {
  13990. // indexed BufferGeometry
  13991. const position = geometry.attributes.position;
  13992. const indices = geometry.index;
  13993. let groups = geometry.groups;
  13994. if ( groups.length === 0 ) {
  13995. groups = [ { start: 0, count: indices.count, materialIndex: 0 } ];
  13996. }
  13997. // create a data structure that contains all edges without duplicates
  13998. for ( let o = 0, ol = groups.length; o < ol; ++ o ) {
  13999. const group = groups[ o ];
  14000. const groupStart = group.start;
  14001. const groupCount = group.count;
  14002. for ( let i = groupStart, l = ( groupStart + groupCount ); i < l; i += 3 ) {
  14003. for ( let j = 0; j < 3; j ++ ) {
  14004. const index1 = indices.getX( i + j );
  14005. const index2 = indices.getX( i + ( j + 1 ) % 3 );
  14006. start.fromBufferAttribute( position, index1 );
  14007. end.fromBufferAttribute( position, index2 );
  14008. if ( isUniqueEdge( start, end, edges ) === true ) {
  14009. vertices.push( start.x, start.y, start.z );
  14010. vertices.push( end.x, end.y, end.z );
  14011. }
  14012. }
  14013. }
  14014. }
  14015. } else {
  14016. // non-indexed BufferGeometry
  14017. const position = geometry.attributes.position;
  14018. for ( let i = 0, l = ( position.count / 3 ); i < l; i ++ ) {
  14019. for ( let j = 0; j < 3; j ++ ) {
  14020. // three edges per triangle, an edge is represented as (index1, index2)
  14021. // e.g. the first triangle has the following edges: (0,1),(1,2),(2,0)
  14022. const index1 = 3 * i + j;
  14023. const index2 = 3 * i + ( ( j + 1 ) % 3 );
  14024. start.fromBufferAttribute( position, index1 );
  14025. end.fromBufferAttribute( position, index2 );
  14026. if ( isUniqueEdge( start, end, edges ) === true ) {
  14027. vertices.push( start.x, start.y, start.z );
  14028. vertices.push( end.x, end.y, end.z );
  14029. }
  14030. }
  14031. }
  14032. }
  14033. // build geometry
  14034. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  14035. }
  14036. }
  14037. copy( source ) {
  14038. super.copy( source );
  14039. this.parameters = Object.assign( {}, source.parameters );
  14040. return this;
  14041. }
  14042. }
  14043. function isUniqueEdge( start, end, edges ) {
  14044. const hash1 = `${start.x},${start.y},${start.z}-${end.x},${end.y},${end.z}`;
  14045. const hash2 = `${end.x},${end.y},${end.z}-${start.x},${start.y},${start.z}`; // coincident edge
  14046. if ( edges.has( hash1 ) === true || edges.has( hash2 ) === true ) {
  14047. return false;
  14048. } else {
  14049. edges.add( hash1 );
  14050. edges.add( hash2 );
  14051. return true;
  14052. }
  14053. }
  14054. var Geometries = /*#__PURE__*/Object.freeze({
  14055. __proto__: null,
  14056. BoxGeometry: BoxGeometry,
  14057. CapsuleGeometry: CapsuleGeometry,
  14058. CircleGeometry: CircleGeometry,
  14059. ConeGeometry: ConeGeometry,
  14060. CylinderGeometry: CylinderGeometry,
  14061. DodecahedronGeometry: DodecahedronGeometry,
  14062. EdgesGeometry: EdgesGeometry,
  14063. ExtrudeGeometry: ExtrudeGeometry,
  14064. IcosahedronGeometry: IcosahedronGeometry,
  14065. LatheGeometry: LatheGeometry,
  14066. OctahedronGeometry: OctahedronGeometry,
  14067. PlaneGeometry: PlaneGeometry,
  14068. PolyhedronGeometry: PolyhedronGeometry,
  14069. RingGeometry: RingGeometry,
  14070. ShapeGeometry: ShapeGeometry,
  14071. SphereGeometry: SphereGeometry,
  14072. TetrahedronGeometry: TetrahedronGeometry,
  14073. TorusGeometry: TorusGeometry,
  14074. TorusKnotGeometry: TorusKnotGeometry,
  14075. TubeGeometry: TubeGeometry,
  14076. WireframeGeometry: WireframeGeometry
  14077. });
  14078. class ShadowMaterial extends Material {
  14079. constructor( parameters ) {
  14080. super();
  14081. this.isShadowMaterial = true;
  14082. this.type = 'ShadowMaterial';
  14083. this.color = new Color( 0x000000 );
  14084. this.transparent = true;
  14085. this.fog = true;
  14086. this.setValues( parameters );
  14087. }
  14088. copy( source ) {
  14089. super.copy( source );
  14090. this.color.copy( source.color );
  14091. this.fog = source.fog;
  14092. return this;
  14093. }
  14094. }
  14095. class RawShaderMaterial extends ShaderMaterial {
  14096. constructor( parameters ) {
  14097. super( parameters );
  14098. this.isRawShaderMaterial = true;
  14099. this.type = 'RawShaderMaterial';
  14100. }
  14101. }
  14102. class MeshStandardMaterial extends Material {
  14103. constructor( parameters ) {
  14104. super();
  14105. this.isMeshStandardMaterial = true;
  14106. this.type = 'MeshStandardMaterial';
  14107. this.defines = { 'STANDARD': '' };
  14108. this.color = new Color( 0xffffff ); // diffuse
  14109. this.roughness = 1.0;
  14110. this.metalness = 0.0;
  14111. this.map = null;
  14112. this.lightMap = null;
  14113. this.lightMapIntensity = 1.0;
  14114. this.aoMap = null;
  14115. this.aoMapIntensity = 1.0;
  14116. this.emissive = new Color( 0x000000 );
  14117. this.emissiveIntensity = 1.0;
  14118. this.emissiveMap = null;
  14119. this.bumpMap = null;
  14120. this.bumpScale = 1;
  14121. this.normalMap = null;
  14122. this.normalMapType = TangentSpaceNormalMap;
  14123. this.normalScale = new Vector2( 1, 1 );
  14124. this.displacementMap = null;
  14125. this.displacementScale = 1;
  14126. this.displacementBias = 0;
  14127. this.roughnessMap = null;
  14128. this.metalnessMap = null;
  14129. this.alphaMap = null;
  14130. this.envMap = null;
  14131. this.envMapRotation = new Euler();
  14132. this.envMapIntensity = 1.0;
  14133. this.wireframe = false;
  14134. this.wireframeLinewidth = 1;
  14135. this.wireframeLinecap = 'round';
  14136. this.wireframeLinejoin = 'round';
  14137. this.flatShading = false;
  14138. this.fog = true;
  14139. this.setValues( parameters );
  14140. }
  14141. copy( source ) {
  14142. super.copy( source );
  14143. this.defines = { 'STANDARD': '' };
  14144. this.color.copy( source.color );
  14145. this.roughness = source.roughness;
  14146. this.metalness = source.metalness;
  14147. this.map = source.map;
  14148. this.lightMap = source.lightMap;
  14149. this.lightMapIntensity = source.lightMapIntensity;
  14150. this.aoMap = source.aoMap;
  14151. this.aoMapIntensity = source.aoMapIntensity;
  14152. this.emissive.copy( source.emissive );
  14153. this.emissiveMap = source.emissiveMap;
  14154. this.emissiveIntensity = source.emissiveIntensity;
  14155. this.bumpMap = source.bumpMap;
  14156. this.bumpScale = source.bumpScale;
  14157. this.normalMap = source.normalMap;
  14158. this.normalMapType = source.normalMapType;
  14159. this.normalScale.copy( source.normalScale );
  14160. this.displacementMap = source.displacementMap;
  14161. this.displacementScale = source.displacementScale;
  14162. this.displacementBias = source.displacementBias;
  14163. this.roughnessMap = source.roughnessMap;
  14164. this.metalnessMap = source.metalnessMap;
  14165. this.alphaMap = source.alphaMap;
  14166. this.envMap = source.envMap;
  14167. this.envMapRotation.copy( source.envMapRotation );
  14168. this.envMapIntensity = source.envMapIntensity;
  14169. this.wireframe = source.wireframe;
  14170. this.wireframeLinewidth = source.wireframeLinewidth;
  14171. this.wireframeLinecap = source.wireframeLinecap;
  14172. this.wireframeLinejoin = source.wireframeLinejoin;
  14173. this.flatShading = source.flatShading;
  14174. this.fog = source.fog;
  14175. return this;
  14176. }
  14177. }
  14178. class MeshPhysicalMaterial extends MeshStandardMaterial {
  14179. constructor( parameters ) {
  14180. super();
  14181. this.isMeshPhysicalMaterial = true;
  14182. this.defines = {
  14183. 'STANDARD': '',
  14184. 'PHYSICAL': ''
  14185. };
  14186. this.type = 'MeshPhysicalMaterial';
  14187. this.anisotropyRotation = 0;
  14188. this.anisotropyMap = null;
  14189. this.clearcoatMap = null;
  14190. this.clearcoatRoughness = 0.0;
  14191. this.clearcoatRoughnessMap = null;
  14192. this.clearcoatNormalScale = new Vector2( 1, 1 );
  14193. this.clearcoatNormalMap = null;
  14194. this.ior = 1.5;
  14195. Object.defineProperty( this, 'reflectivity', {
  14196. get: function () {
  14197. return ( clamp( 2.5 * ( this.ior - 1 ) / ( this.ior + 1 ), 0, 1 ) );
  14198. },
  14199. set: function ( reflectivity ) {
  14200. this.ior = ( 1 + 0.4 * reflectivity ) / ( 1 - 0.4 * reflectivity );
  14201. }
  14202. } );
  14203. this.iridescenceMap = null;
  14204. this.iridescenceIOR = 1.3;
  14205. this.iridescenceThicknessRange = [ 100, 400 ];
  14206. this.iridescenceThicknessMap = null;
  14207. this.sheenColor = new Color( 0x000000 );
  14208. this.sheenColorMap = null;
  14209. this.sheenRoughness = 1.0;
  14210. this.sheenRoughnessMap = null;
  14211. this.transmissionMap = null;
  14212. this.thickness = 0;
  14213. this.thicknessMap = null;
  14214. this.attenuationDistance = Infinity;
  14215. this.attenuationColor = new Color( 1, 1, 1 );
  14216. this.specularIntensity = 1.0;
  14217. this.specularIntensityMap = null;
  14218. this.specularColor = new Color( 1, 1, 1 );
  14219. this.specularColorMap = null;
  14220. this._anisotropy = 0;
  14221. this._clearcoat = 0;
  14222. this._dispersion = 0;
  14223. this._iridescence = 0;
  14224. this._sheen = 0.0;
  14225. this._transmission = 0;
  14226. this.setValues( parameters );
  14227. }
  14228. get anisotropy() {
  14229. return this._anisotropy;
  14230. }
  14231. set anisotropy( value ) {
  14232. if ( this._anisotropy > 0 !== value > 0 ) {
  14233. this.version ++;
  14234. }
  14235. this._anisotropy = value;
  14236. }
  14237. get clearcoat() {
  14238. return this._clearcoat;
  14239. }
  14240. set clearcoat( value ) {
  14241. if ( this._clearcoat > 0 !== value > 0 ) {
  14242. this.version ++;
  14243. }
  14244. this._clearcoat = value;
  14245. }
  14246. get iridescence() {
  14247. return this._iridescence;
  14248. }
  14249. set iridescence( value ) {
  14250. if ( this._iridescence > 0 !== value > 0 ) {
  14251. this.version ++;
  14252. }
  14253. this._iridescence = value;
  14254. }
  14255. get dispersion() {
  14256. return this._dispersion;
  14257. }
  14258. set dispersion( value ) {
  14259. if ( this._dispersion > 0 !== value > 0 ) {
  14260. this.version ++;
  14261. }
  14262. this._dispersion = value;
  14263. }
  14264. get sheen() {
  14265. return this._sheen;
  14266. }
  14267. set sheen( value ) {
  14268. if ( this._sheen > 0 !== value > 0 ) {
  14269. this.version ++;
  14270. }
  14271. this._sheen = value;
  14272. }
  14273. get transmission() {
  14274. return this._transmission;
  14275. }
  14276. set transmission( value ) {
  14277. if ( this._transmission > 0 !== value > 0 ) {
  14278. this.version ++;
  14279. }
  14280. this._transmission = value;
  14281. }
  14282. copy( source ) {
  14283. super.copy( source );
  14284. this.defines = {
  14285. 'STANDARD': '',
  14286. 'PHYSICAL': ''
  14287. };
  14288. this.anisotropy = source.anisotropy;
  14289. this.anisotropyRotation = source.anisotropyRotation;
  14290. this.anisotropyMap = source.anisotropyMap;
  14291. this.clearcoat = source.clearcoat;
  14292. this.clearcoatMap = source.clearcoatMap;
  14293. this.clearcoatRoughness = source.clearcoatRoughness;
  14294. this.clearcoatRoughnessMap = source.clearcoatRoughnessMap;
  14295. this.clearcoatNormalMap = source.clearcoatNormalMap;
  14296. this.clearcoatNormalScale.copy( source.clearcoatNormalScale );
  14297. this.dispersion = source.dispersion;
  14298. this.ior = source.ior;
  14299. this.iridescence = source.iridescence;
  14300. this.iridescenceMap = source.iridescenceMap;
  14301. this.iridescenceIOR = source.iridescenceIOR;
  14302. this.iridescenceThicknessRange = [ ...source.iridescenceThicknessRange ];
  14303. this.iridescenceThicknessMap = source.iridescenceThicknessMap;
  14304. this.sheen = source.sheen;
  14305. this.sheenColor.copy( source.sheenColor );
  14306. this.sheenColorMap = source.sheenColorMap;
  14307. this.sheenRoughness = source.sheenRoughness;
  14308. this.sheenRoughnessMap = source.sheenRoughnessMap;
  14309. this.transmission = source.transmission;
  14310. this.transmissionMap = source.transmissionMap;
  14311. this.thickness = source.thickness;
  14312. this.thicknessMap = source.thicknessMap;
  14313. this.attenuationDistance = source.attenuationDistance;
  14314. this.attenuationColor.copy( source.attenuationColor );
  14315. this.specularIntensity = source.specularIntensity;
  14316. this.specularIntensityMap = source.specularIntensityMap;
  14317. this.specularColor.copy( source.specularColor );
  14318. this.specularColorMap = source.specularColorMap;
  14319. return this;
  14320. }
  14321. }
  14322. class MeshPhongMaterial extends Material {
  14323. constructor( parameters ) {
  14324. super();
  14325. this.isMeshPhongMaterial = true;
  14326. this.type = 'MeshPhongMaterial';
  14327. this.color = new Color( 0xffffff ); // diffuse
  14328. this.specular = new Color( 0x111111 );
  14329. this.shininess = 30;
  14330. this.map = null;
  14331. this.lightMap = null;
  14332. this.lightMapIntensity = 1.0;
  14333. this.aoMap = null;
  14334. this.aoMapIntensity = 1.0;
  14335. this.emissive = new Color( 0x000000 );
  14336. this.emissiveIntensity = 1.0;
  14337. this.emissiveMap = null;
  14338. this.bumpMap = null;
  14339. this.bumpScale = 1;
  14340. this.normalMap = null;
  14341. this.normalMapType = TangentSpaceNormalMap;
  14342. this.normalScale = new Vector2( 1, 1 );
  14343. this.displacementMap = null;
  14344. this.displacementScale = 1;
  14345. this.displacementBias = 0;
  14346. this.specularMap = null;
  14347. this.alphaMap = null;
  14348. this.envMap = null;
  14349. this.envMapRotation = new Euler();
  14350. this.combine = MultiplyOperation;
  14351. this.reflectivity = 1;
  14352. this.refractionRatio = 0.98;
  14353. this.wireframe = false;
  14354. this.wireframeLinewidth = 1;
  14355. this.wireframeLinecap = 'round';
  14356. this.wireframeLinejoin = 'round';
  14357. this.flatShading = false;
  14358. this.fog = true;
  14359. this.setValues( parameters );
  14360. }
  14361. copy( source ) {
  14362. super.copy( source );
  14363. this.color.copy( source.color );
  14364. this.specular.copy( source.specular );
  14365. this.shininess = source.shininess;
  14366. this.map = source.map;
  14367. this.lightMap = source.lightMap;
  14368. this.lightMapIntensity = source.lightMapIntensity;
  14369. this.aoMap = source.aoMap;
  14370. this.aoMapIntensity = source.aoMapIntensity;
  14371. this.emissive.copy( source.emissive );
  14372. this.emissiveMap = source.emissiveMap;
  14373. this.emissiveIntensity = source.emissiveIntensity;
  14374. this.bumpMap = source.bumpMap;
  14375. this.bumpScale = source.bumpScale;
  14376. this.normalMap = source.normalMap;
  14377. this.normalMapType = source.normalMapType;
  14378. this.normalScale.copy( source.normalScale );
  14379. this.displacementMap = source.displacementMap;
  14380. this.displacementScale = source.displacementScale;
  14381. this.displacementBias = source.displacementBias;
  14382. this.specularMap = source.specularMap;
  14383. this.alphaMap = source.alphaMap;
  14384. this.envMap = source.envMap;
  14385. this.envMapRotation.copy( source.envMapRotation );
  14386. this.combine = source.combine;
  14387. this.reflectivity = source.reflectivity;
  14388. this.refractionRatio = source.refractionRatio;
  14389. this.wireframe = source.wireframe;
  14390. this.wireframeLinewidth = source.wireframeLinewidth;
  14391. this.wireframeLinecap = source.wireframeLinecap;
  14392. this.wireframeLinejoin = source.wireframeLinejoin;
  14393. this.flatShading = source.flatShading;
  14394. this.fog = source.fog;
  14395. return this;
  14396. }
  14397. }
  14398. class MeshToonMaterial extends Material {
  14399. constructor( parameters ) {
  14400. super();
  14401. this.isMeshToonMaterial = true;
  14402. this.defines = { 'TOON': '' };
  14403. this.type = 'MeshToonMaterial';
  14404. this.color = new Color( 0xffffff );
  14405. this.map = null;
  14406. this.gradientMap = null;
  14407. this.lightMap = null;
  14408. this.lightMapIntensity = 1.0;
  14409. this.aoMap = null;
  14410. this.aoMapIntensity = 1.0;
  14411. this.emissive = new Color( 0x000000 );
  14412. this.emissiveIntensity = 1.0;
  14413. this.emissiveMap = null;
  14414. this.bumpMap = null;
  14415. this.bumpScale = 1;
  14416. this.normalMap = null;
  14417. this.normalMapType = TangentSpaceNormalMap;
  14418. this.normalScale = new Vector2( 1, 1 );
  14419. this.displacementMap = null;
  14420. this.displacementScale = 1;
  14421. this.displacementBias = 0;
  14422. this.alphaMap = null;
  14423. this.wireframe = false;
  14424. this.wireframeLinewidth = 1;
  14425. this.wireframeLinecap = 'round';
  14426. this.wireframeLinejoin = 'round';
  14427. this.fog = true;
  14428. this.setValues( parameters );
  14429. }
  14430. copy( source ) {
  14431. super.copy( source );
  14432. this.color.copy( source.color );
  14433. this.map = source.map;
  14434. this.gradientMap = source.gradientMap;
  14435. this.lightMap = source.lightMap;
  14436. this.lightMapIntensity = source.lightMapIntensity;
  14437. this.aoMap = source.aoMap;
  14438. this.aoMapIntensity = source.aoMapIntensity;
  14439. this.emissive.copy( source.emissive );
  14440. this.emissiveMap = source.emissiveMap;
  14441. this.emissiveIntensity = source.emissiveIntensity;
  14442. this.bumpMap = source.bumpMap;
  14443. this.bumpScale = source.bumpScale;
  14444. this.normalMap = source.normalMap;
  14445. this.normalMapType = source.normalMapType;
  14446. this.normalScale.copy( source.normalScale );
  14447. this.displacementMap = source.displacementMap;
  14448. this.displacementScale = source.displacementScale;
  14449. this.displacementBias = source.displacementBias;
  14450. this.alphaMap = source.alphaMap;
  14451. this.wireframe = source.wireframe;
  14452. this.wireframeLinewidth = source.wireframeLinewidth;
  14453. this.wireframeLinecap = source.wireframeLinecap;
  14454. this.wireframeLinejoin = source.wireframeLinejoin;
  14455. this.fog = source.fog;
  14456. return this;
  14457. }
  14458. }
  14459. class MeshNormalMaterial extends Material {
  14460. constructor( parameters ) {
  14461. super();
  14462. this.isMeshNormalMaterial = true;
  14463. this.type = 'MeshNormalMaterial';
  14464. this.bumpMap = null;
  14465. this.bumpScale = 1;
  14466. this.normalMap = null;
  14467. this.normalMapType = TangentSpaceNormalMap;
  14468. this.normalScale = new Vector2( 1, 1 );
  14469. this.displacementMap = null;
  14470. this.displacementScale = 1;
  14471. this.displacementBias = 0;
  14472. this.wireframe = false;
  14473. this.wireframeLinewidth = 1;
  14474. this.flatShading = false;
  14475. this.setValues( parameters );
  14476. }
  14477. copy( source ) {
  14478. super.copy( source );
  14479. this.bumpMap = source.bumpMap;
  14480. this.bumpScale = source.bumpScale;
  14481. this.normalMap = source.normalMap;
  14482. this.normalMapType = source.normalMapType;
  14483. this.normalScale.copy( source.normalScale );
  14484. this.displacementMap = source.displacementMap;
  14485. this.displacementScale = source.displacementScale;
  14486. this.displacementBias = source.displacementBias;
  14487. this.wireframe = source.wireframe;
  14488. this.wireframeLinewidth = source.wireframeLinewidth;
  14489. this.flatShading = source.flatShading;
  14490. return this;
  14491. }
  14492. }
  14493. class MeshLambertMaterial extends Material {
  14494. constructor( parameters ) {
  14495. super();
  14496. this.isMeshLambertMaterial = true;
  14497. this.type = 'MeshLambertMaterial';
  14498. this.color = new Color( 0xffffff ); // diffuse
  14499. this.map = null;
  14500. this.lightMap = null;
  14501. this.lightMapIntensity = 1.0;
  14502. this.aoMap = null;
  14503. this.aoMapIntensity = 1.0;
  14504. this.emissive = new Color( 0x000000 );
  14505. this.emissiveIntensity = 1.0;
  14506. this.emissiveMap = null;
  14507. this.bumpMap = null;
  14508. this.bumpScale = 1;
  14509. this.normalMap = null;
  14510. this.normalMapType = TangentSpaceNormalMap;
  14511. this.normalScale = new Vector2( 1, 1 );
  14512. this.displacementMap = null;
  14513. this.displacementScale = 1;
  14514. this.displacementBias = 0;
  14515. this.specularMap = null;
  14516. this.alphaMap = null;
  14517. this.envMap = null;
  14518. this.envMapRotation = new Euler();
  14519. this.combine = MultiplyOperation;
  14520. this.reflectivity = 1;
  14521. this.refractionRatio = 0.98;
  14522. this.wireframe = false;
  14523. this.wireframeLinewidth = 1;
  14524. this.wireframeLinecap = 'round';
  14525. this.wireframeLinejoin = 'round';
  14526. this.flatShading = false;
  14527. this.fog = true;
  14528. this.setValues( parameters );
  14529. }
  14530. copy( source ) {
  14531. super.copy( source );
  14532. this.color.copy( source.color );
  14533. this.map = source.map;
  14534. this.lightMap = source.lightMap;
  14535. this.lightMapIntensity = source.lightMapIntensity;
  14536. this.aoMap = source.aoMap;
  14537. this.aoMapIntensity = source.aoMapIntensity;
  14538. this.emissive.copy( source.emissive );
  14539. this.emissiveMap = source.emissiveMap;
  14540. this.emissiveIntensity = source.emissiveIntensity;
  14541. this.bumpMap = source.bumpMap;
  14542. this.bumpScale = source.bumpScale;
  14543. this.normalMap = source.normalMap;
  14544. this.normalMapType = source.normalMapType;
  14545. this.normalScale.copy( source.normalScale );
  14546. this.displacementMap = source.displacementMap;
  14547. this.displacementScale = source.displacementScale;
  14548. this.displacementBias = source.displacementBias;
  14549. this.specularMap = source.specularMap;
  14550. this.alphaMap = source.alphaMap;
  14551. this.envMap = source.envMap;
  14552. this.envMapRotation.copy( source.envMapRotation );
  14553. this.combine = source.combine;
  14554. this.reflectivity = source.reflectivity;
  14555. this.refractionRatio = source.refractionRatio;
  14556. this.wireframe = source.wireframe;
  14557. this.wireframeLinewidth = source.wireframeLinewidth;
  14558. this.wireframeLinecap = source.wireframeLinecap;
  14559. this.wireframeLinejoin = source.wireframeLinejoin;
  14560. this.flatShading = source.flatShading;
  14561. this.fog = source.fog;
  14562. return this;
  14563. }
  14564. }
  14565. class MeshDepthMaterial extends Material {
  14566. constructor( parameters ) {
  14567. super();
  14568. this.isMeshDepthMaterial = true;
  14569. this.type = 'MeshDepthMaterial';
  14570. this.depthPacking = BasicDepthPacking;
  14571. this.map = null;
  14572. this.alphaMap = null;
  14573. this.displacementMap = null;
  14574. this.displacementScale = 1;
  14575. this.displacementBias = 0;
  14576. this.wireframe = false;
  14577. this.wireframeLinewidth = 1;
  14578. this.setValues( parameters );
  14579. }
  14580. copy( source ) {
  14581. super.copy( source );
  14582. this.depthPacking = source.depthPacking;
  14583. this.map = source.map;
  14584. this.alphaMap = source.alphaMap;
  14585. this.displacementMap = source.displacementMap;
  14586. this.displacementScale = source.displacementScale;
  14587. this.displacementBias = source.displacementBias;
  14588. this.wireframe = source.wireframe;
  14589. this.wireframeLinewidth = source.wireframeLinewidth;
  14590. return this;
  14591. }
  14592. }
  14593. class MeshDistanceMaterial extends Material {
  14594. constructor( parameters ) {
  14595. super();
  14596. this.isMeshDistanceMaterial = true;
  14597. this.type = 'MeshDistanceMaterial';
  14598. this.map = null;
  14599. this.alphaMap = null;
  14600. this.displacementMap = null;
  14601. this.displacementScale = 1;
  14602. this.displacementBias = 0;
  14603. this.setValues( parameters );
  14604. }
  14605. copy( source ) {
  14606. super.copy( source );
  14607. this.map = source.map;
  14608. this.alphaMap = source.alphaMap;
  14609. this.displacementMap = source.displacementMap;
  14610. this.displacementScale = source.displacementScale;
  14611. this.displacementBias = source.displacementBias;
  14612. return this;
  14613. }
  14614. }
  14615. class MeshMatcapMaterial extends Material {
  14616. constructor( parameters ) {
  14617. super();
  14618. this.isMeshMatcapMaterial = true;
  14619. this.defines = { 'MATCAP': '' };
  14620. this.type = 'MeshMatcapMaterial';
  14621. this.color = new Color( 0xffffff ); // diffuse
  14622. this.matcap = null;
  14623. this.map = null;
  14624. this.bumpMap = null;
  14625. this.bumpScale = 1;
  14626. this.normalMap = null;
  14627. this.normalMapType = TangentSpaceNormalMap;
  14628. this.normalScale = new Vector2( 1, 1 );
  14629. this.displacementMap = null;
  14630. this.displacementScale = 1;
  14631. this.displacementBias = 0;
  14632. this.alphaMap = null;
  14633. this.flatShading = false;
  14634. this.fog = true;
  14635. this.setValues( parameters );
  14636. }
  14637. copy( source ) {
  14638. super.copy( source );
  14639. this.defines = { 'MATCAP': '' };
  14640. this.color.copy( source.color );
  14641. this.matcap = source.matcap;
  14642. this.map = source.map;
  14643. this.bumpMap = source.bumpMap;
  14644. this.bumpScale = source.bumpScale;
  14645. this.normalMap = source.normalMap;
  14646. this.normalMapType = source.normalMapType;
  14647. this.normalScale.copy( source.normalScale );
  14648. this.displacementMap = source.displacementMap;
  14649. this.displacementScale = source.displacementScale;
  14650. this.displacementBias = source.displacementBias;
  14651. this.alphaMap = source.alphaMap;
  14652. this.flatShading = source.flatShading;
  14653. this.fog = source.fog;
  14654. return this;
  14655. }
  14656. }
  14657. class LineDashedMaterial extends LineBasicMaterial {
  14658. constructor( parameters ) {
  14659. super();
  14660. this.isLineDashedMaterial = true;
  14661. this.type = 'LineDashedMaterial';
  14662. this.scale = 1;
  14663. this.dashSize = 3;
  14664. this.gapSize = 1;
  14665. this.setValues( parameters );
  14666. }
  14667. copy( source ) {
  14668. super.copy( source );
  14669. this.scale = source.scale;
  14670. this.dashSize = source.dashSize;
  14671. this.gapSize = source.gapSize;
  14672. return this;
  14673. }
  14674. }
  14675. // converts an array to a specific type
  14676. function convertArray( array, type, forceClone ) {
  14677. if ( ! array || // let 'undefined' and 'null' pass
  14678. ! forceClone && array.constructor === type ) return array;
  14679. if ( typeof type.BYTES_PER_ELEMENT === 'number' ) {
  14680. return new type( array ); // create typed array
  14681. }
  14682. return Array.prototype.slice.call( array ); // create Array
  14683. }
  14684. function isTypedArray( object ) {
  14685. return ArrayBuffer.isView( object ) &&
  14686. ! ( object instanceof DataView );
  14687. }
  14688. // returns an array by which times and values can be sorted
  14689. function getKeyframeOrder( times ) {
  14690. function compareTime( i, j ) {
  14691. return times[ i ] - times[ j ];
  14692. }
  14693. const n = times.length;
  14694. const result = new Array( n );
  14695. for ( let i = 0; i !== n; ++ i ) result[ i ] = i;
  14696. result.sort( compareTime );
  14697. return result;
  14698. }
  14699. // uses the array previously returned by 'getKeyframeOrder' to sort data
  14700. function sortedArray( values, stride, order ) {
  14701. const nValues = values.length;
  14702. const result = new values.constructor( nValues );
  14703. for ( let i = 0, dstOffset = 0; dstOffset !== nValues; ++ i ) {
  14704. const srcOffset = order[ i ] * stride;
  14705. for ( let j = 0; j !== stride; ++ j ) {
  14706. result[ dstOffset ++ ] = values[ srcOffset + j ];
  14707. }
  14708. }
  14709. return result;
  14710. }
  14711. // function for parsing AOS keyframe formats
  14712. function flattenJSON( jsonKeys, times, values, valuePropertyName ) {
  14713. let i = 1, key = jsonKeys[ 0 ];
  14714. while ( key !== undefined && key[ valuePropertyName ] === undefined ) {
  14715. key = jsonKeys[ i ++ ];
  14716. }
  14717. if ( key === undefined ) return; // no data
  14718. let value = key[ valuePropertyName ];
  14719. if ( value === undefined ) return; // no data
  14720. if ( Array.isArray( value ) ) {
  14721. do {
  14722. value = key[ valuePropertyName ];
  14723. if ( value !== undefined ) {
  14724. times.push( key.time );
  14725. values.push.apply( values, value ); // push all elements
  14726. }
  14727. key = jsonKeys[ i ++ ];
  14728. } while ( key !== undefined );
  14729. } else if ( value.toArray !== undefined ) {
  14730. // ...assume THREE.Math-ish
  14731. do {
  14732. value = key[ valuePropertyName ];
  14733. if ( value !== undefined ) {
  14734. times.push( key.time );
  14735. value.toArray( values, values.length );
  14736. }
  14737. key = jsonKeys[ i ++ ];
  14738. } while ( key !== undefined );
  14739. } else {
  14740. // otherwise push as-is
  14741. do {
  14742. value = key[ valuePropertyName ];
  14743. if ( value !== undefined ) {
  14744. times.push( key.time );
  14745. values.push( value );
  14746. }
  14747. key = jsonKeys[ i ++ ];
  14748. } while ( key !== undefined );
  14749. }
  14750. }
  14751. function subclip( sourceClip, name, startFrame, endFrame, fps = 30 ) {
  14752. const clip = sourceClip.clone();
  14753. clip.name = name;
  14754. const tracks = [];
  14755. for ( let i = 0; i < clip.tracks.length; ++ i ) {
  14756. const track = clip.tracks[ i ];
  14757. const valueSize = track.getValueSize();
  14758. const times = [];
  14759. const values = [];
  14760. for ( let j = 0; j < track.times.length; ++ j ) {
  14761. const frame = track.times[ j ] * fps;
  14762. if ( frame < startFrame || frame >= endFrame ) continue;
  14763. times.push( track.times[ j ] );
  14764. for ( let k = 0; k < valueSize; ++ k ) {
  14765. values.push( track.values[ j * valueSize + k ] );
  14766. }
  14767. }
  14768. if ( times.length === 0 ) continue;
  14769. track.times = convertArray( times, track.times.constructor );
  14770. track.values = convertArray( values, track.values.constructor );
  14771. tracks.push( track );
  14772. }
  14773. clip.tracks = tracks;
  14774. // find minimum .times value across all tracks in the trimmed clip
  14775. let minStartTime = Infinity;
  14776. for ( let i = 0; i < clip.tracks.length; ++ i ) {
  14777. if ( minStartTime > clip.tracks[ i ].times[ 0 ] ) {
  14778. minStartTime = clip.tracks[ i ].times[ 0 ];
  14779. }
  14780. }
  14781. // shift all tracks such that clip begins at t=0
  14782. for ( let i = 0; i < clip.tracks.length; ++ i ) {
  14783. clip.tracks[ i ].shift( - 1 * minStartTime );
  14784. }
  14785. clip.resetDuration();
  14786. return clip;
  14787. }
  14788. function makeClipAdditive( targetClip, referenceFrame = 0, referenceClip = targetClip, fps = 30 ) {
  14789. if ( fps <= 0 ) fps = 30;
  14790. const numTracks = referenceClip.tracks.length;
  14791. const referenceTime = referenceFrame / fps;
  14792. // Make each track's values relative to the values at the reference frame
  14793. for ( let i = 0; i < numTracks; ++ i ) {
  14794. const referenceTrack = referenceClip.tracks[ i ];
  14795. const referenceTrackType = referenceTrack.ValueTypeName;
  14796. // Skip this track if it's non-numeric
  14797. if ( referenceTrackType === 'bool' || referenceTrackType === 'string' ) continue;
  14798. // Find the track in the target clip whose name and type matches the reference track
  14799. const targetTrack = targetClip.tracks.find( function ( track ) {
  14800. return track.name === referenceTrack.name
  14801. && track.ValueTypeName === referenceTrackType;
  14802. } );
  14803. if ( targetTrack === undefined ) continue;
  14804. let referenceOffset = 0;
  14805. const referenceValueSize = referenceTrack.getValueSize();
  14806. if ( referenceTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) {
  14807. referenceOffset = referenceValueSize / 3;
  14808. }
  14809. let targetOffset = 0;
  14810. const targetValueSize = targetTrack.getValueSize();
  14811. if ( targetTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) {
  14812. targetOffset = targetValueSize / 3;
  14813. }
  14814. const lastIndex = referenceTrack.times.length - 1;
  14815. let referenceValue;
  14816. // Find the value to subtract out of the track
  14817. if ( referenceTime <= referenceTrack.times[ 0 ] ) {
  14818. // Reference frame is earlier than the first keyframe, so just use the first keyframe
  14819. const startIndex = referenceOffset;
  14820. const endIndex = referenceValueSize - referenceOffset;
  14821. referenceValue = referenceTrack.values.slice( startIndex, endIndex );
  14822. } else if ( referenceTime >= referenceTrack.times[ lastIndex ] ) {
  14823. // Reference frame is after the last keyframe, so just use the last keyframe
  14824. const startIndex = lastIndex * referenceValueSize + referenceOffset;
  14825. const endIndex = startIndex + referenceValueSize - referenceOffset;
  14826. referenceValue = referenceTrack.values.slice( startIndex, endIndex );
  14827. } else {
  14828. // Interpolate to the reference value
  14829. const interpolant = referenceTrack.createInterpolant();
  14830. const startIndex = referenceOffset;
  14831. const endIndex = referenceValueSize - referenceOffset;
  14832. interpolant.evaluate( referenceTime );
  14833. referenceValue = interpolant.resultBuffer.slice( startIndex, endIndex );
  14834. }
  14835. // Conjugate the quaternion
  14836. if ( referenceTrackType === 'quaternion' ) {
  14837. const referenceQuat = new Quaternion().fromArray( referenceValue ).normalize().conjugate();
  14838. referenceQuat.toArray( referenceValue );
  14839. }
  14840. // Subtract the reference value from all of the track values
  14841. const numTimes = targetTrack.times.length;
  14842. for ( let j = 0; j < numTimes; ++ j ) {
  14843. const valueStart = j * targetValueSize + targetOffset;
  14844. if ( referenceTrackType === 'quaternion' ) {
  14845. // Multiply the conjugate for quaternion track types
  14846. Quaternion.multiplyQuaternionsFlat(
  14847. targetTrack.values,
  14848. valueStart,
  14849. referenceValue,
  14850. 0,
  14851. targetTrack.values,
  14852. valueStart
  14853. );
  14854. } else {
  14855. const valueEnd = targetValueSize - targetOffset * 2;
  14856. // Subtract each value for all other numeric track types
  14857. for ( let k = 0; k < valueEnd; ++ k ) {
  14858. targetTrack.values[ valueStart + k ] -= referenceValue[ k ];
  14859. }
  14860. }
  14861. }
  14862. }
  14863. targetClip.blendMode = AdditiveAnimationBlendMode;
  14864. return targetClip;
  14865. }
  14866. const AnimationUtils = {
  14867. convertArray: convertArray,
  14868. isTypedArray: isTypedArray,
  14869. getKeyframeOrder: getKeyframeOrder,
  14870. sortedArray: sortedArray,
  14871. flattenJSON: flattenJSON,
  14872. subclip: subclip,
  14873. makeClipAdditive: makeClipAdditive
  14874. };
  14875. /**
  14876. * Abstract base class of interpolants over parametric samples.
  14877. *
  14878. * The parameter domain is one dimensional, typically the time or a path
  14879. * along a curve defined by the data.
  14880. *
  14881. * The sample values can have any dimensionality and derived classes may
  14882. * apply special interpretations to the data.
  14883. *
  14884. * This class provides the interval seek in a Template Method, deferring
  14885. * the actual interpolation to derived classes.
  14886. *
  14887. * Time complexity is O(1) for linear access crossing at most two points
  14888. * and O(log N) for random access, where N is the number of positions.
  14889. *
  14890. * References:
  14891. *
  14892. * http://www.oodesign.com/template-method-pattern.html
  14893. *
  14894. */
  14895. class Interpolant {
  14896. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  14897. this.parameterPositions = parameterPositions;
  14898. this._cachedIndex = 0;
  14899. this.resultBuffer = resultBuffer !== undefined ?
  14900. resultBuffer : new sampleValues.constructor( sampleSize );
  14901. this.sampleValues = sampleValues;
  14902. this.valueSize = sampleSize;
  14903. this.settings = null;
  14904. this.DefaultSettings_ = {};
  14905. }
  14906. evaluate( t ) {
  14907. const pp = this.parameterPositions;
  14908. let i1 = this._cachedIndex,
  14909. t1 = pp[ i1 ],
  14910. t0 = pp[ i1 - 1 ];
  14911. validate_interval: {
  14912. seek: {
  14913. let right;
  14914. linear_scan: {
  14915. //- See http://jsperf.com/comparison-to-undefined/3
  14916. //- slower code:
  14917. //-
  14918. //- if ( t >= t1 || t1 === undefined ) {
  14919. forward_scan: if ( ! ( t < t1 ) ) {
  14920. for ( let giveUpAt = i1 + 2; ; ) {
  14921. if ( t1 === undefined ) {
  14922. if ( t < t0 ) break forward_scan;
  14923. // after end
  14924. i1 = pp.length;
  14925. this._cachedIndex = i1;
  14926. return this.copySampleValue_( i1 - 1 );
  14927. }
  14928. if ( i1 === giveUpAt ) break; // this loop
  14929. t0 = t1;
  14930. t1 = pp[ ++ i1 ];
  14931. if ( t < t1 ) {
  14932. // we have arrived at the sought interval
  14933. break seek;
  14934. }
  14935. }
  14936. // prepare binary search on the right side of the index
  14937. right = pp.length;
  14938. break linear_scan;
  14939. }
  14940. //- slower code:
  14941. //- if ( t < t0 || t0 === undefined ) {
  14942. if ( ! ( t >= t0 ) ) {
  14943. // looping?
  14944. const t1global = pp[ 1 ];
  14945. if ( t < t1global ) {
  14946. i1 = 2; // + 1, using the scan for the details
  14947. t0 = t1global;
  14948. }
  14949. // linear reverse scan
  14950. for ( let giveUpAt = i1 - 2; ; ) {
  14951. if ( t0 === undefined ) {
  14952. // before start
  14953. this._cachedIndex = 0;
  14954. return this.copySampleValue_( 0 );
  14955. }
  14956. if ( i1 === giveUpAt ) break; // this loop
  14957. t1 = t0;
  14958. t0 = pp[ -- i1 - 1 ];
  14959. if ( t >= t0 ) {
  14960. // we have arrived at the sought interval
  14961. break seek;
  14962. }
  14963. }
  14964. // prepare binary search on the left side of the index
  14965. right = i1;
  14966. i1 = 0;
  14967. break linear_scan;
  14968. }
  14969. // the interval is valid
  14970. break validate_interval;
  14971. } // linear scan
  14972. // binary search
  14973. while ( i1 < right ) {
  14974. const mid = ( i1 + right ) >>> 1;
  14975. if ( t < pp[ mid ] ) {
  14976. right = mid;
  14977. } else {
  14978. i1 = mid + 1;
  14979. }
  14980. }
  14981. t1 = pp[ i1 ];
  14982. t0 = pp[ i1 - 1 ];
  14983. // check boundary cases, again
  14984. if ( t0 === undefined ) {
  14985. this._cachedIndex = 0;
  14986. return this.copySampleValue_( 0 );
  14987. }
  14988. if ( t1 === undefined ) {
  14989. i1 = pp.length;
  14990. this._cachedIndex = i1;
  14991. return this.copySampleValue_( i1 - 1 );
  14992. }
  14993. } // seek
  14994. this._cachedIndex = i1;
  14995. this.intervalChanged_( i1, t0, t1 );
  14996. } // validate_interval
  14997. return this.interpolate_( i1, t0, t, t1 );
  14998. }
  14999. getSettings_() {
  15000. return this.settings || this.DefaultSettings_;
  15001. }
  15002. copySampleValue_( index ) {
  15003. // copies a sample value to the result buffer
  15004. const result = this.resultBuffer,
  15005. values = this.sampleValues,
  15006. stride = this.valueSize,
  15007. offset = index * stride;
  15008. for ( let i = 0; i !== stride; ++ i ) {
  15009. result[ i ] = values[ offset + i ];
  15010. }
  15011. return result;
  15012. }
  15013. // Template methods for derived classes:
  15014. interpolate_( /* i1, t0, t, t1 */ ) {
  15015. throw new Error( 'call to abstract method' );
  15016. // implementations shall return this.resultBuffer
  15017. }
  15018. intervalChanged_( /* i1, t0, t1 */ ) {
  15019. // empty
  15020. }
  15021. }
  15022. /**
  15023. * Fast and simple cubic spline interpolant.
  15024. *
  15025. * It was derived from a Hermitian construction setting the first derivative
  15026. * at each sample position to the linear slope between neighboring positions
  15027. * over their parameter interval.
  15028. */
  15029. class CubicInterpolant extends Interpolant {
  15030. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  15031. super( parameterPositions, sampleValues, sampleSize, resultBuffer );
  15032. this._weightPrev = - 0;
  15033. this._offsetPrev = - 0;
  15034. this._weightNext = - 0;
  15035. this._offsetNext = - 0;
  15036. this.DefaultSettings_ = {
  15037. endingStart: ZeroCurvatureEnding,
  15038. endingEnd: ZeroCurvatureEnding
  15039. };
  15040. }
  15041. intervalChanged_( i1, t0, t1 ) {
  15042. const pp = this.parameterPositions;
  15043. let iPrev = i1 - 2,
  15044. iNext = i1 + 1,
  15045. tPrev = pp[ iPrev ],
  15046. tNext = pp[ iNext ];
  15047. if ( tPrev === undefined ) {
  15048. switch ( this.getSettings_().endingStart ) {
  15049. case ZeroSlopeEnding:
  15050. // f'(t0) = 0
  15051. iPrev = i1;
  15052. tPrev = 2 * t0 - t1;
  15053. break;
  15054. case WrapAroundEnding:
  15055. // use the other end of the curve
  15056. iPrev = pp.length - 2;
  15057. tPrev = t0 + pp[ iPrev ] - pp[ iPrev + 1 ];
  15058. break;
  15059. default: // ZeroCurvatureEnding
  15060. // f''(t0) = 0 a.k.a. Natural Spline
  15061. iPrev = i1;
  15062. tPrev = t1;
  15063. }
  15064. }
  15065. if ( tNext === undefined ) {
  15066. switch ( this.getSettings_().endingEnd ) {
  15067. case ZeroSlopeEnding:
  15068. // f'(tN) = 0
  15069. iNext = i1;
  15070. tNext = 2 * t1 - t0;
  15071. break;
  15072. case WrapAroundEnding:
  15073. // use the other end of the curve
  15074. iNext = 1;
  15075. tNext = t1 + pp[ 1 ] - pp[ 0 ];
  15076. break;
  15077. default: // ZeroCurvatureEnding
  15078. // f''(tN) = 0, a.k.a. Natural Spline
  15079. iNext = i1 - 1;
  15080. tNext = t0;
  15081. }
  15082. }
  15083. const halfDt = ( t1 - t0 ) * 0.5,
  15084. stride = this.valueSize;
  15085. this._weightPrev = halfDt / ( t0 - tPrev );
  15086. this._weightNext = halfDt / ( tNext - t1 );
  15087. this._offsetPrev = iPrev * stride;
  15088. this._offsetNext = iNext * stride;
  15089. }
  15090. interpolate_( i1, t0, t, t1 ) {
  15091. const result = this.resultBuffer,
  15092. values = this.sampleValues,
  15093. stride = this.valueSize,
  15094. o1 = i1 * stride, o0 = o1 - stride,
  15095. oP = this._offsetPrev, oN = this._offsetNext,
  15096. wP = this._weightPrev, wN = this._weightNext,
  15097. p = ( t - t0 ) / ( t1 - t0 ),
  15098. pp = p * p,
  15099. ppp = pp * p;
  15100. // evaluate polynomials
  15101. const sP = - wP * ppp + 2 * wP * pp - wP * p;
  15102. const s0 = ( 1 + wP ) * ppp + ( - 1.5 - 2 * wP ) * pp + ( - 0.5 + wP ) * p + 1;
  15103. const s1 = ( - 1 - wN ) * ppp + ( 1.5 + wN ) * pp + 0.5 * p;
  15104. const sN = wN * ppp - wN * pp;
  15105. // combine data linearly
  15106. for ( let i = 0; i !== stride; ++ i ) {
  15107. result[ i ] =
  15108. sP * values[ oP + i ] +
  15109. s0 * values[ o0 + i ] +
  15110. s1 * values[ o1 + i ] +
  15111. sN * values[ oN + i ];
  15112. }
  15113. return result;
  15114. }
  15115. }
  15116. class LinearInterpolant extends Interpolant {
  15117. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  15118. super( parameterPositions, sampleValues, sampleSize, resultBuffer );
  15119. }
  15120. interpolate_( i1, t0, t, t1 ) {
  15121. const result = this.resultBuffer,
  15122. values = this.sampleValues,
  15123. stride = this.valueSize,
  15124. offset1 = i1 * stride,
  15125. offset0 = offset1 - stride,
  15126. weight1 = ( t - t0 ) / ( t1 - t0 ),
  15127. weight0 = 1 - weight1;
  15128. for ( let i = 0; i !== stride; ++ i ) {
  15129. result[ i ] =
  15130. values[ offset0 + i ] * weight0 +
  15131. values[ offset1 + i ] * weight1;
  15132. }
  15133. return result;
  15134. }
  15135. }
  15136. /**
  15137. *
  15138. * Interpolant that evaluates to the sample value at the position preceding
  15139. * the parameter.
  15140. */
  15141. class DiscreteInterpolant extends Interpolant {
  15142. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  15143. super( parameterPositions, sampleValues, sampleSize, resultBuffer );
  15144. }
  15145. interpolate_( i1 /*, t0, t, t1 */ ) {
  15146. return this.copySampleValue_( i1 - 1 );
  15147. }
  15148. }
  15149. class KeyframeTrack {
  15150. constructor( name, times, values, interpolation ) {
  15151. if ( name === undefined ) throw new Error( 'THREE.KeyframeTrack: track name is undefined' );
  15152. if ( times === undefined || times.length === 0 ) throw new Error( 'THREE.KeyframeTrack: no keyframes in track named ' + name );
  15153. this.name = name;
  15154. this.times = convertArray( times, this.TimeBufferType );
  15155. this.values = convertArray( values, this.ValueBufferType );
  15156. this.setInterpolation( interpolation || this.DefaultInterpolation );
  15157. }
  15158. // Serialization (in static context, because of constructor invocation
  15159. // and automatic invocation of .toJSON):
  15160. static toJSON( track ) {
  15161. const trackType = track.constructor;
  15162. let json;
  15163. // derived classes can define a static toJSON method
  15164. if ( trackType.toJSON !== this.toJSON ) {
  15165. json = trackType.toJSON( track );
  15166. } else {
  15167. // by default, we assume the data can be serialized as-is
  15168. json = {
  15169. 'name': track.name,
  15170. 'times': convertArray( track.times, Array ),
  15171. 'values': convertArray( track.values, Array )
  15172. };
  15173. const interpolation = track.getInterpolation();
  15174. if ( interpolation !== track.DefaultInterpolation ) {
  15175. json.interpolation = interpolation;
  15176. }
  15177. }
  15178. json.type = track.ValueTypeName; // mandatory
  15179. return json;
  15180. }
  15181. InterpolantFactoryMethodDiscrete( result ) {
  15182. return new DiscreteInterpolant( this.times, this.values, this.getValueSize(), result );
  15183. }
  15184. InterpolantFactoryMethodLinear( result ) {
  15185. return new LinearInterpolant( this.times, this.values, this.getValueSize(), result );
  15186. }
  15187. InterpolantFactoryMethodSmooth( result ) {
  15188. return new CubicInterpolant( this.times, this.values, this.getValueSize(), result );
  15189. }
  15190. setInterpolation( interpolation ) {
  15191. let factoryMethod;
  15192. switch ( interpolation ) {
  15193. case InterpolateDiscrete:
  15194. factoryMethod = this.InterpolantFactoryMethodDiscrete;
  15195. break;
  15196. case InterpolateLinear:
  15197. factoryMethod = this.InterpolantFactoryMethodLinear;
  15198. break;
  15199. case InterpolateSmooth:
  15200. factoryMethod = this.InterpolantFactoryMethodSmooth;
  15201. break;
  15202. }
  15203. if ( factoryMethod === undefined ) {
  15204. const message = 'unsupported interpolation for ' +
  15205. this.ValueTypeName + ' keyframe track named ' + this.name;
  15206. if ( this.createInterpolant === undefined ) {
  15207. // fall back to default, unless the default itself is messed up
  15208. if ( interpolation !== this.DefaultInterpolation ) {
  15209. this.setInterpolation( this.DefaultInterpolation );
  15210. } else {
  15211. throw new Error( message ); // fatal, in this case
  15212. }
  15213. }
  15214. console.warn( 'THREE.KeyframeTrack:', message );
  15215. return this;
  15216. }
  15217. this.createInterpolant = factoryMethod;
  15218. return this;
  15219. }
  15220. getInterpolation() {
  15221. switch ( this.createInterpolant ) {
  15222. case this.InterpolantFactoryMethodDiscrete:
  15223. return InterpolateDiscrete;
  15224. case this.InterpolantFactoryMethodLinear:
  15225. return InterpolateLinear;
  15226. case this.InterpolantFactoryMethodSmooth:
  15227. return InterpolateSmooth;
  15228. }
  15229. }
  15230. getValueSize() {
  15231. return this.values.length / this.times.length;
  15232. }
  15233. // move all keyframes either forwards or backwards in time
  15234. shift( timeOffset ) {
  15235. if ( timeOffset !== 0.0 ) {
  15236. const times = this.times;
  15237. for ( let i = 0, n = times.length; i !== n; ++ i ) {
  15238. times[ i ] += timeOffset;
  15239. }
  15240. }
  15241. return this;
  15242. }
  15243. // scale all keyframe times by a factor (useful for frame <-> seconds conversions)
  15244. scale( timeScale ) {
  15245. if ( timeScale !== 1.0 ) {
  15246. const times = this.times;
  15247. for ( let i = 0, n = times.length; i !== n; ++ i ) {
  15248. times[ i ] *= timeScale;
  15249. }
  15250. }
  15251. return this;
  15252. }
  15253. // removes keyframes before and after animation without changing any values within the range [startTime, endTime].
  15254. // IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values
  15255. trim( startTime, endTime ) {
  15256. const times = this.times,
  15257. nKeys = times.length;
  15258. let from = 0,
  15259. to = nKeys - 1;
  15260. while ( from !== nKeys && times[ from ] < startTime ) {
  15261. ++ from;
  15262. }
  15263. while ( to !== - 1 && times[ to ] > endTime ) {
  15264. -- to;
  15265. }
  15266. ++ to; // inclusive -> exclusive bound
  15267. if ( from !== 0 || to !== nKeys ) {
  15268. // empty tracks are forbidden, so keep at least one keyframe
  15269. if ( from >= to ) {
  15270. to = Math.max( to, 1 );
  15271. from = to - 1;
  15272. }
  15273. const stride = this.getValueSize();
  15274. this.times = times.slice( from, to );
  15275. this.values = this.values.slice( from * stride, to * stride );
  15276. }
  15277. return this;
  15278. }
  15279. // ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
  15280. validate() {
  15281. let valid = true;
  15282. const valueSize = this.getValueSize();
  15283. if ( valueSize - Math.floor( valueSize ) !== 0 ) {
  15284. console.error( 'THREE.KeyframeTrack: Invalid value size in track.', this );
  15285. valid = false;
  15286. }
  15287. const times = this.times,
  15288. values = this.values,
  15289. nKeys = times.length;
  15290. if ( nKeys === 0 ) {
  15291. console.error( 'THREE.KeyframeTrack: Track is empty.', this );
  15292. valid = false;
  15293. }
  15294. let prevTime = null;
  15295. for ( let i = 0; i !== nKeys; i ++ ) {
  15296. const currTime = times[ i ];
  15297. if ( typeof currTime === 'number' && isNaN( currTime ) ) {
  15298. console.error( 'THREE.KeyframeTrack: Time is not a valid number.', this, i, currTime );
  15299. valid = false;
  15300. break;
  15301. }
  15302. if ( prevTime !== null && prevTime > currTime ) {
  15303. console.error( 'THREE.KeyframeTrack: Out of order keys.', this, i, currTime, prevTime );
  15304. valid = false;
  15305. break;
  15306. }
  15307. prevTime = currTime;
  15308. }
  15309. if ( values !== undefined ) {
  15310. if ( isTypedArray( values ) ) {
  15311. for ( let i = 0, n = values.length; i !== n; ++ i ) {
  15312. const value = values[ i ];
  15313. if ( isNaN( value ) ) {
  15314. console.error( 'THREE.KeyframeTrack: Value is not a valid number.', this, i, value );
  15315. valid = false;
  15316. break;
  15317. }
  15318. }
  15319. }
  15320. }
  15321. return valid;
  15322. }
  15323. // removes equivalent sequential keys as common in morph target sequences
  15324. // (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0)
  15325. optimize() {
  15326. // times or values may be shared with other tracks, so overwriting is unsafe
  15327. const times = this.times.slice(),
  15328. values = this.values.slice(),
  15329. stride = this.getValueSize(),
  15330. smoothInterpolation = this.getInterpolation() === InterpolateSmooth,
  15331. lastIndex = times.length - 1;
  15332. let writeIndex = 1;
  15333. for ( let i = 1; i < lastIndex; ++ i ) {
  15334. let keep = false;
  15335. const time = times[ i ];
  15336. const timeNext = times[ i + 1 ];
  15337. // remove adjacent keyframes scheduled at the same time
  15338. if ( time !== timeNext && ( i !== 1 || time !== times[ 0 ] ) ) {
  15339. if ( ! smoothInterpolation ) {
  15340. // remove unnecessary keyframes same as their neighbors
  15341. const offset = i * stride,
  15342. offsetP = offset - stride,
  15343. offsetN = offset + stride;
  15344. for ( let j = 0; j !== stride; ++ j ) {
  15345. const value = values[ offset + j ];
  15346. if ( value !== values[ offsetP + j ] ||
  15347. value !== values[ offsetN + j ] ) {
  15348. keep = true;
  15349. break;
  15350. }
  15351. }
  15352. } else {
  15353. keep = true;
  15354. }
  15355. }
  15356. // in-place compaction
  15357. if ( keep ) {
  15358. if ( i !== writeIndex ) {
  15359. times[ writeIndex ] = times[ i ];
  15360. const readOffset = i * stride,
  15361. writeOffset = writeIndex * stride;
  15362. for ( let j = 0; j !== stride; ++ j ) {
  15363. values[ writeOffset + j ] = values[ readOffset + j ];
  15364. }
  15365. }
  15366. ++ writeIndex;
  15367. }
  15368. }
  15369. // flush last keyframe (compaction looks ahead)
  15370. if ( lastIndex > 0 ) {
  15371. times[ writeIndex ] = times[ lastIndex ];
  15372. for ( let readOffset = lastIndex * stride, writeOffset = writeIndex * stride, j = 0; j !== stride; ++ j ) {
  15373. values[ writeOffset + j ] = values[ readOffset + j ];
  15374. }
  15375. ++ writeIndex;
  15376. }
  15377. if ( writeIndex !== times.length ) {
  15378. this.times = times.slice( 0, writeIndex );
  15379. this.values = values.slice( 0, writeIndex * stride );
  15380. } else {
  15381. this.times = times;
  15382. this.values = values;
  15383. }
  15384. return this;
  15385. }
  15386. clone() {
  15387. const times = this.times.slice();
  15388. const values = this.values.slice();
  15389. const TypedKeyframeTrack = this.constructor;
  15390. const track = new TypedKeyframeTrack( this.name, times, values );
  15391. // Interpolant argument to constructor is not saved, so copy the factory method directly.
  15392. track.createInterpolant = this.createInterpolant;
  15393. return track;
  15394. }
  15395. }
  15396. KeyframeTrack.prototype.TimeBufferType = Float32Array;
  15397. KeyframeTrack.prototype.ValueBufferType = Float32Array;
  15398. KeyframeTrack.prototype.DefaultInterpolation = InterpolateLinear;
  15399. /**
  15400. * A Track of Boolean keyframe values.
  15401. */
  15402. class BooleanKeyframeTrack extends KeyframeTrack {
  15403. // No interpolation parameter because only InterpolateDiscrete is valid.
  15404. constructor( name, times, values ) {
  15405. super( name, times, values );
  15406. }
  15407. }
  15408. BooleanKeyframeTrack.prototype.ValueTypeName = 'bool';
  15409. BooleanKeyframeTrack.prototype.ValueBufferType = Array;
  15410. BooleanKeyframeTrack.prototype.DefaultInterpolation = InterpolateDiscrete;
  15411. BooleanKeyframeTrack.prototype.InterpolantFactoryMethodLinear = undefined;
  15412. BooleanKeyframeTrack.prototype.InterpolantFactoryMethodSmooth = undefined;
  15413. /**
  15414. * A Track of keyframe values that represent color.
  15415. */
  15416. class ColorKeyframeTrack extends KeyframeTrack {}
  15417. ColorKeyframeTrack.prototype.ValueTypeName = 'color';
  15418. /**
  15419. * A Track of numeric keyframe values.
  15420. */
  15421. class NumberKeyframeTrack extends KeyframeTrack {}
  15422. NumberKeyframeTrack.prototype.ValueTypeName = 'number';
  15423. /**
  15424. * Spherical linear unit quaternion interpolant.
  15425. */
  15426. class QuaternionLinearInterpolant extends Interpolant {
  15427. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  15428. super( parameterPositions, sampleValues, sampleSize, resultBuffer );
  15429. }
  15430. interpolate_( i1, t0, t, t1 ) {
  15431. const result = this.resultBuffer,
  15432. values = this.sampleValues,
  15433. stride = this.valueSize,
  15434. alpha = ( t - t0 ) / ( t1 - t0 );
  15435. let offset = i1 * stride;
  15436. for ( let end = offset + stride; offset !== end; offset += 4 ) {
  15437. Quaternion.slerpFlat( result, 0, values, offset - stride, values, offset, alpha );
  15438. }
  15439. return result;
  15440. }
  15441. }
  15442. /**
  15443. * A Track of quaternion keyframe values.
  15444. */
  15445. class QuaternionKeyframeTrack extends KeyframeTrack {
  15446. InterpolantFactoryMethodLinear( result ) {
  15447. return new QuaternionLinearInterpolant( this.times, this.values, this.getValueSize(), result );
  15448. }
  15449. }
  15450. QuaternionKeyframeTrack.prototype.ValueTypeName = 'quaternion';
  15451. // ValueBufferType is inherited
  15452. // DefaultInterpolation is inherited;
  15453. QuaternionKeyframeTrack.prototype.InterpolantFactoryMethodSmooth = undefined;
  15454. /**
  15455. * A Track that interpolates Strings
  15456. */
  15457. class StringKeyframeTrack extends KeyframeTrack {
  15458. // No interpolation parameter because only InterpolateDiscrete is valid.
  15459. constructor( name, times, values ) {
  15460. super( name, times, values );
  15461. }
  15462. }
  15463. StringKeyframeTrack.prototype.ValueTypeName = 'string';
  15464. StringKeyframeTrack.prototype.ValueBufferType = Array;
  15465. StringKeyframeTrack.prototype.DefaultInterpolation = InterpolateDiscrete;
  15466. StringKeyframeTrack.prototype.InterpolantFactoryMethodLinear = undefined;
  15467. StringKeyframeTrack.prototype.InterpolantFactoryMethodSmooth = undefined;
  15468. /**
  15469. * A Track of vectored keyframe values.
  15470. */
  15471. class VectorKeyframeTrack extends KeyframeTrack {}
  15472. VectorKeyframeTrack.prototype.ValueTypeName = 'vector';
  15473. class AnimationClip {
  15474. constructor( name = '', duration = - 1, tracks = [], blendMode = NormalAnimationBlendMode ) {
  15475. this.name = name;
  15476. this.tracks = tracks;
  15477. this.duration = duration;
  15478. this.blendMode = blendMode;
  15479. this.uuid = generateUUID();
  15480. // this means it should figure out its duration by scanning the tracks
  15481. if ( this.duration < 0 ) {
  15482. this.resetDuration();
  15483. }
  15484. }
  15485. static parse( json ) {
  15486. const tracks = [],
  15487. jsonTracks = json.tracks,
  15488. frameTime = 1.0 / ( json.fps || 1.0 );
  15489. for ( let i = 0, n = jsonTracks.length; i !== n; ++ i ) {
  15490. tracks.push( parseKeyframeTrack( jsonTracks[ i ] ).scale( frameTime ) );
  15491. }
  15492. const clip = new this( json.name, json.duration, tracks, json.blendMode );
  15493. clip.uuid = json.uuid;
  15494. return clip;
  15495. }
  15496. static toJSON( clip ) {
  15497. const tracks = [],
  15498. clipTracks = clip.tracks;
  15499. const json = {
  15500. 'name': clip.name,
  15501. 'duration': clip.duration,
  15502. 'tracks': tracks,
  15503. 'uuid': clip.uuid,
  15504. 'blendMode': clip.blendMode
  15505. };
  15506. for ( let i = 0, n = clipTracks.length; i !== n; ++ i ) {
  15507. tracks.push( KeyframeTrack.toJSON( clipTracks[ i ] ) );
  15508. }
  15509. return json;
  15510. }
  15511. static CreateFromMorphTargetSequence( name, morphTargetSequence, fps, noLoop ) {
  15512. const numMorphTargets = morphTargetSequence.length;
  15513. const tracks = [];
  15514. for ( let i = 0; i < numMorphTargets; i ++ ) {
  15515. let times = [];
  15516. let values = [];
  15517. times.push(
  15518. ( i + numMorphTargets - 1 ) % numMorphTargets,
  15519. i,
  15520. ( i + 1 ) % numMorphTargets );
  15521. values.push( 0, 1, 0 );
  15522. const order = getKeyframeOrder( times );
  15523. times = sortedArray( times, 1, order );
  15524. values = sortedArray( values, 1, order );
  15525. // if there is a key at the first frame, duplicate it as the
  15526. // last frame as well for perfect loop.
  15527. if ( ! noLoop && times[ 0 ] === 0 ) {
  15528. times.push( numMorphTargets );
  15529. values.push( values[ 0 ] );
  15530. }
  15531. tracks.push(
  15532. new NumberKeyframeTrack(
  15533. '.morphTargetInfluences[' + morphTargetSequence[ i ].name + ']',
  15534. times, values
  15535. ).scale( 1.0 / fps ) );
  15536. }
  15537. return new this( name, - 1, tracks );
  15538. }
  15539. static findByName( objectOrClipArray, name ) {
  15540. let clipArray = objectOrClipArray;
  15541. if ( ! Array.isArray( objectOrClipArray ) ) {
  15542. const o = objectOrClipArray;
  15543. clipArray = o.geometry && o.geometry.animations || o.animations;
  15544. }
  15545. for ( let i = 0; i < clipArray.length; i ++ ) {
  15546. if ( clipArray[ i ].name === name ) {
  15547. return clipArray[ i ];
  15548. }
  15549. }
  15550. return null;
  15551. }
  15552. static CreateClipsFromMorphTargetSequences( morphTargets, fps, noLoop ) {
  15553. const animationToMorphTargets = {};
  15554. // tested with https://regex101.com/ on trick sequences
  15555. // such flamingo_flyA_003, flamingo_run1_003, crdeath0059
  15556. const pattern = /^([\w-]*?)([\d]+)$/;
  15557. // sort morph target names into animation groups based
  15558. // patterns like Walk_001, Walk_002, Run_001, Run_002
  15559. for ( let i = 0, il = morphTargets.length; i < il; i ++ ) {
  15560. const morphTarget = morphTargets[ i ];
  15561. const parts = morphTarget.name.match( pattern );
  15562. if ( parts && parts.length > 1 ) {
  15563. const name = parts[ 1 ];
  15564. let animationMorphTargets = animationToMorphTargets[ name ];
  15565. if ( ! animationMorphTargets ) {
  15566. animationToMorphTargets[ name ] = animationMorphTargets = [];
  15567. }
  15568. animationMorphTargets.push( morphTarget );
  15569. }
  15570. }
  15571. const clips = [];
  15572. for ( const name in animationToMorphTargets ) {
  15573. clips.push( this.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps, noLoop ) );
  15574. }
  15575. return clips;
  15576. }
  15577. // parse the animation.hierarchy format
  15578. static parseAnimation( animation, bones ) {
  15579. if ( ! animation ) {
  15580. console.error( 'THREE.AnimationClip: No animation in JSONLoader data.' );
  15581. return null;
  15582. }
  15583. const addNonemptyTrack = function ( trackType, trackName, animationKeys, propertyName, destTracks ) {
  15584. // only return track if there are actually keys.
  15585. if ( animationKeys.length !== 0 ) {
  15586. const times = [];
  15587. const values = [];
  15588. flattenJSON( animationKeys, times, values, propertyName );
  15589. // empty keys are filtered out, so check again
  15590. if ( times.length !== 0 ) {
  15591. destTracks.push( new trackType( trackName, times, values ) );
  15592. }
  15593. }
  15594. };
  15595. const tracks = [];
  15596. const clipName = animation.name || 'default';
  15597. const fps = animation.fps || 30;
  15598. const blendMode = animation.blendMode;
  15599. // automatic length determination in AnimationClip.
  15600. let duration = animation.length || - 1;
  15601. const hierarchyTracks = animation.hierarchy || [];
  15602. for ( let h = 0; h < hierarchyTracks.length; h ++ ) {
  15603. const animationKeys = hierarchyTracks[ h ].keys;
  15604. // skip empty tracks
  15605. if ( ! animationKeys || animationKeys.length === 0 ) continue;
  15606. // process morph targets
  15607. if ( animationKeys[ 0 ].morphTargets ) {
  15608. // figure out all morph targets used in this track
  15609. const morphTargetNames = {};
  15610. let k;
  15611. for ( k = 0; k < animationKeys.length; k ++ ) {
  15612. if ( animationKeys[ k ].morphTargets ) {
  15613. for ( let m = 0; m < animationKeys[ k ].morphTargets.length; m ++ ) {
  15614. morphTargetNames[ animationKeys[ k ].morphTargets[ m ] ] = - 1;
  15615. }
  15616. }
  15617. }
  15618. // create a track for each morph target with all zero
  15619. // morphTargetInfluences except for the keys in which
  15620. // the morphTarget is named.
  15621. for ( const morphTargetName in morphTargetNames ) {
  15622. const times = [];
  15623. const values = [];
  15624. for ( let m = 0; m !== animationKeys[ k ].morphTargets.length; ++ m ) {
  15625. const animationKey = animationKeys[ k ];
  15626. times.push( animationKey.time );
  15627. values.push( ( animationKey.morphTarget === morphTargetName ) ? 1 : 0 );
  15628. }
  15629. tracks.push( new NumberKeyframeTrack( '.morphTargetInfluence[' + morphTargetName + ']', times, values ) );
  15630. }
  15631. duration = morphTargetNames.length * fps;
  15632. } else {
  15633. // ...assume skeletal animation
  15634. const boneName = '.bones[' + bones[ h ].name + ']';
  15635. addNonemptyTrack(
  15636. VectorKeyframeTrack, boneName + '.position',
  15637. animationKeys, 'pos', tracks );
  15638. addNonemptyTrack(
  15639. QuaternionKeyframeTrack, boneName + '.quaternion',
  15640. animationKeys, 'rot', tracks );
  15641. addNonemptyTrack(
  15642. VectorKeyframeTrack, boneName + '.scale',
  15643. animationKeys, 'scl', tracks );
  15644. }
  15645. }
  15646. if ( tracks.length === 0 ) {
  15647. return null;
  15648. }
  15649. const clip = new this( clipName, duration, tracks, blendMode );
  15650. return clip;
  15651. }
  15652. resetDuration() {
  15653. const tracks = this.tracks;
  15654. let duration = 0;
  15655. for ( let i = 0, n = tracks.length; i !== n; ++ i ) {
  15656. const track = this.tracks[ i ];
  15657. duration = Math.max( duration, track.times[ track.times.length - 1 ] );
  15658. }
  15659. this.duration = duration;
  15660. return this;
  15661. }
  15662. trim() {
  15663. for ( let i = 0; i < this.tracks.length; i ++ ) {
  15664. this.tracks[ i ].trim( 0, this.duration );
  15665. }
  15666. return this;
  15667. }
  15668. validate() {
  15669. let valid = true;
  15670. for ( let i = 0; i < this.tracks.length; i ++ ) {
  15671. valid = valid && this.tracks[ i ].validate();
  15672. }
  15673. return valid;
  15674. }
  15675. optimize() {
  15676. for ( let i = 0; i < this.tracks.length; i ++ ) {
  15677. this.tracks[ i ].optimize();
  15678. }
  15679. return this;
  15680. }
  15681. clone() {
  15682. const tracks = [];
  15683. for ( let i = 0; i < this.tracks.length; i ++ ) {
  15684. tracks.push( this.tracks[ i ].clone() );
  15685. }
  15686. return new this.constructor( this.name, this.duration, tracks, this.blendMode );
  15687. }
  15688. toJSON() {
  15689. return this.constructor.toJSON( this );
  15690. }
  15691. }
  15692. function getTrackTypeForValueTypeName( typeName ) {
  15693. switch ( typeName.toLowerCase() ) {
  15694. case 'scalar':
  15695. case 'double':
  15696. case 'float':
  15697. case 'number':
  15698. case 'integer':
  15699. return NumberKeyframeTrack;
  15700. case 'vector':
  15701. case 'vector2':
  15702. case 'vector3':
  15703. case 'vector4':
  15704. return VectorKeyframeTrack;
  15705. case 'color':
  15706. return ColorKeyframeTrack;
  15707. case 'quaternion':
  15708. return QuaternionKeyframeTrack;
  15709. case 'bool':
  15710. case 'boolean':
  15711. return BooleanKeyframeTrack;
  15712. case 'string':
  15713. return StringKeyframeTrack;
  15714. }
  15715. throw new Error( 'THREE.KeyframeTrack: Unsupported typeName: ' + typeName );
  15716. }
  15717. function parseKeyframeTrack( json ) {
  15718. if ( json.type === undefined ) {
  15719. throw new Error( 'THREE.KeyframeTrack: track type undefined, can not parse' );
  15720. }
  15721. const trackType = getTrackTypeForValueTypeName( json.type );
  15722. if ( json.times === undefined ) {
  15723. const times = [], values = [];
  15724. flattenJSON( json.keys, times, values, 'value' );
  15725. json.times = times;
  15726. json.values = values;
  15727. }
  15728. // derived classes can define a static parse method
  15729. if ( trackType.parse !== undefined ) {
  15730. return trackType.parse( json );
  15731. } else {
  15732. // by default, we assume a constructor compatible with the base
  15733. return new trackType( json.name, json.times, json.values, json.interpolation );
  15734. }
  15735. }
  15736. const Cache = {
  15737. enabled: false,
  15738. files: {},
  15739. add: function ( key, file ) {
  15740. if ( this.enabled === false ) return;
  15741. // console.log( 'THREE.Cache', 'Adding key:', key );
  15742. this.files[ key ] = file;
  15743. },
  15744. get: function ( key ) {
  15745. if ( this.enabled === false ) return;
  15746. // console.log( 'THREE.Cache', 'Checking key:', key );
  15747. return this.files[ key ];
  15748. },
  15749. remove: function ( key ) {
  15750. delete this.files[ key ];
  15751. },
  15752. clear: function () {
  15753. this.files = {};
  15754. }
  15755. };
  15756. class LoadingManager {
  15757. constructor( onLoad, onProgress, onError ) {
  15758. const scope = this;
  15759. let isLoading = false;
  15760. let itemsLoaded = 0;
  15761. let itemsTotal = 0;
  15762. let urlModifier = undefined;
  15763. const handlers = [];
  15764. // Refer to #5689 for the reason why we don't set .onStart
  15765. // in the constructor
  15766. this.onStart = undefined;
  15767. this.onLoad = onLoad;
  15768. this.onProgress = onProgress;
  15769. this.onError = onError;
  15770. this.itemStart = function ( url ) {
  15771. itemsTotal ++;
  15772. if ( isLoading === false ) {
  15773. if ( scope.onStart !== undefined ) {
  15774. scope.onStart( url, itemsLoaded, itemsTotal );
  15775. }
  15776. }
  15777. isLoading = true;
  15778. };
  15779. this.itemEnd = function ( url ) {
  15780. itemsLoaded ++;
  15781. if ( scope.onProgress !== undefined ) {
  15782. scope.onProgress( url, itemsLoaded, itemsTotal );
  15783. }
  15784. if ( itemsLoaded === itemsTotal ) {
  15785. isLoading = false;
  15786. if ( scope.onLoad !== undefined ) {
  15787. scope.onLoad();
  15788. }
  15789. }
  15790. };
  15791. this.itemError = function ( url ) {
  15792. if ( scope.onError !== undefined ) {
  15793. scope.onError( url );
  15794. }
  15795. };
  15796. this.resolveURL = function ( url ) {
  15797. if ( urlModifier ) {
  15798. return urlModifier( url );
  15799. }
  15800. return url;
  15801. };
  15802. this.setURLModifier = function ( transform ) {
  15803. urlModifier = transform;
  15804. return this;
  15805. };
  15806. this.addHandler = function ( regex, loader ) {
  15807. handlers.push( regex, loader );
  15808. return this;
  15809. };
  15810. this.removeHandler = function ( regex ) {
  15811. const index = handlers.indexOf( regex );
  15812. if ( index !== - 1 ) {
  15813. handlers.splice( index, 2 );
  15814. }
  15815. return this;
  15816. };
  15817. this.getHandler = function ( file ) {
  15818. for ( let i = 0, l = handlers.length; i < l; i += 2 ) {
  15819. const regex = handlers[ i ];
  15820. const loader = handlers[ i + 1 ];
  15821. if ( regex.global ) regex.lastIndex = 0; // see #17920
  15822. if ( regex.test( file ) ) {
  15823. return loader;
  15824. }
  15825. }
  15826. return null;
  15827. };
  15828. }
  15829. }
  15830. const DefaultLoadingManager = /*@__PURE__*/ new LoadingManager();
  15831. class Loader {
  15832. constructor( manager ) {
  15833. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  15834. this.crossOrigin = 'anonymous';
  15835. this.withCredentials = false;
  15836. this.path = '';
  15837. this.resourcePath = '';
  15838. this.requestHeader = {};
  15839. }
  15840. load( /* url, onLoad, onProgress, onError */ ) {}
  15841. loadAsync( url, onProgress ) {
  15842. const scope = this;
  15843. return new Promise( function ( resolve, reject ) {
  15844. scope.load( url, resolve, onProgress, reject );
  15845. } );
  15846. }
  15847. parse( /* data */ ) {}
  15848. setCrossOrigin( crossOrigin ) {
  15849. this.crossOrigin = crossOrigin;
  15850. return this;
  15851. }
  15852. setWithCredentials( value ) {
  15853. this.withCredentials = value;
  15854. return this;
  15855. }
  15856. setPath( path ) {
  15857. this.path = path;
  15858. return this;
  15859. }
  15860. setResourcePath( resourcePath ) {
  15861. this.resourcePath = resourcePath;
  15862. return this;
  15863. }
  15864. setRequestHeader( requestHeader ) {
  15865. this.requestHeader = requestHeader;
  15866. return this;
  15867. }
  15868. }
  15869. Loader.DEFAULT_MATERIAL_NAME = '__DEFAULT';
  15870. const loading = {};
  15871. class HttpError extends Error {
  15872. constructor( message, response ) {
  15873. super( message );
  15874. this.response = response;
  15875. }
  15876. }
  15877. class FileLoader extends Loader {
  15878. constructor( manager ) {
  15879. super( manager );
  15880. }
  15881. load( url, onLoad, onProgress, onError ) {
  15882. if ( url === undefined ) url = '';
  15883. if ( this.path !== undefined ) url = this.path + url;
  15884. url = this.manager.resolveURL( url );
  15885. const cached = Cache.get( url );
  15886. if ( cached !== undefined ) {
  15887. this.manager.itemStart( url );
  15888. setTimeout( () => {
  15889. if ( onLoad ) onLoad( cached );
  15890. this.manager.itemEnd( url );
  15891. }, 0 );
  15892. return cached;
  15893. }
  15894. // Check if request is duplicate
  15895. if ( loading[ url ] !== undefined ) {
  15896. loading[ url ].push( {
  15897. onLoad: onLoad,
  15898. onProgress: onProgress,
  15899. onError: onError
  15900. } );
  15901. return;
  15902. }
  15903. // Initialise array for duplicate requests
  15904. loading[ url ] = [];
  15905. loading[ url ].push( {
  15906. onLoad: onLoad,
  15907. onProgress: onProgress,
  15908. onError: onError,
  15909. } );
  15910. // create request
  15911. const req = new Request( url, {
  15912. headers: new Headers( this.requestHeader ),
  15913. credentials: this.withCredentials ? 'include' : 'same-origin',
  15914. // An abort controller could be added within a future PR
  15915. } );
  15916. // record states ( avoid data race )
  15917. const mimeType = this.mimeType;
  15918. const responseType = this.responseType;
  15919. // start the fetch
  15920. fetch( req )
  15921. .then( response => {
  15922. if ( response.status === 200 || response.status === 0 ) {
  15923. // Some browsers return HTTP Status 0 when using non-http protocol
  15924. // e.g. 'file://' or 'data://'. Handle as success.
  15925. if ( response.status === 0 ) {
  15926. console.warn( 'THREE.FileLoader: HTTP Status 0 received.' );
  15927. }
  15928. // Workaround: Checking if response.body === undefined for Alipay browser #23548
  15929. if ( typeof ReadableStream === 'undefined' || response.body === undefined || response.body.getReader === undefined ) {
  15930. return response;
  15931. }
  15932. const callbacks = loading[ url ];
  15933. const reader = response.body.getReader();
  15934. // Nginx needs X-File-Size check
  15935. // https://serverfault.com/questions/482875/why-does-nginx-remove-content-length-header-for-chunked-content
  15936. const contentLength = response.headers.get( 'X-File-Size' ) || response.headers.get( 'Content-Length' );
  15937. const total = contentLength ? parseInt( contentLength ) : 0;
  15938. const lengthComputable = total !== 0;
  15939. let loaded = 0;
  15940. // periodically read data into the new stream tracking while download progress
  15941. const stream = new ReadableStream( {
  15942. start( controller ) {
  15943. readData();
  15944. function readData() {
  15945. reader.read().then( ( { done, value } ) => {
  15946. if ( done ) {
  15947. controller.close();
  15948. } else {
  15949. loaded += value.byteLength;
  15950. const event = new ProgressEvent( 'progress', { lengthComputable, loaded, total } );
  15951. for ( let i = 0, il = callbacks.length; i < il; i ++ ) {
  15952. const callback = callbacks[ i ];
  15953. if ( callback.onProgress ) callback.onProgress( event );
  15954. }
  15955. controller.enqueue( value );
  15956. readData();
  15957. }
  15958. }, ( e ) => {
  15959. controller.error( e );
  15960. } );
  15961. }
  15962. }
  15963. } );
  15964. return new Response( stream );
  15965. } else {
  15966. throw new HttpError( `fetch for "${response.url}" responded with ${response.status}: ${response.statusText}`, response );
  15967. }
  15968. } )
  15969. .then( response => {
  15970. switch ( responseType ) {
  15971. case 'arraybuffer':
  15972. return response.arrayBuffer();
  15973. case 'blob':
  15974. return response.blob();
  15975. case 'document':
  15976. return response.text()
  15977. .then( text => {
  15978. const parser = new DOMParser();
  15979. return parser.parseFromString( text, mimeType );
  15980. } );
  15981. case 'json':
  15982. return response.json();
  15983. default:
  15984. if ( mimeType === undefined ) {
  15985. return response.text();
  15986. } else {
  15987. // sniff encoding
  15988. const re = /charset="?([^;"\s]*)"?/i;
  15989. const exec = re.exec( mimeType );
  15990. const label = exec && exec[ 1 ] ? exec[ 1 ].toLowerCase() : undefined;
  15991. const decoder = new TextDecoder( label );
  15992. return response.arrayBuffer().then( ab => decoder.decode( ab ) );
  15993. }
  15994. }
  15995. } )
  15996. .then( data => {
  15997. // Add to cache only on HTTP success, so that we do not cache
  15998. // error response bodies as proper responses to requests.
  15999. Cache.add( url, data );
  16000. const callbacks = loading[ url ];
  16001. delete loading[ url ];
  16002. for ( let i = 0, il = callbacks.length; i < il; i ++ ) {
  16003. const callback = callbacks[ i ];
  16004. if ( callback.onLoad ) callback.onLoad( data );
  16005. }
  16006. } )
  16007. .catch( err => {
  16008. // Abort errors and other errors are handled the same
  16009. const callbacks = loading[ url ];
  16010. if ( callbacks === undefined ) {
  16011. // When onLoad was called and url was deleted in `loading`
  16012. this.manager.itemError( url );
  16013. throw err;
  16014. }
  16015. delete loading[ url ];
  16016. for ( let i = 0, il = callbacks.length; i < il; i ++ ) {
  16017. const callback = callbacks[ i ];
  16018. if ( callback.onError ) callback.onError( err );
  16019. }
  16020. this.manager.itemError( url );
  16021. } )
  16022. .finally( () => {
  16023. this.manager.itemEnd( url );
  16024. } );
  16025. this.manager.itemStart( url );
  16026. }
  16027. setResponseType( value ) {
  16028. this.responseType = value;
  16029. return this;
  16030. }
  16031. setMimeType( value ) {
  16032. this.mimeType = value;
  16033. return this;
  16034. }
  16035. }
  16036. class AnimationLoader extends Loader {
  16037. constructor( manager ) {
  16038. super( manager );
  16039. }
  16040. load( url, onLoad, onProgress, onError ) {
  16041. const scope = this;
  16042. const loader = new FileLoader( this.manager );
  16043. loader.setPath( this.path );
  16044. loader.setRequestHeader( this.requestHeader );
  16045. loader.setWithCredentials( this.withCredentials );
  16046. loader.load( url, function ( text ) {
  16047. try {
  16048. onLoad( scope.parse( JSON.parse( text ) ) );
  16049. } catch ( e ) {
  16050. if ( onError ) {
  16051. onError( e );
  16052. } else {
  16053. console.error( e );
  16054. }
  16055. scope.manager.itemError( url );
  16056. }
  16057. }, onProgress, onError );
  16058. }
  16059. parse( json ) {
  16060. const animations = [];
  16061. for ( let i = 0; i < json.length; i ++ ) {
  16062. const clip = AnimationClip.parse( json[ i ] );
  16063. animations.push( clip );
  16064. }
  16065. return animations;
  16066. }
  16067. }
  16068. /**
  16069. * Abstract Base class to block based textures loader (dds, pvr, ...)
  16070. *
  16071. * Sub classes have to implement the parse() method which will be used in load().
  16072. */
  16073. class CompressedTextureLoader extends Loader {
  16074. constructor( manager ) {
  16075. super( manager );
  16076. }
  16077. load( url, onLoad, onProgress, onError ) {
  16078. const scope = this;
  16079. const images = [];
  16080. const texture = new CompressedTexture();
  16081. const loader = new FileLoader( this.manager );
  16082. loader.setPath( this.path );
  16083. loader.setResponseType( 'arraybuffer' );
  16084. loader.setRequestHeader( this.requestHeader );
  16085. loader.setWithCredentials( scope.withCredentials );
  16086. let loaded = 0;
  16087. function loadTexture( i ) {
  16088. loader.load( url[ i ], function ( buffer ) {
  16089. const texDatas = scope.parse( buffer, true );
  16090. images[ i ] = {
  16091. width: texDatas.width,
  16092. height: texDatas.height,
  16093. format: texDatas.format,
  16094. mipmaps: texDatas.mipmaps
  16095. };
  16096. loaded += 1;
  16097. if ( loaded === 6 ) {
  16098. if ( texDatas.mipmapCount === 1 ) texture.minFilter = LinearFilter;
  16099. texture.image = images;
  16100. texture.format = texDatas.format;
  16101. texture.needsUpdate = true;
  16102. if ( onLoad ) onLoad( texture );
  16103. }
  16104. }, onProgress, onError );
  16105. }
  16106. if ( Array.isArray( url ) ) {
  16107. for ( let i = 0, il = url.length; i < il; ++ i ) {
  16108. loadTexture( i );
  16109. }
  16110. } else {
  16111. // compressed cubemap texture stored in a single DDS file
  16112. loader.load( url, function ( buffer ) {
  16113. const texDatas = scope.parse( buffer, true );
  16114. if ( texDatas.isCubemap ) {
  16115. const faces = texDatas.mipmaps.length / texDatas.mipmapCount;
  16116. for ( let f = 0; f < faces; f ++ ) {
  16117. images[ f ] = { mipmaps: [] };
  16118. for ( let i = 0; i < texDatas.mipmapCount; i ++ ) {
  16119. images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
  16120. images[ f ].format = texDatas.format;
  16121. images[ f ].width = texDatas.width;
  16122. images[ f ].height = texDatas.height;
  16123. }
  16124. }
  16125. texture.image = images;
  16126. } else {
  16127. texture.image.width = texDatas.width;
  16128. texture.image.height = texDatas.height;
  16129. texture.mipmaps = texDatas.mipmaps;
  16130. }
  16131. if ( texDatas.mipmapCount === 1 ) {
  16132. texture.minFilter = LinearFilter;
  16133. }
  16134. texture.format = texDatas.format;
  16135. texture.needsUpdate = true;
  16136. if ( onLoad ) onLoad( texture );
  16137. }, onProgress, onError );
  16138. }
  16139. return texture;
  16140. }
  16141. }
  16142. class ImageLoader extends Loader {
  16143. constructor( manager ) {
  16144. super( manager );
  16145. }
  16146. load( url, onLoad, onProgress, onError ) {
  16147. if ( this.path !== undefined ) url = this.path + url;
  16148. url = this.manager.resolveURL( url );
  16149. const scope = this;
  16150. const cached = Cache.get( url );
  16151. if ( cached !== undefined ) {
  16152. scope.manager.itemStart( url );
  16153. setTimeout( function () {
  16154. if ( onLoad ) onLoad( cached );
  16155. scope.manager.itemEnd( url );
  16156. }, 0 );
  16157. return cached;
  16158. }
  16159. const image = createElementNS( 'img' );
  16160. function onImageLoad() {
  16161. removeEventListeners();
  16162. Cache.add( url, this );
  16163. if ( onLoad ) onLoad( this );
  16164. scope.manager.itemEnd( url );
  16165. }
  16166. function onImageError( event ) {
  16167. removeEventListeners();
  16168. if ( onError ) onError( event );
  16169. scope.manager.itemError( url );
  16170. scope.manager.itemEnd( url );
  16171. }
  16172. function removeEventListeners() {
  16173. image.removeEventListener( 'load', onImageLoad, false );
  16174. image.removeEventListener( 'error', onImageError, false );
  16175. }
  16176. image.addEventListener( 'load', onImageLoad, false );
  16177. image.addEventListener( 'error', onImageError, false );
  16178. if ( url.slice( 0, 5 ) !== 'data:' ) {
  16179. if ( this.crossOrigin !== undefined ) image.crossOrigin = this.crossOrigin;
  16180. }
  16181. scope.manager.itemStart( url );
  16182. image.src = url;
  16183. return image;
  16184. }
  16185. }
  16186. class CubeTextureLoader extends Loader {
  16187. constructor( manager ) {
  16188. super( manager );
  16189. }
  16190. load( urls, onLoad, onProgress, onError ) {
  16191. const texture = new CubeTexture();
  16192. texture.colorSpace = SRGBColorSpace;
  16193. const loader = new ImageLoader( this.manager );
  16194. loader.setCrossOrigin( this.crossOrigin );
  16195. loader.setPath( this.path );
  16196. let loaded = 0;
  16197. function loadTexture( i ) {
  16198. loader.load( urls[ i ], function ( image ) {
  16199. texture.images[ i ] = image;
  16200. loaded ++;
  16201. if ( loaded === 6 ) {
  16202. texture.needsUpdate = true;
  16203. if ( onLoad ) onLoad( texture );
  16204. }
  16205. }, undefined, onError );
  16206. }
  16207. for ( let i = 0; i < urls.length; ++ i ) {
  16208. loadTexture( i );
  16209. }
  16210. return texture;
  16211. }
  16212. }
  16213. /**
  16214. * Abstract Base class to load generic binary textures formats (rgbe, hdr, ...)
  16215. *
  16216. * Sub classes have to implement the parse() method which will be used in load().
  16217. */
  16218. class DataTextureLoader extends Loader {
  16219. constructor( manager ) {
  16220. super( manager );
  16221. }
  16222. load( url, onLoad, onProgress, onError ) {
  16223. const scope = this;
  16224. const texture = new DataTexture();
  16225. const loader = new FileLoader( this.manager );
  16226. loader.setResponseType( 'arraybuffer' );
  16227. loader.setRequestHeader( this.requestHeader );
  16228. loader.setPath( this.path );
  16229. loader.setWithCredentials( scope.withCredentials );
  16230. loader.load( url, function ( buffer ) {
  16231. let texData;
  16232. try {
  16233. texData = scope.parse( buffer );
  16234. } catch ( error ) {
  16235. if ( onError !== undefined ) {
  16236. onError( error );
  16237. } else {
  16238. console.error( error );
  16239. return;
  16240. }
  16241. }
  16242. if ( texData.image !== undefined ) {
  16243. texture.image = texData.image;
  16244. } else if ( texData.data !== undefined ) {
  16245. texture.image.width = texData.width;
  16246. texture.image.height = texData.height;
  16247. texture.image.data = texData.data;
  16248. }
  16249. texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping;
  16250. texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping;
  16251. texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter;
  16252. texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearFilter;
  16253. texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1;
  16254. if ( texData.colorSpace !== undefined ) {
  16255. texture.colorSpace = texData.colorSpace;
  16256. }
  16257. if ( texData.flipY !== undefined ) {
  16258. texture.flipY = texData.flipY;
  16259. }
  16260. if ( texData.format !== undefined ) {
  16261. texture.format = texData.format;
  16262. }
  16263. if ( texData.type !== undefined ) {
  16264. texture.type = texData.type;
  16265. }
  16266. if ( texData.mipmaps !== undefined ) {
  16267. texture.mipmaps = texData.mipmaps;
  16268. texture.minFilter = LinearMipmapLinearFilter; // presumably...
  16269. }
  16270. if ( texData.mipmapCount === 1 ) {
  16271. texture.minFilter = LinearFilter;
  16272. }
  16273. if ( texData.generateMipmaps !== undefined ) {
  16274. texture.generateMipmaps = texData.generateMipmaps;
  16275. }
  16276. texture.needsUpdate = true;
  16277. if ( onLoad ) onLoad( texture, texData );
  16278. }, onProgress, onError );
  16279. return texture;
  16280. }
  16281. }
  16282. class TextureLoader extends Loader {
  16283. constructor( manager ) {
  16284. super( manager );
  16285. }
  16286. load( url, onLoad, onProgress, onError ) {
  16287. const texture = new Texture();
  16288. const loader = new ImageLoader( this.manager );
  16289. loader.setCrossOrigin( this.crossOrigin );
  16290. loader.setPath( this.path );
  16291. loader.load( url, function ( image ) {
  16292. texture.image = image;
  16293. texture.needsUpdate = true;
  16294. if ( onLoad !== undefined ) {
  16295. onLoad( texture );
  16296. }
  16297. }, onProgress, onError );
  16298. return texture;
  16299. }
  16300. }
  16301. class Light extends Object3D {
  16302. constructor( color, intensity = 1 ) {
  16303. super();
  16304. this.isLight = true;
  16305. this.type = 'Light';
  16306. this.color = new Color( color );
  16307. this.intensity = intensity;
  16308. }
  16309. dispose() {
  16310. // Empty here in base class; some subclasses override.
  16311. }
  16312. copy( source, recursive ) {
  16313. super.copy( source, recursive );
  16314. this.color.copy( source.color );
  16315. this.intensity = source.intensity;
  16316. return this;
  16317. }
  16318. toJSON( meta ) {
  16319. const data = super.toJSON( meta );
  16320. data.object.color = this.color.getHex();
  16321. data.object.intensity = this.intensity;
  16322. if ( this.groundColor !== undefined ) data.object.groundColor = this.groundColor.getHex();
  16323. if ( this.distance !== undefined ) data.object.distance = this.distance;
  16324. if ( this.angle !== undefined ) data.object.angle = this.angle;
  16325. if ( this.decay !== undefined ) data.object.decay = this.decay;
  16326. if ( this.penumbra !== undefined ) data.object.penumbra = this.penumbra;
  16327. if ( this.shadow !== undefined ) data.object.shadow = this.shadow.toJSON();
  16328. if ( this.target !== undefined ) data.object.target = this.target.uuid;
  16329. return data;
  16330. }
  16331. }
  16332. class HemisphereLight extends Light {
  16333. constructor( skyColor, groundColor, intensity ) {
  16334. super( skyColor, intensity );
  16335. this.isHemisphereLight = true;
  16336. this.type = 'HemisphereLight';
  16337. this.position.copy( Object3D.DEFAULT_UP );
  16338. this.updateMatrix();
  16339. this.groundColor = new Color( groundColor );
  16340. }
  16341. copy( source, recursive ) {
  16342. super.copy( source, recursive );
  16343. this.groundColor.copy( source.groundColor );
  16344. return this;
  16345. }
  16346. }
  16347. const _projScreenMatrix$1 = /*@__PURE__*/ new Matrix4();
  16348. const _lightPositionWorld$1 = /*@__PURE__*/ new Vector3();
  16349. const _lookTarget$1 = /*@__PURE__*/ new Vector3();
  16350. class LightShadow {
  16351. constructor( camera ) {
  16352. this.camera = camera;
  16353. this.intensity = 1;
  16354. this.bias = 0;
  16355. this.normalBias = 0;
  16356. this.radius = 1;
  16357. this.blurSamples = 8;
  16358. this.mapSize = new Vector2( 512, 512 );
  16359. this.map = null;
  16360. this.mapPass = null;
  16361. this.matrix = new Matrix4();
  16362. this.autoUpdate = true;
  16363. this.needsUpdate = false;
  16364. this._frustum = new Frustum();
  16365. this._frameExtents = new Vector2( 1, 1 );
  16366. this._viewportCount = 1;
  16367. this._viewports = [
  16368. new Vector4( 0, 0, 1, 1 )
  16369. ];
  16370. }
  16371. getViewportCount() {
  16372. return this._viewportCount;
  16373. }
  16374. getFrustum() {
  16375. return this._frustum;
  16376. }
  16377. updateMatrices( light ) {
  16378. const shadowCamera = this.camera;
  16379. const shadowMatrix = this.matrix;
  16380. _lightPositionWorld$1.setFromMatrixPosition( light.matrixWorld );
  16381. shadowCamera.position.copy( _lightPositionWorld$1 );
  16382. _lookTarget$1.setFromMatrixPosition( light.target.matrixWorld );
  16383. shadowCamera.lookAt( _lookTarget$1 );
  16384. shadowCamera.updateMatrixWorld();
  16385. _projScreenMatrix$1.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  16386. this._frustum.setFromProjectionMatrix( _projScreenMatrix$1 );
  16387. shadowMatrix.set(
  16388. 0.5, 0.0, 0.0, 0.5,
  16389. 0.0, 0.5, 0.0, 0.5,
  16390. 0.0, 0.0, 0.5, 0.5,
  16391. 0.0, 0.0, 0.0, 1.0
  16392. );
  16393. shadowMatrix.multiply( _projScreenMatrix$1 );
  16394. }
  16395. getViewport( viewportIndex ) {
  16396. return this._viewports[ viewportIndex ];
  16397. }
  16398. getFrameExtents() {
  16399. return this._frameExtents;
  16400. }
  16401. dispose() {
  16402. if ( this.map ) {
  16403. this.map.dispose();
  16404. }
  16405. if ( this.mapPass ) {
  16406. this.mapPass.dispose();
  16407. }
  16408. }
  16409. copy( source ) {
  16410. this.camera = source.camera.clone();
  16411. this.intensity = source.intensity;
  16412. this.bias = source.bias;
  16413. this.radius = source.radius;
  16414. this.mapSize.copy( source.mapSize );
  16415. return this;
  16416. }
  16417. clone() {
  16418. return new this.constructor().copy( this );
  16419. }
  16420. toJSON() {
  16421. const object = {};
  16422. if ( this.intensity !== 1 ) object.intensity = this.intensity;
  16423. if ( this.bias !== 0 ) object.bias = this.bias;
  16424. if ( this.normalBias !== 0 ) object.normalBias = this.normalBias;
  16425. if ( this.radius !== 1 ) object.radius = this.radius;
  16426. if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) object.mapSize = this.mapSize.toArray();
  16427. object.camera = this.camera.toJSON( false ).object;
  16428. delete object.camera.matrix;
  16429. return object;
  16430. }
  16431. }
  16432. class SpotLightShadow extends LightShadow {
  16433. constructor() {
  16434. super( new PerspectiveCamera( 50, 1, 0.5, 500 ) );
  16435. this.isSpotLightShadow = true;
  16436. this.focus = 1;
  16437. }
  16438. updateMatrices( light ) {
  16439. const camera = this.camera;
  16440. const fov = RAD2DEG * 2 * light.angle * this.focus;
  16441. const aspect = this.mapSize.width / this.mapSize.height;
  16442. const far = light.distance || camera.far;
  16443. if ( fov !== camera.fov || aspect !== camera.aspect || far !== camera.far ) {
  16444. camera.fov = fov;
  16445. camera.aspect = aspect;
  16446. camera.far = far;
  16447. camera.updateProjectionMatrix();
  16448. }
  16449. super.updateMatrices( light );
  16450. }
  16451. copy( source ) {
  16452. super.copy( source );
  16453. this.focus = source.focus;
  16454. return this;
  16455. }
  16456. }
  16457. class SpotLight extends Light {
  16458. constructor( color, intensity, distance = 0, angle = Math.PI / 3, penumbra = 0, decay = 2 ) {
  16459. super( color, intensity );
  16460. this.isSpotLight = true;
  16461. this.type = 'SpotLight';
  16462. this.position.copy( Object3D.DEFAULT_UP );
  16463. this.updateMatrix();
  16464. this.target = new Object3D();
  16465. this.distance = distance;
  16466. this.angle = angle;
  16467. this.penumbra = penumbra;
  16468. this.decay = decay;
  16469. this.map = null;
  16470. this.shadow = new SpotLightShadow();
  16471. }
  16472. get power() {
  16473. // compute the light's luminous power (in lumens) from its intensity (in candela)
  16474. // by convention for a spotlight, luminous power (lm) = π * luminous intensity (cd)
  16475. return this.intensity * Math.PI;
  16476. }
  16477. set power( power ) {
  16478. // set the light's intensity (in candela) from the desired luminous power (in lumens)
  16479. this.intensity = power / Math.PI;
  16480. }
  16481. dispose() {
  16482. this.shadow.dispose();
  16483. }
  16484. copy( source, recursive ) {
  16485. super.copy( source, recursive );
  16486. this.distance = source.distance;
  16487. this.angle = source.angle;
  16488. this.penumbra = source.penumbra;
  16489. this.decay = source.decay;
  16490. this.target = source.target.clone();
  16491. this.shadow = source.shadow.clone();
  16492. return this;
  16493. }
  16494. }
  16495. const _projScreenMatrix = /*@__PURE__*/ new Matrix4();
  16496. const _lightPositionWorld = /*@__PURE__*/ new Vector3();
  16497. const _lookTarget = /*@__PURE__*/ new Vector3();
  16498. class PointLightShadow extends LightShadow {
  16499. constructor() {
  16500. super( new PerspectiveCamera( 90, 1, 0.5, 500 ) );
  16501. this.isPointLightShadow = true;
  16502. this._frameExtents = new Vector2( 4, 2 );
  16503. this._viewportCount = 6;
  16504. this._viewports = [
  16505. // These viewports map a cube-map onto a 2D texture with the
  16506. // following orientation:
  16507. //
  16508. // xzXZ
  16509. // y Y
  16510. //
  16511. // X - Positive x direction
  16512. // x - Negative x direction
  16513. // Y - Positive y direction
  16514. // y - Negative y direction
  16515. // Z - Positive z direction
  16516. // z - Negative z direction
  16517. // positive X
  16518. new Vector4( 2, 1, 1, 1 ),
  16519. // negative X
  16520. new Vector4( 0, 1, 1, 1 ),
  16521. // positive Z
  16522. new Vector4( 3, 1, 1, 1 ),
  16523. // negative Z
  16524. new Vector4( 1, 1, 1, 1 ),
  16525. // positive Y
  16526. new Vector4( 3, 0, 1, 1 ),
  16527. // negative Y
  16528. new Vector4( 1, 0, 1, 1 )
  16529. ];
  16530. this._cubeDirections = [
  16531. new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ),
  16532. new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 )
  16533. ];
  16534. this._cubeUps = [
  16535. new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ),
  16536. new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 )
  16537. ];
  16538. }
  16539. updateMatrices( light, viewportIndex = 0 ) {
  16540. const camera = this.camera;
  16541. const shadowMatrix = this.matrix;
  16542. const far = light.distance || camera.far;
  16543. if ( far !== camera.far ) {
  16544. camera.far = far;
  16545. camera.updateProjectionMatrix();
  16546. }
  16547. _lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
  16548. camera.position.copy( _lightPositionWorld );
  16549. _lookTarget.copy( camera.position );
  16550. _lookTarget.add( this._cubeDirections[ viewportIndex ] );
  16551. camera.up.copy( this._cubeUps[ viewportIndex ] );
  16552. camera.lookAt( _lookTarget );
  16553. camera.updateMatrixWorld();
  16554. shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z );
  16555. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  16556. this._frustum.setFromProjectionMatrix( _projScreenMatrix );
  16557. }
  16558. }
  16559. class PointLight extends Light {
  16560. constructor( color, intensity, distance = 0, decay = 2 ) {
  16561. super( color, intensity );
  16562. this.isPointLight = true;
  16563. this.type = 'PointLight';
  16564. this.distance = distance;
  16565. this.decay = decay;
  16566. this.shadow = new PointLightShadow();
  16567. }
  16568. get power() {
  16569. // compute the light's luminous power (in lumens) from its intensity (in candela)
  16570. // for an isotropic light source, luminous power (lm) = 4 π luminous intensity (cd)
  16571. return this.intensity * 4 * Math.PI;
  16572. }
  16573. set power( power ) {
  16574. // set the light's intensity (in candela) from the desired luminous power (in lumens)
  16575. this.intensity = power / ( 4 * Math.PI );
  16576. }
  16577. dispose() {
  16578. this.shadow.dispose();
  16579. }
  16580. copy( source, recursive ) {
  16581. super.copy( source, recursive );
  16582. this.distance = source.distance;
  16583. this.decay = source.decay;
  16584. this.shadow = source.shadow.clone();
  16585. return this;
  16586. }
  16587. }
  16588. class OrthographicCamera extends Camera {
  16589. constructor( left = - 1, right = 1, top = 1, bottom = - 1, near = 0.1, far = 2000 ) {
  16590. super();
  16591. this.isOrthographicCamera = true;
  16592. this.type = 'OrthographicCamera';
  16593. this.zoom = 1;
  16594. this.view = null;
  16595. this.left = left;
  16596. this.right = right;
  16597. this.top = top;
  16598. this.bottom = bottom;
  16599. this.near = near;
  16600. this.far = far;
  16601. this.updateProjectionMatrix();
  16602. }
  16603. copy( source, recursive ) {
  16604. super.copy( source, recursive );
  16605. this.left = source.left;
  16606. this.right = source.right;
  16607. this.top = source.top;
  16608. this.bottom = source.bottom;
  16609. this.near = source.near;
  16610. this.far = source.far;
  16611. this.zoom = source.zoom;
  16612. this.view = source.view === null ? null : Object.assign( {}, source.view );
  16613. return this;
  16614. }
  16615. setViewOffset( fullWidth, fullHeight, x, y, width, height ) {
  16616. if ( this.view === null ) {
  16617. this.view = {
  16618. enabled: true,
  16619. fullWidth: 1,
  16620. fullHeight: 1,
  16621. offsetX: 0,
  16622. offsetY: 0,
  16623. width: 1,
  16624. height: 1
  16625. };
  16626. }
  16627. this.view.enabled = true;
  16628. this.view.fullWidth = fullWidth;
  16629. this.view.fullHeight = fullHeight;
  16630. this.view.offsetX = x;
  16631. this.view.offsetY = y;
  16632. this.view.width = width;
  16633. this.view.height = height;
  16634. this.updateProjectionMatrix();
  16635. }
  16636. clearViewOffset() {
  16637. if ( this.view !== null ) {
  16638. this.view.enabled = false;
  16639. }
  16640. this.updateProjectionMatrix();
  16641. }
  16642. updateProjectionMatrix() {
  16643. const dx = ( this.right - this.left ) / ( 2 * this.zoom );
  16644. const dy = ( this.top - this.bottom ) / ( 2 * this.zoom );
  16645. const cx = ( this.right + this.left ) / 2;
  16646. const cy = ( this.top + this.bottom ) / 2;
  16647. let left = cx - dx;
  16648. let right = cx + dx;
  16649. let top = cy + dy;
  16650. let bottom = cy - dy;
  16651. if ( this.view !== null && this.view.enabled ) {
  16652. const scaleW = ( this.right - this.left ) / this.view.fullWidth / this.zoom;
  16653. const scaleH = ( this.top - this.bottom ) / this.view.fullHeight / this.zoom;
  16654. left += scaleW * this.view.offsetX;
  16655. right = left + scaleW * this.view.width;
  16656. top -= scaleH * this.view.offsetY;
  16657. bottom = top - scaleH * this.view.height;
  16658. }
  16659. this.projectionMatrix.makeOrthographic( left, right, top, bottom, this.near, this.far, this.coordinateSystem );
  16660. this.projectionMatrixInverse.copy( this.projectionMatrix ).invert();
  16661. }
  16662. toJSON( meta ) {
  16663. const data = super.toJSON( meta );
  16664. data.object.zoom = this.zoom;
  16665. data.object.left = this.left;
  16666. data.object.right = this.right;
  16667. data.object.top = this.top;
  16668. data.object.bottom = this.bottom;
  16669. data.object.near = this.near;
  16670. data.object.far = this.far;
  16671. if ( this.view !== null ) data.object.view = Object.assign( {}, this.view );
  16672. return data;
  16673. }
  16674. }
  16675. class DirectionalLightShadow extends LightShadow {
  16676. constructor() {
  16677. super( new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) );
  16678. this.isDirectionalLightShadow = true;
  16679. }
  16680. }
  16681. class DirectionalLight extends Light {
  16682. constructor( color, intensity ) {
  16683. super( color, intensity );
  16684. this.isDirectionalLight = true;
  16685. this.type = 'DirectionalLight';
  16686. this.position.copy( Object3D.DEFAULT_UP );
  16687. this.updateMatrix();
  16688. this.target = new Object3D();
  16689. this.shadow = new DirectionalLightShadow();
  16690. }
  16691. dispose() {
  16692. this.shadow.dispose();
  16693. }
  16694. copy( source ) {
  16695. super.copy( source );
  16696. this.target = source.target.clone();
  16697. this.shadow = source.shadow.clone();
  16698. return this;
  16699. }
  16700. }
  16701. class AmbientLight extends Light {
  16702. constructor( color, intensity ) {
  16703. super( color, intensity );
  16704. this.isAmbientLight = true;
  16705. this.type = 'AmbientLight';
  16706. }
  16707. }
  16708. class RectAreaLight extends Light {
  16709. constructor( color, intensity, width = 10, height = 10 ) {
  16710. super( color, intensity );
  16711. this.isRectAreaLight = true;
  16712. this.type = 'RectAreaLight';
  16713. this.width = width;
  16714. this.height = height;
  16715. }
  16716. get power() {
  16717. // compute the light's luminous power (in lumens) from its intensity (in nits)
  16718. return this.intensity * this.width * this.height * Math.PI;
  16719. }
  16720. set power( power ) {
  16721. // set the light's intensity (in nits) from the desired luminous power (in lumens)
  16722. this.intensity = power / ( this.width * this.height * Math.PI );
  16723. }
  16724. copy( source ) {
  16725. super.copy( source );
  16726. this.width = source.width;
  16727. this.height = source.height;
  16728. return this;
  16729. }
  16730. toJSON( meta ) {
  16731. const data = super.toJSON( meta );
  16732. data.object.width = this.width;
  16733. data.object.height = this.height;
  16734. return data;
  16735. }
  16736. }
  16737. /**
  16738. * Primary reference:
  16739. * https://graphics.stanford.edu/papers/envmap/envmap.pdf
  16740. *
  16741. * Secondary reference:
  16742. * https://www.ppsloan.org/publications/StupidSH36.pdf
  16743. */
  16744. // 3-band SH defined by 9 coefficients
  16745. class SphericalHarmonics3 {
  16746. constructor() {
  16747. this.isSphericalHarmonics3 = true;
  16748. this.coefficients = [];
  16749. for ( let i = 0; i < 9; i ++ ) {
  16750. this.coefficients.push( new Vector3() );
  16751. }
  16752. }
  16753. set( coefficients ) {
  16754. for ( let i = 0; i < 9; i ++ ) {
  16755. this.coefficients[ i ].copy( coefficients[ i ] );
  16756. }
  16757. return this;
  16758. }
  16759. zero() {
  16760. for ( let i = 0; i < 9; i ++ ) {
  16761. this.coefficients[ i ].set( 0, 0, 0 );
  16762. }
  16763. return this;
  16764. }
  16765. // get the radiance in the direction of the normal
  16766. // target is a Vector3
  16767. getAt( normal, target ) {
  16768. // normal is assumed to be unit length
  16769. const x = normal.x, y = normal.y, z = normal.z;
  16770. const coeff = this.coefficients;
  16771. // band 0
  16772. target.copy( coeff[ 0 ] ).multiplyScalar( 0.282095 );
  16773. // band 1
  16774. target.addScaledVector( coeff[ 1 ], 0.488603 * y );
  16775. target.addScaledVector( coeff[ 2 ], 0.488603 * z );
  16776. target.addScaledVector( coeff[ 3 ], 0.488603 * x );
  16777. // band 2
  16778. target.addScaledVector( coeff[ 4 ], 1.092548 * ( x * y ) );
  16779. target.addScaledVector( coeff[ 5 ], 1.092548 * ( y * z ) );
  16780. target.addScaledVector( coeff[ 6 ], 0.315392 * ( 3.0 * z * z - 1.0 ) );
  16781. target.addScaledVector( coeff[ 7 ], 1.092548 * ( x * z ) );
  16782. target.addScaledVector( coeff[ 8 ], 0.546274 * ( x * x - y * y ) );
  16783. return target;
  16784. }
  16785. // get the irradiance (radiance convolved with cosine lobe) in the direction of the normal
  16786. // target is a Vector3
  16787. // https://graphics.stanford.edu/papers/envmap/envmap.pdf
  16788. getIrradianceAt( normal, target ) {
  16789. // normal is assumed to be unit length
  16790. const x = normal.x, y = normal.y, z = normal.z;
  16791. const coeff = this.coefficients;
  16792. // band 0
  16793. target.copy( coeff[ 0 ] ).multiplyScalar( 0.886227 ); // π * 0.282095
  16794. // band 1
  16795. target.addScaledVector( coeff[ 1 ], 2.0 * 0.511664 * y ); // ( 2 * π / 3 ) * 0.488603
  16796. target.addScaledVector( coeff[ 2 ], 2.0 * 0.511664 * z );
  16797. target.addScaledVector( coeff[ 3 ], 2.0 * 0.511664 * x );
  16798. // band 2
  16799. target.addScaledVector( coeff[ 4 ], 2.0 * 0.429043 * x * y ); // ( π / 4 ) * 1.092548
  16800. target.addScaledVector( coeff[ 5 ], 2.0 * 0.429043 * y * z );
  16801. target.addScaledVector( coeff[ 6 ], 0.743125 * z * z - 0.247708 ); // ( π / 4 ) * 0.315392 * 3
  16802. target.addScaledVector( coeff[ 7 ], 2.0 * 0.429043 * x * z );
  16803. target.addScaledVector( coeff[ 8 ], 0.429043 * ( x * x - y * y ) ); // ( π / 4 ) * 0.546274
  16804. return target;
  16805. }
  16806. add( sh ) {
  16807. for ( let i = 0; i < 9; i ++ ) {
  16808. this.coefficients[ i ].add( sh.coefficients[ i ] );
  16809. }
  16810. return this;
  16811. }
  16812. addScaledSH( sh, s ) {
  16813. for ( let i = 0; i < 9; i ++ ) {
  16814. this.coefficients[ i ].addScaledVector( sh.coefficients[ i ], s );
  16815. }
  16816. return this;
  16817. }
  16818. scale( s ) {
  16819. for ( let i = 0; i < 9; i ++ ) {
  16820. this.coefficients[ i ].multiplyScalar( s );
  16821. }
  16822. return this;
  16823. }
  16824. lerp( sh, alpha ) {
  16825. for ( let i = 0; i < 9; i ++ ) {
  16826. this.coefficients[ i ].lerp( sh.coefficients[ i ], alpha );
  16827. }
  16828. return this;
  16829. }
  16830. equals( sh ) {
  16831. for ( let i = 0; i < 9; i ++ ) {
  16832. if ( ! this.coefficients[ i ].equals( sh.coefficients[ i ] ) ) {
  16833. return false;
  16834. }
  16835. }
  16836. return true;
  16837. }
  16838. copy( sh ) {
  16839. return this.set( sh.coefficients );
  16840. }
  16841. clone() {
  16842. return new this.constructor().copy( this );
  16843. }
  16844. fromArray( array, offset = 0 ) {
  16845. const coefficients = this.coefficients;
  16846. for ( let i = 0; i < 9; i ++ ) {
  16847. coefficients[ i ].fromArray( array, offset + ( i * 3 ) );
  16848. }
  16849. return this;
  16850. }
  16851. toArray( array = [], offset = 0 ) {
  16852. const coefficients = this.coefficients;
  16853. for ( let i = 0; i < 9; i ++ ) {
  16854. coefficients[ i ].toArray( array, offset + ( i * 3 ) );
  16855. }
  16856. return array;
  16857. }
  16858. // evaluate the basis functions
  16859. // shBasis is an Array[ 9 ]
  16860. static getBasisAt( normal, shBasis ) {
  16861. // normal is assumed to be unit length
  16862. const x = normal.x, y = normal.y, z = normal.z;
  16863. // band 0
  16864. shBasis[ 0 ] = 0.282095;
  16865. // band 1
  16866. shBasis[ 1 ] = 0.488603 * y;
  16867. shBasis[ 2 ] = 0.488603 * z;
  16868. shBasis[ 3 ] = 0.488603 * x;
  16869. // band 2
  16870. shBasis[ 4 ] = 1.092548 * x * y;
  16871. shBasis[ 5 ] = 1.092548 * y * z;
  16872. shBasis[ 6 ] = 0.315392 * ( 3 * z * z - 1 );
  16873. shBasis[ 7 ] = 1.092548 * x * z;
  16874. shBasis[ 8 ] = 0.546274 * ( x * x - y * y );
  16875. }
  16876. }
  16877. class LightProbe extends Light {
  16878. constructor( sh = new SphericalHarmonics3(), intensity = 1 ) {
  16879. super( undefined, intensity );
  16880. this.isLightProbe = true;
  16881. this.sh = sh;
  16882. }
  16883. copy( source ) {
  16884. super.copy( source );
  16885. this.sh.copy( source.sh );
  16886. return this;
  16887. }
  16888. fromJSON( json ) {
  16889. this.intensity = json.intensity; // TODO: Move this bit to Light.fromJSON();
  16890. this.sh.fromArray( json.sh );
  16891. return this;
  16892. }
  16893. toJSON( meta ) {
  16894. const data = super.toJSON( meta );
  16895. data.object.sh = this.sh.toArray();
  16896. return data;
  16897. }
  16898. }
  16899. class MaterialLoader extends Loader {
  16900. constructor( manager ) {
  16901. super( manager );
  16902. this.textures = {};
  16903. }
  16904. load( url, onLoad, onProgress, onError ) {
  16905. const scope = this;
  16906. const loader = new FileLoader( scope.manager );
  16907. loader.setPath( scope.path );
  16908. loader.setRequestHeader( scope.requestHeader );
  16909. loader.setWithCredentials( scope.withCredentials );
  16910. loader.load( url, function ( text ) {
  16911. try {
  16912. onLoad( scope.parse( JSON.parse( text ) ) );
  16913. } catch ( e ) {
  16914. if ( onError ) {
  16915. onError( e );
  16916. } else {
  16917. console.error( e );
  16918. }
  16919. scope.manager.itemError( url );
  16920. }
  16921. }, onProgress, onError );
  16922. }
  16923. parse( json ) {
  16924. const textures = this.textures;
  16925. function getTexture( name ) {
  16926. if ( textures[ name ] === undefined ) {
  16927. console.warn( 'THREE.MaterialLoader: Undefined texture', name );
  16928. }
  16929. return textures[ name ];
  16930. }
  16931. const material = this.createMaterialFromType( json.type );
  16932. if ( json.uuid !== undefined ) material.uuid = json.uuid;
  16933. if ( json.name !== undefined ) material.name = json.name;
  16934. if ( json.color !== undefined && material.color !== undefined ) material.color.setHex( json.color );
  16935. if ( json.roughness !== undefined ) material.roughness = json.roughness;
  16936. if ( json.metalness !== undefined ) material.metalness = json.metalness;
  16937. if ( json.sheen !== undefined ) material.sheen = json.sheen;
  16938. if ( json.sheenColor !== undefined ) material.sheenColor = new Color().setHex( json.sheenColor );
  16939. if ( json.sheenRoughness !== undefined ) material.sheenRoughness = json.sheenRoughness;
  16940. if ( json.emissive !== undefined && material.emissive !== undefined ) material.emissive.setHex( json.emissive );
  16941. if ( json.specular !== undefined && material.specular !== undefined ) material.specular.setHex( json.specular );
  16942. if ( json.specularIntensity !== undefined ) material.specularIntensity = json.specularIntensity;
  16943. if ( json.specularColor !== undefined && material.specularColor !== undefined ) material.specularColor.setHex( json.specularColor );
  16944. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  16945. if ( json.clearcoat !== undefined ) material.clearcoat = json.clearcoat;
  16946. if ( json.clearcoatRoughness !== undefined ) material.clearcoatRoughness = json.clearcoatRoughness;
  16947. if ( json.dispersion !== undefined ) material.dispersion = json.dispersion;
  16948. if ( json.iridescence !== undefined ) material.iridescence = json.iridescence;
  16949. if ( json.iridescenceIOR !== undefined ) material.iridescenceIOR = json.iridescenceIOR;
  16950. if ( json.iridescenceThicknessRange !== undefined ) material.iridescenceThicknessRange = json.iridescenceThicknessRange;
  16951. if ( json.transmission !== undefined ) material.transmission = json.transmission;
  16952. if ( json.thickness !== undefined ) material.thickness = json.thickness;
  16953. if ( json.attenuationDistance !== undefined ) material.attenuationDistance = json.attenuationDistance;
  16954. if ( json.attenuationColor !== undefined && material.attenuationColor !== undefined ) material.attenuationColor.setHex( json.attenuationColor );
  16955. if ( json.anisotropy !== undefined ) material.anisotropy = json.anisotropy;
  16956. if ( json.anisotropyRotation !== undefined ) material.anisotropyRotation = json.anisotropyRotation;
  16957. if ( json.fog !== undefined ) material.fog = json.fog;
  16958. if ( json.flatShading !== undefined ) material.flatShading = json.flatShading;
  16959. if ( json.blending !== undefined ) material.blending = json.blending;
  16960. if ( json.combine !== undefined ) material.combine = json.combine;
  16961. if ( json.side !== undefined ) material.side = json.side;
  16962. if ( json.shadowSide !== undefined ) material.shadowSide = json.shadowSide;
  16963. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  16964. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  16965. if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
  16966. if ( json.alphaHash !== undefined ) material.alphaHash = json.alphaHash;
  16967. if ( json.depthFunc !== undefined ) material.depthFunc = json.depthFunc;
  16968. if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
  16969. if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
  16970. if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
  16971. if ( json.blendSrc !== undefined ) material.blendSrc = json.blendSrc;
  16972. if ( json.blendDst !== undefined ) material.blendDst = json.blendDst;
  16973. if ( json.blendEquation !== undefined ) material.blendEquation = json.blendEquation;
  16974. if ( json.blendSrcAlpha !== undefined ) material.blendSrcAlpha = json.blendSrcAlpha;
  16975. if ( json.blendDstAlpha !== undefined ) material.blendDstAlpha = json.blendDstAlpha;
  16976. if ( json.blendEquationAlpha !== undefined ) material.blendEquationAlpha = json.blendEquationAlpha;
  16977. if ( json.blendColor !== undefined && material.blendColor !== undefined ) material.blendColor.setHex( json.blendColor );
  16978. if ( json.blendAlpha !== undefined ) material.blendAlpha = json.blendAlpha;
  16979. if ( json.stencilWriteMask !== undefined ) material.stencilWriteMask = json.stencilWriteMask;
  16980. if ( json.stencilFunc !== undefined ) material.stencilFunc = json.stencilFunc;
  16981. if ( json.stencilRef !== undefined ) material.stencilRef = json.stencilRef;
  16982. if ( json.stencilFuncMask !== undefined ) material.stencilFuncMask = json.stencilFuncMask;
  16983. if ( json.stencilFail !== undefined ) material.stencilFail = json.stencilFail;
  16984. if ( json.stencilZFail !== undefined ) material.stencilZFail = json.stencilZFail;
  16985. if ( json.stencilZPass !== undefined ) material.stencilZPass = json.stencilZPass;
  16986. if ( json.stencilWrite !== undefined ) material.stencilWrite = json.stencilWrite;
  16987. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  16988. if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
  16989. if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap;
  16990. if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin;
  16991. if ( json.rotation !== undefined ) material.rotation = json.rotation;
  16992. if ( json.linewidth !== undefined ) material.linewidth = json.linewidth;
  16993. if ( json.dashSize !== undefined ) material.dashSize = json.dashSize;
  16994. if ( json.gapSize !== undefined ) material.gapSize = json.gapSize;
  16995. if ( json.scale !== undefined ) material.scale = json.scale;
  16996. if ( json.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset;
  16997. if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor;
  16998. if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits;
  16999. if ( json.dithering !== undefined ) material.dithering = json.dithering;
  17000. if ( json.alphaToCoverage !== undefined ) material.alphaToCoverage = json.alphaToCoverage;
  17001. if ( json.premultipliedAlpha !== undefined ) material.premultipliedAlpha = json.premultipliedAlpha;
  17002. if ( json.forceSinglePass !== undefined ) material.forceSinglePass = json.forceSinglePass;
  17003. if ( json.visible !== undefined ) material.visible = json.visible;
  17004. if ( json.toneMapped !== undefined ) material.toneMapped = json.toneMapped;
  17005. if ( json.userData !== undefined ) material.userData = json.userData;
  17006. if ( json.vertexColors !== undefined ) {
  17007. if ( typeof json.vertexColors === 'number' ) {
  17008. material.vertexColors = ( json.vertexColors > 0 ) ? true : false;
  17009. } else {
  17010. material.vertexColors = json.vertexColors;
  17011. }
  17012. }
  17013. // Shader Material
  17014. if ( json.uniforms !== undefined ) {
  17015. for ( const name in json.uniforms ) {
  17016. const uniform = json.uniforms[ name ];
  17017. material.uniforms[ name ] = {};
  17018. switch ( uniform.type ) {
  17019. case 't':
  17020. material.uniforms[ name ].value = getTexture( uniform.value );
  17021. break;
  17022. case 'c':
  17023. material.uniforms[ name ].value = new Color().setHex( uniform.value );
  17024. break;
  17025. case 'v2':
  17026. material.uniforms[ name ].value = new Vector2().fromArray( uniform.value );
  17027. break;
  17028. case 'v3':
  17029. material.uniforms[ name ].value = new Vector3().fromArray( uniform.value );
  17030. break;
  17031. case 'v4':
  17032. material.uniforms[ name ].value = new Vector4().fromArray( uniform.value );
  17033. break;
  17034. case 'm3':
  17035. material.uniforms[ name ].value = new Matrix3().fromArray( uniform.value );
  17036. break;
  17037. case 'm4':
  17038. material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value );
  17039. break;
  17040. default:
  17041. material.uniforms[ name ].value = uniform.value;
  17042. }
  17043. }
  17044. }
  17045. if ( json.defines !== undefined ) material.defines = json.defines;
  17046. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  17047. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  17048. if ( json.glslVersion !== undefined ) material.glslVersion = json.glslVersion;
  17049. if ( json.extensions !== undefined ) {
  17050. for ( const key in json.extensions ) {
  17051. material.extensions[ key ] = json.extensions[ key ];
  17052. }
  17053. }
  17054. if ( json.lights !== undefined ) material.lights = json.lights;
  17055. if ( json.clipping !== undefined ) material.clipping = json.clipping;
  17056. // for PointsMaterial
  17057. if ( json.size !== undefined ) material.size = json.size;
  17058. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  17059. // maps
  17060. if ( json.map !== undefined ) material.map = getTexture( json.map );
  17061. if ( json.matcap !== undefined ) material.matcap = getTexture( json.matcap );
  17062. if ( json.alphaMap !== undefined ) material.alphaMap = getTexture( json.alphaMap );
  17063. if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
  17064. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  17065. if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
  17066. if ( json.normalMapType !== undefined ) material.normalMapType = json.normalMapType;
  17067. if ( json.normalScale !== undefined ) {
  17068. let normalScale = json.normalScale;
  17069. if ( Array.isArray( normalScale ) === false ) {
  17070. // Blender exporter used to export a scalar. See #7459
  17071. normalScale = [ normalScale, normalScale ];
  17072. }
  17073. material.normalScale = new Vector2().fromArray( normalScale );
  17074. }
  17075. if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
  17076. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  17077. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  17078. if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
  17079. if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );
  17080. if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
  17081. if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
  17082. if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );
  17083. if ( json.specularIntensityMap !== undefined ) material.specularIntensityMap = getTexture( json.specularIntensityMap );
  17084. if ( json.specularColorMap !== undefined ) material.specularColorMap = getTexture( json.specularColorMap );
  17085. if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );
  17086. if ( json.envMapRotation !== undefined ) material.envMapRotation.fromArray( json.envMapRotation );
  17087. if ( json.envMapIntensity !== undefined ) material.envMapIntensity = json.envMapIntensity;
  17088. if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
  17089. if ( json.refractionRatio !== undefined ) material.refractionRatio = json.refractionRatio;
  17090. if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
  17091. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  17092. if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
  17093. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  17094. if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );
  17095. if ( json.clearcoatMap !== undefined ) material.clearcoatMap = getTexture( json.clearcoatMap );
  17096. if ( json.clearcoatRoughnessMap !== undefined ) material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap );
  17097. if ( json.clearcoatNormalMap !== undefined ) material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap );
  17098. if ( json.clearcoatNormalScale !== undefined ) material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale );
  17099. if ( json.iridescenceMap !== undefined ) material.iridescenceMap = getTexture( json.iridescenceMap );
  17100. if ( json.iridescenceThicknessMap !== undefined ) material.iridescenceThicknessMap = getTexture( json.iridescenceThicknessMap );
  17101. if ( json.transmissionMap !== undefined ) material.transmissionMap = getTexture( json.transmissionMap );
  17102. if ( json.thicknessMap !== undefined ) material.thicknessMap = getTexture( json.thicknessMap );
  17103. if ( json.anisotropyMap !== undefined ) material.anisotropyMap = getTexture( json.anisotropyMap );
  17104. if ( json.sheenColorMap !== undefined ) material.sheenColorMap = getTexture( json.sheenColorMap );
  17105. if ( json.sheenRoughnessMap !== undefined ) material.sheenRoughnessMap = getTexture( json.sheenRoughnessMap );
  17106. return material;
  17107. }
  17108. setTextures( value ) {
  17109. this.textures = value;
  17110. return this;
  17111. }
  17112. createMaterialFromType( type ) {
  17113. return MaterialLoader.createMaterialFromType( type );
  17114. }
  17115. static createMaterialFromType( type ) {
  17116. const materialLib = {
  17117. ShadowMaterial,
  17118. SpriteMaterial,
  17119. RawShaderMaterial,
  17120. ShaderMaterial,
  17121. PointsMaterial,
  17122. MeshPhysicalMaterial,
  17123. MeshStandardMaterial,
  17124. MeshPhongMaterial,
  17125. MeshToonMaterial,
  17126. MeshNormalMaterial,
  17127. MeshLambertMaterial,
  17128. MeshDepthMaterial,
  17129. MeshDistanceMaterial,
  17130. MeshBasicMaterial,
  17131. MeshMatcapMaterial,
  17132. LineDashedMaterial,
  17133. LineBasicMaterial,
  17134. Material
  17135. };
  17136. return new materialLib[ type ]();
  17137. }
  17138. }
  17139. class LoaderUtils {
  17140. static decodeText( array ) { // @deprecated, r165
  17141. console.warn( 'THREE.LoaderUtils: decodeText() has been deprecated with r165 and will be removed with r175. Use TextDecoder instead.' );
  17142. if ( typeof TextDecoder !== 'undefined' ) {
  17143. return new TextDecoder().decode( array );
  17144. }
  17145. // Avoid the String.fromCharCode.apply(null, array) shortcut, which
  17146. // throws a "maximum call stack size exceeded" error for large arrays.
  17147. let s = '';
  17148. for ( let i = 0, il = array.length; i < il; i ++ ) {
  17149. // Implicitly assumes little-endian.
  17150. s += String.fromCharCode( array[ i ] );
  17151. }
  17152. try {
  17153. // merges multi-byte utf-8 characters.
  17154. return decodeURIComponent( escape( s ) );
  17155. } catch ( e ) { // see #16358
  17156. return s;
  17157. }
  17158. }
  17159. static extractUrlBase( url ) {
  17160. const index = url.lastIndexOf( '/' );
  17161. if ( index === - 1 ) return './';
  17162. return url.slice( 0, index + 1 );
  17163. }
  17164. static resolveURL( url, path ) {
  17165. // Invalid URL
  17166. if ( typeof url !== 'string' || url === '' ) return '';
  17167. // Host Relative URL
  17168. if ( /^https?:\/\//i.test( path ) && /^\//.test( url ) ) {
  17169. path = path.replace( /(^https?:\/\/[^\/]+).*/i, '$1' );
  17170. }
  17171. // Absolute URL http://,https://,//
  17172. if ( /^(https?:)?\/\//i.test( url ) ) return url;
  17173. // Data URI
  17174. if ( /^data:.*,.*$/i.test( url ) ) return url;
  17175. // Blob URL
  17176. if ( /^blob:.*$/i.test( url ) ) return url;
  17177. // Relative URL
  17178. return path + url;
  17179. }
  17180. }
  17181. class InstancedBufferGeometry extends BufferGeometry {
  17182. constructor() {
  17183. super();
  17184. this.isInstancedBufferGeometry = true;
  17185. this.type = 'InstancedBufferGeometry';
  17186. this.instanceCount = Infinity;
  17187. }
  17188. copy( source ) {
  17189. super.copy( source );
  17190. this.instanceCount = source.instanceCount;
  17191. return this;
  17192. }
  17193. toJSON() {
  17194. const data = super.toJSON();
  17195. data.instanceCount = this.instanceCount;
  17196. data.isInstancedBufferGeometry = true;
  17197. return data;
  17198. }
  17199. }
  17200. class BufferGeometryLoader extends Loader {
  17201. constructor( manager ) {
  17202. super( manager );
  17203. }
  17204. load( url, onLoad, onProgress, onError ) {
  17205. const scope = this;
  17206. const loader = new FileLoader( scope.manager );
  17207. loader.setPath( scope.path );
  17208. loader.setRequestHeader( scope.requestHeader );
  17209. loader.setWithCredentials( scope.withCredentials );
  17210. loader.load( url, function ( text ) {
  17211. try {
  17212. onLoad( scope.parse( JSON.parse( text ) ) );
  17213. } catch ( e ) {
  17214. if ( onError ) {
  17215. onError( e );
  17216. } else {
  17217. console.error( e );
  17218. }
  17219. scope.manager.itemError( url );
  17220. }
  17221. }, onProgress, onError );
  17222. }
  17223. parse( json ) {
  17224. const interleavedBufferMap = {};
  17225. const arrayBufferMap = {};
  17226. function getInterleavedBuffer( json, uuid ) {
  17227. if ( interleavedBufferMap[ uuid ] !== undefined ) return interleavedBufferMap[ uuid ];
  17228. const interleavedBuffers = json.interleavedBuffers;
  17229. const interleavedBuffer = interleavedBuffers[ uuid ];
  17230. const buffer = getArrayBuffer( json, interleavedBuffer.buffer );
  17231. const array = getTypedArray( interleavedBuffer.type, buffer );
  17232. const ib = new InterleavedBuffer( array, interleavedBuffer.stride );
  17233. ib.uuid = interleavedBuffer.uuid;
  17234. interleavedBufferMap[ uuid ] = ib;
  17235. return ib;
  17236. }
  17237. function getArrayBuffer( json, uuid ) {
  17238. if ( arrayBufferMap[ uuid ] !== undefined ) return arrayBufferMap[ uuid ];
  17239. const arrayBuffers = json.arrayBuffers;
  17240. const arrayBuffer = arrayBuffers[ uuid ];
  17241. const ab = new Uint32Array( arrayBuffer ).buffer;
  17242. arrayBufferMap[ uuid ] = ab;
  17243. return ab;
  17244. }
  17245. const geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry();
  17246. const index = json.data.index;
  17247. if ( index !== undefined ) {
  17248. const typedArray = getTypedArray( index.type, index.array );
  17249. geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
  17250. }
  17251. const attributes = json.data.attributes;
  17252. for ( const key in attributes ) {
  17253. const attribute = attributes[ key ];
  17254. let bufferAttribute;
  17255. if ( attribute.isInterleavedBufferAttribute ) {
  17256. const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
  17257. bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );
  17258. } else {
  17259. const typedArray = getTypedArray( attribute.type, attribute.array );
  17260. const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
  17261. bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );
  17262. }
  17263. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  17264. if ( attribute.usage !== undefined ) bufferAttribute.setUsage( attribute.usage );
  17265. geometry.setAttribute( key, bufferAttribute );
  17266. }
  17267. const morphAttributes = json.data.morphAttributes;
  17268. if ( morphAttributes ) {
  17269. for ( const key in morphAttributes ) {
  17270. const attributeArray = morphAttributes[ key ];
  17271. const array = [];
  17272. for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
  17273. const attribute = attributeArray[ i ];
  17274. let bufferAttribute;
  17275. if ( attribute.isInterleavedBufferAttribute ) {
  17276. const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
  17277. bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );
  17278. } else {
  17279. const typedArray = getTypedArray( attribute.type, attribute.array );
  17280. bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
  17281. }
  17282. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  17283. array.push( bufferAttribute );
  17284. }
  17285. geometry.morphAttributes[ key ] = array;
  17286. }
  17287. }
  17288. const morphTargetsRelative = json.data.morphTargetsRelative;
  17289. if ( morphTargetsRelative ) {
  17290. geometry.morphTargetsRelative = true;
  17291. }
  17292. const groups = json.data.groups || json.data.drawcalls || json.data.offsets;
  17293. if ( groups !== undefined ) {
  17294. for ( let i = 0, n = groups.length; i !== n; ++ i ) {
  17295. const group = groups[ i ];
  17296. geometry.addGroup( group.start, group.count, group.materialIndex );
  17297. }
  17298. }
  17299. const boundingSphere = json.data.boundingSphere;
  17300. if ( boundingSphere !== undefined ) {
  17301. const center = new Vector3();
  17302. if ( boundingSphere.center !== undefined ) {
  17303. center.fromArray( boundingSphere.center );
  17304. }
  17305. geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
  17306. }
  17307. if ( json.name ) geometry.name = json.name;
  17308. if ( json.userData ) geometry.userData = json.userData;
  17309. return geometry;
  17310. }
  17311. }
  17312. class ObjectLoader extends Loader {
  17313. constructor( manager ) {
  17314. super( manager );
  17315. }
  17316. load( url, onLoad, onProgress, onError ) {
  17317. const scope = this;
  17318. const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  17319. this.resourcePath = this.resourcePath || path;
  17320. const loader = new FileLoader( this.manager );
  17321. loader.setPath( this.path );
  17322. loader.setRequestHeader( this.requestHeader );
  17323. loader.setWithCredentials( this.withCredentials );
  17324. loader.load( url, function ( text ) {
  17325. let json = null;
  17326. try {
  17327. json = JSON.parse( text );
  17328. } catch ( error ) {
  17329. if ( onError !== undefined ) onError( error );
  17330. console.error( 'THREE:ObjectLoader: Can\'t parse ' + url + '.', error.message );
  17331. return;
  17332. }
  17333. const metadata = json.metadata;
  17334. if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
  17335. if ( onError !== undefined ) onError( new Error( 'THREE.ObjectLoader: Can\'t load ' + url ) );
  17336. console.error( 'THREE.ObjectLoader: Can\'t load ' + url );
  17337. return;
  17338. }
  17339. scope.parse( json, onLoad );
  17340. }, onProgress, onError );
  17341. }
  17342. async loadAsync( url, onProgress ) {
  17343. const scope = this;
  17344. const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  17345. this.resourcePath = this.resourcePath || path;
  17346. const loader = new FileLoader( this.manager );
  17347. loader.setPath( this.path );
  17348. loader.setRequestHeader( this.requestHeader );
  17349. loader.setWithCredentials( this.withCredentials );
  17350. const text = await loader.loadAsync( url, onProgress );
  17351. const json = JSON.parse( text );
  17352. const metadata = json.metadata;
  17353. if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
  17354. throw new Error( 'THREE.ObjectLoader: Can\'t load ' + url );
  17355. }
  17356. return await scope.parseAsync( json );
  17357. }
  17358. parse( json, onLoad ) {
  17359. const animations = this.parseAnimations( json.animations );
  17360. const shapes = this.parseShapes( json.shapes );
  17361. const geometries = this.parseGeometries( json.geometries, shapes );
  17362. const images = this.parseImages( json.images, function () {
  17363. if ( onLoad !== undefined ) onLoad( object );
  17364. } );
  17365. const textures = this.parseTextures( json.textures, images );
  17366. const materials = this.parseMaterials( json.materials, textures );
  17367. const object = this.parseObject( json.object, geometries, materials, textures, animations );
  17368. const skeletons = this.parseSkeletons( json.skeletons, object );
  17369. this.bindSkeletons( object, skeletons );
  17370. this.bindLightTargets( object );
  17371. //
  17372. if ( onLoad !== undefined ) {
  17373. let hasImages = false;
  17374. for ( const uuid in images ) {
  17375. if ( images[ uuid ].data instanceof HTMLImageElement ) {
  17376. hasImages = true;
  17377. break;
  17378. }
  17379. }
  17380. if ( hasImages === false ) onLoad( object );
  17381. }
  17382. return object;
  17383. }
  17384. async parseAsync( json ) {
  17385. const animations = this.parseAnimations( json.animations );
  17386. const shapes = this.parseShapes( json.shapes );
  17387. const geometries = this.parseGeometries( json.geometries, shapes );
  17388. const images = await this.parseImagesAsync( json.images );
  17389. const textures = this.parseTextures( json.textures, images );
  17390. const materials = this.parseMaterials( json.materials, textures );
  17391. const object = this.parseObject( json.object, geometries, materials, textures, animations );
  17392. const skeletons = this.parseSkeletons( json.skeletons, object );
  17393. this.bindSkeletons( object, skeletons );
  17394. this.bindLightTargets( object );
  17395. return object;
  17396. }
  17397. parseShapes( json ) {
  17398. const shapes = {};
  17399. if ( json !== undefined ) {
  17400. for ( let i = 0, l = json.length; i < l; i ++ ) {
  17401. const shape = new Shape().fromJSON( json[ i ] );
  17402. shapes[ shape.uuid ] = shape;
  17403. }
  17404. }
  17405. return shapes;
  17406. }
  17407. parseSkeletons( json, object ) {
  17408. const skeletons = {};
  17409. const bones = {};
  17410. // generate bone lookup table
  17411. object.traverse( function ( child ) {
  17412. if ( child.isBone ) bones[ child.uuid ] = child;
  17413. } );
  17414. // create skeletons
  17415. if ( json !== undefined ) {
  17416. for ( let i = 0, l = json.length; i < l; i ++ ) {
  17417. const skeleton = new Skeleton().fromJSON( json[ i ], bones );
  17418. skeletons[ skeleton.uuid ] = skeleton;
  17419. }
  17420. }
  17421. return skeletons;
  17422. }
  17423. parseGeometries( json, shapes ) {
  17424. const geometries = {};
  17425. if ( json !== undefined ) {
  17426. const bufferGeometryLoader = new BufferGeometryLoader();
  17427. for ( let i = 0, l = json.length; i < l; i ++ ) {
  17428. let geometry;
  17429. const data = json[ i ];
  17430. switch ( data.type ) {
  17431. case 'BufferGeometry':
  17432. case 'InstancedBufferGeometry':
  17433. geometry = bufferGeometryLoader.parse( data );
  17434. break;
  17435. default:
  17436. if ( data.type in Geometries ) {
  17437. geometry = Geometries[ data.type ].fromJSON( data, shapes );
  17438. } else {
  17439. console.warn( `THREE.ObjectLoader: Unsupported geometry type "${ data.type }"` );
  17440. }
  17441. }
  17442. geometry.uuid = data.uuid;
  17443. if ( data.name !== undefined ) geometry.name = data.name;
  17444. if ( data.userData !== undefined ) geometry.userData = data.userData;
  17445. geometries[ data.uuid ] = geometry;
  17446. }
  17447. }
  17448. return geometries;
  17449. }
  17450. parseMaterials( json, textures ) {
  17451. const cache = {}; // MultiMaterial
  17452. const materials = {};
  17453. if ( json !== undefined ) {
  17454. const loader = new MaterialLoader();
  17455. loader.setTextures( textures );
  17456. for ( let i = 0, l = json.length; i < l; i ++ ) {
  17457. const data = json[ i ];
  17458. if ( cache[ data.uuid ] === undefined ) {
  17459. cache[ data.uuid ] = loader.parse( data );
  17460. }
  17461. materials[ data.uuid ] = cache[ data.uuid ];
  17462. }
  17463. }
  17464. return materials;
  17465. }
  17466. parseAnimations( json ) {
  17467. const animations = {};
  17468. if ( json !== undefined ) {
  17469. for ( let i = 0; i < json.length; i ++ ) {
  17470. const data = json[ i ];
  17471. const clip = AnimationClip.parse( data );
  17472. animations[ clip.uuid ] = clip;
  17473. }
  17474. }
  17475. return animations;
  17476. }
  17477. parseImages( json, onLoad ) {
  17478. const scope = this;
  17479. const images = {};
  17480. let loader;
  17481. function loadImage( url ) {
  17482. scope.manager.itemStart( url );
  17483. return loader.load( url, function () {
  17484. scope.manager.itemEnd( url );
  17485. }, undefined, function () {
  17486. scope.manager.itemError( url );
  17487. scope.manager.itemEnd( url );
  17488. } );
  17489. }
  17490. function deserializeImage( image ) {
  17491. if ( typeof image === 'string' ) {
  17492. const url = image;
  17493. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  17494. return loadImage( path );
  17495. } else {
  17496. if ( image.data ) {
  17497. return {
  17498. data: getTypedArray( image.type, image.data ),
  17499. width: image.width,
  17500. height: image.height
  17501. };
  17502. } else {
  17503. return null;
  17504. }
  17505. }
  17506. }
  17507. if ( json !== undefined && json.length > 0 ) {
  17508. const manager = new LoadingManager( onLoad );
  17509. loader = new ImageLoader( manager );
  17510. loader.setCrossOrigin( this.crossOrigin );
  17511. for ( let i = 0, il = json.length; i < il; i ++ ) {
  17512. const image = json[ i ];
  17513. const url = image.url;
  17514. if ( Array.isArray( url ) ) {
  17515. // load array of images e.g CubeTexture
  17516. const imageArray = [];
  17517. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  17518. const currentUrl = url[ j ];
  17519. const deserializedImage = deserializeImage( currentUrl );
  17520. if ( deserializedImage !== null ) {
  17521. if ( deserializedImage instanceof HTMLImageElement ) {
  17522. imageArray.push( deserializedImage );
  17523. } else {
  17524. // special case: handle array of data textures for cube textures
  17525. imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  17526. }
  17527. }
  17528. }
  17529. images[ image.uuid ] = new Source( imageArray );
  17530. } else {
  17531. // load single image
  17532. const deserializedImage = deserializeImage( image.url );
  17533. images[ image.uuid ] = new Source( deserializedImage );
  17534. }
  17535. }
  17536. }
  17537. return images;
  17538. }
  17539. async parseImagesAsync( json ) {
  17540. const scope = this;
  17541. const images = {};
  17542. let loader;
  17543. async function deserializeImage( image ) {
  17544. if ( typeof image === 'string' ) {
  17545. const url = image;
  17546. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  17547. return await loader.loadAsync( path );
  17548. } else {
  17549. if ( image.data ) {
  17550. return {
  17551. data: getTypedArray( image.type, image.data ),
  17552. width: image.width,
  17553. height: image.height
  17554. };
  17555. } else {
  17556. return null;
  17557. }
  17558. }
  17559. }
  17560. if ( json !== undefined && json.length > 0 ) {
  17561. loader = new ImageLoader( this.manager );
  17562. loader.setCrossOrigin( this.crossOrigin );
  17563. for ( let i = 0, il = json.length; i < il; i ++ ) {
  17564. const image = json[ i ];
  17565. const url = image.url;
  17566. if ( Array.isArray( url ) ) {
  17567. // load array of images e.g CubeTexture
  17568. const imageArray = [];
  17569. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  17570. const currentUrl = url[ j ];
  17571. const deserializedImage = await deserializeImage( currentUrl );
  17572. if ( deserializedImage !== null ) {
  17573. if ( deserializedImage instanceof HTMLImageElement ) {
  17574. imageArray.push( deserializedImage );
  17575. } else {
  17576. // special case: handle array of data textures for cube textures
  17577. imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  17578. }
  17579. }
  17580. }
  17581. images[ image.uuid ] = new Source( imageArray );
  17582. } else {
  17583. // load single image
  17584. const deserializedImage = await deserializeImage( image.url );
  17585. images[ image.uuid ] = new Source( deserializedImage );
  17586. }
  17587. }
  17588. }
  17589. return images;
  17590. }
  17591. parseTextures( json, images ) {
  17592. function parseConstant( value, type ) {
  17593. if ( typeof value === 'number' ) return value;
  17594. console.warn( 'THREE.ObjectLoader.parseTexture: Constant should be in numeric form.', value );
  17595. return type[ value ];
  17596. }
  17597. const textures = {};
  17598. if ( json !== undefined ) {
  17599. for ( let i = 0, l = json.length; i < l; i ++ ) {
  17600. const data = json[ i ];
  17601. if ( data.image === undefined ) {
  17602. console.warn( 'THREE.ObjectLoader: No "image" specified for', data.uuid );
  17603. }
  17604. if ( images[ data.image ] === undefined ) {
  17605. console.warn( 'THREE.ObjectLoader: Undefined image', data.image );
  17606. }
  17607. const source = images[ data.image ];
  17608. const image = source.data;
  17609. let texture;
  17610. if ( Array.isArray( image ) ) {
  17611. texture = new CubeTexture();
  17612. if ( image.length === 6 ) texture.needsUpdate = true;
  17613. } else {
  17614. if ( image && image.data ) {
  17615. texture = new DataTexture();
  17616. } else {
  17617. texture = new Texture();
  17618. }
  17619. if ( image ) texture.needsUpdate = true; // textures can have undefined image data
  17620. }
  17621. texture.source = source;
  17622. texture.uuid = data.uuid;
  17623. if ( data.name !== undefined ) texture.name = data.name;
  17624. if ( data.mapping !== undefined ) texture.mapping = parseConstant( data.mapping, TEXTURE_MAPPING );
  17625. if ( data.channel !== undefined ) texture.channel = data.channel;
  17626. if ( data.offset !== undefined ) texture.offset.fromArray( data.offset );
  17627. if ( data.repeat !== undefined ) texture.repeat.fromArray( data.repeat );
  17628. if ( data.center !== undefined ) texture.center.fromArray( data.center );
  17629. if ( data.rotation !== undefined ) texture.rotation = data.rotation;
  17630. if ( data.wrap !== undefined ) {
  17631. texture.wrapS = parseConstant( data.wrap[ 0 ], TEXTURE_WRAPPING );
  17632. texture.wrapT = parseConstant( data.wrap[ 1 ], TEXTURE_WRAPPING );
  17633. }
  17634. if ( data.format !== undefined ) texture.format = data.format;
  17635. if ( data.internalFormat !== undefined ) texture.internalFormat = data.internalFormat;
  17636. if ( data.type !== undefined ) texture.type = data.type;
  17637. if ( data.colorSpace !== undefined ) texture.colorSpace = data.colorSpace;
  17638. if ( data.minFilter !== undefined ) texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER );
  17639. if ( data.magFilter !== undefined ) texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER );
  17640. if ( data.anisotropy !== undefined ) texture.anisotropy = data.anisotropy;
  17641. if ( data.flipY !== undefined ) texture.flipY = data.flipY;
  17642. if ( data.generateMipmaps !== undefined ) texture.generateMipmaps = data.generateMipmaps;
  17643. if ( data.premultiplyAlpha !== undefined ) texture.premultiplyAlpha = data.premultiplyAlpha;
  17644. if ( data.unpackAlignment !== undefined ) texture.unpackAlignment = data.unpackAlignment;
  17645. if ( data.compareFunction !== undefined ) texture.compareFunction = data.compareFunction;
  17646. if ( data.userData !== undefined ) texture.userData = data.userData;
  17647. textures[ data.uuid ] = texture;
  17648. }
  17649. }
  17650. return textures;
  17651. }
  17652. parseObject( data, geometries, materials, textures, animations ) {
  17653. let object;
  17654. function getGeometry( name ) {
  17655. if ( geometries[ name ] === undefined ) {
  17656. console.warn( 'THREE.ObjectLoader: Undefined geometry', name );
  17657. }
  17658. return geometries[ name ];
  17659. }
  17660. function getMaterial( name ) {
  17661. if ( name === undefined ) return undefined;
  17662. if ( Array.isArray( name ) ) {
  17663. const array = [];
  17664. for ( let i = 0, l = name.length; i < l; i ++ ) {
  17665. const uuid = name[ i ];
  17666. if ( materials[ uuid ] === undefined ) {
  17667. console.warn( 'THREE.ObjectLoader: Undefined material', uuid );
  17668. }
  17669. array.push( materials[ uuid ] );
  17670. }
  17671. return array;
  17672. }
  17673. if ( materials[ name ] === undefined ) {
  17674. console.warn( 'THREE.ObjectLoader: Undefined material', name );
  17675. }
  17676. return materials[ name ];
  17677. }
  17678. function getTexture( uuid ) {
  17679. if ( textures[ uuid ] === undefined ) {
  17680. console.warn( 'THREE.ObjectLoader: Undefined texture', uuid );
  17681. }
  17682. return textures[ uuid ];
  17683. }
  17684. let geometry, material;
  17685. switch ( data.type ) {
  17686. case 'Scene':
  17687. object = new Scene();
  17688. if ( data.background !== undefined ) {
  17689. if ( Number.isInteger( data.background ) ) {
  17690. object.background = new Color( data.background );
  17691. } else {
  17692. object.background = getTexture( data.background );
  17693. }
  17694. }
  17695. if ( data.environment !== undefined ) {
  17696. object.environment = getTexture( data.environment );
  17697. }
  17698. if ( data.fog !== undefined ) {
  17699. if ( data.fog.type === 'Fog' ) {
  17700. object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far );
  17701. } else if ( data.fog.type === 'FogExp2' ) {
  17702. object.fog = new FogExp2( data.fog.color, data.fog.density );
  17703. }
  17704. if ( data.fog.name !== '' ) {
  17705. object.fog.name = data.fog.name;
  17706. }
  17707. }
  17708. if ( data.backgroundBlurriness !== undefined ) object.backgroundBlurriness = data.backgroundBlurriness;
  17709. if ( data.backgroundIntensity !== undefined ) object.backgroundIntensity = data.backgroundIntensity;
  17710. if ( data.backgroundRotation !== undefined ) object.backgroundRotation.fromArray( data.backgroundRotation );
  17711. if ( data.environmentIntensity !== undefined ) object.environmentIntensity = data.environmentIntensity;
  17712. if ( data.environmentRotation !== undefined ) object.environmentRotation.fromArray( data.environmentRotation );
  17713. break;
  17714. case 'PerspectiveCamera':
  17715. object = new PerspectiveCamera( data.fov, data.aspect, data.near, data.far );
  17716. if ( data.focus !== undefined ) object.focus = data.focus;
  17717. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  17718. if ( data.filmGauge !== undefined ) object.filmGauge = data.filmGauge;
  17719. if ( data.filmOffset !== undefined ) object.filmOffset = data.filmOffset;
  17720. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  17721. break;
  17722. case 'OrthographicCamera':
  17723. object = new OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far );
  17724. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  17725. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  17726. break;
  17727. case 'AmbientLight':
  17728. object = new AmbientLight( data.color, data.intensity );
  17729. break;
  17730. case 'DirectionalLight':
  17731. object = new DirectionalLight( data.color, data.intensity );
  17732. object.target = data.target || '';
  17733. break;
  17734. case 'PointLight':
  17735. object = new PointLight( data.color, data.intensity, data.distance, data.decay );
  17736. break;
  17737. case 'RectAreaLight':
  17738. object = new RectAreaLight( data.color, data.intensity, data.width, data.height );
  17739. break;
  17740. case 'SpotLight':
  17741. object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay );
  17742. object.target = data.target || '';
  17743. break;
  17744. case 'HemisphereLight':
  17745. object = new HemisphereLight( data.color, data.groundColor, data.intensity );
  17746. break;
  17747. case 'LightProbe':
  17748. object = new LightProbe().fromJSON( data );
  17749. break;
  17750. case 'SkinnedMesh':
  17751. geometry = getGeometry( data.geometry );
  17752. material = getMaterial( data.material );
  17753. object = new SkinnedMesh( geometry, material );
  17754. if ( data.bindMode !== undefined ) object.bindMode = data.bindMode;
  17755. if ( data.bindMatrix !== undefined ) object.bindMatrix.fromArray( data.bindMatrix );
  17756. if ( data.skeleton !== undefined ) object.skeleton = data.skeleton;
  17757. break;
  17758. case 'Mesh':
  17759. geometry = getGeometry( data.geometry );
  17760. material = getMaterial( data.material );
  17761. object = new Mesh( geometry, material );
  17762. break;
  17763. case 'InstancedMesh':
  17764. geometry = getGeometry( data.geometry );
  17765. material = getMaterial( data.material );
  17766. const count = data.count;
  17767. const instanceMatrix = data.instanceMatrix;
  17768. const instanceColor = data.instanceColor;
  17769. object = new InstancedMesh( geometry, material, count );
  17770. object.instanceMatrix = new InstancedBufferAttribute( new Float32Array( instanceMatrix.array ), 16 );
  17771. if ( instanceColor !== undefined ) object.instanceColor = new InstancedBufferAttribute( new Float32Array( instanceColor.array ), instanceColor.itemSize );
  17772. break;
  17773. case 'BatchedMesh':
  17774. geometry = getGeometry( data.geometry );
  17775. material = getMaterial( data.material );
  17776. object = new BatchedMesh( data.maxInstanceCount, data.maxVertexCount, data.maxIndexCount, material );
  17777. object.geometry = geometry;
  17778. object.perObjectFrustumCulled = data.perObjectFrustumCulled;
  17779. object.sortObjects = data.sortObjects;
  17780. object._drawRanges = data.drawRanges;
  17781. object._reservedRanges = data.reservedRanges;
  17782. object._visibility = data.visibility;
  17783. object._active = data.active;
  17784. object._bounds = data.bounds.map( bound => {
  17785. const box = new Box3();
  17786. box.min.fromArray( bound.boxMin );
  17787. box.max.fromArray( bound.boxMax );
  17788. const sphere = new Sphere();
  17789. sphere.radius = bound.sphereRadius;
  17790. sphere.center.fromArray( bound.sphereCenter );
  17791. return {
  17792. boxInitialized: bound.boxInitialized,
  17793. box: box,
  17794. sphereInitialized: bound.sphereInitialized,
  17795. sphere: sphere
  17796. };
  17797. } );
  17798. object._maxInstanceCount = data.maxInstanceCount;
  17799. object._maxVertexCount = data.maxVertexCount;
  17800. object._maxIndexCount = data.maxIndexCount;
  17801. object._geometryInitialized = data.geometryInitialized;
  17802. object._geometryCount = data.geometryCount;
  17803. object._matricesTexture = getTexture( data.matricesTexture.uuid );
  17804. if ( data.colorsTexture !== undefined ) object._colorsTexture = getTexture( data.colorsTexture.uuid );
  17805. break;
  17806. case 'LOD':
  17807. object = new LOD();
  17808. break;
  17809. case 'Line':
  17810. object = new Line( getGeometry( data.geometry ), getMaterial( data.material ) );
  17811. break;
  17812. case 'LineLoop':
  17813. object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) );
  17814. break;
  17815. case 'LineSegments':
  17816. object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) );
  17817. break;
  17818. case 'PointCloud':
  17819. case 'Points':
  17820. object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) );
  17821. break;
  17822. case 'Sprite':
  17823. object = new Sprite( getMaterial( data.material ) );
  17824. break;
  17825. case 'Group':
  17826. object = new Group();
  17827. break;
  17828. case 'Bone':
  17829. object = new Bone();
  17830. break;
  17831. default:
  17832. object = new Object3D();
  17833. }
  17834. object.uuid = data.uuid;
  17835. if ( data.name !== undefined ) object.name = data.name;
  17836. if ( data.matrix !== undefined ) {
  17837. object.matrix.fromArray( data.matrix );
  17838. if ( data.matrixAutoUpdate !== undefined ) object.matrixAutoUpdate = data.matrixAutoUpdate;
  17839. if ( object.matrixAutoUpdate ) object.matrix.decompose( object.position, object.quaternion, object.scale );
  17840. } else {
  17841. if ( data.position !== undefined ) object.position.fromArray( data.position );
  17842. if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation );
  17843. if ( data.quaternion !== undefined ) object.quaternion.fromArray( data.quaternion );
  17844. if ( data.scale !== undefined ) object.scale.fromArray( data.scale );
  17845. }
  17846. if ( data.up !== undefined ) object.up.fromArray( data.up );
  17847. if ( data.castShadow !== undefined ) object.castShadow = data.castShadow;
  17848. if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow;
  17849. if ( data.shadow ) {
  17850. if ( data.shadow.intensity !== undefined ) object.shadow.intensity = data.shadow.intensity;
  17851. if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias;
  17852. if ( data.shadow.normalBias !== undefined ) object.shadow.normalBias = data.shadow.normalBias;
  17853. if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius;
  17854. if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize );
  17855. if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera );
  17856. }
  17857. if ( data.visible !== undefined ) object.visible = data.visible;
  17858. if ( data.frustumCulled !== undefined ) object.frustumCulled = data.frustumCulled;
  17859. if ( data.renderOrder !== undefined ) object.renderOrder = data.renderOrder;
  17860. if ( data.userData !== undefined ) object.userData = data.userData;
  17861. if ( data.layers !== undefined ) object.layers.mask = data.layers;
  17862. if ( data.children !== undefined ) {
  17863. const children = data.children;
  17864. for ( let i = 0; i < children.length; i ++ ) {
  17865. object.add( this.parseObject( children[ i ], geometries, materials, textures, animations ) );
  17866. }
  17867. }
  17868. if ( data.animations !== undefined ) {
  17869. const objectAnimations = data.animations;
  17870. for ( let i = 0; i < objectAnimations.length; i ++ ) {
  17871. const uuid = objectAnimations[ i ];
  17872. object.animations.push( animations[ uuid ] );
  17873. }
  17874. }
  17875. if ( data.type === 'LOD' ) {
  17876. if ( data.autoUpdate !== undefined ) object.autoUpdate = data.autoUpdate;
  17877. const levels = data.levels;
  17878. for ( let l = 0; l < levels.length; l ++ ) {
  17879. const level = levels[ l ];
  17880. const child = object.getObjectByProperty( 'uuid', level.object );
  17881. if ( child !== undefined ) {
  17882. object.addLevel( child, level.distance, level.hysteresis );
  17883. }
  17884. }
  17885. }
  17886. return object;
  17887. }
  17888. bindSkeletons( object, skeletons ) {
  17889. if ( Object.keys( skeletons ).length === 0 ) return;
  17890. object.traverse( function ( child ) {
  17891. if ( child.isSkinnedMesh === true && child.skeleton !== undefined ) {
  17892. const skeleton = skeletons[ child.skeleton ];
  17893. if ( skeleton === undefined ) {
  17894. console.warn( 'THREE.ObjectLoader: No skeleton found with UUID:', child.skeleton );
  17895. } else {
  17896. child.bind( skeleton, child.bindMatrix );
  17897. }
  17898. }
  17899. } );
  17900. }
  17901. bindLightTargets( object ) {
  17902. object.traverse( function ( child ) {
  17903. if ( child.isDirectionalLight || child.isSpotLight ) {
  17904. const uuid = child.target;
  17905. const target = object.getObjectByProperty( 'uuid', uuid );
  17906. if ( target !== undefined ) {
  17907. child.target = target;
  17908. } else {
  17909. child.target = new Object3D();
  17910. }
  17911. }
  17912. } );
  17913. }
  17914. }
  17915. const TEXTURE_MAPPING = {
  17916. UVMapping: UVMapping,
  17917. CubeReflectionMapping: CubeReflectionMapping,
  17918. CubeRefractionMapping: CubeRefractionMapping,
  17919. EquirectangularReflectionMapping: EquirectangularReflectionMapping,
  17920. EquirectangularRefractionMapping: EquirectangularRefractionMapping,
  17921. CubeUVReflectionMapping: CubeUVReflectionMapping
  17922. };
  17923. const TEXTURE_WRAPPING = {
  17924. RepeatWrapping: RepeatWrapping,
  17925. ClampToEdgeWrapping: ClampToEdgeWrapping,
  17926. MirroredRepeatWrapping: MirroredRepeatWrapping
  17927. };
  17928. const TEXTURE_FILTER = {
  17929. NearestFilter: NearestFilter,
  17930. NearestMipmapNearestFilter: NearestMipmapNearestFilter,
  17931. NearestMipmapLinearFilter: NearestMipmapLinearFilter,
  17932. LinearFilter: LinearFilter,
  17933. LinearMipmapNearestFilter: LinearMipmapNearestFilter,
  17934. LinearMipmapLinearFilter: LinearMipmapLinearFilter
  17935. };
  17936. class ImageBitmapLoader extends Loader {
  17937. constructor( manager ) {
  17938. super( manager );
  17939. this.isImageBitmapLoader = true;
  17940. if ( typeof createImageBitmap === 'undefined' ) {
  17941. console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
  17942. }
  17943. if ( typeof fetch === 'undefined' ) {
  17944. console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' );
  17945. }
  17946. this.options = { premultiplyAlpha: 'none' };
  17947. }
  17948. setOptions( options ) {
  17949. this.options = options;
  17950. return this;
  17951. }
  17952. load( url, onLoad, onProgress, onError ) {
  17953. if ( url === undefined ) url = '';
  17954. if ( this.path !== undefined ) url = this.path + url;
  17955. url = this.manager.resolveURL( url );
  17956. const scope = this;
  17957. const cached = Cache.get( url );
  17958. if ( cached !== undefined ) {
  17959. scope.manager.itemStart( url );
  17960. // If cached is a promise, wait for it to resolve
  17961. if ( cached.then ) {
  17962. cached.then( imageBitmap => {
  17963. if ( onLoad ) onLoad( imageBitmap );
  17964. scope.manager.itemEnd( url );
  17965. } ).catch( e => {
  17966. if ( onError ) onError( e );
  17967. } );
  17968. return;
  17969. }
  17970. // If cached is not a promise (i.e., it's already an imageBitmap)
  17971. setTimeout( function () {
  17972. if ( onLoad ) onLoad( cached );
  17973. scope.manager.itemEnd( url );
  17974. }, 0 );
  17975. return cached;
  17976. }
  17977. const fetchOptions = {};
  17978. fetchOptions.credentials = ( this.crossOrigin === 'anonymous' ) ? 'same-origin' : 'include';
  17979. fetchOptions.headers = this.requestHeader;
  17980. const promise = fetch( url, fetchOptions ).then( function ( res ) {
  17981. return res.blob();
  17982. } ).then( function ( blob ) {
  17983. return createImageBitmap( blob, Object.assign( scope.options, { colorSpaceConversion: 'none' } ) );
  17984. } ).then( function ( imageBitmap ) {
  17985. Cache.add( url, imageBitmap );
  17986. if ( onLoad ) onLoad( imageBitmap );
  17987. scope.manager.itemEnd( url );
  17988. return imageBitmap;
  17989. } ).catch( function ( e ) {
  17990. if ( onError ) onError( e );
  17991. Cache.remove( url );
  17992. scope.manager.itemError( url );
  17993. scope.manager.itemEnd( url );
  17994. } );
  17995. Cache.add( url, promise );
  17996. scope.manager.itemStart( url );
  17997. }
  17998. }
  17999. let _context;
  18000. class AudioContext {
  18001. static getContext() {
  18002. if ( _context === undefined ) {
  18003. _context = new ( window.AudioContext || window.webkitAudioContext )();
  18004. }
  18005. return _context;
  18006. }
  18007. static setContext( value ) {
  18008. _context = value;
  18009. }
  18010. }
  18011. class AudioLoader extends Loader {
  18012. constructor( manager ) {
  18013. super( manager );
  18014. }
  18015. load( url, onLoad, onProgress, onError ) {
  18016. const scope = this;
  18017. const loader = new FileLoader( this.manager );
  18018. loader.setResponseType( 'arraybuffer' );
  18019. loader.setPath( this.path );
  18020. loader.setRequestHeader( this.requestHeader );
  18021. loader.setWithCredentials( this.withCredentials );
  18022. loader.load( url, function ( buffer ) {
  18023. try {
  18024. // Create a copy of the buffer. The `decodeAudioData` method
  18025. // detaches the buffer when complete, preventing reuse.
  18026. const bufferCopy = buffer.slice( 0 );
  18027. const context = AudioContext.getContext();
  18028. context.decodeAudioData( bufferCopy, function ( audioBuffer ) {
  18029. onLoad( audioBuffer );
  18030. } ).catch( handleError );
  18031. } catch ( e ) {
  18032. handleError( e );
  18033. }
  18034. }, onProgress, onError );
  18035. function handleError( e ) {
  18036. if ( onError ) {
  18037. onError( e );
  18038. } else {
  18039. console.error( e );
  18040. }
  18041. scope.manager.itemError( url );
  18042. }
  18043. }
  18044. }
  18045. const _eyeRight = /*@__PURE__*/ new Matrix4();
  18046. const _eyeLeft = /*@__PURE__*/ new Matrix4();
  18047. const _projectionMatrix = /*@__PURE__*/ new Matrix4();
  18048. class StereoCamera {
  18049. constructor() {
  18050. this.type = 'StereoCamera';
  18051. this.aspect = 1;
  18052. this.eyeSep = 0.064;
  18053. this.cameraL = new PerspectiveCamera();
  18054. this.cameraL.layers.enable( 1 );
  18055. this.cameraL.matrixAutoUpdate = false;
  18056. this.cameraR = new PerspectiveCamera();
  18057. this.cameraR.layers.enable( 2 );
  18058. this.cameraR.matrixAutoUpdate = false;
  18059. this._cache = {
  18060. focus: null,
  18061. fov: null,
  18062. aspect: null,
  18063. near: null,
  18064. far: null,
  18065. zoom: null,
  18066. eyeSep: null
  18067. };
  18068. }
  18069. update( camera ) {
  18070. const cache = this._cache;
  18071. const needsUpdate = cache.focus !== camera.focus || cache.fov !== camera.fov ||
  18072. cache.aspect !== camera.aspect * this.aspect || cache.near !== camera.near ||
  18073. cache.far !== camera.far || cache.zoom !== camera.zoom || cache.eyeSep !== this.eyeSep;
  18074. if ( needsUpdate ) {
  18075. cache.focus = camera.focus;
  18076. cache.fov = camera.fov;
  18077. cache.aspect = camera.aspect * this.aspect;
  18078. cache.near = camera.near;
  18079. cache.far = camera.far;
  18080. cache.zoom = camera.zoom;
  18081. cache.eyeSep = this.eyeSep;
  18082. // Off-axis stereoscopic effect based on
  18083. // http://paulbourke.net/stereographics/stereorender/
  18084. _projectionMatrix.copy( camera.projectionMatrix );
  18085. const eyeSepHalf = cache.eyeSep / 2;
  18086. const eyeSepOnProjection = eyeSepHalf * cache.near / cache.focus;
  18087. const ymax = ( cache.near * Math.tan( DEG2RAD * cache.fov * 0.5 ) ) / cache.zoom;
  18088. let xmin, xmax;
  18089. // translate xOffset
  18090. _eyeLeft.elements[ 12 ] = - eyeSepHalf;
  18091. _eyeRight.elements[ 12 ] = eyeSepHalf;
  18092. // for left eye
  18093. xmin = - ymax * cache.aspect + eyeSepOnProjection;
  18094. xmax = ymax * cache.aspect + eyeSepOnProjection;
  18095. _projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin );
  18096. _projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
  18097. this.cameraL.projectionMatrix.copy( _projectionMatrix );
  18098. // for right eye
  18099. xmin = - ymax * cache.aspect - eyeSepOnProjection;
  18100. xmax = ymax * cache.aspect - eyeSepOnProjection;
  18101. _projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin );
  18102. _projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
  18103. this.cameraR.projectionMatrix.copy( _projectionMatrix );
  18104. }
  18105. this.cameraL.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeLeft );
  18106. this.cameraR.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeRight );
  18107. }
  18108. }
  18109. class ArrayCamera extends PerspectiveCamera {
  18110. constructor( array = [] ) {
  18111. super();
  18112. this.isArrayCamera = true;
  18113. this.cameras = array;
  18114. }
  18115. }
  18116. class Clock {
  18117. constructor( autoStart = true ) {
  18118. this.autoStart = autoStart;
  18119. this.startTime = 0;
  18120. this.oldTime = 0;
  18121. this.elapsedTime = 0;
  18122. this.running = false;
  18123. }
  18124. start() {
  18125. this.startTime = now();
  18126. this.oldTime = this.startTime;
  18127. this.elapsedTime = 0;
  18128. this.running = true;
  18129. }
  18130. stop() {
  18131. this.getElapsedTime();
  18132. this.running = false;
  18133. this.autoStart = false;
  18134. }
  18135. getElapsedTime() {
  18136. this.getDelta();
  18137. return this.elapsedTime;
  18138. }
  18139. getDelta() {
  18140. let diff = 0;
  18141. if ( this.autoStart && ! this.running ) {
  18142. this.start();
  18143. return 0;
  18144. }
  18145. if ( this.running ) {
  18146. const newTime = now();
  18147. diff = ( newTime - this.oldTime ) / 1000;
  18148. this.oldTime = newTime;
  18149. this.elapsedTime += diff;
  18150. }
  18151. return diff;
  18152. }
  18153. }
  18154. function now() {
  18155. return performance.now();
  18156. }
  18157. const _position$1 = /*@__PURE__*/ new Vector3();
  18158. const _quaternion$1 = /*@__PURE__*/ new Quaternion();
  18159. const _scale$1 = /*@__PURE__*/ new Vector3();
  18160. const _orientation$1 = /*@__PURE__*/ new Vector3();
  18161. class AudioListener extends Object3D {
  18162. constructor() {
  18163. super();
  18164. this.type = 'AudioListener';
  18165. this.context = AudioContext.getContext();
  18166. this.gain = this.context.createGain();
  18167. this.gain.connect( this.context.destination );
  18168. this.filter = null;
  18169. this.timeDelta = 0;
  18170. // private
  18171. this._clock = new Clock();
  18172. }
  18173. getInput() {
  18174. return this.gain;
  18175. }
  18176. removeFilter() {
  18177. if ( this.filter !== null ) {
  18178. this.gain.disconnect( this.filter );
  18179. this.filter.disconnect( this.context.destination );
  18180. this.gain.connect( this.context.destination );
  18181. this.filter = null;
  18182. }
  18183. return this;
  18184. }
  18185. getFilter() {
  18186. return this.filter;
  18187. }
  18188. setFilter( value ) {
  18189. if ( this.filter !== null ) {
  18190. this.gain.disconnect( this.filter );
  18191. this.filter.disconnect( this.context.destination );
  18192. } else {
  18193. this.gain.disconnect( this.context.destination );
  18194. }
  18195. this.filter = value;
  18196. this.gain.connect( this.filter );
  18197. this.filter.connect( this.context.destination );
  18198. return this;
  18199. }
  18200. getMasterVolume() {
  18201. return this.gain.gain.value;
  18202. }
  18203. setMasterVolume( value ) {
  18204. this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
  18205. return this;
  18206. }
  18207. updateMatrixWorld( force ) {
  18208. super.updateMatrixWorld( force );
  18209. const listener = this.context.listener;
  18210. const up = this.up;
  18211. this.timeDelta = this._clock.getDelta();
  18212. this.matrixWorld.decompose( _position$1, _quaternion$1, _scale$1 );
  18213. _orientation$1.set( 0, 0, - 1 ).applyQuaternion( _quaternion$1 );
  18214. if ( listener.positionX ) {
  18215. // code path for Chrome (see #14393)
  18216. const endTime = this.context.currentTime + this.timeDelta;
  18217. listener.positionX.linearRampToValueAtTime( _position$1.x, endTime );
  18218. listener.positionY.linearRampToValueAtTime( _position$1.y, endTime );
  18219. listener.positionZ.linearRampToValueAtTime( _position$1.z, endTime );
  18220. listener.forwardX.linearRampToValueAtTime( _orientation$1.x, endTime );
  18221. listener.forwardY.linearRampToValueAtTime( _orientation$1.y, endTime );
  18222. listener.forwardZ.linearRampToValueAtTime( _orientation$1.z, endTime );
  18223. listener.upX.linearRampToValueAtTime( up.x, endTime );
  18224. listener.upY.linearRampToValueAtTime( up.y, endTime );
  18225. listener.upZ.linearRampToValueAtTime( up.z, endTime );
  18226. } else {
  18227. listener.setPosition( _position$1.x, _position$1.y, _position$1.z );
  18228. listener.setOrientation( _orientation$1.x, _orientation$1.y, _orientation$1.z, up.x, up.y, up.z );
  18229. }
  18230. }
  18231. }
  18232. class Audio extends Object3D {
  18233. constructor( listener ) {
  18234. super();
  18235. this.type = 'Audio';
  18236. this.listener = listener;
  18237. this.context = listener.context;
  18238. this.gain = this.context.createGain();
  18239. this.gain.connect( listener.getInput() );
  18240. this.autoplay = false;
  18241. this.buffer = null;
  18242. this.detune = 0;
  18243. this.loop = false;
  18244. this.loopStart = 0;
  18245. this.loopEnd = 0;
  18246. this.offset = 0;
  18247. this.duration = undefined;
  18248. this.playbackRate = 1;
  18249. this.isPlaying = false;
  18250. this.hasPlaybackControl = true;
  18251. this.source = null;
  18252. this.sourceType = 'empty';
  18253. this._startedAt = 0;
  18254. this._progress = 0;
  18255. this._connected = false;
  18256. this.filters = [];
  18257. }
  18258. getOutput() {
  18259. return this.gain;
  18260. }
  18261. setNodeSource( audioNode ) {
  18262. this.hasPlaybackControl = false;
  18263. this.sourceType = 'audioNode';
  18264. this.source = audioNode;
  18265. this.connect();
  18266. return this;
  18267. }
  18268. setMediaElementSource( mediaElement ) {
  18269. this.hasPlaybackControl = false;
  18270. this.sourceType = 'mediaNode';
  18271. this.source = this.context.createMediaElementSource( mediaElement );
  18272. this.connect();
  18273. return this;
  18274. }
  18275. setMediaStreamSource( mediaStream ) {
  18276. this.hasPlaybackControl = false;
  18277. this.sourceType = 'mediaStreamNode';
  18278. this.source = this.context.createMediaStreamSource( mediaStream );
  18279. this.connect();
  18280. return this;
  18281. }
  18282. setBuffer( audioBuffer ) {
  18283. this.buffer = audioBuffer;
  18284. this.sourceType = 'buffer';
  18285. if ( this.autoplay ) this.play();
  18286. return this;
  18287. }
  18288. play( delay = 0 ) {
  18289. if ( this.isPlaying === true ) {
  18290. console.warn( 'THREE.Audio: Audio is already playing.' );
  18291. return;
  18292. }
  18293. if ( this.hasPlaybackControl === false ) {
  18294. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18295. return;
  18296. }
  18297. this._startedAt = this.context.currentTime + delay;
  18298. const source = this.context.createBufferSource();
  18299. source.buffer = this.buffer;
  18300. source.loop = this.loop;
  18301. source.loopStart = this.loopStart;
  18302. source.loopEnd = this.loopEnd;
  18303. source.onended = this.onEnded.bind( this );
  18304. source.start( this._startedAt, this._progress + this.offset, this.duration );
  18305. this.isPlaying = true;
  18306. this.source = source;
  18307. this.setDetune( this.detune );
  18308. this.setPlaybackRate( this.playbackRate );
  18309. return this.connect();
  18310. }
  18311. pause() {
  18312. if ( this.hasPlaybackControl === false ) {
  18313. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18314. return;
  18315. }
  18316. if ( this.isPlaying === true ) {
  18317. // update current progress
  18318. this._progress += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate;
  18319. if ( this.loop === true ) {
  18320. // ensure _progress does not exceed duration with looped audios
  18321. this._progress = this._progress % ( this.duration || this.buffer.duration );
  18322. }
  18323. this.source.stop();
  18324. this.source.onended = null;
  18325. this.isPlaying = false;
  18326. }
  18327. return this;
  18328. }
  18329. stop( delay = 0 ) {
  18330. if ( this.hasPlaybackControl === false ) {
  18331. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18332. return;
  18333. }
  18334. this._progress = 0;
  18335. if ( this.source !== null ) {
  18336. this.source.stop( this.context.currentTime + delay );
  18337. this.source.onended = null;
  18338. }
  18339. this.isPlaying = false;
  18340. return this;
  18341. }
  18342. connect() {
  18343. if ( this.filters.length > 0 ) {
  18344. this.source.connect( this.filters[ 0 ] );
  18345. for ( let i = 1, l = this.filters.length; i < l; i ++ ) {
  18346. this.filters[ i - 1 ].connect( this.filters[ i ] );
  18347. }
  18348. this.filters[ this.filters.length - 1 ].connect( this.getOutput() );
  18349. } else {
  18350. this.source.connect( this.getOutput() );
  18351. }
  18352. this._connected = true;
  18353. return this;
  18354. }
  18355. disconnect() {
  18356. if ( this._connected === false ) {
  18357. return;
  18358. }
  18359. if ( this.filters.length > 0 ) {
  18360. this.source.disconnect( this.filters[ 0 ] );
  18361. for ( let i = 1, l = this.filters.length; i < l; i ++ ) {
  18362. this.filters[ i - 1 ].disconnect( this.filters[ i ] );
  18363. }
  18364. this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() );
  18365. } else {
  18366. this.source.disconnect( this.getOutput() );
  18367. }
  18368. this._connected = false;
  18369. return this;
  18370. }
  18371. getFilters() {
  18372. return this.filters;
  18373. }
  18374. setFilters( value ) {
  18375. if ( ! value ) value = [];
  18376. if ( this._connected === true ) {
  18377. this.disconnect();
  18378. this.filters = value.slice();
  18379. this.connect();
  18380. } else {
  18381. this.filters = value.slice();
  18382. }
  18383. return this;
  18384. }
  18385. setDetune( value ) {
  18386. this.detune = value;
  18387. if ( this.isPlaying === true && this.source.detune !== undefined ) {
  18388. this.source.detune.setTargetAtTime( this.detune, this.context.currentTime, 0.01 );
  18389. }
  18390. return this;
  18391. }
  18392. getDetune() {
  18393. return this.detune;
  18394. }
  18395. getFilter() {
  18396. return this.getFilters()[ 0 ];
  18397. }
  18398. setFilter( filter ) {
  18399. return this.setFilters( filter ? [ filter ] : [] );
  18400. }
  18401. setPlaybackRate( value ) {
  18402. if ( this.hasPlaybackControl === false ) {
  18403. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18404. return;
  18405. }
  18406. this.playbackRate = value;
  18407. if ( this.isPlaying === true ) {
  18408. this.source.playbackRate.setTargetAtTime( this.playbackRate, this.context.currentTime, 0.01 );
  18409. }
  18410. return this;
  18411. }
  18412. getPlaybackRate() {
  18413. return this.playbackRate;
  18414. }
  18415. onEnded() {
  18416. this.isPlaying = false;
  18417. this._progress = 0;
  18418. }
  18419. getLoop() {
  18420. if ( this.hasPlaybackControl === false ) {
  18421. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18422. return false;
  18423. }
  18424. return this.loop;
  18425. }
  18426. setLoop( value ) {
  18427. if ( this.hasPlaybackControl === false ) {
  18428. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18429. return;
  18430. }
  18431. this.loop = value;
  18432. if ( this.isPlaying === true ) {
  18433. this.source.loop = this.loop;
  18434. }
  18435. return this;
  18436. }
  18437. setLoopStart( value ) {
  18438. this.loopStart = value;
  18439. return this;
  18440. }
  18441. setLoopEnd( value ) {
  18442. this.loopEnd = value;
  18443. return this;
  18444. }
  18445. getVolume() {
  18446. return this.gain.gain.value;
  18447. }
  18448. setVolume( value ) {
  18449. this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
  18450. return this;
  18451. }
  18452. copy( source, recursive ) {
  18453. super.copy( source, recursive );
  18454. if ( source.sourceType !== 'buffer' ) {
  18455. console.warn( 'THREE.Audio: Audio source type cannot be copied.' );
  18456. return this;
  18457. }
  18458. this.autoplay = source.autoplay;
  18459. this.buffer = source.buffer;
  18460. this.detune = source.detune;
  18461. this.loop = source.loop;
  18462. this.loopStart = source.loopStart;
  18463. this.loopEnd = source.loopEnd;
  18464. this.offset = source.offset;
  18465. this.duration = source.duration;
  18466. this.playbackRate = source.playbackRate;
  18467. this.hasPlaybackControl = source.hasPlaybackControl;
  18468. this.sourceType = source.sourceType;
  18469. this.filters = source.filters.slice();
  18470. return this;
  18471. }
  18472. clone( recursive ) {
  18473. return new this.constructor( this.listener ).copy( this, recursive );
  18474. }
  18475. }
  18476. const _position = /*@__PURE__*/ new Vector3();
  18477. const _quaternion = /*@__PURE__*/ new Quaternion();
  18478. const _scale = /*@__PURE__*/ new Vector3();
  18479. const _orientation = /*@__PURE__*/ new Vector3();
  18480. class PositionalAudio extends Audio {
  18481. constructor( listener ) {
  18482. super( listener );
  18483. this.panner = this.context.createPanner();
  18484. this.panner.panningModel = 'HRTF';
  18485. this.panner.connect( this.gain );
  18486. }
  18487. connect() {
  18488. super.connect();
  18489. this.panner.connect( this.gain );
  18490. }
  18491. disconnect() {
  18492. super.disconnect();
  18493. this.panner.disconnect( this.gain );
  18494. }
  18495. getOutput() {
  18496. return this.panner;
  18497. }
  18498. getRefDistance() {
  18499. return this.panner.refDistance;
  18500. }
  18501. setRefDistance( value ) {
  18502. this.panner.refDistance = value;
  18503. return this;
  18504. }
  18505. getRolloffFactor() {
  18506. return this.panner.rolloffFactor;
  18507. }
  18508. setRolloffFactor( value ) {
  18509. this.panner.rolloffFactor = value;
  18510. return this;
  18511. }
  18512. getDistanceModel() {
  18513. return this.panner.distanceModel;
  18514. }
  18515. setDistanceModel( value ) {
  18516. this.panner.distanceModel = value;
  18517. return this;
  18518. }
  18519. getMaxDistance() {
  18520. return this.panner.maxDistance;
  18521. }
  18522. setMaxDistance( value ) {
  18523. this.panner.maxDistance = value;
  18524. return this;
  18525. }
  18526. setDirectionalCone( coneInnerAngle, coneOuterAngle, coneOuterGain ) {
  18527. this.panner.coneInnerAngle = coneInnerAngle;
  18528. this.panner.coneOuterAngle = coneOuterAngle;
  18529. this.panner.coneOuterGain = coneOuterGain;
  18530. return this;
  18531. }
  18532. updateMatrixWorld( force ) {
  18533. super.updateMatrixWorld( force );
  18534. if ( this.hasPlaybackControl === true && this.isPlaying === false ) return;
  18535. this.matrixWorld.decompose( _position, _quaternion, _scale );
  18536. _orientation.set( 0, 0, 1 ).applyQuaternion( _quaternion );
  18537. const panner = this.panner;
  18538. if ( panner.positionX ) {
  18539. // code path for Chrome and Firefox (see #14393)
  18540. const endTime = this.context.currentTime + this.listener.timeDelta;
  18541. panner.positionX.linearRampToValueAtTime( _position.x, endTime );
  18542. panner.positionY.linearRampToValueAtTime( _position.y, endTime );
  18543. panner.positionZ.linearRampToValueAtTime( _position.z, endTime );
  18544. panner.orientationX.linearRampToValueAtTime( _orientation.x, endTime );
  18545. panner.orientationY.linearRampToValueAtTime( _orientation.y, endTime );
  18546. panner.orientationZ.linearRampToValueAtTime( _orientation.z, endTime );
  18547. } else {
  18548. panner.setPosition( _position.x, _position.y, _position.z );
  18549. panner.setOrientation( _orientation.x, _orientation.y, _orientation.z );
  18550. }
  18551. }
  18552. }
  18553. class AudioAnalyser {
  18554. constructor( audio, fftSize = 2048 ) {
  18555. this.analyser = audio.context.createAnalyser();
  18556. this.analyser.fftSize = fftSize;
  18557. this.data = new Uint8Array( this.analyser.frequencyBinCount );
  18558. audio.getOutput().connect( this.analyser );
  18559. }
  18560. getFrequencyData() {
  18561. this.analyser.getByteFrequencyData( this.data );
  18562. return this.data;
  18563. }
  18564. getAverageFrequency() {
  18565. let value = 0;
  18566. const data = this.getFrequencyData();
  18567. for ( let i = 0; i < data.length; i ++ ) {
  18568. value += data[ i ];
  18569. }
  18570. return value / data.length;
  18571. }
  18572. }
  18573. class PropertyMixer {
  18574. constructor( binding, typeName, valueSize ) {
  18575. this.binding = binding;
  18576. this.valueSize = valueSize;
  18577. let mixFunction,
  18578. mixFunctionAdditive,
  18579. setIdentity;
  18580. // buffer layout: [ incoming | accu0 | accu1 | orig | addAccu | (optional work) ]
  18581. //
  18582. // interpolators can use .buffer as their .result
  18583. // the data then goes to 'incoming'
  18584. //
  18585. // 'accu0' and 'accu1' are used frame-interleaved for
  18586. // the cumulative result and are compared to detect
  18587. // changes
  18588. //
  18589. // 'orig' stores the original state of the property
  18590. //
  18591. // 'add' is used for additive cumulative results
  18592. //
  18593. // 'work' is optional and is only present for quaternion types. It is used
  18594. // to store intermediate quaternion multiplication results
  18595. switch ( typeName ) {
  18596. case 'quaternion':
  18597. mixFunction = this._slerp;
  18598. mixFunctionAdditive = this._slerpAdditive;
  18599. setIdentity = this._setAdditiveIdentityQuaternion;
  18600. this.buffer = new Float64Array( valueSize * 6 );
  18601. this._workIndex = 5;
  18602. break;
  18603. case 'string':
  18604. case 'bool':
  18605. mixFunction = this._select;
  18606. // Use the regular mix function and for additive on these types,
  18607. // additive is not relevant for non-numeric types
  18608. mixFunctionAdditive = this._select;
  18609. setIdentity = this._setAdditiveIdentityOther;
  18610. this.buffer = new Array( valueSize * 5 );
  18611. break;
  18612. default:
  18613. mixFunction = this._lerp;
  18614. mixFunctionAdditive = this._lerpAdditive;
  18615. setIdentity = this._setAdditiveIdentityNumeric;
  18616. this.buffer = new Float64Array( valueSize * 5 );
  18617. }
  18618. this._mixBufferRegion = mixFunction;
  18619. this._mixBufferRegionAdditive = mixFunctionAdditive;
  18620. this._setIdentity = setIdentity;
  18621. this._origIndex = 3;
  18622. this._addIndex = 4;
  18623. this.cumulativeWeight = 0;
  18624. this.cumulativeWeightAdditive = 0;
  18625. this.useCount = 0;
  18626. this.referenceCount = 0;
  18627. }
  18628. // accumulate data in the 'incoming' region into 'accu<i>'
  18629. accumulate( accuIndex, weight ) {
  18630. // note: happily accumulating nothing when weight = 0, the caller knows
  18631. // the weight and shouldn't have made the call in the first place
  18632. const buffer = this.buffer,
  18633. stride = this.valueSize,
  18634. offset = accuIndex * stride + stride;
  18635. let currentWeight = this.cumulativeWeight;
  18636. if ( currentWeight === 0 ) {
  18637. // accuN := incoming * weight
  18638. for ( let i = 0; i !== stride; ++ i ) {
  18639. buffer[ offset + i ] = buffer[ i ];
  18640. }
  18641. currentWeight = weight;
  18642. } else {
  18643. // accuN := accuN + incoming * weight
  18644. currentWeight += weight;
  18645. const mix = weight / currentWeight;
  18646. this._mixBufferRegion( buffer, offset, 0, mix, stride );
  18647. }
  18648. this.cumulativeWeight = currentWeight;
  18649. }
  18650. // accumulate data in the 'incoming' region into 'add'
  18651. accumulateAdditive( weight ) {
  18652. const buffer = this.buffer,
  18653. stride = this.valueSize,
  18654. offset = stride * this._addIndex;
  18655. if ( this.cumulativeWeightAdditive === 0 ) {
  18656. // add = identity
  18657. this._setIdentity();
  18658. }
  18659. // add := add + incoming * weight
  18660. this._mixBufferRegionAdditive( buffer, offset, 0, weight, stride );
  18661. this.cumulativeWeightAdditive += weight;
  18662. }
  18663. // apply the state of 'accu<i>' to the binding when accus differ
  18664. apply( accuIndex ) {
  18665. const stride = this.valueSize,
  18666. buffer = this.buffer,
  18667. offset = accuIndex * stride + stride,
  18668. weight = this.cumulativeWeight,
  18669. weightAdditive = this.cumulativeWeightAdditive,
  18670. binding = this.binding;
  18671. this.cumulativeWeight = 0;
  18672. this.cumulativeWeightAdditive = 0;
  18673. if ( weight < 1 ) {
  18674. // accuN := accuN + original * ( 1 - cumulativeWeight )
  18675. const originalValueOffset = stride * this._origIndex;
  18676. this._mixBufferRegion(
  18677. buffer, offset, originalValueOffset, 1 - weight, stride );
  18678. }
  18679. if ( weightAdditive > 0 ) {
  18680. // accuN := accuN + additive accuN
  18681. this._mixBufferRegionAdditive( buffer, offset, this._addIndex * stride, 1, stride );
  18682. }
  18683. for ( let i = stride, e = stride + stride; i !== e; ++ i ) {
  18684. if ( buffer[ i ] !== buffer[ i + stride ] ) {
  18685. // value has changed -> update scene graph
  18686. binding.setValue( buffer, offset );
  18687. break;
  18688. }
  18689. }
  18690. }
  18691. // remember the state of the bound property and copy it to both accus
  18692. saveOriginalState() {
  18693. const binding = this.binding;
  18694. const buffer = this.buffer,
  18695. stride = this.valueSize,
  18696. originalValueOffset = stride * this._origIndex;
  18697. binding.getValue( buffer, originalValueOffset );
  18698. // accu[0..1] := orig -- initially detect changes against the original
  18699. for ( let i = stride, e = originalValueOffset; i !== e; ++ i ) {
  18700. buffer[ i ] = buffer[ originalValueOffset + ( i % stride ) ];
  18701. }
  18702. // Add to identity for additive
  18703. this._setIdentity();
  18704. this.cumulativeWeight = 0;
  18705. this.cumulativeWeightAdditive = 0;
  18706. }
  18707. // apply the state previously taken via 'saveOriginalState' to the binding
  18708. restoreOriginalState() {
  18709. const originalValueOffset = this.valueSize * 3;
  18710. this.binding.setValue( this.buffer, originalValueOffset );
  18711. }
  18712. _setAdditiveIdentityNumeric() {
  18713. const startIndex = this._addIndex * this.valueSize;
  18714. const endIndex = startIndex + this.valueSize;
  18715. for ( let i = startIndex; i < endIndex; i ++ ) {
  18716. this.buffer[ i ] = 0;
  18717. }
  18718. }
  18719. _setAdditiveIdentityQuaternion() {
  18720. this._setAdditiveIdentityNumeric();
  18721. this.buffer[ this._addIndex * this.valueSize + 3 ] = 1;
  18722. }
  18723. _setAdditiveIdentityOther() {
  18724. const startIndex = this._origIndex * this.valueSize;
  18725. const targetIndex = this._addIndex * this.valueSize;
  18726. for ( let i = 0; i < this.valueSize; i ++ ) {
  18727. this.buffer[ targetIndex + i ] = this.buffer[ startIndex + i ];
  18728. }
  18729. }
  18730. // mix functions
  18731. _select( buffer, dstOffset, srcOffset, t, stride ) {
  18732. if ( t >= 0.5 ) {
  18733. for ( let i = 0; i !== stride; ++ i ) {
  18734. buffer[ dstOffset + i ] = buffer[ srcOffset + i ];
  18735. }
  18736. }
  18737. }
  18738. _slerp( buffer, dstOffset, srcOffset, t ) {
  18739. Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, srcOffset, t );
  18740. }
  18741. _slerpAdditive( buffer, dstOffset, srcOffset, t, stride ) {
  18742. const workOffset = this._workIndex * stride;
  18743. // Store result in intermediate buffer offset
  18744. Quaternion.multiplyQuaternionsFlat( buffer, workOffset, buffer, dstOffset, buffer, srcOffset );
  18745. // Slerp to the intermediate result
  18746. Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, workOffset, t );
  18747. }
  18748. _lerp( buffer, dstOffset, srcOffset, t, stride ) {
  18749. const s = 1 - t;
  18750. for ( let i = 0; i !== stride; ++ i ) {
  18751. const j = dstOffset + i;
  18752. buffer[ j ] = buffer[ j ] * s + buffer[ srcOffset + i ] * t;
  18753. }
  18754. }
  18755. _lerpAdditive( buffer, dstOffset, srcOffset, t, stride ) {
  18756. for ( let i = 0; i !== stride; ++ i ) {
  18757. const j = dstOffset + i;
  18758. buffer[ j ] = buffer[ j ] + buffer[ srcOffset + i ] * t;
  18759. }
  18760. }
  18761. }
  18762. // Characters [].:/ are reserved for track binding syntax.
  18763. const _RESERVED_CHARS_RE = '\\[\\]\\.:\\/';
  18764. const _reservedRe = new RegExp( '[' + _RESERVED_CHARS_RE + ']', 'g' );
  18765. // Attempts to allow node names from any language. ES5's `\w` regexp matches
  18766. // only latin characters, and the unicode \p{L} is not yet supported. So
  18767. // instead, we exclude reserved characters and match everything else.
  18768. const _wordChar = '[^' + _RESERVED_CHARS_RE + ']';
  18769. const _wordCharOrDot = '[^' + _RESERVED_CHARS_RE.replace( '\\.', '' ) + ']';
  18770. // Parent directories, delimited by '/' or ':'. Currently unused, but must
  18771. // be matched to parse the rest of the track name.
  18772. const _directoryRe = /*@__PURE__*/ /((?:WC+[\/:])*)/.source.replace( 'WC', _wordChar );
  18773. // Target node. May contain word characters (a-zA-Z0-9_) and '.' or '-'.
  18774. const _nodeRe = /*@__PURE__*/ /(WCOD+)?/.source.replace( 'WCOD', _wordCharOrDot );
  18775. // Object on target node, and accessor. May not contain reserved
  18776. // characters. Accessor may contain any character except closing bracket.
  18777. const _objectRe = /*@__PURE__*/ /(?:\.(WC+)(?:\[(.+)\])?)?/.source.replace( 'WC', _wordChar );
  18778. // Property and accessor. May not contain reserved characters. Accessor may
  18779. // contain any non-bracket characters.
  18780. const _propertyRe = /*@__PURE__*/ /\.(WC+)(?:\[(.+)\])?/.source.replace( 'WC', _wordChar );
  18781. const _trackRe = new RegExp( ''
  18782. + '^'
  18783. + _directoryRe
  18784. + _nodeRe
  18785. + _objectRe
  18786. + _propertyRe
  18787. + '$'
  18788. );
  18789. const _supportedObjectNames = [ 'material', 'materials', 'bones', 'map' ];
  18790. class Composite {
  18791. constructor( targetGroup, path, optionalParsedPath ) {
  18792. const parsedPath = optionalParsedPath || PropertyBinding.parseTrackName( path );
  18793. this._targetGroup = targetGroup;
  18794. this._bindings = targetGroup.subscribe_( path, parsedPath );
  18795. }
  18796. getValue( array, offset ) {
  18797. this.bind(); // bind all binding
  18798. const firstValidIndex = this._targetGroup.nCachedObjects_,
  18799. binding = this._bindings[ firstValidIndex ];
  18800. // and only call .getValue on the first
  18801. if ( binding !== undefined ) binding.getValue( array, offset );
  18802. }
  18803. setValue( array, offset ) {
  18804. const bindings = this._bindings;
  18805. for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
  18806. bindings[ i ].setValue( array, offset );
  18807. }
  18808. }
  18809. bind() {
  18810. const bindings = this._bindings;
  18811. for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
  18812. bindings[ i ].bind();
  18813. }
  18814. }
  18815. unbind() {
  18816. const bindings = this._bindings;
  18817. for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
  18818. bindings[ i ].unbind();
  18819. }
  18820. }
  18821. }
  18822. // Note: This class uses a State pattern on a per-method basis:
  18823. // 'bind' sets 'this.getValue' / 'setValue' and shadows the
  18824. // prototype version of these methods with one that represents
  18825. // the bound state. When the property is not found, the methods
  18826. // become no-ops.
  18827. class PropertyBinding {
  18828. constructor( rootNode, path, parsedPath ) {
  18829. this.path = path;
  18830. this.parsedPath = parsedPath || PropertyBinding.parseTrackName( path );
  18831. this.node = PropertyBinding.findNode( rootNode, this.parsedPath.nodeName );
  18832. this.rootNode = rootNode;
  18833. // initial state of these methods that calls 'bind'
  18834. this.getValue = this._getValue_unbound;
  18835. this.setValue = this._setValue_unbound;
  18836. }
  18837. static create( root, path, parsedPath ) {
  18838. if ( ! ( root && root.isAnimationObjectGroup ) ) {
  18839. return new PropertyBinding( root, path, parsedPath );
  18840. } else {
  18841. return new PropertyBinding.Composite( root, path, parsedPath );
  18842. }
  18843. }
  18844. /**
  18845. * Replaces spaces with underscores and removes unsupported characters from
  18846. * node names, to ensure compatibility with parseTrackName().
  18847. *
  18848. * @param {string} name Node name to be sanitized.
  18849. * @return {string}
  18850. */
  18851. static sanitizeNodeName( name ) {
  18852. return name.replace( /\s/g, '_' ).replace( _reservedRe, '' );
  18853. }
  18854. static parseTrackName( trackName ) {
  18855. const matches = _trackRe.exec( trackName );
  18856. if ( matches === null ) {
  18857. throw new Error( 'PropertyBinding: Cannot parse trackName: ' + trackName );
  18858. }
  18859. const results = {
  18860. // directoryName: matches[ 1 ], // (tschw) currently unused
  18861. nodeName: matches[ 2 ],
  18862. objectName: matches[ 3 ],
  18863. objectIndex: matches[ 4 ],
  18864. propertyName: matches[ 5 ], // required
  18865. propertyIndex: matches[ 6 ]
  18866. };
  18867. const lastDot = results.nodeName && results.nodeName.lastIndexOf( '.' );
  18868. if ( lastDot !== undefined && lastDot !== - 1 ) {
  18869. const objectName = results.nodeName.substring( lastDot + 1 );
  18870. // Object names must be checked against an allowlist. Otherwise, there
  18871. // is no way to parse 'foo.bar.baz': 'baz' must be a property, but
  18872. // 'bar' could be the objectName, or part of a nodeName (which can
  18873. // include '.' characters).
  18874. if ( _supportedObjectNames.indexOf( objectName ) !== - 1 ) {
  18875. results.nodeName = results.nodeName.substring( 0, lastDot );
  18876. results.objectName = objectName;
  18877. }
  18878. }
  18879. if ( results.propertyName === null || results.propertyName.length === 0 ) {
  18880. throw new Error( 'PropertyBinding: can not parse propertyName from trackName: ' + trackName );
  18881. }
  18882. return results;
  18883. }
  18884. static findNode( root, nodeName ) {
  18885. if ( nodeName === undefined || nodeName === '' || nodeName === '.' || nodeName === - 1 || nodeName === root.name || nodeName === root.uuid ) {
  18886. return root;
  18887. }
  18888. // search into skeleton bones.
  18889. if ( root.skeleton ) {
  18890. const bone = root.skeleton.getBoneByName( nodeName );
  18891. if ( bone !== undefined ) {
  18892. return bone;
  18893. }
  18894. }
  18895. // search into node subtree.
  18896. if ( root.children ) {
  18897. const searchNodeSubtree = function ( children ) {
  18898. for ( let i = 0; i < children.length; i ++ ) {
  18899. const childNode = children[ i ];
  18900. if ( childNode.name === nodeName || childNode.uuid === nodeName ) {
  18901. return childNode;
  18902. }
  18903. const result = searchNodeSubtree( childNode.children );
  18904. if ( result ) return result;
  18905. }
  18906. return null;
  18907. };
  18908. const subTreeNode = searchNodeSubtree( root.children );
  18909. if ( subTreeNode ) {
  18910. return subTreeNode;
  18911. }
  18912. }
  18913. return null;
  18914. }
  18915. // these are used to "bind" a nonexistent property
  18916. _getValue_unavailable() {}
  18917. _setValue_unavailable() {}
  18918. // Getters
  18919. _getValue_direct( buffer, offset ) {
  18920. buffer[ offset ] = this.targetObject[ this.propertyName ];
  18921. }
  18922. _getValue_array( buffer, offset ) {
  18923. const source = this.resolvedProperty;
  18924. for ( let i = 0, n = source.length; i !== n; ++ i ) {
  18925. buffer[ offset ++ ] = source[ i ];
  18926. }
  18927. }
  18928. _getValue_arrayElement( buffer, offset ) {
  18929. buffer[ offset ] = this.resolvedProperty[ this.propertyIndex ];
  18930. }
  18931. _getValue_toArray( buffer, offset ) {
  18932. this.resolvedProperty.toArray( buffer, offset );
  18933. }
  18934. // Direct
  18935. _setValue_direct( buffer, offset ) {
  18936. this.targetObject[ this.propertyName ] = buffer[ offset ];
  18937. }
  18938. _setValue_direct_setNeedsUpdate( buffer, offset ) {
  18939. this.targetObject[ this.propertyName ] = buffer[ offset ];
  18940. this.targetObject.needsUpdate = true;
  18941. }
  18942. _setValue_direct_setMatrixWorldNeedsUpdate( buffer, offset ) {
  18943. this.targetObject[ this.propertyName ] = buffer[ offset ];
  18944. this.targetObject.matrixWorldNeedsUpdate = true;
  18945. }
  18946. // EntireArray
  18947. _setValue_array( buffer, offset ) {
  18948. const dest = this.resolvedProperty;
  18949. for ( let i = 0, n = dest.length; i !== n; ++ i ) {
  18950. dest[ i ] = buffer[ offset ++ ];
  18951. }
  18952. }
  18953. _setValue_array_setNeedsUpdate( buffer, offset ) {
  18954. const dest = this.resolvedProperty;
  18955. for ( let i = 0, n = dest.length; i !== n; ++ i ) {
  18956. dest[ i ] = buffer[ offset ++ ];
  18957. }
  18958. this.targetObject.needsUpdate = true;
  18959. }
  18960. _setValue_array_setMatrixWorldNeedsUpdate( buffer, offset ) {
  18961. const dest = this.resolvedProperty;
  18962. for ( let i = 0, n = dest.length; i !== n; ++ i ) {
  18963. dest[ i ] = buffer[ offset ++ ];
  18964. }
  18965. this.targetObject.matrixWorldNeedsUpdate = true;
  18966. }
  18967. // ArrayElement
  18968. _setValue_arrayElement( buffer, offset ) {
  18969. this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
  18970. }
  18971. _setValue_arrayElement_setNeedsUpdate( buffer, offset ) {
  18972. this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
  18973. this.targetObject.needsUpdate = true;
  18974. }
  18975. _setValue_arrayElement_setMatrixWorldNeedsUpdate( buffer, offset ) {
  18976. this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
  18977. this.targetObject.matrixWorldNeedsUpdate = true;
  18978. }
  18979. // HasToFromArray
  18980. _setValue_fromArray( buffer, offset ) {
  18981. this.resolvedProperty.fromArray( buffer, offset );
  18982. }
  18983. _setValue_fromArray_setNeedsUpdate( buffer, offset ) {
  18984. this.resolvedProperty.fromArray( buffer, offset );
  18985. this.targetObject.needsUpdate = true;
  18986. }
  18987. _setValue_fromArray_setMatrixWorldNeedsUpdate( buffer, offset ) {
  18988. this.resolvedProperty.fromArray( buffer, offset );
  18989. this.targetObject.matrixWorldNeedsUpdate = true;
  18990. }
  18991. _getValue_unbound( targetArray, offset ) {
  18992. this.bind();
  18993. this.getValue( targetArray, offset );
  18994. }
  18995. _setValue_unbound( sourceArray, offset ) {
  18996. this.bind();
  18997. this.setValue( sourceArray, offset );
  18998. }
  18999. // create getter / setter pair for a property in the scene graph
  19000. bind() {
  19001. let targetObject = this.node;
  19002. const parsedPath = this.parsedPath;
  19003. const objectName = parsedPath.objectName;
  19004. const propertyName = parsedPath.propertyName;
  19005. let propertyIndex = parsedPath.propertyIndex;
  19006. if ( ! targetObject ) {
  19007. targetObject = PropertyBinding.findNode( this.rootNode, parsedPath.nodeName );
  19008. this.node = targetObject;
  19009. }
  19010. // set fail state so we can just 'return' on error
  19011. this.getValue = this._getValue_unavailable;
  19012. this.setValue = this._setValue_unavailable;
  19013. // ensure there is a value node
  19014. if ( ! targetObject ) {
  19015. console.warn( 'THREE.PropertyBinding: No target node found for track: ' + this.path + '.' );
  19016. return;
  19017. }
  19018. if ( objectName ) {
  19019. let objectIndex = parsedPath.objectIndex;
  19020. // special cases were we need to reach deeper into the hierarchy to get the face materials....
  19021. switch ( objectName ) {
  19022. case 'materials':
  19023. if ( ! targetObject.material ) {
  19024. console.error( 'THREE.PropertyBinding: Can not bind to material as node does not have a material.', this );
  19025. return;
  19026. }
  19027. if ( ! targetObject.material.materials ) {
  19028. console.error( 'THREE.PropertyBinding: Can not bind to material.materials as node.material does not have a materials array.', this );
  19029. return;
  19030. }
  19031. targetObject = targetObject.material.materials;
  19032. break;
  19033. case 'bones':
  19034. if ( ! targetObject.skeleton ) {
  19035. console.error( 'THREE.PropertyBinding: Can not bind to bones as node does not have a skeleton.', this );
  19036. return;
  19037. }
  19038. // potential future optimization: skip this if propertyIndex is already an integer
  19039. // and convert the integer string to a true integer.
  19040. targetObject = targetObject.skeleton.bones;
  19041. // support resolving morphTarget names into indices.
  19042. for ( let i = 0; i < targetObject.length; i ++ ) {
  19043. if ( targetObject[ i ].name === objectIndex ) {
  19044. objectIndex = i;
  19045. break;
  19046. }
  19047. }
  19048. break;
  19049. case 'map':
  19050. if ( 'map' in targetObject ) {
  19051. targetObject = targetObject.map;
  19052. break;
  19053. }
  19054. if ( ! targetObject.material ) {
  19055. console.error( 'THREE.PropertyBinding: Can not bind to material as node does not have a material.', this );
  19056. return;
  19057. }
  19058. if ( ! targetObject.material.map ) {
  19059. console.error( 'THREE.PropertyBinding: Can not bind to material.map as node.material does not have a map.', this );
  19060. return;
  19061. }
  19062. targetObject = targetObject.material.map;
  19063. break;
  19064. default:
  19065. if ( targetObject[ objectName ] === undefined ) {
  19066. console.error( 'THREE.PropertyBinding: Can not bind to objectName of node undefined.', this );
  19067. return;
  19068. }
  19069. targetObject = targetObject[ objectName ];
  19070. }
  19071. if ( objectIndex !== undefined ) {
  19072. if ( targetObject[ objectIndex ] === undefined ) {
  19073. console.error( 'THREE.PropertyBinding: Trying to bind to objectIndex of objectName, but is undefined.', this, targetObject );
  19074. return;
  19075. }
  19076. targetObject = targetObject[ objectIndex ];
  19077. }
  19078. }
  19079. // resolve property
  19080. const nodeProperty = targetObject[ propertyName ];
  19081. if ( nodeProperty === undefined ) {
  19082. const nodeName = parsedPath.nodeName;
  19083. console.error( 'THREE.PropertyBinding: Trying to update property for track: ' + nodeName +
  19084. '.' + propertyName + ' but it wasn\'t found.', targetObject );
  19085. return;
  19086. }
  19087. // determine versioning scheme
  19088. let versioning = this.Versioning.None;
  19089. this.targetObject = targetObject;
  19090. if ( targetObject.isMaterial === true ) {
  19091. versioning = this.Versioning.NeedsUpdate;
  19092. } else if ( targetObject.isObject3D === true ) {
  19093. versioning = this.Versioning.MatrixWorldNeedsUpdate;
  19094. }
  19095. // determine how the property gets bound
  19096. let bindingType = this.BindingType.Direct;
  19097. if ( propertyIndex !== undefined ) {
  19098. // access a sub element of the property array (only primitives are supported right now)
  19099. if ( propertyName === 'morphTargetInfluences' ) {
  19100. // potential optimization, skip this if propertyIndex is already an integer, and convert the integer string to a true integer.
  19101. // support resolving morphTarget names into indices.
  19102. if ( ! targetObject.geometry ) {
  19103. console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.', this );
  19104. return;
  19105. }
  19106. if ( ! targetObject.geometry.morphAttributes ) {
  19107. console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphAttributes.', this );
  19108. return;
  19109. }
  19110. if ( targetObject.morphTargetDictionary[ propertyIndex ] !== undefined ) {
  19111. propertyIndex = targetObject.morphTargetDictionary[ propertyIndex ];
  19112. }
  19113. }
  19114. bindingType = this.BindingType.ArrayElement;
  19115. this.resolvedProperty = nodeProperty;
  19116. this.propertyIndex = propertyIndex;
  19117. } else if ( nodeProperty.fromArray !== undefined && nodeProperty.toArray !== undefined ) {
  19118. // must use copy for Object3D.Euler/Quaternion
  19119. bindingType = this.BindingType.HasFromToArray;
  19120. this.resolvedProperty = nodeProperty;
  19121. } else if ( Array.isArray( nodeProperty ) ) {
  19122. bindingType = this.BindingType.EntireArray;
  19123. this.resolvedProperty = nodeProperty;
  19124. } else {
  19125. this.propertyName = propertyName;
  19126. }
  19127. // select getter / setter
  19128. this.getValue = this.GetterByBindingType[ bindingType ];
  19129. this.setValue = this.SetterByBindingTypeAndVersioning[ bindingType ][ versioning ];
  19130. }
  19131. unbind() {
  19132. this.node = null;
  19133. // back to the prototype version of getValue / setValue
  19134. // note: avoiding to mutate the shape of 'this' via 'delete'
  19135. this.getValue = this._getValue_unbound;
  19136. this.setValue = this._setValue_unbound;
  19137. }
  19138. }
  19139. PropertyBinding.Composite = Composite;
  19140. PropertyBinding.prototype.BindingType = {
  19141. Direct: 0,
  19142. EntireArray: 1,
  19143. ArrayElement: 2,
  19144. HasFromToArray: 3
  19145. };
  19146. PropertyBinding.prototype.Versioning = {
  19147. None: 0,
  19148. NeedsUpdate: 1,
  19149. MatrixWorldNeedsUpdate: 2
  19150. };
  19151. PropertyBinding.prototype.GetterByBindingType = [
  19152. PropertyBinding.prototype._getValue_direct,
  19153. PropertyBinding.prototype._getValue_array,
  19154. PropertyBinding.prototype._getValue_arrayElement,
  19155. PropertyBinding.prototype._getValue_toArray,
  19156. ];
  19157. PropertyBinding.prototype.SetterByBindingTypeAndVersioning = [
  19158. [
  19159. // Direct
  19160. PropertyBinding.prototype._setValue_direct,
  19161. PropertyBinding.prototype._setValue_direct_setNeedsUpdate,
  19162. PropertyBinding.prototype._setValue_direct_setMatrixWorldNeedsUpdate,
  19163. ], [
  19164. // EntireArray
  19165. PropertyBinding.prototype._setValue_array,
  19166. PropertyBinding.prototype._setValue_array_setNeedsUpdate,
  19167. PropertyBinding.prototype._setValue_array_setMatrixWorldNeedsUpdate,
  19168. ], [
  19169. // ArrayElement
  19170. PropertyBinding.prototype._setValue_arrayElement,
  19171. PropertyBinding.prototype._setValue_arrayElement_setNeedsUpdate,
  19172. PropertyBinding.prototype._setValue_arrayElement_setMatrixWorldNeedsUpdate,
  19173. ], [
  19174. // HasToFromArray
  19175. PropertyBinding.prototype._setValue_fromArray,
  19176. PropertyBinding.prototype._setValue_fromArray_setNeedsUpdate,
  19177. PropertyBinding.prototype._setValue_fromArray_setMatrixWorldNeedsUpdate,
  19178. ]
  19179. ];
  19180. /**
  19181. *
  19182. * A group of objects that receives a shared animation state.
  19183. *
  19184. * Usage:
  19185. *
  19186. * - Add objects you would otherwise pass as 'root' to the
  19187. * constructor or the .clipAction method of AnimationMixer.
  19188. *
  19189. * - Instead pass this object as 'root'.
  19190. *
  19191. * - You can also add and remove objects later when the mixer
  19192. * is running.
  19193. *
  19194. * Note:
  19195. *
  19196. * Objects of this class appear as one object to the mixer,
  19197. * so cache control of the individual objects must be done
  19198. * on the group.
  19199. *
  19200. * Limitation:
  19201. *
  19202. * - The animated properties must be compatible among the
  19203. * all objects in the group.
  19204. *
  19205. * - A single property can either be controlled through a
  19206. * target group or directly, but not both.
  19207. */
  19208. class AnimationObjectGroup {
  19209. constructor() {
  19210. this.isAnimationObjectGroup = true;
  19211. this.uuid = generateUUID();
  19212. // cached objects followed by the active ones
  19213. this._objects = Array.prototype.slice.call( arguments );
  19214. this.nCachedObjects_ = 0; // threshold
  19215. // note: read by PropertyBinding.Composite
  19216. const indices = {};
  19217. this._indicesByUUID = indices; // for bookkeeping
  19218. for ( let i = 0, n = arguments.length; i !== n; ++ i ) {
  19219. indices[ arguments[ i ].uuid ] = i;
  19220. }
  19221. this._paths = []; // inside: string
  19222. this._parsedPaths = []; // inside: { we don't care, here }
  19223. this._bindings = []; // inside: Array< PropertyBinding >
  19224. this._bindingsIndicesByPath = {}; // inside: indices in these arrays
  19225. const scope = this;
  19226. this.stats = {
  19227. objects: {
  19228. get total() {
  19229. return scope._objects.length;
  19230. },
  19231. get inUse() {
  19232. return this.total - scope.nCachedObjects_;
  19233. }
  19234. },
  19235. get bindingsPerObject() {
  19236. return scope._bindings.length;
  19237. }
  19238. };
  19239. }
  19240. add() {
  19241. const objects = this._objects,
  19242. indicesByUUID = this._indicesByUUID,
  19243. paths = this._paths,
  19244. parsedPaths = this._parsedPaths,
  19245. bindings = this._bindings,
  19246. nBindings = bindings.length;
  19247. let knownObject = undefined,
  19248. nObjects = objects.length,
  19249. nCachedObjects = this.nCachedObjects_;
  19250. for ( let i = 0, n = arguments.length; i !== n; ++ i ) {
  19251. const object = arguments[ i ],
  19252. uuid = object.uuid;
  19253. let index = indicesByUUID[ uuid ];
  19254. if ( index === undefined ) {
  19255. // unknown object -> add it to the ACTIVE region
  19256. index = nObjects ++;
  19257. indicesByUUID[ uuid ] = index;
  19258. objects.push( object );
  19259. // accounting is done, now do the same for all bindings
  19260. for ( let j = 0, m = nBindings; j !== m; ++ j ) {
  19261. bindings[ j ].push( new PropertyBinding( object, paths[ j ], parsedPaths[ j ] ) );
  19262. }
  19263. } else if ( index < nCachedObjects ) {
  19264. knownObject = objects[ index ];
  19265. // move existing object to the ACTIVE region
  19266. const firstActiveIndex = -- nCachedObjects,
  19267. lastCachedObject = objects[ firstActiveIndex ];
  19268. indicesByUUID[ lastCachedObject.uuid ] = index;
  19269. objects[ index ] = lastCachedObject;
  19270. indicesByUUID[ uuid ] = firstActiveIndex;
  19271. objects[ firstActiveIndex ] = object;
  19272. // accounting is done, now do the same for all bindings
  19273. for ( let j = 0, m = nBindings; j !== m; ++ j ) {
  19274. const bindingsForPath = bindings[ j ],
  19275. lastCached = bindingsForPath[ firstActiveIndex ];
  19276. let binding = bindingsForPath[ index ];
  19277. bindingsForPath[ index ] = lastCached;
  19278. if ( binding === undefined ) {
  19279. // since we do not bother to create new bindings
  19280. // for objects that are cached, the binding may
  19281. // or may not exist
  19282. binding = new PropertyBinding( object, paths[ j ], parsedPaths[ j ] );
  19283. }
  19284. bindingsForPath[ firstActiveIndex ] = binding;
  19285. }
  19286. } else if ( objects[ index ] !== knownObject ) {
  19287. console.error( 'THREE.AnimationObjectGroup: Different objects with the same UUID ' +
  19288. 'detected. Clean the caches or recreate your infrastructure when reloading scenes.' );
  19289. } // else the object is already where we want it to be
  19290. } // for arguments
  19291. this.nCachedObjects_ = nCachedObjects;
  19292. }
  19293. remove() {
  19294. const objects = this._objects,
  19295. indicesByUUID = this._indicesByUUID,
  19296. bindings = this._bindings,
  19297. nBindings = bindings.length;
  19298. let nCachedObjects = this.nCachedObjects_;
  19299. for ( let i = 0, n = arguments.length; i !== n; ++ i ) {
  19300. const object = arguments[ i ],
  19301. uuid = object.uuid,
  19302. index = indicesByUUID[ uuid ];
  19303. if ( index !== undefined && index >= nCachedObjects ) {
  19304. // move existing object into the CACHED region
  19305. const lastCachedIndex = nCachedObjects ++,
  19306. firstActiveObject = objects[ lastCachedIndex ];
  19307. indicesByUUID[ firstActiveObject.uuid ] = index;
  19308. objects[ index ] = firstActiveObject;
  19309. indicesByUUID[ uuid ] = lastCachedIndex;
  19310. objects[ lastCachedIndex ] = object;
  19311. // accounting is done, now do the same for all bindings
  19312. for ( let j = 0, m = nBindings; j !== m; ++ j ) {
  19313. const bindingsForPath = bindings[ j ],
  19314. firstActive = bindingsForPath[ lastCachedIndex ],
  19315. binding = bindingsForPath[ index ];
  19316. bindingsForPath[ index ] = firstActive;
  19317. bindingsForPath[ lastCachedIndex ] = binding;
  19318. }
  19319. }
  19320. } // for arguments
  19321. this.nCachedObjects_ = nCachedObjects;
  19322. }
  19323. // remove & forget
  19324. uncache() {
  19325. const objects = this._objects,
  19326. indicesByUUID = this._indicesByUUID,
  19327. bindings = this._bindings,
  19328. nBindings = bindings.length;
  19329. let nCachedObjects = this.nCachedObjects_,
  19330. nObjects = objects.length;
  19331. for ( let i = 0, n = arguments.length; i !== n; ++ i ) {
  19332. const object = arguments[ i ],
  19333. uuid = object.uuid,
  19334. index = indicesByUUID[ uuid ];
  19335. if ( index !== undefined ) {
  19336. delete indicesByUUID[ uuid ];
  19337. if ( index < nCachedObjects ) {
  19338. // object is cached, shrink the CACHED region
  19339. const firstActiveIndex = -- nCachedObjects,
  19340. lastCachedObject = objects[ firstActiveIndex ],
  19341. lastIndex = -- nObjects,
  19342. lastObject = objects[ lastIndex ];
  19343. // last cached object takes this object's place
  19344. indicesByUUID[ lastCachedObject.uuid ] = index;
  19345. objects[ index ] = lastCachedObject;
  19346. // last object goes to the activated slot and pop
  19347. indicesByUUID[ lastObject.uuid ] = firstActiveIndex;
  19348. objects[ firstActiveIndex ] = lastObject;
  19349. objects.pop();
  19350. // accounting is done, now do the same for all bindings
  19351. for ( let j = 0, m = nBindings; j !== m; ++ j ) {
  19352. const bindingsForPath = bindings[ j ],
  19353. lastCached = bindingsForPath[ firstActiveIndex ],
  19354. last = bindingsForPath[ lastIndex ];
  19355. bindingsForPath[ index ] = lastCached;
  19356. bindingsForPath[ firstActiveIndex ] = last;
  19357. bindingsForPath.pop();
  19358. }
  19359. } else {
  19360. // object is active, just swap with the last and pop
  19361. const lastIndex = -- nObjects,
  19362. lastObject = objects[ lastIndex ];
  19363. if ( lastIndex > 0 ) {
  19364. indicesByUUID[ lastObject.uuid ] = index;
  19365. }
  19366. objects[ index ] = lastObject;
  19367. objects.pop();
  19368. // accounting is done, now do the same for all bindings
  19369. for ( let j = 0, m = nBindings; j !== m; ++ j ) {
  19370. const bindingsForPath = bindings[ j ];
  19371. bindingsForPath[ index ] = bindingsForPath[ lastIndex ];
  19372. bindingsForPath.pop();
  19373. }
  19374. } // cached or active
  19375. } // if object is known
  19376. } // for arguments
  19377. this.nCachedObjects_ = nCachedObjects;
  19378. }
  19379. // Internal interface used by befriended PropertyBinding.Composite:
  19380. subscribe_( path, parsedPath ) {
  19381. // returns an array of bindings for the given path that is changed
  19382. // according to the contained objects in the group
  19383. const indicesByPath = this._bindingsIndicesByPath;
  19384. let index = indicesByPath[ path ];
  19385. const bindings = this._bindings;
  19386. if ( index !== undefined ) return bindings[ index ];
  19387. const paths = this._paths,
  19388. parsedPaths = this._parsedPaths,
  19389. objects = this._objects,
  19390. nObjects = objects.length,
  19391. nCachedObjects = this.nCachedObjects_,
  19392. bindingsForPath = new Array( nObjects );
  19393. index = bindings.length;
  19394. indicesByPath[ path ] = index;
  19395. paths.push( path );
  19396. parsedPaths.push( parsedPath );
  19397. bindings.push( bindingsForPath );
  19398. for ( let i = nCachedObjects, n = objects.length; i !== n; ++ i ) {
  19399. const object = objects[ i ];
  19400. bindingsForPath[ i ] = new PropertyBinding( object, path, parsedPath );
  19401. }
  19402. return bindingsForPath;
  19403. }
  19404. unsubscribe_( path ) {
  19405. // tells the group to forget about a property path and no longer
  19406. // update the array previously obtained with 'subscribe_'
  19407. const indicesByPath = this._bindingsIndicesByPath,
  19408. index = indicesByPath[ path ];
  19409. if ( index !== undefined ) {
  19410. const paths = this._paths,
  19411. parsedPaths = this._parsedPaths,
  19412. bindings = this._bindings,
  19413. lastBindingsIndex = bindings.length - 1,
  19414. lastBindings = bindings[ lastBindingsIndex ],
  19415. lastBindingsPath = path[ lastBindingsIndex ];
  19416. indicesByPath[ lastBindingsPath ] = index;
  19417. bindings[ index ] = lastBindings;
  19418. bindings.pop();
  19419. parsedPaths[ index ] = parsedPaths[ lastBindingsIndex ];
  19420. parsedPaths.pop();
  19421. paths[ index ] = paths[ lastBindingsIndex ];
  19422. paths.pop();
  19423. }
  19424. }
  19425. }
  19426. class AnimationAction {
  19427. constructor( mixer, clip, localRoot = null, blendMode = clip.blendMode ) {
  19428. this._mixer = mixer;
  19429. this._clip = clip;
  19430. this._localRoot = localRoot;
  19431. this.blendMode = blendMode;
  19432. const tracks = clip.tracks,
  19433. nTracks = tracks.length,
  19434. interpolants = new Array( nTracks );
  19435. const interpolantSettings = {
  19436. endingStart: ZeroCurvatureEnding,
  19437. endingEnd: ZeroCurvatureEnding
  19438. };
  19439. for ( let i = 0; i !== nTracks; ++ i ) {
  19440. const interpolant = tracks[ i ].createInterpolant( null );
  19441. interpolants[ i ] = interpolant;
  19442. interpolant.settings = interpolantSettings;
  19443. }
  19444. this._interpolantSettings = interpolantSettings;
  19445. this._interpolants = interpolants; // bound by the mixer
  19446. // inside: PropertyMixer (managed by the mixer)
  19447. this._propertyBindings = new Array( nTracks );
  19448. this._cacheIndex = null; // for the memory manager
  19449. this._byClipCacheIndex = null; // for the memory manager
  19450. this._timeScaleInterpolant = null;
  19451. this._weightInterpolant = null;
  19452. this.loop = LoopRepeat;
  19453. this._loopCount = - 1;
  19454. // global mixer time when the action is to be started
  19455. // it's set back to 'null' upon start of the action
  19456. this._startTime = null;
  19457. // scaled local time of the action
  19458. // gets clamped or wrapped to 0..clip.duration according to loop
  19459. this.time = 0;
  19460. this.timeScale = 1;
  19461. this._effectiveTimeScale = 1;
  19462. this.weight = 1;
  19463. this._effectiveWeight = 1;
  19464. this.repetitions = Infinity; // no. of repetitions when looping
  19465. this.paused = false; // true -> zero effective time scale
  19466. this.enabled = true; // false -> zero effective weight
  19467. this.clampWhenFinished = false;// keep feeding the last frame?
  19468. this.zeroSlopeAtStart = true;// for smooth interpolation w/o separate
  19469. this.zeroSlopeAtEnd = true;// clips for start, loop and end
  19470. }
  19471. // State & Scheduling
  19472. play() {
  19473. this._mixer._activateAction( this );
  19474. return this;
  19475. }
  19476. stop() {
  19477. this._mixer._deactivateAction( this );
  19478. return this.reset();
  19479. }
  19480. reset() {
  19481. this.paused = false;
  19482. this.enabled = true;
  19483. this.time = 0; // restart clip
  19484. this._loopCount = - 1;// forget previous loops
  19485. this._startTime = null;// forget scheduling
  19486. return this.stopFading().stopWarping();
  19487. }
  19488. isRunning() {
  19489. return this.enabled && ! this.paused && this.timeScale !== 0 &&
  19490. this._startTime === null && this._mixer._isActiveAction( this );
  19491. }
  19492. // return true when play has been called
  19493. isScheduled() {
  19494. return this._mixer._isActiveAction( this );
  19495. }
  19496. startAt( time ) {
  19497. this._startTime = time;
  19498. return this;
  19499. }
  19500. setLoop( mode, repetitions ) {
  19501. this.loop = mode;
  19502. this.repetitions = repetitions;
  19503. return this;
  19504. }
  19505. // Weight
  19506. // set the weight stopping any scheduled fading
  19507. // although .enabled = false yields an effective weight of zero, this
  19508. // method does *not* change .enabled, because it would be confusing
  19509. setEffectiveWeight( weight ) {
  19510. this.weight = weight;
  19511. // note: same logic as when updated at runtime
  19512. this._effectiveWeight = this.enabled ? weight : 0;
  19513. return this.stopFading();
  19514. }
  19515. // return the weight considering fading and .enabled
  19516. getEffectiveWeight() {
  19517. return this._effectiveWeight;
  19518. }
  19519. fadeIn( duration ) {
  19520. return this._scheduleFading( duration, 0, 1 );
  19521. }
  19522. fadeOut( duration ) {
  19523. return this._scheduleFading( duration, 1, 0 );
  19524. }
  19525. crossFadeFrom( fadeOutAction, duration, warp ) {
  19526. fadeOutAction.fadeOut( duration );
  19527. this.fadeIn( duration );
  19528. if ( warp ) {
  19529. const fadeInDuration = this._clip.duration,
  19530. fadeOutDuration = fadeOutAction._clip.duration,
  19531. startEndRatio = fadeOutDuration / fadeInDuration,
  19532. endStartRatio = fadeInDuration / fadeOutDuration;
  19533. fadeOutAction.warp( 1.0, startEndRatio, duration );
  19534. this.warp( endStartRatio, 1.0, duration );
  19535. }
  19536. return this;
  19537. }
  19538. crossFadeTo( fadeInAction, duration, warp ) {
  19539. return fadeInAction.crossFadeFrom( this, duration, warp );
  19540. }
  19541. stopFading() {
  19542. const weightInterpolant = this._weightInterpolant;
  19543. if ( weightInterpolant !== null ) {
  19544. this._weightInterpolant = null;
  19545. this._mixer._takeBackControlInterpolant( weightInterpolant );
  19546. }
  19547. return this;
  19548. }
  19549. // Time Scale Control
  19550. // set the time scale stopping any scheduled warping
  19551. // although .paused = true yields an effective time scale of zero, this
  19552. // method does *not* change .paused, because it would be confusing
  19553. setEffectiveTimeScale( timeScale ) {
  19554. this.timeScale = timeScale;
  19555. this._effectiveTimeScale = this.paused ? 0 : timeScale;
  19556. return this.stopWarping();
  19557. }
  19558. // return the time scale considering warping and .paused
  19559. getEffectiveTimeScale() {
  19560. return this._effectiveTimeScale;
  19561. }
  19562. setDuration( duration ) {
  19563. this.timeScale = this._clip.duration / duration;
  19564. return this.stopWarping();
  19565. }
  19566. syncWith( action ) {
  19567. this.time = action.time;
  19568. this.timeScale = action.timeScale;
  19569. return this.stopWarping();
  19570. }
  19571. halt( duration ) {
  19572. return this.warp( this._effectiveTimeScale, 0, duration );
  19573. }
  19574. warp( startTimeScale, endTimeScale, duration ) {
  19575. const mixer = this._mixer,
  19576. now = mixer.time,
  19577. timeScale = this.timeScale;
  19578. let interpolant = this._timeScaleInterpolant;
  19579. if ( interpolant === null ) {
  19580. interpolant = mixer._lendControlInterpolant();
  19581. this._timeScaleInterpolant = interpolant;
  19582. }
  19583. const times = interpolant.parameterPositions,
  19584. values = interpolant.sampleValues;
  19585. times[ 0 ] = now;
  19586. times[ 1 ] = now + duration;
  19587. values[ 0 ] = startTimeScale / timeScale;
  19588. values[ 1 ] = endTimeScale / timeScale;
  19589. return this;
  19590. }
  19591. stopWarping() {
  19592. const timeScaleInterpolant = this._timeScaleInterpolant;
  19593. if ( timeScaleInterpolant !== null ) {
  19594. this._timeScaleInterpolant = null;
  19595. this._mixer._takeBackControlInterpolant( timeScaleInterpolant );
  19596. }
  19597. return this;
  19598. }
  19599. // Object Accessors
  19600. getMixer() {
  19601. return this._mixer;
  19602. }
  19603. getClip() {
  19604. return this._clip;
  19605. }
  19606. getRoot() {
  19607. return this._localRoot || this._mixer._root;
  19608. }
  19609. // Interna
  19610. _update( time, deltaTime, timeDirection, accuIndex ) {
  19611. // called by the mixer
  19612. if ( ! this.enabled ) {
  19613. // call ._updateWeight() to update ._effectiveWeight
  19614. this._updateWeight( time );
  19615. return;
  19616. }
  19617. const startTime = this._startTime;
  19618. if ( startTime !== null ) {
  19619. // check for scheduled start of action
  19620. const timeRunning = ( time - startTime ) * timeDirection;
  19621. if ( timeRunning < 0 || timeDirection === 0 ) {
  19622. deltaTime = 0;
  19623. } else {
  19624. this._startTime = null; // unschedule
  19625. deltaTime = timeDirection * timeRunning;
  19626. }
  19627. }
  19628. // apply time scale and advance time
  19629. deltaTime *= this._updateTimeScale( time );
  19630. const clipTime = this._updateTime( deltaTime );
  19631. // note: _updateTime may disable the action resulting in
  19632. // an effective weight of 0
  19633. const weight = this._updateWeight( time );
  19634. if ( weight > 0 ) {
  19635. const interpolants = this._interpolants;
  19636. const propertyMixers = this._propertyBindings;
  19637. switch ( this.blendMode ) {
  19638. case AdditiveAnimationBlendMode:
  19639. for ( let j = 0, m = interpolants.length; j !== m; ++ j ) {
  19640. interpolants[ j ].evaluate( clipTime );
  19641. propertyMixers[ j ].accumulateAdditive( weight );
  19642. }
  19643. break;
  19644. case NormalAnimationBlendMode:
  19645. default:
  19646. for ( let j = 0, m = interpolants.length; j !== m; ++ j ) {
  19647. interpolants[ j ].evaluate( clipTime );
  19648. propertyMixers[ j ].accumulate( accuIndex, weight );
  19649. }
  19650. }
  19651. }
  19652. }
  19653. _updateWeight( time ) {
  19654. let weight = 0;
  19655. if ( this.enabled ) {
  19656. weight = this.weight;
  19657. const interpolant = this._weightInterpolant;
  19658. if ( interpolant !== null ) {
  19659. const interpolantValue = interpolant.evaluate( time )[ 0 ];
  19660. weight *= interpolantValue;
  19661. if ( time > interpolant.parameterPositions[ 1 ] ) {
  19662. this.stopFading();
  19663. if ( interpolantValue === 0 ) {
  19664. // faded out, disable
  19665. this.enabled = false;
  19666. }
  19667. }
  19668. }
  19669. }
  19670. this._effectiveWeight = weight;
  19671. return weight;
  19672. }
  19673. _updateTimeScale( time ) {
  19674. let timeScale = 0;
  19675. if ( ! this.paused ) {
  19676. timeScale = this.timeScale;
  19677. const interpolant = this._timeScaleInterpolant;
  19678. if ( interpolant !== null ) {
  19679. const interpolantValue = interpolant.evaluate( time )[ 0 ];
  19680. timeScale *= interpolantValue;
  19681. if ( time > interpolant.parameterPositions[ 1 ] ) {
  19682. this.stopWarping();
  19683. if ( timeScale === 0 ) {
  19684. // motion has halted, pause
  19685. this.paused = true;
  19686. } else {
  19687. // warp done - apply final time scale
  19688. this.timeScale = timeScale;
  19689. }
  19690. }
  19691. }
  19692. }
  19693. this._effectiveTimeScale = timeScale;
  19694. return timeScale;
  19695. }
  19696. _updateTime( deltaTime ) {
  19697. const duration = this._clip.duration;
  19698. const loop = this.loop;
  19699. let time = this.time + deltaTime;
  19700. let loopCount = this._loopCount;
  19701. const pingPong = ( loop === LoopPingPong );
  19702. if ( deltaTime === 0 ) {
  19703. if ( loopCount === - 1 ) return time;
  19704. return ( pingPong && ( loopCount & 1 ) === 1 ) ? duration - time : time;
  19705. }
  19706. if ( loop === LoopOnce ) {
  19707. if ( loopCount === - 1 ) {
  19708. // just started
  19709. this._loopCount = 0;
  19710. this._setEndings( true, true, false );
  19711. }
  19712. handle_stop: {
  19713. if ( time >= duration ) {
  19714. time = duration;
  19715. } else if ( time < 0 ) {
  19716. time = 0;
  19717. } else {
  19718. this.time = time;
  19719. break handle_stop;
  19720. }
  19721. if ( this.clampWhenFinished ) this.paused = true;
  19722. else this.enabled = false;
  19723. this.time = time;
  19724. this._mixer.dispatchEvent( {
  19725. type: 'finished', action: this,
  19726. direction: deltaTime < 0 ? - 1 : 1
  19727. } );
  19728. }
  19729. } else { // repetitive Repeat or PingPong
  19730. if ( loopCount === - 1 ) {
  19731. // just started
  19732. if ( deltaTime >= 0 ) {
  19733. loopCount = 0;
  19734. this._setEndings( true, this.repetitions === 0, pingPong );
  19735. } else {
  19736. // when looping in reverse direction, the initial
  19737. // transition through zero counts as a repetition,
  19738. // so leave loopCount at -1
  19739. this._setEndings( this.repetitions === 0, true, pingPong );
  19740. }
  19741. }
  19742. if ( time >= duration || time < 0 ) {
  19743. // wrap around
  19744. const loopDelta = Math.floor( time / duration ); // signed
  19745. time -= duration * loopDelta;
  19746. loopCount += Math.abs( loopDelta );
  19747. const pending = this.repetitions - loopCount;
  19748. if ( pending <= 0 ) {
  19749. // have to stop (switch state, clamp time, fire event)
  19750. if ( this.clampWhenFinished ) this.paused = true;
  19751. else this.enabled = false;
  19752. time = deltaTime > 0 ? duration : 0;
  19753. this.time = time;
  19754. this._mixer.dispatchEvent( {
  19755. type: 'finished', action: this,
  19756. direction: deltaTime > 0 ? 1 : - 1
  19757. } );
  19758. } else {
  19759. // keep running
  19760. if ( pending === 1 ) {
  19761. // entering the last round
  19762. const atStart = deltaTime < 0;
  19763. this._setEndings( atStart, ! atStart, pingPong );
  19764. } else {
  19765. this._setEndings( false, false, pingPong );
  19766. }
  19767. this._loopCount = loopCount;
  19768. this.time = time;
  19769. this._mixer.dispatchEvent( {
  19770. type: 'loop', action: this, loopDelta: loopDelta
  19771. } );
  19772. }
  19773. } else {
  19774. this.time = time;
  19775. }
  19776. if ( pingPong && ( loopCount & 1 ) === 1 ) {
  19777. // invert time for the "pong round"
  19778. return duration - time;
  19779. }
  19780. }
  19781. return time;
  19782. }
  19783. _setEndings( atStart, atEnd, pingPong ) {
  19784. const settings = this._interpolantSettings;
  19785. if ( pingPong ) {
  19786. settings.endingStart = ZeroSlopeEnding;
  19787. settings.endingEnd = ZeroSlopeEnding;
  19788. } else {
  19789. // assuming for LoopOnce atStart == atEnd == true
  19790. if ( atStart ) {
  19791. settings.endingStart = this.zeroSlopeAtStart ? ZeroSlopeEnding : ZeroCurvatureEnding;
  19792. } else {
  19793. settings.endingStart = WrapAroundEnding;
  19794. }
  19795. if ( atEnd ) {
  19796. settings.endingEnd = this.zeroSlopeAtEnd ? ZeroSlopeEnding : ZeroCurvatureEnding;
  19797. } else {
  19798. settings.endingEnd = WrapAroundEnding;
  19799. }
  19800. }
  19801. }
  19802. _scheduleFading( duration, weightNow, weightThen ) {
  19803. const mixer = this._mixer, now = mixer.time;
  19804. let interpolant = this._weightInterpolant;
  19805. if ( interpolant === null ) {
  19806. interpolant = mixer._lendControlInterpolant();
  19807. this._weightInterpolant = interpolant;
  19808. }
  19809. const times = interpolant.parameterPositions,
  19810. values = interpolant.sampleValues;
  19811. times[ 0 ] = now;
  19812. values[ 0 ] = weightNow;
  19813. times[ 1 ] = now + duration;
  19814. values[ 1 ] = weightThen;
  19815. return this;
  19816. }
  19817. }
  19818. const _controlInterpolantsResultBuffer = new Float32Array( 1 );
  19819. class AnimationMixer extends EventDispatcher {
  19820. constructor( root ) {
  19821. super();
  19822. this._root = root;
  19823. this._initMemoryManager();
  19824. this._accuIndex = 0;
  19825. this.time = 0;
  19826. this.timeScale = 1.0;
  19827. }
  19828. _bindAction( action, prototypeAction ) {
  19829. const root = action._localRoot || this._root,
  19830. tracks = action._clip.tracks,
  19831. nTracks = tracks.length,
  19832. bindings = action._propertyBindings,
  19833. interpolants = action._interpolants,
  19834. rootUuid = root.uuid,
  19835. bindingsByRoot = this._bindingsByRootAndName;
  19836. let bindingsByName = bindingsByRoot[ rootUuid ];
  19837. if ( bindingsByName === undefined ) {
  19838. bindingsByName = {};
  19839. bindingsByRoot[ rootUuid ] = bindingsByName;
  19840. }
  19841. for ( let i = 0; i !== nTracks; ++ i ) {
  19842. const track = tracks[ i ],
  19843. trackName = track.name;
  19844. let binding = bindingsByName[ trackName ];
  19845. if ( binding !== undefined ) {
  19846. ++ binding.referenceCount;
  19847. bindings[ i ] = binding;
  19848. } else {
  19849. binding = bindings[ i ];
  19850. if ( binding !== undefined ) {
  19851. // existing binding, make sure the cache knows
  19852. if ( binding._cacheIndex === null ) {
  19853. ++ binding.referenceCount;
  19854. this._addInactiveBinding( binding, rootUuid, trackName );
  19855. }
  19856. continue;
  19857. }
  19858. const path = prototypeAction && prototypeAction.
  19859. _propertyBindings[ i ].binding.parsedPath;
  19860. binding = new PropertyMixer(
  19861. PropertyBinding.create( root, trackName, path ),
  19862. track.ValueTypeName, track.getValueSize() );
  19863. ++ binding.referenceCount;
  19864. this._addInactiveBinding( binding, rootUuid, trackName );
  19865. bindings[ i ] = binding;
  19866. }
  19867. interpolants[ i ].resultBuffer = binding.buffer;
  19868. }
  19869. }
  19870. _activateAction( action ) {
  19871. if ( ! this._isActiveAction( action ) ) {
  19872. if ( action._cacheIndex === null ) {
  19873. // this action has been forgotten by the cache, but the user
  19874. // appears to be still using it -> rebind
  19875. const rootUuid = ( action._localRoot || this._root ).uuid,
  19876. clipUuid = action._clip.uuid,
  19877. actionsForClip = this._actionsByClip[ clipUuid ];
  19878. this._bindAction( action,
  19879. actionsForClip && actionsForClip.knownActions[ 0 ] );
  19880. this._addInactiveAction( action, clipUuid, rootUuid );
  19881. }
  19882. const bindings = action._propertyBindings;
  19883. // increment reference counts / sort out state
  19884. for ( let i = 0, n = bindings.length; i !== n; ++ i ) {
  19885. const binding = bindings[ i ];
  19886. if ( binding.useCount ++ === 0 ) {
  19887. this._lendBinding( binding );
  19888. binding.saveOriginalState();
  19889. }
  19890. }
  19891. this._lendAction( action );
  19892. }
  19893. }
  19894. _deactivateAction( action ) {
  19895. if ( this._isActiveAction( action ) ) {
  19896. const bindings = action._propertyBindings;
  19897. // decrement reference counts / sort out state
  19898. for ( let i = 0, n = bindings.length; i !== n; ++ i ) {
  19899. const binding = bindings[ i ];
  19900. if ( -- binding.useCount === 0 ) {
  19901. binding.restoreOriginalState();
  19902. this._takeBackBinding( binding );
  19903. }
  19904. }
  19905. this._takeBackAction( action );
  19906. }
  19907. }
  19908. // Memory manager
  19909. _initMemoryManager() {
  19910. this._actions = []; // 'nActiveActions' followed by inactive ones
  19911. this._nActiveActions = 0;
  19912. this._actionsByClip = {};
  19913. // inside:
  19914. // {
  19915. // knownActions: Array< AnimationAction > - used as prototypes
  19916. // actionByRoot: AnimationAction - lookup
  19917. // }
  19918. this._bindings = []; // 'nActiveBindings' followed by inactive ones
  19919. this._nActiveBindings = 0;
  19920. this._bindingsByRootAndName = {}; // inside: Map< name, PropertyMixer >
  19921. this._controlInterpolants = []; // same game as above
  19922. this._nActiveControlInterpolants = 0;
  19923. const scope = this;
  19924. this.stats = {
  19925. actions: {
  19926. get total() {
  19927. return scope._actions.length;
  19928. },
  19929. get inUse() {
  19930. return scope._nActiveActions;
  19931. }
  19932. },
  19933. bindings: {
  19934. get total() {
  19935. return scope._bindings.length;
  19936. },
  19937. get inUse() {
  19938. return scope._nActiveBindings;
  19939. }
  19940. },
  19941. controlInterpolants: {
  19942. get total() {
  19943. return scope._controlInterpolants.length;
  19944. },
  19945. get inUse() {
  19946. return scope._nActiveControlInterpolants;
  19947. }
  19948. }
  19949. };
  19950. }
  19951. // Memory management for AnimationAction objects
  19952. _isActiveAction( action ) {
  19953. const index = action._cacheIndex;
  19954. return index !== null && index < this._nActiveActions;
  19955. }
  19956. _addInactiveAction( action, clipUuid, rootUuid ) {
  19957. const actions = this._actions,
  19958. actionsByClip = this._actionsByClip;
  19959. let actionsForClip = actionsByClip[ clipUuid ];
  19960. if ( actionsForClip === undefined ) {
  19961. actionsForClip = {
  19962. knownActions: [ action ],
  19963. actionByRoot: {}
  19964. };
  19965. action._byClipCacheIndex = 0;
  19966. actionsByClip[ clipUuid ] = actionsForClip;
  19967. } else {
  19968. const knownActions = actionsForClip.knownActions;
  19969. action._byClipCacheIndex = knownActions.length;
  19970. knownActions.push( action );
  19971. }
  19972. action._cacheIndex = actions.length;
  19973. actions.push( action );
  19974. actionsForClip.actionByRoot[ rootUuid ] = action;
  19975. }
  19976. _removeInactiveAction( action ) {
  19977. const actions = this._actions,
  19978. lastInactiveAction = actions[ actions.length - 1 ],
  19979. cacheIndex = action._cacheIndex;
  19980. lastInactiveAction._cacheIndex = cacheIndex;
  19981. actions[ cacheIndex ] = lastInactiveAction;
  19982. actions.pop();
  19983. action._cacheIndex = null;
  19984. const clipUuid = action._clip.uuid,
  19985. actionsByClip = this._actionsByClip,
  19986. actionsForClip = actionsByClip[ clipUuid ],
  19987. knownActionsForClip = actionsForClip.knownActions,
  19988. lastKnownAction =
  19989. knownActionsForClip[ knownActionsForClip.length - 1 ],
  19990. byClipCacheIndex = action._byClipCacheIndex;
  19991. lastKnownAction._byClipCacheIndex = byClipCacheIndex;
  19992. knownActionsForClip[ byClipCacheIndex ] = lastKnownAction;
  19993. knownActionsForClip.pop();
  19994. action._byClipCacheIndex = null;
  19995. const actionByRoot = actionsForClip.actionByRoot,
  19996. rootUuid = ( action._localRoot || this._root ).uuid;
  19997. delete actionByRoot[ rootUuid ];
  19998. if ( knownActionsForClip.length === 0 ) {
  19999. delete actionsByClip[ clipUuid ];
  20000. }
  20001. this._removeInactiveBindingsForAction( action );
  20002. }
  20003. _removeInactiveBindingsForAction( action ) {
  20004. const bindings = action._propertyBindings;
  20005. for ( let i = 0, n = bindings.length; i !== n; ++ i ) {
  20006. const binding = bindings[ i ];
  20007. if ( -- binding.referenceCount === 0 ) {
  20008. this._removeInactiveBinding( binding );
  20009. }
  20010. }
  20011. }
  20012. _lendAction( action ) {
  20013. // [ active actions | inactive actions ]
  20014. // [ active actions >| inactive actions ]
  20015. // s a
  20016. // <-swap->
  20017. // a s
  20018. const actions = this._actions,
  20019. prevIndex = action._cacheIndex,
  20020. lastActiveIndex = this._nActiveActions ++,
  20021. firstInactiveAction = actions[ lastActiveIndex ];
  20022. action._cacheIndex = lastActiveIndex;
  20023. actions[ lastActiveIndex ] = action;
  20024. firstInactiveAction._cacheIndex = prevIndex;
  20025. actions[ prevIndex ] = firstInactiveAction;
  20026. }
  20027. _takeBackAction( action ) {
  20028. // [ active actions | inactive actions ]
  20029. // [ active actions |< inactive actions ]
  20030. // a s
  20031. // <-swap->
  20032. // s a
  20033. const actions = this._actions,
  20034. prevIndex = action._cacheIndex,
  20035. firstInactiveIndex = -- this._nActiveActions,
  20036. lastActiveAction = actions[ firstInactiveIndex ];
  20037. action._cacheIndex = firstInactiveIndex;
  20038. actions[ firstInactiveIndex ] = action;
  20039. lastActiveAction._cacheIndex = prevIndex;
  20040. actions[ prevIndex ] = lastActiveAction;
  20041. }
  20042. // Memory management for PropertyMixer objects
  20043. _addInactiveBinding( binding, rootUuid, trackName ) {
  20044. const bindingsByRoot = this._bindingsByRootAndName,
  20045. bindings = this._bindings;
  20046. let bindingByName = bindingsByRoot[ rootUuid ];
  20047. if ( bindingByName === undefined ) {
  20048. bindingByName = {};
  20049. bindingsByRoot[ rootUuid ] = bindingByName;
  20050. }
  20051. bindingByName[ trackName ] = binding;
  20052. binding._cacheIndex = bindings.length;
  20053. bindings.push( binding );
  20054. }
  20055. _removeInactiveBinding( binding ) {
  20056. const bindings = this._bindings,
  20057. propBinding = binding.binding,
  20058. rootUuid = propBinding.rootNode.uuid,
  20059. trackName = propBinding.path,
  20060. bindingsByRoot = this._bindingsByRootAndName,
  20061. bindingByName = bindingsByRoot[ rootUuid ],
  20062. lastInactiveBinding = bindings[ bindings.length - 1 ],
  20063. cacheIndex = binding._cacheIndex;
  20064. lastInactiveBinding._cacheIndex = cacheIndex;
  20065. bindings[ cacheIndex ] = lastInactiveBinding;
  20066. bindings.pop();
  20067. delete bindingByName[ trackName ];
  20068. if ( Object.keys( bindingByName ).length === 0 ) {
  20069. delete bindingsByRoot[ rootUuid ];
  20070. }
  20071. }
  20072. _lendBinding( binding ) {
  20073. const bindings = this._bindings,
  20074. prevIndex = binding._cacheIndex,
  20075. lastActiveIndex = this._nActiveBindings ++,
  20076. firstInactiveBinding = bindings[ lastActiveIndex ];
  20077. binding._cacheIndex = lastActiveIndex;
  20078. bindings[ lastActiveIndex ] = binding;
  20079. firstInactiveBinding._cacheIndex = prevIndex;
  20080. bindings[ prevIndex ] = firstInactiveBinding;
  20081. }
  20082. _takeBackBinding( binding ) {
  20083. const bindings = this._bindings,
  20084. prevIndex = binding._cacheIndex,
  20085. firstInactiveIndex = -- this._nActiveBindings,
  20086. lastActiveBinding = bindings[ firstInactiveIndex ];
  20087. binding._cacheIndex = firstInactiveIndex;
  20088. bindings[ firstInactiveIndex ] = binding;
  20089. lastActiveBinding._cacheIndex = prevIndex;
  20090. bindings[ prevIndex ] = lastActiveBinding;
  20091. }
  20092. // Memory management of Interpolants for weight and time scale
  20093. _lendControlInterpolant() {
  20094. const interpolants = this._controlInterpolants,
  20095. lastActiveIndex = this._nActiveControlInterpolants ++;
  20096. let interpolant = interpolants[ lastActiveIndex ];
  20097. if ( interpolant === undefined ) {
  20098. interpolant = new LinearInterpolant(
  20099. new Float32Array( 2 ), new Float32Array( 2 ),
  20100. 1, _controlInterpolantsResultBuffer );
  20101. interpolant.__cacheIndex = lastActiveIndex;
  20102. interpolants[ lastActiveIndex ] = interpolant;
  20103. }
  20104. return interpolant;
  20105. }
  20106. _takeBackControlInterpolant( interpolant ) {
  20107. const interpolants = this._controlInterpolants,
  20108. prevIndex = interpolant.__cacheIndex,
  20109. firstInactiveIndex = -- this._nActiveControlInterpolants,
  20110. lastActiveInterpolant = interpolants[ firstInactiveIndex ];
  20111. interpolant.__cacheIndex = firstInactiveIndex;
  20112. interpolants[ firstInactiveIndex ] = interpolant;
  20113. lastActiveInterpolant.__cacheIndex = prevIndex;
  20114. interpolants[ prevIndex ] = lastActiveInterpolant;
  20115. }
  20116. // return an action for a clip optionally using a custom root target
  20117. // object (this method allocates a lot of dynamic memory in case a
  20118. // previously unknown clip/root combination is specified)
  20119. clipAction( clip, optionalRoot, blendMode ) {
  20120. const root = optionalRoot || this._root,
  20121. rootUuid = root.uuid;
  20122. let clipObject = typeof clip === 'string' ? AnimationClip.findByName( root, clip ) : clip;
  20123. const clipUuid = clipObject !== null ? clipObject.uuid : clip;
  20124. const actionsForClip = this._actionsByClip[ clipUuid ];
  20125. let prototypeAction = null;
  20126. if ( blendMode === undefined ) {
  20127. if ( clipObject !== null ) {
  20128. blendMode = clipObject.blendMode;
  20129. } else {
  20130. blendMode = NormalAnimationBlendMode;
  20131. }
  20132. }
  20133. if ( actionsForClip !== undefined ) {
  20134. const existingAction = actionsForClip.actionByRoot[ rootUuid ];
  20135. if ( existingAction !== undefined && existingAction.blendMode === blendMode ) {
  20136. return existingAction;
  20137. }
  20138. // we know the clip, so we don't have to parse all
  20139. // the bindings again but can just copy
  20140. prototypeAction = actionsForClip.knownActions[ 0 ];
  20141. // also, take the clip from the prototype action
  20142. if ( clipObject === null )
  20143. clipObject = prototypeAction._clip;
  20144. }
  20145. // clip must be known when specified via string
  20146. if ( clipObject === null ) return null;
  20147. // allocate all resources required to run it
  20148. const newAction = new AnimationAction( this, clipObject, optionalRoot, blendMode );
  20149. this._bindAction( newAction, prototypeAction );
  20150. // and make the action known to the memory manager
  20151. this._addInactiveAction( newAction, clipUuid, rootUuid );
  20152. return newAction;
  20153. }
  20154. // get an existing action
  20155. existingAction( clip, optionalRoot ) {
  20156. const root = optionalRoot || this._root,
  20157. rootUuid = root.uuid,
  20158. clipObject = typeof clip === 'string' ?
  20159. AnimationClip.findByName( root, clip ) : clip,
  20160. clipUuid = clipObject ? clipObject.uuid : clip,
  20161. actionsForClip = this._actionsByClip[ clipUuid ];
  20162. if ( actionsForClip !== undefined ) {
  20163. return actionsForClip.actionByRoot[ rootUuid ] || null;
  20164. }
  20165. return null;
  20166. }
  20167. // deactivates all previously scheduled actions
  20168. stopAllAction() {
  20169. const actions = this._actions,
  20170. nActions = this._nActiveActions;
  20171. for ( let i = nActions - 1; i >= 0; -- i ) {
  20172. actions[ i ].stop();
  20173. }
  20174. return this;
  20175. }
  20176. // advance the time and update apply the animation
  20177. update( deltaTime ) {
  20178. deltaTime *= this.timeScale;
  20179. const actions = this._actions,
  20180. nActions = this._nActiveActions,
  20181. time = this.time += deltaTime,
  20182. timeDirection = Math.sign( deltaTime ),
  20183. accuIndex = this._accuIndex ^= 1;
  20184. // run active actions
  20185. for ( let i = 0; i !== nActions; ++ i ) {
  20186. const action = actions[ i ];
  20187. action._update( time, deltaTime, timeDirection, accuIndex );
  20188. }
  20189. // update scene graph
  20190. const bindings = this._bindings,
  20191. nBindings = this._nActiveBindings;
  20192. for ( let i = 0; i !== nBindings; ++ i ) {
  20193. bindings[ i ].apply( accuIndex );
  20194. }
  20195. return this;
  20196. }
  20197. // Allows you to seek to a specific time in an animation.
  20198. setTime( timeInSeconds ) {
  20199. this.time = 0; // Zero out time attribute for AnimationMixer object;
  20200. for ( let i = 0; i < this._actions.length; i ++ ) {
  20201. this._actions[ i ].time = 0; // Zero out time attribute for all associated AnimationAction objects.
  20202. }
  20203. return this.update( timeInSeconds ); // Update used to set exact time. Returns "this" AnimationMixer object.
  20204. }
  20205. // return this mixer's root target object
  20206. getRoot() {
  20207. return this._root;
  20208. }
  20209. // free all resources specific to a particular clip
  20210. uncacheClip( clip ) {
  20211. const actions = this._actions,
  20212. clipUuid = clip.uuid,
  20213. actionsByClip = this._actionsByClip,
  20214. actionsForClip = actionsByClip[ clipUuid ];
  20215. if ( actionsForClip !== undefined ) {
  20216. // note: just calling _removeInactiveAction would mess up the
  20217. // iteration state and also require updating the state we can
  20218. // just throw away
  20219. const actionsToRemove = actionsForClip.knownActions;
  20220. for ( let i = 0, n = actionsToRemove.length; i !== n; ++ i ) {
  20221. const action = actionsToRemove[ i ];
  20222. this._deactivateAction( action );
  20223. const cacheIndex = action._cacheIndex,
  20224. lastInactiveAction = actions[ actions.length - 1 ];
  20225. action._cacheIndex = null;
  20226. action._byClipCacheIndex = null;
  20227. lastInactiveAction._cacheIndex = cacheIndex;
  20228. actions[ cacheIndex ] = lastInactiveAction;
  20229. actions.pop();
  20230. this._removeInactiveBindingsForAction( action );
  20231. }
  20232. delete actionsByClip[ clipUuid ];
  20233. }
  20234. }
  20235. // free all resources specific to a particular root target object
  20236. uncacheRoot( root ) {
  20237. const rootUuid = root.uuid,
  20238. actionsByClip = this._actionsByClip;
  20239. for ( const clipUuid in actionsByClip ) {
  20240. const actionByRoot = actionsByClip[ clipUuid ].actionByRoot,
  20241. action = actionByRoot[ rootUuid ];
  20242. if ( action !== undefined ) {
  20243. this._deactivateAction( action );
  20244. this._removeInactiveAction( action );
  20245. }
  20246. }
  20247. const bindingsByRoot = this._bindingsByRootAndName,
  20248. bindingByName = bindingsByRoot[ rootUuid ];
  20249. if ( bindingByName !== undefined ) {
  20250. for ( const trackName in bindingByName ) {
  20251. const binding = bindingByName[ trackName ];
  20252. binding.restoreOriginalState();
  20253. this._removeInactiveBinding( binding );
  20254. }
  20255. }
  20256. }
  20257. // remove a targeted clip from the cache
  20258. uncacheAction( clip, optionalRoot ) {
  20259. const action = this.existingAction( clip, optionalRoot );
  20260. if ( action !== null ) {
  20261. this._deactivateAction( action );
  20262. this._removeInactiveAction( action );
  20263. }
  20264. }
  20265. }
  20266. class RenderTarget3D extends RenderTarget {
  20267. constructor( width = 1, height = 1, depth = 1, options = {} ) {
  20268. super( width, height, options );
  20269. this.isRenderTarget3D = true;
  20270. this.depth = depth;
  20271. this.texture = new Data3DTexture( null, width, height, depth );
  20272. this.texture.isRenderTargetTexture = true;
  20273. }
  20274. }
  20275. class RenderTargetArray extends RenderTarget {
  20276. constructor( width = 1, height = 1, depth = 1, options = {} ) {
  20277. super( width, height, options );
  20278. this.isRenderTargetArray = true;
  20279. this.depth = depth;
  20280. this.texture = new DataArrayTexture( null, width, height, depth );
  20281. this.texture.isRenderTargetTexture = true;
  20282. }
  20283. }
  20284. class Uniform {
  20285. constructor( value ) {
  20286. this.value = value;
  20287. }
  20288. clone() {
  20289. return new Uniform( this.value.clone === undefined ? this.value : this.value.clone() );
  20290. }
  20291. }
  20292. let _id = 0;
  20293. class UniformsGroup extends EventDispatcher {
  20294. constructor() {
  20295. super();
  20296. this.isUniformsGroup = true;
  20297. Object.defineProperty( this, 'id', { value: _id ++ } );
  20298. this.name = '';
  20299. this.usage = StaticDrawUsage;
  20300. this.uniforms = [];
  20301. }
  20302. add( uniform ) {
  20303. this.uniforms.push( uniform );
  20304. return this;
  20305. }
  20306. remove( uniform ) {
  20307. const index = this.uniforms.indexOf( uniform );
  20308. if ( index !== - 1 ) this.uniforms.splice( index, 1 );
  20309. return this;
  20310. }
  20311. setName( name ) {
  20312. this.name = name;
  20313. return this;
  20314. }
  20315. setUsage( value ) {
  20316. this.usage = value;
  20317. return this;
  20318. }
  20319. dispose() {
  20320. this.dispatchEvent( { type: 'dispose' } );
  20321. return this;
  20322. }
  20323. copy( source ) {
  20324. this.name = source.name;
  20325. this.usage = source.usage;
  20326. const uniformsSource = source.uniforms;
  20327. this.uniforms.length = 0;
  20328. for ( let i = 0, l = uniformsSource.length; i < l; i ++ ) {
  20329. const uniforms = Array.isArray( uniformsSource[ i ] ) ? uniformsSource[ i ] : [ uniformsSource[ i ] ];
  20330. for ( let j = 0; j < uniforms.length; j ++ ) {
  20331. this.uniforms.push( uniforms[ j ].clone() );
  20332. }
  20333. }
  20334. return this;
  20335. }
  20336. clone() {
  20337. return new this.constructor().copy( this );
  20338. }
  20339. }
  20340. class InstancedInterleavedBuffer extends InterleavedBuffer {
  20341. constructor( array, stride, meshPerAttribute = 1 ) {
  20342. super( array, stride );
  20343. this.isInstancedInterleavedBuffer = true;
  20344. this.meshPerAttribute = meshPerAttribute;
  20345. }
  20346. copy( source ) {
  20347. super.copy( source );
  20348. this.meshPerAttribute = source.meshPerAttribute;
  20349. return this;
  20350. }
  20351. clone( data ) {
  20352. const ib = super.clone( data );
  20353. ib.meshPerAttribute = this.meshPerAttribute;
  20354. return ib;
  20355. }
  20356. toJSON( data ) {
  20357. const json = super.toJSON( data );
  20358. json.isInstancedInterleavedBuffer = true;
  20359. json.meshPerAttribute = this.meshPerAttribute;
  20360. return json;
  20361. }
  20362. }
  20363. class GLBufferAttribute {
  20364. constructor( buffer, type, itemSize, elementSize, count ) {
  20365. this.isGLBufferAttribute = true;
  20366. this.name = '';
  20367. this.buffer = buffer;
  20368. this.type = type;
  20369. this.itemSize = itemSize;
  20370. this.elementSize = elementSize;
  20371. this.count = count;
  20372. this.version = 0;
  20373. }
  20374. set needsUpdate( value ) {
  20375. if ( value === true ) this.version ++;
  20376. }
  20377. setBuffer( buffer ) {
  20378. this.buffer = buffer;
  20379. return this;
  20380. }
  20381. setType( type, elementSize ) {
  20382. this.type = type;
  20383. this.elementSize = elementSize;
  20384. return this;
  20385. }
  20386. setItemSize( itemSize ) {
  20387. this.itemSize = itemSize;
  20388. return this;
  20389. }
  20390. setCount( count ) {
  20391. this.count = count;
  20392. return this;
  20393. }
  20394. }
  20395. const _matrix = /*@__PURE__*/ new Matrix4();
  20396. class Raycaster {
  20397. constructor( origin, direction, near = 0, far = Infinity ) {
  20398. this.ray = new Ray( origin, direction );
  20399. // direction is assumed to be normalized (for accurate distance calculations)
  20400. this.near = near;
  20401. this.far = far;
  20402. this.camera = null;
  20403. this.layers = new Layers();
  20404. this.params = {
  20405. Mesh: {},
  20406. Line: { threshold: 1 },
  20407. LOD: {},
  20408. Points: { threshold: 1 },
  20409. Sprite: {}
  20410. };
  20411. }
  20412. set( origin, direction ) {
  20413. // direction is assumed to be normalized (for accurate distance calculations)
  20414. this.ray.set( origin, direction );
  20415. }
  20416. setFromCamera( coords, camera ) {
  20417. if ( camera.isPerspectiveCamera ) {
  20418. this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
  20419. this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
  20420. this.camera = camera;
  20421. } else if ( camera.isOrthographicCamera ) {
  20422. this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera
  20423. this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
  20424. this.camera = camera;
  20425. } else {
  20426. console.error( 'THREE.Raycaster: Unsupported camera type: ' + camera.type );
  20427. }
  20428. }
  20429. setFromXRController( controller ) {
  20430. _matrix.identity().extractRotation( controller.matrixWorld );
  20431. this.ray.origin.setFromMatrixPosition( controller.matrixWorld );
  20432. this.ray.direction.set( 0, 0, - 1 ).applyMatrix4( _matrix );
  20433. return this;
  20434. }
  20435. intersectObject( object, recursive = true, intersects = [] ) {
  20436. intersect( object, this, intersects, recursive );
  20437. intersects.sort( ascSort );
  20438. return intersects;
  20439. }
  20440. intersectObjects( objects, recursive = true, intersects = [] ) {
  20441. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  20442. intersect( objects[ i ], this, intersects, recursive );
  20443. }
  20444. intersects.sort( ascSort );
  20445. return intersects;
  20446. }
  20447. }
  20448. function ascSort( a, b ) {
  20449. return a.distance - b.distance;
  20450. }
  20451. function intersect( object, raycaster, intersects, recursive ) {
  20452. let propagate = true;
  20453. if ( object.layers.test( raycaster.layers ) ) {
  20454. const result = object.raycast( raycaster, intersects );
  20455. if ( result === false ) propagate = false;
  20456. }
  20457. if ( propagate === true && recursive === true ) {
  20458. const children = object.children;
  20459. for ( let i = 0, l = children.length; i < l; i ++ ) {
  20460. intersect( children[ i ], raycaster, intersects, true );
  20461. }
  20462. }
  20463. }
  20464. /**
  20465. * Ref: https://en.wikipedia.org/wiki/Spherical_coordinate_system
  20466. *
  20467. * phi (the polar angle) is measured from the positive y-axis. The positive y-axis is up.
  20468. * theta (the azimuthal angle) is measured from the positive z-axis.
  20469. */
  20470. class Spherical {
  20471. constructor( radius = 1, phi = 0, theta = 0 ) {
  20472. this.radius = radius;
  20473. this.phi = phi; // polar angle
  20474. this.theta = theta; // azimuthal angle
  20475. return this;
  20476. }
  20477. set( radius, phi, theta ) {
  20478. this.radius = radius;
  20479. this.phi = phi;
  20480. this.theta = theta;
  20481. return this;
  20482. }
  20483. copy( other ) {
  20484. this.radius = other.radius;
  20485. this.phi = other.phi;
  20486. this.theta = other.theta;
  20487. return this;
  20488. }
  20489. // restrict phi to be between EPS and PI-EPS
  20490. makeSafe() {
  20491. const EPS = 0.000001;
  20492. this.phi = clamp( this.phi, EPS, Math.PI - EPS );
  20493. return this;
  20494. }
  20495. setFromVector3( v ) {
  20496. return this.setFromCartesianCoords( v.x, v.y, v.z );
  20497. }
  20498. setFromCartesianCoords( x, y, z ) {
  20499. this.radius = Math.sqrt( x * x + y * y + z * z );
  20500. if ( this.radius === 0 ) {
  20501. this.theta = 0;
  20502. this.phi = 0;
  20503. } else {
  20504. this.theta = Math.atan2( x, z );
  20505. this.phi = Math.acos( clamp( y / this.radius, - 1, 1 ) );
  20506. }
  20507. return this;
  20508. }
  20509. clone() {
  20510. return new this.constructor().copy( this );
  20511. }
  20512. }
  20513. /**
  20514. * Ref: https://en.wikipedia.org/wiki/Cylindrical_coordinate_system
  20515. */
  20516. class Cylindrical {
  20517. constructor( radius = 1, theta = 0, y = 0 ) {
  20518. this.radius = radius; // distance from the origin to a point in the x-z plane
  20519. this.theta = theta; // counterclockwise angle in the x-z plane measured in radians from the positive z-axis
  20520. this.y = y; // height above the x-z plane
  20521. return this;
  20522. }
  20523. set( radius, theta, y ) {
  20524. this.radius = radius;
  20525. this.theta = theta;
  20526. this.y = y;
  20527. return this;
  20528. }
  20529. copy( other ) {
  20530. this.radius = other.radius;
  20531. this.theta = other.theta;
  20532. this.y = other.y;
  20533. return this;
  20534. }
  20535. setFromVector3( v ) {
  20536. return this.setFromCartesianCoords( v.x, v.y, v.z );
  20537. }
  20538. setFromCartesianCoords( x, y, z ) {
  20539. this.radius = Math.sqrt( x * x + z * z );
  20540. this.theta = Math.atan2( x, z );
  20541. this.y = y;
  20542. return this;
  20543. }
  20544. clone() {
  20545. return new this.constructor().copy( this );
  20546. }
  20547. }
  20548. class Matrix2 {
  20549. constructor( n11, n12, n21, n22 ) {
  20550. Matrix2.prototype.isMatrix2 = true;
  20551. this.elements = [
  20552. 1, 0,
  20553. 0, 1,
  20554. ];
  20555. if ( n11 !== undefined ) {
  20556. this.set( n11, n12, n21, n22 );
  20557. }
  20558. }
  20559. identity() {
  20560. this.set(
  20561. 1, 0,
  20562. 0, 1,
  20563. );
  20564. return this;
  20565. }
  20566. fromArray( array, offset = 0 ) {
  20567. for ( let i = 0; i < 4; i ++ ) {
  20568. this.elements[ i ] = array[ i + offset ];
  20569. }
  20570. return this;
  20571. }
  20572. set( n11, n12, n21, n22 ) {
  20573. const te = this.elements;
  20574. te[ 0 ] = n11; te[ 2 ] = n12;
  20575. te[ 1 ] = n21; te[ 3 ] = n22;
  20576. return this;
  20577. }
  20578. }
  20579. const _vector$4 = /*@__PURE__*/ new Vector2();
  20580. class Box2 {
  20581. constructor( min = new Vector2( + Infinity, + Infinity ), max = new Vector2( - Infinity, - Infinity ) ) {
  20582. this.isBox2 = true;
  20583. this.min = min;
  20584. this.max = max;
  20585. }
  20586. set( min, max ) {
  20587. this.min.copy( min );
  20588. this.max.copy( max );
  20589. return this;
  20590. }
  20591. setFromPoints( points ) {
  20592. this.makeEmpty();
  20593. for ( let i = 0, il = points.length; i < il; i ++ ) {
  20594. this.expandByPoint( points[ i ] );
  20595. }
  20596. return this;
  20597. }
  20598. setFromCenterAndSize( center, size ) {
  20599. const halfSize = _vector$4.copy( size ).multiplyScalar( 0.5 );
  20600. this.min.copy( center ).sub( halfSize );
  20601. this.max.copy( center ).add( halfSize );
  20602. return this;
  20603. }
  20604. clone() {
  20605. return new this.constructor().copy( this );
  20606. }
  20607. copy( box ) {
  20608. this.min.copy( box.min );
  20609. this.max.copy( box.max );
  20610. return this;
  20611. }
  20612. makeEmpty() {
  20613. this.min.x = this.min.y = + Infinity;
  20614. this.max.x = this.max.y = - Infinity;
  20615. return this;
  20616. }
  20617. isEmpty() {
  20618. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  20619. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
  20620. }
  20621. getCenter( target ) {
  20622. return this.isEmpty() ? target.set( 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  20623. }
  20624. getSize( target ) {
  20625. return this.isEmpty() ? target.set( 0, 0 ) : target.subVectors( this.max, this.min );
  20626. }
  20627. expandByPoint( point ) {
  20628. this.min.min( point );
  20629. this.max.max( point );
  20630. return this;
  20631. }
  20632. expandByVector( vector ) {
  20633. this.min.sub( vector );
  20634. this.max.add( vector );
  20635. return this;
  20636. }
  20637. expandByScalar( scalar ) {
  20638. this.min.addScalar( - scalar );
  20639. this.max.addScalar( scalar );
  20640. return this;
  20641. }
  20642. containsPoint( point ) {
  20643. return point.x >= this.min.x && point.x <= this.max.x &&
  20644. point.y >= this.min.y && point.y <= this.max.y;
  20645. }
  20646. containsBox( box ) {
  20647. return this.min.x <= box.min.x && box.max.x <= this.max.x &&
  20648. this.min.y <= box.min.y && box.max.y <= this.max.y;
  20649. }
  20650. getParameter( point, target ) {
  20651. // This can potentially have a divide by zero if the box
  20652. // has a size dimension of 0.
  20653. return target.set(
  20654. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  20655. ( point.y - this.min.y ) / ( this.max.y - this.min.y )
  20656. );
  20657. }
  20658. intersectsBox( box ) {
  20659. // using 4 splitting planes to rule out intersections
  20660. return box.max.x >= this.min.x && box.min.x <= this.max.x &&
  20661. box.max.y >= this.min.y && box.min.y <= this.max.y;
  20662. }
  20663. clampPoint( point, target ) {
  20664. return target.copy( point ).clamp( this.min, this.max );
  20665. }
  20666. distanceToPoint( point ) {
  20667. return this.clampPoint( point, _vector$4 ).distanceTo( point );
  20668. }
  20669. intersect( box ) {
  20670. this.min.max( box.min );
  20671. this.max.min( box.max );
  20672. if ( this.isEmpty() ) this.makeEmpty();
  20673. return this;
  20674. }
  20675. union( box ) {
  20676. this.min.min( box.min );
  20677. this.max.max( box.max );
  20678. return this;
  20679. }
  20680. translate( offset ) {
  20681. this.min.add( offset );
  20682. this.max.add( offset );
  20683. return this;
  20684. }
  20685. equals( box ) {
  20686. return box.min.equals( this.min ) && box.max.equals( this.max );
  20687. }
  20688. }
  20689. const _startP = /*@__PURE__*/ new Vector3();
  20690. const _startEnd = /*@__PURE__*/ new Vector3();
  20691. class Line3 {
  20692. constructor( start = new Vector3(), end = new Vector3() ) {
  20693. this.start = start;
  20694. this.end = end;
  20695. }
  20696. set( start, end ) {
  20697. this.start.copy( start );
  20698. this.end.copy( end );
  20699. return this;
  20700. }
  20701. copy( line ) {
  20702. this.start.copy( line.start );
  20703. this.end.copy( line.end );
  20704. return this;
  20705. }
  20706. getCenter( target ) {
  20707. return target.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
  20708. }
  20709. delta( target ) {
  20710. return target.subVectors( this.end, this.start );
  20711. }
  20712. distanceSq() {
  20713. return this.start.distanceToSquared( this.end );
  20714. }
  20715. distance() {
  20716. return this.start.distanceTo( this.end );
  20717. }
  20718. at( t, target ) {
  20719. return this.delta( target ).multiplyScalar( t ).add( this.start );
  20720. }
  20721. closestPointToPointParameter( point, clampToLine ) {
  20722. _startP.subVectors( point, this.start );
  20723. _startEnd.subVectors( this.end, this.start );
  20724. const startEnd2 = _startEnd.dot( _startEnd );
  20725. const startEnd_startP = _startEnd.dot( _startP );
  20726. let t = startEnd_startP / startEnd2;
  20727. if ( clampToLine ) {
  20728. t = clamp( t, 0, 1 );
  20729. }
  20730. return t;
  20731. }
  20732. closestPointToPoint( point, clampToLine, target ) {
  20733. const t = this.closestPointToPointParameter( point, clampToLine );
  20734. return this.delta( target ).multiplyScalar( t ).add( this.start );
  20735. }
  20736. applyMatrix4( matrix ) {
  20737. this.start.applyMatrix4( matrix );
  20738. this.end.applyMatrix4( matrix );
  20739. return this;
  20740. }
  20741. equals( line ) {
  20742. return line.start.equals( this.start ) && line.end.equals( this.end );
  20743. }
  20744. clone() {
  20745. return new this.constructor().copy( this );
  20746. }
  20747. }
  20748. const _vector$3 = /*@__PURE__*/ new Vector3();
  20749. class SpotLightHelper extends Object3D {
  20750. constructor( light, color ) {
  20751. super();
  20752. this.light = light;
  20753. this.matrixAutoUpdate = false;
  20754. this.color = color;
  20755. this.type = 'SpotLightHelper';
  20756. const geometry = new BufferGeometry();
  20757. const positions = [
  20758. 0, 0, 0, 0, 0, 1,
  20759. 0, 0, 0, 1, 0, 1,
  20760. 0, 0, 0, - 1, 0, 1,
  20761. 0, 0, 0, 0, 1, 1,
  20762. 0, 0, 0, 0, - 1, 1
  20763. ];
  20764. for ( let i = 0, j = 1, l = 32; i < l; i ++, j ++ ) {
  20765. const p1 = ( i / l ) * Math.PI * 2;
  20766. const p2 = ( j / l ) * Math.PI * 2;
  20767. positions.push(
  20768. Math.cos( p1 ), Math.sin( p1 ), 1,
  20769. Math.cos( p2 ), Math.sin( p2 ), 1
  20770. );
  20771. }
  20772. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  20773. const material = new LineBasicMaterial( { fog: false, toneMapped: false } );
  20774. this.cone = new LineSegments( geometry, material );
  20775. this.add( this.cone );
  20776. this.update();
  20777. }
  20778. dispose() {
  20779. this.cone.geometry.dispose();
  20780. this.cone.material.dispose();
  20781. }
  20782. update() {
  20783. this.light.updateWorldMatrix( true, false );
  20784. this.light.target.updateWorldMatrix( true, false );
  20785. // update the local matrix based on the parent and light target transforms
  20786. if ( this.parent ) {
  20787. this.parent.updateWorldMatrix( true );
  20788. this.matrix
  20789. .copy( this.parent.matrixWorld )
  20790. .invert()
  20791. .multiply( this.light.matrixWorld );
  20792. } else {
  20793. this.matrix.copy( this.light.matrixWorld );
  20794. }
  20795. this.matrixWorld.copy( this.light.matrixWorld );
  20796. const coneLength = this.light.distance ? this.light.distance : 1000;
  20797. const coneWidth = coneLength * Math.tan( this.light.angle );
  20798. this.cone.scale.set( coneWidth, coneWidth, coneLength );
  20799. _vector$3.setFromMatrixPosition( this.light.target.matrixWorld );
  20800. this.cone.lookAt( _vector$3 );
  20801. if ( this.color !== undefined ) {
  20802. this.cone.material.color.set( this.color );
  20803. } else {
  20804. this.cone.material.color.copy( this.light.color );
  20805. }
  20806. }
  20807. }
  20808. const _vector$2 = /*@__PURE__*/ new Vector3();
  20809. const _boneMatrix = /*@__PURE__*/ new Matrix4();
  20810. const _matrixWorldInv = /*@__PURE__*/ new Matrix4();
  20811. class SkeletonHelper extends LineSegments {
  20812. constructor( object ) {
  20813. const bones = getBoneList( object );
  20814. const geometry = new BufferGeometry();
  20815. const vertices = [];
  20816. const colors = [];
  20817. const color1 = new Color( 0, 0, 1 );
  20818. const color2 = new Color( 0, 1, 0 );
  20819. for ( let i = 0; i < bones.length; i ++ ) {
  20820. const bone = bones[ i ];
  20821. if ( bone.parent && bone.parent.isBone ) {
  20822. vertices.push( 0, 0, 0 );
  20823. vertices.push( 0, 0, 0 );
  20824. colors.push( color1.r, color1.g, color1.b );
  20825. colors.push( color2.r, color2.g, color2.b );
  20826. }
  20827. }
  20828. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  20829. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  20830. const material = new LineBasicMaterial( { vertexColors: true, depthTest: false, depthWrite: false, toneMapped: false, transparent: true } );
  20831. super( geometry, material );
  20832. this.isSkeletonHelper = true;
  20833. this.type = 'SkeletonHelper';
  20834. this.root = object;
  20835. this.bones = bones;
  20836. this.matrix = object.matrixWorld;
  20837. this.matrixAutoUpdate = false;
  20838. }
  20839. updateMatrixWorld( force ) {
  20840. const bones = this.bones;
  20841. const geometry = this.geometry;
  20842. const position = geometry.getAttribute( 'position' );
  20843. _matrixWorldInv.copy( this.root.matrixWorld ).invert();
  20844. for ( let i = 0, j = 0; i < bones.length; i ++ ) {
  20845. const bone = bones[ i ];
  20846. if ( bone.parent && bone.parent.isBone ) {
  20847. _boneMatrix.multiplyMatrices( _matrixWorldInv, bone.matrixWorld );
  20848. _vector$2.setFromMatrixPosition( _boneMatrix );
  20849. position.setXYZ( j, _vector$2.x, _vector$2.y, _vector$2.z );
  20850. _boneMatrix.multiplyMatrices( _matrixWorldInv, bone.parent.matrixWorld );
  20851. _vector$2.setFromMatrixPosition( _boneMatrix );
  20852. position.setXYZ( j + 1, _vector$2.x, _vector$2.y, _vector$2.z );
  20853. j += 2;
  20854. }
  20855. }
  20856. geometry.getAttribute( 'position' ).needsUpdate = true;
  20857. super.updateMatrixWorld( force );
  20858. }
  20859. dispose() {
  20860. this.geometry.dispose();
  20861. this.material.dispose();
  20862. }
  20863. }
  20864. function getBoneList( object ) {
  20865. const boneList = [];
  20866. if ( object.isBone === true ) {
  20867. boneList.push( object );
  20868. }
  20869. for ( let i = 0; i < object.children.length; i ++ ) {
  20870. boneList.push.apply( boneList, getBoneList( object.children[ i ] ) );
  20871. }
  20872. return boneList;
  20873. }
  20874. class PointLightHelper extends Mesh {
  20875. constructor( light, sphereSize, color ) {
  20876. const geometry = new SphereGeometry( sphereSize, 4, 2 );
  20877. const material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
  20878. super( geometry, material );
  20879. this.light = light;
  20880. this.color = color;
  20881. this.type = 'PointLightHelper';
  20882. this.matrix = this.light.matrixWorld;
  20883. this.matrixAutoUpdate = false;
  20884. this.update();
  20885. /*
  20886. // TODO: delete this comment?
  20887. const distanceGeometry = new THREE.IcosahedronGeometry( 1, 2 );
  20888. const distanceMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false, wireframe: true, opacity: 0.1, transparent: true } );
  20889. this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial );
  20890. this.lightDistance = new THREE.Mesh( distanceGeometry, distanceMaterial );
  20891. const d = light.distance;
  20892. if ( d === 0.0 ) {
  20893. this.lightDistance.visible = false;
  20894. } else {
  20895. this.lightDistance.scale.set( d, d, d );
  20896. }
  20897. this.add( this.lightDistance );
  20898. */
  20899. }
  20900. dispose() {
  20901. this.geometry.dispose();
  20902. this.material.dispose();
  20903. }
  20904. update() {
  20905. this.light.updateWorldMatrix( true, false );
  20906. if ( this.color !== undefined ) {
  20907. this.material.color.set( this.color );
  20908. } else {
  20909. this.material.color.copy( this.light.color );
  20910. }
  20911. /*
  20912. const d = this.light.distance;
  20913. if ( d === 0.0 ) {
  20914. this.lightDistance.visible = false;
  20915. } else {
  20916. this.lightDistance.visible = true;
  20917. this.lightDistance.scale.set( d, d, d );
  20918. }
  20919. */
  20920. }
  20921. }
  20922. const _vector$1 = /*@__PURE__*/ new Vector3();
  20923. const _color1 = /*@__PURE__*/ new Color();
  20924. const _color2 = /*@__PURE__*/ new Color();
  20925. class HemisphereLightHelper extends Object3D {
  20926. constructor( light, size, color ) {
  20927. super();
  20928. this.light = light;
  20929. this.matrix = light.matrixWorld;
  20930. this.matrixAutoUpdate = false;
  20931. this.color = color;
  20932. this.type = 'HemisphereLightHelper';
  20933. const geometry = new OctahedronGeometry( size );
  20934. geometry.rotateY( Math.PI * 0.5 );
  20935. this.material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
  20936. if ( this.color === undefined ) this.material.vertexColors = true;
  20937. const position = geometry.getAttribute( 'position' );
  20938. const colors = new Float32Array( position.count * 3 );
  20939. geometry.setAttribute( 'color', new BufferAttribute( colors, 3 ) );
  20940. this.add( new Mesh( geometry, this.material ) );
  20941. this.update();
  20942. }
  20943. dispose() {
  20944. this.children[ 0 ].geometry.dispose();
  20945. this.children[ 0 ].material.dispose();
  20946. }
  20947. update() {
  20948. const mesh = this.children[ 0 ];
  20949. if ( this.color !== undefined ) {
  20950. this.material.color.set( this.color );
  20951. } else {
  20952. const colors = mesh.geometry.getAttribute( 'color' );
  20953. _color1.copy( this.light.color );
  20954. _color2.copy( this.light.groundColor );
  20955. for ( let i = 0, l = colors.count; i < l; i ++ ) {
  20956. const color = ( i < ( l / 2 ) ) ? _color1 : _color2;
  20957. colors.setXYZ( i, color.r, color.g, color.b );
  20958. }
  20959. colors.needsUpdate = true;
  20960. }
  20961. this.light.updateWorldMatrix( true, false );
  20962. mesh.lookAt( _vector$1.setFromMatrixPosition( this.light.matrixWorld ).negate() );
  20963. }
  20964. }
  20965. class GridHelper extends LineSegments {
  20966. constructor( size = 10, divisions = 10, color1 = 0x444444, color2 = 0x888888 ) {
  20967. color1 = new Color( color1 );
  20968. color2 = new Color( color2 );
  20969. const center = divisions / 2;
  20970. const step = size / divisions;
  20971. const halfSize = size / 2;
  20972. const vertices = [], colors = [];
  20973. for ( let i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) {
  20974. vertices.push( - halfSize, 0, k, halfSize, 0, k );
  20975. vertices.push( k, 0, - halfSize, k, 0, halfSize );
  20976. const color = i === center ? color1 : color2;
  20977. color.toArray( colors, j ); j += 3;
  20978. color.toArray( colors, j ); j += 3;
  20979. color.toArray( colors, j ); j += 3;
  20980. color.toArray( colors, j ); j += 3;
  20981. }
  20982. const geometry = new BufferGeometry();
  20983. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  20984. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  20985. const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
  20986. super( geometry, material );
  20987. this.type = 'GridHelper';
  20988. }
  20989. dispose() {
  20990. this.geometry.dispose();
  20991. this.material.dispose();
  20992. }
  20993. }
  20994. class PolarGridHelper extends LineSegments {
  20995. constructor( radius = 10, sectors = 16, rings = 8, divisions = 64, color1 = 0x444444, color2 = 0x888888 ) {
  20996. color1 = new Color( color1 );
  20997. color2 = new Color( color2 );
  20998. const vertices = [];
  20999. const colors = [];
  21000. // create the sectors
  21001. if ( sectors > 1 ) {
  21002. for ( let i = 0; i < sectors; i ++ ) {
  21003. const v = ( i / sectors ) * ( Math.PI * 2 );
  21004. const x = Math.sin( v ) * radius;
  21005. const z = Math.cos( v ) * radius;
  21006. vertices.push( 0, 0, 0 );
  21007. vertices.push( x, 0, z );
  21008. const color = ( i & 1 ) ? color1 : color2;
  21009. colors.push( color.r, color.g, color.b );
  21010. colors.push( color.r, color.g, color.b );
  21011. }
  21012. }
  21013. // create the rings
  21014. for ( let i = 0; i < rings; i ++ ) {
  21015. const color = ( i & 1 ) ? color1 : color2;
  21016. const r = radius - ( radius / rings * i );
  21017. for ( let j = 0; j < divisions; j ++ ) {
  21018. // first vertex
  21019. let v = ( j / divisions ) * ( Math.PI * 2 );
  21020. let x = Math.sin( v ) * r;
  21021. let z = Math.cos( v ) * r;
  21022. vertices.push( x, 0, z );
  21023. colors.push( color.r, color.g, color.b );
  21024. // second vertex
  21025. v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 );
  21026. x = Math.sin( v ) * r;
  21027. z = Math.cos( v ) * r;
  21028. vertices.push( x, 0, z );
  21029. colors.push( color.r, color.g, color.b );
  21030. }
  21031. }
  21032. const geometry = new BufferGeometry();
  21033. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  21034. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  21035. const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
  21036. super( geometry, material );
  21037. this.type = 'PolarGridHelper';
  21038. }
  21039. dispose() {
  21040. this.geometry.dispose();
  21041. this.material.dispose();
  21042. }
  21043. }
  21044. const _v1 = /*@__PURE__*/ new Vector3();
  21045. const _v2 = /*@__PURE__*/ new Vector3();
  21046. const _v3 = /*@__PURE__*/ new Vector3();
  21047. class DirectionalLightHelper extends Object3D {
  21048. constructor( light, size, color ) {
  21049. super();
  21050. this.light = light;
  21051. this.matrix = light.matrixWorld;
  21052. this.matrixAutoUpdate = false;
  21053. this.color = color;
  21054. this.type = 'DirectionalLightHelper';
  21055. if ( size === undefined ) size = 1;
  21056. let geometry = new BufferGeometry();
  21057. geometry.setAttribute( 'position', new Float32BufferAttribute( [
  21058. - size, size, 0,
  21059. size, size, 0,
  21060. size, - size, 0,
  21061. - size, - size, 0,
  21062. - size, size, 0
  21063. ], 3 ) );
  21064. const material = new LineBasicMaterial( { fog: false, toneMapped: false } );
  21065. this.lightPlane = new Line( geometry, material );
  21066. this.add( this.lightPlane );
  21067. geometry = new BufferGeometry();
  21068. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  21069. this.targetLine = new Line( geometry, material );
  21070. this.add( this.targetLine );
  21071. this.update();
  21072. }
  21073. dispose() {
  21074. this.lightPlane.geometry.dispose();
  21075. this.lightPlane.material.dispose();
  21076. this.targetLine.geometry.dispose();
  21077. this.targetLine.material.dispose();
  21078. }
  21079. update() {
  21080. this.light.updateWorldMatrix( true, false );
  21081. this.light.target.updateWorldMatrix( true, false );
  21082. _v1.setFromMatrixPosition( this.light.matrixWorld );
  21083. _v2.setFromMatrixPosition( this.light.target.matrixWorld );
  21084. _v3.subVectors( _v2, _v1 );
  21085. this.lightPlane.lookAt( _v2 );
  21086. if ( this.color !== undefined ) {
  21087. this.lightPlane.material.color.set( this.color );
  21088. this.targetLine.material.color.set( this.color );
  21089. } else {
  21090. this.lightPlane.material.color.copy( this.light.color );
  21091. this.targetLine.material.color.copy( this.light.color );
  21092. }
  21093. this.targetLine.lookAt( _v2 );
  21094. this.targetLine.scale.z = _v3.length();
  21095. }
  21096. }
  21097. const _vector = /*@__PURE__*/ new Vector3();
  21098. const _camera = /*@__PURE__*/ new Camera();
  21099. /**
  21100. * - shows frustum, line of sight and up of the camera
  21101. * - suitable for fast updates
  21102. * - based on frustum visualization in lightgl.js shadowmap example
  21103. * https://github.com/evanw/lightgl.js/blob/master/tests/shadowmap.html
  21104. */
  21105. class CameraHelper extends LineSegments {
  21106. constructor( camera ) {
  21107. const geometry = new BufferGeometry();
  21108. const material = new LineBasicMaterial( { color: 0xffffff, vertexColors: true, toneMapped: false } );
  21109. const vertices = [];
  21110. const colors = [];
  21111. const pointMap = {};
  21112. // near
  21113. addLine( 'n1', 'n2' );
  21114. addLine( 'n2', 'n4' );
  21115. addLine( 'n4', 'n3' );
  21116. addLine( 'n3', 'n1' );
  21117. // far
  21118. addLine( 'f1', 'f2' );
  21119. addLine( 'f2', 'f4' );
  21120. addLine( 'f4', 'f3' );
  21121. addLine( 'f3', 'f1' );
  21122. // sides
  21123. addLine( 'n1', 'f1' );
  21124. addLine( 'n2', 'f2' );
  21125. addLine( 'n3', 'f3' );
  21126. addLine( 'n4', 'f4' );
  21127. // cone
  21128. addLine( 'p', 'n1' );
  21129. addLine( 'p', 'n2' );
  21130. addLine( 'p', 'n3' );
  21131. addLine( 'p', 'n4' );
  21132. // up
  21133. addLine( 'u1', 'u2' );
  21134. addLine( 'u2', 'u3' );
  21135. addLine( 'u3', 'u1' );
  21136. // target
  21137. addLine( 'c', 't' );
  21138. addLine( 'p', 'c' );
  21139. // cross
  21140. addLine( 'cn1', 'cn2' );
  21141. addLine( 'cn3', 'cn4' );
  21142. addLine( 'cf1', 'cf2' );
  21143. addLine( 'cf3', 'cf4' );
  21144. function addLine( a, b ) {
  21145. addPoint( a );
  21146. addPoint( b );
  21147. }
  21148. function addPoint( id ) {
  21149. vertices.push( 0, 0, 0 );
  21150. colors.push( 0, 0, 0 );
  21151. if ( pointMap[ id ] === undefined ) {
  21152. pointMap[ id ] = [];
  21153. }
  21154. pointMap[ id ].push( ( vertices.length / 3 ) - 1 );
  21155. }
  21156. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  21157. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  21158. super( geometry, material );
  21159. this.type = 'CameraHelper';
  21160. this.camera = camera;
  21161. if ( this.camera.updateProjectionMatrix ) this.camera.updateProjectionMatrix();
  21162. this.matrix = camera.matrixWorld;
  21163. this.matrixAutoUpdate = false;
  21164. this.pointMap = pointMap;
  21165. this.update();
  21166. // colors
  21167. const colorFrustum = new Color( 0xffaa00 );
  21168. const colorCone = new Color( 0xff0000 );
  21169. const colorUp = new Color( 0x00aaff );
  21170. const colorTarget = new Color( 0xffffff );
  21171. const colorCross = new Color( 0x333333 );
  21172. this.setColors( colorFrustum, colorCone, colorUp, colorTarget, colorCross );
  21173. }
  21174. setColors( frustum, cone, up, target, cross ) {
  21175. const geometry = this.geometry;
  21176. const colorAttribute = geometry.getAttribute( 'color' );
  21177. // near
  21178. colorAttribute.setXYZ( 0, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 1, frustum.r, frustum.g, frustum.b ); // n1, n2
  21179. colorAttribute.setXYZ( 2, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 3, frustum.r, frustum.g, frustum.b ); // n2, n4
  21180. colorAttribute.setXYZ( 4, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 5, frustum.r, frustum.g, frustum.b ); // n4, n3
  21181. colorAttribute.setXYZ( 6, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 7, frustum.r, frustum.g, frustum.b ); // n3, n1
  21182. // far
  21183. colorAttribute.setXYZ( 8, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 9, frustum.r, frustum.g, frustum.b ); // f1, f2
  21184. colorAttribute.setXYZ( 10, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 11, frustum.r, frustum.g, frustum.b ); // f2, f4
  21185. colorAttribute.setXYZ( 12, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 13, frustum.r, frustum.g, frustum.b ); // f4, f3
  21186. colorAttribute.setXYZ( 14, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 15, frustum.r, frustum.g, frustum.b ); // f3, f1
  21187. // sides
  21188. colorAttribute.setXYZ( 16, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 17, frustum.r, frustum.g, frustum.b ); // n1, f1
  21189. colorAttribute.setXYZ( 18, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 19, frustum.r, frustum.g, frustum.b ); // n2, f2
  21190. colorAttribute.setXYZ( 20, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 21, frustum.r, frustum.g, frustum.b ); // n3, f3
  21191. colorAttribute.setXYZ( 22, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 23, frustum.r, frustum.g, frustum.b ); // n4, f4
  21192. // cone
  21193. colorAttribute.setXYZ( 24, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 25, cone.r, cone.g, cone.b ); // p, n1
  21194. colorAttribute.setXYZ( 26, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 27, cone.r, cone.g, cone.b ); // p, n2
  21195. colorAttribute.setXYZ( 28, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 29, cone.r, cone.g, cone.b ); // p, n3
  21196. colorAttribute.setXYZ( 30, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 31, cone.r, cone.g, cone.b ); // p, n4
  21197. // up
  21198. colorAttribute.setXYZ( 32, up.r, up.g, up.b ); colorAttribute.setXYZ( 33, up.r, up.g, up.b ); // u1, u2
  21199. colorAttribute.setXYZ( 34, up.r, up.g, up.b ); colorAttribute.setXYZ( 35, up.r, up.g, up.b ); // u2, u3
  21200. colorAttribute.setXYZ( 36, up.r, up.g, up.b ); colorAttribute.setXYZ( 37, up.r, up.g, up.b ); // u3, u1
  21201. // target
  21202. colorAttribute.setXYZ( 38, target.r, target.g, target.b ); colorAttribute.setXYZ( 39, target.r, target.g, target.b ); // c, t
  21203. colorAttribute.setXYZ( 40, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 41, cross.r, cross.g, cross.b ); // p, c
  21204. // cross
  21205. colorAttribute.setXYZ( 42, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 43, cross.r, cross.g, cross.b ); // cn1, cn2
  21206. colorAttribute.setXYZ( 44, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 45, cross.r, cross.g, cross.b ); // cn3, cn4
  21207. colorAttribute.setXYZ( 46, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 47, cross.r, cross.g, cross.b ); // cf1, cf2
  21208. colorAttribute.setXYZ( 48, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 49, cross.r, cross.g, cross.b ); // cf3, cf4
  21209. colorAttribute.needsUpdate = true;
  21210. }
  21211. update() {
  21212. const geometry = this.geometry;
  21213. const pointMap = this.pointMap;
  21214. const w = 1, h = 1;
  21215. // we need just camera projection matrix inverse
  21216. // world matrix must be identity
  21217. _camera.projectionMatrixInverse.copy( this.camera.projectionMatrixInverse );
  21218. // Adjust z values based on coordinate system
  21219. const nearZ = this.camera.coordinateSystem === WebGLCoordinateSystem ? - 1 : 0;
  21220. // center / target
  21221. setPoint( 'c', pointMap, geometry, _camera, 0, 0, nearZ );
  21222. setPoint( 't', pointMap, geometry, _camera, 0, 0, 1 );
  21223. // near
  21224. setPoint( 'n1', pointMap, geometry, _camera, - w, - h, nearZ );
  21225. setPoint( 'n2', pointMap, geometry, _camera, w, - h, nearZ );
  21226. setPoint( 'n3', pointMap, geometry, _camera, - w, h, nearZ );
  21227. setPoint( 'n4', pointMap, geometry, _camera, w, h, nearZ );
  21228. // far
  21229. setPoint( 'f1', pointMap, geometry, _camera, - w, - h, 1 );
  21230. setPoint( 'f2', pointMap, geometry, _camera, w, - h, 1 );
  21231. setPoint( 'f3', pointMap, geometry, _camera, - w, h, 1 );
  21232. setPoint( 'f4', pointMap, geometry, _camera, w, h, 1 );
  21233. // up
  21234. setPoint( 'u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, nearZ );
  21235. setPoint( 'u2', pointMap, geometry, _camera, - w * 0.7, h * 1.1, nearZ );
  21236. setPoint( 'u3', pointMap, geometry, _camera, 0, h * 2, nearZ );
  21237. // cross
  21238. setPoint( 'cf1', pointMap, geometry, _camera, - w, 0, 1 );
  21239. setPoint( 'cf2', pointMap, geometry, _camera, w, 0, 1 );
  21240. setPoint( 'cf3', pointMap, geometry, _camera, 0, - h, 1 );
  21241. setPoint( 'cf4', pointMap, geometry, _camera, 0, h, 1 );
  21242. setPoint( 'cn1', pointMap, geometry, _camera, - w, 0, nearZ );
  21243. setPoint( 'cn2', pointMap, geometry, _camera, w, 0, nearZ );
  21244. setPoint( 'cn3', pointMap, geometry, _camera, 0, - h, nearZ );
  21245. setPoint( 'cn4', pointMap, geometry, _camera, 0, h, nearZ );
  21246. geometry.getAttribute( 'position' ).needsUpdate = true;
  21247. }
  21248. dispose() {
  21249. this.geometry.dispose();
  21250. this.material.dispose();
  21251. }
  21252. }
  21253. function setPoint( point, pointMap, geometry, camera, x, y, z ) {
  21254. _vector.set( x, y, z ).unproject( camera );
  21255. const points = pointMap[ point ];
  21256. if ( points !== undefined ) {
  21257. const position = geometry.getAttribute( 'position' );
  21258. for ( let i = 0, l = points.length; i < l; i ++ ) {
  21259. position.setXYZ( points[ i ], _vector.x, _vector.y, _vector.z );
  21260. }
  21261. }
  21262. }
  21263. const _box = /*@__PURE__*/ new Box3();
  21264. class BoxHelper extends LineSegments {
  21265. constructor( object, color = 0xffff00 ) {
  21266. const indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] );
  21267. const positions = new Float32Array( 8 * 3 );
  21268. const geometry = new BufferGeometry();
  21269. geometry.setIndex( new BufferAttribute( indices, 1 ) );
  21270. geometry.setAttribute( 'position', new BufferAttribute( positions, 3 ) );
  21271. super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  21272. this.object = object;
  21273. this.type = 'BoxHelper';
  21274. this.matrixAutoUpdate = false;
  21275. this.update();
  21276. }
  21277. update( object ) {
  21278. if ( object !== undefined ) {
  21279. console.warn( 'THREE.BoxHelper: .update() has no longer arguments.' );
  21280. }
  21281. if ( this.object !== undefined ) {
  21282. _box.setFromObject( this.object );
  21283. }
  21284. if ( _box.isEmpty() ) return;
  21285. const min = _box.min;
  21286. const max = _box.max;
  21287. /*
  21288. 5____4
  21289. 1/___0/|
  21290. | 6__|_7
  21291. 2/___3/
  21292. 0: max.x, max.y, max.z
  21293. 1: min.x, max.y, max.z
  21294. 2: min.x, min.y, max.z
  21295. 3: max.x, min.y, max.z
  21296. 4: max.x, max.y, min.z
  21297. 5: min.x, max.y, min.z
  21298. 6: min.x, min.y, min.z
  21299. 7: max.x, min.y, min.z
  21300. */
  21301. const position = this.geometry.attributes.position;
  21302. const array = position.array;
  21303. array[ 0 ] = max.x; array[ 1 ] = max.y; array[ 2 ] = max.z;
  21304. array[ 3 ] = min.x; array[ 4 ] = max.y; array[ 5 ] = max.z;
  21305. array[ 6 ] = min.x; array[ 7 ] = min.y; array[ 8 ] = max.z;
  21306. array[ 9 ] = max.x; array[ 10 ] = min.y; array[ 11 ] = max.z;
  21307. array[ 12 ] = max.x; array[ 13 ] = max.y; array[ 14 ] = min.z;
  21308. array[ 15 ] = min.x; array[ 16 ] = max.y; array[ 17 ] = min.z;
  21309. array[ 18 ] = min.x; array[ 19 ] = min.y; array[ 20 ] = min.z;
  21310. array[ 21 ] = max.x; array[ 22 ] = min.y; array[ 23 ] = min.z;
  21311. position.needsUpdate = true;
  21312. this.geometry.computeBoundingSphere();
  21313. }
  21314. setFromObject( object ) {
  21315. this.object = object;
  21316. this.update();
  21317. return this;
  21318. }
  21319. copy( source, recursive ) {
  21320. super.copy( source, recursive );
  21321. this.object = source.object;
  21322. return this;
  21323. }
  21324. dispose() {
  21325. this.geometry.dispose();
  21326. this.material.dispose();
  21327. }
  21328. }
  21329. class Box3Helper extends LineSegments {
  21330. constructor( box, color = 0xffff00 ) {
  21331. const indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] );
  21332. const positions = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 1, - 1, 1, - 1, - 1 ];
  21333. const geometry = new BufferGeometry();
  21334. geometry.setIndex( new BufferAttribute( indices, 1 ) );
  21335. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  21336. super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  21337. this.box = box;
  21338. this.type = 'Box3Helper';
  21339. this.geometry.computeBoundingSphere();
  21340. }
  21341. updateMatrixWorld( force ) {
  21342. const box = this.box;
  21343. if ( box.isEmpty() ) return;
  21344. box.getCenter( this.position );
  21345. box.getSize( this.scale );
  21346. this.scale.multiplyScalar( 0.5 );
  21347. super.updateMatrixWorld( force );
  21348. }
  21349. dispose() {
  21350. this.geometry.dispose();
  21351. this.material.dispose();
  21352. }
  21353. }
  21354. class PlaneHelper extends Line {
  21355. constructor( plane, size = 1, hex = 0xffff00 ) {
  21356. const color = hex;
  21357. const positions = [ 1, - 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, - 1, 0, 1, 1, 0 ];
  21358. const geometry = new BufferGeometry();
  21359. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  21360. geometry.computeBoundingSphere();
  21361. super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  21362. this.type = 'PlaneHelper';
  21363. this.plane = plane;
  21364. this.size = size;
  21365. const positions2 = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, - 1, 0, 1, - 1, 0 ];
  21366. const geometry2 = new BufferGeometry();
  21367. geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
  21368. geometry2.computeBoundingSphere();
  21369. this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false, toneMapped: false } ) ) );
  21370. }
  21371. updateMatrixWorld( force ) {
  21372. this.position.set( 0, 0, 0 );
  21373. this.scale.set( 0.5 * this.size, 0.5 * this.size, 1 );
  21374. this.lookAt( this.plane.normal );
  21375. this.translateZ( - this.plane.constant );
  21376. super.updateMatrixWorld( force );
  21377. }
  21378. dispose() {
  21379. this.geometry.dispose();
  21380. this.material.dispose();
  21381. this.children[ 0 ].geometry.dispose();
  21382. this.children[ 0 ].material.dispose();
  21383. }
  21384. }
  21385. const _axis = /*@__PURE__*/ new Vector3();
  21386. let _lineGeometry, _coneGeometry;
  21387. class ArrowHelper extends Object3D {
  21388. // dir is assumed to be normalized
  21389. constructor( dir = new Vector3( 0, 0, 1 ), origin = new Vector3( 0, 0, 0 ), length = 1, color = 0xffff00, headLength = length * 0.2, headWidth = headLength * 0.2 ) {
  21390. super();
  21391. this.type = 'ArrowHelper';
  21392. if ( _lineGeometry === undefined ) {
  21393. _lineGeometry = new BufferGeometry();
  21394. _lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
  21395. _coneGeometry = new CylinderGeometry( 0, 0.5, 1, 5, 1 );
  21396. _coneGeometry.translate( 0, - 0.5, 0 );
  21397. }
  21398. this.position.copy( origin );
  21399. this.line = new Line( _lineGeometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  21400. this.line.matrixAutoUpdate = false;
  21401. this.add( this.line );
  21402. this.cone = new Mesh( _coneGeometry, new MeshBasicMaterial( { color: color, toneMapped: false } ) );
  21403. this.cone.matrixAutoUpdate = false;
  21404. this.add( this.cone );
  21405. this.setDirection( dir );
  21406. this.setLength( length, headLength, headWidth );
  21407. }
  21408. setDirection( dir ) {
  21409. // dir is assumed to be normalized
  21410. if ( dir.y > 0.99999 ) {
  21411. this.quaternion.set( 0, 0, 0, 1 );
  21412. } else if ( dir.y < - 0.99999 ) {
  21413. this.quaternion.set( 1, 0, 0, 0 );
  21414. } else {
  21415. _axis.set( dir.z, 0, - dir.x ).normalize();
  21416. const radians = Math.acos( dir.y );
  21417. this.quaternion.setFromAxisAngle( _axis, radians );
  21418. }
  21419. }
  21420. setLength( length, headLength = length * 0.2, headWidth = headLength * 0.2 ) {
  21421. this.line.scale.set( 1, Math.max( 0.0001, length - headLength ), 1 ); // see #17458
  21422. this.line.updateMatrix();
  21423. this.cone.scale.set( headWidth, headLength, headWidth );
  21424. this.cone.position.y = length;
  21425. this.cone.updateMatrix();
  21426. }
  21427. setColor( color ) {
  21428. this.line.material.color.set( color );
  21429. this.cone.material.color.set( color );
  21430. }
  21431. copy( source ) {
  21432. super.copy( source, false );
  21433. this.line.copy( source.line );
  21434. this.cone.copy( source.cone );
  21435. return this;
  21436. }
  21437. dispose() {
  21438. this.line.geometry.dispose();
  21439. this.line.material.dispose();
  21440. this.cone.geometry.dispose();
  21441. this.cone.material.dispose();
  21442. }
  21443. }
  21444. class AxesHelper extends LineSegments {
  21445. constructor( size = 1 ) {
  21446. const vertices = [
  21447. 0, 0, 0, size, 0, 0,
  21448. 0, 0, 0, 0, size, 0,
  21449. 0, 0, 0, 0, 0, size
  21450. ];
  21451. const colors = [
  21452. 1, 0, 0, 1, 0.6, 0,
  21453. 0, 1, 0, 0.6, 1, 0,
  21454. 0, 0, 1, 0, 0.6, 1
  21455. ];
  21456. const geometry = new BufferGeometry();
  21457. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  21458. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  21459. const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
  21460. super( geometry, material );
  21461. this.type = 'AxesHelper';
  21462. }
  21463. setColors( xAxisColor, yAxisColor, zAxisColor ) {
  21464. const color = new Color();
  21465. const array = this.geometry.attributes.color.array;
  21466. color.set( xAxisColor );
  21467. color.toArray( array, 0 );
  21468. color.toArray( array, 3 );
  21469. color.set( yAxisColor );
  21470. color.toArray( array, 6 );
  21471. color.toArray( array, 9 );
  21472. color.set( zAxisColor );
  21473. color.toArray( array, 12 );
  21474. color.toArray( array, 15 );
  21475. this.geometry.attributes.color.needsUpdate = true;
  21476. return this;
  21477. }
  21478. dispose() {
  21479. this.geometry.dispose();
  21480. this.material.dispose();
  21481. }
  21482. }
  21483. class ShapePath {
  21484. constructor() {
  21485. this.type = 'ShapePath';
  21486. this.color = new Color();
  21487. this.subPaths = [];
  21488. this.currentPath = null;
  21489. }
  21490. moveTo( x, y ) {
  21491. this.currentPath = new Path();
  21492. this.subPaths.push( this.currentPath );
  21493. this.currentPath.moveTo( x, y );
  21494. return this;
  21495. }
  21496. lineTo( x, y ) {
  21497. this.currentPath.lineTo( x, y );
  21498. return this;
  21499. }
  21500. quadraticCurveTo( aCPx, aCPy, aX, aY ) {
  21501. this.currentPath.quadraticCurveTo( aCPx, aCPy, aX, aY );
  21502. return this;
  21503. }
  21504. bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
  21505. this.currentPath.bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY );
  21506. return this;
  21507. }
  21508. splineThru( pts ) {
  21509. this.currentPath.splineThru( pts );
  21510. return this;
  21511. }
  21512. toShapes( isCCW ) {
  21513. function toShapesNoHoles( inSubpaths ) {
  21514. const shapes = [];
  21515. for ( let i = 0, l = inSubpaths.length; i < l; i ++ ) {
  21516. const tmpPath = inSubpaths[ i ];
  21517. const tmpShape = new Shape();
  21518. tmpShape.curves = tmpPath.curves;
  21519. shapes.push( tmpShape );
  21520. }
  21521. return shapes;
  21522. }
  21523. function isPointInsidePolygon( inPt, inPolygon ) {
  21524. const polyLen = inPolygon.length;
  21525. // inPt on polygon contour => immediate success or
  21526. // toggling of inside/outside at every single! intersection point of an edge
  21527. // with the horizontal line through inPt, left of inPt
  21528. // not counting lowerY endpoints of edges and whole edges on that line
  21529. let inside = false;
  21530. for ( let p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) {
  21531. let edgeLowPt = inPolygon[ p ];
  21532. let edgeHighPt = inPolygon[ q ];
  21533. let edgeDx = edgeHighPt.x - edgeLowPt.x;
  21534. let edgeDy = edgeHighPt.y - edgeLowPt.y;
  21535. if ( Math.abs( edgeDy ) > Number.EPSILON ) {
  21536. // not parallel
  21537. if ( edgeDy < 0 ) {
  21538. edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx;
  21539. edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy;
  21540. }
  21541. if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue;
  21542. if ( inPt.y === edgeLowPt.y ) {
  21543. if ( inPt.x === edgeLowPt.x ) return true; // inPt is on contour ?
  21544. // continue; // no intersection or edgeLowPt => doesn't count !!!
  21545. } else {
  21546. const perpEdge = edgeDy * ( inPt.x - edgeLowPt.x ) - edgeDx * ( inPt.y - edgeLowPt.y );
  21547. if ( perpEdge === 0 ) return true; // inPt is on contour ?
  21548. if ( perpEdge < 0 ) continue;
  21549. inside = ! inside; // true intersection left of inPt
  21550. }
  21551. } else {
  21552. // parallel or collinear
  21553. if ( inPt.y !== edgeLowPt.y ) continue; // parallel
  21554. // edge lies on the same horizontal line as inPt
  21555. if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) ||
  21556. ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour !
  21557. // continue;
  21558. }
  21559. }
  21560. return inside;
  21561. }
  21562. const isClockWise = ShapeUtils.isClockWise;
  21563. const subPaths = this.subPaths;
  21564. if ( subPaths.length === 0 ) return [];
  21565. let solid, tmpPath, tmpShape;
  21566. const shapes = [];
  21567. if ( subPaths.length === 1 ) {
  21568. tmpPath = subPaths[ 0 ];
  21569. tmpShape = new Shape();
  21570. tmpShape.curves = tmpPath.curves;
  21571. shapes.push( tmpShape );
  21572. return shapes;
  21573. }
  21574. let holesFirst = ! isClockWise( subPaths[ 0 ].getPoints() );
  21575. holesFirst = isCCW ? ! holesFirst : holesFirst;
  21576. // console.log("Holes first", holesFirst);
  21577. const betterShapeHoles = [];
  21578. const newShapes = [];
  21579. let newShapeHoles = [];
  21580. let mainIdx = 0;
  21581. let tmpPoints;
  21582. newShapes[ mainIdx ] = undefined;
  21583. newShapeHoles[ mainIdx ] = [];
  21584. for ( let i = 0, l = subPaths.length; i < l; i ++ ) {
  21585. tmpPath = subPaths[ i ];
  21586. tmpPoints = tmpPath.getPoints();
  21587. solid = isClockWise( tmpPoints );
  21588. solid = isCCW ? ! solid : solid;
  21589. if ( solid ) {
  21590. if ( ( ! holesFirst ) && ( newShapes[ mainIdx ] ) ) mainIdx ++;
  21591. newShapes[ mainIdx ] = { s: new Shape(), p: tmpPoints };
  21592. newShapes[ mainIdx ].s.curves = tmpPath.curves;
  21593. if ( holesFirst ) mainIdx ++;
  21594. newShapeHoles[ mainIdx ] = [];
  21595. //console.log('cw', i);
  21596. } else {
  21597. newShapeHoles[ mainIdx ].push( { h: tmpPath, p: tmpPoints[ 0 ] } );
  21598. //console.log('ccw', i);
  21599. }
  21600. }
  21601. // only Holes? -> probably all Shapes with wrong orientation
  21602. if ( ! newShapes[ 0 ] ) return toShapesNoHoles( subPaths );
  21603. if ( newShapes.length > 1 ) {
  21604. let ambiguous = false;
  21605. let toChange = 0;
  21606. for ( let sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
  21607. betterShapeHoles[ sIdx ] = [];
  21608. }
  21609. for ( let sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
  21610. const sho = newShapeHoles[ sIdx ];
  21611. for ( let hIdx = 0; hIdx < sho.length; hIdx ++ ) {
  21612. const ho = sho[ hIdx ];
  21613. let hole_unassigned = true;
  21614. for ( let s2Idx = 0; s2Idx < newShapes.length; s2Idx ++ ) {
  21615. if ( isPointInsidePolygon( ho.p, newShapes[ s2Idx ].p ) ) {
  21616. if ( sIdx !== s2Idx ) toChange ++;
  21617. if ( hole_unassigned ) {
  21618. hole_unassigned = false;
  21619. betterShapeHoles[ s2Idx ].push( ho );
  21620. } else {
  21621. ambiguous = true;
  21622. }
  21623. }
  21624. }
  21625. if ( hole_unassigned ) {
  21626. betterShapeHoles[ sIdx ].push( ho );
  21627. }
  21628. }
  21629. }
  21630. if ( toChange > 0 && ambiguous === false ) {
  21631. newShapeHoles = betterShapeHoles;
  21632. }
  21633. }
  21634. let tmpHoles;
  21635. for ( let i = 0, il = newShapes.length; i < il; i ++ ) {
  21636. tmpShape = newShapes[ i ].s;
  21637. shapes.push( tmpShape );
  21638. tmpHoles = newShapeHoles[ i ];
  21639. for ( let j = 0, jl = tmpHoles.length; j < jl; j ++ ) {
  21640. tmpShape.holes.push( tmpHoles[ j ].h );
  21641. }
  21642. }
  21643. //console.log("shape", shapes);
  21644. return shapes;
  21645. }
  21646. }
  21647. class Controls extends EventDispatcher {
  21648. constructor( object, domElement = null ) {
  21649. super();
  21650. this.object = object;
  21651. this.domElement = domElement;
  21652. this.enabled = true;
  21653. this.state = - 1;
  21654. this.keys = {};
  21655. this.mouseButtons = { LEFT: null, MIDDLE: null, RIGHT: null };
  21656. this.touches = { ONE: null, TWO: null };
  21657. }
  21658. connect() {}
  21659. disconnect() {}
  21660. dispose() {}
  21661. update( /* delta */ ) {}
  21662. }
  21663. function contain( texture, aspect ) {
  21664. const imageAspect = ( texture.image && texture.image.width ) ? texture.image.width / texture.image.height : 1;
  21665. if ( imageAspect > aspect ) {
  21666. texture.repeat.x = 1;
  21667. texture.repeat.y = imageAspect / aspect;
  21668. texture.offset.x = 0;
  21669. texture.offset.y = ( 1 - texture.repeat.y ) / 2;
  21670. } else {
  21671. texture.repeat.x = aspect / imageAspect;
  21672. texture.repeat.y = 1;
  21673. texture.offset.x = ( 1 - texture.repeat.x ) / 2;
  21674. texture.offset.y = 0;
  21675. }
  21676. return texture;
  21677. }
  21678. function cover( texture, aspect ) {
  21679. const imageAspect = ( texture.image && texture.image.width ) ? texture.image.width / texture.image.height : 1;
  21680. if ( imageAspect > aspect ) {
  21681. texture.repeat.x = aspect / imageAspect;
  21682. texture.repeat.y = 1;
  21683. texture.offset.x = ( 1 - texture.repeat.x ) / 2;
  21684. texture.offset.y = 0;
  21685. } else {
  21686. texture.repeat.x = 1;
  21687. texture.repeat.y = imageAspect / aspect;
  21688. texture.offset.x = 0;
  21689. texture.offset.y = ( 1 - texture.repeat.y ) / 2;
  21690. }
  21691. return texture;
  21692. }
  21693. function fill( texture ) {
  21694. texture.repeat.x = 1;
  21695. texture.repeat.y = 1;
  21696. texture.offset.x = 0;
  21697. texture.offset.y = 0;
  21698. return texture;
  21699. }
  21700. /**
  21701. * Given the width, height, format, and type of a texture. Determines how many
  21702. * bytes must be used to represent the texture.
  21703. *
  21704. * @param {Number} width
  21705. * @param {Number} height
  21706. * @param {Number} format
  21707. * @param {Number} type
  21708. * @return {Number} The number of bytes required to represent the texture.
  21709. */
  21710. function getByteLength( width, height, format, type ) {
  21711. const typeByteLength = getTextureTypeByteLength( type );
  21712. switch ( format ) {
  21713. // https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml
  21714. case AlphaFormat:
  21715. return width * height;
  21716. case LuminanceFormat:
  21717. return width * height;
  21718. case LuminanceAlphaFormat:
  21719. return width * height * 2;
  21720. case RedFormat:
  21721. return ( ( width * height ) / typeByteLength.components ) * typeByteLength.byteLength;
  21722. case RedIntegerFormat:
  21723. return ( ( width * height ) / typeByteLength.components ) * typeByteLength.byteLength;
  21724. case RGFormat:
  21725. return ( ( width * height * 2 ) / typeByteLength.components ) * typeByteLength.byteLength;
  21726. case RGIntegerFormat:
  21727. return ( ( width * height * 2 ) / typeByteLength.components ) * typeByteLength.byteLength;
  21728. case RGBFormat:
  21729. return ( ( width * height * 3 ) / typeByteLength.components ) * typeByteLength.byteLength;
  21730. case RGBAFormat:
  21731. return ( ( width * height * 4 ) / typeByteLength.components ) * typeByteLength.byteLength;
  21732. case RGBAIntegerFormat:
  21733. return ( ( width * height * 4 ) / typeByteLength.components ) * typeByteLength.byteLength;
  21734. // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_s3tc_srgb/
  21735. case RGB_S3TC_DXT1_Format:
  21736. case RGBA_S3TC_DXT1_Format:
  21737. return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 8;
  21738. case RGBA_S3TC_DXT3_Format:
  21739. case RGBA_S3TC_DXT5_Format:
  21740. return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 16;
  21741. // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_pvrtc/
  21742. case RGB_PVRTC_2BPPV1_Format:
  21743. case RGBA_PVRTC_2BPPV1_Format:
  21744. return ( Math.max( width, 16 ) * Math.max( height, 8 ) ) / 4;
  21745. case RGB_PVRTC_4BPPV1_Format:
  21746. case RGBA_PVRTC_4BPPV1_Format:
  21747. return ( Math.max( width, 8 ) * Math.max( height, 8 ) ) / 2;
  21748. // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_etc/
  21749. case RGB_ETC1_Format:
  21750. case RGB_ETC2_Format:
  21751. return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 8;
  21752. case RGBA_ETC2_EAC_Format:
  21753. return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 16;
  21754. // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_astc/
  21755. case RGBA_ASTC_4x4_Format:
  21756. return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 16;
  21757. case RGBA_ASTC_5x4_Format:
  21758. return Math.floor( ( width + 4 ) / 5 ) * Math.floor( ( height + 3 ) / 4 ) * 16;
  21759. case RGBA_ASTC_5x5_Format:
  21760. return Math.floor( ( width + 4 ) / 5 ) * Math.floor( ( height + 4 ) / 5 ) * 16;
  21761. case RGBA_ASTC_6x5_Format:
  21762. return Math.floor( ( width + 5 ) / 6 ) * Math.floor( ( height + 4 ) / 5 ) * 16;
  21763. case RGBA_ASTC_6x6_Format:
  21764. return Math.floor( ( width + 5 ) / 6 ) * Math.floor( ( height + 5 ) / 6 ) * 16;
  21765. case RGBA_ASTC_8x5_Format:
  21766. return Math.floor( ( width + 7 ) / 8 ) * Math.floor( ( height + 4 ) / 5 ) * 16;
  21767. case RGBA_ASTC_8x6_Format:
  21768. return Math.floor( ( width + 7 ) / 8 ) * Math.floor( ( height + 5 ) / 6 ) * 16;
  21769. case RGBA_ASTC_8x8_Format:
  21770. return Math.floor( ( width + 7 ) / 8 ) * Math.floor( ( height + 7 ) / 8 ) * 16;
  21771. case RGBA_ASTC_10x5_Format:
  21772. return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 4 ) / 5 ) * 16;
  21773. case RGBA_ASTC_10x6_Format:
  21774. return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 5 ) / 6 ) * 16;
  21775. case RGBA_ASTC_10x8_Format:
  21776. return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 7 ) / 8 ) * 16;
  21777. case RGBA_ASTC_10x10_Format:
  21778. return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 9 ) / 10 ) * 16;
  21779. case RGBA_ASTC_12x10_Format:
  21780. return Math.floor( ( width + 11 ) / 12 ) * Math.floor( ( height + 9 ) / 10 ) * 16;
  21781. case RGBA_ASTC_12x12_Format:
  21782. return Math.floor( ( width + 11 ) / 12 ) * Math.floor( ( height + 11 ) / 12 ) * 16;
  21783. // https://registry.khronos.org/webgl/extensions/EXT_texture_compression_bptc/
  21784. case RGBA_BPTC_Format:
  21785. case RGB_BPTC_SIGNED_Format:
  21786. case RGB_BPTC_UNSIGNED_Format:
  21787. return Math.ceil( width / 4 ) * Math.ceil( height / 4 ) * 16;
  21788. // https://registry.khronos.org/webgl/extensions/EXT_texture_compression_rgtc/
  21789. case RED_RGTC1_Format:
  21790. case SIGNED_RED_RGTC1_Format:
  21791. return Math.ceil( width / 4 ) * Math.ceil( height / 4 ) * 8;
  21792. case RED_GREEN_RGTC2_Format:
  21793. case SIGNED_RED_GREEN_RGTC2_Format:
  21794. return Math.ceil( width / 4 ) * Math.ceil( height / 4 ) * 16;
  21795. }
  21796. throw new Error(
  21797. `Unable to determine texture byte length for ${format} format.`,
  21798. );
  21799. }
  21800. function getTextureTypeByteLength( type ) {
  21801. switch ( type ) {
  21802. case UnsignedByteType:
  21803. case ByteType:
  21804. return { byteLength: 1, components: 1 };
  21805. case UnsignedShortType:
  21806. case ShortType:
  21807. case HalfFloatType:
  21808. return { byteLength: 2, components: 1 };
  21809. case UnsignedShort4444Type:
  21810. case UnsignedShort5551Type:
  21811. return { byteLength: 2, components: 4 };
  21812. case UnsignedIntType:
  21813. case IntType:
  21814. case FloatType:
  21815. return { byteLength: 4, components: 1 };
  21816. case UnsignedInt5999Type:
  21817. return { byteLength: 4, components: 3 };
  21818. }
  21819. throw new Error( `Unknown texture type ${type}.` );
  21820. }
  21821. const TextureUtils = {
  21822. contain,
  21823. cover,
  21824. fill,
  21825. getByteLength
  21826. };
  21827. if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
  21828. __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'register', { detail: {
  21829. revision: REVISION,
  21830. } } ) );
  21831. }
  21832. if ( typeof window !== 'undefined' ) {
  21833. if ( window.__THREE__ ) {
  21834. console.warn( 'WARNING: Multiple instances of Three.js being imported.' );
  21835. } else {
  21836. window.__THREE__ = REVISION;
  21837. }
  21838. }
  21839. export { ACESFilmicToneMapping, AddEquation, AddOperation, AdditiveAnimationBlendMode, AdditiveBlending, AgXToneMapping, AlphaFormat, AlwaysCompare, AlwaysDepth, AlwaysStencilFunc, AmbientLight, AnimationAction, AnimationClip, AnimationLoader, AnimationMixer, AnimationObjectGroup, AnimationUtils, ArcCurve, ArrayCamera, ArrowHelper, AttachedBindMode, Audio, AudioAnalyser, AudioContext, AudioListener, AudioLoader, AxesHelper, BackSide, BasicDepthPacking, BasicShadowMap, BatchedMesh, Bone, BooleanKeyframeTrack, Box2, Box3, Box3Helper, BoxGeometry, BoxHelper, BufferAttribute, BufferGeometry, BufferGeometryLoader, ByteType, Cache, Camera, CameraHelper, CanvasTexture, CapsuleGeometry, CatmullRomCurve3, CineonToneMapping, CircleGeometry, ClampToEdgeWrapping, Clock, Color, ColorKeyframeTrack, ColorManagement, CompressedArrayTexture, CompressedCubeTexture, CompressedTexture, CompressedTextureLoader, ConeGeometry, ConstantAlphaFactor, ConstantColorFactor, Controls, CubeCamera, CubeReflectionMapping, CubeRefractionMapping, CubeTexture, CubeTextureLoader, CubeUVReflectionMapping, CubicBezierCurve, CubicBezierCurve3, CubicInterpolant, CullFaceBack, CullFaceFront, CullFaceFrontBack, CullFaceNone, Curve, CurvePath, CustomBlending, CustomToneMapping, CylinderGeometry, Cylindrical, Data3DTexture, DataArrayTexture, DataTexture, DataTextureLoader, DataUtils, DecrementStencilOp, DecrementWrapStencilOp, DefaultLoadingManager, DepthFormat, DepthStencilFormat, DepthTexture, DetachedBindMode, DirectionalLight, DirectionalLightHelper, DiscreteInterpolant, DodecahedronGeometry, DoubleSide, DstAlphaFactor, DstColorFactor, DynamicCopyUsage, DynamicDrawUsage, DynamicReadUsage, EdgesGeometry, EllipseCurve, EqualCompare, EqualDepth, EqualStencilFunc, EquirectangularReflectionMapping, EquirectangularRefractionMapping, Euler, EventDispatcher, ExtrudeGeometry, FileLoader, Float16BufferAttribute, Float32BufferAttribute, FloatType, Fog, FogExp2, FramebufferTexture, FrontSide, Frustum, GLBufferAttribute, GLSL1, GLSL3, GreaterCompare, GreaterDepth, GreaterEqualCompare, GreaterEqualDepth, GreaterEqualStencilFunc, GreaterStencilFunc, GridHelper, Group, HalfFloatType, HemisphereLight, HemisphereLightHelper, IcosahedronGeometry, ImageBitmapLoader, ImageLoader, ImageUtils, IncrementStencilOp, IncrementWrapStencilOp, InstancedBufferAttribute, InstancedBufferGeometry, InstancedInterleavedBuffer, InstancedMesh, Int16BufferAttribute, Int32BufferAttribute, Int8BufferAttribute, IntType, InterleavedBuffer, InterleavedBufferAttribute, Interpolant, InterpolateDiscrete, InterpolateLinear, InterpolateSmooth, InvertStencilOp, KeepStencilOp, KeyframeTrack, LOD, LatheGeometry, Layers, LessCompare, LessDepth, LessEqualCompare, LessEqualDepth, LessEqualStencilFunc, LessStencilFunc, Light, LightProbe, Line, Line3, LineBasicMaterial, LineCurve, LineCurve3, LineDashedMaterial, LineLoop, LineSegments, LinearFilter, LinearInterpolant, LinearMipMapLinearFilter, LinearMipMapNearestFilter, LinearMipmapLinearFilter, LinearMipmapNearestFilter, LinearSRGBColorSpace, LinearToneMapping, LinearTransfer, Loader, LoaderUtils, LoadingManager, LoopOnce, LoopPingPong, LoopRepeat, LuminanceAlphaFormat, LuminanceFormat, MOUSE, Material, MaterialLoader, MathUtils, Matrix2, Matrix3, Matrix4, MaxEquation, Mesh, MeshBasicMaterial, MeshDepthMaterial, MeshDistanceMaterial, MeshLambertMaterial, MeshMatcapMaterial, MeshNormalMaterial, MeshPhongMaterial, MeshPhysicalMaterial, MeshStandardMaterial, MeshToonMaterial, MinEquation, MirroredRepeatWrapping, MixOperation, MultiplyBlending, MultiplyOperation, NearestFilter, NearestMipMapLinearFilter, NearestMipMapNearestFilter, NearestMipmapLinearFilter, NearestMipmapNearestFilter, NeutralToneMapping, NeverCompare, NeverDepth, NeverStencilFunc, NoBlending, NoColorSpace, NoToneMapping, NormalAnimationBlendMode, NormalBlending, NotEqualCompare, NotEqualDepth, NotEqualStencilFunc, NumberKeyframeTrack, Object3D, ObjectLoader, ObjectSpaceNormalMap, OctahedronGeometry, OneFactor, OneMinusConstantAlphaFactor, OneMinusConstantColorFactor, OneMinusDstAlphaFactor, OneMinusDstColorFactor, OneMinusSrcAlphaFactor, OneMinusSrcColorFactor, OrthographicCamera, PCFShadowMap, PCFSoftShadowMap, Path, PerspectiveCamera, Plane, PlaneGeometry, PlaneHelper, PointLight, PointLightHelper, Points, PointsMaterial, PolarGridHelper, PolyhedronGeometry, PositionalAudio, PropertyBinding, PropertyMixer, QuadraticBezierCurve, QuadraticBezierCurve3, Quaternion, QuaternionKeyframeTrack, QuaternionLinearInterpolant, RAD2DEG, RED_GREEN_RGTC2_Format, RED_RGTC1_Format, REVISION, RGBADepthPacking, RGBAFormat, RGBAIntegerFormat, RGBA_ASTC_10x10_Format, RGBA_ASTC_10x5_Format, RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format, RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format, RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format, RGBA_BPTC_Format, RGBA_ETC2_EAC_Format, RGBA_PVRTC_2BPPV1_Format, RGBA_PVRTC_4BPPV1_Format, RGBA_S3TC_DXT1_Format, RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format, RGBDepthPacking, RGBFormat, RGBIntegerFormat, RGB_BPTC_SIGNED_Format, RGB_BPTC_UNSIGNED_Format, RGB_ETC1_Format, RGB_ETC2_Format, RGB_PVRTC_2BPPV1_Format, RGB_PVRTC_4BPPV1_Format, RGB_S3TC_DXT1_Format, RGDepthPacking, RGFormat, RGIntegerFormat, RawShaderMaterial, Ray, Raycaster, RectAreaLight, RedFormat, RedIntegerFormat, ReinhardToneMapping, RenderTarget, RenderTarget3D, RenderTargetArray, RepeatWrapping, ReplaceStencilOp, ReverseSubtractEquation, RingGeometry, SIGNED_RED_GREEN_RGTC2_Format, SIGNED_RED_RGTC1_Format, SRGBColorSpace, SRGBTransfer, Scene, ShaderMaterial, ShadowMaterial, Shape, ShapeGeometry, ShapePath, ShapeUtils, ShortType, Skeleton, SkeletonHelper, SkinnedMesh, Source, Sphere, SphereGeometry, Spherical, SphericalHarmonics3, SplineCurve, SpotLight, SpotLightHelper, Sprite, SpriteMaterial, SrcAlphaFactor, SrcAlphaSaturateFactor, SrcColorFactor, StaticCopyUsage, StaticDrawUsage, StaticReadUsage, StereoCamera, StreamCopyUsage, StreamDrawUsage, StreamReadUsage, StringKeyframeTrack, SubtractEquation, SubtractiveBlending, TOUCH, TangentSpaceNormalMap, TetrahedronGeometry, Texture, TextureLoader, TextureUtils, TorusGeometry, TorusKnotGeometry, Triangle, TriangleFanDrawMode, TriangleStripDrawMode, TrianglesDrawMode, TubeGeometry, UVMapping, Uint16BufferAttribute, Uint32BufferAttribute, Uint8BufferAttribute, Uint8ClampedBufferAttribute, Uniform, UniformsGroup, UniformsUtils, UnsignedByteType, UnsignedInt248Type, UnsignedInt5999Type, UnsignedIntType, UnsignedShort4444Type, UnsignedShort5551Type, UnsignedShortType, VSMShadowMap, Vector2, Vector3, Vector4, VectorKeyframeTrack, VideoTexture, WebGL3DRenderTarget, WebGLArrayRenderTarget, WebGLCoordinateSystem, WebGLCubeRenderTarget, WebGLRenderTarget, WebGPUCoordinateSystem, WireframeGeometry, WrapAroundEnding, ZeroCurvatureEnding, ZeroFactor, ZeroSlopeEnding, ZeroStencilOp, arrayNeedsUint32, cloneUniforms, createCanvasElement, createElementNS, getByteLength, getUnlitUniformColorSpace, mergeUniforms, probeAsync, toNormalizedProjectionMatrix, toReversedProjectionMatrix, warnOnce };
粤ICP备19079148号