ThreeWebGL.js 367 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. var THREE = THREE || { REVISION: '49dev' };
  5. if ( ! self.Int32Array ) {
  6. self.Int32Array = Array;
  7. self.Float32Array = Array;
  8. }
  9. // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  10. // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
  11. // requestAnimationFrame polyfill by Erik Möller
  12. // fixes from Paul Irish and Tino Zijdel
  13. (function() {
  14. var lastTime = 0;
  15. var vendors = ['ms', 'moz', 'webkit', 'o'];
  16. for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  17. window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
  18. window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
  19. || window[vendors[x]+'CancelRequestAnimationFrame'];
  20. }
  21. if (!window.requestAnimationFrame)
  22. window.requestAnimationFrame = function(callback, element) {
  23. var currTime = new Date().getTime();
  24. var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  25. var id = window.setTimeout(function() { callback(currTime + timeToCall); },
  26. timeToCall);
  27. lastTime = currTime + timeToCall;
  28. return id;
  29. };
  30. if (!window.cancelAnimationFrame)
  31. window.cancelAnimationFrame = function(id) {
  32. clearTimeout(id);
  33. };
  34. }());
  35. /**
  36. * @author mr.doob / http://mrdoob.com/
  37. */
  38. THREE.Color = function ( hex ) {
  39. if ( hex !== undefined ) this.setHex( hex );
  40. return this;
  41. };
  42. THREE.Color.prototype = {
  43. constructor: THREE.Color,
  44. r: 1, g: 1, b: 1,
  45. copy: function ( color ) {
  46. this.r = color.r;
  47. this.g = color.g;
  48. this.b = color.b;
  49. return this;
  50. },
  51. copyGammaToLinear: function ( color ) {
  52. this.r = color.r * color.r;
  53. this.g = color.g * color.g;
  54. this.b = color.b * color.b;
  55. return this;
  56. },
  57. copyLinearToGamma: function ( color ) {
  58. this.r = Math.sqrt( color.r );
  59. this.g = Math.sqrt( color.g );
  60. this.b = Math.sqrt( color.b );
  61. return this;
  62. },
  63. convertGammaToLinear: function () {
  64. var r = this.r, g = this.g, b = this.b;
  65. this.r = r * r;
  66. this.g = g * g;
  67. this.b = b * b;
  68. return this;
  69. },
  70. convertLinearToGamma: function () {
  71. this.r = Math.sqrt( this.r );
  72. this.g = Math.sqrt( this.g );
  73. this.b = Math.sqrt( this.b );
  74. return this;
  75. },
  76. setRGB: function ( r, g, b ) {
  77. this.r = r;
  78. this.g = g;
  79. this.b = b;
  80. return this;
  81. },
  82. setHSV: function ( h, s, v ) {
  83. // based on MochiKit implementation by Bob Ippolito
  84. // h,s,v ranges are < 0.0 - 1.0 >
  85. var i, f, p, q, t;
  86. if ( v === 0 ) {
  87. this.r = this.g = this.b = 0;
  88. } else {
  89. i = Math.floor( h * 6 );
  90. f = ( h * 6 ) - i;
  91. p = v * ( 1 - s );
  92. q = v * ( 1 - ( s * f ) );
  93. t = v * ( 1 - ( s * ( 1 - f ) ) );
  94. switch ( i ) {
  95. case 1: this.r = q; this.g = v; this.b = p; break;
  96. case 2: this.r = p; this.g = v; this.b = t; break;
  97. case 3: this.r = p; this.g = q; this.b = v; break;
  98. case 4: this.r = t; this.g = p; this.b = v; break;
  99. case 5: this.r = v; this.g = p; this.b = q; break;
  100. case 6: // fall through
  101. case 0: this.r = v; this.g = t; this.b = p; break;
  102. }
  103. }
  104. return this;
  105. },
  106. setHex: function ( hex ) {
  107. hex = Math.floor( hex );
  108. this.r = ( hex >> 16 & 255 ) / 255;
  109. this.g = ( hex >> 8 & 255 ) / 255;
  110. this.b = ( hex & 255 ) / 255;
  111. return this;
  112. },
  113. lerpSelf: function ( color, alpha ) {
  114. this.r += ( color.r - this.r ) * alpha;
  115. this.g += ( color.g - this.g ) * alpha;
  116. this.b += ( color.b - this.b ) * alpha;
  117. return this;
  118. },
  119. getHex: function () {
  120. return Math.floor( this.r * 255 ) << 16 ^ Math.floor( this.g * 255 ) << 8 ^ Math.floor( this.b * 255 );
  121. },
  122. getContextStyle: function () {
  123. return 'rgb(' + Math.floor( this.r * 255 ) + ',' + Math.floor( this.g * 255 ) + ',' + Math.floor( this.b * 255 ) + ')';
  124. },
  125. clone: function () {
  126. return new THREE.Color().setRGB( this.r, this.g, this.b );
  127. }
  128. };
  129. /**
  130. * @author mr.doob / http://mrdoob.com/
  131. * @author philogb / http://blog.thejit.org/
  132. * @author egraether / http://egraether.com/
  133. * @author zz85 / http://www.lab4games.net/zz85/blog
  134. */
  135. THREE.Vector2 = function ( x, y ) {
  136. this.x = x || 0;
  137. this.y = y || 0;
  138. };
  139. THREE.Vector2.prototype = {
  140. constructor: THREE.Vector2,
  141. set: function ( x, y ) {
  142. this.x = x;
  143. this.y = y;
  144. return this;
  145. },
  146. copy: function ( v ) {
  147. this.x = v.x;
  148. this.y = v.y;
  149. return this;
  150. },
  151. add: function ( a, b ) {
  152. this.x = a.x + b.x;
  153. this.y = a.y + b.y;
  154. return this;
  155. },
  156. addSelf: function ( v ) {
  157. this.x += v.x;
  158. this.y += v.y;
  159. return this;
  160. },
  161. sub: function ( a, b ) {
  162. this.x = a.x - b.x;
  163. this.y = a.y - b.y;
  164. return this;
  165. },
  166. subSelf: function ( v ) {
  167. this.x -= v.x;
  168. this.y -= v.y;
  169. return this;
  170. },
  171. multiplyScalar: function ( s ) {
  172. this.x *= s;
  173. this.y *= s;
  174. return this;
  175. },
  176. divideScalar: function ( s ) {
  177. if ( s ) {
  178. this.x /= s;
  179. this.y /= s;
  180. } else {
  181. this.set( 0, 0 );
  182. }
  183. return this;
  184. },
  185. negate: function() {
  186. return this.multiplyScalar( - 1 );
  187. },
  188. dot: function ( v ) {
  189. return this.x * v.x + this.y * v.y;
  190. },
  191. lengthSq: function () {
  192. return this.x * this.x + this.y * this.y;
  193. },
  194. length: function () {
  195. return Math.sqrt( this.lengthSq() );
  196. },
  197. normalize: function () {
  198. return this.divideScalar( this.length() );
  199. },
  200. distanceTo: function ( v ) {
  201. return Math.sqrt( this.distanceToSquared( v ) );
  202. },
  203. distanceToSquared: function ( v ) {
  204. var dx = this.x - v.x, dy = this.y - v.y;
  205. return dx * dx + dy * dy;
  206. },
  207. setLength: function ( l ) {
  208. return this.normalize().multiplyScalar( l );
  209. },
  210. lerpSelf: function ( v, alpha ) {
  211. this.x += ( v.x - this.x ) * alpha;
  212. this.y += ( v.y - this.y ) * alpha;
  213. return this;
  214. },
  215. equals: function( v ) {
  216. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  217. },
  218. isZero: function () {
  219. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  220. },
  221. clone: function () {
  222. return new THREE.Vector2( this.x, this.y );
  223. }
  224. };
  225. /**
  226. * @author mr.doob / http://mrdoob.com/
  227. * @author kile / http://kile.stravaganza.org/
  228. * @author philogb / http://blog.thejit.org/
  229. * @author mikael emtinger / http://gomo.se/
  230. * @author egraether / http://egraether.com/
  231. */
  232. THREE.Vector3 = function ( x, y, z ) {
  233. this.x = x || 0;
  234. this.y = y || 0;
  235. this.z = z || 0;
  236. };
  237. THREE.Vector3.prototype = {
  238. constructor: THREE.Vector3,
  239. set: function ( x, y, z ) {
  240. this.x = x;
  241. this.y = y;
  242. this.z = z;
  243. return this;
  244. },
  245. setX: function ( x ) {
  246. this.x = x;
  247. return this;
  248. },
  249. setY: function ( y ) {
  250. this.y = y;
  251. return this;
  252. },
  253. setZ: function ( z ) {
  254. this.z = z;
  255. return this;
  256. },
  257. copy: function ( v ) {
  258. this.x = v.x;
  259. this.y = v.y;
  260. this.z = v.z;
  261. return this;
  262. },
  263. add: function ( a, b ) {
  264. this.x = a.x + b.x;
  265. this.y = a.y + b.y;
  266. this.z = a.z + b.z;
  267. return this;
  268. },
  269. addSelf: function ( v ) {
  270. this.x += v.x;
  271. this.y += v.y;
  272. this.z += v.z;
  273. return this;
  274. },
  275. addScalar: function ( s ) {
  276. this.x += s;
  277. this.y += s;
  278. this.z += s;
  279. return this;
  280. },
  281. sub: function ( a, b ) {
  282. this.x = a.x - b.x;
  283. this.y = a.y - b.y;
  284. this.z = a.z - b.z;
  285. return this;
  286. },
  287. subSelf: function ( v ) {
  288. this.x -= v.x;
  289. this.y -= v.y;
  290. this.z -= v.z;
  291. return this;
  292. },
  293. multiply: function ( a, b ) {
  294. this.x = a.x * b.x;
  295. this.y = a.y * b.y;
  296. this.z = a.z * b.z;
  297. return this;
  298. },
  299. multiplySelf: function ( v ) {
  300. this.x *= v.x;
  301. this.y *= v.y;
  302. this.z *= v.z;
  303. return this;
  304. },
  305. multiplyScalar: function ( s ) {
  306. this.x *= s;
  307. this.y *= s;
  308. this.z *= s;
  309. return this;
  310. },
  311. divideSelf: function ( v ) {
  312. this.x /= v.x;
  313. this.y /= v.y;
  314. this.z /= v.z;
  315. return this;
  316. },
  317. divideScalar: function ( s ) {
  318. if ( s ) {
  319. this.x /= s;
  320. this.y /= s;
  321. this.z /= s;
  322. } else {
  323. this.x = 0;
  324. this.y = 0;
  325. this.z = 0;
  326. }
  327. return this;
  328. },
  329. negate: function() {
  330. return this.multiplyScalar( - 1 );
  331. },
  332. dot: function ( v ) {
  333. return this.x * v.x + this.y * v.y + this.z * v.z;
  334. },
  335. lengthSq: function () {
  336. return this.x * this.x + this.y * this.y + this.z * this.z;
  337. },
  338. length: function () {
  339. return Math.sqrt( this.lengthSq() );
  340. },
  341. lengthManhattan: function () {
  342. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  343. },
  344. normalize: function () {
  345. return this.divideScalar( this.length() );
  346. },
  347. setLength: function ( l ) {
  348. return this.normalize().multiplyScalar( l );
  349. },
  350. lerpSelf: function ( v, alpha ) {
  351. this.x += ( v.x - this.x ) * alpha;
  352. this.y += ( v.y - this.y ) * alpha;
  353. this.z += ( v.z - this.z ) * alpha;
  354. return this;
  355. },
  356. cross: function ( a, b ) {
  357. this.x = a.y * b.z - a.z * b.y;
  358. this.y = a.z * b.x - a.x * b.z;
  359. this.z = a.x * b.y - a.y * b.x;
  360. return this;
  361. },
  362. crossSelf: function ( v ) {
  363. var x = this.x, y = this.y, z = this.z;
  364. this.x = y * v.z - z * v.y;
  365. this.y = z * v.x - x * v.z;
  366. this.z = x * v.y - y * v.x;
  367. return this;
  368. },
  369. distanceTo: function ( v ) {
  370. return Math.sqrt( this.distanceToSquared( v ) );
  371. },
  372. distanceToSquared: function ( v ) {
  373. return new THREE.Vector3().sub( this, v ).lengthSq();
  374. },
  375. getPositionFromMatrix: function ( m ) {
  376. this.x = m.elements[12];
  377. this.y = m.elements[13];
  378. this.z = m.elements[14];
  379. return this;
  380. },
  381. getRotationFromMatrix: function ( m, scale ) {
  382. var sx = scale ? scale.x : 1;
  383. var sy = scale ? scale.y : 1;
  384. var sz = scale ? scale.z : 1;
  385. var m11 = m.elements[0] / sx, m12 = m.elements[4] / sy, m13 = m.elements[8] / sz;
  386. var m21 = m.elements[1] / sx, m22 = m.elements[5] / sy, m23 = m.elements[9] / sz;
  387. var m33 = m.elements[10] / sz;
  388. this.y = Math.asin( m13 );
  389. var cosY = Math.cos( this.y );
  390. if ( Math.abs( cosY ) > 0.00001 ) {
  391. this.x = Math.atan2( - m23 / cosY, m33 / cosY );
  392. this.z = Math.atan2( - m12 / cosY, m11 / cosY );
  393. } else {
  394. this.x = 0;
  395. this.z = Math.atan2( m21, m22 );
  396. }
  397. return this;
  398. },
  399. /*
  400. // from http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  401. // order XYZ
  402. getEulerXYZFromQuaternion: function ( q ) {
  403. this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z ) );
  404. this.y = Math.asin( 2 * ( q.x * q.z + q.y * q.w ) );
  405. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z ) );
  406. },
  407. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
  408. // order YZX (assuming heading == y, attitude == z, bank == x)
  409. getEulerYZXFromQuaternion: function ( q ) {
  410. var sqw = q.w * q.w;
  411. var sqx = q.x * q.x;
  412. var sqy = q.y * q.y;
  413. var sqz = q.z * q.z;
  414. var unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
  415. var test = q.x * q.y + q.z * q.w;
  416. if ( test > 0.499 * unit ) { // singularity at north pole
  417. this.y = 2 * Math.atan2( q.x, q.w );
  418. this.z = Math.PI / 2;
  419. this.x = 0;
  420. return;
  421. }
  422. if ( test < -0.499 * unit ) { // singularity at south pole
  423. this.y = -2 * Math.atan2( q.x, q.w );
  424. this.z = -Math.PI / 2;
  425. this.x = 0;
  426. return;
  427. }
  428. this.y = Math.atan2( 2 * q.y * q.w - 2 * q.x * q.z, sqx - sqy - sqz + sqw );
  429. this.z = Math.asin( 2 * test / unit );
  430. this.x = Math.atan2( 2 * q.x * q.w - 2 * q.y * q.z, -sqx + sqy - sqz + sqw );
  431. },
  432. */
  433. getScaleFromMatrix: function ( m ) {
  434. var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  435. var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  436. var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  437. this.x = sx;
  438. this.y = sy;
  439. this.z = sz;
  440. },
  441. equals: function ( v ) {
  442. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  443. },
  444. isZero: function () {
  445. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  446. },
  447. clone: function () {
  448. return new THREE.Vector3( this.x, this.y, this.z );
  449. }
  450. };
  451. /**
  452. * @author supereggbert / http://www.paulbrunt.co.uk/
  453. * @author philogb / http://blog.thejit.org/
  454. * @author mikael emtinger / http://gomo.se/
  455. * @author egraether / http://egraether.com/
  456. */
  457. THREE.Vector4 = function ( x, y, z, w ) {
  458. this.x = x || 0;
  459. this.y = y || 0;
  460. this.z = z || 0;
  461. this.w = ( w !== undefined ) ? w : 1;
  462. };
  463. THREE.Vector4.prototype = {
  464. constructor: THREE.Vector4,
  465. set: function ( x, y, z, w ) {
  466. this.x = x;
  467. this.y = y;
  468. this.z = z;
  469. this.w = w;
  470. return this;
  471. },
  472. copy: function ( v ) {
  473. this.x = v.x;
  474. this.y = v.y;
  475. this.z = v.z;
  476. this.w = ( v.w !== undefined ) ? v.w : 1;
  477. return this;
  478. },
  479. add: function ( a, b ) {
  480. this.x = a.x + b.x;
  481. this.y = a.y + b.y;
  482. this.z = a.z + b.z;
  483. this.w = a.w + b.w;
  484. return this;
  485. },
  486. addSelf: function ( v ) {
  487. this.x += v.x;
  488. this.y += v.y;
  489. this.z += v.z;
  490. this.w += v.w;
  491. return this;
  492. },
  493. sub: function ( a, b ) {
  494. this.x = a.x - b.x;
  495. this.y = a.y - b.y;
  496. this.z = a.z - b.z;
  497. this.w = a.w - b.w;
  498. return this;
  499. },
  500. subSelf: function ( v ) {
  501. this.x -= v.x;
  502. this.y -= v.y;
  503. this.z -= v.z;
  504. this.w -= v.w;
  505. return this;
  506. },
  507. multiplyScalar: function ( s ) {
  508. this.x *= s;
  509. this.y *= s;
  510. this.z *= s;
  511. this.w *= s;
  512. return this;
  513. },
  514. divideScalar: function ( s ) {
  515. if ( s ) {
  516. this.x /= s;
  517. this.y /= s;
  518. this.z /= s;
  519. this.w /= s;
  520. } else {
  521. this.x = 0;
  522. this.y = 0;
  523. this.z = 0;
  524. this.w = 1;
  525. }
  526. return this;
  527. },
  528. negate: function() {
  529. return this.multiplyScalar( -1 );
  530. },
  531. dot: function ( v ) {
  532. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  533. },
  534. lengthSq: function () {
  535. return this.dot( this );
  536. },
  537. length: function () {
  538. return Math.sqrt( this.lengthSq() );
  539. },
  540. normalize: function () {
  541. return this.divideScalar( this.length() );
  542. },
  543. setLength: function ( l ) {
  544. return this.normalize().multiplyScalar( l );
  545. },
  546. lerpSelf: function ( v, alpha ) {
  547. this.x += ( v.x - this.x ) * alpha;
  548. this.y += ( v.y - this.y ) * alpha;
  549. this.z += ( v.z - this.z ) * alpha;
  550. this.w += ( v.w - this.w ) * alpha;
  551. return this;
  552. },
  553. clone: function () {
  554. return new THREE.Vector4( this.x, this.y, this.z, this.w );
  555. }
  556. };
  557. /**
  558. * @author mrdoob / http://mrdoob.com/
  559. * @author alteredq / http://alteredqualia.com/
  560. */
  561. THREE.Frustum = function ( ) {
  562. this.planes = [
  563. new THREE.Vector4(),
  564. new THREE.Vector4(),
  565. new THREE.Vector4(),
  566. new THREE.Vector4(),
  567. new THREE.Vector4(),
  568. new THREE.Vector4()
  569. ];
  570. };
  571. THREE.Frustum.prototype.setFromMatrix = function ( m ) {
  572. var i, plane,
  573. planes = this.planes;
  574. planes[ 0 ].set( m.elements[3] - m.elements[0], m.elements[7] - m.elements[4], m.elements[11] - m.elements[8], m.elements[15] - m.elements[12] );
  575. planes[ 1 ].set( m.elements[3] + m.elements[0], m.elements[7] + m.elements[4], m.elements[11] + m.elements[8], m.elements[15] + m.elements[12] );
  576. planes[ 2 ].set( m.elements[3] + m.elements[1], m.elements[7] + m.elements[5], m.elements[11] + m.elements[9], m.elements[15] + m.elements[13] );
  577. planes[ 3 ].set( m.elements[3] - m.elements[1], m.elements[7] - m.elements[5], m.elements[11] - m.elements[9], m.elements[15] - m.elements[13] );
  578. planes[ 4 ].set( m.elements[3] - m.elements[2], m.elements[7] - m.elements[6], m.elements[11] - m.elements[10], m.elements[15] - m.elements[14] );
  579. planes[ 5 ].set( m.elements[3] + m.elements[2], m.elements[7] + m.elements[6], m.elements[11] + m.elements[10], m.elements[15] + m.elements[14] );
  580. for ( i = 0; i < 6; i ++ ) {
  581. plane = planes[ i ];
  582. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  583. }
  584. };
  585. THREE.Frustum.prototype.contains = function ( object ) {
  586. var distance,
  587. planes = this.planes,
  588. matrix = object.matrixWorld,
  589. scale = THREE.Frustum.__v1.set( matrix.getColumnX().length(), matrix.getColumnY().length(), matrix.getColumnZ().length() ),
  590. radius = - object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) );
  591. for ( var i = 0; i < 6; i ++ ) {
  592. distance = planes[ i ].x * matrix.elements[12] + planes[ i ].y * matrix.elements[13] + planes[ i ].z * matrix.elements[14] + planes[ i ].w;
  593. if ( distance <= radius ) return false;
  594. }
  595. return true;
  596. };
  597. THREE.Frustum.__v1 = new THREE.Vector3();
  598. /**
  599. * @author mr.doob / http://mrdoob.com/
  600. */
  601. THREE.Ray = function ( origin, direction ) {
  602. this.origin = origin || new THREE.Vector3();
  603. this.direction = direction || new THREE.Vector3();
  604. var precision = 0.0001;
  605. this.setPrecision = function ( value ) {
  606. precision = value;
  607. };
  608. var a = new THREE.Vector3();
  609. var b = new THREE.Vector3();
  610. var c = new THREE.Vector3();
  611. var d = new THREE.Vector3();
  612. var originCopy = new THREE.Vector3();
  613. var directionCopy = new THREE.Vector3();
  614. var vector = new THREE.Vector3();
  615. var normal = new THREE.Vector3();
  616. var intersectPoint = new THREE.Vector3()
  617. this.intersectObject = function ( object ) {
  618. var intersect, intersects = [];
  619. if ( object instanceof THREE.Particle ) {
  620. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  621. if ( distance > object.scale.x ) {
  622. return [];
  623. }
  624. intersect = {
  625. distance: distance,
  626. point: object.position,
  627. face: null,
  628. object: object
  629. };
  630. intersects.push( intersect );
  631. } else if ( object instanceof THREE.Mesh ) {
  632. // Checking boundingSphere
  633. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  634. var scale = THREE.Frustum.__v1.set( object.matrixWorld.getColumnX().length(), object.matrixWorld.getColumnY().length(), object.matrixWorld.getColumnZ().length() );
  635. if ( distance > object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) ) ) {
  636. return intersects;
  637. }
  638. // Checking faces
  639. var f, fl, face, dot, scalar,
  640. geometry = object.geometry,
  641. vertices = geometry.vertices,
  642. objMatrix;
  643. object.matrixRotationWorld.extractRotation( object.matrixWorld );
  644. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  645. face = geometry.faces[ f ];
  646. originCopy.copy( this.origin );
  647. directionCopy.copy( this.direction );
  648. objMatrix = object.matrixWorld;
  649. // determine if ray intersects the plane of the face
  650. // note: this works regardless of the direction of the face normal
  651. vector = objMatrix.multiplyVector3( vector.copy( face.centroid ) ).subSelf( originCopy );
  652. normal = object.matrixRotationWorld.multiplyVector3( normal.copy( face.normal ) );
  653. dot = directionCopy.dot( normal );
  654. // bail if ray and plane are parallel
  655. if ( Math.abs( dot ) < precision ) continue;
  656. // calc distance to plane
  657. scalar = normal.dot( vector ) / dot;
  658. // if negative distance, then plane is behind ray
  659. if ( scalar < 0 ) continue;
  660. if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) {
  661. intersectPoint.add( originCopy, directionCopy.multiplyScalar( scalar ) );
  662. if ( face instanceof THREE.Face3 ) {
  663. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ].position ) );
  664. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ].position ) );
  665. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ].position ) );
  666. if ( pointInFace3( intersectPoint, a, b, c ) ) {
  667. intersect = {
  668. distance: originCopy.distanceTo( intersectPoint ),
  669. point: intersectPoint.clone(),
  670. face: face,
  671. object: object
  672. };
  673. intersects.push( intersect );
  674. }
  675. } else if ( face instanceof THREE.Face4 ) {
  676. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ].position ) );
  677. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ].position ) );
  678. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ].position ) );
  679. d = objMatrix.multiplyVector3( d.copy( vertices[ face.d ].position ) );
  680. if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
  681. intersect = {
  682. distance: originCopy.distanceTo( intersectPoint ),
  683. point: intersectPoint.clone(),
  684. face: face,
  685. object: object
  686. };
  687. intersects.push( intersect );
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return intersects;
  694. }
  695. this.intersectObjects = function ( objects ) {
  696. var intersects = [];
  697. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  698. Array.prototype.push.apply( intersects, this.intersectObject( objects[ i ] ) );
  699. }
  700. intersects.sort( function ( a, b ) { return a.distance - b.distance; } );
  701. return intersects;
  702. };
  703. var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
  704. var dot, intersect, distance;
  705. function distanceFromIntersection( origin, direction, position ) {
  706. v0.sub( position, origin );
  707. dot = v0.dot( direction );
  708. intersect = v1.add( origin, v2.copy( direction ).multiplyScalar( dot ) );
  709. distance = position.distanceTo( intersect );
  710. return distance;
  711. }
  712. // http://www.blackpawn.com/texts/pointinpoly/default.html
  713. var dot00, dot01, dot02, dot11, dot12, invDenom, u, v;
  714. function pointInFace3( p, a, b, c ) {
  715. v0.sub( c, a );
  716. v1.sub( b, a );
  717. v2.sub( p, a );
  718. dot00 = v0.dot( v0 );
  719. dot01 = v0.dot( v1 );
  720. dot02 = v0.dot( v2 );
  721. dot11 = v1.dot( v1 );
  722. dot12 = v1.dot( v2 );
  723. invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 );
  724. u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  725. v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  726. return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 );
  727. }
  728. };/**
  729. * @author mr.doob / http://mrdoob.com/
  730. */
  731. THREE.Rectangle = function () {
  732. var _left, _top, _right, _bottom,
  733. _width, _height, _isEmpty = true;
  734. function resize() {
  735. _width = _right - _left;
  736. _height = _bottom - _top;
  737. }
  738. this.getX = function () {
  739. return _left;
  740. };
  741. this.getY = function () {
  742. return _top;
  743. };
  744. this.getWidth = function () {
  745. return _width;
  746. };
  747. this.getHeight = function () {
  748. return _height;
  749. };
  750. this.getLeft = function() {
  751. return _left;
  752. };
  753. this.getTop = function() {
  754. return _top;
  755. };
  756. this.getRight = function() {
  757. return _right;
  758. };
  759. this.getBottom = function() {
  760. return _bottom;
  761. };
  762. this.set = function ( left, top, right, bottom ) {
  763. _isEmpty = false;
  764. _left = left; _top = top;
  765. _right = right; _bottom = bottom;
  766. resize();
  767. };
  768. this.addPoint = function ( x, y ) {
  769. if ( _isEmpty ) {
  770. _isEmpty = false;
  771. _left = x; _top = y;
  772. _right = x; _bottom = y;
  773. resize();
  774. } else {
  775. _left = _left < x ? _left : x; // Math.min( _left, x );
  776. _top = _top < y ? _top : y; // Math.min( _top, y );
  777. _right = _right > x ? _right : x; // Math.max( _right, x );
  778. _bottom = _bottom > y ? _bottom : y; // Math.max( _bottom, y );
  779. resize();
  780. }
  781. };
  782. this.add3Points = function ( x1, y1, x2, y2, x3, y3 ) {
  783. if (_isEmpty) {
  784. _isEmpty = false;
  785. _left = x1 < x2 ? ( x1 < x3 ? x1 : x3 ) : ( x2 < x3 ? x2 : x3 );
  786. _top = y1 < y2 ? ( y1 < y3 ? y1 : y3 ) : ( y2 < y3 ? y2 : y3 );
  787. _right = x1 > x2 ? ( x1 > x3 ? x1 : x3 ) : ( x2 > x3 ? x2 : x3 );
  788. _bottom = y1 > y2 ? ( y1 > y3 ? y1 : y3 ) : ( y2 > y3 ? y2 : y3 );
  789. resize();
  790. } else {
  791. _left = x1 < x2 ? ( x1 < x3 ? ( x1 < _left ? x1 : _left ) : ( x3 < _left ? x3 : _left ) ) : ( x2 < x3 ? ( x2 < _left ? x2 : _left ) : ( x3 < _left ? x3 : _left ) );
  792. _top = y1 < y2 ? ( y1 < y3 ? ( y1 < _top ? y1 : _top ) : ( y3 < _top ? y3 : _top ) ) : ( y2 < y3 ? ( y2 < _top ? y2 : _top ) : ( y3 < _top ? y3 : _top ) );
  793. _right = x1 > x2 ? ( x1 > x3 ? ( x1 > _right ? x1 : _right ) : ( x3 > _right ? x3 : _right ) ) : ( x2 > x3 ? ( x2 > _right ? x2 : _right ) : ( x3 > _right ? x3 : _right ) );
  794. _bottom = y1 > y2 ? ( y1 > y3 ? ( y1 > _bottom ? y1 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) ) : ( y2 > y3 ? ( y2 > _bottom ? y2 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) );
  795. resize();
  796. };
  797. };
  798. this.addRectangle = function ( r ) {
  799. if ( _isEmpty ) {
  800. _isEmpty = false;
  801. _left = r.getLeft(); _top = r.getTop();
  802. _right = r.getRight(); _bottom = r.getBottom();
  803. resize();
  804. } else {
  805. _left = _left < r.getLeft() ? _left : r.getLeft(); // Math.min(_left, r.getLeft() );
  806. _top = _top < r.getTop() ? _top : r.getTop(); // Math.min(_top, r.getTop() );
  807. _right = _right > r.getRight() ? _right : r.getRight(); // Math.max(_right, r.getRight() );
  808. _bottom = _bottom > r.getBottom() ? _bottom : r.getBottom(); // Math.max(_bottom, r.getBottom() );
  809. resize();
  810. }
  811. };
  812. this.inflate = function ( v ) {
  813. _left -= v; _top -= v;
  814. _right += v; _bottom += v;
  815. resize();
  816. };
  817. this.minSelf = function ( r ) {
  818. _left = _left > r.getLeft() ? _left : r.getLeft(); // Math.max( _left, r.getLeft() );
  819. _top = _top > r.getTop() ? _top : r.getTop(); // Math.max( _top, r.getTop() );
  820. _right = _right < r.getRight() ? _right : r.getRight(); // Math.min( _right, r.getRight() );
  821. _bottom = _bottom < r.getBottom() ? _bottom : r.getBottom(); // Math.min( _bottom, r.getBottom() );
  822. resize();
  823. };
  824. this.intersects = function ( r ) {
  825. // http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/
  826. if ( _right < r.getLeft() ) return false;
  827. if ( _left > r.getRight() ) return false;
  828. if ( _bottom < r.getTop() ) return false;
  829. if ( _top > r.getBottom() ) return false;
  830. return true;
  831. };
  832. this.empty = function () {
  833. _isEmpty = true;
  834. _left = 0; _top = 0;
  835. _right = 0; _bottom = 0;
  836. resize();
  837. };
  838. this.isEmpty = function () {
  839. return _isEmpty;
  840. };
  841. };
  842. /**
  843. * @author alteredq / http://alteredqualia.com/
  844. */
  845. THREE.Math = {
  846. // Clamp value to range <a, b>
  847. clamp: function ( x, a, b ) {
  848. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  849. },
  850. // Clamp value to range <a, inf)
  851. clampBottom: function ( x, a ) {
  852. return x < a ? a : x;
  853. },
  854. // Linear mapping from range <a1, a2> to range <b1, b2>
  855. mapLinear: function ( x, a1, a2, b1, b2 ) {
  856. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  857. },
  858. // Random float from <0, 1> with 16 bits of randomness
  859. // (standard Math.random() creates repetitive patterns when applied over larger space)
  860. random16: function () {
  861. return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
  862. },
  863. // Random integer from <low, high> interval
  864. randInt: function ( low, high ) {
  865. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  866. },
  867. // Random float from <low, high> interval
  868. randFloat: function ( low, high ) {
  869. return low + Math.random() * ( high - low );
  870. },
  871. // Random float from <-range/2, range/2> interval
  872. randFloatSpread: function ( range ) {
  873. return range * ( 0.5 - Math.random() );
  874. },
  875. sign: function ( x ) {
  876. return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
  877. }
  878. };
  879. /**
  880. * @author alteredq / http://alteredqualia.com/
  881. */
  882. THREE.Matrix3 = function () {
  883. this.m = [];
  884. };
  885. THREE.Matrix3.prototype = {
  886. constructor: THREE.Matrix3,
  887. getInverse: function ( matrix ) {
  888. // input: THREE.Matrix4
  889. // ( based on http://code.google.com/p/webgl-mjs/ )
  890. var a11 = matrix.elements[10] * matrix.elements[5] - matrix.elements[6] * matrix.elements[9];
  891. var a21 = - matrix.elements[10] * matrix.elements[1] + matrix.elements[2] * matrix.elements[9];
  892. var a31 = matrix.elements[6] * matrix.elements[1] - matrix.elements[2] * matrix.elements[5];
  893. var a12 = - matrix.elements[10] * matrix.elements[4] + matrix.elements[6] * matrix.elements[8];
  894. var a22 = matrix.elements[10] * matrix.elements[0] - matrix.elements[2] * matrix.elements[8];
  895. var a32 = - matrix.elements[6] * matrix.elements[0] + matrix.elements[2] * matrix.elements[4];
  896. var a13 = matrix.elements[9] * matrix.elements[4] - matrix.elements[5] * matrix.elements[8];
  897. var a23 = - matrix.elements[9] * matrix.elements[0] + matrix.elements[1] * matrix.elements[8];
  898. var a33 = matrix.elements[5] * matrix.elements[0] - matrix.elements[1] * matrix.elements[4];
  899. var det = matrix.elements[0] * a11 + matrix.elements[1] * a12 + matrix.elements[2] * a13;
  900. // no inverse
  901. if ( det === 0 ) {
  902. console.warn( "Matrix3.getInverse(): determinant == 0" );
  903. }
  904. var idet = 1.0 / det;
  905. var m = this.m;
  906. m[ 0 ] = idet * a11; m[ 1 ] = idet * a21; m[ 2 ] = idet * a31;
  907. m[ 3 ] = idet * a12; m[ 4 ] = idet * a22; m[ 5 ] = idet * a32;
  908. m[ 6 ] = idet * a13; m[ 7 ] = idet * a23; m[ 8 ] = idet * a33;
  909. return this;
  910. },
  911. /*
  912. transpose: function () {
  913. var tmp, m = this.m;
  914. tmp = m[1]; m[1] = m[3]; m[3] = tmp;
  915. tmp = m[2]; m[2] = m[6]; m[6] = tmp;
  916. tmp = m[5]; m[5] = m[7]; m[7] = tmp;
  917. return this;
  918. },
  919. */
  920. transposeIntoArray: function ( r ) {
  921. var m = this.m;
  922. r[ 0 ] = m[ 0 ];
  923. r[ 1 ] = m[ 3 ];
  924. r[ 2 ] = m[ 6 ];
  925. r[ 3 ] = m[ 1 ];
  926. r[ 4 ] = m[ 4 ];
  927. r[ 5 ] = m[ 7 ];
  928. r[ 6 ] = m[ 2 ];
  929. r[ 7 ] = m[ 5 ];
  930. r[ 8 ] = m[ 8 ];
  931. return this;
  932. }
  933. };
  934. /**
  935. * @author mr.doob / http://mrdoob.com/
  936. * @author supereggbert / http://www.paulbrunt.co.uk/
  937. * @author philogb / http://blog.thejit.org/
  938. * @author jordi_ros / http://plattsoft.com
  939. * @author D1plo1d / http://github.com/D1plo1d
  940. * @author alteredq / http://alteredqualia.com/
  941. * @author mikael emtinger / http://gomo.se/
  942. * @author timknip / http://www.floorplanner.com/
  943. */
  944. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  945. this.elements = new Float32Array(16);
  946. this.set(
  947. ( n11 !== undefined ) ? n11 : 1, n12 || 0, n13 || 0, n14 || 0,
  948. n21 || 0, ( n22 !== undefined ) ? n22 : 1, n23 || 0, n24 || 0,
  949. n31 || 0, n32 || 0, ( n33 !== undefined ) ? n33 : 1, n34 || 0,
  950. n41 || 0, n42 || 0, n43 || 0, ( n44 !== undefined ) ? n44 : 1
  951. );
  952. };
  953. THREE.Matrix4.prototype = {
  954. constructor: THREE.Matrix4,
  955. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  956. var te = this.elements;
  957. te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
  958. te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
  959. te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
  960. te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
  961. return this;
  962. },
  963. identity: function () {
  964. this.set(
  965. 1, 0, 0, 0,
  966. 0, 1, 0, 0,
  967. 0, 0, 1, 0,
  968. 0, 0, 0, 1
  969. );
  970. return this;
  971. },
  972. copy: function ( m ) {
  973. this.set(
  974. m.elements[0], m.elements[4], m.elements[8], m.elements[12],
  975. m.elements[1], m.elements[5], m.elements[9], m.elements[13],
  976. m.elements[2], m.elements[6], m.elements[10], m.elements[14],
  977. m.elements[3], m.elements[7], m.elements[11], m.elements[15]
  978. );
  979. return this;
  980. },
  981. lookAt: function ( eye, target, up ) {
  982. var te = this.elements;
  983. var x = THREE.Matrix4.__v1;
  984. var y = THREE.Matrix4.__v2;
  985. var z = THREE.Matrix4.__v3;
  986. z.sub( eye, target ).normalize();
  987. if ( z.length() === 0 ) {
  988. z.z = 1;
  989. }
  990. x.cross( up, z ).normalize();
  991. if ( x.length() === 0 ) {
  992. z.x += 0.0001;
  993. x.cross( up, z ).normalize();
  994. }
  995. y.cross( z, x );
  996. te[0] = x.x; te[4] = y.x; te[8] = z.x;
  997. te[1] = x.y; te[5] = y.y; te[9] = z.y;
  998. te[2] = x.z; te[6] = y.z; te[10] = z.z;
  999. return this;
  1000. },
  1001. multiply: function ( a, b ) {
  1002. var ae = a.elements,
  1003. be = b.elements,
  1004. te = this.elements;
  1005. var a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
  1006. var a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
  1007. var a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
  1008. var a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
  1009. var b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
  1010. var b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
  1011. var b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
  1012. var b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
  1013. te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  1014. te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  1015. te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  1016. te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  1017. te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  1018. te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  1019. te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  1020. te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  1021. te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  1022. te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  1023. te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  1024. te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  1025. te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  1026. te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  1027. te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  1028. te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  1029. return this;
  1030. },
  1031. multiplySelf: function ( m ) {
  1032. return this.multiply( this, m );
  1033. },
  1034. multiplyToArray: function ( a, b, r ) {
  1035. var te = this.elements;
  1036. this.multiply( a, b );
  1037. r[ 0 ] = te[0]; r[ 1 ] = te[1]; r[ 2 ] = te[2]; r[ 3 ] = te[3];
  1038. r[ 4 ] = te[4]; r[ 5 ] = te[5]; r[ 6 ] = te[6]; r[ 7 ] = te[7];
  1039. r[ 8 ] = te[8]; r[ 9 ] = te[9]; r[ 10 ] = te[10]; r[ 11 ] = te[11];
  1040. r[ 12 ] = te[12]; r[ 13 ] = te[13]; r[ 14 ] = te[14]; r[ 15 ] = te[15];
  1041. return this;
  1042. },
  1043. multiplyScalar: function ( s ) {
  1044. var te = this.elements;
  1045. te[0] *= s; te[4] *= s; te[8] *= s; te[12] *= s;
  1046. te[1] *= s; te[5] *= s; te[9] *= s; te[13] *= s;
  1047. te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s;
  1048. te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s;
  1049. return this;
  1050. },
  1051. multiplyVector3: function ( v ) {
  1052. var te = this.elements;
  1053. var vx = v.x, vy = v.y, vz = v.z;
  1054. var d = 1 / ( te[3] * vx + te[7] * vy + te[11] * vz + te[15] );
  1055. v.x = ( te[0] * vx + te[4] * vy + te[8] * vz + te[12] ) * d;
  1056. v.y = ( te[1] * vx + te[5] * vy + te[9] * vz + te[13] ) * d;
  1057. v.z = ( te[2] * vx + te[6] * vy + te[10] * vz + te[14] ) * d;
  1058. return v;
  1059. },
  1060. multiplyVector4: function ( v ) {
  1061. var te = this.elements;
  1062. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  1063. v.x = te[0] * vx + te[4] * vy + te[8] * vz + te[12] * vw;
  1064. v.y = te[1] * vx + te[5] * vy + te[9] * vz + te[13] * vw;
  1065. v.z = te[2] * vx + te[6] * vy + te[10] * vz + te[14] * vw;
  1066. v.w = te[3] * vx + te[7] * vy + te[11] * vz + te[15] * vw;
  1067. return v;
  1068. },
  1069. rotateAxis: function ( v ) {
  1070. var te = this.elements;
  1071. var vx = v.x, vy = v.y, vz = v.z;
  1072. v.x = vx * te[0] + vy * te[4] + vz * te[8];
  1073. v.y = vx * te[1] + vy * te[5] + vz * te[9];
  1074. v.z = vx * te[2] + vy * te[6] + vz * te[10];
  1075. v.normalize();
  1076. return v;
  1077. },
  1078. crossVector: function ( a ) {
  1079. var te = this.elements;
  1080. var v = new THREE.Vector4();
  1081. v.x = te[0] * a.x + te[4] * a.y + te[8] * a.z + te[12] * a.w;
  1082. v.y = te[1] * a.x + te[5] * a.y + te[9] * a.z + te[13] * a.w;
  1083. v.z = te[2] * a.x + te[6] * a.y + te[10] * a.z + te[14] * a.w;
  1084. v.w = ( a.w ) ? te[3] * a.x + te[7] * a.y + te[11] * a.z + te[15] * a.w : 1;
  1085. return v;
  1086. },
  1087. determinant: function () {
  1088. var te = this.elements;
  1089. var n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12];
  1090. var n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13];
  1091. var n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14];
  1092. var n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15];
  1093. //TODO: make this more efficient
  1094. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  1095. return (
  1096. n14 * n23 * n32 * n41-
  1097. n13 * n24 * n32 * n41-
  1098. n14 * n22 * n33 * n41+
  1099. n12 * n24 * n33 * n41+
  1100. n13 * n22 * n34 * n41-
  1101. n12 * n23 * n34 * n41-
  1102. n14 * n23 * n31 * n42+
  1103. n13 * n24 * n31 * n42+
  1104. n14 * n21 * n33 * n42-
  1105. n11 * n24 * n33 * n42-
  1106. n13 * n21 * n34 * n42+
  1107. n11 * n23 * n34 * n42+
  1108. n14 * n22 * n31 * n43-
  1109. n12 * n24 * n31 * n43-
  1110. n14 * n21 * n32 * n43+
  1111. n11 * n24 * n32 * n43+
  1112. n12 * n21 * n34 * n43-
  1113. n11 * n22 * n34 * n43-
  1114. n13 * n22 * n31 * n44+
  1115. n12 * n23 * n31 * n44+
  1116. n13 * n21 * n32 * n44-
  1117. n11 * n23 * n32 * n44-
  1118. n12 * n21 * n33 * n44+
  1119. n11 * n22 * n33 * n44
  1120. );
  1121. },
  1122. transpose: function () {
  1123. var te = this.elements;
  1124. var tmp;
  1125. tmp = te[1]; te[1] = te[4]; te[4] = tmp;
  1126. tmp = te[2]; te[2] = te[8]; te[8] = tmp;
  1127. tmp = te[6]; te[6] = te[9]; te[9] = tmp;
  1128. tmp = te[3]; te[3] = te[12]; te[12] = tmp;
  1129. tmp = te[7]; te[7] = te[13]; te[13] = tmp;
  1130. tmp = te[11]; te[11] = te[14]; te[14] = tmp;
  1131. return this;
  1132. },
  1133. flattenToArray: function ( flat ) {
  1134. var te = this.elements;
  1135. flat[ 0 ] = te[0]; flat[ 1 ] = te[1]; flat[ 2 ] = te[2]; flat[ 3 ] = te[3];
  1136. flat[ 4 ] = te[4]; flat[ 5 ] = te[5]; flat[ 6 ] = te[6]; flat[ 7 ] = te[7];
  1137. flat[ 8 ] = te[8]; flat[ 9 ] = te[9]; flat[ 10 ] = te[10]; flat[ 11 ] = te[11];
  1138. flat[ 12 ] = te[12]; flat[ 13 ] = te[13]; flat[ 14 ] = te[14]; flat[ 15 ] = te[15];
  1139. return flat;
  1140. },
  1141. flattenToArrayOffset: function( flat, offset ) {
  1142. var te = this.elements;
  1143. flat[ offset ] = te[0];
  1144. flat[ offset + 1 ] = te[1];
  1145. flat[ offset + 2 ] = te[2];
  1146. flat[ offset + 3 ] = te[3];
  1147. flat[ offset + 4 ] = te[4];
  1148. flat[ offset + 5 ] = te[5];
  1149. flat[ offset + 6 ] = te[6];
  1150. flat[ offset + 7 ] = te[7];
  1151. flat[ offset + 8 ] = te[8];
  1152. flat[ offset + 9 ] = te[9];
  1153. flat[ offset + 10 ] = te[10];
  1154. flat[ offset + 11 ] = te[11];
  1155. flat[ offset + 12 ] = te[12];
  1156. flat[ offset + 13 ] = te[13];
  1157. flat[ offset + 14 ] = te[14];
  1158. flat[ offset + 15 ] = te[15];
  1159. return flat;
  1160. },
  1161. getPosition: function () {
  1162. var te = this.elements;
  1163. return THREE.Matrix4.__v1.set( te[12], te[13], te[14] );
  1164. },
  1165. setPosition: function ( v ) {
  1166. var te = this.elements;
  1167. te[12] = v.x;
  1168. te[13] = v.y;
  1169. te[14] = v.z;
  1170. return this;
  1171. },
  1172. getColumnX: function () {
  1173. var te = this.elements;
  1174. return THREE.Matrix4.__v1.set( te[0], te[1], te[2] );
  1175. },
  1176. getColumnY: function () {
  1177. var te = this.elements;
  1178. return THREE.Matrix4.__v1.set( te[4], te[5], te[6] );
  1179. },
  1180. getColumnZ: function() {
  1181. var te = this.elements;
  1182. return THREE.Matrix4.__v1.set( te[8], te[9], te[10] );
  1183. },
  1184. getInverse: function ( m ) {
  1185. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  1186. var te = this.elements;
  1187. var n11 = m.elements[0], n12 = m.elements[4], n13 = m.elements[8], n14 = m.elements[12];
  1188. var n21 = m.elements[1], n22 = m.elements[5], n23 = m.elements[9], n24 = m.elements[13];
  1189. var n31 = m.elements[2], n32 = m.elements[6], n33 = m.elements[10], n34 = m.elements[14];
  1190. var n41 = m.elements[3], n42 = m.elements[7], n43 = m.elements[11], n44 = m.elements[15];
  1191. te[0] = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  1192. te[4] = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  1193. te[8] = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  1194. te[12] = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  1195. te[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  1196. te[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  1197. te[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  1198. te[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  1199. te[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  1200. te[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  1201. te[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  1202. te[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  1203. te[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  1204. te[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  1205. te[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  1206. te[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  1207. this.multiplyScalar( 1 / m.determinant() );
  1208. return this;
  1209. },
  1210. setRotationFromEuler: function( v, order ) {
  1211. var te = this.elements;
  1212. var x = v.x, y = v.y, z = v.z;
  1213. var a = Math.cos( x ), b = Math.sin( x );
  1214. var c = Math.cos( y ), d = Math.sin( y );
  1215. var e = Math.cos( z ), f = Math.sin( z );
  1216. switch ( order ) {
  1217. case 'YXZ':
  1218. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  1219. te[0] = ce + df * b;
  1220. te[4] = de * b - cf;
  1221. te[8] = a * d;
  1222. te[1] = a * f;
  1223. te[5] = a * e;
  1224. te[9] = - b;
  1225. te[2] = cf * b - de;
  1226. te[6] = df + ce * b;
  1227. te[10] = a * c;
  1228. break;
  1229. case 'ZXY':
  1230. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  1231. te[0] = ce - df * b;
  1232. te[4] = - a * f;
  1233. te[8] = de + cf * b;
  1234. te[1] = cf + de * b;
  1235. te[5] = a * e;
  1236. te[9] = df - ce * b;
  1237. te[2] = - a * d;
  1238. te[6] = b;
  1239. te[10] = a * c;
  1240. break;
  1241. case 'ZYX':
  1242. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  1243. te[0] = c * e;
  1244. te[4] = be * d - af;
  1245. te[8] = ae * d + bf;
  1246. te[1] = c * f;
  1247. te[5] = bf * d + ae;
  1248. te[9] = af * d - be;
  1249. te[2] = - d;
  1250. te[6] = b * c;
  1251. te[10] = a * c;
  1252. break;
  1253. case 'YZX':
  1254. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  1255. te[0] = c * e;
  1256. te[4] = bd - ac * f;
  1257. te[8] = bc * f + ad;
  1258. te[1] = f;
  1259. te[5] = a * e;
  1260. te[9] = - b * e;
  1261. te[2] = - d * e;
  1262. te[6] = ad * f + bc;
  1263. te[10] = ac - bd * f;
  1264. break;
  1265. case 'XZY':
  1266. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  1267. te[0] = c * e;
  1268. te[4] = - f;
  1269. te[8] = d * e;
  1270. te[1] = ac * f + bd;
  1271. te[5] = a * e;
  1272. te[9] = ad * f - bc;
  1273. te[2] = bc * f - ad;
  1274. te[6] = b * e;
  1275. te[10] = bd * f + ac;
  1276. break;
  1277. default: // 'XYZ'
  1278. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  1279. te[0] = c * e;
  1280. te[4] = - c * f;
  1281. te[8] = d;
  1282. te[1] = af + be * d;
  1283. te[5] = ae - bf * d;
  1284. te[9] = - b * c;
  1285. te[2] = bf - ae * d;
  1286. te[6] = be + af * d;
  1287. te[10] = a * c;
  1288. break;
  1289. }
  1290. return this;
  1291. },
  1292. setRotationFromQuaternion: function( q ) {
  1293. var te = this.elements;
  1294. var x = q.x, y = q.y, z = q.z, w = q.w;
  1295. var x2 = x + x, y2 = y + y, z2 = z + z;
  1296. var xx = x * x2, xy = x * y2, xz = x * z2;
  1297. var yy = y * y2, yz = y * z2, zz = z * z2;
  1298. var wx = w * x2, wy = w * y2, wz = w * z2;
  1299. te[0] = 1 - ( yy + zz );
  1300. te[4] = xy - wz;
  1301. te[8] = xz + wy;
  1302. te[1] = xy + wz;
  1303. te[5] = 1 - ( xx + zz );
  1304. te[9] = yz - wx;
  1305. te[2] = xz - wy;
  1306. te[6] = yz + wx;
  1307. te[10] = 1 - ( xx + yy );
  1308. return this;
  1309. },
  1310. compose: function ( translation, rotation, scale ) {
  1311. var te = this.elements;
  1312. var mRotation = THREE.Matrix4.__m1;
  1313. var mScale = THREE.Matrix4.__m2;
  1314. mRotation.identity();
  1315. mRotation.setRotationFromQuaternion( rotation );
  1316. mScale.makeScale( scale.x, scale.y, scale.z );
  1317. this.multiply( mRotation, mScale );
  1318. te[12] = translation.x;
  1319. te[13] = translation.y;
  1320. te[14] = translation.z;
  1321. return this;
  1322. },
  1323. decompose: function ( translation, rotation, scale ) {
  1324. // grab the axis vectors
  1325. var te = this.elements;
  1326. var x = THREE.Matrix4.__v1;
  1327. var y = THREE.Matrix4.__v2;
  1328. var z = THREE.Matrix4.__v3;
  1329. x.set( te[0], te[1], te[2] );
  1330. y.set( te[4], te[5], te[6] );
  1331. z.set( te[8], te[9], te[10] );
  1332. translation = ( translation instanceof THREE.Vector3 ) ? translation : new THREE.Vector3();
  1333. rotation = ( rotation instanceof THREE.Quaternion ) ? rotation : new THREE.Quaternion();
  1334. scale = ( scale instanceof THREE.Vector3 ) ? scale : new THREE.Vector3();
  1335. scale.x = x.length();
  1336. scale.y = y.length();
  1337. scale.z = z.length();
  1338. translation.x = te[12];
  1339. translation.y = te[13];
  1340. translation.z = te[14];
  1341. // scale the rotation part
  1342. var matrix = THREE.Matrix4.__m1;
  1343. matrix.copy( this );
  1344. matrix.elements[0] /= scale.x;
  1345. matrix.elements[1] /= scale.x;
  1346. matrix.elements[2] /= scale.x;
  1347. matrix.elements[4] /= scale.y;
  1348. matrix.elements[5] /= scale.y;
  1349. matrix.elements[6] /= scale.y;
  1350. matrix.elements[8] /= scale.z;
  1351. matrix.elements[9] /= scale.z;
  1352. matrix.elements[10] /= scale.z;
  1353. rotation.setFromRotationMatrix( matrix );
  1354. return [ translation, rotation, scale ];
  1355. },
  1356. extractPosition: function ( m ) {
  1357. var te = this.elements;
  1358. te[12] = m.elements[12];
  1359. te[13] = m.elements[13];
  1360. te[14] = m.elements[14];
  1361. return this;
  1362. },
  1363. extractRotation: function ( m ) {
  1364. var te = this.elements;
  1365. var vector = THREE.Matrix4.__v1;
  1366. var scaleX = 1 / vector.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  1367. var scaleY = 1 / vector.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  1368. var scaleZ = 1 / vector.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  1369. te[0] = m.elements[0] * scaleX;
  1370. te[1] = m.elements[1] * scaleX;
  1371. te[2] = m.elements[2] * scaleX;
  1372. te[4] = m.elements[4] * scaleY;
  1373. te[5] = m.elements[5] * scaleY;
  1374. te[6] = m.elements[6] * scaleY;
  1375. te[8] = m.elements[8] * scaleZ;
  1376. te[9] = m.elements[9] * scaleZ;
  1377. te[10] = m.elements[10] * scaleZ;
  1378. return this;
  1379. },
  1380. //
  1381. translate: function ( v ) {
  1382. var te = this.elements;
  1383. var x = v.x, y = v.y, z = v.z;
  1384. te[12] = te[0] * x + te[4] * y + te[8] * z + te[12];
  1385. te[13] = te[1] * x + te[5] * y + te[9] * z + te[13];
  1386. te[14] = te[2] * x + te[6] * y + te[10] * z + te[14];
  1387. te[15] = te[3] * x + te[7] * y + te[11] * z + te[15];
  1388. return this;
  1389. },
  1390. rotateX: function ( angle ) {
  1391. var te = this.elements;
  1392. var m12 = te[4];
  1393. var m22 = te[5];
  1394. var m32 = te[6];
  1395. var m42 = te[7];
  1396. var m13 = te[8];
  1397. var m23 = te[9];
  1398. var m33 = te[10];
  1399. var m43 = te[11];
  1400. var c = Math.cos( angle );
  1401. var s = Math.sin( angle );
  1402. te[4] = c * m12 + s * m13;
  1403. te[5] = c * m22 + s * m23;
  1404. te[6] = c * m32 + s * m33;
  1405. te[7] = c * m42 + s * m43;
  1406. te[8] = c * m13 - s * m12;
  1407. te[9] = c * m23 - s * m22;
  1408. te[10] = c * m33 - s * m32;
  1409. te[11] = c * m43 - s * m42;
  1410. return this;
  1411. },
  1412. rotateY: function ( angle ) {
  1413. var te = this.elements;
  1414. var m11 = te[0];
  1415. var m21 = te[1];
  1416. var m31 = te[2];
  1417. var m41 = te[3];
  1418. var m13 = te[8];
  1419. var m23 = te[9];
  1420. var m33 = te[10];
  1421. var m43 = te[11];
  1422. var c = Math.cos( angle );
  1423. var s = Math.sin( angle );
  1424. te[0] = c * m11 - s * m13;
  1425. te[1] = c * m21 - s * m23;
  1426. te[2] = c * m31 - s * m33;
  1427. te[3] = c * m41 - s * m43;
  1428. te[8] = c * m13 + s * m11;
  1429. te[9] = c * m23 + s * m21;
  1430. te[10] = c * m33 + s * m31;
  1431. te[11] = c * m43 + s * m41;
  1432. return this;
  1433. },
  1434. rotateZ: function ( angle ) {
  1435. var te = this.elements;
  1436. var m11 = te[0];
  1437. var m21 = te[1];
  1438. var m31 = te[2];
  1439. var m41 = te[3];
  1440. var m12 = te[4];
  1441. var m22 = te[5];
  1442. var m32 = te[6];
  1443. var m42 = te[7];
  1444. var c = Math.cos( angle );
  1445. var s = Math.sin( angle );
  1446. te[0] = c * m11 + s * m12;
  1447. te[1] = c * m21 + s * m22;
  1448. te[2] = c * m31 + s * m32;
  1449. te[3] = c * m41 + s * m42;
  1450. te[4] = c * m12 - s * m11;
  1451. te[5] = c * m22 - s * m21;
  1452. te[6] = c * m32 - s * m31;
  1453. te[7] = c * m42 - s * m41;
  1454. return this;
  1455. },
  1456. rotateByAxis: function ( axis, angle ) {
  1457. var te = this.elements;
  1458. // optimize by checking axis
  1459. if ( axis.x === 1 && axis.y === 0 && axis.z === 0 ) {
  1460. return this.rotateX( angle );
  1461. } else if ( axis.x === 0 && axis.y === 1 && axis.z === 0 ) {
  1462. return this.rotateY( angle );
  1463. } else if ( axis.x === 0 && axis.y === 0 && axis.z === 1 ) {
  1464. return this.rotateZ( angle );
  1465. }
  1466. var x = axis.x, y = axis.y, z = axis.z;
  1467. var n = Math.sqrt(x * x + y * y + z * z);
  1468. x /= n;
  1469. y /= n;
  1470. z /= n;
  1471. var xx = x * x, yy = y * y, zz = z * z;
  1472. var c = Math.cos( angle );
  1473. var s = Math.sin( angle );
  1474. var oneMinusCosine = 1 - c;
  1475. var xy = x * y * oneMinusCosine;
  1476. var xz = x * z * oneMinusCosine;
  1477. var yz = y * z * oneMinusCosine;
  1478. var xs = x * s;
  1479. var ys = y * s;
  1480. var zs = z * s;
  1481. var r11 = xx + (1 - xx) * c;
  1482. var r21 = xy + zs;
  1483. var r31 = xz - ys;
  1484. var r12 = xy - zs;
  1485. var r22 = yy + (1 - yy) * c;
  1486. var r32 = yz + xs;
  1487. var r13 = xz + ys;
  1488. var r23 = yz - xs;
  1489. var r33 = zz + (1 - zz) * c;
  1490. var m11 = te[0], m21 = te[1], m31 = te[2], m41 = te[3];
  1491. var m12 = te[4], m22 = te[5], m32 = te[6], m42 = te[7];
  1492. var m13 = te[8], m23 = te[9], m33 = te[10], m43 = te[11];
  1493. var m14 = te[12], m24 = te[13], m34 = te[14], m44 = te[15];
  1494. te[0] = r11 * m11 + r21 * m12 + r31 * m13;
  1495. te[1] = r11 * m21 + r21 * m22 + r31 * m23;
  1496. te[2] = r11 * m31 + r21 * m32 + r31 * m33;
  1497. te[3] = r11 * m41 + r21 * m42 + r31 * m43;
  1498. te[4] = r12 * m11 + r22 * m12 + r32 * m13;
  1499. te[5] = r12 * m21 + r22 * m22 + r32 * m23;
  1500. te[6] = r12 * m31 + r22 * m32 + r32 * m33;
  1501. te[7] = r12 * m41 + r22 * m42 + r32 * m43;
  1502. te[8] = r13 * m11 + r23 * m12 + r33 * m13;
  1503. te[9] = r13 * m21 + r23 * m22 + r33 * m23;
  1504. te[10] = r13 * m31 + r23 * m32 + r33 * m33;
  1505. te[11] = r13 * m41 + r23 * m42 + r33 * m43;
  1506. return this;
  1507. },
  1508. scale: function ( v ) {
  1509. var te = this.elements;
  1510. var x = v.x, y = v.y, z = v.z;
  1511. te[0] *= x; te[4] *= y; te[8] *= z;
  1512. te[1] *= x; te[5] *= y; te[9] *= z;
  1513. te[2] *= x; te[6] *= y; te[10] *= z;
  1514. te[3] *= x; te[7] *= y; te[11] *= z;
  1515. return this;
  1516. },
  1517. //
  1518. makeTranslation: function ( x, y, z ) {
  1519. this.set(
  1520. 1, 0, 0, x,
  1521. 0, 1, 0, y,
  1522. 0, 0, 1, z,
  1523. 0, 0, 0, 1
  1524. );
  1525. return this;
  1526. },
  1527. makeRotationX: function ( theta ) {
  1528. var c = Math.cos( theta ), s = Math.sin( theta );
  1529. this.set(
  1530. 1, 0, 0, 0,
  1531. 0, c, -s, 0,
  1532. 0, s, c, 0,
  1533. 0, 0, 0, 1
  1534. );
  1535. return this;
  1536. },
  1537. makeRotationY: function ( theta ) {
  1538. var c = Math.cos( theta ), s = Math.sin( theta );
  1539. this.set(
  1540. c, 0, s, 0,
  1541. 0, 1, 0, 0,
  1542. -s, 0, c, 0,
  1543. 0, 0, 0, 1
  1544. );
  1545. return this;
  1546. },
  1547. makeRotationZ: function ( theta ) {
  1548. var c = Math.cos( theta ), s = Math.sin( theta );
  1549. this.set(
  1550. c, -s, 0, 0,
  1551. s, c, 0, 0,
  1552. 0, 0, 1, 0,
  1553. 0, 0, 0, 1
  1554. );
  1555. return this;
  1556. },
  1557. makeRotationAxis: function ( axis, angle ) {
  1558. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  1559. var c = Math.cos( angle );
  1560. var s = Math.sin( angle );
  1561. var t = 1 - c;
  1562. var x = axis.x, y = axis.y, z = axis.z;
  1563. var tx = t * x, ty = t * y;
  1564. this.set(
  1565. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  1566. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  1567. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  1568. 0, 0, 0, 1
  1569. );
  1570. return this;
  1571. },
  1572. makeScale: function ( x, y, z ) {
  1573. this.set(
  1574. x, 0, 0, 0,
  1575. 0, y, 0, 0,
  1576. 0, 0, z, 0,
  1577. 0, 0, 0, 1
  1578. );
  1579. return this;
  1580. },
  1581. makeFrustum: function ( left, right, bottom, top, near, far ) {
  1582. var te = this.elements;
  1583. var x = 2 * near / ( right - left );
  1584. var y = 2 * near / ( top - bottom );
  1585. var a = ( right + left ) / ( right - left );
  1586. var b = ( top + bottom ) / ( top - bottom );
  1587. var c = - ( far + near ) / ( far - near );
  1588. var d = - 2 * far * near / ( far - near );
  1589. te[0] = x; te[4] = 0; te[8] = a; te[12] = 0;
  1590. te[1] = 0; te[5] = y; te[9] = b; te[13] = 0;
  1591. te[2] = 0; te[6] = 0; te[10] = c; te[14] = d;
  1592. te[3] = 0; te[7] = 0; te[11] = - 1; te[15] = 0;
  1593. return this;
  1594. },
  1595. makePerspective: function ( fov, aspect, near, far ) {
  1596. var ymax = near * Math.tan( fov * Math.PI / 360 );
  1597. var ymin = - ymax;
  1598. var xmin = ymin * aspect;
  1599. var xmax = ymax * aspect;
  1600. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  1601. },
  1602. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  1603. var te = this.elements;
  1604. var w = right - left;
  1605. var h = top - bottom;
  1606. var p = far - near;
  1607. var x = ( right + left ) / w;
  1608. var y = ( top + bottom ) / h;
  1609. var z = ( far + near ) / p;
  1610. te[0] = 2 / w; te[4] = 0; te[8] = 0; te[12] = -x;
  1611. te[1] = 0; te[5] = 2 / h; te[9] = 0; te[13] = -y;
  1612. te[2] = 0; te[6] = 0; te[10] = -2 / p; te[14] = -z;
  1613. te[3] = 0; te[7] = 0; te[11] = 0; te[15] = 1;
  1614. return this;
  1615. },
  1616. clone: function () {
  1617. var te = this.elements;
  1618. return new THREE.Matrix4(
  1619. te[0], te[4], te[8], te[12],
  1620. te[1], te[5], te[9], te[13],
  1621. te[2], te[6], te[10], te[14],
  1622. te[3], te[7], te[11], te[15]
  1623. );
  1624. }
  1625. };
  1626. THREE.Matrix4.__v1 = new THREE.Vector3();
  1627. THREE.Matrix4.__v2 = new THREE.Vector3();
  1628. THREE.Matrix4.__v3 = new THREE.Vector3();
  1629. THREE.Matrix4.__m1 = new THREE.Matrix4();
  1630. THREE.Matrix4.__m2 = new THREE.Matrix4();
  1631. /**
  1632. * @author mr.doob / http://mrdoob.com/
  1633. * @author mikael emtinger / http://gomo.se/
  1634. * @author alteredq / http://alteredqualia.com/
  1635. */
  1636. THREE.Object3D = function () {
  1637. this.id = THREE.Object3DCount ++;
  1638. this.name = '';
  1639. this.parent = undefined;
  1640. this.children = [];
  1641. this.up = new THREE.Vector3( 0, 1, 0 );
  1642. this.position = new THREE.Vector3();
  1643. this.rotation = new THREE.Vector3();
  1644. this.eulerOrder = 'XYZ';
  1645. this.scale = new THREE.Vector3( 1, 1, 1 );
  1646. this.doubleSided = false;
  1647. this.flipSided = false;
  1648. this.renderDepth = null;
  1649. this.rotationAutoUpdate = true;
  1650. this.matrix = new THREE.Matrix4();
  1651. this.matrixWorld = new THREE.Matrix4();
  1652. this.matrixRotationWorld = new THREE.Matrix4();
  1653. this.matrixAutoUpdate = true;
  1654. this.matrixWorldNeedsUpdate = true;
  1655. this.quaternion = new THREE.Quaternion();
  1656. this.useQuaternion = false;
  1657. this.boundRadius = 0.0;
  1658. this.boundRadiusScale = 1.0;
  1659. this.visible = true;
  1660. this.castShadow = false;
  1661. this.receiveShadow = false;
  1662. this.frustumCulled = true;
  1663. this._vector = new THREE.Vector3();
  1664. };
  1665. THREE.Object3D.prototype = {
  1666. constructor: THREE.Object3D,
  1667. applyMatrix: function ( matrix ) {
  1668. this.matrix.multiply( matrix, this.matrix );
  1669. this.scale.getScaleFromMatrix( this.matrix );
  1670. this.rotation.getRotationFromMatrix( this.matrix, this.scale );
  1671. this.position.getPositionFromMatrix( this.matrix );
  1672. },
  1673. translate: function ( distance, axis ) {
  1674. this.matrix.rotateAxis( axis );
  1675. this.position.addSelf( axis.multiplyScalar( distance ) );
  1676. },
  1677. translateX: function ( distance ) {
  1678. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  1679. },
  1680. translateY: function ( distance ) {
  1681. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  1682. },
  1683. translateZ: function ( distance ) {
  1684. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  1685. },
  1686. lookAt: function ( vector ) {
  1687. // TODO: Add hierarchy support.
  1688. this.matrix.lookAt( vector, this.position, this.up );
  1689. if ( this.rotationAutoUpdate ) {
  1690. this.rotation.getRotationFromMatrix( this.matrix );
  1691. }
  1692. },
  1693. add: function ( object ) {
  1694. if ( object === this ) {
  1695. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  1696. return;
  1697. }
  1698. if ( this.children.indexOf( object ) === - 1 ) {
  1699. if ( object.parent !== undefined ) {
  1700. object.parent.remove( object );
  1701. }
  1702. object.parent = this;
  1703. this.children.push( object );
  1704. // add to scene
  1705. var scene = this;
  1706. while ( scene.parent !== undefined ) {
  1707. scene = scene.parent;
  1708. }
  1709. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  1710. scene.__addObject( object );
  1711. }
  1712. }
  1713. },
  1714. remove: function ( object ) {
  1715. var index = this.children.indexOf( object );
  1716. if ( index !== - 1 ) {
  1717. object.parent = undefined;
  1718. this.children.splice( index, 1 );
  1719. // remove from scene
  1720. var scene = this;
  1721. while ( scene.parent !== undefined ) {
  1722. scene = scene.parent;
  1723. }
  1724. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  1725. scene.__removeObject( object );
  1726. }
  1727. }
  1728. },
  1729. getChildByName: function ( name, recursive ) {
  1730. var c, cl, child;
  1731. for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
  1732. child = this.children[ c ];
  1733. if ( child.name === name ) {
  1734. return child;
  1735. }
  1736. if ( recursive ) {
  1737. child = child.getChildByName( name, recursive );
  1738. if ( child !== undefined ) {
  1739. return child;
  1740. }
  1741. }
  1742. }
  1743. return undefined;
  1744. },
  1745. updateMatrix: function () {
  1746. this.matrix.setPosition( this.position );
  1747. if ( this.useQuaternion ) {
  1748. this.matrix.setRotationFromQuaternion( this.quaternion );
  1749. } else {
  1750. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  1751. }
  1752. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  1753. this.matrix.scale( this.scale );
  1754. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  1755. }
  1756. this.matrixWorldNeedsUpdate = true;
  1757. },
  1758. updateMatrixWorld: function ( force ) {
  1759. this.matrixAutoUpdate && this.updateMatrix();
  1760. // update matrixWorld
  1761. if ( this.matrixWorldNeedsUpdate || force ) {
  1762. if ( this.parent ) {
  1763. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  1764. } else {
  1765. this.matrixWorld.copy( this.matrix );
  1766. }
  1767. this.matrixWorldNeedsUpdate = false;
  1768. force = true;
  1769. }
  1770. // update children
  1771. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  1772. this.children[ i ].updateMatrixWorld( force );
  1773. }
  1774. }
  1775. };
  1776. THREE.Object3DCount = 0;
  1777. /**
  1778. * @author mr.doob / http://mrdoob.com/
  1779. * @author supereggbert / http://www.paulbrunt.co.uk/
  1780. * @author julianwa / https://github.com/julianwa
  1781. */
  1782. THREE.Projector = function() {
  1783. var _object, _objectCount, _objectPool = [],
  1784. _vertex, _vertexCount, _vertexPool = [],
  1785. _face, _face3Count, _face3Pool = [], _face4Count, _face4Pool = [],
  1786. _line, _lineCount, _linePool = [],
  1787. _particle, _particleCount, _particlePool = [],
  1788. _renderData = { objects: [], sprites: [], lights: [], elements: [] },
  1789. _vector3 = new THREE.Vector3(),
  1790. _vector4 = new THREE.Vector4(),
  1791. _projScreenMatrix = new THREE.Matrix4(),
  1792. _projScreenobjectMatrixWorld = new THREE.Matrix4(),
  1793. _frustum = new THREE.Frustum(),
  1794. _clippedVertex1PositionScreen = new THREE.Vector4(),
  1795. _clippedVertex2PositionScreen = new THREE.Vector4(),
  1796. _face3VertexNormals;
  1797. this.projectVector = function ( vector, camera ) {
  1798. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1799. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1800. _projScreenMatrix.multiplyVector3( vector );
  1801. return vector;
  1802. };
  1803. this.unprojectVector = function ( vector, camera ) {
  1804. camera.projectionMatrixInverse.getInverse( camera.projectionMatrix );
  1805. _projScreenMatrix.multiply( camera.matrixWorld, camera.projectionMatrixInverse );
  1806. _projScreenMatrix.multiplyVector3( vector );
  1807. return vector;
  1808. };
  1809. this.pickingRay = function ( vector, camera ) {
  1810. var end, ray, t;
  1811. // set two vectors with opposing z values
  1812. vector.z = -1.0;
  1813. end = new THREE.Vector3( vector.x, vector.y, 1.0 );
  1814. this.unprojectVector( vector, camera );
  1815. this.unprojectVector( end, camera );
  1816. // find direction from vector to end
  1817. end.subSelf( vector ).normalize();
  1818. return new THREE.Ray( vector, end );
  1819. };
  1820. this.projectGraph = function ( root, sort ) {
  1821. _objectCount = 0;
  1822. _renderData.objects.length = 0;
  1823. _renderData.sprites.length = 0;
  1824. _renderData.lights.length = 0;
  1825. var projectObject = function ( object ) {
  1826. if ( object.visible === false ) return;
  1827. if ( ( object instanceof THREE.Mesh || object instanceof THREE.Line ) &&
  1828. ( object.frustumCulled === false || _frustum.contains( object ) ) ) {
  1829. _vector3.copy( object.matrixWorld.getPosition() );
  1830. _projScreenMatrix.multiplyVector3( _vector3 );
  1831. _object = getNextObjectInPool();
  1832. _object.object = object;
  1833. _object.z = _vector3.z;
  1834. _renderData.objects.push( _object );
  1835. } else if ( object instanceof THREE.Sprite || object instanceof THREE.Particle ) {
  1836. _vector3.copy( object.matrixWorld.getPosition() );
  1837. _projScreenMatrix.multiplyVector3( _vector3 );
  1838. _object = getNextObjectInPool();
  1839. _object.object = object;
  1840. _object.z = _vector3.z;
  1841. _renderData.sprites.push( _object );
  1842. } else if ( object instanceof THREE.Light ) {
  1843. _renderData.lights.push( object );
  1844. }
  1845. for ( var c = 0, cl = object.children.length; c < cl; c ++ ) {
  1846. projectObject( object.children[ c ] );
  1847. }
  1848. };
  1849. projectObject( root );
  1850. sort && _renderData.objects.sort( painterSort );
  1851. return _renderData;
  1852. };
  1853. this.projectScene = function ( scene, camera, sort ) {
  1854. var near = camera.near, far = camera.far, visible = false,
  1855. o, ol, v, vl, f, fl, n, nl, c, cl, u, ul, object,
  1856. objectMatrixWorld, objectMatrixWorldRotation,
  1857. geometry, geometryMaterials, vertices, vertex, vertexPositionScreen,
  1858. faces, face, faceVertexNormals, normal, faceVertexUvs, uvs,
  1859. v1, v2, v3, v4;
  1860. _face3Count = 0;
  1861. _face4Count = 0;
  1862. _lineCount = 0;
  1863. _particleCount = 0;
  1864. _renderData.elements.length = 0;
  1865. if ( camera.parent === undefined ) {
  1866. console.warn( 'DEPRECATED: Camera hasn\'t been added to a Scene. Adding it...' );
  1867. scene.add( camera );
  1868. }
  1869. scene.updateMatrixWorld();
  1870. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1871. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1872. _frustum.setFromMatrix( _projScreenMatrix );
  1873. _renderData = this.projectGraph( scene, false );
  1874. for ( o = 0, ol = _renderData.objects.length; o < ol; o++ ) {
  1875. object = _renderData.objects[ o ].object;
  1876. objectMatrixWorld = object.matrixWorld;
  1877. _vertexCount = 0;
  1878. if ( object instanceof THREE.Mesh ) {
  1879. geometry = object.geometry;
  1880. geometryMaterials = object.geometry.materials;
  1881. vertices = geometry.vertices;
  1882. faces = geometry.faces;
  1883. faceVertexUvs = geometry.faceVertexUvs;
  1884. objectMatrixWorldRotation = object.matrixRotationWorld.extractRotation( objectMatrixWorld );
  1885. for ( v = 0, vl = vertices.length; v < vl; v ++ ) {
  1886. _vertex = getNextVertexInPool();
  1887. _vertex.positionWorld.copy( vertices[ v ].position );
  1888. objectMatrixWorld.multiplyVector3( _vertex.positionWorld );
  1889. _vertex.positionScreen.copy( _vertex.positionWorld );
  1890. _projScreenMatrix.multiplyVector4( _vertex.positionScreen );
  1891. _vertex.positionScreen.x /= _vertex.positionScreen.w;
  1892. _vertex.positionScreen.y /= _vertex.positionScreen.w;
  1893. _vertex.visible = _vertex.positionScreen.z > near && _vertex.positionScreen.z < far;
  1894. }
  1895. for ( f = 0, fl = faces.length; f < fl; f ++ ) {
  1896. face = faces[ f ];
  1897. if ( face instanceof THREE.Face3 ) {
  1898. v1 = _vertexPool[ face.a ];
  1899. v2 = _vertexPool[ face.b ];
  1900. v3 = _vertexPool[ face.c ];
  1901. if ( v1.visible && v2.visible && v3.visible ) {
  1902. visible = ( ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  1903. ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
  1904. if ( object.doubleSided || visible != object.flipSided ) {
  1905. _face = getNextFace3InPool();
  1906. _face.v1.copy( v1 );
  1907. _face.v2.copy( v2 );
  1908. _face.v3.copy( v3 );
  1909. } else {
  1910. continue;
  1911. }
  1912. } else {
  1913. continue;
  1914. }
  1915. } else if ( face instanceof THREE.Face4 ) {
  1916. v1 = _vertexPool[ face.a ];
  1917. v2 = _vertexPool[ face.b ];
  1918. v3 = _vertexPool[ face.c ];
  1919. v4 = _vertexPool[ face.d ];
  1920. if ( v1.visible && v2.visible && v3.visible && v4.visible ) {
  1921. visible = ( v4.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  1922. ( v4.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ||
  1923. ( v2.positionScreen.x - v3.positionScreen.x ) * ( v4.positionScreen.y - v3.positionScreen.y ) -
  1924. ( v2.positionScreen.y - v3.positionScreen.y ) * ( v4.positionScreen.x - v3.positionScreen.x ) < 0;
  1925. if ( object.doubleSided || visible != object.flipSided ) {
  1926. _face = getNextFace4InPool();
  1927. _face.v1.copy( v1 );
  1928. _face.v2.copy( v2 );
  1929. _face.v3.copy( v3 );
  1930. _face.v4.copy( v4 );
  1931. } else {
  1932. continue;
  1933. }
  1934. } else {
  1935. continue;
  1936. }
  1937. }
  1938. _face.normalWorld.copy( face.normal );
  1939. if ( !visible && ( object.flipSided || object.doubleSided ) ) _face.normalWorld.negate();
  1940. objectMatrixWorldRotation.multiplyVector3( _face.normalWorld );
  1941. _face.centroidWorld.copy( face.centroid );
  1942. objectMatrixWorld.multiplyVector3( _face.centroidWorld );
  1943. _face.centroidScreen.copy( _face.centroidWorld );
  1944. _projScreenMatrix.multiplyVector3( _face.centroidScreen );
  1945. faceVertexNormals = face.vertexNormals;
  1946. for ( n = 0, nl = faceVertexNormals.length; n < nl; n ++ ) {
  1947. normal = _face.vertexNormalsWorld[ n ];
  1948. normal.copy( faceVertexNormals[ n ] );
  1949. if ( !visible && ( object.flipSided || object.doubleSided ) ) normal.negate();
  1950. objectMatrixWorldRotation.multiplyVector3( normal );
  1951. }
  1952. for ( c = 0, cl = faceVertexUvs.length; c < cl; c ++ ) {
  1953. uvs = faceVertexUvs[ c ][ f ];
  1954. if ( !uvs ) continue;
  1955. for ( u = 0, ul = uvs.length; u < ul; u ++ ) {
  1956. _face.uvs[ c ][ u ] = uvs[ u ];
  1957. }
  1958. }
  1959. _face.material = object.material;
  1960. _face.faceMaterial = face.materialIndex !== null ? geometryMaterials[ face.materialIndex ] : null;
  1961. _face.z = _face.centroidScreen.z;
  1962. _renderData.elements.push( _face );
  1963. }
  1964. } else if ( object instanceof THREE.Line ) {
  1965. _projScreenobjectMatrixWorld.multiply( _projScreenMatrix, objectMatrixWorld );
  1966. vertices = object.geometry.vertices;
  1967. v1 = getNextVertexInPool();
  1968. v1.positionScreen.copy( vertices[ 0 ].position );
  1969. _projScreenobjectMatrixWorld.multiplyVector4( v1.positionScreen );
  1970. // Handle LineStrip and LinePieces
  1971. var step = object.type === THREE.LinePieces ? 2 : 1;
  1972. for ( v = 1, vl = vertices.length; v < vl; v ++ ) {
  1973. v1 = getNextVertexInPool();
  1974. v1.positionScreen.copy( vertices[ v ].position );
  1975. _projScreenobjectMatrixWorld.multiplyVector4( v1.positionScreen );
  1976. if ( ( v + 1 ) % step > 0 ) continue;
  1977. v2 = _vertexPool[ _vertexCount - 2 ];
  1978. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  1979. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  1980. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) ) {
  1981. // Perform the perspective divide
  1982. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  1983. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  1984. _line = getNextLineInPool();
  1985. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  1986. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  1987. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  1988. _line.material = object.material;
  1989. _renderData.elements.push( _line );
  1990. }
  1991. }
  1992. }
  1993. }
  1994. for ( o = 0, ol = _renderData.sprites.length; o < ol; o++ ) {
  1995. object = _renderData.sprites[ o ].object;
  1996. objectMatrixWorld = object.matrixWorld;
  1997. if ( object instanceof THREE.Particle ) {
  1998. _vector4.set( objectMatrixWorld.elements[12], objectMatrixWorld.elements[13], objectMatrixWorld.elements[14], 1 );
  1999. _projScreenMatrix.multiplyVector4( _vector4 );
  2000. _vector4.z /= _vector4.w;
  2001. if ( _vector4.z > 0 && _vector4.z < 1 ) {
  2002. _particle = getNextParticleInPool();
  2003. _particle.x = _vector4.x / _vector4.w;
  2004. _particle.y = _vector4.y / _vector4.w;
  2005. _particle.z = _vector4.z;
  2006. _particle.rotation = object.rotation.z;
  2007. _particle.scale.x = object.scale.x * Math.abs( _particle.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) );
  2008. _particle.scale.y = object.scale.y * Math.abs( _particle.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) );
  2009. _particle.material = object.material;
  2010. _renderData.elements.push( _particle );
  2011. }
  2012. }
  2013. }
  2014. sort && _renderData.elements.sort( painterSort );
  2015. return _renderData;
  2016. };
  2017. // Pools
  2018. function getNextObjectInPool() {
  2019. var object = _objectPool[ _objectCount ] = _objectPool[ _objectCount ] || new THREE.RenderableObject();
  2020. _objectCount ++;
  2021. return object;
  2022. }
  2023. function getNextVertexInPool() {
  2024. var vertex = _vertexPool[ _vertexCount ] = _vertexPool[ _vertexCount ] || new THREE.RenderableVertex();
  2025. _vertexCount ++;
  2026. return vertex;
  2027. }
  2028. function getNextFace3InPool() {
  2029. var face = _face3Pool[ _face3Count ] = _face3Pool[ _face3Count ] || new THREE.RenderableFace3();
  2030. _face3Count ++;
  2031. return face;
  2032. }
  2033. function getNextFace4InPool() {
  2034. var face = _face4Pool[ _face4Count ] = _face4Pool[ _face4Count ] || new THREE.RenderableFace4();
  2035. _face4Count ++;
  2036. return face;
  2037. }
  2038. function getNextLineInPool() {
  2039. var line = _linePool[ _lineCount ] = _linePool[ _lineCount ] || new THREE.RenderableLine();
  2040. _lineCount ++;
  2041. return line;
  2042. }
  2043. function getNextParticleInPool() {
  2044. var particle = _particlePool[ _particleCount ] = _particlePool[ _particleCount ] || new THREE.RenderableParticle();
  2045. _particleCount ++;
  2046. return particle;
  2047. }
  2048. //
  2049. function painterSort( a, b ) {
  2050. return b.z - a.z;
  2051. }
  2052. function clipLine( s1, s2 ) {
  2053. var alpha1 = 0, alpha2 = 1,
  2054. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  2055. // Z = -1 and Z = +1, respectively.
  2056. bc1near = s1.z + s1.w,
  2057. bc2near = s2.z + s2.w,
  2058. bc1far = - s1.z + s1.w,
  2059. bc2far = - s2.z + s2.w;
  2060. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  2061. // Both vertices lie entirely within all clip planes.
  2062. return true;
  2063. } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
  2064. // Both vertices lie entirely outside one of the clip planes.
  2065. return false;
  2066. } else {
  2067. // The line segment spans at least one clip plane.
  2068. if ( bc1near < 0 ) {
  2069. // v1 lies outside the near plane, v2 inside
  2070. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  2071. } else if ( bc2near < 0 ) {
  2072. // v2 lies outside the near plane, v1 inside
  2073. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  2074. }
  2075. if ( bc1far < 0 ) {
  2076. // v1 lies outside the far plane, v2 inside
  2077. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  2078. } else if ( bc2far < 0 ) {
  2079. // v2 lies outside the far plane, v2 inside
  2080. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  2081. }
  2082. if ( alpha2 < alpha1 ) {
  2083. // The line segment spans two boundaries, but is outside both of them.
  2084. // (This can't happen when we're only clipping against just near/far but good
  2085. // to leave the check here for future usage if other clip planes are added.)
  2086. return false;
  2087. } else {
  2088. // Update the s1 and s2 vertices to match the clipped line segment.
  2089. s1.lerpSelf( s2, alpha1 );
  2090. s2.lerpSelf( s1, 1 - alpha2 );
  2091. return true;
  2092. }
  2093. }
  2094. }
  2095. };
  2096. /**
  2097. * @author mikael emtinger / http://gomo.se/
  2098. * @author alteredq / http://alteredqualia.com/
  2099. */
  2100. THREE.Quaternion = function( x, y, z, w ) {
  2101. this.x = x || 0;
  2102. this.y = y || 0;
  2103. this.z = z || 0;
  2104. this.w = ( w !== undefined ) ? w : 1;
  2105. };
  2106. THREE.Quaternion.prototype = {
  2107. constructor: THREE.Quaternion,
  2108. set: function ( x, y, z, w ) {
  2109. this.x = x;
  2110. this.y = y;
  2111. this.z = z;
  2112. this.w = w;
  2113. return this;
  2114. },
  2115. copy: function ( q ) {
  2116. this.x = q.x;
  2117. this.y = q.y;
  2118. this.z = q.z;
  2119. this.w = q.w;
  2120. return this;
  2121. },
  2122. setFromEuler: function ( vector ) {
  2123. var c = Math.PI / 360, // 0.5 * Math.PI / 360, // 0.5 is an optimization
  2124. x = vector.x * c,
  2125. y = vector.y * c,
  2126. z = vector.z * c,
  2127. c1 = Math.cos( y ),
  2128. s1 = Math.sin( y ),
  2129. c2 = Math.cos( -z ),
  2130. s2 = Math.sin( -z ),
  2131. c3 = Math.cos( x ),
  2132. s3 = Math.sin( x ),
  2133. c1c2 = c1 * c2,
  2134. s1s2 = s1 * s2;
  2135. this.w = c1c2 * c3 - s1s2 * s3;
  2136. this.x = c1c2 * s3 + s1s2 * c3;
  2137. this.y = s1 * c2 * c3 + c1 * s2 * s3;
  2138. this.z = c1 * s2 * c3 - s1 * c2 * s3;
  2139. return this;
  2140. },
  2141. setFromAxisAngle: function ( axis, angle ) {
  2142. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  2143. // axis have to be normalized
  2144. var halfAngle = angle / 2,
  2145. s = Math.sin( halfAngle );
  2146. this.x = axis.x * s;
  2147. this.y = axis.y * s;
  2148. this.z = axis.z * s;
  2149. this.w = Math.cos( halfAngle );
  2150. return this;
  2151. },
  2152. setFromRotationMatrix: function ( m ) {
  2153. // Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  2154. function copySign( a, b ) {
  2155. return b < 0 ? -Math.abs( a ) : Math.abs( a );
  2156. }
  2157. var absQ = Math.pow( m.determinant(), 1.0 / 3.0 );
  2158. this.w = Math.sqrt( Math.max( 0, absQ + m.elements[0] + m.elements[5] + m.elements[10] ) ) / 2;
  2159. this.x = Math.sqrt( Math.max( 0, absQ + m.elements[0] - m.elements[5] - m.elements[10] ) ) / 2;
  2160. this.y = Math.sqrt( Math.max( 0, absQ - m.elements[0] + m.elements[5] - m.elements[10] ) ) / 2;
  2161. this.z = Math.sqrt( Math.max( 0, absQ - m.elements[0] - m.elements[5] + m.elements[10] ) ) / 2;
  2162. this.x = copySign( this.x, ( m.elements[6] - m.elements[9] ) );
  2163. this.y = copySign( this.y, ( m.elements[8] - m.elements[2] ) );
  2164. this.z = copySign( this.z, ( m.elements[1] - m.elements[4] ) );
  2165. this.normalize();
  2166. return this;
  2167. },
  2168. calculateW : function () {
  2169. this.w = - Math.sqrt( Math.abs( 1.0 - this.x * this.x - this.y * this.y - this.z * this.z ) );
  2170. return this;
  2171. },
  2172. inverse: function () {
  2173. this.x *= -1;
  2174. this.y *= -1;
  2175. this.z *= -1;
  2176. return this;
  2177. },
  2178. length: function () {
  2179. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  2180. },
  2181. normalize: function () {
  2182. var l = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  2183. if ( l === 0 ) {
  2184. this.x = 0;
  2185. this.y = 0;
  2186. this.z = 0;
  2187. this.w = 0;
  2188. } else {
  2189. l = 1 / l;
  2190. this.x = this.x * l;
  2191. this.y = this.y * l;
  2192. this.z = this.z * l;
  2193. this.w = this.w * l;
  2194. }
  2195. return this;
  2196. },
  2197. multiply: function ( a, b ) {
  2198. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  2199. this.x = a.x * b.w + a.y * b.z - a.z * b.y + a.w * b.x;
  2200. this.y = -a.x * b.z + a.y * b.w + a.z * b.x + a.w * b.y;
  2201. this.z = a.x * b.y - a.y * b.x + a.z * b.w + a.w * b.z;
  2202. this.w = -a.x * b.x - a.y * b.y - a.z * b.z + a.w * b.w;
  2203. return this;
  2204. },
  2205. multiplySelf: function ( b ) {
  2206. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  2207. qbx = b.x, qby = b.y, qbz = b.z, qbw = b.w;
  2208. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  2209. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  2210. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  2211. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  2212. return this;
  2213. },
  2214. multiplyVector3: function ( vector, dest ) {
  2215. if ( !dest ) { dest = vector; }
  2216. var x = vector.x, y = vector.y, z = vector.z,
  2217. qx = this.x, qy = this.y, qz = this.z, qw = this.w;
  2218. // calculate quat * vector
  2219. var ix = qw * x + qy * z - qz * y,
  2220. iy = qw * y + qz * x - qx * z,
  2221. iz = qw * z + qx * y - qy * x,
  2222. iw = -qx * x - qy * y - qz * z;
  2223. // calculate result * inverse quat
  2224. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  2225. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  2226. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  2227. return dest;
  2228. },
  2229. clone: function () {
  2230. return new THREE.Quaternion( this.x, this.y, this.z, this.w );
  2231. }
  2232. }
  2233. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  2234. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  2235. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  2236. if (cosHalfTheta < 0) {
  2237. qm.w = -qb.w; qm.x = -qb.x; qm.y = -qb.y; qm.z = -qb.z;
  2238. cosHalfTheta = -cosHalfTheta;
  2239. } else {
  2240. qm.copy(qb);
  2241. }
  2242. if ( Math.abs( cosHalfTheta ) >= 1.0 ) {
  2243. qm.w = qa.w; qm.x = qa.x; qm.y = qa.y; qm.z = qa.z;
  2244. return qm;
  2245. }
  2246. var halfTheta = Math.acos( cosHalfTheta ),
  2247. sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  2248. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  2249. qm.w = 0.5 * ( qa.w + qb.w );
  2250. qm.x = 0.5 * ( qa.x + qb.x );
  2251. qm.y = 0.5 * ( qa.y + qb.y );
  2252. qm.z = 0.5 * ( qa.z + qb.z );
  2253. return qm;
  2254. }
  2255. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  2256. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  2257. qm.w = ( qa.w * ratioA + qm.w * ratioB );
  2258. qm.x = ( qa.x * ratioA + qm.x * ratioB );
  2259. qm.y = ( qa.y * ratioA + qm.y * ratioB );
  2260. qm.z = ( qa.z * ratioA + qm.z * ratioB );
  2261. return qm;
  2262. }
  2263. /**
  2264. * @author mr.doob / http://mrdoob.com/
  2265. */
  2266. THREE.Vertex = function ( position ) {
  2267. this.position = position || new THREE.Vector3();
  2268. };
  2269. THREE.Vertex.prototype = {
  2270. constructor: THREE.Vertex,
  2271. clone: function () {
  2272. return new THREE.Vertex( this.position.clone() );
  2273. }
  2274. };
  2275. /**
  2276. * @author mr.doob / http://mrdoob.com/
  2277. * @author alteredq / http://alteredqualia.com/
  2278. */
  2279. THREE.Face3 = function ( a, b, c, normal, color, materialIndex ) {
  2280. this.a = a;
  2281. this.b = b;
  2282. this.c = c;
  2283. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  2284. this.vertexNormals = normal instanceof Array ? normal : [ ];
  2285. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  2286. this.vertexColors = color instanceof Array ? color : [];
  2287. this.vertexTangents = [];
  2288. this.materialIndex = materialIndex;
  2289. this.centroid = new THREE.Vector3();
  2290. };
  2291. THREE.Face3.prototype = {
  2292. constructor: THREE.Face3,
  2293. clone: function () {
  2294. var face = new THREE.Face3( this.a, this.b, this.c );
  2295. face.normal.copy( this.normal );
  2296. face.color.copy( this.color );
  2297. face.centroid.copy( this.centroid );
  2298. face.materialIndex = this.materialIndex;
  2299. var i, il;
  2300. for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  2301. for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  2302. for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  2303. return face;
  2304. }
  2305. };
  2306. /**
  2307. * @author mr.doob / http://mrdoob.com/
  2308. * @author alteredq / http://alteredqualia.com/
  2309. */
  2310. THREE.Face4 = function ( a, b, c, d, normal, color, materialIndex ) {
  2311. this.a = a;
  2312. this.b = b;
  2313. this.c = c;
  2314. this.d = d;
  2315. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  2316. this.vertexNormals = normal instanceof Array ? normal : [ ];
  2317. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  2318. this.vertexColors = color instanceof Array ? color : [];
  2319. this.vertexTangents = [];
  2320. this.materialIndex = materialIndex;
  2321. this.centroid = new THREE.Vector3();
  2322. };
  2323. THREE.Face4.prototype = {
  2324. constructor: THREE.Face4,
  2325. clone: function () {
  2326. var face = new THREE.Face4( this.a, this.b, this.c, this.d );
  2327. face.normal.copy( this.normal );
  2328. face.color.copy( this.color );
  2329. face.centroid.copy( this.centroid );
  2330. face.materialIndex = this.materialIndex;
  2331. var i, il;
  2332. for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  2333. for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  2334. for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  2335. return face;
  2336. }
  2337. };
  2338. /**
  2339. * @author mr.doob / http://mrdoob.com/
  2340. */
  2341. THREE.UV = function ( u, v ) {
  2342. this.u = u || 0;
  2343. this.v = v || 0;
  2344. };
  2345. THREE.UV.prototype = {
  2346. constructor: THREE.UV,
  2347. set: function ( u, v ) {
  2348. this.u = u;
  2349. this.v = v;
  2350. return this;
  2351. },
  2352. copy: function ( uv ) {
  2353. this.u = uv.u;
  2354. this.v = uv.v;
  2355. return this;
  2356. },
  2357. lerpSelf: function ( uv, alpha ) {
  2358. this.u += ( uv.u - this.u ) * alpha;
  2359. this.v += ( uv.v - this.v ) * alpha;
  2360. return this;
  2361. },
  2362. clone: function () {
  2363. return new THREE.UV( this.u, this.v );
  2364. }
  2365. };
  2366. /**
  2367. * @author mr.doob / http://mrdoob.com/
  2368. * @author kile / http://kile.stravaganza.org/
  2369. * @author alteredq / http://alteredqualia.com/
  2370. * @author mikael emtinger / http://gomo.se/
  2371. * @author zz85 / http://www.lab4games.net/zz85/blog
  2372. */
  2373. THREE.Geometry = function () {
  2374. this.id = THREE.GeometryCount ++;
  2375. this.vertices = [];
  2376. this.colors = []; // one-to-one vertex colors, used in ParticleSystem, Line and Ribbon
  2377. this.materials = [];
  2378. this.faces = [];
  2379. this.faceUvs = [[]];
  2380. this.faceVertexUvs = [[]];
  2381. this.morphTargets = [];
  2382. this.morphColors = [];
  2383. this.morphNormals = [];
  2384. this.skinWeights = [];
  2385. this.skinIndices = [];
  2386. this.boundingBox = null;
  2387. this.boundingSphere = null;
  2388. this.hasTangents = false;
  2389. this.dynamic = false; // unless set to true the *Arrays will be deleted once sent to a buffer.
  2390. };
  2391. THREE.Geometry.prototype = {
  2392. constructor : THREE.Geometry,
  2393. applyMatrix: function ( matrix ) {
  2394. var matrixRotation = new THREE.Matrix4();
  2395. matrixRotation.extractRotation( matrix );
  2396. for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
  2397. var vertex = this.vertices[ i ];
  2398. matrix.multiplyVector3( vertex.position );
  2399. }
  2400. for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
  2401. var face = this.faces[ i ];
  2402. matrixRotation.multiplyVector3( face.normal );
  2403. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  2404. matrixRotation.multiplyVector3( face.vertexNormals[ j ] );
  2405. }
  2406. matrix.multiplyVector3( face.centroid );
  2407. }
  2408. },
  2409. computeCentroids: function () {
  2410. var f, fl, face;
  2411. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2412. face = this.faces[ f ];
  2413. face.centroid.set( 0, 0, 0 );
  2414. if ( face instanceof THREE.Face3 ) {
  2415. face.centroid.addSelf( this.vertices[ face.a ].position );
  2416. face.centroid.addSelf( this.vertices[ face.b ].position );
  2417. face.centroid.addSelf( this.vertices[ face.c ].position );
  2418. face.centroid.divideScalar( 3 );
  2419. } else if ( face instanceof THREE.Face4 ) {
  2420. face.centroid.addSelf( this.vertices[ face.a ].position );
  2421. face.centroid.addSelf( this.vertices[ face.b ].position );
  2422. face.centroid.addSelf( this.vertices[ face.c ].position );
  2423. face.centroid.addSelf( this.vertices[ face.d ].position );
  2424. face.centroid.divideScalar( 4 );
  2425. }
  2426. }
  2427. },
  2428. computeFaceNormals: function () {
  2429. var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
  2430. cb = new THREE.Vector3(), ab = new THREE.Vector3();
  2431. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2432. face = this.faces[ f ];
  2433. vA = this.vertices[ face.a ];
  2434. vB = this.vertices[ face.b ];
  2435. vC = this.vertices[ face.c ];
  2436. cb.sub( vC.position, vB.position );
  2437. ab.sub( vA.position, vB.position );
  2438. cb.crossSelf( ab );
  2439. if ( !cb.isZero() ) {
  2440. cb.normalize();
  2441. }
  2442. face.normal.copy( cb );
  2443. }
  2444. },
  2445. computeVertexNormals: function () {
  2446. var v, vl, f, fl, face, vertices;
  2447. // create internal buffers for reuse when calling this method repeatedly
  2448. // (otherwise memory allocation / deallocation every frame is big resource hog)
  2449. if ( this.__tmpVertices === undefined ) {
  2450. this.__tmpVertices = new Array( this.vertices.length );
  2451. vertices = this.__tmpVertices;
  2452. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2453. vertices[ v ] = new THREE.Vector3();
  2454. }
  2455. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2456. face = this.faces[ f ];
  2457. if ( face instanceof THREE.Face3 ) {
  2458. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  2459. } else if ( face instanceof THREE.Face4 ) {
  2460. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  2461. }
  2462. }
  2463. } else {
  2464. vertices = this.__tmpVertices;
  2465. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2466. vertices[ v ].set( 0, 0, 0 );
  2467. }
  2468. }
  2469. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2470. face = this.faces[ f ];
  2471. if ( face instanceof THREE.Face3 ) {
  2472. vertices[ face.a ].addSelf( face.normal );
  2473. vertices[ face.b ].addSelf( face.normal );
  2474. vertices[ face.c ].addSelf( face.normal );
  2475. } else if ( face instanceof THREE.Face4 ) {
  2476. vertices[ face.a ].addSelf( face.normal );
  2477. vertices[ face.b ].addSelf( face.normal );
  2478. vertices[ face.c ].addSelf( face.normal );
  2479. vertices[ face.d ].addSelf( face.normal );
  2480. }
  2481. }
  2482. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2483. vertices[ v ].normalize();
  2484. }
  2485. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2486. face = this.faces[ f ];
  2487. if ( face instanceof THREE.Face3 ) {
  2488. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  2489. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  2490. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  2491. } else if ( face instanceof THREE.Face4 ) {
  2492. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  2493. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  2494. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  2495. face.vertexNormals[ 3 ].copy( vertices[ face.d ] );
  2496. }
  2497. }
  2498. },
  2499. computeMorphNormals: function () {
  2500. var i, il, f, fl, face;
  2501. // save original normals
  2502. // - create temp variables on first access
  2503. // otherwise just copy (for faster repeated calls)
  2504. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2505. face = this.faces[ f ];
  2506. if ( ! face.__originalFaceNormal ) {
  2507. face.__originalFaceNormal = face.normal.clone();
  2508. } else {
  2509. face.__originalFaceNormal.copy( face.normal );
  2510. }
  2511. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  2512. for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  2513. if ( ! face.__originalVertexNormals[ i ] ) {
  2514. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  2515. } else {
  2516. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  2517. }
  2518. }
  2519. }
  2520. // use temp geometry to compute face and vertex normals for each morph
  2521. var tmpGeo = new THREE.Geometry();
  2522. tmpGeo.faces = this.faces;
  2523. for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  2524. // create on first access
  2525. if ( ! this.morphNormals[ i ] ) {
  2526. this.morphNormals[ i ] = {};
  2527. this.morphNormals[ i ].faceNormals = [];
  2528. this.morphNormals[ i ].vertexNormals = [];
  2529. var dstNormalsFace = this.morphNormals[ i ].faceNormals;
  2530. var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  2531. var faceNormal, vertexNormals;
  2532. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2533. face = this.faces[ f ];
  2534. faceNormal = new THREE.Vector3();
  2535. if ( face instanceof THREE.Face3 ) {
  2536. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() };
  2537. } else {
  2538. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3(), d: new THREE.Vector3() };
  2539. }
  2540. dstNormalsFace.push( faceNormal );
  2541. dstNormalsVertex.push( vertexNormals );
  2542. }
  2543. }
  2544. var morphNormals = this.morphNormals[ i ];
  2545. // set vertices to morph target
  2546. tmpGeo.vertices = this.morphTargets[ i ].vertices;
  2547. // compute morph normals
  2548. tmpGeo.computeFaceNormals();
  2549. tmpGeo.computeVertexNormals();
  2550. // store morph normals
  2551. var faceNormal, vertexNormals;
  2552. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2553. face = this.faces[ f ];
  2554. faceNormal = morphNormals.faceNormals[ f ];
  2555. vertexNormals = morphNormals.vertexNormals[ f ];
  2556. faceNormal.copy( face.normal );
  2557. if ( face instanceof THREE.Face3 ) {
  2558. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  2559. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  2560. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  2561. } else {
  2562. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  2563. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  2564. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  2565. vertexNormals.d.copy( face.vertexNormals[ 3 ] );
  2566. }
  2567. }
  2568. }
  2569. // restore original normals
  2570. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2571. face = this.faces[ f ];
  2572. face.normal = face.__originalFaceNormal;
  2573. face.vertexNormals = face.__originalVertexNormals;
  2574. }
  2575. },
  2576. computeTangents: function () {
  2577. // based on http://www.terathon.com/code/tangent.html
  2578. // tangents go to vertices
  2579. var f, fl, v, vl, i, il, vertexIndex,
  2580. face, uv, vA, vB, vC, uvA, uvB, uvC,
  2581. x1, x2, y1, y2, z1, z2,
  2582. s1, s2, t1, t2, r, t, test,
  2583. tan1 = [], tan2 = [],
  2584. sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
  2585. tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
  2586. n = new THREE.Vector3(), w;
  2587. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2588. tan1[ v ] = new THREE.Vector3();
  2589. tan2[ v ] = new THREE.Vector3();
  2590. }
  2591. function handleTriangle( context, a, b, c, ua, ub, uc ) {
  2592. vA = context.vertices[ a ].position;
  2593. vB = context.vertices[ b ].position;
  2594. vC = context.vertices[ c ].position;
  2595. uvA = uv[ ua ];
  2596. uvB = uv[ ub ];
  2597. uvC = uv[ uc ];
  2598. x1 = vB.x - vA.x;
  2599. x2 = vC.x - vA.x;
  2600. y1 = vB.y - vA.y;
  2601. y2 = vC.y - vA.y;
  2602. z1 = vB.z - vA.z;
  2603. z2 = vC.z - vA.z;
  2604. s1 = uvB.u - uvA.u;
  2605. s2 = uvC.u - uvA.u;
  2606. t1 = uvB.v - uvA.v;
  2607. t2 = uvC.v - uvA.v;
  2608. r = 1.0 / ( s1 * t2 - s2 * t1 );
  2609. sdir.set( ( t2 * x1 - t1 * x2 ) * r,
  2610. ( t2 * y1 - t1 * y2 ) * r,
  2611. ( t2 * z1 - t1 * z2 ) * r );
  2612. tdir.set( ( s1 * x2 - s2 * x1 ) * r,
  2613. ( s1 * y2 - s2 * y1 ) * r,
  2614. ( s1 * z2 - s2 * z1 ) * r );
  2615. tan1[ a ].addSelf( sdir );
  2616. tan1[ b ].addSelf( sdir );
  2617. tan1[ c ].addSelf( sdir );
  2618. tan2[ a ].addSelf( tdir );
  2619. tan2[ b ].addSelf( tdir );
  2620. tan2[ c ].addSelf( tdir );
  2621. }
  2622. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2623. face = this.faces[ f ];
  2624. uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents
  2625. if ( face instanceof THREE.Face3 ) {
  2626. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  2627. } else if ( face instanceof THREE.Face4 ) {
  2628. handleTriangle( this, face.a, face.b, face.d, 0, 1, 3 );
  2629. handleTriangle( this, face.b, face.c, face.d, 1, 2, 3 );
  2630. }
  2631. }
  2632. var faceIndex = [ 'a', 'b', 'c', 'd' ];
  2633. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2634. face = this.faces[ f ];
  2635. for ( i = 0; i < face.vertexNormals.length; i++ ) {
  2636. n.copy( face.vertexNormals[ i ] );
  2637. vertexIndex = face[ faceIndex[ i ] ];
  2638. t = tan1[ vertexIndex ];
  2639. // Gram-Schmidt orthogonalize
  2640. tmp.copy( t );
  2641. tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
  2642. // Calculate handedness
  2643. tmp2.cross( face.vertexNormals[ i ], t );
  2644. test = tmp2.dot( tan2[ vertexIndex ] );
  2645. w = (test < 0.0) ? -1.0 : 1.0;
  2646. face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w );
  2647. }
  2648. }
  2649. this.hasTangents = true;
  2650. },
  2651. computeBoundingBox: function () {
  2652. if ( ! this.boundingBox ) {
  2653. this.boundingBox = { min: new THREE.Vector3(), max: new THREE.Vector3() };
  2654. }
  2655. if ( this.vertices.length > 0 ) {
  2656. var position, firstPosition = this.vertices[ 0 ].position;
  2657. this.boundingBox.min.copy( firstPosition );
  2658. this.boundingBox.max.copy( firstPosition );
  2659. var min = this.boundingBox.min,
  2660. max = this.boundingBox.max;
  2661. for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
  2662. position = this.vertices[ v ].position;
  2663. if ( position.x < min.x ) {
  2664. min.x = position.x;
  2665. } else if ( position.x > max.x ) {
  2666. max.x = position.x;
  2667. }
  2668. if ( position.y < min.y ) {
  2669. min.y = position.y;
  2670. } else if ( position.y > max.y ) {
  2671. max.y = position.y;
  2672. }
  2673. if ( position.z < min.z ) {
  2674. min.z = position.z;
  2675. } else if ( position.z > max.z ) {
  2676. max.z = position.z;
  2677. }
  2678. }
  2679. } else {
  2680. this.boundingBox.min.set( 0, 0, 0 );
  2681. this.boundingBox.max.set( 0, 0, 0 );
  2682. }
  2683. },
  2684. computeBoundingSphere: function () {
  2685. if ( ! this.boundingSphere ) this.boundingSphere = { radius: 0 };
  2686. var radius, maxRadius = 0;
  2687. for ( var v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2688. radius = this.vertices[ v ].position.length();
  2689. if ( radius > maxRadius ) maxRadius = radius;
  2690. }
  2691. this.boundingSphere.radius = maxRadius;
  2692. },
  2693. /*
  2694. * Checks for duplicate vertices with hashmap.
  2695. * Duplicated vertices are removed
  2696. * and faces' vertices are updated.
  2697. */
  2698. mergeVertices: function() {
  2699. var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
  2700. var unique = [], changes = [];
  2701. var v, key;
  2702. var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
  2703. var precision = Math.pow( 10, precisionPoints );
  2704. var i,il, face;
  2705. for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
  2706. v = this.vertices[ i ].position;
  2707. key = [ Math.round( v.x * precision ), Math.round( v.y * precision ), Math.round( v.z * precision ) ].join( '_' );
  2708. if ( verticesMap[ key ] === undefined ) {
  2709. verticesMap[ key ] = i;
  2710. unique.push( this.vertices[ i ] );
  2711. changes[ i ] = unique.length - 1;
  2712. } else {
  2713. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  2714. changes[ i ] = changes[ verticesMap[ key ] ];
  2715. }
  2716. };
  2717. // Start to patch face indices
  2718. for( i = 0, il = this.faces.length; i < il; i ++ ) {
  2719. face = this.faces[ i ];
  2720. if ( face instanceof THREE.Face3 ) {
  2721. face.a = changes[ face.a ];
  2722. face.b = changes[ face.b ];
  2723. face.c = changes[ face.c ];
  2724. } else if ( face instanceof THREE.Face4 ) {
  2725. face.a = changes[ face.a ];
  2726. face.b = changes[ face.b ];
  2727. face.c = changes[ face.c ];
  2728. face.d = changes[ face.d ];
  2729. }
  2730. }
  2731. // Use unique set of vertices
  2732. this.vertices = unique;
  2733. }
  2734. };
  2735. THREE.GeometryCount = 0;
  2736. /**
  2737. * Spline from Tween.js, slightly optimized (and trashed)
  2738. * http://sole.github.com/tween.js/examples/05_spline.html
  2739. *
  2740. * @author mrdoob / http://mrdoob.com/
  2741. * @author alteredq / http://alteredqualia.com/
  2742. */
  2743. THREE.Spline = function ( points ) {
  2744. this.points = points;
  2745. var c = [], v3 = { x: 0, y: 0, z: 0 },
  2746. point, intPoint, weight, w2, w3,
  2747. pa, pb, pc, pd;
  2748. this.initFromArray = function( a ) {
  2749. this.points = [];
  2750. for ( var i = 0; i < a.length; i++ ) {
  2751. this.points[ i ] = { x: a[ i ][ 0 ], y: a[ i ][ 1 ], z: a[ i ][ 2 ] };
  2752. }
  2753. };
  2754. this.getPoint = function ( k ) {
  2755. point = ( this.points.length - 1 ) * k;
  2756. intPoint = Math.floor( point );
  2757. weight = point - intPoint;
  2758. c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1;
  2759. c[ 1 ] = intPoint;
  2760. c[ 2 ] = intPoint > this.points.length - 2 ? this.points.length - 1 : intPoint + 1;
  2761. c[ 3 ] = intPoint > this.points.length - 3 ? this.points.length - 1 : intPoint + 2;
  2762. pa = this.points[ c[ 0 ] ];
  2763. pb = this.points[ c[ 1 ] ];
  2764. pc = this.points[ c[ 2 ] ];
  2765. pd = this.points[ c[ 3 ] ];
  2766. w2 = weight * weight;
  2767. w3 = weight * w2;
  2768. v3.x = interpolate( pa.x, pb.x, pc.x, pd.x, weight, w2, w3 );
  2769. v3.y = interpolate( pa.y, pb.y, pc.y, pd.y, weight, w2, w3 );
  2770. v3.z = interpolate( pa.z, pb.z, pc.z, pd.z, weight, w2, w3 );
  2771. return v3;
  2772. };
  2773. this.getControlPointsArray = function () {
  2774. var i, p, l = this.points.length,
  2775. coords = [];
  2776. for ( i = 0; i < l; i ++ ) {
  2777. p = this.points[ i ];
  2778. coords[ i ] = [ p.x, p.y, p.z ];
  2779. }
  2780. return coords;
  2781. };
  2782. // approximate length by summing linear segments
  2783. this.getLength = function ( nSubDivisions ) {
  2784. var i, index, nSamples, position,
  2785. point = 0, intPoint = 0, oldIntPoint = 0,
  2786. oldPosition = new THREE.Vector3(),
  2787. tmpVec = new THREE.Vector3(),
  2788. chunkLengths = [],
  2789. totalLength = 0;
  2790. // first point has 0 length
  2791. chunkLengths[ 0 ] = 0;
  2792. if ( !nSubDivisions ) nSubDivisions = 100;
  2793. nSamples = this.points.length * nSubDivisions;
  2794. oldPosition.copy( this.points[ 0 ] );
  2795. for ( i = 1; i < nSamples; i ++ ) {
  2796. index = i / nSamples;
  2797. position = this.getPoint( index );
  2798. tmpVec.copy( position );
  2799. totalLength += tmpVec.distanceTo( oldPosition );
  2800. oldPosition.copy( position );
  2801. point = ( this.points.length - 1 ) * index;
  2802. intPoint = Math.floor( point );
  2803. if ( intPoint != oldIntPoint ) {
  2804. chunkLengths[ intPoint ] = totalLength;
  2805. oldIntPoint = intPoint;
  2806. }
  2807. }
  2808. // last point ends with total length
  2809. chunkLengths[ chunkLengths.length ] = totalLength;
  2810. return { chunks: chunkLengths, total: totalLength };
  2811. };
  2812. this.reparametrizeByArcLength = function ( samplingCoef ) {
  2813. var i, j,
  2814. index, indexCurrent, indexNext,
  2815. linearDistance, realDistance,
  2816. sampling, position,
  2817. newpoints = [],
  2818. tmpVec = new THREE.Vector3(),
  2819. sl = this.getLength();
  2820. newpoints.push( tmpVec.copy( this.points[ 0 ] ).clone() );
  2821. for ( i = 1; i < this.points.length; i++ ) {
  2822. //tmpVec.copy( this.points[ i - 1 ] );
  2823. //linearDistance = tmpVec.distanceTo( this.points[ i ] );
  2824. realDistance = sl.chunks[ i ] - sl.chunks[ i - 1 ];
  2825. sampling = Math.ceil( samplingCoef * realDistance / sl.total );
  2826. indexCurrent = ( i - 1 ) / ( this.points.length - 1 );
  2827. indexNext = i / ( this.points.length - 1 );
  2828. for ( j = 1; j < sampling - 1; j++ ) {
  2829. index = indexCurrent + j * ( 1 / sampling ) * ( indexNext - indexCurrent );
  2830. position = this.getPoint( index );
  2831. newpoints.push( tmpVec.copy( position ).clone() );
  2832. }
  2833. newpoints.push( tmpVec.copy( this.points[ i ] ).clone() );
  2834. }
  2835. this.points = newpoints;
  2836. };
  2837. // Catmull-Rom
  2838. function interpolate( p0, p1, p2, p3, t, t2, t3 ) {
  2839. var v0 = ( p2 - p0 ) * 0.5,
  2840. v1 = ( p3 - p1 ) * 0.5;
  2841. return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  2842. };
  2843. };
  2844. /**
  2845. * @author mr.doob / http://mrdoob.com/
  2846. * @author mikael emtinger / http://gomo.se/
  2847. */
  2848. THREE.Camera = function () {
  2849. THREE.Object3D.call( this );
  2850. this.matrixWorldInverse = new THREE.Matrix4();
  2851. this.projectionMatrix = new THREE.Matrix4();
  2852. this.projectionMatrixInverse = new THREE.Matrix4();
  2853. };
  2854. THREE.Camera.prototype = new THREE.Object3D();
  2855. THREE.Camera.prototype.constructor = THREE.Camera;
  2856. THREE.Camera.prototype.lookAt = function ( vector ) {
  2857. // TODO: Add hierarchy support.
  2858. this.matrix.lookAt( this.position, vector, this.up );
  2859. if ( this.rotationAutoUpdate ) {
  2860. this.rotation.getRotationFromMatrix( this.matrix );
  2861. }
  2862. };
  2863. /**
  2864. * @author alteredq / http://alteredqualia.com/
  2865. */
  2866. THREE.OrthographicCamera = function ( left, right, top, bottom, near, far ) {
  2867. THREE.Camera.call( this );
  2868. this.left = left;
  2869. this.right = right;
  2870. this.top = top;
  2871. this.bottom = bottom;
  2872. this.near = ( near !== undefined ) ? near : 0.1;
  2873. this.far = ( far !== undefined ) ? far : 2000;
  2874. this.updateProjectionMatrix();
  2875. };
  2876. THREE.OrthographicCamera.prototype = new THREE.Camera();
  2877. THREE.OrthographicCamera.prototype.constructor = THREE.OrthographicCamera;
  2878. THREE.OrthographicCamera.prototype.updateProjectionMatrix = function () {
  2879. this.projectionMatrix.makeOrthographic( this.left, this.right, this.top, this.bottom, this.near, this.far );
  2880. };
  2881. /**
  2882. * @author mr.doob / http://mrdoob.com/
  2883. * @author greggman / http://games.greggman.com/
  2884. * @author zz85 / http://www.lab4games.net/zz85/blog
  2885. */
  2886. THREE.PerspectiveCamera = function ( fov, aspect, near, far ) {
  2887. THREE.Camera.call( this );
  2888. this.fov = fov !== undefined ? fov : 50;
  2889. this.aspect = aspect !== undefined ? aspect : 1;
  2890. this.near = near !== undefined ? near : 0.1;
  2891. this.far = far !== undefined ? far : 2000;
  2892. this.updateProjectionMatrix();
  2893. };
  2894. THREE.PerspectiveCamera.prototype = new THREE.Camera();
  2895. THREE.PerspectiveCamera.prototype.constructor = THREE.PerspectiveCamera;
  2896. /**
  2897. * Uses Focal Length (in mm) to estimate and set FOV
  2898. * 35mm (fullframe) camera is used if frame size is not specified;
  2899. * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html
  2900. */
  2901. THREE.PerspectiveCamera.prototype.setLens = function ( focalLength, frameHeight ) {
  2902. frameHeight = frameHeight !== undefined ? frameHeight : 24;
  2903. this.fov = 2 * Math.atan( frameHeight / ( focalLength * 2 ) ) * ( 180 / Math.PI );
  2904. this.updateProjectionMatrix();
  2905. }
  2906. /**
  2907. * Sets an offset in a larger frustum. This is useful for multi-window or
  2908. * multi-monitor/multi-machine setups.
  2909. *
  2910. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  2911. * the monitors are in grid like this
  2912. *
  2913. * +---+---+---+
  2914. * | A | B | C |
  2915. * +---+---+---+
  2916. * | D | E | F |
  2917. * +---+---+---+
  2918. *
  2919. * then for each monitor you would call it like this
  2920. *
  2921. * var w = 1920;
  2922. * var h = 1080;
  2923. * var fullWidth = w * 3;
  2924. * var fullHeight = h * 2;
  2925. *
  2926. * --A--
  2927. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  2928. * --B--
  2929. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  2930. * --C--
  2931. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  2932. * --D--
  2933. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  2934. * --E--
  2935. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  2936. * --F--
  2937. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  2938. *
  2939. * Note there is no reason monitors have to be the same size or in a grid.
  2940. */
  2941. THREE.PerspectiveCamera.prototype.setViewOffset = function ( fullWidth, fullHeight, x, y, width, height ) {
  2942. this.fullWidth = fullWidth;
  2943. this.fullHeight = fullHeight;
  2944. this.x = x;
  2945. this.y = y;
  2946. this.width = width;
  2947. this.height = height;
  2948. this.updateProjectionMatrix();
  2949. };
  2950. THREE.PerspectiveCamera.prototype.updateProjectionMatrix = function () {
  2951. if ( this.fullWidth ) {
  2952. var aspect = this.fullWidth / this.fullHeight;
  2953. var top = Math.tan( this.fov * Math.PI / 360 ) * this.near;
  2954. var bottom = -top;
  2955. var left = aspect * bottom;
  2956. var right = aspect * top;
  2957. var width = Math.abs( right - left );
  2958. var height = Math.abs( top - bottom );
  2959. this.projectionMatrix.makeFrustum(
  2960. left + this.x * width / this.fullWidth,
  2961. left + ( this.x + this.width ) * width / this.fullWidth,
  2962. top - ( this.y + this.height ) * height / this.fullHeight,
  2963. top - this.y * height / this.fullHeight,
  2964. this.near,
  2965. this.far
  2966. );
  2967. } else {
  2968. this.projectionMatrix.makePerspective( this.fov, this.aspect, this.near, this.far );
  2969. }
  2970. };
  2971. /**
  2972. * @author mr.doob / http://mrdoob.com/
  2973. * @author alteredq / http://alteredqualia.com/
  2974. */
  2975. THREE.Light = function ( hex ) {
  2976. THREE.Object3D.call( this );
  2977. this.color = new THREE.Color( hex );
  2978. };
  2979. THREE.Light.prototype = new THREE.Object3D();
  2980. THREE.Light.prototype.constructor = THREE.Light;
  2981. THREE.Light.prototype.supr = THREE.Object3D.prototype;
  2982. /**
  2983. * @author mr.doob / http://mrdoob.com/
  2984. */
  2985. THREE.AmbientLight = function ( hex ) {
  2986. THREE.Light.call( this, hex );
  2987. };
  2988. THREE.AmbientLight.prototype = new THREE.Light();
  2989. THREE.AmbientLight.prototype.constructor = THREE.AmbientLight;
  2990. /**
  2991. * @author mr.doob / http://mrdoob.com/
  2992. * @author alteredq / http://alteredqualia.com/
  2993. */
  2994. THREE.DirectionalLight = function ( hex, intensity, distance ) {
  2995. THREE.Light.call( this, hex );
  2996. this.position = new THREE.Vector3( 0, 1, 0 );
  2997. this.target = new THREE.Object3D();
  2998. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  2999. this.distance = ( distance !== undefined ) ? distance : 0;
  3000. this.castShadow = false;
  3001. this.onlyShadow = false;
  3002. //
  3003. this.shadowCameraNear = 50;
  3004. this.shadowCameraFar = 5000;
  3005. this.shadowCameraLeft = -500;
  3006. this.shadowCameraRight = 500;
  3007. this.shadowCameraTop = 500;
  3008. this.shadowCameraBottom = -500;
  3009. this.shadowCameraVisible = false;
  3010. this.shadowBias = 0;
  3011. this.shadowDarkness = 0.5;
  3012. this.shadowMapWidth = 512;
  3013. this.shadowMapHeight = 512;
  3014. //
  3015. this.shadowCascade = false;
  3016. this.shadowCascadeOffset = new THREE.Vector3( 0, 0, -1000 );
  3017. this.shadowCascadeCount = 2;
  3018. this.shadowCascadeBias = [ 0, 0, 0 ];
  3019. this.shadowCascadeWidth = [ 512, 512, 512 ];
  3020. this.shadowCascadeHeight = [ 512, 512, 512 ];
  3021. this.shadowCascadeNearZ = [ -1.000, 0.990, 0.998 ];
  3022. this.shadowCascadeFarZ = [ 0.990, 0.998, 1.000 ];
  3023. this.shadowCascadeArray = [];
  3024. //
  3025. this.shadowMap = null;
  3026. this.shadowMapSize = null;
  3027. this.shadowCamera = null;
  3028. this.shadowMatrix = null;
  3029. };
  3030. THREE.DirectionalLight.prototype = new THREE.Light();
  3031. THREE.DirectionalLight.prototype.constructor = THREE.DirectionalLight;
  3032. /**
  3033. * @author mr.doob / http://mrdoob.com/
  3034. */
  3035. THREE.PointLight = function ( hex, intensity, distance ) {
  3036. THREE.Light.call( this, hex );
  3037. this.position = new THREE.Vector3( 0, 0, 0 );
  3038. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  3039. this.distance = ( distance !== undefined ) ? distance : 0;
  3040. };
  3041. THREE.PointLight.prototype = new THREE.Light();
  3042. THREE.PointLight.prototype.constructor = THREE.PointLight;
  3043. /**
  3044. * @author alteredq / http://alteredqualia.com/
  3045. */
  3046. THREE.SpotLight = function ( hex, intensity, distance ) {
  3047. THREE.Light.call( this, hex );
  3048. this.position = new THREE.Vector3( 0, 1, 0 );
  3049. this.target = new THREE.Object3D();
  3050. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  3051. this.distance = ( distance !== undefined ) ? distance : 0;
  3052. this.castShadow = false;
  3053. this.onlyShadow = false;
  3054. //
  3055. this.shadowCameraNear = 50;
  3056. this.shadowCameraFar = 5000;
  3057. this.shadowCameraFov = 50;
  3058. this.shadowCameraVisible = false;
  3059. this.shadowBias = 0;
  3060. this.shadowDarkness = 0.5;
  3061. this.shadowMapWidth = 512;
  3062. this.shadowMapHeight = 512;
  3063. //
  3064. this.shadowMap = null;
  3065. this.shadowMapSize = null;
  3066. this.shadowCamera = null;
  3067. this.shadowMatrix = null;
  3068. };
  3069. THREE.SpotLight.prototype = new THREE.Light();
  3070. THREE.SpotLight.prototype.constructor = THREE.SpotLight;
  3071. /**
  3072. * @author mr.doob / http://mrdoob.com/
  3073. * @author alteredq / http://alteredqualia.com/
  3074. */
  3075. THREE.Material = function ( parameters ) {
  3076. parameters = parameters || {};
  3077. this.id = THREE.MaterialCount ++;
  3078. this.name = '';
  3079. this.opacity = parameters.opacity !== undefined ? parameters.opacity : 1;
  3080. this.transparent = parameters.transparent !== undefined ? parameters.transparent : false;
  3081. this.blending = parameters.blending !== undefined ? parameters.blending : THREE.NormalBlending;
  3082. this.blendSrc = parameters.blendSrc !== undefined ? parameters.blendSrc : THREE.SrcAlphaFactor;
  3083. this.blendDst = parameters.blendDst !== undefined ? parameters.blendDst : THREE.OneMinusSrcAlphaFactor;
  3084. this.blendEquation = parameters.blendEquation !== undefined ? parameters.blendEquation : THREE.AddEquation;
  3085. this.depthTest = parameters.depthTest !== undefined ? parameters.depthTest : true;
  3086. this.depthWrite = parameters.depthWrite !== undefined ? parameters.depthWrite : true;
  3087. this.polygonOffset = parameters.polygonOffset !== undefined ? parameters.polygonOffset : false;
  3088. this.polygonOffsetFactor = parameters.polygonOffsetFactor !== undefined ? parameters.polygonOffsetFactor : 0;
  3089. this.polygonOffsetUnits = parameters.polygonOffsetUnits !== undefined ? parameters.polygonOffsetUnits : 0;
  3090. this.alphaTest = parameters.alphaTest !== undefined ? parameters.alphaTest : 0;
  3091. this.overdraw = parameters.overdraw !== undefined ? parameters.overdraw : false; // Boolean for fixing antialiasing gaps in CanvasRenderer
  3092. this.needsUpdate = true;
  3093. }
  3094. THREE.MaterialCount = 0;
  3095. // shading
  3096. THREE.NoShading = 0;
  3097. THREE.FlatShading = 1;
  3098. THREE.SmoothShading = 2;
  3099. // colors
  3100. THREE.NoColors = 0;
  3101. THREE.FaceColors = 1;
  3102. THREE.VertexColors = 2;
  3103. // blending modes
  3104. THREE.NoBlending = 0;
  3105. THREE.NormalBlending = 1;
  3106. THREE.AdditiveBlending = 2;
  3107. THREE.SubtractiveBlending = 3;
  3108. THREE.MultiplyBlending = 4;
  3109. THREE.AdditiveAlphaBlending = 5;
  3110. THREE.CustomBlending = 6;
  3111. // custom blending equations
  3112. // (numbers start from 100 not to clash with other
  3113. // mappings to OpenGL constants defined in Texture.js)
  3114. THREE.AddEquation = 100;
  3115. THREE.SubtractEquation = 101;
  3116. THREE.ReverseSubtractEquation = 102;
  3117. // custom blending destination factors
  3118. THREE.ZeroFactor = 200;
  3119. THREE.OneFactor = 201;
  3120. THREE.SrcColorFactor = 202;
  3121. THREE.OneMinusSrcColorFactor = 203;
  3122. THREE.SrcAlphaFactor = 204;
  3123. THREE.OneMinusSrcAlphaFactor = 205;
  3124. THREE.DstAlphaFactor = 206;
  3125. THREE.OneMinusDstAlphaFactor = 207;
  3126. // custom blending source factors
  3127. //THREE.ZeroFactor = 200;
  3128. //THREE.OneFactor = 201;
  3129. //THREE.SrcAlphaFactor = 204;
  3130. //THREE.OneMinusSrcAlphaFactor = 205;
  3131. //THREE.DstAlphaFactor = 206;
  3132. //THREE.OneMinusDstAlphaFactor = 207;
  3133. THREE.DstColorFactor = 208;
  3134. THREE.OneMinusDstColorFactor = 209;
  3135. THREE.SrcAlphaSaturateFactor = 210;
  3136. /**
  3137. * @author mr.doob / http://mrdoob.com/
  3138. * @author alteredq / http://alteredqualia.com/
  3139. *
  3140. * parameters = {
  3141. * color: <hex>,
  3142. * opacity: <float>,
  3143. *
  3144. * blending: THREE.NormalBlending,
  3145. * depthTest: <bool>,
  3146. *
  3147. * linewidth: <float>,
  3148. * linecap: "round",
  3149. * linejoin: "round",
  3150. *
  3151. * vertexColors: <bool>
  3152. *
  3153. * fog: <bool>
  3154. * }
  3155. */
  3156. THREE.LineBasicMaterial = function ( parameters ) {
  3157. THREE.Material.call( this, parameters );
  3158. parameters = parameters || {};
  3159. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3160. this.linewidth = parameters.linewidth !== undefined ? parameters.linewidth : 1;
  3161. this.linecap = parameters.linecap !== undefined ? parameters.linecap : 'round';
  3162. this.linejoin = parameters.linejoin !== undefined ? parameters.linejoin : 'round';
  3163. this.vertexColors = parameters.vertexColors ? parameters.vertexColors : false;
  3164. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3165. };
  3166. THREE.LineBasicMaterial.prototype = new THREE.Material();
  3167. THREE.LineBasicMaterial.prototype.constructor = THREE.LineBasicMaterial;
  3168. /**
  3169. * @author mr.doob / http://mrdoob.com/
  3170. * @author alteredq / http://alteredqualia.com/
  3171. *
  3172. * parameters = {
  3173. * color: <hex>,
  3174. * opacity: <float>,
  3175. * map: new THREE.Texture( <Image> ),
  3176. *
  3177. * lightMap: new THREE.Texture( <Image> ),
  3178. *
  3179. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3180. * combine: THREE.Multiply,
  3181. * reflectivity: <float>,
  3182. * refractionRatio: <float>,
  3183. *
  3184. * shading: THREE.SmoothShading,
  3185. * blending: THREE.NormalBlending,
  3186. * depthTest: <bool>,
  3187. *
  3188. * wireframe: <boolean>,
  3189. * wireframeLinewidth: <float>,
  3190. *
  3191. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3192. *
  3193. * skinning: <bool>,
  3194. * morphTargets: <bool>,
  3195. *
  3196. * fog: <bool>
  3197. * }
  3198. */
  3199. THREE.MeshBasicMaterial = function ( parameters ) {
  3200. THREE.Material.call( this, parameters );
  3201. parameters = parameters || {};
  3202. // color property represents emissive for MeshBasicMaterial
  3203. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3204. this.map = parameters.map !== undefined ? parameters.map : null;
  3205. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3206. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3207. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3208. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3209. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3210. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3211. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3212. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3213. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3214. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3215. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3216. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3217. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3218. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3219. };
  3220. THREE.MeshBasicMaterial.prototype = new THREE.Material();
  3221. THREE.MeshBasicMaterial.prototype.constructor = THREE.MeshBasicMaterial;
  3222. /**
  3223. * @author mr.doob / http://mrdoob.com/
  3224. * @author alteredq / http://alteredqualia.com/
  3225. *
  3226. * parameters = {
  3227. * color: <hex>,
  3228. * ambient: <hex>,
  3229. * emissive: <hex>,
  3230. * opacity: <float>,
  3231. *
  3232. * map: new THREE.Texture( <Image> ),
  3233. *
  3234. * lightMap: new THREE.Texture( <Image> ),
  3235. *
  3236. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3237. * combine: THREE.Multiply,
  3238. * reflectivity: <float>,
  3239. * refractionRatio: <float>,
  3240. *
  3241. * shading: THREE.SmoothShading,
  3242. * blending: THREE.NormalBlending,
  3243. * depthTest: <bool>,
  3244. *
  3245. * wireframe: <boolean>,
  3246. * wireframeLinewidth: <float>,
  3247. *
  3248. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3249. *
  3250. * skinning: <bool>,
  3251. * morphTargets: <bool>,
  3252. * morphNormals: <bool>,
  3253. *
  3254. * fog: <bool>
  3255. * }
  3256. */
  3257. THREE.MeshLambertMaterial = function ( parameters ) {
  3258. THREE.Material.call( this, parameters );
  3259. parameters = parameters || {};
  3260. // color property represents diffuse for MeshLambertMaterial
  3261. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3262. this.ambient = parameters.ambient !== undefined ? new THREE.Color( parameters.ambient ) : new THREE.Color( 0xffffff );
  3263. this.emissive = parameters.emissive !== undefined ? new THREE.Color( parameters.emissive ) : new THREE.Color( 0x000000 );
  3264. this.wrapAround = parameters.wrapAround !== undefined ? parameters.wrapAround: false;
  3265. this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
  3266. this.map = parameters.map !== undefined ? parameters.map : null;
  3267. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3268. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3269. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3270. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3271. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3272. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3273. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3274. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3275. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3276. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3277. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3278. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3279. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3280. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3281. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false;
  3282. };
  3283. THREE.MeshLambertMaterial.prototype = new THREE.Material();
  3284. THREE.MeshLambertMaterial.prototype.constructor = THREE.MeshLambertMaterial;
  3285. /**
  3286. * @author mr.doob / http://mrdoob.com/
  3287. * @author alteredq / http://alteredqualia.com/
  3288. *
  3289. * parameters = {
  3290. * color: <hex>,
  3291. * ambient: <hex>,
  3292. * emissive: <hex>,
  3293. * specular: <hex>,
  3294. * shininess: <float>,
  3295. * opacity: <float>,
  3296. *
  3297. * map: new THREE.Texture( <Image> ),
  3298. *
  3299. * lightMap: new THREE.Texture( <Image> ),
  3300. *
  3301. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3302. * combine: THREE.Multiply,
  3303. * reflectivity: <float>,
  3304. * refractionRatio: <float>,
  3305. *
  3306. * shading: THREE.SmoothShading,
  3307. * blending: THREE.NormalBlending,
  3308. * depthTest: <bool>,
  3309. *
  3310. * wireframe: <boolean>,
  3311. * wireframeLinewidth: <float>,
  3312. *
  3313. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3314. *
  3315. * skinning: <bool>,
  3316. * morphTargets: <bool>,
  3317. * morphNormals: <bool>,
  3318. *
  3319. * fog: <bool>
  3320. * }
  3321. */
  3322. THREE.MeshPhongMaterial = function ( parameters ) {
  3323. THREE.Material.call( this, parameters );
  3324. parameters = parameters || {};
  3325. // color property represents diffuse for MeshPhongMaterial
  3326. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3327. this.ambient = parameters.ambient !== undefined ? new THREE.Color( parameters.ambient ) : new THREE.Color( 0xffffff );
  3328. this.emissive = parameters.emissive !== undefined ? new THREE.Color( parameters.emissive ) : new THREE.Color( 0x000000 );
  3329. this.specular = parameters.specular !== undefined ? new THREE.Color( parameters.specular ) : new THREE.Color( 0x111111 );
  3330. this.shininess = parameters.shininess !== undefined ? parameters.shininess : 30;
  3331. this.metal = parameters.metal !== undefined ? parameters.metal : false;
  3332. this.perPixel = parameters.perPixel !== undefined ? parameters.perPixel : false;
  3333. this.wrapAround = parameters.wrapAround !== undefined ? parameters.wrapAround: false;
  3334. this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
  3335. this.map = parameters.map !== undefined ? parameters.map : null;
  3336. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3337. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3338. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3339. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3340. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3341. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3342. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3343. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3344. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3345. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3346. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3347. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3348. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3349. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3350. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false;
  3351. };
  3352. THREE.MeshPhongMaterial.prototype = new THREE.Material();
  3353. THREE.MeshPhongMaterial.prototype.constructor = THREE.MeshPhongMaterial;
  3354. /**
  3355. * @author mr.doob / http://mrdoob.com/
  3356. * @author alteredq / http://alteredqualia.com/
  3357. *
  3358. * parameters = {
  3359. * opacity: <float>,
  3360. * blending: THREE.NormalBlending,
  3361. * depthTest: <bool>,
  3362. * wireframe: <boolean>,
  3363. * wireframeLinewidth: <float>
  3364. * }
  3365. */
  3366. THREE.MeshDepthMaterial = function ( parameters ) {
  3367. THREE.Material.call( this, parameters );
  3368. parameters = parameters || {};
  3369. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading; // doesn't really apply here, normals are not used
  3370. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3371. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3372. };
  3373. THREE.MeshDepthMaterial.prototype = new THREE.Material();
  3374. THREE.MeshDepthMaterial.prototype.constructor = THREE.MeshDepthMaterial;
  3375. /**
  3376. * @author mr.doob / http://mrdoob.com/
  3377. *
  3378. * parameters = {
  3379. * opacity: <float>,
  3380. * shading: THREE.FlatShading,
  3381. * blending: THREE.NormalBlending,
  3382. * depthTest: <bool>,
  3383. * wireframe: <boolean>,
  3384. * wireframeLinewidth: <float>
  3385. * }
  3386. */
  3387. THREE.MeshNormalMaterial = function ( parameters ) {
  3388. THREE.Material.call( this, parameters );
  3389. parameters = parameters || {};
  3390. this.shading = parameters.shading ? parameters.shading : THREE.FlatShading;
  3391. this.wireframe = parameters.wireframe ? parameters.wireframe : false;
  3392. this.wireframeLinewidth = parameters.wireframeLinewidth ? parameters.wireframeLinewidth : 1;
  3393. };
  3394. THREE.MeshNormalMaterial.prototype = new THREE.Material();
  3395. THREE.MeshNormalMaterial.prototype.constructor = THREE.MeshNormalMaterial;
  3396. /**
  3397. * @author mr.doob / http://mrdoob.com/
  3398. */
  3399. THREE.MeshFaceMaterial = function () {
  3400. };
  3401. /**
  3402. * @author mr.doob / http://mrdoob.com/
  3403. * @author alteredq / http://alteredqualia.com/
  3404. *
  3405. * parameters = {
  3406. * color: <hex>,
  3407. * opacity: <float>,
  3408. * map: new THREE.Texture( <Image> ),
  3409. *
  3410. * size: <float>,
  3411. *
  3412. * blending: THREE.NormalBlending,
  3413. * depthTest: <bool>,
  3414. *
  3415. * vertexColors: <bool>,
  3416. *
  3417. * fog: <bool>
  3418. * }
  3419. */
  3420. THREE.ParticleBasicMaterial = function ( parameters ) {
  3421. THREE.Material.call( this, parameters );
  3422. parameters = parameters || {};
  3423. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3424. this.map = parameters.map !== undefined ? parameters.map : null;
  3425. this.size = parameters.size !== undefined ? parameters.size : 1;
  3426. this.sizeAttenuation = parameters.sizeAttenuation !== undefined ? parameters.sizeAttenuation : true;
  3427. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : false;
  3428. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3429. };
  3430. THREE.ParticleBasicMaterial.prototype = new THREE.Material();
  3431. THREE.ParticleBasicMaterial.prototype.constructor = THREE.ParticleBasicMaterial;
  3432. /**
  3433. * @author alteredq / http://alteredqualia.com/
  3434. *
  3435. * parameters = {
  3436. * fragmentShader: <string>,
  3437. * vertexShader: <string>,
  3438. *
  3439. * uniforms: { "parameter1": { type: "f", value: 1.0 }, "parameter2": { type: "i" value2: 2 } },
  3440. *
  3441. * shading: THREE.SmoothShading,
  3442. * blending: THREE.NormalBlending,
  3443. * depthTest: <bool>,
  3444. *
  3445. * wireframe: <boolean>,
  3446. * wireframeLinewidth: <float>,
  3447. *
  3448. * lights: <bool>,
  3449. *
  3450. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3451. *
  3452. * skinning: <bool>,
  3453. * morphTargets: <bool>,
  3454. * morphNormals: <bool>,
  3455. *
  3456. * fog: <bool>
  3457. * }
  3458. */
  3459. THREE.ShaderMaterial = function ( parameters ) {
  3460. THREE.Material.call( this, parameters );
  3461. parameters = parameters || {};
  3462. this.fragmentShader = parameters.fragmentShader !== undefined ? parameters.fragmentShader : "void main() {}";
  3463. this.vertexShader = parameters.vertexShader !== undefined ? parameters.vertexShader : "void main() {}";
  3464. this.uniforms = parameters.uniforms !== undefined ? parameters.uniforms : {};
  3465. this.attributes = parameters.attributes;
  3466. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3467. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3468. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3469. this.fog = parameters.fog !== undefined ? parameters.fog : false; // set to use scene fog
  3470. this.lights = parameters.lights !== undefined ? parameters.lights : false; // set to use scene lights
  3471. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors; // set to use "color" attribute stream
  3472. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false; // set to use skinning attribute streams
  3473. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false; // set to use morph targets
  3474. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false; // set to use morph normals
  3475. };
  3476. THREE.ShaderMaterial.prototype = new THREE.Material();
  3477. THREE.ShaderMaterial.prototype.constructor = THREE.ShaderMaterial;
  3478. /**
  3479. * @author mr.doob / http://mrdoob.com/
  3480. * @author alteredq / http://alteredqualia.com/
  3481. * @author szimek / https://github.com/szimek/
  3482. */
  3483. THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type ) {
  3484. this.id = THREE.TextureCount ++;
  3485. this.image = image;
  3486. this.mapping = mapping !== undefined ? mapping : new THREE.UVMapping();
  3487. this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
  3488. this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
  3489. this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
  3490. this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;
  3491. this.format = format !== undefined ? format : THREE.RGBAFormat;
  3492. this.type = type !== undefined ? type : THREE.UnsignedByteType;
  3493. this.offset = new THREE.Vector2( 0, 0 );
  3494. this.repeat = new THREE.Vector2( 1, 1 );
  3495. this.generateMipmaps = true;
  3496. this.premultiplyAlpha = false;
  3497. this.needsUpdate = false;
  3498. this.onUpdate = null;
  3499. };
  3500. THREE.Texture.prototype = {
  3501. constructor: THREE.Texture,
  3502. clone: function () {
  3503. var clonedTexture = new THREE.Texture( this.image, this.mapping, this.wrapS, this.wrapT, this.magFilter, this.minFilter, this.format, this.type );
  3504. clonedTexture.offset.copy( this.offset );
  3505. clonedTexture.repeat.copy( this.repeat );
  3506. return clonedTexture;
  3507. }
  3508. };
  3509. THREE.TextureCount = 0;
  3510. THREE.MultiplyOperation = 0;
  3511. THREE.MixOperation = 1;
  3512. // Mapping modes
  3513. THREE.UVMapping = function () {};
  3514. THREE.CubeReflectionMapping = function () {};
  3515. THREE.CubeRefractionMapping = function () {};
  3516. THREE.SphericalReflectionMapping = function () {};
  3517. THREE.SphericalRefractionMapping = function () {};
  3518. // Wrapping modes
  3519. THREE.RepeatWrapping = 0;
  3520. THREE.ClampToEdgeWrapping = 1;
  3521. THREE.MirroredRepeatWrapping = 2;
  3522. // Filters
  3523. THREE.NearestFilter = 3;
  3524. THREE.NearestMipMapNearestFilter = 4;
  3525. THREE.NearestMipMapLinearFilter = 5;
  3526. THREE.LinearFilter = 6;
  3527. THREE.LinearMipMapNearestFilter = 7;
  3528. THREE.LinearMipMapLinearFilter = 8;
  3529. // Types
  3530. THREE.ByteType = 9;
  3531. THREE.UnsignedByteType = 10;
  3532. THREE.ShortType = 11;
  3533. THREE.UnsignedShortType = 12;
  3534. THREE.IntType = 13;
  3535. THREE.UnsignedIntType = 14;
  3536. THREE.FloatType = 15;
  3537. // Formats
  3538. THREE.AlphaFormat = 16;
  3539. THREE.RGBFormat = 17;
  3540. THREE.RGBAFormat = 18;
  3541. THREE.LuminanceFormat = 19;
  3542. THREE.LuminanceAlphaFormat = 20;
  3543. /**
  3544. * @author alteredq / http://alteredqualia.com/
  3545. */
  3546. THREE.DataTexture = function ( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter ) {
  3547. THREE.Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type );
  3548. this.image = { data: data, width: width, height: height };
  3549. };
  3550. THREE.DataTexture.prototype = new THREE.Texture();
  3551. THREE.DataTexture.prototype.constructor = THREE.DataTexture;
  3552. THREE.DataTexture.prototype.clone = function () {
  3553. var clonedTexture = new THREE.DataTexture( this.image.data, this.image.width, this.image.height, this.format, this.type, this.mapping, this.wrapS, this.wrapT, this.magFilter, this.minFilter );
  3554. clonedTexture.offset.copy( this.offset );
  3555. clonedTexture.repeat.copy( this.repeat );
  3556. return clonedTexture;
  3557. };
  3558. /**
  3559. * @author mr.doob / http://mrdoob.com/
  3560. */
  3561. THREE.Particle = function ( material ) {
  3562. THREE.Object3D.call( this );
  3563. this.material = material;
  3564. };
  3565. THREE.Particle.prototype = new THREE.Object3D();
  3566. THREE.Particle.prototype.constructor = THREE.Particle;
  3567. /**
  3568. * @author alteredq / http://alteredqualia.com/
  3569. */
  3570. THREE.ParticleSystem = function ( geometry, material ) {
  3571. THREE.Object3D.call( this );
  3572. this.geometry = geometry;
  3573. this.material = ( material !== undefined ) ? material : new THREE.ParticleBasicMaterial( { color: Math.random() * 0xffffff } );
  3574. this.sortParticles = false;
  3575. if ( this.geometry ) {
  3576. // calc bound radius
  3577. if( !this.geometry.boundingSphere ) {
  3578. this.geometry.computeBoundingSphere();
  3579. }
  3580. this.boundRadius = geometry.boundingSphere.radius;
  3581. }
  3582. this.frustumCulled = false;
  3583. };
  3584. THREE.ParticleSystem.prototype = new THREE.Object3D();
  3585. THREE.ParticleSystem.prototype.constructor = THREE.ParticleSystem;
  3586. /**
  3587. * @author mr.doob / http://mrdoob.com/
  3588. */
  3589. THREE.Line = function ( geometry, material, type ) {
  3590. THREE.Object3D.call( this );
  3591. this.geometry = geometry;
  3592. this.material = ( material !== undefined ) ? material : new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff } );
  3593. this.type = ( type !== undefined ) ? type : THREE.LineStrip;
  3594. if ( this.geometry ) {
  3595. if ( ! this.geometry.boundingSphere ) {
  3596. this.geometry.computeBoundingSphere();
  3597. }
  3598. }
  3599. };
  3600. THREE.LineStrip = 0;
  3601. THREE.LinePieces = 1;
  3602. THREE.Line.prototype = new THREE.Object3D();
  3603. THREE.Line.prototype.constructor = THREE.Line;
  3604. /**
  3605. * @author mr.doob / http://mrdoob.com/
  3606. * @author alteredq / http://alteredqualia.com/
  3607. * @author mikael emtinger / http://gomo.se/
  3608. */
  3609. THREE.Mesh = function ( geometry, material ) {
  3610. THREE.Object3D.call( this );
  3611. this.geometry = geometry;
  3612. this.material = ( material !== undefined ) ? material : new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: true } );
  3613. if ( this.geometry ) {
  3614. // calc bound radius
  3615. if( ! this.geometry.boundingSphere ) {
  3616. this.geometry.computeBoundingSphere();
  3617. }
  3618. this.boundRadius = geometry.boundingSphere.radius;
  3619. // setup morph targets
  3620. if( this.geometry.morphTargets.length ) {
  3621. this.morphTargetBase = -1;
  3622. this.morphTargetForcedOrder = [];
  3623. this.morphTargetInfluences = [];
  3624. this.morphTargetDictionary = {};
  3625. for( var m = 0; m < this.geometry.morphTargets.length; m ++ ) {
  3626. this.morphTargetInfluences.push( 0 );
  3627. this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m;
  3628. }
  3629. }
  3630. }
  3631. }
  3632. THREE.Mesh.prototype = new THREE.Object3D();
  3633. THREE.Mesh.prototype.constructor = THREE.Mesh;
  3634. THREE.Mesh.prototype.supr = THREE.Object3D.prototype;
  3635. /*
  3636. * Get Morph Target Index by Name
  3637. */
  3638. THREE.Mesh.prototype.getMorphTargetIndexByName = function( name ) {
  3639. if ( this.morphTargetDictionary[ name ] !== undefined ) {
  3640. return this.morphTargetDictionary[ name ];
  3641. }
  3642. console.log( "THREE.Mesh.getMorphTargetIndexByName: morph target " + name + " does not exist. Returning 0." );
  3643. return 0;
  3644. }
  3645. /**
  3646. * @author mikael emtinger / http://gomo.se/
  3647. * @author alteredq / http://alteredqualia.com/
  3648. */
  3649. THREE.Bone = function( belongsToSkin ) {
  3650. THREE.Object3D.call( this );
  3651. this.skin = belongsToSkin;
  3652. this.skinMatrix = new THREE.Matrix4();
  3653. };
  3654. THREE.Bone.prototype = new THREE.Object3D();
  3655. THREE.Bone.prototype.constructor = THREE.Bone;
  3656. THREE.Bone.prototype.supr = THREE.Object3D.prototype;
  3657. THREE.Bone.prototype.update = function( parentSkinMatrix, forceUpdate ) {
  3658. // update local
  3659. if ( this.matrixAutoUpdate ) {
  3660. forceUpdate |= this.updateMatrix();
  3661. }
  3662. // update skin matrix
  3663. if ( forceUpdate || this.matrixWorldNeedsUpdate ) {
  3664. if( parentSkinMatrix ) {
  3665. this.skinMatrix.multiply( parentSkinMatrix, this.matrix );
  3666. } else {
  3667. this.skinMatrix.copy( this.matrix );
  3668. }
  3669. this.matrixWorldNeedsUpdate = false;
  3670. forceUpdate = true;
  3671. }
  3672. // update children
  3673. var child, i, l = this.children.length;
  3674. for ( i = 0; i < l; i ++ ) {
  3675. this.children[ i ].update( this.skinMatrix, forceUpdate );
  3676. }
  3677. };
  3678. /**
  3679. * @author mikael emtinger / http://gomo.se/
  3680. * @author alteredq / http://alteredqualia.com/
  3681. */
  3682. THREE.SkinnedMesh = function ( geometry, material ) {
  3683. THREE.Mesh.call( this, geometry, material );
  3684. // init bones
  3685. this.identityMatrix = new THREE.Matrix4();
  3686. this.bones = [];
  3687. this.boneMatrices = [];
  3688. var b, bone, gbone, p, q, s;
  3689. if ( this.geometry.bones !== undefined ) {
  3690. for ( b = 0; b < this.geometry.bones.length; b ++ ) {
  3691. gbone = this.geometry.bones[ b ];
  3692. p = gbone.pos;
  3693. q = gbone.rotq;
  3694. s = gbone.scl;
  3695. bone = this.addBone();
  3696. bone.name = gbone.name;
  3697. bone.position.set( p[0], p[1], p[2] );
  3698. bone.quaternion.set( q[0], q[1], q[2], q[3] );
  3699. bone.useQuaternion = true;
  3700. if ( s !== undefined ) {
  3701. bone.scale.set( s[0], s[1], s[2] );
  3702. } else {
  3703. bone.scale.set( 1, 1, 1 );
  3704. }
  3705. }
  3706. for ( b = 0; b < this.bones.length; b ++ ) {
  3707. gbone = this.geometry.bones[ b ];
  3708. bone = this.bones[ b ];
  3709. if ( gbone.parent === -1 ) {
  3710. this.add( bone );
  3711. } else {
  3712. this.bones[ gbone.parent ].add( bone );
  3713. }
  3714. }
  3715. this.boneMatrices = new Float32Array( 16 * this.bones.length );
  3716. this.pose();
  3717. }
  3718. };
  3719. THREE.SkinnedMesh.prototype = new THREE.Mesh();
  3720. THREE.SkinnedMesh.prototype.constructor = THREE.SkinnedMesh;
  3721. THREE.SkinnedMesh.prototype.addBone = function( bone ) {
  3722. if ( bone === undefined ) {
  3723. bone = new THREE.Bone( this );
  3724. }
  3725. this.bones.push( bone );
  3726. return bone;
  3727. };
  3728. THREE.SkinnedMesh.prototype.updateMatrixWorld = function ( force ) {
  3729. this.matrixAutoUpdate && this.updateMatrix();
  3730. // update matrixWorld
  3731. if ( this.matrixWorldNeedsUpdate || force ) {
  3732. if ( this.parent ) {
  3733. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  3734. } else {
  3735. this.matrixWorld.copy( this.matrix );
  3736. }
  3737. this.matrixWorldNeedsUpdate = false;
  3738. force = true;
  3739. }
  3740. // update children
  3741. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  3742. var child = this.children[ i ];
  3743. if ( child instanceof THREE.Bone ) {
  3744. child.update( this.identityMatrix, false );
  3745. } else {
  3746. child.updateMatrixWorld( true );
  3747. }
  3748. }
  3749. // flatten bone matrices to array
  3750. var b, bl = this.bones.length,
  3751. ba = this.bones,
  3752. bm = this.boneMatrices;
  3753. for ( b = 0; b < bl; b ++ ) {
  3754. ba[ b ].skinMatrix.flattenToArrayOffset( bm, b * 16 );
  3755. }
  3756. };
  3757. /*
  3758. * Pose
  3759. */
  3760. THREE.SkinnedMesh.prototype.pose = function() {
  3761. this.updateMatrixWorld( true );
  3762. var bim, bone, boneInverses = [];
  3763. for ( var b = 0; b < this.bones.length; b ++ ) {
  3764. bone = this.bones[ b ];
  3765. var inverseMatrix = new THREE.Matrix4();
  3766. inverseMatrix.getInverse( bone.skinMatrix );
  3767. boneInverses.push( inverseMatrix );
  3768. bone.skinMatrix.flattenToArrayOffset( this.boneMatrices, b * 16 );
  3769. }
  3770. // project vertices to local
  3771. if ( this.geometry.skinVerticesA === undefined ) {
  3772. this.geometry.skinVerticesA = [];
  3773. this.geometry.skinVerticesB = [];
  3774. var orgVertex, vertex;
  3775. for ( var i = 0; i < this.geometry.skinIndices.length; i ++ ) {
  3776. orgVertex = this.geometry.vertices[ i ].position;
  3777. var indexA = this.geometry.skinIndices[ i ].x;
  3778. var indexB = this.geometry.skinIndices[ i ].y;
  3779. vertex = new THREE.Vector3( orgVertex.x, orgVertex.y, orgVertex.z );
  3780. this.geometry.skinVerticesA.push( boneInverses[ indexA ].multiplyVector3( vertex ) );
  3781. vertex = new THREE.Vector3( orgVertex.x, orgVertex.y, orgVertex.z );
  3782. this.geometry.skinVerticesB.push( boneInverses[ indexB ].multiplyVector3( vertex ) );
  3783. // todo: add more influences
  3784. // normalize weights
  3785. if ( this.geometry.skinWeights[ i ].x + this.geometry.skinWeights[ i ].y !== 1 ) {
  3786. var len = ( 1.0 - ( this.geometry.skinWeights[ i ].x + this.geometry.skinWeights[ i ].y ) ) * 0.5;
  3787. this.geometry.skinWeights[ i ].x += len;
  3788. this.geometry.skinWeights[ i ].y += len;
  3789. }
  3790. }
  3791. }
  3792. };
  3793. /**
  3794. * @author alteredq / http://alteredqualia.com/
  3795. */
  3796. THREE.Ribbon = function ( geometry, material ) {
  3797. THREE.Object3D.call( this );
  3798. this.geometry = geometry;
  3799. this.material = material;
  3800. };
  3801. THREE.Ribbon.prototype = new THREE.Object3D();
  3802. THREE.Ribbon.prototype.constructor = THREE.Ribbon;
  3803. /**
  3804. * @author mikael emtinger / http://gomo.se/
  3805. * @author alteredq / http://alteredqualia.com/
  3806. * @author mr.doob / http://mrdoob.com/
  3807. */
  3808. THREE.LOD = function () {
  3809. THREE.Object3D.call( this );
  3810. this.LODs = [];
  3811. };
  3812. THREE.LOD.prototype = new THREE.Object3D();
  3813. THREE.LOD.prototype.constructor = THREE.LOD;
  3814. THREE.LOD.prototype.supr = THREE.Object3D.prototype;
  3815. THREE.LOD.prototype.addLevel = function ( object3D, visibleAtDistance ) {
  3816. if ( visibleAtDistance === undefined ) {
  3817. visibleAtDistance = 0;
  3818. }
  3819. visibleAtDistance = Math.abs( visibleAtDistance );
  3820. for ( var l = 0; l < this.LODs.length; l ++ ) {
  3821. if ( visibleAtDistance < this.LODs[ l ].visibleAtDistance ) {
  3822. break;
  3823. }
  3824. }
  3825. this.LODs.splice( l, 0, { visibleAtDistance: visibleAtDistance, object3D: object3D } );
  3826. this.add( object3D );
  3827. };
  3828. THREE.LOD.prototype.update = function ( camera ) {
  3829. if ( this.LODs.length > 1 ) {
  3830. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  3831. var inverse = camera.matrixWorldInverse;
  3832. var distance = -( inverse.elements[2] * this.matrixWorld.elements[12] + inverse.elements[6] * this.matrixWorld.elements[13] + inverse.elements[10] * this.matrixWorld.elements[14] + inverse.elements[14] );
  3833. this.LODs[ 0 ].object3D.visible = true;
  3834. for ( var l = 1; l < this.LODs.length; l ++ ) {
  3835. if( distance >= this.LODs[ l ].visibleAtDistance ) {
  3836. this.LODs[ l - 1 ].object3D.visible = false;
  3837. this.LODs[ l ].object3D.visible = true;
  3838. } else {
  3839. break;
  3840. }
  3841. }
  3842. for( ; l < this.LODs.length; l ++ ) {
  3843. this.LODs[ l ].object3D.visible = false;
  3844. }
  3845. }
  3846. };
  3847. /**
  3848. * @author mikael emtinger / http://gomo.se/
  3849. */
  3850. THREE.Sprite = function ( parameters ) {
  3851. THREE.Object3D.call( this );
  3852. this.color = ( parameters.color !== undefined ) ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3853. this.map = ( parameters.map !== undefined ) ? parameters.map : new THREE.Texture();
  3854. this.blending = ( parameters.blending !== undefined ) ? parameters.blending : THREE.NormalBlending;
  3855. this.blendSrc = parameters.blendSrc !== undefined ? parameters.blendSrc : THREE.SrcAlphaFactor;
  3856. this.blendDst = parameters.blendDst !== undefined ? parameters.blendDst : THREE.OneMinusSrcAlphaFactor;
  3857. this.blendEquation = parameters.blendEquation !== undefined ? parameters.blendEquation : THREE.AddEquation;
  3858. this.useScreenCoordinates = ( parameters.useScreenCoordinates !== undefined ) ? parameters.useScreenCoordinates : true;
  3859. this.mergeWith3D = ( parameters.mergeWith3D !== undefined ) ? parameters.mergeWith3D : !this.useScreenCoordinates;
  3860. this.affectedByDistance = ( parameters.affectedByDistance !== undefined ) ? parameters.affectedByDistance : !this.useScreenCoordinates;
  3861. this.scaleByViewport = ( parameters.scaleByViewport !== undefined ) ? parameters.scaleByViewport : !this.affectedByDistance;
  3862. this.alignment = ( parameters.alignment instanceof THREE.Vector2 ) ? parameters.alignment : THREE.SpriteAlignment.center;
  3863. this.rotation3d = this.rotation;
  3864. this.rotation = 0;
  3865. this.opacity = 1;
  3866. this.uvOffset = new THREE.Vector2( 0, 0 );
  3867. this.uvScale = new THREE.Vector2( 1, 1 );
  3868. };
  3869. THREE.Sprite.prototype = new THREE.Object3D();
  3870. THREE.Sprite.prototype.constructor = THREE.Sprite;
  3871. /*
  3872. * Custom update matrix
  3873. */
  3874. THREE.Sprite.prototype.updateMatrix = function () {
  3875. this.matrix.setPosition( this.position );
  3876. this.rotation3d.set( 0, 0, this.rotation );
  3877. this.matrix.setRotationFromEuler( this.rotation3d );
  3878. if ( this.scale.x !== 1 || this.scale.y !== 1 ) {
  3879. this.matrix.scale( this.scale );
  3880. this.boundRadiusScale = Math.max( this.scale.x, this.scale.y );
  3881. }
  3882. this.matrixWorldNeedsUpdate = true;
  3883. };
  3884. /*
  3885. * Alignment
  3886. */
  3887. THREE.SpriteAlignment = {};
  3888. THREE.SpriteAlignment.topLeft = new THREE.Vector2( 1, -1 );
  3889. THREE.SpriteAlignment.topCenter = new THREE.Vector2( 0, -1 );
  3890. THREE.SpriteAlignment.topRight = new THREE.Vector2( -1, -1 );
  3891. THREE.SpriteAlignment.centerLeft = new THREE.Vector2( 1, 0 );
  3892. THREE.SpriteAlignment.center = new THREE.Vector2( 0, 0 );
  3893. THREE.SpriteAlignment.centerRight = new THREE.Vector2( -1, 0 );
  3894. THREE.SpriteAlignment.bottomLeft = new THREE.Vector2( 1, 1 );
  3895. THREE.SpriteAlignment.bottomCenter = new THREE.Vector2( 0, 1 );
  3896. THREE.SpriteAlignment.bottomRight = new THREE.Vector2( -1, 1 );
  3897. /**
  3898. * @author mr.doob / http://mrdoob.com/
  3899. */
  3900. THREE.Scene = function () {
  3901. THREE.Object3D.call( this );
  3902. this.fog = null;
  3903. this.overrideMaterial = null;
  3904. this.matrixAutoUpdate = false;
  3905. this.__objects = [];
  3906. this.__lights = [];
  3907. this.__objectsAdded = [];
  3908. this.__objectsRemoved = [];
  3909. };
  3910. THREE.Scene.prototype = new THREE.Object3D();
  3911. THREE.Scene.prototype.constructor = THREE.Scene;
  3912. THREE.Scene.prototype.__addObject = function ( object ) {
  3913. if ( object instanceof THREE.Light ) {
  3914. if ( this.__lights.indexOf( object ) === - 1 ) {
  3915. this.__lights.push( object );
  3916. }
  3917. } else if ( !( object instanceof THREE.Camera || object instanceof THREE.Bone ) ) {
  3918. if ( this.__objects.indexOf( object ) === - 1 ) {
  3919. this.__objects.push( object );
  3920. this.__objectsAdded.push( object );
  3921. // check if previously removed
  3922. var i = this.__objectsRemoved.indexOf( object );
  3923. if ( i !== -1 ) {
  3924. this.__objectsRemoved.splice( i, 1 );
  3925. }
  3926. }
  3927. }
  3928. for ( var c = 0; c < object.children.length; c ++ ) {
  3929. this.__addObject( object.children[ c ] );
  3930. }
  3931. };
  3932. THREE.Scene.prototype.__removeObject = function ( object ) {
  3933. if ( object instanceof THREE.Light ) {
  3934. var i = this.__lights.indexOf( object );
  3935. if ( i !== -1 ) {
  3936. this.__lights.splice( i, 1 );
  3937. }
  3938. } else if ( !( object instanceof THREE.Camera ) ) {
  3939. var i = this.__objects.indexOf( object );
  3940. if( i !== -1 ) {
  3941. this.__objects.splice( i, 1 );
  3942. this.__objectsRemoved.push( object );
  3943. // check if previously added
  3944. var ai = this.__objectsAdded.indexOf( object );
  3945. if ( ai !== -1 ) {
  3946. this.__objectsAdded.splice( ai, 1 );
  3947. }
  3948. }
  3949. }
  3950. for ( var c = 0; c < object.children.length; c ++ ) {
  3951. this.__removeObject( object.children[ c ] );
  3952. }
  3953. };
  3954. /**
  3955. * @author mr.doob / http://mrdoob.com/
  3956. * @author alteredq / http://alteredqualia.com/
  3957. */
  3958. THREE.Fog = function ( hex, near, far ) {
  3959. this.color = new THREE.Color( hex );
  3960. this.near = ( near !== undefined ) ? near : 1;
  3961. this.far = ( far !== undefined ) ? far : 1000;
  3962. };
  3963. /**
  3964. * @author mr.doob / http://mrdoob.com/
  3965. * @author alteredq / http://alteredqualia.com/
  3966. */
  3967. THREE.FogExp2 = function ( hex, density ) {
  3968. this.color = new THREE.Color( hex );
  3969. this.density = ( density !== undefined ) ? density : 0.00025;
  3970. };
  3971. /**
  3972. * @author alteredq / http://alteredqualia.com/
  3973. * @author mrdoob / http://mrdoob.com/
  3974. * @author mikael emtinger / http://gomo.se/
  3975. */
  3976. THREE.ShaderChunk = {
  3977. // FOG
  3978. fog_pars_fragment: [
  3979. "#ifdef USE_FOG",
  3980. "uniform vec3 fogColor;",
  3981. "#ifdef FOG_EXP2",
  3982. "uniform float fogDensity;",
  3983. "#else",
  3984. "uniform float fogNear;",
  3985. "uniform float fogFar;",
  3986. "#endif",
  3987. "#endif"
  3988. ].join("\n"),
  3989. fog_fragment: [
  3990. "#ifdef USE_FOG",
  3991. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  3992. "#ifdef FOG_EXP2",
  3993. "const float LOG2 = 1.442695;",
  3994. "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
  3995. "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
  3996. "#else",
  3997. "float fogFactor = smoothstep( fogNear, fogFar, depth );",
  3998. "#endif",
  3999. "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
  4000. "#endif"
  4001. ].join("\n"),
  4002. // ENVIRONMENT MAP
  4003. envmap_pars_fragment: [
  4004. "#ifdef USE_ENVMAP",
  4005. "varying vec3 vReflect;",
  4006. "uniform float reflectivity;",
  4007. "uniform samplerCube envMap;",
  4008. "uniform float flipEnvMap;",
  4009. "uniform int combine;",
  4010. "#endif"
  4011. ].join("\n"),
  4012. envmap_fragment: [
  4013. "#ifdef USE_ENVMAP",
  4014. "#ifdef DOUBLE_SIDED",
  4015. "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  4016. "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  4017. "#else",
  4018. "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  4019. "#endif",
  4020. "#ifdef GAMMA_INPUT",
  4021. "cubeColor.xyz *= cubeColor.xyz;",
  4022. "#endif",
  4023. "if ( combine == 1 ) {",
  4024. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, reflectivity );",
  4025. "} else {",
  4026. "gl_FragColor.xyz = gl_FragColor.xyz * cubeColor.xyz;",
  4027. "}",
  4028. "#endif"
  4029. ].join("\n"),
  4030. envmap_pars_vertex: [
  4031. "#ifdef USE_ENVMAP",
  4032. "varying vec3 vReflect;",
  4033. "uniform float refractionRatio;",
  4034. "uniform bool useRefract;",
  4035. "#endif"
  4036. ].join("\n"),
  4037. envmap_vertex : [
  4038. "#ifdef USE_ENVMAP",
  4039. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  4040. "vec3 nWorld = mat3( objectMatrix[ 0 ].xyz, objectMatrix[ 1 ].xyz, objectMatrix[ 2 ].xyz ) * normal;",
  4041. "if ( useRefract ) {",
  4042. "vReflect = refract( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ), refractionRatio );",
  4043. "} else {",
  4044. "vReflect = reflect( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ) );",
  4045. "}",
  4046. "#endif"
  4047. ].join("\n"),
  4048. // COLOR MAP (particles)
  4049. map_particle_pars_fragment: [
  4050. "#ifdef USE_MAP",
  4051. "uniform sampler2D map;",
  4052. "#endif"
  4053. ].join("\n"),
  4054. map_particle_fragment: [
  4055. "#ifdef USE_MAP",
  4056. "gl_FragColor = gl_FragColor * texture2D( map, gl_PointCoord );",
  4057. "#endif"
  4058. ].join("\n"),
  4059. // COLOR MAP (triangles)
  4060. map_pars_vertex: [
  4061. "#ifdef USE_MAP",
  4062. "varying vec2 vUv;",
  4063. "uniform vec4 offsetRepeat;",
  4064. "#endif"
  4065. ].join("\n"),
  4066. map_pars_fragment: [
  4067. "#ifdef USE_MAP",
  4068. "varying vec2 vUv;",
  4069. "uniform sampler2D map;",
  4070. "#endif"
  4071. ].join("\n"),
  4072. map_vertex: [
  4073. "#ifdef USE_MAP",
  4074. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  4075. "#endif"
  4076. ].join("\n"),
  4077. map_fragment: [
  4078. "#ifdef USE_MAP",
  4079. "#ifdef GAMMA_INPUT",
  4080. "vec4 texelColor = texture2D( map, vUv );",
  4081. "texelColor.xyz *= texelColor.xyz;",
  4082. "gl_FragColor = gl_FragColor * texelColor;",
  4083. "#else",
  4084. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  4085. "#endif",
  4086. "#endif"
  4087. ].join("\n"),
  4088. // LIGHT MAP
  4089. lightmap_pars_fragment: [
  4090. "#ifdef USE_LIGHTMAP",
  4091. "varying vec2 vUv2;",
  4092. "uniform sampler2D lightMap;",
  4093. "#endif"
  4094. ].join("\n"),
  4095. lightmap_pars_vertex: [
  4096. "#ifdef USE_LIGHTMAP",
  4097. "varying vec2 vUv2;",
  4098. "#endif"
  4099. ].join("\n"),
  4100. lightmap_fragment: [
  4101. "#ifdef USE_LIGHTMAP",
  4102. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  4103. "#endif"
  4104. ].join("\n"),
  4105. lightmap_vertex: [
  4106. "#ifdef USE_LIGHTMAP",
  4107. "vUv2 = uv2;",
  4108. "#endif"
  4109. ].join("\n"),
  4110. // LIGHTS LAMBERT
  4111. lights_lambert_pars_vertex: [
  4112. "uniform vec3 ambient;",
  4113. "uniform vec3 diffuse;",
  4114. "uniform vec3 emissive;",
  4115. "uniform vec3 ambientLightColor;",
  4116. "#if MAX_DIR_LIGHTS > 0",
  4117. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  4118. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  4119. "#endif",
  4120. "#if MAX_POINT_LIGHTS > 0",
  4121. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  4122. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  4123. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  4124. "#endif",
  4125. "#ifdef WRAP_AROUND",
  4126. "uniform vec3 wrapRGB;",
  4127. "#endif",
  4128. ].join("\n"),
  4129. lights_lambert_vertex: [
  4130. "vLightFront = vec3( 0.0 );",
  4131. "#ifdef DOUBLE_SIDED",
  4132. "vLightBack = vec3( 0.0 );",
  4133. "#endif",
  4134. "transformedNormal = normalize( transformedNormal );",
  4135. "#if MAX_DIR_LIGHTS > 0",
  4136. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  4137. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  4138. "vec3 dirVector = normalize( lDirection.xyz );",
  4139. "float dotProduct = dot( transformedNormal, dirVector );",
  4140. "vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  4141. "#ifdef DOUBLE_SIDED",
  4142. "vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  4143. "#ifdef WRAP_AROUND",
  4144. "vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  4145. "#endif",
  4146. "#endif",
  4147. "#ifdef WRAP_AROUND",
  4148. "vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  4149. "directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );",
  4150. "#ifdef DOUBLE_SIDED",
  4151. "directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );",
  4152. "#endif",
  4153. "#endif",
  4154. "vLightFront += directionalLightColor[ i ] * directionalLightWeighting;",
  4155. "#ifdef DOUBLE_SIDED",
  4156. "vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;",
  4157. "#endif",
  4158. "}",
  4159. "#endif",
  4160. "#if MAX_POINT_LIGHTS > 0",
  4161. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  4162. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  4163. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  4164. "float lDistance = 1.0;",
  4165. "if ( pointLightDistance[ i ] > 0.0 )",
  4166. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  4167. "lVector = normalize( lVector );",
  4168. "float dotProduct = dot( transformedNormal, lVector );",
  4169. "vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  4170. "#ifdef DOUBLE_SIDED",
  4171. "vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  4172. "#ifdef WRAP_AROUND",
  4173. "vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  4174. "#endif",
  4175. "#endif",
  4176. "#ifdef WRAP_AROUND",
  4177. "vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  4178. "pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );",
  4179. "#ifdef DOUBLE_SIDED",
  4180. "pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );",
  4181. "#endif",
  4182. "#endif",
  4183. "vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  4184. "#ifdef DOUBLE_SIDED",
  4185. "vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;",
  4186. "#endif",
  4187. "}",
  4188. "#endif",
  4189. "vLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;",
  4190. "#ifdef DOUBLE_SIDED",
  4191. "vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;",
  4192. "#endif",
  4193. ].join("\n"),
  4194. // LIGHTS PHONG
  4195. lights_phong_pars_vertex: [
  4196. "#if MAX_POINT_LIGHTS > 0",
  4197. "#ifndef PHONG_PER_PIXEL",
  4198. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  4199. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  4200. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  4201. "#endif",
  4202. "#endif"
  4203. ].join("\n"),
  4204. lights_phong_vertex: [
  4205. "#if MAX_POINT_LIGHTS > 0",
  4206. "#ifndef PHONG_PER_PIXEL",
  4207. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  4208. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  4209. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  4210. "float lDistance = 1.0;",
  4211. "if ( pointLightDistance[ i ] > 0.0 )",
  4212. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  4213. "vPointLight[ i ] = vec4( lVector, lDistance );",
  4214. "}",
  4215. "#endif",
  4216. "#endif"
  4217. ].join("\n"),
  4218. lights_phong_pars_fragment: [
  4219. "uniform vec3 ambientLightColor;",
  4220. "#if MAX_DIR_LIGHTS > 0",
  4221. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  4222. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  4223. "#endif",
  4224. "#if MAX_POINT_LIGHTS > 0",
  4225. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  4226. "#ifdef PHONG_PER_PIXEL",
  4227. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  4228. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  4229. "#else",
  4230. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  4231. "#endif",
  4232. "#endif",
  4233. "#ifdef WRAP_AROUND",
  4234. "uniform vec3 wrapRGB;",
  4235. "#endif",
  4236. "varying vec3 vViewPosition;",
  4237. "varying vec3 vNormal;"
  4238. ].join("\n"),
  4239. lights_phong_fragment: [
  4240. "vec3 normal = normalize( vNormal );",
  4241. "vec3 viewPosition = normalize( vViewPosition );",
  4242. "#ifdef DOUBLE_SIDED",
  4243. "normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  4244. "#endif",
  4245. "#if MAX_POINT_LIGHTS > 0",
  4246. "vec3 pointDiffuse = vec3( 0.0 );",
  4247. "vec3 pointSpecular = vec3( 0.0 );",
  4248. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  4249. "#ifdef PHONG_PER_PIXEL",
  4250. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  4251. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  4252. "float lDistance = 1.0;",
  4253. "if ( pointLightDistance[ i ] > 0.0 )",
  4254. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  4255. "lVector = normalize( lVector );",
  4256. "#else",
  4257. "vec3 lVector = normalize( vPointLight[ i ].xyz );",
  4258. "float lDistance = vPointLight[ i ].w;",
  4259. "#endif",
  4260. // diffuse
  4261. "float dotProduct = dot( normal, lVector );",
  4262. "#ifdef WRAP_AROUND",
  4263. "float pointDiffuseWeightFull = max( dotProduct, 0.0 );",
  4264. "float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  4265. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  4266. "#else",
  4267. "float pointDiffuseWeight = max( dotProduct, 0.0 );",
  4268. "#endif",
  4269. "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
  4270. // specular
  4271. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  4272. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  4273. "float pointSpecularWeight = max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  4274. "#ifdef PHYSICALLY_BASED_SHADING",
  4275. // 2.0 => 2.0001 is hack to work around ANGLE bug
  4276. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  4277. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, pointHalfVector ), 5.0 );",
  4278. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance * specularNormalization;",
  4279. "#else",
  4280. "pointSpecular += specular * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance;",
  4281. "#endif",
  4282. "}",
  4283. "#endif",
  4284. "#if MAX_DIR_LIGHTS > 0",
  4285. "vec3 dirDiffuse = vec3( 0.0 );",
  4286. "vec3 dirSpecular = vec3( 0.0 );" ,
  4287. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  4288. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  4289. "vec3 dirVector = normalize( lDirection.xyz );",
  4290. // diffuse
  4291. "float dotProduct = dot( normal, dirVector );",
  4292. "#ifdef WRAP_AROUND",
  4293. "float dirDiffuseWeightFull = max( dotProduct, 0.0 );",
  4294. "float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  4295. "vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );",
  4296. "#else",
  4297. "float dirDiffuseWeight = max( dotProduct, 0.0 );",
  4298. "#endif",
  4299. "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
  4300. // specular
  4301. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  4302. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  4303. "float dirSpecularWeight = max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  4304. "#ifdef PHYSICALLY_BASED_SHADING",
  4305. /*
  4306. // fresnel term from skin shader
  4307. "const float F0 = 0.128;",
  4308. "float base = 1.0 - dot( viewPosition, dirHalfVector );",
  4309. "float exponential = pow( base, 5.0 );",
  4310. "float fresnel = exponential + F0 * ( 1.0 - exponential );",
  4311. */
  4312. /*
  4313. // fresnel term from fresnel shader
  4314. "const float mFresnelBias = 0.08;",
  4315. "const float mFresnelScale = 0.3;",
  4316. "const float mFresnelPower = 5.0;",
  4317. "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
  4318. */
  4319. // 2.0 => 2.0001 is hack to work around ANGLE bug
  4320. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  4321. //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
  4322. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  4323. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  4324. "#else",
  4325. "dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  4326. "#endif",
  4327. "}",
  4328. "#endif",
  4329. "vec3 totalDiffuse = vec3( 0.0 );",
  4330. "vec3 totalSpecular = vec3( 0.0 );",
  4331. "#if MAX_DIR_LIGHTS > 0",
  4332. "totalDiffuse += dirDiffuse;",
  4333. "totalSpecular += dirSpecular;",
  4334. "#endif",
  4335. "#if MAX_POINT_LIGHTS > 0",
  4336. "totalDiffuse += pointDiffuse;",
  4337. "totalSpecular += pointSpecular;",
  4338. "#endif",
  4339. "#ifdef METAL",
  4340. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  4341. "#else",
  4342. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  4343. "#endif"
  4344. ].join("\n"),
  4345. // VERTEX COLORS
  4346. color_pars_fragment: [
  4347. "#ifdef USE_COLOR",
  4348. "varying vec3 vColor;",
  4349. "#endif"
  4350. ].join("\n"),
  4351. color_fragment: [
  4352. "#ifdef USE_COLOR",
  4353. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  4354. "#endif"
  4355. ].join("\n"),
  4356. color_pars_vertex: [
  4357. "#ifdef USE_COLOR",
  4358. "varying vec3 vColor;",
  4359. "#endif"
  4360. ].join("\n"),
  4361. color_vertex: [
  4362. "#ifdef USE_COLOR",
  4363. "#ifdef GAMMA_INPUT",
  4364. "vColor = color * color;",
  4365. "#else",
  4366. "vColor = color;",
  4367. "#endif",
  4368. "#endif"
  4369. ].join("\n"),
  4370. // SKINNING
  4371. skinning_pars_vertex: [
  4372. "#ifdef USE_SKINNING",
  4373. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  4374. "#endif"
  4375. ].join("\n"),
  4376. skinning_vertex: [
  4377. "#ifdef USE_SKINNING",
  4378. "gl_Position = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
  4379. "gl_Position += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
  4380. "gl_Position = projectionMatrix * modelViewMatrix * gl_Position;",
  4381. "#endif"
  4382. ].join("\n"),
  4383. // MORPHING
  4384. morphtarget_pars_vertex: [
  4385. "#ifdef USE_MORPHTARGETS",
  4386. "#ifndef USE_MORPHNORMALS",
  4387. "uniform float morphTargetInfluences[ 8 ];",
  4388. "#else",
  4389. "uniform float morphTargetInfluences[ 4 ];",
  4390. "#endif",
  4391. "#endif"
  4392. ].join("\n"),
  4393. morphtarget_vertex: [
  4394. "#ifdef USE_MORPHTARGETS",
  4395. "vec3 morphed = vec3( 0.0 );",
  4396. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  4397. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  4398. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  4399. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  4400. "#ifndef USE_MORPHNORMALS",
  4401. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  4402. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  4403. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  4404. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  4405. "#endif",
  4406. "morphed += position;",
  4407. "gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );",
  4408. "#endif"
  4409. ].join("\n"),
  4410. default_vertex : [
  4411. "#ifndef USE_MORPHTARGETS",
  4412. "#ifndef USE_SKINNING",
  4413. "gl_Position = projectionMatrix * mvPosition;",
  4414. "#endif",
  4415. "#endif"
  4416. ].join("\n"),
  4417. morphnormal_vertex: [
  4418. "#ifdef USE_MORPHNORMALS",
  4419. "vec3 morphedNormal = vec3( 0.0 );",
  4420. "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
  4421. "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
  4422. "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
  4423. "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
  4424. "morphedNormal += normal;",
  4425. "vec3 transformedNormal = normalMatrix * morphedNormal;",
  4426. "#else",
  4427. "vec3 transformedNormal = normalMatrix * normal;",
  4428. "#endif"
  4429. ].join("\n"),
  4430. // SHADOW MAP
  4431. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  4432. // http://spidergl.org/example.php?id=6
  4433. // http://fabiensanglard.net/shadowmapping
  4434. shadowmap_pars_fragment: [
  4435. "#ifdef USE_SHADOWMAP",
  4436. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  4437. "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
  4438. "uniform float shadowDarkness[ MAX_SHADOWS ];",
  4439. "uniform float shadowBias[ MAX_SHADOWS ];",
  4440. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  4441. "float unpackDepth( const in vec4 rgba_depth ) {",
  4442. "const vec4 bit_shift = vec4( 1.0 / ( 256.0 * 256.0 * 256.0 ), 1.0 / ( 256.0 * 256.0 ), 1.0 / 256.0, 1.0 );",
  4443. "float depth = dot( rgba_depth, bit_shift );",
  4444. "return depth;",
  4445. "}",
  4446. "#endif"
  4447. ].join("\n"),
  4448. shadowmap_fragment: [
  4449. "#ifdef USE_SHADOWMAP",
  4450. "#ifdef SHADOWMAP_DEBUG",
  4451. "vec3 frustumColors[3];",
  4452. "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
  4453. "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
  4454. "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
  4455. "#endif",
  4456. "#ifdef SHADOWMAP_CASCADE",
  4457. "int inFrustumCount = 0;",
  4458. "#endif",
  4459. "float fDepth;",
  4460. "vec3 shadowColor = vec3( 1.0 );",
  4461. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  4462. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  4463. // "if ( something && something )" breaks ATI OpenGL shader compiler
  4464. // "if ( all( something, something ) )" using this instead
  4465. "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
  4466. "bool inFrustum = all( inFrustumVec );",
  4467. // don't shadow pixels outside of light frustum
  4468. // use just first frustum (for cascades)
  4469. // don't shadow pixels behind far plane of light frustum
  4470. "#ifdef SHADOWMAP_CASCADE",
  4471. "inFrustumCount += int( inFrustum );",
  4472. "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
  4473. "#else",
  4474. "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
  4475. "#endif",
  4476. "bool frustumTest = all( frustumTestVec );",
  4477. "if ( frustumTest ) {",
  4478. "shadowCoord.z += shadowBias[ i ];",
  4479. "#ifdef SHADOWMAP_SOFT",
  4480. // Percentage-close filtering
  4481. // (9 pixel kernel)
  4482. // http://fabiensanglard.net/shadowmappingPCF/
  4483. "float shadow = 0.0;",
  4484. /*
  4485. // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
  4486. // must enroll loop manually
  4487. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  4488. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  4489. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  4490. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  4491. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  4492. "float fDepth = unpackDepth( rgbaDepth );",
  4493. "if ( fDepth < shadowCoord.z )",
  4494. "shadow += 1.0;",
  4495. "}",
  4496. "shadow /= 9.0;",
  4497. */
  4498. "const float shadowDelta = 1.0 / 9.0;",
  4499. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  4500. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  4501. "float dx0 = -1.25 * xPixelOffset;",
  4502. "float dy0 = -1.25 * yPixelOffset;",
  4503. "float dx1 = 1.25 * xPixelOffset;",
  4504. "float dy1 = 1.25 * yPixelOffset;",
  4505. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  4506. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4507. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  4508. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4509. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  4510. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4511. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  4512. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4513. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  4514. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4515. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  4516. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4517. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  4518. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4519. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  4520. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4521. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  4522. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4523. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  4524. "#else",
  4525. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  4526. "float fDepth = unpackDepth( rgbaDepth );",
  4527. "if ( fDepth < shadowCoord.z )",
  4528. // spot with multiple shadows is darker
  4529. "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
  4530. // spot with multiple shadows has the same color as single shadow spot
  4531. //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
  4532. "#endif",
  4533. "}",
  4534. "#ifdef SHADOWMAP_DEBUG",
  4535. "#ifdef SHADOWMAP_CASCADE",
  4536. "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
  4537. "#else",
  4538. "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
  4539. "#endif",
  4540. "#endif",
  4541. "}",
  4542. "#ifdef GAMMA_OUTPUT",
  4543. "shadowColor *= shadowColor;",
  4544. "#endif",
  4545. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  4546. "#endif"
  4547. ].join("\n"),
  4548. shadowmap_pars_vertex: [
  4549. "#ifdef USE_SHADOWMAP",
  4550. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  4551. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  4552. "#endif"
  4553. ].join("\n"),
  4554. shadowmap_vertex: [
  4555. "#ifdef USE_SHADOWMAP",
  4556. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  4557. "#ifdef USE_MORPHTARGETS",
  4558. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( morphed, 1.0 );",
  4559. "#else",
  4560. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( position, 1.0 );",
  4561. "#endif",
  4562. "}",
  4563. "#endif"
  4564. ].join("\n"),
  4565. // ALPHATEST
  4566. alphatest_fragment: [
  4567. "#ifdef ALPHATEST",
  4568. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  4569. "#endif"
  4570. ].join("\n"),
  4571. // LINEAR SPACE
  4572. linear_to_gamma_fragment: [
  4573. "#ifdef GAMMA_OUTPUT",
  4574. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  4575. "#endif"
  4576. ].join("\n"),
  4577. };
  4578. THREE.UniformsUtils = {
  4579. merge: function ( uniforms ) {
  4580. var u, p, tmp, merged = {};
  4581. for ( u = 0; u < uniforms.length; u++ ) {
  4582. tmp = this.clone( uniforms[ u ] );
  4583. for ( p in tmp ) {
  4584. merged[ p ] = tmp[ p ];
  4585. }
  4586. }
  4587. return merged;
  4588. },
  4589. clone: function ( uniforms_src ) {
  4590. var u, p, parameter, parameter_src, uniforms_dst = {};
  4591. for ( u in uniforms_src ) {
  4592. uniforms_dst[ u ] = {};
  4593. for ( p in uniforms_src[ u ] ) {
  4594. parameter_src = uniforms_src[ u ][ p ];
  4595. if ( parameter_src instanceof THREE.Color ||
  4596. parameter_src instanceof THREE.Vector2 ||
  4597. parameter_src instanceof THREE.Vector3 ||
  4598. parameter_src instanceof THREE.Vector4 ||
  4599. parameter_src instanceof THREE.Matrix4 ||
  4600. parameter_src instanceof THREE.Texture ) {
  4601. uniforms_dst[ u ][ p ] = parameter_src.clone();
  4602. } else if ( parameter_src instanceof Array ) {
  4603. uniforms_dst[ u ][ p ] = parameter_src.slice();
  4604. } else {
  4605. uniforms_dst[ u ][ p ] = parameter_src;
  4606. }
  4607. }
  4608. }
  4609. return uniforms_dst;
  4610. }
  4611. };
  4612. THREE.UniformsLib = {
  4613. common: {
  4614. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  4615. "opacity" : { type: "f", value: 1.0 },
  4616. "map" : { type: "t", value: 0, texture: null },
  4617. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  4618. "lightMap" : { type: "t", value: 2, texture: null },
  4619. "envMap" : { type: "t", value: 1, texture: null },
  4620. "flipEnvMap" : { type: "f", value: -1 },
  4621. "useRefract" : { type: "i", value: 0 },
  4622. "reflectivity" : { type: "f", value: 1.0 },
  4623. "refractionRatio" : { type: "f", value: 0.98 },
  4624. "combine" : { type: "i", value: 0 },
  4625. "morphTargetInfluences" : { type: "f", value: 0 }
  4626. },
  4627. fog : {
  4628. "fogDensity" : { type: "f", value: 0.00025 },
  4629. "fogNear" : { type: "f", value: 1 },
  4630. "fogFar" : { type: "f", value: 2000 },
  4631. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  4632. },
  4633. lights: {
  4634. "ambientLightColor" : { type: "fv", value: [] },
  4635. "directionalLightDirection" : { type: "fv", value: [] },
  4636. "directionalLightColor" : { type: "fv", value: [] },
  4637. "pointLightColor" : { type: "fv", value: [] },
  4638. "pointLightPosition" : { type: "fv", value: [] },
  4639. "pointLightDistance" : { type: "fv1", value: [] }
  4640. },
  4641. particle: {
  4642. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  4643. "opacity" : { type: "f", value: 1.0 },
  4644. "size" : { type: "f", value: 1.0 },
  4645. "scale" : { type: "f", value: 1.0 },
  4646. "map" : { type: "t", value: 0, texture: null },
  4647. "fogDensity" : { type: "f", value: 0.00025 },
  4648. "fogNear" : { type: "f", value: 1 },
  4649. "fogFar" : { type: "f", value: 2000 },
  4650. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  4651. },
  4652. shadowmap: {
  4653. "shadowMap": { type: "tv", value: 6, texture: [] },
  4654. "shadowMapSize": { type: "v2v", value: [] },
  4655. "shadowBias" : { type: "fv1", value: [] },
  4656. "shadowDarkness": { type: "fv1", value: [] },
  4657. "shadowMatrix" : { type: "m4v", value: [] },
  4658. }
  4659. };
  4660. THREE.ShaderLib = {
  4661. 'depth': {
  4662. uniforms: {
  4663. "mNear": { type: "f", value: 1.0 },
  4664. "mFar" : { type: "f", value: 2000.0 },
  4665. "opacity" : { type: "f", value: 1.0 }
  4666. },
  4667. vertexShader: [
  4668. "void main() {",
  4669. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  4670. "}"
  4671. ].join("\n"),
  4672. fragmentShader: [
  4673. "uniform float mNear;",
  4674. "uniform float mFar;",
  4675. "uniform float opacity;",
  4676. "void main() {",
  4677. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  4678. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  4679. "gl_FragColor = vec4( vec3( color ), opacity );",
  4680. "}"
  4681. ].join("\n")
  4682. },
  4683. 'normal': {
  4684. uniforms: {
  4685. "opacity" : { type: "f", value: 1.0 }
  4686. },
  4687. vertexShader: [
  4688. "varying vec3 vNormal;",
  4689. "void main() {",
  4690. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4691. "vNormal = normalMatrix * normal;",
  4692. "gl_Position = projectionMatrix * mvPosition;",
  4693. "}"
  4694. ].join("\n"),
  4695. fragmentShader: [
  4696. "uniform float opacity;",
  4697. "varying vec3 vNormal;",
  4698. "void main() {",
  4699. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  4700. "}"
  4701. ].join("\n")
  4702. },
  4703. 'basic': {
  4704. uniforms: THREE.UniformsUtils.merge( [
  4705. THREE.UniformsLib[ "common" ],
  4706. THREE.UniformsLib[ "fog" ],
  4707. THREE.UniformsLib[ "shadowmap" ]
  4708. ] ),
  4709. vertexShader: [
  4710. THREE.ShaderChunk[ "map_pars_vertex" ],
  4711. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  4712. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  4713. THREE.ShaderChunk[ "color_pars_vertex" ],
  4714. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  4715. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4716. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4717. "void main() {",
  4718. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4719. THREE.ShaderChunk[ "map_vertex" ],
  4720. THREE.ShaderChunk[ "lightmap_vertex" ],
  4721. THREE.ShaderChunk[ "envmap_vertex" ],
  4722. THREE.ShaderChunk[ "color_vertex" ],
  4723. THREE.ShaderChunk[ "skinning_vertex" ],
  4724. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4725. THREE.ShaderChunk[ "default_vertex" ],
  4726. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4727. "}"
  4728. ].join("\n"),
  4729. fragmentShader: [
  4730. "uniform vec3 diffuse;",
  4731. "uniform float opacity;",
  4732. THREE.ShaderChunk[ "color_pars_fragment" ],
  4733. THREE.ShaderChunk[ "map_pars_fragment" ],
  4734. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  4735. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  4736. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4737. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4738. "void main() {",
  4739. "gl_FragColor = vec4( diffuse, opacity );",
  4740. THREE.ShaderChunk[ "map_fragment" ],
  4741. THREE.ShaderChunk[ "alphatest_fragment" ],
  4742. THREE.ShaderChunk[ "lightmap_fragment" ],
  4743. THREE.ShaderChunk[ "color_fragment" ],
  4744. THREE.ShaderChunk[ "envmap_fragment" ],
  4745. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4746. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  4747. THREE.ShaderChunk[ "fog_fragment" ],
  4748. "}"
  4749. ].join("\n")
  4750. },
  4751. 'lambert': {
  4752. uniforms: THREE.UniformsUtils.merge( [
  4753. THREE.UniformsLib[ "common" ],
  4754. THREE.UniformsLib[ "fog" ],
  4755. THREE.UniformsLib[ "lights" ],
  4756. THREE.UniformsLib[ "shadowmap" ],
  4757. {
  4758. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  4759. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  4760. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  4761. }
  4762. ] ),
  4763. vertexShader: [
  4764. "varying vec3 vLightFront;",
  4765. "#ifdef DOUBLE_SIDED",
  4766. "varying vec3 vLightBack;",
  4767. "#endif",
  4768. THREE.ShaderChunk[ "map_pars_vertex" ],
  4769. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  4770. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  4771. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  4772. THREE.ShaderChunk[ "color_pars_vertex" ],
  4773. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  4774. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4775. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4776. "void main() {",
  4777. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4778. THREE.ShaderChunk[ "map_vertex" ],
  4779. THREE.ShaderChunk[ "lightmap_vertex" ],
  4780. THREE.ShaderChunk[ "envmap_vertex" ],
  4781. THREE.ShaderChunk[ "color_vertex" ],
  4782. THREE.ShaderChunk[ "morphnormal_vertex" ],
  4783. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  4784. THREE.ShaderChunk[ "skinning_vertex" ],
  4785. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4786. THREE.ShaderChunk[ "default_vertex" ],
  4787. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4788. "}"
  4789. ].join("\n"),
  4790. fragmentShader: [
  4791. "uniform float opacity;",
  4792. "varying vec3 vLightFront;",
  4793. "#ifdef DOUBLE_SIDED",
  4794. "varying vec3 vLightBack;",
  4795. "#endif",
  4796. THREE.ShaderChunk[ "color_pars_fragment" ],
  4797. THREE.ShaderChunk[ "map_pars_fragment" ],
  4798. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  4799. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  4800. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4801. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4802. "void main() {",
  4803. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  4804. THREE.ShaderChunk[ "map_fragment" ],
  4805. THREE.ShaderChunk[ "alphatest_fragment" ],
  4806. "#ifdef DOUBLE_SIDED",
  4807. //"float isFront = float( gl_FrontFacing );",
  4808. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  4809. "if ( gl_FrontFacing )",
  4810. "gl_FragColor.xyz *= vLightFront;",
  4811. "else",
  4812. "gl_FragColor.xyz *= vLightBack;",
  4813. "#else",
  4814. "gl_FragColor.xyz *= vLightFront;",
  4815. "#endif",
  4816. THREE.ShaderChunk[ "lightmap_fragment" ],
  4817. THREE.ShaderChunk[ "color_fragment" ],
  4818. THREE.ShaderChunk[ "envmap_fragment" ],
  4819. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4820. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  4821. THREE.ShaderChunk[ "fog_fragment" ],
  4822. "}"
  4823. ].join("\n")
  4824. },
  4825. 'phong': {
  4826. uniforms: THREE.UniformsUtils.merge( [
  4827. THREE.UniformsLib[ "common" ],
  4828. THREE.UniformsLib[ "fog" ],
  4829. THREE.UniformsLib[ "lights" ],
  4830. THREE.UniformsLib[ "shadowmap" ],
  4831. {
  4832. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  4833. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  4834. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  4835. "shininess": { type: "f", value: 30 },
  4836. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  4837. }
  4838. ] ),
  4839. vertexShader: [
  4840. "varying vec3 vViewPosition;",
  4841. "varying vec3 vNormal;",
  4842. THREE.ShaderChunk[ "map_pars_vertex" ],
  4843. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  4844. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  4845. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  4846. THREE.ShaderChunk[ "color_pars_vertex" ],
  4847. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  4848. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4849. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4850. "void main() {",
  4851. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4852. THREE.ShaderChunk[ "map_vertex" ],
  4853. THREE.ShaderChunk[ "lightmap_vertex" ],
  4854. THREE.ShaderChunk[ "envmap_vertex" ],
  4855. THREE.ShaderChunk[ "color_vertex" ],
  4856. "#ifndef USE_ENVMAP",
  4857. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  4858. "#endif",
  4859. "vViewPosition = -mvPosition.xyz;",
  4860. THREE.ShaderChunk[ "morphnormal_vertex" ],
  4861. "vNormal = transformedNormal;",
  4862. THREE.ShaderChunk[ "lights_phong_vertex" ],
  4863. THREE.ShaderChunk[ "skinning_vertex" ],
  4864. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4865. THREE.ShaderChunk[ "default_vertex" ],
  4866. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4867. "}"
  4868. ].join("\n"),
  4869. fragmentShader: [
  4870. "uniform vec3 diffuse;",
  4871. "uniform float opacity;",
  4872. "uniform vec3 ambient;",
  4873. "uniform vec3 emissive;",
  4874. "uniform vec3 specular;",
  4875. "uniform float shininess;",
  4876. THREE.ShaderChunk[ "color_pars_fragment" ],
  4877. THREE.ShaderChunk[ "map_pars_fragment" ],
  4878. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  4879. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  4880. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4881. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  4882. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4883. "void main() {",
  4884. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  4885. THREE.ShaderChunk[ "map_fragment" ],
  4886. THREE.ShaderChunk[ "alphatest_fragment" ],
  4887. THREE.ShaderChunk[ "lights_phong_fragment" ],
  4888. THREE.ShaderChunk[ "lightmap_fragment" ],
  4889. THREE.ShaderChunk[ "color_fragment" ],
  4890. THREE.ShaderChunk[ "envmap_fragment" ],
  4891. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4892. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  4893. THREE.ShaderChunk[ "fog_fragment" ],
  4894. "}"
  4895. ].join("\n")
  4896. },
  4897. 'particle_basic': {
  4898. uniforms: THREE.UniformsUtils.merge( [
  4899. THREE.UniformsLib[ "particle" ],
  4900. THREE.UniformsLib[ "shadowmap" ]
  4901. ] ),
  4902. vertexShader: [
  4903. "uniform float size;",
  4904. "uniform float scale;",
  4905. THREE.ShaderChunk[ "color_pars_vertex" ],
  4906. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4907. "void main() {",
  4908. THREE.ShaderChunk[ "color_vertex" ],
  4909. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4910. "#ifdef USE_SIZEATTENUATION",
  4911. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  4912. "#else",
  4913. "gl_PointSize = size;",
  4914. "#endif",
  4915. "gl_Position = projectionMatrix * mvPosition;",
  4916. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4917. "}"
  4918. ].join("\n"),
  4919. fragmentShader: [
  4920. "uniform vec3 psColor;",
  4921. "uniform float opacity;",
  4922. THREE.ShaderChunk[ "color_pars_fragment" ],
  4923. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  4924. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4925. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4926. "void main() {",
  4927. "gl_FragColor = vec4( psColor, opacity );",
  4928. THREE.ShaderChunk[ "map_particle_fragment" ],
  4929. THREE.ShaderChunk[ "alphatest_fragment" ],
  4930. THREE.ShaderChunk[ "color_fragment" ],
  4931. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4932. THREE.ShaderChunk[ "fog_fragment" ],
  4933. "}"
  4934. ].join("\n")
  4935. },
  4936. // Depth encoding into RGBA texture
  4937. // based on SpiderGL shadow map example
  4938. // http://spidergl.org/example.php?id=6
  4939. // originally from
  4940. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  4941. // see also here:
  4942. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  4943. 'depthRGBA': {
  4944. uniforms: {},
  4945. vertexShader: [
  4946. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4947. "void main() {",
  4948. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4949. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4950. THREE.ShaderChunk[ "default_vertex" ],
  4951. "}"
  4952. ].join("\n"),
  4953. fragmentShader: [
  4954. "vec4 pack_depth( const in float depth ) {",
  4955. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  4956. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  4957. "vec4 res = fract( depth * bit_shift );",
  4958. "res -= res.xxyz * bit_mask;",
  4959. "return res;",
  4960. "}",
  4961. "void main() {",
  4962. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  4963. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  4964. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  4965. //"gl_FragData[ 0 ] = pack_depth( z );",
  4966. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  4967. "}"
  4968. ].join("\n")
  4969. }
  4970. };/**
  4971. * @author supereggbert / http://www.paulbrunt.co.uk/
  4972. * @author mrdoob / http://mrdoob.com/
  4973. * @author alteredq / http://alteredqualia.com/
  4974. * @author szimek / https://github.com/szimek/
  4975. */
  4976. THREE.WebGLRenderer = function ( parameters ) {
  4977. // constructor parameters
  4978. parameters = parameters || {};
  4979. var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
  4980. _precision = parameters.precision !== undefined ? parameters.precision : 'highp',
  4981. _alpha = parameters.alpha !== undefined ? parameters.alpha : true,
  4982. _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
  4983. _antialias = parameters.antialias !== undefined ? parameters.antialias : false,
  4984. _stencil = parameters.stencil !== undefined ? parameters.stencil : true,
  4985. _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
  4986. _clearColor = parameters.clearColor !== undefined ? new THREE.Color( parameters.clearColor ) : new THREE.Color( 0x000000 ),
  4987. _clearAlpha = parameters.clearAlpha !== undefined ? parameters.clearAlpha : 0,
  4988. _maxLights = parameters.maxLights !== undefined ? parameters.maxLights : 4;
  4989. // public properties
  4990. this.domElement = _canvas;
  4991. this.context = null;
  4992. // clearing
  4993. this.autoClear = true;
  4994. this.autoClearColor = true;
  4995. this.autoClearDepth = true;
  4996. this.autoClearStencil = true;
  4997. // scene graph
  4998. this.sortObjects = true;
  4999. this.autoUpdateObjects = true;
  5000. this.autoUpdateScene = true;
  5001. // physically based shading
  5002. this.gammaInput = false;
  5003. this.gammaOutput = false;
  5004. this.physicallyBasedShading = false;
  5005. // shadow map
  5006. this.shadowMapEnabled = false;
  5007. this.shadowMapAutoUpdate = true;
  5008. this.shadowMapSoft = true;
  5009. this.shadowMapCullFrontFaces = true;
  5010. this.shadowMapDebug = false;
  5011. this.shadowMapCascade = false;
  5012. // morphs
  5013. this.maxMorphTargets = 8;
  5014. this.maxMorphNormals = 4;
  5015. // flags
  5016. this.autoScaleCubemaps = true;
  5017. // custom render plugins
  5018. this.renderPluginsPre = [];
  5019. this.renderPluginsPost = [];
  5020. // info
  5021. this.info = {
  5022. memory: {
  5023. programs: 0,
  5024. geometries: 0,
  5025. textures: 0
  5026. },
  5027. render: {
  5028. calls: 0,
  5029. vertices: 0,
  5030. faces: 0,
  5031. points: 0
  5032. }
  5033. };
  5034. // internal properties
  5035. var _this = this,
  5036. _gl,
  5037. _programs = [],
  5038. // internal state cache
  5039. _currentProgram = null,
  5040. _currentFramebuffer = null,
  5041. _currentMaterialId = -1,
  5042. _currentGeometryGroupHash = null,
  5043. _currentCamera = null,
  5044. _geometryGroupCounter = 0,
  5045. // GL state cache
  5046. _oldDoubleSided = null,
  5047. _oldFlipSided = null,
  5048. _oldBlending = null,
  5049. _oldBlendEquation = null,
  5050. _oldBlendSrc = null,
  5051. _oldBlendDst = null,
  5052. _oldDepthTest = null,
  5053. _oldDepthWrite = null,
  5054. _oldPolygonOffset = null,
  5055. _oldPolygonOffsetFactor = null,
  5056. _oldPolygonOffsetUnits = null,
  5057. _oldLineWidth = null,
  5058. _viewportX = 0,
  5059. _viewportY = 0,
  5060. _viewportWidth = 0,
  5061. _viewportHeight = 0,
  5062. _currentWidth = 0,
  5063. _currentHeight = 0,
  5064. // frustum
  5065. _frustum = new THREE.Frustum(),
  5066. // camera matrices cache
  5067. _projScreenMatrix = new THREE.Matrix4(),
  5068. _projScreenMatrixPS = new THREE.Matrix4(),
  5069. _vector3 = new THREE.Vector4(),
  5070. // light arrays cache
  5071. _direction = new THREE.Vector3(),
  5072. _lights = {
  5073. ambient: [ 0, 0, 0 ],
  5074. directional: { length: 0, colors: new Array(), positions: new Array() },
  5075. point: { length: 0, colors: new Array(), positions: new Array(), distances: new Array() }
  5076. };
  5077. // initialize
  5078. _gl = initGL();
  5079. setDefaultGLState();
  5080. this.context = _gl;
  5081. // GPU capabilities
  5082. var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ),
  5083. _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE ),
  5084. _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE );
  5085. // API
  5086. this.getContext = function () {
  5087. return _gl;
  5088. };
  5089. this.supportsVertexTextures = function () {
  5090. return _maxVertexTextures > 0;
  5091. };
  5092. this.setSize = function ( width, height ) {
  5093. _canvas.width = width;
  5094. _canvas.height = height;
  5095. this.setViewport( 0, 0, _canvas.width, _canvas.height );
  5096. };
  5097. this.setViewport = function ( x, y, width, height ) {
  5098. _viewportX = x;
  5099. _viewportY = y;
  5100. _viewportWidth = width;
  5101. _viewportHeight = height;
  5102. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  5103. };
  5104. this.setScissor = function ( x, y, width, height ) {
  5105. _gl.scissor( x, y, width, height );
  5106. };
  5107. this.enableScissorTest = function ( enable ) {
  5108. enable ? _gl.enable( _gl.SCISSOR_TEST ) : _gl.disable( _gl.SCISSOR_TEST );
  5109. };
  5110. // Clearing
  5111. this.setClearColorHex = function ( hex, alpha ) {
  5112. _clearColor.setHex( hex );
  5113. _clearAlpha = alpha;
  5114. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  5115. };
  5116. this.setClearColor = function ( color, alpha ) {
  5117. _clearColor.copy( color );
  5118. _clearAlpha = alpha;
  5119. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  5120. };
  5121. this.getClearColor = function () {
  5122. return _clearColor;
  5123. };
  5124. this.getClearAlpha = function () {
  5125. return _clearAlpha;
  5126. };
  5127. this.clear = function ( color, depth, stencil ) {
  5128. var bits = 0;
  5129. if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
  5130. if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
  5131. if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;
  5132. _gl.clear( bits );
  5133. };
  5134. this.clearTarget = function ( renderTarget, color, depth, stencil ) {
  5135. this.setRenderTarget( renderTarget );
  5136. this.clear( color, depth, stencil );
  5137. };
  5138. // Plugins
  5139. this.addPostPlugin = function ( plugin ) {
  5140. plugin.init( this );
  5141. this.renderPluginsPost.push( plugin );
  5142. };
  5143. this.addPrePlugin = function ( plugin ) {
  5144. plugin.init( this );
  5145. this.renderPluginsPre.push( plugin );
  5146. };
  5147. // Deallocation
  5148. this.deallocateObject = function ( object ) {
  5149. if ( ! object.__webglInit ) return;
  5150. object.__webglInit = false;
  5151. delete object._modelViewMatrix;
  5152. delete object._normalMatrix;
  5153. delete object._normalMatrixArray;
  5154. delete object._modelViewMatrixArray;
  5155. delete object._objectMatrixArray;
  5156. if ( object instanceof THREE.Mesh ) {
  5157. for ( var g in object.geometry.geometryGroups ) {
  5158. deleteMeshBuffers( object.geometry.geometryGroups[ g ] );
  5159. }
  5160. } else if ( object instanceof THREE.Ribbon ) {
  5161. deleteRibbonBuffers( object.geometry );
  5162. } else if ( object instanceof THREE.Line ) {
  5163. deleteLineBuffers( object.geometry );
  5164. } else if ( object instanceof THREE.ParticleSystem ) {
  5165. deleteParticleBuffers( object.geometry );
  5166. }
  5167. };
  5168. this.deallocateTexture = function ( texture ) {
  5169. if ( ! texture.__webglInit ) return;
  5170. texture.__webglInit = false;
  5171. _gl.deleteTexture( texture.__webglTexture );
  5172. _this.info.memory.textures --;
  5173. };
  5174. this.deallocateRenderTarget = function ( renderTarget ) {
  5175. if ( !renderTarget || ! renderTarget.__webglTexture ) return;
  5176. _gl.deleteTexture( renderTarget.__webglTexture );
  5177. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  5178. for ( var i = 0; i < 6; i ++ ) {
  5179. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] );
  5180. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] );
  5181. }
  5182. } else {
  5183. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer );
  5184. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer );
  5185. }
  5186. };
  5187. // Rendering
  5188. this.updateShadowMap = function ( scene, camera ) {
  5189. _currentProgram = null;
  5190. _oldBlending = -1;
  5191. _oldDepthTest = -1;
  5192. _oldDepthWrite = -1;
  5193. _currentGeometryGroupHash = -1;
  5194. _currentMaterialId = -1;
  5195. this.shadowMapPlugin.update( scene, camera );
  5196. };
  5197. // Internal functions
  5198. // Buffer allocation
  5199. function createParticleBuffers ( geometry ) {
  5200. geometry.__webglVertexBuffer = _gl.createBuffer();
  5201. geometry.__webglColorBuffer = _gl.createBuffer();
  5202. _this.info.geometries ++;
  5203. };
  5204. function createLineBuffers ( geometry ) {
  5205. geometry.__webglVertexBuffer = _gl.createBuffer();
  5206. geometry.__webglColorBuffer = _gl.createBuffer();
  5207. _this.info.memory.geometries ++;
  5208. };
  5209. function createRibbonBuffers ( geometry ) {
  5210. geometry.__webglVertexBuffer = _gl.createBuffer();
  5211. geometry.__webglColorBuffer = _gl.createBuffer();
  5212. _this.info.memory.geometries ++;
  5213. };
  5214. function createMeshBuffers ( geometryGroup ) {
  5215. geometryGroup.__webglVertexBuffer = _gl.createBuffer();
  5216. geometryGroup.__webglNormalBuffer = _gl.createBuffer();
  5217. geometryGroup.__webglTangentBuffer = _gl.createBuffer();
  5218. geometryGroup.__webglColorBuffer = _gl.createBuffer();
  5219. geometryGroup.__webglUVBuffer = _gl.createBuffer();
  5220. geometryGroup.__webglUV2Buffer = _gl.createBuffer();
  5221. geometryGroup.__webglSkinVertexABuffer = _gl.createBuffer();
  5222. geometryGroup.__webglSkinVertexBBuffer = _gl.createBuffer();
  5223. geometryGroup.__webglSkinIndicesBuffer = _gl.createBuffer();
  5224. geometryGroup.__webglSkinWeightsBuffer = _gl.createBuffer();
  5225. geometryGroup.__webglFaceBuffer = _gl.createBuffer();
  5226. geometryGroup.__webglLineBuffer = _gl.createBuffer();
  5227. var m, ml;
  5228. if ( geometryGroup.numMorphTargets ) {
  5229. geometryGroup.__webglMorphTargetsBuffers = [];
  5230. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  5231. geometryGroup.__webglMorphTargetsBuffers.push( _gl.createBuffer() );
  5232. }
  5233. }
  5234. if ( geometryGroup.numMorphNormals ) {
  5235. geometryGroup.__webglMorphNormalsBuffers = [];
  5236. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  5237. geometryGroup.__webglMorphNormalsBuffers.push( _gl.createBuffer() );
  5238. }
  5239. }
  5240. _this.info.memory.geometries ++;
  5241. };
  5242. // Buffer deallocation
  5243. function deleteParticleBuffers ( geometry ) {
  5244. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  5245. _gl.deleteBuffer( geometry.__webglColorBuffer );
  5246. _this.info.memory.geometries --;
  5247. };
  5248. function deleteLineBuffers ( geometry ) {
  5249. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  5250. _gl.deleteBuffer( geometry.__webglColorBuffer );
  5251. _this.info.memory.geometries --;
  5252. };
  5253. function deleteRibbonBuffers ( geometry ) {
  5254. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  5255. _gl.deleteBuffer( geometry.__webglColorBuffer );
  5256. _this.info.memory.geometries --;
  5257. };
  5258. function deleteMeshBuffers ( geometryGroup ) {
  5259. _gl.deleteBuffer( geometryGroup.__webglVertexBuffer );
  5260. _gl.deleteBuffer( geometryGroup.__webglNormalBuffer );
  5261. _gl.deleteBuffer( geometryGroup.__webglTangentBuffer );
  5262. _gl.deleteBuffer( geometryGroup.__webglColorBuffer );
  5263. _gl.deleteBuffer( geometryGroup.__webglUVBuffer );
  5264. _gl.deleteBuffer( geometryGroup.__webglUV2Buffer );
  5265. _gl.deleteBuffer( geometryGroup.__webglSkinVertexABuffer );
  5266. _gl.deleteBuffer( geometryGroup.__webglSkinVertexBBuffer );
  5267. _gl.deleteBuffer( geometryGroup.__webglSkinIndicesBuffer );
  5268. _gl.deleteBuffer( geometryGroup.__webglSkinWeightsBuffer );
  5269. _gl.deleteBuffer( geometryGroup.__webglFaceBuffer );
  5270. _gl.deleteBuffer( geometryGroup.__webglLineBuffer );
  5271. var m, ml;
  5272. if ( geometryGroup.numMorphTargets ) {
  5273. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  5274. _gl.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] );
  5275. }
  5276. }
  5277. if ( geometryGroup.numMorphNormals ) {
  5278. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  5279. _gl.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] );
  5280. }
  5281. }
  5282. if ( geometryGroup.__webglCustomAttributesList ) {
  5283. for ( var id in geometryGroup.__webglCustomAttributesList ) {
  5284. _gl.deleteBuffer( geometryGroup.__webglCustomAttributesList[ id ].buffer );
  5285. }
  5286. }
  5287. _this.info.memory.geometries --;
  5288. };
  5289. // Buffer initialization
  5290. function initCustomAttributes ( geometry, object ) {
  5291. var nvertices = geometry.vertices.length;
  5292. var material = object.material;
  5293. if ( material.attributes ) {
  5294. if ( geometry.__webglCustomAttributesList === undefined ) {
  5295. geometry.__webglCustomAttributesList = [];
  5296. }
  5297. for ( var a in material.attributes ) {
  5298. var attribute = material.attributes[ a ];
  5299. if( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  5300. attribute.__webglInitialized = true;
  5301. var size = 1; // "f" and "i"
  5302. if ( attribute.type === "v2" ) size = 2;
  5303. else if ( attribute.type === "v3" ) size = 3;
  5304. else if ( attribute.type === "v4" ) size = 4;
  5305. else if ( attribute.type === "c" ) size = 3;
  5306. attribute.size = size;
  5307. attribute.array = new Float32Array( nvertices * size );
  5308. attribute.buffer = _gl.createBuffer();
  5309. attribute.buffer.belongsToAttribute = a;
  5310. attribute.needsUpdate = true;
  5311. }
  5312. geometry.__webglCustomAttributesList.push( attribute );
  5313. }
  5314. }
  5315. };
  5316. function initParticleBuffers ( geometry, object ) {
  5317. var nvertices = geometry.vertices.length;
  5318. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  5319. geometry.__colorArray = new Float32Array( nvertices * 3 );
  5320. geometry.__sortArray = [];
  5321. geometry.__webglParticleCount = nvertices;
  5322. initCustomAttributes ( geometry, object );
  5323. };
  5324. function initLineBuffers ( geometry, object ) {
  5325. var nvertices = geometry.vertices.length;
  5326. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  5327. geometry.__colorArray = new Float32Array( nvertices * 3 );
  5328. geometry.__webglLineCount = nvertices;
  5329. initCustomAttributes ( geometry, object );
  5330. };
  5331. function initRibbonBuffers ( geometry ) {
  5332. var nvertices = geometry.vertices.length;
  5333. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  5334. geometry.__colorArray = new Float32Array( nvertices * 3 );
  5335. geometry.__webglVertexCount = nvertices;
  5336. };
  5337. function initMeshBuffers ( geometryGroup, object ) {
  5338. var geometry = object.geometry,
  5339. faces3 = geometryGroup.faces3,
  5340. faces4 = geometryGroup.faces4,
  5341. nvertices = faces3.length * 3 + faces4.length * 4,
  5342. ntris = faces3.length * 1 + faces4.length * 2,
  5343. nlines = faces3.length * 3 + faces4.length * 4,
  5344. material = getBufferMaterial( object, geometryGroup ),
  5345. uvType = bufferGuessUVType( material ),
  5346. normalType = bufferGuessNormalType( material ),
  5347. vertexColorType = bufferGuessVertexColorType( material );
  5348. //console.log( "uvType", uvType, "normalType", normalType, "vertexColorType", vertexColorType, object, geometryGroup, material );
  5349. geometryGroup.__vertexArray = new Float32Array( nvertices * 3 );
  5350. if ( normalType ) {
  5351. geometryGroup.__normalArray = new Float32Array( nvertices * 3 );
  5352. }
  5353. if ( geometry.hasTangents ) {
  5354. geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
  5355. }
  5356. if ( vertexColorType ) {
  5357. geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
  5358. }
  5359. if ( uvType ) {
  5360. if ( geometry.faceUvs.length > 0 || geometry.faceVertexUvs.length > 0 ) {
  5361. geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
  5362. }
  5363. if ( geometry.faceUvs.length > 1 || geometry.faceVertexUvs.length > 1 ) {
  5364. geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
  5365. }
  5366. }
  5367. if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) {
  5368. geometryGroup.__skinVertexAArray = new Float32Array( nvertices * 4 );
  5369. geometryGroup.__skinVertexBArray = new Float32Array( nvertices * 4 );
  5370. geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
  5371. geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
  5372. }
  5373. geometryGroup.__faceArray = new Uint16Array( ntris * 3 );
  5374. geometryGroup.__lineArray = new Uint16Array( nlines * 2 );
  5375. var m, ml;
  5376. if ( geometryGroup.numMorphTargets ) {
  5377. geometryGroup.__morphTargetsArrays = [];
  5378. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  5379. geometryGroup.__morphTargetsArrays.push( new Float32Array( nvertices * 3 ) );
  5380. }
  5381. }
  5382. if ( geometryGroup.numMorphNormals ) {
  5383. geometryGroup.__morphNormalsArrays = [];
  5384. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  5385. geometryGroup.__morphNormalsArrays.push( new Float32Array( nvertices * 3 ) );
  5386. }
  5387. }
  5388. geometryGroup.__webglFaceCount = ntris * 3;
  5389. geometryGroup.__webglLineCount = nlines * 2;
  5390. // custom attributes
  5391. if ( material.attributes ) {
  5392. if ( geometryGroup.__webglCustomAttributesList === undefined ) {
  5393. geometryGroup.__webglCustomAttributesList = [];
  5394. }
  5395. for ( var a in material.attributes ) {
  5396. // Do a shallow copy of the attribute object so different geometryGroup chunks use different
  5397. // attribute buffers which are correctly indexed in the setMeshBuffers function
  5398. var originalAttribute = material.attributes[ a ];
  5399. var attribute = {};
  5400. for ( var property in originalAttribute ) {
  5401. attribute[ property ] = originalAttribute[ property ];
  5402. }
  5403. if( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  5404. attribute.__webglInitialized = true;
  5405. var size = 1; // "f" and "i"
  5406. if( attribute.type === "v2" ) size = 2;
  5407. else if( attribute.type === "v3" ) size = 3;
  5408. else if( attribute.type === "v4" ) size = 4;
  5409. else if( attribute.type === "c" ) size = 3;
  5410. attribute.size = size;
  5411. attribute.array = new Float32Array( nvertices * size );
  5412. attribute.buffer = _gl.createBuffer();
  5413. attribute.buffer.belongsToAttribute = a;
  5414. originalAttribute.needsUpdate = true;
  5415. attribute.__original = originalAttribute;
  5416. }
  5417. geometryGroup.__webglCustomAttributesList.push( attribute );
  5418. }
  5419. }
  5420. geometryGroup.__inittedArrays = true;
  5421. };
  5422. function getBufferMaterial( object, geometryGroup ) {
  5423. if ( object.material && ! ( object.material instanceof THREE.MeshFaceMaterial ) ) {
  5424. return object.material;
  5425. } else if ( geometryGroup.materialIndex >= 0 ) {
  5426. return object.geometry.materials[ geometryGroup.materialIndex ];
  5427. }
  5428. };
  5429. function materialNeedsSmoothNormals ( material ) {
  5430. return material && material.shading !== undefined && material.shading === THREE.SmoothShading;
  5431. };
  5432. function bufferGuessNormalType ( material ) {
  5433. // only MeshBasicMaterial and MeshDepthMaterial don't need normals
  5434. if ( ( material instanceof THREE.MeshBasicMaterial && !material.envMap ) || material instanceof THREE.MeshDepthMaterial ) {
  5435. return false;
  5436. }
  5437. if ( materialNeedsSmoothNormals( material ) ) {
  5438. return THREE.SmoothShading;
  5439. } else {
  5440. return THREE.FlatShading;
  5441. }
  5442. };
  5443. function bufferGuessVertexColorType ( material ) {
  5444. if ( material.vertexColors ) {
  5445. return material.vertexColors;
  5446. }
  5447. return false;
  5448. };
  5449. function bufferGuessUVType ( material ) {
  5450. // material must use some texture to require uvs
  5451. if ( material.map || material.lightMap || material instanceof THREE.ShaderMaterial ) {
  5452. return true;
  5453. }
  5454. return false;
  5455. };
  5456. // Buffer setting
  5457. function setParticleBuffers ( geometry, hint, object ) {
  5458. var v, c, vertex, offset, index, color,
  5459. vertices = geometry.vertices,
  5460. vl = vertices.length,
  5461. colors = geometry.colors,
  5462. cl = colors.length,
  5463. vertexArray = geometry.__vertexArray,
  5464. colorArray = geometry.__colorArray,
  5465. sortArray = geometry.__sortArray,
  5466. dirtyVertices = geometry.__dirtyVertices,
  5467. dirtyElements = geometry.__dirtyElements,
  5468. dirtyColors = geometry.__dirtyColors,
  5469. customAttributes = geometry.__webglCustomAttributesList,
  5470. i, il,
  5471. a, ca, cal, value,
  5472. customAttribute;
  5473. if ( object.sortParticles ) {
  5474. _projScreenMatrixPS.copy( _projScreenMatrix );
  5475. _projScreenMatrixPS.multiplySelf( object.matrixWorld );
  5476. for ( v = 0; v < vl; v ++ ) {
  5477. vertex = vertices[ v ].position;
  5478. _vector3.copy( vertex );
  5479. _projScreenMatrixPS.multiplyVector3( _vector3 );
  5480. sortArray[ v ] = [ _vector3.z, v ];
  5481. }
  5482. sortArray.sort( function( a, b ) { return b[ 0 ] - a[ 0 ]; } );
  5483. for ( v = 0; v < vl; v ++ ) {
  5484. vertex = vertices[ sortArray[v][1] ].position;
  5485. offset = v * 3;
  5486. vertexArray[ offset ] = vertex.x;
  5487. vertexArray[ offset + 1 ] = vertex.y;
  5488. vertexArray[ offset + 2 ] = vertex.z;
  5489. }
  5490. for ( c = 0; c < cl; c ++ ) {
  5491. offset = c * 3;
  5492. color = colors[ sortArray[c][1] ];
  5493. colorArray[ offset ] = color.r;
  5494. colorArray[ offset + 1 ] = color.g;
  5495. colorArray[ offset + 2 ] = color.b;
  5496. }
  5497. if ( customAttributes ) {
  5498. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5499. customAttribute = customAttributes[ i ];
  5500. if ( ! ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) ) continue;
  5501. offset = 0;
  5502. cal = customAttribute.value.length;
  5503. if ( customAttribute.size === 1 ) {
  5504. for ( ca = 0; ca < cal; ca ++ ) {
  5505. index = sortArray[ ca ][ 1 ];
  5506. customAttribute.array[ ca ] = customAttribute.value[ index ];
  5507. }
  5508. } else if ( customAttribute.size === 2 ) {
  5509. for ( ca = 0; ca < cal; ca ++ ) {
  5510. index = sortArray[ ca ][ 1 ];
  5511. value = customAttribute.value[ index ];
  5512. customAttribute.array[ offset ] = value.x;
  5513. customAttribute.array[ offset + 1 ] = value.y;
  5514. offset += 2;
  5515. }
  5516. } else if ( customAttribute.size === 3 ) {
  5517. if ( customAttribute.type === "c" ) {
  5518. for ( ca = 0; ca < cal; ca ++ ) {
  5519. index = sortArray[ ca ][ 1 ];
  5520. value = customAttribute.value[ index ];
  5521. customAttribute.array[ offset ] = value.r;
  5522. customAttribute.array[ offset + 1 ] = value.g;
  5523. customAttribute.array[ offset + 2 ] = value.b;
  5524. offset += 3;
  5525. }
  5526. } else {
  5527. for ( ca = 0; ca < cal; ca ++ ) {
  5528. index = sortArray[ ca ][ 1 ];
  5529. value = customAttribute.value[ index ];
  5530. customAttribute.array[ offset ] = value.x;
  5531. customAttribute.array[ offset + 1 ] = value.y;
  5532. customAttribute.array[ offset + 2 ] = value.z;
  5533. offset += 3;
  5534. }
  5535. }
  5536. } else if ( customAttribute.size === 4 ) {
  5537. for ( ca = 0; ca < cal; ca ++ ) {
  5538. index = sortArray[ ca ][ 1 ];
  5539. value = customAttribute.value[ index ];
  5540. customAttribute.array[ offset ] = value.x;
  5541. customAttribute.array[ offset + 1 ] = value.y;
  5542. customAttribute.array[ offset + 2 ] = value.z;
  5543. customAttribute.array[ offset + 3 ] = value.w;
  5544. offset += 4;
  5545. }
  5546. }
  5547. }
  5548. }
  5549. } else {
  5550. if ( dirtyVertices ) {
  5551. for ( v = 0; v < vl; v ++ ) {
  5552. vertex = vertices[ v ].position;
  5553. offset = v * 3;
  5554. vertexArray[ offset ] = vertex.x;
  5555. vertexArray[ offset + 1 ] = vertex.y;
  5556. vertexArray[ offset + 2 ] = vertex.z;
  5557. }
  5558. }
  5559. if ( dirtyColors ) {
  5560. for ( c = 0; c < cl; c ++ ) {
  5561. color = colors[ c ];
  5562. offset = c * 3;
  5563. colorArray[ offset ] = color.r;
  5564. colorArray[ offset + 1 ] = color.g;
  5565. colorArray[ offset + 2 ] = color.b;
  5566. }
  5567. }
  5568. if ( customAttributes ) {
  5569. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5570. customAttribute = customAttributes[ i ];
  5571. if ( customAttribute.needsUpdate &&
  5572. ( customAttribute.boundTo === undefined ||
  5573. customAttribute.boundTo === "vertices") ) {
  5574. cal = customAttribute.value.length;
  5575. offset = 0;
  5576. if ( customAttribute.size === 1 ) {
  5577. for ( ca = 0; ca < cal; ca ++ ) {
  5578. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  5579. }
  5580. } else if ( customAttribute.size === 2 ) {
  5581. for ( ca = 0; ca < cal; ca ++ ) {
  5582. value = customAttribute.value[ ca ];
  5583. customAttribute.array[ offset ] = value.x;
  5584. customAttribute.array[ offset + 1 ] = value.y;
  5585. offset += 2;
  5586. }
  5587. } else if ( customAttribute.size === 3 ) {
  5588. if ( customAttribute.type === "c" ) {
  5589. for ( ca = 0; ca < cal; ca ++ ) {
  5590. value = customAttribute.value[ ca ];
  5591. customAttribute.array[ offset ] = value.r;
  5592. customAttribute.array[ offset + 1 ] = value.g;
  5593. customAttribute.array[ offset + 2 ] = value.b;
  5594. offset += 3;
  5595. }
  5596. } else {
  5597. for ( ca = 0; ca < cal; ca ++ ) {
  5598. value = customAttribute.value[ ca ];
  5599. customAttribute.array[ offset ] = value.x;
  5600. customAttribute.array[ offset + 1 ] = value.y;
  5601. customAttribute.array[ offset + 2 ] = value.z;
  5602. offset += 3;
  5603. }
  5604. }
  5605. } else if ( customAttribute.size === 4 ) {
  5606. for ( ca = 0; ca < cal; ca ++ ) {
  5607. value = customAttribute.value[ ca ];
  5608. customAttribute.array[ offset ] = value.x;
  5609. customAttribute.array[ offset + 1 ] = value.y;
  5610. customAttribute.array[ offset + 2 ] = value.z;
  5611. customAttribute.array[ offset + 3 ] = value.w;
  5612. offset += 4;
  5613. }
  5614. }
  5615. }
  5616. }
  5617. }
  5618. }
  5619. if ( dirtyVertices || object.sortParticles ) {
  5620. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  5621. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5622. }
  5623. if ( dirtyColors || object.sortParticles ) {
  5624. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  5625. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  5626. }
  5627. if ( customAttributes ) {
  5628. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5629. customAttribute = customAttributes[ i ];
  5630. if ( customAttribute.needsUpdate || object.sortParticles ) {
  5631. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  5632. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  5633. }
  5634. }
  5635. }
  5636. };
  5637. function setLineBuffers ( geometry, hint ) {
  5638. var v, c, vertex, offset, color,
  5639. vertices = geometry.vertices,
  5640. colors = geometry.colors,
  5641. vl = vertices.length,
  5642. cl = colors.length,
  5643. vertexArray = geometry.__vertexArray,
  5644. colorArray = geometry.__colorArray,
  5645. dirtyVertices = geometry.__dirtyVertices,
  5646. dirtyColors = geometry.__dirtyColors,
  5647. customAttributes = geometry.__webglCustomAttributesList,
  5648. i, il,
  5649. a, ca, cal, value,
  5650. customAttribute;
  5651. if ( dirtyVertices ) {
  5652. for ( v = 0; v < vl; v ++ ) {
  5653. vertex = vertices[ v ].position;
  5654. offset = v * 3;
  5655. vertexArray[ offset ] = vertex.x;
  5656. vertexArray[ offset + 1 ] = vertex.y;
  5657. vertexArray[ offset + 2 ] = vertex.z;
  5658. }
  5659. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  5660. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5661. }
  5662. if ( dirtyColors ) {
  5663. for ( c = 0; c < cl; c ++ ) {
  5664. color = colors[ c ];
  5665. offset = c * 3;
  5666. colorArray[ offset ] = color.r;
  5667. colorArray[ offset + 1 ] = color.g;
  5668. colorArray[ offset + 2 ] = color.b;
  5669. }
  5670. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  5671. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  5672. }
  5673. if ( customAttributes ) {
  5674. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5675. customAttribute = customAttributes[ i ];
  5676. if ( customAttribute.needsUpdate &&
  5677. ( customAttribute.boundTo === undefined ||
  5678. customAttribute.boundTo === "vertices" ) ) {
  5679. offset = 0;
  5680. cal = customAttribute.value.length;
  5681. if ( customAttribute.size === 1 ) {
  5682. for ( ca = 0; ca < cal; ca ++ ) {
  5683. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  5684. }
  5685. } else if ( customAttribute.size === 2 ) {
  5686. for ( ca = 0; ca < cal; ca ++ ) {
  5687. value = customAttribute.value[ ca ];
  5688. customAttribute.array[ offset ] = value.x;
  5689. customAttribute.array[ offset + 1 ] = value.y;
  5690. offset += 2;
  5691. }
  5692. } else if ( customAttribute.size === 3 ) {
  5693. if ( customAttribute.type === "c" ) {
  5694. for ( ca = 0; ca < cal; ca ++ ) {
  5695. value = customAttribute.value[ ca ];
  5696. customAttribute.array[ offset ] = value.r;
  5697. customAttribute.array[ offset + 1 ] = value.g;
  5698. customAttribute.array[ offset + 2 ] = value.b;
  5699. offset += 3;
  5700. }
  5701. } else {
  5702. for ( ca = 0; ca < cal; ca ++ ) {
  5703. value = customAttribute.value[ ca ];
  5704. customAttribute.array[ offset ] = value.x;
  5705. customAttribute.array[ offset + 1 ] = value.y;
  5706. customAttribute.array[ offset + 2 ] = value.z;
  5707. offset += 3;
  5708. }
  5709. }
  5710. } else if ( customAttribute.size === 4 ) {
  5711. for ( ca = 0; ca < cal; ca ++ ) {
  5712. value = customAttribute.value[ ca ];
  5713. customAttribute.array[ offset ] = value.x;
  5714. customAttribute.array[ offset + 1 ] = value.y;
  5715. customAttribute.array[ offset + 2 ] = value.z;
  5716. customAttribute.array[ offset + 3 ] = value.w;
  5717. offset += 4;
  5718. }
  5719. }
  5720. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  5721. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  5722. }
  5723. }
  5724. }
  5725. };
  5726. function setRibbonBuffers ( geometry, hint ) {
  5727. var v, c, vertex, offset, color,
  5728. vertices = geometry.vertices,
  5729. colors = geometry.colors,
  5730. vl = vertices.length,
  5731. cl = colors.length,
  5732. vertexArray = geometry.__vertexArray,
  5733. colorArray = geometry.__colorArray,
  5734. dirtyVertices = geometry.__dirtyVertices,
  5735. dirtyColors = geometry.__dirtyColors;
  5736. if ( dirtyVertices ) {
  5737. for ( v = 0; v < vl; v ++ ) {
  5738. vertex = vertices[ v ].position;
  5739. offset = v * 3;
  5740. vertexArray[ offset ] = vertex.x;
  5741. vertexArray[ offset + 1 ] = vertex.y;
  5742. vertexArray[ offset + 2 ] = vertex.z;
  5743. }
  5744. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  5745. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5746. }
  5747. if ( dirtyColors ) {
  5748. for ( c = 0; c < cl; c ++ ) {
  5749. color = colors[ c ];
  5750. offset = c * 3;
  5751. colorArray[ offset ] = color.r;
  5752. colorArray[ offset + 1 ] = color.g;
  5753. colorArray[ offset + 2 ] = color.b;
  5754. }
  5755. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  5756. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  5757. }
  5758. };
  5759. function setMeshBuffers( geometryGroup, object, hint, dispose, material ) {
  5760. if ( ! geometryGroup.__inittedArrays ) {
  5761. // console.log( object );
  5762. return;
  5763. }
  5764. var normalType = bufferGuessNormalType( material ),
  5765. vertexColorType = bufferGuessVertexColorType( material ),
  5766. uvType = bufferGuessUVType( material ),
  5767. needsSmoothNormals = ( normalType === THREE.SmoothShading );
  5768. var f, fl, fi, face,
  5769. vertexNormals, faceNormal, normal,
  5770. vertexColors, faceColor,
  5771. vertexTangents,
  5772. uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4, n1, n2, n3, n4,
  5773. c1, c2, c3, c4,
  5774. sw1, sw2, sw3, sw4,
  5775. si1, si2, si3, si4,
  5776. sa1, sa2, sa3, sa4,
  5777. sb1, sb2, sb3, sb4,
  5778. m, ml, i, il,
  5779. vn, uvi, uv2i,
  5780. vk, vkl, vka,
  5781. nka, chf, faceVertexNormals,
  5782. a,
  5783. vertexIndex = 0,
  5784. offset = 0,
  5785. offset_uv = 0,
  5786. offset_uv2 = 0,
  5787. offset_face = 0,
  5788. offset_normal = 0,
  5789. offset_tangent = 0,
  5790. offset_line = 0,
  5791. offset_color = 0,
  5792. offset_skin = 0,
  5793. offset_morphTarget = 0,
  5794. offset_custom = 0,
  5795. offset_customSrc = 0,
  5796. value,
  5797. vertexArray = geometryGroup.__vertexArray,
  5798. uvArray = geometryGroup.__uvArray,
  5799. uv2Array = geometryGroup.__uv2Array,
  5800. normalArray = geometryGroup.__normalArray,
  5801. tangentArray = geometryGroup.__tangentArray,
  5802. colorArray = geometryGroup.__colorArray,
  5803. skinVertexAArray = geometryGroup.__skinVertexAArray,
  5804. skinVertexBArray = geometryGroup.__skinVertexBArray,
  5805. skinIndexArray = geometryGroup.__skinIndexArray,
  5806. skinWeightArray = geometryGroup.__skinWeightArray,
  5807. morphTargetsArrays = geometryGroup.__morphTargetsArrays,
  5808. morphNormalsArrays = geometryGroup.__morphNormalsArrays,
  5809. customAttributes = geometryGroup.__webglCustomAttributesList,
  5810. customAttribute,
  5811. faceArray = geometryGroup.__faceArray,
  5812. lineArray = geometryGroup.__lineArray,
  5813. geometry = object.geometry, // this is shared for all chunks
  5814. dirtyVertices = geometry.__dirtyVertices,
  5815. dirtyElements = geometry.__dirtyElements,
  5816. dirtyUvs = geometry.__dirtyUvs,
  5817. dirtyNormals = geometry.__dirtyNormals,
  5818. dirtyTangents = geometry.__dirtyTangents,
  5819. dirtyColors = geometry.__dirtyColors,
  5820. dirtyMorphTargets = geometry.__dirtyMorphTargets,
  5821. vertices = geometry.vertices,
  5822. chunk_faces3 = geometryGroup.faces3,
  5823. chunk_faces4 = geometryGroup.faces4,
  5824. obj_faces = geometry.faces,
  5825. obj_uvs = geometry.faceVertexUvs[ 0 ],
  5826. obj_uvs2 = geometry.faceVertexUvs[ 1 ],
  5827. obj_colors = geometry.colors,
  5828. obj_skinVerticesA = geometry.skinVerticesA,
  5829. obj_skinVerticesB = geometry.skinVerticesB,
  5830. obj_skinIndices = geometry.skinIndices,
  5831. obj_skinWeights = geometry.skinWeights,
  5832. morphTargets = geometry.morphTargets,
  5833. morphNormals = geometry.morphNormals;
  5834. if ( dirtyVertices ) {
  5835. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  5836. face = obj_faces[ chunk_faces3[ f ] ];
  5837. v1 = vertices[ face.a ].position;
  5838. v2 = vertices[ face.b ].position;
  5839. v3 = vertices[ face.c ].position;
  5840. vertexArray[ offset ] = v1.x;
  5841. vertexArray[ offset + 1 ] = v1.y;
  5842. vertexArray[ offset + 2 ] = v1.z;
  5843. vertexArray[ offset + 3 ] = v2.x;
  5844. vertexArray[ offset + 4 ] = v2.y;
  5845. vertexArray[ offset + 5 ] = v2.z;
  5846. vertexArray[ offset + 6 ] = v3.x;
  5847. vertexArray[ offset + 7 ] = v3.y;
  5848. vertexArray[ offset + 8 ] = v3.z;
  5849. offset += 9;
  5850. }
  5851. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  5852. face = obj_faces[ chunk_faces4[ f ] ];
  5853. v1 = vertices[ face.a ].position;
  5854. v2 = vertices[ face.b ].position;
  5855. v3 = vertices[ face.c ].position;
  5856. v4 = vertices[ face.d ].position;
  5857. vertexArray[ offset ] = v1.x;
  5858. vertexArray[ offset + 1 ] = v1.y;
  5859. vertexArray[ offset + 2 ] = v1.z;
  5860. vertexArray[ offset + 3 ] = v2.x;
  5861. vertexArray[ offset + 4 ] = v2.y;
  5862. vertexArray[ offset + 5 ] = v2.z;
  5863. vertexArray[ offset + 6 ] = v3.x;
  5864. vertexArray[ offset + 7 ] = v3.y;
  5865. vertexArray[ offset + 8 ] = v3.z;
  5866. vertexArray[ offset + 9 ] = v4.x;
  5867. vertexArray[ offset + 10 ] = v4.y;
  5868. vertexArray[ offset + 11 ] = v4.z;
  5869. offset += 12;
  5870. }
  5871. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  5872. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5873. }
  5874. if ( dirtyMorphTargets ) {
  5875. for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) {
  5876. offset_morphTarget = 0;
  5877. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  5878. chf = chunk_faces3[ f ];
  5879. face = obj_faces[ chf ];
  5880. // morph positions
  5881. v1 = morphTargets[ vk ].vertices[ face.a ].position;
  5882. v2 = morphTargets[ vk ].vertices[ face.b ].position;
  5883. v3 = morphTargets[ vk ].vertices[ face.c ].position;
  5884. vka = morphTargetsArrays[ vk ];
  5885. vka[ offset_morphTarget ] = v1.x;
  5886. vka[ offset_morphTarget + 1 ] = v1.y;
  5887. vka[ offset_morphTarget + 2 ] = v1.z;
  5888. vka[ offset_morphTarget + 3 ] = v2.x;
  5889. vka[ offset_morphTarget + 4 ] = v2.y;
  5890. vka[ offset_morphTarget + 5 ] = v2.z;
  5891. vka[ offset_morphTarget + 6 ] = v3.x;
  5892. vka[ offset_morphTarget + 7 ] = v3.y;
  5893. vka[ offset_morphTarget + 8 ] = v3.z;
  5894. // morph normals
  5895. if ( material.morphNormals ) {
  5896. if ( needsSmoothNormals ) {
  5897. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  5898. n1 = faceVertexNormals.a;
  5899. n2 = faceVertexNormals.b;
  5900. n3 = faceVertexNormals.c;
  5901. } else {
  5902. n1 = morphNormals[ vk ].faceNormals[ chf ];
  5903. n2 = n1;
  5904. n3 = n1;
  5905. }
  5906. nka = morphNormalsArrays[ vk ];
  5907. nka[ offset_morphTarget ] = n1.x;
  5908. nka[ offset_morphTarget + 1 ] = n1.y;
  5909. nka[ offset_morphTarget + 2 ] = n1.z;
  5910. nka[ offset_morphTarget + 3 ] = n2.x;
  5911. nka[ offset_morphTarget + 4 ] = n2.y;
  5912. nka[ offset_morphTarget + 5 ] = n2.z;
  5913. nka[ offset_morphTarget + 6 ] = n3.x;
  5914. nka[ offset_morphTarget + 7 ] = n3.y;
  5915. nka[ offset_morphTarget + 8 ] = n3.z;
  5916. }
  5917. //
  5918. offset_morphTarget += 9;
  5919. }
  5920. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  5921. chf = chunk_faces4[ f ];
  5922. face = obj_faces[ chf ];
  5923. // morph positions
  5924. v1 = morphTargets[ vk ].vertices[ face.a ].position;
  5925. v2 = morphTargets[ vk ].vertices[ face.b ].position;
  5926. v3 = morphTargets[ vk ].vertices[ face.c ].position;
  5927. v4 = morphTargets[ vk ].vertices[ face.d ].position;
  5928. vka = morphTargetsArrays[ vk ];
  5929. vka[ offset_morphTarget ] = v1.x;
  5930. vka[ offset_morphTarget + 1 ] = v1.y;
  5931. vka[ offset_morphTarget + 2 ] = v1.z;
  5932. vka[ offset_morphTarget + 3 ] = v2.x;
  5933. vka[ offset_morphTarget + 4 ] = v2.y;
  5934. vka[ offset_morphTarget + 5 ] = v2.z;
  5935. vka[ offset_morphTarget + 6 ] = v3.x;
  5936. vka[ offset_morphTarget + 7 ] = v3.y;
  5937. vka[ offset_morphTarget + 8 ] = v3.z;
  5938. vka[ offset_morphTarget + 9 ] = v4.x;
  5939. vka[ offset_morphTarget + 10 ] = v4.y;
  5940. vka[ offset_morphTarget + 11 ] = v4.z;
  5941. // morph normals
  5942. if ( material.morphNormals ) {
  5943. if ( needsSmoothNormals ) {
  5944. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  5945. n1 = faceVertexNormals.a;
  5946. n2 = faceVertexNormals.b;
  5947. n3 = faceVertexNormals.c;
  5948. n4 = faceVertexNormals.d;
  5949. } else {
  5950. n1 = morphNormals[ vk ].faceNormals[ chf ];
  5951. n2 = n1;
  5952. n3 = n1;
  5953. n4 = n1;
  5954. }
  5955. nka = morphNormalsArrays[ vk ];
  5956. nka[ offset_morphTarget ] = n1.x;
  5957. nka[ offset_morphTarget + 1 ] = n1.y;
  5958. nka[ offset_morphTarget + 2 ] = n1.z;
  5959. nka[ offset_morphTarget + 3 ] = n2.x;
  5960. nka[ offset_morphTarget + 4 ] = n2.y;
  5961. nka[ offset_morphTarget + 5 ] = n2.z;
  5962. nka[ offset_morphTarget + 6 ] = n3.x;
  5963. nka[ offset_morphTarget + 7 ] = n3.y;
  5964. nka[ offset_morphTarget + 8 ] = n3.z;
  5965. nka[ offset_morphTarget + 9 ] = n4.x;
  5966. nka[ offset_morphTarget + 10 ] = n4.y;
  5967. nka[ offset_morphTarget + 11 ] = n4.z;
  5968. }
  5969. //
  5970. offset_morphTarget += 12;
  5971. }
  5972. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ vk ] );
  5973. _gl.bufferData( _gl.ARRAY_BUFFER, morphTargetsArrays[ vk ], hint );
  5974. if ( material.morphNormals ) {
  5975. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ vk ] );
  5976. _gl.bufferData( _gl.ARRAY_BUFFER, morphNormalsArrays[ vk ], hint );
  5977. }
  5978. }
  5979. }
  5980. if ( obj_skinWeights.length ) {
  5981. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  5982. face = obj_faces[ chunk_faces3[ f ] ];
  5983. // weights
  5984. sw1 = obj_skinWeights[ face.a ];
  5985. sw2 = obj_skinWeights[ face.b ];
  5986. sw3 = obj_skinWeights[ face.c ];
  5987. skinWeightArray[ offset_skin ] = sw1.x;
  5988. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  5989. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  5990. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  5991. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  5992. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  5993. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  5994. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  5995. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  5996. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  5997. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  5998. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  5999. // indices
  6000. si1 = obj_skinIndices[ face.a ];
  6001. si2 = obj_skinIndices[ face.b ];
  6002. si3 = obj_skinIndices[ face.c ];
  6003. skinIndexArray[ offset_skin ] = si1.x;
  6004. skinIndexArray[ offset_skin + 1 ] = si1.y;
  6005. skinIndexArray[ offset_skin + 2 ] = si1.z;
  6006. skinIndexArray[ offset_skin + 3 ] = si1.w;
  6007. skinIndexArray[ offset_skin + 4 ] = si2.x;
  6008. skinIndexArray[ offset_skin + 5 ] = si2.y;
  6009. skinIndexArray[ offset_skin + 6 ] = si2.z;
  6010. skinIndexArray[ offset_skin + 7 ] = si2.w;
  6011. skinIndexArray[ offset_skin + 8 ] = si3.x;
  6012. skinIndexArray[ offset_skin + 9 ] = si3.y;
  6013. skinIndexArray[ offset_skin + 10 ] = si3.z;
  6014. skinIndexArray[ offset_skin + 11 ] = si3.w;
  6015. // vertices A
  6016. sa1 = obj_skinVerticesA[ face.a ];
  6017. sa2 = obj_skinVerticesA[ face.b ];
  6018. sa3 = obj_skinVerticesA[ face.c ];
  6019. skinVertexAArray[ offset_skin ] = sa1.x;
  6020. skinVertexAArray[ offset_skin + 1 ] = sa1.y;
  6021. skinVertexAArray[ offset_skin + 2 ] = sa1.z;
  6022. skinVertexAArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6023. skinVertexAArray[ offset_skin + 4 ] = sa2.x;
  6024. skinVertexAArray[ offset_skin + 5 ] = sa2.y;
  6025. skinVertexAArray[ offset_skin + 6 ] = sa2.z;
  6026. skinVertexAArray[ offset_skin + 7 ] = 1;
  6027. skinVertexAArray[ offset_skin + 8 ] = sa3.x;
  6028. skinVertexAArray[ offset_skin + 9 ] = sa3.y;
  6029. skinVertexAArray[ offset_skin + 10 ] = sa3.z;
  6030. skinVertexAArray[ offset_skin + 11 ] = 1;
  6031. // vertices B
  6032. sb1 = obj_skinVerticesB[ face.a ];
  6033. sb2 = obj_skinVerticesB[ face.b ];
  6034. sb3 = obj_skinVerticesB[ face.c ];
  6035. skinVertexBArray[ offset_skin ] = sb1.x;
  6036. skinVertexBArray[ offset_skin + 1 ] = sb1.y;
  6037. skinVertexBArray[ offset_skin + 2 ] = sb1.z;
  6038. skinVertexBArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6039. skinVertexBArray[ offset_skin + 4 ] = sb2.x;
  6040. skinVertexBArray[ offset_skin + 5 ] = sb2.y;
  6041. skinVertexBArray[ offset_skin + 6 ] = sb2.z;
  6042. skinVertexBArray[ offset_skin + 7 ] = 1;
  6043. skinVertexBArray[ offset_skin + 8 ] = sb3.x;
  6044. skinVertexBArray[ offset_skin + 9 ] = sb3.y;
  6045. skinVertexBArray[ offset_skin + 10 ] = sb3.z;
  6046. skinVertexBArray[ offset_skin + 11 ] = 1;
  6047. offset_skin += 12;
  6048. }
  6049. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6050. face = obj_faces[ chunk_faces4[ f ] ];
  6051. // weights
  6052. sw1 = obj_skinWeights[ face.a ];
  6053. sw2 = obj_skinWeights[ face.b ];
  6054. sw3 = obj_skinWeights[ face.c ];
  6055. sw4 = obj_skinWeights[ face.d ];
  6056. skinWeightArray[ offset_skin ] = sw1.x;
  6057. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  6058. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  6059. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  6060. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  6061. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  6062. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  6063. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  6064. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  6065. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  6066. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  6067. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  6068. skinWeightArray[ offset_skin + 12 ] = sw4.x;
  6069. skinWeightArray[ offset_skin + 13 ] = sw4.y;
  6070. skinWeightArray[ offset_skin + 14 ] = sw4.z;
  6071. skinWeightArray[ offset_skin + 15 ] = sw4.w;
  6072. // indices
  6073. si1 = obj_skinIndices[ face.a ];
  6074. si2 = obj_skinIndices[ face.b ];
  6075. si3 = obj_skinIndices[ face.c ];
  6076. si4 = obj_skinIndices[ face.d ];
  6077. skinIndexArray[ offset_skin ] = si1.x;
  6078. skinIndexArray[ offset_skin + 1 ] = si1.y;
  6079. skinIndexArray[ offset_skin + 2 ] = si1.z;
  6080. skinIndexArray[ offset_skin + 3 ] = si1.w;
  6081. skinIndexArray[ offset_skin + 4 ] = si2.x;
  6082. skinIndexArray[ offset_skin + 5 ] = si2.y;
  6083. skinIndexArray[ offset_skin + 6 ] = si2.z;
  6084. skinIndexArray[ offset_skin + 7 ] = si2.w;
  6085. skinIndexArray[ offset_skin + 8 ] = si3.x;
  6086. skinIndexArray[ offset_skin + 9 ] = si3.y;
  6087. skinIndexArray[ offset_skin + 10 ] = si3.z;
  6088. skinIndexArray[ offset_skin + 11 ] = si3.w;
  6089. skinIndexArray[ offset_skin + 12 ] = si4.x;
  6090. skinIndexArray[ offset_skin + 13 ] = si4.y;
  6091. skinIndexArray[ offset_skin + 14 ] = si4.z;
  6092. skinIndexArray[ offset_skin + 15 ] = si4.w;
  6093. // vertices A
  6094. sa1 = obj_skinVerticesA[ face.a ];
  6095. sa2 = obj_skinVerticesA[ face.b ];
  6096. sa3 = obj_skinVerticesA[ face.c ];
  6097. sa4 = obj_skinVerticesA[ face.d ];
  6098. skinVertexAArray[ offset_skin ] = sa1.x;
  6099. skinVertexAArray[ offset_skin + 1 ] = sa1.y;
  6100. skinVertexAArray[ offset_skin + 2 ] = sa1.z;
  6101. skinVertexAArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6102. skinVertexAArray[ offset_skin + 4 ] = sa2.x;
  6103. skinVertexAArray[ offset_skin + 5 ] = sa2.y;
  6104. skinVertexAArray[ offset_skin + 6 ] = sa2.z;
  6105. skinVertexAArray[ offset_skin + 7 ] = 1;
  6106. skinVertexAArray[ offset_skin + 8 ] = sa3.x;
  6107. skinVertexAArray[ offset_skin + 9 ] = sa3.y;
  6108. skinVertexAArray[ offset_skin + 10 ] = sa3.z;
  6109. skinVertexAArray[ offset_skin + 11 ] = 1;
  6110. skinVertexAArray[ offset_skin + 12 ] = sa4.x;
  6111. skinVertexAArray[ offset_skin + 13 ] = sa4.y;
  6112. skinVertexAArray[ offset_skin + 14 ] = sa4.z;
  6113. skinVertexAArray[ offset_skin + 15 ] = 1;
  6114. // vertices B
  6115. sb1 = obj_skinVerticesB[ face.a ];
  6116. sb2 = obj_skinVerticesB[ face.b ];
  6117. sb3 = obj_skinVerticesB[ face.c ];
  6118. sb4 = obj_skinVerticesB[ face.d ];
  6119. skinVertexBArray[ offset_skin ] = sb1.x;
  6120. skinVertexBArray[ offset_skin + 1 ] = sb1.y;
  6121. skinVertexBArray[ offset_skin + 2 ] = sb1.z;
  6122. skinVertexBArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6123. skinVertexBArray[ offset_skin + 4 ] = sb2.x;
  6124. skinVertexBArray[ offset_skin + 5 ] = sb2.y;
  6125. skinVertexBArray[ offset_skin + 6 ] = sb2.z;
  6126. skinVertexBArray[ offset_skin + 7 ] = 1;
  6127. skinVertexBArray[ offset_skin + 8 ] = sb3.x;
  6128. skinVertexBArray[ offset_skin + 9 ] = sb3.y;
  6129. skinVertexBArray[ offset_skin + 10 ] = sb3.z;
  6130. skinVertexBArray[ offset_skin + 11 ] = 1;
  6131. skinVertexBArray[ offset_skin + 12 ] = sb4.x;
  6132. skinVertexBArray[ offset_skin + 13 ] = sb4.y;
  6133. skinVertexBArray[ offset_skin + 14 ] = sb4.z;
  6134. skinVertexBArray[ offset_skin + 15 ] = 1;
  6135. offset_skin += 16;
  6136. }
  6137. if ( offset_skin > 0 ) {
  6138. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexABuffer );
  6139. _gl.bufferData( _gl.ARRAY_BUFFER, skinVertexAArray, hint );
  6140. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexBBuffer );
  6141. _gl.bufferData( _gl.ARRAY_BUFFER, skinVertexBArray, hint );
  6142. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  6143. _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint );
  6144. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  6145. _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint );
  6146. }
  6147. }
  6148. if ( dirtyColors && vertexColorType ) {
  6149. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6150. face = obj_faces[ chunk_faces3[ f ] ];
  6151. vertexColors = face.vertexColors;
  6152. faceColor = face.color;
  6153. if ( vertexColors.length === 3 && vertexColorType === THREE.VertexColors ) {
  6154. c1 = vertexColors[ 0 ];
  6155. c2 = vertexColors[ 1 ];
  6156. c3 = vertexColors[ 2 ];
  6157. } else {
  6158. c1 = faceColor;
  6159. c2 = faceColor;
  6160. c3 = faceColor;
  6161. }
  6162. colorArray[ offset_color ] = c1.r;
  6163. colorArray[ offset_color + 1 ] = c1.g;
  6164. colorArray[ offset_color + 2 ] = c1.b;
  6165. colorArray[ offset_color + 3 ] = c2.r;
  6166. colorArray[ offset_color + 4 ] = c2.g;
  6167. colorArray[ offset_color + 5 ] = c2.b;
  6168. colorArray[ offset_color + 6 ] = c3.r;
  6169. colorArray[ offset_color + 7 ] = c3.g;
  6170. colorArray[ offset_color + 8 ] = c3.b;
  6171. offset_color += 9;
  6172. }
  6173. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6174. face = obj_faces[ chunk_faces4[ f ] ];
  6175. vertexColors = face.vertexColors;
  6176. faceColor = face.color;
  6177. if ( vertexColors.length === 4 && vertexColorType === THREE.VertexColors ) {
  6178. c1 = vertexColors[ 0 ];
  6179. c2 = vertexColors[ 1 ];
  6180. c3 = vertexColors[ 2 ];
  6181. c4 = vertexColors[ 3 ];
  6182. } else {
  6183. c1 = faceColor;
  6184. c2 = faceColor;
  6185. c3 = faceColor;
  6186. c4 = faceColor;
  6187. }
  6188. colorArray[ offset_color ] = c1.r;
  6189. colorArray[ offset_color + 1 ] = c1.g;
  6190. colorArray[ offset_color + 2 ] = c1.b;
  6191. colorArray[ offset_color + 3 ] = c2.r;
  6192. colorArray[ offset_color + 4 ] = c2.g;
  6193. colorArray[ offset_color + 5 ] = c2.b;
  6194. colorArray[ offset_color + 6 ] = c3.r;
  6195. colorArray[ offset_color + 7 ] = c3.g;
  6196. colorArray[ offset_color + 8 ] = c3.b;
  6197. colorArray[ offset_color + 9 ] = c4.r;
  6198. colorArray[ offset_color + 10 ] = c4.g;
  6199. colorArray[ offset_color + 11 ] = c4.b;
  6200. offset_color += 12;
  6201. }
  6202. if ( offset_color > 0 ) {
  6203. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  6204. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  6205. }
  6206. }
  6207. if ( dirtyTangents && geometry.hasTangents ) {
  6208. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6209. face = obj_faces[ chunk_faces3[ f ] ];
  6210. vertexTangents = face.vertexTangents;
  6211. t1 = vertexTangents[ 0 ];
  6212. t2 = vertexTangents[ 1 ];
  6213. t3 = vertexTangents[ 2 ];
  6214. tangentArray[ offset_tangent ] = t1.x;
  6215. tangentArray[ offset_tangent + 1 ] = t1.y;
  6216. tangentArray[ offset_tangent + 2 ] = t1.z;
  6217. tangentArray[ offset_tangent + 3 ] = t1.w;
  6218. tangentArray[ offset_tangent + 4 ] = t2.x;
  6219. tangentArray[ offset_tangent + 5 ] = t2.y;
  6220. tangentArray[ offset_tangent + 6 ] = t2.z;
  6221. tangentArray[ offset_tangent + 7 ] = t2.w;
  6222. tangentArray[ offset_tangent + 8 ] = t3.x;
  6223. tangentArray[ offset_tangent + 9 ] = t3.y;
  6224. tangentArray[ offset_tangent + 10 ] = t3.z;
  6225. tangentArray[ offset_tangent + 11 ] = t3.w;
  6226. offset_tangent += 12;
  6227. }
  6228. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6229. face = obj_faces[ chunk_faces4[ f ] ];
  6230. vertexTangents = face.vertexTangents;
  6231. t1 = vertexTangents[ 0 ];
  6232. t2 = vertexTangents[ 1 ];
  6233. t3 = vertexTangents[ 2 ];
  6234. t4 = vertexTangents[ 3 ];
  6235. tangentArray[ offset_tangent ] = t1.x;
  6236. tangentArray[ offset_tangent + 1 ] = t1.y;
  6237. tangentArray[ offset_tangent + 2 ] = t1.z;
  6238. tangentArray[ offset_tangent + 3 ] = t1.w;
  6239. tangentArray[ offset_tangent + 4 ] = t2.x;
  6240. tangentArray[ offset_tangent + 5 ] = t2.y;
  6241. tangentArray[ offset_tangent + 6 ] = t2.z;
  6242. tangentArray[ offset_tangent + 7 ] = t2.w;
  6243. tangentArray[ offset_tangent + 8 ] = t3.x;
  6244. tangentArray[ offset_tangent + 9 ] = t3.y;
  6245. tangentArray[ offset_tangent + 10 ] = t3.z;
  6246. tangentArray[ offset_tangent + 11 ] = t3.w;
  6247. tangentArray[ offset_tangent + 12 ] = t4.x;
  6248. tangentArray[ offset_tangent + 13 ] = t4.y;
  6249. tangentArray[ offset_tangent + 14 ] = t4.z;
  6250. tangentArray[ offset_tangent + 15 ] = t4.w;
  6251. offset_tangent += 16;
  6252. }
  6253. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  6254. _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint );
  6255. }
  6256. if ( dirtyNormals && normalType ) {
  6257. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6258. face = obj_faces[ chunk_faces3[ f ] ];
  6259. vertexNormals = face.vertexNormals;
  6260. faceNormal = face.normal;
  6261. if ( vertexNormals.length === 3 && needsSmoothNormals ) {
  6262. for ( i = 0; i < 3; i ++ ) {
  6263. vn = vertexNormals[ i ];
  6264. normalArray[ offset_normal ] = vn.x;
  6265. normalArray[ offset_normal + 1 ] = vn.y;
  6266. normalArray[ offset_normal + 2 ] = vn.z;
  6267. offset_normal += 3;
  6268. }
  6269. } else {
  6270. for ( i = 0; i < 3; i ++ ) {
  6271. normalArray[ offset_normal ] = faceNormal.x;
  6272. normalArray[ offset_normal + 1 ] = faceNormal.y;
  6273. normalArray[ offset_normal + 2 ] = faceNormal.z;
  6274. offset_normal += 3;
  6275. }
  6276. }
  6277. }
  6278. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6279. face = obj_faces[ chunk_faces4[ f ] ];
  6280. vertexNormals = face.vertexNormals;
  6281. faceNormal = face.normal;
  6282. if ( vertexNormals.length === 4 && needsSmoothNormals ) {
  6283. for ( i = 0; i < 4; i ++ ) {
  6284. vn = vertexNormals[ i ];
  6285. normalArray[ offset_normal ] = vn.x;
  6286. normalArray[ offset_normal + 1 ] = vn.y;
  6287. normalArray[ offset_normal + 2 ] = vn.z;
  6288. offset_normal += 3;
  6289. }
  6290. } else {
  6291. for ( i = 0; i < 4; i ++ ) {
  6292. normalArray[ offset_normal ] = faceNormal.x;
  6293. normalArray[ offset_normal + 1 ] = faceNormal.y;
  6294. normalArray[ offset_normal + 2 ] = faceNormal.z;
  6295. offset_normal += 3;
  6296. }
  6297. }
  6298. }
  6299. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  6300. _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
  6301. }
  6302. if ( dirtyUvs && obj_uvs && uvType ) {
  6303. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6304. fi = chunk_faces3[ f ];
  6305. face = obj_faces[ fi ];
  6306. uv = obj_uvs[ fi ];
  6307. if ( uv === undefined ) continue;
  6308. for ( i = 0; i < 3; i ++ ) {
  6309. uvi = uv[ i ];
  6310. uvArray[ offset_uv ] = uvi.u;
  6311. uvArray[ offset_uv + 1 ] = uvi.v;
  6312. offset_uv += 2;
  6313. }
  6314. }
  6315. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6316. fi = chunk_faces4[ f ];
  6317. face = obj_faces[ fi ];
  6318. uv = obj_uvs[ fi ];
  6319. if ( uv === undefined ) continue;
  6320. for ( i = 0; i < 4; i ++ ) {
  6321. uvi = uv[ i ];
  6322. uvArray[ offset_uv ] = uvi.u;
  6323. uvArray[ offset_uv + 1 ] = uvi.v;
  6324. offset_uv += 2;
  6325. }
  6326. }
  6327. if ( offset_uv > 0 ) {
  6328. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  6329. _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint );
  6330. }
  6331. }
  6332. if ( dirtyUvs && obj_uvs2 && uvType ) {
  6333. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6334. fi = chunk_faces3[ f ];
  6335. face = obj_faces[ fi ];
  6336. uv2 = obj_uvs2[ fi ];
  6337. if ( uv2 === undefined ) continue;
  6338. for ( i = 0; i < 3; i ++ ) {
  6339. uv2i = uv2[ i ];
  6340. uv2Array[ offset_uv2 ] = uv2i.u;
  6341. uv2Array[ offset_uv2 + 1 ] = uv2i.v;
  6342. offset_uv2 += 2;
  6343. }
  6344. }
  6345. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6346. fi = chunk_faces4[ f ];
  6347. face = obj_faces[ fi ];
  6348. uv2 = obj_uvs2[ fi ];
  6349. if ( uv2 === undefined ) continue;
  6350. for ( i = 0; i < 4; i ++ ) {
  6351. uv2i = uv2[ i ];
  6352. uv2Array[ offset_uv2 ] = uv2i.u;
  6353. uv2Array[ offset_uv2 + 1 ] = uv2i.v;
  6354. offset_uv2 += 2;
  6355. }
  6356. }
  6357. if ( offset_uv2 > 0 ) {
  6358. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  6359. _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint );
  6360. }
  6361. }
  6362. if ( dirtyElements ) {
  6363. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6364. face = obj_faces[ chunk_faces3[ f ] ];
  6365. faceArray[ offset_face ] = vertexIndex;
  6366. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  6367. faceArray[ offset_face + 2 ] = vertexIndex + 2;
  6368. offset_face += 3;
  6369. lineArray[ offset_line ] = vertexIndex;
  6370. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  6371. lineArray[ offset_line + 2 ] = vertexIndex;
  6372. lineArray[ offset_line + 3 ] = vertexIndex + 2;
  6373. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  6374. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  6375. offset_line += 6;
  6376. vertexIndex += 3;
  6377. }
  6378. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6379. face = obj_faces[ chunk_faces4[ f ] ];
  6380. faceArray[ offset_face ] = vertexIndex;
  6381. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  6382. faceArray[ offset_face + 2 ] = vertexIndex + 3;
  6383. faceArray[ offset_face + 3 ] = vertexIndex + 1;
  6384. faceArray[ offset_face + 4 ] = vertexIndex + 2;
  6385. faceArray[ offset_face + 5 ] = vertexIndex + 3;
  6386. offset_face += 6;
  6387. lineArray[ offset_line ] = vertexIndex;
  6388. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  6389. lineArray[ offset_line + 2 ] = vertexIndex;
  6390. lineArray[ offset_line + 3 ] = vertexIndex + 3;
  6391. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  6392. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  6393. lineArray[ offset_line + 6 ] = vertexIndex + 2;
  6394. lineArray[ offset_line + 7 ] = vertexIndex + 3;
  6395. offset_line += 8;
  6396. vertexIndex += 4;
  6397. }
  6398. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  6399. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint );
  6400. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  6401. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
  6402. }
  6403. if ( customAttributes ) {
  6404. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  6405. customAttribute = customAttributes[ i ];
  6406. if ( ! customAttribute.__original.needsUpdate ) continue;
  6407. offset_custom = 0;
  6408. offset_customSrc = 0;
  6409. if ( customAttribute.size === 1 ) {
  6410. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6411. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6412. face = obj_faces[ chunk_faces3[ f ] ];
  6413. customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
  6414. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  6415. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  6416. offset_custom += 3;
  6417. }
  6418. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6419. face = obj_faces[ chunk_faces4[ f ] ];
  6420. customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
  6421. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  6422. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  6423. customAttribute.array[ offset_custom + 3 ] = customAttribute.value[ face.d ];
  6424. offset_custom += 4;
  6425. }
  6426. } else if ( customAttribute.boundTo === "faces" ) {
  6427. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6428. value = customAttribute.value[ chunk_faces3[ f ] ];
  6429. customAttribute.array[ offset_custom ] = value;
  6430. customAttribute.array[ offset_custom + 1 ] = value;
  6431. customAttribute.array[ offset_custom + 2 ] = value;
  6432. offset_custom += 3;
  6433. }
  6434. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6435. value = customAttribute.value[ chunk_faces4[ f ] ];
  6436. customAttribute.array[ offset_custom ] = value;
  6437. customAttribute.array[ offset_custom + 1 ] = value;
  6438. customAttribute.array[ offset_custom + 2 ] = value;
  6439. customAttribute.array[ offset_custom + 3 ] = value;
  6440. offset_custom += 4;
  6441. }
  6442. }
  6443. } else if ( customAttribute.size === 2 ) {
  6444. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6445. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6446. face = obj_faces[ chunk_faces3[ f ] ];
  6447. v1 = customAttribute.value[ face.a ];
  6448. v2 = customAttribute.value[ face.b ];
  6449. v3 = customAttribute.value[ face.c ];
  6450. customAttribute.array[ offset_custom ] = v1.x;
  6451. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6452. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6453. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6454. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6455. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6456. offset_custom += 6;
  6457. }
  6458. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6459. face = obj_faces[ chunk_faces4[ f ] ];
  6460. v1 = customAttribute.value[ face.a ];
  6461. v2 = customAttribute.value[ face.b ];
  6462. v3 = customAttribute.value[ face.c ];
  6463. v4 = customAttribute.value[ face.d ];
  6464. customAttribute.array[ offset_custom ] = v1.x;
  6465. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6466. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6467. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6468. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6469. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6470. customAttribute.array[ offset_custom + 6 ] = v4.x;
  6471. customAttribute.array[ offset_custom + 7 ] = v4.y;
  6472. offset_custom += 8;
  6473. }
  6474. } else if ( customAttribute.boundTo === "faces" ) {
  6475. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6476. value = customAttribute.value[ chunk_faces3[ f ] ];
  6477. v1 = value;
  6478. v2 = value;
  6479. v3 = value;
  6480. customAttribute.array[ offset_custom ] = v1.x;
  6481. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6482. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6483. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6484. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6485. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6486. offset_custom += 6;
  6487. }
  6488. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6489. value = customAttribute.value[ chunk_faces4[ f ] ];
  6490. v1 = value;
  6491. v2 = value;
  6492. v3 = value;
  6493. v4 = value;
  6494. customAttribute.array[ offset_custom ] = v1.x;
  6495. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6496. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6497. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6498. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6499. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6500. customAttribute.array[ offset_custom + 6 ] = v4.x;
  6501. customAttribute.array[ offset_custom + 7 ] = v4.y;
  6502. offset_custom += 8;
  6503. }
  6504. }
  6505. } else if ( customAttribute.size === 3 ) {
  6506. var pp;
  6507. if ( customAttribute.type === "c" ) {
  6508. pp = [ "r", "g", "b" ];
  6509. } else {
  6510. pp = [ "x", "y", "z" ];
  6511. }
  6512. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6513. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6514. face = obj_faces[ chunk_faces3[ f ] ];
  6515. v1 = customAttribute.value[ face.a ];
  6516. v2 = customAttribute.value[ face.b ];
  6517. v3 = customAttribute.value[ face.c ];
  6518. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6519. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6520. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6521. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6522. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6523. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6524. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6525. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6526. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6527. offset_custom += 9;
  6528. }
  6529. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6530. face = obj_faces[ chunk_faces4[ f ] ];
  6531. v1 = customAttribute.value[ face.a ];
  6532. v2 = customAttribute.value[ face.b ];
  6533. v3 = customAttribute.value[ face.c ];
  6534. v4 = customAttribute.value[ face.d ];
  6535. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6536. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6537. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6538. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6539. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6540. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6541. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6542. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6543. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6544. customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
  6545. customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
  6546. customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
  6547. offset_custom += 12;
  6548. }
  6549. } else if ( customAttribute.boundTo === "faces" ) {
  6550. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6551. value = customAttribute.value[ chunk_faces3[ f ] ];
  6552. v1 = value;
  6553. v2 = value;
  6554. v3 = value;
  6555. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6556. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6557. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6558. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6559. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6560. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6561. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6562. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6563. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6564. offset_custom += 9;
  6565. }
  6566. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6567. value = customAttribute.value[ chunk_faces4[ f ] ];
  6568. v1 = value;
  6569. v2 = value;
  6570. v3 = value;
  6571. v4 = value;
  6572. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6573. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6574. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6575. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6576. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6577. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6578. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6579. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6580. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6581. customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
  6582. customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
  6583. customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
  6584. offset_custom += 12;
  6585. }
  6586. }
  6587. } else if ( customAttribute.size === 4 ) {
  6588. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6589. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6590. face = obj_faces[ chunk_faces3[ f ] ];
  6591. v1 = customAttribute.value[ face.a ];
  6592. v2 = customAttribute.value[ face.b ];
  6593. v3 = customAttribute.value[ face.c ];
  6594. customAttribute.array[ offset_custom ] = v1.x;
  6595. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6596. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6597. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6598. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6599. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6600. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6601. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6602. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6603. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6604. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6605. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6606. offset_custom += 12;
  6607. }
  6608. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6609. face = obj_faces[ chunk_faces4[ f ] ];
  6610. v1 = customAttribute.value[ face.a ];
  6611. v2 = customAttribute.value[ face.b ];
  6612. v3 = customAttribute.value[ face.c ];
  6613. v4 = customAttribute.value[ face.d ];
  6614. customAttribute.array[ offset_custom ] = v1.x;
  6615. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6616. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6617. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6618. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6619. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6620. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6621. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6622. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6623. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6624. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6625. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6626. customAttribute.array[ offset_custom + 12 ] = v4.x;
  6627. customAttribute.array[ offset_custom + 13 ] = v4.y;
  6628. customAttribute.array[ offset_custom + 14 ] = v4.z;
  6629. customAttribute.array[ offset_custom + 15 ] = v4.w;
  6630. offset_custom += 16;
  6631. }
  6632. } else if ( customAttribute.boundTo === "faces" ) {
  6633. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6634. value = customAttribute.value[ chunk_faces3[ f ] ];
  6635. v1 = value;
  6636. v2 = value;
  6637. v3 = value;
  6638. customAttribute.array[ offset_custom ] = v1.x;
  6639. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6640. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6641. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6642. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6643. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6644. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6645. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6646. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6647. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6648. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6649. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6650. offset_custom += 12;
  6651. }
  6652. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6653. value = customAttribute.value[ chunk_faces4[ f ] ];
  6654. v1 = value;
  6655. v2 = value;
  6656. v3 = value;
  6657. v4 = value;
  6658. customAttribute.array[ offset_custom ] = v1.x;
  6659. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6660. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6661. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6662. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6663. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6664. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6665. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6666. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6667. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6668. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6669. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6670. customAttribute.array[ offset_custom + 12 ] = v4.x;
  6671. customAttribute.array[ offset_custom + 13 ] = v4.y;
  6672. customAttribute.array[ offset_custom + 14 ] = v4.z;
  6673. customAttribute.array[ offset_custom + 15 ] = v4.w;
  6674. offset_custom += 16;
  6675. }
  6676. }
  6677. }
  6678. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  6679. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  6680. }
  6681. }
  6682. if ( dispose ) {
  6683. delete geometryGroup.__inittedArrays;
  6684. delete geometryGroup.__colorArray;
  6685. delete geometryGroup.__normalArray;
  6686. delete geometryGroup.__tangentArray;
  6687. delete geometryGroup.__uvArray;
  6688. delete geometryGroup.__uv2Array;
  6689. delete geometryGroup.__faceArray;
  6690. delete geometryGroup.__vertexArray;
  6691. delete geometryGroup.__lineArray;
  6692. delete geometryGroup.__skinVertexAArray;
  6693. delete geometryGroup.__skinVertexBArray;
  6694. delete geometryGroup.__skinIndexArray;
  6695. delete geometryGroup.__skinWeightArray;
  6696. }
  6697. };
  6698. // Buffer rendering
  6699. this.renderBufferImmediate = function ( object, program, shading ) {
  6700. if ( ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
  6701. if ( ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
  6702. if ( object.hasPos ) {
  6703. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
  6704. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  6705. _gl.enableVertexAttribArray( program.attributes.position );
  6706. _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6707. }
  6708. if ( object.hasNormal ) {
  6709. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer );
  6710. if ( shading === THREE.FlatShading ) {
  6711. var nx, ny, nz,
  6712. nax, nbx, ncx, nay, nby, ncy, naz, nbz, ncz,
  6713. normalArray,
  6714. i, il = object.count * 3;
  6715. for( i = 0; i < il; i += 9 ) {
  6716. normalArray = object.normalArray;
  6717. nax = normalArray[ i ];
  6718. nay = normalArray[ i + 1 ];
  6719. naz = normalArray[ i + 2 ];
  6720. nbx = normalArray[ i + 3 ];
  6721. nby = normalArray[ i + 4 ];
  6722. nbz = normalArray[ i + 5 ];
  6723. ncx = normalArray[ i + 6 ];
  6724. ncy = normalArray[ i + 7 ];
  6725. ncz = normalArray[ i + 8 ];
  6726. nx = ( nax + nbx + ncx ) / 3;
  6727. ny = ( nay + nby + ncy ) / 3;
  6728. nz = ( naz + nbz + ncz ) / 3;
  6729. normalArray[ i ] = nx;
  6730. normalArray[ i + 1 ] = ny;
  6731. normalArray[ i + 2 ] = nz;
  6732. normalArray[ i + 3 ] = nx;
  6733. normalArray[ i + 4 ] = ny;
  6734. normalArray[ i + 5 ] = nz;
  6735. normalArray[ i + 6 ] = nx;
  6736. normalArray[ i + 7 ] = ny;
  6737. normalArray[ i + 8 ] = nz;
  6738. }
  6739. }
  6740. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  6741. _gl.enableVertexAttribArray( program.attributes.normal );
  6742. _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  6743. }
  6744. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  6745. object.count = 0;
  6746. };
  6747. this.renderBufferDirect = function ( camera, lights, fog, material, geometryGroup, object ) {
  6748. if ( material.opacity === 0 ) return;
  6749. var program, attributes, linewidth, primitives, a, attribute;
  6750. program = setProgram( camera, lights, fog, material, object );
  6751. attributes = program.attributes;
  6752. var updateBuffers = false,
  6753. wireframeBit = material.wireframe ? 1 : 0,
  6754. geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  6755. if ( geometryGroupHash !== _currentGeometryGroupHash ) {
  6756. _currentGeometryGroupHash = geometryGroupHash;
  6757. updateBuffers = true;
  6758. }
  6759. // render mesh
  6760. if ( object instanceof THREE.Mesh ) {
  6761. var offsets = geometryGroup.offsets;
  6762. for ( var i = 0, il = offsets.length; i < il; ++ i ) {
  6763. if ( updateBuffers ) {
  6764. // vertices
  6765. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexPositionBuffer );
  6766. _gl.vertexAttribPointer( attributes.position, geometryGroup.vertexPositionBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 3 );
  6767. // normals
  6768. if ( attributes.normal >= 0 && geometryGroup.vertexNormalBuffer ) {
  6769. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexNormalBuffer );
  6770. _gl.vertexAttribPointer( attributes.normal, geometryGroup.vertexNormalBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 3 );
  6771. }
  6772. // uvs
  6773. if ( attributes.uv >= 0 && geometryGroup.vertexUvBuffer ) {
  6774. if ( geometryGroup.vertexUvBuffer ) {
  6775. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexUvBuffer );
  6776. _gl.vertexAttribPointer( attributes.uv, geometryGroup.vertexUvBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 2 );
  6777. _gl.enableVertexAttribArray( attributes.uv );
  6778. } else {
  6779. _gl.disableVertexAttribArray( attributes.uv );
  6780. }
  6781. }
  6782. // colors
  6783. if ( attributes.color >= 0 && geometryGroup.vertexColorBuffer ) {
  6784. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexColorBuffer );
  6785. _gl.vertexAttribPointer( attributes.color, geometryGroup.vertexColorBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 4 );
  6786. }
  6787. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.vertexIndexBuffer );
  6788. }
  6789. // render indexed triangles
  6790. _gl.drawElements( _gl.TRIANGLES, offsets[ i ].count, _gl.UNSIGNED_SHORT, offsets[ i ].start * 2 ); // 2 = Uint16
  6791. _this.info.render.calls ++;
  6792. _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
  6793. _this.info.render.faces += offsets[ i ].count / 3;
  6794. }
  6795. }
  6796. };
  6797. this.renderBuffer = function ( camera, lights, fog, material, geometryGroup, object ) {
  6798. if ( material.opacity === 0 ) return;
  6799. var program, attributes, linewidth, primitives, a, attribute, i, il;
  6800. program = setProgram( camera, lights, fog, material, object );
  6801. attributes = program.attributes;
  6802. var updateBuffers = false,
  6803. wireframeBit = material.wireframe ? 1 : 0,
  6804. geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  6805. if ( geometryGroupHash !== _currentGeometryGroupHash ) {
  6806. _currentGeometryGroupHash = geometryGroupHash;
  6807. updateBuffers = true;
  6808. }
  6809. // vertices
  6810. if ( !material.morphTargets && attributes.position >= 0 ) {
  6811. if ( updateBuffers ) {
  6812. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  6813. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6814. }
  6815. } else {
  6816. if ( object.morphTargetBase ) {
  6817. setupMorphTargets( material, geometryGroup, object );
  6818. }
  6819. }
  6820. if ( updateBuffers ) {
  6821. // custom attributes
  6822. // Use the per-geometryGroup custom attribute arrays which are setup in initMeshBuffers
  6823. if ( geometryGroup.__webglCustomAttributesList ) {
  6824. for ( i = 0, il = geometryGroup.__webglCustomAttributesList.length; i < il; i ++ ) {
  6825. attribute = geometryGroup.__webglCustomAttributesList[ i ];
  6826. if( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) {
  6827. _gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer );
  6828. _gl.vertexAttribPointer( attributes[ attribute.buffer.belongsToAttribute ], attribute.size, _gl.FLOAT, false, 0, 0 );
  6829. }
  6830. }
  6831. }
  6832. // colors
  6833. if ( attributes.color >= 0 ) {
  6834. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  6835. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  6836. }
  6837. // normals
  6838. if ( attributes.normal >= 0 ) {
  6839. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  6840. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  6841. }
  6842. // tangents
  6843. if ( attributes.tangent >= 0 ) {
  6844. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  6845. _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
  6846. }
  6847. // uvs
  6848. if ( attributes.uv >= 0 ) {
  6849. if ( geometryGroup.__webglUVBuffer ) {
  6850. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  6851. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  6852. _gl.enableVertexAttribArray( attributes.uv );
  6853. } else {
  6854. _gl.disableVertexAttribArray( attributes.uv );
  6855. }
  6856. }
  6857. if ( attributes.uv2 >= 0 ) {
  6858. if ( geometryGroup.__webglUV2Buffer ) {
  6859. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  6860. _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
  6861. _gl.enableVertexAttribArray( attributes.uv2 );
  6862. } else {
  6863. _gl.disableVertexAttribArray( attributes.uv2 );
  6864. }
  6865. }
  6866. if ( material.skinning &&
  6867. attributes.skinVertexA >= 0 && attributes.skinVertexB >= 0 &&
  6868. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  6869. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexABuffer );
  6870. _gl.vertexAttribPointer( attributes.skinVertexA, 4, _gl.FLOAT, false, 0, 0 );
  6871. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexBBuffer );
  6872. _gl.vertexAttribPointer( attributes.skinVertexB, 4, _gl.FLOAT, false, 0, 0 );
  6873. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  6874. _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
  6875. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  6876. _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
  6877. }
  6878. }
  6879. // render mesh
  6880. if ( object instanceof THREE.Mesh ) {
  6881. // wireframe
  6882. if ( material.wireframe ) {
  6883. setLineWidth( material.wireframeLinewidth );
  6884. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  6885. _gl.drawElements( _gl.LINES, geometryGroup.__webglLineCount, _gl.UNSIGNED_SHORT, 0 );
  6886. // triangles
  6887. } else {
  6888. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  6889. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, _gl.UNSIGNED_SHORT, 0 );
  6890. }
  6891. _this.info.render.calls ++;
  6892. _this.info.render.vertices += geometryGroup.__webglFaceCount;
  6893. _this.info.render.faces += geometryGroup.__webglFaceCount / 3;
  6894. // render lines
  6895. } else if ( object instanceof THREE.Line ) {
  6896. primitives = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  6897. setLineWidth( material.linewidth );
  6898. _gl.drawArrays( primitives, 0, geometryGroup.__webglLineCount );
  6899. _this.info.render.calls ++;
  6900. // render particles
  6901. } else if ( object instanceof THREE.ParticleSystem ) {
  6902. _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webglParticleCount );
  6903. _this.info.render.calls ++;
  6904. _this.info.render.points += geometryGroup.__webglParticleCount;
  6905. // render ribbon
  6906. } else if ( object instanceof THREE.Ribbon ) {
  6907. _gl.drawArrays( _gl.TRIANGLE_STRIP, 0, geometryGroup.__webglVertexCount );
  6908. _this.info.render.calls ++;
  6909. }
  6910. };
  6911. function setupMorphTargets ( material, geometryGroup, object ) {
  6912. // set base
  6913. var attributes = material.program.attributes;
  6914. if ( object.morphTargetBase !== - 1 ) {
  6915. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
  6916. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6917. } else if ( attributes.position >= 0 ) {
  6918. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  6919. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6920. }
  6921. if ( object.morphTargetForcedOrder.length ) {
  6922. // set forced order
  6923. var m = 0;
  6924. var order = object.morphTargetForcedOrder;
  6925. var influences = object.morphTargetInfluences;
  6926. while ( m < material.numSupportedMorphTargets && m < order.length ) {
  6927. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
  6928. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6929. if ( material.morphNormals ) {
  6930. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] );
  6931. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6932. }
  6933. object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ];
  6934. m ++;
  6935. }
  6936. } else {
  6937. // find most influencing
  6938. var used = [];
  6939. var candidateInfluence = - 1;
  6940. var candidate = 0;
  6941. var influences = object.morphTargetInfluences;
  6942. var i, il = influences.length;
  6943. var m = 0;
  6944. if ( object.morphTargetBase !== - 1 ) {
  6945. used[ object.morphTargetBase ] = true;
  6946. }
  6947. while ( m < material.numSupportedMorphTargets ) {
  6948. for ( i = 0; i < il; i ++ ) {
  6949. if ( !used[ i ] && influences[ i ] > candidateInfluence ) {
  6950. candidate = i;
  6951. candidateInfluence = influences[ candidate ];
  6952. }
  6953. }
  6954. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ candidate ] );
  6955. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6956. if ( material.morphNormals ) {
  6957. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ candidate ] );
  6958. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6959. }
  6960. object.__webglMorphTargetInfluences[ m ] = candidateInfluence;
  6961. used[ candidate ] = 1;
  6962. candidateInfluence = -1;
  6963. m ++;
  6964. }
  6965. }
  6966. // load updated influences uniform
  6967. if( material.program.uniforms.morphTargetInfluences !== null ) {
  6968. _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
  6969. }
  6970. };
  6971. function painterSort ( a, b ) {
  6972. return b.z - a.z;
  6973. };
  6974. // Rendering
  6975. this.render = function ( scene, camera, renderTarget, forceClear ) {
  6976. var i, il,
  6977. webglObject, object,
  6978. renderList,
  6979. lights = scene.__lights,
  6980. fog = scene.fog;
  6981. _currentMaterialId = -1;
  6982. // update scene graph
  6983. if ( camera.parent === undefined ) {
  6984. console.warn( 'DEPRECATED: Camera hasn\'t been added to a Scene. Adding it...' );
  6985. scene.add( camera );
  6986. }
  6987. if ( this.autoUpdateScene ) scene.updateMatrixWorld();
  6988. // update camera matrices and frustum
  6989. if ( ! camera._viewMatrixArray ) camera._viewMatrixArray = new Float32Array( 16 );
  6990. if ( ! camera._projectionMatrixArray ) camera._projectionMatrixArray = new Float32Array( 16 );
  6991. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  6992. camera.matrixWorldInverse.flattenToArray( camera._viewMatrixArray );
  6993. camera.projectionMatrix.flattenToArray( camera._projectionMatrixArray );
  6994. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  6995. _frustum.setFromMatrix( _projScreenMatrix );
  6996. // update WebGL objects
  6997. if ( this.autoUpdateObjects ) this.initWebGLObjects( scene );
  6998. // custom render plugins (pre pass)
  6999. renderPlugins( this.renderPluginsPre, scene, camera );
  7000. //
  7001. _this.info.render.calls = 0;
  7002. _this.info.render.vertices = 0;
  7003. _this.info.render.faces = 0;
  7004. _this.info.render.points = 0;
  7005. this.setRenderTarget( renderTarget );
  7006. if ( this.autoClear || forceClear ) {
  7007. this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
  7008. }
  7009. // set matrices for regular objects (frustum culled)
  7010. renderList = scene.__webglObjects;
  7011. for ( i = 0, il = renderList.length; i < il; i ++ ) {
  7012. webglObject = renderList[ i ];
  7013. object = webglObject.object;
  7014. webglObject.render = false;
  7015. if ( object.visible ) {
  7016. if ( ! ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem ) || ! ( object.frustumCulled ) || _frustum.contains( object ) ) {
  7017. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  7018. setupMatrices( object, camera );
  7019. unrollBufferMaterial( webglObject );
  7020. webglObject.render = true;
  7021. if ( this.sortObjects ) {
  7022. if ( object.renderDepth ) {
  7023. webglObject.z = object.renderDepth;
  7024. } else {
  7025. _vector3.copy( object.matrixWorld.getPosition() );
  7026. _projScreenMatrix.multiplyVector3( _vector3 );
  7027. webglObject.z = _vector3.z;
  7028. }
  7029. }
  7030. }
  7031. }
  7032. }
  7033. if ( this.sortObjects ) {
  7034. renderList.sort( painterSort );
  7035. }
  7036. // set matrices for immediate objects
  7037. renderList = scene.__webglObjectsImmediate;
  7038. for ( i = 0, il = renderList.length; i < il; i ++ ) {
  7039. webglObject = renderList[ i ];
  7040. object = webglObject.object;
  7041. if ( object.visible ) {
  7042. if( object.matrixAutoUpdate ) {
  7043. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  7044. }
  7045. setupMatrices( object, camera );
  7046. unrollImmediateBufferMaterial( webglObject );
  7047. }
  7048. }
  7049. if ( scene.overrideMaterial ) {
  7050. var material = scene.overrideMaterial;
  7051. this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  7052. this.setDepthTest( material.depthTest );
  7053. this.setDepthWrite( material.depthWrite );
  7054. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  7055. renderObjects( scene.__webglObjects, false, "", camera, lights, fog, true, material );
  7056. renderObjectsImmediate( scene.__webglObjectsImmediate, "", camera, lights, fog, false, material );
  7057. } else {
  7058. // opaque pass (front-to-back order)
  7059. this.setBlending( THREE.NormalBlending );
  7060. renderObjects( scene.__webglObjects, true, "opaque", camera, lights, fog, false );
  7061. renderObjectsImmediate( scene.__webglObjectsImmediate, "opaque", camera, lights, fog, false );
  7062. // transparent pass (back-to-front order)
  7063. renderObjects( scene.__webglObjects, false, "transparent", camera, lights, fog, true );
  7064. renderObjectsImmediate( scene.__webglObjectsImmediate, "transparent", camera, lights, fog, true );
  7065. }
  7066. // custom render plugins (post pass)
  7067. renderPlugins( this.renderPluginsPost, scene, camera );
  7068. // Generate mipmap if we're using any kind of mipmap filtering
  7069. if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
  7070. updateRenderTargetMipmap( renderTarget );
  7071. }
  7072. // Ensure depth buffer writing is enabled so it can be cleared on next render
  7073. this.setDepthTest( true );
  7074. this.setDepthWrite( true );
  7075. // _gl.finish();
  7076. };
  7077. function renderPlugins( plugins, scene, camera ) {
  7078. if ( ! plugins.length ) return;
  7079. for ( var i = 0, il = plugins.length; i < il; i ++ ) {
  7080. _currentProgram = null;
  7081. _currentCamera = null;
  7082. _oldBlending = -1;
  7083. _oldDepthTest = -1;
  7084. _oldDepthWrite = -1;
  7085. _currentGeometryGroupHash = -1;
  7086. _currentMaterialId = -1;
  7087. plugins[ i ].render( scene, camera, _currentWidth, _currentHeight );
  7088. _currentProgram = null;
  7089. _currentCamera = null;
  7090. _oldBlending = -1;
  7091. _oldDepthTest = -1;
  7092. _oldDepthWrite = -1;
  7093. _currentGeometryGroupHash = -1;
  7094. _currentMaterialId = -1;
  7095. }
  7096. };
  7097. function renderObjects ( renderList, reverse, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  7098. var webglObject, object, buffer, material, start, end, delta;
  7099. if ( reverse ) {
  7100. start = renderList.length - 1;
  7101. end = -1;
  7102. delta = -1;
  7103. } else {
  7104. start = 0;
  7105. end = renderList.length;
  7106. delta = 1;
  7107. }
  7108. for ( var i = start; i !== end; i += delta ) {
  7109. webglObject = renderList[ i ];
  7110. if ( webglObject.render ) {
  7111. object = webglObject.object;
  7112. buffer = webglObject.buffer;
  7113. if ( overrideMaterial ) {
  7114. material = overrideMaterial;
  7115. } else {
  7116. material = webglObject[ materialType ];
  7117. if ( ! material ) continue;
  7118. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  7119. _this.setDepthTest( material.depthTest );
  7120. _this.setDepthWrite( material.depthWrite );
  7121. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  7122. }
  7123. _this.setObjectFaces( object );
  7124. if ( buffer instanceof THREE.BufferGeometry ) {
  7125. _this.renderBufferDirect( camera, lights, fog, material, buffer, object );
  7126. } else {
  7127. _this.renderBuffer( camera, lights, fog, material, buffer, object );
  7128. }
  7129. }
  7130. }
  7131. };
  7132. function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  7133. var webglObject, object, material, program;
  7134. for ( var i = 0, il = renderList.length; i < il; i ++ ) {
  7135. webglObject = renderList[ i ];
  7136. object = webglObject.object;
  7137. if ( object.visible ) {
  7138. if ( overrideMaterial ) {
  7139. material = overrideMaterial;
  7140. } else {
  7141. material = webglObject[ materialType ];
  7142. if ( ! material ) continue;
  7143. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  7144. _this.setDepthTest( material.depthTest );
  7145. _this.setDepthWrite( material.depthWrite );
  7146. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  7147. }
  7148. _this.renderImmediateObject( camera, lights, fog, material, object );
  7149. }
  7150. }
  7151. };
  7152. this.renderImmediateObject = function ( camera, lights, fog, material, object ) {
  7153. var program = setProgram( camera, lights, fog, material, object );
  7154. _currentGeometryGroupHash = -1;
  7155. _this.setObjectFaces( object );
  7156. if ( object.immediateRenderCallback ) {
  7157. object.immediateRenderCallback( program, _gl, _frustum );
  7158. } else {
  7159. object.render( function( object ) { _this.renderBufferImmediate( object, program, material.shading ); } );
  7160. }
  7161. };
  7162. function unrollImmediateBufferMaterial ( globject ) {
  7163. var object = globject.object,
  7164. material = object.material;
  7165. if ( material.transparent ) {
  7166. globject.transparent = material;
  7167. globject.opaque = null;
  7168. } else {
  7169. globject.opaque = material;
  7170. globject.transparent = null;
  7171. }
  7172. };
  7173. function unrollBufferMaterial ( globject ) {
  7174. var object = globject.object,
  7175. buffer = globject.buffer,
  7176. material, materialIndex, meshMaterial;
  7177. meshMaterial = object.material;
  7178. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  7179. materialIndex = buffer.materialIndex;
  7180. if ( materialIndex >= 0 ) {
  7181. material = object.geometry.materials[ materialIndex ];
  7182. if ( material.transparent ) {
  7183. globject.transparent = material;
  7184. globject.opaque = null;
  7185. } else {
  7186. globject.opaque = material;
  7187. globject.transparent = null;
  7188. }
  7189. }
  7190. } else {
  7191. material = meshMaterial;
  7192. if ( material ) {
  7193. if ( material.transparent ) {
  7194. globject.transparent = material;
  7195. globject.opaque = null;
  7196. } else {
  7197. globject.opaque = material;
  7198. globject.transparent = null;
  7199. }
  7200. }
  7201. }
  7202. };
  7203. // Geometry splitting
  7204. function sortFacesByMaterial ( geometry ) {
  7205. var f, fl, face, materialIndex, vertices,
  7206. materialHash, groupHash,
  7207. hash_map = {};
  7208. var numMorphTargets = geometry.morphTargets.length;
  7209. var numMorphNormals = geometry.morphNormals.length;
  7210. geometry.geometryGroups = {};
  7211. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  7212. face = geometry.faces[ f ];
  7213. materialIndex = face.materialIndex;
  7214. materialHash = ( materialIndex !== undefined ) ? materialIndex : -1;
  7215. if ( hash_map[ materialHash ] === undefined ) {
  7216. hash_map[ materialHash ] = { 'hash': materialHash, 'counter': 0 };
  7217. }
  7218. groupHash = hash_map[ materialHash ].hash + '_' + hash_map[ materialHash ].counter;
  7219. if ( geometry.geometryGroups[ groupHash ] === undefined ) {
  7220. geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
  7221. }
  7222. vertices = face instanceof THREE.Face3 ? 3 : 4;
  7223. if ( geometry.geometryGroups[ groupHash ].vertices + vertices > 65535 ) {
  7224. hash_map[ materialHash ].counter += 1;
  7225. groupHash = hash_map[ materialHash ].hash + '_' + hash_map[ materialHash ].counter;
  7226. if ( geometry.geometryGroups[ groupHash ] === undefined ) {
  7227. geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
  7228. }
  7229. }
  7230. if ( face instanceof THREE.Face3 ) {
  7231. geometry.geometryGroups[ groupHash ].faces3.push( f );
  7232. } else {
  7233. geometry.geometryGroups[ groupHash ].faces4.push( f );
  7234. }
  7235. geometry.geometryGroups[ groupHash ].vertices += vertices;
  7236. }
  7237. geometry.geometryGroupsList = [];
  7238. for ( var g in geometry.geometryGroups ) {
  7239. geometry.geometryGroups[ g ].id = _geometryGroupCounter ++;
  7240. geometry.geometryGroupsList.push( geometry.geometryGroups[ g ] );
  7241. }
  7242. };
  7243. // Objects refresh
  7244. this.initWebGLObjects = function ( scene ) {
  7245. if ( !scene.__webglObjects ) {
  7246. scene.__webglObjects = [];
  7247. scene.__webglObjectsImmediate = [];
  7248. scene.__webglSprites = [];
  7249. scene.__webglFlares = [];
  7250. }
  7251. while ( scene.__objectsAdded.length ) {
  7252. addObject( scene.__objectsAdded[ 0 ], scene );
  7253. scene.__objectsAdded.splice( 0, 1 );
  7254. }
  7255. while ( scene.__objectsRemoved.length ) {
  7256. removeObject( scene.__objectsRemoved[ 0 ], scene );
  7257. scene.__objectsRemoved.splice( 0, 1 );
  7258. }
  7259. // update must be called after objects adding / removal
  7260. for ( var o = 0, ol = scene.__webglObjects.length; o < ol; o ++ ) {
  7261. updateObject( scene.__webglObjects[ o ].object );
  7262. }
  7263. };
  7264. // Objects adding
  7265. function addObject ( object, scene ) {
  7266. var g, geometry, geometryGroup;
  7267. if ( ! object.__webglInit ) {
  7268. object.__webglInit = true;
  7269. object._modelViewMatrix = new THREE.Matrix4();
  7270. object._normalMatrix = new THREE.Matrix3();
  7271. object._normalMatrixArray = new Float32Array( 9 );
  7272. object._modelViewMatrixArray = new Float32Array( 16 );
  7273. object._objectMatrixArray = new Float32Array( 16 );
  7274. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  7275. if ( object instanceof THREE.Mesh ) {
  7276. geometry = object.geometry;
  7277. if ( geometry instanceof THREE.Geometry ) {
  7278. if ( geometry.geometryGroups === undefined ) {
  7279. sortFacesByMaterial( geometry );
  7280. }
  7281. // create separate VBOs per geometry chunk
  7282. for ( g in geometry.geometryGroups ) {
  7283. geometryGroup = geometry.geometryGroups[ g ];
  7284. // initialise VBO on the first access
  7285. if ( ! geometryGroup.__webglVertexBuffer ) {
  7286. createMeshBuffers( geometryGroup );
  7287. initMeshBuffers( geometryGroup, object );
  7288. geometry.__dirtyVertices = true;
  7289. geometry.__dirtyMorphTargets = true;
  7290. geometry.__dirtyElements = true;
  7291. geometry.__dirtyUvs = true;
  7292. geometry.__dirtyNormals = true;
  7293. geometry.__dirtyTangents = true;
  7294. geometry.__dirtyColors = true;
  7295. }
  7296. }
  7297. }
  7298. } else if ( object instanceof THREE.Ribbon ) {
  7299. geometry = object.geometry;
  7300. if( ! geometry.__webglVertexBuffer ) {
  7301. createRibbonBuffers( geometry );
  7302. initRibbonBuffers( geometry );
  7303. geometry.__dirtyVertices = true;
  7304. geometry.__dirtyColors = true;
  7305. }
  7306. } else if ( object instanceof THREE.Line ) {
  7307. geometry = object.geometry;
  7308. if( ! geometry.__webglVertexBuffer ) {
  7309. createLineBuffers( geometry );
  7310. initLineBuffers( geometry, object );
  7311. geometry.__dirtyVertices = true;
  7312. geometry.__dirtyColors = true;
  7313. }
  7314. } else if ( object instanceof THREE.ParticleSystem ) {
  7315. geometry = object.geometry;
  7316. if ( ! geometry.__webglVertexBuffer ) {
  7317. createParticleBuffers( geometry );
  7318. initParticleBuffers( geometry, object );
  7319. geometry.__dirtyVertices = true;
  7320. geometry.__dirtyColors = true;
  7321. }
  7322. }
  7323. }
  7324. if ( ! object.__webglActive ) {
  7325. if ( object instanceof THREE.Mesh ) {
  7326. geometry = object.geometry;
  7327. if ( geometry instanceof THREE.BufferGeometry ) {
  7328. addBuffer( scene.__webglObjects, geometry, object );
  7329. } else {
  7330. for ( g in geometry.geometryGroups ) {
  7331. geometryGroup = geometry.geometryGroups[ g ];
  7332. addBuffer( scene.__webglObjects, geometryGroup, object );
  7333. }
  7334. }
  7335. } else if ( object instanceof THREE.Ribbon ||
  7336. object instanceof THREE.Line ||
  7337. object instanceof THREE.ParticleSystem ) {
  7338. geometry = object.geometry;
  7339. addBuffer( scene.__webglObjects, geometry, object );
  7340. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  7341. addBufferImmediate( scene.__webglObjectsImmediate, object );
  7342. } else if ( object instanceof THREE.Sprite ) {
  7343. scene.__webglSprites.push( object );
  7344. } else if ( object instanceof THREE.LensFlare ) {
  7345. scene.__webglFlares.push( object );
  7346. }
  7347. object.__webglActive = true;
  7348. }
  7349. };
  7350. function addBuffer ( objlist, buffer, object ) {
  7351. objlist.push(
  7352. {
  7353. buffer: buffer,
  7354. object: object,
  7355. opaque: null,
  7356. transparent: null
  7357. }
  7358. );
  7359. };
  7360. function addBufferImmediate ( objlist, object ) {
  7361. objlist.push(
  7362. {
  7363. object: object,
  7364. opaque: null,
  7365. transparent: null
  7366. }
  7367. );
  7368. };
  7369. // Objects updates
  7370. function updateObject ( object ) {
  7371. var geometry = object.geometry,
  7372. geometryGroup, customAttributesDirty, material;
  7373. if ( object instanceof THREE.Mesh ) {
  7374. if ( geometry instanceof THREE.BufferGeometry ) {
  7375. /*
  7376. if ( geometry.__dirtyVertices || geometry.__dirtyElements ||
  7377. geometry.__dirtyUvs || geometry.__dirtyNormals ||
  7378. geometry.__dirtyColors ) {
  7379. // TODO
  7380. // set buffers from typed arrays
  7381. }
  7382. */
  7383. geometry.__dirtyVertices = false;
  7384. geometry.__dirtyElements = false;
  7385. geometry.__dirtyUvs = false;
  7386. geometry.__dirtyNormals = false;
  7387. geometry.__dirtyColors = false;
  7388. } else {
  7389. // check all geometry groups
  7390. for( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) {
  7391. geometryGroup = geometry.geometryGroupsList[ i ];
  7392. material = getBufferMaterial( object, geometryGroup );
  7393. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  7394. if ( geometry.__dirtyVertices || geometry.__dirtyMorphTargets || geometry.__dirtyElements ||
  7395. geometry.__dirtyUvs || geometry.__dirtyNormals ||
  7396. geometry.__dirtyColors || geometry.__dirtyTangents || customAttributesDirty ) {
  7397. setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW, !geometry.dynamic, material );
  7398. }
  7399. }
  7400. geometry.__dirtyVertices = false;
  7401. geometry.__dirtyMorphTargets = false;
  7402. geometry.__dirtyElements = false;
  7403. geometry.__dirtyUvs = false;
  7404. geometry.__dirtyNormals = false;
  7405. geometry.__dirtyColors = false;
  7406. geometry.__dirtyTangents = false;
  7407. material.attributes && clearCustomAttributes( material );
  7408. }
  7409. } else if ( object instanceof THREE.Ribbon ) {
  7410. if ( geometry.__dirtyVertices || geometry.__dirtyColors ) {
  7411. setRibbonBuffers( geometry, _gl.DYNAMIC_DRAW );
  7412. }
  7413. geometry.__dirtyVertices = false;
  7414. geometry.__dirtyColors = false;
  7415. } else if ( object instanceof THREE.Line ) {
  7416. material = getBufferMaterial( object, geometryGroup );
  7417. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  7418. if ( geometry.__dirtyVertices || geometry.__dirtyColors || customAttributesDirty ) {
  7419. setLineBuffers( geometry, _gl.DYNAMIC_DRAW );
  7420. }
  7421. geometry.__dirtyVertices = false;
  7422. geometry.__dirtyColors = false;
  7423. material.attributes && clearCustomAttributes( material );
  7424. } else if ( object instanceof THREE.ParticleSystem ) {
  7425. material = getBufferMaterial( object, geometryGroup );
  7426. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  7427. if ( geometry.__dirtyVertices || geometry.__dirtyColors || object.sortParticles || customAttributesDirty ) {
  7428. setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object );
  7429. }
  7430. geometry.__dirtyVertices = false;
  7431. geometry.__dirtyColors = false;
  7432. material.attributes && clearCustomAttributes( material );
  7433. }
  7434. };
  7435. // Objects updates - custom attributes check
  7436. function areCustomAttributesDirty ( material ) {
  7437. for ( var a in material.attributes ) {
  7438. if ( material.attributes[ a ].needsUpdate ) return true;
  7439. }
  7440. return false;
  7441. };
  7442. function clearCustomAttributes ( material ) {
  7443. for ( var a in material.attributes ) {
  7444. material.attributes[ a ].needsUpdate = false;
  7445. }
  7446. };
  7447. // Objects removal
  7448. function removeObject ( object, scene ) {
  7449. if ( object instanceof THREE.Mesh ||
  7450. object instanceof THREE.ParticleSystem ||
  7451. object instanceof THREE.Ribbon ||
  7452. object instanceof THREE.Line ) {
  7453. removeInstances( scene.__webglObjects, object );
  7454. } else if ( object instanceof THREE.Sprite ) {
  7455. removeInstancesDirect( scene.__webglSprites, object );
  7456. } else if ( object instanceof THREE.LensFlare ) {
  7457. removeInstancesDirect( scene.__webglFlares, object );
  7458. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  7459. removeInstances( scene.__webglObjectsImmediate, object );
  7460. }
  7461. object.__webglActive = false;
  7462. };
  7463. function removeInstances ( objlist, object ) {
  7464. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  7465. if ( objlist[ o ].object === object ) {
  7466. objlist.splice( o, 1 );
  7467. }
  7468. }
  7469. };
  7470. function removeInstancesDirect ( objlist, object ) {
  7471. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  7472. if ( objlist[ o ] === object ) {
  7473. objlist.splice( o, 1 );
  7474. }
  7475. }
  7476. };
  7477. // Materials
  7478. this.initMaterial = function ( material, lights, fog, object ) {
  7479. var u, a, identifiers, i, parameters, maxLightCount, maxBones, maxShadows, shaderID;
  7480. if ( material instanceof THREE.MeshDepthMaterial ) {
  7481. shaderID = 'depth';
  7482. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  7483. shaderID = 'normal';
  7484. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  7485. shaderID = 'basic';
  7486. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  7487. shaderID = 'lambert';
  7488. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  7489. shaderID = 'phong';
  7490. } else if ( material instanceof THREE.LineBasicMaterial ) {
  7491. shaderID = 'basic';
  7492. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  7493. shaderID = 'particle_basic';
  7494. }
  7495. if ( shaderID ) {
  7496. setMaterialShaders( material, THREE.ShaderLib[ shaderID ] );
  7497. }
  7498. // heuristics to create shader parameters according to lights in the scene
  7499. // (not to blow over maxLights budget)
  7500. maxLightCount = allocateLights( lights );
  7501. maxShadows = allocateShadows( lights );
  7502. maxBones = allocateBones( object );
  7503. parameters = {
  7504. map: !!material.map, envMap: !!material.envMap, lightMap: !!material.lightMap,
  7505. vertexColors: material.vertexColors,
  7506. fog: fog, useFog: material.fog,
  7507. sizeAttenuation: material.sizeAttenuation,
  7508. skinning: material.skinning,
  7509. morphTargets: material.morphTargets,
  7510. morphNormals: material.morphNormals,
  7511. maxMorphTargets: this.maxMorphTargets,
  7512. maxMorphNormals: this.maxMorphNormals,
  7513. maxDirLights: maxLightCount.directional, maxPointLights: maxLightCount.point,
  7514. maxBones: maxBones,
  7515. shadowMapEnabled: this.shadowMapEnabled && object.receiveShadow,
  7516. shadowMapSoft: this.shadowMapSoft,
  7517. shadowMapDebug: this.shadowMapDebug,
  7518. shadowMapCascade: this.shadowMapCascade,
  7519. maxShadows: maxShadows,
  7520. alphaTest: material.alphaTest,
  7521. metal: material.metal,
  7522. perPixel: material.perPixel,
  7523. wrapAround: material.wrapAround,
  7524. doubleSided: object && object.doubleSided
  7525. };
  7526. material.program = buildProgram( shaderID, material.fragmentShader, material.vertexShader, material.uniforms, material.attributes, parameters );
  7527. var attributes = material.program.attributes;
  7528. if ( attributes.position >= 0 ) _gl.enableVertexAttribArray( attributes.position );
  7529. if ( attributes.color >= 0 ) _gl.enableVertexAttribArray( attributes.color );
  7530. if ( attributes.normal >= 0 ) _gl.enableVertexAttribArray( attributes.normal );
  7531. if ( attributes.tangent >= 0 ) _gl.enableVertexAttribArray( attributes.tangent );
  7532. if ( material.skinning &&
  7533. attributes.skinVertexA >=0 && attributes.skinVertexB >= 0 &&
  7534. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  7535. _gl.enableVertexAttribArray( attributes.skinVertexA );
  7536. _gl.enableVertexAttribArray( attributes.skinVertexB );
  7537. _gl.enableVertexAttribArray( attributes.skinIndex );
  7538. _gl.enableVertexAttribArray( attributes.skinWeight );
  7539. }
  7540. if ( material.attributes ) {
  7541. for ( a in material.attributes ) {
  7542. if( attributes[ a ] !== undefined && attributes[ a ] >= 0 ) _gl.enableVertexAttribArray( attributes[ a ] );
  7543. }
  7544. }
  7545. if ( material.morphTargets ) {
  7546. material.numSupportedMorphTargets = 0;
  7547. var id, base = "morphTarget";
  7548. for ( i = 0; i < this.maxMorphTargets; i ++ ) {
  7549. id = base + i;
  7550. if ( attributes[ id ] >= 0 ) {
  7551. _gl.enableVertexAttribArray( attributes[ id ] );
  7552. material.numSupportedMorphTargets ++;
  7553. }
  7554. }
  7555. }
  7556. if ( material.morphNormals ) {
  7557. material.numSupportedMorphNormals = 0;
  7558. var id, base = "morphNormal";
  7559. for ( i = 0; i < this.maxMorphNormals; i ++ ) {
  7560. id = base + i;
  7561. if ( attributes[ id ] >= 0 ) {
  7562. _gl.enableVertexAttribArray( attributes[ id ] );
  7563. material.numSupportedMorphNormals ++;
  7564. }
  7565. }
  7566. }
  7567. material.uniformsList = [];
  7568. for ( u in material.uniforms ) {
  7569. material.uniformsList.push( [ material.uniforms[ u ], u ] );
  7570. }
  7571. };
  7572. function setMaterialShaders( material, shaders ) {
  7573. material.uniforms = THREE.UniformsUtils.clone( shaders.uniforms );
  7574. material.vertexShader = shaders.vertexShader;
  7575. material.fragmentShader = shaders.fragmentShader;
  7576. };
  7577. function setProgram( camera, lights, fog, material, object ) {
  7578. if ( ! material.program || material.needsUpdate ) {
  7579. _this.initMaterial( material, lights, fog, object );
  7580. material.needsUpdate = false;
  7581. }
  7582. if ( material.morphTargets ) {
  7583. if ( ! object.__webglMorphTargetInfluences ) {
  7584. object.__webglMorphTargetInfluences = new Float32Array( _this.maxMorphTargets );
  7585. for ( var i = 0, il = _this.maxMorphTargets; i < il; i ++ ) {
  7586. object.__webglMorphTargetInfluences[ i ] = 0;
  7587. }
  7588. }
  7589. }
  7590. var refreshMaterial = false;
  7591. var program = material.program,
  7592. p_uniforms = program.uniforms,
  7593. m_uniforms = material.uniforms;
  7594. if ( program !== _currentProgram ) {
  7595. _gl.useProgram( program );
  7596. _currentProgram = program;
  7597. refreshMaterial = true;
  7598. }
  7599. if ( material.id !== _currentMaterialId ) {
  7600. _currentMaterialId = material.id;
  7601. refreshMaterial = true;
  7602. }
  7603. if ( refreshMaterial || camera !== _currentCamera ) {
  7604. _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera._projectionMatrixArray );
  7605. if ( camera !== _currentCamera ) _currentCamera = camera;
  7606. }
  7607. if ( refreshMaterial ) {
  7608. // refresh uniforms common to several materials
  7609. if ( fog && material.fog ) {
  7610. refreshUniformsFog( m_uniforms, fog );
  7611. }
  7612. if ( material instanceof THREE.MeshPhongMaterial ||
  7613. material instanceof THREE.MeshLambertMaterial ||
  7614. material.lights ) {
  7615. setupLights( program, lights );
  7616. refreshUniformsLights( m_uniforms, _lights );
  7617. }
  7618. if ( material instanceof THREE.MeshBasicMaterial ||
  7619. material instanceof THREE.MeshLambertMaterial ||
  7620. material instanceof THREE.MeshPhongMaterial ) {
  7621. refreshUniformsCommon( m_uniforms, material );
  7622. }
  7623. // refresh single material specific uniforms
  7624. if ( material instanceof THREE.LineBasicMaterial ) {
  7625. refreshUniformsLine( m_uniforms, material );
  7626. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  7627. refreshUniformsParticle( m_uniforms, material );
  7628. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  7629. refreshUniformsPhong( m_uniforms, material );
  7630. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  7631. refreshUniformsLambert( m_uniforms, material );
  7632. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  7633. m_uniforms.mNear.value = camera.near;
  7634. m_uniforms.mFar.value = camera.far;
  7635. m_uniforms.opacity.value = material.opacity;
  7636. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  7637. m_uniforms.opacity.value = material.opacity;
  7638. }
  7639. if ( object.receiveShadow && ! material._shadowPass ) {
  7640. refreshUniformsShadow( m_uniforms, lights );
  7641. }
  7642. // load common uniforms
  7643. loadUniformsGeneric( program, material.uniformsList );
  7644. // load material specific uniforms
  7645. // (shader material also gets them for the sake of genericity)
  7646. if ( material instanceof THREE.ShaderMaterial ||
  7647. material instanceof THREE.MeshPhongMaterial ||
  7648. material.envMap ) {
  7649. if ( p_uniforms.cameraPosition !== null ) {
  7650. var position = camera.matrixWorld.getPosition();
  7651. _gl.uniform3f( p_uniforms.cameraPosition, position.x, position.y, position.z );
  7652. }
  7653. }
  7654. if ( material instanceof THREE.MeshPhongMaterial ||
  7655. material instanceof THREE.MeshLambertMaterial ||
  7656. material instanceof THREE.ShaderMaterial ||
  7657. material.skinning ) {
  7658. if ( p_uniforms.viewMatrix !== null ) {
  7659. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera._viewMatrixArray );
  7660. }
  7661. }
  7662. if ( material.skinning ) {
  7663. _gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.boneMatrices );
  7664. }
  7665. }
  7666. loadUniformsMatrices( p_uniforms, object );
  7667. if ( material instanceof THREE.ShaderMaterial ||
  7668. material.envMap ||
  7669. material.skinning ||
  7670. object.receiveShadow ) {
  7671. if ( p_uniforms.objectMatrix !== null ) {
  7672. _gl.uniformMatrix4fv( p_uniforms.objectMatrix, false, object._objectMatrixArray );
  7673. }
  7674. }
  7675. return program;
  7676. };
  7677. // Uniforms (refresh uniforms objects)
  7678. function refreshUniformsCommon ( uniforms, material ) {
  7679. uniforms.opacity.value = material.opacity;
  7680. if ( _this.gammaInput ) {
  7681. uniforms.diffuse.value.copyGammaToLinear( material.color );
  7682. } else {
  7683. uniforms.diffuse.value = material.color;
  7684. }
  7685. uniforms.map.texture = material.map;
  7686. if ( material.map ) {
  7687. uniforms.offsetRepeat.value.set( material.map.offset.x, material.map.offset.y, material.map.repeat.x, material.map.repeat.y );
  7688. }
  7689. uniforms.lightMap.texture = material.lightMap;
  7690. uniforms.envMap.texture = material.envMap;
  7691. uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : -1;
  7692. if ( _this.gammaInput ) {
  7693. //uniforms.reflectivity.value = material.reflectivity * material.reflectivity;
  7694. uniforms.reflectivity.value = material.reflectivity;
  7695. } else {
  7696. uniforms.reflectivity.value = material.reflectivity;
  7697. }
  7698. uniforms.refractionRatio.value = material.refractionRatio;
  7699. uniforms.combine.value = material.combine;
  7700. uniforms.useRefract.value = material.envMap && material.envMap.mapping instanceof THREE.CubeRefractionMapping;
  7701. };
  7702. function refreshUniformsLine ( uniforms, material ) {
  7703. uniforms.diffuse.value = material.color;
  7704. uniforms.opacity.value = material.opacity;
  7705. };
  7706. function refreshUniformsParticle ( uniforms, material ) {
  7707. uniforms.psColor.value = material.color;
  7708. uniforms.opacity.value = material.opacity;
  7709. uniforms.size.value = material.size;
  7710. uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.
  7711. uniforms.map.texture = material.map;
  7712. };
  7713. function refreshUniformsFog ( uniforms, fog ) {
  7714. uniforms.fogColor.value = fog.color;
  7715. if ( fog instanceof THREE.Fog ) {
  7716. uniforms.fogNear.value = fog.near;
  7717. uniforms.fogFar.value = fog.far;
  7718. } else if ( fog instanceof THREE.FogExp2 ) {
  7719. uniforms.fogDensity.value = fog.density;
  7720. }
  7721. };
  7722. function refreshUniformsPhong ( uniforms, material ) {
  7723. uniforms.shininess.value = material.shininess;
  7724. if ( _this.gammaInput ) {
  7725. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  7726. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  7727. uniforms.specular.value.copyGammaToLinear( material.specular );
  7728. } else {
  7729. uniforms.ambient.value = material.ambient;
  7730. uniforms.emissive.value = material.emissive;
  7731. uniforms.specular.value = material.specular;
  7732. }
  7733. if ( material.wrapAround ) {
  7734. uniforms.wrapRGB.value.copy( material.wrapRGB );
  7735. }
  7736. };
  7737. function refreshUniformsLambert ( uniforms, material ) {
  7738. if ( _this.gammaInput ) {
  7739. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  7740. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  7741. } else {
  7742. uniforms.ambient.value = material.ambient;
  7743. uniforms.emissive.value = material.emissive;
  7744. }
  7745. if ( material.wrapAround ) {
  7746. uniforms.wrapRGB.value.copy( material.wrapRGB );
  7747. }
  7748. };
  7749. function refreshUniformsLights ( uniforms, lights ) {
  7750. uniforms.ambientLightColor.value = lights.ambient;
  7751. uniforms.directionalLightColor.value = lights.directional.colors;
  7752. uniforms.directionalLightDirection.value = lights.directional.positions;
  7753. uniforms.pointLightColor.value = lights.point.colors;
  7754. uniforms.pointLightPosition.value = lights.point.positions;
  7755. uniforms.pointLightDistance.value = lights.point.distances;
  7756. };
  7757. function refreshUniformsShadow ( uniforms, lights ) {
  7758. if ( uniforms.shadowMatrix ) {
  7759. var j = 0;
  7760. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  7761. var light = lights[ i ];
  7762. if ( ! light.castShadow ) continue;
  7763. if ( light instanceof THREE.SpotLight || ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) ) {
  7764. uniforms.shadowMap.texture[ j ] = light.shadowMap;
  7765. uniforms.shadowMapSize.value[ j ] = light.shadowMapSize;
  7766. uniforms.shadowMatrix.value[ j ] = light.shadowMatrix;
  7767. uniforms.shadowDarkness.value[ j ] = light.shadowDarkness;
  7768. uniforms.shadowBias.value[ j ] = light.shadowBias;
  7769. j ++;
  7770. }
  7771. }
  7772. }
  7773. };
  7774. // Uniforms (load to GPU)
  7775. function loadUniformsMatrices ( uniforms, object ) {
  7776. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrixArray );
  7777. if ( uniforms.normalMatrix ) {
  7778. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrixArray );
  7779. }
  7780. };
  7781. function loadUniformsGeneric ( program, uniforms ) {
  7782. var uniform, value, type, location, texture, i, il, j, jl, offset;
  7783. for( j = 0, jl = uniforms.length; j < jl; j ++ ) {
  7784. location = program.uniforms[ uniforms[ j ][ 1 ] ];
  7785. if ( !location ) continue;
  7786. uniform = uniforms[ j ][ 0 ];
  7787. type = uniform.type;
  7788. value = uniform.value;
  7789. // single integer
  7790. if( type === "i" ) {
  7791. _gl.uniform1i( location, value );
  7792. // single float
  7793. } else if( type === "f" ) {
  7794. _gl.uniform1f( location, value );
  7795. // single THREE.Vector2
  7796. } else if( type === "v2" ) {
  7797. _gl.uniform2f( location, value.x, value.y );
  7798. // single THREE.Vector3
  7799. } else if( type === "v3" ) {
  7800. _gl.uniform3f( location, value.x, value.y, value.z );
  7801. // single THREE.Vector4
  7802. } else if( type === "v4" ) {
  7803. _gl.uniform4f( location, value.x, value.y, value.z, value.w );
  7804. // single THREE.Color
  7805. } else if( type === "c" ) {
  7806. _gl.uniform3f( location, value.r, value.g, value.b );
  7807. // flat array of floats (JS or typed array)
  7808. } else if( type === "fv1" ) {
  7809. _gl.uniform1fv( location, value );
  7810. // flat array of floats with 3 x N size (JS or typed array)
  7811. } else if( type === "fv" ) {
  7812. _gl.uniform3fv( location, value );
  7813. // array of THREE.Vector2
  7814. } else if( type === "v2v" ) {
  7815. if ( ! uniform._array ) {
  7816. uniform._array = new Float32Array( 2 * value.length );
  7817. }
  7818. for ( i = 0, il = value.length; i < il; i ++ ) {
  7819. offset = i * 2;
  7820. uniform._array[ offset ] = value[ i ].x;
  7821. uniform._array[ offset + 1 ] = value[ i ].y;
  7822. }
  7823. _gl.uniform2fv( location, uniform._array );
  7824. // array of THREE.Vector3
  7825. } else if( type === "v3v" ) {
  7826. if ( ! uniform._array ) {
  7827. uniform._array = new Float32Array( 3 * value.length );
  7828. }
  7829. for ( i = 0, il = value.length; i < il; i ++ ) {
  7830. offset = i * 3;
  7831. uniform._array[ offset ] = value[ i ].x;
  7832. uniform._array[ offset + 1 ] = value[ i ].y;
  7833. uniform._array[ offset + 2 ] = value[ i ].z;
  7834. }
  7835. _gl.uniform3fv( location, uniform._array );
  7836. // array of THREE.Vector4
  7837. } else if( type == "v4v" ) {
  7838. if ( ! uniform._array ) {
  7839. uniform._array = new Float32Array( 4 * value.length );
  7840. }
  7841. for ( i = 0, il = value.length; i < il; i ++ ) {
  7842. offset = i * 4;
  7843. uniform._array[ offset ] = value[ i ].x;
  7844. uniform._array[ offset + 1 ] = value[ i ].y;
  7845. uniform._array[ offset + 2 ] = value[ i ].z;
  7846. uniform._array[ offset + 3 ] = value[ i ].w;
  7847. }
  7848. _gl.uniform4fv( location, uniform._array );
  7849. // single THREE.Matrix4
  7850. } else if( type === "m4" ) {
  7851. if ( ! uniform._array ) {
  7852. uniform._array = new Float32Array( 16 );
  7853. }
  7854. value.flattenToArray( uniform._array );
  7855. _gl.uniformMatrix4fv( location, false, uniform._array );
  7856. // array of THREE.Matrix4
  7857. } else if( type === "m4v" ) {
  7858. if ( ! uniform._array ) {
  7859. uniform._array = new Float32Array( 16 * value.length );
  7860. }
  7861. for ( i = 0, il = value.length; i < il; i ++ ) {
  7862. value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
  7863. }
  7864. _gl.uniformMatrix4fv( location, false, uniform._array );
  7865. // single THREE.Texture (2d or cube)
  7866. } else if( type === "t" ) {
  7867. _gl.uniform1i( location, value );
  7868. texture = uniform.texture;
  7869. if ( !texture ) continue;
  7870. if ( texture.image instanceof Array && texture.image.length === 6 ) {
  7871. setCubeTexture( texture, value );
  7872. } else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
  7873. setCubeTextureDynamic( texture, value );
  7874. } else {
  7875. _this.setTexture( texture, value );
  7876. }
  7877. // array of THREE.Texture (2d)
  7878. } else if( type === "tv" ) {
  7879. if ( ! uniform._array ) {
  7880. uniform._array = [];
  7881. for( i = 0, il = uniform.texture.length; i < il; i ++ ) {
  7882. uniform._array[ i ] = value + i;
  7883. }
  7884. }
  7885. _gl.uniform1iv( location, uniform._array );
  7886. for( i = 0, il = uniform.texture.length; i < il; i ++ ) {
  7887. texture = uniform.texture[ i ];
  7888. if ( !texture ) continue;
  7889. _this.setTexture( texture, uniform._array[ i ] );
  7890. }
  7891. }
  7892. }
  7893. };
  7894. function setupMatrices ( object, camera ) {
  7895. object._modelViewMatrix.multiplyToArray( camera.matrixWorldInverse, object.matrixWorld, object._modelViewMatrixArray );
  7896. object._normalMatrix.getInverse( object._modelViewMatrix );
  7897. object._normalMatrix.transposeIntoArray( object._normalMatrixArray );
  7898. };
  7899. function setupLights ( program, lights ) {
  7900. var l, ll, light, n,
  7901. r = 0, g = 0, b = 0,
  7902. color, position, intensity, distance,
  7903. zlights = _lights,
  7904. dcolors = zlights.directional.colors,
  7905. dpositions = zlights.directional.positions,
  7906. pcolors = zlights.point.colors,
  7907. ppositions = zlights.point.positions,
  7908. pdistances = zlights.point.distances,
  7909. dlength = 0,
  7910. plength = 0,
  7911. doffset = 0,
  7912. poffset = 0;
  7913. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  7914. light = lights[ l ];
  7915. if ( light.onlyShadow ) continue;
  7916. color = light.color;
  7917. intensity = light.intensity;
  7918. distance = light.distance;
  7919. if ( light instanceof THREE.AmbientLight ) {
  7920. if ( _this.gammaInput ) {
  7921. r += color.r * color.r;
  7922. g += color.g * color.g;
  7923. b += color.b * color.b;
  7924. } else {
  7925. r += color.r;
  7926. g += color.g;
  7927. b += color.b;
  7928. }
  7929. } else if ( light instanceof THREE.DirectionalLight ) {
  7930. doffset = dlength * 3;
  7931. if ( _this.gammaInput ) {
  7932. dcolors[ doffset ] = color.r * color.r * intensity * intensity;
  7933. dcolors[ doffset + 1 ] = color.g * color.g * intensity * intensity;
  7934. dcolors[ doffset + 2 ] = color.b * color.b * intensity * intensity;
  7935. } else {
  7936. dcolors[ doffset ] = color.r * intensity;
  7937. dcolors[ doffset + 1 ] = color.g * intensity;
  7938. dcolors[ doffset + 2 ] = color.b * intensity;
  7939. }
  7940. _direction.copy( light.matrixWorld.getPosition() );
  7941. _direction.subSelf( light.target.matrixWorld.getPosition() );
  7942. _direction.normalize();
  7943. dpositions[ doffset ] = _direction.x;
  7944. dpositions[ doffset + 1 ] = _direction.y;
  7945. dpositions[ doffset + 2 ] = _direction.z;
  7946. dlength += 1;
  7947. } else if( light instanceof THREE.PointLight || light instanceof THREE.SpotLight ) {
  7948. poffset = plength * 3;
  7949. if ( _this.gammaInput ) {
  7950. pcolors[ poffset ] = color.r * color.r * intensity * intensity;
  7951. pcolors[ poffset + 1 ] = color.g * color.g * intensity * intensity;
  7952. pcolors[ poffset + 2 ] = color.b * color.b * intensity * intensity;
  7953. } else {
  7954. pcolors[ poffset ] = color.r * intensity;
  7955. pcolors[ poffset + 1 ] = color.g * intensity;
  7956. pcolors[ poffset + 2 ] = color.b * intensity;
  7957. }
  7958. position = light.matrixWorld.getPosition();
  7959. ppositions[ poffset ] = position.x;
  7960. ppositions[ poffset + 1 ] = position.y;
  7961. ppositions[ poffset + 2 ] = position.z;
  7962. pdistances[ plength ] = distance;
  7963. plength += 1;
  7964. }
  7965. }
  7966. // null eventual remains from removed lights
  7967. // (this is to avoid if in shader)
  7968. for ( l = dlength * 3, ll = dcolors.length; l < ll; l ++ ) dcolors[ l ] = 0.0;
  7969. for ( l = plength * 3, ll = pcolors.length; l < ll; l ++ ) pcolors[ l ] = 0.0;
  7970. zlights.point.length = plength;
  7971. zlights.directional.length = dlength;
  7972. zlights.ambient[ 0 ] = r;
  7973. zlights.ambient[ 1 ] = g;
  7974. zlights.ambient[ 2 ] = b;
  7975. };
  7976. // GL state setting
  7977. this.setFaceCulling = function ( cullFace, frontFace ) {
  7978. if ( cullFace ) {
  7979. if ( !frontFace || frontFace === "ccw" ) {
  7980. _gl.frontFace( _gl.CCW );
  7981. } else {
  7982. _gl.frontFace( _gl.CW );
  7983. }
  7984. if( cullFace === "back" ) {
  7985. _gl.cullFace( _gl.BACK );
  7986. } else if( cullFace === "front" ) {
  7987. _gl.cullFace( _gl.FRONT );
  7988. } else {
  7989. _gl.cullFace( _gl.FRONT_AND_BACK );
  7990. }
  7991. _gl.enable( _gl.CULL_FACE );
  7992. } else {
  7993. _gl.disable( _gl.CULL_FACE );
  7994. }
  7995. };
  7996. this.setObjectFaces = function ( object ) {
  7997. if ( _oldDoubleSided !== object.doubleSided ) {
  7998. if( object.doubleSided ) {
  7999. _gl.disable( _gl.CULL_FACE );
  8000. } else {
  8001. _gl.enable( _gl.CULL_FACE );
  8002. }
  8003. _oldDoubleSided = object.doubleSided;
  8004. }
  8005. if ( _oldFlipSided !== object.flipSided ) {
  8006. if( object.flipSided ) {
  8007. _gl.frontFace( _gl.CW );
  8008. } else {
  8009. _gl.frontFace( _gl.CCW );
  8010. }
  8011. _oldFlipSided = object.flipSided;
  8012. }
  8013. };
  8014. this.setDepthTest = function ( depthTest ) {
  8015. if ( _oldDepthTest !== depthTest ) {
  8016. if ( depthTest ) {
  8017. _gl.enable( _gl.DEPTH_TEST );
  8018. } else {
  8019. _gl.disable( _gl.DEPTH_TEST );
  8020. }
  8021. _oldDepthTest = depthTest;
  8022. }
  8023. };
  8024. this.setDepthWrite = function ( depthWrite ) {
  8025. if ( _oldDepthWrite !== depthWrite ) {
  8026. _gl.depthMask( depthWrite );
  8027. _oldDepthWrite = depthWrite;
  8028. }
  8029. };
  8030. function setLineWidth ( width ) {
  8031. if ( width !== _oldLineWidth ) {
  8032. _gl.lineWidth( width );
  8033. _oldLineWidth = width;
  8034. }
  8035. };
  8036. function setPolygonOffset ( polygonoffset, factor, units ) {
  8037. if ( _oldPolygonOffset !== polygonoffset ) {
  8038. if ( polygonoffset ) {
  8039. _gl.enable( _gl.POLYGON_OFFSET_FILL );
  8040. } else {
  8041. _gl.disable( _gl.POLYGON_OFFSET_FILL );
  8042. }
  8043. _oldPolygonOffset = polygonoffset;
  8044. }
  8045. if ( polygonoffset && ( _oldPolygonOffsetFactor !== factor || _oldPolygonOffsetUnits !== units ) ) {
  8046. _gl.polygonOffset( factor, units );
  8047. _oldPolygonOffsetFactor = factor;
  8048. _oldPolygonOffsetUnits = units;
  8049. }
  8050. };
  8051. this.setBlending = function ( blending, blendEquation, blendSrc, blendDst ) {
  8052. if ( blending !== _oldBlending ) {
  8053. switch ( blending ) {
  8054. case THREE.NoBlending:
  8055. _gl.disable( _gl.BLEND );
  8056. break;
  8057. case THREE.AdditiveBlending:
  8058. _gl.enable( _gl.BLEND );
  8059. _gl.blendEquation( _gl.FUNC_ADD );
  8060. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE );
  8061. break;
  8062. case THREE.SubtractiveBlending:
  8063. // TODO: Find blendFuncSeparate() combination
  8064. _gl.enable( _gl.BLEND );
  8065. _gl.blendEquation( _gl.FUNC_ADD );
  8066. _gl.blendFunc( _gl.ZERO, _gl.ONE_MINUS_SRC_COLOR );
  8067. break;
  8068. case THREE.MultiplyBlending:
  8069. // TODO: Find blendFuncSeparate() combination
  8070. _gl.enable( _gl.BLEND );
  8071. _gl.blendEquation( _gl.FUNC_ADD );
  8072. _gl.blendFunc( _gl.ZERO, _gl.SRC_COLOR );
  8073. break;
  8074. case THREE.CustomBlending:
  8075. _gl.enable( _gl.BLEND );
  8076. break;
  8077. default:
  8078. _gl.enable( _gl.BLEND );
  8079. _gl.blendEquationSeparate( _gl.FUNC_ADD, _gl.FUNC_ADD );
  8080. _gl.blendFuncSeparate( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA, _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  8081. break;
  8082. }
  8083. _oldBlending = blending;
  8084. }
  8085. if ( blending === THREE.CustomBlending ) {
  8086. if ( blendEquation !== _oldBlendEquation ) {
  8087. _gl.blendEquation( paramThreeToGL( blendEquation ) );
  8088. _oldBlendEquation = blendEquation;
  8089. }
  8090. if ( blendSrc !== _oldBlendSrc || blendDst !== _oldBlendDst ) {
  8091. _gl.blendFunc( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ) );
  8092. _oldBlendSrc = blendSrc;
  8093. _oldBlendDst = blendDst;
  8094. }
  8095. } else {
  8096. _oldBlendEquation = null;
  8097. _oldBlendSrc = null;
  8098. _oldBlendDst = null;
  8099. }
  8100. };
  8101. // Shaders
  8102. function buildProgram ( shaderID, fragmentShader, vertexShader, uniforms, attributes, parameters ) {
  8103. var p, pl, program, code;
  8104. var chunks = [];
  8105. // Generate code
  8106. if ( shaderID ) {
  8107. chunks.push( shaderID );
  8108. } else {
  8109. chunks.push( fragmentShader );
  8110. chunks.push( vertexShader );
  8111. }
  8112. for ( p in parameters ) {
  8113. chunks.push( p );
  8114. chunks.push( parameters[ p ] );
  8115. }
  8116. code = chunks.join();
  8117. // Check if code has been already compiled
  8118. for ( p = 0, pl = _programs.length; p < pl; p ++ ) {
  8119. if ( _programs[ p ].code === code ) {
  8120. // console.log( "Code already compiled." /*: \n\n" + code*/ );
  8121. return _programs[ p ].program;
  8122. }
  8123. }
  8124. //console.log( "building new program " );
  8125. //
  8126. program = _gl.createProgram();
  8127. var prefix_vertex = [
  8128. "precision " + _precision + " float;",
  8129. ( _maxVertexTextures > 0 ) ? "#define VERTEX_TEXTURES" : "",
  8130. _this.gammaInput ? "#define GAMMA_INPUT" : "",
  8131. _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
  8132. _this.physicallyBasedShading ? "#define PHYSICALLY_BASED_SHADING" : "",
  8133. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  8134. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  8135. "#define MAX_SHADOWS " + parameters.maxShadows,
  8136. "#define MAX_BONES " + parameters.maxBones,
  8137. parameters.map ? "#define USE_MAP" : "",
  8138. parameters.envMap ? "#define USE_ENVMAP" : "",
  8139. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  8140. parameters.vertexColors ? "#define USE_COLOR" : "",
  8141. parameters.skinning ? "#define USE_SKINNING" : "",
  8142. parameters.morphTargets ? "#define USE_MORPHTARGETS" : "",
  8143. parameters.morphNormals ? "#define USE_MORPHNORMALS" : "",
  8144. parameters.perPixel ? "#define PHONG_PER_PIXEL" : "",
  8145. parameters.wrapAround ? "#define WRAP_AROUND" : "",
  8146. parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
  8147. parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
  8148. parameters.shadowMapSoft ? "#define SHADOWMAP_SOFT" : "",
  8149. parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
  8150. parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
  8151. parameters.sizeAttenuation ? "#define USE_SIZEATTENUATION" : "",
  8152. "uniform mat4 objectMatrix;",
  8153. "uniform mat4 modelViewMatrix;",
  8154. "uniform mat4 projectionMatrix;",
  8155. "uniform mat4 viewMatrix;",
  8156. "uniform mat3 normalMatrix;",
  8157. "uniform vec3 cameraPosition;",
  8158. "attribute vec3 position;",
  8159. "attribute vec3 normal;",
  8160. "attribute vec2 uv;",
  8161. "attribute vec2 uv2;",
  8162. "#ifdef USE_COLOR",
  8163. "attribute vec3 color;",
  8164. "#endif",
  8165. "#ifdef USE_MORPHTARGETS",
  8166. "attribute vec3 morphTarget0;",
  8167. "attribute vec3 morphTarget1;",
  8168. "attribute vec3 morphTarget2;",
  8169. "attribute vec3 morphTarget3;",
  8170. "#ifdef USE_MORPHNORMALS",
  8171. "attribute vec3 morphNormal0;",
  8172. "attribute vec3 morphNormal1;",
  8173. "attribute vec3 morphNormal2;",
  8174. "attribute vec3 morphNormal3;",
  8175. "#else",
  8176. "attribute vec3 morphTarget4;",
  8177. "attribute vec3 morphTarget5;",
  8178. "attribute vec3 morphTarget6;",
  8179. "attribute vec3 morphTarget7;",
  8180. "#endif",
  8181. "#endif",
  8182. "#ifdef USE_SKINNING",
  8183. "attribute vec4 skinVertexA;",
  8184. "attribute vec4 skinVertexB;",
  8185. "attribute vec4 skinIndex;",
  8186. "attribute vec4 skinWeight;",
  8187. "#endif",
  8188. ""
  8189. ].join("\n");
  8190. var prefix_fragment = [
  8191. "precision " + _precision + " float;",
  8192. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  8193. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  8194. "#define MAX_SHADOWS " + parameters.maxShadows,
  8195. parameters.alphaTest ? "#define ALPHATEST " + parameters.alphaTest: "",
  8196. _this.gammaInput ? "#define GAMMA_INPUT" : "",
  8197. _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
  8198. _this.physicallyBasedShading ? "#define PHYSICALLY_BASED_SHADING" : "",
  8199. ( parameters.useFog && parameters.fog ) ? "#define USE_FOG" : "",
  8200. ( parameters.useFog && parameters.fog instanceof THREE.FogExp2 ) ? "#define FOG_EXP2" : "",
  8201. parameters.map ? "#define USE_MAP" : "",
  8202. parameters.envMap ? "#define USE_ENVMAP" : "",
  8203. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  8204. parameters.vertexColors ? "#define USE_COLOR" : "",
  8205. parameters.metal ? "#define METAL" : "",
  8206. parameters.perPixel ? "#define PHONG_PER_PIXEL" : "",
  8207. parameters.wrapAround ? "#define WRAP_AROUND" : "",
  8208. parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
  8209. parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
  8210. parameters.shadowMapSoft ? "#define SHADOWMAP_SOFT" : "",
  8211. parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
  8212. parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
  8213. "uniform mat4 viewMatrix;",
  8214. "uniform vec3 cameraPosition;",
  8215. ""
  8216. ].join("\n");
  8217. _gl.attachShader( program, getShader( "fragment", prefix_fragment + fragmentShader ) );
  8218. _gl.attachShader( program, getShader( "vertex", prefix_vertex + vertexShader ) );
  8219. _gl.linkProgram( program );
  8220. if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) {
  8221. console.error( "Could not initialise shader\n" + "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + ", gl error [" + _gl.getError() + "]" );
  8222. }
  8223. //console.log( prefix_fragment + fragmentShader );
  8224. //console.log( prefix_vertex + vertexShader );
  8225. program.uniforms = {};
  8226. program.attributes = {};
  8227. var identifiers, u, a, i;
  8228. // cache uniform locations
  8229. identifiers = [
  8230. 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition',
  8231. 'boneGlobalMatrices', 'morphTargetInfluences'
  8232. ];
  8233. for ( u in uniforms ) {
  8234. identifiers.push( u );
  8235. }
  8236. cacheUniformLocations( program, identifiers );
  8237. // cache attributes locations
  8238. identifiers = [
  8239. "position", "normal", "uv", "uv2", "tangent", "color",
  8240. "skinVertexA", "skinVertexB", "skinIndex", "skinWeight"
  8241. ];
  8242. for ( i = 0; i < parameters.maxMorphTargets; i ++ ) {
  8243. identifiers.push( "morphTarget" + i );
  8244. }
  8245. for ( i = 0; i < parameters.maxMorphNormals; i ++ ) {
  8246. identifiers.push( "morphNormal" + i );
  8247. }
  8248. for ( a in attributes ) {
  8249. identifiers.push( a );
  8250. }
  8251. cacheAttributeLocations( program, identifiers );
  8252. program.id = _programs.length;
  8253. _programs.push( { program: program, code: code } );
  8254. _this.info.memory.programs = _programs.length;
  8255. return program;
  8256. };
  8257. // Shader parameters cache
  8258. function cacheUniformLocations ( program, identifiers ) {
  8259. var i, l, id;
  8260. for( i = 0, l = identifiers.length; i < l; i ++ ) {
  8261. id = identifiers[ i ];
  8262. program.uniforms[ id ] = _gl.getUniformLocation( program, id );
  8263. }
  8264. };
  8265. function cacheAttributeLocations ( program, identifiers ) {
  8266. var i, l, id;
  8267. for( i = 0, l = identifiers.length; i < l; i ++ ) {
  8268. id = identifiers[ i ];
  8269. program.attributes[ id ] = _gl.getAttribLocation( program, id );
  8270. }
  8271. };
  8272. function getShader ( type, string ) {
  8273. var shader;
  8274. if ( type === "fragment" ) {
  8275. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  8276. } else if ( type === "vertex" ) {
  8277. shader = _gl.createShader( _gl.VERTEX_SHADER );
  8278. }
  8279. _gl.shaderSource( shader, string );
  8280. _gl.compileShader( shader );
  8281. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  8282. console.error( _gl.getShaderInfoLog( shader ) );
  8283. console.error( string );
  8284. return null;
  8285. }
  8286. return shader;
  8287. };
  8288. // Textures
  8289. function isPowerOfTwo ( value ) {
  8290. return ( value & ( value - 1 ) ) === 0;
  8291. };
  8292. function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) {
  8293. if ( isImagePowerOfTwo ) {
  8294. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
  8295. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
  8296. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
  8297. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
  8298. } else {
  8299. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  8300. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  8301. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
  8302. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
  8303. }
  8304. };
  8305. this.setTexture = function ( texture, slot ) {
  8306. if ( texture.needsUpdate ) {
  8307. if ( ! texture.__webglInit ) {
  8308. texture.__webglInit = true;
  8309. texture.__webglTexture = _gl.createTexture();
  8310. _this.info.memory.textures ++;
  8311. }
  8312. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8313. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  8314. _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  8315. var image = texture.image,
  8316. isImagePowerOfTwo = isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ),
  8317. glFormat = paramThreeToGL( texture.format ),
  8318. glType = paramThreeToGL( texture.type );
  8319. setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo );
  8320. if ( texture instanceof THREE.DataTexture ) {
  8321. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data );
  8322. } else {
  8323. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image );
  8324. }
  8325. if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  8326. texture.needsUpdate = false;
  8327. if ( texture.onUpdate ) texture.onUpdate();
  8328. } else {
  8329. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8330. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  8331. }
  8332. };
  8333. function clampToMaxSize ( image, maxSize ) {
  8334. if ( image.width <= maxSize && image.height <= maxSize ) {
  8335. return image;
  8336. }
  8337. // Warning: Scaling through the canvas will only work with images that use
  8338. // premultiplied alpha.
  8339. var maxDimension = Math.max( image.width, image.height );
  8340. var newWidth = Math.floor( image.width * maxSize / maxDimension );
  8341. var newHeight = Math.floor( image.height * maxSize / maxDimension );
  8342. var canvas = document.createElement( 'canvas' );
  8343. canvas.width = newWidth;
  8344. canvas.height = newHeight;
  8345. var ctx = canvas.getContext( "2d" );
  8346. ctx.drawImage( image, 0, 0, image.width, image.height, 0, 0, newWidth, newHeight );
  8347. return canvas;
  8348. }
  8349. function setCubeTexture ( texture, slot ) {
  8350. if ( texture.image.length === 6 ) {
  8351. if ( texture.needsUpdate ) {
  8352. if ( ! texture.image.__webglTextureCube ) {
  8353. texture.image.__webglTextureCube = _gl.createTexture();
  8354. }
  8355. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8356. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  8357. var cubeImage = [];
  8358. for ( var i = 0; i < 6; i ++ ) {
  8359. if ( _this.autoScaleCubemaps ) {
  8360. cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize );
  8361. } else {
  8362. cubeImage[ i ] = texture.image[ i ];
  8363. }
  8364. }
  8365. var image = cubeImage[ 0 ],
  8366. isImagePowerOfTwo = isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ),
  8367. glFormat = paramThreeToGL( texture.format ),
  8368. glType = paramThreeToGL( texture.type );
  8369. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );
  8370. for ( var i = 0; i < 6; i ++ ) {
  8371. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
  8372. }
  8373. if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  8374. texture.needsUpdate = false;
  8375. if ( texture.onUpdate ) texture.onUpdate();
  8376. } else {
  8377. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8378. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  8379. }
  8380. }
  8381. };
  8382. function setCubeTextureDynamic ( texture, slot ) {
  8383. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8384. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.__webglTexture );
  8385. };
  8386. // Render targets
  8387. function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {
  8388. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  8389. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, renderTarget.__webglTexture, 0 );
  8390. };
  8391. function setupRenderBuffer ( renderbuffer, renderTarget ) {
  8392. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
  8393. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  8394. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
  8395. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  8396. /* For some reason this is not working. Defaulting to RGBA4.
  8397. } else if( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  8398. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
  8399. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  8400. */
  8401. } else if( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  8402. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
  8403. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  8404. } else {
  8405. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );
  8406. }
  8407. };
  8408. this.setRenderTarget = function ( renderTarget ) {
  8409. var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
  8410. if ( renderTarget && ! renderTarget.__webglFramebuffer ) {
  8411. if( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
  8412. if( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;
  8413. renderTarget.__webglTexture = _gl.createTexture();
  8414. // Setup texture, create render and frame buffers
  8415. var isTargetPowerOfTwo = isPowerOfTwo( renderTarget.width ) && isPowerOfTwo( renderTarget.height ),
  8416. glFormat = paramThreeToGL( renderTarget.format ),
  8417. glType = paramThreeToGL( renderTarget.type );
  8418. if ( isCube ) {
  8419. renderTarget.__webglFramebuffer = [];
  8420. renderTarget.__webglRenderbuffer = [];
  8421. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  8422. setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo );
  8423. for ( var i = 0; i < 6; i ++ ) {
  8424. renderTarget.__webglFramebuffer[ i ] = _gl.createFramebuffer();
  8425. renderTarget.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
  8426. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  8427. setupFrameBuffer( renderTarget.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
  8428. setupRenderBuffer( renderTarget.__webglRenderbuffer[ i ], renderTarget );
  8429. }
  8430. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  8431. } else {
  8432. renderTarget.__webglFramebuffer = _gl.createFramebuffer();
  8433. renderTarget.__webglRenderbuffer = _gl.createRenderbuffer();
  8434. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  8435. setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo );
  8436. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  8437. setupFrameBuffer( renderTarget.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
  8438. setupRenderBuffer( renderTarget.__webglRenderbuffer, renderTarget );
  8439. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  8440. }
  8441. // Release everything
  8442. if ( isCube ) {
  8443. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  8444. } else {
  8445. _gl.bindTexture( _gl.TEXTURE_2D, null );
  8446. }
  8447. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  8448. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null);
  8449. }
  8450. var framebuffer, width, height, vx, vy;
  8451. if ( renderTarget ) {
  8452. if ( isCube ) {
  8453. framebuffer = renderTarget.__webglFramebuffer[ renderTarget.activeCubeFace ];
  8454. } else {
  8455. framebuffer = renderTarget.__webglFramebuffer;
  8456. }
  8457. width = renderTarget.width;
  8458. height = renderTarget.height;
  8459. vx = 0;
  8460. vy = 0;
  8461. } else {
  8462. framebuffer = null;
  8463. width = _viewportWidth;
  8464. height = _viewportHeight;
  8465. vx = _viewportX;
  8466. vy = _viewportY;
  8467. }
  8468. if ( framebuffer !== _currentFramebuffer ) {
  8469. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  8470. _gl.viewport( vx, vy, width, height );
  8471. _currentFramebuffer = framebuffer;
  8472. }
  8473. _currentWidth = width;
  8474. _currentHeight = height;
  8475. };
  8476. function updateRenderTargetMipmap ( renderTarget ) {
  8477. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  8478. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  8479. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  8480. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  8481. } else {
  8482. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  8483. _gl.generateMipmap( _gl.TEXTURE_2D );
  8484. _gl.bindTexture( _gl.TEXTURE_2D, null );
  8485. }
  8486. };
  8487. // Fallback filters for non-power-of-2 textures
  8488. function filterFallback ( f ) {
  8489. switch ( f ) {
  8490. case THREE.NearestFilter:
  8491. case THREE.NearestMipMapNearestFilter:
  8492. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST; break;
  8493. case THREE.LinearFilter:
  8494. case THREE.LinearMipMapNearestFilter:
  8495. case THREE.LinearMipMapLinearFilter:
  8496. default:
  8497. return _gl.LINEAR; break;
  8498. }
  8499. };
  8500. // Map three.js constants to WebGL constants
  8501. function paramThreeToGL ( p ) {
  8502. switch ( p ) {
  8503. case THREE.RepeatWrapping: return _gl.REPEAT; break;
  8504. case THREE.ClampToEdgeWrapping: return _gl.CLAMP_TO_EDGE; break;
  8505. case THREE.MirroredRepeatWrapping: return _gl.MIRRORED_REPEAT; break;
  8506. case THREE.NearestFilter: return _gl.NEAREST; break;
  8507. case THREE.NearestMipMapNearestFilter: return _gl.NEAREST_MIPMAP_NEAREST; break;
  8508. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST_MIPMAP_LINEAR; break;
  8509. case THREE.LinearFilter: return _gl.LINEAR; break;
  8510. case THREE.LinearMipMapNearestFilter: return _gl.LINEAR_MIPMAP_NEAREST; break;
  8511. case THREE.LinearMipMapLinearFilter: return _gl.LINEAR_MIPMAP_LINEAR; break;
  8512. case THREE.ByteType: return _gl.BYTE; break;
  8513. case THREE.UnsignedByteType: return _gl.UNSIGNED_BYTE; break;
  8514. case THREE.ShortType: return _gl.SHORT; break;
  8515. case THREE.UnsignedShortType: return _gl.UNSIGNED_SHORT; break;
  8516. case THREE.IntType: return _gl.INT; break;
  8517. case THREE.UnsignedIntType: return _gl.UNSIGNED_INT; break;
  8518. case THREE.FloatType: return _gl.FLOAT; break;
  8519. case THREE.AlphaFormat: return _gl.ALPHA; break;
  8520. case THREE.RGBFormat: return _gl.RGB; break;
  8521. case THREE.RGBAFormat: return _gl.RGBA; break;
  8522. case THREE.LuminanceFormat: return _gl.LUMINANCE; break;
  8523. case THREE.LuminanceAlphaFormat: return _gl.LUMINANCE_ALPHA; break;
  8524. case THREE.AddEquation: return _gl.FUNC_ADD; break;
  8525. case THREE.SubtractEquation: return _gl.FUNC_SUBTRACT; break;
  8526. case THREE.ReverseSubtractEquation: return _gl.FUNC_REVERSE_SUBTRACT; break;
  8527. case THREE.ZeroFactor: return _gl.ZERO; break;
  8528. case THREE.OneFactor: return _gl.ONE; break;
  8529. case THREE.SrcColorFactor: return _gl.SRC_COLOR; break;
  8530. case THREE.OneMinusSrcColorFactor: return _gl.ONE_MINUS_SRC_COLOR; break;
  8531. case THREE.SrcAlphaFactor: return _gl.SRC_ALPHA; break;
  8532. case THREE.OneMinusSrcAlphaFactor: return _gl.ONE_MINUS_SRC_ALPHA; break;
  8533. case THREE.DstAlphaFactor: return _gl.DST_ALPHA; break;
  8534. case THREE.OneMinusDstAlphaFactor: return _gl.ONE_MINUS_DST_ALPHA; break;
  8535. case THREE.DstColorFactor: return _gl.DST_COLOR; break;
  8536. case THREE.OneMinusDstColorFactor: return _gl.ONE_MINUS_DST_COLOR; break;
  8537. case THREE.SrcAlphaSaturateFactor: return _gl.SRC_ALPHA_SATURATE; break;
  8538. }
  8539. return 0;
  8540. };
  8541. // Allocations
  8542. function allocateBones ( object ) {
  8543. // default for when object is not specified
  8544. // ( for example when prebuilding shader
  8545. // to be used with multiple objects )
  8546. //
  8547. // - leave some extra space for other uniforms
  8548. // - limit here is ANGLE's 254 max uniform vectors
  8549. // (up to 54 should be safe)
  8550. var maxBones = 50;
  8551. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  8552. maxBones = object.bones.length;
  8553. }
  8554. return maxBones;
  8555. };
  8556. function allocateLights ( lights ) {
  8557. var l, ll, light, dirLights, pointLights, maxDirLights, maxPointLights;
  8558. dirLights = pointLights = maxDirLights = maxPointLights = 0;
  8559. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  8560. light = lights[ l ];
  8561. if ( light.onlyShadow ) continue;
  8562. if ( light instanceof THREE.DirectionalLight ) dirLights ++;
  8563. if ( light instanceof THREE.PointLight ) pointLights ++;
  8564. if ( light instanceof THREE.SpotLight ) pointLights ++;
  8565. }
  8566. if ( ( pointLights + dirLights ) <= _maxLights ) {
  8567. maxDirLights = dirLights;
  8568. maxPointLights = pointLights;
  8569. } else {
  8570. maxDirLights = Math.ceil( _maxLights * dirLights / ( pointLights + dirLights ) );
  8571. maxPointLights = _maxLights - maxDirLights;
  8572. }
  8573. return { 'directional' : maxDirLights, 'point' : maxPointLights };
  8574. };
  8575. function allocateShadows ( lights ) {
  8576. var l, ll, light, maxShadows = 0;
  8577. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  8578. light = lights[ l ];
  8579. if ( ! light.castShadow ) continue;
  8580. if ( light instanceof THREE.SpotLight ) maxShadows ++;
  8581. if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++;
  8582. }
  8583. return maxShadows;
  8584. };
  8585. // Initialization
  8586. function initGL () {
  8587. var gl;
  8588. try {
  8589. if ( ! ( gl = _canvas.getContext( 'experimental-webgl', { alpha: _alpha, premultipliedAlpha: _premultipliedAlpha, antialias: _antialias, stencil: _stencil, preserveDrawingBuffer: _preserveDrawingBuffer } ) ) ) {
  8590. throw 'Error creating WebGL context.';
  8591. }
  8592. console.log(
  8593. navigator.userAgent + " | " +
  8594. gl.getParameter( gl.VERSION ) + " | " +
  8595. gl.getParameter( gl.VENDOR ) + " | " +
  8596. gl.getParameter( gl.RENDERER ) + " | " +
  8597. gl.getParameter( gl.SHADING_LANGUAGE_VERSION )
  8598. );
  8599. } catch ( error ) {
  8600. console.error( error );
  8601. }
  8602. return gl;
  8603. };
  8604. function setDefaultGLState () {
  8605. _gl.clearColor( 0, 0, 0, 1 );
  8606. _gl.clearDepth( 1 );
  8607. _gl.clearStencil( 0 );
  8608. _gl.enable( _gl.DEPTH_TEST );
  8609. _gl.depthFunc( _gl.LEQUAL );
  8610. _gl.frontFace( _gl.CCW );
  8611. _gl.cullFace( _gl.BACK );
  8612. _gl.enable( _gl.CULL_FACE );
  8613. _gl.enable( _gl.BLEND );
  8614. _gl.blendEquation( _gl.FUNC_ADD );
  8615. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  8616. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  8617. };
  8618. // default plugins (order is important)
  8619. this.shadowMapPlugin = new THREE.ShadowMapPlugin();
  8620. this.addPrePlugin( this.shadowMapPlugin );
  8621. this.addPostPlugin( new THREE.SpritePlugin() );
  8622. this.addPostPlugin( new THREE.LensFlarePlugin() );
  8623. };
  8624. /**
  8625. * @author szimek / https://github.com/szimek/
  8626. */
  8627. THREE.WebGLRenderTarget = function ( width, height, options ) {
  8628. this.width = width;
  8629. this.height = height;
  8630. options = options || {};
  8631. this.wrapS = options.wrapS !== undefined ? options.wrapS : THREE.ClampToEdgeWrapping;
  8632. this.wrapT = options.wrapT !== undefined ? options.wrapT : THREE.ClampToEdgeWrapping;
  8633. this.magFilter = options.magFilter !== undefined ? options.magFilter : THREE.LinearFilter;
  8634. this.minFilter = options.minFilter !== undefined ? options.minFilter : THREE.LinearMipMapLinearFilter;
  8635. this.offset = new THREE.Vector2( 0, 0 );
  8636. this.repeat = new THREE.Vector2( 1, 1 );
  8637. this.format = options.format !== undefined ? options.format : THREE.RGBAFormat;
  8638. this.type = options.type !== undefined ? options.type : THREE.UnsignedByteType;
  8639. this.depthBuffer = options.depthBuffer !== undefined ? options.depthBuffer : true;
  8640. this.stencilBuffer = options.stencilBuffer !== undefined ? options.stencilBuffer : true;
  8641. this.generateMipmaps = true;
  8642. };
  8643. THREE.WebGLRenderTarget.prototype.clone = function() {
  8644. var tmp = new THREE.WebGLRenderTarget( this.width, this.height );
  8645. tmp.wrapS = this.wrapS;
  8646. tmp.wrapT = this.wrapT;
  8647. tmp.magFilter = this.magFilter;
  8648. tmp.minFilter = this.minFilter;
  8649. tmp.offset.copy( this.offset );
  8650. tmp.repeat.copy( this.repeat );
  8651. tmp.format = this.format;
  8652. tmp.type = this.type;
  8653. tmp.depthBuffer = this.depthBuffer;
  8654. tmp.stencilBuffer = this.stencilBuffer;
  8655. return tmp;
  8656. };
  8657. /**
  8658. * @author alteredq / http://alteredqualia.com
  8659. */
  8660. THREE.WebGLRenderTargetCube = function ( width, height, options ) {
  8661. THREE.WebGLRenderTarget.call( this, width, height, options );
  8662. this.activeCubeFace = 0; // PX 0, NX 1, PY 2, NY 3, PZ 4, NZ 5
  8663. };
  8664. THREE.WebGLRenderTargetCube.prototype = new THREE.WebGLRenderTarget();
  8665. THREE.WebGLRenderTargetCube.prototype.constructor = THREE.WebGLRenderTargetCube;
  8666. /**
  8667. * @author mr.doob / http://mrdoob.com/
  8668. */
  8669. THREE.RenderableVertex = function () {
  8670. this.positionWorld = new THREE.Vector3();
  8671. this.positionScreen = new THREE.Vector4();
  8672. this.visible = true;
  8673. };
  8674. THREE.RenderableVertex.prototype.copy = function ( vertex ) {
  8675. this.positionWorld.copy( vertex.positionWorld );
  8676. this.positionScreen.copy( vertex.positionScreen );
  8677. }
  8678. /**
  8679. * @author mr.doob / http://mrdoob.com/
  8680. */
  8681. THREE.RenderableFace3 = function () {
  8682. this.v1 = new THREE.RenderableVertex();
  8683. this.v2 = new THREE.RenderableVertex();
  8684. this.v3 = new THREE.RenderableVertex();
  8685. this.centroidWorld = new THREE.Vector3();
  8686. this.centroidScreen = new THREE.Vector3();
  8687. this.normalWorld = new THREE.Vector3();
  8688. this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  8689. this.material = null;
  8690. this.faceMaterial = null;
  8691. this.uvs = [[]];
  8692. this.z = null;
  8693. };
  8694. /**
  8695. * @author mr.doob / http://mrdoob.com/
  8696. */
  8697. THREE.RenderableFace4 = function () {
  8698. this.v1 = new THREE.RenderableVertex();
  8699. this.v2 = new THREE.RenderableVertex();
  8700. this.v3 = new THREE.RenderableVertex();
  8701. this.v4 = new THREE.RenderableVertex();
  8702. this.centroidWorld = new THREE.Vector3();
  8703. this.centroidScreen = new THREE.Vector3();
  8704. this.normalWorld = new THREE.Vector3();
  8705. this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  8706. this.material = null;
  8707. this.faceMaterial = null;
  8708. this.uvs = [[]];
  8709. this.z = null;
  8710. };
  8711. /**
  8712. * @author mr.doob / http://mrdoob.com/
  8713. */
  8714. THREE.RenderableObject = function () {
  8715. this.object = null;
  8716. this.z = null;
  8717. };
  8718. /**
  8719. * @author mr.doob / http://mrdoob.com/
  8720. */
  8721. THREE.RenderableParticle = function () {
  8722. this.x = null;
  8723. this.y = null;
  8724. this.z = null;
  8725. this.rotation = null;
  8726. this.scale = new THREE.Vector2();
  8727. this.material = null;
  8728. };
  8729. /**
  8730. * @author mr.doob / http://mrdoob.com/
  8731. */
  8732. THREE.RenderableLine = function () {
  8733. this.z = null;
  8734. this.v1 = new THREE.RenderableVertex();
  8735. this.v2 = new THREE.RenderableVertex();
  8736. this.material = null;
  8737. };
  8738. /**
  8739. * @author alteredq / http://alteredqualia.com/
  8740. */
  8741. THREE.BufferGeometry = function () {
  8742. this.id = THREE.GeometryCount ++;
  8743. // GL buffers
  8744. this.vertexIndexBuffer = null;
  8745. this.vertexPositionBuffer = null;
  8746. this.vertexNormalBuffer = null;
  8747. this.vertexUvBuffer = null;
  8748. this.vertexColorBuffer = null;
  8749. // typed arrays (kept only if dynamic flag is set)
  8750. this.vertexIndexArray = null;
  8751. this.vertexPositionArray = null;
  8752. this.vertexNormalArray = null;
  8753. this.vertexUvArray = null;
  8754. this.vertexColorArray = null;
  8755. this.dynamic = false;
  8756. // boundings
  8757. this.boundingBox = null;
  8758. this.boundingSphere = null;
  8759. // for compatibility
  8760. this.morphTargets = [];
  8761. };
  8762. THREE.BufferGeometry.prototype = {
  8763. constructor : THREE.BufferGeometry,
  8764. // for compatibility
  8765. computeBoundingBox: function () {
  8766. },
  8767. // for compatibility
  8768. computeBoundingSphere: function () {
  8769. }
  8770. };
  8771. /**
  8772. * @author alteredq / http://alteredqualia.com/
  8773. */
  8774. THREE.Gyroscope = function () {
  8775. THREE.Object3D.call( this );
  8776. };
  8777. THREE.Gyroscope.prototype = new THREE.Object3D();
  8778. THREE.Gyroscope.prototype.constructor = THREE.Gyroscope;
  8779. THREE.Gyroscope.prototype.updateMatrixWorld = function ( force ) {
  8780. this.matrixAutoUpdate && this.updateMatrix();
  8781. // update matrixWorld
  8782. if ( this.matrixWorldNeedsUpdate || force ) {
  8783. if ( this.parent ) {
  8784. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  8785. this.matrixWorld.decompose( this.translationWorld, this.rotationWorld, this.scaleWorld );
  8786. this.matrix.decompose( this.translationObject, this.rotationObject, this.scaleObject );
  8787. this.matrixWorld.compose( this.translationWorld, this.rotationObject, this.scaleWorld );
  8788. } else {
  8789. this.matrixWorld.copy( this.matrix );
  8790. }
  8791. this.matrixWorldNeedsUpdate = false;
  8792. force = true;
  8793. }
  8794. // update children
  8795. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  8796. this.children[ i ].updateMatrixWorld( force );
  8797. }
  8798. };
  8799. THREE.Gyroscope.prototype.translationWorld = new THREE.Vector3();
  8800. THREE.Gyroscope.prototype.translationObject = new THREE.Vector3();
  8801. THREE.Gyroscope.prototype.rotationWorld = new THREE.Quaternion();
  8802. THREE.Gyroscope.prototype.rotationObject = new THREE.Quaternion();
  8803. THREE.Gyroscope.prototype.scaleWorld = new THREE.Vector3();
  8804. THREE.Gyroscope.prototype.scaleObject = new THREE.Vector3();
  8805. /**
  8806. * @author alteredq / http://alteredqualia.com/
  8807. *
  8808. * - shows frustum, line of sight and up of the camera
  8809. * - suitable for fast updates
  8810. * - based on frustum visualization in lightgl.js shadowmap example
  8811. * http://evanw.github.com/lightgl.js/tests/shadowmap.html
  8812. */
  8813. THREE.CameraHelper = function ( camera ) {
  8814. THREE.Object3D.call( this );
  8815. var _this = this;
  8816. this.lineGeometry = new THREE.Geometry();
  8817. this.lineMaterial = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: THREE.FaceColors } );
  8818. this.pointMap = {};
  8819. // colors
  8820. var hexFrustum = 0xffaa00,
  8821. hexCone = 0xff0000,
  8822. hexUp = 0x00aaff,
  8823. hexTarget = 0xffffff,
  8824. hexCross = 0x333333;
  8825. // near
  8826. addLine( "n1", "n2", hexFrustum );
  8827. addLine( "n2", "n4", hexFrustum );
  8828. addLine( "n4", "n3", hexFrustum );
  8829. addLine( "n3", "n1", hexFrustum );
  8830. // far
  8831. addLine( "f1", "f2", hexFrustum );
  8832. addLine( "f2", "f4", hexFrustum );
  8833. addLine( "f4", "f3", hexFrustum );
  8834. addLine( "f3", "f1", hexFrustum );
  8835. // sides
  8836. addLine( "n1", "f1", hexFrustum );
  8837. addLine( "n2", "f2", hexFrustum );
  8838. addLine( "n3", "f3", hexFrustum );
  8839. addLine( "n4", "f4", hexFrustum );
  8840. // cone
  8841. addLine( "p", "n1", hexCone );
  8842. addLine( "p", "n2", hexCone );
  8843. addLine( "p", "n3", hexCone );
  8844. addLine( "p", "n4", hexCone );
  8845. // up
  8846. addLine( "u1", "u2", hexUp );
  8847. addLine( "u2", "u3", hexUp );
  8848. addLine( "u3", "u1", hexUp );
  8849. // target
  8850. addLine( "c", "t", hexTarget );
  8851. addLine( "p", "c", hexCross );
  8852. // cross
  8853. addLine( "cn1", "cn2", hexCross );
  8854. addLine( "cn3", "cn4", hexCross );
  8855. addLine( "cf1", "cf2", hexCross );
  8856. addLine( "cf3", "cf4", hexCross );
  8857. this.camera = camera;
  8858. function addLine( a, b, hex ) {
  8859. addPoint( a, hex );
  8860. addPoint( b, hex );
  8861. }
  8862. function addPoint( id, hex ) {
  8863. _this.lineGeometry.vertices.push( new THREE.Vertex( new THREE.Vector3() ) );
  8864. _this.lineGeometry.colors.push( new THREE.Color( hex ) );
  8865. if ( _this.pointMap[ id ] === undefined ) _this.pointMap[ id ] = [];
  8866. _this.pointMap[ id ].push( _this.lineGeometry.vertices.length - 1 );
  8867. }
  8868. this.update( camera );
  8869. this.lines = new THREE.Line( this.lineGeometry, this.lineMaterial, THREE.LinePieces );
  8870. this.add( this.lines );
  8871. };
  8872. THREE.CameraHelper.prototype = new THREE.Object3D();
  8873. THREE.CameraHelper.prototype.constructor = THREE.CameraHelper;
  8874. THREE.CameraHelper.prototype.update = function () {
  8875. var camera = this.camera;
  8876. var w = 1;
  8877. var h = 1;
  8878. var _this = this;
  8879. // we need just camera projection matrix
  8880. // world matrix must be identity
  8881. THREE.CameraHelper.__c.projectionMatrix.copy( camera.projectionMatrix );
  8882. // center / target
  8883. setPoint( "c", 0, 0, -1 );
  8884. setPoint( "t", 0, 0, 1 );
  8885. // near
  8886. setPoint( "n1", -w, -h, -1 );
  8887. setPoint( "n2", w, -h, -1 );
  8888. setPoint( "n3", -w, h, -1 );
  8889. setPoint( "n4", w, h, -1 );
  8890. // far
  8891. setPoint( "f1", -w, -h, 1 );
  8892. setPoint( "f2", w, -h, 1 );
  8893. setPoint( "f3", -w, h, 1 );
  8894. setPoint( "f4", w, h, 1 );
  8895. // up
  8896. setPoint( "u1", w * 0.7, h * 1.1, -1 );
  8897. setPoint( "u2", -w * 0.7, h * 1.1, -1 );
  8898. setPoint( "u3", 0, h * 2, -1 );
  8899. // cross
  8900. setPoint( "cf1", -w, 0, 1 );
  8901. setPoint( "cf2", w, 0, 1 );
  8902. setPoint( "cf3", 0, -h, 1 );
  8903. setPoint( "cf4", 0, h, 1 );
  8904. setPoint( "cn1", -w, 0, -1 );
  8905. setPoint( "cn2", w, 0, -1 );
  8906. setPoint( "cn3", 0, -h, -1 );
  8907. setPoint( "cn4", 0, h, -1 );
  8908. function setPoint( point, x, y, z ) {
  8909. THREE.CameraHelper.__v.set( x, y, z );
  8910. THREE.CameraHelper.__projector.unprojectVector( THREE.CameraHelper.__v, THREE.CameraHelper.__c );
  8911. var points = _this.pointMap[ point ];
  8912. if ( points !== undefined ) {
  8913. for ( var i = 0, il = points.length; i < il; i ++ ) {
  8914. var j = points[ i ];
  8915. _this.lineGeometry.vertices[ j ].position.copy( THREE.CameraHelper.__v );
  8916. }
  8917. }
  8918. }
  8919. this.lineGeometry.__dirtyVertices = true;
  8920. };
  8921. THREE.CameraHelper.__projector = new THREE.Projector();
  8922. THREE.CameraHelper.__v = new THREE.Vector3();
  8923. THREE.CameraHelper.__c = new THREE.Camera();
  8924. /**
  8925. * @author mikael emtinger / http://gomo.se/
  8926. * @author alteredq / http://alteredqualia.com/
  8927. */
  8928. THREE.LensFlare = function ( texture, size, distance, blending, color ) {
  8929. THREE.Object3D.call( this );
  8930. this.lensFlares = [];
  8931. this.positionScreen = new THREE.Vector3();
  8932. this.customUpdateCallback = undefined;
  8933. if( texture !== undefined ) {
  8934. this.add( texture, size, distance, blending, color );
  8935. }
  8936. };
  8937. THREE.LensFlare.prototype = new THREE.Object3D();
  8938. THREE.LensFlare.prototype.constructor = THREE.LensFlare;
  8939. THREE.LensFlare.prototype.supr = THREE.Object3D.prototype;
  8940. /*
  8941. * Add: adds another flare
  8942. */
  8943. THREE.LensFlare.prototype.add = function ( texture, size, distance, blending, color, opacity ) {
  8944. if( size === undefined ) size = -1;
  8945. if( distance === undefined ) distance = 0;
  8946. if( opacity === undefined ) opacity = 1;
  8947. if( color === undefined ) color = new THREE.Color( 0xffffff );
  8948. if( blending === undefined ) blending = THREE.NormalBlending;
  8949. distance = Math.min( distance, Math.max( 0, distance ) );
  8950. this.lensFlares.push( { texture: texture, // THREE.Texture
  8951. size: size, // size in pixels (-1 = use texture.width)
  8952. distance: distance, // distance (0-1) from light source (0=at light source)
  8953. x: 0, y: 0, z: 0, // screen position (-1 => 1) z = 0 is ontop z = 1 is back
  8954. scale: 1, // scale
  8955. rotation: 1, // rotation
  8956. opacity: opacity, // opacity
  8957. color: color, // color
  8958. blending: blending } ); // blending
  8959. };
  8960. /*
  8961. * Update lens flares update positions on all flares based on the screen position
  8962. * Set myLensFlare.customUpdateCallback to alter the flares in your project specific way.
  8963. */
  8964. THREE.LensFlare.prototype.updateLensFlares = function () {
  8965. var f, fl = this.lensFlares.length;
  8966. var flare;
  8967. var vecX = -this.positionScreen.x * 2;
  8968. var vecY = -this.positionScreen.y * 2;
  8969. for( f = 0; f < fl; f ++ ) {
  8970. flare = this.lensFlares[ f ];
  8971. flare.x = this.positionScreen.x + vecX * flare.distance;
  8972. flare.y = this.positionScreen.y + vecY * flare.distance;
  8973. flare.wantedRotation = flare.x * Math.PI * 0.25;
  8974. flare.rotation += ( flare.wantedRotation - flare.rotation ) * 0.25;
  8975. }
  8976. };
  8977. /**
  8978. * @author alteredq / http://alteredqualia.com/
  8979. */
  8980. THREE.ImmediateRenderObject = function ( ) {
  8981. THREE.Object3D.call( this );
  8982. this.render = function( renderCallback ) {
  8983. };
  8984. };
  8985. THREE.ImmediateRenderObject.prototype = new THREE.Object3D();
  8986. THREE.ImmediateRenderObject.prototype.constructor = THREE.ImmediateRenderObject;
  8987. /**
  8988. * @author mikael emtinger / http://gomo.se/
  8989. * @author alteredq / http://alteredqualia.com/
  8990. */
  8991. THREE.LensFlarePlugin = function ( ) {
  8992. var _gl, _renderer, _lensFlare = {};
  8993. this.init = function ( renderer ) {
  8994. _gl = renderer.context;
  8995. _renderer = renderer;
  8996. _lensFlare.vertices = new Float32Array( 8 + 8 );
  8997. _lensFlare.faces = new Uint16Array( 6 );
  8998. var i = 0;
  8999. _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = -1; // vertex
  9000. _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 0; // uv... etc.
  9001. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = -1;
  9002. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 0;
  9003. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1;
  9004. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1;
  9005. _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = 1;
  9006. _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 1;
  9007. i = 0;
  9008. _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 1; _lensFlare.faces[ i++ ] = 2;
  9009. _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 2; _lensFlare.faces[ i++ ] = 3;
  9010. // buffers
  9011. _lensFlare.vertexBuffer = _gl.createBuffer();
  9012. _lensFlare.elementBuffer = _gl.createBuffer();
  9013. _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
  9014. _gl.bufferData( _gl.ARRAY_BUFFER, _lensFlare.vertices, _gl.STATIC_DRAW );
  9015. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
  9016. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.faces, _gl.STATIC_DRAW );
  9017. // textures
  9018. _lensFlare.tempTexture = _gl.createTexture();
  9019. _lensFlare.occlusionTexture = _gl.createTexture();
  9020. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  9021. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, 16, 16, 0, _gl.RGB, _gl.UNSIGNED_BYTE, null );
  9022. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  9023. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  9024. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
  9025. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
  9026. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture );
  9027. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, 16, 16, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, null );
  9028. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  9029. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  9030. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
  9031. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
  9032. if ( _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) <= 0 ) {
  9033. _lensFlare.hasVertexTexture = false;
  9034. _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlare" ] );
  9035. } else {
  9036. _lensFlare.hasVertexTexture = true;
  9037. _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlareVertexTexture" ] );
  9038. }
  9039. _lensFlare.attributes = {};
  9040. _lensFlare.uniforms = {};
  9041. _lensFlare.attributes.vertex = _gl.getAttribLocation ( _lensFlare.program, "position" );
  9042. _lensFlare.attributes.uv = _gl.getAttribLocation ( _lensFlare.program, "uv" );
  9043. _lensFlare.uniforms.renderType = _gl.getUniformLocation( _lensFlare.program, "renderType" );
  9044. _lensFlare.uniforms.map = _gl.getUniformLocation( _lensFlare.program, "map" );
  9045. _lensFlare.uniforms.occlusionMap = _gl.getUniformLocation( _lensFlare.program, "occlusionMap" );
  9046. _lensFlare.uniforms.opacity = _gl.getUniformLocation( _lensFlare.program, "opacity" );
  9047. _lensFlare.uniforms.color = _gl.getUniformLocation( _lensFlare.program, "color" );
  9048. _lensFlare.uniforms.scale = _gl.getUniformLocation( _lensFlare.program, "scale" );
  9049. _lensFlare.uniforms.rotation = _gl.getUniformLocation( _lensFlare.program, "rotation" );
  9050. _lensFlare.uniforms.screenPosition = _gl.getUniformLocation( _lensFlare.program, "screenPosition" );
  9051. _lensFlare.attributesEnabled = false;
  9052. };
  9053. /*
  9054. * Render lens flares
  9055. * Method: renders 16x16 0xff00ff-colored points scattered over the light source area,
  9056. * reads these back and calculates occlusion.
  9057. * Then _lensFlare.update_lensFlares() is called to re-position and
  9058. * update transparency of flares. Then they are rendered.
  9059. *
  9060. */
  9061. this.render = function ( scene, camera, viewportWidth, viewportHeight ) {
  9062. var flares = scene.__webglFlares,
  9063. nFlares = flares.length;
  9064. if ( ! nFlares ) return;
  9065. var tempPosition = new THREE.Vector3();
  9066. var invAspect = viewportHeight / viewportWidth,
  9067. halfViewportWidth = viewportWidth * 0.5,
  9068. halfViewportHeight = viewportHeight * 0.5;
  9069. var size = 16 / viewportHeight,
  9070. scale = new THREE.Vector2( size * invAspect, size );
  9071. var screenPosition = new THREE.Vector3( 1, 1, 0 ),
  9072. screenPositionPixels = new THREE.Vector2( 1, 1 );
  9073. var uniforms = _lensFlare.uniforms,
  9074. attributes = _lensFlare.attributes;
  9075. // set _lensFlare program and reset blending
  9076. _gl.useProgram( _lensFlare.program );
  9077. if ( ! _lensFlare.attributesEnabled ) {
  9078. _gl.enableVertexAttribArray( _lensFlare.attributes.vertex );
  9079. _gl.enableVertexAttribArray( _lensFlare.attributes.uv );
  9080. _lensFlare.attributesEnabled = true;
  9081. }
  9082. // loop through all lens flares to update their occlusion and positions
  9083. // setup gl and common used attribs/unforms
  9084. _gl.uniform1i( uniforms.occlusionMap, 0 );
  9085. _gl.uniform1i( uniforms.map, 1 );
  9086. _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
  9087. _gl.vertexAttribPointer( attributes.vertex, 2, _gl.FLOAT, false, 2 * 8, 0 );
  9088. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 );
  9089. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
  9090. _gl.disable( _gl.CULL_FACE );
  9091. _gl.depthMask( false );
  9092. var i, j, jl, flare, sprite;
  9093. for ( i = 0; i < nFlares; i ++ ) {
  9094. size = 16 / viewportHeight;
  9095. scale.set( size * invAspect, size );
  9096. // calc object screen position
  9097. flare = flares[ i ];
  9098. tempPosition.set( flare.matrixWorld.elements[12], flare.matrixWorld.elements[13], flare.matrixWorld.elements[14] );
  9099. camera.matrixWorldInverse.multiplyVector3( tempPosition );
  9100. camera.projectionMatrix.multiplyVector3( tempPosition );
  9101. // setup arrays for gl programs
  9102. screenPosition.copy( tempPosition )
  9103. screenPositionPixels.x = screenPosition.x * halfViewportWidth + halfViewportWidth;
  9104. screenPositionPixels.y = screenPosition.y * halfViewportHeight + halfViewportHeight;
  9105. // screen cull
  9106. if ( _lensFlare.hasVertexTexture || (
  9107. screenPositionPixels.x > 0 &&
  9108. screenPositionPixels.x < viewportWidth &&
  9109. screenPositionPixels.y > 0 &&
  9110. screenPositionPixels.y < viewportHeight ) ) {
  9111. // save current RGB to temp texture
  9112. _gl.activeTexture( _gl.TEXTURE1 );
  9113. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  9114. _gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
  9115. // render pink quad
  9116. _gl.uniform1i( uniforms.renderType, 0 );
  9117. _gl.uniform2f( uniforms.scale, scale.x, scale.y );
  9118. _gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z );
  9119. _gl.disable( _gl.BLEND );
  9120. _gl.enable( _gl.DEPTH_TEST );
  9121. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9122. // copy result to occlusionMap
  9123. _gl.activeTexture( _gl.TEXTURE0 );
  9124. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture );
  9125. _gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
  9126. // restore graphics
  9127. _gl.uniform1i( uniforms.renderType, 1 );
  9128. _gl.disable( _gl.DEPTH_TEST );
  9129. _gl.activeTexture( _gl.TEXTURE1 );
  9130. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  9131. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9132. // update object positions
  9133. flare.positionScreen.copy( screenPosition )
  9134. if ( flare.customUpdateCallback ) {
  9135. flare.customUpdateCallback( flare );
  9136. } else {
  9137. flare.updateLensFlares();
  9138. }
  9139. // render flares
  9140. _gl.uniform1i( uniforms.renderType, 2 );
  9141. _gl.enable( _gl.BLEND );
  9142. for ( j = 0, jl = flare.lensFlares.length; j < jl; j ++ ) {
  9143. sprite = flare.lensFlares[ j ];
  9144. if ( sprite.opacity > 0.001 && sprite.scale > 0.001 ) {
  9145. screenPosition.x = sprite.x;
  9146. screenPosition.y = sprite.y;
  9147. screenPosition.z = sprite.z;
  9148. size = sprite.size * sprite.scale / viewportHeight;
  9149. scale.x = size * invAspect;
  9150. scale.y = size;
  9151. _gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z );
  9152. _gl.uniform2f( uniforms.scale, scale.x, scale.y );
  9153. _gl.uniform1f( uniforms.rotation, sprite.rotation );
  9154. _gl.uniform1f( uniforms.opacity, sprite.opacity );
  9155. _gl.uniform3f( uniforms.color, sprite.color.r, sprite.color.g, sprite.color.b );
  9156. _renderer.setBlending( sprite.blending, sprite.blendEquation, sprite.blendSrc, sprite.blendDst );
  9157. _renderer.setTexture( sprite.texture, 1 );
  9158. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9159. }
  9160. }
  9161. }
  9162. }
  9163. // restore gl
  9164. _gl.enable( _gl.CULL_FACE );
  9165. _gl.enable( _gl.DEPTH_TEST );
  9166. _gl.depthMask( true );
  9167. };
  9168. function createProgram ( shader ) {
  9169. var program = _gl.createProgram();
  9170. var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER );
  9171. var vertexShader = _gl.createShader( _gl.VERTEX_SHADER );
  9172. _gl.shaderSource( fragmentShader, shader.fragmentShader );
  9173. _gl.shaderSource( vertexShader, shader.vertexShader );
  9174. _gl.compileShader( fragmentShader );
  9175. _gl.compileShader( vertexShader );
  9176. _gl.attachShader( program, fragmentShader );
  9177. _gl.attachShader( program, vertexShader );
  9178. _gl.linkProgram( program );
  9179. return program;
  9180. };
  9181. };/**
  9182. * @author alteredq / http://alteredqualia.com/
  9183. */
  9184. THREE.ShadowMapPlugin = function ( ) {
  9185. var _gl,
  9186. _renderer,
  9187. _depthMaterial, _depthMaterialMorph,
  9188. _frustum = new THREE.Frustum(),
  9189. _projScreenMatrix = new THREE.Matrix4(),
  9190. _min = new THREE.Vector3(),
  9191. _max = new THREE.Vector3();
  9192. this.init = function ( renderer ) {
  9193. _gl = renderer.context;
  9194. _renderer = renderer;
  9195. var depthShader = THREE.ShaderLib[ "depthRGBA" ];
  9196. var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms );
  9197. _depthMaterial = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms } );
  9198. _depthMaterialMorph = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, morphTargets: true } );
  9199. _depthMaterial._shadowPass = true;
  9200. _depthMaterialMorph._shadowPass = true;
  9201. };
  9202. this.render = function ( scene, camera ) {
  9203. if ( ! ( _renderer.shadowMapEnabled && _renderer.shadowMapAutoUpdate ) ) return;
  9204. this.update( scene, camera );
  9205. };
  9206. this.update = function ( scene, camera ) {
  9207. var i, il, j, jl, n,
  9208. shadowMap, shadowMatrix, shadowCamera,
  9209. program, buffer, material,
  9210. webglObject, object, light,
  9211. renderList,
  9212. lights = [],
  9213. k = 0,
  9214. fog = null;
  9215. // set GL state for depth map
  9216. _gl.clearColor( 1, 1, 1, 1 );
  9217. _gl.disable( _gl.BLEND );
  9218. if ( _renderer.shadowMapCullFrontFaces ) _gl.cullFace( _gl.FRONT );
  9219. _renderer.setDepthTest( true );
  9220. // preprocess lights
  9221. // - skip lights that are not casting shadows
  9222. // - create virtual lights for cascaded shadow maps
  9223. for ( i = 0, il = scene.__lights.length; i < il; i ++ ) {
  9224. light = scene.__lights[ i ];
  9225. if ( ! light.castShadow ) continue;
  9226. if ( ( light instanceof THREE.DirectionalLight ) && light.shadowCascade ) {
  9227. for ( n = 0; n < light.shadowCascadeCount; n ++ ) {
  9228. var virtualLight;
  9229. if ( ! light.shadowCascadeArray[ n ] ) {
  9230. virtualLight = createVirtualLight( light, n );
  9231. virtualLight.originalCamera = camera;
  9232. var gyro = new THREE.Gyroscope();
  9233. gyro.position = light.shadowCascadeOffset;
  9234. gyro.add( virtualLight );
  9235. gyro.add( virtualLight.target );
  9236. camera.add( gyro );
  9237. light.shadowCascadeArray[ n ] = virtualLight;
  9238. console.log( "Created virtualLight", virtualLight );
  9239. } else {
  9240. virtualLight = light.shadowCascadeArray[ n ];
  9241. }
  9242. updateVirtualLight( light, n );
  9243. lights[ k ] = virtualLight;
  9244. k ++;
  9245. }
  9246. } else {
  9247. lights[ k ] = light;
  9248. k ++;
  9249. }
  9250. }
  9251. // render depth map
  9252. for ( i = 0, il = lights.length; i < il; i ++ ) {
  9253. light = lights[ i ];
  9254. if ( ! light.shadowMap ) {
  9255. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat };
  9256. light.shadowMap = new THREE.WebGLRenderTarget( light.shadowMapWidth, light.shadowMapHeight, pars );
  9257. light.shadowMapSize = new THREE.Vector2( light.shadowMapWidth, light.shadowMapHeight );
  9258. light.shadowMatrix = new THREE.Matrix4();
  9259. }
  9260. if ( ! light.shadowCamera ) {
  9261. if ( light instanceof THREE.SpotLight ) {
  9262. light.shadowCamera = new THREE.PerspectiveCamera( light.shadowCameraFov, light.shadowMapWidth / light.shadowMapHeight, light.shadowCameraNear, light.shadowCameraFar );
  9263. } else if ( light instanceof THREE.DirectionalLight ) {
  9264. light.shadowCamera = new THREE.OrthographicCamera( light.shadowCameraLeft, light.shadowCameraRight, light.shadowCameraTop, light.shadowCameraBottom, light.shadowCameraNear, light.shadowCameraFar );
  9265. } else {
  9266. console.error( "Unsupported light type for shadow" );
  9267. continue;
  9268. }
  9269. scene.add( light.shadowCamera );
  9270. if ( _renderer.autoUpdateScene ) scene.updateMatrixWorld();
  9271. }
  9272. if ( light.shadowCameraVisible && ! light.cameraHelper ) {
  9273. light.cameraHelper = new THREE.CameraHelper( light.shadowCamera );
  9274. light.shadowCamera.add( light.cameraHelper );
  9275. }
  9276. if ( light.isVirtual && virtualLight.originalCamera == camera ) {
  9277. updateShadowCamera( camera, light );
  9278. }
  9279. shadowMap = light.shadowMap;
  9280. shadowMatrix = light.shadowMatrix;
  9281. shadowCamera = light.shadowCamera;
  9282. shadowCamera.position.copy( light.matrixWorld.getPosition() );
  9283. shadowCamera.lookAt( light.target.matrixWorld.getPosition() );
  9284. shadowCamera.updateMatrixWorld();
  9285. shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
  9286. if ( light.cameraHelper ) light.cameraHelper.lines.visible = light.shadowCameraVisible;
  9287. if ( light.shadowCameraVisible ) light.cameraHelper.update();
  9288. // compute shadow matrix
  9289. shadowMatrix.set( 0.5, 0.0, 0.0, 0.5,
  9290. 0.0, 0.5, 0.0, 0.5,
  9291. 0.0, 0.0, 0.5, 0.5,
  9292. 0.0, 0.0, 0.0, 1.0 );
  9293. shadowMatrix.multiplySelf( shadowCamera.projectionMatrix );
  9294. shadowMatrix.multiplySelf( shadowCamera.matrixWorldInverse );
  9295. // update camera matrices and frustum
  9296. if ( ! shadowCamera._viewMatrixArray ) shadowCamera._viewMatrixArray = new Float32Array( 16 );
  9297. if ( ! shadowCamera._projectionMatrixArray ) shadowCamera._projectionMatrixArray = new Float32Array( 16 );
  9298. shadowCamera.matrixWorldInverse.flattenToArray( shadowCamera._viewMatrixArray );
  9299. shadowCamera.projectionMatrix.flattenToArray( shadowCamera._projectionMatrixArray );
  9300. _projScreenMatrix.multiply( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  9301. _frustum.setFromMatrix( _projScreenMatrix );
  9302. // render shadow map
  9303. _renderer.setRenderTarget( shadowMap );
  9304. _renderer.clear();
  9305. // set object matrices & frustum culling
  9306. renderList = scene.__webglObjects;
  9307. for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
  9308. webglObject = renderList[ j ];
  9309. object = webglObject.object;
  9310. webglObject.render = false;
  9311. if ( object.visible && object.castShadow ) {
  9312. if ( ! ( object instanceof THREE.Mesh ) || ! ( object.frustumCulled ) || _frustum.contains( object ) ) {
  9313. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  9314. object._modelViewMatrix.multiplyToArray( shadowCamera.matrixWorldInverse, object.matrixWorld, object._modelViewMatrixArray );
  9315. webglObject.render = true;
  9316. }
  9317. }
  9318. }
  9319. // render regular objects
  9320. for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
  9321. webglObject = renderList[ j ];
  9322. if ( webglObject.render ) {
  9323. object = webglObject.object;
  9324. buffer = webglObject.buffer;
  9325. _renderer.setObjectFaces( object );
  9326. if ( object.customDepthMaterial ) {
  9327. material = object.customDepthMaterial;
  9328. } else if ( object.geometry.morphTargets.length ) {
  9329. material = _depthMaterialMorph;
  9330. } else {
  9331. material = _depthMaterial;
  9332. }
  9333. if ( buffer instanceof THREE.BufferGeometry ) {
  9334. _renderer.renderBufferDirect( shadowCamera, scene.__lights, fog, material, buffer, object );
  9335. } else {
  9336. _renderer.renderBuffer( shadowCamera, scene.__lights, fog, material, buffer, object );
  9337. }
  9338. }
  9339. }
  9340. // set matrices and render immediate objects
  9341. renderList = scene.__webglObjectsImmediate;
  9342. for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
  9343. webglObject = renderList[ j ];
  9344. object = webglObject.object;
  9345. if ( object.visible && object.castShadow ) {
  9346. if( object.matrixAutoUpdate ) {
  9347. object.matrixWorld.flattenToArray( object._objectMatrixArray );
  9348. }
  9349. object._modelViewMatrix.multiplyToArray( shadowCamera.matrixWorldInverse, object.matrixWorld, object._modelViewMatrixArray );
  9350. _renderer.renderImmediateObject( shadowCamera, scene.__lights, fog, _depthMaterial, object );
  9351. }
  9352. }
  9353. }
  9354. // restore GL state
  9355. var clearColor = _renderer.getClearColor(),
  9356. clearAlpha = _renderer.getClearAlpha();
  9357. _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha );
  9358. _gl.enable( _gl.BLEND );
  9359. if ( _renderer.shadowMapCullFrontFaces ) _gl.cullFace( _gl.BACK );
  9360. };
  9361. function createVirtualLight( light, cascade ) {
  9362. var virtualLight = new THREE.DirectionalLight();
  9363. virtualLight.isVirtual = true;
  9364. virtualLight.onlyShadow = true;
  9365. virtualLight.castShadow = true;
  9366. virtualLight.shadowCameraNear = light.shadowCameraNear;
  9367. virtualLight.shadowCameraFar = light.shadowCameraFar;
  9368. virtualLight.shadowCameraLeft = light.shadowCameraLeft;
  9369. virtualLight.shadowCameraRight = light.shadowCameraRight;
  9370. virtualLight.shadowCameraBottom = light.shadowCameraBottom;
  9371. virtualLight.shadowCameraTop = light.shadowCameraTop;
  9372. virtualLight.shadowCameraVisible = light.shadowCameraVisible;
  9373. virtualLight.shadowDarkness = light.shadowDarkness;
  9374. virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
  9375. virtualLight.shadowMapWidth = light.shadowCascadeWidth[ cascade ];
  9376. virtualLight.shadowMapHeight = light.shadowCascadeHeight[ cascade ];
  9377. virtualLight.pointsWorld = [];
  9378. virtualLight.pointsFrustum = [];
  9379. var pointsWorld = virtualLight.pointsWorld,
  9380. pointsFrustum = virtualLight.pointsFrustum;
  9381. for ( var i = 0; i < 8; i ++ ) {
  9382. pointsWorld[ i ] = new THREE.Vector3();
  9383. pointsFrustum[ i ] = new THREE.Vector3();
  9384. }
  9385. var nearZ = light.shadowCascadeNearZ[ cascade ];
  9386. var farZ = light.shadowCascadeFarZ[ cascade ];
  9387. pointsFrustum[ 0 ].set( -1, -1, nearZ );
  9388. pointsFrustum[ 1 ].set( 1, -1, nearZ );
  9389. pointsFrustum[ 2 ].set( -1, 1, nearZ );
  9390. pointsFrustum[ 3 ].set( 1, 1, nearZ );
  9391. pointsFrustum[ 4 ].set( -1, -1, farZ );
  9392. pointsFrustum[ 5 ].set( 1, -1, farZ );
  9393. pointsFrustum[ 6 ].set( -1, 1, farZ );
  9394. pointsFrustum[ 7 ].set( 1, 1, farZ );
  9395. return virtualLight;
  9396. }
  9397. // Synchronize virtual light with the original light
  9398. function updateVirtualLight( light, cascade ) {
  9399. var virtualLight = light.shadowCascadeArray[ cascade ];
  9400. virtualLight.position.copy( light.position );
  9401. virtualLight.target.position.copy( light.target.position );
  9402. virtualLight.lookAt( virtualLight.target );
  9403. virtualLight.shadowCameraVisible = light.shadowCameraVisible;
  9404. virtualLight.shadowDarkness = light.shadowDarkness;
  9405. virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
  9406. var nearZ = light.shadowCascadeNearZ[ cascade ];
  9407. var farZ = light.shadowCascadeFarZ[ cascade ];
  9408. var pointsFrustum = virtualLight.pointsFrustum;
  9409. pointsFrustum[ 0 ].z = nearZ;
  9410. pointsFrustum[ 1 ].z = nearZ;
  9411. pointsFrustum[ 2 ].z = nearZ;
  9412. pointsFrustum[ 3 ].z = nearZ;
  9413. pointsFrustum[ 4 ].z = farZ;
  9414. pointsFrustum[ 5 ].z = farZ;
  9415. pointsFrustum[ 6 ].z = farZ;
  9416. pointsFrustum[ 7 ].z = farZ;
  9417. }
  9418. // Fit shadow camera's ortho frustum to camera frustum
  9419. function updateShadowCamera( camera, light ) {
  9420. var shadowCamera = light.shadowCamera,
  9421. pointsFrustum = light.pointsFrustum,
  9422. pointsWorld = light.pointsWorld;
  9423. _min.set( Infinity, Infinity, Infinity );
  9424. _max.set( -Infinity, -Infinity, -Infinity );
  9425. for ( var i = 0; i < 8; i ++ ) {
  9426. var p = pointsWorld[ i ];
  9427. p.copy( pointsFrustum[ i ] );
  9428. THREE.ShadowMapPlugin.__projector.unprojectVector( p, camera );
  9429. shadowCamera.matrixWorldInverse.multiplyVector3( p );
  9430. if ( p.x < _min.x ) _min.x = p.x;
  9431. if ( p.x > _max.x ) _max.x = p.x;
  9432. if ( p.y < _min.y ) _min.y = p.y;
  9433. if ( p.y > _max.y ) _max.y = p.y;
  9434. if ( p.z < _min.z ) _min.z = p.z;
  9435. if ( p.z > _max.z ) _max.z = p.z;
  9436. }
  9437. shadowCamera.left = _min.x;
  9438. shadowCamera.right = _max.x;
  9439. shadowCamera.top = _max.y;
  9440. shadowCamera.bottom = _min.y;
  9441. // can't really fit near/far
  9442. //shadowCamera.near = _min.z;
  9443. //shadowCamera.far = _max.z;
  9444. shadowCamera.updateProjectionMatrix();
  9445. }
  9446. };
  9447. THREE.ShadowMapPlugin.__projector = new THREE.Projector();
  9448. /**
  9449. * @author mikael emtinger / http://gomo.se/
  9450. * @author alteredq / http://alteredqualia.com/
  9451. */
  9452. THREE.SpritePlugin = function ( ) {
  9453. var _gl, _renderer, _sprite = {};
  9454. this.init = function ( renderer ) {
  9455. _gl = renderer.context;
  9456. _renderer = renderer;
  9457. _sprite.vertices = new Float32Array( 8 + 8 );
  9458. _sprite.faces = new Uint16Array( 6 );
  9459. var i = 0;
  9460. _sprite.vertices[ i++ ] = -1; _sprite.vertices[ i++ ] = -1; // vertex 0
  9461. _sprite.vertices[ i++ ] = 0; _sprite.vertices[ i++ ] = 1; // uv 0
  9462. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = -1; // vertex 1
  9463. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 1; // uv 1
  9464. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 1; // vertex 2
  9465. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 0; // uv 2
  9466. _sprite.vertices[ i++ ] = -1; _sprite.vertices[ i++ ] = 1; // vertex 3
  9467. _sprite.vertices[ i++ ] = 0; _sprite.vertices[ i++ ] = 0; // uv 3
  9468. i = 0;
  9469. _sprite.faces[ i++ ] = 0; _sprite.faces[ i++ ] = 1; _sprite.faces[ i++ ] = 2;
  9470. _sprite.faces[ i++ ] = 0; _sprite.faces[ i++ ] = 2; _sprite.faces[ i++ ] = 3;
  9471. _sprite.vertexBuffer = _gl.createBuffer();
  9472. _sprite.elementBuffer = _gl.createBuffer();
  9473. _gl.bindBuffer( _gl.ARRAY_BUFFER, _sprite.vertexBuffer );
  9474. _gl.bufferData( _gl.ARRAY_BUFFER, _sprite.vertices, _gl.STATIC_DRAW );
  9475. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _sprite.elementBuffer );
  9476. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _sprite.faces, _gl.STATIC_DRAW );
  9477. _sprite.program = createProgram( THREE.ShaderSprite[ "sprite" ] );
  9478. _sprite.attributes = {};
  9479. _sprite.uniforms = {};
  9480. _sprite.attributes.position = _gl.getAttribLocation ( _sprite.program, "position" );
  9481. _sprite.attributes.uv = _gl.getAttribLocation ( _sprite.program, "uv" );
  9482. _sprite.uniforms.uvOffset = _gl.getUniformLocation( _sprite.program, "uvOffset" );
  9483. _sprite.uniforms.uvScale = _gl.getUniformLocation( _sprite.program, "uvScale" );
  9484. _sprite.uniforms.rotation = _gl.getUniformLocation( _sprite.program, "rotation" );
  9485. _sprite.uniforms.scale = _gl.getUniformLocation( _sprite.program, "scale" );
  9486. _sprite.uniforms.alignment = _gl.getUniformLocation( _sprite.program, "alignment" );
  9487. _sprite.uniforms.color = _gl.getUniformLocation( _sprite.program, "color" );
  9488. _sprite.uniforms.map = _gl.getUniformLocation( _sprite.program, "map" );
  9489. _sprite.uniforms.opacity = _gl.getUniformLocation( _sprite.program, "opacity" );
  9490. _sprite.uniforms.useScreenCoordinates = _gl.getUniformLocation( _sprite.program, "useScreenCoordinates" );
  9491. _sprite.uniforms.affectedByDistance = _gl.getUniformLocation( _sprite.program, "affectedByDistance" );
  9492. _sprite.uniforms.screenPosition = _gl.getUniformLocation( _sprite.program, "screenPosition" );
  9493. _sprite.uniforms.modelViewMatrix = _gl.getUniformLocation( _sprite.program, "modelViewMatrix" );
  9494. _sprite.uniforms.projectionMatrix = _gl.getUniformLocation( _sprite.program, "projectionMatrix" );
  9495. _sprite.attributesEnabled = false;
  9496. };
  9497. this.render = function ( scene, camera, viewportWidth, viewportHeight ) {
  9498. var sprites = scene.__webglSprites,
  9499. nSprites = sprites.length;
  9500. if ( ! nSprites ) return;
  9501. var attributes = _sprite.attributes,
  9502. uniforms = _sprite.uniforms;
  9503. var invAspect = viewportHeight / viewportWidth;
  9504. var halfViewportWidth = viewportWidth * 0.5,
  9505. halfViewportHeight = viewportHeight * 0.5;
  9506. var mergeWith3D = true;
  9507. // setup gl
  9508. _gl.useProgram( _sprite.program );
  9509. if ( ! _sprite.attributesEnabled ) {
  9510. _gl.enableVertexAttribArray( attributes.position );
  9511. _gl.enableVertexAttribArray( attributes.uv );
  9512. _sprite.attributesEnabled = true;
  9513. }
  9514. _gl.disable( _gl.CULL_FACE );
  9515. _gl.enable( _gl.BLEND );
  9516. _gl.depthMask( true );
  9517. _gl.bindBuffer( _gl.ARRAY_BUFFER, _sprite.vertexBuffer );
  9518. _gl.vertexAttribPointer( attributes.position, 2, _gl.FLOAT, false, 2 * 8, 0 );
  9519. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 );
  9520. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _sprite.elementBuffer );
  9521. _gl.uniformMatrix4fv( uniforms.projectionMatrix, false, camera._projectionMatrixArray );
  9522. _gl.activeTexture( _gl.TEXTURE0 );
  9523. _gl.uniform1i( uniforms.map, 0 );
  9524. // update positions and sort
  9525. var i, sprite, screenPosition, size, scale = [];
  9526. for( i = 0; i < nSprites; i ++ ) {
  9527. sprite = sprites[ i ];
  9528. if ( ! sprite.visible || sprite.opacity === 0 ) continue;
  9529. if( ! sprite.useScreenCoordinates ) {
  9530. sprite._modelViewMatrix.multiplyToArray( camera.matrixWorldInverse, sprite.matrixWorld, sprite._modelViewMatrixArray );
  9531. sprite.z = - sprite._modelViewMatrix.elements[14];
  9532. } else {
  9533. sprite.z = - sprite.position.z;
  9534. }
  9535. }
  9536. sprites.sort( painterSort );
  9537. // render all sprites
  9538. for( i = 0; i < nSprites; i ++ ) {
  9539. sprite = sprites[ i ];
  9540. if ( ! sprite.visible || sprite.opacity === 0 ) continue;
  9541. if ( sprite.map && sprite.map.image && sprite.map.image.width ) {
  9542. if ( sprite.useScreenCoordinates ) {
  9543. _gl.uniform1i( uniforms.useScreenCoordinates, 1 );
  9544. _gl.uniform3f( uniforms.screenPosition, ( sprite.position.x - halfViewportWidth ) / halfViewportWidth,
  9545. ( halfViewportHeight - sprite.position.y ) / halfViewportHeight,
  9546. Math.max( 0, Math.min( 1, sprite.position.z ) ) );
  9547. } else {
  9548. _gl.uniform1i( uniforms.useScreenCoordinates, 0 );
  9549. _gl.uniform1i( uniforms.affectedByDistance, sprite.affectedByDistance ? 1 : 0 );
  9550. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, sprite._modelViewMatrixArray );
  9551. }
  9552. size = sprite.map.image.width / ( sprite.scaleByViewport ? viewportHeight : 1 );
  9553. scale[ 0 ] = size * invAspect * sprite.scale.x;
  9554. scale[ 1 ] = size * sprite.scale.y;
  9555. _gl.uniform2f( uniforms.uvScale, sprite.uvScale.x, sprite.uvScale.y );
  9556. _gl.uniform2f( uniforms.uvOffset, sprite.uvOffset.x, sprite.uvOffset.y );
  9557. _gl.uniform2f( uniforms.alignment, sprite.alignment.x, sprite.alignment.y );
  9558. _gl.uniform1f( uniforms.opacity, sprite.opacity );
  9559. _gl.uniform3f( uniforms.color, sprite.color.r, sprite.color.g, sprite.color.b );
  9560. _gl.uniform1f( uniforms.rotation, sprite.rotation );
  9561. _gl.uniform2fv( uniforms.scale, scale );
  9562. if ( sprite.mergeWith3D && !mergeWith3D ) {
  9563. _gl.enable( _gl.DEPTH_TEST );
  9564. mergeWith3D = true;
  9565. } else if ( ! sprite.mergeWith3D && mergeWith3D ) {
  9566. _gl.disable( _gl.DEPTH_TEST );
  9567. mergeWith3D = false;
  9568. }
  9569. _renderer.setBlending( sprite.blending, sprite.blendEquation, sprite.blendSrc, sprite.blendDst );
  9570. _renderer.setTexture( sprite.map, 0 );
  9571. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9572. }
  9573. }
  9574. // restore gl
  9575. _gl.enable( _gl.CULL_FACE );
  9576. _gl.enable( _gl.DEPTH_TEST );
  9577. _gl.depthMask( true );
  9578. };
  9579. function createProgram ( shader ) {
  9580. var program = _gl.createProgram();
  9581. var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER );
  9582. var vertexShader = _gl.createShader( _gl.VERTEX_SHADER );
  9583. _gl.shaderSource( fragmentShader, shader.fragmentShader );
  9584. _gl.shaderSource( vertexShader, shader.vertexShader );
  9585. _gl.compileShader( fragmentShader );
  9586. _gl.compileShader( vertexShader );
  9587. _gl.attachShader( program, fragmentShader );
  9588. _gl.attachShader( program, vertexShader );
  9589. _gl.linkProgram( program );
  9590. return program;
  9591. };
  9592. function painterSort ( a, b ) {
  9593. return b.z - a.z;
  9594. };
  9595. };/**
  9596. * @author mikael emtinger / http://gomo.se/
  9597. *
  9598. */
  9599. THREE.ShaderFlares = {
  9600. 'lensFlareVertexTexture': {
  9601. vertexShader: [
  9602. "uniform vec3 screenPosition;",
  9603. "uniform vec2 scale;",
  9604. "uniform float rotation;",
  9605. "uniform int renderType;",
  9606. "uniform sampler2D occlusionMap;",
  9607. "attribute vec2 position;",
  9608. "attribute vec2 uv;",
  9609. "varying vec2 vUV;",
  9610. "varying float vVisibility;",
  9611. "void main() {",
  9612. "vUV = uv;",
  9613. "vec2 pos = position;",
  9614. "if( renderType == 2 ) {",
  9615. "vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) ) +",
  9616. "texture2D( occlusionMap, vec2( 0.5, 0.1 ) ) +",
  9617. "texture2D( occlusionMap, vec2( 0.9, 0.1 ) ) +",
  9618. "texture2D( occlusionMap, vec2( 0.9, 0.5 ) ) +",
  9619. "texture2D( occlusionMap, vec2( 0.9, 0.9 ) ) +",
  9620. "texture2D( occlusionMap, vec2( 0.5, 0.9 ) ) +",
  9621. "texture2D( occlusionMap, vec2( 0.1, 0.9 ) ) +",
  9622. "texture2D( occlusionMap, vec2( 0.1, 0.5 ) ) +",
  9623. "texture2D( occlusionMap, vec2( 0.5, 0.5 ) );",
  9624. "vVisibility = ( visibility.r / 9.0 ) *",
  9625. "( 1.0 - visibility.g / 9.0 ) *",
  9626. "( visibility.b / 9.0 ) *",
  9627. "( 1.0 - visibility.a / 9.0 );",
  9628. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  9629. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  9630. "}",
  9631. "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  9632. "}"
  9633. ].join( "\n" ),
  9634. fragmentShader: [
  9635. "precision mediump float;",
  9636. "uniform sampler2D map;",
  9637. "uniform float opacity;",
  9638. "uniform int renderType;",
  9639. "uniform vec3 color;",
  9640. "varying vec2 vUV;",
  9641. "varying float vVisibility;",
  9642. "void main() {",
  9643. // pink square
  9644. "if( renderType == 0 ) {",
  9645. "gl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );",
  9646. // restore
  9647. "} else if( renderType == 1 ) {",
  9648. "gl_FragColor = texture2D( map, vUV );",
  9649. // flare
  9650. "} else {",
  9651. "vec4 texture = texture2D( map, vUV );",
  9652. "texture.a *= opacity * vVisibility;",
  9653. "gl_FragColor = texture;",
  9654. "gl_FragColor.rgb *= color;",
  9655. "}",
  9656. "}"
  9657. ].join( "\n" )
  9658. },
  9659. 'lensFlare': {
  9660. vertexShader: [
  9661. "uniform vec3 screenPosition;",
  9662. "uniform vec2 scale;",
  9663. "uniform float rotation;",
  9664. "uniform int renderType;",
  9665. "attribute vec2 position;",
  9666. "attribute vec2 uv;",
  9667. "varying vec2 vUV;",
  9668. "void main() {",
  9669. "vUV = uv;",
  9670. "vec2 pos = position;",
  9671. "if( renderType == 2 ) {",
  9672. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  9673. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  9674. "}",
  9675. "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  9676. "}"
  9677. ].join( "\n" ),
  9678. fragmentShader: [
  9679. "precision mediump float;",
  9680. "uniform sampler2D map;",
  9681. "uniform sampler2D occlusionMap;",
  9682. "uniform float opacity;",
  9683. "uniform int renderType;",
  9684. "uniform vec3 color;",
  9685. "varying vec2 vUV;",
  9686. "void main() {",
  9687. // pink square
  9688. "if( renderType == 0 ) {",
  9689. "gl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );",
  9690. // restore
  9691. "} else if( renderType == 1 ) {",
  9692. "gl_FragColor = texture2D( map, vUV );",
  9693. // flare
  9694. "} else {",
  9695. "float visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 ) ).a +",
  9696. "texture2D( occlusionMap, vec2( 0.9, 0.5 ) ).a +",
  9697. "texture2D( occlusionMap, vec2( 0.5, 0.9 ) ).a +",
  9698. "texture2D( occlusionMap, vec2( 0.1, 0.5 ) ).a;",
  9699. "visibility = ( 1.0 - visibility / 4.0 );",
  9700. "vec4 texture = texture2D( map, vUV );",
  9701. "texture.a *= opacity * visibility;",
  9702. "gl_FragColor = texture;",
  9703. "gl_FragColor.rgb *= color;",
  9704. "}",
  9705. "}"
  9706. ].join( "\n" )
  9707. }
  9708. };
  9709. /**
  9710. * @author mikael emtinger / http://gomo.se/
  9711. *
  9712. */
  9713. THREE.ShaderSprite = {
  9714. 'sprite': {
  9715. vertexShader: [
  9716. "uniform int useScreenCoordinates;",
  9717. "uniform int affectedByDistance;",
  9718. "uniform vec3 screenPosition;",
  9719. "uniform mat4 modelViewMatrix;",
  9720. "uniform mat4 projectionMatrix;",
  9721. "uniform float rotation;",
  9722. "uniform vec2 scale;",
  9723. "uniform vec2 alignment;",
  9724. "uniform vec2 uvOffset;",
  9725. "uniform vec2 uvScale;",
  9726. "attribute vec2 position;",
  9727. "attribute vec2 uv;",
  9728. "varying vec2 vUV;",
  9729. "void main() {",
  9730. "vUV = uvOffset + uv * uvScale;",
  9731. "vec2 alignedPosition = position + alignment;",
  9732. "vec2 rotatedPosition;",
  9733. "rotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;",
  9734. "rotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;",
  9735. "vec4 finalPosition;",
  9736. "if( useScreenCoordinates != 0 ) {",
  9737. "finalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );",
  9738. "} else {",
  9739. "finalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
  9740. "finalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );",
  9741. "}",
  9742. "gl_Position = finalPosition;",
  9743. "}"
  9744. ].join( "\n" ),
  9745. fragmentShader: [
  9746. "precision mediump float;",
  9747. "uniform vec3 color;",
  9748. "uniform sampler2D map;",
  9749. "uniform float opacity;",
  9750. "varying vec2 vUV;",
  9751. "void main() {",
  9752. "vec4 texture = texture2D( map, vUV );",
  9753. "gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );",
  9754. "}"
  9755. ].join( "\n" )
  9756. }
  9757. };