ThreeWebGL.js 365 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. var THREE = THREE || { REVISION: '49dev' };
  5. if ( ! self.Int32Array ) {
  6. self.Int32Array = Array;
  7. self.Float32Array = Array;
  8. }
  9. // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  10. // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
  11. // requestAnimationFrame polyfill by Erik Möller
  12. // fixes from Paul Irish and Tino Zijdel
  13. ( function () {
  14. var lastTime = 0;
  15. var vendors = [ 'ms', 'moz', 'webkit', 'o' ];
  16. for ( var x = 0; x < vendors.length && !window.requestAnimationFrame; ++ x ) {
  17. window.requestAnimationFrame = window[ vendors[ x ] + 'RequestAnimationFrame' ];
  18. window.cancelAnimationFrame = window[ vendors[ x ] + 'CancelAnimationFrame' ] || window[ vendors[ x ] + 'CancelRequestAnimationFrame' ];
  19. }
  20. if ( !window.requestAnimationFrame ) {
  21. window.requestAnimationFrame = function ( callback, element ) {
  22. var currTime = Date.now(), timeToCall = Math.max( 0, 16 - ( currTime - lastTime ) );
  23. var id = window.setTimeout( function() { callback( currTime + timeToCall ); }, timeToCall );
  24. lastTime = currTime + timeToCall;
  25. return id;
  26. };
  27. }
  28. if ( !window.cancelAnimationFrame ) {
  29. window.cancelAnimationFrame = function ( id ) { clearTimeout( id ); };
  30. }
  31. }() );
  32. /**
  33. * @author mr.doob / http://mrdoob.com/
  34. */
  35. THREE.Color = function ( hex ) {
  36. if ( hex !== undefined ) this.setHex( hex );
  37. return this;
  38. };
  39. THREE.Color.prototype = {
  40. constructor: THREE.Color,
  41. r: 1, g: 1, b: 1,
  42. copy: function ( color ) {
  43. this.r = color.r;
  44. this.g = color.g;
  45. this.b = color.b;
  46. return this;
  47. },
  48. copyGammaToLinear: function ( color ) {
  49. this.r = color.r * color.r;
  50. this.g = color.g * color.g;
  51. this.b = color.b * color.b;
  52. return this;
  53. },
  54. copyLinearToGamma: function ( color ) {
  55. this.r = Math.sqrt( color.r );
  56. this.g = Math.sqrt( color.g );
  57. this.b = Math.sqrt( color.b );
  58. return this;
  59. },
  60. convertGammaToLinear: function () {
  61. var r = this.r, g = this.g, b = this.b;
  62. this.r = r * r;
  63. this.g = g * g;
  64. this.b = b * b;
  65. return this;
  66. },
  67. convertLinearToGamma: function () {
  68. this.r = Math.sqrt( this.r );
  69. this.g = Math.sqrt( this.g );
  70. this.b = Math.sqrt( this.b );
  71. return this;
  72. },
  73. setRGB: function ( r, g, b ) {
  74. this.r = r;
  75. this.g = g;
  76. this.b = b;
  77. return this;
  78. },
  79. setHSV: function ( h, s, v ) {
  80. // based on MochiKit implementation by Bob Ippolito
  81. // h,s,v ranges are < 0.0 - 1.0 >
  82. var i, f, p, q, t;
  83. if ( v === 0 ) {
  84. this.r = this.g = this.b = 0;
  85. } else {
  86. i = Math.floor( h * 6 );
  87. f = ( h * 6 ) - i;
  88. p = v * ( 1 - s );
  89. q = v * ( 1 - ( s * f ) );
  90. t = v * ( 1 - ( s * ( 1 - f ) ) );
  91. switch ( i ) {
  92. case 1: this.r = q; this.g = v; this.b = p; break;
  93. case 2: this.r = p; this.g = v; this.b = t; break;
  94. case 3: this.r = p; this.g = q; this.b = v; break;
  95. case 4: this.r = t; this.g = p; this.b = v; break;
  96. case 5: this.r = v; this.g = p; this.b = q; break;
  97. case 6: // fall through
  98. case 0: this.r = v; this.g = t; this.b = p; break;
  99. }
  100. }
  101. return this;
  102. },
  103. setHex: function ( hex ) {
  104. hex = Math.floor( hex );
  105. this.r = ( hex >> 16 & 255 ) / 255;
  106. this.g = ( hex >> 8 & 255 ) / 255;
  107. this.b = ( hex & 255 ) / 255;
  108. return this;
  109. },
  110. lerpSelf: function ( color, alpha ) {
  111. this.r += ( color.r - this.r ) * alpha;
  112. this.g += ( color.g - this.g ) * alpha;
  113. this.b += ( color.b - this.b ) * alpha;
  114. return this;
  115. },
  116. getHex: function () {
  117. return Math.floor( this.r * 255 ) << 16 ^ Math.floor( this.g * 255 ) << 8 ^ Math.floor( this.b * 255 );
  118. },
  119. getContextStyle: function () {
  120. return 'rgb(' + Math.floor( this.r * 255 ) + ',' + Math.floor( this.g * 255 ) + ',' + Math.floor( this.b * 255 ) + ')';
  121. },
  122. clone: function () {
  123. return new THREE.Color().setRGB( this.r, this.g, this.b );
  124. }
  125. };
  126. /**
  127. * @author mr.doob / http://mrdoob.com/
  128. * @author philogb / http://blog.thejit.org/
  129. * @author egraether / http://egraether.com/
  130. * @author zz85 / http://www.lab4games.net/zz85/blog
  131. */
  132. THREE.Vector2 = function ( x, y ) {
  133. this.x = x || 0;
  134. this.y = y || 0;
  135. };
  136. THREE.Vector2.prototype = {
  137. constructor: THREE.Vector2,
  138. set: function ( x, y ) {
  139. this.x = x;
  140. this.y = y;
  141. return this;
  142. },
  143. copy: function ( v ) {
  144. this.x = v.x;
  145. this.y = v.y;
  146. return this;
  147. },
  148. add: function ( a, b ) {
  149. this.x = a.x + b.x;
  150. this.y = a.y + b.y;
  151. return this;
  152. },
  153. addSelf: function ( v ) {
  154. this.x += v.x;
  155. this.y += v.y;
  156. return this;
  157. },
  158. sub: function ( a, b ) {
  159. this.x = a.x - b.x;
  160. this.y = a.y - b.y;
  161. return this;
  162. },
  163. subSelf: function ( v ) {
  164. this.x -= v.x;
  165. this.y -= v.y;
  166. return this;
  167. },
  168. multiplyScalar: function ( s ) {
  169. this.x *= s;
  170. this.y *= s;
  171. return this;
  172. },
  173. divideScalar: function ( s ) {
  174. if ( s ) {
  175. this.x /= s;
  176. this.y /= s;
  177. } else {
  178. this.set( 0, 0 );
  179. }
  180. return this;
  181. },
  182. negate: function() {
  183. return this.multiplyScalar( - 1 );
  184. },
  185. dot: function ( v ) {
  186. return this.x * v.x + this.y * v.y;
  187. },
  188. lengthSq: function () {
  189. return this.x * this.x + this.y * this.y;
  190. },
  191. length: function () {
  192. return Math.sqrt( this.lengthSq() );
  193. },
  194. normalize: function () {
  195. return this.divideScalar( this.length() );
  196. },
  197. distanceTo: function ( v ) {
  198. return Math.sqrt( this.distanceToSquared( v ) );
  199. },
  200. distanceToSquared: function ( v ) {
  201. var dx = this.x - v.x, dy = this.y - v.y;
  202. return dx * dx + dy * dy;
  203. },
  204. setLength: function ( l ) {
  205. return this.normalize().multiplyScalar( l );
  206. },
  207. lerpSelf: function ( v, alpha ) {
  208. this.x += ( v.x - this.x ) * alpha;
  209. this.y += ( v.y - this.y ) * alpha;
  210. return this;
  211. },
  212. equals: function( v ) {
  213. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  214. },
  215. isZero: function () {
  216. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  217. },
  218. clone: function () {
  219. return new THREE.Vector2( this.x, this.y );
  220. }
  221. };
  222. /**
  223. * @author mr.doob / http://mrdoob.com/
  224. * @author kile / http://kile.stravaganza.org/
  225. * @author philogb / http://blog.thejit.org/
  226. * @author mikael emtinger / http://gomo.se/
  227. * @author egraether / http://egraether.com/
  228. */
  229. THREE.Vector3 = function ( x, y, z ) {
  230. this.x = x || 0;
  231. this.y = y || 0;
  232. this.z = z || 0;
  233. };
  234. THREE.Vector3.prototype = {
  235. constructor: THREE.Vector3,
  236. set: function ( x, y, z ) {
  237. this.x = x;
  238. this.y = y;
  239. this.z = z;
  240. return this;
  241. },
  242. setX: function ( x ) {
  243. this.x = x;
  244. return this;
  245. },
  246. setY: function ( y ) {
  247. this.y = y;
  248. return this;
  249. },
  250. setZ: function ( z ) {
  251. this.z = z;
  252. return this;
  253. },
  254. copy: function ( v ) {
  255. this.x = v.x;
  256. this.y = v.y;
  257. this.z = v.z;
  258. return this;
  259. },
  260. add: function ( a, b ) {
  261. this.x = a.x + b.x;
  262. this.y = a.y + b.y;
  263. this.z = a.z + b.z;
  264. return this;
  265. },
  266. addSelf: function ( v ) {
  267. this.x += v.x;
  268. this.y += v.y;
  269. this.z += v.z;
  270. return this;
  271. },
  272. addScalar: function ( s ) {
  273. this.x += s;
  274. this.y += s;
  275. this.z += s;
  276. return this;
  277. },
  278. sub: function ( a, b ) {
  279. this.x = a.x - b.x;
  280. this.y = a.y - b.y;
  281. this.z = a.z - b.z;
  282. return this;
  283. },
  284. subSelf: function ( v ) {
  285. this.x -= v.x;
  286. this.y -= v.y;
  287. this.z -= v.z;
  288. return this;
  289. },
  290. multiply: function ( a, b ) {
  291. this.x = a.x * b.x;
  292. this.y = a.y * b.y;
  293. this.z = a.z * b.z;
  294. return this;
  295. },
  296. multiplySelf: function ( v ) {
  297. this.x *= v.x;
  298. this.y *= v.y;
  299. this.z *= v.z;
  300. return this;
  301. },
  302. multiplyScalar: function ( s ) {
  303. this.x *= s;
  304. this.y *= s;
  305. this.z *= s;
  306. return this;
  307. },
  308. divideSelf: function ( v ) {
  309. this.x /= v.x;
  310. this.y /= v.y;
  311. this.z /= v.z;
  312. return this;
  313. },
  314. divideScalar: function ( s ) {
  315. if ( s ) {
  316. this.x /= s;
  317. this.y /= s;
  318. this.z /= s;
  319. } else {
  320. this.x = 0;
  321. this.y = 0;
  322. this.z = 0;
  323. }
  324. return this;
  325. },
  326. negate: function() {
  327. return this.multiplyScalar( - 1 );
  328. },
  329. dot: function ( v ) {
  330. return this.x * v.x + this.y * v.y + this.z * v.z;
  331. },
  332. lengthSq: function () {
  333. return this.x * this.x + this.y * this.y + this.z * this.z;
  334. },
  335. length: function () {
  336. return Math.sqrt( this.lengthSq() );
  337. },
  338. lengthManhattan: function () {
  339. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  340. },
  341. normalize: function () {
  342. return this.divideScalar( this.length() );
  343. },
  344. setLength: function ( l ) {
  345. return this.normalize().multiplyScalar( l );
  346. },
  347. lerpSelf: function ( v, alpha ) {
  348. this.x += ( v.x - this.x ) * alpha;
  349. this.y += ( v.y - this.y ) * alpha;
  350. this.z += ( v.z - this.z ) * alpha;
  351. return this;
  352. },
  353. cross: function ( a, b ) {
  354. this.x = a.y * b.z - a.z * b.y;
  355. this.y = a.z * b.x - a.x * b.z;
  356. this.z = a.x * b.y - a.y * b.x;
  357. return this;
  358. },
  359. crossSelf: function ( v ) {
  360. var x = this.x, y = this.y, z = this.z;
  361. this.x = y * v.z - z * v.y;
  362. this.y = z * v.x - x * v.z;
  363. this.z = x * v.y - y * v.x;
  364. return this;
  365. },
  366. distanceTo: function ( v ) {
  367. return Math.sqrt( this.distanceToSquared( v ) );
  368. },
  369. distanceToSquared: function ( v ) {
  370. return new THREE.Vector3().sub( this, v ).lengthSq();
  371. },
  372. getPositionFromMatrix: function ( m ) {
  373. this.x = m.elements[12];
  374. this.y = m.elements[13];
  375. this.z = m.elements[14];
  376. return this;
  377. },
  378. getRotationFromMatrix: function ( m, scale ) {
  379. var sx = scale ? scale.x : 1;
  380. var sy = scale ? scale.y : 1;
  381. var sz = scale ? scale.z : 1;
  382. var m11 = m.elements[0] / sx, m12 = m.elements[4] / sy, m13 = m.elements[8] / sz;
  383. var m21 = m.elements[1] / sx, m22 = m.elements[5] / sy, m23 = m.elements[9] / sz;
  384. var m33 = m.elements[10] / sz;
  385. this.y = Math.asin( m13 );
  386. var cosY = Math.cos( this.y );
  387. if ( Math.abs( cosY ) > 0.00001 ) {
  388. this.x = Math.atan2( - m23 / cosY, m33 / cosY );
  389. this.z = Math.atan2( - m12 / cosY, m11 / cosY );
  390. } else {
  391. this.x = 0;
  392. this.z = Math.atan2( m21, m22 );
  393. }
  394. return this;
  395. },
  396. /*
  397. // from http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  398. // order XYZ
  399. getEulerXYZFromQuaternion: function ( q ) {
  400. 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 ) );
  401. this.y = Math.asin( 2 * ( q.x * q.z + q.y * q.w ) );
  402. 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 ) );
  403. },
  404. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
  405. // order YZX (assuming heading == y, attitude == z, bank == x)
  406. getEulerYZXFromQuaternion: function ( q ) {
  407. var sqw = q.w * q.w;
  408. var sqx = q.x * q.x;
  409. var sqy = q.y * q.y;
  410. var sqz = q.z * q.z;
  411. var unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
  412. var test = q.x * q.y + q.z * q.w;
  413. if ( test > 0.499 * unit ) { // singularity at north pole
  414. this.y = 2 * Math.atan2( q.x, q.w );
  415. this.z = Math.PI / 2;
  416. this.x = 0;
  417. return;
  418. }
  419. if ( test < -0.499 * unit ) { // singularity at south pole
  420. this.y = -2 * Math.atan2( q.x, q.w );
  421. this.z = -Math.PI / 2;
  422. this.x = 0;
  423. return;
  424. }
  425. this.y = Math.atan2( 2 * q.y * q.w - 2 * q.x * q.z, sqx - sqy - sqz + sqw );
  426. this.z = Math.asin( 2 * test / unit );
  427. this.x = Math.atan2( 2 * q.x * q.w - 2 * q.y * q.z, -sqx + sqy - sqz + sqw );
  428. },
  429. */
  430. getScaleFromMatrix: function ( m ) {
  431. var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  432. var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  433. var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  434. this.x = sx;
  435. this.y = sy;
  436. this.z = sz;
  437. },
  438. equals: function ( v ) {
  439. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  440. },
  441. isZero: function () {
  442. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  443. },
  444. clone: function () {
  445. return new THREE.Vector3( this.x, this.y, this.z );
  446. }
  447. };
  448. /**
  449. * @author supereggbert / http://www.paulbrunt.co.uk/
  450. * @author philogb / http://blog.thejit.org/
  451. * @author mikael emtinger / http://gomo.se/
  452. * @author egraether / http://egraether.com/
  453. */
  454. THREE.Vector4 = function ( x, y, z, w ) {
  455. this.x = x || 0;
  456. this.y = y || 0;
  457. this.z = z || 0;
  458. this.w = ( w !== undefined ) ? w : 1;
  459. };
  460. THREE.Vector4.prototype = {
  461. constructor: THREE.Vector4,
  462. set: function ( x, y, z, w ) {
  463. this.x = x;
  464. this.y = y;
  465. this.z = z;
  466. this.w = w;
  467. return this;
  468. },
  469. copy: function ( v ) {
  470. this.x = v.x;
  471. this.y = v.y;
  472. this.z = v.z;
  473. this.w = ( v.w !== undefined ) ? v.w : 1;
  474. return this;
  475. },
  476. add: function ( a, b ) {
  477. this.x = a.x + b.x;
  478. this.y = a.y + b.y;
  479. this.z = a.z + b.z;
  480. this.w = a.w + b.w;
  481. return this;
  482. },
  483. addSelf: function ( v ) {
  484. this.x += v.x;
  485. this.y += v.y;
  486. this.z += v.z;
  487. this.w += v.w;
  488. return this;
  489. },
  490. sub: function ( a, b ) {
  491. this.x = a.x - b.x;
  492. this.y = a.y - b.y;
  493. this.z = a.z - b.z;
  494. this.w = a.w - b.w;
  495. return this;
  496. },
  497. subSelf: function ( v ) {
  498. this.x -= v.x;
  499. this.y -= v.y;
  500. this.z -= v.z;
  501. this.w -= v.w;
  502. return this;
  503. },
  504. multiplyScalar: function ( s ) {
  505. this.x *= s;
  506. this.y *= s;
  507. this.z *= s;
  508. this.w *= s;
  509. return this;
  510. },
  511. divideScalar: function ( s ) {
  512. if ( s ) {
  513. this.x /= s;
  514. this.y /= s;
  515. this.z /= s;
  516. this.w /= s;
  517. } else {
  518. this.x = 0;
  519. this.y = 0;
  520. this.z = 0;
  521. this.w = 1;
  522. }
  523. return this;
  524. },
  525. negate: function() {
  526. return this.multiplyScalar( -1 );
  527. },
  528. dot: function ( v ) {
  529. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  530. },
  531. lengthSq: function () {
  532. return this.dot( this );
  533. },
  534. length: function () {
  535. return Math.sqrt( this.lengthSq() );
  536. },
  537. normalize: function () {
  538. return this.divideScalar( this.length() );
  539. },
  540. setLength: function ( l ) {
  541. return this.normalize().multiplyScalar( l );
  542. },
  543. lerpSelf: function ( v, alpha ) {
  544. this.x += ( v.x - this.x ) * alpha;
  545. this.y += ( v.y - this.y ) * alpha;
  546. this.z += ( v.z - this.z ) * alpha;
  547. this.w += ( v.w - this.w ) * alpha;
  548. return this;
  549. },
  550. clone: function () {
  551. return new THREE.Vector4( this.x, this.y, this.z, this.w );
  552. }
  553. };
  554. /**
  555. * @author mrdoob / http://mrdoob.com/
  556. * @author alteredq / http://alteredqualia.com/
  557. */
  558. THREE.Frustum = function ( ) {
  559. this.planes = [
  560. new THREE.Vector4(),
  561. new THREE.Vector4(),
  562. new THREE.Vector4(),
  563. new THREE.Vector4(),
  564. new THREE.Vector4(),
  565. new THREE.Vector4()
  566. ];
  567. };
  568. THREE.Frustum.prototype.setFromMatrix = function ( m ) {
  569. var i, plane,
  570. planes = this.planes;
  571. var me = m.elements;
  572. var me0 = me[0], me1 = me[1], me2 = me[2], me3 = me[3];
  573. var me4 = me[4], me5 = me[5], me6 = me[6], me7 = me[7];
  574. var me8 = me[8], me9 = me[9], me10 = me[10], me11 = me[11];
  575. var me12 = me[12], me13 = me[13], me14 = me[14], me15 = me[15];
  576. planes[ 0 ].set( me3 - me0, me7 - me4, me11 - me8, me15 - me12 );
  577. planes[ 1 ].set( me3 + me0, me7 + me4, me11 + me8, me15 + me12 );
  578. planes[ 2 ].set( me3 + me1, me7 + me5, me11 + me9, me15 + me13 );
  579. planes[ 3 ].set( me3 - me1, me7 - me5, me11 - me9, me15 - me13 );
  580. planes[ 4 ].set( me3 - me2, me7 - me6, me11 - me10, me15 - me14 );
  581. planes[ 5 ].set( me3 + me2, me7 + me6, me11 + me10, me15 + me14 );
  582. for ( i = 0; i < 6; i ++ ) {
  583. plane = planes[ i ];
  584. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  585. }
  586. };
  587. THREE.Frustum.prototype.contains = function ( object ) {
  588. var distance,
  589. planes = this.planes,
  590. matrix = object.matrixWorld,
  591. me = matrix.elements,
  592. scale = THREE.Frustum.__v1.set( matrix.getColumnX().length(), matrix.getColumnY().length(), matrix.getColumnZ().length() ),
  593. radius = - object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) );
  594. for ( var i = 0; i < 6; i ++ ) {
  595. distance = planes[ i ].x * me[12] + planes[ i ].y * me[13] + planes[ i ].z * me[14] + planes[ i ].w;
  596. if ( distance <= radius ) return false;
  597. }
  598. return true;
  599. };
  600. THREE.Frustum.__v1 = new THREE.Vector3();
  601. /**
  602. * @author mr.doob / http://mrdoob.com/
  603. */
  604. THREE.Ray = function ( origin, direction ) {
  605. this.origin = origin || new THREE.Vector3();
  606. this.direction = direction || new THREE.Vector3();
  607. var precision = 0.0001;
  608. this.setPrecision = function ( value ) {
  609. precision = value;
  610. };
  611. var a = new THREE.Vector3();
  612. var b = new THREE.Vector3();
  613. var c = new THREE.Vector3();
  614. var d = new THREE.Vector3();
  615. var originCopy = new THREE.Vector3();
  616. var directionCopy = new THREE.Vector3();
  617. var vector = new THREE.Vector3();
  618. var normal = new THREE.Vector3();
  619. var intersectPoint = new THREE.Vector3()
  620. this.intersectObject = function ( object ) {
  621. var intersect, intersects = [];
  622. if ( object instanceof THREE.Particle ) {
  623. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  624. if ( distance > object.scale.x ) {
  625. return [];
  626. }
  627. intersect = {
  628. distance: distance,
  629. point: object.position,
  630. face: null,
  631. object: object
  632. };
  633. intersects.push( intersect );
  634. } else if ( object instanceof THREE.Mesh ) {
  635. // Checking boundingSphere
  636. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  637. var scale = THREE.Frustum.__v1.set( object.matrixWorld.getColumnX().length(), object.matrixWorld.getColumnY().length(), object.matrixWorld.getColumnZ().length() );
  638. if ( distance > object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) ) ) {
  639. return intersects;
  640. }
  641. // Checking faces
  642. var f, fl, face, dot, scalar,
  643. geometry = object.geometry,
  644. vertices = geometry.vertices,
  645. objMatrix;
  646. object.matrixRotationWorld.extractRotation( object.matrixWorld );
  647. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  648. face = geometry.faces[ f ];
  649. originCopy.copy( this.origin );
  650. directionCopy.copy( this.direction );
  651. objMatrix = object.matrixWorld;
  652. // determine if ray intersects the plane of the face
  653. // note: this works regardless of the direction of the face normal
  654. vector = objMatrix.multiplyVector3( vector.copy( face.centroid ) ).subSelf( originCopy );
  655. normal = object.matrixRotationWorld.multiplyVector3( normal.copy( face.normal ) );
  656. dot = directionCopy.dot( normal );
  657. // bail if ray and plane are parallel
  658. if ( Math.abs( dot ) < precision ) continue;
  659. // calc distance to plane
  660. scalar = normal.dot( vector ) / dot;
  661. // if negative distance, then plane is behind ray
  662. if ( scalar < 0 ) continue;
  663. if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) {
  664. intersectPoint.add( originCopy, directionCopy.multiplyScalar( scalar ) );
  665. if ( face instanceof THREE.Face3 ) {
  666. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ] ) );
  667. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ] ) );
  668. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ] ) );
  669. if ( pointInFace3( intersectPoint, a, b, c ) ) {
  670. intersect = {
  671. distance: originCopy.distanceTo( intersectPoint ),
  672. point: intersectPoint.clone(),
  673. face: face,
  674. object: object
  675. };
  676. intersects.push( intersect );
  677. }
  678. } else if ( face instanceof THREE.Face4 ) {
  679. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ] ) );
  680. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ] ) );
  681. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ] ) );
  682. d = objMatrix.multiplyVector3( d.copy( vertices[ face.d ] ) );
  683. if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
  684. intersect = {
  685. distance: originCopy.distanceTo( intersectPoint ),
  686. point: intersectPoint.clone(),
  687. face: face,
  688. object: object
  689. };
  690. intersects.push( intersect );
  691. }
  692. }
  693. }
  694. }
  695. }
  696. return intersects;
  697. }
  698. this.intersectObjects = function ( objects ) {
  699. var intersects = [];
  700. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  701. Array.prototype.push.apply( intersects, this.intersectObject( objects[ i ] ) );
  702. }
  703. intersects.sort( function ( a, b ) { return a.distance - b.distance; } );
  704. return intersects;
  705. };
  706. var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
  707. var dot, intersect, distance;
  708. function distanceFromIntersection( origin, direction, position ) {
  709. v0.sub( position, origin );
  710. dot = v0.dot( direction );
  711. intersect = v1.add( origin, v2.copy( direction ).multiplyScalar( dot ) );
  712. distance = position.distanceTo( intersect );
  713. return distance;
  714. }
  715. // http://www.blackpawn.com/texts/pointinpoly/default.html
  716. var dot00, dot01, dot02, dot11, dot12, invDenom, u, v;
  717. function pointInFace3( p, a, b, c ) {
  718. v0.sub( c, a );
  719. v1.sub( b, a );
  720. v2.sub( p, a );
  721. dot00 = v0.dot( v0 );
  722. dot01 = v0.dot( v1 );
  723. dot02 = v0.dot( v2 );
  724. dot11 = v1.dot( v1 );
  725. dot12 = v1.dot( v2 );
  726. invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 );
  727. u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  728. v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  729. return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 );
  730. }
  731. };
  732. /**
  733. * @author mr.doob / http://mrdoob.com/
  734. */
  735. THREE.Rectangle = function () {
  736. var _left, _top, _right, _bottom,
  737. _width, _height, _isEmpty = true;
  738. function resize() {
  739. _width = _right - _left;
  740. _height = _bottom - _top;
  741. }
  742. this.getX = function () {
  743. return _left;
  744. };
  745. this.getY = function () {
  746. return _top;
  747. };
  748. this.getWidth = function () {
  749. return _width;
  750. };
  751. this.getHeight = function () {
  752. return _height;
  753. };
  754. this.getLeft = function() {
  755. return _left;
  756. };
  757. this.getTop = function() {
  758. return _top;
  759. };
  760. this.getRight = function() {
  761. return _right;
  762. };
  763. this.getBottom = function() {
  764. return _bottom;
  765. };
  766. this.set = function ( left, top, right, bottom ) {
  767. _isEmpty = false;
  768. _left = left; _top = top;
  769. _right = right; _bottom = bottom;
  770. resize();
  771. };
  772. this.addPoint = function ( x, y ) {
  773. if ( _isEmpty ) {
  774. _isEmpty = false;
  775. _left = x; _top = y;
  776. _right = x; _bottom = y;
  777. resize();
  778. } else {
  779. _left = _left < x ? _left : x; // Math.min( _left, x );
  780. _top = _top < y ? _top : y; // Math.min( _top, y );
  781. _right = _right > x ? _right : x; // Math.max( _right, x );
  782. _bottom = _bottom > y ? _bottom : y; // Math.max( _bottom, y );
  783. resize();
  784. }
  785. };
  786. this.add3Points = function ( x1, y1, x2, y2, x3, y3 ) {
  787. if (_isEmpty) {
  788. _isEmpty = false;
  789. _left = x1 < x2 ? ( x1 < x3 ? x1 : x3 ) : ( x2 < x3 ? x2 : x3 );
  790. _top = y1 < y2 ? ( y1 < y3 ? y1 : y3 ) : ( y2 < y3 ? y2 : y3 );
  791. _right = x1 > x2 ? ( x1 > x3 ? x1 : x3 ) : ( x2 > x3 ? x2 : x3 );
  792. _bottom = y1 > y2 ? ( y1 > y3 ? y1 : y3 ) : ( y2 > y3 ? y2 : y3 );
  793. resize();
  794. } else {
  795. _left = x1 < x2 ? ( x1 < x3 ? ( x1 < _left ? x1 : _left ) : ( x3 < _left ? x3 : _left ) ) : ( x2 < x3 ? ( x2 < _left ? x2 : _left ) : ( x3 < _left ? x3 : _left ) );
  796. _top = y1 < y2 ? ( y1 < y3 ? ( y1 < _top ? y1 : _top ) : ( y3 < _top ? y3 : _top ) ) : ( y2 < y3 ? ( y2 < _top ? y2 : _top ) : ( y3 < _top ? y3 : _top ) );
  797. _right = x1 > x2 ? ( x1 > x3 ? ( x1 > _right ? x1 : _right ) : ( x3 > _right ? x3 : _right ) ) : ( x2 > x3 ? ( x2 > _right ? x2 : _right ) : ( x3 > _right ? x3 : _right ) );
  798. _bottom = y1 > y2 ? ( y1 > y3 ? ( y1 > _bottom ? y1 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) ) : ( y2 > y3 ? ( y2 > _bottom ? y2 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) );
  799. resize();
  800. };
  801. };
  802. this.addRectangle = function ( r ) {
  803. if ( _isEmpty ) {
  804. _isEmpty = false;
  805. _left = r.getLeft(); _top = r.getTop();
  806. _right = r.getRight(); _bottom = r.getBottom();
  807. resize();
  808. } else {
  809. _left = _left < r.getLeft() ? _left : r.getLeft(); // Math.min(_left, r.getLeft() );
  810. _top = _top < r.getTop() ? _top : r.getTop(); // Math.min(_top, r.getTop() );
  811. _right = _right > r.getRight() ? _right : r.getRight(); // Math.max(_right, r.getRight() );
  812. _bottom = _bottom > r.getBottom() ? _bottom : r.getBottom(); // Math.max(_bottom, r.getBottom() );
  813. resize();
  814. }
  815. };
  816. this.inflate = function ( v ) {
  817. _left -= v; _top -= v;
  818. _right += v; _bottom += v;
  819. resize();
  820. };
  821. this.minSelf = function ( r ) {
  822. _left = _left > r.getLeft() ? _left : r.getLeft(); // Math.max( _left, r.getLeft() );
  823. _top = _top > r.getTop() ? _top : r.getTop(); // Math.max( _top, r.getTop() );
  824. _right = _right < r.getRight() ? _right : r.getRight(); // Math.min( _right, r.getRight() );
  825. _bottom = _bottom < r.getBottom() ? _bottom : r.getBottom(); // Math.min( _bottom, r.getBottom() );
  826. resize();
  827. };
  828. this.intersects = function ( r ) {
  829. // http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/
  830. if ( _right < r.getLeft() ) return false;
  831. if ( _left > r.getRight() ) return false;
  832. if ( _bottom < r.getTop() ) return false;
  833. if ( _top > r.getBottom() ) return false;
  834. return true;
  835. };
  836. this.empty = function () {
  837. _isEmpty = true;
  838. _left = 0; _top = 0;
  839. _right = 0; _bottom = 0;
  840. resize();
  841. };
  842. this.isEmpty = function () {
  843. return _isEmpty;
  844. };
  845. };
  846. /**
  847. * @author alteredq / http://alteredqualia.com/
  848. */
  849. THREE.Math = {
  850. // Clamp value to range <a, b>
  851. clamp: function ( x, a, b ) {
  852. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  853. },
  854. // Clamp value to range <a, inf)
  855. clampBottom: function ( x, a ) {
  856. return x < a ? a : x;
  857. },
  858. // Linear mapping from range <a1, a2> to range <b1, b2>
  859. mapLinear: function ( x, a1, a2, b1, b2 ) {
  860. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  861. },
  862. // Random float from <0, 1> with 16 bits of randomness
  863. // (standard Math.random() creates repetitive patterns when applied over larger space)
  864. random16: function () {
  865. return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
  866. },
  867. // Random integer from <low, high> interval
  868. randInt: function ( low, high ) {
  869. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  870. },
  871. // Random float from <low, high> interval
  872. randFloat: function ( low, high ) {
  873. return low + Math.random() * ( high - low );
  874. },
  875. // Random float from <-range/2, range/2> interval
  876. randFloatSpread: function ( range ) {
  877. return range * ( 0.5 - Math.random() );
  878. },
  879. sign: function ( x ) {
  880. return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
  881. }
  882. };
  883. /**
  884. * @author alteredq / http://alteredqualia.com/
  885. */
  886. THREE.Matrix3 = function () {
  887. this.elements = new Float32Array(9);
  888. };
  889. THREE.Matrix3.prototype = {
  890. constructor: THREE.Matrix3,
  891. getInverse: function ( matrix ) {
  892. // input: THREE.Matrix4
  893. // ( based on http://code.google.com/p/webgl-mjs/ )
  894. var me = matrix.elements;
  895. var a11 = me[10] * me[5] - me[6] * me[9];
  896. var a21 = - me[10] * me[1] + me[2] * me[9];
  897. var a31 = me[6] * me[1] - me[2] * me[5];
  898. var a12 = - me[10] * me[4] + me[6] * me[8];
  899. var a22 = me[10] * me[0] - me[2] * me[8];
  900. var a32 = - me[6] * me[0] + me[2] * me[4];
  901. var a13 = me[9] * me[4] - me[5] * me[8];
  902. var a23 = - me[9] * me[0] + me[1] * me[8];
  903. var a33 = me[5] * me[0] - me[1] * me[4];
  904. var det = me[0] * a11 + me[1] * a12 + me[2] * a13;
  905. // no inverse
  906. if ( det === 0 ) {
  907. console.warn( "Matrix3.getInverse(): determinant == 0" );
  908. }
  909. var idet = 1.0 / det;
  910. var m = this.elements;
  911. m[ 0 ] = idet * a11; m[ 1 ] = idet * a21; m[ 2 ] = idet * a31;
  912. m[ 3 ] = idet * a12; m[ 4 ] = idet * a22; m[ 5 ] = idet * a32;
  913. m[ 6 ] = idet * a13; m[ 7 ] = idet * a23; m[ 8 ] = idet * a33;
  914. return this;
  915. },
  916. transpose: function () {
  917. var tmp, m = this.elements;
  918. tmp = m[1]; m[1] = m[3]; m[3] = tmp;
  919. tmp = m[2]; m[2] = m[6]; m[6] = tmp;
  920. tmp = m[5]; m[5] = m[7]; m[7] = tmp;
  921. return this;
  922. },
  923. transposeIntoArray: function ( r ) {
  924. var m = this.m;
  925. r[ 0 ] = m[ 0 ];
  926. r[ 1 ] = m[ 3 ];
  927. r[ 2 ] = m[ 6 ];
  928. r[ 3 ] = m[ 1 ];
  929. r[ 4 ] = m[ 4 ];
  930. r[ 5 ] = m[ 7 ];
  931. r[ 6 ] = m[ 2 ];
  932. r[ 7 ] = m[ 5 ];
  933. r[ 8 ] = m[ 8 ];
  934. return this;
  935. }
  936. };
  937. /**
  938. * @author mr.doob / http://mrdoob.com/
  939. * @author supereggbert / http://www.paulbrunt.co.uk/
  940. * @author philogb / http://blog.thejit.org/
  941. * @author jordi_ros / http://plattsoft.com
  942. * @author D1plo1d / http://github.com/D1plo1d
  943. * @author alteredq / http://alteredqualia.com/
  944. * @author mikael emtinger / http://gomo.se/
  945. * @author timknip / http://www.floorplanner.com/
  946. */
  947. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  948. this.elements = new Float32Array(16);
  949. this.set(
  950. ( n11 !== undefined ) ? n11 : 1, n12 || 0, n13 || 0, n14 || 0,
  951. n21 || 0, ( n22 !== undefined ) ? n22 : 1, n23 || 0, n24 || 0,
  952. n31 || 0, n32 || 0, ( n33 !== undefined ) ? n33 : 1, n34 || 0,
  953. n41 || 0, n42 || 0, n43 || 0, ( n44 !== undefined ) ? n44 : 1
  954. );
  955. };
  956. THREE.Matrix4.prototype = {
  957. constructor: THREE.Matrix4,
  958. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  959. var te = this.elements;
  960. te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
  961. te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
  962. te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
  963. te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
  964. return this;
  965. },
  966. identity: function () {
  967. this.set(
  968. 1, 0, 0, 0,
  969. 0, 1, 0, 0,
  970. 0, 0, 1, 0,
  971. 0, 0, 0, 1
  972. );
  973. return this;
  974. },
  975. copy: function ( m ) {
  976. var me = m.elements;
  977. this.set(
  978. me[0], me[4], me[8], me[12],
  979. me[1], me[5], me[9], me[13],
  980. me[2], me[6], me[10], me[14],
  981. me[3], me[7], me[11], me[15]
  982. );
  983. return this;
  984. },
  985. lookAt: function ( eye, target, up ) {
  986. var te = this.elements;
  987. var x = THREE.Matrix4.__v1;
  988. var y = THREE.Matrix4.__v2;
  989. var z = THREE.Matrix4.__v3;
  990. z.sub( eye, target ).normalize();
  991. if ( z.length() === 0 ) {
  992. z.z = 1;
  993. }
  994. x.cross( up, z ).normalize();
  995. if ( x.length() === 0 ) {
  996. z.x += 0.0001;
  997. x.cross( up, z ).normalize();
  998. }
  999. y.cross( z, x );
  1000. te[0] = x.x; te[4] = y.x; te[8] = z.x;
  1001. te[1] = x.y; te[5] = y.y; te[9] = z.y;
  1002. te[2] = x.z; te[6] = y.z; te[10] = z.z;
  1003. return this;
  1004. },
  1005. multiply: function ( a, b ) {
  1006. var ae = a.elements,
  1007. be = b.elements,
  1008. te = this.elements;
  1009. var a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
  1010. var a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
  1011. var a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
  1012. var a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
  1013. var b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
  1014. var b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
  1015. var b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
  1016. var b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
  1017. te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  1018. te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  1019. te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  1020. te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  1021. te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  1022. te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  1023. te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  1024. te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  1025. te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  1026. te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  1027. te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  1028. te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  1029. te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  1030. te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  1031. te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  1032. te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  1033. return this;
  1034. },
  1035. multiplySelf: function ( m ) {
  1036. return this.multiply( this, m );
  1037. },
  1038. multiplyToArray: function ( a, b, r ) {
  1039. var te = this.elements;
  1040. this.multiply( a, b );
  1041. r[ 0 ] = te[0]; r[ 1 ] = te[1]; r[ 2 ] = te[2]; r[ 3 ] = te[3];
  1042. r[ 4 ] = te[4]; r[ 5 ] = te[5]; r[ 6 ] = te[6]; r[ 7 ] = te[7];
  1043. r[ 8 ] = te[8]; r[ 9 ] = te[9]; r[ 10 ] = te[10]; r[ 11 ] = te[11];
  1044. r[ 12 ] = te[12]; r[ 13 ] = te[13]; r[ 14 ] = te[14]; r[ 15 ] = te[15];
  1045. return this;
  1046. },
  1047. multiplyScalar: function ( s ) {
  1048. var te = this.elements;
  1049. te[0] *= s; te[4] *= s; te[8] *= s; te[12] *= s;
  1050. te[1] *= s; te[5] *= s; te[9] *= s; te[13] *= s;
  1051. te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s;
  1052. te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s;
  1053. return this;
  1054. },
  1055. multiplyVector3: function ( v ) {
  1056. var te = this.elements;
  1057. var vx = v.x, vy = v.y, vz = v.z;
  1058. var d = 1 / ( te[3] * vx + te[7] * vy + te[11] * vz + te[15] );
  1059. v.x = ( te[0] * vx + te[4] * vy + te[8] * vz + te[12] ) * d;
  1060. v.y = ( te[1] * vx + te[5] * vy + te[9] * vz + te[13] ) * d;
  1061. v.z = ( te[2] * vx + te[6] * vy + te[10] * vz + te[14] ) * d;
  1062. return v;
  1063. },
  1064. multiplyVector4: function ( v ) {
  1065. var te = this.elements;
  1066. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  1067. v.x = te[0] * vx + te[4] * vy + te[8] * vz + te[12] * vw;
  1068. v.y = te[1] * vx + te[5] * vy + te[9] * vz + te[13] * vw;
  1069. v.z = te[2] * vx + te[6] * vy + te[10] * vz + te[14] * vw;
  1070. v.w = te[3] * vx + te[7] * vy + te[11] * vz + te[15] * vw;
  1071. return v;
  1072. },
  1073. rotateAxis: function ( v ) {
  1074. var te = this.elements;
  1075. var vx = v.x, vy = v.y, vz = v.z;
  1076. v.x = vx * te[0] + vy * te[4] + vz * te[8];
  1077. v.y = vx * te[1] + vy * te[5] + vz * te[9];
  1078. v.z = vx * te[2] + vy * te[6] + vz * te[10];
  1079. v.normalize();
  1080. return v;
  1081. },
  1082. crossVector: function ( a ) {
  1083. var te = this.elements;
  1084. var v = new THREE.Vector4();
  1085. v.x = te[0] * a.x + te[4] * a.y + te[8] * a.z + te[12] * a.w;
  1086. v.y = te[1] * a.x + te[5] * a.y + te[9] * a.z + te[13] * a.w;
  1087. v.z = te[2] * a.x + te[6] * a.y + te[10] * a.z + te[14] * a.w;
  1088. v.w = ( a.w ) ? te[3] * a.x + te[7] * a.y + te[11] * a.z + te[15] * a.w : 1;
  1089. return v;
  1090. },
  1091. determinant: function () {
  1092. var te = this.elements;
  1093. var n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12];
  1094. var n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13];
  1095. var n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14];
  1096. var n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15];
  1097. //TODO: make this more efficient
  1098. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  1099. return (
  1100. n14 * n23 * n32 * n41-
  1101. n13 * n24 * n32 * n41-
  1102. n14 * n22 * n33 * n41+
  1103. n12 * n24 * n33 * n41+
  1104. n13 * n22 * n34 * n41-
  1105. n12 * n23 * n34 * n41-
  1106. n14 * n23 * n31 * n42+
  1107. n13 * n24 * n31 * n42+
  1108. n14 * n21 * n33 * n42-
  1109. n11 * n24 * n33 * n42-
  1110. n13 * n21 * n34 * n42+
  1111. n11 * n23 * n34 * n42+
  1112. n14 * n22 * n31 * n43-
  1113. n12 * n24 * n31 * n43-
  1114. n14 * n21 * n32 * n43+
  1115. n11 * n24 * n32 * n43+
  1116. n12 * n21 * n34 * n43-
  1117. n11 * n22 * n34 * n43-
  1118. n13 * n22 * n31 * n44+
  1119. n12 * n23 * n31 * n44+
  1120. n13 * n21 * n32 * n44-
  1121. n11 * n23 * n32 * n44-
  1122. n12 * n21 * n33 * n44+
  1123. n11 * n22 * n33 * n44
  1124. );
  1125. },
  1126. transpose: function () {
  1127. var te = this.elements;
  1128. var tmp;
  1129. tmp = te[1]; te[1] = te[4]; te[4] = tmp;
  1130. tmp = te[2]; te[2] = te[8]; te[8] = tmp;
  1131. tmp = te[6]; te[6] = te[9]; te[9] = tmp;
  1132. tmp = te[3]; te[3] = te[12]; te[12] = tmp;
  1133. tmp = te[7]; te[7] = te[13]; te[13] = tmp;
  1134. tmp = te[11]; te[11] = te[14]; te[14] = tmp;
  1135. return this;
  1136. },
  1137. flattenToArray: function ( flat ) {
  1138. var te = this.elements;
  1139. flat[ 0 ] = te[0]; flat[ 1 ] = te[1]; flat[ 2 ] = te[2]; flat[ 3 ] = te[3];
  1140. flat[ 4 ] = te[4]; flat[ 5 ] = te[5]; flat[ 6 ] = te[6]; flat[ 7 ] = te[7];
  1141. flat[ 8 ] = te[8]; flat[ 9 ] = te[9]; flat[ 10 ] = te[10]; flat[ 11 ] = te[11];
  1142. flat[ 12 ] = te[12]; flat[ 13 ] = te[13]; flat[ 14 ] = te[14]; flat[ 15 ] = te[15];
  1143. return flat;
  1144. },
  1145. flattenToArrayOffset: function( flat, offset ) {
  1146. var te = this.elements;
  1147. flat[ offset ] = te[0];
  1148. flat[ offset + 1 ] = te[1];
  1149. flat[ offset + 2 ] = te[2];
  1150. flat[ offset + 3 ] = te[3];
  1151. flat[ offset + 4 ] = te[4];
  1152. flat[ offset + 5 ] = te[5];
  1153. flat[ offset + 6 ] = te[6];
  1154. flat[ offset + 7 ] = te[7];
  1155. flat[ offset + 8 ] = te[8];
  1156. flat[ offset + 9 ] = te[9];
  1157. flat[ offset + 10 ] = te[10];
  1158. flat[ offset + 11 ] = te[11];
  1159. flat[ offset + 12 ] = te[12];
  1160. flat[ offset + 13 ] = te[13];
  1161. flat[ offset + 14 ] = te[14];
  1162. flat[ offset + 15 ] = te[15];
  1163. return flat;
  1164. },
  1165. getPosition: function () {
  1166. var te = this.elements;
  1167. return THREE.Matrix4.__v1.set( te[12], te[13], te[14] );
  1168. },
  1169. setPosition: function ( v ) {
  1170. var te = this.elements;
  1171. te[12] = v.x;
  1172. te[13] = v.y;
  1173. te[14] = v.z;
  1174. return this;
  1175. },
  1176. getColumnX: function () {
  1177. var te = this.elements;
  1178. return THREE.Matrix4.__v1.set( te[0], te[1], te[2] );
  1179. },
  1180. getColumnY: function () {
  1181. var te = this.elements;
  1182. return THREE.Matrix4.__v1.set( te[4], te[5], te[6] );
  1183. },
  1184. getColumnZ: function() {
  1185. var te = this.elements;
  1186. return THREE.Matrix4.__v1.set( te[8], te[9], te[10] );
  1187. },
  1188. getInverse: function ( m ) {
  1189. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  1190. var te = this.elements;
  1191. var me = m.elements;
  1192. var n11 = me[0], n12 = me[4], n13 = me[8], n14 = me[12];
  1193. var n21 = me[1], n22 = me[5], n23 = me[9], n24 = me[13];
  1194. var n31 = me[2], n32 = me[6], n33 = me[10], n34 = me[14];
  1195. var n41 = me[3], n42 = me[7], n43 = me[11], n44 = me[15];
  1196. te[0] = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  1197. te[4] = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  1198. te[8] = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  1199. te[12] = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  1200. te[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  1201. te[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  1202. te[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  1203. te[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  1204. te[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  1205. te[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  1206. te[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  1207. te[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  1208. te[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  1209. te[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  1210. te[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  1211. te[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  1212. this.multiplyScalar( 1 / m.determinant() );
  1213. return this;
  1214. },
  1215. setRotationFromEuler: function( v, order ) {
  1216. var te = this.elements;
  1217. var x = v.x, y = v.y, z = v.z;
  1218. var a = Math.cos( x ), b = Math.sin( x );
  1219. var c = Math.cos( y ), d = Math.sin( y );
  1220. var e = Math.cos( z ), f = Math.sin( z );
  1221. switch ( order ) {
  1222. case 'YXZ':
  1223. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  1224. te[0] = ce + df * b;
  1225. te[4] = de * b - cf;
  1226. te[8] = a * d;
  1227. te[1] = a * f;
  1228. te[5] = a * e;
  1229. te[9] = - b;
  1230. te[2] = cf * b - de;
  1231. te[6] = df + ce * b;
  1232. te[10] = a * c;
  1233. break;
  1234. case 'ZXY':
  1235. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  1236. te[0] = ce - df * b;
  1237. te[4] = - a * f;
  1238. te[8] = de + cf * b;
  1239. te[1] = cf + de * b;
  1240. te[5] = a * e;
  1241. te[9] = df - ce * b;
  1242. te[2] = - a * d;
  1243. te[6] = b;
  1244. te[10] = a * c;
  1245. break;
  1246. case 'ZYX':
  1247. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  1248. te[0] = c * e;
  1249. te[4] = be * d - af;
  1250. te[8] = ae * d + bf;
  1251. te[1] = c * f;
  1252. te[5] = bf * d + ae;
  1253. te[9] = af * d - be;
  1254. te[2] = - d;
  1255. te[6] = b * c;
  1256. te[10] = a * c;
  1257. break;
  1258. case 'YZX':
  1259. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  1260. te[0] = c * e;
  1261. te[4] = bd - ac * f;
  1262. te[8] = bc * f + ad;
  1263. te[1] = f;
  1264. te[5] = a * e;
  1265. te[9] = - b * e;
  1266. te[2] = - d * e;
  1267. te[6] = ad * f + bc;
  1268. te[10] = ac - bd * f;
  1269. break;
  1270. case 'XZY':
  1271. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  1272. te[0] = c * e;
  1273. te[4] = - f;
  1274. te[8] = d * e;
  1275. te[1] = ac * f + bd;
  1276. te[5] = a * e;
  1277. te[9] = ad * f - bc;
  1278. te[2] = bc * f - ad;
  1279. te[6] = b * e;
  1280. te[10] = bd * f + ac;
  1281. break;
  1282. default: // 'XYZ'
  1283. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  1284. te[0] = c * e;
  1285. te[4] = - c * f;
  1286. te[8] = d;
  1287. te[1] = af + be * d;
  1288. te[5] = ae - bf * d;
  1289. te[9] = - b * c;
  1290. te[2] = bf - ae * d;
  1291. te[6] = be + af * d;
  1292. te[10] = a * c;
  1293. break;
  1294. }
  1295. return this;
  1296. },
  1297. setRotationFromQuaternion: function( q ) {
  1298. var te = this.elements;
  1299. var x = q.x, y = q.y, z = q.z, w = q.w;
  1300. var x2 = x + x, y2 = y + y, z2 = z + z;
  1301. var xx = x * x2, xy = x * y2, xz = x * z2;
  1302. var yy = y * y2, yz = y * z2, zz = z * z2;
  1303. var wx = w * x2, wy = w * y2, wz = w * z2;
  1304. te[0] = 1 - ( yy + zz );
  1305. te[4] = xy - wz;
  1306. te[8] = xz + wy;
  1307. te[1] = xy + wz;
  1308. te[5] = 1 - ( xx + zz );
  1309. te[9] = yz - wx;
  1310. te[2] = xz - wy;
  1311. te[6] = yz + wx;
  1312. te[10] = 1 - ( xx + yy );
  1313. return this;
  1314. },
  1315. compose: function ( translation, rotation, scale ) {
  1316. var te = this.elements;
  1317. var mRotation = THREE.Matrix4.__m1;
  1318. var mScale = THREE.Matrix4.__m2;
  1319. mRotation.identity();
  1320. mRotation.setRotationFromQuaternion( rotation );
  1321. mScale.makeScale( scale.x, scale.y, scale.z );
  1322. this.multiply( mRotation, mScale );
  1323. te[12] = translation.x;
  1324. te[13] = translation.y;
  1325. te[14] = translation.z;
  1326. return this;
  1327. },
  1328. decompose: function ( translation, rotation, scale ) {
  1329. // grab the axis vectors
  1330. var te = this.elements;
  1331. var x = THREE.Matrix4.__v1;
  1332. var y = THREE.Matrix4.__v2;
  1333. var z = THREE.Matrix4.__v3;
  1334. x.set( te[0], te[1], te[2] );
  1335. y.set( te[4], te[5], te[6] );
  1336. z.set( te[8], te[9], te[10] );
  1337. translation = ( translation instanceof THREE.Vector3 ) ? translation : new THREE.Vector3();
  1338. rotation = ( rotation instanceof THREE.Quaternion ) ? rotation : new THREE.Quaternion();
  1339. scale = ( scale instanceof THREE.Vector3 ) ? scale : new THREE.Vector3();
  1340. scale.x = x.length();
  1341. scale.y = y.length();
  1342. scale.z = z.length();
  1343. translation.x = te[12];
  1344. translation.y = te[13];
  1345. translation.z = te[14];
  1346. // scale the rotation part
  1347. var matrix = THREE.Matrix4.__m1;
  1348. matrix.copy( this );
  1349. matrix.elements[0] /= scale.x;
  1350. matrix.elements[1] /= scale.x;
  1351. matrix.elements[2] /= scale.x;
  1352. matrix.elements[4] /= scale.y;
  1353. matrix.elements[5] /= scale.y;
  1354. matrix.elements[6] /= scale.y;
  1355. matrix.elements[8] /= scale.z;
  1356. matrix.elements[9] /= scale.z;
  1357. matrix.elements[10] /= scale.z;
  1358. rotation.setFromRotationMatrix( matrix );
  1359. return [ translation, rotation, scale ];
  1360. },
  1361. extractPosition: function ( m ) {
  1362. var te = this.elements;
  1363. var me = m.elements;
  1364. te[12] = me[12];
  1365. te[13] = me[13];
  1366. te[14] = me[14];
  1367. return this;
  1368. },
  1369. extractRotation: function ( m ) {
  1370. var te = this.elements;
  1371. var me = m.elements;
  1372. var vector = THREE.Matrix4.__v1;
  1373. var scaleX = 1 / vector.set( me[0], me[1], me[2] ).length();
  1374. var scaleY = 1 / vector.set( me[4], me[5], me[6] ).length();
  1375. var scaleZ = 1 / vector.set( me[8], me[9], me[10] ).length();
  1376. te[0] = me[0] * scaleX;
  1377. te[1] = me[1] * scaleX;
  1378. te[2] = me[2] * scaleX;
  1379. te[4] = me[4] * scaleY;
  1380. te[5] = me[5] * scaleY;
  1381. te[6] = me[6] * scaleY;
  1382. te[8] = me[8] * scaleZ;
  1383. te[9] = me[9] * scaleZ;
  1384. te[10] = me[10] * scaleZ;
  1385. return this;
  1386. },
  1387. //
  1388. translate: function ( v ) {
  1389. var te = this.elements;
  1390. var x = v.x, y = v.y, z = v.z;
  1391. te[12] = te[0] * x + te[4] * y + te[8] * z + te[12];
  1392. te[13] = te[1] * x + te[5] * y + te[9] * z + te[13];
  1393. te[14] = te[2] * x + te[6] * y + te[10] * z + te[14];
  1394. te[15] = te[3] * x + te[7] * y + te[11] * z + te[15];
  1395. return this;
  1396. },
  1397. rotateX: function ( angle ) {
  1398. var te = this.elements;
  1399. var m12 = te[4];
  1400. var m22 = te[5];
  1401. var m32 = te[6];
  1402. var m42 = te[7];
  1403. var m13 = te[8];
  1404. var m23 = te[9];
  1405. var m33 = te[10];
  1406. var m43 = te[11];
  1407. var c = Math.cos( angle );
  1408. var s = Math.sin( angle );
  1409. te[4] = c * m12 + s * m13;
  1410. te[5] = c * m22 + s * m23;
  1411. te[6] = c * m32 + s * m33;
  1412. te[7] = c * m42 + s * m43;
  1413. te[8] = c * m13 - s * m12;
  1414. te[9] = c * m23 - s * m22;
  1415. te[10] = c * m33 - s * m32;
  1416. te[11] = c * m43 - s * m42;
  1417. return this;
  1418. },
  1419. rotateY: function ( angle ) {
  1420. var te = this.elements;
  1421. var m11 = te[0];
  1422. var m21 = te[1];
  1423. var m31 = te[2];
  1424. var m41 = te[3];
  1425. var m13 = te[8];
  1426. var m23 = te[9];
  1427. var m33 = te[10];
  1428. var m43 = te[11];
  1429. var c = Math.cos( angle );
  1430. var s = Math.sin( angle );
  1431. te[0] = c * m11 - s * m13;
  1432. te[1] = c * m21 - s * m23;
  1433. te[2] = c * m31 - s * m33;
  1434. te[3] = c * m41 - s * m43;
  1435. te[8] = c * m13 + s * m11;
  1436. te[9] = c * m23 + s * m21;
  1437. te[10] = c * m33 + s * m31;
  1438. te[11] = c * m43 + s * m41;
  1439. return this;
  1440. },
  1441. rotateZ: function ( angle ) {
  1442. var te = this.elements;
  1443. var m11 = te[0];
  1444. var m21 = te[1];
  1445. var m31 = te[2];
  1446. var m41 = te[3];
  1447. var m12 = te[4];
  1448. var m22 = te[5];
  1449. var m32 = te[6];
  1450. var m42 = te[7];
  1451. var c = Math.cos( angle );
  1452. var s = Math.sin( angle );
  1453. te[0] = c * m11 + s * m12;
  1454. te[1] = c * m21 + s * m22;
  1455. te[2] = c * m31 + s * m32;
  1456. te[3] = c * m41 + s * m42;
  1457. te[4] = c * m12 - s * m11;
  1458. te[5] = c * m22 - s * m21;
  1459. te[6] = c * m32 - s * m31;
  1460. te[7] = c * m42 - s * m41;
  1461. return this;
  1462. },
  1463. rotateByAxis: function ( axis, angle ) {
  1464. var te = this.elements;
  1465. // optimize by checking axis
  1466. if ( axis.x === 1 && axis.y === 0 && axis.z === 0 ) {
  1467. return this.rotateX( angle );
  1468. } else if ( axis.x === 0 && axis.y === 1 && axis.z === 0 ) {
  1469. return this.rotateY( angle );
  1470. } else if ( axis.x === 0 && axis.y === 0 && axis.z === 1 ) {
  1471. return this.rotateZ( angle );
  1472. }
  1473. var x = axis.x, y = axis.y, z = axis.z;
  1474. var n = Math.sqrt(x * x + y * y + z * z);
  1475. x /= n;
  1476. y /= n;
  1477. z /= n;
  1478. var xx = x * x, yy = y * y, zz = z * z;
  1479. var c = Math.cos( angle );
  1480. var s = Math.sin( angle );
  1481. var oneMinusCosine = 1 - c;
  1482. var xy = x * y * oneMinusCosine;
  1483. var xz = x * z * oneMinusCosine;
  1484. var yz = y * z * oneMinusCosine;
  1485. var xs = x * s;
  1486. var ys = y * s;
  1487. var zs = z * s;
  1488. var r11 = xx + (1 - xx) * c;
  1489. var r21 = xy + zs;
  1490. var r31 = xz - ys;
  1491. var r12 = xy - zs;
  1492. var r22 = yy + (1 - yy) * c;
  1493. var r32 = yz + xs;
  1494. var r13 = xz + ys;
  1495. var r23 = yz - xs;
  1496. var r33 = zz + (1 - zz) * c;
  1497. var m11 = te[0], m21 = te[1], m31 = te[2], m41 = te[3];
  1498. var m12 = te[4], m22 = te[5], m32 = te[6], m42 = te[7];
  1499. var m13 = te[8], m23 = te[9], m33 = te[10], m43 = te[11];
  1500. var m14 = te[12], m24 = te[13], m34 = te[14], m44 = te[15];
  1501. te[0] = r11 * m11 + r21 * m12 + r31 * m13;
  1502. te[1] = r11 * m21 + r21 * m22 + r31 * m23;
  1503. te[2] = r11 * m31 + r21 * m32 + r31 * m33;
  1504. te[3] = r11 * m41 + r21 * m42 + r31 * m43;
  1505. te[4] = r12 * m11 + r22 * m12 + r32 * m13;
  1506. te[5] = r12 * m21 + r22 * m22 + r32 * m23;
  1507. te[6] = r12 * m31 + r22 * m32 + r32 * m33;
  1508. te[7] = r12 * m41 + r22 * m42 + r32 * m43;
  1509. te[8] = r13 * m11 + r23 * m12 + r33 * m13;
  1510. te[9] = r13 * m21 + r23 * m22 + r33 * m23;
  1511. te[10] = r13 * m31 + r23 * m32 + r33 * m33;
  1512. te[11] = r13 * m41 + r23 * m42 + r33 * m43;
  1513. return this;
  1514. },
  1515. scale: function ( v ) {
  1516. var te = this.elements;
  1517. var x = v.x, y = v.y, z = v.z;
  1518. te[0] *= x; te[4] *= y; te[8] *= z;
  1519. te[1] *= x; te[5] *= y; te[9] *= z;
  1520. te[2] *= x; te[6] *= y; te[10] *= z;
  1521. te[3] *= x; te[7] *= y; te[11] *= z;
  1522. return this;
  1523. },
  1524. //
  1525. makeTranslation: function ( x, y, z ) {
  1526. this.set(
  1527. 1, 0, 0, x,
  1528. 0, 1, 0, y,
  1529. 0, 0, 1, z,
  1530. 0, 0, 0, 1
  1531. );
  1532. return this;
  1533. },
  1534. makeRotationX: function ( theta ) {
  1535. var c = Math.cos( theta ), s = Math.sin( theta );
  1536. this.set(
  1537. 1, 0, 0, 0,
  1538. 0, c, -s, 0,
  1539. 0, s, c, 0,
  1540. 0, 0, 0, 1
  1541. );
  1542. return this;
  1543. },
  1544. makeRotationY: function ( theta ) {
  1545. var c = Math.cos( theta ), s = Math.sin( theta );
  1546. this.set(
  1547. c, 0, s, 0,
  1548. 0, 1, 0, 0,
  1549. -s, 0, c, 0,
  1550. 0, 0, 0, 1
  1551. );
  1552. return this;
  1553. },
  1554. makeRotationZ: function ( theta ) {
  1555. var c = Math.cos( theta ), s = Math.sin( theta );
  1556. this.set(
  1557. c, -s, 0, 0,
  1558. s, c, 0, 0,
  1559. 0, 0, 1, 0,
  1560. 0, 0, 0, 1
  1561. );
  1562. return this;
  1563. },
  1564. makeRotationAxis: function ( axis, angle ) {
  1565. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  1566. var c = Math.cos( angle );
  1567. var s = Math.sin( angle );
  1568. var t = 1 - c;
  1569. var x = axis.x, y = axis.y, z = axis.z;
  1570. var tx = t * x, ty = t * y;
  1571. this.set(
  1572. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  1573. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  1574. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  1575. 0, 0, 0, 1
  1576. );
  1577. return this;
  1578. },
  1579. makeScale: function ( x, y, z ) {
  1580. this.set(
  1581. x, 0, 0, 0,
  1582. 0, y, 0, 0,
  1583. 0, 0, z, 0,
  1584. 0, 0, 0, 1
  1585. );
  1586. return this;
  1587. },
  1588. makeFrustum: function ( left, right, bottom, top, near, far ) {
  1589. var te = this.elements;
  1590. var x = 2 * near / ( right - left );
  1591. var y = 2 * near / ( top - bottom );
  1592. var a = ( right + left ) / ( right - left );
  1593. var b = ( top + bottom ) / ( top - bottom );
  1594. var c = - ( far + near ) / ( far - near );
  1595. var d = - 2 * far * near / ( far - near );
  1596. te[0] = x; te[4] = 0; te[8] = a; te[12] = 0;
  1597. te[1] = 0; te[5] = y; te[9] = b; te[13] = 0;
  1598. te[2] = 0; te[6] = 0; te[10] = c; te[14] = d;
  1599. te[3] = 0; te[7] = 0; te[11] = - 1; te[15] = 0;
  1600. return this;
  1601. },
  1602. makePerspective: function ( fov, aspect, near, far ) {
  1603. var ymax = near * Math.tan( fov * Math.PI / 360 );
  1604. var ymin = - ymax;
  1605. var xmin = ymin * aspect;
  1606. var xmax = ymax * aspect;
  1607. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  1608. },
  1609. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  1610. var te = this.elements;
  1611. var w = right - left;
  1612. var h = top - bottom;
  1613. var p = far - near;
  1614. var x = ( right + left ) / w;
  1615. var y = ( top + bottom ) / h;
  1616. var z = ( far + near ) / p;
  1617. te[0] = 2 / w; te[4] = 0; te[8] = 0; te[12] = -x;
  1618. te[1] = 0; te[5] = 2 / h; te[9] = 0; te[13] = -y;
  1619. te[2] = 0; te[6] = 0; te[10] = -2 / p; te[14] = -z;
  1620. te[3] = 0; te[7] = 0; te[11] = 0; te[15] = 1;
  1621. return this;
  1622. },
  1623. clone: function () {
  1624. var te = this.elements;
  1625. return new THREE.Matrix4(
  1626. te[0], te[4], te[8], te[12],
  1627. te[1], te[5], te[9], te[13],
  1628. te[2], te[6], te[10], te[14],
  1629. te[3], te[7], te[11], te[15]
  1630. );
  1631. }
  1632. };
  1633. THREE.Matrix4.__v1 = new THREE.Vector3();
  1634. THREE.Matrix4.__v2 = new THREE.Vector3();
  1635. THREE.Matrix4.__v3 = new THREE.Vector3();
  1636. THREE.Matrix4.__m1 = new THREE.Matrix4();
  1637. THREE.Matrix4.__m2 = new THREE.Matrix4();
  1638. /**
  1639. * @author mr.doob / http://mrdoob.com/
  1640. * @author mikael emtinger / http://gomo.se/
  1641. * @author alteredq / http://alteredqualia.com/
  1642. */
  1643. THREE.Object3D = function () {
  1644. this.id = THREE.Object3DCount ++;
  1645. this.name = '';
  1646. this.parent = undefined;
  1647. this.children = [];
  1648. this.up = new THREE.Vector3( 0, 1, 0 );
  1649. this.position = new THREE.Vector3();
  1650. this.rotation = new THREE.Vector3();
  1651. this.eulerOrder = 'XYZ';
  1652. this.scale = new THREE.Vector3( 1, 1, 1 );
  1653. this.doubleSided = false;
  1654. this.flipSided = false;
  1655. this.renderDepth = null;
  1656. this.rotationAutoUpdate = true;
  1657. this.matrix = new THREE.Matrix4();
  1658. this.matrixWorld = new THREE.Matrix4();
  1659. this.matrixRotationWorld = new THREE.Matrix4();
  1660. this.matrixAutoUpdate = true;
  1661. this.matrixWorldNeedsUpdate = true;
  1662. this.quaternion = new THREE.Quaternion();
  1663. this.useQuaternion = false;
  1664. this.boundRadius = 0.0;
  1665. this.boundRadiusScale = 1.0;
  1666. this.visible = true;
  1667. this.castShadow = false;
  1668. this.receiveShadow = false;
  1669. this.frustumCulled = true;
  1670. this._vector = new THREE.Vector3();
  1671. };
  1672. THREE.Object3D.prototype = {
  1673. constructor: THREE.Object3D,
  1674. applyMatrix: function ( matrix ) {
  1675. this.matrix.multiply( matrix, this.matrix );
  1676. this.scale.getScaleFromMatrix( this.matrix );
  1677. this.rotation.getRotationFromMatrix( this.matrix, this.scale );
  1678. this.position.getPositionFromMatrix( this.matrix );
  1679. },
  1680. translate: function ( distance, axis ) {
  1681. this.matrix.rotateAxis( axis );
  1682. this.position.addSelf( axis.multiplyScalar( distance ) );
  1683. },
  1684. translateX: function ( distance ) {
  1685. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  1686. },
  1687. translateY: function ( distance ) {
  1688. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  1689. },
  1690. translateZ: function ( distance ) {
  1691. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  1692. },
  1693. lookAt: function ( vector ) {
  1694. // TODO: Add hierarchy support.
  1695. this.matrix.lookAt( vector, this.position, this.up );
  1696. if ( this.rotationAutoUpdate ) {
  1697. this.rotation.getRotationFromMatrix( this.matrix );
  1698. }
  1699. },
  1700. add: function ( object ) {
  1701. if ( object === this ) {
  1702. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  1703. return;
  1704. }
  1705. if ( object instanceof THREE.Object3D ) { // && this.children.indexOf( object ) === - 1
  1706. if ( object.parent !== undefined ) {
  1707. object.parent.remove( object );
  1708. }
  1709. object.parent = this;
  1710. this.children.push( object );
  1711. // add to scene
  1712. var scene = this;
  1713. while ( scene.parent !== undefined ) {
  1714. scene = scene.parent;
  1715. }
  1716. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  1717. scene.__addObject( object );
  1718. }
  1719. }
  1720. },
  1721. remove: function ( object ) {
  1722. var index = this.children.indexOf( object );
  1723. if ( index !== - 1 ) {
  1724. object.parent = undefined;
  1725. this.children.splice( index, 1 );
  1726. // remove from scene
  1727. var scene = this;
  1728. while ( scene.parent !== undefined ) {
  1729. scene = scene.parent;
  1730. }
  1731. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  1732. scene.__removeObject( object );
  1733. }
  1734. }
  1735. },
  1736. getChildByName: function ( name, recursive ) {
  1737. var c, cl, child;
  1738. for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
  1739. child = this.children[ c ];
  1740. if ( child.name === name ) {
  1741. return child;
  1742. }
  1743. if ( recursive ) {
  1744. child = child.getChildByName( name, recursive );
  1745. if ( child !== undefined ) {
  1746. return child;
  1747. }
  1748. }
  1749. }
  1750. return undefined;
  1751. },
  1752. updateMatrix: function () {
  1753. this.matrix.setPosition( this.position );
  1754. if ( this.useQuaternion ) {
  1755. this.matrix.setRotationFromQuaternion( this.quaternion );
  1756. } else {
  1757. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  1758. }
  1759. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  1760. this.matrix.scale( this.scale );
  1761. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  1762. }
  1763. this.matrixWorldNeedsUpdate = true;
  1764. },
  1765. updateMatrixWorld: function ( force ) {
  1766. this.matrixAutoUpdate && this.updateMatrix();
  1767. // update matrixWorld
  1768. if ( this.matrixWorldNeedsUpdate || force ) {
  1769. if ( this.parent ) {
  1770. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  1771. } else {
  1772. this.matrixWorld.copy( this.matrix );
  1773. }
  1774. this.matrixWorldNeedsUpdate = false;
  1775. force = true;
  1776. }
  1777. // update children
  1778. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  1779. this.children[ i ].updateMatrixWorld( force );
  1780. }
  1781. }
  1782. };
  1783. THREE.Object3DCount = 0;
  1784. /**
  1785. * @author mr.doob / http://mrdoob.com/
  1786. * @author supereggbert / http://www.paulbrunt.co.uk/
  1787. * @author julianwa / https://github.com/julianwa
  1788. */
  1789. THREE.Projector = function() {
  1790. var _object, _objectCount, _objectPool = [],
  1791. _vertex, _vertexCount, _vertexPool = [],
  1792. _face, _face3Count, _face3Pool = [], _face4Count, _face4Pool = [],
  1793. _line, _lineCount, _linePool = [],
  1794. _particle, _particleCount, _particlePool = [],
  1795. _renderData = { objects: [], sprites: [], lights: [], elements: [] },
  1796. _vector3 = new THREE.Vector3(),
  1797. _vector4 = new THREE.Vector4(),
  1798. _projScreenMatrix = new THREE.Matrix4(),
  1799. _projScreenobjectMatrixWorld = new THREE.Matrix4(),
  1800. _frustum = new THREE.Frustum(),
  1801. _clippedVertex1PositionScreen = new THREE.Vector4(),
  1802. _clippedVertex2PositionScreen = new THREE.Vector4(),
  1803. _face3VertexNormals;
  1804. this.projectVector = function ( vector, camera ) {
  1805. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1806. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1807. _projScreenMatrix.multiplyVector3( vector );
  1808. return vector;
  1809. };
  1810. this.unprojectVector = function ( vector, camera ) {
  1811. camera.projectionMatrixInverse.getInverse( camera.projectionMatrix );
  1812. _projScreenMatrix.multiply( camera.matrixWorld, camera.projectionMatrixInverse );
  1813. _projScreenMatrix.multiplyVector3( vector );
  1814. return vector;
  1815. };
  1816. this.pickingRay = function ( vector, camera ) {
  1817. var end, ray, t;
  1818. // set two vectors with opposing z values
  1819. vector.z = -1.0;
  1820. end = new THREE.Vector3( vector.x, vector.y, 1.0 );
  1821. this.unprojectVector( vector, camera );
  1822. this.unprojectVector( end, camera );
  1823. // find direction from vector to end
  1824. end.subSelf( vector ).normalize();
  1825. return new THREE.Ray( vector, end );
  1826. };
  1827. this.projectGraph = function ( root, sort ) {
  1828. _objectCount = 0;
  1829. _renderData.objects.length = 0;
  1830. _renderData.sprites.length = 0;
  1831. _renderData.lights.length = 0;
  1832. var projectObject = function ( object ) {
  1833. if ( object.visible === false ) return;
  1834. if ( ( object instanceof THREE.Mesh || object instanceof THREE.Line ) &&
  1835. ( object.frustumCulled === false || _frustum.contains( object ) ) ) {
  1836. _vector3.copy( object.matrixWorld.getPosition() );
  1837. _projScreenMatrix.multiplyVector3( _vector3 );
  1838. _object = getNextObjectInPool();
  1839. _object.object = object;
  1840. _object.z = _vector3.z;
  1841. _renderData.objects.push( _object );
  1842. } else if ( object instanceof THREE.Sprite || object instanceof THREE.Particle ) {
  1843. _vector3.copy( object.matrixWorld.getPosition() );
  1844. _projScreenMatrix.multiplyVector3( _vector3 );
  1845. _object = getNextObjectInPool();
  1846. _object.object = object;
  1847. _object.z = _vector3.z;
  1848. _renderData.sprites.push( _object );
  1849. } else if ( object instanceof THREE.Light ) {
  1850. _renderData.lights.push( object );
  1851. }
  1852. for ( var c = 0, cl = object.children.length; c < cl; c ++ ) {
  1853. projectObject( object.children[ c ] );
  1854. }
  1855. };
  1856. projectObject( root );
  1857. sort && _renderData.objects.sort( painterSort );
  1858. return _renderData;
  1859. };
  1860. this.projectScene = function ( scene, camera, sort ) {
  1861. var near = camera.near, far = camera.far, visible = false,
  1862. o, ol, v, vl, f, fl, n, nl, c, cl, u, ul, object,
  1863. objectMatrixWorld, objectMatrixWorldRotation,
  1864. geometry, geometryMaterials, vertices, vertex, vertexPositionScreen,
  1865. faces, face, faceVertexNormals, normal, faceVertexUvs, uvs,
  1866. v1, v2, v3, v4;
  1867. _face3Count = 0;
  1868. _face4Count = 0;
  1869. _lineCount = 0;
  1870. _particleCount = 0;
  1871. _renderData.elements.length = 0;
  1872. if ( camera.parent === undefined ) {
  1873. console.warn( 'DEPRECATED: Camera hasn\'t been added to a Scene. Adding it...' );
  1874. scene.add( camera );
  1875. }
  1876. scene.updateMatrixWorld();
  1877. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1878. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1879. _frustum.setFromMatrix( _projScreenMatrix );
  1880. _renderData = this.projectGraph( scene, false );
  1881. for ( o = 0, ol = _renderData.objects.length; o < ol; o++ ) {
  1882. object = _renderData.objects[ o ].object;
  1883. objectMatrixWorld = object.matrixWorld;
  1884. _vertexCount = 0;
  1885. if ( object instanceof THREE.Mesh ) {
  1886. geometry = object.geometry;
  1887. geometryMaterials = object.geometry.materials;
  1888. vertices = geometry.vertices;
  1889. faces = geometry.faces;
  1890. faceVertexUvs = geometry.faceVertexUvs;
  1891. objectMatrixWorldRotation = object.matrixRotationWorld.extractRotation( objectMatrixWorld );
  1892. for ( v = 0, vl = vertices.length; v < vl; v ++ ) {
  1893. _vertex = getNextVertexInPool();
  1894. _vertex.positionWorld.copy( vertices[ v ] );
  1895. objectMatrixWorld.multiplyVector3( _vertex.positionWorld );
  1896. _vertex.positionScreen.copy( _vertex.positionWorld );
  1897. _projScreenMatrix.multiplyVector4( _vertex.positionScreen );
  1898. _vertex.positionScreen.x /= _vertex.positionScreen.w;
  1899. _vertex.positionScreen.y /= _vertex.positionScreen.w;
  1900. _vertex.visible = _vertex.positionScreen.z > near && _vertex.positionScreen.z < far;
  1901. }
  1902. for ( f = 0, fl = faces.length; f < fl; f ++ ) {
  1903. face = faces[ f ];
  1904. if ( face instanceof THREE.Face3 ) {
  1905. v1 = _vertexPool[ face.a ];
  1906. v2 = _vertexPool[ face.b ];
  1907. v3 = _vertexPool[ face.c ];
  1908. if ( v1.visible && v2.visible && v3.visible ) {
  1909. visible = ( ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  1910. ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
  1911. if ( object.doubleSided || visible != object.flipSided ) {
  1912. _face = getNextFace3InPool();
  1913. _face.v1.copy( v1 );
  1914. _face.v2.copy( v2 );
  1915. _face.v3.copy( v3 );
  1916. } else {
  1917. continue;
  1918. }
  1919. } else {
  1920. continue;
  1921. }
  1922. } else if ( face instanceof THREE.Face4 ) {
  1923. v1 = _vertexPool[ face.a ];
  1924. v2 = _vertexPool[ face.b ];
  1925. v3 = _vertexPool[ face.c ];
  1926. v4 = _vertexPool[ face.d ];
  1927. if ( v1.visible && v2.visible && v3.visible && v4.visible ) {
  1928. visible = ( v4.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  1929. ( v4.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ||
  1930. ( v2.positionScreen.x - v3.positionScreen.x ) * ( v4.positionScreen.y - v3.positionScreen.y ) -
  1931. ( v2.positionScreen.y - v3.positionScreen.y ) * ( v4.positionScreen.x - v3.positionScreen.x ) < 0;
  1932. if ( object.doubleSided || visible != object.flipSided ) {
  1933. _face = getNextFace4InPool();
  1934. _face.v1.copy( v1 );
  1935. _face.v2.copy( v2 );
  1936. _face.v3.copy( v3 );
  1937. _face.v4.copy( v4 );
  1938. } else {
  1939. continue;
  1940. }
  1941. } else {
  1942. continue;
  1943. }
  1944. }
  1945. _face.normalWorld.copy( face.normal );
  1946. if ( !visible && ( object.flipSided || object.doubleSided ) ) _face.normalWorld.negate();
  1947. objectMatrixWorldRotation.multiplyVector3( _face.normalWorld );
  1948. _face.centroidWorld.copy( face.centroid );
  1949. objectMatrixWorld.multiplyVector3( _face.centroidWorld );
  1950. _face.centroidScreen.copy( _face.centroidWorld );
  1951. _projScreenMatrix.multiplyVector3( _face.centroidScreen );
  1952. faceVertexNormals = face.vertexNormals;
  1953. for ( n = 0, nl = faceVertexNormals.length; n < nl; n ++ ) {
  1954. normal = _face.vertexNormalsWorld[ n ];
  1955. normal.copy( faceVertexNormals[ n ] );
  1956. if ( !visible && ( object.flipSided || object.doubleSided ) ) normal.negate();
  1957. objectMatrixWorldRotation.multiplyVector3( normal );
  1958. }
  1959. for ( c = 0, cl = faceVertexUvs.length; c < cl; c ++ ) {
  1960. uvs = faceVertexUvs[ c ][ f ];
  1961. if ( !uvs ) continue;
  1962. for ( u = 0, ul = uvs.length; u < ul; u ++ ) {
  1963. _face.uvs[ c ][ u ] = uvs[ u ];
  1964. }
  1965. }
  1966. _face.material = object.material;
  1967. _face.faceMaterial = face.materialIndex !== null ? geometryMaterials[ face.materialIndex ] : null;
  1968. _face.z = _face.centroidScreen.z;
  1969. _renderData.elements.push( _face );
  1970. }
  1971. } else if ( object instanceof THREE.Line ) {
  1972. _projScreenobjectMatrixWorld.multiply( _projScreenMatrix, objectMatrixWorld );
  1973. vertices = object.geometry.vertices;
  1974. v1 = getNextVertexInPool();
  1975. v1.positionScreen.copy( vertices[ 0 ] );
  1976. _projScreenobjectMatrixWorld.multiplyVector4( v1.positionScreen );
  1977. // Handle LineStrip and LinePieces
  1978. var step = object.type === THREE.LinePieces ? 2 : 1;
  1979. for ( v = 1, vl = vertices.length; v < vl; v ++ ) {
  1980. v1 = getNextVertexInPool();
  1981. v1.positionScreen.copy( vertices[ v ] );
  1982. _projScreenobjectMatrixWorld.multiplyVector4( v1.positionScreen );
  1983. if ( ( v + 1 ) % step > 0 ) continue;
  1984. v2 = _vertexPool[ _vertexCount - 2 ];
  1985. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  1986. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  1987. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) ) {
  1988. // Perform the perspective divide
  1989. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  1990. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  1991. _line = getNextLineInPool();
  1992. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  1993. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  1994. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  1995. _line.material = object.material;
  1996. _renderData.elements.push( _line );
  1997. }
  1998. }
  1999. }
  2000. }
  2001. for ( o = 0, ol = _renderData.sprites.length; o < ol; o++ ) {
  2002. object = _renderData.sprites[ o ].object;
  2003. objectMatrixWorld = object.matrixWorld;
  2004. if ( object instanceof THREE.Particle ) {
  2005. _vector4.set( objectMatrixWorld.elements[12], objectMatrixWorld.elements[13], objectMatrixWorld.elements[14], 1 );
  2006. _projScreenMatrix.multiplyVector4( _vector4 );
  2007. _vector4.z /= _vector4.w;
  2008. if ( _vector4.z > 0 && _vector4.z < 1 ) {
  2009. _particle = getNextParticleInPool();
  2010. _particle.x = _vector4.x / _vector4.w;
  2011. _particle.y = _vector4.y / _vector4.w;
  2012. _particle.z = _vector4.z;
  2013. _particle.rotation = object.rotation.z;
  2014. _particle.scale.x = object.scale.x * Math.abs( _particle.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) );
  2015. _particle.scale.y = object.scale.y * Math.abs( _particle.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) );
  2016. _particle.material = object.material;
  2017. _renderData.elements.push( _particle );
  2018. }
  2019. }
  2020. }
  2021. sort && _renderData.elements.sort( painterSort );
  2022. return _renderData;
  2023. };
  2024. // Pools
  2025. function getNextObjectInPool() {
  2026. var object = _objectPool[ _objectCount ] = _objectPool[ _objectCount ] || new THREE.RenderableObject();
  2027. _objectCount ++;
  2028. return object;
  2029. }
  2030. function getNextVertexInPool() {
  2031. var vertex = _vertexPool[ _vertexCount ] = _vertexPool[ _vertexCount ] || new THREE.RenderableVertex();
  2032. _vertexCount ++;
  2033. return vertex;
  2034. }
  2035. function getNextFace3InPool() {
  2036. var face = _face3Pool[ _face3Count ] = _face3Pool[ _face3Count ] || new THREE.RenderableFace3();
  2037. _face3Count ++;
  2038. return face;
  2039. }
  2040. function getNextFace4InPool() {
  2041. var face = _face4Pool[ _face4Count ] = _face4Pool[ _face4Count ] || new THREE.RenderableFace4();
  2042. _face4Count ++;
  2043. return face;
  2044. }
  2045. function getNextLineInPool() {
  2046. var line = _linePool[ _lineCount ] = _linePool[ _lineCount ] || new THREE.RenderableLine();
  2047. _lineCount ++;
  2048. return line;
  2049. }
  2050. function getNextParticleInPool() {
  2051. var particle = _particlePool[ _particleCount ] = _particlePool[ _particleCount ] || new THREE.RenderableParticle();
  2052. _particleCount ++;
  2053. return particle;
  2054. }
  2055. //
  2056. function painterSort( a, b ) {
  2057. return b.z - a.z;
  2058. }
  2059. function clipLine( s1, s2 ) {
  2060. var alpha1 = 0, alpha2 = 1,
  2061. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  2062. // Z = -1 and Z = +1, respectively.
  2063. bc1near = s1.z + s1.w,
  2064. bc2near = s2.z + s2.w,
  2065. bc1far = - s1.z + s1.w,
  2066. bc2far = - s2.z + s2.w;
  2067. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  2068. // Both vertices lie entirely within all clip planes.
  2069. return true;
  2070. } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
  2071. // Both vertices lie entirely outside one of the clip planes.
  2072. return false;
  2073. } else {
  2074. // The line segment spans at least one clip plane.
  2075. if ( bc1near < 0 ) {
  2076. // v1 lies outside the near plane, v2 inside
  2077. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  2078. } else if ( bc2near < 0 ) {
  2079. // v2 lies outside the near plane, v1 inside
  2080. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  2081. }
  2082. if ( bc1far < 0 ) {
  2083. // v1 lies outside the far plane, v2 inside
  2084. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  2085. } else if ( bc2far < 0 ) {
  2086. // v2 lies outside the far plane, v2 inside
  2087. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  2088. }
  2089. if ( alpha2 < alpha1 ) {
  2090. // The line segment spans two boundaries, but is outside both of them.
  2091. // (This can't happen when we're only clipping against just near/far but good
  2092. // to leave the check here for future usage if other clip planes are added.)
  2093. return false;
  2094. } else {
  2095. // Update the s1 and s2 vertices to match the clipped line segment.
  2096. s1.lerpSelf( s2, alpha1 );
  2097. s2.lerpSelf( s1, 1 - alpha2 );
  2098. return true;
  2099. }
  2100. }
  2101. }
  2102. };
  2103. /**
  2104. * @author mikael emtinger / http://gomo.se/
  2105. * @author alteredq / http://alteredqualia.com/
  2106. */
  2107. THREE.Quaternion = function( x, y, z, w ) {
  2108. this.x = x || 0;
  2109. this.y = y || 0;
  2110. this.z = z || 0;
  2111. this.w = ( w !== undefined ) ? w : 1;
  2112. };
  2113. THREE.Quaternion.prototype = {
  2114. constructor: THREE.Quaternion,
  2115. set: function ( x, y, z, w ) {
  2116. this.x = x;
  2117. this.y = y;
  2118. this.z = z;
  2119. this.w = w;
  2120. return this;
  2121. },
  2122. copy: function ( q ) {
  2123. this.x = q.x;
  2124. this.y = q.y;
  2125. this.z = q.z;
  2126. this.w = q.w;
  2127. return this;
  2128. },
  2129. setFromEuler: function ( vector ) {
  2130. var c = Math.PI / 360, // 0.5 * Math.PI / 360, // 0.5 is an optimization
  2131. x = vector.x * c,
  2132. y = vector.y * c,
  2133. z = vector.z * c,
  2134. c1 = Math.cos( y ),
  2135. s1 = Math.sin( y ),
  2136. c2 = Math.cos( -z ),
  2137. s2 = Math.sin( -z ),
  2138. c3 = Math.cos( x ),
  2139. s3 = Math.sin( x ),
  2140. c1c2 = c1 * c2,
  2141. s1s2 = s1 * s2;
  2142. this.w = c1c2 * c3 - s1s2 * s3;
  2143. this.x = c1c2 * s3 + s1s2 * c3;
  2144. this.y = s1 * c2 * c3 + c1 * s2 * s3;
  2145. this.z = c1 * s2 * c3 - s1 * c2 * s3;
  2146. return this;
  2147. },
  2148. setFromAxisAngle: function ( axis, angle ) {
  2149. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  2150. // axis have to be normalized
  2151. var halfAngle = angle / 2,
  2152. s = Math.sin( halfAngle );
  2153. this.x = axis.x * s;
  2154. this.y = axis.y * s;
  2155. this.z = axis.z * s;
  2156. this.w = Math.cos( halfAngle );
  2157. return this;
  2158. },
  2159. setFromRotationMatrix: function ( m ) {
  2160. // Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  2161. function copySign( a, b ) {
  2162. return b < 0 ? -Math.abs( a ) : Math.abs( a );
  2163. }
  2164. var absQ = Math.pow( m.determinant(), 1.0 / 3.0 );
  2165. this.w = Math.sqrt( Math.max( 0, absQ + m.elements[0] + m.elements[5] + m.elements[10] ) ) / 2;
  2166. this.x = Math.sqrt( Math.max( 0, absQ + m.elements[0] - m.elements[5] - m.elements[10] ) ) / 2;
  2167. this.y = Math.sqrt( Math.max( 0, absQ - m.elements[0] + m.elements[5] - m.elements[10] ) ) / 2;
  2168. this.z = Math.sqrt( Math.max( 0, absQ - m.elements[0] - m.elements[5] + m.elements[10] ) ) / 2;
  2169. this.x = copySign( this.x, ( m.elements[6] - m.elements[9] ) );
  2170. this.y = copySign( this.y, ( m.elements[8] - m.elements[2] ) );
  2171. this.z = copySign( this.z, ( m.elements[1] - m.elements[4] ) );
  2172. this.normalize();
  2173. return this;
  2174. },
  2175. calculateW : function () {
  2176. this.w = - Math.sqrt( Math.abs( 1.0 - this.x * this.x - this.y * this.y - this.z * this.z ) );
  2177. return this;
  2178. },
  2179. inverse: function () {
  2180. this.x *= -1;
  2181. this.y *= -1;
  2182. this.z *= -1;
  2183. return this;
  2184. },
  2185. length: function () {
  2186. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  2187. },
  2188. normalize: function () {
  2189. var l = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  2190. if ( l === 0 ) {
  2191. this.x = 0;
  2192. this.y = 0;
  2193. this.z = 0;
  2194. this.w = 0;
  2195. } else {
  2196. l = 1 / l;
  2197. this.x = this.x * l;
  2198. this.y = this.y * l;
  2199. this.z = this.z * l;
  2200. this.w = this.w * l;
  2201. }
  2202. return this;
  2203. },
  2204. multiply: function ( a, b ) {
  2205. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  2206. this.x = a.x * b.w + a.y * b.z - a.z * b.y + a.w * b.x;
  2207. this.y = -a.x * b.z + a.y * b.w + a.z * b.x + a.w * b.y;
  2208. this.z = a.x * b.y - a.y * b.x + a.z * b.w + a.w * b.z;
  2209. this.w = -a.x * b.x - a.y * b.y - a.z * b.z + a.w * b.w;
  2210. return this;
  2211. },
  2212. multiplySelf: function ( b ) {
  2213. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  2214. qbx = b.x, qby = b.y, qbz = b.z, qbw = b.w;
  2215. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  2216. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  2217. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  2218. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  2219. return this;
  2220. },
  2221. multiplyVector3: function ( vector, dest ) {
  2222. if ( !dest ) { dest = vector; }
  2223. var x = vector.x, y = vector.y, z = vector.z,
  2224. qx = this.x, qy = this.y, qz = this.z, qw = this.w;
  2225. // calculate quat * vector
  2226. var ix = qw * x + qy * z - qz * y,
  2227. iy = qw * y + qz * x - qx * z,
  2228. iz = qw * z + qx * y - qy * x,
  2229. iw = -qx * x - qy * y - qz * z;
  2230. // calculate result * inverse quat
  2231. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  2232. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  2233. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  2234. return dest;
  2235. },
  2236. clone: function () {
  2237. return new THREE.Quaternion( this.x, this.y, this.z, this.w );
  2238. }
  2239. }
  2240. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  2241. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  2242. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  2243. if (cosHalfTheta < 0) {
  2244. qm.w = -qb.w; qm.x = -qb.x; qm.y = -qb.y; qm.z = -qb.z;
  2245. cosHalfTheta = -cosHalfTheta;
  2246. } else {
  2247. qm.copy(qb);
  2248. }
  2249. if ( Math.abs( cosHalfTheta ) >= 1.0 ) {
  2250. qm.w = qa.w; qm.x = qa.x; qm.y = qa.y; qm.z = qa.z;
  2251. return qm;
  2252. }
  2253. var halfTheta = Math.acos( cosHalfTheta ),
  2254. sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  2255. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  2256. qm.w = 0.5 * ( qa.w + qb.w );
  2257. qm.x = 0.5 * ( qa.x + qb.x );
  2258. qm.y = 0.5 * ( qa.y + qb.y );
  2259. qm.z = 0.5 * ( qa.z + qb.z );
  2260. return qm;
  2261. }
  2262. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  2263. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  2264. qm.w = ( qa.w * ratioA + qm.w * ratioB );
  2265. qm.x = ( qa.x * ratioA + qm.x * ratioB );
  2266. qm.y = ( qa.y * ratioA + qm.y * ratioB );
  2267. qm.z = ( qa.z * ratioA + qm.z * ratioB );
  2268. return qm;
  2269. }
  2270. /**
  2271. * @author mr.doob / http://mrdoob.com/
  2272. */
  2273. THREE.Vertex = THREE.Vector3;
  2274. /**
  2275. * @author mr.doob / http://mrdoob.com/
  2276. * @author alteredq / http://alteredqualia.com/
  2277. */
  2278. THREE.Face3 = function ( a, b, c, normal, color, materialIndex ) {
  2279. this.a = a;
  2280. this.b = b;
  2281. this.c = c;
  2282. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  2283. this.vertexNormals = normal instanceof Array ? normal : [ ];
  2284. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  2285. this.vertexColors = color instanceof Array ? color : [];
  2286. this.vertexTangents = [];
  2287. this.materialIndex = materialIndex;
  2288. this.centroid = new THREE.Vector3();
  2289. };
  2290. THREE.Face3.prototype = {
  2291. constructor: THREE.Face3,
  2292. clone: function () {
  2293. var face = new THREE.Face3( this.a, this.b, this.c );
  2294. face.normal.copy( this.normal );
  2295. face.color.copy( this.color );
  2296. face.centroid.copy( this.centroid );
  2297. face.materialIndex = this.materialIndex;
  2298. var i, il;
  2299. for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  2300. for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  2301. for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  2302. return face;
  2303. }
  2304. };
  2305. /**
  2306. * @author mr.doob / http://mrdoob.com/
  2307. * @author alteredq / http://alteredqualia.com/
  2308. */
  2309. THREE.Face4 = function ( a, b, c, d, normal, color, materialIndex ) {
  2310. this.a = a;
  2311. this.b = b;
  2312. this.c = c;
  2313. this.d = d;
  2314. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  2315. this.vertexNormals = normal instanceof Array ? normal : [ ];
  2316. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  2317. this.vertexColors = color instanceof Array ? color : [];
  2318. this.vertexTangents = [];
  2319. this.materialIndex = materialIndex;
  2320. this.centroid = new THREE.Vector3();
  2321. };
  2322. THREE.Face4.prototype = {
  2323. constructor: THREE.Face4,
  2324. clone: function () {
  2325. var face = new THREE.Face4( this.a, this.b, this.c, this.d );
  2326. face.normal.copy( this.normal );
  2327. face.color.copy( this.color );
  2328. face.centroid.copy( this.centroid );
  2329. face.materialIndex = this.materialIndex;
  2330. var i, il;
  2331. for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  2332. for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  2333. for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  2334. return face;
  2335. }
  2336. };
  2337. /**
  2338. * @author mr.doob / http://mrdoob.com/
  2339. */
  2340. THREE.UV = function ( u, v ) {
  2341. this.u = u || 0;
  2342. this.v = v || 0;
  2343. };
  2344. THREE.UV.prototype = {
  2345. constructor: THREE.UV,
  2346. set: function ( u, v ) {
  2347. this.u = u;
  2348. this.v = v;
  2349. return this;
  2350. },
  2351. copy: function ( uv ) {
  2352. this.u = uv.u;
  2353. this.v = uv.v;
  2354. return this;
  2355. },
  2356. lerpSelf: function ( uv, alpha ) {
  2357. this.u += ( uv.u - this.u ) * alpha;
  2358. this.v += ( uv.v - this.v ) * alpha;
  2359. return this;
  2360. },
  2361. clone: function () {
  2362. return new THREE.UV( this.u, this.v );
  2363. }
  2364. };
  2365. /**
  2366. * @author mr.doob / http://mrdoob.com/
  2367. * @author kile / http://kile.stravaganza.org/
  2368. * @author alteredq / http://alteredqualia.com/
  2369. * @author mikael emtinger / http://gomo.se/
  2370. * @author zz85 / http://www.lab4games.net/zz85/blog
  2371. */
  2372. THREE.Geometry = function () {
  2373. this.id = THREE.GeometryCount ++;
  2374. this.vertices = [];
  2375. this.colors = []; // one-to-one vertex colors, used in ParticleSystem, Line and Ribbon
  2376. this.materials = [];
  2377. this.faces = [];
  2378. this.faceUvs = [[]];
  2379. this.faceVertexUvs = [[]];
  2380. this.morphTargets = [];
  2381. this.morphColors = [];
  2382. this.morphNormals = [];
  2383. this.skinWeights = [];
  2384. this.skinIndices = [];
  2385. this.boundingBox = null;
  2386. this.boundingSphere = null;
  2387. this.hasTangents = false;
  2388. this.dynamic = false; // unless set to true the *Arrays will be deleted once sent to a buffer.
  2389. };
  2390. THREE.Geometry.prototype = {
  2391. constructor : THREE.Geometry,
  2392. applyMatrix: function ( matrix ) {
  2393. var matrixRotation = new THREE.Matrix4();
  2394. matrixRotation.extractRotation( matrix );
  2395. for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
  2396. var vertex = this.vertices[ i ];
  2397. matrix.multiplyVector3( vertex );
  2398. }
  2399. for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
  2400. var face = this.faces[ i ];
  2401. matrixRotation.multiplyVector3( face.normal );
  2402. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  2403. matrixRotation.multiplyVector3( face.vertexNormals[ j ] );
  2404. }
  2405. matrix.multiplyVector3( face.centroid );
  2406. }
  2407. },
  2408. computeCentroids: function () {
  2409. var f, fl, face;
  2410. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2411. face = this.faces[ f ];
  2412. face.centroid.set( 0, 0, 0 );
  2413. if ( face instanceof THREE.Face3 ) {
  2414. face.centroid.addSelf( this.vertices[ face.a ] );
  2415. face.centroid.addSelf( this.vertices[ face.b ] );
  2416. face.centroid.addSelf( this.vertices[ face.c ] );
  2417. face.centroid.divideScalar( 3 );
  2418. } else if ( face instanceof THREE.Face4 ) {
  2419. face.centroid.addSelf( this.vertices[ face.a ] );
  2420. face.centroid.addSelf( this.vertices[ face.b ] );
  2421. face.centroid.addSelf( this.vertices[ face.c ] );
  2422. face.centroid.addSelf( this.vertices[ face.d ] );
  2423. face.centroid.divideScalar( 4 );
  2424. }
  2425. }
  2426. },
  2427. computeFaceNormals: function () {
  2428. var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
  2429. cb = new THREE.Vector3(), ab = new THREE.Vector3();
  2430. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2431. face = this.faces[ f ];
  2432. vA = this.vertices[ face.a ];
  2433. vB = this.vertices[ face.b ];
  2434. vC = this.vertices[ face.c ];
  2435. cb.sub( vC, vB );
  2436. ab.sub( vA, vB );
  2437. cb.crossSelf( ab );
  2438. if ( !cb.isZero() ) {
  2439. cb.normalize();
  2440. }
  2441. face.normal.copy( cb );
  2442. }
  2443. },
  2444. computeVertexNormals: function () {
  2445. var v, vl, f, fl, face, vertices;
  2446. // create internal buffers for reuse when calling this method repeatedly
  2447. // (otherwise memory allocation / deallocation every frame is big resource hog)
  2448. if ( this.__tmpVertices === undefined ) {
  2449. this.__tmpVertices = new Array( this.vertices.length );
  2450. vertices = this.__tmpVertices;
  2451. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2452. vertices[ v ] = new THREE.Vector3();
  2453. }
  2454. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2455. face = this.faces[ f ];
  2456. if ( face instanceof THREE.Face3 ) {
  2457. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  2458. } else if ( face instanceof THREE.Face4 ) {
  2459. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  2460. }
  2461. }
  2462. } else {
  2463. vertices = this.__tmpVertices;
  2464. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2465. vertices[ v ].set( 0, 0, 0 );
  2466. }
  2467. }
  2468. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2469. face = this.faces[ f ];
  2470. if ( face instanceof THREE.Face3 ) {
  2471. vertices[ face.a ].addSelf( face.normal );
  2472. vertices[ face.b ].addSelf( face.normal );
  2473. vertices[ face.c ].addSelf( face.normal );
  2474. } else if ( face instanceof THREE.Face4 ) {
  2475. vertices[ face.a ].addSelf( face.normal );
  2476. vertices[ face.b ].addSelf( face.normal );
  2477. vertices[ face.c ].addSelf( face.normal );
  2478. vertices[ face.d ].addSelf( face.normal );
  2479. }
  2480. }
  2481. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2482. vertices[ v ].normalize();
  2483. }
  2484. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2485. face = this.faces[ f ];
  2486. if ( face instanceof THREE.Face3 ) {
  2487. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  2488. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  2489. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  2490. } else if ( face instanceof THREE.Face4 ) {
  2491. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  2492. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  2493. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  2494. face.vertexNormals[ 3 ].copy( vertices[ face.d ] );
  2495. }
  2496. }
  2497. },
  2498. computeMorphNormals: function () {
  2499. var i, il, f, fl, face;
  2500. // save original normals
  2501. // - create temp variables on first access
  2502. // otherwise just copy (for faster repeated calls)
  2503. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2504. face = this.faces[ f ];
  2505. if ( ! face.__originalFaceNormal ) {
  2506. face.__originalFaceNormal = face.normal.clone();
  2507. } else {
  2508. face.__originalFaceNormal.copy( face.normal );
  2509. }
  2510. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  2511. for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  2512. if ( ! face.__originalVertexNormals[ i ] ) {
  2513. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  2514. } else {
  2515. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  2516. }
  2517. }
  2518. }
  2519. // use temp geometry to compute face and vertex normals for each morph
  2520. var tmpGeo = new THREE.Geometry();
  2521. tmpGeo.faces = this.faces;
  2522. for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  2523. // create on first access
  2524. if ( ! this.morphNormals[ i ] ) {
  2525. this.morphNormals[ i ] = {};
  2526. this.morphNormals[ i ].faceNormals = [];
  2527. this.morphNormals[ i ].vertexNormals = [];
  2528. var dstNormalsFace = this.morphNormals[ i ].faceNormals;
  2529. var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  2530. var faceNormal, vertexNormals;
  2531. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2532. face = this.faces[ f ];
  2533. faceNormal = new THREE.Vector3();
  2534. if ( face instanceof THREE.Face3 ) {
  2535. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() };
  2536. } else {
  2537. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3(), d: new THREE.Vector3() };
  2538. }
  2539. dstNormalsFace.push( faceNormal );
  2540. dstNormalsVertex.push( vertexNormals );
  2541. }
  2542. }
  2543. var morphNormals = this.morphNormals[ i ];
  2544. // set vertices to morph target
  2545. tmpGeo.vertices = this.morphTargets[ i ].vertices;
  2546. // compute morph normals
  2547. tmpGeo.computeFaceNormals();
  2548. tmpGeo.computeVertexNormals();
  2549. // store morph normals
  2550. var faceNormal, vertexNormals;
  2551. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2552. face = this.faces[ f ];
  2553. faceNormal = morphNormals.faceNormals[ f ];
  2554. vertexNormals = morphNormals.vertexNormals[ f ];
  2555. faceNormal.copy( face.normal );
  2556. if ( face instanceof THREE.Face3 ) {
  2557. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  2558. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  2559. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  2560. } else {
  2561. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  2562. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  2563. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  2564. vertexNormals.d.copy( face.vertexNormals[ 3 ] );
  2565. }
  2566. }
  2567. }
  2568. // restore original normals
  2569. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2570. face = this.faces[ f ];
  2571. face.normal = face.__originalFaceNormal;
  2572. face.vertexNormals = face.__originalVertexNormals;
  2573. }
  2574. },
  2575. computeTangents: function () {
  2576. // based on http://www.terathon.com/code/tangent.html
  2577. // tangents go to vertices
  2578. var f, fl, v, vl, i, il, vertexIndex,
  2579. face, uv, vA, vB, vC, uvA, uvB, uvC,
  2580. x1, x2, y1, y2, z1, z2,
  2581. s1, s2, t1, t2, r, t, test,
  2582. tan1 = [], tan2 = [],
  2583. sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
  2584. tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
  2585. n = new THREE.Vector3(), w;
  2586. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2587. tan1[ v ] = new THREE.Vector3();
  2588. tan2[ v ] = new THREE.Vector3();
  2589. }
  2590. function handleTriangle( context, a, b, c, ua, ub, uc ) {
  2591. vA = context.vertices[ a ];
  2592. vB = context.vertices[ b ];
  2593. vC = context.vertices[ c ];
  2594. uvA = uv[ ua ];
  2595. uvB = uv[ ub ];
  2596. uvC = uv[ uc ];
  2597. x1 = vB.x - vA.x;
  2598. x2 = vC.x - vA.x;
  2599. y1 = vB.y - vA.y;
  2600. y2 = vC.y - vA.y;
  2601. z1 = vB.z - vA.z;
  2602. z2 = vC.z - vA.z;
  2603. s1 = uvB.u - uvA.u;
  2604. s2 = uvC.u - uvA.u;
  2605. t1 = uvB.v - uvA.v;
  2606. t2 = uvC.v - uvA.v;
  2607. r = 1.0 / ( s1 * t2 - s2 * t1 );
  2608. sdir.set( ( t2 * x1 - t1 * x2 ) * r,
  2609. ( t2 * y1 - t1 * y2 ) * r,
  2610. ( t2 * z1 - t1 * z2 ) * r );
  2611. tdir.set( ( s1 * x2 - s2 * x1 ) * r,
  2612. ( s1 * y2 - s2 * y1 ) * r,
  2613. ( s1 * z2 - s2 * z1 ) * r );
  2614. tan1[ a ].addSelf( sdir );
  2615. tan1[ b ].addSelf( sdir );
  2616. tan1[ c ].addSelf( sdir );
  2617. tan2[ a ].addSelf( tdir );
  2618. tan2[ b ].addSelf( tdir );
  2619. tan2[ c ].addSelf( tdir );
  2620. }
  2621. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2622. face = this.faces[ f ];
  2623. uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents
  2624. if ( face instanceof THREE.Face3 ) {
  2625. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  2626. } else if ( face instanceof THREE.Face4 ) {
  2627. handleTriangle( this, face.a, face.b, face.d, 0, 1, 3 );
  2628. handleTriangle( this, face.b, face.c, face.d, 1, 2, 3 );
  2629. }
  2630. }
  2631. var faceIndex = [ 'a', 'b', 'c', 'd' ];
  2632. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2633. face = this.faces[ f ];
  2634. for ( i = 0; i < face.vertexNormals.length; i++ ) {
  2635. n.copy( face.vertexNormals[ i ] );
  2636. vertexIndex = face[ faceIndex[ i ] ];
  2637. t = tan1[ vertexIndex ];
  2638. // Gram-Schmidt orthogonalize
  2639. tmp.copy( t );
  2640. tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
  2641. // Calculate handedness
  2642. tmp2.cross( face.vertexNormals[ i ], t );
  2643. test = tmp2.dot( tan2[ vertexIndex ] );
  2644. w = (test < 0.0) ? -1.0 : 1.0;
  2645. face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w );
  2646. }
  2647. }
  2648. this.hasTangents = true;
  2649. },
  2650. computeBoundingBox: function () {
  2651. if ( ! this.boundingBox ) {
  2652. this.boundingBox = { min: new THREE.Vector3(), max: new THREE.Vector3() };
  2653. }
  2654. if ( this.vertices.length > 0 ) {
  2655. var position, firstPosition = this.vertices[ 0 ];
  2656. this.boundingBox.min.copy( firstPosition );
  2657. this.boundingBox.max.copy( firstPosition );
  2658. var min = this.boundingBox.min,
  2659. max = this.boundingBox.max;
  2660. for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
  2661. position = this.vertices[ v ];
  2662. if ( position.x < min.x ) {
  2663. min.x = position.x;
  2664. } else if ( position.x > max.x ) {
  2665. max.x = position.x;
  2666. }
  2667. if ( position.y < min.y ) {
  2668. min.y = position.y;
  2669. } else if ( position.y > max.y ) {
  2670. max.y = position.y;
  2671. }
  2672. if ( position.z < min.z ) {
  2673. min.z = position.z;
  2674. } else if ( position.z > max.z ) {
  2675. max.z = position.z;
  2676. }
  2677. }
  2678. } else {
  2679. this.boundingBox.min.set( 0, 0, 0 );
  2680. this.boundingBox.max.set( 0, 0, 0 );
  2681. }
  2682. },
  2683. computeBoundingSphere: function () {
  2684. if ( ! this.boundingSphere ) this.boundingSphere = { radius: 0 };
  2685. var radius, maxRadius = 0;
  2686. for ( var v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2687. radius = this.vertices[ v ].length();
  2688. if ( radius > maxRadius ) maxRadius = radius;
  2689. }
  2690. this.boundingSphere.radius = maxRadius;
  2691. },
  2692. /*
  2693. * Checks for duplicate vertices with hashmap.
  2694. * Duplicated vertices are removed
  2695. * and faces' vertices are updated.
  2696. */
  2697. mergeVertices: function() {
  2698. var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
  2699. var unique = [], changes = [];
  2700. var v, key;
  2701. var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
  2702. var precision = Math.pow( 10, precisionPoints );
  2703. var i,il, face;
  2704. for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
  2705. v = this.vertices[ i ];
  2706. key = [ Math.round( v.x * precision ), Math.round( v.y * precision ), Math.round( v.z * precision ) ].join( '_' );
  2707. if ( verticesMap[ key ] === undefined ) {
  2708. verticesMap[ key ] = i;
  2709. unique.push( this.vertices[ i ] );
  2710. changes[ i ] = unique.length - 1;
  2711. } else {
  2712. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  2713. changes[ i ] = changes[ verticesMap[ key ] ];
  2714. }
  2715. };
  2716. // Start to patch face indices
  2717. for( i = 0, il = this.faces.length; i < il; i ++ ) {
  2718. face = this.faces[ i ];
  2719. if ( face instanceof THREE.Face3 ) {
  2720. face.a = changes[ face.a ];
  2721. face.b = changes[ face.b ];
  2722. face.c = changes[ face.c ];
  2723. } else if ( face instanceof THREE.Face4 ) {
  2724. face.a = changes[ face.a ];
  2725. face.b = changes[ face.b ];
  2726. face.c = changes[ face.c ];
  2727. face.d = changes[ face.d ];
  2728. }
  2729. }
  2730. // Use unique set of vertices
  2731. this.vertices = unique;
  2732. }
  2733. };
  2734. THREE.GeometryCount = 0;
  2735. /**
  2736. * Spline from Tween.js, slightly optimized (and trashed)
  2737. * http://sole.github.com/tween.js/examples/05_spline.html
  2738. *
  2739. * @author mrdoob / http://mrdoob.com/
  2740. * @author alteredq / http://alteredqualia.com/
  2741. */
  2742. THREE.Spline = function ( points ) {
  2743. this.points = points;
  2744. var c = [], v3 = { x: 0, y: 0, z: 0 },
  2745. point, intPoint, weight, w2, w3,
  2746. pa, pb, pc, pd;
  2747. this.initFromArray = function( a ) {
  2748. this.points = [];
  2749. for ( var i = 0; i < a.length; i++ ) {
  2750. this.points[ i ] = { x: a[ i ][ 0 ], y: a[ i ][ 1 ], z: a[ i ][ 2 ] };
  2751. }
  2752. };
  2753. this.getPoint = function ( k ) {
  2754. point = ( this.points.length - 1 ) * k;
  2755. intPoint = Math.floor( point );
  2756. weight = point - intPoint;
  2757. c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1;
  2758. c[ 1 ] = intPoint;
  2759. c[ 2 ] = intPoint > this.points.length - 2 ? this.points.length - 1 : intPoint + 1;
  2760. c[ 3 ] = intPoint > this.points.length - 3 ? this.points.length - 1 : intPoint + 2;
  2761. pa = this.points[ c[ 0 ] ];
  2762. pb = this.points[ c[ 1 ] ];
  2763. pc = this.points[ c[ 2 ] ];
  2764. pd = this.points[ c[ 3 ] ];
  2765. w2 = weight * weight;
  2766. w3 = weight * w2;
  2767. v3.x = interpolate( pa.x, pb.x, pc.x, pd.x, weight, w2, w3 );
  2768. v3.y = interpolate( pa.y, pb.y, pc.y, pd.y, weight, w2, w3 );
  2769. v3.z = interpolate( pa.z, pb.z, pc.z, pd.z, weight, w2, w3 );
  2770. return v3;
  2771. };
  2772. this.getControlPointsArray = function () {
  2773. var i, p, l = this.points.length,
  2774. coords = [];
  2775. for ( i = 0; i < l; i ++ ) {
  2776. p = this.points[ i ];
  2777. coords[ i ] = [ p.x, p.y, p.z ];
  2778. }
  2779. return coords;
  2780. };
  2781. // approximate length by summing linear segments
  2782. this.getLength = function ( nSubDivisions ) {
  2783. var i, index, nSamples, position,
  2784. point = 0, intPoint = 0, oldIntPoint = 0,
  2785. oldPosition = new THREE.Vector3(),
  2786. tmpVec = new THREE.Vector3(),
  2787. chunkLengths = [],
  2788. totalLength = 0;
  2789. // first point has 0 length
  2790. chunkLengths[ 0 ] = 0;
  2791. if ( !nSubDivisions ) nSubDivisions = 100;
  2792. nSamples = this.points.length * nSubDivisions;
  2793. oldPosition.copy( this.points[ 0 ] );
  2794. for ( i = 1; i < nSamples; i ++ ) {
  2795. index = i / nSamples;
  2796. position = this.getPoint( index );
  2797. tmpVec.copy( position );
  2798. totalLength += tmpVec.distanceTo( oldPosition );
  2799. oldPosition.copy( position );
  2800. point = ( this.points.length - 1 ) * index;
  2801. intPoint = Math.floor( point );
  2802. if ( intPoint != oldIntPoint ) {
  2803. chunkLengths[ intPoint ] = totalLength;
  2804. oldIntPoint = intPoint;
  2805. }
  2806. }
  2807. // last point ends with total length
  2808. chunkLengths[ chunkLengths.length ] = totalLength;
  2809. return { chunks: chunkLengths, total: totalLength };
  2810. };
  2811. this.reparametrizeByArcLength = function ( samplingCoef ) {
  2812. var i, j,
  2813. index, indexCurrent, indexNext,
  2814. linearDistance, realDistance,
  2815. sampling, position,
  2816. newpoints = [],
  2817. tmpVec = new THREE.Vector3(),
  2818. sl = this.getLength();
  2819. newpoints.push( tmpVec.copy( this.points[ 0 ] ).clone() );
  2820. for ( i = 1; i < this.points.length; i++ ) {
  2821. //tmpVec.copy( this.points[ i - 1 ] );
  2822. //linearDistance = tmpVec.distanceTo( this.points[ i ] );
  2823. realDistance = sl.chunks[ i ] - sl.chunks[ i - 1 ];
  2824. sampling = Math.ceil( samplingCoef * realDistance / sl.total );
  2825. indexCurrent = ( i - 1 ) / ( this.points.length - 1 );
  2826. indexNext = i / ( this.points.length - 1 );
  2827. for ( j = 1; j < sampling - 1; j++ ) {
  2828. index = indexCurrent + j * ( 1 / sampling ) * ( indexNext - indexCurrent );
  2829. position = this.getPoint( index );
  2830. newpoints.push( tmpVec.copy( position ).clone() );
  2831. }
  2832. newpoints.push( tmpVec.copy( this.points[ i ] ).clone() );
  2833. }
  2834. this.points = newpoints;
  2835. };
  2836. // Catmull-Rom
  2837. function interpolate( p0, p1, p2, p3, t, t2, t3 ) {
  2838. var v0 = ( p2 - p0 ) * 0.5,
  2839. v1 = ( p3 - p1 ) * 0.5;
  2840. return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  2841. };
  2842. };
  2843. /**
  2844. * @author mr.doob / http://mrdoob.com/
  2845. * @author mikael emtinger / http://gomo.se/
  2846. */
  2847. THREE.Camera = function () {
  2848. THREE.Object3D.call( this );
  2849. this.matrixWorldInverse = new THREE.Matrix4();
  2850. this.projectionMatrix = new THREE.Matrix4();
  2851. this.projectionMatrixInverse = new THREE.Matrix4();
  2852. };
  2853. THREE.Camera.prototype = new THREE.Object3D();
  2854. THREE.Camera.prototype.constructor = THREE.Camera;
  2855. THREE.Camera.prototype.lookAt = function ( vector ) {
  2856. // TODO: Add hierarchy support.
  2857. this.matrix.lookAt( this.position, vector, this.up );
  2858. if ( this.rotationAutoUpdate ) {
  2859. this.rotation.getRotationFromMatrix( this.matrix );
  2860. }
  2861. };
  2862. /**
  2863. * @author alteredq / http://alteredqualia.com/
  2864. */
  2865. THREE.OrthographicCamera = function ( left, right, top, bottom, near, far ) {
  2866. THREE.Camera.call( this );
  2867. this.left = left;
  2868. this.right = right;
  2869. this.top = top;
  2870. this.bottom = bottom;
  2871. this.near = ( near !== undefined ) ? near : 0.1;
  2872. this.far = ( far !== undefined ) ? far : 2000;
  2873. this.updateProjectionMatrix();
  2874. };
  2875. THREE.OrthographicCamera.prototype = new THREE.Camera();
  2876. THREE.OrthographicCamera.prototype.constructor = THREE.OrthographicCamera;
  2877. THREE.OrthographicCamera.prototype.updateProjectionMatrix = function () {
  2878. this.projectionMatrix.makeOrthographic( this.left, this.right, this.top, this.bottom, this.near, this.far );
  2879. };
  2880. /**
  2881. * @author mr.doob / http://mrdoob.com/
  2882. * @author greggman / http://games.greggman.com/
  2883. * @author zz85 / http://www.lab4games.net/zz85/blog
  2884. */
  2885. THREE.PerspectiveCamera = function ( fov, aspect, near, far ) {
  2886. THREE.Camera.call( this );
  2887. this.fov = fov !== undefined ? fov : 50;
  2888. this.aspect = aspect !== undefined ? aspect : 1;
  2889. this.near = near !== undefined ? near : 0.1;
  2890. this.far = far !== undefined ? far : 2000;
  2891. this.updateProjectionMatrix();
  2892. };
  2893. THREE.PerspectiveCamera.prototype = new THREE.Camera();
  2894. THREE.PerspectiveCamera.prototype.constructor = THREE.PerspectiveCamera;
  2895. /**
  2896. * Uses Focal Length (in mm) to estimate and set FOV
  2897. * 35mm (fullframe) camera is used if frame size is not specified;
  2898. * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html
  2899. */
  2900. THREE.PerspectiveCamera.prototype.setLens = function ( focalLength, frameHeight ) {
  2901. frameHeight = frameHeight !== undefined ? frameHeight : 24;
  2902. this.fov = 2 * Math.atan( frameHeight / ( focalLength * 2 ) ) * ( 180 / Math.PI );
  2903. this.updateProjectionMatrix();
  2904. }
  2905. /**
  2906. * Sets an offset in a larger frustum. This is useful for multi-window or
  2907. * multi-monitor/multi-machine setups.
  2908. *
  2909. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  2910. * the monitors are in grid like this
  2911. *
  2912. * +---+---+---+
  2913. * | A | B | C |
  2914. * +---+---+---+
  2915. * | D | E | F |
  2916. * +---+---+---+
  2917. *
  2918. * then for each monitor you would call it like this
  2919. *
  2920. * var w = 1920;
  2921. * var h = 1080;
  2922. * var fullWidth = w * 3;
  2923. * var fullHeight = h * 2;
  2924. *
  2925. * --A--
  2926. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  2927. * --B--
  2928. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  2929. * --C--
  2930. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  2931. * --D--
  2932. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  2933. * --E--
  2934. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  2935. * --F--
  2936. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  2937. *
  2938. * Note there is no reason monitors have to be the same size or in a grid.
  2939. */
  2940. THREE.PerspectiveCamera.prototype.setViewOffset = function ( fullWidth, fullHeight, x, y, width, height ) {
  2941. this.fullWidth = fullWidth;
  2942. this.fullHeight = fullHeight;
  2943. this.x = x;
  2944. this.y = y;
  2945. this.width = width;
  2946. this.height = height;
  2947. this.updateProjectionMatrix();
  2948. };
  2949. THREE.PerspectiveCamera.prototype.updateProjectionMatrix = function () {
  2950. if ( this.fullWidth ) {
  2951. var aspect = this.fullWidth / this.fullHeight;
  2952. var top = Math.tan( this.fov * Math.PI / 360 ) * this.near;
  2953. var bottom = -top;
  2954. var left = aspect * bottom;
  2955. var right = aspect * top;
  2956. var width = Math.abs( right - left );
  2957. var height = Math.abs( top - bottom );
  2958. this.projectionMatrix.makeFrustum(
  2959. left + this.x * width / this.fullWidth,
  2960. left + ( this.x + this.width ) * width / this.fullWidth,
  2961. top - ( this.y + this.height ) * height / this.fullHeight,
  2962. top - this.y * height / this.fullHeight,
  2963. this.near,
  2964. this.far
  2965. );
  2966. } else {
  2967. this.projectionMatrix.makePerspective( this.fov, this.aspect, this.near, this.far );
  2968. }
  2969. };
  2970. /**
  2971. * @author mr.doob / http://mrdoob.com/
  2972. * @author alteredq / http://alteredqualia.com/
  2973. */
  2974. THREE.Light = function ( hex ) {
  2975. THREE.Object3D.call( this );
  2976. this.color = new THREE.Color( hex );
  2977. };
  2978. THREE.Light.prototype = new THREE.Object3D();
  2979. THREE.Light.prototype.constructor = THREE.Light;
  2980. THREE.Light.prototype.supr = THREE.Object3D.prototype;
  2981. /**
  2982. * @author mr.doob / http://mrdoob.com/
  2983. */
  2984. THREE.AmbientLight = function ( hex ) {
  2985. THREE.Light.call( this, hex );
  2986. };
  2987. THREE.AmbientLight.prototype = new THREE.Light();
  2988. THREE.AmbientLight.prototype.constructor = THREE.AmbientLight;
  2989. /**
  2990. * @author mr.doob / http://mrdoob.com/
  2991. * @author alteredq / http://alteredqualia.com/
  2992. */
  2993. THREE.DirectionalLight = function ( hex, intensity, distance ) {
  2994. THREE.Light.call( this, hex );
  2995. this.position = new THREE.Vector3( 0, 1, 0 );
  2996. this.target = new THREE.Object3D();
  2997. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  2998. this.distance = ( distance !== undefined ) ? distance : 0;
  2999. this.castShadow = false;
  3000. this.onlyShadow = false;
  3001. //
  3002. this.shadowCameraNear = 50;
  3003. this.shadowCameraFar = 5000;
  3004. this.shadowCameraLeft = -500;
  3005. this.shadowCameraRight = 500;
  3006. this.shadowCameraTop = 500;
  3007. this.shadowCameraBottom = -500;
  3008. this.shadowCameraVisible = false;
  3009. this.shadowBias = 0;
  3010. this.shadowDarkness = 0.5;
  3011. this.shadowMapWidth = 512;
  3012. this.shadowMapHeight = 512;
  3013. //
  3014. this.shadowCascade = false;
  3015. this.shadowCascadeOffset = new THREE.Vector3( 0, 0, -1000 );
  3016. this.shadowCascadeCount = 2;
  3017. this.shadowCascadeBias = [ 0, 0, 0 ];
  3018. this.shadowCascadeWidth = [ 512, 512, 512 ];
  3019. this.shadowCascadeHeight = [ 512, 512, 512 ];
  3020. this.shadowCascadeNearZ = [ -1.000, 0.990, 0.998 ];
  3021. this.shadowCascadeFarZ = [ 0.990, 0.998, 1.000 ];
  3022. this.shadowCascadeArray = [];
  3023. //
  3024. this.shadowMap = null;
  3025. this.shadowMapSize = null;
  3026. this.shadowCamera = null;
  3027. this.shadowMatrix = null;
  3028. };
  3029. THREE.DirectionalLight.prototype = new THREE.Light();
  3030. THREE.DirectionalLight.prototype.constructor = THREE.DirectionalLight;
  3031. /**
  3032. * @author mr.doob / http://mrdoob.com/
  3033. */
  3034. THREE.PointLight = function ( hex, intensity, distance ) {
  3035. THREE.Light.call( this, hex );
  3036. this.position = new THREE.Vector3( 0, 0, 0 );
  3037. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  3038. this.distance = ( distance !== undefined ) ? distance : 0;
  3039. };
  3040. THREE.PointLight.prototype = new THREE.Light();
  3041. THREE.PointLight.prototype.constructor = THREE.PointLight;
  3042. /**
  3043. * @author alteredq / http://alteredqualia.com/
  3044. */
  3045. THREE.SpotLight = function ( hex, intensity, distance ) {
  3046. THREE.Light.call( this, hex );
  3047. this.position = new THREE.Vector3( 0, 1, 0 );
  3048. this.target = new THREE.Object3D();
  3049. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  3050. this.distance = ( distance !== undefined ) ? distance : 0;
  3051. this.castShadow = false;
  3052. this.onlyShadow = false;
  3053. //
  3054. this.shadowCameraNear = 50;
  3055. this.shadowCameraFar = 5000;
  3056. this.shadowCameraFov = 50;
  3057. this.shadowCameraVisible = false;
  3058. this.shadowBias = 0;
  3059. this.shadowDarkness = 0.5;
  3060. this.shadowMapWidth = 512;
  3061. this.shadowMapHeight = 512;
  3062. //
  3063. this.shadowMap = null;
  3064. this.shadowMapSize = null;
  3065. this.shadowCamera = null;
  3066. this.shadowMatrix = null;
  3067. };
  3068. THREE.SpotLight.prototype = new THREE.Light();
  3069. THREE.SpotLight.prototype.constructor = THREE.SpotLight;
  3070. /**
  3071. * @author mr.doob / http://mrdoob.com/
  3072. * @author alteredq / http://alteredqualia.com/
  3073. */
  3074. THREE.Material = function ( parameters ) {
  3075. parameters = parameters || {};
  3076. this.id = THREE.MaterialCount ++;
  3077. this.name = '';
  3078. this.opacity = parameters.opacity !== undefined ? parameters.opacity : 1;
  3079. this.transparent = parameters.transparent !== undefined ? parameters.transparent : false;
  3080. this.blending = parameters.blending !== undefined ? parameters.blending : THREE.NormalBlending;
  3081. this.blendSrc = parameters.blendSrc !== undefined ? parameters.blendSrc : THREE.SrcAlphaFactor;
  3082. this.blendDst = parameters.blendDst !== undefined ? parameters.blendDst : THREE.OneMinusSrcAlphaFactor;
  3083. this.blendEquation = parameters.blendEquation !== undefined ? parameters.blendEquation : THREE.AddEquation;
  3084. this.depthTest = parameters.depthTest !== undefined ? parameters.depthTest : true;
  3085. this.depthWrite = parameters.depthWrite !== undefined ? parameters.depthWrite : true;
  3086. this.polygonOffset = parameters.polygonOffset !== undefined ? parameters.polygonOffset : false;
  3087. this.polygonOffsetFactor = parameters.polygonOffsetFactor !== undefined ? parameters.polygonOffsetFactor : 0;
  3088. this.polygonOffsetUnits = parameters.polygonOffsetUnits !== undefined ? parameters.polygonOffsetUnits : 0;
  3089. this.alphaTest = parameters.alphaTest !== undefined ? parameters.alphaTest : 0;
  3090. this.overdraw = parameters.overdraw !== undefined ? parameters.overdraw : false; // Boolean for fixing antialiasing gaps in CanvasRenderer
  3091. this.needsUpdate = true;
  3092. }
  3093. THREE.MaterialCount = 0;
  3094. // shading
  3095. THREE.NoShading = 0;
  3096. THREE.FlatShading = 1;
  3097. THREE.SmoothShading = 2;
  3098. // colors
  3099. THREE.NoColors = 0;
  3100. THREE.FaceColors = 1;
  3101. THREE.VertexColors = 2;
  3102. // blending modes
  3103. THREE.NoBlending = 0;
  3104. THREE.NormalBlending = 1;
  3105. THREE.AdditiveBlending = 2;
  3106. THREE.SubtractiveBlending = 3;
  3107. THREE.MultiplyBlending = 4;
  3108. THREE.AdditiveAlphaBlending = 5;
  3109. THREE.CustomBlending = 6;
  3110. // custom blending equations
  3111. // (numbers start from 100 not to clash with other
  3112. // mappings to OpenGL constants defined in Texture.js)
  3113. THREE.AddEquation = 100;
  3114. THREE.SubtractEquation = 101;
  3115. THREE.ReverseSubtractEquation = 102;
  3116. // custom blending destination factors
  3117. THREE.ZeroFactor = 200;
  3118. THREE.OneFactor = 201;
  3119. THREE.SrcColorFactor = 202;
  3120. THREE.OneMinusSrcColorFactor = 203;
  3121. THREE.SrcAlphaFactor = 204;
  3122. THREE.OneMinusSrcAlphaFactor = 205;
  3123. THREE.DstAlphaFactor = 206;
  3124. THREE.OneMinusDstAlphaFactor = 207;
  3125. // custom blending source factors
  3126. //THREE.ZeroFactor = 200;
  3127. //THREE.OneFactor = 201;
  3128. //THREE.SrcAlphaFactor = 204;
  3129. //THREE.OneMinusSrcAlphaFactor = 205;
  3130. //THREE.DstAlphaFactor = 206;
  3131. //THREE.OneMinusDstAlphaFactor = 207;
  3132. THREE.DstColorFactor = 208;
  3133. THREE.OneMinusDstColorFactor = 209;
  3134. THREE.SrcAlphaSaturateFactor = 210;
  3135. /**
  3136. * @author mr.doob / http://mrdoob.com/
  3137. * @author alteredq / http://alteredqualia.com/
  3138. *
  3139. * parameters = {
  3140. * color: <hex>,
  3141. * opacity: <float>,
  3142. *
  3143. * blending: THREE.NormalBlending,
  3144. * depthTest: <bool>,
  3145. *
  3146. * linewidth: <float>,
  3147. * linecap: "round",
  3148. * linejoin: "round",
  3149. *
  3150. * vertexColors: <bool>
  3151. *
  3152. * fog: <bool>
  3153. * }
  3154. */
  3155. THREE.LineBasicMaterial = function ( parameters ) {
  3156. THREE.Material.call( this, parameters );
  3157. parameters = parameters || {};
  3158. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3159. this.linewidth = parameters.linewidth !== undefined ? parameters.linewidth : 1;
  3160. this.linecap = parameters.linecap !== undefined ? parameters.linecap : 'round';
  3161. this.linejoin = parameters.linejoin !== undefined ? parameters.linejoin : 'round';
  3162. this.vertexColors = parameters.vertexColors ? parameters.vertexColors : false;
  3163. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3164. };
  3165. THREE.LineBasicMaterial.prototype = new THREE.Material();
  3166. THREE.LineBasicMaterial.prototype.constructor = THREE.LineBasicMaterial;
  3167. /**
  3168. * @author mr.doob / http://mrdoob.com/
  3169. * @author alteredq / http://alteredqualia.com/
  3170. *
  3171. * parameters = {
  3172. * color: <hex>,
  3173. * opacity: <float>,
  3174. * map: new THREE.Texture( <Image> ),
  3175. *
  3176. * lightMap: new THREE.Texture( <Image> ),
  3177. *
  3178. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3179. * combine: THREE.Multiply,
  3180. * reflectivity: <float>,
  3181. * refractionRatio: <float>,
  3182. *
  3183. * shading: THREE.SmoothShading,
  3184. * blending: THREE.NormalBlending,
  3185. * depthTest: <bool>,
  3186. *
  3187. * wireframe: <boolean>,
  3188. * wireframeLinewidth: <float>,
  3189. *
  3190. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3191. *
  3192. * skinning: <bool>,
  3193. * morphTargets: <bool>,
  3194. *
  3195. * fog: <bool>
  3196. * }
  3197. */
  3198. THREE.MeshBasicMaterial = function ( parameters ) {
  3199. THREE.Material.call( this, parameters );
  3200. parameters = parameters || {};
  3201. // color property represents emissive for MeshBasicMaterial
  3202. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3203. this.map = parameters.map !== undefined ? parameters.map : null;
  3204. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3205. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3206. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3207. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3208. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3209. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3210. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3211. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3212. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3213. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3214. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3215. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3216. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3217. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3218. };
  3219. THREE.MeshBasicMaterial.prototype = new THREE.Material();
  3220. THREE.MeshBasicMaterial.prototype.constructor = THREE.MeshBasicMaterial;
  3221. /**
  3222. * @author mr.doob / http://mrdoob.com/
  3223. * @author alteredq / http://alteredqualia.com/
  3224. *
  3225. * parameters = {
  3226. * color: <hex>,
  3227. * ambient: <hex>,
  3228. * emissive: <hex>,
  3229. * opacity: <float>,
  3230. *
  3231. * map: new THREE.Texture( <Image> ),
  3232. *
  3233. * lightMap: new THREE.Texture( <Image> ),
  3234. *
  3235. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3236. * combine: THREE.Multiply,
  3237. * reflectivity: <float>,
  3238. * refractionRatio: <float>,
  3239. *
  3240. * shading: THREE.SmoothShading,
  3241. * blending: THREE.NormalBlending,
  3242. * depthTest: <bool>,
  3243. *
  3244. * wireframe: <boolean>,
  3245. * wireframeLinewidth: <float>,
  3246. *
  3247. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3248. *
  3249. * skinning: <bool>,
  3250. * morphTargets: <bool>,
  3251. * morphNormals: <bool>,
  3252. *
  3253. * fog: <bool>
  3254. * }
  3255. */
  3256. THREE.MeshLambertMaterial = function ( parameters ) {
  3257. THREE.Material.call( this, parameters );
  3258. parameters = parameters || {};
  3259. // color property represents diffuse for MeshLambertMaterial
  3260. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3261. this.ambient = parameters.ambient !== undefined ? new THREE.Color( parameters.ambient ) : new THREE.Color( 0xffffff );
  3262. this.emissive = parameters.emissive !== undefined ? new THREE.Color( parameters.emissive ) : new THREE.Color( 0x000000 );
  3263. this.wrapAround = parameters.wrapAround !== undefined ? parameters.wrapAround: false;
  3264. this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
  3265. this.map = parameters.map !== undefined ? parameters.map : null;
  3266. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3267. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3268. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3269. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3270. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3271. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3272. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3273. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3274. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3275. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3276. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3277. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3278. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3279. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3280. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false;
  3281. };
  3282. THREE.MeshLambertMaterial.prototype = new THREE.Material();
  3283. THREE.MeshLambertMaterial.prototype.constructor = THREE.MeshLambertMaterial;
  3284. /**
  3285. * @author mr.doob / http://mrdoob.com/
  3286. * @author alteredq / http://alteredqualia.com/
  3287. *
  3288. * parameters = {
  3289. * color: <hex>,
  3290. * ambient: <hex>,
  3291. * emissive: <hex>,
  3292. * specular: <hex>,
  3293. * shininess: <float>,
  3294. * opacity: <float>,
  3295. *
  3296. * map: new THREE.Texture( <Image> ),
  3297. *
  3298. * lightMap: new THREE.Texture( <Image> ),
  3299. *
  3300. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3301. * combine: THREE.Multiply,
  3302. * reflectivity: <float>,
  3303. * refractionRatio: <float>,
  3304. *
  3305. * shading: THREE.SmoothShading,
  3306. * blending: THREE.NormalBlending,
  3307. * depthTest: <bool>,
  3308. *
  3309. * wireframe: <boolean>,
  3310. * wireframeLinewidth: <float>,
  3311. *
  3312. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3313. *
  3314. * skinning: <bool>,
  3315. * morphTargets: <bool>,
  3316. * morphNormals: <bool>,
  3317. *
  3318. * fog: <bool>
  3319. * }
  3320. */
  3321. THREE.MeshPhongMaterial = function ( parameters ) {
  3322. THREE.Material.call( this, parameters );
  3323. parameters = parameters || {};
  3324. // color property represents diffuse for MeshPhongMaterial
  3325. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3326. this.ambient = parameters.ambient !== undefined ? new THREE.Color( parameters.ambient ) : new THREE.Color( 0xffffff );
  3327. this.emissive = parameters.emissive !== undefined ? new THREE.Color( parameters.emissive ) : new THREE.Color( 0x000000 );
  3328. this.specular = parameters.specular !== undefined ? new THREE.Color( parameters.specular ) : new THREE.Color( 0x111111 );
  3329. this.shininess = parameters.shininess !== undefined ? parameters.shininess : 30;
  3330. this.metal = parameters.metal !== undefined ? parameters.metal : false;
  3331. this.perPixel = parameters.perPixel !== undefined ? parameters.perPixel : false;
  3332. this.wrapAround = parameters.wrapAround !== undefined ? parameters.wrapAround: false;
  3333. this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
  3334. this.map = parameters.map !== undefined ? parameters.map : null;
  3335. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3336. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3337. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3338. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3339. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3340. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3341. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3342. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3343. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3344. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3345. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3346. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3347. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3348. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3349. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false;
  3350. };
  3351. THREE.MeshPhongMaterial.prototype = new THREE.Material();
  3352. THREE.MeshPhongMaterial.prototype.constructor = THREE.MeshPhongMaterial;
  3353. /**
  3354. * @author mr.doob / http://mrdoob.com/
  3355. * @author alteredq / http://alteredqualia.com/
  3356. *
  3357. * parameters = {
  3358. * opacity: <float>,
  3359. * blending: THREE.NormalBlending,
  3360. * depthTest: <bool>,
  3361. * wireframe: <boolean>,
  3362. * wireframeLinewidth: <float>
  3363. * }
  3364. */
  3365. THREE.MeshDepthMaterial = function ( parameters ) {
  3366. THREE.Material.call( this, parameters );
  3367. parameters = parameters || {};
  3368. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading; // doesn't really apply here, normals are not used
  3369. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3370. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3371. };
  3372. THREE.MeshDepthMaterial.prototype = new THREE.Material();
  3373. THREE.MeshDepthMaterial.prototype.constructor = THREE.MeshDepthMaterial;
  3374. /**
  3375. * @author mr.doob / http://mrdoob.com/
  3376. *
  3377. * parameters = {
  3378. * opacity: <float>,
  3379. * shading: THREE.FlatShading,
  3380. * blending: THREE.NormalBlending,
  3381. * depthTest: <bool>,
  3382. * wireframe: <boolean>,
  3383. * wireframeLinewidth: <float>
  3384. * }
  3385. */
  3386. THREE.MeshNormalMaterial = function ( parameters ) {
  3387. THREE.Material.call( this, parameters );
  3388. parameters = parameters || {};
  3389. this.shading = parameters.shading ? parameters.shading : THREE.FlatShading;
  3390. this.wireframe = parameters.wireframe ? parameters.wireframe : false;
  3391. this.wireframeLinewidth = parameters.wireframeLinewidth ? parameters.wireframeLinewidth : 1;
  3392. };
  3393. THREE.MeshNormalMaterial.prototype = new THREE.Material();
  3394. THREE.MeshNormalMaterial.prototype.constructor = THREE.MeshNormalMaterial;
  3395. /**
  3396. * @author mr.doob / http://mrdoob.com/
  3397. */
  3398. THREE.MeshFaceMaterial = function () {
  3399. };
  3400. /**
  3401. * @author mr.doob / http://mrdoob.com/
  3402. * @author alteredq / http://alteredqualia.com/
  3403. *
  3404. * parameters = {
  3405. * color: <hex>,
  3406. * opacity: <float>,
  3407. * map: new THREE.Texture( <Image> ),
  3408. *
  3409. * size: <float>,
  3410. *
  3411. * blending: THREE.NormalBlending,
  3412. * depthTest: <bool>,
  3413. *
  3414. * vertexColors: <bool>,
  3415. *
  3416. * fog: <bool>
  3417. * }
  3418. */
  3419. THREE.ParticleBasicMaterial = function ( parameters ) {
  3420. THREE.Material.call( this, parameters );
  3421. parameters = parameters || {};
  3422. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3423. this.map = parameters.map !== undefined ? parameters.map : null;
  3424. this.size = parameters.size !== undefined ? parameters.size : 1;
  3425. this.sizeAttenuation = parameters.sizeAttenuation !== undefined ? parameters.sizeAttenuation : true;
  3426. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : false;
  3427. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3428. };
  3429. THREE.ParticleBasicMaterial.prototype = new THREE.Material();
  3430. THREE.ParticleBasicMaterial.prototype.constructor = THREE.ParticleBasicMaterial;
  3431. /**
  3432. * @author alteredq / http://alteredqualia.com/
  3433. *
  3434. * parameters = {
  3435. * fragmentShader: <string>,
  3436. * vertexShader: <string>,
  3437. *
  3438. * uniforms: { "parameter1": { type: "f", value: 1.0 }, "parameter2": { type: "i" value2: 2 } },
  3439. *
  3440. * shading: THREE.SmoothShading,
  3441. * blending: THREE.NormalBlending,
  3442. * depthTest: <bool>,
  3443. *
  3444. * wireframe: <boolean>,
  3445. * wireframeLinewidth: <float>,
  3446. *
  3447. * lights: <bool>,
  3448. *
  3449. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3450. *
  3451. * skinning: <bool>,
  3452. * morphTargets: <bool>,
  3453. * morphNormals: <bool>,
  3454. *
  3455. * fog: <bool>
  3456. * }
  3457. */
  3458. THREE.ShaderMaterial = function ( parameters ) {
  3459. THREE.Material.call( this, parameters );
  3460. parameters = parameters || {};
  3461. this.fragmentShader = parameters.fragmentShader !== undefined ? parameters.fragmentShader : "void main() {}";
  3462. this.vertexShader = parameters.vertexShader !== undefined ? parameters.vertexShader : "void main() {}";
  3463. this.uniforms = parameters.uniforms !== undefined ? parameters.uniforms : {};
  3464. this.attributes = parameters.attributes;
  3465. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3466. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3467. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3468. this.fog = parameters.fog !== undefined ? parameters.fog : false; // set to use scene fog
  3469. this.lights = parameters.lights !== undefined ? parameters.lights : false; // set to use scene lights
  3470. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors; // set to use "color" attribute stream
  3471. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false; // set to use skinning attribute streams
  3472. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false; // set to use morph targets
  3473. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false; // set to use morph normals
  3474. };
  3475. THREE.ShaderMaterial.prototype = new THREE.Material();
  3476. THREE.ShaderMaterial.prototype.constructor = THREE.ShaderMaterial;
  3477. /**
  3478. * @author mr.doob / http://mrdoob.com/
  3479. * @author alteredq / http://alteredqualia.com/
  3480. * @author szimek / https://github.com/szimek/
  3481. */
  3482. THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type ) {
  3483. this.id = THREE.TextureCount ++;
  3484. this.image = image;
  3485. this.mapping = mapping !== undefined ? mapping : new THREE.UVMapping();
  3486. this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
  3487. this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
  3488. this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
  3489. this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;
  3490. this.format = format !== undefined ? format : THREE.RGBAFormat;
  3491. this.type = type !== undefined ? type : THREE.UnsignedByteType;
  3492. this.offset = new THREE.Vector2( 0, 0 );
  3493. this.repeat = new THREE.Vector2( 1, 1 );
  3494. this.generateMipmaps = true;
  3495. this.premultiplyAlpha = false;
  3496. this.needsUpdate = false;
  3497. this.onUpdate = null;
  3498. };
  3499. THREE.Texture.prototype = {
  3500. constructor: THREE.Texture,
  3501. clone: function () {
  3502. var clonedTexture = new THREE.Texture( this.image, this.mapping, this.wrapS, this.wrapT, this.magFilter, this.minFilter, this.format, this.type );
  3503. clonedTexture.offset.copy( this.offset );
  3504. clonedTexture.repeat.copy( this.repeat );
  3505. return clonedTexture;
  3506. }
  3507. };
  3508. THREE.TextureCount = 0;
  3509. THREE.MultiplyOperation = 0;
  3510. THREE.MixOperation = 1;
  3511. // Mapping modes
  3512. THREE.UVMapping = function () {};
  3513. THREE.CubeReflectionMapping = function () {};
  3514. THREE.CubeRefractionMapping = function () {};
  3515. THREE.SphericalReflectionMapping = function () {};
  3516. THREE.SphericalRefractionMapping = function () {};
  3517. // Wrapping modes
  3518. THREE.RepeatWrapping = 0;
  3519. THREE.ClampToEdgeWrapping = 1;
  3520. THREE.MirroredRepeatWrapping = 2;
  3521. // Filters
  3522. THREE.NearestFilter = 3;
  3523. THREE.NearestMipMapNearestFilter = 4;
  3524. THREE.NearestMipMapLinearFilter = 5;
  3525. THREE.LinearFilter = 6;
  3526. THREE.LinearMipMapNearestFilter = 7;
  3527. THREE.LinearMipMapLinearFilter = 8;
  3528. // Types
  3529. THREE.ByteType = 9;
  3530. THREE.UnsignedByteType = 10;
  3531. THREE.ShortType = 11;
  3532. THREE.UnsignedShortType = 12;
  3533. THREE.IntType = 13;
  3534. THREE.UnsignedIntType = 14;
  3535. THREE.FloatType = 15;
  3536. // Formats
  3537. THREE.AlphaFormat = 16;
  3538. THREE.RGBFormat = 17;
  3539. THREE.RGBAFormat = 18;
  3540. THREE.LuminanceFormat = 19;
  3541. THREE.LuminanceAlphaFormat = 20;
  3542. /**
  3543. * @author alteredq / http://alteredqualia.com/
  3544. */
  3545. THREE.DataTexture = function ( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter ) {
  3546. THREE.Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type );
  3547. this.image = { data: data, width: width, height: height };
  3548. };
  3549. THREE.DataTexture.prototype = new THREE.Texture();
  3550. THREE.DataTexture.prototype.constructor = THREE.DataTexture;
  3551. THREE.DataTexture.prototype.clone = function () {
  3552. 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 );
  3553. clonedTexture.offset.copy( this.offset );
  3554. clonedTexture.repeat.copy( this.repeat );
  3555. return clonedTexture;
  3556. };
  3557. /**
  3558. * @author mr.doob / http://mrdoob.com/
  3559. */
  3560. THREE.Particle = function ( material ) {
  3561. THREE.Object3D.call( this );
  3562. this.material = material;
  3563. };
  3564. THREE.Particle.prototype = new THREE.Object3D();
  3565. THREE.Particle.prototype.constructor = THREE.Particle;
  3566. /**
  3567. * @author alteredq / http://alteredqualia.com/
  3568. */
  3569. THREE.ParticleSystem = function ( geometry, material ) {
  3570. THREE.Object3D.call( this );
  3571. this.geometry = geometry;
  3572. this.material = ( material !== undefined ) ? material : new THREE.ParticleBasicMaterial( { color: Math.random() * 0xffffff } );
  3573. this.sortParticles = false;
  3574. if ( this.geometry ) {
  3575. // calc bound radius
  3576. if( !this.geometry.boundingSphere ) {
  3577. this.geometry.computeBoundingSphere();
  3578. }
  3579. this.boundRadius = geometry.boundingSphere.radius;
  3580. }
  3581. this.frustumCulled = false;
  3582. };
  3583. THREE.ParticleSystem.prototype = new THREE.Object3D();
  3584. THREE.ParticleSystem.prototype.constructor = THREE.ParticleSystem;
  3585. /**
  3586. * @author mr.doob / http://mrdoob.com/
  3587. */
  3588. THREE.Line = function ( geometry, material, type ) {
  3589. THREE.Object3D.call( this );
  3590. this.geometry = geometry;
  3591. this.material = ( material !== undefined ) ? material : new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff } );
  3592. this.type = ( type !== undefined ) ? type : THREE.LineStrip;
  3593. if ( this.geometry ) {
  3594. if ( ! this.geometry.boundingSphere ) {
  3595. this.geometry.computeBoundingSphere();
  3596. }
  3597. }
  3598. };
  3599. THREE.LineStrip = 0;
  3600. THREE.LinePieces = 1;
  3601. THREE.Line.prototype = new THREE.Object3D();
  3602. THREE.Line.prototype.constructor = THREE.Line;
  3603. /**
  3604. * @author mr.doob / http://mrdoob.com/
  3605. * @author alteredq / http://alteredqualia.com/
  3606. * @author mikael emtinger / http://gomo.se/
  3607. */
  3608. THREE.Mesh = function ( geometry, material ) {
  3609. THREE.Object3D.call( this );
  3610. this.geometry = geometry;
  3611. this.material = ( material !== undefined ) ? material : new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: true } );
  3612. if ( this.geometry ) {
  3613. // calc bound radius
  3614. if( ! this.geometry.boundingSphere ) {
  3615. this.geometry.computeBoundingSphere();
  3616. }
  3617. this.boundRadius = geometry.boundingSphere.radius;
  3618. // setup morph targets
  3619. if( this.geometry.morphTargets.length ) {
  3620. this.morphTargetBase = -1;
  3621. this.morphTargetForcedOrder = [];
  3622. this.morphTargetInfluences = [];
  3623. this.morphTargetDictionary = {};
  3624. for( var m = 0; m < this.geometry.morphTargets.length; m ++ ) {
  3625. this.morphTargetInfluences.push( 0 );
  3626. this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m;
  3627. }
  3628. }
  3629. }
  3630. }
  3631. THREE.Mesh.prototype = new THREE.Object3D();
  3632. THREE.Mesh.prototype.constructor = THREE.Mesh;
  3633. THREE.Mesh.prototype.supr = THREE.Object3D.prototype;
  3634. /*
  3635. * Get Morph Target Index by Name
  3636. */
  3637. THREE.Mesh.prototype.getMorphTargetIndexByName = function( name ) {
  3638. if ( this.morphTargetDictionary[ name ] !== undefined ) {
  3639. return this.morphTargetDictionary[ name ];
  3640. }
  3641. console.log( "THREE.Mesh.getMorphTargetIndexByName: morph target " + name + " does not exist. Returning 0." );
  3642. return 0;
  3643. }
  3644. /**
  3645. * @author mikael emtinger / http://gomo.se/
  3646. * @author alteredq / http://alteredqualia.com/
  3647. */
  3648. THREE.Bone = function( belongsToSkin ) {
  3649. THREE.Object3D.call( this );
  3650. this.skin = belongsToSkin;
  3651. this.skinMatrix = new THREE.Matrix4();
  3652. };
  3653. THREE.Bone.prototype = new THREE.Object3D();
  3654. THREE.Bone.prototype.constructor = THREE.Bone;
  3655. THREE.Bone.prototype.supr = THREE.Object3D.prototype;
  3656. THREE.Bone.prototype.update = function( parentSkinMatrix, forceUpdate ) {
  3657. // update local
  3658. if ( this.matrixAutoUpdate ) {
  3659. forceUpdate |= this.updateMatrix();
  3660. }
  3661. // update skin matrix
  3662. if ( forceUpdate || this.matrixWorldNeedsUpdate ) {
  3663. if( parentSkinMatrix ) {
  3664. this.skinMatrix.multiply( parentSkinMatrix, this.matrix );
  3665. } else {
  3666. this.skinMatrix.copy( this.matrix );
  3667. }
  3668. this.matrixWorldNeedsUpdate = false;
  3669. forceUpdate = true;
  3670. }
  3671. // update children
  3672. var child, i, l = this.children.length;
  3673. for ( i = 0; i < l; i ++ ) {
  3674. this.children[ i ].update( this.skinMatrix, forceUpdate );
  3675. }
  3676. };
  3677. /**
  3678. * @author mikael emtinger / http://gomo.se/
  3679. * @author alteredq / http://alteredqualia.com/
  3680. */
  3681. THREE.SkinnedMesh = function ( geometry, material ) {
  3682. THREE.Mesh.call( this, geometry, material );
  3683. // init bones
  3684. this.identityMatrix = new THREE.Matrix4();
  3685. this.bones = [];
  3686. this.boneMatrices = [];
  3687. var b, bone, gbone, p, q, s;
  3688. if ( this.geometry.bones !== undefined ) {
  3689. for ( b = 0; b < this.geometry.bones.length; b ++ ) {
  3690. gbone = this.geometry.bones[ b ];
  3691. p = gbone.pos;
  3692. q = gbone.rotq;
  3693. s = gbone.scl;
  3694. bone = this.addBone();
  3695. bone.name = gbone.name;
  3696. bone.position.set( p[0], p[1], p[2] );
  3697. bone.quaternion.set( q[0], q[1], q[2], q[3] );
  3698. bone.useQuaternion = true;
  3699. if ( s !== undefined ) {
  3700. bone.scale.set( s[0], s[1], s[2] );
  3701. } else {
  3702. bone.scale.set( 1, 1, 1 );
  3703. }
  3704. }
  3705. for ( b = 0; b < this.bones.length; b ++ ) {
  3706. gbone = this.geometry.bones[ b ];
  3707. bone = this.bones[ b ];
  3708. if ( gbone.parent === -1 ) {
  3709. this.add( bone );
  3710. } else {
  3711. this.bones[ gbone.parent ].add( bone );
  3712. }
  3713. }
  3714. this.boneMatrices = new Float32Array( 16 * this.bones.length );
  3715. this.pose();
  3716. }
  3717. };
  3718. THREE.SkinnedMesh.prototype = new THREE.Mesh();
  3719. THREE.SkinnedMesh.prototype.constructor = THREE.SkinnedMesh;
  3720. THREE.SkinnedMesh.prototype.addBone = function( bone ) {
  3721. if ( bone === undefined ) {
  3722. bone = new THREE.Bone( this );
  3723. }
  3724. this.bones.push( bone );
  3725. return bone;
  3726. };
  3727. THREE.SkinnedMesh.prototype.updateMatrixWorld = function ( force ) {
  3728. this.matrixAutoUpdate && this.updateMatrix();
  3729. // update matrixWorld
  3730. if ( this.matrixWorldNeedsUpdate || force ) {
  3731. if ( this.parent ) {
  3732. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  3733. } else {
  3734. this.matrixWorld.copy( this.matrix );
  3735. }
  3736. this.matrixWorldNeedsUpdate = false;
  3737. force = true;
  3738. }
  3739. // update children
  3740. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  3741. var child = this.children[ i ];
  3742. if ( child instanceof THREE.Bone ) {
  3743. child.update( this.identityMatrix, false );
  3744. } else {
  3745. child.updateMatrixWorld( true );
  3746. }
  3747. }
  3748. // flatten bone matrices to array
  3749. var b, bl = this.bones.length,
  3750. ba = this.bones,
  3751. bm = this.boneMatrices;
  3752. for ( b = 0; b < bl; b ++ ) {
  3753. ba[ b ].skinMatrix.flattenToArrayOffset( bm, b * 16 );
  3754. }
  3755. };
  3756. /*
  3757. * Pose
  3758. */
  3759. THREE.SkinnedMesh.prototype.pose = function() {
  3760. this.updateMatrixWorld( true );
  3761. var bim, bone, boneInverses = [];
  3762. for ( var b = 0; b < this.bones.length; b ++ ) {
  3763. bone = this.bones[ b ];
  3764. var inverseMatrix = new THREE.Matrix4();
  3765. inverseMatrix.getInverse( bone.skinMatrix );
  3766. boneInverses.push( inverseMatrix );
  3767. bone.skinMatrix.flattenToArrayOffset( this.boneMatrices, b * 16 );
  3768. }
  3769. // project vertices to local
  3770. if ( this.geometry.skinVerticesA === undefined ) {
  3771. this.geometry.skinVerticesA = [];
  3772. this.geometry.skinVerticesB = [];
  3773. var orgVertex, vertex;
  3774. for ( var i = 0; i < this.geometry.skinIndices.length; i ++ ) {
  3775. orgVertex = this.geometry.vertices[ i ];
  3776. var indexA = this.geometry.skinIndices[ i ].x;
  3777. var indexB = this.geometry.skinIndices[ i ].y;
  3778. vertex = new THREE.Vector3( orgVertex.x, orgVertex.y, orgVertex.z );
  3779. this.geometry.skinVerticesA.push( boneInverses[ indexA ].multiplyVector3( vertex ) );
  3780. vertex = new THREE.Vector3( orgVertex.x, orgVertex.y, orgVertex.z );
  3781. this.geometry.skinVerticesB.push( boneInverses[ indexB ].multiplyVector3( vertex ) );
  3782. // todo: add more influences
  3783. // normalize weights
  3784. if ( this.geometry.skinWeights[ i ].x + this.geometry.skinWeights[ i ].y !== 1 ) {
  3785. var len = ( 1.0 - ( this.geometry.skinWeights[ i ].x + this.geometry.skinWeights[ i ].y ) ) * 0.5;
  3786. this.geometry.skinWeights[ i ].x += len;
  3787. this.geometry.skinWeights[ i ].y += len;
  3788. }
  3789. }
  3790. }
  3791. };
  3792. /**
  3793. * @author alteredq / http://alteredqualia.com/
  3794. */
  3795. THREE.Ribbon = function ( geometry, material ) {
  3796. THREE.Object3D.call( this );
  3797. this.geometry = geometry;
  3798. this.material = material;
  3799. };
  3800. THREE.Ribbon.prototype = new THREE.Object3D();
  3801. THREE.Ribbon.prototype.constructor = THREE.Ribbon;
  3802. /**
  3803. * @author mikael emtinger / http://gomo.se/
  3804. * @author alteredq / http://alteredqualia.com/
  3805. * @author mr.doob / http://mrdoob.com/
  3806. */
  3807. THREE.LOD = function () {
  3808. THREE.Object3D.call( this );
  3809. this.LODs = [];
  3810. };
  3811. THREE.LOD.prototype = new THREE.Object3D();
  3812. THREE.LOD.prototype.constructor = THREE.LOD;
  3813. THREE.LOD.prototype.supr = THREE.Object3D.prototype;
  3814. THREE.LOD.prototype.addLevel = function ( object3D, visibleAtDistance ) {
  3815. if ( visibleAtDistance === undefined ) {
  3816. visibleAtDistance = 0;
  3817. }
  3818. visibleAtDistance = Math.abs( visibleAtDistance );
  3819. for ( var l = 0; l < this.LODs.length; l ++ ) {
  3820. if ( visibleAtDistance < this.LODs[ l ].visibleAtDistance ) {
  3821. break;
  3822. }
  3823. }
  3824. this.LODs.splice( l, 0, { visibleAtDistance: visibleAtDistance, object3D: object3D } );
  3825. this.add( object3D );
  3826. };
  3827. THREE.LOD.prototype.update = function ( camera ) {
  3828. if ( this.LODs.length > 1 ) {
  3829. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  3830. var inverse = camera.matrixWorldInverse;
  3831. 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] );
  3832. this.LODs[ 0 ].object3D.visible = true;
  3833. for ( var l = 1; l < this.LODs.length; l ++ ) {
  3834. if( distance >= this.LODs[ l ].visibleAtDistance ) {
  3835. this.LODs[ l - 1 ].object3D.visible = false;
  3836. this.LODs[ l ].object3D.visible = true;
  3837. } else {
  3838. break;
  3839. }
  3840. }
  3841. for( ; l < this.LODs.length; l ++ ) {
  3842. this.LODs[ l ].object3D.visible = false;
  3843. }
  3844. }
  3845. };
  3846. /**
  3847. * @author mikael emtinger / http://gomo.se/
  3848. */
  3849. THREE.Sprite = function ( parameters ) {
  3850. THREE.Object3D.call( this );
  3851. this.color = ( parameters.color !== undefined ) ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3852. this.map = ( parameters.map !== undefined ) ? parameters.map : new THREE.Texture();
  3853. this.blending = ( parameters.blending !== undefined ) ? parameters.blending : THREE.NormalBlending;
  3854. this.blendSrc = parameters.blendSrc !== undefined ? parameters.blendSrc : THREE.SrcAlphaFactor;
  3855. this.blendDst = parameters.blendDst !== undefined ? parameters.blendDst : THREE.OneMinusSrcAlphaFactor;
  3856. this.blendEquation = parameters.blendEquation !== undefined ? parameters.blendEquation : THREE.AddEquation;
  3857. this.useScreenCoordinates = ( parameters.useScreenCoordinates !== undefined ) ? parameters.useScreenCoordinates : true;
  3858. this.mergeWith3D = ( parameters.mergeWith3D !== undefined ) ? parameters.mergeWith3D : !this.useScreenCoordinates;
  3859. this.affectedByDistance = ( parameters.affectedByDistance !== undefined ) ? parameters.affectedByDistance : !this.useScreenCoordinates;
  3860. this.scaleByViewport = ( parameters.scaleByViewport !== undefined ) ? parameters.scaleByViewport : !this.affectedByDistance;
  3861. this.alignment = ( parameters.alignment instanceof THREE.Vector2 ) ? parameters.alignment : THREE.SpriteAlignment.center;
  3862. this.rotation3d = this.rotation;
  3863. this.rotation = 0;
  3864. this.opacity = 1;
  3865. this.uvOffset = new THREE.Vector2( 0, 0 );
  3866. this.uvScale = new THREE.Vector2( 1, 1 );
  3867. };
  3868. THREE.Sprite.prototype = new THREE.Object3D();
  3869. THREE.Sprite.prototype.constructor = THREE.Sprite;
  3870. /*
  3871. * Custom update matrix
  3872. */
  3873. THREE.Sprite.prototype.updateMatrix = function () {
  3874. this.matrix.setPosition( this.position );
  3875. this.rotation3d.set( 0, 0, this.rotation );
  3876. this.matrix.setRotationFromEuler( this.rotation3d );
  3877. if ( this.scale.x !== 1 || this.scale.y !== 1 ) {
  3878. this.matrix.scale( this.scale );
  3879. this.boundRadiusScale = Math.max( this.scale.x, this.scale.y );
  3880. }
  3881. this.matrixWorldNeedsUpdate = true;
  3882. };
  3883. /*
  3884. * Alignment
  3885. */
  3886. THREE.SpriteAlignment = {};
  3887. THREE.SpriteAlignment.topLeft = new THREE.Vector2( 1, -1 );
  3888. THREE.SpriteAlignment.topCenter = new THREE.Vector2( 0, -1 );
  3889. THREE.SpriteAlignment.topRight = new THREE.Vector2( -1, -1 );
  3890. THREE.SpriteAlignment.centerLeft = new THREE.Vector2( 1, 0 );
  3891. THREE.SpriteAlignment.center = new THREE.Vector2( 0, 0 );
  3892. THREE.SpriteAlignment.centerRight = new THREE.Vector2( -1, 0 );
  3893. THREE.SpriteAlignment.bottomLeft = new THREE.Vector2( 1, 1 );
  3894. THREE.SpriteAlignment.bottomCenter = new THREE.Vector2( 0, 1 );
  3895. THREE.SpriteAlignment.bottomRight = new THREE.Vector2( -1, 1 );
  3896. /**
  3897. * @author mr.doob / http://mrdoob.com/
  3898. */
  3899. THREE.Scene = function () {
  3900. THREE.Object3D.call( this );
  3901. this.fog = null;
  3902. this.overrideMaterial = null;
  3903. this.matrixAutoUpdate = false;
  3904. this.__objects = [];
  3905. this.__lights = [];
  3906. this.__objectsAdded = [];
  3907. this.__objectsRemoved = [];
  3908. };
  3909. THREE.Scene.prototype = new THREE.Object3D();
  3910. THREE.Scene.prototype.constructor = THREE.Scene;
  3911. THREE.Scene.prototype.__addObject = function ( object ) {
  3912. if ( object instanceof THREE.Light ) {
  3913. if ( this.__lights.indexOf( object ) === - 1 ) {
  3914. this.__lights.push( object );
  3915. }
  3916. } else if ( !( object instanceof THREE.Camera || object instanceof THREE.Bone ) ) {
  3917. if ( this.__objects.indexOf( object ) === - 1 ) {
  3918. this.__objects.push( object );
  3919. this.__objectsAdded.push( object );
  3920. // check if previously removed
  3921. var i = this.__objectsRemoved.indexOf( object );
  3922. if ( i !== -1 ) {
  3923. this.__objectsRemoved.splice( i, 1 );
  3924. }
  3925. }
  3926. }
  3927. for ( var c = 0; c < object.children.length; c ++ ) {
  3928. this.__addObject( object.children[ c ] );
  3929. }
  3930. };
  3931. THREE.Scene.prototype.__removeObject = function ( object ) {
  3932. if ( object instanceof THREE.Light ) {
  3933. var i = this.__lights.indexOf( object );
  3934. if ( i !== -1 ) {
  3935. this.__lights.splice( i, 1 );
  3936. }
  3937. } else if ( !( object instanceof THREE.Camera ) ) {
  3938. var i = this.__objects.indexOf( object );
  3939. if( i !== -1 ) {
  3940. this.__objects.splice( i, 1 );
  3941. this.__objectsRemoved.push( object );
  3942. // check if previously added
  3943. var ai = this.__objectsAdded.indexOf( object );
  3944. if ( ai !== -1 ) {
  3945. this.__objectsAdded.splice( ai, 1 );
  3946. }
  3947. }
  3948. }
  3949. for ( var c = 0; c < object.children.length; c ++ ) {
  3950. this.__removeObject( object.children[ c ] );
  3951. }
  3952. };
  3953. /**
  3954. * @author mr.doob / http://mrdoob.com/
  3955. * @author alteredq / http://alteredqualia.com/
  3956. */
  3957. THREE.Fog = function ( hex, near, far ) {
  3958. this.color = new THREE.Color( hex );
  3959. this.near = ( near !== undefined ) ? near : 1;
  3960. this.far = ( far !== undefined ) ? far : 1000;
  3961. };
  3962. /**
  3963. * @author mr.doob / http://mrdoob.com/
  3964. * @author alteredq / http://alteredqualia.com/
  3965. */
  3966. THREE.FogExp2 = function ( hex, density ) {
  3967. this.color = new THREE.Color( hex );
  3968. this.density = ( density !== undefined ) ? density : 0.00025;
  3969. };
  3970. /**
  3971. * @author alteredq / http://alteredqualia.com/
  3972. * @author mrdoob / http://mrdoob.com/
  3973. * @author mikael emtinger / http://gomo.se/
  3974. */
  3975. THREE.ShaderChunk = {
  3976. // FOG
  3977. fog_pars_fragment: [
  3978. "#ifdef USE_FOG",
  3979. "uniform vec3 fogColor;",
  3980. "#ifdef FOG_EXP2",
  3981. "uniform float fogDensity;",
  3982. "#else",
  3983. "uniform float fogNear;",
  3984. "uniform float fogFar;",
  3985. "#endif",
  3986. "#endif"
  3987. ].join("\n"),
  3988. fog_fragment: [
  3989. "#ifdef USE_FOG",
  3990. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  3991. "#ifdef FOG_EXP2",
  3992. "const float LOG2 = 1.442695;",
  3993. "float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );",
  3994. "fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );",
  3995. "#else",
  3996. "float fogFactor = smoothstep( fogNear, fogFar, depth );",
  3997. "#endif",
  3998. "gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );",
  3999. "#endif"
  4000. ].join("\n"),
  4001. // ENVIRONMENT MAP
  4002. envmap_pars_fragment: [
  4003. "#ifdef USE_ENVMAP",
  4004. "varying vec3 vReflect;",
  4005. "uniform float reflectivity;",
  4006. "uniform samplerCube envMap;",
  4007. "uniform float flipEnvMap;",
  4008. "uniform int combine;",
  4009. "#endif"
  4010. ].join("\n"),
  4011. envmap_fragment: [
  4012. "#ifdef USE_ENVMAP",
  4013. "#ifdef DOUBLE_SIDED",
  4014. "float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  4015. "vec4 cubeColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  4016. "#else",
  4017. "vec4 cubeColor = textureCube( envMap, vec3( flipEnvMap * vReflect.x, vReflect.yz ) );",
  4018. "#endif",
  4019. "#ifdef GAMMA_INPUT",
  4020. "cubeColor.xyz *= cubeColor.xyz;",
  4021. "#endif",
  4022. "if ( combine == 1 ) {",
  4023. "gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, reflectivity );",
  4024. "} else {",
  4025. "gl_FragColor.xyz = gl_FragColor.xyz * cubeColor.xyz;",
  4026. "}",
  4027. "#endif"
  4028. ].join("\n"),
  4029. envmap_pars_vertex: [
  4030. "#ifdef USE_ENVMAP",
  4031. "varying vec3 vReflect;",
  4032. "uniform float refractionRatio;",
  4033. "uniform bool useRefract;",
  4034. "#endif"
  4035. ].join("\n"),
  4036. envmap_vertex : [
  4037. "#ifdef USE_ENVMAP",
  4038. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  4039. "vec3 nWorld = mat3( objectMatrix[ 0 ].xyz, objectMatrix[ 1 ].xyz, objectMatrix[ 2 ].xyz ) * normal;",
  4040. "if ( useRefract ) {",
  4041. "vReflect = refract( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ), refractionRatio );",
  4042. "} else {",
  4043. "vReflect = reflect( normalize( mPosition.xyz - cameraPosition ), normalize( nWorld.xyz ) );",
  4044. "}",
  4045. "#endif"
  4046. ].join("\n"),
  4047. // COLOR MAP (particles)
  4048. map_particle_pars_fragment: [
  4049. "#ifdef USE_MAP",
  4050. "uniform sampler2D map;",
  4051. "#endif"
  4052. ].join("\n"),
  4053. map_particle_fragment: [
  4054. "#ifdef USE_MAP",
  4055. "gl_FragColor = gl_FragColor * texture2D( map, gl_PointCoord );",
  4056. "#endif"
  4057. ].join("\n"),
  4058. // COLOR MAP (triangles)
  4059. map_pars_vertex: [
  4060. "#ifdef USE_MAP",
  4061. "varying vec2 vUv;",
  4062. "uniform vec4 offsetRepeat;",
  4063. "#endif"
  4064. ].join("\n"),
  4065. map_pars_fragment: [
  4066. "#ifdef USE_MAP",
  4067. "varying vec2 vUv;",
  4068. "uniform sampler2D map;",
  4069. "#endif"
  4070. ].join("\n"),
  4071. map_vertex: [
  4072. "#ifdef USE_MAP",
  4073. "vUv = uv * offsetRepeat.zw + offsetRepeat.xy;",
  4074. "#endif"
  4075. ].join("\n"),
  4076. map_fragment: [
  4077. "#ifdef USE_MAP",
  4078. "#ifdef GAMMA_INPUT",
  4079. "vec4 texelColor = texture2D( map, vUv );",
  4080. "texelColor.xyz *= texelColor.xyz;",
  4081. "gl_FragColor = gl_FragColor * texelColor;",
  4082. "#else",
  4083. "gl_FragColor = gl_FragColor * texture2D( map, vUv );",
  4084. "#endif",
  4085. "#endif"
  4086. ].join("\n"),
  4087. // LIGHT MAP
  4088. lightmap_pars_fragment: [
  4089. "#ifdef USE_LIGHTMAP",
  4090. "varying vec2 vUv2;",
  4091. "uniform sampler2D lightMap;",
  4092. "#endif"
  4093. ].join("\n"),
  4094. lightmap_pars_vertex: [
  4095. "#ifdef USE_LIGHTMAP",
  4096. "varying vec2 vUv2;",
  4097. "#endif"
  4098. ].join("\n"),
  4099. lightmap_fragment: [
  4100. "#ifdef USE_LIGHTMAP",
  4101. "gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );",
  4102. "#endif"
  4103. ].join("\n"),
  4104. lightmap_vertex: [
  4105. "#ifdef USE_LIGHTMAP",
  4106. "vUv2 = uv2;",
  4107. "#endif"
  4108. ].join("\n"),
  4109. // LIGHTS LAMBERT
  4110. lights_lambert_pars_vertex: [
  4111. "uniform vec3 ambient;",
  4112. "uniform vec3 diffuse;",
  4113. "uniform vec3 emissive;",
  4114. "uniform vec3 ambientLightColor;",
  4115. "#if MAX_DIR_LIGHTS > 0",
  4116. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  4117. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  4118. "#endif",
  4119. "#if MAX_POINT_LIGHTS > 0",
  4120. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  4121. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  4122. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  4123. "#endif",
  4124. "#ifdef WRAP_AROUND",
  4125. "uniform vec3 wrapRGB;",
  4126. "#endif",
  4127. ].join("\n"),
  4128. lights_lambert_vertex: [
  4129. "vLightFront = vec3( 0.0 );",
  4130. "#ifdef DOUBLE_SIDED",
  4131. "vLightBack = vec3( 0.0 );",
  4132. "#endif",
  4133. "transformedNormal = normalize( transformedNormal );",
  4134. "#if MAX_DIR_LIGHTS > 0",
  4135. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  4136. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  4137. "vec3 dirVector = normalize( lDirection.xyz );",
  4138. "float dotProduct = dot( transformedNormal, dirVector );",
  4139. "vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  4140. "#ifdef DOUBLE_SIDED",
  4141. "vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  4142. "#ifdef WRAP_AROUND",
  4143. "vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  4144. "#endif",
  4145. "#endif",
  4146. "#ifdef WRAP_AROUND",
  4147. "vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  4148. "directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );",
  4149. "#ifdef DOUBLE_SIDED",
  4150. "directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );",
  4151. "#endif",
  4152. "#endif",
  4153. "vLightFront += directionalLightColor[ i ] * directionalLightWeighting;",
  4154. "#ifdef DOUBLE_SIDED",
  4155. "vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;",
  4156. "#endif",
  4157. "}",
  4158. "#endif",
  4159. "#if MAX_POINT_LIGHTS > 0",
  4160. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  4161. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  4162. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  4163. "float lDistance = 1.0;",
  4164. "if ( pointLightDistance[ i ] > 0.0 )",
  4165. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  4166. "lVector = normalize( lVector );",
  4167. "float dotProduct = dot( transformedNormal, lVector );",
  4168. "vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );",
  4169. "#ifdef DOUBLE_SIDED",
  4170. "vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );",
  4171. "#ifdef WRAP_AROUND",
  4172. "vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );",
  4173. "#endif",
  4174. "#endif",
  4175. "#ifdef WRAP_AROUND",
  4176. "vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );",
  4177. "pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );",
  4178. "#ifdef DOUBLE_SIDED",
  4179. "pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );",
  4180. "#endif",
  4181. "#endif",
  4182. "vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;",
  4183. "#ifdef DOUBLE_SIDED",
  4184. "vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;",
  4185. "#endif",
  4186. "}",
  4187. "#endif",
  4188. "vLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;",
  4189. "#ifdef DOUBLE_SIDED",
  4190. "vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;",
  4191. "#endif",
  4192. ].join("\n"),
  4193. // LIGHTS PHONG
  4194. lights_phong_pars_vertex: [
  4195. "#if MAX_POINT_LIGHTS > 0",
  4196. "#ifndef PHONG_PER_PIXEL",
  4197. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  4198. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  4199. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  4200. "#endif",
  4201. "#endif"
  4202. ].join("\n"),
  4203. lights_phong_vertex: [
  4204. "#if MAX_POINT_LIGHTS > 0",
  4205. "#ifndef PHONG_PER_PIXEL",
  4206. "for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  4207. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  4208. "vec3 lVector = lPosition.xyz - mvPosition.xyz;",
  4209. "float lDistance = 1.0;",
  4210. "if ( pointLightDistance[ i ] > 0.0 )",
  4211. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  4212. "vPointLight[ i ] = vec4( lVector, lDistance );",
  4213. "}",
  4214. "#endif",
  4215. "#endif"
  4216. ].join("\n"),
  4217. lights_phong_pars_fragment: [
  4218. "uniform vec3 ambientLightColor;",
  4219. "#if MAX_DIR_LIGHTS > 0",
  4220. "uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  4221. "uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  4222. "#endif",
  4223. "#if MAX_POINT_LIGHTS > 0",
  4224. "uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  4225. "#ifdef PHONG_PER_PIXEL",
  4226. "uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  4227. "uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  4228. "#else",
  4229. "varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",
  4230. "#endif",
  4231. "#endif",
  4232. "#ifdef WRAP_AROUND",
  4233. "uniform vec3 wrapRGB;",
  4234. "#endif",
  4235. "varying vec3 vViewPosition;",
  4236. "varying vec3 vNormal;"
  4237. ].join("\n"),
  4238. lights_phong_fragment: [
  4239. "vec3 normal = normalize( vNormal );",
  4240. "vec3 viewPosition = normalize( vViewPosition );",
  4241. "#ifdef DOUBLE_SIDED",
  4242. "normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",
  4243. "#endif",
  4244. "#if MAX_POINT_LIGHTS > 0",
  4245. "vec3 pointDiffuse = vec3( 0.0 );",
  4246. "vec3 pointSpecular = vec3( 0.0 );",
  4247. "for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  4248. "#ifdef PHONG_PER_PIXEL",
  4249. "vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  4250. "vec3 lVector = lPosition.xyz + vViewPosition.xyz;",
  4251. "float lDistance = 1.0;",
  4252. "if ( pointLightDistance[ i ] > 0.0 )",
  4253. "lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",
  4254. "lVector = normalize( lVector );",
  4255. "#else",
  4256. "vec3 lVector = normalize( vPointLight[ i ].xyz );",
  4257. "float lDistance = vPointLight[ i ].w;",
  4258. "#endif",
  4259. // diffuse
  4260. "float dotProduct = dot( normal, lVector );",
  4261. "#ifdef WRAP_AROUND",
  4262. "float pointDiffuseWeightFull = max( dotProduct, 0.0 );",
  4263. "float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  4264. "vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  4265. "#else",
  4266. "float pointDiffuseWeight = max( dotProduct, 0.0 );",
  4267. "#endif",
  4268. "pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;",
  4269. // specular
  4270. "vec3 pointHalfVector = normalize( lVector + viewPosition );",
  4271. "float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  4272. "float pointSpecularWeight = max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  4273. "#ifdef PHYSICALLY_BASED_SHADING",
  4274. // 2.0 => 2.0001 is hack to work around ANGLE bug
  4275. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  4276. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( lVector, pointHalfVector ), 5.0 );",
  4277. "pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance * specularNormalization;",
  4278. "#else",
  4279. "pointSpecular += specular * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance;",
  4280. "#endif",
  4281. "}",
  4282. "#endif",
  4283. "#if MAX_DIR_LIGHTS > 0",
  4284. "vec3 dirDiffuse = vec3( 0.0 );",
  4285. "vec3 dirSpecular = vec3( 0.0 );" ,
  4286. "for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {",
  4287. "vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  4288. "vec3 dirVector = normalize( lDirection.xyz );",
  4289. // diffuse
  4290. "float dotProduct = dot( normal, dirVector );",
  4291. "#ifdef WRAP_AROUND",
  4292. "float dirDiffuseWeightFull = max( dotProduct, 0.0 );",
  4293. "float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );",
  4294. "vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );",
  4295. "#else",
  4296. "float dirDiffuseWeight = max( dotProduct, 0.0 );",
  4297. "#endif",
  4298. "dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;",
  4299. // specular
  4300. "vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  4301. "float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  4302. "float dirSpecularWeight = max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  4303. "#ifdef PHYSICALLY_BASED_SHADING",
  4304. /*
  4305. // fresnel term from skin shader
  4306. "const float F0 = 0.128;",
  4307. "float base = 1.0 - dot( viewPosition, dirHalfVector );",
  4308. "float exponential = pow( base, 5.0 );",
  4309. "float fresnel = exponential + F0 * ( 1.0 - exponential );",
  4310. */
  4311. /*
  4312. // fresnel term from fresnel shader
  4313. "const float mFresnelBias = 0.08;",
  4314. "const float mFresnelScale = 0.3;",
  4315. "const float mFresnelPower = 5.0;",
  4316. "float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );",
  4317. */
  4318. // 2.0 => 2.0001 is hack to work around ANGLE bug
  4319. "float specularNormalization = ( shininess + 2.0001 ) / 8.0;",
  4320. //"dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;",
  4321. "vec3 schlick = specular + vec3( 1.0 - specular ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",
  4322. "dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  4323. "#else",
  4324. "dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight;",
  4325. "#endif",
  4326. "}",
  4327. "#endif",
  4328. "vec3 totalDiffuse = vec3( 0.0 );",
  4329. "vec3 totalSpecular = vec3( 0.0 );",
  4330. "#if MAX_DIR_LIGHTS > 0",
  4331. "totalDiffuse += dirDiffuse;",
  4332. "totalSpecular += dirSpecular;",
  4333. "#endif",
  4334. "#if MAX_POINT_LIGHTS > 0",
  4335. "totalDiffuse += pointDiffuse;",
  4336. "totalSpecular += pointSpecular;",
  4337. "#endif",
  4338. "#ifdef METAL",
  4339. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  4340. "#else",
  4341. "gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  4342. "#endif"
  4343. ].join("\n"),
  4344. // VERTEX COLORS
  4345. color_pars_fragment: [
  4346. "#ifdef USE_COLOR",
  4347. "varying vec3 vColor;",
  4348. "#endif"
  4349. ].join("\n"),
  4350. color_fragment: [
  4351. "#ifdef USE_COLOR",
  4352. "gl_FragColor = gl_FragColor * vec4( vColor, opacity );",
  4353. "#endif"
  4354. ].join("\n"),
  4355. color_pars_vertex: [
  4356. "#ifdef USE_COLOR",
  4357. "varying vec3 vColor;",
  4358. "#endif"
  4359. ].join("\n"),
  4360. color_vertex: [
  4361. "#ifdef USE_COLOR",
  4362. "#ifdef GAMMA_INPUT",
  4363. "vColor = color * color;",
  4364. "#else",
  4365. "vColor = color;",
  4366. "#endif",
  4367. "#endif"
  4368. ].join("\n"),
  4369. // SKINNING
  4370. skinning_pars_vertex: [
  4371. "#ifdef USE_SKINNING",
  4372. "uniform mat4 boneGlobalMatrices[ MAX_BONES ];",
  4373. "#endif"
  4374. ].join("\n"),
  4375. skinning_vertex: [
  4376. "#ifdef USE_SKINNING",
  4377. "gl_Position = ( boneGlobalMatrices[ int( skinIndex.x ) ] * skinVertexA ) * skinWeight.x;",
  4378. "gl_Position += ( boneGlobalMatrices[ int( skinIndex.y ) ] * skinVertexB ) * skinWeight.y;",
  4379. "gl_Position = projectionMatrix * modelViewMatrix * gl_Position;",
  4380. "#endif"
  4381. ].join("\n"),
  4382. // MORPHING
  4383. morphtarget_pars_vertex: [
  4384. "#ifdef USE_MORPHTARGETS",
  4385. "#ifndef USE_MORPHNORMALS",
  4386. "uniform float morphTargetInfluences[ 8 ];",
  4387. "#else",
  4388. "uniform float morphTargetInfluences[ 4 ];",
  4389. "#endif",
  4390. "#endif"
  4391. ].join("\n"),
  4392. morphtarget_vertex: [
  4393. "#ifdef USE_MORPHTARGETS",
  4394. "vec3 morphed = vec3( 0.0 );",
  4395. "morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];",
  4396. "morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];",
  4397. "morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];",
  4398. "morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];",
  4399. "#ifndef USE_MORPHNORMALS",
  4400. "morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];",
  4401. "morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];",
  4402. "morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];",
  4403. "morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];",
  4404. "#endif",
  4405. "morphed += position;",
  4406. "gl_Position = projectionMatrix * modelViewMatrix * vec4( morphed, 1.0 );",
  4407. "#endif"
  4408. ].join("\n"),
  4409. default_vertex : [
  4410. "#ifndef USE_MORPHTARGETS",
  4411. "#ifndef USE_SKINNING",
  4412. "gl_Position = projectionMatrix * mvPosition;",
  4413. "#endif",
  4414. "#endif"
  4415. ].join("\n"),
  4416. morphnormal_vertex: [
  4417. "#ifdef USE_MORPHNORMALS",
  4418. "vec3 morphedNormal = vec3( 0.0 );",
  4419. "morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];",
  4420. "morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];",
  4421. "morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];",
  4422. "morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];",
  4423. "morphedNormal += normal;",
  4424. "vec3 transformedNormal = normalMatrix * morphedNormal;",
  4425. "#else",
  4426. "vec3 transformedNormal = normalMatrix * normal;",
  4427. "#endif"
  4428. ].join("\n"),
  4429. // SHADOW MAP
  4430. // based on SpiderGL shadow map and Fabien Sanglard's GLSL shadow mapping examples
  4431. // http://spidergl.org/example.php?id=6
  4432. // http://fabiensanglard.net/shadowmapping
  4433. shadowmap_pars_fragment: [
  4434. "#ifdef USE_SHADOWMAP",
  4435. "uniform sampler2D shadowMap[ MAX_SHADOWS ];",
  4436. "uniform vec2 shadowMapSize[ MAX_SHADOWS ];",
  4437. "uniform float shadowDarkness[ MAX_SHADOWS ];",
  4438. "uniform float shadowBias[ MAX_SHADOWS ];",
  4439. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  4440. "float unpackDepth( const in vec4 rgba_depth ) {",
  4441. "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 );",
  4442. "float depth = dot( rgba_depth, bit_shift );",
  4443. "return depth;",
  4444. "}",
  4445. "#endif"
  4446. ].join("\n"),
  4447. shadowmap_fragment: [
  4448. "#ifdef USE_SHADOWMAP",
  4449. "#ifdef SHADOWMAP_DEBUG",
  4450. "vec3 frustumColors[3];",
  4451. "frustumColors[0] = vec3( 1.0, 0.5, 0.0 );",
  4452. "frustumColors[1] = vec3( 0.0, 1.0, 0.8 );",
  4453. "frustumColors[2] = vec3( 0.0, 0.5, 1.0 );",
  4454. "#endif",
  4455. "#ifdef SHADOWMAP_CASCADE",
  4456. "int inFrustumCount = 0;",
  4457. "#endif",
  4458. "float fDepth;",
  4459. "vec3 shadowColor = vec3( 1.0 );",
  4460. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  4461. "vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;",
  4462. // "if ( something && something )" breaks ATI OpenGL shader compiler
  4463. // "if ( all( something, something ) )" using this instead
  4464. "bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );",
  4465. "bool inFrustum = all( inFrustumVec );",
  4466. // don't shadow pixels outside of light frustum
  4467. // use just first frustum (for cascades)
  4468. // don't shadow pixels behind far plane of light frustum
  4469. "#ifdef SHADOWMAP_CASCADE",
  4470. "inFrustumCount += int( inFrustum );",
  4471. "bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );",
  4472. "#else",
  4473. "bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );",
  4474. "#endif",
  4475. "bool frustumTest = all( frustumTestVec );",
  4476. "if ( frustumTest ) {",
  4477. "shadowCoord.z += shadowBias[ i ];",
  4478. "#ifdef SHADOWMAP_SOFT",
  4479. // Percentage-close filtering
  4480. // (9 pixel kernel)
  4481. // http://fabiensanglard.net/shadowmappingPCF/
  4482. "float shadow = 0.0;",
  4483. /*
  4484. // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL
  4485. // must enroll loop manually
  4486. "for ( float y = -1.25; y <= 1.25; y += 1.25 )",
  4487. "for ( float x = -1.25; x <= 1.25; x += 1.25 ) {",
  4488. "vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );",
  4489. // doesn't seem to produce any noticeable visual difference compared to simple "texture2D" lookup
  4490. //"vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );",
  4491. "float fDepth = unpackDepth( rgbaDepth );",
  4492. "if ( fDepth < shadowCoord.z )",
  4493. "shadow += 1.0;",
  4494. "}",
  4495. "shadow /= 9.0;",
  4496. */
  4497. "const float shadowDelta = 1.0 / 9.0;",
  4498. "float xPixelOffset = 1.0 / shadowMapSize[ i ].x;",
  4499. "float yPixelOffset = 1.0 / shadowMapSize[ i ].y;",
  4500. "float dx0 = -1.25 * xPixelOffset;",
  4501. "float dy0 = -1.25 * yPixelOffset;",
  4502. "float dx1 = 1.25 * xPixelOffset;",
  4503. "float dy1 = 1.25 * yPixelOffset;",
  4504. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );",
  4505. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4506. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );",
  4507. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4508. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );",
  4509. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4510. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );",
  4511. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4512. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );",
  4513. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4514. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );",
  4515. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4516. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );",
  4517. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4518. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );",
  4519. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4520. "fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );",
  4521. "if ( fDepth < shadowCoord.z ) shadow += shadowDelta;",
  4522. "shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );",
  4523. "#else",
  4524. "vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );",
  4525. "float fDepth = unpackDepth( rgbaDepth );",
  4526. "if ( fDepth < shadowCoord.z )",
  4527. // spot with multiple shadows is darker
  4528. "shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );",
  4529. // spot with multiple shadows has the same color as single shadow spot
  4530. //"shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );",
  4531. "#endif",
  4532. "}",
  4533. "#ifdef SHADOWMAP_DEBUG",
  4534. "#ifdef SHADOWMAP_CASCADE",
  4535. "if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];",
  4536. "#else",
  4537. "if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];",
  4538. "#endif",
  4539. "#endif",
  4540. "}",
  4541. "#ifdef GAMMA_OUTPUT",
  4542. "shadowColor *= shadowColor;",
  4543. "#endif",
  4544. "gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;",
  4545. "#endif"
  4546. ].join("\n"),
  4547. shadowmap_pars_vertex: [
  4548. "#ifdef USE_SHADOWMAP",
  4549. "varying vec4 vShadowCoord[ MAX_SHADOWS ];",
  4550. "uniform mat4 shadowMatrix[ MAX_SHADOWS ];",
  4551. "#endif"
  4552. ].join("\n"),
  4553. shadowmap_vertex: [
  4554. "#ifdef USE_SHADOWMAP",
  4555. "for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  4556. "#ifdef USE_MORPHTARGETS",
  4557. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( morphed, 1.0 );",
  4558. "#else",
  4559. "vShadowCoord[ i ] = shadowMatrix[ i ] * objectMatrix * vec4( position, 1.0 );",
  4560. "#endif",
  4561. "}",
  4562. "#endif"
  4563. ].join("\n"),
  4564. // ALPHATEST
  4565. alphatest_fragment: [
  4566. "#ifdef ALPHATEST",
  4567. "if ( gl_FragColor.a < ALPHATEST ) discard;",
  4568. "#endif"
  4569. ].join("\n"),
  4570. // LINEAR SPACE
  4571. linear_to_gamma_fragment: [
  4572. "#ifdef GAMMA_OUTPUT",
  4573. "gl_FragColor.xyz = sqrt( gl_FragColor.xyz );",
  4574. "#endif"
  4575. ].join("\n"),
  4576. };
  4577. THREE.UniformsUtils = {
  4578. merge: function ( uniforms ) {
  4579. var u, p, tmp, merged = {};
  4580. for ( u = 0; u < uniforms.length; u++ ) {
  4581. tmp = this.clone( uniforms[ u ] );
  4582. for ( p in tmp ) {
  4583. merged[ p ] = tmp[ p ];
  4584. }
  4585. }
  4586. return merged;
  4587. },
  4588. clone: function ( uniforms_src ) {
  4589. var u, p, parameter, parameter_src, uniforms_dst = {};
  4590. for ( u in uniforms_src ) {
  4591. uniforms_dst[ u ] = {};
  4592. for ( p in uniforms_src[ u ] ) {
  4593. parameter_src = uniforms_src[ u ][ p ];
  4594. if ( parameter_src instanceof THREE.Color ||
  4595. parameter_src instanceof THREE.Vector2 ||
  4596. parameter_src instanceof THREE.Vector3 ||
  4597. parameter_src instanceof THREE.Vector4 ||
  4598. parameter_src instanceof THREE.Matrix4 ||
  4599. parameter_src instanceof THREE.Texture ) {
  4600. uniforms_dst[ u ][ p ] = parameter_src.clone();
  4601. } else if ( parameter_src instanceof Array ) {
  4602. uniforms_dst[ u ][ p ] = parameter_src.slice();
  4603. } else {
  4604. uniforms_dst[ u ][ p ] = parameter_src;
  4605. }
  4606. }
  4607. }
  4608. return uniforms_dst;
  4609. }
  4610. };
  4611. THREE.UniformsLib = {
  4612. common: {
  4613. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  4614. "opacity" : { type: "f", value: 1.0 },
  4615. "map" : { type: "t", value: 0, texture: null },
  4616. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  4617. "lightMap" : { type: "t", value: 2, texture: null },
  4618. "envMap" : { type: "t", value: 1, texture: null },
  4619. "flipEnvMap" : { type: "f", value: -1 },
  4620. "useRefract" : { type: "i", value: 0 },
  4621. "reflectivity" : { type: "f", value: 1.0 },
  4622. "refractionRatio" : { type: "f", value: 0.98 },
  4623. "combine" : { type: "i", value: 0 },
  4624. "morphTargetInfluences" : { type: "f", value: 0 }
  4625. },
  4626. fog : {
  4627. "fogDensity" : { type: "f", value: 0.00025 },
  4628. "fogNear" : { type: "f", value: 1 },
  4629. "fogFar" : { type: "f", value: 2000 },
  4630. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  4631. },
  4632. lights: {
  4633. "ambientLightColor" : { type: "fv", value: [] },
  4634. "directionalLightDirection" : { type: "fv", value: [] },
  4635. "directionalLightColor" : { type: "fv", value: [] },
  4636. "pointLightColor" : { type: "fv", value: [] },
  4637. "pointLightPosition" : { type: "fv", value: [] },
  4638. "pointLightDistance" : { type: "fv1", value: [] }
  4639. },
  4640. particle: {
  4641. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  4642. "opacity" : { type: "f", value: 1.0 },
  4643. "size" : { type: "f", value: 1.0 },
  4644. "scale" : { type: "f", value: 1.0 },
  4645. "map" : { type: "t", value: 0, texture: null },
  4646. "fogDensity" : { type: "f", value: 0.00025 },
  4647. "fogNear" : { type: "f", value: 1 },
  4648. "fogFar" : { type: "f", value: 2000 },
  4649. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  4650. },
  4651. shadowmap: {
  4652. "shadowMap": { type: "tv", value: 6, texture: [] },
  4653. "shadowMapSize": { type: "v2v", value: [] },
  4654. "shadowBias" : { type: "fv1", value: [] },
  4655. "shadowDarkness": { type: "fv1", value: [] },
  4656. "shadowMatrix" : { type: "m4v", value: [] },
  4657. }
  4658. };
  4659. THREE.ShaderLib = {
  4660. 'depth': {
  4661. uniforms: {
  4662. "mNear": { type: "f", value: 1.0 },
  4663. "mFar" : { type: "f", value: 2000.0 },
  4664. "opacity" : { type: "f", value: 1.0 }
  4665. },
  4666. vertexShader: [
  4667. "void main() {",
  4668. "gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  4669. "}"
  4670. ].join("\n"),
  4671. fragmentShader: [
  4672. "uniform float mNear;",
  4673. "uniform float mFar;",
  4674. "uniform float opacity;",
  4675. "void main() {",
  4676. "float depth = gl_FragCoord.z / gl_FragCoord.w;",
  4677. "float color = 1.0 - smoothstep( mNear, mFar, depth );",
  4678. "gl_FragColor = vec4( vec3( color ), opacity );",
  4679. "}"
  4680. ].join("\n")
  4681. },
  4682. 'normal': {
  4683. uniforms: {
  4684. "opacity" : { type: "f", value: 1.0 }
  4685. },
  4686. vertexShader: [
  4687. "varying vec3 vNormal;",
  4688. "void main() {",
  4689. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4690. "vNormal = normalMatrix * normal;",
  4691. "gl_Position = projectionMatrix * mvPosition;",
  4692. "}"
  4693. ].join("\n"),
  4694. fragmentShader: [
  4695. "uniform float opacity;",
  4696. "varying vec3 vNormal;",
  4697. "void main() {",
  4698. "gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  4699. "}"
  4700. ].join("\n")
  4701. },
  4702. 'basic': {
  4703. uniforms: THREE.UniformsUtils.merge( [
  4704. THREE.UniformsLib[ "common" ],
  4705. THREE.UniformsLib[ "fog" ],
  4706. THREE.UniformsLib[ "shadowmap" ]
  4707. ] ),
  4708. vertexShader: [
  4709. THREE.ShaderChunk[ "map_pars_vertex" ],
  4710. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  4711. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  4712. THREE.ShaderChunk[ "color_pars_vertex" ],
  4713. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  4714. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4715. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4716. "void main() {",
  4717. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4718. THREE.ShaderChunk[ "map_vertex" ],
  4719. THREE.ShaderChunk[ "lightmap_vertex" ],
  4720. THREE.ShaderChunk[ "envmap_vertex" ],
  4721. THREE.ShaderChunk[ "color_vertex" ],
  4722. THREE.ShaderChunk[ "skinning_vertex" ],
  4723. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4724. THREE.ShaderChunk[ "default_vertex" ],
  4725. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4726. "}"
  4727. ].join("\n"),
  4728. fragmentShader: [
  4729. "uniform vec3 diffuse;",
  4730. "uniform float opacity;",
  4731. THREE.ShaderChunk[ "color_pars_fragment" ],
  4732. THREE.ShaderChunk[ "map_pars_fragment" ],
  4733. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  4734. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  4735. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4736. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4737. "void main() {",
  4738. "gl_FragColor = vec4( diffuse, opacity );",
  4739. THREE.ShaderChunk[ "map_fragment" ],
  4740. THREE.ShaderChunk[ "alphatest_fragment" ],
  4741. THREE.ShaderChunk[ "lightmap_fragment" ],
  4742. THREE.ShaderChunk[ "color_fragment" ],
  4743. THREE.ShaderChunk[ "envmap_fragment" ],
  4744. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4745. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  4746. THREE.ShaderChunk[ "fog_fragment" ],
  4747. "}"
  4748. ].join("\n")
  4749. },
  4750. 'lambert': {
  4751. uniforms: THREE.UniformsUtils.merge( [
  4752. THREE.UniformsLib[ "common" ],
  4753. THREE.UniformsLib[ "fog" ],
  4754. THREE.UniformsLib[ "lights" ],
  4755. THREE.UniformsLib[ "shadowmap" ],
  4756. {
  4757. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  4758. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  4759. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  4760. }
  4761. ] ),
  4762. vertexShader: [
  4763. "varying vec3 vLightFront;",
  4764. "#ifdef DOUBLE_SIDED",
  4765. "varying vec3 vLightBack;",
  4766. "#endif",
  4767. THREE.ShaderChunk[ "map_pars_vertex" ],
  4768. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  4769. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  4770. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  4771. THREE.ShaderChunk[ "color_pars_vertex" ],
  4772. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  4773. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4774. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4775. "void main() {",
  4776. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4777. THREE.ShaderChunk[ "map_vertex" ],
  4778. THREE.ShaderChunk[ "lightmap_vertex" ],
  4779. THREE.ShaderChunk[ "envmap_vertex" ],
  4780. THREE.ShaderChunk[ "color_vertex" ],
  4781. THREE.ShaderChunk[ "morphnormal_vertex" ],
  4782. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  4783. THREE.ShaderChunk[ "skinning_vertex" ],
  4784. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4785. THREE.ShaderChunk[ "default_vertex" ],
  4786. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4787. "}"
  4788. ].join("\n"),
  4789. fragmentShader: [
  4790. "uniform float opacity;",
  4791. "varying vec3 vLightFront;",
  4792. "#ifdef DOUBLE_SIDED",
  4793. "varying vec3 vLightBack;",
  4794. "#endif",
  4795. THREE.ShaderChunk[ "color_pars_fragment" ],
  4796. THREE.ShaderChunk[ "map_pars_fragment" ],
  4797. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  4798. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  4799. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4800. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4801. "void main() {",
  4802. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  4803. THREE.ShaderChunk[ "map_fragment" ],
  4804. THREE.ShaderChunk[ "alphatest_fragment" ],
  4805. "#ifdef DOUBLE_SIDED",
  4806. //"float isFront = float( gl_FrontFacing );",
  4807. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  4808. "if ( gl_FrontFacing )",
  4809. "gl_FragColor.xyz *= vLightFront;",
  4810. "else",
  4811. "gl_FragColor.xyz *= vLightBack;",
  4812. "#else",
  4813. "gl_FragColor.xyz *= vLightFront;",
  4814. "#endif",
  4815. THREE.ShaderChunk[ "lightmap_fragment" ],
  4816. THREE.ShaderChunk[ "color_fragment" ],
  4817. THREE.ShaderChunk[ "envmap_fragment" ],
  4818. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4819. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  4820. THREE.ShaderChunk[ "fog_fragment" ],
  4821. "}"
  4822. ].join("\n")
  4823. },
  4824. 'phong': {
  4825. uniforms: THREE.UniformsUtils.merge( [
  4826. THREE.UniformsLib[ "common" ],
  4827. THREE.UniformsLib[ "fog" ],
  4828. THREE.UniformsLib[ "lights" ],
  4829. THREE.UniformsLib[ "shadowmap" ],
  4830. {
  4831. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  4832. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  4833. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  4834. "shininess": { type: "f", value: 30 },
  4835. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  4836. }
  4837. ] ),
  4838. vertexShader: [
  4839. "varying vec3 vViewPosition;",
  4840. "varying vec3 vNormal;",
  4841. THREE.ShaderChunk[ "map_pars_vertex" ],
  4842. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  4843. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  4844. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  4845. THREE.ShaderChunk[ "color_pars_vertex" ],
  4846. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  4847. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4848. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4849. "void main() {",
  4850. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4851. THREE.ShaderChunk[ "map_vertex" ],
  4852. THREE.ShaderChunk[ "lightmap_vertex" ],
  4853. THREE.ShaderChunk[ "envmap_vertex" ],
  4854. THREE.ShaderChunk[ "color_vertex" ],
  4855. "#ifndef USE_ENVMAP",
  4856. "vec4 mPosition = objectMatrix * vec4( position, 1.0 );",
  4857. "#endif",
  4858. "vViewPosition = -mvPosition.xyz;",
  4859. THREE.ShaderChunk[ "morphnormal_vertex" ],
  4860. "vNormal = transformedNormal;",
  4861. THREE.ShaderChunk[ "lights_phong_vertex" ],
  4862. THREE.ShaderChunk[ "skinning_vertex" ],
  4863. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4864. THREE.ShaderChunk[ "default_vertex" ],
  4865. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4866. "}"
  4867. ].join("\n"),
  4868. fragmentShader: [
  4869. "uniform vec3 diffuse;",
  4870. "uniform float opacity;",
  4871. "uniform vec3 ambient;",
  4872. "uniform vec3 emissive;",
  4873. "uniform vec3 specular;",
  4874. "uniform float shininess;",
  4875. THREE.ShaderChunk[ "color_pars_fragment" ],
  4876. THREE.ShaderChunk[ "map_pars_fragment" ],
  4877. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  4878. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  4879. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4880. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  4881. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4882. "void main() {",
  4883. "gl_FragColor = vec4( vec3 ( 1.0 ), opacity );",
  4884. THREE.ShaderChunk[ "map_fragment" ],
  4885. THREE.ShaderChunk[ "alphatest_fragment" ],
  4886. THREE.ShaderChunk[ "lights_phong_fragment" ],
  4887. THREE.ShaderChunk[ "lightmap_fragment" ],
  4888. THREE.ShaderChunk[ "color_fragment" ],
  4889. THREE.ShaderChunk[ "envmap_fragment" ],
  4890. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4891. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  4892. THREE.ShaderChunk[ "fog_fragment" ],
  4893. "}"
  4894. ].join("\n")
  4895. },
  4896. 'particle_basic': {
  4897. uniforms: THREE.UniformsUtils.merge( [
  4898. THREE.UniformsLib[ "particle" ],
  4899. THREE.UniformsLib[ "shadowmap" ]
  4900. ] ),
  4901. vertexShader: [
  4902. "uniform float size;",
  4903. "uniform float scale;",
  4904. THREE.ShaderChunk[ "color_pars_vertex" ],
  4905. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  4906. "void main() {",
  4907. THREE.ShaderChunk[ "color_vertex" ],
  4908. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4909. "#ifdef USE_SIZEATTENUATION",
  4910. "gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  4911. "#else",
  4912. "gl_PointSize = size;",
  4913. "#endif",
  4914. "gl_Position = projectionMatrix * mvPosition;",
  4915. THREE.ShaderChunk[ "shadowmap_vertex" ],
  4916. "}"
  4917. ].join("\n"),
  4918. fragmentShader: [
  4919. "uniform vec3 psColor;",
  4920. "uniform float opacity;",
  4921. THREE.ShaderChunk[ "color_pars_fragment" ],
  4922. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  4923. THREE.ShaderChunk[ "fog_pars_fragment" ],
  4924. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  4925. "void main() {",
  4926. "gl_FragColor = vec4( psColor, opacity );",
  4927. THREE.ShaderChunk[ "map_particle_fragment" ],
  4928. THREE.ShaderChunk[ "alphatest_fragment" ],
  4929. THREE.ShaderChunk[ "color_fragment" ],
  4930. THREE.ShaderChunk[ "shadowmap_fragment" ],
  4931. THREE.ShaderChunk[ "fog_fragment" ],
  4932. "}"
  4933. ].join("\n")
  4934. },
  4935. // Depth encoding into RGBA texture
  4936. // based on SpiderGL shadow map example
  4937. // http://spidergl.org/example.php?id=6
  4938. // originally from
  4939. // http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  4940. // see also here:
  4941. // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  4942. 'depthRGBA': {
  4943. uniforms: {},
  4944. vertexShader: [
  4945. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  4946. "void main() {",
  4947. "vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  4948. THREE.ShaderChunk[ "morphtarget_vertex" ],
  4949. THREE.ShaderChunk[ "default_vertex" ],
  4950. "}"
  4951. ].join("\n"),
  4952. fragmentShader: [
  4953. "vec4 pack_depth( const in float depth ) {",
  4954. "const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  4955. "const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  4956. "vec4 res = fract( depth * bit_shift );",
  4957. "res -= res.xxyz * bit_mask;",
  4958. "return res;",
  4959. "}",
  4960. "void main() {",
  4961. "gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  4962. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  4963. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  4964. //"gl_FragData[ 0 ] = pack_depth( z );",
  4965. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  4966. "}"
  4967. ].join("\n")
  4968. }
  4969. };/**
  4970. * @author supereggbert / http://www.paulbrunt.co.uk/
  4971. * @author mrdoob / http://mrdoob.com/
  4972. * @author alteredq / http://alteredqualia.com/
  4973. * @author szimek / https://github.com/szimek/
  4974. */
  4975. THREE.WebGLRenderer = function ( parameters ) {
  4976. console.log( 'THREE.WebGLRenderer', THREE.REVISION );
  4977. parameters = parameters || {};
  4978. var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
  4979. _precision = parameters.precision !== undefined ? parameters.precision : 'highp',
  4980. _alpha = parameters.alpha !== undefined ? parameters.alpha : true,
  4981. _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
  4982. _antialias = parameters.antialias !== undefined ? parameters.antialias : false,
  4983. _stencil = parameters.stencil !== undefined ? parameters.stencil : true,
  4984. _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
  4985. _clearColor = parameters.clearColor !== undefined ? new THREE.Color( parameters.clearColor ) : new THREE.Color( 0x000000 ),
  4986. _clearAlpha = parameters.clearAlpha !== undefined ? parameters.clearAlpha : 0,
  4987. _maxLights = parameters.maxLights !== undefined ? parameters.maxLights : 4;
  4988. // public properties
  4989. this.domElement = _canvas;
  4990. this.context = null;
  4991. // clearing
  4992. this.autoClear = true;
  4993. this.autoClearColor = true;
  4994. this.autoClearDepth = true;
  4995. this.autoClearStencil = true;
  4996. // scene graph
  4997. this.sortObjects = true;
  4998. this.autoUpdateObjects = true;
  4999. this.autoUpdateScene = true;
  5000. // physically based shading
  5001. this.gammaInput = false;
  5002. this.gammaOutput = false;
  5003. this.physicallyBasedShading = false;
  5004. // shadow map
  5005. this.shadowMapEnabled = false;
  5006. this.shadowMapAutoUpdate = true;
  5007. this.shadowMapSoft = true;
  5008. this.shadowMapCullFrontFaces = true;
  5009. this.shadowMapDebug = false;
  5010. this.shadowMapCascade = false;
  5011. // morphs
  5012. this.maxMorphTargets = 8;
  5013. this.maxMorphNormals = 4;
  5014. // flags
  5015. this.autoScaleCubemaps = true;
  5016. // custom render plugins
  5017. this.renderPluginsPre = [];
  5018. this.renderPluginsPost = [];
  5019. // info
  5020. this.info = {
  5021. memory: {
  5022. programs: 0,
  5023. geometries: 0,
  5024. textures: 0
  5025. },
  5026. render: {
  5027. calls: 0,
  5028. vertices: 0,
  5029. faces: 0,
  5030. points: 0
  5031. }
  5032. };
  5033. // internal properties
  5034. var _this = this,
  5035. _gl,
  5036. _programs = [],
  5037. // internal state cache
  5038. _currentProgram = null,
  5039. _currentFramebuffer = null,
  5040. _currentMaterialId = -1,
  5041. _currentGeometryGroupHash = null,
  5042. _currentCamera = null,
  5043. _geometryGroupCounter = 0,
  5044. // GL state cache
  5045. _oldDoubleSided = null,
  5046. _oldFlipSided = null,
  5047. _oldBlending = null,
  5048. _oldBlendEquation = null,
  5049. _oldBlendSrc = null,
  5050. _oldBlendDst = null,
  5051. _oldDepthTest = null,
  5052. _oldDepthWrite = null,
  5053. _oldPolygonOffset = null,
  5054. _oldPolygonOffsetFactor = null,
  5055. _oldPolygonOffsetUnits = null,
  5056. _oldLineWidth = null,
  5057. _viewportX = 0,
  5058. _viewportY = 0,
  5059. _viewportWidth = 0,
  5060. _viewportHeight = 0,
  5061. _currentWidth = 0,
  5062. _currentHeight = 0,
  5063. // frustum
  5064. _frustum = new THREE.Frustum(),
  5065. // camera matrices cache
  5066. _projScreenMatrix = new THREE.Matrix4(),
  5067. _projScreenMatrixPS = new THREE.Matrix4(),
  5068. _vector3 = new THREE.Vector4(),
  5069. // light arrays cache
  5070. _direction = new THREE.Vector3(),
  5071. _lights = {
  5072. ambient: [ 0, 0, 0 ],
  5073. directional: { length: 0, colors: new Array(), positions: new Array() },
  5074. point: { length: 0, colors: new Array(), positions: new Array(), distances: new Array() }
  5075. };
  5076. // initialize
  5077. _gl = initGL();
  5078. setDefaultGLState();
  5079. this.context = _gl;
  5080. // GPU capabilities
  5081. var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ),
  5082. _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE ),
  5083. _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE );
  5084. // API
  5085. this.getContext = function () {
  5086. return _gl;
  5087. };
  5088. this.supportsVertexTextures = function () {
  5089. return _maxVertexTextures > 0;
  5090. };
  5091. this.setSize = function ( width, height ) {
  5092. _canvas.width = width;
  5093. _canvas.height = height;
  5094. this.setViewport( 0, 0, _canvas.width, _canvas.height );
  5095. };
  5096. this.setViewport = function ( x, y, width, height ) {
  5097. _viewportX = x;
  5098. _viewportY = y;
  5099. _viewportWidth = width;
  5100. _viewportHeight = height;
  5101. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  5102. };
  5103. this.setScissor = function ( x, y, width, height ) {
  5104. _gl.scissor( x, y, width, height );
  5105. };
  5106. this.enableScissorTest = function ( enable ) {
  5107. enable ? _gl.enable( _gl.SCISSOR_TEST ) : _gl.disable( _gl.SCISSOR_TEST );
  5108. };
  5109. // Clearing
  5110. this.setClearColorHex = function ( hex, alpha ) {
  5111. _clearColor.setHex( hex );
  5112. _clearAlpha = alpha;
  5113. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  5114. };
  5115. this.setClearColor = function ( color, alpha ) {
  5116. _clearColor.copy( color );
  5117. _clearAlpha = alpha;
  5118. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  5119. };
  5120. this.getClearColor = function () {
  5121. return _clearColor;
  5122. };
  5123. this.getClearAlpha = function () {
  5124. return _clearAlpha;
  5125. };
  5126. this.clear = function ( color, depth, stencil ) {
  5127. var bits = 0;
  5128. if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
  5129. if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
  5130. if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;
  5131. _gl.clear( bits );
  5132. };
  5133. this.clearTarget = function ( renderTarget, color, depth, stencil ) {
  5134. this.setRenderTarget( renderTarget );
  5135. this.clear( color, depth, stencil );
  5136. };
  5137. // Plugins
  5138. this.addPostPlugin = function ( plugin ) {
  5139. plugin.init( this );
  5140. this.renderPluginsPost.push( plugin );
  5141. };
  5142. this.addPrePlugin = function ( plugin ) {
  5143. plugin.init( this );
  5144. this.renderPluginsPre.push( plugin );
  5145. };
  5146. // Deallocation
  5147. this.deallocateObject = function ( object ) {
  5148. if ( ! object.__webglInit ) return;
  5149. object.__webglInit = false;
  5150. delete object._modelViewMatrix;
  5151. delete object._normalMatrix;
  5152. delete object._normalMatrixArray;
  5153. delete object._modelViewMatrixArray;
  5154. delete object._objectMatrixArray;
  5155. if ( object instanceof THREE.Mesh ) {
  5156. for ( var g in object.geometry.geometryGroups ) {
  5157. deleteMeshBuffers( object.geometry.geometryGroups[ g ] );
  5158. }
  5159. } else if ( object instanceof THREE.Ribbon ) {
  5160. deleteRibbonBuffers( object.geometry );
  5161. } else if ( object instanceof THREE.Line ) {
  5162. deleteLineBuffers( object.geometry );
  5163. } else if ( object instanceof THREE.ParticleSystem ) {
  5164. deleteParticleBuffers( object.geometry );
  5165. }
  5166. };
  5167. this.deallocateTexture = function ( texture ) {
  5168. if ( ! texture.__webglInit ) return;
  5169. texture.__webglInit = false;
  5170. _gl.deleteTexture( texture.__webglTexture );
  5171. _this.info.memory.textures --;
  5172. };
  5173. this.deallocateRenderTarget = function ( renderTarget ) {
  5174. if ( !renderTarget || ! renderTarget.__webglTexture ) return;
  5175. _gl.deleteTexture( renderTarget.__webglTexture );
  5176. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  5177. for ( var i = 0; i < 6; i ++ ) {
  5178. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] );
  5179. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] );
  5180. }
  5181. } else {
  5182. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer );
  5183. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer );
  5184. }
  5185. };
  5186. // Rendering
  5187. this.updateShadowMap = function ( scene, camera ) {
  5188. _currentProgram = null;
  5189. _oldBlending = -1;
  5190. _oldDepthTest = -1;
  5191. _oldDepthWrite = -1;
  5192. _currentGeometryGroupHash = -1;
  5193. _currentMaterialId = -1;
  5194. this.shadowMapPlugin.update( scene, camera );
  5195. };
  5196. // Internal functions
  5197. // Buffer allocation
  5198. function createParticleBuffers ( geometry ) {
  5199. geometry.__webglVertexBuffer = _gl.createBuffer();
  5200. geometry.__webglColorBuffer = _gl.createBuffer();
  5201. _this.info.geometries ++;
  5202. };
  5203. function createLineBuffers ( geometry ) {
  5204. geometry.__webglVertexBuffer = _gl.createBuffer();
  5205. geometry.__webglColorBuffer = _gl.createBuffer();
  5206. _this.info.memory.geometries ++;
  5207. };
  5208. function createRibbonBuffers ( geometry ) {
  5209. geometry.__webglVertexBuffer = _gl.createBuffer();
  5210. geometry.__webglColorBuffer = _gl.createBuffer();
  5211. _this.info.memory.geometries ++;
  5212. };
  5213. function createMeshBuffers ( geometryGroup ) {
  5214. geometryGroup.__webglVertexBuffer = _gl.createBuffer();
  5215. geometryGroup.__webglNormalBuffer = _gl.createBuffer();
  5216. geometryGroup.__webglTangentBuffer = _gl.createBuffer();
  5217. geometryGroup.__webglColorBuffer = _gl.createBuffer();
  5218. geometryGroup.__webglUVBuffer = _gl.createBuffer();
  5219. geometryGroup.__webglUV2Buffer = _gl.createBuffer();
  5220. geometryGroup.__webglSkinVertexABuffer = _gl.createBuffer();
  5221. geometryGroup.__webglSkinVertexBBuffer = _gl.createBuffer();
  5222. geometryGroup.__webglSkinIndicesBuffer = _gl.createBuffer();
  5223. geometryGroup.__webglSkinWeightsBuffer = _gl.createBuffer();
  5224. geometryGroup.__webglFaceBuffer = _gl.createBuffer();
  5225. geometryGroup.__webglLineBuffer = _gl.createBuffer();
  5226. var m, ml;
  5227. if ( geometryGroup.numMorphTargets ) {
  5228. geometryGroup.__webglMorphTargetsBuffers = [];
  5229. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  5230. geometryGroup.__webglMorphTargetsBuffers.push( _gl.createBuffer() );
  5231. }
  5232. }
  5233. if ( geometryGroup.numMorphNormals ) {
  5234. geometryGroup.__webglMorphNormalsBuffers = [];
  5235. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  5236. geometryGroup.__webglMorphNormalsBuffers.push( _gl.createBuffer() );
  5237. }
  5238. }
  5239. _this.info.memory.geometries ++;
  5240. };
  5241. // Buffer deallocation
  5242. function deleteParticleBuffers ( geometry ) {
  5243. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  5244. _gl.deleteBuffer( geometry.__webglColorBuffer );
  5245. _this.info.memory.geometries --;
  5246. };
  5247. function deleteLineBuffers ( geometry ) {
  5248. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  5249. _gl.deleteBuffer( geometry.__webglColorBuffer );
  5250. _this.info.memory.geometries --;
  5251. };
  5252. function deleteRibbonBuffers ( geometry ) {
  5253. _gl.deleteBuffer( geometry.__webglVertexBuffer );
  5254. _gl.deleteBuffer( geometry.__webglColorBuffer );
  5255. _this.info.memory.geometries --;
  5256. };
  5257. function deleteMeshBuffers ( geometryGroup ) {
  5258. _gl.deleteBuffer( geometryGroup.__webglVertexBuffer );
  5259. _gl.deleteBuffer( geometryGroup.__webglNormalBuffer );
  5260. _gl.deleteBuffer( geometryGroup.__webglTangentBuffer );
  5261. _gl.deleteBuffer( geometryGroup.__webglColorBuffer );
  5262. _gl.deleteBuffer( geometryGroup.__webglUVBuffer );
  5263. _gl.deleteBuffer( geometryGroup.__webglUV2Buffer );
  5264. _gl.deleteBuffer( geometryGroup.__webglSkinVertexABuffer );
  5265. _gl.deleteBuffer( geometryGroup.__webglSkinVertexBBuffer );
  5266. _gl.deleteBuffer( geometryGroup.__webglSkinIndicesBuffer );
  5267. _gl.deleteBuffer( geometryGroup.__webglSkinWeightsBuffer );
  5268. _gl.deleteBuffer( geometryGroup.__webglFaceBuffer );
  5269. _gl.deleteBuffer( geometryGroup.__webglLineBuffer );
  5270. var m, ml;
  5271. if ( geometryGroup.numMorphTargets ) {
  5272. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  5273. _gl.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] );
  5274. }
  5275. }
  5276. if ( geometryGroup.numMorphNormals ) {
  5277. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  5278. _gl.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] );
  5279. }
  5280. }
  5281. if ( geometryGroup.__webglCustomAttributesList ) {
  5282. for ( var id in geometryGroup.__webglCustomAttributesList ) {
  5283. _gl.deleteBuffer( geometryGroup.__webglCustomAttributesList[ id ].buffer );
  5284. }
  5285. }
  5286. _this.info.memory.geometries --;
  5287. };
  5288. // Buffer initialization
  5289. function initCustomAttributes ( geometry, object ) {
  5290. var nvertices = geometry.vertices.length;
  5291. var material = object.material;
  5292. if ( material.attributes ) {
  5293. if ( geometry.__webglCustomAttributesList === undefined ) {
  5294. geometry.__webglCustomAttributesList = [];
  5295. }
  5296. for ( var a in material.attributes ) {
  5297. var attribute = material.attributes[ a ];
  5298. if( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  5299. attribute.__webglInitialized = true;
  5300. var size = 1; // "f" and "i"
  5301. if ( attribute.type === "v2" ) size = 2;
  5302. else if ( attribute.type === "v3" ) size = 3;
  5303. else if ( attribute.type === "v4" ) size = 4;
  5304. else if ( attribute.type === "c" ) size = 3;
  5305. attribute.size = size;
  5306. attribute.array = new Float32Array( nvertices * size );
  5307. attribute.buffer = _gl.createBuffer();
  5308. attribute.buffer.belongsToAttribute = a;
  5309. attribute.needsUpdate = true;
  5310. }
  5311. geometry.__webglCustomAttributesList.push( attribute );
  5312. }
  5313. }
  5314. };
  5315. function initParticleBuffers ( geometry, object ) {
  5316. var nvertices = geometry.vertices.length;
  5317. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  5318. geometry.__colorArray = new Float32Array( nvertices * 3 );
  5319. geometry.__sortArray = [];
  5320. geometry.__webglParticleCount = nvertices;
  5321. initCustomAttributes ( geometry, object );
  5322. };
  5323. function initLineBuffers ( geometry, object ) {
  5324. var nvertices = geometry.vertices.length;
  5325. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  5326. geometry.__colorArray = new Float32Array( nvertices * 3 );
  5327. geometry.__webglLineCount = nvertices;
  5328. initCustomAttributes ( geometry, object );
  5329. };
  5330. function initRibbonBuffers ( geometry ) {
  5331. var nvertices = geometry.vertices.length;
  5332. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  5333. geometry.__colorArray = new Float32Array( nvertices * 3 );
  5334. geometry.__webglVertexCount = nvertices;
  5335. };
  5336. function initMeshBuffers ( geometryGroup, object ) {
  5337. var geometry = object.geometry,
  5338. faces3 = geometryGroup.faces3,
  5339. faces4 = geometryGroup.faces4,
  5340. nvertices = faces3.length * 3 + faces4.length * 4,
  5341. ntris = faces3.length * 1 + faces4.length * 2,
  5342. nlines = faces3.length * 3 + faces4.length * 4,
  5343. material = getBufferMaterial( object, geometryGroup ),
  5344. uvType = bufferGuessUVType( material ),
  5345. normalType = bufferGuessNormalType( material ),
  5346. vertexColorType = bufferGuessVertexColorType( material );
  5347. //console.log( "uvType", uvType, "normalType", normalType, "vertexColorType", vertexColorType, object, geometryGroup, material );
  5348. geometryGroup.__vertexArray = new Float32Array( nvertices * 3 );
  5349. if ( normalType ) {
  5350. geometryGroup.__normalArray = new Float32Array( nvertices * 3 );
  5351. }
  5352. if ( geometry.hasTangents ) {
  5353. geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
  5354. }
  5355. if ( vertexColorType ) {
  5356. geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
  5357. }
  5358. if ( uvType ) {
  5359. if ( geometry.faceUvs.length > 0 || geometry.faceVertexUvs.length > 0 ) {
  5360. geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
  5361. }
  5362. if ( geometry.faceUvs.length > 1 || geometry.faceVertexUvs.length > 1 ) {
  5363. geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
  5364. }
  5365. }
  5366. if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) {
  5367. geometryGroup.__skinVertexAArray = new Float32Array( nvertices * 4 );
  5368. geometryGroup.__skinVertexBArray = new Float32Array( nvertices * 4 );
  5369. geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
  5370. geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
  5371. }
  5372. geometryGroup.__faceArray = new Uint16Array( ntris * 3 );
  5373. geometryGroup.__lineArray = new Uint16Array( nlines * 2 );
  5374. var m, ml;
  5375. if ( geometryGroup.numMorphTargets ) {
  5376. geometryGroup.__morphTargetsArrays = [];
  5377. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  5378. geometryGroup.__morphTargetsArrays.push( new Float32Array( nvertices * 3 ) );
  5379. }
  5380. }
  5381. if ( geometryGroup.numMorphNormals ) {
  5382. geometryGroup.__morphNormalsArrays = [];
  5383. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  5384. geometryGroup.__morphNormalsArrays.push( new Float32Array( nvertices * 3 ) );
  5385. }
  5386. }
  5387. geometryGroup.__webglFaceCount = ntris * 3;
  5388. geometryGroup.__webglLineCount = nlines * 2;
  5389. // custom attributes
  5390. if ( material.attributes ) {
  5391. if ( geometryGroup.__webglCustomAttributesList === undefined ) {
  5392. geometryGroup.__webglCustomAttributesList = [];
  5393. }
  5394. for ( var a in material.attributes ) {
  5395. // Do a shallow copy of the attribute object so different geometryGroup chunks use different
  5396. // attribute buffers which are correctly indexed in the setMeshBuffers function
  5397. var originalAttribute = material.attributes[ a ];
  5398. var attribute = {};
  5399. for ( var property in originalAttribute ) {
  5400. attribute[ property ] = originalAttribute[ property ];
  5401. }
  5402. if( !attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  5403. attribute.__webglInitialized = true;
  5404. var size = 1; // "f" and "i"
  5405. if( attribute.type === "v2" ) size = 2;
  5406. else if( attribute.type === "v3" ) size = 3;
  5407. else if( attribute.type === "v4" ) size = 4;
  5408. else if( attribute.type === "c" ) size = 3;
  5409. attribute.size = size;
  5410. attribute.array = new Float32Array( nvertices * size );
  5411. attribute.buffer = _gl.createBuffer();
  5412. attribute.buffer.belongsToAttribute = a;
  5413. originalAttribute.needsUpdate = true;
  5414. attribute.__original = originalAttribute;
  5415. }
  5416. geometryGroup.__webglCustomAttributesList.push( attribute );
  5417. }
  5418. }
  5419. geometryGroup.__inittedArrays = true;
  5420. };
  5421. function getBufferMaterial( object, geometryGroup ) {
  5422. if ( object.material && ! ( object.material instanceof THREE.MeshFaceMaterial ) ) {
  5423. return object.material;
  5424. } else if ( geometryGroup.materialIndex >= 0 ) {
  5425. return object.geometry.materials[ geometryGroup.materialIndex ];
  5426. }
  5427. };
  5428. function materialNeedsSmoothNormals ( material ) {
  5429. return material && material.shading !== undefined && material.shading === THREE.SmoothShading;
  5430. };
  5431. function bufferGuessNormalType ( material ) {
  5432. // only MeshBasicMaterial and MeshDepthMaterial don't need normals
  5433. if ( ( material instanceof THREE.MeshBasicMaterial && !material.envMap ) || material instanceof THREE.MeshDepthMaterial ) {
  5434. return false;
  5435. }
  5436. if ( materialNeedsSmoothNormals( material ) ) {
  5437. return THREE.SmoothShading;
  5438. } else {
  5439. return THREE.FlatShading;
  5440. }
  5441. };
  5442. function bufferGuessVertexColorType ( material ) {
  5443. if ( material.vertexColors ) {
  5444. return material.vertexColors;
  5445. }
  5446. return false;
  5447. };
  5448. function bufferGuessUVType ( material ) {
  5449. // material must use some texture to require uvs
  5450. if ( material.map || material.lightMap || material instanceof THREE.ShaderMaterial ) {
  5451. return true;
  5452. }
  5453. return false;
  5454. };
  5455. // Buffer setting
  5456. function setParticleBuffers ( geometry, hint, object ) {
  5457. var v, c, vertex, offset, index, color,
  5458. vertices = geometry.vertices,
  5459. vl = vertices.length,
  5460. colors = geometry.colors,
  5461. cl = colors.length,
  5462. vertexArray = geometry.__vertexArray,
  5463. colorArray = geometry.__colorArray,
  5464. sortArray = geometry.__sortArray,
  5465. dirtyVertices = geometry.__dirtyVertices,
  5466. dirtyElements = geometry.__dirtyElements,
  5467. dirtyColors = geometry.__dirtyColors,
  5468. customAttributes = geometry.__webglCustomAttributesList,
  5469. i, il,
  5470. a, ca, cal, value,
  5471. customAttribute;
  5472. if ( object.sortParticles ) {
  5473. _projScreenMatrixPS.copy( _projScreenMatrix );
  5474. _projScreenMatrixPS.multiplySelf( object.matrixWorld );
  5475. for ( v = 0; v < vl; v ++ ) {
  5476. vertex = vertices[ v ];
  5477. _vector3.copy( vertex );
  5478. _projScreenMatrixPS.multiplyVector3( _vector3 );
  5479. sortArray[ v ] = [ _vector3.z, v ];
  5480. }
  5481. sortArray.sort( function( a, b ) { return b[ 0 ] - a[ 0 ]; } );
  5482. for ( v = 0; v < vl; v ++ ) {
  5483. vertex = vertices[ sortArray[v][1] ];
  5484. offset = v * 3;
  5485. vertexArray[ offset ] = vertex.x;
  5486. vertexArray[ offset + 1 ] = vertex.y;
  5487. vertexArray[ offset + 2 ] = vertex.z;
  5488. }
  5489. for ( c = 0; c < cl; c ++ ) {
  5490. offset = c * 3;
  5491. color = colors[ sortArray[c][1] ];
  5492. colorArray[ offset ] = color.r;
  5493. colorArray[ offset + 1 ] = color.g;
  5494. colorArray[ offset + 2 ] = color.b;
  5495. }
  5496. if ( customAttributes ) {
  5497. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5498. customAttribute = customAttributes[ i ];
  5499. if ( ! ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) ) continue;
  5500. offset = 0;
  5501. cal = customAttribute.value.length;
  5502. if ( customAttribute.size === 1 ) {
  5503. for ( ca = 0; ca < cal; ca ++ ) {
  5504. index = sortArray[ ca ][ 1 ];
  5505. customAttribute.array[ ca ] = customAttribute.value[ index ];
  5506. }
  5507. } else if ( customAttribute.size === 2 ) {
  5508. for ( ca = 0; ca < cal; ca ++ ) {
  5509. index = sortArray[ ca ][ 1 ];
  5510. value = customAttribute.value[ index ];
  5511. customAttribute.array[ offset ] = value.x;
  5512. customAttribute.array[ offset + 1 ] = value.y;
  5513. offset += 2;
  5514. }
  5515. } else if ( customAttribute.size === 3 ) {
  5516. if ( customAttribute.type === "c" ) {
  5517. for ( ca = 0; ca < cal; ca ++ ) {
  5518. index = sortArray[ ca ][ 1 ];
  5519. value = customAttribute.value[ index ];
  5520. customAttribute.array[ offset ] = value.r;
  5521. customAttribute.array[ offset + 1 ] = value.g;
  5522. customAttribute.array[ offset + 2 ] = value.b;
  5523. offset += 3;
  5524. }
  5525. } else {
  5526. for ( ca = 0; ca < cal; ca ++ ) {
  5527. index = sortArray[ ca ][ 1 ];
  5528. value = customAttribute.value[ index ];
  5529. customAttribute.array[ offset ] = value.x;
  5530. customAttribute.array[ offset + 1 ] = value.y;
  5531. customAttribute.array[ offset + 2 ] = value.z;
  5532. offset += 3;
  5533. }
  5534. }
  5535. } else if ( customAttribute.size === 4 ) {
  5536. for ( ca = 0; ca < cal; ca ++ ) {
  5537. index = sortArray[ ca ][ 1 ];
  5538. value = customAttribute.value[ index ];
  5539. customAttribute.array[ offset ] = value.x;
  5540. customAttribute.array[ offset + 1 ] = value.y;
  5541. customAttribute.array[ offset + 2 ] = value.z;
  5542. customAttribute.array[ offset + 3 ] = value.w;
  5543. offset += 4;
  5544. }
  5545. }
  5546. }
  5547. }
  5548. } else {
  5549. if ( dirtyVertices ) {
  5550. for ( v = 0; v < vl; v ++ ) {
  5551. vertex = vertices[ v ];
  5552. offset = v * 3;
  5553. vertexArray[ offset ] = vertex.x;
  5554. vertexArray[ offset + 1 ] = vertex.y;
  5555. vertexArray[ offset + 2 ] = vertex.z;
  5556. }
  5557. }
  5558. if ( dirtyColors ) {
  5559. for ( c = 0; c < cl; c ++ ) {
  5560. color = colors[ c ];
  5561. offset = c * 3;
  5562. colorArray[ offset ] = color.r;
  5563. colorArray[ offset + 1 ] = color.g;
  5564. colorArray[ offset + 2 ] = color.b;
  5565. }
  5566. }
  5567. if ( customAttributes ) {
  5568. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5569. customAttribute = customAttributes[ i ];
  5570. if ( customAttribute.needsUpdate &&
  5571. ( customAttribute.boundTo === undefined ||
  5572. customAttribute.boundTo === "vertices") ) {
  5573. cal = customAttribute.value.length;
  5574. offset = 0;
  5575. if ( customAttribute.size === 1 ) {
  5576. for ( ca = 0; ca < cal; ca ++ ) {
  5577. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  5578. }
  5579. } else if ( customAttribute.size === 2 ) {
  5580. for ( ca = 0; ca < cal; ca ++ ) {
  5581. value = customAttribute.value[ ca ];
  5582. customAttribute.array[ offset ] = value.x;
  5583. customAttribute.array[ offset + 1 ] = value.y;
  5584. offset += 2;
  5585. }
  5586. } else if ( customAttribute.size === 3 ) {
  5587. if ( customAttribute.type === "c" ) {
  5588. for ( ca = 0; ca < cal; ca ++ ) {
  5589. value = customAttribute.value[ ca ];
  5590. customAttribute.array[ offset ] = value.r;
  5591. customAttribute.array[ offset + 1 ] = value.g;
  5592. customAttribute.array[ offset + 2 ] = value.b;
  5593. offset += 3;
  5594. }
  5595. } else {
  5596. for ( ca = 0; ca < cal; ca ++ ) {
  5597. value = customAttribute.value[ ca ];
  5598. customAttribute.array[ offset ] = value.x;
  5599. customAttribute.array[ offset + 1 ] = value.y;
  5600. customAttribute.array[ offset + 2 ] = value.z;
  5601. offset += 3;
  5602. }
  5603. }
  5604. } else if ( customAttribute.size === 4 ) {
  5605. for ( ca = 0; ca < cal; ca ++ ) {
  5606. value = customAttribute.value[ ca ];
  5607. customAttribute.array[ offset ] = value.x;
  5608. customAttribute.array[ offset + 1 ] = value.y;
  5609. customAttribute.array[ offset + 2 ] = value.z;
  5610. customAttribute.array[ offset + 3 ] = value.w;
  5611. offset += 4;
  5612. }
  5613. }
  5614. }
  5615. }
  5616. }
  5617. }
  5618. if ( dirtyVertices || object.sortParticles ) {
  5619. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  5620. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5621. }
  5622. if ( dirtyColors || object.sortParticles ) {
  5623. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  5624. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  5625. }
  5626. if ( customAttributes ) {
  5627. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5628. customAttribute = customAttributes[ i ];
  5629. if ( customAttribute.needsUpdate || object.sortParticles ) {
  5630. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  5631. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  5632. }
  5633. }
  5634. }
  5635. };
  5636. function setLineBuffers ( geometry, hint ) {
  5637. var v, c, vertex, offset, color,
  5638. vertices = geometry.vertices,
  5639. colors = geometry.colors,
  5640. vl = vertices.length,
  5641. cl = colors.length,
  5642. vertexArray = geometry.__vertexArray,
  5643. colorArray = geometry.__colorArray,
  5644. dirtyVertices = geometry.__dirtyVertices,
  5645. dirtyColors = geometry.__dirtyColors,
  5646. customAttributes = geometry.__webglCustomAttributesList,
  5647. i, il,
  5648. a, ca, cal, value,
  5649. customAttribute;
  5650. if ( dirtyVertices ) {
  5651. for ( v = 0; v < vl; v ++ ) {
  5652. vertex = vertices[ v ];
  5653. offset = v * 3;
  5654. vertexArray[ offset ] = vertex.x;
  5655. vertexArray[ offset + 1 ] = vertex.y;
  5656. vertexArray[ offset + 2 ] = vertex.z;
  5657. }
  5658. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  5659. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5660. }
  5661. if ( dirtyColors ) {
  5662. for ( c = 0; c < cl; c ++ ) {
  5663. color = colors[ c ];
  5664. offset = c * 3;
  5665. colorArray[ offset ] = color.r;
  5666. colorArray[ offset + 1 ] = color.g;
  5667. colorArray[ offset + 2 ] = color.b;
  5668. }
  5669. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  5670. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  5671. }
  5672. if ( customAttributes ) {
  5673. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  5674. customAttribute = customAttributes[ i ];
  5675. if ( customAttribute.needsUpdate &&
  5676. ( customAttribute.boundTo === undefined ||
  5677. customAttribute.boundTo === "vertices" ) ) {
  5678. offset = 0;
  5679. cal = customAttribute.value.length;
  5680. if ( customAttribute.size === 1 ) {
  5681. for ( ca = 0; ca < cal; ca ++ ) {
  5682. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  5683. }
  5684. } else if ( customAttribute.size === 2 ) {
  5685. for ( ca = 0; ca < cal; ca ++ ) {
  5686. value = customAttribute.value[ ca ];
  5687. customAttribute.array[ offset ] = value.x;
  5688. customAttribute.array[ offset + 1 ] = value.y;
  5689. offset += 2;
  5690. }
  5691. } else if ( customAttribute.size === 3 ) {
  5692. if ( customAttribute.type === "c" ) {
  5693. for ( ca = 0; ca < cal; ca ++ ) {
  5694. value = customAttribute.value[ ca ];
  5695. customAttribute.array[ offset ] = value.r;
  5696. customAttribute.array[ offset + 1 ] = value.g;
  5697. customAttribute.array[ offset + 2 ] = value.b;
  5698. offset += 3;
  5699. }
  5700. } else {
  5701. for ( ca = 0; ca < cal; ca ++ ) {
  5702. value = customAttribute.value[ ca ];
  5703. customAttribute.array[ offset ] = value.x;
  5704. customAttribute.array[ offset + 1 ] = value.y;
  5705. customAttribute.array[ offset + 2 ] = value.z;
  5706. offset += 3;
  5707. }
  5708. }
  5709. } else if ( customAttribute.size === 4 ) {
  5710. for ( ca = 0; ca < cal; ca ++ ) {
  5711. value = customAttribute.value[ ca ];
  5712. customAttribute.array[ offset ] = value.x;
  5713. customAttribute.array[ offset + 1 ] = value.y;
  5714. customAttribute.array[ offset + 2 ] = value.z;
  5715. customAttribute.array[ offset + 3 ] = value.w;
  5716. offset += 4;
  5717. }
  5718. }
  5719. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  5720. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  5721. }
  5722. }
  5723. }
  5724. };
  5725. function setRibbonBuffers ( geometry, hint ) {
  5726. var v, c, vertex, offset, color,
  5727. vertices = geometry.vertices,
  5728. colors = geometry.colors,
  5729. vl = vertices.length,
  5730. cl = colors.length,
  5731. vertexArray = geometry.__vertexArray,
  5732. colorArray = geometry.__colorArray,
  5733. dirtyVertices = geometry.__dirtyVertices,
  5734. dirtyColors = geometry.__dirtyColors;
  5735. if ( dirtyVertices ) {
  5736. for ( v = 0; v < vl; v ++ ) {
  5737. vertex = vertices[ v ];
  5738. offset = v * 3;
  5739. vertexArray[ offset ] = vertex.x;
  5740. vertexArray[ offset + 1 ] = vertex.y;
  5741. vertexArray[ offset + 2 ] = vertex.z;
  5742. }
  5743. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  5744. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5745. }
  5746. if ( dirtyColors ) {
  5747. for ( c = 0; c < cl; c ++ ) {
  5748. color = colors[ c ];
  5749. offset = c * 3;
  5750. colorArray[ offset ] = color.r;
  5751. colorArray[ offset + 1 ] = color.g;
  5752. colorArray[ offset + 2 ] = color.b;
  5753. }
  5754. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  5755. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  5756. }
  5757. };
  5758. function setMeshBuffers( geometryGroup, object, hint, dispose, material ) {
  5759. if ( ! geometryGroup.__inittedArrays ) {
  5760. // console.log( object );
  5761. return;
  5762. }
  5763. var normalType = bufferGuessNormalType( material ),
  5764. vertexColorType = bufferGuessVertexColorType( material ),
  5765. uvType = bufferGuessUVType( material ),
  5766. needsSmoothNormals = ( normalType === THREE.SmoothShading );
  5767. var f, fl, fi, face,
  5768. vertexNormals, faceNormal, normal,
  5769. vertexColors, faceColor,
  5770. vertexTangents,
  5771. uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4, n1, n2, n3, n4,
  5772. c1, c2, c3, c4,
  5773. sw1, sw2, sw3, sw4,
  5774. si1, si2, si3, si4,
  5775. sa1, sa2, sa3, sa4,
  5776. sb1, sb2, sb3, sb4,
  5777. m, ml, i, il,
  5778. vn, uvi, uv2i,
  5779. vk, vkl, vka,
  5780. nka, chf, faceVertexNormals,
  5781. a,
  5782. vertexIndex = 0,
  5783. offset = 0,
  5784. offset_uv = 0,
  5785. offset_uv2 = 0,
  5786. offset_face = 0,
  5787. offset_normal = 0,
  5788. offset_tangent = 0,
  5789. offset_line = 0,
  5790. offset_color = 0,
  5791. offset_skin = 0,
  5792. offset_morphTarget = 0,
  5793. offset_custom = 0,
  5794. offset_customSrc = 0,
  5795. value,
  5796. vertexArray = geometryGroup.__vertexArray,
  5797. uvArray = geometryGroup.__uvArray,
  5798. uv2Array = geometryGroup.__uv2Array,
  5799. normalArray = geometryGroup.__normalArray,
  5800. tangentArray = geometryGroup.__tangentArray,
  5801. colorArray = geometryGroup.__colorArray,
  5802. skinVertexAArray = geometryGroup.__skinVertexAArray,
  5803. skinVertexBArray = geometryGroup.__skinVertexBArray,
  5804. skinIndexArray = geometryGroup.__skinIndexArray,
  5805. skinWeightArray = geometryGroup.__skinWeightArray,
  5806. morphTargetsArrays = geometryGroup.__morphTargetsArrays,
  5807. morphNormalsArrays = geometryGroup.__morphNormalsArrays,
  5808. customAttributes = geometryGroup.__webglCustomAttributesList,
  5809. customAttribute,
  5810. faceArray = geometryGroup.__faceArray,
  5811. lineArray = geometryGroup.__lineArray,
  5812. geometry = object.geometry, // this is shared for all chunks
  5813. dirtyVertices = geometry.__dirtyVertices,
  5814. dirtyElements = geometry.__dirtyElements,
  5815. dirtyUvs = geometry.__dirtyUvs,
  5816. dirtyNormals = geometry.__dirtyNormals,
  5817. dirtyTangents = geometry.__dirtyTangents,
  5818. dirtyColors = geometry.__dirtyColors,
  5819. dirtyMorphTargets = geometry.__dirtyMorphTargets,
  5820. vertices = geometry.vertices,
  5821. chunk_faces3 = geometryGroup.faces3,
  5822. chunk_faces4 = geometryGroup.faces4,
  5823. obj_faces = geometry.faces,
  5824. obj_uvs = geometry.faceVertexUvs[ 0 ],
  5825. obj_uvs2 = geometry.faceVertexUvs[ 1 ],
  5826. obj_colors = geometry.colors,
  5827. obj_skinVerticesA = geometry.skinVerticesA,
  5828. obj_skinVerticesB = geometry.skinVerticesB,
  5829. obj_skinIndices = geometry.skinIndices,
  5830. obj_skinWeights = geometry.skinWeights,
  5831. morphTargets = geometry.morphTargets,
  5832. morphNormals = geometry.morphNormals;
  5833. if ( dirtyVertices ) {
  5834. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  5835. face = obj_faces[ chunk_faces3[ f ] ];
  5836. v1 = vertices[ face.a ];
  5837. v2 = vertices[ face.b ];
  5838. v3 = vertices[ face.c ];
  5839. vertexArray[ offset ] = v1.x;
  5840. vertexArray[ offset + 1 ] = v1.y;
  5841. vertexArray[ offset + 2 ] = v1.z;
  5842. vertexArray[ offset + 3 ] = v2.x;
  5843. vertexArray[ offset + 4 ] = v2.y;
  5844. vertexArray[ offset + 5 ] = v2.z;
  5845. vertexArray[ offset + 6 ] = v3.x;
  5846. vertexArray[ offset + 7 ] = v3.y;
  5847. vertexArray[ offset + 8 ] = v3.z;
  5848. offset += 9;
  5849. }
  5850. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  5851. face = obj_faces[ chunk_faces4[ f ] ];
  5852. v1 = vertices[ face.a ];
  5853. v2 = vertices[ face.b ];
  5854. v3 = vertices[ face.c ];
  5855. v4 = vertices[ face.d ];
  5856. vertexArray[ offset ] = v1.x;
  5857. vertexArray[ offset + 1 ] = v1.y;
  5858. vertexArray[ offset + 2 ] = v1.z;
  5859. vertexArray[ offset + 3 ] = v2.x;
  5860. vertexArray[ offset + 4 ] = v2.y;
  5861. vertexArray[ offset + 5 ] = v2.z;
  5862. vertexArray[ offset + 6 ] = v3.x;
  5863. vertexArray[ offset + 7 ] = v3.y;
  5864. vertexArray[ offset + 8 ] = v3.z;
  5865. vertexArray[ offset + 9 ] = v4.x;
  5866. vertexArray[ offset + 10 ] = v4.y;
  5867. vertexArray[ offset + 11 ] = v4.z;
  5868. offset += 12;
  5869. }
  5870. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  5871. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  5872. }
  5873. if ( dirtyMorphTargets ) {
  5874. for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) {
  5875. offset_morphTarget = 0;
  5876. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  5877. chf = chunk_faces3[ f ];
  5878. face = obj_faces[ chf ];
  5879. // morph positions
  5880. v1 = morphTargets[ vk ].vertices[ face.a ];
  5881. v2 = morphTargets[ vk ].vertices[ face.b ];
  5882. v3 = morphTargets[ vk ].vertices[ face.c ];
  5883. vka = morphTargetsArrays[ vk ];
  5884. vka[ offset_morphTarget ] = v1.x;
  5885. vka[ offset_morphTarget + 1 ] = v1.y;
  5886. vka[ offset_morphTarget + 2 ] = v1.z;
  5887. vka[ offset_morphTarget + 3 ] = v2.x;
  5888. vka[ offset_morphTarget + 4 ] = v2.y;
  5889. vka[ offset_morphTarget + 5 ] = v2.z;
  5890. vka[ offset_morphTarget + 6 ] = v3.x;
  5891. vka[ offset_morphTarget + 7 ] = v3.y;
  5892. vka[ offset_morphTarget + 8 ] = v3.z;
  5893. // morph normals
  5894. if ( material.morphNormals ) {
  5895. if ( needsSmoothNormals ) {
  5896. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  5897. n1 = faceVertexNormals.a;
  5898. n2 = faceVertexNormals.b;
  5899. n3 = faceVertexNormals.c;
  5900. } else {
  5901. n1 = morphNormals[ vk ].faceNormals[ chf ];
  5902. n2 = n1;
  5903. n3 = n1;
  5904. }
  5905. nka = morphNormalsArrays[ vk ];
  5906. nka[ offset_morphTarget ] = n1.x;
  5907. nka[ offset_morphTarget + 1 ] = n1.y;
  5908. nka[ offset_morphTarget + 2 ] = n1.z;
  5909. nka[ offset_morphTarget + 3 ] = n2.x;
  5910. nka[ offset_morphTarget + 4 ] = n2.y;
  5911. nka[ offset_morphTarget + 5 ] = n2.z;
  5912. nka[ offset_morphTarget + 6 ] = n3.x;
  5913. nka[ offset_morphTarget + 7 ] = n3.y;
  5914. nka[ offset_morphTarget + 8 ] = n3.z;
  5915. }
  5916. //
  5917. offset_morphTarget += 9;
  5918. }
  5919. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  5920. chf = chunk_faces4[ f ];
  5921. face = obj_faces[ chf ];
  5922. // morph positions
  5923. v1 = morphTargets[ vk ].vertices[ face.a ];
  5924. v2 = morphTargets[ vk ].vertices[ face.b ];
  5925. v3 = morphTargets[ vk ].vertices[ face.c ];
  5926. v4 = morphTargets[ vk ].vertices[ face.d ];
  5927. vka = morphTargetsArrays[ vk ];
  5928. vka[ offset_morphTarget ] = v1.x;
  5929. vka[ offset_morphTarget + 1 ] = v1.y;
  5930. vka[ offset_morphTarget + 2 ] = v1.z;
  5931. vka[ offset_morphTarget + 3 ] = v2.x;
  5932. vka[ offset_morphTarget + 4 ] = v2.y;
  5933. vka[ offset_morphTarget + 5 ] = v2.z;
  5934. vka[ offset_morphTarget + 6 ] = v3.x;
  5935. vka[ offset_morphTarget + 7 ] = v3.y;
  5936. vka[ offset_morphTarget + 8 ] = v3.z;
  5937. vka[ offset_morphTarget + 9 ] = v4.x;
  5938. vka[ offset_morphTarget + 10 ] = v4.y;
  5939. vka[ offset_morphTarget + 11 ] = v4.z;
  5940. // morph normals
  5941. if ( material.morphNormals ) {
  5942. if ( needsSmoothNormals ) {
  5943. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  5944. n1 = faceVertexNormals.a;
  5945. n2 = faceVertexNormals.b;
  5946. n3 = faceVertexNormals.c;
  5947. n4 = faceVertexNormals.d;
  5948. } else {
  5949. n1 = morphNormals[ vk ].faceNormals[ chf ];
  5950. n2 = n1;
  5951. n3 = n1;
  5952. n4 = n1;
  5953. }
  5954. nka = morphNormalsArrays[ vk ];
  5955. nka[ offset_morphTarget ] = n1.x;
  5956. nka[ offset_morphTarget + 1 ] = n1.y;
  5957. nka[ offset_morphTarget + 2 ] = n1.z;
  5958. nka[ offset_morphTarget + 3 ] = n2.x;
  5959. nka[ offset_morphTarget + 4 ] = n2.y;
  5960. nka[ offset_morphTarget + 5 ] = n2.z;
  5961. nka[ offset_morphTarget + 6 ] = n3.x;
  5962. nka[ offset_morphTarget + 7 ] = n3.y;
  5963. nka[ offset_morphTarget + 8 ] = n3.z;
  5964. nka[ offset_morphTarget + 9 ] = n4.x;
  5965. nka[ offset_morphTarget + 10 ] = n4.y;
  5966. nka[ offset_morphTarget + 11 ] = n4.z;
  5967. }
  5968. //
  5969. offset_morphTarget += 12;
  5970. }
  5971. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ vk ] );
  5972. _gl.bufferData( _gl.ARRAY_BUFFER, morphTargetsArrays[ vk ], hint );
  5973. if ( material.morphNormals ) {
  5974. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ vk ] );
  5975. _gl.bufferData( _gl.ARRAY_BUFFER, morphNormalsArrays[ vk ], hint );
  5976. }
  5977. }
  5978. }
  5979. if ( obj_skinWeights.length ) {
  5980. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  5981. face = obj_faces[ chunk_faces3[ f ] ];
  5982. // weights
  5983. sw1 = obj_skinWeights[ face.a ];
  5984. sw2 = obj_skinWeights[ face.b ];
  5985. sw3 = obj_skinWeights[ face.c ];
  5986. skinWeightArray[ offset_skin ] = sw1.x;
  5987. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  5988. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  5989. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  5990. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  5991. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  5992. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  5993. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  5994. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  5995. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  5996. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  5997. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  5998. // indices
  5999. si1 = obj_skinIndices[ face.a ];
  6000. si2 = obj_skinIndices[ face.b ];
  6001. si3 = obj_skinIndices[ face.c ];
  6002. skinIndexArray[ offset_skin ] = si1.x;
  6003. skinIndexArray[ offset_skin + 1 ] = si1.y;
  6004. skinIndexArray[ offset_skin + 2 ] = si1.z;
  6005. skinIndexArray[ offset_skin + 3 ] = si1.w;
  6006. skinIndexArray[ offset_skin + 4 ] = si2.x;
  6007. skinIndexArray[ offset_skin + 5 ] = si2.y;
  6008. skinIndexArray[ offset_skin + 6 ] = si2.z;
  6009. skinIndexArray[ offset_skin + 7 ] = si2.w;
  6010. skinIndexArray[ offset_skin + 8 ] = si3.x;
  6011. skinIndexArray[ offset_skin + 9 ] = si3.y;
  6012. skinIndexArray[ offset_skin + 10 ] = si3.z;
  6013. skinIndexArray[ offset_skin + 11 ] = si3.w;
  6014. // vertices A
  6015. sa1 = obj_skinVerticesA[ face.a ];
  6016. sa2 = obj_skinVerticesA[ face.b ];
  6017. sa3 = obj_skinVerticesA[ face.c ];
  6018. skinVertexAArray[ offset_skin ] = sa1.x;
  6019. skinVertexAArray[ offset_skin + 1 ] = sa1.y;
  6020. skinVertexAArray[ offset_skin + 2 ] = sa1.z;
  6021. skinVertexAArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6022. skinVertexAArray[ offset_skin + 4 ] = sa2.x;
  6023. skinVertexAArray[ offset_skin + 5 ] = sa2.y;
  6024. skinVertexAArray[ offset_skin + 6 ] = sa2.z;
  6025. skinVertexAArray[ offset_skin + 7 ] = 1;
  6026. skinVertexAArray[ offset_skin + 8 ] = sa3.x;
  6027. skinVertexAArray[ offset_skin + 9 ] = sa3.y;
  6028. skinVertexAArray[ offset_skin + 10 ] = sa3.z;
  6029. skinVertexAArray[ offset_skin + 11 ] = 1;
  6030. // vertices B
  6031. sb1 = obj_skinVerticesB[ face.a ];
  6032. sb2 = obj_skinVerticesB[ face.b ];
  6033. sb3 = obj_skinVerticesB[ face.c ];
  6034. skinVertexBArray[ offset_skin ] = sb1.x;
  6035. skinVertexBArray[ offset_skin + 1 ] = sb1.y;
  6036. skinVertexBArray[ offset_skin + 2 ] = sb1.z;
  6037. skinVertexBArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6038. skinVertexBArray[ offset_skin + 4 ] = sb2.x;
  6039. skinVertexBArray[ offset_skin + 5 ] = sb2.y;
  6040. skinVertexBArray[ offset_skin + 6 ] = sb2.z;
  6041. skinVertexBArray[ offset_skin + 7 ] = 1;
  6042. skinVertexBArray[ offset_skin + 8 ] = sb3.x;
  6043. skinVertexBArray[ offset_skin + 9 ] = sb3.y;
  6044. skinVertexBArray[ offset_skin + 10 ] = sb3.z;
  6045. skinVertexBArray[ offset_skin + 11 ] = 1;
  6046. offset_skin += 12;
  6047. }
  6048. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6049. face = obj_faces[ chunk_faces4[ f ] ];
  6050. // weights
  6051. sw1 = obj_skinWeights[ face.a ];
  6052. sw2 = obj_skinWeights[ face.b ];
  6053. sw3 = obj_skinWeights[ face.c ];
  6054. sw4 = obj_skinWeights[ face.d ];
  6055. skinWeightArray[ offset_skin ] = sw1.x;
  6056. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  6057. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  6058. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  6059. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  6060. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  6061. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  6062. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  6063. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  6064. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  6065. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  6066. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  6067. skinWeightArray[ offset_skin + 12 ] = sw4.x;
  6068. skinWeightArray[ offset_skin + 13 ] = sw4.y;
  6069. skinWeightArray[ offset_skin + 14 ] = sw4.z;
  6070. skinWeightArray[ offset_skin + 15 ] = sw4.w;
  6071. // indices
  6072. si1 = obj_skinIndices[ face.a ];
  6073. si2 = obj_skinIndices[ face.b ];
  6074. si3 = obj_skinIndices[ face.c ];
  6075. si4 = obj_skinIndices[ face.d ];
  6076. skinIndexArray[ offset_skin ] = si1.x;
  6077. skinIndexArray[ offset_skin + 1 ] = si1.y;
  6078. skinIndexArray[ offset_skin + 2 ] = si1.z;
  6079. skinIndexArray[ offset_skin + 3 ] = si1.w;
  6080. skinIndexArray[ offset_skin + 4 ] = si2.x;
  6081. skinIndexArray[ offset_skin + 5 ] = si2.y;
  6082. skinIndexArray[ offset_skin + 6 ] = si2.z;
  6083. skinIndexArray[ offset_skin + 7 ] = si2.w;
  6084. skinIndexArray[ offset_skin + 8 ] = si3.x;
  6085. skinIndexArray[ offset_skin + 9 ] = si3.y;
  6086. skinIndexArray[ offset_skin + 10 ] = si3.z;
  6087. skinIndexArray[ offset_skin + 11 ] = si3.w;
  6088. skinIndexArray[ offset_skin + 12 ] = si4.x;
  6089. skinIndexArray[ offset_skin + 13 ] = si4.y;
  6090. skinIndexArray[ offset_skin + 14 ] = si4.z;
  6091. skinIndexArray[ offset_skin + 15 ] = si4.w;
  6092. // vertices A
  6093. sa1 = obj_skinVerticesA[ face.a ];
  6094. sa2 = obj_skinVerticesA[ face.b ];
  6095. sa3 = obj_skinVerticesA[ face.c ];
  6096. sa4 = obj_skinVerticesA[ face.d ];
  6097. skinVertexAArray[ offset_skin ] = sa1.x;
  6098. skinVertexAArray[ offset_skin + 1 ] = sa1.y;
  6099. skinVertexAArray[ offset_skin + 2 ] = sa1.z;
  6100. skinVertexAArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6101. skinVertexAArray[ offset_skin + 4 ] = sa2.x;
  6102. skinVertexAArray[ offset_skin + 5 ] = sa2.y;
  6103. skinVertexAArray[ offset_skin + 6 ] = sa2.z;
  6104. skinVertexAArray[ offset_skin + 7 ] = 1;
  6105. skinVertexAArray[ offset_skin + 8 ] = sa3.x;
  6106. skinVertexAArray[ offset_skin + 9 ] = sa3.y;
  6107. skinVertexAArray[ offset_skin + 10 ] = sa3.z;
  6108. skinVertexAArray[ offset_skin + 11 ] = 1;
  6109. skinVertexAArray[ offset_skin + 12 ] = sa4.x;
  6110. skinVertexAArray[ offset_skin + 13 ] = sa4.y;
  6111. skinVertexAArray[ offset_skin + 14 ] = sa4.z;
  6112. skinVertexAArray[ offset_skin + 15 ] = 1;
  6113. // vertices B
  6114. sb1 = obj_skinVerticesB[ face.a ];
  6115. sb2 = obj_skinVerticesB[ face.b ];
  6116. sb3 = obj_skinVerticesB[ face.c ];
  6117. sb4 = obj_skinVerticesB[ face.d ];
  6118. skinVertexBArray[ offset_skin ] = sb1.x;
  6119. skinVertexBArray[ offset_skin + 1 ] = sb1.y;
  6120. skinVertexBArray[ offset_skin + 2 ] = sb1.z;
  6121. skinVertexBArray[ offset_skin + 3 ] = 1; // pad for faster vertex shader
  6122. skinVertexBArray[ offset_skin + 4 ] = sb2.x;
  6123. skinVertexBArray[ offset_skin + 5 ] = sb2.y;
  6124. skinVertexBArray[ offset_skin + 6 ] = sb2.z;
  6125. skinVertexBArray[ offset_skin + 7 ] = 1;
  6126. skinVertexBArray[ offset_skin + 8 ] = sb3.x;
  6127. skinVertexBArray[ offset_skin + 9 ] = sb3.y;
  6128. skinVertexBArray[ offset_skin + 10 ] = sb3.z;
  6129. skinVertexBArray[ offset_skin + 11 ] = 1;
  6130. skinVertexBArray[ offset_skin + 12 ] = sb4.x;
  6131. skinVertexBArray[ offset_skin + 13 ] = sb4.y;
  6132. skinVertexBArray[ offset_skin + 14 ] = sb4.z;
  6133. skinVertexBArray[ offset_skin + 15 ] = 1;
  6134. offset_skin += 16;
  6135. }
  6136. if ( offset_skin > 0 ) {
  6137. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexABuffer );
  6138. _gl.bufferData( _gl.ARRAY_BUFFER, skinVertexAArray, hint );
  6139. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexBBuffer );
  6140. _gl.bufferData( _gl.ARRAY_BUFFER, skinVertexBArray, hint );
  6141. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  6142. _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint );
  6143. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  6144. _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint );
  6145. }
  6146. }
  6147. if ( dirtyColors && vertexColorType ) {
  6148. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6149. face = obj_faces[ chunk_faces3[ f ] ];
  6150. vertexColors = face.vertexColors;
  6151. faceColor = face.color;
  6152. if ( vertexColors.length === 3 && vertexColorType === THREE.VertexColors ) {
  6153. c1 = vertexColors[ 0 ];
  6154. c2 = vertexColors[ 1 ];
  6155. c3 = vertexColors[ 2 ];
  6156. } else {
  6157. c1 = faceColor;
  6158. c2 = faceColor;
  6159. c3 = faceColor;
  6160. }
  6161. colorArray[ offset_color ] = c1.r;
  6162. colorArray[ offset_color + 1 ] = c1.g;
  6163. colorArray[ offset_color + 2 ] = c1.b;
  6164. colorArray[ offset_color + 3 ] = c2.r;
  6165. colorArray[ offset_color + 4 ] = c2.g;
  6166. colorArray[ offset_color + 5 ] = c2.b;
  6167. colorArray[ offset_color + 6 ] = c3.r;
  6168. colorArray[ offset_color + 7 ] = c3.g;
  6169. colorArray[ offset_color + 8 ] = c3.b;
  6170. offset_color += 9;
  6171. }
  6172. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6173. face = obj_faces[ chunk_faces4[ f ] ];
  6174. vertexColors = face.vertexColors;
  6175. faceColor = face.color;
  6176. if ( vertexColors.length === 4 && vertexColorType === THREE.VertexColors ) {
  6177. c1 = vertexColors[ 0 ];
  6178. c2 = vertexColors[ 1 ];
  6179. c3 = vertexColors[ 2 ];
  6180. c4 = vertexColors[ 3 ];
  6181. } else {
  6182. c1 = faceColor;
  6183. c2 = faceColor;
  6184. c3 = faceColor;
  6185. c4 = faceColor;
  6186. }
  6187. colorArray[ offset_color ] = c1.r;
  6188. colorArray[ offset_color + 1 ] = c1.g;
  6189. colorArray[ offset_color + 2 ] = c1.b;
  6190. colorArray[ offset_color + 3 ] = c2.r;
  6191. colorArray[ offset_color + 4 ] = c2.g;
  6192. colorArray[ offset_color + 5 ] = c2.b;
  6193. colorArray[ offset_color + 6 ] = c3.r;
  6194. colorArray[ offset_color + 7 ] = c3.g;
  6195. colorArray[ offset_color + 8 ] = c3.b;
  6196. colorArray[ offset_color + 9 ] = c4.r;
  6197. colorArray[ offset_color + 10 ] = c4.g;
  6198. colorArray[ offset_color + 11 ] = c4.b;
  6199. offset_color += 12;
  6200. }
  6201. if ( offset_color > 0 ) {
  6202. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  6203. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  6204. }
  6205. }
  6206. if ( dirtyTangents && geometry.hasTangents ) {
  6207. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6208. face = obj_faces[ chunk_faces3[ f ] ];
  6209. vertexTangents = face.vertexTangents;
  6210. t1 = vertexTangents[ 0 ];
  6211. t2 = vertexTangents[ 1 ];
  6212. t3 = vertexTangents[ 2 ];
  6213. tangentArray[ offset_tangent ] = t1.x;
  6214. tangentArray[ offset_tangent + 1 ] = t1.y;
  6215. tangentArray[ offset_tangent + 2 ] = t1.z;
  6216. tangentArray[ offset_tangent + 3 ] = t1.w;
  6217. tangentArray[ offset_tangent + 4 ] = t2.x;
  6218. tangentArray[ offset_tangent + 5 ] = t2.y;
  6219. tangentArray[ offset_tangent + 6 ] = t2.z;
  6220. tangentArray[ offset_tangent + 7 ] = t2.w;
  6221. tangentArray[ offset_tangent + 8 ] = t3.x;
  6222. tangentArray[ offset_tangent + 9 ] = t3.y;
  6223. tangentArray[ offset_tangent + 10 ] = t3.z;
  6224. tangentArray[ offset_tangent + 11 ] = t3.w;
  6225. offset_tangent += 12;
  6226. }
  6227. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6228. face = obj_faces[ chunk_faces4[ f ] ];
  6229. vertexTangents = face.vertexTangents;
  6230. t1 = vertexTangents[ 0 ];
  6231. t2 = vertexTangents[ 1 ];
  6232. t3 = vertexTangents[ 2 ];
  6233. t4 = vertexTangents[ 3 ];
  6234. tangentArray[ offset_tangent ] = t1.x;
  6235. tangentArray[ offset_tangent + 1 ] = t1.y;
  6236. tangentArray[ offset_tangent + 2 ] = t1.z;
  6237. tangentArray[ offset_tangent + 3 ] = t1.w;
  6238. tangentArray[ offset_tangent + 4 ] = t2.x;
  6239. tangentArray[ offset_tangent + 5 ] = t2.y;
  6240. tangentArray[ offset_tangent + 6 ] = t2.z;
  6241. tangentArray[ offset_tangent + 7 ] = t2.w;
  6242. tangentArray[ offset_tangent + 8 ] = t3.x;
  6243. tangentArray[ offset_tangent + 9 ] = t3.y;
  6244. tangentArray[ offset_tangent + 10 ] = t3.z;
  6245. tangentArray[ offset_tangent + 11 ] = t3.w;
  6246. tangentArray[ offset_tangent + 12 ] = t4.x;
  6247. tangentArray[ offset_tangent + 13 ] = t4.y;
  6248. tangentArray[ offset_tangent + 14 ] = t4.z;
  6249. tangentArray[ offset_tangent + 15 ] = t4.w;
  6250. offset_tangent += 16;
  6251. }
  6252. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  6253. _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint );
  6254. }
  6255. if ( dirtyNormals && normalType ) {
  6256. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6257. face = obj_faces[ chunk_faces3[ f ] ];
  6258. vertexNormals = face.vertexNormals;
  6259. faceNormal = face.normal;
  6260. if ( vertexNormals.length === 3 && needsSmoothNormals ) {
  6261. for ( i = 0; i < 3; i ++ ) {
  6262. vn = vertexNormals[ i ];
  6263. normalArray[ offset_normal ] = vn.x;
  6264. normalArray[ offset_normal + 1 ] = vn.y;
  6265. normalArray[ offset_normal + 2 ] = vn.z;
  6266. offset_normal += 3;
  6267. }
  6268. } else {
  6269. for ( i = 0; i < 3; i ++ ) {
  6270. normalArray[ offset_normal ] = faceNormal.x;
  6271. normalArray[ offset_normal + 1 ] = faceNormal.y;
  6272. normalArray[ offset_normal + 2 ] = faceNormal.z;
  6273. offset_normal += 3;
  6274. }
  6275. }
  6276. }
  6277. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6278. face = obj_faces[ chunk_faces4[ f ] ];
  6279. vertexNormals = face.vertexNormals;
  6280. faceNormal = face.normal;
  6281. if ( vertexNormals.length === 4 && needsSmoothNormals ) {
  6282. for ( i = 0; i < 4; i ++ ) {
  6283. vn = vertexNormals[ i ];
  6284. normalArray[ offset_normal ] = vn.x;
  6285. normalArray[ offset_normal + 1 ] = vn.y;
  6286. normalArray[ offset_normal + 2 ] = vn.z;
  6287. offset_normal += 3;
  6288. }
  6289. } else {
  6290. for ( i = 0; i < 4; i ++ ) {
  6291. normalArray[ offset_normal ] = faceNormal.x;
  6292. normalArray[ offset_normal + 1 ] = faceNormal.y;
  6293. normalArray[ offset_normal + 2 ] = faceNormal.z;
  6294. offset_normal += 3;
  6295. }
  6296. }
  6297. }
  6298. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  6299. _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
  6300. }
  6301. if ( dirtyUvs && obj_uvs && uvType ) {
  6302. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6303. fi = chunk_faces3[ f ];
  6304. face = obj_faces[ fi ];
  6305. uv = obj_uvs[ fi ];
  6306. if ( uv === undefined ) continue;
  6307. for ( i = 0; i < 3; i ++ ) {
  6308. uvi = uv[ i ];
  6309. uvArray[ offset_uv ] = uvi.u;
  6310. uvArray[ offset_uv + 1 ] = uvi.v;
  6311. offset_uv += 2;
  6312. }
  6313. }
  6314. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6315. fi = chunk_faces4[ f ];
  6316. face = obj_faces[ fi ];
  6317. uv = obj_uvs[ fi ];
  6318. if ( uv === undefined ) continue;
  6319. for ( i = 0; i < 4; i ++ ) {
  6320. uvi = uv[ i ];
  6321. uvArray[ offset_uv ] = uvi.u;
  6322. uvArray[ offset_uv + 1 ] = uvi.v;
  6323. offset_uv += 2;
  6324. }
  6325. }
  6326. if ( offset_uv > 0 ) {
  6327. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  6328. _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint );
  6329. }
  6330. }
  6331. if ( dirtyUvs && obj_uvs2 && uvType ) {
  6332. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6333. fi = chunk_faces3[ f ];
  6334. face = obj_faces[ fi ];
  6335. uv2 = obj_uvs2[ fi ];
  6336. if ( uv2 === undefined ) continue;
  6337. for ( i = 0; i < 3; i ++ ) {
  6338. uv2i = uv2[ i ];
  6339. uv2Array[ offset_uv2 ] = uv2i.u;
  6340. uv2Array[ offset_uv2 + 1 ] = uv2i.v;
  6341. offset_uv2 += 2;
  6342. }
  6343. }
  6344. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6345. fi = chunk_faces4[ f ];
  6346. face = obj_faces[ fi ];
  6347. uv2 = obj_uvs2[ fi ];
  6348. if ( uv2 === undefined ) continue;
  6349. for ( i = 0; i < 4; i ++ ) {
  6350. uv2i = uv2[ i ];
  6351. uv2Array[ offset_uv2 ] = uv2i.u;
  6352. uv2Array[ offset_uv2 + 1 ] = uv2i.v;
  6353. offset_uv2 += 2;
  6354. }
  6355. }
  6356. if ( offset_uv2 > 0 ) {
  6357. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  6358. _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint );
  6359. }
  6360. }
  6361. if ( dirtyElements ) {
  6362. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6363. face = obj_faces[ chunk_faces3[ f ] ];
  6364. faceArray[ offset_face ] = vertexIndex;
  6365. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  6366. faceArray[ offset_face + 2 ] = vertexIndex + 2;
  6367. offset_face += 3;
  6368. lineArray[ offset_line ] = vertexIndex;
  6369. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  6370. lineArray[ offset_line + 2 ] = vertexIndex;
  6371. lineArray[ offset_line + 3 ] = vertexIndex + 2;
  6372. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  6373. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  6374. offset_line += 6;
  6375. vertexIndex += 3;
  6376. }
  6377. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6378. face = obj_faces[ chunk_faces4[ f ] ];
  6379. faceArray[ offset_face ] = vertexIndex;
  6380. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  6381. faceArray[ offset_face + 2 ] = vertexIndex + 3;
  6382. faceArray[ offset_face + 3 ] = vertexIndex + 1;
  6383. faceArray[ offset_face + 4 ] = vertexIndex + 2;
  6384. faceArray[ offset_face + 5 ] = vertexIndex + 3;
  6385. offset_face += 6;
  6386. lineArray[ offset_line ] = vertexIndex;
  6387. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  6388. lineArray[ offset_line + 2 ] = vertexIndex;
  6389. lineArray[ offset_line + 3 ] = vertexIndex + 3;
  6390. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  6391. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  6392. lineArray[ offset_line + 6 ] = vertexIndex + 2;
  6393. lineArray[ offset_line + 7 ] = vertexIndex + 3;
  6394. offset_line += 8;
  6395. vertexIndex += 4;
  6396. }
  6397. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  6398. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint );
  6399. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  6400. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
  6401. }
  6402. if ( customAttributes ) {
  6403. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  6404. customAttribute = customAttributes[ i ];
  6405. if ( ! customAttribute.__original.needsUpdate ) continue;
  6406. offset_custom = 0;
  6407. offset_customSrc = 0;
  6408. if ( customAttribute.size === 1 ) {
  6409. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6410. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6411. face = obj_faces[ chunk_faces3[ f ] ];
  6412. customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
  6413. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  6414. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  6415. offset_custom += 3;
  6416. }
  6417. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6418. face = obj_faces[ chunk_faces4[ 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. customAttribute.array[ offset_custom + 3 ] = customAttribute.value[ face.d ];
  6423. offset_custom += 4;
  6424. }
  6425. } else if ( customAttribute.boundTo === "faces" ) {
  6426. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6427. value = customAttribute.value[ chunk_faces3[ f ] ];
  6428. customAttribute.array[ offset_custom ] = value;
  6429. customAttribute.array[ offset_custom + 1 ] = value;
  6430. customAttribute.array[ offset_custom + 2 ] = value;
  6431. offset_custom += 3;
  6432. }
  6433. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6434. value = customAttribute.value[ chunk_faces4[ f ] ];
  6435. customAttribute.array[ offset_custom ] = value;
  6436. customAttribute.array[ offset_custom + 1 ] = value;
  6437. customAttribute.array[ offset_custom + 2 ] = value;
  6438. customAttribute.array[ offset_custom + 3 ] = value;
  6439. offset_custom += 4;
  6440. }
  6441. }
  6442. } else if ( customAttribute.size === 2 ) {
  6443. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6444. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6445. face = obj_faces[ chunk_faces3[ f ] ];
  6446. v1 = customAttribute.value[ face.a ];
  6447. v2 = customAttribute.value[ face.b ];
  6448. v3 = customAttribute.value[ face.c ];
  6449. customAttribute.array[ offset_custom ] = v1.x;
  6450. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6451. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6452. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6453. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6454. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6455. offset_custom += 6;
  6456. }
  6457. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6458. face = obj_faces[ chunk_faces4[ f ] ];
  6459. v1 = customAttribute.value[ face.a ];
  6460. v2 = customAttribute.value[ face.b ];
  6461. v3 = customAttribute.value[ face.c ];
  6462. v4 = customAttribute.value[ face.d ];
  6463. customAttribute.array[ offset_custom ] = v1.x;
  6464. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6465. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6466. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6467. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6468. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6469. customAttribute.array[ offset_custom + 6 ] = v4.x;
  6470. customAttribute.array[ offset_custom + 7 ] = v4.y;
  6471. offset_custom += 8;
  6472. }
  6473. } else if ( customAttribute.boundTo === "faces" ) {
  6474. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6475. value = customAttribute.value[ chunk_faces3[ f ] ];
  6476. v1 = value;
  6477. v2 = value;
  6478. v3 = value;
  6479. customAttribute.array[ offset_custom ] = v1.x;
  6480. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6481. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6482. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6483. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6484. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6485. offset_custom += 6;
  6486. }
  6487. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6488. value = customAttribute.value[ chunk_faces4[ f ] ];
  6489. v1 = value;
  6490. v2 = value;
  6491. v3 = value;
  6492. v4 = value;
  6493. customAttribute.array[ offset_custom ] = v1.x;
  6494. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6495. customAttribute.array[ offset_custom + 2 ] = v2.x;
  6496. customAttribute.array[ offset_custom + 3 ] = v2.y;
  6497. customAttribute.array[ offset_custom + 4 ] = v3.x;
  6498. customAttribute.array[ offset_custom + 5 ] = v3.y;
  6499. customAttribute.array[ offset_custom + 6 ] = v4.x;
  6500. customAttribute.array[ offset_custom + 7 ] = v4.y;
  6501. offset_custom += 8;
  6502. }
  6503. }
  6504. } else if ( customAttribute.size === 3 ) {
  6505. var pp;
  6506. if ( customAttribute.type === "c" ) {
  6507. pp = [ "r", "g", "b" ];
  6508. } else {
  6509. pp = [ "x", "y", "z" ];
  6510. }
  6511. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6512. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6513. face = obj_faces[ chunk_faces3[ f ] ];
  6514. v1 = customAttribute.value[ face.a ];
  6515. v2 = customAttribute.value[ face.b ];
  6516. v3 = customAttribute.value[ face.c ];
  6517. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6518. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6519. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6520. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6521. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6522. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6523. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6524. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6525. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6526. offset_custom += 9;
  6527. }
  6528. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6529. face = obj_faces[ chunk_faces4[ f ] ];
  6530. v1 = customAttribute.value[ face.a ];
  6531. v2 = customAttribute.value[ face.b ];
  6532. v3 = customAttribute.value[ face.c ];
  6533. v4 = customAttribute.value[ face.d ];
  6534. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6535. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6536. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6537. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6538. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6539. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6540. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6541. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6542. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6543. customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
  6544. customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
  6545. customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
  6546. offset_custom += 12;
  6547. }
  6548. } else if ( customAttribute.boundTo === "faces" ) {
  6549. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6550. value = customAttribute.value[ chunk_faces3[ f ] ];
  6551. v1 = value;
  6552. v2 = value;
  6553. v3 = value;
  6554. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6555. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6556. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6557. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6558. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6559. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6560. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6561. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6562. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6563. offset_custom += 9;
  6564. }
  6565. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6566. value = customAttribute.value[ chunk_faces4[ f ] ];
  6567. v1 = value;
  6568. v2 = value;
  6569. v3 = value;
  6570. v4 = value;
  6571. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  6572. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  6573. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  6574. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  6575. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  6576. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  6577. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  6578. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  6579. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  6580. customAttribute.array[ offset_custom + 9 ] = v4[ pp[ 0 ] ];
  6581. customAttribute.array[ offset_custom + 10 ] = v4[ pp[ 1 ] ];
  6582. customAttribute.array[ offset_custom + 11 ] = v4[ pp[ 2 ] ];
  6583. offset_custom += 12;
  6584. }
  6585. }
  6586. } else if ( customAttribute.size === 4 ) {
  6587. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === "vertices" ) {
  6588. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6589. face = obj_faces[ chunk_faces3[ f ] ];
  6590. v1 = customAttribute.value[ face.a ];
  6591. v2 = customAttribute.value[ face.b ];
  6592. v3 = customAttribute.value[ face.c ];
  6593. customAttribute.array[ offset_custom ] = v1.x;
  6594. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6595. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6596. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6597. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6598. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6599. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6600. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6601. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6602. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6603. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6604. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6605. offset_custom += 12;
  6606. }
  6607. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6608. face = obj_faces[ chunk_faces4[ f ] ];
  6609. v1 = customAttribute.value[ face.a ];
  6610. v2 = customAttribute.value[ face.b ];
  6611. v3 = customAttribute.value[ face.c ];
  6612. v4 = customAttribute.value[ face.d ];
  6613. customAttribute.array[ offset_custom ] = v1.x;
  6614. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6615. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6616. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6617. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6618. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6619. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6620. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6621. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6622. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6623. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6624. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6625. customAttribute.array[ offset_custom + 12 ] = v4.x;
  6626. customAttribute.array[ offset_custom + 13 ] = v4.y;
  6627. customAttribute.array[ offset_custom + 14 ] = v4.z;
  6628. customAttribute.array[ offset_custom + 15 ] = v4.w;
  6629. offset_custom += 16;
  6630. }
  6631. } else if ( customAttribute.boundTo === "faces" ) {
  6632. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  6633. value = customAttribute.value[ chunk_faces3[ f ] ];
  6634. v1 = value;
  6635. v2 = value;
  6636. v3 = value;
  6637. customAttribute.array[ offset_custom ] = v1.x;
  6638. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6639. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6640. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6641. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6642. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6643. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6644. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6645. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6646. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6647. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6648. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6649. offset_custom += 12;
  6650. }
  6651. for ( f = 0, fl = chunk_faces4.length; f < fl; f ++ ) {
  6652. value = customAttribute.value[ chunk_faces4[ f ] ];
  6653. v1 = value;
  6654. v2 = value;
  6655. v3 = value;
  6656. v4 = value;
  6657. customAttribute.array[ offset_custom ] = v1.x;
  6658. customAttribute.array[ offset_custom + 1 ] = v1.y;
  6659. customAttribute.array[ offset_custom + 2 ] = v1.z;
  6660. customAttribute.array[ offset_custom + 3 ] = v1.w;
  6661. customAttribute.array[ offset_custom + 4 ] = v2.x;
  6662. customAttribute.array[ offset_custom + 5 ] = v2.y;
  6663. customAttribute.array[ offset_custom + 6 ] = v2.z;
  6664. customAttribute.array[ offset_custom + 7 ] = v2.w;
  6665. customAttribute.array[ offset_custom + 8 ] = v3.x;
  6666. customAttribute.array[ offset_custom + 9 ] = v3.y;
  6667. customAttribute.array[ offset_custom + 10 ] = v3.z;
  6668. customAttribute.array[ offset_custom + 11 ] = v3.w;
  6669. customAttribute.array[ offset_custom + 12 ] = v4.x;
  6670. customAttribute.array[ offset_custom + 13 ] = v4.y;
  6671. customAttribute.array[ offset_custom + 14 ] = v4.z;
  6672. customAttribute.array[ offset_custom + 15 ] = v4.w;
  6673. offset_custom += 16;
  6674. }
  6675. }
  6676. }
  6677. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  6678. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  6679. }
  6680. }
  6681. if ( dispose ) {
  6682. delete geometryGroup.__inittedArrays;
  6683. delete geometryGroup.__colorArray;
  6684. delete geometryGroup.__normalArray;
  6685. delete geometryGroup.__tangentArray;
  6686. delete geometryGroup.__uvArray;
  6687. delete geometryGroup.__uv2Array;
  6688. delete geometryGroup.__faceArray;
  6689. delete geometryGroup.__vertexArray;
  6690. delete geometryGroup.__lineArray;
  6691. delete geometryGroup.__skinVertexAArray;
  6692. delete geometryGroup.__skinVertexBArray;
  6693. delete geometryGroup.__skinIndexArray;
  6694. delete geometryGroup.__skinWeightArray;
  6695. }
  6696. };
  6697. // Buffer rendering
  6698. this.renderBufferImmediate = function ( object, program, shading ) {
  6699. if ( ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
  6700. if ( ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
  6701. if ( object.hasPos ) {
  6702. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
  6703. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  6704. _gl.enableVertexAttribArray( program.attributes.position );
  6705. _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6706. }
  6707. if ( object.hasNormal ) {
  6708. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer );
  6709. if ( shading === THREE.FlatShading ) {
  6710. var nx, ny, nz,
  6711. nax, nbx, ncx, nay, nby, ncy, naz, nbz, ncz,
  6712. normalArray,
  6713. i, il = object.count * 3;
  6714. for( i = 0; i < il; i += 9 ) {
  6715. normalArray = object.normalArray;
  6716. nax = normalArray[ i ];
  6717. nay = normalArray[ i + 1 ];
  6718. naz = normalArray[ i + 2 ];
  6719. nbx = normalArray[ i + 3 ];
  6720. nby = normalArray[ i + 4 ];
  6721. nbz = normalArray[ i + 5 ];
  6722. ncx = normalArray[ i + 6 ];
  6723. ncy = normalArray[ i + 7 ];
  6724. ncz = normalArray[ i + 8 ];
  6725. nx = ( nax + nbx + ncx ) / 3;
  6726. ny = ( nay + nby + ncy ) / 3;
  6727. nz = ( naz + nbz + ncz ) / 3;
  6728. normalArray[ i ] = nx;
  6729. normalArray[ i + 1 ] = ny;
  6730. normalArray[ i + 2 ] = nz;
  6731. normalArray[ i + 3 ] = nx;
  6732. normalArray[ i + 4 ] = ny;
  6733. normalArray[ i + 5 ] = nz;
  6734. normalArray[ i + 6 ] = nx;
  6735. normalArray[ i + 7 ] = ny;
  6736. normalArray[ i + 8 ] = nz;
  6737. }
  6738. }
  6739. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  6740. _gl.enableVertexAttribArray( program.attributes.normal );
  6741. _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  6742. }
  6743. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  6744. object.count = 0;
  6745. };
  6746. this.renderBufferDirect = function ( camera, lights, fog, material, geometryGroup, object ) {
  6747. if ( material.opacity === 0 ) return;
  6748. var program, attributes, linewidth, primitives, a, attribute;
  6749. program = setProgram( camera, lights, fog, material, object );
  6750. attributes = program.attributes;
  6751. var updateBuffers = false,
  6752. wireframeBit = material.wireframe ? 1 : 0,
  6753. geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  6754. if ( geometryGroupHash !== _currentGeometryGroupHash ) {
  6755. _currentGeometryGroupHash = geometryGroupHash;
  6756. updateBuffers = true;
  6757. }
  6758. // render mesh
  6759. if ( object instanceof THREE.Mesh ) {
  6760. var offsets = geometryGroup.offsets;
  6761. for ( var i = 0, il = offsets.length; i < il; ++ i ) {
  6762. if ( updateBuffers ) {
  6763. // vertices
  6764. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexPositionBuffer );
  6765. _gl.vertexAttribPointer( attributes.position, geometryGroup.vertexPositionBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 3 );
  6766. // normals
  6767. if ( attributes.normal >= 0 && geometryGroup.vertexNormalBuffer ) {
  6768. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexNormalBuffer );
  6769. _gl.vertexAttribPointer( attributes.normal, geometryGroup.vertexNormalBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 3 );
  6770. }
  6771. // uvs
  6772. if ( attributes.uv >= 0 && geometryGroup.vertexUvBuffer ) {
  6773. if ( geometryGroup.vertexUvBuffer ) {
  6774. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexUvBuffer );
  6775. _gl.vertexAttribPointer( attributes.uv, geometryGroup.vertexUvBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 2 );
  6776. _gl.enableVertexAttribArray( attributes.uv );
  6777. } else {
  6778. _gl.disableVertexAttribArray( attributes.uv );
  6779. }
  6780. }
  6781. // colors
  6782. if ( attributes.color >= 0 && geometryGroup.vertexColorBuffer ) {
  6783. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.vertexColorBuffer );
  6784. _gl.vertexAttribPointer( attributes.color, geometryGroup.vertexColorBuffer.itemSize, _gl.FLOAT, false, 0, offsets[ i ].index * 4 * 4 );
  6785. }
  6786. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.vertexIndexBuffer );
  6787. }
  6788. // render indexed triangles
  6789. _gl.drawElements( _gl.TRIANGLES, offsets[ i ].count, _gl.UNSIGNED_SHORT, offsets[ i ].start * 2 ); // 2 = Uint16
  6790. _this.info.render.calls ++;
  6791. _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
  6792. _this.info.render.faces += offsets[ i ].count / 3;
  6793. }
  6794. }
  6795. };
  6796. this.renderBuffer = function ( camera, lights, fog, material, geometryGroup, object ) {
  6797. if ( material.opacity === 0 ) return;
  6798. var program, attributes, linewidth, primitives, a, attribute, i, il;
  6799. program = setProgram( camera, lights, fog, material, object );
  6800. attributes = program.attributes;
  6801. var updateBuffers = false,
  6802. wireframeBit = material.wireframe ? 1 : 0,
  6803. geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  6804. if ( geometryGroupHash !== _currentGeometryGroupHash ) {
  6805. _currentGeometryGroupHash = geometryGroupHash;
  6806. updateBuffers = true;
  6807. }
  6808. // vertices
  6809. if ( !material.morphTargets && attributes.position >= 0 ) {
  6810. if ( updateBuffers ) {
  6811. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  6812. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6813. }
  6814. } else {
  6815. if ( object.morphTargetBase ) {
  6816. setupMorphTargets( material, geometryGroup, object );
  6817. }
  6818. }
  6819. if ( updateBuffers ) {
  6820. // custom attributes
  6821. // Use the per-geometryGroup custom attribute arrays which are setup in initMeshBuffers
  6822. if ( geometryGroup.__webglCustomAttributesList ) {
  6823. for ( i = 0, il = geometryGroup.__webglCustomAttributesList.length; i < il; i ++ ) {
  6824. attribute = geometryGroup.__webglCustomAttributesList[ i ];
  6825. if( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) {
  6826. _gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer );
  6827. _gl.vertexAttribPointer( attributes[ attribute.buffer.belongsToAttribute ], attribute.size, _gl.FLOAT, false, 0, 0 );
  6828. }
  6829. }
  6830. }
  6831. // colors
  6832. if ( attributes.color >= 0 ) {
  6833. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  6834. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  6835. }
  6836. // normals
  6837. if ( attributes.normal >= 0 ) {
  6838. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  6839. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  6840. }
  6841. // tangents
  6842. if ( attributes.tangent >= 0 ) {
  6843. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  6844. _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
  6845. }
  6846. // uvs
  6847. if ( attributes.uv >= 0 ) {
  6848. if ( geometryGroup.__webglUVBuffer ) {
  6849. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  6850. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  6851. _gl.enableVertexAttribArray( attributes.uv );
  6852. } else {
  6853. _gl.disableVertexAttribArray( attributes.uv );
  6854. }
  6855. }
  6856. if ( attributes.uv2 >= 0 ) {
  6857. if ( geometryGroup.__webglUV2Buffer ) {
  6858. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  6859. _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
  6860. _gl.enableVertexAttribArray( attributes.uv2 );
  6861. } else {
  6862. _gl.disableVertexAttribArray( attributes.uv2 );
  6863. }
  6864. }
  6865. if ( material.skinning &&
  6866. attributes.skinVertexA >= 0 && attributes.skinVertexB >= 0 &&
  6867. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  6868. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexABuffer );
  6869. _gl.vertexAttribPointer( attributes.skinVertexA, 4, _gl.FLOAT, false, 0, 0 );
  6870. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinVertexBBuffer );
  6871. _gl.vertexAttribPointer( attributes.skinVertexB, 4, _gl.FLOAT, false, 0, 0 );
  6872. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  6873. _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
  6874. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  6875. _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
  6876. }
  6877. }
  6878. // render mesh
  6879. if ( object instanceof THREE.Mesh ) {
  6880. // wireframe
  6881. if ( material.wireframe ) {
  6882. setLineWidth( material.wireframeLinewidth );
  6883. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  6884. _gl.drawElements( _gl.LINES, geometryGroup.__webglLineCount, _gl.UNSIGNED_SHORT, 0 );
  6885. // triangles
  6886. } else {
  6887. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  6888. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, _gl.UNSIGNED_SHORT, 0 );
  6889. }
  6890. _this.info.render.calls ++;
  6891. _this.info.render.vertices += geometryGroup.__webglFaceCount;
  6892. _this.info.render.faces += geometryGroup.__webglFaceCount / 3;
  6893. // render lines
  6894. } else if ( object instanceof THREE.Line ) {
  6895. primitives = ( object.type === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  6896. setLineWidth( material.linewidth );
  6897. _gl.drawArrays( primitives, 0, geometryGroup.__webglLineCount );
  6898. _this.info.render.calls ++;
  6899. // render particles
  6900. } else if ( object instanceof THREE.ParticleSystem ) {
  6901. _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webglParticleCount );
  6902. _this.info.render.calls ++;
  6903. _this.info.render.points += geometryGroup.__webglParticleCount;
  6904. // render ribbon
  6905. } else if ( object instanceof THREE.Ribbon ) {
  6906. _gl.drawArrays( _gl.TRIANGLE_STRIP, 0, geometryGroup.__webglVertexCount );
  6907. _this.info.render.calls ++;
  6908. }
  6909. };
  6910. function setupMorphTargets ( material, geometryGroup, object ) {
  6911. // set base
  6912. var attributes = material.program.attributes;
  6913. if ( object.morphTargetBase !== - 1 ) {
  6914. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
  6915. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6916. } else if ( attributes.position >= 0 ) {
  6917. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  6918. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  6919. }
  6920. if ( object.morphTargetForcedOrder.length ) {
  6921. // set forced order
  6922. var m = 0;
  6923. var order = object.morphTargetForcedOrder;
  6924. var influences = object.morphTargetInfluences;
  6925. while ( m < material.numSupportedMorphTargets && m < order.length ) {
  6926. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
  6927. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6928. if ( material.morphNormals ) {
  6929. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] );
  6930. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6931. }
  6932. object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ];
  6933. m ++;
  6934. }
  6935. } else {
  6936. // find most influencing
  6937. var used = [];
  6938. var candidateInfluence = - 1;
  6939. var candidate = 0;
  6940. var influences = object.morphTargetInfluences;
  6941. var i, il = influences.length;
  6942. var m = 0;
  6943. if ( object.morphTargetBase !== - 1 ) {
  6944. used[ object.morphTargetBase ] = true;
  6945. }
  6946. while ( m < material.numSupportedMorphTargets ) {
  6947. for ( i = 0; i < il; i ++ ) {
  6948. if ( !used[ i ] && influences[ i ] > candidateInfluence ) {
  6949. candidate = i;
  6950. candidateInfluence = influences[ candidate ];
  6951. }
  6952. }
  6953. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ candidate ] );
  6954. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6955. if ( material.morphNormals ) {
  6956. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ candidate ] );
  6957. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  6958. }
  6959. object.__webglMorphTargetInfluences[ m ] = candidateInfluence;
  6960. used[ candidate ] = 1;
  6961. candidateInfluence = -1;
  6962. m ++;
  6963. }
  6964. }
  6965. // load updated influences uniform
  6966. if( material.program.uniforms.morphTargetInfluences !== null ) {
  6967. _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
  6968. }
  6969. };
  6970. function painterSort ( a, b ) {
  6971. return b.z - a.z;
  6972. };
  6973. // Rendering
  6974. this.render = function ( scene, camera, renderTarget, forceClear ) {
  6975. var i, il,
  6976. webglObject, object,
  6977. renderList,
  6978. lights = scene.__lights,
  6979. fog = scene.fog;
  6980. _currentMaterialId = -1;
  6981. // update scene graph
  6982. if ( camera.parent === undefined ) {
  6983. console.warn( 'DEPRECATED: Camera hasn\'t been added to a Scene. Adding it...' );
  6984. scene.add( camera );
  6985. }
  6986. if ( this.autoUpdateScene ) scene.updateMatrixWorld();
  6987. // update camera matrices and frustum
  6988. if ( ! camera._viewMatrixArray ) camera._viewMatrixArray = new Float32Array( 16 );
  6989. if ( ! camera._projectionMatrixArray ) camera._projectionMatrixArray = new Float32Array( 16 );
  6990. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  6991. camera.matrixWorldInverse.flattenToArray( camera._viewMatrixArray );
  6992. camera.projectionMatrix.flattenToArray( camera._projectionMatrixArray );
  6993. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  6994. _frustum.setFromMatrix( _projScreenMatrix );
  6995. // update WebGL objects
  6996. if ( this.autoUpdateObjects ) this.initWebGLObjects( scene );
  6997. // custom render plugins (pre pass)
  6998. renderPlugins( this.renderPluginsPre, scene, camera );
  6999. //
  7000. _this.info.render.calls = 0;
  7001. _this.info.render.vertices = 0;
  7002. _this.info.render.faces = 0;
  7003. _this.info.render.points = 0;
  7004. this.setRenderTarget( renderTarget );
  7005. if ( this.autoClear || forceClear ) {
  7006. this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
  7007. }
  7008. // set matrices for regular objects (frustum culled)
  7009. renderList = scene.__webglObjects;
  7010. for ( i = 0, il = renderList.length; i < il; i ++ ) {
  7011. webglObject = renderList[ i ];
  7012. object = webglObject.object;
  7013. webglObject.render = false;
  7014. if ( object.visible ) {
  7015. if ( ! ( object instanceof THREE.Mesh || object instanceof THREE.ParticleSystem ) || ! ( object.frustumCulled ) || _frustum.contains( object ) ) {
  7016. //object.matrixWorld.flattenToArray( object._objectMatrixArray );
  7017. setupMatrices( object, camera );
  7018. unrollBufferMaterial( webglObject );
  7019. webglObject.render = true;
  7020. if ( this.sortObjects ) {
  7021. if ( object.renderDepth ) {
  7022. webglObject.z = object.renderDepth;
  7023. } else {
  7024. _vector3.copy( object.matrixWorld.getPosition() );
  7025. _projScreenMatrix.multiplyVector3( _vector3 );
  7026. webglObject.z = _vector3.z;
  7027. }
  7028. }
  7029. }
  7030. }
  7031. }
  7032. if ( this.sortObjects ) {
  7033. renderList.sort( painterSort );
  7034. }
  7035. // set matrices for immediate objects
  7036. renderList = scene.__webglObjectsImmediate;
  7037. for ( i = 0, il = renderList.length; i < il; i ++ ) {
  7038. webglObject = renderList[ i ];
  7039. object = webglObject.object;
  7040. if ( object.visible ) {
  7041. if( object.matrixAutoUpdate ) {
  7042. //object.matrixWorld.flattenToArray( object._objectMatrixArray );
  7043. }
  7044. setupMatrices( object, camera );
  7045. unrollImmediateBufferMaterial( webglObject );
  7046. }
  7047. }
  7048. if ( scene.overrideMaterial ) {
  7049. var material = scene.overrideMaterial;
  7050. this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  7051. this.setDepthTest( material.depthTest );
  7052. this.setDepthWrite( material.depthWrite );
  7053. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  7054. renderObjects( scene.__webglObjects, false, "", camera, lights, fog, true, material );
  7055. renderObjectsImmediate( scene.__webglObjectsImmediate, "", camera, lights, fog, false, material );
  7056. } else {
  7057. // opaque pass (front-to-back order)
  7058. this.setBlending( THREE.NormalBlending );
  7059. renderObjects( scene.__webglObjects, true, "opaque", camera, lights, fog, false );
  7060. renderObjectsImmediate( scene.__webglObjectsImmediate, "opaque", camera, lights, fog, false );
  7061. // transparent pass (back-to-front order)
  7062. renderObjects( scene.__webglObjects, false, "transparent", camera, lights, fog, true );
  7063. renderObjectsImmediate( scene.__webglObjectsImmediate, "transparent", camera, lights, fog, true );
  7064. }
  7065. // custom render plugins (post pass)
  7066. renderPlugins( this.renderPluginsPost, scene, camera );
  7067. // Generate mipmap if we're using any kind of mipmap filtering
  7068. if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
  7069. updateRenderTargetMipmap( renderTarget );
  7070. }
  7071. // Ensure depth buffer writing is enabled so it can be cleared on next render
  7072. this.setDepthTest( true );
  7073. this.setDepthWrite( true );
  7074. // _gl.finish();
  7075. };
  7076. function renderPlugins( plugins, scene, camera ) {
  7077. if ( ! plugins.length ) return;
  7078. for ( var i = 0, il = plugins.length; i < il; i ++ ) {
  7079. _currentProgram = null;
  7080. _currentCamera = null;
  7081. _oldBlending = -1;
  7082. _oldDepthTest = -1;
  7083. _oldDepthWrite = -1;
  7084. _currentGeometryGroupHash = -1;
  7085. _currentMaterialId = -1;
  7086. plugins[ i ].render( scene, camera, _currentWidth, _currentHeight );
  7087. _currentProgram = null;
  7088. _currentCamera = null;
  7089. _oldBlending = -1;
  7090. _oldDepthTest = -1;
  7091. _oldDepthWrite = -1;
  7092. _currentGeometryGroupHash = -1;
  7093. _currentMaterialId = -1;
  7094. }
  7095. };
  7096. function renderObjects ( renderList, reverse, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  7097. var webglObject, object, buffer, material, start, end, delta;
  7098. if ( reverse ) {
  7099. start = renderList.length - 1;
  7100. end = -1;
  7101. delta = -1;
  7102. } else {
  7103. start = 0;
  7104. end = renderList.length;
  7105. delta = 1;
  7106. }
  7107. for ( var i = start; i !== end; i += delta ) {
  7108. webglObject = renderList[ i ];
  7109. if ( webglObject.render ) {
  7110. object = webglObject.object;
  7111. buffer = webglObject.buffer;
  7112. if ( overrideMaterial ) {
  7113. material = overrideMaterial;
  7114. } else {
  7115. material = webglObject[ materialType ];
  7116. if ( ! material ) continue;
  7117. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  7118. _this.setDepthTest( material.depthTest );
  7119. _this.setDepthWrite( material.depthWrite );
  7120. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  7121. }
  7122. _this.setObjectFaces( object );
  7123. if ( buffer instanceof THREE.BufferGeometry ) {
  7124. _this.renderBufferDirect( camera, lights, fog, material, buffer, object );
  7125. } else {
  7126. _this.renderBuffer( camera, lights, fog, material, buffer, object );
  7127. }
  7128. }
  7129. }
  7130. };
  7131. function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  7132. var webglObject, object, material, program;
  7133. for ( var i = 0, il = renderList.length; i < il; i ++ ) {
  7134. webglObject = renderList[ i ];
  7135. object = webglObject.object;
  7136. if ( object.visible ) {
  7137. if ( overrideMaterial ) {
  7138. material = overrideMaterial;
  7139. } else {
  7140. material = webglObject[ materialType ];
  7141. if ( ! material ) continue;
  7142. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  7143. _this.setDepthTest( material.depthTest );
  7144. _this.setDepthWrite( material.depthWrite );
  7145. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  7146. }
  7147. _this.renderImmediateObject( camera, lights, fog, material, object );
  7148. }
  7149. }
  7150. };
  7151. this.renderImmediateObject = function ( camera, lights, fog, material, object ) {
  7152. var program = setProgram( camera, lights, fog, material, object );
  7153. _currentGeometryGroupHash = -1;
  7154. _this.setObjectFaces( object );
  7155. if ( object.immediateRenderCallback ) {
  7156. object.immediateRenderCallback( program, _gl, _frustum );
  7157. } else {
  7158. object.render( function( object ) { _this.renderBufferImmediate( object, program, material.shading ); } );
  7159. }
  7160. };
  7161. function unrollImmediateBufferMaterial ( globject ) {
  7162. var object = globject.object,
  7163. material = object.material;
  7164. if ( material.transparent ) {
  7165. globject.transparent = material;
  7166. globject.opaque = null;
  7167. } else {
  7168. globject.opaque = material;
  7169. globject.transparent = null;
  7170. }
  7171. };
  7172. function unrollBufferMaterial ( globject ) {
  7173. var object = globject.object,
  7174. buffer = globject.buffer,
  7175. material, materialIndex, meshMaterial;
  7176. meshMaterial = object.material;
  7177. if ( meshMaterial instanceof THREE.MeshFaceMaterial ) {
  7178. materialIndex = buffer.materialIndex;
  7179. if ( materialIndex >= 0 ) {
  7180. material = object.geometry.materials[ materialIndex ];
  7181. if ( material.transparent ) {
  7182. globject.transparent = material;
  7183. globject.opaque = null;
  7184. } else {
  7185. globject.opaque = material;
  7186. globject.transparent = null;
  7187. }
  7188. }
  7189. } else {
  7190. material = meshMaterial;
  7191. if ( material ) {
  7192. if ( material.transparent ) {
  7193. globject.transparent = material;
  7194. globject.opaque = null;
  7195. } else {
  7196. globject.opaque = material;
  7197. globject.transparent = null;
  7198. }
  7199. }
  7200. }
  7201. };
  7202. // Geometry splitting
  7203. function sortFacesByMaterial ( geometry ) {
  7204. var f, fl, face, materialIndex, vertices,
  7205. materialHash, groupHash,
  7206. hash_map = {};
  7207. var numMorphTargets = geometry.morphTargets.length;
  7208. var numMorphNormals = geometry.morphNormals.length;
  7209. geometry.geometryGroups = {};
  7210. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  7211. face = geometry.faces[ f ];
  7212. materialIndex = face.materialIndex;
  7213. materialHash = ( materialIndex !== undefined ) ? materialIndex : -1;
  7214. if ( hash_map[ materialHash ] === undefined ) {
  7215. hash_map[ materialHash ] = { 'hash': materialHash, 'counter': 0 };
  7216. }
  7217. groupHash = hash_map[ materialHash ].hash + '_' + hash_map[ materialHash ].counter;
  7218. if ( geometry.geometryGroups[ groupHash ] === undefined ) {
  7219. geometry.geometryGroups[ groupHash ] = { 'faces3': [], 'faces4': [], 'materialIndex': materialIndex, 'vertices': 0, 'numMorphTargets': numMorphTargets, 'numMorphNormals': numMorphNormals };
  7220. }
  7221. vertices = face instanceof THREE.Face3 ? 3 : 4;
  7222. if ( geometry.geometryGroups[ groupHash ].vertices + vertices > 65535 ) {
  7223. hash_map[ materialHash ].counter += 1;
  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. }
  7229. if ( face instanceof THREE.Face3 ) {
  7230. geometry.geometryGroups[ groupHash ].faces3.push( f );
  7231. } else {
  7232. geometry.geometryGroups[ groupHash ].faces4.push( f );
  7233. }
  7234. geometry.geometryGroups[ groupHash ].vertices += vertices;
  7235. }
  7236. geometry.geometryGroupsList = [];
  7237. for ( var g in geometry.geometryGroups ) {
  7238. geometry.geometryGroups[ g ].id = _geometryGroupCounter ++;
  7239. geometry.geometryGroupsList.push( geometry.geometryGroups[ g ] );
  7240. }
  7241. };
  7242. // Objects refresh
  7243. this.initWebGLObjects = function ( scene ) {
  7244. if ( !scene.__webglObjects ) {
  7245. scene.__webglObjects = [];
  7246. scene.__webglObjectsImmediate = [];
  7247. scene.__webglSprites = [];
  7248. scene.__webglFlares = [];
  7249. }
  7250. while ( scene.__objectsAdded.length ) {
  7251. addObject( scene.__objectsAdded[ 0 ], scene );
  7252. scene.__objectsAdded.splice( 0, 1 );
  7253. }
  7254. while ( scene.__objectsRemoved.length ) {
  7255. removeObject( scene.__objectsRemoved[ 0 ], scene );
  7256. scene.__objectsRemoved.splice( 0, 1 );
  7257. }
  7258. // update must be called after objects adding / removal
  7259. for ( var o = 0, ol = scene.__webglObjects.length; o < ol; o ++ ) {
  7260. updateObject( scene.__webglObjects[ o ].object );
  7261. }
  7262. };
  7263. // Objects adding
  7264. function addObject ( object, scene ) {
  7265. var g, geometry, geometryGroup;
  7266. if ( ! object.__webglInit ) {
  7267. object.__webglInit = true;
  7268. object._modelViewMatrix = new THREE.Matrix4();
  7269. object._normalMatrix = new THREE.Matrix3();
  7270. //object._normalMatrixArray = new Float32Array( 9 );
  7271. //object._modelViewMatrixArray = new Float32Array( 16 );
  7272. //object._objectMatrixArray = new Float32Array( 16 );
  7273. //object.matrixWorld.flattenToArray( object._objectMatrixArray );
  7274. if ( object instanceof THREE.Mesh ) {
  7275. geometry = object.geometry;
  7276. if ( geometry instanceof THREE.Geometry ) {
  7277. if ( geometry.geometryGroups === undefined ) {
  7278. sortFacesByMaterial( geometry );
  7279. }
  7280. // create separate VBOs per geometry chunk
  7281. for ( g in geometry.geometryGroups ) {
  7282. geometryGroup = geometry.geometryGroups[ g ];
  7283. // initialise VBO on the first access
  7284. if ( ! geometryGroup.__webglVertexBuffer ) {
  7285. createMeshBuffers( geometryGroup );
  7286. initMeshBuffers( geometryGroup, object );
  7287. geometry.__dirtyVertices = true;
  7288. geometry.__dirtyMorphTargets = true;
  7289. geometry.__dirtyElements = true;
  7290. geometry.__dirtyUvs = true;
  7291. geometry.__dirtyNormals = true;
  7292. geometry.__dirtyTangents = true;
  7293. geometry.__dirtyColors = true;
  7294. }
  7295. }
  7296. }
  7297. } else if ( object instanceof THREE.Ribbon ) {
  7298. geometry = object.geometry;
  7299. if( ! geometry.__webglVertexBuffer ) {
  7300. createRibbonBuffers( geometry );
  7301. initRibbonBuffers( geometry );
  7302. geometry.__dirtyVertices = true;
  7303. geometry.__dirtyColors = true;
  7304. }
  7305. } else if ( object instanceof THREE.Line ) {
  7306. geometry = object.geometry;
  7307. if( ! geometry.__webglVertexBuffer ) {
  7308. createLineBuffers( geometry );
  7309. initLineBuffers( geometry, object );
  7310. geometry.__dirtyVertices = true;
  7311. geometry.__dirtyColors = true;
  7312. }
  7313. } else if ( object instanceof THREE.ParticleSystem ) {
  7314. geometry = object.geometry;
  7315. if ( ! geometry.__webglVertexBuffer ) {
  7316. createParticleBuffers( geometry );
  7317. initParticleBuffers( geometry, object );
  7318. geometry.__dirtyVertices = true;
  7319. geometry.__dirtyColors = true;
  7320. }
  7321. }
  7322. }
  7323. if ( ! object.__webglActive ) {
  7324. if ( object instanceof THREE.Mesh ) {
  7325. geometry = object.geometry;
  7326. if ( geometry instanceof THREE.BufferGeometry ) {
  7327. addBuffer( scene.__webglObjects, geometry, object );
  7328. } else {
  7329. for ( g in geometry.geometryGroups ) {
  7330. geometryGroup = geometry.geometryGroups[ g ];
  7331. addBuffer( scene.__webglObjects, geometryGroup, object );
  7332. }
  7333. }
  7334. } else if ( object instanceof THREE.Ribbon ||
  7335. object instanceof THREE.Line ||
  7336. object instanceof THREE.ParticleSystem ) {
  7337. geometry = object.geometry;
  7338. addBuffer( scene.__webglObjects, geometry, object );
  7339. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  7340. addBufferImmediate( scene.__webglObjectsImmediate, object );
  7341. } else if ( object instanceof THREE.Sprite ) {
  7342. scene.__webglSprites.push( object );
  7343. } else if ( object instanceof THREE.LensFlare ) {
  7344. scene.__webglFlares.push( object );
  7345. }
  7346. object.__webglActive = true;
  7347. }
  7348. };
  7349. function addBuffer ( objlist, buffer, object ) {
  7350. objlist.push(
  7351. {
  7352. buffer: buffer,
  7353. object: object,
  7354. opaque: null,
  7355. transparent: null
  7356. }
  7357. );
  7358. };
  7359. function addBufferImmediate ( objlist, object ) {
  7360. objlist.push(
  7361. {
  7362. object: object,
  7363. opaque: null,
  7364. transparent: null
  7365. }
  7366. );
  7367. };
  7368. // Objects updates
  7369. function updateObject ( object ) {
  7370. var geometry = object.geometry,
  7371. geometryGroup, customAttributesDirty, material;
  7372. if ( object instanceof THREE.Mesh ) {
  7373. if ( geometry instanceof THREE.BufferGeometry ) {
  7374. /*
  7375. if ( geometry.__dirtyVertices || geometry.__dirtyElements ||
  7376. geometry.__dirtyUvs || geometry.__dirtyNormals ||
  7377. geometry.__dirtyColors ) {
  7378. // TODO
  7379. // set buffers from typed arrays
  7380. }
  7381. */
  7382. geometry.__dirtyVertices = false;
  7383. geometry.__dirtyElements = false;
  7384. geometry.__dirtyUvs = false;
  7385. geometry.__dirtyNormals = false;
  7386. geometry.__dirtyColors = false;
  7387. } else {
  7388. // check all geometry groups
  7389. for( var i = 0, il = geometry.geometryGroupsList.length; i < il; i ++ ) {
  7390. geometryGroup = geometry.geometryGroupsList[ i ];
  7391. material = getBufferMaterial( object, geometryGroup );
  7392. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  7393. if ( geometry.__dirtyVertices || geometry.__dirtyMorphTargets || geometry.__dirtyElements ||
  7394. geometry.__dirtyUvs || geometry.__dirtyNormals ||
  7395. geometry.__dirtyColors || geometry.__dirtyTangents || customAttributesDirty ) {
  7396. setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW, !geometry.dynamic, material );
  7397. }
  7398. }
  7399. geometry.__dirtyVertices = false;
  7400. geometry.__dirtyMorphTargets = false;
  7401. geometry.__dirtyElements = false;
  7402. geometry.__dirtyUvs = false;
  7403. geometry.__dirtyNormals = false;
  7404. geometry.__dirtyColors = false;
  7405. geometry.__dirtyTangents = false;
  7406. material.attributes && clearCustomAttributes( material );
  7407. }
  7408. } else if ( object instanceof THREE.Ribbon ) {
  7409. if ( geometry.__dirtyVertices || geometry.__dirtyColors ) {
  7410. setRibbonBuffers( geometry, _gl.DYNAMIC_DRAW );
  7411. }
  7412. geometry.__dirtyVertices = false;
  7413. geometry.__dirtyColors = false;
  7414. } else if ( object instanceof THREE.Line ) {
  7415. material = getBufferMaterial( object, geometryGroup );
  7416. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  7417. if ( geometry.__dirtyVertices || geometry.__dirtyColors || customAttributesDirty ) {
  7418. setLineBuffers( geometry, _gl.DYNAMIC_DRAW );
  7419. }
  7420. geometry.__dirtyVertices = false;
  7421. geometry.__dirtyColors = false;
  7422. material.attributes && clearCustomAttributes( material );
  7423. } else if ( object instanceof THREE.ParticleSystem ) {
  7424. material = getBufferMaterial( object, geometryGroup );
  7425. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  7426. if ( geometry.__dirtyVertices || geometry.__dirtyColors || object.sortParticles || customAttributesDirty ) {
  7427. setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object );
  7428. }
  7429. geometry.__dirtyVertices = false;
  7430. geometry.__dirtyColors = false;
  7431. material.attributes && clearCustomAttributes( material );
  7432. }
  7433. };
  7434. // Objects updates - custom attributes check
  7435. function areCustomAttributesDirty ( material ) {
  7436. for ( var a in material.attributes ) {
  7437. if ( material.attributes[ a ].needsUpdate ) return true;
  7438. }
  7439. return false;
  7440. };
  7441. function clearCustomAttributes ( material ) {
  7442. for ( var a in material.attributes ) {
  7443. material.attributes[ a ].needsUpdate = false;
  7444. }
  7445. };
  7446. // Objects removal
  7447. function removeObject ( object, scene ) {
  7448. if ( object instanceof THREE.Mesh ||
  7449. object instanceof THREE.ParticleSystem ||
  7450. object instanceof THREE.Ribbon ||
  7451. object instanceof THREE.Line ) {
  7452. removeInstances( scene.__webglObjects, object );
  7453. } else if ( object instanceof THREE.Sprite ) {
  7454. removeInstancesDirect( scene.__webglSprites, object );
  7455. } else if ( object instanceof THREE.LensFlare ) {
  7456. removeInstancesDirect( scene.__webglFlares, object );
  7457. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  7458. removeInstances( scene.__webglObjectsImmediate, object );
  7459. }
  7460. object.__webglActive = false;
  7461. };
  7462. function removeInstances ( objlist, object ) {
  7463. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  7464. if ( objlist[ o ].object === object ) {
  7465. objlist.splice( o, 1 );
  7466. }
  7467. }
  7468. };
  7469. function removeInstancesDirect ( objlist, object ) {
  7470. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  7471. if ( objlist[ o ] === object ) {
  7472. objlist.splice( o, 1 );
  7473. }
  7474. }
  7475. };
  7476. // Materials
  7477. this.initMaterial = function ( material, lights, fog, object ) {
  7478. var u, a, identifiers, i, parameters, maxLightCount, maxBones, maxShadows, shaderID;
  7479. if ( material instanceof THREE.MeshDepthMaterial ) {
  7480. shaderID = 'depth';
  7481. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  7482. shaderID = 'normal';
  7483. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  7484. shaderID = 'basic';
  7485. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  7486. shaderID = 'lambert';
  7487. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  7488. shaderID = 'phong';
  7489. } else if ( material instanceof THREE.LineBasicMaterial ) {
  7490. shaderID = 'basic';
  7491. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  7492. shaderID = 'particle_basic';
  7493. }
  7494. if ( shaderID ) {
  7495. setMaterialShaders( material, THREE.ShaderLib[ shaderID ] );
  7496. }
  7497. // heuristics to create shader parameters according to lights in the scene
  7498. // (not to blow over maxLights budget)
  7499. maxLightCount = allocateLights( lights );
  7500. maxShadows = allocateShadows( lights );
  7501. maxBones = allocateBones( object );
  7502. parameters = {
  7503. map: !!material.map, envMap: !!material.envMap, lightMap: !!material.lightMap,
  7504. vertexColors: material.vertexColors,
  7505. fog: fog, useFog: material.fog,
  7506. sizeAttenuation: material.sizeAttenuation,
  7507. skinning: material.skinning,
  7508. morphTargets: material.morphTargets,
  7509. morphNormals: material.morphNormals,
  7510. maxMorphTargets: this.maxMorphTargets,
  7511. maxMorphNormals: this.maxMorphNormals,
  7512. maxDirLights: maxLightCount.directional, maxPointLights: maxLightCount.point,
  7513. maxBones: maxBones,
  7514. shadowMapEnabled: this.shadowMapEnabled && object.receiveShadow,
  7515. shadowMapSoft: this.shadowMapSoft,
  7516. shadowMapDebug: this.shadowMapDebug,
  7517. shadowMapCascade: this.shadowMapCascade,
  7518. maxShadows: maxShadows,
  7519. alphaTest: material.alphaTest,
  7520. metal: material.metal,
  7521. perPixel: material.perPixel,
  7522. wrapAround: material.wrapAround,
  7523. doubleSided: object && object.doubleSided
  7524. };
  7525. material.program = buildProgram( shaderID, material.fragmentShader, material.vertexShader, material.uniforms, material.attributes, parameters );
  7526. var attributes = material.program.attributes;
  7527. if ( attributes.position >= 0 ) _gl.enableVertexAttribArray( attributes.position );
  7528. if ( attributes.color >= 0 ) _gl.enableVertexAttribArray( attributes.color );
  7529. if ( attributes.normal >= 0 ) _gl.enableVertexAttribArray( attributes.normal );
  7530. if ( attributes.tangent >= 0 ) _gl.enableVertexAttribArray( attributes.tangent );
  7531. if ( material.skinning &&
  7532. attributes.skinVertexA >=0 && attributes.skinVertexB >= 0 &&
  7533. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  7534. _gl.enableVertexAttribArray( attributes.skinVertexA );
  7535. _gl.enableVertexAttribArray( attributes.skinVertexB );
  7536. _gl.enableVertexAttribArray( attributes.skinIndex );
  7537. _gl.enableVertexAttribArray( attributes.skinWeight );
  7538. }
  7539. if ( material.attributes ) {
  7540. for ( a in material.attributes ) {
  7541. if( attributes[ a ] !== undefined && attributes[ a ] >= 0 ) _gl.enableVertexAttribArray( attributes[ a ] );
  7542. }
  7543. }
  7544. if ( material.morphTargets ) {
  7545. material.numSupportedMorphTargets = 0;
  7546. var id, base = "morphTarget";
  7547. for ( i = 0; i < this.maxMorphTargets; i ++ ) {
  7548. id = base + i;
  7549. if ( attributes[ id ] >= 0 ) {
  7550. _gl.enableVertexAttribArray( attributes[ id ] );
  7551. material.numSupportedMorphTargets ++;
  7552. }
  7553. }
  7554. }
  7555. if ( material.morphNormals ) {
  7556. material.numSupportedMorphNormals = 0;
  7557. var id, base = "morphNormal";
  7558. for ( i = 0; i < this.maxMorphNormals; i ++ ) {
  7559. id = base + i;
  7560. if ( attributes[ id ] >= 0 ) {
  7561. _gl.enableVertexAttribArray( attributes[ id ] );
  7562. material.numSupportedMorphNormals ++;
  7563. }
  7564. }
  7565. }
  7566. material.uniformsList = [];
  7567. for ( u in material.uniforms ) {
  7568. material.uniformsList.push( [ material.uniforms[ u ], u ] );
  7569. }
  7570. };
  7571. function setMaterialShaders( material, shaders ) {
  7572. material.uniforms = THREE.UniformsUtils.clone( shaders.uniforms );
  7573. material.vertexShader = shaders.vertexShader;
  7574. material.fragmentShader = shaders.fragmentShader;
  7575. };
  7576. function setProgram( camera, lights, fog, material, object ) {
  7577. if ( ! material.program || material.needsUpdate ) {
  7578. _this.initMaterial( material, lights, fog, object );
  7579. material.needsUpdate = false;
  7580. }
  7581. if ( material.morphTargets ) {
  7582. if ( ! object.__webglMorphTargetInfluences ) {
  7583. object.__webglMorphTargetInfluences = new Float32Array( _this.maxMorphTargets );
  7584. for ( var i = 0, il = _this.maxMorphTargets; i < il; i ++ ) {
  7585. object.__webglMorphTargetInfluences[ i ] = 0;
  7586. }
  7587. }
  7588. }
  7589. var refreshMaterial = false;
  7590. var program = material.program,
  7591. p_uniforms = program.uniforms,
  7592. m_uniforms = material.uniforms;
  7593. if ( program !== _currentProgram ) {
  7594. _gl.useProgram( program );
  7595. _currentProgram = program;
  7596. refreshMaterial = true;
  7597. }
  7598. if ( material.id !== _currentMaterialId ) {
  7599. _currentMaterialId = material.id;
  7600. refreshMaterial = true;
  7601. }
  7602. if ( refreshMaterial || camera !== _currentCamera ) {
  7603. _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera._projectionMatrixArray );
  7604. if ( camera !== _currentCamera ) _currentCamera = camera;
  7605. }
  7606. if ( refreshMaterial ) {
  7607. // refresh uniforms common to several materials
  7608. if ( fog && material.fog ) {
  7609. refreshUniformsFog( m_uniforms, fog );
  7610. }
  7611. if ( material instanceof THREE.MeshPhongMaterial ||
  7612. material instanceof THREE.MeshLambertMaterial ||
  7613. material.lights ) {
  7614. setupLights( program, lights );
  7615. refreshUniformsLights( m_uniforms, _lights );
  7616. }
  7617. if ( material instanceof THREE.MeshBasicMaterial ||
  7618. material instanceof THREE.MeshLambertMaterial ||
  7619. material instanceof THREE.MeshPhongMaterial ) {
  7620. refreshUniformsCommon( m_uniforms, material );
  7621. }
  7622. // refresh single material specific uniforms
  7623. if ( material instanceof THREE.LineBasicMaterial ) {
  7624. refreshUniformsLine( m_uniforms, material );
  7625. } else if ( material instanceof THREE.ParticleBasicMaterial ) {
  7626. refreshUniformsParticle( m_uniforms, material );
  7627. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  7628. refreshUniformsPhong( m_uniforms, material );
  7629. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  7630. refreshUniformsLambert( m_uniforms, material );
  7631. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  7632. m_uniforms.mNear.value = camera.near;
  7633. m_uniforms.mFar.value = camera.far;
  7634. m_uniforms.opacity.value = material.opacity;
  7635. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  7636. m_uniforms.opacity.value = material.opacity;
  7637. }
  7638. if ( object.receiveShadow && ! material._shadowPass ) {
  7639. refreshUniformsShadow( m_uniforms, lights );
  7640. }
  7641. // load common uniforms
  7642. loadUniformsGeneric( program, material.uniformsList );
  7643. // load material specific uniforms
  7644. // (shader material also gets them for the sake of genericity)
  7645. if ( material instanceof THREE.ShaderMaterial ||
  7646. material instanceof THREE.MeshPhongMaterial ||
  7647. material.envMap ) {
  7648. if ( p_uniforms.cameraPosition !== null ) {
  7649. var position = camera.matrixWorld.getPosition();
  7650. _gl.uniform3f( p_uniforms.cameraPosition, position.x, position.y, position.z );
  7651. }
  7652. }
  7653. if ( material instanceof THREE.MeshPhongMaterial ||
  7654. material instanceof THREE.MeshLambertMaterial ||
  7655. material instanceof THREE.ShaderMaterial ||
  7656. material.skinning ) {
  7657. if ( p_uniforms.viewMatrix !== null ) {
  7658. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera._viewMatrixArray );
  7659. }
  7660. }
  7661. if ( material.skinning ) {
  7662. _gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.boneMatrices );
  7663. }
  7664. }
  7665. loadUniformsMatrices( p_uniforms, object );
  7666. if ( material instanceof THREE.ShaderMaterial ||
  7667. material.envMap ||
  7668. material.skinning ||
  7669. object.receiveShadow ) {
  7670. if ( p_uniforms.objectMatrix !== null ) {
  7671. _gl.uniformMatrix4fv( p_uniforms.objectMatrix, false, object.matrixWorld.elements );
  7672. }
  7673. }
  7674. return program;
  7675. };
  7676. // Uniforms (refresh uniforms objects)
  7677. function refreshUniformsCommon ( uniforms, material ) {
  7678. uniforms.opacity.value = material.opacity;
  7679. if ( _this.gammaInput ) {
  7680. uniforms.diffuse.value.copyGammaToLinear( material.color );
  7681. } else {
  7682. uniforms.diffuse.value = material.color;
  7683. }
  7684. uniforms.map.texture = material.map;
  7685. if ( material.map ) {
  7686. uniforms.offsetRepeat.value.set( material.map.offset.x, material.map.offset.y, material.map.repeat.x, material.map.repeat.y );
  7687. }
  7688. uniforms.lightMap.texture = material.lightMap;
  7689. uniforms.envMap.texture = material.envMap;
  7690. uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : -1;
  7691. if ( _this.gammaInput ) {
  7692. //uniforms.reflectivity.value = material.reflectivity * material.reflectivity;
  7693. uniforms.reflectivity.value = material.reflectivity;
  7694. } else {
  7695. uniforms.reflectivity.value = material.reflectivity;
  7696. }
  7697. uniforms.refractionRatio.value = material.refractionRatio;
  7698. uniforms.combine.value = material.combine;
  7699. uniforms.useRefract.value = material.envMap && material.envMap.mapping instanceof THREE.CubeRefractionMapping;
  7700. };
  7701. function refreshUniformsLine ( uniforms, material ) {
  7702. uniforms.diffuse.value = material.color;
  7703. uniforms.opacity.value = material.opacity;
  7704. };
  7705. function refreshUniformsParticle ( uniforms, material ) {
  7706. uniforms.psColor.value = material.color;
  7707. uniforms.opacity.value = material.opacity;
  7708. uniforms.size.value = material.size;
  7709. uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.
  7710. uniforms.map.texture = material.map;
  7711. };
  7712. function refreshUniformsFog ( uniforms, fog ) {
  7713. uniforms.fogColor.value = fog.color;
  7714. if ( fog instanceof THREE.Fog ) {
  7715. uniforms.fogNear.value = fog.near;
  7716. uniforms.fogFar.value = fog.far;
  7717. } else if ( fog instanceof THREE.FogExp2 ) {
  7718. uniforms.fogDensity.value = fog.density;
  7719. }
  7720. };
  7721. function refreshUniformsPhong ( uniforms, material ) {
  7722. uniforms.shininess.value = material.shininess;
  7723. if ( _this.gammaInput ) {
  7724. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  7725. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  7726. uniforms.specular.value.copyGammaToLinear( material.specular );
  7727. } else {
  7728. uniforms.ambient.value = material.ambient;
  7729. uniforms.emissive.value = material.emissive;
  7730. uniforms.specular.value = material.specular;
  7731. }
  7732. if ( material.wrapAround ) {
  7733. uniforms.wrapRGB.value.copy( material.wrapRGB );
  7734. }
  7735. };
  7736. function refreshUniformsLambert ( uniforms, material ) {
  7737. if ( _this.gammaInput ) {
  7738. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  7739. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  7740. } else {
  7741. uniforms.ambient.value = material.ambient;
  7742. uniforms.emissive.value = material.emissive;
  7743. }
  7744. if ( material.wrapAround ) {
  7745. uniforms.wrapRGB.value.copy( material.wrapRGB );
  7746. }
  7747. };
  7748. function refreshUniformsLights ( uniforms, lights ) {
  7749. uniforms.ambientLightColor.value = lights.ambient;
  7750. uniforms.directionalLightColor.value = lights.directional.colors;
  7751. uniforms.directionalLightDirection.value = lights.directional.positions;
  7752. uniforms.pointLightColor.value = lights.point.colors;
  7753. uniforms.pointLightPosition.value = lights.point.positions;
  7754. uniforms.pointLightDistance.value = lights.point.distances;
  7755. };
  7756. function refreshUniformsShadow ( uniforms, lights ) {
  7757. if ( uniforms.shadowMatrix ) {
  7758. var j = 0;
  7759. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  7760. var light = lights[ i ];
  7761. if ( ! light.castShadow ) continue;
  7762. if ( light instanceof THREE.SpotLight || ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) ) {
  7763. uniforms.shadowMap.texture[ j ] = light.shadowMap;
  7764. uniforms.shadowMapSize.value[ j ] = light.shadowMapSize;
  7765. uniforms.shadowMatrix.value[ j ] = light.shadowMatrix;
  7766. uniforms.shadowDarkness.value[ j ] = light.shadowDarkness;
  7767. uniforms.shadowBias.value[ j ] = light.shadowBias;
  7768. j ++;
  7769. }
  7770. }
  7771. }
  7772. };
  7773. // Uniforms (load to GPU)
  7774. function loadUniformsMatrices ( uniforms, object ) {
  7775. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrix.elements );
  7776. if ( uniforms.normalMatrix ) {
  7777. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrix.elements );
  7778. }
  7779. };
  7780. function loadUniformsGeneric ( program, uniforms ) {
  7781. var uniform, value, type, location, texture, i, il, j, jl, offset;
  7782. for( j = 0, jl = uniforms.length; j < jl; j ++ ) {
  7783. location = program.uniforms[ uniforms[ j ][ 1 ] ];
  7784. if ( !location ) continue;
  7785. uniform = uniforms[ j ][ 0 ];
  7786. type = uniform.type;
  7787. value = uniform.value;
  7788. // single integer
  7789. if( type === "i" ) {
  7790. _gl.uniform1i( location, value );
  7791. // single float
  7792. } else if( type === "f" ) {
  7793. _gl.uniform1f( location, value );
  7794. // single THREE.Vector2
  7795. } else if( type === "v2" ) {
  7796. _gl.uniform2f( location, value.x, value.y );
  7797. // single THREE.Vector3
  7798. } else if( type === "v3" ) {
  7799. _gl.uniform3f( location, value.x, value.y, value.z );
  7800. // single THREE.Vector4
  7801. } else if( type === "v4" ) {
  7802. _gl.uniform4f( location, value.x, value.y, value.z, value.w );
  7803. // single THREE.Color
  7804. } else if( type === "c" ) {
  7805. _gl.uniform3f( location, value.r, value.g, value.b );
  7806. // flat array of floats (JS or typed array)
  7807. } else if( type === "fv1" ) {
  7808. _gl.uniform1fv( location, value );
  7809. // flat array of floats with 3 x N size (JS or typed array)
  7810. } else if( type === "fv" ) {
  7811. _gl.uniform3fv( location, value );
  7812. // array of THREE.Vector2
  7813. } else if( type === "v2v" ) {
  7814. if ( ! uniform._array ) {
  7815. uniform._array = new Float32Array( 2 * value.length );
  7816. }
  7817. for ( i = 0, il = value.length; i < il; i ++ ) {
  7818. offset = i * 2;
  7819. uniform._array[ offset ] = value[ i ].x;
  7820. uniform._array[ offset + 1 ] = value[ i ].y;
  7821. }
  7822. _gl.uniform2fv( location, uniform._array );
  7823. // array of THREE.Vector3
  7824. } else if( type === "v3v" ) {
  7825. if ( ! uniform._array ) {
  7826. uniform._array = new Float32Array( 3 * value.length );
  7827. }
  7828. for ( i = 0, il = value.length; i < il; i ++ ) {
  7829. offset = i * 3;
  7830. uniform._array[ offset ] = value[ i ].x;
  7831. uniform._array[ offset + 1 ] = value[ i ].y;
  7832. uniform._array[ offset + 2 ] = value[ i ].z;
  7833. }
  7834. _gl.uniform3fv( location, uniform._array );
  7835. // array of THREE.Vector4
  7836. } else if( type == "v4v" ) {
  7837. if ( ! uniform._array ) {
  7838. uniform._array = new Float32Array( 4 * value.length );
  7839. }
  7840. for ( i = 0, il = value.length; i < il; i ++ ) {
  7841. offset = i * 4;
  7842. uniform._array[ offset ] = value[ i ].x;
  7843. uniform._array[ offset + 1 ] = value[ i ].y;
  7844. uniform._array[ offset + 2 ] = value[ i ].z;
  7845. uniform._array[ offset + 3 ] = value[ i ].w;
  7846. }
  7847. _gl.uniform4fv( location, uniform._array );
  7848. // single THREE.Matrix4
  7849. } else if( type === "m4" ) {
  7850. if ( ! uniform._array ) {
  7851. uniform._array = new Float32Array( 16 );
  7852. }
  7853. value.flattenToArray( uniform._array );
  7854. _gl.uniformMatrix4fv( location, false, uniform._array );
  7855. // array of THREE.Matrix4
  7856. } else if( type === "m4v" ) {
  7857. if ( ! uniform._array ) {
  7858. uniform._array = new Float32Array( 16 * value.length );
  7859. }
  7860. for ( i = 0, il = value.length; i < il; i ++ ) {
  7861. value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
  7862. }
  7863. _gl.uniformMatrix4fv( location, false, uniform._array );
  7864. // single THREE.Texture (2d or cube)
  7865. } else if( type === "t" ) {
  7866. _gl.uniform1i( location, value );
  7867. texture = uniform.texture;
  7868. if ( !texture ) continue;
  7869. if ( texture.image instanceof Array && texture.image.length === 6 ) {
  7870. setCubeTexture( texture, value );
  7871. } else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
  7872. setCubeTextureDynamic( texture, value );
  7873. } else {
  7874. _this.setTexture( texture, value );
  7875. }
  7876. // array of THREE.Texture (2d)
  7877. } else if( type === "tv" ) {
  7878. if ( ! uniform._array ) {
  7879. uniform._array = [];
  7880. for( i = 0, il = uniform.texture.length; i < il; i ++ ) {
  7881. uniform._array[ i ] = value + i;
  7882. }
  7883. }
  7884. _gl.uniform1iv( location, uniform._array );
  7885. for( i = 0, il = uniform.texture.length; i < il; i ++ ) {
  7886. texture = uniform.texture[ i ];
  7887. if ( !texture ) continue;
  7888. _this.setTexture( texture, uniform._array[ i ] );
  7889. }
  7890. }
  7891. }
  7892. };
  7893. function setupMatrices ( object, camera ) {
  7894. object._modelViewMatrix.multiply( camera.matrixWorldInverse, object.matrixWorld);
  7895. object._normalMatrix.getInverse( object._modelViewMatrix );
  7896. object._normalMatrix.transpose();
  7897. };
  7898. function setupLights ( program, lights ) {
  7899. var l, ll, light, n,
  7900. r = 0, g = 0, b = 0,
  7901. color, position, intensity, distance,
  7902. zlights = _lights,
  7903. dcolors = zlights.directional.colors,
  7904. dpositions = zlights.directional.positions,
  7905. pcolors = zlights.point.colors,
  7906. ppositions = zlights.point.positions,
  7907. pdistances = zlights.point.distances,
  7908. dlength = 0,
  7909. plength = 0,
  7910. doffset = 0,
  7911. poffset = 0;
  7912. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  7913. light = lights[ l ];
  7914. if ( light.onlyShadow ) continue;
  7915. color = light.color;
  7916. intensity = light.intensity;
  7917. distance = light.distance;
  7918. if ( light instanceof THREE.AmbientLight ) {
  7919. if ( _this.gammaInput ) {
  7920. r += color.r * color.r;
  7921. g += color.g * color.g;
  7922. b += color.b * color.b;
  7923. } else {
  7924. r += color.r;
  7925. g += color.g;
  7926. b += color.b;
  7927. }
  7928. } else if ( light instanceof THREE.DirectionalLight ) {
  7929. doffset = dlength * 3;
  7930. if ( _this.gammaInput ) {
  7931. dcolors[ doffset ] = color.r * color.r * intensity * intensity;
  7932. dcolors[ doffset + 1 ] = color.g * color.g * intensity * intensity;
  7933. dcolors[ doffset + 2 ] = color.b * color.b * intensity * intensity;
  7934. } else {
  7935. dcolors[ doffset ] = color.r * intensity;
  7936. dcolors[ doffset + 1 ] = color.g * intensity;
  7937. dcolors[ doffset + 2 ] = color.b * intensity;
  7938. }
  7939. _direction.copy( light.matrixWorld.getPosition() );
  7940. _direction.subSelf( light.target.matrixWorld.getPosition() );
  7941. _direction.normalize();
  7942. dpositions[ doffset ] = _direction.x;
  7943. dpositions[ doffset + 1 ] = _direction.y;
  7944. dpositions[ doffset + 2 ] = _direction.z;
  7945. dlength += 1;
  7946. } else if( light instanceof THREE.PointLight || light instanceof THREE.SpotLight ) {
  7947. poffset = plength * 3;
  7948. if ( _this.gammaInput ) {
  7949. pcolors[ poffset ] = color.r * color.r * intensity * intensity;
  7950. pcolors[ poffset + 1 ] = color.g * color.g * intensity * intensity;
  7951. pcolors[ poffset + 2 ] = color.b * color.b * intensity * intensity;
  7952. } else {
  7953. pcolors[ poffset ] = color.r * intensity;
  7954. pcolors[ poffset + 1 ] = color.g * intensity;
  7955. pcolors[ poffset + 2 ] = color.b * intensity;
  7956. }
  7957. position = light.matrixWorld.getPosition();
  7958. ppositions[ poffset ] = position.x;
  7959. ppositions[ poffset + 1 ] = position.y;
  7960. ppositions[ poffset + 2 ] = position.z;
  7961. pdistances[ plength ] = distance;
  7962. plength += 1;
  7963. }
  7964. }
  7965. // null eventual remains from removed lights
  7966. // (this is to avoid if in shader)
  7967. for ( l = dlength * 3, ll = dcolors.length; l < ll; l ++ ) dcolors[ l ] = 0.0;
  7968. for ( l = plength * 3, ll = pcolors.length; l < ll; l ++ ) pcolors[ l ] = 0.0;
  7969. zlights.point.length = plength;
  7970. zlights.directional.length = dlength;
  7971. zlights.ambient[ 0 ] = r;
  7972. zlights.ambient[ 1 ] = g;
  7973. zlights.ambient[ 2 ] = b;
  7974. };
  7975. // GL state setting
  7976. this.setFaceCulling = function ( cullFace, frontFace ) {
  7977. if ( cullFace ) {
  7978. if ( !frontFace || frontFace === "ccw" ) {
  7979. _gl.frontFace( _gl.CCW );
  7980. } else {
  7981. _gl.frontFace( _gl.CW );
  7982. }
  7983. if( cullFace === "back" ) {
  7984. _gl.cullFace( _gl.BACK );
  7985. } else if( cullFace === "front" ) {
  7986. _gl.cullFace( _gl.FRONT );
  7987. } else {
  7988. _gl.cullFace( _gl.FRONT_AND_BACK );
  7989. }
  7990. _gl.enable( _gl.CULL_FACE );
  7991. } else {
  7992. _gl.disable( _gl.CULL_FACE );
  7993. }
  7994. };
  7995. this.setObjectFaces = function ( object ) {
  7996. if ( _oldDoubleSided !== object.doubleSided ) {
  7997. if( object.doubleSided ) {
  7998. _gl.disable( _gl.CULL_FACE );
  7999. } else {
  8000. _gl.enable( _gl.CULL_FACE );
  8001. }
  8002. _oldDoubleSided = object.doubleSided;
  8003. }
  8004. if ( _oldFlipSided !== object.flipSided ) {
  8005. if( object.flipSided ) {
  8006. _gl.frontFace( _gl.CW );
  8007. } else {
  8008. _gl.frontFace( _gl.CCW );
  8009. }
  8010. _oldFlipSided = object.flipSided;
  8011. }
  8012. };
  8013. this.setDepthTest = function ( depthTest ) {
  8014. if ( _oldDepthTest !== depthTest ) {
  8015. if ( depthTest ) {
  8016. _gl.enable( _gl.DEPTH_TEST );
  8017. } else {
  8018. _gl.disable( _gl.DEPTH_TEST );
  8019. }
  8020. _oldDepthTest = depthTest;
  8021. }
  8022. };
  8023. this.setDepthWrite = function ( depthWrite ) {
  8024. if ( _oldDepthWrite !== depthWrite ) {
  8025. _gl.depthMask( depthWrite );
  8026. _oldDepthWrite = depthWrite;
  8027. }
  8028. };
  8029. function setLineWidth ( width ) {
  8030. if ( width !== _oldLineWidth ) {
  8031. _gl.lineWidth( width );
  8032. _oldLineWidth = width;
  8033. }
  8034. };
  8035. function setPolygonOffset ( polygonoffset, factor, units ) {
  8036. if ( _oldPolygonOffset !== polygonoffset ) {
  8037. if ( polygonoffset ) {
  8038. _gl.enable( _gl.POLYGON_OFFSET_FILL );
  8039. } else {
  8040. _gl.disable( _gl.POLYGON_OFFSET_FILL );
  8041. }
  8042. _oldPolygonOffset = polygonoffset;
  8043. }
  8044. if ( polygonoffset && ( _oldPolygonOffsetFactor !== factor || _oldPolygonOffsetUnits !== units ) ) {
  8045. _gl.polygonOffset( factor, units );
  8046. _oldPolygonOffsetFactor = factor;
  8047. _oldPolygonOffsetUnits = units;
  8048. }
  8049. };
  8050. this.setBlending = function ( blending, blendEquation, blendSrc, blendDst ) {
  8051. if ( blending !== _oldBlending ) {
  8052. switch ( blending ) {
  8053. case THREE.NoBlending:
  8054. _gl.disable( _gl.BLEND );
  8055. break;
  8056. case THREE.AdditiveBlending:
  8057. _gl.enable( _gl.BLEND );
  8058. _gl.blendEquation( _gl.FUNC_ADD );
  8059. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE );
  8060. break;
  8061. case THREE.SubtractiveBlending:
  8062. // TODO: Find blendFuncSeparate() combination
  8063. _gl.enable( _gl.BLEND );
  8064. _gl.blendEquation( _gl.FUNC_ADD );
  8065. _gl.blendFunc( _gl.ZERO, _gl.ONE_MINUS_SRC_COLOR );
  8066. break;
  8067. case THREE.MultiplyBlending:
  8068. // TODO: Find blendFuncSeparate() combination
  8069. _gl.enable( _gl.BLEND );
  8070. _gl.blendEquation( _gl.FUNC_ADD );
  8071. _gl.blendFunc( _gl.ZERO, _gl.SRC_COLOR );
  8072. break;
  8073. case THREE.CustomBlending:
  8074. _gl.enable( _gl.BLEND );
  8075. break;
  8076. default:
  8077. _gl.enable( _gl.BLEND );
  8078. _gl.blendEquationSeparate( _gl.FUNC_ADD, _gl.FUNC_ADD );
  8079. _gl.blendFuncSeparate( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA, _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  8080. break;
  8081. }
  8082. _oldBlending = blending;
  8083. }
  8084. if ( blending === THREE.CustomBlending ) {
  8085. if ( blendEquation !== _oldBlendEquation ) {
  8086. _gl.blendEquation( paramThreeToGL( blendEquation ) );
  8087. _oldBlendEquation = blendEquation;
  8088. }
  8089. if ( blendSrc !== _oldBlendSrc || blendDst !== _oldBlendDst ) {
  8090. _gl.blendFunc( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ) );
  8091. _oldBlendSrc = blendSrc;
  8092. _oldBlendDst = blendDst;
  8093. }
  8094. } else {
  8095. _oldBlendEquation = null;
  8096. _oldBlendSrc = null;
  8097. _oldBlendDst = null;
  8098. }
  8099. };
  8100. // Shaders
  8101. function buildProgram ( shaderID, fragmentShader, vertexShader, uniforms, attributes, parameters ) {
  8102. var p, pl, program, code;
  8103. var chunks = [];
  8104. // Generate code
  8105. if ( shaderID ) {
  8106. chunks.push( shaderID );
  8107. } else {
  8108. chunks.push( fragmentShader );
  8109. chunks.push( vertexShader );
  8110. }
  8111. for ( p in parameters ) {
  8112. chunks.push( p );
  8113. chunks.push( parameters[ p ] );
  8114. }
  8115. code = chunks.join();
  8116. // Check if code has been already compiled
  8117. for ( p = 0, pl = _programs.length; p < pl; p ++ ) {
  8118. if ( _programs[ p ].code === code ) {
  8119. // console.log( "Code already compiled." /*: \n\n" + code*/ );
  8120. return _programs[ p ].program;
  8121. }
  8122. }
  8123. //console.log( "building new program " );
  8124. //
  8125. program = _gl.createProgram();
  8126. var prefix_vertex = [
  8127. "precision " + _precision + " float;",
  8128. ( _maxVertexTextures > 0 ) ? "#define VERTEX_TEXTURES" : "",
  8129. _this.gammaInput ? "#define GAMMA_INPUT" : "",
  8130. _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
  8131. _this.physicallyBasedShading ? "#define PHYSICALLY_BASED_SHADING" : "",
  8132. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  8133. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  8134. "#define MAX_SHADOWS " + parameters.maxShadows,
  8135. "#define MAX_BONES " + parameters.maxBones,
  8136. parameters.map ? "#define USE_MAP" : "",
  8137. parameters.envMap ? "#define USE_ENVMAP" : "",
  8138. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  8139. parameters.vertexColors ? "#define USE_COLOR" : "",
  8140. parameters.skinning ? "#define USE_SKINNING" : "",
  8141. parameters.morphTargets ? "#define USE_MORPHTARGETS" : "",
  8142. parameters.morphNormals ? "#define USE_MORPHNORMALS" : "",
  8143. parameters.perPixel ? "#define PHONG_PER_PIXEL" : "",
  8144. parameters.wrapAround ? "#define WRAP_AROUND" : "",
  8145. parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
  8146. parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
  8147. parameters.shadowMapSoft ? "#define SHADOWMAP_SOFT" : "",
  8148. parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
  8149. parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
  8150. parameters.sizeAttenuation ? "#define USE_SIZEATTENUATION" : "",
  8151. "uniform mat4 objectMatrix;",
  8152. "uniform mat4 modelViewMatrix;",
  8153. "uniform mat4 projectionMatrix;",
  8154. "uniform mat4 viewMatrix;",
  8155. "uniform mat3 normalMatrix;",
  8156. "uniform vec3 cameraPosition;",
  8157. "attribute vec3 position;",
  8158. "attribute vec3 normal;",
  8159. "attribute vec2 uv;",
  8160. "attribute vec2 uv2;",
  8161. "#ifdef USE_COLOR",
  8162. "attribute vec3 color;",
  8163. "#endif",
  8164. "#ifdef USE_MORPHTARGETS",
  8165. "attribute vec3 morphTarget0;",
  8166. "attribute vec3 morphTarget1;",
  8167. "attribute vec3 morphTarget2;",
  8168. "attribute vec3 morphTarget3;",
  8169. "#ifdef USE_MORPHNORMALS",
  8170. "attribute vec3 morphNormal0;",
  8171. "attribute vec3 morphNormal1;",
  8172. "attribute vec3 morphNormal2;",
  8173. "attribute vec3 morphNormal3;",
  8174. "#else",
  8175. "attribute vec3 morphTarget4;",
  8176. "attribute vec3 morphTarget5;",
  8177. "attribute vec3 morphTarget6;",
  8178. "attribute vec3 morphTarget7;",
  8179. "#endif",
  8180. "#endif",
  8181. "#ifdef USE_SKINNING",
  8182. "attribute vec4 skinVertexA;",
  8183. "attribute vec4 skinVertexB;",
  8184. "attribute vec4 skinIndex;",
  8185. "attribute vec4 skinWeight;",
  8186. "#endif",
  8187. ""
  8188. ].join("\n");
  8189. var prefix_fragment = [
  8190. "precision " + _precision + " float;",
  8191. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  8192. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  8193. "#define MAX_SHADOWS " + parameters.maxShadows,
  8194. parameters.alphaTest ? "#define ALPHATEST " + parameters.alphaTest: "",
  8195. _this.gammaInput ? "#define GAMMA_INPUT" : "",
  8196. _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
  8197. _this.physicallyBasedShading ? "#define PHYSICALLY_BASED_SHADING" : "",
  8198. ( parameters.useFog && parameters.fog ) ? "#define USE_FOG" : "",
  8199. ( parameters.useFog && parameters.fog instanceof THREE.FogExp2 ) ? "#define FOG_EXP2" : "",
  8200. parameters.map ? "#define USE_MAP" : "",
  8201. parameters.envMap ? "#define USE_ENVMAP" : "",
  8202. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  8203. parameters.vertexColors ? "#define USE_COLOR" : "",
  8204. parameters.metal ? "#define METAL" : "",
  8205. parameters.perPixel ? "#define PHONG_PER_PIXEL" : "",
  8206. parameters.wrapAround ? "#define WRAP_AROUND" : "",
  8207. parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
  8208. parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
  8209. parameters.shadowMapSoft ? "#define SHADOWMAP_SOFT" : "",
  8210. parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
  8211. parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
  8212. "uniform mat4 viewMatrix;",
  8213. "uniform vec3 cameraPosition;",
  8214. ""
  8215. ].join("\n");
  8216. _gl.attachShader( program, getShader( "fragment", prefix_fragment + fragmentShader ) );
  8217. _gl.attachShader( program, getShader( "vertex", prefix_vertex + vertexShader ) );
  8218. _gl.linkProgram( program );
  8219. if ( !_gl.getProgramParameter( program, _gl.LINK_STATUS ) ) {
  8220. console.error( "Could not initialise shader\n" + "VALIDATE_STATUS: " + _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) + ", gl error [" + _gl.getError() + "]" );
  8221. }
  8222. //console.log( prefix_fragment + fragmentShader );
  8223. //console.log( prefix_vertex + vertexShader );
  8224. program.uniforms = {};
  8225. program.attributes = {};
  8226. var identifiers, u, a, i;
  8227. // cache uniform locations
  8228. identifiers = [
  8229. 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'objectMatrix', 'cameraPosition',
  8230. 'boneGlobalMatrices', 'morphTargetInfluences'
  8231. ];
  8232. for ( u in uniforms ) {
  8233. identifiers.push( u );
  8234. }
  8235. cacheUniformLocations( program, identifiers );
  8236. // cache attributes locations
  8237. identifiers = [
  8238. "position", "normal", "uv", "uv2", "tangent", "color",
  8239. "skinVertexA", "skinVertexB", "skinIndex", "skinWeight"
  8240. ];
  8241. for ( i = 0; i < parameters.maxMorphTargets; i ++ ) {
  8242. identifiers.push( "morphTarget" + i );
  8243. }
  8244. for ( i = 0; i < parameters.maxMorphNormals; i ++ ) {
  8245. identifiers.push( "morphNormal" + i );
  8246. }
  8247. for ( a in attributes ) {
  8248. identifiers.push( a );
  8249. }
  8250. cacheAttributeLocations( program, identifiers );
  8251. program.id = _programs.length;
  8252. _programs.push( { program: program, code: code } );
  8253. _this.info.memory.programs = _programs.length;
  8254. return program;
  8255. };
  8256. // Shader parameters cache
  8257. function cacheUniformLocations ( program, identifiers ) {
  8258. var i, l, id;
  8259. for( i = 0, l = identifiers.length; i < l; i ++ ) {
  8260. id = identifiers[ i ];
  8261. program.uniforms[ id ] = _gl.getUniformLocation( program, id );
  8262. }
  8263. };
  8264. function cacheAttributeLocations ( program, identifiers ) {
  8265. var i, l, id;
  8266. for( i = 0, l = identifiers.length; i < l; i ++ ) {
  8267. id = identifiers[ i ];
  8268. program.attributes[ id ] = _gl.getAttribLocation( program, id );
  8269. }
  8270. };
  8271. function getShader ( type, string ) {
  8272. var shader;
  8273. if ( type === "fragment" ) {
  8274. shader = _gl.createShader( _gl.FRAGMENT_SHADER );
  8275. } else if ( type === "vertex" ) {
  8276. shader = _gl.createShader( _gl.VERTEX_SHADER );
  8277. }
  8278. _gl.shaderSource( shader, string );
  8279. _gl.compileShader( shader );
  8280. if ( !_gl.getShaderParameter( shader, _gl.COMPILE_STATUS ) ) {
  8281. console.error( _gl.getShaderInfoLog( shader ) );
  8282. console.error( string );
  8283. return null;
  8284. }
  8285. return shader;
  8286. };
  8287. // Textures
  8288. function isPowerOfTwo ( value ) {
  8289. return ( value & ( value - 1 ) ) === 0;
  8290. };
  8291. function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) {
  8292. if ( isImagePowerOfTwo ) {
  8293. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
  8294. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
  8295. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
  8296. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
  8297. } else {
  8298. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  8299. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  8300. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
  8301. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
  8302. }
  8303. };
  8304. this.setTexture = function ( texture, slot ) {
  8305. if ( texture.needsUpdate ) {
  8306. if ( ! texture.__webglInit ) {
  8307. texture.__webglInit = true;
  8308. texture.__webglTexture = _gl.createTexture();
  8309. _this.info.memory.textures ++;
  8310. }
  8311. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8312. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  8313. _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  8314. var image = texture.image,
  8315. isImagePowerOfTwo = isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ),
  8316. glFormat = paramThreeToGL( texture.format ),
  8317. glType = paramThreeToGL( texture.type );
  8318. setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo );
  8319. if ( texture instanceof THREE.DataTexture ) {
  8320. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data );
  8321. } else {
  8322. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image );
  8323. }
  8324. if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  8325. texture.needsUpdate = false;
  8326. if ( texture.onUpdate ) texture.onUpdate();
  8327. } else {
  8328. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8329. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  8330. }
  8331. };
  8332. function clampToMaxSize ( image, maxSize ) {
  8333. if ( image.width <= maxSize && image.height <= maxSize ) {
  8334. return image;
  8335. }
  8336. // Warning: Scaling through the canvas will only work with images that use
  8337. // premultiplied alpha.
  8338. var maxDimension = Math.max( image.width, image.height );
  8339. var newWidth = Math.floor( image.width * maxSize / maxDimension );
  8340. var newHeight = Math.floor( image.height * maxSize / maxDimension );
  8341. var canvas = document.createElement( 'canvas' );
  8342. canvas.width = newWidth;
  8343. canvas.height = newHeight;
  8344. var ctx = canvas.getContext( "2d" );
  8345. ctx.drawImage( image, 0, 0, image.width, image.height, 0, 0, newWidth, newHeight );
  8346. return canvas;
  8347. }
  8348. function setCubeTexture ( texture, slot ) {
  8349. if ( texture.image.length === 6 ) {
  8350. if ( texture.needsUpdate ) {
  8351. if ( ! texture.image.__webglTextureCube ) {
  8352. texture.image.__webglTextureCube = _gl.createTexture();
  8353. }
  8354. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8355. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  8356. var cubeImage = [];
  8357. for ( var i = 0; i < 6; i ++ ) {
  8358. if ( _this.autoScaleCubemaps ) {
  8359. cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize );
  8360. } else {
  8361. cubeImage[ i ] = texture.image[ i ];
  8362. }
  8363. }
  8364. var image = cubeImage[ 0 ],
  8365. isImagePowerOfTwo = isPowerOfTwo( image.width ) && isPowerOfTwo( image.height ),
  8366. glFormat = paramThreeToGL( texture.format ),
  8367. glType = paramThreeToGL( texture.type );
  8368. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );
  8369. for ( var i = 0; i < 6; i ++ ) {
  8370. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
  8371. }
  8372. if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  8373. texture.needsUpdate = false;
  8374. if ( texture.onUpdate ) texture.onUpdate();
  8375. } else {
  8376. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8377. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  8378. }
  8379. }
  8380. };
  8381. function setCubeTextureDynamic ( texture, slot ) {
  8382. _gl.activeTexture( _gl.TEXTURE0 + slot );
  8383. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.__webglTexture );
  8384. };
  8385. // Render targets
  8386. function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {
  8387. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  8388. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, renderTarget.__webglTexture, 0 );
  8389. };
  8390. function setupRenderBuffer ( renderbuffer, renderTarget ) {
  8391. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
  8392. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  8393. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
  8394. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  8395. /* For some reason this is not working. Defaulting to RGBA4.
  8396. } else if( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  8397. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
  8398. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  8399. */
  8400. } else if( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  8401. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
  8402. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  8403. } else {
  8404. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );
  8405. }
  8406. };
  8407. this.setRenderTarget = function ( renderTarget ) {
  8408. var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
  8409. if ( renderTarget && ! renderTarget.__webglFramebuffer ) {
  8410. if( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
  8411. if( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;
  8412. renderTarget.__webglTexture = _gl.createTexture();
  8413. // Setup texture, create render and frame buffers
  8414. var isTargetPowerOfTwo = isPowerOfTwo( renderTarget.width ) && isPowerOfTwo( renderTarget.height ),
  8415. glFormat = paramThreeToGL( renderTarget.format ),
  8416. glType = paramThreeToGL( renderTarget.type );
  8417. if ( isCube ) {
  8418. renderTarget.__webglFramebuffer = [];
  8419. renderTarget.__webglRenderbuffer = [];
  8420. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  8421. setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo );
  8422. for ( var i = 0; i < 6; i ++ ) {
  8423. renderTarget.__webglFramebuffer[ i ] = _gl.createFramebuffer();
  8424. renderTarget.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
  8425. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  8426. setupFrameBuffer( renderTarget.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
  8427. setupRenderBuffer( renderTarget.__webglRenderbuffer[ i ], renderTarget );
  8428. }
  8429. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  8430. } else {
  8431. renderTarget.__webglFramebuffer = _gl.createFramebuffer();
  8432. renderTarget.__webglRenderbuffer = _gl.createRenderbuffer();
  8433. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  8434. setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo );
  8435. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  8436. setupFrameBuffer( renderTarget.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
  8437. setupRenderBuffer( renderTarget.__webglRenderbuffer, renderTarget );
  8438. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  8439. }
  8440. // Release everything
  8441. if ( isCube ) {
  8442. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  8443. } else {
  8444. _gl.bindTexture( _gl.TEXTURE_2D, null );
  8445. }
  8446. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  8447. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null);
  8448. }
  8449. var framebuffer, width, height, vx, vy;
  8450. if ( renderTarget ) {
  8451. if ( isCube ) {
  8452. framebuffer = renderTarget.__webglFramebuffer[ renderTarget.activeCubeFace ];
  8453. } else {
  8454. framebuffer = renderTarget.__webglFramebuffer;
  8455. }
  8456. width = renderTarget.width;
  8457. height = renderTarget.height;
  8458. vx = 0;
  8459. vy = 0;
  8460. } else {
  8461. framebuffer = null;
  8462. width = _viewportWidth;
  8463. height = _viewportHeight;
  8464. vx = _viewportX;
  8465. vy = _viewportY;
  8466. }
  8467. if ( framebuffer !== _currentFramebuffer ) {
  8468. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  8469. _gl.viewport( vx, vy, width, height );
  8470. _currentFramebuffer = framebuffer;
  8471. }
  8472. _currentWidth = width;
  8473. _currentHeight = height;
  8474. };
  8475. function updateRenderTargetMipmap ( renderTarget ) {
  8476. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  8477. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  8478. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  8479. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  8480. } else {
  8481. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  8482. _gl.generateMipmap( _gl.TEXTURE_2D );
  8483. _gl.bindTexture( _gl.TEXTURE_2D, null );
  8484. }
  8485. };
  8486. // Fallback filters for non-power-of-2 textures
  8487. function filterFallback ( f ) {
  8488. switch ( f ) {
  8489. case THREE.NearestFilter:
  8490. case THREE.NearestMipMapNearestFilter:
  8491. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST; break;
  8492. case THREE.LinearFilter:
  8493. case THREE.LinearMipMapNearestFilter:
  8494. case THREE.LinearMipMapLinearFilter:
  8495. default:
  8496. return _gl.LINEAR; break;
  8497. }
  8498. };
  8499. // Map three.js constants to WebGL constants
  8500. function paramThreeToGL ( p ) {
  8501. switch ( p ) {
  8502. case THREE.RepeatWrapping: return _gl.REPEAT; break;
  8503. case THREE.ClampToEdgeWrapping: return _gl.CLAMP_TO_EDGE; break;
  8504. case THREE.MirroredRepeatWrapping: return _gl.MIRRORED_REPEAT; break;
  8505. case THREE.NearestFilter: return _gl.NEAREST; break;
  8506. case THREE.NearestMipMapNearestFilter: return _gl.NEAREST_MIPMAP_NEAREST; break;
  8507. case THREE.NearestMipMapLinearFilter: return _gl.NEAREST_MIPMAP_LINEAR; break;
  8508. case THREE.LinearFilter: return _gl.LINEAR; break;
  8509. case THREE.LinearMipMapNearestFilter: return _gl.LINEAR_MIPMAP_NEAREST; break;
  8510. case THREE.LinearMipMapLinearFilter: return _gl.LINEAR_MIPMAP_LINEAR; break;
  8511. case THREE.ByteType: return _gl.BYTE; break;
  8512. case THREE.UnsignedByteType: return _gl.UNSIGNED_BYTE; break;
  8513. case THREE.ShortType: return _gl.SHORT; break;
  8514. case THREE.UnsignedShortType: return _gl.UNSIGNED_SHORT; break;
  8515. case THREE.IntType: return _gl.INT; break;
  8516. case THREE.UnsignedIntType: return _gl.UNSIGNED_INT; break;
  8517. case THREE.FloatType: return _gl.FLOAT; break;
  8518. case THREE.AlphaFormat: return _gl.ALPHA; break;
  8519. case THREE.RGBFormat: return _gl.RGB; break;
  8520. case THREE.RGBAFormat: return _gl.RGBA; break;
  8521. case THREE.LuminanceFormat: return _gl.LUMINANCE; break;
  8522. case THREE.LuminanceAlphaFormat: return _gl.LUMINANCE_ALPHA; break;
  8523. case THREE.AddEquation: return _gl.FUNC_ADD; break;
  8524. case THREE.SubtractEquation: return _gl.FUNC_SUBTRACT; break;
  8525. case THREE.ReverseSubtractEquation: return _gl.FUNC_REVERSE_SUBTRACT; break;
  8526. case THREE.ZeroFactor: return _gl.ZERO; break;
  8527. case THREE.OneFactor: return _gl.ONE; break;
  8528. case THREE.SrcColorFactor: return _gl.SRC_COLOR; break;
  8529. case THREE.OneMinusSrcColorFactor: return _gl.ONE_MINUS_SRC_COLOR; break;
  8530. case THREE.SrcAlphaFactor: return _gl.SRC_ALPHA; break;
  8531. case THREE.OneMinusSrcAlphaFactor: return _gl.ONE_MINUS_SRC_ALPHA; break;
  8532. case THREE.DstAlphaFactor: return _gl.DST_ALPHA; break;
  8533. case THREE.OneMinusDstAlphaFactor: return _gl.ONE_MINUS_DST_ALPHA; break;
  8534. case THREE.DstColorFactor: return _gl.DST_COLOR; break;
  8535. case THREE.OneMinusDstColorFactor: return _gl.ONE_MINUS_DST_COLOR; break;
  8536. case THREE.SrcAlphaSaturateFactor: return _gl.SRC_ALPHA_SATURATE; break;
  8537. }
  8538. return 0;
  8539. };
  8540. // Allocations
  8541. function allocateBones ( object ) {
  8542. // default for when object is not specified
  8543. // ( for example when prebuilding shader
  8544. // to be used with multiple objects )
  8545. //
  8546. // - leave some extra space for other uniforms
  8547. // - limit here is ANGLE's 254 max uniform vectors
  8548. // (up to 54 should be safe)
  8549. var maxBones = 50;
  8550. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  8551. maxBones = object.bones.length;
  8552. }
  8553. return maxBones;
  8554. };
  8555. function allocateLights ( lights ) {
  8556. var l, ll, light, dirLights, pointLights, maxDirLights, maxPointLights;
  8557. dirLights = pointLights = maxDirLights = maxPointLights = 0;
  8558. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  8559. light = lights[ l ];
  8560. if ( light.onlyShadow ) continue;
  8561. if ( light instanceof THREE.DirectionalLight ) dirLights ++;
  8562. if ( light instanceof THREE.PointLight ) pointLights ++;
  8563. if ( light instanceof THREE.SpotLight ) pointLights ++;
  8564. }
  8565. if ( ( pointLights + dirLights ) <= _maxLights ) {
  8566. maxDirLights = dirLights;
  8567. maxPointLights = pointLights;
  8568. } else {
  8569. maxDirLights = Math.ceil( _maxLights * dirLights / ( pointLights + dirLights ) );
  8570. maxPointLights = _maxLights - maxDirLights;
  8571. }
  8572. return { 'directional' : maxDirLights, 'point' : maxPointLights };
  8573. };
  8574. function allocateShadows ( lights ) {
  8575. var l, ll, light, maxShadows = 0;
  8576. for ( l = 0, ll = lights.length; l < ll; l++ ) {
  8577. light = lights[ l ];
  8578. if ( ! light.castShadow ) continue;
  8579. if ( light instanceof THREE.SpotLight ) maxShadows ++;
  8580. if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++;
  8581. }
  8582. return maxShadows;
  8583. };
  8584. // Initialization
  8585. function initGL () {
  8586. var gl;
  8587. try {
  8588. if ( ! ( gl = _canvas.getContext( 'experimental-webgl', { alpha: _alpha, premultipliedAlpha: _premultipliedAlpha, antialias: _antialias, stencil: _stencil, preserveDrawingBuffer: _preserveDrawingBuffer } ) ) ) {
  8589. throw 'Error creating WebGL context.';
  8590. }
  8591. } catch ( error ) {
  8592. console.error( error );
  8593. }
  8594. return gl;
  8595. };
  8596. function setDefaultGLState () {
  8597. _gl.clearColor( 0, 0, 0, 1 );
  8598. _gl.clearDepth( 1 );
  8599. _gl.clearStencil( 0 );
  8600. _gl.enable( _gl.DEPTH_TEST );
  8601. _gl.depthFunc( _gl.LEQUAL );
  8602. _gl.frontFace( _gl.CCW );
  8603. _gl.cullFace( _gl.BACK );
  8604. _gl.enable( _gl.CULL_FACE );
  8605. _gl.enable( _gl.BLEND );
  8606. _gl.blendEquation( _gl.FUNC_ADD );
  8607. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  8608. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  8609. };
  8610. // default plugins (order is important)
  8611. this.shadowMapPlugin = new THREE.ShadowMapPlugin();
  8612. this.addPrePlugin( this.shadowMapPlugin );
  8613. this.addPostPlugin( new THREE.SpritePlugin() );
  8614. this.addPostPlugin( new THREE.LensFlarePlugin() );
  8615. };
  8616. /**
  8617. * @author szimek / https://github.com/szimek/
  8618. */
  8619. THREE.WebGLRenderTarget = function ( width, height, options ) {
  8620. this.width = width;
  8621. this.height = height;
  8622. options = options || {};
  8623. this.wrapS = options.wrapS !== undefined ? options.wrapS : THREE.ClampToEdgeWrapping;
  8624. this.wrapT = options.wrapT !== undefined ? options.wrapT : THREE.ClampToEdgeWrapping;
  8625. this.magFilter = options.magFilter !== undefined ? options.magFilter : THREE.LinearFilter;
  8626. this.minFilter = options.minFilter !== undefined ? options.minFilter : THREE.LinearMipMapLinearFilter;
  8627. this.offset = new THREE.Vector2( 0, 0 );
  8628. this.repeat = new THREE.Vector2( 1, 1 );
  8629. this.format = options.format !== undefined ? options.format : THREE.RGBAFormat;
  8630. this.type = options.type !== undefined ? options.type : THREE.UnsignedByteType;
  8631. this.depthBuffer = options.depthBuffer !== undefined ? options.depthBuffer : true;
  8632. this.stencilBuffer = options.stencilBuffer !== undefined ? options.stencilBuffer : true;
  8633. this.generateMipmaps = true;
  8634. };
  8635. THREE.WebGLRenderTarget.prototype.clone = function() {
  8636. var tmp = new THREE.WebGLRenderTarget( this.width, this.height );
  8637. tmp.wrapS = this.wrapS;
  8638. tmp.wrapT = this.wrapT;
  8639. tmp.magFilter = this.magFilter;
  8640. tmp.minFilter = this.minFilter;
  8641. tmp.offset.copy( this.offset );
  8642. tmp.repeat.copy( this.repeat );
  8643. tmp.format = this.format;
  8644. tmp.type = this.type;
  8645. tmp.depthBuffer = this.depthBuffer;
  8646. tmp.stencilBuffer = this.stencilBuffer;
  8647. return tmp;
  8648. };
  8649. /**
  8650. * @author alteredq / http://alteredqualia.com
  8651. */
  8652. THREE.WebGLRenderTargetCube = function ( width, height, options ) {
  8653. THREE.WebGLRenderTarget.call( this, width, height, options );
  8654. this.activeCubeFace = 0; // PX 0, NX 1, PY 2, NY 3, PZ 4, NZ 5
  8655. };
  8656. THREE.WebGLRenderTargetCube.prototype = new THREE.WebGLRenderTarget();
  8657. THREE.WebGLRenderTargetCube.prototype.constructor = THREE.WebGLRenderTargetCube;
  8658. /**
  8659. * @author mr.doob / http://mrdoob.com/
  8660. */
  8661. THREE.RenderableVertex = function () {
  8662. this.positionWorld = new THREE.Vector3();
  8663. this.positionScreen = new THREE.Vector4();
  8664. this.visible = true;
  8665. };
  8666. THREE.RenderableVertex.prototype.copy = function ( vertex ) {
  8667. this.positionWorld.copy( vertex.positionWorld );
  8668. this.positionScreen.copy( vertex.positionScreen );
  8669. }
  8670. /**
  8671. * @author mr.doob / http://mrdoob.com/
  8672. */
  8673. THREE.RenderableFace3 = function () {
  8674. this.v1 = new THREE.RenderableVertex();
  8675. this.v2 = new THREE.RenderableVertex();
  8676. this.v3 = new THREE.RenderableVertex();
  8677. this.centroidWorld = new THREE.Vector3();
  8678. this.centroidScreen = new THREE.Vector3();
  8679. this.normalWorld = new THREE.Vector3();
  8680. this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  8681. this.material = null;
  8682. this.faceMaterial = null;
  8683. this.uvs = [[]];
  8684. this.z = null;
  8685. };
  8686. /**
  8687. * @author mr.doob / http://mrdoob.com/
  8688. */
  8689. THREE.RenderableFace4 = function () {
  8690. this.v1 = new THREE.RenderableVertex();
  8691. this.v2 = new THREE.RenderableVertex();
  8692. this.v3 = new THREE.RenderableVertex();
  8693. this.v4 = new THREE.RenderableVertex();
  8694. this.centroidWorld = new THREE.Vector3();
  8695. this.centroidScreen = new THREE.Vector3();
  8696. this.normalWorld = new THREE.Vector3();
  8697. this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  8698. this.material = null;
  8699. this.faceMaterial = null;
  8700. this.uvs = [[]];
  8701. this.z = null;
  8702. };
  8703. /**
  8704. * @author mr.doob / http://mrdoob.com/
  8705. */
  8706. THREE.RenderableObject = function () {
  8707. this.object = null;
  8708. this.z = null;
  8709. };
  8710. /**
  8711. * @author mr.doob / http://mrdoob.com/
  8712. */
  8713. THREE.RenderableParticle = function () {
  8714. this.x = null;
  8715. this.y = null;
  8716. this.z = null;
  8717. this.rotation = null;
  8718. this.scale = new THREE.Vector2();
  8719. this.material = null;
  8720. };
  8721. /**
  8722. * @author mr.doob / http://mrdoob.com/
  8723. */
  8724. THREE.RenderableLine = function () {
  8725. this.z = null;
  8726. this.v1 = new THREE.RenderableVertex();
  8727. this.v2 = new THREE.RenderableVertex();
  8728. this.material = null;
  8729. };
  8730. /**
  8731. * @author alteredq / http://alteredqualia.com/
  8732. */
  8733. THREE.BufferGeometry = function () {
  8734. this.id = THREE.GeometryCount ++;
  8735. // GL buffers
  8736. this.vertexIndexBuffer = null;
  8737. this.vertexPositionBuffer = null;
  8738. this.vertexNormalBuffer = null;
  8739. this.vertexUvBuffer = null;
  8740. this.vertexColorBuffer = null;
  8741. // typed arrays (kept only if dynamic flag is set)
  8742. this.vertexIndexArray = null;
  8743. this.vertexPositionArray = null;
  8744. this.vertexNormalArray = null;
  8745. this.vertexUvArray = null;
  8746. this.vertexColorArray = null;
  8747. this.dynamic = false;
  8748. // boundings
  8749. this.boundingBox = null;
  8750. this.boundingSphere = null;
  8751. // for compatibility
  8752. this.morphTargets = [];
  8753. };
  8754. THREE.BufferGeometry.prototype = {
  8755. constructor : THREE.BufferGeometry,
  8756. // for compatibility
  8757. computeBoundingBox: function () {
  8758. },
  8759. // for compatibility
  8760. computeBoundingSphere: function () {
  8761. }
  8762. };
  8763. /**
  8764. * @author alteredq / http://alteredqualia.com/
  8765. */
  8766. THREE.Gyroscope = function () {
  8767. THREE.Object3D.call( this );
  8768. };
  8769. THREE.Gyroscope.prototype = new THREE.Object3D();
  8770. THREE.Gyroscope.prototype.constructor = THREE.Gyroscope;
  8771. THREE.Gyroscope.prototype.updateMatrixWorld = function ( force ) {
  8772. this.matrixAutoUpdate && this.updateMatrix();
  8773. // update matrixWorld
  8774. if ( this.matrixWorldNeedsUpdate || force ) {
  8775. if ( this.parent ) {
  8776. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  8777. this.matrixWorld.decompose( this.translationWorld, this.rotationWorld, this.scaleWorld );
  8778. this.matrix.decompose( this.translationObject, this.rotationObject, this.scaleObject );
  8779. this.matrixWorld.compose( this.translationWorld, this.rotationObject, this.scaleWorld );
  8780. } else {
  8781. this.matrixWorld.copy( this.matrix );
  8782. }
  8783. this.matrixWorldNeedsUpdate = false;
  8784. force = true;
  8785. }
  8786. // update children
  8787. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  8788. this.children[ i ].updateMatrixWorld( force );
  8789. }
  8790. };
  8791. THREE.Gyroscope.prototype.translationWorld = new THREE.Vector3();
  8792. THREE.Gyroscope.prototype.translationObject = new THREE.Vector3();
  8793. THREE.Gyroscope.prototype.rotationWorld = new THREE.Quaternion();
  8794. THREE.Gyroscope.prototype.rotationObject = new THREE.Quaternion();
  8795. THREE.Gyroscope.prototype.scaleWorld = new THREE.Vector3();
  8796. THREE.Gyroscope.prototype.scaleObject = new THREE.Vector3();
  8797. /**
  8798. * @author alteredq / http://alteredqualia.com/
  8799. *
  8800. * - shows frustum, line of sight and up of the camera
  8801. * - suitable for fast updates
  8802. * - based on frustum visualization in lightgl.js shadowmap example
  8803. * http://evanw.github.com/lightgl.js/tests/shadowmap.html
  8804. */
  8805. THREE.CameraHelper = function ( camera ) {
  8806. THREE.Object3D.call( this );
  8807. var _this = this;
  8808. this.lineGeometry = new THREE.Geometry();
  8809. this.lineMaterial = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: THREE.FaceColors } );
  8810. this.pointMap = {};
  8811. // colors
  8812. var hexFrustum = 0xffaa00,
  8813. hexCone = 0xff0000,
  8814. hexUp = 0x00aaff,
  8815. hexTarget = 0xffffff,
  8816. hexCross = 0x333333;
  8817. // near
  8818. addLine( "n1", "n2", hexFrustum );
  8819. addLine( "n2", "n4", hexFrustum );
  8820. addLine( "n4", "n3", hexFrustum );
  8821. addLine( "n3", "n1", hexFrustum );
  8822. // far
  8823. addLine( "f1", "f2", hexFrustum );
  8824. addLine( "f2", "f4", hexFrustum );
  8825. addLine( "f4", "f3", hexFrustum );
  8826. addLine( "f3", "f1", hexFrustum );
  8827. // sides
  8828. addLine( "n1", "f1", hexFrustum );
  8829. addLine( "n2", "f2", hexFrustum );
  8830. addLine( "n3", "f3", hexFrustum );
  8831. addLine( "n4", "f4", hexFrustum );
  8832. // cone
  8833. addLine( "p", "n1", hexCone );
  8834. addLine( "p", "n2", hexCone );
  8835. addLine( "p", "n3", hexCone );
  8836. addLine( "p", "n4", hexCone );
  8837. // up
  8838. addLine( "u1", "u2", hexUp );
  8839. addLine( "u2", "u3", hexUp );
  8840. addLine( "u3", "u1", hexUp );
  8841. // target
  8842. addLine( "c", "t", hexTarget );
  8843. addLine( "p", "c", hexCross );
  8844. // cross
  8845. addLine( "cn1", "cn2", hexCross );
  8846. addLine( "cn3", "cn4", hexCross );
  8847. addLine( "cf1", "cf2", hexCross );
  8848. addLine( "cf3", "cf4", hexCross );
  8849. this.camera = camera;
  8850. function addLine( a, b, hex ) {
  8851. addPoint( a, hex );
  8852. addPoint( b, hex );
  8853. }
  8854. function addPoint( id, hex ) {
  8855. _this.lineGeometry.vertices.push( new THREE.Vertex() );
  8856. _this.lineGeometry.colors.push( new THREE.Color( hex ) );
  8857. if ( _this.pointMap[ id ] === undefined ) _this.pointMap[ id ] = [];
  8858. _this.pointMap[ id ].push( _this.lineGeometry.vertices.length - 1 );
  8859. }
  8860. this.update( camera );
  8861. this.lines = new THREE.Line( this.lineGeometry, this.lineMaterial, THREE.LinePieces );
  8862. this.add( this.lines );
  8863. };
  8864. THREE.CameraHelper.prototype = new THREE.Object3D();
  8865. THREE.CameraHelper.prototype.constructor = THREE.CameraHelper;
  8866. THREE.CameraHelper.prototype.update = function () {
  8867. var camera = this.camera;
  8868. var w = 1;
  8869. var h = 1;
  8870. var _this = this;
  8871. // we need just camera projection matrix
  8872. // world matrix must be identity
  8873. THREE.CameraHelper.__c.projectionMatrix.copy( camera.projectionMatrix );
  8874. // center / target
  8875. setPoint( "c", 0, 0, -1 );
  8876. setPoint( "t", 0, 0, 1 );
  8877. // near
  8878. setPoint( "n1", -w, -h, -1 );
  8879. setPoint( "n2", w, -h, -1 );
  8880. setPoint( "n3", -w, h, -1 );
  8881. setPoint( "n4", w, h, -1 );
  8882. // far
  8883. setPoint( "f1", -w, -h, 1 );
  8884. setPoint( "f2", w, -h, 1 );
  8885. setPoint( "f3", -w, h, 1 );
  8886. setPoint( "f4", w, h, 1 );
  8887. // up
  8888. setPoint( "u1", w * 0.7, h * 1.1, -1 );
  8889. setPoint( "u2", -w * 0.7, h * 1.1, -1 );
  8890. setPoint( "u3", 0, h * 2, -1 );
  8891. // cross
  8892. setPoint( "cf1", -w, 0, 1 );
  8893. setPoint( "cf2", w, 0, 1 );
  8894. setPoint( "cf3", 0, -h, 1 );
  8895. setPoint( "cf4", 0, h, 1 );
  8896. setPoint( "cn1", -w, 0, -1 );
  8897. setPoint( "cn2", w, 0, -1 );
  8898. setPoint( "cn3", 0, -h, -1 );
  8899. setPoint( "cn4", 0, h, -1 );
  8900. function setPoint( point, x, y, z ) {
  8901. THREE.CameraHelper.__v.set( x, y, z );
  8902. THREE.CameraHelper.__projector.unprojectVector( THREE.CameraHelper.__v, THREE.CameraHelper.__c );
  8903. var points = _this.pointMap[ point ];
  8904. if ( points !== undefined ) {
  8905. for ( var i = 0, il = points.length; i < il; i ++ ) {
  8906. var j = points[ i ];
  8907. _this.lineGeometry.vertices[ j ].copy( THREE.CameraHelper.__v );
  8908. }
  8909. }
  8910. }
  8911. this.lineGeometry.__dirtyVertices = true;
  8912. };
  8913. THREE.CameraHelper.__projector = new THREE.Projector();
  8914. THREE.CameraHelper.__v = new THREE.Vector3();
  8915. THREE.CameraHelper.__c = new THREE.Camera();
  8916. /**
  8917. * @author mikael emtinger / http://gomo.se/
  8918. * @author alteredq / http://alteredqualia.com/
  8919. */
  8920. THREE.LensFlare = function ( texture, size, distance, blending, color ) {
  8921. THREE.Object3D.call( this );
  8922. this.lensFlares = [];
  8923. this.positionScreen = new THREE.Vector3();
  8924. this.customUpdateCallback = undefined;
  8925. if( texture !== undefined ) {
  8926. this.add( texture, size, distance, blending, color );
  8927. }
  8928. };
  8929. THREE.LensFlare.prototype = new THREE.Object3D();
  8930. THREE.LensFlare.prototype.constructor = THREE.LensFlare;
  8931. THREE.LensFlare.prototype.supr = THREE.Object3D.prototype;
  8932. /*
  8933. * Add: adds another flare
  8934. */
  8935. THREE.LensFlare.prototype.add = function ( texture, size, distance, blending, color, opacity ) {
  8936. if( size === undefined ) size = -1;
  8937. if( distance === undefined ) distance = 0;
  8938. if( opacity === undefined ) opacity = 1;
  8939. if( color === undefined ) color = new THREE.Color( 0xffffff );
  8940. if( blending === undefined ) blending = THREE.NormalBlending;
  8941. distance = Math.min( distance, Math.max( 0, distance ) );
  8942. this.lensFlares.push( { texture: texture, // THREE.Texture
  8943. size: size, // size in pixels (-1 = use texture.width)
  8944. distance: distance, // distance (0-1) from light source (0=at light source)
  8945. x: 0, y: 0, z: 0, // screen position (-1 => 1) z = 0 is ontop z = 1 is back
  8946. scale: 1, // scale
  8947. rotation: 1, // rotation
  8948. opacity: opacity, // opacity
  8949. color: color, // color
  8950. blending: blending } ); // blending
  8951. };
  8952. /*
  8953. * Update lens flares update positions on all flares based on the screen position
  8954. * Set myLensFlare.customUpdateCallback to alter the flares in your project specific way.
  8955. */
  8956. THREE.LensFlare.prototype.updateLensFlares = function () {
  8957. var f, fl = this.lensFlares.length;
  8958. var flare;
  8959. var vecX = -this.positionScreen.x * 2;
  8960. var vecY = -this.positionScreen.y * 2;
  8961. for( f = 0; f < fl; f ++ ) {
  8962. flare = this.lensFlares[ f ];
  8963. flare.x = this.positionScreen.x + vecX * flare.distance;
  8964. flare.y = this.positionScreen.y + vecY * flare.distance;
  8965. flare.wantedRotation = flare.x * Math.PI * 0.25;
  8966. flare.rotation += ( flare.wantedRotation - flare.rotation ) * 0.25;
  8967. }
  8968. };
  8969. /**
  8970. * @author alteredq / http://alteredqualia.com/
  8971. */
  8972. THREE.ImmediateRenderObject = function ( ) {
  8973. THREE.Object3D.call( this );
  8974. this.render = function( renderCallback ) {
  8975. };
  8976. };
  8977. THREE.ImmediateRenderObject.prototype = new THREE.Object3D();
  8978. THREE.ImmediateRenderObject.prototype.constructor = THREE.ImmediateRenderObject;
  8979. /**
  8980. * @author mikael emtinger / http://gomo.se/
  8981. * @author alteredq / http://alteredqualia.com/
  8982. */
  8983. THREE.LensFlarePlugin = function ( ) {
  8984. var _gl, _renderer, _lensFlare = {};
  8985. this.init = function ( renderer ) {
  8986. _gl = renderer.context;
  8987. _renderer = renderer;
  8988. _lensFlare.vertices = new Float32Array( 8 + 8 );
  8989. _lensFlare.faces = new Uint16Array( 6 );
  8990. var i = 0;
  8991. _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = -1; // vertex
  8992. _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 0; // uv... etc.
  8993. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = -1;
  8994. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 0;
  8995. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1;
  8996. _lensFlare.vertices[ i++ ] = 1; _lensFlare.vertices[ i++ ] = 1;
  8997. _lensFlare.vertices[ i++ ] = -1; _lensFlare.vertices[ i++ ] = 1;
  8998. _lensFlare.vertices[ i++ ] = 0; _lensFlare.vertices[ i++ ] = 1;
  8999. i = 0;
  9000. _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 1; _lensFlare.faces[ i++ ] = 2;
  9001. _lensFlare.faces[ i++ ] = 0; _lensFlare.faces[ i++ ] = 2; _lensFlare.faces[ i++ ] = 3;
  9002. // buffers
  9003. _lensFlare.vertexBuffer = _gl.createBuffer();
  9004. _lensFlare.elementBuffer = _gl.createBuffer();
  9005. _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
  9006. _gl.bufferData( _gl.ARRAY_BUFFER, _lensFlare.vertices, _gl.STATIC_DRAW );
  9007. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
  9008. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.faces, _gl.STATIC_DRAW );
  9009. // textures
  9010. _lensFlare.tempTexture = _gl.createTexture();
  9011. _lensFlare.occlusionTexture = _gl.createTexture();
  9012. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  9013. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, 16, 16, 0, _gl.RGB, _gl.UNSIGNED_BYTE, null );
  9014. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  9015. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  9016. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
  9017. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
  9018. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture );
  9019. _gl.texImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, 16, 16, 0, _gl.RGBA, _gl.UNSIGNED_BYTE, null );
  9020. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  9021. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  9022. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MAG_FILTER, _gl.NEAREST );
  9023. _gl.texParameteri( _gl.TEXTURE_2D, _gl.TEXTURE_MIN_FILTER, _gl.NEAREST );
  9024. if ( _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) <= 0 ) {
  9025. _lensFlare.hasVertexTexture = false;
  9026. _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlare" ] );
  9027. } else {
  9028. _lensFlare.hasVertexTexture = true;
  9029. _lensFlare.program = createProgram( THREE.ShaderFlares[ "lensFlareVertexTexture" ] );
  9030. }
  9031. _lensFlare.attributes = {};
  9032. _lensFlare.uniforms = {};
  9033. _lensFlare.attributes.vertex = _gl.getAttribLocation ( _lensFlare.program, "position" );
  9034. _lensFlare.attributes.uv = _gl.getAttribLocation ( _lensFlare.program, "uv" );
  9035. _lensFlare.uniforms.renderType = _gl.getUniformLocation( _lensFlare.program, "renderType" );
  9036. _lensFlare.uniforms.map = _gl.getUniformLocation( _lensFlare.program, "map" );
  9037. _lensFlare.uniforms.occlusionMap = _gl.getUniformLocation( _lensFlare.program, "occlusionMap" );
  9038. _lensFlare.uniforms.opacity = _gl.getUniformLocation( _lensFlare.program, "opacity" );
  9039. _lensFlare.uniforms.color = _gl.getUniformLocation( _lensFlare.program, "color" );
  9040. _lensFlare.uniforms.scale = _gl.getUniformLocation( _lensFlare.program, "scale" );
  9041. _lensFlare.uniforms.rotation = _gl.getUniformLocation( _lensFlare.program, "rotation" );
  9042. _lensFlare.uniforms.screenPosition = _gl.getUniformLocation( _lensFlare.program, "screenPosition" );
  9043. _lensFlare.attributesEnabled = false;
  9044. };
  9045. /*
  9046. * Render lens flares
  9047. * Method: renders 16x16 0xff00ff-colored points scattered over the light source area,
  9048. * reads these back and calculates occlusion.
  9049. * Then _lensFlare.update_lensFlares() is called to re-position and
  9050. * update transparency of flares. Then they are rendered.
  9051. *
  9052. */
  9053. this.render = function ( scene, camera, viewportWidth, viewportHeight ) {
  9054. var flares = scene.__webglFlares,
  9055. nFlares = flares.length;
  9056. if ( ! nFlares ) return;
  9057. var tempPosition = new THREE.Vector3();
  9058. var invAspect = viewportHeight / viewportWidth,
  9059. halfViewportWidth = viewportWidth * 0.5,
  9060. halfViewportHeight = viewportHeight * 0.5;
  9061. var size = 16 / viewportHeight,
  9062. scale = new THREE.Vector2( size * invAspect, size );
  9063. var screenPosition = new THREE.Vector3( 1, 1, 0 ),
  9064. screenPositionPixels = new THREE.Vector2( 1, 1 );
  9065. var uniforms = _lensFlare.uniforms,
  9066. attributes = _lensFlare.attributes;
  9067. // set _lensFlare program and reset blending
  9068. _gl.useProgram( _lensFlare.program );
  9069. if ( ! _lensFlare.attributesEnabled ) {
  9070. _gl.enableVertexAttribArray( _lensFlare.attributes.vertex );
  9071. _gl.enableVertexAttribArray( _lensFlare.attributes.uv );
  9072. _lensFlare.attributesEnabled = true;
  9073. }
  9074. // loop through all lens flares to update their occlusion and positions
  9075. // setup gl and common used attribs/unforms
  9076. _gl.uniform1i( uniforms.occlusionMap, 0 );
  9077. _gl.uniform1i( uniforms.map, 1 );
  9078. _gl.bindBuffer( _gl.ARRAY_BUFFER, _lensFlare.vertexBuffer );
  9079. _gl.vertexAttribPointer( attributes.vertex, 2, _gl.FLOAT, false, 2 * 8, 0 );
  9080. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 );
  9081. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _lensFlare.elementBuffer );
  9082. _gl.disable( _gl.CULL_FACE );
  9083. _gl.depthMask( false );
  9084. var i, j, jl, flare, sprite;
  9085. for ( i = 0; i < nFlares; i ++ ) {
  9086. size = 16 / viewportHeight;
  9087. scale.set( size * invAspect, size );
  9088. // calc object screen position
  9089. flare = flares[ i ];
  9090. tempPosition.set( flare.matrixWorld.elements[12], flare.matrixWorld.elements[13], flare.matrixWorld.elements[14] );
  9091. camera.matrixWorldInverse.multiplyVector3( tempPosition );
  9092. camera.projectionMatrix.multiplyVector3( tempPosition );
  9093. // setup arrays for gl programs
  9094. screenPosition.copy( tempPosition )
  9095. screenPositionPixels.x = screenPosition.x * halfViewportWidth + halfViewportWidth;
  9096. screenPositionPixels.y = screenPosition.y * halfViewportHeight + halfViewportHeight;
  9097. // screen cull
  9098. if ( _lensFlare.hasVertexTexture || (
  9099. screenPositionPixels.x > 0 &&
  9100. screenPositionPixels.x < viewportWidth &&
  9101. screenPositionPixels.y > 0 &&
  9102. screenPositionPixels.y < viewportHeight ) ) {
  9103. // save current RGB to temp texture
  9104. _gl.activeTexture( _gl.TEXTURE1 );
  9105. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  9106. _gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGB, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
  9107. // render pink quad
  9108. _gl.uniform1i( uniforms.renderType, 0 );
  9109. _gl.uniform2f( uniforms.scale, scale.x, scale.y );
  9110. _gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z );
  9111. _gl.disable( _gl.BLEND );
  9112. _gl.enable( _gl.DEPTH_TEST );
  9113. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9114. // copy result to occlusionMap
  9115. _gl.activeTexture( _gl.TEXTURE0 );
  9116. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.occlusionTexture );
  9117. _gl.copyTexImage2D( _gl.TEXTURE_2D, 0, _gl.RGBA, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
  9118. // restore graphics
  9119. _gl.uniform1i( uniforms.renderType, 1 );
  9120. _gl.disable( _gl.DEPTH_TEST );
  9121. _gl.activeTexture( _gl.TEXTURE1 );
  9122. _gl.bindTexture( _gl.TEXTURE_2D, _lensFlare.tempTexture );
  9123. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9124. // update object positions
  9125. flare.positionScreen.copy( screenPosition )
  9126. if ( flare.customUpdateCallback ) {
  9127. flare.customUpdateCallback( flare );
  9128. } else {
  9129. flare.updateLensFlares();
  9130. }
  9131. // render flares
  9132. _gl.uniform1i( uniforms.renderType, 2 );
  9133. _gl.enable( _gl.BLEND );
  9134. for ( j = 0, jl = flare.lensFlares.length; j < jl; j ++ ) {
  9135. sprite = flare.lensFlares[ j ];
  9136. if ( sprite.opacity > 0.001 && sprite.scale > 0.001 ) {
  9137. screenPosition.x = sprite.x;
  9138. screenPosition.y = sprite.y;
  9139. screenPosition.z = sprite.z;
  9140. size = sprite.size * sprite.scale / viewportHeight;
  9141. scale.x = size * invAspect;
  9142. scale.y = size;
  9143. _gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z );
  9144. _gl.uniform2f( uniforms.scale, scale.x, scale.y );
  9145. _gl.uniform1f( uniforms.rotation, sprite.rotation );
  9146. _gl.uniform1f( uniforms.opacity, sprite.opacity );
  9147. _gl.uniform3f( uniforms.color, sprite.color.r, sprite.color.g, sprite.color.b );
  9148. _renderer.setBlending( sprite.blending, sprite.blendEquation, sprite.blendSrc, sprite.blendDst );
  9149. _renderer.setTexture( sprite.texture, 1 );
  9150. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9151. }
  9152. }
  9153. }
  9154. }
  9155. // restore gl
  9156. _gl.enable( _gl.CULL_FACE );
  9157. _gl.enable( _gl.DEPTH_TEST );
  9158. _gl.depthMask( true );
  9159. };
  9160. function createProgram ( shader ) {
  9161. var program = _gl.createProgram();
  9162. var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER );
  9163. var vertexShader = _gl.createShader( _gl.VERTEX_SHADER );
  9164. _gl.shaderSource( fragmentShader, shader.fragmentShader );
  9165. _gl.shaderSource( vertexShader, shader.vertexShader );
  9166. _gl.compileShader( fragmentShader );
  9167. _gl.compileShader( vertexShader );
  9168. _gl.attachShader( program, fragmentShader );
  9169. _gl.attachShader( program, vertexShader );
  9170. _gl.linkProgram( program );
  9171. return program;
  9172. };
  9173. };/**
  9174. * @author alteredq / http://alteredqualia.com/
  9175. */
  9176. THREE.ShadowMapPlugin = function ( ) {
  9177. var _gl,
  9178. _renderer,
  9179. _depthMaterial, _depthMaterialMorph,
  9180. _frustum = new THREE.Frustum(),
  9181. _projScreenMatrix = new THREE.Matrix4(),
  9182. _min = new THREE.Vector3(),
  9183. _max = new THREE.Vector3();
  9184. this.init = function ( renderer ) {
  9185. _gl = renderer.context;
  9186. _renderer = renderer;
  9187. var depthShader = THREE.ShaderLib[ "depthRGBA" ];
  9188. var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms );
  9189. _depthMaterial = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms } );
  9190. _depthMaterialMorph = new THREE.ShaderMaterial( { fragmentShader: depthShader.fragmentShader, vertexShader: depthShader.vertexShader, uniforms: depthUniforms, morphTargets: true } );
  9191. _depthMaterial._shadowPass = true;
  9192. _depthMaterialMorph._shadowPass = true;
  9193. };
  9194. this.render = function ( scene, camera ) {
  9195. if ( ! ( _renderer.shadowMapEnabled && _renderer.shadowMapAutoUpdate ) ) return;
  9196. this.update( scene, camera );
  9197. };
  9198. this.update = function ( scene, camera ) {
  9199. var i, il, j, jl, n,
  9200. shadowMap, shadowMatrix, shadowCamera,
  9201. program, buffer, material,
  9202. webglObject, object, light,
  9203. renderList,
  9204. lights = [],
  9205. k = 0,
  9206. fog = null;
  9207. // set GL state for depth map
  9208. _gl.clearColor( 1, 1, 1, 1 );
  9209. _gl.disable( _gl.BLEND );
  9210. if ( _renderer.shadowMapCullFrontFaces ) _gl.cullFace( _gl.FRONT );
  9211. _renderer.setDepthTest( true );
  9212. // preprocess lights
  9213. // - skip lights that are not casting shadows
  9214. // - create virtual lights for cascaded shadow maps
  9215. for ( i = 0, il = scene.__lights.length; i < il; i ++ ) {
  9216. light = scene.__lights[ i ];
  9217. if ( ! light.castShadow ) continue;
  9218. if ( ( light instanceof THREE.DirectionalLight ) && light.shadowCascade ) {
  9219. for ( n = 0; n < light.shadowCascadeCount; n ++ ) {
  9220. var virtualLight;
  9221. if ( ! light.shadowCascadeArray[ n ] ) {
  9222. virtualLight = createVirtualLight( light, n );
  9223. virtualLight.originalCamera = camera;
  9224. var gyro = new THREE.Gyroscope();
  9225. gyro.position = light.shadowCascadeOffset;
  9226. gyro.add( virtualLight );
  9227. gyro.add( virtualLight.target );
  9228. camera.add( gyro );
  9229. light.shadowCascadeArray[ n ] = virtualLight;
  9230. console.log( "Created virtualLight", virtualLight );
  9231. } else {
  9232. virtualLight = light.shadowCascadeArray[ n ];
  9233. }
  9234. updateVirtualLight( light, n );
  9235. lights[ k ] = virtualLight;
  9236. k ++;
  9237. }
  9238. } else {
  9239. lights[ k ] = light;
  9240. k ++;
  9241. }
  9242. }
  9243. // render depth map
  9244. for ( i = 0, il = lights.length; i < il; i ++ ) {
  9245. light = lights[ i ];
  9246. if ( ! light.shadowMap ) {
  9247. var pars = { minFilter: THREE.LinearFilter, magFilter: THREE.LinearFilter, format: THREE.RGBAFormat };
  9248. light.shadowMap = new THREE.WebGLRenderTarget( light.shadowMapWidth, light.shadowMapHeight, pars );
  9249. light.shadowMapSize = new THREE.Vector2( light.shadowMapWidth, light.shadowMapHeight );
  9250. light.shadowMatrix = new THREE.Matrix4();
  9251. }
  9252. if ( ! light.shadowCamera ) {
  9253. if ( light instanceof THREE.SpotLight ) {
  9254. light.shadowCamera = new THREE.PerspectiveCamera( light.shadowCameraFov, light.shadowMapWidth / light.shadowMapHeight, light.shadowCameraNear, light.shadowCameraFar );
  9255. } else if ( light instanceof THREE.DirectionalLight ) {
  9256. light.shadowCamera = new THREE.OrthographicCamera( light.shadowCameraLeft, light.shadowCameraRight, light.shadowCameraTop, light.shadowCameraBottom, light.shadowCameraNear, light.shadowCameraFar );
  9257. } else {
  9258. console.error( "Unsupported light type for shadow" );
  9259. continue;
  9260. }
  9261. scene.add( light.shadowCamera );
  9262. if ( _renderer.autoUpdateScene ) scene.updateMatrixWorld();
  9263. }
  9264. if ( light.shadowCameraVisible && ! light.cameraHelper ) {
  9265. light.cameraHelper = new THREE.CameraHelper( light.shadowCamera );
  9266. light.shadowCamera.add( light.cameraHelper );
  9267. }
  9268. if ( light.isVirtual && virtualLight.originalCamera == camera ) {
  9269. updateShadowCamera( camera, light );
  9270. }
  9271. shadowMap = light.shadowMap;
  9272. shadowMatrix = light.shadowMatrix;
  9273. shadowCamera = light.shadowCamera;
  9274. shadowCamera.position.copy( light.matrixWorld.getPosition() );
  9275. shadowCamera.lookAt( light.target.matrixWorld.getPosition() );
  9276. shadowCamera.updateMatrixWorld();
  9277. shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
  9278. if ( light.cameraHelper ) light.cameraHelper.lines.visible = light.shadowCameraVisible;
  9279. if ( light.shadowCameraVisible ) light.cameraHelper.update();
  9280. // compute shadow matrix
  9281. shadowMatrix.set( 0.5, 0.0, 0.0, 0.5,
  9282. 0.0, 0.5, 0.0, 0.5,
  9283. 0.0, 0.0, 0.5, 0.5,
  9284. 0.0, 0.0, 0.0, 1.0 );
  9285. shadowMatrix.multiplySelf( shadowCamera.projectionMatrix );
  9286. shadowMatrix.multiplySelf( shadowCamera.matrixWorldInverse );
  9287. // update camera matrices and frustum
  9288. if ( ! shadowCamera._viewMatrixArray ) shadowCamera._viewMatrixArray = new Float32Array( 16 );
  9289. if ( ! shadowCamera._projectionMatrixArray ) shadowCamera._projectionMatrixArray = new Float32Array( 16 );
  9290. shadowCamera.matrixWorldInverse.flattenToArray( shadowCamera._viewMatrixArray );
  9291. shadowCamera.projectionMatrix.flattenToArray( shadowCamera._projectionMatrixArray );
  9292. _projScreenMatrix.multiply( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  9293. _frustum.setFromMatrix( _projScreenMatrix );
  9294. // render shadow map
  9295. _renderer.setRenderTarget( shadowMap );
  9296. _renderer.clear();
  9297. // set object matrices & frustum culling
  9298. renderList = scene.__webglObjects;
  9299. for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
  9300. webglObject = renderList[ j ];
  9301. object = webglObject.object;
  9302. webglObject.render = false;
  9303. if ( object.visible && object.castShadow ) {
  9304. if ( ! ( object instanceof THREE.Mesh ) || ! ( object.frustumCulled ) || _frustum.contains( object ) ) {
  9305. //object.matrixWorld.flattenToArray( object._objectMatrixArray );
  9306. object._modelViewMatrix.multiply( shadowCamera.matrixWorldInverse, object.matrixWorld);
  9307. webglObject.render = true;
  9308. }
  9309. }
  9310. }
  9311. // render regular objects
  9312. for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
  9313. webglObject = renderList[ j ];
  9314. if ( webglObject.render ) {
  9315. object = webglObject.object;
  9316. buffer = webglObject.buffer;
  9317. _renderer.setObjectFaces( object );
  9318. if ( object.customDepthMaterial ) {
  9319. material = object.customDepthMaterial;
  9320. } else if ( object.geometry.morphTargets.length ) {
  9321. material = _depthMaterialMorph;
  9322. } else {
  9323. material = _depthMaterial;
  9324. }
  9325. if ( buffer instanceof THREE.BufferGeometry ) {
  9326. _renderer.renderBufferDirect( shadowCamera, scene.__lights, fog, material, buffer, object );
  9327. } else {
  9328. _renderer.renderBuffer( shadowCamera, scene.__lights, fog, material, buffer, object );
  9329. }
  9330. }
  9331. }
  9332. // set matrices and render immediate objects
  9333. renderList = scene.__webglObjectsImmediate;
  9334. for ( j = 0, jl = renderList.length; j < jl; j ++ ) {
  9335. webglObject = renderList[ j ];
  9336. object = webglObject.object;
  9337. if ( object.visible && object.castShadow ) {
  9338. if( object.matrixAutoUpdate ) {
  9339. //object.matrixWorld.flattenToArray( object._objectMatrixArray );
  9340. }
  9341. object._modelViewMatrix.multiply( shadowCamera.matrixWorldInverse, object.matrixWorld);
  9342. _renderer.renderImmediateObject( shadowCamera, scene.__lights, fog, _depthMaterial, object );
  9343. }
  9344. }
  9345. }
  9346. // restore GL state
  9347. var clearColor = _renderer.getClearColor(),
  9348. clearAlpha = _renderer.getClearAlpha();
  9349. _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha );
  9350. _gl.enable( _gl.BLEND );
  9351. if ( _renderer.shadowMapCullFrontFaces ) _gl.cullFace( _gl.BACK );
  9352. };
  9353. function createVirtualLight( light, cascade ) {
  9354. var virtualLight = new THREE.DirectionalLight();
  9355. virtualLight.isVirtual = true;
  9356. virtualLight.onlyShadow = true;
  9357. virtualLight.castShadow = true;
  9358. virtualLight.shadowCameraNear = light.shadowCameraNear;
  9359. virtualLight.shadowCameraFar = light.shadowCameraFar;
  9360. virtualLight.shadowCameraLeft = light.shadowCameraLeft;
  9361. virtualLight.shadowCameraRight = light.shadowCameraRight;
  9362. virtualLight.shadowCameraBottom = light.shadowCameraBottom;
  9363. virtualLight.shadowCameraTop = light.shadowCameraTop;
  9364. virtualLight.shadowCameraVisible = light.shadowCameraVisible;
  9365. virtualLight.shadowDarkness = light.shadowDarkness;
  9366. virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
  9367. virtualLight.shadowMapWidth = light.shadowCascadeWidth[ cascade ];
  9368. virtualLight.shadowMapHeight = light.shadowCascadeHeight[ cascade ];
  9369. virtualLight.pointsWorld = [];
  9370. virtualLight.pointsFrustum = [];
  9371. var pointsWorld = virtualLight.pointsWorld,
  9372. pointsFrustum = virtualLight.pointsFrustum;
  9373. for ( var i = 0; i < 8; i ++ ) {
  9374. pointsWorld[ i ] = new THREE.Vector3();
  9375. pointsFrustum[ i ] = new THREE.Vector3();
  9376. }
  9377. var nearZ = light.shadowCascadeNearZ[ cascade ];
  9378. var farZ = light.shadowCascadeFarZ[ cascade ];
  9379. pointsFrustum[ 0 ].set( -1, -1, nearZ );
  9380. pointsFrustum[ 1 ].set( 1, -1, nearZ );
  9381. pointsFrustum[ 2 ].set( -1, 1, nearZ );
  9382. pointsFrustum[ 3 ].set( 1, 1, nearZ );
  9383. pointsFrustum[ 4 ].set( -1, -1, farZ );
  9384. pointsFrustum[ 5 ].set( 1, -1, farZ );
  9385. pointsFrustum[ 6 ].set( -1, 1, farZ );
  9386. pointsFrustum[ 7 ].set( 1, 1, farZ );
  9387. return virtualLight;
  9388. }
  9389. // Synchronize virtual light with the original light
  9390. function updateVirtualLight( light, cascade ) {
  9391. var virtualLight = light.shadowCascadeArray[ cascade ];
  9392. virtualLight.position.copy( light.position );
  9393. virtualLight.target.position.copy( light.target.position );
  9394. virtualLight.lookAt( virtualLight.target );
  9395. virtualLight.shadowCameraVisible = light.shadowCameraVisible;
  9396. virtualLight.shadowDarkness = light.shadowDarkness;
  9397. virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
  9398. var nearZ = light.shadowCascadeNearZ[ cascade ];
  9399. var farZ = light.shadowCascadeFarZ[ cascade ];
  9400. var pointsFrustum = virtualLight.pointsFrustum;
  9401. pointsFrustum[ 0 ].z = nearZ;
  9402. pointsFrustum[ 1 ].z = nearZ;
  9403. pointsFrustum[ 2 ].z = nearZ;
  9404. pointsFrustum[ 3 ].z = nearZ;
  9405. pointsFrustum[ 4 ].z = farZ;
  9406. pointsFrustum[ 5 ].z = farZ;
  9407. pointsFrustum[ 6 ].z = farZ;
  9408. pointsFrustum[ 7 ].z = farZ;
  9409. }
  9410. // Fit shadow camera's ortho frustum to camera frustum
  9411. function updateShadowCamera( camera, light ) {
  9412. var shadowCamera = light.shadowCamera,
  9413. pointsFrustum = light.pointsFrustum,
  9414. pointsWorld = light.pointsWorld;
  9415. _min.set( Infinity, Infinity, Infinity );
  9416. _max.set( -Infinity, -Infinity, -Infinity );
  9417. for ( var i = 0; i < 8; i ++ ) {
  9418. var p = pointsWorld[ i ];
  9419. p.copy( pointsFrustum[ i ] );
  9420. THREE.ShadowMapPlugin.__projector.unprojectVector( p, camera );
  9421. shadowCamera.matrixWorldInverse.multiplyVector3( p );
  9422. if ( p.x < _min.x ) _min.x = p.x;
  9423. if ( p.x > _max.x ) _max.x = p.x;
  9424. if ( p.y < _min.y ) _min.y = p.y;
  9425. if ( p.y > _max.y ) _max.y = p.y;
  9426. if ( p.z < _min.z ) _min.z = p.z;
  9427. if ( p.z > _max.z ) _max.z = p.z;
  9428. }
  9429. shadowCamera.left = _min.x;
  9430. shadowCamera.right = _max.x;
  9431. shadowCamera.top = _max.y;
  9432. shadowCamera.bottom = _min.y;
  9433. // can't really fit near/far
  9434. //shadowCamera.near = _min.z;
  9435. //shadowCamera.far = _max.z;
  9436. shadowCamera.updateProjectionMatrix();
  9437. }
  9438. };
  9439. THREE.ShadowMapPlugin.__projector = new THREE.Projector();
  9440. /**
  9441. * @author mikael emtinger / http://gomo.se/
  9442. * @author alteredq / http://alteredqualia.com/
  9443. */
  9444. THREE.SpritePlugin = function ( ) {
  9445. var _gl, _renderer, _sprite = {};
  9446. this.init = function ( renderer ) {
  9447. _gl = renderer.context;
  9448. _renderer = renderer;
  9449. _sprite.vertices = new Float32Array( 8 + 8 );
  9450. _sprite.faces = new Uint16Array( 6 );
  9451. var i = 0;
  9452. _sprite.vertices[ i++ ] = -1; _sprite.vertices[ i++ ] = -1; // vertex 0
  9453. _sprite.vertices[ i++ ] = 0; _sprite.vertices[ i++ ] = 1; // uv 0
  9454. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = -1; // vertex 1
  9455. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 1; // uv 1
  9456. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 1; // vertex 2
  9457. _sprite.vertices[ i++ ] = 1; _sprite.vertices[ i++ ] = 0; // uv 2
  9458. _sprite.vertices[ i++ ] = -1; _sprite.vertices[ i++ ] = 1; // vertex 3
  9459. _sprite.vertices[ i++ ] = 0; _sprite.vertices[ i++ ] = 0; // uv 3
  9460. i = 0;
  9461. _sprite.faces[ i++ ] = 0; _sprite.faces[ i++ ] = 1; _sprite.faces[ i++ ] = 2;
  9462. _sprite.faces[ i++ ] = 0; _sprite.faces[ i++ ] = 2; _sprite.faces[ i++ ] = 3;
  9463. _sprite.vertexBuffer = _gl.createBuffer();
  9464. _sprite.elementBuffer = _gl.createBuffer();
  9465. _gl.bindBuffer( _gl.ARRAY_BUFFER, _sprite.vertexBuffer );
  9466. _gl.bufferData( _gl.ARRAY_BUFFER, _sprite.vertices, _gl.STATIC_DRAW );
  9467. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _sprite.elementBuffer );
  9468. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, _sprite.faces, _gl.STATIC_DRAW );
  9469. _sprite.program = createProgram( THREE.ShaderSprite[ "sprite" ] );
  9470. _sprite.attributes = {};
  9471. _sprite.uniforms = {};
  9472. _sprite.attributes.position = _gl.getAttribLocation ( _sprite.program, "position" );
  9473. _sprite.attributes.uv = _gl.getAttribLocation ( _sprite.program, "uv" );
  9474. _sprite.uniforms.uvOffset = _gl.getUniformLocation( _sprite.program, "uvOffset" );
  9475. _sprite.uniforms.uvScale = _gl.getUniformLocation( _sprite.program, "uvScale" );
  9476. _sprite.uniforms.rotation = _gl.getUniformLocation( _sprite.program, "rotation" );
  9477. _sprite.uniforms.scale = _gl.getUniformLocation( _sprite.program, "scale" );
  9478. _sprite.uniforms.alignment = _gl.getUniformLocation( _sprite.program, "alignment" );
  9479. _sprite.uniforms.color = _gl.getUniformLocation( _sprite.program, "color" );
  9480. _sprite.uniforms.map = _gl.getUniformLocation( _sprite.program, "map" );
  9481. _sprite.uniforms.opacity = _gl.getUniformLocation( _sprite.program, "opacity" );
  9482. _sprite.uniforms.useScreenCoordinates = _gl.getUniformLocation( _sprite.program, "useScreenCoordinates" );
  9483. _sprite.uniforms.affectedByDistance = _gl.getUniformLocation( _sprite.program, "affectedByDistance" );
  9484. _sprite.uniforms.screenPosition = _gl.getUniformLocation( _sprite.program, "screenPosition" );
  9485. _sprite.uniforms.modelViewMatrix = _gl.getUniformLocation( _sprite.program, "modelViewMatrix" );
  9486. _sprite.uniforms.projectionMatrix = _gl.getUniformLocation( _sprite.program, "projectionMatrix" );
  9487. _sprite.attributesEnabled = false;
  9488. };
  9489. this.render = function ( scene, camera, viewportWidth, viewportHeight ) {
  9490. var sprites = scene.__webglSprites,
  9491. nSprites = sprites.length;
  9492. if ( ! nSprites ) return;
  9493. var attributes = _sprite.attributes,
  9494. uniforms = _sprite.uniforms;
  9495. var invAspect = viewportHeight / viewportWidth;
  9496. var halfViewportWidth = viewportWidth * 0.5,
  9497. halfViewportHeight = viewportHeight * 0.5;
  9498. var mergeWith3D = true;
  9499. // setup gl
  9500. _gl.useProgram( _sprite.program );
  9501. if ( ! _sprite.attributesEnabled ) {
  9502. _gl.enableVertexAttribArray( attributes.position );
  9503. _gl.enableVertexAttribArray( attributes.uv );
  9504. _sprite.attributesEnabled = true;
  9505. }
  9506. _gl.disable( _gl.CULL_FACE );
  9507. _gl.enable( _gl.BLEND );
  9508. _gl.depthMask( true );
  9509. _gl.bindBuffer( _gl.ARRAY_BUFFER, _sprite.vertexBuffer );
  9510. _gl.vertexAttribPointer( attributes.position, 2, _gl.FLOAT, false, 2 * 8, 0 );
  9511. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 2 * 8, 8 );
  9512. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, _sprite.elementBuffer );
  9513. _gl.uniformMatrix4fv( uniforms.projectionMatrix, false, camera._projectionMatrixArray );
  9514. _gl.activeTexture( _gl.TEXTURE0 );
  9515. _gl.uniform1i( uniforms.map, 0 );
  9516. // update positions and sort
  9517. var i, sprite, screenPosition, size, scale = [];
  9518. for( i = 0; i < nSprites; i ++ ) {
  9519. sprite = sprites[ i ];
  9520. if ( ! sprite.visible || sprite.opacity === 0 ) continue;
  9521. if( ! sprite.useScreenCoordinates ) {
  9522. sprite._modelViewMatrix.multiply( camera.matrixWorldInverse, sprite.matrixWorld);
  9523. sprite.z = - sprite._modelViewMatrix.elements[14];
  9524. } else {
  9525. sprite.z = - sprite.position.z;
  9526. }
  9527. }
  9528. sprites.sort( painterSort );
  9529. // render all sprites
  9530. for( i = 0; i < nSprites; i ++ ) {
  9531. sprite = sprites[ i ];
  9532. if ( ! sprite.visible || sprite.opacity === 0 ) continue;
  9533. if ( sprite.map && sprite.map.image && sprite.map.image.width ) {
  9534. if ( sprite.useScreenCoordinates ) {
  9535. _gl.uniform1i( uniforms.useScreenCoordinates, 1 );
  9536. _gl.uniform3f( uniforms.screenPosition, ( sprite.position.x - halfViewportWidth ) / halfViewportWidth,
  9537. ( halfViewportHeight - sprite.position.y ) / halfViewportHeight,
  9538. Math.max( 0, Math.min( 1, sprite.position.z ) ) );
  9539. } else {
  9540. _gl.uniform1i( uniforms.useScreenCoordinates, 0 );
  9541. _gl.uniform1i( uniforms.affectedByDistance, sprite.affectedByDistance ? 1 : 0 );
  9542. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, sprite._modelViewMatrix.elements);
  9543. }
  9544. size = sprite.map.image.width / ( sprite.scaleByViewport ? viewportHeight : 1 );
  9545. scale[ 0 ] = size * invAspect * sprite.scale.x;
  9546. scale[ 1 ] = size * sprite.scale.y;
  9547. _gl.uniform2f( uniforms.uvScale, sprite.uvScale.x, sprite.uvScale.y );
  9548. _gl.uniform2f( uniforms.uvOffset, sprite.uvOffset.x, sprite.uvOffset.y );
  9549. _gl.uniform2f( uniforms.alignment, sprite.alignment.x, sprite.alignment.y );
  9550. _gl.uniform1f( uniforms.opacity, sprite.opacity );
  9551. _gl.uniform3f( uniforms.color, sprite.color.r, sprite.color.g, sprite.color.b );
  9552. _gl.uniform1f( uniforms.rotation, sprite.rotation );
  9553. _gl.uniform2fv( uniforms.scale, scale );
  9554. if ( sprite.mergeWith3D && !mergeWith3D ) {
  9555. _gl.enable( _gl.DEPTH_TEST );
  9556. mergeWith3D = true;
  9557. } else if ( ! sprite.mergeWith3D && mergeWith3D ) {
  9558. _gl.disable( _gl.DEPTH_TEST );
  9559. mergeWith3D = false;
  9560. }
  9561. _renderer.setBlending( sprite.blending, sprite.blendEquation, sprite.blendSrc, sprite.blendDst );
  9562. _renderer.setTexture( sprite.map, 0 );
  9563. _gl.drawElements( _gl.TRIANGLES, 6, _gl.UNSIGNED_SHORT, 0 );
  9564. }
  9565. }
  9566. // restore gl
  9567. _gl.enable( _gl.CULL_FACE );
  9568. _gl.enable( _gl.DEPTH_TEST );
  9569. _gl.depthMask( true );
  9570. };
  9571. function createProgram ( shader ) {
  9572. var program = _gl.createProgram();
  9573. var fragmentShader = _gl.createShader( _gl.FRAGMENT_SHADER );
  9574. var vertexShader = _gl.createShader( _gl.VERTEX_SHADER );
  9575. _gl.shaderSource( fragmentShader, shader.fragmentShader );
  9576. _gl.shaderSource( vertexShader, shader.vertexShader );
  9577. _gl.compileShader( fragmentShader );
  9578. _gl.compileShader( vertexShader );
  9579. _gl.attachShader( program, fragmentShader );
  9580. _gl.attachShader( program, vertexShader );
  9581. _gl.linkProgram( program );
  9582. return program;
  9583. };
  9584. function painterSort ( a, b ) {
  9585. return b.z - a.z;
  9586. };
  9587. };/**
  9588. * @author mikael emtinger / http://gomo.se/
  9589. *
  9590. */
  9591. THREE.ShaderFlares = {
  9592. 'lensFlareVertexTexture': {
  9593. vertexShader: [
  9594. "uniform vec3 screenPosition;",
  9595. "uniform vec2 scale;",
  9596. "uniform float rotation;",
  9597. "uniform int renderType;",
  9598. "uniform sampler2D occlusionMap;",
  9599. "attribute vec2 position;",
  9600. "attribute vec2 uv;",
  9601. "varying vec2 vUV;",
  9602. "varying float vVisibility;",
  9603. "void main() {",
  9604. "vUV = uv;",
  9605. "vec2 pos = position;",
  9606. "if( renderType == 2 ) {",
  9607. "vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) ) +",
  9608. "texture2D( occlusionMap, vec2( 0.5, 0.1 ) ) +",
  9609. "texture2D( occlusionMap, vec2( 0.9, 0.1 ) ) +",
  9610. "texture2D( occlusionMap, vec2( 0.9, 0.5 ) ) +",
  9611. "texture2D( occlusionMap, vec2( 0.9, 0.9 ) ) +",
  9612. "texture2D( occlusionMap, vec2( 0.5, 0.9 ) ) +",
  9613. "texture2D( occlusionMap, vec2( 0.1, 0.9 ) ) +",
  9614. "texture2D( occlusionMap, vec2( 0.1, 0.5 ) ) +",
  9615. "texture2D( occlusionMap, vec2( 0.5, 0.5 ) );",
  9616. "vVisibility = ( visibility.r / 9.0 ) *",
  9617. "( 1.0 - visibility.g / 9.0 ) *",
  9618. "( visibility.b / 9.0 ) *",
  9619. "( 1.0 - visibility.a / 9.0 );",
  9620. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  9621. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  9622. "}",
  9623. "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  9624. "}"
  9625. ].join( "\n" ),
  9626. fragmentShader: [
  9627. "precision mediump float;",
  9628. "uniform sampler2D map;",
  9629. "uniform float opacity;",
  9630. "uniform int renderType;",
  9631. "uniform vec3 color;",
  9632. "varying vec2 vUV;",
  9633. "varying float vVisibility;",
  9634. "void main() {",
  9635. // pink square
  9636. "if( renderType == 0 ) {",
  9637. "gl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );",
  9638. // restore
  9639. "} else if( renderType == 1 ) {",
  9640. "gl_FragColor = texture2D( map, vUV );",
  9641. // flare
  9642. "} else {",
  9643. "vec4 texture = texture2D( map, vUV );",
  9644. "texture.a *= opacity * vVisibility;",
  9645. "gl_FragColor = texture;",
  9646. "gl_FragColor.rgb *= color;",
  9647. "}",
  9648. "}"
  9649. ].join( "\n" )
  9650. },
  9651. 'lensFlare': {
  9652. vertexShader: [
  9653. "uniform vec3 screenPosition;",
  9654. "uniform vec2 scale;",
  9655. "uniform float rotation;",
  9656. "uniform int renderType;",
  9657. "attribute vec2 position;",
  9658. "attribute vec2 uv;",
  9659. "varying vec2 vUV;",
  9660. "void main() {",
  9661. "vUV = uv;",
  9662. "vec2 pos = position;",
  9663. "if( renderType == 2 ) {",
  9664. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  9665. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  9666. "}",
  9667. "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  9668. "}"
  9669. ].join( "\n" ),
  9670. fragmentShader: [
  9671. "precision mediump float;",
  9672. "uniform sampler2D map;",
  9673. "uniform sampler2D occlusionMap;",
  9674. "uniform float opacity;",
  9675. "uniform int renderType;",
  9676. "uniform vec3 color;",
  9677. "varying vec2 vUV;",
  9678. "void main() {",
  9679. // pink square
  9680. "if( renderType == 0 ) {",
  9681. "gl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );",
  9682. // restore
  9683. "} else if( renderType == 1 ) {",
  9684. "gl_FragColor = texture2D( map, vUV );",
  9685. // flare
  9686. "} else {",
  9687. "float visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 ) ).a +",
  9688. "texture2D( occlusionMap, vec2( 0.9, 0.5 ) ).a +",
  9689. "texture2D( occlusionMap, vec2( 0.5, 0.9 ) ).a +",
  9690. "texture2D( occlusionMap, vec2( 0.1, 0.5 ) ).a;",
  9691. "visibility = ( 1.0 - visibility / 4.0 );",
  9692. "vec4 texture = texture2D( map, vUV );",
  9693. "texture.a *= opacity * visibility;",
  9694. "gl_FragColor = texture;",
  9695. "gl_FragColor.rgb *= color;",
  9696. "}",
  9697. "}"
  9698. ].join( "\n" )
  9699. }
  9700. };
  9701. /**
  9702. * @author mikael emtinger / http://gomo.se/
  9703. *
  9704. */
  9705. THREE.ShaderSprite = {
  9706. 'sprite': {
  9707. vertexShader: [
  9708. "uniform int useScreenCoordinates;",
  9709. "uniform int affectedByDistance;",
  9710. "uniform vec3 screenPosition;",
  9711. "uniform mat4 modelViewMatrix;",
  9712. "uniform mat4 projectionMatrix;",
  9713. "uniform float rotation;",
  9714. "uniform vec2 scale;",
  9715. "uniform vec2 alignment;",
  9716. "uniform vec2 uvOffset;",
  9717. "uniform vec2 uvScale;",
  9718. "attribute vec2 position;",
  9719. "attribute vec2 uv;",
  9720. "varying vec2 vUV;",
  9721. "void main() {",
  9722. "vUV = uvOffset + uv * uvScale;",
  9723. "vec2 alignedPosition = position + alignment;",
  9724. "vec2 rotatedPosition;",
  9725. "rotatedPosition.x = ( cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y ) * scale.x;",
  9726. "rotatedPosition.y = ( sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y ) * scale.y;",
  9727. "vec4 finalPosition;",
  9728. "if( useScreenCoordinates != 0 ) {",
  9729. "finalPosition = vec4( screenPosition.xy + rotatedPosition, screenPosition.z, 1.0 );",
  9730. "} else {",
  9731. "finalPosition = projectionMatrix * modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );",
  9732. "finalPosition.xy += rotatedPosition * ( affectedByDistance == 1 ? 1.0 : finalPosition.z );",
  9733. "}",
  9734. "gl_Position = finalPosition;",
  9735. "}"
  9736. ].join( "\n" ),
  9737. fragmentShader: [
  9738. "precision mediump float;",
  9739. "uniform vec3 color;",
  9740. "uniform sampler2D map;",
  9741. "uniform float opacity;",
  9742. "varying vec2 vUV;",
  9743. "void main() {",
  9744. "vec4 texture = texture2D( map, vUV );",
  9745. "gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );",
  9746. "}"
  9747. ].join( "\n" )
  9748. }
  9749. };
粤ICP备19079148号