three.core.js 734 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946349473494834949349503495134952349533495434955349563495734958349593496034961349623496334964349653496634967349683496934970349713497234973349743497534976349773497834979349803498134982349833498434985349863498734988349893499034991349923499334994349953499634997349983499935000350013500235003350043500535006350073500835009350103501135012350133501435015350163501735018350193502035021350223502335024350253502635027350283502935030350313503235033350343503535036350373503835039350403504135042350433504435045350463504735048350493505035051350523505335054350553505635057350583505935060350613506235063350643506535066350673506835069350703507135072350733507435075350763507735078350793508035081350823508335084350853508635087350883508935090350913509235093350943509535096350973509835099351003510135102351033510435105351063510735108351093511035111351123511335114351153511635117351183511935120351213512235123351243512535126351273512835129351303513135132351333513435135351363513735138351393514035141351423514335144351453514635147351483514935150351513515235153351543515535156351573515835159351603516135162351633516435165351663516735168351693517035171351723517335174351753517635177351783517935180351813518235183351843518535186351873518835189351903519135192351933519435195351963519735198351993520035201352023520335204352053520635207352083520935210352113521235213352143521535216352173521835219352203522135222352233522435225352263522735228352293523035231352323523335234352353523635237352383523935240352413524235243352443524535246352473524835249352503525135252352533525435255352563525735258352593526035261352623526335264352653526635267352683526935270352713527235273352743527535276352773527835279352803528135282352833528435285352863528735288352893529035291352923529335294352953529635297352983529935300353013530235303353043530535306353073530835309353103531135312353133531435315353163531735318353193532035321353223532335324353253532635327353283532935330353313533235333353343533535336353373533835339353403534135342353433534435345353463534735348353493535035351353523535335354353553535635357353583535935360353613536235363353643536535366353673536835369353703537135372353733537435375353763537735378353793538035381353823538335384353853538635387353883538935390353913539235393353943539535396353973539835399354003540135402354033540435405354063540735408354093541035411354123541335414354153541635417354183541935420354213542235423354243542535426354273542835429354303543135432354333543435435354363543735438354393544035441354423544335444354453544635447354483544935450354513545235453354543545535456354573545835459354603546135462354633546435465354663546735468354693547035471354723547335474354753547635477354783547935480354813548235483354843548535486354873548835489354903549135492354933549435495354963549735498354993550035501355023550335504355053550635507355083550935510355113551235513355143551535516355173551835519355203552135522355233552435525355263552735528355293553035531355323553335534355353553635537355383553935540355413554235543355443554535546355473554835549355503555135552355533555435555355563555735558355593556035561355623556335564355653556635567355683556935570355713557235573355743557535576355773557835579355803558135582355833558435585355863558735588355893559035591355923559335594355953559635597355983559935600356013560235603356043560535606356073560835609356103561135612356133561435615356163561735618356193562035621356223562335624356253562635627356283562935630356313563235633356343563535636356373563835639356403564135642356433564435645356463564735648356493565035651356523565335654356553565635657356583565935660356613566235663356643566535666356673566835669356703567135672356733567435675356763567735678356793568035681356823568335684356853568635687356883568935690356913569235693356943569535696356973569835699357003570135702357033570435705357063570735708357093571035711357123571335714357153571635717357183571935720357213572235723357243572535726357273572835729357303573135732357333573435735357363573735738357393574035741357423574335744357453574635747357483574935750357513575235753357543575535756357573575835759357603576135762357633576435765357663576735768357693577035771357723577335774357753577635777357783577935780357813578235783357843578535786357873578835789357903579135792357933579435795357963579735798357993580035801358023580335804358053580635807358083580935810358113581235813358143581535816358173581835819358203582135822358233582435825358263582735828358293583035831358323583335834358353583635837358383583935840358413584235843358443584535846358473584835849358503585135852358533585435855358563585735858358593586035861358623586335864358653586635867358683586935870358713587235873358743587535876358773587835879358803588135882358833588435885358863588735888358893589035891358923589335894358953589635897358983589935900359013590235903359043590535906359073590835909359103591135912359133591435915359163591735918359193592035921359223592335924359253592635927359283592935930359313593235933359343593535936359373593835939359403594135942359433594435945359463594735948359493595035951359523595335954359553595635957359583595935960359613596235963359643596535966359673596835969359703597135972359733597435975359763597735978359793598035981359823598335984359853598635987359883598935990359913599235993359943599535996359973599835999360003600136002360033600436005360063600736008360093601036011360123601336014360153601636017360183601936020360213602236023360243602536026360273602836029360303603136032360333603436035360363603736038360393604036041360423604336044360453604636047360483604936050360513605236053360543605536056360573605836059360603606136062360633606436065360663606736068360693607036071360723607336074360753607636077360783607936080360813608236083360843608536086360873608836089360903609136092360933609436095360963609736098360993610036101361023610336104361053610636107361083610936110361113611236113361143611536116361173611836119361203612136122361233612436125361263612736128361293613036131361323613336134361353613636137361383613936140361413614236143361443614536146361473614836149361503615136152361533615436155361563615736158361593616036161361623616336164361653616636167361683616936170361713617236173361743617536176361773617836179361803618136182361833618436185361863618736188361893619036191361923619336194361953619636197361983619936200362013620236203362043620536206362073620836209362103621136212362133621436215362163621736218362193622036221362223622336224362253622636227362283622936230362313623236233362343623536236362373623836239362403624136242362433624436245362463624736248362493625036251362523625336254362553625636257362583625936260362613626236263362643626536266362673626836269362703627136272362733627436275362763627736278362793628036281362823628336284362853628636287362883628936290362913629236293362943629536296362973629836299363003630136302363033630436305363063630736308363093631036311363123631336314363153631636317363183631936320363213632236323363243632536326363273632836329363303633136332363333633436335363363633736338363393634036341363423634336344363453634636347363483634936350363513635236353363543635536356363573635836359363603636136362363633636436365363663636736368363693637036371363723637336374363753637636377363783637936380363813638236383363843638536386363873638836389363903639136392363933639436395363963639736398363993640036401364023640336404364053640636407364083640936410364113641236413364143641536416364173641836419364203642136422364233642436425364263642736428364293643036431364323643336434364353643636437364383643936440364413644236443364443644536446364473644836449364503645136452364533645436455364563645736458364593646036461364623646336464364653646636467364683646936470364713647236473364743647536476364773647836479364803648136482364833648436485364863648736488364893649036491364923649336494364953649636497364983649936500365013650236503365043650536506365073650836509365103651136512365133651436515365163651736518365193652036521365223652336524365253652636527365283652936530365313653236533365343653536536365373653836539365403654136542365433654436545365463654736548365493655036551365523655336554365553655636557365583655936560365613656236563365643656536566365673656836569365703657136572365733657436575365763657736578365793658036581365823658336584365853658636587365883658936590365913659236593365943659536596365973659836599366003660136602366033660436605366063660736608366093661036611366123661336614366153661636617366183661936620366213662236623366243662536626366273662836629366303663136632366333663436635366363663736638366393664036641366423664336644366453664636647366483664936650366513665236653366543665536656366573665836659366603666136662366633666436665366663666736668366693667036671366723667336674366753667636677366783667936680366813668236683366843668536686366873668836689366903669136692366933669436695366963669736698366993670036701367023670336704367053670636707367083670936710367113671236713367143671536716367173671836719367203672136722367233672436725367263672736728367293673036731367323673336734367353673636737367383673936740367413674236743367443674536746367473674836749367503675136752367533675436755367563675736758367593676036761367623676336764367653676636767367683676936770367713677236773367743677536776367773677836779367803678136782367833678436785367863678736788367893679036791367923679336794367953679636797367983679936800368013680236803368043680536806368073680836809368103681136812368133681436815368163681736818368193682036821368223682336824368253682636827368283682936830368313683236833368343683536836368373683836839368403684136842368433684436845368463684736848368493685036851368523685336854368553685636857368583685936860368613686236863368643686536866368673686836869368703687136872368733687436875368763687736878368793688036881368823688336884368853688636887368883688936890368913689236893368943689536896368973689836899369003690136902369033690436905369063690736908369093691036911369123691336914369153691636917369183691936920369213692236923369243692536926369273692836929369303693136932369333693436935369363693736938369393694036941369423694336944369453694636947
  1. /**
  2. * @license
  3. * Copyright 2010-2024 Three.js Authors
  4. * SPDX-License-Identifier: MIT
  5. */
  6. const REVISION = '171dev';
  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.isRenderTargetTexture = false; // indicates whether a texture belongs to a render target or not
  1304. this.pmremVersion = 0; // indicates whether this texture should be processed by PMREMGenerator or not (only relevant for render target textures)
  1305. }
  1306. get image() {
  1307. return this.source.data;
  1308. }
  1309. set image( value = null ) {
  1310. this.source.data = value;
  1311. }
  1312. updateMatrix() {
  1313. this.matrix.setUvTransform( this.offset.x, this.offset.y, this.repeat.x, this.repeat.y, this.rotation, this.center.x, this.center.y );
  1314. }
  1315. clone() {
  1316. return new this.constructor().copy( this );
  1317. }
  1318. copy( source ) {
  1319. this.name = source.name;
  1320. this.source = source.source;
  1321. this.mipmaps = source.mipmaps.slice( 0 );
  1322. this.mapping = source.mapping;
  1323. this.channel = source.channel;
  1324. this.wrapS = source.wrapS;
  1325. this.wrapT = source.wrapT;
  1326. this.magFilter = source.magFilter;
  1327. this.minFilter = source.minFilter;
  1328. this.anisotropy = source.anisotropy;
  1329. this.format = source.format;
  1330. this.internalFormat = source.internalFormat;
  1331. this.type = source.type;
  1332. this.offset.copy( source.offset );
  1333. this.repeat.copy( source.repeat );
  1334. this.center.copy( source.center );
  1335. this.rotation = source.rotation;
  1336. this.matrixAutoUpdate = source.matrixAutoUpdate;
  1337. this.matrix.copy( source.matrix );
  1338. this.generateMipmaps = source.generateMipmaps;
  1339. this.premultiplyAlpha = source.premultiplyAlpha;
  1340. this.flipY = source.flipY;
  1341. this.unpackAlignment = source.unpackAlignment;
  1342. this.colorSpace = source.colorSpace;
  1343. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  1344. this.needsUpdate = true;
  1345. return this;
  1346. }
  1347. toJSON( meta ) {
  1348. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  1349. if ( ! isRootObject && meta.textures[ this.uuid ] !== undefined ) {
  1350. return meta.textures[ this.uuid ];
  1351. }
  1352. const output = {
  1353. metadata: {
  1354. version: 4.6,
  1355. type: 'Texture',
  1356. generator: 'Texture.toJSON'
  1357. },
  1358. uuid: this.uuid,
  1359. name: this.name,
  1360. image: this.source.toJSON( meta ).uuid,
  1361. mapping: this.mapping,
  1362. channel: this.channel,
  1363. repeat: [ this.repeat.x, this.repeat.y ],
  1364. offset: [ this.offset.x, this.offset.y ],
  1365. center: [ this.center.x, this.center.y ],
  1366. rotation: this.rotation,
  1367. wrap: [ this.wrapS, this.wrapT ],
  1368. format: this.format,
  1369. internalFormat: this.internalFormat,
  1370. type: this.type,
  1371. colorSpace: this.colorSpace,
  1372. minFilter: this.minFilter,
  1373. magFilter: this.magFilter,
  1374. anisotropy: this.anisotropy,
  1375. flipY: this.flipY,
  1376. generateMipmaps: this.generateMipmaps,
  1377. premultiplyAlpha: this.premultiplyAlpha,
  1378. unpackAlignment: this.unpackAlignment
  1379. };
  1380. if ( Object.keys( this.userData ).length > 0 ) output.userData = this.userData;
  1381. if ( ! isRootObject ) {
  1382. meta.textures[ this.uuid ] = output;
  1383. }
  1384. return output;
  1385. }
  1386. dispose() {
  1387. this.dispatchEvent( { type: 'dispose' } );
  1388. }
  1389. transformUv( uv ) {
  1390. if ( this.mapping !== UVMapping ) return uv;
  1391. uv.applyMatrix3( this.matrix );
  1392. if ( uv.x < 0 || uv.x > 1 ) {
  1393. switch ( this.wrapS ) {
  1394. case RepeatWrapping:
  1395. uv.x = uv.x - Math.floor( uv.x );
  1396. break;
  1397. case ClampToEdgeWrapping:
  1398. uv.x = uv.x < 0 ? 0 : 1;
  1399. break;
  1400. case MirroredRepeatWrapping:
  1401. if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) {
  1402. uv.x = Math.ceil( uv.x ) - uv.x;
  1403. } else {
  1404. uv.x = uv.x - Math.floor( uv.x );
  1405. }
  1406. break;
  1407. }
  1408. }
  1409. if ( uv.y < 0 || uv.y > 1 ) {
  1410. switch ( this.wrapT ) {
  1411. case RepeatWrapping:
  1412. uv.y = uv.y - Math.floor( uv.y );
  1413. break;
  1414. case ClampToEdgeWrapping:
  1415. uv.y = uv.y < 0 ? 0 : 1;
  1416. break;
  1417. case MirroredRepeatWrapping:
  1418. if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) {
  1419. uv.y = Math.ceil( uv.y ) - uv.y;
  1420. } else {
  1421. uv.y = uv.y - Math.floor( uv.y );
  1422. }
  1423. break;
  1424. }
  1425. }
  1426. if ( this.flipY ) {
  1427. uv.y = 1 - uv.y;
  1428. }
  1429. return uv;
  1430. }
  1431. set needsUpdate( value ) {
  1432. if ( value === true ) {
  1433. this.version ++;
  1434. this.source.needsUpdate = true;
  1435. }
  1436. }
  1437. set needsPMREMUpdate( value ) {
  1438. if ( value === true ) {
  1439. this.pmremVersion ++;
  1440. }
  1441. }
  1442. }
  1443. Texture.DEFAULT_IMAGE = null;
  1444. Texture.DEFAULT_MAPPING = UVMapping;
  1445. Texture.DEFAULT_ANISOTROPY = 1;
  1446. class Vector4 {
  1447. constructor( x = 0, y = 0, z = 0, w = 1 ) {
  1448. Vector4.prototype.isVector4 = true;
  1449. this.x = x;
  1450. this.y = y;
  1451. this.z = z;
  1452. this.w = w;
  1453. }
  1454. get width() {
  1455. return this.z;
  1456. }
  1457. set width( value ) {
  1458. this.z = value;
  1459. }
  1460. get height() {
  1461. return this.w;
  1462. }
  1463. set height( value ) {
  1464. this.w = value;
  1465. }
  1466. set( x, y, z, w ) {
  1467. this.x = x;
  1468. this.y = y;
  1469. this.z = z;
  1470. this.w = w;
  1471. return this;
  1472. }
  1473. setScalar( scalar ) {
  1474. this.x = scalar;
  1475. this.y = scalar;
  1476. this.z = scalar;
  1477. this.w = scalar;
  1478. return this;
  1479. }
  1480. setX( x ) {
  1481. this.x = x;
  1482. return this;
  1483. }
  1484. setY( y ) {
  1485. this.y = y;
  1486. return this;
  1487. }
  1488. setZ( z ) {
  1489. this.z = z;
  1490. return this;
  1491. }
  1492. setW( w ) {
  1493. this.w = w;
  1494. return this;
  1495. }
  1496. setComponent( index, value ) {
  1497. switch ( index ) {
  1498. case 0: this.x = value; break;
  1499. case 1: this.y = value; break;
  1500. case 2: this.z = value; break;
  1501. case 3: this.w = value; break;
  1502. default: throw new Error( 'index is out of range: ' + index );
  1503. }
  1504. return this;
  1505. }
  1506. getComponent( index ) {
  1507. switch ( index ) {
  1508. case 0: return this.x;
  1509. case 1: return this.y;
  1510. case 2: return this.z;
  1511. case 3: return this.w;
  1512. default: throw new Error( 'index is out of range: ' + index );
  1513. }
  1514. }
  1515. clone() {
  1516. return new this.constructor( this.x, this.y, this.z, this.w );
  1517. }
  1518. copy( v ) {
  1519. this.x = v.x;
  1520. this.y = v.y;
  1521. this.z = v.z;
  1522. this.w = ( v.w !== undefined ) ? v.w : 1;
  1523. return this;
  1524. }
  1525. add( v ) {
  1526. this.x += v.x;
  1527. this.y += v.y;
  1528. this.z += v.z;
  1529. this.w += v.w;
  1530. return this;
  1531. }
  1532. addScalar( s ) {
  1533. this.x += s;
  1534. this.y += s;
  1535. this.z += s;
  1536. this.w += s;
  1537. return this;
  1538. }
  1539. addVectors( a, b ) {
  1540. this.x = a.x + b.x;
  1541. this.y = a.y + b.y;
  1542. this.z = a.z + b.z;
  1543. this.w = a.w + b.w;
  1544. return this;
  1545. }
  1546. addScaledVector( v, s ) {
  1547. this.x += v.x * s;
  1548. this.y += v.y * s;
  1549. this.z += v.z * s;
  1550. this.w += v.w * s;
  1551. return this;
  1552. }
  1553. sub( v ) {
  1554. this.x -= v.x;
  1555. this.y -= v.y;
  1556. this.z -= v.z;
  1557. this.w -= v.w;
  1558. return this;
  1559. }
  1560. subScalar( s ) {
  1561. this.x -= s;
  1562. this.y -= s;
  1563. this.z -= s;
  1564. this.w -= s;
  1565. return this;
  1566. }
  1567. subVectors( a, b ) {
  1568. this.x = a.x - b.x;
  1569. this.y = a.y - b.y;
  1570. this.z = a.z - b.z;
  1571. this.w = a.w - b.w;
  1572. return this;
  1573. }
  1574. multiply( v ) {
  1575. this.x *= v.x;
  1576. this.y *= v.y;
  1577. this.z *= v.z;
  1578. this.w *= v.w;
  1579. return this;
  1580. }
  1581. multiplyScalar( scalar ) {
  1582. this.x *= scalar;
  1583. this.y *= scalar;
  1584. this.z *= scalar;
  1585. this.w *= scalar;
  1586. return this;
  1587. }
  1588. applyMatrix4( m ) {
  1589. const x = this.x, y = this.y, z = this.z, w = this.w;
  1590. const e = m.elements;
  1591. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w;
  1592. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w;
  1593. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w;
  1594. this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
  1595. return this;
  1596. }
  1597. divide( v ) {
  1598. this.x /= v.x;
  1599. this.y /= v.y;
  1600. this.z /= v.z;
  1601. this.w /= v.w;
  1602. return this;
  1603. }
  1604. divideScalar( scalar ) {
  1605. return this.multiplyScalar( 1 / scalar );
  1606. }
  1607. setAxisAngleFromQuaternion( q ) {
  1608. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
  1609. // q is assumed to be normalized
  1610. this.w = 2 * Math.acos( q.w );
  1611. const s = Math.sqrt( 1 - q.w * q.w );
  1612. if ( s < 0.0001 ) {
  1613. this.x = 1;
  1614. this.y = 0;
  1615. this.z = 0;
  1616. } else {
  1617. this.x = q.x / s;
  1618. this.y = q.y / s;
  1619. this.z = q.z / s;
  1620. }
  1621. return this;
  1622. }
  1623. setAxisAngleFromRotationMatrix( m ) {
  1624. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
  1625. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  1626. let angle, x, y, z; // variables for result
  1627. const epsilon = 0.01, // margin to allow for rounding errors
  1628. epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees
  1629. te = m.elements,
  1630. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  1631. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  1632. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  1633. if ( ( Math.abs( m12 - m21 ) < epsilon ) &&
  1634. ( Math.abs( m13 - m31 ) < epsilon ) &&
  1635. ( Math.abs( m23 - m32 ) < epsilon ) ) {
  1636. // singularity found
  1637. // first check for identity matrix which must have +1 for all terms
  1638. // in leading diagonal and zero in other terms
  1639. if ( ( Math.abs( m12 + m21 ) < epsilon2 ) &&
  1640. ( Math.abs( m13 + m31 ) < epsilon2 ) &&
  1641. ( Math.abs( m23 + m32 ) < epsilon2 ) &&
  1642. ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
  1643. // this singularity is identity matrix so angle = 0
  1644. this.set( 1, 0, 0, 0 );
  1645. return this; // zero angle, arbitrary axis
  1646. }
  1647. // otherwise this singularity is angle = 180
  1648. angle = Math.PI;
  1649. const xx = ( m11 + 1 ) / 2;
  1650. const yy = ( m22 + 1 ) / 2;
  1651. const zz = ( m33 + 1 ) / 2;
  1652. const xy = ( m12 + m21 ) / 4;
  1653. const xz = ( m13 + m31 ) / 4;
  1654. const yz = ( m23 + m32 ) / 4;
  1655. if ( ( xx > yy ) && ( xx > zz ) ) {
  1656. // m11 is the largest diagonal term
  1657. if ( xx < epsilon ) {
  1658. x = 0;
  1659. y = 0.707106781;
  1660. z = 0.707106781;
  1661. } else {
  1662. x = Math.sqrt( xx );
  1663. y = xy / x;
  1664. z = xz / x;
  1665. }
  1666. } else if ( yy > zz ) {
  1667. // m22 is the largest diagonal term
  1668. if ( yy < epsilon ) {
  1669. x = 0.707106781;
  1670. y = 0;
  1671. z = 0.707106781;
  1672. } else {
  1673. y = Math.sqrt( yy );
  1674. x = xy / y;
  1675. z = yz / y;
  1676. }
  1677. } else {
  1678. // m33 is the largest diagonal term so base result on this
  1679. if ( zz < epsilon ) {
  1680. x = 0.707106781;
  1681. y = 0.707106781;
  1682. z = 0;
  1683. } else {
  1684. z = Math.sqrt( zz );
  1685. x = xz / z;
  1686. y = yz / z;
  1687. }
  1688. }
  1689. this.set( x, y, z, angle );
  1690. return this; // return 180 deg rotation
  1691. }
  1692. // as we have reached here there are no singularities so we can handle normally
  1693. let s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) +
  1694. ( m13 - m31 ) * ( m13 - m31 ) +
  1695. ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
  1696. if ( Math.abs( s ) < 0.001 ) s = 1;
  1697. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  1698. // caught by singularity test above, but I've left it in just in case
  1699. this.x = ( m32 - m23 ) / s;
  1700. this.y = ( m13 - m31 ) / s;
  1701. this.z = ( m21 - m12 ) / s;
  1702. this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
  1703. return this;
  1704. }
  1705. setFromMatrixPosition( m ) {
  1706. const e = m.elements;
  1707. this.x = e[ 12 ];
  1708. this.y = e[ 13 ];
  1709. this.z = e[ 14 ];
  1710. this.w = e[ 15 ];
  1711. return this;
  1712. }
  1713. min( v ) {
  1714. this.x = Math.min( this.x, v.x );
  1715. this.y = Math.min( this.y, v.y );
  1716. this.z = Math.min( this.z, v.z );
  1717. this.w = Math.min( this.w, v.w );
  1718. return this;
  1719. }
  1720. max( v ) {
  1721. this.x = Math.max( this.x, v.x );
  1722. this.y = Math.max( this.y, v.y );
  1723. this.z = Math.max( this.z, v.z );
  1724. this.w = Math.max( this.w, v.w );
  1725. return this;
  1726. }
  1727. clamp( min, max ) {
  1728. // assumes min < max, componentwise
  1729. this.x = clamp( this.x, min.x, max.x );
  1730. this.y = clamp( this.y, min.y, max.y );
  1731. this.z = clamp( this.z, min.z, max.z );
  1732. this.w = clamp( this.w, min.w, max.w );
  1733. return this;
  1734. }
  1735. clampScalar( minVal, maxVal ) {
  1736. this.x = clamp( this.x, minVal, maxVal );
  1737. this.y = clamp( this.y, minVal, maxVal );
  1738. this.z = clamp( this.z, minVal, maxVal );
  1739. this.w = clamp( this.w, minVal, maxVal );
  1740. return this;
  1741. }
  1742. clampLength( min, max ) {
  1743. const length = this.length();
  1744. return this.divideScalar( length || 1 ).multiplyScalar( clamp( length, min, max ) );
  1745. }
  1746. floor() {
  1747. this.x = Math.floor( this.x );
  1748. this.y = Math.floor( this.y );
  1749. this.z = Math.floor( this.z );
  1750. this.w = Math.floor( this.w );
  1751. return this;
  1752. }
  1753. ceil() {
  1754. this.x = Math.ceil( this.x );
  1755. this.y = Math.ceil( this.y );
  1756. this.z = Math.ceil( this.z );
  1757. this.w = Math.ceil( this.w );
  1758. return this;
  1759. }
  1760. round() {
  1761. this.x = Math.round( this.x );
  1762. this.y = Math.round( this.y );
  1763. this.z = Math.round( this.z );
  1764. this.w = Math.round( this.w );
  1765. return this;
  1766. }
  1767. roundToZero() {
  1768. this.x = Math.trunc( this.x );
  1769. this.y = Math.trunc( this.y );
  1770. this.z = Math.trunc( this.z );
  1771. this.w = Math.trunc( this.w );
  1772. return this;
  1773. }
  1774. negate() {
  1775. this.x = - this.x;
  1776. this.y = - this.y;
  1777. this.z = - this.z;
  1778. this.w = - this.w;
  1779. return this;
  1780. }
  1781. dot( v ) {
  1782. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  1783. }
  1784. lengthSq() {
  1785. return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  1786. }
  1787. length() {
  1788. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  1789. }
  1790. manhattanLength() {
  1791. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
  1792. }
  1793. normalize() {
  1794. return this.divideScalar( this.length() || 1 );
  1795. }
  1796. setLength( length ) {
  1797. return this.normalize().multiplyScalar( length );
  1798. }
  1799. lerp( v, alpha ) {
  1800. this.x += ( v.x - this.x ) * alpha;
  1801. this.y += ( v.y - this.y ) * alpha;
  1802. this.z += ( v.z - this.z ) * alpha;
  1803. this.w += ( v.w - this.w ) * alpha;
  1804. return this;
  1805. }
  1806. lerpVectors( v1, v2, alpha ) {
  1807. this.x = v1.x + ( v2.x - v1.x ) * alpha;
  1808. this.y = v1.y + ( v2.y - v1.y ) * alpha;
  1809. this.z = v1.z + ( v2.z - v1.z ) * alpha;
  1810. this.w = v1.w + ( v2.w - v1.w ) * alpha;
  1811. return this;
  1812. }
  1813. equals( v ) {
  1814. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
  1815. }
  1816. fromArray( array, offset = 0 ) {
  1817. this.x = array[ offset ];
  1818. this.y = array[ offset + 1 ];
  1819. this.z = array[ offset + 2 ];
  1820. this.w = array[ offset + 3 ];
  1821. return this;
  1822. }
  1823. toArray( array = [], offset = 0 ) {
  1824. array[ offset ] = this.x;
  1825. array[ offset + 1 ] = this.y;
  1826. array[ offset + 2 ] = this.z;
  1827. array[ offset + 3 ] = this.w;
  1828. return array;
  1829. }
  1830. fromBufferAttribute( attribute, index ) {
  1831. this.x = attribute.getX( index );
  1832. this.y = attribute.getY( index );
  1833. this.z = attribute.getZ( index );
  1834. this.w = attribute.getW( index );
  1835. return this;
  1836. }
  1837. random() {
  1838. this.x = Math.random();
  1839. this.y = Math.random();
  1840. this.z = Math.random();
  1841. this.w = Math.random();
  1842. return this;
  1843. }
  1844. *[ Symbol.iterator ]() {
  1845. yield this.x;
  1846. yield this.y;
  1847. yield this.z;
  1848. yield this.w;
  1849. }
  1850. }
  1851. /*
  1852. In options, we can specify:
  1853. * Texture parameters for an auto-generated target texture
  1854. * depthBuffer/stencilBuffer: Booleans to indicate if we should generate these buffers
  1855. */
  1856. class RenderTarget extends EventDispatcher {
  1857. constructor( width = 1, height = 1, options = {} ) {
  1858. super();
  1859. this.isRenderTarget = true;
  1860. this.width = width;
  1861. this.height = height;
  1862. this.depth = 1;
  1863. this.scissor = new Vector4( 0, 0, width, height );
  1864. this.scissorTest = false;
  1865. this.viewport = new Vector4( 0, 0, width, height );
  1866. const image = { width: width, height: height, depth: 1 };
  1867. options = Object.assign( {
  1868. generateMipmaps: false,
  1869. internalFormat: null,
  1870. minFilter: LinearFilter,
  1871. depthBuffer: true,
  1872. stencilBuffer: false,
  1873. resolveDepthBuffer: true,
  1874. resolveStencilBuffer: true,
  1875. depthTexture: null,
  1876. samples: 0,
  1877. count: 1
  1878. }, options );
  1879. const texture = new Texture( image, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace );
  1880. texture.flipY = false;
  1881. texture.generateMipmaps = options.generateMipmaps;
  1882. texture.internalFormat = options.internalFormat;
  1883. this.textures = [];
  1884. const count = options.count;
  1885. for ( let i = 0; i < count; i ++ ) {
  1886. this.textures[ i ] = texture.clone();
  1887. this.textures[ i ].isRenderTargetTexture = true;
  1888. }
  1889. this.depthBuffer = options.depthBuffer;
  1890. this.stencilBuffer = options.stencilBuffer;
  1891. this.resolveDepthBuffer = options.resolveDepthBuffer;
  1892. this.resolveStencilBuffer = options.resolveStencilBuffer;
  1893. this.depthTexture = options.depthTexture;
  1894. this.samples = options.samples;
  1895. }
  1896. get texture() {
  1897. return this.textures[ 0 ];
  1898. }
  1899. set texture( value ) {
  1900. this.textures[ 0 ] = value;
  1901. }
  1902. setSize( width, height, depth = 1 ) {
  1903. if ( this.width !== width || this.height !== height || this.depth !== depth ) {
  1904. this.width = width;
  1905. this.height = height;
  1906. this.depth = depth;
  1907. for ( let i = 0, il = this.textures.length; i < il; i ++ ) {
  1908. this.textures[ i ].image.width = width;
  1909. this.textures[ i ].image.height = height;
  1910. this.textures[ i ].image.depth = depth;
  1911. }
  1912. this.dispose();
  1913. }
  1914. this.viewport.set( 0, 0, width, height );
  1915. this.scissor.set( 0, 0, width, height );
  1916. }
  1917. clone() {
  1918. return new this.constructor().copy( this );
  1919. }
  1920. copy( source ) {
  1921. this.width = source.width;
  1922. this.height = source.height;
  1923. this.depth = source.depth;
  1924. this.scissor.copy( source.scissor );
  1925. this.scissorTest = source.scissorTest;
  1926. this.viewport.copy( source.viewport );
  1927. this.textures.length = 0;
  1928. for ( let i = 0, il = source.textures.length; i < il; i ++ ) {
  1929. this.textures[ i ] = source.textures[ i ].clone();
  1930. this.textures[ i ].isRenderTargetTexture = true;
  1931. }
  1932. // ensure image object is not shared, see #20328
  1933. const image = Object.assign( {}, source.texture.image );
  1934. this.texture.source = new Source( image );
  1935. this.depthBuffer = source.depthBuffer;
  1936. this.stencilBuffer = source.stencilBuffer;
  1937. this.resolveDepthBuffer = source.resolveDepthBuffer;
  1938. this.resolveStencilBuffer = source.resolveStencilBuffer;
  1939. if ( source.depthTexture !== null ) this.depthTexture = source.depthTexture.clone();
  1940. this.samples = source.samples;
  1941. return this;
  1942. }
  1943. dispose() {
  1944. this.dispatchEvent( { type: 'dispose' } );
  1945. }
  1946. }
  1947. class WebGLRenderTarget extends RenderTarget {
  1948. constructor( width = 1, height = 1, options = {} ) {
  1949. super( width, height, options );
  1950. this.isWebGLRenderTarget = true;
  1951. }
  1952. }
  1953. class DataArrayTexture extends Texture {
  1954. constructor( data = null, width = 1, height = 1, depth = 1 ) {
  1955. super( null );
  1956. this.isDataArrayTexture = true;
  1957. this.image = { data, width, height, depth };
  1958. this.magFilter = NearestFilter;
  1959. this.minFilter = NearestFilter;
  1960. this.wrapR = ClampToEdgeWrapping;
  1961. this.generateMipmaps = false;
  1962. this.flipY = false;
  1963. this.unpackAlignment = 1;
  1964. this.layerUpdates = new Set();
  1965. }
  1966. addLayerUpdate( layerIndex ) {
  1967. this.layerUpdates.add( layerIndex );
  1968. }
  1969. clearLayerUpdates() {
  1970. this.layerUpdates.clear();
  1971. }
  1972. }
  1973. class WebGLArrayRenderTarget extends WebGLRenderTarget {
  1974. constructor( width = 1, height = 1, depth = 1, options = {} ) {
  1975. super( width, height, options );
  1976. this.isWebGLArrayRenderTarget = true;
  1977. this.depth = depth;
  1978. this.texture = new DataArrayTexture( null, width, height, depth );
  1979. this.texture.isRenderTargetTexture = true;
  1980. }
  1981. }
  1982. class Data3DTexture extends Texture {
  1983. constructor( data = null, width = 1, height = 1, depth = 1 ) {
  1984. // We're going to add .setXXX() methods for setting properties later.
  1985. // Users can still set in DataTexture3D directly.
  1986. //
  1987. // const texture = new THREE.DataTexture3D( data, width, height, depth );
  1988. // texture.anisotropy = 16;
  1989. //
  1990. // See #14839
  1991. super( null );
  1992. this.isData3DTexture = true;
  1993. this.image = { data, width, height, depth };
  1994. this.magFilter = NearestFilter;
  1995. this.minFilter = NearestFilter;
  1996. this.wrapR = ClampToEdgeWrapping;
  1997. this.generateMipmaps = false;
  1998. this.flipY = false;
  1999. this.unpackAlignment = 1;
  2000. }
  2001. }
  2002. class WebGL3DRenderTarget extends WebGLRenderTarget {
  2003. constructor( width = 1, height = 1, depth = 1, options = {} ) {
  2004. super( width, height, options );
  2005. this.isWebGL3DRenderTarget = true;
  2006. this.depth = depth;
  2007. this.texture = new Data3DTexture( null, width, height, depth );
  2008. this.texture.isRenderTargetTexture = true;
  2009. }
  2010. }
  2011. class Quaternion {
  2012. constructor( x = 0, y = 0, z = 0, w = 1 ) {
  2013. this.isQuaternion = true;
  2014. this._x = x;
  2015. this._y = y;
  2016. this._z = z;
  2017. this._w = w;
  2018. }
  2019. static slerpFlat( dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t ) {
  2020. // fuzz-free, array-based Quaternion SLERP operation
  2021. let x0 = src0[ srcOffset0 + 0 ],
  2022. y0 = src0[ srcOffset0 + 1 ],
  2023. z0 = src0[ srcOffset0 + 2 ],
  2024. w0 = src0[ srcOffset0 + 3 ];
  2025. const x1 = src1[ srcOffset1 + 0 ],
  2026. y1 = src1[ srcOffset1 + 1 ],
  2027. z1 = src1[ srcOffset1 + 2 ],
  2028. w1 = src1[ srcOffset1 + 3 ];
  2029. if ( t === 0 ) {
  2030. dst[ dstOffset + 0 ] = x0;
  2031. dst[ dstOffset + 1 ] = y0;
  2032. dst[ dstOffset + 2 ] = z0;
  2033. dst[ dstOffset + 3 ] = w0;
  2034. return;
  2035. }
  2036. if ( t === 1 ) {
  2037. dst[ dstOffset + 0 ] = x1;
  2038. dst[ dstOffset + 1 ] = y1;
  2039. dst[ dstOffset + 2 ] = z1;
  2040. dst[ dstOffset + 3 ] = w1;
  2041. return;
  2042. }
  2043. if ( w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1 ) {
  2044. let s = 1 - t;
  2045. const cos = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1,
  2046. dir = ( cos >= 0 ? 1 : - 1 ),
  2047. sqrSin = 1 - cos * cos;
  2048. // Skip the Slerp for tiny steps to avoid numeric problems:
  2049. if ( sqrSin > Number.EPSILON ) {
  2050. const sin = Math.sqrt( sqrSin ),
  2051. len = Math.atan2( sin, cos * dir );
  2052. s = Math.sin( s * len ) / sin;
  2053. t = Math.sin( t * len ) / sin;
  2054. }
  2055. const tDir = t * dir;
  2056. x0 = x0 * s + x1 * tDir;
  2057. y0 = y0 * s + y1 * tDir;
  2058. z0 = z0 * s + z1 * tDir;
  2059. w0 = w0 * s + w1 * tDir;
  2060. // Normalize in case we just did a lerp:
  2061. if ( s === 1 - t ) {
  2062. const f = 1 / Math.sqrt( x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0 );
  2063. x0 *= f;
  2064. y0 *= f;
  2065. z0 *= f;
  2066. w0 *= f;
  2067. }
  2068. }
  2069. dst[ dstOffset ] = x0;
  2070. dst[ dstOffset + 1 ] = y0;
  2071. dst[ dstOffset + 2 ] = z0;
  2072. dst[ dstOffset + 3 ] = w0;
  2073. }
  2074. static multiplyQuaternionsFlat( dst, dstOffset, src0, srcOffset0, src1, srcOffset1 ) {
  2075. const x0 = src0[ srcOffset0 ];
  2076. const y0 = src0[ srcOffset0 + 1 ];
  2077. const z0 = src0[ srcOffset0 + 2 ];
  2078. const w0 = src0[ srcOffset0 + 3 ];
  2079. const x1 = src1[ srcOffset1 ];
  2080. const y1 = src1[ srcOffset1 + 1 ];
  2081. const z1 = src1[ srcOffset1 + 2 ];
  2082. const w1 = src1[ srcOffset1 + 3 ];
  2083. dst[ dstOffset ] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1;
  2084. dst[ dstOffset + 1 ] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1;
  2085. dst[ dstOffset + 2 ] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1;
  2086. dst[ dstOffset + 3 ] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1;
  2087. return dst;
  2088. }
  2089. get x() {
  2090. return this._x;
  2091. }
  2092. set x( value ) {
  2093. this._x = value;
  2094. this._onChangeCallback();
  2095. }
  2096. get y() {
  2097. return this._y;
  2098. }
  2099. set y( value ) {
  2100. this._y = value;
  2101. this._onChangeCallback();
  2102. }
  2103. get z() {
  2104. return this._z;
  2105. }
  2106. set z( value ) {
  2107. this._z = value;
  2108. this._onChangeCallback();
  2109. }
  2110. get w() {
  2111. return this._w;
  2112. }
  2113. set w( value ) {
  2114. this._w = value;
  2115. this._onChangeCallback();
  2116. }
  2117. set( x, y, z, w ) {
  2118. this._x = x;
  2119. this._y = y;
  2120. this._z = z;
  2121. this._w = w;
  2122. this._onChangeCallback();
  2123. return this;
  2124. }
  2125. clone() {
  2126. return new this.constructor( this._x, this._y, this._z, this._w );
  2127. }
  2128. copy( quaternion ) {
  2129. this._x = quaternion.x;
  2130. this._y = quaternion.y;
  2131. this._z = quaternion.z;
  2132. this._w = quaternion.w;
  2133. this._onChangeCallback();
  2134. return this;
  2135. }
  2136. setFromEuler( euler, update = true ) {
  2137. const x = euler._x, y = euler._y, z = euler._z, order = euler._order;
  2138. // http://www.mathworks.com/matlabcentral/fileexchange/
  2139. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  2140. // content/SpinCalc.m
  2141. const cos = Math.cos;
  2142. const sin = Math.sin;
  2143. const c1 = cos( x / 2 );
  2144. const c2 = cos( y / 2 );
  2145. const c3 = cos( z / 2 );
  2146. const s1 = sin( x / 2 );
  2147. const s2 = sin( y / 2 );
  2148. const s3 = sin( z / 2 );
  2149. switch ( order ) {
  2150. case 'XYZ':
  2151. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  2152. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  2153. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  2154. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  2155. break;
  2156. case 'YXZ':
  2157. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  2158. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  2159. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  2160. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  2161. break;
  2162. case 'ZXY':
  2163. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  2164. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  2165. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  2166. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  2167. break;
  2168. case 'ZYX':
  2169. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  2170. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  2171. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  2172. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  2173. break;
  2174. case 'YZX':
  2175. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  2176. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  2177. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  2178. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  2179. break;
  2180. case 'XZY':
  2181. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  2182. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  2183. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  2184. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  2185. break;
  2186. default:
  2187. console.warn( 'THREE.Quaternion: .setFromEuler() encountered an unknown order: ' + order );
  2188. }
  2189. if ( update === true ) this._onChangeCallback();
  2190. return this;
  2191. }
  2192. setFromAxisAngle( axis, angle ) {
  2193. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  2194. // assumes axis is normalized
  2195. const halfAngle = angle / 2, s = Math.sin( halfAngle );
  2196. this._x = axis.x * s;
  2197. this._y = axis.y * s;
  2198. this._z = axis.z * s;
  2199. this._w = Math.cos( halfAngle );
  2200. this._onChangeCallback();
  2201. return this;
  2202. }
  2203. setFromRotationMatrix( m ) {
  2204. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  2205. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  2206. const te = m.elements,
  2207. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  2208. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  2209. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
  2210. trace = m11 + m22 + m33;
  2211. if ( trace > 0 ) {
  2212. const s = 0.5 / Math.sqrt( trace + 1.0 );
  2213. this._w = 0.25 / s;
  2214. this._x = ( m32 - m23 ) * s;
  2215. this._y = ( m13 - m31 ) * s;
  2216. this._z = ( m21 - m12 ) * s;
  2217. } else if ( m11 > m22 && m11 > m33 ) {
  2218. const s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  2219. this._w = ( m32 - m23 ) / s;
  2220. this._x = 0.25 * s;
  2221. this._y = ( m12 + m21 ) / s;
  2222. this._z = ( m13 + m31 ) / s;
  2223. } else if ( m22 > m33 ) {
  2224. const s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  2225. this._w = ( m13 - m31 ) / s;
  2226. this._x = ( m12 + m21 ) / s;
  2227. this._y = 0.25 * s;
  2228. this._z = ( m23 + m32 ) / s;
  2229. } else {
  2230. const s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  2231. this._w = ( m21 - m12 ) / s;
  2232. this._x = ( m13 + m31 ) / s;
  2233. this._y = ( m23 + m32 ) / s;
  2234. this._z = 0.25 * s;
  2235. }
  2236. this._onChangeCallback();
  2237. return this;
  2238. }
  2239. setFromUnitVectors( vFrom, vTo ) {
  2240. // assumes direction vectors vFrom and vTo are normalized
  2241. let r = vFrom.dot( vTo ) + 1;
  2242. if ( r < Number.EPSILON ) {
  2243. // vFrom and vTo point in opposite directions
  2244. r = 0;
  2245. if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
  2246. this._x = - vFrom.y;
  2247. this._y = vFrom.x;
  2248. this._z = 0;
  2249. this._w = r;
  2250. } else {
  2251. this._x = 0;
  2252. this._y = - vFrom.z;
  2253. this._z = vFrom.y;
  2254. this._w = r;
  2255. }
  2256. } else {
  2257. // crossVectors( vFrom, vTo ); // inlined to avoid cyclic dependency on Vector3
  2258. this._x = vFrom.y * vTo.z - vFrom.z * vTo.y;
  2259. this._y = vFrom.z * vTo.x - vFrom.x * vTo.z;
  2260. this._z = vFrom.x * vTo.y - vFrom.y * vTo.x;
  2261. this._w = r;
  2262. }
  2263. return this.normalize();
  2264. }
  2265. angleTo( q ) {
  2266. return 2 * Math.acos( Math.abs( clamp( this.dot( q ), - 1, 1 ) ) );
  2267. }
  2268. rotateTowards( q, step ) {
  2269. const angle = this.angleTo( q );
  2270. if ( angle === 0 ) return this;
  2271. const t = Math.min( 1, step / angle );
  2272. this.slerp( q, t );
  2273. return this;
  2274. }
  2275. identity() {
  2276. return this.set( 0, 0, 0, 1 );
  2277. }
  2278. invert() {
  2279. // quaternion is assumed to have unit length
  2280. return this.conjugate();
  2281. }
  2282. conjugate() {
  2283. this._x *= - 1;
  2284. this._y *= - 1;
  2285. this._z *= - 1;
  2286. this._onChangeCallback();
  2287. return this;
  2288. }
  2289. dot( v ) {
  2290. return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
  2291. }
  2292. lengthSq() {
  2293. return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
  2294. }
  2295. length() {
  2296. return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
  2297. }
  2298. normalize() {
  2299. let l = this.length();
  2300. if ( l === 0 ) {
  2301. this._x = 0;
  2302. this._y = 0;
  2303. this._z = 0;
  2304. this._w = 1;
  2305. } else {
  2306. l = 1 / l;
  2307. this._x = this._x * l;
  2308. this._y = this._y * l;
  2309. this._z = this._z * l;
  2310. this._w = this._w * l;
  2311. }
  2312. this._onChangeCallback();
  2313. return this;
  2314. }
  2315. multiply( q ) {
  2316. return this.multiplyQuaternions( this, q );
  2317. }
  2318. premultiply( q ) {
  2319. return this.multiplyQuaternions( q, this );
  2320. }
  2321. multiplyQuaternions( a, b ) {
  2322. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  2323. const qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
  2324. const qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
  2325. this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  2326. this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  2327. this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  2328. this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  2329. this._onChangeCallback();
  2330. return this;
  2331. }
  2332. slerp( qb, t ) {
  2333. if ( t === 0 ) return this;
  2334. if ( t === 1 ) return this.copy( qb );
  2335. const x = this._x, y = this._y, z = this._z, w = this._w;
  2336. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  2337. let cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
  2338. if ( cosHalfTheta < 0 ) {
  2339. this._w = - qb._w;
  2340. this._x = - qb._x;
  2341. this._y = - qb._y;
  2342. this._z = - qb._z;
  2343. cosHalfTheta = - cosHalfTheta;
  2344. } else {
  2345. this.copy( qb );
  2346. }
  2347. if ( cosHalfTheta >= 1.0 ) {
  2348. this._w = w;
  2349. this._x = x;
  2350. this._y = y;
  2351. this._z = z;
  2352. return this;
  2353. }
  2354. const sqrSinHalfTheta = 1.0 - cosHalfTheta * cosHalfTheta;
  2355. if ( sqrSinHalfTheta <= Number.EPSILON ) {
  2356. const s = 1 - t;
  2357. this._w = s * w + t * this._w;
  2358. this._x = s * x + t * this._x;
  2359. this._y = s * y + t * this._y;
  2360. this._z = s * z + t * this._z;
  2361. this.normalize(); // normalize calls _onChangeCallback()
  2362. return this;
  2363. }
  2364. const sinHalfTheta = Math.sqrt( sqrSinHalfTheta );
  2365. const halfTheta = Math.atan2( sinHalfTheta, cosHalfTheta );
  2366. const ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  2367. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  2368. this._w = ( w * ratioA + this._w * ratioB );
  2369. this._x = ( x * ratioA + this._x * ratioB );
  2370. this._y = ( y * ratioA + this._y * ratioB );
  2371. this._z = ( z * ratioA + this._z * ratioB );
  2372. this._onChangeCallback();
  2373. return this;
  2374. }
  2375. slerpQuaternions( qa, qb, t ) {
  2376. return this.copy( qa ).slerp( qb, t );
  2377. }
  2378. random() {
  2379. // sets this quaternion to a uniform random unit quaternnion
  2380. // Ken Shoemake
  2381. // Uniform random rotations
  2382. // D. Kirk, editor, Graphics Gems III, pages 124-132. Academic Press, New York, 1992.
  2383. const theta1 = 2 * Math.PI * Math.random();
  2384. const theta2 = 2 * Math.PI * Math.random();
  2385. const x0 = Math.random();
  2386. const r1 = Math.sqrt( 1 - x0 );
  2387. const r2 = Math.sqrt( x0 );
  2388. return this.set(
  2389. r1 * Math.sin( theta1 ),
  2390. r1 * Math.cos( theta1 ),
  2391. r2 * Math.sin( theta2 ),
  2392. r2 * Math.cos( theta2 ),
  2393. );
  2394. }
  2395. equals( quaternion ) {
  2396. return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
  2397. }
  2398. fromArray( array, offset = 0 ) {
  2399. this._x = array[ offset ];
  2400. this._y = array[ offset + 1 ];
  2401. this._z = array[ offset + 2 ];
  2402. this._w = array[ offset + 3 ];
  2403. this._onChangeCallback();
  2404. return this;
  2405. }
  2406. toArray( array = [], offset = 0 ) {
  2407. array[ offset ] = this._x;
  2408. array[ offset + 1 ] = this._y;
  2409. array[ offset + 2 ] = this._z;
  2410. array[ offset + 3 ] = this._w;
  2411. return array;
  2412. }
  2413. fromBufferAttribute( attribute, index ) {
  2414. this._x = attribute.getX( index );
  2415. this._y = attribute.getY( index );
  2416. this._z = attribute.getZ( index );
  2417. this._w = attribute.getW( index );
  2418. this._onChangeCallback();
  2419. return this;
  2420. }
  2421. toJSON() {
  2422. return this.toArray();
  2423. }
  2424. _onChange( callback ) {
  2425. this._onChangeCallback = callback;
  2426. return this;
  2427. }
  2428. _onChangeCallback() {}
  2429. *[ Symbol.iterator ]() {
  2430. yield this._x;
  2431. yield this._y;
  2432. yield this._z;
  2433. yield this._w;
  2434. }
  2435. }
  2436. class Vector3 {
  2437. constructor( x = 0, y = 0, z = 0 ) {
  2438. Vector3.prototype.isVector3 = true;
  2439. this.x = x;
  2440. this.y = y;
  2441. this.z = z;
  2442. }
  2443. set( x, y, z ) {
  2444. if ( z === undefined ) z = this.z; // sprite.scale.set(x,y)
  2445. this.x = x;
  2446. this.y = y;
  2447. this.z = z;
  2448. return this;
  2449. }
  2450. setScalar( scalar ) {
  2451. this.x = scalar;
  2452. this.y = scalar;
  2453. this.z = scalar;
  2454. return this;
  2455. }
  2456. setX( x ) {
  2457. this.x = x;
  2458. return this;
  2459. }
  2460. setY( y ) {
  2461. this.y = y;
  2462. return this;
  2463. }
  2464. setZ( z ) {
  2465. this.z = z;
  2466. return this;
  2467. }
  2468. setComponent( index, value ) {
  2469. switch ( index ) {
  2470. case 0: this.x = value; break;
  2471. case 1: this.y = value; break;
  2472. case 2: this.z = value; break;
  2473. default: throw new Error( 'index is out of range: ' + index );
  2474. }
  2475. return this;
  2476. }
  2477. getComponent( index ) {
  2478. switch ( index ) {
  2479. case 0: return this.x;
  2480. case 1: return this.y;
  2481. case 2: return this.z;
  2482. default: throw new Error( 'index is out of range: ' + index );
  2483. }
  2484. }
  2485. clone() {
  2486. return new this.constructor( this.x, this.y, this.z );
  2487. }
  2488. copy( v ) {
  2489. this.x = v.x;
  2490. this.y = v.y;
  2491. this.z = v.z;
  2492. return this;
  2493. }
  2494. add( v ) {
  2495. this.x += v.x;
  2496. this.y += v.y;
  2497. this.z += v.z;
  2498. return this;
  2499. }
  2500. addScalar( s ) {
  2501. this.x += s;
  2502. this.y += s;
  2503. this.z += s;
  2504. return this;
  2505. }
  2506. addVectors( a, b ) {
  2507. this.x = a.x + b.x;
  2508. this.y = a.y + b.y;
  2509. this.z = a.z + b.z;
  2510. return this;
  2511. }
  2512. addScaledVector( v, s ) {
  2513. this.x += v.x * s;
  2514. this.y += v.y * s;
  2515. this.z += v.z * s;
  2516. return this;
  2517. }
  2518. sub( v ) {
  2519. this.x -= v.x;
  2520. this.y -= v.y;
  2521. this.z -= v.z;
  2522. return this;
  2523. }
  2524. subScalar( s ) {
  2525. this.x -= s;
  2526. this.y -= s;
  2527. this.z -= s;
  2528. return this;
  2529. }
  2530. subVectors( a, b ) {
  2531. this.x = a.x - b.x;
  2532. this.y = a.y - b.y;
  2533. this.z = a.z - b.z;
  2534. return this;
  2535. }
  2536. multiply( v ) {
  2537. this.x *= v.x;
  2538. this.y *= v.y;
  2539. this.z *= v.z;
  2540. return this;
  2541. }
  2542. multiplyScalar( scalar ) {
  2543. this.x *= scalar;
  2544. this.y *= scalar;
  2545. this.z *= scalar;
  2546. return this;
  2547. }
  2548. multiplyVectors( a, b ) {
  2549. this.x = a.x * b.x;
  2550. this.y = a.y * b.y;
  2551. this.z = a.z * b.z;
  2552. return this;
  2553. }
  2554. applyEuler( euler ) {
  2555. return this.applyQuaternion( _quaternion$4.setFromEuler( euler ) );
  2556. }
  2557. applyAxisAngle( axis, angle ) {
  2558. return this.applyQuaternion( _quaternion$4.setFromAxisAngle( axis, angle ) );
  2559. }
  2560. applyMatrix3( m ) {
  2561. const x = this.x, y = this.y, z = this.z;
  2562. const e = m.elements;
  2563. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
  2564. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
  2565. this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
  2566. return this;
  2567. }
  2568. applyNormalMatrix( m ) {
  2569. return this.applyMatrix3( m ).normalize();
  2570. }
  2571. applyMatrix4( m ) {
  2572. const x = this.x, y = this.y, z = this.z;
  2573. const e = m.elements;
  2574. const w = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] );
  2575. this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * w;
  2576. this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * w;
  2577. this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * w;
  2578. return this;
  2579. }
  2580. applyQuaternion( q ) {
  2581. // quaternion q is assumed to have unit length
  2582. const vx = this.x, vy = this.y, vz = this.z;
  2583. const qx = q.x, qy = q.y, qz = q.z, qw = q.w;
  2584. // t = 2 * cross( q.xyz, v );
  2585. const tx = 2 * ( qy * vz - qz * vy );
  2586. const ty = 2 * ( qz * vx - qx * vz );
  2587. const tz = 2 * ( qx * vy - qy * vx );
  2588. // v + q.w * t + cross( q.xyz, t );
  2589. this.x = vx + qw * tx + qy * tz - qz * ty;
  2590. this.y = vy + qw * ty + qz * tx - qx * tz;
  2591. this.z = vz + qw * tz + qx * ty - qy * tx;
  2592. return this;
  2593. }
  2594. project( camera ) {
  2595. return this.applyMatrix4( camera.matrixWorldInverse ).applyMatrix4( camera.projectionMatrix );
  2596. }
  2597. unproject( camera ) {
  2598. return this.applyMatrix4( camera.projectionMatrixInverse ).applyMatrix4( camera.matrixWorld );
  2599. }
  2600. transformDirection( m ) {
  2601. // input: THREE.Matrix4 affine matrix
  2602. // vector interpreted as a direction
  2603. const x = this.x, y = this.y, z = this.z;
  2604. const e = m.elements;
  2605. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
  2606. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
  2607. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
  2608. return this.normalize();
  2609. }
  2610. divide( v ) {
  2611. this.x /= v.x;
  2612. this.y /= v.y;
  2613. this.z /= v.z;
  2614. return this;
  2615. }
  2616. divideScalar( scalar ) {
  2617. return this.multiplyScalar( 1 / scalar );
  2618. }
  2619. min( v ) {
  2620. this.x = Math.min( this.x, v.x );
  2621. this.y = Math.min( this.y, v.y );
  2622. this.z = Math.min( this.z, v.z );
  2623. return this;
  2624. }
  2625. max( v ) {
  2626. this.x = Math.max( this.x, v.x );
  2627. this.y = Math.max( this.y, v.y );
  2628. this.z = Math.max( this.z, v.z );
  2629. return this;
  2630. }
  2631. clamp( min, max ) {
  2632. // assumes min < max, componentwise
  2633. this.x = clamp( this.x, min.x, max.x );
  2634. this.y = clamp( this.y, min.y, max.y );
  2635. this.z = clamp( this.z, min.z, max.z );
  2636. return this;
  2637. }
  2638. clampScalar( minVal, maxVal ) {
  2639. this.x = clamp( this.x, minVal, maxVal );
  2640. this.y = clamp( this.y, minVal, maxVal );
  2641. this.z = clamp( this.z, minVal, maxVal );
  2642. return this;
  2643. }
  2644. clampLength( min, max ) {
  2645. const length = this.length();
  2646. return this.divideScalar( length || 1 ).multiplyScalar( clamp( length, min, max ) );
  2647. }
  2648. floor() {
  2649. this.x = Math.floor( this.x );
  2650. this.y = Math.floor( this.y );
  2651. this.z = Math.floor( this.z );
  2652. return this;
  2653. }
  2654. ceil() {
  2655. this.x = Math.ceil( this.x );
  2656. this.y = Math.ceil( this.y );
  2657. this.z = Math.ceil( this.z );
  2658. return this;
  2659. }
  2660. round() {
  2661. this.x = Math.round( this.x );
  2662. this.y = Math.round( this.y );
  2663. this.z = Math.round( this.z );
  2664. return this;
  2665. }
  2666. roundToZero() {
  2667. this.x = Math.trunc( this.x );
  2668. this.y = Math.trunc( this.y );
  2669. this.z = Math.trunc( this.z );
  2670. return this;
  2671. }
  2672. negate() {
  2673. this.x = - this.x;
  2674. this.y = - this.y;
  2675. this.z = - this.z;
  2676. return this;
  2677. }
  2678. dot( v ) {
  2679. return this.x * v.x + this.y * v.y + this.z * v.z;
  2680. }
  2681. // TODO lengthSquared?
  2682. lengthSq() {
  2683. return this.x * this.x + this.y * this.y + this.z * this.z;
  2684. }
  2685. length() {
  2686. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  2687. }
  2688. manhattanLength() {
  2689. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  2690. }
  2691. normalize() {
  2692. return this.divideScalar( this.length() || 1 );
  2693. }
  2694. setLength( length ) {
  2695. return this.normalize().multiplyScalar( length );
  2696. }
  2697. lerp( v, alpha ) {
  2698. this.x += ( v.x - this.x ) * alpha;
  2699. this.y += ( v.y - this.y ) * alpha;
  2700. this.z += ( v.z - this.z ) * alpha;
  2701. return this;
  2702. }
  2703. lerpVectors( v1, v2, alpha ) {
  2704. this.x = v1.x + ( v2.x - v1.x ) * alpha;
  2705. this.y = v1.y + ( v2.y - v1.y ) * alpha;
  2706. this.z = v1.z + ( v2.z - v1.z ) * alpha;
  2707. return this;
  2708. }
  2709. cross( v ) {
  2710. return this.crossVectors( this, v );
  2711. }
  2712. crossVectors( a, b ) {
  2713. const ax = a.x, ay = a.y, az = a.z;
  2714. const bx = b.x, by = b.y, bz = b.z;
  2715. this.x = ay * bz - az * by;
  2716. this.y = az * bx - ax * bz;
  2717. this.z = ax * by - ay * bx;
  2718. return this;
  2719. }
  2720. projectOnVector( v ) {
  2721. const denominator = v.lengthSq();
  2722. if ( denominator === 0 ) return this.set( 0, 0, 0 );
  2723. const scalar = v.dot( this ) / denominator;
  2724. return this.copy( v ).multiplyScalar( scalar );
  2725. }
  2726. projectOnPlane( planeNormal ) {
  2727. _vector$c.copy( this ).projectOnVector( planeNormal );
  2728. return this.sub( _vector$c );
  2729. }
  2730. reflect( normal ) {
  2731. // reflect incident vector off plane orthogonal to normal
  2732. // normal is assumed to have unit length
  2733. return this.sub( _vector$c.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
  2734. }
  2735. angleTo( v ) {
  2736. const denominator = Math.sqrt( this.lengthSq() * v.lengthSq() );
  2737. if ( denominator === 0 ) return Math.PI / 2;
  2738. const theta = this.dot( v ) / denominator;
  2739. // clamp, to handle numerical problems
  2740. return Math.acos( clamp( theta, - 1, 1 ) );
  2741. }
  2742. distanceTo( v ) {
  2743. return Math.sqrt( this.distanceToSquared( v ) );
  2744. }
  2745. distanceToSquared( v ) {
  2746. const dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z;
  2747. return dx * dx + dy * dy + dz * dz;
  2748. }
  2749. manhattanDistanceTo( v ) {
  2750. return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ) + Math.abs( this.z - v.z );
  2751. }
  2752. setFromSpherical( s ) {
  2753. return this.setFromSphericalCoords( s.radius, s.phi, s.theta );
  2754. }
  2755. setFromSphericalCoords( radius, phi, theta ) {
  2756. const sinPhiRadius = Math.sin( phi ) * radius;
  2757. this.x = sinPhiRadius * Math.sin( theta );
  2758. this.y = Math.cos( phi ) * radius;
  2759. this.z = sinPhiRadius * Math.cos( theta );
  2760. return this;
  2761. }
  2762. setFromCylindrical( c ) {
  2763. return this.setFromCylindricalCoords( c.radius, c.theta, c.y );
  2764. }
  2765. setFromCylindricalCoords( radius, theta, y ) {
  2766. this.x = radius * Math.sin( theta );
  2767. this.y = y;
  2768. this.z = radius * Math.cos( theta );
  2769. return this;
  2770. }
  2771. setFromMatrixPosition( m ) {
  2772. const e = m.elements;
  2773. this.x = e[ 12 ];
  2774. this.y = e[ 13 ];
  2775. this.z = e[ 14 ];
  2776. return this;
  2777. }
  2778. setFromMatrixScale( m ) {
  2779. const sx = this.setFromMatrixColumn( m, 0 ).length();
  2780. const sy = this.setFromMatrixColumn( m, 1 ).length();
  2781. const sz = this.setFromMatrixColumn( m, 2 ).length();
  2782. this.x = sx;
  2783. this.y = sy;
  2784. this.z = sz;
  2785. return this;
  2786. }
  2787. setFromMatrixColumn( m, index ) {
  2788. return this.fromArray( m.elements, index * 4 );
  2789. }
  2790. setFromMatrix3Column( m, index ) {
  2791. return this.fromArray( m.elements, index * 3 );
  2792. }
  2793. setFromEuler( e ) {
  2794. this.x = e._x;
  2795. this.y = e._y;
  2796. this.z = e._z;
  2797. return this;
  2798. }
  2799. setFromColor( c ) {
  2800. this.x = c.r;
  2801. this.y = c.g;
  2802. this.z = c.b;
  2803. return this;
  2804. }
  2805. equals( v ) {
  2806. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  2807. }
  2808. fromArray( array, offset = 0 ) {
  2809. this.x = array[ offset ];
  2810. this.y = array[ offset + 1 ];
  2811. this.z = array[ offset + 2 ];
  2812. return this;
  2813. }
  2814. toArray( array = [], offset = 0 ) {
  2815. array[ offset ] = this.x;
  2816. array[ offset + 1 ] = this.y;
  2817. array[ offset + 2 ] = this.z;
  2818. return array;
  2819. }
  2820. fromBufferAttribute( attribute, index ) {
  2821. this.x = attribute.getX( index );
  2822. this.y = attribute.getY( index );
  2823. this.z = attribute.getZ( index );
  2824. return this;
  2825. }
  2826. random() {
  2827. this.x = Math.random();
  2828. this.y = Math.random();
  2829. this.z = Math.random();
  2830. return this;
  2831. }
  2832. randomDirection() {
  2833. // https://mathworld.wolfram.com/SpherePointPicking.html
  2834. const theta = Math.random() * Math.PI * 2;
  2835. const u = Math.random() * 2 - 1;
  2836. const c = Math.sqrt( 1 - u * u );
  2837. this.x = c * Math.cos( theta );
  2838. this.y = u;
  2839. this.z = c * Math.sin( theta );
  2840. return this;
  2841. }
  2842. *[ Symbol.iterator ]() {
  2843. yield this.x;
  2844. yield this.y;
  2845. yield this.z;
  2846. }
  2847. }
  2848. const _vector$c = /*@__PURE__*/ new Vector3();
  2849. const _quaternion$4 = /*@__PURE__*/ new Quaternion();
  2850. class Box3 {
  2851. constructor( min = new Vector3( + Infinity, + Infinity, + Infinity ), max = new Vector3( - Infinity, - Infinity, - Infinity ) ) {
  2852. this.isBox3 = true;
  2853. this.min = min;
  2854. this.max = max;
  2855. }
  2856. set( min, max ) {
  2857. this.min.copy( min );
  2858. this.max.copy( max );
  2859. return this;
  2860. }
  2861. setFromArray( array ) {
  2862. this.makeEmpty();
  2863. for ( let i = 0, il = array.length; i < il; i += 3 ) {
  2864. this.expandByPoint( _vector$b.fromArray( array, i ) );
  2865. }
  2866. return this;
  2867. }
  2868. setFromBufferAttribute( attribute ) {
  2869. this.makeEmpty();
  2870. for ( let i = 0, il = attribute.count; i < il; i ++ ) {
  2871. this.expandByPoint( _vector$b.fromBufferAttribute( attribute, i ) );
  2872. }
  2873. return this;
  2874. }
  2875. setFromPoints( points ) {
  2876. this.makeEmpty();
  2877. for ( let i = 0, il = points.length; i < il; i ++ ) {
  2878. this.expandByPoint( points[ i ] );
  2879. }
  2880. return this;
  2881. }
  2882. setFromCenterAndSize( center, size ) {
  2883. const halfSize = _vector$b.copy( size ).multiplyScalar( 0.5 );
  2884. this.min.copy( center ).sub( halfSize );
  2885. this.max.copy( center ).add( halfSize );
  2886. return this;
  2887. }
  2888. setFromObject( object, precise = false ) {
  2889. this.makeEmpty();
  2890. return this.expandByObject( object, precise );
  2891. }
  2892. clone() {
  2893. return new this.constructor().copy( this );
  2894. }
  2895. copy( box ) {
  2896. this.min.copy( box.min );
  2897. this.max.copy( box.max );
  2898. return this;
  2899. }
  2900. makeEmpty() {
  2901. this.min.x = this.min.y = this.min.z = + Infinity;
  2902. this.max.x = this.max.y = this.max.z = - Infinity;
  2903. return this;
  2904. }
  2905. isEmpty() {
  2906. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  2907. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
  2908. }
  2909. getCenter( target ) {
  2910. return this.isEmpty() ? target.set( 0, 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  2911. }
  2912. getSize( target ) {
  2913. return this.isEmpty() ? target.set( 0, 0, 0 ) : target.subVectors( this.max, this.min );
  2914. }
  2915. expandByPoint( point ) {
  2916. this.min.min( point );
  2917. this.max.max( point );
  2918. return this;
  2919. }
  2920. expandByVector( vector ) {
  2921. this.min.sub( vector );
  2922. this.max.add( vector );
  2923. return this;
  2924. }
  2925. expandByScalar( scalar ) {
  2926. this.min.addScalar( - scalar );
  2927. this.max.addScalar( scalar );
  2928. return this;
  2929. }
  2930. expandByObject( object, precise = false ) {
  2931. // Computes the world-axis-aligned bounding box of an object (including its children),
  2932. // accounting for both the object's, and children's, world transforms
  2933. object.updateWorldMatrix( false, false );
  2934. const geometry = object.geometry;
  2935. if ( geometry !== undefined ) {
  2936. const positionAttribute = geometry.getAttribute( 'position' );
  2937. // precise AABB computation based on vertex data requires at least a position attribute.
  2938. // instancing isn't supported so far and uses the normal (conservative) code path.
  2939. if ( precise === true && positionAttribute !== undefined && object.isInstancedMesh !== true ) {
  2940. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  2941. if ( object.isMesh === true ) {
  2942. object.getVertexPosition( i, _vector$b );
  2943. } else {
  2944. _vector$b.fromBufferAttribute( positionAttribute, i );
  2945. }
  2946. _vector$b.applyMatrix4( object.matrixWorld );
  2947. this.expandByPoint( _vector$b );
  2948. }
  2949. } else {
  2950. if ( object.boundingBox !== undefined ) {
  2951. // object-level bounding box
  2952. if ( object.boundingBox === null ) {
  2953. object.computeBoundingBox();
  2954. }
  2955. _box$4.copy( object.boundingBox );
  2956. } else {
  2957. // geometry-level bounding box
  2958. if ( geometry.boundingBox === null ) {
  2959. geometry.computeBoundingBox();
  2960. }
  2961. _box$4.copy( geometry.boundingBox );
  2962. }
  2963. _box$4.applyMatrix4( object.matrixWorld );
  2964. this.union( _box$4 );
  2965. }
  2966. }
  2967. const children = object.children;
  2968. for ( let i = 0, l = children.length; i < l; i ++ ) {
  2969. this.expandByObject( children[ i ], precise );
  2970. }
  2971. return this;
  2972. }
  2973. containsPoint( point ) {
  2974. return point.x >= this.min.x && point.x <= this.max.x &&
  2975. point.y >= this.min.y && point.y <= this.max.y &&
  2976. point.z >= this.min.z && point.z <= this.max.z;
  2977. }
  2978. containsBox( box ) {
  2979. return this.min.x <= box.min.x && box.max.x <= this.max.x &&
  2980. this.min.y <= box.min.y && box.max.y <= this.max.y &&
  2981. this.min.z <= box.min.z && box.max.z <= this.max.z;
  2982. }
  2983. getParameter( point, target ) {
  2984. // This can potentially have a divide by zero if the box
  2985. // has a size dimension of 0.
  2986. return target.set(
  2987. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  2988. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  2989. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  2990. );
  2991. }
  2992. intersectsBox( box ) {
  2993. // using 6 splitting planes to rule out intersections.
  2994. return box.max.x >= this.min.x && box.min.x <= this.max.x &&
  2995. box.max.y >= this.min.y && box.min.y <= this.max.y &&
  2996. box.max.z >= this.min.z && box.min.z <= this.max.z;
  2997. }
  2998. intersectsSphere( sphere ) {
  2999. // Find the point on the AABB closest to the sphere center.
  3000. this.clampPoint( sphere.center, _vector$b );
  3001. // If that point is inside the sphere, the AABB and sphere intersect.
  3002. return _vector$b.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius );
  3003. }
  3004. intersectsPlane( plane ) {
  3005. // We compute the minimum and maximum dot product values. If those values
  3006. // are on the same side (back or front) of the plane, then there is no intersection.
  3007. let min, max;
  3008. if ( plane.normal.x > 0 ) {
  3009. min = plane.normal.x * this.min.x;
  3010. max = plane.normal.x * this.max.x;
  3011. } else {
  3012. min = plane.normal.x * this.max.x;
  3013. max = plane.normal.x * this.min.x;
  3014. }
  3015. if ( plane.normal.y > 0 ) {
  3016. min += plane.normal.y * this.min.y;
  3017. max += plane.normal.y * this.max.y;
  3018. } else {
  3019. min += plane.normal.y * this.max.y;
  3020. max += plane.normal.y * this.min.y;
  3021. }
  3022. if ( plane.normal.z > 0 ) {
  3023. min += plane.normal.z * this.min.z;
  3024. max += plane.normal.z * this.max.z;
  3025. } else {
  3026. min += plane.normal.z * this.max.z;
  3027. max += plane.normal.z * this.min.z;
  3028. }
  3029. return ( min <= - plane.constant && max >= - plane.constant );
  3030. }
  3031. intersectsTriangle( triangle ) {
  3032. if ( this.isEmpty() ) {
  3033. return false;
  3034. }
  3035. // compute box center and extents
  3036. this.getCenter( _center );
  3037. _extents.subVectors( this.max, _center );
  3038. // translate triangle to aabb origin
  3039. _v0$2.subVectors( triangle.a, _center );
  3040. _v1$7.subVectors( triangle.b, _center );
  3041. _v2$4.subVectors( triangle.c, _center );
  3042. // compute edge vectors for triangle
  3043. _f0.subVectors( _v1$7, _v0$2 );
  3044. _f1.subVectors( _v2$4, _v1$7 );
  3045. _f2.subVectors( _v0$2, _v2$4 );
  3046. // test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb
  3047. // 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
  3048. // axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned)
  3049. let axes = [
  3050. 0, - _f0.z, _f0.y, 0, - _f1.z, _f1.y, 0, - _f2.z, _f2.y,
  3051. _f0.z, 0, - _f0.x, _f1.z, 0, - _f1.x, _f2.z, 0, - _f2.x,
  3052. - _f0.y, _f0.x, 0, - _f1.y, _f1.x, 0, - _f2.y, _f2.x, 0
  3053. ];
  3054. if ( ! satForAxes( axes, _v0$2, _v1$7, _v2$4, _extents ) ) {
  3055. return false;
  3056. }
  3057. // test 3 face normals from the aabb
  3058. axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ];
  3059. if ( ! satForAxes( axes, _v0$2, _v1$7, _v2$4, _extents ) ) {
  3060. return false;
  3061. }
  3062. // finally testing the face normal of the triangle
  3063. // use already existing triangle edge vectors here
  3064. _triangleNormal.crossVectors( _f0, _f1 );
  3065. axes = [ _triangleNormal.x, _triangleNormal.y, _triangleNormal.z ];
  3066. return satForAxes( axes, _v0$2, _v1$7, _v2$4, _extents );
  3067. }
  3068. clampPoint( point, target ) {
  3069. return target.copy( point ).clamp( this.min, this.max );
  3070. }
  3071. distanceToPoint( point ) {
  3072. return this.clampPoint( point, _vector$b ).distanceTo( point );
  3073. }
  3074. getBoundingSphere( target ) {
  3075. if ( this.isEmpty() ) {
  3076. target.makeEmpty();
  3077. } else {
  3078. this.getCenter( target.center );
  3079. target.radius = this.getSize( _vector$b ).length() * 0.5;
  3080. }
  3081. return target;
  3082. }
  3083. intersect( box ) {
  3084. this.min.max( box.min );
  3085. this.max.min( box.max );
  3086. // 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.
  3087. if ( this.isEmpty() ) this.makeEmpty();
  3088. return this;
  3089. }
  3090. union( box ) {
  3091. this.min.min( box.min );
  3092. this.max.max( box.max );
  3093. return this;
  3094. }
  3095. applyMatrix4( matrix ) {
  3096. // transform of empty box is an empty box.
  3097. if ( this.isEmpty() ) return this;
  3098. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  3099. _points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  3100. _points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  3101. _points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  3102. _points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  3103. _points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  3104. _points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  3105. _points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  3106. _points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  3107. this.setFromPoints( _points );
  3108. return this;
  3109. }
  3110. translate( offset ) {
  3111. this.min.add( offset );
  3112. this.max.add( offset );
  3113. return this;
  3114. }
  3115. equals( box ) {
  3116. return box.min.equals( this.min ) && box.max.equals( this.max );
  3117. }
  3118. }
  3119. const _points = [
  3120. /*@__PURE__*/ new Vector3(),
  3121. /*@__PURE__*/ new Vector3(),
  3122. /*@__PURE__*/ new Vector3(),
  3123. /*@__PURE__*/ new Vector3(),
  3124. /*@__PURE__*/ new Vector3(),
  3125. /*@__PURE__*/ new Vector3(),
  3126. /*@__PURE__*/ new Vector3(),
  3127. /*@__PURE__*/ new Vector3()
  3128. ];
  3129. const _vector$b = /*@__PURE__*/ new Vector3();
  3130. const _box$4 = /*@__PURE__*/ new Box3();
  3131. // triangle centered vertices
  3132. const _v0$2 = /*@__PURE__*/ new Vector3();
  3133. const _v1$7 = /*@__PURE__*/ new Vector3();
  3134. const _v2$4 = /*@__PURE__*/ new Vector3();
  3135. // triangle edge vectors
  3136. const _f0 = /*@__PURE__*/ new Vector3();
  3137. const _f1 = /*@__PURE__*/ new Vector3();
  3138. const _f2 = /*@__PURE__*/ new Vector3();
  3139. const _center = /*@__PURE__*/ new Vector3();
  3140. const _extents = /*@__PURE__*/ new Vector3();
  3141. const _triangleNormal = /*@__PURE__*/ new Vector3();
  3142. const _testAxis = /*@__PURE__*/ new Vector3();
  3143. function satForAxes( axes, v0, v1, v2, extents ) {
  3144. for ( let i = 0, j = axes.length - 3; i <= j; i += 3 ) {
  3145. _testAxis.fromArray( axes, i );
  3146. // project the aabb onto the separating axis
  3147. const r = extents.x * Math.abs( _testAxis.x ) + extents.y * Math.abs( _testAxis.y ) + extents.z * Math.abs( _testAxis.z );
  3148. // project all 3 vertices of the triangle onto the separating axis
  3149. const p0 = v0.dot( _testAxis );
  3150. const p1 = v1.dot( _testAxis );
  3151. const p2 = v2.dot( _testAxis );
  3152. // actual test, basically see if either of the most extreme of the triangle points intersects r
  3153. if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) {
  3154. // points of the projected triangle are outside the projected half-length of the aabb
  3155. // the axis is separating and we can exit
  3156. return false;
  3157. }
  3158. }
  3159. return true;
  3160. }
  3161. const _box$3 = /*@__PURE__*/ new Box3();
  3162. const _v1$6 = /*@__PURE__*/ new Vector3();
  3163. const _v2$3 = /*@__PURE__*/ new Vector3();
  3164. class Sphere {
  3165. constructor( center = new Vector3(), radius = - 1 ) {
  3166. this.isSphere = true;
  3167. this.center = center;
  3168. this.radius = radius;
  3169. }
  3170. set( center, radius ) {
  3171. this.center.copy( center );
  3172. this.radius = radius;
  3173. return this;
  3174. }
  3175. setFromPoints( points, optionalCenter ) {
  3176. const center = this.center;
  3177. if ( optionalCenter !== undefined ) {
  3178. center.copy( optionalCenter );
  3179. } else {
  3180. _box$3.setFromPoints( points ).getCenter( center );
  3181. }
  3182. let maxRadiusSq = 0;
  3183. for ( let i = 0, il = points.length; i < il; i ++ ) {
  3184. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
  3185. }
  3186. this.radius = Math.sqrt( maxRadiusSq );
  3187. return this;
  3188. }
  3189. copy( sphere ) {
  3190. this.center.copy( sphere.center );
  3191. this.radius = sphere.radius;
  3192. return this;
  3193. }
  3194. isEmpty() {
  3195. return ( this.radius < 0 );
  3196. }
  3197. makeEmpty() {
  3198. this.center.set( 0, 0, 0 );
  3199. this.radius = - 1;
  3200. return this;
  3201. }
  3202. containsPoint( point ) {
  3203. return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
  3204. }
  3205. distanceToPoint( point ) {
  3206. return ( point.distanceTo( this.center ) - this.radius );
  3207. }
  3208. intersectsSphere( sphere ) {
  3209. const radiusSum = this.radius + sphere.radius;
  3210. return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
  3211. }
  3212. intersectsBox( box ) {
  3213. return box.intersectsSphere( this );
  3214. }
  3215. intersectsPlane( plane ) {
  3216. return Math.abs( plane.distanceToPoint( this.center ) ) <= this.radius;
  3217. }
  3218. clampPoint( point, target ) {
  3219. const deltaLengthSq = this.center.distanceToSquared( point );
  3220. target.copy( point );
  3221. if ( deltaLengthSq > ( this.radius * this.radius ) ) {
  3222. target.sub( this.center ).normalize();
  3223. target.multiplyScalar( this.radius ).add( this.center );
  3224. }
  3225. return target;
  3226. }
  3227. getBoundingBox( target ) {
  3228. if ( this.isEmpty() ) {
  3229. // Empty sphere produces empty bounding box
  3230. target.makeEmpty();
  3231. return target;
  3232. }
  3233. target.set( this.center, this.center );
  3234. target.expandByScalar( this.radius );
  3235. return target;
  3236. }
  3237. applyMatrix4( matrix ) {
  3238. this.center.applyMatrix4( matrix );
  3239. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  3240. return this;
  3241. }
  3242. translate( offset ) {
  3243. this.center.add( offset );
  3244. return this;
  3245. }
  3246. expandByPoint( point ) {
  3247. if ( this.isEmpty() ) {
  3248. this.center.copy( point );
  3249. this.radius = 0;
  3250. return this;
  3251. }
  3252. _v1$6.subVectors( point, this.center );
  3253. const lengthSq = _v1$6.lengthSq();
  3254. if ( lengthSq > ( this.radius * this.radius ) ) {
  3255. // calculate the minimal sphere
  3256. const length = Math.sqrt( lengthSq );
  3257. const delta = ( length - this.radius ) * 0.5;
  3258. this.center.addScaledVector( _v1$6, delta / length );
  3259. this.radius += delta;
  3260. }
  3261. return this;
  3262. }
  3263. union( sphere ) {
  3264. if ( sphere.isEmpty() ) {
  3265. return this;
  3266. }
  3267. if ( this.isEmpty() ) {
  3268. this.copy( sphere );
  3269. return this;
  3270. }
  3271. if ( this.center.equals( sphere.center ) === true ) {
  3272. this.radius = Math.max( this.radius, sphere.radius );
  3273. } else {
  3274. _v2$3.subVectors( sphere.center, this.center ).setLength( sphere.radius );
  3275. this.expandByPoint( _v1$6.copy( sphere.center ).add( _v2$3 ) );
  3276. this.expandByPoint( _v1$6.copy( sphere.center ).sub( _v2$3 ) );
  3277. }
  3278. return this;
  3279. }
  3280. equals( sphere ) {
  3281. return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
  3282. }
  3283. clone() {
  3284. return new this.constructor().copy( this );
  3285. }
  3286. }
  3287. const _vector$a = /*@__PURE__*/ new Vector3();
  3288. const _segCenter = /*@__PURE__*/ new Vector3();
  3289. const _segDir = /*@__PURE__*/ new Vector3();
  3290. const _diff = /*@__PURE__*/ new Vector3();
  3291. const _edge1 = /*@__PURE__*/ new Vector3();
  3292. const _edge2 = /*@__PURE__*/ new Vector3();
  3293. const _normal$1 = /*@__PURE__*/ new Vector3();
  3294. class Ray {
  3295. constructor( origin = new Vector3(), direction = new Vector3( 0, 0, - 1 ) ) {
  3296. this.origin = origin;
  3297. this.direction = direction;
  3298. }
  3299. set( origin, direction ) {
  3300. this.origin.copy( origin );
  3301. this.direction.copy( direction );
  3302. return this;
  3303. }
  3304. copy( ray ) {
  3305. this.origin.copy( ray.origin );
  3306. this.direction.copy( ray.direction );
  3307. return this;
  3308. }
  3309. at( t, target ) {
  3310. return target.copy( this.origin ).addScaledVector( this.direction, t );
  3311. }
  3312. lookAt( v ) {
  3313. this.direction.copy( v ).sub( this.origin ).normalize();
  3314. return this;
  3315. }
  3316. recast( t ) {
  3317. this.origin.copy( this.at( t, _vector$a ) );
  3318. return this;
  3319. }
  3320. closestPointToPoint( point, target ) {
  3321. target.subVectors( point, this.origin );
  3322. const directionDistance = target.dot( this.direction );
  3323. if ( directionDistance < 0 ) {
  3324. return target.copy( this.origin );
  3325. }
  3326. return target.copy( this.origin ).addScaledVector( this.direction, directionDistance );
  3327. }
  3328. distanceToPoint( point ) {
  3329. return Math.sqrt( this.distanceSqToPoint( point ) );
  3330. }
  3331. distanceSqToPoint( point ) {
  3332. const directionDistance = _vector$a.subVectors( point, this.origin ).dot( this.direction );
  3333. // point behind the ray
  3334. if ( directionDistance < 0 ) {
  3335. return this.origin.distanceToSquared( point );
  3336. }
  3337. _vector$a.copy( this.origin ).addScaledVector( this.direction, directionDistance );
  3338. return _vector$a.distanceToSquared( point );
  3339. }
  3340. distanceSqToSegment( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
  3341. // from https://github.com/pmjoniak/GeometricTools/blob/master/GTEngine/Include/Mathematics/GteDistRaySegment.h
  3342. // It returns the min distance between the ray and the segment
  3343. // defined by v0 and v1
  3344. // It can also set two optional targets :
  3345. // - The closest point on the ray
  3346. // - The closest point on the segment
  3347. _segCenter.copy( v0 ).add( v1 ).multiplyScalar( 0.5 );
  3348. _segDir.copy( v1 ).sub( v0 ).normalize();
  3349. _diff.copy( this.origin ).sub( _segCenter );
  3350. const segExtent = v0.distanceTo( v1 ) * 0.5;
  3351. const a01 = - this.direction.dot( _segDir );
  3352. const b0 = _diff.dot( this.direction );
  3353. const b1 = - _diff.dot( _segDir );
  3354. const c = _diff.lengthSq();
  3355. const det = Math.abs( 1 - a01 * a01 );
  3356. let s0, s1, sqrDist, extDet;
  3357. if ( det > 0 ) {
  3358. // The ray and segment are not parallel.
  3359. s0 = a01 * b1 - b0;
  3360. s1 = a01 * b0 - b1;
  3361. extDet = segExtent * det;
  3362. if ( s0 >= 0 ) {
  3363. if ( s1 >= - extDet ) {
  3364. if ( s1 <= extDet ) {
  3365. // region 0
  3366. // Minimum at interior points of ray and segment.
  3367. const invDet = 1 / det;
  3368. s0 *= invDet;
  3369. s1 *= invDet;
  3370. sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c;
  3371. } else {
  3372. // region 1
  3373. s1 = segExtent;
  3374. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3375. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3376. }
  3377. } else {
  3378. // region 5
  3379. s1 = - segExtent;
  3380. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3381. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3382. }
  3383. } else {
  3384. if ( s1 <= - extDet ) {
  3385. // region 4
  3386. s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) );
  3387. s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3388. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3389. } else if ( s1 <= extDet ) {
  3390. // region 3
  3391. s0 = 0;
  3392. s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3393. sqrDist = s1 * ( s1 + 2 * b1 ) + c;
  3394. } else {
  3395. // region 2
  3396. s0 = Math.max( 0, - ( a01 * segExtent + b0 ) );
  3397. s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3398. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3399. }
  3400. }
  3401. } else {
  3402. // Ray and segment are parallel.
  3403. s1 = ( a01 > 0 ) ? - segExtent : segExtent;
  3404. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3405. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3406. }
  3407. if ( optionalPointOnRay ) {
  3408. optionalPointOnRay.copy( this.origin ).addScaledVector( this.direction, s0 );
  3409. }
  3410. if ( optionalPointOnSegment ) {
  3411. optionalPointOnSegment.copy( _segCenter ).addScaledVector( _segDir, s1 );
  3412. }
  3413. return sqrDist;
  3414. }
  3415. intersectSphere( sphere, target ) {
  3416. _vector$a.subVectors( sphere.center, this.origin );
  3417. const tca = _vector$a.dot( this.direction );
  3418. const d2 = _vector$a.dot( _vector$a ) - tca * tca;
  3419. const radius2 = sphere.radius * sphere.radius;
  3420. if ( d2 > radius2 ) return null;
  3421. const thc = Math.sqrt( radius2 - d2 );
  3422. // t0 = first intersect point - entrance on front of sphere
  3423. const t0 = tca - thc;
  3424. // t1 = second intersect point - exit point on back of sphere
  3425. const t1 = tca + thc;
  3426. // test to see if t1 is behind the ray - if so, return null
  3427. if ( t1 < 0 ) return null;
  3428. // test to see if t0 is behind the ray:
  3429. // if it is, the ray is inside the sphere, so return the second exit point scaled by t1,
  3430. // in order to always return an intersect point that is in front of the ray.
  3431. if ( t0 < 0 ) return this.at( t1, target );
  3432. // else t0 is in front of the ray, so return the first collision point scaled by t0
  3433. return this.at( t0, target );
  3434. }
  3435. intersectsSphere( sphere ) {
  3436. return this.distanceSqToPoint( sphere.center ) <= ( sphere.radius * sphere.radius );
  3437. }
  3438. distanceToPlane( plane ) {
  3439. const denominator = plane.normal.dot( this.direction );
  3440. if ( denominator === 0 ) {
  3441. // line is coplanar, return origin
  3442. if ( plane.distanceToPoint( this.origin ) === 0 ) {
  3443. return 0;
  3444. }
  3445. // Null is preferable to undefined since undefined means.... it is undefined
  3446. return null;
  3447. }
  3448. const t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator;
  3449. // Return if the ray never intersects the plane
  3450. return t >= 0 ? t : null;
  3451. }
  3452. intersectPlane( plane, target ) {
  3453. const t = this.distanceToPlane( plane );
  3454. if ( t === null ) {
  3455. return null;
  3456. }
  3457. return this.at( t, target );
  3458. }
  3459. intersectsPlane( plane ) {
  3460. // check if the ray lies on the plane first
  3461. const distToPoint = plane.distanceToPoint( this.origin );
  3462. if ( distToPoint === 0 ) {
  3463. return true;
  3464. }
  3465. const denominator = plane.normal.dot( this.direction );
  3466. if ( denominator * distToPoint < 0 ) {
  3467. return true;
  3468. }
  3469. // ray origin is behind the plane (and is pointing behind it)
  3470. return false;
  3471. }
  3472. intersectBox( box, target ) {
  3473. let tmin, tmax, tymin, tymax, tzmin, tzmax;
  3474. const invdirx = 1 / this.direction.x,
  3475. invdiry = 1 / this.direction.y,
  3476. invdirz = 1 / this.direction.z;
  3477. const origin = this.origin;
  3478. if ( invdirx >= 0 ) {
  3479. tmin = ( box.min.x - origin.x ) * invdirx;
  3480. tmax = ( box.max.x - origin.x ) * invdirx;
  3481. } else {
  3482. tmin = ( box.max.x - origin.x ) * invdirx;
  3483. tmax = ( box.min.x - origin.x ) * invdirx;
  3484. }
  3485. if ( invdiry >= 0 ) {
  3486. tymin = ( box.min.y - origin.y ) * invdiry;
  3487. tymax = ( box.max.y - origin.y ) * invdiry;
  3488. } else {
  3489. tymin = ( box.max.y - origin.y ) * invdiry;
  3490. tymax = ( box.min.y - origin.y ) * invdiry;
  3491. }
  3492. if ( ( tmin > tymax ) || ( tymin > tmax ) ) return null;
  3493. if ( tymin > tmin || isNaN( tmin ) ) tmin = tymin;
  3494. if ( tymax < tmax || isNaN( tmax ) ) tmax = tymax;
  3495. if ( invdirz >= 0 ) {
  3496. tzmin = ( box.min.z - origin.z ) * invdirz;
  3497. tzmax = ( box.max.z - origin.z ) * invdirz;
  3498. } else {
  3499. tzmin = ( box.max.z - origin.z ) * invdirz;
  3500. tzmax = ( box.min.z - origin.z ) * invdirz;
  3501. }
  3502. if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) return null;
  3503. if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin;
  3504. if ( tzmax < tmax || tmax !== tmax ) tmax = tzmax;
  3505. //return point closest to the ray (positive side)
  3506. if ( tmax < 0 ) return null;
  3507. return this.at( tmin >= 0 ? tmin : tmax, target );
  3508. }
  3509. intersectsBox( box ) {
  3510. return this.intersectBox( box, _vector$a ) !== null;
  3511. }
  3512. intersectTriangle( a, b, c, backfaceCulling, target ) {
  3513. // Compute the offset origin, edges, and normal.
  3514. // from https://github.com/pmjoniak/GeometricTools/blob/master/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h
  3515. _edge1.subVectors( b, a );
  3516. _edge2.subVectors( c, a );
  3517. _normal$1.crossVectors( _edge1, _edge2 );
  3518. // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
  3519. // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
  3520. // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
  3521. // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
  3522. // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
  3523. let DdN = this.direction.dot( _normal$1 );
  3524. let sign;
  3525. if ( DdN > 0 ) {
  3526. if ( backfaceCulling ) return null;
  3527. sign = 1;
  3528. } else if ( DdN < 0 ) {
  3529. sign = - 1;
  3530. DdN = - DdN;
  3531. } else {
  3532. return null;
  3533. }
  3534. _diff.subVectors( this.origin, a );
  3535. const DdQxE2 = sign * this.direction.dot( _edge2.crossVectors( _diff, _edge2 ) );
  3536. // b1 < 0, no intersection
  3537. if ( DdQxE2 < 0 ) {
  3538. return null;
  3539. }
  3540. const DdE1xQ = sign * this.direction.dot( _edge1.cross( _diff ) );
  3541. // b2 < 0, no intersection
  3542. if ( DdE1xQ < 0 ) {
  3543. return null;
  3544. }
  3545. // b1+b2 > 1, no intersection
  3546. if ( DdQxE2 + DdE1xQ > DdN ) {
  3547. return null;
  3548. }
  3549. // Line intersects triangle, check if ray does.
  3550. const QdN = - sign * _diff.dot( _normal$1 );
  3551. // t < 0, no intersection
  3552. if ( QdN < 0 ) {
  3553. return null;
  3554. }
  3555. // Ray intersects triangle.
  3556. return this.at( QdN / DdN, target );
  3557. }
  3558. applyMatrix4( matrix4 ) {
  3559. this.origin.applyMatrix4( matrix4 );
  3560. this.direction.transformDirection( matrix4 );
  3561. return this;
  3562. }
  3563. equals( ray ) {
  3564. return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
  3565. }
  3566. clone() {
  3567. return new this.constructor().copy( this );
  3568. }
  3569. }
  3570. class Matrix4 {
  3571. constructor( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  3572. Matrix4.prototype.isMatrix4 = true;
  3573. this.elements = [
  3574. 1, 0, 0, 0,
  3575. 0, 1, 0, 0,
  3576. 0, 0, 1, 0,
  3577. 0, 0, 0, 1
  3578. ];
  3579. if ( n11 !== undefined ) {
  3580. this.set( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 );
  3581. }
  3582. }
  3583. set( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  3584. const te = this.elements;
  3585. te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14;
  3586. te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24;
  3587. te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34;
  3588. te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44;
  3589. return this;
  3590. }
  3591. identity() {
  3592. this.set(
  3593. 1, 0, 0, 0,
  3594. 0, 1, 0, 0,
  3595. 0, 0, 1, 0,
  3596. 0, 0, 0, 1
  3597. );
  3598. return this;
  3599. }
  3600. clone() {
  3601. return new Matrix4().fromArray( this.elements );
  3602. }
  3603. copy( m ) {
  3604. const te = this.elements;
  3605. const me = m.elements;
  3606. te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ]; te[ 3 ] = me[ 3 ];
  3607. te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ]; te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ];
  3608. te[ 8 ] = me[ 8 ]; te[ 9 ] = me[ 9 ]; te[ 10 ] = me[ 10 ]; te[ 11 ] = me[ 11 ];
  3609. te[ 12 ] = me[ 12 ]; te[ 13 ] = me[ 13 ]; te[ 14 ] = me[ 14 ]; te[ 15 ] = me[ 15 ];
  3610. return this;
  3611. }
  3612. copyPosition( m ) {
  3613. const te = this.elements, me = m.elements;
  3614. te[ 12 ] = me[ 12 ];
  3615. te[ 13 ] = me[ 13 ];
  3616. te[ 14 ] = me[ 14 ];
  3617. return this;
  3618. }
  3619. setFromMatrix3( m ) {
  3620. const me = m.elements;
  3621. this.set(
  3622. me[ 0 ], me[ 3 ], me[ 6 ], 0,
  3623. me[ 1 ], me[ 4 ], me[ 7 ], 0,
  3624. me[ 2 ], me[ 5 ], me[ 8 ], 0,
  3625. 0, 0, 0, 1
  3626. );
  3627. return this;
  3628. }
  3629. extractBasis( xAxis, yAxis, zAxis ) {
  3630. xAxis.setFromMatrixColumn( this, 0 );
  3631. yAxis.setFromMatrixColumn( this, 1 );
  3632. zAxis.setFromMatrixColumn( this, 2 );
  3633. return this;
  3634. }
  3635. makeBasis( xAxis, yAxis, zAxis ) {
  3636. this.set(
  3637. xAxis.x, yAxis.x, zAxis.x, 0,
  3638. xAxis.y, yAxis.y, zAxis.y, 0,
  3639. xAxis.z, yAxis.z, zAxis.z, 0,
  3640. 0, 0, 0, 1
  3641. );
  3642. return this;
  3643. }
  3644. extractRotation( m ) {
  3645. // this method does not support reflection matrices
  3646. const te = this.elements;
  3647. const me = m.elements;
  3648. const scaleX = 1 / _v1$5.setFromMatrixColumn( m, 0 ).length();
  3649. const scaleY = 1 / _v1$5.setFromMatrixColumn( m, 1 ).length();
  3650. const scaleZ = 1 / _v1$5.setFromMatrixColumn( m, 2 ).length();
  3651. te[ 0 ] = me[ 0 ] * scaleX;
  3652. te[ 1 ] = me[ 1 ] * scaleX;
  3653. te[ 2 ] = me[ 2 ] * scaleX;
  3654. te[ 3 ] = 0;
  3655. te[ 4 ] = me[ 4 ] * scaleY;
  3656. te[ 5 ] = me[ 5 ] * scaleY;
  3657. te[ 6 ] = me[ 6 ] * scaleY;
  3658. te[ 7 ] = 0;
  3659. te[ 8 ] = me[ 8 ] * scaleZ;
  3660. te[ 9 ] = me[ 9 ] * scaleZ;
  3661. te[ 10 ] = me[ 10 ] * scaleZ;
  3662. te[ 11 ] = 0;
  3663. te[ 12 ] = 0;
  3664. te[ 13 ] = 0;
  3665. te[ 14 ] = 0;
  3666. te[ 15 ] = 1;
  3667. return this;
  3668. }
  3669. makeRotationFromEuler( euler ) {
  3670. const te = this.elements;
  3671. const x = euler.x, y = euler.y, z = euler.z;
  3672. const a = Math.cos( x ), b = Math.sin( x );
  3673. const c = Math.cos( y ), d = Math.sin( y );
  3674. const e = Math.cos( z ), f = Math.sin( z );
  3675. if ( euler.order === 'XYZ' ) {
  3676. const ae = a * e, af = a * f, be = b * e, bf = b * f;
  3677. te[ 0 ] = c * e;
  3678. te[ 4 ] = - c * f;
  3679. te[ 8 ] = d;
  3680. te[ 1 ] = af + be * d;
  3681. te[ 5 ] = ae - bf * d;
  3682. te[ 9 ] = - b * c;
  3683. te[ 2 ] = bf - ae * d;
  3684. te[ 6 ] = be + af * d;
  3685. te[ 10 ] = a * c;
  3686. } else if ( euler.order === 'YXZ' ) {
  3687. const ce = c * e, cf = c * f, de = d * e, df = d * f;
  3688. te[ 0 ] = ce + df * b;
  3689. te[ 4 ] = de * b - cf;
  3690. te[ 8 ] = a * d;
  3691. te[ 1 ] = a * f;
  3692. te[ 5 ] = a * e;
  3693. te[ 9 ] = - b;
  3694. te[ 2 ] = cf * b - de;
  3695. te[ 6 ] = df + ce * b;
  3696. te[ 10 ] = a * c;
  3697. } else if ( euler.order === 'ZXY' ) {
  3698. const ce = c * e, cf = c * f, de = d * e, df = d * f;
  3699. te[ 0 ] = ce - df * b;
  3700. te[ 4 ] = - a * f;
  3701. te[ 8 ] = de + cf * b;
  3702. te[ 1 ] = cf + de * b;
  3703. te[ 5 ] = a * e;
  3704. te[ 9 ] = df - ce * b;
  3705. te[ 2 ] = - a * d;
  3706. te[ 6 ] = b;
  3707. te[ 10 ] = a * c;
  3708. } else if ( euler.order === 'ZYX' ) {
  3709. const ae = a * e, af = a * f, be = b * e, bf = b * f;
  3710. te[ 0 ] = c * e;
  3711. te[ 4 ] = be * d - af;
  3712. te[ 8 ] = ae * d + bf;
  3713. te[ 1 ] = c * f;
  3714. te[ 5 ] = bf * d + ae;
  3715. te[ 9 ] = af * d - be;
  3716. te[ 2 ] = - d;
  3717. te[ 6 ] = b * c;
  3718. te[ 10 ] = a * c;
  3719. } else if ( euler.order === 'YZX' ) {
  3720. const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  3721. te[ 0 ] = c * e;
  3722. te[ 4 ] = bd - ac * f;
  3723. te[ 8 ] = bc * f + ad;
  3724. te[ 1 ] = f;
  3725. te[ 5 ] = a * e;
  3726. te[ 9 ] = - b * e;
  3727. te[ 2 ] = - d * e;
  3728. te[ 6 ] = ad * f + bc;
  3729. te[ 10 ] = ac - bd * f;
  3730. } else if ( euler.order === 'XZY' ) {
  3731. const ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  3732. te[ 0 ] = c * e;
  3733. te[ 4 ] = - f;
  3734. te[ 8 ] = d * e;
  3735. te[ 1 ] = ac * f + bd;
  3736. te[ 5 ] = a * e;
  3737. te[ 9 ] = ad * f - bc;
  3738. te[ 2 ] = bc * f - ad;
  3739. te[ 6 ] = b * e;
  3740. te[ 10 ] = bd * f + ac;
  3741. }
  3742. // bottom row
  3743. te[ 3 ] = 0;
  3744. te[ 7 ] = 0;
  3745. te[ 11 ] = 0;
  3746. // last column
  3747. te[ 12 ] = 0;
  3748. te[ 13 ] = 0;
  3749. te[ 14 ] = 0;
  3750. te[ 15 ] = 1;
  3751. return this;
  3752. }
  3753. makeRotationFromQuaternion( q ) {
  3754. return this.compose( _zero, q, _one );
  3755. }
  3756. lookAt( eye, target, up ) {
  3757. const te = this.elements;
  3758. _z.subVectors( eye, target );
  3759. if ( _z.lengthSq() === 0 ) {
  3760. // eye and target are in the same position
  3761. _z.z = 1;
  3762. }
  3763. _z.normalize();
  3764. _x.crossVectors( up, _z );
  3765. if ( _x.lengthSq() === 0 ) {
  3766. // up and z are parallel
  3767. if ( Math.abs( up.z ) === 1 ) {
  3768. _z.x += 0.0001;
  3769. } else {
  3770. _z.z += 0.0001;
  3771. }
  3772. _z.normalize();
  3773. _x.crossVectors( up, _z );
  3774. }
  3775. _x.normalize();
  3776. _y.crossVectors( _z, _x );
  3777. te[ 0 ] = _x.x; te[ 4 ] = _y.x; te[ 8 ] = _z.x;
  3778. te[ 1 ] = _x.y; te[ 5 ] = _y.y; te[ 9 ] = _z.y;
  3779. te[ 2 ] = _x.z; te[ 6 ] = _y.z; te[ 10 ] = _z.z;
  3780. return this;
  3781. }
  3782. multiply( m ) {
  3783. return this.multiplyMatrices( this, m );
  3784. }
  3785. premultiply( m ) {
  3786. return this.multiplyMatrices( m, this );
  3787. }
  3788. multiplyMatrices( a, b ) {
  3789. const ae = a.elements;
  3790. const be = b.elements;
  3791. const te = this.elements;
  3792. const a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ];
  3793. const a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ];
  3794. const a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ];
  3795. const a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ];
  3796. const b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ];
  3797. const b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ];
  3798. const b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ];
  3799. const b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ];
  3800. te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  3801. te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  3802. te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  3803. te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  3804. te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  3805. te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  3806. te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  3807. te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  3808. te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  3809. te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  3810. te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  3811. te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  3812. te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  3813. te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  3814. te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  3815. te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  3816. return this;
  3817. }
  3818. multiplyScalar( s ) {
  3819. const te = this.elements;
  3820. te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s;
  3821. te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s;
  3822. te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s;
  3823. te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s;
  3824. return this;
  3825. }
  3826. determinant() {
  3827. const te = this.elements;
  3828. const n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ];
  3829. const n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ];
  3830. const n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ];
  3831. const n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ];
  3832. //TODO: make this more efficient
  3833. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  3834. return (
  3835. n41 * (
  3836. + n14 * n23 * n32
  3837. - n13 * n24 * n32
  3838. - n14 * n22 * n33
  3839. + n12 * n24 * n33
  3840. + n13 * n22 * n34
  3841. - n12 * n23 * n34
  3842. ) +
  3843. n42 * (
  3844. + n11 * n23 * n34
  3845. - n11 * n24 * n33
  3846. + n14 * n21 * n33
  3847. - n13 * n21 * n34
  3848. + n13 * n24 * n31
  3849. - n14 * n23 * n31
  3850. ) +
  3851. n43 * (
  3852. + n11 * n24 * n32
  3853. - n11 * n22 * n34
  3854. - n14 * n21 * n32
  3855. + n12 * n21 * n34
  3856. + n14 * n22 * n31
  3857. - n12 * n24 * n31
  3858. ) +
  3859. n44 * (
  3860. - n13 * n22 * n31
  3861. - n11 * n23 * n32
  3862. + n11 * n22 * n33
  3863. + n13 * n21 * n32
  3864. - n12 * n21 * n33
  3865. + n12 * n23 * n31
  3866. )
  3867. );
  3868. }
  3869. transpose() {
  3870. const te = this.elements;
  3871. let tmp;
  3872. tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp;
  3873. tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp;
  3874. tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp;
  3875. tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp;
  3876. tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp;
  3877. tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp;
  3878. return this;
  3879. }
  3880. setPosition( x, y, z ) {
  3881. const te = this.elements;
  3882. if ( x.isVector3 ) {
  3883. te[ 12 ] = x.x;
  3884. te[ 13 ] = x.y;
  3885. te[ 14 ] = x.z;
  3886. } else {
  3887. te[ 12 ] = x;
  3888. te[ 13 ] = y;
  3889. te[ 14 ] = z;
  3890. }
  3891. return this;
  3892. }
  3893. invert() {
  3894. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  3895. const te = this.elements,
  3896. n11 = te[ 0 ], n21 = te[ 1 ], n31 = te[ 2 ], n41 = te[ 3 ],
  3897. n12 = te[ 4 ], n22 = te[ 5 ], n32 = te[ 6 ], n42 = te[ 7 ],
  3898. n13 = te[ 8 ], n23 = te[ 9 ], n33 = te[ 10 ], n43 = te[ 11 ],
  3899. n14 = te[ 12 ], n24 = te[ 13 ], n34 = te[ 14 ], n44 = te[ 15 ],
  3900. t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44,
  3901. t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44,
  3902. t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44,
  3903. t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
  3904. const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14;
  3905. if ( det === 0 ) return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );
  3906. const detInv = 1 / det;
  3907. te[ 0 ] = t11 * detInv;
  3908. te[ 1 ] = ( n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44 ) * detInv;
  3909. te[ 2 ] = ( n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44 ) * detInv;
  3910. te[ 3 ] = ( n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43 ) * detInv;
  3911. te[ 4 ] = t12 * detInv;
  3912. te[ 5 ] = ( n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44 ) * detInv;
  3913. te[ 6 ] = ( n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44 ) * detInv;
  3914. te[ 7 ] = ( n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43 ) * detInv;
  3915. te[ 8 ] = t13 * detInv;
  3916. te[ 9 ] = ( n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44 ) * detInv;
  3917. te[ 10 ] = ( n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44 ) * detInv;
  3918. te[ 11 ] = ( n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43 ) * detInv;
  3919. te[ 12 ] = t14 * detInv;
  3920. te[ 13 ] = ( n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34 ) * detInv;
  3921. te[ 14 ] = ( n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34 ) * detInv;
  3922. te[ 15 ] = ( n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33 ) * detInv;
  3923. return this;
  3924. }
  3925. scale( v ) {
  3926. const te = this.elements;
  3927. const x = v.x, y = v.y, z = v.z;
  3928. te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z;
  3929. te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z;
  3930. te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z;
  3931. te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z;
  3932. return this;
  3933. }
  3934. getMaxScaleOnAxis() {
  3935. const te = this.elements;
  3936. const scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ];
  3937. const scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ];
  3938. const scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ];
  3939. return Math.sqrt( Math.max( scaleXSq, scaleYSq, scaleZSq ) );
  3940. }
  3941. makeTranslation( x, y, z ) {
  3942. if ( x.isVector3 ) {
  3943. this.set(
  3944. 1, 0, 0, x.x,
  3945. 0, 1, 0, x.y,
  3946. 0, 0, 1, x.z,
  3947. 0, 0, 0, 1
  3948. );
  3949. } else {
  3950. this.set(
  3951. 1, 0, 0, x,
  3952. 0, 1, 0, y,
  3953. 0, 0, 1, z,
  3954. 0, 0, 0, 1
  3955. );
  3956. }
  3957. return this;
  3958. }
  3959. makeRotationX( theta ) {
  3960. const c = Math.cos( theta ), s = Math.sin( theta );
  3961. this.set(
  3962. 1, 0, 0, 0,
  3963. 0, c, - s, 0,
  3964. 0, s, c, 0,
  3965. 0, 0, 0, 1
  3966. );
  3967. return this;
  3968. }
  3969. makeRotationY( theta ) {
  3970. const c = Math.cos( theta ), s = Math.sin( theta );
  3971. this.set(
  3972. c, 0, s, 0,
  3973. 0, 1, 0, 0,
  3974. - s, 0, c, 0,
  3975. 0, 0, 0, 1
  3976. );
  3977. return this;
  3978. }
  3979. makeRotationZ( theta ) {
  3980. const c = Math.cos( theta ), s = Math.sin( theta );
  3981. this.set(
  3982. c, - s, 0, 0,
  3983. s, c, 0, 0,
  3984. 0, 0, 1, 0,
  3985. 0, 0, 0, 1
  3986. );
  3987. return this;
  3988. }
  3989. makeRotationAxis( axis, angle ) {
  3990. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  3991. const c = Math.cos( angle );
  3992. const s = Math.sin( angle );
  3993. const t = 1 - c;
  3994. const x = axis.x, y = axis.y, z = axis.z;
  3995. const tx = t * x, ty = t * y;
  3996. this.set(
  3997. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  3998. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  3999. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  4000. 0, 0, 0, 1
  4001. );
  4002. return this;
  4003. }
  4004. makeScale( x, y, z ) {
  4005. this.set(
  4006. x, 0, 0, 0,
  4007. 0, y, 0, 0,
  4008. 0, 0, z, 0,
  4009. 0, 0, 0, 1
  4010. );
  4011. return this;
  4012. }
  4013. makeShear( xy, xz, yx, yz, zx, zy ) {
  4014. this.set(
  4015. 1, yx, zx, 0,
  4016. xy, 1, zy, 0,
  4017. xz, yz, 1, 0,
  4018. 0, 0, 0, 1
  4019. );
  4020. return this;
  4021. }
  4022. compose( position, quaternion, scale ) {
  4023. const te = this.elements;
  4024. const x = quaternion._x, y = quaternion._y, z = quaternion._z, w = quaternion._w;
  4025. const x2 = x + x, y2 = y + y, z2 = z + z;
  4026. const xx = x * x2, xy = x * y2, xz = x * z2;
  4027. const yy = y * y2, yz = y * z2, zz = z * z2;
  4028. const wx = w * x2, wy = w * y2, wz = w * z2;
  4029. const sx = scale.x, sy = scale.y, sz = scale.z;
  4030. te[ 0 ] = ( 1 - ( yy + zz ) ) * sx;
  4031. te[ 1 ] = ( xy + wz ) * sx;
  4032. te[ 2 ] = ( xz - wy ) * sx;
  4033. te[ 3 ] = 0;
  4034. te[ 4 ] = ( xy - wz ) * sy;
  4035. te[ 5 ] = ( 1 - ( xx + zz ) ) * sy;
  4036. te[ 6 ] = ( yz + wx ) * sy;
  4037. te[ 7 ] = 0;
  4038. te[ 8 ] = ( xz + wy ) * sz;
  4039. te[ 9 ] = ( yz - wx ) * sz;
  4040. te[ 10 ] = ( 1 - ( xx + yy ) ) * sz;
  4041. te[ 11 ] = 0;
  4042. te[ 12 ] = position.x;
  4043. te[ 13 ] = position.y;
  4044. te[ 14 ] = position.z;
  4045. te[ 15 ] = 1;
  4046. return this;
  4047. }
  4048. decompose( position, quaternion, scale ) {
  4049. const te = this.elements;
  4050. let sx = _v1$5.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();
  4051. const sy = _v1$5.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length();
  4052. const sz = _v1$5.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length();
  4053. // if determine is negative, we need to invert one scale
  4054. const det = this.determinant();
  4055. if ( det < 0 ) sx = - sx;
  4056. position.x = te[ 12 ];
  4057. position.y = te[ 13 ];
  4058. position.z = te[ 14 ];
  4059. // scale the rotation part
  4060. _m1$2.copy( this );
  4061. const invSX = 1 / sx;
  4062. const invSY = 1 / sy;
  4063. const invSZ = 1 / sz;
  4064. _m1$2.elements[ 0 ] *= invSX;
  4065. _m1$2.elements[ 1 ] *= invSX;
  4066. _m1$2.elements[ 2 ] *= invSX;
  4067. _m1$2.elements[ 4 ] *= invSY;
  4068. _m1$2.elements[ 5 ] *= invSY;
  4069. _m1$2.elements[ 6 ] *= invSY;
  4070. _m1$2.elements[ 8 ] *= invSZ;
  4071. _m1$2.elements[ 9 ] *= invSZ;
  4072. _m1$2.elements[ 10 ] *= invSZ;
  4073. quaternion.setFromRotationMatrix( _m1$2 );
  4074. scale.x = sx;
  4075. scale.y = sy;
  4076. scale.z = sz;
  4077. return this;
  4078. }
  4079. makePerspective( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem ) {
  4080. const te = this.elements;
  4081. const x = 2 * near / ( right - left );
  4082. const y = 2 * near / ( top - bottom );
  4083. const a = ( right + left ) / ( right - left );
  4084. const b = ( top + bottom ) / ( top - bottom );
  4085. let c, d;
  4086. if ( coordinateSystem === WebGLCoordinateSystem ) {
  4087. c = - ( far + near ) / ( far - near );
  4088. d = ( - 2 * far * near ) / ( far - near );
  4089. } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
  4090. c = - far / ( far - near );
  4091. d = ( - far * near ) / ( far - near );
  4092. } else {
  4093. throw new Error( 'THREE.Matrix4.makePerspective(): Invalid coordinate system: ' + coordinateSystem );
  4094. }
  4095. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0;
  4096. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0;
  4097. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  4098. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0;
  4099. return this;
  4100. }
  4101. makeOrthographic( left, right, top, bottom, near, far, coordinateSystem = WebGLCoordinateSystem ) {
  4102. const te = this.elements;
  4103. const w = 1.0 / ( right - left );
  4104. const h = 1.0 / ( top - bottom );
  4105. const p = 1.0 / ( far - near );
  4106. const x = ( right + left ) * w;
  4107. const y = ( top + bottom ) * h;
  4108. let z, zInv;
  4109. if ( coordinateSystem === WebGLCoordinateSystem ) {
  4110. z = ( far + near ) * p;
  4111. zInv = - 2 * p;
  4112. } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
  4113. z = near * p;
  4114. zInv = - 1 * p;
  4115. } else {
  4116. throw new Error( 'THREE.Matrix4.makeOrthographic(): Invalid coordinate system: ' + coordinateSystem );
  4117. }
  4118. te[ 0 ] = 2 * w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x;
  4119. te[ 1 ] = 0; te[ 5 ] = 2 * h; te[ 9 ] = 0; te[ 13 ] = - y;
  4120. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = zInv; te[ 14 ] = - z;
  4121. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1;
  4122. return this;
  4123. }
  4124. equals( matrix ) {
  4125. const te = this.elements;
  4126. const me = matrix.elements;
  4127. for ( let i = 0; i < 16; i ++ ) {
  4128. if ( te[ i ] !== me[ i ] ) return false;
  4129. }
  4130. return true;
  4131. }
  4132. fromArray( array, offset = 0 ) {
  4133. for ( let i = 0; i < 16; i ++ ) {
  4134. this.elements[ i ] = array[ i + offset ];
  4135. }
  4136. return this;
  4137. }
  4138. toArray( array = [], offset = 0 ) {
  4139. const te = this.elements;
  4140. array[ offset ] = te[ 0 ];
  4141. array[ offset + 1 ] = te[ 1 ];
  4142. array[ offset + 2 ] = te[ 2 ];
  4143. array[ offset + 3 ] = te[ 3 ];
  4144. array[ offset + 4 ] = te[ 4 ];
  4145. array[ offset + 5 ] = te[ 5 ];
  4146. array[ offset + 6 ] = te[ 6 ];
  4147. array[ offset + 7 ] = te[ 7 ];
  4148. array[ offset + 8 ] = te[ 8 ];
  4149. array[ offset + 9 ] = te[ 9 ];
  4150. array[ offset + 10 ] = te[ 10 ];
  4151. array[ offset + 11 ] = te[ 11 ];
  4152. array[ offset + 12 ] = te[ 12 ];
  4153. array[ offset + 13 ] = te[ 13 ];
  4154. array[ offset + 14 ] = te[ 14 ];
  4155. array[ offset + 15 ] = te[ 15 ];
  4156. return array;
  4157. }
  4158. }
  4159. const _v1$5 = /*@__PURE__*/ new Vector3();
  4160. const _m1$2 = /*@__PURE__*/ new Matrix4();
  4161. const _zero = /*@__PURE__*/ new Vector3( 0, 0, 0 );
  4162. const _one = /*@__PURE__*/ new Vector3( 1, 1, 1 );
  4163. const _x = /*@__PURE__*/ new Vector3();
  4164. const _y = /*@__PURE__*/ new Vector3();
  4165. const _z = /*@__PURE__*/ new Vector3();
  4166. const _matrix$2 = /*@__PURE__*/ new Matrix4();
  4167. const _quaternion$3 = /*@__PURE__*/ new Quaternion();
  4168. class Euler {
  4169. constructor( x = 0, y = 0, z = 0, order = Euler.DEFAULT_ORDER ) {
  4170. this.isEuler = true;
  4171. this._x = x;
  4172. this._y = y;
  4173. this._z = z;
  4174. this._order = order;
  4175. }
  4176. get x() {
  4177. return this._x;
  4178. }
  4179. set x( value ) {
  4180. this._x = value;
  4181. this._onChangeCallback();
  4182. }
  4183. get y() {
  4184. return this._y;
  4185. }
  4186. set y( value ) {
  4187. this._y = value;
  4188. this._onChangeCallback();
  4189. }
  4190. get z() {
  4191. return this._z;
  4192. }
  4193. set z( value ) {
  4194. this._z = value;
  4195. this._onChangeCallback();
  4196. }
  4197. get order() {
  4198. return this._order;
  4199. }
  4200. set order( value ) {
  4201. this._order = value;
  4202. this._onChangeCallback();
  4203. }
  4204. set( x, y, z, order = this._order ) {
  4205. this._x = x;
  4206. this._y = y;
  4207. this._z = z;
  4208. this._order = order;
  4209. this._onChangeCallback();
  4210. return this;
  4211. }
  4212. clone() {
  4213. return new this.constructor( this._x, this._y, this._z, this._order );
  4214. }
  4215. copy( euler ) {
  4216. this._x = euler._x;
  4217. this._y = euler._y;
  4218. this._z = euler._z;
  4219. this._order = euler._order;
  4220. this._onChangeCallback();
  4221. return this;
  4222. }
  4223. setFromRotationMatrix( m, order = this._order, update = true ) {
  4224. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  4225. const te = m.elements;
  4226. const m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
  4227. const m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
  4228. const m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  4229. switch ( order ) {
  4230. case 'XYZ':
  4231. this._y = Math.asin( clamp( m13, - 1, 1 ) );
  4232. if ( Math.abs( m13 ) < 0.9999999 ) {
  4233. this._x = Math.atan2( - m23, m33 );
  4234. this._z = Math.atan2( - m12, m11 );
  4235. } else {
  4236. this._x = Math.atan2( m32, m22 );
  4237. this._z = 0;
  4238. }
  4239. break;
  4240. case 'YXZ':
  4241. this._x = Math.asin( - clamp( m23, - 1, 1 ) );
  4242. if ( Math.abs( m23 ) < 0.9999999 ) {
  4243. this._y = Math.atan2( m13, m33 );
  4244. this._z = Math.atan2( m21, m22 );
  4245. } else {
  4246. this._y = Math.atan2( - m31, m11 );
  4247. this._z = 0;
  4248. }
  4249. break;
  4250. case 'ZXY':
  4251. this._x = Math.asin( clamp( m32, - 1, 1 ) );
  4252. if ( Math.abs( m32 ) < 0.9999999 ) {
  4253. this._y = Math.atan2( - m31, m33 );
  4254. this._z = Math.atan2( - m12, m22 );
  4255. } else {
  4256. this._y = 0;
  4257. this._z = Math.atan2( m21, m11 );
  4258. }
  4259. break;
  4260. case 'ZYX':
  4261. this._y = Math.asin( - clamp( m31, - 1, 1 ) );
  4262. if ( Math.abs( m31 ) < 0.9999999 ) {
  4263. this._x = Math.atan2( m32, m33 );
  4264. this._z = Math.atan2( m21, m11 );
  4265. } else {
  4266. this._x = 0;
  4267. this._z = Math.atan2( - m12, m22 );
  4268. }
  4269. break;
  4270. case 'YZX':
  4271. this._z = Math.asin( clamp( m21, - 1, 1 ) );
  4272. if ( Math.abs( m21 ) < 0.9999999 ) {
  4273. this._x = Math.atan2( - m23, m22 );
  4274. this._y = Math.atan2( - m31, m11 );
  4275. } else {
  4276. this._x = 0;
  4277. this._y = Math.atan2( m13, m33 );
  4278. }
  4279. break;
  4280. case 'XZY':
  4281. this._z = Math.asin( - clamp( m12, - 1, 1 ) );
  4282. if ( Math.abs( m12 ) < 0.9999999 ) {
  4283. this._x = Math.atan2( m32, m22 );
  4284. this._y = Math.atan2( m13, m11 );
  4285. } else {
  4286. this._x = Math.atan2( - m23, m33 );
  4287. this._y = 0;
  4288. }
  4289. break;
  4290. default:
  4291. console.warn( 'THREE.Euler: .setFromRotationMatrix() encountered an unknown order: ' + order );
  4292. }
  4293. this._order = order;
  4294. if ( update === true ) this._onChangeCallback();
  4295. return this;
  4296. }
  4297. setFromQuaternion( q, order, update ) {
  4298. _matrix$2.makeRotationFromQuaternion( q );
  4299. return this.setFromRotationMatrix( _matrix$2, order, update );
  4300. }
  4301. setFromVector3( v, order = this._order ) {
  4302. return this.set( v.x, v.y, v.z, order );
  4303. }
  4304. reorder( newOrder ) {
  4305. // WARNING: this discards revolution information -bhouston
  4306. _quaternion$3.setFromEuler( this );
  4307. return this.setFromQuaternion( _quaternion$3, newOrder );
  4308. }
  4309. equals( euler ) {
  4310. return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
  4311. }
  4312. fromArray( array ) {
  4313. this._x = array[ 0 ];
  4314. this._y = array[ 1 ];
  4315. this._z = array[ 2 ];
  4316. if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
  4317. this._onChangeCallback();
  4318. return this;
  4319. }
  4320. toArray( array = [], offset = 0 ) {
  4321. array[ offset ] = this._x;
  4322. array[ offset + 1 ] = this._y;
  4323. array[ offset + 2 ] = this._z;
  4324. array[ offset + 3 ] = this._order;
  4325. return array;
  4326. }
  4327. _onChange( callback ) {
  4328. this._onChangeCallback = callback;
  4329. return this;
  4330. }
  4331. _onChangeCallback() {}
  4332. *[ Symbol.iterator ]() {
  4333. yield this._x;
  4334. yield this._y;
  4335. yield this._z;
  4336. yield this._order;
  4337. }
  4338. }
  4339. Euler.DEFAULT_ORDER = 'XYZ';
  4340. class Layers {
  4341. constructor() {
  4342. this.mask = 1 | 0;
  4343. }
  4344. set( channel ) {
  4345. this.mask = ( 1 << channel | 0 ) >>> 0;
  4346. }
  4347. enable( channel ) {
  4348. this.mask |= 1 << channel | 0;
  4349. }
  4350. enableAll() {
  4351. this.mask = 0xffffffff | 0;
  4352. }
  4353. toggle( channel ) {
  4354. this.mask ^= 1 << channel | 0;
  4355. }
  4356. disable( channel ) {
  4357. this.mask &= ~ ( 1 << channel | 0 );
  4358. }
  4359. disableAll() {
  4360. this.mask = 0;
  4361. }
  4362. test( layers ) {
  4363. return ( this.mask & layers.mask ) !== 0;
  4364. }
  4365. isEnabled( channel ) {
  4366. return ( this.mask & ( 1 << channel | 0 ) ) !== 0;
  4367. }
  4368. }
  4369. let _object3DId = 0;
  4370. const _v1$4 = /*@__PURE__*/ new Vector3();
  4371. const _q1 = /*@__PURE__*/ new Quaternion();
  4372. const _m1$1 = /*@__PURE__*/ new Matrix4();
  4373. const _target = /*@__PURE__*/ new Vector3();
  4374. const _position$3 = /*@__PURE__*/ new Vector3();
  4375. const _scale$2 = /*@__PURE__*/ new Vector3();
  4376. const _quaternion$2 = /*@__PURE__*/ new Quaternion();
  4377. const _xAxis = /*@__PURE__*/ new Vector3( 1, 0, 0 );
  4378. const _yAxis = /*@__PURE__*/ new Vector3( 0, 1, 0 );
  4379. const _zAxis = /*@__PURE__*/ new Vector3( 0, 0, 1 );
  4380. const _addedEvent = { type: 'added' };
  4381. const _removedEvent = { type: 'removed' };
  4382. const _childaddedEvent = { type: 'childadded', child: null };
  4383. const _childremovedEvent = { type: 'childremoved', child: null };
  4384. class Object3D extends EventDispatcher {
  4385. constructor() {
  4386. super();
  4387. this.isObject3D = true;
  4388. Object.defineProperty( this, 'id', { value: _object3DId ++ } );
  4389. this.uuid = generateUUID();
  4390. this.name = '';
  4391. this.type = 'Object3D';
  4392. this.parent = null;
  4393. this.children = [];
  4394. this.up = Object3D.DEFAULT_UP.clone();
  4395. const position = new Vector3();
  4396. const rotation = new Euler();
  4397. const quaternion = new Quaternion();
  4398. const scale = new Vector3( 1, 1, 1 );
  4399. function onRotationChange() {
  4400. quaternion.setFromEuler( rotation, false );
  4401. }
  4402. function onQuaternionChange() {
  4403. rotation.setFromQuaternion( quaternion, undefined, false );
  4404. }
  4405. rotation._onChange( onRotationChange );
  4406. quaternion._onChange( onQuaternionChange );
  4407. Object.defineProperties( this, {
  4408. position: {
  4409. configurable: true,
  4410. enumerable: true,
  4411. value: position
  4412. },
  4413. rotation: {
  4414. configurable: true,
  4415. enumerable: true,
  4416. value: rotation
  4417. },
  4418. quaternion: {
  4419. configurable: true,
  4420. enumerable: true,
  4421. value: quaternion
  4422. },
  4423. scale: {
  4424. configurable: true,
  4425. enumerable: true,
  4426. value: scale
  4427. },
  4428. modelViewMatrix: {
  4429. value: new Matrix4()
  4430. },
  4431. normalMatrix: {
  4432. value: new Matrix3()
  4433. }
  4434. } );
  4435. this.matrix = new Matrix4();
  4436. this.matrixWorld = new Matrix4();
  4437. this.matrixAutoUpdate = Object3D.DEFAULT_MATRIX_AUTO_UPDATE;
  4438. this.matrixWorldAutoUpdate = Object3D.DEFAULT_MATRIX_WORLD_AUTO_UPDATE; // checked by the renderer
  4439. this.matrixWorldNeedsUpdate = false;
  4440. this.layers = new Layers();
  4441. this.visible = true;
  4442. this.castShadow = false;
  4443. this.receiveShadow = false;
  4444. this.frustumCulled = true;
  4445. this.renderOrder = 0;
  4446. this.animations = [];
  4447. this.userData = {};
  4448. }
  4449. onBeforeShadow( /* renderer, object, camera, shadowCamera, geometry, depthMaterial, group */ ) {}
  4450. onAfterShadow( /* renderer, object, camera, shadowCamera, geometry, depthMaterial, group */ ) {}
  4451. onBeforeRender( /* renderer, scene, camera, geometry, material, group */ ) {}
  4452. onAfterRender( /* renderer, scene, camera, geometry, material, group */ ) {}
  4453. applyMatrix4( matrix ) {
  4454. if ( this.matrixAutoUpdate ) this.updateMatrix();
  4455. this.matrix.premultiply( matrix );
  4456. this.matrix.decompose( this.position, this.quaternion, this.scale );
  4457. }
  4458. applyQuaternion( q ) {
  4459. this.quaternion.premultiply( q );
  4460. return this;
  4461. }
  4462. setRotationFromAxisAngle( axis, angle ) {
  4463. // assumes axis is normalized
  4464. this.quaternion.setFromAxisAngle( axis, angle );
  4465. }
  4466. setRotationFromEuler( euler ) {
  4467. this.quaternion.setFromEuler( euler, true );
  4468. }
  4469. setRotationFromMatrix( m ) {
  4470. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  4471. this.quaternion.setFromRotationMatrix( m );
  4472. }
  4473. setRotationFromQuaternion( q ) {
  4474. // assumes q is normalized
  4475. this.quaternion.copy( q );
  4476. }
  4477. rotateOnAxis( axis, angle ) {
  4478. // rotate object on axis in object space
  4479. // axis is assumed to be normalized
  4480. _q1.setFromAxisAngle( axis, angle );
  4481. this.quaternion.multiply( _q1 );
  4482. return this;
  4483. }
  4484. rotateOnWorldAxis( axis, angle ) {
  4485. // rotate object on axis in world space
  4486. // axis is assumed to be normalized
  4487. // method assumes no rotated parent
  4488. _q1.setFromAxisAngle( axis, angle );
  4489. this.quaternion.premultiply( _q1 );
  4490. return this;
  4491. }
  4492. rotateX( angle ) {
  4493. return this.rotateOnAxis( _xAxis, angle );
  4494. }
  4495. rotateY( angle ) {
  4496. return this.rotateOnAxis( _yAxis, angle );
  4497. }
  4498. rotateZ( angle ) {
  4499. return this.rotateOnAxis( _zAxis, angle );
  4500. }
  4501. translateOnAxis( axis, distance ) {
  4502. // translate object by distance along axis in object space
  4503. // axis is assumed to be normalized
  4504. _v1$4.copy( axis ).applyQuaternion( this.quaternion );
  4505. this.position.add( _v1$4.multiplyScalar( distance ) );
  4506. return this;
  4507. }
  4508. translateX( distance ) {
  4509. return this.translateOnAxis( _xAxis, distance );
  4510. }
  4511. translateY( distance ) {
  4512. return this.translateOnAxis( _yAxis, distance );
  4513. }
  4514. translateZ( distance ) {
  4515. return this.translateOnAxis( _zAxis, distance );
  4516. }
  4517. localToWorld( vector ) {
  4518. this.updateWorldMatrix( true, false );
  4519. return vector.applyMatrix4( this.matrixWorld );
  4520. }
  4521. worldToLocal( vector ) {
  4522. this.updateWorldMatrix( true, false );
  4523. return vector.applyMatrix4( _m1$1.copy( this.matrixWorld ).invert() );
  4524. }
  4525. lookAt( x, y, z ) {
  4526. // This method does not support objects having non-uniformly-scaled parent(s)
  4527. if ( x.isVector3 ) {
  4528. _target.copy( x );
  4529. } else {
  4530. _target.set( x, y, z );
  4531. }
  4532. const parent = this.parent;
  4533. this.updateWorldMatrix( true, false );
  4534. _position$3.setFromMatrixPosition( this.matrixWorld );
  4535. if ( this.isCamera || this.isLight ) {
  4536. _m1$1.lookAt( _position$3, _target, this.up );
  4537. } else {
  4538. _m1$1.lookAt( _target, _position$3, this.up );
  4539. }
  4540. this.quaternion.setFromRotationMatrix( _m1$1 );
  4541. if ( parent ) {
  4542. _m1$1.extractRotation( parent.matrixWorld );
  4543. _q1.setFromRotationMatrix( _m1$1 );
  4544. this.quaternion.premultiply( _q1.invert() );
  4545. }
  4546. }
  4547. add( object ) {
  4548. if ( arguments.length > 1 ) {
  4549. for ( let i = 0; i < arguments.length; i ++ ) {
  4550. this.add( arguments[ i ] );
  4551. }
  4552. return this;
  4553. }
  4554. if ( object === this ) {
  4555. console.error( 'THREE.Object3D.add: object can\'t be added as a child of itself.', object );
  4556. return this;
  4557. }
  4558. if ( object && object.isObject3D ) {
  4559. object.removeFromParent();
  4560. object.parent = this;
  4561. this.children.push( object );
  4562. object.dispatchEvent( _addedEvent );
  4563. _childaddedEvent.child = object;
  4564. this.dispatchEvent( _childaddedEvent );
  4565. _childaddedEvent.child = null;
  4566. } else {
  4567. console.error( 'THREE.Object3D.add: object not an instance of THREE.Object3D.', object );
  4568. }
  4569. return this;
  4570. }
  4571. remove( object ) {
  4572. if ( arguments.length > 1 ) {
  4573. for ( let i = 0; i < arguments.length; i ++ ) {
  4574. this.remove( arguments[ i ] );
  4575. }
  4576. return this;
  4577. }
  4578. const index = this.children.indexOf( object );
  4579. if ( index !== - 1 ) {
  4580. object.parent = null;
  4581. this.children.splice( index, 1 );
  4582. object.dispatchEvent( _removedEvent );
  4583. _childremovedEvent.child = object;
  4584. this.dispatchEvent( _childremovedEvent );
  4585. _childremovedEvent.child = null;
  4586. }
  4587. return this;
  4588. }
  4589. removeFromParent() {
  4590. const parent = this.parent;
  4591. if ( parent !== null ) {
  4592. parent.remove( this );
  4593. }
  4594. return this;
  4595. }
  4596. clear() {
  4597. return this.remove( ... this.children );
  4598. }
  4599. attach( object ) {
  4600. // adds object as a child of this, while maintaining the object's world transform
  4601. // Note: This method does not support scene graphs having non-uniformly-scaled nodes(s)
  4602. this.updateWorldMatrix( true, false );
  4603. _m1$1.copy( this.matrixWorld ).invert();
  4604. if ( object.parent !== null ) {
  4605. object.parent.updateWorldMatrix( true, false );
  4606. _m1$1.multiply( object.parent.matrixWorld );
  4607. }
  4608. object.applyMatrix4( _m1$1 );
  4609. object.removeFromParent();
  4610. object.parent = this;
  4611. this.children.push( object );
  4612. object.updateWorldMatrix( false, true );
  4613. object.dispatchEvent( _addedEvent );
  4614. _childaddedEvent.child = object;
  4615. this.dispatchEvent( _childaddedEvent );
  4616. _childaddedEvent.child = null;
  4617. return this;
  4618. }
  4619. getObjectById( id ) {
  4620. return this.getObjectByProperty( 'id', id );
  4621. }
  4622. getObjectByName( name ) {
  4623. return this.getObjectByProperty( 'name', name );
  4624. }
  4625. getObjectByProperty( name, value ) {
  4626. if ( this[ name ] === value ) return this;
  4627. for ( let i = 0, l = this.children.length; i < l; i ++ ) {
  4628. const child = this.children[ i ];
  4629. const object = child.getObjectByProperty( name, value );
  4630. if ( object !== undefined ) {
  4631. return object;
  4632. }
  4633. }
  4634. return undefined;
  4635. }
  4636. getObjectsByProperty( name, value, result = [] ) {
  4637. if ( this[ name ] === value ) result.push( this );
  4638. const children = this.children;
  4639. for ( let i = 0, l = children.length; i < l; i ++ ) {
  4640. children[ i ].getObjectsByProperty( name, value, result );
  4641. }
  4642. return result;
  4643. }
  4644. getWorldPosition( target ) {
  4645. this.updateWorldMatrix( true, false );
  4646. return target.setFromMatrixPosition( this.matrixWorld );
  4647. }
  4648. getWorldQuaternion( target ) {
  4649. this.updateWorldMatrix( true, false );
  4650. this.matrixWorld.decompose( _position$3, target, _scale$2 );
  4651. return target;
  4652. }
  4653. getWorldScale( target ) {
  4654. this.updateWorldMatrix( true, false );
  4655. this.matrixWorld.decompose( _position$3, _quaternion$2, target );
  4656. return target;
  4657. }
  4658. getWorldDirection( target ) {
  4659. this.updateWorldMatrix( true, false );
  4660. const e = this.matrixWorld.elements;
  4661. return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize();
  4662. }
  4663. raycast( /* raycaster, intersects */ ) {}
  4664. traverse( callback ) {
  4665. callback( this );
  4666. const children = this.children;
  4667. for ( let i = 0, l = children.length; i < l; i ++ ) {
  4668. children[ i ].traverse( callback );
  4669. }
  4670. }
  4671. traverseVisible( callback ) {
  4672. if ( this.visible === false ) return;
  4673. callback( this );
  4674. const children = this.children;
  4675. for ( let i = 0, l = children.length; i < l; i ++ ) {
  4676. children[ i ].traverseVisible( callback );
  4677. }
  4678. }
  4679. traverseAncestors( callback ) {
  4680. const parent = this.parent;
  4681. if ( parent !== null ) {
  4682. callback( parent );
  4683. parent.traverseAncestors( callback );
  4684. }
  4685. }
  4686. updateMatrix() {
  4687. this.matrix.compose( this.position, this.quaternion, this.scale );
  4688. this.matrixWorldNeedsUpdate = true;
  4689. }
  4690. updateMatrixWorld( force ) {
  4691. if ( this.matrixAutoUpdate ) this.updateMatrix();
  4692. if ( this.matrixWorldNeedsUpdate || force ) {
  4693. if ( this.matrixWorldAutoUpdate === true ) {
  4694. if ( this.parent === null ) {
  4695. this.matrixWorld.copy( this.matrix );
  4696. } else {
  4697. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  4698. }
  4699. }
  4700. this.matrixWorldNeedsUpdate = false;
  4701. force = true;
  4702. }
  4703. // make sure descendants are updated if required
  4704. const children = this.children;
  4705. for ( let i = 0, l = children.length; i < l; i ++ ) {
  4706. const child = children[ i ];
  4707. child.updateMatrixWorld( force );
  4708. }
  4709. }
  4710. updateWorldMatrix( updateParents, updateChildren ) {
  4711. const parent = this.parent;
  4712. if ( updateParents === true && parent !== null ) {
  4713. parent.updateWorldMatrix( true, false );
  4714. }
  4715. if ( this.matrixAutoUpdate ) this.updateMatrix();
  4716. if ( this.matrixWorldAutoUpdate === true ) {
  4717. if ( this.parent === null ) {
  4718. this.matrixWorld.copy( this.matrix );
  4719. } else {
  4720. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  4721. }
  4722. }
  4723. // make sure descendants are updated
  4724. if ( updateChildren === true ) {
  4725. const children = this.children;
  4726. for ( let i = 0, l = children.length; i < l; i ++ ) {
  4727. const child = children[ i ];
  4728. child.updateWorldMatrix( false, true );
  4729. }
  4730. }
  4731. }
  4732. toJSON( meta ) {
  4733. // meta is a string when called from JSON.stringify
  4734. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  4735. const output = {};
  4736. // meta is a hash used to collect geometries, materials.
  4737. // not providing it implies that this is the root object
  4738. // being serialized.
  4739. if ( isRootObject ) {
  4740. // initialize meta obj
  4741. meta = {
  4742. geometries: {},
  4743. materials: {},
  4744. textures: {},
  4745. images: {},
  4746. shapes: {},
  4747. skeletons: {},
  4748. animations: {},
  4749. nodes: {}
  4750. };
  4751. output.metadata = {
  4752. version: 4.6,
  4753. type: 'Object',
  4754. generator: 'Object3D.toJSON'
  4755. };
  4756. }
  4757. // standard Object3D serialization
  4758. const object = {};
  4759. object.uuid = this.uuid;
  4760. object.type = this.type;
  4761. if ( this.name !== '' ) object.name = this.name;
  4762. if ( this.castShadow === true ) object.castShadow = true;
  4763. if ( this.receiveShadow === true ) object.receiveShadow = true;
  4764. if ( this.visible === false ) object.visible = false;
  4765. if ( this.frustumCulled === false ) object.frustumCulled = false;
  4766. if ( this.renderOrder !== 0 ) object.renderOrder = this.renderOrder;
  4767. if ( Object.keys( this.userData ).length > 0 ) object.userData = this.userData;
  4768. object.layers = this.layers.mask;
  4769. object.matrix = this.matrix.toArray();
  4770. object.up = this.up.toArray();
  4771. if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false;
  4772. // object specific properties
  4773. if ( this.isInstancedMesh ) {
  4774. object.type = 'InstancedMesh';
  4775. object.count = this.count;
  4776. object.instanceMatrix = this.instanceMatrix.toJSON();
  4777. if ( this.instanceColor !== null ) object.instanceColor = this.instanceColor.toJSON();
  4778. }
  4779. if ( this.isBatchedMesh ) {
  4780. object.type = 'BatchedMesh';
  4781. object.perObjectFrustumCulled = this.perObjectFrustumCulled;
  4782. object.sortObjects = this.sortObjects;
  4783. object.drawRanges = this._drawRanges;
  4784. object.reservedRanges = this._reservedRanges;
  4785. object.visibility = this._visibility;
  4786. object.active = this._active;
  4787. object.bounds = this._bounds.map( bound => ( {
  4788. boxInitialized: bound.boxInitialized,
  4789. boxMin: bound.box.min.toArray(),
  4790. boxMax: bound.box.max.toArray(),
  4791. sphereInitialized: bound.sphereInitialized,
  4792. sphereRadius: bound.sphere.radius,
  4793. sphereCenter: bound.sphere.center.toArray()
  4794. } ) );
  4795. object.maxInstanceCount = this._maxInstanceCount;
  4796. object.maxVertexCount = this._maxVertexCount;
  4797. object.maxIndexCount = this._maxIndexCount;
  4798. object.geometryInitialized = this._geometryInitialized;
  4799. object.geometryCount = this._geometryCount;
  4800. object.matricesTexture = this._matricesTexture.toJSON( meta );
  4801. if ( this._colorsTexture !== null ) object.colorsTexture = this._colorsTexture.toJSON( meta );
  4802. if ( this.boundingSphere !== null ) {
  4803. object.boundingSphere = {
  4804. center: object.boundingSphere.center.toArray(),
  4805. radius: object.boundingSphere.radius
  4806. };
  4807. }
  4808. if ( this.boundingBox !== null ) {
  4809. object.boundingBox = {
  4810. min: object.boundingBox.min.toArray(),
  4811. max: object.boundingBox.max.toArray()
  4812. };
  4813. }
  4814. }
  4815. //
  4816. function serialize( library, element ) {
  4817. if ( library[ element.uuid ] === undefined ) {
  4818. library[ element.uuid ] = element.toJSON( meta );
  4819. }
  4820. return element.uuid;
  4821. }
  4822. if ( this.isScene ) {
  4823. if ( this.background ) {
  4824. if ( this.background.isColor ) {
  4825. object.background = this.background.toJSON();
  4826. } else if ( this.background.isTexture ) {
  4827. object.background = this.background.toJSON( meta ).uuid;
  4828. }
  4829. }
  4830. if ( this.environment && this.environment.isTexture && this.environment.isRenderTargetTexture !== true ) {
  4831. object.environment = this.environment.toJSON( meta ).uuid;
  4832. }
  4833. } else if ( this.isMesh || this.isLine || this.isPoints ) {
  4834. object.geometry = serialize( meta.geometries, this.geometry );
  4835. const parameters = this.geometry.parameters;
  4836. if ( parameters !== undefined && parameters.shapes !== undefined ) {
  4837. const shapes = parameters.shapes;
  4838. if ( Array.isArray( shapes ) ) {
  4839. for ( let i = 0, l = shapes.length; i < l; i ++ ) {
  4840. const shape = shapes[ i ];
  4841. serialize( meta.shapes, shape );
  4842. }
  4843. } else {
  4844. serialize( meta.shapes, shapes );
  4845. }
  4846. }
  4847. }
  4848. if ( this.isSkinnedMesh ) {
  4849. object.bindMode = this.bindMode;
  4850. object.bindMatrix = this.bindMatrix.toArray();
  4851. if ( this.skeleton !== undefined ) {
  4852. serialize( meta.skeletons, this.skeleton );
  4853. object.skeleton = this.skeleton.uuid;
  4854. }
  4855. }
  4856. if ( this.material !== undefined ) {
  4857. if ( Array.isArray( this.material ) ) {
  4858. const uuids = [];
  4859. for ( let i = 0, l = this.material.length; i < l; i ++ ) {
  4860. uuids.push( serialize( meta.materials, this.material[ i ] ) );
  4861. }
  4862. object.material = uuids;
  4863. } else {
  4864. object.material = serialize( meta.materials, this.material );
  4865. }
  4866. }
  4867. //
  4868. if ( this.children.length > 0 ) {
  4869. object.children = [];
  4870. for ( let i = 0; i < this.children.length; i ++ ) {
  4871. object.children.push( this.children[ i ].toJSON( meta ).object );
  4872. }
  4873. }
  4874. //
  4875. if ( this.animations.length > 0 ) {
  4876. object.animations = [];
  4877. for ( let i = 0; i < this.animations.length; i ++ ) {
  4878. const animation = this.animations[ i ];
  4879. object.animations.push( serialize( meta.animations, animation ) );
  4880. }
  4881. }
  4882. if ( isRootObject ) {
  4883. const geometries = extractFromCache( meta.geometries );
  4884. const materials = extractFromCache( meta.materials );
  4885. const textures = extractFromCache( meta.textures );
  4886. const images = extractFromCache( meta.images );
  4887. const shapes = extractFromCache( meta.shapes );
  4888. const skeletons = extractFromCache( meta.skeletons );
  4889. const animations = extractFromCache( meta.animations );
  4890. const nodes = extractFromCache( meta.nodes );
  4891. if ( geometries.length > 0 ) output.geometries = geometries;
  4892. if ( materials.length > 0 ) output.materials = materials;
  4893. if ( textures.length > 0 ) output.textures = textures;
  4894. if ( images.length > 0 ) output.images = images;
  4895. if ( shapes.length > 0 ) output.shapes = shapes;
  4896. if ( skeletons.length > 0 ) output.skeletons = skeletons;
  4897. if ( animations.length > 0 ) output.animations = animations;
  4898. if ( nodes.length > 0 ) output.nodes = nodes;
  4899. }
  4900. output.object = object;
  4901. return output;
  4902. // extract data from the cache hash
  4903. // remove metadata on each item
  4904. // and return as array
  4905. function extractFromCache( cache ) {
  4906. const values = [];
  4907. for ( const key in cache ) {
  4908. const data = cache[ key ];
  4909. delete data.metadata;
  4910. values.push( data );
  4911. }
  4912. return values;
  4913. }
  4914. }
  4915. clone( recursive ) {
  4916. return new this.constructor().copy( this, recursive );
  4917. }
  4918. copy( source, recursive = true ) {
  4919. this.name = source.name;
  4920. this.up.copy( source.up );
  4921. this.position.copy( source.position );
  4922. this.rotation.order = source.rotation.order;
  4923. this.quaternion.copy( source.quaternion );
  4924. this.scale.copy( source.scale );
  4925. this.matrix.copy( source.matrix );
  4926. this.matrixWorld.copy( source.matrixWorld );
  4927. this.matrixAutoUpdate = source.matrixAutoUpdate;
  4928. this.matrixWorldAutoUpdate = source.matrixWorldAutoUpdate;
  4929. this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate;
  4930. this.layers.mask = source.layers.mask;
  4931. this.visible = source.visible;
  4932. this.castShadow = source.castShadow;
  4933. this.receiveShadow = source.receiveShadow;
  4934. this.frustumCulled = source.frustumCulled;
  4935. this.renderOrder = source.renderOrder;
  4936. this.animations = source.animations.slice();
  4937. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  4938. if ( recursive === true ) {
  4939. for ( let i = 0; i < source.children.length; i ++ ) {
  4940. const child = source.children[ i ];
  4941. this.add( child.clone() );
  4942. }
  4943. }
  4944. return this;
  4945. }
  4946. }
  4947. Object3D.DEFAULT_UP = /*@__PURE__*/ new Vector3( 0, 1, 0 );
  4948. Object3D.DEFAULT_MATRIX_AUTO_UPDATE = true;
  4949. Object3D.DEFAULT_MATRIX_WORLD_AUTO_UPDATE = true;
  4950. const _v0$1 = /*@__PURE__*/ new Vector3();
  4951. const _v1$3 = /*@__PURE__*/ new Vector3();
  4952. const _v2$2 = /*@__PURE__*/ new Vector3();
  4953. const _v3$2 = /*@__PURE__*/ new Vector3();
  4954. const _vab = /*@__PURE__*/ new Vector3();
  4955. const _vac = /*@__PURE__*/ new Vector3();
  4956. const _vbc = /*@__PURE__*/ new Vector3();
  4957. const _vap = /*@__PURE__*/ new Vector3();
  4958. const _vbp = /*@__PURE__*/ new Vector3();
  4959. const _vcp = /*@__PURE__*/ new Vector3();
  4960. const _v40 = /*@__PURE__*/ new Vector4();
  4961. const _v41 = /*@__PURE__*/ new Vector4();
  4962. const _v42 = /*@__PURE__*/ new Vector4();
  4963. class Triangle {
  4964. constructor( a = new Vector3(), b = new Vector3(), c = new Vector3() ) {
  4965. this.a = a;
  4966. this.b = b;
  4967. this.c = c;
  4968. }
  4969. static getNormal( a, b, c, target ) {
  4970. target.subVectors( c, b );
  4971. _v0$1.subVectors( a, b );
  4972. target.cross( _v0$1 );
  4973. const targetLengthSq = target.lengthSq();
  4974. if ( targetLengthSq > 0 ) {
  4975. return target.multiplyScalar( 1 / Math.sqrt( targetLengthSq ) );
  4976. }
  4977. return target.set( 0, 0, 0 );
  4978. }
  4979. // static/instance method to calculate barycentric coordinates
  4980. // based on: http://www.blackpawn.com/texts/pointinpoly/default.html
  4981. static getBarycoord( point, a, b, c, target ) {
  4982. _v0$1.subVectors( c, a );
  4983. _v1$3.subVectors( b, a );
  4984. _v2$2.subVectors( point, a );
  4985. const dot00 = _v0$1.dot( _v0$1 );
  4986. const dot01 = _v0$1.dot( _v1$3 );
  4987. const dot02 = _v0$1.dot( _v2$2 );
  4988. const dot11 = _v1$3.dot( _v1$3 );
  4989. const dot12 = _v1$3.dot( _v2$2 );
  4990. const denom = ( dot00 * dot11 - dot01 * dot01 );
  4991. // collinear or singular triangle
  4992. if ( denom === 0 ) {
  4993. target.set( 0, 0, 0 );
  4994. return null;
  4995. }
  4996. const invDenom = 1 / denom;
  4997. const u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  4998. const v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  4999. // barycentric coordinates must always sum to 1
  5000. return target.set( 1 - u - v, v, u );
  5001. }
  5002. static containsPoint( point, a, b, c ) {
  5003. // if the triangle is degenerate then we can't contain a point
  5004. if ( this.getBarycoord( point, a, b, c, _v3$2 ) === null ) {
  5005. return false;
  5006. }
  5007. return ( _v3$2.x >= 0 ) && ( _v3$2.y >= 0 ) && ( ( _v3$2.x + _v3$2.y ) <= 1 );
  5008. }
  5009. static getInterpolation( point, p1, p2, p3, v1, v2, v3, target ) {
  5010. if ( this.getBarycoord( point, p1, p2, p3, _v3$2 ) === null ) {
  5011. target.x = 0;
  5012. target.y = 0;
  5013. if ( 'z' in target ) target.z = 0;
  5014. if ( 'w' in target ) target.w = 0;
  5015. return null;
  5016. }
  5017. target.setScalar( 0 );
  5018. target.addScaledVector( v1, _v3$2.x );
  5019. target.addScaledVector( v2, _v3$2.y );
  5020. target.addScaledVector( v3, _v3$2.z );
  5021. return target;
  5022. }
  5023. static getInterpolatedAttribute( attr, i1, i2, i3, barycoord, target ) {
  5024. _v40.setScalar( 0 );
  5025. _v41.setScalar( 0 );
  5026. _v42.setScalar( 0 );
  5027. _v40.fromBufferAttribute( attr, i1 );
  5028. _v41.fromBufferAttribute( attr, i2 );
  5029. _v42.fromBufferAttribute( attr, i3 );
  5030. target.setScalar( 0 );
  5031. target.addScaledVector( _v40, barycoord.x );
  5032. target.addScaledVector( _v41, barycoord.y );
  5033. target.addScaledVector( _v42, barycoord.z );
  5034. return target;
  5035. }
  5036. static isFrontFacing( a, b, c, direction ) {
  5037. _v0$1.subVectors( c, b );
  5038. _v1$3.subVectors( a, b );
  5039. // strictly front facing
  5040. return ( _v0$1.cross( _v1$3 ).dot( direction ) < 0 ) ? true : false;
  5041. }
  5042. set( a, b, c ) {
  5043. this.a.copy( a );
  5044. this.b.copy( b );
  5045. this.c.copy( c );
  5046. return this;
  5047. }
  5048. setFromPointsAndIndices( points, i0, i1, i2 ) {
  5049. this.a.copy( points[ i0 ] );
  5050. this.b.copy( points[ i1 ] );
  5051. this.c.copy( points[ i2 ] );
  5052. return this;
  5053. }
  5054. setFromAttributeAndIndices( attribute, i0, i1, i2 ) {
  5055. this.a.fromBufferAttribute( attribute, i0 );
  5056. this.b.fromBufferAttribute( attribute, i1 );
  5057. this.c.fromBufferAttribute( attribute, i2 );
  5058. return this;
  5059. }
  5060. clone() {
  5061. return new this.constructor().copy( this );
  5062. }
  5063. copy( triangle ) {
  5064. this.a.copy( triangle.a );
  5065. this.b.copy( triangle.b );
  5066. this.c.copy( triangle.c );
  5067. return this;
  5068. }
  5069. getArea() {
  5070. _v0$1.subVectors( this.c, this.b );
  5071. _v1$3.subVectors( this.a, this.b );
  5072. return _v0$1.cross( _v1$3 ).length() * 0.5;
  5073. }
  5074. getMidpoint( target ) {
  5075. return target.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
  5076. }
  5077. getNormal( target ) {
  5078. return Triangle.getNormal( this.a, this.b, this.c, target );
  5079. }
  5080. getPlane( target ) {
  5081. return target.setFromCoplanarPoints( this.a, this.b, this.c );
  5082. }
  5083. getBarycoord( point, target ) {
  5084. return Triangle.getBarycoord( point, this.a, this.b, this.c, target );
  5085. }
  5086. getInterpolation( point, v1, v2, v3, target ) {
  5087. return Triangle.getInterpolation( point, this.a, this.b, this.c, v1, v2, v3, target );
  5088. }
  5089. containsPoint( point ) {
  5090. return Triangle.containsPoint( point, this.a, this.b, this.c );
  5091. }
  5092. isFrontFacing( direction ) {
  5093. return Triangle.isFrontFacing( this.a, this.b, this.c, direction );
  5094. }
  5095. intersectsBox( box ) {
  5096. return box.intersectsTriangle( this );
  5097. }
  5098. closestPointToPoint( p, target ) {
  5099. const a = this.a, b = this.b, c = this.c;
  5100. let v, w;
  5101. // algorithm thanks to Real-Time Collision Detection by Christer Ericson,
  5102. // published by Morgan Kaufmann Publishers, (c) 2005 Elsevier Inc.,
  5103. // under the accompanying license; see chapter 5.1.5 for detailed explanation.
  5104. // basically, we're distinguishing which of the voronoi regions of the triangle
  5105. // the point lies in with the minimum amount of redundant computation.
  5106. _vab.subVectors( b, a );
  5107. _vac.subVectors( c, a );
  5108. _vap.subVectors( p, a );
  5109. const d1 = _vab.dot( _vap );
  5110. const d2 = _vac.dot( _vap );
  5111. if ( d1 <= 0 && d2 <= 0 ) {
  5112. // vertex region of A; barycentric coords (1, 0, 0)
  5113. return target.copy( a );
  5114. }
  5115. _vbp.subVectors( p, b );
  5116. const d3 = _vab.dot( _vbp );
  5117. const d4 = _vac.dot( _vbp );
  5118. if ( d3 >= 0 && d4 <= d3 ) {
  5119. // vertex region of B; barycentric coords (0, 1, 0)
  5120. return target.copy( b );
  5121. }
  5122. const vc = d1 * d4 - d3 * d2;
  5123. if ( vc <= 0 && d1 >= 0 && d3 <= 0 ) {
  5124. v = d1 / ( d1 - d3 );
  5125. // edge region of AB; barycentric coords (1-v, v, 0)
  5126. return target.copy( a ).addScaledVector( _vab, v );
  5127. }
  5128. _vcp.subVectors( p, c );
  5129. const d5 = _vab.dot( _vcp );
  5130. const d6 = _vac.dot( _vcp );
  5131. if ( d6 >= 0 && d5 <= d6 ) {
  5132. // vertex region of C; barycentric coords (0, 0, 1)
  5133. return target.copy( c );
  5134. }
  5135. const vb = d5 * d2 - d1 * d6;
  5136. if ( vb <= 0 && d2 >= 0 && d6 <= 0 ) {
  5137. w = d2 / ( d2 - d6 );
  5138. // edge region of AC; barycentric coords (1-w, 0, w)
  5139. return target.copy( a ).addScaledVector( _vac, w );
  5140. }
  5141. const va = d3 * d6 - d5 * d4;
  5142. if ( va <= 0 && ( d4 - d3 ) >= 0 && ( d5 - d6 ) >= 0 ) {
  5143. _vbc.subVectors( c, b );
  5144. w = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) );
  5145. // edge region of BC; barycentric coords (0, 1-w, w)
  5146. return target.copy( b ).addScaledVector( _vbc, w ); // edge region of BC
  5147. }
  5148. // face region
  5149. const denom = 1 / ( va + vb + vc );
  5150. // u = va * denom
  5151. v = vb * denom;
  5152. w = vc * denom;
  5153. return target.copy( a ).addScaledVector( _vab, v ).addScaledVector( _vac, w );
  5154. }
  5155. equals( triangle ) {
  5156. return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
  5157. }
  5158. }
  5159. const _colorKeywords = { 'aliceblue': 0xF0F8FF, 'antiquewhite': 0xFAEBD7, 'aqua': 0x00FFFF, 'aquamarine': 0x7FFFD4, 'azure': 0xF0FFFF,
  5160. 'beige': 0xF5F5DC, 'bisque': 0xFFE4C4, 'black': 0x000000, 'blanchedalmond': 0xFFEBCD, 'blue': 0x0000FF, 'blueviolet': 0x8A2BE2,
  5161. 'brown': 0xA52A2A, 'burlywood': 0xDEB887, 'cadetblue': 0x5F9EA0, 'chartreuse': 0x7FFF00, 'chocolate': 0xD2691E, 'coral': 0xFF7F50,
  5162. 'cornflowerblue': 0x6495ED, 'cornsilk': 0xFFF8DC, 'crimson': 0xDC143C, 'cyan': 0x00FFFF, 'darkblue': 0x00008B, 'darkcyan': 0x008B8B,
  5163. 'darkgoldenrod': 0xB8860B, 'darkgray': 0xA9A9A9, 'darkgreen': 0x006400, 'darkgrey': 0xA9A9A9, 'darkkhaki': 0xBDB76B, 'darkmagenta': 0x8B008B,
  5164. 'darkolivegreen': 0x556B2F, 'darkorange': 0xFF8C00, 'darkorchid': 0x9932CC, 'darkred': 0x8B0000, 'darksalmon': 0xE9967A, 'darkseagreen': 0x8FBC8F,
  5165. 'darkslateblue': 0x483D8B, 'darkslategray': 0x2F4F4F, 'darkslategrey': 0x2F4F4F, 'darkturquoise': 0x00CED1, 'darkviolet': 0x9400D3,
  5166. 'deeppink': 0xFF1493, 'deepskyblue': 0x00BFFF, 'dimgray': 0x696969, 'dimgrey': 0x696969, 'dodgerblue': 0x1E90FF, 'firebrick': 0xB22222,
  5167. 'floralwhite': 0xFFFAF0, 'forestgreen': 0x228B22, 'fuchsia': 0xFF00FF, 'gainsboro': 0xDCDCDC, 'ghostwhite': 0xF8F8FF, 'gold': 0xFFD700,
  5168. 'goldenrod': 0xDAA520, 'gray': 0x808080, 'green': 0x008000, 'greenyellow': 0xADFF2F, 'grey': 0x808080, 'honeydew': 0xF0FFF0, 'hotpink': 0xFF69B4,
  5169. 'indianred': 0xCD5C5C, 'indigo': 0x4B0082, 'ivory': 0xFFFFF0, 'khaki': 0xF0E68C, 'lavender': 0xE6E6FA, 'lavenderblush': 0xFFF0F5, 'lawngreen': 0x7CFC00,
  5170. 'lemonchiffon': 0xFFFACD, 'lightblue': 0xADD8E6, 'lightcoral': 0xF08080, 'lightcyan': 0xE0FFFF, 'lightgoldenrodyellow': 0xFAFAD2, 'lightgray': 0xD3D3D3,
  5171. 'lightgreen': 0x90EE90, 'lightgrey': 0xD3D3D3, 'lightpink': 0xFFB6C1, 'lightsalmon': 0xFFA07A, 'lightseagreen': 0x20B2AA, 'lightskyblue': 0x87CEFA,
  5172. 'lightslategray': 0x778899, 'lightslategrey': 0x778899, 'lightsteelblue': 0xB0C4DE, 'lightyellow': 0xFFFFE0, 'lime': 0x00FF00, 'limegreen': 0x32CD32,
  5173. 'linen': 0xFAF0E6, 'magenta': 0xFF00FF, 'maroon': 0x800000, 'mediumaquamarine': 0x66CDAA, 'mediumblue': 0x0000CD, 'mediumorchid': 0xBA55D3,
  5174. 'mediumpurple': 0x9370DB, 'mediumseagreen': 0x3CB371, 'mediumslateblue': 0x7B68EE, 'mediumspringgreen': 0x00FA9A, 'mediumturquoise': 0x48D1CC,
  5175. 'mediumvioletred': 0xC71585, 'midnightblue': 0x191970, 'mintcream': 0xF5FFFA, 'mistyrose': 0xFFE4E1, 'moccasin': 0xFFE4B5, 'navajowhite': 0xFFDEAD,
  5176. 'navy': 0x000080, 'oldlace': 0xFDF5E6, 'olive': 0x808000, 'olivedrab': 0x6B8E23, 'orange': 0xFFA500, 'orangered': 0xFF4500, 'orchid': 0xDA70D6,
  5177. 'palegoldenrod': 0xEEE8AA, 'palegreen': 0x98FB98, 'paleturquoise': 0xAFEEEE, 'palevioletred': 0xDB7093, 'papayawhip': 0xFFEFD5, 'peachpuff': 0xFFDAB9,
  5178. 'peru': 0xCD853F, 'pink': 0xFFC0CB, 'plum': 0xDDA0DD, 'powderblue': 0xB0E0E6, 'purple': 0x800080, 'rebeccapurple': 0x663399, 'red': 0xFF0000, 'rosybrown': 0xBC8F8F,
  5179. 'royalblue': 0x4169E1, 'saddlebrown': 0x8B4513, 'salmon': 0xFA8072, 'sandybrown': 0xF4A460, 'seagreen': 0x2E8B57, 'seashell': 0xFFF5EE,
  5180. 'sienna': 0xA0522D, 'silver': 0xC0C0C0, 'skyblue': 0x87CEEB, 'slateblue': 0x6A5ACD, 'slategray': 0x708090, 'slategrey': 0x708090, 'snow': 0xFFFAFA,
  5181. 'springgreen': 0x00FF7F, 'steelblue': 0x4682B4, 'tan': 0xD2B48C, 'teal': 0x008080, 'thistle': 0xD8BFD8, 'tomato': 0xFF6347, 'turquoise': 0x40E0D0,
  5182. 'violet': 0xEE82EE, 'wheat': 0xF5DEB3, 'white': 0xFFFFFF, 'whitesmoke': 0xF5F5F5, 'yellow': 0xFFFF00, 'yellowgreen': 0x9ACD32 };
  5183. const _hslA = { h: 0, s: 0, l: 0 };
  5184. const _hslB = { h: 0, s: 0, l: 0 };
  5185. function hue2rgb( p, q, t ) {
  5186. if ( t < 0 ) t += 1;
  5187. if ( t > 1 ) t -= 1;
  5188. if ( t < 1 / 6 ) return p + ( q - p ) * 6 * t;
  5189. if ( t < 1 / 2 ) return q;
  5190. if ( t < 2 / 3 ) return p + ( q - p ) * 6 * ( 2 / 3 - t );
  5191. return p;
  5192. }
  5193. class Color {
  5194. constructor( r, g, b ) {
  5195. this.isColor = true;
  5196. this.r = 1;
  5197. this.g = 1;
  5198. this.b = 1;
  5199. return this.set( r, g, b );
  5200. }
  5201. set( r, g, b ) {
  5202. if ( g === undefined && b === undefined ) {
  5203. // r is THREE.Color, hex or string
  5204. const value = r;
  5205. if ( value && value.isColor ) {
  5206. this.copy( value );
  5207. } else if ( typeof value === 'number' ) {
  5208. this.setHex( value );
  5209. } else if ( typeof value === 'string' ) {
  5210. this.setStyle( value );
  5211. }
  5212. } else {
  5213. this.setRGB( r, g, b );
  5214. }
  5215. return this;
  5216. }
  5217. setScalar( scalar ) {
  5218. this.r = scalar;
  5219. this.g = scalar;
  5220. this.b = scalar;
  5221. return this;
  5222. }
  5223. setHex( hex, colorSpace = SRGBColorSpace ) {
  5224. hex = Math.floor( hex );
  5225. this.r = ( hex >> 16 & 255 ) / 255;
  5226. this.g = ( hex >> 8 & 255 ) / 255;
  5227. this.b = ( hex & 255 ) / 255;
  5228. ColorManagement.toWorkingColorSpace( this, colorSpace );
  5229. return this;
  5230. }
  5231. setRGB( r, g, b, colorSpace = ColorManagement.workingColorSpace ) {
  5232. this.r = r;
  5233. this.g = g;
  5234. this.b = b;
  5235. ColorManagement.toWorkingColorSpace( this, colorSpace );
  5236. return this;
  5237. }
  5238. setHSL( h, s, l, colorSpace = ColorManagement.workingColorSpace ) {
  5239. // h,s,l ranges are in 0.0 - 1.0
  5240. h = euclideanModulo( h, 1 );
  5241. s = clamp( s, 0, 1 );
  5242. l = clamp( l, 0, 1 );
  5243. if ( s === 0 ) {
  5244. this.r = this.g = this.b = l;
  5245. } else {
  5246. const p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s );
  5247. const q = ( 2 * l ) - p;
  5248. this.r = hue2rgb( q, p, h + 1 / 3 );
  5249. this.g = hue2rgb( q, p, h );
  5250. this.b = hue2rgb( q, p, h - 1 / 3 );
  5251. }
  5252. ColorManagement.toWorkingColorSpace( this, colorSpace );
  5253. return this;
  5254. }
  5255. setStyle( style, colorSpace = SRGBColorSpace ) {
  5256. function handleAlpha( string ) {
  5257. if ( string === undefined ) return;
  5258. if ( parseFloat( string ) < 1 ) {
  5259. console.warn( 'THREE.Color: Alpha component of ' + style + ' will be ignored.' );
  5260. }
  5261. }
  5262. let m;
  5263. if ( m = /^(\w+)\(([^\)]*)\)/.exec( style ) ) {
  5264. // rgb / hsl
  5265. let color;
  5266. const name = m[ 1 ];
  5267. const components = m[ 2 ];
  5268. switch ( name ) {
  5269. case 'rgb':
  5270. case 'rgba':
  5271. if ( color = /^\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
  5272. // rgb(255,0,0) rgba(255,0,0,0.5)
  5273. handleAlpha( color[ 4 ] );
  5274. return this.setRGB(
  5275. Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255,
  5276. Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255,
  5277. Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255,
  5278. colorSpace
  5279. );
  5280. }
  5281. if ( color = /^\s*(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
  5282. // rgb(100%,0%,0%) rgba(100%,0%,0%,0.5)
  5283. handleAlpha( color[ 4 ] );
  5284. return this.setRGB(
  5285. Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100,
  5286. Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100,
  5287. Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100,
  5288. colorSpace
  5289. );
  5290. }
  5291. break;
  5292. case 'hsl':
  5293. case 'hsla':
  5294. if ( color = /^\s*(\d*\.?\d+)\s*,\s*(\d*\.?\d+)\%\s*,\s*(\d*\.?\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) {
  5295. // hsl(120,50%,50%) hsla(120,50%,50%,0.5)
  5296. handleAlpha( color[ 4 ] );
  5297. return this.setHSL(
  5298. parseFloat( color[ 1 ] ) / 360,
  5299. parseFloat( color[ 2 ] ) / 100,
  5300. parseFloat( color[ 3 ] ) / 100,
  5301. colorSpace
  5302. );
  5303. }
  5304. break;
  5305. default:
  5306. console.warn( 'THREE.Color: Unknown color model ' + style );
  5307. }
  5308. } else if ( m = /^\#([A-Fa-f\d]+)$/.exec( style ) ) {
  5309. // hex color
  5310. const hex = m[ 1 ];
  5311. const size = hex.length;
  5312. if ( size === 3 ) {
  5313. // #ff0
  5314. return this.setRGB(
  5315. parseInt( hex.charAt( 0 ), 16 ) / 15,
  5316. parseInt( hex.charAt( 1 ), 16 ) / 15,
  5317. parseInt( hex.charAt( 2 ), 16 ) / 15,
  5318. colorSpace
  5319. );
  5320. } else if ( size === 6 ) {
  5321. // #ff0000
  5322. return this.setHex( parseInt( hex, 16 ), colorSpace );
  5323. } else {
  5324. console.warn( 'THREE.Color: Invalid hex color ' + style );
  5325. }
  5326. } else if ( style && style.length > 0 ) {
  5327. return this.setColorName( style, colorSpace );
  5328. }
  5329. return this;
  5330. }
  5331. setColorName( style, colorSpace = SRGBColorSpace ) {
  5332. // color keywords
  5333. const hex = _colorKeywords[ style.toLowerCase() ];
  5334. if ( hex !== undefined ) {
  5335. // red
  5336. this.setHex( hex, colorSpace );
  5337. } else {
  5338. // unknown color
  5339. console.warn( 'THREE.Color: Unknown color ' + style );
  5340. }
  5341. return this;
  5342. }
  5343. clone() {
  5344. return new this.constructor( this.r, this.g, this.b );
  5345. }
  5346. copy( color ) {
  5347. this.r = color.r;
  5348. this.g = color.g;
  5349. this.b = color.b;
  5350. return this;
  5351. }
  5352. copySRGBToLinear( color ) {
  5353. this.r = SRGBToLinear( color.r );
  5354. this.g = SRGBToLinear( color.g );
  5355. this.b = SRGBToLinear( color.b );
  5356. return this;
  5357. }
  5358. copyLinearToSRGB( color ) {
  5359. this.r = LinearToSRGB( color.r );
  5360. this.g = LinearToSRGB( color.g );
  5361. this.b = LinearToSRGB( color.b );
  5362. return this;
  5363. }
  5364. convertSRGBToLinear() {
  5365. this.copySRGBToLinear( this );
  5366. return this;
  5367. }
  5368. convertLinearToSRGB() {
  5369. this.copyLinearToSRGB( this );
  5370. return this;
  5371. }
  5372. getHex( colorSpace = SRGBColorSpace ) {
  5373. ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace );
  5374. 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 ) );
  5375. }
  5376. getHexString( colorSpace = SRGBColorSpace ) {
  5377. return ( '000000' + this.getHex( colorSpace ).toString( 16 ) ).slice( - 6 );
  5378. }
  5379. getHSL( target, colorSpace = ColorManagement.workingColorSpace ) {
  5380. // h,s,l ranges are in 0.0 - 1.0
  5381. ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace );
  5382. const r = _color.r, g = _color.g, b = _color.b;
  5383. const max = Math.max( r, g, b );
  5384. const min = Math.min( r, g, b );
  5385. let hue, saturation;
  5386. const lightness = ( min + max ) / 2.0;
  5387. if ( min === max ) {
  5388. hue = 0;
  5389. saturation = 0;
  5390. } else {
  5391. const delta = max - min;
  5392. saturation = lightness <= 0.5 ? delta / ( max + min ) : delta / ( 2 - max - min );
  5393. switch ( max ) {
  5394. case r: hue = ( g - b ) / delta + ( g < b ? 6 : 0 ); break;
  5395. case g: hue = ( b - r ) / delta + 2; break;
  5396. case b: hue = ( r - g ) / delta + 4; break;
  5397. }
  5398. hue /= 6;
  5399. }
  5400. target.h = hue;
  5401. target.s = saturation;
  5402. target.l = lightness;
  5403. return target;
  5404. }
  5405. getRGB( target, colorSpace = ColorManagement.workingColorSpace ) {
  5406. ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace );
  5407. target.r = _color.r;
  5408. target.g = _color.g;
  5409. target.b = _color.b;
  5410. return target;
  5411. }
  5412. getStyle( colorSpace = SRGBColorSpace ) {
  5413. ColorManagement.fromWorkingColorSpace( _color.copy( this ), colorSpace );
  5414. const r = _color.r, g = _color.g, b = _color.b;
  5415. if ( colorSpace !== SRGBColorSpace ) {
  5416. // Requires CSS Color Module Level 4 (https://www.w3.org/TR/css-color-4/).
  5417. return `color(${ colorSpace } ${ r.toFixed( 3 ) } ${ g.toFixed( 3 ) } ${ b.toFixed( 3 ) })`;
  5418. }
  5419. return `rgb(${ Math.round( r * 255 ) },${ Math.round( g * 255 ) },${ Math.round( b * 255 ) })`;
  5420. }
  5421. offsetHSL( h, s, l ) {
  5422. this.getHSL( _hslA );
  5423. return this.setHSL( _hslA.h + h, _hslA.s + s, _hslA.l + l );
  5424. }
  5425. add( color ) {
  5426. this.r += color.r;
  5427. this.g += color.g;
  5428. this.b += color.b;
  5429. return this;
  5430. }
  5431. addColors( color1, color2 ) {
  5432. this.r = color1.r + color2.r;
  5433. this.g = color1.g + color2.g;
  5434. this.b = color1.b + color2.b;
  5435. return this;
  5436. }
  5437. addScalar( s ) {
  5438. this.r += s;
  5439. this.g += s;
  5440. this.b += s;
  5441. return this;
  5442. }
  5443. sub( color ) {
  5444. this.r = Math.max( 0, this.r - color.r );
  5445. this.g = Math.max( 0, this.g - color.g );
  5446. this.b = Math.max( 0, this.b - color.b );
  5447. return this;
  5448. }
  5449. multiply( color ) {
  5450. this.r *= color.r;
  5451. this.g *= color.g;
  5452. this.b *= color.b;
  5453. return this;
  5454. }
  5455. multiplyScalar( s ) {
  5456. this.r *= s;
  5457. this.g *= s;
  5458. this.b *= s;
  5459. return this;
  5460. }
  5461. lerp( color, alpha ) {
  5462. this.r += ( color.r - this.r ) * alpha;
  5463. this.g += ( color.g - this.g ) * alpha;
  5464. this.b += ( color.b - this.b ) * alpha;
  5465. return this;
  5466. }
  5467. lerpColors( color1, color2, alpha ) {
  5468. this.r = color1.r + ( color2.r - color1.r ) * alpha;
  5469. this.g = color1.g + ( color2.g - color1.g ) * alpha;
  5470. this.b = color1.b + ( color2.b - color1.b ) * alpha;
  5471. return this;
  5472. }
  5473. lerpHSL( color, alpha ) {
  5474. this.getHSL( _hslA );
  5475. color.getHSL( _hslB );
  5476. const h = lerp( _hslA.h, _hslB.h, alpha );
  5477. const s = lerp( _hslA.s, _hslB.s, alpha );
  5478. const l = lerp( _hslA.l, _hslB.l, alpha );
  5479. this.setHSL( h, s, l );
  5480. return this;
  5481. }
  5482. setFromVector3( v ) {
  5483. this.r = v.x;
  5484. this.g = v.y;
  5485. this.b = v.z;
  5486. return this;
  5487. }
  5488. applyMatrix3( m ) {
  5489. const r = this.r, g = this.g, b = this.b;
  5490. const e = m.elements;
  5491. this.r = e[ 0 ] * r + e[ 3 ] * g + e[ 6 ] * b;
  5492. this.g = e[ 1 ] * r + e[ 4 ] * g + e[ 7 ] * b;
  5493. this.b = e[ 2 ] * r + e[ 5 ] * g + e[ 8 ] * b;
  5494. return this;
  5495. }
  5496. equals( c ) {
  5497. return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b );
  5498. }
  5499. fromArray( array, offset = 0 ) {
  5500. this.r = array[ offset ];
  5501. this.g = array[ offset + 1 ];
  5502. this.b = array[ offset + 2 ];
  5503. return this;
  5504. }
  5505. toArray( array = [], offset = 0 ) {
  5506. array[ offset ] = this.r;
  5507. array[ offset + 1 ] = this.g;
  5508. array[ offset + 2 ] = this.b;
  5509. return array;
  5510. }
  5511. fromBufferAttribute( attribute, index ) {
  5512. this.r = attribute.getX( index );
  5513. this.g = attribute.getY( index );
  5514. this.b = attribute.getZ( index );
  5515. return this;
  5516. }
  5517. toJSON() {
  5518. return this.getHex();
  5519. }
  5520. *[ Symbol.iterator ]() {
  5521. yield this.r;
  5522. yield this.g;
  5523. yield this.b;
  5524. }
  5525. }
  5526. const _color = /*@__PURE__*/ new Color();
  5527. Color.NAMES = _colorKeywords;
  5528. let _materialId = 0;
  5529. class Material extends EventDispatcher {
  5530. constructor() {
  5531. super();
  5532. this.isMaterial = true;
  5533. Object.defineProperty( this, 'id', { value: _materialId ++ } );
  5534. this.uuid = generateUUID();
  5535. this.name = '';
  5536. this.type = 'Material';
  5537. this.blending = NormalBlending;
  5538. this.side = FrontSide;
  5539. this.vertexColors = false;
  5540. this.opacity = 1;
  5541. this.transparent = false;
  5542. this.alphaHash = false;
  5543. this.blendSrc = SrcAlphaFactor;
  5544. this.blendDst = OneMinusSrcAlphaFactor;
  5545. this.blendEquation = AddEquation;
  5546. this.blendSrcAlpha = null;
  5547. this.blendDstAlpha = null;
  5548. this.blendEquationAlpha = null;
  5549. this.blendColor = new Color( 0, 0, 0 );
  5550. this.blendAlpha = 0;
  5551. this.depthFunc = LessEqualDepth;
  5552. this.depthTest = true;
  5553. this.depthWrite = true;
  5554. this.stencilWriteMask = 0xff;
  5555. this.stencilFunc = AlwaysStencilFunc;
  5556. this.stencilRef = 0;
  5557. this.stencilFuncMask = 0xff;
  5558. this.stencilFail = KeepStencilOp;
  5559. this.stencilZFail = KeepStencilOp;
  5560. this.stencilZPass = KeepStencilOp;
  5561. this.stencilWrite = false;
  5562. this.clippingPlanes = null;
  5563. this.clipIntersection = false;
  5564. this.clipShadows = false;
  5565. this.shadowSide = null;
  5566. this.colorWrite = true;
  5567. this.precision = null; // override the renderer's default precision for this material
  5568. this.polygonOffset = false;
  5569. this.polygonOffsetFactor = 0;
  5570. this.polygonOffsetUnits = 0;
  5571. this.dithering = false;
  5572. this.alphaToCoverage = false;
  5573. this.premultipliedAlpha = false;
  5574. this.forceSinglePass = false;
  5575. this.visible = true;
  5576. this.toneMapped = true;
  5577. this.userData = {};
  5578. this.version = 0;
  5579. this._alphaTest = 0;
  5580. }
  5581. get alphaTest() {
  5582. return this._alphaTest;
  5583. }
  5584. set alphaTest( value ) {
  5585. if ( this._alphaTest > 0 !== value > 0 ) {
  5586. this.version ++;
  5587. }
  5588. this._alphaTest = value;
  5589. }
  5590. // onBeforeRender and onBeforeCompile only supported in WebGLRenderer
  5591. onBeforeRender( /* renderer, scene, camera, geometry, object, group */ ) {}
  5592. onBeforeCompile( /* shaderobject, renderer */ ) {}
  5593. customProgramCacheKey() {
  5594. return this.onBeforeCompile.toString();
  5595. }
  5596. setValues( values ) {
  5597. if ( values === undefined ) return;
  5598. for ( const key in values ) {
  5599. const newValue = values[ key ];
  5600. if ( newValue === undefined ) {
  5601. console.warn( `THREE.Material: parameter '${ key }' has value of undefined.` );
  5602. continue;
  5603. }
  5604. const currentValue = this[ key ];
  5605. if ( currentValue === undefined ) {
  5606. console.warn( `THREE.Material: '${ key }' is not a property of THREE.${ this.type }.` );
  5607. continue;
  5608. }
  5609. if ( currentValue && currentValue.isColor ) {
  5610. currentValue.set( newValue );
  5611. } else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) {
  5612. currentValue.copy( newValue );
  5613. } else {
  5614. this[ key ] = newValue;
  5615. }
  5616. }
  5617. }
  5618. toJSON( meta ) {
  5619. const isRootObject = ( meta === undefined || typeof meta === 'string' );
  5620. if ( isRootObject ) {
  5621. meta = {
  5622. textures: {},
  5623. images: {}
  5624. };
  5625. }
  5626. const data = {
  5627. metadata: {
  5628. version: 4.6,
  5629. type: 'Material',
  5630. generator: 'Material.toJSON'
  5631. }
  5632. };
  5633. // standard Material serialization
  5634. data.uuid = this.uuid;
  5635. data.type = this.type;
  5636. if ( this.name !== '' ) data.name = this.name;
  5637. if ( this.color && this.color.isColor ) data.color = this.color.getHex();
  5638. if ( this.roughness !== undefined ) data.roughness = this.roughness;
  5639. if ( this.metalness !== undefined ) data.metalness = this.metalness;
  5640. if ( this.sheen !== undefined ) data.sheen = this.sheen;
  5641. if ( this.sheenColor && this.sheenColor.isColor ) data.sheenColor = this.sheenColor.getHex();
  5642. if ( this.sheenRoughness !== undefined ) data.sheenRoughness = this.sheenRoughness;
  5643. if ( this.emissive && this.emissive.isColor ) data.emissive = this.emissive.getHex();
  5644. if ( this.emissiveIntensity !== undefined && this.emissiveIntensity !== 1 ) data.emissiveIntensity = this.emissiveIntensity;
  5645. if ( this.specular && this.specular.isColor ) data.specular = this.specular.getHex();
  5646. if ( this.specularIntensity !== undefined ) data.specularIntensity = this.specularIntensity;
  5647. if ( this.specularColor && this.specularColor.isColor ) data.specularColor = this.specularColor.getHex();
  5648. if ( this.shininess !== undefined ) data.shininess = this.shininess;
  5649. if ( this.clearcoat !== undefined ) data.clearcoat = this.clearcoat;
  5650. if ( this.clearcoatRoughness !== undefined ) data.clearcoatRoughness = this.clearcoatRoughness;
  5651. if ( this.clearcoatMap && this.clearcoatMap.isTexture ) {
  5652. data.clearcoatMap = this.clearcoatMap.toJSON( meta ).uuid;
  5653. }
  5654. if ( this.clearcoatRoughnessMap && this.clearcoatRoughnessMap.isTexture ) {
  5655. data.clearcoatRoughnessMap = this.clearcoatRoughnessMap.toJSON( meta ).uuid;
  5656. }
  5657. if ( this.clearcoatNormalMap && this.clearcoatNormalMap.isTexture ) {
  5658. data.clearcoatNormalMap = this.clearcoatNormalMap.toJSON( meta ).uuid;
  5659. data.clearcoatNormalScale = this.clearcoatNormalScale.toArray();
  5660. }
  5661. if ( this.dispersion !== undefined ) data.dispersion = this.dispersion;
  5662. if ( this.iridescence !== undefined ) data.iridescence = this.iridescence;
  5663. if ( this.iridescenceIOR !== undefined ) data.iridescenceIOR = this.iridescenceIOR;
  5664. if ( this.iridescenceThicknessRange !== undefined ) data.iridescenceThicknessRange = this.iridescenceThicknessRange;
  5665. if ( this.iridescenceMap && this.iridescenceMap.isTexture ) {
  5666. data.iridescenceMap = this.iridescenceMap.toJSON( meta ).uuid;
  5667. }
  5668. if ( this.iridescenceThicknessMap && this.iridescenceThicknessMap.isTexture ) {
  5669. data.iridescenceThicknessMap = this.iridescenceThicknessMap.toJSON( meta ).uuid;
  5670. }
  5671. if ( this.anisotropy !== undefined ) data.anisotropy = this.anisotropy;
  5672. if ( this.anisotropyRotation !== undefined ) data.anisotropyRotation = this.anisotropyRotation;
  5673. if ( this.anisotropyMap && this.anisotropyMap.isTexture ) {
  5674. data.anisotropyMap = this.anisotropyMap.toJSON( meta ).uuid;
  5675. }
  5676. if ( this.map && this.map.isTexture ) data.map = this.map.toJSON( meta ).uuid;
  5677. if ( this.matcap && this.matcap.isTexture ) data.matcap = this.matcap.toJSON( meta ).uuid;
  5678. if ( this.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid;
  5679. if ( this.lightMap && this.lightMap.isTexture ) {
  5680. data.lightMap = this.lightMap.toJSON( meta ).uuid;
  5681. data.lightMapIntensity = this.lightMapIntensity;
  5682. }
  5683. if ( this.aoMap && this.aoMap.isTexture ) {
  5684. data.aoMap = this.aoMap.toJSON( meta ).uuid;
  5685. data.aoMapIntensity = this.aoMapIntensity;
  5686. }
  5687. if ( this.bumpMap && this.bumpMap.isTexture ) {
  5688. data.bumpMap = this.bumpMap.toJSON( meta ).uuid;
  5689. data.bumpScale = this.bumpScale;
  5690. }
  5691. if ( this.normalMap && this.normalMap.isTexture ) {
  5692. data.normalMap = this.normalMap.toJSON( meta ).uuid;
  5693. data.normalMapType = this.normalMapType;
  5694. data.normalScale = this.normalScale.toArray();
  5695. }
  5696. if ( this.displacementMap && this.displacementMap.isTexture ) {
  5697. data.displacementMap = this.displacementMap.toJSON( meta ).uuid;
  5698. data.displacementScale = this.displacementScale;
  5699. data.displacementBias = this.displacementBias;
  5700. }
  5701. if ( this.roughnessMap && this.roughnessMap.isTexture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid;
  5702. if ( this.metalnessMap && this.metalnessMap.isTexture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid;
  5703. if ( this.emissiveMap && this.emissiveMap.isTexture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid;
  5704. if ( this.specularMap && this.specularMap.isTexture ) data.specularMap = this.specularMap.toJSON( meta ).uuid;
  5705. if ( this.specularIntensityMap && this.specularIntensityMap.isTexture ) data.specularIntensityMap = this.specularIntensityMap.toJSON( meta ).uuid;
  5706. if ( this.specularColorMap && this.specularColorMap.isTexture ) data.specularColorMap = this.specularColorMap.toJSON( meta ).uuid;
  5707. if ( this.envMap && this.envMap.isTexture ) {
  5708. data.envMap = this.envMap.toJSON( meta ).uuid;
  5709. if ( this.combine !== undefined ) data.combine = this.combine;
  5710. }
  5711. if ( this.envMapRotation !== undefined ) data.envMapRotation = this.envMapRotation.toArray();
  5712. if ( this.envMapIntensity !== undefined ) data.envMapIntensity = this.envMapIntensity;
  5713. if ( this.reflectivity !== undefined ) data.reflectivity = this.reflectivity;
  5714. if ( this.refractionRatio !== undefined ) data.refractionRatio = this.refractionRatio;
  5715. if ( this.gradientMap && this.gradientMap.isTexture ) {
  5716. data.gradientMap = this.gradientMap.toJSON( meta ).uuid;
  5717. }
  5718. if ( this.transmission !== undefined ) data.transmission = this.transmission;
  5719. if ( this.transmissionMap && this.transmissionMap.isTexture ) data.transmissionMap = this.transmissionMap.toJSON( meta ).uuid;
  5720. if ( this.thickness !== undefined ) data.thickness = this.thickness;
  5721. if ( this.thicknessMap && this.thicknessMap.isTexture ) data.thicknessMap = this.thicknessMap.toJSON( meta ).uuid;
  5722. if ( this.attenuationDistance !== undefined && this.attenuationDistance !== Infinity ) data.attenuationDistance = this.attenuationDistance;
  5723. if ( this.attenuationColor !== undefined ) data.attenuationColor = this.attenuationColor.getHex();
  5724. if ( this.size !== undefined ) data.size = this.size;
  5725. if ( this.shadowSide !== null ) data.shadowSide = this.shadowSide;
  5726. if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation;
  5727. if ( this.blending !== NormalBlending ) data.blending = this.blending;
  5728. if ( this.side !== FrontSide ) data.side = this.side;
  5729. if ( this.vertexColors === true ) data.vertexColors = true;
  5730. if ( this.opacity < 1 ) data.opacity = this.opacity;
  5731. if ( this.transparent === true ) data.transparent = true;
  5732. if ( this.blendSrc !== SrcAlphaFactor ) data.blendSrc = this.blendSrc;
  5733. if ( this.blendDst !== OneMinusSrcAlphaFactor ) data.blendDst = this.blendDst;
  5734. if ( this.blendEquation !== AddEquation ) data.blendEquation = this.blendEquation;
  5735. if ( this.blendSrcAlpha !== null ) data.blendSrcAlpha = this.blendSrcAlpha;
  5736. if ( this.blendDstAlpha !== null ) data.blendDstAlpha = this.blendDstAlpha;
  5737. if ( this.blendEquationAlpha !== null ) data.blendEquationAlpha = this.blendEquationAlpha;
  5738. if ( this.blendColor && this.blendColor.isColor ) data.blendColor = this.blendColor.getHex();
  5739. if ( this.blendAlpha !== 0 ) data.blendAlpha = this.blendAlpha;
  5740. if ( this.depthFunc !== LessEqualDepth ) data.depthFunc = this.depthFunc;
  5741. if ( this.depthTest === false ) data.depthTest = this.depthTest;
  5742. if ( this.depthWrite === false ) data.depthWrite = this.depthWrite;
  5743. if ( this.colorWrite === false ) data.colorWrite = this.colorWrite;
  5744. if ( this.stencilWriteMask !== 0xff ) data.stencilWriteMask = this.stencilWriteMask;
  5745. if ( this.stencilFunc !== AlwaysStencilFunc ) data.stencilFunc = this.stencilFunc;
  5746. if ( this.stencilRef !== 0 ) data.stencilRef = this.stencilRef;
  5747. if ( this.stencilFuncMask !== 0xff ) data.stencilFuncMask = this.stencilFuncMask;
  5748. if ( this.stencilFail !== KeepStencilOp ) data.stencilFail = this.stencilFail;
  5749. if ( this.stencilZFail !== KeepStencilOp ) data.stencilZFail = this.stencilZFail;
  5750. if ( this.stencilZPass !== KeepStencilOp ) data.stencilZPass = this.stencilZPass;
  5751. if ( this.stencilWrite === true ) data.stencilWrite = this.stencilWrite;
  5752. // rotation (SpriteMaterial)
  5753. if ( this.rotation !== undefined && this.rotation !== 0 ) data.rotation = this.rotation;
  5754. if ( this.polygonOffset === true ) data.polygonOffset = true;
  5755. if ( this.polygonOffsetFactor !== 0 ) data.polygonOffsetFactor = this.polygonOffsetFactor;
  5756. if ( this.polygonOffsetUnits !== 0 ) data.polygonOffsetUnits = this.polygonOffsetUnits;
  5757. if ( this.linewidth !== undefined && this.linewidth !== 1 ) data.linewidth = this.linewidth;
  5758. if ( this.dashSize !== undefined ) data.dashSize = this.dashSize;
  5759. if ( this.gapSize !== undefined ) data.gapSize = this.gapSize;
  5760. if ( this.scale !== undefined ) data.scale = this.scale;
  5761. if ( this.dithering === true ) data.dithering = true;
  5762. if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest;
  5763. if ( this.alphaHash === true ) data.alphaHash = true;
  5764. if ( this.alphaToCoverage === true ) data.alphaToCoverage = true;
  5765. if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = true;
  5766. if ( this.forceSinglePass === true ) data.forceSinglePass = true;
  5767. if ( this.wireframe === true ) data.wireframe = true;
  5768. if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth;
  5769. if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap;
  5770. if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin;
  5771. if ( this.flatShading === true ) data.flatShading = true;
  5772. if ( this.visible === false ) data.visible = false;
  5773. if ( this.toneMapped === false ) data.toneMapped = false;
  5774. if ( this.fog === false ) data.fog = false;
  5775. if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;
  5776. // TODO: Copied from Object3D.toJSON
  5777. function extractFromCache( cache ) {
  5778. const values = [];
  5779. for ( const key in cache ) {
  5780. const data = cache[ key ];
  5781. delete data.metadata;
  5782. values.push( data );
  5783. }
  5784. return values;
  5785. }
  5786. if ( isRootObject ) {
  5787. const textures = extractFromCache( meta.textures );
  5788. const images = extractFromCache( meta.images );
  5789. if ( textures.length > 0 ) data.textures = textures;
  5790. if ( images.length > 0 ) data.images = images;
  5791. }
  5792. return data;
  5793. }
  5794. clone() {
  5795. return new this.constructor().copy( this );
  5796. }
  5797. copy( source ) {
  5798. this.name = source.name;
  5799. this.blending = source.blending;
  5800. this.side = source.side;
  5801. this.vertexColors = source.vertexColors;
  5802. this.opacity = source.opacity;
  5803. this.transparent = source.transparent;
  5804. this.blendSrc = source.blendSrc;
  5805. this.blendDst = source.blendDst;
  5806. this.blendEquation = source.blendEquation;
  5807. this.blendSrcAlpha = source.blendSrcAlpha;
  5808. this.blendDstAlpha = source.blendDstAlpha;
  5809. this.blendEquationAlpha = source.blendEquationAlpha;
  5810. this.blendColor.copy( source.blendColor );
  5811. this.blendAlpha = source.blendAlpha;
  5812. this.depthFunc = source.depthFunc;
  5813. this.depthTest = source.depthTest;
  5814. this.depthWrite = source.depthWrite;
  5815. this.stencilWriteMask = source.stencilWriteMask;
  5816. this.stencilFunc = source.stencilFunc;
  5817. this.stencilRef = source.stencilRef;
  5818. this.stencilFuncMask = source.stencilFuncMask;
  5819. this.stencilFail = source.stencilFail;
  5820. this.stencilZFail = source.stencilZFail;
  5821. this.stencilZPass = source.stencilZPass;
  5822. this.stencilWrite = source.stencilWrite;
  5823. const srcPlanes = source.clippingPlanes;
  5824. let dstPlanes = null;
  5825. if ( srcPlanes !== null ) {
  5826. const n = srcPlanes.length;
  5827. dstPlanes = new Array( n );
  5828. for ( let i = 0; i !== n; ++ i ) {
  5829. dstPlanes[ i ] = srcPlanes[ i ].clone();
  5830. }
  5831. }
  5832. this.clippingPlanes = dstPlanes;
  5833. this.clipIntersection = source.clipIntersection;
  5834. this.clipShadows = source.clipShadows;
  5835. this.shadowSide = source.shadowSide;
  5836. this.colorWrite = source.colorWrite;
  5837. this.precision = source.precision;
  5838. this.polygonOffset = source.polygonOffset;
  5839. this.polygonOffsetFactor = source.polygonOffsetFactor;
  5840. this.polygonOffsetUnits = source.polygonOffsetUnits;
  5841. this.dithering = source.dithering;
  5842. this.alphaTest = source.alphaTest;
  5843. this.alphaHash = source.alphaHash;
  5844. this.alphaToCoverage = source.alphaToCoverage;
  5845. this.premultipliedAlpha = source.premultipliedAlpha;
  5846. this.forceSinglePass = source.forceSinglePass;
  5847. this.visible = source.visible;
  5848. this.toneMapped = source.toneMapped;
  5849. this.userData = JSON.parse( JSON.stringify( source.userData ) );
  5850. return this;
  5851. }
  5852. dispose() {
  5853. this.dispatchEvent( { type: 'dispose' } );
  5854. }
  5855. set needsUpdate( value ) {
  5856. if ( value === true ) this.version ++;
  5857. }
  5858. onBuild( /* shaderobject, renderer */ ) {
  5859. console.warn( 'Material: onBuild() has been removed.' ); // @deprecated, r166
  5860. }
  5861. }
  5862. class MeshBasicMaterial extends Material {
  5863. constructor( parameters ) {
  5864. super();
  5865. this.isMeshBasicMaterial = true;
  5866. this.type = 'MeshBasicMaterial';
  5867. this.color = new Color( 0xffffff ); // emissive
  5868. this.map = null;
  5869. this.lightMap = null;
  5870. this.lightMapIntensity = 1.0;
  5871. this.aoMap = null;
  5872. this.aoMapIntensity = 1.0;
  5873. this.specularMap = null;
  5874. this.alphaMap = null;
  5875. this.envMap = null;
  5876. this.envMapRotation = new Euler();
  5877. this.combine = MultiplyOperation;
  5878. this.reflectivity = 1;
  5879. this.refractionRatio = 0.98;
  5880. this.wireframe = false;
  5881. this.wireframeLinewidth = 1;
  5882. this.wireframeLinecap = 'round';
  5883. this.wireframeLinejoin = 'round';
  5884. this.fog = true;
  5885. this.setValues( parameters );
  5886. }
  5887. copy( source ) {
  5888. super.copy( source );
  5889. this.color.copy( source.color );
  5890. this.map = source.map;
  5891. this.lightMap = source.lightMap;
  5892. this.lightMapIntensity = source.lightMapIntensity;
  5893. this.aoMap = source.aoMap;
  5894. this.aoMapIntensity = source.aoMapIntensity;
  5895. this.specularMap = source.specularMap;
  5896. this.alphaMap = source.alphaMap;
  5897. this.envMap = source.envMap;
  5898. this.envMapRotation.copy( source.envMapRotation );
  5899. this.combine = source.combine;
  5900. this.reflectivity = source.reflectivity;
  5901. this.refractionRatio = source.refractionRatio;
  5902. this.wireframe = source.wireframe;
  5903. this.wireframeLinewidth = source.wireframeLinewidth;
  5904. this.wireframeLinecap = source.wireframeLinecap;
  5905. this.wireframeLinejoin = source.wireframeLinejoin;
  5906. this.fog = source.fog;
  5907. return this;
  5908. }
  5909. }
  5910. // Fast Half Float Conversions, http://www.fox-toolkit.org/ftp/fasthalffloatconversion.pdf
  5911. const _tables = /*@__PURE__*/ _generateTables();
  5912. function _generateTables() {
  5913. // float32 to float16 helpers
  5914. const buffer = new ArrayBuffer( 4 );
  5915. const floatView = new Float32Array( buffer );
  5916. const uint32View = new Uint32Array( buffer );
  5917. const baseTable = new Uint32Array( 512 );
  5918. const shiftTable = new Uint32Array( 512 );
  5919. for ( let i = 0; i < 256; ++ i ) {
  5920. const e = i - 127;
  5921. // very small number (0, -0)
  5922. if ( e < - 27 ) {
  5923. baseTable[ i ] = 0x0000;
  5924. baseTable[ i | 0x100 ] = 0x8000;
  5925. shiftTable[ i ] = 24;
  5926. shiftTable[ i | 0x100 ] = 24;
  5927. // small number (denorm)
  5928. } else if ( e < - 14 ) {
  5929. baseTable[ i ] = 0x0400 >> ( - e - 14 );
  5930. baseTable[ i | 0x100 ] = ( 0x0400 >> ( - e - 14 ) ) | 0x8000;
  5931. shiftTable[ i ] = - e - 1;
  5932. shiftTable[ i | 0x100 ] = - e - 1;
  5933. // normal number
  5934. } else if ( e <= 15 ) {
  5935. baseTable[ i ] = ( e + 15 ) << 10;
  5936. baseTable[ i | 0x100 ] = ( ( e + 15 ) << 10 ) | 0x8000;
  5937. shiftTable[ i ] = 13;
  5938. shiftTable[ i | 0x100 ] = 13;
  5939. // large number (Infinity, -Infinity)
  5940. } else if ( e < 128 ) {
  5941. baseTable[ i ] = 0x7c00;
  5942. baseTable[ i | 0x100 ] = 0xfc00;
  5943. shiftTable[ i ] = 24;
  5944. shiftTable[ i | 0x100 ] = 24;
  5945. // stay (NaN, Infinity, -Infinity)
  5946. } else {
  5947. baseTable[ i ] = 0x7c00;
  5948. baseTable[ i | 0x100 ] = 0xfc00;
  5949. shiftTable[ i ] = 13;
  5950. shiftTable[ i | 0x100 ] = 13;
  5951. }
  5952. }
  5953. // float16 to float32 helpers
  5954. const mantissaTable = new Uint32Array( 2048 );
  5955. const exponentTable = new Uint32Array( 64 );
  5956. const offsetTable = new Uint32Array( 64 );
  5957. for ( let i = 1; i < 1024; ++ i ) {
  5958. let m = i << 13; // zero pad mantissa bits
  5959. let e = 0; // zero exponent
  5960. // normalized
  5961. while ( ( m & 0x00800000 ) === 0 ) {
  5962. m <<= 1;
  5963. e -= 0x00800000; // decrement exponent
  5964. }
  5965. m &= ~ 0x00800000; // clear leading 1 bit
  5966. e += 0x38800000; // adjust bias
  5967. mantissaTable[ i ] = m | e;
  5968. }
  5969. for ( let i = 1024; i < 2048; ++ i ) {
  5970. mantissaTable[ i ] = 0x38000000 + ( ( i - 1024 ) << 13 );
  5971. }
  5972. for ( let i = 1; i < 31; ++ i ) {
  5973. exponentTable[ i ] = i << 23;
  5974. }
  5975. exponentTable[ 31 ] = 0x47800000;
  5976. exponentTable[ 32 ] = 0x80000000;
  5977. for ( let i = 33; i < 63; ++ i ) {
  5978. exponentTable[ i ] = 0x80000000 + ( ( i - 32 ) << 23 );
  5979. }
  5980. exponentTable[ 63 ] = 0xc7800000;
  5981. for ( let i = 1; i < 64; ++ i ) {
  5982. if ( i !== 32 ) {
  5983. offsetTable[ i ] = 1024;
  5984. }
  5985. }
  5986. return {
  5987. floatView: floatView,
  5988. uint32View: uint32View,
  5989. baseTable: baseTable,
  5990. shiftTable: shiftTable,
  5991. mantissaTable: mantissaTable,
  5992. exponentTable: exponentTable,
  5993. offsetTable: offsetTable
  5994. };
  5995. }
  5996. // float32 to float16
  5997. function toHalfFloat( val ) {
  5998. if ( Math.abs( val ) > 65504 ) console.warn( 'THREE.DataUtils.toHalfFloat(): Value out of range.' );
  5999. val = clamp( val, - 65504, 65504 );
  6000. _tables.floatView[ 0 ] = val;
  6001. const f = _tables.uint32View[ 0 ];
  6002. const e = ( f >> 23 ) & 0x1ff;
  6003. return _tables.baseTable[ e ] + ( ( f & 0x007fffff ) >> _tables.shiftTable[ e ] );
  6004. }
  6005. // float16 to float32
  6006. function fromHalfFloat( val ) {
  6007. const m = val >> 10;
  6008. _tables.uint32View[ 0 ] = _tables.mantissaTable[ _tables.offsetTable[ m ] + ( val & 0x3ff ) ] + _tables.exponentTable[ m ];
  6009. return _tables.floatView[ 0 ];
  6010. }
  6011. const DataUtils = {
  6012. toHalfFloat: toHalfFloat,
  6013. fromHalfFloat: fromHalfFloat,
  6014. };
  6015. const _vector$9 = /*@__PURE__*/ new Vector3();
  6016. const _vector2$1 = /*@__PURE__*/ new Vector2();
  6017. class BufferAttribute {
  6018. constructor( array, itemSize, normalized = false ) {
  6019. if ( Array.isArray( array ) ) {
  6020. throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' );
  6021. }
  6022. this.isBufferAttribute = true;
  6023. this.name = '';
  6024. this.array = array;
  6025. this.itemSize = itemSize;
  6026. this.count = array !== undefined ? array.length / itemSize : 0;
  6027. this.normalized = normalized;
  6028. this.usage = StaticDrawUsage;
  6029. this.updateRanges = [];
  6030. this.gpuType = FloatType;
  6031. this.version = 0;
  6032. }
  6033. onUploadCallback() {}
  6034. set needsUpdate( value ) {
  6035. if ( value === true ) this.version ++;
  6036. }
  6037. setUsage( value ) {
  6038. this.usage = value;
  6039. return this;
  6040. }
  6041. addUpdateRange( start, count ) {
  6042. this.updateRanges.push( { start, count } );
  6043. }
  6044. clearUpdateRanges() {
  6045. this.updateRanges.length = 0;
  6046. }
  6047. copy( source ) {
  6048. this.name = source.name;
  6049. this.array = new source.array.constructor( source.array );
  6050. this.itemSize = source.itemSize;
  6051. this.count = source.count;
  6052. this.normalized = source.normalized;
  6053. this.usage = source.usage;
  6054. this.gpuType = source.gpuType;
  6055. return this;
  6056. }
  6057. copyAt( index1, attribute, index2 ) {
  6058. index1 *= this.itemSize;
  6059. index2 *= attribute.itemSize;
  6060. for ( let i = 0, l = this.itemSize; i < l; i ++ ) {
  6061. this.array[ index1 + i ] = attribute.array[ index2 + i ];
  6062. }
  6063. return this;
  6064. }
  6065. copyArray( array ) {
  6066. this.array.set( array );
  6067. return this;
  6068. }
  6069. applyMatrix3( m ) {
  6070. if ( this.itemSize === 2 ) {
  6071. for ( let i = 0, l = this.count; i < l; i ++ ) {
  6072. _vector2$1.fromBufferAttribute( this, i );
  6073. _vector2$1.applyMatrix3( m );
  6074. this.setXY( i, _vector2$1.x, _vector2$1.y );
  6075. }
  6076. } else if ( this.itemSize === 3 ) {
  6077. for ( let i = 0, l = this.count; i < l; i ++ ) {
  6078. _vector$9.fromBufferAttribute( this, i );
  6079. _vector$9.applyMatrix3( m );
  6080. this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z );
  6081. }
  6082. }
  6083. return this;
  6084. }
  6085. applyMatrix4( m ) {
  6086. for ( let i = 0, l = this.count; i < l; i ++ ) {
  6087. _vector$9.fromBufferAttribute( this, i );
  6088. _vector$9.applyMatrix4( m );
  6089. this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z );
  6090. }
  6091. return this;
  6092. }
  6093. applyNormalMatrix( m ) {
  6094. for ( let i = 0, l = this.count; i < l; i ++ ) {
  6095. _vector$9.fromBufferAttribute( this, i );
  6096. _vector$9.applyNormalMatrix( m );
  6097. this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z );
  6098. }
  6099. return this;
  6100. }
  6101. transformDirection( m ) {
  6102. for ( let i = 0, l = this.count; i < l; i ++ ) {
  6103. _vector$9.fromBufferAttribute( this, i );
  6104. _vector$9.transformDirection( m );
  6105. this.setXYZ( i, _vector$9.x, _vector$9.y, _vector$9.z );
  6106. }
  6107. return this;
  6108. }
  6109. set( value, offset = 0 ) {
  6110. // Matching BufferAttribute constructor, do not normalize the array.
  6111. this.array.set( value, offset );
  6112. return this;
  6113. }
  6114. getComponent( index, component ) {
  6115. let value = this.array[ index * this.itemSize + component ];
  6116. if ( this.normalized ) value = denormalize( value, this.array );
  6117. return value;
  6118. }
  6119. setComponent( index, component, value ) {
  6120. if ( this.normalized ) value = normalize( value, this.array );
  6121. this.array[ index * this.itemSize + component ] = value;
  6122. return this;
  6123. }
  6124. getX( index ) {
  6125. let x = this.array[ index * this.itemSize ];
  6126. if ( this.normalized ) x = denormalize( x, this.array );
  6127. return x;
  6128. }
  6129. setX( index, x ) {
  6130. if ( this.normalized ) x = normalize( x, this.array );
  6131. this.array[ index * this.itemSize ] = x;
  6132. return this;
  6133. }
  6134. getY( index ) {
  6135. let y = this.array[ index * this.itemSize + 1 ];
  6136. if ( this.normalized ) y = denormalize( y, this.array );
  6137. return y;
  6138. }
  6139. setY( index, y ) {
  6140. if ( this.normalized ) y = normalize( y, this.array );
  6141. this.array[ index * this.itemSize + 1 ] = y;
  6142. return this;
  6143. }
  6144. getZ( index ) {
  6145. let z = this.array[ index * this.itemSize + 2 ];
  6146. if ( this.normalized ) z = denormalize( z, this.array );
  6147. return z;
  6148. }
  6149. setZ( index, z ) {
  6150. if ( this.normalized ) z = normalize( z, this.array );
  6151. this.array[ index * this.itemSize + 2 ] = z;
  6152. return this;
  6153. }
  6154. getW( index ) {
  6155. let w = this.array[ index * this.itemSize + 3 ];
  6156. if ( this.normalized ) w = denormalize( w, this.array );
  6157. return w;
  6158. }
  6159. setW( index, w ) {
  6160. if ( this.normalized ) w = normalize( w, this.array );
  6161. this.array[ index * this.itemSize + 3 ] = w;
  6162. return this;
  6163. }
  6164. setXY( index, x, y ) {
  6165. index *= this.itemSize;
  6166. if ( this.normalized ) {
  6167. x = normalize( x, this.array );
  6168. y = normalize( y, this.array );
  6169. }
  6170. this.array[ index + 0 ] = x;
  6171. this.array[ index + 1 ] = y;
  6172. return this;
  6173. }
  6174. setXYZ( index, x, y, z ) {
  6175. index *= this.itemSize;
  6176. if ( this.normalized ) {
  6177. x = normalize( x, this.array );
  6178. y = normalize( y, this.array );
  6179. z = normalize( z, this.array );
  6180. }
  6181. this.array[ index + 0 ] = x;
  6182. this.array[ index + 1 ] = y;
  6183. this.array[ index + 2 ] = z;
  6184. return this;
  6185. }
  6186. setXYZW( index, x, y, z, w ) {
  6187. index *= this.itemSize;
  6188. if ( this.normalized ) {
  6189. x = normalize( x, this.array );
  6190. y = normalize( y, this.array );
  6191. z = normalize( z, this.array );
  6192. w = normalize( w, this.array );
  6193. }
  6194. this.array[ index + 0 ] = x;
  6195. this.array[ index + 1 ] = y;
  6196. this.array[ index + 2 ] = z;
  6197. this.array[ index + 3 ] = w;
  6198. return this;
  6199. }
  6200. onUpload( callback ) {
  6201. this.onUploadCallback = callback;
  6202. return this;
  6203. }
  6204. clone() {
  6205. return new this.constructor( this.array, this.itemSize ).copy( this );
  6206. }
  6207. toJSON() {
  6208. const data = {
  6209. itemSize: this.itemSize,
  6210. type: this.array.constructor.name,
  6211. array: Array.from( this.array ),
  6212. normalized: this.normalized
  6213. };
  6214. if ( this.name !== '' ) data.name = this.name;
  6215. if ( this.usage !== StaticDrawUsage ) data.usage = this.usage;
  6216. return data;
  6217. }
  6218. }
  6219. //
  6220. class Int8BufferAttribute extends BufferAttribute {
  6221. constructor( array, itemSize, normalized ) {
  6222. super( new Int8Array( array ), itemSize, normalized );
  6223. }
  6224. }
  6225. class Uint8BufferAttribute extends BufferAttribute {
  6226. constructor( array, itemSize, normalized ) {
  6227. super( new Uint8Array( array ), itemSize, normalized );
  6228. }
  6229. }
  6230. class Uint8ClampedBufferAttribute extends BufferAttribute {
  6231. constructor( array, itemSize, normalized ) {
  6232. super( new Uint8ClampedArray( array ), itemSize, normalized );
  6233. }
  6234. }
  6235. class Int16BufferAttribute extends BufferAttribute {
  6236. constructor( array, itemSize, normalized ) {
  6237. super( new Int16Array( array ), itemSize, normalized );
  6238. }
  6239. }
  6240. class Uint16BufferAttribute extends BufferAttribute {
  6241. constructor( array, itemSize, normalized ) {
  6242. super( new Uint16Array( array ), itemSize, normalized );
  6243. }
  6244. }
  6245. class Int32BufferAttribute extends BufferAttribute {
  6246. constructor( array, itemSize, normalized ) {
  6247. super( new Int32Array( array ), itemSize, normalized );
  6248. }
  6249. }
  6250. class Uint32BufferAttribute extends BufferAttribute {
  6251. constructor( array, itemSize, normalized ) {
  6252. super( new Uint32Array( array ), itemSize, normalized );
  6253. }
  6254. }
  6255. class Float16BufferAttribute extends BufferAttribute {
  6256. constructor( array, itemSize, normalized ) {
  6257. super( new Uint16Array( array ), itemSize, normalized );
  6258. this.isFloat16BufferAttribute = true;
  6259. }
  6260. getX( index ) {
  6261. let x = fromHalfFloat( this.array[ index * this.itemSize ] );
  6262. if ( this.normalized ) x = denormalize( x, this.array );
  6263. return x;
  6264. }
  6265. setX( index, x ) {
  6266. if ( this.normalized ) x = normalize( x, this.array );
  6267. this.array[ index * this.itemSize ] = toHalfFloat( x );
  6268. return this;
  6269. }
  6270. getY( index ) {
  6271. let y = fromHalfFloat( this.array[ index * this.itemSize + 1 ] );
  6272. if ( this.normalized ) y = denormalize( y, this.array );
  6273. return y;
  6274. }
  6275. setY( index, y ) {
  6276. if ( this.normalized ) y = normalize( y, this.array );
  6277. this.array[ index * this.itemSize + 1 ] = toHalfFloat( y );
  6278. return this;
  6279. }
  6280. getZ( index ) {
  6281. let z = fromHalfFloat( this.array[ index * this.itemSize + 2 ] );
  6282. if ( this.normalized ) z = denormalize( z, this.array );
  6283. return z;
  6284. }
  6285. setZ( index, z ) {
  6286. if ( this.normalized ) z = normalize( z, this.array );
  6287. this.array[ index * this.itemSize + 2 ] = toHalfFloat( z );
  6288. return this;
  6289. }
  6290. getW( index ) {
  6291. let w = fromHalfFloat( this.array[ index * this.itemSize + 3 ] );
  6292. if ( this.normalized ) w = denormalize( w, this.array );
  6293. return w;
  6294. }
  6295. setW( index, w ) {
  6296. if ( this.normalized ) w = normalize( w, this.array );
  6297. this.array[ index * this.itemSize + 3 ] = toHalfFloat( w );
  6298. return this;
  6299. }
  6300. setXY( index, x, y ) {
  6301. index *= this.itemSize;
  6302. if ( this.normalized ) {
  6303. x = normalize( x, this.array );
  6304. y = normalize( y, this.array );
  6305. }
  6306. this.array[ index + 0 ] = toHalfFloat( x );
  6307. this.array[ index + 1 ] = toHalfFloat( y );
  6308. return this;
  6309. }
  6310. setXYZ( index, x, y, z ) {
  6311. index *= this.itemSize;
  6312. if ( this.normalized ) {
  6313. x = normalize( x, this.array );
  6314. y = normalize( y, this.array );
  6315. z = normalize( z, this.array );
  6316. }
  6317. this.array[ index + 0 ] = toHalfFloat( x );
  6318. this.array[ index + 1 ] = toHalfFloat( y );
  6319. this.array[ index + 2 ] = toHalfFloat( z );
  6320. return this;
  6321. }
  6322. setXYZW( index, x, y, z, w ) {
  6323. index *= this.itemSize;
  6324. if ( this.normalized ) {
  6325. x = normalize( x, this.array );
  6326. y = normalize( y, this.array );
  6327. z = normalize( z, this.array );
  6328. w = normalize( w, this.array );
  6329. }
  6330. this.array[ index + 0 ] = toHalfFloat( x );
  6331. this.array[ index + 1 ] = toHalfFloat( y );
  6332. this.array[ index + 2 ] = toHalfFloat( z );
  6333. this.array[ index + 3 ] = toHalfFloat( w );
  6334. return this;
  6335. }
  6336. }
  6337. class Float32BufferAttribute extends BufferAttribute {
  6338. constructor( array, itemSize, normalized ) {
  6339. super( new Float32Array( array ), itemSize, normalized );
  6340. }
  6341. }
  6342. let _id$1 = 0;
  6343. const _m1 = /*@__PURE__*/ new Matrix4();
  6344. const _obj = /*@__PURE__*/ new Object3D();
  6345. const _offset = /*@__PURE__*/ new Vector3();
  6346. const _box$2 = /*@__PURE__*/ new Box3();
  6347. const _boxMorphTargets = /*@__PURE__*/ new Box3();
  6348. const _vector$8 = /*@__PURE__*/ new Vector3();
  6349. class BufferGeometry extends EventDispatcher {
  6350. constructor() {
  6351. super();
  6352. this.isBufferGeometry = true;
  6353. Object.defineProperty( this, 'id', { value: _id$1 ++ } );
  6354. this.uuid = generateUUID();
  6355. this.name = '';
  6356. this.type = 'BufferGeometry';
  6357. this.index = null;
  6358. this.indirect = null;
  6359. this.attributes = {};
  6360. this.morphAttributes = {};
  6361. this.morphTargetsRelative = false;
  6362. this.groups = [];
  6363. this.boundingBox = null;
  6364. this.boundingSphere = null;
  6365. this.drawRange = { start: 0, count: Infinity };
  6366. this.userData = {};
  6367. }
  6368. getIndex() {
  6369. return this.index;
  6370. }
  6371. setIndex( index ) {
  6372. if ( Array.isArray( index ) ) {
  6373. this.index = new ( arrayNeedsUint32( index ) ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 );
  6374. } else {
  6375. this.index = index;
  6376. }
  6377. return this;
  6378. }
  6379. setIndirect( indirect ) {
  6380. this.indirect = indirect;
  6381. return this;
  6382. }
  6383. getIndirect() {
  6384. return this.indirect;
  6385. }
  6386. getAttribute( name ) {
  6387. return this.attributes[ name ];
  6388. }
  6389. setAttribute( name, attribute ) {
  6390. this.attributes[ name ] = attribute;
  6391. return this;
  6392. }
  6393. deleteAttribute( name ) {
  6394. delete this.attributes[ name ];
  6395. return this;
  6396. }
  6397. hasAttribute( name ) {
  6398. return this.attributes[ name ] !== undefined;
  6399. }
  6400. addGroup( start, count, materialIndex = 0 ) {
  6401. this.groups.push( {
  6402. start: start,
  6403. count: count,
  6404. materialIndex: materialIndex
  6405. } );
  6406. }
  6407. clearGroups() {
  6408. this.groups = [];
  6409. }
  6410. setDrawRange( start, count ) {
  6411. this.drawRange.start = start;
  6412. this.drawRange.count = count;
  6413. }
  6414. applyMatrix4( matrix ) {
  6415. const position = this.attributes.position;
  6416. if ( position !== undefined ) {
  6417. position.applyMatrix4( matrix );
  6418. position.needsUpdate = true;
  6419. }
  6420. const normal = this.attributes.normal;
  6421. if ( normal !== undefined ) {
  6422. const normalMatrix = new Matrix3().getNormalMatrix( matrix );
  6423. normal.applyNormalMatrix( normalMatrix );
  6424. normal.needsUpdate = true;
  6425. }
  6426. const tangent = this.attributes.tangent;
  6427. if ( tangent !== undefined ) {
  6428. tangent.transformDirection( matrix );
  6429. tangent.needsUpdate = true;
  6430. }
  6431. if ( this.boundingBox !== null ) {
  6432. this.computeBoundingBox();
  6433. }
  6434. if ( this.boundingSphere !== null ) {
  6435. this.computeBoundingSphere();
  6436. }
  6437. return this;
  6438. }
  6439. applyQuaternion( q ) {
  6440. _m1.makeRotationFromQuaternion( q );
  6441. this.applyMatrix4( _m1 );
  6442. return this;
  6443. }
  6444. rotateX( angle ) {
  6445. // rotate geometry around world x-axis
  6446. _m1.makeRotationX( angle );
  6447. this.applyMatrix4( _m1 );
  6448. return this;
  6449. }
  6450. rotateY( angle ) {
  6451. // rotate geometry around world y-axis
  6452. _m1.makeRotationY( angle );
  6453. this.applyMatrix4( _m1 );
  6454. return this;
  6455. }
  6456. rotateZ( angle ) {
  6457. // rotate geometry around world z-axis
  6458. _m1.makeRotationZ( angle );
  6459. this.applyMatrix4( _m1 );
  6460. return this;
  6461. }
  6462. translate( x, y, z ) {
  6463. // translate geometry
  6464. _m1.makeTranslation( x, y, z );
  6465. this.applyMatrix4( _m1 );
  6466. return this;
  6467. }
  6468. scale( x, y, z ) {
  6469. // scale geometry
  6470. _m1.makeScale( x, y, z );
  6471. this.applyMatrix4( _m1 );
  6472. return this;
  6473. }
  6474. lookAt( vector ) {
  6475. _obj.lookAt( vector );
  6476. _obj.updateMatrix();
  6477. this.applyMatrix4( _obj.matrix );
  6478. return this;
  6479. }
  6480. center() {
  6481. this.computeBoundingBox();
  6482. this.boundingBox.getCenter( _offset ).negate();
  6483. this.translate( _offset.x, _offset.y, _offset.z );
  6484. return this;
  6485. }
  6486. setFromPoints( points ) {
  6487. const positionAttribute = this.getAttribute( 'position' );
  6488. if ( positionAttribute === undefined ) {
  6489. const position = [];
  6490. for ( let i = 0, l = points.length; i < l; i ++ ) {
  6491. const point = points[ i ];
  6492. position.push( point.x, point.y, point.z || 0 );
  6493. }
  6494. this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) );
  6495. } else {
  6496. for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) {
  6497. const point = points[ i ];
  6498. positionAttribute.setXYZ( i, point.x, point.y, point.z || 0 );
  6499. }
  6500. if ( points.length > positionAttribute.count ) {
  6501. console.warn( 'THREE.BufferGeometry: Buffer size too small for points data. Use .dispose() and create a new geometry.' );
  6502. }
  6503. positionAttribute.needsUpdate = true;
  6504. }
  6505. return this;
  6506. }
  6507. computeBoundingBox() {
  6508. if ( this.boundingBox === null ) {
  6509. this.boundingBox = new Box3();
  6510. }
  6511. const position = this.attributes.position;
  6512. const morphAttributesPosition = this.morphAttributes.position;
  6513. if ( position && position.isGLBufferAttribute ) {
  6514. console.error( 'THREE.BufferGeometry.computeBoundingBox(): GLBufferAttribute requires a manual bounding box.', this );
  6515. this.boundingBox.set(
  6516. new Vector3( - Infinity, - Infinity, - Infinity ),
  6517. new Vector3( + Infinity, + Infinity, + Infinity )
  6518. );
  6519. return;
  6520. }
  6521. if ( position !== undefined ) {
  6522. this.boundingBox.setFromBufferAttribute( position );
  6523. // process morph attributes if present
  6524. if ( morphAttributesPosition ) {
  6525. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  6526. const morphAttribute = morphAttributesPosition[ i ];
  6527. _box$2.setFromBufferAttribute( morphAttribute );
  6528. if ( this.morphTargetsRelative ) {
  6529. _vector$8.addVectors( this.boundingBox.min, _box$2.min );
  6530. this.boundingBox.expandByPoint( _vector$8 );
  6531. _vector$8.addVectors( this.boundingBox.max, _box$2.max );
  6532. this.boundingBox.expandByPoint( _vector$8 );
  6533. } else {
  6534. this.boundingBox.expandByPoint( _box$2.min );
  6535. this.boundingBox.expandByPoint( _box$2.max );
  6536. }
  6537. }
  6538. }
  6539. } else {
  6540. this.boundingBox.makeEmpty();
  6541. }
  6542. if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
  6543. console.error( 'THREE.BufferGeometry.computeBoundingBox(): Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this );
  6544. }
  6545. }
  6546. computeBoundingSphere() {
  6547. if ( this.boundingSphere === null ) {
  6548. this.boundingSphere = new Sphere();
  6549. }
  6550. const position = this.attributes.position;
  6551. const morphAttributesPosition = this.morphAttributes.position;
  6552. if ( position && position.isGLBufferAttribute ) {
  6553. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): GLBufferAttribute requires a manual bounding sphere.', this );
  6554. this.boundingSphere.set( new Vector3(), Infinity );
  6555. return;
  6556. }
  6557. if ( position ) {
  6558. // first, find the center of the bounding sphere
  6559. const center = this.boundingSphere.center;
  6560. _box$2.setFromBufferAttribute( position );
  6561. // process morph attributes if present
  6562. if ( morphAttributesPosition ) {
  6563. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  6564. const morphAttribute = morphAttributesPosition[ i ];
  6565. _boxMorphTargets.setFromBufferAttribute( morphAttribute );
  6566. if ( this.morphTargetsRelative ) {
  6567. _vector$8.addVectors( _box$2.min, _boxMorphTargets.min );
  6568. _box$2.expandByPoint( _vector$8 );
  6569. _vector$8.addVectors( _box$2.max, _boxMorphTargets.max );
  6570. _box$2.expandByPoint( _vector$8 );
  6571. } else {
  6572. _box$2.expandByPoint( _boxMorphTargets.min );
  6573. _box$2.expandByPoint( _boxMorphTargets.max );
  6574. }
  6575. }
  6576. }
  6577. _box$2.getCenter( center );
  6578. // second, try to find a boundingSphere with a radius smaller than the
  6579. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  6580. let maxRadiusSq = 0;
  6581. for ( let i = 0, il = position.count; i < il; i ++ ) {
  6582. _vector$8.fromBufferAttribute( position, i );
  6583. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$8 ) );
  6584. }
  6585. // process morph attributes if present
  6586. if ( morphAttributesPosition ) {
  6587. for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) {
  6588. const morphAttribute = morphAttributesPosition[ i ];
  6589. const morphTargetsRelative = this.morphTargetsRelative;
  6590. for ( let j = 0, jl = morphAttribute.count; j < jl; j ++ ) {
  6591. _vector$8.fromBufferAttribute( morphAttribute, j );
  6592. if ( morphTargetsRelative ) {
  6593. _offset.fromBufferAttribute( position, j );
  6594. _vector$8.add( _offset );
  6595. }
  6596. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$8 ) );
  6597. }
  6598. }
  6599. }
  6600. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  6601. if ( isNaN( this.boundingSphere.radius ) ) {
  6602. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this );
  6603. }
  6604. }
  6605. }
  6606. computeTangents() {
  6607. const index = this.index;
  6608. const attributes = this.attributes;
  6609. // based on http://www.terathon.com/code/tangent.html
  6610. // (per vertex tangents)
  6611. if ( index === null ||
  6612. attributes.position === undefined ||
  6613. attributes.normal === undefined ||
  6614. attributes.uv === undefined ) {
  6615. console.error( 'THREE.BufferGeometry: .computeTangents() failed. Missing required attributes (index, position, normal or uv)' );
  6616. return;
  6617. }
  6618. const positionAttribute = attributes.position;
  6619. const normalAttribute = attributes.normal;
  6620. const uvAttribute = attributes.uv;
  6621. if ( this.hasAttribute( 'tangent' ) === false ) {
  6622. this.setAttribute( 'tangent', new BufferAttribute( new Float32Array( 4 * positionAttribute.count ), 4 ) );
  6623. }
  6624. const tangentAttribute = this.getAttribute( 'tangent' );
  6625. const tan1 = [], tan2 = [];
  6626. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  6627. tan1[ i ] = new Vector3();
  6628. tan2[ i ] = new Vector3();
  6629. }
  6630. const vA = new Vector3(),
  6631. vB = new Vector3(),
  6632. vC = new Vector3(),
  6633. uvA = new Vector2(),
  6634. uvB = new Vector2(),
  6635. uvC = new Vector2(),
  6636. sdir = new Vector3(),
  6637. tdir = new Vector3();
  6638. function handleTriangle( a, b, c ) {
  6639. vA.fromBufferAttribute( positionAttribute, a );
  6640. vB.fromBufferAttribute( positionAttribute, b );
  6641. vC.fromBufferAttribute( positionAttribute, c );
  6642. uvA.fromBufferAttribute( uvAttribute, a );
  6643. uvB.fromBufferAttribute( uvAttribute, b );
  6644. uvC.fromBufferAttribute( uvAttribute, c );
  6645. vB.sub( vA );
  6646. vC.sub( vA );
  6647. uvB.sub( uvA );
  6648. uvC.sub( uvA );
  6649. const r = 1.0 / ( uvB.x * uvC.y - uvC.x * uvB.y );
  6650. // silently ignore degenerate uv triangles having coincident or colinear vertices
  6651. if ( ! isFinite( r ) ) return;
  6652. sdir.copy( vB ).multiplyScalar( uvC.y ).addScaledVector( vC, - uvB.y ).multiplyScalar( r );
  6653. tdir.copy( vC ).multiplyScalar( uvB.x ).addScaledVector( vB, - uvC.x ).multiplyScalar( r );
  6654. tan1[ a ].add( sdir );
  6655. tan1[ b ].add( sdir );
  6656. tan1[ c ].add( sdir );
  6657. tan2[ a ].add( tdir );
  6658. tan2[ b ].add( tdir );
  6659. tan2[ c ].add( tdir );
  6660. }
  6661. let groups = this.groups;
  6662. if ( groups.length === 0 ) {
  6663. groups = [ {
  6664. start: 0,
  6665. count: index.count
  6666. } ];
  6667. }
  6668. for ( let i = 0, il = groups.length; i < il; ++ i ) {
  6669. const group = groups[ i ];
  6670. const start = group.start;
  6671. const count = group.count;
  6672. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  6673. handleTriangle(
  6674. index.getX( j + 0 ),
  6675. index.getX( j + 1 ),
  6676. index.getX( j + 2 )
  6677. );
  6678. }
  6679. }
  6680. const tmp = new Vector3(), tmp2 = new Vector3();
  6681. const n = new Vector3(), n2 = new Vector3();
  6682. function handleVertex( v ) {
  6683. n.fromBufferAttribute( normalAttribute, v );
  6684. n2.copy( n );
  6685. const t = tan1[ v ];
  6686. // Gram-Schmidt orthogonalize
  6687. tmp.copy( t );
  6688. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  6689. // Calculate handedness
  6690. tmp2.crossVectors( n2, t );
  6691. const test = tmp2.dot( tan2[ v ] );
  6692. const w = ( test < 0.0 ) ? - 1.0 : 1.0;
  6693. tangentAttribute.setXYZW( v, tmp.x, tmp.y, tmp.z, w );
  6694. }
  6695. for ( let i = 0, il = groups.length; i < il; ++ i ) {
  6696. const group = groups[ i ];
  6697. const start = group.start;
  6698. const count = group.count;
  6699. for ( let j = start, jl = start + count; j < jl; j += 3 ) {
  6700. handleVertex( index.getX( j + 0 ) );
  6701. handleVertex( index.getX( j + 1 ) );
  6702. handleVertex( index.getX( j + 2 ) );
  6703. }
  6704. }
  6705. }
  6706. computeVertexNormals() {
  6707. const index = this.index;
  6708. const positionAttribute = this.getAttribute( 'position' );
  6709. if ( positionAttribute !== undefined ) {
  6710. let normalAttribute = this.getAttribute( 'normal' );
  6711. if ( normalAttribute === undefined ) {
  6712. normalAttribute = new BufferAttribute( new Float32Array( positionAttribute.count * 3 ), 3 );
  6713. this.setAttribute( 'normal', normalAttribute );
  6714. } else {
  6715. // reset existing normals to zero
  6716. for ( let i = 0, il = normalAttribute.count; i < il; i ++ ) {
  6717. normalAttribute.setXYZ( i, 0, 0, 0 );
  6718. }
  6719. }
  6720. const pA = new Vector3(), pB = new Vector3(), pC = new Vector3();
  6721. const nA = new Vector3(), nB = new Vector3(), nC = new Vector3();
  6722. const cb = new Vector3(), ab = new Vector3();
  6723. // indexed elements
  6724. if ( index ) {
  6725. for ( let i = 0, il = index.count; i < il; i += 3 ) {
  6726. const vA = index.getX( i + 0 );
  6727. const vB = index.getX( i + 1 );
  6728. const vC = index.getX( i + 2 );
  6729. pA.fromBufferAttribute( positionAttribute, vA );
  6730. pB.fromBufferAttribute( positionAttribute, vB );
  6731. pC.fromBufferAttribute( positionAttribute, vC );
  6732. cb.subVectors( pC, pB );
  6733. ab.subVectors( pA, pB );
  6734. cb.cross( ab );
  6735. nA.fromBufferAttribute( normalAttribute, vA );
  6736. nB.fromBufferAttribute( normalAttribute, vB );
  6737. nC.fromBufferAttribute( normalAttribute, vC );
  6738. nA.add( cb );
  6739. nB.add( cb );
  6740. nC.add( cb );
  6741. normalAttribute.setXYZ( vA, nA.x, nA.y, nA.z );
  6742. normalAttribute.setXYZ( vB, nB.x, nB.y, nB.z );
  6743. normalAttribute.setXYZ( vC, nC.x, nC.y, nC.z );
  6744. }
  6745. } else {
  6746. // non-indexed elements (unconnected triangle soup)
  6747. for ( let i = 0, il = positionAttribute.count; i < il; i += 3 ) {
  6748. pA.fromBufferAttribute( positionAttribute, i + 0 );
  6749. pB.fromBufferAttribute( positionAttribute, i + 1 );
  6750. pC.fromBufferAttribute( positionAttribute, i + 2 );
  6751. cb.subVectors( pC, pB );
  6752. ab.subVectors( pA, pB );
  6753. cb.cross( ab );
  6754. normalAttribute.setXYZ( i + 0, cb.x, cb.y, cb.z );
  6755. normalAttribute.setXYZ( i + 1, cb.x, cb.y, cb.z );
  6756. normalAttribute.setXYZ( i + 2, cb.x, cb.y, cb.z );
  6757. }
  6758. }
  6759. this.normalizeNormals();
  6760. normalAttribute.needsUpdate = true;
  6761. }
  6762. }
  6763. normalizeNormals() {
  6764. const normals = this.attributes.normal;
  6765. for ( let i = 0, il = normals.count; i < il; i ++ ) {
  6766. _vector$8.fromBufferAttribute( normals, i );
  6767. _vector$8.normalize();
  6768. normals.setXYZ( i, _vector$8.x, _vector$8.y, _vector$8.z );
  6769. }
  6770. }
  6771. toNonIndexed() {
  6772. function convertBufferAttribute( attribute, indices ) {
  6773. const array = attribute.array;
  6774. const itemSize = attribute.itemSize;
  6775. const normalized = attribute.normalized;
  6776. const array2 = new array.constructor( indices.length * itemSize );
  6777. let index = 0, index2 = 0;
  6778. for ( let i = 0, l = indices.length; i < l; i ++ ) {
  6779. if ( attribute.isInterleavedBufferAttribute ) {
  6780. index = indices[ i ] * attribute.data.stride + attribute.offset;
  6781. } else {
  6782. index = indices[ i ] * itemSize;
  6783. }
  6784. for ( let j = 0; j < itemSize; j ++ ) {
  6785. array2[ index2 ++ ] = array[ index ++ ];
  6786. }
  6787. }
  6788. return new BufferAttribute( array2, itemSize, normalized );
  6789. }
  6790. //
  6791. if ( this.index === null ) {
  6792. console.warn( 'THREE.BufferGeometry.toNonIndexed(): BufferGeometry is already non-indexed.' );
  6793. return this;
  6794. }
  6795. const geometry2 = new BufferGeometry();
  6796. const indices = this.index.array;
  6797. const attributes = this.attributes;
  6798. // attributes
  6799. for ( const name in attributes ) {
  6800. const attribute = attributes[ name ];
  6801. const newAttribute = convertBufferAttribute( attribute, indices );
  6802. geometry2.setAttribute( name, newAttribute );
  6803. }
  6804. // morph attributes
  6805. const morphAttributes = this.morphAttributes;
  6806. for ( const name in morphAttributes ) {
  6807. const morphArray = [];
  6808. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  6809. for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) {
  6810. const attribute = morphAttribute[ i ];
  6811. const newAttribute = convertBufferAttribute( attribute, indices );
  6812. morphArray.push( newAttribute );
  6813. }
  6814. geometry2.morphAttributes[ name ] = morphArray;
  6815. }
  6816. geometry2.morphTargetsRelative = this.morphTargetsRelative;
  6817. // groups
  6818. const groups = this.groups;
  6819. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  6820. const group = groups[ i ];
  6821. geometry2.addGroup( group.start, group.count, group.materialIndex );
  6822. }
  6823. return geometry2;
  6824. }
  6825. toJSON() {
  6826. const data = {
  6827. metadata: {
  6828. version: 4.6,
  6829. type: 'BufferGeometry',
  6830. generator: 'BufferGeometry.toJSON'
  6831. }
  6832. };
  6833. // standard BufferGeometry serialization
  6834. data.uuid = this.uuid;
  6835. data.type = this.type;
  6836. if ( this.name !== '' ) data.name = this.name;
  6837. if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData;
  6838. if ( this.parameters !== undefined ) {
  6839. const parameters = this.parameters;
  6840. for ( const key in parameters ) {
  6841. if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ];
  6842. }
  6843. return data;
  6844. }
  6845. // for simplicity the code assumes attributes are not shared across geometries, see #15811
  6846. data.data = { attributes: {} };
  6847. const index = this.index;
  6848. if ( index !== null ) {
  6849. data.data.index = {
  6850. type: index.array.constructor.name,
  6851. array: Array.prototype.slice.call( index.array )
  6852. };
  6853. }
  6854. const attributes = this.attributes;
  6855. for ( const key in attributes ) {
  6856. const attribute = attributes[ key ];
  6857. data.data.attributes[ key ] = attribute.toJSON( data.data );
  6858. }
  6859. const morphAttributes = {};
  6860. let hasMorphAttributes = false;
  6861. for ( const key in this.morphAttributes ) {
  6862. const attributeArray = this.morphAttributes[ key ];
  6863. const array = [];
  6864. for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
  6865. const attribute = attributeArray[ i ];
  6866. array.push( attribute.toJSON( data.data ) );
  6867. }
  6868. if ( array.length > 0 ) {
  6869. morphAttributes[ key ] = array;
  6870. hasMorphAttributes = true;
  6871. }
  6872. }
  6873. if ( hasMorphAttributes ) {
  6874. data.data.morphAttributes = morphAttributes;
  6875. data.data.morphTargetsRelative = this.morphTargetsRelative;
  6876. }
  6877. const groups = this.groups;
  6878. if ( groups.length > 0 ) {
  6879. data.data.groups = JSON.parse( JSON.stringify( groups ) );
  6880. }
  6881. const boundingSphere = this.boundingSphere;
  6882. if ( boundingSphere !== null ) {
  6883. data.data.boundingSphere = {
  6884. center: boundingSphere.center.toArray(),
  6885. radius: boundingSphere.radius
  6886. };
  6887. }
  6888. return data;
  6889. }
  6890. clone() {
  6891. return new this.constructor().copy( this );
  6892. }
  6893. copy( source ) {
  6894. // reset
  6895. this.index = null;
  6896. this.attributes = {};
  6897. this.morphAttributes = {};
  6898. this.groups = [];
  6899. this.boundingBox = null;
  6900. this.boundingSphere = null;
  6901. // used for storing cloned, shared data
  6902. const data = {};
  6903. // name
  6904. this.name = source.name;
  6905. // index
  6906. const index = source.index;
  6907. if ( index !== null ) {
  6908. this.setIndex( index.clone( data ) );
  6909. }
  6910. // attributes
  6911. const attributes = source.attributes;
  6912. for ( const name in attributes ) {
  6913. const attribute = attributes[ name ];
  6914. this.setAttribute( name, attribute.clone( data ) );
  6915. }
  6916. // morph attributes
  6917. const morphAttributes = source.morphAttributes;
  6918. for ( const name in morphAttributes ) {
  6919. const array = [];
  6920. const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes
  6921. for ( let i = 0, l = morphAttribute.length; i < l; i ++ ) {
  6922. array.push( morphAttribute[ i ].clone( data ) );
  6923. }
  6924. this.morphAttributes[ name ] = array;
  6925. }
  6926. this.morphTargetsRelative = source.morphTargetsRelative;
  6927. // groups
  6928. const groups = source.groups;
  6929. for ( let i = 0, l = groups.length; i < l; i ++ ) {
  6930. const group = groups[ i ];
  6931. this.addGroup( group.start, group.count, group.materialIndex );
  6932. }
  6933. // bounding box
  6934. const boundingBox = source.boundingBox;
  6935. if ( boundingBox !== null ) {
  6936. this.boundingBox = boundingBox.clone();
  6937. }
  6938. // bounding sphere
  6939. const boundingSphere = source.boundingSphere;
  6940. if ( boundingSphere !== null ) {
  6941. this.boundingSphere = boundingSphere.clone();
  6942. }
  6943. // draw range
  6944. this.drawRange.start = source.drawRange.start;
  6945. this.drawRange.count = source.drawRange.count;
  6946. // user data
  6947. this.userData = source.userData;
  6948. return this;
  6949. }
  6950. dispose() {
  6951. this.dispatchEvent( { type: 'dispose' } );
  6952. }
  6953. }
  6954. const _inverseMatrix$3 = /*@__PURE__*/ new Matrix4();
  6955. const _ray$3 = /*@__PURE__*/ new Ray();
  6956. const _sphere$6 = /*@__PURE__*/ new Sphere();
  6957. const _sphereHitAt = /*@__PURE__*/ new Vector3();
  6958. const _vA$1 = /*@__PURE__*/ new Vector3();
  6959. const _vB$1 = /*@__PURE__*/ new Vector3();
  6960. const _vC$1 = /*@__PURE__*/ new Vector3();
  6961. const _tempA = /*@__PURE__*/ new Vector3();
  6962. const _morphA = /*@__PURE__*/ new Vector3();
  6963. const _intersectionPoint = /*@__PURE__*/ new Vector3();
  6964. const _intersectionPointWorld = /*@__PURE__*/ new Vector3();
  6965. class Mesh extends Object3D {
  6966. constructor( geometry = new BufferGeometry(), material = new MeshBasicMaterial() ) {
  6967. super();
  6968. this.isMesh = true;
  6969. this.type = 'Mesh';
  6970. this.geometry = geometry;
  6971. this.material = material;
  6972. this.updateMorphTargets();
  6973. }
  6974. copy( source, recursive ) {
  6975. super.copy( source, recursive );
  6976. if ( source.morphTargetInfluences !== undefined ) {
  6977. this.morphTargetInfluences = source.morphTargetInfluences.slice();
  6978. }
  6979. if ( source.morphTargetDictionary !== undefined ) {
  6980. this.morphTargetDictionary = Object.assign( {}, source.morphTargetDictionary );
  6981. }
  6982. this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
  6983. this.geometry = source.geometry;
  6984. return this;
  6985. }
  6986. updateMorphTargets() {
  6987. const geometry = this.geometry;
  6988. const morphAttributes = geometry.morphAttributes;
  6989. const keys = Object.keys( morphAttributes );
  6990. if ( keys.length > 0 ) {
  6991. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  6992. if ( morphAttribute !== undefined ) {
  6993. this.morphTargetInfluences = [];
  6994. this.morphTargetDictionary = {};
  6995. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  6996. const name = morphAttribute[ m ].name || String( m );
  6997. this.morphTargetInfluences.push( 0 );
  6998. this.morphTargetDictionary[ name ] = m;
  6999. }
  7000. }
  7001. }
  7002. }
  7003. getVertexPosition( index, target ) {
  7004. const geometry = this.geometry;
  7005. const position = geometry.attributes.position;
  7006. const morphPosition = geometry.morphAttributes.position;
  7007. const morphTargetsRelative = geometry.morphTargetsRelative;
  7008. target.fromBufferAttribute( position, index );
  7009. const morphInfluences = this.morphTargetInfluences;
  7010. if ( morphPosition && morphInfluences ) {
  7011. _morphA.set( 0, 0, 0 );
  7012. for ( let i = 0, il = morphPosition.length; i < il; i ++ ) {
  7013. const influence = morphInfluences[ i ];
  7014. const morphAttribute = morphPosition[ i ];
  7015. if ( influence === 0 ) continue;
  7016. _tempA.fromBufferAttribute( morphAttribute, index );
  7017. if ( morphTargetsRelative ) {
  7018. _morphA.addScaledVector( _tempA, influence );
  7019. } else {
  7020. _morphA.addScaledVector( _tempA.sub( target ), influence );
  7021. }
  7022. }
  7023. target.add( _morphA );
  7024. }
  7025. return target;
  7026. }
  7027. raycast( raycaster, intersects ) {
  7028. const geometry = this.geometry;
  7029. const material = this.material;
  7030. const matrixWorld = this.matrixWorld;
  7031. if ( material === undefined ) return;
  7032. // test with bounding sphere in world space
  7033. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  7034. _sphere$6.copy( geometry.boundingSphere );
  7035. _sphere$6.applyMatrix4( matrixWorld );
  7036. // check distance from ray origin to bounding sphere
  7037. _ray$3.copy( raycaster.ray ).recast( raycaster.near );
  7038. if ( _sphere$6.containsPoint( _ray$3.origin ) === false ) {
  7039. if ( _ray$3.intersectSphere( _sphere$6, _sphereHitAt ) === null ) return;
  7040. if ( _ray$3.origin.distanceToSquared( _sphereHitAt ) > ( raycaster.far - raycaster.near ) ** 2 ) return;
  7041. }
  7042. // convert ray to local space of mesh
  7043. _inverseMatrix$3.copy( matrixWorld ).invert();
  7044. _ray$3.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$3 );
  7045. // test with bounding box in local space
  7046. if ( geometry.boundingBox !== null ) {
  7047. if ( _ray$3.intersectsBox( geometry.boundingBox ) === false ) return;
  7048. }
  7049. // test for intersections with geometry
  7050. this._computeIntersections( raycaster, intersects, _ray$3 );
  7051. }
  7052. _computeIntersections( raycaster, intersects, rayLocalSpace ) {
  7053. let intersection;
  7054. const geometry = this.geometry;
  7055. const material = this.material;
  7056. const index = geometry.index;
  7057. const position = geometry.attributes.position;
  7058. const uv = geometry.attributes.uv;
  7059. const uv1 = geometry.attributes.uv1;
  7060. const normal = geometry.attributes.normal;
  7061. const groups = geometry.groups;
  7062. const drawRange = geometry.drawRange;
  7063. if ( index !== null ) {
  7064. // indexed buffer geometry
  7065. if ( Array.isArray( material ) ) {
  7066. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  7067. const group = groups[ i ];
  7068. const groupMaterial = material[ group.materialIndex ];
  7069. const start = Math.max( group.start, drawRange.start );
  7070. const end = Math.min( index.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) );
  7071. for ( let j = start, jl = end; j < jl; j += 3 ) {
  7072. const a = index.getX( j );
  7073. const b = index.getX( j + 1 );
  7074. const c = index.getX( j + 2 );
  7075. intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  7076. if ( intersection ) {
  7077. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics
  7078. intersection.face.materialIndex = group.materialIndex;
  7079. intersects.push( intersection );
  7080. }
  7081. }
  7082. }
  7083. } else {
  7084. const start = Math.max( 0, drawRange.start );
  7085. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  7086. for ( let i = start, il = end; i < il; i += 3 ) {
  7087. const a = index.getX( i );
  7088. const b = index.getX( i + 1 );
  7089. const c = index.getX( i + 2 );
  7090. intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  7091. if ( intersection ) {
  7092. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics
  7093. intersects.push( intersection );
  7094. }
  7095. }
  7096. }
  7097. } else if ( position !== undefined ) {
  7098. // non-indexed buffer geometry
  7099. if ( Array.isArray( material ) ) {
  7100. for ( let i = 0, il = groups.length; i < il; i ++ ) {
  7101. const group = groups[ i ];
  7102. const groupMaterial = material[ group.materialIndex ];
  7103. const start = Math.max( group.start, drawRange.start );
  7104. const end = Math.min( position.count, Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ) );
  7105. for ( let j = start, jl = end; j < jl; j += 3 ) {
  7106. const a = j;
  7107. const b = j + 1;
  7108. const c = j + 2;
  7109. intersection = checkGeometryIntersection( this, groupMaterial, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  7110. if ( intersection ) {
  7111. intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics
  7112. intersection.face.materialIndex = group.materialIndex;
  7113. intersects.push( intersection );
  7114. }
  7115. }
  7116. }
  7117. } else {
  7118. const start = Math.max( 0, drawRange.start );
  7119. const end = Math.min( position.count, ( drawRange.start + drawRange.count ) );
  7120. for ( let i = start, il = end; i < il; i += 3 ) {
  7121. const a = i;
  7122. const b = i + 1;
  7123. const c = i + 2;
  7124. intersection = checkGeometryIntersection( this, material, raycaster, rayLocalSpace, uv, uv1, normal, a, b, c );
  7125. if ( intersection ) {
  7126. intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics
  7127. intersects.push( intersection );
  7128. }
  7129. }
  7130. }
  7131. }
  7132. }
  7133. }
  7134. function checkIntersection$1( object, material, raycaster, ray, pA, pB, pC, point ) {
  7135. let intersect;
  7136. if ( material.side === BackSide ) {
  7137. intersect = ray.intersectTriangle( pC, pB, pA, true, point );
  7138. } else {
  7139. intersect = ray.intersectTriangle( pA, pB, pC, ( material.side === FrontSide ), point );
  7140. }
  7141. if ( intersect === null ) return null;
  7142. _intersectionPointWorld.copy( point );
  7143. _intersectionPointWorld.applyMatrix4( object.matrixWorld );
  7144. const distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld );
  7145. if ( distance < raycaster.near || distance > raycaster.far ) return null;
  7146. return {
  7147. distance: distance,
  7148. point: _intersectionPointWorld.clone(),
  7149. object: object
  7150. };
  7151. }
  7152. function checkGeometryIntersection( object, material, raycaster, ray, uv, uv1, normal, a, b, c ) {
  7153. object.getVertexPosition( a, _vA$1 );
  7154. object.getVertexPosition( b, _vB$1 );
  7155. object.getVertexPosition( c, _vC$1 );
  7156. const intersection = checkIntersection$1( object, material, raycaster, ray, _vA$1, _vB$1, _vC$1, _intersectionPoint );
  7157. if ( intersection ) {
  7158. const barycoord = new Vector3();
  7159. Triangle.getBarycoord( _intersectionPoint, _vA$1, _vB$1, _vC$1, barycoord );
  7160. if ( uv ) {
  7161. intersection.uv = Triangle.getInterpolatedAttribute( uv, a, b, c, barycoord, new Vector2() );
  7162. }
  7163. if ( uv1 ) {
  7164. intersection.uv1 = Triangle.getInterpolatedAttribute( uv1, a, b, c, barycoord, new Vector2() );
  7165. }
  7166. if ( normal ) {
  7167. intersection.normal = Triangle.getInterpolatedAttribute( normal, a, b, c, barycoord, new Vector3() );
  7168. if ( intersection.normal.dot( ray.direction ) > 0 ) {
  7169. intersection.normal.multiplyScalar( - 1 );
  7170. }
  7171. }
  7172. const face = {
  7173. a: a,
  7174. b: b,
  7175. c: c,
  7176. normal: new Vector3(),
  7177. materialIndex: 0
  7178. };
  7179. Triangle.getNormal( _vA$1, _vB$1, _vC$1, face.normal );
  7180. intersection.face = face;
  7181. intersection.barycoord = barycoord;
  7182. }
  7183. return intersection;
  7184. }
  7185. class BoxGeometry extends BufferGeometry {
  7186. constructor( width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1 ) {
  7187. super();
  7188. this.type = 'BoxGeometry';
  7189. this.parameters = {
  7190. width: width,
  7191. height: height,
  7192. depth: depth,
  7193. widthSegments: widthSegments,
  7194. heightSegments: heightSegments,
  7195. depthSegments: depthSegments
  7196. };
  7197. const scope = this;
  7198. // segments
  7199. widthSegments = Math.floor( widthSegments );
  7200. heightSegments = Math.floor( heightSegments );
  7201. depthSegments = Math.floor( depthSegments );
  7202. // buffers
  7203. const indices = [];
  7204. const vertices = [];
  7205. const normals = [];
  7206. const uvs = [];
  7207. // helper variables
  7208. let numberOfVertices = 0;
  7209. let groupStart = 0;
  7210. // build each side of the box geometry
  7211. buildPlane( 'z', 'y', 'x', - 1, - 1, depth, height, width, depthSegments, heightSegments, 0 ); // px
  7212. buildPlane( 'z', 'y', 'x', 1, - 1, depth, height, - width, depthSegments, heightSegments, 1 ); // nx
  7213. buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height, widthSegments, depthSegments, 2 ); // py
  7214. buildPlane( 'x', 'z', 'y', 1, - 1, width, depth, - height, widthSegments, depthSegments, 3 ); // ny
  7215. buildPlane( 'x', 'y', 'z', 1, - 1, width, height, depth, widthSegments, heightSegments, 4 ); // pz
  7216. buildPlane( 'x', 'y', 'z', - 1, - 1, width, height, - depth, widthSegments, heightSegments, 5 ); // nz
  7217. // build geometry
  7218. this.setIndex( indices );
  7219. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  7220. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  7221. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  7222. function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) {
  7223. const segmentWidth = width / gridX;
  7224. const segmentHeight = height / gridY;
  7225. const widthHalf = width / 2;
  7226. const heightHalf = height / 2;
  7227. const depthHalf = depth / 2;
  7228. const gridX1 = gridX + 1;
  7229. const gridY1 = gridY + 1;
  7230. let vertexCounter = 0;
  7231. let groupCount = 0;
  7232. const vector = new Vector3();
  7233. // generate vertices, normals and uvs
  7234. for ( let iy = 0; iy < gridY1; iy ++ ) {
  7235. const y = iy * segmentHeight - heightHalf;
  7236. for ( let ix = 0; ix < gridX1; ix ++ ) {
  7237. const x = ix * segmentWidth - widthHalf;
  7238. // set values to correct vector component
  7239. vector[ u ] = x * udir;
  7240. vector[ v ] = y * vdir;
  7241. vector[ w ] = depthHalf;
  7242. // now apply vector to vertex buffer
  7243. vertices.push( vector.x, vector.y, vector.z );
  7244. // set values to correct vector component
  7245. vector[ u ] = 0;
  7246. vector[ v ] = 0;
  7247. vector[ w ] = depth > 0 ? 1 : - 1;
  7248. // now apply vector to normal buffer
  7249. normals.push( vector.x, vector.y, vector.z );
  7250. // uvs
  7251. uvs.push( ix / gridX );
  7252. uvs.push( 1 - ( iy / gridY ) );
  7253. // counters
  7254. vertexCounter += 1;
  7255. }
  7256. }
  7257. // indices
  7258. // 1. you need three indices to draw a single face
  7259. // 2. a single segment consists of two faces
  7260. // 3. so we need to generate six (2*3) indices per segment
  7261. for ( let iy = 0; iy < gridY; iy ++ ) {
  7262. for ( let ix = 0; ix < gridX; ix ++ ) {
  7263. const a = numberOfVertices + ix + gridX1 * iy;
  7264. const b = numberOfVertices + ix + gridX1 * ( iy + 1 );
  7265. const c = numberOfVertices + ( ix + 1 ) + gridX1 * ( iy + 1 );
  7266. const d = numberOfVertices + ( ix + 1 ) + gridX1 * iy;
  7267. // faces
  7268. indices.push( a, b, d );
  7269. indices.push( b, c, d );
  7270. // increase counter
  7271. groupCount += 6;
  7272. }
  7273. }
  7274. // add a group to the geometry. this will ensure multi material support
  7275. scope.addGroup( groupStart, groupCount, materialIndex );
  7276. // calculate new start value for groups
  7277. groupStart += groupCount;
  7278. // update total number of vertices
  7279. numberOfVertices += vertexCounter;
  7280. }
  7281. }
  7282. copy( source ) {
  7283. super.copy( source );
  7284. this.parameters = Object.assign( {}, source.parameters );
  7285. return this;
  7286. }
  7287. static fromJSON( data ) {
  7288. return new BoxGeometry( data.width, data.height, data.depth, data.widthSegments, data.heightSegments, data.depthSegments );
  7289. }
  7290. }
  7291. /**
  7292. * Uniform Utilities
  7293. */
  7294. function cloneUniforms( src ) {
  7295. const dst = {};
  7296. for ( const u in src ) {
  7297. dst[ u ] = {};
  7298. for ( const p in src[ u ] ) {
  7299. const property = src[ u ][ p ];
  7300. if ( property && ( property.isColor ||
  7301. property.isMatrix3 || property.isMatrix4 ||
  7302. property.isVector2 || property.isVector3 || property.isVector4 ||
  7303. property.isTexture || property.isQuaternion ) ) {
  7304. if ( property.isRenderTargetTexture ) {
  7305. console.warn( 'UniformsUtils: Textures of render targets cannot be cloned via cloneUniforms() or mergeUniforms().' );
  7306. dst[ u ][ p ] = null;
  7307. } else {
  7308. dst[ u ][ p ] = property.clone();
  7309. }
  7310. } else if ( Array.isArray( property ) ) {
  7311. dst[ u ][ p ] = property.slice();
  7312. } else {
  7313. dst[ u ][ p ] = property;
  7314. }
  7315. }
  7316. }
  7317. return dst;
  7318. }
  7319. function mergeUniforms( uniforms ) {
  7320. const merged = {};
  7321. for ( let u = 0; u < uniforms.length; u ++ ) {
  7322. const tmp = cloneUniforms( uniforms[ u ] );
  7323. for ( const p in tmp ) {
  7324. merged[ p ] = tmp[ p ];
  7325. }
  7326. }
  7327. return merged;
  7328. }
  7329. function cloneUniformsGroups( src ) {
  7330. const dst = [];
  7331. for ( let u = 0; u < src.length; u ++ ) {
  7332. dst.push( src[ u ].clone() );
  7333. }
  7334. return dst;
  7335. }
  7336. function getUnlitUniformColorSpace( renderer ) {
  7337. const currentRenderTarget = renderer.getRenderTarget();
  7338. if ( currentRenderTarget === null ) {
  7339. // https://github.com/mrdoob/three.js/pull/23937#issuecomment-1111067398
  7340. return renderer.outputColorSpace;
  7341. }
  7342. // https://github.com/mrdoob/three.js/issues/27868
  7343. if ( currentRenderTarget.isXRRenderTarget === true ) {
  7344. return currentRenderTarget.texture.colorSpace;
  7345. }
  7346. return ColorManagement.workingColorSpace;
  7347. }
  7348. // Legacy
  7349. const UniformsUtils = { clone: cloneUniforms, merge: mergeUniforms };
  7350. var default_vertex = "void main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}";
  7351. var default_fragment = "void main() {\n\tgl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n}";
  7352. class ShaderMaterial extends Material {
  7353. constructor( parameters ) {
  7354. super();
  7355. this.isShaderMaterial = true;
  7356. this.type = 'ShaderMaterial';
  7357. this.defines = {};
  7358. this.uniforms = {};
  7359. this.uniformsGroups = [];
  7360. this.vertexShader = default_vertex;
  7361. this.fragmentShader = default_fragment;
  7362. this.linewidth = 1;
  7363. this.wireframe = false;
  7364. this.wireframeLinewidth = 1;
  7365. this.fog = false; // set to use scene fog
  7366. this.lights = false; // set to use scene lights
  7367. this.clipping = false; // set to use user-defined clipping planes
  7368. this.forceSinglePass = true;
  7369. this.extensions = {
  7370. clipCullDistance: false, // set to use vertex shader clipping
  7371. multiDraw: false // set to use vertex shader multi_draw / enable gl_DrawID
  7372. };
  7373. // When rendered geometry doesn't include these attributes but the material does,
  7374. // use these default values in WebGL. This avoids errors when buffer data is missing.
  7375. this.defaultAttributeValues = {
  7376. 'color': [ 1, 1, 1 ],
  7377. 'uv': [ 0, 0 ],
  7378. 'uv1': [ 0, 0 ]
  7379. };
  7380. this.index0AttributeName = undefined;
  7381. this.uniformsNeedUpdate = false;
  7382. this.glslVersion = null;
  7383. if ( parameters !== undefined ) {
  7384. this.setValues( parameters );
  7385. }
  7386. }
  7387. copy( source ) {
  7388. super.copy( source );
  7389. this.fragmentShader = source.fragmentShader;
  7390. this.vertexShader = source.vertexShader;
  7391. this.uniforms = cloneUniforms( source.uniforms );
  7392. this.uniformsGroups = cloneUniformsGroups( source.uniformsGroups );
  7393. this.defines = Object.assign( {}, source.defines );
  7394. this.wireframe = source.wireframe;
  7395. this.wireframeLinewidth = source.wireframeLinewidth;
  7396. this.fog = source.fog;
  7397. this.lights = source.lights;
  7398. this.clipping = source.clipping;
  7399. this.extensions = Object.assign( {}, source.extensions );
  7400. this.glslVersion = source.glslVersion;
  7401. return this;
  7402. }
  7403. toJSON( meta ) {
  7404. const data = super.toJSON( meta );
  7405. data.glslVersion = this.glslVersion;
  7406. data.uniforms = {};
  7407. for ( const name in this.uniforms ) {
  7408. const uniform = this.uniforms[ name ];
  7409. const value = uniform.value;
  7410. if ( value && value.isTexture ) {
  7411. data.uniforms[ name ] = {
  7412. type: 't',
  7413. value: value.toJSON( meta ).uuid
  7414. };
  7415. } else if ( value && value.isColor ) {
  7416. data.uniforms[ name ] = {
  7417. type: 'c',
  7418. value: value.getHex()
  7419. };
  7420. } else if ( value && value.isVector2 ) {
  7421. data.uniforms[ name ] = {
  7422. type: 'v2',
  7423. value: value.toArray()
  7424. };
  7425. } else if ( value && value.isVector3 ) {
  7426. data.uniforms[ name ] = {
  7427. type: 'v3',
  7428. value: value.toArray()
  7429. };
  7430. } else if ( value && value.isVector4 ) {
  7431. data.uniforms[ name ] = {
  7432. type: 'v4',
  7433. value: value.toArray()
  7434. };
  7435. } else if ( value && value.isMatrix3 ) {
  7436. data.uniforms[ name ] = {
  7437. type: 'm3',
  7438. value: value.toArray()
  7439. };
  7440. } else if ( value && value.isMatrix4 ) {
  7441. data.uniforms[ name ] = {
  7442. type: 'm4',
  7443. value: value.toArray()
  7444. };
  7445. } else {
  7446. data.uniforms[ name ] = {
  7447. value: value
  7448. };
  7449. // note: the array variants v2v, v3v, v4v, m4v and tv are not supported so far
  7450. }
  7451. }
  7452. if ( Object.keys( this.defines ).length > 0 ) data.defines = this.defines;
  7453. data.vertexShader = this.vertexShader;
  7454. data.fragmentShader = this.fragmentShader;
  7455. data.lights = this.lights;
  7456. data.clipping = this.clipping;
  7457. const extensions = {};
  7458. for ( const key in this.extensions ) {
  7459. if ( this.extensions[ key ] === true ) extensions[ key ] = true;
  7460. }
  7461. if ( Object.keys( extensions ).length > 0 ) data.extensions = extensions;
  7462. return data;
  7463. }
  7464. }
  7465. class Camera extends Object3D {
  7466. constructor() {
  7467. super();
  7468. this.isCamera = true;
  7469. this.type = 'Camera';
  7470. this.matrixWorldInverse = new Matrix4();
  7471. this.projectionMatrix = new Matrix4();
  7472. this.projectionMatrixInverse = new Matrix4();
  7473. this.coordinateSystem = WebGLCoordinateSystem;
  7474. }
  7475. copy( source, recursive ) {
  7476. super.copy( source, recursive );
  7477. this.matrixWorldInverse.copy( source.matrixWorldInverse );
  7478. this.projectionMatrix.copy( source.projectionMatrix );
  7479. this.projectionMatrixInverse.copy( source.projectionMatrixInverse );
  7480. this.coordinateSystem = source.coordinateSystem;
  7481. return this;
  7482. }
  7483. getWorldDirection( target ) {
  7484. return super.getWorldDirection( target ).negate();
  7485. }
  7486. updateMatrixWorld( force ) {
  7487. super.updateMatrixWorld( force );
  7488. this.matrixWorldInverse.copy( this.matrixWorld ).invert();
  7489. }
  7490. updateWorldMatrix( updateParents, updateChildren ) {
  7491. super.updateWorldMatrix( updateParents, updateChildren );
  7492. this.matrixWorldInverse.copy( this.matrixWorld ).invert();
  7493. }
  7494. clone() {
  7495. return new this.constructor().copy( this );
  7496. }
  7497. }
  7498. const _v3$1 = /*@__PURE__*/ new Vector3();
  7499. const _minTarget = /*@__PURE__*/ new Vector2();
  7500. const _maxTarget = /*@__PURE__*/ new Vector2();
  7501. class PerspectiveCamera extends Camera {
  7502. constructor( fov = 50, aspect = 1, near = 0.1, far = 2000 ) {
  7503. super();
  7504. this.isPerspectiveCamera = true;
  7505. this.type = 'PerspectiveCamera';
  7506. this.fov = fov;
  7507. this.zoom = 1;
  7508. this.near = near;
  7509. this.far = far;
  7510. this.focus = 10;
  7511. this.aspect = aspect;
  7512. this.view = null;
  7513. this.filmGauge = 35; // width of the film (default in millimeters)
  7514. this.filmOffset = 0; // horizontal film offset (same unit as gauge)
  7515. this.updateProjectionMatrix();
  7516. }
  7517. copy( source, recursive ) {
  7518. super.copy( source, recursive );
  7519. this.fov = source.fov;
  7520. this.zoom = source.zoom;
  7521. this.near = source.near;
  7522. this.far = source.far;
  7523. this.focus = source.focus;
  7524. this.aspect = source.aspect;
  7525. this.view = source.view === null ? null : Object.assign( {}, source.view );
  7526. this.filmGauge = source.filmGauge;
  7527. this.filmOffset = source.filmOffset;
  7528. return this;
  7529. }
  7530. /**
  7531. * Sets the FOV by focal length in respect to the current .filmGauge.
  7532. *
  7533. * The default film gauge is 35, so that the focal length can be specified for
  7534. * a 35mm (full frame) camera.
  7535. *
  7536. * Values for focal length and film gauge must have the same unit.
  7537. */
  7538. setFocalLength( focalLength ) {
  7539. /** see {@link http://www.bobatkins.com/photography/technical/field_of_view.html} */
  7540. const vExtentSlope = 0.5 * this.getFilmHeight() / focalLength;
  7541. this.fov = RAD2DEG * 2 * Math.atan( vExtentSlope );
  7542. this.updateProjectionMatrix();
  7543. }
  7544. /**
  7545. * Calculates the focal length from the current .fov and .filmGauge.
  7546. */
  7547. getFocalLength() {
  7548. const vExtentSlope = Math.tan( DEG2RAD * 0.5 * this.fov );
  7549. return 0.5 * this.getFilmHeight() / vExtentSlope;
  7550. }
  7551. getEffectiveFOV() {
  7552. return RAD2DEG * 2 * Math.atan(
  7553. Math.tan( DEG2RAD * 0.5 * this.fov ) / this.zoom );
  7554. }
  7555. getFilmWidth() {
  7556. // film not completely covered in portrait format (aspect < 1)
  7557. return this.filmGauge * Math.min( this.aspect, 1 );
  7558. }
  7559. getFilmHeight() {
  7560. // film not completely covered in landscape format (aspect > 1)
  7561. return this.filmGauge / Math.max( this.aspect, 1 );
  7562. }
  7563. /**
  7564. * Computes the 2D bounds of the camera's viewable rectangle at a given distance along the viewing direction.
  7565. * Sets minTarget and maxTarget to the coordinates of the lower-left and upper-right corners of the view rectangle.
  7566. */
  7567. getViewBounds( distance, minTarget, maxTarget ) {
  7568. _v3$1.set( - 1, - 1, 0.5 ).applyMatrix4( this.projectionMatrixInverse );
  7569. minTarget.set( _v3$1.x, _v3$1.y ).multiplyScalar( - distance / _v3$1.z );
  7570. _v3$1.set( 1, 1, 0.5 ).applyMatrix4( this.projectionMatrixInverse );
  7571. maxTarget.set( _v3$1.x, _v3$1.y ).multiplyScalar( - distance / _v3$1.z );
  7572. }
  7573. /**
  7574. * Computes the width and height of the camera's viewable rectangle at a given distance along the viewing direction.
  7575. * Copies the result into the target Vector2, where x is width and y is height.
  7576. */
  7577. getViewSize( distance, target ) {
  7578. this.getViewBounds( distance, _minTarget, _maxTarget );
  7579. return target.subVectors( _maxTarget, _minTarget );
  7580. }
  7581. /**
  7582. * Sets an offset in a larger frustum. This is useful for multi-window or
  7583. * multi-monitor/multi-machine setups.
  7584. *
  7585. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  7586. * the monitors are in grid like this
  7587. *
  7588. * +---+---+---+
  7589. * | A | B | C |
  7590. * +---+---+---+
  7591. * | D | E | F |
  7592. * +---+---+---+
  7593. *
  7594. * then for each monitor you would call it like this
  7595. *
  7596. * const w = 1920;
  7597. * const h = 1080;
  7598. * const fullWidth = w * 3;
  7599. * const fullHeight = h * 2;
  7600. *
  7601. * --A--
  7602. * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  7603. * --B--
  7604. * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  7605. * --C--
  7606. * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  7607. * --D--
  7608. * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  7609. * --E--
  7610. * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  7611. * --F--
  7612. * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  7613. *
  7614. * Note there is no reason monitors have to be the same size or in a grid.
  7615. */
  7616. setViewOffset( fullWidth, fullHeight, x, y, width, height ) {
  7617. this.aspect = fullWidth / fullHeight;
  7618. if ( this.view === null ) {
  7619. this.view = {
  7620. enabled: true,
  7621. fullWidth: 1,
  7622. fullHeight: 1,
  7623. offsetX: 0,
  7624. offsetY: 0,
  7625. width: 1,
  7626. height: 1
  7627. };
  7628. }
  7629. this.view.enabled = true;
  7630. this.view.fullWidth = fullWidth;
  7631. this.view.fullHeight = fullHeight;
  7632. this.view.offsetX = x;
  7633. this.view.offsetY = y;
  7634. this.view.width = width;
  7635. this.view.height = height;
  7636. this.updateProjectionMatrix();
  7637. }
  7638. clearViewOffset() {
  7639. if ( this.view !== null ) {
  7640. this.view.enabled = false;
  7641. }
  7642. this.updateProjectionMatrix();
  7643. }
  7644. updateProjectionMatrix() {
  7645. const near = this.near;
  7646. let top = near * Math.tan( DEG2RAD * 0.5 * this.fov ) / this.zoom;
  7647. let height = 2 * top;
  7648. let width = this.aspect * height;
  7649. let left = - 0.5 * width;
  7650. const view = this.view;
  7651. if ( this.view !== null && this.view.enabled ) {
  7652. const fullWidth = view.fullWidth,
  7653. fullHeight = view.fullHeight;
  7654. left += view.offsetX * width / fullWidth;
  7655. top -= view.offsetY * height / fullHeight;
  7656. width *= view.width / fullWidth;
  7657. height *= view.height / fullHeight;
  7658. }
  7659. const skew = this.filmOffset;
  7660. if ( skew !== 0 ) left += near * skew / this.getFilmWidth();
  7661. this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far, this.coordinateSystem );
  7662. this.projectionMatrixInverse.copy( this.projectionMatrix ).invert();
  7663. }
  7664. toJSON( meta ) {
  7665. const data = super.toJSON( meta );
  7666. data.object.fov = this.fov;
  7667. data.object.zoom = this.zoom;
  7668. data.object.near = this.near;
  7669. data.object.far = this.far;
  7670. data.object.focus = this.focus;
  7671. data.object.aspect = this.aspect;
  7672. if ( this.view !== null ) data.object.view = Object.assign( {}, this.view );
  7673. data.object.filmGauge = this.filmGauge;
  7674. data.object.filmOffset = this.filmOffset;
  7675. return data;
  7676. }
  7677. }
  7678. const fov = - 90; // negative fov is not an error
  7679. const aspect = 1;
  7680. class CubeCamera extends Object3D {
  7681. constructor( near, far, renderTarget ) {
  7682. super();
  7683. this.type = 'CubeCamera';
  7684. this.renderTarget = renderTarget;
  7685. this.coordinateSystem = null;
  7686. this.activeMipmapLevel = 0;
  7687. const cameraPX = new PerspectiveCamera( fov, aspect, near, far );
  7688. cameraPX.layers = this.layers;
  7689. this.add( cameraPX );
  7690. const cameraNX = new PerspectiveCamera( fov, aspect, near, far );
  7691. cameraNX.layers = this.layers;
  7692. this.add( cameraNX );
  7693. const cameraPY = new PerspectiveCamera( fov, aspect, near, far );
  7694. cameraPY.layers = this.layers;
  7695. this.add( cameraPY );
  7696. const cameraNY = new PerspectiveCamera( fov, aspect, near, far );
  7697. cameraNY.layers = this.layers;
  7698. this.add( cameraNY );
  7699. const cameraPZ = new PerspectiveCamera( fov, aspect, near, far );
  7700. cameraPZ.layers = this.layers;
  7701. this.add( cameraPZ );
  7702. const cameraNZ = new PerspectiveCamera( fov, aspect, near, far );
  7703. cameraNZ.layers = this.layers;
  7704. this.add( cameraNZ );
  7705. }
  7706. updateCoordinateSystem() {
  7707. const coordinateSystem = this.coordinateSystem;
  7708. const cameras = this.children.concat();
  7709. const [ cameraPX, cameraNX, cameraPY, cameraNY, cameraPZ, cameraNZ ] = cameras;
  7710. for ( const camera of cameras ) this.remove( camera );
  7711. if ( coordinateSystem === WebGLCoordinateSystem ) {
  7712. cameraPX.up.set( 0, 1, 0 );
  7713. cameraPX.lookAt( 1, 0, 0 );
  7714. cameraNX.up.set( 0, 1, 0 );
  7715. cameraNX.lookAt( - 1, 0, 0 );
  7716. cameraPY.up.set( 0, 0, - 1 );
  7717. cameraPY.lookAt( 0, 1, 0 );
  7718. cameraNY.up.set( 0, 0, 1 );
  7719. cameraNY.lookAt( 0, - 1, 0 );
  7720. cameraPZ.up.set( 0, 1, 0 );
  7721. cameraPZ.lookAt( 0, 0, 1 );
  7722. cameraNZ.up.set( 0, 1, 0 );
  7723. cameraNZ.lookAt( 0, 0, - 1 );
  7724. } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
  7725. cameraPX.up.set( 0, - 1, 0 );
  7726. cameraPX.lookAt( - 1, 0, 0 );
  7727. cameraNX.up.set( 0, - 1, 0 );
  7728. cameraNX.lookAt( 1, 0, 0 );
  7729. cameraPY.up.set( 0, 0, 1 );
  7730. cameraPY.lookAt( 0, 1, 0 );
  7731. cameraNY.up.set( 0, 0, - 1 );
  7732. cameraNY.lookAt( 0, - 1, 0 );
  7733. cameraPZ.up.set( 0, - 1, 0 );
  7734. cameraPZ.lookAt( 0, 0, 1 );
  7735. cameraNZ.up.set( 0, - 1, 0 );
  7736. cameraNZ.lookAt( 0, 0, - 1 );
  7737. } else {
  7738. throw new Error( 'THREE.CubeCamera.updateCoordinateSystem(): Invalid coordinate system: ' + coordinateSystem );
  7739. }
  7740. for ( const camera of cameras ) {
  7741. this.add( camera );
  7742. camera.updateMatrixWorld();
  7743. }
  7744. }
  7745. update( renderer, scene ) {
  7746. if ( this.parent === null ) this.updateMatrixWorld();
  7747. const { renderTarget, activeMipmapLevel } = this;
  7748. if ( this.coordinateSystem !== renderer.coordinateSystem ) {
  7749. this.coordinateSystem = renderer.coordinateSystem;
  7750. this.updateCoordinateSystem();
  7751. }
  7752. const [ cameraPX, cameraNX, cameraPY, cameraNY, cameraPZ, cameraNZ ] = this.children;
  7753. const currentRenderTarget = renderer.getRenderTarget();
  7754. const currentActiveCubeFace = renderer.getActiveCubeFace();
  7755. const currentActiveMipmapLevel = renderer.getActiveMipmapLevel();
  7756. const currentXrEnabled = renderer.xr.enabled;
  7757. renderer.xr.enabled = false;
  7758. const generateMipmaps = renderTarget.texture.generateMipmaps;
  7759. renderTarget.texture.generateMipmaps = false;
  7760. renderer.setRenderTarget( renderTarget, 0, activeMipmapLevel );
  7761. renderer.render( scene, cameraPX );
  7762. renderer.setRenderTarget( renderTarget, 1, activeMipmapLevel );
  7763. renderer.render( scene, cameraNX );
  7764. renderer.setRenderTarget( renderTarget, 2, activeMipmapLevel );
  7765. renderer.render( scene, cameraPY );
  7766. renderer.setRenderTarget( renderTarget, 3, activeMipmapLevel );
  7767. renderer.render( scene, cameraNY );
  7768. renderer.setRenderTarget( renderTarget, 4, activeMipmapLevel );
  7769. renderer.render( scene, cameraPZ );
  7770. // mipmaps are generated during the last call of render()
  7771. // at this point, all sides of the cube render target are defined
  7772. renderTarget.texture.generateMipmaps = generateMipmaps;
  7773. renderer.setRenderTarget( renderTarget, 5, activeMipmapLevel );
  7774. renderer.render( scene, cameraNZ );
  7775. renderer.setRenderTarget( currentRenderTarget, currentActiveCubeFace, currentActiveMipmapLevel );
  7776. renderer.xr.enabled = currentXrEnabled;
  7777. renderTarget.texture.needsPMREMUpdate = true;
  7778. }
  7779. }
  7780. class CubeTexture extends Texture {
  7781. constructor( images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace ) {
  7782. images = images !== undefined ? images : [];
  7783. mapping = mapping !== undefined ? mapping : CubeReflectionMapping;
  7784. super( images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace );
  7785. this.isCubeTexture = true;
  7786. this.flipY = false;
  7787. }
  7788. get images() {
  7789. return this.image;
  7790. }
  7791. set images( value ) {
  7792. this.image = value;
  7793. }
  7794. }
  7795. class WebGLCubeRenderTarget extends WebGLRenderTarget {
  7796. constructor( size = 1, options = {} ) {
  7797. super( size, size, options );
  7798. this.isWebGLCubeRenderTarget = true;
  7799. const image = { width: size, height: size, depth: 1 };
  7800. const images = [ image, image, image, image, image, image ];
  7801. this.texture = new CubeTexture( images, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.colorSpace );
  7802. // By convention -- likely based on the RenderMan spec from the 1990's -- cube maps are specified by WebGL (and three.js)
  7803. // in a coordinate system in which positive-x is to the right when looking up the positive-z axis -- in other words,
  7804. // in a left-handed coordinate system. By continuing this convention, preexisting cube maps continued to render correctly.
  7805. // three.js uses a right-handed coordinate system. So environment maps used in three.js appear to have px and nx swapped
  7806. // and the flag isRenderTargetTexture controls this conversion. The flip is not required when using WebGLCubeRenderTarget.texture
  7807. // as a cube texture (this is detected when isRenderTargetTexture is set to true for cube textures).
  7808. this.texture.isRenderTargetTexture = true;
  7809. this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false;
  7810. this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter;
  7811. }
  7812. fromEquirectangularTexture( renderer, texture ) {
  7813. this.texture.type = texture.type;
  7814. this.texture.colorSpace = texture.colorSpace;
  7815. this.texture.generateMipmaps = texture.generateMipmaps;
  7816. this.texture.minFilter = texture.minFilter;
  7817. this.texture.magFilter = texture.magFilter;
  7818. const shader = {
  7819. uniforms: {
  7820. tEquirect: { value: null },
  7821. },
  7822. vertexShader: /* glsl */`
  7823. varying vec3 vWorldDirection;
  7824. vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
  7825. return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
  7826. }
  7827. void main() {
  7828. vWorldDirection = transformDirection( position, modelMatrix );
  7829. #include <begin_vertex>
  7830. #include <project_vertex>
  7831. }
  7832. `,
  7833. fragmentShader: /* glsl */`
  7834. uniform sampler2D tEquirect;
  7835. varying vec3 vWorldDirection;
  7836. #include <common>
  7837. void main() {
  7838. vec3 direction = normalize( vWorldDirection );
  7839. vec2 sampleUV = equirectUv( direction );
  7840. gl_FragColor = texture2D( tEquirect, sampleUV );
  7841. }
  7842. `
  7843. };
  7844. const geometry = new BoxGeometry( 5, 5, 5 );
  7845. const material = new ShaderMaterial( {
  7846. name: 'CubemapFromEquirect',
  7847. uniforms: cloneUniforms( shader.uniforms ),
  7848. vertexShader: shader.vertexShader,
  7849. fragmentShader: shader.fragmentShader,
  7850. side: BackSide,
  7851. blending: NoBlending
  7852. } );
  7853. material.uniforms.tEquirect.value = texture;
  7854. const mesh = new Mesh( geometry, material );
  7855. const currentMinFilter = texture.minFilter;
  7856. // Avoid blurred poles
  7857. if ( texture.minFilter === LinearMipmapLinearFilter ) texture.minFilter = LinearFilter;
  7858. const camera = new CubeCamera( 1, 10, this );
  7859. camera.update( renderer, mesh );
  7860. texture.minFilter = currentMinFilter;
  7861. mesh.geometry.dispose();
  7862. mesh.material.dispose();
  7863. return this;
  7864. }
  7865. clear( renderer, color, depth, stencil ) {
  7866. const currentRenderTarget = renderer.getRenderTarget();
  7867. for ( let i = 0; i < 6; i ++ ) {
  7868. renderer.setRenderTarget( this, i );
  7869. renderer.clear( color, depth, stencil );
  7870. }
  7871. renderer.setRenderTarget( currentRenderTarget );
  7872. }
  7873. }
  7874. class FogExp2 {
  7875. constructor( color, density = 0.00025 ) {
  7876. this.isFogExp2 = true;
  7877. this.name = '';
  7878. this.color = new Color( color );
  7879. this.density = density;
  7880. }
  7881. clone() {
  7882. return new FogExp2( this.color, this.density );
  7883. }
  7884. toJSON( /* meta */ ) {
  7885. return {
  7886. type: 'FogExp2',
  7887. name: this.name,
  7888. color: this.color.getHex(),
  7889. density: this.density
  7890. };
  7891. }
  7892. }
  7893. class Fog {
  7894. constructor( color, near = 1, far = 1000 ) {
  7895. this.isFog = true;
  7896. this.name = '';
  7897. this.color = new Color( color );
  7898. this.near = near;
  7899. this.far = far;
  7900. }
  7901. clone() {
  7902. return new Fog( this.color, this.near, this.far );
  7903. }
  7904. toJSON( /* meta */ ) {
  7905. return {
  7906. type: 'Fog',
  7907. name: this.name,
  7908. color: this.color.getHex(),
  7909. near: this.near,
  7910. far: this.far
  7911. };
  7912. }
  7913. }
  7914. class Scene extends Object3D {
  7915. constructor() {
  7916. super();
  7917. this.isScene = true;
  7918. this.type = 'Scene';
  7919. this.background = null;
  7920. this.environment = null;
  7921. this.fog = null;
  7922. this.backgroundBlurriness = 0;
  7923. this.backgroundIntensity = 1;
  7924. this.backgroundRotation = new Euler();
  7925. this.environmentIntensity = 1;
  7926. this.environmentRotation = new Euler();
  7927. this.overrideMaterial = null;
  7928. if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
  7929. __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) );
  7930. }
  7931. }
  7932. copy( source, recursive ) {
  7933. super.copy( source, recursive );
  7934. if ( source.background !== null ) this.background = source.background.clone();
  7935. if ( source.environment !== null ) this.environment = source.environment.clone();
  7936. if ( source.fog !== null ) this.fog = source.fog.clone();
  7937. this.backgroundBlurriness = source.backgroundBlurriness;
  7938. this.backgroundIntensity = source.backgroundIntensity;
  7939. this.backgroundRotation.copy( source.backgroundRotation );
  7940. this.environmentIntensity = source.environmentIntensity;
  7941. this.environmentRotation.copy( source.environmentRotation );
  7942. if ( source.overrideMaterial !== null ) this.overrideMaterial = source.overrideMaterial.clone();
  7943. this.matrixAutoUpdate = source.matrixAutoUpdate;
  7944. return this;
  7945. }
  7946. toJSON( meta ) {
  7947. const data = super.toJSON( meta );
  7948. if ( this.fog !== null ) data.object.fog = this.fog.toJSON();
  7949. if ( this.backgroundBlurriness > 0 ) data.object.backgroundBlurriness = this.backgroundBlurriness;
  7950. if ( this.backgroundIntensity !== 1 ) data.object.backgroundIntensity = this.backgroundIntensity;
  7951. data.object.backgroundRotation = this.backgroundRotation.toArray();
  7952. if ( this.environmentIntensity !== 1 ) data.object.environmentIntensity = this.environmentIntensity;
  7953. data.object.environmentRotation = this.environmentRotation.toArray();
  7954. return data;
  7955. }
  7956. }
  7957. class InterleavedBuffer {
  7958. constructor( array, stride ) {
  7959. this.isInterleavedBuffer = true;
  7960. this.array = array;
  7961. this.stride = stride;
  7962. this.count = array !== undefined ? array.length / stride : 0;
  7963. this.usage = StaticDrawUsage;
  7964. this.updateRanges = [];
  7965. this.version = 0;
  7966. this.uuid = generateUUID();
  7967. }
  7968. onUploadCallback() {}
  7969. set needsUpdate( value ) {
  7970. if ( value === true ) this.version ++;
  7971. }
  7972. setUsage( value ) {
  7973. this.usage = value;
  7974. return this;
  7975. }
  7976. addUpdateRange( start, count ) {
  7977. this.updateRanges.push( { start, count } );
  7978. }
  7979. clearUpdateRanges() {
  7980. this.updateRanges.length = 0;
  7981. }
  7982. copy( source ) {
  7983. this.array = new source.array.constructor( source.array );
  7984. this.count = source.count;
  7985. this.stride = source.stride;
  7986. this.usage = source.usage;
  7987. return this;
  7988. }
  7989. copyAt( index1, attribute, index2 ) {
  7990. index1 *= this.stride;
  7991. index2 *= attribute.stride;
  7992. for ( let i = 0, l = this.stride; i < l; i ++ ) {
  7993. this.array[ index1 + i ] = attribute.array[ index2 + i ];
  7994. }
  7995. return this;
  7996. }
  7997. set( value, offset = 0 ) {
  7998. this.array.set( value, offset );
  7999. return this;
  8000. }
  8001. clone( data ) {
  8002. if ( data.arrayBuffers === undefined ) {
  8003. data.arrayBuffers = {};
  8004. }
  8005. if ( this.array.buffer._uuid === undefined ) {
  8006. this.array.buffer._uuid = generateUUID();
  8007. }
  8008. if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) {
  8009. data.arrayBuffers[ this.array.buffer._uuid ] = this.array.slice( 0 ).buffer;
  8010. }
  8011. const array = new this.array.constructor( data.arrayBuffers[ this.array.buffer._uuid ] );
  8012. const ib = new this.constructor( array, this.stride );
  8013. ib.setUsage( this.usage );
  8014. return ib;
  8015. }
  8016. onUpload( callback ) {
  8017. this.onUploadCallback = callback;
  8018. return this;
  8019. }
  8020. toJSON( data ) {
  8021. if ( data.arrayBuffers === undefined ) {
  8022. data.arrayBuffers = {};
  8023. }
  8024. // generate UUID for array buffer if necessary
  8025. if ( this.array.buffer._uuid === undefined ) {
  8026. this.array.buffer._uuid = generateUUID();
  8027. }
  8028. if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) {
  8029. data.arrayBuffers[ this.array.buffer._uuid ] = Array.from( new Uint32Array( this.array.buffer ) );
  8030. }
  8031. //
  8032. return {
  8033. uuid: this.uuid,
  8034. buffer: this.array.buffer._uuid,
  8035. type: this.array.constructor.name,
  8036. stride: this.stride
  8037. };
  8038. }
  8039. }
  8040. const _vector$7 = /*@__PURE__*/ new Vector3();
  8041. class InterleavedBufferAttribute {
  8042. constructor( interleavedBuffer, itemSize, offset, normalized = false ) {
  8043. this.isInterleavedBufferAttribute = true;
  8044. this.name = '';
  8045. this.data = interleavedBuffer;
  8046. this.itemSize = itemSize;
  8047. this.offset = offset;
  8048. this.normalized = normalized;
  8049. }
  8050. get count() {
  8051. return this.data.count;
  8052. }
  8053. get array() {
  8054. return this.data.array;
  8055. }
  8056. set needsUpdate( value ) {
  8057. this.data.needsUpdate = value;
  8058. }
  8059. applyMatrix4( m ) {
  8060. for ( let i = 0, l = this.data.count; i < l; i ++ ) {
  8061. _vector$7.fromBufferAttribute( this, i );
  8062. _vector$7.applyMatrix4( m );
  8063. this.setXYZ( i, _vector$7.x, _vector$7.y, _vector$7.z );
  8064. }
  8065. return this;
  8066. }
  8067. applyNormalMatrix( m ) {
  8068. for ( let i = 0, l = this.count; i < l; i ++ ) {
  8069. _vector$7.fromBufferAttribute( this, i );
  8070. _vector$7.applyNormalMatrix( m );
  8071. this.setXYZ( i, _vector$7.x, _vector$7.y, _vector$7.z );
  8072. }
  8073. return this;
  8074. }
  8075. transformDirection( m ) {
  8076. for ( let i = 0, l = this.count; i < l; i ++ ) {
  8077. _vector$7.fromBufferAttribute( this, i );
  8078. _vector$7.transformDirection( m );
  8079. this.setXYZ( i, _vector$7.x, _vector$7.y, _vector$7.z );
  8080. }
  8081. return this;
  8082. }
  8083. getComponent( index, component ) {
  8084. let value = this.array[ index * this.data.stride + this.offset + component ];
  8085. if ( this.normalized ) value = denormalize( value, this.array );
  8086. return value;
  8087. }
  8088. setComponent( index, component, value ) {
  8089. if ( this.normalized ) value = normalize( value, this.array );
  8090. this.data.array[ index * this.data.stride + this.offset + component ] = value;
  8091. return this;
  8092. }
  8093. setX( index, x ) {
  8094. if ( this.normalized ) x = normalize( x, this.array );
  8095. this.data.array[ index * this.data.stride + this.offset ] = x;
  8096. return this;
  8097. }
  8098. setY( index, y ) {
  8099. if ( this.normalized ) y = normalize( y, this.array );
  8100. this.data.array[ index * this.data.stride + this.offset + 1 ] = y;
  8101. return this;
  8102. }
  8103. setZ( index, z ) {
  8104. if ( this.normalized ) z = normalize( z, this.array );
  8105. this.data.array[ index * this.data.stride + this.offset + 2 ] = z;
  8106. return this;
  8107. }
  8108. setW( index, w ) {
  8109. if ( this.normalized ) w = normalize( w, this.array );
  8110. this.data.array[ index * this.data.stride + this.offset + 3 ] = w;
  8111. return this;
  8112. }
  8113. getX( index ) {
  8114. let x = this.data.array[ index * this.data.stride + this.offset ];
  8115. if ( this.normalized ) x = denormalize( x, this.array );
  8116. return x;
  8117. }
  8118. getY( index ) {
  8119. let y = this.data.array[ index * this.data.stride + this.offset + 1 ];
  8120. if ( this.normalized ) y = denormalize( y, this.array );
  8121. return y;
  8122. }
  8123. getZ( index ) {
  8124. let z = this.data.array[ index * this.data.stride + this.offset + 2 ];
  8125. if ( this.normalized ) z = denormalize( z, this.array );
  8126. return z;
  8127. }
  8128. getW( index ) {
  8129. let w = this.data.array[ index * this.data.stride + this.offset + 3 ];
  8130. if ( this.normalized ) w = denormalize( w, this.array );
  8131. return w;
  8132. }
  8133. setXY( index, x, y ) {
  8134. index = index * this.data.stride + this.offset;
  8135. if ( this.normalized ) {
  8136. x = normalize( x, this.array );
  8137. y = normalize( y, this.array );
  8138. }
  8139. this.data.array[ index + 0 ] = x;
  8140. this.data.array[ index + 1 ] = y;
  8141. return this;
  8142. }
  8143. setXYZ( index, x, y, z ) {
  8144. index = index * this.data.stride + this.offset;
  8145. if ( this.normalized ) {
  8146. x = normalize( x, this.array );
  8147. y = normalize( y, this.array );
  8148. z = normalize( z, this.array );
  8149. }
  8150. this.data.array[ index + 0 ] = x;
  8151. this.data.array[ index + 1 ] = y;
  8152. this.data.array[ index + 2 ] = z;
  8153. return this;
  8154. }
  8155. setXYZW( index, x, y, z, w ) {
  8156. index = index * this.data.stride + this.offset;
  8157. if ( this.normalized ) {
  8158. x = normalize( x, this.array );
  8159. y = normalize( y, this.array );
  8160. z = normalize( z, this.array );
  8161. w = normalize( w, this.array );
  8162. }
  8163. this.data.array[ index + 0 ] = x;
  8164. this.data.array[ index + 1 ] = y;
  8165. this.data.array[ index + 2 ] = z;
  8166. this.data.array[ index + 3 ] = w;
  8167. return this;
  8168. }
  8169. clone( data ) {
  8170. if ( data === undefined ) {
  8171. console.log( 'THREE.InterleavedBufferAttribute.clone(): Cloning an interleaved buffer attribute will de-interleave buffer data.' );
  8172. const array = [];
  8173. for ( let i = 0; i < this.count; i ++ ) {
  8174. const index = i * this.data.stride + this.offset;
  8175. for ( let j = 0; j < this.itemSize; j ++ ) {
  8176. array.push( this.data.array[ index + j ] );
  8177. }
  8178. }
  8179. return new BufferAttribute( new this.array.constructor( array ), this.itemSize, this.normalized );
  8180. } else {
  8181. if ( data.interleavedBuffers === undefined ) {
  8182. data.interleavedBuffers = {};
  8183. }
  8184. if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) {
  8185. data.interleavedBuffers[ this.data.uuid ] = this.data.clone( data );
  8186. }
  8187. return new InterleavedBufferAttribute( data.interleavedBuffers[ this.data.uuid ], this.itemSize, this.offset, this.normalized );
  8188. }
  8189. }
  8190. toJSON( data ) {
  8191. if ( data === undefined ) {
  8192. console.log( 'THREE.InterleavedBufferAttribute.toJSON(): Serializing an interleaved buffer attribute will de-interleave buffer data.' );
  8193. const array = [];
  8194. for ( let i = 0; i < this.count; i ++ ) {
  8195. const index = i * this.data.stride + this.offset;
  8196. for ( let j = 0; j < this.itemSize; j ++ ) {
  8197. array.push( this.data.array[ index + j ] );
  8198. }
  8199. }
  8200. // de-interleave data and save it as an ordinary buffer attribute for now
  8201. return {
  8202. itemSize: this.itemSize,
  8203. type: this.array.constructor.name,
  8204. array: array,
  8205. normalized: this.normalized
  8206. };
  8207. } else {
  8208. // save as true interleaved attribute
  8209. if ( data.interleavedBuffers === undefined ) {
  8210. data.interleavedBuffers = {};
  8211. }
  8212. if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) {
  8213. data.interleavedBuffers[ this.data.uuid ] = this.data.toJSON( data );
  8214. }
  8215. return {
  8216. isInterleavedBufferAttribute: true,
  8217. itemSize: this.itemSize,
  8218. data: this.data.uuid,
  8219. offset: this.offset,
  8220. normalized: this.normalized
  8221. };
  8222. }
  8223. }
  8224. }
  8225. class SpriteMaterial extends Material {
  8226. constructor( parameters ) {
  8227. super();
  8228. this.isSpriteMaterial = true;
  8229. this.type = 'SpriteMaterial';
  8230. this.color = new Color( 0xffffff );
  8231. this.map = null;
  8232. this.alphaMap = null;
  8233. this.rotation = 0;
  8234. this.sizeAttenuation = true;
  8235. this.transparent = true;
  8236. this.fog = true;
  8237. this.setValues( parameters );
  8238. }
  8239. copy( source ) {
  8240. super.copy( source );
  8241. this.color.copy( source.color );
  8242. this.map = source.map;
  8243. this.alphaMap = source.alphaMap;
  8244. this.rotation = source.rotation;
  8245. this.sizeAttenuation = source.sizeAttenuation;
  8246. this.fog = source.fog;
  8247. return this;
  8248. }
  8249. }
  8250. let _geometry;
  8251. const _intersectPoint = /*@__PURE__*/ new Vector3();
  8252. const _worldScale = /*@__PURE__*/ new Vector3();
  8253. const _mvPosition = /*@__PURE__*/ new Vector3();
  8254. const _alignedPosition = /*@__PURE__*/ new Vector2();
  8255. const _rotatedPosition = /*@__PURE__*/ new Vector2();
  8256. const _viewWorldMatrix = /*@__PURE__*/ new Matrix4();
  8257. const _vA = /*@__PURE__*/ new Vector3();
  8258. const _vB = /*@__PURE__*/ new Vector3();
  8259. const _vC = /*@__PURE__*/ new Vector3();
  8260. const _uvA = /*@__PURE__*/ new Vector2();
  8261. const _uvB = /*@__PURE__*/ new Vector2();
  8262. const _uvC = /*@__PURE__*/ new Vector2();
  8263. class Sprite extends Object3D {
  8264. constructor( material = new SpriteMaterial() ) {
  8265. super();
  8266. this.isSprite = true;
  8267. this.type = 'Sprite';
  8268. if ( _geometry === undefined ) {
  8269. _geometry = new BufferGeometry();
  8270. const float32Array = new Float32Array( [
  8271. - 0.5, - 0.5, 0, 0, 0,
  8272. 0.5, - 0.5, 0, 1, 0,
  8273. 0.5, 0.5, 0, 1, 1,
  8274. - 0.5, 0.5, 0, 0, 1
  8275. ] );
  8276. const interleavedBuffer = new InterleavedBuffer( float32Array, 5 );
  8277. _geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] );
  8278. _geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) );
  8279. _geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) );
  8280. }
  8281. this.geometry = _geometry;
  8282. this.material = material;
  8283. this.center = new Vector2( 0.5, 0.5 );
  8284. }
  8285. raycast( raycaster, intersects ) {
  8286. if ( raycaster.camera === null ) {
  8287. console.error( 'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.' );
  8288. }
  8289. _worldScale.setFromMatrixScale( this.matrixWorld );
  8290. _viewWorldMatrix.copy( raycaster.camera.matrixWorld );
  8291. this.modelViewMatrix.multiplyMatrices( raycaster.camera.matrixWorldInverse, this.matrixWorld );
  8292. _mvPosition.setFromMatrixPosition( this.modelViewMatrix );
  8293. if ( raycaster.camera.isPerspectiveCamera && this.material.sizeAttenuation === false ) {
  8294. _worldScale.multiplyScalar( - _mvPosition.z );
  8295. }
  8296. const rotation = this.material.rotation;
  8297. let sin, cos;
  8298. if ( rotation !== 0 ) {
  8299. cos = Math.cos( rotation );
  8300. sin = Math.sin( rotation );
  8301. }
  8302. const center = this.center;
  8303. transformVertex( _vA.set( - 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
  8304. transformVertex( _vB.set( 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
  8305. transformVertex( _vC.set( 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
  8306. _uvA.set( 0, 0 );
  8307. _uvB.set( 1, 0 );
  8308. _uvC.set( 1, 1 );
  8309. // check first triangle
  8310. let intersect = raycaster.ray.intersectTriangle( _vA, _vB, _vC, false, _intersectPoint );
  8311. if ( intersect === null ) {
  8312. // check second triangle
  8313. transformVertex( _vB.set( - 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos );
  8314. _uvB.set( 0, 1 );
  8315. intersect = raycaster.ray.intersectTriangle( _vA, _vC, _vB, false, _intersectPoint );
  8316. if ( intersect === null ) {
  8317. return;
  8318. }
  8319. }
  8320. const distance = raycaster.ray.origin.distanceTo( _intersectPoint );
  8321. if ( distance < raycaster.near || distance > raycaster.far ) return;
  8322. intersects.push( {
  8323. distance: distance,
  8324. point: _intersectPoint.clone(),
  8325. uv: Triangle.getInterpolation( _intersectPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() ),
  8326. face: null,
  8327. object: this
  8328. } );
  8329. }
  8330. copy( source, recursive ) {
  8331. super.copy( source, recursive );
  8332. if ( source.center !== undefined ) this.center.copy( source.center );
  8333. this.material = source.material;
  8334. return this;
  8335. }
  8336. }
  8337. function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) {
  8338. // compute position in camera space
  8339. _alignedPosition.subVectors( vertexPosition, center ).addScalar( 0.5 ).multiply( scale );
  8340. // to check if rotation is not zero
  8341. if ( sin !== undefined ) {
  8342. _rotatedPosition.x = ( cos * _alignedPosition.x ) - ( sin * _alignedPosition.y );
  8343. _rotatedPosition.y = ( sin * _alignedPosition.x ) + ( cos * _alignedPosition.y );
  8344. } else {
  8345. _rotatedPosition.copy( _alignedPosition );
  8346. }
  8347. vertexPosition.copy( mvPosition );
  8348. vertexPosition.x += _rotatedPosition.x;
  8349. vertexPosition.y += _rotatedPosition.y;
  8350. // transform to world space
  8351. vertexPosition.applyMatrix4( _viewWorldMatrix );
  8352. }
  8353. const _v1$2 = /*@__PURE__*/ new Vector3();
  8354. const _v2$1 = /*@__PURE__*/ new Vector3();
  8355. class LOD extends Object3D {
  8356. constructor() {
  8357. super();
  8358. this._currentLevel = 0;
  8359. this.type = 'LOD';
  8360. Object.defineProperties( this, {
  8361. levels: {
  8362. enumerable: true,
  8363. value: []
  8364. },
  8365. isLOD: {
  8366. value: true,
  8367. }
  8368. } );
  8369. this.autoUpdate = true;
  8370. }
  8371. copy( source ) {
  8372. super.copy( source, false );
  8373. const levels = source.levels;
  8374. for ( let i = 0, l = levels.length; i < l; i ++ ) {
  8375. const level = levels[ i ];
  8376. this.addLevel( level.object.clone(), level.distance, level.hysteresis );
  8377. }
  8378. this.autoUpdate = source.autoUpdate;
  8379. return this;
  8380. }
  8381. addLevel( object, distance = 0, hysteresis = 0 ) {
  8382. distance = Math.abs( distance );
  8383. const levels = this.levels;
  8384. let l;
  8385. for ( l = 0; l < levels.length; l ++ ) {
  8386. if ( distance < levels[ l ].distance ) {
  8387. break;
  8388. }
  8389. }
  8390. levels.splice( l, 0, { distance: distance, hysteresis: hysteresis, object: object } );
  8391. this.add( object );
  8392. return this;
  8393. }
  8394. removeLevel( distance ) {
  8395. const levels = this.levels;
  8396. for ( let i = 0; i < levels.length; i ++ ) {
  8397. if ( levels[ i ].distance === distance ) {
  8398. const removedElements = levels.splice( i, 1 );
  8399. this.remove( removedElements[ 0 ].object );
  8400. return true;
  8401. }
  8402. }
  8403. return false;
  8404. }
  8405. getCurrentLevel() {
  8406. return this._currentLevel;
  8407. }
  8408. getObjectForDistance( distance ) {
  8409. const levels = this.levels;
  8410. if ( levels.length > 0 ) {
  8411. let i, l;
  8412. for ( i = 1, l = levels.length; i < l; i ++ ) {
  8413. let levelDistance = levels[ i ].distance;
  8414. if ( levels[ i ].object.visible ) {
  8415. levelDistance -= levelDistance * levels[ i ].hysteresis;
  8416. }
  8417. if ( distance < levelDistance ) {
  8418. break;
  8419. }
  8420. }
  8421. return levels[ i - 1 ].object;
  8422. }
  8423. return null;
  8424. }
  8425. raycast( raycaster, intersects ) {
  8426. const levels = this.levels;
  8427. if ( levels.length > 0 ) {
  8428. _v1$2.setFromMatrixPosition( this.matrixWorld );
  8429. const distance = raycaster.ray.origin.distanceTo( _v1$2 );
  8430. this.getObjectForDistance( distance ).raycast( raycaster, intersects );
  8431. }
  8432. }
  8433. update( camera ) {
  8434. const levels = this.levels;
  8435. if ( levels.length > 1 ) {
  8436. _v1$2.setFromMatrixPosition( camera.matrixWorld );
  8437. _v2$1.setFromMatrixPosition( this.matrixWorld );
  8438. const distance = _v1$2.distanceTo( _v2$1 ) / camera.zoom;
  8439. levels[ 0 ].object.visible = true;
  8440. let i, l;
  8441. for ( i = 1, l = levels.length; i < l; i ++ ) {
  8442. let levelDistance = levels[ i ].distance;
  8443. if ( levels[ i ].object.visible ) {
  8444. levelDistance -= levelDistance * levels[ i ].hysteresis;
  8445. }
  8446. if ( distance >= levelDistance ) {
  8447. levels[ i - 1 ].object.visible = false;
  8448. levels[ i ].object.visible = true;
  8449. } else {
  8450. break;
  8451. }
  8452. }
  8453. this._currentLevel = i - 1;
  8454. for ( ; i < l; i ++ ) {
  8455. levels[ i ].object.visible = false;
  8456. }
  8457. }
  8458. }
  8459. toJSON( meta ) {
  8460. const data = super.toJSON( meta );
  8461. if ( this.autoUpdate === false ) data.object.autoUpdate = false;
  8462. data.object.levels = [];
  8463. const levels = this.levels;
  8464. for ( let i = 0, l = levels.length; i < l; i ++ ) {
  8465. const level = levels[ i ];
  8466. data.object.levels.push( {
  8467. object: level.object.uuid,
  8468. distance: level.distance,
  8469. hysteresis: level.hysteresis
  8470. } );
  8471. }
  8472. return data;
  8473. }
  8474. }
  8475. const _basePosition = /*@__PURE__*/ new Vector3();
  8476. const _skinIndex = /*@__PURE__*/ new Vector4();
  8477. const _skinWeight = /*@__PURE__*/ new Vector4();
  8478. const _vector3 = /*@__PURE__*/ new Vector3();
  8479. const _matrix4 = /*@__PURE__*/ new Matrix4();
  8480. const _vertex = /*@__PURE__*/ new Vector3();
  8481. const _sphere$5 = /*@__PURE__*/ new Sphere();
  8482. const _inverseMatrix$2 = /*@__PURE__*/ new Matrix4();
  8483. const _ray$2 = /*@__PURE__*/ new Ray();
  8484. class SkinnedMesh extends Mesh {
  8485. constructor( geometry, material ) {
  8486. super( geometry, material );
  8487. this.isSkinnedMesh = true;
  8488. this.type = 'SkinnedMesh';
  8489. this.bindMode = AttachedBindMode;
  8490. this.bindMatrix = new Matrix4();
  8491. this.bindMatrixInverse = new Matrix4();
  8492. this.boundingBox = null;
  8493. this.boundingSphere = null;
  8494. }
  8495. computeBoundingBox() {
  8496. const geometry = this.geometry;
  8497. if ( this.boundingBox === null ) {
  8498. this.boundingBox = new Box3();
  8499. }
  8500. this.boundingBox.makeEmpty();
  8501. const positionAttribute = geometry.getAttribute( 'position' );
  8502. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  8503. this.getVertexPosition( i, _vertex );
  8504. this.boundingBox.expandByPoint( _vertex );
  8505. }
  8506. }
  8507. computeBoundingSphere() {
  8508. const geometry = this.geometry;
  8509. if ( this.boundingSphere === null ) {
  8510. this.boundingSphere = new Sphere();
  8511. }
  8512. this.boundingSphere.makeEmpty();
  8513. const positionAttribute = geometry.getAttribute( 'position' );
  8514. for ( let i = 0; i < positionAttribute.count; i ++ ) {
  8515. this.getVertexPosition( i, _vertex );
  8516. this.boundingSphere.expandByPoint( _vertex );
  8517. }
  8518. }
  8519. copy( source, recursive ) {
  8520. super.copy( source, recursive );
  8521. this.bindMode = source.bindMode;
  8522. this.bindMatrix.copy( source.bindMatrix );
  8523. this.bindMatrixInverse.copy( source.bindMatrixInverse );
  8524. this.skeleton = source.skeleton;
  8525. if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone();
  8526. if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone();
  8527. return this;
  8528. }
  8529. raycast( raycaster, intersects ) {
  8530. const material = this.material;
  8531. const matrixWorld = this.matrixWorld;
  8532. if ( material === undefined ) return;
  8533. // test with bounding sphere in world space
  8534. if ( this.boundingSphere === null ) this.computeBoundingSphere();
  8535. _sphere$5.copy( this.boundingSphere );
  8536. _sphere$5.applyMatrix4( matrixWorld );
  8537. if ( raycaster.ray.intersectsSphere( _sphere$5 ) === false ) return;
  8538. // convert ray to local space of skinned mesh
  8539. _inverseMatrix$2.copy( matrixWorld ).invert();
  8540. _ray$2.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$2 );
  8541. // test with bounding box in local space
  8542. if ( this.boundingBox !== null ) {
  8543. if ( _ray$2.intersectsBox( this.boundingBox ) === false ) return;
  8544. }
  8545. // test for intersections with geometry
  8546. this._computeIntersections( raycaster, intersects, _ray$2 );
  8547. }
  8548. getVertexPosition( index, target ) {
  8549. super.getVertexPosition( index, target );
  8550. this.applyBoneTransform( index, target );
  8551. return target;
  8552. }
  8553. bind( skeleton, bindMatrix ) {
  8554. this.skeleton = skeleton;
  8555. if ( bindMatrix === undefined ) {
  8556. this.updateMatrixWorld( true );
  8557. this.skeleton.calculateInverses();
  8558. bindMatrix = this.matrixWorld;
  8559. }
  8560. this.bindMatrix.copy( bindMatrix );
  8561. this.bindMatrixInverse.copy( bindMatrix ).invert();
  8562. }
  8563. pose() {
  8564. this.skeleton.pose();
  8565. }
  8566. normalizeSkinWeights() {
  8567. const vector = new Vector4();
  8568. const skinWeight = this.geometry.attributes.skinWeight;
  8569. for ( let i = 0, l = skinWeight.count; i < l; i ++ ) {
  8570. vector.fromBufferAttribute( skinWeight, i );
  8571. const scale = 1.0 / vector.manhattanLength();
  8572. if ( scale !== Infinity ) {
  8573. vector.multiplyScalar( scale );
  8574. } else {
  8575. vector.set( 1, 0, 0, 0 ); // do something reasonable
  8576. }
  8577. skinWeight.setXYZW( i, vector.x, vector.y, vector.z, vector.w );
  8578. }
  8579. }
  8580. updateMatrixWorld( force ) {
  8581. super.updateMatrixWorld( force );
  8582. if ( this.bindMode === AttachedBindMode ) {
  8583. this.bindMatrixInverse.copy( this.matrixWorld ).invert();
  8584. } else if ( this.bindMode === DetachedBindMode ) {
  8585. this.bindMatrixInverse.copy( this.bindMatrix ).invert();
  8586. } else {
  8587. console.warn( 'THREE.SkinnedMesh: Unrecognized bindMode: ' + this.bindMode );
  8588. }
  8589. }
  8590. applyBoneTransform( index, vector ) {
  8591. const skeleton = this.skeleton;
  8592. const geometry = this.geometry;
  8593. _skinIndex.fromBufferAttribute( geometry.attributes.skinIndex, index );
  8594. _skinWeight.fromBufferAttribute( geometry.attributes.skinWeight, index );
  8595. _basePosition.copy( vector ).applyMatrix4( this.bindMatrix );
  8596. vector.set( 0, 0, 0 );
  8597. for ( let i = 0; i < 4; i ++ ) {
  8598. const weight = _skinWeight.getComponent( i );
  8599. if ( weight !== 0 ) {
  8600. const boneIndex = _skinIndex.getComponent( i );
  8601. _matrix4.multiplyMatrices( skeleton.bones[ boneIndex ].matrixWorld, skeleton.boneInverses[ boneIndex ] );
  8602. vector.addScaledVector( _vector3.copy( _basePosition ).applyMatrix4( _matrix4 ), weight );
  8603. }
  8604. }
  8605. return vector.applyMatrix4( this.bindMatrixInverse );
  8606. }
  8607. }
  8608. class Bone extends Object3D {
  8609. constructor() {
  8610. super();
  8611. this.isBone = true;
  8612. this.type = 'Bone';
  8613. }
  8614. }
  8615. class DataTexture extends Texture {
  8616. constructor( data = null, width = 1, height = 1, format, type, mapping, wrapS, wrapT, magFilter = NearestFilter, minFilter = NearestFilter, anisotropy, colorSpace ) {
  8617. super( null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace );
  8618. this.isDataTexture = true;
  8619. this.image = { data: data, width: width, height: height };
  8620. this.generateMipmaps = false;
  8621. this.flipY = false;
  8622. this.unpackAlignment = 1;
  8623. }
  8624. }
  8625. const _offsetMatrix = /*@__PURE__*/ new Matrix4();
  8626. const _identityMatrix = /*@__PURE__*/ new Matrix4();
  8627. class Skeleton {
  8628. constructor( bones = [], boneInverses = [] ) {
  8629. this.uuid = generateUUID();
  8630. this.bones = bones.slice( 0 );
  8631. this.boneInverses = boneInverses;
  8632. this.boneMatrices = null;
  8633. this.boneTexture = null;
  8634. this.init();
  8635. }
  8636. init() {
  8637. const bones = this.bones;
  8638. const boneInverses = this.boneInverses;
  8639. this.boneMatrices = new Float32Array( bones.length * 16 );
  8640. // calculate inverse bone matrices if necessary
  8641. if ( boneInverses.length === 0 ) {
  8642. this.calculateInverses();
  8643. } else {
  8644. // handle special case
  8645. if ( bones.length !== boneInverses.length ) {
  8646. console.warn( 'THREE.Skeleton: Number of inverse bone matrices does not match amount of bones.' );
  8647. this.boneInverses = [];
  8648. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  8649. this.boneInverses.push( new Matrix4() );
  8650. }
  8651. }
  8652. }
  8653. }
  8654. calculateInverses() {
  8655. this.boneInverses.length = 0;
  8656. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  8657. const inverse = new Matrix4();
  8658. if ( this.bones[ i ] ) {
  8659. inverse.copy( this.bones[ i ].matrixWorld ).invert();
  8660. }
  8661. this.boneInverses.push( inverse );
  8662. }
  8663. }
  8664. pose() {
  8665. // recover the bind-time world matrices
  8666. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  8667. const bone = this.bones[ i ];
  8668. if ( bone ) {
  8669. bone.matrixWorld.copy( this.boneInverses[ i ] ).invert();
  8670. }
  8671. }
  8672. // compute the local matrices, positions, rotations and scales
  8673. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  8674. const bone = this.bones[ i ];
  8675. if ( bone ) {
  8676. if ( bone.parent && bone.parent.isBone ) {
  8677. bone.matrix.copy( bone.parent.matrixWorld ).invert();
  8678. bone.matrix.multiply( bone.matrixWorld );
  8679. } else {
  8680. bone.matrix.copy( bone.matrixWorld );
  8681. }
  8682. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  8683. }
  8684. }
  8685. }
  8686. update() {
  8687. const bones = this.bones;
  8688. const boneInverses = this.boneInverses;
  8689. const boneMatrices = this.boneMatrices;
  8690. const boneTexture = this.boneTexture;
  8691. // flatten bone matrices to array
  8692. for ( let i = 0, il = bones.length; i < il; i ++ ) {
  8693. // compute the offset between the current and the original transform
  8694. const matrix = bones[ i ] ? bones[ i ].matrixWorld : _identityMatrix;
  8695. _offsetMatrix.multiplyMatrices( matrix, boneInverses[ i ] );
  8696. _offsetMatrix.toArray( boneMatrices, i * 16 );
  8697. }
  8698. if ( boneTexture !== null ) {
  8699. boneTexture.needsUpdate = true;
  8700. }
  8701. }
  8702. clone() {
  8703. return new Skeleton( this.bones, this.boneInverses );
  8704. }
  8705. computeBoneTexture() {
  8706. // layout (1 matrix = 4 pixels)
  8707. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  8708. // with 8x8 pixel texture max 16 bones * 4 pixels = (8 * 8)
  8709. // 16x16 pixel texture max 64 bones * 4 pixels = (16 * 16)
  8710. // 32x32 pixel texture max 256 bones * 4 pixels = (32 * 32)
  8711. // 64x64 pixel texture max 1024 bones * 4 pixels = (64 * 64)
  8712. let size = Math.sqrt( this.bones.length * 4 ); // 4 pixels needed for 1 matrix
  8713. size = Math.ceil( size / 4 ) * 4;
  8714. size = Math.max( size, 4 );
  8715. const boneMatrices = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
  8716. boneMatrices.set( this.boneMatrices ); // copy current values
  8717. const boneTexture = new DataTexture( boneMatrices, size, size, RGBAFormat, FloatType );
  8718. boneTexture.needsUpdate = true;
  8719. this.boneMatrices = boneMatrices;
  8720. this.boneTexture = boneTexture;
  8721. return this;
  8722. }
  8723. getBoneByName( name ) {
  8724. for ( let i = 0, il = this.bones.length; i < il; i ++ ) {
  8725. const bone = this.bones[ i ];
  8726. if ( bone.name === name ) {
  8727. return bone;
  8728. }
  8729. }
  8730. return undefined;
  8731. }
  8732. dispose( ) {
  8733. if ( this.boneTexture !== null ) {
  8734. this.boneTexture.dispose();
  8735. this.boneTexture = null;
  8736. }
  8737. }
  8738. fromJSON( json, bones ) {
  8739. this.uuid = json.uuid;
  8740. for ( let i = 0, l = json.bones.length; i < l; i ++ ) {
  8741. const uuid = json.bones[ i ];
  8742. let bone = bones[ uuid ];
  8743. if ( bone === undefined ) {
  8744. console.warn( 'THREE.Skeleton: No bone found with UUID:', uuid );
  8745. bone = new Bone();
  8746. }
  8747. this.bones.push( bone );
  8748. this.boneInverses.push( new Matrix4().fromArray( json.boneInverses[ i ] ) );
  8749. }
  8750. this.init();
  8751. return this;
  8752. }
  8753. toJSON() {
  8754. const data = {
  8755. metadata: {
  8756. version: 4.6,
  8757. type: 'Skeleton',
  8758. generator: 'Skeleton.toJSON'
  8759. },
  8760. bones: [],
  8761. boneInverses: []
  8762. };
  8763. data.uuid = this.uuid;
  8764. const bones = this.bones;
  8765. const boneInverses = this.boneInverses;
  8766. for ( let i = 0, l = bones.length; i < l; i ++ ) {
  8767. const bone = bones[ i ];
  8768. data.bones.push( bone.uuid );
  8769. const boneInverse = boneInverses[ i ];
  8770. data.boneInverses.push( boneInverse.toArray() );
  8771. }
  8772. return data;
  8773. }
  8774. }
  8775. class InstancedBufferAttribute extends BufferAttribute {
  8776. constructor( array, itemSize, normalized, meshPerAttribute = 1 ) {
  8777. super( array, itemSize, normalized );
  8778. this.isInstancedBufferAttribute = true;
  8779. this.meshPerAttribute = meshPerAttribute;
  8780. }
  8781. copy( source ) {
  8782. super.copy( source );
  8783. this.meshPerAttribute = source.meshPerAttribute;
  8784. return this;
  8785. }
  8786. toJSON() {
  8787. const data = super.toJSON();
  8788. data.meshPerAttribute = this.meshPerAttribute;
  8789. data.isInstancedBufferAttribute = true;
  8790. return data;
  8791. }
  8792. }
  8793. const _instanceLocalMatrix = /*@__PURE__*/ new Matrix4();
  8794. const _instanceWorldMatrix = /*@__PURE__*/ new Matrix4();
  8795. const _instanceIntersects = [];
  8796. const _box3 = /*@__PURE__*/ new Box3();
  8797. const _identity = /*@__PURE__*/ new Matrix4();
  8798. const _mesh$1 = /*@__PURE__*/ new Mesh();
  8799. const _sphere$4 = /*@__PURE__*/ new Sphere();
  8800. class InstancedMesh extends Mesh {
  8801. constructor( geometry, material, count ) {
  8802. super( geometry, material );
  8803. this.isInstancedMesh = true;
  8804. this.instanceMatrix = new InstancedBufferAttribute( new Float32Array( count * 16 ), 16 );
  8805. this.instanceColor = null;
  8806. this.morphTexture = null;
  8807. this.count = count;
  8808. this.boundingBox = null;
  8809. this.boundingSphere = null;
  8810. for ( let i = 0; i < count; i ++ ) {
  8811. this.setMatrixAt( i, _identity );
  8812. }
  8813. }
  8814. computeBoundingBox() {
  8815. const geometry = this.geometry;
  8816. const count = this.count;
  8817. if ( this.boundingBox === null ) {
  8818. this.boundingBox = new Box3();
  8819. }
  8820. if ( geometry.boundingBox === null ) {
  8821. geometry.computeBoundingBox();
  8822. }
  8823. this.boundingBox.makeEmpty();
  8824. for ( let i = 0; i < count; i ++ ) {
  8825. this.getMatrixAt( i, _instanceLocalMatrix );
  8826. _box3.copy( geometry.boundingBox ).applyMatrix4( _instanceLocalMatrix );
  8827. this.boundingBox.union( _box3 );
  8828. }
  8829. }
  8830. computeBoundingSphere() {
  8831. const geometry = this.geometry;
  8832. const count = this.count;
  8833. if ( this.boundingSphere === null ) {
  8834. this.boundingSphere = new Sphere();
  8835. }
  8836. if ( geometry.boundingSphere === null ) {
  8837. geometry.computeBoundingSphere();
  8838. }
  8839. this.boundingSphere.makeEmpty();
  8840. for ( let i = 0; i < count; i ++ ) {
  8841. this.getMatrixAt( i, _instanceLocalMatrix );
  8842. _sphere$4.copy( geometry.boundingSphere ).applyMatrix4( _instanceLocalMatrix );
  8843. this.boundingSphere.union( _sphere$4 );
  8844. }
  8845. }
  8846. copy( source, recursive ) {
  8847. super.copy( source, recursive );
  8848. this.instanceMatrix.copy( source.instanceMatrix );
  8849. if ( source.morphTexture !== null ) this.morphTexture = source.morphTexture.clone();
  8850. if ( source.instanceColor !== null ) this.instanceColor = source.instanceColor.clone();
  8851. this.count = source.count;
  8852. if ( source.boundingBox !== null ) this.boundingBox = source.boundingBox.clone();
  8853. if ( source.boundingSphere !== null ) this.boundingSphere = source.boundingSphere.clone();
  8854. return this;
  8855. }
  8856. getColorAt( index, color ) {
  8857. color.fromArray( this.instanceColor.array, index * 3 );
  8858. }
  8859. getMatrixAt( index, matrix ) {
  8860. matrix.fromArray( this.instanceMatrix.array, index * 16 );
  8861. }
  8862. getMorphAt( index, object ) {
  8863. const objectInfluences = object.morphTargetInfluences;
  8864. const array = this.morphTexture.source.data.data;
  8865. const len = objectInfluences.length + 1; // All influences + the baseInfluenceSum
  8866. const dataIndex = index * len + 1; // Skip the baseInfluenceSum at the beginning
  8867. for ( let i = 0; i < objectInfluences.length; i ++ ) {
  8868. objectInfluences[ i ] = array[ dataIndex + i ];
  8869. }
  8870. }
  8871. raycast( raycaster, intersects ) {
  8872. const matrixWorld = this.matrixWorld;
  8873. const raycastTimes = this.count;
  8874. _mesh$1.geometry = this.geometry;
  8875. _mesh$1.material = this.material;
  8876. if ( _mesh$1.material === undefined ) return;
  8877. // test with bounding sphere first
  8878. if ( this.boundingSphere === null ) this.computeBoundingSphere();
  8879. _sphere$4.copy( this.boundingSphere );
  8880. _sphere$4.applyMatrix4( matrixWorld );
  8881. if ( raycaster.ray.intersectsSphere( _sphere$4 ) === false ) return;
  8882. // now test each instance
  8883. for ( let instanceId = 0; instanceId < raycastTimes; instanceId ++ ) {
  8884. // calculate the world matrix for each instance
  8885. this.getMatrixAt( instanceId, _instanceLocalMatrix );
  8886. _instanceWorldMatrix.multiplyMatrices( matrixWorld, _instanceLocalMatrix );
  8887. // the mesh represents this single instance
  8888. _mesh$1.matrixWorld = _instanceWorldMatrix;
  8889. _mesh$1.raycast( raycaster, _instanceIntersects );
  8890. // process the result of raycast
  8891. for ( let i = 0, l = _instanceIntersects.length; i < l; i ++ ) {
  8892. const intersect = _instanceIntersects[ i ];
  8893. intersect.instanceId = instanceId;
  8894. intersect.object = this;
  8895. intersects.push( intersect );
  8896. }
  8897. _instanceIntersects.length = 0;
  8898. }
  8899. }
  8900. setColorAt( index, color ) {
  8901. if ( this.instanceColor === null ) {
  8902. this.instanceColor = new InstancedBufferAttribute( new Float32Array( this.instanceMatrix.count * 3 ).fill( 1 ), 3 );
  8903. }
  8904. color.toArray( this.instanceColor.array, index * 3 );
  8905. }
  8906. setMatrixAt( index, matrix ) {
  8907. matrix.toArray( this.instanceMatrix.array, index * 16 );
  8908. }
  8909. setMorphAt( index, object ) {
  8910. const objectInfluences = object.morphTargetInfluences;
  8911. const len = objectInfluences.length + 1; // morphBaseInfluence + all influences
  8912. if ( this.morphTexture === null ) {
  8913. this.morphTexture = new DataTexture( new Float32Array( len * this.count ), len, this.count, RedFormat, FloatType );
  8914. }
  8915. const array = this.morphTexture.source.data.data;
  8916. let morphInfluencesSum = 0;
  8917. for ( let i = 0; i < objectInfluences.length; i ++ ) {
  8918. morphInfluencesSum += objectInfluences[ i ];
  8919. }
  8920. const morphBaseInfluence = this.geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum;
  8921. const dataIndex = len * index;
  8922. array[ dataIndex ] = morphBaseInfluence;
  8923. array.set( objectInfluences, dataIndex + 1 );
  8924. }
  8925. updateMorphTargets() {
  8926. }
  8927. dispose() {
  8928. this.dispatchEvent( { type: 'dispose' } );
  8929. if ( this.morphTexture !== null ) {
  8930. this.morphTexture.dispose();
  8931. this.morphTexture = null;
  8932. }
  8933. return this;
  8934. }
  8935. }
  8936. const _vector1 = /*@__PURE__*/ new Vector3();
  8937. const _vector2 = /*@__PURE__*/ new Vector3();
  8938. const _normalMatrix = /*@__PURE__*/ new Matrix3();
  8939. class Plane {
  8940. constructor( normal = new Vector3( 1, 0, 0 ), constant = 0 ) {
  8941. this.isPlane = true;
  8942. // normal is assumed to be normalized
  8943. this.normal = normal;
  8944. this.constant = constant;
  8945. }
  8946. set( normal, constant ) {
  8947. this.normal.copy( normal );
  8948. this.constant = constant;
  8949. return this;
  8950. }
  8951. setComponents( x, y, z, w ) {
  8952. this.normal.set( x, y, z );
  8953. this.constant = w;
  8954. return this;
  8955. }
  8956. setFromNormalAndCoplanarPoint( normal, point ) {
  8957. this.normal.copy( normal );
  8958. this.constant = - point.dot( this.normal );
  8959. return this;
  8960. }
  8961. setFromCoplanarPoints( a, b, c ) {
  8962. const normal = _vector1.subVectors( c, b ).cross( _vector2.subVectors( a, b ) ).normalize();
  8963. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  8964. this.setFromNormalAndCoplanarPoint( normal, a );
  8965. return this;
  8966. }
  8967. copy( plane ) {
  8968. this.normal.copy( plane.normal );
  8969. this.constant = plane.constant;
  8970. return this;
  8971. }
  8972. normalize() {
  8973. // Note: will lead to a divide by zero if the plane is invalid.
  8974. const inverseNormalLength = 1.0 / this.normal.length();
  8975. this.normal.multiplyScalar( inverseNormalLength );
  8976. this.constant *= inverseNormalLength;
  8977. return this;
  8978. }
  8979. negate() {
  8980. this.constant *= - 1;
  8981. this.normal.negate();
  8982. return this;
  8983. }
  8984. distanceToPoint( point ) {
  8985. return this.normal.dot( point ) + this.constant;
  8986. }
  8987. distanceToSphere( sphere ) {
  8988. return this.distanceToPoint( sphere.center ) - sphere.radius;
  8989. }
  8990. projectPoint( point, target ) {
  8991. return target.copy( point ).addScaledVector( this.normal, - this.distanceToPoint( point ) );
  8992. }
  8993. intersectLine( line, target ) {
  8994. const direction = line.delta( _vector1 );
  8995. const denominator = this.normal.dot( direction );
  8996. if ( denominator === 0 ) {
  8997. // line is coplanar, return origin
  8998. if ( this.distanceToPoint( line.start ) === 0 ) {
  8999. return target.copy( line.start );
  9000. }
  9001. // Unsure if this is the correct method to handle this case.
  9002. return null;
  9003. }
  9004. const t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
  9005. if ( t < 0 || t > 1 ) {
  9006. return null;
  9007. }
  9008. return target.copy( line.start ).addScaledVector( direction, t );
  9009. }
  9010. intersectsLine( line ) {
  9011. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  9012. const startSign = this.distanceToPoint( line.start );
  9013. const endSign = this.distanceToPoint( line.end );
  9014. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  9015. }
  9016. intersectsBox( box ) {
  9017. return box.intersectsPlane( this );
  9018. }
  9019. intersectsSphere( sphere ) {
  9020. return sphere.intersectsPlane( this );
  9021. }
  9022. coplanarPoint( target ) {
  9023. return target.copy( this.normal ).multiplyScalar( - this.constant );
  9024. }
  9025. applyMatrix4( matrix, optionalNormalMatrix ) {
  9026. const normalMatrix = optionalNormalMatrix || _normalMatrix.getNormalMatrix( matrix );
  9027. const referencePoint = this.coplanarPoint( _vector1 ).applyMatrix4( matrix );
  9028. const normal = this.normal.applyMatrix3( normalMatrix ).normalize();
  9029. this.constant = - referencePoint.dot( normal );
  9030. return this;
  9031. }
  9032. translate( offset ) {
  9033. this.constant -= offset.dot( this.normal );
  9034. return this;
  9035. }
  9036. equals( plane ) {
  9037. return plane.normal.equals( this.normal ) && ( plane.constant === this.constant );
  9038. }
  9039. clone() {
  9040. return new this.constructor().copy( this );
  9041. }
  9042. }
  9043. const _sphere$3 = /*@__PURE__*/ new Sphere();
  9044. const _vector$6 = /*@__PURE__*/ new Vector3();
  9045. class Frustum {
  9046. constructor( p0 = new Plane(), p1 = new Plane(), p2 = new Plane(), p3 = new Plane(), p4 = new Plane(), p5 = new Plane() ) {
  9047. this.planes = [ p0, p1, p2, p3, p4, p5 ];
  9048. }
  9049. set( p0, p1, p2, p3, p4, p5 ) {
  9050. const planes = this.planes;
  9051. planes[ 0 ].copy( p0 );
  9052. planes[ 1 ].copy( p1 );
  9053. planes[ 2 ].copy( p2 );
  9054. planes[ 3 ].copy( p3 );
  9055. planes[ 4 ].copy( p4 );
  9056. planes[ 5 ].copy( p5 );
  9057. return this;
  9058. }
  9059. copy( frustum ) {
  9060. const planes = this.planes;
  9061. for ( let i = 0; i < 6; i ++ ) {
  9062. planes[ i ].copy( frustum.planes[ i ] );
  9063. }
  9064. return this;
  9065. }
  9066. setFromProjectionMatrix( m, coordinateSystem = WebGLCoordinateSystem ) {
  9067. const planes = this.planes;
  9068. const me = m.elements;
  9069. const me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ];
  9070. const me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ];
  9071. const me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ];
  9072. const me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ];
  9073. planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize();
  9074. planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();
  9075. planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();
  9076. planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();
  9077. planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();
  9078. if ( coordinateSystem === WebGLCoordinateSystem ) {
  9079. planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize();
  9080. } else if ( coordinateSystem === WebGPUCoordinateSystem ) {
  9081. planes[ 5 ].setComponents( me2, me6, me10, me14 ).normalize();
  9082. } else {
  9083. throw new Error( 'THREE.Frustum.setFromProjectionMatrix(): Invalid coordinate system: ' + coordinateSystem );
  9084. }
  9085. return this;
  9086. }
  9087. intersectsObject( object ) {
  9088. if ( object.boundingSphere !== undefined ) {
  9089. if ( object.boundingSphere === null ) object.computeBoundingSphere();
  9090. _sphere$3.copy( object.boundingSphere ).applyMatrix4( object.matrixWorld );
  9091. } else {
  9092. const geometry = object.geometry;
  9093. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  9094. _sphere$3.copy( geometry.boundingSphere ).applyMatrix4( object.matrixWorld );
  9095. }
  9096. return this.intersectsSphere( _sphere$3 );
  9097. }
  9098. intersectsSprite( sprite ) {
  9099. _sphere$3.center.set( 0, 0, 0 );
  9100. _sphere$3.radius = 0.7071067811865476;
  9101. _sphere$3.applyMatrix4( sprite.matrixWorld );
  9102. return this.intersectsSphere( _sphere$3 );
  9103. }
  9104. intersectsSphere( sphere ) {
  9105. const planes = this.planes;
  9106. const center = sphere.center;
  9107. const negRadius = - sphere.radius;
  9108. for ( let i = 0; i < 6; i ++ ) {
  9109. const distance = planes[ i ].distanceToPoint( center );
  9110. if ( distance < negRadius ) {
  9111. return false;
  9112. }
  9113. }
  9114. return true;
  9115. }
  9116. intersectsBox( box ) {
  9117. const planes = this.planes;
  9118. for ( let i = 0; i < 6; i ++ ) {
  9119. const plane = planes[ i ];
  9120. // corner at max distance
  9121. _vector$6.x = plane.normal.x > 0 ? box.max.x : box.min.x;
  9122. _vector$6.y = plane.normal.y > 0 ? box.max.y : box.min.y;
  9123. _vector$6.z = plane.normal.z > 0 ? box.max.z : box.min.z;
  9124. if ( plane.distanceToPoint( _vector$6 ) < 0 ) {
  9125. return false;
  9126. }
  9127. }
  9128. return true;
  9129. }
  9130. containsPoint( point ) {
  9131. const planes = this.planes;
  9132. for ( let i = 0; i < 6; i ++ ) {
  9133. if ( planes[ i ].distanceToPoint( point ) < 0 ) {
  9134. return false;
  9135. }
  9136. }
  9137. return true;
  9138. }
  9139. clone() {
  9140. return new this.constructor().copy( this );
  9141. }
  9142. }
  9143. function ascIdSort( a, b ) {
  9144. return a - b;
  9145. }
  9146. function sortOpaque( a, b ) {
  9147. return a.z - b.z;
  9148. }
  9149. function sortTransparent( a, b ) {
  9150. return b.z - a.z;
  9151. }
  9152. class MultiDrawRenderList {
  9153. constructor() {
  9154. this.index = 0;
  9155. this.pool = [];
  9156. this.list = [];
  9157. }
  9158. push( start, count, z, index ) {
  9159. const pool = this.pool;
  9160. const list = this.list;
  9161. if ( this.index >= pool.length ) {
  9162. pool.push( {
  9163. start: - 1,
  9164. count: - 1,
  9165. z: - 1,
  9166. index: - 1,
  9167. } );
  9168. }
  9169. const item = pool[ this.index ];
  9170. list.push( item );
  9171. this.index ++;
  9172. item.start = start;
  9173. item.count = count;
  9174. item.z = z;
  9175. item.index = index;
  9176. }
  9177. reset() {
  9178. this.list.length = 0;
  9179. this.index = 0;
  9180. }
  9181. }
  9182. const _matrix$1 = /*@__PURE__*/ new Matrix4();
  9183. const _whiteColor = /*@__PURE__*/ new Color( 1, 1, 1 );
  9184. const _frustum = /*@__PURE__*/ new Frustum();
  9185. const _box$1 = /*@__PURE__*/ new Box3();
  9186. const _sphere$2 = /*@__PURE__*/ new Sphere();
  9187. const _vector$5 = /*@__PURE__*/ new Vector3();
  9188. const _forward = /*@__PURE__*/ new Vector3();
  9189. const _temp = /*@__PURE__*/ new Vector3();
  9190. const _renderList = /*@__PURE__*/ new MultiDrawRenderList();
  9191. const _mesh = /*@__PURE__*/ new Mesh();
  9192. const _batchIntersects = [];
  9193. // copies data from attribute "src" into "target" starting at "targetOffset"
  9194. function copyAttributeData( src, target, targetOffset = 0 ) {
  9195. const itemSize = target.itemSize;
  9196. if ( src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor ) {
  9197. // use the component getters and setters if the array data cannot
  9198. // be copied directly
  9199. const vertexCount = src.count;
  9200. for ( let i = 0; i < vertexCount; i ++ ) {
  9201. for ( let c = 0; c < itemSize; c ++ ) {
  9202. target.setComponent( i + targetOffset, c, src.getComponent( i, c ) );
  9203. }
  9204. }
  9205. } else {
  9206. // faster copy approach using typed array set function
  9207. target.array.set( src.array, targetOffset * itemSize );
  9208. }
  9209. target.needsUpdate = true;
  9210. }
  9211. // safely copies array contents to a potentially smaller array
  9212. function copyArrayContents( src, target ) {
  9213. if ( src.constructor !== target.constructor ) {
  9214. // if arrays are of a different type (eg due to index size increasing) then data must be per-element copied
  9215. const len = Math.min( src.length, target.length );
  9216. for ( let i = 0; i < len; i ++ ) {
  9217. target[ i ] = src[ i ];
  9218. }
  9219. } else {
  9220. // if the arrays use the same data layout we can use a fast block copy
  9221. const len = Math.min( src.length, target.length );
  9222. target.set( new src.constructor( src.buffer, 0, len ) );
  9223. }
  9224. }
  9225. class BatchedMesh extends Mesh {
  9226. get maxInstanceCount() {
  9227. return this._maxInstanceCount;
  9228. }
  9229. get instanceCount() {
  9230. return this._instanceInfo.length - this._availableInstanceIds.length;
  9231. }
  9232. get unusedVertexCount() {
  9233. return this._maxVertexCount - this._nextVertexStart;
  9234. }
  9235. get unusedIndexCount() {
  9236. return this._maxIndexCount - this._nextIndexStart;
  9237. }
  9238. constructor( maxInstanceCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material ) {
  9239. super( new BufferGeometry(), material );
  9240. this.isBatchedMesh = true;
  9241. this.perObjectFrustumCulled = true;
  9242. this.sortObjects = true;
  9243. this.boundingBox = null;
  9244. this.boundingSphere = null;
  9245. this.customSort = null;
  9246. // stores visible, active, and geometry id per instance and reserved buffer ranges for geometries
  9247. this._instanceInfo = [];
  9248. this._geometryInfo = [];
  9249. // instance, geometry ids that have been set as inactive, and are available to be overwritten
  9250. this._availableInstanceIds = [];
  9251. this._availableGeometryIds = [];
  9252. // used to track where the next point is that geometry should be inserted
  9253. this._nextIndexStart = 0;
  9254. this._nextVertexStart = 0;
  9255. this._geometryCount = 0;
  9256. // flags
  9257. this._visibilityChanged = true;
  9258. this._geometryInitialized = false;
  9259. // cached user options
  9260. this._maxInstanceCount = maxInstanceCount;
  9261. this._maxVertexCount = maxVertexCount;
  9262. this._maxIndexCount = maxIndexCount;
  9263. // buffers for multi draw
  9264. this._multiDrawCounts = new Int32Array( maxInstanceCount );
  9265. this._multiDrawStarts = new Int32Array( maxInstanceCount );
  9266. this._multiDrawCount = 0;
  9267. this._multiDrawInstances = null;
  9268. // Local matrix per geometry by using data texture
  9269. this._matricesTexture = null;
  9270. this._indirectTexture = null;
  9271. this._colorsTexture = null;
  9272. this._initMatricesTexture();
  9273. this._initIndirectTexture();
  9274. }
  9275. _initMatricesTexture() {
  9276. // layout (1 matrix = 4 pixels)
  9277. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  9278. // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)
  9279. // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)
  9280. // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)
  9281. // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)
  9282. let size = Math.sqrt( this._maxInstanceCount * 4 ); // 4 pixels needed for 1 matrix
  9283. size = Math.ceil( size / 4 ) * 4;
  9284. size = Math.max( size, 4 );
  9285. const matricesArray = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
  9286. const matricesTexture = new DataTexture( matricesArray, size, size, RGBAFormat, FloatType );
  9287. this._matricesTexture = matricesTexture;
  9288. }
  9289. _initIndirectTexture() {
  9290. let size = Math.sqrt( this._maxInstanceCount );
  9291. size = Math.ceil( size );
  9292. const indirectArray = new Uint32Array( size * size );
  9293. const indirectTexture = new DataTexture( indirectArray, size, size, RedIntegerFormat, UnsignedIntType );
  9294. this._indirectTexture = indirectTexture;
  9295. }
  9296. _initColorsTexture() {
  9297. let size = Math.sqrt( this._maxInstanceCount );
  9298. size = Math.ceil( size );
  9299. // 4 floats per RGBA pixel initialized to white
  9300. const colorsArray = new Float32Array( size * size * 4 ).fill( 1 );
  9301. const colorsTexture = new DataTexture( colorsArray, size, size, RGBAFormat, FloatType );
  9302. colorsTexture.colorSpace = ColorManagement.workingColorSpace;
  9303. this._colorsTexture = colorsTexture;
  9304. }
  9305. _initializeGeometry( reference ) {
  9306. const geometry = this.geometry;
  9307. const maxVertexCount = this._maxVertexCount;
  9308. const maxIndexCount = this._maxIndexCount;
  9309. if ( this._geometryInitialized === false ) {
  9310. for ( const attributeName in reference.attributes ) {
  9311. const srcAttribute = reference.getAttribute( attributeName );
  9312. const { array, itemSize, normalized } = srcAttribute;
  9313. const dstArray = new array.constructor( maxVertexCount * itemSize );
  9314. const dstAttribute = new BufferAttribute( dstArray, itemSize, normalized );
  9315. geometry.setAttribute( attributeName, dstAttribute );
  9316. }
  9317. if ( reference.getIndex() !== null ) {
  9318. // Reserve last u16 index for primitive restart.
  9319. const indexArray = maxVertexCount > 65535
  9320. ? new Uint32Array( maxIndexCount )
  9321. : new Uint16Array( maxIndexCount );
  9322. geometry.setIndex( new BufferAttribute( indexArray, 1 ) );
  9323. }
  9324. this._geometryInitialized = true;
  9325. }
  9326. }
  9327. // Make sure the geometry is compatible with the existing combined geometry attributes
  9328. _validateGeometry( geometry ) {
  9329. // check to ensure the geometries are using consistent attributes and indices
  9330. const batchGeometry = this.geometry;
  9331. if ( Boolean( geometry.getIndex() ) !== Boolean( batchGeometry.getIndex() ) ) {
  9332. throw new Error( 'THREE.BatchedMesh: All geometries must consistently have "index".' );
  9333. }
  9334. for ( const attributeName in batchGeometry.attributes ) {
  9335. if ( ! geometry.hasAttribute( attributeName ) ) {
  9336. throw new Error( `THREE.BatchedMesh: Added geometry missing "${ attributeName }". All geometries must have consistent attributes.` );
  9337. }
  9338. const srcAttribute = geometry.getAttribute( attributeName );
  9339. const dstAttribute = batchGeometry.getAttribute( attributeName );
  9340. if ( srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized ) {
  9341. throw new Error( 'THREE.BatchedMesh: All attributes must have a consistent itemSize and normalized value.' );
  9342. }
  9343. }
  9344. }
  9345. validateInstanceId( instanceId ) {
  9346. const instanceInfo = this._instanceInfo;
  9347. if ( instanceId < 0 || instanceId >= instanceInfo.length || instanceInfo[ instanceId ].active === false ) {
  9348. throw new Error( `THREE.BatchedMesh: Invalid instanceId ${instanceId}. Instance is either out of range or has been deleted.` );
  9349. }
  9350. }
  9351. validateGeometryId( geometryId ) {
  9352. const geometryInfoList = this._geometryInfo;
  9353. if ( geometryId < 0 || geometryId >= geometryInfoList.length || geometryInfoList[ geometryId ].active === false ) {
  9354. throw new Error( `THREE.BatchedMesh: Invalid geometryId ${geometryId}. Geometry is either out of range or has been deleted.` );
  9355. }
  9356. }
  9357. setCustomSort( func ) {
  9358. this.customSort = func;
  9359. return this;
  9360. }
  9361. computeBoundingBox() {
  9362. if ( this.boundingBox === null ) {
  9363. this.boundingBox = new Box3();
  9364. }
  9365. const boundingBox = this.boundingBox;
  9366. const instanceInfo = this._instanceInfo;
  9367. boundingBox.makeEmpty();
  9368. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9369. if ( instanceInfo[ i ].active === false ) continue;
  9370. const geometryId = instanceInfo[ i ].geometryIndex;
  9371. this.getMatrixAt( i, _matrix$1 );
  9372. this.getBoundingBoxAt( geometryId, _box$1 ).applyMatrix4( _matrix$1 );
  9373. boundingBox.union( _box$1 );
  9374. }
  9375. }
  9376. computeBoundingSphere() {
  9377. if ( this.boundingSphere === null ) {
  9378. this.boundingSphere = new Sphere();
  9379. }
  9380. const boundingSphere = this.boundingSphere;
  9381. const instanceInfo = this._instanceInfo;
  9382. boundingSphere.makeEmpty();
  9383. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9384. if ( instanceInfo[ i ].active === false ) continue;
  9385. const geometryId = instanceInfo[ i ].geometryIndex;
  9386. this.getMatrixAt( i, _matrix$1 );
  9387. this.getBoundingSphereAt( geometryId, _sphere$2 ).applyMatrix4( _matrix$1 );
  9388. boundingSphere.union( _sphere$2 );
  9389. }
  9390. }
  9391. addInstance( geometryId ) {
  9392. const atCapacity = this._instanceInfo.length >= this.maxInstanceCount;
  9393. // ensure we're not over geometry
  9394. if ( atCapacity && this._availableInstanceIds.length === 0 ) {
  9395. throw new Error( 'THREE.BatchedMesh: Maximum item count reached.' );
  9396. }
  9397. const instanceInfo = {
  9398. visible: true,
  9399. active: true,
  9400. geometryIndex: geometryId,
  9401. };
  9402. let drawId = null;
  9403. // Prioritize using previously freed instance ids
  9404. if ( this._availableInstanceIds.length > 0 ) {
  9405. this._availableInstanceIds.sort( ascIdSort );
  9406. drawId = this._availableInstanceIds.shift();
  9407. this._instanceInfo[ drawId ] = instanceInfo;
  9408. } else {
  9409. drawId = this._instanceInfo.length;
  9410. this._instanceInfo.push( instanceInfo );
  9411. }
  9412. const matricesTexture = this._matricesTexture;
  9413. _matrix$1.identity().toArray( matricesTexture.image.data, drawId * 16 );
  9414. matricesTexture.needsUpdate = true;
  9415. const colorsTexture = this._colorsTexture;
  9416. if ( colorsTexture ) {
  9417. _whiteColor.toArray( colorsTexture.image.data, drawId * 4 );
  9418. colorsTexture.needsUpdate = true;
  9419. }
  9420. this._visibilityChanged = true;
  9421. return drawId;
  9422. }
  9423. addGeometry( geometry, reservedVertexCount = - 1, reservedIndexCount = - 1 ) {
  9424. this._initializeGeometry( geometry );
  9425. this._validateGeometry( geometry );
  9426. const geometryInfo = {
  9427. // geometry information
  9428. vertexStart: - 1,
  9429. vertexCount: - 1,
  9430. reservedVertexCount: - 1,
  9431. indexStart: - 1,
  9432. indexCount: - 1,
  9433. reservedIndexCount: - 1,
  9434. // draw range information
  9435. start: - 1,
  9436. count: - 1,
  9437. // state
  9438. boundingBox: null,
  9439. boundingSphere: null,
  9440. active: true,
  9441. };
  9442. const geometryInfoList = this._geometryInfo;
  9443. geometryInfo.vertexStart = this._nextVertexStart;
  9444. geometryInfo.reservedVertexCount = reservedVertexCount === - 1 ? geometry.getAttribute( 'position' ).count : reservedVertexCount;
  9445. const index = geometry.getIndex();
  9446. const hasIndex = index !== null;
  9447. if ( hasIndex ) {
  9448. geometryInfo.indexStart = this._nextIndexStart;
  9449. geometryInfo.reservedIndexCount = reservedIndexCount === - 1 ? index.count : reservedIndexCount;
  9450. }
  9451. if (
  9452. geometryInfo.indexStart !== - 1 &&
  9453. geometryInfo.indexStart + geometryInfo.reservedIndexCount > this._maxIndexCount ||
  9454. geometryInfo.vertexStart + geometryInfo.reservedVertexCount > this._maxVertexCount
  9455. ) {
  9456. throw new Error( 'THREE.BatchedMesh: Reserved space request exceeds the maximum buffer size.' );
  9457. }
  9458. // update id
  9459. let geometryId;
  9460. if ( this._availableGeometryIds.length > 0 ) {
  9461. this._availableGeometryIds.sort( ascIdSort );
  9462. geometryId = this._availableGeometryIds.shift();
  9463. geometryInfoList[ geometryId ] = geometryInfo;
  9464. } else {
  9465. geometryId = this._geometryCount;
  9466. this._geometryCount ++;
  9467. geometryInfoList.push( geometryInfo );
  9468. }
  9469. // update the geometry
  9470. this.setGeometryAt( geometryId, geometry );
  9471. // increment the next geometry position
  9472. this._nextIndexStart = geometryInfo.indexStart + geometryInfo.reservedIndexCount;
  9473. this._nextVertexStart = geometryInfo.vertexStart + geometryInfo.reservedVertexCount;
  9474. return geometryId;
  9475. }
  9476. setGeometryAt( geometryId, geometry ) {
  9477. if ( geometryId >= this._geometryCount ) {
  9478. throw new Error( 'THREE.BatchedMesh: Maximum geometry count reached.' );
  9479. }
  9480. this._validateGeometry( geometry );
  9481. const batchGeometry = this.geometry;
  9482. const hasIndex = batchGeometry.getIndex() !== null;
  9483. const dstIndex = batchGeometry.getIndex();
  9484. const srcIndex = geometry.getIndex();
  9485. const geometryInfo = this._geometryInfo[ geometryId ];
  9486. if (
  9487. hasIndex &&
  9488. srcIndex.count > geometryInfo.reservedIndexCount ||
  9489. geometry.attributes.position.count > geometryInfo.reservedVertexCount
  9490. ) {
  9491. throw new Error( 'THREE.BatchedMesh: Reserved space not large enough for provided geometry.' );
  9492. }
  9493. // copy geometry buffer data over
  9494. const vertexStart = geometryInfo.vertexStart;
  9495. const reservedVertexCount = geometryInfo.reservedVertexCount;
  9496. geometryInfo.vertexCount = geometry.getAttribute( 'position' ).count;
  9497. for ( const attributeName in batchGeometry.attributes ) {
  9498. // copy attribute data
  9499. const srcAttribute = geometry.getAttribute( attributeName );
  9500. const dstAttribute = batchGeometry.getAttribute( attributeName );
  9501. copyAttributeData( srcAttribute, dstAttribute, vertexStart );
  9502. // fill the rest in with zeroes
  9503. const itemSize = srcAttribute.itemSize;
  9504. for ( let i = srcAttribute.count, l = reservedVertexCount; i < l; i ++ ) {
  9505. const index = vertexStart + i;
  9506. for ( let c = 0; c < itemSize; c ++ ) {
  9507. dstAttribute.setComponent( index, c, 0 );
  9508. }
  9509. }
  9510. dstAttribute.needsUpdate = true;
  9511. dstAttribute.addUpdateRange( vertexStart * itemSize, reservedVertexCount * itemSize );
  9512. }
  9513. // copy index
  9514. if ( hasIndex ) {
  9515. const indexStart = geometryInfo.indexStart;
  9516. const reservedIndexCount = geometryInfo.reservedIndexCount;
  9517. geometryInfo.indexCount = geometry.getIndex().count;
  9518. // copy index data over
  9519. for ( let i = 0; i < srcIndex.count; i ++ ) {
  9520. dstIndex.setX( indexStart + i, vertexStart + srcIndex.getX( i ) );
  9521. }
  9522. // fill the rest in with zeroes
  9523. for ( let i = srcIndex.count, l = reservedIndexCount; i < l; i ++ ) {
  9524. dstIndex.setX( indexStart + i, vertexStart );
  9525. }
  9526. dstIndex.needsUpdate = true;
  9527. dstIndex.addUpdateRange( indexStart, geometryInfo.reservedIndexCount );
  9528. }
  9529. // update the draw range
  9530. geometryInfo.start = hasIndex ? geometryInfo.indexStart : geometryInfo.vertexStart;
  9531. geometryInfo.count = hasIndex ? geometryInfo.indexCount : geometryInfo.vertexCount;
  9532. // store the bounding boxes
  9533. geometryInfo.boundingBox = null;
  9534. if ( geometry.boundingBox !== null ) {
  9535. geometryInfo.boundingBox = geometry.boundingBox.clone();
  9536. }
  9537. geometryInfo.boundingSphere = null;
  9538. if ( geometry.boundingSphere !== null ) {
  9539. geometryInfo.boundingSphere = geometry.boundingSphere.clone();
  9540. }
  9541. this._visibilityChanged = true;
  9542. return geometryId;
  9543. }
  9544. deleteGeometry( geometryId ) {
  9545. const geometryInfoList = this._geometryInfo;
  9546. if ( geometryId >= geometryInfoList.length || geometryInfoList[ geometryId ].active === false ) {
  9547. return this;
  9548. }
  9549. // delete any instances associated with this geometry
  9550. const instanceInfo = this._instanceInfo;
  9551. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9552. if ( instanceInfo[ i ].geometryIndex === geometryId ) {
  9553. this.deleteInstance( i );
  9554. }
  9555. }
  9556. geometryInfoList[ geometryId ].active = false;
  9557. this._availableGeometryIds.push( geometryId );
  9558. this._visibilityChanged = true;
  9559. return this;
  9560. }
  9561. deleteInstance( instanceId ) {
  9562. this.validateInstanceId( instanceId );
  9563. this._instanceInfo[ instanceId ].active = false;
  9564. this._availableInstanceIds.push( instanceId );
  9565. this._visibilityChanged = true;
  9566. return this;
  9567. }
  9568. optimize() {
  9569. // track the next indices to copy data to
  9570. let nextVertexStart = 0;
  9571. let nextIndexStart = 0;
  9572. // Iterate over all geometry ranges in order sorted from earliest in the geometry buffer to latest
  9573. // in the geometry buffer. Because draw range objects can be reused there is no guarantee of their order.
  9574. const geometryInfoList = this._geometryInfo;
  9575. const indices = geometryInfoList
  9576. .map( ( e, i ) => i )
  9577. .sort( ( a, b ) => {
  9578. return geometryInfoList[ a ].vertexStart - geometryInfoList[ b ].vertexStart;
  9579. } );
  9580. const geometry = this.geometry;
  9581. for ( let i = 0, l = geometryInfoList.length; i < l; i ++ ) {
  9582. // if a geometry range is inactive then don't copy anything
  9583. const index = indices[ i ];
  9584. const geometryInfo = geometryInfoList[ index ];
  9585. if ( geometryInfo.active === false ) {
  9586. continue;
  9587. }
  9588. // if a geometry contains an index buffer then shift it, as well
  9589. if ( geometry.index !== null ) {
  9590. if ( geometryInfo.indexStart !== nextIndexStart ) {
  9591. const { indexStart, vertexStart, reservedIndexCount } = geometryInfo;
  9592. const index = geometry.index;
  9593. const array = index.array;
  9594. // shift the index pointers based on how the vertex data will shift
  9595. // adjusting the index must happen first so the original vertex start value is available
  9596. const elementDelta = nextVertexStart - vertexStart;
  9597. for ( let j = indexStart; j < indexStart + reservedIndexCount; j ++ ) {
  9598. array[ j ] = array[ j ] + elementDelta;
  9599. }
  9600. index.array.copyWithin( nextIndexStart, indexStart, indexStart + reservedIndexCount );
  9601. index.addUpdateRange( nextIndexStart, reservedIndexCount );
  9602. geometryInfo.indexStart = nextIndexStart;
  9603. }
  9604. nextIndexStart += geometryInfo.reservedIndexCount;
  9605. }
  9606. // if a geometry needs to be moved then copy attribute data to overwrite unused space
  9607. if ( geometryInfo.vertexStart !== nextVertexStart ) {
  9608. const { vertexStart, reservedVertexCount } = geometryInfo;
  9609. const attributes = geometry.attributes;
  9610. for ( const key in attributes ) {
  9611. const attribute = attributes[ key ];
  9612. const { array, itemSize } = attribute;
  9613. array.copyWithin( nextVertexStart * itemSize, vertexStart * itemSize, ( vertexStart + reservedVertexCount ) * itemSize );
  9614. attribute.addUpdateRange( nextVertexStart * itemSize, reservedVertexCount * itemSize );
  9615. }
  9616. geometryInfo.vertexStart = nextVertexStart;
  9617. }
  9618. nextVertexStart += geometryInfo.reservedVertexCount;
  9619. geometryInfo.start = geometry.index ? geometryInfo.indexStart : geometryInfo.vertexStart;
  9620. // step the next geometry points to the shifted position
  9621. this._nextIndexStart = geometry.index ? geometryInfo.indexStart + geometryInfo.reservedIndexCount : 0;
  9622. this._nextVertexStart = geometryInfo.vertexStart + geometryInfo.reservedVertexCount;
  9623. }
  9624. return this;
  9625. }
  9626. // get bounding box and compute it if it doesn't exist
  9627. getBoundingBoxAt( geometryId, target ) {
  9628. if ( geometryId >= this._geometryCount ) {
  9629. return null;
  9630. }
  9631. // compute bounding box
  9632. const geometry = this.geometry;
  9633. const geometryInfo = this._geometryInfo[ geometryId ];
  9634. if ( geometryInfo.boundingBox === null ) {
  9635. const box = new Box3();
  9636. const index = geometry.index;
  9637. const position = geometry.attributes.position;
  9638. for ( let i = geometryInfo.start, l = geometryInfo.start + geometryInfo.count; i < l; i ++ ) {
  9639. let iv = i;
  9640. if ( index ) {
  9641. iv = index.getX( iv );
  9642. }
  9643. box.expandByPoint( _vector$5.fromBufferAttribute( position, iv ) );
  9644. }
  9645. geometryInfo.boundingBox = box;
  9646. }
  9647. target.copy( geometryInfo.boundingBox );
  9648. return target;
  9649. }
  9650. // get bounding sphere and compute it if it doesn't exist
  9651. getBoundingSphereAt( geometryId, target ) {
  9652. if ( geometryId >= this._geometryCount ) {
  9653. return null;
  9654. }
  9655. // compute bounding sphere
  9656. const geometry = this.geometry;
  9657. const geometryInfo = this._geometryInfo[ geometryId ];
  9658. if ( geometryInfo.boundingSphere === null ) {
  9659. const sphere = new Sphere();
  9660. this.getBoundingBoxAt( geometryId, _box$1 );
  9661. _box$1.getCenter( sphere.center );
  9662. const index = geometry.index;
  9663. const position = geometry.attributes.position;
  9664. let maxRadiusSq = 0;
  9665. for ( let i = geometryInfo.start, l = geometryInfo.start + geometryInfo.count; i < l; i ++ ) {
  9666. let iv = i;
  9667. if ( index ) {
  9668. iv = index.getX( iv );
  9669. }
  9670. _vector$5.fromBufferAttribute( position, iv );
  9671. maxRadiusSq = Math.max( maxRadiusSq, sphere.center.distanceToSquared( _vector$5 ) );
  9672. }
  9673. sphere.radius = Math.sqrt( maxRadiusSq );
  9674. geometryInfo.boundingSphere = sphere;
  9675. }
  9676. target.copy( geometryInfo.boundingSphere );
  9677. return target;
  9678. }
  9679. setMatrixAt( instanceId, matrix ) {
  9680. this.validateInstanceId( instanceId );
  9681. const matricesTexture = this._matricesTexture;
  9682. const matricesArray = this._matricesTexture.image.data;
  9683. matrix.toArray( matricesArray, instanceId * 16 );
  9684. matricesTexture.needsUpdate = true;
  9685. return this;
  9686. }
  9687. getMatrixAt( instanceId, matrix ) {
  9688. this.validateInstanceId( instanceId );
  9689. return matrix.fromArray( this._matricesTexture.image.data, instanceId * 16 );
  9690. }
  9691. setColorAt( instanceId, color ) {
  9692. this.validateInstanceId( instanceId );
  9693. if ( this._colorsTexture === null ) {
  9694. this._initColorsTexture();
  9695. }
  9696. color.toArray( this._colorsTexture.image.data, instanceId * 4 );
  9697. this._colorsTexture.needsUpdate = true;
  9698. return this;
  9699. }
  9700. getColorAt( instanceId, color ) {
  9701. this.validateInstanceId( instanceId );
  9702. return color.fromArray( this._colorsTexture.image.data, instanceId * 4 );
  9703. }
  9704. setVisibleAt( instanceId, value ) {
  9705. this.validateInstanceId( instanceId );
  9706. if ( this._instanceInfo[ instanceId ].visible === value ) {
  9707. return this;
  9708. }
  9709. this._instanceInfo[ instanceId ].visible = value;
  9710. this._visibilityChanged = true;
  9711. return this;
  9712. }
  9713. getVisibleAt( instanceId ) {
  9714. this.validateInstanceId( instanceId );
  9715. return this._instanceInfo[ instanceId ].visible;
  9716. }
  9717. setGeometryIdAt( instanceId, geometryId ) {
  9718. this.validateInstanceId( instanceId );
  9719. this.validateGeometryId( geometryId );
  9720. this._instanceInfo[ instanceId ].geometryIndex = geometryId;
  9721. return this;
  9722. }
  9723. getGeometryIdAt( instanceId ) {
  9724. this.validateInstanceId( instanceId );
  9725. return this._instanceInfo[ instanceId ].geometryIndex;
  9726. }
  9727. getGeometryRangeAt( geometryId, target = {} ) {
  9728. this.validateGeometryId( geometryId );
  9729. const geometryInfo = this._geometryInfo[ geometryId ];
  9730. target.vertexStart = geometryInfo.vertexStart;
  9731. target.vertexCount = geometryInfo.vertexCount;
  9732. target.reservedVertexCount = geometryInfo.reservedVertexCount;
  9733. target.indexStart = geometryInfo.indexStart;
  9734. target.indexCount = geometryInfo.indexCount;
  9735. target.reservedIndexCount = geometryInfo.reservedIndexCount;
  9736. target.start = geometryInfo.start;
  9737. target.count = geometryInfo.count;
  9738. return target;
  9739. }
  9740. setInstanceCount( maxInstanceCount ) {
  9741. // shrink the available instances as much as possible
  9742. const availableInstanceIds = this._availableInstanceIds;
  9743. const instanceInfo = this._instanceInfo;
  9744. availableInstanceIds.sort( ascIdSort );
  9745. while ( availableInstanceIds[ availableInstanceIds.length - 1 ] === instanceInfo.length ) {
  9746. instanceInfo.pop();
  9747. availableInstanceIds.pop();
  9748. }
  9749. // throw an error if it can't be shrunk to the desired size
  9750. if ( maxInstanceCount < instanceInfo.length ) {
  9751. throw new Error( `BatchedMesh: Instance ids outside the range ${ maxInstanceCount } are being used. Cannot shrink instance count.` );
  9752. }
  9753. // copy the multi draw counts
  9754. const multiDrawCounts = new Int32Array( maxInstanceCount );
  9755. const multiDrawStarts = new Int32Array( maxInstanceCount );
  9756. copyArrayContents( this._multiDrawCounts, multiDrawCounts );
  9757. copyArrayContents( this._multiDrawStarts, multiDrawStarts );
  9758. this._multiDrawCounts = multiDrawCounts;
  9759. this._multiDrawStarts = multiDrawStarts;
  9760. this._maxInstanceCount = maxInstanceCount;
  9761. // update texture data for instance sampling
  9762. const indirectTexture = this._indirectTexture;
  9763. const matricesTexture = this._matricesTexture;
  9764. const colorsTexture = this._colorsTexture;
  9765. indirectTexture.dispose();
  9766. this._initIndirectTexture();
  9767. copyArrayContents( indirectTexture.image.data, this._indirectTexture.image.data );
  9768. matricesTexture.dispose();
  9769. this._initMatricesTexture();
  9770. copyArrayContents( matricesTexture.image.data, this._matricesTexture.image.data );
  9771. if ( colorsTexture ) {
  9772. colorsTexture.dispose();
  9773. this._initColorsTexture();
  9774. copyArrayContents( colorsTexture.image.data, this._colorsTexture.image.data );
  9775. }
  9776. }
  9777. setGeometrySize( maxVertexCount, maxIndexCount ) {
  9778. // Check if we can shrink to the requested vertex attribute size
  9779. const validRanges = [ ...this._geometryInfo ].filter( info => info.active );
  9780. const requiredVertexLength = Math.max( ...validRanges.map( range => range.vertexStart + range.reservedVertexCount ) );
  9781. if ( requiredVertexLength > maxVertexCount ) {
  9782. throw new Error( `BatchedMesh: Geometry vertex values are being used outside the range ${ maxIndexCount }. Cannot shrink further.` );
  9783. }
  9784. // Check if we can shrink to the requested index attribute size
  9785. if ( this.geometry.index ) {
  9786. const requiredIndexLength = Math.max( ...validRanges.map( range => range.indexStart + range.reservedIndexCount ) );
  9787. if ( requiredIndexLength > maxIndexCount ) {
  9788. throw new Error( `BatchedMesh: Geometry index values are being used outside the range ${ maxIndexCount }. Cannot shrink further.` );
  9789. }
  9790. }
  9791. //
  9792. // dispose of the previous geometry
  9793. const oldGeometry = this.geometry;
  9794. oldGeometry.dispose();
  9795. // recreate the geometry needed based on the previous variant
  9796. this._maxVertexCount = maxVertexCount;
  9797. this._maxIndexCount = maxIndexCount;
  9798. if ( this._geometryInitialized ) {
  9799. this._geometryInitialized = false;
  9800. this.geometry = new BufferGeometry();
  9801. this._initializeGeometry( oldGeometry );
  9802. }
  9803. // copy data from the previous geometry
  9804. const geometry = this.geometry;
  9805. if ( oldGeometry.index ) {
  9806. copyArrayContents( oldGeometry.index.array, geometry.index.array );
  9807. }
  9808. for ( const key in oldGeometry.attributes ) {
  9809. copyArrayContents( oldGeometry.attributes[ key ].array, geometry.attributes[ key ].array );
  9810. }
  9811. }
  9812. raycast( raycaster, intersects ) {
  9813. const instanceInfo = this._instanceInfo;
  9814. const geometryInfoList = this._geometryInfo;
  9815. const matrixWorld = this.matrixWorld;
  9816. const batchGeometry = this.geometry;
  9817. // iterate over each geometry
  9818. _mesh.material = this.material;
  9819. _mesh.geometry.index = batchGeometry.index;
  9820. _mesh.geometry.attributes = batchGeometry.attributes;
  9821. if ( _mesh.geometry.boundingBox === null ) {
  9822. _mesh.geometry.boundingBox = new Box3();
  9823. }
  9824. if ( _mesh.geometry.boundingSphere === null ) {
  9825. _mesh.geometry.boundingSphere = new Sphere();
  9826. }
  9827. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9828. if ( ! instanceInfo[ i ].visible || ! instanceInfo[ i ].active ) {
  9829. continue;
  9830. }
  9831. const geometryId = instanceInfo[ i ].geometryIndex;
  9832. const geometryInfo = geometryInfoList[ geometryId ];
  9833. _mesh.geometry.setDrawRange( geometryInfo.start, geometryInfo.count );
  9834. // get the intersects
  9835. this.getMatrixAt( i, _mesh.matrixWorld ).premultiply( matrixWorld );
  9836. this.getBoundingBoxAt( geometryId, _mesh.geometry.boundingBox );
  9837. this.getBoundingSphereAt( geometryId, _mesh.geometry.boundingSphere );
  9838. _mesh.raycast( raycaster, _batchIntersects );
  9839. // add batch id to the intersects
  9840. for ( let j = 0, l = _batchIntersects.length; j < l; j ++ ) {
  9841. const intersect = _batchIntersects[ j ];
  9842. intersect.object = this;
  9843. intersect.batchId = i;
  9844. intersects.push( intersect );
  9845. }
  9846. _batchIntersects.length = 0;
  9847. }
  9848. _mesh.material = null;
  9849. _mesh.geometry.index = null;
  9850. _mesh.geometry.attributes = {};
  9851. _mesh.geometry.setDrawRange( 0, Infinity );
  9852. }
  9853. copy( source ) {
  9854. super.copy( source );
  9855. this.geometry = source.geometry.clone();
  9856. this.perObjectFrustumCulled = source.perObjectFrustumCulled;
  9857. this.sortObjects = source.sortObjects;
  9858. this.boundingBox = source.boundingBox !== null ? source.boundingBox.clone() : null;
  9859. this.boundingSphere = source.boundingSphere !== null ? source.boundingSphere.clone() : null;
  9860. this._geometryInfo = source._geometryInfo.map( info => ( {
  9861. ...info,
  9862. boundingBox: info.boundingBox !== null ? info.boundingBox.clone() : null,
  9863. boundingSphere: info.boundingSphere !== null ? info.boundingSphere.clone() : null,
  9864. } ) );
  9865. this._instanceInfo = source._instanceInfo.map( info => ( { ...info } ) );
  9866. this._maxInstanceCount = source._maxInstanceCount;
  9867. this._maxVertexCount = source._maxVertexCount;
  9868. this._maxIndexCount = source._maxIndexCount;
  9869. this._geometryInitialized = source._geometryInitialized;
  9870. this._geometryCount = source._geometryCount;
  9871. this._multiDrawCounts = source._multiDrawCounts.slice();
  9872. this._multiDrawStarts = source._multiDrawStarts.slice();
  9873. this._matricesTexture = source._matricesTexture.clone();
  9874. this._matricesTexture.image.data = this._matricesTexture.image.data.slice();
  9875. if ( this._colorsTexture !== null ) {
  9876. this._colorsTexture = source._colorsTexture.clone();
  9877. this._colorsTexture.image.data = this._colorsTexture.image.data.slice();
  9878. }
  9879. return this;
  9880. }
  9881. dispose() {
  9882. // Assuming the geometry is not shared with other meshes
  9883. this.geometry.dispose();
  9884. this._matricesTexture.dispose();
  9885. this._matricesTexture = null;
  9886. this._indirectTexture.dispose();
  9887. this._indirectTexture = null;
  9888. if ( this._colorsTexture !== null ) {
  9889. this._colorsTexture.dispose();
  9890. this._colorsTexture = null;
  9891. }
  9892. return this;
  9893. }
  9894. onBeforeRender( renderer, scene, camera, geometry, material/*, _group*/ ) {
  9895. // if visibility has not changed and frustum culling and object sorting is not required
  9896. // then skip iterating over all items
  9897. if ( ! this._visibilityChanged && ! this.perObjectFrustumCulled && ! this.sortObjects ) {
  9898. return;
  9899. }
  9900. // the indexed version of the multi draw function requires specifying the start
  9901. // offset in bytes.
  9902. const index = geometry.getIndex();
  9903. const bytesPerElement = index === null ? 1 : index.array.BYTES_PER_ELEMENT;
  9904. const instanceInfo = this._instanceInfo;
  9905. const multiDrawStarts = this._multiDrawStarts;
  9906. const multiDrawCounts = this._multiDrawCounts;
  9907. const geometryInfoList = this._geometryInfo;
  9908. const perObjectFrustumCulled = this.perObjectFrustumCulled;
  9909. const indirectTexture = this._indirectTexture;
  9910. const indirectArray = indirectTexture.image.data;
  9911. // prepare the frustum in the local frame
  9912. if ( perObjectFrustumCulled ) {
  9913. _matrix$1
  9914. .multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse )
  9915. .multiply( this.matrixWorld );
  9916. _frustum.setFromProjectionMatrix(
  9917. _matrix$1,
  9918. renderer.coordinateSystem
  9919. );
  9920. }
  9921. let multiDrawCount = 0;
  9922. if ( this.sortObjects ) {
  9923. // get the camera position in the local frame
  9924. _matrix$1.copy( this.matrixWorld ).invert();
  9925. _vector$5.setFromMatrixPosition( camera.matrixWorld ).applyMatrix4( _matrix$1 );
  9926. _forward.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld ).transformDirection( _matrix$1 );
  9927. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9928. if ( instanceInfo[ i ].visible && instanceInfo[ i ].active ) {
  9929. const geometryId = instanceInfo[ i ].geometryIndex;
  9930. // get the bounds in world space
  9931. this.getMatrixAt( i, _matrix$1 );
  9932. this.getBoundingSphereAt( geometryId, _sphere$2 ).applyMatrix4( _matrix$1 );
  9933. // determine whether the batched geometry is within the frustum
  9934. let culled = false;
  9935. if ( perObjectFrustumCulled ) {
  9936. culled = ! _frustum.intersectsSphere( _sphere$2 );
  9937. }
  9938. if ( ! culled ) {
  9939. // get the distance from camera used for sorting
  9940. const geometryInfo = geometryInfoList[ geometryId ];
  9941. const z = _temp.subVectors( _sphere$2.center, _vector$5 ).dot( _forward );
  9942. _renderList.push( geometryInfo.start, geometryInfo.count, z, i );
  9943. }
  9944. }
  9945. }
  9946. // Sort the draw ranges and prep for rendering
  9947. const list = _renderList.list;
  9948. const customSort = this.customSort;
  9949. if ( customSort === null ) {
  9950. list.sort( material.transparent ? sortTransparent : sortOpaque );
  9951. } else {
  9952. customSort.call( this, list, camera );
  9953. }
  9954. for ( let i = 0, l = list.length; i < l; i ++ ) {
  9955. const item = list[ i ];
  9956. multiDrawStarts[ multiDrawCount ] = item.start * bytesPerElement;
  9957. multiDrawCounts[ multiDrawCount ] = item.count;
  9958. indirectArray[ multiDrawCount ] = item.index;
  9959. multiDrawCount ++;
  9960. }
  9961. _renderList.reset();
  9962. } else {
  9963. for ( let i = 0, l = instanceInfo.length; i < l; i ++ ) {
  9964. if ( instanceInfo[ i ].visible && instanceInfo[ i ].active ) {
  9965. const geometryId = instanceInfo[ i ].geometryIndex;
  9966. // determine whether the batched geometry is within the frustum
  9967. let culled = false;
  9968. if ( perObjectFrustumCulled ) {
  9969. // get the bounds in world space
  9970. this.getMatrixAt( i, _matrix$1 );
  9971. this.getBoundingSphereAt( geometryId, _sphere$2 ).applyMatrix4( _matrix$1 );
  9972. culled = ! _frustum.intersectsSphere( _sphere$2 );
  9973. }
  9974. if ( ! culled ) {
  9975. const geometryInfo = geometryInfoList[ geometryId ];
  9976. multiDrawStarts[ multiDrawCount ] = geometryInfo.start * bytesPerElement;
  9977. multiDrawCounts[ multiDrawCount ] = geometryInfo.count;
  9978. indirectArray[ multiDrawCount ] = i;
  9979. multiDrawCount ++;
  9980. }
  9981. }
  9982. }
  9983. }
  9984. indirectTexture.needsUpdate = true;
  9985. this._multiDrawCount = multiDrawCount;
  9986. this._visibilityChanged = false;
  9987. }
  9988. onBeforeShadow( renderer, object, camera, shadowCamera, geometry, depthMaterial/* , group */ ) {
  9989. this.onBeforeRender( renderer, null, shadowCamera, geometry, depthMaterial );
  9990. }
  9991. }
  9992. class LineBasicMaterial extends Material {
  9993. constructor( parameters ) {
  9994. super();
  9995. this.isLineBasicMaterial = true;
  9996. this.type = 'LineBasicMaterial';
  9997. this.color = new Color( 0xffffff );
  9998. this.map = null;
  9999. this.linewidth = 1;
  10000. this.linecap = 'round';
  10001. this.linejoin = 'round';
  10002. this.fog = true;
  10003. this.setValues( parameters );
  10004. }
  10005. copy( source ) {
  10006. super.copy( source );
  10007. this.color.copy( source.color );
  10008. this.map = source.map;
  10009. this.linewidth = source.linewidth;
  10010. this.linecap = source.linecap;
  10011. this.linejoin = source.linejoin;
  10012. this.fog = source.fog;
  10013. return this;
  10014. }
  10015. }
  10016. const _vStart = /*@__PURE__*/ new Vector3();
  10017. const _vEnd = /*@__PURE__*/ new Vector3();
  10018. const _inverseMatrix$1 = /*@__PURE__*/ new Matrix4();
  10019. const _ray$1 = /*@__PURE__*/ new Ray();
  10020. const _sphere$1 = /*@__PURE__*/ new Sphere();
  10021. const _intersectPointOnRay = /*@__PURE__*/ new Vector3();
  10022. const _intersectPointOnSegment = /*@__PURE__*/ new Vector3();
  10023. class Line extends Object3D {
  10024. constructor( geometry = new BufferGeometry(), material = new LineBasicMaterial() ) {
  10025. super();
  10026. this.isLine = true;
  10027. this.type = 'Line';
  10028. this.geometry = geometry;
  10029. this.material = material;
  10030. this.updateMorphTargets();
  10031. }
  10032. copy( source, recursive ) {
  10033. super.copy( source, recursive );
  10034. this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
  10035. this.geometry = source.geometry;
  10036. return this;
  10037. }
  10038. computeLineDistances() {
  10039. const geometry = this.geometry;
  10040. // we assume non-indexed geometry
  10041. if ( geometry.index === null ) {
  10042. const positionAttribute = geometry.attributes.position;
  10043. const lineDistances = [ 0 ];
  10044. for ( let i = 1, l = positionAttribute.count; i < l; i ++ ) {
  10045. _vStart.fromBufferAttribute( positionAttribute, i - 1 );
  10046. _vEnd.fromBufferAttribute( positionAttribute, i );
  10047. lineDistances[ i ] = lineDistances[ i - 1 ];
  10048. lineDistances[ i ] += _vStart.distanceTo( _vEnd );
  10049. }
  10050. geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
  10051. } else {
  10052. console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
  10053. }
  10054. return this;
  10055. }
  10056. raycast( raycaster, intersects ) {
  10057. const geometry = this.geometry;
  10058. const matrixWorld = this.matrixWorld;
  10059. const threshold = raycaster.params.Line.threshold;
  10060. const drawRange = geometry.drawRange;
  10061. // Checking boundingSphere distance to ray
  10062. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  10063. _sphere$1.copy( geometry.boundingSphere );
  10064. _sphere$1.applyMatrix4( matrixWorld );
  10065. _sphere$1.radius += threshold;
  10066. if ( raycaster.ray.intersectsSphere( _sphere$1 ) === false ) return;
  10067. //
  10068. _inverseMatrix$1.copy( matrixWorld ).invert();
  10069. _ray$1.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$1 );
  10070. const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  10071. const localThresholdSq = localThreshold * localThreshold;
  10072. const step = this.isLineSegments ? 2 : 1;
  10073. const index = geometry.index;
  10074. const attributes = geometry.attributes;
  10075. const positionAttribute = attributes.position;
  10076. if ( index !== null ) {
  10077. const start = Math.max( 0, drawRange.start );
  10078. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  10079. for ( let i = start, l = end - 1; i < l; i += step ) {
  10080. const a = index.getX( i );
  10081. const b = index.getX( i + 1 );
  10082. const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, a, b );
  10083. if ( intersect ) {
  10084. intersects.push( intersect );
  10085. }
  10086. }
  10087. if ( this.isLineLoop ) {
  10088. const a = index.getX( end - 1 );
  10089. const b = index.getX( start );
  10090. const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, a, b );
  10091. if ( intersect ) {
  10092. intersects.push( intersect );
  10093. }
  10094. }
  10095. } else {
  10096. const start = Math.max( 0, drawRange.start );
  10097. const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
  10098. for ( let i = start, l = end - 1; i < l; i += step ) {
  10099. const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, i, i + 1 );
  10100. if ( intersect ) {
  10101. intersects.push( intersect );
  10102. }
  10103. }
  10104. if ( this.isLineLoop ) {
  10105. const intersect = checkIntersection( this, raycaster, _ray$1, localThresholdSq, end - 1, start );
  10106. if ( intersect ) {
  10107. intersects.push( intersect );
  10108. }
  10109. }
  10110. }
  10111. }
  10112. updateMorphTargets() {
  10113. const geometry = this.geometry;
  10114. const morphAttributes = geometry.morphAttributes;
  10115. const keys = Object.keys( morphAttributes );
  10116. if ( keys.length > 0 ) {
  10117. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  10118. if ( morphAttribute !== undefined ) {
  10119. this.morphTargetInfluences = [];
  10120. this.morphTargetDictionary = {};
  10121. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  10122. const name = morphAttribute[ m ].name || String( m );
  10123. this.morphTargetInfluences.push( 0 );
  10124. this.morphTargetDictionary[ name ] = m;
  10125. }
  10126. }
  10127. }
  10128. }
  10129. }
  10130. function checkIntersection( object, raycaster, ray, thresholdSq, a, b ) {
  10131. const positionAttribute = object.geometry.attributes.position;
  10132. _vStart.fromBufferAttribute( positionAttribute, a );
  10133. _vEnd.fromBufferAttribute( positionAttribute, b );
  10134. const distSq = ray.distanceSqToSegment( _vStart, _vEnd, _intersectPointOnRay, _intersectPointOnSegment );
  10135. if ( distSq > thresholdSq ) return;
  10136. _intersectPointOnRay.applyMatrix4( object.matrixWorld ); // Move back to world space for distance calculation
  10137. const distance = raycaster.ray.origin.distanceTo( _intersectPointOnRay );
  10138. if ( distance < raycaster.near || distance > raycaster.far ) return;
  10139. return {
  10140. distance: distance,
  10141. // What do we want? intersection point on the ray or on the segment??
  10142. // point: raycaster.ray.at( distance ),
  10143. point: _intersectPointOnSegment.clone().applyMatrix4( object.matrixWorld ),
  10144. index: a,
  10145. face: null,
  10146. faceIndex: null,
  10147. barycoord: null,
  10148. object: object
  10149. };
  10150. }
  10151. const _start = /*@__PURE__*/ new Vector3();
  10152. const _end = /*@__PURE__*/ new Vector3();
  10153. class LineSegments extends Line {
  10154. constructor( geometry, material ) {
  10155. super( geometry, material );
  10156. this.isLineSegments = true;
  10157. this.type = 'LineSegments';
  10158. }
  10159. computeLineDistances() {
  10160. const geometry = this.geometry;
  10161. // we assume non-indexed geometry
  10162. if ( geometry.index === null ) {
  10163. const positionAttribute = geometry.attributes.position;
  10164. const lineDistances = [];
  10165. for ( let i = 0, l = positionAttribute.count; i < l; i += 2 ) {
  10166. _start.fromBufferAttribute( positionAttribute, i );
  10167. _end.fromBufferAttribute( positionAttribute, i + 1 );
  10168. lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ];
  10169. lineDistances[ i + 1 ] = lineDistances[ i ] + _start.distanceTo( _end );
  10170. }
  10171. geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) );
  10172. } else {
  10173. console.warn( 'THREE.LineSegments.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' );
  10174. }
  10175. return this;
  10176. }
  10177. }
  10178. class LineLoop extends Line {
  10179. constructor( geometry, material ) {
  10180. super( geometry, material );
  10181. this.isLineLoop = true;
  10182. this.type = 'LineLoop';
  10183. }
  10184. }
  10185. class PointsMaterial extends Material {
  10186. constructor( parameters ) {
  10187. super();
  10188. this.isPointsMaterial = true;
  10189. this.type = 'PointsMaterial';
  10190. this.color = new Color( 0xffffff );
  10191. this.map = null;
  10192. this.alphaMap = null;
  10193. this.size = 1;
  10194. this.sizeAttenuation = true;
  10195. this.fog = true;
  10196. this.setValues( parameters );
  10197. }
  10198. copy( source ) {
  10199. super.copy( source );
  10200. this.color.copy( source.color );
  10201. this.map = source.map;
  10202. this.alphaMap = source.alphaMap;
  10203. this.size = source.size;
  10204. this.sizeAttenuation = source.sizeAttenuation;
  10205. this.fog = source.fog;
  10206. return this;
  10207. }
  10208. }
  10209. const _inverseMatrix = /*@__PURE__*/ new Matrix4();
  10210. const _ray = /*@__PURE__*/ new Ray();
  10211. const _sphere = /*@__PURE__*/ new Sphere();
  10212. const _position$2 = /*@__PURE__*/ new Vector3();
  10213. class Points extends Object3D {
  10214. constructor( geometry = new BufferGeometry(), material = new PointsMaterial() ) {
  10215. super();
  10216. this.isPoints = true;
  10217. this.type = 'Points';
  10218. this.geometry = geometry;
  10219. this.material = material;
  10220. this.updateMorphTargets();
  10221. }
  10222. copy( source, recursive ) {
  10223. super.copy( source, recursive );
  10224. this.material = Array.isArray( source.material ) ? source.material.slice() : source.material;
  10225. this.geometry = source.geometry;
  10226. return this;
  10227. }
  10228. raycast( raycaster, intersects ) {
  10229. const geometry = this.geometry;
  10230. const matrixWorld = this.matrixWorld;
  10231. const threshold = raycaster.params.Points.threshold;
  10232. const drawRange = geometry.drawRange;
  10233. // Checking boundingSphere distance to ray
  10234. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  10235. _sphere.copy( geometry.boundingSphere );
  10236. _sphere.applyMatrix4( matrixWorld );
  10237. _sphere.radius += threshold;
  10238. if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return;
  10239. //
  10240. _inverseMatrix.copy( matrixWorld ).invert();
  10241. _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix );
  10242. const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  10243. const localThresholdSq = localThreshold * localThreshold;
  10244. const index = geometry.index;
  10245. const attributes = geometry.attributes;
  10246. const positionAttribute = attributes.position;
  10247. if ( index !== null ) {
  10248. const start = Math.max( 0, drawRange.start );
  10249. const end = Math.min( index.count, ( drawRange.start + drawRange.count ) );
  10250. for ( let i = start, il = end; i < il; i ++ ) {
  10251. const a = index.getX( i );
  10252. _position$2.fromBufferAttribute( positionAttribute, a );
  10253. testPoint( _position$2, a, localThresholdSq, matrixWorld, raycaster, intersects, this );
  10254. }
  10255. } else {
  10256. const start = Math.max( 0, drawRange.start );
  10257. const end = Math.min( positionAttribute.count, ( drawRange.start + drawRange.count ) );
  10258. for ( let i = start, l = end; i < l; i ++ ) {
  10259. _position$2.fromBufferAttribute( positionAttribute, i );
  10260. testPoint( _position$2, i, localThresholdSq, matrixWorld, raycaster, intersects, this );
  10261. }
  10262. }
  10263. }
  10264. updateMorphTargets() {
  10265. const geometry = this.geometry;
  10266. const morphAttributes = geometry.morphAttributes;
  10267. const keys = Object.keys( morphAttributes );
  10268. if ( keys.length > 0 ) {
  10269. const morphAttribute = morphAttributes[ keys[ 0 ] ];
  10270. if ( morphAttribute !== undefined ) {
  10271. this.morphTargetInfluences = [];
  10272. this.morphTargetDictionary = {};
  10273. for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) {
  10274. const name = morphAttribute[ m ].name || String( m );
  10275. this.morphTargetInfluences.push( 0 );
  10276. this.morphTargetDictionary[ name ] = m;
  10277. }
  10278. }
  10279. }
  10280. }
  10281. }
  10282. function testPoint( point, index, localThresholdSq, matrixWorld, raycaster, intersects, object ) {
  10283. const rayPointDistanceSq = _ray.distanceSqToPoint( point );
  10284. if ( rayPointDistanceSq < localThresholdSq ) {
  10285. const intersectPoint = new Vector3();
  10286. _ray.closestPointToPoint( point, intersectPoint );
  10287. intersectPoint.applyMatrix4( matrixWorld );
  10288. const distance = raycaster.ray.origin.distanceTo( intersectPoint );
  10289. if ( distance < raycaster.near || distance > raycaster.far ) return;
  10290. intersects.push( {
  10291. distance: distance,
  10292. distanceToRay: Math.sqrt( rayPointDistanceSq ),
  10293. point: intersectPoint,
  10294. index: index,
  10295. face: null,
  10296. faceIndex: null,
  10297. barycoord: null,
  10298. object: object
  10299. } );
  10300. }
  10301. }
  10302. class Group extends Object3D {
  10303. constructor() {
  10304. super();
  10305. this.isGroup = true;
  10306. this.type = 'Group';
  10307. }
  10308. }
  10309. class VideoTexture extends Texture {
  10310. constructor( video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
  10311. super( video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
  10312. this.isVideoTexture = true;
  10313. this.minFilter = minFilter !== undefined ? minFilter : LinearFilter;
  10314. this.magFilter = magFilter !== undefined ? magFilter : LinearFilter;
  10315. this.generateMipmaps = false;
  10316. const scope = this;
  10317. function updateVideo() {
  10318. scope.needsUpdate = true;
  10319. video.requestVideoFrameCallback( updateVideo );
  10320. }
  10321. if ( 'requestVideoFrameCallback' in video ) {
  10322. video.requestVideoFrameCallback( updateVideo );
  10323. }
  10324. }
  10325. clone() {
  10326. return new this.constructor( this.image ).copy( this );
  10327. }
  10328. update() {
  10329. const video = this.image;
  10330. const hasVideoFrameCallback = 'requestVideoFrameCallback' in video;
  10331. if ( hasVideoFrameCallback === false && video.readyState >= video.HAVE_CURRENT_DATA ) {
  10332. this.needsUpdate = true;
  10333. }
  10334. }
  10335. }
  10336. class FramebufferTexture extends Texture {
  10337. constructor( width, height ) {
  10338. super( { width, height } );
  10339. this.isFramebufferTexture = true;
  10340. this.magFilter = NearestFilter;
  10341. this.minFilter = NearestFilter;
  10342. this.generateMipmaps = false;
  10343. this.needsUpdate = true;
  10344. }
  10345. }
  10346. class CompressedTexture extends Texture {
  10347. constructor( mipmaps, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, colorSpace ) {
  10348. super( null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, colorSpace );
  10349. this.isCompressedTexture = true;
  10350. this.image = { width: width, height: height };
  10351. this.mipmaps = mipmaps;
  10352. // no flipping for cube textures
  10353. // (also flipping doesn't work for compressed textures )
  10354. this.flipY = false;
  10355. // can't generate mipmaps for compressed textures
  10356. // mips must be embedded in DDS files
  10357. this.generateMipmaps = false;
  10358. }
  10359. }
  10360. class CompressedArrayTexture extends CompressedTexture {
  10361. constructor( mipmaps, width, height, depth, format, type ) {
  10362. super( mipmaps, width, height, format, type );
  10363. this.isCompressedArrayTexture = true;
  10364. this.image.depth = depth;
  10365. this.wrapR = ClampToEdgeWrapping;
  10366. this.layerUpdates = new Set();
  10367. }
  10368. addLayerUpdate( layerIndex ) {
  10369. this.layerUpdates.add( layerIndex );
  10370. }
  10371. clearLayerUpdates() {
  10372. this.layerUpdates.clear();
  10373. }
  10374. }
  10375. class CompressedCubeTexture extends CompressedTexture {
  10376. constructor( images, format, type ) {
  10377. super( undefined, images[ 0 ].width, images[ 0 ].height, format, type, CubeReflectionMapping );
  10378. this.isCompressedCubeTexture = true;
  10379. this.isCubeTexture = true;
  10380. this.image = images;
  10381. }
  10382. }
  10383. class CanvasTexture extends Texture {
  10384. constructor( canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
  10385. super( canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
  10386. this.isCanvasTexture = true;
  10387. this.needsUpdate = true;
  10388. }
  10389. }
  10390. class DepthTexture extends Texture {
  10391. constructor( width, height, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, format = DepthFormat ) {
  10392. if ( format !== DepthFormat && format !== DepthStencilFormat ) {
  10393. throw new Error( 'DepthTexture format must be either THREE.DepthFormat or THREE.DepthStencilFormat' );
  10394. }
  10395. if ( type === undefined && format === DepthFormat ) type = UnsignedIntType;
  10396. if ( type === undefined && format === DepthStencilFormat ) type = UnsignedInt248Type;
  10397. super( null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
  10398. this.isDepthTexture = true;
  10399. this.image = { width: width, height: height };
  10400. this.magFilter = magFilter !== undefined ? magFilter : NearestFilter;
  10401. this.minFilter = minFilter !== undefined ? minFilter : NearestFilter;
  10402. this.flipY = false;
  10403. this.generateMipmaps = false;
  10404. this.compareFunction = null;
  10405. }
  10406. copy( source ) {
  10407. super.copy( source );
  10408. this.compareFunction = source.compareFunction;
  10409. return this;
  10410. }
  10411. toJSON( meta ) {
  10412. const data = super.toJSON( meta );
  10413. if ( this.compareFunction !== null ) data.compareFunction = this.compareFunction;
  10414. return data;
  10415. }
  10416. }
  10417. /**
  10418. * Extensible curve object.
  10419. *
  10420. * Some common of curve methods:
  10421. * .getPoint( t, optionalTarget ), .getTangent( t, optionalTarget )
  10422. * .getPointAt( u, optionalTarget ), .getTangentAt( u, optionalTarget )
  10423. * .getPoints(), .getSpacedPoints()
  10424. * .getLength()
  10425. * .updateArcLengths()
  10426. *
  10427. * This following curves inherit from THREE.Curve:
  10428. *
  10429. * -- 2D curves --
  10430. * THREE.ArcCurve
  10431. * THREE.CubicBezierCurve
  10432. * THREE.EllipseCurve
  10433. * THREE.LineCurve
  10434. * THREE.QuadraticBezierCurve
  10435. * THREE.SplineCurve
  10436. *
  10437. * -- 3D curves --
  10438. * THREE.CatmullRomCurve3
  10439. * THREE.CubicBezierCurve3
  10440. * THREE.LineCurve3
  10441. * THREE.QuadraticBezierCurve3
  10442. *
  10443. * A series of curves can be represented as a THREE.CurvePath.
  10444. *
  10445. **/
  10446. class Curve {
  10447. constructor() {
  10448. this.type = 'Curve';
  10449. this.arcLengthDivisions = 200;
  10450. }
  10451. // Virtual base class method to overwrite and implement in subclasses
  10452. // - t [0 .. 1]
  10453. getPoint( /* t, optionalTarget */ ) {
  10454. console.warn( 'THREE.Curve: .getPoint() not implemented.' );
  10455. return null;
  10456. }
  10457. // Get point at relative position in curve according to arc length
  10458. // - u [0 .. 1]
  10459. getPointAt( u, optionalTarget ) {
  10460. const t = this.getUtoTmapping( u );
  10461. return this.getPoint( t, optionalTarget );
  10462. }
  10463. // Get sequence of points using getPoint( t )
  10464. getPoints( divisions = 5 ) {
  10465. const points = [];
  10466. for ( let d = 0; d <= divisions; d ++ ) {
  10467. points.push( this.getPoint( d / divisions ) );
  10468. }
  10469. return points;
  10470. }
  10471. // Get sequence of points using getPointAt( u )
  10472. getSpacedPoints( divisions = 5 ) {
  10473. const points = [];
  10474. for ( let d = 0; d <= divisions; d ++ ) {
  10475. points.push( this.getPointAt( d / divisions ) );
  10476. }
  10477. return points;
  10478. }
  10479. // Get total curve arc length
  10480. getLength() {
  10481. const lengths = this.getLengths();
  10482. return lengths[ lengths.length - 1 ];
  10483. }
  10484. // Get list of cumulative segment lengths
  10485. getLengths( divisions = this.arcLengthDivisions ) {
  10486. if ( this.cacheArcLengths &&
  10487. ( this.cacheArcLengths.length === divisions + 1 ) &&
  10488. ! this.needsUpdate ) {
  10489. return this.cacheArcLengths;
  10490. }
  10491. this.needsUpdate = false;
  10492. const cache = [];
  10493. let current, last = this.getPoint( 0 );
  10494. let sum = 0;
  10495. cache.push( 0 );
  10496. for ( let p = 1; p <= divisions; p ++ ) {
  10497. current = this.getPoint( p / divisions );
  10498. sum += current.distanceTo( last );
  10499. cache.push( sum );
  10500. last = current;
  10501. }
  10502. this.cacheArcLengths = cache;
  10503. return cache; // { sums: cache, sum: sum }; Sum is in the last element.
  10504. }
  10505. updateArcLengths() {
  10506. this.needsUpdate = true;
  10507. this.getLengths();
  10508. }
  10509. // Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant
  10510. getUtoTmapping( u, distance ) {
  10511. const arcLengths = this.getLengths();
  10512. let i = 0;
  10513. const il = arcLengths.length;
  10514. let targetArcLength; // The targeted u distance value to get
  10515. if ( distance ) {
  10516. targetArcLength = distance;
  10517. } else {
  10518. targetArcLength = u * arcLengths[ il - 1 ];
  10519. }
  10520. // binary search for the index with largest value smaller than target u distance
  10521. let low = 0, high = il - 1, comparison;
  10522. while ( low <= high ) {
  10523. 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
  10524. comparison = arcLengths[ i ] - targetArcLength;
  10525. if ( comparison < 0 ) {
  10526. low = i + 1;
  10527. } else if ( comparison > 0 ) {
  10528. high = i - 1;
  10529. } else {
  10530. high = i;
  10531. break;
  10532. // DONE
  10533. }
  10534. }
  10535. i = high;
  10536. if ( arcLengths[ i ] === targetArcLength ) {
  10537. return i / ( il - 1 );
  10538. }
  10539. // we could get finer grain at lengths, or use simple interpolation between two points
  10540. const lengthBefore = arcLengths[ i ];
  10541. const lengthAfter = arcLengths[ i + 1 ];
  10542. const segmentLength = lengthAfter - lengthBefore;
  10543. // determine where we are between the 'before' and 'after' points
  10544. const segmentFraction = ( targetArcLength - lengthBefore ) / segmentLength;
  10545. // add that fractional amount to t
  10546. const t = ( i + segmentFraction ) / ( il - 1 );
  10547. return t;
  10548. }
  10549. // Returns a unit vector tangent at t
  10550. // In case any sub curve does not implement its tangent derivation,
  10551. // 2 points a small delta apart will be used to find its gradient
  10552. // which seems to give a reasonable approximation
  10553. getTangent( t, optionalTarget ) {
  10554. const delta = 0.0001;
  10555. let t1 = t - delta;
  10556. let t2 = t + delta;
  10557. // Capping in case of danger
  10558. if ( t1 < 0 ) t1 = 0;
  10559. if ( t2 > 1 ) t2 = 1;
  10560. const pt1 = this.getPoint( t1 );
  10561. const pt2 = this.getPoint( t2 );
  10562. const tangent = optionalTarget || ( ( pt1.isVector2 ) ? new Vector2() : new Vector3() );
  10563. tangent.copy( pt2 ).sub( pt1 ).normalize();
  10564. return tangent;
  10565. }
  10566. getTangentAt( u, optionalTarget ) {
  10567. const t = this.getUtoTmapping( u );
  10568. return this.getTangent( t, optionalTarget );
  10569. }
  10570. computeFrenetFrames( segments, closed ) {
  10571. // see http://www.cs.indiana.edu/pub/techreports/TR425.pdf
  10572. const normal = new Vector3();
  10573. const tangents = [];
  10574. const normals = [];
  10575. const binormals = [];
  10576. const vec = new Vector3();
  10577. const mat = new Matrix4();
  10578. // compute the tangent vectors for each segment on the curve
  10579. for ( let i = 0; i <= segments; i ++ ) {
  10580. const u = i / segments;
  10581. tangents[ i ] = this.getTangentAt( u, new Vector3() );
  10582. }
  10583. // select an initial normal vector perpendicular to the first tangent vector,
  10584. // and in the direction of the minimum tangent xyz component
  10585. normals[ 0 ] = new Vector3();
  10586. binormals[ 0 ] = new Vector3();
  10587. let min = Number.MAX_VALUE;
  10588. const tx = Math.abs( tangents[ 0 ].x );
  10589. const ty = Math.abs( tangents[ 0 ].y );
  10590. const tz = Math.abs( tangents[ 0 ].z );
  10591. if ( tx <= min ) {
  10592. min = tx;
  10593. normal.set( 1, 0, 0 );
  10594. }
  10595. if ( ty <= min ) {
  10596. min = ty;
  10597. normal.set( 0, 1, 0 );
  10598. }
  10599. if ( tz <= min ) {
  10600. normal.set( 0, 0, 1 );
  10601. }
  10602. vec.crossVectors( tangents[ 0 ], normal ).normalize();
  10603. normals[ 0 ].crossVectors( tangents[ 0 ], vec );
  10604. binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] );
  10605. // compute the slowly-varying normal and binormal vectors for each segment on the curve
  10606. for ( let i = 1; i <= segments; i ++ ) {
  10607. normals[ i ] = normals[ i - 1 ].clone();
  10608. binormals[ i ] = binormals[ i - 1 ].clone();
  10609. vec.crossVectors( tangents[ i - 1 ], tangents[ i ] );
  10610. if ( vec.length() > Number.EPSILON ) {
  10611. vec.normalize();
  10612. const theta = Math.acos( clamp( tangents[ i - 1 ].dot( tangents[ i ] ), - 1, 1 ) ); // clamp for floating pt errors
  10613. normals[ i ].applyMatrix4( mat.makeRotationAxis( vec, theta ) );
  10614. }
  10615. binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
  10616. }
  10617. // if the curve is closed, postprocess the vectors so the first and last normal vectors are the same
  10618. if ( closed === true ) {
  10619. let theta = Math.acos( clamp( normals[ 0 ].dot( normals[ segments ] ), - 1, 1 ) );
  10620. theta /= segments;
  10621. if ( tangents[ 0 ].dot( vec.crossVectors( normals[ 0 ], normals[ segments ] ) ) > 0 ) {
  10622. theta = - theta;
  10623. }
  10624. for ( let i = 1; i <= segments; i ++ ) {
  10625. // twist a little...
  10626. normals[ i ].applyMatrix4( mat.makeRotationAxis( tangents[ i ], theta * i ) );
  10627. binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
  10628. }
  10629. }
  10630. return {
  10631. tangents: tangents,
  10632. normals: normals,
  10633. binormals: binormals
  10634. };
  10635. }
  10636. clone() {
  10637. return new this.constructor().copy( this );
  10638. }
  10639. copy( source ) {
  10640. this.arcLengthDivisions = source.arcLengthDivisions;
  10641. return this;
  10642. }
  10643. toJSON() {
  10644. const data = {
  10645. metadata: {
  10646. version: 4.6,
  10647. type: 'Curve',
  10648. generator: 'Curve.toJSON'
  10649. }
  10650. };
  10651. data.arcLengthDivisions = this.arcLengthDivisions;
  10652. data.type = this.type;
  10653. return data;
  10654. }
  10655. fromJSON( json ) {
  10656. this.arcLengthDivisions = json.arcLengthDivisions;
  10657. return this;
  10658. }
  10659. }
  10660. class EllipseCurve extends Curve {
  10661. constructor( aX = 0, aY = 0, xRadius = 1, yRadius = 1, aStartAngle = 0, aEndAngle = Math.PI * 2, aClockwise = false, aRotation = 0 ) {
  10662. super();
  10663. this.isEllipseCurve = true;
  10664. this.type = 'EllipseCurve';
  10665. this.aX = aX;
  10666. this.aY = aY;
  10667. this.xRadius = xRadius;
  10668. this.yRadius = yRadius;
  10669. this.aStartAngle = aStartAngle;
  10670. this.aEndAngle = aEndAngle;
  10671. this.aClockwise = aClockwise;
  10672. this.aRotation = aRotation;
  10673. }
  10674. getPoint( t, optionalTarget = new Vector2() ) {
  10675. const point = optionalTarget;
  10676. const twoPi = Math.PI * 2;
  10677. let deltaAngle = this.aEndAngle - this.aStartAngle;
  10678. const samePoints = Math.abs( deltaAngle ) < Number.EPSILON;
  10679. // ensures that deltaAngle is 0 .. 2 PI
  10680. while ( deltaAngle < 0 ) deltaAngle += twoPi;
  10681. while ( deltaAngle > twoPi ) deltaAngle -= twoPi;
  10682. if ( deltaAngle < Number.EPSILON ) {
  10683. if ( samePoints ) {
  10684. deltaAngle = 0;
  10685. } else {
  10686. deltaAngle = twoPi;
  10687. }
  10688. }
  10689. if ( this.aClockwise === true && ! samePoints ) {
  10690. if ( deltaAngle === twoPi ) {
  10691. deltaAngle = - twoPi;
  10692. } else {
  10693. deltaAngle = deltaAngle - twoPi;
  10694. }
  10695. }
  10696. const angle = this.aStartAngle + t * deltaAngle;
  10697. let x = this.aX + this.xRadius * Math.cos( angle );
  10698. let y = this.aY + this.yRadius * Math.sin( angle );
  10699. if ( this.aRotation !== 0 ) {
  10700. const cos = Math.cos( this.aRotation );
  10701. const sin = Math.sin( this.aRotation );
  10702. const tx = x - this.aX;
  10703. const ty = y - this.aY;
  10704. // Rotate the point about the center of the ellipse.
  10705. x = tx * cos - ty * sin + this.aX;
  10706. y = tx * sin + ty * cos + this.aY;
  10707. }
  10708. return point.set( x, y );
  10709. }
  10710. copy( source ) {
  10711. super.copy( source );
  10712. this.aX = source.aX;
  10713. this.aY = source.aY;
  10714. this.xRadius = source.xRadius;
  10715. this.yRadius = source.yRadius;
  10716. this.aStartAngle = source.aStartAngle;
  10717. this.aEndAngle = source.aEndAngle;
  10718. this.aClockwise = source.aClockwise;
  10719. this.aRotation = source.aRotation;
  10720. return this;
  10721. }
  10722. toJSON() {
  10723. const data = super.toJSON();
  10724. data.aX = this.aX;
  10725. data.aY = this.aY;
  10726. data.xRadius = this.xRadius;
  10727. data.yRadius = this.yRadius;
  10728. data.aStartAngle = this.aStartAngle;
  10729. data.aEndAngle = this.aEndAngle;
  10730. data.aClockwise = this.aClockwise;
  10731. data.aRotation = this.aRotation;
  10732. return data;
  10733. }
  10734. fromJSON( json ) {
  10735. super.fromJSON( json );
  10736. this.aX = json.aX;
  10737. this.aY = json.aY;
  10738. this.xRadius = json.xRadius;
  10739. this.yRadius = json.yRadius;
  10740. this.aStartAngle = json.aStartAngle;
  10741. this.aEndAngle = json.aEndAngle;
  10742. this.aClockwise = json.aClockwise;
  10743. this.aRotation = json.aRotation;
  10744. return this;
  10745. }
  10746. }
  10747. class ArcCurve extends EllipseCurve {
  10748. constructor( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
  10749. super( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
  10750. this.isArcCurve = true;
  10751. this.type = 'ArcCurve';
  10752. }
  10753. }
  10754. /**
  10755. * Centripetal CatmullRom Curve - which is useful for avoiding
  10756. * cusps and self-intersections in non-uniform catmull rom curves.
  10757. * http://www.cemyuksel.com/research/catmullrom_param/catmullrom.pdf
  10758. *
  10759. * curve.type accepts centripetal(default), chordal and catmullrom
  10760. * curve.tension is used for catmullrom which defaults to 0.5
  10761. */
  10762. /*
  10763. Based on an optimized c++ solution in
  10764. - http://stackoverflow.com/questions/9489736/catmull-rom-curve-with-no-cusps-and-no-self-intersections/
  10765. - http://ideone.com/NoEbVM
  10766. This CubicPoly class could be used for reusing some variables and calculations,
  10767. but for three.js curve use, it could be possible inlined and flatten into a single function call
  10768. which can be placed in CurveUtils.
  10769. */
  10770. function CubicPoly() {
  10771. let c0 = 0, c1 = 0, c2 = 0, c3 = 0;
  10772. /*
  10773. * Compute coefficients for a cubic polynomial
  10774. * p(s) = c0 + c1*s + c2*s^2 + c3*s^3
  10775. * such that
  10776. * p(0) = x0, p(1) = x1
  10777. * and
  10778. * p'(0) = t0, p'(1) = t1.
  10779. */
  10780. function init( x0, x1, t0, t1 ) {
  10781. c0 = x0;
  10782. c1 = t0;
  10783. c2 = - 3 * x0 + 3 * x1 - 2 * t0 - t1;
  10784. c3 = 2 * x0 - 2 * x1 + t0 + t1;
  10785. }
  10786. return {
  10787. initCatmullRom: function ( x0, x1, x2, x3, tension ) {
  10788. init( x1, x2, tension * ( x2 - x0 ), tension * ( x3 - x1 ) );
  10789. },
  10790. initNonuniformCatmullRom: function ( x0, x1, x2, x3, dt0, dt1, dt2 ) {
  10791. // compute tangents when parameterized in [t1,t2]
  10792. let t1 = ( x1 - x0 ) / dt0 - ( x2 - x0 ) / ( dt0 + dt1 ) + ( x2 - x1 ) / dt1;
  10793. let t2 = ( x2 - x1 ) / dt1 - ( x3 - x1 ) / ( dt1 + dt2 ) + ( x3 - x2 ) / dt2;
  10794. // rescale tangents for parametrization in [0,1]
  10795. t1 *= dt1;
  10796. t2 *= dt1;
  10797. init( x1, x2, t1, t2 );
  10798. },
  10799. calc: function ( t ) {
  10800. const t2 = t * t;
  10801. const t3 = t2 * t;
  10802. return c0 + c1 * t + c2 * t2 + c3 * t3;
  10803. }
  10804. };
  10805. }
  10806. //
  10807. const tmp = /*@__PURE__*/ new Vector3();
  10808. const px = /*@__PURE__*/ new CubicPoly();
  10809. const py = /*@__PURE__*/ new CubicPoly();
  10810. const pz = /*@__PURE__*/ new CubicPoly();
  10811. class CatmullRomCurve3 extends Curve {
  10812. constructor( points = [], closed = false, curveType = 'centripetal', tension = 0.5 ) {
  10813. super();
  10814. this.isCatmullRomCurve3 = true;
  10815. this.type = 'CatmullRomCurve3';
  10816. this.points = points;
  10817. this.closed = closed;
  10818. this.curveType = curveType;
  10819. this.tension = tension;
  10820. }
  10821. getPoint( t, optionalTarget = new Vector3() ) {
  10822. const point = optionalTarget;
  10823. const points = this.points;
  10824. const l = points.length;
  10825. const p = ( l - ( this.closed ? 0 : 1 ) ) * t;
  10826. let intPoint = Math.floor( p );
  10827. let weight = p - intPoint;
  10828. if ( this.closed ) {
  10829. intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / l ) + 1 ) * l;
  10830. } else if ( weight === 0 && intPoint === l - 1 ) {
  10831. intPoint = l - 2;
  10832. weight = 1;
  10833. }
  10834. let p0, p3; // 4 points (p1 & p2 defined below)
  10835. if ( this.closed || intPoint > 0 ) {
  10836. p0 = points[ ( intPoint - 1 ) % l ];
  10837. } else {
  10838. // extrapolate first point
  10839. tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] );
  10840. p0 = tmp;
  10841. }
  10842. const p1 = points[ intPoint % l ];
  10843. const p2 = points[ ( intPoint + 1 ) % l ];
  10844. if ( this.closed || intPoint + 2 < l ) {
  10845. p3 = points[ ( intPoint + 2 ) % l ];
  10846. } else {
  10847. // extrapolate last point
  10848. tmp.subVectors( points[ l - 1 ], points[ l - 2 ] ).add( points[ l - 1 ] );
  10849. p3 = tmp;
  10850. }
  10851. if ( this.curveType === 'centripetal' || this.curveType === 'chordal' ) {
  10852. // init Centripetal / Chordal Catmull-Rom
  10853. const pow = this.curveType === 'chordal' ? 0.5 : 0.25;
  10854. let dt0 = Math.pow( p0.distanceToSquared( p1 ), pow );
  10855. let dt1 = Math.pow( p1.distanceToSquared( p2 ), pow );
  10856. let dt2 = Math.pow( p2.distanceToSquared( p3 ), pow );
  10857. // safety check for repeated points
  10858. if ( dt1 < 1e-4 ) dt1 = 1.0;
  10859. if ( dt0 < 1e-4 ) dt0 = dt1;
  10860. if ( dt2 < 1e-4 ) dt2 = dt1;
  10861. px.initNonuniformCatmullRom( p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2 );
  10862. py.initNonuniformCatmullRom( p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2 );
  10863. pz.initNonuniformCatmullRom( p0.z, p1.z, p2.z, p3.z, dt0, dt1, dt2 );
  10864. } else if ( this.curveType === 'catmullrom' ) {
  10865. px.initCatmullRom( p0.x, p1.x, p2.x, p3.x, this.tension );
  10866. py.initCatmullRom( p0.y, p1.y, p2.y, p3.y, this.tension );
  10867. pz.initCatmullRom( p0.z, p1.z, p2.z, p3.z, this.tension );
  10868. }
  10869. point.set(
  10870. px.calc( weight ),
  10871. py.calc( weight ),
  10872. pz.calc( weight )
  10873. );
  10874. return point;
  10875. }
  10876. copy( source ) {
  10877. super.copy( source );
  10878. this.points = [];
  10879. for ( let i = 0, l = source.points.length; i < l; i ++ ) {
  10880. const point = source.points[ i ];
  10881. this.points.push( point.clone() );
  10882. }
  10883. this.closed = source.closed;
  10884. this.curveType = source.curveType;
  10885. this.tension = source.tension;
  10886. return this;
  10887. }
  10888. toJSON() {
  10889. const data = super.toJSON();
  10890. data.points = [];
  10891. for ( let i = 0, l = this.points.length; i < l; i ++ ) {
  10892. const point = this.points[ i ];
  10893. data.points.push( point.toArray() );
  10894. }
  10895. data.closed = this.closed;
  10896. data.curveType = this.curveType;
  10897. data.tension = this.tension;
  10898. return data;
  10899. }
  10900. fromJSON( json ) {
  10901. super.fromJSON( json );
  10902. this.points = [];
  10903. for ( let i = 0, l = json.points.length; i < l; i ++ ) {
  10904. const point = json.points[ i ];
  10905. this.points.push( new Vector3().fromArray( point ) );
  10906. }
  10907. this.closed = json.closed;
  10908. this.curveType = json.curveType;
  10909. this.tension = json.tension;
  10910. return this;
  10911. }
  10912. }
  10913. /**
  10914. * Bezier Curves formulas obtained from
  10915. * https://en.wikipedia.org/wiki/B%C3%A9zier_curve
  10916. */
  10917. function CatmullRom( t, p0, p1, p2, p3 ) {
  10918. const v0 = ( p2 - p0 ) * 0.5;
  10919. const v1 = ( p3 - p1 ) * 0.5;
  10920. const t2 = t * t;
  10921. const t3 = t * t2;
  10922. return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  10923. }
  10924. //
  10925. function QuadraticBezierP0( t, p ) {
  10926. const k = 1 - t;
  10927. return k * k * p;
  10928. }
  10929. function QuadraticBezierP1( t, p ) {
  10930. return 2 * ( 1 - t ) * t * p;
  10931. }
  10932. function QuadraticBezierP2( t, p ) {
  10933. return t * t * p;
  10934. }
  10935. function QuadraticBezier( t, p0, p1, p2 ) {
  10936. return QuadraticBezierP0( t, p0 ) + QuadraticBezierP1( t, p1 ) +
  10937. QuadraticBezierP2( t, p2 );
  10938. }
  10939. //
  10940. function CubicBezierP0( t, p ) {
  10941. const k = 1 - t;
  10942. return k * k * k * p;
  10943. }
  10944. function CubicBezierP1( t, p ) {
  10945. const k = 1 - t;
  10946. return 3 * k * k * t * p;
  10947. }
  10948. function CubicBezierP2( t, p ) {
  10949. return 3 * ( 1 - t ) * t * t * p;
  10950. }
  10951. function CubicBezierP3( t, p ) {
  10952. return t * t * t * p;
  10953. }
  10954. function CubicBezier( t, p0, p1, p2, p3 ) {
  10955. return CubicBezierP0( t, p0 ) + CubicBezierP1( t, p1 ) + CubicBezierP2( t, p2 ) +
  10956. CubicBezierP3( t, p3 );
  10957. }
  10958. class CubicBezierCurve extends Curve {
  10959. constructor( v0 = new Vector2(), v1 = new Vector2(), v2 = new Vector2(), v3 = new Vector2() ) {
  10960. super();
  10961. this.isCubicBezierCurve = true;
  10962. this.type = 'CubicBezierCurve';
  10963. this.v0 = v0;
  10964. this.v1 = v1;
  10965. this.v2 = v2;
  10966. this.v3 = v3;
  10967. }
  10968. getPoint( t, optionalTarget = new Vector2() ) {
  10969. const point = optionalTarget;
  10970. const v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
  10971. point.set(
  10972. CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
  10973. CubicBezier( t, v0.y, v1.y, v2.y, v3.y )
  10974. );
  10975. return point;
  10976. }
  10977. copy( source ) {
  10978. super.copy( source );
  10979. this.v0.copy( source.v0 );
  10980. this.v1.copy( source.v1 );
  10981. this.v2.copy( source.v2 );
  10982. this.v3.copy( source.v3 );
  10983. return this;
  10984. }
  10985. toJSON() {
  10986. const data = super.toJSON();
  10987. data.v0 = this.v0.toArray();
  10988. data.v1 = this.v1.toArray();
  10989. data.v2 = this.v2.toArray();
  10990. data.v3 = this.v3.toArray();
  10991. return data;
  10992. }
  10993. fromJSON( json ) {
  10994. super.fromJSON( json );
  10995. this.v0.fromArray( json.v0 );
  10996. this.v1.fromArray( json.v1 );
  10997. this.v2.fromArray( json.v2 );
  10998. this.v3.fromArray( json.v3 );
  10999. return this;
  11000. }
  11001. }
  11002. class CubicBezierCurve3 extends Curve {
  11003. constructor( v0 = new Vector3(), v1 = new Vector3(), v2 = new Vector3(), v3 = new Vector3() ) {
  11004. super();
  11005. this.isCubicBezierCurve3 = true;
  11006. this.type = 'CubicBezierCurve3';
  11007. this.v0 = v0;
  11008. this.v1 = v1;
  11009. this.v2 = v2;
  11010. this.v3 = v3;
  11011. }
  11012. getPoint( t, optionalTarget = new Vector3() ) {
  11013. const point = optionalTarget;
  11014. const v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
  11015. point.set(
  11016. CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
  11017. CubicBezier( t, v0.y, v1.y, v2.y, v3.y ),
  11018. CubicBezier( t, v0.z, v1.z, v2.z, v3.z )
  11019. );
  11020. return point;
  11021. }
  11022. copy( source ) {
  11023. super.copy( source );
  11024. this.v0.copy( source.v0 );
  11025. this.v1.copy( source.v1 );
  11026. this.v2.copy( source.v2 );
  11027. this.v3.copy( source.v3 );
  11028. return this;
  11029. }
  11030. toJSON() {
  11031. const data = super.toJSON();
  11032. data.v0 = this.v0.toArray();
  11033. data.v1 = this.v1.toArray();
  11034. data.v2 = this.v2.toArray();
  11035. data.v3 = this.v3.toArray();
  11036. return data;
  11037. }
  11038. fromJSON( json ) {
  11039. super.fromJSON( json );
  11040. this.v0.fromArray( json.v0 );
  11041. this.v1.fromArray( json.v1 );
  11042. this.v2.fromArray( json.v2 );
  11043. this.v3.fromArray( json.v3 );
  11044. return this;
  11045. }
  11046. }
  11047. class LineCurve extends Curve {
  11048. constructor( v1 = new Vector2(), v2 = new Vector2() ) {
  11049. super();
  11050. this.isLineCurve = true;
  11051. this.type = 'LineCurve';
  11052. this.v1 = v1;
  11053. this.v2 = v2;
  11054. }
  11055. getPoint( t, optionalTarget = new Vector2() ) {
  11056. const point = optionalTarget;
  11057. if ( t === 1 ) {
  11058. point.copy( this.v2 );
  11059. } else {
  11060. point.copy( this.v2 ).sub( this.v1 );
  11061. point.multiplyScalar( t ).add( this.v1 );
  11062. }
  11063. return point;
  11064. }
  11065. // Line curve is linear, so we can overwrite default getPointAt
  11066. getPointAt( u, optionalTarget ) {
  11067. return this.getPoint( u, optionalTarget );
  11068. }
  11069. getTangent( t, optionalTarget = new Vector2() ) {
  11070. return optionalTarget.subVectors( this.v2, this.v1 ).normalize();
  11071. }
  11072. getTangentAt( u, optionalTarget ) {
  11073. return this.getTangent( u, optionalTarget );
  11074. }
  11075. copy( source ) {
  11076. super.copy( source );
  11077. this.v1.copy( source.v1 );
  11078. this.v2.copy( source.v2 );
  11079. return this;
  11080. }
  11081. toJSON() {
  11082. const data = super.toJSON();
  11083. data.v1 = this.v1.toArray();
  11084. data.v2 = this.v2.toArray();
  11085. return data;
  11086. }
  11087. fromJSON( json ) {
  11088. super.fromJSON( json );
  11089. this.v1.fromArray( json.v1 );
  11090. this.v2.fromArray( json.v2 );
  11091. return this;
  11092. }
  11093. }
  11094. class LineCurve3 extends Curve {
  11095. constructor( v1 = new Vector3(), v2 = new Vector3() ) {
  11096. super();
  11097. this.isLineCurve3 = true;
  11098. this.type = 'LineCurve3';
  11099. this.v1 = v1;
  11100. this.v2 = v2;
  11101. }
  11102. getPoint( t, optionalTarget = new Vector3() ) {
  11103. const point = optionalTarget;
  11104. if ( t === 1 ) {
  11105. point.copy( this.v2 );
  11106. } else {
  11107. point.copy( this.v2 ).sub( this.v1 );
  11108. point.multiplyScalar( t ).add( this.v1 );
  11109. }
  11110. return point;
  11111. }
  11112. // Line curve is linear, so we can overwrite default getPointAt
  11113. getPointAt( u, optionalTarget ) {
  11114. return this.getPoint( u, optionalTarget );
  11115. }
  11116. getTangent( t, optionalTarget = new Vector3() ) {
  11117. return optionalTarget.subVectors( this.v2, this.v1 ).normalize();
  11118. }
  11119. getTangentAt( u, optionalTarget ) {
  11120. return this.getTangent( u, optionalTarget );
  11121. }
  11122. copy( source ) {
  11123. super.copy( source );
  11124. this.v1.copy( source.v1 );
  11125. this.v2.copy( source.v2 );
  11126. return this;
  11127. }
  11128. toJSON() {
  11129. const data = super.toJSON();
  11130. data.v1 = this.v1.toArray();
  11131. data.v2 = this.v2.toArray();
  11132. return data;
  11133. }
  11134. fromJSON( json ) {
  11135. super.fromJSON( json );
  11136. this.v1.fromArray( json.v1 );
  11137. this.v2.fromArray( json.v2 );
  11138. return this;
  11139. }
  11140. }
  11141. class QuadraticBezierCurve extends Curve {
  11142. constructor( v0 = new Vector2(), v1 = new Vector2(), v2 = new Vector2() ) {
  11143. super();
  11144. this.isQuadraticBezierCurve = true;
  11145. this.type = 'QuadraticBezierCurve';
  11146. this.v0 = v0;
  11147. this.v1 = v1;
  11148. this.v2 = v2;
  11149. }
  11150. getPoint( t, optionalTarget = new Vector2() ) {
  11151. const point = optionalTarget;
  11152. const v0 = this.v0, v1 = this.v1, v2 = this.v2;
  11153. point.set(
  11154. QuadraticBezier( t, v0.x, v1.x, v2.x ),
  11155. QuadraticBezier( t, v0.y, v1.y, v2.y )
  11156. );
  11157. return point;
  11158. }
  11159. copy( source ) {
  11160. super.copy( source );
  11161. this.v0.copy( source.v0 );
  11162. this.v1.copy( source.v1 );
  11163. this.v2.copy( source.v2 );
  11164. return this;
  11165. }
  11166. toJSON() {
  11167. const data = super.toJSON();
  11168. data.v0 = this.v0.toArray();
  11169. data.v1 = this.v1.toArray();
  11170. data.v2 = this.v2.toArray();
  11171. return data;
  11172. }
  11173. fromJSON( json ) {
  11174. super.fromJSON( json );
  11175. this.v0.fromArray( json.v0 );
  11176. this.v1.fromArray( json.v1 );
  11177. this.v2.fromArray( json.v2 );
  11178. return this;
  11179. }
  11180. }
  11181. class QuadraticBezierCurve3 extends Curve {
  11182. constructor( v0 = new Vector3(), v1 = new Vector3(), v2 = new Vector3() ) {
  11183. super();
  11184. this.isQuadraticBezierCurve3 = true;
  11185. this.type = 'QuadraticBezierCurve3';
  11186. this.v0 = v0;
  11187. this.v1 = v1;
  11188. this.v2 = v2;
  11189. }
  11190. getPoint( t, optionalTarget = new Vector3() ) {
  11191. const point = optionalTarget;
  11192. const v0 = this.v0, v1 = this.v1, v2 = this.v2;
  11193. point.set(
  11194. QuadraticBezier( t, v0.x, v1.x, v2.x ),
  11195. QuadraticBezier( t, v0.y, v1.y, v2.y ),
  11196. QuadraticBezier( t, v0.z, v1.z, v2.z )
  11197. );
  11198. return point;
  11199. }
  11200. copy( source ) {
  11201. super.copy( source );
  11202. this.v0.copy( source.v0 );
  11203. this.v1.copy( source.v1 );
  11204. this.v2.copy( source.v2 );
  11205. return this;
  11206. }
  11207. toJSON() {
  11208. const data = super.toJSON();
  11209. data.v0 = this.v0.toArray();
  11210. data.v1 = this.v1.toArray();
  11211. data.v2 = this.v2.toArray();
  11212. return data;
  11213. }
  11214. fromJSON( json ) {
  11215. super.fromJSON( json );
  11216. this.v0.fromArray( json.v0 );
  11217. this.v1.fromArray( json.v1 );
  11218. this.v2.fromArray( json.v2 );
  11219. return this;
  11220. }
  11221. }
  11222. class SplineCurve extends Curve {
  11223. constructor( points = [] ) {
  11224. super();
  11225. this.isSplineCurve = true;
  11226. this.type = 'SplineCurve';
  11227. this.points = points;
  11228. }
  11229. getPoint( t, optionalTarget = new Vector2() ) {
  11230. const point = optionalTarget;
  11231. const points = this.points;
  11232. const p = ( points.length - 1 ) * t;
  11233. const intPoint = Math.floor( p );
  11234. const weight = p - intPoint;
  11235. const p0 = points[ intPoint === 0 ? intPoint : intPoint - 1 ];
  11236. const p1 = points[ intPoint ];
  11237. const p2 = points[ intPoint > points.length - 2 ? points.length - 1 : intPoint + 1 ];
  11238. const p3 = points[ intPoint > points.length - 3 ? points.length - 1 : intPoint + 2 ];
  11239. point.set(
  11240. CatmullRom( weight, p0.x, p1.x, p2.x, p3.x ),
  11241. CatmullRom( weight, p0.y, p1.y, p2.y, p3.y )
  11242. );
  11243. return point;
  11244. }
  11245. copy( source ) {
  11246. super.copy( source );
  11247. this.points = [];
  11248. for ( let i = 0, l = source.points.length; i < l; i ++ ) {
  11249. const point = source.points[ i ];
  11250. this.points.push( point.clone() );
  11251. }
  11252. return this;
  11253. }
  11254. toJSON() {
  11255. const data = super.toJSON();
  11256. data.points = [];
  11257. for ( let i = 0, l = this.points.length; i < l; i ++ ) {
  11258. const point = this.points[ i ];
  11259. data.points.push( point.toArray() );
  11260. }
  11261. return data;
  11262. }
  11263. fromJSON( json ) {
  11264. super.fromJSON( json );
  11265. this.points = [];
  11266. for ( let i = 0, l = json.points.length; i < l; i ++ ) {
  11267. const point = json.points[ i ];
  11268. this.points.push( new Vector2().fromArray( point ) );
  11269. }
  11270. return this;
  11271. }
  11272. }
  11273. var Curves = /*#__PURE__*/Object.freeze({
  11274. __proto__: null,
  11275. ArcCurve: ArcCurve,
  11276. CatmullRomCurve3: CatmullRomCurve3,
  11277. CubicBezierCurve: CubicBezierCurve,
  11278. CubicBezierCurve3: CubicBezierCurve3,
  11279. EllipseCurve: EllipseCurve,
  11280. LineCurve: LineCurve,
  11281. LineCurve3: LineCurve3,
  11282. QuadraticBezierCurve: QuadraticBezierCurve,
  11283. QuadraticBezierCurve3: QuadraticBezierCurve3,
  11284. SplineCurve: SplineCurve
  11285. });
  11286. /**************************************************************
  11287. * Curved Path - a curve path is simply a array of connected
  11288. * curves, but retains the api of a curve
  11289. **************************************************************/
  11290. class CurvePath extends Curve {
  11291. constructor() {
  11292. super();
  11293. this.type = 'CurvePath';
  11294. this.curves = [];
  11295. this.autoClose = false; // Automatically closes the path
  11296. }
  11297. add( curve ) {
  11298. this.curves.push( curve );
  11299. }
  11300. closePath() {
  11301. // Add a line curve if start and end of lines are not connected
  11302. const startPoint = this.curves[ 0 ].getPoint( 0 );
  11303. const endPoint = this.curves[ this.curves.length - 1 ].getPoint( 1 );
  11304. if ( ! startPoint.equals( endPoint ) ) {
  11305. const lineType = ( startPoint.isVector2 === true ) ? 'LineCurve' : 'LineCurve3';
  11306. this.curves.push( new Curves[ lineType ]( endPoint, startPoint ) );
  11307. }
  11308. return this;
  11309. }
  11310. // To get accurate point with reference to
  11311. // entire path distance at time t,
  11312. // following has to be done:
  11313. // 1. Length of each sub path have to be known
  11314. // 2. Locate and identify type of curve
  11315. // 3. Get t for the curve
  11316. // 4. Return curve.getPointAt(t')
  11317. getPoint( t, optionalTarget ) {
  11318. const d = t * this.getLength();
  11319. const curveLengths = this.getCurveLengths();
  11320. let i = 0;
  11321. // To think about boundaries points.
  11322. while ( i < curveLengths.length ) {
  11323. if ( curveLengths[ i ] >= d ) {
  11324. const diff = curveLengths[ i ] - d;
  11325. const curve = this.curves[ i ];
  11326. const segmentLength = curve.getLength();
  11327. const u = segmentLength === 0 ? 0 : 1 - diff / segmentLength;
  11328. return curve.getPointAt( u, optionalTarget );
  11329. }
  11330. i ++;
  11331. }
  11332. return null;
  11333. // loop where sum != 0, sum > d , sum+1 <d
  11334. }
  11335. // We cannot use the default THREE.Curve getPoint() with getLength() because in
  11336. // THREE.Curve, getLength() depends on getPoint() but in THREE.CurvePath
  11337. // getPoint() depends on getLength
  11338. getLength() {
  11339. const lens = this.getCurveLengths();
  11340. return lens[ lens.length - 1 ];
  11341. }
  11342. // cacheLengths must be recalculated.
  11343. updateArcLengths() {
  11344. this.needsUpdate = true;
  11345. this.cacheLengths = null;
  11346. this.getCurveLengths();
  11347. }
  11348. // Compute lengths and cache them
  11349. // We cannot overwrite getLengths() because UtoT mapping uses it.
  11350. getCurveLengths() {
  11351. // We use cache values if curves and cache array are same length
  11352. if ( this.cacheLengths && this.cacheLengths.length === this.curves.length ) {
  11353. return this.cacheLengths;
  11354. }
  11355. // Get length of sub-curve
  11356. // Push sums into cached array
  11357. const lengths = [];
  11358. let sums = 0;
  11359. for ( let i = 0, l = this.curves.length; i < l; i ++ ) {
  11360. sums += this.curves[ i ].getLength();
  11361. lengths.push( sums );
  11362. }
  11363. this.cacheLengths = lengths;
  11364. return lengths;
  11365. }
  11366. getSpacedPoints( divisions = 40 ) {
  11367. const points = [];
  11368. for ( let i = 0; i <= divisions; i ++ ) {
  11369. points.push( this.getPoint( i / divisions ) );
  11370. }
  11371. if ( this.autoClose ) {
  11372. points.push( points[ 0 ] );
  11373. }
  11374. return points;
  11375. }
  11376. getPoints( divisions = 12 ) {
  11377. const points = [];
  11378. let last;
  11379. for ( let i = 0, curves = this.curves; i < curves.length; i ++ ) {
  11380. const curve = curves[ i ];
  11381. const resolution = curve.isEllipseCurve ? divisions * 2
  11382. : ( curve.isLineCurve || curve.isLineCurve3 ) ? 1
  11383. : curve.isSplineCurve ? divisions * curve.points.length
  11384. : divisions;
  11385. const pts = curve.getPoints( resolution );
  11386. for ( let j = 0; j < pts.length; j ++ ) {
  11387. const point = pts[ j ];
  11388. if ( last && last.equals( point ) ) continue; // ensures no consecutive points are duplicates
  11389. points.push( point );
  11390. last = point;
  11391. }
  11392. }
  11393. if ( this.autoClose && points.length > 1 && ! points[ points.length - 1 ].equals( points[ 0 ] ) ) {
  11394. points.push( points[ 0 ] );
  11395. }
  11396. return points;
  11397. }
  11398. copy( source ) {
  11399. super.copy( source );
  11400. this.curves = [];
  11401. for ( let i = 0, l = source.curves.length; i < l; i ++ ) {
  11402. const curve = source.curves[ i ];
  11403. this.curves.push( curve.clone() );
  11404. }
  11405. this.autoClose = source.autoClose;
  11406. return this;
  11407. }
  11408. toJSON() {
  11409. const data = super.toJSON();
  11410. data.autoClose = this.autoClose;
  11411. data.curves = [];
  11412. for ( let i = 0, l = this.curves.length; i < l; i ++ ) {
  11413. const curve = this.curves[ i ];
  11414. data.curves.push( curve.toJSON() );
  11415. }
  11416. return data;
  11417. }
  11418. fromJSON( json ) {
  11419. super.fromJSON( json );
  11420. this.autoClose = json.autoClose;
  11421. this.curves = [];
  11422. for ( let i = 0, l = json.curves.length; i < l; i ++ ) {
  11423. const curve = json.curves[ i ];
  11424. this.curves.push( new Curves[ curve.type ]().fromJSON( curve ) );
  11425. }
  11426. return this;
  11427. }
  11428. }
  11429. class Path extends CurvePath {
  11430. constructor( points ) {
  11431. super();
  11432. this.type = 'Path';
  11433. this.currentPoint = new Vector2();
  11434. if ( points ) {
  11435. this.setFromPoints( points );
  11436. }
  11437. }
  11438. setFromPoints( points ) {
  11439. this.moveTo( points[ 0 ].x, points[ 0 ].y );
  11440. for ( let i = 1, l = points.length; i < l; i ++ ) {
  11441. this.lineTo( points[ i ].x, points[ i ].y );
  11442. }
  11443. return this;
  11444. }
  11445. moveTo( x, y ) {
  11446. this.currentPoint.set( x, y ); // TODO consider referencing vectors instead of copying?
  11447. return this;
  11448. }
  11449. lineTo( x, y ) {
  11450. const curve = new LineCurve( this.currentPoint.clone(), new Vector2( x, y ) );
  11451. this.curves.push( curve );
  11452. this.currentPoint.set( x, y );
  11453. return this;
  11454. }
  11455. quadraticCurveTo( aCPx, aCPy, aX, aY ) {
  11456. const curve = new QuadraticBezierCurve(
  11457. this.currentPoint.clone(),
  11458. new Vector2( aCPx, aCPy ),
  11459. new Vector2( aX, aY )
  11460. );
  11461. this.curves.push( curve );
  11462. this.currentPoint.set( aX, aY );
  11463. return this;
  11464. }
  11465. bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
  11466. const curve = new CubicBezierCurve(
  11467. this.currentPoint.clone(),
  11468. new Vector2( aCP1x, aCP1y ),
  11469. new Vector2( aCP2x, aCP2y ),
  11470. new Vector2( aX, aY )
  11471. );
  11472. this.curves.push( curve );
  11473. this.currentPoint.set( aX, aY );
  11474. return this;
  11475. }
  11476. splineThru( pts /*Array of Vector*/ ) {
  11477. const npts = [ this.currentPoint.clone() ].concat( pts );
  11478. const curve = new SplineCurve( npts );
  11479. this.curves.push( curve );
  11480. this.currentPoint.copy( pts[ pts.length - 1 ] );
  11481. return this;
  11482. }
  11483. arc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
  11484. const x0 = this.currentPoint.x;
  11485. const y0 = this.currentPoint.y;
  11486. this.absarc( aX + x0, aY + y0, aRadius,
  11487. aStartAngle, aEndAngle, aClockwise );
  11488. return this;
  11489. }
  11490. absarc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
  11491. this.absellipse( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
  11492. return this;
  11493. }
  11494. ellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
  11495. const x0 = this.currentPoint.x;
  11496. const y0 = this.currentPoint.y;
  11497. this.absellipse( aX + x0, aY + y0, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
  11498. return this;
  11499. }
  11500. absellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
  11501. const curve = new EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
  11502. if ( this.curves.length > 0 ) {
  11503. // if a previous curve is present, attempt to join
  11504. const firstPoint = curve.getPoint( 0 );
  11505. if ( ! firstPoint.equals( this.currentPoint ) ) {
  11506. this.lineTo( firstPoint.x, firstPoint.y );
  11507. }
  11508. }
  11509. this.curves.push( curve );
  11510. const lastPoint = curve.getPoint( 1 );
  11511. this.currentPoint.copy( lastPoint );
  11512. return this;
  11513. }
  11514. copy( source ) {
  11515. super.copy( source );
  11516. this.currentPoint.copy( source.currentPoint );
  11517. return this;
  11518. }
  11519. toJSON() {
  11520. const data = super.toJSON();
  11521. data.currentPoint = this.currentPoint.toArray();
  11522. return data;
  11523. }
  11524. fromJSON( json ) {
  11525. super.fromJSON( json );
  11526. this.currentPoint.fromArray( json.currentPoint );
  11527. return this;
  11528. }
  11529. }
  11530. class LatheGeometry extends BufferGeometry {
  11531. 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 ) {
  11532. super();
  11533. this.type = 'LatheGeometry';
  11534. this.parameters = {
  11535. points: points,
  11536. segments: segments,
  11537. phiStart: phiStart,
  11538. phiLength: phiLength
  11539. };
  11540. segments = Math.floor( segments );
  11541. // clamp phiLength so it's in range of [ 0, 2PI ]
  11542. phiLength = clamp( phiLength, 0, Math.PI * 2 );
  11543. // buffers
  11544. const indices = [];
  11545. const vertices = [];
  11546. const uvs = [];
  11547. const initNormals = [];
  11548. const normals = [];
  11549. // helper variables
  11550. const inverseSegments = 1.0 / segments;
  11551. const vertex = new Vector3();
  11552. const uv = new Vector2();
  11553. const normal = new Vector3();
  11554. const curNormal = new Vector3();
  11555. const prevNormal = new Vector3();
  11556. let dx = 0;
  11557. let dy = 0;
  11558. // pre-compute normals for initial "meridian"
  11559. for ( let j = 0; j <= ( points.length - 1 ); j ++ ) {
  11560. switch ( j ) {
  11561. case 0: // special handling for 1st vertex on path
  11562. dx = points[ j + 1 ].x - points[ j ].x;
  11563. dy = points[ j + 1 ].y - points[ j ].y;
  11564. normal.x = dy * 1.0;
  11565. normal.y = - dx;
  11566. normal.z = dy * 0.0;
  11567. prevNormal.copy( normal );
  11568. normal.normalize();
  11569. initNormals.push( normal.x, normal.y, normal.z );
  11570. break;
  11571. case ( points.length - 1 ): // special handling for last Vertex on path
  11572. initNormals.push( prevNormal.x, prevNormal.y, prevNormal.z );
  11573. break;
  11574. default: // default handling for all vertices in between
  11575. dx = points[ j + 1 ].x - points[ j ].x;
  11576. dy = points[ j + 1 ].y - points[ j ].y;
  11577. normal.x = dy * 1.0;
  11578. normal.y = - dx;
  11579. normal.z = dy * 0.0;
  11580. curNormal.copy( normal );
  11581. normal.x += prevNormal.x;
  11582. normal.y += prevNormal.y;
  11583. normal.z += prevNormal.z;
  11584. normal.normalize();
  11585. initNormals.push( normal.x, normal.y, normal.z );
  11586. prevNormal.copy( curNormal );
  11587. }
  11588. }
  11589. // generate vertices, uvs and normals
  11590. for ( let i = 0; i <= segments; i ++ ) {
  11591. const phi = phiStart + i * inverseSegments * phiLength;
  11592. const sin = Math.sin( phi );
  11593. const cos = Math.cos( phi );
  11594. for ( let j = 0; j <= ( points.length - 1 ); j ++ ) {
  11595. // vertex
  11596. vertex.x = points[ j ].x * sin;
  11597. vertex.y = points[ j ].y;
  11598. vertex.z = points[ j ].x * cos;
  11599. vertices.push( vertex.x, vertex.y, vertex.z );
  11600. // uv
  11601. uv.x = i / segments;
  11602. uv.y = j / ( points.length - 1 );
  11603. uvs.push( uv.x, uv.y );
  11604. // normal
  11605. const x = initNormals[ 3 * j + 0 ] * sin;
  11606. const y = initNormals[ 3 * j + 1 ];
  11607. const z = initNormals[ 3 * j + 0 ] * cos;
  11608. normals.push( x, y, z );
  11609. }
  11610. }
  11611. // indices
  11612. for ( let i = 0; i < segments; i ++ ) {
  11613. for ( let j = 0; j < ( points.length - 1 ); j ++ ) {
  11614. const base = j + i * points.length;
  11615. const a = base;
  11616. const b = base + points.length;
  11617. const c = base + points.length + 1;
  11618. const d = base + 1;
  11619. // faces
  11620. indices.push( a, b, d );
  11621. indices.push( c, d, b );
  11622. }
  11623. }
  11624. // build geometry
  11625. this.setIndex( indices );
  11626. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  11627. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  11628. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  11629. }
  11630. copy( source ) {
  11631. super.copy( source );
  11632. this.parameters = Object.assign( {}, source.parameters );
  11633. return this;
  11634. }
  11635. static fromJSON( data ) {
  11636. return new LatheGeometry( data.points, data.segments, data.phiStart, data.phiLength );
  11637. }
  11638. }
  11639. class CapsuleGeometry extends LatheGeometry {
  11640. constructor( radius = 1, length = 1, capSegments = 4, radialSegments = 8 ) {
  11641. const path = new Path();
  11642. path.absarc( 0, - length / 2, radius, Math.PI * 1.5, 0 );
  11643. path.absarc( 0, length / 2, radius, 0, Math.PI * 0.5 );
  11644. super( path.getPoints( capSegments ), radialSegments );
  11645. this.type = 'CapsuleGeometry';
  11646. this.parameters = {
  11647. radius: radius,
  11648. length: length,
  11649. capSegments: capSegments,
  11650. radialSegments: radialSegments,
  11651. };
  11652. }
  11653. static fromJSON( data ) {
  11654. return new CapsuleGeometry( data.radius, data.length, data.capSegments, data.radialSegments );
  11655. }
  11656. }
  11657. class CircleGeometry extends BufferGeometry {
  11658. constructor( radius = 1, segments = 32, thetaStart = 0, thetaLength = Math.PI * 2 ) {
  11659. super();
  11660. this.type = 'CircleGeometry';
  11661. this.parameters = {
  11662. radius: radius,
  11663. segments: segments,
  11664. thetaStart: thetaStart,
  11665. thetaLength: thetaLength
  11666. };
  11667. segments = Math.max( 3, segments );
  11668. // buffers
  11669. const indices = [];
  11670. const vertices = [];
  11671. const normals = [];
  11672. const uvs = [];
  11673. // helper variables
  11674. const vertex = new Vector3();
  11675. const uv = new Vector2();
  11676. // center point
  11677. vertices.push( 0, 0, 0 );
  11678. normals.push( 0, 0, 1 );
  11679. uvs.push( 0.5, 0.5 );
  11680. for ( let s = 0, i = 3; s <= segments; s ++, i += 3 ) {
  11681. const segment = thetaStart + s / segments * thetaLength;
  11682. // vertex
  11683. vertex.x = radius * Math.cos( segment );
  11684. vertex.y = radius * Math.sin( segment );
  11685. vertices.push( vertex.x, vertex.y, vertex.z );
  11686. // normal
  11687. normals.push( 0, 0, 1 );
  11688. // uvs
  11689. uv.x = ( vertices[ i ] / radius + 1 ) / 2;
  11690. uv.y = ( vertices[ i + 1 ] / radius + 1 ) / 2;
  11691. uvs.push( uv.x, uv.y );
  11692. }
  11693. // indices
  11694. for ( let i = 1; i <= segments; i ++ ) {
  11695. indices.push( i, i + 1, 0 );
  11696. }
  11697. // build geometry
  11698. this.setIndex( indices );
  11699. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  11700. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  11701. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  11702. }
  11703. copy( source ) {
  11704. super.copy( source );
  11705. this.parameters = Object.assign( {}, source.parameters );
  11706. return this;
  11707. }
  11708. static fromJSON( data ) {
  11709. return new CircleGeometry( data.radius, data.segments, data.thetaStart, data.thetaLength );
  11710. }
  11711. }
  11712. class CylinderGeometry extends BufferGeometry {
  11713. constructor( radiusTop = 1, radiusBottom = 1, height = 1, radialSegments = 32, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 ) {
  11714. super();
  11715. this.type = 'CylinderGeometry';
  11716. this.parameters = {
  11717. radiusTop: radiusTop,
  11718. radiusBottom: radiusBottom,
  11719. height: height,
  11720. radialSegments: radialSegments,
  11721. heightSegments: heightSegments,
  11722. openEnded: openEnded,
  11723. thetaStart: thetaStart,
  11724. thetaLength: thetaLength
  11725. };
  11726. const scope = this;
  11727. radialSegments = Math.floor( radialSegments );
  11728. heightSegments = Math.floor( heightSegments );
  11729. // buffers
  11730. const indices = [];
  11731. const vertices = [];
  11732. const normals = [];
  11733. const uvs = [];
  11734. // helper variables
  11735. let index = 0;
  11736. const indexArray = [];
  11737. const halfHeight = height / 2;
  11738. let groupStart = 0;
  11739. // generate geometry
  11740. generateTorso();
  11741. if ( openEnded === false ) {
  11742. if ( radiusTop > 0 ) generateCap( true );
  11743. if ( radiusBottom > 0 ) generateCap( false );
  11744. }
  11745. // build geometry
  11746. this.setIndex( indices );
  11747. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  11748. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  11749. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  11750. function generateTorso() {
  11751. const normal = new Vector3();
  11752. const vertex = new Vector3();
  11753. let groupCount = 0;
  11754. // this will be used to calculate the normal
  11755. const slope = ( radiusBottom - radiusTop ) / height;
  11756. // generate vertices, normals and uvs
  11757. for ( let y = 0; y <= heightSegments; y ++ ) {
  11758. const indexRow = [];
  11759. const v = y / heightSegments;
  11760. // calculate the radius of the current row
  11761. const radius = v * ( radiusBottom - radiusTop ) + radiusTop;
  11762. for ( let x = 0; x <= radialSegments; x ++ ) {
  11763. const u = x / radialSegments;
  11764. const theta = u * thetaLength + thetaStart;
  11765. const sinTheta = Math.sin( theta );
  11766. const cosTheta = Math.cos( theta );
  11767. // vertex
  11768. vertex.x = radius * sinTheta;
  11769. vertex.y = - v * height + halfHeight;
  11770. vertex.z = radius * cosTheta;
  11771. vertices.push( vertex.x, vertex.y, vertex.z );
  11772. // normal
  11773. normal.set( sinTheta, slope, cosTheta ).normalize();
  11774. normals.push( normal.x, normal.y, normal.z );
  11775. // uv
  11776. uvs.push( u, 1 - v );
  11777. // save index of vertex in respective row
  11778. indexRow.push( index ++ );
  11779. }
  11780. // now save vertices of the row in our index array
  11781. indexArray.push( indexRow );
  11782. }
  11783. // generate indices
  11784. for ( let x = 0; x < radialSegments; x ++ ) {
  11785. for ( let y = 0; y < heightSegments; y ++ ) {
  11786. // we use the index array to access the correct indices
  11787. const a = indexArray[ y ][ x ];
  11788. const b = indexArray[ y + 1 ][ x ];
  11789. const c = indexArray[ y + 1 ][ x + 1 ];
  11790. const d = indexArray[ y ][ x + 1 ];
  11791. // faces
  11792. if ( radiusTop > 0 || y !== 0 ) {
  11793. indices.push( a, b, d );
  11794. groupCount += 3;
  11795. }
  11796. if ( radiusBottom > 0 || y !== heightSegments - 1 ) {
  11797. indices.push( b, c, d );
  11798. groupCount += 3;
  11799. }
  11800. }
  11801. }
  11802. // add a group to the geometry. this will ensure multi material support
  11803. scope.addGroup( groupStart, groupCount, 0 );
  11804. // calculate new start value for groups
  11805. groupStart += groupCount;
  11806. }
  11807. function generateCap( top ) {
  11808. // save the index of the first center vertex
  11809. const centerIndexStart = index;
  11810. const uv = new Vector2();
  11811. const vertex = new Vector3();
  11812. let groupCount = 0;
  11813. const radius = ( top === true ) ? radiusTop : radiusBottom;
  11814. const sign = ( top === true ) ? 1 : - 1;
  11815. // first we generate the center vertex data of the cap.
  11816. // because the geometry needs one set of uvs per face,
  11817. // we must generate a center vertex per face/segment
  11818. for ( let x = 1; x <= radialSegments; x ++ ) {
  11819. // vertex
  11820. vertices.push( 0, halfHeight * sign, 0 );
  11821. // normal
  11822. normals.push( 0, sign, 0 );
  11823. // uv
  11824. uvs.push( 0.5, 0.5 );
  11825. // increase index
  11826. index ++;
  11827. }
  11828. // save the index of the last center vertex
  11829. const centerIndexEnd = index;
  11830. // now we generate the surrounding vertices, normals and uvs
  11831. for ( let x = 0; x <= radialSegments; x ++ ) {
  11832. const u = x / radialSegments;
  11833. const theta = u * thetaLength + thetaStart;
  11834. const cosTheta = Math.cos( theta );
  11835. const sinTheta = Math.sin( theta );
  11836. // vertex
  11837. vertex.x = radius * sinTheta;
  11838. vertex.y = halfHeight * sign;
  11839. vertex.z = radius * cosTheta;
  11840. vertices.push( vertex.x, vertex.y, vertex.z );
  11841. // normal
  11842. normals.push( 0, sign, 0 );
  11843. // uv
  11844. uv.x = ( cosTheta * 0.5 ) + 0.5;
  11845. uv.y = ( sinTheta * 0.5 * sign ) + 0.5;
  11846. uvs.push( uv.x, uv.y );
  11847. // increase index
  11848. index ++;
  11849. }
  11850. // generate indices
  11851. for ( let x = 0; x < radialSegments; x ++ ) {
  11852. const c = centerIndexStart + x;
  11853. const i = centerIndexEnd + x;
  11854. if ( top === true ) {
  11855. // face top
  11856. indices.push( i, i + 1, c );
  11857. } else {
  11858. // face bottom
  11859. indices.push( i + 1, i, c );
  11860. }
  11861. groupCount += 3;
  11862. }
  11863. // add a group to the geometry. this will ensure multi material support
  11864. scope.addGroup( groupStart, groupCount, top === true ? 1 : 2 );
  11865. // calculate new start value for groups
  11866. groupStart += groupCount;
  11867. }
  11868. }
  11869. copy( source ) {
  11870. super.copy( source );
  11871. this.parameters = Object.assign( {}, source.parameters );
  11872. return this;
  11873. }
  11874. static fromJSON( data ) {
  11875. return new CylinderGeometry( data.radiusTop, data.radiusBottom, data.height, data.radialSegments, data.heightSegments, data.openEnded, data.thetaStart, data.thetaLength );
  11876. }
  11877. }
  11878. class ConeGeometry extends CylinderGeometry {
  11879. constructor( radius = 1, height = 1, radialSegments = 32, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 ) {
  11880. super( 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength );
  11881. this.type = 'ConeGeometry';
  11882. this.parameters = {
  11883. radius: radius,
  11884. height: height,
  11885. radialSegments: radialSegments,
  11886. heightSegments: heightSegments,
  11887. openEnded: openEnded,
  11888. thetaStart: thetaStart,
  11889. thetaLength: thetaLength
  11890. };
  11891. }
  11892. static fromJSON( data ) {
  11893. return new ConeGeometry( data.radius, data.height, data.radialSegments, data.heightSegments, data.openEnded, data.thetaStart, data.thetaLength );
  11894. }
  11895. }
  11896. class PolyhedronGeometry extends BufferGeometry {
  11897. constructor( vertices = [], indices = [], radius = 1, detail = 0 ) {
  11898. super();
  11899. this.type = 'PolyhedronGeometry';
  11900. this.parameters = {
  11901. vertices: vertices,
  11902. indices: indices,
  11903. radius: radius,
  11904. detail: detail
  11905. };
  11906. // default buffer data
  11907. const vertexBuffer = [];
  11908. const uvBuffer = [];
  11909. // the subdivision creates the vertex buffer data
  11910. subdivide( detail );
  11911. // all vertices should lie on a conceptual sphere with a given radius
  11912. applyRadius( radius );
  11913. // finally, create the uv data
  11914. generateUVs();
  11915. // build non-indexed geometry
  11916. this.setAttribute( 'position', new Float32BufferAttribute( vertexBuffer, 3 ) );
  11917. this.setAttribute( 'normal', new Float32BufferAttribute( vertexBuffer.slice(), 3 ) );
  11918. this.setAttribute( 'uv', new Float32BufferAttribute( uvBuffer, 2 ) );
  11919. if ( detail === 0 ) {
  11920. this.computeVertexNormals(); // flat normals
  11921. } else {
  11922. this.normalizeNormals(); // smooth normals
  11923. }
  11924. // helper functions
  11925. function subdivide( detail ) {
  11926. const a = new Vector3();
  11927. const b = new Vector3();
  11928. const c = new Vector3();
  11929. // iterate over all faces and apply a subdivision with the given detail value
  11930. for ( let i = 0; i < indices.length; i += 3 ) {
  11931. // get the vertices of the face
  11932. getVertexByIndex( indices[ i + 0 ], a );
  11933. getVertexByIndex( indices[ i + 1 ], b );
  11934. getVertexByIndex( indices[ i + 2 ], c );
  11935. // perform subdivision
  11936. subdivideFace( a, b, c, detail );
  11937. }
  11938. }
  11939. function subdivideFace( a, b, c, detail ) {
  11940. const cols = detail + 1;
  11941. // we use this multidimensional array as a data structure for creating the subdivision
  11942. const v = [];
  11943. // construct all of the vertices for this subdivision
  11944. for ( let i = 0; i <= cols; i ++ ) {
  11945. v[ i ] = [];
  11946. const aj = a.clone().lerp( c, i / cols );
  11947. const bj = b.clone().lerp( c, i / cols );
  11948. const rows = cols - i;
  11949. for ( let j = 0; j <= rows; j ++ ) {
  11950. if ( j === 0 && i === cols ) {
  11951. v[ i ][ j ] = aj;
  11952. } else {
  11953. v[ i ][ j ] = aj.clone().lerp( bj, j / rows );
  11954. }
  11955. }
  11956. }
  11957. // construct all of the faces
  11958. for ( let i = 0; i < cols; i ++ ) {
  11959. for ( let j = 0; j < 2 * ( cols - i ) - 1; j ++ ) {
  11960. const k = Math.floor( j / 2 );
  11961. if ( j % 2 === 0 ) {
  11962. pushVertex( v[ i ][ k + 1 ] );
  11963. pushVertex( v[ i + 1 ][ k ] );
  11964. pushVertex( v[ i ][ k ] );
  11965. } else {
  11966. pushVertex( v[ i ][ k + 1 ] );
  11967. pushVertex( v[ i + 1 ][ k + 1 ] );
  11968. pushVertex( v[ i + 1 ][ k ] );
  11969. }
  11970. }
  11971. }
  11972. }
  11973. function applyRadius( radius ) {
  11974. const vertex = new Vector3();
  11975. // iterate over the entire buffer and apply the radius to each vertex
  11976. for ( let i = 0; i < vertexBuffer.length; i += 3 ) {
  11977. vertex.x = vertexBuffer[ i + 0 ];
  11978. vertex.y = vertexBuffer[ i + 1 ];
  11979. vertex.z = vertexBuffer[ i + 2 ];
  11980. vertex.normalize().multiplyScalar( radius );
  11981. vertexBuffer[ i + 0 ] = vertex.x;
  11982. vertexBuffer[ i + 1 ] = vertex.y;
  11983. vertexBuffer[ i + 2 ] = vertex.z;
  11984. }
  11985. }
  11986. function generateUVs() {
  11987. const vertex = new Vector3();
  11988. for ( let i = 0; i < vertexBuffer.length; i += 3 ) {
  11989. vertex.x = vertexBuffer[ i + 0 ];
  11990. vertex.y = vertexBuffer[ i + 1 ];
  11991. vertex.z = vertexBuffer[ i + 2 ];
  11992. const u = azimuth( vertex ) / 2 / Math.PI + 0.5;
  11993. const v = inclination( vertex ) / Math.PI + 0.5;
  11994. uvBuffer.push( u, 1 - v );
  11995. }
  11996. correctUVs();
  11997. correctSeam();
  11998. }
  11999. function correctSeam() {
  12000. // handle case when face straddles the seam, see #3269
  12001. for ( let i = 0; i < uvBuffer.length; i += 6 ) {
  12002. // uv data of a single face
  12003. const x0 = uvBuffer[ i + 0 ];
  12004. const x1 = uvBuffer[ i + 2 ];
  12005. const x2 = uvBuffer[ i + 4 ];
  12006. const max = Math.max( x0, x1, x2 );
  12007. const min = Math.min( x0, x1, x2 );
  12008. // 0.9 is somewhat arbitrary
  12009. if ( max > 0.9 && min < 0.1 ) {
  12010. if ( x0 < 0.2 ) uvBuffer[ i + 0 ] += 1;
  12011. if ( x1 < 0.2 ) uvBuffer[ i + 2 ] += 1;
  12012. if ( x2 < 0.2 ) uvBuffer[ i + 4 ] += 1;
  12013. }
  12014. }
  12015. }
  12016. function pushVertex( vertex ) {
  12017. vertexBuffer.push( vertex.x, vertex.y, vertex.z );
  12018. }
  12019. function getVertexByIndex( index, vertex ) {
  12020. const stride = index * 3;
  12021. vertex.x = vertices[ stride + 0 ];
  12022. vertex.y = vertices[ stride + 1 ];
  12023. vertex.z = vertices[ stride + 2 ];
  12024. }
  12025. function correctUVs() {
  12026. const a = new Vector3();
  12027. const b = new Vector3();
  12028. const c = new Vector3();
  12029. const centroid = new Vector3();
  12030. const uvA = new Vector2();
  12031. const uvB = new Vector2();
  12032. const uvC = new Vector2();
  12033. for ( let i = 0, j = 0; i < vertexBuffer.length; i += 9, j += 6 ) {
  12034. a.set( vertexBuffer[ i + 0 ], vertexBuffer[ i + 1 ], vertexBuffer[ i + 2 ] );
  12035. b.set( vertexBuffer[ i + 3 ], vertexBuffer[ i + 4 ], vertexBuffer[ i + 5 ] );
  12036. c.set( vertexBuffer[ i + 6 ], vertexBuffer[ i + 7 ], vertexBuffer[ i + 8 ] );
  12037. uvA.set( uvBuffer[ j + 0 ], uvBuffer[ j + 1 ] );
  12038. uvB.set( uvBuffer[ j + 2 ], uvBuffer[ j + 3 ] );
  12039. uvC.set( uvBuffer[ j + 4 ], uvBuffer[ j + 5 ] );
  12040. centroid.copy( a ).add( b ).add( c ).divideScalar( 3 );
  12041. const azi = azimuth( centroid );
  12042. correctUV( uvA, j + 0, a, azi );
  12043. correctUV( uvB, j + 2, b, azi );
  12044. correctUV( uvC, j + 4, c, azi );
  12045. }
  12046. }
  12047. function correctUV( uv, stride, vector, azimuth ) {
  12048. if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) {
  12049. uvBuffer[ stride ] = uv.x - 1;
  12050. }
  12051. if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) {
  12052. uvBuffer[ stride ] = azimuth / 2 / Math.PI + 0.5;
  12053. }
  12054. }
  12055. // Angle around the Y axis, counter-clockwise when looking from above.
  12056. function azimuth( vector ) {
  12057. return Math.atan2( vector.z, - vector.x );
  12058. }
  12059. // Angle above the XZ plane.
  12060. function inclination( vector ) {
  12061. return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) );
  12062. }
  12063. }
  12064. copy( source ) {
  12065. super.copy( source );
  12066. this.parameters = Object.assign( {}, source.parameters );
  12067. return this;
  12068. }
  12069. static fromJSON( data ) {
  12070. return new PolyhedronGeometry( data.vertices, data.indices, data.radius, data.details );
  12071. }
  12072. }
  12073. class DodecahedronGeometry extends PolyhedronGeometry {
  12074. constructor( radius = 1, detail = 0 ) {
  12075. const t = ( 1 + Math.sqrt( 5 ) ) / 2;
  12076. const r = 1 / t;
  12077. const vertices = [
  12078. // (±1, ±1, ±1)
  12079. - 1, - 1, - 1, - 1, - 1, 1,
  12080. - 1, 1, - 1, - 1, 1, 1,
  12081. 1, - 1, - 1, 1, - 1, 1,
  12082. 1, 1, - 1, 1, 1, 1,
  12083. // (0, ±1/φ, ±φ)
  12084. 0, - r, - t, 0, - r, t,
  12085. 0, r, - t, 0, r, t,
  12086. // (±1/φ, ±φ, 0)
  12087. - r, - t, 0, - r, t, 0,
  12088. r, - t, 0, r, t, 0,
  12089. // (±φ, 0, ±1/φ)
  12090. - t, 0, - r, t, 0, - r,
  12091. - t, 0, r, t, 0, r
  12092. ];
  12093. const indices = [
  12094. 3, 11, 7, 3, 7, 15, 3, 15, 13,
  12095. 7, 19, 17, 7, 17, 6, 7, 6, 15,
  12096. 17, 4, 8, 17, 8, 10, 17, 10, 6,
  12097. 8, 0, 16, 8, 16, 2, 8, 2, 10,
  12098. 0, 12, 1, 0, 1, 18, 0, 18, 16,
  12099. 6, 10, 2, 6, 2, 13, 6, 13, 15,
  12100. 2, 16, 18, 2, 18, 3, 2, 3, 13,
  12101. 18, 1, 9, 18, 9, 11, 18, 11, 3,
  12102. 4, 14, 12, 4, 12, 0, 4, 0, 8,
  12103. 11, 9, 5, 11, 5, 19, 11, 19, 7,
  12104. 19, 5, 14, 19, 14, 4, 19, 4, 17,
  12105. 1, 12, 14, 1, 14, 5, 1, 5, 9
  12106. ];
  12107. super( vertices, indices, radius, detail );
  12108. this.type = 'DodecahedronGeometry';
  12109. this.parameters = {
  12110. radius: radius,
  12111. detail: detail
  12112. };
  12113. }
  12114. static fromJSON( data ) {
  12115. return new DodecahedronGeometry( data.radius, data.detail );
  12116. }
  12117. }
  12118. const _v0 = /*@__PURE__*/ new Vector3();
  12119. const _v1$1 = /*@__PURE__*/ new Vector3();
  12120. const _normal = /*@__PURE__*/ new Vector3();
  12121. const _triangle = /*@__PURE__*/ new Triangle();
  12122. class EdgesGeometry extends BufferGeometry {
  12123. constructor( geometry = null, thresholdAngle = 1 ) {
  12124. super();
  12125. this.type = 'EdgesGeometry';
  12126. this.parameters = {
  12127. geometry: geometry,
  12128. thresholdAngle: thresholdAngle
  12129. };
  12130. if ( geometry !== null ) {
  12131. const precisionPoints = 4;
  12132. const precision = Math.pow( 10, precisionPoints );
  12133. const thresholdDot = Math.cos( DEG2RAD * thresholdAngle );
  12134. const indexAttr = geometry.getIndex();
  12135. const positionAttr = geometry.getAttribute( 'position' );
  12136. const indexCount = indexAttr ? indexAttr.count : positionAttr.count;
  12137. const indexArr = [ 0, 0, 0 ];
  12138. const vertKeys = [ 'a', 'b', 'c' ];
  12139. const hashes = new Array( 3 );
  12140. const edgeData = {};
  12141. const vertices = [];
  12142. for ( let i = 0; i < indexCount; i += 3 ) {
  12143. if ( indexAttr ) {
  12144. indexArr[ 0 ] = indexAttr.getX( i );
  12145. indexArr[ 1 ] = indexAttr.getX( i + 1 );
  12146. indexArr[ 2 ] = indexAttr.getX( i + 2 );
  12147. } else {
  12148. indexArr[ 0 ] = i;
  12149. indexArr[ 1 ] = i + 1;
  12150. indexArr[ 2 ] = i + 2;
  12151. }
  12152. const { a, b, c } = _triangle;
  12153. a.fromBufferAttribute( positionAttr, indexArr[ 0 ] );
  12154. b.fromBufferAttribute( positionAttr, indexArr[ 1 ] );
  12155. c.fromBufferAttribute( positionAttr, indexArr[ 2 ] );
  12156. _triangle.getNormal( _normal );
  12157. // create hashes for the edge from the vertices
  12158. hashes[ 0 ] = `${ Math.round( a.x * precision ) },${ Math.round( a.y * precision ) },${ Math.round( a.z * precision ) }`;
  12159. hashes[ 1 ] = `${ Math.round( b.x * precision ) },${ Math.round( b.y * precision ) },${ Math.round( b.z * precision ) }`;
  12160. hashes[ 2 ] = `${ Math.round( c.x * precision ) },${ Math.round( c.y * precision ) },${ Math.round( c.z * precision ) }`;
  12161. // skip degenerate triangles
  12162. if ( hashes[ 0 ] === hashes[ 1 ] || hashes[ 1 ] === hashes[ 2 ] || hashes[ 2 ] === hashes[ 0 ] ) {
  12163. continue;
  12164. }
  12165. // iterate over every edge
  12166. for ( let j = 0; j < 3; j ++ ) {
  12167. // get the first and next vertex making up the edge
  12168. const jNext = ( j + 1 ) % 3;
  12169. const vecHash0 = hashes[ j ];
  12170. const vecHash1 = hashes[ jNext ];
  12171. const v0 = _triangle[ vertKeys[ j ] ];
  12172. const v1 = _triangle[ vertKeys[ jNext ] ];
  12173. const hash = `${ vecHash0 }_${ vecHash1 }`;
  12174. const reverseHash = `${ vecHash1 }_${ vecHash0 }`;
  12175. if ( reverseHash in edgeData && edgeData[ reverseHash ] ) {
  12176. // if we found a sibling edge add it into the vertex array if
  12177. // it meets the angle threshold and delete the edge from the map.
  12178. if ( _normal.dot( edgeData[ reverseHash ].normal ) <= thresholdDot ) {
  12179. vertices.push( v0.x, v0.y, v0.z );
  12180. vertices.push( v1.x, v1.y, v1.z );
  12181. }
  12182. edgeData[ reverseHash ] = null;
  12183. } else if ( ! ( hash in edgeData ) ) {
  12184. // if we've already got an edge here then skip adding a new one
  12185. edgeData[ hash ] = {
  12186. index0: indexArr[ j ],
  12187. index1: indexArr[ jNext ],
  12188. normal: _normal.clone(),
  12189. };
  12190. }
  12191. }
  12192. }
  12193. // iterate over all remaining, unmatched edges and add them to the vertex array
  12194. for ( const key in edgeData ) {
  12195. if ( edgeData[ key ] ) {
  12196. const { index0, index1 } = edgeData[ key ];
  12197. _v0.fromBufferAttribute( positionAttr, index0 );
  12198. _v1$1.fromBufferAttribute( positionAttr, index1 );
  12199. vertices.push( _v0.x, _v0.y, _v0.z );
  12200. vertices.push( _v1$1.x, _v1$1.y, _v1$1.z );
  12201. }
  12202. }
  12203. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  12204. }
  12205. }
  12206. copy( source ) {
  12207. super.copy( source );
  12208. this.parameters = Object.assign( {}, source.parameters );
  12209. return this;
  12210. }
  12211. }
  12212. class Shape extends Path {
  12213. constructor( points ) {
  12214. super( points );
  12215. this.uuid = generateUUID();
  12216. this.type = 'Shape';
  12217. this.holes = [];
  12218. }
  12219. getPointsHoles( divisions ) {
  12220. const holesPts = [];
  12221. for ( let i = 0, l = this.holes.length; i < l; i ++ ) {
  12222. holesPts[ i ] = this.holes[ i ].getPoints( divisions );
  12223. }
  12224. return holesPts;
  12225. }
  12226. // get points of shape and holes (keypoints based on segments parameter)
  12227. extractPoints( divisions ) {
  12228. return {
  12229. shape: this.getPoints( divisions ),
  12230. holes: this.getPointsHoles( divisions )
  12231. };
  12232. }
  12233. copy( source ) {
  12234. super.copy( source );
  12235. this.holes = [];
  12236. for ( let i = 0, l = source.holes.length; i < l; i ++ ) {
  12237. const hole = source.holes[ i ];
  12238. this.holes.push( hole.clone() );
  12239. }
  12240. return this;
  12241. }
  12242. toJSON() {
  12243. const data = super.toJSON();
  12244. data.uuid = this.uuid;
  12245. data.holes = [];
  12246. for ( let i = 0, l = this.holes.length; i < l; i ++ ) {
  12247. const hole = this.holes[ i ];
  12248. data.holes.push( hole.toJSON() );
  12249. }
  12250. return data;
  12251. }
  12252. fromJSON( json ) {
  12253. super.fromJSON( json );
  12254. this.uuid = json.uuid;
  12255. this.holes = [];
  12256. for ( let i = 0, l = json.holes.length; i < l; i ++ ) {
  12257. const hole = json.holes[ i ];
  12258. this.holes.push( new Path().fromJSON( hole ) );
  12259. }
  12260. return this;
  12261. }
  12262. }
  12263. /**
  12264. * Port from https://github.com/mapbox/earcut (v2.2.4)
  12265. */
  12266. const Earcut = {
  12267. triangulate: function ( data, holeIndices, dim = 2 ) {
  12268. const hasHoles = holeIndices && holeIndices.length;
  12269. const outerLen = hasHoles ? holeIndices[ 0 ] * dim : data.length;
  12270. let outerNode = linkedList( data, 0, outerLen, dim, true );
  12271. const triangles = [];
  12272. if ( ! outerNode || outerNode.next === outerNode.prev ) return triangles;
  12273. let minX, minY, maxX, maxY, x, y, invSize;
  12274. if ( hasHoles ) outerNode = eliminateHoles( data, holeIndices, outerNode, dim );
  12275. // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox
  12276. if ( data.length > 80 * dim ) {
  12277. minX = maxX = data[ 0 ];
  12278. minY = maxY = data[ 1 ];
  12279. for ( let i = dim; i < outerLen; i += dim ) {
  12280. x = data[ i ];
  12281. y = data[ i + 1 ];
  12282. if ( x < minX ) minX = x;
  12283. if ( y < minY ) minY = y;
  12284. if ( x > maxX ) maxX = x;
  12285. if ( y > maxY ) maxY = y;
  12286. }
  12287. // minX, minY and invSize are later used to transform coords into integers for z-order calculation
  12288. invSize = Math.max( maxX - minX, maxY - minY );
  12289. invSize = invSize !== 0 ? 32767 / invSize : 0;
  12290. }
  12291. earcutLinked( outerNode, triangles, dim, minX, minY, invSize, 0 );
  12292. return triangles;
  12293. }
  12294. };
  12295. // create a circular doubly linked list from polygon points in the specified winding order
  12296. function linkedList( data, start, end, dim, clockwise ) {
  12297. let i, last;
  12298. if ( clockwise === ( signedArea( data, start, end, dim ) > 0 ) ) {
  12299. for ( i = start; i < end; i += dim ) last = insertNode( i, data[ i ], data[ i + 1 ], last );
  12300. } else {
  12301. for ( i = end - dim; i >= start; i -= dim ) last = insertNode( i, data[ i ], data[ i + 1 ], last );
  12302. }
  12303. if ( last && equals( last, last.next ) ) {
  12304. removeNode( last );
  12305. last = last.next;
  12306. }
  12307. return last;
  12308. }
  12309. // eliminate colinear or duplicate points
  12310. function filterPoints( start, end ) {
  12311. if ( ! start ) return start;
  12312. if ( ! end ) end = start;
  12313. let p = start,
  12314. again;
  12315. do {
  12316. again = false;
  12317. if ( ! p.steiner && ( equals( p, p.next ) || area( p.prev, p, p.next ) === 0 ) ) {
  12318. removeNode( p );
  12319. p = end = p.prev;
  12320. if ( p === p.next ) break;
  12321. again = true;
  12322. } else {
  12323. p = p.next;
  12324. }
  12325. } while ( again || p !== end );
  12326. return end;
  12327. }
  12328. // main ear slicing loop which triangulates a polygon (given as a linked list)
  12329. function earcutLinked( ear, triangles, dim, minX, minY, invSize, pass ) {
  12330. if ( ! ear ) return;
  12331. // interlink polygon nodes in z-order
  12332. if ( ! pass && invSize ) indexCurve( ear, minX, minY, invSize );
  12333. let stop = ear,
  12334. prev, next;
  12335. // iterate through ears, slicing them one by one
  12336. while ( ear.prev !== ear.next ) {
  12337. prev = ear.prev;
  12338. next = ear.next;
  12339. if ( invSize ? isEarHashed( ear, minX, minY, invSize ) : isEar( ear ) ) {
  12340. // cut off the triangle
  12341. triangles.push( prev.i / dim | 0 );
  12342. triangles.push( ear.i / dim | 0 );
  12343. triangles.push( next.i / dim | 0 );
  12344. removeNode( ear );
  12345. // skipping the next vertex leads to less sliver triangles
  12346. ear = next.next;
  12347. stop = next.next;
  12348. continue;
  12349. }
  12350. ear = next;
  12351. // if we looped through the whole remaining polygon and can't find any more ears
  12352. if ( ear === stop ) {
  12353. // try filtering points and slicing again
  12354. if ( ! pass ) {
  12355. earcutLinked( filterPoints( ear ), triangles, dim, minX, minY, invSize, 1 );
  12356. // if this didn't work, try curing all small self-intersections locally
  12357. } else if ( pass === 1 ) {
  12358. ear = cureLocalIntersections( filterPoints( ear ), triangles, dim );
  12359. earcutLinked( ear, triangles, dim, minX, minY, invSize, 2 );
  12360. // as a last resort, try splitting the remaining polygon into two
  12361. } else if ( pass === 2 ) {
  12362. splitEarcut( ear, triangles, dim, minX, minY, invSize );
  12363. }
  12364. break;
  12365. }
  12366. }
  12367. }
  12368. // check whether a polygon node forms a valid ear with adjacent nodes
  12369. function isEar( ear ) {
  12370. const a = ear.prev,
  12371. b = ear,
  12372. c = ear.next;
  12373. if ( area( a, b, c ) >= 0 ) return false; // reflex, can't be an ear
  12374. // now make sure we don't have other points inside the potential ear
  12375. const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
  12376. // triangle bbox; min & max are calculated like this for speed
  12377. const x0 = ax < bx ? ( ax < cx ? ax : cx ) : ( bx < cx ? bx : cx ),
  12378. y0 = ay < by ? ( ay < cy ? ay : cy ) : ( by < cy ? by : cy ),
  12379. x1 = ax > bx ? ( ax > cx ? ax : cx ) : ( bx > cx ? bx : cx ),
  12380. y1 = ay > by ? ( ay > cy ? ay : cy ) : ( by > cy ? by : cy );
  12381. let p = c.next;
  12382. while ( p !== a ) {
  12383. if ( p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 &&
  12384. pointInTriangle( ax, ay, bx, by, cx, cy, p.x, p.y ) &&
  12385. area( p.prev, p, p.next ) >= 0 ) return false;
  12386. p = p.next;
  12387. }
  12388. return true;
  12389. }
  12390. function isEarHashed( ear, minX, minY, invSize ) {
  12391. const a = ear.prev,
  12392. b = ear,
  12393. c = ear.next;
  12394. if ( area( a, b, c ) >= 0 ) return false; // reflex, can't be an ear
  12395. const ax = a.x, bx = b.x, cx = c.x, ay = a.y, by = b.y, cy = c.y;
  12396. // triangle bbox; min & max are calculated like this for speed
  12397. const x0 = ax < bx ? ( ax < cx ? ax : cx ) : ( bx < cx ? bx : cx ),
  12398. y0 = ay < by ? ( ay < cy ? ay : cy ) : ( by < cy ? by : cy ),
  12399. x1 = ax > bx ? ( ax > cx ? ax : cx ) : ( bx > cx ? bx : cx ),
  12400. y1 = ay > by ? ( ay > cy ? ay : cy ) : ( by > cy ? by : cy );
  12401. // z-order range for the current triangle bbox;
  12402. const minZ = zOrder( x0, y0, minX, minY, invSize ),
  12403. maxZ = zOrder( x1, y1, minX, minY, invSize );
  12404. let p = ear.prevZ,
  12405. n = ear.nextZ;
  12406. // look for points inside the triangle in both directions
  12407. while ( p && p.z >= minZ && n && n.z <= maxZ ) {
  12408. if ( p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
  12409. pointInTriangle( ax, ay, bx, by, cx, cy, p.x, p.y ) && area( p.prev, p, p.next ) >= 0 ) return false;
  12410. p = p.prevZ;
  12411. if ( n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
  12412. pointInTriangle( ax, ay, bx, by, cx, cy, n.x, n.y ) && area( n.prev, n, n.next ) >= 0 ) return false;
  12413. n = n.nextZ;
  12414. }
  12415. // look for remaining points in decreasing z-order
  12416. while ( p && p.z >= minZ ) {
  12417. if ( p.x >= x0 && p.x <= x1 && p.y >= y0 && p.y <= y1 && p !== a && p !== c &&
  12418. pointInTriangle( ax, ay, bx, by, cx, cy, p.x, p.y ) && area( p.prev, p, p.next ) >= 0 ) return false;
  12419. p = p.prevZ;
  12420. }
  12421. // look for remaining points in increasing z-order
  12422. while ( n && n.z <= maxZ ) {
  12423. if ( n.x >= x0 && n.x <= x1 && n.y >= y0 && n.y <= y1 && n !== a && n !== c &&
  12424. pointInTriangle( ax, ay, bx, by, cx, cy, n.x, n.y ) && area( n.prev, n, n.next ) >= 0 ) return false;
  12425. n = n.nextZ;
  12426. }
  12427. return true;
  12428. }
  12429. // go through all polygon nodes and cure small local self-intersections
  12430. function cureLocalIntersections( start, triangles, dim ) {
  12431. let p = start;
  12432. do {
  12433. const a = p.prev,
  12434. b = p.next.next;
  12435. if ( ! equals( a, b ) && intersects( a, p, p.next, b ) && locallyInside( a, b ) && locallyInside( b, a ) ) {
  12436. triangles.push( a.i / dim | 0 );
  12437. triangles.push( p.i / dim | 0 );
  12438. triangles.push( b.i / dim | 0 );
  12439. // remove two nodes involved
  12440. removeNode( p );
  12441. removeNode( p.next );
  12442. p = start = b;
  12443. }
  12444. p = p.next;
  12445. } while ( p !== start );
  12446. return filterPoints( p );
  12447. }
  12448. // try splitting polygon into two and triangulate them independently
  12449. function splitEarcut( start, triangles, dim, minX, minY, invSize ) {
  12450. // look for a valid diagonal that divides the polygon into two
  12451. let a = start;
  12452. do {
  12453. let b = a.next.next;
  12454. while ( b !== a.prev ) {
  12455. if ( a.i !== b.i && isValidDiagonal( a, b ) ) {
  12456. // split the polygon in two by the diagonal
  12457. let c = splitPolygon( a, b );
  12458. // filter colinear points around the cuts
  12459. a = filterPoints( a, a.next );
  12460. c = filterPoints( c, c.next );
  12461. // run earcut on each half
  12462. earcutLinked( a, triangles, dim, minX, minY, invSize, 0 );
  12463. earcutLinked( c, triangles, dim, minX, minY, invSize, 0 );
  12464. return;
  12465. }
  12466. b = b.next;
  12467. }
  12468. a = a.next;
  12469. } while ( a !== start );
  12470. }
  12471. // link every hole into the outer loop, producing a single-ring polygon without holes
  12472. function eliminateHoles( data, holeIndices, outerNode, dim ) {
  12473. const queue = [];
  12474. let i, len, start, end, list;
  12475. for ( i = 0, len = holeIndices.length; i < len; i ++ ) {
  12476. start = holeIndices[ i ] * dim;
  12477. end = i < len - 1 ? holeIndices[ i + 1 ] * dim : data.length;
  12478. list = linkedList( data, start, end, dim, false );
  12479. if ( list === list.next ) list.steiner = true;
  12480. queue.push( getLeftmost( list ) );
  12481. }
  12482. queue.sort( compareX );
  12483. // process holes from left to right
  12484. for ( i = 0; i < queue.length; i ++ ) {
  12485. outerNode = eliminateHole( queue[ i ], outerNode );
  12486. }
  12487. return outerNode;
  12488. }
  12489. function compareX( a, b ) {
  12490. return a.x - b.x;
  12491. }
  12492. // find a bridge between vertices that connects hole with an outer ring and link it
  12493. function eliminateHole( hole, outerNode ) {
  12494. const bridge = findHoleBridge( hole, outerNode );
  12495. if ( ! bridge ) {
  12496. return outerNode;
  12497. }
  12498. const bridgeReverse = splitPolygon( bridge, hole );
  12499. // filter collinear points around the cuts
  12500. filterPoints( bridgeReverse, bridgeReverse.next );
  12501. return filterPoints( bridge, bridge.next );
  12502. }
  12503. // David Eberly's algorithm for finding a bridge between hole and outer polygon
  12504. function findHoleBridge( hole, outerNode ) {
  12505. let p = outerNode,
  12506. qx = - Infinity,
  12507. m;
  12508. const hx = hole.x, hy = hole.y;
  12509. // find a segment intersected by a ray from the hole's leftmost point to the left;
  12510. // segment's endpoint with lesser x will be potential connection point
  12511. do {
  12512. if ( hy <= p.y && hy >= p.next.y && p.next.y !== p.y ) {
  12513. const x = p.x + ( hy - p.y ) * ( p.next.x - p.x ) / ( p.next.y - p.y );
  12514. if ( x <= hx && x > qx ) {
  12515. qx = x;
  12516. m = p.x < p.next.x ? p : p.next;
  12517. if ( x === hx ) return m; // hole touches outer segment; pick leftmost endpoint
  12518. }
  12519. }
  12520. p = p.next;
  12521. } while ( p !== outerNode );
  12522. if ( ! m ) return null;
  12523. // look for points inside the triangle of hole point, segment intersection and endpoint;
  12524. // if there are no points found, we have a valid connection;
  12525. // otherwise choose the point of the minimum angle with the ray as connection point
  12526. const stop = m,
  12527. mx = m.x,
  12528. my = m.y;
  12529. let tanMin = Infinity, tan;
  12530. p = m;
  12531. do {
  12532. if ( hx >= p.x && p.x >= mx && hx !== p.x &&
  12533. pointInTriangle( hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y ) ) {
  12534. tan = Math.abs( hy - p.y ) / ( hx - p.x ); // tangential
  12535. if ( locallyInside( p, hole ) && ( tan < tanMin || ( tan === tanMin && ( p.x > m.x || ( p.x === m.x && sectorContainsSector( m, p ) ) ) ) ) ) {
  12536. m = p;
  12537. tanMin = tan;
  12538. }
  12539. }
  12540. p = p.next;
  12541. } while ( p !== stop );
  12542. return m;
  12543. }
  12544. // whether sector in vertex m contains sector in vertex p in the same coordinates
  12545. function sectorContainsSector( m, p ) {
  12546. return area( m.prev, m, p.prev ) < 0 && area( p.next, m, m.next ) < 0;
  12547. }
  12548. // interlink polygon nodes in z-order
  12549. function indexCurve( start, minX, minY, invSize ) {
  12550. let p = start;
  12551. do {
  12552. if ( p.z === 0 ) p.z = zOrder( p.x, p.y, minX, minY, invSize );
  12553. p.prevZ = p.prev;
  12554. p.nextZ = p.next;
  12555. p = p.next;
  12556. } while ( p !== start );
  12557. p.prevZ.nextZ = null;
  12558. p.prevZ = null;
  12559. sortLinked( p );
  12560. }
  12561. // Simon Tatham's linked list merge sort algorithm
  12562. // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html
  12563. function sortLinked( list ) {
  12564. let i, p, q, e, tail, numMerges, pSize, qSize,
  12565. inSize = 1;
  12566. do {
  12567. p = list;
  12568. list = null;
  12569. tail = null;
  12570. numMerges = 0;
  12571. while ( p ) {
  12572. numMerges ++;
  12573. q = p;
  12574. pSize = 0;
  12575. for ( i = 0; i < inSize; i ++ ) {
  12576. pSize ++;
  12577. q = q.nextZ;
  12578. if ( ! q ) break;
  12579. }
  12580. qSize = inSize;
  12581. while ( pSize > 0 || ( qSize > 0 && q ) ) {
  12582. if ( pSize !== 0 && ( qSize === 0 || ! q || p.z <= q.z ) ) {
  12583. e = p;
  12584. p = p.nextZ;
  12585. pSize --;
  12586. } else {
  12587. e = q;
  12588. q = q.nextZ;
  12589. qSize --;
  12590. }
  12591. if ( tail ) tail.nextZ = e;
  12592. else list = e;
  12593. e.prevZ = tail;
  12594. tail = e;
  12595. }
  12596. p = q;
  12597. }
  12598. tail.nextZ = null;
  12599. inSize *= 2;
  12600. } while ( numMerges > 1 );
  12601. return list;
  12602. }
  12603. // z-order of a point given coords and inverse of the longer side of data bbox
  12604. function zOrder( x, y, minX, minY, invSize ) {
  12605. // coords are transformed into non-negative 15-bit integer range
  12606. x = ( x - minX ) * invSize | 0;
  12607. y = ( y - minY ) * invSize | 0;
  12608. x = ( x | ( x << 8 ) ) & 0x00FF00FF;
  12609. x = ( x | ( x << 4 ) ) & 0x0F0F0F0F;
  12610. x = ( x | ( x << 2 ) ) & 0x33333333;
  12611. x = ( x | ( x << 1 ) ) & 0x55555555;
  12612. y = ( y | ( y << 8 ) ) & 0x00FF00FF;
  12613. y = ( y | ( y << 4 ) ) & 0x0F0F0F0F;
  12614. y = ( y | ( y << 2 ) ) & 0x33333333;
  12615. y = ( y | ( y << 1 ) ) & 0x55555555;
  12616. return x | ( y << 1 );
  12617. }
  12618. // find the leftmost node of a polygon ring
  12619. function getLeftmost( start ) {
  12620. let p = start,
  12621. leftmost = start;
  12622. do {
  12623. if ( p.x < leftmost.x || ( p.x === leftmost.x && p.y < leftmost.y ) ) leftmost = p;
  12624. p = p.next;
  12625. } while ( p !== start );
  12626. return leftmost;
  12627. }
  12628. // check if a point lies within a convex triangle
  12629. function pointInTriangle( ax, ay, bx, by, cx, cy, px, py ) {
  12630. return ( cx - px ) * ( ay - py ) >= ( ax - px ) * ( cy - py ) &&
  12631. ( ax - px ) * ( by - py ) >= ( bx - px ) * ( ay - py ) &&
  12632. ( bx - px ) * ( cy - py ) >= ( cx - px ) * ( by - py );
  12633. }
  12634. // check if a diagonal between two polygon nodes is valid (lies in polygon interior)
  12635. function isValidDiagonal( a, b ) {
  12636. return a.next.i !== b.i && a.prev.i !== b.i && ! intersectsPolygon( a, b ) && // dones't intersect other edges
  12637. ( locallyInside( a, b ) && locallyInside( b, a ) && middleInside( a, b ) && // locally visible
  12638. ( area( a.prev, a, b.prev ) || area( a, b.prev, b ) ) || // does not create opposite-facing sectors
  12639. equals( a, b ) && area( a.prev, a, a.next ) > 0 && area( b.prev, b, b.next ) > 0 ); // special zero-length case
  12640. }
  12641. // signed area of a triangle
  12642. function area( p, q, r ) {
  12643. return ( q.y - p.y ) * ( r.x - q.x ) - ( q.x - p.x ) * ( r.y - q.y );
  12644. }
  12645. // check if two points are equal
  12646. function equals( p1, p2 ) {
  12647. return p1.x === p2.x && p1.y === p2.y;
  12648. }
  12649. // check if two segments intersect
  12650. function intersects( p1, q1, p2, q2 ) {
  12651. const o1 = sign( area( p1, q1, p2 ) );
  12652. const o2 = sign( area( p1, q1, q2 ) );
  12653. const o3 = sign( area( p2, q2, p1 ) );
  12654. const o4 = sign( area( p2, q2, q1 ) );
  12655. if ( o1 !== o2 && o3 !== o4 ) return true; // general case
  12656. if ( o1 === 0 && onSegment( p1, p2, q1 ) ) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1
  12657. if ( o2 === 0 && onSegment( p1, q2, q1 ) ) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1
  12658. if ( o3 === 0 && onSegment( p2, p1, q2 ) ) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2
  12659. if ( o4 === 0 && onSegment( p2, q1, q2 ) ) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2
  12660. return false;
  12661. }
  12662. // for collinear points p, q, r, check if point q lies on segment pr
  12663. function onSegment( p, q, r ) {
  12664. 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 );
  12665. }
  12666. function sign( num ) {
  12667. return num > 0 ? 1 : num < 0 ? - 1 : 0;
  12668. }
  12669. // check if a polygon diagonal intersects any polygon segments
  12670. function intersectsPolygon( a, b ) {
  12671. let p = a;
  12672. do {
  12673. if ( p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i &&
  12674. intersects( p, p.next, a, b ) ) return true;
  12675. p = p.next;
  12676. } while ( p !== a );
  12677. return false;
  12678. }
  12679. // check if a polygon diagonal is locally inside the polygon
  12680. function locallyInside( a, b ) {
  12681. return area( a.prev, a, a.next ) < 0 ?
  12682. area( a, b, a.next ) >= 0 && area( a, a.prev, b ) >= 0 :
  12683. area( a, b, a.prev ) < 0 || area( a, a.next, b ) < 0;
  12684. }
  12685. // check if the middle point of a polygon diagonal is inside the polygon
  12686. function middleInside( a, b ) {
  12687. let p = a,
  12688. inside = false;
  12689. const px = ( a.x + b.x ) / 2,
  12690. py = ( a.y + b.y ) / 2;
  12691. do {
  12692. if ( ( ( p.y > py ) !== ( p.next.y > py ) ) && p.next.y !== p.y &&
  12693. ( px < ( p.next.x - p.x ) * ( py - p.y ) / ( p.next.y - p.y ) + p.x ) )
  12694. inside = ! inside;
  12695. p = p.next;
  12696. } while ( p !== a );
  12697. return inside;
  12698. }
  12699. // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two;
  12700. // if one belongs to the outer ring and another to a hole, it merges it into a single ring
  12701. function splitPolygon( a, b ) {
  12702. const a2 = new Node( a.i, a.x, a.y ),
  12703. b2 = new Node( b.i, b.x, b.y ),
  12704. an = a.next,
  12705. bp = b.prev;
  12706. a.next = b;
  12707. b.prev = a;
  12708. a2.next = an;
  12709. an.prev = a2;
  12710. b2.next = a2;
  12711. a2.prev = b2;
  12712. bp.next = b2;
  12713. b2.prev = bp;
  12714. return b2;
  12715. }
  12716. // create a node and optionally link it with previous one (in a circular doubly linked list)
  12717. function insertNode( i, x, y, last ) {
  12718. const p = new Node( i, x, y );
  12719. if ( ! last ) {
  12720. p.prev = p;
  12721. p.next = p;
  12722. } else {
  12723. p.next = last.next;
  12724. p.prev = last;
  12725. last.next.prev = p;
  12726. last.next = p;
  12727. }
  12728. return p;
  12729. }
  12730. function removeNode( p ) {
  12731. p.next.prev = p.prev;
  12732. p.prev.next = p.next;
  12733. if ( p.prevZ ) p.prevZ.nextZ = p.nextZ;
  12734. if ( p.nextZ ) p.nextZ.prevZ = p.prevZ;
  12735. }
  12736. function Node( i, x, y ) {
  12737. // vertex index in coordinates array
  12738. this.i = i;
  12739. // vertex coordinates
  12740. this.x = x;
  12741. this.y = y;
  12742. // previous and next vertex nodes in a polygon ring
  12743. this.prev = null;
  12744. this.next = null;
  12745. // z-order curve value
  12746. this.z = 0;
  12747. // previous and next nodes in z-order
  12748. this.prevZ = null;
  12749. this.nextZ = null;
  12750. // indicates whether this is a steiner point
  12751. this.steiner = false;
  12752. }
  12753. function signedArea( data, start, end, dim ) {
  12754. let sum = 0;
  12755. for ( let i = start, j = end - dim; i < end; i += dim ) {
  12756. sum += ( data[ j ] - data[ i ] ) * ( data[ i + 1 ] + data[ j + 1 ] );
  12757. j = i;
  12758. }
  12759. return sum;
  12760. }
  12761. class ShapeUtils {
  12762. // calculate area of the contour polygon
  12763. static area( contour ) {
  12764. const n = contour.length;
  12765. let a = 0.0;
  12766. for ( let p = n - 1, q = 0; q < n; p = q ++ ) {
  12767. a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
  12768. }
  12769. return a * 0.5;
  12770. }
  12771. static isClockWise( pts ) {
  12772. return ShapeUtils.area( pts ) < 0;
  12773. }
  12774. static triangulateShape( contour, holes ) {
  12775. const vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ]
  12776. const holeIndices = []; // array of hole indices
  12777. const faces = []; // final array of vertex indices like [ [ a,b,d ], [ b,c,d ] ]
  12778. removeDupEndPts( contour );
  12779. addContour( vertices, contour );
  12780. //
  12781. let holeIndex = contour.length;
  12782. holes.forEach( removeDupEndPts );
  12783. for ( let i = 0; i < holes.length; i ++ ) {
  12784. holeIndices.push( holeIndex );
  12785. holeIndex += holes[ i ].length;
  12786. addContour( vertices, holes[ i ] );
  12787. }
  12788. //
  12789. const triangles = Earcut.triangulate( vertices, holeIndices );
  12790. //
  12791. for ( let i = 0; i < triangles.length; i += 3 ) {
  12792. faces.push( triangles.slice( i, i + 3 ) );
  12793. }
  12794. return faces;
  12795. }
  12796. }
  12797. function removeDupEndPts( points ) {
  12798. const l = points.length;
  12799. if ( l > 2 && points[ l - 1 ].equals( points[ 0 ] ) ) {
  12800. points.pop();
  12801. }
  12802. }
  12803. function addContour( vertices, contour ) {
  12804. for ( let i = 0; i < contour.length; i ++ ) {
  12805. vertices.push( contour[ i ].x );
  12806. vertices.push( contour[ i ].y );
  12807. }
  12808. }
  12809. /**
  12810. * Creates extruded geometry from a path shape.
  12811. *
  12812. * parameters = {
  12813. *
  12814. * curveSegments: <int>, // number of points on the curves
  12815. * steps: <int>, // number of points for z-side extrusions / used for subdividing segments of extrude spline too
  12816. * depth: <float>, // Depth to extrude the shape
  12817. *
  12818. * bevelEnabled: <bool>, // turn on bevel
  12819. * bevelThickness: <float>, // how deep into the original shape bevel goes
  12820. * bevelSize: <float>, // how far from shape outline (including bevelOffset) is bevel
  12821. * bevelOffset: <float>, // how far from shape outline does bevel start
  12822. * bevelSegments: <int>, // number of bevel layers
  12823. *
  12824. * extrudePath: <THREE.Curve> // curve to extrude shape along
  12825. *
  12826. * UVGenerator: <Object> // object that provides UV generator functions
  12827. *
  12828. * }
  12829. */
  12830. class ExtrudeGeometry extends BufferGeometry {
  12831. 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 = {} ) {
  12832. super();
  12833. this.type = 'ExtrudeGeometry';
  12834. this.parameters = {
  12835. shapes: shapes,
  12836. options: options
  12837. };
  12838. shapes = Array.isArray( shapes ) ? shapes : [ shapes ];
  12839. const scope = this;
  12840. const verticesArray = [];
  12841. const uvArray = [];
  12842. for ( let i = 0, l = shapes.length; i < l; i ++ ) {
  12843. const shape = shapes[ i ];
  12844. addShape( shape );
  12845. }
  12846. // build geometry
  12847. this.setAttribute( 'position', new Float32BufferAttribute( verticesArray, 3 ) );
  12848. this.setAttribute( 'uv', new Float32BufferAttribute( uvArray, 2 ) );
  12849. this.computeVertexNormals();
  12850. // functions
  12851. function addShape( shape ) {
  12852. const placeholder = [];
  12853. // options
  12854. const curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
  12855. const steps = options.steps !== undefined ? options.steps : 1;
  12856. const depth = options.depth !== undefined ? options.depth : 1;
  12857. let bevelEnabled = options.bevelEnabled !== undefined ? options.bevelEnabled : true;
  12858. let bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 0.2;
  12859. let bevelSize = options.bevelSize !== undefined ? options.bevelSize : bevelThickness - 0.1;
  12860. let bevelOffset = options.bevelOffset !== undefined ? options.bevelOffset : 0;
  12861. let bevelSegments = options.bevelSegments !== undefined ? options.bevelSegments : 3;
  12862. const extrudePath = options.extrudePath;
  12863. const uvgen = options.UVGenerator !== undefined ? options.UVGenerator : WorldUVGenerator;
  12864. //
  12865. let extrudePts, extrudeByPath = false;
  12866. let splineTube, binormal, normal, position2;
  12867. if ( extrudePath ) {
  12868. extrudePts = extrudePath.getSpacedPoints( steps );
  12869. extrudeByPath = true;
  12870. bevelEnabled = false; // bevels not supported for path extrusion
  12871. // SETUP TNB variables
  12872. // TODO1 - have a .isClosed in spline?
  12873. splineTube = extrudePath.computeFrenetFrames( steps, false );
  12874. // console.log(splineTube, 'splineTube', splineTube.normals.length, 'steps', steps, 'extrudePts', extrudePts.length);
  12875. binormal = new Vector3();
  12876. normal = new Vector3();
  12877. position2 = new Vector3();
  12878. }
  12879. // Safeguards if bevels are not enabled
  12880. if ( ! bevelEnabled ) {
  12881. bevelSegments = 0;
  12882. bevelThickness = 0;
  12883. bevelSize = 0;
  12884. bevelOffset = 0;
  12885. }
  12886. // Variables initialization
  12887. const shapePoints = shape.extractPoints( curveSegments );
  12888. let vertices = shapePoints.shape;
  12889. const holes = shapePoints.holes;
  12890. const reverse = ! ShapeUtils.isClockWise( vertices );
  12891. if ( reverse ) {
  12892. vertices = vertices.reverse();
  12893. // Maybe we should also check if holes are in the opposite direction, just to be safe ...
  12894. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  12895. const ahole = holes[ h ];
  12896. if ( ShapeUtils.isClockWise( ahole ) ) {
  12897. holes[ h ] = ahole.reverse();
  12898. }
  12899. }
  12900. }
  12901. const faces = ShapeUtils.triangulateShape( vertices, holes );
  12902. /* Vertices */
  12903. const contour = vertices; // vertices has all points but contour has only points of circumference
  12904. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  12905. const ahole = holes[ h ];
  12906. vertices = vertices.concat( ahole );
  12907. }
  12908. function scalePt2( pt, vec, size ) {
  12909. if ( ! vec ) console.error( 'THREE.ExtrudeGeometry: vec does not exist' );
  12910. return pt.clone().addScaledVector( vec, size );
  12911. }
  12912. const vlen = vertices.length, flen = faces.length;
  12913. // Find directions for point movement
  12914. function getBevelVec( inPt, inPrev, inNext ) {
  12915. // computes for inPt the corresponding point inPt' on a new contour
  12916. // shifted by 1 unit (length of normalized vector) to the left
  12917. // if we walk along contour clockwise, this new contour is outside the old one
  12918. //
  12919. // inPt' is the intersection of the two lines parallel to the two
  12920. // adjacent edges of inPt at a distance of 1 unit on the left side.
  12921. let v_trans_x, v_trans_y, shrink_by; // resulting translation vector for inPt
  12922. // good reading for geometry algorithms (here: line-line intersection)
  12923. // http://geomalgorithms.com/a05-_intersect-1.html
  12924. const v_prev_x = inPt.x - inPrev.x,
  12925. v_prev_y = inPt.y - inPrev.y;
  12926. const v_next_x = inNext.x - inPt.x,
  12927. v_next_y = inNext.y - inPt.y;
  12928. const v_prev_lensq = ( v_prev_x * v_prev_x + v_prev_y * v_prev_y );
  12929. // check for collinear edges
  12930. const collinear0 = ( v_prev_x * v_next_y - v_prev_y * v_next_x );
  12931. if ( Math.abs( collinear0 ) > Number.EPSILON ) {
  12932. // not collinear
  12933. // length of vectors for normalizing
  12934. const v_prev_len = Math.sqrt( v_prev_lensq );
  12935. const v_next_len = Math.sqrt( v_next_x * v_next_x + v_next_y * v_next_y );
  12936. // shift adjacent points by unit vectors to the left
  12937. const ptPrevShift_x = ( inPrev.x - v_prev_y / v_prev_len );
  12938. const ptPrevShift_y = ( inPrev.y + v_prev_x / v_prev_len );
  12939. const ptNextShift_x = ( inNext.x - v_next_y / v_next_len );
  12940. const ptNextShift_y = ( inNext.y + v_next_x / v_next_len );
  12941. // scaling factor for v_prev to intersection point
  12942. const sf = ( ( ptNextShift_x - ptPrevShift_x ) * v_next_y -
  12943. ( ptNextShift_y - ptPrevShift_y ) * v_next_x ) /
  12944. ( v_prev_x * v_next_y - v_prev_y * v_next_x );
  12945. // vector from inPt to intersection point
  12946. v_trans_x = ( ptPrevShift_x + v_prev_x * sf - inPt.x );
  12947. v_trans_y = ( ptPrevShift_y + v_prev_y * sf - inPt.y );
  12948. // Don't normalize!, otherwise sharp corners become ugly
  12949. // but prevent crazy spikes
  12950. const v_trans_lensq = ( v_trans_x * v_trans_x + v_trans_y * v_trans_y );
  12951. if ( v_trans_lensq <= 2 ) {
  12952. return new Vector2( v_trans_x, v_trans_y );
  12953. } else {
  12954. shrink_by = Math.sqrt( v_trans_lensq / 2 );
  12955. }
  12956. } else {
  12957. // handle special case of collinear edges
  12958. let direction_eq = false; // assumes: opposite
  12959. if ( v_prev_x > Number.EPSILON ) {
  12960. if ( v_next_x > Number.EPSILON ) {
  12961. direction_eq = true;
  12962. }
  12963. } else {
  12964. if ( v_prev_x < - Number.EPSILON ) {
  12965. if ( v_next_x < - Number.EPSILON ) {
  12966. direction_eq = true;
  12967. }
  12968. } else {
  12969. if ( Math.sign( v_prev_y ) === Math.sign( v_next_y ) ) {
  12970. direction_eq = true;
  12971. }
  12972. }
  12973. }
  12974. if ( direction_eq ) {
  12975. // console.log("Warning: lines are a straight sequence");
  12976. v_trans_x = - v_prev_y;
  12977. v_trans_y = v_prev_x;
  12978. shrink_by = Math.sqrt( v_prev_lensq );
  12979. } else {
  12980. // console.log("Warning: lines are a straight spike");
  12981. v_trans_x = v_prev_x;
  12982. v_trans_y = v_prev_y;
  12983. shrink_by = Math.sqrt( v_prev_lensq / 2 );
  12984. }
  12985. }
  12986. return new Vector2( v_trans_x / shrink_by, v_trans_y / shrink_by );
  12987. }
  12988. const contourMovements = [];
  12989. for ( let i = 0, il = contour.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
  12990. if ( j === il ) j = 0;
  12991. if ( k === il ) k = 0;
  12992. // (j)---(i)---(k)
  12993. // console.log('i,j,k', i, j , k)
  12994. contourMovements[ i ] = getBevelVec( contour[ i ], contour[ j ], contour[ k ] );
  12995. }
  12996. const holesMovements = [];
  12997. let oneHoleMovements, verticesMovements = contourMovements.concat();
  12998. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  12999. const ahole = holes[ h ];
  13000. oneHoleMovements = [];
  13001. for ( let i = 0, il = ahole.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
  13002. if ( j === il ) j = 0;
  13003. if ( k === il ) k = 0;
  13004. // (j)---(i)---(k)
  13005. oneHoleMovements[ i ] = getBevelVec( ahole[ i ], ahole[ j ], ahole[ k ] );
  13006. }
  13007. holesMovements.push( oneHoleMovements );
  13008. verticesMovements = verticesMovements.concat( oneHoleMovements );
  13009. }
  13010. // Loop bevelSegments, 1 for the front, 1 for the back
  13011. for ( let b = 0; b < bevelSegments; b ++ ) {
  13012. //for ( b = bevelSegments; b > 0; b -- ) {
  13013. const t = b / bevelSegments;
  13014. const z = bevelThickness * Math.cos( t * Math.PI / 2 );
  13015. const bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset;
  13016. // contract shape
  13017. for ( let i = 0, il = contour.length; i < il; i ++ ) {
  13018. const vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
  13019. v( vert.x, vert.y, - z );
  13020. }
  13021. // expand holes
  13022. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  13023. const ahole = holes[ h ];
  13024. oneHoleMovements = holesMovements[ h ];
  13025. for ( let i = 0, il = ahole.length; i < il; i ++ ) {
  13026. const vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
  13027. v( vert.x, vert.y, - z );
  13028. }
  13029. }
  13030. }
  13031. const bs = bevelSize + bevelOffset;
  13032. // Back facing vertices
  13033. for ( let i = 0; i < vlen; i ++ ) {
  13034. const vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
  13035. if ( ! extrudeByPath ) {
  13036. v( vert.x, vert.y, 0 );
  13037. } else {
  13038. // v( vert.x, vert.y + extrudePts[ 0 ].y, extrudePts[ 0 ].x );
  13039. normal.copy( splineTube.normals[ 0 ] ).multiplyScalar( vert.x );
  13040. binormal.copy( splineTube.binormals[ 0 ] ).multiplyScalar( vert.y );
  13041. position2.copy( extrudePts[ 0 ] ).add( normal ).add( binormal );
  13042. v( position2.x, position2.y, position2.z );
  13043. }
  13044. }
  13045. // Add stepped vertices...
  13046. // Including front facing vertices
  13047. for ( let s = 1; s <= steps; s ++ ) {
  13048. for ( let i = 0; i < vlen; i ++ ) {
  13049. const vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
  13050. if ( ! extrudeByPath ) {
  13051. v( vert.x, vert.y, depth / steps * s );
  13052. } else {
  13053. // v( vert.x, vert.y + extrudePts[ s - 1 ].y, extrudePts[ s - 1 ].x );
  13054. normal.copy( splineTube.normals[ s ] ).multiplyScalar( vert.x );
  13055. binormal.copy( splineTube.binormals[ s ] ).multiplyScalar( vert.y );
  13056. position2.copy( extrudePts[ s ] ).add( normal ).add( binormal );
  13057. v( position2.x, position2.y, position2.z );
  13058. }
  13059. }
  13060. }
  13061. // Add bevel segments planes
  13062. //for ( b = 1; b <= bevelSegments; b ++ ) {
  13063. for ( let b = bevelSegments - 1; b >= 0; b -- ) {
  13064. const t = b / bevelSegments;
  13065. const z = bevelThickness * Math.cos( t * Math.PI / 2 );
  13066. const bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset;
  13067. // contract shape
  13068. for ( let i = 0, il = contour.length; i < il; i ++ ) {
  13069. const vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
  13070. v( vert.x, vert.y, depth + z );
  13071. }
  13072. // expand holes
  13073. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  13074. const ahole = holes[ h ];
  13075. oneHoleMovements = holesMovements[ h ];
  13076. for ( let i = 0, il = ahole.length; i < il; i ++ ) {
  13077. const vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
  13078. if ( ! extrudeByPath ) {
  13079. v( vert.x, vert.y, depth + z );
  13080. } else {
  13081. v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x + z );
  13082. }
  13083. }
  13084. }
  13085. }
  13086. /* Faces */
  13087. // Top and bottom faces
  13088. buildLidFaces();
  13089. // Sides faces
  13090. buildSideFaces();
  13091. ///// Internal functions
  13092. function buildLidFaces() {
  13093. const start = verticesArray.length / 3;
  13094. if ( bevelEnabled ) {
  13095. let layer = 0; // steps + 1
  13096. let offset = vlen * layer;
  13097. // Bottom faces
  13098. for ( let i = 0; i < flen; i ++ ) {
  13099. const face = faces[ i ];
  13100. f3( face[ 2 ] + offset, face[ 1 ] + offset, face[ 0 ] + offset );
  13101. }
  13102. layer = steps + bevelSegments * 2;
  13103. offset = vlen * layer;
  13104. // Top faces
  13105. for ( let i = 0; i < flen; i ++ ) {
  13106. const face = faces[ i ];
  13107. f3( face[ 0 ] + offset, face[ 1 ] + offset, face[ 2 ] + offset );
  13108. }
  13109. } else {
  13110. // Bottom faces
  13111. for ( let i = 0; i < flen; i ++ ) {
  13112. const face = faces[ i ];
  13113. f3( face[ 2 ], face[ 1 ], face[ 0 ] );
  13114. }
  13115. // Top faces
  13116. for ( let i = 0; i < flen; i ++ ) {
  13117. const face = faces[ i ];
  13118. f3( face[ 0 ] + vlen * steps, face[ 1 ] + vlen * steps, face[ 2 ] + vlen * steps );
  13119. }
  13120. }
  13121. scope.addGroup( start, verticesArray.length / 3 - start, 0 );
  13122. }
  13123. // Create faces for the z-sides of the shape
  13124. function buildSideFaces() {
  13125. const start = verticesArray.length / 3;
  13126. let layeroffset = 0;
  13127. sidewalls( contour, layeroffset );
  13128. layeroffset += contour.length;
  13129. for ( let h = 0, hl = holes.length; h < hl; h ++ ) {
  13130. const ahole = holes[ h ];
  13131. sidewalls( ahole, layeroffset );
  13132. //, true
  13133. layeroffset += ahole.length;
  13134. }
  13135. scope.addGroup( start, verticesArray.length / 3 - start, 1 );
  13136. }
  13137. function sidewalls( contour, layeroffset ) {
  13138. let i = contour.length;
  13139. while ( -- i >= 0 ) {
  13140. const j = i;
  13141. let k = i - 1;
  13142. if ( k < 0 ) k = contour.length - 1;
  13143. //console.log('b', i,j, i-1, k,vertices.length);
  13144. for ( let s = 0, sl = ( steps + bevelSegments * 2 ); s < sl; s ++ ) {
  13145. const slen1 = vlen * s;
  13146. const slen2 = vlen * ( s + 1 );
  13147. const a = layeroffset + j + slen1,
  13148. b = layeroffset + k + slen1,
  13149. c = layeroffset + k + slen2,
  13150. d = layeroffset + j + slen2;
  13151. f4( a, b, c, d );
  13152. }
  13153. }
  13154. }
  13155. function v( x, y, z ) {
  13156. placeholder.push( x );
  13157. placeholder.push( y );
  13158. placeholder.push( z );
  13159. }
  13160. function f3( a, b, c ) {
  13161. addVertex( a );
  13162. addVertex( b );
  13163. addVertex( c );
  13164. const nextIndex = verticesArray.length / 3;
  13165. const uvs = uvgen.generateTopUV( scope, verticesArray, nextIndex - 3, nextIndex - 2, nextIndex - 1 );
  13166. addUV( uvs[ 0 ] );
  13167. addUV( uvs[ 1 ] );
  13168. addUV( uvs[ 2 ] );
  13169. }
  13170. function f4( a, b, c, d ) {
  13171. addVertex( a );
  13172. addVertex( b );
  13173. addVertex( d );
  13174. addVertex( b );
  13175. addVertex( c );
  13176. addVertex( d );
  13177. const nextIndex = verticesArray.length / 3;
  13178. const uvs = uvgen.generateSideWallUV( scope, verticesArray, nextIndex - 6, nextIndex - 3, nextIndex - 2, nextIndex - 1 );
  13179. addUV( uvs[ 0 ] );
  13180. addUV( uvs[ 1 ] );
  13181. addUV( uvs[ 3 ] );
  13182. addUV( uvs[ 1 ] );
  13183. addUV( uvs[ 2 ] );
  13184. addUV( uvs[ 3 ] );
  13185. }
  13186. function addVertex( index ) {
  13187. verticesArray.push( placeholder[ index * 3 + 0 ] );
  13188. verticesArray.push( placeholder[ index * 3 + 1 ] );
  13189. verticesArray.push( placeholder[ index * 3 + 2 ] );
  13190. }
  13191. function addUV( vector2 ) {
  13192. uvArray.push( vector2.x );
  13193. uvArray.push( vector2.y );
  13194. }
  13195. }
  13196. }
  13197. copy( source ) {
  13198. super.copy( source );
  13199. this.parameters = Object.assign( {}, source.parameters );
  13200. return this;
  13201. }
  13202. toJSON() {
  13203. const data = super.toJSON();
  13204. const shapes = this.parameters.shapes;
  13205. const options = this.parameters.options;
  13206. return toJSON$1( shapes, options, data );
  13207. }
  13208. static fromJSON( data, shapes ) {
  13209. const geometryShapes = [];
  13210. for ( let j = 0, jl = data.shapes.length; j < jl; j ++ ) {
  13211. const shape = shapes[ data.shapes[ j ] ];
  13212. geometryShapes.push( shape );
  13213. }
  13214. const extrudePath = data.options.extrudePath;
  13215. if ( extrudePath !== undefined ) {
  13216. data.options.extrudePath = new Curves[ extrudePath.type ]().fromJSON( extrudePath );
  13217. }
  13218. return new ExtrudeGeometry( geometryShapes, data.options );
  13219. }
  13220. }
  13221. const WorldUVGenerator = {
  13222. generateTopUV: function ( geometry, vertices, indexA, indexB, indexC ) {
  13223. const a_x = vertices[ indexA * 3 ];
  13224. const a_y = vertices[ indexA * 3 + 1 ];
  13225. const b_x = vertices[ indexB * 3 ];
  13226. const b_y = vertices[ indexB * 3 + 1 ];
  13227. const c_x = vertices[ indexC * 3 ];
  13228. const c_y = vertices[ indexC * 3 + 1 ];
  13229. return [
  13230. new Vector2( a_x, a_y ),
  13231. new Vector2( b_x, b_y ),
  13232. new Vector2( c_x, c_y )
  13233. ];
  13234. },
  13235. generateSideWallUV: function ( geometry, vertices, indexA, indexB, indexC, indexD ) {
  13236. const a_x = vertices[ indexA * 3 ];
  13237. const a_y = vertices[ indexA * 3 + 1 ];
  13238. const a_z = vertices[ indexA * 3 + 2 ];
  13239. const b_x = vertices[ indexB * 3 ];
  13240. const b_y = vertices[ indexB * 3 + 1 ];
  13241. const b_z = vertices[ indexB * 3 + 2 ];
  13242. const c_x = vertices[ indexC * 3 ];
  13243. const c_y = vertices[ indexC * 3 + 1 ];
  13244. const c_z = vertices[ indexC * 3 + 2 ];
  13245. const d_x = vertices[ indexD * 3 ];
  13246. const d_y = vertices[ indexD * 3 + 1 ];
  13247. const d_z = vertices[ indexD * 3 + 2 ];
  13248. if ( Math.abs( a_y - b_y ) < Math.abs( a_x - b_x ) ) {
  13249. return [
  13250. new Vector2( a_x, 1 - a_z ),
  13251. new Vector2( b_x, 1 - b_z ),
  13252. new Vector2( c_x, 1 - c_z ),
  13253. new Vector2( d_x, 1 - d_z )
  13254. ];
  13255. } else {
  13256. return [
  13257. new Vector2( a_y, 1 - a_z ),
  13258. new Vector2( b_y, 1 - b_z ),
  13259. new Vector2( c_y, 1 - c_z ),
  13260. new Vector2( d_y, 1 - d_z )
  13261. ];
  13262. }
  13263. }
  13264. };
  13265. function toJSON$1( shapes, options, data ) {
  13266. data.shapes = [];
  13267. if ( Array.isArray( shapes ) ) {
  13268. for ( let i = 0, l = shapes.length; i < l; i ++ ) {
  13269. const shape = shapes[ i ];
  13270. data.shapes.push( shape.uuid );
  13271. }
  13272. } else {
  13273. data.shapes.push( shapes.uuid );
  13274. }
  13275. data.options = Object.assign( {}, options );
  13276. if ( options.extrudePath !== undefined ) data.options.extrudePath = options.extrudePath.toJSON();
  13277. return data;
  13278. }
  13279. class IcosahedronGeometry extends PolyhedronGeometry {
  13280. constructor( radius = 1, detail = 0 ) {
  13281. const t = ( 1 + Math.sqrt( 5 ) ) / 2;
  13282. const vertices = [
  13283. - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t, 0,
  13284. 0, - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t,
  13285. t, 0, - 1, t, 0, 1, - t, 0, - 1, - t, 0, 1
  13286. ];
  13287. const indices = [
  13288. 0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
  13289. 1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
  13290. 3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
  13291. 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1
  13292. ];
  13293. super( vertices, indices, radius, detail );
  13294. this.type = 'IcosahedronGeometry';
  13295. this.parameters = {
  13296. radius: radius,
  13297. detail: detail
  13298. };
  13299. }
  13300. static fromJSON( data ) {
  13301. return new IcosahedronGeometry( data.radius, data.detail );
  13302. }
  13303. }
  13304. class OctahedronGeometry extends PolyhedronGeometry {
  13305. constructor( radius = 1, detail = 0 ) {
  13306. const vertices = [
  13307. 1, 0, 0, - 1, 0, 0, 0, 1, 0,
  13308. 0, - 1, 0, 0, 0, 1, 0, 0, - 1
  13309. ];
  13310. const indices = [
  13311. 0, 2, 4, 0, 4, 3, 0, 3, 5,
  13312. 0, 5, 2, 1, 2, 5, 1, 5, 3,
  13313. 1, 3, 4, 1, 4, 2
  13314. ];
  13315. super( vertices, indices, radius, detail );
  13316. this.type = 'OctahedronGeometry';
  13317. this.parameters = {
  13318. radius: radius,
  13319. detail: detail
  13320. };
  13321. }
  13322. static fromJSON( data ) {
  13323. return new OctahedronGeometry( data.radius, data.detail );
  13324. }
  13325. }
  13326. class PlaneGeometry extends BufferGeometry {
  13327. constructor( width = 1, height = 1, widthSegments = 1, heightSegments = 1 ) {
  13328. super();
  13329. this.type = 'PlaneGeometry';
  13330. this.parameters = {
  13331. width: width,
  13332. height: height,
  13333. widthSegments: widthSegments,
  13334. heightSegments: heightSegments
  13335. };
  13336. const width_half = width / 2;
  13337. const height_half = height / 2;
  13338. const gridX = Math.floor( widthSegments );
  13339. const gridY = Math.floor( heightSegments );
  13340. const gridX1 = gridX + 1;
  13341. const gridY1 = gridY + 1;
  13342. const segment_width = width / gridX;
  13343. const segment_height = height / gridY;
  13344. //
  13345. const indices = [];
  13346. const vertices = [];
  13347. const normals = [];
  13348. const uvs = [];
  13349. for ( let iy = 0; iy < gridY1; iy ++ ) {
  13350. const y = iy * segment_height - height_half;
  13351. for ( let ix = 0; ix < gridX1; ix ++ ) {
  13352. const x = ix * segment_width - width_half;
  13353. vertices.push( x, - y, 0 );
  13354. normals.push( 0, 0, 1 );
  13355. uvs.push( ix / gridX );
  13356. uvs.push( 1 - ( iy / gridY ) );
  13357. }
  13358. }
  13359. for ( let iy = 0; iy < gridY; iy ++ ) {
  13360. for ( let ix = 0; ix < gridX; ix ++ ) {
  13361. const a = ix + gridX1 * iy;
  13362. const b = ix + gridX1 * ( iy + 1 );
  13363. const c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  13364. const d = ( ix + 1 ) + gridX1 * iy;
  13365. indices.push( a, b, d );
  13366. indices.push( b, c, d );
  13367. }
  13368. }
  13369. this.setIndex( indices );
  13370. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13371. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13372. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13373. }
  13374. copy( source ) {
  13375. super.copy( source );
  13376. this.parameters = Object.assign( {}, source.parameters );
  13377. return this;
  13378. }
  13379. static fromJSON( data ) {
  13380. return new PlaneGeometry( data.width, data.height, data.widthSegments, data.heightSegments );
  13381. }
  13382. }
  13383. class RingGeometry extends BufferGeometry {
  13384. constructor( innerRadius = 0.5, outerRadius = 1, thetaSegments = 32, phiSegments = 1, thetaStart = 0, thetaLength = Math.PI * 2 ) {
  13385. super();
  13386. this.type = 'RingGeometry';
  13387. this.parameters = {
  13388. innerRadius: innerRadius,
  13389. outerRadius: outerRadius,
  13390. thetaSegments: thetaSegments,
  13391. phiSegments: phiSegments,
  13392. thetaStart: thetaStart,
  13393. thetaLength: thetaLength
  13394. };
  13395. thetaSegments = Math.max( 3, thetaSegments );
  13396. phiSegments = Math.max( 1, phiSegments );
  13397. // buffers
  13398. const indices = [];
  13399. const vertices = [];
  13400. const normals = [];
  13401. const uvs = [];
  13402. // some helper variables
  13403. let radius = innerRadius;
  13404. const radiusStep = ( ( outerRadius - innerRadius ) / phiSegments );
  13405. const vertex = new Vector3();
  13406. const uv = new Vector2();
  13407. // generate vertices, normals and uvs
  13408. for ( let j = 0; j <= phiSegments; j ++ ) {
  13409. for ( let i = 0; i <= thetaSegments; i ++ ) {
  13410. // values are generate from the inside of the ring to the outside
  13411. const segment = thetaStart + i / thetaSegments * thetaLength;
  13412. // vertex
  13413. vertex.x = radius * Math.cos( segment );
  13414. vertex.y = radius * Math.sin( segment );
  13415. vertices.push( vertex.x, vertex.y, vertex.z );
  13416. // normal
  13417. normals.push( 0, 0, 1 );
  13418. // uv
  13419. uv.x = ( vertex.x / outerRadius + 1 ) / 2;
  13420. uv.y = ( vertex.y / outerRadius + 1 ) / 2;
  13421. uvs.push( uv.x, uv.y );
  13422. }
  13423. // increase the radius for next row of vertices
  13424. radius += radiusStep;
  13425. }
  13426. // indices
  13427. for ( let j = 0; j < phiSegments; j ++ ) {
  13428. const thetaSegmentLevel = j * ( thetaSegments + 1 );
  13429. for ( let i = 0; i < thetaSegments; i ++ ) {
  13430. const segment = i + thetaSegmentLevel;
  13431. const a = segment;
  13432. const b = segment + thetaSegments + 1;
  13433. const c = segment + thetaSegments + 2;
  13434. const d = segment + 1;
  13435. // faces
  13436. indices.push( a, b, d );
  13437. indices.push( b, c, d );
  13438. }
  13439. }
  13440. // build geometry
  13441. this.setIndex( indices );
  13442. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13443. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13444. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13445. }
  13446. copy( source ) {
  13447. super.copy( source );
  13448. this.parameters = Object.assign( {}, source.parameters );
  13449. return this;
  13450. }
  13451. static fromJSON( data ) {
  13452. return new RingGeometry( data.innerRadius, data.outerRadius, data.thetaSegments, data.phiSegments, data.thetaStart, data.thetaLength );
  13453. }
  13454. }
  13455. class ShapeGeometry extends BufferGeometry {
  13456. constructor( shapes = new Shape( [ new Vector2( 0, 0.5 ), new Vector2( - 0.5, - 0.5 ), new Vector2( 0.5, - 0.5 ) ] ), curveSegments = 12 ) {
  13457. super();
  13458. this.type = 'ShapeGeometry';
  13459. this.parameters = {
  13460. shapes: shapes,
  13461. curveSegments: curveSegments
  13462. };
  13463. // buffers
  13464. const indices = [];
  13465. const vertices = [];
  13466. const normals = [];
  13467. const uvs = [];
  13468. // helper variables
  13469. let groupStart = 0;
  13470. let groupCount = 0;
  13471. // allow single and array values for "shapes" parameter
  13472. if ( Array.isArray( shapes ) === false ) {
  13473. addShape( shapes );
  13474. } else {
  13475. for ( let i = 0; i < shapes.length; i ++ ) {
  13476. addShape( shapes[ i ] );
  13477. this.addGroup( groupStart, groupCount, i ); // enables MultiMaterial support
  13478. groupStart += groupCount;
  13479. groupCount = 0;
  13480. }
  13481. }
  13482. // build geometry
  13483. this.setIndex( indices );
  13484. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13485. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13486. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13487. // helper functions
  13488. function addShape( shape ) {
  13489. const indexOffset = vertices.length / 3;
  13490. const points = shape.extractPoints( curveSegments );
  13491. let shapeVertices = points.shape;
  13492. const shapeHoles = points.holes;
  13493. // check direction of vertices
  13494. if ( ShapeUtils.isClockWise( shapeVertices ) === false ) {
  13495. shapeVertices = shapeVertices.reverse();
  13496. }
  13497. for ( let i = 0, l = shapeHoles.length; i < l; i ++ ) {
  13498. const shapeHole = shapeHoles[ i ];
  13499. if ( ShapeUtils.isClockWise( shapeHole ) === true ) {
  13500. shapeHoles[ i ] = shapeHole.reverse();
  13501. }
  13502. }
  13503. const faces = ShapeUtils.triangulateShape( shapeVertices, shapeHoles );
  13504. // join vertices of inner and outer paths to a single array
  13505. for ( let i = 0, l = shapeHoles.length; i < l; i ++ ) {
  13506. const shapeHole = shapeHoles[ i ];
  13507. shapeVertices = shapeVertices.concat( shapeHole );
  13508. }
  13509. // vertices, normals, uvs
  13510. for ( let i = 0, l = shapeVertices.length; i < l; i ++ ) {
  13511. const vertex = shapeVertices[ i ];
  13512. vertices.push( vertex.x, vertex.y, 0 );
  13513. normals.push( 0, 0, 1 );
  13514. uvs.push( vertex.x, vertex.y ); // world uvs
  13515. }
  13516. // indices
  13517. for ( let i = 0, l = faces.length; i < l; i ++ ) {
  13518. const face = faces[ i ];
  13519. const a = face[ 0 ] + indexOffset;
  13520. const b = face[ 1 ] + indexOffset;
  13521. const c = face[ 2 ] + indexOffset;
  13522. indices.push( a, b, c );
  13523. groupCount += 3;
  13524. }
  13525. }
  13526. }
  13527. copy( source ) {
  13528. super.copy( source );
  13529. this.parameters = Object.assign( {}, source.parameters );
  13530. return this;
  13531. }
  13532. toJSON() {
  13533. const data = super.toJSON();
  13534. const shapes = this.parameters.shapes;
  13535. return toJSON( shapes, data );
  13536. }
  13537. static fromJSON( data, shapes ) {
  13538. const geometryShapes = [];
  13539. for ( let j = 0, jl = data.shapes.length; j < jl; j ++ ) {
  13540. const shape = shapes[ data.shapes[ j ] ];
  13541. geometryShapes.push( shape );
  13542. }
  13543. return new ShapeGeometry( geometryShapes, data.curveSegments );
  13544. }
  13545. }
  13546. function toJSON( shapes, data ) {
  13547. data.shapes = [];
  13548. if ( Array.isArray( shapes ) ) {
  13549. for ( let i = 0, l = shapes.length; i < l; i ++ ) {
  13550. const shape = shapes[ i ];
  13551. data.shapes.push( shape.uuid );
  13552. }
  13553. } else {
  13554. data.shapes.push( shapes.uuid );
  13555. }
  13556. return data;
  13557. }
  13558. class SphereGeometry extends BufferGeometry {
  13559. constructor( radius = 1, widthSegments = 32, heightSegments = 16, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI ) {
  13560. super();
  13561. this.type = 'SphereGeometry';
  13562. this.parameters = {
  13563. radius: radius,
  13564. widthSegments: widthSegments,
  13565. heightSegments: heightSegments,
  13566. phiStart: phiStart,
  13567. phiLength: phiLength,
  13568. thetaStart: thetaStart,
  13569. thetaLength: thetaLength
  13570. };
  13571. widthSegments = Math.max( 3, Math.floor( widthSegments ) );
  13572. heightSegments = Math.max( 2, Math.floor( heightSegments ) );
  13573. const thetaEnd = Math.min( thetaStart + thetaLength, Math.PI );
  13574. let index = 0;
  13575. const grid = [];
  13576. const vertex = new Vector3();
  13577. const normal = new Vector3();
  13578. // buffers
  13579. const indices = [];
  13580. const vertices = [];
  13581. const normals = [];
  13582. const uvs = [];
  13583. // generate vertices, normals and uvs
  13584. for ( let iy = 0; iy <= heightSegments; iy ++ ) {
  13585. const verticesRow = [];
  13586. const v = iy / heightSegments;
  13587. // special case for the poles
  13588. let uOffset = 0;
  13589. if ( iy === 0 && thetaStart === 0 ) {
  13590. uOffset = 0.5 / widthSegments;
  13591. } else if ( iy === heightSegments && thetaEnd === Math.PI ) {
  13592. uOffset = - 0.5 / widthSegments;
  13593. }
  13594. for ( let ix = 0; ix <= widthSegments; ix ++ ) {
  13595. const u = ix / widthSegments;
  13596. // vertex
  13597. vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  13598. vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
  13599. vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  13600. vertices.push( vertex.x, vertex.y, vertex.z );
  13601. // normal
  13602. normal.copy( vertex ).normalize();
  13603. normals.push( normal.x, normal.y, normal.z );
  13604. // uv
  13605. uvs.push( u + uOffset, 1 - v );
  13606. verticesRow.push( index ++ );
  13607. }
  13608. grid.push( verticesRow );
  13609. }
  13610. // indices
  13611. for ( let iy = 0; iy < heightSegments; iy ++ ) {
  13612. for ( let ix = 0; ix < widthSegments; ix ++ ) {
  13613. const a = grid[ iy ][ ix + 1 ];
  13614. const b = grid[ iy ][ ix ];
  13615. const c = grid[ iy + 1 ][ ix ];
  13616. const d = grid[ iy + 1 ][ ix + 1 ];
  13617. if ( iy !== 0 || thetaStart > 0 ) indices.push( a, b, d );
  13618. if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) indices.push( b, c, d );
  13619. }
  13620. }
  13621. // build geometry
  13622. this.setIndex( indices );
  13623. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13624. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13625. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13626. }
  13627. copy( source ) {
  13628. super.copy( source );
  13629. this.parameters = Object.assign( {}, source.parameters );
  13630. return this;
  13631. }
  13632. static fromJSON( data ) {
  13633. return new SphereGeometry( data.radius, data.widthSegments, data.heightSegments, data.phiStart, data.phiLength, data.thetaStart, data.thetaLength );
  13634. }
  13635. }
  13636. class TetrahedronGeometry extends PolyhedronGeometry {
  13637. constructor( radius = 1, detail = 0 ) {
  13638. const vertices = [
  13639. 1, 1, 1, - 1, - 1, 1, - 1, 1, - 1, 1, - 1, - 1
  13640. ];
  13641. const indices = [
  13642. 2, 1, 0, 0, 3, 2, 1, 3, 0, 2, 3, 1
  13643. ];
  13644. super( vertices, indices, radius, detail );
  13645. this.type = 'TetrahedronGeometry';
  13646. this.parameters = {
  13647. radius: radius,
  13648. detail: detail
  13649. };
  13650. }
  13651. static fromJSON( data ) {
  13652. return new TetrahedronGeometry( data.radius, data.detail );
  13653. }
  13654. }
  13655. class TorusGeometry extends BufferGeometry {
  13656. constructor( radius = 1, tube = 0.4, radialSegments = 12, tubularSegments = 48, arc = Math.PI * 2 ) {
  13657. super();
  13658. this.type = 'TorusGeometry';
  13659. this.parameters = {
  13660. radius: radius,
  13661. tube: tube,
  13662. radialSegments: radialSegments,
  13663. tubularSegments: tubularSegments,
  13664. arc: arc
  13665. };
  13666. radialSegments = Math.floor( radialSegments );
  13667. tubularSegments = Math.floor( tubularSegments );
  13668. // buffers
  13669. const indices = [];
  13670. const vertices = [];
  13671. const normals = [];
  13672. const uvs = [];
  13673. // helper variables
  13674. const center = new Vector3();
  13675. const vertex = new Vector3();
  13676. const normal = new Vector3();
  13677. // generate vertices, normals and uvs
  13678. for ( let j = 0; j <= radialSegments; j ++ ) {
  13679. for ( let i = 0; i <= tubularSegments; i ++ ) {
  13680. const u = i / tubularSegments * arc;
  13681. const v = j / radialSegments * Math.PI * 2;
  13682. // vertex
  13683. vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u );
  13684. vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u );
  13685. vertex.z = tube * Math.sin( v );
  13686. vertices.push( vertex.x, vertex.y, vertex.z );
  13687. // normal
  13688. center.x = radius * Math.cos( u );
  13689. center.y = radius * Math.sin( u );
  13690. normal.subVectors( vertex, center ).normalize();
  13691. normals.push( normal.x, normal.y, normal.z );
  13692. // uv
  13693. uvs.push( i / tubularSegments );
  13694. uvs.push( j / radialSegments );
  13695. }
  13696. }
  13697. // generate indices
  13698. for ( let j = 1; j <= radialSegments; j ++ ) {
  13699. for ( let i = 1; i <= tubularSegments; i ++ ) {
  13700. // indices
  13701. const a = ( tubularSegments + 1 ) * j + i - 1;
  13702. const b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1;
  13703. const c = ( tubularSegments + 1 ) * ( j - 1 ) + i;
  13704. const d = ( tubularSegments + 1 ) * j + i;
  13705. // faces
  13706. indices.push( a, b, d );
  13707. indices.push( b, c, d );
  13708. }
  13709. }
  13710. // build geometry
  13711. this.setIndex( indices );
  13712. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13713. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13714. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13715. }
  13716. copy( source ) {
  13717. super.copy( source );
  13718. this.parameters = Object.assign( {}, source.parameters );
  13719. return this;
  13720. }
  13721. static fromJSON( data ) {
  13722. return new TorusGeometry( data.radius, data.tube, data.radialSegments, data.tubularSegments, data.arc );
  13723. }
  13724. }
  13725. class TorusKnotGeometry extends BufferGeometry {
  13726. constructor( radius = 1, tube = 0.4, tubularSegments = 64, radialSegments = 8, p = 2, q = 3 ) {
  13727. super();
  13728. this.type = 'TorusKnotGeometry';
  13729. this.parameters = {
  13730. radius: radius,
  13731. tube: tube,
  13732. tubularSegments: tubularSegments,
  13733. radialSegments: radialSegments,
  13734. p: p,
  13735. q: q
  13736. };
  13737. tubularSegments = Math.floor( tubularSegments );
  13738. radialSegments = Math.floor( radialSegments );
  13739. // buffers
  13740. const indices = [];
  13741. const vertices = [];
  13742. const normals = [];
  13743. const uvs = [];
  13744. // helper variables
  13745. const vertex = new Vector3();
  13746. const normal = new Vector3();
  13747. const P1 = new Vector3();
  13748. const P2 = new Vector3();
  13749. const B = new Vector3();
  13750. const T = new Vector3();
  13751. const N = new Vector3();
  13752. // generate vertices, normals and uvs
  13753. for ( let i = 0; i <= tubularSegments; ++ i ) {
  13754. // the radian "u" is used to calculate the position on the torus curve of the current tubular segment
  13755. const u = i / tubularSegments * p * Math.PI * 2;
  13756. // now we calculate two points. P1 is our current position on the curve, P2 is a little farther ahead.
  13757. // these points are used to create a special "coordinate space", which is necessary to calculate the correct vertex positions
  13758. calculatePositionOnCurve( u, p, q, radius, P1 );
  13759. calculatePositionOnCurve( u + 0.01, p, q, radius, P2 );
  13760. // calculate orthonormal basis
  13761. T.subVectors( P2, P1 );
  13762. N.addVectors( P2, P1 );
  13763. B.crossVectors( T, N );
  13764. N.crossVectors( B, T );
  13765. // normalize B, N. T can be ignored, we don't use it
  13766. B.normalize();
  13767. N.normalize();
  13768. for ( let j = 0; j <= radialSegments; ++ j ) {
  13769. // now calculate the vertices. they are nothing more than an extrusion of the torus curve.
  13770. // because we extrude a shape in the xy-plane, there is no need to calculate a z-value.
  13771. const v = j / radialSegments * Math.PI * 2;
  13772. const cx = - tube * Math.cos( v );
  13773. const cy = tube * Math.sin( v );
  13774. // now calculate the final vertex position.
  13775. // first we orient the extrusion with our basis vectors, then we add it to the current position on the curve
  13776. vertex.x = P1.x + ( cx * N.x + cy * B.x );
  13777. vertex.y = P1.y + ( cx * N.y + cy * B.y );
  13778. vertex.z = P1.z + ( cx * N.z + cy * B.z );
  13779. vertices.push( vertex.x, vertex.y, vertex.z );
  13780. // normal (P1 is always the center/origin of the extrusion, thus we can use it to calculate the normal)
  13781. normal.subVectors( vertex, P1 ).normalize();
  13782. normals.push( normal.x, normal.y, normal.z );
  13783. // uv
  13784. uvs.push( i / tubularSegments );
  13785. uvs.push( j / radialSegments );
  13786. }
  13787. }
  13788. // generate indices
  13789. for ( let j = 1; j <= tubularSegments; j ++ ) {
  13790. for ( let i = 1; i <= radialSegments; i ++ ) {
  13791. // indices
  13792. const a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
  13793. const b = ( radialSegments + 1 ) * j + ( i - 1 );
  13794. const c = ( radialSegments + 1 ) * j + i;
  13795. const d = ( radialSegments + 1 ) * ( j - 1 ) + i;
  13796. // faces
  13797. indices.push( a, b, d );
  13798. indices.push( b, c, d );
  13799. }
  13800. }
  13801. // build geometry
  13802. this.setIndex( indices );
  13803. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13804. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13805. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13806. // this function calculates the current position on the torus curve
  13807. function calculatePositionOnCurve( u, p, q, radius, position ) {
  13808. const cu = Math.cos( u );
  13809. const su = Math.sin( u );
  13810. const quOverP = q / p * u;
  13811. const cs = Math.cos( quOverP );
  13812. position.x = radius * ( 2 + cs ) * 0.5 * cu;
  13813. position.y = radius * ( 2 + cs ) * su * 0.5;
  13814. position.z = radius * Math.sin( quOverP ) * 0.5;
  13815. }
  13816. }
  13817. copy( source ) {
  13818. super.copy( source );
  13819. this.parameters = Object.assign( {}, source.parameters );
  13820. return this;
  13821. }
  13822. static fromJSON( data ) {
  13823. return new TorusKnotGeometry( data.radius, data.tube, data.tubularSegments, data.radialSegments, data.p, data.q );
  13824. }
  13825. }
  13826. class TubeGeometry extends BufferGeometry {
  13827. 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 ) {
  13828. super();
  13829. this.type = 'TubeGeometry';
  13830. this.parameters = {
  13831. path: path,
  13832. tubularSegments: tubularSegments,
  13833. radius: radius,
  13834. radialSegments: radialSegments,
  13835. closed: closed
  13836. };
  13837. const frames = path.computeFrenetFrames( tubularSegments, closed );
  13838. // expose internals
  13839. this.tangents = frames.tangents;
  13840. this.normals = frames.normals;
  13841. this.binormals = frames.binormals;
  13842. // helper variables
  13843. const vertex = new Vector3();
  13844. const normal = new Vector3();
  13845. const uv = new Vector2();
  13846. let P = new Vector3();
  13847. // buffer
  13848. const vertices = [];
  13849. const normals = [];
  13850. const uvs = [];
  13851. const indices = [];
  13852. // create buffer data
  13853. generateBufferData();
  13854. // build geometry
  13855. this.setIndex( indices );
  13856. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  13857. this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) );
  13858. this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) );
  13859. // functions
  13860. function generateBufferData() {
  13861. for ( let i = 0; i < tubularSegments; i ++ ) {
  13862. generateSegment( i );
  13863. }
  13864. // if the geometry is not closed, generate the last row of vertices and normals
  13865. // at the regular position on the given path
  13866. //
  13867. // if the geometry is closed, duplicate the first row of vertices and normals (uvs will differ)
  13868. generateSegment( ( closed === false ) ? tubularSegments : 0 );
  13869. // uvs are generated in a separate function.
  13870. // this makes it easy compute correct values for closed geometries
  13871. generateUVs();
  13872. // finally create faces
  13873. generateIndices();
  13874. }
  13875. function generateSegment( i ) {
  13876. // we use getPointAt to sample evenly distributed points from the given path
  13877. P = path.getPointAt( i / tubularSegments, P );
  13878. // retrieve corresponding normal and binormal
  13879. const N = frames.normals[ i ];
  13880. const B = frames.binormals[ i ];
  13881. // generate normals and vertices for the current segment
  13882. for ( let j = 0; j <= radialSegments; j ++ ) {
  13883. const v = j / radialSegments * Math.PI * 2;
  13884. const sin = Math.sin( v );
  13885. const cos = - Math.cos( v );
  13886. // normal
  13887. normal.x = ( cos * N.x + sin * B.x );
  13888. normal.y = ( cos * N.y + sin * B.y );
  13889. normal.z = ( cos * N.z + sin * B.z );
  13890. normal.normalize();
  13891. normals.push( normal.x, normal.y, normal.z );
  13892. // vertex
  13893. vertex.x = P.x + radius * normal.x;
  13894. vertex.y = P.y + radius * normal.y;
  13895. vertex.z = P.z + radius * normal.z;
  13896. vertices.push( vertex.x, vertex.y, vertex.z );
  13897. }
  13898. }
  13899. function generateIndices() {
  13900. for ( let j = 1; j <= tubularSegments; j ++ ) {
  13901. for ( let i = 1; i <= radialSegments; i ++ ) {
  13902. const a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 );
  13903. const b = ( radialSegments + 1 ) * j + ( i - 1 );
  13904. const c = ( radialSegments + 1 ) * j + i;
  13905. const d = ( radialSegments + 1 ) * ( j - 1 ) + i;
  13906. // faces
  13907. indices.push( a, b, d );
  13908. indices.push( b, c, d );
  13909. }
  13910. }
  13911. }
  13912. function generateUVs() {
  13913. for ( let i = 0; i <= tubularSegments; i ++ ) {
  13914. for ( let j = 0; j <= radialSegments; j ++ ) {
  13915. uv.x = i / tubularSegments;
  13916. uv.y = j / radialSegments;
  13917. uvs.push( uv.x, uv.y );
  13918. }
  13919. }
  13920. }
  13921. }
  13922. copy( source ) {
  13923. super.copy( source );
  13924. this.parameters = Object.assign( {}, source.parameters );
  13925. return this;
  13926. }
  13927. toJSON() {
  13928. const data = super.toJSON();
  13929. data.path = this.parameters.path.toJSON();
  13930. return data;
  13931. }
  13932. static fromJSON( data ) {
  13933. // This only works for built-in curves (e.g. CatmullRomCurve3).
  13934. // User defined curves or instances of CurvePath will not be deserialized.
  13935. return new TubeGeometry(
  13936. new Curves[ data.path.type ]().fromJSON( data.path ),
  13937. data.tubularSegments,
  13938. data.radius,
  13939. data.radialSegments,
  13940. data.closed
  13941. );
  13942. }
  13943. }
  13944. class WireframeGeometry extends BufferGeometry {
  13945. constructor( geometry = null ) {
  13946. super();
  13947. this.type = 'WireframeGeometry';
  13948. this.parameters = {
  13949. geometry: geometry
  13950. };
  13951. if ( geometry !== null ) {
  13952. // buffer
  13953. const vertices = [];
  13954. const edges = new Set();
  13955. // helper variables
  13956. const start = new Vector3();
  13957. const end = new Vector3();
  13958. if ( geometry.index !== null ) {
  13959. // indexed BufferGeometry
  13960. const position = geometry.attributes.position;
  13961. const indices = geometry.index;
  13962. let groups = geometry.groups;
  13963. if ( groups.length === 0 ) {
  13964. groups = [ { start: 0, count: indices.count, materialIndex: 0 } ];
  13965. }
  13966. // create a data structure that contains all edges without duplicates
  13967. for ( let o = 0, ol = groups.length; o < ol; ++ o ) {
  13968. const group = groups[ o ];
  13969. const groupStart = group.start;
  13970. const groupCount = group.count;
  13971. for ( let i = groupStart, l = ( groupStart + groupCount ); i < l; i += 3 ) {
  13972. for ( let j = 0; j < 3; j ++ ) {
  13973. const index1 = indices.getX( i + j );
  13974. const index2 = indices.getX( i + ( j + 1 ) % 3 );
  13975. start.fromBufferAttribute( position, index1 );
  13976. end.fromBufferAttribute( position, index2 );
  13977. if ( isUniqueEdge( start, end, edges ) === true ) {
  13978. vertices.push( start.x, start.y, start.z );
  13979. vertices.push( end.x, end.y, end.z );
  13980. }
  13981. }
  13982. }
  13983. }
  13984. } else {
  13985. // non-indexed BufferGeometry
  13986. const position = geometry.attributes.position;
  13987. for ( let i = 0, l = ( position.count / 3 ); i < l; i ++ ) {
  13988. for ( let j = 0; j < 3; j ++ ) {
  13989. // three edges per triangle, an edge is represented as (index1, index2)
  13990. // e.g. the first triangle has the following edges: (0,1),(1,2),(2,0)
  13991. const index1 = 3 * i + j;
  13992. const index2 = 3 * i + ( ( j + 1 ) % 3 );
  13993. start.fromBufferAttribute( position, index1 );
  13994. end.fromBufferAttribute( position, index2 );
  13995. if ( isUniqueEdge( start, end, edges ) === true ) {
  13996. vertices.push( start.x, start.y, start.z );
  13997. vertices.push( end.x, end.y, end.z );
  13998. }
  13999. }
  14000. }
  14001. }
  14002. // build geometry
  14003. this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  14004. }
  14005. }
  14006. copy( source ) {
  14007. super.copy( source );
  14008. this.parameters = Object.assign( {}, source.parameters );
  14009. return this;
  14010. }
  14011. }
  14012. function isUniqueEdge( start, end, edges ) {
  14013. const hash1 = `${start.x},${start.y},${start.z}-${end.x},${end.y},${end.z}`;
  14014. const hash2 = `${end.x},${end.y},${end.z}-${start.x},${start.y},${start.z}`; // coincident edge
  14015. if ( edges.has( hash1 ) === true || edges.has( hash2 ) === true ) {
  14016. return false;
  14017. } else {
  14018. edges.add( hash1 );
  14019. edges.add( hash2 );
  14020. return true;
  14021. }
  14022. }
  14023. var Geometries = /*#__PURE__*/Object.freeze({
  14024. __proto__: null,
  14025. BoxGeometry: BoxGeometry,
  14026. CapsuleGeometry: CapsuleGeometry,
  14027. CircleGeometry: CircleGeometry,
  14028. ConeGeometry: ConeGeometry,
  14029. CylinderGeometry: CylinderGeometry,
  14030. DodecahedronGeometry: DodecahedronGeometry,
  14031. EdgesGeometry: EdgesGeometry,
  14032. ExtrudeGeometry: ExtrudeGeometry,
  14033. IcosahedronGeometry: IcosahedronGeometry,
  14034. LatheGeometry: LatheGeometry,
  14035. OctahedronGeometry: OctahedronGeometry,
  14036. PlaneGeometry: PlaneGeometry,
  14037. PolyhedronGeometry: PolyhedronGeometry,
  14038. RingGeometry: RingGeometry,
  14039. ShapeGeometry: ShapeGeometry,
  14040. SphereGeometry: SphereGeometry,
  14041. TetrahedronGeometry: TetrahedronGeometry,
  14042. TorusGeometry: TorusGeometry,
  14043. TorusKnotGeometry: TorusKnotGeometry,
  14044. TubeGeometry: TubeGeometry,
  14045. WireframeGeometry: WireframeGeometry
  14046. });
  14047. class ShadowMaterial extends Material {
  14048. constructor( parameters ) {
  14049. super();
  14050. this.isShadowMaterial = true;
  14051. this.type = 'ShadowMaterial';
  14052. this.color = new Color( 0x000000 );
  14053. this.transparent = true;
  14054. this.fog = true;
  14055. this.setValues( parameters );
  14056. }
  14057. copy( source ) {
  14058. super.copy( source );
  14059. this.color.copy( source.color );
  14060. this.fog = source.fog;
  14061. return this;
  14062. }
  14063. }
  14064. class RawShaderMaterial extends ShaderMaterial {
  14065. constructor( parameters ) {
  14066. super( parameters );
  14067. this.isRawShaderMaterial = true;
  14068. this.type = 'RawShaderMaterial';
  14069. }
  14070. }
  14071. class MeshStandardMaterial extends Material {
  14072. constructor( parameters ) {
  14073. super();
  14074. this.isMeshStandardMaterial = true;
  14075. this.type = 'MeshStandardMaterial';
  14076. this.defines = { 'STANDARD': '' };
  14077. this.color = new Color( 0xffffff ); // diffuse
  14078. this.roughness = 1.0;
  14079. this.metalness = 0.0;
  14080. this.map = null;
  14081. this.lightMap = null;
  14082. this.lightMapIntensity = 1.0;
  14083. this.aoMap = null;
  14084. this.aoMapIntensity = 1.0;
  14085. this.emissive = new Color( 0x000000 );
  14086. this.emissiveIntensity = 1.0;
  14087. this.emissiveMap = null;
  14088. this.bumpMap = null;
  14089. this.bumpScale = 1;
  14090. this.normalMap = null;
  14091. this.normalMapType = TangentSpaceNormalMap;
  14092. this.normalScale = new Vector2( 1, 1 );
  14093. this.displacementMap = null;
  14094. this.displacementScale = 1;
  14095. this.displacementBias = 0;
  14096. this.roughnessMap = null;
  14097. this.metalnessMap = null;
  14098. this.alphaMap = null;
  14099. this.envMap = null;
  14100. this.envMapRotation = new Euler();
  14101. this.envMapIntensity = 1.0;
  14102. this.wireframe = false;
  14103. this.wireframeLinewidth = 1;
  14104. this.wireframeLinecap = 'round';
  14105. this.wireframeLinejoin = 'round';
  14106. this.flatShading = false;
  14107. this.fog = true;
  14108. this.setValues( parameters );
  14109. }
  14110. copy( source ) {
  14111. super.copy( source );
  14112. this.defines = { 'STANDARD': '' };
  14113. this.color.copy( source.color );
  14114. this.roughness = source.roughness;
  14115. this.metalness = source.metalness;
  14116. this.map = source.map;
  14117. this.lightMap = source.lightMap;
  14118. this.lightMapIntensity = source.lightMapIntensity;
  14119. this.aoMap = source.aoMap;
  14120. this.aoMapIntensity = source.aoMapIntensity;
  14121. this.emissive.copy( source.emissive );
  14122. this.emissiveMap = source.emissiveMap;
  14123. this.emissiveIntensity = source.emissiveIntensity;
  14124. this.bumpMap = source.bumpMap;
  14125. this.bumpScale = source.bumpScale;
  14126. this.normalMap = source.normalMap;
  14127. this.normalMapType = source.normalMapType;
  14128. this.normalScale.copy( source.normalScale );
  14129. this.displacementMap = source.displacementMap;
  14130. this.displacementScale = source.displacementScale;
  14131. this.displacementBias = source.displacementBias;
  14132. this.roughnessMap = source.roughnessMap;
  14133. this.metalnessMap = source.metalnessMap;
  14134. this.alphaMap = source.alphaMap;
  14135. this.envMap = source.envMap;
  14136. this.envMapRotation.copy( source.envMapRotation );
  14137. this.envMapIntensity = source.envMapIntensity;
  14138. this.wireframe = source.wireframe;
  14139. this.wireframeLinewidth = source.wireframeLinewidth;
  14140. this.wireframeLinecap = source.wireframeLinecap;
  14141. this.wireframeLinejoin = source.wireframeLinejoin;
  14142. this.flatShading = source.flatShading;
  14143. this.fog = source.fog;
  14144. return this;
  14145. }
  14146. }
  14147. class MeshPhysicalMaterial extends MeshStandardMaterial {
  14148. constructor( parameters ) {
  14149. super();
  14150. this.isMeshPhysicalMaterial = true;
  14151. this.defines = {
  14152. 'STANDARD': '',
  14153. 'PHYSICAL': ''
  14154. };
  14155. this.type = 'MeshPhysicalMaterial';
  14156. this.anisotropyRotation = 0;
  14157. this.anisotropyMap = null;
  14158. this.clearcoatMap = null;
  14159. this.clearcoatRoughness = 0.0;
  14160. this.clearcoatRoughnessMap = null;
  14161. this.clearcoatNormalScale = new Vector2( 1, 1 );
  14162. this.clearcoatNormalMap = null;
  14163. this.ior = 1.5;
  14164. Object.defineProperty( this, 'reflectivity', {
  14165. get: function () {
  14166. return ( clamp( 2.5 * ( this.ior - 1 ) / ( this.ior + 1 ), 0, 1 ) );
  14167. },
  14168. set: function ( reflectivity ) {
  14169. this.ior = ( 1 + 0.4 * reflectivity ) / ( 1 - 0.4 * reflectivity );
  14170. }
  14171. } );
  14172. this.iridescenceMap = null;
  14173. this.iridescenceIOR = 1.3;
  14174. this.iridescenceThicknessRange = [ 100, 400 ];
  14175. this.iridescenceThicknessMap = null;
  14176. this.sheenColor = new Color( 0x000000 );
  14177. this.sheenColorMap = null;
  14178. this.sheenRoughness = 1.0;
  14179. this.sheenRoughnessMap = null;
  14180. this.transmissionMap = null;
  14181. this.thickness = 0;
  14182. this.thicknessMap = null;
  14183. this.attenuationDistance = Infinity;
  14184. this.attenuationColor = new Color( 1, 1, 1 );
  14185. this.specularIntensity = 1.0;
  14186. this.specularIntensityMap = null;
  14187. this.specularColor = new Color( 1, 1, 1 );
  14188. this.specularColorMap = null;
  14189. this._anisotropy = 0;
  14190. this._clearcoat = 0;
  14191. this._dispersion = 0;
  14192. this._iridescence = 0;
  14193. this._sheen = 0.0;
  14194. this._transmission = 0;
  14195. this.setValues( parameters );
  14196. }
  14197. get anisotropy() {
  14198. return this._anisotropy;
  14199. }
  14200. set anisotropy( value ) {
  14201. if ( this._anisotropy > 0 !== value > 0 ) {
  14202. this.version ++;
  14203. }
  14204. this._anisotropy = value;
  14205. }
  14206. get clearcoat() {
  14207. return this._clearcoat;
  14208. }
  14209. set clearcoat( value ) {
  14210. if ( this._clearcoat > 0 !== value > 0 ) {
  14211. this.version ++;
  14212. }
  14213. this._clearcoat = value;
  14214. }
  14215. get iridescence() {
  14216. return this._iridescence;
  14217. }
  14218. set iridescence( value ) {
  14219. if ( this._iridescence > 0 !== value > 0 ) {
  14220. this.version ++;
  14221. }
  14222. this._iridescence = value;
  14223. }
  14224. get dispersion() {
  14225. return this._dispersion;
  14226. }
  14227. set dispersion( value ) {
  14228. if ( this._dispersion > 0 !== value > 0 ) {
  14229. this.version ++;
  14230. }
  14231. this._dispersion = value;
  14232. }
  14233. get sheen() {
  14234. return this._sheen;
  14235. }
  14236. set sheen( value ) {
  14237. if ( this._sheen > 0 !== value > 0 ) {
  14238. this.version ++;
  14239. }
  14240. this._sheen = value;
  14241. }
  14242. get transmission() {
  14243. return this._transmission;
  14244. }
  14245. set transmission( value ) {
  14246. if ( this._transmission > 0 !== value > 0 ) {
  14247. this.version ++;
  14248. }
  14249. this._transmission = value;
  14250. }
  14251. copy( source ) {
  14252. super.copy( source );
  14253. this.defines = {
  14254. 'STANDARD': '',
  14255. 'PHYSICAL': ''
  14256. };
  14257. this.anisotropy = source.anisotropy;
  14258. this.anisotropyRotation = source.anisotropyRotation;
  14259. this.anisotropyMap = source.anisotropyMap;
  14260. this.clearcoat = source.clearcoat;
  14261. this.clearcoatMap = source.clearcoatMap;
  14262. this.clearcoatRoughness = source.clearcoatRoughness;
  14263. this.clearcoatRoughnessMap = source.clearcoatRoughnessMap;
  14264. this.clearcoatNormalMap = source.clearcoatNormalMap;
  14265. this.clearcoatNormalScale.copy( source.clearcoatNormalScale );
  14266. this.dispersion = source.dispersion;
  14267. this.ior = source.ior;
  14268. this.iridescence = source.iridescence;
  14269. this.iridescenceMap = source.iridescenceMap;
  14270. this.iridescenceIOR = source.iridescenceIOR;
  14271. this.iridescenceThicknessRange = [ ...source.iridescenceThicknessRange ];
  14272. this.iridescenceThicknessMap = source.iridescenceThicknessMap;
  14273. this.sheen = source.sheen;
  14274. this.sheenColor.copy( source.sheenColor );
  14275. this.sheenColorMap = source.sheenColorMap;
  14276. this.sheenRoughness = source.sheenRoughness;
  14277. this.sheenRoughnessMap = source.sheenRoughnessMap;
  14278. this.transmission = source.transmission;
  14279. this.transmissionMap = source.transmissionMap;
  14280. this.thickness = source.thickness;
  14281. this.thicknessMap = source.thicknessMap;
  14282. this.attenuationDistance = source.attenuationDistance;
  14283. this.attenuationColor.copy( source.attenuationColor );
  14284. this.specularIntensity = source.specularIntensity;
  14285. this.specularIntensityMap = source.specularIntensityMap;
  14286. this.specularColor.copy( source.specularColor );
  14287. this.specularColorMap = source.specularColorMap;
  14288. return this;
  14289. }
  14290. }
  14291. class MeshPhongMaterial extends Material {
  14292. constructor( parameters ) {
  14293. super();
  14294. this.isMeshPhongMaterial = true;
  14295. this.type = 'MeshPhongMaterial';
  14296. this.color = new Color( 0xffffff ); // diffuse
  14297. this.specular = new Color( 0x111111 );
  14298. this.shininess = 30;
  14299. this.map = null;
  14300. this.lightMap = null;
  14301. this.lightMapIntensity = 1.0;
  14302. this.aoMap = null;
  14303. this.aoMapIntensity = 1.0;
  14304. this.emissive = new Color( 0x000000 );
  14305. this.emissiveIntensity = 1.0;
  14306. this.emissiveMap = null;
  14307. this.bumpMap = null;
  14308. this.bumpScale = 1;
  14309. this.normalMap = null;
  14310. this.normalMapType = TangentSpaceNormalMap;
  14311. this.normalScale = new Vector2( 1, 1 );
  14312. this.displacementMap = null;
  14313. this.displacementScale = 1;
  14314. this.displacementBias = 0;
  14315. this.specularMap = null;
  14316. this.alphaMap = null;
  14317. this.envMap = null;
  14318. this.envMapRotation = new Euler();
  14319. this.combine = MultiplyOperation;
  14320. this.reflectivity = 1;
  14321. this.refractionRatio = 0.98;
  14322. this.wireframe = false;
  14323. this.wireframeLinewidth = 1;
  14324. this.wireframeLinecap = 'round';
  14325. this.wireframeLinejoin = 'round';
  14326. this.flatShading = false;
  14327. this.fog = true;
  14328. this.setValues( parameters );
  14329. }
  14330. copy( source ) {
  14331. super.copy( source );
  14332. this.color.copy( source.color );
  14333. this.specular.copy( source.specular );
  14334. this.shininess = source.shininess;
  14335. this.map = source.map;
  14336. this.lightMap = source.lightMap;
  14337. this.lightMapIntensity = source.lightMapIntensity;
  14338. this.aoMap = source.aoMap;
  14339. this.aoMapIntensity = source.aoMapIntensity;
  14340. this.emissive.copy( source.emissive );
  14341. this.emissiveMap = source.emissiveMap;
  14342. this.emissiveIntensity = source.emissiveIntensity;
  14343. this.bumpMap = source.bumpMap;
  14344. this.bumpScale = source.bumpScale;
  14345. this.normalMap = source.normalMap;
  14346. this.normalMapType = source.normalMapType;
  14347. this.normalScale.copy( source.normalScale );
  14348. this.displacementMap = source.displacementMap;
  14349. this.displacementScale = source.displacementScale;
  14350. this.displacementBias = source.displacementBias;
  14351. this.specularMap = source.specularMap;
  14352. this.alphaMap = source.alphaMap;
  14353. this.envMap = source.envMap;
  14354. this.envMapRotation.copy( source.envMapRotation );
  14355. this.combine = source.combine;
  14356. this.reflectivity = source.reflectivity;
  14357. this.refractionRatio = source.refractionRatio;
  14358. this.wireframe = source.wireframe;
  14359. this.wireframeLinewidth = source.wireframeLinewidth;
  14360. this.wireframeLinecap = source.wireframeLinecap;
  14361. this.wireframeLinejoin = source.wireframeLinejoin;
  14362. this.flatShading = source.flatShading;
  14363. this.fog = source.fog;
  14364. return this;
  14365. }
  14366. }
  14367. class MeshToonMaterial extends Material {
  14368. constructor( parameters ) {
  14369. super();
  14370. this.isMeshToonMaterial = true;
  14371. this.defines = { 'TOON': '' };
  14372. this.type = 'MeshToonMaterial';
  14373. this.color = new Color( 0xffffff );
  14374. this.map = null;
  14375. this.gradientMap = null;
  14376. this.lightMap = null;
  14377. this.lightMapIntensity = 1.0;
  14378. this.aoMap = null;
  14379. this.aoMapIntensity = 1.0;
  14380. this.emissive = new Color( 0x000000 );
  14381. this.emissiveIntensity = 1.0;
  14382. this.emissiveMap = null;
  14383. this.bumpMap = null;
  14384. this.bumpScale = 1;
  14385. this.normalMap = null;
  14386. this.normalMapType = TangentSpaceNormalMap;
  14387. this.normalScale = new Vector2( 1, 1 );
  14388. this.displacementMap = null;
  14389. this.displacementScale = 1;
  14390. this.displacementBias = 0;
  14391. this.alphaMap = null;
  14392. this.wireframe = false;
  14393. this.wireframeLinewidth = 1;
  14394. this.wireframeLinecap = 'round';
  14395. this.wireframeLinejoin = 'round';
  14396. this.fog = true;
  14397. this.setValues( parameters );
  14398. }
  14399. copy( source ) {
  14400. super.copy( source );
  14401. this.color.copy( source.color );
  14402. this.map = source.map;
  14403. this.gradientMap = source.gradientMap;
  14404. this.lightMap = source.lightMap;
  14405. this.lightMapIntensity = source.lightMapIntensity;
  14406. this.aoMap = source.aoMap;
  14407. this.aoMapIntensity = source.aoMapIntensity;
  14408. this.emissive.copy( source.emissive );
  14409. this.emissiveMap = source.emissiveMap;
  14410. this.emissiveIntensity = source.emissiveIntensity;
  14411. this.bumpMap = source.bumpMap;
  14412. this.bumpScale = source.bumpScale;
  14413. this.normalMap = source.normalMap;
  14414. this.normalMapType = source.normalMapType;
  14415. this.normalScale.copy( source.normalScale );
  14416. this.displacementMap = source.displacementMap;
  14417. this.displacementScale = source.displacementScale;
  14418. this.displacementBias = source.displacementBias;
  14419. this.alphaMap = source.alphaMap;
  14420. this.wireframe = source.wireframe;
  14421. this.wireframeLinewidth = source.wireframeLinewidth;
  14422. this.wireframeLinecap = source.wireframeLinecap;
  14423. this.wireframeLinejoin = source.wireframeLinejoin;
  14424. this.fog = source.fog;
  14425. return this;
  14426. }
  14427. }
  14428. class MeshNormalMaterial extends Material {
  14429. constructor( parameters ) {
  14430. super();
  14431. this.isMeshNormalMaterial = true;
  14432. this.type = 'MeshNormalMaterial';
  14433. this.bumpMap = null;
  14434. this.bumpScale = 1;
  14435. this.normalMap = null;
  14436. this.normalMapType = TangentSpaceNormalMap;
  14437. this.normalScale = new Vector2( 1, 1 );
  14438. this.displacementMap = null;
  14439. this.displacementScale = 1;
  14440. this.displacementBias = 0;
  14441. this.wireframe = false;
  14442. this.wireframeLinewidth = 1;
  14443. this.flatShading = false;
  14444. this.setValues( parameters );
  14445. }
  14446. copy( source ) {
  14447. super.copy( source );
  14448. this.bumpMap = source.bumpMap;
  14449. this.bumpScale = source.bumpScale;
  14450. this.normalMap = source.normalMap;
  14451. this.normalMapType = source.normalMapType;
  14452. this.normalScale.copy( source.normalScale );
  14453. this.displacementMap = source.displacementMap;
  14454. this.displacementScale = source.displacementScale;
  14455. this.displacementBias = source.displacementBias;
  14456. this.wireframe = source.wireframe;
  14457. this.wireframeLinewidth = source.wireframeLinewidth;
  14458. this.flatShading = source.flatShading;
  14459. return this;
  14460. }
  14461. }
  14462. class MeshLambertMaterial extends Material {
  14463. constructor( parameters ) {
  14464. super();
  14465. this.isMeshLambertMaterial = true;
  14466. this.type = 'MeshLambertMaterial';
  14467. this.color = new Color( 0xffffff ); // diffuse
  14468. this.map = null;
  14469. this.lightMap = null;
  14470. this.lightMapIntensity = 1.0;
  14471. this.aoMap = null;
  14472. this.aoMapIntensity = 1.0;
  14473. this.emissive = new Color( 0x000000 );
  14474. this.emissiveIntensity = 1.0;
  14475. this.emissiveMap = null;
  14476. this.bumpMap = null;
  14477. this.bumpScale = 1;
  14478. this.normalMap = null;
  14479. this.normalMapType = TangentSpaceNormalMap;
  14480. this.normalScale = new Vector2( 1, 1 );
  14481. this.displacementMap = null;
  14482. this.displacementScale = 1;
  14483. this.displacementBias = 0;
  14484. this.specularMap = null;
  14485. this.alphaMap = null;
  14486. this.envMap = null;
  14487. this.envMapRotation = new Euler();
  14488. this.combine = MultiplyOperation;
  14489. this.reflectivity = 1;
  14490. this.refractionRatio = 0.98;
  14491. this.wireframe = false;
  14492. this.wireframeLinewidth = 1;
  14493. this.wireframeLinecap = 'round';
  14494. this.wireframeLinejoin = 'round';
  14495. this.flatShading = false;
  14496. this.fog = true;
  14497. this.setValues( parameters );
  14498. }
  14499. copy( source ) {
  14500. super.copy( source );
  14501. this.color.copy( source.color );
  14502. this.map = source.map;
  14503. this.lightMap = source.lightMap;
  14504. this.lightMapIntensity = source.lightMapIntensity;
  14505. this.aoMap = source.aoMap;
  14506. this.aoMapIntensity = source.aoMapIntensity;
  14507. this.emissive.copy( source.emissive );
  14508. this.emissiveMap = source.emissiveMap;
  14509. this.emissiveIntensity = source.emissiveIntensity;
  14510. this.bumpMap = source.bumpMap;
  14511. this.bumpScale = source.bumpScale;
  14512. this.normalMap = source.normalMap;
  14513. this.normalMapType = source.normalMapType;
  14514. this.normalScale.copy( source.normalScale );
  14515. this.displacementMap = source.displacementMap;
  14516. this.displacementScale = source.displacementScale;
  14517. this.displacementBias = source.displacementBias;
  14518. this.specularMap = source.specularMap;
  14519. this.alphaMap = source.alphaMap;
  14520. this.envMap = source.envMap;
  14521. this.envMapRotation.copy( source.envMapRotation );
  14522. this.combine = source.combine;
  14523. this.reflectivity = source.reflectivity;
  14524. this.refractionRatio = source.refractionRatio;
  14525. this.wireframe = source.wireframe;
  14526. this.wireframeLinewidth = source.wireframeLinewidth;
  14527. this.wireframeLinecap = source.wireframeLinecap;
  14528. this.wireframeLinejoin = source.wireframeLinejoin;
  14529. this.flatShading = source.flatShading;
  14530. this.fog = source.fog;
  14531. return this;
  14532. }
  14533. }
  14534. class MeshDepthMaterial extends Material {
  14535. constructor( parameters ) {
  14536. super();
  14537. this.isMeshDepthMaterial = true;
  14538. this.type = 'MeshDepthMaterial';
  14539. this.depthPacking = BasicDepthPacking;
  14540. this.map = null;
  14541. this.alphaMap = null;
  14542. this.displacementMap = null;
  14543. this.displacementScale = 1;
  14544. this.displacementBias = 0;
  14545. this.wireframe = false;
  14546. this.wireframeLinewidth = 1;
  14547. this.setValues( parameters );
  14548. }
  14549. copy( source ) {
  14550. super.copy( source );
  14551. this.depthPacking = source.depthPacking;
  14552. this.map = source.map;
  14553. this.alphaMap = source.alphaMap;
  14554. this.displacementMap = source.displacementMap;
  14555. this.displacementScale = source.displacementScale;
  14556. this.displacementBias = source.displacementBias;
  14557. this.wireframe = source.wireframe;
  14558. this.wireframeLinewidth = source.wireframeLinewidth;
  14559. return this;
  14560. }
  14561. }
  14562. class MeshDistanceMaterial extends Material {
  14563. constructor( parameters ) {
  14564. super();
  14565. this.isMeshDistanceMaterial = true;
  14566. this.type = 'MeshDistanceMaterial';
  14567. this.map = null;
  14568. this.alphaMap = null;
  14569. this.displacementMap = null;
  14570. this.displacementScale = 1;
  14571. this.displacementBias = 0;
  14572. this.setValues( parameters );
  14573. }
  14574. copy( source ) {
  14575. super.copy( source );
  14576. this.map = source.map;
  14577. this.alphaMap = source.alphaMap;
  14578. this.displacementMap = source.displacementMap;
  14579. this.displacementScale = source.displacementScale;
  14580. this.displacementBias = source.displacementBias;
  14581. return this;
  14582. }
  14583. }
  14584. class MeshMatcapMaterial extends Material {
  14585. constructor( parameters ) {
  14586. super();
  14587. this.isMeshMatcapMaterial = true;
  14588. this.defines = { 'MATCAP': '' };
  14589. this.type = 'MeshMatcapMaterial';
  14590. this.color = new Color( 0xffffff ); // diffuse
  14591. this.matcap = null;
  14592. this.map = null;
  14593. this.bumpMap = null;
  14594. this.bumpScale = 1;
  14595. this.normalMap = null;
  14596. this.normalMapType = TangentSpaceNormalMap;
  14597. this.normalScale = new Vector2( 1, 1 );
  14598. this.displacementMap = null;
  14599. this.displacementScale = 1;
  14600. this.displacementBias = 0;
  14601. this.alphaMap = null;
  14602. this.flatShading = false;
  14603. this.fog = true;
  14604. this.setValues( parameters );
  14605. }
  14606. copy( source ) {
  14607. super.copy( source );
  14608. this.defines = { 'MATCAP': '' };
  14609. this.color.copy( source.color );
  14610. this.matcap = source.matcap;
  14611. this.map = source.map;
  14612. this.bumpMap = source.bumpMap;
  14613. this.bumpScale = source.bumpScale;
  14614. this.normalMap = source.normalMap;
  14615. this.normalMapType = source.normalMapType;
  14616. this.normalScale.copy( source.normalScale );
  14617. this.displacementMap = source.displacementMap;
  14618. this.displacementScale = source.displacementScale;
  14619. this.displacementBias = source.displacementBias;
  14620. this.alphaMap = source.alphaMap;
  14621. this.flatShading = source.flatShading;
  14622. this.fog = source.fog;
  14623. return this;
  14624. }
  14625. }
  14626. class LineDashedMaterial extends LineBasicMaterial {
  14627. constructor( parameters ) {
  14628. super();
  14629. this.isLineDashedMaterial = true;
  14630. this.type = 'LineDashedMaterial';
  14631. this.scale = 1;
  14632. this.dashSize = 3;
  14633. this.gapSize = 1;
  14634. this.setValues( parameters );
  14635. }
  14636. copy( source ) {
  14637. super.copy( source );
  14638. this.scale = source.scale;
  14639. this.dashSize = source.dashSize;
  14640. this.gapSize = source.gapSize;
  14641. return this;
  14642. }
  14643. }
  14644. // converts an array to a specific type
  14645. function convertArray( array, type, forceClone ) {
  14646. if ( ! array || // let 'undefined' and 'null' pass
  14647. ! forceClone && array.constructor === type ) return array;
  14648. if ( typeof type.BYTES_PER_ELEMENT === 'number' ) {
  14649. return new type( array ); // create typed array
  14650. }
  14651. return Array.prototype.slice.call( array ); // create Array
  14652. }
  14653. function isTypedArray( object ) {
  14654. return ArrayBuffer.isView( object ) &&
  14655. ! ( object instanceof DataView );
  14656. }
  14657. // returns an array by which times and values can be sorted
  14658. function getKeyframeOrder( times ) {
  14659. function compareTime( i, j ) {
  14660. return times[ i ] - times[ j ];
  14661. }
  14662. const n = times.length;
  14663. const result = new Array( n );
  14664. for ( let i = 0; i !== n; ++ i ) result[ i ] = i;
  14665. result.sort( compareTime );
  14666. return result;
  14667. }
  14668. // uses the array previously returned by 'getKeyframeOrder' to sort data
  14669. function sortedArray( values, stride, order ) {
  14670. const nValues = values.length;
  14671. const result = new values.constructor( nValues );
  14672. for ( let i = 0, dstOffset = 0; dstOffset !== nValues; ++ i ) {
  14673. const srcOffset = order[ i ] * stride;
  14674. for ( let j = 0; j !== stride; ++ j ) {
  14675. result[ dstOffset ++ ] = values[ srcOffset + j ];
  14676. }
  14677. }
  14678. return result;
  14679. }
  14680. // function for parsing AOS keyframe formats
  14681. function flattenJSON( jsonKeys, times, values, valuePropertyName ) {
  14682. let i = 1, key = jsonKeys[ 0 ];
  14683. while ( key !== undefined && key[ valuePropertyName ] === undefined ) {
  14684. key = jsonKeys[ i ++ ];
  14685. }
  14686. if ( key === undefined ) return; // no data
  14687. let value = key[ valuePropertyName ];
  14688. if ( value === undefined ) return; // no data
  14689. if ( Array.isArray( value ) ) {
  14690. do {
  14691. value = key[ valuePropertyName ];
  14692. if ( value !== undefined ) {
  14693. times.push( key.time );
  14694. values.push.apply( values, value ); // push all elements
  14695. }
  14696. key = jsonKeys[ i ++ ];
  14697. } while ( key !== undefined );
  14698. } else if ( value.toArray !== undefined ) {
  14699. // ...assume THREE.Math-ish
  14700. do {
  14701. value = key[ valuePropertyName ];
  14702. if ( value !== undefined ) {
  14703. times.push( key.time );
  14704. value.toArray( values, values.length );
  14705. }
  14706. key = jsonKeys[ i ++ ];
  14707. } while ( key !== undefined );
  14708. } else {
  14709. // otherwise push as-is
  14710. do {
  14711. value = key[ valuePropertyName ];
  14712. if ( value !== undefined ) {
  14713. times.push( key.time );
  14714. values.push( value );
  14715. }
  14716. key = jsonKeys[ i ++ ];
  14717. } while ( key !== undefined );
  14718. }
  14719. }
  14720. function subclip( sourceClip, name, startFrame, endFrame, fps = 30 ) {
  14721. const clip = sourceClip.clone();
  14722. clip.name = name;
  14723. const tracks = [];
  14724. for ( let i = 0; i < clip.tracks.length; ++ i ) {
  14725. const track = clip.tracks[ i ];
  14726. const valueSize = track.getValueSize();
  14727. const times = [];
  14728. const values = [];
  14729. for ( let j = 0; j < track.times.length; ++ j ) {
  14730. const frame = track.times[ j ] * fps;
  14731. if ( frame < startFrame || frame >= endFrame ) continue;
  14732. times.push( track.times[ j ] );
  14733. for ( let k = 0; k < valueSize; ++ k ) {
  14734. values.push( track.values[ j * valueSize + k ] );
  14735. }
  14736. }
  14737. if ( times.length === 0 ) continue;
  14738. track.times = convertArray( times, track.times.constructor );
  14739. track.values = convertArray( values, track.values.constructor );
  14740. tracks.push( track );
  14741. }
  14742. clip.tracks = tracks;
  14743. // find minimum .times value across all tracks in the trimmed clip
  14744. let minStartTime = Infinity;
  14745. for ( let i = 0; i < clip.tracks.length; ++ i ) {
  14746. if ( minStartTime > clip.tracks[ i ].times[ 0 ] ) {
  14747. minStartTime = clip.tracks[ i ].times[ 0 ];
  14748. }
  14749. }
  14750. // shift all tracks such that clip begins at t=0
  14751. for ( let i = 0; i < clip.tracks.length; ++ i ) {
  14752. clip.tracks[ i ].shift( - 1 * minStartTime );
  14753. }
  14754. clip.resetDuration();
  14755. return clip;
  14756. }
  14757. function makeClipAdditive( targetClip, referenceFrame = 0, referenceClip = targetClip, fps = 30 ) {
  14758. if ( fps <= 0 ) fps = 30;
  14759. const numTracks = referenceClip.tracks.length;
  14760. const referenceTime = referenceFrame / fps;
  14761. // Make each track's values relative to the values at the reference frame
  14762. for ( let i = 0; i < numTracks; ++ i ) {
  14763. const referenceTrack = referenceClip.tracks[ i ];
  14764. const referenceTrackType = referenceTrack.ValueTypeName;
  14765. // Skip this track if it's non-numeric
  14766. if ( referenceTrackType === 'bool' || referenceTrackType === 'string' ) continue;
  14767. // Find the track in the target clip whose name and type matches the reference track
  14768. const targetTrack = targetClip.tracks.find( function ( track ) {
  14769. return track.name === referenceTrack.name
  14770. && track.ValueTypeName === referenceTrackType;
  14771. } );
  14772. if ( targetTrack === undefined ) continue;
  14773. let referenceOffset = 0;
  14774. const referenceValueSize = referenceTrack.getValueSize();
  14775. if ( referenceTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) {
  14776. referenceOffset = referenceValueSize / 3;
  14777. }
  14778. let targetOffset = 0;
  14779. const targetValueSize = targetTrack.getValueSize();
  14780. if ( targetTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) {
  14781. targetOffset = targetValueSize / 3;
  14782. }
  14783. const lastIndex = referenceTrack.times.length - 1;
  14784. let referenceValue;
  14785. // Find the value to subtract out of the track
  14786. if ( referenceTime <= referenceTrack.times[ 0 ] ) {
  14787. // Reference frame is earlier than the first keyframe, so just use the first keyframe
  14788. const startIndex = referenceOffset;
  14789. const endIndex = referenceValueSize - referenceOffset;
  14790. referenceValue = referenceTrack.values.slice( startIndex, endIndex );
  14791. } else if ( referenceTime >= referenceTrack.times[ lastIndex ] ) {
  14792. // Reference frame is after the last keyframe, so just use the last keyframe
  14793. const startIndex = lastIndex * referenceValueSize + referenceOffset;
  14794. const endIndex = startIndex + referenceValueSize - referenceOffset;
  14795. referenceValue = referenceTrack.values.slice( startIndex, endIndex );
  14796. } else {
  14797. // Interpolate to the reference value
  14798. const interpolant = referenceTrack.createInterpolant();
  14799. const startIndex = referenceOffset;
  14800. const endIndex = referenceValueSize - referenceOffset;
  14801. interpolant.evaluate( referenceTime );
  14802. referenceValue = interpolant.resultBuffer.slice( startIndex, endIndex );
  14803. }
  14804. // Conjugate the quaternion
  14805. if ( referenceTrackType === 'quaternion' ) {
  14806. const referenceQuat = new Quaternion().fromArray( referenceValue ).normalize().conjugate();
  14807. referenceQuat.toArray( referenceValue );
  14808. }
  14809. // Subtract the reference value from all of the track values
  14810. const numTimes = targetTrack.times.length;
  14811. for ( let j = 0; j < numTimes; ++ j ) {
  14812. const valueStart = j * targetValueSize + targetOffset;
  14813. if ( referenceTrackType === 'quaternion' ) {
  14814. // Multiply the conjugate for quaternion track types
  14815. Quaternion.multiplyQuaternionsFlat(
  14816. targetTrack.values,
  14817. valueStart,
  14818. referenceValue,
  14819. 0,
  14820. targetTrack.values,
  14821. valueStart
  14822. );
  14823. } else {
  14824. const valueEnd = targetValueSize - targetOffset * 2;
  14825. // Subtract each value for all other numeric track types
  14826. for ( let k = 0; k < valueEnd; ++ k ) {
  14827. targetTrack.values[ valueStart + k ] -= referenceValue[ k ];
  14828. }
  14829. }
  14830. }
  14831. }
  14832. targetClip.blendMode = AdditiveAnimationBlendMode;
  14833. return targetClip;
  14834. }
  14835. const AnimationUtils = {
  14836. convertArray: convertArray,
  14837. isTypedArray: isTypedArray,
  14838. getKeyframeOrder: getKeyframeOrder,
  14839. sortedArray: sortedArray,
  14840. flattenJSON: flattenJSON,
  14841. subclip: subclip,
  14842. makeClipAdditive: makeClipAdditive
  14843. };
  14844. /**
  14845. * Abstract base class of interpolants over parametric samples.
  14846. *
  14847. * The parameter domain is one dimensional, typically the time or a path
  14848. * along a curve defined by the data.
  14849. *
  14850. * The sample values can have any dimensionality and derived classes may
  14851. * apply special interpretations to the data.
  14852. *
  14853. * This class provides the interval seek in a Template Method, deferring
  14854. * the actual interpolation to derived classes.
  14855. *
  14856. * Time complexity is O(1) for linear access crossing at most two points
  14857. * and O(log N) for random access, where N is the number of positions.
  14858. *
  14859. * References:
  14860. *
  14861. * http://www.oodesign.com/template-method-pattern.html
  14862. *
  14863. */
  14864. class Interpolant {
  14865. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  14866. this.parameterPositions = parameterPositions;
  14867. this._cachedIndex = 0;
  14868. this.resultBuffer = resultBuffer !== undefined ?
  14869. resultBuffer : new sampleValues.constructor( sampleSize );
  14870. this.sampleValues = sampleValues;
  14871. this.valueSize = sampleSize;
  14872. this.settings = null;
  14873. this.DefaultSettings_ = {};
  14874. }
  14875. evaluate( t ) {
  14876. const pp = this.parameterPositions;
  14877. let i1 = this._cachedIndex,
  14878. t1 = pp[ i1 ],
  14879. t0 = pp[ i1 - 1 ];
  14880. validate_interval: {
  14881. seek: {
  14882. let right;
  14883. linear_scan: {
  14884. //- See http://jsperf.com/comparison-to-undefined/3
  14885. //- slower code:
  14886. //-
  14887. //- if ( t >= t1 || t1 === undefined ) {
  14888. forward_scan: if ( ! ( t < t1 ) ) {
  14889. for ( let giveUpAt = i1 + 2; ; ) {
  14890. if ( t1 === undefined ) {
  14891. if ( t < t0 ) break forward_scan;
  14892. // after end
  14893. i1 = pp.length;
  14894. this._cachedIndex = i1;
  14895. return this.copySampleValue_( i1 - 1 );
  14896. }
  14897. if ( i1 === giveUpAt ) break; // this loop
  14898. t0 = t1;
  14899. t1 = pp[ ++ i1 ];
  14900. if ( t < t1 ) {
  14901. // we have arrived at the sought interval
  14902. break seek;
  14903. }
  14904. }
  14905. // prepare binary search on the right side of the index
  14906. right = pp.length;
  14907. break linear_scan;
  14908. }
  14909. //- slower code:
  14910. //- if ( t < t0 || t0 === undefined ) {
  14911. if ( ! ( t >= t0 ) ) {
  14912. // looping?
  14913. const t1global = pp[ 1 ];
  14914. if ( t < t1global ) {
  14915. i1 = 2; // + 1, using the scan for the details
  14916. t0 = t1global;
  14917. }
  14918. // linear reverse scan
  14919. for ( let giveUpAt = i1 - 2; ; ) {
  14920. if ( t0 === undefined ) {
  14921. // before start
  14922. this._cachedIndex = 0;
  14923. return this.copySampleValue_( 0 );
  14924. }
  14925. if ( i1 === giveUpAt ) break; // this loop
  14926. t1 = t0;
  14927. t0 = pp[ -- i1 - 1 ];
  14928. if ( t >= t0 ) {
  14929. // we have arrived at the sought interval
  14930. break seek;
  14931. }
  14932. }
  14933. // prepare binary search on the left side of the index
  14934. right = i1;
  14935. i1 = 0;
  14936. break linear_scan;
  14937. }
  14938. // the interval is valid
  14939. break validate_interval;
  14940. } // linear scan
  14941. // binary search
  14942. while ( i1 < right ) {
  14943. const mid = ( i1 + right ) >>> 1;
  14944. if ( t < pp[ mid ] ) {
  14945. right = mid;
  14946. } else {
  14947. i1 = mid + 1;
  14948. }
  14949. }
  14950. t1 = pp[ i1 ];
  14951. t0 = pp[ i1 - 1 ];
  14952. // check boundary cases, again
  14953. if ( t0 === undefined ) {
  14954. this._cachedIndex = 0;
  14955. return this.copySampleValue_( 0 );
  14956. }
  14957. if ( t1 === undefined ) {
  14958. i1 = pp.length;
  14959. this._cachedIndex = i1;
  14960. return this.copySampleValue_( i1 - 1 );
  14961. }
  14962. } // seek
  14963. this._cachedIndex = i1;
  14964. this.intervalChanged_( i1, t0, t1 );
  14965. } // validate_interval
  14966. return this.interpolate_( i1, t0, t, t1 );
  14967. }
  14968. getSettings_() {
  14969. return this.settings || this.DefaultSettings_;
  14970. }
  14971. copySampleValue_( index ) {
  14972. // copies a sample value to the result buffer
  14973. const result = this.resultBuffer,
  14974. values = this.sampleValues,
  14975. stride = this.valueSize,
  14976. offset = index * stride;
  14977. for ( let i = 0; i !== stride; ++ i ) {
  14978. result[ i ] = values[ offset + i ];
  14979. }
  14980. return result;
  14981. }
  14982. // Template methods for derived classes:
  14983. interpolate_( /* i1, t0, t, t1 */ ) {
  14984. throw new Error( 'call to abstract method' );
  14985. // implementations shall return this.resultBuffer
  14986. }
  14987. intervalChanged_( /* i1, t0, t1 */ ) {
  14988. // empty
  14989. }
  14990. }
  14991. /**
  14992. * Fast and simple cubic spline interpolant.
  14993. *
  14994. * It was derived from a Hermitian construction setting the first derivative
  14995. * at each sample position to the linear slope between neighboring positions
  14996. * over their parameter interval.
  14997. */
  14998. class CubicInterpolant extends Interpolant {
  14999. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  15000. super( parameterPositions, sampleValues, sampleSize, resultBuffer );
  15001. this._weightPrev = - 0;
  15002. this._offsetPrev = - 0;
  15003. this._weightNext = - 0;
  15004. this._offsetNext = - 0;
  15005. this.DefaultSettings_ = {
  15006. endingStart: ZeroCurvatureEnding,
  15007. endingEnd: ZeroCurvatureEnding
  15008. };
  15009. }
  15010. intervalChanged_( i1, t0, t1 ) {
  15011. const pp = this.parameterPositions;
  15012. let iPrev = i1 - 2,
  15013. iNext = i1 + 1,
  15014. tPrev = pp[ iPrev ],
  15015. tNext = pp[ iNext ];
  15016. if ( tPrev === undefined ) {
  15017. switch ( this.getSettings_().endingStart ) {
  15018. case ZeroSlopeEnding:
  15019. // f'(t0) = 0
  15020. iPrev = i1;
  15021. tPrev = 2 * t0 - t1;
  15022. break;
  15023. case WrapAroundEnding:
  15024. // use the other end of the curve
  15025. iPrev = pp.length - 2;
  15026. tPrev = t0 + pp[ iPrev ] - pp[ iPrev + 1 ];
  15027. break;
  15028. default: // ZeroCurvatureEnding
  15029. // f''(t0) = 0 a.k.a. Natural Spline
  15030. iPrev = i1;
  15031. tPrev = t1;
  15032. }
  15033. }
  15034. if ( tNext === undefined ) {
  15035. switch ( this.getSettings_().endingEnd ) {
  15036. case ZeroSlopeEnding:
  15037. // f'(tN) = 0
  15038. iNext = i1;
  15039. tNext = 2 * t1 - t0;
  15040. break;
  15041. case WrapAroundEnding:
  15042. // use the other end of the curve
  15043. iNext = 1;
  15044. tNext = t1 + pp[ 1 ] - pp[ 0 ];
  15045. break;
  15046. default: // ZeroCurvatureEnding
  15047. // f''(tN) = 0, a.k.a. Natural Spline
  15048. iNext = i1 - 1;
  15049. tNext = t0;
  15050. }
  15051. }
  15052. const halfDt = ( t1 - t0 ) * 0.5,
  15053. stride = this.valueSize;
  15054. this._weightPrev = halfDt / ( t0 - tPrev );
  15055. this._weightNext = halfDt / ( tNext - t1 );
  15056. this._offsetPrev = iPrev * stride;
  15057. this._offsetNext = iNext * stride;
  15058. }
  15059. interpolate_( i1, t0, t, t1 ) {
  15060. const result = this.resultBuffer,
  15061. values = this.sampleValues,
  15062. stride = this.valueSize,
  15063. o1 = i1 * stride, o0 = o1 - stride,
  15064. oP = this._offsetPrev, oN = this._offsetNext,
  15065. wP = this._weightPrev, wN = this._weightNext,
  15066. p = ( t - t0 ) / ( t1 - t0 ),
  15067. pp = p * p,
  15068. ppp = pp * p;
  15069. // evaluate polynomials
  15070. const sP = - wP * ppp + 2 * wP * pp - wP * p;
  15071. const s0 = ( 1 + wP ) * ppp + ( - 1.5 - 2 * wP ) * pp + ( - 0.5 + wP ) * p + 1;
  15072. const s1 = ( - 1 - wN ) * ppp + ( 1.5 + wN ) * pp + 0.5 * p;
  15073. const sN = wN * ppp - wN * pp;
  15074. // combine data linearly
  15075. for ( let i = 0; i !== stride; ++ i ) {
  15076. result[ i ] =
  15077. sP * values[ oP + i ] +
  15078. s0 * values[ o0 + i ] +
  15079. s1 * values[ o1 + i ] +
  15080. sN * values[ oN + i ];
  15081. }
  15082. return result;
  15083. }
  15084. }
  15085. class LinearInterpolant extends Interpolant {
  15086. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  15087. super( parameterPositions, sampleValues, sampleSize, resultBuffer );
  15088. }
  15089. interpolate_( i1, t0, t, t1 ) {
  15090. const result = this.resultBuffer,
  15091. values = this.sampleValues,
  15092. stride = this.valueSize,
  15093. offset1 = i1 * stride,
  15094. offset0 = offset1 - stride,
  15095. weight1 = ( t - t0 ) / ( t1 - t0 ),
  15096. weight0 = 1 - weight1;
  15097. for ( let i = 0; i !== stride; ++ i ) {
  15098. result[ i ] =
  15099. values[ offset0 + i ] * weight0 +
  15100. values[ offset1 + i ] * weight1;
  15101. }
  15102. return result;
  15103. }
  15104. }
  15105. /**
  15106. *
  15107. * Interpolant that evaluates to the sample value at the position preceding
  15108. * the parameter.
  15109. */
  15110. class DiscreteInterpolant extends Interpolant {
  15111. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  15112. super( parameterPositions, sampleValues, sampleSize, resultBuffer );
  15113. }
  15114. interpolate_( i1 /*, t0, t, t1 */ ) {
  15115. return this.copySampleValue_( i1 - 1 );
  15116. }
  15117. }
  15118. class KeyframeTrack {
  15119. constructor( name, times, values, interpolation ) {
  15120. if ( name === undefined ) throw new Error( 'THREE.KeyframeTrack: track name is undefined' );
  15121. if ( times === undefined || times.length === 0 ) throw new Error( 'THREE.KeyframeTrack: no keyframes in track named ' + name );
  15122. this.name = name;
  15123. this.times = convertArray( times, this.TimeBufferType );
  15124. this.values = convertArray( values, this.ValueBufferType );
  15125. this.setInterpolation( interpolation || this.DefaultInterpolation );
  15126. }
  15127. // Serialization (in static context, because of constructor invocation
  15128. // and automatic invocation of .toJSON):
  15129. static toJSON( track ) {
  15130. const trackType = track.constructor;
  15131. let json;
  15132. // derived classes can define a static toJSON method
  15133. if ( trackType.toJSON !== this.toJSON ) {
  15134. json = trackType.toJSON( track );
  15135. } else {
  15136. // by default, we assume the data can be serialized as-is
  15137. json = {
  15138. 'name': track.name,
  15139. 'times': convertArray( track.times, Array ),
  15140. 'values': convertArray( track.values, Array )
  15141. };
  15142. const interpolation = track.getInterpolation();
  15143. if ( interpolation !== track.DefaultInterpolation ) {
  15144. json.interpolation = interpolation;
  15145. }
  15146. }
  15147. json.type = track.ValueTypeName; // mandatory
  15148. return json;
  15149. }
  15150. InterpolantFactoryMethodDiscrete( result ) {
  15151. return new DiscreteInterpolant( this.times, this.values, this.getValueSize(), result );
  15152. }
  15153. InterpolantFactoryMethodLinear( result ) {
  15154. return new LinearInterpolant( this.times, this.values, this.getValueSize(), result );
  15155. }
  15156. InterpolantFactoryMethodSmooth( result ) {
  15157. return new CubicInterpolant( this.times, this.values, this.getValueSize(), result );
  15158. }
  15159. setInterpolation( interpolation ) {
  15160. let factoryMethod;
  15161. switch ( interpolation ) {
  15162. case InterpolateDiscrete:
  15163. factoryMethod = this.InterpolantFactoryMethodDiscrete;
  15164. break;
  15165. case InterpolateLinear:
  15166. factoryMethod = this.InterpolantFactoryMethodLinear;
  15167. break;
  15168. case InterpolateSmooth:
  15169. factoryMethod = this.InterpolantFactoryMethodSmooth;
  15170. break;
  15171. }
  15172. if ( factoryMethod === undefined ) {
  15173. const message = 'unsupported interpolation for ' +
  15174. this.ValueTypeName + ' keyframe track named ' + this.name;
  15175. if ( this.createInterpolant === undefined ) {
  15176. // fall back to default, unless the default itself is messed up
  15177. if ( interpolation !== this.DefaultInterpolation ) {
  15178. this.setInterpolation( this.DefaultInterpolation );
  15179. } else {
  15180. throw new Error( message ); // fatal, in this case
  15181. }
  15182. }
  15183. console.warn( 'THREE.KeyframeTrack:', message );
  15184. return this;
  15185. }
  15186. this.createInterpolant = factoryMethod;
  15187. return this;
  15188. }
  15189. getInterpolation() {
  15190. switch ( this.createInterpolant ) {
  15191. case this.InterpolantFactoryMethodDiscrete:
  15192. return InterpolateDiscrete;
  15193. case this.InterpolantFactoryMethodLinear:
  15194. return InterpolateLinear;
  15195. case this.InterpolantFactoryMethodSmooth:
  15196. return InterpolateSmooth;
  15197. }
  15198. }
  15199. getValueSize() {
  15200. return this.values.length / this.times.length;
  15201. }
  15202. // move all keyframes either forwards or backwards in time
  15203. shift( timeOffset ) {
  15204. if ( timeOffset !== 0.0 ) {
  15205. const times = this.times;
  15206. for ( let i = 0, n = times.length; i !== n; ++ i ) {
  15207. times[ i ] += timeOffset;
  15208. }
  15209. }
  15210. return this;
  15211. }
  15212. // scale all keyframe times by a factor (useful for frame <-> seconds conversions)
  15213. scale( timeScale ) {
  15214. if ( timeScale !== 1.0 ) {
  15215. const times = this.times;
  15216. for ( let i = 0, n = times.length; i !== n; ++ i ) {
  15217. times[ i ] *= timeScale;
  15218. }
  15219. }
  15220. return this;
  15221. }
  15222. // removes keyframes before and after animation without changing any values within the range [startTime, endTime].
  15223. // IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values
  15224. trim( startTime, endTime ) {
  15225. const times = this.times,
  15226. nKeys = times.length;
  15227. let from = 0,
  15228. to = nKeys - 1;
  15229. while ( from !== nKeys && times[ from ] < startTime ) {
  15230. ++ from;
  15231. }
  15232. while ( to !== - 1 && times[ to ] > endTime ) {
  15233. -- to;
  15234. }
  15235. ++ to; // inclusive -> exclusive bound
  15236. if ( from !== 0 || to !== nKeys ) {
  15237. // empty tracks are forbidden, so keep at least one keyframe
  15238. if ( from >= to ) {
  15239. to = Math.max( to, 1 );
  15240. from = to - 1;
  15241. }
  15242. const stride = this.getValueSize();
  15243. this.times = times.slice( from, to );
  15244. this.values = this.values.slice( from * stride, to * stride );
  15245. }
  15246. return this;
  15247. }
  15248. // ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable
  15249. validate() {
  15250. let valid = true;
  15251. const valueSize = this.getValueSize();
  15252. if ( valueSize - Math.floor( valueSize ) !== 0 ) {
  15253. console.error( 'THREE.KeyframeTrack: Invalid value size in track.', this );
  15254. valid = false;
  15255. }
  15256. const times = this.times,
  15257. values = this.values,
  15258. nKeys = times.length;
  15259. if ( nKeys === 0 ) {
  15260. console.error( 'THREE.KeyframeTrack: Track is empty.', this );
  15261. valid = false;
  15262. }
  15263. let prevTime = null;
  15264. for ( let i = 0; i !== nKeys; i ++ ) {
  15265. const currTime = times[ i ];
  15266. if ( typeof currTime === 'number' && isNaN( currTime ) ) {
  15267. console.error( 'THREE.KeyframeTrack: Time is not a valid number.', this, i, currTime );
  15268. valid = false;
  15269. break;
  15270. }
  15271. if ( prevTime !== null && prevTime > currTime ) {
  15272. console.error( 'THREE.KeyframeTrack: Out of order keys.', this, i, currTime, prevTime );
  15273. valid = false;
  15274. break;
  15275. }
  15276. prevTime = currTime;
  15277. }
  15278. if ( values !== undefined ) {
  15279. if ( isTypedArray( values ) ) {
  15280. for ( let i = 0, n = values.length; i !== n; ++ i ) {
  15281. const value = values[ i ];
  15282. if ( isNaN( value ) ) {
  15283. console.error( 'THREE.KeyframeTrack: Value is not a valid number.', this, i, value );
  15284. valid = false;
  15285. break;
  15286. }
  15287. }
  15288. }
  15289. }
  15290. return valid;
  15291. }
  15292. // removes equivalent sequential keys as common in morph target sequences
  15293. // (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0)
  15294. optimize() {
  15295. // times or values may be shared with other tracks, so overwriting is unsafe
  15296. const times = this.times.slice(),
  15297. values = this.values.slice(),
  15298. stride = this.getValueSize(),
  15299. smoothInterpolation = this.getInterpolation() === InterpolateSmooth,
  15300. lastIndex = times.length - 1;
  15301. let writeIndex = 1;
  15302. for ( let i = 1; i < lastIndex; ++ i ) {
  15303. let keep = false;
  15304. const time = times[ i ];
  15305. const timeNext = times[ i + 1 ];
  15306. // remove adjacent keyframes scheduled at the same time
  15307. if ( time !== timeNext && ( i !== 1 || time !== times[ 0 ] ) ) {
  15308. if ( ! smoothInterpolation ) {
  15309. // remove unnecessary keyframes same as their neighbors
  15310. const offset = i * stride,
  15311. offsetP = offset - stride,
  15312. offsetN = offset + stride;
  15313. for ( let j = 0; j !== stride; ++ j ) {
  15314. const value = values[ offset + j ];
  15315. if ( value !== values[ offsetP + j ] ||
  15316. value !== values[ offsetN + j ] ) {
  15317. keep = true;
  15318. break;
  15319. }
  15320. }
  15321. } else {
  15322. keep = true;
  15323. }
  15324. }
  15325. // in-place compaction
  15326. if ( keep ) {
  15327. if ( i !== writeIndex ) {
  15328. times[ writeIndex ] = times[ i ];
  15329. const readOffset = i * stride,
  15330. writeOffset = writeIndex * stride;
  15331. for ( let j = 0; j !== stride; ++ j ) {
  15332. values[ writeOffset + j ] = values[ readOffset + j ];
  15333. }
  15334. }
  15335. ++ writeIndex;
  15336. }
  15337. }
  15338. // flush last keyframe (compaction looks ahead)
  15339. if ( lastIndex > 0 ) {
  15340. times[ writeIndex ] = times[ lastIndex ];
  15341. for ( let readOffset = lastIndex * stride, writeOffset = writeIndex * stride, j = 0; j !== stride; ++ j ) {
  15342. values[ writeOffset + j ] = values[ readOffset + j ];
  15343. }
  15344. ++ writeIndex;
  15345. }
  15346. if ( writeIndex !== times.length ) {
  15347. this.times = times.slice( 0, writeIndex );
  15348. this.values = values.slice( 0, writeIndex * stride );
  15349. } else {
  15350. this.times = times;
  15351. this.values = values;
  15352. }
  15353. return this;
  15354. }
  15355. clone() {
  15356. const times = this.times.slice();
  15357. const values = this.values.slice();
  15358. const TypedKeyframeTrack = this.constructor;
  15359. const track = new TypedKeyframeTrack( this.name, times, values );
  15360. // Interpolant argument to constructor is not saved, so copy the factory method directly.
  15361. track.createInterpolant = this.createInterpolant;
  15362. return track;
  15363. }
  15364. }
  15365. KeyframeTrack.prototype.TimeBufferType = Float32Array;
  15366. KeyframeTrack.prototype.ValueBufferType = Float32Array;
  15367. KeyframeTrack.prototype.DefaultInterpolation = InterpolateLinear;
  15368. /**
  15369. * A Track of Boolean keyframe values.
  15370. */
  15371. class BooleanKeyframeTrack extends KeyframeTrack {
  15372. // No interpolation parameter because only InterpolateDiscrete is valid.
  15373. constructor( name, times, values ) {
  15374. super( name, times, values );
  15375. }
  15376. }
  15377. BooleanKeyframeTrack.prototype.ValueTypeName = 'bool';
  15378. BooleanKeyframeTrack.prototype.ValueBufferType = Array;
  15379. BooleanKeyframeTrack.prototype.DefaultInterpolation = InterpolateDiscrete;
  15380. BooleanKeyframeTrack.prototype.InterpolantFactoryMethodLinear = undefined;
  15381. BooleanKeyframeTrack.prototype.InterpolantFactoryMethodSmooth = undefined;
  15382. /**
  15383. * A Track of keyframe values that represent color.
  15384. */
  15385. class ColorKeyframeTrack extends KeyframeTrack {}
  15386. ColorKeyframeTrack.prototype.ValueTypeName = 'color';
  15387. /**
  15388. * A Track of numeric keyframe values.
  15389. */
  15390. class NumberKeyframeTrack extends KeyframeTrack {}
  15391. NumberKeyframeTrack.prototype.ValueTypeName = 'number';
  15392. /**
  15393. * Spherical linear unit quaternion interpolant.
  15394. */
  15395. class QuaternionLinearInterpolant extends Interpolant {
  15396. constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) {
  15397. super( parameterPositions, sampleValues, sampleSize, resultBuffer );
  15398. }
  15399. interpolate_( i1, t0, t, t1 ) {
  15400. const result = this.resultBuffer,
  15401. values = this.sampleValues,
  15402. stride = this.valueSize,
  15403. alpha = ( t - t0 ) / ( t1 - t0 );
  15404. let offset = i1 * stride;
  15405. for ( let end = offset + stride; offset !== end; offset += 4 ) {
  15406. Quaternion.slerpFlat( result, 0, values, offset - stride, values, offset, alpha );
  15407. }
  15408. return result;
  15409. }
  15410. }
  15411. /**
  15412. * A Track of quaternion keyframe values.
  15413. */
  15414. class QuaternionKeyframeTrack extends KeyframeTrack {
  15415. InterpolantFactoryMethodLinear( result ) {
  15416. return new QuaternionLinearInterpolant( this.times, this.values, this.getValueSize(), result );
  15417. }
  15418. }
  15419. QuaternionKeyframeTrack.prototype.ValueTypeName = 'quaternion';
  15420. // ValueBufferType is inherited
  15421. // DefaultInterpolation is inherited;
  15422. QuaternionKeyframeTrack.prototype.InterpolantFactoryMethodSmooth = undefined;
  15423. /**
  15424. * A Track that interpolates Strings
  15425. */
  15426. class StringKeyframeTrack extends KeyframeTrack {
  15427. // No interpolation parameter because only InterpolateDiscrete is valid.
  15428. constructor( name, times, values ) {
  15429. super( name, times, values );
  15430. }
  15431. }
  15432. StringKeyframeTrack.prototype.ValueTypeName = 'string';
  15433. StringKeyframeTrack.prototype.ValueBufferType = Array;
  15434. StringKeyframeTrack.prototype.DefaultInterpolation = InterpolateDiscrete;
  15435. StringKeyframeTrack.prototype.InterpolantFactoryMethodLinear = undefined;
  15436. StringKeyframeTrack.prototype.InterpolantFactoryMethodSmooth = undefined;
  15437. /**
  15438. * A Track of vectored keyframe values.
  15439. */
  15440. class VectorKeyframeTrack extends KeyframeTrack {}
  15441. VectorKeyframeTrack.prototype.ValueTypeName = 'vector';
  15442. class AnimationClip {
  15443. constructor( name = '', duration = - 1, tracks = [], blendMode = NormalAnimationBlendMode ) {
  15444. this.name = name;
  15445. this.tracks = tracks;
  15446. this.duration = duration;
  15447. this.blendMode = blendMode;
  15448. this.uuid = generateUUID();
  15449. // this means it should figure out its duration by scanning the tracks
  15450. if ( this.duration < 0 ) {
  15451. this.resetDuration();
  15452. }
  15453. }
  15454. static parse( json ) {
  15455. const tracks = [],
  15456. jsonTracks = json.tracks,
  15457. frameTime = 1.0 / ( json.fps || 1.0 );
  15458. for ( let i = 0, n = jsonTracks.length; i !== n; ++ i ) {
  15459. tracks.push( parseKeyframeTrack( jsonTracks[ i ] ).scale( frameTime ) );
  15460. }
  15461. const clip = new this( json.name, json.duration, tracks, json.blendMode );
  15462. clip.uuid = json.uuid;
  15463. return clip;
  15464. }
  15465. static toJSON( clip ) {
  15466. const tracks = [],
  15467. clipTracks = clip.tracks;
  15468. const json = {
  15469. 'name': clip.name,
  15470. 'duration': clip.duration,
  15471. 'tracks': tracks,
  15472. 'uuid': clip.uuid,
  15473. 'blendMode': clip.blendMode
  15474. };
  15475. for ( let i = 0, n = clipTracks.length; i !== n; ++ i ) {
  15476. tracks.push( KeyframeTrack.toJSON( clipTracks[ i ] ) );
  15477. }
  15478. return json;
  15479. }
  15480. static CreateFromMorphTargetSequence( name, morphTargetSequence, fps, noLoop ) {
  15481. const numMorphTargets = morphTargetSequence.length;
  15482. const tracks = [];
  15483. for ( let i = 0; i < numMorphTargets; i ++ ) {
  15484. let times = [];
  15485. let values = [];
  15486. times.push(
  15487. ( i + numMorphTargets - 1 ) % numMorphTargets,
  15488. i,
  15489. ( i + 1 ) % numMorphTargets );
  15490. values.push( 0, 1, 0 );
  15491. const order = getKeyframeOrder( times );
  15492. times = sortedArray( times, 1, order );
  15493. values = sortedArray( values, 1, order );
  15494. // if there is a key at the first frame, duplicate it as the
  15495. // last frame as well for perfect loop.
  15496. if ( ! noLoop && times[ 0 ] === 0 ) {
  15497. times.push( numMorphTargets );
  15498. values.push( values[ 0 ] );
  15499. }
  15500. tracks.push(
  15501. new NumberKeyframeTrack(
  15502. '.morphTargetInfluences[' + morphTargetSequence[ i ].name + ']',
  15503. times, values
  15504. ).scale( 1.0 / fps ) );
  15505. }
  15506. return new this( name, - 1, tracks );
  15507. }
  15508. static findByName( objectOrClipArray, name ) {
  15509. let clipArray = objectOrClipArray;
  15510. if ( ! Array.isArray( objectOrClipArray ) ) {
  15511. const o = objectOrClipArray;
  15512. clipArray = o.geometry && o.geometry.animations || o.animations;
  15513. }
  15514. for ( let i = 0; i < clipArray.length; i ++ ) {
  15515. if ( clipArray[ i ].name === name ) {
  15516. return clipArray[ i ];
  15517. }
  15518. }
  15519. return null;
  15520. }
  15521. static CreateClipsFromMorphTargetSequences( morphTargets, fps, noLoop ) {
  15522. const animationToMorphTargets = {};
  15523. // tested with https://regex101.com/ on trick sequences
  15524. // such flamingo_flyA_003, flamingo_run1_003, crdeath0059
  15525. const pattern = /^([\w-]*?)([\d]+)$/;
  15526. // sort morph target names into animation groups based
  15527. // patterns like Walk_001, Walk_002, Run_001, Run_002
  15528. for ( let i = 0, il = morphTargets.length; i < il; i ++ ) {
  15529. const morphTarget = morphTargets[ i ];
  15530. const parts = morphTarget.name.match( pattern );
  15531. if ( parts && parts.length > 1 ) {
  15532. const name = parts[ 1 ];
  15533. let animationMorphTargets = animationToMorphTargets[ name ];
  15534. if ( ! animationMorphTargets ) {
  15535. animationToMorphTargets[ name ] = animationMorphTargets = [];
  15536. }
  15537. animationMorphTargets.push( morphTarget );
  15538. }
  15539. }
  15540. const clips = [];
  15541. for ( const name in animationToMorphTargets ) {
  15542. clips.push( this.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps, noLoop ) );
  15543. }
  15544. return clips;
  15545. }
  15546. // parse the animation.hierarchy format
  15547. static parseAnimation( animation, bones ) {
  15548. if ( ! animation ) {
  15549. console.error( 'THREE.AnimationClip: No animation in JSONLoader data.' );
  15550. return null;
  15551. }
  15552. const addNonemptyTrack = function ( trackType, trackName, animationKeys, propertyName, destTracks ) {
  15553. // only return track if there are actually keys.
  15554. if ( animationKeys.length !== 0 ) {
  15555. const times = [];
  15556. const values = [];
  15557. flattenJSON( animationKeys, times, values, propertyName );
  15558. // empty keys are filtered out, so check again
  15559. if ( times.length !== 0 ) {
  15560. destTracks.push( new trackType( trackName, times, values ) );
  15561. }
  15562. }
  15563. };
  15564. const tracks = [];
  15565. const clipName = animation.name || 'default';
  15566. const fps = animation.fps || 30;
  15567. const blendMode = animation.blendMode;
  15568. // automatic length determination in AnimationClip.
  15569. let duration = animation.length || - 1;
  15570. const hierarchyTracks = animation.hierarchy || [];
  15571. for ( let h = 0; h < hierarchyTracks.length; h ++ ) {
  15572. const animationKeys = hierarchyTracks[ h ].keys;
  15573. // skip empty tracks
  15574. if ( ! animationKeys || animationKeys.length === 0 ) continue;
  15575. // process morph targets
  15576. if ( animationKeys[ 0 ].morphTargets ) {
  15577. // figure out all morph targets used in this track
  15578. const morphTargetNames = {};
  15579. let k;
  15580. for ( k = 0; k < animationKeys.length; k ++ ) {
  15581. if ( animationKeys[ k ].morphTargets ) {
  15582. for ( let m = 0; m < animationKeys[ k ].morphTargets.length; m ++ ) {
  15583. morphTargetNames[ animationKeys[ k ].morphTargets[ m ] ] = - 1;
  15584. }
  15585. }
  15586. }
  15587. // create a track for each morph target with all zero
  15588. // morphTargetInfluences except for the keys in which
  15589. // the morphTarget is named.
  15590. for ( const morphTargetName in morphTargetNames ) {
  15591. const times = [];
  15592. const values = [];
  15593. for ( let m = 0; m !== animationKeys[ k ].morphTargets.length; ++ m ) {
  15594. const animationKey = animationKeys[ k ];
  15595. times.push( animationKey.time );
  15596. values.push( ( animationKey.morphTarget === morphTargetName ) ? 1 : 0 );
  15597. }
  15598. tracks.push( new NumberKeyframeTrack( '.morphTargetInfluence[' + morphTargetName + ']', times, values ) );
  15599. }
  15600. duration = morphTargetNames.length * fps;
  15601. } else {
  15602. // ...assume skeletal animation
  15603. const boneName = '.bones[' + bones[ h ].name + ']';
  15604. addNonemptyTrack(
  15605. VectorKeyframeTrack, boneName + '.position',
  15606. animationKeys, 'pos', tracks );
  15607. addNonemptyTrack(
  15608. QuaternionKeyframeTrack, boneName + '.quaternion',
  15609. animationKeys, 'rot', tracks );
  15610. addNonemptyTrack(
  15611. VectorKeyframeTrack, boneName + '.scale',
  15612. animationKeys, 'scl', tracks );
  15613. }
  15614. }
  15615. if ( tracks.length === 0 ) {
  15616. return null;
  15617. }
  15618. const clip = new this( clipName, duration, tracks, blendMode );
  15619. return clip;
  15620. }
  15621. resetDuration() {
  15622. const tracks = this.tracks;
  15623. let duration = 0;
  15624. for ( let i = 0, n = tracks.length; i !== n; ++ i ) {
  15625. const track = this.tracks[ i ];
  15626. duration = Math.max( duration, track.times[ track.times.length - 1 ] );
  15627. }
  15628. this.duration = duration;
  15629. return this;
  15630. }
  15631. trim() {
  15632. for ( let i = 0; i < this.tracks.length; i ++ ) {
  15633. this.tracks[ i ].trim( 0, this.duration );
  15634. }
  15635. return this;
  15636. }
  15637. validate() {
  15638. let valid = true;
  15639. for ( let i = 0; i < this.tracks.length; i ++ ) {
  15640. valid = valid && this.tracks[ i ].validate();
  15641. }
  15642. return valid;
  15643. }
  15644. optimize() {
  15645. for ( let i = 0; i < this.tracks.length; i ++ ) {
  15646. this.tracks[ i ].optimize();
  15647. }
  15648. return this;
  15649. }
  15650. clone() {
  15651. const tracks = [];
  15652. for ( let i = 0; i < this.tracks.length; i ++ ) {
  15653. tracks.push( this.tracks[ i ].clone() );
  15654. }
  15655. return new this.constructor( this.name, this.duration, tracks, this.blendMode );
  15656. }
  15657. toJSON() {
  15658. return this.constructor.toJSON( this );
  15659. }
  15660. }
  15661. function getTrackTypeForValueTypeName( typeName ) {
  15662. switch ( typeName.toLowerCase() ) {
  15663. case 'scalar':
  15664. case 'double':
  15665. case 'float':
  15666. case 'number':
  15667. case 'integer':
  15668. return NumberKeyframeTrack;
  15669. case 'vector':
  15670. case 'vector2':
  15671. case 'vector3':
  15672. case 'vector4':
  15673. return VectorKeyframeTrack;
  15674. case 'color':
  15675. return ColorKeyframeTrack;
  15676. case 'quaternion':
  15677. return QuaternionKeyframeTrack;
  15678. case 'bool':
  15679. case 'boolean':
  15680. return BooleanKeyframeTrack;
  15681. case 'string':
  15682. return StringKeyframeTrack;
  15683. }
  15684. throw new Error( 'THREE.KeyframeTrack: Unsupported typeName: ' + typeName );
  15685. }
  15686. function parseKeyframeTrack( json ) {
  15687. if ( json.type === undefined ) {
  15688. throw new Error( 'THREE.KeyframeTrack: track type undefined, can not parse' );
  15689. }
  15690. const trackType = getTrackTypeForValueTypeName( json.type );
  15691. if ( json.times === undefined ) {
  15692. const times = [], values = [];
  15693. flattenJSON( json.keys, times, values, 'value' );
  15694. json.times = times;
  15695. json.values = values;
  15696. }
  15697. // derived classes can define a static parse method
  15698. if ( trackType.parse !== undefined ) {
  15699. return trackType.parse( json );
  15700. } else {
  15701. // by default, we assume a constructor compatible with the base
  15702. return new trackType( json.name, json.times, json.values, json.interpolation );
  15703. }
  15704. }
  15705. const Cache = {
  15706. enabled: false,
  15707. files: {},
  15708. add: function ( key, file ) {
  15709. if ( this.enabled === false ) return;
  15710. // console.log( 'THREE.Cache', 'Adding key:', key );
  15711. this.files[ key ] = file;
  15712. },
  15713. get: function ( key ) {
  15714. if ( this.enabled === false ) return;
  15715. // console.log( 'THREE.Cache', 'Checking key:', key );
  15716. return this.files[ key ];
  15717. },
  15718. remove: function ( key ) {
  15719. delete this.files[ key ];
  15720. },
  15721. clear: function () {
  15722. this.files = {};
  15723. }
  15724. };
  15725. class LoadingManager {
  15726. constructor( onLoad, onProgress, onError ) {
  15727. const scope = this;
  15728. let isLoading = false;
  15729. let itemsLoaded = 0;
  15730. let itemsTotal = 0;
  15731. let urlModifier = undefined;
  15732. const handlers = [];
  15733. // Refer to #5689 for the reason why we don't set .onStart
  15734. // in the constructor
  15735. this.onStart = undefined;
  15736. this.onLoad = onLoad;
  15737. this.onProgress = onProgress;
  15738. this.onError = onError;
  15739. this.itemStart = function ( url ) {
  15740. itemsTotal ++;
  15741. if ( isLoading === false ) {
  15742. if ( scope.onStart !== undefined ) {
  15743. scope.onStart( url, itemsLoaded, itemsTotal );
  15744. }
  15745. }
  15746. isLoading = true;
  15747. };
  15748. this.itemEnd = function ( url ) {
  15749. itemsLoaded ++;
  15750. if ( scope.onProgress !== undefined ) {
  15751. scope.onProgress( url, itemsLoaded, itemsTotal );
  15752. }
  15753. if ( itemsLoaded === itemsTotal ) {
  15754. isLoading = false;
  15755. if ( scope.onLoad !== undefined ) {
  15756. scope.onLoad();
  15757. }
  15758. }
  15759. };
  15760. this.itemError = function ( url ) {
  15761. if ( scope.onError !== undefined ) {
  15762. scope.onError( url );
  15763. }
  15764. };
  15765. this.resolveURL = function ( url ) {
  15766. if ( urlModifier ) {
  15767. return urlModifier( url );
  15768. }
  15769. return url;
  15770. };
  15771. this.setURLModifier = function ( transform ) {
  15772. urlModifier = transform;
  15773. return this;
  15774. };
  15775. this.addHandler = function ( regex, loader ) {
  15776. handlers.push( regex, loader );
  15777. return this;
  15778. };
  15779. this.removeHandler = function ( regex ) {
  15780. const index = handlers.indexOf( regex );
  15781. if ( index !== - 1 ) {
  15782. handlers.splice( index, 2 );
  15783. }
  15784. return this;
  15785. };
  15786. this.getHandler = function ( file ) {
  15787. for ( let i = 0, l = handlers.length; i < l; i += 2 ) {
  15788. const regex = handlers[ i ];
  15789. const loader = handlers[ i + 1 ];
  15790. if ( regex.global ) regex.lastIndex = 0; // see #17920
  15791. if ( regex.test( file ) ) {
  15792. return loader;
  15793. }
  15794. }
  15795. return null;
  15796. };
  15797. }
  15798. }
  15799. const DefaultLoadingManager = /*@__PURE__*/ new LoadingManager();
  15800. class Loader {
  15801. constructor( manager ) {
  15802. this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager;
  15803. this.crossOrigin = 'anonymous';
  15804. this.withCredentials = false;
  15805. this.path = '';
  15806. this.resourcePath = '';
  15807. this.requestHeader = {};
  15808. }
  15809. load( /* url, onLoad, onProgress, onError */ ) {}
  15810. loadAsync( url, onProgress ) {
  15811. const scope = this;
  15812. return new Promise( function ( resolve, reject ) {
  15813. scope.load( url, resolve, onProgress, reject );
  15814. } );
  15815. }
  15816. parse( /* data */ ) {}
  15817. setCrossOrigin( crossOrigin ) {
  15818. this.crossOrigin = crossOrigin;
  15819. return this;
  15820. }
  15821. setWithCredentials( value ) {
  15822. this.withCredentials = value;
  15823. return this;
  15824. }
  15825. setPath( path ) {
  15826. this.path = path;
  15827. return this;
  15828. }
  15829. setResourcePath( resourcePath ) {
  15830. this.resourcePath = resourcePath;
  15831. return this;
  15832. }
  15833. setRequestHeader( requestHeader ) {
  15834. this.requestHeader = requestHeader;
  15835. return this;
  15836. }
  15837. }
  15838. Loader.DEFAULT_MATERIAL_NAME = '__DEFAULT';
  15839. const loading = {};
  15840. class HttpError extends Error {
  15841. constructor( message, response ) {
  15842. super( message );
  15843. this.response = response;
  15844. }
  15845. }
  15846. class FileLoader extends Loader {
  15847. constructor( manager ) {
  15848. super( manager );
  15849. }
  15850. load( url, onLoad, onProgress, onError ) {
  15851. if ( url === undefined ) url = '';
  15852. if ( this.path !== undefined ) url = this.path + url;
  15853. url = this.manager.resolveURL( url );
  15854. const cached = Cache.get( url );
  15855. if ( cached !== undefined ) {
  15856. this.manager.itemStart( url );
  15857. setTimeout( () => {
  15858. if ( onLoad ) onLoad( cached );
  15859. this.manager.itemEnd( url );
  15860. }, 0 );
  15861. return cached;
  15862. }
  15863. // Check if request is duplicate
  15864. if ( loading[ url ] !== undefined ) {
  15865. loading[ url ].push( {
  15866. onLoad: onLoad,
  15867. onProgress: onProgress,
  15868. onError: onError
  15869. } );
  15870. return;
  15871. }
  15872. // Initialise array for duplicate requests
  15873. loading[ url ] = [];
  15874. loading[ url ].push( {
  15875. onLoad: onLoad,
  15876. onProgress: onProgress,
  15877. onError: onError,
  15878. } );
  15879. // create request
  15880. const req = new Request( url, {
  15881. headers: new Headers( this.requestHeader ),
  15882. credentials: this.withCredentials ? 'include' : 'same-origin',
  15883. // An abort controller could be added within a future PR
  15884. } );
  15885. // record states ( avoid data race )
  15886. const mimeType = this.mimeType;
  15887. const responseType = this.responseType;
  15888. // start the fetch
  15889. fetch( req )
  15890. .then( response => {
  15891. if ( response.status === 200 || response.status === 0 ) {
  15892. // Some browsers return HTTP Status 0 when using non-http protocol
  15893. // e.g. 'file://' or 'data://'. Handle as success.
  15894. if ( response.status === 0 ) {
  15895. console.warn( 'THREE.FileLoader: HTTP Status 0 received.' );
  15896. }
  15897. // Workaround: Checking if response.body === undefined for Alipay browser #23548
  15898. if ( typeof ReadableStream === 'undefined' || response.body === undefined || response.body.getReader === undefined ) {
  15899. return response;
  15900. }
  15901. const callbacks = loading[ url ];
  15902. const reader = response.body.getReader();
  15903. // Nginx needs X-File-Size check
  15904. // https://serverfault.com/questions/482875/why-does-nginx-remove-content-length-header-for-chunked-content
  15905. const contentLength = response.headers.get( 'X-File-Size' ) || response.headers.get( 'Content-Length' );
  15906. const total = contentLength ? parseInt( contentLength ) : 0;
  15907. const lengthComputable = total !== 0;
  15908. let loaded = 0;
  15909. // periodically read data into the new stream tracking while download progress
  15910. const stream = new ReadableStream( {
  15911. start( controller ) {
  15912. readData();
  15913. function readData() {
  15914. reader.read().then( ( { done, value } ) => {
  15915. if ( done ) {
  15916. controller.close();
  15917. } else {
  15918. loaded += value.byteLength;
  15919. const event = new ProgressEvent( 'progress', { lengthComputable, loaded, total } );
  15920. for ( let i = 0, il = callbacks.length; i < il; i ++ ) {
  15921. const callback = callbacks[ i ];
  15922. if ( callback.onProgress ) callback.onProgress( event );
  15923. }
  15924. controller.enqueue( value );
  15925. readData();
  15926. }
  15927. }, ( e ) => {
  15928. controller.error( e );
  15929. } );
  15930. }
  15931. }
  15932. } );
  15933. return new Response( stream );
  15934. } else {
  15935. throw new HttpError( `fetch for "${response.url}" responded with ${response.status}: ${response.statusText}`, response );
  15936. }
  15937. } )
  15938. .then( response => {
  15939. switch ( responseType ) {
  15940. case 'arraybuffer':
  15941. return response.arrayBuffer();
  15942. case 'blob':
  15943. return response.blob();
  15944. case 'document':
  15945. return response.text()
  15946. .then( text => {
  15947. const parser = new DOMParser();
  15948. return parser.parseFromString( text, mimeType );
  15949. } );
  15950. case 'json':
  15951. return response.json();
  15952. default:
  15953. if ( mimeType === undefined ) {
  15954. return response.text();
  15955. } else {
  15956. // sniff encoding
  15957. const re = /charset="?([^;"\s]*)"?/i;
  15958. const exec = re.exec( mimeType );
  15959. const label = exec && exec[ 1 ] ? exec[ 1 ].toLowerCase() : undefined;
  15960. const decoder = new TextDecoder( label );
  15961. return response.arrayBuffer().then( ab => decoder.decode( ab ) );
  15962. }
  15963. }
  15964. } )
  15965. .then( data => {
  15966. // Add to cache only on HTTP success, so that we do not cache
  15967. // error response bodies as proper responses to requests.
  15968. Cache.add( url, data );
  15969. const callbacks = loading[ url ];
  15970. delete loading[ url ];
  15971. for ( let i = 0, il = callbacks.length; i < il; i ++ ) {
  15972. const callback = callbacks[ i ];
  15973. if ( callback.onLoad ) callback.onLoad( data );
  15974. }
  15975. } )
  15976. .catch( err => {
  15977. // Abort errors and other errors are handled the same
  15978. const callbacks = loading[ url ];
  15979. if ( callbacks === undefined ) {
  15980. // When onLoad was called and url was deleted in `loading`
  15981. this.manager.itemError( url );
  15982. throw err;
  15983. }
  15984. delete loading[ url ];
  15985. for ( let i = 0, il = callbacks.length; i < il; i ++ ) {
  15986. const callback = callbacks[ i ];
  15987. if ( callback.onError ) callback.onError( err );
  15988. }
  15989. this.manager.itemError( url );
  15990. } )
  15991. .finally( () => {
  15992. this.manager.itemEnd( url );
  15993. } );
  15994. this.manager.itemStart( url );
  15995. }
  15996. setResponseType( value ) {
  15997. this.responseType = value;
  15998. return this;
  15999. }
  16000. setMimeType( value ) {
  16001. this.mimeType = value;
  16002. return this;
  16003. }
  16004. }
  16005. class AnimationLoader extends Loader {
  16006. constructor( manager ) {
  16007. super( manager );
  16008. }
  16009. load( url, onLoad, onProgress, onError ) {
  16010. const scope = this;
  16011. const loader = new FileLoader( this.manager );
  16012. loader.setPath( this.path );
  16013. loader.setRequestHeader( this.requestHeader );
  16014. loader.setWithCredentials( this.withCredentials );
  16015. loader.load( url, function ( text ) {
  16016. try {
  16017. onLoad( scope.parse( JSON.parse( text ) ) );
  16018. } catch ( e ) {
  16019. if ( onError ) {
  16020. onError( e );
  16021. } else {
  16022. console.error( e );
  16023. }
  16024. scope.manager.itemError( url );
  16025. }
  16026. }, onProgress, onError );
  16027. }
  16028. parse( json ) {
  16029. const animations = [];
  16030. for ( let i = 0; i < json.length; i ++ ) {
  16031. const clip = AnimationClip.parse( json[ i ] );
  16032. animations.push( clip );
  16033. }
  16034. return animations;
  16035. }
  16036. }
  16037. /**
  16038. * Abstract Base class to block based textures loader (dds, pvr, ...)
  16039. *
  16040. * Sub classes have to implement the parse() method which will be used in load().
  16041. */
  16042. class CompressedTextureLoader extends Loader {
  16043. constructor( manager ) {
  16044. super( manager );
  16045. }
  16046. load( url, onLoad, onProgress, onError ) {
  16047. const scope = this;
  16048. const images = [];
  16049. const texture = new CompressedTexture();
  16050. const loader = new FileLoader( this.manager );
  16051. loader.setPath( this.path );
  16052. loader.setResponseType( 'arraybuffer' );
  16053. loader.setRequestHeader( this.requestHeader );
  16054. loader.setWithCredentials( scope.withCredentials );
  16055. let loaded = 0;
  16056. function loadTexture( i ) {
  16057. loader.load( url[ i ], function ( buffer ) {
  16058. const texDatas = scope.parse( buffer, true );
  16059. images[ i ] = {
  16060. width: texDatas.width,
  16061. height: texDatas.height,
  16062. format: texDatas.format,
  16063. mipmaps: texDatas.mipmaps
  16064. };
  16065. loaded += 1;
  16066. if ( loaded === 6 ) {
  16067. if ( texDatas.mipmapCount === 1 ) texture.minFilter = LinearFilter;
  16068. texture.image = images;
  16069. texture.format = texDatas.format;
  16070. texture.needsUpdate = true;
  16071. if ( onLoad ) onLoad( texture );
  16072. }
  16073. }, onProgress, onError );
  16074. }
  16075. if ( Array.isArray( url ) ) {
  16076. for ( let i = 0, il = url.length; i < il; ++ i ) {
  16077. loadTexture( i );
  16078. }
  16079. } else {
  16080. // compressed cubemap texture stored in a single DDS file
  16081. loader.load( url, function ( buffer ) {
  16082. const texDatas = scope.parse( buffer, true );
  16083. if ( texDatas.isCubemap ) {
  16084. const faces = texDatas.mipmaps.length / texDatas.mipmapCount;
  16085. for ( let f = 0; f < faces; f ++ ) {
  16086. images[ f ] = { mipmaps: [] };
  16087. for ( let i = 0; i < texDatas.mipmapCount; i ++ ) {
  16088. images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
  16089. images[ f ].format = texDatas.format;
  16090. images[ f ].width = texDatas.width;
  16091. images[ f ].height = texDatas.height;
  16092. }
  16093. }
  16094. texture.image = images;
  16095. } else {
  16096. texture.image.width = texDatas.width;
  16097. texture.image.height = texDatas.height;
  16098. texture.mipmaps = texDatas.mipmaps;
  16099. }
  16100. if ( texDatas.mipmapCount === 1 ) {
  16101. texture.minFilter = LinearFilter;
  16102. }
  16103. texture.format = texDatas.format;
  16104. texture.needsUpdate = true;
  16105. if ( onLoad ) onLoad( texture );
  16106. }, onProgress, onError );
  16107. }
  16108. return texture;
  16109. }
  16110. }
  16111. class ImageLoader extends Loader {
  16112. constructor( manager ) {
  16113. super( manager );
  16114. }
  16115. load( url, onLoad, onProgress, onError ) {
  16116. if ( this.path !== undefined ) url = this.path + url;
  16117. url = this.manager.resolveURL( url );
  16118. const scope = this;
  16119. const cached = Cache.get( url );
  16120. if ( cached !== undefined ) {
  16121. scope.manager.itemStart( url );
  16122. setTimeout( function () {
  16123. if ( onLoad ) onLoad( cached );
  16124. scope.manager.itemEnd( url );
  16125. }, 0 );
  16126. return cached;
  16127. }
  16128. const image = createElementNS( 'img' );
  16129. function onImageLoad() {
  16130. removeEventListeners();
  16131. Cache.add( url, this );
  16132. if ( onLoad ) onLoad( this );
  16133. scope.manager.itemEnd( url );
  16134. }
  16135. function onImageError( event ) {
  16136. removeEventListeners();
  16137. if ( onError ) onError( event );
  16138. scope.manager.itemError( url );
  16139. scope.manager.itemEnd( url );
  16140. }
  16141. function removeEventListeners() {
  16142. image.removeEventListener( 'load', onImageLoad, false );
  16143. image.removeEventListener( 'error', onImageError, false );
  16144. }
  16145. image.addEventListener( 'load', onImageLoad, false );
  16146. image.addEventListener( 'error', onImageError, false );
  16147. if ( url.slice( 0, 5 ) !== 'data:' ) {
  16148. if ( this.crossOrigin !== undefined ) image.crossOrigin = this.crossOrigin;
  16149. }
  16150. scope.manager.itemStart( url );
  16151. image.src = url;
  16152. return image;
  16153. }
  16154. }
  16155. class CubeTextureLoader extends Loader {
  16156. constructor( manager ) {
  16157. super( manager );
  16158. }
  16159. load( urls, onLoad, onProgress, onError ) {
  16160. const texture = new CubeTexture();
  16161. texture.colorSpace = SRGBColorSpace;
  16162. const loader = new ImageLoader( this.manager );
  16163. loader.setCrossOrigin( this.crossOrigin );
  16164. loader.setPath( this.path );
  16165. let loaded = 0;
  16166. function loadTexture( i ) {
  16167. loader.load( urls[ i ], function ( image ) {
  16168. texture.images[ i ] = image;
  16169. loaded ++;
  16170. if ( loaded === 6 ) {
  16171. texture.needsUpdate = true;
  16172. if ( onLoad ) onLoad( texture );
  16173. }
  16174. }, undefined, onError );
  16175. }
  16176. for ( let i = 0; i < urls.length; ++ i ) {
  16177. loadTexture( i );
  16178. }
  16179. return texture;
  16180. }
  16181. }
  16182. /**
  16183. * Abstract Base class to load generic binary textures formats (rgbe, hdr, ...)
  16184. *
  16185. * Sub classes have to implement the parse() method which will be used in load().
  16186. */
  16187. class DataTextureLoader extends Loader {
  16188. constructor( manager ) {
  16189. super( manager );
  16190. }
  16191. load( url, onLoad, onProgress, onError ) {
  16192. const scope = this;
  16193. const texture = new DataTexture();
  16194. const loader = new FileLoader( this.manager );
  16195. loader.setResponseType( 'arraybuffer' );
  16196. loader.setRequestHeader( this.requestHeader );
  16197. loader.setPath( this.path );
  16198. loader.setWithCredentials( scope.withCredentials );
  16199. loader.load( url, function ( buffer ) {
  16200. let texData;
  16201. try {
  16202. texData = scope.parse( buffer );
  16203. } catch ( error ) {
  16204. if ( onError !== undefined ) {
  16205. onError( error );
  16206. } else {
  16207. console.error( error );
  16208. return;
  16209. }
  16210. }
  16211. if ( texData.image !== undefined ) {
  16212. texture.image = texData.image;
  16213. } else if ( texData.data !== undefined ) {
  16214. texture.image.width = texData.width;
  16215. texture.image.height = texData.height;
  16216. texture.image.data = texData.data;
  16217. }
  16218. texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping;
  16219. texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping;
  16220. texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter;
  16221. texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearFilter;
  16222. texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1;
  16223. if ( texData.colorSpace !== undefined ) {
  16224. texture.colorSpace = texData.colorSpace;
  16225. }
  16226. if ( texData.flipY !== undefined ) {
  16227. texture.flipY = texData.flipY;
  16228. }
  16229. if ( texData.format !== undefined ) {
  16230. texture.format = texData.format;
  16231. }
  16232. if ( texData.type !== undefined ) {
  16233. texture.type = texData.type;
  16234. }
  16235. if ( texData.mipmaps !== undefined ) {
  16236. texture.mipmaps = texData.mipmaps;
  16237. texture.minFilter = LinearMipmapLinearFilter; // presumably...
  16238. }
  16239. if ( texData.mipmapCount === 1 ) {
  16240. texture.minFilter = LinearFilter;
  16241. }
  16242. if ( texData.generateMipmaps !== undefined ) {
  16243. texture.generateMipmaps = texData.generateMipmaps;
  16244. }
  16245. texture.needsUpdate = true;
  16246. if ( onLoad ) onLoad( texture, texData );
  16247. }, onProgress, onError );
  16248. return texture;
  16249. }
  16250. }
  16251. class TextureLoader extends Loader {
  16252. constructor( manager ) {
  16253. super( manager );
  16254. }
  16255. load( url, onLoad, onProgress, onError ) {
  16256. const texture = new Texture();
  16257. const loader = new ImageLoader( this.manager );
  16258. loader.setCrossOrigin( this.crossOrigin );
  16259. loader.setPath( this.path );
  16260. loader.load( url, function ( image ) {
  16261. texture.image = image;
  16262. texture.needsUpdate = true;
  16263. if ( onLoad !== undefined ) {
  16264. onLoad( texture );
  16265. }
  16266. }, onProgress, onError );
  16267. return texture;
  16268. }
  16269. }
  16270. class Light extends Object3D {
  16271. constructor( color, intensity = 1 ) {
  16272. super();
  16273. this.isLight = true;
  16274. this.type = 'Light';
  16275. this.color = new Color( color );
  16276. this.intensity = intensity;
  16277. }
  16278. dispose() {
  16279. // Empty here in base class; some subclasses override.
  16280. }
  16281. copy( source, recursive ) {
  16282. super.copy( source, recursive );
  16283. this.color.copy( source.color );
  16284. this.intensity = source.intensity;
  16285. return this;
  16286. }
  16287. toJSON( meta ) {
  16288. const data = super.toJSON( meta );
  16289. data.object.color = this.color.getHex();
  16290. data.object.intensity = this.intensity;
  16291. if ( this.groundColor !== undefined ) data.object.groundColor = this.groundColor.getHex();
  16292. if ( this.distance !== undefined ) data.object.distance = this.distance;
  16293. if ( this.angle !== undefined ) data.object.angle = this.angle;
  16294. if ( this.decay !== undefined ) data.object.decay = this.decay;
  16295. if ( this.penumbra !== undefined ) data.object.penumbra = this.penumbra;
  16296. if ( this.shadow !== undefined ) data.object.shadow = this.shadow.toJSON();
  16297. if ( this.target !== undefined ) data.object.target = this.target.uuid;
  16298. return data;
  16299. }
  16300. }
  16301. class HemisphereLight extends Light {
  16302. constructor( skyColor, groundColor, intensity ) {
  16303. super( skyColor, intensity );
  16304. this.isHemisphereLight = true;
  16305. this.type = 'HemisphereLight';
  16306. this.position.copy( Object3D.DEFAULT_UP );
  16307. this.updateMatrix();
  16308. this.groundColor = new Color( groundColor );
  16309. }
  16310. copy( source, recursive ) {
  16311. super.copy( source, recursive );
  16312. this.groundColor.copy( source.groundColor );
  16313. return this;
  16314. }
  16315. }
  16316. const _projScreenMatrix$1 = /*@__PURE__*/ new Matrix4();
  16317. const _lightPositionWorld$1 = /*@__PURE__*/ new Vector3();
  16318. const _lookTarget$1 = /*@__PURE__*/ new Vector3();
  16319. class LightShadow {
  16320. constructor( camera ) {
  16321. this.camera = camera;
  16322. this.intensity = 1;
  16323. this.bias = 0;
  16324. this.normalBias = 0;
  16325. this.radius = 1;
  16326. this.blurSamples = 8;
  16327. this.mapSize = new Vector2( 512, 512 );
  16328. this.map = null;
  16329. this.mapPass = null;
  16330. this.matrix = new Matrix4();
  16331. this.autoUpdate = true;
  16332. this.needsUpdate = false;
  16333. this._frustum = new Frustum();
  16334. this._frameExtents = new Vector2( 1, 1 );
  16335. this._viewportCount = 1;
  16336. this._viewports = [
  16337. new Vector4( 0, 0, 1, 1 )
  16338. ];
  16339. }
  16340. getViewportCount() {
  16341. return this._viewportCount;
  16342. }
  16343. getFrustum() {
  16344. return this._frustum;
  16345. }
  16346. updateMatrices( light ) {
  16347. const shadowCamera = this.camera;
  16348. const shadowMatrix = this.matrix;
  16349. _lightPositionWorld$1.setFromMatrixPosition( light.matrixWorld );
  16350. shadowCamera.position.copy( _lightPositionWorld$1 );
  16351. _lookTarget$1.setFromMatrixPosition( light.target.matrixWorld );
  16352. shadowCamera.lookAt( _lookTarget$1 );
  16353. shadowCamera.updateMatrixWorld();
  16354. _projScreenMatrix$1.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  16355. this._frustum.setFromProjectionMatrix( _projScreenMatrix$1 );
  16356. shadowMatrix.set(
  16357. 0.5, 0.0, 0.0, 0.5,
  16358. 0.0, 0.5, 0.0, 0.5,
  16359. 0.0, 0.0, 0.5, 0.5,
  16360. 0.0, 0.0, 0.0, 1.0
  16361. );
  16362. shadowMatrix.multiply( _projScreenMatrix$1 );
  16363. }
  16364. getViewport( viewportIndex ) {
  16365. return this._viewports[ viewportIndex ];
  16366. }
  16367. getFrameExtents() {
  16368. return this._frameExtents;
  16369. }
  16370. dispose() {
  16371. if ( this.map ) {
  16372. this.map.dispose();
  16373. }
  16374. if ( this.mapPass ) {
  16375. this.mapPass.dispose();
  16376. }
  16377. }
  16378. copy( source ) {
  16379. this.camera = source.camera.clone();
  16380. this.intensity = source.intensity;
  16381. this.bias = source.bias;
  16382. this.radius = source.radius;
  16383. this.mapSize.copy( source.mapSize );
  16384. return this;
  16385. }
  16386. clone() {
  16387. return new this.constructor().copy( this );
  16388. }
  16389. toJSON() {
  16390. const object = {};
  16391. if ( this.intensity !== 1 ) object.intensity = this.intensity;
  16392. if ( this.bias !== 0 ) object.bias = this.bias;
  16393. if ( this.normalBias !== 0 ) object.normalBias = this.normalBias;
  16394. if ( this.radius !== 1 ) object.radius = this.radius;
  16395. if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) object.mapSize = this.mapSize.toArray();
  16396. object.camera = this.camera.toJSON( false ).object;
  16397. delete object.camera.matrix;
  16398. return object;
  16399. }
  16400. }
  16401. class SpotLightShadow extends LightShadow {
  16402. constructor() {
  16403. super( new PerspectiveCamera( 50, 1, 0.5, 500 ) );
  16404. this.isSpotLightShadow = true;
  16405. this.focus = 1;
  16406. }
  16407. updateMatrices( light ) {
  16408. const camera = this.camera;
  16409. const fov = RAD2DEG * 2 * light.angle * this.focus;
  16410. const aspect = this.mapSize.width / this.mapSize.height;
  16411. const far = light.distance || camera.far;
  16412. if ( fov !== camera.fov || aspect !== camera.aspect || far !== camera.far ) {
  16413. camera.fov = fov;
  16414. camera.aspect = aspect;
  16415. camera.far = far;
  16416. camera.updateProjectionMatrix();
  16417. }
  16418. super.updateMatrices( light );
  16419. }
  16420. copy( source ) {
  16421. super.copy( source );
  16422. this.focus = source.focus;
  16423. return this;
  16424. }
  16425. }
  16426. class SpotLight extends Light {
  16427. constructor( color, intensity, distance = 0, angle = Math.PI / 3, penumbra = 0, decay = 2 ) {
  16428. super( color, intensity );
  16429. this.isSpotLight = true;
  16430. this.type = 'SpotLight';
  16431. this.position.copy( Object3D.DEFAULT_UP );
  16432. this.updateMatrix();
  16433. this.target = new Object3D();
  16434. this.distance = distance;
  16435. this.angle = angle;
  16436. this.penumbra = penumbra;
  16437. this.decay = decay;
  16438. this.map = null;
  16439. this.shadow = new SpotLightShadow();
  16440. }
  16441. get power() {
  16442. // compute the light's luminous power (in lumens) from its intensity (in candela)
  16443. // by convention for a spotlight, luminous power (lm) = π * luminous intensity (cd)
  16444. return this.intensity * Math.PI;
  16445. }
  16446. set power( power ) {
  16447. // set the light's intensity (in candela) from the desired luminous power (in lumens)
  16448. this.intensity = power / Math.PI;
  16449. }
  16450. dispose() {
  16451. this.shadow.dispose();
  16452. }
  16453. copy( source, recursive ) {
  16454. super.copy( source, recursive );
  16455. this.distance = source.distance;
  16456. this.angle = source.angle;
  16457. this.penumbra = source.penumbra;
  16458. this.decay = source.decay;
  16459. this.target = source.target.clone();
  16460. this.shadow = source.shadow.clone();
  16461. return this;
  16462. }
  16463. }
  16464. const _projScreenMatrix = /*@__PURE__*/ new Matrix4();
  16465. const _lightPositionWorld = /*@__PURE__*/ new Vector3();
  16466. const _lookTarget = /*@__PURE__*/ new Vector3();
  16467. class PointLightShadow extends LightShadow {
  16468. constructor() {
  16469. super( new PerspectiveCamera( 90, 1, 0.5, 500 ) );
  16470. this.isPointLightShadow = true;
  16471. this._frameExtents = new Vector2( 4, 2 );
  16472. this._viewportCount = 6;
  16473. this._viewports = [
  16474. // These viewports map a cube-map onto a 2D texture with the
  16475. // following orientation:
  16476. //
  16477. // xzXZ
  16478. // y Y
  16479. //
  16480. // X - Positive x direction
  16481. // x - Negative x direction
  16482. // Y - Positive y direction
  16483. // y - Negative y direction
  16484. // Z - Positive z direction
  16485. // z - Negative z direction
  16486. // positive X
  16487. new Vector4( 2, 1, 1, 1 ),
  16488. // negative X
  16489. new Vector4( 0, 1, 1, 1 ),
  16490. // positive Z
  16491. new Vector4( 3, 1, 1, 1 ),
  16492. // negative Z
  16493. new Vector4( 1, 1, 1, 1 ),
  16494. // positive Y
  16495. new Vector4( 3, 0, 1, 1 ),
  16496. // negative Y
  16497. new Vector4( 1, 0, 1, 1 )
  16498. ];
  16499. this._cubeDirections = [
  16500. new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ),
  16501. new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 )
  16502. ];
  16503. this._cubeUps = [
  16504. new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ),
  16505. new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 )
  16506. ];
  16507. }
  16508. updateMatrices( light, viewportIndex = 0 ) {
  16509. const camera = this.camera;
  16510. const shadowMatrix = this.matrix;
  16511. const far = light.distance || camera.far;
  16512. if ( far !== camera.far ) {
  16513. camera.far = far;
  16514. camera.updateProjectionMatrix();
  16515. }
  16516. _lightPositionWorld.setFromMatrixPosition( light.matrixWorld );
  16517. camera.position.copy( _lightPositionWorld );
  16518. _lookTarget.copy( camera.position );
  16519. _lookTarget.add( this._cubeDirections[ viewportIndex ] );
  16520. camera.up.copy( this._cubeUps[ viewportIndex ] );
  16521. camera.lookAt( _lookTarget );
  16522. camera.updateMatrixWorld();
  16523. shadowMatrix.makeTranslation( - _lightPositionWorld.x, - _lightPositionWorld.y, - _lightPositionWorld.z );
  16524. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  16525. this._frustum.setFromProjectionMatrix( _projScreenMatrix );
  16526. }
  16527. }
  16528. class PointLight extends Light {
  16529. constructor( color, intensity, distance = 0, decay = 2 ) {
  16530. super( color, intensity );
  16531. this.isPointLight = true;
  16532. this.type = 'PointLight';
  16533. this.distance = distance;
  16534. this.decay = decay;
  16535. this.shadow = new PointLightShadow();
  16536. }
  16537. get power() {
  16538. // compute the light's luminous power (in lumens) from its intensity (in candela)
  16539. // for an isotropic light source, luminous power (lm) = 4 π luminous intensity (cd)
  16540. return this.intensity * 4 * Math.PI;
  16541. }
  16542. set power( power ) {
  16543. // set the light's intensity (in candela) from the desired luminous power (in lumens)
  16544. this.intensity = power / ( 4 * Math.PI );
  16545. }
  16546. dispose() {
  16547. this.shadow.dispose();
  16548. }
  16549. copy( source, recursive ) {
  16550. super.copy( source, recursive );
  16551. this.distance = source.distance;
  16552. this.decay = source.decay;
  16553. this.shadow = source.shadow.clone();
  16554. return this;
  16555. }
  16556. }
  16557. class OrthographicCamera extends Camera {
  16558. constructor( left = - 1, right = 1, top = 1, bottom = - 1, near = 0.1, far = 2000 ) {
  16559. super();
  16560. this.isOrthographicCamera = true;
  16561. this.type = 'OrthographicCamera';
  16562. this.zoom = 1;
  16563. this.view = null;
  16564. this.left = left;
  16565. this.right = right;
  16566. this.top = top;
  16567. this.bottom = bottom;
  16568. this.near = near;
  16569. this.far = far;
  16570. this.updateProjectionMatrix();
  16571. }
  16572. copy( source, recursive ) {
  16573. super.copy( source, recursive );
  16574. this.left = source.left;
  16575. this.right = source.right;
  16576. this.top = source.top;
  16577. this.bottom = source.bottom;
  16578. this.near = source.near;
  16579. this.far = source.far;
  16580. this.zoom = source.zoom;
  16581. this.view = source.view === null ? null : Object.assign( {}, source.view );
  16582. return this;
  16583. }
  16584. setViewOffset( fullWidth, fullHeight, x, y, width, height ) {
  16585. if ( this.view === null ) {
  16586. this.view = {
  16587. enabled: true,
  16588. fullWidth: 1,
  16589. fullHeight: 1,
  16590. offsetX: 0,
  16591. offsetY: 0,
  16592. width: 1,
  16593. height: 1
  16594. };
  16595. }
  16596. this.view.enabled = true;
  16597. this.view.fullWidth = fullWidth;
  16598. this.view.fullHeight = fullHeight;
  16599. this.view.offsetX = x;
  16600. this.view.offsetY = y;
  16601. this.view.width = width;
  16602. this.view.height = height;
  16603. this.updateProjectionMatrix();
  16604. }
  16605. clearViewOffset() {
  16606. if ( this.view !== null ) {
  16607. this.view.enabled = false;
  16608. }
  16609. this.updateProjectionMatrix();
  16610. }
  16611. updateProjectionMatrix() {
  16612. const dx = ( this.right - this.left ) / ( 2 * this.zoom );
  16613. const dy = ( this.top - this.bottom ) / ( 2 * this.zoom );
  16614. const cx = ( this.right + this.left ) / 2;
  16615. const cy = ( this.top + this.bottom ) / 2;
  16616. let left = cx - dx;
  16617. let right = cx + dx;
  16618. let top = cy + dy;
  16619. let bottom = cy - dy;
  16620. if ( this.view !== null && this.view.enabled ) {
  16621. const scaleW = ( this.right - this.left ) / this.view.fullWidth / this.zoom;
  16622. const scaleH = ( this.top - this.bottom ) / this.view.fullHeight / this.zoom;
  16623. left += scaleW * this.view.offsetX;
  16624. right = left + scaleW * this.view.width;
  16625. top -= scaleH * this.view.offsetY;
  16626. bottom = top - scaleH * this.view.height;
  16627. }
  16628. this.projectionMatrix.makeOrthographic( left, right, top, bottom, this.near, this.far, this.coordinateSystem );
  16629. this.projectionMatrixInverse.copy( this.projectionMatrix ).invert();
  16630. }
  16631. toJSON( meta ) {
  16632. const data = super.toJSON( meta );
  16633. data.object.zoom = this.zoom;
  16634. data.object.left = this.left;
  16635. data.object.right = this.right;
  16636. data.object.top = this.top;
  16637. data.object.bottom = this.bottom;
  16638. data.object.near = this.near;
  16639. data.object.far = this.far;
  16640. if ( this.view !== null ) data.object.view = Object.assign( {}, this.view );
  16641. return data;
  16642. }
  16643. }
  16644. class DirectionalLightShadow extends LightShadow {
  16645. constructor() {
  16646. super( new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) );
  16647. this.isDirectionalLightShadow = true;
  16648. }
  16649. }
  16650. class DirectionalLight extends Light {
  16651. constructor( color, intensity ) {
  16652. super( color, intensity );
  16653. this.isDirectionalLight = true;
  16654. this.type = 'DirectionalLight';
  16655. this.position.copy( Object3D.DEFAULT_UP );
  16656. this.updateMatrix();
  16657. this.target = new Object3D();
  16658. this.shadow = new DirectionalLightShadow();
  16659. }
  16660. dispose() {
  16661. this.shadow.dispose();
  16662. }
  16663. copy( source ) {
  16664. super.copy( source );
  16665. this.target = source.target.clone();
  16666. this.shadow = source.shadow.clone();
  16667. return this;
  16668. }
  16669. }
  16670. class AmbientLight extends Light {
  16671. constructor( color, intensity ) {
  16672. super( color, intensity );
  16673. this.isAmbientLight = true;
  16674. this.type = 'AmbientLight';
  16675. }
  16676. }
  16677. class RectAreaLight extends Light {
  16678. constructor( color, intensity, width = 10, height = 10 ) {
  16679. super( color, intensity );
  16680. this.isRectAreaLight = true;
  16681. this.type = 'RectAreaLight';
  16682. this.width = width;
  16683. this.height = height;
  16684. }
  16685. get power() {
  16686. // compute the light's luminous power (in lumens) from its intensity (in nits)
  16687. return this.intensity * this.width * this.height * Math.PI;
  16688. }
  16689. set power( power ) {
  16690. // set the light's intensity (in nits) from the desired luminous power (in lumens)
  16691. this.intensity = power / ( this.width * this.height * Math.PI );
  16692. }
  16693. copy( source ) {
  16694. super.copy( source );
  16695. this.width = source.width;
  16696. this.height = source.height;
  16697. return this;
  16698. }
  16699. toJSON( meta ) {
  16700. const data = super.toJSON( meta );
  16701. data.object.width = this.width;
  16702. data.object.height = this.height;
  16703. return data;
  16704. }
  16705. }
  16706. /**
  16707. * Primary reference:
  16708. * https://graphics.stanford.edu/papers/envmap/envmap.pdf
  16709. *
  16710. * Secondary reference:
  16711. * https://www.ppsloan.org/publications/StupidSH36.pdf
  16712. */
  16713. // 3-band SH defined by 9 coefficients
  16714. class SphericalHarmonics3 {
  16715. constructor() {
  16716. this.isSphericalHarmonics3 = true;
  16717. this.coefficients = [];
  16718. for ( let i = 0; i < 9; i ++ ) {
  16719. this.coefficients.push( new Vector3() );
  16720. }
  16721. }
  16722. set( coefficients ) {
  16723. for ( let i = 0; i < 9; i ++ ) {
  16724. this.coefficients[ i ].copy( coefficients[ i ] );
  16725. }
  16726. return this;
  16727. }
  16728. zero() {
  16729. for ( let i = 0; i < 9; i ++ ) {
  16730. this.coefficients[ i ].set( 0, 0, 0 );
  16731. }
  16732. return this;
  16733. }
  16734. // get the radiance in the direction of the normal
  16735. // target is a Vector3
  16736. getAt( normal, target ) {
  16737. // normal is assumed to be unit length
  16738. const x = normal.x, y = normal.y, z = normal.z;
  16739. const coeff = this.coefficients;
  16740. // band 0
  16741. target.copy( coeff[ 0 ] ).multiplyScalar( 0.282095 );
  16742. // band 1
  16743. target.addScaledVector( coeff[ 1 ], 0.488603 * y );
  16744. target.addScaledVector( coeff[ 2 ], 0.488603 * z );
  16745. target.addScaledVector( coeff[ 3 ], 0.488603 * x );
  16746. // band 2
  16747. target.addScaledVector( coeff[ 4 ], 1.092548 * ( x * y ) );
  16748. target.addScaledVector( coeff[ 5 ], 1.092548 * ( y * z ) );
  16749. target.addScaledVector( coeff[ 6 ], 0.315392 * ( 3.0 * z * z - 1.0 ) );
  16750. target.addScaledVector( coeff[ 7 ], 1.092548 * ( x * z ) );
  16751. target.addScaledVector( coeff[ 8 ], 0.546274 * ( x * x - y * y ) );
  16752. return target;
  16753. }
  16754. // get the irradiance (radiance convolved with cosine lobe) in the direction of the normal
  16755. // target is a Vector3
  16756. // https://graphics.stanford.edu/papers/envmap/envmap.pdf
  16757. getIrradianceAt( normal, target ) {
  16758. // normal is assumed to be unit length
  16759. const x = normal.x, y = normal.y, z = normal.z;
  16760. const coeff = this.coefficients;
  16761. // band 0
  16762. target.copy( coeff[ 0 ] ).multiplyScalar( 0.886227 ); // π * 0.282095
  16763. // band 1
  16764. target.addScaledVector( coeff[ 1 ], 2.0 * 0.511664 * y ); // ( 2 * π / 3 ) * 0.488603
  16765. target.addScaledVector( coeff[ 2 ], 2.0 * 0.511664 * z );
  16766. target.addScaledVector( coeff[ 3 ], 2.0 * 0.511664 * x );
  16767. // band 2
  16768. target.addScaledVector( coeff[ 4 ], 2.0 * 0.429043 * x * y ); // ( π / 4 ) * 1.092548
  16769. target.addScaledVector( coeff[ 5 ], 2.0 * 0.429043 * y * z );
  16770. target.addScaledVector( coeff[ 6 ], 0.743125 * z * z - 0.247708 ); // ( π / 4 ) * 0.315392 * 3
  16771. target.addScaledVector( coeff[ 7 ], 2.0 * 0.429043 * x * z );
  16772. target.addScaledVector( coeff[ 8 ], 0.429043 * ( x * x - y * y ) ); // ( π / 4 ) * 0.546274
  16773. return target;
  16774. }
  16775. add( sh ) {
  16776. for ( let i = 0; i < 9; i ++ ) {
  16777. this.coefficients[ i ].add( sh.coefficients[ i ] );
  16778. }
  16779. return this;
  16780. }
  16781. addScaledSH( sh, s ) {
  16782. for ( let i = 0; i < 9; i ++ ) {
  16783. this.coefficients[ i ].addScaledVector( sh.coefficients[ i ], s );
  16784. }
  16785. return this;
  16786. }
  16787. scale( s ) {
  16788. for ( let i = 0; i < 9; i ++ ) {
  16789. this.coefficients[ i ].multiplyScalar( s );
  16790. }
  16791. return this;
  16792. }
  16793. lerp( sh, alpha ) {
  16794. for ( let i = 0; i < 9; i ++ ) {
  16795. this.coefficients[ i ].lerp( sh.coefficients[ i ], alpha );
  16796. }
  16797. return this;
  16798. }
  16799. equals( sh ) {
  16800. for ( let i = 0; i < 9; i ++ ) {
  16801. if ( ! this.coefficients[ i ].equals( sh.coefficients[ i ] ) ) {
  16802. return false;
  16803. }
  16804. }
  16805. return true;
  16806. }
  16807. copy( sh ) {
  16808. return this.set( sh.coefficients );
  16809. }
  16810. clone() {
  16811. return new this.constructor().copy( this );
  16812. }
  16813. fromArray( array, offset = 0 ) {
  16814. const coefficients = this.coefficients;
  16815. for ( let i = 0; i < 9; i ++ ) {
  16816. coefficients[ i ].fromArray( array, offset + ( i * 3 ) );
  16817. }
  16818. return this;
  16819. }
  16820. toArray( array = [], offset = 0 ) {
  16821. const coefficients = this.coefficients;
  16822. for ( let i = 0; i < 9; i ++ ) {
  16823. coefficients[ i ].toArray( array, offset + ( i * 3 ) );
  16824. }
  16825. return array;
  16826. }
  16827. // evaluate the basis functions
  16828. // shBasis is an Array[ 9 ]
  16829. static getBasisAt( normal, shBasis ) {
  16830. // normal is assumed to be unit length
  16831. const x = normal.x, y = normal.y, z = normal.z;
  16832. // band 0
  16833. shBasis[ 0 ] = 0.282095;
  16834. // band 1
  16835. shBasis[ 1 ] = 0.488603 * y;
  16836. shBasis[ 2 ] = 0.488603 * z;
  16837. shBasis[ 3 ] = 0.488603 * x;
  16838. // band 2
  16839. shBasis[ 4 ] = 1.092548 * x * y;
  16840. shBasis[ 5 ] = 1.092548 * y * z;
  16841. shBasis[ 6 ] = 0.315392 * ( 3 * z * z - 1 );
  16842. shBasis[ 7 ] = 1.092548 * x * z;
  16843. shBasis[ 8 ] = 0.546274 * ( x * x - y * y );
  16844. }
  16845. }
  16846. class LightProbe extends Light {
  16847. constructor( sh = new SphericalHarmonics3(), intensity = 1 ) {
  16848. super( undefined, intensity );
  16849. this.isLightProbe = true;
  16850. this.sh = sh;
  16851. }
  16852. copy( source ) {
  16853. super.copy( source );
  16854. this.sh.copy( source.sh );
  16855. return this;
  16856. }
  16857. fromJSON( json ) {
  16858. this.intensity = json.intensity; // TODO: Move this bit to Light.fromJSON();
  16859. this.sh.fromArray( json.sh );
  16860. return this;
  16861. }
  16862. toJSON( meta ) {
  16863. const data = super.toJSON( meta );
  16864. data.object.sh = this.sh.toArray();
  16865. return data;
  16866. }
  16867. }
  16868. class MaterialLoader extends Loader {
  16869. constructor( manager ) {
  16870. super( manager );
  16871. this.textures = {};
  16872. }
  16873. load( url, onLoad, onProgress, onError ) {
  16874. const scope = this;
  16875. const loader = new FileLoader( scope.manager );
  16876. loader.setPath( scope.path );
  16877. loader.setRequestHeader( scope.requestHeader );
  16878. loader.setWithCredentials( scope.withCredentials );
  16879. loader.load( url, function ( text ) {
  16880. try {
  16881. onLoad( scope.parse( JSON.parse( text ) ) );
  16882. } catch ( e ) {
  16883. if ( onError ) {
  16884. onError( e );
  16885. } else {
  16886. console.error( e );
  16887. }
  16888. scope.manager.itemError( url );
  16889. }
  16890. }, onProgress, onError );
  16891. }
  16892. parse( json ) {
  16893. const textures = this.textures;
  16894. function getTexture( name ) {
  16895. if ( textures[ name ] === undefined ) {
  16896. console.warn( 'THREE.MaterialLoader: Undefined texture', name );
  16897. }
  16898. return textures[ name ];
  16899. }
  16900. const material = this.createMaterialFromType( json.type );
  16901. if ( json.uuid !== undefined ) material.uuid = json.uuid;
  16902. if ( json.name !== undefined ) material.name = json.name;
  16903. if ( json.color !== undefined && material.color !== undefined ) material.color.setHex( json.color );
  16904. if ( json.roughness !== undefined ) material.roughness = json.roughness;
  16905. if ( json.metalness !== undefined ) material.metalness = json.metalness;
  16906. if ( json.sheen !== undefined ) material.sheen = json.sheen;
  16907. if ( json.sheenColor !== undefined ) material.sheenColor = new Color().setHex( json.sheenColor );
  16908. if ( json.sheenRoughness !== undefined ) material.sheenRoughness = json.sheenRoughness;
  16909. if ( json.emissive !== undefined && material.emissive !== undefined ) material.emissive.setHex( json.emissive );
  16910. if ( json.specular !== undefined && material.specular !== undefined ) material.specular.setHex( json.specular );
  16911. if ( json.specularIntensity !== undefined ) material.specularIntensity = json.specularIntensity;
  16912. if ( json.specularColor !== undefined && material.specularColor !== undefined ) material.specularColor.setHex( json.specularColor );
  16913. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  16914. if ( json.clearcoat !== undefined ) material.clearcoat = json.clearcoat;
  16915. if ( json.clearcoatRoughness !== undefined ) material.clearcoatRoughness = json.clearcoatRoughness;
  16916. if ( json.dispersion !== undefined ) material.dispersion = json.dispersion;
  16917. if ( json.iridescence !== undefined ) material.iridescence = json.iridescence;
  16918. if ( json.iridescenceIOR !== undefined ) material.iridescenceIOR = json.iridescenceIOR;
  16919. if ( json.iridescenceThicknessRange !== undefined ) material.iridescenceThicknessRange = json.iridescenceThicknessRange;
  16920. if ( json.transmission !== undefined ) material.transmission = json.transmission;
  16921. if ( json.thickness !== undefined ) material.thickness = json.thickness;
  16922. if ( json.attenuationDistance !== undefined ) material.attenuationDistance = json.attenuationDistance;
  16923. if ( json.attenuationColor !== undefined && material.attenuationColor !== undefined ) material.attenuationColor.setHex( json.attenuationColor );
  16924. if ( json.anisotropy !== undefined ) material.anisotropy = json.anisotropy;
  16925. if ( json.anisotropyRotation !== undefined ) material.anisotropyRotation = json.anisotropyRotation;
  16926. if ( json.fog !== undefined ) material.fog = json.fog;
  16927. if ( json.flatShading !== undefined ) material.flatShading = json.flatShading;
  16928. if ( json.blending !== undefined ) material.blending = json.blending;
  16929. if ( json.combine !== undefined ) material.combine = json.combine;
  16930. if ( json.side !== undefined ) material.side = json.side;
  16931. if ( json.shadowSide !== undefined ) material.shadowSide = json.shadowSide;
  16932. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  16933. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  16934. if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest;
  16935. if ( json.alphaHash !== undefined ) material.alphaHash = json.alphaHash;
  16936. if ( json.depthFunc !== undefined ) material.depthFunc = json.depthFunc;
  16937. if ( json.depthTest !== undefined ) material.depthTest = json.depthTest;
  16938. if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite;
  16939. if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite;
  16940. if ( json.blendSrc !== undefined ) material.blendSrc = json.blendSrc;
  16941. if ( json.blendDst !== undefined ) material.blendDst = json.blendDst;
  16942. if ( json.blendEquation !== undefined ) material.blendEquation = json.blendEquation;
  16943. if ( json.blendSrcAlpha !== undefined ) material.blendSrcAlpha = json.blendSrcAlpha;
  16944. if ( json.blendDstAlpha !== undefined ) material.blendDstAlpha = json.blendDstAlpha;
  16945. if ( json.blendEquationAlpha !== undefined ) material.blendEquationAlpha = json.blendEquationAlpha;
  16946. if ( json.blendColor !== undefined && material.blendColor !== undefined ) material.blendColor.setHex( json.blendColor );
  16947. if ( json.blendAlpha !== undefined ) material.blendAlpha = json.blendAlpha;
  16948. if ( json.stencilWriteMask !== undefined ) material.stencilWriteMask = json.stencilWriteMask;
  16949. if ( json.stencilFunc !== undefined ) material.stencilFunc = json.stencilFunc;
  16950. if ( json.stencilRef !== undefined ) material.stencilRef = json.stencilRef;
  16951. if ( json.stencilFuncMask !== undefined ) material.stencilFuncMask = json.stencilFuncMask;
  16952. if ( json.stencilFail !== undefined ) material.stencilFail = json.stencilFail;
  16953. if ( json.stencilZFail !== undefined ) material.stencilZFail = json.stencilZFail;
  16954. if ( json.stencilZPass !== undefined ) material.stencilZPass = json.stencilZPass;
  16955. if ( json.stencilWrite !== undefined ) material.stencilWrite = json.stencilWrite;
  16956. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  16957. if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth;
  16958. if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap;
  16959. if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin;
  16960. if ( json.rotation !== undefined ) material.rotation = json.rotation;
  16961. if ( json.linewidth !== undefined ) material.linewidth = json.linewidth;
  16962. if ( json.dashSize !== undefined ) material.dashSize = json.dashSize;
  16963. if ( json.gapSize !== undefined ) material.gapSize = json.gapSize;
  16964. if ( json.scale !== undefined ) material.scale = json.scale;
  16965. if ( json.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset;
  16966. if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor;
  16967. if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits;
  16968. if ( json.dithering !== undefined ) material.dithering = json.dithering;
  16969. if ( json.alphaToCoverage !== undefined ) material.alphaToCoverage = json.alphaToCoverage;
  16970. if ( json.premultipliedAlpha !== undefined ) material.premultipliedAlpha = json.premultipliedAlpha;
  16971. if ( json.forceSinglePass !== undefined ) material.forceSinglePass = json.forceSinglePass;
  16972. if ( json.visible !== undefined ) material.visible = json.visible;
  16973. if ( json.toneMapped !== undefined ) material.toneMapped = json.toneMapped;
  16974. if ( json.userData !== undefined ) material.userData = json.userData;
  16975. if ( json.vertexColors !== undefined ) {
  16976. if ( typeof json.vertexColors === 'number' ) {
  16977. material.vertexColors = ( json.vertexColors > 0 ) ? true : false;
  16978. } else {
  16979. material.vertexColors = json.vertexColors;
  16980. }
  16981. }
  16982. // Shader Material
  16983. if ( json.uniforms !== undefined ) {
  16984. for ( const name in json.uniforms ) {
  16985. const uniform = json.uniforms[ name ];
  16986. material.uniforms[ name ] = {};
  16987. switch ( uniform.type ) {
  16988. case 't':
  16989. material.uniforms[ name ].value = getTexture( uniform.value );
  16990. break;
  16991. case 'c':
  16992. material.uniforms[ name ].value = new Color().setHex( uniform.value );
  16993. break;
  16994. case 'v2':
  16995. material.uniforms[ name ].value = new Vector2().fromArray( uniform.value );
  16996. break;
  16997. case 'v3':
  16998. material.uniforms[ name ].value = new Vector3().fromArray( uniform.value );
  16999. break;
  17000. case 'v4':
  17001. material.uniforms[ name ].value = new Vector4().fromArray( uniform.value );
  17002. break;
  17003. case 'm3':
  17004. material.uniforms[ name ].value = new Matrix3().fromArray( uniform.value );
  17005. break;
  17006. case 'm4':
  17007. material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value );
  17008. break;
  17009. default:
  17010. material.uniforms[ name ].value = uniform.value;
  17011. }
  17012. }
  17013. }
  17014. if ( json.defines !== undefined ) material.defines = json.defines;
  17015. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  17016. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  17017. if ( json.glslVersion !== undefined ) material.glslVersion = json.glslVersion;
  17018. if ( json.extensions !== undefined ) {
  17019. for ( const key in json.extensions ) {
  17020. material.extensions[ key ] = json.extensions[ key ];
  17021. }
  17022. }
  17023. if ( json.lights !== undefined ) material.lights = json.lights;
  17024. if ( json.clipping !== undefined ) material.clipping = json.clipping;
  17025. // for PointsMaterial
  17026. if ( json.size !== undefined ) material.size = json.size;
  17027. if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation;
  17028. // maps
  17029. if ( json.map !== undefined ) material.map = getTexture( json.map );
  17030. if ( json.matcap !== undefined ) material.matcap = getTexture( json.matcap );
  17031. if ( json.alphaMap !== undefined ) material.alphaMap = getTexture( json.alphaMap );
  17032. if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap );
  17033. if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale;
  17034. if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap );
  17035. if ( json.normalMapType !== undefined ) material.normalMapType = json.normalMapType;
  17036. if ( json.normalScale !== undefined ) {
  17037. let normalScale = json.normalScale;
  17038. if ( Array.isArray( normalScale ) === false ) {
  17039. // Blender exporter used to export a scalar. See #7459
  17040. normalScale = [ normalScale, normalScale ];
  17041. }
  17042. material.normalScale = new Vector2().fromArray( normalScale );
  17043. }
  17044. if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap );
  17045. if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale;
  17046. if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias;
  17047. if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap );
  17048. if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap );
  17049. if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap );
  17050. if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity;
  17051. if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap );
  17052. if ( json.specularIntensityMap !== undefined ) material.specularIntensityMap = getTexture( json.specularIntensityMap );
  17053. if ( json.specularColorMap !== undefined ) material.specularColorMap = getTexture( json.specularColorMap );
  17054. if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap );
  17055. if ( json.envMapRotation !== undefined ) material.envMapRotation.fromArray( json.envMapRotation );
  17056. if ( json.envMapIntensity !== undefined ) material.envMapIntensity = json.envMapIntensity;
  17057. if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity;
  17058. if ( json.refractionRatio !== undefined ) material.refractionRatio = json.refractionRatio;
  17059. if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap );
  17060. if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity;
  17061. if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap );
  17062. if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity;
  17063. if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap );
  17064. if ( json.clearcoatMap !== undefined ) material.clearcoatMap = getTexture( json.clearcoatMap );
  17065. if ( json.clearcoatRoughnessMap !== undefined ) material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap );
  17066. if ( json.clearcoatNormalMap !== undefined ) material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap );
  17067. if ( json.clearcoatNormalScale !== undefined ) material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale );
  17068. if ( json.iridescenceMap !== undefined ) material.iridescenceMap = getTexture( json.iridescenceMap );
  17069. if ( json.iridescenceThicknessMap !== undefined ) material.iridescenceThicknessMap = getTexture( json.iridescenceThicknessMap );
  17070. if ( json.transmissionMap !== undefined ) material.transmissionMap = getTexture( json.transmissionMap );
  17071. if ( json.thicknessMap !== undefined ) material.thicknessMap = getTexture( json.thicknessMap );
  17072. if ( json.anisotropyMap !== undefined ) material.anisotropyMap = getTexture( json.anisotropyMap );
  17073. if ( json.sheenColorMap !== undefined ) material.sheenColorMap = getTexture( json.sheenColorMap );
  17074. if ( json.sheenRoughnessMap !== undefined ) material.sheenRoughnessMap = getTexture( json.sheenRoughnessMap );
  17075. return material;
  17076. }
  17077. setTextures( value ) {
  17078. this.textures = value;
  17079. return this;
  17080. }
  17081. createMaterialFromType( type ) {
  17082. return MaterialLoader.createMaterialFromType( type );
  17083. }
  17084. static createMaterialFromType( type ) {
  17085. const materialLib = {
  17086. ShadowMaterial,
  17087. SpriteMaterial,
  17088. RawShaderMaterial,
  17089. ShaderMaterial,
  17090. PointsMaterial,
  17091. MeshPhysicalMaterial,
  17092. MeshStandardMaterial,
  17093. MeshPhongMaterial,
  17094. MeshToonMaterial,
  17095. MeshNormalMaterial,
  17096. MeshLambertMaterial,
  17097. MeshDepthMaterial,
  17098. MeshDistanceMaterial,
  17099. MeshBasicMaterial,
  17100. MeshMatcapMaterial,
  17101. LineDashedMaterial,
  17102. LineBasicMaterial,
  17103. Material
  17104. };
  17105. return new materialLib[ type ]();
  17106. }
  17107. }
  17108. class LoaderUtils {
  17109. static decodeText( array ) { // @deprecated, r165
  17110. console.warn( 'THREE.LoaderUtils: decodeText() has been deprecated with r165 and will be removed with r175. Use TextDecoder instead.' );
  17111. if ( typeof TextDecoder !== 'undefined' ) {
  17112. return new TextDecoder().decode( array );
  17113. }
  17114. // Avoid the String.fromCharCode.apply(null, array) shortcut, which
  17115. // throws a "maximum call stack size exceeded" error for large arrays.
  17116. let s = '';
  17117. for ( let i = 0, il = array.length; i < il; i ++ ) {
  17118. // Implicitly assumes little-endian.
  17119. s += String.fromCharCode( array[ i ] );
  17120. }
  17121. try {
  17122. // merges multi-byte utf-8 characters.
  17123. return decodeURIComponent( escape( s ) );
  17124. } catch ( e ) { // see #16358
  17125. return s;
  17126. }
  17127. }
  17128. static extractUrlBase( url ) {
  17129. const index = url.lastIndexOf( '/' );
  17130. if ( index === - 1 ) return './';
  17131. return url.slice( 0, index + 1 );
  17132. }
  17133. static resolveURL( url, path ) {
  17134. // Invalid URL
  17135. if ( typeof url !== 'string' || url === '' ) return '';
  17136. // Host Relative URL
  17137. if ( /^https?:\/\//i.test( path ) && /^\//.test( url ) ) {
  17138. path = path.replace( /(^https?:\/\/[^\/]+).*/i, '$1' );
  17139. }
  17140. // Absolute URL http://,https://,//
  17141. if ( /^(https?:)?\/\//i.test( url ) ) return url;
  17142. // Data URI
  17143. if ( /^data:.*,.*$/i.test( url ) ) return url;
  17144. // Blob URL
  17145. if ( /^blob:.*$/i.test( url ) ) return url;
  17146. // Relative URL
  17147. return path + url;
  17148. }
  17149. }
  17150. class InstancedBufferGeometry extends BufferGeometry {
  17151. constructor() {
  17152. super();
  17153. this.isInstancedBufferGeometry = true;
  17154. this.type = 'InstancedBufferGeometry';
  17155. this.instanceCount = Infinity;
  17156. }
  17157. copy( source ) {
  17158. super.copy( source );
  17159. this.instanceCount = source.instanceCount;
  17160. return this;
  17161. }
  17162. toJSON() {
  17163. const data = super.toJSON();
  17164. data.instanceCount = this.instanceCount;
  17165. data.isInstancedBufferGeometry = true;
  17166. return data;
  17167. }
  17168. }
  17169. class BufferGeometryLoader extends Loader {
  17170. constructor( manager ) {
  17171. super( manager );
  17172. }
  17173. load( url, onLoad, onProgress, onError ) {
  17174. const scope = this;
  17175. const loader = new FileLoader( scope.manager );
  17176. loader.setPath( scope.path );
  17177. loader.setRequestHeader( scope.requestHeader );
  17178. loader.setWithCredentials( scope.withCredentials );
  17179. loader.load( url, function ( text ) {
  17180. try {
  17181. onLoad( scope.parse( JSON.parse( text ) ) );
  17182. } catch ( e ) {
  17183. if ( onError ) {
  17184. onError( e );
  17185. } else {
  17186. console.error( e );
  17187. }
  17188. scope.manager.itemError( url );
  17189. }
  17190. }, onProgress, onError );
  17191. }
  17192. parse( json ) {
  17193. const interleavedBufferMap = {};
  17194. const arrayBufferMap = {};
  17195. function getInterleavedBuffer( json, uuid ) {
  17196. if ( interleavedBufferMap[ uuid ] !== undefined ) return interleavedBufferMap[ uuid ];
  17197. const interleavedBuffers = json.interleavedBuffers;
  17198. const interleavedBuffer = interleavedBuffers[ uuid ];
  17199. const buffer = getArrayBuffer( json, interleavedBuffer.buffer );
  17200. const array = getTypedArray( interleavedBuffer.type, buffer );
  17201. const ib = new InterleavedBuffer( array, interleavedBuffer.stride );
  17202. ib.uuid = interleavedBuffer.uuid;
  17203. interleavedBufferMap[ uuid ] = ib;
  17204. return ib;
  17205. }
  17206. function getArrayBuffer( json, uuid ) {
  17207. if ( arrayBufferMap[ uuid ] !== undefined ) return arrayBufferMap[ uuid ];
  17208. const arrayBuffers = json.arrayBuffers;
  17209. const arrayBuffer = arrayBuffers[ uuid ];
  17210. const ab = new Uint32Array( arrayBuffer ).buffer;
  17211. arrayBufferMap[ uuid ] = ab;
  17212. return ab;
  17213. }
  17214. const geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry();
  17215. const index = json.data.index;
  17216. if ( index !== undefined ) {
  17217. const typedArray = getTypedArray( index.type, index.array );
  17218. geometry.setIndex( new BufferAttribute( typedArray, 1 ) );
  17219. }
  17220. const attributes = json.data.attributes;
  17221. for ( const key in attributes ) {
  17222. const attribute = attributes[ key ];
  17223. let bufferAttribute;
  17224. if ( attribute.isInterleavedBufferAttribute ) {
  17225. const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
  17226. bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );
  17227. } else {
  17228. const typedArray = getTypedArray( attribute.type, attribute.array );
  17229. const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute;
  17230. bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized );
  17231. }
  17232. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  17233. if ( attribute.usage !== undefined ) bufferAttribute.setUsage( attribute.usage );
  17234. geometry.setAttribute( key, bufferAttribute );
  17235. }
  17236. const morphAttributes = json.data.morphAttributes;
  17237. if ( morphAttributes ) {
  17238. for ( const key in morphAttributes ) {
  17239. const attributeArray = morphAttributes[ key ];
  17240. const array = [];
  17241. for ( let i = 0, il = attributeArray.length; i < il; i ++ ) {
  17242. const attribute = attributeArray[ i ];
  17243. let bufferAttribute;
  17244. if ( attribute.isInterleavedBufferAttribute ) {
  17245. const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data );
  17246. bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized );
  17247. } else {
  17248. const typedArray = getTypedArray( attribute.type, attribute.array );
  17249. bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized );
  17250. }
  17251. if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name;
  17252. array.push( bufferAttribute );
  17253. }
  17254. geometry.morphAttributes[ key ] = array;
  17255. }
  17256. }
  17257. const morphTargetsRelative = json.data.morphTargetsRelative;
  17258. if ( morphTargetsRelative ) {
  17259. geometry.morphTargetsRelative = true;
  17260. }
  17261. const groups = json.data.groups || json.data.drawcalls || json.data.offsets;
  17262. if ( groups !== undefined ) {
  17263. for ( let i = 0, n = groups.length; i !== n; ++ i ) {
  17264. const group = groups[ i ];
  17265. geometry.addGroup( group.start, group.count, group.materialIndex );
  17266. }
  17267. }
  17268. const boundingSphere = json.data.boundingSphere;
  17269. if ( boundingSphere !== undefined ) {
  17270. const center = new Vector3();
  17271. if ( boundingSphere.center !== undefined ) {
  17272. center.fromArray( boundingSphere.center );
  17273. }
  17274. geometry.boundingSphere = new Sphere( center, boundingSphere.radius );
  17275. }
  17276. if ( json.name ) geometry.name = json.name;
  17277. if ( json.userData ) geometry.userData = json.userData;
  17278. return geometry;
  17279. }
  17280. }
  17281. class ObjectLoader extends Loader {
  17282. constructor( manager ) {
  17283. super( manager );
  17284. }
  17285. load( url, onLoad, onProgress, onError ) {
  17286. const scope = this;
  17287. const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  17288. this.resourcePath = this.resourcePath || path;
  17289. const loader = new FileLoader( this.manager );
  17290. loader.setPath( this.path );
  17291. loader.setRequestHeader( this.requestHeader );
  17292. loader.setWithCredentials( this.withCredentials );
  17293. loader.load( url, function ( text ) {
  17294. let json = null;
  17295. try {
  17296. json = JSON.parse( text );
  17297. } catch ( error ) {
  17298. if ( onError !== undefined ) onError( error );
  17299. console.error( 'THREE:ObjectLoader: Can\'t parse ' + url + '.', error.message );
  17300. return;
  17301. }
  17302. const metadata = json.metadata;
  17303. if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
  17304. if ( onError !== undefined ) onError( new Error( 'THREE.ObjectLoader: Can\'t load ' + url ) );
  17305. console.error( 'THREE.ObjectLoader: Can\'t load ' + url );
  17306. return;
  17307. }
  17308. scope.parse( json, onLoad );
  17309. }, onProgress, onError );
  17310. }
  17311. async loadAsync( url, onProgress ) {
  17312. const scope = this;
  17313. const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path;
  17314. this.resourcePath = this.resourcePath || path;
  17315. const loader = new FileLoader( this.manager );
  17316. loader.setPath( this.path );
  17317. loader.setRequestHeader( this.requestHeader );
  17318. loader.setWithCredentials( this.withCredentials );
  17319. const text = await loader.loadAsync( url, onProgress );
  17320. const json = JSON.parse( text );
  17321. const metadata = json.metadata;
  17322. if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) {
  17323. throw new Error( 'THREE.ObjectLoader: Can\'t load ' + url );
  17324. }
  17325. return await scope.parseAsync( json );
  17326. }
  17327. parse( json, onLoad ) {
  17328. const animations = this.parseAnimations( json.animations );
  17329. const shapes = this.parseShapes( json.shapes );
  17330. const geometries = this.parseGeometries( json.geometries, shapes );
  17331. const images = this.parseImages( json.images, function () {
  17332. if ( onLoad !== undefined ) onLoad( object );
  17333. } );
  17334. const textures = this.parseTextures( json.textures, images );
  17335. const materials = this.parseMaterials( json.materials, textures );
  17336. const object = this.parseObject( json.object, geometries, materials, textures, animations );
  17337. const skeletons = this.parseSkeletons( json.skeletons, object );
  17338. this.bindSkeletons( object, skeletons );
  17339. this.bindLightTargets( object );
  17340. //
  17341. if ( onLoad !== undefined ) {
  17342. let hasImages = false;
  17343. for ( const uuid in images ) {
  17344. if ( images[ uuid ].data instanceof HTMLImageElement ) {
  17345. hasImages = true;
  17346. break;
  17347. }
  17348. }
  17349. if ( hasImages === false ) onLoad( object );
  17350. }
  17351. return object;
  17352. }
  17353. async parseAsync( json ) {
  17354. const animations = this.parseAnimations( json.animations );
  17355. const shapes = this.parseShapes( json.shapes );
  17356. const geometries = this.parseGeometries( json.geometries, shapes );
  17357. const images = await this.parseImagesAsync( json.images );
  17358. const textures = this.parseTextures( json.textures, images );
  17359. const materials = this.parseMaterials( json.materials, textures );
  17360. const object = this.parseObject( json.object, geometries, materials, textures, animations );
  17361. const skeletons = this.parseSkeletons( json.skeletons, object );
  17362. this.bindSkeletons( object, skeletons );
  17363. this.bindLightTargets( object );
  17364. return object;
  17365. }
  17366. parseShapes( json ) {
  17367. const shapes = {};
  17368. if ( json !== undefined ) {
  17369. for ( let i = 0, l = json.length; i < l; i ++ ) {
  17370. const shape = new Shape().fromJSON( json[ i ] );
  17371. shapes[ shape.uuid ] = shape;
  17372. }
  17373. }
  17374. return shapes;
  17375. }
  17376. parseSkeletons( json, object ) {
  17377. const skeletons = {};
  17378. const bones = {};
  17379. // generate bone lookup table
  17380. object.traverse( function ( child ) {
  17381. if ( child.isBone ) bones[ child.uuid ] = child;
  17382. } );
  17383. // create skeletons
  17384. if ( json !== undefined ) {
  17385. for ( let i = 0, l = json.length; i < l; i ++ ) {
  17386. const skeleton = new Skeleton().fromJSON( json[ i ], bones );
  17387. skeletons[ skeleton.uuid ] = skeleton;
  17388. }
  17389. }
  17390. return skeletons;
  17391. }
  17392. parseGeometries( json, shapes ) {
  17393. const geometries = {};
  17394. if ( json !== undefined ) {
  17395. const bufferGeometryLoader = new BufferGeometryLoader();
  17396. for ( let i = 0, l = json.length; i < l; i ++ ) {
  17397. let geometry;
  17398. const data = json[ i ];
  17399. switch ( data.type ) {
  17400. case 'BufferGeometry':
  17401. case 'InstancedBufferGeometry':
  17402. geometry = bufferGeometryLoader.parse( data );
  17403. break;
  17404. default:
  17405. if ( data.type in Geometries ) {
  17406. geometry = Geometries[ data.type ].fromJSON( data, shapes );
  17407. } else {
  17408. console.warn( `THREE.ObjectLoader: Unsupported geometry type "${ data.type }"` );
  17409. }
  17410. }
  17411. geometry.uuid = data.uuid;
  17412. if ( data.name !== undefined ) geometry.name = data.name;
  17413. if ( data.userData !== undefined ) geometry.userData = data.userData;
  17414. geometries[ data.uuid ] = geometry;
  17415. }
  17416. }
  17417. return geometries;
  17418. }
  17419. parseMaterials( json, textures ) {
  17420. const cache = {}; // MultiMaterial
  17421. const materials = {};
  17422. if ( json !== undefined ) {
  17423. const loader = new MaterialLoader();
  17424. loader.setTextures( textures );
  17425. for ( let i = 0, l = json.length; i < l; i ++ ) {
  17426. const data = json[ i ];
  17427. if ( cache[ data.uuid ] === undefined ) {
  17428. cache[ data.uuid ] = loader.parse( data );
  17429. }
  17430. materials[ data.uuid ] = cache[ data.uuid ];
  17431. }
  17432. }
  17433. return materials;
  17434. }
  17435. parseAnimations( json ) {
  17436. const animations = {};
  17437. if ( json !== undefined ) {
  17438. for ( let i = 0; i < json.length; i ++ ) {
  17439. const data = json[ i ];
  17440. const clip = AnimationClip.parse( data );
  17441. animations[ clip.uuid ] = clip;
  17442. }
  17443. }
  17444. return animations;
  17445. }
  17446. parseImages( json, onLoad ) {
  17447. const scope = this;
  17448. const images = {};
  17449. let loader;
  17450. function loadImage( url ) {
  17451. scope.manager.itemStart( url );
  17452. return loader.load( url, function () {
  17453. scope.manager.itemEnd( url );
  17454. }, undefined, function () {
  17455. scope.manager.itemError( url );
  17456. scope.manager.itemEnd( url );
  17457. } );
  17458. }
  17459. function deserializeImage( image ) {
  17460. if ( typeof image === 'string' ) {
  17461. const url = image;
  17462. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  17463. return loadImage( path );
  17464. } else {
  17465. if ( image.data ) {
  17466. return {
  17467. data: getTypedArray( image.type, image.data ),
  17468. width: image.width,
  17469. height: image.height
  17470. };
  17471. } else {
  17472. return null;
  17473. }
  17474. }
  17475. }
  17476. if ( json !== undefined && json.length > 0 ) {
  17477. const manager = new LoadingManager( onLoad );
  17478. loader = new ImageLoader( manager );
  17479. loader.setCrossOrigin( this.crossOrigin );
  17480. for ( let i = 0, il = json.length; i < il; i ++ ) {
  17481. const image = json[ i ];
  17482. const url = image.url;
  17483. if ( Array.isArray( url ) ) {
  17484. // load array of images e.g CubeTexture
  17485. const imageArray = [];
  17486. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  17487. const currentUrl = url[ j ];
  17488. const deserializedImage = deserializeImage( currentUrl );
  17489. if ( deserializedImage !== null ) {
  17490. if ( deserializedImage instanceof HTMLImageElement ) {
  17491. imageArray.push( deserializedImage );
  17492. } else {
  17493. // special case: handle array of data textures for cube textures
  17494. imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  17495. }
  17496. }
  17497. }
  17498. images[ image.uuid ] = new Source( imageArray );
  17499. } else {
  17500. // load single image
  17501. const deserializedImage = deserializeImage( image.url );
  17502. images[ image.uuid ] = new Source( deserializedImage );
  17503. }
  17504. }
  17505. }
  17506. return images;
  17507. }
  17508. async parseImagesAsync( json ) {
  17509. const scope = this;
  17510. const images = {};
  17511. let loader;
  17512. async function deserializeImage( image ) {
  17513. if ( typeof image === 'string' ) {
  17514. const url = image;
  17515. const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url;
  17516. return await loader.loadAsync( path );
  17517. } else {
  17518. if ( image.data ) {
  17519. return {
  17520. data: getTypedArray( image.type, image.data ),
  17521. width: image.width,
  17522. height: image.height
  17523. };
  17524. } else {
  17525. return null;
  17526. }
  17527. }
  17528. }
  17529. if ( json !== undefined && json.length > 0 ) {
  17530. loader = new ImageLoader( this.manager );
  17531. loader.setCrossOrigin( this.crossOrigin );
  17532. for ( let i = 0, il = json.length; i < il; i ++ ) {
  17533. const image = json[ i ];
  17534. const url = image.url;
  17535. if ( Array.isArray( url ) ) {
  17536. // load array of images e.g CubeTexture
  17537. const imageArray = [];
  17538. for ( let j = 0, jl = url.length; j < jl; j ++ ) {
  17539. const currentUrl = url[ j ];
  17540. const deserializedImage = await deserializeImage( currentUrl );
  17541. if ( deserializedImage !== null ) {
  17542. if ( deserializedImage instanceof HTMLImageElement ) {
  17543. imageArray.push( deserializedImage );
  17544. } else {
  17545. // special case: handle array of data textures for cube textures
  17546. imageArray.push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) );
  17547. }
  17548. }
  17549. }
  17550. images[ image.uuid ] = new Source( imageArray );
  17551. } else {
  17552. // load single image
  17553. const deserializedImage = await deserializeImage( image.url );
  17554. images[ image.uuid ] = new Source( deserializedImage );
  17555. }
  17556. }
  17557. }
  17558. return images;
  17559. }
  17560. parseTextures( json, images ) {
  17561. function parseConstant( value, type ) {
  17562. if ( typeof value === 'number' ) return value;
  17563. console.warn( 'THREE.ObjectLoader.parseTexture: Constant should be in numeric form.', value );
  17564. return type[ value ];
  17565. }
  17566. const textures = {};
  17567. if ( json !== undefined ) {
  17568. for ( let i = 0, l = json.length; i < l; i ++ ) {
  17569. const data = json[ i ];
  17570. if ( data.image === undefined ) {
  17571. console.warn( 'THREE.ObjectLoader: No "image" specified for', data.uuid );
  17572. }
  17573. if ( images[ data.image ] === undefined ) {
  17574. console.warn( 'THREE.ObjectLoader: Undefined image', data.image );
  17575. }
  17576. const source = images[ data.image ];
  17577. const image = source.data;
  17578. let texture;
  17579. if ( Array.isArray( image ) ) {
  17580. texture = new CubeTexture();
  17581. if ( image.length === 6 ) texture.needsUpdate = true;
  17582. } else {
  17583. if ( image && image.data ) {
  17584. texture = new DataTexture();
  17585. } else {
  17586. texture = new Texture();
  17587. }
  17588. if ( image ) texture.needsUpdate = true; // textures can have undefined image data
  17589. }
  17590. texture.source = source;
  17591. texture.uuid = data.uuid;
  17592. if ( data.name !== undefined ) texture.name = data.name;
  17593. if ( data.mapping !== undefined ) texture.mapping = parseConstant( data.mapping, TEXTURE_MAPPING );
  17594. if ( data.channel !== undefined ) texture.channel = data.channel;
  17595. if ( data.offset !== undefined ) texture.offset.fromArray( data.offset );
  17596. if ( data.repeat !== undefined ) texture.repeat.fromArray( data.repeat );
  17597. if ( data.center !== undefined ) texture.center.fromArray( data.center );
  17598. if ( data.rotation !== undefined ) texture.rotation = data.rotation;
  17599. if ( data.wrap !== undefined ) {
  17600. texture.wrapS = parseConstant( data.wrap[ 0 ], TEXTURE_WRAPPING );
  17601. texture.wrapT = parseConstant( data.wrap[ 1 ], TEXTURE_WRAPPING );
  17602. }
  17603. if ( data.format !== undefined ) texture.format = data.format;
  17604. if ( data.internalFormat !== undefined ) texture.internalFormat = data.internalFormat;
  17605. if ( data.type !== undefined ) texture.type = data.type;
  17606. if ( data.colorSpace !== undefined ) texture.colorSpace = data.colorSpace;
  17607. if ( data.minFilter !== undefined ) texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER );
  17608. if ( data.magFilter !== undefined ) texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER );
  17609. if ( data.anisotropy !== undefined ) texture.anisotropy = data.anisotropy;
  17610. if ( data.flipY !== undefined ) texture.flipY = data.flipY;
  17611. if ( data.generateMipmaps !== undefined ) texture.generateMipmaps = data.generateMipmaps;
  17612. if ( data.premultiplyAlpha !== undefined ) texture.premultiplyAlpha = data.premultiplyAlpha;
  17613. if ( data.unpackAlignment !== undefined ) texture.unpackAlignment = data.unpackAlignment;
  17614. if ( data.compareFunction !== undefined ) texture.compareFunction = data.compareFunction;
  17615. if ( data.userData !== undefined ) texture.userData = data.userData;
  17616. textures[ data.uuid ] = texture;
  17617. }
  17618. }
  17619. return textures;
  17620. }
  17621. parseObject( data, geometries, materials, textures, animations ) {
  17622. let object;
  17623. function getGeometry( name ) {
  17624. if ( geometries[ name ] === undefined ) {
  17625. console.warn( 'THREE.ObjectLoader: Undefined geometry', name );
  17626. }
  17627. return geometries[ name ];
  17628. }
  17629. function getMaterial( name ) {
  17630. if ( name === undefined ) return undefined;
  17631. if ( Array.isArray( name ) ) {
  17632. const array = [];
  17633. for ( let i = 0, l = name.length; i < l; i ++ ) {
  17634. const uuid = name[ i ];
  17635. if ( materials[ uuid ] === undefined ) {
  17636. console.warn( 'THREE.ObjectLoader: Undefined material', uuid );
  17637. }
  17638. array.push( materials[ uuid ] );
  17639. }
  17640. return array;
  17641. }
  17642. if ( materials[ name ] === undefined ) {
  17643. console.warn( 'THREE.ObjectLoader: Undefined material', name );
  17644. }
  17645. return materials[ name ];
  17646. }
  17647. function getTexture( uuid ) {
  17648. if ( textures[ uuid ] === undefined ) {
  17649. console.warn( 'THREE.ObjectLoader: Undefined texture', uuid );
  17650. }
  17651. return textures[ uuid ];
  17652. }
  17653. let geometry, material;
  17654. switch ( data.type ) {
  17655. case 'Scene':
  17656. object = new Scene();
  17657. if ( data.background !== undefined ) {
  17658. if ( Number.isInteger( data.background ) ) {
  17659. object.background = new Color( data.background );
  17660. } else {
  17661. object.background = getTexture( data.background );
  17662. }
  17663. }
  17664. if ( data.environment !== undefined ) {
  17665. object.environment = getTexture( data.environment );
  17666. }
  17667. if ( data.fog !== undefined ) {
  17668. if ( data.fog.type === 'Fog' ) {
  17669. object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far );
  17670. } else if ( data.fog.type === 'FogExp2' ) {
  17671. object.fog = new FogExp2( data.fog.color, data.fog.density );
  17672. }
  17673. if ( data.fog.name !== '' ) {
  17674. object.fog.name = data.fog.name;
  17675. }
  17676. }
  17677. if ( data.backgroundBlurriness !== undefined ) object.backgroundBlurriness = data.backgroundBlurriness;
  17678. if ( data.backgroundIntensity !== undefined ) object.backgroundIntensity = data.backgroundIntensity;
  17679. if ( data.backgroundRotation !== undefined ) object.backgroundRotation.fromArray( data.backgroundRotation );
  17680. if ( data.environmentIntensity !== undefined ) object.environmentIntensity = data.environmentIntensity;
  17681. if ( data.environmentRotation !== undefined ) object.environmentRotation.fromArray( data.environmentRotation );
  17682. break;
  17683. case 'PerspectiveCamera':
  17684. object = new PerspectiveCamera( data.fov, data.aspect, data.near, data.far );
  17685. if ( data.focus !== undefined ) object.focus = data.focus;
  17686. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  17687. if ( data.filmGauge !== undefined ) object.filmGauge = data.filmGauge;
  17688. if ( data.filmOffset !== undefined ) object.filmOffset = data.filmOffset;
  17689. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  17690. break;
  17691. case 'OrthographicCamera':
  17692. object = new OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far );
  17693. if ( data.zoom !== undefined ) object.zoom = data.zoom;
  17694. if ( data.view !== undefined ) object.view = Object.assign( {}, data.view );
  17695. break;
  17696. case 'AmbientLight':
  17697. object = new AmbientLight( data.color, data.intensity );
  17698. break;
  17699. case 'DirectionalLight':
  17700. object = new DirectionalLight( data.color, data.intensity );
  17701. object.target = data.target || '';
  17702. break;
  17703. case 'PointLight':
  17704. object = new PointLight( data.color, data.intensity, data.distance, data.decay );
  17705. break;
  17706. case 'RectAreaLight':
  17707. object = new RectAreaLight( data.color, data.intensity, data.width, data.height );
  17708. break;
  17709. case 'SpotLight':
  17710. object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay );
  17711. object.target = data.target || '';
  17712. break;
  17713. case 'HemisphereLight':
  17714. object = new HemisphereLight( data.color, data.groundColor, data.intensity );
  17715. break;
  17716. case 'LightProbe':
  17717. object = new LightProbe().fromJSON( data );
  17718. break;
  17719. case 'SkinnedMesh':
  17720. geometry = getGeometry( data.geometry );
  17721. material = getMaterial( data.material );
  17722. object = new SkinnedMesh( geometry, material );
  17723. if ( data.bindMode !== undefined ) object.bindMode = data.bindMode;
  17724. if ( data.bindMatrix !== undefined ) object.bindMatrix.fromArray( data.bindMatrix );
  17725. if ( data.skeleton !== undefined ) object.skeleton = data.skeleton;
  17726. break;
  17727. case 'Mesh':
  17728. geometry = getGeometry( data.geometry );
  17729. material = getMaterial( data.material );
  17730. object = new Mesh( geometry, material );
  17731. break;
  17732. case 'InstancedMesh':
  17733. geometry = getGeometry( data.geometry );
  17734. material = getMaterial( data.material );
  17735. const count = data.count;
  17736. const instanceMatrix = data.instanceMatrix;
  17737. const instanceColor = data.instanceColor;
  17738. object = new InstancedMesh( geometry, material, count );
  17739. object.instanceMatrix = new InstancedBufferAttribute( new Float32Array( instanceMatrix.array ), 16 );
  17740. if ( instanceColor !== undefined ) object.instanceColor = new InstancedBufferAttribute( new Float32Array( instanceColor.array ), instanceColor.itemSize );
  17741. break;
  17742. case 'BatchedMesh':
  17743. geometry = getGeometry( data.geometry );
  17744. material = getMaterial( data.material );
  17745. object = new BatchedMesh( data.maxInstanceCount, data.maxVertexCount, data.maxIndexCount, material );
  17746. object.geometry = geometry;
  17747. object.perObjectFrustumCulled = data.perObjectFrustumCulled;
  17748. object.sortObjects = data.sortObjects;
  17749. object._drawRanges = data.drawRanges;
  17750. object._reservedRanges = data.reservedRanges;
  17751. object._visibility = data.visibility;
  17752. object._active = data.active;
  17753. object._bounds = data.bounds.map( bound => {
  17754. const box = new Box3();
  17755. box.min.fromArray( bound.boxMin );
  17756. box.max.fromArray( bound.boxMax );
  17757. const sphere = new Sphere();
  17758. sphere.radius = bound.sphereRadius;
  17759. sphere.center.fromArray( bound.sphereCenter );
  17760. return {
  17761. boxInitialized: bound.boxInitialized,
  17762. box: box,
  17763. sphereInitialized: bound.sphereInitialized,
  17764. sphere: sphere
  17765. };
  17766. } );
  17767. object._maxInstanceCount = data.maxInstanceCount;
  17768. object._maxVertexCount = data.maxVertexCount;
  17769. object._maxIndexCount = data.maxIndexCount;
  17770. object._geometryInitialized = data.geometryInitialized;
  17771. object._geometryCount = data.geometryCount;
  17772. object._matricesTexture = getTexture( data.matricesTexture.uuid );
  17773. if ( data.colorsTexture !== undefined ) object._colorsTexture = getTexture( data.colorsTexture.uuid );
  17774. break;
  17775. case 'LOD':
  17776. object = new LOD();
  17777. break;
  17778. case 'Line':
  17779. object = new Line( getGeometry( data.geometry ), getMaterial( data.material ) );
  17780. break;
  17781. case 'LineLoop':
  17782. object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) );
  17783. break;
  17784. case 'LineSegments':
  17785. object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) );
  17786. break;
  17787. case 'PointCloud':
  17788. case 'Points':
  17789. object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) );
  17790. break;
  17791. case 'Sprite':
  17792. object = new Sprite( getMaterial( data.material ) );
  17793. break;
  17794. case 'Group':
  17795. object = new Group();
  17796. break;
  17797. case 'Bone':
  17798. object = new Bone();
  17799. break;
  17800. default:
  17801. object = new Object3D();
  17802. }
  17803. object.uuid = data.uuid;
  17804. if ( data.name !== undefined ) object.name = data.name;
  17805. if ( data.matrix !== undefined ) {
  17806. object.matrix.fromArray( data.matrix );
  17807. if ( data.matrixAutoUpdate !== undefined ) object.matrixAutoUpdate = data.matrixAutoUpdate;
  17808. if ( object.matrixAutoUpdate ) object.matrix.decompose( object.position, object.quaternion, object.scale );
  17809. } else {
  17810. if ( data.position !== undefined ) object.position.fromArray( data.position );
  17811. if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation );
  17812. if ( data.quaternion !== undefined ) object.quaternion.fromArray( data.quaternion );
  17813. if ( data.scale !== undefined ) object.scale.fromArray( data.scale );
  17814. }
  17815. if ( data.up !== undefined ) object.up.fromArray( data.up );
  17816. if ( data.castShadow !== undefined ) object.castShadow = data.castShadow;
  17817. if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow;
  17818. if ( data.shadow ) {
  17819. if ( data.shadow.intensity !== undefined ) object.shadow.intensity = data.shadow.intensity;
  17820. if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias;
  17821. if ( data.shadow.normalBias !== undefined ) object.shadow.normalBias = data.shadow.normalBias;
  17822. if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius;
  17823. if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize );
  17824. if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera );
  17825. }
  17826. if ( data.visible !== undefined ) object.visible = data.visible;
  17827. if ( data.frustumCulled !== undefined ) object.frustumCulled = data.frustumCulled;
  17828. if ( data.renderOrder !== undefined ) object.renderOrder = data.renderOrder;
  17829. if ( data.userData !== undefined ) object.userData = data.userData;
  17830. if ( data.layers !== undefined ) object.layers.mask = data.layers;
  17831. if ( data.children !== undefined ) {
  17832. const children = data.children;
  17833. for ( let i = 0; i < children.length; i ++ ) {
  17834. object.add( this.parseObject( children[ i ], geometries, materials, textures, animations ) );
  17835. }
  17836. }
  17837. if ( data.animations !== undefined ) {
  17838. const objectAnimations = data.animations;
  17839. for ( let i = 0; i < objectAnimations.length; i ++ ) {
  17840. const uuid = objectAnimations[ i ];
  17841. object.animations.push( animations[ uuid ] );
  17842. }
  17843. }
  17844. if ( data.type === 'LOD' ) {
  17845. if ( data.autoUpdate !== undefined ) object.autoUpdate = data.autoUpdate;
  17846. const levels = data.levels;
  17847. for ( let l = 0; l < levels.length; l ++ ) {
  17848. const level = levels[ l ];
  17849. const child = object.getObjectByProperty( 'uuid', level.object );
  17850. if ( child !== undefined ) {
  17851. object.addLevel( child, level.distance, level.hysteresis );
  17852. }
  17853. }
  17854. }
  17855. return object;
  17856. }
  17857. bindSkeletons( object, skeletons ) {
  17858. if ( Object.keys( skeletons ).length === 0 ) return;
  17859. object.traverse( function ( child ) {
  17860. if ( child.isSkinnedMesh === true && child.skeleton !== undefined ) {
  17861. const skeleton = skeletons[ child.skeleton ];
  17862. if ( skeleton === undefined ) {
  17863. console.warn( 'THREE.ObjectLoader: No skeleton found with UUID:', child.skeleton );
  17864. } else {
  17865. child.bind( skeleton, child.bindMatrix );
  17866. }
  17867. }
  17868. } );
  17869. }
  17870. bindLightTargets( object ) {
  17871. object.traverse( function ( child ) {
  17872. if ( child.isDirectionalLight || child.isSpotLight ) {
  17873. const uuid = child.target;
  17874. const target = object.getObjectByProperty( 'uuid', uuid );
  17875. if ( target !== undefined ) {
  17876. child.target = target;
  17877. } else {
  17878. child.target = new Object3D();
  17879. }
  17880. }
  17881. } );
  17882. }
  17883. }
  17884. const TEXTURE_MAPPING = {
  17885. UVMapping: UVMapping,
  17886. CubeReflectionMapping: CubeReflectionMapping,
  17887. CubeRefractionMapping: CubeRefractionMapping,
  17888. EquirectangularReflectionMapping: EquirectangularReflectionMapping,
  17889. EquirectangularRefractionMapping: EquirectangularRefractionMapping,
  17890. CubeUVReflectionMapping: CubeUVReflectionMapping
  17891. };
  17892. const TEXTURE_WRAPPING = {
  17893. RepeatWrapping: RepeatWrapping,
  17894. ClampToEdgeWrapping: ClampToEdgeWrapping,
  17895. MirroredRepeatWrapping: MirroredRepeatWrapping
  17896. };
  17897. const TEXTURE_FILTER = {
  17898. NearestFilter: NearestFilter,
  17899. NearestMipmapNearestFilter: NearestMipmapNearestFilter,
  17900. NearestMipmapLinearFilter: NearestMipmapLinearFilter,
  17901. LinearFilter: LinearFilter,
  17902. LinearMipmapNearestFilter: LinearMipmapNearestFilter,
  17903. LinearMipmapLinearFilter: LinearMipmapLinearFilter
  17904. };
  17905. class ImageBitmapLoader extends Loader {
  17906. constructor( manager ) {
  17907. super( manager );
  17908. this.isImageBitmapLoader = true;
  17909. if ( typeof createImageBitmap === 'undefined' ) {
  17910. console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' );
  17911. }
  17912. if ( typeof fetch === 'undefined' ) {
  17913. console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' );
  17914. }
  17915. this.options = { premultiplyAlpha: 'none' };
  17916. }
  17917. setOptions( options ) {
  17918. this.options = options;
  17919. return this;
  17920. }
  17921. load( url, onLoad, onProgress, onError ) {
  17922. if ( url === undefined ) url = '';
  17923. if ( this.path !== undefined ) url = this.path + url;
  17924. url = this.manager.resolveURL( url );
  17925. const scope = this;
  17926. const cached = Cache.get( url );
  17927. if ( cached !== undefined ) {
  17928. scope.manager.itemStart( url );
  17929. // If cached is a promise, wait for it to resolve
  17930. if ( cached.then ) {
  17931. cached.then( imageBitmap => {
  17932. if ( onLoad ) onLoad( imageBitmap );
  17933. scope.manager.itemEnd( url );
  17934. } ).catch( e => {
  17935. if ( onError ) onError( e );
  17936. } );
  17937. return;
  17938. }
  17939. // If cached is not a promise (i.e., it's already an imageBitmap)
  17940. setTimeout( function () {
  17941. if ( onLoad ) onLoad( cached );
  17942. scope.manager.itemEnd( url );
  17943. }, 0 );
  17944. return cached;
  17945. }
  17946. const fetchOptions = {};
  17947. fetchOptions.credentials = ( this.crossOrigin === 'anonymous' ) ? 'same-origin' : 'include';
  17948. fetchOptions.headers = this.requestHeader;
  17949. const promise = fetch( url, fetchOptions ).then( function ( res ) {
  17950. return res.blob();
  17951. } ).then( function ( blob ) {
  17952. return createImageBitmap( blob, Object.assign( scope.options, { colorSpaceConversion: 'none' } ) );
  17953. } ).then( function ( imageBitmap ) {
  17954. Cache.add( url, imageBitmap );
  17955. if ( onLoad ) onLoad( imageBitmap );
  17956. scope.manager.itemEnd( url );
  17957. return imageBitmap;
  17958. } ).catch( function ( e ) {
  17959. if ( onError ) onError( e );
  17960. Cache.remove( url );
  17961. scope.manager.itemError( url );
  17962. scope.manager.itemEnd( url );
  17963. } );
  17964. Cache.add( url, promise );
  17965. scope.manager.itemStart( url );
  17966. }
  17967. }
  17968. let _context;
  17969. class AudioContext {
  17970. static getContext() {
  17971. if ( _context === undefined ) {
  17972. _context = new ( window.AudioContext || window.webkitAudioContext )();
  17973. }
  17974. return _context;
  17975. }
  17976. static setContext( value ) {
  17977. _context = value;
  17978. }
  17979. }
  17980. class AudioLoader extends Loader {
  17981. constructor( manager ) {
  17982. super( manager );
  17983. }
  17984. load( url, onLoad, onProgress, onError ) {
  17985. const scope = this;
  17986. const loader = new FileLoader( this.manager );
  17987. loader.setResponseType( 'arraybuffer' );
  17988. loader.setPath( this.path );
  17989. loader.setRequestHeader( this.requestHeader );
  17990. loader.setWithCredentials( this.withCredentials );
  17991. loader.load( url, function ( buffer ) {
  17992. try {
  17993. // Create a copy of the buffer. The `decodeAudioData` method
  17994. // detaches the buffer when complete, preventing reuse.
  17995. const bufferCopy = buffer.slice( 0 );
  17996. const context = AudioContext.getContext();
  17997. context.decodeAudioData( bufferCopy, function ( audioBuffer ) {
  17998. onLoad( audioBuffer );
  17999. } ).catch( handleError );
  18000. } catch ( e ) {
  18001. handleError( e );
  18002. }
  18003. }, onProgress, onError );
  18004. function handleError( e ) {
  18005. if ( onError ) {
  18006. onError( e );
  18007. } else {
  18008. console.error( e );
  18009. }
  18010. scope.manager.itemError( url );
  18011. }
  18012. }
  18013. }
  18014. const _eyeRight = /*@__PURE__*/ new Matrix4();
  18015. const _eyeLeft = /*@__PURE__*/ new Matrix4();
  18016. const _projectionMatrix = /*@__PURE__*/ new Matrix4();
  18017. class StereoCamera {
  18018. constructor() {
  18019. this.type = 'StereoCamera';
  18020. this.aspect = 1;
  18021. this.eyeSep = 0.064;
  18022. this.cameraL = new PerspectiveCamera();
  18023. this.cameraL.layers.enable( 1 );
  18024. this.cameraL.matrixAutoUpdate = false;
  18025. this.cameraR = new PerspectiveCamera();
  18026. this.cameraR.layers.enable( 2 );
  18027. this.cameraR.matrixAutoUpdate = false;
  18028. this._cache = {
  18029. focus: null,
  18030. fov: null,
  18031. aspect: null,
  18032. near: null,
  18033. far: null,
  18034. zoom: null,
  18035. eyeSep: null
  18036. };
  18037. }
  18038. update( camera ) {
  18039. const cache = this._cache;
  18040. const needsUpdate = cache.focus !== camera.focus || cache.fov !== camera.fov ||
  18041. cache.aspect !== camera.aspect * this.aspect || cache.near !== camera.near ||
  18042. cache.far !== camera.far || cache.zoom !== camera.zoom || cache.eyeSep !== this.eyeSep;
  18043. if ( needsUpdate ) {
  18044. cache.focus = camera.focus;
  18045. cache.fov = camera.fov;
  18046. cache.aspect = camera.aspect * this.aspect;
  18047. cache.near = camera.near;
  18048. cache.far = camera.far;
  18049. cache.zoom = camera.zoom;
  18050. cache.eyeSep = this.eyeSep;
  18051. // Off-axis stereoscopic effect based on
  18052. // http://paulbourke.net/stereographics/stereorender/
  18053. _projectionMatrix.copy( camera.projectionMatrix );
  18054. const eyeSepHalf = cache.eyeSep / 2;
  18055. const eyeSepOnProjection = eyeSepHalf * cache.near / cache.focus;
  18056. const ymax = ( cache.near * Math.tan( DEG2RAD * cache.fov * 0.5 ) ) / cache.zoom;
  18057. let xmin, xmax;
  18058. // translate xOffset
  18059. _eyeLeft.elements[ 12 ] = - eyeSepHalf;
  18060. _eyeRight.elements[ 12 ] = eyeSepHalf;
  18061. // for left eye
  18062. xmin = - ymax * cache.aspect + eyeSepOnProjection;
  18063. xmax = ymax * cache.aspect + eyeSepOnProjection;
  18064. _projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin );
  18065. _projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
  18066. this.cameraL.projectionMatrix.copy( _projectionMatrix );
  18067. // for right eye
  18068. xmin = - ymax * cache.aspect - eyeSepOnProjection;
  18069. xmax = ymax * cache.aspect - eyeSepOnProjection;
  18070. _projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin );
  18071. _projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin );
  18072. this.cameraR.projectionMatrix.copy( _projectionMatrix );
  18073. }
  18074. this.cameraL.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeLeft );
  18075. this.cameraR.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeRight );
  18076. }
  18077. }
  18078. class ArrayCamera extends PerspectiveCamera {
  18079. constructor( array = [] ) {
  18080. super();
  18081. this.isArrayCamera = true;
  18082. this.cameras = array;
  18083. }
  18084. }
  18085. class Clock {
  18086. constructor( autoStart = true ) {
  18087. this.autoStart = autoStart;
  18088. this.startTime = 0;
  18089. this.oldTime = 0;
  18090. this.elapsedTime = 0;
  18091. this.running = false;
  18092. }
  18093. start() {
  18094. this.startTime = now();
  18095. this.oldTime = this.startTime;
  18096. this.elapsedTime = 0;
  18097. this.running = true;
  18098. }
  18099. stop() {
  18100. this.getElapsedTime();
  18101. this.running = false;
  18102. this.autoStart = false;
  18103. }
  18104. getElapsedTime() {
  18105. this.getDelta();
  18106. return this.elapsedTime;
  18107. }
  18108. getDelta() {
  18109. let diff = 0;
  18110. if ( this.autoStart && ! this.running ) {
  18111. this.start();
  18112. return 0;
  18113. }
  18114. if ( this.running ) {
  18115. const newTime = now();
  18116. diff = ( newTime - this.oldTime ) / 1000;
  18117. this.oldTime = newTime;
  18118. this.elapsedTime += diff;
  18119. }
  18120. return diff;
  18121. }
  18122. }
  18123. function now() {
  18124. return performance.now();
  18125. }
  18126. const _position$1 = /*@__PURE__*/ new Vector3();
  18127. const _quaternion$1 = /*@__PURE__*/ new Quaternion();
  18128. const _scale$1 = /*@__PURE__*/ new Vector3();
  18129. const _orientation$1 = /*@__PURE__*/ new Vector3();
  18130. class AudioListener extends Object3D {
  18131. constructor() {
  18132. super();
  18133. this.type = 'AudioListener';
  18134. this.context = AudioContext.getContext();
  18135. this.gain = this.context.createGain();
  18136. this.gain.connect( this.context.destination );
  18137. this.filter = null;
  18138. this.timeDelta = 0;
  18139. // private
  18140. this._clock = new Clock();
  18141. }
  18142. getInput() {
  18143. return this.gain;
  18144. }
  18145. removeFilter() {
  18146. if ( this.filter !== null ) {
  18147. this.gain.disconnect( this.filter );
  18148. this.filter.disconnect( this.context.destination );
  18149. this.gain.connect( this.context.destination );
  18150. this.filter = null;
  18151. }
  18152. return this;
  18153. }
  18154. getFilter() {
  18155. return this.filter;
  18156. }
  18157. setFilter( value ) {
  18158. if ( this.filter !== null ) {
  18159. this.gain.disconnect( this.filter );
  18160. this.filter.disconnect( this.context.destination );
  18161. } else {
  18162. this.gain.disconnect( this.context.destination );
  18163. }
  18164. this.filter = value;
  18165. this.gain.connect( this.filter );
  18166. this.filter.connect( this.context.destination );
  18167. return this;
  18168. }
  18169. getMasterVolume() {
  18170. return this.gain.gain.value;
  18171. }
  18172. setMasterVolume( value ) {
  18173. this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
  18174. return this;
  18175. }
  18176. updateMatrixWorld( force ) {
  18177. super.updateMatrixWorld( force );
  18178. const listener = this.context.listener;
  18179. const up = this.up;
  18180. this.timeDelta = this._clock.getDelta();
  18181. this.matrixWorld.decompose( _position$1, _quaternion$1, _scale$1 );
  18182. _orientation$1.set( 0, 0, - 1 ).applyQuaternion( _quaternion$1 );
  18183. if ( listener.positionX ) {
  18184. // code path for Chrome (see #14393)
  18185. const endTime = this.context.currentTime + this.timeDelta;
  18186. listener.positionX.linearRampToValueAtTime( _position$1.x, endTime );
  18187. listener.positionY.linearRampToValueAtTime( _position$1.y, endTime );
  18188. listener.positionZ.linearRampToValueAtTime( _position$1.z, endTime );
  18189. listener.forwardX.linearRampToValueAtTime( _orientation$1.x, endTime );
  18190. listener.forwardY.linearRampToValueAtTime( _orientation$1.y, endTime );
  18191. listener.forwardZ.linearRampToValueAtTime( _orientation$1.z, endTime );
  18192. listener.upX.linearRampToValueAtTime( up.x, endTime );
  18193. listener.upY.linearRampToValueAtTime( up.y, endTime );
  18194. listener.upZ.linearRampToValueAtTime( up.z, endTime );
  18195. } else {
  18196. listener.setPosition( _position$1.x, _position$1.y, _position$1.z );
  18197. listener.setOrientation( _orientation$1.x, _orientation$1.y, _orientation$1.z, up.x, up.y, up.z );
  18198. }
  18199. }
  18200. }
  18201. class Audio extends Object3D {
  18202. constructor( listener ) {
  18203. super();
  18204. this.type = 'Audio';
  18205. this.listener = listener;
  18206. this.context = listener.context;
  18207. this.gain = this.context.createGain();
  18208. this.gain.connect( listener.getInput() );
  18209. this.autoplay = false;
  18210. this.buffer = null;
  18211. this.detune = 0;
  18212. this.loop = false;
  18213. this.loopStart = 0;
  18214. this.loopEnd = 0;
  18215. this.offset = 0;
  18216. this.duration = undefined;
  18217. this.playbackRate = 1;
  18218. this.isPlaying = false;
  18219. this.hasPlaybackControl = true;
  18220. this.source = null;
  18221. this.sourceType = 'empty';
  18222. this._startedAt = 0;
  18223. this._progress = 0;
  18224. this._connected = false;
  18225. this.filters = [];
  18226. }
  18227. getOutput() {
  18228. return this.gain;
  18229. }
  18230. setNodeSource( audioNode ) {
  18231. this.hasPlaybackControl = false;
  18232. this.sourceType = 'audioNode';
  18233. this.source = audioNode;
  18234. this.connect();
  18235. return this;
  18236. }
  18237. setMediaElementSource( mediaElement ) {
  18238. this.hasPlaybackControl = false;
  18239. this.sourceType = 'mediaNode';
  18240. this.source = this.context.createMediaElementSource( mediaElement );
  18241. this.connect();
  18242. return this;
  18243. }
  18244. setMediaStreamSource( mediaStream ) {
  18245. this.hasPlaybackControl = false;
  18246. this.sourceType = 'mediaStreamNode';
  18247. this.source = this.context.createMediaStreamSource( mediaStream );
  18248. this.connect();
  18249. return this;
  18250. }
  18251. setBuffer( audioBuffer ) {
  18252. this.buffer = audioBuffer;
  18253. this.sourceType = 'buffer';
  18254. if ( this.autoplay ) this.play();
  18255. return this;
  18256. }
  18257. play( delay = 0 ) {
  18258. if ( this.isPlaying === true ) {
  18259. console.warn( 'THREE.Audio: Audio is already playing.' );
  18260. return;
  18261. }
  18262. if ( this.hasPlaybackControl === false ) {
  18263. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18264. return;
  18265. }
  18266. this._startedAt = this.context.currentTime + delay;
  18267. const source = this.context.createBufferSource();
  18268. source.buffer = this.buffer;
  18269. source.loop = this.loop;
  18270. source.loopStart = this.loopStart;
  18271. source.loopEnd = this.loopEnd;
  18272. source.onended = this.onEnded.bind( this );
  18273. source.start( this._startedAt, this._progress + this.offset, this.duration );
  18274. this.isPlaying = true;
  18275. this.source = source;
  18276. this.setDetune( this.detune );
  18277. this.setPlaybackRate( this.playbackRate );
  18278. return this.connect();
  18279. }
  18280. pause() {
  18281. if ( this.hasPlaybackControl === false ) {
  18282. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18283. return;
  18284. }
  18285. if ( this.isPlaying === true ) {
  18286. // update current progress
  18287. this._progress += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate;
  18288. if ( this.loop === true ) {
  18289. // ensure _progress does not exceed duration with looped audios
  18290. this._progress = this._progress % ( this.duration || this.buffer.duration );
  18291. }
  18292. this.source.stop();
  18293. this.source.onended = null;
  18294. this.isPlaying = false;
  18295. }
  18296. return this;
  18297. }
  18298. stop( delay = 0 ) {
  18299. if ( this.hasPlaybackControl === false ) {
  18300. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18301. return;
  18302. }
  18303. this._progress = 0;
  18304. if ( this.source !== null ) {
  18305. this.source.stop( this.context.currentTime + delay );
  18306. this.source.onended = null;
  18307. }
  18308. this.isPlaying = false;
  18309. return this;
  18310. }
  18311. connect() {
  18312. if ( this.filters.length > 0 ) {
  18313. this.source.connect( this.filters[ 0 ] );
  18314. for ( let i = 1, l = this.filters.length; i < l; i ++ ) {
  18315. this.filters[ i - 1 ].connect( this.filters[ i ] );
  18316. }
  18317. this.filters[ this.filters.length - 1 ].connect( this.getOutput() );
  18318. } else {
  18319. this.source.connect( this.getOutput() );
  18320. }
  18321. this._connected = true;
  18322. return this;
  18323. }
  18324. disconnect() {
  18325. if ( this._connected === false ) {
  18326. return;
  18327. }
  18328. if ( this.filters.length > 0 ) {
  18329. this.source.disconnect( this.filters[ 0 ] );
  18330. for ( let i = 1, l = this.filters.length; i < l; i ++ ) {
  18331. this.filters[ i - 1 ].disconnect( this.filters[ i ] );
  18332. }
  18333. this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() );
  18334. } else {
  18335. this.source.disconnect( this.getOutput() );
  18336. }
  18337. this._connected = false;
  18338. return this;
  18339. }
  18340. getFilters() {
  18341. return this.filters;
  18342. }
  18343. setFilters( value ) {
  18344. if ( ! value ) value = [];
  18345. if ( this._connected === true ) {
  18346. this.disconnect();
  18347. this.filters = value.slice();
  18348. this.connect();
  18349. } else {
  18350. this.filters = value.slice();
  18351. }
  18352. return this;
  18353. }
  18354. setDetune( value ) {
  18355. this.detune = value;
  18356. if ( this.isPlaying === true && this.source.detune !== undefined ) {
  18357. this.source.detune.setTargetAtTime( this.detune, this.context.currentTime, 0.01 );
  18358. }
  18359. return this;
  18360. }
  18361. getDetune() {
  18362. return this.detune;
  18363. }
  18364. getFilter() {
  18365. return this.getFilters()[ 0 ];
  18366. }
  18367. setFilter( filter ) {
  18368. return this.setFilters( filter ? [ filter ] : [] );
  18369. }
  18370. setPlaybackRate( value ) {
  18371. if ( this.hasPlaybackControl === false ) {
  18372. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18373. return;
  18374. }
  18375. this.playbackRate = value;
  18376. if ( this.isPlaying === true ) {
  18377. this.source.playbackRate.setTargetAtTime( this.playbackRate, this.context.currentTime, 0.01 );
  18378. }
  18379. return this;
  18380. }
  18381. getPlaybackRate() {
  18382. return this.playbackRate;
  18383. }
  18384. onEnded() {
  18385. this.isPlaying = false;
  18386. }
  18387. getLoop() {
  18388. if ( this.hasPlaybackControl === false ) {
  18389. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18390. return false;
  18391. }
  18392. return this.loop;
  18393. }
  18394. setLoop( value ) {
  18395. if ( this.hasPlaybackControl === false ) {
  18396. console.warn( 'THREE.Audio: this Audio has no playback control.' );
  18397. return;
  18398. }
  18399. this.loop = value;
  18400. if ( this.isPlaying === true ) {
  18401. this.source.loop = this.loop;
  18402. }
  18403. return this;
  18404. }
  18405. setLoopStart( value ) {
  18406. this.loopStart = value;
  18407. return this;
  18408. }
  18409. setLoopEnd( value ) {
  18410. this.loopEnd = value;
  18411. return this;
  18412. }
  18413. getVolume() {
  18414. return this.gain.gain.value;
  18415. }
  18416. setVolume( value ) {
  18417. this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 );
  18418. return this;
  18419. }
  18420. }
  18421. const _position = /*@__PURE__*/ new Vector3();
  18422. const _quaternion = /*@__PURE__*/ new Quaternion();
  18423. const _scale = /*@__PURE__*/ new Vector3();
  18424. const _orientation = /*@__PURE__*/ new Vector3();
  18425. class PositionalAudio extends Audio {
  18426. constructor( listener ) {
  18427. super( listener );
  18428. this.panner = this.context.createPanner();
  18429. this.panner.panningModel = 'HRTF';
  18430. this.panner.connect( this.gain );
  18431. }
  18432. connect() {
  18433. super.connect();
  18434. this.panner.connect( this.gain );
  18435. }
  18436. disconnect() {
  18437. super.disconnect();
  18438. this.panner.disconnect( this.gain );
  18439. }
  18440. getOutput() {
  18441. return this.panner;
  18442. }
  18443. getRefDistance() {
  18444. return this.panner.refDistance;
  18445. }
  18446. setRefDistance( value ) {
  18447. this.panner.refDistance = value;
  18448. return this;
  18449. }
  18450. getRolloffFactor() {
  18451. return this.panner.rolloffFactor;
  18452. }
  18453. setRolloffFactor( value ) {
  18454. this.panner.rolloffFactor = value;
  18455. return this;
  18456. }
  18457. getDistanceModel() {
  18458. return this.panner.distanceModel;
  18459. }
  18460. setDistanceModel( value ) {
  18461. this.panner.distanceModel = value;
  18462. return this;
  18463. }
  18464. getMaxDistance() {
  18465. return this.panner.maxDistance;
  18466. }
  18467. setMaxDistance( value ) {
  18468. this.panner.maxDistance = value;
  18469. return this;
  18470. }
  18471. setDirectionalCone( coneInnerAngle, coneOuterAngle, coneOuterGain ) {
  18472. this.panner.coneInnerAngle = coneInnerAngle;
  18473. this.panner.coneOuterAngle = coneOuterAngle;
  18474. this.panner.coneOuterGain = coneOuterGain;
  18475. return this;
  18476. }
  18477. updateMatrixWorld( force ) {
  18478. super.updateMatrixWorld( force );
  18479. if ( this.hasPlaybackControl === true && this.isPlaying === false ) return;
  18480. this.matrixWorld.decompose( _position, _quaternion, _scale );
  18481. _orientation.set( 0, 0, 1 ).applyQuaternion( _quaternion );
  18482. const panner = this.panner;
  18483. if ( panner.positionX ) {
  18484. // code path for Chrome and Firefox (see #14393)
  18485. const endTime = this.context.currentTime + this.listener.timeDelta;
  18486. panner.positionX.linearRampToValueAtTime( _position.x, endTime );
  18487. panner.positionY.linearRampToValueAtTime( _position.y, endTime );
  18488. panner.positionZ.linearRampToValueAtTime( _position.z, endTime );
  18489. panner.orientationX.linearRampToValueAtTime( _orientation.x, endTime );
  18490. panner.orientationY.linearRampToValueAtTime( _orientation.y, endTime );
  18491. panner.orientationZ.linearRampToValueAtTime( _orientation.z, endTime );
  18492. } else {
  18493. panner.setPosition( _position.x, _position.y, _position.z );
  18494. panner.setOrientation( _orientation.x, _orientation.y, _orientation.z );
  18495. }
  18496. }
  18497. }
  18498. class AudioAnalyser {
  18499. constructor( audio, fftSize = 2048 ) {
  18500. this.analyser = audio.context.createAnalyser();
  18501. this.analyser.fftSize = fftSize;
  18502. this.data = new Uint8Array( this.analyser.frequencyBinCount );
  18503. audio.getOutput().connect( this.analyser );
  18504. }
  18505. getFrequencyData() {
  18506. this.analyser.getByteFrequencyData( this.data );
  18507. return this.data;
  18508. }
  18509. getAverageFrequency() {
  18510. let value = 0;
  18511. const data = this.getFrequencyData();
  18512. for ( let i = 0; i < data.length; i ++ ) {
  18513. value += data[ i ];
  18514. }
  18515. return value / data.length;
  18516. }
  18517. }
  18518. class PropertyMixer {
  18519. constructor( binding, typeName, valueSize ) {
  18520. this.binding = binding;
  18521. this.valueSize = valueSize;
  18522. let mixFunction,
  18523. mixFunctionAdditive,
  18524. setIdentity;
  18525. // buffer layout: [ incoming | accu0 | accu1 | orig | addAccu | (optional work) ]
  18526. //
  18527. // interpolators can use .buffer as their .result
  18528. // the data then goes to 'incoming'
  18529. //
  18530. // 'accu0' and 'accu1' are used frame-interleaved for
  18531. // the cumulative result and are compared to detect
  18532. // changes
  18533. //
  18534. // 'orig' stores the original state of the property
  18535. //
  18536. // 'add' is used for additive cumulative results
  18537. //
  18538. // 'work' is optional and is only present for quaternion types. It is used
  18539. // to store intermediate quaternion multiplication results
  18540. switch ( typeName ) {
  18541. case 'quaternion':
  18542. mixFunction = this._slerp;
  18543. mixFunctionAdditive = this._slerpAdditive;
  18544. setIdentity = this._setAdditiveIdentityQuaternion;
  18545. this.buffer = new Float64Array( valueSize * 6 );
  18546. this._workIndex = 5;
  18547. break;
  18548. case 'string':
  18549. case 'bool':
  18550. mixFunction = this._select;
  18551. // Use the regular mix function and for additive on these types,
  18552. // additive is not relevant for non-numeric types
  18553. mixFunctionAdditive = this._select;
  18554. setIdentity = this._setAdditiveIdentityOther;
  18555. this.buffer = new Array( valueSize * 5 );
  18556. break;
  18557. default:
  18558. mixFunction = this._lerp;
  18559. mixFunctionAdditive = this._lerpAdditive;
  18560. setIdentity = this._setAdditiveIdentityNumeric;
  18561. this.buffer = new Float64Array( valueSize * 5 );
  18562. }
  18563. this._mixBufferRegion = mixFunction;
  18564. this._mixBufferRegionAdditive = mixFunctionAdditive;
  18565. this._setIdentity = setIdentity;
  18566. this._origIndex = 3;
  18567. this._addIndex = 4;
  18568. this.cumulativeWeight = 0;
  18569. this.cumulativeWeightAdditive = 0;
  18570. this.useCount = 0;
  18571. this.referenceCount = 0;
  18572. }
  18573. // accumulate data in the 'incoming' region into 'accu<i>'
  18574. accumulate( accuIndex, weight ) {
  18575. // note: happily accumulating nothing when weight = 0, the caller knows
  18576. // the weight and shouldn't have made the call in the first place
  18577. const buffer = this.buffer,
  18578. stride = this.valueSize,
  18579. offset = accuIndex * stride + stride;
  18580. let currentWeight = this.cumulativeWeight;
  18581. if ( currentWeight === 0 ) {
  18582. // accuN := incoming * weight
  18583. for ( let i = 0; i !== stride; ++ i ) {
  18584. buffer[ offset + i ] = buffer[ i ];
  18585. }
  18586. currentWeight = weight;
  18587. } else {
  18588. // accuN := accuN + incoming * weight
  18589. currentWeight += weight;
  18590. const mix = weight / currentWeight;
  18591. this._mixBufferRegion( buffer, offset, 0, mix, stride );
  18592. }
  18593. this.cumulativeWeight = currentWeight;
  18594. }
  18595. // accumulate data in the 'incoming' region into 'add'
  18596. accumulateAdditive( weight ) {
  18597. const buffer = this.buffer,
  18598. stride = this.valueSize,
  18599. offset = stride * this._addIndex;
  18600. if ( this.cumulativeWeightAdditive === 0 ) {
  18601. // add = identity
  18602. this._setIdentity();
  18603. }
  18604. // add := add + incoming * weight
  18605. this._mixBufferRegionAdditive( buffer, offset, 0, weight, stride );
  18606. this.cumulativeWeightAdditive += weight;
  18607. }
  18608. // apply the state of 'accu<i>' to the binding when accus differ
  18609. apply( accuIndex ) {
  18610. const stride = this.valueSize,
  18611. buffer = this.buffer,
  18612. offset = accuIndex * stride + stride,
  18613. weight = this.cumulativeWeight,
  18614. weightAdditive = this.cumulativeWeightAdditive,
  18615. binding = this.binding;
  18616. this.cumulativeWeight = 0;
  18617. this.cumulativeWeightAdditive = 0;
  18618. if ( weight < 1 ) {
  18619. // accuN := accuN + original * ( 1 - cumulativeWeight )
  18620. const originalValueOffset = stride * this._origIndex;
  18621. this._mixBufferRegion(
  18622. buffer, offset, originalValueOffset, 1 - weight, stride );
  18623. }
  18624. if ( weightAdditive > 0 ) {
  18625. // accuN := accuN + additive accuN
  18626. this._mixBufferRegionAdditive( buffer, offset, this._addIndex * stride, 1, stride );
  18627. }
  18628. for ( let i = stride, e = stride + stride; i !== e; ++ i ) {
  18629. if ( buffer[ i ] !== buffer[ i + stride ] ) {
  18630. // value has changed -> update scene graph
  18631. binding.setValue( buffer, offset );
  18632. break;
  18633. }
  18634. }
  18635. }
  18636. // remember the state of the bound property and copy it to both accus
  18637. saveOriginalState() {
  18638. const binding = this.binding;
  18639. const buffer = this.buffer,
  18640. stride = this.valueSize,
  18641. originalValueOffset = stride * this._origIndex;
  18642. binding.getValue( buffer, originalValueOffset );
  18643. // accu[0..1] := orig -- initially detect changes against the original
  18644. for ( let i = stride, e = originalValueOffset; i !== e; ++ i ) {
  18645. buffer[ i ] = buffer[ originalValueOffset + ( i % stride ) ];
  18646. }
  18647. // Add to identity for additive
  18648. this._setIdentity();
  18649. this.cumulativeWeight = 0;
  18650. this.cumulativeWeightAdditive = 0;
  18651. }
  18652. // apply the state previously taken via 'saveOriginalState' to the binding
  18653. restoreOriginalState() {
  18654. const originalValueOffset = this.valueSize * 3;
  18655. this.binding.setValue( this.buffer, originalValueOffset );
  18656. }
  18657. _setAdditiveIdentityNumeric() {
  18658. const startIndex = this._addIndex * this.valueSize;
  18659. const endIndex = startIndex + this.valueSize;
  18660. for ( let i = startIndex; i < endIndex; i ++ ) {
  18661. this.buffer[ i ] = 0;
  18662. }
  18663. }
  18664. _setAdditiveIdentityQuaternion() {
  18665. this._setAdditiveIdentityNumeric();
  18666. this.buffer[ this._addIndex * this.valueSize + 3 ] = 1;
  18667. }
  18668. _setAdditiveIdentityOther() {
  18669. const startIndex = this._origIndex * this.valueSize;
  18670. const targetIndex = this._addIndex * this.valueSize;
  18671. for ( let i = 0; i < this.valueSize; i ++ ) {
  18672. this.buffer[ targetIndex + i ] = this.buffer[ startIndex + i ];
  18673. }
  18674. }
  18675. // mix functions
  18676. _select( buffer, dstOffset, srcOffset, t, stride ) {
  18677. if ( t >= 0.5 ) {
  18678. for ( let i = 0; i !== stride; ++ i ) {
  18679. buffer[ dstOffset + i ] = buffer[ srcOffset + i ];
  18680. }
  18681. }
  18682. }
  18683. _slerp( buffer, dstOffset, srcOffset, t ) {
  18684. Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, srcOffset, t );
  18685. }
  18686. _slerpAdditive( buffer, dstOffset, srcOffset, t, stride ) {
  18687. const workOffset = this._workIndex * stride;
  18688. // Store result in intermediate buffer offset
  18689. Quaternion.multiplyQuaternionsFlat( buffer, workOffset, buffer, dstOffset, buffer, srcOffset );
  18690. // Slerp to the intermediate result
  18691. Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, workOffset, t );
  18692. }
  18693. _lerp( buffer, dstOffset, srcOffset, t, stride ) {
  18694. const s = 1 - t;
  18695. for ( let i = 0; i !== stride; ++ i ) {
  18696. const j = dstOffset + i;
  18697. buffer[ j ] = buffer[ j ] * s + buffer[ srcOffset + i ] * t;
  18698. }
  18699. }
  18700. _lerpAdditive( buffer, dstOffset, srcOffset, t, stride ) {
  18701. for ( let i = 0; i !== stride; ++ i ) {
  18702. const j = dstOffset + i;
  18703. buffer[ j ] = buffer[ j ] + buffer[ srcOffset + i ] * t;
  18704. }
  18705. }
  18706. }
  18707. // Characters [].:/ are reserved for track binding syntax.
  18708. const _RESERVED_CHARS_RE = '\\[\\]\\.:\\/';
  18709. const _reservedRe = new RegExp( '[' + _RESERVED_CHARS_RE + ']', 'g' );
  18710. // Attempts to allow node names from any language. ES5's `\w` regexp matches
  18711. // only latin characters, and the unicode \p{L} is not yet supported. So
  18712. // instead, we exclude reserved characters and match everything else.
  18713. const _wordChar = '[^' + _RESERVED_CHARS_RE + ']';
  18714. const _wordCharOrDot = '[^' + _RESERVED_CHARS_RE.replace( '\\.', '' ) + ']';
  18715. // Parent directories, delimited by '/' or ':'. Currently unused, but must
  18716. // be matched to parse the rest of the track name.
  18717. const _directoryRe = /*@__PURE__*/ /((?:WC+[\/:])*)/.source.replace( 'WC', _wordChar );
  18718. // Target node. May contain word characters (a-zA-Z0-9_) and '.' or '-'.
  18719. const _nodeRe = /*@__PURE__*/ /(WCOD+)?/.source.replace( 'WCOD', _wordCharOrDot );
  18720. // Object on target node, and accessor. May not contain reserved
  18721. // characters. Accessor may contain any character except closing bracket.
  18722. const _objectRe = /*@__PURE__*/ /(?:\.(WC+)(?:\[(.+)\])?)?/.source.replace( 'WC', _wordChar );
  18723. // Property and accessor. May not contain reserved characters. Accessor may
  18724. // contain any non-bracket characters.
  18725. const _propertyRe = /*@__PURE__*/ /\.(WC+)(?:\[(.+)\])?/.source.replace( 'WC', _wordChar );
  18726. const _trackRe = new RegExp( ''
  18727. + '^'
  18728. + _directoryRe
  18729. + _nodeRe
  18730. + _objectRe
  18731. + _propertyRe
  18732. + '$'
  18733. );
  18734. const _supportedObjectNames = [ 'material', 'materials', 'bones', 'map' ];
  18735. class Composite {
  18736. constructor( targetGroup, path, optionalParsedPath ) {
  18737. const parsedPath = optionalParsedPath || PropertyBinding.parseTrackName( path );
  18738. this._targetGroup = targetGroup;
  18739. this._bindings = targetGroup.subscribe_( path, parsedPath );
  18740. }
  18741. getValue( array, offset ) {
  18742. this.bind(); // bind all binding
  18743. const firstValidIndex = this._targetGroup.nCachedObjects_,
  18744. binding = this._bindings[ firstValidIndex ];
  18745. // and only call .getValue on the first
  18746. if ( binding !== undefined ) binding.getValue( array, offset );
  18747. }
  18748. setValue( array, offset ) {
  18749. const bindings = this._bindings;
  18750. for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
  18751. bindings[ i ].setValue( array, offset );
  18752. }
  18753. }
  18754. bind() {
  18755. const bindings = this._bindings;
  18756. for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
  18757. bindings[ i ].bind();
  18758. }
  18759. }
  18760. unbind() {
  18761. const bindings = this._bindings;
  18762. for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) {
  18763. bindings[ i ].unbind();
  18764. }
  18765. }
  18766. }
  18767. // Note: This class uses a State pattern on a per-method basis:
  18768. // 'bind' sets 'this.getValue' / 'setValue' and shadows the
  18769. // prototype version of these methods with one that represents
  18770. // the bound state. When the property is not found, the methods
  18771. // become no-ops.
  18772. class PropertyBinding {
  18773. constructor( rootNode, path, parsedPath ) {
  18774. this.path = path;
  18775. this.parsedPath = parsedPath || PropertyBinding.parseTrackName( path );
  18776. this.node = PropertyBinding.findNode( rootNode, this.parsedPath.nodeName );
  18777. this.rootNode = rootNode;
  18778. // initial state of these methods that calls 'bind'
  18779. this.getValue = this._getValue_unbound;
  18780. this.setValue = this._setValue_unbound;
  18781. }
  18782. static create( root, path, parsedPath ) {
  18783. if ( ! ( root && root.isAnimationObjectGroup ) ) {
  18784. return new PropertyBinding( root, path, parsedPath );
  18785. } else {
  18786. return new PropertyBinding.Composite( root, path, parsedPath );
  18787. }
  18788. }
  18789. /**
  18790. * Replaces spaces with underscores and removes unsupported characters from
  18791. * node names, to ensure compatibility with parseTrackName().
  18792. *
  18793. * @param {string} name Node name to be sanitized.
  18794. * @return {string}
  18795. */
  18796. static sanitizeNodeName( name ) {
  18797. return name.replace( /\s/g, '_' ).replace( _reservedRe, '' );
  18798. }
  18799. static parseTrackName( trackName ) {
  18800. const matches = _trackRe.exec( trackName );
  18801. if ( matches === null ) {
  18802. throw new Error( 'PropertyBinding: Cannot parse trackName: ' + trackName );
  18803. }
  18804. const results = {
  18805. // directoryName: matches[ 1 ], // (tschw) currently unused
  18806. nodeName: matches[ 2 ],
  18807. objectName: matches[ 3 ],
  18808. objectIndex: matches[ 4 ],
  18809. propertyName: matches[ 5 ], // required
  18810. propertyIndex: matches[ 6 ]
  18811. };
  18812. const lastDot = results.nodeName && results.nodeName.lastIndexOf( '.' );
  18813. if ( lastDot !== undefined && lastDot !== - 1 ) {
  18814. const objectName = results.nodeName.substring( lastDot + 1 );
  18815. // Object names must be checked against an allowlist. Otherwise, there
  18816. // is no way to parse 'foo.bar.baz': 'baz' must be a property, but
  18817. // 'bar' could be the objectName, or part of a nodeName (which can
  18818. // include '.' characters).
  18819. if ( _supportedObjectNames.indexOf( objectName ) !== - 1 ) {
  18820. results.nodeName = results.nodeName.substring( 0, lastDot );
  18821. results.objectName = objectName;
  18822. }
  18823. }
  18824. if ( results.propertyName === null || results.propertyName.length === 0 ) {
  18825. throw new Error( 'PropertyBinding: can not parse propertyName from trackName: ' + trackName );
  18826. }
  18827. return results;
  18828. }
  18829. static findNode( root, nodeName ) {
  18830. if ( nodeName === undefined || nodeName === '' || nodeName === '.' || nodeName === - 1 || nodeName === root.name || nodeName === root.uuid ) {
  18831. return root;
  18832. }
  18833. // search into skeleton bones.
  18834. if ( root.skeleton ) {
  18835. const bone = root.skeleton.getBoneByName( nodeName );
  18836. if ( bone !== undefined ) {
  18837. return bone;
  18838. }
  18839. }
  18840. // search into node subtree.
  18841. if ( root.children ) {
  18842. const searchNodeSubtree = function ( children ) {
  18843. for ( let i = 0; i < children.length; i ++ ) {
  18844. const childNode = children[ i ];
  18845. if ( childNode.name === nodeName || childNode.uuid === nodeName ) {
  18846. return childNode;
  18847. }
  18848. const result = searchNodeSubtree( childNode.children );
  18849. if ( result ) return result;
  18850. }
  18851. return null;
  18852. };
  18853. const subTreeNode = searchNodeSubtree( root.children );
  18854. if ( subTreeNode ) {
  18855. return subTreeNode;
  18856. }
  18857. }
  18858. return null;
  18859. }
  18860. // these are used to "bind" a nonexistent property
  18861. _getValue_unavailable() {}
  18862. _setValue_unavailable() {}
  18863. // Getters
  18864. _getValue_direct( buffer, offset ) {
  18865. buffer[ offset ] = this.targetObject[ this.propertyName ];
  18866. }
  18867. _getValue_array( buffer, offset ) {
  18868. const source = this.resolvedProperty;
  18869. for ( let i = 0, n = source.length; i !== n; ++ i ) {
  18870. buffer[ offset ++ ] = source[ i ];
  18871. }
  18872. }
  18873. _getValue_arrayElement( buffer, offset ) {
  18874. buffer[ offset ] = this.resolvedProperty[ this.propertyIndex ];
  18875. }
  18876. _getValue_toArray( buffer, offset ) {
  18877. this.resolvedProperty.toArray( buffer, offset );
  18878. }
  18879. // Direct
  18880. _setValue_direct( buffer, offset ) {
  18881. this.targetObject[ this.propertyName ] = buffer[ offset ];
  18882. }
  18883. _setValue_direct_setNeedsUpdate( buffer, offset ) {
  18884. this.targetObject[ this.propertyName ] = buffer[ offset ];
  18885. this.targetObject.needsUpdate = true;
  18886. }
  18887. _setValue_direct_setMatrixWorldNeedsUpdate( buffer, offset ) {
  18888. this.targetObject[ this.propertyName ] = buffer[ offset ];
  18889. this.targetObject.matrixWorldNeedsUpdate = true;
  18890. }
  18891. // EntireArray
  18892. _setValue_array( buffer, offset ) {
  18893. const dest = this.resolvedProperty;
  18894. for ( let i = 0, n = dest.length; i !== n; ++ i ) {
  18895. dest[ i ] = buffer[ offset ++ ];
  18896. }
  18897. }
  18898. _setValue_array_setNeedsUpdate( buffer, offset ) {
  18899. const dest = this.resolvedProperty;
  18900. for ( let i = 0, n = dest.length; i !== n; ++ i ) {
  18901. dest[ i ] = buffer[ offset ++ ];
  18902. }
  18903. this.targetObject.needsUpdate = true;
  18904. }
  18905. _setValue_array_setMatrixWorldNeedsUpdate( buffer, offset ) {
  18906. const dest = this.resolvedProperty;
  18907. for ( let i = 0, n = dest.length; i !== n; ++ i ) {
  18908. dest[ i ] = buffer[ offset ++ ];
  18909. }
  18910. this.targetObject.matrixWorldNeedsUpdate = true;
  18911. }
  18912. // ArrayElement
  18913. _setValue_arrayElement( buffer, offset ) {
  18914. this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
  18915. }
  18916. _setValue_arrayElement_setNeedsUpdate( buffer, offset ) {
  18917. this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
  18918. this.targetObject.needsUpdate = true;
  18919. }
  18920. _setValue_arrayElement_setMatrixWorldNeedsUpdate( buffer, offset ) {
  18921. this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ];
  18922. this.targetObject.matrixWorldNeedsUpdate = true;
  18923. }
  18924. // HasToFromArray
  18925. _setValue_fromArray( buffer, offset ) {
  18926. this.resolvedProperty.fromArray( buffer, offset );
  18927. }
  18928. _setValue_fromArray_setNeedsUpdate( buffer, offset ) {
  18929. this.resolvedProperty.fromArray( buffer, offset );
  18930. this.targetObject.needsUpdate = true;
  18931. }
  18932. _setValue_fromArray_setMatrixWorldNeedsUpdate( buffer, offset ) {
  18933. this.resolvedProperty.fromArray( buffer, offset );
  18934. this.targetObject.matrixWorldNeedsUpdate = true;
  18935. }
  18936. _getValue_unbound( targetArray, offset ) {
  18937. this.bind();
  18938. this.getValue( targetArray, offset );
  18939. }
  18940. _setValue_unbound( sourceArray, offset ) {
  18941. this.bind();
  18942. this.setValue( sourceArray, offset );
  18943. }
  18944. // create getter / setter pair for a property in the scene graph
  18945. bind() {
  18946. let targetObject = this.node;
  18947. const parsedPath = this.parsedPath;
  18948. const objectName = parsedPath.objectName;
  18949. const propertyName = parsedPath.propertyName;
  18950. let propertyIndex = parsedPath.propertyIndex;
  18951. if ( ! targetObject ) {
  18952. targetObject = PropertyBinding.findNode( this.rootNode, parsedPath.nodeName );
  18953. this.node = targetObject;
  18954. }
  18955. // set fail state so we can just 'return' on error
  18956. this.getValue = this._getValue_unavailable;
  18957. this.setValue = this._setValue_unavailable;
  18958. // ensure there is a value node
  18959. if ( ! targetObject ) {
  18960. console.warn( 'THREE.PropertyBinding: No target node found for track: ' + this.path + '.' );
  18961. return;
  18962. }
  18963. if ( objectName ) {
  18964. let objectIndex = parsedPath.objectIndex;
  18965. // special cases were we need to reach deeper into the hierarchy to get the face materials....
  18966. switch ( objectName ) {
  18967. case 'materials':
  18968. if ( ! targetObject.material ) {
  18969. console.error( 'THREE.PropertyBinding: Can not bind to material as node does not have a material.', this );
  18970. return;
  18971. }
  18972. if ( ! targetObject.material.materials ) {
  18973. console.error( 'THREE.PropertyBinding: Can not bind to material.materials as node.material does not have a materials array.', this );
  18974. return;
  18975. }
  18976. targetObject = targetObject.material.materials;
  18977. break;
  18978. case 'bones':
  18979. if ( ! targetObject.skeleton ) {
  18980. console.error( 'THREE.PropertyBinding: Can not bind to bones as node does not have a skeleton.', this );
  18981. return;
  18982. }
  18983. // potential future optimization: skip this if propertyIndex is already an integer
  18984. // and convert the integer string to a true integer.
  18985. targetObject = targetObject.skeleton.bones;
  18986. // support resolving morphTarget names into indices.
  18987. for ( let i = 0; i < targetObject.length; i ++ ) {
  18988. if ( targetObject[ i ].name === objectIndex ) {
  18989. objectIndex = i;
  18990. break;
  18991. }
  18992. }
  18993. break;
  18994. case 'map':
  18995. if ( 'map' in targetObject ) {
  18996. targetObject = targetObject.map;
  18997. break;
  18998. }
  18999. if ( ! targetObject.material ) {
  19000. console.error( 'THREE.PropertyBinding: Can not bind to material as node does not have a material.', this );
  19001. return;
  19002. }
  19003. if ( ! targetObject.material.map ) {
  19004. console.error( 'THREE.PropertyBinding: Can not bind to material.map as node.material does not have a map.', this );
  19005. return;
  19006. }
  19007. targetObject = targetObject.material.map;
  19008. break;
  19009. default:
  19010. if ( targetObject[ objectName ] === undefined ) {
  19011. console.error( 'THREE.PropertyBinding: Can not bind to objectName of node undefined.', this );
  19012. return;
  19013. }
  19014. targetObject = targetObject[ objectName ];
  19015. }
  19016. if ( objectIndex !== undefined ) {
  19017. if ( targetObject[ objectIndex ] === undefined ) {
  19018. console.error( 'THREE.PropertyBinding: Trying to bind to objectIndex of objectName, but is undefined.', this, targetObject );
  19019. return;
  19020. }
  19021. targetObject = targetObject[ objectIndex ];
  19022. }
  19023. }
  19024. // resolve property
  19025. const nodeProperty = targetObject[ propertyName ];
  19026. if ( nodeProperty === undefined ) {
  19027. const nodeName = parsedPath.nodeName;
  19028. console.error( 'THREE.PropertyBinding: Trying to update property for track: ' + nodeName +
  19029. '.' + propertyName + ' but it wasn\'t found.', targetObject );
  19030. return;
  19031. }
  19032. // determine versioning scheme
  19033. let versioning = this.Versioning.None;
  19034. this.targetObject = targetObject;
  19035. if ( targetObject.needsUpdate !== undefined ) { // material
  19036. versioning = this.Versioning.NeedsUpdate;
  19037. } else if ( targetObject.matrixWorldNeedsUpdate !== undefined ) { // node transform
  19038. versioning = this.Versioning.MatrixWorldNeedsUpdate;
  19039. }
  19040. // determine how the property gets bound
  19041. let bindingType = this.BindingType.Direct;
  19042. if ( propertyIndex !== undefined ) {
  19043. // access a sub element of the property array (only primitives are supported right now)
  19044. if ( propertyName === 'morphTargetInfluences' ) {
  19045. // potential optimization, skip this if propertyIndex is already an integer, and convert the integer string to a true integer.
  19046. // support resolving morphTarget names into indices.
  19047. if ( ! targetObject.geometry ) {
  19048. console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.', this );
  19049. return;
  19050. }
  19051. if ( ! targetObject.geometry.morphAttributes ) {
  19052. console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphAttributes.', this );
  19053. return;
  19054. }
  19055. if ( targetObject.morphTargetDictionary[ propertyIndex ] !== undefined ) {
  19056. propertyIndex = targetObject.morphTargetDictionary[ propertyIndex ];
  19057. }
  19058. }
  19059. bindingType = this.BindingType.ArrayElement;
  19060. this.resolvedProperty = nodeProperty;
  19061. this.propertyIndex = propertyIndex;
  19062. } else if ( nodeProperty.fromArray !== undefined && nodeProperty.toArray !== undefined ) {
  19063. // must use copy for Object3D.Euler/Quaternion
  19064. bindingType = this.BindingType.HasFromToArray;
  19065. this.resolvedProperty = nodeProperty;
  19066. } else if ( Array.isArray( nodeProperty ) ) {
  19067. bindingType = this.BindingType.EntireArray;
  19068. this.resolvedProperty = nodeProperty;
  19069. } else {
  19070. this.propertyName = propertyName;
  19071. }
  19072. // select getter / setter
  19073. this.getValue = this.GetterByBindingType[ bindingType ];
  19074. this.setValue = this.SetterByBindingTypeAndVersioning[ bindingType ][ versioning ];
  19075. }
  19076. unbind() {
  19077. this.node = null;
  19078. // back to the prototype version of getValue / setValue
  19079. // note: avoiding to mutate the shape of 'this' via 'delete'
  19080. this.getValue = this._getValue_unbound;
  19081. this.setValue = this._setValue_unbound;
  19082. }
  19083. }
  19084. PropertyBinding.Composite = Composite;
  19085. PropertyBinding.prototype.BindingType = {
  19086. Direct: 0,
  19087. EntireArray: 1,
  19088. ArrayElement: 2,
  19089. HasFromToArray: 3
  19090. };
  19091. PropertyBinding.prototype.Versioning = {
  19092. None: 0,
  19093. NeedsUpdate: 1,
  19094. MatrixWorldNeedsUpdate: 2
  19095. };
  19096. PropertyBinding.prototype.GetterByBindingType = [
  19097. PropertyBinding.prototype._getValue_direct,
  19098. PropertyBinding.prototype._getValue_array,
  19099. PropertyBinding.prototype._getValue_arrayElement,
  19100. PropertyBinding.prototype._getValue_toArray,
  19101. ];
  19102. PropertyBinding.prototype.SetterByBindingTypeAndVersioning = [
  19103. [
  19104. // Direct
  19105. PropertyBinding.prototype._setValue_direct,
  19106. PropertyBinding.prototype._setValue_direct_setNeedsUpdate,
  19107. PropertyBinding.prototype._setValue_direct_setMatrixWorldNeedsUpdate,
  19108. ], [
  19109. // EntireArray
  19110. PropertyBinding.prototype._setValue_array,
  19111. PropertyBinding.prototype._setValue_array_setNeedsUpdate,
  19112. PropertyBinding.prototype._setValue_array_setMatrixWorldNeedsUpdate,
  19113. ], [
  19114. // ArrayElement
  19115. PropertyBinding.prototype._setValue_arrayElement,
  19116. PropertyBinding.prototype._setValue_arrayElement_setNeedsUpdate,
  19117. PropertyBinding.prototype._setValue_arrayElement_setMatrixWorldNeedsUpdate,
  19118. ], [
  19119. // HasToFromArray
  19120. PropertyBinding.prototype._setValue_fromArray,
  19121. PropertyBinding.prototype._setValue_fromArray_setNeedsUpdate,
  19122. PropertyBinding.prototype._setValue_fromArray_setMatrixWorldNeedsUpdate,
  19123. ]
  19124. ];
  19125. /**
  19126. *
  19127. * A group of objects that receives a shared animation state.
  19128. *
  19129. * Usage:
  19130. *
  19131. * - Add objects you would otherwise pass as 'root' to the
  19132. * constructor or the .clipAction method of AnimationMixer.
  19133. *
  19134. * - Instead pass this object as 'root'.
  19135. *
  19136. * - You can also add and remove objects later when the mixer
  19137. * is running.
  19138. *
  19139. * Note:
  19140. *
  19141. * Objects of this class appear as one object to the mixer,
  19142. * so cache control of the individual objects must be done
  19143. * on the group.
  19144. *
  19145. * Limitation:
  19146. *
  19147. * - The animated properties must be compatible among the
  19148. * all objects in the group.
  19149. *
  19150. * - A single property can either be controlled through a
  19151. * target group or directly, but not both.
  19152. */
  19153. class AnimationObjectGroup {
  19154. constructor() {
  19155. this.isAnimationObjectGroup = true;
  19156. this.uuid = generateUUID();
  19157. // cached objects followed by the active ones
  19158. this._objects = Array.prototype.slice.call( arguments );
  19159. this.nCachedObjects_ = 0; // threshold
  19160. // note: read by PropertyBinding.Composite
  19161. const indices = {};
  19162. this._indicesByUUID = indices; // for bookkeeping
  19163. for ( let i = 0, n = arguments.length; i !== n; ++ i ) {
  19164. indices[ arguments[ i ].uuid ] = i;
  19165. }
  19166. this._paths = []; // inside: string
  19167. this._parsedPaths = []; // inside: { we don't care, here }
  19168. this._bindings = []; // inside: Array< PropertyBinding >
  19169. this._bindingsIndicesByPath = {}; // inside: indices in these arrays
  19170. const scope = this;
  19171. this.stats = {
  19172. objects: {
  19173. get total() {
  19174. return scope._objects.length;
  19175. },
  19176. get inUse() {
  19177. return this.total - scope.nCachedObjects_;
  19178. }
  19179. },
  19180. get bindingsPerObject() {
  19181. return scope._bindings.length;
  19182. }
  19183. };
  19184. }
  19185. add() {
  19186. const objects = this._objects,
  19187. indicesByUUID = this._indicesByUUID,
  19188. paths = this._paths,
  19189. parsedPaths = this._parsedPaths,
  19190. bindings = this._bindings,
  19191. nBindings = bindings.length;
  19192. let knownObject = undefined,
  19193. nObjects = objects.length,
  19194. nCachedObjects = this.nCachedObjects_;
  19195. for ( let i = 0, n = arguments.length; i !== n; ++ i ) {
  19196. const object = arguments[ i ],
  19197. uuid = object.uuid;
  19198. let index = indicesByUUID[ uuid ];
  19199. if ( index === undefined ) {
  19200. // unknown object -> add it to the ACTIVE region
  19201. index = nObjects ++;
  19202. indicesByUUID[ uuid ] = index;
  19203. objects.push( object );
  19204. // accounting is done, now do the same for all bindings
  19205. for ( let j = 0, m = nBindings; j !== m; ++ j ) {
  19206. bindings[ j ].push( new PropertyBinding( object, paths[ j ], parsedPaths[ j ] ) );
  19207. }
  19208. } else if ( index < nCachedObjects ) {
  19209. knownObject = objects[ index ];
  19210. // move existing object to the ACTIVE region
  19211. const firstActiveIndex = -- nCachedObjects,
  19212. lastCachedObject = objects[ firstActiveIndex ];
  19213. indicesByUUID[ lastCachedObject.uuid ] = index;
  19214. objects[ index ] = lastCachedObject;
  19215. indicesByUUID[ uuid ] = firstActiveIndex;
  19216. objects[ firstActiveIndex ] = object;
  19217. // accounting is done, now do the same for all bindings
  19218. for ( let j = 0, m = nBindings; j !== m; ++ j ) {
  19219. const bindingsForPath = bindings[ j ],
  19220. lastCached = bindingsForPath[ firstActiveIndex ];
  19221. let binding = bindingsForPath[ index ];
  19222. bindingsForPath[ index ] = lastCached;
  19223. if ( binding === undefined ) {
  19224. // since we do not bother to create new bindings
  19225. // for objects that are cached, the binding may
  19226. // or may not exist
  19227. binding = new PropertyBinding( object, paths[ j ], parsedPaths[ j ] );
  19228. }
  19229. bindingsForPath[ firstActiveIndex ] = binding;
  19230. }
  19231. } else if ( objects[ index ] !== knownObject ) {
  19232. console.error( 'THREE.AnimationObjectGroup: Different objects with the same UUID ' +
  19233. 'detected. Clean the caches or recreate your infrastructure when reloading scenes.' );
  19234. } // else the object is already where we want it to be
  19235. } // for arguments
  19236. this.nCachedObjects_ = nCachedObjects;
  19237. }
  19238. remove() {
  19239. const objects = this._objects,
  19240. indicesByUUID = this._indicesByUUID,
  19241. bindings = this._bindings,
  19242. nBindings = bindings.length;
  19243. let nCachedObjects = this.nCachedObjects_;
  19244. for ( let i = 0, n = arguments.length; i !== n; ++ i ) {
  19245. const object = arguments[ i ],
  19246. uuid = object.uuid,
  19247. index = indicesByUUID[ uuid ];
  19248. if ( index !== undefined && index >= nCachedObjects ) {
  19249. // move existing object into the CACHED region
  19250. const lastCachedIndex = nCachedObjects ++,
  19251. firstActiveObject = objects[ lastCachedIndex ];
  19252. indicesByUUID[ firstActiveObject.uuid ] = index;
  19253. objects[ index ] = firstActiveObject;
  19254. indicesByUUID[ uuid ] = lastCachedIndex;
  19255. objects[ lastCachedIndex ] = object;
  19256. // accounting is done, now do the same for all bindings
  19257. for ( let j = 0, m = nBindings; j !== m; ++ j ) {
  19258. const bindingsForPath = bindings[ j ],
  19259. firstActive = bindingsForPath[ lastCachedIndex ],
  19260. binding = bindingsForPath[ index ];
  19261. bindingsForPath[ index ] = firstActive;
  19262. bindingsForPath[ lastCachedIndex ] = binding;
  19263. }
  19264. }
  19265. } // for arguments
  19266. this.nCachedObjects_ = nCachedObjects;
  19267. }
  19268. // remove & forget
  19269. uncache() {
  19270. const objects = this._objects,
  19271. indicesByUUID = this._indicesByUUID,
  19272. bindings = this._bindings,
  19273. nBindings = bindings.length;
  19274. let nCachedObjects = this.nCachedObjects_,
  19275. nObjects = objects.length;
  19276. for ( let i = 0, n = arguments.length; i !== n; ++ i ) {
  19277. const object = arguments[ i ],
  19278. uuid = object.uuid,
  19279. index = indicesByUUID[ uuid ];
  19280. if ( index !== undefined ) {
  19281. delete indicesByUUID[ uuid ];
  19282. if ( index < nCachedObjects ) {
  19283. // object is cached, shrink the CACHED region
  19284. const firstActiveIndex = -- nCachedObjects,
  19285. lastCachedObject = objects[ firstActiveIndex ],
  19286. lastIndex = -- nObjects,
  19287. lastObject = objects[ lastIndex ];
  19288. // last cached object takes this object's place
  19289. indicesByUUID[ lastCachedObject.uuid ] = index;
  19290. objects[ index ] = lastCachedObject;
  19291. // last object goes to the activated slot and pop
  19292. indicesByUUID[ lastObject.uuid ] = firstActiveIndex;
  19293. objects[ firstActiveIndex ] = lastObject;
  19294. objects.pop();
  19295. // accounting is done, now do the same for all bindings
  19296. for ( let j = 0, m = nBindings; j !== m; ++ j ) {
  19297. const bindingsForPath = bindings[ j ],
  19298. lastCached = bindingsForPath[ firstActiveIndex ],
  19299. last = bindingsForPath[ lastIndex ];
  19300. bindingsForPath[ index ] = lastCached;
  19301. bindingsForPath[ firstActiveIndex ] = last;
  19302. bindingsForPath.pop();
  19303. }
  19304. } else {
  19305. // object is active, just swap with the last and pop
  19306. const lastIndex = -- nObjects,
  19307. lastObject = objects[ lastIndex ];
  19308. if ( lastIndex > 0 ) {
  19309. indicesByUUID[ lastObject.uuid ] = index;
  19310. }
  19311. objects[ index ] = lastObject;
  19312. objects.pop();
  19313. // accounting is done, now do the same for all bindings
  19314. for ( let j = 0, m = nBindings; j !== m; ++ j ) {
  19315. const bindingsForPath = bindings[ j ];
  19316. bindingsForPath[ index ] = bindingsForPath[ lastIndex ];
  19317. bindingsForPath.pop();
  19318. }
  19319. } // cached or active
  19320. } // if object is known
  19321. } // for arguments
  19322. this.nCachedObjects_ = nCachedObjects;
  19323. }
  19324. // Internal interface used by befriended PropertyBinding.Composite:
  19325. subscribe_( path, parsedPath ) {
  19326. // returns an array of bindings for the given path that is changed
  19327. // according to the contained objects in the group
  19328. const indicesByPath = this._bindingsIndicesByPath;
  19329. let index = indicesByPath[ path ];
  19330. const bindings = this._bindings;
  19331. if ( index !== undefined ) return bindings[ index ];
  19332. const paths = this._paths,
  19333. parsedPaths = this._parsedPaths,
  19334. objects = this._objects,
  19335. nObjects = objects.length,
  19336. nCachedObjects = this.nCachedObjects_,
  19337. bindingsForPath = new Array( nObjects );
  19338. index = bindings.length;
  19339. indicesByPath[ path ] = index;
  19340. paths.push( path );
  19341. parsedPaths.push( parsedPath );
  19342. bindings.push( bindingsForPath );
  19343. for ( let i = nCachedObjects, n = objects.length; i !== n; ++ i ) {
  19344. const object = objects[ i ];
  19345. bindingsForPath[ i ] = new PropertyBinding( object, path, parsedPath );
  19346. }
  19347. return bindingsForPath;
  19348. }
  19349. unsubscribe_( path ) {
  19350. // tells the group to forget about a property path and no longer
  19351. // update the array previously obtained with 'subscribe_'
  19352. const indicesByPath = this._bindingsIndicesByPath,
  19353. index = indicesByPath[ path ];
  19354. if ( index !== undefined ) {
  19355. const paths = this._paths,
  19356. parsedPaths = this._parsedPaths,
  19357. bindings = this._bindings,
  19358. lastBindingsIndex = bindings.length - 1,
  19359. lastBindings = bindings[ lastBindingsIndex ],
  19360. lastBindingsPath = path[ lastBindingsIndex ];
  19361. indicesByPath[ lastBindingsPath ] = index;
  19362. bindings[ index ] = lastBindings;
  19363. bindings.pop();
  19364. parsedPaths[ index ] = parsedPaths[ lastBindingsIndex ];
  19365. parsedPaths.pop();
  19366. paths[ index ] = paths[ lastBindingsIndex ];
  19367. paths.pop();
  19368. }
  19369. }
  19370. }
  19371. class AnimationAction {
  19372. constructor( mixer, clip, localRoot = null, blendMode = clip.blendMode ) {
  19373. this._mixer = mixer;
  19374. this._clip = clip;
  19375. this._localRoot = localRoot;
  19376. this.blendMode = blendMode;
  19377. const tracks = clip.tracks,
  19378. nTracks = tracks.length,
  19379. interpolants = new Array( nTracks );
  19380. const interpolantSettings = {
  19381. endingStart: ZeroCurvatureEnding,
  19382. endingEnd: ZeroCurvatureEnding
  19383. };
  19384. for ( let i = 0; i !== nTracks; ++ i ) {
  19385. const interpolant = tracks[ i ].createInterpolant( null );
  19386. interpolants[ i ] = interpolant;
  19387. interpolant.settings = interpolantSettings;
  19388. }
  19389. this._interpolantSettings = interpolantSettings;
  19390. this._interpolants = interpolants; // bound by the mixer
  19391. // inside: PropertyMixer (managed by the mixer)
  19392. this._propertyBindings = new Array( nTracks );
  19393. this._cacheIndex = null; // for the memory manager
  19394. this._byClipCacheIndex = null; // for the memory manager
  19395. this._timeScaleInterpolant = null;
  19396. this._weightInterpolant = null;
  19397. this.loop = LoopRepeat;
  19398. this._loopCount = - 1;
  19399. // global mixer time when the action is to be started
  19400. // it's set back to 'null' upon start of the action
  19401. this._startTime = null;
  19402. // scaled local time of the action
  19403. // gets clamped or wrapped to 0..clip.duration according to loop
  19404. this.time = 0;
  19405. this.timeScale = 1;
  19406. this._effectiveTimeScale = 1;
  19407. this.weight = 1;
  19408. this._effectiveWeight = 1;
  19409. this.repetitions = Infinity; // no. of repetitions when looping
  19410. this.paused = false; // true -> zero effective time scale
  19411. this.enabled = true; // false -> zero effective weight
  19412. this.clampWhenFinished = false;// keep feeding the last frame?
  19413. this.zeroSlopeAtStart = true;// for smooth interpolation w/o separate
  19414. this.zeroSlopeAtEnd = true;// clips for start, loop and end
  19415. }
  19416. // State & Scheduling
  19417. play() {
  19418. this._mixer._activateAction( this );
  19419. return this;
  19420. }
  19421. stop() {
  19422. this._mixer._deactivateAction( this );
  19423. return this.reset();
  19424. }
  19425. reset() {
  19426. this.paused = false;
  19427. this.enabled = true;
  19428. this.time = 0; // restart clip
  19429. this._loopCount = - 1;// forget previous loops
  19430. this._startTime = null;// forget scheduling
  19431. return this.stopFading().stopWarping();
  19432. }
  19433. isRunning() {
  19434. return this.enabled && ! this.paused && this.timeScale !== 0 &&
  19435. this._startTime === null && this._mixer._isActiveAction( this );
  19436. }
  19437. // return true when play has been called
  19438. isScheduled() {
  19439. return this._mixer._isActiveAction( this );
  19440. }
  19441. startAt( time ) {
  19442. this._startTime = time;
  19443. return this;
  19444. }
  19445. setLoop( mode, repetitions ) {
  19446. this.loop = mode;
  19447. this.repetitions = repetitions;
  19448. return this;
  19449. }
  19450. // Weight
  19451. // set the weight stopping any scheduled fading
  19452. // although .enabled = false yields an effective weight of zero, this
  19453. // method does *not* change .enabled, because it would be confusing
  19454. setEffectiveWeight( weight ) {
  19455. this.weight = weight;
  19456. // note: same logic as when updated at runtime
  19457. this._effectiveWeight = this.enabled ? weight : 0;
  19458. return this.stopFading();
  19459. }
  19460. // return the weight considering fading and .enabled
  19461. getEffectiveWeight() {
  19462. return this._effectiveWeight;
  19463. }
  19464. fadeIn( duration ) {
  19465. return this._scheduleFading( duration, 0, 1 );
  19466. }
  19467. fadeOut( duration ) {
  19468. return this._scheduleFading( duration, 1, 0 );
  19469. }
  19470. crossFadeFrom( fadeOutAction, duration, warp ) {
  19471. fadeOutAction.fadeOut( duration );
  19472. this.fadeIn( duration );
  19473. if ( warp ) {
  19474. const fadeInDuration = this._clip.duration,
  19475. fadeOutDuration = fadeOutAction._clip.duration,
  19476. startEndRatio = fadeOutDuration / fadeInDuration,
  19477. endStartRatio = fadeInDuration / fadeOutDuration;
  19478. fadeOutAction.warp( 1.0, startEndRatio, duration );
  19479. this.warp( endStartRatio, 1.0, duration );
  19480. }
  19481. return this;
  19482. }
  19483. crossFadeTo( fadeInAction, duration, warp ) {
  19484. return fadeInAction.crossFadeFrom( this, duration, warp );
  19485. }
  19486. stopFading() {
  19487. const weightInterpolant = this._weightInterpolant;
  19488. if ( weightInterpolant !== null ) {
  19489. this._weightInterpolant = null;
  19490. this._mixer._takeBackControlInterpolant( weightInterpolant );
  19491. }
  19492. return this;
  19493. }
  19494. // Time Scale Control
  19495. // set the time scale stopping any scheduled warping
  19496. // although .paused = true yields an effective time scale of zero, this
  19497. // method does *not* change .paused, because it would be confusing
  19498. setEffectiveTimeScale( timeScale ) {
  19499. this.timeScale = timeScale;
  19500. this._effectiveTimeScale = this.paused ? 0 : timeScale;
  19501. return this.stopWarping();
  19502. }
  19503. // return the time scale considering warping and .paused
  19504. getEffectiveTimeScale() {
  19505. return this._effectiveTimeScale;
  19506. }
  19507. setDuration( duration ) {
  19508. this.timeScale = this._clip.duration / duration;
  19509. return this.stopWarping();
  19510. }
  19511. syncWith( action ) {
  19512. this.time = action.time;
  19513. this.timeScale = action.timeScale;
  19514. return this.stopWarping();
  19515. }
  19516. halt( duration ) {
  19517. return this.warp( this._effectiveTimeScale, 0, duration );
  19518. }
  19519. warp( startTimeScale, endTimeScale, duration ) {
  19520. const mixer = this._mixer,
  19521. now = mixer.time,
  19522. timeScale = this.timeScale;
  19523. let interpolant = this._timeScaleInterpolant;
  19524. if ( interpolant === null ) {
  19525. interpolant = mixer._lendControlInterpolant();
  19526. this._timeScaleInterpolant = interpolant;
  19527. }
  19528. const times = interpolant.parameterPositions,
  19529. values = interpolant.sampleValues;
  19530. times[ 0 ] = now;
  19531. times[ 1 ] = now + duration;
  19532. values[ 0 ] = startTimeScale / timeScale;
  19533. values[ 1 ] = endTimeScale / timeScale;
  19534. return this;
  19535. }
  19536. stopWarping() {
  19537. const timeScaleInterpolant = this._timeScaleInterpolant;
  19538. if ( timeScaleInterpolant !== null ) {
  19539. this._timeScaleInterpolant = null;
  19540. this._mixer._takeBackControlInterpolant( timeScaleInterpolant );
  19541. }
  19542. return this;
  19543. }
  19544. // Object Accessors
  19545. getMixer() {
  19546. return this._mixer;
  19547. }
  19548. getClip() {
  19549. return this._clip;
  19550. }
  19551. getRoot() {
  19552. return this._localRoot || this._mixer._root;
  19553. }
  19554. // Interna
  19555. _update( time, deltaTime, timeDirection, accuIndex ) {
  19556. // called by the mixer
  19557. if ( ! this.enabled ) {
  19558. // call ._updateWeight() to update ._effectiveWeight
  19559. this._updateWeight( time );
  19560. return;
  19561. }
  19562. const startTime = this._startTime;
  19563. if ( startTime !== null ) {
  19564. // check for scheduled start of action
  19565. const timeRunning = ( time - startTime ) * timeDirection;
  19566. if ( timeRunning < 0 || timeDirection === 0 ) {
  19567. deltaTime = 0;
  19568. } else {
  19569. this._startTime = null; // unschedule
  19570. deltaTime = timeDirection * timeRunning;
  19571. }
  19572. }
  19573. // apply time scale and advance time
  19574. deltaTime *= this._updateTimeScale( time );
  19575. const clipTime = this._updateTime( deltaTime );
  19576. // note: _updateTime may disable the action resulting in
  19577. // an effective weight of 0
  19578. const weight = this._updateWeight( time );
  19579. if ( weight > 0 ) {
  19580. const interpolants = this._interpolants;
  19581. const propertyMixers = this._propertyBindings;
  19582. switch ( this.blendMode ) {
  19583. case AdditiveAnimationBlendMode:
  19584. for ( let j = 0, m = interpolants.length; j !== m; ++ j ) {
  19585. interpolants[ j ].evaluate( clipTime );
  19586. propertyMixers[ j ].accumulateAdditive( weight );
  19587. }
  19588. break;
  19589. case NormalAnimationBlendMode:
  19590. default:
  19591. for ( let j = 0, m = interpolants.length; j !== m; ++ j ) {
  19592. interpolants[ j ].evaluate( clipTime );
  19593. propertyMixers[ j ].accumulate( accuIndex, weight );
  19594. }
  19595. }
  19596. }
  19597. }
  19598. _updateWeight( time ) {
  19599. let weight = 0;
  19600. if ( this.enabled ) {
  19601. weight = this.weight;
  19602. const interpolant = this._weightInterpolant;
  19603. if ( interpolant !== null ) {
  19604. const interpolantValue = interpolant.evaluate( time )[ 0 ];
  19605. weight *= interpolantValue;
  19606. if ( time > interpolant.parameterPositions[ 1 ] ) {
  19607. this.stopFading();
  19608. if ( interpolantValue === 0 ) {
  19609. // faded out, disable
  19610. this.enabled = false;
  19611. }
  19612. }
  19613. }
  19614. }
  19615. this._effectiveWeight = weight;
  19616. return weight;
  19617. }
  19618. _updateTimeScale( time ) {
  19619. let timeScale = 0;
  19620. if ( ! this.paused ) {
  19621. timeScale = this.timeScale;
  19622. const interpolant = this._timeScaleInterpolant;
  19623. if ( interpolant !== null ) {
  19624. const interpolantValue = interpolant.evaluate( time )[ 0 ];
  19625. timeScale *= interpolantValue;
  19626. if ( time > interpolant.parameterPositions[ 1 ] ) {
  19627. this.stopWarping();
  19628. if ( timeScale === 0 ) {
  19629. // motion has halted, pause
  19630. this.paused = true;
  19631. } else {
  19632. // warp done - apply final time scale
  19633. this.timeScale = timeScale;
  19634. }
  19635. }
  19636. }
  19637. }
  19638. this._effectiveTimeScale = timeScale;
  19639. return timeScale;
  19640. }
  19641. _updateTime( deltaTime ) {
  19642. const duration = this._clip.duration;
  19643. const loop = this.loop;
  19644. let time = this.time + deltaTime;
  19645. let loopCount = this._loopCount;
  19646. const pingPong = ( loop === LoopPingPong );
  19647. if ( deltaTime === 0 ) {
  19648. if ( loopCount === - 1 ) return time;
  19649. return ( pingPong && ( loopCount & 1 ) === 1 ) ? duration - time : time;
  19650. }
  19651. if ( loop === LoopOnce ) {
  19652. if ( loopCount === - 1 ) {
  19653. // just started
  19654. this._loopCount = 0;
  19655. this._setEndings( true, true, false );
  19656. }
  19657. handle_stop: {
  19658. if ( time >= duration ) {
  19659. time = duration;
  19660. } else if ( time < 0 ) {
  19661. time = 0;
  19662. } else {
  19663. this.time = time;
  19664. break handle_stop;
  19665. }
  19666. if ( this.clampWhenFinished ) this.paused = true;
  19667. else this.enabled = false;
  19668. this.time = time;
  19669. this._mixer.dispatchEvent( {
  19670. type: 'finished', action: this,
  19671. direction: deltaTime < 0 ? - 1 : 1
  19672. } );
  19673. }
  19674. } else { // repetitive Repeat or PingPong
  19675. if ( loopCount === - 1 ) {
  19676. // just started
  19677. if ( deltaTime >= 0 ) {
  19678. loopCount = 0;
  19679. this._setEndings( true, this.repetitions === 0, pingPong );
  19680. } else {
  19681. // when looping in reverse direction, the initial
  19682. // transition through zero counts as a repetition,
  19683. // so leave loopCount at -1
  19684. this._setEndings( this.repetitions === 0, true, pingPong );
  19685. }
  19686. }
  19687. if ( time >= duration || time < 0 ) {
  19688. // wrap around
  19689. const loopDelta = Math.floor( time / duration ); // signed
  19690. time -= duration * loopDelta;
  19691. loopCount += Math.abs( loopDelta );
  19692. const pending = this.repetitions - loopCount;
  19693. if ( pending <= 0 ) {
  19694. // have to stop (switch state, clamp time, fire event)
  19695. if ( this.clampWhenFinished ) this.paused = true;
  19696. else this.enabled = false;
  19697. time = deltaTime > 0 ? duration : 0;
  19698. this.time = time;
  19699. this._mixer.dispatchEvent( {
  19700. type: 'finished', action: this,
  19701. direction: deltaTime > 0 ? 1 : - 1
  19702. } );
  19703. } else {
  19704. // keep running
  19705. if ( pending === 1 ) {
  19706. // entering the last round
  19707. const atStart = deltaTime < 0;
  19708. this._setEndings( atStart, ! atStart, pingPong );
  19709. } else {
  19710. this._setEndings( false, false, pingPong );
  19711. }
  19712. this._loopCount = loopCount;
  19713. this.time = time;
  19714. this._mixer.dispatchEvent( {
  19715. type: 'loop', action: this, loopDelta: loopDelta
  19716. } );
  19717. }
  19718. } else {
  19719. this.time = time;
  19720. }
  19721. if ( pingPong && ( loopCount & 1 ) === 1 ) {
  19722. // invert time for the "pong round"
  19723. return duration - time;
  19724. }
  19725. }
  19726. return time;
  19727. }
  19728. _setEndings( atStart, atEnd, pingPong ) {
  19729. const settings = this._interpolantSettings;
  19730. if ( pingPong ) {
  19731. settings.endingStart = ZeroSlopeEnding;
  19732. settings.endingEnd = ZeroSlopeEnding;
  19733. } else {
  19734. // assuming for LoopOnce atStart == atEnd == true
  19735. if ( atStart ) {
  19736. settings.endingStart = this.zeroSlopeAtStart ? ZeroSlopeEnding : ZeroCurvatureEnding;
  19737. } else {
  19738. settings.endingStart = WrapAroundEnding;
  19739. }
  19740. if ( atEnd ) {
  19741. settings.endingEnd = this.zeroSlopeAtEnd ? ZeroSlopeEnding : ZeroCurvatureEnding;
  19742. } else {
  19743. settings.endingEnd = WrapAroundEnding;
  19744. }
  19745. }
  19746. }
  19747. _scheduleFading( duration, weightNow, weightThen ) {
  19748. const mixer = this._mixer, now = mixer.time;
  19749. let interpolant = this._weightInterpolant;
  19750. if ( interpolant === null ) {
  19751. interpolant = mixer._lendControlInterpolant();
  19752. this._weightInterpolant = interpolant;
  19753. }
  19754. const times = interpolant.parameterPositions,
  19755. values = interpolant.sampleValues;
  19756. times[ 0 ] = now;
  19757. values[ 0 ] = weightNow;
  19758. times[ 1 ] = now + duration;
  19759. values[ 1 ] = weightThen;
  19760. return this;
  19761. }
  19762. }
  19763. const _controlInterpolantsResultBuffer = new Float32Array( 1 );
  19764. class AnimationMixer extends EventDispatcher {
  19765. constructor( root ) {
  19766. super();
  19767. this._root = root;
  19768. this._initMemoryManager();
  19769. this._accuIndex = 0;
  19770. this.time = 0;
  19771. this.timeScale = 1.0;
  19772. }
  19773. _bindAction( action, prototypeAction ) {
  19774. const root = action._localRoot || this._root,
  19775. tracks = action._clip.tracks,
  19776. nTracks = tracks.length,
  19777. bindings = action._propertyBindings,
  19778. interpolants = action._interpolants,
  19779. rootUuid = root.uuid,
  19780. bindingsByRoot = this._bindingsByRootAndName;
  19781. let bindingsByName = bindingsByRoot[ rootUuid ];
  19782. if ( bindingsByName === undefined ) {
  19783. bindingsByName = {};
  19784. bindingsByRoot[ rootUuid ] = bindingsByName;
  19785. }
  19786. for ( let i = 0; i !== nTracks; ++ i ) {
  19787. const track = tracks[ i ],
  19788. trackName = track.name;
  19789. let binding = bindingsByName[ trackName ];
  19790. if ( binding !== undefined ) {
  19791. ++ binding.referenceCount;
  19792. bindings[ i ] = binding;
  19793. } else {
  19794. binding = bindings[ i ];
  19795. if ( binding !== undefined ) {
  19796. // existing binding, make sure the cache knows
  19797. if ( binding._cacheIndex === null ) {
  19798. ++ binding.referenceCount;
  19799. this._addInactiveBinding( binding, rootUuid, trackName );
  19800. }
  19801. continue;
  19802. }
  19803. const path = prototypeAction && prototypeAction.
  19804. _propertyBindings[ i ].binding.parsedPath;
  19805. binding = new PropertyMixer(
  19806. PropertyBinding.create( root, trackName, path ),
  19807. track.ValueTypeName, track.getValueSize() );
  19808. ++ binding.referenceCount;
  19809. this._addInactiveBinding( binding, rootUuid, trackName );
  19810. bindings[ i ] = binding;
  19811. }
  19812. interpolants[ i ].resultBuffer = binding.buffer;
  19813. }
  19814. }
  19815. _activateAction( action ) {
  19816. if ( ! this._isActiveAction( action ) ) {
  19817. if ( action._cacheIndex === null ) {
  19818. // this action has been forgotten by the cache, but the user
  19819. // appears to be still using it -> rebind
  19820. const rootUuid = ( action._localRoot || this._root ).uuid,
  19821. clipUuid = action._clip.uuid,
  19822. actionsForClip = this._actionsByClip[ clipUuid ];
  19823. this._bindAction( action,
  19824. actionsForClip && actionsForClip.knownActions[ 0 ] );
  19825. this._addInactiveAction( action, clipUuid, rootUuid );
  19826. }
  19827. const bindings = action._propertyBindings;
  19828. // increment reference counts / sort out state
  19829. for ( let i = 0, n = bindings.length; i !== n; ++ i ) {
  19830. const binding = bindings[ i ];
  19831. if ( binding.useCount ++ === 0 ) {
  19832. this._lendBinding( binding );
  19833. binding.saveOriginalState();
  19834. }
  19835. }
  19836. this._lendAction( action );
  19837. }
  19838. }
  19839. _deactivateAction( action ) {
  19840. if ( this._isActiveAction( action ) ) {
  19841. const bindings = action._propertyBindings;
  19842. // decrement reference counts / sort out state
  19843. for ( let i = 0, n = bindings.length; i !== n; ++ i ) {
  19844. const binding = bindings[ i ];
  19845. if ( -- binding.useCount === 0 ) {
  19846. binding.restoreOriginalState();
  19847. this._takeBackBinding( binding );
  19848. }
  19849. }
  19850. this._takeBackAction( action );
  19851. }
  19852. }
  19853. // Memory manager
  19854. _initMemoryManager() {
  19855. this._actions = []; // 'nActiveActions' followed by inactive ones
  19856. this._nActiveActions = 0;
  19857. this._actionsByClip = {};
  19858. // inside:
  19859. // {
  19860. // knownActions: Array< AnimationAction > - used as prototypes
  19861. // actionByRoot: AnimationAction - lookup
  19862. // }
  19863. this._bindings = []; // 'nActiveBindings' followed by inactive ones
  19864. this._nActiveBindings = 0;
  19865. this._bindingsByRootAndName = {}; // inside: Map< name, PropertyMixer >
  19866. this._controlInterpolants = []; // same game as above
  19867. this._nActiveControlInterpolants = 0;
  19868. const scope = this;
  19869. this.stats = {
  19870. actions: {
  19871. get total() {
  19872. return scope._actions.length;
  19873. },
  19874. get inUse() {
  19875. return scope._nActiveActions;
  19876. }
  19877. },
  19878. bindings: {
  19879. get total() {
  19880. return scope._bindings.length;
  19881. },
  19882. get inUse() {
  19883. return scope._nActiveBindings;
  19884. }
  19885. },
  19886. controlInterpolants: {
  19887. get total() {
  19888. return scope._controlInterpolants.length;
  19889. },
  19890. get inUse() {
  19891. return scope._nActiveControlInterpolants;
  19892. }
  19893. }
  19894. };
  19895. }
  19896. // Memory management for AnimationAction objects
  19897. _isActiveAction( action ) {
  19898. const index = action._cacheIndex;
  19899. return index !== null && index < this._nActiveActions;
  19900. }
  19901. _addInactiveAction( action, clipUuid, rootUuid ) {
  19902. const actions = this._actions,
  19903. actionsByClip = this._actionsByClip;
  19904. let actionsForClip = actionsByClip[ clipUuid ];
  19905. if ( actionsForClip === undefined ) {
  19906. actionsForClip = {
  19907. knownActions: [ action ],
  19908. actionByRoot: {}
  19909. };
  19910. action._byClipCacheIndex = 0;
  19911. actionsByClip[ clipUuid ] = actionsForClip;
  19912. } else {
  19913. const knownActions = actionsForClip.knownActions;
  19914. action._byClipCacheIndex = knownActions.length;
  19915. knownActions.push( action );
  19916. }
  19917. action._cacheIndex = actions.length;
  19918. actions.push( action );
  19919. actionsForClip.actionByRoot[ rootUuid ] = action;
  19920. }
  19921. _removeInactiveAction( action ) {
  19922. const actions = this._actions,
  19923. lastInactiveAction = actions[ actions.length - 1 ],
  19924. cacheIndex = action._cacheIndex;
  19925. lastInactiveAction._cacheIndex = cacheIndex;
  19926. actions[ cacheIndex ] = lastInactiveAction;
  19927. actions.pop();
  19928. action._cacheIndex = null;
  19929. const clipUuid = action._clip.uuid,
  19930. actionsByClip = this._actionsByClip,
  19931. actionsForClip = actionsByClip[ clipUuid ],
  19932. knownActionsForClip = actionsForClip.knownActions,
  19933. lastKnownAction =
  19934. knownActionsForClip[ knownActionsForClip.length - 1 ],
  19935. byClipCacheIndex = action._byClipCacheIndex;
  19936. lastKnownAction._byClipCacheIndex = byClipCacheIndex;
  19937. knownActionsForClip[ byClipCacheIndex ] = lastKnownAction;
  19938. knownActionsForClip.pop();
  19939. action._byClipCacheIndex = null;
  19940. const actionByRoot = actionsForClip.actionByRoot,
  19941. rootUuid = ( action._localRoot || this._root ).uuid;
  19942. delete actionByRoot[ rootUuid ];
  19943. if ( knownActionsForClip.length === 0 ) {
  19944. delete actionsByClip[ clipUuid ];
  19945. }
  19946. this._removeInactiveBindingsForAction( action );
  19947. }
  19948. _removeInactiveBindingsForAction( action ) {
  19949. const bindings = action._propertyBindings;
  19950. for ( let i = 0, n = bindings.length; i !== n; ++ i ) {
  19951. const binding = bindings[ i ];
  19952. if ( -- binding.referenceCount === 0 ) {
  19953. this._removeInactiveBinding( binding );
  19954. }
  19955. }
  19956. }
  19957. _lendAction( action ) {
  19958. // [ active actions | inactive actions ]
  19959. // [ active actions >| inactive actions ]
  19960. // s a
  19961. // <-swap->
  19962. // a s
  19963. const actions = this._actions,
  19964. prevIndex = action._cacheIndex,
  19965. lastActiveIndex = this._nActiveActions ++,
  19966. firstInactiveAction = actions[ lastActiveIndex ];
  19967. action._cacheIndex = lastActiveIndex;
  19968. actions[ lastActiveIndex ] = action;
  19969. firstInactiveAction._cacheIndex = prevIndex;
  19970. actions[ prevIndex ] = firstInactiveAction;
  19971. }
  19972. _takeBackAction( action ) {
  19973. // [ active actions | inactive actions ]
  19974. // [ active actions |< inactive actions ]
  19975. // a s
  19976. // <-swap->
  19977. // s a
  19978. const actions = this._actions,
  19979. prevIndex = action._cacheIndex,
  19980. firstInactiveIndex = -- this._nActiveActions,
  19981. lastActiveAction = actions[ firstInactiveIndex ];
  19982. action._cacheIndex = firstInactiveIndex;
  19983. actions[ firstInactiveIndex ] = action;
  19984. lastActiveAction._cacheIndex = prevIndex;
  19985. actions[ prevIndex ] = lastActiveAction;
  19986. }
  19987. // Memory management for PropertyMixer objects
  19988. _addInactiveBinding( binding, rootUuid, trackName ) {
  19989. const bindingsByRoot = this._bindingsByRootAndName,
  19990. bindings = this._bindings;
  19991. let bindingByName = bindingsByRoot[ rootUuid ];
  19992. if ( bindingByName === undefined ) {
  19993. bindingByName = {};
  19994. bindingsByRoot[ rootUuid ] = bindingByName;
  19995. }
  19996. bindingByName[ trackName ] = binding;
  19997. binding._cacheIndex = bindings.length;
  19998. bindings.push( binding );
  19999. }
  20000. _removeInactiveBinding( binding ) {
  20001. const bindings = this._bindings,
  20002. propBinding = binding.binding,
  20003. rootUuid = propBinding.rootNode.uuid,
  20004. trackName = propBinding.path,
  20005. bindingsByRoot = this._bindingsByRootAndName,
  20006. bindingByName = bindingsByRoot[ rootUuid ],
  20007. lastInactiveBinding = bindings[ bindings.length - 1 ],
  20008. cacheIndex = binding._cacheIndex;
  20009. lastInactiveBinding._cacheIndex = cacheIndex;
  20010. bindings[ cacheIndex ] = lastInactiveBinding;
  20011. bindings.pop();
  20012. delete bindingByName[ trackName ];
  20013. if ( Object.keys( bindingByName ).length === 0 ) {
  20014. delete bindingsByRoot[ rootUuid ];
  20015. }
  20016. }
  20017. _lendBinding( binding ) {
  20018. const bindings = this._bindings,
  20019. prevIndex = binding._cacheIndex,
  20020. lastActiveIndex = this._nActiveBindings ++,
  20021. firstInactiveBinding = bindings[ lastActiveIndex ];
  20022. binding._cacheIndex = lastActiveIndex;
  20023. bindings[ lastActiveIndex ] = binding;
  20024. firstInactiveBinding._cacheIndex = prevIndex;
  20025. bindings[ prevIndex ] = firstInactiveBinding;
  20026. }
  20027. _takeBackBinding( binding ) {
  20028. const bindings = this._bindings,
  20029. prevIndex = binding._cacheIndex,
  20030. firstInactiveIndex = -- this._nActiveBindings,
  20031. lastActiveBinding = bindings[ firstInactiveIndex ];
  20032. binding._cacheIndex = firstInactiveIndex;
  20033. bindings[ firstInactiveIndex ] = binding;
  20034. lastActiveBinding._cacheIndex = prevIndex;
  20035. bindings[ prevIndex ] = lastActiveBinding;
  20036. }
  20037. // Memory management of Interpolants for weight and time scale
  20038. _lendControlInterpolant() {
  20039. const interpolants = this._controlInterpolants,
  20040. lastActiveIndex = this._nActiveControlInterpolants ++;
  20041. let interpolant = interpolants[ lastActiveIndex ];
  20042. if ( interpolant === undefined ) {
  20043. interpolant = new LinearInterpolant(
  20044. new Float32Array( 2 ), new Float32Array( 2 ),
  20045. 1, _controlInterpolantsResultBuffer );
  20046. interpolant.__cacheIndex = lastActiveIndex;
  20047. interpolants[ lastActiveIndex ] = interpolant;
  20048. }
  20049. return interpolant;
  20050. }
  20051. _takeBackControlInterpolant( interpolant ) {
  20052. const interpolants = this._controlInterpolants,
  20053. prevIndex = interpolant.__cacheIndex,
  20054. firstInactiveIndex = -- this._nActiveControlInterpolants,
  20055. lastActiveInterpolant = interpolants[ firstInactiveIndex ];
  20056. interpolant.__cacheIndex = firstInactiveIndex;
  20057. interpolants[ firstInactiveIndex ] = interpolant;
  20058. lastActiveInterpolant.__cacheIndex = prevIndex;
  20059. interpolants[ prevIndex ] = lastActiveInterpolant;
  20060. }
  20061. // return an action for a clip optionally using a custom root target
  20062. // object (this method allocates a lot of dynamic memory in case a
  20063. // previously unknown clip/root combination is specified)
  20064. clipAction( clip, optionalRoot, blendMode ) {
  20065. const root = optionalRoot || this._root,
  20066. rootUuid = root.uuid;
  20067. let clipObject = typeof clip === 'string' ? AnimationClip.findByName( root, clip ) : clip;
  20068. const clipUuid = clipObject !== null ? clipObject.uuid : clip;
  20069. const actionsForClip = this._actionsByClip[ clipUuid ];
  20070. let prototypeAction = null;
  20071. if ( blendMode === undefined ) {
  20072. if ( clipObject !== null ) {
  20073. blendMode = clipObject.blendMode;
  20074. } else {
  20075. blendMode = NormalAnimationBlendMode;
  20076. }
  20077. }
  20078. if ( actionsForClip !== undefined ) {
  20079. const existingAction = actionsForClip.actionByRoot[ rootUuid ];
  20080. if ( existingAction !== undefined && existingAction.blendMode === blendMode ) {
  20081. return existingAction;
  20082. }
  20083. // we know the clip, so we don't have to parse all
  20084. // the bindings again but can just copy
  20085. prototypeAction = actionsForClip.knownActions[ 0 ];
  20086. // also, take the clip from the prototype action
  20087. if ( clipObject === null )
  20088. clipObject = prototypeAction._clip;
  20089. }
  20090. // clip must be known when specified via string
  20091. if ( clipObject === null ) return null;
  20092. // allocate all resources required to run it
  20093. const newAction = new AnimationAction( this, clipObject, optionalRoot, blendMode );
  20094. this._bindAction( newAction, prototypeAction );
  20095. // and make the action known to the memory manager
  20096. this._addInactiveAction( newAction, clipUuid, rootUuid );
  20097. return newAction;
  20098. }
  20099. // get an existing action
  20100. existingAction( clip, optionalRoot ) {
  20101. const root = optionalRoot || this._root,
  20102. rootUuid = root.uuid,
  20103. clipObject = typeof clip === 'string' ?
  20104. AnimationClip.findByName( root, clip ) : clip,
  20105. clipUuid = clipObject ? clipObject.uuid : clip,
  20106. actionsForClip = this._actionsByClip[ clipUuid ];
  20107. if ( actionsForClip !== undefined ) {
  20108. return actionsForClip.actionByRoot[ rootUuid ] || null;
  20109. }
  20110. return null;
  20111. }
  20112. // deactivates all previously scheduled actions
  20113. stopAllAction() {
  20114. const actions = this._actions,
  20115. nActions = this._nActiveActions;
  20116. for ( let i = nActions - 1; i >= 0; -- i ) {
  20117. actions[ i ].stop();
  20118. }
  20119. return this;
  20120. }
  20121. // advance the time and update apply the animation
  20122. update( deltaTime ) {
  20123. deltaTime *= this.timeScale;
  20124. const actions = this._actions,
  20125. nActions = this._nActiveActions,
  20126. time = this.time += deltaTime,
  20127. timeDirection = Math.sign( deltaTime ),
  20128. accuIndex = this._accuIndex ^= 1;
  20129. // run active actions
  20130. for ( let i = 0; i !== nActions; ++ i ) {
  20131. const action = actions[ i ];
  20132. action._update( time, deltaTime, timeDirection, accuIndex );
  20133. }
  20134. // update scene graph
  20135. const bindings = this._bindings,
  20136. nBindings = this._nActiveBindings;
  20137. for ( let i = 0; i !== nBindings; ++ i ) {
  20138. bindings[ i ].apply( accuIndex );
  20139. }
  20140. return this;
  20141. }
  20142. // Allows you to seek to a specific time in an animation.
  20143. setTime( timeInSeconds ) {
  20144. this.time = 0; // Zero out time attribute for AnimationMixer object;
  20145. for ( let i = 0; i < this._actions.length; i ++ ) {
  20146. this._actions[ i ].time = 0; // Zero out time attribute for all associated AnimationAction objects.
  20147. }
  20148. return this.update( timeInSeconds ); // Update used to set exact time. Returns "this" AnimationMixer object.
  20149. }
  20150. // return this mixer's root target object
  20151. getRoot() {
  20152. return this._root;
  20153. }
  20154. // free all resources specific to a particular clip
  20155. uncacheClip( clip ) {
  20156. const actions = this._actions,
  20157. clipUuid = clip.uuid,
  20158. actionsByClip = this._actionsByClip,
  20159. actionsForClip = actionsByClip[ clipUuid ];
  20160. if ( actionsForClip !== undefined ) {
  20161. // note: just calling _removeInactiveAction would mess up the
  20162. // iteration state and also require updating the state we can
  20163. // just throw away
  20164. const actionsToRemove = actionsForClip.knownActions;
  20165. for ( let i = 0, n = actionsToRemove.length; i !== n; ++ i ) {
  20166. const action = actionsToRemove[ i ];
  20167. this._deactivateAction( action );
  20168. const cacheIndex = action._cacheIndex,
  20169. lastInactiveAction = actions[ actions.length - 1 ];
  20170. action._cacheIndex = null;
  20171. action._byClipCacheIndex = null;
  20172. lastInactiveAction._cacheIndex = cacheIndex;
  20173. actions[ cacheIndex ] = lastInactiveAction;
  20174. actions.pop();
  20175. this._removeInactiveBindingsForAction( action );
  20176. }
  20177. delete actionsByClip[ clipUuid ];
  20178. }
  20179. }
  20180. // free all resources specific to a particular root target object
  20181. uncacheRoot( root ) {
  20182. const rootUuid = root.uuid,
  20183. actionsByClip = this._actionsByClip;
  20184. for ( const clipUuid in actionsByClip ) {
  20185. const actionByRoot = actionsByClip[ clipUuid ].actionByRoot,
  20186. action = actionByRoot[ rootUuid ];
  20187. if ( action !== undefined ) {
  20188. this._deactivateAction( action );
  20189. this._removeInactiveAction( action );
  20190. }
  20191. }
  20192. const bindingsByRoot = this._bindingsByRootAndName,
  20193. bindingByName = bindingsByRoot[ rootUuid ];
  20194. if ( bindingByName !== undefined ) {
  20195. for ( const trackName in bindingByName ) {
  20196. const binding = bindingByName[ trackName ];
  20197. binding.restoreOriginalState();
  20198. this._removeInactiveBinding( binding );
  20199. }
  20200. }
  20201. }
  20202. // remove a targeted clip from the cache
  20203. uncacheAction( clip, optionalRoot ) {
  20204. const action = this.existingAction( clip, optionalRoot );
  20205. if ( action !== null ) {
  20206. this._deactivateAction( action );
  20207. this._removeInactiveAction( action );
  20208. }
  20209. }
  20210. }
  20211. class Uniform {
  20212. constructor( value ) {
  20213. this.value = value;
  20214. }
  20215. clone() {
  20216. return new Uniform( this.value.clone === undefined ? this.value : this.value.clone() );
  20217. }
  20218. }
  20219. let _id = 0;
  20220. class UniformsGroup extends EventDispatcher {
  20221. constructor() {
  20222. super();
  20223. this.isUniformsGroup = true;
  20224. Object.defineProperty( this, 'id', { value: _id ++ } );
  20225. this.name = '';
  20226. this.usage = StaticDrawUsage;
  20227. this.uniforms = [];
  20228. }
  20229. add( uniform ) {
  20230. this.uniforms.push( uniform );
  20231. return this;
  20232. }
  20233. remove( uniform ) {
  20234. const index = this.uniforms.indexOf( uniform );
  20235. if ( index !== - 1 ) this.uniforms.splice( index, 1 );
  20236. return this;
  20237. }
  20238. setName( name ) {
  20239. this.name = name;
  20240. return this;
  20241. }
  20242. setUsage( value ) {
  20243. this.usage = value;
  20244. return this;
  20245. }
  20246. dispose() {
  20247. this.dispatchEvent( { type: 'dispose' } );
  20248. return this;
  20249. }
  20250. copy( source ) {
  20251. this.name = source.name;
  20252. this.usage = source.usage;
  20253. const uniformsSource = source.uniforms;
  20254. this.uniforms.length = 0;
  20255. for ( let i = 0, l = uniformsSource.length; i < l; i ++ ) {
  20256. const uniforms = Array.isArray( uniformsSource[ i ] ) ? uniformsSource[ i ] : [ uniformsSource[ i ] ];
  20257. for ( let j = 0; j < uniforms.length; j ++ ) {
  20258. this.uniforms.push( uniforms[ j ].clone() );
  20259. }
  20260. }
  20261. return this;
  20262. }
  20263. clone() {
  20264. return new this.constructor().copy( this );
  20265. }
  20266. }
  20267. class InstancedInterleavedBuffer extends InterleavedBuffer {
  20268. constructor( array, stride, meshPerAttribute = 1 ) {
  20269. super( array, stride );
  20270. this.isInstancedInterleavedBuffer = true;
  20271. this.meshPerAttribute = meshPerAttribute;
  20272. }
  20273. copy( source ) {
  20274. super.copy( source );
  20275. this.meshPerAttribute = source.meshPerAttribute;
  20276. return this;
  20277. }
  20278. clone( data ) {
  20279. const ib = super.clone( data );
  20280. ib.meshPerAttribute = this.meshPerAttribute;
  20281. return ib;
  20282. }
  20283. toJSON( data ) {
  20284. const json = super.toJSON( data );
  20285. json.isInstancedInterleavedBuffer = true;
  20286. json.meshPerAttribute = this.meshPerAttribute;
  20287. return json;
  20288. }
  20289. }
  20290. class GLBufferAttribute {
  20291. constructor( buffer, type, itemSize, elementSize, count ) {
  20292. this.isGLBufferAttribute = true;
  20293. this.name = '';
  20294. this.buffer = buffer;
  20295. this.type = type;
  20296. this.itemSize = itemSize;
  20297. this.elementSize = elementSize;
  20298. this.count = count;
  20299. this.version = 0;
  20300. }
  20301. set needsUpdate( value ) {
  20302. if ( value === true ) this.version ++;
  20303. }
  20304. setBuffer( buffer ) {
  20305. this.buffer = buffer;
  20306. return this;
  20307. }
  20308. setType( type, elementSize ) {
  20309. this.type = type;
  20310. this.elementSize = elementSize;
  20311. return this;
  20312. }
  20313. setItemSize( itemSize ) {
  20314. this.itemSize = itemSize;
  20315. return this;
  20316. }
  20317. setCount( count ) {
  20318. this.count = count;
  20319. return this;
  20320. }
  20321. }
  20322. const _matrix = /*@__PURE__*/ new Matrix4();
  20323. class Raycaster {
  20324. constructor( origin, direction, near = 0, far = Infinity ) {
  20325. this.ray = new Ray( origin, direction );
  20326. // direction is assumed to be normalized (for accurate distance calculations)
  20327. this.near = near;
  20328. this.far = far;
  20329. this.camera = null;
  20330. this.layers = new Layers();
  20331. this.params = {
  20332. Mesh: {},
  20333. Line: { threshold: 1 },
  20334. LOD: {},
  20335. Points: { threshold: 1 },
  20336. Sprite: {}
  20337. };
  20338. }
  20339. set( origin, direction ) {
  20340. // direction is assumed to be normalized (for accurate distance calculations)
  20341. this.ray.set( origin, direction );
  20342. }
  20343. setFromCamera( coords, camera ) {
  20344. if ( camera.isPerspectiveCamera ) {
  20345. this.ray.origin.setFromMatrixPosition( camera.matrixWorld );
  20346. this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize();
  20347. this.camera = camera;
  20348. } else if ( camera.isOrthographicCamera ) {
  20349. this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera
  20350. this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld );
  20351. this.camera = camera;
  20352. } else {
  20353. console.error( 'THREE.Raycaster: Unsupported camera type: ' + camera.type );
  20354. }
  20355. }
  20356. setFromXRController( controller ) {
  20357. _matrix.identity().extractRotation( controller.matrixWorld );
  20358. this.ray.origin.setFromMatrixPosition( controller.matrixWorld );
  20359. this.ray.direction.set( 0, 0, - 1 ).applyMatrix4( _matrix );
  20360. return this;
  20361. }
  20362. intersectObject( object, recursive = true, intersects = [] ) {
  20363. intersect( object, this, intersects, recursive );
  20364. intersects.sort( ascSort );
  20365. return intersects;
  20366. }
  20367. intersectObjects( objects, recursive = true, intersects = [] ) {
  20368. for ( let i = 0, l = objects.length; i < l; i ++ ) {
  20369. intersect( objects[ i ], this, intersects, recursive );
  20370. }
  20371. intersects.sort( ascSort );
  20372. return intersects;
  20373. }
  20374. }
  20375. function ascSort( a, b ) {
  20376. return a.distance - b.distance;
  20377. }
  20378. function intersect( object, raycaster, intersects, recursive ) {
  20379. let propagate = true;
  20380. if ( object.layers.test( raycaster.layers ) ) {
  20381. const result = object.raycast( raycaster, intersects );
  20382. if ( result === false ) propagate = false;
  20383. }
  20384. if ( propagate === true && recursive === true ) {
  20385. const children = object.children;
  20386. for ( let i = 0, l = children.length; i < l; i ++ ) {
  20387. intersect( children[ i ], raycaster, intersects, true );
  20388. }
  20389. }
  20390. }
  20391. /**
  20392. * Ref: https://en.wikipedia.org/wiki/Spherical_coordinate_system
  20393. *
  20394. * phi (the polar angle) is measured from the positive y-axis. The positive y-axis is up.
  20395. * theta (the azimuthal angle) is measured from the positive z-axis.
  20396. */
  20397. class Spherical {
  20398. constructor( radius = 1, phi = 0, theta = 0 ) {
  20399. this.radius = radius;
  20400. this.phi = phi; // polar angle
  20401. this.theta = theta; // azimuthal angle
  20402. return this;
  20403. }
  20404. set( radius, phi, theta ) {
  20405. this.radius = radius;
  20406. this.phi = phi;
  20407. this.theta = theta;
  20408. return this;
  20409. }
  20410. copy( other ) {
  20411. this.radius = other.radius;
  20412. this.phi = other.phi;
  20413. this.theta = other.theta;
  20414. return this;
  20415. }
  20416. // restrict phi to be between EPS and PI-EPS
  20417. makeSafe() {
  20418. const EPS = 0.000001;
  20419. this.phi = clamp( this.phi, EPS, Math.PI - EPS );
  20420. return this;
  20421. }
  20422. setFromVector3( v ) {
  20423. return this.setFromCartesianCoords( v.x, v.y, v.z );
  20424. }
  20425. setFromCartesianCoords( x, y, z ) {
  20426. this.radius = Math.sqrt( x * x + y * y + z * z );
  20427. if ( this.radius === 0 ) {
  20428. this.theta = 0;
  20429. this.phi = 0;
  20430. } else {
  20431. this.theta = Math.atan2( x, z );
  20432. this.phi = Math.acos( clamp( y / this.radius, - 1, 1 ) );
  20433. }
  20434. return this;
  20435. }
  20436. clone() {
  20437. return new this.constructor().copy( this );
  20438. }
  20439. }
  20440. /**
  20441. * Ref: https://en.wikipedia.org/wiki/Cylindrical_coordinate_system
  20442. */
  20443. class Cylindrical {
  20444. constructor( radius = 1, theta = 0, y = 0 ) {
  20445. this.radius = radius; // distance from the origin to a point in the x-z plane
  20446. this.theta = theta; // counterclockwise angle in the x-z plane measured in radians from the positive z-axis
  20447. this.y = y; // height above the x-z plane
  20448. return this;
  20449. }
  20450. set( radius, theta, y ) {
  20451. this.radius = radius;
  20452. this.theta = theta;
  20453. this.y = y;
  20454. return this;
  20455. }
  20456. copy( other ) {
  20457. this.radius = other.radius;
  20458. this.theta = other.theta;
  20459. this.y = other.y;
  20460. return this;
  20461. }
  20462. setFromVector3( v ) {
  20463. return this.setFromCartesianCoords( v.x, v.y, v.z );
  20464. }
  20465. setFromCartesianCoords( x, y, z ) {
  20466. this.radius = Math.sqrt( x * x + z * z );
  20467. this.theta = Math.atan2( x, z );
  20468. this.y = y;
  20469. return this;
  20470. }
  20471. clone() {
  20472. return new this.constructor().copy( this );
  20473. }
  20474. }
  20475. class Matrix2 {
  20476. constructor( n11, n12, n21, n22 ) {
  20477. Matrix2.prototype.isMatrix2 = true;
  20478. this.elements = [
  20479. 1, 0,
  20480. 0, 1,
  20481. ];
  20482. if ( n11 !== undefined ) {
  20483. this.set( n11, n12, n21, n22 );
  20484. }
  20485. }
  20486. identity() {
  20487. this.set(
  20488. 1, 0,
  20489. 0, 1,
  20490. );
  20491. return this;
  20492. }
  20493. fromArray( array, offset = 0 ) {
  20494. for ( let i = 0; i < 4; i ++ ) {
  20495. this.elements[ i ] = array[ i + offset ];
  20496. }
  20497. return this;
  20498. }
  20499. set( n11, n12, n21, n22 ) {
  20500. const te = this.elements;
  20501. te[ 0 ] = n11; te[ 2 ] = n12;
  20502. te[ 1 ] = n21; te[ 3 ] = n22;
  20503. return this;
  20504. }
  20505. }
  20506. const _vector$4 = /*@__PURE__*/ new Vector2();
  20507. class Box2 {
  20508. constructor( min = new Vector2( + Infinity, + Infinity ), max = new Vector2( - Infinity, - Infinity ) ) {
  20509. this.isBox2 = true;
  20510. this.min = min;
  20511. this.max = max;
  20512. }
  20513. set( min, max ) {
  20514. this.min.copy( min );
  20515. this.max.copy( max );
  20516. return this;
  20517. }
  20518. setFromPoints( points ) {
  20519. this.makeEmpty();
  20520. for ( let i = 0, il = points.length; i < il; i ++ ) {
  20521. this.expandByPoint( points[ i ] );
  20522. }
  20523. return this;
  20524. }
  20525. setFromCenterAndSize( center, size ) {
  20526. const halfSize = _vector$4.copy( size ).multiplyScalar( 0.5 );
  20527. this.min.copy( center ).sub( halfSize );
  20528. this.max.copy( center ).add( halfSize );
  20529. return this;
  20530. }
  20531. clone() {
  20532. return new this.constructor().copy( this );
  20533. }
  20534. copy( box ) {
  20535. this.min.copy( box.min );
  20536. this.max.copy( box.max );
  20537. return this;
  20538. }
  20539. makeEmpty() {
  20540. this.min.x = this.min.y = + Infinity;
  20541. this.max.x = this.max.y = - Infinity;
  20542. return this;
  20543. }
  20544. isEmpty() {
  20545. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  20546. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
  20547. }
  20548. getCenter( target ) {
  20549. return this.isEmpty() ? target.set( 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  20550. }
  20551. getSize( target ) {
  20552. return this.isEmpty() ? target.set( 0, 0 ) : target.subVectors( this.max, this.min );
  20553. }
  20554. expandByPoint( point ) {
  20555. this.min.min( point );
  20556. this.max.max( point );
  20557. return this;
  20558. }
  20559. expandByVector( vector ) {
  20560. this.min.sub( vector );
  20561. this.max.add( vector );
  20562. return this;
  20563. }
  20564. expandByScalar( scalar ) {
  20565. this.min.addScalar( - scalar );
  20566. this.max.addScalar( scalar );
  20567. return this;
  20568. }
  20569. containsPoint( point ) {
  20570. return point.x >= this.min.x && point.x <= this.max.x &&
  20571. point.y >= this.min.y && point.y <= this.max.y;
  20572. }
  20573. containsBox( box ) {
  20574. return this.min.x <= box.min.x && box.max.x <= this.max.x &&
  20575. this.min.y <= box.min.y && box.max.y <= this.max.y;
  20576. }
  20577. getParameter( point, target ) {
  20578. // This can potentially have a divide by zero if the box
  20579. // has a size dimension of 0.
  20580. return target.set(
  20581. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  20582. ( point.y - this.min.y ) / ( this.max.y - this.min.y )
  20583. );
  20584. }
  20585. intersectsBox( box ) {
  20586. // using 4 splitting planes to rule out intersections
  20587. return box.max.x >= this.min.x && box.min.x <= this.max.x &&
  20588. box.max.y >= this.min.y && box.min.y <= this.max.y;
  20589. }
  20590. clampPoint( point, target ) {
  20591. return target.copy( point ).clamp( this.min, this.max );
  20592. }
  20593. distanceToPoint( point ) {
  20594. return this.clampPoint( point, _vector$4 ).distanceTo( point );
  20595. }
  20596. intersect( box ) {
  20597. this.min.max( box.min );
  20598. this.max.min( box.max );
  20599. if ( this.isEmpty() ) this.makeEmpty();
  20600. return this;
  20601. }
  20602. union( box ) {
  20603. this.min.min( box.min );
  20604. this.max.max( box.max );
  20605. return this;
  20606. }
  20607. translate( offset ) {
  20608. this.min.add( offset );
  20609. this.max.add( offset );
  20610. return this;
  20611. }
  20612. equals( box ) {
  20613. return box.min.equals( this.min ) && box.max.equals( this.max );
  20614. }
  20615. }
  20616. const _startP = /*@__PURE__*/ new Vector3();
  20617. const _startEnd = /*@__PURE__*/ new Vector3();
  20618. class Line3 {
  20619. constructor( start = new Vector3(), end = new Vector3() ) {
  20620. this.start = start;
  20621. this.end = end;
  20622. }
  20623. set( start, end ) {
  20624. this.start.copy( start );
  20625. this.end.copy( end );
  20626. return this;
  20627. }
  20628. copy( line ) {
  20629. this.start.copy( line.start );
  20630. this.end.copy( line.end );
  20631. return this;
  20632. }
  20633. getCenter( target ) {
  20634. return target.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
  20635. }
  20636. delta( target ) {
  20637. return target.subVectors( this.end, this.start );
  20638. }
  20639. distanceSq() {
  20640. return this.start.distanceToSquared( this.end );
  20641. }
  20642. distance() {
  20643. return this.start.distanceTo( this.end );
  20644. }
  20645. at( t, target ) {
  20646. return this.delta( target ).multiplyScalar( t ).add( this.start );
  20647. }
  20648. closestPointToPointParameter( point, clampToLine ) {
  20649. _startP.subVectors( point, this.start );
  20650. _startEnd.subVectors( this.end, this.start );
  20651. const startEnd2 = _startEnd.dot( _startEnd );
  20652. const startEnd_startP = _startEnd.dot( _startP );
  20653. let t = startEnd_startP / startEnd2;
  20654. if ( clampToLine ) {
  20655. t = clamp( t, 0, 1 );
  20656. }
  20657. return t;
  20658. }
  20659. closestPointToPoint( point, clampToLine, target ) {
  20660. const t = this.closestPointToPointParameter( point, clampToLine );
  20661. return this.delta( target ).multiplyScalar( t ).add( this.start );
  20662. }
  20663. applyMatrix4( matrix ) {
  20664. this.start.applyMatrix4( matrix );
  20665. this.end.applyMatrix4( matrix );
  20666. return this;
  20667. }
  20668. equals( line ) {
  20669. return line.start.equals( this.start ) && line.end.equals( this.end );
  20670. }
  20671. clone() {
  20672. return new this.constructor().copy( this );
  20673. }
  20674. }
  20675. const _vector$3 = /*@__PURE__*/ new Vector3();
  20676. class SpotLightHelper extends Object3D {
  20677. constructor( light, color ) {
  20678. super();
  20679. this.light = light;
  20680. this.matrixAutoUpdate = false;
  20681. this.color = color;
  20682. this.type = 'SpotLightHelper';
  20683. const geometry = new BufferGeometry();
  20684. const positions = [
  20685. 0, 0, 0, 0, 0, 1,
  20686. 0, 0, 0, 1, 0, 1,
  20687. 0, 0, 0, - 1, 0, 1,
  20688. 0, 0, 0, 0, 1, 1,
  20689. 0, 0, 0, 0, - 1, 1
  20690. ];
  20691. for ( let i = 0, j = 1, l = 32; i < l; i ++, j ++ ) {
  20692. const p1 = ( i / l ) * Math.PI * 2;
  20693. const p2 = ( j / l ) * Math.PI * 2;
  20694. positions.push(
  20695. Math.cos( p1 ), Math.sin( p1 ), 1,
  20696. Math.cos( p2 ), Math.sin( p2 ), 1
  20697. );
  20698. }
  20699. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  20700. const material = new LineBasicMaterial( { fog: false, toneMapped: false } );
  20701. this.cone = new LineSegments( geometry, material );
  20702. this.add( this.cone );
  20703. this.update();
  20704. }
  20705. dispose() {
  20706. this.cone.geometry.dispose();
  20707. this.cone.material.dispose();
  20708. }
  20709. update() {
  20710. this.light.updateWorldMatrix( true, false );
  20711. this.light.target.updateWorldMatrix( true, false );
  20712. // update the local matrix based on the parent and light target transforms
  20713. if ( this.parent ) {
  20714. this.parent.updateWorldMatrix( true );
  20715. this.matrix
  20716. .copy( this.parent.matrixWorld )
  20717. .invert()
  20718. .multiply( this.light.matrixWorld );
  20719. } else {
  20720. this.matrix.copy( this.light.matrixWorld );
  20721. }
  20722. this.matrixWorld.copy( this.light.matrixWorld );
  20723. const coneLength = this.light.distance ? this.light.distance : 1000;
  20724. const coneWidth = coneLength * Math.tan( this.light.angle );
  20725. this.cone.scale.set( coneWidth, coneWidth, coneLength );
  20726. _vector$3.setFromMatrixPosition( this.light.target.matrixWorld );
  20727. this.cone.lookAt( _vector$3 );
  20728. if ( this.color !== undefined ) {
  20729. this.cone.material.color.set( this.color );
  20730. } else {
  20731. this.cone.material.color.copy( this.light.color );
  20732. }
  20733. }
  20734. }
  20735. const _vector$2 = /*@__PURE__*/ new Vector3();
  20736. const _boneMatrix = /*@__PURE__*/ new Matrix4();
  20737. const _matrixWorldInv = /*@__PURE__*/ new Matrix4();
  20738. class SkeletonHelper extends LineSegments {
  20739. constructor( object ) {
  20740. const bones = getBoneList( object );
  20741. const geometry = new BufferGeometry();
  20742. const vertices = [];
  20743. const colors = [];
  20744. const color1 = new Color( 0, 0, 1 );
  20745. const color2 = new Color( 0, 1, 0 );
  20746. for ( let i = 0; i < bones.length; i ++ ) {
  20747. const bone = bones[ i ];
  20748. if ( bone.parent && bone.parent.isBone ) {
  20749. vertices.push( 0, 0, 0 );
  20750. vertices.push( 0, 0, 0 );
  20751. colors.push( color1.r, color1.g, color1.b );
  20752. colors.push( color2.r, color2.g, color2.b );
  20753. }
  20754. }
  20755. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  20756. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  20757. const material = new LineBasicMaterial( { vertexColors: true, depthTest: false, depthWrite: false, toneMapped: false, transparent: true } );
  20758. super( geometry, material );
  20759. this.isSkeletonHelper = true;
  20760. this.type = 'SkeletonHelper';
  20761. this.root = object;
  20762. this.bones = bones;
  20763. this.matrix = object.matrixWorld;
  20764. this.matrixAutoUpdate = false;
  20765. }
  20766. updateMatrixWorld( force ) {
  20767. const bones = this.bones;
  20768. const geometry = this.geometry;
  20769. const position = geometry.getAttribute( 'position' );
  20770. _matrixWorldInv.copy( this.root.matrixWorld ).invert();
  20771. for ( let i = 0, j = 0; i < bones.length; i ++ ) {
  20772. const bone = bones[ i ];
  20773. if ( bone.parent && bone.parent.isBone ) {
  20774. _boneMatrix.multiplyMatrices( _matrixWorldInv, bone.matrixWorld );
  20775. _vector$2.setFromMatrixPosition( _boneMatrix );
  20776. position.setXYZ( j, _vector$2.x, _vector$2.y, _vector$2.z );
  20777. _boneMatrix.multiplyMatrices( _matrixWorldInv, bone.parent.matrixWorld );
  20778. _vector$2.setFromMatrixPosition( _boneMatrix );
  20779. position.setXYZ( j + 1, _vector$2.x, _vector$2.y, _vector$2.z );
  20780. j += 2;
  20781. }
  20782. }
  20783. geometry.getAttribute( 'position' ).needsUpdate = true;
  20784. super.updateMatrixWorld( force );
  20785. }
  20786. dispose() {
  20787. this.geometry.dispose();
  20788. this.material.dispose();
  20789. }
  20790. }
  20791. function getBoneList( object ) {
  20792. const boneList = [];
  20793. if ( object.isBone === true ) {
  20794. boneList.push( object );
  20795. }
  20796. for ( let i = 0; i < object.children.length; i ++ ) {
  20797. boneList.push.apply( boneList, getBoneList( object.children[ i ] ) );
  20798. }
  20799. return boneList;
  20800. }
  20801. class PointLightHelper extends Mesh {
  20802. constructor( light, sphereSize, color ) {
  20803. const geometry = new SphereGeometry( sphereSize, 4, 2 );
  20804. const material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
  20805. super( geometry, material );
  20806. this.light = light;
  20807. this.color = color;
  20808. this.type = 'PointLightHelper';
  20809. this.matrix = this.light.matrixWorld;
  20810. this.matrixAutoUpdate = false;
  20811. this.update();
  20812. /*
  20813. // TODO: delete this comment?
  20814. const distanceGeometry = new THREE.IcosahedronGeometry( 1, 2 );
  20815. const distanceMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false, wireframe: true, opacity: 0.1, transparent: true } );
  20816. this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial );
  20817. this.lightDistance = new THREE.Mesh( distanceGeometry, distanceMaterial );
  20818. const d = light.distance;
  20819. if ( d === 0.0 ) {
  20820. this.lightDistance.visible = false;
  20821. } else {
  20822. this.lightDistance.scale.set( d, d, d );
  20823. }
  20824. this.add( this.lightDistance );
  20825. */
  20826. }
  20827. dispose() {
  20828. this.geometry.dispose();
  20829. this.material.dispose();
  20830. }
  20831. update() {
  20832. this.light.updateWorldMatrix( true, false );
  20833. if ( this.color !== undefined ) {
  20834. this.material.color.set( this.color );
  20835. } else {
  20836. this.material.color.copy( this.light.color );
  20837. }
  20838. /*
  20839. const d = this.light.distance;
  20840. if ( d === 0.0 ) {
  20841. this.lightDistance.visible = false;
  20842. } else {
  20843. this.lightDistance.visible = true;
  20844. this.lightDistance.scale.set( d, d, d );
  20845. }
  20846. */
  20847. }
  20848. }
  20849. const _vector$1 = /*@__PURE__*/ new Vector3();
  20850. const _color1 = /*@__PURE__*/ new Color();
  20851. const _color2 = /*@__PURE__*/ new Color();
  20852. class HemisphereLightHelper extends Object3D {
  20853. constructor( light, size, color ) {
  20854. super();
  20855. this.light = light;
  20856. this.matrix = light.matrixWorld;
  20857. this.matrixAutoUpdate = false;
  20858. this.color = color;
  20859. this.type = 'HemisphereLightHelper';
  20860. const geometry = new OctahedronGeometry( size );
  20861. geometry.rotateY( Math.PI * 0.5 );
  20862. this.material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } );
  20863. if ( this.color === undefined ) this.material.vertexColors = true;
  20864. const position = geometry.getAttribute( 'position' );
  20865. const colors = new Float32Array( position.count * 3 );
  20866. geometry.setAttribute( 'color', new BufferAttribute( colors, 3 ) );
  20867. this.add( new Mesh( geometry, this.material ) );
  20868. this.update();
  20869. }
  20870. dispose() {
  20871. this.children[ 0 ].geometry.dispose();
  20872. this.children[ 0 ].material.dispose();
  20873. }
  20874. update() {
  20875. const mesh = this.children[ 0 ];
  20876. if ( this.color !== undefined ) {
  20877. this.material.color.set( this.color );
  20878. } else {
  20879. const colors = mesh.geometry.getAttribute( 'color' );
  20880. _color1.copy( this.light.color );
  20881. _color2.copy( this.light.groundColor );
  20882. for ( let i = 0, l = colors.count; i < l; i ++ ) {
  20883. const color = ( i < ( l / 2 ) ) ? _color1 : _color2;
  20884. colors.setXYZ( i, color.r, color.g, color.b );
  20885. }
  20886. colors.needsUpdate = true;
  20887. }
  20888. this.light.updateWorldMatrix( true, false );
  20889. mesh.lookAt( _vector$1.setFromMatrixPosition( this.light.matrixWorld ).negate() );
  20890. }
  20891. }
  20892. class GridHelper extends LineSegments {
  20893. constructor( size = 10, divisions = 10, color1 = 0x444444, color2 = 0x888888 ) {
  20894. color1 = new Color( color1 );
  20895. color2 = new Color( color2 );
  20896. const center = divisions / 2;
  20897. const step = size / divisions;
  20898. const halfSize = size / 2;
  20899. const vertices = [], colors = [];
  20900. for ( let i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) {
  20901. vertices.push( - halfSize, 0, k, halfSize, 0, k );
  20902. vertices.push( k, 0, - halfSize, k, 0, halfSize );
  20903. const color = i === center ? color1 : color2;
  20904. color.toArray( colors, j ); j += 3;
  20905. color.toArray( colors, j ); j += 3;
  20906. color.toArray( colors, j ); j += 3;
  20907. color.toArray( colors, j ); j += 3;
  20908. }
  20909. const geometry = new BufferGeometry();
  20910. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  20911. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  20912. const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
  20913. super( geometry, material );
  20914. this.type = 'GridHelper';
  20915. }
  20916. dispose() {
  20917. this.geometry.dispose();
  20918. this.material.dispose();
  20919. }
  20920. }
  20921. class PolarGridHelper extends LineSegments {
  20922. constructor( radius = 10, sectors = 16, rings = 8, divisions = 64, color1 = 0x444444, color2 = 0x888888 ) {
  20923. color1 = new Color( color1 );
  20924. color2 = new Color( color2 );
  20925. const vertices = [];
  20926. const colors = [];
  20927. // create the sectors
  20928. if ( sectors > 1 ) {
  20929. for ( let i = 0; i < sectors; i ++ ) {
  20930. const v = ( i / sectors ) * ( Math.PI * 2 );
  20931. const x = Math.sin( v ) * radius;
  20932. const z = Math.cos( v ) * radius;
  20933. vertices.push( 0, 0, 0 );
  20934. vertices.push( x, 0, z );
  20935. const color = ( i & 1 ) ? color1 : color2;
  20936. colors.push( color.r, color.g, color.b );
  20937. colors.push( color.r, color.g, color.b );
  20938. }
  20939. }
  20940. // create the rings
  20941. for ( let i = 0; i < rings; i ++ ) {
  20942. const color = ( i & 1 ) ? color1 : color2;
  20943. const r = radius - ( radius / rings * i );
  20944. for ( let j = 0; j < divisions; j ++ ) {
  20945. // first vertex
  20946. let v = ( j / divisions ) * ( Math.PI * 2 );
  20947. let x = Math.sin( v ) * r;
  20948. let z = Math.cos( v ) * r;
  20949. vertices.push( x, 0, z );
  20950. colors.push( color.r, color.g, color.b );
  20951. // second vertex
  20952. v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 );
  20953. x = Math.sin( v ) * r;
  20954. z = Math.cos( v ) * r;
  20955. vertices.push( x, 0, z );
  20956. colors.push( color.r, color.g, color.b );
  20957. }
  20958. }
  20959. const geometry = new BufferGeometry();
  20960. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  20961. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  20962. const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
  20963. super( geometry, material );
  20964. this.type = 'PolarGridHelper';
  20965. }
  20966. dispose() {
  20967. this.geometry.dispose();
  20968. this.material.dispose();
  20969. }
  20970. }
  20971. const _v1 = /*@__PURE__*/ new Vector3();
  20972. const _v2 = /*@__PURE__*/ new Vector3();
  20973. const _v3 = /*@__PURE__*/ new Vector3();
  20974. class DirectionalLightHelper extends Object3D {
  20975. constructor( light, size, color ) {
  20976. super();
  20977. this.light = light;
  20978. this.matrix = light.matrixWorld;
  20979. this.matrixAutoUpdate = false;
  20980. this.color = color;
  20981. this.type = 'DirectionalLightHelper';
  20982. if ( size === undefined ) size = 1;
  20983. let geometry = new BufferGeometry();
  20984. geometry.setAttribute( 'position', new Float32BufferAttribute( [
  20985. - size, size, 0,
  20986. size, size, 0,
  20987. size, - size, 0,
  20988. - size, - size, 0,
  20989. - size, size, 0
  20990. ], 3 ) );
  20991. const material = new LineBasicMaterial( { fog: false, toneMapped: false } );
  20992. this.lightPlane = new Line( geometry, material );
  20993. this.add( this.lightPlane );
  20994. geometry = new BufferGeometry();
  20995. geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) );
  20996. this.targetLine = new Line( geometry, material );
  20997. this.add( this.targetLine );
  20998. this.update();
  20999. }
  21000. dispose() {
  21001. this.lightPlane.geometry.dispose();
  21002. this.lightPlane.material.dispose();
  21003. this.targetLine.geometry.dispose();
  21004. this.targetLine.material.dispose();
  21005. }
  21006. update() {
  21007. this.light.updateWorldMatrix( true, false );
  21008. this.light.target.updateWorldMatrix( true, false );
  21009. _v1.setFromMatrixPosition( this.light.matrixWorld );
  21010. _v2.setFromMatrixPosition( this.light.target.matrixWorld );
  21011. _v3.subVectors( _v2, _v1 );
  21012. this.lightPlane.lookAt( _v2 );
  21013. if ( this.color !== undefined ) {
  21014. this.lightPlane.material.color.set( this.color );
  21015. this.targetLine.material.color.set( this.color );
  21016. } else {
  21017. this.lightPlane.material.color.copy( this.light.color );
  21018. this.targetLine.material.color.copy( this.light.color );
  21019. }
  21020. this.targetLine.lookAt( _v2 );
  21021. this.targetLine.scale.z = _v3.length();
  21022. }
  21023. }
  21024. const _vector = /*@__PURE__*/ new Vector3();
  21025. const _camera = /*@__PURE__*/ new Camera();
  21026. /**
  21027. * - shows frustum, line of sight and up of the camera
  21028. * - suitable for fast updates
  21029. * - based on frustum visualization in lightgl.js shadowmap example
  21030. * https://github.com/evanw/lightgl.js/blob/master/tests/shadowmap.html
  21031. */
  21032. class CameraHelper extends LineSegments {
  21033. constructor( camera ) {
  21034. const geometry = new BufferGeometry();
  21035. const material = new LineBasicMaterial( { color: 0xffffff, vertexColors: true, toneMapped: false } );
  21036. const vertices = [];
  21037. const colors = [];
  21038. const pointMap = {};
  21039. // near
  21040. addLine( 'n1', 'n2' );
  21041. addLine( 'n2', 'n4' );
  21042. addLine( 'n4', 'n3' );
  21043. addLine( 'n3', 'n1' );
  21044. // far
  21045. addLine( 'f1', 'f2' );
  21046. addLine( 'f2', 'f4' );
  21047. addLine( 'f4', 'f3' );
  21048. addLine( 'f3', 'f1' );
  21049. // sides
  21050. addLine( 'n1', 'f1' );
  21051. addLine( 'n2', 'f2' );
  21052. addLine( 'n3', 'f3' );
  21053. addLine( 'n4', 'f4' );
  21054. // cone
  21055. addLine( 'p', 'n1' );
  21056. addLine( 'p', 'n2' );
  21057. addLine( 'p', 'n3' );
  21058. addLine( 'p', 'n4' );
  21059. // up
  21060. addLine( 'u1', 'u2' );
  21061. addLine( 'u2', 'u3' );
  21062. addLine( 'u3', 'u1' );
  21063. // target
  21064. addLine( 'c', 't' );
  21065. addLine( 'p', 'c' );
  21066. // cross
  21067. addLine( 'cn1', 'cn2' );
  21068. addLine( 'cn3', 'cn4' );
  21069. addLine( 'cf1', 'cf2' );
  21070. addLine( 'cf3', 'cf4' );
  21071. function addLine( a, b ) {
  21072. addPoint( a );
  21073. addPoint( b );
  21074. }
  21075. function addPoint( id ) {
  21076. vertices.push( 0, 0, 0 );
  21077. colors.push( 0, 0, 0 );
  21078. if ( pointMap[ id ] === undefined ) {
  21079. pointMap[ id ] = [];
  21080. }
  21081. pointMap[ id ].push( ( vertices.length / 3 ) - 1 );
  21082. }
  21083. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  21084. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  21085. super( geometry, material );
  21086. this.type = 'CameraHelper';
  21087. this.camera = camera;
  21088. if ( this.camera.updateProjectionMatrix ) this.camera.updateProjectionMatrix();
  21089. this.matrix = camera.matrixWorld;
  21090. this.matrixAutoUpdate = false;
  21091. this.pointMap = pointMap;
  21092. this.update();
  21093. // colors
  21094. const colorFrustum = new Color( 0xffaa00 );
  21095. const colorCone = new Color( 0xff0000 );
  21096. const colorUp = new Color( 0x00aaff );
  21097. const colorTarget = new Color( 0xffffff );
  21098. const colorCross = new Color( 0x333333 );
  21099. this.setColors( colorFrustum, colorCone, colorUp, colorTarget, colorCross );
  21100. }
  21101. setColors( frustum, cone, up, target, cross ) {
  21102. const geometry = this.geometry;
  21103. const colorAttribute = geometry.getAttribute( 'color' );
  21104. // near
  21105. colorAttribute.setXYZ( 0, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 1, frustum.r, frustum.g, frustum.b ); // n1, n2
  21106. colorAttribute.setXYZ( 2, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 3, frustum.r, frustum.g, frustum.b ); // n2, n4
  21107. colorAttribute.setXYZ( 4, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 5, frustum.r, frustum.g, frustum.b ); // n4, n3
  21108. colorAttribute.setXYZ( 6, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 7, frustum.r, frustum.g, frustum.b ); // n3, n1
  21109. // far
  21110. colorAttribute.setXYZ( 8, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 9, frustum.r, frustum.g, frustum.b ); // f1, f2
  21111. colorAttribute.setXYZ( 10, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 11, frustum.r, frustum.g, frustum.b ); // f2, f4
  21112. colorAttribute.setXYZ( 12, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 13, frustum.r, frustum.g, frustum.b ); // f4, f3
  21113. colorAttribute.setXYZ( 14, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 15, frustum.r, frustum.g, frustum.b ); // f3, f1
  21114. // sides
  21115. colorAttribute.setXYZ( 16, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 17, frustum.r, frustum.g, frustum.b ); // n1, f1
  21116. colorAttribute.setXYZ( 18, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 19, frustum.r, frustum.g, frustum.b ); // n2, f2
  21117. colorAttribute.setXYZ( 20, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 21, frustum.r, frustum.g, frustum.b ); // n3, f3
  21118. colorAttribute.setXYZ( 22, frustum.r, frustum.g, frustum.b ); colorAttribute.setXYZ( 23, frustum.r, frustum.g, frustum.b ); // n4, f4
  21119. // cone
  21120. colorAttribute.setXYZ( 24, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 25, cone.r, cone.g, cone.b ); // p, n1
  21121. colorAttribute.setXYZ( 26, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 27, cone.r, cone.g, cone.b ); // p, n2
  21122. colorAttribute.setXYZ( 28, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 29, cone.r, cone.g, cone.b ); // p, n3
  21123. colorAttribute.setXYZ( 30, cone.r, cone.g, cone.b ); colorAttribute.setXYZ( 31, cone.r, cone.g, cone.b ); // p, n4
  21124. // up
  21125. colorAttribute.setXYZ( 32, up.r, up.g, up.b ); colorAttribute.setXYZ( 33, up.r, up.g, up.b ); // u1, u2
  21126. colorAttribute.setXYZ( 34, up.r, up.g, up.b ); colorAttribute.setXYZ( 35, up.r, up.g, up.b ); // u2, u3
  21127. colorAttribute.setXYZ( 36, up.r, up.g, up.b ); colorAttribute.setXYZ( 37, up.r, up.g, up.b ); // u3, u1
  21128. // target
  21129. colorAttribute.setXYZ( 38, target.r, target.g, target.b ); colorAttribute.setXYZ( 39, target.r, target.g, target.b ); // c, t
  21130. colorAttribute.setXYZ( 40, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 41, cross.r, cross.g, cross.b ); // p, c
  21131. // cross
  21132. colorAttribute.setXYZ( 42, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 43, cross.r, cross.g, cross.b ); // cn1, cn2
  21133. colorAttribute.setXYZ( 44, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 45, cross.r, cross.g, cross.b ); // cn3, cn4
  21134. colorAttribute.setXYZ( 46, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 47, cross.r, cross.g, cross.b ); // cf1, cf2
  21135. colorAttribute.setXYZ( 48, cross.r, cross.g, cross.b ); colorAttribute.setXYZ( 49, cross.r, cross.g, cross.b ); // cf3, cf4
  21136. colorAttribute.needsUpdate = true;
  21137. }
  21138. update() {
  21139. const geometry = this.geometry;
  21140. const pointMap = this.pointMap;
  21141. const w = 1, h = 1;
  21142. // we need just camera projection matrix inverse
  21143. // world matrix must be identity
  21144. _camera.projectionMatrixInverse.copy( this.camera.projectionMatrixInverse );
  21145. // center / target
  21146. setPoint( 'c', pointMap, geometry, _camera, 0, 0, - 1 );
  21147. setPoint( 't', pointMap, geometry, _camera, 0, 0, 1 );
  21148. // near
  21149. setPoint( 'n1', pointMap, geometry, _camera, - w, - h, - 1 );
  21150. setPoint( 'n2', pointMap, geometry, _camera, w, - h, - 1 );
  21151. setPoint( 'n3', pointMap, geometry, _camera, - w, h, - 1 );
  21152. setPoint( 'n4', pointMap, geometry, _camera, w, h, - 1 );
  21153. // far
  21154. setPoint( 'f1', pointMap, geometry, _camera, - w, - h, 1 );
  21155. setPoint( 'f2', pointMap, geometry, _camera, w, - h, 1 );
  21156. setPoint( 'f3', pointMap, geometry, _camera, - w, h, 1 );
  21157. setPoint( 'f4', pointMap, geometry, _camera, w, h, 1 );
  21158. // up
  21159. setPoint( 'u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, - 1 );
  21160. setPoint( 'u2', pointMap, geometry, _camera, - w * 0.7, h * 1.1, - 1 );
  21161. setPoint( 'u3', pointMap, geometry, _camera, 0, h * 2, - 1 );
  21162. // cross
  21163. setPoint( 'cf1', pointMap, geometry, _camera, - w, 0, 1 );
  21164. setPoint( 'cf2', pointMap, geometry, _camera, w, 0, 1 );
  21165. setPoint( 'cf3', pointMap, geometry, _camera, 0, - h, 1 );
  21166. setPoint( 'cf4', pointMap, geometry, _camera, 0, h, 1 );
  21167. setPoint( 'cn1', pointMap, geometry, _camera, - w, 0, - 1 );
  21168. setPoint( 'cn2', pointMap, geometry, _camera, w, 0, - 1 );
  21169. setPoint( 'cn3', pointMap, geometry, _camera, 0, - h, - 1 );
  21170. setPoint( 'cn4', pointMap, geometry, _camera, 0, h, - 1 );
  21171. geometry.getAttribute( 'position' ).needsUpdate = true;
  21172. }
  21173. dispose() {
  21174. this.geometry.dispose();
  21175. this.material.dispose();
  21176. }
  21177. }
  21178. function setPoint( point, pointMap, geometry, camera, x, y, z ) {
  21179. _vector.set( x, y, z ).unproject( camera );
  21180. const points = pointMap[ point ];
  21181. if ( points !== undefined ) {
  21182. const position = geometry.getAttribute( 'position' );
  21183. for ( let i = 0, l = points.length; i < l; i ++ ) {
  21184. position.setXYZ( points[ i ], _vector.x, _vector.y, _vector.z );
  21185. }
  21186. }
  21187. }
  21188. const _box = /*@__PURE__*/ new Box3();
  21189. class BoxHelper extends LineSegments {
  21190. constructor( object, color = 0xffff00 ) {
  21191. 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 ] );
  21192. const positions = new Float32Array( 8 * 3 );
  21193. const geometry = new BufferGeometry();
  21194. geometry.setIndex( new BufferAttribute( indices, 1 ) );
  21195. geometry.setAttribute( 'position', new BufferAttribute( positions, 3 ) );
  21196. super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  21197. this.object = object;
  21198. this.type = 'BoxHelper';
  21199. this.matrixAutoUpdate = false;
  21200. this.update();
  21201. }
  21202. update( object ) {
  21203. if ( object !== undefined ) {
  21204. console.warn( 'THREE.BoxHelper: .update() has no longer arguments.' );
  21205. }
  21206. if ( this.object !== undefined ) {
  21207. _box.setFromObject( this.object );
  21208. }
  21209. if ( _box.isEmpty() ) return;
  21210. const min = _box.min;
  21211. const max = _box.max;
  21212. /*
  21213. 5____4
  21214. 1/___0/|
  21215. | 6__|_7
  21216. 2/___3/
  21217. 0: max.x, max.y, max.z
  21218. 1: min.x, max.y, max.z
  21219. 2: min.x, min.y, max.z
  21220. 3: max.x, min.y, max.z
  21221. 4: max.x, max.y, min.z
  21222. 5: min.x, max.y, min.z
  21223. 6: min.x, min.y, min.z
  21224. 7: max.x, min.y, min.z
  21225. */
  21226. const position = this.geometry.attributes.position;
  21227. const array = position.array;
  21228. array[ 0 ] = max.x; array[ 1 ] = max.y; array[ 2 ] = max.z;
  21229. array[ 3 ] = min.x; array[ 4 ] = max.y; array[ 5 ] = max.z;
  21230. array[ 6 ] = min.x; array[ 7 ] = min.y; array[ 8 ] = max.z;
  21231. array[ 9 ] = max.x; array[ 10 ] = min.y; array[ 11 ] = max.z;
  21232. array[ 12 ] = max.x; array[ 13 ] = max.y; array[ 14 ] = min.z;
  21233. array[ 15 ] = min.x; array[ 16 ] = max.y; array[ 17 ] = min.z;
  21234. array[ 18 ] = min.x; array[ 19 ] = min.y; array[ 20 ] = min.z;
  21235. array[ 21 ] = max.x; array[ 22 ] = min.y; array[ 23 ] = min.z;
  21236. position.needsUpdate = true;
  21237. this.geometry.computeBoundingSphere();
  21238. }
  21239. setFromObject( object ) {
  21240. this.object = object;
  21241. this.update();
  21242. return this;
  21243. }
  21244. copy( source, recursive ) {
  21245. super.copy( source, recursive );
  21246. this.object = source.object;
  21247. return this;
  21248. }
  21249. dispose() {
  21250. this.geometry.dispose();
  21251. this.material.dispose();
  21252. }
  21253. }
  21254. class Box3Helper extends LineSegments {
  21255. constructor( box, color = 0xffff00 ) {
  21256. 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 ] );
  21257. 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 ];
  21258. const geometry = new BufferGeometry();
  21259. geometry.setIndex( new BufferAttribute( indices, 1 ) );
  21260. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  21261. super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  21262. this.box = box;
  21263. this.type = 'Box3Helper';
  21264. this.geometry.computeBoundingSphere();
  21265. }
  21266. updateMatrixWorld( force ) {
  21267. const box = this.box;
  21268. if ( box.isEmpty() ) return;
  21269. box.getCenter( this.position );
  21270. box.getSize( this.scale );
  21271. this.scale.multiplyScalar( 0.5 );
  21272. super.updateMatrixWorld( force );
  21273. }
  21274. dispose() {
  21275. this.geometry.dispose();
  21276. this.material.dispose();
  21277. }
  21278. }
  21279. class PlaneHelper extends Line {
  21280. constructor( plane, size = 1, hex = 0xffff00 ) {
  21281. const color = hex;
  21282. 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 ];
  21283. const geometry = new BufferGeometry();
  21284. geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
  21285. geometry.computeBoundingSphere();
  21286. super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  21287. this.type = 'PlaneHelper';
  21288. this.plane = plane;
  21289. this.size = size;
  21290. const positions2 = [ 1, 1, 0, - 1, 1, 0, - 1, - 1, 0, 1, 1, 0, - 1, - 1, 0, 1, - 1, 0 ];
  21291. const geometry2 = new BufferGeometry();
  21292. geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
  21293. geometry2.computeBoundingSphere();
  21294. this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false, toneMapped: false } ) ) );
  21295. }
  21296. updateMatrixWorld( force ) {
  21297. this.position.set( 0, 0, 0 );
  21298. this.scale.set( 0.5 * this.size, 0.5 * this.size, 1 );
  21299. this.lookAt( this.plane.normal );
  21300. this.translateZ( - this.plane.constant );
  21301. super.updateMatrixWorld( force );
  21302. }
  21303. dispose() {
  21304. this.geometry.dispose();
  21305. this.material.dispose();
  21306. this.children[ 0 ].geometry.dispose();
  21307. this.children[ 0 ].material.dispose();
  21308. }
  21309. }
  21310. const _axis = /*@__PURE__*/ new Vector3();
  21311. let _lineGeometry, _coneGeometry;
  21312. class ArrowHelper extends Object3D {
  21313. // dir is assumed to be normalized
  21314. 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 ) {
  21315. super();
  21316. this.type = 'ArrowHelper';
  21317. if ( _lineGeometry === undefined ) {
  21318. _lineGeometry = new BufferGeometry();
  21319. _lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) );
  21320. _coneGeometry = new CylinderGeometry( 0, 0.5, 1, 5, 1 );
  21321. _coneGeometry.translate( 0, - 0.5, 0 );
  21322. }
  21323. this.position.copy( origin );
  21324. this.line = new Line( _lineGeometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );
  21325. this.line.matrixAutoUpdate = false;
  21326. this.add( this.line );
  21327. this.cone = new Mesh( _coneGeometry, new MeshBasicMaterial( { color: color, toneMapped: false } ) );
  21328. this.cone.matrixAutoUpdate = false;
  21329. this.add( this.cone );
  21330. this.setDirection( dir );
  21331. this.setLength( length, headLength, headWidth );
  21332. }
  21333. setDirection( dir ) {
  21334. // dir is assumed to be normalized
  21335. if ( dir.y > 0.99999 ) {
  21336. this.quaternion.set( 0, 0, 0, 1 );
  21337. } else if ( dir.y < - 0.99999 ) {
  21338. this.quaternion.set( 1, 0, 0, 0 );
  21339. } else {
  21340. _axis.set( dir.z, 0, - dir.x ).normalize();
  21341. const radians = Math.acos( dir.y );
  21342. this.quaternion.setFromAxisAngle( _axis, radians );
  21343. }
  21344. }
  21345. setLength( length, headLength = length * 0.2, headWidth = headLength * 0.2 ) {
  21346. this.line.scale.set( 1, Math.max( 0.0001, length - headLength ), 1 ); // see #17458
  21347. this.line.updateMatrix();
  21348. this.cone.scale.set( headWidth, headLength, headWidth );
  21349. this.cone.position.y = length;
  21350. this.cone.updateMatrix();
  21351. }
  21352. setColor( color ) {
  21353. this.line.material.color.set( color );
  21354. this.cone.material.color.set( color );
  21355. }
  21356. copy( source ) {
  21357. super.copy( source, false );
  21358. this.line.copy( source.line );
  21359. this.cone.copy( source.cone );
  21360. return this;
  21361. }
  21362. dispose() {
  21363. this.line.geometry.dispose();
  21364. this.line.material.dispose();
  21365. this.cone.geometry.dispose();
  21366. this.cone.material.dispose();
  21367. }
  21368. }
  21369. class AxesHelper extends LineSegments {
  21370. constructor( size = 1 ) {
  21371. const vertices = [
  21372. 0, 0, 0, size, 0, 0,
  21373. 0, 0, 0, 0, size, 0,
  21374. 0, 0, 0, 0, 0, size
  21375. ];
  21376. const colors = [
  21377. 1, 0, 0, 1, 0.6, 0,
  21378. 0, 1, 0, 0.6, 1, 0,
  21379. 0, 0, 1, 0, 0.6, 1
  21380. ];
  21381. const geometry = new BufferGeometry();
  21382. geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) );
  21383. geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) );
  21384. const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } );
  21385. super( geometry, material );
  21386. this.type = 'AxesHelper';
  21387. }
  21388. setColors( xAxisColor, yAxisColor, zAxisColor ) {
  21389. const color = new Color();
  21390. const array = this.geometry.attributes.color.array;
  21391. color.set( xAxisColor );
  21392. color.toArray( array, 0 );
  21393. color.toArray( array, 3 );
  21394. color.set( yAxisColor );
  21395. color.toArray( array, 6 );
  21396. color.toArray( array, 9 );
  21397. color.set( zAxisColor );
  21398. color.toArray( array, 12 );
  21399. color.toArray( array, 15 );
  21400. this.geometry.attributes.color.needsUpdate = true;
  21401. return this;
  21402. }
  21403. dispose() {
  21404. this.geometry.dispose();
  21405. this.material.dispose();
  21406. }
  21407. }
  21408. class ShapePath {
  21409. constructor() {
  21410. this.type = 'ShapePath';
  21411. this.color = new Color();
  21412. this.subPaths = [];
  21413. this.currentPath = null;
  21414. }
  21415. moveTo( x, y ) {
  21416. this.currentPath = new Path();
  21417. this.subPaths.push( this.currentPath );
  21418. this.currentPath.moveTo( x, y );
  21419. return this;
  21420. }
  21421. lineTo( x, y ) {
  21422. this.currentPath.lineTo( x, y );
  21423. return this;
  21424. }
  21425. quadraticCurveTo( aCPx, aCPy, aX, aY ) {
  21426. this.currentPath.quadraticCurveTo( aCPx, aCPy, aX, aY );
  21427. return this;
  21428. }
  21429. bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
  21430. this.currentPath.bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY );
  21431. return this;
  21432. }
  21433. splineThru( pts ) {
  21434. this.currentPath.splineThru( pts );
  21435. return this;
  21436. }
  21437. toShapes( isCCW ) {
  21438. function toShapesNoHoles( inSubpaths ) {
  21439. const shapes = [];
  21440. for ( let i = 0, l = inSubpaths.length; i < l; i ++ ) {
  21441. const tmpPath = inSubpaths[ i ];
  21442. const tmpShape = new Shape();
  21443. tmpShape.curves = tmpPath.curves;
  21444. shapes.push( tmpShape );
  21445. }
  21446. return shapes;
  21447. }
  21448. function isPointInsidePolygon( inPt, inPolygon ) {
  21449. const polyLen = inPolygon.length;
  21450. // inPt on polygon contour => immediate success or
  21451. // toggling of inside/outside at every single! intersection point of an edge
  21452. // with the horizontal line through inPt, left of inPt
  21453. // not counting lowerY endpoints of edges and whole edges on that line
  21454. let inside = false;
  21455. for ( let p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) {
  21456. let edgeLowPt = inPolygon[ p ];
  21457. let edgeHighPt = inPolygon[ q ];
  21458. let edgeDx = edgeHighPt.x - edgeLowPt.x;
  21459. let edgeDy = edgeHighPt.y - edgeLowPt.y;
  21460. if ( Math.abs( edgeDy ) > Number.EPSILON ) {
  21461. // not parallel
  21462. if ( edgeDy < 0 ) {
  21463. edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx;
  21464. edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy;
  21465. }
  21466. if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue;
  21467. if ( inPt.y === edgeLowPt.y ) {
  21468. if ( inPt.x === edgeLowPt.x ) return true; // inPt is on contour ?
  21469. // continue; // no intersection or edgeLowPt => doesn't count !!!
  21470. } else {
  21471. const perpEdge = edgeDy * ( inPt.x - edgeLowPt.x ) - edgeDx * ( inPt.y - edgeLowPt.y );
  21472. if ( perpEdge === 0 ) return true; // inPt is on contour ?
  21473. if ( perpEdge < 0 ) continue;
  21474. inside = ! inside; // true intersection left of inPt
  21475. }
  21476. } else {
  21477. // parallel or collinear
  21478. if ( inPt.y !== edgeLowPt.y ) continue; // parallel
  21479. // edge lies on the same horizontal line as inPt
  21480. if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) ||
  21481. ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour !
  21482. // continue;
  21483. }
  21484. }
  21485. return inside;
  21486. }
  21487. const isClockWise = ShapeUtils.isClockWise;
  21488. const subPaths = this.subPaths;
  21489. if ( subPaths.length === 0 ) return [];
  21490. let solid, tmpPath, tmpShape;
  21491. const shapes = [];
  21492. if ( subPaths.length === 1 ) {
  21493. tmpPath = subPaths[ 0 ];
  21494. tmpShape = new Shape();
  21495. tmpShape.curves = tmpPath.curves;
  21496. shapes.push( tmpShape );
  21497. return shapes;
  21498. }
  21499. let holesFirst = ! isClockWise( subPaths[ 0 ].getPoints() );
  21500. holesFirst = isCCW ? ! holesFirst : holesFirst;
  21501. // console.log("Holes first", holesFirst);
  21502. const betterShapeHoles = [];
  21503. const newShapes = [];
  21504. let newShapeHoles = [];
  21505. let mainIdx = 0;
  21506. let tmpPoints;
  21507. newShapes[ mainIdx ] = undefined;
  21508. newShapeHoles[ mainIdx ] = [];
  21509. for ( let i = 0, l = subPaths.length; i < l; i ++ ) {
  21510. tmpPath = subPaths[ i ];
  21511. tmpPoints = tmpPath.getPoints();
  21512. solid = isClockWise( tmpPoints );
  21513. solid = isCCW ? ! solid : solid;
  21514. if ( solid ) {
  21515. if ( ( ! holesFirst ) && ( newShapes[ mainIdx ] ) ) mainIdx ++;
  21516. newShapes[ mainIdx ] = { s: new Shape(), p: tmpPoints };
  21517. newShapes[ mainIdx ].s.curves = tmpPath.curves;
  21518. if ( holesFirst ) mainIdx ++;
  21519. newShapeHoles[ mainIdx ] = [];
  21520. //console.log('cw', i);
  21521. } else {
  21522. newShapeHoles[ mainIdx ].push( { h: tmpPath, p: tmpPoints[ 0 ] } );
  21523. //console.log('ccw', i);
  21524. }
  21525. }
  21526. // only Holes? -> probably all Shapes with wrong orientation
  21527. if ( ! newShapes[ 0 ] ) return toShapesNoHoles( subPaths );
  21528. if ( newShapes.length > 1 ) {
  21529. let ambiguous = false;
  21530. let toChange = 0;
  21531. for ( let sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
  21532. betterShapeHoles[ sIdx ] = [];
  21533. }
  21534. for ( let sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
  21535. const sho = newShapeHoles[ sIdx ];
  21536. for ( let hIdx = 0; hIdx < sho.length; hIdx ++ ) {
  21537. const ho = sho[ hIdx ];
  21538. let hole_unassigned = true;
  21539. for ( let s2Idx = 0; s2Idx < newShapes.length; s2Idx ++ ) {
  21540. if ( isPointInsidePolygon( ho.p, newShapes[ s2Idx ].p ) ) {
  21541. if ( sIdx !== s2Idx ) toChange ++;
  21542. if ( hole_unassigned ) {
  21543. hole_unassigned = false;
  21544. betterShapeHoles[ s2Idx ].push( ho );
  21545. } else {
  21546. ambiguous = true;
  21547. }
  21548. }
  21549. }
  21550. if ( hole_unassigned ) {
  21551. betterShapeHoles[ sIdx ].push( ho );
  21552. }
  21553. }
  21554. }
  21555. if ( toChange > 0 && ambiguous === false ) {
  21556. newShapeHoles = betterShapeHoles;
  21557. }
  21558. }
  21559. let tmpHoles;
  21560. for ( let i = 0, il = newShapes.length; i < il; i ++ ) {
  21561. tmpShape = newShapes[ i ].s;
  21562. shapes.push( tmpShape );
  21563. tmpHoles = newShapeHoles[ i ];
  21564. for ( let j = 0, jl = tmpHoles.length; j < jl; j ++ ) {
  21565. tmpShape.holes.push( tmpHoles[ j ].h );
  21566. }
  21567. }
  21568. //console.log("shape", shapes);
  21569. return shapes;
  21570. }
  21571. }
  21572. class Controls extends EventDispatcher {
  21573. constructor( object, domElement = null ) {
  21574. super();
  21575. this.object = object;
  21576. this.domElement = domElement;
  21577. this.enabled = true;
  21578. this.state = - 1;
  21579. this.keys = {};
  21580. this.mouseButtons = { LEFT: null, MIDDLE: null, RIGHT: null };
  21581. this.touches = { ONE: null, TWO: null };
  21582. }
  21583. connect() {}
  21584. disconnect() {}
  21585. dispose() {}
  21586. update( /* delta */ ) {}
  21587. }
  21588. function contain( texture, aspect ) {
  21589. const imageAspect = ( texture.image && texture.image.width ) ? texture.image.width / texture.image.height : 1;
  21590. if ( imageAspect > aspect ) {
  21591. texture.repeat.x = 1;
  21592. texture.repeat.y = imageAspect / aspect;
  21593. texture.offset.x = 0;
  21594. texture.offset.y = ( 1 - texture.repeat.y ) / 2;
  21595. } else {
  21596. texture.repeat.x = aspect / imageAspect;
  21597. texture.repeat.y = 1;
  21598. texture.offset.x = ( 1 - texture.repeat.x ) / 2;
  21599. texture.offset.y = 0;
  21600. }
  21601. return texture;
  21602. }
  21603. function cover( texture, aspect ) {
  21604. const imageAspect = ( texture.image && texture.image.width ) ? texture.image.width / texture.image.height : 1;
  21605. if ( imageAspect > aspect ) {
  21606. texture.repeat.x = aspect / imageAspect;
  21607. texture.repeat.y = 1;
  21608. texture.offset.x = ( 1 - texture.repeat.x ) / 2;
  21609. texture.offset.y = 0;
  21610. } else {
  21611. texture.repeat.x = 1;
  21612. texture.repeat.y = imageAspect / aspect;
  21613. texture.offset.x = 0;
  21614. texture.offset.y = ( 1 - texture.repeat.y ) / 2;
  21615. }
  21616. return texture;
  21617. }
  21618. function fill( texture ) {
  21619. texture.repeat.x = 1;
  21620. texture.repeat.y = 1;
  21621. texture.offset.x = 0;
  21622. texture.offset.y = 0;
  21623. return texture;
  21624. }
  21625. /**
  21626. * Given the width, height, format, and type of a texture. Determines how many
  21627. * bytes must be used to represent the texture.
  21628. */
  21629. function getByteLength( width, height, format, type ) {
  21630. const typeByteLength = getTextureTypeByteLength( type );
  21631. switch ( format ) {
  21632. // https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml
  21633. case AlphaFormat:
  21634. return width * height;
  21635. case LuminanceFormat:
  21636. return width * height;
  21637. case LuminanceAlphaFormat:
  21638. return width * height * 2;
  21639. case RedFormat:
  21640. return ( ( width * height ) / typeByteLength.components ) * typeByteLength.byteLength;
  21641. case RedIntegerFormat:
  21642. return ( ( width * height ) / typeByteLength.components ) * typeByteLength.byteLength;
  21643. case RGFormat:
  21644. return ( ( width * height * 2 ) / typeByteLength.components ) * typeByteLength.byteLength;
  21645. case RGIntegerFormat:
  21646. return ( ( width * height * 2 ) / typeByteLength.components ) * typeByteLength.byteLength;
  21647. case RGBFormat:
  21648. return ( ( width * height * 3 ) / typeByteLength.components ) * typeByteLength.byteLength;
  21649. case RGBAFormat:
  21650. return ( ( width * height * 4 ) / typeByteLength.components ) * typeByteLength.byteLength;
  21651. case RGBAIntegerFormat:
  21652. return ( ( width * height * 4 ) / typeByteLength.components ) * typeByteLength.byteLength;
  21653. // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_s3tc_srgb/
  21654. case RGB_S3TC_DXT1_Format:
  21655. case RGBA_S3TC_DXT1_Format:
  21656. return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 8;
  21657. case RGBA_S3TC_DXT3_Format:
  21658. case RGBA_S3TC_DXT5_Format:
  21659. return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 16;
  21660. // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_pvrtc/
  21661. case RGB_PVRTC_2BPPV1_Format:
  21662. case RGBA_PVRTC_2BPPV1_Format:
  21663. return ( Math.max( width, 16 ) * Math.max( height, 8 ) ) / 4;
  21664. case RGB_PVRTC_4BPPV1_Format:
  21665. case RGBA_PVRTC_4BPPV1_Format:
  21666. return ( Math.max( width, 8 ) * Math.max( height, 8 ) ) / 2;
  21667. // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_etc/
  21668. case RGB_ETC1_Format:
  21669. case RGB_ETC2_Format:
  21670. return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 8;
  21671. case RGBA_ETC2_EAC_Format:
  21672. return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 16;
  21673. // https://registry.khronos.org/webgl/extensions/WEBGL_compressed_texture_astc/
  21674. case RGBA_ASTC_4x4_Format:
  21675. return Math.floor( ( width + 3 ) / 4 ) * Math.floor( ( height + 3 ) / 4 ) * 16;
  21676. case RGBA_ASTC_5x4_Format:
  21677. return Math.floor( ( width + 4 ) / 5 ) * Math.floor( ( height + 3 ) / 4 ) * 16;
  21678. case RGBA_ASTC_5x5_Format:
  21679. return Math.floor( ( width + 4 ) / 5 ) * Math.floor( ( height + 4 ) / 5 ) * 16;
  21680. case RGBA_ASTC_6x5_Format:
  21681. return Math.floor( ( width + 5 ) / 6 ) * Math.floor( ( height + 4 ) / 5 ) * 16;
  21682. case RGBA_ASTC_6x6_Format:
  21683. return Math.floor( ( width + 5 ) / 6 ) * Math.floor( ( height + 5 ) / 6 ) * 16;
  21684. case RGBA_ASTC_8x5_Format:
  21685. return Math.floor( ( width + 7 ) / 8 ) * Math.floor( ( height + 4 ) / 5 ) * 16;
  21686. case RGBA_ASTC_8x6_Format:
  21687. return Math.floor( ( width + 7 ) / 8 ) * Math.floor( ( height + 5 ) / 6 ) * 16;
  21688. case RGBA_ASTC_8x8_Format:
  21689. return Math.floor( ( width + 7 ) / 8 ) * Math.floor( ( height + 7 ) / 8 ) * 16;
  21690. case RGBA_ASTC_10x5_Format:
  21691. return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 4 ) / 5 ) * 16;
  21692. case RGBA_ASTC_10x6_Format:
  21693. return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 5 ) / 6 ) * 16;
  21694. case RGBA_ASTC_10x8_Format:
  21695. return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 7 ) / 8 ) * 16;
  21696. case RGBA_ASTC_10x10_Format:
  21697. return Math.floor( ( width + 9 ) / 10 ) * Math.floor( ( height + 9 ) / 10 ) * 16;
  21698. case RGBA_ASTC_12x10_Format:
  21699. return Math.floor( ( width + 11 ) / 12 ) * Math.floor( ( height + 9 ) / 10 ) * 16;
  21700. case RGBA_ASTC_12x12_Format:
  21701. return Math.floor( ( width + 11 ) / 12 ) * Math.floor( ( height + 11 ) / 12 ) * 16;
  21702. // https://registry.khronos.org/webgl/extensions/EXT_texture_compression_bptc/
  21703. case RGBA_BPTC_Format:
  21704. case RGB_BPTC_SIGNED_Format:
  21705. case RGB_BPTC_UNSIGNED_Format:
  21706. return Math.ceil( width / 4 ) * Math.ceil( height / 4 ) * 16;
  21707. // https://registry.khronos.org/webgl/extensions/EXT_texture_compression_rgtc/
  21708. case RED_RGTC1_Format:
  21709. case SIGNED_RED_RGTC1_Format:
  21710. return Math.ceil( width / 4 ) * Math.ceil( height / 4 ) * 8;
  21711. case RED_GREEN_RGTC2_Format:
  21712. case SIGNED_RED_GREEN_RGTC2_Format:
  21713. return Math.ceil( width / 4 ) * Math.ceil( height / 4 ) * 16;
  21714. }
  21715. throw new Error(
  21716. `Unable to determine texture byte length for ${format} format.`,
  21717. );
  21718. }
  21719. function getTextureTypeByteLength( type ) {
  21720. switch ( type ) {
  21721. case UnsignedByteType:
  21722. case ByteType:
  21723. return { byteLength: 1, components: 1 };
  21724. case UnsignedShortType:
  21725. case ShortType:
  21726. case HalfFloatType:
  21727. return { byteLength: 2, components: 1 };
  21728. case UnsignedShort4444Type:
  21729. case UnsignedShort5551Type:
  21730. return { byteLength: 2, components: 4 };
  21731. case UnsignedIntType:
  21732. case IntType:
  21733. case FloatType:
  21734. return { byteLength: 4, components: 1 };
  21735. case UnsignedInt5999Type:
  21736. return { byteLength: 4, components: 3 };
  21737. }
  21738. throw new Error( `Unknown texture type ${type}.` );
  21739. }
  21740. const TextureUtils = {
  21741. contain,
  21742. cover,
  21743. fill,
  21744. getByteLength
  21745. };
  21746. class WebGLMultipleRenderTargets extends WebGLRenderTarget { // @deprecated, r162
  21747. constructor( width = 1, height = 1, count = 1, options = {} ) {
  21748. console.warn( 'THREE.WebGLMultipleRenderTargets has been deprecated and will be removed in r172. Use THREE.WebGLRenderTarget and set the "count" parameter to enable MRT.' );
  21749. super( width, height, { ...options, count } );
  21750. this.isWebGLMultipleRenderTargets = true;
  21751. }
  21752. get texture() {
  21753. return this.textures;
  21754. }
  21755. }
  21756. if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) {
  21757. __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'register', { detail: {
  21758. revision: REVISION,
  21759. } } ) );
  21760. }
  21761. if ( typeof window !== 'undefined' ) {
  21762. if ( window.__THREE__ ) {
  21763. console.warn( 'WARNING: Multiple instances of Three.js being imported.' );
  21764. } else {
  21765. window.__THREE__ = REVISION;
  21766. }
  21767. }
  21768. 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, 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, WebGLMultipleRenderTargets, WebGLRenderTarget, WebGPUCoordinateSystem, WireframeGeometry, WrapAroundEnding, ZeroCurvatureEnding, ZeroFactor, ZeroSlopeEnding, ZeroStencilOp, arrayNeedsUint32, cloneUniforms, createCanvasElement, createElementNS, getByteLength, getUnlitUniformColorSpace, mergeUniforms, probeAsync, toNormalizedProjectionMatrix, toReversedProjectionMatrix, warnOnce };
粤ICP备19079148号