ThreeWebGL.js 366 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055
  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. var me = m.elements;
  575. planes[ 0 ].set( me[3] - me[0], me[7] - me[4], me[11] - me[8], me[15] - me[12] );
  576. planes[ 1 ].set( me[3] + me[0], me[7] + me[4], me[11] + me[8], me[15] + me[12] );
  577. planes[ 2 ].set( me[3] + me[1], me[7] + me[5], me[11] + me[9], me[15] + me[13] );
  578. planes[ 3 ].set( me[3] - me[1], me[7] - me[5], me[11] - me[9], me[15] - me[13] );
  579. planes[ 4 ].set( me[3] - me[2], me[7] - me[6], me[11] - me[10], me[15] - me[14] );
  580. planes[ 5 ].set( me[3] + me[2], me[7] + me[6], me[11] + me[10], me[15] + me[14] );
  581. for ( i = 0; i < 6; i ++ ) {
  582. plane = planes[ i ];
  583. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  584. }
  585. };
  586. THREE.Frustum.prototype.contains = function ( object ) {
  587. var distance,
  588. planes = this.planes,
  589. matrix = object.matrixWorld,
  590. me = matrix.elements,
  591. scale = THREE.Frustum.__v1.set( matrix.getColumnX().length(), matrix.getColumnY().length(), matrix.getColumnZ().length() ),
  592. radius = - object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) );
  593. for ( var i = 0; i < 6; i ++ ) {
  594. distance = planes[ i ].x * me[12] + planes[ i ].y * me[13] + planes[ i ].z * me[14] + planes[ i ].w;
  595. if ( distance <= radius ) return false;
  596. }
  597. return true;
  598. };
  599. THREE.Frustum.__v1 = new THREE.Vector3();
  600. /**
  601. * @author mr.doob / http://mrdoob.com/
  602. */
  603. THREE.Ray = function ( origin, direction ) {
  604. this.origin = origin || new THREE.Vector3();
  605. this.direction = direction || new THREE.Vector3();
  606. var precision = 0.0001;
  607. this.setPrecision = function ( value ) {
  608. precision = value;
  609. };
  610. var a = new THREE.Vector3();
  611. var b = new THREE.Vector3();
  612. var c = new THREE.Vector3();
  613. var d = new THREE.Vector3();
  614. var originCopy = new THREE.Vector3();
  615. var directionCopy = new THREE.Vector3();
  616. var vector = new THREE.Vector3();
  617. var normal = new THREE.Vector3();
  618. var intersectPoint = new THREE.Vector3()
  619. this.intersectObject = function ( object ) {
  620. var intersect, intersects = [];
  621. if ( object instanceof THREE.Particle ) {
  622. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  623. if ( distance > object.scale.x ) {
  624. return [];
  625. }
  626. intersect = {
  627. distance: distance,
  628. point: object.position,
  629. face: null,
  630. object: object
  631. };
  632. intersects.push( intersect );
  633. } else if ( object instanceof THREE.Mesh ) {
  634. // Checking boundingSphere
  635. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  636. var scale = THREE.Frustum.__v1.set( object.matrixWorld.getColumnX().length(), object.matrixWorld.getColumnY().length(), object.matrixWorld.getColumnZ().length() );
  637. if ( distance > object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) ) ) {
  638. return intersects;
  639. }
  640. // Checking faces
  641. var f, fl, face, dot, scalar,
  642. geometry = object.geometry,
  643. vertices = geometry.vertices,
  644. objMatrix;
  645. object.matrixRotationWorld.extractRotation( object.matrixWorld );
  646. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  647. face = geometry.faces[ f ];
  648. originCopy.copy( this.origin );
  649. directionCopy.copy( this.direction );
  650. objMatrix = object.matrixWorld;
  651. // determine if ray intersects the plane of the face
  652. // note: this works regardless of the direction of the face normal
  653. vector = objMatrix.multiplyVector3( vector.copy( face.centroid ) ).subSelf( originCopy );
  654. normal = object.matrixRotationWorld.multiplyVector3( normal.copy( face.normal ) );
  655. dot = directionCopy.dot( normal );
  656. // bail if ray and plane are parallel
  657. if ( Math.abs( dot ) < precision ) continue;
  658. // calc distance to plane
  659. scalar = normal.dot( vector ) / dot;
  660. // if negative distance, then plane is behind ray
  661. if ( scalar < 0 ) continue;
  662. if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) {
  663. intersectPoint.add( originCopy, directionCopy.multiplyScalar( scalar ) );
  664. if ( face instanceof THREE.Face3 ) {
  665. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ].position ) );
  666. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ].position ) );
  667. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ].position ) );
  668. if ( pointInFace3( intersectPoint, a, b, c ) ) {
  669. intersect = {
  670. distance: originCopy.distanceTo( intersectPoint ),
  671. point: intersectPoint.clone(),
  672. face: face,
  673. object: object
  674. };
  675. intersects.push( intersect );
  676. }
  677. } else if ( face instanceof THREE.Face4 ) {
  678. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ].position ) );
  679. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ].position ) );
  680. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ].position ) );
  681. d = objMatrix.multiplyVector3( d.copy( vertices[ face.d ].position ) );
  682. if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
  683. intersect = {
  684. distance: originCopy.distanceTo( intersectPoint ),
  685. point: intersectPoint.clone(),
  686. face: face,
  687. object: object
  688. };
  689. intersects.push( intersect );
  690. }
  691. }
  692. }
  693. }
  694. }
  695. return intersects;
  696. }
  697. this.intersectObjects = function ( objects ) {
  698. var intersects = [];
  699. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  700. Array.prototype.push.apply( intersects, this.intersectObject( objects[ i ] ) );
  701. }
  702. intersects.sort( function ( a, b ) { return a.distance - b.distance; } );
  703. return intersects;
  704. };
  705. var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
  706. var dot, intersect, distance;
  707. function distanceFromIntersection( origin, direction, position ) {
  708. v0.sub( position, origin );
  709. dot = v0.dot( direction );
  710. intersect = v1.add( origin, v2.copy( direction ).multiplyScalar( dot ) );
  711. distance = position.distanceTo( intersect );
  712. return distance;
  713. }
  714. // http://www.blackpawn.com/texts/pointinpoly/default.html
  715. var dot00, dot01, dot02, dot11, dot12, invDenom, u, v;
  716. function pointInFace3( p, a, b, c ) {
  717. v0.sub( c, a );
  718. v1.sub( b, a );
  719. v2.sub( p, a );
  720. dot00 = v0.dot( v0 );
  721. dot01 = v0.dot( v1 );
  722. dot02 = v0.dot( v2 );
  723. dot11 = v1.dot( v1 );
  724. dot12 = v1.dot( v2 );
  725. invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 );
  726. u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  727. v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  728. return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 );
  729. }
  730. };/**
  731. * @author mr.doob / http://mrdoob.com/
  732. */
  733. THREE.Rectangle = function () {
  734. var _left, _top, _right, _bottom,
  735. _width, _height, _isEmpty = true;
  736. function resize() {
  737. _width = _right - _left;
  738. _height = _bottom - _top;
  739. }
  740. this.getX = function () {
  741. return _left;
  742. };
  743. this.getY = function () {
  744. return _top;
  745. };
  746. this.getWidth = function () {
  747. return _width;
  748. };
  749. this.getHeight = function () {
  750. return _height;
  751. };
  752. this.getLeft = function() {
  753. return _left;
  754. };
  755. this.getTop = function() {
  756. return _top;
  757. };
  758. this.getRight = function() {
  759. return _right;
  760. };
  761. this.getBottom = function() {
  762. return _bottom;
  763. };
  764. this.set = function ( left, top, right, bottom ) {
  765. _isEmpty = false;
  766. _left = left; _top = top;
  767. _right = right; _bottom = bottom;
  768. resize();
  769. };
  770. this.addPoint = function ( x, y ) {
  771. if ( _isEmpty ) {
  772. _isEmpty = false;
  773. _left = x; _top = y;
  774. _right = x; _bottom = y;
  775. resize();
  776. } else {
  777. _left = _left < x ? _left : x; // Math.min( _left, x );
  778. _top = _top < y ? _top : y; // Math.min( _top, y );
  779. _right = _right > x ? _right : x; // Math.max( _right, x );
  780. _bottom = _bottom > y ? _bottom : y; // Math.max( _bottom, y );
  781. resize();
  782. }
  783. };
  784. this.add3Points = function ( x1, y1, x2, y2, x3, y3 ) {
  785. if (_isEmpty) {
  786. _isEmpty = false;
  787. _left = x1 < x2 ? ( x1 < x3 ? x1 : x3 ) : ( x2 < x3 ? x2 : x3 );
  788. _top = y1 < y2 ? ( y1 < y3 ? y1 : y3 ) : ( y2 < y3 ? y2 : y3 );
  789. _right = x1 > x2 ? ( x1 > x3 ? x1 : x3 ) : ( x2 > x3 ? x2 : x3 );
  790. _bottom = y1 > y2 ? ( y1 > y3 ? y1 : y3 ) : ( y2 > y3 ? y2 : y3 );
  791. resize();
  792. } else {
  793. _left = x1 < x2 ? ( x1 < x3 ? ( x1 < _left ? x1 : _left ) : ( x3 < _left ? x3 : _left ) ) : ( x2 < x3 ? ( x2 < _left ? x2 : _left ) : ( x3 < _left ? x3 : _left ) );
  794. _top = y1 < y2 ? ( y1 < y3 ? ( y1 < _top ? y1 : _top ) : ( y3 < _top ? y3 : _top ) ) : ( y2 < y3 ? ( y2 < _top ? y2 : _top ) : ( y3 < _top ? y3 : _top ) );
  795. _right = x1 > x2 ? ( x1 > x3 ? ( x1 > _right ? x1 : _right ) : ( x3 > _right ? x3 : _right ) ) : ( x2 > x3 ? ( x2 > _right ? x2 : _right ) : ( x3 > _right ? x3 : _right ) );
  796. _bottom = y1 > y2 ? ( y1 > y3 ? ( y1 > _bottom ? y1 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) ) : ( y2 > y3 ? ( y2 > _bottom ? y2 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) );
  797. resize();
  798. };
  799. };
  800. this.addRectangle = function ( r ) {
  801. if ( _isEmpty ) {
  802. _isEmpty = false;
  803. _left = r.getLeft(); _top = r.getTop();
  804. _right = r.getRight(); _bottom = r.getBottom();
  805. resize();
  806. } else {
  807. _left = _left < r.getLeft() ? _left : r.getLeft(); // Math.min(_left, r.getLeft() );
  808. _top = _top < r.getTop() ? _top : r.getTop(); // Math.min(_top, r.getTop() );
  809. _right = _right > r.getRight() ? _right : r.getRight(); // Math.max(_right, r.getRight() );
  810. _bottom = _bottom > r.getBottom() ? _bottom : r.getBottom(); // Math.max(_bottom, r.getBottom() );
  811. resize();
  812. }
  813. };
  814. this.inflate = function ( v ) {
  815. _left -= v; _top -= v;
  816. _right += v; _bottom += v;
  817. resize();
  818. };
  819. this.minSelf = function ( r ) {
  820. _left = _left > r.getLeft() ? _left : r.getLeft(); // Math.max( _left, r.getLeft() );
  821. _top = _top > r.getTop() ? _top : r.getTop(); // Math.max( _top, r.getTop() );
  822. _right = _right < r.getRight() ? _right : r.getRight(); // Math.min( _right, r.getRight() );
  823. _bottom = _bottom < r.getBottom() ? _bottom : r.getBottom(); // Math.min( _bottom, r.getBottom() );
  824. resize();
  825. };
  826. this.intersects = function ( r ) {
  827. // http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/
  828. if ( _right < r.getLeft() ) return false;
  829. if ( _left > r.getRight() ) return false;
  830. if ( _bottom < r.getTop() ) return false;
  831. if ( _top > r.getBottom() ) return false;
  832. return true;
  833. };
  834. this.empty = function () {
  835. _isEmpty = true;
  836. _left = 0; _top = 0;
  837. _right = 0; _bottom = 0;
  838. resize();
  839. };
  840. this.isEmpty = function () {
  841. return _isEmpty;
  842. };
  843. };
  844. /**
  845. * @author alteredq / http://alteredqualia.com/
  846. */
  847. THREE.Math = {
  848. // Clamp value to range <a, b>
  849. clamp: function ( x, a, b ) {
  850. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  851. },
  852. // Clamp value to range <a, inf)
  853. clampBottom: function ( x, a ) {
  854. return x < a ? a : x;
  855. },
  856. // Linear mapping from range <a1, a2> to range <b1, b2>
  857. mapLinear: function ( x, a1, a2, b1, b2 ) {
  858. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  859. },
  860. // Random float from <0, 1> with 16 bits of randomness
  861. // (standard Math.random() creates repetitive patterns when applied over larger space)
  862. random16: function () {
  863. return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
  864. },
  865. // Random integer from <low, high> interval
  866. randInt: function ( low, high ) {
  867. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  868. },
  869. // Random float from <low, high> interval
  870. randFloat: function ( low, high ) {
  871. return low + Math.random() * ( high - low );
  872. },
  873. // Random float from <-range/2, range/2> interval
  874. randFloatSpread: function ( range ) {
  875. return range * ( 0.5 - Math.random() );
  876. },
  877. sign: function ( x ) {
  878. return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
  879. }
  880. };
  881. /**
  882. * @author alteredq / http://alteredqualia.com/
  883. */
  884. THREE.Matrix3 = function () {
  885. this.m = [];
  886. };
  887. THREE.Matrix3.prototype = {
  888. constructor: THREE.Matrix3,
  889. getInverse: function ( matrix ) {
  890. // input: THREE.Matrix4
  891. // ( based on http://code.google.com/p/webgl-mjs/ )
  892. var a11 = matrix.elements[10] * matrix.elements[5] - matrix.elements[6] * matrix.elements[9];
  893. var a21 = - matrix.elements[10] * matrix.elements[1] + matrix.elements[2] * matrix.elements[9];
  894. var a31 = matrix.elements[6] * matrix.elements[1] - matrix.elements[2] * matrix.elements[5];
  895. var a12 = - matrix.elements[10] * matrix.elements[4] + matrix.elements[6] * matrix.elements[8];
  896. var a22 = matrix.elements[10] * matrix.elements[0] - matrix.elements[2] * matrix.elements[8];
  897. var a32 = - matrix.elements[6] * matrix.elements[0] + matrix.elements[2] * matrix.elements[4];
  898. var a13 = matrix.elements[9] * matrix.elements[4] - matrix.elements[5] * matrix.elements[8];
  899. var a23 = - matrix.elements[9] * matrix.elements[0] + matrix.elements[1] * matrix.elements[8];
  900. var a33 = matrix.elements[5] * matrix.elements[0] - matrix.elements[1] * matrix.elements[4];
  901. var det = matrix.elements[0] * a11 + matrix.elements[1] * a12 + matrix.elements[2] * a13;
  902. // no inverse
  903. if ( det === 0 ) {
  904. console.warn( "Matrix3.getInverse(): determinant == 0" );
  905. }
  906. var idet = 1.0 / det;
  907. var m = this.m;
  908. m[ 0 ] = idet * a11; m[ 1 ] = idet * a21; m[ 2 ] = idet * a31;
  909. m[ 3 ] = idet * a12; m[ 4 ] = idet * a22; m[ 5 ] = idet * a32;
  910. m[ 6 ] = idet * a13; m[ 7 ] = idet * a23; m[ 8 ] = idet * a33;
  911. return this;
  912. },
  913. /*
  914. transpose: function () {
  915. var tmp, m = this.m;
  916. tmp = m[1]; m[1] = m[3]; m[3] = tmp;
  917. tmp = m[2]; m[2] = m[6]; m[6] = tmp;
  918. tmp = m[5]; m[5] = m[7]; m[7] = tmp;
  919. return this;
  920. },
  921. */
  922. transposeIntoArray: function ( r ) {
  923. var m = this.m;
  924. r[ 0 ] = m[ 0 ];
  925. r[ 1 ] = m[ 3 ];
  926. r[ 2 ] = m[ 6 ];
  927. r[ 3 ] = m[ 1 ];
  928. r[ 4 ] = m[ 4 ];
  929. r[ 5 ] = m[ 7 ];
  930. r[ 6 ] = m[ 2 ];
  931. r[ 7 ] = m[ 5 ];
  932. r[ 8 ] = m[ 8 ];
  933. return this;
  934. }
  935. };
  936. /**
  937. * @author mr.doob / http://mrdoob.com/
  938. * @author supereggbert / http://www.paulbrunt.co.uk/
  939. * @author philogb / http://blog.thejit.org/
  940. * @author jordi_ros / http://plattsoft.com
  941. * @author D1plo1d / http://github.com/D1plo1d
  942. * @author alteredq / http://alteredqualia.com/
  943. * @author mikael emtinger / http://gomo.se/
  944. * @author timknip / http://www.floorplanner.com/
  945. */
  946. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  947. this.elements = new Float32Array(16);
  948. this.set(
  949. ( n11 !== undefined ) ? n11 : 1, n12 || 0, n13 || 0, n14 || 0,
  950. n21 || 0, ( n22 !== undefined ) ? n22 : 1, n23 || 0, n24 || 0,
  951. n31 || 0, n32 || 0, ( n33 !== undefined ) ? n33 : 1, n34 || 0,
  952. n41 || 0, n42 || 0, n43 || 0, ( n44 !== undefined ) ? n44 : 1
  953. );
  954. };
  955. THREE.Matrix4.prototype = {
  956. constructor: THREE.Matrix4,
  957. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  958. var te = this.elements;
  959. te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
  960. te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
  961. te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
  962. te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
  963. return this;
  964. },
  965. identity: function () {
  966. this.set(
  967. 1, 0, 0, 0,
  968. 0, 1, 0, 0,
  969. 0, 0, 1, 0,
  970. 0, 0, 0, 1
  971. );
  972. return this;
  973. },
  974. copy: function ( m ) {
  975. var me = m.elements;
  976. this.set(
  977. me[0], me[4], me[8], me[12],
  978. me[1], me[5], me[9], me[13],
  979. me[2], me[6], me[10], me[14],
  980. me[3], me[7], me[11], me[15]
  981. );
  982. return this;
  983. },
  984. lookAt: function ( eye, target, up ) {
  985. var te = this.elements;
  986. var x = THREE.Matrix4.__v1;
  987. var y = THREE.Matrix4.__v2;
  988. var z = THREE.Matrix4.__v3;
  989. z.sub( eye, target ).normalize();
  990. if ( z.length() === 0 ) {
  991. z.z = 1;
  992. }
  993. x.cross( up, z ).normalize();
  994. if ( x.length() === 0 ) {
  995. z.x += 0.0001;
  996. x.cross( up, z ).normalize();
  997. }
  998. y.cross( z, x );
  999. te[0] = x.x; te[4] = y.x; te[8] = z.x;
  1000. te[1] = x.y; te[5] = y.y; te[9] = z.y;
  1001. te[2] = x.z; te[6] = y.z; te[10] = z.z;
  1002. return this;
  1003. },
  1004. multiply: function ( a, b ) {
  1005. var ae = a.elements,
  1006. be = b.elements,
  1007. te = this.elements;
  1008. var a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
  1009. var a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
  1010. var a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
  1011. var a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
  1012. var b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
  1013. var b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
  1014. var b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
  1015. var b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
  1016. te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  1017. te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  1018. te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  1019. te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  1020. te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  1021. te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  1022. te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  1023. te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  1024. te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  1025. te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  1026. te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  1027. te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  1028. te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  1029. te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  1030. te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  1031. te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  1032. return this;
  1033. },
  1034. multiplySelf: function ( m ) {
  1035. return this.multiply( this, m );
  1036. },
  1037. multiplyToArray: function ( a, b, r ) {
  1038. var te = this.elements;
  1039. this.multiply( a, b );
  1040. r[ 0 ] = te[0]; r[ 1 ] = te[1]; r[ 2 ] = te[2]; r[ 3 ] = te[3];
  1041. r[ 4 ] = te[4]; r[ 5 ] = te[5]; r[ 6 ] = te[6]; r[ 7 ] = te[7];
  1042. r[ 8 ] = te[8]; r[ 9 ] = te[9]; r[ 10 ] = te[10]; r[ 11 ] = te[11];
  1043. r[ 12 ] = te[12]; r[ 13 ] = te[13]; r[ 14 ] = te[14]; r[ 15 ] = te[15];
  1044. return this;
  1045. },
  1046. multiplyScalar: function ( s ) {
  1047. var te = this.elements;
  1048. te[0] *= s; te[4] *= s; te[8] *= s; te[12] *= s;
  1049. te[1] *= s; te[5] *= s; te[9] *= s; te[13] *= s;
  1050. te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s;
  1051. te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s;
  1052. return this;
  1053. },
  1054. multiplyVector3: function ( v ) {
  1055. var te = this.elements;
  1056. var vx = v.x, vy = v.y, vz = v.z;
  1057. var d = 1 / ( te[3] * vx + te[7] * vy + te[11] * vz + te[15] );
  1058. v.x = ( te[0] * vx + te[4] * vy + te[8] * vz + te[12] ) * d;
  1059. v.y = ( te[1] * vx + te[5] * vy + te[9] * vz + te[13] ) * d;
  1060. v.z = ( te[2] * vx + te[6] * vy + te[10] * vz + te[14] ) * d;
  1061. return v;
  1062. },
  1063. multiplyVector4: function ( v ) {
  1064. var te = this.elements;
  1065. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  1066. v.x = te[0] * vx + te[4] * vy + te[8] * vz + te[12] * vw;
  1067. v.y = te[1] * vx + te[5] * vy + te[9] * vz + te[13] * vw;
  1068. v.z = te[2] * vx + te[6] * vy + te[10] * vz + te[14] * vw;
  1069. v.w = te[3] * vx + te[7] * vy + te[11] * vz + te[15] * vw;
  1070. return v;
  1071. },
  1072. rotateAxis: function ( v ) {
  1073. var te = this.elements;
  1074. var vx = v.x, vy = v.y, vz = v.z;
  1075. v.x = vx * te[0] + vy * te[4] + vz * te[8];
  1076. v.y = vx * te[1] + vy * te[5] + vz * te[9];
  1077. v.z = vx * te[2] + vy * te[6] + vz * te[10];
  1078. v.normalize();
  1079. return v;
  1080. },
  1081. crossVector: function ( a ) {
  1082. var te = this.elements;
  1083. var v = new THREE.Vector4();
  1084. v.x = te[0] * a.x + te[4] * a.y + te[8] * a.z + te[12] * a.w;
  1085. v.y = te[1] * a.x + te[5] * a.y + te[9] * a.z + te[13] * a.w;
  1086. v.z = te[2] * a.x + te[6] * a.y + te[10] * a.z + te[14] * a.w;
  1087. v.w = ( a.w ) ? te[3] * a.x + te[7] * a.y + te[11] * a.z + te[15] * a.w : 1;
  1088. return v;
  1089. },
  1090. determinant: function () {
  1091. var te = this.elements;
  1092. var n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12];
  1093. var n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13];
  1094. var n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14];
  1095. var n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15];
  1096. //TODO: make this more efficient
  1097. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  1098. return (
  1099. n14 * n23 * n32 * n41-
  1100. n13 * n24 * n32 * n41-
  1101. n14 * n22 * n33 * n41+
  1102. n12 * n24 * n33 * n41+
  1103. n13 * n22 * n34 * n41-
  1104. n12 * n23 * n34 * n41-
  1105. n14 * n23 * n31 * n42+
  1106. n13 * n24 * n31 * n42+
  1107. n14 * n21 * n33 * n42-
  1108. n11 * n24 * n33 * n42-
  1109. n13 * n21 * n34 * n42+
  1110. n11 * n23 * n34 * n42+
  1111. n14 * n22 * n31 * n43-
  1112. n12 * n24 * n31 * n43-
  1113. n14 * n21 * n32 * n43+
  1114. n11 * n24 * n32 * n43+
  1115. n12 * n21 * n34 * n43-
  1116. n11 * n22 * n34 * n43-
  1117. n13 * n22 * n31 * n44+
  1118. n12 * n23 * n31 * n44+
  1119. n13 * n21 * n32 * n44-
  1120. n11 * n23 * n32 * n44-
  1121. n12 * n21 * n33 * n44+
  1122. n11 * n22 * n33 * n44
  1123. );
  1124. },
  1125. transpose: function () {
  1126. var te = this.elements;
  1127. var tmp;
  1128. tmp = te[1]; te[1] = te[4]; te[4] = tmp;
  1129. tmp = te[2]; te[2] = te[8]; te[8] = tmp;
  1130. tmp = te[6]; te[6] = te[9]; te[9] = tmp;
  1131. tmp = te[3]; te[3] = te[12]; te[12] = tmp;
  1132. tmp = te[7]; te[7] = te[13]; te[13] = tmp;
  1133. tmp = te[11]; te[11] = te[14]; te[14] = tmp;
  1134. return this;
  1135. },
  1136. flattenToArray: function ( flat ) {
  1137. var te = this.elements;
  1138. flat[ 0 ] = te[0]; flat[ 1 ] = te[1]; flat[ 2 ] = te[2]; flat[ 3 ] = te[3];
  1139. flat[ 4 ] = te[4]; flat[ 5 ] = te[5]; flat[ 6 ] = te[6]; flat[ 7 ] = te[7];
  1140. flat[ 8 ] = te[8]; flat[ 9 ] = te[9]; flat[ 10 ] = te[10]; flat[ 11 ] = te[11];
  1141. flat[ 12 ] = te[12]; flat[ 13 ] = te[13]; flat[ 14 ] = te[14]; flat[ 15 ] = te[15];
  1142. return flat;
  1143. },
  1144. flattenToArrayOffset: function( flat, offset ) {
  1145. var te = this.elements;
  1146. flat[ offset ] = te[0];
  1147. flat[ offset + 1 ] = te[1];
  1148. flat[ offset + 2 ] = te[2];
  1149. flat[ offset + 3 ] = te[3];
  1150. flat[ offset + 4 ] = te[4];
  1151. flat[ offset + 5 ] = te[5];
  1152. flat[ offset + 6 ] = te[6];
  1153. flat[ offset + 7 ] = te[7];
  1154. flat[ offset + 8 ] = te[8];
  1155. flat[ offset + 9 ] = te[9];
  1156. flat[ offset + 10 ] = te[10];
  1157. flat[ offset + 11 ] = te[11];
  1158. flat[ offset + 12 ] = te[12];
  1159. flat[ offset + 13 ] = te[13];
  1160. flat[ offset + 14 ] = te[14];
  1161. flat[ offset + 15 ] = te[15];
  1162. return flat;
  1163. },
  1164. getPosition: function () {
  1165. var te = this.elements;
  1166. return THREE.Matrix4.__v1.set( te[12], te[13], te[14] );
  1167. },
  1168. setPosition: function ( v ) {
  1169. var te = this.elements;
  1170. te[12] = v.x;
  1171. te[13] = v.y;
  1172. te[14] = v.z;
  1173. return this;
  1174. },
  1175. getColumnX: function () {
  1176. var te = this.elements;
  1177. return THREE.Matrix4.__v1.set( te[0], te[1], te[2] );
  1178. },
  1179. getColumnY: function () {
  1180. var te = this.elements;
  1181. return THREE.Matrix4.__v1.set( te[4], te[5], te[6] );
  1182. },
  1183. getColumnZ: function() {
  1184. var te = this.elements;
  1185. return THREE.Matrix4.__v1.set( te[8], te[9], te[10] );
  1186. },
  1187. getInverse: function ( m ) {
  1188. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  1189. var te = this.elements;
  1190. var me = m.elements;
  1191. var n11 = me[0], n12 = me[4], n13 = me[8], n14 = me[12];
  1192. var n21 = me[1], n22 = me[5], n23 = me[9], n24 = me[13];
  1193. var n31 = me[2], n32 = me[6], n33 = me[10], n34 = me[14];
  1194. var n41 = me[3], n42 = me[7], n43 = me[11], n44 = me[15];
  1195. te[0] = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  1196. te[4] = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  1197. te[8] = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  1198. te[12] = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  1199. te[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  1200. te[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  1201. te[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  1202. te[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  1203. te[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  1204. te[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  1205. te[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  1206. te[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  1207. te[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  1208. te[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  1209. te[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  1210. te[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  1211. this.multiplyScalar( 1 / m.determinant() );
  1212. return this;
  1213. },
  1214. setRotationFromEuler: function( v, order ) {
  1215. var te = this.elements;
  1216. var x = v.x, y = v.y, z = v.z;
  1217. var a = Math.cos( x ), b = Math.sin( x );
  1218. var c = Math.cos( y ), d = Math.sin( y );
  1219. var e = Math.cos( z ), f = Math.sin( z );
  1220. switch ( order ) {
  1221. case 'YXZ':
  1222. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  1223. te[0] = ce + df * b;
  1224. te[4] = de * b - cf;
  1225. te[8] = a * d;
  1226. te[1] = a * f;
  1227. te[5] = a * e;
  1228. te[9] = - b;
  1229. te[2] = cf * b - de;
  1230. te[6] = df + ce * b;
  1231. te[10] = a * c;
  1232. break;
  1233. case 'ZXY':
  1234. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  1235. te[0] = ce - df * b;
  1236. te[4] = - a * f;
  1237. te[8] = de + cf * b;
  1238. te[1] = cf + de * b;
  1239. te[5] = a * e;
  1240. te[9] = df - ce * b;
  1241. te[2] = - a * d;
  1242. te[6] = b;
  1243. te[10] = a * c;
  1244. break;
  1245. case 'ZYX':
  1246. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  1247. te[0] = c * e;
  1248. te[4] = be * d - af;
  1249. te[8] = ae * d + bf;
  1250. te[1] = c * f;
  1251. te[5] = bf * d + ae;
  1252. te[9] = af * d - be;
  1253. te[2] = - d;
  1254. te[6] = b * c;
  1255. te[10] = a * c;
  1256. break;
  1257. case 'YZX':
  1258. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  1259. te[0] = c * e;
  1260. te[4] = bd - ac * f;
  1261. te[8] = bc * f + ad;
  1262. te[1] = f;
  1263. te[5] = a * e;
  1264. te[9] = - b * e;
  1265. te[2] = - d * e;
  1266. te[6] = ad * f + bc;
  1267. te[10] = ac - bd * f;
  1268. break;
  1269. case 'XZY':
  1270. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  1271. te[0] = c * e;
  1272. te[4] = - f;
  1273. te[8] = d * e;
  1274. te[1] = ac * f + bd;
  1275. te[5] = a * e;
  1276. te[9] = ad * f - bc;
  1277. te[2] = bc * f - ad;
  1278. te[6] = b * e;
  1279. te[10] = bd * f + ac;
  1280. break;
  1281. default: // 'XYZ'
  1282. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  1283. te[0] = c * e;
  1284. te[4] = - c * f;
  1285. te[8] = d;
  1286. te[1] = af + be * d;
  1287. te[5] = ae - bf * d;
  1288. te[9] = - b * c;
  1289. te[2] = bf - ae * d;
  1290. te[6] = be + af * d;
  1291. te[10] = a * c;
  1292. break;
  1293. }
  1294. return this;
  1295. },
  1296. setRotationFromQuaternion: function( q ) {
  1297. var te = this.elements;
  1298. var x = q.x, y = q.y, z = q.z, w = q.w;
  1299. var x2 = x + x, y2 = y + y, z2 = z + z;
  1300. var xx = x * x2, xy = x * y2, xz = x * z2;
  1301. var yy = y * y2, yz = y * z2, zz = z * z2;
  1302. var wx = w * x2, wy = w * y2, wz = w * z2;
  1303. te[0] = 1 - ( yy + zz );
  1304. te[4] = xy - wz;
  1305. te[8] = xz + wy;
  1306. te[1] = xy + wz;
  1307. te[5] = 1 - ( xx + zz );
  1308. te[9] = yz - wx;
  1309. te[2] = xz - wy;
  1310. te[6] = yz + wx;
  1311. te[10] = 1 - ( xx + yy );
  1312. return this;
  1313. },
  1314. compose: function ( translation, rotation, scale ) {
  1315. var te = this.elements;
  1316. var mRotation = THREE.Matrix4.__m1;
  1317. var mScale = THREE.Matrix4.__m2;
  1318. mRotation.identity();
  1319. mRotation.setRotationFromQuaternion( rotation );
  1320. mScale.makeScale( scale.x, scale.y, scale.z );
  1321. this.multiply( mRotation, mScale );
  1322. te[12] = translation.x;
  1323. te[13] = translation.y;
  1324. te[14] = translation.z;
  1325. return this;
  1326. },
  1327. decompose: function ( translation, rotation, scale ) {
  1328. // grab the axis vectors
  1329. var te = this.elements;
  1330. var x = THREE.Matrix4.__v1;
  1331. var y = THREE.Matrix4.__v2;
  1332. var z = THREE.Matrix4.__v3;
  1333. x.set( te[0], te[1], te[2] );
  1334. y.set( te[4], te[5], te[6] );
  1335. z.set( te[8], te[9], te[10] );
  1336. translation = ( translation instanceof THREE.Vector3 ) ? translation : new THREE.Vector3();
  1337. rotation = ( rotation instanceof THREE.Quaternion ) ? rotation : new THREE.Quaternion();
  1338. scale = ( scale instanceof THREE.Vector3 ) ? scale : new THREE.Vector3();
  1339. scale.x = x.length();
  1340. scale.y = y.length();
  1341. scale.z = z.length();
  1342. translation.x = te[12];
  1343. translation.y = te[13];
  1344. translation.z = te[14];
  1345. // scale the rotation part
  1346. var matrix = THREE.Matrix4.__m1;
  1347. matrix.copy( this );
  1348. matrix.elements[0] /= scale.x;
  1349. matrix.elements[1] /= scale.x;
  1350. matrix.elements[2] /= scale.x;
  1351. matrix.elements[4] /= scale.y;
  1352. matrix.elements[5] /= scale.y;
  1353. matrix.elements[6] /= scale.y;
  1354. matrix.elements[8] /= scale.z;
  1355. matrix.elements[9] /= scale.z;
  1356. matrix.elements[10] /= scale.z;
  1357. rotation.setFromRotationMatrix( matrix );
  1358. return [ translation, rotation, scale ];
  1359. },
  1360. extractPosition: function ( m ) {
  1361. var te = this.elements;
  1362. var me = m.elements;
  1363. te[12] = me[12];
  1364. te[13] = me[13];
  1365. te[14] = me[14];
  1366. return this;
  1367. },
  1368. extractRotation: function ( m ) {
  1369. var te = this.elements;
  1370. var me = m.elements;
  1371. var vector = THREE.Matrix4.__v1;
  1372. var scaleX = 1 / vector.set( me[0], me[1], me[2] ).length();
  1373. var scaleY = 1 / vector.set( me[4], me[5], me[6] ).length();
  1374. var scaleZ = 1 / vector.set( me[8], me[9], me[10] ).length();
  1375. te[0] = me[0] * scaleX;
  1376. te[1] = me[1] * scaleX;
  1377. te[2] = me[2] * scaleX;
  1378. te[4] = me[4] * scaleY;
  1379. te[5] = me[5] * scaleY;
  1380. te[6] = me[6] * scaleY;
  1381. te[8] = me[8] * scaleZ;
  1382. te[9] = me[9] * scaleZ;
  1383. te[10] = me[10] * scaleZ;
  1384. return this;
  1385. },
  1386. //
  1387. translate: function ( v ) {
  1388. var te = this.elements;
  1389. var x = v.x, y = v.y, z = v.z;
  1390. te[12] = te[0] * x + te[4] * y + te[8] * z + te[12];
  1391. te[13] = te[1] * x + te[5] * y + te[9] * z + te[13];
  1392. te[14] = te[2] * x + te[6] * y + te[10] * z + te[14];
  1393. te[15] = te[3] * x + te[7] * y + te[11] * z + te[15];
  1394. return this;
  1395. },
  1396. rotateX: function ( angle ) {
  1397. var te = this.elements;
  1398. var m12 = te[4];
  1399. var m22 = te[5];
  1400. var m32 = te[6];
  1401. var m42 = te[7];
  1402. var m13 = te[8];
  1403. var m23 = te[9];
  1404. var m33 = te[10];
  1405. var m43 = te[11];
  1406. var c = Math.cos( angle );
  1407. var s = Math.sin( angle );
  1408. te[4] = c * m12 + s * m13;
  1409. te[5] = c * m22 + s * m23;
  1410. te[6] = c * m32 + s * m33;
  1411. te[7] = c * m42 + s * m43;
  1412. te[8] = c * m13 - s * m12;
  1413. te[9] = c * m23 - s * m22;
  1414. te[10] = c * m33 - s * m32;
  1415. te[11] = c * m43 - s * m42;
  1416. return this;
  1417. },
  1418. rotateY: function ( angle ) {
  1419. var te = this.elements;
  1420. var m11 = te[0];
  1421. var m21 = te[1];
  1422. var m31 = te[2];
  1423. var m41 = te[3];
  1424. var m13 = te[8];
  1425. var m23 = te[9];
  1426. var m33 = te[10];
  1427. var m43 = te[11];
  1428. var c = Math.cos( angle );
  1429. var s = Math.sin( angle );
  1430. te[0] = c * m11 - s * m13;
  1431. te[1] = c * m21 - s * m23;
  1432. te[2] = c * m31 - s * m33;
  1433. te[3] = c * m41 - s * m43;
  1434. te[8] = c * m13 + s * m11;
  1435. te[9] = c * m23 + s * m21;
  1436. te[10] = c * m33 + s * m31;
  1437. te[11] = c * m43 + s * m41;
  1438. return this;
  1439. },
  1440. rotateZ: function ( angle ) {
  1441. var te = this.elements;
  1442. var m11 = te[0];
  1443. var m21 = te[1];
  1444. var m31 = te[2];
  1445. var m41 = te[3];
  1446. var m12 = te[4];
  1447. var m22 = te[5];
  1448. var m32 = te[6];
  1449. var m42 = te[7];
  1450. var c = Math.cos( angle );
  1451. var s = Math.sin( angle );
  1452. te[0] = c * m11 + s * m12;
  1453. te[1] = c * m21 + s * m22;
  1454. te[2] = c * m31 + s * m32;
  1455. te[3] = c * m41 + s * m42;
  1456. te[4] = c * m12 - s * m11;
  1457. te[5] = c * m22 - s * m21;
  1458. te[6] = c * m32 - s * m31;
  1459. te[7] = c * m42 - s * m41;
  1460. return this;
  1461. },
  1462. rotateByAxis: function ( axis, angle ) {
  1463. var te = this.elements;
  1464. // optimize by checking axis
  1465. if ( axis.x === 1 && axis.y === 0 && axis.z === 0 ) {
  1466. return this.rotateX( angle );
  1467. } else if ( axis.x === 0 && axis.y === 1 && axis.z === 0 ) {
  1468. return this.rotateY( angle );
  1469. } else if ( axis.x === 0 && axis.y === 0 && axis.z === 1 ) {
  1470. return this.rotateZ( angle );
  1471. }
  1472. var x = axis.x, y = axis.y, z = axis.z;
  1473. var n = Math.sqrt(x * x + y * y + z * z);
  1474. x /= n;
  1475. y /= n;
  1476. z /= n;
  1477. var xx = x * x, yy = y * y, zz = z * z;
  1478. var c = Math.cos( angle );
  1479. var s = Math.sin( angle );
  1480. var oneMinusCosine = 1 - c;
  1481. var xy = x * y * oneMinusCosine;
  1482. var xz = x * z * oneMinusCosine;
  1483. var yz = y * z * oneMinusCosine;
  1484. var xs = x * s;
  1485. var ys = y * s;
  1486. var zs = z * s;
  1487. var r11 = xx + (1 - xx) * c;
  1488. var r21 = xy + zs;
  1489. var r31 = xz - ys;
  1490. var r12 = xy - zs;
  1491. var r22 = yy + (1 - yy) * c;
  1492. var r32 = yz + xs;
  1493. var r13 = xz + ys;
  1494. var r23 = yz - xs;
  1495. var r33 = zz + (1 - zz) * c;
  1496. var m11 = te[0], m21 = te[1], m31 = te[2], m41 = te[3];
  1497. var m12 = te[4], m22 = te[5], m32 = te[6], m42 = te[7];
  1498. var m13 = te[8], m23 = te[9], m33 = te[10], m43 = te[11];
  1499. var m14 = te[12], m24 = te[13], m34 = te[14], m44 = te[15];
  1500. te[0] = r11 * m11 + r21 * m12 + r31 * m13;
  1501. te[1] = r11 * m21 + r21 * m22 + r31 * m23;
  1502. te[2] = r11 * m31 + r21 * m32 + r31 * m33;
  1503. te[3] = r11 * m41 + r21 * m42 + r31 * m43;
  1504. te[4] = r12 * m11 + r22 * m12 + r32 * m13;
  1505. te[5] = r12 * m21 + r22 * m22 + r32 * m23;
  1506. te[6] = r12 * m31 + r22 * m32 + r32 * m33;
  1507. te[7] = r12 * m41 + r22 * m42 + r32 * m43;
  1508. te[8] = r13 * m11 + r23 * m12 + r33 * m13;
  1509. te[9] = r13 * m21 + r23 * m22 + r33 * m23;
  1510. te[10] = r13 * m31 + r23 * m32 + r33 * m33;
  1511. te[11] = r13 * m41 + r23 * m42 + r33 * m43;
  1512. return this;
  1513. },
  1514. scale: function ( v ) {
  1515. var te = this.elements;
  1516. var x = v.x, y = v.y, z = v.z;
  1517. te[0] *= x; te[4] *= y; te[8] *= z;
  1518. te[1] *= x; te[5] *= y; te[9] *= z;
  1519. te[2] *= x; te[6] *= y; te[10] *= z;
  1520. te[3] *= x; te[7] *= y; te[11] *= z;
  1521. return this;
  1522. },
  1523. //
  1524. makeTranslation: function ( x, y, z ) {
  1525. this.set(
  1526. 1, 0, 0, x,
  1527. 0, 1, 0, y,
  1528. 0, 0, 1, z,
  1529. 0, 0, 0, 1
  1530. );
  1531. return this;
  1532. },
  1533. makeRotationX: function ( theta ) {
  1534. var c = Math.cos( theta ), s = Math.sin( theta );
  1535. this.set(
  1536. 1, 0, 0, 0,
  1537. 0, c, -s, 0,
  1538. 0, s, c, 0,
  1539. 0, 0, 0, 1
  1540. );
  1541. return this;
  1542. },
  1543. makeRotationY: function ( theta ) {
  1544. var c = Math.cos( theta ), s = Math.sin( theta );
  1545. this.set(
  1546. c, 0, s, 0,
  1547. 0, 1, 0, 0,
  1548. -s, 0, c, 0,
  1549. 0, 0, 0, 1
  1550. );
  1551. return this;
  1552. },
  1553. makeRotationZ: function ( theta ) {
  1554. var c = Math.cos( theta ), s = Math.sin( theta );
  1555. this.set(
  1556. c, -s, 0, 0,
  1557. s, c, 0, 0,
  1558. 0, 0, 1, 0,
  1559. 0, 0, 0, 1
  1560. );
  1561. return this;
  1562. },
  1563. makeRotationAxis: function ( axis, angle ) {
  1564. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  1565. var c = Math.cos( angle );
  1566. var s = Math.sin( angle );
  1567. var t = 1 - c;
  1568. var x = axis.x, y = axis.y, z = axis.z;
  1569. var tx = t * x, ty = t * y;
  1570. this.set(
  1571. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  1572. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  1573. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  1574. 0, 0, 0, 1
  1575. );
  1576. return this;
  1577. },
  1578. makeScale: function ( x, y, z ) {
  1579. this.set(
  1580. x, 0, 0, 0,
  1581. 0, y, 0, 0,
  1582. 0, 0, z, 0,
  1583. 0, 0, 0, 1
  1584. );
  1585. return this;
  1586. },
  1587. makeFrustum: function ( left, right, bottom, top, near, far ) {
  1588. var te = this.elements;
  1589. var x = 2 * near / ( right - left );
  1590. var y = 2 * near / ( top - bottom );
  1591. var a = ( right + left ) / ( right - left );
  1592. var b = ( top + bottom ) / ( top - bottom );
  1593. var c = - ( far + near ) / ( far - near );
  1594. var d = - 2 * far * near / ( far - near );
  1595. te[0] = x; te[4] = 0; te[8] = a; te[12] = 0;
  1596. te[1] = 0; te[5] = y; te[9] = b; te[13] = 0;
  1597. te[2] = 0; te[6] = 0; te[10] = c; te[14] = d;
  1598. te[3] = 0; te[7] = 0; te[11] = - 1; te[15] = 0;
  1599. return this;
  1600. },
  1601. makePerspective: function ( fov, aspect, near, far ) {
  1602. var ymax = near * Math.tan( fov * Math.PI / 360 );
  1603. var ymin = - ymax;
  1604. var xmin = ymin * aspect;
  1605. var xmax = ymax * aspect;
  1606. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  1607. },
  1608. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  1609. var te = this.elements;
  1610. var w = right - left;
  1611. var h = top - bottom;
  1612. var p = far - near;
  1613. var x = ( right + left ) / w;
  1614. var y = ( top + bottom ) / h;
  1615. var z = ( far + near ) / p;
  1616. te[0] = 2 / w; te[4] = 0; te[8] = 0; te[12] = -x;
  1617. te[1] = 0; te[5] = 2 / h; te[9] = 0; te[13] = -y;
  1618. te[2] = 0; te[6] = 0; te[10] = -2 / p; te[14] = -z;
  1619. te[3] = 0; te[7] = 0; te[11] = 0; te[15] = 1;
  1620. return this;
  1621. },
  1622. clone: function () {
  1623. var te = this.elements;
  1624. return new THREE.Matrix4(
  1625. te[0], te[4], te[8], te[12],
  1626. te[1], te[5], te[9], te[13],
  1627. te[2], te[6], te[10], te[14],
  1628. te[3], te[7], te[11], te[15]
  1629. );
  1630. }
  1631. };
  1632. THREE.Matrix4.__v1 = new THREE.Vector3();
  1633. THREE.Matrix4.__v2 = new THREE.Vector3();
  1634. THREE.Matrix4.__v3 = new THREE.Vector3();
  1635. THREE.Matrix4.__m1 = new THREE.Matrix4();
  1636. THREE.Matrix4.__m2 = new THREE.Matrix4();
  1637. /**
  1638. * @author mr.doob / http://mrdoob.com/
  1639. * @author mikael emtinger / http://gomo.se/
  1640. * @author alteredq / http://alteredqualia.com/
  1641. */
  1642. THREE.Object3D = function () {
  1643. this.id = THREE.Object3DCount ++;
  1644. this.name = '';
  1645. this.parent = undefined;
  1646. this.children = [];
  1647. this.up = new THREE.Vector3( 0, 1, 0 );
  1648. this.position = new THREE.Vector3();
  1649. this.rotation = new THREE.Vector3();
  1650. this.eulerOrder = 'XYZ';
  1651. this.scale = new THREE.Vector3( 1, 1, 1 );
  1652. this.doubleSided = false;
  1653. this.flipSided = false;
  1654. this.renderDepth = null;
  1655. this.rotationAutoUpdate = true;
  1656. this.matrix = new THREE.Matrix4();
  1657. this.matrixWorld = new THREE.Matrix4();
  1658. this.matrixRotationWorld = new THREE.Matrix4();
  1659. this.matrixAutoUpdate = true;
  1660. this.matrixWorldNeedsUpdate = true;
  1661. this.quaternion = new THREE.Quaternion();
  1662. this.useQuaternion = false;
  1663. this.boundRadius = 0.0;
  1664. this.boundRadiusScale = 1.0;
  1665. this.visible = true;
  1666. this.castShadow = false;
  1667. this.receiveShadow = false;
  1668. this.frustumCulled = true;
  1669. this._vector = new THREE.Vector3();
  1670. };
  1671. THREE.Object3D.prototype = {
  1672. constructor: THREE.Object3D,
  1673. applyMatrix: function ( matrix ) {
  1674. this.matrix.multiply( matrix, this.matrix );
  1675. this.scale.getScaleFromMatrix( this.matrix );
  1676. this.rotation.getRotationFromMatrix( this.matrix, this.scale );
  1677. this.position.getPositionFromMatrix( this.matrix );
  1678. },
  1679. translate: function ( distance, axis ) {
  1680. this.matrix.rotateAxis( axis );
  1681. this.position.addSelf( axis.multiplyScalar( distance ) );
  1682. },
  1683. translateX: function ( distance ) {
  1684. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  1685. },
  1686. translateY: function ( distance ) {
  1687. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  1688. },
  1689. translateZ: function ( distance ) {
  1690. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  1691. },
  1692. lookAt: function ( vector ) {
  1693. // TODO: Add hierarchy support.
  1694. this.matrix.lookAt( vector, this.position, this.up );
  1695. if ( this.rotationAutoUpdate ) {
  1696. this.rotation.getRotationFromMatrix( this.matrix );
  1697. }
  1698. },
  1699. add: function ( object ) {
  1700. if ( object === this ) {
  1701. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  1702. return;
  1703. }
  1704. if ( this.children.indexOf( object ) === - 1 ) {
  1705. if ( object.parent !== undefined ) {
  1706. object.parent.remove( object );
  1707. }
  1708. object.parent = this;
  1709. this.children.push( object );
  1710. // add to scene
  1711. var scene = this;
  1712. while ( scene.parent !== undefined ) {
  1713. scene = scene.parent;
  1714. }
  1715. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  1716. scene.__addObject( object );
  1717. }
  1718. }
  1719. },
  1720. remove: function ( object ) {
  1721. var index = this.children.indexOf( object );
  1722. if ( index !== - 1 ) {
  1723. object.parent = undefined;
  1724. this.children.splice( index, 1 );
  1725. // remove from scene
  1726. var scene = this;
  1727. while ( scene.parent !== undefined ) {
  1728. scene = scene.parent;
  1729. }
  1730. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  1731. scene.__removeObject( object );
  1732. }
  1733. }
  1734. },
  1735. getChildByName: function ( name, recursive ) {
  1736. var c, cl, child;
  1737. for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
  1738. child = this.children[ c ];
  1739. if ( child.name === name ) {
  1740. return child;
  1741. }
  1742. if ( recursive ) {
  1743. child = child.getChildByName( name, recursive );
  1744. if ( child !== undefined ) {
  1745. return child;
  1746. }
  1747. }
  1748. }
  1749. return undefined;
  1750. },
  1751. updateMatrix: function () {
  1752. this.matrix.setPosition( this.position );
  1753. if ( this.useQuaternion ) {
  1754. this.matrix.setRotationFromQuaternion( this.quaternion );
  1755. } else {
  1756. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  1757. }
  1758. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  1759. this.matrix.scale( this.scale );
  1760. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  1761. }
  1762. this.matrixWorldNeedsUpdate = true;
  1763. },
  1764. updateMatrixWorld: function ( force ) {
  1765. this.matrixAutoUpdate && this.updateMatrix();
  1766. // update matrixWorld
  1767. if ( this.matrixWorldNeedsUpdate || force ) {
  1768. if ( this.parent ) {
  1769. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  1770. } else {
  1771. this.matrixWorld.copy( this.matrix );
  1772. }
  1773. this.matrixWorldNeedsUpdate = false;
  1774. force = true;
  1775. }
  1776. // update children
  1777. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  1778. this.children[ i ].updateMatrixWorld( force );
  1779. }
  1780. }
  1781. };
  1782. THREE.Object3DCount = 0;
  1783. /**
  1784. * @author mr.doob / http://mrdoob.com/
  1785. * @author supereggbert / http://www.paulbrunt.co.uk/
  1786. * @author julianwa / https://github.com/julianwa
  1787. */
  1788. THREE.Projector = function() {
  1789. var _object, _objectCount, _objectPool = [],
  1790. _vertex, _vertexCount, _vertexPool = [],
  1791. _face, _face3Count, _face3Pool = [], _face4Count, _face4Pool = [],
  1792. _line, _lineCount, _linePool = [],
  1793. _particle, _particleCount, _particlePool = [],
  1794. _renderData = { objects: [], sprites: [], lights: [], elements: [] },
  1795. _vector3 = new THREE.Vector3(),
  1796. _vector4 = new THREE.Vector4(),
  1797. _projScreenMatrix = new THREE.Matrix4(),
  1798. _projScreenobjectMatrixWorld = new THREE.Matrix4(),
  1799. _frustum = new THREE.Frustum(),
  1800. _clippedVertex1PositionScreen = new THREE.Vector4(),
  1801. _clippedVertex2PositionScreen = new THREE.Vector4(),
  1802. _face3VertexNormals;
  1803. this.projectVector = function ( vector, camera ) {
  1804. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1805. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1806. _projScreenMatrix.multiplyVector3( vector );
  1807. return vector;
  1808. };
  1809. this.unprojectVector = function ( vector, camera ) {
  1810. camera.projectionMatrixInverse.getInverse( camera.projectionMatrix );
  1811. _projScreenMatrix.multiply( camera.matrixWorld, camera.projectionMatrixInverse );
  1812. _projScreenMatrix.multiplyVector3( vector );
  1813. return vector;
  1814. };
  1815. this.pickingRay = function ( vector, camera ) {
  1816. var end, ray, t;
  1817. // set two vectors with opposing z values
  1818. vector.z = -1.0;
  1819. end = new THREE.Vector3( vector.x, vector.y, 1.0 );
  1820. this.unprojectVector( vector, camera );
  1821. this.unprojectVector( end, camera );
  1822. // find direction from vector to end
  1823. end.subSelf( vector ).normalize();
  1824. return new THREE.Ray( vector, end );
  1825. };
  1826. this.projectGraph = function ( root, sort ) {
  1827. _objectCount = 0;
  1828. _renderData.objects.length = 0;
  1829. _renderData.sprites.length = 0;
  1830. _renderData.lights.length = 0;
  1831. var projectObject = function ( object ) {
  1832. if ( object.visible === false ) return;
  1833. if ( ( object instanceof THREE.Mesh || object instanceof THREE.Line ) &&
  1834. ( object.frustumCulled === false || _frustum.contains( object ) ) ) {
  1835. _vector3.copy( object.matrixWorld.getPosition() );
  1836. _projScreenMatrix.multiplyVector3( _vector3 );
  1837. _object = getNextObjectInPool();
  1838. _object.object = object;
  1839. _object.z = _vector3.z;
  1840. _renderData.objects.push( _object );
  1841. } else if ( object instanceof THREE.Sprite || object instanceof THREE.Particle ) {
  1842. _vector3.copy( object.matrixWorld.getPosition() );
  1843. _projScreenMatrix.multiplyVector3( _vector3 );
  1844. _object = getNextObjectInPool();
  1845. _object.object = object;
  1846. _object.z = _vector3.z;
  1847. _renderData.sprites.push( _object );
  1848. } else if ( object instanceof THREE.Light ) {
  1849. _renderData.lights.push( object );
  1850. }
  1851. for ( var c = 0, cl = object.children.length; c < cl; c ++ ) {
  1852. projectObject( object.children[ c ] );
  1853. }
  1854. };
  1855. projectObject( root );
  1856. sort && _renderData.objects.sort( painterSort );
  1857. return _renderData;
  1858. };
  1859. this.projectScene = function ( scene, camera, sort ) {
  1860. var near = camera.near, far = camera.far, visible = false,
  1861. o, ol, v, vl, f, fl, n, nl, c, cl, u, ul, object,
  1862. objectMatrixWorld, objectMatrixWorldRotation,
  1863. geometry, geometryMaterials, vertices, vertex, vertexPositionScreen,
  1864. faces, face, faceVertexNormals, normal, faceVertexUvs, uvs,
  1865. v1, v2, v3, v4;
  1866. _face3Count = 0;
  1867. _face4Count = 0;
  1868. _lineCount = 0;
  1869. _particleCount = 0;
  1870. _renderData.elements.length = 0;
  1871. if ( camera.parent === undefined ) {
  1872. console.warn( 'DEPRECATED: Camera hasn\'t been added to a Scene. Adding it...' );
  1873. scene.add( camera );
  1874. }
  1875. scene.updateMatrixWorld();
  1876. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1877. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1878. _frustum.setFromMatrix( _projScreenMatrix );
  1879. _renderData = this.projectGraph( scene, false );
  1880. for ( o = 0, ol = _renderData.objects.length; o < ol; o++ ) {
  1881. object = _renderData.objects[ o ].object;
  1882. objectMatrixWorld = object.matrixWorld;
  1883. _vertexCount = 0;
  1884. if ( object instanceof THREE.Mesh ) {
  1885. geometry = object.geometry;
  1886. geometryMaterials = object.geometry.materials;
  1887. vertices = geometry.vertices;
  1888. faces = geometry.faces;
  1889. faceVertexUvs = geometry.faceVertexUvs;
  1890. objectMatrixWorldRotation = object.matrixRotationWorld.extractRotation( objectMatrixWorld );
  1891. for ( v = 0, vl = vertices.length; v < vl; v ++ ) {
  1892. _vertex = getNextVertexInPool();
  1893. _vertex.positionWorld.copy( vertices[ v ].position );
  1894. objectMatrixWorld.multiplyVector3( _vertex.positionWorld );
  1895. _vertex.positionScreen.copy( _vertex.positionWorld );
  1896. _projScreenMatrix.multiplyVector4( _vertex.positionScreen );
  1897. _vertex.positionScreen.x /= _vertex.positionScreen.w;
  1898. _vertex.positionScreen.y /= _vertex.positionScreen.w;
  1899. _vertex.visible = _vertex.positionScreen.z > near && _vertex.positionScreen.z < far;
  1900. }
  1901. for ( f = 0, fl = faces.length; f < fl; f ++ ) {
  1902. face = faces[ f ];
  1903. if ( face instanceof THREE.Face3 ) {
  1904. v1 = _vertexPool[ face.a ];
  1905. v2 = _vertexPool[ face.b ];
  1906. v3 = _vertexPool[ face.c ];
  1907. if ( v1.visible && v2.visible && v3.visible ) {
  1908. visible = ( ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  1909. ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
  1910. if ( object.doubleSided || visible != object.flipSided ) {
  1911. _face = getNextFace3InPool();
  1912. _face.v1.copy( v1 );
  1913. _face.v2.copy( v2 );
  1914. _face.v3.copy( v3 );
  1915. } else {
  1916. continue;
  1917. }
  1918. } else {
  1919. continue;
  1920. }
  1921. } else if ( face instanceof THREE.Face4 ) {
  1922. v1 = _vertexPool[ face.a ];
  1923. v2 = _vertexPool[ face.b ];
  1924. v3 = _vertexPool[ face.c ];
  1925. v4 = _vertexPool[ face.d ];
  1926. if ( v1.visible && v2.visible && v3.visible && v4.visible ) {
  1927. visible = ( v4.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  1928. ( v4.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ||
  1929. ( v2.positionScreen.x - v3.positionScreen.x ) * ( v4.positionScreen.y - v3.positionScreen.y ) -
  1930. ( v2.positionScreen.y - v3.positionScreen.y ) * ( v4.positionScreen.x - v3.positionScreen.x ) < 0;
  1931. if ( object.doubleSided || visible != object.flipSided ) {
  1932. _face = getNextFace4InPool();
  1933. _face.v1.copy( v1 );
  1934. _face.v2.copy( v2 );
  1935. _face.v3.copy( v3 );
  1936. _face.v4.copy( v4 );
  1937. } else {
  1938. continue;
  1939. }
  1940. } else {
  1941. continue;
  1942. }
  1943. }
  1944. _face.normalWorld.copy( face.normal );
  1945. if ( !visible && ( object.flipSided || object.doubleSided ) ) _face.normalWorld.negate();
  1946. objectMatrixWorldRotation.multiplyVector3( _face.normalWorld );
  1947. _face.centroidWorld.copy( face.centroid );
  1948. objectMatrixWorld.multiplyVector3( _face.centroidWorld );
  1949. _face.centroidScreen.copy( _face.centroidWorld );
  1950. _projScreenMatrix.multiplyVector3( _face.centroidScreen );
  1951. faceVertexNormals = face.vertexNormals;
  1952. for ( n = 0, nl = faceVertexNormals.length; n < nl; n ++ ) {
  1953. normal = _face.vertexNormalsWorld[ n ];
  1954. normal.copy( faceVertexNormals[ n ] );
  1955. if ( !visible && ( object.flipSided || object.doubleSided ) ) normal.negate();
  1956. objectMatrixWorldRotation.multiplyVector3( normal );
  1957. }
  1958. for ( c = 0, cl = faceVertexUvs.length; c < cl; c ++ ) {
  1959. uvs = faceVertexUvs[ c ][ f ];
  1960. if ( !uvs ) continue;
  1961. for ( u = 0, ul = uvs.length; u < ul; u ++ ) {
  1962. _face.uvs[ c ][ u ] = uvs[ u ];
  1963. }
  1964. }
  1965. _face.material = object.material;
  1966. _face.faceMaterial = face.materialIndex !== null ? geometryMaterials[ face.materialIndex ] : null;
  1967. _face.z = _face.centroidScreen.z;
  1968. _renderData.elements.push( _face );
  1969. }
  1970. } else if ( object instanceof THREE.Line ) {
  1971. _projScreenobjectMatrixWorld.multiply( _projScreenMatrix, objectMatrixWorld );
  1972. vertices = object.geometry.vertices;
  1973. v1 = getNextVertexInPool();
  1974. v1.positionScreen.copy( vertices[ 0 ].position );
  1975. _projScreenobjectMatrixWorld.multiplyVector4( v1.positionScreen );
  1976. // Handle LineStrip and LinePieces
  1977. var step = object.type === THREE.LinePieces ? 2 : 1;
  1978. for ( v = 1, vl = vertices.length; v < vl; v ++ ) {
  1979. v1 = getNextVertexInPool();
  1980. v1.positionScreen.copy( vertices[ v ].position );
  1981. _projScreenobjectMatrixWorld.multiplyVector4( v1.positionScreen );
  1982. if ( ( v + 1 ) % step > 0 ) continue;
  1983. v2 = _vertexPool[ _vertexCount - 2 ];
  1984. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  1985. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  1986. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) ) {
  1987. // Perform the perspective divide
  1988. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  1989. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  1990. _line = getNextLineInPool();
  1991. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  1992. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  1993. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  1994. _line.material = object.material;
  1995. _renderData.elements.push( _line );
  1996. }
  1997. }
  1998. }
  1999. }
  2000. for ( o = 0, ol = _renderData.sprites.length; o < ol; o++ ) {
  2001. object = _renderData.sprites[ o ].object;
  2002. objectMatrixWorld = object.matrixWorld;
  2003. if ( object instanceof THREE.Particle ) {
  2004. _vector4.set( objectMatrixWorld.elements[12], objectMatrixWorld.elements[13], objectMatrixWorld.elements[14], 1 );
  2005. _projScreenMatrix.multiplyVector4( _vector4 );
  2006. _vector4.z /= _vector4.w;
  2007. if ( _vector4.z > 0 && _vector4.z < 1 ) {
  2008. _particle = getNextParticleInPool();
  2009. _particle.x = _vector4.x / _vector4.w;
  2010. _particle.y = _vector4.y / _vector4.w;
  2011. _particle.z = _vector4.z;
  2012. _particle.rotation = object.rotation.z;
  2013. _particle.scale.x = object.scale.x * Math.abs( _particle.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) );
  2014. _particle.scale.y = object.scale.y * Math.abs( _particle.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) );
  2015. _particle.material = object.material;
  2016. _renderData.elements.push( _particle );
  2017. }
  2018. }
  2019. }
  2020. sort && _renderData.elements.sort( painterSort );
  2021. return _renderData;
  2022. };
  2023. // Pools
  2024. function getNextObjectInPool() {
  2025. var object = _objectPool[ _objectCount ] = _objectPool[ _objectCount ] || new THREE.RenderableObject();
  2026. _objectCount ++;
  2027. return object;
  2028. }
  2029. function getNextVertexInPool() {
  2030. var vertex = _vertexPool[ _vertexCount ] = _vertexPool[ _vertexCount ] || new THREE.RenderableVertex();
  2031. _vertexCount ++;
  2032. return vertex;
  2033. }
  2034. function getNextFace3InPool() {
  2035. var face = _face3Pool[ _face3Count ] = _face3Pool[ _face3Count ] || new THREE.RenderableFace3();
  2036. _face3Count ++;
  2037. return face;
  2038. }
  2039. function getNextFace4InPool() {
  2040. var face = _face4Pool[ _face4Count ] = _face4Pool[ _face4Count ] || new THREE.RenderableFace4();
  2041. _face4Count ++;
  2042. return face;
  2043. }
  2044. function getNextLineInPool() {
  2045. var line = _linePool[ _lineCount ] = _linePool[ _lineCount ] || new THREE.RenderableLine();
  2046. _lineCount ++;
  2047. return line;
  2048. }
  2049. function getNextParticleInPool() {
  2050. var particle = _particlePool[ _particleCount ] = _particlePool[ _particleCount ] || new THREE.RenderableParticle();
  2051. _particleCount ++;
  2052. return particle;
  2053. }
  2054. //
  2055. function painterSort( a, b ) {
  2056. return b.z - a.z;
  2057. }
  2058. function clipLine( s1, s2 ) {
  2059. var alpha1 = 0, alpha2 = 1,
  2060. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  2061. // Z = -1 and Z = +1, respectively.
  2062. bc1near = s1.z + s1.w,
  2063. bc2near = s2.z + s2.w,
  2064. bc1far = - s1.z + s1.w,
  2065. bc2far = - s2.z + s2.w;
  2066. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  2067. // Both vertices lie entirely within all clip planes.
  2068. return true;
  2069. } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
  2070. // Both vertices lie entirely outside one of the clip planes.
  2071. return false;
  2072. } else {
  2073. // The line segment spans at least one clip plane.
  2074. if ( bc1near < 0 ) {
  2075. // v1 lies outside the near plane, v2 inside
  2076. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  2077. } else if ( bc2near < 0 ) {
  2078. // v2 lies outside the near plane, v1 inside
  2079. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  2080. }
  2081. if ( bc1far < 0 ) {
  2082. // v1 lies outside the far plane, v2 inside
  2083. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  2084. } else if ( bc2far < 0 ) {
  2085. // v2 lies outside the far plane, v2 inside
  2086. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  2087. }
  2088. if ( alpha2 < alpha1 ) {
  2089. // The line segment spans two boundaries, but is outside both of them.
  2090. // (This can't happen when we're only clipping against just near/far but good
  2091. // to leave the check here for future usage if other clip planes are added.)
  2092. return false;
  2093. } else {
  2094. // Update the s1 and s2 vertices to match the clipped line segment.
  2095. s1.lerpSelf( s2, alpha1 );
  2096. s2.lerpSelf( s1, 1 - alpha2 );
  2097. return true;
  2098. }
  2099. }
  2100. }
  2101. };
  2102. /**
  2103. * @author mikael emtinger / http://gomo.se/
  2104. * @author alteredq / http://alteredqualia.com/
  2105. */
  2106. THREE.Quaternion = function( x, y, z, w ) {
  2107. this.x = x || 0;
  2108. this.y = y || 0;
  2109. this.z = z || 0;
  2110. this.w = ( w !== undefined ) ? w : 1;
  2111. };
  2112. THREE.Quaternion.prototype = {
  2113. constructor: THREE.Quaternion,
  2114. set: function ( x, y, z, w ) {
  2115. this.x = x;
  2116. this.y = y;
  2117. this.z = z;
  2118. this.w = w;
  2119. return this;
  2120. },
  2121. copy: function ( q ) {
  2122. this.x = q.x;
  2123. this.y = q.y;
  2124. this.z = q.z;
  2125. this.w = q.w;
  2126. return this;
  2127. },
  2128. setFromEuler: function ( vector ) {
  2129. var c = Math.PI / 360, // 0.5 * Math.PI / 360, // 0.5 is an optimization
  2130. x = vector.x * c,
  2131. y = vector.y * c,
  2132. z = vector.z * c,
  2133. c1 = Math.cos( y ),
  2134. s1 = Math.sin( y ),
  2135. c2 = Math.cos( -z ),
  2136. s2 = Math.sin( -z ),
  2137. c3 = Math.cos( x ),
  2138. s3 = Math.sin( x ),
  2139. c1c2 = c1 * c2,
  2140. s1s2 = s1 * s2;
  2141. this.w = c1c2 * c3 - s1s2 * s3;
  2142. this.x = c1c2 * s3 + s1s2 * c3;
  2143. this.y = s1 * c2 * c3 + c1 * s2 * s3;
  2144. this.z = c1 * s2 * c3 - s1 * c2 * s3;
  2145. return this;
  2146. },
  2147. setFromAxisAngle: function ( axis, angle ) {
  2148. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  2149. // axis have to be normalized
  2150. var halfAngle = angle / 2,
  2151. s = Math.sin( halfAngle );
  2152. this.x = axis.x * s;
  2153. this.y = axis.y * s;
  2154. this.z = axis.z * s;
  2155. this.w = Math.cos( halfAngle );
  2156. return this;
  2157. },
  2158. setFromRotationMatrix: function ( m ) {
  2159. // Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  2160. function copySign( a, b ) {
  2161. return b < 0 ? -Math.abs( a ) : Math.abs( a );
  2162. }
  2163. var absQ = Math.pow( m.determinant(), 1.0 / 3.0 );
  2164. this.w = Math.sqrt( Math.max( 0, absQ + m.elements[0] + m.elements[5] + m.elements[10] ) ) / 2;
  2165. this.x = Math.sqrt( Math.max( 0, absQ + m.elements[0] - m.elements[5] - m.elements[10] ) ) / 2;
  2166. this.y = Math.sqrt( Math.max( 0, absQ - m.elements[0] + m.elements[5] - m.elements[10] ) ) / 2;
  2167. this.z = Math.sqrt( Math.max( 0, absQ - m.elements[0] - m.elements[5] + m.elements[10] ) ) / 2;
  2168. this.x = copySign( this.x, ( m.elements[6] - m.elements[9] ) );
  2169. this.y = copySign( this.y, ( m.elements[8] - m.elements[2] ) );
  2170. this.z = copySign( this.z, ( m.elements[1] - m.elements[4] ) );
  2171. this.normalize();
  2172. return this;
  2173. },
  2174. calculateW : function () {
  2175. this.w = - Math.sqrt( Math.abs( 1.0 - this.x * this.x - this.y * this.y - this.z * this.z ) );
  2176. return this;
  2177. },
  2178. inverse: function () {
  2179. this.x *= -1;
  2180. this.y *= -1;
  2181. this.z *= -1;
  2182. return this;
  2183. },
  2184. length: function () {
  2185. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  2186. },
  2187. normalize: function () {
  2188. var l = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  2189. if ( l === 0 ) {
  2190. this.x = 0;
  2191. this.y = 0;
  2192. this.z = 0;
  2193. this.w = 0;
  2194. } else {
  2195. l = 1 / l;
  2196. this.x = this.x * l;
  2197. this.y = this.y * l;
  2198. this.z = this.z * l;
  2199. this.w = this.w * l;
  2200. }
  2201. return this;
  2202. },
  2203. multiply: function ( a, b ) {
  2204. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  2205. this.x = a.x * b.w + a.y * b.z - a.z * b.y + a.w * b.x;
  2206. this.y = -a.x * b.z + a.y * b.w + a.z * b.x + a.w * b.y;
  2207. this.z = a.x * b.y - a.y * b.x + a.z * b.w + a.w * b.z;
  2208. this.w = -a.x * b.x - a.y * b.y - a.z * b.z + a.w * b.w;
  2209. return this;
  2210. },
  2211. multiplySelf: function ( b ) {
  2212. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  2213. qbx = b.x, qby = b.y, qbz = b.z, qbw = b.w;
  2214. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  2215. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  2216. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  2217. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  2218. return this;
  2219. },
  2220. multiplyVector3: function ( vector, dest ) {
  2221. if ( !dest ) { dest = vector; }
  2222. var x = vector.x, y = vector.y, z = vector.z,
  2223. qx = this.x, qy = this.y, qz = this.z, qw = this.w;
  2224. // calculate quat * vector
  2225. var ix = qw * x + qy * z - qz * y,
  2226. iy = qw * y + qz * x - qx * z,
  2227. iz = qw * z + qx * y - qy * x,
  2228. iw = -qx * x - qy * y - qz * z;
  2229. // calculate result * inverse quat
  2230. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  2231. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  2232. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  2233. return dest;
  2234. },
  2235. clone: function () {
  2236. return new THREE.Quaternion( this.x, this.y, this.z, this.w );
  2237. }
  2238. }
  2239. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  2240. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  2241. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  2242. if (cosHalfTheta < 0) {
  2243. qm.w = -qb.w; qm.x = -qb.x; qm.y = -qb.y; qm.z = -qb.z;
  2244. cosHalfTheta = -cosHalfTheta;
  2245. } else {
  2246. qm.copy(qb);
  2247. }
  2248. if ( Math.abs( cosHalfTheta ) >= 1.0 ) {
  2249. qm.w = qa.w; qm.x = qa.x; qm.y = qa.y; qm.z = qa.z;
  2250. return qm;
  2251. }
  2252. var halfTheta = Math.acos( cosHalfTheta ),
  2253. sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  2254. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  2255. qm.w = 0.5 * ( qa.w + qb.w );
  2256. qm.x = 0.5 * ( qa.x + qb.x );
  2257. qm.y = 0.5 * ( qa.y + qb.y );
  2258. qm.z = 0.5 * ( qa.z + qb.z );
  2259. return qm;
  2260. }
  2261. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  2262. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  2263. qm.w = ( qa.w * ratioA + qm.w * ratioB );
  2264. qm.x = ( qa.x * ratioA + qm.x * ratioB );
  2265. qm.y = ( qa.y * ratioA + qm.y * ratioB );
  2266. qm.z = ( qa.z * ratioA + qm.z * ratioB );
  2267. return qm;
  2268. }
  2269. /**
  2270. * @author mr.doob / http://mrdoob.com/
  2271. */
  2272. THREE.Vertex = function ( position ) {
  2273. this.position = position || new THREE.Vector3();
  2274. };
  2275. THREE.Vertex.prototype = {
  2276. constructor: THREE.Vertex,
  2277. clone: function () {
  2278. return new THREE.Vertex( this.position.clone() );
  2279. }
  2280. };
  2281. /**
  2282. * @author mr.doob / http://mrdoob.com/
  2283. * @author alteredq / http://alteredqualia.com/
  2284. */
  2285. THREE.Face3 = function ( a, b, c, normal, color, materialIndex ) {
  2286. this.a = a;
  2287. this.b = b;
  2288. this.c = c;
  2289. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  2290. this.vertexNormals = normal instanceof Array ? normal : [ ];
  2291. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  2292. this.vertexColors = color instanceof Array ? color : [];
  2293. this.vertexTangents = [];
  2294. this.materialIndex = materialIndex;
  2295. this.centroid = new THREE.Vector3();
  2296. };
  2297. THREE.Face3.prototype = {
  2298. constructor: THREE.Face3,
  2299. clone: function () {
  2300. var face = new THREE.Face3( this.a, this.b, this.c );
  2301. face.normal.copy( this.normal );
  2302. face.color.copy( this.color );
  2303. face.centroid.copy( this.centroid );
  2304. face.materialIndex = this.materialIndex;
  2305. var i, il;
  2306. for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  2307. for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  2308. for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  2309. return face;
  2310. }
  2311. };
  2312. /**
  2313. * @author mr.doob / http://mrdoob.com/
  2314. * @author alteredq / http://alteredqualia.com/
  2315. */
  2316. THREE.Face4 = function ( a, b, c, d, normal, color, materialIndex ) {
  2317. this.a = a;
  2318. this.b = b;
  2319. this.c = c;
  2320. this.d = d;
  2321. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  2322. this.vertexNormals = normal instanceof Array ? normal : [ ];
  2323. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  2324. this.vertexColors = color instanceof Array ? color : [];
  2325. this.vertexTangents = [];
  2326. this.materialIndex = materialIndex;
  2327. this.centroid = new THREE.Vector3();
  2328. };
  2329. THREE.Face4.prototype = {
  2330. constructor: THREE.Face4,
  2331. clone: function () {
  2332. var face = new THREE.Face4( this.a, this.b, this.c, this.d );
  2333. face.normal.copy( this.normal );
  2334. face.color.copy( this.color );
  2335. face.centroid.copy( this.centroid );
  2336. face.materialIndex = this.materialIndex;
  2337. var i, il;
  2338. for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  2339. for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  2340. for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  2341. return face;
  2342. }
  2343. };
  2344. /**
  2345. * @author mr.doob / http://mrdoob.com/
  2346. */
  2347. THREE.UV = function ( u, v ) {
  2348. this.u = u || 0;
  2349. this.v = v || 0;
  2350. };
  2351. THREE.UV.prototype = {
  2352. constructor: THREE.UV,
  2353. set: function ( u, v ) {
  2354. this.u = u;
  2355. this.v = v;
  2356. return this;
  2357. },
  2358. copy: function ( uv ) {
  2359. this.u = uv.u;
  2360. this.v = uv.v;
  2361. return this;
  2362. },
  2363. lerpSelf: function ( uv, alpha ) {
  2364. this.u += ( uv.u - this.u ) * alpha;
  2365. this.v += ( uv.v - this.v ) * alpha;
  2366. return this;
  2367. },
  2368. clone: function () {
  2369. return new THREE.UV( this.u, this.v );
  2370. }
  2371. };
  2372. /**
  2373. * @author mr.doob / http://mrdoob.com/
  2374. * @author kile / http://kile.stravaganza.org/
  2375. * @author alteredq / http://alteredqualia.com/
  2376. * @author mikael emtinger / http://gomo.se/
  2377. * @author zz85 / http://www.lab4games.net/zz85/blog
  2378. */
  2379. THREE.Geometry = function () {
  2380. this.id = THREE.GeometryCount ++;
  2381. this.vertices = [];
  2382. this.colors = []; // one-to-one vertex colors, used in ParticleSystem, Line and Ribbon
  2383. this.materials = [];
  2384. this.faces = [];
  2385. this.faceUvs = [[]];
  2386. this.faceVertexUvs = [[]];
  2387. this.morphTargets = [];
  2388. this.morphColors = [];
  2389. this.morphNormals = [];
  2390. this.skinWeights = [];
  2391. this.skinIndices = [];
  2392. this.boundingBox = null;
  2393. this.boundingSphere = null;
  2394. this.hasTangents = false;
  2395. this.dynamic = false; // unless set to true the *Arrays will be deleted once sent to a buffer.
  2396. };
  2397. THREE.Geometry.prototype = {
  2398. constructor : THREE.Geometry,
  2399. applyMatrix: function ( matrix ) {
  2400. var matrixRotation = new THREE.Matrix4();
  2401. matrixRotation.extractRotation( matrix );
  2402. for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
  2403. var vertex = this.vertices[ i ];
  2404. matrix.multiplyVector3( vertex.position );
  2405. }
  2406. for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
  2407. var face = this.faces[ i ];
  2408. matrixRotation.multiplyVector3( face.normal );
  2409. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  2410. matrixRotation.multiplyVector3( face.vertexNormals[ j ] );
  2411. }
  2412. matrix.multiplyVector3( face.centroid );
  2413. }
  2414. },
  2415. computeCentroids: function () {
  2416. var f, fl, face;
  2417. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2418. face = this.faces[ f ];
  2419. face.centroid.set( 0, 0, 0 );
  2420. if ( face instanceof THREE.Face3 ) {
  2421. face.centroid.addSelf( this.vertices[ face.a ].position );
  2422. face.centroid.addSelf( this.vertices[ face.b ].position );
  2423. face.centroid.addSelf( this.vertices[ face.c ].position );
  2424. face.centroid.divideScalar( 3 );
  2425. } else if ( face instanceof THREE.Face4 ) {
  2426. face.centroid.addSelf( this.vertices[ face.a ].position );
  2427. face.centroid.addSelf( this.vertices[ face.b ].position );
  2428. face.centroid.addSelf( this.vertices[ face.c ].position );
  2429. face.centroid.addSelf( this.vertices[ face.d ].position );
  2430. face.centroid.divideScalar( 4 );
  2431. }
  2432. }
  2433. },
  2434. computeFaceNormals: function () {
  2435. var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
  2436. cb = new THREE.Vector3(), ab = new THREE.Vector3();
  2437. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2438. face = this.faces[ f ];
  2439. vA = this.vertices[ face.a ];
  2440. vB = this.vertices[ face.b ];
  2441. vC = this.vertices[ face.c ];
  2442. cb.sub( vC.position, vB.position );
  2443. ab.sub( vA.position, vB.position );
  2444. cb.crossSelf( ab );
  2445. if ( !cb.isZero() ) {
  2446. cb.normalize();
  2447. }
  2448. face.normal.copy( cb );
  2449. }
  2450. },
  2451. computeVertexNormals: function () {
  2452. var v, vl, f, fl, face, vertices;
  2453. // create internal buffers for reuse when calling this method repeatedly
  2454. // (otherwise memory allocation / deallocation every frame is big resource hog)
  2455. if ( this.__tmpVertices === undefined ) {
  2456. this.__tmpVertices = new Array( this.vertices.length );
  2457. vertices = this.__tmpVertices;
  2458. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2459. vertices[ v ] = new THREE.Vector3();
  2460. }
  2461. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2462. face = this.faces[ f ];
  2463. if ( face instanceof THREE.Face3 ) {
  2464. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  2465. } else if ( face instanceof THREE.Face4 ) {
  2466. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  2467. }
  2468. }
  2469. } else {
  2470. vertices = this.__tmpVertices;
  2471. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2472. vertices[ v ].set( 0, 0, 0 );
  2473. }
  2474. }
  2475. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2476. face = this.faces[ f ];
  2477. if ( face instanceof THREE.Face3 ) {
  2478. vertices[ face.a ].addSelf( face.normal );
  2479. vertices[ face.b ].addSelf( face.normal );
  2480. vertices[ face.c ].addSelf( face.normal );
  2481. } else if ( face instanceof THREE.Face4 ) {
  2482. vertices[ face.a ].addSelf( face.normal );
  2483. vertices[ face.b ].addSelf( face.normal );
  2484. vertices[ face.c ].addSelf( face.normal );
  2485. vertices[ face.d ].addSelf( face.normal );
  2486. }
  2487. }
  2488. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2489. vertices[ v ].normalize();
  2490. }
  2491. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2492. face = this.faces[ f ];
  2493. if ( face instanceof THREE.Face3 ) {
  2494. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  2495. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  2496. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  2497. } else if ( face instanceof THREE.Face4 ) {
  2498. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  2499. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  2500. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  2501. face.vertexNormals[ 3 ].copy( vertices[ face.d ] );
  2502. }
  2503. }
  2504. },
  2505. computeMorphNormals: function () {
  2506. var i, il, f, fl, face;
  2507. // save original normals
  2508. // - create temp variables on first access
  2509. // otherwise just copy (for faster repeated calls)
  2510. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2511. face = this.faces[ f ];
  2512. if ( ! face.__originalFaceNormal ) {
  2513. face.__originalFaceNormal = face.normal.clone();
  2514. } else {
  2515. face.__originalFaceNormal.copy( face.normal );
  2516. }
  2517. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  2518. for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  2519. if ( ! face.__originalVertexNormals[ i ] ) {
  2520. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  2521. } else {
  2522. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  2523. }
  2524. }
  2525. }
  2526. // use temp geometry to compute face and vertex normals for each morph
  2527. var tmpGeo = new THREE.Geometry();
  2528. tmpGeo.faces = this.faces;
  2529. for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  2530. // create on first access
  2531. if ( ! this.morphNormals[ i ] ) {
  2532. this.morphNormals[ i ] = {};
  2533. this.morphNormals[ i ].faceNormals = [];
  2534. this.morphNormals[ i ].vertexNormals = [];
  2535. var dstNormalsFace = this.morphNormals[ i ].faceNormals;
  2536. var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  2537. var faceNormal, vertexNormals;
  2538. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2539. face = this.faces[ f ];
  2540. faceNormal = new THREE.Vector3();
  2541. if ( face instanceof THREE.Face3 ) {
  2542. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() };
  2543. } else {
  2544. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3(), d: new THREE.Vector3() };
  2545. }
  2546. dstNormalsFace.push( faceNormal );
  2547. dstNormalsVertex.push( vertexNormals );
  2548. }
  2549. }
  2550. var morphNormals = this.morphNormals[ i ];
  2551. // set vertices to morph target
  2552. tmpGeo.vertices = this.morphTargets[ i ].vertices;
  2553. // compute morph normals
  2554. tmpGeo.computeFaceNormals();
  2555. tmpGeo.computeVertexNormals();
  2556. // store morph normals
  2557. var faceNormal, vertexNormals;
  2558. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2559. face = this.faces[ f ];
  2560. faceNormal = morphNormals.faceNormals[ f ];
  2561. vertexNormals = morphNormals.vertexNormals[ f ];
  2562. faceNormal.copy( face.normal );
  2563. if ( face instanceof THREE.Face3 ) {
  2564. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  2565. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  2566. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  2567. } else {
  2568. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  2569. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  2570. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  2571. vertexNormals.d.copy( face.vertexNormals[ 3 ] );
  2572. }
  2573. }
  2574. }
  2575. // restore original normals
  2576. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2577. face = this.faces[ f ];
  2578. face.normal = face.__originalFaceNormal;
  2579. face.vertexNormals = face.__originalVertexNormals;
  2580. }
  2581. },
  2582. computeTangents: function () {
  2583. // based on http://www.terathon.com/code/tangent.html
  2584. // tangents go to vertices
  2585. var f, fl, v, vl, i, il, vertexIndex,
  2586. face, uv, vA, vB, vC, uvA, uvB, uvC,
  2587. x1, x2, y1, y2, z1, z2,
  2588. s1, s2, t1, t2, r, t, test,
  2589. tan1 = [], tan2 = [],
  2590. sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
  2591. tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
  2592. n = new THREE.Vector3(), w;
  2593. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2594. tan1[ v ] = new THREE.Vector3();
  2595. tan2[ v ] = new THREE.Vector3();
  2596. }
  2597. function handleTriangle( context, a, b, c, ua, ub, uc ) {
  2598. vA = context.vertices[ a ].position;
  2599. vB = context.vertices[ b ].position;
  2600. vC = context.vertices[ c ].position;
  2601. uvA = uv[ ua ];
  2602. uvB = uv[ ub ];
  2603. uvC = uv[ uc ];
  2604. x1 = vB.x - vA.x;
  2605. x2 = vC.x - vA.x;
  2606. y1 = vB.y - vA.y;
  2607. y2 = vC.y - vA.y;
  2608. z1 = vB.z - vA.z;
  2609. z2 = vC.z - vA.z;
  2610. s1 = uvB.u - uvA.u;
  2611. s2 = uvC.u - uvA.u;
  2612. t1 = uvB.v - uvA.v;
  2613. t2 = uvC.v - uvA.v;
  2614. r = 1.0 / ( s1 * t2 - s2 * t1 );
  2615. sdir.set( ( t2 * x1 - t1 * x2 ) * r,
  2616. ( t2 * y1 - t1 * y2 ) * r,
  2617. ( t2 * z1 - t1 * z2 ) * r );
  2618. tdir.set( ( s1 * x2 - s2 * x1 ) * r,
  2619. ( s1 * y2 - s2 * y1 ) * r,
  2620. ( s1 * z2 - s2 * z1 ) * r );
  2621. tan1[ a ].addSelf( sdir );
  2622. tan1[ b ].addSelf( sdir );
  2623. tan1[ c ].addSelf( sdir );
  2624. tan2[ a ].addSelf( tdir );
  2625. tan2[ b ].addSelf( tdir );
  2626. tan2[ c ].addSelf( tdir );
  2627. }
  2628. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2629. face = this.faces[ f ];
  2630. uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents
  2631. if ( face instanceof THREE.Face3 ) {
  2632. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  2633. } else if ( face instanceof THREE.Face4 ) {
  2634. handleTriangle( this, face.a, face.b, face.d, 0, 1, 3 );
  2635. handleTriangle( this, face.b, face.c, face.d, 1, 2, 3 );
  2636. }
  2637. }
  2638. var faceIndex = [ 'a', 'b', 'c', 'd' ];
  2639. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2640. face = this.faces[ f ];
  2641. for ( i = 0; i < face.vertexNormals.length; i++ ) {
  2642. n.copy( face.vertexNormals[ i ] );
  2643. vertexIndex = face[ faceIndex[ i ] ];
  2644. t = tan1[ vertexIndex ];
  2645. // Gram-Schmidt orthogonalize
  2646. tmp.copy( t );
  2647. tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
  2648. // Calculate handedness
  2649. tmp2.cross( face.vertexNormals[ i ], t );
  2650. test = tmp2.dot( tan2[ vertexIndex ] );
  2651. w = (test < 0.0) ? -1.0 : 1.0;
  2652. face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w );
  2653. }
  2654. }
  2655. this.hasTangents = true;
  2656. },
  2657. computeBoundingBox: function () {
  2658. if ( ! this.boundingBox ) {
  2659. this.boundingBox = { min: new THREE.Vector3(), max: new THREE.Vector3() };
  2660. }
  2661. if ( this.vertices.length > 0 ) {
  2662. var position, firstPosition = this.vertices[ 0 ].position;
  2663. this.boundingBox.min.copy( firstPosition );
  2664. this.boundingBox.max.copy( firstPosition );
  2665. var min = this.boundingBox.min,
  2666. max = this.boundingBox.max;
  2667. for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
  2668. position = this.vertices[ v ].position;
  2669. if ( position.x < min.x ) {
  2670. min.x = position.x;
  2671. } else if ( position.x > max.x ) {
  2672. max.x = position.x;
  2673. }
  2674. if ( position.y < min.y ) {
  2675. min.y = position.y;
  2676. } else if ( position.y > max.y ) {
  2677. max.y = position.y;
  2678. }
  2679. if ( position.z < min.z ) {
  2680. min.z = position.z;
  2681. } else if ( position.z > max.z ) {
  2682. max.z = position.z;
  2683. }
  2684. }
  2685. } else {
  2686. this.boundingBox.min.set( 0, 0, 0 );
  2687. this.boundingBox.max.set( 0, 0, 0 );
  2688. }
  2689. },
  2690. computeBoundingSphere: function () {
  2691. if ( ! this.boundingSphere ) this.boundingSphere = { radius: 0 };
  2692. var radius, maxRadius = 0;
  2693. for ( var v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2694. radius = this.vertices[ v ].position.length();
  2695. if ( radius > maxRadius ) maxRadius = radius;
  2696. }
  2697. this.boundingSphere.radius = maxRadius;
  2698. },
  2699. /*
  2700. * Checks for duplicate vertices with hashmap.
  2701. * Duplicated vertices are removed
  2702. * and faces' vertices are updated.
  2703. */
  2704. mergeVertices: function() {
  2705. var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
  2706. var unique = [], changes = [];
  2707. var v, key;
  2708. var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
  2709. var precision = Math.pow( 10, precisionPoints );
  2710. var i,il, face;
  2711. for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
  2712. v = this.vertices[ i ].position;
  2713. key = [ Math.round( v.x * precision ), Math.round( v.y * precision ), Math.round( v.z * precision ) ].join( '_' );
  2714. if ( verticesMap[ key ] === undefined ) {
  2715. verticesMap[ key ] = i;
  2716. unique.push( this.vertices[ i ] );
  2717. changes[ i ] = unique.length - 1;
  2718. } else {
  2719. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  2720. changes[ i ] = changes[ verticesMap[ key ] ];
  2721. }
  2722. };
  2723. // Start to patch face indices
  2724. for( i = 0, il = this.faces.length; i < il; i ++ ) {
  2725. face = this.faces[ i ];
  2726. if ( face instanceof THREE.Face3 ) {
  2727. face.a = changes[ face.a ];
  2728. face.b = changes[ face.b ];
  2729. face.c = changes[ face.c ];
  2730. } else if ( face instanceof THREE.Face4 ) {
  2731. face.a = changes[ face.a ];
  2732. face.b = changes[ face.b ];
  2733. face.c = changes[ face.c ];
  2734. face.d = changes[ face.d ];
  2735. }
  2736. }
  2737. // Use unique set of vertices
  2738. this.vertices = unique;
  2739. }
  2740. };
  2741. THREE.GeometryCount = 0;
  2742. /**
  2743. * Spline from Tween.js, slightly optimized (and trashed)
  2744. * http://sole.github.com/tween.js/examples/05_spline.html
  2745. *
  2746. * @author mrdoob / http://mrdoob.com/
  2747. * @author alteredq / http://alteredqualia.com/
  2748. */
  2749. THREE.Spline = function ( points ) {
  2750. this.points = points;
  2751. var c = [], v3 = { x: 0, y: 0, z: 0 },
  2752. point, intPoint, weight, w2, w3,
  2753. pa, pb, pc, pd;
  2754. this.initFromArray = function( a ) {
  2755. this.points = [];
  2756. for ( var i = 0; i < a.length; i++ ) {
  2757. this.points[ i ] = { x: a[ i ][ 0 ], y: a[ i ][ 1 ], z: a[ i ][ 2 ] };
  2758. }
  2759. };
  2760. this.getPoint = function ( k ) {
  2761. point = ( this.points.length - 1 ) * k;
  2762. intPoint = Math.floor( point );
  2763. weight = point - intPoint;
  2764. c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1;
  2765. c[ 1 ] = intPoint;
  2766. c[ 2 ] = intPoint > this.points.length - 2 ? this.points.length - 1 : intPoint + 1;
  2767. c[ 3 ] = intPoint > this.points.length - 3 ? this.points.length - 1 : intPoint + 2;
  2768. pa = this.points[ c[ 0 ] ];
  2769. pb = this.points[ c[ 1 ] ];
  2770. pc = this.points[ c[ 2 ] ];
  2771. pd = this.points[ c[ 3 ] ];
  2772. w2 = weight * weight;
  2773. w3 = weight * w2;
  2774. v3.x = interpolate( pa.x, pb.x, pc.x, pd.x, weight, w2, w3 );
  2775. v3.y = interpolate( pa.y, pb.y, pc.y, pd.y, weight, w2, w3 );
  2776. v3.z = interpolate( pa.z, pb.z, pc.z, pd.z, weight, w2, w3 );
  2777. return v3;
  2778. };
  2779. this.getControlPointsArray = function () {
  2780. var i, p, l = this.points.length,
  2781. coords = [];
  2782. for ( i = 0; i < l; i ++ ) {
  2783. p = this.points[ i ];
  2784. coords[ i ] = [ p.x, p.y, p.z ];
  2785. }
  2786. return coords;
  2787. };
  2788. // approximate length by summing linear segments
  2789. this.getLength = function ( nSubDivisions ) {
  2790. var i, index, nSamples, position,
  2791. point = 0, intPoint = 0, oldIntPoint = 0,
  2792. oldPosition = new THREE.Vector3(),
  2793. tmpVec = new THREE.Vector3(),
  2794. chunkLengths = [],
  2795. totalLength = 0;
  2796. // first point has 0 length
  2797. chunkLengths[ 0 ] = 0;
  2798. if ( !nSubDivisions ) nSubDivisions = 100;
  2799. nSamples = this.points.length * nSubDivisions;
  2800. oldPosition.copy( this.points[ 0 ] );
  2801. for ( i = 1; i < nSamples; i ++ ) {
  2802. index = i / nSamples;
  2803. position = this.getPoint( index );
  2804. tmpVec.copy( position );
  2805. totalLength += tmpVec.distanceTo( oldPosition );
  2806. oldPosition.copy( position );
  2807. point = ( this.points.length - 1 ) * index;
  2808. intPoint = Math.floor( point );
  2809. if ( intPoint != oldIntPoint ) {
  2810. chunkLengths[ intPoint ] = totalLength;
  2811. oldIntPoint = intPoint;
  2812. }
  2813. }
  2814. // last point ends with total length
  2815. chunkLengths[ chunkLengths.length ] = totalLength;
  2816. return { chunks: chunkLengths, total: totalLength };
  2817. };
  2818. this.reparametrizeByArcLength = function ( samplingCoef ) {
  2819. var i, j,
  2820. index, indexCurrent, indexNext,
  2821. linearDistance, realDistance,
  2822. sampling, position,
  2823. newpoints = [],
  2824. tmpVec = new THREE.Vector3(),
  2825. sl = this.getLength();
  2826. newpoints.push( tmpVec.copy( this.points[ 0 ] ).clone() );
  2827. for ( i = 1; i < this.points.length; i++ ) {
  2828. //tmpVec.copy( this.points[ i - 1 ] );
  2829. //linearDistance = tmpVec.distanceTo( this.points[ i ] );
  2830. realDistance = sl.chunks[ i ] - sl.chunks[ i - 1 ];
  2831. sampling = Math.ceil( samplingCoef * realDistance / sl.total );
  2832. indexCurrent = ( i - 1 ) / ( this.points.length - 1 );
  2833. indexNext = i / ( this.points.length - 1 );
  2834. for ( j = 1; j < sampling - 1; j++ ) {
  2835. index = indexCurrent + j * ( 1 / sampling ) * ( indexNext - indexCurrent );
  2836. position = this.getPoint( index );
  2837. newpoints.push( tmpVec.copy( position ).clone() );
  2838. }
  2839. newpoints.push( tmpVec.copy( this.points[ i ] ).clone() );
  2840. }
  2841. this.points = newpoints;
  2842. };
  2843. // Catmull-Rom
  2844. function interpolate( p0, p1, p2, p3, t, t2, t3 ) {
  2845. var v0 = ( p2 - p0 ) * 0.5,
  2846. v1 = ( p3 - p1 ) * 0.5;
  2847. return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  2848. };
  2849. };
  2850. /**
  2851. * @author mr.doob / http://mrdoob.com/
  2852. * @author mikael emtinger / http://gomo.se/
  2853. */
  2854. THREE.Camera = function () {
  2855. THREE.Object3D.call( this );
  2856. this.matrixWorldInverse = new THREE.Matrix4();
  2857. this.projectionMatrix = new THREE.Matrix4();
  2858. this.projectionMatrixInverse = new THREE.Matrix4();
  2859. };
  2860. THREE.Camera.prototype = new THREE.Object3D();
  2861. THREE.Camera.prototype.constructor = THREE.Camera;
  2862. THREE.Camera.prototype.lookAt = function ( vector ) {
  2863. // TODO: Add hierarchy support.
  2864. this.matrix.lookAt( this.position, vector, this.up );
  2865. if ( this.rotationAutoUpdate ) {
  2866. this.rotation.getRotationFromMatrix( this.matrix );
  2867. }
  2868. };
  2869. /**
  2870. * @author alteredq / http://alteredqualia.com/
  2871. */
  2872. THREE.OrthographicCamera = function ( left, right, top, bottom, near, far ) {
  2873. THREE.Camera.call( this );
  2874. this.left = left;
  2875. this.right = right;
  2876. this.top = top;
  2877. this.bottom = bottom;
  2878. this.near = ( near !== undefined ) ? near : 0.1;
  2879. this.far = ( far !== undefined ) ? far : 2000;
  2880. this.updateProjectionMatrix();
  2881. };
  2882. THREE.OrthographicCamera.prototype = new THREE.Camera();
  2883. THREE.OrthographicCamera.prototype.constructor = THREE.OrthographicCamera;
  2884. THREE.OrthographicCamera.prototype.updateProjectionMatrix = function () {
  2885. this.projectionMatrix.makeOrthographic( this.left, this.right, this.top, this.bottom, this.near, this.far );
  2886. };
  2887. /**
  2888. * @author mr.doob / http://mrdoob.com/
  2889. * @author greggman / http://games.greggman.com/
  2890. * @author zz85 / http://www.lab4games.net/zz85/blog
  2891. */
  2892. THREE.PerspectiveCamera = function ( fov, aspect, near, far ) {
  2893. THREE.Camera.call( this );
  2894. this.fov = fov !== undefined ? fov : 50;
  2895. this.aspect = aspect !== undefined ? aspect : 1;
  2896. this.near = near !== undefined ? near : 0.1;
  2897. this.far = far !== undefined ? far : 2000;
  2898. this.updateProjectionMatrix();
  2899. };
  2900. THREE.PerspectiveCamera.prototype = new THREE.Camera();
  2901. THREE.PerspectiveCamera.prototype.constructor = THREE.PerspectiveCamera;
  2902. /**
  2903. * Uses Focal Length (in mm) to estimate and set FOV
  2904. * 35mm (fullframe) camera is used if frame size is not specified;
  2905. * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html
  2906. */
  2907. THREE.PerspectiveCamera.prototype.setLens = function ( focalLength, frameHeight ) {
  2908. frameHeight = frameHeight !== undefined ? frameHeight : 24;
  2909. this.fov = 2 * Math.atan( frameHeight / ( focalLength * 2 ) ) * ( 180 / Math.PI );
  2910. this.updateProjectionMatrix();
  2911. }
  2912. /**
  2913. * Sets an offset in a larger frustum. This is useful for multi-window or
  2914. * multi-monitor/multi-machine setups.
  2915. *
  2916. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  2917. * the monitors are in grid like this
  2918. *
  2919. * +---+---+---+
  2920. * | A | B | C |
  2921. * +---+---+---+
  2922. * | D | E | F |
  2923. * +---+---+---+
  2924. *
  2925. * then for each monitor you would call it like this
  2926. *
  2927. * var w = 1920;
  2928. * var h = 1080;
  2929. * var fullWidth = w * 3;
  2930. * var fullHeight = h * 2;
  2931. *
  2932. * --A--
  2933. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  2934. * --B--
  2935. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  2936. * --C--
  2937. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  2938. * --D--
  2939. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  2940. * --E--
  2941. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  2942. * --F--
  2943. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  2944. *
  2945. * Note there is no reason monitors have to be the same size or in a grid.
  2946. */
  2947. THREE.PerspectiveCamera.prototype.setViewOffset = function ( fullWidth, fullHeight, x, y, width, height ) {
  2948. this.fullWidth = fullWidth;
  2949. this.fullHeight = fullHeight;
  2950. this.x = x;
  2951. this.y = y;
  2952. this.width = width;
  2953. this.height = height;
  2954. this.updateProjectionMatrix();
  2955. };
  2956. THREE.PerspectiveCamera.prototype.updateProjectionMatrix = function () {
  2957. if ( this.fullWidth ) {
  2958. var aspect = this.fullWidth / this.fullHeight;
  2959. var top = Math.tan( this.fov * Math.PI / 360 ) * this.near;
  2960. var bottom = -top;
  2961. var left = aspect * bottom;
  2962. var right = aspect * top;
  2963. var width = Math.abs( right - left );
  2964. var height = Math.abs( top - bottom );
  2965. this.projectionMatrix.makeFrustum(
  2966. left + this.x * width / this.fullWidth,
  2967. left + ( this.x + this.width ) * width / this.fullWidth,
  2968. top - ( this.y + this.height ) * height / this.fullHeight,
  2969. top - this.y * height / this.fullHeight,
  2970. this.near,
  2971. this.far
  2972. );
  2973. } else {
  2974. this.projectionMatrix.makePerspective( this.fov, this.aspect, this.near, this.far );
  2975. }
  2976. };
  2977. /**
  2978. * @author mr.doob / http://mrdoob.com/
  2979. * @author alteredq / http://alteredqualia.com/
  2980. */
  2981. THREE.Light = function ( hex ) {
  2982. THREE.Object3D.call( this );
  2983. this.color = new THREE.Color( hex );
  2984. };
  2985. THREE.Light.prototype = new THREE.Object3D();
  2986. THREE.Light.prototype.constructor = THREE.Light;
  2987. THREE.Light.prototype.supr = THREE.Object3D.prototype;
  2988. /**
  2989. * @author mr.doob / http://mrdoob.com/
  2990. */
  2991. THREE.AmbientLight = function ( hex ) {
  2992. THREE.Light.call( this, hex );
  2993. };
  2994. THREE.AmbientLight.prototype = new THREE.Light();
  2995. THREE.AmbientLight.prototype.constructor = THREE.AmbientLight;
  2996. /**
  2997. * @author mr.doob / http://mrdoob.com/
  2998. * @author alteredq / http://alteredqualia.com/
  2999. */
  3000. THREE.DirectionalLight = function ( hex, intensity, distance ) {
  3001. THREE.Light.call( this, hex );
  3002. this.position = new THREE.Vector3( 0, 1, 0 );
  3003. this.target = new THREE.Object3D();
  3004. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  3005. this.distance = ( distance !== undefined ) ? distance : 0;
  3006. this.castShadow = false;
  3007. this.onlyShadow = false;
  3008. //
  3009. this.shadowCameraNear = 50;
  3010. this.shadowCameraFar = 5000;
  3011. this.shadowCameraLeft = -500;
  3012. this.shadowCameraRight = 500;
  3013. this.shadowCameraTop = 500;
  3014. this.shadowCameraBottom = -500;
  3015. this.shadowCameraVisible = false;
  3016. this.shadowBias = 0;
  3017. this.shadowDarkness = 0.5;
  3018. this.shadowMapWidth = 512;
  3019. this.shadowMapHeight = 512;
  3020. //
  3021. this.shadowCascade = false;
  3022. this.shadowCascadeOffset = new THREE.Vector3( 0, 0, -1000 );
  3023. this.shadowCascadeCount = 2;
  3024. this.shadowCascadeBias = [ 0, 0, 0 ];
  3025. this.shadowCascadeWidth = [ 512, 512, 512 ];
  3026. this.shadowCascadeHeight = [ 512, 512, 512 ];
  3027. this.shadowCascadeNearZ = [ -1.000, 0.990, 0.998 ];
  3028. this.shadowCascadeFarZ = [ 0.990, 0.998, 1.000 ];
  3029. this.shadowCascadeArray = [];
  3030. //
  3031. this.shadowMap = null;
  3032. this.shadowMapSize = null;
  3033. this.shadowCamera = null;
  3034. this.shadowMatrix = null;
  3035. };
  3036. THREE.DirectionalLight.prototype = new THREE.Light();
  3037. THREE.DirectionalLight.prototype.constructor = THREE.DirectionalLight;
  3038. /**
  3039. * @author mr.doob / http://mrdoob.com/
  3040. */
  3041. THREE.PointLight = function ( hex, intensity, distance ) {
  3042. THREE.Light.call( this, hex );
  3043. this.position = new THREE.Vector3( 0, 0, 0 );
  3044. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  3045. this.distance = ( distance !== undefined ) ? distance : 0;
  3046. };
  3047. THREE.PointLight.prototype = new THREE.Light();
  3048. THREE.PointLight.prototype.constructor = THREE.PointLight;
  3049. /**
  3050. * @author alteredq / http://alteredqualia.com/
  3051. */
  3052. THREE.SpotLight = function ( hex, intensity, distance ) {
  3053. THREE.Light.call( this, hex );
  3054. this.position = new THREE.Vector3( 0, 1, 0 );
  3055. this.target = new THREE.Object3D();
  3056. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  3057. this.distance = ( distance !== undefined ) ? distance : 0;
  3058. this.castShadow = false;
  3059. this.onlyShadow = false;
  3060. //
  3061. this.shadowCameraNear = 50;
  3062. this.shadowCameraFar = 5000;
  3063. this.shadowCameraFov = 50;
  3064. this.shadowCameraVisible = false;
  3065. this.shadowBias = 0;
  3066. this.shadowDarkness = 0.5;
  3067. this.shadowMapWidth = 512;
  3068. this.shadowMapHeight = 512;
  3069. //
  3070. this.shadowMap = null;
  3071. this.shadowMapSize = null;
  3072. this.shadowCamera = null;
  3073. this.shadowMatrix = null;
  3074. };
  3075. THREE.SpotLight.prototype = new THREE.Light();
  3076. THREE.SpotLight.prototype.constructor = THREE.SpotLight;
  3077. /**
  3078. * @author mr.doob / http://mrdoob.com/
  3079. * @author alteredq / http://alteredqualia.com/
  3080. */
  3081. THREE.Material = function ( parameters ) {
  3082. parameters = parameters || {};
  3083. this.id = THREE.MaterialCount ++;
  3084. this.name = '';
  3085. this.opacity = parameters.opacity !== undefined ? parameters.opacity : 1;
  3086. this.transparent = parameters.transparent !== undefined ? parameters.transparent : false;
  3087. this.blending = parameters.blending !== undefined ? parameters.blending : THREE.NormalBlending;
  3088. this.blendSrc = parameters.blendSrc !== undefined ? parameters.blendSrc : THREE.SrcAlphaFactor;
  3089. this.blendDst = parameters.blendDst !== undefined ? parameters.blendDst : THREE.OneMinusSrcAlphaFactor;
  3090. this.blendEquation = parameters.blendEquation !== undefined ? parameters.blendEquation : THREE.AddEquation;
  3091. this.depthTest = parameters.depthTest !== undefined ? parameters.depthTest : true;
  3092. this.depthWrite = parameters.depthWrite !== undefined ? parameters.depthWrite : true;
  3093. this.polygonOffset = parameters.polygonOffset !== undefined ? parameters.polygonOffset : false;
  3094. this.polygonOffsetFactor = parameters.polygonOffsetFactor !== undefined ? parameters.polygonOffsetFactor : 0;
  3095. this.polygonOffsetUnits = parameters.polygonOffsetUnits !== undefined ? parameters.polygonOffsetUnits : 0;
  3096. this.alphaTest = parameters.alphaTest !== undefined ? parameters.alphaTest : 0;
  3097. this.overdraw = parameters.overdraw !== undefined ? parameters.overdraw : false; // Boolean for fixing antialiasing gaps in CanvasRenderer
  3098. this.needsUpdate = true;
  3099. }
  3100. THREE.MaterialCount = 0;
  3101. // shading
  3102. THREE.NoShading = 0;
  3103. THREE.FlatShading = 1;
  3104. THREE.SmoothShading = 2;
  3105. // colors
  3106. THREE.NoColors = 0;
  3107. THREE.FaceColors = 1;
  3108. THREE.VertexColors = 2;
  3109. // blending modes
  3110. THREE.NoBlending = 0;
  3111. THREE.NormalBlending = 1;
  3112. THREE.AdditiveBlending = 2;
  3113. THREE.SubtractiveBlending = 3;
  3114. THREE.MultiplyBlending = 4;
  3115. THREE.AdditiveAlphaBlending = 5;
  3116. THREE.CustomBlending = 6;
  3117. // custom blending equations
  3118. // (numbers start from 100 not to clash with other
  3119. // mappings to OpenGL constants defined in Texture.js)
  3120. THREE.AddEquation = 100;
  3121. THREE.SubtractEquation = 101;
  3122. THREE.ReverseSubtractEquation = 102;
  3123. // custom blending destination factors
  3124. THREE.ZeroFactor = 200;
  3125. THREE.OneFactor = 201;
  3126. THREE.SrcColorFactor = 202;
  3127. THREE.OneMinusSrcColorFactor = 203;
  3128. THREE.SrcAlphaFactor = 204;
  3129. THREE.OneMinusSrcAlphaFactor = 205;
  3130. THREE.DstAlphaFactor = 206;
  3131. THREE.OneMinusDstAlphaFactor = 207;
  3132. // custom blending source factors
  3133. //THREE.ZeroFactor = 200;
  3134. //THREE.OneFactor = 201;
  3135. //THREE.SrcAlphaFactor = 204;
  3136. //THREE.OneMinusSrcAlphaFactor = 205;
  3137. //THREE.DstAlphaFactor = 206;
  3138. //THREE.OneMinusDstAlphaFactor = 207;
  3139. THREE.DstColorFactor = 208;
  3140. THREE.OneMinusDstColorFactor = 209;
  3141. THREE.SrcAlphaSaturateFactor = 210;
  3142. /**
  3143. * @author mr.doob / http://mrdoob.com/
  3144. * @author alteredq / http://alteredqualia.com/
  3145. *
  3146. * parameters = {
  3147. * color: <hex>,
  3148. * opacity: <float>,
  3149. *
  3150. * blending: THREE.NormalBlending,
  3151. * depthTest: <bool>,
  3152. *
  3153. * linewidth: <float>,
  3154. * linecap: "round",
  3155. * linejoin: "round",
  3156. *
  3157. * vertexColors: <bool>
  3158. *
  3159. * fog: <bool>
  3160. * }
  3161. */
  3162. THREE.LineBasicMaterial = function ( parameters ) {
  3163. THREE.Material.call( this, parameters );
  3164. parameters = parameters || {};
  3165. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3166. this.linewidth = parameters.linewidth !== undefined ? parameters.linewidth : 1;
  3167. this.linecap = parameters.linecap !== undefined ? parameters.linecap : 'round';
  3168. this.linejoin = parameters.linejoin !== undefined ? parameters.linejoin : 'round';
  3169. this.vertexColors = parameters.vertexColors ? parameters.vertexColors : false;
  3170. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3171. };
  3172. THREE.LineBasicMaterial.prototype = new THREE.Material();
  3173. THREE.LineBasicMaterial.prototype.constructor = THREE.LineBasicMaterial;
  3174. /**
  3175. * @author mr.doob / http://mrdoob.com/
  3176. * @author alteredq / http://alteredqualia.com/
  3177. *
  3178. * parameters = {
  3179. * color: <hex>,
  3180. * opacity: <float>,
  3181. * map: new THREE.Texture( <Image> ),
  3182. *
  3183. * lightMap: new THREE.Texture( <Image> ),
  3184. *
  3185. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3186. * combine: THREE.Multiply,
  3187. * reflectivity: <float>,
  3188. * refractionRatio: <float>,
  3189. *
  3190. * shading: THREE.SmoothShading,
  3191. * blending: THREE.NormalBlending,
  3192. * depthTest: <bool>,
  3193. *
  3194. * wireframe: <boolean>,
  3195. * wireframeLinewidth: <float>,
  3196. *
  3197. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3198. *
  3199. * skinning: <bool>,
  3200. * morphTargets: <bool>,
  3201. *
  3202. * fog: <bool>
  3203. * }
  3204. */
  3205. THREE.MeshBasicMaterial = function ( parameters ) {
  3206. THREE.Material.call( this, parameters );
  3207. parameters = parameters || {};
  3208. // color property represents emissive for MeshBasicMaterial
  3209. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3210. this.map = parameters.map !== undefined ? parameters.map : null;
  3211. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3212. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3213. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3214. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3215. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3216. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3217. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3218. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3219. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3220. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3221. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3222. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3223. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3224. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3225. };
  3226. THREE.MeshBasicMaterial.prototype = new THREE.Material();
  3227. THREE.MeshBasicMaterial.prototype.constructor = THREE.MeshBasicMaterial;
  3228. /**
  3229. * @author mr.doob / http://mrdoob.com/
  3230. * @author alteredq / http://alteredqualia.com/
  3231. *
  3232. * parameters = {
  3233. * color: <hex>,
  3234. * ambient: <hex>,
  3235. * emissive: <hex>,
  3236. * opacity: <float>,
  3237. *
  3238. * map: new THREE.Texture( <Image> ),
  3239. *
  3240. * lightMap: new THREE.Texture( <Image> ),
  3241. *
  3242. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3243. * combine: THREE.Multiply,
  3244. * reflectivity: <float>,
  3245. * refractionRatio: <float>,
  3246. *
  3247. * shading: THREE.SmoothShading,
  3248. * blending: THREE.NormalBlending,
  3249. * depthTest: <bool>,
  3250. *
  3251. * wireframe: <boolean>,
  3252. * wireframeLinewidth: <float>,
  3253. *
  3254. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3255. *
  3256. * skinning: <bool>,
  3257. * morphTargets: <bool>,
  3258. * morphNormals: <bool>,
  3259. *
  3260. * fog: <bool>
  3261. * }
  3262. */
  3263. THREE.MeshLambertMaterial = function ( parameters ) {
  3264. THREE.Material.call( this, parameters );
  3265. parameters = parameters || {};
  3266. // color property represents diffuse for MeshLambertMaterial
  3267. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3268. this.ambient = parameters.ambient !== undefined ? new THREE.Color( parameters.ambient ) : new THREE.Color( 0xffffff );
  3269. this.emissive = parameters.emissive !== undefined ? new THREE.Color( parameters.emissive ) : new THREE.Color( 0x000000 );
  3270. this.wrapAround = parameters.wrapAround !== undefined ? parameters.wrapAround: false;
  3271. this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
  3272. this.map = parameters.map !== undefined ? parameters.map : null;
  3273. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3274. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3275. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3276. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3277. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3278. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3279. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3280. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3281. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3282. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3283. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3284. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3285. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3286. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3287. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false;
  3288. };
  3289. THREE.MeshLambertMaterial.prototype = new THREE.Material();
  3290. THREE.MeshLambertMaterial.prototype.constructor = THREE.MeshLambertMaterial;
  3291. /**
  3292. * @author mr.doob / http://mrdoob.com/
  3293. * @author alteredq / http://alteredqualia.com/
  3294. *
  3295. * parameters = {
  3296. * color: <hex>,
  3297. * ambient: <hex>,
  3298. * emissive: <hex>,
  3299. * specular: <hex>,
  3300. * shininess: <float>,
  3301. * opacity: <float>,
  3302. *
  3303. * map: new THREE.Texture( <Image> ),
  3304. *
  3305. * lightMap: new THREE.Texture( <Image> ),
  3306. *
  3307. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3308. * combine: THREE.Multiply,
  3309. * reflectivity: <float>,
  3310. * refractionRatio: <float>,
  3311. *
  3312. * shading: THREE.SmoothShading,
  3313. * blending: THREE.NormalBlending,
  3314. * depthTest: <bool>,
  3315. *
  3316. * wireframe: <boolean>,
  3317. * wireframeLinewidth: <float>,
  3318. *
  3319. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3320. *
  3321. * skinning: <bool>,
  3322. * morphTargets: <bool>,
  3323. * morphNormals: <bool>,
  3324. *
  3325. * fog: <bool>
  3326. * }
  3327. */
  3328. THREE.MeshPhongMaterial = function ( parameters ) {
  3329. THREE.Material.call( this, parameters );
  3330. parameters = parameters || {};
  3331. // color property represents diffuse for MeshPhongMaterial
  3332. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3333. this.ambient = parameters.ambient !== undefined ? new THREE.Color( parameters.ambient ) : new THREE.Color( 0xffffff );
  3334. this.emissive = parameters.emissive !== undefined ? new THREE.Color( parameters.emissive ) : new THREE.Color( 0x000000 );
  3335. this.specular = parameters.specular !== undefined ? new THREE.Color( parameters.specular ) : new THREE.Color( 0x111111 );
  3336. this.shininess = parameters.shininess !== undefined ? parameters.shininess : 30;
  3337. this.metal = parameters.metal !== undefined ? parameters.metal : false;
  3338. this.perPixel = parameters.perPixel !== undefined ? parameters.perPixel : false;
  3339. this.wrapAround = parameters.wrapAround !== undefined ? parameters.wrapAround: false;
  3340. this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
  3341. this.map = parameters.map !== undefined ? parameters.map : null;
  3342. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3343. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3344. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3345. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3346. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3347. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3348. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3349. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3350. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3351. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3352. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3353. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3354. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3355. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3356. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false;
  3357. };
  3358. THREE.MeshPhongMaterial.prototype = new THREE.Material();
  3359. THREE.MeshPhongMaterial.prototype.constructor = THREE.MeshPhongMaterial;
  3360. /**
  3361. * @author mr.doob / http://mrdoob.com/
  3362. * @author alteredq / http://alteredqualia.com/
  3363. *
  3364. * parameters = {
  3365. * opacity: <float>,
  3366. * blending: THREE.NormalBlending,
  3367. * depthTest: <bool>,
  3368. * wireframe: <boolean>,
  3369. * wireframeLinewidth: <float>
  3370. * }
  3371. */
  3372. THREE.MeshDepthMaterial = function ( parameters ) {
  3373. THREE.Material.call( this, parameters );
  3374. parameters = parameters || {};
  3375. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading; // doesn't really apply here, normals are not used
  3376. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3377. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3378. };
  3379. THREE.MeshDepthMaterial.prototype = new THREE.Material();
  3380. THREE.MeshDepthMaterial.prototype.constructor = THREE.MeshDepthMaterial;
  3381. /**
  3382. * @author mr.doob / http://mrdoob.com/
  3383. *
  3384. * parameters = {
  3385. * opacity: <float>,
  3386. * shading: THREE.FlatShading,
  3387. * blending: THREE.NormalBlending,
  3388. * depthTest: <bool>,
  3389. * wireframe: <boolean>,
  3390. * wireframeLinewidth: <float>
  3391. * }
  3392. */
  3393. THREE.MeshNormalMaterial = function ( parameters ) {
  3394. THREE.Material.call( this, parameters );
  3395. parameters = parameters || {};
  3396. this.shading = parameters.shading ? parameters.shading : THREE.FlatShading;
  3397. this.wireframe = parameters.wireframe ? parameters.wireframe : false;
  3398. this.wireframeLinewidth = parameters.wireframeLinewidth ? parameters.wireframeLinewidth : 1;
  3399. };
  3400. THREE.MeshNormalMaterial.prototype = new THREE.Material();
  3401. THREE.MeshNormalMaterial.prototype.constructor = THREE.MeshNormalMaterial;
  3402. /**
  3403. * @author mr.doob / http://mrdoob.com/
  3404. */
  3405. THREE.MeshFaceMaterial = function () {
  3406. };
  3407. /**
  3408. * @author mr.doob / http://mrdoob.com/
  3409. * @author alteredq / http://alteredqualia.com/
  3410. *
  3411. * parameters = {
  3412. * color: <hex>,
  3413. * opacity: <float>,
  3414. * map: new THREE.Texture( <Image> ),
  3415. *
  3416. * size: <float>,
  3417. *
  3418. * blending: THREE.NormalBlending,
  3419. * depthTest: <bool>,
  3420. *
  3421. * vertexColors: <bool>,
  3422. *
  3423. * fog: <bool>
  3424. * }
  3425. */
  3426. THREE.ParticleBasicMaterial = function ( parameters ) {
  3427. THREE.Material.call( this, parameters );
  3428. parameters = parameters || {};
  3429. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3430. this.map = parameters.map !== undefined ? parameters.map : null;
  3431. this.size = parameters.size !== undefined ? parameters.size : 1;
  3432. this.sizeAttenuation = parameters.sizeAttenuation !== undefined ? parameters.sizeAttenuation : true;
  3433. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : false;
  3434. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3435. };
  3436. THREE.ParticleBasicMaterial.prototype = new THREE.Material();
  3437. THREE.ParticleBasicMaterial.prototype.constructor = THREE.ParticleBasicMaterial;
  3438. /**
  3439. * @author alteredq / http://alteredqualia.com/
  3440. *
  3441. * parameters = {
  3442. * fragmentShader: <string>,
  3443. * vertexShader: <string>,
  3444. *
  3445. * uniforms: { "parameter1": { type: "f", value: 1.0 }, "parameter2": { type: "i" value2: 2 } },
  3446. *
  3447. * shading: THREE.SmoothShading,
  3448. * blending: THREE.NormalBlending,
  3449. * depthTest: <bool>,
  3450. *
  3451. * wireframe: <boolean>,
  3452. * wireframeLinewidth: <float>,
  3453. *
  3454. * lights: <bool>,
  3455. *
  3456. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3457. *
  3458. * skinning: <bool>,
  3459. * morphTargets: <bool>,
  3460. * morphNormals: <bool>,
  3461. *
  3462. * fog: <bool>
  3463. * }
  3464. */
  3465. THREE.ShaderMaterial = function ( parameters ) {
  3466. THREE.Material.call( this, parameters );
  3467. parameters = parameters || {};
  3468. this.fragmentShader = parameters.fragmentShader !== undefined ? parameters.fragmentShader : "void main() {}";
  3469. this.vertexShader = parameters.vertexShader !== undefined ? parameters.vertexShader : "void main() {}";
  3470. this.uniforms = parameters.uniforms !== undefined ? parameters.uniforms : {};
  3471. this.attributes = parameters.attributes;
  3472. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3473. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3474. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3475. this.fog = parameters.fog !== undefined ? parameters.fog : false; // set to use scene fog
  3476. this.lights = parameters.lights !== undefined ? parameters.lights : false; // set to use scene lights
  3477. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors; // set to use "color" attribute stream
  3478. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false; // set to use skinning attribute streams
  3479. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false; // set to use morph targets
  3480. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false; // set to use morph normals
  3481. };
  3482. THREE.ShaderMaterial.prototype = new THREE.Material();
  3483. THREE.ShaderMaterial.prototype.constructor = THREE.ShaderMaterial;
  3484. /**
  3485. * @author mr.doob / http://mrdoob.com/
  3486. * @author alteredq / http://alteredqualia.com/
  3487. * @author szimek / https://github.com/szimek/
  3488. */
  3489. THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type ) {
  3490. this.id = THREE.TextureCount ++;
  3491. this.image = image;
  3492. this.mapping = mapping !== undefined ? mapping : new THREE.UVMapping();
  3493. this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
  3494. this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
  3495. this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
  3496. this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;
  3497. this.format = format !== undefined ? format : THREE.RGBAFormat;
  3498. this.type = type !== undefined ? type : THREE.UnsignedByteType;
  3499. this.offset = new THREE.Vector2( 0, 0 );
  3500. this.repeat = new THREE.Vector2( 1, 1 );
  3501. this.generateMipmaps = true;
  3502. this.premultiplyAlpha = false;
  3503. this.needsUpdate = false;
  3504. this.onUpdate = null;
  3505. };
  3506. THREE.Texture.prototype = {
  3507. constructor: THREE.Texture,
  3508. clone: function () {
  3509. var clonedTexture = new THREE.Texture( this.image, this.mapping, this.wrapS, this.wrapT, this.magFilter, this.minFilter, this.format, this.type );
  3510. clonedTexture.offset.copy( this.offset );
  3511. clonedTexture.repeat.copy( this.repeat );
  3512. return clonedTexture;
  3513. }
  3514. };
  3515. THREE.TextureCount = 0;
  3516. THREE.MultiplyOperation = 0;
  3517. THREE.MixOperation = 1;
  3518. // Mapping modes
  3519. THREE.UVMapping = function () {};
  3520. THREE.CubeReflectionMapping = function () {};
  3521. THREE.CubeRefractionMapping = function () {};
  3522. THREE.SphericalReflectionMapping = function () {};
  3523. THREE.SphericalRefractionMapping = function () {};
  3524. // Wrapping modes
  3525. THREE.RepeatWrapping = 0;
  3526. THREE.ClampToEdgeWrapping = 1;
  3527. THREE.MirroredRepeatWrapping = 2;
  3528. // Filters
  3529. THREE.NearestFilter = 3;
  3530. THREE.NearestMipMapNearestFilter = 4;
  3531. THREE.NearestMipMapLinearFilter = 5;
  3532. THREE.LinearFilter = 6;
  3533. THREE.LinearMipMapNearestFilter = 7;
  3534. THREE.LinearMipMapLinearFilter = 8;
  3535. // Types
  3536. THREE.ByteType = 9;
  3537. THREE.UnsignedByteType = 10;
  3538. THREE.ShortType = 11;
  3539. THREE.UnsignedShortType = 12;
  3540. THREE.IntType = 13;
  3541. THREE.UnsignedIntType = 14;
  3542. THREE.FloatType = 15;
  3543. // Formats
  3544. THREE.AlphaFormat = 16;
  3545. THREE.RGBFormat = 17;
  3546. THREE.RGBAFormat = 18;
  3547. THREE.LuminanceFormat = 19;
  3548. THREE.LuminanceAlphaFormat = 20;
  3549. /**
  3550. * @author alteredq / http://alteredqualia.com/
  3551. */
  3552. THREE.DataTexture = function ( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter ) {
  3553. THREE.Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type );
  3554. this.image = { data: data, width: width, height: height };
  3555. };
  3556. THREE.DataTexture.prototype = new THREE.Texture();
  3557. THREE.DataTexture.prototype.constructor = THREE.DataTexture;
  3558. THREE.DataTexture.prototype.clone = function () {
  3559. 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 );
  3560. clonedTexture.offset.copy( this.offset );
  3561. clonedTexture.repeat.copy( this.repeat );
  3562. return clonedTexture;
  3563. };
  3564. /**
  3565. * @author mr.doob / http://mrdoob.com/
  3566. */
  3567. THREE.Particle = function ( material ) {
  3568. THREE.Object3D.call( this );
  3569. this.material = material;
  3570. };
  3571. THREE.Particle.prototype = new THREE.Object3D();
  3572. THREE.Particle.prototype.constructor = THREE.Particle;
  3573. /**
  3574. * @author alteredq / http://alteredqualia.com/
  3575. */
  3576. THREE.ParticleSystem = function ( geometry, material ) {
  3577. THREE.Object3D.call( this );
  3578. this.geometry = geometry;
  3579. this.material = ( material !== undefined ) ? material : new THREE.ParticleBasicMaterial( { color: Math.random() * 0xffffff } );
  3580. this.sortParticles = false;
  3581. if ( this.geometry ) {
  3582. // calc bound radius
  3583. if( !this.geometry.boundingSphere ) {
  3584. this.geometry.computeBoundingSphere();
  3585. }
  3586. this.boundRadius = geometry.boundingSphere.radius;
  3587. }
  3588. this.frustumCulled = false;
  3589. };
  3590. THREE.ParticleSystem.prototype = new THREE.Object3D();
  3591. THREE.ParticleSystem.prototype.constructor = THREE.ParticleSystem;
  3592. /**
  3593. * @author mr.doob / http://mrdoob.com/
  3594. */
  3595. THREE.Line = function ( geometry, material, type ) {
  3596. THREE.Object3D.call( this );
  3597. this.geometry = geometry;
  3598. this.material = ( material !== undefined ) ? material : new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff } );
  3599. this.type = ( type !== undefined ) ? type : THREE.LineStrip;
  3600. if ( this.geometry ) {
  3601. if ( ! this.geometry.boundingSphere ) {
  3602. this.geometry.computeBoundingSphere();
  3603. }
  3604. }
  3605. };
  3606. THREE.LineStrip = 0;
  3607. THREE.LinePieces = 1;
  3608. THREE.Line.prototype = new THREE.Object3D();
  3609. THREE.Line.prototype.constructor = THREE.Line;
  3610. /**
  3611. * @author mr.doob / http://mrdoob.com/
  3612. * @author alteredq / http://alteredqualia.com/
  3613. * @author mikael emtinger / http://gomo.se/
  3614. */
  3615. THREE.Mesh = function ( geometry, material ) {
  3616. THREE.Object3D.call( this );
  3617. this.geometry = geometry;
  3618. this.material = ( material !== undefined ) ? material : new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: true } );
  3619. if ( this.geometry ) {
  3620. // calc bound radius
  3621. if( ! this.geometry.boundingSphere ) {
  3622. this.geometry.computeBoundingSphere();
  3623. }
  3624. this.boundRadius = geometry.boundingSphere.radius;
  3625. // setup morph targets
  3626. if( this.geometry.morphTargets.length ) {
  3627. this.morphTargetBase = -1;
  3628. this.morphTargetForcedOrder = [];
  3629. this.morphTargetInfluences = [];
  3630. this.morphTargetDictionary = {};
  3631. for( var m = 0; m < this.geometry.morphTargets.length; m ++ ) {
  3632. this.morphTargetInfluences.push( 0 );
  3633. this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m;
  3634. }
  3635. }
  3636. }
  3637. }
  3638. THREE.Mesh.prototype = new THREE.Object3D();
  3639. THREE.Mesh.prototype.constructor = THREE.Mesh;
  3640. THREE.Mesh.prototype.supr = THREE.Object3D.prototype;
  3641. /*
  3642. * Get Morph Target Index by Name
  3643. */
  3644. THREE.Mesh.prototype.getMorphTargetIndexByName = function( name ) {
  3645. if ( this.morphTargetDictionary[ name ] !== undefined ) {
  3646. return this.morphTargetDictionary[ name ];
  3647. }
  3648. console.log( "THREE.Mesh.getMorphTargetIndexByName: morph target " + name + " does not exist. Returning 0." );
  3649. return 0;
  3650. }
  3651. /**
  3652. * @author mikael emtinger / http://gomo.se/
  3653. * @author alteredq / http://alteredqualia.com/
  3654. */
  3655. THREE.Bone = function( belongsToSkin ) {
  3656. THREE.Object3D.call( this );
  3657. this.skin = belongsToSkin;
  3658. this.skinMatrix = new THREE.Matrix4();
  3659. };
  3660. THREE.Bone.prototype = new THREE.Object3D();
  3661. THREE.Bone.prototype.constructor = THREE.Bone;
  3662. THREE.Bone.prototype.supr = THREE.Object3D.prototype;
  3663. THREE.Bone.prototype.update = function( parentSkinMatrix, forceUpdate ) {
  3664. // update local
  3665. if ( this.matrixAutoUpdate ) {
  3666. forceUpdate |= this.updateMatrix();
  3667. }
  3668. // update skin matrix
  3669. if ( forceUpdate || this.matrixWorldNeedsUpdate ) {
  3670. if( parentSkinMatrix ) {
  3671. this.skinMatrix.multiply( parentSkinMatrix, this.matrix );
  3672. } else {
  3673. this.skinMatrix.copy( this.matrix );
  3674. }
  3675. this.matrixWorldNeedsUpdate = false;
  3676. forceUpdate = true;
  3677. }
  3678. // update children
  3679. var child, i, l = this.children.length;
  3680. for ( i = 0; i < l; i ++ ) {
  3681. this.children[ i ].update( this.skinMatrix, forceUpdate );
  3682. }
  3683. };
  3684. /**
  3685. * @author mikael emtinger / http://gomo.se/
  3686. * @author alteredq / http://alteredqualia.com/
  3687. */
  3688. THREE.SkinnedMesh = function ( geometry, material ) {
  3689. THREE.Mesh.call( this, geometry, material );
  3690. // init bones
  3691. this.identityMatrix = new THREE.Matrix4();
  3692. this.bones = [];
  3693. this.boneMatrices = [];
  3694. var b, bone, gbone, p, q, s;
  3695. if ( this.geometry.bones !== undefined ) {
  3696. for ( b = 0; b < this.geometry.bones.length; b ++ ) {
  3697. gbone = this.geometry.bones[ b ];
  3698. p = gbone.pos;
  3699. q = gbone.rotq;
  3700. s = gbone.scl;
  3701. bone = this.addBone();
  3702. bone.name = gbone.name;
  3703. bone.position.set( p[0], p[1], p[2] );
  3704. bone.quaternion.set( q[0], q[1], q[2], q[3] );
  3705. bone.useQuaternion = true;
  3706. if ( s !== undefined ) {
  3707. bone.scale.set( s[0], s[1], s[2] );
  3708. } else {
  3709. bone.scale.set( 1, 1, 1 );
  3710. }
  3711. }
  3712. for ( b = 0; b < this.bones.length; b ++ ) {
  3713. gbone = this.geometry.bones[ b ];
  3714. bone = this.bones[ b ];
  3715. if ( gbone.parent === -1 ) {
  3716. this.add( bone );
  3717. } else {
  3718. this.bones[ gbone.parent ].add( bone );
  3719. }
  3720. }
  3721. this.boneMatrices = new Float32Array( 16 * this.bones.length );
  3722. this.pose();
  3723. }
  3724. };
  3725. THREE.SkinnedMesh.prototype = new THREE.Mesh();
  3726. THREE.SkinnedMesh.prototype.constructor = THREE.SkinnedMesh;
  3727. THREE.SkinnedMesh.prototype.addBone = function( bone ) {
  3728. if ( bone === undefined ) {
  3729. bone = new THREE.Bone( this );
  3730. }
  3731. this.bones.push( bone );
  3732. return bone;
  3733. };
  3734. THREE.SkinnedMesh.prototype.updateMatrixWorld = function ( force ) {
  3735. this.matrixAutoUpdate && this.updateMatrix();
  3736. // update matrixWorld
  3737. if ( this.matrixWorldNeedsUpdate || force ) {
  3738. if ( this.parent ) {
  3739. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  3740. } else {
  3741. this.matrixWorld.copy( this.matrix );
  3742. }
  3743. this.matrixWorldNeedsUpdate = false;
  3744. force = true;
  3745. }
  3746. // update children
  3747. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  3748. var child = this.children[ i ];
  3749. if ( child instanceof THREE.Bone ) {
  3750. child.update( this.identityMatrix, false );
  3751. } else {
  3752. child.updateMatrixWorld( true );
  3753. }
  3754. }
  3755. // flatten bone matrices to array
  3756. var b, bl = this.bones.length,
  3757. ba = this.bones,
  3758. bm = this.boneMatrices;
  3759. for ( b = 0; b < bl; b ++ ) {
  3760. ba[ b ].skinMatrix.flattenToArrayOffset( bm, b * 16 );
  3761. }
  3762. };
  3763. /*
  3764. * Pose
  3765. */
  3766. THREE.SkinnedMesh.prototype.pose = function() {
  3767. this.updateMatrixWorld( true );
  3768. var bim, bone, boneInverses = [];
  3769. for ( var b = 0; b < this.bones.length; b ++ ) {
  3770. bone = this.bones[ b ];
  3771. var inverseMatrix = new THREE.Matrix4();
  3772. inverseMatrix.getInverse( bone.skinMatrix );
  3773. boneInverses.push( inverseMatrix );
  3774. bone.skinMatrix.flattenToArrayOffset( this.boneMatrices, b * 16 );
  3775. }
  3776. // project vertices to local
  3777. if ( this.geometry.skinVerticesA === undefined ) {
  3778. this.geometry.skinVerticesA = [];
  3779. this.geometry.skinVerticesB = [];
  3780. var orgVertex, vertex;
  3781. for ( var i = 0; i < this.geometry.skinIndices.length; i ++ ) {
  3782. orgVertex = this.geometry.vertices[ i ].position;
  3783. var indexA = this.geometry.skinIndices[ i ].x;
  3784. var indexB = this.geometry.skinIndices[ i ].y;
  3785. vertex = new THREE.Vector3( orgVertex.x, orgVertex.y, orgVertex.z );
  3786. this.geometry.skinVerticesA.push( boneInverses[ indexA ].multiplyVector3( vertex ) );
  3787. vertex = new THREE.Vector3( orgVertex.x, orgVertex.y, orgVertex.z );
  3788. this.geometry.skinVerticesB.push( boneInverses[ indexB ].multiplyVector3( vertex ) );
  3789. // todo: add more influences
  3790. // normalize weights
  3791. if ( this.geometry.skinWeights[ i ].x + this.geometry.skinWeights[ i ].y !== 1 ) {
  3792. var len = ( 1.0 - ( this.geometry.skinWeights[ i ].x + this.geometry.skinWeights[ i ].y ) ) * 0.5;
  3793. this.geometry.skinWeights[ i ].x += len;
  3794. this.geometry.skinWeights[ i ].y += len;
  3795. }
  3796. }
  3797. }
  3798. };
  3799. /**
  3800. * @author alteredq / http://alteredqualia.com/
  3801. */
  3802. THREE.Ribbon = function ( geometry, material ) {
  3803. THREE.Object3D.call( this );
  3804. this.geometry = geometry;
  3805. this.material = material;
  3806. };
  3807. THREE.Ribbon.prototype = new THREE.Object3D();
  3808. THREE.Ribbon.prototype.constructor = THREE.Ribbon;
  3809. /**
  3810. * @author mikael emtinger / http://gomo.se/
  3811. * @author alteredq / http://alteredqualia.com/
  3812. * @author mr.doob / http://mrdoob.com/
  3813. */
  3814. THREE.LOD = function () {
  3815. THREE.Object3D.call( this );
  3816. this.LODs = [];
  3817. };
  3818. THREE.LOD.prototype = new THREE.Object3D();
  3819. THREE.LOD.prototype.constructor = THREE.LOD;
  3820. THREE.LOD.prototype.supr = THREE.Object3D.prototype;
  3821. THREE.LOD.prototype.addLevel = function ( object3D, visibleAtDistance ) {
  3822. if ( visibleAtDistance === undefined ) {
  3823. visibleAtDistance = 0;
  3824. }
  3825. visibleAtDistance = Math.abs( visibleAtDistance );
  3826. for ( var l = 0; l < this.LODs.length; l ++ ) {
  3827. if ( visibleAtDistance < this.LODs[ l ].visibleAtDistance ) {
  3828. break;
  3829. }
  3830. }
  3831. this.LODs.splice( l, 0, { visibleAtDistance: visibleAtDistance, object3D: object3D } );
  3832. this.add( object3D );
  3833. };
  3834. THREE.LOD.prototype.update = function ( camera ) {
  3835. if ( this.LODs.length > 1 ) {
  3836. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  3837. var inverse = camera.matrixWorldInverse;
  3838. 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] );
  3839. this.LODs[ 0 ].object3D.visible = true;
  3840. for ( var l = 1; l < this.LODs.length; l ++ ) {
  3841. if( distance >= this.LODs[ l ].visibleAtDistance ) {
  3842. this.LODs[ l - 1 ].object3D.visible = false;
  3843. this.LODs[ l ].object3D.visible = true;
  3844. } else {
  3845. break;
  3846. }
  3847. }
  3848. for( ; l < this.LODs.length; l ++ ) {
  3849. this.LODs[ l ].object3D.visible = false;
  3850. }
  3851. }
  3852. };
  3853. /**
  3854. * @author mikael emtinger / http://gomo.se/
  3855. */
  3856. THREE.Sprite = function ( parameters ) {
  3857. THREE.Object3D.call( this );
  3858. this.color = ( parameters.color !== undefined ) ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3859. this.map = ( parameters.map !== undefined ) ? parameters.map : new THREE.Texture();
  3860. this.blending = ( parameters.blending !== undefined ) ? parameters.blending : THREE.NormalBlending;
  3861. this.blendSrc = parameters.blendSrc !== undefined ? parameters.blendSrc : THREE.SrcAlphaFactor;
  3862. this.blendDst = parameters.blendDst !== undefined ? parameters.blendDst : THREE.OneMinusSrcAlphaFactor;
  3863. this.blendEquation = parameters.blendEquation !== undefined ? parameters.blendEquation : THREE.AddEquation;
  3864. this.useScreenCoordinates = ( parameters.useScreenCoordinates !== undefined ) ? parameters.useScreenCoordinates : true;
  3865. this.mergeWith3D = ( parameters.mergeWith3D !== undefined ) ? parameters.mergeWith3D : !this.useScreenCoordinates;
  3866. this.affectedByDistance = ( parameters.affectedByDistance !== undefined ) ? parameters.affectedByDistance : !this.useScreenCoordinates;
  3867. this.scaleByViewport = ( parameters.scaleByViewport !== undefined ) ? parameters.scaleByViewport : !this.affectedByDistance;
  3868. this.alignment = ( parameters.alignment instanceof THREE.Vector2 ) ? parameters.alignment : THREE.SpriteAlignment.center;
  3869. this.rotation3d = this.rotation;
  3870. this.rotation = 0;
  3871. this.opacity = 1;
  3872. this.uvOffset = new THREE.Vector2( 0, 0 );
  3873. this.uvScale = new THREE.Vector2( 1, 1 );
  3874. };
  3875. THREE.Sprite.prototype = new THREE.Object3D();
  3876. THREE.Sprite.prototype.constructor = THREE.Sprite;
  3877. /*
  3878. * Custom update matrix
  3879. */
  3880. THREE.Sprite.prototype.updateMatrix = function () {
  3881. this.matrix.setPosition( this.position );
  3882. this.rotation3d.set( 0, 0, this.rotation );
  3883. this.matrix.setRotationFromEuler( this.rotation3d );
  3884. if ( this.scale.x !== 1 || this.scale.y !== 1 ) {
  3885. this.matrix.scale( this.scale );
  3886. this.boundRadiusScale = Math.max( this.scale.x, this.scale.y );
  3887. }
  3888. this.matrixWorldNeedsUpdate = true;
  3889. };
  3890. /*
  3891. * Alignment
  3892. */
  3893. THREE.SpriteAlignment = {};
  3894. THREE.SpriteAlignment.topLeft = new THREE.Vector2( 1, -1 );
  3895. THREE.SpriteAlignment.topCenter = new THREE.Vector2( 0, -1 );
  3896. THREE.SpriteAlignment.topRight = new THREE.Vector2( -1, -1 );
  3897. THREE.SpriteAlignment.centerLeft = new THREE.Vector2( 1, 0 );
  3898. THREE.SpriteAlignment.center = new THREE.Vector2( 0, 0 );
  3899. THREE.SpriteAlignment.centerRight = new THREE.Vector2( -1, 0 );
  3900. THREE.SpriteAlignment.bottomLeft = new THREE.Vector2( 1, 1 );
  3901. THREE.SpriteAlignment.bottomCenter = new THREE.Vector2( 0, 1 );
  3902. THREE.SpriteAlignment.bottomRight = new THREE.Vector2( -1, 1 );
  3903. /**
  3904. * @author mr.doob / http://mrdoob.com/
  3905. */
  3906. THREE.Scene = function () {
  3907. THREE.Object3D.call( this );
  3908. this.fog = null;
  3909. this.overrideMaterial = null;
  3910. this.matrixAutoUpdate = false;
  3911. this.__objects = [];
  3912. this.__lights = [];
  3913. this.__objectsAdded = [];
  3914. this.__objectsRemoved = [];
  3915. };
  3916. THREE.Scene.prototype = new THREE.Object3D();
  3917. THREE.Scene.prototype.constructor = THREE.Scene;
  3918. THREE.Scene.prototype.__addObject = function ( object ) {
  3919. if ( object instanceof THREE.Light ) {
  3920. if ( this.__lights.indexOf( object ) === - 1 ) {
  3921. this.__lights.push( object );
  3922. }
  3923. } else if ( !( object instanceof THREE.Camera || object instanceof THREE.Bone ) ) {
  3924. if ( this.__objects.indexOf( object ) === - 1 ) {
  3925. this.__objects.push( object );
  3926. this.__objectsAdded.push( object );
  3927. // check if previously removed
  3928. var i = this.__objectsRemoved.indexOf( object );
  3929. if ( i !== -1 ) {
  3930. this.__objectsRemoved.splice( i, 1 );
  3931. }
  3932. }
  3933. }
  3934. for ( var c = 0; c < object.children.length; c ++ ) {
  3935. this.__addObject( object.children[ c ] );
  3936. }
  3937. };
  3938. THREE.Scene.prototype.__removeObject = function ( object ) {
  3939. if ( object instanceof THREE.Light ) {
  3940. var i = this.__lights.indexOf( object );
  3941. if ( i !== -1 ) {
  3942. this.__lights.splice( i, 1 );
  3943. }
  3944. } else if ( !( object instanceof THREE.Camera ) ) {
  3945. var i = this.__objects.indexOf( object );
  3946. if( i !== -1 ) {
  3947. this.__objects.splice( i, 1 );
  3948. this.__objectsRemoved.push( object );
  3949. // check if previously added
  3950. var ai = this.__objectsAdded.indexOf( object );
  3951. if ( ai !== -1 ) {
  3952. this.__objectsAdded.splice( ai, 1 );
  3953. }
  3954. }
  3955. }
  3956. for ( var c = 0; c < object.children.length; c ++ ) {
  3957. this.__removeObject( object.children[ c ] );
  3958. }
  3959. };
  3960. /**
  3961. * @author mr.doob / http://mrdoob.com/
  3962. * @author alteredq / http://alteredqualia.com/
  3963. */
  3964. THREE.Fog = function ( hex, near, far ) {
  3965. this.color = new THREE.Color( hex );
  3966. this.near = ( near !== undefined ) ? near : 1;
  3967. this.far = ( far !== undefined ) ? far : 1000;
  3968. };
  3969. /**
  3970. * @author mr.doob / http://mrdoob.com/
  3971. * @author alteredq / http://alteredqualia.com/
  3972. */
  3973. THREE.FogExp2 = function ( hex, density ) {
  3974. this.color = new THREE.Color( hex );
  3975. this.density = ( density !== undefined ) ? density : 0.00025;
  3976. };
  3977. /**
  3978. * @author alteredq / http://alteredqualia.com/
  3979. * @author mrdoob / http://mrdoob.com/
  3980. * @author mikael emtinger / http://gomo.se/
  3981. */
  3982. THREE.ShaderChunk = {
  3983. // FOG
  3984. fog_pars_fragment: [
  3985. "#ifdef USE_FOG",
  3986. "uniform vec3 fogColor;",
  3987. "#ifdef FOG_EXP2",
  3988. "uniform float fogDensity;",
  3989. "#else",
  3990. "uniform float fogNear;",
  3991. "uniform float fogFar;",
  3992. "#endif",
  3993. "#endif"
  3994. ].join("\n"),
  3995. fog_fragment: [
  3996. "#ifdef USE_FOG",
  3997. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  3998. "#ifdef FOG_EXP2",
  3999. "const float LOG2 = 1.442695;",
  4000. "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
  4001. "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
  4002. "#else",
  4003. "float fogFactor = smoothstep( fogNear, fogFar, depth );",
  4004. "#endif",
  4005. "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
  4006. "#endif"
  4007. ].join("\n"),
  4008. // ENVIRONMENT MAP
  4009. envmap_pars_fragment: [
  4010. "#ifdef USE_ENVMAP",
  4011. "varying vec3 vReflect;",
  4012. "uniform float reflectivity;",
  4013. "uniform samplerCube envMap;",
  4014. "uniform float flipEnvMap;",
  4015. "uniform int combine;",
  4016. "#endif"
  4017. ].join("\n"),
  4018. envmap_fragment: [
  4019. "#ifdef USE_ENVMAP",
  4020. "#ifdef DOUBLE_SIDED",
  4021. "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  4022. "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  4023. "#else",
  4024. "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  4025. "#endif",
  4026. "#ifdef GAMMA_INPUT",
  4027. "cubeColor.xyz *= cubeColor.xyz;",
  4028. "#endif",
  4029. "if ( combine == 1 ) {",
  4030. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, reflectivity );",
  4031. "} else {",
  4032. "gl_FragColor.xyz = gl_FragColor.xyz * cubeColor.xyz;",
  4033. "}",
  4034. "#endif"
  4035. ].join("\n"),
  4036. envmap_pars_vertex: [
  4037. "#ifdef USE_ENVMAP",
  4038. "varying vec3 vReflect;",
  4039. "uniform float refractionRatio;",
  4040. "uniform bool useRefract;",
  4041. "#endif"
  4042. ].join("\n"),
  4043. envmap_vertex : [
  4044. "#ifdef USE_ENVMAP",
  4045. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  4046. "vec3 nWorld = mat3( objectMatrix[ 0 ].xyz, objectMatrix[ 1 ].xyz, objectMatrix[ 2 ].xyz ) * normal;",
  4047. "if ( useRefract ) {",
  4048. "vReflect = refract( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ), refractionRatio );",
  4049. "} else {",
  4050. "vReflect = reflect( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ) );",
  4051. "}",
  4052. "#endif"
  4053. ].join("\n"),
  4054. // COLOR MAP (particles)
  4055. map_particle_pars_fragment: [
  4056. "#ifdef USE_MAP",
  4057. "uniform sampler2D map;",
  4058. "#endif"
  4059. ].join("\n"),
  4060. map_particle_fragment: [
  4061. "#ifdef USE_MAP",
  4062. "gl_FragColor = gl_FragColor * texture2D( map, gl_PointCoord );",
  4063. "#endif"
  4064. ].join("\n"),
  4065. // COLOR MAP (triangles)
  4066. map_pars_vertex: [
  4067. "#ifdef USE_MAP",
  4068. "varying vec2 vUv;",
  4069. "uniform vec4 offsetRepeat;",
  4070. "#endif"
  4071. ].join("\n"),
  4072. map_pars_fragment: [
  4073. "#ifdef USE_MAP",
  4074. "varying vec2 vUv;",
  4075. "uniform sampler2D map;",
  4076. "#endif"
  4077. ].join("\n"),
  4078. map_vertex: [
  4079. "#ifdef USE_MAP",
  4080. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  4081. "#endif"
  4082. ].join("\n"),
  4083. map_fragment: [
  4084. "#ifdef USE_MAP",
  4085. "#ifdef GAMMA_INPUT",
  4086. "vec4 texelColor = texture2D( map, vUv );",
  4087. "texelColor.xyz *= texelColor.xyz;",
  4088. "gl_FragColor = gl_FragColor * texelColor;",
  4089. "#else",
  4090. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  4091. "#endif",
  4092. "#endif"
  4093. ].join("\n"),
  4094. // LIGHT MAP
  4095. lightmap_pars_fragment: [
  4096. "#ifdef USE_LIGHTMAP",
  4097. "varying vec2 vUv2;",
  4098. "uniform sampler2D lightMap;",
  4099. "#endif"
  4100. ].join("\n"),
  4101. lightmap_pars_vertex: [
  4102. "#ifdef USE_LIGHTMAP",
  4103. "varying vec2 vUv2;",
  4104. "#endif"
  4105. ].join("\n"),
  4106. lightmap_fragment: [
  4107. "#ifdef USE_LIGHTMAP",
  4108. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  4109. "#endif"
  4110. ].join("\n"),
  4111. lightmap_vertex: [
  4112. "#ifdef USE_LIGHTMAP",
  4113. "vUv2 = uv2;",
  4114. "#endif"
  4115. ].join("\n"),
  4116. // LIGHTS LAMBERT
  4117. lights_lambert_pars_vertex: [
  4118. "uniform vec3 ambient;",
  4119. "uniform vec3 diffuse;",
  4120. "uniform vec3 emissive;",
  4121. "uniform vec3 ambientLightColor;",
  4122. "#if MAX_DIR_LIGHTS > 0",
  4123. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  4124. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  4125. "#endif",
  4126. "#if MAX_POINT_LIGHTS > 0",
  4127. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  4128. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  4129. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  4130. "#endif",
  4131. "#ifdef WRAP_AROUND",
  4132. "uniform vec3 wrapRGB;",
  4133. "#endif",
  4134. ].join("\n"),
  4135. lights_lambert_vertex: [
  4136. "vLightFront = vec3( 0.0 );",
  4137. "#ifdef DOUBLE_SIDED",
  4138. "vLightBack = vec3( 0.0 );",
  4139. "#endif",
  4140. "transformedNormal = normalize( transformedNormal );",
  4141. "#if MAX_DIR_LIGHTS > 0",
  4142. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  4143. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  4144. "vec3 dirVector = normalize( lDirection.xyz );",
  4145. "float dotProduct = dot( transformedNormal, dirVector );",
  4146. "vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  4147. "#ifdef DOUBLE_SIDED",
  4148. "vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  4149. "#ifdef WRAP_AROUND",
  4150. "vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  4151. "#endif",
  4152. "#endif",
  4153. "#ifdef WRAP_AROUND",
  4154. "vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  4155. "directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );",
  4156. "#ifdef DOUBLE_SIDED",
  4157. "directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );",
  4158. "#endif",
  4159. "#endif",
  4160. "vLightFront += directionalLightColor[ i ] * directionalLightWeighting;",
  4161. "#ifdef DOUBLE_SIDED",
  4162. "vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;",
  4163. "#endif",
  4164. "}",
  4165. "#endif",
  4166. "#if MAX_POINT_LIGHTS > 0",
  4167. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  4168. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  4169. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  4170. "float lDistance = 1.0;",
  4171. "if ( pointLightDistance[ i ] > 0.0 )",
  4172. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  4173. "lVector = normalize( lVector );",
  4174. "float dotProduct = dot( transformedNormal, lVector );",
  4175. "vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  4176. "#ifdef DOUBLE_SIDED",
  4177. "vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  4178. "#ifdef WRAP_AROUND",
  4179. "vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  4180. "#endif",
  4181. "#endif",
  4182. "#ifdef WRAP_AROUND",
  4183. "vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  4184. "pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );",
  4185. "#ifdef DOUBLE_SIDED",
  4186. "pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );",
  4187. "#endif",
  4188. "#endif",
  4189. "vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  4190. "#ifdef DOUBLE_SIDED",
  4191. "vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;",
  4192. "#endif",
  4193. "}",
  4194. "#endif",
  4195. "vLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;",
  4196. "#ifdef DOUBLE_SIDED",
  4197. "vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;",
  4198. "#endif",
  4199. ].join("\n"),
  4200. // LIGHTS PHONG
  4201. lights_phong_pars_vertex: [
  4202. "#if MAX_POINT_LIGHTS > 0",
  4203. "#ifndef PHONG_PER_PIXEL",
  4204. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  4205. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  4206. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  4207. "#endif",
  4208. "#endif"
  4209. ].join("\n"),
  4210. lights_phong_vertex: [
  4211. "#if MAX_POINT_LIGHTS > 0",
  4212. "#ifndef PHONG_PER_PIXEL",
  4213. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  4214. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  4215. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  4216. "float lDistance = 1.0;",
  4217. "if ( pointLightDistance[ i ] > 0.0 )",
  4218. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  4219. "vPointLight[ i ] = vec4( lVector, lDistance );",
  4220. "}",
  4221. "#endif",
  4222. "#endif"
  4223. ].join("\n"),
  4224. lights_phong_pars_fragment: [
  4225. "uniform vec3 ambientLightColor;",
  4226. "#if MAX_DIR_LIGHTS > 0",
  4227. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  4228. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  4229. "#endif",
  4230. "#if MAX_POINT_LIGHTS > 0",
  4231. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  4232. "#ifdef PHONG_PER_PIXEL",
  4233. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  4234. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  4235. "#else",
  4236. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  4237. "#endif",
  4238. "#endif",
  4239. "#ifdef WRAP_AROUND",
  4240. "uniform vec3 wrapRGB;",
  4241. "#endif",
  4242. "varying vec3 vViewPosition;",
  4243. "varying vec3 vNormal;"
  4244. ].join("\n"),
  4245. lights_phong_fragment: [
  4246. "vec3 normal = normalize( vNormal );",
  4247. "vec3 viewPosition = normalize( vViewPosition );",
  4248. "#ifdef DOUBLE_SIDED",
  4249. "normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  4250. "#endif",
  4251. "#if MAX_POINT_LIGHTS > 0",
  4252. "vec3 pointDiffuse = vec3( 0.0 );",
  4253. "vec3 pointSpecular = vec3( 0.0 );",
  4254. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  4255. "#ifdef PHONG_PER_PIXEL",
  4256. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  4257. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  4258. "float lDistance = 1.0;",
  4259. "if ( pointLightDistance[ i ] > 0.0 )",
  4260. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  4261. "lVector = normalize( lVector );",
  4262. "#else",
  4263. "vec3 lVector = normalize( vPointLight[ i ].xyz );",
  4264. "float lDistance = vPointLight[ i ].w;",
  4265. "#endif",
  4266. // diffuse
  4267. "float dotProduct = dot( normal, lVector );",
  4268. "#ifdef WRAP_AROUND",
  4269. "float pointDiffuseWeightFull = max( dotProduct, 0.0 );",
  4270. "float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  4271. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  4272. "#else",
  4273. "float pointDiffuseWeight = max( dotProduct, 0.0 );",
  4274. "#endif",
  4275. "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
  4276. // specular
  4277. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  4278. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  4279. "float pointSpecularWeight = max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  4280. "#ifdef PHYSICALLY_BASED_SHADING",
  4281. // 2.0 => 2.0001 is hack to work around ANGLE bug
  4282. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  4283. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, pointHalfVector ), 5.0 );",
  4284. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance * specularNormalization;",
  4285. "#else",
  4286. "pointSpecular += specular * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance;",
  4287. "#endif",
  4288. "}",
  4289. "#endif",
  4290. "#if MAX_DIR_LIGHTS > 0",
  4291. "vec3 dirDiffuse = vec3( 0.0 );",
  4292. "vec3 dirSpecular = vec3( 0.0 );" ,
  4293. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  4294. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  4295. "vec3 dirVector = normalize( lDirection.xyz );",
  4296. // diffuse
  4297. "float dotProduct = dot( normal, dirVector );",
  4298. "#ifdef WRAP_AROUND",
  4299. "float dirDiffuseWeightFull = max( dotProduct, 0.0 );",
  4300. "float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  4301. "vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );",
  4302. "#else",
  4303. "float dirDiffuseWeight = max( dotProduct, 0.0 );",
  4304. "#endif",
  4305. "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
  4306. // specular
  4307. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  4308. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  4309. "float dirSpecularWeight = max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  4310. "#ifdef PHYSICALLY_BASED_SHADING",
  4311. /*
  4312. // fresnel term from skin shader
  4313. "const float F0 = 0.128;",
  4314. "float base = 1.0 - dot( viewPosition, dirHalfVector );",
  4315. "float exponential = pow( base, 5.0 );",
  4316. "float fresnel = exponential + F0 * ( 1.0 - exponential );",
  4317. */
  4318. /*
  4319. // fresnel term from fresnel shader
  4320. "const float mFresnelBias = 0.08;",
  4321. "const float mFresnelScale = 0.3;",
  4322. "const float mFresnelPower = 5.0;",
  4323. "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
  4324. */
  4325. // 2.0 => 2.0001 is hack to work around ANGLE bug
  4326. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  4327. //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
  4328. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  4329. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  4330. "#else",
  4331. "dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  4332. "#endif",
  4333. "}",
  4334. "#endif",
  4335. "vec3 totalDiffuse = vec3( 0.0 );",
  4336. "vec3 totalSpecular = vec3( 0.0 );",
  4337. "#if MAX_DIR_LIGHTS > 0",
  4338. "totalDiffuse += dirDiffuse;",
  4339. "totalSpecular += dirSpecular;",
  4340. "#endif",
  4341. "#if MAX_POINT_LIGHTS > 0",
  4342. "totalDiffuse += pointDiffuse;",
  4343. "totalSpecular += pointSpecular;",
  4344. "#endif",
  4345. "#ifdef METAL",
  4346. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  4347. "#else",
  4348. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  4349. "#endif"
  4350. ].join("\n"),
  4351. // VERTEX COLORS
  4352. color_pars_fragment: [
  4353. "#ifdef USE_COLOR",
  4354. "varying vec3 vColor;",
  4355. "#endif"
  4356. ].join("\n"),
  4357. color_fragment: [
  4358. "#ifdef USE_COLOR",
  4359. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  4360. "#endif"
  4361. ].join("\n"),
  4362. color_pars_vertex: [
  4363. "#ifdef USE_COLOR",
  4364. "varying vec3 vColor;",
  4365. "#endif"
  4366. ].join("\n"),
  4367. color_vertex: [
  4368. "#ifdef USE_COLOR",
  4369. "#ifdef GAMMA_INPUT",
  4370. "vColor = color * color;",
  4371. "#else",
  4372. "vColor = color;",
  4373. "#endif",
  4374. "#endif"
  4375. ].join("\n"),
  4376. // SKINNING
  4377. skinning_pars_vertex: [
  4378. "#ifdef USE_SKINNING",
  4379. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  4380. "#endif"
  4381. ].join("\n"),
  4382. skinning_vertex: [
  4383. "#ifdef USE_SKINNING",
  4384. "gl_Position = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
  4385. "gl_Position += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
  4386. "gl_Position = projectionMatrix * modelViewMatrix * gl_Position;",
  4387. "#endif"
  4388. ].join("\n"),
  4389. // MORPHING
  4390. morphtarget_pars_vertex: [
  4391. "#ifdef USE_MORPHTARGETS",
  4392. "#ifndef USE_MORPHNORMALS",
  4393. "uniform float morphTargetInfluences[ 8 ];",
  4394. "#else",
  4395. "uniform float morphTargetInfluences[ 4 ];",
  4396. "#endif",
  4397. "#endif"
  4398. ].join("\n"),
  4399. morphtarget_vertex: [
  4400. "#ifdef USE_MORPHTARGETS",
  4401. "vec3 morphed = vec3( 0.0 );",
  4402. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  4403. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  4404. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  4405. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  4406. "#ifndef USE_MORPHNORMALS",
  4407. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  4408. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  4409. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  4410. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  4411. "#endif",
  4412. "morphed += position;",
  4413. "gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );",
  4414. "#endif"
  4415. ].join("\n"),
  4416. default_vertex : [
  4417. "#ifndef USE_MORPHTARGETS",
  4418. "#ifndef USE_SKINNING",
  4419. "gl_Position = projectionMatrix * mvPosition;",
  4420. "#endif",
  4421. "#endif"
  4422. ].join("\n"),
  4423. morphnormal_vertex: [
  4424. "#ifdef USE_MORPHNORMALS",
  4425. "vec3 morphedNormal = vec3( 0.0 );",
  4426. "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
  4427. "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
  4428. "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
  4429. "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
  4430. "morphedNormal += normal;",
  4431. "vec3 transformedNormal = normalMatrix * morphedNormal;",
  4432. "#else",
  4433. "vec3 transformedNormal = normalMatrix * normal;",
  4434. "#endif"
  4435. ].join("\n"),
  4436. // SHADOW MAP
  4437. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  4438. // http://spidergl.org/example.php?id=6
  4439. // http://fabiensanglard.net/shadowmapping
  4440. shadowmap_pars_fragment: [
  4441. "#ifdef USE_SHADOWMAP",
  4442. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  4443. "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
  4444. "uniform float shadowDarkness[ MAX_SHADOWS ];",
  4445. "uniform float shadowBias[ MAX_SHADOWS ];",
  4446. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  4447. "float unpackDepth( const in vec4 rgba_depth ) {",
  4448. "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 );",
  4449. "float depth = dot( rgba_depth, bit_shift );",
  4450. "return depth;",
  4451. "}",
  4452. "#endif"
  4453. ].join("\n"),
  4454. shadowmap_fragment: [
  4455. "#ifdef USE_SHADOWMAP",
  4456. "#ifdef SHADOWMAP_DEBUG",
  4457. "vec3 frustumColors[3];",
  4458. "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
  4459. "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
  4460. "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
  4461. "#endif",
  4462. "#ifdef SHADOWMAP_CASCADE",
  4463. "int inFrustumCount = 0;",
  4464. "#endif",
  4465. "float fDepth;",
  4466. "vec3 shadowColor = vec3( 1.0 );",
  4467. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  4468. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  4469. // "if ( something && something )" breaks ATI OpenGL shader compiler
  4470. // "if ( all( something, something ) )" using this instead
  4471. "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
  4472. "bool inFrustum = all( inFrustumVec );",
  4473. // don't shadow pixels outside of light frustum
  4474. // use just first frustum (for cascades)
  4475. // don't shadow pixels behind far plane of light frustum
  4476. "#ifdef SHADOWMAP_CASCADE",
  4477. "inFrustumCount += int( inFrustum );",
  4478. "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
  4479. "#else",
  4480. "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
  4481. "#endif",
  4482. "bool frustumTest = all( frustumTestVec );",
  4483. "if ( frustumTest ) {",
  4484. "shadowCoord.z += shadowBias[ i ];",
  4485. "#ifdef SHADOWMAP_SOFT",
  4486. // Percentage-close filtering
  4487. // (9 pixel kernel)
  4488. // http://fabiensanglard.net/shadowmappingPCF/
  4489. "float shadow = 0.0;",
  4490. /*
  4491. // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
  4492. // must enroll loop manually
  4493. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  4494. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  4495. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  4496. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  4497. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  4498. "float fDepth = unpackDepth( rgbaDepth );",
  4499. "if ( fDepth < shadowCoord.z )",
  4500. "shadow += 1.0;",
  4501. "}",
  4502. "shadow /= 9.0;",
  4503. */
  4504. "const float shadowDelta = 1.0 / 9.0;",
  4505. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  4506. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  4507. "float dx0 = -1.25 * xPixelOffset;",
  4508. "float dy0 = -1.25 * yPixelOffset;",
  4509. "float dx1 = 1.25 * xPixelOffset;",
  4510. "float dy1 = 1.25 * yPixelOffset;",
  4511. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  4512. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4513. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  4514. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4515. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  4516. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4517. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  4518. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4519. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  4520. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4521. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  4522. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4523. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  4524. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4525. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  4526. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4527. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  4528. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4529. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  4530. "#else",
  4531. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  4532. "float fDepth = unpackDepth( rgbaDepth );",
  4533. "if ( fDepth < shadowCoord.z )",
  4534. // spot with multiple shadows is darker
  4535. "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
  4536. // spot with multiple shadows has the same color as single shadow spot
  4537. //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
  4538. "#endif",
  4539. "}",
  4540. "#ifdef SHADOWMAP_DEBUG",
  4541. "#ifdef SHADOWMAP_CASCADE",
  4542. "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
  4543. "#else",
  4544. "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
  4545. "#endif",
  4546. "#endif",
  4547. "}",
  4548. "#ifdef GAMMA_OUTPUT",
  4549. "shadowColor *= shadowColor;",
  4550. "#endif",
  4551. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  4552. "#endif"
  4553. ].join("\n"),
  4554. shadowmap_pars_vertex: [
  4555. "#ifdef USE_SHADOWMAP",
  4556. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  4557. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  4558. "#endif"
  4559. ].join("\n"),
  4560. shadowmap_vertex: [
  4561. "#ifdef USE_SHADOWMAP",
  4562. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  4563. "#ifdef USE_MORPHTARGETS",
  4564. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( morphed, 1.0 );",
  4565. "#else",
  4566. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( position, 1.0 );",
  4567. "#endif",
  4568. "}",
  4569. "#endif"
  4570. ].join("\n"),
  4571. // ALPHATEST
  4572. alphatest_fragment: [
  4573. "#ifdef ALPHATEST",
  4574. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  4575. "#endif"
  4576. ].join("\n"),
  4577. // LINEAR SPACE
  4578. linear_to_gamma_fragment: [
  4579. "#ifdef GAMMA_OUTPUT",
  4580. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  4581. "#endif"
  4582. ].join("\n"),
  4583. };
  4584. THREE.UniformsUtils = {
  4585. merge: function ( uniforms ) {
  4586. var u, p, tmp, merged = {};
  4587. for ( u = 0; u < uniforms.length; u++ ) {
  4588. tmp = this.clone( uniforms[ u ] );
  4589. for ( p in tmp ) {
  4590. merged[ p ] = tmp[ p ];
  4591. }
  4592. }
  4593. return merged;
  4594. },
  4595. clone: function ( uniforms_src ) {
  4596. var u, p, parameter, parameter_src, uniforms_dst = {};
  4597. for ( u in uniforms_src ) {
  4598. uniforms_dst[ u ] = {};
  4599. for ( p in uniforms_src[ u ] ) {
  4600. parameter_src = uniforms_src[ u ][ p ];
  4601. if ( parameter_src instanceof THREE.Color ||
  4602. parameter_src instanceof THREE.Vector2 ||
  4603. parameter_src instanceof THREE.Vector3 ||
  4604. parameter_src instanceof THREE.Vector4 ||
  4605. parameter_src instanceof THREE.Matrix4 ||
  4606. parameter_src instanceof THREE.Texture ) {
  4607. uniforms_dst[ u ][ p ] = parameter_src.clone();
  4608. } else if ( parameter_src instanceof Array ) {
  4609. uniforms_dst[ u ][ p ] = parameter_src.slice();
  4610. } else {
  4611. uniforms_dst[ u ][ p ] = parameter_src;
  4612. }
  4613. }
  4614. }
  4615. return uniforms_dst;
  4616. }
  4617. };
  4618. THREE.UniformsLib = {
  4619. common: {
  4620. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  4621. "opacity" : { type: "f", value: 1.0 },
  4622. "map" : { type: "t", value: 0, texture: null },
  4623. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  4624. "lightMap" : { type: "t", value: 2, texture: null },
  4625. "envMap" : { type: "t", value: 1, texture: null },
  4626. "flipEnvMap" : { type: "f", value: -1 },
  4627. "useRefract" : { type: "i", value: 0 },
  4628. "reflectivity" : { type: "f", value: 1.0 },
  4629. "refractionRatio" : { type: "f", value: 0.98 },
  4630. "combine" : { type: "i", value: 0 },
  4631. "morphTargetInfluences" : { type: "f", value: 0 }
  4632. },
  4633. fog : {
  4634. "fogDensity" : { type: "f", value: 0.00025 },
  4635. "fogNear" : { type: "f", value: 1 },
  4636. "fogFar" : { type: "f", value: 2000 },
  4637. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  4638. },
  4639. lights: {
  4640. "ambientLightColor" : { type: "fv", value: [] },
  4641. "directionalLightDirection" : { type: "fv", value: [] },
  4642. "directionalLightColor" : { type: "fv", value: [] },
  4643. "pointLightColor" : { type: "fv", value: [] },
  4644. "pointLightPosition" : { type: "fv", value: [] },
  4645. "pointLightDistance" : { type: "fv1", value: [] }
  4646. },
  4647. particle: {
  4648. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  4649. "opacity" : { type: "f", value: 1.0 },
  4650. "size" : { type: "f", value: 1.0 },
  4651. "scale" : { type: "f", value: 1.0 },
  4652. "map" : { type: "t", value: 0, texture: null },
  4653. "fogDensity" : { type: "f", value: 0.00025 },
  4654. "fogNear" : { type: "f", value: 1 },
  4655. "fogFar" : { type: "f", value: 2000 },
  4656. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  4657. },
  4658. shadowmap: {
  4659. "shadowMap": { type: "tv", value: 6, texture: [] },
  4660. "shadowMapSize": { type: "v2v", value: [] },
  4661. "shadowBias" : { type: "fv1", value: [] },
  4662. "shadowDarkness": { type: "fv1", value: [] },
  4663. "shadowMatrix" : { type: "m4v", value: [] },
  4664. }
  4665. };
  4666. THREE.ShaderLib = {
  4667. 'depth': {
  4668. uniforms: {
  4669. "mNear": { type: "f", value: 1.0 },
  4670. "mFar" : { type: "f", value: 2000.0 },
  4671. "opacity" : { type: "f", value: 1.0 }
  4672. },
  4673. vertexShader: [
  4674. "void main() {",
  4675. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  4676. "}"
  4677. ].join("\n"),
  4678. fragmentShader: [
  4679. "uniform float mNear;",
  4680. "uniform float mFar;",
  4681. "uniform float opacity;",
  4682. "void main() {",
  4683. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  4684. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  4685. "gl_FragColor = vec4( vec3( color ), opacity );",
  4686. "}"
  4687. ].join("\n")
  4688. },
  4689. 'normal': {
  4690. uniforms: {
  4691. "opacity" : { type: "f", value: 1.0 }
  4692. },
  4693. vertexShader: [
  4694. "varying vec3 vNormal;",
  4695. "void main() {",
  4696. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4697. "vNormal = normalMatrix * normal;",
  4698. "gl_Position = projectionMatrix * mvPosition;",
  4699. "}"
  4700. ].join("\n"),
  4701. fragmentShader: [
  4702. "uniform float opacity;",
  4703. "varying vec3 vNormal;",
  4704. "void main() {",
  4705. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  4706. "}"
  4707. ].join("\n")
  4708. },
  4709. 'basic': {
  4710. uniforms: THREE.UniformsUtils.merge( [
  4711. THREE.UniformsLib[ "common" ],
  4712. THREE.UniformsLib[ "fog" ],
  4713. THREE.UniformsLib[ "shadowmap" ]
  4714. ] ),
  4715. vertexShader: [
  4716. THREE.ShaderChunk[ "map_pars_vertex" ],
  4717. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  4718. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  4719. THREE.ShaderChunk[ "color_pars_vertex" ],
  4720. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  4721. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4722. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4723. "void main() {",
  4724. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4725. THREE.ShaderChunk[ "map_vertex" ],
  4726. THREE.ShaderChunk[ "lightmap_vertex" ],
  4727. THREE.ShaderChunk[ "envmap_vertex" ],
  4728. THREE.ShaderChunk[ "color_vertex" ],
  4729. THREE.ShaderChunk[ "skinning_vertex" ],
  4730. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4731. THREE.ShaderChunk[ "default_vertex" ],
  4732. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4733. "}"
  4734. ].join("\n"),
  4735. fragmentShader: [
  4736. "uniform vec3 diffuse;",
  4737. "uniform float opacity;",
  4738. THREE.ShaderChunk[ "color_pars_fragment" ],
  4739. THREE.ShaderChunk[ "map_pars_fragment" ],
  4740. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  4741. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  4742. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4743. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4744. "void main() {",
  4745. "gl_FragColor = vec4( diffuse, opacity );",
  4746. THREE.ShaderChunk[ "map_fragment" ],
  4747. THREE.ShaderChunk[ "alphatest_fragment" ],
  4748. THREE.ShaderChunk[ "lightmap_fragment" ],
  4749. THREE.ShaderChunk[ "color_fragment" ],
  4750. THREE.ShaderChunk[ "envmap_fragment" ],
  4751. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4752. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  4753. THREE.ShaderChunk[ "fog_fragment" ],
  4754. "}"
  4755. ].join("\n")
  4756. },
  4757. 'lambert': {
  4758. uniforms: THREE.UniformsUtils.merge( [
  4759. THREE.UniformsLib[ "common" ],
  4760. THREE.UniformsLib[ "fog" ],
  4761. THREE.UniformsLib[ "lights" ],
  4762. THREE.UniformsLib[ "shadowmap" ],
  4763. {
  4764. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  4765. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  4766. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  4767. }
  4768. ] ),
  4769. vertexShader: [
  4770. "varying vec3 vLightFront;",
  4771. "#ifdef DOUBLE_SIDED",
  4772. "varying vec3 vLightBack;",
  4773. "#endif",
  4774. THREE.ShaderChunk[ "map_pars_vertex" ],
  4775. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  4776. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  4777. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  4778. THREE.ShaderChunk[ "color_pars_vertex" ],
  4779. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  4780. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4781. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4782. "void main() {",
  4783. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4784. THREE.ShaderChunk[ "map_vertex" ],
  4785. THREE.ShaderChunk[ "lightmap_vertex" ],
  4786. THREE.ShaderChunk[ "envmap_vertex" ],
  4787. THREE.ShaderChunk[ "color_vertex" ],
  4788. THREE.ShaderChunk[ "morphnormal_vertex" ],
  4789. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  4790. THREE.ShaderChunk[ "skinning_vertex" ],
  4791. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4792. THREE.ShaderChunk[ "default_vertex" ],
  4793. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4794. "}"
  4795. ].join("\n"),
  4796. fragmentShader: [
  4797. "uniform float opacity;",
  4798. "varying vec3 vLightFront;",
  4799. "#ifdef DOUBLE_SIDED",
  4800. "varying vec3 vLightBack;",
  4801. "#endif",
  4802. THREE.ShaderChunk[ "color_pars_fragment" ],
  4803. THREE.ShaderChunk[ "map_pars_fragment" ],
  4804. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  4805. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  4806. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4807. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4808. "void main() {",
  4809. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  4810. THREE.ShaderChunk[ "map_fragment" ],
  4811. THREE.ShaderChunk[ "alphatest_fragment" ],
  4812. "#ifdef DOUBLE_SIDED",
  4813. //"float isFront = float( gl_FrontFacing );",
  4814. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  4815. "if ( gl_FrontFacing )",
  4816. "gl_FragColor.xyz *= vLightFront;",
  4817. "else",
  4818. "gl_FragColor.xyz *= vLightBack;",
  4819. "#else",
  4820. "gl_FragColor.xyz *= vLightFront;",
  4821. "#endif",
  4822. THREE.ShaderChunk[ "lightmap_fragment" ],
  4823. THREE.ShaderChunk[ "color_fragment" ],
  4824. THREE.ShaderChunk[ "envmap_fragment" ],
  4825. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4826. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  4827. THREE.ShaderChunk[ "fog_fragment" ],
  4828. "}"
  4829. ].join("\n")
  4830. },
  4831. 'phong': {
  4832. uniforms: THREE.UniformsUtils.merge( [
  4833. THREE.UniformsLib[ "common" ],
  4834. THREE.UniformsLib[ "fog" ],
  4835. THREE.UniformsLib[ "lights" ],
  4836. THREE.UniformsLib[ "shadowmap" ],
  4837. {
  4838. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  4839. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  4840. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  4841. "shininess": { type: "f", value: 30 },
  4842. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  4843. }
  4844. ] ),
  4845. vertexShader: [
  4846. "varying vec3 vViewPosition;",
  4847. "varying vec3 vNormal;",
  4848. THREE.ShaderChunk[ "map_pars_vertex" ],
  4849. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  4850. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  4851. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  4852. THREE.ShaderChunk[ "color_pars_vertex" ],
  4853. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  4854. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4855. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4856. "void main() {",
  4857. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4858. THREE.ShaderChunk[ "map_vertex" ],
  4859. THREE.ShaderChunk[ "lightmap_vertex" ],
  4860. THREE.ShaderChunk[ "envmap_vertex" ],
  4861. THREE.ShaderChunk[ "color_vertex" ],
  4862. "#ifndef USE_ENVMAP",
  4863. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  4864. "#endif",
  4865. "vViewPosition = -mvPosition.xyz;",
  4866. THREE.ShaderChunk[ "morphnormal_vertex" ],
  4867. "vNormal = transformedNormal;",
  4868. THREE.ShaderChunk[ "lights_phong_vertex" ],
  4869. THREE.ShaderChunk[ "skinning_vertex" ],
  4870. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4871. THREE.ShaderChunk[ "default_vertex" ],
  4872. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4873. "}"
  4874. ].join("\n"),
  4875. fragmentShader: [
  4876. "uniform vec3 diffuse;",
  4877. "uniform float opacity;",
  4878. "uniform vec3 ambient;",
  4879. "uniform vec3 emissive;",
  4880. "uniform vec3 specular;",
  4881. "uniform float shininess;",
  4882. THREE.ShaderChunk[ "color_pars_fragment" ],
  4883. THREE.ShaderChunk[ "map_pars_fragment" ],
  4884. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  4885. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  4886. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4887. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  4888. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4889. "void main() {",
  4890. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  4891. THREE.ShaderChunk[ "map_fragment" ],
  4892. THREE.ShaderChunk[ "alphatest_fragment" ],
  4893. THREE.ShaderChunk[ "lights_phong_fragment" ],
  4894. THREE.ShaderChunk[ "lightmap_fragment" ],
  4895. THREE.ShaderChunk[ "color_fragment" ],
  4896. THREE.ShaderChunk[ "envmap_fragment" ],
  4897. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4898. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  4899. THREE.ShaderChunk[ "fog_fragment" ],
  4900. "}"
  4901. ].join("\n")
  4902. },
  4903. 'particle_basic': {
  4904. uniforms: THREE.UniformsUtils.merge( [
  4905. THREE.UniformsLib[ "particle" ],
  4906. THREE.UniformsLib[ "shadowmap" ]
  4907. ] ),
  4908. vertexShader: [
  4909. "uniform float size;",
  4910. "uniform float scale;",
  4911. THREE.ShaderChunk[ "color_pars_vertex" ],
  4912. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4913. "void main() {",
  4914. THREE.ShaderChunk[ "color_vertex" ],
  4915. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4916. "#ifdef USE_SIZEATTENUATION",
  4917. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  4918. "#else",
  4919. "gl_PointSize = size;",
  4920. "#endif",
  4921. "gl_Position = projectionMatrix * mvPosition;",
  4922. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4923. "}"
  4924. ].join("\n"),
  4925. fragmentShader: [
  4926. "uniform vec3 psColor;",
  4927. "uniform float opacity;",
  4928. THREE.ShaderChunk[ "color_pars_fragment" ],
  4929. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  4930. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4931. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4932. "void main() {",
  4933. "gl_FragColor = vec4( psColor, opacity );",
  4934. THREE.ShaderChunk[ "map_particle_fragment" ],
  4935. THREE.ShaderChunk[ "alphatest_fragment" ],
  4936. THREE.ShaderChunk[ "color_fragment" ],
  4937. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4938. THREE.ShaderChunk[ "fog_fragment" ],
  4939. "}"
  4940. ].join("\n")
  4941. },
  4942. // Depth encoding into RGBA texture
  4943. // based on SpiderGL shadow map example
  4944. // http://spidergl.org/example.php?id=6
  4945. // originally from
  4946. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  4947. // see also here:
  4948. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  4949. 'depthRGBA': {
  4950. uniforms: {},
  4951. vertexShader: [
  4952. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4953. "void main() {",
  4954. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4955. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4956. THREE.ShaderChunk[ "default_vertex" ],
  4957. "}"
  4958. ].join("\n"),
  4959. fragmentShader: [
  4960. "vec4 pack_depth( const in float depth ) {",
  4961. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  4962. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  4963. "vec4 res = fract( depth * bit_shift );",
  4964. "res -= res.xxyz * bit_mask;",
  4965. "return res;",
  4966. "}",
  4967. "void main() {",
  4968. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  4969. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  4970. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  4971. //"gl_FragData[ 0 ] = pack_depth( z );",
  4972. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  4973. "}"
  4974. ].join("\n")
  4975. }
  4976. };/**
  4977. * @author supereggbert / http://www.paulbrunt.co.uk/
  4978. * @author mrdoob / http://mrdoob.com/
  4979. * @author alteredq / http://alteredqualia.com/
  4980. * @author szimek / https://github.com/szimek/
  4981. */
  4982. THREE.WebGLRenderer = function ( parameters ) {
  4983. // constructor parameters
  4984. parameters = parameters || {};
  4985. var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
  4986. _precision = parameters.precision !== undefined ? parameters.precision : 'highp',
  4987. _alpha = parameters.alpha !== undefined ? parameters.alpha : true,
  4988. _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
  4989. _antialias = parameters.antialias !== undefined ? parameters.antialias : false,
  4990. _stencil = parameters.stencil !== undefined ? parameters.stencil : true,
  4991. _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
  4992. _clearColor = parameters.clearColor !== undefined ? new THREE.Color( parameters.clearColor ) : new THREE.Color( 0x000000 ),
  4993. _clearAlpha = parameters.clearAlpha !== undefined ? parameters.clearAlpha : 0,
  4994. _maxLights = parameters.maxLights !== undefined ? parameters.maxLights : 4;
  4995. // public properties
  4996. this.domElement = _canvas;
  4997. this.context = null;
  4998. // clearing
  4999. this.autoClear = true;
  5000. this.autoClearColor = true;
  5001. this.autoClearDepth = true;
  5002. this.autoClearStencil = true;
  5003. // scene graph
  5004. this.sortObjects = true;
  5005. this.autoUpdateObjects = true;
  5006. this.autoUpdateScene = true;
  5007. // physically based shading
  5008. this.gammaInput = false;
  5009. this.gammaOutput = false;
  5010. this.physicallyBasedShading = false;
  5011. // shadow map
  5012. this.shadowMapEnabled = false;
  5013. this.shadowMapAutoUpdate = true;
  5014. this.shadowMapSoft = true;
  5015. this.shadowMapCullFrontFaces = true;
  5016. this.shadowMapDebug = false;
  5017. this.shadowMapCascade = false;
  5018. // morphs
  5019. this.maxMorphTargets = 8;
  5020. this.maxMorphNormals = 4;
  5021. // flags
  5022. this.autoScaleCubemaps = true;
  5023. // custom render plugins
  5024. this.renderPluginsPre = [];
  5025. this.renderPluginsPost = [];
  5026. // info
  5027. this.info = {
  5028. memory: {
  5029. programs: 0,
  5030. geometries: 0,
  5031. textures: 0
  5032. },
  5033. render: {
  5034. calls: 0,
  5035. vertices: 0,
  5036. faces: 0,
  5037. points: 0
  5038. }
  5039. };
  5040. // internal properties
  5041. var _this = this,
  5042. _gl,
  5043. _programs = [],
  5044. // internal state cache
  5045. _currentProgram = null,
  5046. _currentFramebuffer = null,
  5047. _currentMaterialId = -1,
  5048. _currentGeometryGroupHash = null,
  5049. _currentCamera = null,
  5050. _geometryGroupCounter = 0,
  5051. // GL state cache
  5052. _oldDoubleSided = null,
  5053. _oldFlipSided = null,
  5054. _oldBlending = null,
  5055. _oldBlendEquation = null,
  5056. _oldBlendSrc = null,
  5057. _oldBlendDst = null,
  5058. _oldDepthTest = null,
  5059. _oldDepthWrite = null,
  5060. _oldPolygonOffset = null,
  5061. _oldPolygonOffsetFactor = null,
  5062. _oldPolygonOffsetUnits = null,
  5063. _oldLineWidth = null,
  5064. _viewportX = 0,
  5065. _viewportY = 0,
  5066. _viewportWidth = 0,
  5067. _viewportHeight = 0,
  5068. _currentWidth = 0,
  5069. _currentHeight = 0,
  5070. // frustum
  5071. _frustum = new THREE.Frustum(),
  5072. // camera matrices cache
  5073. _projScreenMatrix = new THREE.Matrix4(),
  5074. _projScreenMatrixPS = new THREE.Matrix4(),
  5075. _vector3 = new THREE.Vector4(),
  5076. // light arrays cache
  5077. _direction = new THREE.Vector3(),
  5078. _lights = {
  5079. ambient: [ 0, 0, 0 ],
  5080. directional: { length: 0, colors: new Array(), positions: new Array() },
  5081. point: { length: 0, colors: new Array(), positions: new Array(), distances: new Array() }
  5082. };
  5083. // initialize
  5084. _gl = initGL();
  5085. setDefaultGLState();
  5086. this.context = _gl;
  5087. // GPU capabilities
  5088. var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ),
  5089. _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE ),
  5090. _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE );
  5091. // API
  5092. this.getContext = function () {
  5093. return _gl;
  5094. };
  5095. this.supportsVertexTextures = function () {
  5096. return _maxVertexTextures > 0;
  5097. };
  5098. this.setSize = function ( width, height ) {
  5099. _canvas.width = width;
  5100. _canvas.height = height;
  5101. this.setViewport( 0, 0, _canvas.width, _canvas.height );
  5102. };
  5103. this.setViewport = function ( x, y, width, height ) {
  5104. _viewportX = x;
  5105. _viewportY = y;
  5106. _viewportWidth = width;
  5107. _viewportHeight = height;
  5108. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  5109. };
  5110. this.setScissor = function ( x, y, width, height ) {
  5111. _gl.scissor( x, y, width, height );
  5112. };
  5113. this.enableScissorTest = function ( enable ) {
  5114. enable ? _gl.enable( _gl.SCISSOR_TEST ) : _gl.disable( _gl.SCISSOR_TEST );
  5115. };
  5116. // Clearing
  5117. this.setClearColorHex = function ( hex, alpha ) {
  5118. _clearColor.setHex( hex );
  5119. _clearAlpha = alpha;
  5120. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  5121. };
  5122. this.setClearColor = function ( color, alpha ) {
  5123. _clearColor.copy( color );
  5124. _clearAlpha = alpha;
  5125. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  5126. };
  5127. this.getClearColor = function () {
  5128. return _clearColor;
  5129. };
  5130. this.getClearAlpha = function () {
  5131. return _clearAlpha;
  5132. };
  5133. this.clear = function ( color, depth, stencil ) {
  5134. var bits = 0;
  5135. if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
  5136. if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
  5137. if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;
  5138. _gl.clear( bits );
  5139. };
  5140. this.clearTarget = function ( renderTarget, color, depth, stencil ) {
  5141. this.setRenderTarget( renderTarget );
  5142. this.clear( color, depth, stencil );
  5143. };
  5144. // Plugins
  5145. this.addPostPlugin = function ( plugin ) {
  5146. plugin.init( this );
  5147. this.renderPluginsPost.push( plugin );
  5148. };
  5149. this.addPrePlugin = function ( plugin ) {
  5150. plugin.init( this );
  5151. this.renderPluginsPre.push( plugin );
  5152. };
  5153. // Deallocation
  5154. this.deallocateObject = function ( object ) {
  5155. if ( ! object.__webglInit ) return;
  5156. object.__webglInit = false;
  5157. delete object._modelViewMatrix;
  5158. delete object._normalMatrix;
  5159. delete object._normalMatrixArray;
  5160. delete object._modelViewMatrixArray;
  5161. delete object._objectMatrixArray;
  5162. if ( object instanceof THREE.Mesh ) {
  5163. for ( var g in object.geometry.geometryGroups ) {
  5164. deleteMeshBuffers( object.geometry.geometryGroups[ g ] );
  5165. }
  5166. } else if ( object instanceof THREE.Ribbon ) {
  5167. deleteRibbonBuffers( object.geometry );
  5168. } else if ( object instanceof THREE.Line ) {
  5169. deleteLineBuffers( object.geometry );
  5170. } else if ( object instanceof THREE.ParticleSystem ) {
  5171. deleteParticleBuffers( object.geometry );
  5172. }
  5173. };
  5174. this.deallocateTexture = function ( texture ) {
  5175. if ( ! texture.__webglInit ) return;
  5176. texture.__webglInit = false;
  5177. _gl.deleteTexture( texture.__webglTexture );
  5178. _this.info.memory.textures --;
  5179. };
  5180. this.deallocateRenderTarget = function ( renderTarget ) {
  5181. if ( !renderTarget || ! renderTarget.__webglTexture ) return;
  5182. _gl.deleteTexture( renderTarget.__webglTexture );
  5183. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  5184. for ( var i = 0; i < 6; i ++ ) {
  5185. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] );
  5186. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] );
  5187. }
  5188. } else {
  5189. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer );
  5190. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer );
  5191. }
  5192. };
  5193. // Rendering
  5194. this.updateShadowMap = function ( scene, camera ) {
  5195. _currentProgram = null;
  5196. _oldBlending = -1;
  5197. _oldDepthTest = -1;
  5198. _oldDepthWrite = -1;
  5199. _currentGeometryGroupHash = -1;
  5200. _currentMaterialId = -1;
  5201. this.shadowMapPlugin.update( scene, camera );
  5202. };
  5203. // Internal functions
  5204. // Buffer allocation
  5205. function createParticleBuffers ( geometry ) {
  5206. geometry.__webglVertexBuffer = _gl.createBuffer();
  5207. geometry.__webglColorBuffer = _gl.createBuffer();
  5208. _this.info.geometries ++;
  5209. };
  5210. function createLineBuffers ( geometry ) {
  5211. geometry.__webglVertexBuffer = _gl.createBuffer();
  5212. geometry.__webglColorBuffer = _gl.createBuffer();
  5213. _this.info.memory.geometries ++;
  5214. };
  5215. function createRibbonBuffers ( geometry ) {
  5216. geometry.__webglVertexBuffer = _gl.createBuffer();
  5217. geometry.__webglColorBuffer = _gl.createBuffer();
  5218. _this.info.memory.geometries ++;
  5219. };
  5220. function createMeshBuffers ( geometryGroup ) {
  5221. geometryGroup.__webglVertexBuffer = _gl.createBuffer();
  5222. geometryGroup.__webglNormalBuffer = _gl.createBuffer();
  5223. geometryGroup.__webglTangentBuffer = _gl.createBuffer();
  5224. geometryGroup.__webglColorBuffer = _gl.createBuffer();
  5225. geometryGroup.__webglUVBuffer = _gl.createBuffer();
  5226. geometryGroup.__webglUV2Buffer = _gl.createBuffer();
  5227. geometryGroup.__webglSkinVertexABuffer = _gl.createBuffer();
  5228. geometryGroup.__webglSkinVertexBBuffer = _gl.createBuffer();
  5229. geometryGroup.__webglSkinIndicesBuffer = _gl.createBuffer();
  5230. geometryGroup.__webglSkinWeightsBuffer = _gl.createBuffer();
  5231. geometryGroup.__webglFaceBuffer = _gl.createBuffer();
  5232. geometryGroup.__webglLineBuffer = _gl.createBuffer();
  5233. var m, ml;
  5234. if ( geometryGroup.numMorphTargets ) {
  5235. geometryGroup.__webglMorphTargetsBuffers = [];
  5236. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  5237. geometryGroup.__webglMorphTargetsBuffers.push( _gl.createBuffer() );
  5238. }
  5239. }
  5240. if ( geometryGroup.numMorphNormals ) {
  5241. geometryGroup.__webglMorphNormalsBuffers = [];
  5242. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  5243. geometryGroup.__webglMorphNormalsBuffers.push( _gl.createBuffer() );
  5244. }
  5245. }
  5246. _this.info.memory.geometries ++;
  5247. };
  5248. // Buffer deallocation
  5249. function deleteParticleBuffers ( geometry ) {
  5250. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  5251. _gl.deleteBuffer( geometry.__webglColorBuffer );
  5252. _this.info.memory.geometries --;
  5253. };
  5254. function deleteLineBuffers ( geometry ) {
  5255. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  5256. _gl.deleteBuffer( geometry.__webglColorBuffer );
  5257. _this.info.memory.geometries --;
  5258. };
  5259. function deleteRibbonBuffers ( geometry ) {
  5260. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  5261. _gl.deleteBuffer( geometry.__webglColorBuffer );
  5262. _this.info.memory.geometries --;
  5263. };
  5264. function deleteMeshBuffers ( geometryGroup ) {
  5265. _gl.deleteBuffer( geometryGroup.__webglVertexBuffer );
  5266. _gl.deleteBuffer( geometryGroup.__webglNormalBuffer );
  5267. _gl.deleteBuffer( geometryGroup.__webglTangentBuffer );
  5268. _gl.deleteBuffer( geometryGroup.__webglColorBuffer );
  5269. _gl.deleteBuffer( geometryGroup.__webglUVBuffer );
  5270. _gl.deleteBuffer( geometryGroup.__webglUV2Buffer );
  5271. _gl.deleteBuffer( geometryGroup.__webglSkinVertexABuffer );
  5272. _gl.deleteBuffer( geometryGroup.__webglSkinVertexBBuffer );
  5273. _gl.deleteBuffer( geometryGroup.__webglSkinIndicesBuffer );
  5274. _gl.deleteBuffer( geometryGroup.__webglSkinWeightsBuffer );
  5275. _gl.deleteBuffer( geometryGroup.__webglFaceBuffer );
  5276. _gl.deleteBuffer( geometryGroup.__webglLineBuffer );
  5277. var m, ml;
  5278. if ( geometryGroup.numMorphTargets ) {
  5279. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  5280. _gl.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] );
  5281. }
  5282. }
  5283. if ( geometryGroup.numMorphNormals ) {
  5284. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  5285. _gl.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] );
  5286. }
  5287. }
  5288. if ( geometryGroup.__webglCustomAttributesList ) {
  5289. for ( var id in geometryGroup.__webglCustomAttributesList ) {
  5290. _gl.deleteBuffer( geometryGroup.__webglCustomAttributesList[ id ].buffer );
  5291. }
  5292. }
  5293. _this.info.memory.geometries --;
  5294. };
  5295. // Buffer initialization
  5296. function initCustomAttributes ( geometry, object ) {
  5297. var nvertices = geometry.vertices.length;
  5298. var material = object.material;
  5299. if ( material.attributes ) {
  5300. if ( geometry.__webglCustomAttributesList === undefined ) {
  5301. geometry.__webglCustomAttributesList = [];
  5302. }
  5303. for ( var a in material.attributes ) {
  5304. var attribute = material.attributes[ a ];
  5305. if( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  5306. attribute.__webglInitialized = true;
  5307. var size = 1; // "f" and "i"
  5308. if ( attribute.type === "v2" ) size = 2;
  5309. else if ( attribute.type === "v3" ) size = 3;
  5310. else if ( attribute.type === "v4" ) size = 4;
  5311. else if ( attribute.type === "c" ) size = 3;
  5312. attribute.size = size;
  5313. attribute.array = new Float32Array( nvertices * size );
  5314. attribute.buffer = _gl.createBuffer();
  5315. attribute.buffer.belongsToAttribute = a;
  5316. attribute.needsUpdate = true;
  5317. }
  5318. geometry.__webglCustomAttributesList.push( attribute );
  5319. }
  5320. }
  5321. };
  5322. function initParticleBuffers ( geometry, object ) {
  5323. var nvertices = geometry.vertices.length;
  5324. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  5325. geometry.__colorArray = new Float32Array( nvertices * 3 );
  5326. geometry.__sortArray = [];
  5327. geometry.__webglParticleCount = nvertices;
  5328. initCustomAttributes ( geometry, object );
  5329. };
  5330. function initLineBuffers ( geometry, object ) {
  5331. var nvertices = geometry.vertices.length;
  5332. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  5333. geometry.__colorArray = new Float32Array( nvertices * 3 );
  5334. geometry.__webglLineCount = nvertices;
  5335. initCustomAttributes ( geometry, object );
  5336. };
  5337. function initRibbonBuffers ( geometry ) {
  5338. var nvertices = geometry.vertices.length;
  5339. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  5340. geometry.__colorArray = new Float32Array( nvertices * 3 );
  5341. geometry.__webglVertexCount = nvertices;
  5342. };
  5343. function initMeshBuffers ( geometryGroup, object ) {
  5344. var geometry = object.geometry,
  5345. faces3 = geometryGroup.faces3,
  5346. faces4 = geometryGroup.faces4,
  5347. nvertices = faces3.length * 3 + faces4.length * 4,
  5348. ntris = faces3.length * 1 + faces4.length * 2,
  5349. nlines = faces3.length * 3 + faces4.length * 4,
  5350. material = getBufferMaterial( object, geometryGroup ),
  5351. uvType = bufferGuessUVType( material ),
  5352. normalType = bufferGuessNormalType( material ),
  5353. vertexColorType = bufferGuessVertexColorType( material );
  5354. //console.log( "uvType", uvType, "normalType", normalType, "vertexColorType", vertexColorType, object, geometryGroup, material );
  5355. geometryGroup.__vertexArray = new Float32Array( nvertices * 3 );
  5356. if ( normalType ) {
  5357. geometryGroup.__normalArray = new Float32Array( nvertices * 3 );
  5358. }
  5359. if ( geometry.hasTangents ) {
  5360. geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
  5361. }
  5362. if ( vertexColorType ) {
  5363. geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
  5364. }
  5365. if ( uvType ) {
  5366. if ( geometry.faceUvs.length > 0 || geometry.faceVertexUvs.length > 0 ) {
  5367. geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
  5368. }
  5369. if ( geometry.faceUvs.length > 1 || geometry.faceVertexUvs.length > 1 ) {
  5370. geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
  5371. }
  5372. }
  5373. if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) {
  5374. geometryGroup.__skinVertexAArray = new Float32Array( nvertices * 4 );
  5375. geometryGroup.__skinVertexBArray = new Float32Array( nvertices * 4 );
  5376. geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
  5377. geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
  5378. }
  5379. geometryGroup.__faceArray = new Uint16Array( ntris * 3 );
  5380. geometryGroup.__lineArray = new Uint16Array( nlines * 2 );
  5381. var m, ml;
  5382. if ( geometryGroup.numMorphTargets ) {
  5383. geometryGroup.__morphTargetsArrays = [];
  5384. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  5385. geometryGroup.__morphTargetsArrays.push( new Float32Array( nvertices * 3 ) );
  5386. }
  5387. }
  5388. if ( geometryGroup.numMorphNormals ) {
  5389. geometryGroup.__morphNormalsArrays = [];
  5390. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  5391. geometryGroup.__morphNormalsArrays.push( new Float32Array( nvertices * 3 ) );
  5392. }
  5393. }
  5394. geometryGroup.__webglFaceCount = ntris * 3;
  5395. geometryGroup.__webglLineCount = nlines * 2;
  5396. // custom attributes
  5397. if ( material.attributes ) {
  5398. if ( geometryGroup.__webglCustomAttributesList === undefined ) {
  5399. geometryGroup.__webglCustomAttributesList = [];
  5400. }
  5401. for ( var a in material.attributes ) {
  5402. // Do a shallow copy of the attribute object so different geometryGroup chunks use different
  5403. // attribute buffers which are correctly indexed in the setMeshBuffers function
  5404. var originalAttribute = material.attributes[ a ];
  5405. var attribute = {};
  5406. for ( var property in originalAttribute ) {
  5407. attribute[ property ] = originalAttribute[ property ];
  5408. }
  5409. if( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  5410. attribute.__webglInitialized = true;
  5411. var size = 1; // "f" and "i"
  5412. if( attribute.type === "v2" ) size = 2;
  5413. else if( attribute.type === "v3" ) size = 3;
  5414. else if( attribute.type === "v4" ) size = 4;
  5415. else if( attribute.type === "c" ) size = 3;
  5416. attribute.size = size;
  5417. attribute.array = new Float32Array( nvertices * size );
  5418. attribute.buffer = _gl.createBuffer();
  5419. attribute.buffer.belongsToAttribute = a;
  5420. originalAttribute.needsUpdate = true;
  5421. attribute.__original = originalAttribute;
  5422. }
  5423. geometryGroup.__webglCustomAttributesList.push( attribute );
  5424. }
  5425. }
  5426. geometryGroup.__inittedArrays = true;
  5427. };
  5428. function getBufferMaterial( object, geometryGroup ) {
  5429. if ( object.material && ! ( object.material instanceof THREE.MeshFaceMaterial ) ) {
  5430. return object.material;
  5431. } else if ( geometryGroup.materialIndex >= 0 ) {
  5432. return object.geometry.materials[ geometryGroup.materialIndex ];
  5433. }
  5434. };
  5435. function materialNeedsSmoothNormals ( material ) {
  5436. return material && material.shading !== undefined && material.shading === THREE.SmoothShading;
  5437. };
  5438. function bufferGuessNormalType ( material ) {
  5439. // only MeshBasicMaterial and MeshDepthMaterial don't need normals
  5440. if ( ( material instanceof THREE.MeshBasicMaterial && !material.envMap ) || material instanceof THREE.MeshDepthMaterial ) {
  5441. return false;
  5442. }
  5443. if ( materialNeedsSmoothNormals( material ) ) {
  5444. return THREE.SmoothShading;
  5445. } else {
  5446. return THREE.FlatShading;
  5447. }
  5448. };
  5449. function bufferGuessVertexColorType ( material ) {
  5450. if ( material.vertexColors ) {
  5451. return material.vertexColors;
  5452. }
  5453. return false;
  5454. };
  5455. function bufferGuessUVType ( material ) {
  5456. // material must use some texture to require uvs
  5457. if ( material.map || material.lightMap || material instanceof THREE.ShaderMaterial ) {
  5458. return true;
  5459. }
  5460. return false;
  5461. };
  5462. // Buffer setting
  5463. function setParticleBuffers ( geometry, hint, object ) {
  5464. var v, c, vertex, offset, index, color,
  5465. vertices = geometry.vertices,
  5466. vl = vertices.length,
  5467. colors = geometry.colors,
  5468. cl = colors.length,
  5469. vertexArray = geometry.__vertexArray,
  5470. colorArray = geometry.__colorArray,
  5471. sortArray = geometry.__sortArray,
  5472. dirtyVertices = geometry.__dirtyVertices,
  5473. dirtyElements = geometry.__dirtyElements,
  5474. dirtyColors = geometry.__dirtyColors,
  5475. customAttributes = geometry.__webglCustomAttributesList,
  5476. i, il,
  5477. a, ca, cal, value,
  5478. customAttribute;
  5479. if ( object.sortParticles ) {
  5480. _projScreenMatrixPS.copy( _projScreenMatrix );
  5481. _projScreenMatrixPS.multiplySelf( object.matrixWorld );
  5482. for ( v = 0; v < vl; v ++ ) {
  5483. vertex = vertices[ v ].position;
  5484. _vector3.copy( vertex );
  5485. _projScreenMatrixPS.multiplyVector3( _vector3 );
  5486. sortArray[ v ] = [ _vector3.z, v ];
  5487. }
  5488. sortArray.sort( function( a, b ) { return b[ 0 ] - a[ 0 ]; } );
  5489. for ( v = 0; v < vl; v ++ ) {
  5490. vertex = vertices[ sortArray[v][1] ].position;
  5491. offset = v * 3;
  5492. vertexArray[ offset ] = vertex.x;
  5493. vertexArray[ offset + 1 ] = vertex.y;
  5494. vertexArray[ offset + 2 ] = vertex.z;
  5495. }
  5496. for ( c = 0; c < cl; c ++ ) {
  5497. offset = c * 3;
  5498. color = colors[ sortArray[c][1] ];
  5499. colorArray[ offset ] = color.r;
  5500. colorArray[ offset + 1 ] = color.g;
  5501. colorArray[ offset + 2 ] = color.b;
  5502. }
  5503. if ( customAttributes ) {
  5504. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5505. customAttribute = customAttributes[ i ];
  5506. if ( ! ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) ) continue;
  5507. offset = 0;
  5508. cal = customAttribute.value.length;
  5509. if ( customAttribute.size === 1 ) {
  5510. for ( ca = 0; ca < cal; ca ++ ) {
  5511. index = sortArray[ ca ][ 1 ];
  5512. customAttribute.array[ ca ] = customAttribute.value[ index ];
  5513. }
  5514. } else if ( customAttribute.size === 2 ) {
  5515. for ( ca = 0; ca < cal; ca ++ ) {
  5516. index = sortArray[ ca ][ 1 ];
  5517. value = customAttribute.value[ index ];
  5518. customAttribute.array[ offset ] = value.x;
  5519. customAttribute.array[ offset + 1 ] = value.y;
  5520. offset += 2;
  5521. }
  5522. } else if ( customAttribute.size === 3 ) {
  5523. if ( customAttribute.type === "c" ) {
  5524. for ( ca = 0; ca < cal; ca ++ ) {
  5525. index = sortArray[ ca ][ 1 ];
  5526. value = customAttribute.value[ index ];
  5527. customAttribute.array[ offset ] = value.r;
  5528. customAttribute.array[ offset + 1 ] = value.g;
  5529. customAttribute.array[ offset + 2 ] = value.b;
  5530. offset += 3;
  5531. }
  5532. } else {
  5533. for ( ca = 0; ca < cal; ca ++ ) {
  5534. index = sortArray[ ca ][ 1 ];
  5535. value = customAttribute.value[ index ];
  5536. customAttribute.array[ offset ] = value.x;
  5537. customAttribute.array[ offset + 1 ] = value.y;
  5538. customAttribute.array[ offset + 2 ] = value.z;
  5539. offset += 3;
  5540. }
  5541. }
  5542. } else if ( customAttribute.size === 4 ) {
  5543. for ( ca = 0; ca < cal; ca ++ ) {
  5544. index = sortArray[ ca ][ 1 ];
  5545. value = customAttribute.value[ index ];
  5546. customAttribute.array[ offset ] = value.x;
  5547. customAttribute.array[ offset + 1 ] = value.y;
  5548. customAttribute.array[ offset + 2 ] = value.z;
  5549. customAttribute.array[ offset + 3 ] = value.w;
  5550. offset += 4;
  5551. }
  5552. }
  5553. }
  5554. }
  5555. } else {
  5556. if ( dirtyVertices ) {
  5557. for ( v = 0; v < vl; v ++ ) {
  5558. vertex = vertices[ v ].position;
  5559. offset = v * 3;
  5560. vertexArray[ offset ] = vertex.x;
  5561. vertexArray[ offset + 1 ] = vertex.y;
  5562. vertexArray[ offset + 2 ] = vertex.z;
  5563. }
  5564. }
  5565. if ( dirtyColors ) {
  5566. for ( c = 0; c < cl; c ++ ) {
  5567. color = colors[ c ];
  5568. offset = c * 3;
  5569. colorArray[ offset ] = color.r;
  5570. colorArray[ offset + 1 ] = color.g;
  5571. colorArray[ offset + 2 ] = color.b;
  5572. }
  5573. }
  5574. if ( customAttributes ) {
  5575. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5576. customAttribute = customAttributes[ i ];
  5577. if ( customAttribute.needsUpdate &&
  5578. ( customAttribute.boundTo === undefined ||
  5579. customAttribute.boundTo === "vertices") ) {
  5580. cal = customAttribute.value.length;
  5581. offset = 0;
  5582. if ( customAttribute.size === 1 ) {
  5583. for ( ca = 0; ca < cal; ca ++ ) {
  5584. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  5585. }
  5586. } else if ( customAttribute.size === 2 ) {
  5587. for ( ca = 0; ca < cal; ca ++ ) {
  5588. value = customAttribute.value[ ca ];
  5589. customAttribute.array[ offset ] = value.x;
  5590. customAttribute.array[ offset + 1 ] = value.y;
  5591. offset += 2;
  5592. }
  5593. } else if ( customAttribute.size === 3 ) {
  5594. if ( customAttribute.type === "c" ) {
  5595. for ( ca = 0; ca < cal; ca ++ ) {
  5596. value = customAttribute.value[ ca ];
  5597. customAttribute.array[ offset ] = value.r;
  5598. customAttribute.array[ offset + 1 ] = value.g;
  5599. customAttribute.array[ offset + 2 ] = value.b;
  5600. offset += 3;
  5601. }
  5602. } else {
  5603. for ( ca = 0; ca < cal; ca ++ ) {
  5604. value = customAttribute.value[ ca ];
  5605. customAttribute.array[ offset ] = value.x;
  5606. customAttribute.array[ offset + 1 ] = value.y;
  5607. customAttribute.array[ offset + 2 ] = value.z;
  5608. offset += 3;
  5609. }
  5610. }
  5611. } else if ( customAttribute.size === 4 ) {
  5612. for ( ca = 0; ca < cal; ca ++ ) {
  5613. value = customAttribute.value[ ca ];
  5614. customAttribute.array[ offset ] = value.x;
  5615. customAttribute.array[ offset + 1 ] = value.y;
  5616. customAttribute.array[ offset + 2 ] = value.z;
  5617. customAttribute.array[ offset + 3 ] = value.w;
  5618. offset += 4;
  5619. }
  5620. }
  5621. }
  5622. }
  5623. }
  5624. }
  5625. if ( dirtyVertices || object.sortParticles ) {
  5626. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  5627. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5628. }
  5629. if ( dirtyColors || object.sortParticles ) {
  5630. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  5631. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  5632. }
  5633. if ( customAttributes ) {
  5634. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5635. customAttribute = customAttributes[ i ];
  5636. if ( customAttribute.needsUpdate || object.sortParticles ) {
  5637. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  5638. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  5639. }
  5640. }
  5641. }
  5642. };
  5643. function setLineBuffers ( geometry, hint ) {
  5644. var v, c, vertex, offset, color,
  5645. vertices = geometry.vertices,
  5646. colors = geometry.colors,
  5647. vl = vertices.length,
  5648. cl = colors.length,
  5649. vertexArray = geometry.__vertexArray,
  5650. colorArray = geometry.__colorArray,
  5651. dirtyVertices = geometry.__dirtyVertices,
  5652. dirtyColors = geometry.__dirtyColors,
  5653. customAttributes = geometry.__webglCustomAttributesList,
  5654. i, il,
  5655. a, ca, cal, value,
  5656. customAttribute;
  5657. if ( dirtyVertices ) {
  5658. for ( v = 0; v < vl; v ++ ) {
  5659. vertex = vertices[ v ].position;
  5660. offset = v * 3;
  5661. vertexArray[ offset ] = vertex.x;
  5662. vertexArray[ offset + 1 ] = vertex.y;
  5663. vertexArray[ offset + 2 ] = vertex.z;
  5664. }
  5665. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  5666. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5667. }
  5668. if ( dirtyColors ) {
  5669. for ( c = 0; c < cl; c ++ ) {
  5670. color = colors[ c ];
  5671. offset = c * 3;
  5672. colorArray[ offset ] = color.r;
  5673. colorArray[ offset + 1 ] = color.g;
  5674. colorArray[ offset + 2 ] = color.b;
  5675. }
  5676. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  5677. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  5678. }
  5679. if ( customAttributes ) {
  5680. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5681. customAttribute = customAttributes[ i ];
  5682. if ( customAttribute.needsUpdate &&
  5683. ( customAttribute.boundTo === undefined ||
  5684. customAttribute.boundTo === "vertices" ) ) {
  5685. offset = 0;
  5686. cal = customAttribute.value.length;
  5687. if ( customAttribute.size === 1 ) {
  5688. for ( ca = 0; ca < cal; ca ++ ) {
  5689. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  5690. }
  5691. } else if ( customAttribute.size === 2 ) {
  5692. for ( ca = 0; ca < cal; ca ++ ) {
  5693. value = customAttribute.value[ ca ];
  5694. customAttribute.array[ offset ] = value.x;
  5695. customAttribute.array[ offset + 1 ] = value.y;
  5696. offset += 2;
  5697. }
  5698. } else if ( customAttribute.size === 3 ) {
  5699. if ( customAttribute.type === "c" ) {
  5700. for ( ca = 0; ca < cal; ca ++ ) {
  5701. value = customAttribute.value[ ca ];
  5702. customAttribute.array[ offset ] = value.r;
  5703. customAttribute.array[ offset + 1 ] = value.g;
  5704. customAttribute.array[ offset + 2 ] = value.b;
  5705. offset += 3;
  5706. }
  5707. } else {
  5708. for ( ca = 0; ca < cal; ca ++ ) {
  5709. value = customAttribute.value[ ca ];
  5710. customAttribute.array[ offset ] = value.x;
  5711. customAttribute.array[ offset + 1 ] = value.y;
  5712. customAttribute.array[ offset + 2 ] = value.z;
  5713. offset += 3;
  5714. }
  5715. }
  5716. } else if ( customAttribute.size === 4 ) {
  5717. for ( ca = 0; ca < cal; ca ++ ) {
  5718. value = customAttribute.value[ ca ];
  5719. customAttribute.array[ offset ] = value.x;
  5720. customAttribute.array[ offset + 1 ] = value.y;
  5721. customAttribute.array[ offset + 2 ] = value.z;
  5722. customAttribute.array[ offset + 3 ] = value.w;
  5723. offset += 4;
  5724. }
  5725. }
  5726. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  5727. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  5728. }
  5729. }
  5730. }
  5731. };
  5732. function setRibbonBuffers ( geometry, hint ) {
  5733. var v, c, vertex, offset, color,
  5734. vertices = geometry.vertices,
  5735. colors = geometry.colors,
  5736. vl = vertices.length,
  5737. cl = colors.length,
  5738. vertexArray = geometry.__vertexArray,
  5739. colorArray = geometry.__colorArray,
  5740. dirtyVertices = geometry.__dirtyVertices,
  5741. dirtyColors = geometry.__dirtyColors;
  5742. if ( dirtyVertices ) {
  5743. for ( v = 0; v < vl; v ++ ) {
  5744. vertex = vertices[ v ].position;
  5745. offset = v * 3;
  5746. vertexArray[ offset ] = vertex.x;
  5747. vertexArray[ offset + 1 ] = vertex.y;
  5748. vertexArray[ offset + 2 ] = vertex.z;
  5749. }
  5750. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  5751. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5752. }
  5753. if ( dirtyColors ) {
  5754. for ( c = 0; c < cl; c ++ ) {
  5755. color = colors[ c ];
  5756. offset = c * 3;
  5757. colorArray[ offset ] = color.r;
  5758. colorArray[ offset + 1 ] = color.g;
  5759. colorArray[ offset + 2 ] = color.b;
  5760. }
  5761. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  5762. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  5763. }
  5764. };
  5765. function setMeshBuffers( geometryGroup, object, hint, dispose, material ) {
  5766. if ( ! geometryGroup.__inittedArrays ) {
  5767. // console.log( object );
  5768. return;
  5769. }
  5770. var normalType = bufferGuessNormalType( material ),
  5771. vertexColorType = bufferGuessVertexColorType( material ),
  5772. uvType = bufferGuessUVType( material ),
  5773. needsSmoothNormals = ( normalType === THREE.SmoothShading );
  5774. var f, fl, fi, face,
  5775. vertexNormals, faceNormal, normal,
  5776. vertexColors, faceColor,
  5777. vertexTangents,
  5778. uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4, n1, n2, n3, n4,
  5779. c1, c2, c3, c4,
  5780. sw1, sw2, sw3, sw4,
  5781. si1, si2, si3, si4,
  5782. sa1, sa2, sa3, sa4,
  5783. sb1, sb2, sb3, sb4,
  5784. m, ml, i, il,
  5785. vn, uvi, uv2i,
  5786. vk, vkl, vka,
  5787. nka, chf, faceVertexNormals,
  5788. a,
  5789. vertexIndex = 0,
  5790. offset = 0,
  5791. offset_uv = 0,
  5792. offset_uv2 = 0,
  5793. offset_face = 0,
  5794. offset_normal = 0,
  5795. offset_tangent = 0,
  5796. offset_line = 0,
  5797. offset_color = 0,
  5798. offset_skin = 0,
  5799. offset_morphTarget = 0,
  5800. offset_custom = 0,
  5801. offset_customSrc = 0,
  5802. value,
  5803. vertexArray = geometryGroup.__vertexArray,
  5804. uvArray = geometryGroup.__uvArray,
  5805. uv2Array = geometryGroup.__uv2Array,
  5806. normalArray = geometryGroup.__normalArray,
  5807. tangentArray = geometryGroup.__tangentArray,
  5808. colorArray = geometryGroup.__colorArray,
  5809. skinVertexAArray = geometryGroup.__skinVertexAArray,
  5810. skinVertexBArray = geometryGroup.__skinVertexBArray,
  5811. skinIndexArray = geometryGroup.__skinIndexArray,
  5812. skinWeightArray = geometryGroup.__skinWeightArray,
  5813. morphTargetsArrays = geometryGroup.__morphTargetsArrays,
  5814. morphNormalsArrays = geometryGroup.__morphNormalsArrays,
  5815. customAttributes = geometryGroup.__webglCustomAttributesList,
  5816. customAttribute,
  5817. faceArray = geometryGroup.__faceArray,
  5818. lineArray = geometryGroup.__lineArray,
  5819. geometry = object.geometry, // this is shared for all chunks
  5820. dirtyVertices = geometry.__dirtyVertices,
  5821. dirtyElements = geometry.__dirtyElements,
  5822. dirtyUvs = geometry.__dirtyUvs,
  5823. dirtyNormals = geometry.__dirtyNormals,
  5824. dirtyTangents = geometry.__dirtyTangents,
  5825. dirtyColors = geometry.__dirtyColors,
  5826. dirtyMorphTargets = geometry.__dirtyMorphTargets,
  5827. vertices = geometry.vertices,
  5828. chunk_faces3 = geometryGroup.faces3,
  5829. chunk_faces4 = geometryGroup.faces4,
  5830. obj_faces = geometry.faces,
  5831. obj_uvs = geometry.faceVertexUvs[ 0 ],
  5832. obj_uvs2 = geometry.faceVertexUvs[ 1 ],
  5833. obj_colors = geometry.colors,
  5834. obj_skinVerticesA = geometry.skinVerticesA,
  5835. obj_skinVerticesB = geometry.skinVerticesB,
  5836. obj_skinIndices = geometry.skinIndices,
  5837. obj_skinWeights = geometry.skinWeights,
  5838. morphTargets = geometry.morphTargets,
  5839. morphNormals = geometry.morphNormals;
  5840. if ( dirtyVertices ) {
  5841. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  5842. face = obj_faces[ chunk_faces3[ f ] ];
  5843. v1 = vertices[ face.a ].position;
  5844. v2 = vertices[ face.b ].position;
  5845. v3 = vertices[ face.c ].position;
  5846. vertexArray[ offset ] = v1.x;
  5847. vertexArray[ offset + 1 ] = v1.y;
  5848. vertexArray[ offset + 2 ] = v1.z;
  5849. vertexArray[ offset + 3 ] = v2.x;
  5850. vertexArray[ offset + 4 ] = v2.y;
  5851. vertexArray[ offset + 5 ] = v2.z;
  5852. vertexArray[ offset + 6 ] = v3.x;
  5853. vertexArray[ offset + 7 ] = v3.y;
  5854. vertexArray[ offset + 8 ] = v3.z;
  5855. offset += 9;
  5856. }
  5857. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  5858. face = obj_faces[ chunk_faces4[ f ] ];
  5859. v1 = vertices[ face.a ].position;
  5860. v2 = vertices[ face.b ].position;
  5861. v3 = vertices[ face.c ].position;
  5862. v4 = vertices[ face.d ].position;
  5863. vertexArray[ offset ] = v1.x;
  5864. vertexArray[ offset + 1 ] = v1.y;
  5865. vertexArray[ offset + 2 ] = v1.z;
  5866. vertexArray[ offset + 3 ] = v2.x;
  5867. vertexArray[ offset + 4 ] = v2.y;
  5868. vertexArray[ offset + 5 ] = v2.z;
  5869. vertexArray[ offset + 6 ] = v3.x;
  5870. vertexArray[ offset + 7 ] = v3.y;
  5871. vertexArray[ offset + 8 ] = v3.z;
  5872. vertexArray[ offset + 9 ] = v4.x;
  5873. vertexArray[ offset + 10 ] = v4.y;
  5874. vertexArray[ offset + 11 ] = v4.z;
  5875. offset += 12;
  5876. }
  5877. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  5878. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5879. }
  5880. if ( dirtyMorphTargets ) {
  5881. for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) {
  5882. offset_morphTarget = 0;
  5883. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  5884. chf = chunk_faces3[ f ];
  5885. face = obj_faces[ chf ];
  5886. // morph positions
  5887. v1 = morphTargets[ vk ].vertices[ face.a ].position;
  5888. v2 = morphTargets[ vk ].vertices[ face.b ].position;
  5889. v3 = morphTargets[ vk ].vertices[ face.c ].position;
  5890. vka = morphTargetsArrays[ vk ];
  5891. vka[ offset_morphTarget ] = v1.x;
  5892. vka[ offset_morphTarget + 1 ] = v1.y;
  5893. vka[ offset_morphTarget + 2 ] = v1.z;
  5894. vka[ offset_morphTarget + 3 ] = v2.x;
  5895. vka[ offset_morphTarget + 4 ] = v2.y;
  5896. vka[ offset_morphTarget + 5 ] = v2.z;
  5897. vka[ offset_morphTarget + 6 ] = v3.x;
  5898. vka[ offset_morphTarget + 7 ] = v3.y;
  5899. vka[ offset_morphTarget + 8 ] = v3.z;
  5900. // morph normals
  5901. if ( material.morphNormals ) {
  5902. if ( needsSmoothNormals ) {
  5903. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  5904. n1 = faceVertexNormals.a;
  5905. n2 = faceVertexNormals.b;
  5906. n3 = faceVertexNormals.c;
  5907. } else {
  5908. n1 = morphNormals[ vk ].faceNormals[ chf ];
  5909. n2 = n1;
  5910. n3 = n1;
  5911. }
  5912. nka = morphNormalsArrays[ vk ];
  5913. nka[ offset_morphTarget ] = n1.x;
  5914. nka[ offset_morphTarget + 1 ] = n1.y;
  5915. nka[ offset_morphTarget + 2 ] = n1.z;
  5916. nka[ offset_morphTarget + 3 ] = n2.x;
  5917. nka[ offset_morphTarget + 4 ] = n2.y;
  5918. nka[ offset_morphTarget + 5 ] = n2.z;
  5919. nka[ offset_morphTarget + 6 ] = n3.x;
  5920. nka[ offset_morphTarget + 7 ] = n3.y;
  5921. nka[ offset_morphTarget + 8 ] = n3.z;
  5922. }
  5923. //
  5924. offset_morphTarget += 9;
  5925. }
  5926. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  5927. chf = chunk_faces4[ f ];
  5928. face = obj_faces[ chf ];
  5929. // morph positions
  5930. v1 = morphTargets[ vk ].vertices[ face.a ].position;
  5931. v2 = morphTargets[ vk ].vertices[ face.b ].position;
  5932. v3 = morphTargets[ vk ].vertices[ face.c ].position;
  5933. v4 = morphTargets[ vk ].vertices[ face.d ].position;
  5934. vka = morphTargetsArrays[ vk ];
  5935. vka[ offset_morphTarget ] = v1.x;
  5936. vka[ offset_morphTarget + 1 ] = v1.y;
  5937. vka[ offset_morphTarget + 2 ] = v1.z;
  5938. vka[ offset_morphTarget + 3 ] = v2.x;
  5939. vka[ offset_morphTarget + 4 ] = v2.y;
  5940. vka[ offset_morphTarget + 5 ] = v2.z;
  5941. vka[ offset_morphTarget + 6 ] = v3.x;
  5942. vka[ offset_morphTarget + 7 ] = v3.y;
  5943. vka[ offset_morphTarget + 8 ] = v3.z;
  5944. vka[ offset_morphTarget + 9 ] = v4.x;
  5945. vka[ offset_morphTarget + 10 ] = v4.y;
  5946. vka[ offset_morphTarget + 11 ] = v4.z;
  5947. // morph normals
  5948. if ( material.morphNormals ) {
  5949. if ( needsSmoothNormals ) {
  5950. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  5951. n1 = faceVertexNormals.a;
  5952. n2 = faceVertexNormals.b;
  5953. n3 = faceVertexNormals.c;
  5954. n4 = faceVertexNormals.d;
  5955. } else {
  5956. n1 = morphNormals[ vk ].faceNormals[ chf ];
  5957. n2 = n1;
  5958. n3 = n1;
  5959. n4 = n1;
  5960. }
  5961. nka = morphNormalsArrays[ vk ];
  5962. nka[ offset_morphTarget ] = n1.x;
  5963. nka[ offset_morphTarget + 1 ] = n1.y;
  5964. nka[ offset_morphTarget + 2 ] = n1.z;
  5965. nka[ offset_morphTarget + 3 ] = n2.x;
  5966. nka[ offset_morphTarget + 4 ] = n2.y;
  5967. nka[ offset_morphTarget + 5 ] = n2.z;
  5968. nka[ offset_morphTarget + 6 ] = n3.x;
  5969. nka[ offset_morphTarget + 7 ] = n3.y;
  5970. nka[ offset_morphTarget + 8 ] = n3.z;
  5971. nka[ offset_morphTarget + 9 ] = n4.x;
  5972. nka[ offset_morphTarget + 10 ] = n4.y;
  5973. nka[ offset_morphTarget + 11 ] = n4.z;
  5974. }
  5975. //
  5976. offset_morphTarget += 12;
  5977. }
  5978. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ vk ] );
  5979. _gl.bufferData( _gl.ARRAY_BUFFER, morphTargetsArrays[ vk ], hint );
  5980. if ( material.morphNormals ) {
  5981. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ vk ] );
  5982. _gl.bufferData( _gl.ARRAY_BUFFER, morphNormalsArrays[ vk ], hint );
  5983. }
  5984. }
  5985. }
  5986. if ( obj_skinWeights.length ) {
  5987. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  5988. face = obj_faces[ chunk_faces3[ f ] ];
  5989. // weights
  5990. sw1 = obj_skinWeights[ face.a ];
  5991. sw2 = obj_skinWeights[ face.b ];
  5992. sw3 = obj_skinWeights[ face.c ];
  5993. skinWeightArray[ offset_skin ] = sw1.x;
  5994. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  5995. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  5996. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  5997. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  5998. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  5999. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  6000. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  6001. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  6002. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  6003. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  6004. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  6005. // indices
  6006. si1 = obj_skinIndices[ face.a ];
  6007. si2 = obj_skinIndices[ face.b ];
  6008. si3 = obj_skinIndices[ face.c ];
  6009. skinIndexArray[ offset_skin ] = si1.x;
  6010. skinIndexArray[ offset_skin + 1 ] = si1.y;
  6011. skinIndexArray[ offset_skin + 2 ] = si1.z;
  6012. skinIndexArray[ offset_skin + 3 ] = si1.w;
  6013. skinIndexArray[ offset_skin + 4 ] = si2.x;
  6014. skinIndexArray[ offset_skin + 5 ] = si2.y;
  6015. skinIndexArray[ offset_skin + 6 ] = si2.z;
  6016. skinIndexArray[ offset_skin + 7 ] = si2.w;
  6017. skinIndexArray[ offset_skin + 8 ] = si3.x;
  6018. skinIndexArray[ offset_skin + 9 ] = si3.y;
  6019. skinIndexArray[ offset_skin + 10 ] = si3.z;
  6020. skinIndexArray[ offset_skin + 11 ] = si3.w;
  6021. // vertices A
  6022. sa1 = obj_skinVerticesA[ face.a ];
  6023. sa2 = obj_skinVerticesA[ face.b ];
  6024. sa3 = obj_skinVerticesA[ face.c ];
  6025. skinVertexAArray[ offset_skin ] = sa1.x;
  6026. skinVertexAArray[ offset_skin + 1 ] = sa1.y;
  6027. skinVertexAArray[ offset_skin + 2 ] = sa1.z;
  6028. skinVertexAArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6029. skinVertexAArray[ offset_skin + 4 ] = sa2.x;
  6030. skinVertexAArray[ offset_skin + 5 ] = sa2.y;
  6031. skinVertexAArray[ offset_skin + 6 ] = sa2.z;
  6032. skinVertexAArray[ offset_skin + 7 ] = 1;
  6033. skinVertexAArray[ offset_skin + 8 ] = sa3.x;
  6034. skinVertexAArray[ offset_skin + 9 ] = sa3.y;
  6035. skinVertexAArray[ offset_skin + 10 ] = sa3.z;
  6036. skinVertexAArray[ offset_skin + 11 ] = 1;
  6037. // vertices B
  6038. sb1 = obj_skinVerticesB[ face.a ];
  6039. sb2 = obj_skinVerticesB[ face.b ];
  6040. sb3 = obj_skinVerticesB[ face.c ];
  6041. skinVertexBArray[ offset_skin ] = sb1.x;
  6042. skinVertexBArray[ offset_skin + 1 ] = sb1.y;
  6043. skinVertexBArray[ offset_skin + 2 ] = sb1.z;
  6044. skinVertexBArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6045. skinVertexBArray[ offset_skin + 4 ] = sb2.x;
  6046. skinVertexBArray[ offset_skin + 5 ] = sb2.y;
  6047. skinVertexBArray[ offset_skin + 6 ] = sb2.z;
  6048. skinVertexBArray[ offset_skin + 7 ] = 1;
  6049. skinVertexBArray[ offset_skin + 8 ] = sb3.x;
  6050. skinVertexBArray[ offset_skin + 9 ] = sb3.y;
  6051. skinVertexBArray[ offset_skin + 10 ] = sb3.z;
  6052. skinVertexBArray[ offset_skin + 11 ] = 1;
  6053. offset_skin += 12;
  6054. }
  6055. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6056. face = obj_faces[ chunk_faces4[ f ] ];
  6057. // weights
  6058. sw1 = obj_skinWeights[ face.a ];
  6059. sw2 = obj_skinWeights[ face.b ];
  6060. sw3 = obj_skinWeights[ face.c ];
  6061. sw4 = obj_skinWeights[ face.d ];
  6062. skinWeightArray[ offset_skin ] = sw1.x;
  6063. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  6064. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  6065. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  6066. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  6067. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  6068. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  6069. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  6070. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  6071. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  6072. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  6073. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  6074. skinWeightArray[ offset_skin + 12 ] = sw4.x;
  6075. skinWeightArray[ offset_skin + 13 ] = sw4.y;
  6076. skinWeightArray[ offset_skin + 14 ] = sw4.z;
  6077. skinWeightArray[ offset_skin + 15 ] = sw4.w;
  6078. // indices
  6079. si1 = obj_skinIndices[ face.a ];
  6080. si2 = obj_skinIndices[ face.b ];
  6081. si3 = obj_skinIndices[ face.c ];
  6082. si4 = obj_skinIndices[ face.d ];
  6083. skinIndexArray[ offset_skin ] = si1.x;
  6084. skinIndexArray[ offset_skin + 1 ] = si1.y;
  6085. skinIndexArray[ offset_skin + 2 ] = si1.z;
  6086. skinIndexArray[ offset_skin + 3 ] = si1.w;
  6087. skinIndexArray[ offset_skin + 4 ] = si2.x;
  6088. skinIndexArray[ offset_skin + 5 ] = si2.y;
  6089. skinIndexArray[ offset_skin + 6 ] = si2.z;
  6090. skinIndexArray[ offset_skin + 7 ] = si2.w;
  6091. skinIndexArray[ offset_skin + 8 ] = si3.x;
  6092. skinIndexArray[ offset_skin + 9 ] = si3.y;
  6093. skinIndexArray[ offset_skin + 10 ] = si3.z;
  6094. skinIndexArray[ offset_skin + 11 ] = si3.w;
  6095. skinIndexArray[ offset_skin + 12 ] = si4.x;
  6096. skinIndexArray[ offset_skin + 13 ] = si4.y;
  6097. skinIndexArray[ offset_skin + 14 ] = si4.z;
  6098. skinIndexArray[ offset_skin + 15 ] = si4.w;
  6099. // vertices A
  6100. sa1 = obj_skinVerticesA[ face.a ];
  6101. sa2 = obj_skinVerticesA[ face.b ];
  6102. sa3 = obj_skinVerticesA[ face.c ];
  6103. sa4 = obj_skinVerticesA[ face.d ];
  6104. skinVertexAArray[ offset_skin ] = sa1.x;
  6105. skinVertexAArray[ offset_skin + 1 ] = sa1.y;
  6106. skinVertexAArray[ offset_skin + 2 ] = sa1.z;
  6107. skinVertexAArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6108. skinVertexAArray[ offset_skin + 4 ] = sa2.x;
  6109. skinVertexAArray[ offset_skin + 5 ] = sa2.y;
  6110. skinVertexAArray[ offset_skin + 6 ] = sa2.z;
  6111. skinVertexAArray[ offset_skin + 7 ] = 1;
  6112. skinVertexAArray[ offset_skin + 8 ] = sa3.x;
  6113. skinVertexAArray[ offset_skin + 9 ] = sa3.y;
  6114. skinVertexAArray[ offset_skin + 10 ] = sa3.z;
  6115. skinVertexAArray[ offset_skin + 11 ] = 1;
  6116. skinVertexAArray[ offset_skin + 12 ] = sa4.x;
  6117. skinVertexAArray[ offset_skin + 13 ] = sa4.y;
  6118. skinVertexAArray[ offset_skin + 14 ] = sa4.z;
  6119. skinVertexAArray[ offset_skin + 15 ] = 1;
  6120. // vertices B
  6121. sb1 = obj_skinVerticesB[ face.a ];
  6122. sb2 = obj_skinVerticesB[ face.b ];
  6123. sb3 = obj_skinVerticesB[ face.c ];
  6124. sb4 = obj_skinVerticesB[ face.d ];
  6125. skinVertexBArray[ offset_skin ] = sb1.x;
  6126. skinVertexBArray[ offset_skin + 1 ] = sb1.y;
  6127. skinVertexBArray[ offset_skin + 2 ] = sb1.z;
  6128. skinVertexBArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6129. skinVertexBArray[ offset_skin + 4 ] = sb2.x;
  6130. skinVertexBArray[ offset_skin + 5 ] = sb2.y;
  6131. skinVertexBArray[ offset_skin + 6 ] = sb2.z;
  6132. skinVertexBArray[ offset_skin + 7 ] = 1;
  6133. skinVertexBArray[ offset_skin + 8 ] = sb3.x;
  6134. skinVertexBArray[ offset_skin + 9 ] = sb3.y;
  6135. skinVertexBArray[ offset_skin + 10 ] = sb3.z;
  6136. skinVertexBArray[ offset_skin + 11 ] = 1;
  6137. skinVertexBArray[ offset_skin + 12 ] = sb4.x;
  6138. skinVertexBArray[ offset_skin + 13 ] = sb4.y;
  6139. skinVertexBArray[ offset_skin + 14 ] = sb4.z;
  6140. skinVertexBArray[ offset_skin + 15 ] = 1;
  6141. offset_skin += 16;
  6142. }
  6143. if ( offset_skin > 0 ) {
  6144. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexABuffer );
  6145. _gl.bufferData( _gl.ARRAY_BUFFER, skinVertexAArray, hint );
  6146. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexBBuffer );
  6147. _gl.bufferData( _gl.ARRAY_BUFFER, skinVertexBArray, hint );
  6148. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  6149. _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint );
  6150. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  6151. _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint );
  6152. }
  6153. }
  6154. if ( dirtyColors && vertexColorType ) {
  6155. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6156. face = obj_faces[ chunk_faces3[ f ] ];
  6157. vertexColors = face.vertexColors;
  6158. faceColor = face.color;
  6159. if ( vertexColors.length === 3 && vertexColorType === THREE.VertexColors ) {
  6160. c1 = vertexColors[ 0 ];
  6161. c2 = vertexColors[ 1 ];
  6162. c3 = vertexColors[ 2 ];
  6163. } else {
  6164. c1 = faceColor;
  6165. c2 = faceColor;
  6166. c3 = faceColor;
  6167. }
  6168. colorArray[ offset_color ] = c1.r;
  6169. colorArray[ offset_color + 1 ] = c1.g;
  6170. colorArray[ offset_color + 2 ] = c1.b;
  6171. colorArray[ offset_color + 3 ] = c2.r;
  6172. colorArray[ offset_color + 4 ] = c2.g;
  6173. colorArray[ offset_color + 5 ] = c2.b;
  6174. colorArray[ offset_color + 6 ] = c3.r;
  6175. colorArray[ offset_color + 7 ] = c3.g;
  6176. colorArray[ offset_color + 8 ] = c3.b;
  6177. offset_color += 9;
  6178. }
  6179. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6180. face = obj_faces[ chunk_faces4[ f ] ];
  6181. vertexColors = face.vertexColors;
  6182. faceColor = face.color;
  6183. if ( vertexColors.length === 4 && vertexColorType === THREE.VertexColors ) {
  6184. c1 = vertexColors[ 0 ];
  6185. c2 = vertexColors[ 1 ];
  6186. c3 = vertexColors[ 2 ];
  6187. c4 = vertexColors[ 3 ];
  6188. } else {
  6189. c1 = faceColor;
  6190. c2 = faceColor;
  6191. c3 = faceColor;
  6192. c4 = faceColor;
  6193. }
  6194. colorArray[ offset_color ] = c1.r;
  6195. colorArray[ offset_color + 1 ] = c1.g;
  6196. colorArray[ offset_color + 2 ] = c1.b;
  6197. colorArray[ offset_color + 3 ] = c2.r;
  6198. colorArray[ offset_color + 4 ] = c2.g;
  6199. colorArray[ offset_color + 5 ] = c2.b;
  6200. colorArray[ offset_color + 6 ] = c3.r;
  6201. colorArray[ offset_color + 7 ] = c3.g;
  6202. colorArray[ offset_color + 8 ] = c3.b;
  6203. colorArray[ offset_color + 9 ] = c4.r;
  6204. colorArray[ offset_color + 10 ] = c4.g;
  6205. colorArray[ offset_color + 11 ] = c4.b;
  6206. offset_color += 12;
  6207. }
  6208. if ( offset_color > 0 ) {
  6209. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  6210. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  6211. }
  6212. }
  6213. if ( dirtyTangents && geometry.hasTangents ) {
  6214. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6215. face = obj_faces[ chunk_faces3[ f ] ];
  6216. vertexTangents = face.vertexTangents;
  6217. t1 = vertexTangents[ 0 ];
  6218. t2 = vertexTangents[ 1 ];
  6219. t3 = vertexTangents[ 2 ];
  6220. tangentArray[ offset_tangent ] = t1.x;
  6221. tangentArray[ offset_tangent + 1 ] = t1.y;
  6222. tangentArray[ offset_tangent + 2 ] = t1.z;
  6223. tangentArray[ offset_tangent + 3 ] = t1.w;
  6224. tangentArray[ offset_tangent + 4 ] = t2.x;
  6225. tangentArray[ offset_tangent + 5 ] = t2.y;
  6226. tangentArray[ offset_tangent + 6 ] = t2.z;
  6227. tangentArray[ offset_tangent + 7 ] = t2.w;
  6228. tangentArray[ offset_tangent + 8 ] = t3.x;
  6229. tangentArray[ offset_tangent + 9 ] = t3.y;
  6230. tangentArray[ offset_tangent + 10 ] = t3.z;
  6231. tangentArray[ offset_tangent + 11 ] = t3.w;
  6232. offset_tangent += 12;
  6233. }
  6234. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6235. face = obj_faces[ chunk_faces4[ f ] ];
  6236. vertexTangents = face.vertexTangents;
  6237. t1 = vertexTangents[ 0 ];
  6238. t2 = vertexTangents[ 1 ];
  6239. t3 = vertexTangents[ 2 ];
  6240. t4 = vertexTangents[ 3 ];
  6241. tangentArray[ offset_tangent ] = t1.x;
  6242. tangentArray[ offset_tangent + 1 ] = t1.y;
  6243. tangentArray[ offset_tangent + 2 ] = t1.z;
  6244. tangentArray[ offset_tangent + 3 ] = t1.w;
  6245. tangentArray[ offset_tangent + 4 ] = t2.x;
  6246. tangentArray[ offset_tangent + 5 ] = t2.y;
  6247. tangentArray[ offset_tangent + 6 ] = t2.z;
  6248. tangentArray[ offset_tangent + 7 ] = t2.w;
  6249. tangentArray[ offset_tangent + 8 ] = t3.x;
  6250. tangentArray[ offset_tangent + 9 ] = t3.y;
  6251. tangentArray[ offset_tangent + 10 ] = t3.z;
  6252. tangentArray[ offset_tangent + 11 ] = t3.w;
  6253. tangentArray[ offset_tangent + 12 ] = t4.x;
  6254. tangentArray[ offset_tangent + 13 ] = t4.y;
  6255. tangentArray[ offset_tangent + 14 ] = t4.z;
  6256. tangentArray[ offset_tangent + 15 ] = t4.w;
  6257. offset_tangent += 16;
  6258. }
  6259. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  6260. _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint );
  6261. }
  6262. if ( dirtyNormals && normalType ) {
  6263. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6264. face = obj_faces[ chunk_faces3[ f ] ];
  6265. vertexNormals = face.vertexNormals;
  6266. faceNormal = face.normal;
  6267. if ( vertexNormals.length === 3 && needsSmoothNormals ) {
  6268. for ( i = 0; i < 3; i ++ ) {
  6269. vn = vertexNormals[ i ];
  6270. normalArray[ offset_normal ] = vn.x;
  6271. normalArray[ offset_normal + 1 ] = vn.y;
  6272. normalArray[ offset_normal + 2 ] = vn.z;
  6273. offset_normal += 3;
  6274. }
  6275. } else {
  6276. for ( i = 0; i < 3; i ++ ) {
  6277. normalArray[ offset_normal ] = faceNormal.x;
  6278. normalArray[ offset_normal + 1 ] = faceNormal.y;
  6279. normalArray[ offset_normal + 2 ] = faceNormal.z;
  6280. offset_normal += 3;
  6281. }
  6282. }
  6283. }
  6284. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6285. face = obj_faces[ chunk_faces4[ f ] ];
  6286. vertexNormals = face.vertexNormals;
  6287. faceNormal = face.normal;
  6288. if ( vertexNormals.length === 4 && needsSmoothNormals ) {
  6289. for ( i = 0; i < 4; i ++ ) {
  6290. vn = vertexNormals[ i ];
  6291. normalArray[ offset_normal ] = vn.x;
  6292. normalArray[ offset_normal + 1 ] = vn.y;
  6293. normalArray[ offset_normal + 2 ] = vn.z;
  6294. offset_normal += 3;
  6295. }
  6296. } else {
  6297. for ( i = 0; i < 4; i ++ ) {
  6298. normalArray[ offset_normal ] = faceNormal.x;
  6299. normalArray[ offset_normal + 1 ] = faceNormal.y;
  6300. normalArray[ offset_normal + 2 ] = faceNormal.z;
  6301. offset_normal += 3;
  6302. }
  6303. }
  6304. }
  6305. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  6306. _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
  6307. }
  6308. if ( dirtyUvs && obj_uvs && uvType ) {
  6309. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6310. fi = chunk_faces3[ f ];
  6311. face = obj_faces[ fi ];
  6312. uv = obj_uvs[ fi ];
  6313. if ( uv === undefined ) continue;
  6314. for ( i = 0; i < 3; i ++ ) {
  6315. uvi = uv[ i ];
  6316. uvArray[ offset_uv ] = uvi.u;
  6317. uvArray[ offset_uv + 1 ] = uvi.v;
  6318. offset_uv += 2;
  6319. }
  6320. }
  6321. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6322. fi = chunk_faces4[ f ];
  6323. face = obj_faces[ fi ];
  6324. uv = obj_uvs[ fi ];
  6325. if ( uv === undefined ) continue;
  6326. for ( i = 0; i < 4; i ++ ) {
  6327. uvi = uv[ i ];
  6328. uvArray[ offset_uv ] = uvi.u;
  6329. uvArray[ offset_uv + 1 ] = uvi.v;
  6330. offset_uv += 2;
  6331. }
  6332. }
  6333. if ( offset_uv > 0 ) {
  6334. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  6335. _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint );
  6336. }
  6337. }
  6338. if ( dirtyUvs && obj_uvs2 && uvType ) {
  6339. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6340. fi = chunk_faces3[ f ];
  6341. face = obj_faces[ fi ];
  6342. uv2 = obj_uvs2[ fi ];
  6343. if ( uv2 === undefined ) continue;
  6344. for ( i = 0; i < 3; i ++ ) {
  6345. uv2i = uv2[ i ];
  6346. uv2Array[ offset_uv2 ] = uv2i.u;
  6347. uv2Array[ offset_uv2 + 1 ] = uv2i.v;
  6348. offset_uv2 += 2;
  6349. }
  6350. }
  6351. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6352. fi = chunk_faces4[ f ];
  6353. face = obj_faces[ fi ];
  6354. uv2 = obj_uvs2[ fi ];
  6355. if ( uv2 === undefined ) continue;
  6356. for ( i = 0; i < 4; i ++ ) {
  6357. uv2i = uv2[ i ];
  6358. uv2Array[ offset_uv2 ] = uv2i.u;
  6359. uv2Array[ offset_uv2 + 1 ] = uv2i.v;
  6360. offset_uv2 += 2;
  6361. }
  6362. }
  6363. if ( offset_uv2 > 0 ) {
  6364. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  6365. _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint );
  6366. }
  6367. }
  6368. if ( dirtyElements ) {
  6369. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6370. face = obj_faces[ chunk_faces3[ f ] ];
  6371. faceArray[ offset_face ] = vertexIndex;
  6372. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  6373. faceArray[ offset_face + 2 ] = vertexIndex + 2;
  6374. offset_face += 3;
  6375. lineArray[ offset_line ] = vertexIndex;
  6376. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  6377. lineArray[ offset_line + 2 ] = vertexIndex;
  6378. lineArray[ offset_line + 3 ] = vertexIndex + 2;
  6379. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  6380. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  6381. offset_line += 6;
  6382. vertexIndex += 3;
  6383. }
  6384. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6385. face = obj_faces[ chunk_faces4[ f ] ];
  6386. faceArray[ offset_face ] = vertexIndex;
  6387. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  6388. faceArray[ offset_face + 2 ] = vertexIndex + 3;
  6389. faceArray[ offset_face + 3 ] = vertexIndex + 1;
  6390. faceArray[ offset_face + 4 ] = vertexIndex + 2;
  6391. faceArray[ offset_face + 5 ] = vertexIndex + 3;
  6392. offset_face += 6;
  6393. lineArray[ offset_line ] = vertexIndex;
  6394. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  6395. lineArray[ offset_line + 2 ] = vertexIndex;
  6396. lineArray[ offset_line + 3 ] = vertexIndex + 3;
  6397. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  6398. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  6399. lineArray[ offset_line + 6 ] = vertexIndex + 2;
  6400. lineArray[ offset_line + 7 ] = vertexIndex + 3;
  6401. offset_line += 8;
  6402. vertexIndex += 4;
  6403. }
  6404. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  6405. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint );
  6406. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  6407. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
  6408. }
  6409. if ( customAttributes ) {
  6410. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  6411. customAttribute = customAttributes[ i ];
  6412. if ( ! customAttribute.__original.needsUpdate ) continue;
  6413. offset_custom = 0;
  6414. offset_customSrc = 0;
  6415. if ( customAttribute.size === 1 ) {
  6416. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6417. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6418. face = obj_faces[ chunk_faces3[ f ] ];
  6419. customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
  6420. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  6421. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  6422. offset_custom += 3;
  6423. }
  6424. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6425. face = obj_faces[ chunk_faces4[ f ] ];
  6426. customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
  6427. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  6428. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  6429. customAttribute.array[ offset_custom + 3 ] = customAttribute.value[ face.d ];
  6430. offset_custom += 4;
  6431. }
  6432. } else if ( customAttribute.boundTo === "faces" ) {
  6433. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6434. value = customAttribute.value[ chunk_faces3[ f ] ];
  6435. customAttribute.array[ offset_custom ] = value;
  6436. customAttribute.array[ offset_custom + 1 ] = value;
  6437. customAttribute.array[ offset_custom + 2 ] = value;
  6438. offset_custom += 3;
  6439. }
  6440. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6441. value = customAttribute.value[ chunk_faces4[ f ] ];
  6442. customAttribute.array[ offset_custom ] = value;
  6443. customAttribute.array[ offset_custom + 1 ] = value;
  6444. customAttribute.array[ offset_custom + 2 ] = value;
  6445. customAttribute.array[ offset_custom + 3 ] = value;
  6446. offset_custom += 4;
  6447. }
  6448. }
  6449. } else if ( customAttribute.size === 2 ) {
  6450. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6451. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6452. face = obj_faces[ chunk_faces3[ f ] ];
  6453. v1 = customAttribute.value[ face.a ];
  6454. v2 = customAttribute.value[ face.b ];
  6455. v3 = customAttribute.value[ face.c ];
  6456. customAttribute.array[ offset_custom ] = v1.x;
  6457. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6458. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6459. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6460. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6461. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6462. offset_custom += 6;
  6463. }
  6464. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6465. face = obj_faces[ chunk_faces4[ f ] ];
  6466. v1 = customAttribute.value[ face.a ];
  6467. v2 = customAttribute.value[ face.b ];
  6468. v3 = customAttribute.value[ face.c ];
  6469. v4 = customAttribute.value[ face.d ];
  6470. customAttribute.array[ offset_custom ] = v1.x;
  6471. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6472. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6473. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6474. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6475. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6476. customAttribute.array[ offset_custom + 6 ] = v4.x;
  6477. customAttribute.array[ offset_custom + 7 ] = v4.y;
  6478. offset_custom += 8;
  6479. }
  6480. } else if ( customAttribute.boundTo === "faces" ) {
  6481. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6482. value = customAttribute.value[ chunk_faces3[ f ] ];
  6483. v1 = value;
  6484. v2 = value;
  6485. v3 = value;
  6486. customAttribute.array[ offset_custom ] = v1.x;
  6487. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6488. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6489. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6490. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6491. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6492. offset_custom += 6;
  6493. }
  6494. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6495. value = customAttribute.value[ chunk_faces4[ f ] ];
  6496. v1 = value;
  6497. v2 = value;
  6498. v3 = value;
  6499. v4 = value;
  6500. customAttribute.array[ offset_custom ] = v1.x;
  6501. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6502. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6503. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6504. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6505. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6506. customAttribute.array[ offset_custom + 6 ] = v4.x;
  6507. customAttribute.array[ offset_custom + 7 ] = v4.y;
  6508. offset_custom += 8;
  6509. }
  6510. }
  6511. } else if ( customAttribute.size === 3 ) {
  6512. var pp;
  6513. if ( customAttribute.type === "c" ) {
  6514. pp = [ "r", "g", "b" ];
  6515. } else {
  6516. pp = [ "x", "y", "z" ];
  6517. }
  6518. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6519. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6520. face = obj_faces[ chunk_faces3[ f ] ];
  6521. v1 = customAttribute.value[ face.a ];
  6522. v2 = customAttribute.value[ face.b ];
  6523. v3 = customAttribute.value[ face.c ];
  6524. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6525. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6526. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6527. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6528. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6529. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6530. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6531. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6532. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6533. offset_custom += 9;
  6534. }
  6535. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6536. face = obj_faces[ chunk_faces4[ f ] ];
  6537. v1 = customAttribute.value[ face.a ];
  6538. v2 = customAttribute.value[ face.b ];
  6539. v3 = customAttribute.value[ face.c ];
  6540. v4 = customAttribute.value[ face.d ];
  6541. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6542. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6543. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6544. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6545. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6546. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6547. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6548. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6549. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6550. customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
  6551. customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
  6552. customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
  6553. offset_custom += 12;
  6554. }
  6555. } else if ( customAttribute.boundTo === "faces" ) {
  6556. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6557. value = customAttribute.value[ chunk_faces3[ f ] ];
  6558. v1 = value;
  6559. v2 = value;
  6560. v3 = value;
  6561. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6562. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6563. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6564. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6565. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6566. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6567. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6568. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6569. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6570. offset_custom += 9;
  6571. }
  6572. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6573. value = customAttribute.value[ chunk_faces4[ f ] ];
  6574. v1 = value;
  6575. v2 = value;
  6576. v3 = value;
  6577. v4 = value;
  6578. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6579. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6580. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6581. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6582. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6583. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6584. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6585. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6586. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6587. customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
  6588. customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
  6589. customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
  6590. offset_custom += 12;
  6591. }
  6592. }
  6593. } else if ( customAttribute.size === 4 ) {
  6594. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6595. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6596. face = obj_faces[ chunk_faces3[ f ] ];
  6597. v1 = customAttribute.value[ face.a ];
  6598. v2 = customAttribute.value[ face.b ];
  6599. v3 = customAttribute.value[ face.c ];
  6600. customAttribute.array[ offset_custom ] = v1.x;
  6601. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6602. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6603. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6604. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6605. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6606. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6607. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6608. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6609. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6610. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6611. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6612. offset_custom += 12;
  6613. }
  6614. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6615. face = obj_faces[ chunk_faces4[ f ] ];
  6616. v1 = customAttribute.value[ face.a ];
  6617. v2 = customAttribute.value[ face.b ];
  6618. v3 = customAttribute.value[ face.c ];
  6619. v4 = customAttribute.value[ face.d ];
  6620. customAttribute.array[ offset_custom ] = v1.x;
  6621. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6622. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6623. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6624. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6625. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6626. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6627. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6628. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6629. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6630. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6631. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6632. customAttribute.array[ offset_custom + 12 ] = v4.x;
  6633. customAttribute.array[ offset_custom + 13 ] = v4.y;
  6634. customAttribute.array[ offset_custom + 14 ] = v4.z;
  6635. customAttribute.array[ offset_custom + 15 ] = v4.w;
  6636. offset_custom += 16;
  6637. }
  6638. } else if ( customAttribute.boundTo === "faces" ) {
  6639. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6640. value = customAttribute.value[ chunk_faces3[ f ] ];
  6641. v1 = value;
  6642. v2 = value;
  6643. v3 = value;
  6644. customAttribute.array[ offset_custom ] = v1.x;
  6645. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6646. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6647. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6648. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6649. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6650. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6651. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6652. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6653. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6654. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6655. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6656. offset_custom += 12;
  6657. }
  6658. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6659. value = customAttribute.value[ chunk_faces4[ f ] ];
  6660. v1 = value;
  6661. v2 = value;
  6662. v3 = value;
  6663. v4 = value;
  6664. customAttribute.array[ offset_custom ] = v1.x;
  6665. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6666. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6667. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6668. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6669. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6670. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6671. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6672. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6673. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6674. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6675. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6676. customAttribute.array[ offset_custom + 12 ] = v4.x;
  6677. customAttribute.array[ offset_custom + 13 ] = v4.y;
  6678. customAttribute.array[ offset_custom + 14 ] = v4.z;
  6679. customAttribute.array[ offset_custom + 15 ] = v4.w;
  6680. offset_custom += 16;
  6681. }
  6682. }
  6683. }
  6684. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  6685. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  6686. }
  6687. }
  6688. if ( dispose ) {
  6689. delete geometryGroup.__inittedArrays;
  6690. delete geometryGroup.__colorArray;
  6691. delete geometryGroup.__normalArray;
  6692. delete geometryGroup.__tangentArray;
  6693. delete geometryGroup.__uvArray;
  6694. delete geometryGroup.__uv2Array;
  6695. delete geometryGroup.__faceArray;
  6696. delete geometryGroup.__vertexArray;
  6697. delete geometryGroup.__lineArray;
  6698. delete geometryGroup.__skinVertexAArray;
  6699. delete geometryGroup.__skinVertexBArray;
  6700. delete geometryGroup.__skinIndexArray;
  6701. delete geometryGroup.__skinWeightArray;
  6702. }
  6703. };
  6704. // Buffer rendering
  6705. this.renderBufferImmediate = function ( object, program, shading ) {
  6706. if ( ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
  6707. if ( ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
  6708. if ( object.hasPos ) {
  6709. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
  6710. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  6711. _gl.enableVertexAttribArray( program.attributes.position );
  6712. _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6713. }
  6714. if ( object.hasNormal ) {
  6715. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer );
  6716. if ( shading === THREE.FlatShading ) {
  6717. var nx, ny, nz,
  6718. nax, nbx, ncx, nay, nby, ncy, naz, nbz, ncz,
  6719. normalArray,
  6720. i, il = object.count * 3;
  6721. for( i = 0; i < il; i += 9 ) {
  6722. normalArray = object.normalArray;
  6723. nax = normalArray[ i ];
  6724. nay = normalArray[ i + 1 ];
  6725. naz = normalArray[ i + 2 ];
  6726. nbx = normalArray[ i + 3 ];
  6727. nby = normalArray[ i + 4 ];
  6728. nbz = normalArray[ i + 5 ];
  6729. ncx = normalArray[ i + 6 ];
  6730. ncy = normalArray[ i + 7 ];
  6731. ncz = normalArray[ i + 8 ];
  6732. nx = ( nax + nbx + ncx ) / 3;
  6733. ny = ( nay + nby + ncy ) / 3;
  6734. nz = ( naz + nbz + ncz ) / 3;
  6735. normalArray[ i ] = nx;
  6736. normalArray[ i + 1 ] = ny;
  6737. normalArray[ i + 2 ] = nz;
  6738. normalArray[ i + 3 ] = nx;
  6739. normalArray[ i + 4 ] = ny;
  6740. normalArray[ i + 5 ] = nz;
  6741. normalArray[ i + 6 ] = nx;
  6742. normalArray[ i + 7 ] = ny;
  6743. normalArray[ i + 8 ] = nz;
  6744. }
  6745. }
  6746. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  6747. _gl.enableVertexAttribArray( program.attributes.normal );
  6748. _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  6749. }
  6750. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  6751. object.count = 0;
  6752. };
  6753. this.renderBufferDirect = function ( camera, lights, fog, material, geometryGroup, object ) {
  6754. if ( material.opacity === 0 ) return;
  6755. var program, attributes, linewidth, primitives, a, attribute;
  6756. program = setProgram( camera, lights, fog, material, object );
  6757. attributes = program.attributes;
  6758. var updateBuffers = false,
  6759. wireframeBit = material.wireframe ? 1 : 0,
  6760. geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  6761. if ( geometryGroupHash !== _currentGeometryGroupHash ) {
  6762. _currentGeometryGroupHash = geometryGroupHash;
  6763. updateBuffers = true;
  6764. }
  6765. // render mesh
  6766. if ( object instanceof THREE.Mesh ) {
  6767. var offsets = geometryGroup.offsets;
  6768. for ( var i = 0, il = offsets.length; i < il; ++ i ) {
  6769. if ( updateBuffers ) {
  6770. // vertices
  6771. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexPositionBuffer );
  6772. _gl.vertexAttribPointer( attributes.position, geometryGroup.vertexPositionBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 3 );
  6773. // normals
  6774. if ( attributes.normal >= 0 && geometryGroup.vertexNormalBuffer ) {
  6775. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexNormalBuffer );
  6776. _gl.vertexAttribPointer( attributes.normal, geometryGroup.vertexNormalBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 3 );
  6777. }
  6778. // uvs
  6779. if ( attributes.uv >= 0 && geometryGroup.vertexUvBuffer ) {
  6780. if ( geometryGroup.vertexUvBuffer ) {
  6781. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexUvBuffer );
  6782. _gl.vertexAttribPointer( attributes.uv, geometryGroup.vertexUvBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 2 );
  6783. _gl.enableVertexAttribArray( attributes.uv );
  6784. } else {
  6785. _gl.disableVertexAttribArray( attributes.uv );
  6786. }
  6787. }
  6788. // colors
  6789. if ( attributes.color >= 0 && geometryGroup.vertexColorBuffer ) {
  6790. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexColorBuffer );
  6791. _gl.vertexAttribPointer( attributes.color, geometryGroup.vertexColorBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 4 );
  6792. }
  6793. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.vertexIndexBuffer );
  6794. }
  6795. // render indexed triangles
  6796. _gl.drawElements( _gl.TRIANGLES, offsets[ i ].count, _gl.UNSIGNED_SHORT, offsets[ i ].start * 2 ); // 2 = Uint16
  6797. _this.info.render.calls ++;
  6798. _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
  6799. _this.info.render.faces += offsets[ i ].count / 3;
  6800. }
  6801. }
  6802. };
  6803. this.renderBuffer = function ( camera, lights, fog, material, geometryGroup, object ) {
  6804. if ( material.opacity === 0 ) return;
  6805. var program, attributes, linewidth, primitives, a, attribute, i, il;
  6806. program = setProgram( camera, lights, fog, material, object );
  6807. attributes = program.attributes;
  6808. var updateBuffers = false,
  6809. wireframeBit = material.wireframe ? 1 : 0,
  6810. geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  6811. if ( geometryGroupHash !== _currentGeometryGroupHash ) {
  6812. _currentGeometryGroupHash = geometryGroupHash;
  6813. updateBuffers = true;
  6814. }
  6815. // vertices
  6816. if ( !material.morphTargets && attributes.position >= 0 ) {
  6817. if ( updateBuffers ) {
  6818. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  6819. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6820. }
  6821. } else {
  6822. if ( object.morphTargetBase ) {
  6823. setupMorphTargets( material, geometryGroup, object );
  6824. }
  6825. }
  6826. if ( updateBuffers ) {
  6827. // custom attributes
  6828. // Use the per-geometryGroup custom attribute arrays which are setup in initMeshBuffers
  6829. if ( geometryGroup.__webglCustomAttributesList ) {
  6830. for ( i = 0, il = geometryGroup.__webglCustomAttributesList.length; i < il; i ++ ) {
  6831. attribute = geometryGroup.__webglCustomAttributesList[ i ];
  6832. if( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) {
  6833. _gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer );
  6834. _gl.vertexAttribPointer( attributes[ attribute.buffer.belongsToAttribute ], attribute.size, _gl.FLOAT, false, 0, 0 );
  6835. }
  6836. }
  6837. }
  6838. // colors
  6839. if ( attributes.color >= 0 ) {
  6840. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  6841. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  6842. }
  6843. // normals
  6844. if ( attributes.normal >= 0 ) {
  6845. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  6846. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  6847. }
  6848. // tangents
  6849. if ( attributes.tangent >= 0 ) {
  6850. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  6851. _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
  6852. }
  6853. // uvs
  6854. if ( attributes.uv >= 0 ) {
  6855. if ( geometryGroup.__webglUVBuffer ) {
  6856. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  6857. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  6858. _gl.enableVertexAttribArray( attributes.uv );
  6859. } else {
  6860. _gl.disableVertexAttribArray( attributes.uv );
  6861. }
  6862. }
  6863. if ( attributes.uv2 >= 0 ) {
  6864. if ( geometryGroup.__webglUV2Buffer ) {
  6865. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  6866. _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
  6867. _gl.enableVertexAttribArray( attributes.uv2 );
  6868. } else {
  6869. _gl.disableVertexAttribArray( attributes.uv2 );
  6870. }
  6871. }
  6872. if ( material.skinning &&
  6873. attributes.skinVertexA >= 0 && attributes.skinVertexB >= 0 &&
  6874. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  6875. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexABuffer );
  6876. _gl.vertexAttribPointer( attributes.skinVertexA, 4, _gl.FLOAT, false, 0, 0 );
  6877. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexBBuffer );
  6878. _gl.vertexAttribPointer( attributes.skinVertexB, 4, _gl.FLOAT, false, 0, 0 );
  6879. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  6880. _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
  6881. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  6882. _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
  6883. }
  6884. }
  6885. // render mesh
  6886. if ( object instanceof THREE.Mesh ) {
  6887. // wireframe
  6888. if ( material.wireframe ) {
  6889. setLineWidth( material.wireframeLinewidth );
  6890. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  6891. _gl.drawElements( _gl.LINES, geometryGroup.__webglLineCount, _gl.UNSIGNED_SHORT, 0 );
  6892. // triangles
  6893. } else {
  6894. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  6895. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, _gl.UNSIGNED_SHORT, 0 );
  6896. }
  6897. _this.info.render.calls ++;
  6898. _this.info.render.vertices += geometryGroup.__webglFaceCount;
  6899. _this.info.render.faces += geometryGroup.__webglFaceCount / 3;
  6900. // render lines
  6901. } else if ( object instanceof THREE.Line ) {
  6902. primitives = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  6903. setLineWidth( material.linewidth );
  6904. _gl.drawArrays( primitives, 0, geometryGroup.__webglLineCount );
  6905. _this.info.render.calls ++;
  6906. // render particles
  6907. } else if ( object instanceof THREE.ParticleSystem ) {
  6908. _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webglParticleCount );
  6909. _this.info.render.calls ++;
  6910. _this.info.render.points += geometryGroup.__webglParticleCount;
  6911. // render ribbon
  6912. } else if ( object instanceof THREE.Ribbon ) {
  6913. _gl.drawArrays( _gl.TRIANGLE_STRIP, 0, geometryGroup.__webglVertexCount );
  6914. _this.info.render.calls ++;
  6915. }
  6916. };
  6917. function setupMorphTargets ( material, geometryGroup, object ) {
  6918. // set base
  6919. var attributes = material.program.attributes;
  6920. if ( object.morphTargetBase !== - 1 ) {
  6921. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
  6922. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6923. } else if ( attributes.position >= 0 ) {
  6924. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  6925. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6926. }
  6927. if ( object.morphTargetForcedOrder.length ) {
  6928. // set forced order
  6929. var m = 0;
  6930. var order = object.morphTargetForcedOrder;
  6931. var influences = object.morphTargetInfluences;
  6932. while ( m < material.numSupportedMorphTargets && m < order.length ) {
  6933. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
  6934. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6935. if ( material.morphNormals ) {
  6936. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] );
  6937. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6938. }
  6939. object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ];
  6940. m ++;
  6941. }
  6942. } else {
  6943. // find most influencing
  6944. var used = [];
  6945. var candidateInfluence = - 1;
  6946. var candidate = 0;
  6947. var influences = object.morphTargetInfluences;
  6948. var i, il = influences.length;
  6949. var m = 0;
  6950. if ( object.morphTargetBase !== - 1 ) {
  6951. used[ object.morphTargetBase ] = true;
  6952. }
  6953. while ( m < material.numSupportedMorphTargets ) {
  6954. for ( i = 0; i < il; i ++ ) {
  6955. if ( !used[ i ] && influences[ i ] > candidateInfluence ) {
  6956. candidate = i;
  6957. candidateInfluence = influences[ candidate ];
  6958. }
  6959. }
  6960. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ candidate ] );
  6961. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6962. if ( material.morphNormals ) {
  6963. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ candidate ] );
  6964. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6965. }
  6966. object.__webglMorphTargetInfluences[ m ] = candidateInfluence;
  6967. used[ candidate ] = 1;
  6968. candidateInfluence = -1;
  6969. m ++;
  6970. }
  6971. }
  6972. // load updated influences uniform
  6973. if( material.program.uniforms.morphTargetInfluences !== null ) {
  6974. _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
  6975. }
  6976. };
  6977. function painterSort ( a, b ) {
  6978. return b.z - a.z;
  6979. };
  6980. // Rendering
  6981. this.render = function ( scene, camera, renderTarget, forceClear ) {
  6982. var i, il,
  6983. webglObject, object,
  6984. renderList,
  6985. lights = scene.__lights,
  6986. fog = scene.fog;
  6987. _currentMaterialId = -1;
  6988. // update scene graph
  6989. if ( camera.parent === undefined ) {
  6990. console.warn( 'DEPRECATED: Camera hasn\'t been added to a Scene. Adding it...' );
  6991. scene.add( camera );
  6992. }
  6993. if ( this.autoUpdateScene ) scene.updateMatrixWorld();
  6994. // update camera matrices and frustum
  6995. if ( ! camera._viewMatrixArray ) camera._viewMatrixArray = new Float32Array( 16 );
  6996. if ( ! camera._projectionMatrixArray ) camera._projectionMatrixArray = new Float32Array( 16 );
  6997. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  6998. camera.matrixWorldInverse.flattenToArray( camera._viewMatrixArray );
  6999. camera.projectionMatrix.flattenToArray( camera._projectionMatrixArray );
  7000. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  7001. _frustum.setFromMatrix( _projScreenMatrix );
  7002. // update WebGL objects
  7003. if ( this.autoUpdateObjects ) this.initWebGLObjects( scene );
  7004. // custom render plugins (pre pass)
  7005. renderPlugins( this.renderPluginsPre, scene, camera );
  7006. //
  7007. _this.info.render.calls = 0;
  7008. _this.info.render.vertices = 0;
  7009. _this.info.render.faces = 0;
  7010. _this.info.render.points = 0;
  7011. this.setRenderTarget( renderTarget );
  7012. if ( this.autoClear || forceClear ) {
  7013. this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
  7014. }
  7015. // set matrices for regular objects (frustum culled)
  7016. renderList = scene.__webglObjects;
  7017. for ( i = 0, il = renderList.length; i < il; i ++ ) {
  7018. webglObject = renderList[ i ];
  7019. object = webglObject.object;
  7020. webglObject.render = false;
  7021. if ( object.visible ) {
  7022. if ( ! ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem ) || ! ( object.frustumCulled ) || _frustum.contains( object ) ) {
  7023. //object.matrixWorld.flattenToArray( object._objectMatrixArray );
  7024. setupMatrices( object, camera );
  7025. unrollBufferMaterial( webglObject );
  7026. webglObject.render = true;
  7027. if ( this.sortObjects ) {
  7028. if ( object.renderDepth ) {
  7029. webglObject.z = object.renderDepth;
  7030. } else {
  7031. _vector3.copy( object.matrixWorld.getPosition() );
  7032. _projScreenMatrix.multiplyVector3( _vector3 );
  7033. webglObject.z = _vector3.z;
  7034. }
  7035. }
  7036. }
  7037. }
  7038. }
  7039. if ( this.sortObjects ) {
  7040. renderList.sort( painterSort );
  7041. }
  7042. // set matrices for immediate objects
  7043. renderList = scene.__webglObjectsImmediate;
  7044. for ( i = 0, il = renderList.length; i < il; i ++ ) {
  7045. webglObject = renderList[ i ];
  7046. object = webglObject.object;
  7047. if ( object.visible ) {
  7048. if( object.matrixAutoUpdate ) {
  7049. //object.matrixWorld.flattenToArray( object._objectMatrixArray );
  7050. }
  7051. setupMatrices( object, camera );
  7052. unrollImmediateBufferMaterial( webglObject );
  7053. }
  7054. }
  7055. if ( scene.overrideMaterial ) {
  7056. var material = scene.overrideMaterial;
  7057. this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  7058. this.setDepthTest( material.depthTest );
  7059. this.setDepthWrite( material.depthWrite );
  7060. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  7061. renderObjects( scene.__webglObjects, false, "", camera, lights, fog, true, material );
  7062. renderObjectsImmediate( scene.__webglObjectsImmediate, "", camera, lights, fog, false, material );
  7063. } else {
  7064. // opaque pass (front-to-back order)
  7065. this.setBlending( THREE.NormalBlending );
  7066. renderObjects( scene.__webglObjects, true, "opaque", camera, lights, fog, false );
  7067. renderObjectsImmediate( scene.__webglObjectsImmediate, "opaque", camera, lights, fog, false );
  7068. // transparent pass (back-to-front order)
  7069. renderObjects( scene.__webglObjects, false, "transparent", camera, lights, fog, true );
  7070. renderObjectsImmediate( scene.__webglObjectsImmediate, "transparent", camera, lights, fog, true );
  7071. }
  7072. // custom render plugins (post pass)
  7073. renderPlugins( this.renderPluginsPost, scene, camera );
  7074. // Generate mipmap if we're using any kind of mipmap filtering
  7075. if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
  7076. updateRenderTargetMipmap( renderTarget );
  7077. }
  7078. // Ensure depth buffer writing is enabled so it can be cleared on next render
  7079. this.setDepthTest( true );
  7080. this.setDepthWrite( true );
  7081. // _gl.finish();
  7082. };
  7083. function renderPlugins( plugins, scene, camera ) {
  7084. if ( ! plugins.length ) return;
  7085. for ( var i = 0, il = plugins.length; i < il; i ++ ) {
  7086. _currentProgram = null;
  7087. _currentCamera = null;
  7088. _oldBlending = -1;
  7089. _oldDepthTest = -1;
  7090. _oldDepthWrite = -1;
  7091. _currentGeometryGroupHash = -1;
  7092. _currentMaterialId = -1;
  7093. plugins[ i ].render( scene, camera, _currentWidth, _currentHeight );
  7094. _currentProgram = null;
  7095. _currentCamera = null;
  7096. _oldBlending = -1;
  7097. _oldDepthTest = -1;
  7098. _oldDepthWrite = -1;
  7099. _currentGeometryGroupHash = -1;
  7100. _currentMaterialId = -1;
  7101. }
  7102. };
  7103. function renderObjects ( renderList, reverse, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  7104. var webglObject, object, buffer, material, start, end, delta;
  7105. if ( reverse ) {
  7106. start = renderList.length - 1;
  7107. end = -1;
  7108. delta = -1;
  7109. } else {
  7110. start = 0;
  7111. end = renderList.length;
  7112. delta = 1;
  7113. }
  7114. for ( var i = start; i !== end; i += delta ) {
  7115. webglObject = renderList[ i ];
  7116. if ( webglObject.render ) {
  7117. object = webglObject.object;
  7118. buffer = webglObject.buffer;
  7119. if ( overrideMaterial ) {
  7120. material = overrideMaterial;
  7121. } else {
  7122. material = webglObject[ materialType ];
  7123. if ( ! material ) continue;
  7124. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  7125. _this.setDepthTest( material.depthTest );
  7126. _this.setDepthWrite( material.depthWrite );
  7127. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  7128. }
  7129. _this.setObjectFaces( object );
  7130. if ( buffer instanceof THREE.BufferGeometry ) {
  7131. _this.renderBufferDirect( camera, lights, fog, material, buffer, object );
  7132. } else {
  7133. _this.renderBuffer( camera, lights, fog, material, buffer, object );
  7134. }
  7135. }
  7136. }
  7137. };
  7138. function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  7139. var webglObject, object, material, program;
  7140. for ( var i = 0, il = renderList.length; i < il; i ++ ) {
  7141. webglObject = renderList[ i ];
  7142. object = webglObject.object;
  7143. if ( object.visible ) {
  7144. if ( overrideMaterial ) {
  7145. material = overrideMaterial;
  7146. } else {
  7147. material = webglObject[ materialType ];
  7148. if ( ! material ) continue;
  7149. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  7150. _this.setDepthTest( material.depthTest );
  7151. _this.setDepthWrite( material.depthWrite );
  7152. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  7153. }
  7154. _this.renderImmediateObject( camera, lights, fog, material, object );
  7155. }
  7156. }
  7157. };
  7158. this.renderImmediateObject = function ( camera, lights, fog, material, object ) {
  7159. var program = setProgram( camera, lights, fog, material, object );
  7160. _currentGeometryGroupHash = -1;
  7161. _this.setObjectFaces( object );
  7162. if ( object.immediateRenderCallback ) {
  7163. object.immediateRenderCallback( program, _gl, _frustum );
  7164. } else {
  7165. object.render( function( object ) { _this.renderBufferImmediate( object, program, material.shading ); } );
  7166. }
  7167. };
  7168. function unrollImmediateBufferMaterial ( globject ) {
  7169. var object = globject.object,
  7170. material = object.material;
  7171. if ( material.transparent ) {
  7172. globject.transparent = material;
  7173. globject.opaque = null;
  7174. } else {
  7175. globject.opaque = material;
  7176. globject.transparent = null;
  7177. }
  7178. };
  7179. function unrollBufferMaterial ( globject ) {
  7180. var object = globject.object,
  7181. buffer = globject.buffer,
  7182. material, materialIndex, meshMaterial;
  7183. meshMaterial = object.material;
  7184. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  7185. materialIndex = buffer.materialIndex;
  7186. if ( materialIndex >= 0 ) {
  7187. material = object.geometry.materials[ materialIndex ];
  7188. if ( material.transparent ) {
  7189. globject.transparent = material;
  7190. globject.opaque = null;
  7191. } else {
  7192. globject.opaque = material;
  7193. globject.transparent = null;
  7194. }
  7195. }
  7196. } else {
  7197. material = meshMaterial;
  7198. if ( material ) {
  7199. if ( material.transparent ) {
  7200. globject.transparent = material;
  7201. globject.opaque = null;
  7202. } else {
  7203. globject.opaque = material;
  7204. globject.transparent = null;
  7205. }
  7206. }
  7207. }
  7208. };
  7209. // Geometry splitting
  7210. function sortFacesByMaterial ( geometry ) {
  7211. var f, fl, face, materialIndex, vertices,
  7212. materialHash, groupHash,
  7213. hash_map = {};
  7214. var numMorphTargets = geometry.morphTargets.length;
  7215. var numMorphNormals = geometry.morphNormals.length;
  7216. geometry.geometryGroups = {};
  7217. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  7218. face = geometry.faces[ f ];
  7219. materialIndex = face.materialIndex;
  7220. materialHash = ( materialIndex !== undefined ) ? materialIndex : -1;
  7221. if ( hash_map[ materialHash ] === undefined ) {
  7222. hash_map[ materialHash ] = { 'hash': materialHash, 'counter': 0 };
  7223. }
  7224. groupHash = hash_map[ materialHash ].hash + '_' + hash_map[ materialHash ].counter;
  7225. if ( geometry.geometryGroups[ groupHash ] === undefined ) {
  7226. geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
  7227. }
  7228. vertices = face instanceof THREE.Face3 ? 3 : 4;
  7229. if ( geometry.geometryGroups[ groupHash ].vertices + vertices > 65535 ) {
  7230. hash_map[ materialHash ].counter += 1;
  7231. groupHash = hash_map[ materialHash ].hash + '_' + hash_map[ materialHash ].counter;
  7232. if ( geometry.geometryGroups[ groupHash ] === undefined ) {
  7233. geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
  7234. }
  7235. }
  7236. if ( face instanceof THREE.Face3 ) {
  7237. geometry.geometryGroups[ groupHash ].faces3.push( f );
  7238. } else {
  7239. geometry.geometryGroups[ groupHash ].faces4.push( f );
  7240. }
  7241. geometry.geometryGroups[ groupHash ].vertices += vertices;
  7242. }
  7243. geometry.geometryGroupsList = [];
  7244. for ( var g in geometry.geometryGroups ) {
  7245. geometry.geometryGroups[ g ].id = _geometryGroupCounter ++;
  7246. geometry.geometryGroupsList.push( geometry.geometryGroups[ g ] );
  7247. }
  7248. };
  7249. // Objects refresh
  7250. this.initWebGLObjects = function ( scene ) {
  7251. if ( !scene.__webglObjects ) {
  7252. scene.__webglObjects = [];
  7253. scene.__webglObjectsImmediate = [];
  7254. scene.__webglSprites = [];
  7255. scene.__webglFlares = [];
  7256. }
  7257. while ( scene.__objectsAdded.length ) {
  7258. addObject( scene.__objectsAdded[ 0 ], scene );
  7259. scene.__objectsAdded.splice( 0, 1 );
  7260. }
  7261. while ( scene.__objectsRemoved.length ) {
  7262. removeObject( scene.__objectsRemoved[ 0 ], scene );
  7263. scene.__objectsRemoved.splice( 0, 1 );
  7264. }
  7265. // update must be called after objects adding / removal
  7266. for ( var o = 0, ol = scene.__webglObjects.length; o < ol; o ++ ) {
  7267. updateObject( scene.__webglObjects[ o ].object );
  7268. }
  7269. };
  7270. // Objects adding
  7271. function addObject ( object, scene ) {
  7272. var g, geometry, geometryGroup;
  7273. if ( ! object.__webglInit ) {
  7274. object.__webglInit = true;
  7275. object._modelViewMatrix = new THREE.Matrix4();
  7276. object._normalMatrix = new THREE.Matrix3();
  7277. object._normalMatrixArray = new Float32Array( 9 );
  7278. object._modelViewMatrixArray = new Float32Array( 16 );
  7279. object._objectMatrixArray = new Float32Array( 16 );
  7280. //object.matrixWorld.flattenToArray( object._objectMatrixArray );
  7281. if ( object instanceof THREE.Mesh ) {
  7282. geometry = object.geometry;
  7283. if ( geometry instanceof THREE.Geometry ) {
  7284. if ( geometry.geometryGroups === undefined ) {
  7285. sortFacesByMaterial( geometry );
  7286. }
  7287. // create separate VBOs per geometry chunk
  7288. for ( g in geometry.geometryGroups ) {
  7289. geometryGroup = geometry.geometryGroups[ g ];
  7290. // initialise VBO on the first access
  7291. if ( ! geometryGroup.__webglVertexBuffer ) {
  7292. createMeshBuffers( geometryGroup );
  7293. initMeshBuffers( geometryGroup, object );
  7294. geometry.__dirtyVertices = true;
  7295. geometry.__dirtyMorphTargets = true;
  7296. geometry.__dirtyElements = true;
  7297. geometry.__dirtyUvs = true;
  7298. geometry.__dirtyNormals = true;
  7299. geometry.__dirtyTangents = true;
  7300. geometry.__dirtyColors = true;
  7301. }
  7302. }
  7303. }
  7304. } else if ( object instanceof THREE.Ribbon ) {
  7305. geometry = object.geometry;
  7306. if( ! geometry.__webglVertexBuffer ) {
  7307. createRibbonBuffers( geometry );
  7308. initRibbonBuffers( geometry );
  7309. geometry.__dirtyVertices = true;
  7310. geometry.__dirtyColors = true;
  7311. }
  7312. } else if ( object instanceof THREE.Line ) {
  7313. geometry = object.geometry;
  7314. if( ! geometry.__webglVertexBuffer ) {
  7315. createLineBuffers( geometry );
  7316. initLineBuffers( geometry, object );
  7317. geometry.__dirtyVertices = true;
  7318. geometry.__dirtyColors = true;
  7319. }
  7320. } else if ( object instanceof THREE.ParticleSystem ) {
  7321. geometry = object.geometry;
  7322. if ( ! geometry.__webglVertexBuffer ) {
  7323. createParticleBuffers( geometry );
  7324. initParticleBuffers( geometry, object );
  7325. geometry.__dirtyVertices = true;
  7326. geometry.__dirtyColors = true;
  7327. }
  7328. }
  7329. }
  7330. if ( ! object.__webglActive ) {
  7331. if ( object instanceof THREE.Mesh ) {
  7332. geometry = object.geometry;
  7333. if ( geometry instanceof THREE.BufferGeometry ) {
  7334. addBuffer( scene.__webglObjects, geometry, object );
  7335. } else {
  7336. for ( g in geometry.geometryGroups ) {
  7337. geometryGroup = geometry.geometryGroups[ g ];
  7338. addBuffer( scene.__webglObjects, geometryGroup, object );
  7339. }
  7340. }
  7341. } else if ( object instanceof THREE.Ribbon ||
  7342. object instanceof THREE.Line ||
  7343. object instanceof THREE.ParticleSystem ) {
  7344. geometry = object.geometry;
  7345. addBuffer( scene.__webglObjects, geometry, object );
  7346. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  7347. addBufferImmediate( scene.__webglObjectsImmediate, object );
  7348. } else if ( object instanceof THREE.Sprite ) {
  7349. scene.__webglSprites.push( object );
  7350. } else if ( object instanceof THREE.LensFlare ) {
  7351. scene.__webglFlares.push( object );
  7352. }
  7353. object.__webglActive = true;
  7354. }
  7355. };
  7356. function addBuffer ( objlist, buffer, object ) {
  7357. objlist.push(
  7358. {
  7359. buffer: buffer,
  7360. object: object,
  7361. opaque: null,
  7362. transparent: null
  7363. }
  7364. );
  7365. };
  7366. function addBufferImmediate ( objlist, object ) {
  7367. objlist.push(
  7368. {
  7369. object: object,
  7370. opaque: null,
  7371. transparent: null
  7372. }
  7373. );
  7374. };
  7375. // Objects updates
  7376. function updateObject ( object ) {
  7377. var geometry = object.geometry,
  7378. geometryGroup, customAttributesDirty, material;
  7379. if ( object instanceof THREE.Mesh ) {
  7380. if ( geometry instanceof THREE.BufferGeometry ) {
  7381. /*
  7382. if ( geometry.__dirtyVertices || geometry.__dirtyElements ||
  7383. geometry.__dirtyUvs || geometry.__dirtyNormals ||
  7384. geometry.__dirtyColors ) {
  7385. // TODO
  7386. // set buffers from typed arrays
  7387. }
  7388. */
  7389. geometry.__dirtyVertices = false;
  7390. geometry.__dirtyElements = false;
  7391. geometry.__dirtyUvs = false;
  7392. geometry.__dirtyNormals = false;
  7393. geometry.__dirtyColors = false;
  7394. } else {
  7395. // check all geometry groups
  7396. for( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) {
  7397. geometryGroup = geometry.geometryGroupsList[ i ];
  7398. material = getBufferMaterial( object, geometryGroup );
  7399. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  7400. if ( geometry.__dirtyVertices || geometry.__dirtyMorphTargets || geometry.__dirtyElements ||
  7401. geometry.__dirtyUvs || geometry.__dirtyNormals ||
  7402. geometry.__dirtyColors || geometry.__dirtyTangents || customAttributesDirty ) {
  7403. setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW, !geometry.dynamic, material );
  7404. }
  7405. }
  7406. geometry.__dirtyVertices = false;
  7407. geometry.__dirtyMorphTargets = false;
  7408. geometry.__dirtyElements = false;
  7409. geometry.__dirtyUvs = false;
  7410. geometry.__dirtyNormals = false;
  7411. geometry.__dirtyColors = false;
  7412. geometry.__dirtyTangents = false;
  7413. material.attributes && clearCustomAttributes( material );
  7414. }
  7415. } else if ( object instanceof THREE.Ribbon ) {
  7416. if ( geometry.__dirtyVertices || geometry.__dirtyColors ) {
  7417. setRibbonBuffers( geometry, _gl.DYNAMIC_DRAW );
  7418. }
  7419. geometry.__dirtyVertices = false;
  7420. geometry.__dirtyColors = false;
  7421. } else if ( object instanceof THREE.Line ) {
  7422. material = getBufferMaterial( object, geometryGroup );
  7423. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  7424. if ( geometry.__dirtyVertices || geometry.__dirtyColors || customAttributesDirty ) {
  7425. setLineBuffers( geometry, _gl.DYNAMIC_DRAW );
  7426. }
  7427. geometry.__dirtyVertices = false;
  7428. geometry.__dirtyColors = false;
  7429. material.attributes && clearCustomAttributes( material );
  7430. } else if ( object instanceof THREE.ParticleSystem ) {
  7431. material = getBufferMaterial( object, geometryGroup );
  7432. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  7433. if ( geometry.__dirtyVertices || geometry.__dirtyColors || object.sortParticles || customAttributesDirty ) {
  7434. setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object );
  7435. }
  7436. geometry.__dirtyVertices = false;
  7437. geometry.__dirtyColors = false;
  7438. material.attributes && clearCustomAttributes( material );
  7439. }
  7440. };
  7441. // Objects updates - custom attributes check
  7442. function areCustomAttributesDirty ( material ) {
  7443. for ( var a in material.attributes ) {
  7444. if ( material.attributes[ a ].needsUpdate ) return true;
  7445. }
  7446. return false;
  7447. };
  7448. function clearCustomAttributes ( material ) {
  7449. for ( var a in material.attributes ) {
  7450. material.attributes[ a ].needsUpdate = false;
  7451. }
  7452. };
  7453. // Objects removal
  7454. function removeObject ( object, scene ) {
  7455. if ( object instanceof THREE.Mesh ||
  7456. object instanceof THREE.ParticleSystem ||
  7457. object instanceof THREE.Ribbon ||
  7458. object instanceof THREE.Line ) {
  7459. removeInstances( scene.__webglObjects, object );
  7460. } else if ( object instanceof THREE.Sprite ) {
  7461. removeInstancesDirect( scene.__webglSprites, object );
  7462. } else if ( object instanceof THREE.LensFlare ) {
  7463. removeInstancesDirect( scene.__webglFlares, object );
  7464. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  7465. removeInstances( scene.__webglObjectsImmediate, object );
  7466. }
  7467. object.__webglActive = false;
  7468. };
  7469. function removeInstances ( objlist, object ) {
  7470. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  7471. if ( objlist[ o ].object === object ) {
  7472. objlist.splice( o, 1 );
  7473. }
  7474. }
  7475. };
  7476. function removeInstancesDirect ( objlist, object ) {
  7477. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  7478. if ( objlist[ o ] === object ) {
  7479. objlist.splice( o, 1 );
  7480. }
  7481. }
  7482. };
  7483. // Materials
  7484. this.initMaterial = function ( material, lights, fog, object ) {
  7485. var u, a, identifiers, i, parameters, maxLightCount, maxBones, maxShadows, shaderID;
  7486. if ( material instanceof THREE.MeshDepthMaterial ) {
  7487. shaderID = 'depth';
  7488. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  7489. shaderID = 'normal';
  7490. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  7491. shaderID = 'basic';
  7492. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  7493. shaderID = 'lambert';
  7494. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  7495. shaderID = 'phong';
  7496. } else if ( material instanceof THREE.LineBasicMaterial ) {
  7497. shaderID = 'basic';
  7498. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  7499. shaderID = 'particle_basic';
  7500. }
  7501. if ( shaderID ) {
  7502. setMaterialShaders( material, THREE.ShaderLib[ shaderID ] );
  7503. }
  7504. // heuristics to create shader parameters according to lights in the scene
  7505. // (not to blow over maxLights budget)
  7506. maxLightCount = allocateLights( lights );
  7507. maxShadows = allocateShadows( lights );
  7508. maxBones = allocateBones( object );
  7509. parameters = {
  7510. map: !!material.map, envMap: !!material.envMap, lightMap: !!material.lightMap,
  7511. vertexColors: material.vertexColors,
  7512. fog: fog, useFog: material.fog,
  7513. sizeAttenuation: material.sizeAttenuation,
  7514. skinning: material.skinning,
  7515. morphTargets: material.morphTargets,
  7516. morphNormals: material.morphNormals,
  7517. maxMorphTargets: this.maxMorphTargets,
  7518. maxMorphNormals: this.maxMorphNormals,
  7519. maxDirLights: maxLightCount.directional, maxPointLights: maxLightCount.point,
  7520. maxBones: maxBones,
  7521. shadowMapEnabled: this.shadowMapEnabled && object.receiveShadow,
  7522. shadowMapSoft: this.shadowMapSoft,
  7523. shadowMapDebug: this.shadowMapDebug,
  7524. shadowMapCascade: this.shadowMapCascade,
  7525. maxShadows: maxShadows,
  7526. alphaTest: material.alphaTest,
  7527. metal: material.metal,
  7528. perPixel: material.perPixel,
  7529. wrapAround: material.wrapAround,
  7530. doubleSided: object && object.doubleSided
  7531. };
  7532. material.program = buildProgram( shaderID, material.fragmentShader, material.vertexShader, material.uniforms, material.attributes, parameters );
  7533. var attributes = material.program.attributes;
  7534. if ( attributes.position >= 0 ) _gl.enableVertexAttribArray( attributes.position );
  7535. if ( attributes.color >= 0 ) _gl.enableVertexAttribArray( attributes.color );
  7536. if ( attributes.normal >= 0 ) _gl.enableVertexAttribArray( attributes.normal );
  7537. if ( attributes.tangent >= 0 ) _gl.enableVertexAttribArray( attributes.tangent );
  7538. if ( material.skinning &&
  7539. attributes.skinVertexA >=0 && attributes.skinVertexB >= 0 &&
  7540. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  7541. _gl.enableVertexAttribArray( attributes.skinVertexA );
  7542. _gl.enableVertexAttribArray( attributes.skinVertexB );
  7543. _gl.enableVertexAttribArray( attributes.skinIndex );
  7544. _gl.enableVertexAttribArray( attributes.skinWeight );
  7545. }
  7546. if ( material.attributes ) {
  7547. for ( a in material.attributes ) {
  7548. if( attributes[ a ] !== undefined && attributes[ a ] >= 0 ) _gl.enableVertexAttribArray( attributes[ a ] );
  7549. }
  7550. }
  7551. if ( material.morphTargets ) {
  7552. material.numSupportedMorphTargets = 0;
  7553. var id, base = "morphTarget";
  7554. for ( i = 0; i < this.maxMorphTargets; i ++ ) {
  7555. id = base + i;
  7556. if ( attributes[ id ] >= 0 ) {
  7557. _gl.enableVertexAttribArray( attributes[ id ] );
  7558. material.numSupportedMorphTargets ++;
  7559. }
  7560. }
  7561. }
  7562. if ( material.morphNormals ) {
  7563. material.numSupportedMorphNormals = 0;
  7564. var id, base = "morphNormal";
  7565. for ( i = 0; i < this.maxMorphNormals; i ++ ) {
  7566. id = base + i;
  7567. if ( attributes[ id ] >= 0 ) {
  7568. _gl.enableVertexAttribArray( attributes[ id ] );
  7569. material.numSupportedMorphNormals ++;
  7570. }
  7571. }
  7572. }
  7573. material.uniformsList = [];
  7574. for ( u in material.uniforms ) {
  7575. material.uniformsList.push( [ material.uniforms[ u ], u ] );
  7576. }
  7577. };
  7578. function setMaterialShaders( material, shaders ) {
  7579. material.uniforms = THREE.UniformsUtils.clone( shaders.uniforms );
  7580. material.vertexShader = shaders.vertexShader;
  7581. material.fragmentShader = shaders.fragmentShader;
  7582. };
  7583. function setProgram( camera, lights, fog, material, object ) {
  7584. if ( ! material.program || material.needsUpdate ) {
  7585. _this.initMaterial( material, lights, fog, object );
  7586. material.needsUpdate = false;
  7587. }
  7588. if ( material.morphTargets ) {
  7589. if ( ! object.__webglMorphTargetInfluences ) {
  7590. object.__webglMorphTargetInfluences = new Float32Array( _this.maxMorphTargets );
  7591. for ( var i = 0, il = _this.maxMorphTargets; i < il; i ++ ) {
  7592. object.__webglMorphTargetInfluences[ i ] = 0;
  7593. }
  7594. }
  7595. }
  7596. var refreshMaterial = false;
  7597. var program = material.program,
  7598. p_uniforms = program.uniforms,
  7599. m_uniforms = material.uniforms;
  7600. if ( program !== _currentProgram ) {
  7601. _gl.useProgram( program );
  7602. _currentProgram = program;
  7603. refreshMaterial = true;
  7604. }
  7605. if ( material.id !== _currentMaterialId ) {
  7606. _currentMaterialId = material.id;
  7607. refreshMaterial = true;
  7608. }
  7609. if ( refreshMaterial || camera !== _currentCamera ) {
  7610. _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera._projectionMatrixArray );
  7611. if ( camera !== _currentCamera ) _currentCamera = camera;
  7612. }
  7613. if ( refreshMaterial ) {
  7614. // refresh uniforms common to several materials
  7615. if ( fog && material.fog ) {
  7616. refreshUniformsFog( m_uniforms, fog );
  7617. }
  7618. if ( material instanceof THREE.MeshPhongMaterial ||
  7619. material instanceof THREE.MeshLambertMaterial ||
  7620. material.lights ) {
  7621. setupLights( program, lights );
  7622. refreshUniformsLights( m_uniforms, _lights );
  7623. }
  7624. if ( material instanceof THREE.MeshBasicMaterial ||
  7625. material instanceof THREE.MeshLambertMaterial ||
  7626. material instanceof THREE.MeshPhongMaterial ) {
  7627. refreshUniformsCommon( m_uniforms, material );
  7628. }
  7629. // refresh single material specific uniforms
  7630. if ( material instanceof THREE.LineBasicMaterial ) {
  7631. refreshUniformsLine( m_uniforms, material );
  7632. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  7633. refreshUniformsParticle( m_uniforms, material );
  7634. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  7635. refreshUniformsPhong( m_uniforms, material );
  7636. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  7637. refreshUniformsLambert( m_uniforms, material );
  7638. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  7639. m_uniforms.mNear.value = camera.near;
  7640. m_uniforms.mFar.value = camera.far;
  7641. m_uniforms.opacity.value = material.opacity;
  7642. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  7643. m_uniforms.opacity.value = material.opacity;
  7644. }
  7645. if ( object.receiveShadow && ! material._shadowPass ) {
  7646. refreshUniformsShadow( m_uniforms, lights );
  7647. }
  7648. // load common uniforms
  7649. loadUniformsGeneric( program, material.uniformsList );
  7650. // load material specific uniforms
  7651. // (shader material also gets them for the sake of genericity)
  7652. if ( material instanceof THREE.ShaderMaterial ||
  7653. material instanceof THREE.MeshPhongMaterial ||
  7654. material.envMap ) {
  7655. if ( p_uniforms.cameraPosition !== null ) {
  7656. var position = camera.matrixWorld.getPosition();
  7657. _gl.uniform3f( p_uniforms.cameraPosition, position.x, position.y, position.z );
  7658. }
  7659. }
  7660. if ( material instanceof THREE.MeshPhongMaterial ||
  7661. material instanceof THREE.MeshLambertMaterial ||
  7662. material instanceof THREE.ShaderMaterial ||
  7663. material.skinning ) {
  7664. if ( p_uniforms.viewMatrix !== null ) {
  7665. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera._viewMatrixArray );
  7666. }
  7667. }
  7668. if ( material.skinning ) {
  7669. _gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.boneMatrices );
  7670. }
  7671. }
  7672. loadUniformsMatrices( p_uniforms, object );
  7673. if ( material instanceof THREE.ShaderMaterial ||
  7674. material.envMap ||
  7675. material.skinning ||
  7676. object.receiveShadow ) {
  7677. if ( p_uniforms.objectMatrix !== null ) {
  7678. _gl.uniformMatrix4fv( p_uniforms.objectMatrix, false, object.matrixWorld.elements );
  7679. }
  7680. }
  7681. return program;
  7682. };
  7683. // Uniforms (refresh uniforms objects)
  7684. function refreshUniformsCommon ( uniforms, material ) {
  7685. uniforms.opacity.value = material.opacity;
  7686. if ( _this.gammaInput ) {
  7687. uniforms.diffuse.value.copyGammaToLinear( material.color );
  7688. } else {
  7689. uniforms.diffuse.value = material.color;
  7690. }
  7691. uniforms.map.texture = material.map;
  7692. if ( material.map ) {
  7693. uniforms.offsetRepeat.value.set( material.map.offset.x, material.map.offset.y, material.map.repeat.x, material.map.repeat.y );
  7694. }
  7695. uniforms.lightMap.texture = material.lightMap;
  7696. uniforms.envMap.texture = material.envMap;
  7697. uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : -1;
  7698. if ( _this.gammaInput ) {
  7699. //uniforms.reflectivity.value = material.reflectivity * material.reflectivity;
  7700. uniforms.reflectivity.value = material.reflectivity;
  7701. } else {
  7702. uniforms.reflectivity.value = material.reflectivity;
  7703. }
  7704. uniforms.refractionRatio.value = material.refractionRatio;
  7705. uniforms.combine.value = material.combine;
  7706. uniforms.useRefract.value = material.envMap && material.envMap.mapping instanceof THREE.CubeRefractionMapping;
  7707. };
  7708. function refreshUniformsLine ( uniforms, material ) {
  7709. uniforms.diffuse.value = material.color;
  7710. uniforms.opacity.value = material.opacity;
  7711. };
  7712. function refreshUniformsParticle ( uniforms, material ) {
  7713. uniforms.psColor.value = material.color;
  7714. uniforms.opacity.value = material.opacity;
  7715. uniforms.size.value = material.size;
  7716. uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.
  7717. uniforms.map.texture = material.map;
  7718. };
  7719. function refreshUniformsFog ( uniforms, fog ) {
  7720. uniforms.fogColor.value = fog.color;
  7721. if ( fog instanceof THREE.Fog ) {
  7722. uniforms.fogNear.value = fog.near;
  7723. uniforms.fogFar.value = fog.far;
  7724. } else if ( fog instanceof THREE.FogExp2 ) {
  7725. uniforms.fogDensity.value = fog.density;
  7726. }
  7727. };
  7728. function refreshUniformsPhong ( uniforms, material ) {
  7729. uniforms.shininess.value = material.shininess;
  7730. if ( _this.gammaInput ) {
  7731. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  7732. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  7733. uniforms.specular.value.copyGammaToLinear( material.specular );
  7734. } else {
  7735. uniforms.ambient.value = material.ambient;
  7736. uniforms.emissive.value = material.emissive;
  7737. uniforms.specular.value = material.specular;
  7738. }
  7739. if ( material.wrapAround ) {
  7740. uniforms.wrapRGB.value.copy( material.wrapRGB );
  7741. }
  7742. };
  7743. function refreshUniformsLambert ( uniforms, material ) {
  7744. if ( _this.gammaInput ) {
  7745. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  7746. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  7747. } else {
  7748. uniforms.ambient.value = material.ambient;
  7749. uniforms.emissive.value = material.emissive;
  7750. }
  7751. if ( material.wrapAround ) {
  7752. uniforms.wrapRGB.value.copy( material.wrapRGB );
  7753. }
  7754. };
  7755. function refreshUniformsLights ( uniforms, lights ) {
  7756. uniforms.ambientLightColor.value = lights.ambient;
  7757. uniforms.directionalLightColor.value = lights.directional.colors;
  7758. uniforms.directionalLightDirection.value = lights.directional.positions;
  7759. uniforms.pointLightColor.value = lights.point.colors;
  7760. uniforms.pointLightPosition.value = lights.point.positions;
  7761. uniforms.pointLightDistance.value = lights.point.distances;
  7762. };
  7763. function refreshUniformsShadow ( uniforms, lights ) {
  7764. if ( uniforms.shadowMatrix ) {
  7765. var j = 0;
  7766. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  7767. var light = lights[ i ];
  7768. if ( ! light.castShadow ) continue;
  7769. if ( light instanceof THREE.SpotLight || ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) ) {
  7770. uniforms.shadowMap.texture[ j ] = light.shadowMap;
  7771. uniforms.shadowMapSize.value[ j ] = light.shadowMapSize;
  7772. uniforms.shadowMatrix.value[ j ] = light.shadowMatrix;
  7773. uniforms.shadowDarkness.value[ j ] = light.shadowDarkness;
  7774. uniforms.shadowBias.value[ j ] = light.shadowBias;
  7775. j ++;
  7776. }
  7777. }
  7778. }
  7779. };
  7780. // Uniforms (load to GPU)
  7781. function loadUniformsMatrices ( uniforms, object ) {
  7782. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrix.elements );
  7783. if ( uniforms.normalMatrix ) {
  7784. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrixArray );
  7785. }
  7786. };
  7787. function loadUniformsGeneric ( program, uniforms ) {
  7788. var uniform, value, type, location, texture, i, il, j, jl, offset;
  7789. for( j = 0, jl = uniforms.length; j < jl; j ++ ) {
  7790. location = program.uniforms[ uniforms[ j ][ 1 ] ];
  7791. if ( !location ) continue;
  7792. uniform = uniforms[ j ][ 0 ];
  7793. type = uniform.type;
  7794. value = uniform.value;
  7795. // single integer
  7796. if( type === "i" ) {
  7797. _gl.uniform1i( location, value );
  7798. // single float
  7799. } else if( type === "f" ) {
  7800. _gl.uniform1f( location, value );
  7801. // single THREE.Vector2
  7802. } else if( type === "v2" ) {
  7803. _gl.uniform2f( location, value.x, value.y );
  7804. // single THREE.Vector3
  7805. } else if( type === "v3" ) {
  7806. _gl.uniform3f( location, value.x, value.y, value.z );
  7807. // single THREE.Vector4
  7808. } else if( type === "v4" ) {
  7809. _gl.uniform4f( location, value.x, value.y, value.z, value.w );
  7810. // single THREE.Color
  7811. } else if( type === "c" ) {
  7812. _gl.uniform3f( location, value.r, value.g, value.b );
  7813. // flat array of floats (JS or typed array)
  7814. } else if( type === "fv1" ) {
  7815. _gl.uniform1fv( location, value );
  7816. // flat array of floats with 3 x N size (JS or typed array)
  7817. } else if( type === "fv" ) {
  7818. _gl.uniform3fv( location, value );
  7819. // array of THREE.Vector2
  7820. } else if( type === "v2v" ) {
  7821. if ( ! uniform._array ) {
  7822. uniform._array = new Float32Array( 2 * value.length );
  7823. }
  7824. for ( i = 0, il = value.length; i < il; i ++ ) {
  7825. offset = i * 2;
  7826. uniform._array[ offset ] = value[ i ].x;
  7827. uniform._array[ offset + 1 ] = value[ i ].y;
  7828. }
  7829. _gl.uniform2fv( location, uniform._array );
  7830. // array of THREE.Vector3
  7831. } else if( type === "v3v" ) {
  7832. if ( ! uniform._array ) {
  7833. uniform._array = new Float32Array( 3 * value.length );
  7834. }
  7835. for ( i = 0, il = value.length; i < il; i ++ ) {
  7836. offset = i * 3;
  7837. uniform._array[ offset ] = value[ i ].x;
  7838. uniform._array[ offset + 1 ] = value[ i ].y;
  7839. uniform._array[ offset + 2 ] = value[ i ].z;
  7840. }
  7841. _gl.uniform3fv( location, uniform._array );
  7842. // array of THREE.Vector4
  7843. } else if( type == "v4v" ) {
  7844. if ( ! uniform._array ) {
  7845. uniform._array = new Float32Array( 4 * value.length );
  7846. }
  7847. for ( i = 0, il = value.length; i < il; i ++ ) {
  7848. offset = i * 4;
  7849. uniform._array[ offset ] = value[ i ].x;
  7850. uniform._array[ offset + 1 ] = value[ i ].y;
  7851. uniform._array[ offset + 2 ] = value[ i ].z;
  7852. uniform._array[ offset + 3 ] = value[ i ].w;
  7853. }
  7854. _gl.uniform4fv( location, uniform._array );
  7855. // single THREE.Matrix4
  7856. } else if( type === "m4" ) {
  7857. if ( ! uniform._array ) {
  7858. uniform._array = new Float32Array( 16 );
  7859. }
  7860. value.flattenToArray( uniform._array );
  7861. _gl.uniformMatrix4fv( location, false, uniform._array );
  7862. // array of THREE.Matrix4
  7863. } else if( type === "m4v" ) {
  7864. if ( ! uniform._array ) {
  7865. uniform._array = new Float32Array( 16 * value.length );
  7866. }
  7867. for ( i = 0, il = value.length; i < il; i ++ ) {
  7868. value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
  7869. }
  7870. _gl.uniformMatrix4fv( location, false, uniform._array );
  7871. // single THREE.Texture (2d or cube)
  7872. } else if( type === "t" ) {
  7873. _gl.uniform1i( location, value );
  7874. texture = uniform.texture;
  7875. if ( !texture ) continue;
  7876. if ( texture.image instanceof Array && texture.image.length === 6 ) {
  7877. setCubeTexture( texture, value );
  7878. } else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
  7879. setCubeTextureDynamic( texture, value );
  7880. } else {
  7881. _this.setTexture( texture, value );
  7882. }
  7883. // array of THREE.Texture (2d)
  7884. } else if( type === "tv" ) {
  7885. if ( ! uniform._array ) {
  7886. uniform._array = [];
  7887. for( i = 0, il = uniform.texture.length; i < il; i ++ ) {
  7888. uniform._array[ i ] = value + i;
  7889. }
  7890. }
  7891. _gl.uniform1iv( location, uniform._array );
  7892. for( i = 0, il = uniform.texture.length; i < il; i ++ ) {
  7893. texture = uniform.texture[ i ];
  7894. if ( !texture ) continue;
  7895. _this.setTexture( texture, uniform._array[ i ] );
  7896. }
  7897. }
  7898. }
  7899. };
  7900. function setupMatrices ( object, camera ) {
  7901. object._modelViewMatrix.multiply( camera.matrixWorldInverse, object.matrixWorld);
  7902. object._normalMatrix.getInverse( object._modelViewMatrix );
  7903. object._normalMatrix.transposeIntoArray( object._normalMatrixArray );
  7904. };
  7905. function setupLights ( program, lights ) {
  7906. var l, ll, light, n,
  7907. r = 0, g = 0, b = 0,
  7908. color, position, intensity, distance,
  7909. zlights = _lights,
  7910. dcolors = zlights.directional.colors,
  7911. dpositions = zlights.directional.positions,
  7912. pcolors = zlights.point.colors,
  7913. ppositions = zlights.point.positions,
  7914. pdistances = zlights.point.distances,
  7915. dlength = 0,
  7916. plength = 0,
  7917. doffset = 0,
  7918. poffset = 0;
  7919. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  7920. light = lights[ l ];
  7921. if ( light.onlyShadow ) continue;
  7922. color = light.color;
  7923. intensity = light.intensity;
  7924. distance = light.distance;
  7925. if ( light instanceof THREE.AmbientLight ) {
  7926. if ( _this.gammaInput ) {
  7927. r += color.r * color.r;
  7928. g += color.g * color.g;
  7929. b += color.b * color.b;
  7930. } else {
  7931. r += color.r;
  7932. g += color.g;
  7933. b += color.b;
  7934. }
  7935. } else if ( light instanceof THREE.DirectionalLight ) {
  7936. doffset = dlength * 3;
  7937. if ( _this.gammaInput ) {
  7938. dcolors[ doffset ] = color.r * color.r * intensity * intensity;
  7939. dcolors[ doffset + 1 ] = color.g * color.g * intensity * intensity;
  7940. dcolors[ doffset + 2 ] = color.b * color.b * intensity * intensity;
  7941. } else {
  7942. dcolors[ doffset ] = color.r * intensity;
  7943. dcolors[ doffset + 1 ] = color.g * intensity;
  7944. dcolors[ doffset + 2 ] = color.b * intensity;
  7945. }
  7946. _direction.copy( light.matrixWorld.getPosition() );
  7947. _direction.subSelf( light.target.matrixWorld.getPosition() );
  7948. _direction.normalize();
  7949. dpositions[ doffset ] = _direction.x;
  7950. dpositions[ doffset + 1 ] = _direction.y;
  7951. dpositions[ doffset + 2 ] = _direction.z;
  7952. dlength += 1;
  7953. } else if( light instanceof THREE.PointLight || light instanceof THREE.SpotLight ) {
  7954. poffset = plength * 3;
  7955. if ( _this.gammaInput ) {
  7956. pcolors[ poffset ] = color.r * color.r * intensity * intensity;
  7957. pcolors[ poffset + 1 ] = color.g * color.g * intensity * intensity;
  7958. pcolors[ poffset + 2 ] = color.b * color.b * intensity * intensity;
  7959. } else {
  7960. pcolors[ poffset ] = color.r * intensity;
  7961. pcolors[ poffset + 1 ] = color.g * intensity;
  7962. pcolors[ poffset + 2 ] = color.b * intensity;
  7963. }
  7964. position = light.matrixWorld.getPosition();
  7965. ppositions[ poffset ] = position.x;
  7966. ppositions[ poffset + 1 ] = position.y;
  7967. ppositions[ poffset + 2 ] = position.z;
  7968. pdistances[ plength ] = distance;
  7969. plength += 1;
  7970. }
  7971. }
  7972. // null eventual remains from removed lights
  7973. // (this is to avoid if in shader)
  7974. for ( l = dlength * 3, ll = dcolors.length; l < ll; l ++ ) dcolors[ l ] = 0.0;
  7975. for ( l = plength * 3, ll = pcolors.length; l < ll; l ++ ) pcolors[ l ] = 0.0;
  7976. zlights.point.length = plength;
  7977. zlights.directional.length = dlength;
  7978. zlights.ambient[ 0 ] = r;
  7979. zlights.ambient[ 1 ] = g;
  7980. zlights.ambient[ 2 ] = b;
  7981. };
  7982. // GL state setting
  7983. this.setFaceCulling = function ( cullFace, frontFace ) {
  7984. if ( cullFace ) {
  7985. if ( !frontFace || frontFace === "ccw" ) {
  7986. _gl.frontFace( _gl.CCW );
  7987. } else {
  7988. _gl.frontFace( _gl.CW );
  7989. }
  7990. if( cullFace === "back" ) {
  7991. _gl.cullFace( _gl.BACK );
  7992. } else if( cullFace === "front" ) {
  7993. _gl.cullFace( _gl.FRONT );
  7994. } else {
  7995. _gl.cullFace( _gl.FRONT_AND_BACK );
  7996. }
  7997. _gl.enable( _gl.CULL_FACE );
  7998. } else {
  7999. _gl.disable( _gl.CULL_FACE );
  8000. }
  8001. };
  8002. this.setObjectFaces = function ( object ) {
  8003. if ( _oldDoubleSided !== object.doubleSided ) {
  8004. if( object.doubleSided ) {
  8005. _gl.disable( _gl.CULL_FACE );
  8006. } else {
  8007. _gl.enable( _gl.CULL_FACE );
  8008. }
  8009. _oldDoubleSided = object.doubleSided;
  8010. }
  8011. if ( _oldFlipSided !== object.flipSided ) {
  8012. if( object.flipSided ) {
  8013. _gl.frontFace( _gl.CW );
  8014. } else {
  8015. _gl.frontFace( _gl.CCW );
  8016. }
  8017. _oldFlipSided = object.flipSided;
  8018. }
  8019. };
  8020. this.setDepthTest = function ( depthTest ) {
  8021. if ( _oldDepthTest !== depthTest ) {
  8022. if ( depthTest ) {
  8023. _gl.enable( _gl.DEPTH_TEST );
  8024. } else {
  8025. _gl.disable( _gl.DEPTH_TEST );
  8026. }
  8027. _oldDepthTest = depthTest;
  8028. }
  8029. };
  8030. this.setDepthWrite = function ( depthWrite ) {
  8031. if ( _oldDepthWrite !== depthWrite ) {
  8032. _gl.depthMask( depthWrite );
  8033. _oldDepthWrite = depthWrite;
  8034. }
  8035. };
  8036. function setLineWidth ( width ) {
  8037. if ( width !== _oldLineWidth ) {
  8038. _gl.lineWidth( width );
  8039. _oldLineWidth = width;
  8040. }
  8041. };
  8042. function setPolygonOffset ( polygonoffset, factor, units ) {
  8043. if ( _oldPolygonOffset !== polygonoffset ) {
  8044. if ( polygonoffset ) {
  8045. _gl.enable( _gl.POLYGON_OFFSET_FILL );
  8046. } else {
  8047. _gl.disable( _gl.POLYGON_OFFSET_FILL );
  8048. }
  8049. _oldPolygonOffset = polygonoffset;
  8050. }
  8051. if ( polygonoffset && ( _oldPolygonOffsetFactor !== factor || _oldPolygonOffsetUnits !== units ) ) {
  8052. _gl.polygonOffset( factor, units );
  8053. _oldPolygonOffsetFactor = factor;
  8054. _oldPolygonOffsetUnits = units;
  8055. }
  8056. };
  8057. this.setBlending = function ( blending, blendEquation, blendSrc, blendDst ) {
  8058. if ( blending !== _oldBlending ) {
  8059. switch ( blending ) {
  8060. case THREE.NoBlending:
  8061. _gl.disable( _gl.BLEND );
  8062. break;
  8063. case THREE.AdditiveBlending:
  8064. _gl.enable( _gl.BLEND );
  8065. _gl.blendEquation( _gl.FUNC_ADD );
  8066. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE );
  8067. break;
  8068. case THREE.SubtractiveBlending:
  8069. // TODO: Find blendFuncSeparate() combination
  8070. _gl.enable( _gl.BLEND );
  8071. _gl.blendEquation( _gl.FUNC_ADD );
  8072. _gl.blendFunc( _gl.ZERO, _gl.ONE_MINUS_SRC_COLOR );
  8073. break;
  8074. case THREE.MultiplyBlending:
  8075. // TODO: Find blendFuncSeparate() combination
  8076. _gl.enable( _gl.BLEND );
  8077. _gl.blendEquation( _gl.FUNC_ADD );
  8078. _gl.blendFunc( _gl.ZERO, _gl.SRC_COLOR );
  8079. break;
  8080. case THREE.CustomBlending:
  8081. _gl.enable( _gl.BLEND );
  8082. break;
  8083. default:
  8084. _gl.enable( _gl.BLEND );
  8085. _gl.blendEquationSeparate( _gl.FUNC_ADD, _gl.FUNC_ADD );
  8086. _gl.blendFuncSeparate( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA, _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  8087. break;
  8088. }
  8089. _oldBlending = blending;
  8090. }
  8091. if ( blending === THREE.CustomBlending ) {
  8092. if ( blendEquation !== _oldBlendEquation ) {
  8093. _gl.blendEquation( paramThreeToGL( blendEquation ) );
  8094. _oldBlendEquation = blendEquation;
  8095. }
  8096. if ( blendSrc !== _oldBlendSrc || blendDst !== _oldBlendDst ) {
  8097. _gl.blendFunc( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ) );
  8098. _oldBlendSrc = blendSrc;
  8099. _oldBlendDst = blendDst;
  8100. }
  8101. } else {
  8102. _oldBlendEquation = null;
  8103. _oldBlendSrc = null;
  8104. _oldBlendDst = null;
  8105. }
  8106. };
  8107. // Shaders
  8108. function buildProgram ( shaderID, fragmentShader, vertexShader, uniforms, attributes, parameters ) {
  8109. var p, pl, program, code;
  8110. var chunks = [];
  8111. // Generate code
  8112. if ( shaderID ) {
  8113. chunks.push( shaderID );
  8114. } else {
  8115. chunks.push( fragmentShader );
  8116. chunks.push( vertexShader );
  8117. }
  8118. for ( p in parameters ) {
  8119. chunks.push( p );
  8120. chunks.push( parameters[ p ] );
  8121. }
  8122. code = chunks.join();
  8123. // Check if code has been already compiled
  8124. for ( p = 0, pl = _programs.length; p < pl; p ++ ) {
  8125. if ( _programs[ p ].code === code ) {
  8126. // console.log( "Code already compiled." /*: \n\n" + code*/ );
  8127. return _programs[ p ].program;
  8128. }
  8129. }
  8130. //console.log( "building new program " );
  8131. //
  8132. program = _gl.createProgram();
  8133. var prefix_vertex = [
  8134. "precision " + _precision + " float;",
  8135. ( _maxVertexTextures > 0 ) ? "#define VERTEX_TEXTURES" : "",
  8136. _this.gammaInput ? "#define GAMMA_INPUT" : "",
  8137. _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
  8138. _this.physicallyBasedShading ? "#define PHYSICALLY_BASED_SHADING" : "",
  8139. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  8140. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  8141. "#define MAX_SHADOWS " + parameters.maxShadows,
  8142. "#define MAX_BONES " + parameters.maxBones,
  8143. parameters.map ? "#define USE_MAP" : "",
  8144. parameters.envMap ? "#define USE_ENVMAP" : "",
  8145. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  8146. parameters.vertexColors ? "#define USE_COLOR" : "",
  8147. parameters.skinning ? "#define USE_SKINNING" : "",
  8148. parameters.morphTargets ? "#define USE_MORPHTARGETS" : "",
  8149. parameters.morphNormals ? "#define USE_MORPHNORMALS" : "",
  8150. parameters.perPixel ? "#define PHONG_PER_PIXEL" : "",
  8151. parameters.wrapAround ? "#define WRAP_AROUND" : "",
  8152. parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
  8153. parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
  8154. parameters.shadowMapSoft ? "#define SHADOWMAP_SOFT" : "",
  8155. parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
  8156. parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
  8157. parameters.sizeAttenuation ? "#define USE_SIZEATTENUATION" : "",
  8158. "uniform mat4 objectMatrix;",
  8159. "uniform mat4 modelViewMatrix;",
  8160. "uniform mat4 projectionMatrix;",
  8161. "uniform mat4 viewMatrix;",
  8162. "uniform mat3 normalMatrix;",
  8163. "uniform vec3 cameraPosition;",
  8164. "attribute vec3 position;",
  8165. "attribute vec3 normal;",
  8166. "attribute vec2 uv;",
  8167. "attribute vec2 uv2;",
  8168. "#ifdef USE_COLOR",
  8169. "attribute vec3 color;",
  8170. "#endif",
  8171. "#ifdef USE_MORPHTARGETS",
  8172. "attribute vec3 morphTarget0;",
  8173. "attribute vec3 morphTarget1;",
  8174. "attribute vec3 morphTarget2;",
  8175. "attribute vec3 morphTarget3;",
  8176. "#ifdef USE_MORPHNORMALS",
  8177. "attribute vec3 morphNormal0;",
  8178. "attribute vec3 morphNormal1;",
  8179. "attribute vec3 morphNormal2;",
  8180. "attribute vec3 morphNormal3;",
  8181. "#else",
  8182. "attribute vec3 morphTarget4;",
  8183. "attribute vec3 morphTarget5;",
  8184. "attribute vec3 morphTarget6;",
  8185. "attribute vec3 morphTarget7;",
  8186. "#endif",
  8187. "#endif",
  8188. "#ifdef USE_SKINNING",
  8189. "attribute vec4 skinVertexA;",
  8190. "attribute vec4 skinVertexB;",
  8191. "attribute vec4 skinIndex;",
  8192. "attribute vec4 skinWeight;",
  8193. "#endif",
  8194. ""
  8195. ].join("\n");
  8196. var prefix_fragment = [
  8197. "precision " + _precision + " float;",
  8198. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  8199. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  8200. "#define MAX_SHADOWS " + parameters.maxShadows,
  8201. parameters.alphaTest ? "#define ALPHATEST " + parameters.alphaTest: "",
  8202. _this.gammaInput ? "#define GAMMA_INPUT" : "",
  8203. _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
  8204. _this.physicallyBasedShading ? "#define PHYSICALLY_BASED_SHADING" : "",
  8205. ( parameters.useFog && parameters.fog ) ? "#define USE_FOG" : "",
  8206. ( parameters.useFog && parameters.fog instanceof THREE.FogExp2 ) ? "#define FOG_EXP2" : "",
  8207. parameters.map ? "#define USE_MAP" : "",
  8208. parameters.envMap ? "#define USE_ENVMAP" : "",
  8209. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  8210. parameters.vertexColors ? "#define USE_COLOR" : "",
  8211. parameters.metal ? "#define METAL" : "",
  8212. parameters.perPixel ? "#define PHONG_PER_PIXEL" : "",
  8213. parameters.wrapAround ? "#define WRAP_AROUND" : "",
  8214. parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
  8215. parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
  8216. parameters.shadowMapSoft ? "#define SHADOWMAP_SOFT" : "",
  8217. parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
  8218. parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
  8219. "uniform mat4 viewMatrix;",
  8220. "uniform vec3 cameraPosition;",
  8221. ""
  8222. ].join("\n");
  8223. _gl.attachShader( program, getShader( "fragment", prefix_fragment + fragmentShader ) );
  8224. _gl.attachShader( program, getShader( "vertex", prefix_vertex + vertexShader ) );
  8225. _gl.linkProgram( program );
  8226. if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) {
  8227. console.error( "Could not initialise shader\n" + "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + ", gl error [" + _gl.getError() + "]" );
  8228. }
  8229. //console.log( prefix_fragment + fragmentShader );
  8230. //console.log( prefix_vertex + vertexShader );
  8231. program.uniforms = {};
  8232. program.attributes = {};
  8233. var identifiers, u, a, i;
  8234. // cache uniform locations
  8235. identifiers = [
  8236. 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition',
  8237. 'boneGlobalMatrices', 'morphTargetInfluences'
  8238. ];
  8239. for ( u in uniforms ) {
  8240. identifiers.push( u );
  8241. }
  8242. cacheUniformLocations( program, identifiers );
  8243. // cache attributes locations
  8244. identifiers = [
  8245. "position", "normal", "uv", "uv2", "tangent", "color",
  8246. "skinVertexA", "skinVertexB", "skinIndex", "skinWeight"
  8247. ];
  8248. for ( i = 0; i < parameters.maxMorphTargets; i ++ ) {
  8249. identifiers.push( "morphTarget" + i );
  8250. }
  8251. for ( i = 0; i < parameters.maxMorphNormals; i ++ ) {
  8252. identifiers.push( "morphNormal" + i );
  8253. }
  8254. for ( a in attributes ) {
  8255. identifiers.push( a );
  8256. }
  8257. cacheAttributeLocations( program, identifiers );
  8258. program.id = _programs.length;
  8259. _programs.push( { program: program, code: code } );
  8260. _this.info.memory.programs = _programs.length;
  8261. return program;
  8262. };
  8263. // Shader parameters cache
  8264. function cacheUniformLocations ( program, identifiers ) {
  8265. var i, l, id;
  8266. for( i = 0, l = identifiers.length; i < l; i ++ ) {
  8267. id = identifiers[ i ];
  8268. program.uniforms[ id ] = _gl.getUniformLocation( program, id );
  8269. }
  8270. };
  8271. function cacheAttributeLocations ( program, identifiers ) {
  8272. var i, l, id;
  8273. for( i = 0, l = identifiers.length; i < l; i ++ ) {
  8274. id = identifiers[ i ];
  8275. program.attributes[ id ] = _gl.getAttribLocation( program, id );
  8276. }
  8277. };
  8278. function getShader ( type, string ) {
  8279. var shader;
  8280. if ( type === "fragment" ) {
  8281. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  8282. } else if ( type === "vertex" ) {
  8283. shader = _gl.createShader( _gl.VERTEX_SHADER );
  8284. }
  8285. _gl.shaderSource( shader, string );
  8286. _gl.compileShader( shader );
  8287. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  8288. console.error( _gl.getShaderInfoLog( shader ) );
  8289. console.error( string );
  8290. return null;
  8291. }
  8292. return shader;
  8293. };
  8294. // Textures
  8295. function isPowerOfTwo ( value ) {
  8296. return ( value & ( value - 1 ) ) === 0;
  8297. };
  8298. function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) {
  8299. if ( isImagePowerOfTwo ) {
  8300. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
  8301. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
  8302. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
  8303. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
  8304. } else {
  8305. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  8306. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  8307. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
  8308. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
  8309. }
  8310. };
  8311. this.setTexture = function ( texture, slot ) {
  8312. if ( texture.needsUpdate ) {
  8313. if ( ! texture.__webglInit ) {
  8314. texture.__webglInit = true;
  8315. texture.__webglTexture = _gl.createTexture();
  8316. _this.info.memory.textures ++;
  8317. }
  8318. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8319. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  8320. _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  8321. var image = texture.image,
  8322. isImagePowerOfTwo = isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ),
  8323. glFormat = paramThreeToGL( texture.format ),
  8324. glType = paramThreeToGL( texture.type );
  8325. setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo );
  8326. if ( texture instanceof THREE.DataTexture ) {
  8327. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data );
  8328. } else {
  8329. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image );
  8330. }
  8331. if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  8332. texture.needsUpdate = false;
  8333. if ( texture.onUpdate ) texture.onUpdate();
  8334. } else {
  8335. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8336. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  8337. }
  8338. };
  8339. function clampToMaxSize ( image, maxSize ) {
  8340. if ( image.width <= maxSize && image.height <= maxSize ) {
  8341. return image;
  8342. }
  8343. // Warning: Scaling through the canvas will only work with images that use
  8344. // premultiplied alpha.
  8345. var maxDimension = Math.max( image.width, image.height );
  8346. var newWidth = Math.floor( image.width * maxSize / maxDimension );
  8347. var newHeight = Math.floor( image.height * maxSize / maxDimension );
  8348. var canvas = document.createElement( 'canvas' );
  8349. canvas.width = newWidth;
  8350. canvas.height = newHeight;
  8351. var ctx = canvas.getContext( "2d" );
  8352. ctx.drawImage( image, 0, 0, image.width, image.height, 0, 0, newWidth, newHeight );
  8353. return canvas;
  8354. }
  8355. function setCubeTexture ( texture, slot ) {
  8356. if ( texture.image.length === 6 ) {
  8357. if ( texture.needsUpdate ) {
  8358. if ( ! texture.image.__webglTextureCube ) {
  8359. texture.image.__webglTextureCube = _gl.createTexture();
  8360. }
  8361. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8362. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  8363. var cubeImage = [];
  8364. for ( var i = 0; i < 6; i ++ ) {
  8365. if ( _this.autoScaleCubemaps ) {
  8366. cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize );
  8367. } else {
  8368. cubeImage[ i ] = texture.image[ i ];
  8369. }
  8370. }
  8371. var image = cubeImage[ 0 ],
  8372. isImagePowerOfTwo = isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ),
  8373. glFormat = paramThreeToGL( texture.format ),
  8374. glType = paramThreeToGL( texture.type );
  8375. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );
  8376. for ( var i = 0; i < 6; i ++ ) {
  8377. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
  8378. }
  8379. if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  8380. texture.needsUpdate = false;
  8381. if ( texture.onUpdate ) texture.onUpdate();
  8382. } else {
  8383. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8384. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  8385. }
  8386. }
  8387. };
  8388. function setCubeTextureDynamic ( texture, slot ) {
  8389. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8390. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.__webglTexture );
  8391. };
  8392. // Render targets
  8393. function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {
  8394. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  8395. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, renderTarget.__webglTexture, 0 );
  8396. };
  8397. function setupRenderBuffer ( renderbuffer, renderTarget ) {
  8398. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
  8399. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  8400. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
  8401. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  8402. /* For some reason this is not working. Defaulting to RGBA4.
  8403. } else if( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  8404. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
  8405. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  8406. */
  8407. } else if( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  8408. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
  8409. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  8410. } else {
  8411. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );
  8412. }
  8413. };
  8414. this.setRenderTarget = function ( renderTarget ) {
  8415. var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
  8416. if ( renderTarget && ! renderTarget.__webglFramebuffer ) {
  8417. if( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
  8418. if( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;
  8419. renderTarget.__webglTexture = _gl.createTexture();
  8420. // Setup texture, create render and frame buffers
  8421. var isTargetPowerOfTwo = isPowerOfTwo( renderTarget.width ) && isPowerOfTwo( renderTarget.height ),
  8422. glFormat = paramThreeToGL( renderTarget.format ),
  8423. glType = paramThreeToGL( renderTarget.type );
  8424. if ( isCube ) {
  8425. renderTarget.__webglFramebuffer = [];
  8426. renderTarget.__webglRenderbuffer = [];
  8427. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  8428. setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo );
  8429. for ( var i = 0; i < 6; i ++ ) {
  8430. renderTarget.__webglFramebuffer[ i ] = _gl.createFramebuffer();
  8431. renderTarget.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
  8432. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  8433. setupFrameBuffer( renderTarget.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
  8434. setupRenderBuffer( renderTarget.__webglRenderbuffer[ i ], renderTarget );
  8435. }
  8436. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  8437. } else {
  8438. renderTarget.__webglFramebuffer = _gl.createFramebuffer();
  8439. renderTarget.__webglRenderbuffer = _gl.createRenderbuffer();
  8440. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  8441. setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo );
  8442. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  8443. setupFrameBuffer( renderTarget.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
  8444. setupRenderBuffer( renderTarget.__webglRenderbuffer, renderTarget );
  8445. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  8446. }
  8447. // Release everything
  8448. if ( isCube ) {
  8449. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  8450. } else {
  8451. _gl.bindTexture( _gl.TEXTURE_2D, null );
  8452. }
  8453. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  8454. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null);
  8455. }
  8456. var framebuffer, width, height, vx, vy;
  8457. if ( renderTarget ) {
  8458. if ( isCube ) {
  8459. framebuffer = renderTarget.__webglFramebuffer[ renderTarget.activeCubeFace ];
  8460. } else {
  8461. framebuffer = renderTarget.__webglFramebuffer;
  8462. }
  8463. width = renderTarget.width;
  8464. height = renderTarget.height;
  8465. vx = 0;
  8466. vy = 0;
  8467. } else {
  8468. framebuffer = null;
  8469. width = _viewportWidth;
  8470. height = _viewportHeight;
  8471. vx = _viewportX;
  8472. vy = _viewportY;
  8473. }
  8474. if ( framebuffer !== _currentFramebuffer ) {
  8475. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  8476. _gl.viewport( vx, vy, width, height );
  8477. _currentFramebuffer = framebuffer;
  8478. }
  8479. _currentWidth = width;
  8480. _currentHeight = height;
  8481. };
  8482. function updateRenderTargetMipmap ( renderTarget ) {
  8483. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  8484. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  8485. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  8486. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  8487. } else {
  8488. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  8489. _gl.generateMipmap( _gl.TEXTURE_2D );
  8490. _gl.bindTexture( _gl.TEXTURE_2D, null );
  8491. }
  8492. };
  8493. // Fallback filters for non-power-of-2 textures
  8494. function filterFallback ( f ) {
  8495. switch ( f ) {
  8496. case THREE.NearestFilter:
  8497. case THREE.NearestMipMapNearestFilter:
  8498. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST; break;
  8499. case THREE.LinearFilter:
  8500. case THREE.LinearMipMapNearestFilter:
  8501. case THREE.LinearMipMapLinearFilter:
  8502. default:
  8503. return _gl.LINEAR; break;
  8504. }
  8505. };
  8506. // Map three.js constants to WebGL constants
  8507. function paramThreeToGL ( p ) {
  8508. switch ( p ) {
  8509. case THREE.RepeatWrapping: return _gl.REPEAT; break;
  8510. case THREE.ClampToEdgeWrapping: return _gl.CLAMP_TO_EDGE; break;
  8511. case THREE.MirroredRepeatWrapping: return _gl.MIRRORED_REPEAT; break;
  8512. case THREE.NearestFilter: return _gl.NEAREST; break;
  8513. case THREE.NearestMipMapNearestFilter: return _gl.NEAREST_MIPMAP_NEAREST; break;
  8514. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST_MIPMAP_LINEAR; break;
  8515. case THREE.LinearFilter: return _gl.LINEAR; break;
  8516. case THREE.LinearMipMapNearestFilter: return _gl.LINEAR_MIPMAP_NEAREST; break;
  8517. case THREE.LinearMipMapLinearFilter: return _gl.LINEAR_MIPMAP_LINEAR; break;
  8518. case THREE.ByteType: return _gl.BYTE; break;
  8519. case THREE.UnsignedByteType: return _gl.UNSIGNED_BYTE; break;
  8520. case THREE.ShortType: return _gl.SHORT; break;
  8521. case THREE.UnsignedShortType: return _gl.UNSIGNED_SHORT; break;
  8522. case THREE.IntType: return _gl.INT; break;
  8523. case THREE.UnsignedIntType: return _gl.UNSIGNED_INT; break;
  8524. case THREE.FloatType: return _gl.FLOAT; break;
  8525. case THREE.AlphaFormat: return _gl.ALPHA; break;
  8526. case THREE.RGBFormat: return _gl.RGB; break;
  8527. case THREE.RGBAFormat: return _gl.RGBA; break;
  8528. case THREE.LuminanceFormat: return _gl.LUMINANCE; break;
  8529. case THREE.LuminanceAlphaFormat: return _gl.LUMINANCE_ALPHA; break;
  8530. case THREE.AddEquation: return _gl.FUNC_ADD; break;
  8531. case THREE.SubtractEquation: return _gl.FUNC_SUBTRACT; break;
  8532. case THREE.ReverseSubtractEquation: return _gl.FUNC_REVERSE_SUBTRACT; break;
  8533. case THREE.ZeroFactor: return _gl.ZERO; break;
  8534. case THREE.OneFactor: return _gl.ONE; break;
  8535. case THREE.SrcColorFactor: return _gl.SRC_COLOR; break;
  8536. case THREE.OneMinusSrcColorFactor: return _gl.ONE_MINUS_SRC_COLOR; break;
  8537. case THREE.SrcAlphaFactor: return _gl.SRC_ALPHA; break;
  8538. case THREE.OneMinusSrcAlphaFactor: return _gl.ONE_MINUS_SRC_ALPHA; break;
  8539. case THREE.DstAlphaFactor: return _gl.DST_ALPHA; break;
  8540. case THREE.OneMinusDstAlphaFactor: return _gl.ONE_MINUS_DST_ALPHA; break;
  8541. case THREE.DstColorFactor: return _gl.DST_COLOR; break;
  8542. case THREE.OneMinusDstColorFactor: return _gl.ONE_MINUS_DST_COLOR; break;
  8543. case THREE.SrcAlphaSaturateFactor: return _gl.SRC_ALPHA_SATURATE; break;
  8544. }
  8545. return 0;
  8546. };
  8547. // Allocations
  8548. function allocateBones ( object ) {
  8549. // default for when object is not specified
  8550. // ( for example when prebuilding shader
  8551. // to be used with multiple objects )
  8552. //
  8553. // - leave some extra space for other uniforms
  8554. // - limit here is ANGLE's 254 max uniform vectors
  8555. // (up to 54 should be safe)
  8556. var maxBones = 50;
  8557. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  8558. maxBones = object.bones.length;
  8559. }
  8560. return maxBones;
  8561. };
  8562. function allocateLights ( lights ) {
  8563. var l, ll, light, dirLights, pointLights, maxDirLights, maxPointLights;
  8564. dirLights = pointLights = maxDirLights = maxPointLights = 0;
  8565. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  8566. light = lights[ l ];
  8567. if ( light.onlyShadow ) continue;
  8568. if ( light instanceof THREE.DirectionalLight ) dirLights ++;
  8569. if ( light instanceof THREE.PointLight ) pointLights ++;
  8570. if ( light instanceof THREE.SpotLight ) pointLights ++;
  8571. }
  8572. if ( ( pointLights + dirLights ) <= _maxLights ) {
  8573. maxDirLights = dirLights;
  8574. maxPointLights = pointLights;
  8575. } else {
  8576. maxDirLights = Math.ceil( _maxLights * dirLights / ( pointLights + dirLights ) );
  8577. maxPointLights = _maxLights - maxDirLights;
  8578. }
  8579. return { 'directional' : maxDirLights, 'point' : maxPointLights };
  8580. };
  8581. function allocateShadows ( lights ) {
  8582. var l, ll, light, maxShadows = 0;
  8583. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  8584. light = lights[ l ];
  8585. if ( ! light.castShadow ) continue;
  8586. if ( light instanceof THREE.SpotLight ) maxShadows ++;
  8587. if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++;
  8588. }
  8589. return maxShadows;
  8590. };
  8591. // Initialization
  8592. function initGL () {
  8593. var gl;
  8594. try {
  8595. if ( ! ( gl = _canvas.getContext( 'experimental-webgl', { alpha: _alpha, premultipliedAlpha: _premultipliedAlpha, antialias: _antialias, stencil: _stencil, preserveDrawingBuffer: _preserveDrawingBuffer } ) ) ) {
  8596. throw 'Error creating WebGL context.';
  8597. }
  8598. console.log(
  8599. navigator.userAgent + " | " +
  8600. gl.getParameter( gl.VERSION ) + " | " +
  8601. gl.getParameter( gl.VENDOR ) + " | " +
  8602. gl.getParameter( gl.RENDERER ) + " | " +
  8603. gl.getParameter( gl.SHADING_LANGUAGE_VERSION )
  8604. );
  8605. } catch ( error ) {
  8606. console.error( error );
  8607. }
  8608. return gl;
  8609. };
  8610. function setDefaultGLState () {
  8611. _gl.clearColor( 0, 0, 0, 1 );
  8612. _gl.clearDepth( 1 );
  8613. _gl.clearStencil( 0 );
  8614. _gl.enable( _gl.DEPTH_TEST );
  8615. _gl.depthFunc( _gl.LEQUAL );
  8616. _gl.frontFace( _gl.CCW );
  8617. _gl.cullFace( _gl.BACK );
  8618. _gl.enable( _gl.CULL_FACE );
  8619. _gl.enable( _gl.BLEND );
  8620. _gl.blendEquation( _gl.FUNC_ADD );
  8621. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  8622. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  8623. };
  8624. // default plugins (order is important)
  8625. this.shadowMapPlugin = new THREE.ShadowMapPlugin();
  8626. this.addPrePlugin( this.shadowMapPlugin );
  8627. this.addPostPlugin( new THREE.SpritePlugin() );
  8628. this.addPostPlugin( new THREE.LensFlarePlugin() );
  8629. };
  8630. /**
  8631. * @author szimek / https://github.com/szimek/
  8632. */
  8633. THREE.WebGLRenderTarget = function ( width, height, options ) {
  8634. this.width = width;
  8635. this.height = height;
  8636. options = options || {};
  8637. this.wrapS = options.wrapS !== undefined ? options.wrapS : THREE.ClampToEdgeWrapping;
  8638. this.wrapT = options.wrapT !== undefined ? options.wrapT : THREE.ClampToEdgeWrapping;
  8639. this.magFilter = options.magFilter !== undefined ? options.magFilter : THREE.LinearFilter;
  8640. this.minFilter = options.minFilter !== undefined ? options.minFilter : THREE.LinearMipMapLinearFilter;
  8641. this.offset = new THREE.Vector2( 0, 0 );
  8642. this.repeat = new THREE.Vector2( 1, 1 );
  8643. this.format = options.format !== undefined ? options.format : THREE.RGBAFormat;
  8644. this.type = options.type !== undefined ? options.type : THREE.UnsignedByteType;
  8645. this.depthBuffer = options.depthBuffer !== undefined ? options.depthBuffer : true;
  8646. this.stencilBuffer = options.stencilBuffer !== undefined ? options.stencilBuffer : true;
  8647. this.generateMipmaps = true;
  8648. };
  8649. THREE.WebGLRenderTarget.prototype.clone = function() {
  8650. var tmp = new THREE.WebGLRenderTarget( this.width, this.height );
  8651. tmp.wrapS = this.wrapS;
  8652. tmp.wrapT = this.wrapT;
  8653. tmp.magFilter = this.magFilter;
  8654. tmp.minFilter = this.minFilter;
  8655. tmp.offset.copy( this.offset );
  8656. tmp.repeat.copy( this.repeat );
  8657. tmp.format = this.format;
  8658. tmp.type = this.type;
  8659. tmp.depthBuffer = this.depthBuffer;
  8660. tmp.stencilBuffer = this.stencilBuffer;
  8661. return tmp;
  8662. };
  8663. /**
  8664. * @author alteredq / http://alteredqualia.com
  8665. */
  8666. THREE.WebGLRenderTargetCube = function ( width, height, options ) {
  8667. THREE.WebGLRenderTarget.call( this, width, height, options );
  8668. this.activeCubeFace = 0; // PX 0, NX 1, PY 2, NY 3, PZ 4, NZ 5
  8669. };
  8670. THREE.WebGLRenderTargetCube.prototype = new THREE.WebGLRenderTarget();
  8671. THREE.WebGLRenderTargetCube.prototype.constructor = THREE.WebGLRenderTargetCube;
  8672. /**
  8673. * @author mr.doob / http://mrdoob.com/
  8674. */
  8675. THREE.RenderableVertex = function () {
  8676. this.positionWorld = new THREE.Vector3();
  8677. this.positionScreen = new THREE.Vector4();
  8678. this.visible = true;
  8679. };
  8680. THREE.RenderableVertex.prototype.copy = function ( vertex ) {
  8681. this.positionWorld.copy( vertex.positionWorld );
  8682. this.positionScreen.copy( vertex.positionScreen );
  8683. }
  8684. /**
  8685. * @author mr.doob / http://mrdoob.com/
  8686. */
  8687. THREE.RenderableFace3 = function () {
  8688. this.v1 = new THREE.RenderableVertex();
  8689. this.v2 = new THREE.RenderableVertex();
  8690. this.v3 = new THREE.RenderableVertex();
  8691. this.centroidWorld = new THREE.Vector3();
  8692. this.centroidScreen = new THREE.Vector3();
  8693. this.normalWorld = new THREE.Vector3();
  8694. this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  8695. this.material = null;
  8696. this.faceMaterial = null;
  8697. this.uvs = [[]];
  8698. this.z = null;
  8699. };
  8700. /**
  8701. * @author mr.doob / http://mrdoob.com/
  8702. */
  8703. THREE.RenderableFace4 = function () {
  8704. this.v1 = new THREE.RenderableVertex();
  8705. this.v2 = new THREE.RenderableVertex();
  8706. this.v3 = new THREE.RenderableVertex();
  8707. this.v4 = new THREE.RenderableVertex();
  8708. this.centroidWorld = new THREE.Vector3();
  8709. this.centroidScreen = new THREE.Vector3();
  8710. this.normalWorld = new THREE.Vector3();
  8711. this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  8712. this.material = null;
  8713. this.faceMaterial = null;
  8714. this.uvs = [[]];
  8715. this.z = null;
  8716. };
  8717. /**
  8718. * @author mr.doob / http://mrdoob.com/
  8719. */
  8720. THREE.RenderableObject = function () {
  8721. this.object = null;
  8722. this.z = null;
  8723. };
  8724. /**
  8725. * @author mr.doob / http://mrdoob.com/
  8726. */
  8727. THREE.RenderableParticle = function () {
  8728. this.x = null;
  8729. this.y = null;
  8730. this.z = null;
  8731. this.rotation = null;
  8732. this.scale = new THREE.Vector2();
  8733. this.material = null;
  8734. };
  8735. /**
  8736. * @author mr.doob / http://mrdoob.com/
  8737. */
  8738. THREE.RenderableLine = function () {
  8739. this.z = null;
  8740. this.v1 = new THREE.RenderableVertex();
  8741. this.v2 = new THREE.RenderableVertex();
  8742. this.material = null;
  8743. };
  8744. /**
  8745. * @author alteredq / http://alteredqualia.com/
  8746. */
  8747. THREE.BufferGeometry = function () {
  8748. this.id = THREE.GeometryCount ++;
  8749. // GL buffers
  8750. this.vertexIndexBuffer = null;
  8751. this.vertexPositionBuffer = null;
  8752. this.vertexNormalBuffer = null;
  8753. this.vertexUvBuffer = null;
  8754. this.vertexColorBuffer = null;
  8755. // typed arrays (kept only if dynamic flag is set)
  8756. this.vertexIndexArray = null;
  8757. this.vertexPositionArray = null;
  8758. this.vertexNormalArray = null;
  8759. this.vertexUvArray = null;
  8760. this.vertexColorArray = null;
  8761. this.dynamic = false;
  8762. // boundings
  8763. this.boundingBox = null;
  8764. this.boundingSphere = null;
  8765. // for compatibility
  8766. this.morphTargets = [];
  8767. };
  8768. THREE.BufferGeometry.prototype = {
  8769. constructor : THREE.BufferGeometry,
  8770. // for compatibility
  8771. computeBoundingBox: function () {
  8772. },
  8773. // for compatibility
  8774. computeBoundingSphere: function () {
  8775. }
  8776. };
  8777. /**
  8778. * @author alteredq / http://alteredqualia.com/
  8779. */
  8780. THREE.Gyroscope = function () {
  8781. THREE.Object3D.call( this );
  8782. };
  8783. THREE.Gyroscope.prototype = new THREE.Object3D();
  8784. THREE.Gyroscope.prototype.constructor = THREE.Gyroscope;
  8785. THREE.Gyroscope.prototype.updateMatrixWorld = function ( force ) {
  8786. this.matrixAutoUpdate && this.updateMatrix();
  8787. // update matrixWorld
  8788. if ( this.matrixWorldNeedsUpdate || force ) {
  8789. if ( this.parent ) {
  8790. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  8791. this.matrixWorld.decompose( this.translationWorld, this.rotationWorld, this.scaleWorld );
  8792. this.matrix.decompose( this.translationObject, this.rotationObject, this.scaleObject );
  8793. this.matrixWorld.compose( this.translationWorld, this.rotationObject, this.scaleWorld );
  8794. } else {
  8795. this.matrixWorld.copy( this.matrix );
  8796. }
  8797. this.matrixWorldNeedsUpdate = false;
  8798. force = true;
  8799. }
  8800. // update children
  8801. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  8802. this.children[ i ].updateMatrixWorld( force );
  8803. }
  8804. };
  8805. THREE.Gyroscope.prototype.translationWorld = new THREE.Vector3();
  8806. THREE.Gyroscope.prototype.translationObject = new THREE.Vector3();
  8807. THREE.Gyroscope.prototype.rotationWorld = new THREE.Quaternion();
  8808. THREE.Gyroscope.prototype.rotationObject = new THREE.Quaternion();
  8809. THREE.Gyroscope.prototype.scaleWorld = new THREE.Vector3();
  8810. THREE.Gyroscope.prototype.scaleObject = new THREE.Vector3();
  8811. /**
  8812. * @author alteredq / http://alteredqualia.com/
  8813. *
  8814. * - shows frustum, line of sight and up of the camera
  8815. * - suitable for fast updates
  8816. * - based on frustum visualization in lightgl.js shadowmap example
  8817. * http://evanw.github.com/lightgl.js/tests/shadowmap.html
  8818. */
  8819. THREE.CameraHelper = function ( camera ) {
  8820. THREE.Object3D.call( this );
  8821. var _this = this;
  8822. this.lineGeometry = new THREE.Geometry();
  8823. this.lineMaterial = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: THREE.FaceColors } );
  8824. this.pointMap = {};
  8825. // colors
  8826. var hexFrustum = 0xffaa00,
  8827. hexCone = 0xff0000,
  8828. hexUp = 0x00aaff,
  8829. hexTarget = 0xffffff,
  8830. hexCross = 0x333333;
  8831. // near
  8832. addLine( "n1", "n2", hexFrustum );
  8833. addLine( "n2", "n4", hexFrustum );
  8834. addLine( "n4", "n3", hexFrustum );
  8835. addLine( "n3", "n1", hexFrustum );
  8836. // far
  8837. addLine( "f1", "f2", hexFrustum );
  8838. addLine( "f2", "f4", hexFrustum );
  8839. addLine( "f4", "f3", hexFrustum );
  8840. addLine( "f3", "f1", hexFrustum );
  8841. // sides
  8842. addLine( "n1", "f1", hexFrustum );
  8843. addLine( "n2", "f2", hexFrustum );
  8844. addLine( "n3", "f3", hexFrustum );
  8845. addLine( "n4", "f4", hexFrustum );
  8846. // cone
  8847. addLine( "p", "n1", hexCone );
  8848. addLine( "p", "n2", hexCone );
  8849. addLine( "p", "n3", hexCone );
  8850. addLine( "p", "n4", hexCone );
  8851. // up
  8852. addLine( "u1", "u2", hexUp );
  8853. addLine( "u2", "u3", hexUp );
  8854. addLine( "u3", "u1", hexUp );
  8855. // target
  8856. addLine( "c", "t", hexTarget );
  8857. addLine( "p", "c", hexCross );
  8858. // cross
  8859. addLine( "cn1", "cn2", hexCross );
  8860. addLine( "cn3", "cn4", hexCross );
  8861. addLine( "cf1", "cf2", hexCross );
  8862. addLine( "cf3", "cf4", hexCross );
  8863. this.camera = camera;
  8864. function addLine( a, b, hex ) {
  8865. addPoint( a, hex );
  8866. addPoint( b, hex );
  8867. }
  8868. function addPoint( id, hex ) {
  8869. _this.lineGeometry.vertices.push( new THREE.Vertex( new THREE.Vector3() ) );
  8870. _this.lineGeometry.colors.push( new THREE.Color( hex ) );
  8871. if ( _this.pointMap[ id ] === undefined ) _this.pointMap[ id ] = [];
  8872. _this.pointMap[ id ].push( _this.lineGeometry.vertices.length - 1 );
  8873. }
  8874. this.update( camera );
  8875. this.lines = new THREE.Line( this.lineGeometry, this.lineMaterial, THREE.LinePieces );
  8876. this.add( this.lines );
  8877. };
  8878. THREE.CameraHelper.prototype = new THREE.Object3D();
  8879. THREE.CameraHelper.prototype.constructor = THREE.CameraHelper;
  8880. THREE.CameraHelper.prototype.update = function () {
  8881. var camera = this.camera;
  8882. var w = 1;
  8883. var h = 1;
  8884. var _this = this;
  8885. // we need just camera projection matrix
  8886. // world matrix must be identity
  8887. THREE.CameraHelper.__c.projectionMatrix.copy( camera.projectionMatrix );
  8888. // center / target
  8889. setPoint( "c", 0, 0, -1 );
  8890. setPoint( "t", 0, 0, 1 );
  8891. // near
  8892. setPoint( "n1", -w, -h, -1 );
  8893. setPoint( "n2", w, -h, -1 );
  8894. setPoint( "n3", -w, h, -1 );
  8895. setPoint( "n4", w, h, -1 );
  8896. // far
  8897. setPoint( "f1", -w, -h, 1 );
  8898. setPoint( "f2", w, -h, 1 );
  8899. setPoint( "f3", -w, h, 1 );
  8900. setPoint( "f4", w, h, 1 );
  8901. // up
  8902. setPoint( "u1", w * 0.7, h * 1.1, -1 );
  8903. setPoint( "u2", -w * 0.7, h * 1.1, -1 );
  8904. setPoint( "u3", 0, h * 2, -1 );
  8905. // cross
  8906. setPoint( "cf1", -w, 0, 1 );
  8907. setPoint( "cf2", w, 0, 1 );
  8908. setPoint( "cf3", 0, -h, 1 );
  8909. setPoint( "cf4", 0, h, 1 );
  8910. setPoint( "cn1", -w, 0, -1 );
  8911. setPoint( "cn2", w, 0, -1 );
  8912. setPoint( "cn3", 0, -h, -1 );
  8913. setPoint( "cn4", 0, h, -1 );
  8914. function setPoint( point, x, y, z ) {
  8915. THREE.CameraHelper.__v.set( x, y, z );
  8916. THREE.CameraHelper.__projector.unprojectVector( THREE.CameraHelper.__v, THREE.CameraHelper.__c );
  8917. var points = _this.pointMap[ point ];
  8918. if ( points !== undefined ) {
  8919. for ( var i = 0, il = points.length; i < il; i ++ ) {
  8920. var j = points[ i ];
  8921. _this.lineGeometry.vertices[ j ].position.copy( THREE.CameraHelper.__v );
  8922. }
  8923. }
  8924. }
  8925. this.lineGeometry.__dirtyVertices = true;
  8926. };
  8927. THREE.CameraHelper.__projector = new THREE.Projector();
  8928. THREE.CameraHelper.__v = new THREE.Vector3();
  8929. THREE.CameraHelper.__c = new THREE.Camera();
  8930. /**
  8931. * @author mikael emtinger / http://gomo.se/
  8932. * @author alteredq / http://alteredqualia.com/
  8933. */
  8934. THREE.LensFlare = function ( texture, size, distance, blending, color ) {
  8935. THREE.Object3D.call( this );
  8936. this.lensFlares = [];
  8937. this.positionScreen = new THREE.Vector3();
  8938. this.customUpdateCallback = undefined;
  8939. if( texture !== undefined ) {
  8940. this.add( texture, size, distance, blending, color );
  8941. }
  8942. };
  8943. THREE.LensFlare.prototype = new THREE.Object3D();
  8944. THREE.LensFlare.prototype.constructor = THREE.LensFlare;
  8945. THREE.LensFlare.prototype.supr = THREE.Object3D.prototype;
  8946. /*
  8947. * Add: adds another flare
  8948. */
  8949. THREE.LensFlare.prototype.add = function ( texture, size, distance, blending, color, opacity ) {
  8950. if( size === undefined ) size = -1;
  8951. if( distance === undefined ) distance = 0;
  8952. if( opacity === undefined ) opacity = 1;
  8953. if( color === undefined ) color = new THREE.Color( 0xffffff );
  8954. if( blending === undefined ) blending = THREE.NormalBlending;
  8955. distance = Math.min( distance, Math.max( 0, distance ) );
  8956. this.lensFlares.push( { texture: texture, // THREE.Texture
  8957. size: size, // size in pixels (-1 = use texture.width)
  8958. distance: distance, // distance (0-1) from light source (0=at light source)
  8959. x: 0, y: 0, z: 0, // screen position (-1 => 1) z = 0 is ontop z = 1 is back
  8960. scale: 1, // scale
  8961. rotation: 1, // rotation
  8962. opacity: opacity, // opacity
  8963. color: color, // color
  8964. blending: blending } ); // blending
  8965. };
  8966. /*
  8967. * Update lens flares update positions on all flares based on the screen position
  8968. * Set myLensFlare.customUpdateCallback to alter the flares in your project specific way.
  8969. */
  8970. THREE.LensFlare.prototype.updateLensFlares = function () {
  8971. var f, fl = this.lensFlares.length;
  8972. var flare;
  8973. var vecX = -this.positionScreen.x * 2;
  8974. var vecY = -this.positionScreen.y * 2;
  8975. for( f = 0; f < fl; f ++ ) {
  8976. flare = this.lensFlares[ f ];
  8977. flare.x = this.positionScreen.x + vecX * flare.distance;
  8978. flare.y = this.positionScreen.y + vecY * flare.distance;
  8979. flare.wantedRotation = flare.x * Math.PI * 0.25;
  8980. flare.rotation += ( flare.wantedRotation - flare.rotation ) * 0.25;
  8981. }
  8982. };
  8983. /**
  8984. * @author alteredq / http://alteredqualia.com/
  8985. */
  8986. THREE.ImmediateRenderObject = function ( ) {
  8987. THREE.Object3D.call( this );
  8988. this.render = function( renderCallback ) {
  8989. };
  8990. };
  8991. THREE.ImmediateRenderObject.prototype = new THREE.Object3D();
  8992. THREE.ImmediateRenderObject.prototype.constructor = THREE.ImmediateRenderObject;
  8993. /**
  8994. * @author mikael emtinger / http://gomo.se/
  8995. * @author alteredq / http://alteredqualia.com/
  8996. */
  8997. THREE.LensFlarePlugin = function ( ) {
  8998. var _gl, _renderer, _lensFlare = {};
  8999. this.init = function ( renderer ) {
  9000. _gl = renderer.context;
  9001. _renderer = renderer;
  9002. _lensFlare.vertices = new Float32Array( 8 + 8 );
  9003. _lensFlare.faces = new Uint16Array( 6 );
  9004. var i = 0;
  9005. _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = -1; // vertex
  9006. _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 0; // uv... etc.
  9007. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = -1;
  9008. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 0;
  9009. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1;
  9010. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1;
  9011. _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = 1;
  9012. _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 1;
  9013. i = 0;
  9014. _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 1; _lensFlare.faces[ i++ ] = 2;
  9015. _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 2; _lensFlare.faces[ i++ ] = 3;
  9016. // buffers
  9017. _lensFlare.vertexBuffer = _gl.createBuffer();
  9018. _lensFlare.elementBuffer = _gl.createBuffer();
  9019. _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
  9020. _gl.bufferData( _gl.ARRAY_BUFFER, _lensFlare.vertices, _gl.STATIC_DRAW );
  9021. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
  9022. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.faces, _gl.STATIC_DRAW );
  9023. // textures
  9024. _lensFlare.tempTexture = _gl.createTexture();
  9025. _lensFlare.occlusionTexture = _gl.createTexture();
  9026. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  9027. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, 16, 16, 0, _gl.RGB, _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. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture );
  9033. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, 16, 16, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, null );
  9034. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  9035. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  9036. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
  9037. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
  9038. if ( _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) <= 0 ) {
  9039. _lensFlare.hasVertexTexture = false;
  9040. _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlare" ] );
  9041. } else {
  9042. _lensFlare.hasVertexTexture = true;
  9043. _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlareVertexTexture" ] );
  9044. }
  9045. _lensFlare.attributes = {};
  9046. _lensFlare.uniforms = {};
  9047. _lensFlare.attributes.vertex = _gl.getAttribLocation ( _lensFlare.program, "position" );
  9048. _lensFlare.attributes.uv = _gl.getAttribLocation ( _lensFlare.program, "uv" );
  9049. _lensFlare.uniforms.renderType = _gl.getUniformLocation( _lensFlare.program, "renderType" );
  9050. _lensFlare.uniforms.map = _gl.getUniformLocation( _lensFlare.program, "map" );
  9051. _lensFlare.uniforms.occlusionMap = _gl.getUniformLocation( _lensFlare.program, "occlusionMap" );
  9052. _lensFlare.uniforms.opacity = _gl.getUniformLocation( _lensFlare.program, "opacity" );
  9053. _lensFlare.uniforms.color = _gl.getUniformLocation( _lensFlare.program, "color" );
  9054. _lensFlare.uniforms.scale = _gl.getUniformLocation( _lensFlare.program, "scale" );
  9055. _lensFlare.uniforms.rotation = _gl.getUniformLocation( _lensFlare.program, "rotation" );
  9056. _lensFlare.uniforms.screenPosition = _gl.getUniformLocation( _lensFlare.program, "screenPosition" );
  9057. _lensFlare.attributesEnabled = false;
  9058. };
  9059. /*
  9060. * Render lens flares
  9061. * Method: renders 16x16 0xff00ff-colored points scattered over the light source area,
  9062. * reads these back and calculates occlusion.
  9063. * Then _lensFlare.update_lensFlares() is called to re-position and
  9064. * update transparency of flares. Then they are rendered.
  9065. *
  9066. */
  9067. this.render = function ( scene, camera, viewportWidth, viewportHeight ) {
  9068. var flares = scene.__webglFlares,
  9069. nFlares = flares.length;
  9070. if ( ! nFlares ) return;
  9071. var tempPosition = new THREE.Vector3();
  9072. var invAspect = viewportHeight / viewportWidth,
  9073. halfViewportWidth = viewportWidth * 0.5,
  9074. halfViewportHeight = viewportHeight * 0.5;
  9075. var size = 16 / viewportHeight,
  9076. scale = new THREE.Vector2( size * invAspect, size );
  9077. var screenPosition = new THREE.Vector3( 1, 1, 0 ),
  9078. screenPositionPixels = new THREE.Vector2( 1, 1 );
  9079. var uniforms = _lensFlare.uniforms,
  9080. attributes = _lensFlare.attributes;
  9081. // set _lensFlare program and reset blending
  9082. _gl.useProgram( _lensFlare.program );
  9083. if ( ! _lensFlare.attributesEnabled ) {
  9084. _gl.enableVertexAttribArray( _lensFlare.attributes.vertex );
  9085. _gl.enableVertexAttribArray( _lensFlare.attributes.uv );
  9086. _lensFlare.attributesEnabled = true;
  9087. }
  9088. // loop through all lens flares to update their occlusion and positions
  9089. // setup gl and common used attribs/unforms
  9090. _gl.uniform1i( uniforms.occlusionMap, 0 );
  9091. _gl.uniform1i( uniforms.map, 1 );
  9092. _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
  9093. _gl.vertexAttribPointer( attributes.vertex, 2, _gl.FLOAT, false, 2 * 8, 0 );
  9094. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 );
  9095. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
  9096. _gl.disable( _gl.CULL_FACE );
  9097. _gl.depthMask( false );
  9098. var i, j, jl, flare, sprite;
  9099. for ( i = 0; i < nFlares; i ++ ) {
  9100. size = 16 / viewportHeight;
  9101. scale.set( size * invAspect, size );
  9102. // calc object screen position
  9103. flare = flares[ i ];
  9104. tempPosition.set( flare.matrixWorld.elements[12], flare.matrixWorld.elements[13], flare.matrixWorld.elements[14] );
  9105. camera.matrixWorldInverse.multiplyVector3( tempPosition );
  9106. camera.projectionMatrix.multiplyVector3( tempPosition );
  9107. // setup arrays for gl programs
  9108. screenPosition.copy( tempPosition )
  9109. screenPositionPixels.x = screenPosition.x * halfViewportWidth + halfViewportWidth;
  9110. screenPositionPixels.y = screenPosition.y * halfViewportHeight + halfViewportHeight;
  9111. // screen cull
  9112. if ( _lensFlare.hasVertexTexture || (
  9113. screenPositionPixels.x > 0 &&
  9114. screenPositionPixels.x < viewportWidth &&
  9115. screenPositionPixels.y > 0 &&
  9116. screenPositionPixels.y < viewportHeight ) ) {
  9117. // save current RGB to temp texture
  9118. _gl.activeTexture( _gl.TEXTURE1 );
  9119. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  9120. _gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
  9121. // render pink quad
  9122. _gl.uniform1i( uniforms.renderType, 0 );
  9123. _gl.uniform2f( uniforms.scale, scale.x, scale.y );
  9124. _gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z );
  9125. _gl.disable( _gl.BLEND );
  9126. _gl.enable( _gl.DEPTH_TEST );
  9127. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9128. // copy result to occlusionMap
  9129. _gl.activeTexture( _gl.TEXTURE0 );
  9130. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture );
  9131. _gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
  9132. // restore graphics
  9133. _gl.uniform1i( uniforms.renderType, 1 );
  9134. _gl.disable( _gl.DEPTH_TEST );
  9135. _gl.activeTexture( _gl.TEXTURE1 );
  9136. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  9137. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9138. // update object positions
  9139. flare.positionScreen.copy( screenPosition )
  9140. if ( flare.customUpdateCallback ) {
  9141. flare.customUpdateCallback( flare );
  9142. } else {
  9143. flare.updateLensFlares();
  9144. }
  9145. // render flares
  9146. _gl.uniform1i( uniforms.renderType, 2 );
  9147. _gl.enable( _gl.BLEND );
  9148. for ( j = 0, jl = flare.lensFlares.length; j < jl; j ++ ) {
  9149. sprite = flare.lensFlares[ j ];
  9150. if ( sprite.opacity > 0.001 && sprite.scale > 0.001 ) {
  9151. screenPosition.x = sprite.x;
  9152. screenPosition.y = sprite.y;
  9153. screenPosition.z = sprite.z;
  9154. size = sprite.size * sprite.scale / viewportHeight;
  9155. scale.x = size * invAspect;
  9156. scale.y = size;
  9157. _gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z );
  9158. _gl.uniform2f( uniforms.scale, scale.x, scale.y );
  9159. _gl.uniform1f( uniforms.rotation, sprite.rotation );
  9160. _gl.uniform1f( uniforms.opacity, sprite.opacity );
  9161. _gl.uniform3f( uniforms.color, sprite.color.r, sprite.color.g, sprite.color.b );
  9162. _renderer.setBlending( sprite.blending, sprite.blendEquation, sprite.blendSrc, sprite.blendDst );
  9163. _renderer.setTexture( sprite.texture, 1 );
  9164. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9165. }
  9166. }
  9167. }
  9168. }
  9169. // restore gl
  9170. _gl.enable( _gl.CULL_FACE );
  9171. _gl.enable( _gl.DEPTH_TEST );
  9172. _gl.depthMask( true );
  9173. };
  9174. function createProgram ( shader ) {
  9175. var program = _gl.createProgram();
  9176. var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER );
  9177. var vertexShader = _gl.createShader( _gl.VERTEX_SHADER );
  9178. _gl.shaderSource( fragmentShader, shader.fragmentShader );
  9179. _gl.shaderSource( vertexShader, shader.vertexShader );
  9180. _gl.compileShader( fragmentShader );
  9181. _gl.compileShader( vertexShader );
  9182. _gl.attachShader( program, fragmentShader );
  9183. _gl.attachShader( program, vertexShader );
  9184. _gl.linkProgram( program );
  9185. return program;
  9186. };
  9187. };/**
  9188. * @author alteredq / http://alteredqualia.com/
  9189. */
  9190. THREE.ShadowMapPlugin = function ( ) {
  9191. var _gl,
  9192. _renderer,
  9193. _depthMaterial, _depthMaterialMorph,
  9194. _frustum = new THREE.Frustum(),
  9195. _projScreenMatrix = new THREE.Matrix4(),
  9196. _min = new THREE.Vector3(),
  9197. _max = new THREE.Vector3();
  9198. this.init = function ( renderer ) {
  9199. _gl = renderer.context;
  9200. _renderer = renderer;
  9201. var depthShader = THREE.ShaderLib[ "depthRGBA" ];
  9202. var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms );
  9203. _depthMaterial = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms } );
  9204. _depthMaterialMorph = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, morphTargets: true } );
  9205. _depthMaterial._shadowPass = true;
  9206. _depthMaterialMorph._shadowPass = true;
  9207. };
  9208. this.render = function ( scene, camera ) {
  9209. if ( ! ( _renderer.shadowMapEnabled && _renderer.shadowMapAutoUpdate ) ) return;
  9210. this.update( scene, camera );
  9211. };
  9212. this.update = function ( scene, camera ) {
  9213. var i, il, j, jl, n,
  9214. shadowMap, shadowMatrix, shadowCamera,
  9215. program, buffer, material,
  9216. webglObject, object, light,
  9217. renderList,
  9218. lights = [],
  9219. k = 0,
  9220. fog = null;
  9221. // set GL state for depth map
  9222. _gl.clearColor( 1, 1, 1, 1 );
  9223. _gl.disable( _gl.BLEND );
  9224. if ( _renderer.shadowMapCullFrontFaces ) _gl.cullFace( _gl.FRONT );
  9225. _renderer.setDepthTest( true );
  9226. // preprocess lights
  9227. // - skip lights that are not casting shadows
  9228. // - create virtual lights for cascaded shadow maps
  9229. for ( i = 0, il = scene.__lights.length; i < il; i ++ ) {
  9230. light = scene.__lights[ i ];
  9231. if ( ! light.castShadow ) continue;
  9232. if ( ( light instanceof THREE.DirectionalLight ) && light.shadowCascade ) {
  9233. for ( n = 0; n < light.shadowCascadeCount; n ++ ) {
  9234. var virtualLight;
  9235. if ( ! light.shadowCascadeArray[ n ] ) {
  9236. virtualLight = createVirtualLight( light, n );
  9237. virtualLight.originalCamera = camera;
  9238. var gyro = new THREE.Gyroscope();
  9239. gyro.position = light.shadowCascadeOffset;
  9240. gyro.add( virtualLight );
  9241. gyro.add( virtualLight.target );
  9242. camera.add( gyro );
  9243. light.shadowCascadeArray[ n ] = virtualLight;
  9244. console.log( "Created virtualLight", virtualLight );
  9245. } else {
  9246. virtualLight = light.shadowCascadeArray[ n ];
  9247. }
  9248. updateVirtualLight( light, n );
  9249. lights[ k ] = virtualLight;
  9250. k ++;
  9251. }
  9252. } else {
  9253. lights[ k ] = light;
  9254. k ++;
  9255. }
  9256. }
  9257. // render depth map
  9258. for ( i = 0, il = lights.length; i < il; i ++ ) {
  9259. light = lights[ i ];
  9260. if ( ! light.shadowMap ) {
  9261. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat };
  9262. light.shadowMap = new THREE.WebGLRenderTarget( light.shadowMapWidth, light.shadowMapHeight, pars );
  9263. light.shadowMapSize = new THREE.Vector2( light.shadowMapWidth, light.shadowMapHeight );
  9264. light.shadowMatrix = new THREE.Matrix4();
  9265. }
  9266. if ( ! light.shadowCamera ) {
  9267. if ( light instanceof THREE.SpotLight ) {
  9268. light.shadowCamera = new THREE.PerspectiveCamera( light.shadowCameraFov, light.shadowMapWidth / light.shadowMapHeight, light.shadowCameraNear, light.shadowCameraFar );
  9269. } else if ( light instanceof THREE.DirectionalLight ) {
  9270. light.shadowCamera = new THREE.OrthographicCamera( light.shadowCameraLeft, light.shadowCameraRight, light.shadowCameraTop, light.shadowCameraBottom, light.shadowCameraNear, light.shadowCameraFar );
  9271. } else {
  9272. console.error( "Unsupported light type for shadow" );
  9273. continue;
  9274. }
  9275. scene.add( light.shadowCamera );
  9276. if ( _renderer.autoUpdateScene ) scene.updateMatrixWorld();
  9277. }
  9278. if ( light.shadowCameraVisible && ! light.cameraHelper ) {
  9279. light.cameraHelper = new THREE.CameraHelper( light.shadowCamera );
  9280. light.shadowCamera.add( light.cameraHelper );
  9281. }
  9282. if ( light.isVirtual && virtualLight.originalCamera == camera ) {
  9283. updateShadowCamera( camera, light );
  9284. }
  9285. shadowMap = light.shadowMap;
  9286. shadowMatrix = light.shadowMatrix;
  9287. shadowCamera = light.shadowCamera;
  9288. shadowCamera.position.copy( light.matrixWorld.getPosition() );
  9289. shadowCamera.lookAt( light.target.matrixWorld.getPosition() );
  9290. shadowCamera.updateMatrixWorld();
  9291. shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
  9292. if ( light.cameraHelper ) light.cameraHelper.lines.visible = light.shadowCameraVisible;
  9293. if ( light.shadowCameraVisible ) light.cameraHelper.update();
  9294. // compute shadow matrix
  9295. shadowMatrix.set( 0.5, 0.0, 0.0, 0.5,
  9296. 0.0, 0.5, 0.0, 0.5,
  9297. 0.0, 0.0, 0.5, 0.5,
  9298. 0.0, 0.0, 0.0, 1.0 );
  9299. shadowMatrix.multiplySelf( shadowCamera.projectionMatrix );
  9300. shadowMatrix.multiplySelf( shadowCamera.matrixWorldInverse );
  9301. // update camera matrices and frustum
  9302. if ( ! shadowCamera._viewMatrixArray ) shadowCamera._viewMatrixArray = new Float32Array( 16 );
  9303. if ( ! shadowCamera._projectionMatrixArray ) shadowCamera._projectionMatrixArray = new Float32Array( 16 );
  9304. shadowCamera.matrixWorldInverse.flattenToArray( shadowCamera._viewMatrixArray );
  9305. shadowCamera.projectionMatrix.flattenToArray( shadowCamera._projectionMatrixArray );
  9306. _projScreenMatrix.multiply( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  9307. _frustum.setFromMatrix( _projScreenMatrix );
  9308. // render shadow map
  9309. _renderer.setRenderTarget( shadowMap );
  9310. _renderer.clear();
  9311. // set object matrices & frustum culling
  9312. renderList = scene.__webglObjects;
  9313. for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
  9314. webglObject = renderList[ j ];
  9315. object = webglObject.object;
  9316. webglObject.render = false;
  9317. if ( object.visible && object.castShadow ) {
  9318. if ( ! ( object instanceof THREE.Mesh ) || ! ( object.frustumCulled ) || _frustum.contains( object ) ) {
  9319. //object.matrixWorld.flattenToArray( object._objectMatrixArray );
  9320. object._modelViewMatrix.multiply( shadowCamera.matrixWorldInverse, object.matrixWorld);
  9321. webglObject.render = true;
  9322. }
  9323. }
  9324. }
  9325. // render regular objects
  9326. for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
  9327. webglObject = renderList[ j ];
  9328. if ( webglObject.render ) {
  9329. object = webglObject.object;
  9330. buffer = webglObject.buffer;
  9331. _renderer.setObjectFaces( object );
  9332. if ( object.customDepthMaterial ) {
  9333. material = object.customDepthMaterial;
  9334. } else if ( object.geometry.morphTargets.length ) {
  9335. material = _depthMaterialMorph;
  9336. } else {
  9337. material = _depthMaterial;
  9338. }
  9339. if ( buffer instanceof THREE.BufferGeometry ) {
  9340. _renderer.renderBufferDirect( shadowCamera, scene.__lights, fog, material, buffer, object );
  9341. } else {
  9342. _renderer.renderBuffer( shadowCamera, scene.__lights, fog, material, buffer, object );
  9343. }
  9344. }
  9345. }
  9346. // set matrices and render immediate objects
  9347. renderList = scene.__webglObjectsImmediate;
  9348. for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
  9349. webglObject = renderList[ j ];
  9350. object = webglObject.object;
  9351. if ( object.visible && object.castShadow ) {
  9352. if( object.matrixAutoUpdate ) {
  9353. //object.matrixWorld.flattenToArray( object._objectMatrixArray );
  9354. }
  9355. object._modelViewMatrix.multiply( shadowCamera.matrixWorldInverse, object.matrixWorld);
  9356. _renderer.renderImmediateObject( shadowCamera, scene.__lights, fog, _depthMaterial, object );
  9357. }
  9358. }
  9359. }
  9360. // restore GL state
  9361. var clearColor = _renderer.getClearColor(),
  9362. clearAlpha = _renderer.getClearAlpha();
  9363. _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha );
  9364. _gl.enable( _gl.BLEND );
  9365. if ( _renderer.shadowMapCullFrontFaces ) _gl.cullFace( _gl.BACK );
  9366. };
  9367. function createVirtualLight( light, cascade ) {
  9368. var virtualLight = new THREE.DirectionalLight();
  9369. virtualLight.isVirtual = true;
  9370. virtualLight.onlyShadow = true;
  9371. virtualLight.castShadow = true;
  9372. virtualLight.shadowCameraNear = light.shadowCameraNear;
  9373. virtualLight.shadowCameraFar = light.shadowCameraFar;
  9374. virtualLight.shadowCameraLeft = light.shadowCameraLeft;
  9375. virtualLight.shadowCameraRight = light.shadowCameraRight;
  9376. virtualLight.shadowCameraBottom = light.shadowCameraBottom;
  9377. virtualLight.shadowCameraTop = light.shadowCameraTop;
  9378. virtualLight.shadowCameraVisible = light.shadowCameraVisible;
  9379. virtualLight.shadowDarkness = light.shadowDarkness;
  9380. virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
  9381. virtualLight.shadowMapWidth = light.shadowCascadeWidth[ cascade ];
  9382. virtualLight.shadowMapHeight = light.shadowCascadeHeight[ cascade ];
  9383. virtualLight.pointsWorld = [];
  9384. virtualLight.pointsFrustum = [];
  9385. var pointsWorld = virtualLight.pointsWorld,
  9386. pointsFrustum = virtualLight.pointsFrustum;
  9387. for ( var i = 0; i < 8; i ++ ) {
  9388. pointsWorld[ i ] = new THREE.Vector3();
  9389. pointsFrustum[ i ] = new THREE.Vector3();
  9390. }
  9391. var nearZ = light.shadowCascadeNearZ[ cascade ];
  9392. var farZ = light.shadowCascadeFarZ[ cascade ];
  9393. pointsFrustum[ 0 ].set( -1, -1, nearZ );
  9394. pointsFrustum[ 1 ].set( 1, -1, nearZ );
  9395. pointsFrustum[ 2 ].set( -1, 1, nearZ );
  9396. pointsFrustum[ 3 ].set( 1, 1, nearZ );
  9397. pointsFrustum[ 4 ].set( -1, -1, farZ );
  9398. pointsFrustum[ 5 ].set( 1, -1, farZ );
  9399. pointsFrustum[ 6 ].set( -1, 1, farZ );
  9400. pointsFrustum[ 7 ].set( 1, 1, farZ );
  9401. return virtualLight;
  9402. }
  9403. // Synchronize virtual light with the original light
  9404. function updateVirtualLight( light, cascade ) {
  9405. var virtualLight = light.shadowCascadeArray[ cascade ];
  9406. virtualLight.position.copy( light.position );
  9407. virtualLight.target.position.copy( light.target.position );
  9408. virtualLight.lookAt( virtualLight.target );
  9409. virtualLight.shadowCameraVisible = light.shadowCameraVisible;
  9410. virtualLight.shadowDarkness = light.shadowDarkness;
  9411. virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
  9412. var nearZ = light.shadowCascadeNearZ[ cascade ];
  9413. var farZ = light.shadowCascadeFarZ[ cascade ];
  9414. var pointsFrustum = virtualLight.pointsFrustum;
  9415. pointsFrustum[ 0 ].z = nearZ;
  9416. pointsFrustum[ 1 ].z = nearZ;
  9417. pointsFrustum[ 2 ].z = nearZ;
  9418. pointsFrustum[ 3 ].z = nearZ;
  9419. pointsFrustum[ 4 ].z = farZ;
  9420. pointsFrustum[ 5 ].z = farZ;
  9421. pointsFrustum[ 6 ].z = farZ;
  9422. pointsFrustum[ 7 ].z = farZ;
  9423. }
  9424. // Fit shadow camera's ortho frustum to camera frustum
  9425. function updateShadowCamera( camera, light ) {
  9426. var shadowCamera = light.shadowCamera,
  9427. pointsFrustum = light.pointsFrustum,
  9428. pointsWorld = light.pointsWorld;
  9429. _min.set( Infinity, Infinity, Infinity );
  9430. _max.set( -Infinity, -Infinity, -Infinity );
  9431. for ( var i = 0; i < 8; i ++ ) {
  9432. var p = pointsWorld[ i ];
  9433. p.copy( pointsFrustum[ i ] );
  9434. THREE.ShadowMapPlugin.__projector.unprojectVector( p, camera );
  9435. shadowCamera.matrixWorldInverse.multiplyVector3( p );
  9436. if ( p.x < _min.x ) _min.x = p.x;
  9437. if ( p.x > _max.x ) _max.x = p.x;
  9438. if ( p.y < _min.y ) _min.y = p.y;
  9439. if ( p.y > _max.y ) _max.y = p.y;
  9440. if ( p.z < _min.z ) _min.z = p.z;
  9441. if ( p.z > _max.z ) _max.z = p.z;
  9442. }
  9443. shadowCamera.left = _min.x;
  9444. shadowCamera.right = _max.x;
  9445. shadowCamera.top = _max.y;
  9446. shadowCamera.bottom = _min.y;
  9447. // can't really fit near/far
  9448. //shadowCamera.near = _min.z;
  9449. //shadowCamera.far = _max.z;
  9450. shadowCamera.updateProjectionMatrix();
  9451. }
  9452. };
  9453. THREE.ShadowMapPlugin.__projector = new THREE.Projector();
  9454. /**
  9455. * @author mikael emtinger / http://gomo.se/
  9456. * @author alteredq / http://alteredqualia.com/
  9457. */
  9458. THREE.SpritePlugin = function ( ) {
  9459. var _gl, _renderer, _sprite = {};
  9460. this.init = function ( renderer ) {
  9461. _gl = renderer.context;
  9462. _renderer = renderer;
  9463. _sprite.vertices = new Float32Array( 8 + 8 );
  9464. _sprite.faces = new Uint16Array( 6 );
  9465. var i = 0;
  9466. _sprite.vertices[ i++ ] = -1; _sprite.vertices[ i++ ] = -1; // vertex 0
  9467. _sprite.vertices[ i++ ] = 0; _sprite.vertices[ i++ ] = 1; // uv 0
  9468. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = -1; // vertex 1
  9469. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 1; // uv 1
  9470. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 1; // vertex 2
  9471. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 0; // uv 2
  9472. _sprite.vertices[ i++ ] = -1; _sprite.vertices[ i++ ] = 1; // vertex 3
  9473. _sprite.vertices[ i++ ] = 0; _sprite.vertices[ i++ ] = 0; // uv 3
  9474. i = 0;
  9475. _sprite.faces[ i++ ] = 0; _sprite.faces[ i++ ] = 1; _sprite.faces[ i++ ] = 2;
  9476. _sprite.faces[ i++ ] = 0; _sprite.faces[ i++ ] = 2; _sprite.faces[ i++ ] = 3;
  9477. _sprite.vertexBuffer = _gl.createBuffer();
  9478. _sprite.elementBuffer = _gl.createBuffer();
  9479. _gl.bindBuffer( _gl.ARRAY_BUFFER, _sprite.vertexBuffer );
  9480. _gl.bufferData( _gl.ARRAY_BUFFER, _sprite.vertices, _gl.STATIC_DRAW );
  9481. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _sprite.elementBuffer );
  9482. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _sprite.faces, _gl.STATIC_DRAW );
  9483. _sprite.program = createProgram( THREE.ShaderSprite[ "sprite" ] );
  9484. _sprite.attributes = {};
  9485. _sprite.uniforms = {};
  9486. _sprite.attributes.position = _gl.getAttribLocation ( _sprite.program, "position" );
  9487. _sprite.attributes.uv = _gl.getAttribLocation ( _sprite.program, "uv" );
  9488. _sprite.uniforms.uvOffset = _gl.getUniformLocation( _sprite.program, "uvOffset" );
  9489. _sprite.uniforms.uvScale = _gl.getUniformLocation( _sprite.program, "uvScale" );
  9490. _sprite.uniforms.rotation = _gl.getUniformLocation( _sprite.program, "rotation" );
  9491. _sprite.uniforms.scale = _gl.getUniformLocation( _sprite.program, "scale" );
  9492. _sprite.uniforms.alignment = _gl.getUniformLocation( _sprite.program, "alignment" );
  9493. _sprite.uniforms.color = _gl.getUniformLocation( _sprite.program, "color" );
  9494. _sprite.uniforms.map = _gl.getUniformLocation( _sprite.program, "map" );
  9495. _sprite.uniforms.opacity = _gl.getUniformLocation( _sprite.program, "opacity" );
  9496. _sprite.uniforms.useScreenCoordinates = _gl.getUniformLocation( _sprite.program, "useScreenCoordinates" );
  9497. _sprite.uniforms.affectedByDistance = _gl.getUniformLocation( _sprite.program, "affectedByDistance" );
  9498. _sprite.uniforms.screenPosition = _gl.getUniformLocation( _sprite.program, "screenPosition" );
  9499. _sprite.uniforms.modelViewMatrix = _gl.getUniformLocation( _sprite.program, "modelViewMatrix" );
  9500. _sprite.uniforms.projectionMatrix = _gl.getUniformLocation( _sprite.program, "projectionMatrix" );
  9501. _sprite.attributesEnabled = false;
  9502. };
  9503. this.render = function ( scene, camera, viewportWidth, viewportHeight ) {
  9504. var sprites = scene.__webglSprites,
  9505. nSprites = sprites.length;
  9506. if ( ! nSprites ) return;
  9507. var attributes = _sprite.attributes,
  9508. uniforms = _sprite.uniforms;
  9509. var invAspect = viewportHeight / viewportWidth;
  9510. var halfViewportWidth = viewportWidth * 0.5,
  9511. halfViewportHeight = viewportHeight * 0.5;
  9512. var mergeWith3D = true;
  9513. // setup gl
  9514. _gl.useProgram( _sprite.program );
  9515. if ( ! _sprite.attributesEnabled ) {
  9516. _gl.enableVertexAttribArray( attributes.position );
  9517. _gl.enableVertexAttribArray( attributes.uv );
  9518. _sprite.attributesEnabled = true;
  9519. }
  9520. _gl.disable( _gl.CULL_FACE );
  9521. _gl.enable( _gl.BLEND );
  9522. _gl.depthMask( true );
  9523. _gl.bindBuffer( _gl.ARRAY_BUFFER, _sprite.vertexBuffer );
  9524. _gl.vertexAttribPointer( attributes.position, 2, _gl.FLOAT, false, 2 * 8, 0 );
  9525. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 );
  9526. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _sprite.elementBuffer );
  9527. _gl.uniformMatrix4fv( uniforms.projectionMatrix, false, camera._projectionMatrixArray );
  9528. _gl.activeTexture( _gl.TEXTURE0 );
  9529. _gl.uniform1i( uniforms.map, 0 );
  9530. // update positions and sort
  9531. var i, sprite, screenPosition, size, scale = [];
  9532. for( i = 0; i < nSprites; i ++ ) {
  9533. sprite = sprites[ i ];
  9534. if ( ! sprite.visible || sprite.opacity === 0 ) continue;
  9535. if( ! sprite.useScreenCoordinates ) {
  9536. sprite._modelViewMatrix.multiply( camera.matrixWorldInverse, sprite.matrixWorld);
  9537. sprite.z = - sprite._modelViewMatrix.elements[14];
  9538. } else {
  9539. sprite.z = - sprite.position.z;
  9540. }
  9541. }
  9542. sprites.sort( painterSort );
  9543. // render all sprites
  9544. for( i = 0; i < nSprites; i ++ ) {
  9545. sprite = sprites[ i ];
  9546. if ( ! sprite.visible || sprite.opacity === 0 ) continue;
  9547. if ( sprite.map && sprite.map.image && sprite.map.image.width ) {
  9548. if ( sprite.useScreenCoordinates ) {
  9549. _gl.uniform1i( uniforms.useScreenCoordinates, 1 );
  9550. _gl.uniform3f( uniforms.screenPosition, ( sprite.position.x - halfViewportWidth ) / halfViewportWidth,
  9551. ( halfViewportHeight - sprite.position.y ) / halfViewportHeight,
  9552. Math.max( 0, Math.min( 1, sprite.position.z ) ) );
  9553. } else {
  9554. _gl.uniform1i( uniforms.useScreenCoordinates, 0 );
  9555. _gl.uniform1i( uniforms.affectedByDistance, sprite.affectedByDistance ? 1 : 0 );
  9556. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, sprite._modelViewMatrix.elements);
  9557. }
  9558. size = sprite.map.image.width / ( sprite.scaleByViewport ? viewportHeight : 1 );
  9559. scale[ 0 ] = size * invAspect * sprite.scale.x;
  9560. scale[ 1 ] = size * sprite.scale.y;
  9561. _gl.uniform2f( uniforms.uvScale, sprite.uvScale.x, sprite.uvScale.y );
  9562. _gl.uniform2f( uniforms.uvOffset, sprite.uvOffset.x, sprite.uvOffset.y );
  9563. _gl.uniform2f( uniforms.alignment, sprite.alignment.x, sprite.alignment.y );
  9564. _gl.uniform1f( uniforms.opacity, sprite.opacity );
  9565. _gl.uniform3f( uniforms.color, sprite.color.r, sprite.color.g, sprite.color.b );
  9566. _gl.uniform1f( uniforms.rotation, sprite.rotation );
  9567. _gl.uniform2fv( uniforms.scale, scale );
  9568. if ( sprite.mergeWith3D && !mergeWith3D ) {
  9569. _gl.enable( _gl.DEPTH_TEST );
  9570. mergeWith3D = true;
  9571. } else if ( ! sprite.mergeWith3D && mergeWith3D ) {
  9572. _gl.disable( _gl.DEPTH_TEST );
  9573. mergeWith3D = false;
  9574. }
  9575. _renderer.setBlending( sprite.blending, sprite.blendEquation, sprite.blendSrc, sprite.blendDst );
  9576. _renderer.setTexture( sprite.map, 0 );
  9577. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9578. }
  9579. }
  9580. // restore gl
  9581. _gl.enable( _gl.CULL_FACE );
  9582. _gl.enable( _gl.DEPTH_TEST );
  9583. _gl.depthMask( true );
  9584. };
  9585. function createProgram ( shader ) {
  9586. var program = _gl.createProgram();
  9587. var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER );
  9588. var vertexShader = _gl.createShader( _gl.VERTEX_SHADER );
  9589. _gl.shaderSource( fragmentShader, shader.fragmentShader );
  9590. _gl.shaderSource( vertexShader, shader.vertexShader );
  9591. _gl.compileShader( fragmentShader );
  9592. _gl.compileShader( vertexShader );
  9593. _gl.attachShader( program, fragmentShader );
  9594. _gl.attachShader( program, vertexShader );
  9595. _gl.linkProgram( program );
  9596. return program;
  9597. };
  9598. function painterSort ( a, b ) {
  9599. return b.z - a.z;
  9600. };
  9601. };/**
  9602. * @author mikael emtinger / http://gomo.se/
  9603. *
  9604. */
  9605. THREE.ShaderFlares = {
  9606. 'lensFlareVertexTexture': {
  9607. vertexShader: [
  9608. "uniform vec3 screenPosition;",
  9609. "uniform vec2 scale;",
  9610. "uniform float rotation;",
  9611. "uniform int renderType;",
  9612. "uniform sampler2D occlusionMap;",
  9613. "attribute vec2 position;",
  9614. "attribute vec2 uv;",
  9615. "varying vec2 vUV;",
  9616. "varying float vVisibility;",
  9617. "void main() {",
  9618. "vUV = uv;",
  9619. "vec2 pos = position;",
  9620. "if( renderType == 2 ) {",
  9621. "vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) ) +",
  9622. "texture2D( occlusionMap, vec2( 0.5, 0.1 ) ) +",
  9623. "texture2D( occlusionMap, vec2( 0.9, 0.1 ) ) +",
  9624. "texture2D( occlusionMap, vec2( 0.9, 0.5 ) ) +",
  9625. "texture2D( occlusionMap, vec2( 0.9, 0.9 ) ) +",
  9626. "texture2D( occlusionMap, vec2( 0.5, 0.9 ) ) +",
  9627. "texture2D( occlusionMap, vec2( 0.1, 0.9 ) ) +",
  9628. "texture2D( occlusionMap, vec2( 0.1, 0.5 ) ) +",
  9629. "texture2D( occlusionMap, vec2( 0.5, 0.5 ) );",
  9630. "vVisibility = ( visibility.r / 9.0 ) *",
  9631. "( 1.0 - visibility.g / 9.0 ) *",
  9632. "( visibility.b / 9.0 ) *",
  9633. "( 1.0 - visibility.a / 9.0 );",
  9634. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  9635. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  9636. "}",
  9637. "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  9638. "}"
  9639. ].join( "\n" ),
  9640. fragmentShader: [
  9641. "precision mediump float;",
  9642. "uniform sampler2D map;",
  9643. "uniform float opacity;",
  9644. "uniform int renderType;",
  9645. "uniform vec3 color;",
  9646. "varying vec2 vUV;",
  9647. "varying float vVisibility;",
  9648. "void main() {",
  9649. // pink square
  9650. "if( renderType == 0 ) {",
  9651. "gl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );",
  9652. // restore
  9653. "} else if( renderType == 1 ) {",
  9654. "gl_FragColor = texture2D( map, vUV );",
  9655. // flare
  9656. "} else {",
  9657. "vec4 texture = texture2D( map, vUV );",
  9658. "texture.a *= opacity * vVisibility;",
  9659. "gl_FragColor = texture;",
  9660. "gl_FragColor.rgb *= color;",
  9661. "}",
  9662. "}"
  9663. ].join( "\n" )
  9664. },
  9665. 'lensFlare': {
  9666. vertexShader: [
  9667. "uniform vec3 screenPosition;",
  9668. "uniform vec2 scale;",
  9669. "uniform float rotation;",
  9670. "uniform int renderType;",
  9671. "attribute vec2 position;",
  9672. "attribute vec2 uv;",
  9673. "varying vec2 vUV;",
  9674. "void main() {",
  9675. "vUV = uv;",
  9676. "vec2 pos = position;",
  9677. "if( renderType == 2 ) {",
  9678. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  9679. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  9680. "}",
  9681. "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  9682. "}"
  9683. ].join( "\n" ),
  9684. fragmentShader: [
  9685. "precision mediump float;",
  9686. "uniform sampler2D map;",
  9687. "uniform sampler2D occlusionMap;",
  9688. "uniform float opacity;",
  9689. "uniform int renderType;",
  9690. "uniform vec3 color;",
  9691. "varying vec2 vUV;",
  9692. "void main() {",
  9693. // pink square
  9694. "if( renderType == 0 ) {",
  9695. "gl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );",
  9696. // restore
  9697. "} else if( renderType == 1 ) {",
  9698. "gl_FragColor = texture2D( map, vUV );",
  9699. // flare
  9700. "} else {",
  9701. "float visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 ) ).a +",
  9702. "texture2D( occlusionMap, vec2( 0.9, 0.5 ) ).a +",
  9703. "texture2D( occlusionMap, vec2( 0.5, 0.9 ) ).a +",
  9704. "texture2D( occlusionMap, vec2( 0.1, 0.5 ) ).a;",
  9705. "visibility = ( 1.0 - visibility / 4.0 );",
  9706. "vec4 texture = texture2D( map, vUV );",
  9707. "texture.a *= opacity * visibility;",
  9708. "gl_FragColor = texture;",
  9709. "gl_FragColor.rgb *= color;",
  9710. "}",
  9711. "}"
  9712. ].join( "\n" )
  9713. }
  9714. };
  9715. /**
  9716. * @author mikael emtinger / http://gomo.se/
  9717. *
  9718. */
  9719. THREE.ShaderSprite = {
  9720. 'sprite': {
  9721. vertexShader: [
  9722. "uniform int useScreenCoordinates;",
  9723. "uniform int affectedByDistance;",
  9724. "uniform vec3 screenPosition;",
  9725. "uniform mat4 modelViewMatrix;",
  9726. "uniform mat4 projectionMatrix;",
  9727. "uniform float rotation;",
  9728. "uniform vec2 scale;",
  9729. "uniform vec2 alignment;",
  9730. "uniform vec2 uvOffset;",
  9731. "uniform vec2 uvScale;",
  9732. "attribute vec2 position;",
  9733. "attribute vec2 uv;",
  9734. "varying vec2 vUV;",
  9735. "void main() {",
  9736. "vUV = uvOffset + uv * uvScale;",
  9737. "vec2 alignedPosition = position + alignment;",
  9738. "vec2 rotatedPosition;",
  9739. "rotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;",
  9740. "rotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;",
  9741. "vec4 finalPosition;",
  9742. "if( useScreenCoordinates != 0 ) {",
  9743. "finalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );",
  9744. "} else {",
  9745. "finalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
  9746. "finalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );",
  9747. "}",
  9748. "gl_Position = finalPosition;",
  9749. "}"
  9750. ].join( "\n" ),
  9751. fragmentShader: [
  9752. "precision mediump float;",
  9753. "uniform vec3 color;",
  9754. "uniform sampler2D map;",
  9755. "uniform float opacity;",
  9756. "varying vec2 vUV;",
  9757. "void main() {",
  9758. "vec4 texture = texture2D( map, vUV );",
  9759. "gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );",
  9760. "}"
  9761. ].join( "\n" )
  9762. }
  9763. };