three.js 786 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831148321483314834148351483614837148381483914840148411484214843148441484514846148471484814849148501485114852148531485414855148561485714858148591486014861148621486314864148651486614867148681486914870148711487214873148741487514876148771487814879148801488114882148831488414885148861488714888148891489014891148921489314894148951489614897148981489914900149011490214903149041490514906149071490814909149101491114912149131491414915149161491714918149191492014921149221492314924149251492614927149281492914930149311493214933149341493514936149371493814939149401494114942149431494414945149461494714948149491495014951149521495314954149551495614957149581495914960149611496214963149641496514966149671496814969149701497114972149731497414975149761497714978149791498014981149821498314984149851498614987149881498914990149911499214993149941499514996149971499814999150001500115002150031500415005150061500715008150091501015011150121501315014150151501615017150181501915020150211502215023150241502515026150271502815029150301503115032150331503415035150361503715038150391504015041150421504315044150451504615047150481504915050150511505215053150541505515056150571505815059150601506115062150631506415065150661506715068150691507015071150721507315074150751507615077150781507915080150811508215083150841508515086150871508815089150901509115092150931509415095150961509715098150991510015101151021510315104151051510615107151081510915110151111511215113151141511515116151171511815119151201512115122151231512415125151261512715128151291513015131151321513315134151351513615137151381513915140151411514215143151441514515146151471514815149151501515115152151531515415155151561515715158151591516015161151621516315164151651516615167151681516915170151711517215173151741517515176151771517815179151801518115182151831518415185151861518715188151891519015191151921519315194151951519615197151981519915200152011520215203152041520515206152071520815209152101521115212152131521415215152161521715218152191522015221152221522315224152251522615227152281522915230152311523215233152341523515236152371523815239152401524115242152431524415245152461524715248152491525015251152521525315254152551525615257152581525915260152611526215263152641526515266152671526815269152701527115272152731527415275152761527715278152791528015281152821528315284152851528615287152881528915290152911529215293152941529515296152971529815299153001530115302153031530415305153061530715308153091531015311153121531315314153151531615317153181531915320153211532215323153241532515326153271532815329153301533115332153331533415335153361533715338153391534015341153421534315344153451534615347153481534915350153511535215353153541535515356153571535815359153601536115362153631536415365153661536715368153691537015371153721537315374153751537615377153781537915380153811538215383153841538515386153871538815389153901539115392153931539415395153961539715398153991540015401154021540315404154051540615407154081540915410154111541215413154141541515416154171541815419154201542115422154231542415425154261542715428154291543015431154321543315434154351543615437154381543915440154411544215443154441544515446154471544815449154501545115452154531545415455154561545715458154591546015461154621546315464154651546615467154681546915470154711547215473154741547515476154771547815479154801548115482154831548415485154861548715488154891549015491154921549315494154951549615497154981549915500155011550215503155041550515506155071550815509155101551115512155131551415515155161551715518155191552015521155221552315524155251552615527155281552915530155311553215533155341553515536155371553815539155401554115542155431554415545155461554715548155491555015551155521555315554155551555615557155581555915560155611556215563155641556515566155671556815569155701557115572155731557415575155761557715578155791558015581155821558315584155851558615587155881558915590155911559215593155941559515596155971559815599156001560115602156031560415605156061560715608156091561015611156121561315614156151561615617156181561915620156211562215623156241562515626156271562815629156301563115632156331563415635156361563715638156391564015641156421564315644156451564615647156481564915650156511565215653156541565515656156571565815659156601566115662156631566415665156661566715668156691567015671156721567315674156751567615677156781567915680156811568215683156841568515686156871568815689156901569115692156931569415695156961569715698156991570015701157021570315704157051570615707157081570915710157111571215713157141571515716157171571815719157201572115722157231572415725157261572715728157291573015731157321573315734157351573615737157381573915740157411574215743157441574515746157471574815749157501575115752157531575415755157561575715758157591576015761157621576315764157651576615767157681576915770157711577215773157741577515776157771577815779157801578115782157831578415785157861578715788157891579015791157921579315794157951579615797157981579915800158011580215803158041580515806158071580815809158101581115812158131581415815158161581715818158191582015821158221582315824158251582615827158281582915830158311583215833158341583515836158371583815839158401584115842158431584415845158461584715848158491585015851158521585315854158551585615857158581585915860158611586215863158641586515866158671586815869158701587115872158731587415875158761587715878158791588015881158821588315884158851588615887158881588915890158911589215893158941589515896158971589815899159001590115902159031590415905159061590715908159091591015911159121591315914159151591615917159181591915920159211592215923159241592515926159271592815929159301593115932159331593415935159361593715938159391594015941159421594315944159451594615947159481594915950159511595215953159541595515956159571595815959159601596115962159631596415965159661596715968159691597015971159721597315974159751597615977159781597915980159811598215983159841598515986159871598815989159901599115992159931599415995159961599715998159991600016001160021600316004160051600616007160081600916010160111601216013160141601516016160171601816019160201602116022160231602416025160261602716028160291603016031160321603316034160351603616037160381603916040160411604216043160441604516046160471604816049160501605116052160531605416055160561605716058160591606016061160621606316064160651606616067160681606916070160711607216073160741607516076160771607816079160801608116082160831608416085160861608716088160891609016091160921609316094160951609616097160981609916100161011610216103161041610516106161071610816109161101611116112161131611416115161161611716118161191612016121161221612316124161251612616127161281612916130161311613216133161341613516136161371613816139161401614116142161431614416145161461614716148161491615016151161521615316154161551615616157161581615916160161611616216163161641616516166161671616816169161701617116172161731617416175161761617716178161791618016181161821618316184161851618616187161881618916190161911619216193161941619516196161971619816199162001620116202162031620416205162061620716208162091621016211162121621316214162151621616217162181621916220162211622216223162241622516226162271622816229162301623116232162331623416235162361623716238162391624016241162421624316244162451624616247162481624916250162511625216253162541625516256162571625816259162601626116262162631626416265162661626716268162691627016271162721627316274162751627616277162781627916280162811628216283162841628516286162871628816289162901629116292162931629416295162961629716298162991630016301163021630316304163051630616307163081630916310163111631216313163141631516316163171631816319163201632116322163231632416325163261632716328163291633016331163321633316334163351633616337163381633916340163411634216343163441634516346163471634816349163501635116352163531635416355163561635716358163591636016361163621636316364163651636616367163681636916370163711637216373163741637516376163771637816379163801638116382163831638416385163861638716388163891639016391163921639316394163951639616397163981639916400164011640216403164041640516406164071640816409164101641116412164131641416415164161641716418164191642016421164221642316424164251642616427164281642916430164311643216433164341643516436164371643816439164401644116442164431644416445164461644716448164491645016451164521645316454164551645616457164581645916460164611646216463164641646516466164671646816469164701647116472164731647416475164761647716478164791648016481164821648316484164851648616487164881648916490164911649216493164941649516496164971649816499165001650116502165031650416505165061650716508165091651016511165121651316514165151651616517165181651916520165211652216523165241652516526165271652816529165301653116532165331653416535165361653716538165391654016541165421654316544165451654616547165481654916550165511655216553165541655516556165571655816559165601656116562165631656416565165661656716568165691657016571165721657316574165751657616577165781657916580165811658216583165841658516586165871658816589165901659116592165931659416595165961659716598165991660016601166021660316604166051660616607166081660916610166111661216613166141661516616166171661816619166201662116622166231662416625166261662716628166291663016631166321663316634166351663616637166381663916640166411664216643166441664516646166471664816649166501665116652166531665416655166561665716658166591666016661166621666316664166651666616667166681666916670166711667216673166741667516676166771667816679166801668116682166831668416685166861668716688166891669016691166921669316694166951669616697166981669916700167011670216703167041670516706167071670816709167101671116712167131671416715167161671716718167191672016721167221672316724167251672616727167281672916730167311673216733167341673516736167371673816739167401674116742167431674416745167461674716748167491675016751167521675316754167551675616757167581675916760167611676216763167641676516766167671676816769167701677116772167731677416775167761677716778167791678016781167821678316784167851678616787167881678916790167911679216793167941679516796167971679816799168001680116802168031680416805168061680716808168091681016811168121681316814168151681616817168181681916820168211682216823168241682516826168271682816829168301683116832168331683416835168361683716838168391684016841168421684316844168451684616847168481684916850168511685216853168541685516856168571685816859168601686116862168631686416865168661686716868168691687016871168721687316874168751687616877168781687916880168811688216883168841688516886168871688816889168901689116892168931689416895168961689716898168991690016901169021690316904169051690616907169081690916910169111691216913169141691516916169171691816919169201692116922169231692416925169261692716928169291693016931169321693316934169351693616937169381693916940169411694216943169441694516946169471694816949169501695116952169531695416955169561695716958169591696016961169621696316964169651696616967169681696916970169711697216973169741697516976169771697816979169801698116982169831698416985169861698716988169891699016991169921699316994169951699616997169981699917000170011700217003170041700517006170071700817009170101701117012170131701417015170161701717018170191702017021170221702317024170251702617027170281702917030170311703217033170341703517036170371703817039170401704117042170431704417045170461704717048170491705017051170521705317054170551705617057170581705917060170611706217063170641706517066170671706817069170701707117072170731707417075170761707717078170791708017081170821708317084170851708617087170881708917090170911709217093170941709517096170971709817099171001710117102171031710417105171061710717108171091711017111171121711317114171151711617117171181711917120171211712217123171241712517126171271712817129171301713117132171331713417135171361713717138171391714017141171421714317144171451714617147171481714917150171511715217153171541715517156171571715817159171601716117162171631716417165171661716717168171691717017171171721717317174171751717617177171781717917180171811718217183171841718517186171871718817189171901719117192171931719417195171961719717198171991720017201172021720317204172051720617207172081720917210172111721217213172141721517216172171721817219172201722117222172231722417225172261722717228172291723017231172321723317234172351723617237172381723917240172411724217243172441724517246172471724817249172501725117252172531725417255172561725717258172591726017261172621726317264172651726617267172681726917270172711727217273172741727517276172771727817279172801728117282172831728417285172861728717288172891729017291172921729317294172951729617297172981729917300173011730217303173041730517306173071730817309173101731117312173131731417315173161731717318173191732017321173221732317324173251732617327173281732917330173311733217333173341733517336173371733817339173401734117342173431734417345173461734717348173491735017351173521735317354173551735617357173581735917360173611736217363173641736517366173671736817369173701737117372173731737417375173761737717378173791738017381173821738317384173851738617387173881738917390173911739217393173941739517396173971739817399174001740117402174031740417405174061740717408174091741017411174121741317414174151741617417174181741917420174211742217423174241742517426174271742817429174301743117432174331743417435174361743717438174391744017441174421744317444174451744617447174481744917450174511745217453174541745517456174571745817459174601746117462174631746417465174661746717468174691747017471174721747317474174751747617477174781747917480174811748217483174841748517486174871748817489174901749117492174931749417495174961749717498174991750017501175021750317504175051750617507175081750917510175111751217513175141751517516175171751817519175201752117522175231752417525175261752717528175291753017531175321753317534175351753617537175381753917540175411754217543175441754517546175471754817549175501755117552175531755417555175561755717558175591756017561175621756317564175651756617567175681756917570175711757217573175741757517576175771757817579175801758117582175831758417585175861758717588175891759017591175921759317594175951759617597175981759917600176011760217603176041760517606176071760817609176101761117612176131761417615176161761717618176191762017621176221762317624176251762617627176281762917630176311763217633176341763517636176371763817639176401764117642176431764417645176461764717648176491765017651176521765317654176551765617657176581765917660176611766217663176641766517666176671766817669176701767117672176731767417675176761767717678176791768017681176821768317684176851768617687176881768917690176911769217693176941769517696176971769817699177001770117702177031770417705177061770717708177091771017711177121771317714177151771617717177181771917720177211772217723177241772517726177271772817729177301773117732177331773417735177361773717738177391774017741177421774317744177451774617747177481774917750177511775217753177541775517756177571775817759177601776117762177631776417765177661776717768177691777017771177721777317774177751777617777177781777917780177811778217783177841778517786177871778817789177901779117792177931779417795177961779717798177991780017801178021780317804178051780617807178081780917810178111781217813178141781517816178171781817819178201782117822178231782417825178261782717828178291783017831178321783317834178351783617837178381783917840178411784217843178441784517846178471784817849178501785117852178531785417855178561785717858178591786017861178621786317864178651786617867178681786917870178711787217873178741787517876178771787817879178801788117882178831788417885178861788717888178891789017891178921789317894178951789617897178981789917900179011790217903179041790517906179071790817909179101791117912179131791417915179161791717918179191792017921179221792317924179251792617927179281792917930179311793217933179341793517936179371793817939179401794117942179431794417945179461794717948179491795017951179521795317954179551795617957179581795917960179611796217963179641796517966179671796817969179701797117972179731797417975179761797717978179791798017981179821798317984179851798617987179881798917990179911799217993179941799517996179971799817999180001800118002180031800418005180061800718008180091801018011180121801318014180151801618017180181801918020180211802218023180241802518026180271802818029180301803118032180331803418035180361803718038180391804018041180421804318044180451804618047180481804918050180511805218053180541805518056180571805818059180601806118062180631806418065180661806718068180691807018071180721807318074180751807618077180781807918080180811808218083180841808518086180871808818089180901809118092180931809418095180961809718098180991810018101181021810318104181051810618107181081810918110181111811218113181141811518116181171811818119181201812118122181231812418125181261812718128181291813018131181321813318134181351813618137181381813918140181411814218143181441814518146181471814818149181501815118152181531815418155181561815718158181591816018161181621816318164181651816618167181681816918170181711817218173181741817518176181771817818179181801818118182181831818418185181861818718188181891819018191181921819318194181951819618197181981819918200182011820218203182041820518206182071820818209182101821118212182131821418215182161821718218182191822018221182221822318224182251822618227182281822918230182311823218233182341823518236182371823818239182401824118242182431824418245182461824718248182491825018251182521825318254182551825618257182581825918260182611826218263182641826518266182671826818269182701827118272182731827418275182761827718278182791828018281182821828318284182851828618287182881828918290182911829218293182941829518296182971829818299183001830118302183031830418305183061830718308183091831018311183121831318314183151831618317183181831918320183211832218323183241832518326183271832818329183301833118332183331833418335183361833718338183391834018341183421834318344183451834618347183481834918350183511835218353183541835518356183571835818359183601836118362183631836418365183661836718368183691837018371183721837318374183751837618377183781837918380183811838218383183841838518386183871838818389183901839118392183931839418395183961839718398183991840018401184021840318404184051840618407184081840918410184111841218413184141841518416184171841818419184201842118422184231842418425184261842718428184291843018431184321843318434184351843618437184381843918440184411844218443184441844518446184471844818449184501845118452184531845418455184561845718458184591846018461184621846318464184651846618467184681846918470184711847218473184741847518476184771847818479184801848118482184831848418485184861848718488184891849018491184921849318494184951849618497184981849918500185011850218503185041850518506185071850818509185101851118512185131851418515185161851718518185191852018521185221852318524185251852618527185281852918530185311853218533185341853518536185371853818539185401854118542185431854418545185461854718548185491855018551185521855318554185551855618557185581855918560185611856218563185641856518566185671856818569185701857118572185731857418575185761857718578185791858018581185821858318584185851858618587185881858918590185911859218593185941859518596185971859818599186001860118602186031860418605186061860718608186091861018611186121861318614186151861618617186181861918620186211862218623186241862518626186271862818629186301863118632186331863418635186361863718638186391864018641186421864318644186451864618647186481864918650186511865218653186541865518656186571865818659186601866118662186631866418665186661866718668186691867018671186721867318674186751867618677186781867918680186811868218683186841868518686186871868818689186901869118692186931869418695186961869718698186991870018701187021870318704187051870618707187081870918710187111871218713187141871518716187171871818719187201872118722187231872418725187261872718728187291873018731187321873318734187351873618737187381873918740187411874218743187441874518746187471874818749187501875118752187531875418755187561875718758187591876018761187621876318764187651876618767187681876918770187711877218773187741877518776187771877818779187801878118782187831878418785187861878718788187891879018791187921879318794187951879618797187981879918800188011880218803188041880518806188071880818809188101881118812188131881418815188161881718818188191882018821188221882318824188251882618827188281882918830188311883218833188341883518836188371883818839188401884118842188431884418845188461884718848188491885018851188521885318854188551885618857188581885918860188611886218863188641886518866188671886818869188701887118872188731887418875188761887718878188791888018881188821888318884188851888618887188881888918890188911889218893188941889518896188971889818899189001890118902189031890418905189061890718908189091891018911189121891318914189151891618917189181891918920189211892218923189241892518926189271892818929189301893118932189331893418935189361893718938189391894018941189421894318944189451894618947189481894918950189511895218953189541895518956189571895818959189601896118962189631896418965189661896718968189691897018971189721897318974189751897618977189781897918980189811898218983189841898518986189871898818989189901899118992189931899418995189961899718998189991900019001190021900319004190051900619007190081900919010190111901219013190141901519016190171901819019190201902119022190231902419025190261902719028190291903019031190321903319034190351903619037190381903919040190411904219043190441904519046190471904819049190501905119052190531905419055190561905719058190591906019061190621906319064190651906619067190681906919070190711907219073190741907519076190771907819079190801908119082190831908419085190861908719088190891909019091190921909319094190951909619097190981909919100191011910219103191041910519106191071910819109191101911119112191131911419115191161911719118191191912019121191221912319124191251912619127191281912919130191311913219133191341913519136191371913819139191401914119142191431914419145191461914719148191491915019151191521915319154191551915619157191581915919160191611916219163191641916519166191671916819169191701917119172191731917419175191761917719178191791918019181191821918319184191851918619187191881918919190191911919219193191941919519196191971919819199192001920119202192031920419205192061920719208192091921019211192121921319214192151921619217192181921919220192211922219223192241922519226192271922819229192301923119232192331923419235192361923719238192391924019241192421924319244192451924619247192481924919250192511925219253192541925519256192571925819259192601926119262192631926419265192661926719268192691927019271192721927319274192751927619277192781927919280192811928219283192841928519286192871928819289192901929119292192931929419295192961929719298192991930019301193021930319304193051930619307193081930919310193111931219313193141931519316193171931819319193201932119322193231932419325193261932719328193291933019331193321933319334193351933619337193381933919340193411934219343193441934519346193471934819349193501935119352193531935419355193561935719358193591936019361193621936319364193651936619367193681936919370193711937219373193741937519376193771937819379193801938119382193831938419385193861938719388193891939019391193921939319394193951939619397193981939919400194011940219403194041940519406194071940819409194101941119412194131941419415194161941719418194191942019421194221942319424194251942619427194281942919430194311943219433194341943519436194371943819439194401944119442194431944419445194461944719448194491945019451194521945319454194551945619457194581945919460194611946219463194641946519466194671946819469194701947119472194731947419475194761947719478194791948019481194821948319484194851948619487194881948919490194911949219493194941949519496194971949819499195001950119502195031950419505195061950719508195091951019511195121951319514195151951619517195181951919520195211952219523195241952519526195271952819529195301953119532195331953419535195361953719538195391954019541195421954319544195451954619547195481954919550195511955219553195541955519556195571955819559195601956119562195631956419565195661956719568195691957019571195721957319574195751957619577195781957919580195811958219583195841958519586195871958819589195901959119592195931959419595195961959719598195991960019601196021960319604196051960619607196081960919610196111961219613196141961519616196171961819619196201962119622196231962419625196261962719628196291963019631196321963319634196351963619637196381963919640196411964219643196441964519646196471964819649196501965119652196531965419655196561965719658196591966019661196621966319664196651966619667196681966919670196711967219673196741967519676196771967819679196801968119682196831968419685196861968719688196891969019691196921969319694196951969619697196981969919700197011970219703197041970519706197071970819709197101971119712197131971419715197161971719718197191972019721197221972319724197251972619727197281972919730197311973219733197341973519736197371973819739197401974119742197431974419745197461974719748197491975019751197521975319754197551975619757197581975919760197611976219763197641976519766197671976819769197701977119772197731977419775197761977719778197791978019781197821978319784197851978619787197881978919790197911979219793197941979519796197971979819799198001980119802198031980419805198061980719808198091981019811198121981319814198151981619817198181981919820198211982219823198241982519826198271982819829198301983119832198331983419835198361983719838198391984019841198421984319844198451984619847198481984919850198511985219853198541985519856198571985819859198601986119862198631986419865198661986719868198691987019871198721987319874198751987619877198781987919880198811988219883198841988519886198871988819889198901989119892198931989419895198961989719898198991990019901199021990319904199051990619907199081990919910199111991219913199141991519916199171991819919199201992119922199231992419925199261992719928199291993019931199321993319934199351993619937199381993919940199411994219943199441994519946199471994819949199501995119952199531995419955199561995719958199591996019961199621996319964199651996619967199681996919970199711997219973199741997519976199771997819979199801998119982199831998419985199861998719988199891999019991199921999319994199951999619997199981999920000200012000220003200042000520006200072000820009200102001120012200132001420015200162001720018200192002020021200222002320024200252002620027200282002920030200312003220033200342003520036200372003820039200402004120042200432004420045200462004720048200492005020051200522005320054200552005620057200582005920060200612006220063200642006520066200672006820069200702007120072200732007420075200762007720078200792008020081200822008320084200852008620087200882008920090200912009220093200942009520096200972009820099201002010120102201032010420105201062010720108201092011020111201122011320114201152011620117201182011920120201212012220123201242012520126201272012820129201302013120132201332013420135201362013720138201392014020141201422014320144201452014620147201482014920150201512015220153201542015520156201572015820159201602016120162201632016420165201662016720168201692017020171201722017320174201752017620177201782017920180201812018220183201842018520186201872018820189201902019120192201932019420195201962019720198201992020020201202022020320204202052020620207202082020920210202112021220213202142021520216202172021820219202202022120222202232022420225202262022720228202292023020231202322023320234202352023620237202382023920240202412024220243202442024520246202472024820249202502025120252202532025420255202562025720258202592026020261202622026320264202652026620267202682026920270202712027220273202742027520276202772027820279202802028120282202832028420285202862028720288202892029020291202922029320294202952029620297202982029920300203012030220303203042030520306203072030820309203102031120312203132031420315203162031720318203192032020321203222032320324203252032620327203282032920330203312033220333203342033520336203372033820339203402034120342203432034420345203462034720348203492035020351203522035320354203552035620357203582035920360203612036220363203642036520366203672036820369203702037120372203732037420375203762037720378203792038020381203822038320384203852038620387203882038920390203912039220393203942039520396203972039820399204002040120402204032040420405204062040720408204092041020411204122041320414204152041620417204182041920420204212042220423204242042520426204272042820429204302043120432204332043420435204362043720438204392044020441204422044320444204452044620447204482044920450204512045220453204542045520456204572045820459204602046120462204632046420465204662046720468204692047020471204722047320474204752047620477204782047920480204812048220483204842048520486204872048820489204902049120492204932049420495204962049720498204992050020501205022050320504205052050620507205082050920510205112051220513205142051520516205172051820519205202052120522205232052420525205262052720528205292053020531205322053320534205352053620537205382053920540205412054220543205442054520546205472054820549205502055120552205532055420555205562055720558205592056020561205622056320564205652056620567205682056920570205712057220573205742057520576205772057820579205802058120582205832058420585205862058720588205892059020591205922059320594205952059620597205982059920600206012060220603206042060520606206072060820609206102061120612206132061420615206162061720618206192062020621206222062320624206252062620627206282062920630206312063220633206342063520636206372063820639206402064120642206432064420645206462064720648206492065020651206522065320654206552065620657206582065920660206612066220663206642066520666206672066820669206702067120672206732067420675206762067720678206792068020681206822068320684206852068620687206882068920690206912069220693206942069520696206972069820699207002070120702207032070420705207062070720708207092071020711207122071320714207152071620717207182071920720207212072220723207242072520726207272072820729207302073120732207332073420735207362073720738207392074020741207422074320744207452074620747207482074920750207512075220753207542075520756207572075820759207602076120762207632076420765207662076720768207692077020771207722077320774207752077620777207782077920780207812078220783207842078520786207872078820789207902079120792207932079420795207962079720798207992080020801208022080320804208052080620807208082080920810208112081220813208142081520816208172081820819208202082120822208232082420825208262082720828208292083020831208322083320834208352083620837208382083920840208412084220843208442084520846208472084820849208502085120852208532085420855208562085720858208592086020861208622086320864208652086620867208682086920870208712087220873208742087520876208772087820879208802088120882208832088420885208862088720888208892089020891208922089320894208952089620897208982089920900209012090220903209042090520906209072090820909209102091120912209132091420915209162091720918209192092020921209222092320924209252092620927209282092920930209312093220933209342093520936209372093820939209402094120942209432094420945209462094720948209492095020951209522095320954209552095620957209582095920960209612096220963209642096520966209672096820969209702097120972209732097420975209762097720978209792098020981209822098320984209852098620987209882098920990209912099220993209942099520996209972099820999210002100121002210032100421005210062100721008210092101021011210122101321014210152101621017210182101921020210212102221023210242102521026210272102821029210302103121032210332103421035210362103721038210392104021041210422104321044210452104621047210482104921050210512105221053210542105521056210572105821059210602106121062210632106421065210662106721068210692107021071210722107321074210752107621077210782107921080210812108221083210842108521086210872108821089210902109121092210932109421095210962109721098210992110021101211022110321104211052110621107211082110921110211112111221113211142111521116211172111821119211202112121122211232112421125211262112721128211292113021131211322113321134211352113621137211382113921140211412114221143211442114521146211472114821149211502115121152211532115421155211562115721158211592116021161211622116321164211652116621167211682116921170211712117221173211742117521176211772117821179211802118121182211832118421185211862118721188211892119021191211922119321194211952119621197211982119921200212012120221203212042120521206212072120821209212102121121212212132121421215212162121721218212192122021221212222122321224212252122621227212282122921230212312123221233212342123521236212372123821239212402124121242212432124421245212462124721248212492125021251212522125321254212552125621257212582125921260212612126221263212642126521266212672126821269212702127121272212732127421275212762127721278212792128021281212822128321284212852128621287212882128921290212912129221293212942129521296212972129821299213002130121302213032130421305213062130721308213092131021311213122131321314213152131621317213182131921320213212132221323213242132521326213272132821329213302133121332213332133421335213362133721338213392134021341213422134321344213452134621347213482134921350213512135221353213542135521356213572135821359213602136121362213632136421365213662136721368213692137021371213722137321374213752137621377213782137921380213812138221383213842138521386213872138821389213902139121392213932139421395213962139721398213992140021401214022140321404214052140621407214082140921410214112141221413214142141521416214172141821419214202142121422214232142421425214262142721428214292143021431214322143321434214352143621437214382143921440214412144221443214442144521446214472144821449214502145121452214532145421455214562145721458214592146021461214622146321464214652146621467214682146921470214712147221473214742147521476214772147821479214802148121482214832148421485214862148721488214892149021491214922149321494214952149621497214982149921500215012150221503215042150521506215072150821509215102151121512215132151421515215162151721518215192152021521215222152321524215252152621527215282152921530215312153221533215342153521536215372153821539215402154121542215432154421545215462154721548215492155021551215522155321554215552155621557215582155921560215612156221563215642156521566215672156821569215702157121572215732157421575215762157721578215792158021581215822158321584215852158621587215882158921590215912159221593215942159521596215972159821599216002160121602216032160421605216062160721608216092161021611216122161321614216152161621617216182161921620216212162221623216242162521626216272162821629216302163121632216332163421635216362163721638216392164021641216422164321644216452164621647216482164921650216512165221653216542165521656216572165821659216602166121662216632166421665216662166721668216692167021671216722167321674216752167621677216782167921680216812168221683216842168521686216872168821689216902169121692216932169421695216962169721698216992170021701217022170321704217052170621707217082170921710217112171221713217142171521716217172171821719217202172121722217232172421725217262172721728217292173021731217322173321734217352173621737217382173921740217412174221743217442174521746217472174821749217502175121752217532175421755217562175721758217592176021761217622176321764217652176621767217682176921770217712177221773217742177521776217772177821779217802178121782217832178421785217862178721788217892179021791217922179321794217952179621797217982179921800218012180221803218042180521806218072180821809218102181121812218132181421815218162181721818218192182021821218222182321824218252182621827218282182921830218312183221833218342183521836218372183821839218402184121842218432184421845218462184721848218492185021851218522185321854218552185621857218582185921860218612186221863218642186521866218672186821869218702187121872218732187421875218762187721878218792188021881218822188321884218852188621887218882188921890218912189221893218942189521896218972189821899219002190121902219032190421905219062190721908219092191021911219122191321914219152191621917219182191921920219212192221923219242192521926219272192821929219302193121932219332193421935219362193721938219392194021941219422194321944219452194621947219482194921950219512195221953219542195521956219572195821959219602196121962219632196421965219662196721968219692197021971219722197321974219752197621977219782197921980219812198221983219842198521986219872198821989219902199121992219932199421995219962199721998219992200022001220022200322004220052200622007220082200922010220112201222013220142201522016220172201822019220202202122022220232202422025220262202722028220292203022031220322203322034220352203622037220382203922040220412204222043220442204522046220472204822049220502205122052220532205422055220562205722058220592206022061220622206322064220652206622067220682206922070220712207222073220742207522076220772207822079220802208122082220832208422085220862208722088220892209022091220922209322094220952209622097220982209922100221012210222103221042210522106221072210822109221102211122112221132211422115221162211722118221192212022121221222212322124221252212622127221282212922130221312213222133221342213522136221372213822139221402214122142221432214422145221462214722148221492215022151221522215322154221552215622157221582215922160221612216222163221642216522166221672216822169221702217122172221732217422175221762217722178221792218022181221822218322184221852218622187221882218922190221912219222193221942219522196221972219822199222002220122202222032220422205222062220722208222092221022211222122221322214222152221622217222182221922220222212222222223222242222522226222272222822229222302223122232222332223422235222362223722238222392224022241222422224322244222452224622247222482224922250222512225222253222542225522256222572225822259222602226122262222632226422265222662226722268222692227022271222722227322274222752227622277222782227922280222812228222283222842228522286222872228822289222902229122292222932229422295222962229722298222992230022301223022230322304223052230622307223082230922310223112231222313223142231522316223172231822319223202232122322223232232422325223262232722328223292233022331223322233322334223352233622337223382233922340223412234222343223442234522346223472234822349223502235122352223532235422355223562235722358223592236022361223622236322364223652236622367223682236922370223712237222373223742237522376223772237822379223802238122382223832238422385223862238722388223892239022391223922239322394223952239622397223982239922400224012240222403224042240522406224072240822409224102241122412224132241422415224162241722418224192242022421224222242322424224252242622427224282242922430224312243222433224342243522436224372243822439224402244122442224432244422445224462244722448224492245022451224522245322454224552245622457224582245922460224612246222463224642246522466224672246822469224702247122472224732247422475224762247722478224792248022481224822248322484224852248622487224882248922490224912249222493224942249522496224972249822499225002250122502225032250422505225062250722508225092251022511225122251322514225152251622517225182251922520225212252222523225242252522526225272252822529225302253122532225332253422535225362253722538225392254022541225422254322544225452254622547225482254922550225512255222553225542255522556225572255822559225602256122562225632256422565225662256722568225692257022571225722257322574225752257622577225782257922580225812258222583225842258522586225872258822589225902259122592225932259422595225962259722598225992260022601226022260322604226052260622607226082260922610226112261222613226142261522616226172261822619226202262122622226232262422625226262262722628226292263022631226322263322634226352263622637226382263922640226412264222643226442264522646226472264822649226502265122652226532265422655226562265722658226592266022661226622266322664226652266622667226682266922670226712267222673226742267522676226772267822679226802268122682226832268422685226862268722688226892269022691226922269322694226952269622697226982269922700227012270222703227042270522706227072270822709227102271122712227132271422715227162271722718227192272022721227222272322724227252272622727227282272922730227312273222733227342273522736227372273822739227402274122742227432274422745227462274722748227492275022751227522275322754227552275622757227582275922760227612276222763227642276522766227672276822769227702277122772227732277422775227762277722778227792278022781227822278322784227852278622787227882278922790227912279222793227942279522796227972279822799228002280122802228032280422805228062280722808228092281022811228122281322814228152281622817228182281922820228212282222823228242282522826228272282822829228302283122832228332283422835228362283722838228392284022841228422284322844228452284622847228482284922850228512285222853228542285522856228572285822859228602286122862228632286422865228662286722868228692287022871228722287322874228752287622877228782287922880228812288222883228842288522886228872288822889228902289122892228932289422895228962289722898228992290022901229022290322904229052290622907229082290922910229112291222913229142291522916229172291822919229202292122922229232292422925229262292722928229292293022931229322293322934229352293622937229382293922940229412294222943229442294522946229472294822949229502295122952229532295422955229562295722958229592296022961229622296322964229652296622967229682296922970229712297222973229742297522976229772297822979229802298122982229832298422985229862298722988229892299022991229922299322994229952299622997229982299923000230012300223003230042300523006230072300823009230102301123012230132301423015230162301723018230192302023021230222302323024230252302623027230282302923030230312303223033230342303523036230372303823039230402304123042230432304423045230462304723048230492305023051230522305323054230552305623057230582305923060230612306223063230642306523066230672306823069230702307123072230732307423075230762307723078230792308023081230822308323084230852308623087230882308923090230912309223093230942309523096230972309823099231002310123102231032310423105231062310723108231092311023111231122311323114231152311623117231182311923120231212312223123231242312523126231272312823129231302313123132231332313423135231362313723138231392314023141231422314323144231452314623147231482314923150231512315223153231542315523156231572315823159231602316123162231632316423165231662316723168231692317023171231722317323174231752317623177231782317923180231812318223183231842318523186231872318823189231902319123192231932319423195231962319723198231992320023201232022320323204232052320623207232082320923210232112321223213232142321523216232172321823219232202322123222232232322423225232262322723228232292323023231232322323323234232352323623237232382323923240232412324223243232442324523246232472324823249232502325123252232532325423255232562325723258232592326023261232622326323264232652326623267232682326923270232712327223273232742327523276232772327823279232802328123282232832328423285232862328723288232892329023291232922329323294232952329623297232982329923300233012330223303233042330523306233072330823309233102331123312233132331423315233162331723318233192332023321233222332323324233252332623327233282332923330233312333223333233342333523336233372333823339233402334123342233432334423345233462334723348233492335023351233522335323354233552335623357233582335923360233612336223363233642336523366233672336823369233702337123372233732337423375233762337723378233792338023381233822338323384233852338623387233882338923390233912339223393233942339523396233972339823399234002340123402234032340423405234062340723408234092341023411234122341323414234152341623417234182341923420234212342223423234242342523426234272342823429234302343123432234332343423435234362343723438234392344023441234422344323444234452344623447234482344923450234512345223453234542345523456234572345823459234602346123462234632346423465234662346723468234692347023471234722347323474234752347623477234782347923480234812348223483234842348523486234872348823489234902349123492234932349423495234962349723498234992350023501235022350323504235052350623507235082350923510235112351223513235142351523516235172351823519235202352123522235232352423525235262352723528235292353023531235322353323534235352353623537235382353923540235412354223543235442354523546235472354823549235502355123552235532355423555235562355723558235592356023561235622356323564235652356623567235682356923570235712357223573235742357523576235772357823579235802358123582235832358423585235862358723588235892359023591235922359323594235952359623597235982359923600236012360223603236042360523606236072360823609236102361123612236132361423615236162361723618236192362023621236222362323624236252362623627236282362923630236312363223633236342363523636236372363823639236402364123642236432364423645236462364723648236492365023651236522365323654236552365623657236582365923660236612366223663236642366523666236672366823669236702367123672236732367423675236762367723678236792368023681236822368323684236852368623687236882368923690236912369223693236942369523696236972369823699237002370123702237032370423705237062370723708237092371023711237122371323714237152371623717237182371923720237212372223723237242372523726237272372823729237302373123732237332373423735237362373723738237392374023741237422374323744237452374623747237482374923750237512375223753237542375523756237572375823759237602376123762237632376423765237662376723768237692377023771237722377323774237752377623777237782377923780237812378223783237842378523786237872378823789237902379123792237932379423795237962379723798237992380023801238022380323804238052380623807238082380923810238112381223813238142381523816238172381823819238202382123822238232382423825238262382723828238292383023831238322383323834238352383623837238382383923840238412384223843238442384523846238472384823849238502385123852238532385423855238562385723858238592386023861238622386323864238652386623867238682386923870238712387223873238742387523876238772387823879238802388123882238832388423885238862388723888238892389023891238922389323894238952389623897238982389923900239012390223903239042390523906239072390823909239102391123912239132391423915239162391723918239192392023921239222392323924239252392623927239282392923930239312393223933239342393523936239372393823939239402394123942239432394423945239462394723948239492395023951239522395323954239552395623957239582395923960239612396223963239642396523966239672396823969239702397123972239732397423975239762397723978239792398023981239822398323984239852398623987239882398923990239912399223993239942399523996239972399823999240002400124002240032400424005240062400724008240092401024011240122401324014240152401624017240182401924020240212402224023240242402524026240272402824029240302403124032240332403424035240362403724038240392404024041240422404324044240452404624047240482404924050240512405224053240542405524056240572405824059240602406124062240632406424065240662406724068240692407024071240722407324074240752407624077240782407924080240812408224083240842408524086240872408824089240902409124092240932409424095240962409724098240992410024101241022410324104241052410624107241082410924110241112411224113241142411524116241172411824119241202412124122241232412424125241262412724128241292413024131241322413324134241352413624137241382413924140241412414224143241442414524146241472414824149241502415124152241532415424155241562415724158241592416024161241622416324164241652416624167241682416924170241712417224173241742417524176241772417824179241802418124182241832418424185241862418724188241892419024191241922419324194241952419624197241982419924200242012420224203242042420524206242072420824209242102421124212242132421424215242162421724218242192422024221242222422324224242252422624227242282422924230242312423224233242342423524236242372423824239242402424124242242432424424245242462424724248242492425024251242522425324254242552425624257242582425924260242612426224263242642426524266242672426824269242702427124272242732427424275242762427724278242792428024281242822428324284242852428624287242882428924290242912429224293242942429524296242972429824299243002430124302243032430424305243062430724308243092431024311243122431324314243152431624317243182431924320243212432224323243242432524326243272432824329243302433124332243332433424335243362433724338243392434024341243422434324344243452434624347243482434924350243512435224353243542435524356243572435824359243602436124362243632436424365243662436724368243692437024371243722437324374243752437624377243782437924380243812438224383243842438524386243872438824389243902439124392243932439424395243962439724398243992440024401244022440324404244052440624407244082440924410244112441224413244142441524416244172441824419244202442124422244232442424425244262442724428244292443024431244322443324434244352443624437244382443924440244412444224443244442444524446244472444824449244502445124452244532445424455244562445724458244592446024461244622446324464244652446624467244682446924470244712447224473244742447524476244772447824479244802448124482244832448424485244862448724488244892449024491244922449324494244952449624497244982449924500245012450224503245042450524506245072450824509245102451124512245132451424515245162451724518245192452024521245222452324524245252452624527245282452924530245312453224533245342453524536245372453824539245402454124542245432454424545245462454724548245492455024551245522455324554245552455624557245582455924560245612456224563245642456524566245672456824569245702457124572245732457424575245762457724578245792458024581245822458324584245852458624587245882458924590245912459224593245942459524596245972459824599246002460124602246032460424605246062460724608246092461024611246122461324614246152461624617246182461924620246212462224623246242462524626246272462824629246302463124632246332463424635246362463724638246392464024641246422464324644246452464624647246482464924650246512465224653246542465524656246572465824659246602466124662246632466424665246662466724668246692467024671246722467324674246752467624677246782467924680246812468224683246842468524686246872468824689246902469124692246932469424695246962469724698246992470024701247022470324704247052470624707247082470924710247112471224713247142471524716247172471824719247202472124722247232472424725247262472724728247292473024731247322473324734247352473624737247382473924740247412474224743247442474524746247472474824749247502475124752247532475424755247562475724758247592476024761247622476324764247652476624767247682476924770247712477224773247742477524776247772477824779247802478124782247832478424785247862478724788247892479024791247922479324794247952479624797247982479924800248012480224803248042480524806248072480824809248102481124812248132481424815248162481724818248192482024821248222482324824248252482624827248282482924830248312483224833248342483524836248372483824839248402484124842248432484424845248462484724848248492485024851248522485324854248552485624857248582485924860248612486224863248642486524866248672486824869248702487124872248732487424875248762487724878248792488024881248822488324884248852488624887248882488924890248912489224893248942489524896248972489824899249002490124902249032490424905249062490724908249092491024911249122491324914249152491624917249182491924920249212492224923249242492524926249272492824929249302493124932249332493424935249362493724938249392494024941249422494324944249452494624947249482494924950249512495224953249542495524956249572495824959249602496124962249632496424965249662496724968249692497024971249722497324974249752497624977249782497924980249812498224983249842498524986249872498824989249902499124992249932499424995249962499724998249992500025001250022500325004250052500625007250082500925010250112501225013250142501525016250172501825019250202502125022250232502425025250262502725028250292503025031250322503325034250352503625037250382503925040250412504225043250442504525046250472504825049250502505125052250532505425055250562505725058250592506025061250622506325064250652506625067250682506925070250712507225073250742507525076250772507825079250802508125082250832508425085250862508725088250892509025091250922509325094250952509625097250982509925100251012510225103251042510525106251072510825109251102511125112251132511425115251162511725118251192512025121251222512325124251252512625127251282512925130251312513225133251342513525136251372513825139251402514125142251432514425145251462514725148251492515025151251522515325154251552515625157251582515925160251612516225163251642516525166251672516825169251702517125172251732517425175251762517725178251792518025181251822518325184251852518625187251882518925190251912519225193251942519525196251972519825199252002520125202252032520425205252062520725208252092521025211252122521325214252152521625217252182521925220252212522225223252242522525226252272522825229252302523125232252332523425235252362523725238252392524025241252422524325244252452524625247252482524925250252512525225253252542525525256252572525825259252602526125262252632526425265252662526725268252692527025271252722527325274252752527625277252782527925280252812528225283252842528525286252872528825289252902529125292252932529425295252962529725298252992530025301253022530325304253052530625307253082530925310253112531225313253142531525316253172531825319253202532125322253232532425325253262532725328253292533025331253322533325334253352533625337253382533925340253412534225343253442534525346253472534825349253502535125352253532535425355253562535725358253592536025361253622536325364253652536625367253682536925370253712537225373253742537525376253772537825379253802538125382253832538425385253862538725388253892539025391253922539325394253952539625397253982539925400254012540225403254042540525406254072540825409254102541125412254132541425415254162541725418254192542025421254222542325424254252542625427254282542925430254312543225433254342543525436254372543825439254402544125442254432544425445254462544725448254492545025451254522545325454254552545625457254582545925460254612546225463254642546525466254672546825469254702547125472254732547425475254762547725478254792548025481254822548325484254852548625487254882548925490254912549225493254942549525496254972549825499255002550125502255032550425505255062550725508255092551025511255122551325514255152551625517255182551925520255212552225523255242552525526255272552825529255302553125532255332553425535255362553725538255392554025541255422554325544255452554625547255482554925550255512555225553255542555525556255572555825559255602556125562255632556425565255662556725568255692557025571255722557325574255752557625577255782557925580255812558225583255842558525586255872558825589255902559125592255932559425595255962559725598255992560025601256022560325604256052560625607256082560925610256112561225613256142561525616256172561825619256202562125622256232562425625256262562725628256292563025631256322563325634256352563625637256382563925640256412564225643256442564525646256472564825649256502565125652256532565425655256562565725658256592566025661256622566325664256652566625667256682566925670256712567225673256742567525676256772567825679256802568125682256832568425685256862568725688256892569025691256922569325694256952569625697256982569925700257012570225703257042570525706257072570825709257102571125712257132571425715257162571725718257192572025721257222572325724257252572625727257282572925730257312573225733257342573525736257372573825739257402574125742257432574425745257462574725748257492575025751257522575325754257552575625757257582575925760257612576225763257642576525766257672576825769257702577125772257732577425775257762577725778257792578025781257822578325784257852578625787257882578925790257912579225793257942579525796257972579825799258002580125802258032580425805258062580725808258092581025811258122581325814258152581625817258182581925820258212582225823258242582525826258272582825829258302583125832258332583425835258362583725838258392584025841258422584325844258452584625847258482584925850258512585225853258542585525856258572585825859258602586125862258632586425865258662586725868258692587025871258722587325874258752587625877258782587925880258812588225883258842588525886258872588825889258902589125892258932589425895258962589725898258992590025901259022590325904259052590625907259082590925910259112591225913259142591525916259172591825919259202592125922259232592425925259262592725928259292593025931259322593325934259352593625937259382593925940259412594225943259442594525946259472594825949259502595125952259532595425955259562595725958259592596025961259622596325964259652596625967259682596925970259712597225973259742597525976259772597825979259802598125982259832598425985259862598725988259892599025991259922599325994259952599625997259982599926000260012600226003260042600526006260072600826009260102601126012260132601426015260162601726018260192602026021260222602326024260252602626027260282602926030260312603226033260342603526036260372603826039260402604126042260432604426045260462604726048260492605026051260522605326054260552605626057260582605926060260612606226063260642606526066260672606826069260702607126072260732607426075260762607726078260792608026081260822608326084260852608626087260882608926090260912609226093260942609526096260972609826099261002610126102261032610426105261062610726108261092611026111261122611326114261152611626117261182611926120261212612226123261242612526126261272612826129261302613126132261332613426135261362613726138261392614026141261422614326144261452614626147261482614926150261512615226153261542615526156261572615826159261602616126162261632616426165261662616726168261692617026171261722617326174261752617626177261782617926180261812618226183261842618526186261872618826189261902619126192261932619426195261962619726198261992620026201262022620326204262052620626207262082620926210262112621226213262142621526216262172621826219262202622126222262232622426225262262622726228262292623026231262322623326234262352623626237262382623926240262412624226243262442624526246262472624826249262502625126252262532625426255262562625726258262592626026261262622626326264262652626626267262682626926270262712627226273262742627526276262772627826279262802628126282262832628426285262862628726288262892629026291262922629326294262952629626297262982629926300263012630226303263042630526306263072630826309263102631126312263132631426315263162631726318263192632026321263222632326324263252632626327263282632926330263312633226333263342633526336263372633826339263402634126342263432634426345263462634726348263492635026351263522635326354263552635626357263582635926360263612636226363263642636526366263672636826369263702637126372263732637426375263762637726378263792638026381263822638326384263852638626387263882638926390263912639226393263942639526396263972639826399264002640126402264032640426405264062640726408264092641026411264122641326414264152641626417264182641926420264212642226423264242642526426264272642826429264302643126432264332643426435264362643726438264392644026441264422644326444264452644626447264482644926450264512645226453264542645526456264572645826459264602646126462264632646426465264662646726468264692647026471264722647326474264752647626477264782647926480264812648226483264842648526486264872648826489264902649126492264932649426495264962649726498264992650026501265022650326504265052650626507265082650926510265112651226513265142651526516265172651826519265202652126522265232652426525265262652726528265292653026531265322653326534265352653626537265382653926540265412654226543265442654526546265472654826549265502655126552265532655426555265562655726558265592656026561265622656326564265652656626567265682656926570265712657226573265742657526576265772657826579265802658126582265832658426585265862658726588265892659026591265922659326594265952659626597265982659926600266012660226603266042660526606266072660826609266102661126612266132661426615266162661726618266192662026621266222662326624266252662626627266282662926630266312663226633266342663526636266372663826639266402664126642266432664426645266462664726648266492665026651266522665326654266552665626657266582665926660266612666226663266642666526666266672666826669266702667126672266732667426675266762667726678266792668026681266822668326684266852668626687266882668926690266912669226693266942669526696266972669826699267002670126702267032670426705267062670726708267092671026711267122671326714267152671626717267182671926720267212672226723267242672526726267272672826729267302673126732267332673426735267362673726738267392674026741267422674326744267452674626747267482674926750267512675226753267542675526756267572675826759267602676126762267632676426765267662676726768267692677026771267722677326774267752677626777267782677926780267812678226783267842678526786267872678826789267902679126792267932679426795267962679726798267992680026801268022680326804268052680626807268082680926810268112681226813268142681526816268172681826819268202682126822268232682426825268262682726828268292683026831268322683326834268352683626837268382683926840268412684226843268442684526846268472684826849268502685126852268532685426855268562685726858268592686026861268622686326864268652686626867268682686926870268712687226873268742687526876268772687826879268802688126882268832688426885268862688726888268892689026891268922689326894268952689626897268982689926900269012690226903269042690526906269072690826909269102691126912269132691426915269162691726918269192692026921269222692326924269252692626927269282692926930269312693226933269342693526936269372693826939269402694126942269432694426945269462694726948269492695026951269522695326954269552695626957269582695926960269612696226963269642696526966269672696826969269702697126972269732697426975269762697726978269792698026981269822698326984269852698626987269882698926990269912699226993269942699526996269972699826999270002700127002270032700427005270062700727008270092701027011270122701327014270152701627017270182701927020270212702227023270242702527026270272702827029270302703127032270332703427035270362703727038270392704027041270422704327044270452704627047270482704927050270512705227053270542705527056270572705827059270602706127062270632706427065270662706727068270692707027071270722707327074270752707627077270782707927080270812708227083270842708527086270872708827089270902709127092270932709427095270962709727098270992710027101271022710327104271052710627107271082710927110271112711227113271142711527116271172711827119271202712127122271232712427125271262712727128271292713027131271322713327134271352713627137271382713927140271412714227143271442714527146271472714827149271502715127152271532715427155271562715727158271592716027161271622716327164271652716627167271682716927170271712717227173271742717527176271772717827179271802718127182271832718427185271862718727188271892719027191271922719327194271952719627197271982719927200272012720227203272042720527206272072720827209272102721127212272132721427215272162721727218272192722027221272222722327224272252722627227272282722927230272312723227233272342723527236272372723827239272402724127242272432724427245272462724727248272492725027251272522725327254272552725627257272582725927260272612726227263272642726527266272672726827269272702727127272272732727427275272762727727278272792728027281272822728327284272852728627287272882728927290272912729227293272942729527296272972729827299273002730127302273032730427305273062730727308273092731027311273122731327314273152731627317273182731927320273212732227323273242732527326273272732827329273302733127332273332733427335273362733727338273392734027341273422734327344273452734627347273482734927350273512735227353273542735527356273572735827359273602736127362273632736427365273662736727368273692737027371273722737327374273752737627377273782737927380273812738227383273842738527386273872738827389273902739127392273932739427395273962739727398273992740027401274022740327404274052740627407274082740927410274112741227413274142741527416274172741827419274202742127422274232742427425274262742727428274292743027431274322743327434274352743627437274382743927440274412744227443274442744527446274472744827449274502745127452274532745427455274562745727458274592746027461274622746327464274652746627467274682746927470274712747227473274742747527476274772747827479274802748127482274832748427485274862748727488274892749027491274922749327494274952749627497274982749927500275012750227503275042750527506275072750827509275102751127512275132751427515275162751727518275192752027521275222752327524275252752627527275282752927530275312753227533275342753527536275372753827539275402754127542275432754427545275462754727548275492755027551275522755327554275552755627557275582755927560275612756227563275642756527566275672756827569275702757127572275732757427575275762757727578275792758027581275822758327584275852758627587275882758927590275912759227593275942759527596275972759827599276002760127602276032760427605276062760727608276092761027611276122761327614276152761627617276182761927620276212762227623276242762527626276272762827629276302763127632276332763427635276362763727638276392764027641276422764327644276452764627647276482764927650276512765227653276542765527656276572765827659276602766127662276632766427665276662766727668276692767027671276722767327674276752767627677276782767927680276812768227683276842768527686276872768827689276902769127692276932769427695276962769727698276992770027701277022770327704277052770627707277082770927710277112771227713277142771527716277172771827719277202772127722277232772427725277262772727728277292773027731277322773327734277352773627737277382773927740277412774227743277442774527746277472774827749277502775127752277532775427755277562775727758277592776027761277622776327764277652776627767277682776927770277712777227773277742777527776277772777827779277802778127782277832778427785277862778727788277892779027791277922779327794277952779627797277982779927800278012780227803278042780527806278072780827809278102781127812278132781427815278162781727818278192782027821278222782327824278252782627827278282782927830278312783227833278342783527836278372783827839278402784127842278432784427845278462784727848278492785027851278522785327854278552785627857278582785927860278612786227863278642786527866278672786827869278702787127872278732787427875278762787727878278792788027881278822788327884278852788627887278882788927890278912789227893278942789527896278972789827899279002790127902279032790427905279062790727908279092791027911279122791327914279152791627917279182791927920279212792227923279242792527926279272792827929279302793127932279332793427935279362793727938279392794027941279422794327944279452794627947279482794927950279512795227953279542795527956279572795827959279602796127962279632796427965279662796727968279692797027971279722797327974279752797627977279782797927980279812798227983279842798527986279872798827989279902799127992279932799427995279962799727998279992800028001280022800328004280052800628007280082800928010280112801228013280142801528016280172801828019280202802128022280232802428025280262802728028280292803028031280322803328034280352803628037280382803928040280412804228043280442804528046280472804828049280502805128052280532805428055280562805728058280592806028061280622806328064280652806628067280682806928070280712807228073280742807528076280772807828079280802808128082280832808428085280862808728088280892809028091280922809328094280952809628097280982809928100281012810228103281042810528106281072810828109281102811128112281132811428115281162811728118281192812028121281222812328124281252812628127281282812928130281312813228133281342813528136281372813828139281402814128142281432814428145281462814728148281492815028151281522815328154281552815628157281582815928160281612816228163281642816528166281672816828169281702817128172281732817428175281762817728178281792818028181281822818328184281852818628187281882818928190281912819228193281942819528196281972819828199282002820128202282032820428205282062820728208282092821028211282122821328214282152821628217282182821928220282212822228223282242822528226282272822828229282302823128232282332823428235282362823728238282392824028241282422824328244282452824628247282482824928250282512825228253282542825528256282572825828259282602826128262282632826428265282662826728268282692827028271282722827328274282752827628277282782827928280282812828228283282842828528286282872828828289282902829128292282932829428295282962829728298282992830028301283022830328304283052830628307283082830928310283112831228313283142831528316283172831828319283202832128322283232832428325283262832728328283292833028331283322833328334283352833628337283382833928340283412834228343283442834528346283472834828349283502835128352283532835428355283562835728358283592836028361283622836328364283652836628367283682836928370283712837228373283742837528376283772837828379283802838128382283832838428385283862838728388283892839028391283922839328394283952839628397283982839928400284012840228403284042840528406284072840828409284102841128412284132841428415284162841728418284192842028421284222842328424284252842628427284282842928430284312843228433284342843528436284372843828439284402844128442284432844428445284462844728448284492845028451284522845328454284552845628457284582845928460284612846228463284642846528466284672846828469284702847128472284732847428475284762847728478284792848028481284822848328484284852848628487284882848928490284912849228493284942849528496284972849828499285002850128502285032850428505285062850728508285092851028511285122851328514285152851628517285182851928520285212852228523285242852528526285272852828529285302853128532285332853428535285362853728538285392854028541285422854328544285452854628547285482854928550285512855228553285542855528556285572855828559285602856128562285632856428565285662856728568285692857028571285722857328574285752857628577285782857928580285812858228583285842858528586285872858828589285902859128592285932859428595285962859728598285992860028601286022860328604286052860628607286082860928610286112861228613286142861528616286172861828619286202862128622286232862428625286262862728628286292863028631286322863328634286352863628637286382863928640286412864228643286442864528646286472864828649286502865128652286532865428655286562865728658286592866028661286622866328664286652866628667286682866928670286712867228673286742867528676286772867828679286802868128682286832868428685286862868728688286892869028691286922869328694286952869628697286982869928700287012870228703287042870528706287072870828709287102871128712287132871428715287162871728718287192872028721287222872328724287252872628727287282872928730287312873228733287342873528736287372873828739287402874128742287432874428745287462874728748287492875028751287522875328754287552875628757287582875928760287612876228763287642876528766287672876828769287702877128772287732877428775287762877728778287792878028781287822878328784287852878628787287882878928790287912879228793287942879528796287972879828799288002880128802288032880428805288062880728808288092881028811288122881328814288152881628817288182881928820288212882228823288242882528826288272882828829288302883128832288332883428835288362883728838288392884028841288422884328844288452884628847288482884928850288512885228853288542885528856288572885828859288602886128862288632886428865288662886728868288692887028871288722887328874288752887628877288782887928880288812888228883288842888528886288872888828889288902889128892288932889428895288962889728898288992890028901289022890328904289052890628907289082890928910289112891228913289142891528916289172891828919289202892128922289232892428925289262892728928289292893028931289322893328934289352893628937289382893928940289412894228943289442894528946289472894828949289502895128952289532895428955289562895728958289592896028961289622896328964289652896628967289682896928970289712897228973289742897528976289772897828979289802898128982289832898428985289862898728988289892899028991289922899328994289952899628997289982899929000290012900229003290042900529006290072900829009290102901129012290132901429015290162901729018290192902029021290222902329024290252902629027290282902929030290312903229033290342903529036290372903829039290402904129042290432904429045290462904729048290492905029051290522905329054290552905629057290582905929060290612906229063290642906529066290672906829069290702907129072290732907429075290762907729078290792908029081290822908329084290852908629087290882908929090290912909229093290942909529096290972909829099291002910129102291032910429105291062910729108291092911029111291122911329114291152911629117291182911929120291212912229123291242912529126291272912829129291302913129132291332913429135291362913729138291392914029141291422914329144291452914629147291482914929150291512915229153291542915529156291572915829159291602916129162291632916429165291662916729168291692917029171291722917329174291752917629177291782917929180291812918229183291842918529186291872918829189291902919129192291932919429195291962919729198291992920029201292022920329204292052920629207292082920929210292112921229213292142921529216292172921829219292202922129222292232922429225292262922729228292292923029231292322923329234292352923629237292382923929240292412924229243292442924529246292472924829249292502925129252292532925429255292562925729258292592926029261292622926329264292652926629267292682926929270292712927229273292742927529276292772927829279292802928129282292832928429285292862928729288292892929029291292922929329294292952929629297292982929929300293012930229303293042930529306293072930829309293102931129312293132931429315293162931729318293192932029321293222932329324293252932629327293282932929330293312933229333293342933529336293372933829339293402934129342293432934429345293462934729348293492935029351293522935329354293552935629357293582935929360293612936229363293642936529366293672936829369293702937129372293732937429375293762937729378293792938029381293822938329384293852938629387293882938929390293912939229393293942939529396293972939829399294002940129402294032940429405294062940729408294092941029411294122941329414294152941629417294182941929420294212942229423294242942529426294272942829429294302943129432294332943429435294362943729438294392944029441294422944329444294452944629447294482944929450294512945229453294542945529456294572945829459294602946129462294632946429465294662946729468294692947029471294722947329474294752947629477294782947929480294812948229483294842948529486294872948829489294902949129492294932949429495294962949729498294992950029501295022950329504295052950629507295082950929510295112951229513295142951529516295172951829519295202952129522295232952429525295262952729528295292953029531295322953329534295352953629537295382953929540295412954229543295442954529546295472954829549295502955129552295532955429555295562955729558295592956029561295622956329564295652956629567295682956929570295712957229573295742957529576295772957829579295802958129582295832958429585295862958729588295892959029591295922959329594295952959629597295982959929600296012960229603296042960529606296072960829609296102961129612296132961429615296162961729618296192962029621296222962329624296252962629627296282962929630296312963229633296342963529636296372963829639296402964129642296432964429645296462964729648296492965029651296522965329654296552965629657296582965929660296612966229663296642966529666296672966829669296702967129672296732967429675296762967729678296792968029681296822968329684296852968629687296882968929690296912969229693296942969529696296972969829699297002970129702297032970429705297062970729708297092971029711297122971329714297152971629717297182971929720297212972229723297242972529726297272972829729297302973129732297332973429735297362973729738297392974029741297422974329744297452974629747297482974929750297512975229753297542975529756297572975829759297602976129762297632976429765297662976729768297692977029771297722977329774297752977629777297782977929780297812978229783297842978529786297872978829789297902979129792297932979429795297962979729798297992980029801298022980329804298052980629807298082980929810298112981229813298142981529816298172981829819298202982129822298232982429825298262982729828298292983029831298322983329834298352983629837298382983929840298412984229843298442984529846298472984829849298502985129852298532985429855298562985729858298592986029861298622986329864298652986629867298682986929870298712987229873298742987529876298772987829879298802988129882298832988429885298862988729888298892989029891298922989329894298952989629897298982989929900299012990229903299042990529906299072990829909299102991129912299132991429915299162991729918299192992029921299222992329924299252992629927299282992929930299312993229933299342993529936299372993829939299402994129942299432994429945299462994729948299492995029951299522995329954299552995629957299582995929960299612996229963299642996529966299672996829969299702997129972299732997429975299762997729978299792998029981299822998329984299852998629987299882998929990299912999229993299942999529996299972999829999300003000130002300033000430005300063000730008300093001030011300123001330014300153001630017300183001930020300213002230023300243002530026300273002830029300303003130032300333003430035300363003730038300393004030041300423004330044300453004630047300483004930050300513005230053300543005530056300573005830059300603006130062300633006430065300663006730068300693007030071300723007330074300753007630077300783007930080300813008230083300843008530086300873008830089300903009130092300933009430095300963009730098300993010030101301023010330104301053010630107301083010930110301113011230113301143011530116301173011830119301203012130122301233012430125301263012730128301293013030131301323013330134301353013630137301383013930140301413014230143301443014530146301473014830149301503015130152301533015430155301563015730158301593016030161301623016330164301653016630167301683016930170301713017230173301743017530176301773017830179301803018130182301833018430185301863018730188301893019030191301923019330194301953019630197301983019930200302013020230203302043020530206302073020830209302103021130212302133021430215302163021730218302193022030221302223022330224302253022630227302283022930230302313023230233302343023530236302373023830239302403024130242302433024430245302463024730248302493025030251302523025330254302553025630257302583025930260302613026230263302643026530266302673026830269302703027130272302733027430275302763027730278302793028030281302823028330284302853028630287302883028930290302913029230293302943029530296302973029830299303003030130302303033030430305303063030730308303093031030311303123031330314303153031630317303183031930320303213032230323303243032530326303273032830329303303033130332303333033430335303363033730338303393034030341303423034330344303453034630347303483034930350303513035230353303543035530356303573035830359303603036130362303633036430365303663036730368303693037030371303723037330374303753037630377303783037930380303813038230383303843038530386303873038830389303903039130392303933039430395303963039730398303993040030401304023040330404304053040630407304083040930410304113041230413304143041530416304173041830419304203042130422304233042430425304263042730428304293043030431304323043330434304353043630437304383043930440304413044230443304443044530446304473044830449304503045130452304533045430455304563045730458304593046030461304623046330464304653046630467304683046930470304713047230473304743047530476304773047830479304803048130482304833048430485304863048730488304893049030491304923049330494304953049630497304983049930500305013050230503305043050530506305073050830509305103051130512305133051430515305163051730518305193052030521305223052330524305253052630527305283052930530305313053230533305343053530536305373053830539305403054130542305433054430545305463054730548305493055030551305523055330554305553055630557305583055930560305613056230563305643056530566305673056830569305703057130572305733057430575305763057730578305793058030581305823058330584305853058630587305883058930590305913059230593305943059530596305973059830599306003060130602306033060430605306063060730608306093061030611306123061330614306153061630617306183061930620306213062230623306243062530626306273062830629306303063130632306333063430635306363063730638306393064030641306423064330644306453064630647306483064930650306513065230653306543065530656306573065830659306603066130662306633066430665306663066730668306693067030671306723067330674306753067630677306783067930680306813068230683306843068530686306873068830689306903069130692306933069430695306963069730698306993070030701307023070330704307053070630707307083070930710307113071230713307143071530716307173071830719307203072130722307233072430725307263072730728307293073030731307323073330734307353073630737307383073930740307413074230743307443074530746307473074830749307503075130752307533075430755307563075730758307593076030761307623076330764307653076630767307683076930770307713077230773307743077530776307773077830779307803078130782307833078430785307863078730788307893079030791307923079330794307953079630797307983079930800308013080230803308043080530806308073080830809308103081130812308133081430815308163081730818308193082030821308223082330824308253082630827308283082930830308313083230833308343083530836308373083830839308403084130842308433084430845308463084730848308493085030851308523085330854308553085630857308583085930860308613086230863308643086530866308673086830869308703087130872308733087430875308763087730878308793088030881308823088330884308853088630887308883088930890308913089230893308943089530896308973089830899309003090130902309033090430905309063090730908309093091030911309123091330914309153091630917309183091930920309213092230923309243092530926309273092830929309303093130932309333093430935309363093730938309393094030941309423094330944309453094630947309483094930950309513095230953309543095530956309573095830959309603096130962309633096430965309663096730968309693097030971309723097330974309753097630977309783097930980309813098230983309843098530986309873098830989309903099130992309933099430995309963099730998309993100031001310023100331004310053100631007310083100931010310113101231013310143101531016310173101831019310203102131022310233102431025310263102731028310293103031031310323103331034310353103631037310383103931040310413104231043310443104531046310473104831049310503105131052310533105431055310563105731058310593106031061310623106331064310653106631067310683106931070310713107231073310743107531076310773107831079310803108131082310833108431085310863108731088310893109031091310923109331094310953109631097310983109931100311013110231103311043110531106311073110831109311103111131112311133111431115311163111731118311193112031121311223112331124311253112631127311283112931130311313113231133311343113531136311373113831139311403114131142311433114431145311463114731148311493115031151311523115331154311553115631157311583115931160311613116231163311643116531166311673116831169311703117131172311733117431175311763117731178311793118031181311823118331184311853118631187311883118931190311913119231193311943119531196311973119831199312003120131202312033120431205312063120731208312093121031211312123121331214312153121631217312183121931220312213122231223312243122531226312273122831229312303123131232312333123431235312363123731238312393124031241312423124331244312453124631247312483124931250312513125231253312543125531256312573125831259312603126131262312633126431265312663126731268312693127031271312723127331274312753127631277312783127931280312813128231283312843128531286312873128831289312903129131292312933129431295312963129731298312993130031301313023130331304313053130631307313083130931310313113131231313313143131531316313173131831319313203132131322313233132431325313263132731328313293133031331313323133331334313353133631337313383133931340313413134231343313443134531346313473134831349313503135131352313533135431355313563135731358313593136031361313623136331364313653136631367313683136931370313713137231373313743137531376313773137831379313803138131382313833138431385313863138731388313893139031391313923139331394313953139631397313983139931400314013140231403314043140531406314073140831409314103141131412314133141431415314163141731418314193142031421314223142331424314253142631427314283142931430314313143231433314343143531436314373143831439314403144131442314433144431445314463144731448314493145031451314523145331454314553145631457314583145931460314613146231463314643146531466314673146831469314703147131472314733147431475314763147731478314793148031481314823148331484314853148631487314883148931490314913149231493314943149531496314973149831499315003150131502315033150431505315063150731508315093151031511315123151331514315153151631517315183151931520315213152231523315243152531526315273152831529315303153131532315333153431535315363153731538315393154031541315423154331544315453154631547315483154931550315513155231553315543155531556315573155831559315603156131562315633156431565315663156731568315693157031571315723157331574315753157631577315783157931580315813158231583315843158531586315873158831589315903159131592315933159431595315963159731598315993160031601316023160331604316053160631607316083160931610316113161231613316143161531616316173161831619316203162131622316233162431625316263162731628316293163031631316323163331634316353163631637316383163931640316413164231643316443164531646316473164831649316503165131652316533165431655316563165731658316593166031661316623166331664316653166631667316683166931670316713167231673316743167531676316773167831679316803168131682316833168431685316863168731688316893169031691316923169331694316953169631697316983169931700317013170231703317043170531706317073170831709317103171131712317133171431715317163171731718317193172031721317223172331724317253172631727317283172931730317313173231733317343173531736317373173831739317403174131742317433174431745317463174731748317493175031751317523175331754317553175631757317583175931760317613176231763317643176531766317673176831769317703177131772317733177431775317763177731778317793178031781317823178331784317853178631787317883178931790317913179231793317943179531796317973179831799318003180131802318033180431805318063180731808318093181031811318123181331814318153181631817318183181931820318213182231823318243182531826318273182831829318303183131832318333183431835318363183731838318393184031841318423184331844318453184631847318483184931850318513185231853318543185531856318573185831859318603186131862318633186431865318663186731868318693187031871318723187331874318753187631877318783187931880318813188231883318843188531886318873188831889318903189131892318933189431895318963189731898318993190031901319023190331904319053190631907319083190931910319113191231913319143191531916319173191831919319203192131922319233192431925319263192731928319293193031931319323193331934319353193631937319383193931940319413194231943319443194531946319473194831949319503195131952319533195431955319563195731958319593196031961319623196331964319653196631967319683196931970319713197231973319743197531976319773197831979319803198131982319833198431985319863198731988319893199031991319923199331994319953199631997319983199932000320013200232003320043200532006320073200832009320103201132012320133201432015320163201732018320193202032021320223202332024320253202632027320283202932030320313203232033320343203532036320373203832039320403204132042320433204432045320463204732048320493205032051320523205332054320553205632057320583205932060320613206232063320643206532066320673206832069320703207132072320733207432075320763207732078320793208032081320823208332084320853208632087320883208932090320913209232093320943209532096320973209832099321003210132102321033210432105321063210732108321093211032111321123211332114321153211632117321183211932120321213212232123321243212532126321273212832129321303213132132321333213432135321363213732138321393214032141321423214332144321453214632147321483214932150321513215232153321543215532156321573215832159321603216132162321633216432165321663216732168321693217032171321723217332174321753217632177321783217932180321813218232183321843218532186321873218832189321903219132192321933219432195321963219732198321993220032201322023220332204322053220632207322083220932210322113221232213322143221532216322173221832219322203222132222322233222432225322263222732228322293223032231322323223332234322353223632237322383223932240322413224232243322443224532246322473224832249322503225132252322533225432255322563225732258322593226032261322623226332264322653226632267322683226932270322713227232273322743227532276322773227832279322803228132282322833228432285322863228732288322893229032291322923229332294322953229632297322983229932300323013230232303323043230532306323073230832309323103231132312323133231432315323163231732318323193232032321323223232332324323253232632327323283232932330323313233232333323343233532336323373233832339323403234132342323433234432345323463234732348323493235032351323523235332354323553235632357323583235932360323613236232363323643236532366323673236832369323703237132372323733237432375323763237732378323793238032381323823238332384323853238632387323883238932390323913239232393323943239532396323973239832399324003240132402324033240432405324063240732408324093241032411324123241332414324153241632417324183241932420324213242232423324243242532426324273242832429324303243132432324333243432435324363243732438324393244032441324423244332444324453244632447324483244932450324513245232453324543245532456324573245832459324603246132462324633246432465324663246732468324693247032471324723247332474324753247632477324783247932480324813248232483324843248532486324873248832489324903249132492324933249432495324963249732498324993250032501325023250332504325053250632507325083250932510325113251232513325143251532516325173251832519325203252132522325233252432525325263252732528325293253032531325323253332534325353253632537325383253932540325413254232543325443254532546325473254832549325503255132552325533255432555325563255732558325593256032561325623256332564325653256632567325683256932570325713257232573325743257532576325773257832579325803258132582325833258432585325863258732588325893259032591325923259332594325953259632597325983259932600326013260232603326043260532606326073260832609326103261132612326133261432615326163261732618326193262032621326223262332624326253262632627326283262932630326313263232633326343263532636326373263832639326403264132642326433264432645326463264732648326493265032651326523265332654326553265632657326583265932660326613266232663326643266532666326673266832669326703267132672326733267432675326763267732678326793268032681326823268332684326853268632687326883268932690326913269232693326943269532696326973269832699327003270132702327033270432705327063270732708327093271032711327123271332714327153271632717327183271932720327213272232723327243272532726327273272832729327303273132732327333273432735327363273732738327393274032741327423274332744327453274632747327483274932750327513275232753327543275532756327573275832759327603276132762327633276432765327663276732768327693277032771327723277332774327753277632777327783277932780327813278232783327843278532786327873278832789327903279132792327933279432795327963279732798327993280032801328023280332804328053280632807328083280932810328113281232813328143281532816328173281832819328203282132822328233282432825328263282732828328293283032831328323283332834328353283632837328383283932840328413284232843328443284532846328473284832849328503285132852328533285432855328563285732858328593286032861328623286332864328653286632867328683286932870328713287232873328743287532876328773287832879328803288132882328833288432885328863288732888328893289032891328923289332894328953289632897328983289932900329013290232903329043290532906329073290832909329103291132912329133291432915329163291732918329193292032921329223292332924329253292632927329283292932930329313293232933329343293532936329373293832939329403294132942329433294432945329463294732948329493295032951329523295332954329553295632957329583295932960329613296232963329643296532966329673296832969329703297132972329733297432975329763297732978329793298032981329823298332984329853298632987329883298932990329913299232993329943299532996329973299832999330003300133002330033300433005330063300733008330093301033011330123301333014330153301633017330183301933020330213302233023330243302533026330273302833029330303303133032330333303433035330363303733038330393304033041330423304333044330453304633047330483304933050330513305233053330543305533056330573305833059330603306133062330633306433065330663306733068330693307033071330723307333074330753307633077330783307933080330813308233083330843308533086330873308833089330903309133092330933309433095330963309733098330993310033101331023310333104331053310633107331083310933110331113311233113331143311533116331173311833119331203312133122331233312433125331263312733128331293313033131331323313333134331353313633137331383313933140331413314233143331443314533146331473314833149331503315133152331533315433155331563315733158331593316033161331623316333164331653316633167331683316933170331713317233173331743317533176331773317833179331803318133182331833318433185331863318733188331893319033191331923319333194331953319633197331983319933200332013320233203332043320533206332073320833209332103321133212332133321433215332163321733218332193322033221332223322333224332253322633227332283322933230332313323233233332343323533236332373323833239332403324133242332433324433245332463324733248332493325033251332523325333254332553325633257332583325933260332613326233263332643326533266332673326833269332703327133272332733327433275332763327733278332793328033281332823328333284332853328633287332883328933290332913329233293332943329533296332973329833299333003330133302333033330433305333063330733308333093331033311333123331333314333153331633317333183331933320333213332233323333243332533326333273332833329333303333133332333333333433335333363333733338333393334033341333423334333344333453334633347333483334933350333513335233353333543335533356333573335833359333603336133362333633336433365333663336733368333693337033371333723337333374333753337633377333783337933380333813338233383333843338533386333873338833389333903339133392333933339433395333963339733398333993340033401334023340333404334053340633407334083340933410334113341233413334143341533416334173341833419334203342133422334233342433425334263342733428334293343033431334323343333434334353343633437334383343933440334413344233443334443344533446334473344833449334503345133452334533345433455334563345733458334593346033461334623346333464334653346633467334683346933470334713347233473334743347533476334773347833479334803348133482334833348433485334863348733488334893349033491334923349333494334953349633497334983349933500335013350233503335043350533506335073350833509335103351133512335133351433515335163351733518335193352033521335223352333524335253352633527335283352933530335313353233533335343353533536335373353833539335403354133542335433354433545335463354733548335493355033551335523355333554335553355633557335583355933560335613356233563335643356533566335673356833569335703357133572335733357433575335763357733578335793358033581335823358333584335853358633587335883358933590335913359233593335943359533596335973359833599336003360133602336033360433605336063360733608336093361033611336123361333614336153361633617336183361933620336213362233623336243362533626336273362833629336303363133632336333363433635336363363733638336393364033641336423364333644336453364633647336483364933650336513365233653336543365533656336573365833659336603366133662336633366433665336663366733668336693367033671336723367333674336753367633677336783367933680336813368233683336843368533686336873368833689336903369133692336933369433695336963369733698336993370033701337023370333704337053370633707337083370933710337113371233713337143371533716337173371833719337203372133722337233372433725337263372733728337293373033731337323373333734337353373633737337383373933740337413374233743337443374533746337473374833749337503375133752337533375433755337563375733758337593376033761337623376333764337653376633767337683376933770337713377233773337743377533776337773377833779337803378133782337833378433785337863378733788337893379033791337923379333794337953379633797337983379933800338013380233803338043380533806338073380833809338103381133812338133381433815338163381733818338193382033821338223382333824338253382633827338283382933830338313383233833338343383533836338373383833839338403384133842338433384433845338463384733848338493385033851338523385333854338553385633857338583385933860338613386233863338643386533866338673386833869338703387133872338733387433875338763387733878338793388033881338823388333884338853388633887338883388933890338913389233893338943389533896338973389833899339003390133902339033390433905339063390733908339093391033911339123391333914339153391633917339183391933920339213392233923339243392533926339273392833929339303393133932339333393433935339363393733938339393394033941339423394333944339453394633947339483394933950339513395233953339543395533956339573395833959339603396133962339633396433965339663396733968339693397033971339723397333974339753397633977339783397933980339813398233983339843398533986339873398833989339903399133992339933399433995339963399733998339993400034001340023400334004340053400634007340083400934010340113401234013340143401534016340173401834019340203402134022340233402434025340263402734028340293403034031340323403334034340353403634037340383403934040340413404234043340443404534046340473404834049340503405134052340533405434055340563405734058340593406034061340623406334064340653406634067340683406934070340713407234073340743407534076340773407834079340803408134082340833408434085340863408734088340893409034091340923409334094340953409634097340983409934100341013410234103341043410534106341073410834109341103411134112341133411434115341163411734118341193412034121341223412334124341253412634127341283412934130341313413234133341343413534136341373413834139341403414134142341433414434145341463414734148341493415034151341523415334154341553415634157341583415934160341613416234163341643416534166341673416834169341703417134172341733417434175341763417734178341793418034181341823418334184341853418634187341883418934190341913419234193341943419534196341973419834199342003420134202342033420434205342063420734208342093421034211342123421334214342153421634217342183421934220342213422234223342243422534226342273422834229342303423134232342333423434235342363423734238342393424034241342423424334244342453424634247342483424934250342513425234253342543425534256342573425834259342603426134262342633426434265342663426734268342693427034271342723427334274342753427634277342783427934280342813428234283342843428534286342873428834289342903429134292342933429434295342963429734298342993430034301343023430334304343053430634307343083430934310343113431234313343143431534316343173431834319343203432134322343233432434325343263432734328343293433034331343323433334334343353433634337343383433934340343413434234343343443434534346343473434834349343503435134352343533435434355343563435734358343593436034361343623436334364343653436634367343683436934370343713437234373343743437534376343773437834379343803438134382343833438434385343863438734388343893439034391343923439334394343953439634397343983439934400344013440234403344043440534406344073440834409344103441134412344133441434415344163441734418344193442034421344223442334424344253442634427344283442934430344313443234433344343443534436344373443834439344403444134442344433444434445344463444734448344493445034451344523445334454344553445634457344583445934460344613446234463344643446534466344673446834469344703447134472344733447434475344763447734478344793448034481344823448334484344853448634487344883448934490344913449234493344943449534496344973449834499345003450134502345033450434505345063450734508345093451034511345123451334514345153451634517345183451934520345213452234523345243452534526345273452834529345303453134532345333453434535345363453734538345393454034541345423454334544345453454634547345483454934550345513455234553345543455534556345573455834559345603456134562345633456434565345663456734568345693457034571345723457334574345753457634577345783457934580345813458234583345843458534586345873458834589345903459134592345933459434595345963459734598345993460034601346023460334604346053460634607346083460934610346113461234613346143461534616346173461834619346203462134622346233462434625346263462734628346293463034631346323463334634346353463634637346383463934640346413464234643346443464534646346473464834649346503465134652346533465434655346563465734658346593466034661346623466334664346653466634667346683466934670346713467234673346743467534676346773467834679346803468134682346833468434685346863468734688346893469034691346923469334694346953469634697346983469934700347013470234703347043470534706347073470834709347103471134712347133471434715347163471734718347193472034721347223472334724347253472634727347283472934730347313473234733347343473534736347373473834739347403474134742347433474434745347463474734748347493475034751347523475334754347553475634757347583475934760347613476234763347643476534766347673476834769347703477134772347733477434775347763477734778347793478034781347823478334784347853478634787347883478934790347913479234793347943479534796347973479834799348003480134802348033480434805348063480734808348093481034811348123481334814348153481634817348183481934820348213482234823348243482534826348273482834829348303483134832348333483434835348363483734838348393484034841348423484334844348453484634847348483484934850348513485234853348543485534856348573485834859348603486134862348633486434865348663486734868348693487034871348723487334874348753487634877348783487934880348813488234883348843488534886348873488834889348903489134892348933489434895348963489734898348993490034901349023490334904349053490634907349083490934910349113491234913349143491534916349173491834919349203492134922349233492434925349263492734928349293493034931349323493334934349353493634937349383493934940349413494234943349443494534946
  1. // File:src/Three.js
  2. /**
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. var THREE = { REVISION: '70dev' };
  6. // browserify support
  7. if ( typeof module === 'object' ) {
  8. module.exports = THREE;
  9. }
  10. // polyfills
  11. if ( Math.sign === undefined ) {
  12. Math.sign = function ( x ) {
  13. return ( x < 0 ) ? - 1 : ( x > 0 ) ? 1 : 0;
  14. };
  15. }
  16. // https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent.button
  17. THREE.MOUSE = { LEFT: 0, MIDDLE: 1, RIGHT: 2 };
  18. // GL STATE CONSTANTS
  19. THREE.CullFaceNone = 0;
  20. THREE.CullFaceBack = 1;
  21. THREE.CullFaceFront = 2;
  22. THREE.CullFaceFrontBack = 3;
  23. THREE.FrontFaceDirectionCW = 0;
  24. THREE.FrontFaceDirectionCCW = 1;
  25. // SHADOWING TYPES
  26. THREE.BasicShadowMap = 0;
  27. THREE.PCFShadowMap = 1;
  28. THREE.PCFSoftShadowMap = 2;
  29. // MATERIAL CONSTANTS
  30. // side
  31. THREE.FrontSide = 0;
  32. THREE.BackSide = 1;
  33. THREE.DoubleSide = 2;
  34. // shading
  35. THREE.NoShading = 0;
  36. THREE.FlatShading = 1;
  37. THREE.SmoothShading = 2;
  38. // colors
  39. THREE.NoColors = 0;
  40. THREE.FaceColors = 1;
  41. THREE.VertexColors = 2;
  42. // blending modes
  43. THREE.NoBlending = 0;
  44. THREE.NormalBlending = 1;
  45. THREE.AdditiveBlending = 2;
  46. THREE.SubtractiveBlending = 3;
  47. THREE.MultiplyBlending = 4;
  48. THREE.CustomBlending = 5;
  49. // custom blending equations
  50. // (numbers start from 100 not to clash with other
  51. // mappings to OpenGL constants defined in Texture.js)
  52. THREE.AddEquation = 100;
  53. THREE.SubtractEquation = 101;
  54. THREE.ReverseSubtractEquation = 102;
  55. THREE.MinEquation = 103;
  56. THREE.MaxEquation = 104;
  57. // custom blending destination factors
  58. THREE.ZeroFactor = 200;
  59. THREE.OneFactor = 201;
  60. THREE.SrcColorFactor = 202;
  61. THREE.OneMinusSrcColorFactor = 203;
  62. THREE.SrcAlphaFactor = 204;
  63. THREE.OneMinusSrcAlphaFactor = 205;
  64. THREE.DstAlphaFactor = 206;
  65. THREE.OneMinusDstAlphaFactor = 207;
  66. // custom blending source factors
  67. //THREE.ZeroFactor = 200;
  68. //THREE.OneFactor = 201;
  69. //THREE.SrcAlphaFactor = 204;
  70. //THREE.OneMinusSrcAlphaFactor = 205;
  71. //THREE.DstAlphaFactor = 206;
  72. //THREE.OneMinusDstAlphaFactor = 207;
  73. THREE.DstColorFactor = 208;
  74. THREE.OneMinusDstColorFactor = 209;
  75. THREE.SrcAlphaSaturateFactor = 210;
  76. // TEXTURE CONSTANTS
  77. THREE.MultiplyOperation = 0;
  78. THREE.MixOperation = 1;
  79. THREE.AddOperation = 2;
  80. // Mapping modes
  81. THREE.UVMapping = 300;
  82. THREE.CubeReflectionMapping = 301;
  83. THREE.CubeRefractionMapping = 302;
  84. THREE.EquirectangularReflectionMapping = 303;
  85. THREE.EquirectangularRefractionMapping = 304;
  86. THREE.SphericalReflectionMapping = 305;
  87. // Wrapping modes
  88. THREE.RepeatWrapping = 1000;
  89. THREE.ClampToEdgeWrapping = 1001;
  90. THREE.MirroredRepeatWrapping = 1002;
  91. // Filters
  92. THREE.NearestFilter = 1003;
  93. THREE.NearestMipMapNearestFilter = 1004;
  94. THREE.NearestMipMapLinearFilter = 1005;
  95. THREE.LinearFilter = 1006;
  96. THREE.LinearMipMapNearestFilter = 1007;
  97. THREE.LinearMipMapLinearFilter = 1008;
  98. // Data types
  99. THREE.UnsignedByteType = 1009;
  100. THREE.ByteType = 1010;
  101. THREE.ShortType = 1011;
  102. THREE.UnsignedShortType = 1012;
  103. THREE.IntType = 1013;
  104. THREE.UnsignedIntType = 1014;
  105. THREE.FloatType = 1015;
  106. // Pixel types
  107. //THREE.UnsignedByteType = 1009;
  108. THREE.UnsignedShort4444Type = 1016;
  109. THREE.UnsignedShort5551Type = 1017;
  110. THREE.UnsignedShort565Type = 1018;
  111. // Pixel formats
  112. THREE.AlphaFormat = 1019;
  113. THREE.RGBFormat = 1020;
  114. THREE.RGBAFormat = 1021;
  115. THREE.LuminanceFormat = 1022;
  116. THREE.LuminanceAlphaFormat = 1023;
  117. // THREE.RGBEFormat handled as THREE.RGBAFormat in shaders
  118. THREE.RGBEFormat = THREE.RGBAFormat; //1024;
  119. // DDS / ST3C Compressed texture formats
  120. THREE.RGB_S3TC_DXT1_Format = 2001;
  121. THREE.RGBA_S3TC_DXT1_Format = 2002;
  122. THREE.RGBA_S3TC_DXT3_Format = 2003;
  123. THREE.RGBA_S3TC_DXT5_Format = 2004;
  124. // PVRTC compressed texture formats
  125. THREE.RGB_PVRTC_4BPPV1_Format = 2100;
  126. THREE.RGB_PVRTC_2BPPV1_Format = 2101;
  127. THREE.RGBA_PVRTC_4BPPV1_Format = 2102;
  128. THREE.RGBA_PVRTC_2BPPV1_Format = 2103;
  129. // DEPRECATED
  130. THREE.Projector = function () {
  131. console.error( 'THREE.Projector has been moved to /examples/js/renderers/Projector.js.' );
  132. this.projectVector = function ( vector, camera ) {
  133. console.warn( 'THREE.Projector: .projectVector() is now vector.project().' );
  134. vector.project( camera );
  135. };
  136. this.unprojectVector = function ( vector, camera ) {
  137. console.warn( 'THREE.Projector: .unprojectVector() is now vector.unproject().' );
  138. vector.unproject( camera );
  139. };
  140. this.pickingRay = function ( vector, camera ) {
  141. console.error( 'THREE.Projector: .pickingRay() has been removed.' );
  142. };
  143. };
  144. THREE.CanvasRenderer = function () {
  145. console.error( 'THREE.CanvasRenderer has been moved to /examples/js/renderers/CanvasRenderer.js' );
  146. this.domElement = document.createElement( 'canvas' );
  147. this.clear = function () {};
  148. this.render = function () {};
  149. this.setClearColor = function () {};
  150. this.setSize = function () {};
  151. };
  152. // File:src/math/Color.js
  153. /**
  154. * @author mrdoob / http://mrdoob.com/
  155. */
  156. THREE.Color = function ( color ) {
  157. if ( arguments.length === 3 ) {
  158. return this.setRGB( arguments[ 0 ], arguments[ 1 ], arguments[ 2 ] );
  159. }
  160. return this.set( color )
  161. };
  162. THREE.Color.prototype = {
  163. constructor: THREE.Color,
  164. r: 1, g: 1, b: 1,
  165. set: function ( value ) {
  166. if ( value instanceof THREE.Color ) {
  167. this.copy( value );
  168. } else if ( typeof value === 'number' ) {
  169. this.setHex( value );
  170. } else if ( typeof value === 'string' ) {
  171. this.setStyle( value );
  172. }
  173. return this;
  174. },
  175. setHex: function ( hex ) {
  176. hex = Math.floor( hex );
  177. this.r = ( hex >> 16 & 255 ) / 255;
  178. this.g = ( hex >> 8 & 255 ) / 255;
  179. this.b = ( hex & 255 ) / 255;
  180. return this;
  181. },
  182. setRGB: function ( r, g, b ) {
  183. this.r = r;
  184. this.g = g;
  185. this.b = b;
  186. return this;
  187. },
  188. setHSL: function ( h, s, l ) {
  189. // h,s,l ranges are in 0.0 - 1.0
  190. if ( s === 0 ) {
  191. this.r = this.g = this.b = l;
  192. } else {
  193. var hue2rgb = function ( p, q, t ) {
  194. if ( t < 0 ) t += 1;
  195. if ( t > 1 ) t -= 1;
  196. if ( t < 1 / 6 ) return p + ( q - p ) * 6 * t;
  197. if ( t < 1 / 2 ) return q;
  198. if ( t < 2 / 3 ) return p + ( q - p ) * 6 * ( 2 / 3 - t );
  199. return p;
  200. };
  201. var p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s );
  202. var q = ( 2 * l ) - p;
  203. this.r = hue2rgb( q, p, h + 1 / 3 );
  204. this.g = hue2rgb( q, p, h );
  205. this.b = hue2rgb( q, p, h - 1 / 3 );
  206. }
  207. return this;
  208. },
  209. setStyle: function ( style ) {
  210. // rgb(255,0,0)
  211. if ( /^rgb\((\d+), ?(\d+), ?(\d+)\)$/i.test( style ) ) {
  212. var color = /^rgb\((\d+), ?(\d+), ?(\d+)\)$/i.exec( style );
  213. this.r = Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255;
  214. this.g = Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255;
  215. this.b = Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255;
  216. return this;
  217. }
  218. // rgb(100%,0%,0%)
  219. if ( /^rgb\((\d+)\%, ?(\d+)\%, ?(\d+)\%\)$/i.test( style ) ) {
  220. var color = /^rgb\((\d+)\%, ?(\d+)\%, ?(\d+)\%\)$/i.exec( style );
  221. this.r = Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100;
  222. this.g = Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100;
  223. this.b = Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100;
  224. return this;
  225. }
  226. // #ff0000
  227. if ( /^\#([0-9a-f]{6})$/i.test( style ) ) {
  228. var color = /^\#([0-9a-f]{6})$/i.exec( style );
  229. this.setHex( parseInt( color[ 1 ], 16 ) );
  230. return this;
  231. }
  232. // #f00
  233. if ( /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i.test( style ) ) {
  234. var color = /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i.exec( style );
  235. this.setHex( parseInt( color[ 1 ] + color[ 1 ] + color[ 2 ] + color[ 2 ] + color[ 3 ] + color[ 3 ], 16 ) );
  236. return this;
  237. }
  238. // red
  239. if ( /^(\w+)$/i.test( style ) ) {
  240. this.setHex( THREE.ColorKeywords[ style ] );
  241. return this;
  242. }
  243. },
  244. copy: function ( color ) {
  245. this.r = color.r;
  246. this.g = color.g;
  247. this.b = color.b;
  248. return this;
  249. },
  250. copyGammaToLinear: function ( color ) {
  251. this.r = color.r * color.r;
  252. this.g = color.g * color.g;
  253. this.b = color.b * color.b;
  254. return this;
  255. },
  256. copyLinearToGamma: function ( color ) {
  257. this.r = Math.sqrt( color.r );
  258. this.g = Math.sqrt( color.g );
  259. this.b = Math.sqrt( color.b );
  260. return this;
  261. },
  262. convertGammaToLinear: function () {
  263. var r = this.r, g = this.g, b = this.b;
  264. this.r = r * r;
  265. this.g = g * g;
  266. this.b = b * b;
  267. return this;
  268. },
  269. convertLinearToGamma: function () {
  270. this.r = Math.sqrt( this.r );
  271. this.g = Math.sqrt( this.g );
  272. this.b = Math.sqrt( this.b );
  273. return this;
  274. },
  275. getHex: function () {
  276. return ( this.r * 255 ) << 16 ^ ( this.g * 255 ) << 8 ^ ( this.b * 255 ) << 0;
  277. },
  278. getHexString: function () {
  279. return ( '000000' + this.getHex().toString( 16 ) ).slice( - 6 );
  280. },
  281. getHSL: function ( optionalTarget ) {
  282. // h,s,l ranges are in 0.0 - 1.0
  283. var hsl = optionalTarget || { h: 0, s: 0, l: 0 };
  284. var r = this.r, g = this.g, b = this.b;
  285. var max = Math.max( r, g, b );
  286. var min = Math.min( r, g, b );
  287. var hue, saturation;
  288. var lightness = ( min + max ) / 2.0;
  289. if ( min === max ) {
  290. hue = 0;
  291. saturation = 0;
  292. } else {
  293. var delta = max - min;
  294. saturation = lightness <= 0.5 ? delta / ( max + min ) : delta / ( 2 - max - min );
  295. switch ( max ) {
  296. case r: hue = ( g - b ) / delta + ( g < b ? 6 : 0 ); break;
  297. case g: hue = ( b - r ) / delta + 2; break;
  298. case b: hue = ( r - g ) / delta + 4; break;
  299. }
  300. hue /= 6;
  301. }
  302. hsl.h = hue;
  303. hsl.s = saturation;
  304. hsl.l = lightness;
  305. return hsl;
  306. },
  307. getStyle: function () {
  308. return 'rgb(' + ( ( this.r * 255 ) | 0 ) + ',' + ( ( this.g * 255 ) | 0 ) + ',' + ( ( this.b * 255 ) | 0 ) + ')';
  309. },
  310. offsetHSL: function ( h, s, l ) {
  311. var hsl = this.getHSL();
  312. hsl.h += h; hsl.s += s; hsl.l += l;
  313. this.setHSL( hsl.h, hsl.s, hsl.l );
  314. return this;
  315. },
  316. add: function ( color ) {
  317. this.r += color.r;
  318. this.g += color.g;
  319. this.b += color.b;
  320. return this;
  321. },
  322. addColors: function ( color1, color2 ) {
  323. this.r = color1.r + color2.r;
  324. this.g = color1.g + color2.g;
  325. this.b = color1.b + color2.b;
  326. return this;
  327. },
  328. addScalar: function ( s ) {
  329. this.r += s;
  330. this.g += s;
  331. this.b += s;
  332. return this;
  333. },
  334. multiply: function ( color ) {
  335. this.r *= color.r;
  336. this.g *= color.g;
  337. this.b *= color.b;
  338. return this;
  339. },
  340. multiplyScalar: function ( s ) {
  341. this.r *= s;
  342. this.g *= s;
  343. this.b *= s;
  344. return this;
  345. },
  346. lerp: function ( color, alpha ) {
  347. this.r += ( color.r - this.r ) * alpha;
  348. this.g += ( color.g - this.g ) * alpha;
  349. this.b += ( color.b - this.b ) * alpha;
  350. return this;
  351. },
  352. equals: function ( c ) {
  353. return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b );
  354. },
  355. fromArray: function ( array ) {
  356. this.r = array[ 0 ];
  357. this.g = array[ 1 ];
  358. this.b = array[ 2 ];
  359. return this;
  360. },
  361. toArray: function () {
  362. return [ this.r, this.g, this.b ];
  363. },
  364. clone: function () {
  365. return new THREE.Color().setRGB( this.r, this.g, this.b );
  366. }
  367. };
  368. THREE.ColorKeywords = { 'aliceblue': 0xF0F8FF, 'antiquewhite': 0xFAEBD7, 'aqua': 0x00FFFF, 'aquamarine': 0x7FFFD4, 'azure': 0xF0FFFF,
  369. 'beige': 0xF5F5DC, 'bisque': 0xFFE4C4, 'black': 0x000000, 'blanchedalmond': 0xFFEBCD, 'blue': 0x0000FF, 'blueviolet': 0x8A2BE2,
  370. 'brown': 0xA52A2A, 'burlywood': 0xDEB887, 'cadetblue': 0x5F9EA0, 'chartreuse': 0x7FFF00, 'chocolate': 0xD2691E, 'coral': 0xFF7F50,
  371. 'cornflowerblue': 0x6495ED, 'cornsilk': 0xFFF8DC, 'crimson': 0xDC143C, 'cyan': 0x00FFFF, 'darkblue': 0x00008B, 'darkcyan': 0x008B8B,
  372. 'darkgoldenrod': 0xB8860B, 'darkgray': 0xA9A9A9, 'darkgreen': 0x006400, 'darkgrey': 0xA9A9A9, 'darkkhaki': 0xBDB76B, 'darkmagenta': 0x8B008B,
  373. 'darkolivegreen': 0x556B2F, 'darkorange': 0xFF8C00, 'darkorchid': 0x9932CC, 'darkred': 0x8B0000, 'darksalmon': 0xE9967A, 'darkseagreen': 0x8FBC8F,
  374. 'darkslateblue': 0x483D8B, 'darkslategray': 0x2F4F4F, 'darkslategrey': 0x2F4F4F, 'darkturquoise': 0x00CED1, 'darkviolet': 0x9400D3,
  375. 'deeppink': 0xFF1493, 'deepskyblue': 0x00BFFF, 'dimgray': 0x696969, 'dimgrey': 0x696969, 'dodgerblue': 0x1E90FF, 'firebrick': 0xB22222,
  376. 'floralwhite': 0xFFFAF0, 'forestgreen': 0x228B22, 'fuchsia': 0xFF00FF, 'gainsboro': 0xDCDCDC, 'ghostwhite': 0xF8F8FF, 'gold': 0xFFD700,
  377. 'goldenrod': 0xDAA520, 'gray': 0x808080, 'green': 0x008000, 'greenyellow': 0xADFF2F, 'grey': 0x808080, 'honeydew': 0xF0FFF0, 'hotpink': 0xFF69B4,
  378. 'indianred': 0xCD5C5C, 'indigo': 0x4B0082, 'ivory': 0xFFFFF0, 'khaki': 0xF0E68C, 'lavender': 0xE6E6FA, 'lavenderblush': 0xFFF0F5, 'lawngreen': 0x7CFC00,
  379. 'lemonchiffon': 0xFFFACD, 'lightblue': 0xADD8E6, 'lightcoral': 0xF08080, 'lightcyan': 0xE0FFFF, 'lightgoldenrodyellow': 0xFAFAD2, 'lightgray': 0xD3D3D3,
  380. 'lightgreen': 0x90EE90, 'lightgrey': 0xD3D3D3, 'lightpink': 0xFFB6C1, 'lightsalmon': 0xFFA07A, 'lightseagreen': 0x20B2AA, 'lightskyblue': 0x87CEFA,
  381. 'lightslategray': 0x778899, 'lightslategrey': 0x778899, 'lightsteelblue': 0xB0C4DE, 'lightyellow': 0xFFFFE0, 'lime': 0x00FF00, 'limegreen': 0x32CD32,
  382. 'linen': 0xFAF0E6, 'magenta': 0xFF00FF, 'maroon': 0x800000, 'mediumaquamarine': 0x66CDAA, 'mediumblue': 0x0000CD, 'mediumorchid': 0xBA55D3,
  383. 'mediumpurple': 0x9370DB, 'mediumseagreen': 0x3CB371, 'mediumslateblue': 0x7B68EE, 'mediumspringgreen': 0x00FA9A, 'mediumturquoise': 0x48D1CC,
  384. 'mediumvioletred': 0xC71585, 'midnightblue': 0x191970, 'mintcream': 0xF5FFFA, 'mistyrose': 0xFFE4E1, 'moccasin': 0xFFE4B5, 'navajowhite': 0xFFDEAD,
  385. 'navy': 0x000080, 'oldlace': 0xFDF5E6, 'olive': 0x808000, 'olivedrab': 0x6B8E23, 'orange': 0xFFA500, 'orangered': 0xFF4500, 'orchid': 0xDA70D6,
  386. 'palegoldenrod': 0xEEE8AA, 'palegreen': 0x98FB98, 'paleturquoise': 0xAFEEEE, 'palevioletred': 0xDB7093, 'papayawhip': 0xFFEFD5, 'peachpuff': 0xFFDAB9,
  387. 'peru': 0xCD853F, 'pink': 0xFFC0CB, 'plum': 0xDDA0DD, 'powderblue': 0xB0E0E6, 'purple': 0x800080, 'red': 0xFF0000, 'rosybrown': 0xBC8F8F,
  388. 'royalblue': 0x4169E1, 'saddlebrown': 0x8B4513, 'salmon': 0xFA8072, 'sandybrown': 0xF4A460, 'seagreen': 0x2E8B57, 'seashell': 0xFFF5EE,
  389. 'sienna': 0xA0522D, 'silver': 0xC0C0C0, 'skyblue': 0x87CEEB, 'slateblue': 0x6A5ACD, 'slategray': 0x708090, 'slategrey': 0x708090, 'snow': 0xFFFAFA,
  390. 'springgreen': 0x00FF7F, 'steelblue': 0x4682B4, 'tan': 0xD2B48C, 'teal': 0x008080, 'thistle': 0xD8BFD8, 'tomato': 0xFF6347, 'turquoise': 0x40E0D0,
  391. 'violet': 0xEE82EE, 'wheat': 0xF5DEB3, 'white': 0xFFFFFF, 'whitesmoke': 0xF5F5F5, 'yellow': 0xFFFF00, 'yellowgreen': 0x9ACD32 };
  392. // File:src/math/Quaternion.js
  393. /**
  394. * @author mikael emtinger / http://gomo.se/
  395. * @author alteredq / http://alteredqualia.com/
  396. * @author WestLangley / http://github.com/WestLangley
  397. * @author bhouston / http://exocortex.com
  398. */
  399. THREE.Quaternion = function ( x, y, z, w ) {
  400. this._x = x || 0;
  401. this._y = y || 0;
  402. this._z = z || 0;
  403. this._w = ( w !== undefined ) ? w : 1;
  404. };
  405. THREE.Quaternion.prototype = {
  406. constructor: THREE.Quaternion,
  407. _x: 0,_y: 0, _z: 0, _w: 0,
  408. get x () {
  409. return this._x;
  410. },
  411. set x ( value ) {
  412. this._x = value;
  413. this.onChangeCallback();
  414. },
  415. get y () {
  416. return this._y;
  417. },
  418. set y ( value ) {
  419. this._y = value;
  420. this.onChangeCallback();
  421. },
  422. get z () {
  423. return this._z;
  424. },
  425. set z ( value ) {
  426. this._z = value;
  427. this.onChangeCallback();
  428. },
  429. get w () {
  430. return this._w;
  431. },
  432. set w ( value ) {
  433. this._w = value;
  434. this.onChangeCallback();
  435. },
  436. set: function ( x, y, z, w ) {
  437. this._x = x;
  438. this._y = y;
  439. this._z = z;
  440. this._w = w;
  441. this.onChangeCallback();
  442. return this;
  443. },
  444. copy: function ( quaternion ) {
  445. this._x = quaternion.x;
  446. this._y = quaternion.y;
  447. this._z = quaternion.z;
  448. this._w = quaternion.w;
  449. this.onChangeCallback();
  450. return this;
  451. },
  452. setFromEuler: function ( euler, update ) {
  453. if ( euler instanceof THREE.Euler === false ) {
  454. throw new Error( 'THREE.Quaternion: .setFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  455. }
  456. // http://www.mathworks.com/matlabcentral/fileexchange/
  457. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  458. // content/SpinCalc.m
  459. var c1 = Math.cos( euler._x / 2 );
  460. var c2 = Math.cos( euler._y / 2 );
  461. var c3 = Math.cos( euler._z / 2 );
  462. var s1 = Math.sin( euler._x / 2 );
  463. var s2 = Math.sin( euler._y / 2 );
  464. var s3 = Math.sin( euler._z / 2 );
  465. if ( euler.order === 'XYZ' ) {
  466. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  467. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  468. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  469. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  470. } else if ( euler.order === 'YXZ' ) {
  471. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  472. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  473. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  474. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  475. } else if ( euler.order === 'ZXY' ) {
  476. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  477. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  478. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  479. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  480. } else if ( euler.order === 'ZYX' ) {
  481. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  482. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  483. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  484. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  485. } else if ( euler.order === 'YZX' ) {
  486. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  487. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  488. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  489. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  490. } else if ( euler.order === 'XZY' ) {
  491. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  492. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  493. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  494. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  495. }
  496. if ( update !== false ) this.onChangeCallback();
  497. return this;
  498. },
  499. setFromAxisAngle: function ( axis, angle ) {
  500. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  501. // assumes axis is normalized
  502. var halfAngle = angle / 2, s = Math.sin( halfAngle );
  503. this._x = axis.x * s;
  504. this._y = axis.y * s;
  505. this._z = axis.z * s;
  506. this._w = Math.cos( halfAngle );
  507. this.onChangeCallback();
  508. return this;
  509. },
  510. setFromRotationMatrix: function ( m ) {
  511. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  512. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  513. var te = m.elements,
  514. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  515. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  516. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
  517. trace = m11 + m22 + m33,
  518. s;
  519. if ( trace > 0 ) {
  520. s = 0.5 / Math.sqrt( trace + 1.0 );
  521. this._w = 0.25 / s;
  522. this._x = ( m32 - m23 ) * s;
  523. this._y = ( m13 - m31 ) * s;
  524. this._z = ( m21 - m12 ) * s;
  525. } else if ( m11 > m22 && m11 > m33 ) {
  526. s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  527. this._w = ( m32 - m23 ) / s;
  528. this._x = 0.25 * s;
  529. this._y = ( m12 + m21 ) / s;
  530. this._z = ( m13 + m31 ) / s;
  531. } else if ( m22 > m33 ) {
  532. s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  533. this._w = ( m13 - m31 ) / s;
  534. this._x = ( m12 + m21 ) / s;
  535. this._y = 0.25 * s;
  536. this._z = ( m23 + m32 ) / s;
  537. } else {
  538. s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  539. this._w = ( m21 - m12 ) / s;
  540. this._x = ( m13 + m31 ) / s;
  541. this._y = ( m23 + m32 ) / s;
  542. this._z = 0.25 * s;
  543. }
  544. this.onChangeCallback();
  545. return this;
  546. },
  547. setFromUnitVectors: function () {
  548. // http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
  549. // assumes direction vectors vFrom and vTo are normalized
  550. var v1, r;
  551. var EPS = 0.000001;
  552. return function ( vFrom, vTo ) {
  553. if ( v1 === undefined ) v1 = new THREE.Vector3();
  554. r = vFrom.dot( vTo ) + 1;
  555. if ( r < EPS ) {
  556. r = 0;
  557. if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
  558. v1.set( - vFrom.y, vFrom.x, 0 );
  559. } else {
  560. v1.set( 0, - vFrom.z, vFrom.y );
  561. }
  562. } else {
  563. v1.crossVectors( vFrom, vTo );
  564. }
  565. this._x = v1.x;
  566. this._y = v1.y;
  567. this._z = v1.z;
  568. this._w = r;
  569. this.normalize();
  570. return this;
  571. }
  572. }(),
  573. inverse: function () {
  574. this.conjugate().normalize();
  575. return this;
  576. },
  577. conjugate: function () {
  578. this._x *= - 1;
  579. this._y *= - 1;
  580. this._z *= - 1;
  581. this.onChangeCallback();
  582. return this;
  583. },
  584. dot: function ( v ) {
  585. return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
  586. },
  587. lengthSq: function () {
  588. return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
  589. },
  590. length: function () {
  591. return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
  592. },
  593. normalize: function () {
  594. var l = this.length();
  595. if ( l === 0 ) {
  596. this._x = 0;
  597. this._y = 0;
  598. this._z = 0;
  599. this._w = 1;
  600. } else {
  601. l = 1 / l;
  602. this._x = this._x * l;
  603. this._y = this._y * l;
  604. this._z = this._z * l;
  605. this._w = this._w * l;
  606. }
  607. this.onChangeCallback();
  608. return this;
  609. },
  610. multiply: function ( q, p ) {
  611. if ( p !== undefined ) {
  612. console.warn( 'THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' );
  613. return this.multiplyQuaternions( q, p );
  614. }
  615. return this.multiplyQuaternions( this, q );
  616. },
  617. multiplyQuaternions: function ( a, b ) {
  618. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  619. var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
  620. var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
  621. this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  622. this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  623. this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  624. this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  625. this.onChangeCallback();
  626. return this;
  627. },
  628. multiplyVector3: function ( vector ) {
  629. console.warn( 'THREE.Quaternion: .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' );
  630. return vector.applyQuaternion( this );
  631. },
  632. slerp: function ( qb, t ) {
  633. if ( t === 0 ) return this;
  634. if ( t === 1 ) return this.copy( qb );
  635. var x = this._x, y = this._y, z = this._z, w = this._w;
  636. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  637. var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
  638. if ( cosHalfTheta < 0 ) {
  639. this._w = - qb._w;
  640. this._x = - qb._x;
  641. this._y = - qb._y;
  642. this._z = - qb._z;
  643. cosHalfTheta = - cosHalfTheta;
  644. } else {
  645. this.copy( qb );
  646. }
  647. if ( cosHalfTheta >= 1.0 ) {
  648. this._w = w;
  649. this._x = x;
  650. this._y = y;
  651. this._z = z;
  652. return this;
  653. }
  654. var halfTheta = Math.acos( cosHalfTheta );
  655. var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  656. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  657. this._w = 0.5 * ( w + this._w );
  658. this._x = 0.5 * ( x + this._x );
  659. this._y = 0.5 * ( y + this._y );
  660. this._z = 0.5 * ( z + this._z );
  661. return this;
  662. }
  663. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  664. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  665. this._w = ( w * ratioA + this._w * ratioB );
  666. this._x = ( x * ratioA + this._x * ratioB );
  667. this._y = ( y * ratioA + this._y * ratioB );
  668. this._z = ( z * ratioA + this._z * ratioB );
  669. this.onChangeCallback();
  670. return this;
  671. },
  672. equals: function ( quaternion ) {
  673. return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
  674. },
  675. fromArray: function ( array, offset ) {
  676. if ( offset === undefined ) offset = 0;
  677. this._x = array[ offset ];
  678. this._y = array[ offset + 1 ];
  679. this._z = array[ offset + 2 ];
  680. this._w = array[ offset + 3 ];
  681. this.onChangeCallback();
  682. return this;
  683. },
  684. toArray: function ( array, offset ) {
  685. if ( array === undefined ) array = [];
  686. if ( offset === undefined ) offset = 0;
  687. array[ offset ] = this._x;
  688. array[ offset + 1 ] = this._y;
  689. array[ offset + 2 ] = this._z;
  690. array[ offset + 3 ] = this._w;
  691. return array;
  692. },
  693. onChange: function ( callback ) {
  694. this.onChangeCallback = callback;
  695. return this;
  696. },
  697. onChangeCallback: function () {},
  698. clone: function () {
  699. return new THREE.Quaternion( this._x, this._y, this._z, this._w );
  700. }
  701. };
  702. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  703. return qm.copy( qa ).slerp( qb, t );
  704. }
  705. // File:src/math/Vector2.js
  706. /**
  707. * @author mrdoob / http://mrdoob.com/
  708. * @author philogb / http://blog.thejit.org/
  709. * @author egraether / http://egraether.com/
  710. * @author zz85 / http://www.lab4games.net/zz85/blog
  711. */
  712. THREE.Vector2 = function ( x, y ) {
  713. this.x = x || 0;
  714. this.y = y || 0;
  715. };
  716. THREE.Vector2.prototype = {
  717. constructor: THREE.Vector2,
  718. set: function ( x, y ) {
  719. this.x = x;
  720. this.y = y;
  721. return this;
  722. },
  723. setX: function ( x ) {
  724. this.x = x;
  725. return this;
  726. },
  727. setY: function ( y ) {
  728. this.y = y;
  729. return this;
  730. },
  731. setComponent: function ( index, value ) {
  732. switch ( index ) {
  733. case 0: this.x = value; break;
  734. case 1: this.y = value; break;
  735. default: throw new Error( 'index is out of range: ' + index );
  736. }
  737. },
  738. getComponent: function ( index ) {
  739. switch ( index ) {
  740. case 0: return this.x;
  741. case 1: return this.y;
  742. default: throw new Error( 'index is out of range: ' + index );
  743. }
  744. },
  745. copy: function ( v ) {
  746. this.x = v.x;
  747. this.y = v.y;
  748. return this;
  749. },
  750. add: function ( v, w ) {
  751. if ( w !== undefined ) {
  752. console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  753. return this.addVectors( v, w );
  754. }
  755. this.x += v.x;
  756. this.y += v.y;
  757. return this;
  758. },
  759. addVectors: function ( a, b ) {
  760. this.x = a.x + b.x;
  761. this.y = a.y + b.y;
  762. return this;
  763. },
  764. addScalar: function ( s ) {
  765. this.x += s;
  766. this.y += s;
  767. return this;
  768. },
  769. sub: function ( v, w ) {
  770. if ( w !== undefined ) {
  771. console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  772. return this.subVectors( v, w );
  773. }
  774. this.x -= v.x;
  775. this.y -= v.y;
  776. return this;
  777. },
  778. subVectors: function ( a, b ) {
  779. this.x = a.x - b.x;
  780. this.y = a.y - b.y;
  781. return this;
  782. },
  783. multiply: function ( v ) {
  784. this.x *= v.x;
  785. this.y *= v.y;
  786. return this;
  787. },
  788. multiplyScalar: function ( s ) {
  789. this.x *= s;
  790. this.y *= s;
  791. return this;
  792. },
  793. divide: function ( v ) {
  794. this.x /= v.x;
  795. this.y /= v.y;
  796. return this;
  797. },
  798. divideScalar: function ( scalar ) {
  799. if ( scalar !== 0 ) {
  800. var invScalar = 1 / scalar;
  801. this.x *= invScalar;
  802. this.y *= invScalar;
  803. } else {
  804. this.x = 0;
  805. this.y = 0;
  806. }
  807. return this;
  808. },
  809. min: function ( v ) {
  810. if ( this.x > v.x ) {
  811. this.x = v.x;
  812. }
  813. if ( this.y > v.y ) {
  814. this.y = v.y;
  815. }
  816. return this;
  817. },
  818. max: function ( v ) {
  819. if ( this.x < v.x ) {
  820. this.x = v.x;
  821. }
  822. if ( this.y < v.y ) {
  823. this.y = v.y;
  824. }
  825. return this;
  826. },
  827. clamp: function ( min, max ) {
  828. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  829. if ( this.x < min.x ) {
  830. this.x = min.x;
  831. } else if ( this.x > max.x ) {
  832. this.x = max.x;
  833. }
  834. if ( this.y < min.y ) {
  835. this.y = min.y;
  836. } else if ( this.y > max.y ) {
  837. this.y = max.y;
  838. }
  839. return this;
  840. },
  841. clampScalar: ( function () {
  842. var min, max;
  843. return function ( minVal, maxVal ) {
  844. if ( min === undefined ) {
  845. min = new THREE.Vector2();
  846. max = new THREE.Vector2();
  847. }
  848. min.set( minVal, minVal );
  849. max.set( maxVal, maxVal );
  850. return this.clamp( min, max );
  851. };
  852. } )(),
  853. floor: function () {
  854. this.x = Math.floor( this.x );
  855. this.y = Math.floor( this.y );
  856. return this;
  857. },
  858. ceil: function () {
  859. this.x = Math.ceil( this.x );
  860. this.y = Math.ceil( this.y );
  861. return this;
  862. },
  863. round: function () {
  864. this.x = Math.round( this.x );
  865. this.y = Math.round( this.y );
  866. return this;
  867. },
  868. roundToZero: function () {
  869. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  870. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  871. return this;
  872. },
  873. negate: function () {
  874. this.x = - this.x;
  875. this.y = - this.y;
  876. return this;
  877. },
  878. dot: function ( v ) {
  879. return this.x * v.x + this.y * v.y;
  880. },
  881. lengthSq: function () {
  882. return this.x * this.x + this.y * this.y;
  883. },
  884. length: function () {
  885. return Math.sqrt( this.x * this.x + this.y * this.y );
  886. },
  887. normalize: function () {
  888. return this.divideScalar( this.length() );
  889. },
  890. distanceTo: function ( v ) {
  891. return Math.sqrt( this.distanceToSquared( v ) );
  892. },
  893. distanceToSquared: function ( v ) {
  894. var dx = this.x - v.x, dy = this.y - v.y;
  895. return dx * dx + dy * dy;
  896. },
  897. setLength: function ( l ) {
  898. var oldLength = this.length();
  899. if ( oldLength !== 0 && l !== oldLength ) {
  900. this.multiplyScalar( l / oldLength );
  901. }
  902. return this;
  903. },
  904. lerp: function ( v, alpha ) {
  905. this.x += ( v.x - this.x ) * alpha;
  906. this.y += ( v.y - this.y ) * alpha;
  907. return this;
  908. },
  909. equals: function ( v ) {
  910. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  911. },
  912. fromArray: function ( array, offset ) {
  913. if ( offset === undefined ) offset = 0;
  914. this.x = array[ offset ];
  915. this.y = array[ offset + 1 ];
  916. return this;
  917. },
  918. toArray: function ( array, offset ) {
  919. if ( array === undefined ) array = [];
  920. if ( offset === undefined ) offset = 0;
  921. array[ offset ] = this.x;
  922. array[ offset + 1 ] = this.y;
  923. return array;
  924. },
  925. clone: function () {
  926. return new THREE.Vector2( this.x, this.y );
  927. }
  928. };
  929. // File:src/math/Vector3.js
  930. /**
  931. * @author mrdoob / http://mrdoob.com/
  932. * @author *kile / http://kile.stravaganza.org/
  933. * @author philogb / http://blog.thejit.org/
  934. * @author mikael emtinger / http://gomo.se/
  935. * @author egraether / http://egraether.com/
  936. * @author WestLangley / http://github.com/WestLangley
  937. */
  938. THREE.Vector3 = function ( x, y, z ) {
  939. this.x = x || 0;
  940. this.y = y || 0;
  941. this.z = z || 0;
  942. };
  943. THREE.Vector3.prototype = {
  944. constructor: THREE.Vector3,
  945. set: function ( x, y, z ) {
  946. this.x = x;
  947. this.y = y;
  948. this.z = z;
  949. return this;
  950. },
  951. setX: function ( x ) {
  952. this.x = x;
  953. return this;
  954. },
  955. setY: function ( y ) {
  956. this.y = y;
  957. return this;
  958. },
  959. setZ: function ( z ) {
  960. this.z = z;
  961. return this;
  962. },
  963. setComponent: function ( index, value ) {
  964. switch ( index ) {
  965. case 0: this.x = value; break;
  966. case 1: this.y = value; break;
  967. case 2: this.z = value; break;
  968. default: throw new Error( 'index is out of range: ' + index );
  969. }
  970. },
  971. getComponent: function ( index ) {
  972. switch ( index ) {
  973. case 0: return this.x;
  974. case 1: return this.y;
  975. case 2: return this.z;
  976. default: throw new Error( 'index is out of range: ' + index );
  977. }
  978. },
  979. copy: function ( v ) {
  980. this.x = v.x;
  981. this.y = v.y;
  982. this.z = v.z;
  983. return this;
  984. },
  985. add: function ( v, w ) {
  986. if ( w !== undefined ) {
  987. console.warn( 'THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  988. return this.addVectors( v, w );
  989. }
  990. this.x += v.x;
  991. this.y += v.y;
  992. this.z += v.z;
  993. return this;
  994. },
  995. addScalar: function ( s ) {
  996. this.x += s;
  997. this.y += s;
  998. this.z += s;
  999. return this;
  1000. },
  1001. addVectors: function ( a, b ) {
  1002. this.x = a.x + b.x;
  1003. this.y = a.y + b.y;
  1004. this.z = a.z + b.z;
  1005. return this;
  1006. },
  1007. sub: function ( v, w ) {
  1008. if ( w !== undefined ) {
  1009. console.warn( 'THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  1010. return this.subVectors( v, w );
  1011. }
  1012. this.x -= v.x;
  1013. this.y -= v.y;
  1014. this.z -= v.z;
  1015. return this;
  1016. },
  1017. subVectors: function ( a, b ) {
  1018. this.x = a.x - b.x;
  1019. this.y = a.y - b.y;
  1020. this.z = a.z - b.z;
  1021. return this;
  1022. },
  1023. multiply: function ( v, w ) {
  1024. if ( w !== undefined ) {
  1025. console.warn( 'THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
  1026. return this.multiplyVectors( v, w );
  1027. }
  1028. this.x *= v.x;
  1029. this.y *= v.y;
  1030. this.z *= v.z;
  1031. return this;
  1032. },
  1033. multiplyScalar: function ( scalar ) {
  1034. this.x *= scalar;
  1035. this.y *= scalar;
  1036. this.z *= scalar;
  1037. return this;
  1038. },
  1039. multiplyVectors: function ( a, b ) {
  1040. this.x = a.x * b.x;
  1041. this.y = a.y * b.y;
  1042. this.z = a.z * b.z;
  1043. return this;
  1044. },
  1045. applyEuler: function () {
  1046. var quaternion;
  1047. return function ( euler ) {
  1048. if ( euler instanceof THREE.Euler === false ) {
  1049. console.error( 'THREE.Vector3: .applyEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  1050. }
  1051. if ( quaternion === undefined ) quaternion = new THREE.Quaternion();
  1052. this.applyQuaternion( quaternion.setFromEuler( euler ) );
  1053. return this;
  1054. };
  1055. }(),
  1056. applyAxisAngle: function () {
  1057. var quaternion;
  1058. return function ( axis, angle ) {
  1059. if ( quaternion === undefined ) quaternion = new THREE.Quaternion();
  1060. this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );
  1061. return this;
  1062. };
  1063. }(),
  1064. applyMatrix3: function ( m ) {
  1065. var x = this.x;
  1066. var y = this.y;
  1067. var z = this.z;
  1068. var e = m.elements;
  1069. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
  1070. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
  1071. this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
  1072. return this;
  1073. },
  1074. applyMatrix4: function ( m ) {
  1075. // input: THREE.Matrix4 affine matrix
  1076. var x = this.x, y = this.y, z = this.z;
  1077. var e = m.elements;
  1078. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ];
  1079. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ];
  1080. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ];
  1081. return this;
  1082. },
  1083. applyProjection: function ( m ) {
  1084. // input: THREE.Matrix4 projection matrix
  1085. var x = this.x, y = this.y, z = this.z;
  1086. var e = m.elements;
  1087. var d = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] ); // perspective divide
  1088. this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * d;
  1089. this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * d;
  1090. this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * d;
  1091. return this;
  1092. },
  1093. applyQuaternion: function ( q ) {
  1094. var x = this.x;
  1095. var y = this.y;
  1096. var z = this.z;
  1097. var qx = q.x;
  1098. var qy = q.y;
  1099. var qz = q.z;
  1100. var qw = q.w;
  1101. // calculate quat * vector
  1102. var ix = qw * x + qy * z - qz * y;
  1103. var iy = qw * y + qz * x - qx * z;
  1104. var iz = qw * z + qx * y - qy * x;
  1105. var iw = - qx * x - qy * y - qz * z;
  1106. // calculate result * inverse quat
  1107. this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
  1108. this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
  1109. this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
  1110. return this;
  1111. },
  1112. project: function () {
  1113. var matrix;
  1114. return function ( camera ) {
  1115. if ( matrix === undefined ) matrix = new THREE.Matrix4();
  1116. matrix.multiplyMatrices( camera.projectionMatrix, matrix.getInverse( camera.matrixWorld ) );
  1117. return this.applyProjection( matrix );
  1118. };
  1119. }(),
  1120. unproject: function () {
  1121. var matrix;
  1122. return function ( camera ) {
  1123. if ( matrix === undefined ) matrix = new THREE.Matrix4();
  1124. matrix.multiplyMatrices( camera.matrixWorld, matrix.getInverse( camera.projectionMatrix ) );
  1125. return this.applyProjection( matrix );
  1126. };
  1127. }(),
  1128. transformDirection: function ( m ) {
  1129. // input: THREE.Matrix4 affine matrix
  1130. // vector interpreted as a direction
  1131. var x = this.x, y = this.y, z = this.z;
  1132. var e = m.elements;
  1133. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
  1134. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
  1135. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
  1136. this.normalize();
  1137. return this;
  1138. },
  1139. divide: function ( v ) {
  1140. this.x /= v.x;
  1141. this.y /= v.y;
  1142. this.z /= v.z;
  1143. return this;
  1144. },
  1145. divideScalar: function ( scalar ) {
  1146. if ( scalar !== 0 ) {
  1147. var invScalar = 1 / scalar;
  1148. this.x *= invScalar;
  1149. this.y *= invScalar;
  1150. this.z *= invScalar;
  1151. } else {
  1152. this.x = 0;
  1153. this.y = 0;
  1154. this.z = 0;
  1155. }
  1156. return this;
  1157. },
  1158. min: function ( v ) {
  1159. if ( this.x > v.x ) {
  1160. this.x = v.x;
  1161. }
  1162. if ( this.y > v.y ) {
  1163. this.y = v.y;
  1164. }
  1165. if ( this.z > v.z ) {
  1166. this.z = v.z;
  1167. }
  1168. return this;
  1169. },
  1170. max: function ( v ) {
  1171. if ( this.x < v.x ) {
  1172. this.x = v.x;
  1173. }
  1174. if ( this.y < v.y ) {
  1175. this.y = v.y;
  1176. }
  1177. if ( this.z < v.z ) {
  1178. this.z = v.z;
  1179. }
  1180. return this;
  1181. },
  1182. clamp: function ( min, max ) {
  1183. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  1184. if ( this.x < min.x ) {
  1185. this.x = min.x;
  1186. } else if ( this.x > max.x ) {
  1187. this.x = max.x;
  1188. }
  1189. if ( this.y < min.y ) {
  1190. this.y = min.y;
  1191. } else if ( this.y > max.y ) {
  1192. this.y = max.y;
  1193. }
  1194. if ( this.z < min.z ) {
  1195. this.z = min.z;
  1196. } else if ( this.z > max.z ) {
  1197. this.z = max.z;
  1198. }
  1199. return this;
  1200. },
  1201. clampScalar: ( function () {
  1202. var min, max;
  1203. return function ( minVal, maxVal ) {
  1204. if ( min === undefined ) {
  1205. min = new THREE.Vector3();
  1206. max = new THREE.Vector3();
  1207. }
  1208. min.set( minVal, minVal, minVal );
  1209. max.set( maxVal, maxVal, maxVal );
  1210. return this.clamp( min, max );
  1211. };
  1212. } )(),
  1213. floor: function () {
  1214. this.x = Math.floor( this.x );
  1215. this.y = Math.floor( this.y );
  1216. this.z = Math.floor( this.z );
  1217. return this;
  1218. },
  1219. ceil: function () {
  1220. this.x = Math.ceil( this.x );
  1221. this.y = Math.ceil( this.y );
  1222. this.z = Math.ceil( this.z );
  1223. return this;
  1224. },
  1225. round: function () {
  1226. this.x = Math.round( this.x );
  1227. this.y = Math.round( this.y );
  1228. this.z = Math.round( this.z );
  1229. return this;
  1230. },
  1231. roundToZero: function () {
  1232. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  1233. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  1234. this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
  1235. return this;
  1236. },
  1237. negate: function () {
  1238. this.x = - this.x;
  1239. this.y = - this.y;
  1240. this.z = - this.z;
  1241. return this;
  1242. },
  1243. dot: function ( v ) {
  1244. return this.x * v.x + this.y * v.y + this.z * v.z;
  1245. },
  1246. lengthSq: function () {
  1247. return this.x * this.x + this.y * this.y + this.z * this.z;
  1248. },
  1249. length: function () {
  1250. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  1251. },
  1252. lengthManhattan: function () {
  1253. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  1254. },
  1255. normalize: function () {
  1256. return this.divideScalar( this.length() );
  1257. },
  1258. setLength: function ( l ) {
  1259. var oldLength = this.length();
  1260. if ( oldLength !== 0 && l !== oldLength ) {
  1261. this.multiplyScalar( l / oldLength );
  1262. }
  1263. return this;
  1264. },
  1265. lerp: function ( v, alpha ) {
  1266. this.x += ( v.x - this.x ) * alpha;
  1267. this.y += ( v.y - this.y ) * alpha;
  1268. this.z += ( v.z - this.z ) * alpha;
  1269. return this;
  1270. },
  1271. cross: function ( v, w ) {
  1272. if ( w !== undefined ) {
  1273. console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
  1274. return this.crossVectors( v, w );
  1275. }
  1276. var x = this.x, y = this.y, z = this.z;
  1277. this.x = y * v.z - z * v.y;
  1278. this.y = z * v.x - x * v.z;
  1279. this.z = x * v.y - y * v.x;
  1280. return this;
  1281. },
  1282. crossVectors: function ( a, b ) {
  1283. var ax = a.x, ay = a.y, az = a.z;
  1284. var bx = b.x, by = b.y, bz = b.z;
  1285. this.x = ay * bz - az * by;
  1286. this.y = az * bx - ax * bz;
  1287. this.z = ax * by - ay * bx;
  1288. return this;
  1289. },
  1290. projectOnVector: function () {
  1291. var v1, dot;
  1292. return function ( vector ) {
  1293. if ( v1 === undefined ) v1 = new THREE.Vector3();
  1294. v1.copy( vector ).normalize();
  1295. dot = this.dot( v1 );
  1296. return this.copy( v1 ).multiplyScalar( dot );
  1297. };
  1298. }(),
  1299. projectOnPlane: function () {
  1300. var v1;
  1301. return function ( planeNormal ) {
  1302. if ( v1 === undefined ) v1 = new THREE.Vector3();
  1303. v1.copy( this ).projectOnVector( planeNormal );
  1304. return this.sub( v1 );
  1305. }
  1306. }(),
  1307. reflect: function () {
  1308. // reflect incident vector off plane orthogonal to normal
  1309. // normal is assumed to have unit length
  1310. var v1;
  1311. return function ( normal ) {
  1312. if ( v1 === undefined ) v1 = new THREE.Vector3();
  1313. return this.sub( v1.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
  1314. }
  1315. }(),
  1316. angleTo: function ( v ) {
  1317. var theta = this.dot( v ) / ( this.length() * v.length() );
  1318. // clamp, to handle numerical problems
  1319. return Math.acos( THREE.Math.clamp( theta, - 1, 1 ) );
  1320. },
  1321. distanceTo: function ( v ) {
  1322. return Math.sqrt( this.distanceToSquared( v ) );
  1323. },
  1324. distanceToSquared: function ( v ) {
  1325. var dx = this.x - v.x;
  1326. var dy = this.y - v.y;
  1327. var dz = this.z - v.z;
  1328. return dx * dx + dy * dy + dz * dz;
  1329. },
  1330. setEulerFromRotationMatrix: function ( m, order ) {
  1331. console.error( 'THREE.Vector3: .setEulerFromRotationMatrix() has been removed. Use Euler.setFromRotationMatrix() instead.' );
  1332. },
  1333. setEulerFromQuaternion: function ( q, order ) {
  1334. console.error( 'THREE.Vector3: .setEulerFromQuaternion() has been removed. Use Euler.setFromQuaternion() instead.' );
  1335. },
  1336. getPositionFromMatrix: function ( m ) {
  1337. console.warn( 'THREE.Vector3: .getPositionFromMatrix() has been renamed to .setFromMatrixPosition().' );
  1338. return this.setFromMatrixPosition( m );
  1339. },
  1340. getScaleFromMatrix: function ( m ) {
  1341. console.warn( 'THREE.Vector3: .getScaleFromMatrix() has been renamed to .setFromMatrixScale().' );
  1342. return this.setFromMatrixScale( m );
  1343. },
  1344. getColumnFromMatrix: function ( index, matrix ) {
  1345. console.warn( 'THREE.Vector3: .getColumnFromMatrix() has been renamed to .setFromMatrixColumn().' );
  1346. return this.setFromMatrixColumn( index, matrix );
  1347. },
  1348. setFromMatrixPosition: function ( m ) {
  1349. this.x = m.elements[ 12 ];
  1350. this.y = m.elements[ 13 ];
  1351. this.z = m.elements[ 14 ];
  1352. return this;
  1353. },
  1354. setFromMatrixScale: function ( m ) {
  1355. var sx = this.set( m.elements[ 0 ], m.elements[ 1 ], m.elements[ 2 ] ).length();
  1356. var sy = this.set( m.elements[ 4 ], m.elements[ 5 ], m.elements[ 6 ] ).length();
  1357. var sz = this.set( m.elements[ 8 ], m.elements[ 9 ], m.elements[ 10 ] ).length();
  1358. this.x = sx;
  1359. this.y = sy;
  1360. this.z = sz;
  1361. return this;
  1362. },
  1363. setFromMatrixColumn: function ( index, matrix ) {
  1364. var offset = index * 4;
  1365. var me = matrix.elements;
  1366. this.x = me[ offset ];
  1367. this.y = me[ offset + 1 ];
  1368. this.z = me[ offset + 2 ];
  1369. return this;
  1370. },
  1371. equals: function ( v ) {
  1372. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  1373. },
  1374. fromArray: function ( array, offset ) {
  1375. if ( offset === undefined ) offset = 0;
  1376. this.x = array[ offset ];
  1377. this.y = array[ offset + 1 ];
  1378. this.z = array[ offset + 2 ];
  1379. return this;
  1380. },
  1381. toArray: function ( array, offset ) {
  1382. if ( array === undefined ) array = [];
  1383. if ( offset === undefined ) offset = 0;
  1384. array[ offset ] = this.x;
  1385. array[ offset + 1 ] = this.y;
  1386. array[ offset + 2 ] = this.z;
  1387. return array;
  1388. },
  1389. clone: function () {
  1390. return new THREE.Vector3( this.x, this.y, this.z );
  1391. }
  1392. };
  1393. // File:src/math/Vector4.js
  1394. /**
  1395. * @author supereggbert / http://www.paulbrunt.co.uk/
  1396. * @author philogb / http://blog.thejit.org/
  1397. * @author mikael emtinger / http://gomo.se/
  1398. * @author egraether / http://egraether.com/
  1399. * @author WestLangley / http://github.com/WestLangley
  1400. */
  1401. THREE.Vector4 = function ( x, y, z, w ) {
  1402. this.x = x || 0;
  1403. this.y = y || 0;
  1404. this.z = z || 0;
  1405. this.w = ( w !== undefined ) ? w : 1;
  1406. };
  1407. THREE.Vector4.prototype = {
  1408. constructor: THREE.Vector4,
  1409. set: function ( x, y, z, w ) {
  1410. this.x = x;
  1411. this.y = y;
  1412. this.z = z;
  1413. this.w = w;
  1414. return this;
  1415. },
  1416. setX: function ( x ) {
  1417. this.x = x;
  1418. return this;
  1419. },
  1420. setY: function ( y ) {
  1421. this.y = y;
  1422. return this;
  1423. },
  1424. setZ: function ( z ) {
  1425. this.z = z;
  1426. return this;
  1427. },
  1428. setW: function ( w ) {
  1429. this.w = w;
  1430. return this;
  1431. },
  1432. setComponent: function ( index, value ) {
  1433. switch ( index ) {
  1434. case 0: this.x = value; break;
  1435. case 1: this.y = value; break;
  1436. case 2: this.z = value; break;
  1437. case 3: this.w = value; break;
  1438. default: throw new Error( 'index is out of range: ' + index );
  1439. }
  1440. },
  1441. getComponent: function ( index ) {
  1442. switch ( index ) {
  1443. case 0: return this.x;
  1444. case 1: return this.y;
  1445. case 2: return this.z;
  1446. case 3: return this.w;
  1447. default: throw new Error( 'index is out of range: ' + index );
  1448. }
  1449. },
  1450. copy: function ( v ) {
  1451. this.x = v.x;
  1452. this.y = v.y;
  1453. this.z = v.z;
  1454. this.w = ( v.w !== undefined ) ? v.w : 1;
  1455. return this;
  1456. },
  1457. add: function ( v, w ) {
  1458. if ( w !== undefined ) {
  1459. console.warn( 'THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  1460. return this.addVectors( v, w );
  1461. }
  1462. this.x += v.x;
  1463. this.y += v.y;
  1464. this.z += v.z;
  1465. this.w += v.w;
  1466. return this;
  1467. },
  1468. addScalar: function ( s ) {
  1469. this.x += s;
  1470. this.y += s;
  1471. this.z += s;
  1472. this.w += s;
  1473. return this;
  1474. },
  1475. addVectors: function ( a, b ) {
  1476. this.x = a.x + b.x;
  1477. this.y = a.y + b.y;
  1478. this.z = a.z + b.z;
  1479. this.w = a.w + b.w;
  1480. return this;
  1481. },
  1482. sub: function ( v, w ) {
  1483. if ( w !== undefined ) {
  1484. console.warn( 'THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  1485. return this.subVectors( v, w );
  1486. }
  1487. this.x -= v.x;
  1488. this.y -= v.y;
  1489. this.z -= v.z;
  1490. this.w -= v.w;
  1491. return this;
  1492. },
  1493. subVectors: function ( a, b ) {
  1494. this.x = a.x - b.x;
  1495. this.y = a.y - b.y;
  1496. this.z = a.z - b.z;
  1497. this.w = a.w - b.w;
  1498. return this;
  1499. },
  1500. multiplyScalar: function ( scalar ) {
  1501. this.x *= scalar;
  1502. this.y *= scalar;
  1503. this.z *= scalar;
  1504. this.w *= scalar;
  1505. return this;
  1506. },
  1507. applyMatrix4: function ( m ) {
  1508. var x = this.x;
  1509. var y = this.y;
  1510. var z = this.z;
  1511. var w = this.w;
  1512. var e = m.elements;
  1513. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w;
  1514. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w;
  1515. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w;
  1516. this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
  1517. return this;
  1518. },
  1519. divideScalar: function ( scalar ) {
  1520. if ( scalar !== 0 ) {
  1521. var invScalar = 1 / scalar;
  1522. this.x *= invScalar;
  1523. this.y *= invScalar;
  1524. this.z *= invScalar;
  1525. this.w *= invScalar;
  1526. } else {
  1527. this.x = 0;
  1528. this.y = 0;
  1529. this.z = 0;
  1530. this.w = 1;
  1531. }
  1532. return this;
  1533. },
  1534. setAxisAngleFromQuaternion: function ( q ) {
  1535. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
  1536. // q is assumed to be normalized
  1537. this.w = 2 * Math.acos( q.w );
  1538. var s = Math.sqrt( 1 - q.w * q.w );
  1539. if ( s < 0.0001 ) {
  1540. this.x = 1;
  1541. this.y = 0;
  1542. this.z = 0;
  1543. } else {
  1544. this.x = q.x / s;
  1545. this.y = q.y / s;
  1546. this.z = q.z / s;
  1547. }
  1548. return this;
  1549. },
  1550. setAxisAngleFromRotationMatrix: function ( m ) {
  1551. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
  1552. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  1553. var angle, x, y, z, // variables for result
  1554. epsilon = 0.01, // margin to allow for rounding errors
  1555. epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees
  1556. te = m.elements,
  1557. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  1558. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  1559. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  1560. if ( ( Math.abs( m12 - m21 ) < epsilon )
  1561. && ( Math.abs( m13 - m31 ) < epsilon )
  1562. && ( Math.abs( m23 - m32 ) < epsilon ) ) {
  1563. // singularity found
  1564. // first check for identity matrix which must have +1 for all terms
  1565. // in leading diagonal and zero in other terms
  1566. if ( ( Math.abs( m12 + m21 ) < epsilon2 )
  1567. && ( Math.abs( m13 + m31 ) < epsilon2 )
  1568. && ( Math.abs( m23 + m32 ) < epsilon2 )
  1569. && ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
  1570. // this singularity is identity matrix so angle = 0
  1571. this.set( 1, 0, 0, 0 );
  1572. return this; // zero angle, arbitrary axis
  1573. }
  1574. // otherwise this singularity is angle = 180
  1575. angle = Math.PI;
  1576. var xx = ( m11 + 1 ) / 2;
  1577. var yy = ( m22 + 1 ) / 2;
  1578. var zz = ( m33 + 1 ) / 2;
  1579. var xy = ( m12 + m21 ) / 4;
  1580. var xz = ( m13 + m31 ) / 4;
  1581. var yz = ( m23 + m32 ) / 4;
  1582. if ( ( xx > yy ) && ( xx > zz ) ) { // m11 is the largest diagonal term
  1583. if ( xx < epsilon ) {
  1584. x = 0;
  1585. y = 0.707106781;
  1586. z = 0.707106781;
  1587. } else {
  1588. x = Math.sqrt( xx );
  1589. y = xy / x;
  1590. z = xz / x;
  1591. }
  1592. } else if ( yy > zz ) { // m22 is the largest diagonal term
  1593. if ( yy < epsilon ) {
  1594. x = 0.707106781;
  1595. y = 0;
  1596. z = 0.707106781;
  1597. } else {
  1598. y = Math.sqrt( yy );
  1599. x = xy / y;
  1600. z = yz / y;
  1601. }
  1602. } else { // m33 is the largest diagonal term so base result on this
  1603. if ( zz < epsilon ) {
  1604. x = 0.707106781;
  1605. y = 0.707106781;
  1606. z = 0;
  1607. } else {
  1608. z = Math.sqrt( zz );
  1609. x = xz / z;
  1610. y = yz / z;
  1611. }
  1612. }
  1613. this.set( x, y, z, angle );
  1614. return this; // return 180 deg rotation
  1615. }
  1616. // as we have reached here there are no singularities so we can handle normally
  1617. var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 )
  1618. + ( m13 - m31 ) * ( m13 - m31 )
  1619. + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
  1620. if ( Math.abs( s ) < 0.001 ) s = 1;
  1621. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  1622. // caught by singularity test above, but I've left it in just in case
  1623. this.x = ( m32 - m23 ) / s;
  1624. this.y = ( m13 - m31 ) / s;
  1625. this.z = ( m21 - m12 ) / s;
  1626. this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
  1627. return this;
  1628. },
  1629. min: function ( v ) {
  1630. if ( this.x > v.x ) {
  1631. this.x = v.x;
  1632. }
  1633. if ( this.y > v.y ) {
  1634. this.y = v.y;
  1635. }
  1636. if ( this.z > v.z ) {
  1637. this.z = v.z;
  1638. }
  1639. if ( this.w > v.w ) {
  1640. this.w = v.w;
  1641. }
  1642. return this;
  1643. },
  1644. max: function ( v ) {
  1645. if ( this.x < v.x ) {
  1646. this.x = v.x;
  1647. }
  1648. if ( this.y < v.y ) {
  1649. this.y = v.y;
  1650. }
  1651. if ( this.z < v.z ) {
  1652. this.z = v.z;
  1653. }
  1654. if ( this.w < v.w ) {
  1655. this.w = v.w;
  1656. }
  1657. return this;
  1658. },
  1659. clamp: function ( min, max ) {
  1660. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  1661. if ( this.x < min.x ) {
  1662. this.x = min.x;
  1663. } else if ( this.x > max.x ) {
  1664. this.x = max.x;
  1665. }
  1666. if ( this.y < min.y ) {
  1667. this.y = min.y;
  1668. } else if ( this.y > max.y ) {
  1669. this.y = max.y;
  1670. }
  1671. if ( this.z < min.z ) {
  1672. this.z = min.z;
  1673. } else if ( this.z > max.z ) {
  1674. this.z = max.z;
  1675. }
  1676. if ( this.w < min.w ) {
  1677. this.w = min.w;
  1678. } else if ( this.w > max.w ) {
  1679. this.w = max.w;
  1680. }
  1681. return this;
  1682. },
  1683. clampScalar: ( function () {
  1684. var min, max;
  1685. return function ( minVal, maxVal ) {
  1686. if ( min === undefined ) {
  1687. min = new THREE.Vector4();
  1688. max = new THREE.Vector4();
  1689. }
  1690. min.set( minVal, minVal, minVal, minVal );
  1691. max.set( maxVal, maxVal, maxVal, maxVal );
  1692. return this.clamp( min, max );
  1693. };
  1694. } )(),
  1695. floor: function () {
  1696. this.x = Math.floor( this.x );
  1697. this.y = Math.floor( this.y );
  1698. this.z = Math.floor( this.z );
  1699. this.w = Math.floor( this.w );
  1700. return this;
  1701. },
  1702. ceil: function () {
  1703. this.x = Math.ceil( this.x );
  1704. this.y = Math.ceil( this.y );
  1705. this.z = Math.ceil( this.z );
  1706. this.w = Math.ceil( this.w );
  1707. return this;
  1708. },
  1709. round: function () {
  1710. this.x = Math.round( this.x );
  1711. this.y = Math.round( this.y );
  1712. this.z = Math.round( this.z );
  1713. this.w = Math.round( this.w );
  1714. return this;
  1715. },
  1716. roundToZero: function () {
  1717. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  1718. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  1719. this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
  1720. this.w = ( this.w < 0 ) ? Math.ceil( this.w ) : Math.floor( this.w );
  1721. return this;
  1722. },
  1723. negate: function () {
  1724. this.x = - this.x;
  1725. this.y = - this.y;
  1726. this.z = - this.z;
  1727. this.w = - this.w;
  1728. return this;
  1729. },
  1730. dot: function ( v ) {
  1731. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  1732. },
  1733. lengthSq: function () {
  1734. return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  1735. },
  1736. length: function () {
  1737. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  1738. },
  1739. lengthManhattan: function () {
  1740. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
  1741. },
  1742. normalize: function () {
  1743. return this.divideScalar( this.length() );
  1744. },
  1745. setLength: function ( l ) {
  1746. var oldLength = this.length();
  1747. if ( oldLength !== 0 && l !== oldLength ) {
  1748. this.multiplyScalar( l / oldLength );
  1749. }
  1750. return this;
  1751. },
  1752. lerp: function ( v, alpha ) {
  1753. this.x += ( v.x - this.x ) * alpha;
  1754. this.y += ( v.y - this.y ) * alpha;
  1755. this.z += ( v.z - this.z ) * alpha;
  1756. this.w += ( v.w - this.w ) * alpha;
  1757. return this;
  1758. },
  1759. equals: function ( v ) {
  1760. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
  1761. },
  1762. fromArray: function ( array, offset ) {
  1763. if ( offset === undefined ) offset = 0;
  1764. this.x = array[ offset ];
  1765. this.y = array[ offset + 1 ];
  1766. this.z = array[ offset + 2 ];
  1767. this.w = array[ offset + 3 ];
  1768. return this;
  1769. },
  1770. toArray: function ( array, offset ) {
  1771. if ( array === undefined ) array = [];
  1772. if ( offset === undefined ) offset = 0;
  1773. array[ offset ] = this.x;
  1774. array[ offset + 1 ] = this.y;
  1775. array[ offset + 2 ] = this.z;
  1776. array[ offset + 3 ] = this.w;
  1777. return array;
  1778. },
  1779. clone: function () {
  1780. return new THREE.Vector4( this.x, this.y, this.z, this.w );
  1781. }
  1782. };
  1783. // File:src/math/Euler.js
  1784. /**
  1785. * @author mrdoob / http://mrdoob.com/
  1786. * @author WestLangley / http://github.com/WestLangley
  1787. * @author bhouston / http://exocortex.com
  1788. */
  1789. THREE.Euler = function ( x, y, z, order ) {
  1790. this._x = x || 0;
  1791. this._y = y || 0;
  1792. this._z = z || 0;
  1793. this._order = order || THREE.Euler.DefaultOrder;
  1794. };
  1795. THREE.Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];
  1796. THREE.Euler.DefaultOrder = 'XYZ';
  1797. THREE.Euler.prototype = {
  1798. constructor: THREE.Euler,
  1799. _x: 0, _y: 0, _z: 0, _order: THREE.Euler.DefaultOrder,
  1800. get x () {
  1801. return this._x;
  1802. },
  1803. set x ( value ) {
  1804. this._x = value;
  1805. this.onChangeCallback();
  1806. },
  1807. get y () {
  1808. return this._y;
  1809. },
  1810. set y ( value ) {
  1811. this._y = value;
  1812. this.onChangeCallback();
  1813. },
  1814. get z () {
  1815. return this._z;
  1816. },
  1817. set z ( value ) {
  1818. this._z = value;
  1819. this.onChangeCallback();
  1820. },
  1821. get order () {
  1822. return this._order;
  1823. },
  1824. set order ( value ) {
  1825. this._order = value;
  1826. this.onChangeCallback();
  1827. },
  1828. set: function ( x, y, z, order ) {
  1829. this._x = x;
  1830. this._y = y;
  1831. this._z = z;
  1832. this._order = order || this._order;
  1833. this.onChangeCallback();
  1834. return this;
  1835. },
  1836. copy: function ( euler ) {
  1837. this._x = euler._x;
  1838. this._y = euler._y;
  1839. this._z = euler._z;
  1840. this._order = euler._order;
  1841. this.onChangeCallback();
  1842. return this;
  1843. },
  1844. setFromRotationMatrix: function ( m, order, update ) {
  1845. var clamp = THREE.Math.clamp;
  1846. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  1847. var te = m.elements;
  1848. var m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
  1849. var m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
  1850. var m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  1851. order = order || this._order;
  1852. if ( order === 'XYZ' ) {
  1853. this._y = Math.asin( clamp( m13, - 1, 1 ) );
  1854. if ( Math.abs( m13 ) < 0.99999 ) {
  1855. this._x = Math.atan2( - m23, m33 );
  1856. this._z = Math.atan2( - m12, m11 );
  1857. } else {
  1858. this._x = Math.atan2( m32, m22 );
  1859. this._z = 0;
  1860. }
  1861. } else if ( order === 'YXZ' ) {
  1862. this._x = Math.asin( - clamp( m23, - 1, 1 ) );
  1863. if ( Math.abs( m23 ) < 0.99999 ) {
  1864. this._y = Math.atan2( m13, m33 );
  1865. this._z = Math.atan2( m21, m22 );
  1866. } else {
  1867. this._y = Math.atan2( - m31, m11 );
  1868. this._z = 0;
  1869. }
  1870. } else if ( order === 'ZXY' ) {
  1871. this._x = Math.asin( clamp( m32, - 1, 1 ) );
  1872. if ( Math.abs( m32 ) < 0.99999 ) {
  1873. this._y = Math.atan2( - m31, m33 );
  1874. this._z = Math.atan2( - m12, m22 );
  1875. } else {
  1876. this._y = 0;
  1877. this._z = Math.atan2( m21, m11 );
  1878. }
  1879. } else if ( order === 'ZYX' ) {
  1880. this._y = Math.asin( - clamp( m31, - 1, 1 ) );
  1881. if ( Math.abs( m31 ) < 0.99999 ) {
  1882. this._x = Math.atan2( m32, m33 );
  1883. this._z = Math.atan2( m21, m11 );
  1884. } else {
  1885. this._x = 0;
  1886. this._z = Math.atan2( - m12, m22 );
  1887. }
  1888. } else if ( order === 'YZX' ) {
  1889. this._z = Math.asin( clamp( m21, - 1, 1 ) );
  1890. if ( Math.abs( m21 ) < 0.99999 ) {
  1891. this._x = Math.atan2( - m23, m22 );
  1892. this._y = Math.atan2( - m31, m11 );
  1893. } else {
  1894. this._x = 0;
  1895. this._y = Math.atan2( m13, m33 );
  1896. }
  1897. } else if ( order === 'XZY' ) {
  1898. this._z = Math.asin( - clamp( m12, - 1, 1 ) );
  1899. if ( Math.abs( m12 ) < 0.99999 ) {
  1900. this._x = Math.atan2( m32, m22 );
  1901. this._y = Math.atan2( m13, m11 );
  1902. } else {
  1903. this._x = Math.atan2( - m23, m33 );
  1904. this._y = 0;
  1905. }
  1906. } else {
  1907. console.warn( 'THREE.Euler: .setFromRotationMatrix() given unsupported order: ' + order )
  1908. }
  1909. this._order = order;
  1910. if ( update !== false ) this.onChangeCallback();
  1911. return this;
  1912. },
  1913. setFromQuaternion: function () {
  1914. var matrix;
  1915. return function ( q, order, update ) {
  1916. if ( matrix === undefined ) matrix = new THREE.Matrix4();
  1917. matrix.makeRotationFromQuaternion( q );
  1918. this.setFromRotationMatrix( matrix, order, update );
  1919. return this;
  1920. };
  1921. }(),
  1922. reorder: function () {
  1923. // WARNING: this discards revolution information -bhouston
  1924. var q = new THREE.Quaternion();
  1925. return function ( newOrder ) {
  1926. q.setFromEuler( this );
  1927. this.setFromQuaternion( q, newOrder );
  1928. };
  1929. }(),
  1930. equals: function ( euler ) {
  1931. return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
  1932. },
  1933. fromArray: function ( array ) {
  1934. this._x = array[ 0 ];
  1935. this._y = array[ 1 ];
  1936. this._z = array[ 2 ];
  1937. if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
  1938. this.onChangeCallback();
  1939. return this;
  1940. },
  1941. toArray: function () {
  1942. return [ this._x, this._y, this._z, this._order ];
  1943. },
  1944. onChange: function ( callback ) {
  1945. this.onChangeCallback = callback;
  1946. return this;
  1947. },
  1948. onChangeCallback: function () {},
  1949. clone: function () {
  1950. return new THREE.Euler( this._x, this._y, this._z, this._order );
  1951. }
  1952. };
  1953. // File:src/math/Line3.js
  1954. /**
  1955. * @author bhouston / http://exocortex.com
  1956. */
  1957. THREE.Line3 = function ( start, end ) {
  1958. this.start = ( start !== undefined ) ? start : new THREE.Vector3();
  1959. this.end = ( end !== undefined ) ? end : new THREE.Vector3();
  1960. };
  1961. THREE.Line3.prototype = {
  1962. constructor: THREE.Line3,
  1963. set: function ( start, end ) {
  1964. this.start.copy( start );
  1965. this.end.copy( end );
  1966. return this;
  1967. },
  1968. copy: function ( line ) {
  1969. this.start.copy( line.start );
  1970. this.end.copy( line.end );
  1971. return this;
  1972. },
  1973. center: function ( optionalTarget ) {
  1974. var result = optionalTarget || new THREE.Vector3();
  1975. return result.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
  1976. },
  1977. delta: function ( optionalTarget ) {
  1978. var result = optionalTarget || new THREE.Vector3();
  1979. return result.subVectors( this.end, this.start );
  1980. },
  1981. distanceSq: function () {
  1982. return this.start.distanceToSquared( this.end );
  1983. },
  1984. distance: function () {
  1985. return this.start.distanceTo( this.end );
  1986. },
  1987. at: function ( t, optionalTarget ) {
  1988. var result = optionalTarget || new THREE.Vector3();
  1989. return this.delta( result ).multiplyScalar( t ).add( this.start );
  1990. },
  1991. closestPointToPointParameter: function () {
  1992. var startP = new THREE.Vector3();
  1993. var startEnd = new THREE.Vector3();
  1994. return function ( point, clampToLine ) {
  1995. startP.subVectors( point, this.start );
  1996. startEnd.subVectors( this.end, this.start );
  1997. var startEnd2 = startEnd.dot( startEnd );
  1998. var startEnd_startP = startEnd.dot( startP );
  1999. var t = startEnd_startP / startEnd2;
  2000. if ( clampToLine ) {
  2001. t = THREE.Math.clamp( t, 0, 1 );
  2002. }
  2003. return t;
  2004. };
  2005. }(),
  2006. closestPointToPoint: function ( point, clampToLine, optionalTarget ) {
  2007. var t = this.closestPointToPointParameter( point, clampToLine );
  2008. var result = optionalTarget || new THREE.Vector3();
  2009. return this.delta( result ).multiplyScalar( t ).add( this.start );
  2010. },
  2011. applyMatrix4: function ( matrix ) {
  2012. this.start.applyMatrix4( matrix );
  2013. this.end.applyMatrix4( matrix );
  2014. return this;
  2015. },
  2016. equals: function ( line ) {
  2017. return line.start.equals( this.start ) && line.end.equals( this.end );
  2018. },
  2019. clone: function () {
  2020. return new THREE.Line3().copy( this );
  2021. }
  2022. };
  2023. // File:src/math/Box2.js
  2024. /**
  2025. * @author bhouston / http://exocortex.com
  2026. */
  2027. THREE.Box2 = function ( min, max ) {
  2028. this.min = ( min !== undefined ) ? min : new THREE.Vector2( Infinity, Infinity );
  2029. this.max = ( max !== undefined ) ? max : new THREE.Vector2( - Infinity, - Infinity );
  2030. };
  2031. THREE.Box2.prototype = {
  2032. constructor: THREE.Box2,
  2033. set: function ( min, max ) {
  2034. this.min.copy( min );
  2035. this.max.copy( max );
  2036. return this;
  2037. },
  2038. setFromPoints: function ( points ) {
  2039. this.makeEmpty();
  2040. for ( var i = 0, il = points.length; i < il; i ++ ) {
  2041. this.expandByPoint( points[ i ] )
  2042. }
  2043. return this;
  2044. },
  2045. setFromCenterAndSize: function () {
  2046. var v1 = new THREE.Vector2();
  2047. return function ( center, size ) {
  2048. var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
  2049. this.min.copy( center ).sub( halfSize );
  2050. this.max.copy( center ).add( halfSize );
  2051. return this;
  2052. };
  2053. }(),
  2054. copy: function ( box ) {
  2055. this.min.copy( box.min );
  2056. this.max.copy( box.max );
  2057. return this;
  2058. },
  2059. makeEmpty: function () {
  2060. this.min.x = this.min.y = Infinity;
  2061. this.max.x = this.max.y = - Infinity;
  2062. return this;
  2063. },
  2064. empty: function () {
  2065. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  2066. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
  2067. },
  2068. center: function ( optionalTarget ) {
  2069. var result = optionalTarget || new THREE.Vector2();
  2070. return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  2071. },
  2072. size: function ( optionalTarget ) {
  2073. var result = optionalTarget || new THREE.Vector2();
  2074. return result.subVectors( this.max, this.min );
  2075. },
  2076. expandByPoint: function ( point ) {
  2077. this.min.min( point );
  2078. this.max.max( point );
  2079. return this;
  2080. },
  2081. expandByVector: function ( vector ) {
  2082. this.min.sub( vector );
  2083. this.max.add( vector );
  2084. return this;
  2085. },
  2086. expandByScalar: function ( scalar ) {
  2087. this.min.addScalar( - scalar );
  2088. this.max.addScalar( scalar );
  2089. return this;
  2090. },
  2091. containsPoint: function ( point ) {
  2092. if ( point.x < this.min.x || point.x > this.max.x ||
  2093. point.y < this.min.y || point.y > this.max.y ) {
  2094. return false;
  2095. }
  2096. return true;
  2097. },
  2098. containsBox: function ( box ) {
  2099. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  2100. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) ) {
  2101. return true;
  2102. }
  2103. return false;
  2104. },
  2105. getParameter: function ( point, optionalTarget ) {
  2106. // This can potentially have a divide by zero if the box
  2107. // has a size dimension of 0.
  2108. var result = optionalTarget || new THREE.Vector2();
  2109. return result.set(
  2110. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  2111. ( point.y - this.min.y ) / ( this.max.y - this.min.y )
  2112. );
  2113. },
  2114. isIntersectionBox: function ( box ) {
  2115. // using 6 splitting planes to rule out intersections.
  2116. if ( box.max.x < this.min.x || box.min.x > this.max.x ||
  2117. box.max.y < this.min.y || box.min.y > this.max.y ) {
  2118. return false;
  2119. }
  2120. return true;
  2121. },
  2122. clampPoint: function ( point, optionalTarget ) {
  2123. var result = optionalTarget || new THREE.Vector2();
  2124. return result.copy( point ).clamp( this.min, this.max );
  2125. },
  2126. distanceToPoint: function () {
  2127. var v1 = new THREE.Vector2();
  2128. return function ( point ) {
  2129. var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
  2130. return clampedPoint.sub( point ).length();
  2131. };
  2132. }(),
  2133. intersect: function ( box ) {
  2134. this.min.max( box.min );
  2135. this.max.min( box.max );
  2136. return this;
  2137. },
  2138. union: function ( box ) {
  2139. this.min.min( box.min );
  2140. this.max.max( box.max );
  2141. return this;
  2142. },
  2143. translate: function ( offset ) {
  2144. this.min.add( offset );
  2145. this.max.add( offset );
  2146. return this;
  2147. },
  2148. equals: function ( box ) {
  2149. return box.min.equals( this.min ) && box.max.equals( this.max );
  2150. },
  2151. clone: function () {
  2152. return new THREE.Box2().copy( this );
  2153. }
  2154. };
  2155. // File:src/math/Box3.js
  2156. /**
  2157. * @author bhouston / http://exocortex.com
  2158. * @author WestLangley / http://github.com/WestLangley
  2159. */
  2160. THREE.Box3 = function ( min, max ) {
  2161. this.min = ( min !== undefined ) ? min : new THREE.Vector3( Infinity, Infinity, Infinity );
  2162. this.max = ( max !== undefined ) ? max : new THREE.Vector3( - Infinity, - Infinity, - Infinity );
  2163. };
  2164. THREE.Box3.prototype = {
  2165. constructor: THREE.Box3,
  2166. set: function ( min, max ) {
  2167. this.min.copy( min );
  2168. this.max.copy( max );
  2169. return this;
  2170. },
  2171. setFromPoints: function ( points ) {
  2172. this.makeEmpty();
  2173. for ( var i = 0, il = points.length; i < il; i ++ ) {
  2174. this.expandByPoint( points[ i ] )
  2175. }
  2176. return this;
  2177. },
  2178. setFromCenterAndSize: function () {
  2179. var v1 = new THREE.Vector3();
  2180. return function ( center, size ) {
  2181. var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
  2182. this.min.copy( center ).sub( halfSize );
  2183. this.max.copy( center ).add( halfSize );
  2184. return this;
  2185. };
  2186. }(),
  2187. setFromObject: function () {
  2188. // Computes the world-axis-aligned bounding box of an object (including its children),
  2189. // accounting for both the object's, and childrens', world transforms
  2190. var v1 = new THREE.Vector3();
  2191. return function ( object ) {
  2192. var scope = this;
  2193. object.updateMatrixWorld( true );
  2194. this.makeEmpty();
  2195. object.traverse( function ( node ) {
  2196. var geometry = node.geometry;
  2197. if ( geometry !== undefined ) {
  2198. if ( geometry instanceof THREE.Geometry ) {
  2199. var vertices = geometry.vertices;
  2200. for ( var i = 0, il = vertices.length; i < il; i ++ ) {
  2201. v1.copy( vertices[ i ] );
  2202. v1.applyMatrix4( node.matrixWorld );
  2203. scope.expandByPoint( v1 );
  2204. }
  2205. } else if ( geometry instanceof THREE.BufferGeometry && geometry.attributes[ 'position' ] !== undefined ) {
  2206. var positions = geometry.attributes[ 'position' ].array;
  2207. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  2208. v1.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  2209. v1.applyMatrix4( node.matrixWorld );
  2210. scope.expandByPoint( v1 );
  2211. }
  2212. }
  2213. }
  2214. } );
  2215. return this;
  2216. };
  2217. }(),
  2218. copy: function ( box ) {
  2219. this.min.copy( box.min );
  2220. this.max.copy( box.max );
  2221. return this;
  2222. },
  2223. makeEmpty: function () {
  2224. this.min.x = this.min.y = this.min.z = Infinity;
  2225. this.max.x = this.max.y = this.max.z = - Infinity;
  2226. return this;
  2227. },
  2228. empty: function () {
  2229. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  2230. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
  2231. },
  2232. center: function ( optionalTarget ) {
  2233. var result = optionalTarget || new THREE.Vector3();
  2234. return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  2235. },
  2236. size: function ( optionalTarget ) {
  2237. var result = optionalTarget || new THREE.Vector3();
  2238. return result.subVectors( this.max, this.min );
  2239. },
  2240. expandByPoint: function ( point ) {
  2241. this.min.min( point );
  2242. this.max.max( point );
  2243. return this;
  2244. },
  2245. expandByVector: function ( vector ) {
  2246. this.min.sub( vector );
  2247. this.max.add( vector );
  2248. return this;
  2249. },
  2250. expandByScalar: function ( scalar ) {
  2251. this.min.addScalar( - scalar );
  2252. this.max.addScalar( scalar );
  2253. return this;
  2254. },
  2255. containsPoint: function ( point ) {
  2256. if ( point.x < this.min.x || point.x > this.max.x ||
  2257. point.y < this.min.y || point.y > this.max.y ||
  2258. point.z < this.min.z || point.z > this.max.z ) {
  2259. return false;
  2260. }
  2261. return true;
  2262. },
  2263. containsBox: function ( box ) {
  2264. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  2265. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
  2266. ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) {
  2267. return true;
  2268. }
  2269. return false;
  2270. },
  2271. getParameter: function ( point, optionalTarget ) {
  2272. // This can potentially have a divide by zero if the box
  2273. // has a size dimension of 0.
  2274. var result = optionalTarget || new THREE.Vector3();
  2275. return result.set(
  2276. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  2277. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  2278. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  2279. );
  2280. },
  2281. isIntersectionBox: function ( box ) {
  2282. // using 6 splitting planes to rule out intersections.
  2283. if ( box.max.x < this.min.x || box.min.x > this.max.x ||
  2284. box.max.y < this.min.y || box.min.y > this.max.y ||
  2285. box.max.z < this.min.z || box.min.z > this.max.z ) {
  2286. return false;
  2287. }
  2288. return true;
  2289. },
  2290. clampPoint: function ( point, optionalTarget ) {
  2291. var result = optionalTarget || new THREE.Vector3();
  2292. return result.copy( point ).clamp( this.min, this.max );
  2293. },
  2294. distanceToPoint: function () {
  2295. var v1 = new THREE.Vector3();
  2296. return function ( point ) {
  2297. var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
  2298. return clampedPoint.sub( point ).length();
  2299. };
  2300. }(),
  2301. getBoundingSphere: function () {
  2302. var v1 = new THREE.Vector3();
  2303. return function ( optionalTarget ) {
  2304. var result = optionalTarget || new THREE.Sphere();
  2305. result.center = this.center();
  2306. result.radius = this.size( v1 ).length() * 0.5;
  2307. return result;
  2308. };
  2309. }(),
  2310. intersect: function ( box ) {
  2311. this.min.max( box.min );
  2312. this.max.min( box.max );
  2313. return this;
  2314. },
  2315. union: function ( box ) {
  2316. this.min.min( box.min );
  2317. this.max.max( box.max );
  2318. return this;
  2319. },
  2320. applyMatrix4: function () {
  2321. var points = [
  2322. new THREE.Vector3(),
  2323. new THREE.Vector3(),
  2324. new THREE.Vector3(),
  2325. new THREE.Vector3(),
  2326. new THREE.Vector3(),
  2327. new THREE.Vector3(),
  2328. new THREE.Vector3(),
  2329. new THREE.Vector3()
  2330. ];
  2331. return function ( matrix ) {
  2332. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  2333. points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  2334. points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  2335. points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  2336. points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  2337. points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  2338. points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  2339. points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  2340. points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  2341. this.makeEmpty();
  2342. this.setFromPoints( points );
  2343. return this;
  2344. };
  2345. }(),
  2346. translate: function ( offset ) {
  2347. this.min.add( offset );
  2348. this.max.add( offset );
  2349. return this;
  2350. },
  2351. equals: function ( box ) {
  2352. return box.min.equals( this.min ) && box.max.equals( this.max );
  2353. },
  2354. clone: function () {
  2355. return new THREE.Box3().copy( this );
  2356. }
  2357. };
  2358. // File:src/math/Matrix3.js
  2359. /**
  2360. * @author alteredq / http://alteredqualia.com/
  2361. * @author WestLangley / http://github.com/WestLangley
  2362. * @author bhouston / http://exocortex.com
  2363. */
  2364. THREE.Matrix3 = function () {
  2365. this.elements = new Float32Array( [
  2366. 1, 0, 0,
  2367. 0, 1, 0,
  2368. 0, 0, 1
  2369. ] );
  2370. if ( arguments.length > 0 ) {
  2371. console.error( 'THREE.Matrix3: the constructor no longer reads arguments. use .set() instead.' );
  2372. }
  2373. };
  2374. THREE.Matrix3.prototype = {
  2375. constructor: THREE.Matrix3,
  2376. set: function ( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) {
  2377. var te = this.elements;
  2378. te[ 0 ] = n11; te[ 3 ] = n12; te[ 6 ] = n13;
  2379. te[ 1 ] = n21; te[ 4 ] = n22; te[ 7 ] = n23;
  2380. te[ 2 ] = n31; te[ 5 ] = n32; te[ 8 ] = n33;
  2381. return this;
  2382. },
  2383. identity: function () {
  2384. this.set(
  2385. 1, 0, 0,
  2386. 0, 1, 0,
  2387. 0, 0, 1
  2388. );
  2389. return this;
  2390. },
  2391. copy: function ( m ) {
  2392. var me = m.elements;
  2393. this.set(
  2394. me[ 0 ], me[ 3 ], me[ 6 ],
  2395. me[ 1 ], me[ 4 ], me[ 7 ],
  2396. me[ 2 ], me[ 5 ], me[ 8 ]
  2397. );
  2398. return this;
  2399. },
  2400. multiplyVector3: function ( vector ) {
  2401. console.warn( 'THREE.Matrix3: .multiplyVector3() has been removed. Use vector.applyMatrix3( matrix ) instead.' );
  2402. return vector.applyMatrix3( this );
  2403. },
  2404. multiplyVector3Array: function ( a ) {
  2405. console.warn( 'THREE.Matrix3: .multiplyVector3Array() has been renamed. Use matrix.applyToVector3Array( array ) instead.' );
  2406. return this.applyToVector3Array( a );
  2407. },
  2408. applyToVector3Array: function () {
  2409. var v1 = new THREE.Vector3();
  2410. return function ( array, offset, length ) {
  2411. if ( offset === undefined ) offset = 0;
  2412. if ( length === undefined ) length = array.length;
  2413. for ( var i = 0, j = offset, il; i < length; i += 3, j += 3 ) {
  2414. v1.x = array[ j ];
  2415. v1.y = array[ j + 1 ];
  2416. v1.z = array[ j + 2 ];
  2417. v1.applyMatrix3( this );
  2418. array[ j ] = v1.x;
  2419. array[ j + 1 ] = v1.y;
  2420. array[ j + 2 ] = v1.z;
  2421. }
  2422. return array;
  2423. };
  2424. }(),
  2425. multiplyScalar: function ( s ) {
  2426. var te = this.elements;
  2427. te[ 0 ] *= s; te[ 3 ] *= s; te[ 6 ] *= s;
  2428. te[ 1 ] *= s; te[ 4 ] *= s; te[ 7 ] *= s;
  2429. te[ 2 ] *= s; te[ 5 ] *= s; te[ 8 ] *= s;
  2430. return this;
  2431. },
  2432. determinant: function () {
  2433. var te = this.elements;
  2434. var a = te[ 0 ], b = te[ 1 ], c = te[ 2 ],
  2435. d = te[ 3 ], e = te[ 4 ], f = te[ 5 ],
  2436. g = te[ 6 ], h = te[ 7 ], i = te[ 8 ];
  2437. return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
  2438. },
  2439. getInverse: function ( matrix, throwOnInvertible ) {
  2440. // input: THREE.Matrix4
  2441. // ( based on http://code.google.com/p/webgl-mjs/ )
  2442. var me = matrix.elements;
  2443. var te = this.elements;
  2444. te[ 0 ] = me[ 10 ] * me[ 5 ] - me[ 6 ] * me[ 9 ];
  2445. te[ 1 ] = - me[ 10 ] * me[ 1 ] + me[ 2 ] * me[ 9 ];
  2446. te[ 2 ] = me[ 6 ] * me[ 1 ] - me[ 2 ] * me[ 5 ];
  2447. te[ 3 ] = - me[ 10 ] * me[ 4 ] + me[ 6 ] * me[ 8 ];
  2448. te[ 4 ] = me[ 10 ] * me[ 0 ] - me[ 2 ] * me[ 8 ];
  2449. te[ 5 ] = - me[ 6 ] * me[ 0 ] + me[ 2 ] * me[ 4 ];
  2450. te[ 6 ] = me[ 9 ] * me[ 4 ] - me[ 5 ] * me[ 8 ];
  2451. te[ 7 ] = - me[ 9 ] * me[ 0 ] + me[ 1 ] * me[ 8 ];
  2452. te[ 8 ] = me[ 5 ] * me[ 0 ] - me[ 1 ] * me[ 4 ];
  2453. var det = me[ 0 ] * te[ 0 ] + me[ 1 ] * te[ 3 ] + me[ 2 ] * te[ 6 ];
  2454. // no inverse
  2455. if ( det === 0 ) {
  2456. var msg = "Matrix3.getInverse(): can't invert matrix, determinant is 0";
  2457. if ( throwOnInvertible || false ) {
  2458. throw new Error( msg );
  2459. } else {
  2460. console.warn( msg );
  2461. }
  2462. this.identity();
  2463. return this;
  2464. }
  2465. this.multiplyScalar( 1.0 / det );
  2466. return this;
  2467. },
  2468. transpose: function () {
  2469. var tmp, m = this.elements;
  2470. tmp = m[ 1 ]; m[ 1 ] = m[ 3 ]; m[ 3 ] = tmp;
  2471. tmp = m[ 2 ]; m[ 2 ] = m[ 6 ]; m[ 6 ] = tmp;
  2472. tmp = m[ 5 ]; m[ 5 ] = m[ 7 ]; m[ 7 ] = tmp;
  2473. return this;
  2474. },
  2475. flattenToArrayOffset: function ( array, offset ) {
  2476. var te = this.elements;
  2477. array[ offset ] = te[ 0 ];
  2478. array[ offset + 1 ] = te[ 1 ];
  2479. array[ offset + 2 ] = te[ 2 ];
  2480. array[ offset + 3 ] = te[ 3 ];
  2481. array[ offset + 4 ] = te[ 4 ];
  2482. array[ offset + 5 ] = te[ 5 ];
  2483. array[ offset + 6 ] = te[ 6 ];
  2484. array[ offset + 7 ] = te[ 7 ];
  2485. array[ offset + 8 ] = te[ 8 ];
  2486. return array;
  2487. },
  2488. getNormalMatrix: function ( m ) {
  2489. // input: THREE.Matrix4
  2490. this.getInverse( m ).transpose();
  2491. return this;
  2492. },
  2493. transposeIntoArray: function ( r ) {
  2494. var m = this.elements;
  2495. r[ 0 ] = m[ 0 ];
  2496. r[ 1 ] = m[ 3 ];
  2497. r[ 2 ] = m[ 6 ];
  2498. r[ 3 ] = m[ 1 ];
  2499. r[ 4 ] = m[ 4 ];
  2500. r[ 5 ] = m[ 7 ];
  2501. r[ 6 ] = m[ 2 ];
  2502. r[ 7 ] = m[ 5 ];
  2503. r[ 8 ] = m[ 8 ];
  2504. return this;
  2505. },
  2506. fromArray: function ( array ) {
  2507. this.elements.set( array );
  2508. return this;
  2509. },
  2510. toArray: function () {
  2511. var te = this.elements;
  2512. return [
  2513. te[ 0 ], te[ 1 ], te[ 2 ],
  2514. te[ 3 ], te[ 4 ], te[ 5 ],
  2515. te[ 6 ], te[ 7 ], te[ 8 ]
  2516. ];
  2517. },
  2518. clone: function () {
  2519. return new THREE.Matrix3().fromArray( this.elements );
  2520. }
  2521. };
  2522. // File:src/math/Matrix4.js
  2523. /**
  2524. * @author mrdoob / http://mrdoob.com/
  2525. * @author supereggbert / http://www.paulbrunt.co.uk/
  2526. * @author philogb / http://blog.thejit.org/
  2527. * @author jordi_ros / http://plattsoft.com
  2528. * @author D1plo1d / http://github.com/D1plo1d
  2529. * @author alteredq / http://alteredqualia.com/
  2530. * @author mikael emtinger / http://gomo.se/
  2531. * @author timknip / http://www.floorplanner.com/
  2532. * @author bhouston / http://exocortex.com
  2533. * @author WestLangley / http://github.com/WestLangley
  2534. */
  2535. THREE.Matrix4 = function () {
  2536. this.elements = new Float32Array( [
  2537. 1, 0, 0, 0,
  2538. 0, 1, 0, 0,
  2539. 0, 0, 1, 0,
  2540. 0, 0, 0, 1
  2541. ] );
  2542. if ( arguments.length > 0 ) {
  2543. console.error( 'THREE.Matrix4: the constructor no longer reads arguments. use .set() instead.' );
  2544. }
  2545. };
  2546. THREE.Matrix4.prototype = {
  2547. constructor: THREE.Matrix4,
  2548. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  2549. var te = this.elements;
  2550. te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14;
  2551. te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24;
  2552. te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34;
  2553. te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44;
  2554. return this;
  2555. },
  2556. identity: function () {
  2557. this.set(
  2558. 1, 0, 0, 0,
  2559. 0, 1, 0, 0,
  2560. 0, 0, 1, 0,
  2561. 0, 0, 0, 1
  2562. );
  2563. return this;
  2564. },
  2565. copy: function ( m ) {
  2566. this.elements.set( m.elements );
  2567. return this;
  2568. },
  2569. extractPosition: function ( m ) {
  2570. console.warn( 'THREE.Matrix4: .extractPosition() has been renamed to .copyPosition().' );
  2571. return this.copyPosition( m );
  2572. },
  2573. copyPosition: function ( m ) {
  2574. var te = this.elements;
  2575. var me = m.elements;
  2576. te[ 12 ] = me[ 12 ];
  2577. te[ 13 ] = me[ 13 ];
  2578. te[ 14 ] = me[ 14 ];
  2579. return this;
  2580. },
  2581. extractRotation: function () {
  2582. var v1 = new THREE.Vector3();
  2583. return function ( m ) {
  2584. var te = this.elements;
  2585. var me = m.elements;
  2586. var scaleX = 1 / v1.set( me[ 0 ], me[ 1 ], me[ 2 ] ).length();
  2587. var scaleY = 1 / v1.set( me[ 4 ], me[ 5 ], me[ 6 ] ).length();
  2588. var scaleZ = 1 / v1.set( me[ 8 ], me[ 9 ], me[ 10 ] ).length();
  2589. te[ 0 ] = me[ 0 ] * scaleX;
  2590. te[ 1 ] = me[ 1 ] * scaleX;
  2591. te[ 2 ] = me[ 2 ] * scaleX;
  2592. te[ 4 ] = me[ 4 ] * scaleY;
  2593. te[ 5 ] = me[ 5 ] * scaleY;
  2594. te[ 6 ] = me[ 6 ] * scaleY;
  2595. te[ 8 ] = me[ 8 ] * scaleZ;
  2596. te[ 9 ] = me[ 9 ] * scaleZ;
  2597. te[ 10 ] = me[ 10 ] * scaleZ;
  2598. return this;
  2599. };
  2600. }(),
  2601. makeRotationFromEuler: function ( euler ) {
  2602. if ( euler instanceof THREE.Euler === false ) {
  2603. console.error( 'THREE.Matrix: .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  2604. }
  2605. var te = this.elements;
  2606. var x = euler.x, y = euler.y, z = euler.z;
  2607. var a = Math.cos( x ), b = Math.sin( x );
  2608. var c = Math.cos( y ), d = Math.sin( y );
  2609. var e = Math.cos( z ), f = Math.sin( z );
  2610. if ( euler.order === 'XYZ' ) {
  2611. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  2612. te[ 0 ] = c * e;
  2613. te[ 4 ] = - c * f;
  2614. te[ 8 ] = d;
  2615. te[ 1 ] = af + be * d;
  2616. te[ 5 ] = ae - bf * d;
  2617. te[ 9 ] = - b * c;
  2618. te[ 2 ] = bf - ae * d;
  2619. te[ 6 ] = be + af * d;
  2620. te[ 10 ] = a * c;
  2621. } else if ( euler.order === 'YXZ' ) {
  2622. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  2623. te[ 0 ] = ce + df * b;
  2624. te[ 4 ] = de * b - cf;
  2625. te[ 8 ] = a * d;
  2626. te[ 1 ] = a * f;
  2627. te[ 5 ] = a * e;
  2628. te[ 9 ] = - b;
  2629. te[ 2 ] = cf * b - de;
  2630. te[ 6 ] = df + ce * b;
  2631. te[ 10 ] = a * c;
  2632. } else if ( euler.order === 'ZXY' ) {
  2633. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  2634. te[ 0 ] = ce - df * b;
  2635. te[ 4 ] = - a * f;
  2636. te[ 8 ] = de + cf * b;
  2637. te[ 1 ] = cf + de * b;
  2638. te[ 5 ] = a * e;
  2639. te[ 9 ] = df - ce * b;
  2640. te[ 2 ] = - a * d;
  2641. te[ 6 ] = b;
  2642. te[ 10 ] = a * c;
  2643. } else if ( euler.order === 'ZYX' ) {
  2644. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  2645. te[ 0 ] = c * e;
  2646. te[ 4 ] = be * d - af;
  2647. te[ 8 ] = ae * d + bf;
  2648. te[ 1 ] = c * f;
  2649. te[ 5 ] = bf * d + ae;
  2650. te[ 9 ] = af * d - be;
  2651. te[ 2 ] = - d;
  2652. te[ 6 ] = b * c;
  2653. te[ 10 ] = a * c;
  2654. } else if ( euler.order === 'YZX' ) {
  2655. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  2656. te[ 0 ] = c * e;
  2657. te[ 4 ] = bd - ac * f;
  2658. te[ 8 ] = bc * f + ad;
  2659. te[ 1 ] = f;
  2660. te[ 5 ] = a * e;
  2661. te[ 9 ] = - b * e;
  2662. te[ 2 ] = - d * e;
  2663. te[ 6 ] = ad * f + bc;
  2664. te[ 10 ] = ac - bd * f;
  2665. } else if ( euler.order === 'XZY' ) {
  2666. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  2667. te[ 0 ] = c * e;
  2668. te[ 4 ] = - f;
  2669. te[ 8 ] = d * e;
  2670. te[ 1 ] = ac * f + bd;
  2671. te[ 5 ] = a * e;
  2672. te[ 9 ] = ad * f - bc;
  2673. te[ 2 ] = bc * f - ad;
  2674. te[ 6 ] = b * e;
  2675. te[ 10 ] = bd * f + ac;
  2676. }
  2677. // last column
  2678. te[ 3 ] = 0;
  2679. te[ 7 ] = 0;
  2680. te[ 11 ] = 0;
  2681. // bottom row
  2682. te[ 12 ] = 0;
  2683. te[ 13 ] = 0;
  2684. te[ 14 ] = 0;
  2685. te[ 15 ] = 1;
  2686. return this;
  2687. },
  2688. setRotationFromQuaternion: function ( q ) {
  2689. console.warn( 'THREE.Matrix4: .setRotationFromQuaternion() has been renamed to .makeRotationFromQuaternion().' );
  2690. return this.makeRotationFromQuaternion( q );
  2691. },
  2692. makeRotationFromQuaternion: function ( q ) {
  2693. var te = this.elements;
  2694. var x = q.x, y = q.y, z = q.z, w = q.w;
  2695. var x2 = x + x, y2 = y + y, z2 = z + z;
  2696. var xx = x * x2, xy = x * y2, xz = x * z2;
  2697. var yy = y * y2, yz = y * z2, zz = z * z2;
  2698. var wx = w * x2, wy = w * y2, wz = w * z2;
  2699. te[ 0 ] = 1 - ( yy + zz );
  2700. te[ 4 ] = xy - wz;
  2701. te[ 8 ] = xz + wy;
  2702. te[ 1 ] = xy + wz;
  2703. te[ 5 ] = 1 - ( xx + zz );
  2704. te[ 9 ] = yz - wx;
  2705. te[ 2 ] = xz - wy;
  2706. te[ 6 ] = yz + wx;
  2707. te[ 10 ] = 1 - ( xx + yy );
  2708. // last column
  2709. te[ 3 ] = 0;
  2710. te[ 7 ] = 0;
  2711. te[ 11 ] = 0;
  2712. // bottom row
  2713. te[ 12 ] = 0;
  2714. te[ 13 ] = 0;
  2715. te[ 14 ] = 0;
  2716. te[ 15 ] = 1;
  2717. return this;
  2718. },
  2719. lookAt: function () {
  2720. var x = new THREE.Vector3();
  2721. var y = new THREE.Vector3();
  2722. var z = new THREE.Vector3();
  2723. return function ( eye, target, up ) {
  2724. var te = this.elements;
  2725. z.subVectors( eye, target ).normalize();
  2726. if ( z.length() === 0 ) {
  2727. z.z = 1;
  2728. }
  2729. x.crossVectors( up, z ).normalize();
  2730. if ( x.length() === 0 ) {
  2731. z.x += 0.0001;
  2732. x.crossVectors( up, z ).normalize();
  2733. }
  2734. y.crossVectors( z, x );
  2735. te[ 0 ] = x.x; te[ 4 ] = y.x; te[ 8 ] = z.x;
  2736. te[ 1 ] = x.y; te[ 5 ] = y.y; te[ 9 ] = z.y;
  2737. te[ 2 ] = x.z; te[ 6 ] = y.z; te[ 10 ] = z.z;
  2738. return this;
  2739. };
  2740. }(),
  2741. multiply: function ( m, n ) {
  2742. if ( n !== undefined ) {
  2743. console.warn( 'THREE.Matrix4: .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.' );
  2744. return this.multiplyMatrices( m, n );
  2745. }
  2746. return this.multiplyMatrices( this, m );
  2747. },
  2748. multiplyMatrices: function ( a, b ) {
  2749. var ae = a.elements;
  2750. var be = b.elements;
  2751. var te = this.elements;
  2752. var a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ];
  2753. var a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ];
  2754. var a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ];
  2755. var a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ];
  2756. var b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ];
  2757. var b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ];
  2758. var b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ];
  2759. var b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ];
  2760. te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  2761. te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  2762. te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  2763. te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  2764. te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  2765. te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  2766. te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  2767. te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  2768. te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  2769. te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  2770. te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  2771. te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  2772. te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  2773. te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  2774. te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  2775. te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  2776. return this;
  2777. },
  2778. multiplyToArray: function ( a, b, r ) {
  2779. var te = this.elements;
  2780. this.multiplyMatrices( a, b );
  2781. r[ 0 ] = te[ 0 ]; r[ 1 ] = te[ 1 ]; r[ 2 ] = te[ 2 ]; r[ 3 ] = te[ 3 ];
  2782. r[ 4 ] = te[ 4 ]; r[ 5 ] = te[ 5 ]; r[ 6 ] = te[ 6 ]; r[ 7 ] = te[ 7 ];
  2783. r[ 8 ] = te[ 8 ]; r[ 9 ] = te[ 9 ]; r[ 10 ] = te[ 10 ]; r[ 11 ] = te[ 11 ];
  2784. r[ 12 ] = te[ 12 ]; r[ 13 ] = te[ 13 ]; r[ 14 ] = te[ 14 ]; r[ 15 ] = te[ 15 ];
  2785. return this;
  2786. },
  2787. multiplyScalar: function ( s ) {
  2788. var te = this.elements;
  2789. te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s;
  2790. te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s;
  2791. te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s;
  2792. te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s;
  2793. return this;
  2794. },
  2795. multiplyVector3: function ( vector ) {
  2796. console.warn( 'THREE.Matrix4: .multiplyVector3() has been removed. Use vector.applyMatrix4( matrix ) or vector.applyProjection( matrix ) instead.' );
  2797. return vector.applyProjection( this );
  2798. },
  2799. multiplyVector4: function ( vector ) {
  2800. console.warn( 'THREE.Matrix4: .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
  2801. return vector.applyMatrix4( this );
  2802. },
  2803. multiplyVector3Array: function ( a ) {
  2804. console.warn( 'THREE.Matrix4: .multiplyVector3Array() has been renamed. Use matrix.applyToVector3Array( array ) instead.' );
  2805. return this.applyToVector3Array( a );
  2806. },
  2807. applyToVector3Array: function () {
  2808. var v1 = new THREE.Vector3();
  2809. return function ( array, offset, length ) {
  2810. if ( offset === undefined ) offset = 0;
  2811. if ( length === undefined ) length = array.length;
  2812. for ( var i = 0, j = offset, il; i < length; i += 3, j += 3 ) {
  2813. v1.x = array[ j ];
  2814. v1.y = array[ j + 1 ];
  2815. v1.z = array[ j + 2 ];
  2816. v1.applyMatrix4( this );
  2817. array[ j ] = v1.x;
  2818. array[ j + 1 ] = v1.y;
  2819. array[ j + 2 ] = v1.z;
  2820. }
  2821. return array;
  2822. };
  2823. }(),
  2824. rotateAxis: function ( v ) {
  2825. console.warn( 'THREE.Matrix4: .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );
  2826. v.transformDirection( this );
  2827. },
  2828. crossVector: function ( vector ) {
  2829. console.warn( 'THREE.Matrix4: .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
  2830. return vector.applyMatrix4( this );
  2831. },
  2832. determinant: function () {
  2833. var te = this.elements;
  2834. var n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ];
  2835. var n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ];
  2836. var n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ];
  2837. var n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ];
  2838. //TODO: make this more efficient
  2839. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  2840. return (
  2841. n41 * (
  2842. + n14 * n23 * n32
  2843. - n13 * n24 * n32
  2844. - n14 * n22 * n33
  2845. + n12 * n24 * n33
  2846. + n13 * n22 * n34
  2847. - n12 * n23 * n34
  2848. ) +
  2849. n42 * (
  2850. + n11 * n23 * n34
  2851. - n11 * n24 * n33
  2852. + n14 * n21 * n33
  2853. - n13 * n21 * n34
  2854. + n13 * n24 * n31
  2855. - n14 * n23 * n31
  2856. ) +
  2857. n43 * (
  2858. + n11 * n24 * n32
  2859. - n11 * n22 * n34
  2860. - n14 * n21 * n32
  2861. + n12 * n21 * n34
  2862. + n14 * n22 * n31
  2863. - n12 * n24 * n31
  2864. ) +
  2865. n44 * (
  2866. - n13 * n22 * n31
  2867. - n11 * n23 * n32
  2868. + n11 * n22 * n33
  2869. + n13 * n21 * n32
  2870. - n12 * n21 * n33
  2871. + n12 * n23 * n31
  2872. )
  2873. );
  2874. },
  2875. transpose: function () {
  2876. var te = this.elements;
  2877. var tmp;
  2878. tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp;
  2879. tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp;
  2880. tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp;
  2881. tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp;
  2882. tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp;
  2883. tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp;
  2884. return this;
  2885. },
  2886. flattenToArrayOffset: function ( array, offset ) {
  2887. var te = this.elements;
  2888. array[ offset ] = te[ 0 ];
  2889. array[ offset + 1 ] = te[ 1 ];
  2890. array[ offset + 2 ] = te[ 2 ];
  2891. array[ offset + 3 ] = te[ 3 ];
  2892. array[ offset + 4 ] = te[ 4 ];
  2893. array[ offset + 5 ] = te[ 5 ];
  2894. array[ offset + 6 ] = te[ 6 ];
  2895. array[ offset + 7 ] = te[ 7 ];
  2896. array[ offset + 8 ] = te[ 8 ];
  2897. array[ offset + 9 ] = te[ 9 ];
  2898. array[ offset + 10 ] = te[ 10 ];
  2899. array[ offset + 11 ] = te[ 11 ];
  2900. array[ offset + 12 ] = te[ 12 ];
  2901. array[ offset + 13 ] = te[ 13 ];
  2902. array[ offset + 14 ] = te[ 14 ];
  2903. array[ offset + 15 ] = te[ 15 ];
  2904. return array;
  2905. },
  2906. getPosition: function () {
  2907. var v1 = new THREE.Vector3();
  2908. return function () {
  2909. console.warn( 'THREE.Matrix4: .getPosition() has been removed. Use Vector3.setFromMatrixPosition( matrix ) instead.' );
  2910. var te = this.elements;
  2911. return v1.set( te[ 12 ], te[ 13 ], te[ 14 ] );
  2912. };
  2913. }(),
  2914. setPosition: function ( v ) {
  2915. var te = this.elements;
  2916. te[ 12 ] = v.x;
  2917. te[ 13 ] = v.y;
  2918. te[ 14 ] = v.z;
  2919. return this;
  2920. },
  2921. getInverse: function ( m, throwOnInvertible ) {
  2922. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  2923. var te = this.elements;
  2924. var me = m.elements;
  2925. var n11 = me[ 0 ], n12 = me[ 4 ], n13 = me[ 8 ], n14 = me[ 12 ];
  2926. var n21 = me[ 1 ], n22 = me[ 5 ], n23 = me[ 9 ], n24 = me[ 13 ];
  2927. var n31 = me[ 2 ], n32 = me[ 6 ], n33 = me[ 10 ], n34 = me[ 14 ];
  2928. var n41 = me[ 3 ], n42 = me[ 7 ], n43 = me[ 11 ], n44 = me[ 15 ];
  2929. te[ 0 ] = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44;
  2930. te[ 4 ] = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44;
  2931. te[ 8 ] = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44;
  2932. te[ 12 ] = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
  2933. te[ 1 ] = n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44;
  2934. te[ 5 ] = n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44;
  2935. te[ 9 ] = n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44;
  2936. te[ 13 ] = n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34;
  2937. te[ 2 ] = n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44;
  2938. te[ 6 ] = n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44;
  2939. te[ 10 ] = n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44;
  2940. te[ 14 ] = n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34;
  2941. te[ 3 ] = n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43;
  2942. te[ 7 ] = n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43;
  2943. te[ 11 ] = n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43;
  2944. te[ 15 ] = n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33;
  2945. var det = n11 * te[ 0 ] + n21 * te[ 4 ] + n31 * te[ 8 ] + n41 * te[ 12 ];
  2946. if ( det == 0 ) {
  2947. var msg = "Matrix4.getInverse(): can't invert matrix, determinant is 0";
  2948. if ( throwOnInvertible || false ) {
  2949. throw new Error( msg );
  2950. } else {
  2951. console.warn( msg );
  2952. }
  2953. this.identity();
  2954. return this;
  2955. }
  2956. this.multiplyScalar( 1 / det );
  2957. return this;
  2958. },
  2959. translate: function ( v ) {
  2960. console.warn( 'THREE.Matrix4: .translate() has been removed.' );
  2961. },
  2962. rotateX: function ( angle ) {
  2963. console.warn( 'THREE.Matrix4: .rotateX() has been removed.' );
  2964. },
  2965. rotateY: function ( angle ) {
  2966. console.warn( 'THREE.Matrix4: .rotateY() has been removed.' );
  2967. },
  2968. rotateZ: function ( angle ) {
  2969. console.warn( 'THREE.Matrix4: .rotateZ() has been removed.' );
  2970. },
  2971. rotateByAxis: function ( axis, angle ) {
  2972. console.warn( 'THREE.Matrix4: .rotateByAxis() has been removed.' );
  2973. },
  2974. scale: function ( v ) {
  2975. var te = this.elements;
  2976. var x = v.x, y = v.y, z = v.z;
  2977. te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z;
  2978. te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z;
  2979. te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z;
  2980. te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z;
  2981. return this;
  2982. },
  2983. getMaxScaleOnAxis: function () {
  2984. var te = this.elements;
  2985. var scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ];
  2986. var scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ];
  2987. var scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ];
  2988. return Math.sqrt( Math.max( scaleXSq, Math.max( scaleYSq, scaleZSq ) ) );
  2989. },
  2990. makeTranslation: function ( x, y, z ) {
  2991. this.set(
  2992. 1, 0, 0, x,
  2993. 0, 1, 0, y,
  2994. 0, 0, 1, z,
  2995. 0, 0, 0, 1
  2996. );
  2997. return this;
  2998. },
  2999. makeRotationX: function ( theta ) {
  3000. var c = Math.cos( theta ), s = Math.sin( theta );
  3001. this.set(
  3002. 1, 0, 0, 0,
  3003. 0, c, - s, 0,
  3004. 0, s, c, 0,
  3005. 0, 0, 0, 1
  3006. );
  3007. return this;
  3008. },
  3009. makeRotationY: function ( theta ) {
  3010. var c = Math.cos( theta ), s = Math.sin( theta );
  3011. this.set(
  3012. c, 0, s, 0,
  3013. 0, 1, 0, 0,
  3014. - s, 0, c, 0,
  3015. 0, 0, 0, 1
  3016. );
  3017. return this;
  3018. },
  3019. makeRotationZ: function ( theta ) {
  3020. var c = Math.cos( theta ), s = Math.sin( theta );
  3021. this.set(
  3022. c, - s, 0, 0,
  3023. s, c, 0, 0,
  3024. 0, 0, 1, 0,
  3025. 0, 0, 0, 1
  3026. );
  3027. return this;
  3028. },
  3029. makeRotationAxis: function ( axis, angle ) {
  3030. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  3031. var c = Math.cos( angle );
  3032. var s = Math.sin( angle );
  3033. var t = 1 - c;
  3034. var x = axis.x, y = axis.y, z = axis.z;
  3035. var tx = t * x, ty = t * y;
  3036. this.set(
  3037. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  3038. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  3039. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  3040. 0, 0, 0, 1
  3041. );
  3042. return this;
  3043. },
  3044. makeScale: function ( x, y, z ) {
  3045. this.set(
  3046. x, 0, 0, 0,
  3047. 0, y, 0, 0,
  3048. 0, 0, z, 0,
  3049. 0, 0, 0, 1
  3050. );
  3051. return this;
  3052. },
  3053. compose: function ( position, quaternion, scale ) {
  3054. this.makeRotationFromQuaternion( quaternion );
  3055. this.scale( scale );
  3056. this.setPosition( position );
  3057. return this;
  3058. },
  3059. decompose: function () {
  3060. var vector = new THREE.Vector3();
  3061. var matrix = new THREE.Matrix4();
  3062. return function ( position, quaternion, scale ) {
  3063. var te = this.elements;
  3064. var sx = vector.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();
  3065. var sy = vector.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length();
  3066. var sz = vector.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length();
  3067. // if determine is negative, we need to invert one scale
  3068. var det = this.determinant();
  3069. if ( det < 0 ) {
  3070. sx = - sx;
  3071. }
  3072. position.x = te[ 12 ];
  3073. position.y = te[ 13 ];
  3074. position.z = te[ 14 ];
  3075. // scale the rotation part
  3076. matrix.elements.set( this.elements ); // at this point matrix is incomplete so we can't use .copy()
  3077. var invSX = 1 / sx;
  3078. var invSY = 1 / sy;
  3079. var invSZ = 1 / sz;
  3080. matrix.elements[ 0 ] *= invSX;
  3081. matrix.elements[ 1 ] *= invSX;
  3082. matrix.elements[ 2 ] *= invSX;
  3083. matrix.elements[ 4 ] *= invSY;
  3084. matrix.elements[ 5 ] *= invSY;
  3085. matrix.elements[ 6 ] *= invSY;
  3086. matrix.elements[ 8 ] *= invSZ;
  3087. matrix.elements[ 9 ] *= invSZ;
  3088. matrix.elements[ 10 ] *= invSZ;
  3089. quaternion.setFromRotationMatrix( matrix );
  3090. scale.x = sx;
  3091. scale.y = sy;
  3092. scale.z = sz;
  3093. return this;
  3094. };
  3095. }(),
  3096. makeFrustum: function ( left, right, bottom, top, near, far ) {
  3097. var te = this.elements;
  3098. var x = 2 * near / ( right - left );
  3099. var y = 2 * near / ( top - bottom );
  3100. var a = ( right + left ) / ( right - left );
  3101. var b = ( top + bottom ) / ( top - bottom );
  3102. var c = - ( far + near ) / ( far - near );
  3103. var d = - 2 * far * near / ( far - near );
  3104. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0;
  3105. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0;
  3106. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  3107. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0;
  3108. return this;
  3109. },
  3110. makePerspective: function ( fov, aspect, near, far ) {
  3111. var ymax = near * Math.tan( THREE.Math.degToRad( fov * 0.5 ) );
  3112. var ymin = - ymax;
  3113. var xmin = ymin * aspect;
  3114. var xmax = ymax * aspect;
  3115. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  3116. },
  3117. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  3118. var te = this.elements;
  3119. var w = right - left;
  3120. var h = top - bottom;
  3121. var p = far - near;
  3122. var x = ( right + left ) / w;
  3123. var y = ( top + bottom ) / h;
  3124. var z = ( far + near ) / p;
  3125. te[ 0 ] = 2 / w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x;
  3126. te[ 1 ] = 0; te[ 5 ] = 2 / h; te[ 9 ] = 0; te[ 13 ] = - y;
  3127. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = - 2 / p; te[ 14 ] = - z;
  3128. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1;
  3129. return this;
  3130. },
  3131. fromArray: function ( array ) {
  3132. this.elements.set( array );
  3133. return this;
  3134. },
  3135. toArray: function () {
  3136. var te = this.elements;
  3137. return [
  3138. te[ 0 ], te[ 1 ], te[ 2 ], te[ 3 ],
  3139. te[ 4 ], te[ 5 ], te[ 6 ], te[ 7 ],
  3140. te[ 8 ], te[ 9 ], te[ 10 ], te[ 11 ],
  3141. te[ 12 ], te[ 13 ], te[ 14 ], te[ 15 ]
  3142. ];
  3143. },
  3144. clone: function () {
  3145. return new THREE.Matrix4().fromArray( this.elements );
  3146. }
  3147. };
  3148. // File:src/math/Ray.js
  3149. /**
  3150. * @author bhouston / http://exocortex.com
  3151. */
  3152. THREE.Ray = function ( origin, direction ) {
  3153. this.origin = ( origin !== undefined ) ? origin : new THREE.Vector3();
  3154. this.direction = ( direction !== undefined ) ? direction : new THREE.Vector3();
  3155. };
  3156. THREE.Ray.prototype = {
  3157. constructor: THREE.Ray,
  3158. set: function ( origin, direction ) {
  3159. this.origin.copy( origin );
  3160. this.direction.copy( direction );
  3161. return this;
  3162. },
  3163. copy: function ( ray ) {
  3164. this.origin.copy( ray.origin );
  3165. this.direction.copy( ray.direction );
  3166. return this;
  3167. },
  3168. at: function ( t, optionalTarget ) {
  3169. var result = optionalTarget || new THREE.Vector3();
  3170. return result.copy( this.direction ).multiplyScalar( t ).add( this.origin );
  3171. },
  3172. recast: function () {
  3173. var v1 = new THREE.Vector3();
  3174. return function ( t ) {
  3175. this.origin.copy( this.at( t, v1 ) );
  3176. return this;
  3177. };
  3178. }(),
  3179. closestPointToPoint: function ( point, optionalTarget ) {
  3180. var result = optionalTarget || new THREE.Vector3();
  3181. result.subVectors( point, this.origin );
  3182. var directionDistance = result.dot( this.direction );
  3183. if ( directionDistance < 0 ) {
  3184. return result.copy( this.origin );
  3185. }
  3186. return result.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  3187. },
  3188. distanceToPoint: function () {
  3189. var v1 = new THREE.Vector3();
  3190. return function ( point ) {
  3191. var directionDistance = v1.subVectors( point, this.origin ).dot( this.direction );
  3192. // point behind the ray
  3193. if ( directionDistance < 0 ) {
  3194. return this.origin.distanceTo( point );
  3195. }
  3196. v1.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  3197. return v1.distanceTo( point );
  3198. };
  3199. }(),
  3200. distanceSqToSegment: function ( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
  3201. // from http://www.geometrictools.com/LibMathematics/Distance/Wm5DistRay3Segment3.cpp
  3202. // It returns the min distance between the ray and the segment
  3203. // defined by v0 and v1
  3204. // It can also set two optional targets :
  3205. // - The closest point on the ray
  3206. // - The closest point on the segment
  3207. var segCenter = v0.clone().add( v1 ).multiplyScalar( 0.5 );
  3208. var segDir = v1.clone().sub( v0 ).normalize();
  3209. var segExtent = v0.distanceTo( v1 ) * 0.5;
  3210. var diff = this.origin.clone().sub( segCenter );
  3211. var a01 = - this.direction.dot( segDir );
  3212. var b0 = diff.dot( this.direction );
  3213. var b1 = - diff.dot( segDir );
  3214. var c = diff.lengthSq();
  3215. var det = Math.abs( 1 - a01 * a01 );
  3216. var s0, s1, sqrDist, extDet;
  3217. if ( det >= 0 ) {
  3218. // The ray and segment are not parallel.
  3219. s0 = a01 * b1 - b0;
  3220. s1 = a01 * b0 - b1;
  3221. extDet = segExtent * det;
  3222. if ( s0 >= 0 ) {
  3223. if ( s1 >= - extDet ) {
  3224. if ( s1 <= extDet ) {
  3225. // region 0
  3226. // Minimum at interior points of ray and segment.
  3227. var invDet = 1 / det;
  3228. s0 *= invDet;
  3229. s1 *= invDet;
  3230. sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c;
  3231. } else {
  3232. // region 1
  3233. s1 = segExtent;
  3234. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3235. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3236. }
  3237. } else {
  3238. // region 5
  3239. s1 = - segExtent;
  3240. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3241. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3242. }
  3243. } else {
  3244. if ( s1 <= - extDet ) {
  3245. // region 4
  3246. s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) );
  3247. s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3248. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3249. } else if ( s1 <= extDet ) {
  3250. // region 3
  3251. s0 = 0;
  3252. s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3253. sqrDist = s1 * ( s1 + 2 * b1 ) + c;
  3254. } else {
  3255. // region 2
  3256. s0 = Math.max( 0, - ( a01 * segExtent + b0 ) );
  3257. s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3258. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3259. }
  3260. }
  3261. } else {
  3262. // Ray and segment are parallel.
  3263. s1 = ( a01 > 0 ) ? - segExtent : segExtent;
  3264. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3265. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3266. }
  3267. if ( optionalPointOnRay ) {
  3268. optionalPointOnRay.copy( this.direction.clone().multiplyScalar( s0 ).add( this.origin ) );
  3269. }
  3270. if ( optionalPointOnSegment ) {
  3271. optionalPointOnSegment.copy( segDir.clone().multiplyScalar( s1 ).add( segCenter ) );
  3272. }
  3273. return sqrDist;
  3274. },
  3275. isIntersectionSphere: function ( sphere ) {
  3276. return this.distanceToPoint( sphere.center ) <= sphere.radius;
  3277. },
  3278. intersectSphere: function () {
  3279. // from http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-sphere-intersection/
  3280. var v1 = new THREE.Vector3();
  3281. return function ( sphere, optionalTarget ) {
  3282. v1.subVectors( sphere.center, this.origin );
  3283. var tca = v1.dot( this.direction );
  3284. var d2 = v1.dot( v1 ) - tca * tca;
  3285. var radius2 = sphere.radius * sphere.radius;
  3286. if ( d2 > radius2 ) return null;
  3287. var thc = Math.sqrt( radius2 - d2 );
  3288. // t0 = first intersect point - entrance on front of sphere
  3289. var t0 = tca - thc;
  3290. // t1 = second intersect point - exit point on back of sphere
  3291. var t1 = tca + thc;
  3292. // test to see if both t0 and t1 are behind the ray - if so, return null
  3293. if ( t0 < 0 && t1 < 0 ) return null;
  3294. // test to see if t0 is behind the ray:
  3295. // if it is, the ray is inside the sphere, so return the second exit point scaled by t1,
  3296. // in order to always return an intersect point that is in front of the ray.
  3297. if ( t0 < 0 ) return this.at( t1, optionalTarget );
  3298. // else t0 is in front of the ray, so return the first collision point scaled by t0
  3299. return this.at( t0, optionalTarget );
  3300. }
  3301. }(),
  3302. isIntersectionPlane: function ( plane ) {
  3303. // check if the ray lies on the plane first
  3304. var distToPoint = plane.distanceToPoint( this.origin );
  3305. if ( distToPoint === 0 ) {
  3306. return true;
  3307. }
  3308. var denominator = plane.normal.dot( this.direction );
  3309. if ( denominator * distToPoint < 0 ) {
  3310. return true;
  3311. }
  3312. // ray origin is behind the plane (and is pointing behind it)
  3313. return false;
  3314. },
  3315. distanceToPlane: function ( plane ) {
  3316. var denominator = plane.normal.dot( this.direction );
  3317. if ( denominator == 0 ) {
  3318. // line is coplanar, return origin
  3319. if ( plane.distanceToPoint( this.origin ) == 0 ) {
  3320. return 0;
  3321. }
  3322. // Null is preferable to undefined since undefined means.... it is undefined
  3323. return null;
  3324. }
  3325. var t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator;
  3326. // Return if the ray never intersects the plane
  3327. return t >= 0 ? t : null;
  3328. },
  3329. intersectPlane: function ( plane, optionalTarget ) {
  3330. var t = this.distanceToPlane( plane );
  3331. if ( t === null ) {
  3332. return null;
  3333. }
  3334. return this.at( t, optionalTarget );
  3335. },
  3336. isIntersectionBox: function () {
  3337. var v = new THREE.Vector3();
  3338. return function ( box ) {
  3339. return this.intersectBox( box, v ) !== null;
  3340. };
  3341. }(),
  3342. intersectBox: function ( box , optionalTarget ) {
  3343. // http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-box-intersection/
  3344. var tmin,tmax,tymin,tymax,tzmin,tzmax;
  3345. var invdirx = 1 / this.direction.x,
  3346. invdiry = 1 / this.direction.y,
  3347. invdirz = 1 / this.direction.z;
  3348. var origin = this.origin;
  3349. if ( invdirx >= 0 ) {
  3350. tmin = ( box.min.x - origin.x ) * invdirx;
  3351. tmax = ( box.max.x - origin.x ) * invdirx;
  3352. } else {
  3353. tmin = ( box.max.x - origin.x ) * invdirx;
  3354. tmax = ( box.min.x - origin.x ) * invdirx;
  3355. }
  3356. if ( invdiry >= 0 ) {
  3357. tymin = ( box.min.y - origin.y ) * invdiry;
  3358. tymax = ( box.max.y - origin.y ) * invdiry;
  3359. } else {
  3360. tymin = ( box.max.y - origin.y ) * invdiry;
  3361. tymax = ( box.min.y - origin.y ) * invdiry;
  3362. }
  3363. if ( ( tmin > tymax ) || ( tymin > tmax ) ) return null;
  3364. // These lines also handle the case where tmin or tmax is NaN
  3365. // (result of 0 * Infinity). x !== x returns true if x is NaN
  3366. if ( tymin > tmin || tmin !== tmin ) tmin = tymin;
  3367. if ( tymax < tmax || tmax !== tmax ) tmax = tymax;
  3368. if ( invdirz >= 0 ) {
  3369. tzmin = ( box.min.z - origin.z ) * invdirz;
  3370. tzmax = ( box.max.z - origin.z ) * invdirz;
  3371. } else {
  3372. tzmin = ( box.max.z - origin.z ) * invdirz;
  3373. tzmax = ( box.min.z - origin.z ) * invdirz;
  3374. }
  3375. if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) return null;
  3376. if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin;
  3377. if ( tzmax < tmax || tmax !== tmax ) tmax = tzmax;
  3378. //return point closest to the ray (positive side)
  3379. if ( tmax < 0 ) return null;
  3380. return this.at( tmin >= 0 ? tmin : tmax, optionalTarget );
  3381. },
  3382. intersectTriangle: function () {
  3383. // Compute the offset origin, edges, and normal.
  3384. var diff = new THREE.Vector3();
  3385. var edge1 = new THREE.Vector3();
  3386. var edge2 = new THREE.Vector3();
  3387. var normal = new THREE.Vector3();
  3388. return function ( a, b, c, backfaceCulling, optionalTarget ) {
  3389. // from http://www.geometrictools.com/LibMathematics/Intersection/Wm5IntrRay3Triangle3.cpp
  3390. edge1.subVectors( b, a );
  3391. edge2.subVectors( c, a );
  3392. normal.crossVectors( edge1, edge2 );
  3393. // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
  3394. // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
  3395. // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
  3396. // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
  3397. // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
  3398. var DdN = this.direction.dot( normal );
  3399. var sign;
  3400. if ( DdN > 0 ) {
  3401. if ( backfaceCulling ) return null;
  3402. sign = 1;
  3403. } else if ( DdN < 0 ) {
  3404. sign = - 1;
  3405. DdN = - DdN;
  3406. } else {
  3407. return null;
  3408. }
  3409. diff.subVectors( this.origin, a );
  3410. var DdQxE2 = sign * this.direction.dot( edge2.crossVectors( diff, edge2 ) );
  3411. // b1 < 0, no intersection
  3412. if ( DdQxE2 < 0 ) {
  3413. return null;
  3414. }
  3415. var DdE1xQ = sign * this.direction.dot( edge1.cross( diff ) );
  3416. // b2 < 0, no intersection
  3417. if ( DdE1xQ < 0 ) {
  3418. return null;
  3419. }
  3420. // b1+b2 > 1, no intersection
  3421. if ( DdQxE2 + DdE1xQ > DdN ) {
  3422. return null;
  3423. }
  3424. // Line intersects triangle, check if ray does.
  3425. var QdN = - sign * diff.dot( normal );
  3426. // t < 0, no intersection
  3427. if ( QdN < 0 ) {
  3428. return null;
  3429. }
  3430. // Ray intersects triangle.
  3431. return this.at( QdN / DdN, optionalTarget );
  3432. };
  3433. }(),
  3434. applyMatrix4: function ( matrix4 ) {
  3435. this.direction.add( this.origin ).applyMatrix4( matrix4 );
  3436. this.origin.applyMatrix4( matrix4 );
  3437. this.direction.sub( this.origin );
  3438. this.direction.normalize();
  3439. return this;
  3440. },
  3441. equals: function ( ray ) {
  3442. return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
  3443. },
  3444. clone: function () {
  3445. return new THREE.Ray().copy( this );
  3446. }
  3447. };
  3448. // File:src/math/Sphere.js
  3449. /**
  3450. * @author bhouston / http://exocortex.com
  3451. * @author mrdoob / http://mrdoob.com/
  3452. */
  3453. THREE.Sphere = function ( center, radius ) {
  3454. this.center = ( center !== undefined ) ? center : new THREE.Vector3();
  3455. this.radius = ( radius !== undefined ) ? radius : 0;
  3456. };
  3457. THREE.Sphere.prototype = {
  3458. constructor: THREE.Sphere,
  3459. set: function ( center, radius ) {
  3460. this.center.copy( center );
  3461. this.radius = radius;
  3462. return this;
  3463. },
  3464. setFromPoints: function () {
  3465. var box = new THREE.Box3();
  3466. return function ( points, optionalCenter ) {
  3467. var center = this.center;
  3468. if ( optionalCenter !== undefined ) {
  3469. center.copy( optionalCenter );
  3470. } else {
  3471. box.setFromPoints( points ).center( center );
  3472. }
  3473. var maxRadiusSq = 0;
  3474. for ( var i = 0, il = points.length; i < il; i ++ ) {
  3475. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
  3476. }
  3477. this.radius = Math.sqrt( maxRadiusSq );
  3478. return this;
  3479. };
  3480. }(),
  3481. copy: function ( sphere ) {
  3482. this.center.copy( sphere.center );
  3483. this.radius = sphere.radius;
  3484. return this;
  3485. },
  3486. empty: function () {
  3487. return ( this.radius <= 0 );
  3488. },
  3489. containsPoint: function ( point ) {
  3490. return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
  3491. },
  3492. distanceToPoint: function ( point ) {
  3493. return ( point.distanceTo( this.center ) - this.radius );
  3494. },
  3495. intersectsSphere: function ( sphere ) {
  3496. var radiusSum = this.radius + sphere.radius;
  3497. return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
  3498. },
  3499. clampPoint: function ( point, optionalTarget ) {
  3500. var deltaLengthSq = this.center.distanceToSquared( point );
  3501. var result = optionalTarget || new THREE.Vector3();
  3502. result.copy( point );
  3503. if ( deltaLengthSq > ( this.radius * this.radius ) ) {
  3504. result.sub( this.center ).normalize();
  3505. result.multiplyScalar( this.radius ).add( this.center );
  3506. }
  3507. return result;
  3508. },
  3509. getBoundingBox: function ( optionalTarget ) {
  3510. var box = optionalTarget || new THREE.Box3();
  3511. box.set( this.center, this.center );
  3512. box.expandByScalar( this.radius );
  3513. return box;
  3514. },
  3515. applyMatrix4: function ( matrix ) {
  3516. this.center.applyMatrix4( matrix );
  3517. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  3518. return this;
  3519. },
  3520. translate: function ( offset ) {
  3521. this.center.add( offset );
  3522. return this;
  3523. },
  3524. equals: function ( sphere ) {
  3525. return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
  3526. },
  3527. clone: function () {
  3528. return new THREE.Sphere().copy( this );
  3529. }
  3530. };
  3531. // File:src/math/Frustum.js
  3532. /**
  3533. * @author mrdoob / http://mrdoob.com/
  3534. * @author alteredq / http://alteredqualia.com/
  3535. * @author bhouston / http://exocortex.com
  3536. */
  3537. THREE.Frustum = function ( p0, p1, p2, p3, p4, p5 ) {
  3538. this.planes = [
  3539. ( p0 !== undefined ) ? p0 : new THREE.Plane(),
  3540. ( p1 !== undefined ) ? p1 : new THREE.Plane(),
  3541. ( p2 !== undefined ) ? p2 : new THREE.Plane(),
  3542. ( p3 !== undefined ) ? p3 : new THREE.Plane(),
  3543. ( p4 !== undefined ) ? p4 : new THREE.Plane(),
  3544. ( p5 !== undefined ) ? p5 : new THREE.Plane()
  3545. ];
  3546. };
  3547. THREE.Frustum.prototype = {
  3548. constructor: THREE.Frustum,
  3549. set: function ( p0, p1, p2, p3, p4, p5 ) {
  3550. var planes = this.planes;
  3551. planes[ 0 ].copy( p0 );
  3552. planes[ 1 ].copy( p1 );
  3553. planes[ 2 ].copy( p2 );
  3554. planes[ 3 ].copy( p3 );
  3555. planes[ 4 ].copy( p4 );
  3556. planes[ 5 ].copy( p5 );
  3557. return this;
  3558. },
  3559. copy: function ( frustum ) {
  3560. var planes = this.planes;
  3561. for ( var i = 0; i < 6; i ++ ) {
  3562. planes[ i ].copy( frustum.planes[ i ] );
  3563. }
  3564. return this;
  3565. },
  3566. setFromMatrix: function ( m ) {
  3567. var planes = this.planes;
  3568. var me = m.elements;
  3569. var me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ];
  3570. var me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ];
  3571. var me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ];
  3572. var me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ];
  3573. planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize();
  3574. planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();
  3575. planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();
  3576. planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();
  3577. planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();
  3578. planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize();
  3579. return this;
  3580. },
  3581. intersectsObject: function () {
  3582. var sphere = new THREE.Sphere();
  3583. return function ( object ) {
  3584. var geometry = object.geometry;
  3585. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  3586. sphere.copy( geometry.boundingSphere );
  3587. sphere.applyMatrix4( object.matrixWorld );
  3588. return this.intersectsSphere( sphere );
  3589. };
  3590. }(),
  3591. intersectsSphere: function ( sphere ) {
  3592. var planes = this.planes;
  3593. var center = sphere.center;
  3594. var negRadius = - sphere.radius;
  3595. for ( var i = 0; i < 6; i ++ ) {
  3596. var distance = planes[ i ].distanceToPoint( center );
  3597. if ( distance < negRadius ) {
  3598. return false;
  3599. }
  3600. }
  3601. return true;
  3602. },
  3603. intersectsBox: function () {
  3604. var p1 = new THREE.Vector3(),
  3605. p2 = new THREE.Vector3();
  3606. return function ( box ) {
  3607. var planes = this.planes;
  3608. for ( var i = 0; i < 6 ; i ++ ) {
  3609. var plane = planes[ i ];
  3610. p1.x = plane.normal.x > 0 ? box.min.x : box.max.x;
  3611. p2.x = plane.normal.x > 0 ? box.max.x : box.min.x;
  3612. p1.y = plane.normal.y > 0 ? box.min.y : box.max.y;
  3613. p2.y = plane.normal.y > 0 ? box.max.y : box.min.y;
  3614. p1.z = plane.normal.z > 0 ? box.min.z : box.max.z;
  3615. p2.z = plane.normal.z > 0 ? box.max.z : box.min.z;
  3616. var d1 = plane.distanceToPoint( p1 );
  3617. var d2 = plane.distanceToPoint( p2 );
  3618. // if both outside plane, no intersection
  3619. if ( d1 < 0 && d2 < 0 ) {
  3620. return false;
  3621. }
  3622. }
  3623. return true;
  3624. };
  3625. }(),
  3626. containsPoint: function ( point ) {
  3627. var planes = this.planes;
  3628. for ( var i = 0; i < 6; i ++ ) {
  3629. if ( planes[ i ].distanceToPoint( point ) < 0 ) {
  3630. return false;
  3631. }
  3632. }
  3633. return true;
  3634. },
  3635. clone: function () {
  3636. return new THREE.Frustum().copy( this );
  3637. }
  3638. };
  3639. // File:src/math/Plane.js
  3640. /**
  3641. * @author bhouston / http://exocortex.com
  3642. */
  3643. THREE.Plane = function ( normal, constant ) {
  3644. this.normal = ( normal !== undefined ) ? normal : new THREE.Vector3( 1, 0, 0 );
  3645. this.constant = ( constant !== undefined ) ? constant : 0;
  3646. };
  3647. THREE.Plane.prototype = {
  3648. constructor: THREE.Plane,
  3649. set: function ( normal, constant ) {
  3650. this.normal.copy( normal );
  3651. this.constant = constant;
  3652. return this;
  3653. },
  3654. setComponents: function ( x, y, z, w ) {
  3655. this.normal.set( x, y, z );
  3656. this.constant = w;
  3657. return this;
  3658. },
  3659. setFromNormalAndCoplanarPoint: function ( normal, point ) {
  3660. this.normal.copy( normal );
  3661. this.constant = - point.dot( this.normal ); // must be this.normal, not normal, as this.normal is normalized
  3662. return this;
  3663. },
  3664. setFromCoplanarPoints: function () {
  3665. var v1 = new THREE.Vector3();
  3666. var v2 = new THREE.Vector3();
  3667. return function ( a, b, c ) {
  3668. var normal = v1.subVectors( c, b ).cross( v2.subVectors( a, b ) ).normalize();
  3669. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  3670. this.setFromNormalAndCoplanarPoint( normal, a );
  3671. return this;
  3672. };
  3673. }(),
  3674. copy: function ( plane ) {
  3675. this.normal.copy( plane.normal );
  3676. this.constant = plane.constant;
  3677. return this;
  3678. },
  3679. normalize: function () {
  3680. // Note: will lead to a divide by zero if the plane is invalid.
  3681. var inverseNormalLength = 1.0 / this.normal.length();
  3682. this.normal.multiplyScalar( inverseNormalLength );
  3683. this.constant *= inverseNormalLength;
  3684. return this;
  3685. },
  3686. negate: function () {
  3687. this.constant *= - 1;
  3688. this.normal.negate();
  3689. return this;
  3690. },
  3691. distanceToPoint: function ( point ) {
  3692. return this.normal.dot( point ) + this.constant;
  3693. },
  3694. distanceToSphere: function ( sphere ) {
  3695. return this.distanceToPoint( sphere.center ) - sphere.radius;
  3696. },
  3697. projectPoint: function ( point, optionalTarget ) {
  3698. return this.orthoPoint( point, optionalTarget ).sub( point ).negate();
  3699. },
  3700. orthoPoint: function ( point, optionalTarget ) {
  3701. var perpendicularMagnitude = this.distanceToPoint( point );
  3702. var result = optionalTarget || new THREE.Vector3();
  3703. return result.copy( this.normal ).multiplyScalar( perpendicularMagnitude );
  3704. },
  3705. isIntersectionLine: function ( line ) {
  3706. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  3707. var startSign = this.distanceToPoint( line.start );
  3708. var endSign = this.distanceToPoint( line.end );
  3709. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  3710. },
  3711. intersectLine: function () {
  3712. var v1 = new THREE.Vector3();
  3713. return function ( line, optionalTarget ) {
  3714. var result = optionalTarget || new THREE.Vector3();
  3715. var direction = line.delta( v1 );
  3716. var denominator = this.normal.dot( direction );
  3717. if ( denominator == 0 ) {
  3718. // line is coplanar, return origin
  3719. if ( this.distanceToPoint( line.start ) == 0 ) {
  3720. return result.copy( line.start );
  3721. }
  3722. // Unsure if this is the correct method to handle this case.
  3723. return undefined;
  3724. }
  3725. var t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
  3726. if ( t < 0 || t > 1 ) {
  3727. return undefined;
  3728. }
  3729. return result.copy( direction ).multiplyScalar( t ).add( line.start );
  3730. };
  3731. }(),
  3732. coplanarPoint: function ( optionalTarget ) {
  3733. var result = optionalTarget || new THREE.Vector3();
  3734. return result.copy( this.normal ).multiplyScalar( - this.constant );
  3735. },
  3736. applyMatrix4: function () {
  3737. var v1 = new THREE.Vector3();
  3738. var v2 = new THREE.Vector3();
  3739. var m1 = new THREE.Matrix3();
  3740. return function ( matrix, optionalNormalMatrix ) {
  3741. // compute new normal based on theory here:
  3742. // http://www.songho.ca/opengl/gl_normaltransform.html
  3743. var normalMatrix = optionalNormalMatrix || m1.getNormalMatrix( matrix );
  3744. var newNormal = v1.copy( this.normal ).applyMatrix3( normalMatrix );
  3745. var newCoplanarPoint = this.coplanarPoint( v2 );
  3746. newCoplanarPoint.applyMatrix4( matrix );
  3747. this.setFromNormalAndCoplanarPoint( newNormal, newCoplanarPoint );
  3748. return this;
  3749. };
  3750. }(),
  3751. translate: function ( offset ) {
  3752. this.constant = this.constant - offset.dot( this.normal );
  3753. return this;
  3754. },
  3755. equals: function ( plane ) {
  3756. return plane.normal.equals( this.normal ) && ( plane.constant == this.constant );
  3757. },
  3758. clone: function () {
  3759. return new THREE.Plane().copy( this );
  3760. }
  3761. };
  3762. // File:src/math/Math.js
  3763. /**
  3764. * @author alteredq / http://alteredqualia.com/
  3765. * @author mrdoob / http://mrdoob.com/
  3766. */
  3767. THREE.Math = {
  3768. generateUUID: function () {
  3769. // http://www.broofa.com/Tools/Math.uuid.htm
  3770. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split( '' );
  3771. var uuid = new Array( 36 );
  3772. var rnd = 0, r;
  3773. return function () {
  3774. for ( var i = 0; i < 36; i ++ ) {
  3775. if ( i == 8 || i == 13 || i == 18 || i == 23 ) {
  3776. uuid[ i ] = '-';
  3777. } else if ( i == 14 ) {
  3778. uuid[ i ] = '4';
  3779. } else {
  3780. if ( rnd <= 0x02 ) rnd = 0x2000000 + ( Math.random() * 0x1000000 ) | 0;
  3781. r = rnd & 0xf;
  3782. rnd = rnd >> 4;
  3783. uuid[ i ] = chars[ ( i == 19 ) ? ( r & 0x3 ) | 0x8 : r ];
  3784. }
  3785. }
  3786. return uuid.join( '' );
  3787. };
  3788. }(),
  3789. // Clamp value to range <a, b>
  3790. clamp: function ( x, a, b ) {
  3791. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  3792. },
  3793. // Clamp value to range <a, inf)
  3794. clampBottom: function ( x, a ) {
  3795. return x < a ? a : x;
  3796. },
  3797. // Linear mapping from range <a1, a2> to range <b1, b2>
  3798. mapLinear: function ( x, a1, a2, b1, b2 ) {
  3799. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  3800. },
  3801. // http://en.wikipedia.org/wiki/Smoothstep
  3802. smoothstep: function ( x, min, max ) {
  3803. if ( x <= min ) return 0;
  3804. if ( x >= max ) return 1;
  3805. x = ( x - min ) / ( max - min );
  3806. return x * x * ( 3 - 2 * x );
  3807. },
  3808. smootherstep: function ( x, min, max ) {
  3809. if ( x <= min ) return 0;
  3810. if ( x >= max ) return 1;
  3811. x = ( x - min ) / ( max - min );
  3812. return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
  3813. },
  3814. // Random float from <0, 1> with 16 bits of randomness
  3815. // (standard Math.random() creates repetitive patterns when applied over larger space)
  3816. random16: function () {
  3817. return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
  3818. },
  3819. // Random integer from <low, high> interval
  3820. randInt: function ( low, high ) {
  3821. return Math.floor( this.randFloat( low, high ) );
  3822. },
  3823. // Random float from <low, high> interval
  3824. randFloat: function ( low, high ) {
  3825. return low + Math.random() * ( high - low );
  3826. },
  3827. // Random float from <-range/2, range/2> interval
  3828. randFloatSpread: function ( range ) {
  3829. return range * ( 0.5 - Math.random() );
  3830. },
  3831. degToRad: function () {
  3832. var degreeToRadiansFactor = Math.PI / 180;
  3833. return function ( degrees ) {
  3834. return degrees * degreeToRadiansFactor;
  3835. };
  3836. }(),
  3837. radToDeg: function () {
  3838. var radianToDegreesFactor = 180 / Math.PI;
  3839. return function ( radians ) {
  3840. return radians * radianToDegreesFactor;
  3841. };
  3842. }(),
  3843. isPowerOfTwo: function ( value ) {
  3844. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  3845. }
  3846. };
  3847. // File:src/math/Spline.js
  3848. /**
  3849. * Spline from Tween.js, slightly optimized (and trashed)
  3850. * http://sole.github.com/tween.js/examples/05_spline.html
  3851. *
  3852. * @author mrdoob / http://mrdoob.com/
  3853. * @author alteredq / http://alteredqualia.com/
  3854. */
  3855. THREE.Spline = function ( points ) {
  3856. this.points = points;
  3857. var c = [], v3 = { x: 0, y: 0, z: 0 },
  3858. point, intPoint, weight, w2, w3,
  3859. pa, pb, pc, pd;
  3860. this.initFromArray = function ( a ) {
  3861. this.points = [];
  3862. for ( var i = 0; i < a.length; i ++ ) {
  3863. this.points[ i ] = { x: a[ i ][ 0 ], y: a[ i ][ 1 ], z: a[ i ][ 2 ] };
  3864. }
  3865. };
  3866. this.getPoint = function ( k ) {
  3867. point = ( this.points.length - 1 ) * k;
  3868. intPoint = Math.floor( point );
  3869. weight = point - intPoint;
  3870. c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1;
  3871. c[ 1 ] = intPoint;
  3872. c[ 2 ] = intPoint > this.points.length - 2 ? this.points.length - 1 : intPoint + 1;
  3873. c[ 3 ] = intPoint > this.points.length - 3 ? this.points.length - 1 : intPoint + 2;
  3874. pa = this.points[ c[ 0 ] ];
  3875. pb = this.points[ c[ 1 ] ];
  3876. pc = this.points[ c[ 2 ] ];
  3877. pd = this.points[ c[ 3 ] ];
  3878. w2 = weight * weight;
  3879. w3 = weight * w2;
  3880. v3.x = interpolate( pa.x, pb.x, pc.x, pd.x, weight, w2, w3 );
  3881. v3.y = interpolate( pa.y, pb.y, pc.y, pd.y, weight, w2, w3 );
  3882. v3.z = interpolate( pa.z, pb.z, pc.z, pd.z, weight, w2, w3 );
  3883. return v3;
  3884. };
  3885. this.getControlPointsArray = function () {
  3886. var i, p, l = this.points.length,
  3887. coords = [];
  3888. for ( i = 0; i < l; i ++ ) {
  3889. p = this.points[ i ];
  3890. coords[ i ] = [ p.x, p.y, p.z ];
  3891. }
  3892. return coords;
  3893. };
  3894. // approximate length by summing linear segments
  3895. this.getLength = function ( nSubDivisions ) {
  3896. var i, index, nSamples, position,
  3897. point = 0, intPoint = 0, oldIntPoint = 0,
  3898. oldPosition = new THREE.Vector3(),
  3899. tmpVec = new THREE.Vector3(),
  3900. chunkLengths = [],
  3901. totalLength = 0;
  3902. // first point has 0 length
  3903. chunkLengths[ 0 ] = 0;
  3904. if ( ! nSubDivisions ) nSubDivisions = 100;
  3905. nSamples = this.points.length * nSubDivisions;
  3906. oldPosition.copy( this.points[ 0 ] );
  3907. for ( i = 1; i < nSamples; i ++ ) {
  3908. index = i / nSamples;
  3909. position = this.getPoint( index );
  3910. tmpVec.copy( position );
  3911. totalLength += tmpVec.distanceTo( oldPosition );
  3912. oldPosition.copy( position );
  3913. point = ( this.points.length - 1 ) * index;
  3914. intPoint = Math.floor( point );
  3915. if ( intPoint != oldIntPoint ) {
  3916. chunkLengths[ intPoint ] = totalLength;
  3917. oldIntPoint = intPoint;
  3918. }
  3919. }
  3920. // last point ends with total length
  3921. chunkLengths[ chunkLengths.length ] = totalLength;
  3922. return { chunks: chunkLengths, total: totalLength };
  3923. };
  3924. this.reparametrizeByArcLength = function ( samplingCoef ) {
  3925. var i, j,
  3926. index, indexCurrent, indexNext,
  3927. linearDistance, realDistance,
  3928. sampling, position,
  3929. newpoints = [],
  3930. tmpVec = new THREE.Vector3(),
  3931. sl = this.getLength();
  3932. newpoints.push( tmpVec.copy( this.points[ 0 ] ).clone() );
  3933. for ( i = 1; i < this.points.length; i ++ ) {
  3934. //tmpVec.copy( this.points[ i - 1 ] );
  3935. //linearDistance = tmpVec.distanceTo( this.points[ i ] );
  3936. realDistance = sl.chunks[ i ] - sl.chunks[ i - 1 ];
  3937. sampling = Math.ceil( samplingCoef * realDistance / sl.total );
  3938. indexCurrent = ( i - 1 ) / ( this.points.length - 1 );
  3939. indexNext = i / ( this.points.length - 1 );
  3940. for ( j = 1; j < sampling - 1; j ++ ) {
  3941. index = indexCurrent + j * ( 1 / sampling ) * ( indexNext - indexCurrent );
  3942. position = this.getPoint( index );
  3943. newpoints.push( tmpVec.copy( position ).clone() );
  3944. }
  3945. newpoints.push( tmpVec.copy( this.points[ i ] ).clone() );
  3946. }
  3947. this.points = newpoints;
  3948. };
  3949. // Catmull-Rom
  3950. function interpolate( p0, p1, p2, p3, t, t2, t3 ) {
  3951. var v0 = ( p2 - p0 ) * 0.5,
  3952. v1 = ( p3 - p1 ) * 0.5;
  3953. return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  3954. };
  3955. };
  3956. // File:src/math/Triangle.js
  3957. /**
  3958. * @author bhouston / http://exocortex.com
  3959. * @author mrdoob / http://mrdoob.com/
  3960. */
  3961. THREE.Triangle = function ( a, b, c ) {
  3962. this.a = ( a !== undefined ) ? a : new THREE.Vector3();
  3963. this.b = ( b !== undefined ) ? b : new THREE.Vector3();
  3964. this.c = ( c !== undefined ) ? c : new THREE.Vector3();
  3965. };
  3966. THREE.Triangle.normal = function () {
  3967. var v0 = new THREE.Vector3();
  3968. return function ( a, b, c, optionalTarget ) {
  3969. var result = optionalTarget || new THREE.Vector3();
  3970. result.subVectors( c, b );
  3971. v0.subVectors( a, b );
  3972. result.cross( v0 );
  3973. var resultLengthSq = result.lengthSq();
  3974. if ( resultLengthSq > 0 ) {
  3975. return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
  3976. }
  3977. return result.set( 0, 0, 0 );
  3978. };
  3979. }();
  3980. // static/instance method to calculate barycoordinates
  3981. // based on: http://www.blackpawn.com/texts/pointinpoly/default.html
  3982. THREE.Triangle.barycoordFromPoint = function () {
  3983. var v0 = new THREE.Vector3();
  3984. var v1 = new THREE.Vector3();
  3985. var v2 = new THREE.Vector3();
  3986. return function ( point, a, b, c, optionalTarget ) {
  3987. v0.subVectors( c, a );
  3988. v1.subVectors( b, a );
  3989. v2.subVectors( point, a );
  3990. var dot00 = v0.dot( v0 );
  3991. var dot01 = v0.dot( v1 );
  3992. var dot02 = v0.dot( v2 );
  3993. var dot11 = v1.dot( v1 );
  3994. var dot12 = v1.dot( v2 );
  3995. var denom = ( dot00 * dot11 - dot01 * dot01 );
  3996. var result = optionalTarget || new THREE.Vector3();
  3997. // colinear or singular triangle
  3998. if ( denom == 0 ) {
  3999. // arbitrary location outside of triangle?
  4000. // not sure if this is the best idea, maybe should be returning undefined
  4001. return result.set( - 2, - 1, - 1 );
  4002. }
  4003. var invDenom = 1 / denom;
  4004. var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  4005. var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  4006. // barycoordinates must always sum to 1
  4007. return result.set( 1 - u - v, v, u );
  4008. };
  4009. }();
  4010. THREE.Triangle.containsPoint = function () {
  4011. var v1 = new THREE.Vector3();
  4012. return function ( point, a, b, c ) {
  4013. var result = THREE.Triangle.barycoordFromPoint( point, a, b, c, v1 );
  4014. return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 );
  4015. };
  4016. }();
  4017. THREE.Triangle.prototype = {
  4018. constructor: THREE.Triangle,
  4019. set: function ( a, b, c ) {
  4020. this.a.copy( a );
  4021. this.b.copy( b );
  4022. this.c.copy( c );
  4023. return this;
  4024. },
  4025. setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
  4026. this.a.copy( points[ i0 ] );
  4027. this.b.copy( points[ i1 ] );
  4028. this.c.copy( points[ i2 ] );
  4029. return this;
  4030. },
  4031. copy: function ( triangle ) {
  4032. this.a.copy( triangle.a );
  4033. this.b.copy( triangle.b );
  4034. this.c.copy( triangle.c );
  4035. return this;
  4036. },
  4037. area: function () {
  4038. var v0 = new THREE.Vector3();
  4039. var v1 = new THREE.Vector3();
  4040. return function () {
  4041. v0.subVectors( this.c, this.b );
  4042. v1.subVectors( this.a, this.b );
  4043. return v0.cross( v1 ).length() * 0.5;
  4044. };
  4045. }(),
  4046. midpoint: function ( optionalTarget ) {
  4047. var result = optionalTarget || new THREE.Vector3();
  4048. return result.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
  4049. },
  4050. normal: function ( optionalTarget ) {
  4051. return THREE.Triangle.normal( this.a, this.b, this.c, optionalTarget );
  4052. },
  4053. plane: function ( optionalTarget ) {
  4054. var result = optionalTarget || new THREE.Plane();
  4055. return result.setFromCoplanarPoints( this.a, this.b, this.c );
  4056. },
  4057. barycoordFromPoint: function ( point, optionalTarget ) {
  4058. return THREE.Triangle.barycoordFromPoint( point, this.a, this.b, this.c, optionalTarget );
  4059. },
  4060. containsPoint: function ( point ) {
  4061. return THREE.Triangle.containsPoint( point, this.a, this.b, this.c );
  4062. },
  4063. equals: function ( triangle ) {
  4064. return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
  4065. },
  4066. clone: function () {
  4067. return new THREE.Triangle().copy( this );
  4068. }
  4069. };
  4070. // File:src/core/Clock.js
  4071. /**
  4072. * @author alteredq / http://alteredqualia.com/
  4073. */
  4074. THREE.Clock = function ( autoStart ) {
  4075. this.autoStart = ( autoStart !== undefined ) ? autoStart : true;
  4076. this.startTime = 0;
  4077. this.oldTime = 0;
  4078. this.elapsedTime = 0;
  4079. this.running = false;
  4080. };
  4081. THREE.Clock.prototype = {
  4082. constructor: THREE.Clock,
  4083. start: function () {
  4084. this.startTime = self.performance !== undefined && self.performance.now !== undefined
  4085. ? self.performance.now()
  4086. : Date.now();
  4087. this.oldTime = this.startTime;
  4088. this.running = true;
  4089. },
  4090. stop: function () {
  4091. this.getElapsedTime();
  4092. this.running = false;
  4093. },
  4094. getElapsedTime: function () {
  4095. this.getDelta();
  4096. return this.elapsedTime;
  4097. },
  4098. getDelta: function () {
  4099. var diff = 0;
  4100. if ( this.autoStart && ! this.running ) {
  4101. this.start();
  4102. }
  4103. if ( this.running ) {
  4104. var newTime = self.performance !== undefined && self.performance.now !== undefined
  4105. ? self.performance.now()
  4106. : Date.now();
  4107. diff = 0.001 * ( newTime - this.oldTime );
  4108. this.oldTime = newTime;
  4109. this.elapsedTime += diff;
  4110. }
  4111. return diff;
  4112. }
  4113. };
  4114. // File:src/core/EventDispatcher.js
  4115. /**
  4116. * https://github.com/mrdoob/eventdispatcher.js/
  4117. */
  4118. THREE.EventDispatcher = function () {}
  4119. THREE.EventDispatcher.prototype = {
  4120. constructor: THREE.EventDispatcher,
  4121. apply: function ( object ) {
  4122. object.addEventListener = THREE.EventDispatcher.prototype.addEventListener;
  4123. object.hasEventListener = THREE.EventDispatcher.prototype.hasEventListener;
  4124. object.removeEventListener = THREE.EventDispatcher.prototype.removeEventListener;
  4125. object.dispatchEvent = THREE.EventDispatcher.prototype.dispatchEvent;
  4126. },
  4127. addEventListener: function ( type, listener ) {
  4128. if ( this._listeners === undefined ) this._listeners = {};
  4129. var listeners = this._listeners;
  4130. if ( listeners[ type ] === undefined ) {
  4131. listeners[ type ] = [];
  4132. }
  4133. if ( listeners[ type ].indexOf( listener ) === - 1 ) {
  4134. listeners[ type ].push( listener );
  4135. }
  4136. },
  4137. hasEventListener: function ( type, listener ) {
  4138. if ( this._listeners === undefined ) return false;
  4139. var listeners = this._listeners;
  4140. if ( listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1 ) {
  4141. return true;
  4142. }
  4143. return false;
  4144. },
  4145. removeEventListener: function ( type, listener ) {
  4146. if ( this._listeners === undefined ) return;
  4147. var listeners = this._listeners;
  4148. var listenerArray = listeners[ type ];
  4149. if ( listenerArray !== undefined ) {
  4150. var index = listenerArray.indexOf( listener );
  4151. if ( index !== - 1 ) {
  4152. listenerArray.splice( index, 1 );
  4153. }
  4154. }
  4155. },
  4156. dispatchEvent: function ( event ) {
  4157. if ( this._listeners === undefined ) return;
  4158. var listeners = this._listeners;
  4159. var listenerArray = listeners[ event.type ];
  4160. if ( listenerArray !== undefined ) {
  4161. event.target = this;
  4162. var array = [];
  4163. var length = listenerArray.length;
  4164. for ( var i = 0; i < length; i ++ ) {
  4165. array[ i ] = listenerArray[ i ];
  4166. }
  4167. for ( var i = 0; i < length; i ++ ) {
  4168. array[ i ].call( this, event );
  4169. }
  4170. }
  4171. }
  4172. };
  4173. // File:src/core/Raycaster.js
  4174. /**
  4175. * @author mrdoob / http://mrdoob.com/
  4176. * @author bhouston / http://exocortex.com/
  4177. * @author stephomi / http://stephaneginier.com/
  4178. */
  4179. ( function ( THREE ) {
  4180. THREE.Raycaster = function ( origin, direction, near, far ) {
  4181. this.ray = new THREE.Ray( origin, direction );
  4182. // direction is assumed to be normalized (for accurate distance calculations)
  4183. this.near = near || 0;
  4184. this.far = far || Infinity;
  4185. this.params = {
  4186. Sprite: {},
  4187. Mesh: {},
  4188. PointCloud: { threshold: 1 },
  4189. LOD: {},
  4190. Line: {}
  4191. };
  4192. };
  4193. var descSort = function ( a, b ) {
  4194. return a.distance - b.distance;
  4195. };
  4196. var intersectObject = function ( object, raycaster, intersects, recursive ) {
  4197. object.raycast( raycaster, intersects );
  4198. if ( recursive === true ) {
  4199. var children = object.children;
  4200. for ( var i = 0, l = children.length; i < l; i ++ ) {
  4201. intersectObject( children[ i ], raycaster, intersects, true );
  4202. }
  4203. }
  4204. };
  4205. //
  4206. THREE.Raycaster.prototype = {
  4207. constructor: THREE.Raycaster,
  4208. precision: 0.0001,
  4209. linePrecision: 1,
  4210. set: function ( origin, direction ) {
  4211. this.ray.set( origin, direction );
  4212. // direction is assumed to be normalized (for accurate distance calculations)
  4213. },
  4214. intersectObject: function ( object, recursive ) {
  4215. var intersects = [];
  4216. intersectObject( object, this, intersects, recursive );
  4217. intersects.sort( descSort );
  4218. return intersects;
  4219. },
  4220. intersectObjects: function ( objects, recursive ) {
  4221. var intersects = [];
  4222. if ( objects instanceof Array === false ) {
  4223. console.log( 'THREE.Raycaster.intersectObjects: objects is not an Array.' );
  4224. return intersects;
  4225. }
  4226. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  4227. intersectObject( objects[ i ], this, intersects, recursive );
  4228. }
  4229. intersects.sort( descSort );
  4230. return intersects;
  4231. }
  4232. };
  4233. }( THREE ) );
  4234. // File:src/core/Object3D.js
  4235. /**
  4236. * @author mrdoob / http://mrdoob.com/
  4237. * @author mikael emtinger / http://gomo.se/
  4238. * @author alteredq / http://alteredqualia.com/
  4239. * @author WestLangley / http://github.com/WestLangley
  4240. */
  4241. THREE.Object3D = function () {
  4242. Object.defineProperty( this, 'id', { value: THREE.Object3DIdCount ++ } );
  4243. this.uuid = THREE.Math.generateUUID();
  4244. this.name = '';
  4245. this.type = 'Object3D';
  4246. this.parent = undefined;
  4247. this.children = [];
  4248. this.up = THREE.Object3D.DefaultUp.clone();
  4249. var scope = this;
  4250. var position = new THREE.Vector3();
  4251. var rotation = new THREE.Euler();
  4252. var quaternion = new THREE.Quaternion();
  4253. var scale = new THREE.Vector3( 1, 1, 1 );
  4254. var onRotationChange = function () {
  4255. quaternion.setFromEuler( rotation, false );
  4256. };
  4257. var onQuaternionChange = function () {
  4258. rotation.setFromQuaternion( quaternion, undefined, false );
  4259. };
  4260. rotation.onChange( onRotationChange );
  4261. quaternion.onChange( onQuaternionChange );
  4262. Object.defineProperties( this, {
  4263. position: {
  4264. enumerable: true,
  4265. value: position
  4266. },
  4267. rotation: {
  4268. enumerable: true,
  4269. value: rotation
  4270. },
  4271. quaternion: {
  4272. enumerable: true,
  4273. value: quaternion
  4274. },
  4275. scale: {
  4276. enumerable: true,
  4277. value: scale
  4278. },
  4279. } );
  4280. this.renderDepth = null;
  4281. this.rotationAutoUpdate = true;
  4282. this.matrix = new THREE.Matrix4();
  4283. this.matrixWorld = new THREE.Matrix4();
  4284. this.matrixAutoUpdate = true;
  4285. this.matrixWorldNeedsUpdate = false;
  4286. this.visible = true;
  4287. this.castShadow = false;
  4288. this.receiveShadow = false;
  4289. this.frustumCulled = true;
  4290. this.userData = {};
  4291. };
  4292. THREE.Object3D.DefaultUp = new THREE.Vector3( 0, 1, 0 );
  4293. THREE.Object3D.prototype = {
  4294. constructor: THREE.Object3D,
  4295. get eulerOrder () {
  4296. console.warn( 'THREE.Object3D: .eulerOrder has been moved to .rotation.order.' );
  4297. return this.rotation.order;
  4298. },
  4299. set eulerOrder ( value ) {
  4300. console.warn( 'THREE.Object3D: .eulerOrder has been moved to .rotation.order.' );
  4301. this.rotation.order = value;
  4302. },
  4303. get useQuaternion () {
  4304. console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
  4305. },
  4306. set useQuaternion ( value ) {
  4307. console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' );
  4308. },
  4309. applyMatrix: function ( matrix ) {
  4310. this.matrix.multiplyMatrices( matrix, this.matrix );
  4311. this.matrix.decompose( this.position, this.quaternion, this.scale );
  4312. },
  4313. setRotationFromAxisAngle: function ( axis, angle ) {
  4314. // assumes axis is normalized
  4315. this.quaternion.setFromAxisAngle( axis, angle );
  4316. },
  4317. setRotationFromEuler: function ( euler ) {
  4318. this.quaternion.setFromEuler( euler, true );
  4319. },
  4320. setRotationFromMatrix: function ( m ) {
  4321. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  4322. this.quaternion.setFromRotationMatrix( m );
  4323. },
  4324. setRotationFromQuaternion: function ( q ) {
  4325. // assumes q is normalized
  4326. this.quaternion.copy( q );
  4327. },
  4328. rotateOnAxis: function () {
  4329. // rotate object on axis in object space
  4330. // axis is assumed to be normalized
  4331. var q1 = new THREE.Quaternion();
  4332. return function ( axis, angle ) {
  4333. q1.setFromAxisAngle( axis, angle );
  4334. this.quaternion.multiply( q1 );
  4335. return this;
  4336. }
  4337. }(),
  4338. rotateX: function () {
  4339. var v1 = new THREE.Vector3( 1, 0, 0 );
  4340. return function ( angle ) {
  4341. return this.rotateOnAxis( v1, angle );
  4342. };
  4343. }(),
  4344. rotateY: function () {
  4345. var v1 = new THREE.Vector3( 0, 1, 0 );
  4346. return function ( angle ) {
  4347. return this.rotateOnAxis( v1, angle );
  4348. };
  4349. }(),
  4350. rotateZ: function () {
  4351. var v1 = new THREE.Vector3( 0, 0, 1 );
  4352. return function ( angle ) {
  4353. return this.rotateOnAxis( v1, angle );
  4354. };
  4355. }(),
  4356. translateOnAxis: function () {
  4357. // translate object by distance along axis in object space
  4358. // axis is assumed to be normalized
  4359. var v1 = new THREE.Vector3();
  4360. return function ( axis, distance ) {
  4361. v1.copy( axis ).applyQuaternion( this.quaternion );
  4362. this.position.add( v1.multiplyScalar( distance ) );
  4363. return this;
  4364. }
  4365. }(),
  4366. translate: function ( distance, axis ) {
  4367. console.warn( 'THREE.Object3D: .translate() has been removed. Use .translateOnAxis( axis, distance ) instead.' );
  4368. return this.translateOnAxis( axis, distance );
  4369. },
  4370. translateX: function () {
  4371. var v1 = new THREE.Vector3( 1, 0, 0 );
  4372. return function ( distance ) {
  4373. return this.translateOnAxis( v1, distance );
  4374. };
  4375. }(),
  4376. translateY: function () {
  4377. var v1 = new THREE.Vector3( 0, 1, 0 );
  4378. return function ( distance ) {
  4379. return this.translateOnAxis( v1, distance );
  4380. };
  4381. }(),
  4382. translateZ: function () {
  4383. var v1 = new THREE.Vector3( 0, 0, 1 );
  4384. return function ( distance ) {
  4385. return this.translateOnAxis( v1, distance );
  4386. };
  4387. }(),
  4388. localToWorld: function ( vector ) {
  4389. return vector.applyMatrix4( this.matrixWorld );
  4390. },
  4391. worldToLocal: function () {
  4392. var m1 = new THREE.Matrix4();
  4393. return function ( vector ) {
  4394. return vector.applyMatrix4( m1.getInverse( this.matrixWorld ) );
  4395. };
  4396. }(),
  4397. lookAt: function () {
  4398. // This routine does not support objects with rotated and/or translated parent(s)
  4399. var m1 = new THREE.Matrix4();
  4400. return function ( vector ) {
  4401. m1.lookAt( vector, this.position, this.up );
  4402. this.quaternion.setFromRotationMatrix( m1 );
  4403. };
  4404. }(),
  4405. add: function ( object ) {
  4406. if ( arguments.length > 1 ) {
  4407. for ( var i = 0; i < arguments.length; i++ ) {
  4408. this.add( arguments[ i ] );
  4409. }
  4410. return this;
  4411. };
  4412. if ( object === this ) {
  4413. console.error( "THREE.Object3D.add:", object, "can't be added as a child of itself." );
  4414. return this;
  4415. }
  4416. if ( object instanceof THREE.Object3D ) {
  4417. if ( object.parent !== undefined ) {
  4418. object.parent.remove( object );
  4419. }
  4420. object.parent = this;
  4421. object.dispatchEvent( { type: 'added' } );
  4422. this.children.push( object );
  4423. } else {
  4424. console.error( "THREE.Object3D.add:", object, "is not an instance of THREE.Object3D." );
  4425. }
  4426. return this;
  4427. },
  4428. remove: function ( object ) {
  4429. if ( arguments.length > 1 ) {
  4430. for ( var i = 0; i < arguments.length; i++ ) {
  4431. this.remove( arguments[ i ] );
  4432. }
  4433. };
  4434. var index = this.children.indexOf( object );
  4435. if ( index !== - 1 ) {
  4436. object.parent = undefined;
  4437. object.dispatchEvent( { type: 'removed' } );
  4438. this.children.splice( index, 1 );
  4439. }
  4440. },
  4441. getChildByName: function ( name, recursive ) {
  4442. console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' );
  4443. return this.getObjectByName( name, recursive );
  4444. },
  4445. getObjectById: function ( id, recursive ) {
  4446. if ( this.id === id ) return this;
  4447. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  4448. var child = this.children[ i ];
  4449. var object = child.getObjectById( id, recursive );
  4450. if ( object !== undefined ) {
  4451. return object;
  4452. }
  4453. }
  4454. return undefined;
  4455. },
  4456. getObjectByName: function ( name, recursive ) {
  4457. if ( this.name === name ) return this;
  4458. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  4459. var child = this.children[ i ];
  4460. var object = child.getObjectByName( name, recursive );
  4461. if ( object !== undefined ) {
  4462. return object;
  4463. }
  4464. }
  4465. return undefined;
  4466. },
  4467. getWorldPosition: function ( optionalTarget ) {
  4468. var result = optionalTarget || new THREE.Vector3();
  4469. this.updateMatrixWorld( true );
  4470. return result.setFromMatrixPosition( this.matrixWorld );
  4471. },
  4472. getWorldQuaternion: function () {
  4473. var position = new THREE.Vector3();
  4474. var scale = new THREE.Vector3();
  4475. return function ( optionalTarget ) {
  4476. var result = optionalTarget || new THREE.Quaternion();
  4477. this.updateMatrixWorld( true );
  4478. this.matrixWorld.decompose( position, result, scale );
  4479. return result;
  4480. }
  4481. }(),
  4482. getWorldRotation: function () {
  4483. var quaternion = new THREE.Quaternion();
  4484. return function ( optionalTarget ) {
  4485. var result = optionalTarget || new THREE.Euler();
  4486. this.getWorldQuaternion( quaternion );
  4487. return result.setFromQuaternion( quaternion, this.rotation.order, false );
  4488. }
  4489. }(),
  4490. getWorldScale: function () {
  4491. var position = new THREE.Vector3();
  4492. var quaternion = new THREE.Quaternion();
  4493. return function ( optionalTarget ) {
  4494. var result = optionalTarget || new THREE.Vector3();
  4495. this.updateMatrixWorld( true );
  4496. this.matrixWorld.decompose( position, quaternion, result );
  4497. return result;
  4498. }
  4499. }(),
  4500. getWorldDirection: function () {
  4501. var quaternion = new THREE.Quaternion();
  4502. return function ( optionalTarget ) {
  4503. var result = optionalTarget || new THREE.Vector3();
  4504. this.getWorldQuaternion( quaternion );
  4505. return result.set( 0, 0, 1 ).applyQuaternion( quaternion );
  4506. }
  4507. }(),
  4508. raycast: function () {},
  4509. traverse: function ( callback ) {
  4510. callback( this );
  4511. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  4512. this.children[ i ].traverse( callback );
  4513. }
  4514. },
  4515. traverseVisible: function ( callback ) {
  4516. if ( this.visible === false ) return;
  4517. callback( this );
  4518. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  4519. this.children[ i ].traverseVisible( callback );
  4520. }
  4521. },
  4522. traverseAncestors: function ( callback ) {
  4523. if ( this.parent ) {
  4524. callback( this.parent );
  4525. this.parent.traverseAncestors( callback );
  4526. }
  4527. },
  4528. updateMatrix: function () {
  4529. this.matrix.compose( this.position, this.quaternion, this.scale );
  4530. this.matrixWorldNeedsUpdate = true;
  4531. },
  4532. updateMatrixWorld: function ( force ) {
  4533. if ( this.matrixAutoUpdate === true ) this.updateMatrix();
  4534. if ( this.matrixWorldNeedsUpdate === true || force === true ) {
  4535. if ( this.parent === undefined ) {
  4536. this.matrixWorld.copy( this.matrix );
  4537. } else {
  4538. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  4539. }
  4540. this.matrixWorldNeedsUpdate = false;
  4541. force = true;
  4542. }
  4543. // update children
  4544. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  4545. this.children[ i ].updateMatrixWorld( force );
  4546. }
  4547. },
  4548. toJSON: function () {
  4549. var output = {
  4550. metadata: {
  4551. version: 4.3,
  4552. type: 'Object',
  4553. generator: 'ObjectExporter'
  4554. }
  4555. };
  4556. //
  4557. var geometries = {};
  4558. var parseGeometry = function ( geometry ) {
  4559. if ( output.geometries === undefined ) {
  4560. output.geometries = [];
  4561. }
  4562. if ( geometries[ geometry.uuid ] === undefined ) {
  4563. var json = geometry.toJSON();
  4564. delete json.metadata;
  4565. geometries[ geometry.uuid ] = json;
  4566. output.geometries.push( json );
  4567. }
  4568. return geometry.uuid;
  4569. };
  4570. //
  4571. var materials = {};
  4572. var parseMaterial = function ( material ) {
  4573. if ( output.materials === undefined ) {
  4574. output.materials = [];
  4575. }
  4576. if ( materials[ material.uuid ] === undefined ) {
  4577. var json = material.toJSON();
  4578. delete json.metadata;
  4579. materials[ material.uuid ] = json;
  4580. output.materials.push( json );
  4581. }
  4582. return material.uuid;
  4583. };
  4584. //
  4585. var parseObject = function ( object ) {
  4586. var data = {};
  4587. data.uuid = object.uuid;
  4588. data.type = object.type;
  4589. if ( object.name !== '' ) data.name = object.name;
  4590. if ( JSON.stringify( object.userData ) !== '{}' ) data.userData = object.userData;
  4591. if ( object.visible !== true ) data.visible = object.visible;
  4592. if ( object instanceof THREE.PerspectiveCamera ) {
  4593. data.fov = object.fov;
  4594. data.aspect = object.aspect;
  4595. data.near = object.near;
  4596. data.far = object.far;
  4597. } else if ( object instanceof THREE.OrthographicCamera ) {
  4598. data.left = object.left;
  4599. data.right = object.right;
  4600. data.top = object.top;
  4601. data.bottom = object.bottom;
  4602. data.near = object.near;
  4603. data.far = object.far;
  4604. } else if ( object instanceof THREE.AmbientLight ) {
  4605. data.color = object.color.getHex();
  4606. } else if ( object instanceof THREE.DirectionalLight ) {
  4607. data.color = object.color.getHex();
  4608. data.intensity = object.intensity;
  4609. } else if ( object instanceof THREE.PointLight ) {
  4610. data.color = object.color.getHex();
  4611. data.intensity = object.intensity;
  4612. data.distance = object.distance;
  4613. } else if ( object instanceof THREE.SpotLight ) {
  4614. data.color = object.color.getHex();
  4615. data.intensity = object.intensity;
  4616. data.distance = object.distance;
  4617. data.angle = object.angle;
  4618. data.exponent = object.exponent;
  4619. } else if ( object instanceof THREE.HemisphereLight ) {
  4620. data.color = object.color.getHex();
  4621. data.groundColor = object.groundColor.getHex();
  4622. } else if ( object instanceof THREE.Mesh ) {
  4623. data.geometry = parseGeometry( object.geometry );
  4624. data.material = parseMaterial( object.material );
  4625. } else if ( object instanceof THREE.Line ) {
  4626. data.geometry = parseGeometry( object.geometry );
  4627. data.material = parseMaterial( object.material );
  4628. } else if ( object instanceof THREE.Sprite ) {
  4629. data.material = parseMaterial( object.material );
  4630. }
  4631. data.matrix = object.matrix.toArray();
  4632. if ( object.children.length > 0 ) {
  4633. data.children = [];
  4634. for ( var i = 0; i < object.children.length; i ++ ) {
  4635. data.children.push( parseObject( object.children[ i ] ) );
  4636. }
  4637. }
  4638. return data;
  4639. }
  4640. output.object = parseObject( this );
  4641. return output;
  4642. },
  4643. clone: function ( object, recursive ) {
  4644. if ( object === undefined ) object = new THREE.Object3D();
  4645. if ( recursive === undefined ) recursive = true;
  4646. object.name = this.name;
  4647. object.up.copy( this.up );
  4648. object.position.copy( this.position );
  4649. object.quaternion.copy( this.quaternion );
  4650. object.scale.copy( this.scale );
  4651. object.renderDepth = this.renderDepth;
  4652. object.rotationAutoUpdate = this.rotationAutoUpdate;
  4653. object.matrix.copy( this.matrix );
  4654. object.matrixWorld.copy( this.matrixWorld );
  4655. object.matrixAutoUpdate = this.matrixAutoUpdate;
  4656. object.matrixWorldNeedsUpdate = this.matrixWorldNeedsUpdate;
  4657. object.visible = this.visible;
  4658. object.castShadow = this.castShadow;
  4659. object.receiveShadow = this.receiveShadow;
  4660. object.frustumCulled = this.frustumCulled;
  4661. object.userData = JSON.parse( JSON.stringify( this.userData ) );
  4662. if ( recursive === true ) {
  4663. for ( var i = 0; i < this.children.length; i ++ ) {
  4664. var child = this.children[ i ];
  4665. object.add( child.clone() );
  4666. }
  4667. }
  4668. return object;
  4669. }
  4670. };
  4671. THREE.EventDispatcher.prototype.apply( THREE.Object3D.prototype );
  4672. THREE.Object3DIdCount = 0;
  4673. // File:src/core/Face3.js
  4674. /**
  4675. * @author mrdoob / http://mrdoob.com/
  4676. * @author alteredq / http://alteredqualia.com/
  4677. */
  4678. THREE.Face3 = function ( a, b, c, normal, color, materialIndex ) {
  4679. this.a = a;
  4680. this.b = b;
  4681. this.c = c;
  4682. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  4683. this.vertexNormals = normal instanceof Array ? normal : [];
  4684. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  4685. this.vertexColors = color instanceof Array ? color : [];
  4686. this.vertexTangents = [];
  4687. this.materialIndex = materialIndex !== undefined ? materialIndex : 0;
  4688. };
  4689. THREE.Face3.prototype = {
  4690. constructor: THREE.Face3,
  4691. clone: function () {
  4692. var face = new THREE.Face3( this.a, this.b, this.c );
  4693. face.normal.copy( this.normal );
  4694. face.color.copy( this.color );
  4695. face.materialIndex = this.materialIndex;
  4696. for ( var i = 0, il = this.vertexNormals.length; i < il; i ++ ) {
  4697. face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  4698. }
  4699. for ( var i = 0, il = this.vertexColors.length; i < il; i ++ ) {
  4700. face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  4701. }
  4702. for ( var i = 0, il = this.vertexTangents.length; i < il; i ++ ) {
  4703. face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  4704. }
  4705. return face;
  4706. }
  4707. };
  4708. // File:src/core/Face4.js
  4709. /**
  4710. * @author mrdoob / http://mrdoob.com/
  4711. */
  4712. THREE.Face4 = function ( a, b, c, d, normal, color, materialIndex ) {
  4713. console.warn( 'THREE.Face4 has been removed. A THREE.Face3 will be created instead.' )
  4714. return new THREE.Face3( a, b, c, normal, color, materialIndex );
  4715. };
  4716. // File:src/core/BufferAttribute.js
  4717. /**
  4718. * @author mrdoob / http://mrdoob.com/
  4719. */
  4720. THREE.BufferAttribute = function ( array, itemSize ) {
  4721. this.array = array;
  4722. this.itemSize = itemSize;
  4723. this.needsUpdate = false;
  4724. };
  4725. THREE.BufferAttribute.prototype = {
  4726. constructor: THREE.BufferAttribute,
  4727. get length () {
  4728. return this.array.length;
  4729. },
  4730. copyAt: function ( index1, attribute, index2 ) {
  4731. index1 *= this.itemSize;
  4732. index2 *= attribute.itemSize;
  4733. for ( var i = 0, l = this.itemSize; i < l; i ++ ) {
  4734. this.array[ index1 + i ] = attribute.array[ index2 + i ];
  4735. }
  4736. },
  4737. set: function ( value ) {
  4738. this.array.set( value );
  4739. return this;
  4740. },
  4741. setX: function ( index, x ) {
  4742. this.array[ index * this.itemSize ] = x;
  4743. return this;
  4744. },
  4745. setY: function ( index, y ) {
  4746. this.array[ index * this.itemSize + 1 ] = y;
  4747. return this;
  4748. },
  4749. setZ: function ( index, z ) {
  4750. this.array[ index * this.itemSize + 2 ] = z;
  4751. return this;
  4752. },
  4753. setXY: function ( index, x, y ) {
  4754. index *= this.itemSize;
  4755. this.array[ index ] = x;
  4756. this.array[ index + 1 ] = y;
  4757. return this;
  4758. },
  4759. setXYZ: function ( index, x, y, z ) {
  4760. index *= this.itemSize;
  4761. this.array[ index ] = x;
  4762. this.array[ index + 1 ] = y;
  4763. this.array[ index + 2 ] = z;
  4764. return this;
  4765. },
  4766. setXYZW: function ( index, x, y, z, w ) {
  4767. index *= this.itemSize;
  4768. this.array[ index ] = x;
  4769. this.array[ index + 1 ] = y;
  4770. this.array[ index + 2 ] = z;
  4771. this.array[ index + 3 ] = w;
  4772. return this;
  4773. },
  4774. clone: function () {
  4775. return new THREE.BufferAttribute( new this.array.constructor( this.array ), this.itemSize );
  4776. }
  4777. };
  4778. //
  4779. THREE.Int8Attribute = function ( data, itemSize ) {
  4780. console.warn( 'THREE.Int8Attribute has been removed. Use THREE.BufferAttribute( array, itemSize ) instead.' );
  4781. return new THREE.BufferAttribute( data, itemSize );
  4782. };
  4783. THREE.Uint8Attribute = function ( data, itemSize ) {
  4784. console.warn( 'THREE.Uint8Attribute has been removed. Use THREE.BufferAttribute( array, itemSize ) instead.' );
  4785. return new THREE.BufferAttribute( data, itemSize );
  4786. };
  4787. THREE.Uint8ClampedAttribute = function ( data, itemSize ) {
  4788. console.warn( 'THREE.Uint8ClampedAttribute has been removed. Use THREE.BufferAttribute( array, itemSize ) instead.' );
  4789. return new THREE.BufferAttribute( data, itemSize );
  4790. };
  4791. THREE.Int16Attribute = function ( data, itemSize ) {
  4792. console.warn( 'THREE.Int16Attribute has been removed. Use THREE.BufferAttribute( array, itemSize ) instead.' );
  4793. return new THREE.BufferAttribute( data, itemSize );
  4794. };
  4795. THREE.Uint16Attribute = function ( data, itemSize ) {
  4796. console.warn( 'THREE.Uint16Attribute has been removed. Use THREE.BufferAttribute( array, itemSize ) instead.' );
  4797. return new THREE.BufferAttribute( data, itemSize );
  4798. };
  4799. THREE.Int32Attribute = function ( data, itemSize ) {
  4800. console.warn( 'THREE.Int32Attribute has been removed. Use THREE.BufferAttribute( array, itemSize ) instead.' );
  4801. return new THREE.BufferAttribute( data, itemSize );
  4802. };
  4803. THREE.Uint32Attribute = function ( data, itemSize ) {
  4804. console.warn( 'THREE.Uint32Attribute has been removed. Use THREE.BufferAttribute( array, itemSize ) instead.' );
  4805. return new THREE.BufferAttribute( data, itemSize );
  4806. };
  4807. THREE.Float32Attribute = function ( data, itemSize ) {
  4808. console.warn( 'THREE.Float32Attribute has been removed. Use THREE.BufferAttribute( array, itemSize ) instead.' );
  4809. return new THREE.BufferAttribute( data, itemSize );
  4810. };
  4811. THREE.Float64Attribute = function ( data, itemSize ) {
  4812. console.warn( 'THREE.Float64Attribute has been removed. Use THREE.BufferAttribute( array, itemSize ) instead.' );
  4813. return new THREE.BufferAttribute( data, itemSize );
  4814. };
  4815. // File:src/core/BufferGeometry.js
  4816. /**
  4817. * @author alteredq / http://alteredqualia.com/
  4818. * @author mrdoob / http://mrdoob.com/
  4819. */
  4820. THREE.BufferGeometry = function () {
  4821. Object.defineProperty( this, 'id', { value: THREE.GeometryIdCount ++ } );
  4822. this.uuid = THREE.Math.generateUUID();
  4823. this.name = '';
  4824. this.type = 'BufferGeometry';
  4825. this.attributes = {};
  4826. this.attributesKeys = [];
  4827. this.drawcalls = [];
  4828. this.offsets = this.drawcalls; // backwards compatibility
  4829. this.boundingBox = null;
  4830. this.boundingSphere = null;
  4831. };
  4832. THREE.BufferGeometry.prototype = {
  4833. constructor: THREE.BufferGeometry,
  4834. addAttribute: function ( name, attribute ) {
  4835. if ( attribute instanceof THREE.BufferAttribute === false ) {
  4836. console.warn( 'THREE.BufferGeometry: .addAttribute() now expects ( name, attribute ).' );
  4837. this.attributes[ name ] = { array: arguments[ 1 ], itemSize: arguments[ 2 ] };
  4838. return;
  4839. }
  4840. this.attributes[ name ] = attribute;
  4841. this.attributesKeys = Object.keys( this.attributes );
  4842. },
  4843. getAttribute: function ( name ) {
  4844. return this.attributes[ name ];
  4845. },
  4846. addDrawCall: function ( start, count, indexOffset ) {
  4847. this.drawcalls.push( {
  4848. start: start,
  4849. count: count,
  4850. index: indexOffset !== undefined ? indexOffset : 0
  4851. } );
  4852. },
  4853. applyMatrix: function ( matrix ) {
  4854. var position = this.attributes.position;
  4855. if ( position !== undefined ) {
  4856. matrix.applyToVector3Array( position.array );
  4857. position.needsUpdate = true;
  4858. }
  4859. var normal = this.attributes.normal;
  4860. if ( normal !== undefined ) {
  4861. var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  4862. normalMatrix.applyToVector3Array( normal.array );
  4863. normal.needsUpdate = true;
  4864. }
  4865. },
  4866. center: function () {
  4867. // TODO
  4868. },
  4869. fromGeometry: function ( geometry, settings ) {
  4870. settings = settings || { 'vertexColors': THREE.NoColors };
  4871. var vertices = geometry.vertices;
  4872. var faces = geometry.faces;
  4873. var faceVertexUvs = geometry.faceVertexUvs;
  4874. var vertexColors = settings.vertexColors;
  4875. var hasFaceVertexUv = faceVertexUvs[ 0 ].length > 0;
  4876. var hasFaceVertexNormals = faces[ 0 ].vertexNormals.length == 3;
  4877. var positions = new Float32Array( faces.length * 3 * 3 );
  4878. this.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  4879. var normals = new Float32Array( faces.length * 3 * 3 );
  4880. this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  4881. if ( vertexColors !== THREE.NoColors ) {
  4882. var colors = new Float32Array( faces.length * 3 * 3 );
  4883. this.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  4884. }
  4885. if ( hasFaceVertexUv === true ) {
  4886. var uvs = new Float32Array( faces.length * 3 * 2 );
  4887. this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
  4888. }
  4889. for ( var i = 0, i2 = 0, i3 = 0; i < faces.length; i ++, i2 += 6, i3 += 9 ) {
  4890. var face = faces[ i ];
  4891. var a = vertices[ face.a ];
  4892. var b = vertices[ face.b ];
  4893. var c = vertices[ face.c ];
  4894. positions[ i3 ] = a.x;
  4895. positions[ i3 + 1 ] = a.y;
  4896. positions[ i3 + 2 ] = a.z;
  4897. positions[ i3 + 3 ] = b.x;
  4898. positions[ i3 + 4 ] = b.y;
  4899. positions[ i3 + 5 ] = b.z;
  4900. positions[ i3 + 6 ] = c.x;
  4901. positions[ i3 + 7 ] = c.y;
  4902. positions[ i3 + 8 ] = c.z;
  4903. if ( hasFaceVertexNormals === true ) {
  4904. var na = face.vertexNormals[ 0 ];
  4905. var nb = face.vertexNormals[ 1 ];
  4906. var nc = face.vertexNormals[ 2 ];
  4907. normals[ i3 ] = na.x;
  4908. normals[ i3 + 1 ] = na.y;
  4909. normals[ i3 + 2 ] = na.z;
  4910. normals[ i3 + 3 ] = nb.x;
  4911. normals[ i3 + 4 ] = nb.y;
  4912. normals[ i3 + 5 ] = nb.z;
  4913. normals[ i3 + 6 ] = nc.x;
  4914. normals[ i3 + 7 ] = nc.y;
  4915. normals[ i3 + 8 ] = nc.z;
  4916. } else {
  4917. var n = face.normal;
  4918. normals[ i3 ] = n.x;
  4919. normals[ i3 + 1 ] = n.y;
  4920. normals[ i3 + 2 ] = n.z;
  4921. normals[ i3 + 3 ] = n.x;
  4922. normals[ i3 + 4 ] = n.y;
  4923. normals[ i3 + 5 ] = n.z;
  4924. normals[ i3 + 6 ] = n.x;
  4925. normals[ i3 + 7 ] = n.y;
  4926. normals[ i3 + 8 ] = n.z;
  4927. }
  4928. if ( vertexColors === THREE.FaceColors ) {
  4929. var fc = face.color;
  4930. colors[ i3 ] = fc.r;
  4931. colors[ i3 + 1 ] = fc.g;
  4932. colors[ i3 + 2 ] = fc.b;
  4933. colors[ i3 + 3 ] = fc.r;
  4934. colors[ i3 + 4 ] = fc.g;
  4935. colors[ i3 + 5 ] = fc.b;
  4936. colors[ i3 + 6 ] = fc.r;
  4937. colors[ i3 + 7 ] = fc.g;
  4938. colors[ i3 + 8 ] = fc.b;
  4939. } else if ( vertexColors === THREE.VertexColors ) {
  4940. var vca = face.vertexColors[ 0 ];
  4941. var vcb = face.vertexColors[ 1 ];
  4942. var vcc = face.vertexColors[ 2 ];
  4943. colors[ i3 ] = vca.r;
  4944. colors[ i3 + 1 ] = vca.g;
  4945. colors[ i3 + 2 ] = vca.b;
  4946. colors[ i3 + 3 ] = vcb.r;
  4947. colors[ i3 + 4 ] = vcb.g;
  4948. colors[ i3 + 5 ] = vcb.b;
  4949. colors[ i3 + 6 ] = vcc.r;
  4950. colors[ i3 + 7 ] = vcc.g;
  4951. colors[ i3 + 8 ] = vcc.b;
  4952. }
  4953. if ( hasFaceVertexUv === true ) {
  4954. var uva = faceVertexUvs[ 0 ][ i ][ 0 ];
  4955. var uvb = faceVertexUvs[ 0 ][ i ][ 1 ];
  4956. var uvc = faceVertexUvs[ 0 ][ i ][ 2 ];
  4957. uvs[ i2 ] = uva.x;
  4958. uvs[ i2 + 1 ] = uva.y;
  4959. uvs[ i2 + 2 ] = uvb.x;
  4960. uvs[ i2 + 3 ] = uvb.y;
  4961. uvs[ i2 + 4 ] = uvc.x;
  4962. uvs[ i2 + 5 ] = uvc.y;
  4963. }
  4964. }
  4965. this.computeBoundingSphere()
  4966. return this;
  4967. },
  4968. computeBoundingBox: function () {
  4969. var vector = new THREE.Vector3();
  4970. return function () {
  4971. if ( this.boundingBox === null ) {
  4972. this.boundingBox = new THREE.Box3();
  4973. }
  4974. var positions = this.attributes.position.array;
  4975. if ( positions ) {
  4976. var bb = this.boundingBox;
  4977. bb.makeEmpty();
  4978. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  4979. vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  4980. bb.expandByPoint( vector );
  4981. }
  4982. }
  4983. if ( positions === undefined || positions.length === 0 ) {
  4984. this.boundingBox.min.set( 0, 0, 0 );
  4985. this.boundingBox.max.set( 0, 0, 0 );
  4986. }
  4987. if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) {
  4988. console.error( 'THREE.BufferGeometry.computeBoundingBox: Computed min/max have NaN values. The "position" attribute is likely to have NaN values.' );
  4989. }
  4990. }
  4991. }(),
  4992. computeBoundingSphere: function () {
  4993. var box = new THREE.Box3();
  4994. var vector = new THREE.Vector3();
  4995. return function () {
  4996. if ( this.boundingSphere === null ) {
  4997. this.boundingSphere = new THREE.Sphere();
  4998. }
  4999. var positions = this.attributes.position.array;
  5000. if ( positions ) {
  5001. box.makeEmpty();
  5002. var center = this.boundingSphere.center;
  5003. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  5004. vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  5005. box.expandByPoint( vector );
  5006. }
  5007. box.center( center );
  5008. // hoping to find a boundingSphere with a radius smaller than the
  5009. // boundingSphere of the boundingBox: sqrt(3) smaller in the best case
  5010. var maxRadiusSq = 0;
  5011. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  5012. vector.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  5013. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( vector ) );
  5014. }
  5015. this.boundingSphere.radius = Math.sqrt( maxRadiusSq );
  5016. if ( isNaN( this.boundingSphere.radius ) ) {
  5017. console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.' );
  5018. }
  5019. }
  5020. }
  5021. }(),
  5022. computeFaceNormals: function () {
  5023. // backwards compatibility
  5024. },
  5025. computeVertexNormals: function () {
  5026. var attributes = this.attributes;
  5027. if ( attributes.position ) {
  5028. var positions = attributes.position.array;
  5029. if ( attributes.normal === undefined ) {
  5030. this.addAttribute( 'normal', new THREE.BufferAttribute( new Float32Array( positions.length ), 3 ) );
  5031. } else {
  5032. // reset existing normals to zero
  5033. var normals = attributes.normal.array;
  5034. for ( var i = 0, il = normals.length; i < il; i ++ ) {
  5035. normals[ i ] = 0;
  5036. }
  5037. }
  5038. var normals = attributes.normal.array;
  5039. var vA, vB, vC,
  5040. pA = new THREE.Vector3(),
  5041. pB = new THREE.Vector3(),
  5042. pC = new THREE.Vector3(),
  5043. cb = new THREE.Vector3(),
  5044. ab = new THREE.Vector3();
  5045. // indexed elements
  5046. if ( attributes.index ) {
  5047. var indices = attributes.index.array;
  5048. var offsets = ( this.offsets.length > 0 ? this.offsets : [ { start: 0, count: indices.length, index: 0 } ] );
  5049. for ( var j = 0, jl = offsets.length; j < jl; ++ j ) {
  5050. var start = offsets[ j ].start;
  5051. var count = offsets[ j ].count;
  5052. var index = offsets[ j ].index;
  5053. for ( var i = start, il = start + count; i < il; i += 3 ) {
  5054. vA = ( index + indices[ i ] ) * 3;
  5055. vB = ( index + indices[ i + 1 ] ) * 3;
  5056. vC = ( index + indices[ i + 2 ] ) * 3;
  5057. pA.fromArray( positions, vA );
  5058. pB.fromArray( positions, vB );
  5059. pC.fromArray( positions, vC );
  5060. cb.subVectors( pC, pB );
  5061. ab.subVectors( pA, pB );
  5062. cb.cross( ab );
  5063. normals[ vA ] += cb.x;
  5064. normals[ vA + 1 ] += cb.y;
  5065. normals[ vA + 2 ] += cb.z;
  5066. normals[ vB ] += cb.x;
  5067. normals[ vB + 1 ] += cb.y;
  5068. normals[ vB + 2 ] += cb.z;
  5069. normals[ vC ] += cb.x;
  5070. normals[ vC + 1 ] += cb.y;
  5071. normals[ vC + 2 ] += cb.z;
  5072. }
  5073. }
  5074. } else {
  5075. // non-indexed elements (unconnected triangle soup)
  5076. for ( var i = 0, il = positions.length; i < il; i += 9 ) {
  5077. pA.fromArray( positions, i );
  5078. pB.fromArray( positions, i + 3 );
  5079. pC.fromArray( positions, i + 6 );
  5080. cb.subVectors( pC, pB );
  5081. ab.subVectors( pA, pB );
  5082. cb.cross( ab );
  5083. normals[ i ] = cb.x;
  5084. normals[ i + 1 ] = cb.y;
  5085. normals[ i + 2 ] = cb.z;
  5086. normals[ i + 3 ] = cb.x;
  5087. normals[ i + 4 ] = cb.y;
  5088. normals[ i + 5 ] = cb.z;
  5089. normals[ i + 6 ] = cb.x;
  5090. normals[ i + 7 ] = cb.y;
  5091. normals[ i + 8 ] = cb.z;
  5092. }
  5093. }
  5094. this.normalizeNormals();
  5095. attributes.normal.needsUpdate = true;
  5096. }
  5097. },
  5098. computeTangents: function () {
  5099. // based on http://www.terathon.com/code/tangent.html
  5100. // (per vertex tangents)
  5101. if ( this.attributes.index === undefined ||
  5102. this.attributes.position === undefined ||
  5103. this.attributes.normal === undefined ||
  5104. this.attributes.uv === undefined ) {
  5105. console.warn( 'Missing required attributes (index, position, normal or uv) in BufferGeometry.computeTangents()' );
  5106. return;
  5107. }
  5108. var indices = this.attributes.index.array;
  5109. var positions = this.attributes.position.array;
  5110. var normals = this.attributes.normal.array;
  5111. var uvs = this.attributes.uv.array;
  5112. var nVertices = positions.length / 3;
  5113. if ( this.attributes.tangent === undefined ) {
  5114. this.addAttribute( 'tangent', new THREE.BufferAttribute( new Float32Array( 4 * nVertices ), 4 ) );
  5115. }
  5116. var tangents = this.attributes.tangent.array;
  5117. var tan1 = [], tan2 = [];
  5118. for ( var k = 0; k < nVertices; k ++ ) {
  5119. tan1[ k ] = new THREE.Vector3();
  5120. tan2[ k ] = new THREE.Vector3();
  5121. }
  5122. var vA = new THREE.Vector3(),
  5123. vB = new THREE.Vector3(),
  5124. vC = new THREE.Vector3(),
  5125. uvA = new THREE.Vector2(),
  5126. uvB = new THREE.Vector2(),
  5127. uvC = new THREE.Vector2(),
  5128. x1, x2, y1, y2, z1, z2,
  5129. s1, s2, t1, t2, r;
  5130. var sdir = new THREE.Vector3(), tdir = new THREE.Vector3();
  5131. function handleTriangle( a, b, c ) {
  5132. vA.fromArray( positions, a * 3 );
  5133. vB.fromArray( positions, b * 3 );
  5134. vC.fromArray( positions, c * 3 );
  5135. uvA.fromArray( uvs, a * 2 );
  5136. uvB.fromArray( uvs, b * 2 );
  5137. uvC.fromArray( uvs, c * 2 );
  5138. x1 = vB.x - vA.x;
  5139. x2 = vC.x - vA.x;
  5140. y1 = vB.y - vA.y;
  5141. y2 = vC.y - vA.y;
  5142. z1 = vB.z - vA.z;
  5143. z2 = vC.z - vA.z;
  5144. s1 = uvB.x - uvA.x;
  5145. s2 = uvC.x - uvA.x;
  5146. t1 = uvB.y - uvA.y;
  5147. t2 = uvC.y - uvA.y;
  5148. r = 1.0 / ( s1 * t2 - s2 * t1 );
  5149. sdir.set(
  5150. ( t2 * x1 - t1 * x2 ) * r,
  5151. ( t2 * y1 - t1 * y2 ) * r,
  5152. ( t2 * z1 - t1 * z2 ) * r
  5153. );
  5154. tdir.set(
  5155. ( s1 * x2 - s2 * x1 ) * r,
  5156. ( s1 * y2 - s2 * y1 ) * r,
  5157. ( s1 * z2 - s2 * z1 ) * r
  5158. );
  5159. tan1[ a ].add( sdir );
  5160. tan1[ b ].add( sdir );
  5161. tan1[ c ].add( sdir );
  5162. tan2[ a ].add( tdir );
  5163. tan2[ b ].add( tdir );
  5164. tan2[ c ].add( tdir );
  5165. }
  5166. var i, il;
  5167. var j, jl;
  5168. var iA, iB, iC;
  5169. if ( this.drawcalls.length === 0 ) {
  5170. this.addDrawCall( 0, indices.length, 0 );
  5171. }
  5172. var drawcalls = this.drawcalls;
  5173. for ( j = 0, jl = drawcalls.length; j < jl; ++ j ) {
  5174. var start = drawcalls[ j ].start;
  5175. var count = drawcalls[ j ].count;
  5176. var index = drawcalls[ j ].index;
  5177. for ( i = start, il = start + count; i < il; i += 3 ) {
  5178. iA = index + indices[ i ];
  5179. iB = index + indices[ i + 1 ];
  5180. iC = index + indices[ i + 2 ];
  5181. handleTriangle( iA, iB, iC );
  5182. }
  5183. }
  5184. var tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3();
  5185. var n = new THREE.Vector3(), n2 = new THREE.Vector3();
  5186. var w, t, test;
  5187. function handleVertex( v ) {
  5188. n.fromArray( normals, v * 3 );
  5189. n2.copy( n );
  5190. t = tan1[ v ];
  5191. // Gram-Schmidt orthogonalize
  5192. tmp.copy( t );
  5193. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  5194. // Calculate handedness
  5195. tmp2.crossVectors( n2, t );
  5196. test = tmp2.dot( tan2[ v ] );
  5197. w = ( test < 0.0 ) ? - 1.0 : 1.0;
  5198. tangents[ v * 4 ] = tmp.x;
  5199. tangents[ v * 4 + 1 ] = tmp.y;
  5200. tangents[ v * 4 + 2 ] = tmp.z;
  5201. tangents[ v * 4 + 3 ] = w;
  5202. }
  5203. for ( j = 0, jl = drawcalls.length; j < jl; ++ j ) {
  5204. var start = drawcalls[ j ].start;
  5205. var count = drawcalls[ j ].count;
  5206. var index = drawcalls[ j ].index;
  5207. for ( i = start, il = start + count; i < il; i += 3 ) {
  5208. iA = index + indices[ i ];
  5209. iB = index + indices[ i + 1 ];
  5210. iC = index + indices[ i + 2 ];
  5211. handleVertex( iA );
  5212. handleVertex( iB );
  5213. handleVertex( iC );
  5214. }
  5215. }
  5216. },
  5217. /*
  5218. computeOffsets
  5219. Compute the draw offset for large models by chunking the index buffer into chunks of 65k addressable vertices.
  5220. This method will effectively rewrite the index buffer and remap all attributes to match the new indices.
  5221. WARNING: This method will also expand the vertex count to prevent sprawled triangles across draw offsets.
  5222. indexBufferSize - Defaults to 65535, but allows for larger or smaller chunks.
  5223. */
  5224. computeOffsets: function ( indexBufferSize ) {
  5225. var size = indexBufferSize;
  5226. if ( indexBufferSize === undefined )
  5227. size = 65535; //WebGL limits type of index buffer values to 16-bit.
  5228. var s = Date.now();
  5229. var indices = this.attributes.index.array;
  5230. var vertices = this.attributes.position.array;
  5231. var verticesCount = ( vertices.length / 3 );
  5232. var facesCount = ( indices.length / 3 );
  5233. /*
  5234. console.log("Computing buffers in offsets of "+size+" -> indices:"+indices.length+" vertices:"+vertices.length);
  5235. console.log("Faces to process: "+(indices.length/3));
  5236. console.log("Reordering "+verticesCount+" vertices.");
  5237. */
  5238. var sortedIndices = new Uint16Array( indices.length ); //16-bit buffers
  5239. var indexPtr = 0;
  5240. var vertexPtr = 0;
  5241. var offsets = [ { start:0, count:0, index:0 } ];
  5242. var offset = offsets[ 0 ];
  5243. var duplicatedVertices = 0;
  5244. var newVerticeMaps = 0;
  5245. var faceVertices = new Int32Array( 6 );
  5246. var vertexMap = new Int32Array( vertices.length );
  5247. var revVertexMap = new Int32Array( vertices.length );
  5248. for ( var j = 0; j < vertices.length; j ++ ) { vertexMap[ j ] = - 1; revVertexMap[ j ] = - 1; }
  5249. /*
  5250. Traverse every face and reorder vertices in the proper offsets of 65k.
  5251. We can have more than 65k entries in the index buffer per offset, but only reference 65k values.
  5252. */
  5253. for ( var findex = 0; findex < facesCount; findex ++ ) {
  5254. newVerticeMaps = 0;
  5255. for ( var vo = 0; vo < 3; vo ++ ) {
  5256. var vid = indices[ findex * 3 + vo ];
  5257. if ( vertexMap[ vid ] == - 1 ) {
  5258. //Unmapped vertice
  5259. faceVertices[ vo * 2 ] = vid;
  5260. faceVertices[ vo * 2 + 1 ] = - 1;
  5261. newVerticeMaps ++;
  5262. } else if ( vertexMap[ vid ] < offset.index ) {
  5263. //Reused vertices from previous block (duplicate)
  5264. faceVertices[ vo * 2 ] = vid;
  5265. faceVertices[ vo * 2 + 1 ] = - 1;
  5266. duplicatedVertices ++;
  5267. } else {
  5268. //Reused vertice in the current block
  5269. faceVertices[ vo * 2 ] = vid;
  5270. faceVertices[ vo * 2 + 1 ] = vertexMap[ vid ];
  5271. }
  5272. }
  5273. var faceMax = vertexPtr + newVerticeMaps;
  5274. if ( faceMax > ( offset.index + size ) ) {
  5275. var new_offset = { start:indexPtr, count:0, index:vertexPtr };
  5276. offsets.push( new_offset );
  5277. offset = new_offset;
  5278. //Re-evaluate reused vertices in light of new offset.
  5279. for ( var v = 0; v < 6; v += 2 ) {
  5280. var new_vid = faceVertices[ v + 1 ];
  5281. if ( new_vid > - 1 && new_vid < offset.index )
  5282. faceVertices[ v + 1 ] = - 1;
  5283. }
  5284. }
  5285. //Reindex the face.
  5286. for ( var v = 0; v < 6; v += 2 ) {
  5287. var vid = faceVertices[ v ];
  5288. var new_vid = faceVertices[ v + 1 ];
  5289. if ( new_vid === - 1 )
  5290. new_vid = vertexPtr ++;
  5291. vertexMap[ vid ] = new_vid;
  5292. revVertexMap[ new_vid ] = vid;
  5293. sortedIndices[ indexPtr ++ ] = new_vid - offset.index; //XXX overflows at 16bit
  5294. offset.count ++;
  5295. }
  5296. }
  5297. /* Move all attribute values to map to the new computed indices , also expand the vertice stack to match our new vertexPtr. */
  5298. this.reorderBuffers( sortedIndices, revVertexMap, vertexPtr );
  5299. this.offsets = offsets;
  5300. /*
  5301. var orderTime = Date.now();
  5302. console.log("Reorder time: "+(orderTime-s)+"ms");
  5303. console.log("Duplicated "+duplicatedVertices+" vertices.");
  5304. console.log("Compute Buffers time: "+(Date.now()-s)+"ms");
  5305. console.log("Draw offsets: "+offsets.length);
  5306. */
  5307. return offsets;
  5308. },
  5309. merge: function ( geometry, offset ) {
  5310. if ( geometry instanceof THREE.BufferGeometry === false ) {
  5311. console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry );
  5312. return;
  5313. }
  5314. if ( offset === undefined ) offset = 0;
  5315. var attributes = this.attributes;
  5316. for ( var key in attributes ) {
  5317. if ( geometry.attributes[ key ] === undefined ) continue;
  5318. var attribute1 = attributes[ key ];
  5319. var attributeArray1 = attribute1.array;
  5320. var attribute2 = geometry.attributes[ key ];
  5321. var attributeArray2 = attribute2.array;
  5322. var attributeSize = attribute2.itemSize;
  5323. for ( var i = 0, j = attributeSize * offset; i < attributeArray2.length; i ++, j ++ ) {
  5324. attributeArray1[ j ] = attributeArray2[ i ];
  5325. }
  5326. }
  5327. return this;
  5328. },
  5329. normalizeNormals: function () {
  5330. var normals = this.attributes.normal.array;
  5331. var x, y, z, n;
  5332. for ( var i = 0, il = normals.length; i < il; i += 3 ) {
  5333. x = normals[ i ];
  5334. y = normals[ i + 1 ];
  5335. z = normals[ i + 2 ];
  5336. n = 1.0 / Math.sqrt( x * x + y * y + z * z );
  5337. normals[ i ] *= n;
  5338. normals[ i + 1 ] *= n;
  5339. normals[ i + 2 ] *= n;
  5340. }
  5341. },
  5342. /*
  5343. reoderBuffers:
  5344. Reorder attributes based on a new indexBuffer and indexMap.
  5345. indexBuffer - Uint16Array of the new ordered indices.
  5346. indexMap - Int32Array where the position is the new vertex ID and the value the old vertex ID for each vertex.
  5347. vertexCount - Amount of total vertices considered in this reordering (in case you want to grow the vertice stack).
  5348. */
  5349. reorderBuffers: function ( indexBuffer, indexMap, vertexCount ) {
  5350. /* Create a copy of all attributes for reordering. */
  5351. var sortedAttributes = {};
  5352. for ( var attr in this.attributes ) {
  5353. if ( attr == 'index' )
  5354. continue;
  5355. var sourceArray = this.attributes[ attr ].array;
  5356. sortedAttributes[ attr ] = new sourceArray.constructor( this.attributes[ attr ].itemSize * vertexCount );
  5357. }
  5358. /* Move attribute positions based on the new index map */
  5359. for ( var new_vid = 0; new_vid < vertexCount; new_vid ++ ) {
  5360. var vid = indexMap[ new_vid ];
  5361. for ( var attr in this.attributes ) {
  5362. if ( attr == 'index' )
  5363. continue;
  5364. var attrArray = this.attributes[ attr ].array;
  5365. var attrSize = this.attributes[ attr ].itemSize;
  5366. var sortedAttr = sortedAttributes[ attr ];
  5367. for ( var k = 0; k < attrSize; k ++ )
  5368. sortedAttr[ new_vid * attrSize + k ] = attrArray[ vid * attrSize + k ];
  5369. }
  5370. }
  5371. /* Carry the new sorted buffers locally */
  5372. this.attributes[ 'index' ].array = indexBuffer;
  5373. for ( var attr in this.attributes ) {
  5374. if ( attr == 'index' )
  5375. continue;
  5376. this.attributes[ attr ].array = sortedAttributes[ attr ];
  5377. this.attributes[ attr ].numItems = this.attributes[ attr ].itemSize * vertexCount;
  5378. }
  5379. },
  5380. toJSON: function () {
  5381. var output = {
  5382. metadata: {
  5383. version: 4.0,
  5384. type: 'BufferGeometry',
  5385. generator: 'BufferGeometryExporter'
  5386. },
  5387. uuid: this.uuid,
  5388. type: this.type,
  5389. data: {
  5390. attributes: {}
  5391. }
  5392. };
  5393. var attributes = this.attributes;
  5394. var offsets = this.offsets;
  5395. var boundingSphere = this.boundingSphere;
  5396. for ( var key in attributes ) {
  5397. var attribute = attributes[ key ];
  5398. var array = [], typeArray = attribute.array;
  5399. for ( var i = 0, l = typeArray.length; i < l; i ++ ) {
  5400. array[ i ] = typeArray[ i ];
  5401. }
  5402. output.data.attributes[ key ] = {
  5403. itemSize: attribute.itemSize,
  5404. type: attribute.array.constructor.name,
  5405. array: array
  5406. }
  5407. }
  5408. if ( offsets.length > 0 ) {
  5409. output.data.offsets = JSON.parse( JSON.stringify( offsets ) );
  5410. }
  5411. if ( boundingSphere !== null ) {
  5412. output.data.boundingSphere = {
  5413. center: boundingSphere.center.toArray(),
  5414. radius: boundingSphere.radius
  5415. }
  5416. }
  5417. return output;
  5418. },
  5419. clone: function () {
  5420. var geometry = new THREE.BufferGeometry();
  5421. for ( var attr in this.attributes ) {
  5422. var sourceAttr = this.attributes[ attr ];
  5423. geometry.addAttribute( attr, sourceAttr.clone() );
  5424. }
  5425. for ( var i = 0, il = this.offsets.length; i < il; i ++ ) {
  5426. var offset = this.offsets[ i ];
  5427. geometry.offsets.push( {
  5428. start: offset.start,
  5429. index: offset.index,
  5430. count: offset.count
  5431. } );
  5432. }
  5433. return geometry;
  5434. },
  5435. dispose: function () {
  5436. this.dispatchEvent( { type: 'dispose' } );
  5437. }
  5438. };
  5439. THREE.EventDispatcher.prototype.apply( THREE.BufferGeometry.prototype );
  5440. // File:src/core/Geometry.js
  5441. /**
  5442. * @author mrdoob / http://mrdoob.com/
  5443. * @author kile / http://kile.stravaganza.org/
  5444. * @author alteredq / http://alteredqualia.com/
  5445. * @author mikael emtinger / http://gomo.se/
  5446. * @author zz85 / http://www.lab4games.net/zz85/blog
  5447. * @author bhouston / http://exocortex.com
  5448. */
  5449. THREE.Geometry = function () {
  5450. Object.defineProperty( this, 'id', { value: THREE.GeometryIdCount ++ } );
  5451. this.uuid = THREE.Math.generateUUID();
  5452. this.name = '';
  5453. this.type = 'Geometry';
  5454. this.vertices = [];
  5455. this.colors = []; // one-to-one vertex colors, used in Points and Line
  5456. this.faces = [];
  5457. this.faceVertexUvs = [ [] ];
  5458. this.morphTargets = [];
  5459. this.morphColors = [];
  5460. this.morphNormals = [];
  5461. this.skinWeights = [];
  5462. this.skinIndices = [];
  5463. this.lineDistances = [];
  5464. this.boundingBox = null;
  5465. this.boundingSphere = null;
  5466. this.hasTangents = false;
  5467. this.dynamic = true; // the intermediate typed arrays will be deleted when set to false
  5468. // update flags
  5469. this.verticesNeedUpdate = false;
  5470. this.elementsNeedUpdate = false;
  5471. this.uvsNeedUpdate = false;
  5472. this.normalsNeedUpdate = false;
  5473. this.tangentsNeedUpdate = false;
  5474. this.colorsNeedUpdate = false;
  5475. this.lineDistancesNeedUpdate = false;
  5476. this.groupsNeedUpdate = false;
  5477. };
  5478. THREE.Geometry.prototype = {
  5479. constructor: THREE.Geometry,
  5480. applyMatrix: function ( matrix ) {
  5481. var normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  5482. for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
  5483. var vertex = this.vertices[ i ];
  5484. vertex.applyMatrix4( matrix );
  5485. }
  5486. for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
  5487. var face = this.faces[ i ];
  5488. face.normal.applyMatrix3( normalMatrix ).normalize();
  5489. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  5490. face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize();
  5491. }
  5492. }
  5493. if ( this.boundingBox instanceof THREE.Box3 ) {
  5494. this.computeBoundingBox();
  5495. }
  5496. if ( this.boundingSphere instanceof THREE.Sphere ) {
  5497. this.computeBoundingSphere();
  5498. }
  5499. },
  5500. fromBufferGeometry: function ( geometry ) {
  5501. var scope = this;
  5502. var attributes = geometry.attributes;
  5503. var vertices = attributes.position.array;
  5504. var indices = attributes.index !== undefined ? attributes.index.array : undefined;
  5505. var normals = attributes.normal !== undefined ? attributes.normal.array : undefined;
  5506. var colors = attributes.color !== undefined ? attributes.color.array : undefined;
  5507. var uvs = attributes.uv !== undefined ? attributes.uv.array : undefined;
  5508. var tempNormals = [];
  5509. var tempUVs = [];
  5510. for ( var i = 0, j = 0; i < vertices.length; i += 3, j += 2 ) {
  5511. scope.vertices.push( new THREE.Vector3( vertices[ i ], vertices[ i + 1 ], vertices[ i + 2 ] ) );
  5512. if ( normals !== undefined ) {
  5513. tempNormals.push( new THREE.Vector3( normals[ i ], normals[ i + 1 ], normals[ i + 2 ] ) );
  5514. }
  5515. if ( colors !== undefined ) {
  5516. scope.colors.push( new THREE.Color( colors[ i ], colors[ i + 1 ], colors[ i + 2 ] ) );
  5517. }
  5518. if ( uvs !== undefined ) {
  5519. tempUVs.push( new THREE.Vector2( uvs[ j ], uvs[ j + 1 ] ) );
  5520. }
  5521. }
  5522. var addFace = function ( a, b, c ) {
  5523. var vertexNormals = normals !== undefined ? [ tempNormals[ a ].clone(), tempNormals[ b ].clone(), tempNormals[ c ].clone() ] : [];
  5524. var vertexColors = colors !== undefined ? [ scope.colors[ a ].clone(), scope.colors[ b ].clone(), scope.colors[ c ].clone() ] : [];
  5525. scope.faces.push( new THREE.Face3( a, b, c, vertexNormals, vertexColors ) );
  5526. if ( uvs !== undefined ) {
  5527. scope.faceVertexUvs[ 0 ].push( [ tempUVs[ a ].clone(), tempUVs[ b ].clone(), tempUVs[ c ].clone() ] );
  5528. }
  5529. };
  5530. if ( indices !== undefined ) {
  5531. for ( var i = 0; i < indices.length; i += 3 ) {
  5532. addFace( indices[ i ], indices[ i + 1 ], indices[ i + 2 ] );
  5533. }
  5534. } else {
  5535. for ( var i = 0; i < vertices.length / 3; i += 3 ) {
  5536. addFace( i, i + 1, i + 2 );
  5537. }
  5538. }
  5539. this.computeFaceNormals();
  5540. if ( geometry.boundingBox !== null ) {
  5541. this.boundingBox = geometry.boundingBox.clone();
  5542. }
  5543. if ( geometry.boundingSphere !== null ) {
  5544. this.boundingSphere = geometry.boundingSphere.clone();
  5545. }
  5546. return this;
  5547. },
  5548. center: function () {
  5549. this.computeBoundingBox();
  5550. var offset = new THREE.Vector3();
  5551. offset.addVectors( this.boundingBox.min, this.boundingBox.max );
  5552. offset.multiplyScalar( - 0.5 );
  5553. this.applyMatrix( new THREE.Matrix4().makeTranslation( offset.x, offset.y, offset.z ) );
  5554. this.computeBoundingBox();
  5555. return offset;
  5556. },
  5557. computeFaceNormals: function () {
  5558. var cb = new THREE.Vector3(), ab = new THREE.Vector3();
  5559. for ( var f = 0, fl = this.faces.length; f < fl; f ++ ) {
  5560. var face = this.faces[ f ];
  5561. var vA = this.vertices[ face.a ];
  5562. var vB = this.vertices[ face.b ];
  5563. var vC = this.vertices[ face.c ];
  5564. cb.subVectors( vC, vB );
  5565. ab.subVectors( vA, vB );
  5566. cb.cross( ab );
  5567. cb.normalize();
  5568. face.normal.copy( cb );
  5569. }
  5570. },
  5571. computeVertexNormals: function ( areaWeighted ) {
  5572. var v, vl, f, fl, face, vertices;
  5573. vertices = new Array( this.vertices.length );
  5574. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  5575. vertices[ v ] = new THREE.Vector3();
  5576. }
  5577. if ( areaWeighted ) {
  5578. // vertex normals weighted by triangle areas
  5579. // http://www.iquilezles.org/www/articles/normals/normals.htm
  5580. var vA, vB, vC, vD;
  5581. var cb = new THREE.Vector3(), ab = new THREE.Vector3(),
  5582. db = new THREE.Vector3(), dc = new THREE.Vector3(), bc = new THREE.Vector3();
  5583. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  5584. face = this.faces[ f ];
  5585. vA = this.vertices[ face.a ];
  5586. vB = this.vertices[ face.b ];
  5587. vC = this.vertices[ face.c ];
  5588. cb.subVectors( vC, vB );
  5589. ab.subVectors( vA, vB );
  5590. cb.cross( ab );
  5591. vertices[ face.a ].add( cb );
  5592. vertices[ face.b ].add( cb );
  5593. vertices[ face.c ].add( cb );
  5594. }
  5595. } else {
  5596. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  5597. face = this.faces[ f ];
  5598. vertices[ face.a ].add( face.normal );
  5599. vertices[ face.b ].add( face.normal );
  5600. vertices[ face.c ].add( face.normal );
  5601. }
  5602. }
  5603. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  5604. vertices[ v ].normalize();
  5605. }
  5606. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  5607. face = this.faces[ f ];
  5608. face.vertexNormals[ 0 ] = vertices[ face.a ].clone();
  5609. face.vertexNormals[ 1 ] = vertices[ face.b ].clone();
  5610. face.vertexNormals[ 2 ] = vertices[ face.c ].clone();
  5611. }
  5612. },
  5613. computeMorphNormals: function () {
  5614. var i, il, f, fl, face;
  5615. // save original normals
  5616. // - create temp variables on first access
  5617. // otherwise just copy (for faster repeated calls)
  5618. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  5619. face = this.faces[ f ];
  5620. if ( ! face.__originalFaceNormal ) {
  5621. face.__originalFaceNormal = face.normal.clone();
  5622. } else {
  5623. face.__originalFaceNormal.copy( face.normal );
  5624. }
  5625. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  5626. for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  5627. if ( ! face.__originalVertexNormals[ i ] ) {
  5628. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  5629. } else {
  5630. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  5631. }
  5632. }
  5633. }
  5634. // use temp geometry to compute face and vertex normals for each morph
  5635. var tmpGeo = new THREE.Geometry();
  5636. tmpGeo.faces = this.faces;
  5637. for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  5638. // create on first access
  5639. if ( ! this.morphNormals[ i ] ) {
  5640. this.morphNormals[ i ] = {};
  5641. this.morphNormals[ i ].faceNormals = [];
  5642. this.morphNormals[ i ].vertexNormals = [];
  5643. var dstNormalsFace = this.morphNormals[ i ].faceNormals;
  5644. var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  5645. var faceNormal, vertexNormals;
  5646. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  5647. faceNormal = new THREE.Vector3();
  5648. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() };
  5649. dstNormalsFace.push( faceNormal );
  5650. dstNormalsVertex.push( vertexNormals );
  5651. }
  5652. }
  5653. var morphNormals = this.morphNormals[ i ];
  5654. // set vertices to morph target
  5655. tmpGeo.vertices = this.morphTargets[ i ].vertices;
  5656. // compute morph normals
  5657. tmpGeo.computeFaceNormals();
  5658. tmpGeo.computeVertexNormals();
  5659. // store morph normals
  5660. var faceNormal, vertexNormals;
  5661. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  5662. face = this.faces[ f ];
  5663. faceNormal = morphNormals.faceNormals[ f ];
  5664. vertexNormals = morphNormals.vertexNormals[ f ];
  5665. faceNormal.copy( face.normal );
  5666. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  5667. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  5668. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  5669. }
  5670. }
  5671. // restore original normals
  5672. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  5673. face = this.faces[ f ];
  5674. face.normal = face.__originalFaceNormal;
  5675. face.vertexNormals = face.__originalVertexNormals;
  5676. }
  5677. },
  5678. computeTangents: function () {
  5679. // based on http://www.terathon.com/code/tangent.html
  5680. // tangents go to vertices
  5681. var f, fl, v, vl, i, il, vertexIndex,
  5682. face, uv, vA, vB, vC, uvA, uvB, uvC,
  5683. x1, x2, y1, y2, z1, z2,
  5684. s1, s2, t1, t2, r, t, test,
  5685. tan1 = [], tan2 = [],
  5686. sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
  5687. tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
  5688. n = new THREE.Vector3(), w;
  5689. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  5690. tan1[ v ] = new THREE.Vector3();
  5691. tan2[ v ] = new THREE.Vector3();
  5692. }
  5693. function handleTriangle( context, a, b, c, ua, ub, uc ) {
  5694. vA = context.vertices[ a ];
  5695. vB = context.vertices[ b ];
  5696. vC = context.vertices[ c ];
  5697. uvA = uv[ ua ];
  5698. uvB = uv[ ub ];
  5699. uvC = uv[ uc ];
  5700. x1 = vB.x - vA.x;
  5701. x2 = vC.x - vA.x;
  5702. y1 = vB.y - vA.y;
  5703. y2 = vC.y - vA.y;
  5704. z1 = vB.z - vA.z;
  5705. z2 = vC.z - vA.z;
  5706. s1 = uvB.x - uvA.x;
  5707. s2 = uvC.x - uvA.x;
  5708. t1 = uvB.y - uvA.y;
  5709. t2 = uvC.y - uvA.y;
  5710. r = 1.0 / ( s1 * t2 - s2 * t1 );
  5711. sdir.set( ( t2 * x1 - t1 * x2 ) * r,
  5712. ( t2 * y1 - t1 * y2 ) * r,
  5713. ( t2 * z1 - t1 * z2 ) * r );
  5714. tdir.set( ( s1 * x2 - s2 * x1 ) * r,
  5715. ( s1 * y2 - s2 * y1 ) * r,
  5716. ( s1 * z2 - s2 * z1 ) * r );
  5717. tan1[ a ].add( sdir );
  5718. tan1[ b ].add( sdir );
  5719. tan1[ c ].add( sdir );
  5720. tan2[ a ].add( tdir );
  5721. tan2[ b ].add( tdir );
  5722. tan2[ c ].add( tdir );
  5723. }
  5724. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  5725. face = this.faces[ f ];
  5726. uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents
  5727. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  5728. }
  5729. var faceIndex = [ 'a', 'b', 'c', 'd' ];
  5730. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  5731. face = this.faces[ f ];
  5732. for ( i = 0; i < Math.min( face.vertexNormals.length, 3 ); i ++ ) {
  5733. n.copy( face.vertexNormals[ i ] );
  5734. vertexIndex = face[ faceIndex[ i ] ];
  5735. t = tan1[ vertexIndex ];
  5736. // Gram-Schmidt orthogonalize
  5737. tmp.copy( t );
  5738. tmp.sub( n.multiplyScalar( n.dot( t ) ) ).normalize();
  5739. // Calculate handedness
  5740. tmp2.crossVectors( face.vertexNormals[ i ], t );
  5741. test = tmp2.dot( tan2[ vertexIndex ] );
  5742. w = ( test < 0.0 ) ? - 1.0 : 1.0;
  5743. face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w );
  5744. }
  5745. }
  5746. this.hasTangents = true;
  5747. },
  5748. computeLineDistances: function () {
  5749. var d = 0;
  5750. var vertices = this.vertices;
  5751. for ( var i = 0, il = vertices.length; i < il; i ++ ) {
  5752. if ( i > 0 ) {
  5753. d += vertices[ i ].distanceTo( vertices[ i - 1 ] );
  5754. }
  5755. this.lineDistances[ i ] = d;
  5756. }
  5757. },
  5758. computeBoundingBox: function () {
  5759. if ( this.boundingBox === null ) {
  5760. this.boundingBox = new THREE.Box3();
  5761. }
  5762. this.boundingBox.setFromPoints( this.vertices );
  5763. },
  5764. computeBoundingSphere: function () {
  5765. if ( this.boundingSphere === null ) {
  5766. this.boundingSphere = new THREE.Sphere();
  5767. }
  5768. this.boundingSphere.setFromPoints( this.vertices );
  5769. },
  5770. merge: function ( geometry, matrix, materialIndexOffset ) {
  5771. if ( geometry instanceof THREE.Geometry === false ) {
  5772. console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry );
  5773. return;
  5774. }
  5775. var normalMatrix,
  5776. vertexOffset = this.vertices.length,
  5777. vertices1 = this.vertices,
  5778. vertices2 = geometry.vertices,
  5779. faces1 = this.faces,
  5780. faces2 = geometry.faces,
  5781. uvs1 = this.faceVertexUvs[ 0 ],
  5782. uvs2 = geometry.faceVertexUvs[ 0 ];
  5783. if ( materialIndexOffset === undefined ) materialIndexOffset = 0;
  5784. if ( matrix !== undefined ) {
  5785. normalMatrix = new THREE.Matrix3().getNormalMatrix( matrix );
  5786. }
  5787. // vertices
  5788. for ( var i = 0, il = vertices2.length; i < il; i ++ ) {
  5789. var vertex = vertices2[ i ];
  5790. var vertexCopy = vertex.clone();
  5791. if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix );
  5792. vertices1.push( vertexCopy );
  5793. }
  5794. // faces
  5795. for ( i = 0, il = faces2.length; i < il; i ++ ) {
  5796. var face = faces2[ i ], faceCopy, normal, color,
  5797. faceVertexNormals = face.vertexNormals,
  5798. faceVertexColors = face.vertexColors;
  5799. faceCopy = new THREE.Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset );
  5800. faceCopy.normal.copy( face.normal );
  5801. if ( normalMatrix !== undefined ) {
  5802. faceCopy.normal.applyMatrix3( normalMatrix ).normalize();
  5803. }
  5804. for ( var j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) {
  5805. normal = faceVertexNormals[ j ].clone();
  5806. if ( normalMatrix !== undefined ) {
  5807. normal.applyMatrix3( normalMatrix ).normalize();
  5808. }
  5809. faceCopy.vertexNormals.push( normal );
  5810. }
  5811. faceCopy.color.copy( face.color );
  5812. for ( var j = 0, jl = faceVertexColors.length; j < jl; j ++ ) {
  5813. color = faceVertexColors[ j ];
  5814. faceCopy.vertexColors.push( color.clone() );
  5815. }
  5816. faceCopy.materialIndex = face.materialIndex + materialIndexOffset;
  5817. faces1.push( faceCopy );
  5818. }
  5819. // uvs
  5820. for ( i = 0, il = uvs2.length; i < il; i ++ ) {
  5821. var uv = uvs2[ i ], uvCopy = [];
  5822. if ( uv === undefined ) {
  5823. continue;
  5824. }
  5825. for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
  5826. uvCopy.push( uv[ j ].clone() );
  5827. }
  5828. uvs1.push( uvCopy );
  5829. }
  5830. },
  5831. mergeMesh: function ( mesh ) {
  5832. if ( mesh instanceof THREE.Mesh === false ) {
  5833. console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh );
  5834. return;
  5835. }
  5836. mesh.matrixAutoUpdate && mesh.updateMatrix();
  5837. this.merge( mesh.geometry, mesh.matrix );
  5838. },
  5839. /*
  5840. * Checks for duplicate vertices with hashmap.
  5841. * Duplicated vertices are removed
  5842. * and faces' vertices are updated.
  5843. */
  5844. mergeVertices: function () {
  5845. var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
  5846. var unique = [], changes = [];
  5847. var v, key;
  5848. var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
  5849. var precision = Math.pow( 10, precisionPoints );
  5850. var i,il, face;
  5851. var indices, k, j, jl, u;
  5852. for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
  5853. v = this.vertices[ i ];
  5854. key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision );
  5855. if ( verticesMap[ key ] === undefined ) {
  5856. verticesMap[ key ] = i;
  5857. unique.push( this.vertices[ i ] );
  5858. changes[ i ] = unique.length - 1;
  5859. } else {
  5860. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  5861. changes[ i ] = changes[ verticesMap[ key ] ];
  5862. }
  5863. };
  5864. // if faces are completely degenerate after merging vertices, we
  5865. // have to remove them from the geometry.
  5866. var faceIndicesToRemove = [];
  5867. for ( i = 0, il = this.faces.length; i < il; i ++ ) {
  5868. face = this.faces[ i ];
  5869. face.a = changes[ face.a ];
  5870. face.b = changes[ face.b ];
  5871. face.c = changes[ face.c ];
  5872. indices = [ face.a, face.b, face.c ];
  5873. var dupIndex = - 1;
  5874. // if any duplicate vertices are found in a Face3
  5875. // we have to remove the face as nothing can be saved
  5876. for ( var n = 0; n < 3; n ++ ) {
  5877. if ( indices[ n ] == indices[ ( n + 1 ) % 3 ] ) {
  5878. dupIndex = n;
  5879. faceIndicesToRemove.push( i );
  5880. break;
  5881. }
  5882. }
  5883. }
  5884. for ( i = faceIndicesToRemove.length - 1; i >= 0; i -- ) {
  5885. var idx = faceIndicesToRemove[ i ];
  5886. this.faces.splice( idx, 1 );
  5887. for ( j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) {
  5888. this.faceVertexUvs[ j ].splice( idx, 1 );
  5889. }
  5890. }
  5891. // Use unique set of vertices
  5892. var diff = this.vertices.length - unique.length;
  5893. this.vertices = unique;
  5894. return diff;
  5895. },
  5896. toJSON: function () {
  5897. var output = {
  5898. metadata: {
  5899. version: 4.0,
  5900. type: 'BufferGeometry',
  5901. generator: 'BufferGeometryExporter'
  5902. },
  5903. uuid: this.uuid,
  5904. type: this.type
  5905. };
  5906. if ( this.name !== "" ) output.name = this.name;
  5907. if ( this.parameters !== undefined ) {
  5908. var parameters = this.parameters;
  5909. for ( var key in parameters ) {
  5910. if ( parameters[ key ] !== undefined ) output[ key ] = parameters[ key ];
  5911. }
  5912. return output;
  5913. }
  5914. var vertices = [];
  5915. for ( var i = 0; i < this.vertices.length; i ++ ) {
  5916. var vertex = this.vertices[ i ];
  5917. vertices.push( vertex.x, vertex.y, vertex.z );
  5918. }
  5919. var faces = [];
  5920. var normals = [];
  5921. var normalsHash = {};
  5922. var colors = [];
  5923. var colorsHash = {};
  5924. var uvs = [];
  5925. var uvsHash = {};
  5926. for ( var i = 0; i < this.faces.length; i ++ ) {
  5927. var face = this.faces[ i ];
  5928. var hasMaterial = false; // face.materialIndex !== undefined;
  5929. var hasFaceUv = false; // deprecated
  5930. var hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined;
  5931. var hasFaceNormal = face.normal.length() > 0;
  5932. var hasFaceVertexNormal = face.vertexNormals.length > 0;
  5933. var hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1;
  5934. var hasFaceVertexColor = face.vertexColors.length > 0;
  5935. var faceType = 0;
  5936. faceType = setBit( faceType, 0, 0 );
  5937. faceType = setBit( faceType, 1, hasMaterial );
  5938. faceType = setBit( faceType, 2, hasFaceUv );
  5939. faceType = setBit( faceType, 3, hasFaceVertexUv );
  5940. faceType = setBit( faceType, 4, hasFaceNormal );
  5941. faceType = setBit( faceType, 5, hasFaceVertexNormal );
  5942. faceType = setBit( faceType, 6, hasFaceColor );
  5943. faceType = setBit( faceType, 7, hasFaceVertexColor );
  5944. faces.push( faceType );
  5945. faces.push( face.a, face.b, face.c );
  5946. /*
  5947. if ( hasMaterial ) {
  5948. faces.push( face.materialIndex );
  5949. }
  5950. */
  5951. if ( hasFaceVertexUv ) {
  5952. var faceVertexUvs = this.faceVertexUvs[ 0 ][ i ];
  5953. faces.push(
  5954. getUvIndex( faceVertexUvs[ 0 ] ),
  5955. getUvIndex( faceVertexUvs[ 1 ] ),
  5956. getUvIndex( faceVertexUvs[ 2 ] )
  5957. );
  5958. }
  5959. if ( hasFaceNormal ) {
  5960. faces.push( getNormalIndex( face.normal ) );
  5961. }
  5962. if ( hasFaceVertexNormal ) {
  5963. var vertexNormals = face.vertexNormals;
  5964. faces.push(
  5965. getNormalIndex( vertexNormals[ 0 ] ),
  5966. getNormalIndex( vertexNormals[ 1 ] ),
  5967. getNormalIndex( vertexNormals[ 2 ] )
  5968. );
  5969. }
  5970. if ( hasFaceColor ) {
  5971. faces.push( getColorIndex( face.color ) );
  5972. }
  5973. if ( hasFaceVertexColor ) {
  5974. var vertexColors = face.vertexColors;
  5975. faces.push(
  5976. getColorIndex( vertexColors[ 0 ] ),
  5977. getColorIndex( vertexColors[ 1 ] ),
  5978. getColorIndex( vertexColors[ 2 ] )
  5979. );
  5980. }
  5981. }
  5982. function setBit( value, position, enabled ) {
  5983. return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position) );
  5984. }
  5985. function getNormalIndex( normal ) {
  5986. var hash = normal.x.toString() + normal.y.toString() + normal.z.toString();
  5987. if ( normalsHash[ hash ] !== undefined ) {
  5988. return normalsHash[ hash ];
  5989. }
  5990. normalsHash[ hash ] = normals.length / 3;
  5991. normals.push( normal.x, normal.y, normal.z );
  5992. return normalsHash[ hash ];
  5993. }
  5994. function getColorIndex( color ) {
  5995. var hash = color.r.toString() + color.g.toString() + color.b.toString();
  5996. if ( colorsHash[ hash ] !== undefined ) {
  5997. return colorsHash[ hash ];
  5998. }
  5999. colorsHash[ hash ] = colors.length;
  6000. colors.push( color.getHex() );
  6001. return colorsHash[ hash ];
  6002. }
  6003. function getUvIndex( uv ) {
  6004. var hash = uv.x.toString() + uv.y.toString();
  6005. if ( uvsHash[ hash ] !== undefined ) {
  6006. return uvsHash[ hash ];
  6007. }
  6008. uvsHash[ hash ] = uvs.length / 2;
  6009. uvs.push( uv.x, uv.y );
  6010. return uvsHash[ hash ];
  6011. }
  6012. output.data = {};
  6013. output.data.vertices = vertices;
  6014. output.data.normals = normals;
  6015. if ( colors.length > 0 ) output.data.colors = colors;
  6016. if ( uvs.length > 0 ) output.data.uvs = [ uvs ]; // temporal backward compatibility
  6017. output.data.faces = faces;
  6018. //
  6019. return output;
  6020. },
  6021. clone: function () {
  6022. var geometry = new THREE.Geometry();
  6023. var vertices = this.vertices;
  6024. for ( var i = 0, il = vertices.length; i < il; i ++ ) {
  6025. geometry.vertices.push( vertices[ i ].clone() );
  6026. }
  6027. var faces = this.faces;
  6028. for ( var i = 0, il = faces.length; i < il; i ++ ) {
  6029. geometry.faces.push( faces[ i ].clone() );
  6030. }
  6031. var uvs = this.faceVertexUvs[ 0 ];
  6032. for ( var i = 0, il = uvs.length; i < il; i ++ ) {
  6033. var uv = uvs[ i ], uvCopy = [];
  6034. for ( var j = 0, jl = uv.length; j < jl; j ++ ) {
  6035. uvCopy.push( new THREE.Vector2( uv[ j ].x, uv[ j ].y ) );
  6036. }
  6037. geometry.faceVertexUvs[ 0 ].push( uvCopy );
  6038. }
  6039. return geometry;
  6040. },
  6041. dispose: function () {
  6042. this.dispatchEvent( { type: 'dispose' } );
  6043. }
  6044. };
  6045. THREE.EventDispatcher.prototype.apply( THREE.Geometry.prototype );
  6046. THREE.GeometryIdCount = 0;
  6047. // File:src/cameras/Camera.js
  6048. /**
  6049. * @author mrdoob / http://mrdoob.com/
  6050. * @author mikael emtinger / http://gomo.se/
  6051. * @author WestLangley / http://github.com/WestLangley
  6052. */
  6053. THREE.Camera = function () {
  6054. THREE.Object3D.call( this );
  6055. this.type = 'Camera';
  6056. this.matrixWorldInverse = new THREE.Matrix4();
  6057. this.projectionMatrix = new THREE.Matrix4();
  6058. };
  6059. THREE.Camera.prototype = Object.create( THREE.Object3D.prototype );
  6060. THREE.Camera.prototype.constructor = THREE.Camera;
  6061. THREE.Camera.prototype.getWorldDirection = function () {
  6062. var quaternion = new THREE.Quaternion();
  6063. return function ( optionalTarget ) {
  6064. var result = optionalTarget || new THREE.Vector3();
  6065. this.getWorldQuaternion( quaternion );
  6066. return result.set( 0, 0, - 1 ).applyQuaternion( quaternion );
  6067. }
  6068. }();
  6069. THREE.Camera.prototype.lookAt = function () {
  6070. // This routine does not support cameras with rotated and/or translated parent(s)
  6071. var m1 = new THREE.Matrix4();
  6072. return function ( vector ) {
  6073. m1.lookAt( this.position, vector, this.up );
  6074. this.quaternion.setFromRotationMatrix( m1 );
  6075. };
  6076. }();
  6077. THREE.Camera.prototype.clone = function ( camera ) {
  6078. if ( camera === undefined ) camera = new THREE.Camera();
  6079. THREE.Object3D.prototype.clone.call( this, camera );
  6080. camera.matrixWorldInverse.copy( this.matrixWorldInverse );
  6081. camera.projectionMatrix.copy( this.projectionMatrix );
  6082. return camera;
  6083. };
  6084. // File:src/cameras/CubeCamera.js
  6085. /**
  6086. * Camera for rendering cube maps
  6087. * - renders scene into axis-aligned cube
  6088. *
  6089. * @author alteredq / http://alteredqualia.com/
  6090. */
  6091. THREE.CubeCamera = function ( near, far, cubeResolution ) {
  6092. THREE.Object3D.call( this );
  6093. this.type = 'CubeCamera';
  6094. var fov = 90, aspect = 1;
  6095. var cameraPX = new THREE.PerspectiveCamera( fov, aspect, near, far );
  6096. cameraPX.up.set( 0, - 1, 0 );
  6097. cameraPX.lookAt( new THREE.Vector3( 1, 0, 0 ) );
  6098. this.add( cameraPX );
  6099. var cameraNX = new THREE.PerspectiveCamera( fov, aspect, near, far );
  6100. cameraNX.up.set( 0, - 1, 0 );
  6101. cameraNX.lookAt( new THREE.Vector3( - 1, 0, 0 ) );
  6102. this.add( cameraNX );
  6103. var cameraPY = new THREE.PerspectiveCamera( fov, aspect, near, far );
  6104. cameraPY.up.set( 0, 0, 1 );
  6105. cameraPY.lookAt( new THREE.Vector3( 0, 1, 0 ) );
  6106. this.add( cameraPY );
  6107. var cameraNY = new THREE.PerspectiveCamera( fov, aspect, near, far );
  6108. cameraNY.up.set( 0, 0, - 1 );
  6109. cameraNY.lookAt( new THREE.Vector3( 0, - 1, 0 ) );
  6110. this.add( cameraNY );
  6111. var cameraPZ = new THREE.PerspectiveCamera( fov, aspect, near, far );
  6112. cameraPZ.up.set( 0, - 1, 0 );
  6113. cameraPZ.lookAt( new THREE.Vector3( 0, 0, 1 ) );
  6114. this.add( cameraPZ );
  6115. var cameraNZ = new THREE.PerspectiveCamera( fov, aspect, near, far );
  6116. cameraNZ.up.set( 0, - 1, 0 );
  6117. cameraNZ.lookAt( new THREE.Vector3( 0, 0, - 1 ) );
  6118. this.add( cameraNZ );
  6119. this.renderTarget = new THREE.WebGLRenderTargetCube( cubeResolution, cubeResolution, { format: THREE.RGBFormat, magFilter: THREE.LinearFilter, minFilter: THREE.LinearFilter } );
  6120. this.updateCubeMap = function ( renderer, scene ) {
  6121. var renderTarget = this.renderTarget;
  6122. var generateMipmaps = renderTarget.generateMipmaps;
  6123. renderTarget.generateMipmaps = false;
  6124. renderTarget.activeCubeFace = 0;
  6125. renderer.render( scene, cameraPX, renderTarget );
  6126. renderTarget.activeCubeFace = 1;
  6127. renderer.render( scene, cameraNX, renderTarget );
  6128. renderTarget.activeCubeFace = 2;
  6129. renderer.render( scene, cameraPY, renderTarget );
  6130. renderTarget.activeCubeFace = 3;
  6131. renderer.render( scene, cameraNY, renderTarget );
  6132. renderTarget.activeCubeFace = 4;
  6133. renderer.render( scene, cameraPZ, renderTarget );
  6134. renderTarget.generateMipmaps = generateMipmaps;
  6135. renderTarget.activeCubeFace = 5;
  6136. renderer.render( scene, cameraNZ, renderTarget );
  6137. };
  6138. };
  6139. THREE.CubeCamera.prototype = Object.create( THREE.Object3D.prototype );
  6140. THREE.CubeCamera.prototype.constructor = THREE.CubeCamera;
  6141. // File:src/cameras/OrthographicCamera.js
  6142. /**
  6143. * @author alteredq / http://alteredqualia.com/
  6144. */
  6145. THREE.OrthographicCamera = function ( left, right, top, bottom, near, far ) {
  6146. THREE.Camera.call( this );
  6147. this.type = 'OrthographicCamera';
  6148. this.zoom = 1;
  6149. this.left = left;
  6150. this.right = right;
  6151. this.top = top;
  6152. this.bottom = bottom;
  6153. this.near = ( near !== undefined ) ? near : 0.1;
  6154. this.far = ( far !== undefined ) ? far : 2000;
  6155. this.updateProjectionMatrix();
  6156. };
  6157. THREE.OrthographicCamera.prototype = Object.create( THREE.Camera.prototype );
  6158. THREE.OrthographicCamera.prototype.constructor = THREE.OrthographicCamera;
  6159. THREE.OrthographicCamera.prototype.updateProjectionMatrix = function () {
  6160. var dx = ( this.right - this.left ) / ( 2 * this.zoom );
  6161. var dy = ( this.top - this.bottom ) / ( 2 * this.zoom );
  6162. var cx = ( this.right + this.left ) / 2;
  6163. var cy = ( this.top + this.bottom ) / 2;
  6164. this.projectionMatrix.makeOrthographic( cx - dx, cx + dx, cy + dy, cy - dy, this.near, this.far );
  6165. };
  6166. THREE.OrthographicCamera.prototype.clone = function () {
  6167. var camera = new THREE.OrthographicCamera();
  6168. THREE.Camera.prototype.clone.call( this, camera );
  6169. camera.zoom = this.zoom;
  6170. camera.left = this.left;
  6171. camera.right = this.right;
  6172. camera.top = this.top;
  6173. camera.bottom = this.bottom;
  6174. camera.near = this.near;
  6175. camera.far = this.far;
  6176. camera.projectionMatrix.copy( this.projectionMatrix );
  6177. return camera;
  6178. };
  6179. // File:src/cameras/PerspectiveCamera.js
  6180. /**
  6181. * @author mrdoob / http://mrdoob.com/
  6182. * @author greggman / http://games.greggman.com/
  6183. * @author zz85 / http://www.lab4games.net/zz85/blog
  6184. */
  6185. THREE.PerspectiveCamera = function ( fov, aspect, near, far ) {
  6186. THREE.Camera.call( this );
  6187. this.type = 'PerspectiveCamera';
  6188. this.zoom = 1;
  6189. this.fov = fov !== undefined ? fov : 50;
  6190. this.aspect = aspect !== undefined ? aspect : 1;
  6191. this.near = near !== undefined ? near : 0.1;
  6192. this.far = far !== undefined ? far : 2000;
  6193. this.updateProjectionMatrix();
  6194. };
  6195. THREE.PerspectiveCamera.prototype = Object.create( THREE.Camera.prototype );
  6196. THREE.PerspectiveCamera.prototype.constructor = THREE.PerspectiveCamera;
  6197. /**
  6198. * Uses Focal Length (in mm) to estimate and set FOV
  6199. * 35mm (fullframe) camera is used if frame size is not specified;
  6200. * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html
  6201. */
  6202. THREE.PerspectiveCamera.prototype.setLens = function ( focalLength, frameHeight ) {
  6203. if ( frameHeight === undefined ) frameHeight = 24;
  6204. this.fov = 2 * THREE.Math.radToDeg( Math.atan( frameHeight / ( focalLength * 2 ) ) );
  6205. this.updateProjectionMatrix();
  6206. }
  6207. /**
  6208. * Sets an offset in a larger frustum. This is useful for multi-window or
  6209. * multi-monitor/multi-machine setups.
  6210. *
  6211. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  6212. * the monitors are in grid like this
  6213. *
  6214. * +---+---+---+
  6215. * | A | B | C |
  6216. * +---+---+---+
  6217. * | D | E | F |
  6218. * +---+---+---+
  6219. *
  6220. * then for each monitor you would call it like this
  6221. *
  6222. * var w = 1920;
  6223. * var h = 1080;
  6224. * var fullWidth = w * 3;
  6225. * var fullHeight = h * 2;
  6226. *
  6227. * --A--
  6228. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  6229. * --B--
  6230. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  6231. * --C--
  6232. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  6233. * --D--
  6234. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  6235. * --E--
  6236. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  6237. * --F--
  6238. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  6239. *
  6240. * Note there is no reason monitors have to be the same size or in a grid.
  6241. */
  6242. THREE.PerspectiveCamera.prototype.setViewOffset = function ( fullWidth, fullHeight, x, y, width, height ) {
  6243. this.fullWidth = fullWidth;
  6244. this.fullHeight = fullHeight;
  6245. this.x = x;
  6246. this.y = y;
  6247. this.width = width;
  6248. this.height = height;
  6249. this.updateProjectionMatrix();
  6250. };
  6251. THREE.PerspectiveCamera.prototype.updateProjectionMatrix = function () {
  6252. var fov = THREE.Math.radToDeg( 2 * Math.atan( Math.tan( THREE.Math.degToRad( this.fov ) * 0.5 ) / this.zoom ) );
  6253. if ( this.fullWidth ) {
  6254. var aspect = this.fullWidth / this.fullHeight;
  6255. var top = Math.tan( THREE.Math.degToRad( fov * 0.5 ) ) * this.near;
  6256. var bottom = - top;
  6257. var left = aspect * bottom;
  6258. var right = aspect * top;
  6259. var width = Math.abs( right - left );
  6260. var height = Math.abs( top - bottom );
  6261. this.projectionMatrix.makeFrustum(
  6262. left + this.x * width / this.fullWidth,
  6263. left + ( this.x + this.width ) * width / this.fullWidth,
  6264. top - ( this.y + this.height ) * height / this.fullHeight,
  6265. top - this.y * height / this.fullHeight,
  6266. this.near,
  6267. this.far
  6268. );
  6269. } else {
  6270. this.projectionMatrix.makePerspective( fov, this.aspect, this.near, this.far );
  6271. }
  6272. };
  6273. THREE.PerspectiveCamera.prototype.clone = function () {
  6274. var camera = new THREE.PerspectiveCamera();
  6275. THREE.Camera.prototype.clone.call( this, camera );
  6276. camera.zoom = this.zoom;
  6277. camera.fov = this.fov;
  6278. camera.aspect = this.aspect;
  6279. camera.near = this.near;
  6280. camera.far = this.far;
  6281. camera.projectionMatrix.copy( this.projectionMatrix );
  6282. return camera;
  6283. };
  6284. // File:src/lights/Light.js
  6285. /**
  6286. * @author mrdoob / http://mrdoob.com/
  6287. * @author alteredq / http://alteredqualia.com/
  6288. */
  6289. THREE.Light = function ( color ) {
  6290. THREE.Object3D.call( this );
  6291. this.type = 'Light';
  6292. this.color = new THREE.Color( color );
  6293. };
  6294. THREE.Light.prototype = Object.create( THREE.Object3D.prototype );
  6295. THREE.Light.prototype.constructor = THREE.Light;
  6296. THREE.Light.prototype.clone = function ( light ) {
  6297. if ( light === undefined ) light = new THREE.Light();
  6298. THREE.Object3D.prototype.clone.call( this, light );
  6299. light.color.copy( this.color );
  6300. return light;
  6301. };
  6302. // File:src/lights/AmbientLight.js
  6303. /**
  6304. * @author mrdoob / http://mrdoob.com/
  6305. */
  6306. THREE.AmbientLight = function ( color ) {
  6307. THREE.Light.call( this, color );
  6308. this.type = 'AmbientLight';
  6309. };
  6310. THREE.AmbientLight.prototype = Object.create( THREE.Light.prototype );
  6311. THREE.AmbientLight.prototype.constructor = THREE.AmbientLight;
  6312. THREE.AmbientLight.prototype.clone = function () {
  6313. var light = new THREE.AmbientLight();
  6314. THREE.Light.prototype.clone.call( this, light );
  6315. return light;
  6316. };
  6317. // File:src/lights/AreaLight.js
  6318. /**
  6319. * @author MPanknin / http://www.redplant.de/
  6320. * @author alteredq / http://alteredqualia.com/
  6321. */
  6322. THREE.AreaLight = function ( color, intensity ) {
  6323. THREE.Light.call( this, color );
  6324. this.type = 'AreaLight';
  6325. this.normal = new THREE.Vector3( 0, - 1, 0 );
  6326. this.right = new THREE.Vector3( 1, 0, 0 );
  6327. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  6328. this.width = 1.0;
  6329. this.height = 1.0;
  6330. this.constantAttenuation = 1.5;
  6331. this.linearAttenuation = 0.5;
  6332. this.quadraticAttenuation = 0.1;
  6333. };
  6334. THREE.AreaLight.prototype = Object.create( THREE.Light.prototype );
  6335. THREE.AreaLight.prototype.constructor = THREE.AreaLight;
  6336. // File:src/lights/DirectionalLight.js
  6337. /**
  6338. * @author mrdoob / http://mrdoob.com/
  6339. * @author alteredq / http://alteredqualia.com/
  6340. */
  6341. THREE.DirectionalLight = function ( color, intensity ) {
  6342. THREE.Light.call( this, color );
  6343. this.type = 'DirectionalLight';
  6344. this.position.set( 0, 1, 0 );
  6345. this.target = new THREE.Object3D();
  6346. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  6347. this.castShadow = false;
  6348. this.onlyShadow = false;
  6349. //
  6350. this.shadowCameraNear = 50;
  6351. this.shadowCameraFar = 5000;
  6352. this.shadowCameraLeft = - 500;
  6353. this.shadowCameraRight = 500;
  6354. this.shadowCameraTop = 500;
  6355. this.shadowCameraBottom = - 500;
  6356. this.shadowCameraVisible = false;
  6357. this.shadowBias = 0;
  6358. this.shadowDarkness = 0.5;
  6359. this.shadowMapWidth = 512;
  6360. this.shadowMapHeight = 512;
  6361. //
  6362. this.shadowCascade = false;
  6363. this.shadowCascadeOffset = new THREE.Vector3( 0, 0, - 1000 );
  6364. this.shadowCascadeCount = 2;
  6365. this.shadowCascadeBias = [ 0, 0, 0 ];
  6366. this.shadowCascadeWidth = [ 512, 512, 512 ];
  6367. this.shadowCascadeHeight = [ 512, 512, 512 ];
  6368. this.shadowCascadeNearZ = [ - 1.000, 0.990, 0.998 ];
  6369. this.shadowCascadeFarZ = [ 0.990, 0.998, 1.000 ];
  6370. this.shadowCascadeArray = [];
  6371. //
  6372. this.shadowMap = null;
  6373. this.shadowMapSize = null;
  6374. this.shadowCamera = null;
  6375. this.shadowMatrix = null;
  6376. };
  6377. THREE.DirectionalLight.prototype = Object.create( THREE.Light.prototype );
  6378. THREE.DirectionalLight.prototype.constructor = THREE.DirectionalLight;
  6379. THREE.DirectionalLight.prototype.clone = function () {
  6380. var light = new THREE.DirectionalLight();
  6381. THREE.Light.prototype.clone.call( this, light );
  6382. light.target = this.target.clone();
  6383. light.intensity = this.intensity;
  6384. light.castShadow = this.castShadow;
  6385. light.onlyShadow = this.onlyShadow;
  6386. //
  6387. light.shadowCameraNear = this.shadowCameraNear;
  6388. light.shadowCameraFar = this.shadowCameraFar;
  6389. light.shadowCameraLeft = this.shadowCameraLeft;
  6390. light.shadowCameraRight = this.shadowCameraRight;
  6391. light.shadowCameraTop = this.shadowCameraTop;
  6392. light.shadowCameraBottom = this.shadowCameraBottom;
  6393. light.shadowCameraVisible = this.shadowCameraVisible;
  6394. light.shadowBias = this.shadowBias;
  6395. light.shadowDarkness = this.shadowDarkness;
  6396. light.shadowMapWidth = this.shadowMapWidth;
  6397. light.shadowMapHeight = this.shadowMapHeight;
  6398. //
  6399. light.shadowCascade = this.shadowCascade;
  6400. light.shadowCascadeOffset.copy( this.shadowCascadeOffset );
  6401. light.shadowCascadeCount = this.shadowCascadeCount;
  6402. light.shadowCascadeBias = this.shadowCascadeBias.slice( 0 );
  6403. light.shadowCascadeWidth = this.shadowCascadeWidth.slice( 0 );
  6404. light.shadowCascadeHeight = this.shadowCascadeHeight.slice( 0 );
  6405. light.shadowCascadeNearZ = this.shadowCascadeNearZ.slice( 0 );
  6406. light.shadowCascadeFarZ = this.shadowCascadeFarZ.slice( 0 );
  6407. return light;
  6408. };
  6409. // File:src/lights/HemisphereLight.js
  6410. /**
  6411. * @author alteredq / http://alteredqualia.com/
  6412. */
  6413. THREE.HemisphereLight = function ( skyColor, groundColor, intensity ) {
  6414. THREE.Light.call( this, skyColor );
  6415. this.type = 'HemisphereLight';
  6416. this.position.set( 0, 100, 0 );
  6417. this.groundColor = new THREE.Color( groundColor );
  6418. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  6419. };
  6420. THREE.HemisphereLight.prototype = Object.create( THREE.Light.prototype );
  6421. THREE.HemisphereLight.prototype.constructor = THREE.HemisphereLight;
  6422. THREE.HemisphereLight.prototype.clone = function () {
  6423. var light = new THREE.HemisphereLight();
  6424. THREE.Light.prototype.clone.call( this, light );
  6425. light.groundColor.copy( this.groundColor );
  6426. light.intensity = this.intensity;
  6427. return light;
  6428. };
  6429. // File:src/lights/PointLight.js
  6430. /**
  6431. * @author mrdoob / http://mrdoob.com/
  6432. */
  6433. THREE.PointLight = function ( color, intensity, distance ) {
  6434. THREE.Light.call( this, color );
  6435. this.type = 'PointLight';
  6436. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  6437. this.distance = ( distance !== undefined ) ? distance : 0;
  6438. };
  6439. THREE.PointLight.prototype = Object.create( THREE.Light.prototype );
  6440. THREE.PointLight.prototype.constructor = THREE.PointLight;
  6441. THREE.PointLight.prototype.clone = function () {
  6442. var light = new THREE.PointLight();
  6443. THREE.Light.prototype.clone.call( this, light );
  6444. light.intensity = this.intensity;
  6445. light.distance = this.distance;
  6446. return light;
  6447. };
  6448. // File:src/lights/SpotLight.js
  6449. /**
  6450. * @author alteredq / http://alteredqualia.com/
  6451. */
  6452. THREE.SpotLight = function ( color, intensity, distance, angle, exponent ) {
  6453. THREE.Light.call( this, color );
  6454. this.type = 'SpotLight';
  6455. this.position.set( 0, 1, 0 );
  6456. this.target = new THREE.Object3D();
  6457. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  6458. this.distance = ( distance !== undefined ) ? distance : 0;
  6459. this.angle = ( angle !== undefined ) ? angle : Math.PI / 3;
  6460. this.exponent = ( exponent !== undefined ) ? exponent : 10;
  6461. this.castShadow = false;
  6462. this.onlyShadow = false;
  6463. //
  6464. this.shadowCameraNear = 50;
  6465. this.shadowCameraFar = 5000;
  6466. this.shadowCameraFov = 50;
  6467. this.shadowCameraVisible = false;
  6468. this.shadowBias = 0;
  6469. this.shadowDarkness = 0.5;
  6470. this.shadowMapWidth = 512;
  6471. this.shadowMapHeight = 512;
  6472. //
  6473. this.shadowMap = null;
  6474. this.shadowMapSize = null;
  6475. this.shadowCamera = null;
  6476. this.shadowMatrix = null;
  6477. };
  6478. THREE.SpotLight.prototype = Object.create( THREE.Light.prototype );
  6479. THREE.SpotLight.prototype.constructor = THREE.SpotLight;
  6480. THREE.SpotLight.prototype.clone = function () {
  6481. var light = new THREE.SpotLight();
  6482. THREE.Light.prototype.clone.call( this, light );
  6483. light.target = this.target.clone();
  6484. light.intensity = this.intensity;
  6485. light.distance = this.distance;
  6486. light.angle = this.angle;
  6487. light.exponent = this.exponent;
  6488. light.castShadow = this.castShadow;
  6489. light.onlyShadow = this.onlyShadow;
  6490. //
  6491. light.shadowCameraNear = this.shadowCameraNear;
  6492. light.shadowCameraFar = this.shadowCameraFar;
  6493. light.shadowCameraFov = this.shadowCameraFov;
  6494. light.shadowCameraVisible = this.shadowCameraVisible;
  6495. light.shadowBias = this.shadowBias;
  6496. light.shadowDarkness = this.shadowDarkness;
  6497. light.shadowMapWidth = this.shadowMapWidth;
  6498. light.shadowMapHeight = this.shadowMapHeight;
  6499. return light;
  6500. };
  6501. // File:src/loaders/Cache.js
  6502. /**
  6503. * @author mrdoob / http://mrdoob.com/
  6504. */
  6505. THREE.Cache = function () {
  6506. this.files = {};
  6507. };
  6508. THREE.Cache.prototype = {
  6509. constructor: THREE.Cache,
  6510. add: function ( key, file ) {
  6511. // console.log( 'THREE.Cache', 'Adding key:', key );
  6512. this.files[ key ] = file;
  6513. },
  6514. get: function ( key ) {
  6515. // console.log( 'THREE.Cache', 'Checking key:', key );
  6516. return this.files[ key ];
  6517. },
  6518. remove: function ( key ) {
  6519. delete this.files[ key ];
  6520. },
  6521. clear: function () {
  6522. this.files = {}
  6523. }
  6524. };
  6525. // File:src/loaders/Loader.js
  6526. /**
  6527. * @author alteredq / http://alteredqualia.com/
  6528. */
  6529. THREE.Loader = function ( showStatus ) {
  6530. this.showStatus = showStatus;
  6531. this.statusDomElement = showStatus ? THREE.Loader.prototype.addStatusElement() : null;
  6532. this.imageLoader = new THREE.ImageLoader();
  6533. this.onLoadStart = function () {};
  6534. this.onLoadProgress = function () {};
  6535. this.onLoadComplete = function () {};
  6536. };
  6537. THREE.Loader.prototype = {
  6538. constructor: THREE.Loader,
  6539. crossOrigin: undefined,
  6540. addStatusElement: function () {
  6541. var e = document.createElement( 'div' );
  6542. e.style.position = 'absolute';
  6543. e.style.right = '0px';
  6544. e.style.top = '0px';
  6545. e.style.fontSize = '0.8em';
  6546. e.style.textAlign = 'left';
  6547. e.style.background = 'rgba(0,0,0,0.25)';
  6548. e.style.color = '#fff';
  6549. e.style.width = '120px';
  6550. e.style.padding = '0.5em 0.5em 0.5em 0.5em';
  6551. e.style.zIndex = 1000;
  6552. e.innerHTML = 'Loading ...';
  6553. return e;
  6554. },
  6555. updateProgress: function ( progress ) {
  6556. var message = 'Loaded ';
  6557. if ( progress.total ) {
  6558. message += ( 100 * progress.loaded / progress.total ).toFixed( 0 ) + '%';
  6559. } else {
  6560. message += ( progress.loaded / 1024 ).toFixed( 2 ) + ' KB';
  6561. }
  6562. this.statusDomElement.innerHTML = message;
  6563. },
  6564. extractUrlBase: function ( url ) {
  6565. var parts = url.split( '/' );
  6566. if ( parts.length === 1 ) return './';
  6567. parts.pop();
  6568. return parts.join( '/' ) + '/';
  6569. },
  6570. initMaterials: function ( materials, texturePath ) {
  6571. var array = [];
  6572. for ( var i = 0; i < materials.length; ++ i ) {
  6573. array[ i ] = this.createMaterial( materials[ i ], texturePath );
  6574. }
  6575. return array;
  6576. },
  6577. needsTangents: function ( materials ) {
  6578. for ( var i = 0, il = materials.length; i < il; i ++ ) {
  6579. var m = materials[ i ];
  6580. if ( m instanceof THREE.ShaderMaterial ) return true;
  6581. }
  6582. return false;
  6583. },
  6584. createMaterial: function ( m, texturePath ) {
  6585. var scope = this;
  6586. function nearest_pow2( n ) {
  6587. var l = Math.log( n ) / Math.LN2;
  6588. return Math.pow( 2, Math.round( l ) );
  6589. }
  6590. function create_texture( where, name, sourceFile, repeat, offset, wrap, anisotropy ) {
  6591. var fullPath = texturePath + sourceFile;
  6592. var texture;
  6593. var loader = THREE.Loader.Handlers.get( fullPath );
  6594. if ( loader !== null ) {
  6595. texture = loader.load( fullPath );
  6596. } else {
  6597. texture = new THREE.Texture();
  6598. loader = scope.imageLoader;
  6599. loader.crossOrigin = scope.crossOrigin;
  6600. loader.load( fullPath, function ( image ) {
  6601. if ( THREE.Math.isPowerOfTwo( image.width ) === false ||
  6602. THREE.Math.isPowerOfTwo( image.height ) === false ) {
  6603. var width = nearest_pow2( image.width );
  6604. var height = nearest_pow2( image.height );
  6605. var canvas = document.createElement( 'canvas' );
  6606. canvas.width = width;
  6607. canvas.height = height;
  6608. var context = canvas.getContext( '2d' );
  6609. context.drawImage( image, 0, 0, width, height );
  6610. texture.image = canvas;
  6611. } else {
  6612. texture.image = image;
  6613. }
  6614. texture.needsUpdate = true;
  6615. } );
  6616. }
  6617. texture.sourceFile = sourceFile;
  6618. if ( repeat ) {
  6619. texture.repeat.set( repeat[ 0 ], repeat[ 1 ] );
  6620. if ( repeat[ 0 ] !== 1 ) texture.wrapS = THREE.RepeatWrapping;
  6621. if ( repeat[ 1 ] !== 1 ) texture.wrapT = THREE.RepeatWrapping;
  6622. }
  6623. if ( offset ) {
  6624. texture.offset.set( offset[ 0 ], offset[ 1 ] );
  6625. }
  6626. if ( wrap ) {
  6627. var wrapMap = {
  6628. 'repeat': THREE.RepeatWrapping,
  6629. 'mirror': THREE.MirroredRepeatWrapping
  6630. }
  6631. if ( wrapMap[ wrap[ 0 ] ] !== undefined ) texture.wrapS = wrapMap[ wrap[ 0 ] ];
  6632. if ( wrapMap[ wrap[ 1 ] ] !== undefined ) texture.wrapT = wrapMap[ wrap[ 1 ] ];
  6633. }
  6634. if ( anisotropy ) {
  6635. texture.anisotropy = anisotropy;
  6636. }
  6637. where[ name ] = texture;
  6638. }
  6639. function rgb2hex( rgb ) {
  6640. return ( rgb[ 0 ] * 255 << 16 ) + ( rgb[ 1 ] * 255 << 8 ) + rgb[ 2 ] * 255;
  6641. }
  6642. // defaults
  6643. var mtype = 'MeshLambertMaterial';
  6644. var mpars = { color: 0xeeeeee, opacity: 1.0, map: null, lightMap: null, normalMap: null, bumpMap: null, wireframe: false };
  6645. // parameters from model file
  6646. if ( m.shading ) {
  6647. var shading = m.shading.toLowerCase();
  6648. if ( shading === 'phong' ) mtype = 'MeshPhongMaterial';
  6649. else if ( shading === 'basic' ) mtype = 'MeshBasicMaterial';
  6650. }
  6651. if ( m.blending !== undefined && THREE[ m.blending ] !== undefined ) {
  6652. mpars.blending = THREE[ m.blending ];
  6653. }
  6654. if ( m.transparent !== undefined || m.opacity < 1.0 ) {
  6655. mpars.transparent = m.transparent;
  6656. }
  6657. if ( m.depthTest !== undefined ) {
  6658. mpars.depthTest = m.depthTest;
  6659. }
  6660. if ( m.depthWrite !== undefined ) {
  6661. mpars.depthWrite = m.depthWrite;
  6662. }
  6663. if ( m.visible !== undefined ) {
  6664. mpars.visible = m.visible;
  6665. }
  6666. if ( m.flipSided !== undefined ) {
  6667. mpars.side = THREE.BackSide;
  6668. }
  6669. if ( m.doubleSided !== undefined ) {
  6670. mpars.side = THREE.DoubleSide;
  6671. }
  6672. if ( m.wireframe !== undefined ) {
  6673. mpars.wireframe = m.wireframe;
  6674. }
  6675. if ( m.vertexColors !== undefined ) {
  6676. if ( m.vertexColors === 'face' ) {
  6677. mpars.vertexColors = THREE.FaceColors;
  6678. } else if ( m.vertexColors ) {
  6679. mpars.vertexColors = THREE.VertexColors;
  6680. }
  6681. }
  6682. // colors
  6683. if ( m.colorDiffuse ) {
  6684. mpars.color = rgb2hex( m.colorDiffuse );
  6685. } else if ( m.DbgColor ) {
  6686. mpars.color = m.DbgColor;
  6687. }
  6688. if ( m.colorSpecular ) {
  6689. mpars.specular = rgb2hex( m.colorSpecular );
  6690. }
  6691. if ( m.colorAmbient ) {
  6692. mpars.ambient = rgb2hex( m.colorAmbient );
  6693. }
  6694. if ( m.colorEmissive ) {
  6695. mpars.emissive = rgb2hex( m.colorEmissive );
  6696. }
  6697. // modifiers
  6698. if ( m.transparency ) {
  6699. mpars.opacity = m.transparency;
  6700. }
  6701. if ( m.specularCoef ) {
  6702. mpars.shininess = m.specularCoef;
  6703. }
  6704. // textures
  6705. if ( m.mapDiffuse && texturePath ) {
  6706. create_texture( mpars, 'map', m.mapDiffuse, m.mapDiffuseRepeat, m.mapDiffuseOffset, m.mapDiffuseWrap, m.mapDiffuseAnisotropy );
  6707. }
  6708. if ( m.mapLight && texturePath ) {
  6709. create_texture( mpars, 'lightMap', m.mapLight, m.mapLightRepeat, m.mapLightOffset, m.mapLightWrap, m.mapLightAnisotropy );
  6710. }
  6711. if ( m.mapBump && texturePath ) {
  6712. create_texture( mpars, 'bumpMap', m.mapBump, m.mapBumpRepeat, m.mapBumpOffset, m.mapBumpWrap, m.mapBumpAnisotropy );
  6713. }
  6714. if ( m.mapNormal && texturePath ) {
  6715. create_texture( mpars, 'normalMap', m.mapNormal, m.mapNormalRepeat, m.mapNormalOffset, m.mapNormalWrap, m.mapNormalAnisotropy );
  6716. }
  6717. if ( m.mapSpecular && texturePath ) {
  6718. create_texture( mpars, 'specularMap', m.mapSpecular, m.mapSpecularRepeat, m.mapSpecularOffset, m.mapSpecularWrap, m.mapSpecularAnisotropy );
  6719. }
  6720. if ( m.mapAlpha && texturePath ) {
  6721. create_texture( mpars, 'alphaMap', m.mapAlpha, m.mapAlphaRepeat, m.mapAlphaOffset, m.mapAlphaWrap, m.mapAlphaAnisotropy );
  6722. }
  6723. //
  6724. if ( m.mapBumpScale ) {
  6725. mpars.bumpScale = m.mapBumpScale;
  6726. }
  6727. // special case for normal mapped material
  6728. if ( m.mapNormal ) {
  6729. var shader = THREE.ShaderLib[ 'normalmap' ];
  6730. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  6731. uniforms[ 'tNormal' ].value = mpars.normalMap;
  6732. if ( m.mapNormalFactor ) {
  6733. uniforms[ 'uNormalScale' ].value.set( m.mapNormalFactor, m.mapNormalFactor );
  6734. }
  6735. if ( mpars.map ) {
  6736. uniforms[ 'tDiffuse' ].value = mpars.map;
  6737. uniforms[ 'enableDiffuse' ].value = true;
  6738. }
  6739. if ( mpars.specularMap ) {
  6740. uniforms[ 'tSpecular' ].value = mpars.specularMap;
  6741. uniforms[ 'enableSpecular' ].value = true;
  6742. }
  6743. if ( mpars.lightMap ) {
  6744. uniforms[ 'tAO' ].value = mpars.lightMap;
  6745. uniforms[ 'enableAO' ].value = true;
  6746. }
  6747. // for the moment don't handle displacement texture
  6748. uniforms[ 'diffuse' ].value.setHex( mpars.color );
  6749. uniforms[ 'specular' ].value.setHex( mpars.specular );
  6750. uniforms[ 'ambient' ].value.setHex( mpars.ambient );
  6751. uniforms[ 'shininess' ].value = mpars.shininess;
  6752. if ( mpars.opacity !== undefined ) {
  6753. uniforms[ 'opacity' ].value = mpars.opacity;
  6754. }
  6755. var parameters = { fragmentShader: shader.fragmentShader, vertexShader: shader.vertexShader, uniforms: uniforms, lights: true, fog: true };
  6756. var material = new THREE.ShaderMaterial( parameters );
  6757. if ( mpars.transparent ) {
  6758. material.transparent = true;
  6759. }
  6760. } else {
  6761. var material = new THREE[ mtype ]( mpars );
  6762. }
  6763. if ( m.DbgName !== undefined ) material.name = m.DbgName;
  6764. return material;
  6765. }
  6766. };
  6767. THREE.Loader.Handlers = {
  6768. handlers: [],
  6769. add: function ( regex, loader ) {
  6770. this.handlers.push( regex, loader );
  6771. },
  6772. get: function ( file ) {
  6773. for ( var i = 0, l = this.handlers.length; i < l; i += 2 ) {
  6774. var regex = this.handlers[ i ];
  6775. var loader = this.handlers[ i + 1 ];
  6776. if ( regex.test( file ) ) {
  6777. return loader;
  6778. }
  6779. }
  6780. return null;
  6781. }
  6782. };
  6783. // File:src/loaders/XHRLoader.js
  6784. /**
  6785. * @author mrdoob / http://mrdoob.com/
  6786. */
  6787. THREE.XHRLoader = function ( manager ) {
  6788. this.cache = new THREE.Cache();
  6789. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6790. };
  6791. THREE.XHRLoader.prototype = {
  6792. constructor: THREE.XHRLoader,
  6793. load: function ( url, onLoad, onProgress, onError ) {
  6794. var scope = this;
  6795. var cached = scope.cache.get( url );
  6796. if ( cached !== undefined ) {
  6797. if ( onLoad ) onLoad( cached );
  6798. return;
  6799. }
  6800. var request = new XMLHttpRequest();
  6801. request.open( 'GET', url, true );
  6802. request.addEventListener( 'load', function ( event ) {
  6803. scope.cache.add( url, this.response );
  6804. if ( onLoad ) onLoad( this.response );
  6805. scope.manager.itemEnd( url );
  6806. }, false );
  6807. if ( onProgress !== undefined ) {
  6808. request.addEventListener( 'progress', function ( event ) {
  6809. onProgress( event );
  6810. }, false );
  6811. }
  6812. if ( onError !== undefined ) {
  6813. request.addEventListener( 'error', function ( event ) {
  6814. onError( event );
  6815. }, false );
  6816. }
  6817. if ( this.crossOrigin !== undefined ) request.crossOrigin = this.crossOrigin;
  6818. if ( this.responseType !== undefined ) request.responseType = this.responseType;
  6819. request.send( null );
  6820. scope.manager.itemStart( url );
  6821. },
  6822. setResponseType: function ( value ) {
  6823. this.responseType = value;
  6824. },
  6825. setCrossOrigin: function ( value ) {
  6826. this.crossOrigin = value;
  6827. }
  6828. };
  6829. // File:src/loaders/ImageLoader.js
  6830. /**
  6831. * @author mrdoob / http://mrdoob.com/
  6832. */
  6833. THREE.ImageLoader = function ( manager ) {
  6834. this.cache = new THREE.Cache();
  6835. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  6836. };
  6837. THREE.ImageLoader.prototype = {
  6838. constructor: THREE.ImageLoader,
  6839. load: function ( url, onLoad, onProgress, onError ) {
  6840. var scope = this;
  6841. var cached = scope.cache.get( url );
  6842. if ( cached !== undefined ) {
  6843. onLoad( cached );
  6844. return;
  6845. }
  6846. var image = document.createElement( 'img' );
  6847. if ( onLoad !== undefined ) {
  6848. image.addEventListener( 'load', function ( event ) {
  6849. scope.cache.add( url, this );
  6850. onLoad( this );
  6851. scope.manager.itemEnd( url );
  6852. }, false );
  6853. }
  6854. if ( onProgress !== undefined ) {
  6855. image.addEventListener( 'progress', function ( event ) {
  6856. onProgress( event );
  6857. }, false );
  6858. }
  6859. if ( onError !== undefined ) {
  6860. image.addEventListener( 'error', function ( event ) {
  6861. onError( event );
  6862. }, false );
  6863. }
  6864. if ( this.crossOrigin !== undefined ) image.crossOrigin = this.crossOrigin;
  6865. image.src = url;
  6866. scope.manager.itemStart( url );
  6867. return image;
  6868. },
  6869. setCrossOrigin: function ( value ) {
  6870. this.crossOrigin = value;
  6871. }
  6872. }
  6873. // File:src/loaders/JSONLoader.js
  6874. /**
  6875. * @author mrdoob / http://mrdoob.com/
  6876. * @author alteredq / http://alteredqualia.com/
  6877. */
  6878. THREE.JSONLoader = function ( showStatus ) {
  6879. THREE.Loader.call( this, showStatus );
  6880. this.withCredentials = false;
  6881. };
  6882. THREE.JSONLoader.prototype = Object.create( THREE.Loader.prototype );
  6883. THREE.JSONLoader.prototype.constructor = THREE.JSONLoader;
  6884. THREE.JSONLoader.prototype.load = function ( url, callback, texturePath ) {
  6885. var scope = this;
  6886. // todo: unify load API to for easier SceneLoader use
  6887. texturePath = texturePath && ( typeof texturePath === 'string' ) ? texturePath : this.extractUrlBase( url );
  6888. this.onLoadStart();
  6889. this.loadAjaxJSON( this, url, callback, texturePath );
  6890. };
  6891. THREE.JSONLoader.prototype.loadAjaxJSON = function ( context, url, callback, texturePath, callbackProgress ) {
  6892. var xhr = new XMLHttpRequest();
  6893. var length = 0;
  6894. xhr.onreadystatechange = function () {
  6895. if ( xhr.readyState === xhr.DONE ) {
  6896. if ( xhr.status === 200 || xhr.status === 0 ) {
  6897. if ( xhr.responseText ) {
  6898. var json = JSON.parse( xhr.responseText );
  6899. if ( json.metadata !== undefined && json.metadata.type === 'scene' ) {
  6900. console.error( 'THREE.JSONLoader: "' + url + '" seems to be a Scene. Use THREE.SceneLoader instead.' );
  6901. return;
  6902. }
  6903. var result = context.parse( json, texturePath );
  6904. callback( result.geometry, result.materials );
  6905. } else {
  6906. console.error( 'THREE.JSONLoader: "' + url + '" seems to be unreachable or the file is empty.' );
  6907. }
  6908. // in context of more complex asset initialization
  6909. // do not block on single failed file
  6910. // maybe should go even one more level up
  6911. context.onLoadComplete();
  6912. } else {
  6913. console.error( 'THREE.JSONLoader: Couldn\'t load "' + url + '" (' + xhr.status + ')' );
  6914. }
  6915. } else if ( xhr.readyState === xhr.LOADING ) {
  6916. if ( callbackProgress ) {
  6917. if ( length === 0 ) {
  6918. length = xhr.getResponseHeader( 'Content-Length' );
  6919. }
  6920. callbackProgress( { total: length, loaded: xhr.responseText.length } );
  6921. }
  6922. } else if ( xhr.readyState === xhr.HEADERS_RECEIVED ) {
  6923. if ( callbackProgress !== undefined ) {
  6924. length = xhr.getResponseHeader( 'Content-Length' );
  6925. }
  6926. }
  6927. };
  6928. xhr.open( 'GET', url, true );
  6929. xhr.withCredentials = this.withCredentials;
  6930. xhr.send( null );
  6931. };
  6932. THREE.JSONLoader.prototype.parse = function ( json, texturePath ) {
  6933. var scope = this,
  6934. geometry = new THREE.Geometry(),
  6935. scale = ( json.scale !== undefined ) ? 1.0 / json.scale : 1.0;
  6936. parseModel( scale );
  6937. parseSkin();
  6938. parseMorphing( scale );
  6939. geometry.computeFaceNormals();
  6940. geometry.computeBoundingSphere();
  6941. function parseModel( scale ) {
  6942. function isBitSet( value, position ) {
  6943. return value & ( 1 << position );
  6944. }
  6945. var i, j, fi,
  6946. offset, zLength,
  6947. colorIndex, normalIndex, uvIndex, materialIndex,
  6948. type,
  6949. isQuad,
  6950. hasMaterial,
  6951. hasFaceVertexUv,
  6952. hasFaceNormal, hasFaceVertexNormal,
  6953. hasFaceColor, hasFaceVertexColor,
  6954. vertex, face, faceA, faceB, color, hex, normal,
  6955. uvLayer, uv, u, v,
  6956. faces = json.faces,
  6957. vertices = json.vertices,
  6958. normals = json.normals,
  6959. colors = json.colors,
  6960. nUvLayers = 0;
  6961. if ( json.uvs !== undefined ) {
  6962. // disregard empty arrays
  6963. for ( i = 0; i < json.uvs.length; i ++ ) {
  6964. if ( json.uvs[ i ].length ) nUvLayers ++;
  6965. }
  6966. for ( i = 0; i < nUvLayers; i ++ ) {
  6967. geometry.faceVertexUvs[ i ] = [];
  6968. }
  6969. }
  6970. offset = 0;
  6971. zLength = vertices.length;
  6972. while ( offset < zLength ) {
  6973. vertex = new THREE.Vector3();
  6974. vertex.x = vertices[ offset ++ ] * scale;
  6975. vertex.y = vertices[ offset ++ ] * scale;
  6976. vertex.z = vertices[ offset ++ ] * scale;
  6977. geometry.vertices.push( vertex );
  6978. }
  6979. offset = 0;
  6980. zLength = faces.length;
  6981. while ( offset < zLength ) {
  6982. type = faces[ offset ++ ];
  6983. isQuad = isBitSet( type, 0 );
  6984. hasMaterial = isBitSet( type, 1 );
  6985. hasFaceVertexUv = isBitSet( type, 3 );
  6986. hasFaceNormal = isBitSet( type, 4 );
  6987. hasFaceVertexNormal = isBitSet( type, 5 );
  6988. hasFaceColor = isBitSet( type, 6 );
  6989. hasFaceVertexColor = isBitSet( type, 7 );
  6990. // console.log("type", type, "bits", isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);
  6991. if ( isQuad ) {
  6992. faceA = new THREE.Face3();
  6993. faceA.a = faces[ offset ];
  6994. faceA.b = faces[ offset + 1 ];
  6995. faceA.c = faces[ offset + 3 ];
  6996. faceB = new THREE.Face3();
  6997. faceB.a = faces[ offset + 1 ];
  6998. faceB.b = faces[ offset + 2 ];
  6999. faceB.c = faces[ offset + 3 ];
  7000. offset += 4;
  7001. if ( hasMaterial ) {
  7002. materialIndex = faces[ offset ++ ];
  7003. faceA.materialIndex = materialIndex;
  7004. faceB.materialIndex = materialIndex;
  7005. }
  7006. // to get face <=> uv index correspondence
  7007. fi = geometry.faces.length;
  7008. if ( hasFaceVertexUv ) {
  7009. for ( i = 0; i < nUvLayers; i ++ ) {
  7010. uvLayer = json.uvs[ i ];
  7011. geometry.faceVertexUvs[ i ][ fi ] = [];
  7012. geometry.faceVertexUvs[ i ][ fi + 1 ] = []
  7013. for ( j = 0; j < 4; j ++ ) {
  7014. uvIndex = faces[ offset ++ ];
  7015. u = uvLayer[ uvIndex * 2 ];
  7016. v = uvLayer[ uvIndex * 2 + 1 ];
  7017. uv = new THREE.Vector2( u, v );
  7018. if ( j !== 2 ) geometry.faceVertexUvs[ i ][ fi ].push( uv );
  7019. if ( j !== 0 ) geometry.faceVertexUvs[ i ][ fi + 1 ].push( uv );
  7020. }
  7021. }
  7022. }
  7023. if ( hasFaceNormal ) {
  7024. normalIndex = faces[ offset ++ ] * 3;
  7025. faceA.normal.set(
  7026. normals[ normalIndex ++ ],
  7027. normals[ normalIndex ++ ],
  7028. normals[ normalIndex ]
  7029. );
  7030. faceB.normal.copy( faceA.normal );
  7031. }
  7032. if ( hasFaceVertexNormal ) {
  7033. for ( i = 0; i < 4; i ++ ) {
  7034. normalIndex = faces[ offset ++ ] * 3;
  7035. normal = new THREE.Vector3(
  7036. normals[ normalIndex ++ ],
  7037. normals[ normalIndex ++ ],
  7038. normals[ normalIndex ]
  7039. );
  7040. if ( i !== 2 ) faceA.vertexNormals.push( normal );
  7041. if ( i !== 0 ) faceB.vertexNormals.push( normal );
  7042. }
  7043. }
  7044. if ( hasFaceColor ) {
  7045. colorIndex = faces[ offset ++ ];
  7046. hex = colors[ colorIndex ];
  7047. faceA.color.setHex( hex );
  7048. faceB.color.setHex( hex );
  7049. }
  7050. if ( hasFaceVertexColor ) {
  7051. for ( i = 0; i < 4; i ++ ) {
  7052. colorIndex = faces[ offset ++ ];
  7053. hex = colors[ colorIndex ];
  7054. if ( i !== 2 ) faceA.vertexColors.push( new THREE.Color( hex ) );
  7055. if ( i !== 0 ) faceB.vertexColors.push( new THREE.Color( hex ) );
  7056. }
  7057. }
  7058. geometry.faces.push( faceA );
  7059. geometry.faces.push( faceB );
  7060. } else {
  7061. face = new THREE.Face3();
  7062. face.a = faces[ offset ++ ];
  7063. face.b = faces[ offset ++ ];
  7064. face.c = faces[ offset ++ ];
  7065. if ( hasMaterial ) {
  7066. materialIndex = faces[ offset ++ ];
  7067. face.materialIndex = materialIndex;
  7068. }
  7069. // to get face <=> uv index correspondence
  7070. fi = geometry.faces.length;
  7071. if ( hasFaceVertexUv ) {
  7072. for ( i = 0; i < nUvLayers; i ++ ) {
  7073. uvLayer = json.uvs[ i ];
  7074. geometry.faceVertexUvs[ i ][ fi ] = [];
  7075. for ( j = 0; j < 3; j ++ ) {
  7076. uvIndex = faces[ offset ++ ];
  7077. u = uvLayer[ uvIndex * 2 ];
  7078. v = uvLayer[ uvIndex * 2 + 1 ];
  7079. uv = new THREE.Vector2( u, v );
  7080. geometry.faceVertexUvs[ i ][ fi ].push( uv );
  7081. }
  7082. }
  7083. }
  7084. if ( hasFaceNormal ) {
  7085. normalIndex = faces[ offset ++ ] * 3;
  7086. face.normal.set(
  7087. normals[ normalIndex ++ ],
  7088. normals[ normalIndex ++ ],
  7089. normals[ normalIndex ]
  7090. );
  7091. }
  7092. if ( hasFaceVertexNormal ) {
  7093. for ( i = 0; i < 3; i ++ ) {
  7094. normalIndex = faces[ offset ++ ] * 3;
  7095. normal = new THREE.Vector3(
  7096. normals[ normalIndex ++ ],
  7097. normals[ normalIndex ++ ],
  7098. normals[ normalIndex ]
  7099. );
  7100. face.vertexNormals.push( normal );
  7101. }
  7102. }
  7103. if ( hasFaceColor ) {
  7104. colorIndex = faces[ offset ++ ];
  7105. face.color.setHex( colors[ colorIndex ] );
  7106. }
  7107. if ( hasFaceVertexColor ) {
  7108. for ( i = 0; i < 3; i ++ ) {
  7109. colorIndex = faces[ offset ++ ];
  7110. face.vertexColors.push( new THREE.Color( colors[ colorIndex ] ) );
  7111. }
  7112. }
  7113. geometry.faces.push( face );
  7114. }
  7115. }
  7116. };
  7117. function parseSkin() {
  7118. var influencesPerVertex = ( json.influencesPerVertex !== undefined ) ? json.influencesPerVertex : 2;
  7119. if ( json.skinWeights ) {
  7120. for ( var i = 0, l = json.skinWeights.length; i < l; i += influencesPerVertex ) {
  7121. var x = json.skinWeights[ i ];
  7122. var y = ( influencesPerVertex > 1 ) ? json.skinWeights[ i + 1 ] : 0;
  7123. var z = ( influencesPerVertex > 2 ) ? json.skinWeights[ i + 2 ] : 0;
  7124. var w = ( influencesPerVertex > 3 ) ? json.skinWeights[ i + 3 ] : 0;
  7125. geometry.skinWeights.push( new THREE.Vector4( x, y, z, w ) );
  7126. }
  7127. }
  7128. if ( json.skinIndices ) {
  7129. for ( var i = 0, l = json.skinIndices.length; i < l; i += influencesPerVertex ) {
  7130. var a = json.skinIndices[ i ];
  7131. var b = ( influencesPerVertex > 1 ) ? json.skinIndices[ i + 1 ] : 0;
  7132. var c = ( influencesPerVertex > 2 ) ? json.skinIndices[ i + 2 ] : 0;
  7133. var d = ( influencesPerVertex > 3 ) ? json.skinIndices[ i + 3 ] : 0;
  7134. geometry.skinIndices.push( new THREE.Vector4( a, b, c, d ) );
  7135. }
  7136. }
  7137. geometry.bones = json.bones;
  7138. if ( geometry.bones && geometry.bones.length > 0 && ( geometry.skinWeights.length !== geometry.skinIndices.length || geometry.skinIndices.length !== geometry.vertices.length ) ) {
  7139. console.warn( 'When skinning, number of vertices (' + geometry.vertices.length + '), skinIndices (' +
  7140. geometry.skinIndices.length + '), and skinWeights (' + geometry.skinWeights.length + ') should match.' );
  7141. }
  7142. // could change this to json.animations[0] or remove completely
  7143. geometry.animation = json.animation;
  7144. geometry.animations = json.animations;
  7145. };
  7146. function parseMorphing( scale ) {
  7147. if ( json.morphTargets !== undefined ) {
  7148. var i, l, v, vl, dstVertices, srcVertices;
  7149. for ( i = 0, l = json.morphTargets.length; i < l; i ++ ) {
  7150. geometry.morphTargets[ i ] = {};
  7151. geometry.morphTargets[ i ].name = json.morphTargets[ i ].name;
  7152. geometry.morphTargets[ i ].vertices = [];
  7153. dstVertices = geometry.morphTargets[ i ].vertices;
  7154. srcVertices = json.morphTargets [ i ].vertices;
  7155. for ( v = 0, vl = srcVertices.length; v < vl; v += 3 ) {
  7156. var vertex = new THREE.Vector3();
  7157. vertex.x = srcVertices[ v ] * scale;
  7158. vertex.y = srcVertices[ v + 1 ] * scale;
  7159. vertex.z = srcVertices[ v + 2 ] * scale;
  7160. dstVertices.push( vertex );
  7161. }
  7162. }
  7163. }
  7164. if ( json.morphColors !== undefined ) {
  7165. var i, l, c, cl, dstColors, srcColors, color;
  7166. for ( i = 0, l = json.morphColors.length; i < l; i ++ ) {
  7167. geometry.morphColors[ i ] = {};
  7168. geometry.morphColors[ i ].name = json.morphColors[ i ].name;
  7169. geometry.morphColors[ i ].colors = [];
  7170. dstColors = geometry.morphColors[ i ].colors;
  7171. srcColors = json.morphColors [ i ].colors;
  7172. for ( c = 0, cl = srcColors.length; c < cl; c += 3 ) {
  7173. color = new THREE.Color( 0xffaa00 );
  7174. color.setRGB( srcColors[ c ], srcColors[ c + 1 ], srcColors[ c + 2 ] );
  7175. dstColors.push( color );
  7176. }
  7177. }
  7178. }
  7179. };
  7180. if ( json.materials === undefined || json.materials.length === 0 ) {
  7181. return { geometry: geometry };
  7182. } else {
  7183. var materials = this.initMaterials( json.materials, texturePath );
  7184. if ( this.needsTangents( materials ) ) {
  7185. geometry.computeTangents();
  7186. }
  7187. return { geometry: geometry, materials: materials };
  7188. }
  7189. };
  7190. // File:src/loaders/LoadingManager.js
  7191. /**
  7192. * @author mrdoob / http://mrdoob.com/
  7193. */
  7194. THREE.LoadingManager = function ( onLoad, onProgress, onError ) {
  7195. var scope = this;
  7196. var loaded = 0, total = 0;
  7197. this.onLoad = onLoad;
  7198. this.onProgress = onProgress;
  7199. this.onError = onError;
  7200. this.itemStart = function ( url ) {
  7201. total ++;
  7202. };
  7203. this.itemEnd = function ( url ) {
  7204. loaded ++;
  7205. if ( scope.onProgress !== undefined ) {
  7206. scope.onProgress( url, loaded, total );
  7207. }
  7208. if ( loaded === total && scope.onLoad !== undefined ) {
  7209. scope.onLoad();
  7210. }
  7211. };
  7212. };
  7213. THREE.DefaultLoadingManager = new THREE.LoadingManager();
  7214. // File:src/loaders/BufferGeometryLoader.js
  7215. /**
  7216. * @author mrdoob / http://mrdoob.com/
  7217. */
  7218. THREE.BufferGeometryLoader = function ( manager ) {
  7219. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  7220. };
  7221. THREE.BufferGeometryLoader.prototype = {
  7222. constructor: THREE.BufferGeometryLoader,
  7223. load: function ( url, onLoad, onProgress, onError ) {
  7224. var scope = this;
  7225. var loader = new THREE.XHRLoader( scope.manager );
  7226. loader.setCrossOrigin( this.crossOrigin );
  7227. loader.load( url, function ( text ) {
  7228. onLoad( scope.parse( JSON.parse( text ) ) );
  7229. }, onProgress, onError );
  7230. },
  7231. setCrossOrigin: function ( value ) {
  7232. this.crossOrigin = value;
  7233. },
  7234. parse: function ( json ) {
  7235. var geometry = new THREE.BufferGeometry();
  7236. var attributes = json.attributes;
  7237. for ( var key in attributes ) {
  7238. var attribute = attributes[ key ];
  7239. var typedArray = new self[ attribute.type ]( attribute.array );
  7240. geometry.addAttribute( key, new THREE.BufferAttribute( typedArray, attribute.itemSize ) );
  7241. }
  7242. var offsets = json.offsets;
  7243. if ( offsets !== undefined ) {
  7244. geometry.offsets = JSON.parse( JSON.stringify( offsets ) );
  7245. }
  7246. var boundingSphere = json.boundingSphere;
  7247. if ( boundingSphere !== undefined ) {
  7248. var center = new THREE.Vector3();
  7249. if ( boundingSphere.center !== undefined ) {
  7250. center.fromArray( boundingSphere.center );
  7251. }
  7252. geometry.boundingSphere = new THREE.Sphere( center, boundingSphere.radius );
  7253. }
  7254. return geometry;
  7255. }
  7256. };
  7257. // File:src/loaders/MaterialLoader.js
  7258. /**
  7259. * @author mrdoob / http://mrdoob.com/
  7260. */
  7261. THREE.MaterialLoader = function ( manager ) {
  7262. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  7263. };
  7264. THREE.MaterialLoader.prototype = {
  7265. constructor: THREE.MaterialLoader,
  7266. load: function ( url, onLoad, onProgress, onError ) {
  7267. var scope = this;
  7268. var loader = new THREE.XHRLoader( scope.manager );
  7269. loader.setCrossOrigin( this.crossOrigin );
  7270. loader.load( url, function ( text ) {
  7271. onLoad( scope.parse( JSON.parse( text ) ) );
  7272. }, onProgress, onError );
  7273. },
  7274. setCrossOrigin: function ( value ) {
  7275. this.crossOrigin = value;
  7276. },
  7277. parse: function ( json ) {
  7278. var material = new THREE[ json.type ];
  7279. if ( json.color !== undefined ) material.color.setHex( json.color );
  7280. if ( json.ambient !== undefined ) material.ambient.setHex( json.ambient );
  7281. if ( json.emissive !== undefined ) material.emissive.setHex( json.emissive );
  7282. if ( json.specular !== undefined ) material.specular.setHex( json.specular );
  7283. if ( json.shininess !== undefined ) material.shininess = json.shininess;
  7284. if ( json.uniforms !== undefined ) material.uniforms = json.uniforms;
  7285. if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader;
  7286. if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader;
  7287. if ( json.vertexColors !== undefined ) material.vertexColors = json.vertexColors;
  7288. if ( json.shading !== undefined ) material.shading = json.shading;
  7289. if ( json.blending !== undefined ) material.blending = json.blending;
  7290. if ( json.side !== undefined ) material.side = json.side;
  7291. if ( json.opacity !== undefined ) material.opacity = json.opacity;
  7292. if ( json.transparent !== undefined ) material.transparent = json.transparent;
  7293. if ( json.wireframe !== undefined ) material.wireframe = json.wireframe;
  7294. if ( json.materials !== undefined ) {
  7295. for ( var i = 0, l = json.materials.length; i < l; i ++ ) {
  7296. material.materials.push( this.parse( json.materials[ i ] ) );
  7297. }
  7298. }
  7299. return material;
  7300. }
  7301. };
  7302. // File:src/loaders/ObjectLoader.js
  7303. /**
  7304. * @author mrdoob / http://mrdoob.com/
  7305. */
  7306. THREE.ObjectLoader = function ( manager ) {
  7307. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  7308. };
  7309. THREE.ObjectLoader.prototype = {
  7310. constructor: THREE.ObjectLoader,
  7311. load: function ( url, onLoad, onProgress, onError ) {
  7312. var scope = this;
  7313. var loader = new THREE.XHRLoader( scope.manager );
  7314. loader.setCrossOrigin( this.crossOrigin );
  7315. loader.load( url, function ( text ) {
  7316. onLoad( scope.parse( JSON.parse( text ) ) );
  7317. }, onProgress, onError );
  7318. },
  7319. setCrossOrigin: function ( value ) {
  7320. this.crossOrigin = value;
  7321. },
  7322. parse: function ( json ) {
  7323. var geometries = this.parseGeometries( json.geometries );
  7324. var materials = this.parseMaterials( json.materials );
  7325. var object = this.parseObject( json.object, geometries, materials );
  7326. return object;
  7327. },
  7328. parseGeometries: function ( json ) {
  7329. var geometries = {};
  7330. if ( json !== undefined ) {
  7331. var geometryLoader = new THREE.JSONLoader();
  7332. var bufferGeometryLoader = new THREE.BufferGeometryLoader();
  7333. for ( var i = 0, l = json.length; i < l; i ++ ) {
  7334. var geometry;
  7335. var data = json[ i ];
  7336. switch ( data.type ) {
  7337. case 'PlaneGeometry':
  7338. geometry = new THREE.PlaneGeometry(
  7339. data.width,
  7340. data.height,
  7341. data.widthSegments,
  7342. data.heightSegments
  7343. );
  7344. break;
  7345. case 'BoxGeometry':
  7346. case 'CubeGeometry': // backwards compatible
  7347. geometry = new THREE.BoxGeometry(
  7348. data.width,
  7349. data.height,
  7350. data.depth,
  7351. data.widthSegments,
  7352. data.heightSegments,
  7353. data.depthSegments
  7354. );
  7355. break;
  7356. case 'CircleGeometry':
  7357. geometry = new THREE.CircleGeometry(
  7358. data.radius,
  7359. data.segments
  7360. );
  7361. break;
  7362. case 'CylinderGeometry':
  7363. geometry = new THREE.CylinderGeometry(
  7364. data.radiusTop,
  7365. data.radiusBottom,
  7366. data.height,
  7367. data.radialSegments,
  7368. data.heightSegments,
  7369. data.openEnded
  7370. );
  7371. break;
  7372. case 'SphereGeometry':
  7373. geometry = new THREE.SphereGeometry(
  7374. data.radius,
  7375. data.widthSegments,
  7376. data.heightSegments,
  7377. data.phiStart,
  7378. data.phiLength,
  7379. data.thetaStart,
  7380. data.thetaLength
  7381. );
  7382. break;
  7383. case 'IcosahedronGeometry':
  7384. geometry = new THREE.IcosahedronGeometry(
  7385. data.radius,
  7386. data.detail
  7387. );
  7388. break;
  7389. case 'TorusGeometry':
  7390. geometry = new THREE.TorusGeometry(
  7391. data.radius,
  7392. data.tube,
  7393. data.radialSegments,
  7394. data.tubularSegments,
  7395. data.arc
  7396. );
  7397. break;
  7398. case 'TorusKnotGeometry':
  7399. geometry = new THREE.TorusKnotGeometry(
  7400. data.radius,
  7401. data.tube,
  7402. data.radialSegments,
  7403. data.tubularSegments,
  7404. data.p,
  7405. data.q,
  7406. data.heightScale
  7407. );
  7408. break;
  7409. case 'BufferGeometry':
  7410. geometry = bufferGeometryLoader.parse( data.data );
  7411. break;
  7412. case 'Geometry':
  7413. geometry = geometryLoader.parse( data.data ).geometry;
  7414. break;
  7415. }
  7416. geometry.uuid = data.uuid;
  7417. if ( data.name !== undefined ) geometry.name = data.name;
  7418. geometries[ data.uuid ] = geometry;
  7419. }
  7420. }
  7421. return geometries;
  7422. },
  7423. parseMaterials: function ( json ) {
  7424. var materials = {};
  7425. if ( json !== undefined ) {
  7426. var loader = new THREE.MaterialLoader();
  7427. for ( var i = 0, l = json.length; i < l; i ++ ) {
  7428. var data = json[ i ];
  7429. var material = loader.parse( data );
  7430. material.uuid = data.uuid;
  7431. if ( data.name !== undefined ) material.name = data.name;
  7432. materials[ data.uuid ] = material;
  7433. }
  7434. }
  7435. return materials;
  7436. },
  7437. parseObject: function () {
  7438. var matrix = new THREE.Matrix4();
  7439. return function ( data, geometries, materials ) {
  7440. var object;
  7441. switch ( data.type ) {
  7442. case 'Scene':
  7443. object = new THREE.Scene();
  7444. break;
  7445. case 'PerspectiveCamera':
  7446. object = new THREE.PerspectiveCamera( data.fov, data.aspect, data.near, data.far );
  7447. break;
  7448. case 'OrthographicCamera':
  7449. object = new THREE.OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far );
  7450. break;
  7451. case 'AmbientLight':
  7452. object = new THREE.AmbientLight( data.color );
  7453. break;
  7454. case 'DirectionalLight':
  7455. object = new THREE.DirectionalLight( data.color, data.intensity );
  7456. break;
  7457. case 'PointLight':
  7458. object = new THREE.PointLight( data.color, data.intensity, data.distance );
  7459. break;
  7460. case 'SpotLight':
  7461. object = new THREE.SpotLight( data.color, data.intensity, data.distance, data.angle, data.exponent );
  7462. break;
  7463. case 'HemisphereLight':
  7464. object = new THREE.HemisphereLight( data.color, data.groundColor, data.intensity );
  7465. break;
  7466. case 'Mesh':
  7467. var geometry = geometries[ data.geometry ];
  7468. var material = materials[ data.material ];
  7469. if ( geometry === undefined ) {
  7470. console.warn( 'THREE.ObjectLoader: Undefined geometry', data.geometry );
  7471. }
  7472. if ( material === undefined ) {
  7473. console.warn( 'THREE.ObjectLoader: Undefined material', data.material );
  7474. }
  7475. object = new THREE.Mesh( geometry, material );
  7476. break;
  7477. case 'Line':
  7478. var geometry = geometries[ data.geometry ];
  7479. var material = materials[ data.material ];
  7480. if ( geometry === undefined ) {
  7481. console.warn( 'THREE.ObjectLoader: Undefined geometry', data.geometry );
  7482. }
  7483. if ( material === undefined ) {
  7484. console.warn( 'THREE.ObjectLoader: Undefined material', data.material );
  7485. }
  7486. object = new THREE.Line( geometry, material );
  7487. break;
  7488. case 'Sprite':
  7489. var material = materials[ data.material ];
  7490. if ( material === undefined ) {
  7491. console.warn( 'THREE.ObjectLoader: Undefined material', data.material );
  7492. }
  7493. object = new THREE.Sprite( material );
  7494. break;
  7495. case 'Group':
  7496. object = new THREE.Group();
  7497. break;
  7498. default:
  7499. object = new THREE.Object3D();
  7500. }
  7501. object.uuid = data.uuid;
  7502. if ( data.name !== undefined ) object.name = data.name;
  7503. if ( data.matrix !== undefined ) {
  7504. matrix.fromArray( data.matrix );
  7505. matrix.decompose( object.position, object.quaternion, object.scale );
  7506. } else {
  7507. if ( data.position !== undefined ) object.position.fromArray( data.position );
  7508. if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation );
  7509. if ( data.scale !== undefined ) object.scale.fromArray( data.scale );
  7510. }
  7511. if ( data.visible !== undefined ) object.visible = data.visible;
  7512. if ( data.userData !== undefined ) object.userData = data.userData;
  7513. if ( data.children !== undefined ) {
  7514. for ( var child in data.children ) {
  7515. object.add( this.parseObject( data.children[ child ], geometries, materials ) );
  7516. }
  7517. }
  7518. return object;
  7519. }
  7520. }()
  7521. };
  7522. // File:src/loaders/TextureLoader.js
  7523. /**
  7524. * @author mrdoob / http://mrdoob.com/
  7525. */
  7526. THREE.TextureLoader = function ( manager ) {
  7527. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  7528. };
  7529. THREE.TextureLoader.prototype = {
  7530. constructor: THREE.TextureLoader,
  7531. load: function ( url, onLoad, onProgress, onError ) {
  7532. var scope = this;
  7533. var loader = new THREE.ImageLoader( scope.manager );
  7534. loader.setCrossOrigin( this.crossOrigin );
  7535. loader.load( url, function ( image ) {
  7536. var texture = new THREE.Texture( image );
  7537. texture.needsUpdate = true;
  7538. if ( onLoad !== undefined ) {
  7539. onLoad( texture );
  7540. }
  7541. }, onProgress, onError );
  7542. },
  7543. setCrossOrigin: function ( value ) {
  7544. this.crossOrigin = value;
  7545. }
  7546. };
  7547. // File:src/loaders/BinaryTextureLoader.js
  7548. /**
  7549. * @author Nikos M. / https://github.com/foo123/
  7550. *
  7551. * Abstract Base class to load generic binary textures formats (rgbe, hdr, ...)
  7552. */
  7553. THREE.DataTextureLoader = THREE.BinaryTextureLoader = function () {
  7554. // override in sub classes
  7555. this._parser = null;
  7556. };
  7557. THREE.BinaryTextureLoader.prototype = {
  7558. constructor: THREE.BinaryTextureLoader,
  7559. load: function ( url, onLoad, onProgress, onError ) {
  7560. var scope = this;
  7561. var texture = new THREE.DataTexture( );
  7562. var loader = new THREE.XHRLoader();
  7563. loader.setResponseType( 'arraybuffer' );
  7564. loader.load( url, function ( buffer ) {
  7565. var texData = scope._parser( buffer );
  7566. if ( !texData ) return;
  7567. if ( undefined !== texData.image ) {
  7568. texture.image = texData.image;
  7569. } else if ( undefined !== texData.data ){
  7570. texture.image.width = texData.width;
  7571. texture.image.height = texData.height;
  7572. texture.image.data = texData.data;
  7573. }
  7574. texture.wrapS = undefined !== texData.wrapS ? texData.wrapS : THREE.ClampToEdgeWrapping;
  7575. texture.wrapT = undefined !== texData.wrapT ? texData.wrapT : THREE.ClampToEdgeWrapping;
  7576. texture.magFilter = undefined !== texData.magFilter ? texData.magFilter : THREE.LinearFilter;
  7577. texture.minFilter = undefined !== texData.minFilter ? texData.minFilter : THREE.LinearMipMapLinearFilter;
  7578. texture.anisotropy = undefined !== texData.anisotropy ? texData.anisotropy : 1;
  7579. if ( undefined !== texData.format ) {
  7580. texture.format = texData.format;
  7581. }
  7582. if ( undefined !== texData.type ) {
  7583. texture.type = texData.type;
  7584. }
  7585. if ( undefined !== texData.mipmaps ) {
  7586. texture.mipmaps = texData.mipmaps;
  7587. }
  7588. if ( 1 === texData.mipmapCount ) {
  7589. texture.minFilter = THREE.LinearFilter;
  7590. }
  7591. texture.needsUpdate = true;
  7592. if ( onLoad ) onLoad( texture, texData );
  7593. }, onProgress, onError );
  7594. return texture;
  7595. }
  7596. };
  7597. // File:src/loaders/CompressedTextureLoader.js
  7598. /**
  7599. * @author mrdoob / http://mrdoob.com/
  7600. *
  7601. * Abstract Base class to block based textures loader (dds, pvr, ...)
  7602. */
  7603. THREE.CompressedTextureLoader = function () {
  7604. // override in sub classes
  7605. this._parser = null;
  7606. };
  7607. THREE.CompressedTextureLoader.prototype = {
  7608. constructor: THREE.CompressedTextureLoader,
  7609. load: function ( url, onLoad, onError ) {
  7610. var scope = this;
  7611. var images = [];
  7612. var texture = new THREE.CompressedTexture();
  7613. texture.image = images;
  7614. var loader = new THREE.XHRLoader();
  7615. loader.setResponseType( 'arraybuffer' );
  7616. if ( url instanceof Array ) {
  7617. var loaded = 0;
  7618. var loadTexture = function ( i ) {
  7619. loader.load( url[ i ], function ( buffer ) {
  7620. var texDatas = scope._parser( buffer, true );
  7621. images[ i ] = {
  7622. width: texDatas.width,
  7623. height: texDatas.height,
  7624. format: texDatas.format,
  7625. mipmaps: texDatas.mipmaps
  7626. };
  7627. loaded += 1;
  7628. if ( loaded === 6 ) {
  7629. if (texDatas.mipmapCount == 1)
  7630. texture.minFilter = THREE.LinearFilter;
  7631. texture.format = texDatas.format;
  7632. texture.needsUpdate = true;
  7633. if ( onLoad ) onLoad( texture );
  7634. }
  7635. } );
  7636. };
  7637. for ( var i = 0, il = url.length; i < il; ++ i ) {
  7638. loadTexture( i );
  7639. }
  7640. } else {
  7641. // compressed cubemap texture stored in a single DDS file
  7642. loader.load( url, function ( buffer ) {
  7643. var texDatas = scope._parser( buffer, true );
  7644. if ( texDatas.isCubemap ) {
  7645. var faces = texDatas.mipmaps.length / texDatas.mipmapCount;
  7646. for ( var f = 0; f < faces; f ++ ) {
  7647. images[ f ] = { mipmaps : [] };
  7648. for ( var i = 0; i < texDatas.mipmapCount; i ++ ) {
  7649. images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] );
  7650. images[ f ].format = texDatas.format;
  7651. images[ f ].width = texDatas.width;
  7652. images[ f ].height = texDatas.height;
  7653. }
  7654. }
  7655. } else {
  7656. texture.image.width = texDatas.width;
  7657. texture.image.height = texDatas.height;
  7658. texture.mipmaps = texDatas.mipmaps;
  7659. }
  7660. if ( texDatas.mipmapCount === 1 ) {
  7661. texture.minFilter = THREE.LinearFilter;
  7662. }
  7663. texture.format = texDatas.format;
  7664. texture.needsUpdate = true;
  7665. if ( onLoad ) onLoad( texture );
  7666. } );
  7667. }
  7668. return texture;
  7669. }
  7670. };
  7671. // File:src/materials/Material.js
  7672. /**
  7673. * @author mrdoob / http://mrdoob.com/
  7674. * @author alteredq / http://alteredqualia.com/
  7675. */
  7676. THREE.Material = function () {
  7677. Object.defineProperty( this, 'id', { value: THREE.MaterialIdCount ++ } );
  7678. this.uuid = THREE.Math.generateUUID();
  7679. this.name = '';
  7680. this.type = 'Material';
  7681. this.side = THREE.FrontSide;
  7682. this.opacity = 1;
  7683. this.transparent = false;
  7684. this.blending = THREE.NormalBlending;
  7685. this.blendSrc = THREE.SrcAlphaFactor;
  7686. this.blendDst = THREE.OneMinusSrcAlphaFactor;
  7687. this.blendEquation = THREE.AddEquation;
  7688. this.depthTest = true;
  7689. this.depthWrite = true;
  7690. this.polygonOffset = false;
  7691. this.polygonOffsetFactor = 0;
  7692. this.polygonOffsetUnits = 0;
  7693. this.alphaTest = 0;
  7694. this.overdraw = 0; // Overdrawn pixels (typically between 0 and 1) for fixing antialiasing gaps in CanvasRenderer
  7695. this.visible = true;
  7696. this.needsUpdate = true;
  7697. };
  7698. THREE.Material.prototype = {
  7699. constructor: THREE.Material,
  7700. setValues: function ( values ) {
  7701. if ( values === undefined ) return;
  7702. for ( var key in values ) {
  7703. var newValue = values[ key ];
  7704. if ( newValue === undefined ) {
  7705. console.warn( "THREE.Material: '" + key + "' parameter is undefined." );
  7706. continue;
  7707. }
  7708. if ( key in this ) {
  7709. var currentValue = this[ key ];
  7710. if ( currentValue instanceof THREE.Color ) {
  7711. currentValue.set( newValue );
  7712. } else if ( currentValue instanceof THREE.Vector3 && newValue instanceof THREE.Vector3 ) {
  7713. currentValue.copy( newValue );
  7714. } else if ( key == 'overdraw' ) {
  7715. // ensure overdraw is backwards-compatable with legacy boolean type
  7716. this[ key ] = Number( newValue );
  7717. } else {
  7718. this[ key ] = newValue;
  7719. }
  7720. }
  7721. }
  7722. },
  7723. toJSON: function () {
  7724. var output = {
  7725. metadata: {
  7726. version: 4.2,
  7727. type: 'material',
  7728. generator: 'MaterialExporter'
  7729. },
  7730. uuid: this.uuid,
  7731. type: this.type
  7732. };
  7733. if ( this.name !== "" ) output.name = this.name;
  7734. if ( this instanceof THREE.MeshBasicMaterial ) {
  7735. output.color = this.color.getHex();
  7736. if ( this.vertexColors !== THREE.NoColors ) output.vertexColors = this.vertexColors;
  7737. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  7738. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  7739. } else if ( this instanceof THREE.MeshLambertMaterial ) {
  7740. output.color = this.color.getHex();
  7741. output.ambient = this.ambient.getHex();
  7742. output.emissive = this.emissive.getHex();
  7743. if ( this.vertexColors !== THREE.NoColors ) output.vertexColors = this.vertexColors;
  7744. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  7745. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  7746. } else if ( this instanceof THREE.MeshPhongMaterial ) {
  7747. output.color = this.color.getHex();
  7748. output.ambient = this.ambient.getHex();
  7749. output.emissive = this.emissive.getHex();
  7750. output.specular = this.specular.getHex();
  7751. output.shininess = this.shininess;
  7752. if ( this.vertexColors !== THREE.NoColors ) output.vertexColors = this.vertexColors;
  7753. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  7754. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  7755. } else if ( this instanceof THREE.MeshNormalMaterial ) {
  7756. if ( this.shading !== THREE.FlatShading ) output.shading = this.shading;
  7757. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  7758. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  7759. } else if ( this instanceof THREE.MeshDepthMaterial ) {
  7760. if ( this.blending !== THREE.NormalBlending ) output.blending = this.blending;
  7761. if ( this.side !== THREE.FrontSide ) output.side = this.side;
  7762. } else if ( this instanceof THREE.ShaderMaterial ) {
  7763. output.uniforms = this.uniforms;
  7764. output.vertexShader = this.vertexShader;
  7765. output.fragmentShader = this.fragmentShader;
  7766. } else if ( this instanceof THREE.SpriteMaterial ) {
  7767. output.color = this.color.getHex();
  7768. }
  7769. if ( this.opacity < 1 ) output.opacity = this.opacity;
  7770. if ( this.transparent !== false ) output.transparent = this.transparent;
  7771. if ( this.wireframe !== false ) output.wireframe = this.wireframe;
  7772. return output;
  7773. },
  7774. clone: function ( material ) {
  7775. if ( material === undefined ) material = new THREE.Material();
  7776. material.name = this.name;
  7777. material.side = this.side;
  7778. material.opacity = this.opacity;
  7779. material.transparent = this.transparent;
  7780. material.blending = this.blending;
  7781. material.blendSrc = this.blendSrc;
  7782. material.blendDst = this.blendDst;
  7783. material.blendEquation = this.blendEquation;
  7784. material.depthTest = this.depthTest;
  7785. material.depthWrite = this.depthWrite;
  7786. material.polygonOffset = this.polygonOffset;
  7787. material.polygonOffsetFactor = this.polygonOffsetFactor;
  7788. material.polygonOffsetUnits = this.polygonOffsetUnits;
  7789. material.alphaTest = this.alphaTest;
  7790. material.overdraw = this.overdraw;
  7791. material.visible = this.visible;
  7792. return material;
  7793. },
  7794. dispose: function () {
  7795. this.dispatchEvent( { type: 'dispose' } );
  7796. }
  7797. };
  7798. THREE.EventDispatcher.prototype.apply( THREE.Material.prototype );
  7799. THREE.MaterialIdCount = 0;
  7800. // File:src/materials/LineBasicMaterial.js
  7801. /**
  7802. * @author mrdoob / http://mrdoob.com/
  7803. * @author alteredq / http://alteredqualia.com/
  7804. *
  7805. * parameters = {
  7806. * color: <hex>,
  7807. * opacity: <float>,
  7808. *
  7809. * blending: THREE.NormalBlending,
  7810. * depthTest: <bool>,
  7811. * depthWrite: <bool>,
  7812. *
  7813. * linewidth: <float>,
  7814. * linecap: "round",
  7815. * linejoin: "round",
  7816. *
  7817. * vertexColors: <bool>
  7818. *
  7819. * fog: <bool>
  7820. * }
  7821. */
  7822. THREE.LineBasicMaterial = function ( parameters ) {
  7823. THREE.Material.call( this );
  7824. this.type = 'LineBasicMaterial';
  7825. this.color = new THREE.Color( 0xffffff );
  7826. this.linewidth = 1;
  7827. this.linecap = 'round';
  7828. this.linejoin = 'round';
  7829. this.vertexColors = THREE.NoColors;
  7830. this.fog = true;
  7831. this.setValues( parameters );
  7832. };
  7833. THREE.LineBasicMaterial.prototype = Object.create( THREE.Material.prototype );
  7834. THREE.LineBasicMaterial.prototype.constructor = THREE.LineBasicMaterial;
  7835. THREE.LineBasicMaterial.prototype.clone = function () {
  7836. var material = new THREE.LineBasicMaterial();
  7837. THREE.Material.prototype.clone.call( this, material );
  7838. material.color.copy( this.color );
  7839. material.linewidth = this.linewidth;
  7840. material.linecap = this.linecap;
  7841. material.linejoin = this.linejoin;
  7842. material.vertexColors = this.vertexColors;
  7843. material.fog = this.fog;
  7844. return material;
  7845. };
  7846. // File:src/materials/LineDashedMaterial.js
  7847. /**
  7848. * @author alteredq / http://alteredqualia.com/
  7849. *
  7850. * parameters = {
  7851. * color: <hex>,
  7852. * opacity: <float>,
  7853. *
  7854. * blending: THREE.NormalBlending,
  7855. * depthTest: <bool>,
  7856. * depthWrite: <bool>,
  7857. *
  7858. * linewidth: <float>,
  7859. *
  7860. * scale: <float>,
  7861. * dashSize: <float>,
  7862. * gapSize: <float>,
  7863. *
  7864. * vertexColors: <bool>
  7865. *
  7866. * fog: <bool>
  7867. * }
  7868. */
  7869. THREE.LineDashedMaterial = function ( parameters ) {
  7870. THREE.Material.call( this );
  7871. this.type = 'LineDashedMaterial';
  7872. this.color = new THREE.Color( 0xffffff );
  7873. this.linewidth = 1;
  7874. this.scale = 1;
  7875. this.dashSize = 3;
  7876. this.gapSize = 1;
  7877. this.vertexColors = false;
  7878. this.fog = true;
  7879. this.setValues( parameters );
  7880. };
  7881. THREE.LineDashedMaterial.prototype = Object.create( THREE.Material.prototype );
  7882. THREE.LineDashedMaterial.prototype.constructor = THREE.LineDashedMaterial;
  7883. THREE.LineDashedMaterial.prototype.clone = function () {
  7884. var material = new THREE.LineDashedMaterial();
  7885. THREE.Material.prototype.clone.call( this, material );
  7886. material.color.copy( this.color );
  7887. material.linewidth = this.linewidth;
  7888. material.scale = this.scale;
  7889. material.dashSize = this.dashSize;
  7890. material.gapSize = this.gapSize;
  7891. material.vertexColors = this.vertexColors;
  7892. material.fog = this.fog;
  7893. return material;
  7894. };
  7895. // File:src/materials/MeshBasicMaterial.js
  7896. /**
  7897. * @author mrdoob / http://mrdoob.com/
  7898. * @author alteredq / http://alteredqualia.com/
  7899. *
  7900. * parameters = {
  7901. * color: <hex>,
  7902. * opacity: <float>,
  7903. * map: new THREE.Texture( <Image> ),
  7904. *
  7905. * lightMap: new THREE.Texture( <Image> ),
  7906. *
  7907. * specularMap: new THREE.Texture( <Image> ),
  7908. *
  7909. * alphaMap: new THREE.Texture( <Image> ),
  7910. *
  7911. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  7912. * combine: THREE.Multiply,
  7913. * reflectivity: <float>,
  7914. * refractionRatio: <float>,
  7915. *
  7916. * shading: THREE.SmoothShading,
  7917. * blending: THREE.NormalBlending,
  7918. * depthTest: <bool>,
  7919. * depthWrite: <bool>,
  7920. *
  7921. * wireframe: <boolean>,
  7922. * wireframeLinewidth: <float>,
  7923. *
  7924. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  7925. *
  7926. * skinning: <bool>,
  7927. * morphTargets: <bool>,
  7928. *
  7929. * fog: <bool>
  7930. * }
  7931. */
  7932. THREE.MeshBasicMaterial = function ( parameters ) {
  7933. THREE.Material.call( this );
  7934. this.type = 'MeshBasicMaterial';
  7935. this.color = new THREE.Color( 0xffffff ); // emissive
  7936. this.map = null;
  7937. this.lightMap = null;
  7938. this.specularMap = null;
  7939. this.alphaMap = null;
  7940. this.envMap = null;
  7941. this.combine = THREE.MultiplyOperation;
  7942. this.reflectivity = 1;
  7943. this.refractionRatio = 0.98;
  7944. this.fog = true;
  7945. this.shading = THREE.SmoothShading;
  7946. this.wireframe = false;
  7947. this.wireframeLinewidth = 1;
  7948. this.wireframeLinecap = 'round';
  7949. this.wireframeLinejoin = 'round';
  7950. this.vertexColors = THREE.NoColors;
  7951. this.skinning = false;
  7952. this.morphTargets = false;
  7953. this.setValues( parameters );
  7954. };
  7955. THREE.MeshBasicMaterial.prototype = Object.create( THREE.Material.prototype );
  7956. THREE.MeshBasicMaterial.prototype.constructor = THREE.MeshBasicMaterial;
  7957. THREE.MeshBasicMaterial.prototype.clone = function () {
  7958. var material = new THREE.MeshBasicMaterial();
  7959. THREE.Material.prototype.clone.call( this, material );
  7960. material.color.copy( this.color );
  7961. material.map = this.map;
  7962. material.lightMap = this.lightMap;
  7963. material.specularMap = this.specularMap;
  7964. material.alphaMap = this.alphaMap;
  7965. material.envMap = this.envMap;
  7966. material.combine = this.combine;
  7967. material.reflectivity = this.reflectivity;
  7968. material.refractionRatio = this.refractionRatio;
  7969. material.fog = this.fog;
  7970. material.shading = this.shading;
  7971. material.wireframe = this.wireframe;
  7972. material.wireframeLinewidth = this.wireframeLinewidth;
  7973. material.wireframeLinecap = this.wireframeLinecap;
  7974. material.wireframeLinejoin = this.wireframeLinejoin;
  7975. material.vertexColors = this.vertexColors;
  7976. material.skinning = this.skinning;
  7977. material.morphTargets = this.morphTargets;
  7978. return material;
  7979. };
  7980. // File:src/materials/MeshLambertMaterial.js
  7981. /**
  7982. * @author mrdoob / http://mrdoob.com/
  7983. * @author alteredq / http://alteredqualia.com/
  7984. *
  7985. * parameters = {
  7986. * color: <hex>,
  7987. * ambient: <hex>,
  7988. * emissive: <hex>,
  7989. * opacity: <float>,
  7990. *
  7991. * map: new THREE.Texture( <Image> ),
  7992. *
  7993. * lightMap: new THREE.Texture( <Image> ),
  7994. *
  7995. * specularMap: new THREE.Texture( <Image> ),
  7996. *
  7997. * alphaMap: new THREE.Texture( <Image> ),
  7998. *
  7999. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  8000. * combine: THREE.Multiply,
  8001. * reflectivity: <float>,
  8002. * refractionRatio: <float>,
  8003. *
  8004. * shading: THREE.SmoothShading,
  8005. * blending: THREE.NormalBlending,
  8006. * depthTest: <bool>,
  8007. * depthWrite: <bool>,
  8008. *
  8009. * wireframe: <boolean>,
  8010. * wireframeLinewidth: <float>,
  8011. *
  8012. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  8013. *
  8014. * skinning: <bool>,
  8015. * morphTargets: <bool>,
  8016. * morphNormals: <bool>,
  8017. *
  8018. * fog: <bool>
  8019. * }
  8020. */
  8021. THREE.MeshLambertMaterial = function ( parameters ) {
  8022. THREE.Material.call( this );
  8023. this.type = 'MeshLambertMaterial';
  8024. this.color = new THREE.Color( 0xffffff ); // diffuse
  8025. this.ambient = new THREE.Color( 0xffffff );
  8026. this.emissive = new THREE.Color( 0x000000 );
  8027. this.wrapAround = false;
  8028. this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
  8029. this.map = null;
  8030. this.lightMap = null;
  8031. this.specularMap = null;
  8032. this.alphaMap = null;
  8033. this.envMap = null;
  8034. this.combine = THREE.MultiplyOperation;
  8035. this.reflectivity = 1;
  8036. this.refractionRatio = 0.98;
  8037. this.fog = true;
  8038. this.shading = THREE.SmoothShading;
  8039. this.wireframe = false;
  8040. this.wireframeLinewidth = 1;
  8041. this.wireframeLinecap = 'round';
  8042. this.wireframeLinejoin = 'round';
  8043. this.vertexColors = THREE.NoColors;
  8044. this.skinning = false;
  8045. this.morphTargets = false;
  8046. this.morphNormals = false;
  8047. this.setValues( parameters );
  8048. };
  8049. THREE.MeshLambertMaterial.prototype = Object.create( THREE.Material.prototype );
  8050. THREE.MeshLambertMaterial.prototype.constructor = THREE.MeshLambertMaterial;
  8051. THREE.MeshLambertMaterial.prototype.clone = function () {
  8052. var material = new THREE.MeshLambertMaterial();
  8053. THREE.Material.prototype.clone.call( this, material );
  8054. material.color.copy( this.color );
  8055. material.ambient.copy( this.ambient );
  8056. material.emissive.copy( this.emissive );
  8057. material.wrapAround = this.wrapAround;
  8058. material.wrapRGB.copy( this.wrapRGB );
  8059. material.map = this.map;
  8060. material.lightMap = this.lightMap;
  8061. material.specularMap = this.specularMap;
  8062. material.alphaMap = this.alphaMap;
  8063. material.envMap = this.envMap;
  8064. material.combine = this.combine;
  8065. material.reflectivity = this.reflectivity;
  8066. material.refractionRatio = this.refractionRatio;
  8067. material.fog = this.fog;
  8068. material.shading = this.shading;
  8069. material.wireframe = this.wireframe;
  8070. material.wireframeLinewidth = this.wireframeLinewidth;
  8071. material.wireframeLinecap = this.wireframeLinecap;
  8072. material.wireframeLinejoin = this.wireframeLinejoin;
  8073. material.vertexColors = this.vertexColors;
  8074. material.skinning = this.skinning;
  8075. material.morphTargets = this.morphTargets;
  8076. material.morphNormals = this.morphNormals;
  8077. return material;
  8078. };
  8079. // File:src/materials/MeshPhongMaterial.js
  8080. /**
  8081. * @author mrdoob / http://mrdoob.com/
  8082. * @author alteredq / http://alteredqualia.com/
  8083. *
  8084. * parameters = {
  8085. * color: <hex>,
  8086. * ambient: <hex>,
  8087. * emissive: <hex>,
  8088. * specular: <hex>,
  8089. * shininess: <float>,
  8090. * opacity: <float>,
  8091. *
  8092. * map: new THREE.Texture( <Image> ),
  8093. *
  8094. * lightMap: new THREE.Texture( <Image> ),
  8095. *
  8096. * bumpMap: new THREE.Texture( <Image> ),
  8097. * bumpScale: <float>,
  8098. *
  8099. * normalMap: new THREE.Texture( <Image> ),
  8100. * normalScale: <Vector2>,
  8101. *
  8102. * specularMap: new THREE.Texture( <Image> ),
  8103. *
  8104. * alphaMap: new THREE.Texture( <Image> ),
  8105. *
  8106. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  8107. * combine: THREE.Multiply,
  8108. * reflectivity: <float>,
  8109. * refractionRatio: <float>,
  8110. *
  8111. * shading: THREE.SmoothShading,
  8112. * blending: THREE.NormalBlending,
  8113. * depthTest: <bool>,
  8114. * depthWrite: <bool>,
  8115. *
  8116. * wireframe: <boolean>,
  8117. * wireframeLinewidth: <float>,
  8118. *
  8119. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  8120. *
  8121. * skinning: <bool>,
  8122. * morphTargets: <bool>,
  8123. * morphNormals: <bool>,
  8124. *
  8125. * fog: <bool>
  8126. * }
  8127. */
  8128. THREE.MeshPhongMaterial = function ( parameters ) {
  8129. THREE.Material.call( this );
  8130. this.type = 'MeshPhongMaterial';
  8131. this.color = new THREE.Color( 0xffffff ); // diffuse
  8132. this.ambient = new THREE.Color( 0xffffff );
  8133. this.emissive = new THREE.Color( 0x000000 );
  8134. this.specular = new THREE.Color( 0x111111 );
  8135. this.shininess = 30;
  8136. this.metal = false;
  8137. this.wrapAround = false;
  8138. this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
  8139. this.map = null;
  8140. this.lightMap = null;
  8141. this.bumpMap = null;
  8142. this.bumpScale = 1;
  8143. this.normalMap = null;
  8144. this.normalScale = new THREE.Vector2( 1, 1 );
  8145. this.specularMap = null;
  8146. this.alphaMap = null;
  8147. this.envMap = null;
  8148. this.combine = THREE.MultiplyOperation;
  8149. this.reflectivity = 1;
  8150. this.refractionRatio = 0.98;
  8151. this.fog = true;
  8152. this.shading = THREE.SmoothShading;
  8153. this.wireframe = false;
  8154. this.wireframeLinewidth = 1;
  8155. this.wireframeLinecap = 'round';
  8156. this.wireframeLinejoin = 'round';
  8157. this.vertexColors = THREE.NoColors;
  8158. this.skinning = false;
  8159. this.morphTargets = false;
  8160. this.morphNormals = false;
  8161. this.setValues( parameters );
  8162. };
  8163. THREE.MeshPhongMaterial.prototype = Object.create( THREE.Material.prototype );
  8164. THREE.MeshPhongMaterial.prototype.constructor = THREE.MeshPhongMaterial;
  8165. THREE.MeshPhongMaterial.prototype.clone = function () {
  8166. var material = new THREE.MeshPhongMaterial();
  8167. THREE.Material.prototype.clone.call( this, material );
  8168. material.color.copy( this.color );
  8169. material.ambient.copy( this.ambient );
  8170. material.emissive.copy( this.emissive );
  8171. material.specular.copy( this.specular );
  8172. material.shininess = this.shininess;
  8173. material.metal = this.metal;
  8174. material.wrapAround = this.wrapAround;
  8175. material.wrapRGB.copy( this.wrapRGB );
  8176. material.map = this.map;
  8177. material.lightMap = this.lightMap;
  8178. material.bumpMap = this.bumpMap;
  8179. material.bumpScale = this.bumpScale;
  8180. material.normalMap = this.normalMap;
  8181. material.normalScale.copy( this.normalScale );
  8182. material.specularMap = this.specularMap;
  8183. material.alphaMap = this.alphaMap;
  8184. material.envMap = this.envMap;
  8185. material.combine = this.combine;
  8186. material.reflectivity = this.reflectivity;
  8187. material.refractionRatio = this.refractionRatio;
  8188. material.fog = this.fog;
  8189. material.shading = this.shading;
  8190. material.wireframe = this.wireframe;
  8191. material.wireframeLinewidth = this.wireframeLinewidth;
  8192. material.wireframeLinecap = this.wireframeLinecap;
  8193. material.wireframeLinejoin = this.wireframeLinejoin;
  8194. material.vertexColors = this.vertexColors;
  8195. material.skinning = this.skinning;
  8196. material.morphTargets = this.morphTargets;
  8197. material.morphNormals = this.morphNormals;
  8198. return material;
  8199. };
  8200. // File:src/materials/MeshDepthMaterial.js
  8201. /**
  8202. * @author mrdoob / http://mrdoob.com/
  8203. * @author alteredq / http://alteredqualia.com/
  8204. *
  8205. * parameters = {
  8206. * opacity: <float>,
  8207. *
  8208. * blending: THREE.NormalBlending,
  8209. * depthTest: <bool>,
  8210. * depthWrite: <bool>,
  8211. *
  8212. * wireframe: <boolean>,
  8213. * wireframeLinewidth: <float>
  8214. * }
  8215. */
  8216. THREE.MeshDepthMaterial = function ( parameters ) {
  8217. THREE.Material.call( this );
  8218. this.type = 'MeshDepthMaterial';
  8219. this.morphTargets = false;
  8220. this.wireframe = false;
  8221. this.wireframeLinewidth = 1;
  8222. this.setValues( parameters );
  8223. };
  8224. THREE.MeshDepthMaterial.prototype = Object.create( THREE.Material.prototype );
  8225. THREE.MeshDepthMaterial.prototype.constructor = THREE.MeshDepthMaterial;
  8226. THREE.MeshDepthMaterial.prototype.clone = function () {
  8227. var material = new THREE.MeshDepthMaterial();
  8228. THREE.Material.prototype.clone.call( this, material );
  8229. material.wireframe = this.wireframe;
  8230. material.wireframeLinewidth = this.wireframeLinewidth;
  8231. return material;
  8232. };
  8233. // File:src/materials/MeshNormalMaterial.js
  8234. /**
  8235. * @author mrdoob / http://mrdoob.com/
  8236. *
  8237. * parameters = {
  8238. * opacity: <float>,
  8239. *
  8240. * shading: THREE.FlatShading,
  8241. * blending: THREE.NormalBlending,
  8242. * depthTest: <bool>,
  8243. * depthWrite: <bool>,
  8244. *
  8245. * wireframe: <boolean>,
  8246. * wireframeLinewidth: <float>
  8247. * }
  8248. */
  8249. THREE.MeshNormalMaterial = function ( parameters ) {
  8250. THREE.Material.call( this, parameters );
  8251. this.type = 'MeshNormalMaterial';
  8252. this.shading = THREE.FlatShading;
  8253. this.wireframe = false;
  8254. this.wireframeLinewidth = 1;
  8255. this.morphTargets = false;
  8256. this.setValues( parameters );
  8257. };
  8258. THREE.MeshNormalMaterial.prototype = Object.create( THREE.Material.prototype );
  8259. THREE.MeshNormalMaterial.prototype.constructor = THREE.MeshNormalMaterial;
  8260. THREE.MeshNormalMaterial.prototype.clone = function () {
  8261. var material = new THREE.MeshNormalMaterial();
  8262. THREE.Material.prototype.clone.call( this, material );
  8263. material.shading = this.shading;
  8264. material.wireframe = this.wireframe;
  8265. material.wireframeLinewidth = this.wireframeLinewidth;
  8266. return material;
  8267. };
  8268. // File:src/materials/MeshFaceMaterial.js
  8269. /**
  8270. * @author mrdoob / http://mrdoob.com/
  8271. */
  8272. THREE.MeshFaceMaterial = function ( materials ) {
  8273. this.uuid = THREE.Math.generateUUID();
  8274. this.type = 'MeshFaceMaterial';
  8275. this.materials = materials instanceof Array ? materials : [];
  8276. };
  8277. THREE.MeshFaceMaterial.prototype = {
  8278. constructor: THREE.MeshFaceMaterial,
  8279. toJSON: function () {
  8280. var output = {
  8281. metadata: {
  8282. version: 4.2,
  8283. type: 'material',
  8284. generator: 'MaterialExporter'
  8285. },
  8286. uuid: this.uuid,
  8287. type: this.type,
  8288. materials: []
  8289. };
  8290. for ( var i = 0, l = this.materials.length; i < l; i ++ ) {
  8291. output.materials.push( this.materials[ i ].toJSON() );
  8292. }
  8293. return output;
  8294. },
  8295. clone: function () {
  8296. var material = new THREE.MeshFaceMaterial();
  8297. for ( var i = 0; i < this.materials.length; i ++ ) {
  8298. material.materials.push( this.materials[ i ].clone() );
  8299. }
  8300. return material;
  8301. }
  8302. };
  8303. // File:src/materials/PointCloudMaterial.js
  8304. /**
  8305. * @author mrdoob / http://mrdoob.com/
  8306. * @author alteredq / http://alteredqualia.com/
  8307. *
  8308. * parameters = {
  8309. * color: <hex>,
  8310. * opacity: <float>,
  8311. * map: new THREE.Texture( <Image> ),
  8312. *
  8313. * size: <float>,
  8314. *
  8315. * blending: THREE.NormalBlending,
  8316. * depthTest: <bool>,
  8317. * depthWrite: <bool>,
  8318. *
  8319. * vertexColors: <bool>,
  8320. *
  8321. * fog: <bool>
  8322. * }
  8323. */
  8324. THREE.PointCloudMaterial = function ( parameters ) {
  8325. THREE.Material.call( this );
  8326. this.type = 'PointCloudMaterial';
  8327. this.color = new THREE.Color( 0xffffff );
  8328. this.map = null;
  8329. this.size = 1;
  8330. this.sizeAttenuation = true;
  8331. this.vertexColors = THREE.NoColors;
  8332. this.fog = true;
  8333. this.setValues( parameters );
  8334. };
  8335. THREE.PointCloudMaterial.prototype = Object.create( THREE.Material.prototype );
  8336. THREE.PointCloudMaterial.prototype.constructor = THREE.PointCloudMaterial;
  8337. THREE.PointCloudMaterial.prototype.clone = function () {
  8338. var material = new THREE.PointCloudMaterial();
  8339. THREE.Material.prototype.clone.call( this, material );
  8340. material.color.copy( this.color );
  8341. material.map = this.map;
  8342. material.size = this.size;
  8343. material.sizeAttenuation = this.sizeAttenuation;
  8344. material.vertexColors = this.vertexColors;
  8345. material.fog = this.fog;
  8346. return material;
  8347. };
  8348. // backwards compatibility
  8349. THREE.ParticleBasicMaterial = function ( parameters ) {
  8350. console.warn( 'THREE.ParticleBasicMaterial has been renamed to THREE.PointCloudMaterial.' );
  8351. return new THREE.PointCloudMaterial( parameters );
  8352. };
  8353. THREE.ParticleSystemMaterial = function ( parameters ) {
  8354. console.warn( 'THREE.ParticleSystemMaterial has been renamed to THREE.PointCloudMaterial.' );
  8355. return new THREE.PointCloudMaterial( parameters );
  8356. };
  8357. // File:src/materials/ShaderMaterial.js
  8358. /**
  8359. * @author alteredq / http://alteredqualia.com/
  8360. *
  8361. * parameters = {
  8362. * defines: { "label" : "value" },
  8363. * uniforms: { "parameter1": { type: "f", value: 1.0 }, "parameter2": { type: "i" value2: 2 } },
  8364. *
  8365. * fragmentShader: <string>,
  8366. * vertexShader: <string>,
  8367. *
  8368. * shading: THREE.SmoothShading,
  8369. * blending: THREE.NormalBlending,
  8370. * depthTest: <bool>,
  8371. * depthWrite: <bool>,
  8372. *
  8373. * wireframe: <boolean>,
  8374. * wireframeLinewidth: <float>,
  8375. *
  8376. * lights: <bool>,
  8377. *
  8378. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  8379. *
  8380. * skinning: <bool>,
  8381. * morphTargets: <bool>,
  8382. * morphNormals: <bool>,
  8383. *
  8384. * fog: <bool>
  8385. * }
  8386. */
  8387. THREE.ShaderMaterial = function ( parameters ) {
  8388. THREE.Material.call( this );
  8389. this.type = 'ShaderMaterial';
  8390. this.defines = {};
  8391. this.uniforms = {};
  8392. this.attributes = null;
  8393. this.vertexShader = 'void main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}';
  8394. this.fragmentShader = 'void main() {\n\tgl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n}';
  8395. this.shading = THREE.SmoothShading;
  8396. this.linewidth = 1;
  8397. this.wireframe = false;
  8398. this.wireframeLinewidth = 1;
  8399. this.fog = false; // set to use scene fog
  8400. this.lights = false; // set to use scene lights
  8401. this.vertexColors = THREE.NoColors; // set to use "color" attribute stream
  8402. this.skinning = false; // set to use skinning attribute streams
  8403. this.morphTargets = false; // set to use morph targets
  8404. this.morphNormals = false; // set to use morph normals
  8405. // When rendered geometry doesn't include these attributes but the material does,
  8406. // use these default values in WebGL. This avoids errors when buffer data is missing.
  8407. this.defaultAttributeValues = {
  8408. 'color': [ 1, 1, 1 ],
  8409. 'uv': [ 0, 0 ],
  8410. 'uv2': [ 0, 0 ]
  8411. };
  8412. this.index0AttributeName = undefined;
  8413. this.setValues( parameters );
  8414. };
  8415. THREE.ShaderMaterial.prototype = Object.create( THREE.Material.prototype );
  8416. THREE.ShaderMaterial.prototype.constructor = THREE.ShaderMaterial;
  8417. THREE.ShaderMaterial.prototype.clone = function () {
  8418. var material = new THREE.ShaderMaterial();
  8419. THREE.Material.prototype.clone.call( this, material );
  8420. material.fragmentShader = this.fragmentShader;
  8421. material.vertexShader = this.vertexShader;
  8422. material.uniforms = THREE.UniformsUtils.clone( this.uniforms );
  8423. material.attributes = this.attributes;
  8424. material.defines = this.defines;
  8425. material.shading = this.shading;
  8426. material.wireframe = this.wireframe;
  8427. material.wireframeLinewidth = this.wireframeLinewidth;
  8428. material.fog = this.fog;
  8429. material.lights = this.lights;
  8430. material.vertexColors = this.vertexColors;
  8431. material.skinning = this.skinning;
  8432. material.morphTargets = this.morphTargets;
  8433. material.morphNormals = this.morphNormals;
  8434. return material;
  8435. };
  8436. // File:src/materials/RawShaderMaterial.js
  8437. /**
  8438. * @author mrdoob / http://mrdoob.com/
  8439. */
  8440. THREE.RawShaderMaterial = function ( parameters ) {
  8441. THREE.ShaderMaterial.call( this, parameters );
  8442. this.type = 'RawShaderMaterial';
  8443. };
  8444. THREE.RawShaderMaterial.prototype = Object.create( THREE.ShaderMaterial.prototype );
  8445. THREE.RawShaderMaterial.prototype.constructor = THREE.RawShaderMaterial;
  8446. THREE.RawShaderMaterial.prototype.clone = function () {
  8447. var material = new THREE.RawShaderMaterial();
  8448. THREE.ShaderMaterial.prototype.clone.call( this, material );
  8449. return material;
  8450. };
  8451. // File:src/materials/SpriteMaterial.js
  8452. /**
  8453. * @author alteredq / http://alteredqualia.com/
  8454. *
  8455. * parameters = {
  8456. * color: <hex>,
  8457. * opacity: <float>,
  8458. * map: new THREE.Texture( <Image> ),
  8459. *
  8460. * blending: THREE.NormalBlending,
  8461. * depthTest: <bool>,
  8462. * depthWrite: <bool>,
  8463. *
  8464. * uvOffset: new THREE.Vector2(),
  8465. * uvScale: new THREE.Vector2(),
  8466. *
  8467. * fog: <bool>
  8468. * }
  8469. */
  8470. THREE.SpriteMaterial = function ( parameters ) {
  8471. THREE.Material.call( this );
  8472. this.type = 'SpriteMaterial';
  8473. this.color = new THREE.Color( 0xffffff );
  8474. this.map = null;
  8475. this.rotation = 0;
  8476. this.fog = false;
  8477. // set parameters
  8478. this.setValues( parameters );
  8479. };
  8480. THREE.SpriteMaterial.prototype = Object.create( THREE.Material.prototype );
  8481. THREE.SpriteMaterial.prototype.constructor = THREE.SpriteMaterial;
  8482. THREE.SpriteMaterial.prototype.clone = function () {
  8483. var material = new THREE.SpriteMaterial();
  8484. THREE.Material.prototype.clone.call( this, material );
  8485. material.color.copy( this.color );
  8486. material.map = this.map;
  8487. material.rotation = this.rotation;
  8488. material.fog = this.fog;
  8489. return material;
  8490. };
  8491. // File:src/textures/Texture.js
  8492. /**
  8493. * @author mrdoob / http://mrdoob.com/
  8494. * @author alteredq / http://alteredqualia.com/
  8495. * @author szimek / https://github.com/szimek/
  8496. */
  8497. THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
  8498. Object.defineProperty( this, 'id', { value: THREE.TextureIdCount ++ } );
  8499. this.uuid = THREE.Math.generateUUID();
  8500. this.name = '';
  8501. this.image = image !== undefined ? image : THREE.Texture.DEFAULT_IMAGE;
  8502. this.mipmaps = [];
  8503. this.mapping = mapping !== undefined ? mapping : THREE.Texture.DEFAULT_MAPPING;
  8504. this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
  8505. this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
  8506. this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
  8507. this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;
  8508. this.anisotropy = anisotropy !== undefined ? anisotropy : 1;
  8509. this.format = format !== undefined ? format : THREE.RGBAFormat;
  8510. this.type = type !== undefined ? type : THREE.UnsignedByteType;
  8511. this.offset = new THREE.Vector2( 0, 0 );
  8512. this.repeat = new THREE.Vector2( 1, 1 );
  8513. this.generateMipmaps = true;
  8514. this.premultiplyAlpha = false;
  8515. this.flipY = true;
  8516. this.unpackAlignment = 4; // valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml)
  8517. this._needsUpdate = false;
  8518. this.onUpdate = null;
  8519. };
  8520. THREE.Texture.DEFAULT_IMAGE = undefined;
  8521. THREE.Texture.DEFAULT_MAPPING = THREE.UVMapping;
  8522. THREE.Texture.prototype = {
  8523. constructor: THREE.Texture,
  8524. get needsUpdate () {
  8525. return this._needsUpdate;
  8526. },
  8527. set needsUpdate ( value ) {
  8528. if ( value === true ) this.update();
  8529. this._needsUpdate = value;
  8530. },
  8531. clone: function ( texture ) {
  8532. if ( texture === undefined ) texture = new THREE.Texture();
  8533. texture.image = this.image;
  8534. texture.mipmaps = this.mipmaps.slice( 0 );
  8535. texture.mapping = this.mapping;
  8536. texture.wrapS = this.wrapS;
  8537. texture.wrapT = this.wrapT;
  8538. texture.magFilter = this.magFilter;
  8539. texture.minFilter = this.minFilter;
  8540. texture.anisotropy = this.anisotropy;
  8541. texture.format = this.format;
  8542. texture.type = this.type;
  8543. texture.offset.copy( this.offset );
  8544. texture.repeat.copy( this.repeat );
  8545. texture.generateMipmaps = this.generateMipmaps;
  8546. texture.premultiplyAlpha = this.premultiplyAlpha;
  8547. texture.flipY = this.flipY;
  8548. texture.unpackAlignment = this.unpackAlignment;
  8549. return texture;
  8550. },
  8551. update: function () {
  8552. this.dispatchEvent( { type: 'update' } );
  8553. },
  8554. dispose: function () {
  8555. this.dispatchEvent( { type: 'dispose' } );
  8556. }
  8557. };
  8558. THREE.EventDispatcher.prototype.apply( THREE.Texture.prototype );
  8559. THREE.TextureIdCount = 0;
  8560. // File:src/textures/CubeTexture.js
  8561. /**
  8562. * @author mrdoob / http://mrdoob.com/
  8563. */
  8564. THREE.CubeTexture = function ( images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
  8565. mapping = mapping !== undefined ? mapping : THREE.CubeReflectionMapping;
  8566. THREE.Texture.call( this, images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
  8567. this.images = images;
  8568. };
  8569. THREE.CubeTexture.prototype = Object.create( THREE.Texture.prototype );
  8570. THREE.CubeTexture.prototype.constructor = THREE.CubeTexture;
  8571. THREE.CubeTexture.clone = function ( texture ) {
  8572. if ( texture === undefined ) texture = new THREE.CubeTexture();
  8573. THREE.Texture.prototype.clone.call( this, texture );
  8574. texture.images = this.images;
  8575. return texture;
  8576. };
  8577. // File:src/textures/CompressedTexture.js
  8578. /**
  8579. * @author alteredq / http://alteredqualia.com/
  8580. */
  8581. THREE.CompressedTexture = function ( mipmaps, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy ) {
  8582. THREE.Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
  8583. this.image = { width: width, height: height };
  8584. this.mipmaps = mipmaps;
  8585. // no flipping for cube textures
  8586. // (also flipping doesn't work for compressed textures )
  8587. this.flipY = false;
  8588. // can't generate mipmaps for compressed textures
  8589. // mips must be embedded in DDS files
  8590. this.generateMipmaps = false;
  8591. };
  8592. THREE.CompressedTexture.prototype = Object.create( THREE.Texture.prototype );
  8593. THREE.CompressedTexture.prototype.constructor = THREE.CompressedTexture;
  8594. THREE.CompressedTexture.prototype.clone = function () {
  8595. var texture = new THREE.CompressedTexture();
  8596. THREE.Texture.prototype.clone.call( this, texture );
  8597. return texture;
  8598. };
  8599. // File:src/textures/DataTexture.js
  8600. /**
  8601. * @author alteredq / http://alteredqualia.com/
  8602. */
  8603. THREE.DataTexture = function ( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy ) {
  8604. THREE.Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
  8605. this.image = { data: data, width: width, height: height };
  8606. };
  8607. THREE.DataTexture.prototype = Object.create( THREE.Texture.prototype );
  8608. THREE.DataTexture.prototype.constructor = THREE.DataTexture;
  8609. THREE.DataTexture.prototype.clone = function () {
  8610. var texture = new THREE.DataTexture();
  8611. THREE.Texture.prototype.clone.call( this, texture );
  8612. return texture;
  8613. };
  8614. // File:src/textures/VideoTexture.js
  8615. /**
  8616. * @author mrdoob / http://mrdoob.com/
  8617. */
  8618. THREE.VideoTexture = function ( video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) {
  8619. THREE.Texture.call( this, video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy );
  8620. this.generateMipmaps = false;
  8621. var scope = this;
  8622. var update = function () {
  8623. requestAnimationFrame( update );
  8624. if ( video.readyState === video.HAVE_ENOUGH_DATA ) {
  8625. scope.needsUpdate = true;
  8626. }
  8627. };
  8628. update();
  8629. };
  8630. THREE.VideoTexture.prototype = Object.create( THREE.Texture.prototype );
  8631. THREE.VideoTexture.prototype.constructor = THREE.VideoTexture;
  8632. // File:src/objects/Group.js
  8633. /**
  8634. * @author mrdoob / http://mrdoob.com/
  8635. */
  8636. THREE.Group = function () {
  8637. THREE.Object3D.call( this );
  8638. this.type = 'Group';
  8639. };
  8640. THREE.Group.prototype = Object.create( THREE.Object3D.prototype );
  8641. THREE.Group.prototype.constructor = THREE.Group;
  8642. // File:src/objects/PointCloud.js
  8643. /**
  8644. * @author alteredq / http://alteredqualia.com/
  8645. */
  8646. THREE.PointCloud = function ( geometry, material ) {
  8647. THREE.Object3D.call( this );
  8648. this.type = 'PointCloud';
  8649. this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
  8650. this.material = material !== undefined ? material : new THREE.PointCloudMaterial( { color: Math.random() * 0xffffff } );
  8651. };
  8652. THREE.PointCloud.prototype = Object.create( THREE.Object3D.prototype );
  8653. THREE.PointCloud.prototype.constructor = THREE.PointCloud;
  8654. THREE.PointCloud.prototype.raycast = ( function () {
  8655. var inverseMatrix = new THREE.Matrix4();
  8656. var ray = new THREE.Ray();
  8657. return function ( raycaster, intersects ) {
  8658. var object = this;
  8659. var geometry = object.geometry;
  8660. var threshold = raycaster.params.PointCloud.threshold;
  8661. inverseMatrix.getInverse( this.matrixWorld );
  8662. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  8663. if ( geometry.boundingBox !== null ) {
  8664. if ( ray.isIntersectionBox( geometry.boundingBox ) === false ) {
  8665. return;
  8666. }
  8667. }
  8668. var localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 );
  8669. var position = new THREE.Vector3();
  8670. var testPoint = function ( point, index ) {
  8671. var rayPointDistance = ray.distanceToPoint( point );
  8672. if ( rayPointDistance < localThreshold ) {
  8673. var intersectPoint = ray.closestPointToPoint( point );
  8674. intersectPoint.applyMatrix4( object.matrixWorld );
  8675. var distance = raycaster.ray.origin.distanceTo( intersectPoint );
  8676. intersects.push( {
  8677. distance: distance,
  8678. distanceToRay: rayPointDistance,
  8679. point: intersectPoint.clone(),
  8680. index: index,
  8681. face: null,
  8682. object: object
  8683. } );
  8684. }
  8685. };
  8686. if ( geometry instanceof THREE.BufferGeometry ) {
  8687. var attributes = geometry.attributes;
  8688. var positions = attributes.position.array;
  8689. if ( attributes.index !== undefined ) {
  8690. var indices = attributes.index.array;
  8691. var offsets = geometry.offsets;
  8692. if ( offsets.length === 0 ) {
  8693. var offset = {
  8694. start: 0,
  8695. count: indices.length,
  8696. index: 0
  8697. };
  8698. offsets = [ offset ];
  8699. }
  8700. for ( var oi = 0, ol = offsets.length; oi < ol; ++oi ) {
  8701. var start = offsets[ oi ].start;
  8702. var count = offsets[ oi ].count;
  8703. var index = offsets[ oi ].index;
  8704. for ( var i = start, il = start + count; i < il; i ++ ) {
  8705. var a = index + indices[ i ];
  8706. position.fromArray( positions, a * 3 );
  8707. testPoint( position, a );
  8708. }
  8709. }
  8710. } else {
  8711. var pointCount = positions.length / 3;
  8712. for ( var i = 0; i < pointCount; i ++ ) {
  8713. position.set(
  8714. positions[ 3 * i ],
  8715. positions[ 3 * i + 1 ],
  8716. positions[ 3 * i + 2 ]
  8717. );
  8718. testPoint( position, i );
  8719. }
  8720. }
  8721. } else {
  8722. var vertices = this.geometry.vertices;
  8723. for ( var i = 0; i < vertices.length; i ++ ) {
  8724. testPoint( vertices[ i ], i );
  8725. }
  8726. }
  8727. };
  8728. }() );
  8729. THREE.PointCloud.prototype.clone = function ( object ) {
  8730. if ( object === undefined ) object = new THREE.PointCloud( this.geometry, this.material );
  8731. THREE.Object3D.prototype.clone.call( this, object );
  8732. return object;
  8733. };
  8734. // Backwards compatibility
  8735. THREE.ParticleSystem = function ( geometry, material ) {
  8736. console.warn( 'THREE.ParticleSystem has been renamed to THREE.PointCloud.' );
  8737. return new THREE.PointCloud( geometry, material );
  8738. };
  8739. // File:src/objects/Line.js
  8740. /**
  8741. * @author mrdoob / http://mrdoob.com/
  8742. */
  8743. THREE.Line = function ( geometry, material, mode ) {
  8744. THREE.Object3D.call( this );
  8745. this.type = 'Line';
  8746. this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
  8747. this.material = material !== undefined ? material : new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff } );
  8748. this.mode = ( mode !== undefined ) ? mode : THREE.LineStrip;
  8749. };
  8750. THREE.LineStrip = 0;
  8751. THREE.LinePieces = 1;
  8752. THREE.Line.prototype = Object.create( THREE.Object3D.prototype );
  8753. THREE.Line.prototype.constructor = THREE.Line;
  8754. THREE.Line.prototype.raycast = ( function () {
  8755. var inverseMatrix = new THREE.Matrix4();
  8756. var ray = new THREE.Ray();
  8757. var sphere = new THREE.Sphere();
  8758. return function ( raycaster, intersects ) {
  8759. var precision = raycaster.linePrecision;
  8760. var precisionSq = precision * precision;
  8761. var geometry = this.geometry;
  8762. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  8763. // Checking boundingSphere distance to ray
  8764. sphere.copy( geometry.boundingSphere );
  8765. sphere.applyMatrix4( this.matrixWorld );
  8766. if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
  8767. return;
  8768. }
  8769. inverseMatrix.getInverse( this.matrixWorld );
  8770. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  8771. /* if ( geometry instanceof THREE.BufferGeometry ) {
  8772. } else */ if ( geometry instanceof THREE.Geometry ) {
  8773. var vertices = geometry.vertices;
  8774. var nbVertices = vertices.length;
  8775. var interSegment = new THREE.Vector3();
  8776. var interRay = new THREE.Vector3();
  8777. var step = this.mode === THREE.LineStrip ? 1 : 2;
  8778. for ( var i = 0; i < nbVertices - 1; i = i + step ) {
  8779. var distSq = ray.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment );
  8780. if ( distSq > precisionSq ) continue;
  8781. var distance = ray.origin.distanceTo( interRay );
  8782. if ( distance < raycaster.near || distance > raycaster.far ) continue;
  8783. intersects.push( {
  8784. distance: distance,
  8785. // What do we want? intersection point on the ray or on the segment??
  8786. // point: raycaster.ray.at( distance ),
  8787. point: interSegment.clone().applyMatrix4( this.matrixWorld ),
  8788. face: null,
  8789. faceIndex: null,
  8790. object: this
  8791. } );
  8792. }
  8793. }
  8794. };
  8795. }() );
  8796. THREE.Line.prototype.clone = function ( object ) {
  8797. if ( object === undefined ) object = new THREE.Line( this.geometry, this.material, this.mode );
  8798. THREE.Object3D.prototype.clone.call( this, object );
  8799. return object;
  8800. };
  8801. // File:src/objects/Mesh.js
  8802. /**
  8803. * @author mrdoob / http://mrdoob.com/
  8804. * @author alteredq / http://alteredqualia.com/
  8805. * @author mikael emtinger / http://gomo.se/
  8806. * @author jonobr1 / http://jonobr1.com/
  8807. */
  8808. THREE.Mesh = function ( geometry, material ) {
  8809. THREE.Object3D.call( this );
  8810. this.type = 'Mesh';
  8811. this.geometry = geometry !== undefined ? geometry : new THREE.Geometry();
  8812. this.material = material !== undefined ? material : new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff } );
  8813. this.updateMorphTargets();
  8814. };
  8815. THREE.Mesh.prototype = Object.create( THREE.Object3D.prototype );
  8816. THREE.Mesh.prototype.constructor = THREE.Mesh;
  8817. THREE.Mesh.prototype.updateMorphTargets = function () {
  8818. if ( this.geometry.morphTargets !== undefined && this.geometry.morphTargets.length > 0 ) {
  8819. this.morphTargetBase = - 1;
  8820. this.morphTargetForcedOrder = [];
  8821. this.morphTargetInfluences = [];
  8822. this.morphTargetDictionary = {};
  8823. for ( var m = 0, ml = this.geometry.morphTargets.length; m < ml; m ++ ) {
  8824. this.morphTargetInfluences.push( 0 );
  8825. this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m;
  8826. }
  8827. }
  8828. };
  8829. THREE.Mesh.prototype.getMorphTargetIndexByName = function ( name ) {
  8830. if ( this.morphTargetDictionary[ name ] !== undefined ) {
  8831. return this.morphTargetDictionary[ name ];
  8832. }
  8833. console.log( 'THREE.Mesh.getMorphTargetIndexByName: morph target ' + name + ' does not exist. Returning 0.' );
  8834. return 0;
  8835. };
  8836. THREE.Mesh.prototype.raycast = ( function () {
  8837. var inverseMatrix = new THREE.Matrix4();
  8838. var ray = new THREE.Ray();
  8839. var sphere = new THREE.Sphere();
  8840. var vA = new THREE.Vector3();
  8841. var vB = new THREE.Vector3();
  8842. var vC = new THREE.Vector3();
  8843. return function ( raycaster, intersects ) {
  8844. var geometry = this.geometry;
  8845. // Checking boundingSphere distance to ray
  8846. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  8847. sphere.copy( geometry.boundingSphere );
  8848. sphere.applyMatrix4( this.matrixWorld );
  8849. if ( raycaster.ray.isIntersectionSphere( sphere ) === false ) {
  8850. return;
  8851. }
  8852. // Check boundingBox before continuing
  8853. inverseMatrix.getInverse( this.matrixWorld );
  8854. ray.copy( raycaster.ray ).applyMatrix4( inverseMatrix );
  8855. if ( geometry.boundingBox !== null ) {
  8856. if ( ray.isIntersectionBox( geometry.boundingBox ) === false ) {
  8857. return;
  8858. }
  8859. }
  8860. if ( geometry instanceof THREE.BufferGeometry ) {
  8861. var material = this.material;
  8862. if ( material === undefined ) return;
  8863. var attributes = geometry.attributes;
  8864. var a, b, c;
  8865. var precision = raycaster.precision;
  8866. if ( attributes.index !== undefined ) {
  8867. var indices = attributes.index.array;
  8868. var positions = attributes.position.array;
  8869. var offsets = geometry.offsets;
  8870. if ( offsets.length === 0 ) {
  8871. offsets = [ { start: 0, count: indices.length, index: 0 } ];
  8872. }
  8873. for ( var oi = 0, ol = offsets.length; oi < ol; ++oi ) {
  8874. var start = offsets[ oi ].start;
  8875. var count = offsets[ oi ].count;
  8876. var index = offsets[ oi ].index;
  8877. for ( var i = start, il = start + count; i < il; i += 3 ) {
  8878. a = index + indices[ i ];
  8879. b = index + indices[ i + 1 ];
  8880. c = index + indices[ i + 2 ];
  8881. vA.fromArray( positions, a * 3 );
  8882. vB.fromArray( positions, b * 3 );
  8883. vC.fromArray( positions, c * 3 );
  8884. if ( material.side === THREE.BackSide ) {
  8885. var intersectionPoint = ray.intersectTriangle( vC, vB, vA, true );
  8886. } else {
  8887. var intersectionPoint = ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
  8888. }
  8889. if ( intersectionPoint === null ) continue;
  8890. intersectionPoint.applyMatrix4( this.matrixWorld );
  8891. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  8892. if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue;
  8893. intersects.push( {
  8894. distance: distance,
  8895. point: intersectionPoint,
  8896. face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
  8897. faceIndex: null,
  8898. object: this
  8899. } );
  8900. }
  8901. }
  8902. } else {
  8903. var positions = attributes.position.array;
  8904. for ( var i = 0, j = 0, il = positions.length; i < il; i += 3, j += 9 ) {
  8905. a = i;
  8906. b = i + 1;
  8907. c = i + 2;
  8908. vA.fromArray( positions, j );
  8909. vB.fromArray( positions, j + 3 );
  8910. vC.fromArray( positions, j + 6 );
  8911. if ( material.side === THREE.BackSide ) {
  8912. var intersectionPoint = ray.intersectTriangle( vC, vB, vA, true );
  8913. } else {
  8914. var intersectionPoint = ray.intersectTriangle( vA, vB, vC, material.side !== THREE.DoubleSide );
  8915. }
  8916. if ( intersectionPoint === null ) continue;
  8917. intersectionPoint.applyMatrix4( this.matrixWorld );
  8918. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  8919. if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue;
  8920. intersects.push( {
  8921. distance: distance,
  8922. point: intersectionPoint,
  8923. face: new THREE.Face3( a, b, c, THREE.Triangle.normal( vA, vB, vC ) ),
  8924. faceIndex: null,
  8925. object: this
  8926. } );
  8927. }
  8928. }
  8929. } else if ( geometry instanceof THREE.Geometry ) {
  8930. var isFaceMaterial = this.material instanceof THREE.MeshFaceMaterial;
  8931. var objectMaterials = isFaceMaterial === true ? this.material.materials : null;
  8932. var a, b, c, d;
  8933. var precision = raycaster.precision;
  8934. var vertices = geometry.vertices;
  8935. for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  8936. var face = geometry.faces[ f ];
  8937. var material = isFaceMaterial === true ? objectMaterials[ face.materialIndex ] : this.material;
  8938. if ( material === undefined ) continue;
  8939. a = vertices[ face.a ];
  8940. b = vertices[ face.b ];
  8941. c = vertices[ face.c ];
  8942. if ( material.morphTargets === true ) {
  8943. var morphTargets = geometry.morphTargets;
  8944. var morphInfluences = this.morphTargetInfluences;
  8945. vA.set( 0, 0, 0 );
  8946. vB.set( 0, 0, 0 );
  8947. vC.set( 0, 0, 0 );
  8948. for ( var t = 0, tl = morphTargets.length; t < tl; t ++ ) {
  8949. var influence = morphInfluences[ t ];
  8950. if ( influence === 0 ) continue;
  8951. var targets = morphTargets[ t ].vertices;
  8952. vA.x += ( targets[ face.a ].x - a.x ) * influence;
  8953. vA.y += ( targets[ face.a ].y - a.y ) * influence;
  8954. vA.z += ( targets[ face.a ].z - a.z ) * influence;
  8955. vB.x += ( targets[ face.b ].x - b.x ) * influence;
  8956. vB.y += ( targets[ face.b ].y - b.y ) * influence;
  8957. vB.z += ( targets[ face.b ].z - b.z ) * influence;
  8958. vC.x += ( targets[ face.c ].x - c.x ) * influence;
  8959. vC.y += ( targets[ face.c ].y - c.y ) * influence;
  8960. vC.z += ( targets[ face.c ].z - c.z ) * influence;
  8961. }
  8962. vA.add( a );
  8963. vB.add( b );
  8964. vC.add( c );
  8965. a = vA;
  8966. b = vB;
  8967. c = vC;
  8968. }
  8969. if ( material.side === THREE.BackSide ) {
  8970. var intersectionPoint = ray.intersectTriangle( c, b, a, true );
  8971. } else {
  8972. var intersectionPoint = ray.intersectTriangle( a, b, c, material.side !== THREE.DoubleSide );
  8973. }
  8974. if ( intersectionPoint === null ) continue;
  8975. intersectionPoint.applyMatrix4( this.matrixWorld );
  8976. var distance = raycaster.ray.origin.distanceTo( intersectionPoint );
  8977. if ( distance < precision || distance < raycaster.near || distance > raycaster.far ) continue;
  8978. intersects.push( {
  8979. distance: distance,
  8980. point: intersectionPoint,
  8981. face: face,
  8982. faceIndex: f,
  8983. object: this
  8984. } );
  8985. }
  8986. }
  8987. };
  8988. }() );
  8989. THREE.Mesh.prototype.clone = function ( object, recursive ) {
  8990. if ( object === undefined ) object = new THREE.Mesh( this.geometry, this.material );
  8991. THREE.Object3D.prototype.clone.call( this, object, recursive );
  8992. return object;
  8993. };
  8994. // File:src/objects/Bone.js
  8995. /**
  8996. * @author mikael emtinger / http://gomo.se/
  8997. * @author alteredq / http://alteredqualia.com/
  8998. * @author ikerr / http://verold.com
  8999. */
  9000. THREE.Bone = function ( belongsToSkin ) {
  9001. THREE.Object3D.call( this );
  9002. this.skin = belongsToSkin;
  9003. };
  9004. THREE.Bone.prototype = Object.create( THREE.Object3D.prototype );
  9005. THREE.Bone.prototype.constructor = THREE.Bone;
  9006. // File:src/objects/Skeleton.js
  9007. /**
  9008. * @author mikael emtinger / http://gomo.se/
  9009. * @author alteredq / http://alteredqualia.com/
  9010. * @author michael guerrero / http://realitymeltdown.com
  9011. * @author ikerr / http://verold.com
  9012. */
  9013. THREE.Skeleton = function ( bones, boneInverses, useVertexTexture ) {
  9014. this.useVertexTexture = useVertexTexture !== undefined ? useVertexTexture : true;
  9015. this.identityMatrix = new THREE.Matrix4();
  9016. // copy the bone array
  9017. bones = bones || [];
  9018. this.bones = bones.slice( 0 );
  9019. // create a bone texture or an array of floats
  9020. if ( this.useVertexTexture ) {
  9021. // layout (1 matrix = 4 pixels)
  9022. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  9023. // with 8x8 pixel texture max 16 bones (8 * 8 / 4)
  9024. // 16x16 pixel texture max 64 bones (16 * 16 / 4)
  9025. // 32x32 pixel texture max 256 bones (32 * 32 / 4)
  9026. // 64x64 pixel texture max 1024 bones (64 * 64 / 4)
  9027. var size;
  9028. if ( this.bones.length > 256 )
  9029. size = 64;
  9030. else if ( this.bones.length > 64 )
  9031. size = 32;
  9032. else if ( this.bones.length > 16 )
  9033. size = 16;
  9034. else
  9035. size = 8;
  9036. this.boneTextureWidth = size;
  9037. this.boneTextureHeight = size;
  9038. this.boneMatrices = new Float32Array( this.boneTextureWidth * this.boneTextureHeight * 4 ); // 4 floats per RGBA pixel
  9039. this.boneTexture = new THREE.DataTexture( this.boneMatrices, this.boneTextureWidth, this.boneTextureHeight, THREE.RGBAFormat, THREE.FloatType );
  9040. this.boneTexture.minFilter = THREE.NearestFilter;
  9041. this.boneTexture.magFilter = THREE.NearestFilter;
  9042. this.boneTexture.generateMipmaps = false;
  9043. this.boneTexture.flipY = false;
  9044. } else {
  9045. this.boneMatrices = new Float32Array( 16 * this.bones.length );
  9046. }
  9047. // use the supplied bone inverses or calculate the inverses
  9048. if ( boneInverses === undefined ) {
  9049. this.calculateInverses();
  9050. } else {
  9051. if ( this.bones.length === boneInverses.length ) {
  9052. this.boneInverses = boneInverses.slice( 0 );
  9053. } else {
  9054. console.warn( 'THREE.Skeleton bonInverses is the wrong length.' );
  9055. this.boneInverses = [];
  9056. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  9057. this.boneInverses.push( new THREE.Matrix4() );
  9058. }
  9059. }
  9060. }
  9061. };
  9062. THREE.Skeleton.prototype.calculateInverses = function () {
  9063. this.boneInverses = [];
  9064. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  9065. var inverse = new THREE.Matrix4();
  9066. if ( this.bones[ b ] ) {
  9067. inverse.getInverse( this.bones[ b ].matrixWorld );
  9068. }
  9069. this.boneInverses.push( inverse );
  9070. }
  9071. };
  9072. THREE.Skeleton.prototype.pose = function () {
  9073. var bone;
  9074. // recover the bind-time world matrices
  9075. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  9076. bone = this.bones[ b ];
  9077. if ( bone ) {
  9078. bone.matrixWorld.getInverse( this.boneInverses[ b ] );
  9079. }
  9080. }
  9081. // compute the local matrices, positions, rotations and scales
  9082. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  9083. bone = this.bones[ b ];
  9084. if ( bone ) {
  9085. if ( bone.parent ) {
  9086. bone.matrix.getInverse( bone.parent.matrixWorld );
  9087. bone.matrix.multiply( bone.matrixWorld );
  9088. } else {
  9089. bone.matrix.copy( bone.matrixWorld );
  9090. }
  9091. bone.matrix.decompose( bone.position, bone.quaternion, bone.scale );
  9092. }
  9093. }
  9094. };
  9095. THREE.Skeleton.prototype.update = ( function () {
  9096. var offsetMatrix = new THREE.Matrix4();
  9097. return function () {
  9098. // flatten bone matrices to array
  9099. for ( var b = 0, bl = this.bones.length; b < bl; b ++ ) {
  9100. // compute the offset between the current and the original transform
  9101. var matrix = this.bones[ b ] ? this.bones[ b ].matrixWorld : this.identityMatrix;
  9102. offsetMatrix.multiplyMatrices( matrix, this.boneInverses[ b ] );
  9103. offsetMatrix.flattenToArrayOffset( this.boneMatrices, b * 16 );
  9104. }
  9105. if ( this.useVertexTexture ) {
  9106. this.boneTexture.needsUpdate = true;
  9107. }
  9108. };
  9109. } )();
  9110. // File:src/objects/SkinnedMesh.js
  9111. /**
  9112. * @author mikael emtinger / http://gomo.se/
  9113. * @author alteredq / http://alteredqualia.com/
  9114. * @author ikerr / http://verold.com
  9115. */
  9116. THREE.SkinnedMesh = function ( geometry, material, useVertexTexture ) {
  9117. THREE.Mesh.call( this, geometry, material );
  9118. this.type = 'SkinnedMesh';
  9119. this.bindMode = "attached";
  9120. this.bindMatrix = new THREE.Matrix4();
  9121. this.bindMatrixInverse = new THREE.Matrix4();
  9122. // init bones
  9123. // TODO: remove bone creation as there is no reason (other than
  9124. // convenience) for THREE.SkinnedMesh to do this.
  9125. var bones = [];
  9126. if ( this.geometry && this.geometry.bones !== undefined ) {
  9127. var bone, gbone, p, q, s;
  9128. for ( var b = 0, bl = this.geometry.bones.length; b < bl; ++b ) {
  9129. gbone = this.geometry.bones[ b ];
  9130. p = gbone.pos;
  9131. q = gbone.rotq;
  9132. s = gbone.scl;
  9133. bone = new THREE.Bone( this );
  9134. bones.push( bone );
  9135. bone.name = gbone.name;
  9136. bone.position.set( p[ 0 ], p[ 1 ], p[ 2 ] );
  9137. bone.quaternion.set( q[ 0 ], q[ 1 ], q[ 2 ], q[ 3 ] );
  9138. if ( s !== undefined ) {
  9139. bone.scale.set( s[ 0 ], s[ 1 ], s[ 2 ] );
  9140. } else {
  9141. bone.scale.set( 1, 1, 1 );
  9142. }
  9143. }
  9144. for ( var b = 0, bl = this.geometry.bones.length; b < bl; ++b ) {
  9145. gbone = this.geometry.bones[ b ];
  9146. if ( gbone.parent !== - 1 ) {
  9147. bones[ gbone.parent ].add( bones[ b ] );
  9148. } else {
  9149. this.add( bones[ b ] );
  9150. }
  9151. }
  9152. }
  9153. this.normalizeSkinWeights();
  9154. this.updateMatrixWorld( true );
  9155. this.bind( new THREE.Skeleton( bones, undefined, useVertexTexture ) );
  9156. };
  9157. THREE.SkinnedMesh.prototype = Object.create( THREE.Mesh.prototype );
  9158. THREE.SkinnedMesh.prototype.constructor = THREE.SkinnedMesh;
  9159. THREE.SkinnedMesh.prototype.bind = function( skeleton, bindMatrix ) {
  9160. this.skeleton = skeleton;
  9161. if ( bindMatrix === undefined ) {
  9162. this.updateMatrixWorld( true );
  9163. bindMatrix = this.matrixWorld;
  9164. }
  9165. this.bindMatrix.copy( bindMatrix );
  9166. this.bindMatrixInverse.getInverse( bindMatrix );
  9167. };
  9168. THREE.SkinnedMesh.prototype.pose = function () {
  9169. this.skeleton.pose();
  9170. };
  9171. THREE.SkinnedMesh.prototype.normalizeSkinWeights = function () {
  9172. if ( this.geometry instanceof THREE.Geometry ) {
  9173. for ( var i = 0; i < this.geometry.skinIndices.length; i ++ ) {
  9174. var sw = this.geometry.skinWeights[ i ];
  9175. var scale = 1.0 / sw.lengthManhattan();
  9176. if ( scale !== Infinity ) {
  9177. sw.multiplyScalar( scale );
  9178. } else {
  9179. sw.set( 1 ); // this will be normalized by the shader anyway
  9180. }
  9181. }
  9182. } else {
  9183. // skinning weights assumed to be normalized for THREE.BufferGeometry
  9184. }
  9185. };
  9186. THREE.SkinnedMesh.prototype.updateMatrixWorld = function( force ) {
  9187. THREE.Mesh.prototype.updateMatrixWorld.call( this, true );
  9188. if ( this.bindMode === "attached" ) {
  9189. this.bindMatrixInverse.getInverse( this.matrixWorld );
  9190. } else if ( this.bindMode === "detached" ) {
  9191. this.bindMatrixInverse.getInverse( this.bindMatrix );
  9192. } else {
  9193. console.warn( 'THREE.SkinnedMesh unreckognized bindMode: ' + this.bindMode );
  9194. }
  9195. };
  9196. THREE.SkinnedMesh.prototype.clone = function( object ) {
  9197. if ( object === undefined ) {
  9198. object = new THREE.SkinnedMesh( this.geometry, this.material, this.useVertexTexture );
  9199. }
  9200. THREE.Mesh.prototype.clone.call( this, object );
  9201. return object;
  9202. };
  9203. // File:src/objects/MorphAnimMesh.js
  9204. /**
  9205. * @author alteredq / http://alteredqualia.com/
  9206. */
  9207. THREE.MorphAnimMesh = function ( geometry, material ) {
  9208. THREE.Mesh.call( this, geometry, material );
  9209. this.type = 'MorphAnimMesh';
  9210. // API
  9211. this.duration = 1000; // milliseconds
  9212. this.mirroredLoop = false;
  9213. this.time = 0;
  9214. // internals
  9215. this.lastKeyframe = 0;
  9216. this.currentKeyframe = 0;
  9217. this.direction = 1;
  9218. this.directionBackwards = false;
  9219. this.setFrameRange( 0, this.geometry.morphTargets.length - 1 );
  9220. };
  9221. THREE.MorphAnimMesh.prototype = Object.create( THREE.Mesh.prototype );
  9222. THREE.MorphAnimMesh.prototype.constructor = THREE.MorphAnimMesh;
  9223. THREE.MorphAnimMesh.prototype.setFrameRange = function ( start, end ) {
  9224. this.startKeyframe = start;
  9225. this.endKeyframe = end;
  9226. this.length = this.endKeyframe - this.startKeyframe + 1;
  9227. };
  9228. THREE.MorphAnimMesh.prototype.setDirectionForward = function () {
  9229. this.direction = 1;
  9230. this.directionBackwards = false;
  9231. };
  9232. THREE.MorphAnimMesh.prototype.setDirectionBackward = function () {
  9233. this.direction = - 1;
  9234. this.directionBackwards = true;
  9235. };
  9236. THREE.MorphAnimMesh.prototype.parseAnimations = function () {
  9237. var geometry = this.geometry;
  9238. if ( ! geometry.animations ) geometry.animations = {};
  9239. var firstAnimation, animations = geometry.animations;
  9240. var pattern = /([a-z]+)_?(\d+)/;
  9241. for ( var i = 0, il = geometry.morphTargets.length; i < il; i ++ ) {
  9242. var morph = geometry.morphTargets[ i ];
  9243. var parts = morph.name.match( pattern );
  9244. if ( parts && parts.length > 1 ) {
  9245. var label = parts[ 1 ];
  9246. var num = parts[ 2 ];
  9247. if ( ! animations[ label ] ) animations[ label ] = { start: Infinity, end: - Infinity };
  9248. var animation = animations[ label ];
  9249. if ( i < animation.start ) animation.start = i;
  9250. if ( i > animation.end ) animation.end = i;
  9251. if ( ! firstAnimation ) firstAnimation = label;
  9252. }
  9253. }
  9254. geometry.firstAnimation = firstAnimation;
  9255. };
  9256. THREE.MorphAnimMesh.prototype.setAnimationLabel = function ( label, start, end ) {
  9257. if ( ! this.geometry.animations ) this.geometry.animations = {};
  9258. this.geometry.animations[ label ] = { start: start, end: end };
  9259. };
  9260. THREE.MorphAnimMesh.prototype.playAnimation = function ( label, fps ) {
  9261. var animation = this.geometry.animations[ label ];
  9262. if ( animation ) {
  9263. this.setFrameRange( animation.start, animation.end );
  9264. this.duration = 1000 * ( ( animation.end - animation.start ) / fps );
  9265. this.time = 0;
  9266. } else {
  9267. console.warn( 'animation[' + label + '] undefined' );
  9268. }
  9269. };
  9270. THREE.MorphAnimMesh.prototype.updateAnimation = function ( delta ) {
  9271. var frameTime = this.duration / this.length;
  9272. this.time += this.direction * delta;
  9273. if ( this.mirroredLoop ) {
  9274. if ( this.time > this.duration || this.time < 0 ) {
  9275. this.direction *= - 1;
  9276. if ( this.time > this.duration ) {
  9277. this.time = this.duration;
  9278. this.directionBackwards = true;
  9279. }
  9280. if ( this.time < 0 ) {
  9281. this.time = 0;
  9282. this.directionBackwards = false;
  9283. }
  9284. }
  9285. } else {
  9286. this.time = this.time % this.duration;
  9287. if ( this.time < 0 ) this.time += this.duration;
  9288. }
  9289. var keyframe = this.startKeyframe + THREE.Math.clamp( Math.floor( this.time / frameTime ), 0, this.length - 1 );
  9290. if ( keyframe !== this.currentKeyframe ) {
  9291. this.morphTargetInfluences[ this.lastKeyframe ] = 0;
  9292. this.morphTargetInfluences[ this.currentKeyframe ] = 1;
  9293. this.morphTargetInfluences[ keyframe ] = 0;
  9294. this.lastKeyframe = this.currentKeyframe;
  9295. this.currentKeyframe = keyframe;
  9296. }
  9297. var mix = ( this.time % frameTime ) / frameTime;
  9298. if ( this.directionBackwards ) {
  9299. mix = 1 - mix;
  9300. }
  9301. this.morphTargetInfluences[ this.currentKeyframe ] = mix;
  9302. this.morphTargetInfluences[ this.lastKeyframe ] = 1 - mix;
  9303. };
  9304. THREE.MorphAnimMesh.prototype.interpolateTargets = function ( a, b, t ) {
  9305. var influences = this.morphTargetInfluences;
  9306. for ( var i = 0, l = influences.length; i < l; i ++ ) {
  9307. influences[ i ] = 0;
  9308. }
  9309. if ( a > -1 ) influences[ a ] = 1 - t;
  9310. if ( b > -1 ) influences[ b ] = t;
  9311. };
  9312. THREE.MorphAnimMesh.prototype.clone = function ( object ) {
  9313. if ( object === undefined ) object = new THREE.MorphAnimMesh( this.geometry, this.material );
  9314. object.duration = this.duration;
  9315. object.mirroredLoop = this.mirroredLoop;
  9316. object.time = this.time;
  9317. object.lastKeyframe = this.lastKeyframe;
  9318. object.currentKeyframe = this.currentKeyframe;
  9319. object.direction = this.direction;
  9320. object.directionBackwards = this.directionBackwards;
  9321. THREE.Mesh.prototype.clone.call( this, object );
  9322. return object;
  9323. };
  9324. // File:src/objects/LOD.js
  9325. /**
  9326. * @author mikael emtinger / http://gomo.se/
  9327. * @author alteredq / http://alteredqualia.com/
  9328. * @author mrdoob / http://mrdoob.com/
  9329. */
  9330. THREE.LOD = function () {
  9331. THREE.Object3D.call( this );
  9332. this.objects = [];
  9333. };
  9334. THREE.LOD.prototype = Object.create( THREE.Object3D.prototype );
  9335. THREE.LOD.prototype.constructor = THREE.LOD;
  9336. THREE.LOD.prototype.addLevel = function ( object, distance ) {
  9337. if ( distance === undefined ) distance = 0;
  9338. distance = Math.abs( distance );
  9339. for ( var l = 0; l < this.objects.length; l ++ ) {
  9340. if ( distance < this.objects[ l ].distance ) {
  9341. break;
  9342. }
  9343. }
  9344. this.objects.splice( l, 0, { distance: distance, object: object } );
  9345. this.add( object );
  9346. };
  9347. THREE.LOD.prototype.getObjectForDistance = function ( distance ) {
  9348. for ( var i = 1, l = this.objects.length; i < l; i ++ ) {
  9349. if ( distance < this.objects[ i ].distance ) {
  9350. break;
  9351. }
  9352. }
  9353. return this.objects[ i - 1 ].object;
  9354. };
  9355. THREE.LOD.prototype.raycast = ( function () {
  9356. var matrixPosition = new THREE.Vector3();
  9357. return function ( raycaster, intersects ) {
  9358. matrixPosition.setFromMatrixPosition( this.matrixWorld );
  9359. var distance = raycaster.ray.origin.distanceTo( matrixPosition );
  9360. this.getObjectForDistance( distance ).raycast( raycaster, intersects );
  9361. };
  9362. }() );
  9363. THREE.LOD.prototype.update = function () {
  9364. var v1 = new THREE.Vector3();
  9365. var v2 = new THREE.Vector3();
  9366. return function ( camera ) {
  9367. if ( this.objects.length > 1 ) {
  9368. v1.setFromMatrixPosition( camera.matrixWorld );
  9369. v2.setFromMatrixPosition( this.matrixWorld );
  9370. var distance = v1.distanceTo( v2 );
  9371. this.objects[ 0 ].object.visible = true;
  9372. for ( var i = 1, l = this.objects.length; i < l; i ++ ) {
  9373. if ( distance >= this.objects[ i ].distance ) {
  9374. this.objects[ i - 1 ].object.visible = false;
  9375. this.objects[ i ].object.visible = true;
  9376. } else {
  9377. break;
  9378. }
  9379. }
  9380. for ( ; i < l; i ++ ) {
  9381. this.objects[ i ].object.visible = false;
  9382. }
  9383. }
  9384. };
  9385. }();
  9386. THREE.LOD.prototype.clone = function ( object ) {
  9387. if ( object === undefined ) object = new THREE.LOD();
  9388. THREE.Object3D.prototype.clone.call( this, object );
  9389. for ( var i = 0, l = this.objects.length; i < l; i ++ ) {
  9390. var x = this.objects[ i ].object.clone();
  9391. x.visible = i === 0;
  9392. object.addLevel( x, this.objects[ i ].distance );
  9393. }
  9394. return object;
  9395. };
  9396. // File:src/objects/Sprite.js
  9397. /**
  9398. * @author mikael emtinger / http://gomo.se/
  9399. * @author alteredq / http://alteredqualia.com/
  9400. */
  9401. THREE.Sprite = ( function () {
  9402. var indices = new Uint16Array( [ 0, 1, 2, 0, 2, 3 ] );
  9403. var vertices = new Float32Array( [ - 0.5, - 0.5, 0, 0.5, - 0.5, 0, 0.5, 0.5, 0, - 0.5, 0.5, 0 ] );
  9404. var uvs = new Float32Array( [ 0, 0, 1, 0, 1, 1, 0, 1 ] );
  9405. var geometry = new THREE.BufferGeometry();
  9406. geometry.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
  9407. geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  9408. geometry.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
  9409. return function ( material ) {
  9410. THREE.Object3D.call( this );
  9411. this.type = 'Sprite';
  9412. this.geometry = geometry;
  9413. this.material = ( material !== undefined ) ? material : new THREE.SpriteMaterial();
  9414. };
  9415. } )();
  9416. THREE.Sprite.prototype = Object.create( THREE.Object3D.prototype );
  9417. THREE.Sprite.prototype.constructor = THREE.Sprite;
  9418. THREE.Sprite.prototype.raycast = ( function () {
  9419. var matrixPosition = new THREE.Vector3();
  9420. return function ( raycaster, intersects ) {
  9421. matrixPosition.setFromMatrixPosition( this.matrixWorld );
  9422. var distance = raycaster.ray.distanceToPoint( matrixPosition );
  9423. if ( distance > this.scale.x ) {
  9424. return;
  9425. }
  9426. intersects.push( {
  9427. distance: distance,
  9428. point: this.position,
  9429. face: null,
  9430. object: this
  9431. } );
  9432. };
  9433. }() );
  9434. THREE.Sprite.prototype.clone = function ( object ) {
  9435. if ( object === undefined ) object = new THREE.Sprite( this.material );
  9436. THREE.Object3D.prototype.clone.call( this, object );
  9437. return object;
  9438. };
  9439. // Backwards compatibility
  9440. THREE.Particle = THREE.Sprite;
  9441. // File:src/objects/LensFlare.js
  9442. /**
  9443. * @author mikael emtinger / http://gomo.se/
  9444. * @author alteredq / http://alteredqualia.com/
  9445. */
  9446. THREE.LensFlare = function ( texture, size, distance, blending, color ) {
  9447. THREE.Object3D.call( this );
  9448. this.lensFlares = [];
  9449. this.positionScreen = new THREE.Vector3();
  9450. this.customUpdateCallback = undefined;
  9451. if( texture !== undefined ) {
  9452. this.add( texture, size, distance, blending, color );
  9453. }
  9454. };
  9455. THREE.LensFlare.prototype = Object.create( THREE.Object3D.prototype );
  9456. THREE.LensFlare.prototype.constructor = THREE.LensFlare;
  9457. /*
  9458. * Add: adds another flare
  9459. */
  9460. THREE.LensFlare.prototype.add = function ( texture, size, distance, blending, color, opacity ) {
  9461. if ( size === undefined ) size = - 1;
  9462. if ( distance === undefined ) distance = 0;
  9463. if ( opacity === undefined ) opacity = 1;
  9464. if ( color === undefined ) color = new THREE.Color( 0xffffff );
  9465. if ( blending === undefined ) blending = THREE.NormalBlending;
  9466. distance = Math.min( distance, Math.max( 0, distance ) );
  9467. this.lensFlares.push( {
  9468. texture: texture, // THREE.Texture
  9469. size: size, // size in pixels (-1 = use texture.width)
  9470. distance: distance, // distance (0-1) from light source (0=at light source)
  9471. x: 0, y: 0, z: 0, // screen position (-1 => 1) z = 0 is ontop z = 1 is back
  9472. scale: 1, // scale
  9473. rotation: 1, // rotation
  9474. opacity: opacity, // opacity
  9475. color: color, // color
  9476. blending: blending // blending
  9477. } );
  9478. };
  9479. /*
  9480. * Update lens flares update positions on all flares based on the screen position
  9481. * Set myLensFlare.customUpdateCallback to alter the flares in your project specific way.
  9482. */
  9483. THREE.LensFlare.prototype.updateLensFlares = function () {
  9484. var f, fl = this.lensFlares.length;
  9485. var flare;
  9486. var vecX = - this.positionScreen.x * 2;
  9487. var vecY = - this.positionScreen.y * 2;
  9488. for( f = 0; f < fl; f ++ ) {
  9489. flare = this.lensFlares[ f ];
  9490. flare.x = this.positionScreen.x + vecX * flare.distance;
  9491. flare.y = this.positionScreen.y + vecY * flare.distance;
  9492. flare.wantedRotation = flare.x * Math.PI * 0.25;
  9493. flare.rotation += ( flare.wantedRotation - flare.rotation ) * 0.25;
  9494. }
  9495. };
  9496. // File:src/scenes/Scene.js
  9497. /**
  9498. * @author mrdoob / http://mrdoob.com/
  9499. */
  9500. THREE.Scene = function () {
  9501. THREE.Object3D.call( this );
  9502. this.type = 'Scene';
  9503. this.fog = null;
  9504. this.overrideMaterial = null;
  9505. this.autoUpdate = true; // checked by the renderer
  9506. };
  9507. THREE.Scene.prototype = Object.create( THREE.Object3D.prototype );
  9508. THREE.Scene.prototype.constructor = THREE.Scene;
  9509. THREE.Scene.prototype.clone = function ( object ) {
  9510. if ( object === undefined ) object = new THREE.Scene();
  9511. THREE.Object3D.prototype.clone.call( this, object );
  9512. if ( this.fog !== null ) object.fog = this.fog.clone();
  9513. if ( this.overrideMaterial !== null ) object.overrideMaterial = this.overrideMaterial.clone();
  9514. object.autoUpdate = this.autoUpdate;
  9515. object.matrixAutoUpdate = this.matrixAutoUpdate;
  9516. return object;
  9517. };
  9518. // File:src/scenes/Fog.js
  9519. /**
  9520. * @author mrdoob / http://mrdoob.com/
  9521. * @author alteredq / http://alteredqualia.com/
  9522. */
  9523. THREE.Fog = function ( color, near, far ) {
  9524. this.name = '';
  9525. this.color = new THREE.Color( color );
  9526. this.near = ( near !== undefined ) ? near : 1;
  9527. this.far = ( far !== undefined ) ? far : 1000;
  9528. };
  9529. THREE.Fog.prototype.clone = function () {
  9530. return new THREE.Fog( this.color.getHex(), this.near, this.far );
  9531. };
  9532. // File:src/scenes/FogExp2.js
  9533. /**
  9534. * @author mrdoob / http://mrdoob.com/
  9535. * @author alteredq / http://alteredqualia.com/
  9536. */
  9537. THREE.FogExp2 = function ( color, density ) {
  9538. this.name = '';
  9539. this.color = new THREE.Color( color );
  9540. this.density = ( density !== undefined ) ? density : 0.00025;
  9541. };
  9542. THREE.FogExp2.prototype.clone = function () {
  9543. return new THREE.FogExp2( this.color.getHex(), this.density );
  9544. };
  9545. // File:src/renderers/shaders/ShaderChunk.js
  9546. THREE.ShaderChunk = {};
  9547. // File:src/renderers/shaders/ShaderChunk/alphatest_fragment.glsl
  9548. THREE.ShaderChunk[ 'alphatest_fragment'] = "#ifdef ALPHATEST\n\n if ( gl_FragColor.a < ALPHATEST ) discard;\n\n#endif\n";
  9549. // File:src/renderers/shaders/ShaderChunk/lights_lambert_vertex.glsl
  9550. THREE.ShaderChunk[ 'lights_lambert_vertex'] = "vLightFront = vec3( 0.0 );\n\n#ifdef DOUBLE_SIDED\n\n vLightBack = vec3( 0.0 );\n\n#endif\n\ntransformedNormal = normalize( transformedNormal );\n\n#if MAX_DIR_LIGHTS > 0\n\nfor( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {\n\n vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );\n vec3 dirVector = normalize( lDirection.xyz );\n\n float dotProduct = dot( transformedNormal, dirVector );\n vec3 directionalLightWeighting = vec3( max( dotProduct, 0.0 ) );\n\n #ifdef DOUBLE_SIDED\n\n vec3 directionalLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );\n\n #ifdef WRAP_AROUND\n\n vec3 directionalLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );\n\n #endif\n\n #endif\n\n #ifdef WRAP_AROUND\n\n vec3 directionalLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );\n directionalLightWeighting = mix( directionalLightWeighting, directionalLightWeightingHalf, wrapRGB );\n\n #ifdef DOUBLE_SIDED\n\n directionalLightWeightingBack = mix( directionalLightWeightingBack, directionalLightWeightingHalfBack, wrapRGB );\n\n #endif\n\n #endif\n\n vLightFront += directionalLightColor[ i ] * directionalLightWeighting;\n\n #ifdef DOUBLE_SIDED\n\n vLightBack += directionalLightColor[ i ] * directionalLightWeightingBack;\n\n #endif\n\n}\n\n#endif\n\n#if MAX_POINT_LIGHTS > 0\n\n for( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {\n\n vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );\n vec3 lVector = lPosition.xyz - mvPosition.xyz;\n\n float lDistance = 1.0;\n if ( pointLightDistance[ i ] > 0.0 )\n lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );\n\n lVector = normalize( lVector );\n float dotProduct = dot( transformedNormal, lVector );\n\n vec3 pointLightWeighting = vec3( max( dotProduct, 0.0 ) );\n\n #ifdef DOUBLE_SIDED\n\n vec3 pointLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );\n\n #ifdef WRAP_AROUND\n\n vec3 pointLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );\n\n #endif\n\n #endif\n\n #ifdef WRAP_AROUND\n\n vec3 pointLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );\n pointLightWeighting = mix( pointLightWeighting, pointLightWeightingHalf, wrapRGB );\n\n #ifdef DOUBLE_SIDED\n\n pointLightWeightingBack = mix( pointLightWeightingBack, pointLightWeightingHalfBack, wrapRGB );\n\n #endif\n\n #endif\n\n vLightFront += pointLightColor[ i ] * pointLightWeighting * lDistance;\n\n #ifdef DOUBLE_SIDED\n\n vLightBack += pointLightColor[ i ] * pointLightWeightingBack * lDistance;\n\n #endif\n\n }\n\n#endif\n\n#if MAX_SPOT_LIGHTS > 0\n\n for( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {\n\n vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );\n vec3 lVector = lPosition.xyz - mvPosition.xyz;\n\n float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - worldPosition.xyz ) );\n\n if ( spotEffect > spotLightAngleCos[ i ] ) {\n\n spotEffect = max( pow( max( spotEffect, 0.0 ), spotLightExponent[ i ] ), 0.0 );\n\n float lDistance = 1.0;\n if ( spotLightDistance[ i ] > 0.0 )\n lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );\n\n lVector = normalize( lVector );\n\n float dotProduct = dot( transformedNormal, lVector );\n vec3 spotLightWeighting = vec3( max( dotProduct, 0.0 ) );\n\n #ifdef DOUBLE_SIDED\n\n vec3 spotLightWeightingBack = vec3( max( -dotProduct, 0.0 ) );\n\n #ifdef WRAP_AROUND\n\n vec3 spotLightWeightingHalfBack = vec3( max( -0.5 * dotProduct + 0.5, 0.0 ) );\n\n #endif\n\n #endif\n\n #ifdef WRAP_AROUND\n\n vec3 spotLightWeightingHalf = vec3( max( 0.5 * dotProduct + 0.5, 0.0 ) );\n spotLightWeighting = mix( spotLightWeighting, spotLightWeightingHalf, wrapRGB );\n\n #ifdef DOUBLE_SIDED\n\n spotLightWeightingBack = mix( spotLightWeightingBack, spotLightWeightingHalfBack, wrapRGB );\n\n #endif\n\n #endif\n\n vLightFront += spotLightColor[ i ] * spotLightWeighting * lDistance * spotEffect;\n\n #ifdef DOUBLE_SIDED\n\n vLightBack += spotLightColor[ i ] * spotLightWeightingBack * lDistance * spotEffect;\n\n #endif\n\n }\n\n }\n\n#endif\n\n#if MAX_HEMI_LIGHTS > 0\n\n for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {\n\n vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );\n vec3 lVector = normalize( lDirection.xyz );\n\n float dotProduct = dot( transformedNormal, lVector );\n\n float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;\n float hemiDiffuseWeightBack = -0.5 * dotProduct + 0.5;\n\n vLightFront += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );\n\n #ifdef DOUBLE_SIDED\n\n vLightBack += mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeightBack );\n\n #endif\n\n }\n\n#endif\n\nvLightFront = vLightFront * diffuse + ambient * ambientLightColor + emissive;\n\n#ifdef DOUBLE_SIDED\n\n vLightBack = vLightBack * diffuse + ambient * ambientLightColor + emissive;\n\n#endif";
  9551. // File:src/renderers/shaders/ShaderChunk/map_particle_pars_fragment.glsl
  9552. THREE.ShaderChunk[ 'map_particle_pars_fragment'] = "#ifdef USE_MAP\n\n uniform sampler2D map;\n\n#endif";
  9553. // File:src/renderers/shaders/ShaderChunk/default_vertex.glsl
  9554. THREE.ShaderChunk[ 'default_vertex'] = "vec4 mvPosition;\n\n#ifdef USE_SKINNING\n\n mvPosition = modelViewMatrix * skinned;\n\n#endif\n\n#if !defined( USE_SKINNING ) && defined( USE_MORPHTARGETS )\n\n mvPosition = modelViewMatrix * vec4( morphed, 1.0 );\n\n#endif\n\n#if !defined( USE_SKINNING ) && ! defined( USE_MORPHTARGETS )\n\n mvPosition = modelViewMatrix * vec4( position, 1.0 );\n\n#endif\n\ngl_Position = projectionMatrix * mvPosition;";
  9555. // File:src/renderers/shaders/ShaderChunk/map_pars_fragment.glsl
  9556. THREE.ShaderChunk[ 'map_pars_fragment'] = "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP )\n\n varying vec2 vUv;\n\n#endif\n\n#ifdef USE_MAP\n\n uniform sampler2D map;\n\n#endif";
  9557. // File:src/renderers/shaders/ShaderChunk/skinnormal_vertex.glsl
  9558. THREE.ShaderChunk[ 'skinnormal_vertex'] = "#ifdef USE_SKINNING\n\n mat4 skinMatrix = mat4( 0.0 );\n skinMatrix += skinWeight.x * boneMatX;\n skinMatrix += skinWeight.y * boneMatY;\n skinMatrix += skinWeight.z * boneMatZ;\n skinMatrix += skinWeight.w * boneMatW;\n skinMatrix = bindMatrixInverse * skinMatrix * bindMatrix;\n\n #ifdef USE_MORPHNORMALS\n\n vec4 skinnedNormal = skinMatrix * vec4( morphedNormal, 0.0 );\n\n #else\n\n vec4 skinnedNormal = skinMatrix * vec4( normal, 0.0 );\n\n #endif\n\n#endif\n";
  9559. // File:src/renderers/shaders/ShaderChunk/logdepthbuf_pars_vertex.glsl
  9560. THREE.ShaderChunk[ 'logdepthbuf_pars_vertex'] = "#ifdef USE_LOGDEPTHBUF\n\n #ifdef USE_LOGDEPTHBUF_EXT\n\n varying float vFragDepth;\n\n #endif\n\n uniform float logDepthBufFC;\n\n#endif";
  9561. // File:src/renderers/shaders/ShaderChunk/lightmap_pars_vertex.glsl
  9562. THREE.ShaderChunk[ 'lightmap_pars_vertex'] = "#ifdef USE_LIGHTMAP\n\n varying vec2 vUv2;\n\n#endif";
  9563. // File:src/renderers/shaders/ShaderChunk/lights_phong_fragment.glsl
  9564. THREE.ShaderChunk[ 'lights_phong_fragment'] = "vec3 normal = normalize( vNormal );\nvec3 viewPosition = normalize( vViewPosition );\n\n#ifdef DOUBLE_SIDED\n\n normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );\n\n#endif\n\n#ifdef USE_NORMALMAP\n\n normal = perturbNormal2Arb( -vViewPosition, normal );\n\n#elif defined( USE_BUMPMAP )\n\n normal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );\n\n#endif\n\n#if MAX_POINT_LIGHTS > 0\n\n vec3 pointDiffuse = vec3( 0.0 );\n vec3 pointSpecular = vec3( 0.0 );\n\n for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {\n\n vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );\n vec3 lVector = lPosition.xyz + vViewPosition.xyz;\n\n float lDistance = 1.0;\n if ( pointLightDistance[ i ] > 0.0 )\n lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );\n\n lVector = normalize( lVector );\n\n // diffuse\n\n float dotProduct = dot( normal, lVector );\n\n #ifdef WRAP_AROUND\n\n float pointDiffuseWeightFull = max( dotProduct, 0.0 );\n float pointDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );\n\n vec3 pointDiffuseWeight = mix( vec3( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );\n\n #else\n\n float pointDiffuseWeight = max( dotProduct, 0.0 );\n\n #endif\n\n pointDiffuse += diffuse * pointLightColor[ i ] * pointDiffuseWeight * lDistance;\n\n // specular\n\n vec3 pointHalfVector = normalize( lVector + viewPosition );\n float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );\n float pointSpecularWeight = specularStrength * max( pow( pointDotNormalHalf, shininess ), 0.0 );\n\n float specularNormalization = ( shininess + 2.0 ) / 8.0;\n\n vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVector, pointHalfVector ), 0.0 ), 5.0 );\n pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * lDistance * specularNormalization;\n\n }\n\n#endif\n\n#if MAX_SPOT_LIGHTS > 0\n\n vec3 spotDiffuse = vec3( 0.0 );\n vec3 spotSpecular = vec3( 0.0 );\n\n for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {\n\n vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );\n vec3 lVector = lPosition.xyz + vViewPosition.xyz;\n\n float lDistance = 1.0;\n if ( spotLightDistance[ i ] > 0.0 )\n lDistance = 1.0 - min( ( length( lVector ) / spotLightDistance[ i ] ), 1.0 );\n\n lVector = normalize( lVector );\n\n float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );\n\n if ( spotEffect > spotLightAngleCos[ i ] ) {\n\n spotEffect = max( pow( max( spotEffect, 0.0 ), spotLightExponent[ i ] ), 0.0 );\n\n // diffuse\n\n float dotProduct = dot( normal, lVector );\n\n #ifdef WRAP_AROUND\n\n float spotDiffuseWeightFull = max( dotProduct, 0.0 );\n float spotDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );\n\n vec3 spotDiffuseWeight = mix( vec3( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );\n\n #else\n\n float spotDiffuseWeight = max( dotProduct, 0.0 );\n\n #endif\n\n spotDiffuse += diffuse * spotLightColor[ i ] * spotDiffuseWeight * lDistance * spotEffect;\n\n // specular\n\n vec3 spotHalfVector = normalize( lVector + viewPosition );\n float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );\n float spotSpecularWeight = specularStrength * max( pow( spotDotNormalHalf, shininess ), 0.0 );\n\n float specularNormalization = ( shininess + 2.0 ) / 8.0;\n\n vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVector, spotHalfVector ), 0.0 ), 5.0 );\n spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * lDistance * specularNormalization * spotEffect;\n\n }\n\n }\n\n#endif\n\n#if MAX_DIR_LIGHTS > 0\n\n vec3 dirDiffuse = vec3( 0.0 );\n vec3 dirSpecular = vec3( 0.0 );\n\n for( int i = 0; i < MAX_DIR_LIGHTS; i ++ ) {\n\n vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );\n vec3 dirVector = normalize( lDirection.xyz );\n\n // diffuse\n\n float dotProduct = dot( normal, dirVector );\n\n #ifdef WRAP_AROUND\n\n float dirDiffuseWeightFull = max( dotProduct, 0.0 );\n float dirDiffuseWeightHalf = max( 0.5 * dotProduct + 0.5, 0.0 );\n\n vec3 dirDiffuseWeight = mix( vec3( dirDiffuseWeightFull ), vec3( dirDiffuseWeightHalf ), wrapRGB );\n\n #else\n\n float dirDiffuseWeight = max( dotProduct, 0.0 );\n\n #endif\n\n dirDiffuse += diffuse * directionalLightColor[ i ] * dirDiffuseWeight;\n\n // specular\n\n vec3 dirHalfVector = normalize( dirVector + viewPosition );\n float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );\n float dirSpecularWeight = specularStrength * max( pow( dirDotNormalHalf, shininess ), 0.0 );\n\n /*\n // fresnel term from skin shader\n const float F0 = 0.128;\n\n float base = 1.0 - dot( viewPosition, dirHalfVector );\n float exponential = pow( base, 5.0 );\n\n float fresnel = exponential + F0 * ( 1.0 - exponential );\n */\n\n /*\n // fresnel term from fresnel shader\n const float mFresnelBias = 0.08;\n const float mFresnelScale = 0.3;\n const float mFresnelPower = 5.0;\n\n float fresnel = mFresnelBias + mFresnelScale * pow( 1.0 + dot( normalize( -viewPosition ), normal ), mFresnelPower );\n */\n\n float specularNormalization = ( shininess + 2.0 ) / 8.0;\n\n // dirSpecular += specular * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization * fresnel;\n\n vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( dirVector, dirHalfVector ), 0.0 ), 5.0 );\n dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;\n\n\n }\n\n#endif\n\n#if MAX_HEMI_LIGHTS > 0\n\n vec3 hemiDiffuse = vec3( 0.0 );\n vec3 hemiSpecular = vec3( 0.0 );\n\n for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {\n\n vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );\n vec3 lVector = normalize( lDirection.xyz );\n\n // diffuse\n\n float dotProduct = dot( normal, lVector );\n float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;\n\n vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );\n\n hemiDiffuse += diffuse * hemiColor;\n\n // specular (sky light)\n\n vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );\n float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;\n float hemiSpecularWeightSky = specularStrength * max( pow( max( hemiDotNormalHalfSky, 0.0 ), shininess ), 0.0 );\n\n // specular (ground light)\n\n vec3 lVectorGround = -lVector;\n\n vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );\n float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;\n float hemiSpecularWeightGround = specularStrength * max( pow( max( hemiDotNormalHalfGround, 0.0 ), shininess ), 0.0 );\n\n float dotProductGround = dot( normal, lVectorGround );\n\n float specularNormalization = ( shininess + 2.0 ) / 8.0;\n\n vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVector, hemiHalfVectorSky ), 0.0 ), 5.0 );\n vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 0.0 ), 5.0 );\n hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );\n\n }\n\n#endif\n\nvec3 totalDiffuse = vec3( 0.0 );\nvec3 totalSpecular = vec3( 0.0 );\n\n#if MAX_DIR_LIGHTS > 0\n\n totalDiffuse += dirDiffuse;\n totalSpecular += dirSpecular;\n\n#endif\n\n#if MAX_HEMI_LIGHTS > 0\n\n totalDiffuse += hemiDiffuse;\n totalSpecular += hemiSpecular;\n\n#endif\n\n#if MAX_POINT_LIGHTS > 0\n\n totalDiffuse += pointDiffuse;\n totalSpecular += pointSpecular;\n\n#endif\n\n#if MAX_SPOT_LIGHTS > 0\n\n totalDiffuse += spotDiffuse;\n totalSpecular += spotSpecular;\n\n#endif\n\n#ifdef METAL\n\n gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient + totalSpecular );\n\n#else\n\n gl_FragColor.xyz = gl_FragColor.xyz * ( emissive + totalDiffuse + ambientLightColor * ambient ) + totalSpecular;\n\n#endif";
  9565. // File:src/renderers/shaders/ShaderChunk/fog_pars_fragment.glsl
  9566. THREE.ShaderChunk[ 'fog_pars_fragment'] = "#ifdef USE_FOG\n\n uniform vec3 fogColor;\n\n #ifdef FOG_EXP2\n\n uniform float fogDensity;\n\n #else\n\n uniform float fogNear;\n uniform float fogFar;\n #endif\n\n#endif";
  9567. // File:src/renderers/shaders/ShaderChunk/morphnormal_vertex.glsl
  9568. THREE.ShaderChunk[ 'morphnormal_vertex'] = "#ifdef USE_MORPHNORMALS\n\n vec3 morphedNormal = vec3( 0.0 );\n\n morphedNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];\n morphedNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];\n morphedNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];\n morphedNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];\n\n morphedNormal += normal;\n\n#endif";
  9569. // File:src/renderers/shaders/ShaderChunk/envmap_pars_fragment.glsl
  9570. THREE.ShaderChunk[ 'envmap_pars_fragment'] = "#ifdef USE_ENVMAP\n\n uniform float reflectivity;\n #ifdef ENVMAP_TYPE_CUBE\n uniform samplerCube envMap;\n #else\n uniform sampler2D envMap;\n #endif\n uniform float flipEnvMap;\n uniform int combine;\n\n #if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG )\n\n uniform bool useRefract;\n uniform float refractionRatio;\n\n #else\n\n varying vec3 vReflect;\n\n #endif\n\n#endif";
  9571. // File:src/renderers/shaders/ShaderChunk/logdepthbuf_fragment.glsl
  9572. THREE.ShaderChunk[ 'logdepthbuf_fragment'] = "#if defined(USE_LOGDEPTHBUF) && defined(USE_LOGDEPTHBUF_EXT)\n\n gl_FragDepthEXT = log2(vFragDepth) * logDepthBufFC * 0.5;\n\n#endif";
  9573. // File:src/renderers/shaders/ShaderChunk/normalmap_pars_fragment.glsl
  9574. THREE.ShaderChunk[ 'normalmap_pars_fragment'] = "#ifdef USE_NORMALMAP\n\n uniform sampler2D normalMap;\n uniform vec2 normalScale;\n\n // Per-Pixel Tangent Space Normal Mapping\n // http://hacksoflife.blogspot.ch/2009/11/per-pixel-tangent-space-normal-mapping.html\n\n vec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm ) {\n\n vec3 q0 = dFdx( eye_pos.xyz );\n vec3 q1 = dFdy( eye_pos.xyz );\n vec2 st0 = dFdx( vUv.st );\n vec2 st1 = dFdy( vUv.st );\n\n vec3 S = normalize( q0 * st1.t - q1 * st0.t );\n vec3 T = normalize( -q0 * st1.s + q1 * st0.s );\n vec3 N = normalize( surf_norm );\n\n vec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;\n mapN.xy = normalScale * mapN.xy;\n mat3 tsn = mat3( S, T, N );\n return normalize( tsn * mapN );\n\n }\n\n#endif\n";
  9575. // File:src/renderers/shaders/ShaderChunk/lights_phong_pars_vertex.glsl
  9576. THREE.ShaderChunk[ 'lights_phong_pars_vertex'] = "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP ) || defined( USE_ENVMAP )\n\n varying vec3 vWorldPosition;\n\n#endif\n";
  9577. // File:src/renderers/shaders/ShaderChunk/lightmap_pars_fragment.glsl
  9578. THREE.ShaderChunk[ 'lightmap_pars_fragment'] = "#ifdef USE_LIGHTMAP\n\n varying vec2 vUv2;\n uniform sampler2D lightMap;\n\n#endif";
  9579. // File:src/renderers/shaders/ShaderChunk/shadowmap_vertex.glsl
  9580. THREE.ShaderChunk[ 'shadowmap_vertex'] = "#ifdef USE_SHADOWMAP\n\n for( int i = 0; i < MAX_SHADOWS; i ++ ) {\n\n vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;\n\n }\n\n#endif";
  9581. // File:src/renderers/shaders/ShaderChunk/lights_phong_vertex.glsl
  9582. THREE.ShaderChunk[ 'lights_phong_vertex'] = "#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP ) || defined( USE_ENVMAP )\n\n vWorldPosition = worldPosition.xyz;\n\n#endif";
  9583. // File:src/renderers/shaders/ShaderChunk/map_fragment.glsl
  9584. THREE.ShaderChunk[ 'map_fragment'] = "#ifdef USE_MAP\n\n vec4 texelColor = texture2D( map, vUv );\n\n #ifdef GAMMA_INPUT\n\n texelColor.xyz *= texelColor.xyz;\n\n #endif\n\n gl_FragColor = gl_FragColor * texelColor;\n\n#endif";
  9585. // File:src/renderers/shaders/ShaderChunk/lightmap_vertex.glsl
  9586. THREE.ShaderChunk[ 'lightmap_vertex'] = "#ifdef USE_LIGHTMAP\n\n vUv2 = uv2;\n\n#endif";
  9587. // File:src/renderers/shaders/ShaderChunk/map_particle_fragment.glsl
  9588. THREE.ShaderChunk[ 'map_particle_fragment'] = "#ifdef USE_MAP\n\n gl_FragColor = gl_FragColor * texture2D( map, vec2( gl_PointCoord.x, 1.0 - gl_PointCoord.y ) );\n\n#endif";
  9589. // File:src/renderers/shaders/ShaderChunk/color_pars_fragment.glsl
  9590. THREE.ShaderChunk[ 'color_pars_fragment'] = "#ifdef USE_COLOR\n\n varying vec3 vColor;\n\n#endif\n";
  9591. // File:src/renderers/shaders/ShaderChunk/color_vertex.glsl
  9592. THREE.ShaderChunk[ 'color_vertex'] = "#ifdef USE_COLOR\n\n #ifdef GAMMA_INPUT\n\n vColor = color * color;\n\n #else\n\n vColor = color;\n\n #endif\n\n#endif";
  9593. // File:src/renderers/shaders/ShaderChunk/skinning_vertex.glsl
  9594. THREE.ShaderChunk[ 'skinning_vertex'] = "#ifdef USE_SKINNING\n\n #ifdef USE_MORPHTARGETS\n\n vec4 skinVertex = bindMatrix * vec4( morphed, 1.0 );\n\n #else\n\n vec4 skinVertex = bindMatrix * vec4( position, 1.0 );\n\n #endif\n\n vec4 skinned = vec4( 0.0 );\n skinned += boneMatX * skinVertex * skinWeight.x;\n skinned += boneMatY * skinVertex * skinWeight.y;\n skinned += boneMatZ * skinVertex * skinWeight.z;\n skinned += boneMatW * skinVertex * skinWeight.w;\n skinned = bindMatrixInverse * skinned;\n\n#endif\n";
  9595. // File:src/renderers/shaders/ShaderChunk/envmap_pars_vertex.glsl
  9596. THREE.ShaderChunk[ 'envmap_pars_vertex'] = "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG )\n\n varying vec3 vReflect;\n\n uniform float refractionRatio;\n uniform bool useRefract;\n\n#endif\n";
  9597. // File:src/renderers/shaders/ShaderChunk/linear_to_gamma_fragment.glsl
  9598. THREE.ShaderChunk[ 'linear_to_gamma_fragment'] = "#ifdef GAMMA_OUTPUT\n\n gl_FragColor.xyz = sqrt( gl_FragColor.xyz );\n\n#endif";
  9599. // File:src/renderers/shaders/ShaderChunk/color_pars_vertex.glsl
  9600. THREE.ShaderChunk[ 'color_pars_vertex'] = "#ifdef USE_COLOR\n\n varying vec3 vColor;\n\n#endif";
  9601. // File:src/renderers/shaders/ShaderChunk/lights_lambert_pars_vertex.glsl
  9602. THREE.ShaderChunk[ 'lights_lambert_pars_vertex'] = "uniform vec3 ambient;\nuniform vec3 diffuse;\nuniform vec3 emissive;\n\nuniform vec3 ambientLightColor;\n\n#if MAX_DIR_LIGHTS > 0\n\n uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];\n uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];\n\n#endif\n\n#if MAX_HEMI_LIGHTS > 0\n\n uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];\n uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];\n uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];\n\n#endif\n\n#if MAX_POINT_LIGHTS > 0\n\n uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];\n uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];\n uniform float pointLightDistance[ MAX_POINT_LIGHTS ];\n\n#endif\n\n#if MAX_SPOT_LIGHTS > 0\n\n uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];\n uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];\n uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];\n uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];\n uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];\n uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];\n\n#endif\n\n#ifdef WRAP_AROUND\n\n uniform vec3 wrapRGB;\n\n#endif\n";
  9603. // File:src/renderers/shaders/ShaderChunk/map_pars_vertex.glsl
  9604. THREE.ShaderChunk[ 'map_pars_vertex'] = "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP )\n\n varying vec2 vUv;\n uniform vec4 offsetRepeat;\n\n#endif\n";
  9605. // File:src/renderers/shaders/ShaderChunk/envmap_fragment.glsl
  9606. THREE.ShaderChunk[ 'envmap_fragment'] = "#ifdef USE_ENVMAP\n\n vec3 reflectVec;\n\n #if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG )\n\n vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );\n\n // http://en.wikibooks.org/wiki/GLSL_Programming/Applying_Matrix_Transformations\n // Transforming Normal Vectors with the Inverse Transformation\n\n vec3 worldNormal = normalize( vec3( vec4( normal, 0.0 ) * viewMatrix ) );\n\n if ( useRefract ) {\n\n reflectVec = refract( cameraToVertex, worldNormal, refractionRatio );\n\n } else { \n\n reflectVec = reflect( cameraToVertex, worldNormal );\n\n }\n\n #else\n\n reflectVec = vReflect;\n\n #endif\n\n #ifdef DOUBLE_SIDED\n float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );\n #else\n float flipNormal = 1.0;\n #endif\n\n #ifdef ENVMAP_TYPE_CUBE\n vec4 envColor = textureCube( envMap, flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );\n\n #elif defined( ENVMAP_TYPE_EQUIREC )\n vec2 sampleUV;\n sampleUV.y = clamp( flipNormal * reflectVec.y * 0.5 + 0.5, 0.0, 1.0);\n sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * 0.15915494309189533576888376337251 + 0.5; // reciprocal( 2 PI ) + 0.5\n vec4 envColor = texture2D( envMap, sampleUV );\n \n #elif defined( ENVMAP_TYPE_SPHERE )\n vec3 reflectView = flipNormal * normalize((viewMatrix * vec4( reflectVec, 0.0 )).xyz + vec3(0.0,0.0,1.0));\n vec4 envColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5 );\n #endif\n\n #ifdef GAMMA_INPUT\n\n envColor.xyz *= envColor.xyz;\n\n #endif\n\n if ( combine == 1 ) {\n\n gl_FragColor.xyz = mix( gl_FragColor.xyz, envColor.xyz, specularStrength * reflectivity );\n\n } else if ( combine == 2 ) {\n\n gl_FragColor.xyz += envColor.xyz * specularStrength * reflectivity;\n\n } else {\n\n gl_FragColor.xyz = mix( gl_FragColor.xyz, gl_FragColor.xyz * envColor.xyz, specularStrength * reflectivity );\n\n }\n\n#endif";
  9607. // File:src/renderers/shaders/ShaderChunk/specularmap_pars_fragment.glsl
  9608. THREE.ShaderChunk[ 'specularmap_pars_fragment'] = "#ifdef USE_SPECULARMAP\n\n uniform sampler2D specularMap;\n\n#endif";
  9609. // File:src/renderers/shaders/ShaderChunk/logdepthbuf_vertex.glsl
  9610. THREE.ShaderChunk[ 'logdepthbuf_vertex'] = "#ifdef USE_LOGDEPTHBUF\n\n gl_Position.z = log2(max(1e-6, gl_Position.w + 1.0)) * logDepthBufFC;\n\n #ifdef USE_LOGDEPTHBUF_EXT\n\n vFragDepth = 1.0 + gl_Position.w;\n\n#else\n\n gl_Position.z = (gl_Position.z - 1.0) * gl_Position.w;\n\n #endif\n\n#endif";
  9611. // File:src/renderers/shaders/ShaderChunk/morphtarget_pars_vertex.glsl
  9612. THREE.ShaderChunk[ 'morphtarget_pars_vertex'] = "#ifdef USE_MORPHTARGETS\n\n #ifndef USE_MORPHNORMALS\n\n uniform float morphTargetInfluences[ 8 ];\n\n #else\n\n uniform float morphTargetInfluences[ 4 ];\n\n #endif\n\n#endif";
  9613. // File:src/renderers/shaders/ShaderChunk/specularmap_fragment.glsl
  9614. THREE.ShaderChunk[ 'specularmap_fragment'] = "float specularStrength;\n\n#ifdef USE_SPECULARMAP\n\n vec4 texelSpecular = texture2D( specularMap, vUv );\n specularStrength = texelSpecular.r;\n\n#else\n\n specularStrength = 1.0;\n\n#endif";
  9615. // File:src/renderers/shaders/ShaderChunk/fog_fragment.glsl
  9616. THREE.ShaderChunk[ 'fog_fragment'] = "#ifdef USE_FOG\n\n #ifdef USE_LOGDEPTHBUF_EXT\n\n float depth = gl_FragDepthEXT / gl_FragCoord.w;\n\n #else\n\n float depth = gl_FragCoord.z / gl_FragCoord.w;\n\n #endif\n\n #ifdef FOG_EXP2\n\n const float LOG2 = 1.442695;\n float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );\n fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );\n\n #else\n\n float fogFactor = smoothstep( fogNear, fogFar, depth );\n\n #endif\n \n gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );\n\n#endif";
  9617. // File:src/renderers/shaders/ShaderChunk/bumpmap_pars_fragment.glsl
  9618. THREE.ShaderChunk[ 'bumpmap_pars_fragment'] = "#ifdef USE_BUMPMAP\n\n uniform sampler2D bumpMap;\n uniform float bumpScale;\n\n // Derivative maps - bump mapping unparametrized surfaces by Morten Mikkelsen\n // http://mmikkelsen3d.blogspot.sk/2011/07/derivative-maps.html\n\n // Evaluate the derivative of the height w.r.t. screen-space using forward differencing (listing 2)\n\n vec2 dHdxy_fwd() {\n\n vec2 dSTdx = dFdx( vUv );\n vec2 dSTdy = dFdy( vUv );\n\n float Hll = bumpScale * texture2D( bumpMap, vUv ).x;\n float dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;\n float dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;\n\n return vec2( dBx, dBy );\n\n }\n\n vec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {\n\n vec3 vSigmaX = dFdx( surf_pos );\n vec3 vSigmaY = dFdy( surf_pos );\n vec3 vN = surf_norm; // normalized\n\n vec3 R1 = cross( vSigmaY, vN );\n vec3 R2 = cross( vN, vSigmaX );\n\n float fDet = dot( vSigmaX, R1 );\n\n vec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );\n return normalize( abs( fDet ) * surf_norm - vGrad );\n\n }\n\n#endif";
  9619. // File:src/renderers/shaders/ShaderChunk/defaultnormal_vertex.glsl
  9620. THREE.ShaderChunk[ 'defaultnormal_vertex'] = "vec3 objectNormal;\n\n#ifdef USE_SKINNING\n\n objectNormal = skinnedNormal.xyz;\n\n#endif\n\n#if !defined( USE_SKINNING ) && defined( USE_MORPHNORMALS )\n\n objectNormal = morphedNormal;\n\n#endif\n\n#if !defined( USE_SKINNING ) && ! defined( USE_MORPHNORMALS )\n\n objectNormal = normal;\n\n#endif\n\n#ifdef FLIP_SIDED\n\n objectNormal = -objectNormal;\n\n#endif\n\nvec3 transformedNormal = normalMatrix * objectNormal;";
  9621. // File:src/renderers/shaders/ShaderChunk/lights_phong_pars_fragment.glsl
  9622. THREE.ShaderChunk[ 'lights_phong_pars_fragment'] = "uniform vec3 ambientLightColor;\n\n#if MAX_DIR_LIGHTS > 0\n\n uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];\n uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];\n\n#endif\n\n#if MAX_HEMI_LIGHTS > 0\n\n uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];\n uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];\n uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];\n\n#endif\n\n#if MAX_POINT_LIGHTS > 0\n\n uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];\n\n uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];\n uniform float pointLightDistance[ MAX_POINT_LIGHTS ];\n\n#endif\n\n#if MAX_SPOT_LIGHTS > 0\n\n uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];\n uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];\n uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];\n uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];\n uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];\n\n uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];\n\n#endif\n\n#if MAX_SPOT_LIGHTS > 0 || defined( USE_BUMPMAP ) || defined( USE_ENVMAP )\n\n varying vec3 vWorldPosition;\n\n#endif\n\n#ifdef WRAP_AROUND\n\n uniform vec3 wrapRGB;\n\n#endif\n\nvarying vec3 vViewPosition;\nvarying vec3 vNormal;";
  9623. // File:src/renderers/shaders/ShaderChunk/skinbase_vertex.glsl
  9624. THREE.ShaderChunk[ 'skinbase_vertex'] = "#ifdef USE_SKINNING\n\n mat4 boneMatX = getBoneMatrix( skinIndex.x );\n mat4 boneMatY = getBoneMatrix( skinIndex.y );\n mat4 boneMatZ = getBoneMatrix( skinIndex.z );\n mat4 boneMatW = getBoneMatrix( skinIndex.w );\n\n#endif";
  9625. // File:src/renderers/shaders/ShaderChunk/map_vertex.glsl
  9626. THREE.ShaderChunk[ 'map_vertex'] = "#if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP )\n\n vUv = uv * offsetRepeat.zw + offsetRepeat.xy;\n\n#endif";
  9627. // File:src/renderers/shaders/ShaderChunk/lightmap_fragment.glsl
  9628. THREE.ShaderChunk[ 'lightmap_fragment'] = "#ifdef USE_LIGHTMAP\n\n gl_FragColor = gl_FragColor * texture2D( lightMap, vUv2 );\n\n#endif";
  9629. // File:src/renderers/shaders/ShaderChunk/shadowmap_pars_vertex.glsl
  9630. THREE.ShaderChunk[ 'shadowmap_pars_vertex'] = "#ifdef USE_SHADOWMAP\n\n varying vec4 vShadowCoord[ MAX_SHADOWS ];\n uniform mat4 shadowMatrix[ MAX_SHADOWS ];\n\n#endif";
  9631. // File:src/renderers/shaders/ShaderChunk/color_fragment.glsl
  9632. THREE.ShaderChunk[ 'color_fragment'] = "#ifdef USE_COLOR\n\n gl_FragColor = gl_FragColor * vec4( vColor, 1.0 );\n\n#endif";
  9633. // File:src/renderers/shaders/ShaderChunk/morphtarget_vertex.glsl
  9634. THREE.ShaderChunk[ 'morphtarget_vertex'] = "#ifdef USE_MORPHTARGETS\n\n vec3 morphed = vec3( 0.0 );\n morphed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];\n morphed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];\n morphed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];\n morphed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];\n\n #ifndef USE_MORPHNORMALS\n\n morphed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];\n morphed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];\n morphed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];\n morphed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];\n\n #endif\n\n morphed += position;\n\n#endif";
  9635. // File:src/renderers/shaders/ShaderChunk/envmap_vertex.glsl
  9636. THREE.ShaderChunk[ 'envmap_vertex'] = "#if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG )\n\n vec3 worldNormal = mat3( modelMatrix[ 0 ].xyz, modelMatrix[ 1 ].xyz, modelMatrix[ 2 ].xyz ) * objectNormal;\n worldNormal = normalize( worldNormal );\n\n vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition );\n\n if ( useRefract ) {\n\n vReflect = refract( cameraToVertex, worldNormal, refractionRatio );\n\n } else {\n\n vReflect = reflect( cameraToVertex, worldNormal );\n\n }\n\n#endif";
  9637. // File:src/renderers/shaders/ShaderChunk/shadowmap_fragment.glsl
  9638. THREE.ShaderChunk[ 'shadowmap_fragment'] = "#ifdef USE_SHADOWMAP\n\n #ifdef SHADOWMAP_DEBUG\n\n vec3 frustumColors[3];\n frustumColors[0] = vec3( 1.0, 0.5, 0.0 );\n frustumColors[1] = vec3( 0.0, 1.0, 0.8 );\n frustumColors[2] = vec3( 0.0, 0.5, 1.0 );\n\n #endif\n\n #ifdef SHADOWMAP_CASCADE\n\n int inFrustumCount = 0;\n\n #endif\n\n float fDepth;\n vec3 shadowColor = vec3( 1.0 );\n\n for( int i = 0; i < MAX_SHADOWS; i ++ ) {\n\n vec3 shadowCoord = vShadowCoord[ i ].xyz / vShadowCoord[ i ].w;\n\n // if ( something && something ) breaks ATI OpenGL shader compiler\n // if ( all( something, something ) ) using this instead\n\n bvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );\n bool inFrustum = all( inFrustumVec );\n\n // don't shadow pixels outside of light frustum\n // use just first frustum (for cascades)\n // don't shadow pixels behind far plane of light frustum\n\n #ifdef SHADOWMAP_CASCADE\n\n inFrustumCount += int( inFrustum );\n bvec3 frustumTestVec = bvec3( inFrustum, inFrustumCount == 1, shadowCoord.z <= 1.0 );\n\n #else\n\n bvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );\n\n #endif\n\n bool frustumTest = all( frustumTestVec );\n\n if ( frustumTest ) {\n\n shadowCoord.z += shadowBias[ i ];\n\n #if defined( SHADOWMAP_TYPE_PCF )\n\n // Percentage-close filtering\n // (9 pixel kernel)\n // http://fabiensanglard.net/shadowmappingPCF/\n\n float shadow = 0.0;\n\n /*\n // nested loops breaks shader compiler / validator on some ATI cards when using OpenGL\n // must enroll loop manually\n\n for ( float y = -1.25; y <= 1.25; y += 1.25 )\n for ( float x = -1.25; x <= 1.25; x += 1.25 ) {\n\n vec4 rgbaDepth = texture2D( shadowMap[ i ], vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy );\n\n // doesn't seem to produce any noticeable visual difference compared to simple texture2D lookup\n //vec4 rgbaDepth = texture2DProj( shadowMap[ i ], vec4( vShadowCoord[ i ].w * ( vec2( x * xPixelOffset, y * yPixelOffset ) + shadowCoord.xy ), 0.05, vShadowCoord[ i ].w ) );\n\n float fDepth = unpackDepth( rgbaDepth );\n\n if ( fDepth < shadowCoord.z )\n shadow += 1.0;\n\n }\n\n shadow /= 9.0;\n\n */\n\n const float shadowDelta = 1.0 / 9.0;\n\n float xPixelOffset = 1.0 / shadowMapSize[ i ].x;\n float yPixelOffset = 1.0 / shadowMapSize[ i ].y;\n\n float dx0 = -1.25 * xPixelOffset;\n float dy0 = -1.25 * yPixelOffset;\n float dx1 = 1.25 * xPixelOffset;\n float dy1 = 1.25 * yPixelOffset;\n\n fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );\n if ( fDepth < shadowCoord.z ) shadow += shadowDelta;\n\n fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );\n if ( fDepth < shadowCoord.z ) shadow += shadowDelta;\n\n fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );\n if ( fDepth < shadowCoord.z ) shadow += shadowDelta;\n\n fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );\n if ( fDepth < shadowCoord.z ) shadow += shadowDelta;\n\n fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );\n if ( fDepth < shadowCoord.z ) shadow += shadowDelta;\n\n fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );\n if ( fDepth < shadowCoord.z ) shadow += shadowDelta;\n\n fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );\n if ( fDepth < shadowCoord.z ) shadow += shadowDelta;\n\n fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );\n if ( fDepth < shadowCoord.z ) shadow += shadowDelta;\n\n fDepth = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );\n if ( fDepth < shadowCoord.z ) shadow += shadowDelta;\n\n shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );\n\n #elif defined( SHADOWMAP_TYPE_PCF_SOFT )\n\n // Percentage-close filtering\n // (9 pixel kernel)\n // http://fabiensanglard.net/shadowmappingPCF/\n\n float shadow = 0.0;\n\n float xPixelOffset = 1.0 / shadowMapSize[ i ].x;\n float yPixelOffset = 1.0 / shadowMapSize[ i ].y;\n\n float dx0 = -1.0 * xPixelOffset;\n float dy0 = -1.0 * yPixelOffset;\n float dx1 = 1.0 * xPixelOffset;\n float dy1 = 1.0 * yPixelOffset;\n\n mat3 shadowKernel;\n mat3 depthKernel;\n\n depthKernel[0][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy0 ) ) );\n depthKernel[0][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, 0.0 ) ) );\n depthKernel[0][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx0, dy1 ) ) );\n depthKernel[1][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy0 ) ) );\n depthKernel[1][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy ) );\n depthKernel[1][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( 0.0, dy1 ) ) );\n depthKernel[2][0] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy0 ) ) );\n depthKernel[2][1] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, 0.0 ) ) );\n depthKernel[2][2] = unpackDepth( texture2D( shadowMap[ i ], shadowCoord.xy + vec2( dx1, dy1 ) ) );\n\n vec3 shadowZ = vec3( shadowCoord.z );\n shadowKernel[0] = vec3(lessThan(depthKernel[0], shadowZ ));\n shadowKernel[0] *= vec3(0.25);\n\n shadowKernel[1] = vec3(lessThan(depthKernel[1], shadowZ ));\n shadowKernel[1] *= vec3(0.25);\n\n shadowKernel[2] = vec3(lessThan(depthKernel[2], shadowZ ));\n shadowKernel[2] *= vec3(0.25);\n\n vec2 fractionalCoord = 1.0 - fract( shadowCoord.xy * shadowMapSize[i].xy );\n\n shadowKernel[0] = mix( shadowKernel[1], shadowKernel[0], fractionalCoord.x );\n shadowKernel[1] = mix( shadowKernel[2], shadowKernel[1], fractionalCoord.x );\n\n vec4 shadowValues;\n shadowValues.x = mix( shadowKernel[0][1], shadowKernel[0][0], fractionalCoord.y );\n shadowValues.y = mix( shadowKernel[0][2], shadowKernel[0][1], fractionalCoord.y );\n shadowValues.z = mix( shadowKernel[1][1], shadowKernel[1][0], fractionalCoord.y );\n shadowValues.w = mix( shadowKernel[1][2], shadowKernel[1][1], fractionalCoord.y );\n\n shadow = dot( shadowValues, vec4( 1.0 ) );\n\n shadowColor = shadowColor * vec3( ( 1.0 - shadowDarkness[ i ] * shadow ) );\n\n #else\n\n vec4 rgbaDepth = texture2D( shadowMap[ i ], shadowCoord.xy );\n float fDepth = unpackDepth( rgbaDepth );\n\n if ( fDepth < shadowCoord.z )\n\n // spot with multiple shadows is darker\n\n shadowColor = shadowColor * vec3( 1.0 - shadowDarkness[ i ] );\n\n // spot with multiple shadows has the same color as single shadow spot\n\n // shadowColor = min( shadowColor, vec3( shadowDarkness[ i ] ) );\n\n #endif\n\n }\n\n\n #ifdef SHADOWMAP_DEBUG\n\n #ifdef SHADOWMAP_CASCADE\n\n if ( inFrustum && inFrustumCount == 1 ) gl_FragColor.xyz *= frustumColors[ i ];\n\n #else\n\n if ( inFrustum ) gl_FragColor.xyz *= frustumColors[ i ];\n\n #endif\n\n #endif\n\n }\n\n #ifdef GAMMA_OUTPUT\n\n shadowColor *= shadowColor;\n\n #endif\n\n gl_FragColor.xyz = gl_FragColor.xyz * shadowColor;\n\n#endif\n";
  9639. // File:src/renderers/shaders/ShaderChunk/worldpos_vertex.glsl
  9640. THREE.ShaderChunk[ 'worldpos_vertex'] = "#if defined( USE_ENVMAP ) || defined( PHONG ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP )\n\n #ifdef USE_SKINNING\n\n vec4 worldPosition = modelMatrix * skinned;\n\n #endif\n\n #if defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )\n\n vec4 worldPosition = modelMatrix * vec4( morphed, 1.0 );\n\n #endif\n\n #if ! defined( USE_MORPHTARGETS ) && ! defined( USE_SKINNING )\n\n vec4 worldPosition = modelMatrix * vec4( position, 1.0 );\n\n #endif\n\n#endif";
  9641. // File:src/renderers/shaders/ShaderChunk/shadowmap_pars_fragment.glsl
  9642. THREE.ShaderChunk[ 'shadowmap_pars_fragment'] = "#ifdef USE_SHADOWMAP\n\n uniform sampler2D shadowMap[ MAX_SHADOWS ];\n uniform vec2 shadowMapSize[ MAX_SHADOWS ];\n\n uniform float shadowDarkness[ MAX_SHADOWS ];\n uniform float shadowBias[ MAX_SHADOWS ];\n\n varying vec4 vShadowCoord[ MAX_SHADOWS ];\n\n float unpackDepth( const in vec4 rgba_depth ) {\n\n 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 );\n float depth = dot( rgba_depth, bit_shift );\n return depth;\n\n }\n\n#endif";
  9643. // File:src/renderers/shaders/ShaderChunk/skinning_pars_vertex.glsl
  9644. THREE.ShaderChunk[ 'skinning_pars_vertex'] = "#ifdef USE_SKINNING\n\n uniform mat4 bindMatrix;\n uniform mat4 bindMatrixInverse;\n\n #ifdef BONE_TEXTURE\n\n uniform sampler2D boneTexture;\n uniform int boneTextureWidth;\n uniform int boneTextureHeight;\n\n mat4 getBoneMatrix( const in float i ) {\n\n float j = i * 4.0;\n float x = mod( j, float( boneTextureWidth ) );\n float y = floor( j / float( boneTextureWidth ) );\n\n float dx = 1.0 / float( boneTextureWidth );\n float dy = 1.0 / float( boneTextureHeight );\n\n y = dy * ( y + 0.5 );\n\n vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );\n vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );\n vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );\n vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );\n\n mat4 bone = mat4( v1, v2, v3, v4 );\n\n return bone;\n\n }\n\n #else\n\n uniform mat4 boneGlobalMatrices[ MAX_BONES ];\n\n mat4 getBoneMatrix( const in float i ) {\n\n mat4 bone = boneGlobalMatrices[ int(i) ];\n return bone;\n\n }\n\n #endif\n\n#endif\n";
  9645. // File:src/renderers/shaders/ShaderChunk/logdepthbuf_pars_fragment.glsl
  9646. THREE.ShaderChunk[ 'logdepthbuf_pars_fragment'] = "#ifdef USE_LOGDEPTHBUF\n\n uniform float logDepthBufFC;\n\n #ifdef USE_LOGDEPTHBUF_EXT\n\n #extension GL_EXT_frag_depth : enable\n varying float vFragDepth;\n\n #endif\n\n#endif";
  9647. // File:src/renderers/shaders/ShaderChunk/alphamap_fragment.glsl
  9648. THREE.ShaderChunk[ 'alphamap_fragment'] = "#ifdef USE_ALPHAMAP\n\n gl_FragColor.a *= texture2D( alphaMap, vUv ).g;\n\n#endif\n";
  9649. // File:src/renderers/shaders/ShaderChunk/alphamap_pars_fragment.glsl
  9650. THREE.ShaderChunk[ 'alphamap_pars_fragment'] = "#ifdef USE_ALPHAMAP\n\n uniform sampler2D alphaMap;\n\n#endif\n";
  9651. // File:src/renderers/shaders/UniformsUtils.js
  9652. /**
  9653. * Uniform Utilities
  9654. */
  9655. THREE.UniformsUtils = {
  9656. merge: function ( uniforms ) {
  9657. var merged = {};
  9658. for ( var u = 0; u < uniforms.length; u ++ ) {
  9659. var tmp = this.clone( uniforms[ u ] );
  9660. for ( var p in tmp ) {
  9661. merged[ p ] = tmp[ p ];
  9662. }
  9663. }
  9664. return merged;
  9665. },
  9666. clone: function ( uniforms_src ) {
  9667. var uniforms_dst = {};
  9668. for ( var u in uniforms_src ) {
  9669. uniforms_dst[ u ] = {};
  9670. for ( var p in uniforms_src[ u ] ) {
  9671. var parameter_src = uniforms_src[ u ][ p ];
  9672. if ( parameter_src instanceof THREE.Color ||
  9673. parameter_src instanceof THREE.Vector2 ||
  9674. parameter_src instanceof THREE.Vector3 ||
  9675. parameter_src instanceof THREE.Vector4 ||
  9676. parameter_src instanceof THREE.Matrix4 ||
  9677. parameter_src instanceof THREE.Texture ) {
  9678. uniforms_dst[ u ][ p ] = parameter_src.clone();
  9679. } else if ( parameter_src instanceof Array ) {
  9680. uniforms_dst[ u ][ p ] = parameter_src.slice();
  9681. } else {
  9682. uniforms_dst[ u ][ p ] = parameter_src;
  9683. }
  9684. }
  9685. }
  9686. return uniforms_dst;
  9687. }
  9688. };
  9689. // File:src/renderers/shaders/UniformsLib.js
  9690. /**
  9691. * Uniforms library for shared webgl shaders
  9692. */
  9693. THREE.UniformsLib = {
  9694. common: {
  9695. "diffuse" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  9696. "opacity" : { type: "f", value: 1.0 },
  9697. "map" : { type: "t", value: null },
  9698. "offsetRepeat" : { type: "v4", value: new THREE.Vector4( 0, 0, 1, 1 ) },
  9699. "lightMap" : { type: "t", value: null },
  9700. "specularMap" : { type: "t", value: null },
  9701. "alphaMap" : { type: "t", value: null },
  9702. "envMap" : { type: "t", value: null },
  9703. "flipEnvMap" : { type: "f", value: - 1 },
  9704. "useRefract" : { type: "i", value: 0 },
  9705. "reflectivity" : { type: "f", value: 1.0 },
  9706. "refractionRatio" : { type: "f", value: 0.98 },
  9707. "combine" : { type: "i", value: 0 },
  9708. "morphTargetInfluences" : { type: "f", value: 0 }
  9709. },
  9710. bump: {
  9711. "bumpMap" : { type: "t", value: null },
  9712. "bumpScale" : { type: "f", value: 1 }
  9713. },
  9714. normalmap: {
  9715. "normalMap" : { type: "t", value: null },
  9716. "normalScale" : { type: "v2", value: new THREE.Vector2( 1, 1 ) }
  9717. },
  9718. fog : {
  9719. "fogDensity" : { type: "f", value: 0.00025 },
  9720. "fogNear" : { type: "f", value: 1 },
  9721. "fogFar" : { type: "f", value: 2000 },
  9722. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  9723. },
  9724. lights: {
  9725. "ambientLightColor" : { type: "fv", value: [] },
  9726. "directionalLightDirection" : { type: "fv", value: [] },
  9727. "directionalLightColor" : { type: "fv", value: [] },
  9728. "hemisphereLightDirection" : { type: "fv", value: [] },
  9729. "hemisphereLightSkyColor" : { type: "fv", value: [] },
  9730. "hemisphereLightGroundColor" : { type: "fv", value: [] },
  9731. "pointLightColor" : { type: "fv", value: [] },
  9732. "pointLightPosition" : { type: "fv", value: [] },
  9733. "pointLightDistance" : { type: "fv1", value: [] },
  9734. "spotLightColor" : { type: "fv", value: [] },
  9735. "spotLightPosition" : { type: "fv", value: [] },
  9736. "spotLightDirection" : { type: "fv", value: [] },
  9737. "spotLightDistance" : { type: "fv1", value: [] },
  9738. "spotLightAngleCos" : { type: "fv1", value: [] },
  9739. "spotLightExponent" : { type: "fv1", value: [] }
  9740. },
  9741. particle: {
  9742. "psColor" : { type: "c", value: new THREE.Color( 0xeeeeee ) },
  9743. "opacity" : { type: "f", value: 1.0 },
  9744. "size" : { type: "f", value: 1.0 },
  9745. "scale" : { type: "f", value: 1.0 },
  9746. "map" : { type: "t", value: null },
  9747. "fogDensity" : { type: "f", value: 0.00025 },
  9748. "fogNear" : { type: "f", value: 1 },
  9749. "fogFar" : { type: "f", value: 2000 },
  9750. "fogColor" : { type: "c", value: new THREE.Color( 0xffffff ) }
  9751. },
  9752. shadowmap: {
  9753. "shadowMap": { type: "tv", value: [] },
  9754. "shadowMapSize": { type: "v2v", value: [] },
  9755. "shadowBias" : { type: "fv1", value: [] },
  9756. "shadowDarkness": { type: "fv1", value: [] },
  9757. "shadowMatrix" : { type: "m4v", value: [] }
  9758. }
  9759. };
  9760. // File:src/renderers/shaders/ShaderLib.js
  9761. /**
  9762. * Webgl Shader Library for three.js
  9763. *
  9764. * @author alteredq / http://alteredqualia.com/
  9765. * @author mrdoob / http://mrdoob.com/
  9766. * @author mikael emtinger / http://gomo.se/
  9767. */
  9768. THREE.ShaderLib = {
  9769. 'basic': {
  9770. uniforms: THREE.UniformsUtils.merge( [
  9771. THREE.UniformsLib[ "common" ],
  9772. THREE.UniformsLib[ "fog" ],
  9773. THREE.UniformsLib[ "shadowmap" ]
  9774. ] ),
  9775. vertexShader: [
  9776. THREE.ShaderChunk[ "map_pars_vertex" ],
  9777. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  9778. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  9779. THREE.ShaderChunk[ "color_pars_vertex" ],
  9780. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  9781. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  9782. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  9783. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  9784. "void main() {",
  9785. THREE.ShaderChunk[ "map_vertex" ],
  9786. THREE.ShaderChunk[ "lightmap_vertex" ],
  9787. THREE.ShaderChunk[ "color_vertex" ],
  9788. THREE.ShaderChunk[ "skinbase_vertex" ],
  9789. " #ifdef USE_ENVMAP",
  9790. THREE.ShaderChunk[ "morphnormal_vertex" ],
  9791. THREE.ShaderChunk[ "skinnormal_vertex" ],
  9792. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  9793. " #endif",
  9794. THREE.ShaderChunk[ "morphtarget_vertex" ],
  9795. THREE.ShaderChunk[ "skinning_vertex" ],
  9796. THREE.ShaderChunk[ "default_vertex" ],
  9797. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  9798. THREE.ShaderChunk[ "worldpos_vertex" ],
  9799. THREE.ShaderChunk[ "envmap_vertex" ],
  9800. THREE.ShaderChunk[ "shadowmap_vertex" ],
  9801. "}"
  9802. ].join("\n"),
  9803. fragmentShader: [
  9804. "uniform vec3 diffuse;",
  9805. "uniform float opacity;",
  9806. THREE.ShaderChunk[ "color_pars_fragment" ],
  9807. THREE.ShaderChunk[ "map_pars_fragment" ],
  9808. THREE.ShaderChunk[ "alphamap_pars_fragment" ],
  9809. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  9810. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  9811. THREE.ShaderChunk[ "fog_pars_fragment" ],
  9812. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  9813. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  9814. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  9815. "void main() {",
  9816. " gl_FragColor = vec4( diffuse, opacity );",
  9817. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  9818. THREE.ShaderChunk[ "map_fragment" ],
  9819. THREE.ShaderChunk[ "alphamap_fragment" ],
  9820. THREE.ShaderChunk[ "alphatest_fragment" ],
  9821. THREE.ShaderChunk[ "specularmap_fragment" ],
  9822. THREE.ShaderChunk[ "lightmap_fragment" ],
  9823. THREE.ShaderChunk[ "color_fragment" ],
  9824. THREE.ShaderChunk[ "envmap_fragment" ],
  9825. THREE.ShaderChunk[ "shadowmap_fragment" ],
  9826. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  9827. THREE.ShaderChunk[ "fog_fragment" ],
  9828. "}"
  9829. ].join("\n")
  9830. },
  9831. 'lambert': {
  9832. uniforms: THREE.UniformsUtils.merge( [
  9833. THREE.UniformsLib[ "common" ],
  9834. THREE.UniformsLib[ "fog" ],
  9835. THREE.UniformsLib[ "lights" ],
  9836. THREE.UniformsLib[ "shadowmap" ],
  9837. {
  9838. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  9839. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  9840. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  9841. }
  9842. ] ),
  9843. vertexShader: [
  9844. "#define LAMBERT",
  9845. "varying vec3 vLightFront;",
  9846. "#ifdef DOUBLE_SIDED",
  9847. " varying vec3 vLightBack;",
  9848. "#endif",
  9849. THREE.ShaderChunk[ "map_pars_vertex" ],
  9850. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  9851. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  9852. THREE.ShaderChunk[ "lights_lambert_pars_vertex" ],
  9853. THREE.ShaderChunk[ "color_pars_vertex" ],
  9854. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  9855. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  9856. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  9857. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  9858. "void main() {",
  9859. THREE.ShaderChunk[ "map_vertex" ],
  9860. THREE.ShaderChunk[ "lightmap_vertex" ],
  9861. THREE.ShaderChunk[ "color_vertex" ],
  9862. THREE.ShaderChunk[ "morphnormal_vertex" ],
  9863. THREE.ShaderChunk[ "skinbase_vertex" ],
  9864. THREE.ShaderChunk[ "skinnormal_vertex" ],
  9865. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  9866. THREE.ShaderChunk[ "morphtarget_vertex" ],
  9867. THREE.ShaderChunk[ "skinning_vertex" ],
  9868. THREE.ShaderChunk[ "default_vertex" ],
  9869. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  9870. THREE.ShaderChunk[ "worldpos_vertex" ],
  9871. THREE.ShaderChunk[ "envmap_vertex" ],
  9872. THREE.ShaderChunk[ "lights_lambert_vertex" ],
  9873. THREE.ShaderChunk[ "shadowmap_vertex" ],
  9874. "}"
  9875. ].join("\n"),
  9876. fragmentShader: [
  9877. "uniform float opacity;",
  9878. "varying vec3 vLightFront;",
  9879. "#ifdef DOUBLE_SIDED",
  9880. " varying vec3 vLightBack;",
  9881. "#endif",
  9882. THREE.ShaderChunk[ "color_pars_fragment" ],
  9883. THREE.ShaderChunk[ "map_pars_fragment" ],
  9884. THREE.ShaderChunk[ "alphamap_pars_fragment" ],
  9885. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  9886. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  9887. THREE.ShaderChunk[ "fog_pars_fragment" ],
  9888. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  9889. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  9890. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  9891. "void main() {",
  9892. " gl_FragColor = vec4( vec3( 1.0 ), opacity );",
  9893. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  9894. THREE.ShaderChunk[ "map_fragment" ],
  9895. THREE.ShaderChunk[ "alphamap_fragment" ],
  9896. THREE.ShaderChunk[ "alphatest_fragment" ],
  9897. THREE.ShaderChunk[ "specularmap_fragment" ],
  9898. " #ifdef DOUBLE_SIDED",
  9899. //"float isFront = float( gl_FrontFacing );",
  9900. //"gl_FragColor.xyz *= isFront * vLightFront + ( 1.0 - isFront ) * vLightBack;",
  9901. " if ( gl_FrontFacing )",
  9902. " gl_FragColor.xyz *= vLightFront;",
  9903. " else",
  9904. " gl_FragColor.xyz *= vLightBack;",
  9905. " #else",
  9906. " gl_FragColor.xyz *= vLightFront;",
  9907. " #endif",
  9908. THREE.ShaderChunk[ "lightmap_fragment" ],
  9909. THREE.ShaderChunk[ "color_fragment" ],
  9910. THREE.ShaderChunk[ "envmap_fragment" ],
  9911. THREE.ShaderChunk[ "shadowmap_fragment" ],
  9912. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  9913. THREE.ShaderChunk[ "fog_fragment" ],
  9914. "}"
  9915. ].join("\n")
  9916. },
  9917. 'phong': {
  9918. uniforms: THREE.UniformsUtils.merge( [
  9919. THREE.UniformsLib[ "common" ],
  9920. THREE.UniformsLib[ "bump" ],
  9921. THREE.UniformsLib[ "normalmap" ],
  9922. THREE.UniformsLib[ "fog" ],
  9923. THREE.UniformsLib[ "lights" ],
  9924. THREE.UniformsLib[ "shadowmap" ],
  9925. {
  9926. "ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
  9927. "emissive" : { type: "c", value: new THREE.Color( 0x000000 ) },
  9928. "specular" : { type: "c", value: new THREE.Color( 0x111111 ) },
  9929. "shininess": { type: "f", value: 30 },
  9930. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  9931. }
  9932. ] ),
  9933. vertexShader: [
  9934. "#define PHONG",
  9935. "varying vec3 vViewPosition;",
  9936. "varying vec3 vNormal;",
  9937. THREE.ShaderChunk[ "map_pars_vertex" ],
  9938. THREE.ShaderChunk[ "lightmap_pars_vertex" ],
  9939. THREE.ShaderChunk[ "envmap_pars_vertex" ],
  9940. THREE.ShaderChunk[ "lights_phong_pars_vertex" ],
  9941. THREE.ShaderChunk[ "color_pars_vertex" ],
  9942. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  9943. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  9944. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  9945. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  9946. "void main() {",
  9947. THREE.ShaderChunk[ "map_vertex" ],
  9948. THREE.ShaderChunk[ "lightmap_vertex" ],
  9949. THREE.ShaderChunk[ "color_vertex" ],
  9950. THREE.ShaderChunk[ "morphnormal_vertex" ],
  9951. THREE.ShaderChunk[ "skinbase_vertex" ],
  9952. THREE.ShaderChunk[ "skinnormal_vertex" ],
  9953. THREE.ShaderChunk[ "defaultnormal_vertex" ],
  9954. " vNormal = normalize( transformedNormal );",
  9955. THREE.ShaderChunk[ "morphtarget_vertex" ],
  9956. THREE.ShaderChunk[ "skinning_vertex" ],
  9957. THREE.ShaderChunk[ "default_vertex" ],
  9958. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  9959. " vViewPosition = -mvPosition.xyz;",
  9960. THREE.ShaderChunk[ "worldpos_vertex" ],
  9961. THREE.ShaderChunk[ "envmap_vertex" ],
  9962. THREE.ShaderChunk[ "lights_phong_vertex" ],
  9963. THREE.ShaderChunk[ "shadowmap_vertex" ],
  9964. "}"
  9965. ].join("\n"),
  9966. fragmentShader: [
  9967. "#define PHONG",
  9968. "uniform vec3 diffuse;",
  9969. "uniform float opacity;",
  9970. "uniform vec3 ambient;",
  9971. "uniform vec3 emissive;",
  9972. "uniform vec3 specular;",
  9973. "uniform float shininess;",
  9974. THREE.ShaderChunk[ "color_pars_fragment" ],
  9975. THREE.ShaderChunk[ "map_pars_fragment" ],
  9976. THREE.ShaderChunk[ "alphamap_pars_fragment" ],
  9977. THREE.ShaderChunk[ "lightmap_pars_fragment" ],
  9978. THREE.ShaderChunk[ "envmap_pars_fragment" ],
  9979. THREE.ShaderChunk[ "fog_pars_fragment" ],
  9980. THREE.ShaderChunk[ "lights_phong_pars_fragment" ],
  9981. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  9982. THREE.ShaderChunk[ "bumpmap_pars_fragment" ],
  9983. THREE.ShaderChunk[ "normalmap_pars_fragment" ],
  9984. THREE.ShaderChunk[ "specularmap_pars_fragment" ],
  9985. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  9986. "void main() {",
  9987. " gl_FragColor = vec4( vec3( 1.0 ), opacity );",
  9988. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  9989. THREE.ShaderChunk[ "map_fragment" ],
  9990. THREE.ShaderChunk[ "alphamap_fragment" ],
  9991. THREE.ShaderChunk[ "alphatest_fragment" ],
  9992. THREE.ShaderChunk[ "specularmap_fragment" ],
  9993. THREE.ShaderChunk[ "lights_phong_fragment" ],
  9994. THREE.ShaderChunk[ "lightmap_fragment" ],
  9995. THREE.ShaderChunk[ "color_fragment" ],
  9996. THREE.ShaderChunk[ "envmap_fragment" ],
  9997. THREE.ShaderChunk[ "shadowmap_fragment" ],
  9998. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  9999. THREE.ShaderChunk[ "fog_fragment" ],
  10000. "}"
  10001. ].join("\n")
  10002. },
  10003. 'particle_basic': {
  10004. uniforms: THREE.UniformsUtils.merge( [
  10005. THREE.UniformsLib[ "particle" ],
  10006. THREE.UniformsLib[ "shadowmap" ]
  10007. ] ),
  10008. vertexShader: [
  10009. "uniform float size;",
  10010. "uniform float scale;",
  10011. THREE.ShaderChunk[ "color_pars_vertex" ],
  10012. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  10013. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  10014. "void main() {",
  10015. THREE.ShaderChunk[ "color_vertex" ],
  10016. " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  10017. " #ifdef USE_SIZEATTENUATION",
  10018. " gl_PointSize = size * ( scale / length( mvPosition.xyz ) );",
  10019. " #else",
  10020. " gl_PointSize = size;",
  10021. " #endif",
  10022. " gl_Position = projectionMatrix * mvPosition;",
  10023. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  10024. THREE.ShaderChunk[ "worldpos_vertex" ],
  10025. THREE.ShaderChunk[ "shadowmap_vertex" ],
  10026. "}"
  10027. ].join("\n"),
  10028. fragmentShader: [
  10029. "uniform vec3 psColor;",
  10030. "uniform float opacity;",
  10031. THREE.ShaderChunk[ "color_pars_fragment" ],
  10032. THREE.ShaderChunk[ "map_particle_pars_fragment" ],
  10033. THREE.ShaderChunk[ "fog_pars_fragment" ],
  10034. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  10035. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  10036. "void main() {",
  10037. " gl_FragColor = vec4( psColor, opacity );",
  10038. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  10039. THREE.ShaderChunk[ "map_particle_fragment" ],
  10040. THREE.ShaderChunk[ "alphatest_fragment" ],
  10041. THREE.ShaderChunk[ "color_fragment" ],
  10042. THREE.ShaderChunk[ "shadowmap_fragment" ],
  10043. THREE.ShaderChunk[ "fog_fragment" ],
  10044. "}"
  10045. ].join("\n")
  10046. },
  10047. 'dashed': {
  10048. uniforms: THREE.UniformsUtils.merge( [
  10049. THREE.UniformsLib[ "common" ],
  10050. THREE.UniformsLib[ "fog" ],
  10051. {
  10052. "scale" : { type: "f", value: 1 },
  10053. "dashSize" : { type: "f", value: 1 },
  10054. "totalSize": { type: "f", value: 2 }
  10055. }
  10056. ] ),
  10057. vertexShader: [
  10058. "uniform float scale;",
  10059. "attribute float lineDistance;",
  10060. "varying float vLineDistance;",
  10061. THREE.ShaderChunk[ "color_pars_vertex" ],
  10062. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  10063. "void main() {",
  10064. THREE.ShaderChunk[ "color_vertex" ],
  10065. " vLineDistance = scale * lineDistance;",
  10066. " vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",
  10067. " gl_Position = projectionMatrix * mvPosition;",
  10068. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  10069. "}"
  10070. ].join("\n"),
  10071. fragmentShader: [
  10072. "uniform vec3 diffuse;",
  10073. "uniform float opacity;",
  10074. "uniform float dashSize;",
  10075. "uniform float totalSize;",
  10076. "varying float vLineDistance;",
  10077. THREE.ShaderChunk[ "color_pars_fragment" ],
  10078. THREE.ShaderChunk[ "fog_pars_fragment" ],
  10079. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  10080. "void main() {",
  10081. " if ( mod( vLineDistance, totalSize ) > dashSize ) {",
  10082. " discard;",
  10083. " }",
  10084. " gl_FragColor = vec4( diffuse, opacity );",
  10085. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  10086. THREE.ShaderChunk[ "color_fragment" ],
  10087. THREE.ShaderChunk[ "fog_fragment" ],
  10088. "}"
  10089. ].join("\n")
  10090. },
  10091. 'depth': {
  10092. uniforms: {
  10093. "mNear": { type: "f", value: 1.0 },
  10094. "mFar" : { type: "f", value: 2000.0 },
  10095. "opacity" : { type: "f", value: 1.0 }
  10096. },
  10097. vertexShader: [
  10098. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  10099. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  10100. "void main() {",
  10101. THREE.ShaderChunk[ "morphtarget_vertex" ],
  10102. THREE.ShaderChunk[ "default_vertex" ],
  10103. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  10104. "}"
  10105. ].join("\n"),
  10106. fragmentShader: [
  10107. "uniform float mNear;",
  10108. "uniform float mFar;",
  10109. "uniform float opacity;",
  10110. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  10111. "void main() {",
  10112. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  10113. " #ifdef USE_LOGDEPTHBUF_EXT",
  10114. " float depth = gl_FragDepthEXT / gl_FragCoord.w;",
  10115. " #else",
  10116. " float depth = gl_FragCoord.z / gl_FragCoord.w;",
  10117. " #endif",
  10118. " float color = 1.0 - smoothstep( mNear, mFar, depth );",
  10119. " gl_FragColor = vec4( vec3( color ), opacity );",
  10120. "}"
  10121. ].join("\n")
  10122. },
  10123. 'normal': {
  10124. uniforms: {
  10125. "opacity" : { type: "f", value: 1.0 }
  10126. },
  10127. vertexShader: [
  10128. "varying vec3 vNormal;",
  10129. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  10130. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  10131. "void main() {",
  10132. " vNormal = normalize( normalMatrix * normal );",
  10133. THREE.ShaderChunk[ "morphtarget_vertex" ],
  10134. THREE.ShaderChunk[ "default_vertex" ],
  10135. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  10136. "}"
  10137. ].join("\n"),
  10138. fragmentShader: [
  10139. "uniform float opacity;",
  10140. "varying vec3 vNormal;",
  10141. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  10142. "void main() {",
  10143. " gl_FragColor = vec4( 0.5 * normalize( vNormal ) + 0.5, opacity );",
  10144. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  10145. "}"
  10146. ].join("\n")
  10147. },
  10148. /* -------------------------------------------------------------------------
  10149. // Normal map shader
  10150. // - Blinn-Phong
  10151. // - normal + diffuse + specular + AO + displacement + reflection + shadow maps
  10152. // - point and directional lights (use with "lights: true" material option)
  10153. ------------------------------------------------------------------------- */
  10154. 'normalmap' : {
  10155. uniforms: THREE.UniformsUtils.merge( [
  10156. THREE.UniformsLib[ "fog" ],
  10157. THREE.UniformsLib[ "lights" ],
  10158. THREE.UniformsLib[ "shadowmap" ],
  10159. {
  10160. "enableAO" : { type: "i", value: 0 },
  10161. "enableDiffuse" : { type: "i", value: 0 },
  10162. "enableSpecular" : { type: "i", value: 0 },
  10163. "enableReflection" : { type: "i", value: 0 },
  10164. "enableDisplacement": { type: "i", value: 0 },
  10165. "tDisplacement": { type: "t", value: null }, // must go first as this is vertex texture
  10166. "tDiffuse" : { type: "t", value: null },
  10167. "tCube" : { type: "t", value: null },
  10168. "tNormal" : { type: "t", value: null },
  10169. "tSpecular" : { type: "t", value: null },
  10170. "tAO" : { type: "t", value: null },
  10171. "uNormalScale": { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  10172. "uDisplacementBias": { type: "f", value: 0.0 },
  10173. "uDisplacementScale": { type: "f", value: 1.0 },
  10174. "diffuse": { type: "c", value: new THREE.Color( 0xffffff ) },
  10175. "specular": { type: "c", value: new THREE.Color( 0x111111 ) },
  10176. "ambient": { type: "c", value: new THREE.Color( 0xffffff ) },
  10177. "shininess": { type: "f", value: 30 },
  10178. "opacity": { type: "f", value: 1 },
  10179. "useRefract": { type: "i", value: 0 },
  10180. "refractionRatio": { type: "f", value: 0.98 },
  10181. "reflectivity": { type: "f", value: 0.5 },
  10182. "uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },
  10183. "uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },
  10184. "wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }
  10185. }
  10186. ] ),
  10187. fragmentShader: [
  10188. "uniform vec3 ambient;",
  10189. "uniform vec3 diffuse;",
  10190. "uniform vec3 specular;",
  10191. "uniform float shininess;",
  10192. "uniform float opacity;",
  10193. "uniform bool enableDiffuse;",
  10194. "uniform bool enableSpecular;",
  10195. "uniform bool enableAO;",
  10196. "uniform bool enableReflection;",
  10197. "uniform sampler2D tDiffuse;",
  10198. "uniform sampler2D tNormal;",
  10199. "uniform sampler2D tSpecular;",
  10200. "uniform sampler2D tAO;",
  10201. "uniform samplerCube tCube;",
  10202. "uniform vec2 uNormalScale;",
  10203. "uniform bool useRefract;",
  10204. "uniform float refractionRatio;",
  10205. "uniform float reflectivity;",
  10206. "varying vec3 vTangent;",
  10207. "varying vec3 vBinormal;",
  10208. "varying vec3 vNormal;",
  10209. "varying vec2 vUv;",
  10210. "uniform vec3 ambientLightColor;",
  10211. "#if MAX_DIR_LIGHTS > 0",
  10212. " uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",
  10213. " uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",
  10214. "#endif",
  10215. "#if MAX_HEMI_LIGHTS > 0",
  10216. " uniform vec3 hemisphereLightSkyColor[ MAX_HEMI_LIGHTS ];",
  10217. " uniform vec3 hemisphereLightGroundColor[ MAX_HEMI_LIGHTS ];",
  10218. " uniform vec3 hemisphereLightDirection[ MAX_HEMI_LIGHTS ];",
  10219. "#endif",
  10220. "#if MAX_POINT_LIGHTS > 0",
  10221. " uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",
  10222. " uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",
  10223. " uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",
  10224. "#endif",
  10225. "#if MAX_SPOT_LIGHTS > 0",
  10226. " uniform vec3 spotLightColor[ MAX_SPOT_LIGHTS ];",
  10227. " uniform vec3 spotLightPosition[ MAX_SPOT_LIGHTS ];",
  10228. " uniform vec3 spotLightDirection[ MAX_SPOT_LIGHTS ];",
  10229. " uniform float spotLightAngleCos[ MAX_SPOT_LIGHTS ];",
  10230. " uniform float spotLightExponent[ MAX_SPOT_LIGHTS ];",
  10231. " uniform float spotLightDistance[ MAX_SPOT_LIGHTS ];",
  10232. "#endif",
  10233. "#ifdef WRAP_AROUND",
  10234. " uniform vec3 wrapRGB;",
  10235. "#endif",
  10236. "varying vec3 vWorldPosition;",
  10237. "varying vec3 vViewPosition;",
  10238. THREE.ShaderChunk[ "shadowmap_pars_fragment" ],
  10239. THREE.ShaderChunk[ "fog_pars_fragment" ],
  10240. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  10241. "void main() {",
  10242. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  10243. " gl_FragColor = vec4( vec3( 1.0 ), opacity );",
  10244. " vec3 specularTex = vec3( 1.0 );",
  10245. " vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",
  10246. " normalTex.xy *= uNormalScale;",
  10247. " normalTex = normalize( normalTex );",
  10248. " if( enableDiffuse ) {",
  10249. " #ifdef GAMMA_INPUT",
  10250. " vec4 texelColor = texture2D( tDiffuse, vUv );",
  10251. " texelColor.xyz *= texelColor.xyz;",
  10252. " gl_FragColor = gl_FragColor * texelColor;",
  10253. " #else",
  10254. " gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",
  10255. " #endif",
  10256. " }",
  10257. " if( enableAO ) {",
  10258. " #ifdef GAMMA_INPUT",
  10259. " vec4 aoColor = texture2D( tAO, vUv );",
  10260. " aoColor.xyz *= aoColor.xyz;",
  10261. " gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",
  10262. " #else",
  10263. " gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",
  10264. " #endif",
  10265. " }",
  10266. THREE.ShaderChunk[ "alphatest_fragment" ],
  10267. " if( enableSpecular )",
  10268. " specularTex = texture2D( tSpecular, vUv ).xyz;",
  10269. " mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",
  10270. " vec3 finalNormal = tsb * normalTex;",
  10271. " #ifdef FLIP_SIDED",
  10272. " finalNormal = -finalNormal;",
  10273. " #endif",
  10274. " vec3 normal = normalize( finalNormal );",
  10275. " vec3 viewPosition = normalize( vViewPosition );",
  10276. // point lights
  10277. " #if MAX_POINT_LIGHTS > 0",
  10278. " vec3 pointDiffuse = vec3( 0.0 );",
  10279. " vec3 pointSpecular = vec3( 0.0 );",
  10280. " for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",
  10281. " vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",
  10282. " vec3 pointVector = lPosition.xyz + vViewPosition.xyz;",
  10283. " float pointDistance = 1.0;",
  10284. " if ( pointLightDistance[ i ] > 0.0 )",
  10285. " pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",
  10286. " pointVector = normalize( pointVector );",
  10287. // diffuse
  10288. " #ifdef WRAP_AROUND",
  10289. " float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",
  10290. " float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",
  10291. " vec3 pointDiffuseWeight = mix( vec3( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",
  10292. " #else",
  10293. " float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",
  10294. " #endif",
  10295. " pointDiffuse += pointDistance * pointLightColor[ i ] * diffuse * pointDiffuseWeight;",
  10296. // specular
  10297. " vec3 pointHalfVector = normalize( pointVector + viewPosition );",
  10298. " float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",
  10299. " float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, shininess ), 0.0 );",
  10300. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  10301. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( pointVector, pointHalfVector ), 0.0 ), 5.0 );",
  10302. " pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",
  10303. " }",
  10304. " #endif",
  10305. // spot lights
  10306. " #if MAX_SPOT_LIGHTS > 0",
  10307. " vec3 spotDiffuse = vec3( 0.0 );",
  10308. " vec3 spotSpecular = vec3( 0.0 );",
  10309. " for ( int i = 0; i < MAX_SPOT_LIGHTS; i ++ ) {",
  10310. " vec4 lPosition = viewMatrix * vec4( spotLightPosition[ i ], 1.0 );",
  10311. " vec3 spotVector = lPosition.xyz + vViewPosition.xyz;",
  10312. " float spotDistance = 1.0;",
  10313. " if ( spotLightDistance[ i ] > 0.0 )",
  10314. " spotDistance = 1.0 - min( ( length( spotVector ) / spotLightDistance[ i ] ), 1.0 );",
  10315. " spotVector = normalize( spotVector );",
  10316. " float spotEffect = dot( spotLightDirection[ i ], normalize( spotLightPosition[ i ] - vWorldPosition ) );",
  10317. " if ( spotEffect > spotLightAngleCos[ i ] ) {",
  10318. " spotEffect = max( pow( max( spotEffect, 0.0 ), spotLightExponent[ i ] ), 0.0 );",
  10319. // diffuse
  10320. " #ifdef WRAP_AROUND",
  10321. " float spotDiffuseWeightFull = max( dot( normal, spotVector ), 0.0 );",
  10322. " float spotDiffuseWeightHalf = max( 0.5 * dot( normal, spotVector ) + 0.5, 0.0 );",
  10323. " vec3 spotDiffuseWeight = mix( vec3( spotDiffuseWeightFull ), vec3( spotDiffuseWeightHalf ), wrapRGB );",
  10324. " #else",
  10325. " float spotDiffuseWeight = max( dot( normal, spotVector ), 0.0 );",
  10326. " #endif",
  10327. " spotDiffuse += spotDistance * spotLightColor[ i ] * diffuse * spotDiffuseWeight * spotEffect;",
  10328. // specular
  10329. " vec3 spotHalfVector = normalize( spotVector + viewPosition );",
  10330. " float spotDotNormalHalf = max( dot( normal, spotHalfVector ), 0.0 );",
  10331. " float spotSpecularWeight = specularTex.r * max( pow( spotDotNormalHalf, shininess ), 0.0 );",
  10332. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  10333. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( spotVector, spotHalfVector ), 0.0 ), 5.0 );",
  10334. " spotSpecular += schlick * spotLightColor[ i ] * spotSpecularWeight * spotDiffuseWeight * spotDistance * specularNormalization * spotEffect;",
  10335. " }",
  10336. " }",
  10337. " #endif",
  10338. // directional lights
  10339. " #if MAX_DIR_LIGHTS > 0",
  10340. " vec3 dirDiffuse = vec3( 0.0 );",
  10341. " vec3 dirSpecular = vec3( 0.0 );",
  10342. " for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",
  10343. " vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",
  10344. " vec3 dirVector = normalize( lDirection.xyz );",
  10345. // diffuse
  10346. " #ifdef WRAP_AROUND",
  10347. " float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",
  10348. " float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",
  10349. " vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",
  10350. " #else",
  10351. " float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",
  10352. " #endif",
  10353. " dirDiffuse += directionalLightColor[ i ] * diffuse * dirDiffuseWeight;",
  10354. // specular
  10355. " vec3 dirHalfVector = normalize( dirVector + viewPosition );",
  10356. " float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",
  10357. " float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, shininess ), 0.0 );",
  10358. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  10359. " vec3 schlick = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( dirVector, dirHalfVector ), 0.0 ), 5.0 );",
  10360. " dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",
  10361. " }",
  10362. " #endif",
  10363. // hemisphere lights
  10364. " #if MAX_HEMI_LIGHTS > 0",
  10365. " vec3 hemiDiffuse = vec3( 0.0 );",
  10366. " vec3 hemiSpecular = vec3( 0.0 );" ,
  10367. " for( int i = 0; i < MAX_HEMI_LIGHTS; i ++ ) {",
  10368. " vec4 lDirection = viewMatrix * vec4( hemisphereLightDirection[ i ], 0.0 );",
  10369. " vec3 lVector = normalize( lDirection.xyz );",
  10370. // diffuse
  10371. " float dotProduct = dot( normal, lVector );",
  10372. " float hemiDiffuseWeight = 0.5 * dotProduct + 0.5;",
  10373. " vec3 hemiColor = mix( hemisphereLightGroundColor[ i ], hemisphereLightSkyColor[ i ], hemiDiffuseWeight );",
  10374. " hemiDiffuse += diffuse * hemiColor;",
  10375. // specular (sky light)
  10376. " vec3 hemiHalfVectorSky = normalize( lVector + viewPosition );",
  10377. " float hemiDotNormalHalfSky = 0.5 * dot( normal, hemiHalfVectorSky ) + 0.5;",
  10378. " float hemiSpecularWeightSky = specularTex.r * max( pow( max( hemiDotNormalHalfSky, 0.0 ), shininess ), 0.0 );",
  10379. // specular (ground light)
  10380. " vec3 lVectorGround = -lVector;",
  10381. " vec3 hemiHalfVectorGround = normalize( lVectorGround + viewPosition );",
  10382. " float hemiDotNormalHalfGround = 0.5 * dot( normal, hemiHalfVectorGround ) + 0.5;",
  10383. " float hemiSpecularWeightGround = specularTex.r * max( pow( max( hemiDotNormalHalfGround, 0.0 ), shininess ), 0.0 );",
  10384. " float dotProductGround = dot( normal, lVectorGround );",
  10385. " float specularNormalization = ( shininess + 2.0 ) / 8.0;",
  10386. " vec3 schlickSky = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVector, hemiHalfVectorSky ), 0.0 ), 5.0 );",
  10387. " vec3 schlickGround = specular + vec3( 1.0 - specular ) * pow( max( 1.0 - dot( lVectorGround, hemiHalfVectorGround ), 0.0 ), 5.0 );",
  10388. " hemiSpecular += hemiColor * specularNormalization * ( schlickSky * hemiSpecularWeightSky * max( dotProduct, 0.0 ) + schlickGround * hemiSpecularWeightGround * max( dotProductGround, 0.0 ) );",
  10389. " }",
  10390. " #endif",
  10391. // all lights contribution summation
  10392. " vec3 totalDiffuse = vec3( 0.0 );",
  10393. " vec3 totalSpecular = vec3( 0.0 );",
  10394. " #if MAX_DIR_LIGHTS > 0",
  10395. " totalDiffuse += dirDiffuse;",
  10396. " totalSpecular += dirSpecular;",
  10397. " #endif",
  10398. " #if MAX_HEMI_LIGHTS > 0",
  10399. " totalDiffuse += hemiDiffuse;",
  10400. " totalSpecular += hemiSpecular;",
  10401. " #endif",
  10402. " #if MAX_POINT_LIGHTS > 0",
  10403. " totalDiffuse += pointDiffuse;",
  10404. " totalSpecular += pointSpecular;",
  10405. " #endif",
  10406. " #if MAX_SPOT_LIGHTS > 0",
  10407. " totalDiffuse += spotDiffuse;",
  10408. " totalSpecular += spotSpecular;",
  10409. " #endif",
  10410. " #ifdef METAL",
  10411. " gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient + totalSpecular );",
  10412. " #else",
  10413. " gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * ambient ) + totalSpecular;",
  10414. " #endif",
  10415. " if ( enableReflection ) {",
  10416. " vec3 vReflect;",
  10417. " vec3 cameraToVertex = normalize( vWorldPosition - cameraPosition );",
  10418. " if ( useRefract ) {",
  10419. " vReflect = refract( cameraToVertex, normal, refractionRatio );",
  10420. " } else {",
  10421. " vReflect = reflect( cameraToVertex, normal );",
  10422. " }",
  10423. " vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",
  10424. " #ifdef GAMMA_INPUT",
  10425. " cubeColor.xyz *= cubeColor.xyz;",
  10426. " #endif",
  10427. " gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * reflectivity );",
  10428. " }",
  10429. THREE.ShaderChunk[ "shadowmap_fragment" ],
  10430. THREE.ShaderChunk[ "linear_to_gamma_fragment" ],
  10431. THREE.ShaderChunk[ "fog_fragment" ],
  10432. "}"
  10433. ].join("\n"),
  10434. vertexShader: [
  10435. "attribute vec4 tangent;",
  10436. "uniform vec2 uOffset;",
  10437. "uniform vec2 uRepeat;",
  10438. "uniform bool enableDisplacement;",
  10439. "#ifdef VERTEX_TEXTURES",
  10440. " uniform sampler2D tDisplacement;",
  10441. " uniform float uDisplacementScale;",
  10442. " uniform float uDisplacementBias;",
  10443. "#endif",
  10444. "varying vec3 vTangent;",
  10445. "varying vec3 vBinormal;",
  10446. "varying vec3 vNormal;",
  10447. "varying vec2 vUv;",
  10448. "varying vec3 vWorldPosition;",
  10449. "varying vec3 vViewPosition;",
  10450. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  10451. THREE.ShaderChunk[ "shadowmap_pars_vertex" ],
  10452. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  10453. "void main() {",
  10454. THREE.ShaderChunk[ "skinbase_vertex" ],
  10455. THREE.ShaderChunk[ "skinnormal_vertex" ],
  10456. // normal, tangent and binormal vectors
  10457. " #ifdef USE_SKINNING",
  10458. " vNormal = normalize( normalMatrix * skinnedNormal.xyz );",
  10459. " vec4 skinnedTangent = skinMatrix * vec4( tangent.xyz, 0.0 );",
  10460. " vTangent = normalize( normalMatrix * skinnedTangent.xyz );",
  10461. " #else",
  10462. " vNormal = normalize( normalMatrix * normal );",
  10463. " vTangent = normalize( normalMatrix * tangent.xyz );",
  10464. " #endif",
  10465. " vBinormal = normalize( cross( vNormal, vTangent ) * tangent.w );",
  10466. " vUv = uv * uRepeat + uOffset;",
  10467. // displacement mapping
  10468. " vec3 displacedPosition;",
  10469. " #ifdef VERTEX_TEXTURES",
  10470. " if ( enableDisplacement ) {",
  10471. " vec3 dv = texture2D( tDisplacement, uv ).xyz;",
  10472. " float df = uDisplacementScale * dv.x + uDisplacementBias;",
  10473. " displacedPosition = position + normalize( normal ) * df;",
  10474. " } else {",
  10475. " #ifdef USE_SKINNING",
  10476. " vec4 skinVertex = bindMatrix * vec4( position, 1.0 );",
  10477. " vec4 skinned = vec4( 0.0 );",
  10478. " skinned += boneMatX * skinVertex * skinWeight.x;",
  10479. " skinned += boneMatY * skinVertex * skinWeight.y;",
  10480. " skinned += boneMatZ * skinVertex * skinWeight.z;",
  10481. " skinned += boneMatW * skinVertex * skinWeight.w;",
  10482. " skinned = bindMatrixInverse * skinned;",
  10483. " displacedPosition = skinned.xyz;",
  10484. " #else",
  10485. " displacedPosition = position;",
  10486. " #endif",
  10487. " }",
  10488. " #else",
  10489. " #ifdef USE_SKINNING",
  10490. " vec4 skinVertex = bindMatrix * vec4( position, 1.0 );",
  10491. " vec4 skinned = vec4( 0.0 );",
  10492. " skinned += boneMatX * skinVertex * skinWeight.x;",
  10493. " skinned += boneMatY * skinVertex * skinWeight.y;",
  10494. " skinned += boneMatZ * skinVertex * skinWeight.z;",
  10495. " skinned += boneMatW * skinVertex * skinWeight.w;",
  10496. " skinned = bindMatrixInverse * skinned;",
  10497. " displacedPosition = skinned.xyz;",
  10498. " #else",
  10499. " displacedPosition = position;",
  10500. " #endif",
  10501. " #endif",
  10502. //
  10503. " vec4 mvPosition = modelViewMatrix * vec4( displacedPosition, 1.0 );",
  10504. " vec4 worldPosition = modelMatrix * vec4( displacedPosition, 1.0 );",
  10505. " gl_Position = projectionMatrix * mvPosition;",
  10506. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  10507. //
  10508. " vWorldPosition = worldPosition.xyz;",
  10509. " vViewPosition = -mvPosition.xyz;",
  10510. // shadows
  10511. " #ifdef USE_SHADOWMAP",
  10512. " for( int i = 0; i < MAX_SHADOWS; i ++ ) {",
  10513. " vShadowCoord[ i ] = shadowMatrix[ i ] * worldPosition;",
  10514. " }",
  10515. " #endif",
  10516. "}"
  10517. ].join("\n")
  10518. },
  10519. /* -------------------------------------------------------------------------
  10520. // Cube map shader
  10521. ------------------------------------------------------------------------- */
  10522. 'cube': {
  10523. uniforms: { "tCube": { type: "t", value: null },
  10524. "tFlip": { type: "f", value: - 1 } },
  10525. vertexShader: [
  10526. "varying vec3 vWorldPosition;",
  10527. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  10528. "void main() {",
  10529. " vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  10530. " vWorldPosition = worldPosition.xyz;",
  10531. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  10532. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  10533. "}"
  10534. ].join("\n"),
  10535. fragmentShader: [
  10536. "uniform samplerCube tCube;",
  10537. "uniform float tFlip;",
  10538. "varying vec3 vWorldPosition;",
  10539. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  10540. "void main() {",
  10541. " gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );",
  10542. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  10543. "}"
  10544. ].join("\n")
  10545. },
  10546. /* -------------------------------------------------------------------------
  10547. // Cube map shader
  10548. ------------------------------------------------------------------------- */
  10549. 'equirect': {
  10550. uniforms: { "tEquirect": { type: "t", value: null },
  10551. "tFlip": { type: "f", value: - 1 } },
  10552. vertexShader: [
  10553. "varying vec3 vWorldPosition;",
  10554. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  10555. "void main() {",
  10556. " vec4 worldPosition = modelMatrix * vec4( position, 1.0 );",
  10557. " vWorldPosition = worldPosition.xyz;",
  10558. " gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",
  10559. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  10560. "}"
  10561. ].join("\n"),
  10562. fragmentShader: [
  10563. "uniform sampler2D tEquirect;",
  10564. "uniform float tFlip;",
  10565. "varying vec3 vWorldPosition;",
  10566. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  10567. "void main() {",
  10568. // " gl_FragColor = textureCube( tCube, vec3( tFlip * vWorldPosition.x, vWorldPosition.yz ) );",
  10569. "vec3 direction = normalize( vWorldPosition );",
  10570. "vec2 sampleUV;",
  10571. "sampleUV.y = clamp( tFlip * direction.y * -0.5 + 0.5, 0.0, 1.0);",
  10572. "sampleUV.x = atan( direction.z, direction.x ) * 0.15915494309189533576888376337251 + 0.5;", // reciprocal( 2 PI ) + 0.5
  10573. "gl_FragColor = texture2D( tEquirect, sampleUV );",
  10574. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  10575. "}"
  10576. ].join("\n")
  10577. },
  10578. /* Depth encoding into RGBA texture
  10579. *
  10580. * based on SpiderGL shadow map example
  10581. * http://spidergl.org/example.php?id=6
  10582. *
  10583. * originally from
  10584. * http://www.gamedev.net/topic/442138-packing-a-float-into-a-a8r8g8b8-texture-shader/page__whichpage__1%25EF%25BF%25BD
  10585. *
  10586. * see also
  10587. * http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/
  10588. */
  10589. 'depthRGBA': {
  10590. uniforms: {},
  10591. vertexShader: [
  10592. THREE.ShaderChunk[ "morphtarget_pars_vertex" ],
  10593. THREE.ShaderChunk[ "skinning_pars_vertex" ],
  10594. THREE.ShaderChunk[ "logdepthbuf_pars_vertex" ],
  10595. "void main() {",
  10596. THREE.ShaderChunk[ "skinbase_vertex" ],
  10597. THREE.ShaderChunk[ "morphtarget_vertex" ],
  10598. THREE.ShaderChunk[ "skinning_vertex" ],
  10599. THREE.ShaderChunk[ "default_vertex" ],
  10600. THREE.ShaderChunk[ "logdepthbuf_vertex" ],
  10601. "}"
  10602. ].join("\n"),
  10603. fragmentShader: [
  10604. THREE.ShaderChunk[ "logdepthbuf_pars_fragment" ],
  10605. "vec4 pack_depth( const in float depth ) {",
  10606. " const vec4 bit_shift = vec4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0 );",
  10607. " const vec4 bit_mask = vec4( 0.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0 );",
  10608. " vec4 res = mod( depth * bit_shift * vec4( 255 ), vec4( 256 ) ) / vec4( 255 );", // " vec4 res = fract( depth * bit_shift );",
  10609. " res -= res.xxyz * bit_mask;",
  10610. " return res;",
  10611. "}",
  10612. "void main() {",
  10613. THREE.ShaderChunk[ "logdepthbuf_fragment" ],
  10614. " #ifdef USE_LOGDEPTHBUF_EXT",
  10615. " gl_FragData[ 0 ] = pack_depth( gl_FragDepthEXT );",
  10616. " #else",
  10617. " gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z );",
  10618. " #endif",
  10619. //"gl_FragData[ 0 ] = pack_depth( gl_FragCoord.z / gl_FragCoord.w );",
  10620. //"float z = ( ( gl_FragCoord.z / gl_FragCoord.w ) - 3.0 ) / ( 4000.0 - 3.0 );",
  10621. //"gl_FragData[ 0 ] = pack_depth( z );",
  10622. //"gl_FragData[ 0 ] = vec4( z, z, z, 1.0 );",
  10623. "}"
  10624. ].join("\n")
  10625. }
  10626. };
  10627. // File:src/renderers/WebGLRenderer.js
  10628. /**
  10629. * @author supereggbert / http://www.paulbrunt.co.uk/
  10630. * @author mrdoob / http://mrdoob.com/
  10631. * @author alteredq / http://alteredqualia.com/
  10632. * @author szimek / https://github.com/szimek/
  10633. */
  10634. THREE.WebGLRenderer = function ( parameters ) {
  10635. console.log( 'THREE.WebGLRenderer', THREE.REVISION );
  10636. parameters = parameters || {};
  10637. var _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
  10638. _context = parameters.context !== undefined ? parameters.context : null,
  10639. _precision = parameters.precision !== undefined ? parameters.precision : 'highp',
  10640. _alpha = parameters.alpha !== undefined ? parameters.alpha : false,
  10641. _depth = parameters.depth !== undefined ? parameters.depth : true,
  10642. _stencil = parameters.stencil !== undefined ? parameters.stencil : true,
  10643. _antialias = parameters.antialias !== undefined ? parameters.antialias : false,
  10644. _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true,
  10645. _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false,
  10646. _logarithmicDepthBuffer = parameters.logarithmicDepthBuffer !== undefined ? parameters.logarithmicDepthBuffer : false,
  10647. _clearColor = new THREE.Color( 0x000000 ),
  10648. _clearAlpha = 0;
  10649. var lights = [];
  10650. var _webglObjects = {};
  10651. var _webglObjectsImmediate = [];
  10652. var opaqueObjects = [];
  10653. var transparentObjects = [];
  10654. var sprites = [];
  10655. var lensFlares = [];
  10656. // public properties
  10657. this.domElement = _canvas;
  10658. this.context = null;
  10659. this.devicePixelRatio = self.devicePixelRatio !== undefined ? self.devicePixelRatio : 1;
  10660. // clearing
  10661. this.autoClear = true;
  10662. this.autoClearColor = true;
  10663. this.autoClearDepth = true;
  10664. this.autoClearStencil = true;
  10665. // scene graph
  10666. this.sortObjects = true;
  10667. // physically based shading
  10668. this.gammaInput = false;
  10669. this.gammaOutput = false;
  10670. // shadow map
  10671. this.shadowMapEnabled = false;
  10672. this.shadowMapType = THREE.PCFShadowMap;
  10673. this.shadowMapCullFace = THREE.CullFaceFront;
  10674. this.shadowMapDebug = false;
  10675. this.shadowMapCascade = false;
  10676. // morphs
  10677. this.maxMorphTargets = 8;
  10678. this.maxMorphNormals = 4;
  10679. // flags
  10680. this.autoScaleCubemaps = true;
  10681. // info
  10682. this.info = {
  10683. memory: {
  10684. programs: 0,
  10685. geometries: 0,
  10686. textures: 0
  10687. },
  10688. render: {
  10689. calls: 0,
  10690. vertices: 0,
  10691. faces: 0,
  10692. points: 0
  10693. }
  10694. };
  10695. // internal properties
  10696. var _this = this,
  10697. _programs = [],
  10698. // internal state cache
  10699. _currentProgram = null,
  10700. _currentFramebuffer = null,
  10701. _currentMaterialId = - 1,
  10702. _currentGeometryGroupHash = - 1,
  10703. _currentCamera = null,
  10704. _usedTextureUnits = 0,
  10705. // GL state cache
  10706. _oldDoubleSided = - 1,
  10707. _oldFlipSided = - 1,
  10708. _oldBlending = - 1,
  10709. _oldBlendEquation = - 1,
  10710. _oldBlendSrc = - 1,
  10711. _oldBlendDst = - 1,
  10712. _oldDepthTest = - 1,
  10713. _oldDepthWrite = - 1,
  10714. _oldPolygonOffset = null,
  10715. _oldPolygonOffsetFactor = null,
  10716. _oldPolygonOffsetUnits = null,
  10717. _oldLineWidth = null,
  10718. _viewportX = 0,
  10719. _viewportY = 0,
  10720. _viewportWidth = _canvas.width,
  10721. _viewportHeight = _canvas.height,
  10722. _currentWidth = 0,
  10723. _currentHeight = 0,
  10724. _newAttributes = new Uint8Array( 16 ),
  10725. _enabledAttributes = new Uint8Array( 16 ),
  10726. // frustum
  10727. _frustum = new THREE.Frustum(),
  10728. // camera matrices cache
  10729. _projScreenMatrix = new THREE.Matrix4(),
  10730. _projScreenMatrixPS = new THREE.Matrix4(),
  10731. _vector3 = new THREE.Vector3(),
  10732. // light arrays cache
  10733. _direction = new THREE.Vector3(),
  10734. _lightsNeedUpdate = true,
  10735. _lights = {
  10736. ambient: [ 0, 0, 0 ],
  10737. directional: { length: 0, colors:[], positions: [] },
  10738. point: { length: 0, colors: [], positions: [], distances: [] },
  10739. spot: { length: 0, colors: [], positions: [], distances: [], directions: [], anglesCos: [], exponents: [] },
  10740. hemi: { length: 0, skyColors: [], groundColors: [], positions: [] }
  10741. };
  10742. // initialize
  10743. var _gl;
  10744. try {
  10745. var attributes = {
  10746. alpha: _alpha,
  10747. depth: _depth,
  10748. stencil: _stencil,
  10749. antialias: _antialias,
  10750. premultipliedAlpha: _premultipliedAlpha,
  10751. preserveDrawingBuffer: _preserveDrawingBuffer
  10752. };
  10753. _gl = _context || _canvas.getContext( 'webgl', attributes ) || _canvas.getContext( 'experimental-webgl', attributes );
  10754. if ( _gl === null ) {
  10755. if ( _canvas.getContext( 'webgl') !== null ) {
  10756. throw 'Error creating WebGL context with your selected attributes.';
  10757. } else {
  10758. throw 'Error creating WebGL context.';
  10759. }
  10760. }
  10761. _canvas.addEventListener( 'webglcontextlost', function ( event ) {
  10762. event.preventDefault();
  10763. resetGLState();
  10764. setDefaultGLState();
  10765. _webglObjects = {};
  10766. }, false);
  10767. } catch ( error ) {
  10768. console.error( error );
  10769. }
  10770. if ( _gl.getShaderPrecisionFormat === undefined ) {
  10771. _gl.getShaderPrecisionFormat = function () {
  10772. return {
  10773. 'rangeMin': 1,
  10774. 'rangeMax': 1,
  10775. 'precision': 1
  10776. };
  10777. }
  10778. }
  10779. var extensions = new THREE.WebGLExtensions( _gl );
  10780. extensions.get( 'OES_texture_float' );
  10781. extensions.get( 'OES_texture_float_linear' );
  10782. extensions.get( 'OES_standard_derivatives' );
  10783. if ( _logarithmicDepthBuffer ) {
  10784. extensions.get( 'EXT_frag_depth' );
  10785. }
  10786. //
  10787. var setDefaultGLState = function () {
  10788. _gl.clearColor( 0, 0, 0, 1 );
  10789. _gl.clearDepth( 1 );
  10790. _gl.clearStencil( 0 );
  10791. _gl.enable( _gl.DEPTH_TEST );
  10792. _gl.depthFunc( _gl.LEQUAL );
  10793. _gl.frontFace( _gl.CCW );
  10794. _gl.cullFace( _gl.BACK );
  10795. _gl.enable( _gl.CULL_FACE );
  10796. _gl.enable( _gl.BLEND );
  10797. _gl.blendEquation( _gl.FUNC_ADD );
  10798. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA );
  10799. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  10800. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  10801. };
  10802. var resetGLState = function () {
  10803. _currentProgram = null;
  10804. _currentCamera = null;
  10805. _oldBlending = - 1;
  10806. _oldDepthTest = - 1;
  10807. _oldDepthWrite = - 1;
  10808. _oldDoubleSided = - 1;
  10809. _oldFlipSided = - 1;
  10810. _currentGeometryGroupHash = - 1;
  10811. _currentMaterialId = - 1;
  10812. _lightsNeedUpdate = true;
  10813. for ( var i = 0; i < _enabledAttributes.length; i ++ ) {
  10814. _enabledAttributes[ i ] = 0;
  10815. }
  10816. };
  10817. setDefaultGLState();
  10818. this.context = _gl;
  10819. // GPU capabilities
  10820. var _maxTextures = _gl.getParameter( _gl.MAX_TEXTURE_IMAGE_UNITS );
  10821. var _maxVertexTextures = _gl.getParameter( _gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS );
  10822. var _maxTextureSize = _gl.getParameter( _gl.MAX_TEXTURE_SIZE );
  10823. var _maxCubemapSize = _gl.getParameter( _gl.MAX_CUBE_MAP_TEXTURE_SIZE );
  10824. var _supportsVertexTextures = _maxVertexTextures > 0;
  10825. var _supportsBoneTextures = _supportsVertexTextures && extensions.get( 'OES_texture_float' );
  10826. //
  10827. var _vertexShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.HIGH_FLOAT );
  10828. var _vertexShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.MEDIUM_FLOAT );
  10829. var _vertexShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.VERTEX_SHADER, _gl.LOW_FLOAT );
  10830. var _fragmentShaderPrecisionHighpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.HIGH_FLOAT );
  10831. var _fragmentShaderPrecisionMediumpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.MEDIUM_FLOAT );
  10832. var _fragmentShaderPrecisionLowpFloat = _gl.getShaderPrecisionFormat( _gl.FRAGMENT_SHADER, _gl.LOW_FLOAT );
  10833. var getCompressedTextureFormats = ( function () {
  10834. var array;
  10835. return function () {
  10836. if ( array !== undefined ) {
  10837. return array;
  10838. }
  10839. array = [];
  10840. if ( extensions.get( 'WEBGL_compressed_texture_pvrtc' ) || extensions.get( 'WEBGL_compressed_texture_s3tc' ) ) {
  10841. var formats = _gl.getParameter( _gl.COMPRESSED_TEXTURE_FORMATS );
  10842. for ( var i = 0; i < formats.length; i ++ ){
  10843. array.push( formats[ i ] );
  10844. }
  10845. }
  10846. return array;
  10847. };
  10848. } )();
  10849. // clamp precision to maximum available
  10850. var highpAvailable = _vertexShaderPrecisionHighpFloat.precision > 0 && _fragmentShaderPrecisionHighpFloat.precision > 0;
  10851. var mediumpAvailable = _vertexShaderPrecisionMediumpFloat.precision > 0 && _fragmentShaderPrecisionMediumpFloat.precision > 0;
  10852. if ( _precision === 'highp' && ! highpAvailable ) {
  10853. if ( mediumpAvailable ) {
  10854. _precision = 'mediump';
  10855. console.warn( 'THREE.WebGLRenderer: highp not supported, using mediump.' );
  10856. } else {
  10857. _precision = 'lowp';
  10858. console.warn( 'THREE.WebGLRenderer: highp and mediump not supported, using lowp.' );
  10859. }
  10860. }
  10861. if ( _precision === 'mediump' && ! mediumpAvailable ) {
  10862. _precision = 'lowp';
  10863. console.warn( 'THREE.WebGLRenderer: mediump not supported, using lowp.' );
  10864. }
  10865. // Plugins
  10866. var shadowMapPlugin = new THREE.ShadowMapPlugin( this, lights, _webglObjects, _webglObjectsImmediate );
  10867. var spritePlugin = new THREE.SpritePlugin( this, sprites );
  10868. var lensFlarePlugin = new THREE.LensFlarePlugin( this, lensFlares );
  10869. // API
  10870. this.getContext = function () {
  10871. return _gl;
  10872. };
  10873. this.forceContextLoss = function () {
  10874. extensions.get( 'WEBGL_lose_context' ).loseContext();
  10875. };
  10876. this.supportsVertexTextures = function () {
  10877. return _supportsVertexTextures;
  10878. };
  10879. this.supportsFloatTextures = function () {
  10880. return extensions.get( 'OES_texture_float' );
  10881. };
  10882. this.supportsStandardDerivatives = function () {
  10883. return extensions.get( 'OES_standard_derivatives' );
  10884. };
  10885. this.supportsCompressedTextureS3TC = function () {
  10886. return extensions.get( 'WEBGL_compressed_texture_s3tc' );
  10887. };
  10888. this.supportsCompressedTexturePVRTC = function () {
  10889. return extensions.get( 'WEBGL_compressed_texture_pvrtc' );
  10890. };
  10891. this.supportsBlendMinMax = function () {
  10892. return extensions.get( 'EXT_blend_minmax' );
  10893. };
  10894. this.getMaxAnisotropy = ( function () {
  10895. var value;
  10896. return function () {
  10897. if ( value !== undefined ) {
  10898. return value;
  10899. }
  10900. var extension = extensions.get( 'EXT_texture_filter_anisotropic' );
  10901. value = extension !== null ? _gl.getParameter( extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT ) : 0;
  10902. return value;
  10903. }
  10904. } )();
  10905. this.getPrecision = function () {
  10906. return _precision;
  10907. };
  10908. this.setSize = function ( width, height, updateStyle ) {
  10909. _canvas.width = width * this.devicePixelRatio;
  10910. _canvas.height = height * this.devicePixelRatio;
  10911. if ( updateStyle !== false ) {
  10912. _canvas.style.width = width + 'px';
  10913. _canvas.style.height = height + 'px';
  10914. }
  10915. this.setViewport( 0, 0, width, height );
  10916. };
  10917. this.setViewport = function ( x, y, width, height ) {
  10918. _viewportX = x * this.devicePixelRatio;
  10919. _viewportY = y * this.devicePixelRatio;
  10920. _viewportWidth = width * this.devicePixelRatio;
  10921. _viewportHeight = height * this.devicePixelRatio;
  10922. _gl.viewport( _viewportX, _viewportY, _viewportWidth, _viewportHeight );
  10923. };
  10924. this.setScissor = function ( x, y, width, height ) {
  10925. _gl.scissor(
  10926. x * this.devicePixelRatio,
  10927. y * this.devicePixelRatio,
  10928. width * this.devicePixelRatio,
  10929. height * this.devicePixelRatio
  10930. );
  10931. };
  10932. this.enableScissorTest = function ( enable ) {
  10933. enable ? _gl.enable( _gl.SCISSOR_TEST ) : _gl.disable( _gl.SCISSOR_TEST );
  10934. };
  10935. // Clearing
  10936. this.setClearColor = function ( color, alpha ) {
  10937. _clearColor.set( color );
  10938. _clearAlpha = alpha !== undefined ? alpha : 1;
  10939. _gl.clearColor( _clearColor.r, _clearColor.g, _clearColor.b, _clearAlpha );
  10940. };
  10941. this.setClearColorHex = function ( hex, alpha ) {
  10942. console.warn( 'THREE.WebGLRenderer: .setClearColorHex() is being removed. Use .setClearColor() instead.' );
  10943. this.setClearColor( hex, alpha );
  10944. };
  10945. this.getClearColor = function () {
  10946. return _clearColor;
  10947. };
  10948. this.getClearAlpha = function () {
  10949. return _clearAlpha;
  10950. };
  10951. this.clear = function ( color, depth, stencil ) {
  10952. var bits = 0;
  10953. if ( color === undefined || color ) bits |= _gl.COLOR_BUFFER_BIT;
  10954. if ( depth === undefined || depth ) bits |= _gl.DEPTH_BUFFER_BIT;
  10955. if ( stencil === undefined || stencil ) bits |= _gl.STENCIL_BUFFER_BIT;
  10956. _gl.clear( bits );
  10957. };
  10958. this.clearColor = function () {
  10959. _gl.clear( _gl.COLOR_BUFFER_BIT );
  10960. };
  10961. this.clearDepth = function () {
  10962. _gl.clear( _gl.DEPTH_BUFFER_BIT );
  10963. };
  10964. this.clearStencil = function () {
  10965. _gl.clear( _gl.STENCIL_BUFFER_BIT );
  10966. };
  10967. this.clearTarget = function ( renderTarget, color, depth, stencil ) {
  10968. this.setRenderTarget( renderTarget );
  10969. this.clear( color, depth, stencil );
  10970. };
  10971. // Reset
  10972. this.resetGLState = resetGLState;
  10973. // Buffer allocation
  10974. function createParticleBuffers ( geometry ) {
  10975. geometry.__webglVertexBuffer = _gl.createBuffer();
  10976. geometry.__webglColorBuffer = _gl.createBuffer();
  10977. _this.info.memory.geometries ++;
  10978. };
  10979. function createLineBuffers ( geometry ) {
  10980. geometry.__webglVertexBuffer = _gl.createBuffer();
  10981. geometry.__webglColorBuffer = _gl.createBuffer();
  10982. geometry.__webglLineDistanceBuffer = _gl.createBuffer();
  10983. _this.info.memory.geometries ++;
  10984. };
  10985. function createMeshBuffers ( geometryGroup ) {
  10986. geometryGroup.__webglVertexBuffer = _gl.createBuffer();
  10987. geometryGroup.__webglNormalBuffer = _gl.createBuffer();
  10988. geometryGroup.__webglTangentBuffer = _gl.createBuffer();
  10989. geometryGroup.__webglColorBuffer = _gl.createBuffer();
  10990. geometryGroup.__webglUVBuffer = _gl.createBuffer();
  10991. geometryGroup.__webglUV2Buffer = _gl.createBuffer();
  10992. geometryGroup.__webglSkinIndicesBuffer = _gl.createBuffer();
  10993. geometryGroup.__webglSkinWeightsBuffer = _gl.createBuffer();
  10994. geometryGroup.__webglFaceBuffer = _gl.createBuffer();
  10995. geometryGroup.__webglLineBuffer = _gl.createBuffer();
  10996. var m, ml;
  10997. if ( geometryGroup.numMorphTargets ) {
  10998. geometryGroup.__webglMorphTargetsBuffers = [];
  10999. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  11000. geometryGroup.__webglMorphTargetsBuffers.push( _gl.createBuffer() );
  11001. }
  11002. }
  11003. if ( geometryGroup.numMorphNormals ) {
  11004. geometryGroup.__webglMorphNormalsBuffers = [];
  11005. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  11006. geometryGroup.__webglMorphNormalsBuffers.push( _gl.createBuffer() );
  11007. }
  11008. }
  11009. _this.info.memory.geometries ++;
  11010. };
  11011. // Events
  11012. var onObjectRemoved = function ( event ) {
  11013. var object = event.target;
  11014. object.traverse( function ( child ) {
  11015. child.removeEventListener( 'remove', onObjectRemoved );
  11016. removeObject( child );
  11017. } );
  11018. };
  11019. var onGeometryDispose = function ( event ) {
  11020. var geometry = event.target;
  11021. geometry.removeEventListener( 'dispose', onGeometryDispose );
  11022. deallocateGeometry( geometry );
  11023. };
  11024. var onTextureDispose = function ( event ) {
  11025. var texture = event.target;
  11026. texture.removeEventListener( 'dispose', onTextureDispose );
  11027. deallocateTexture( texture );
  11028. _this.info.memory.textures --;
  11029. };
  11030. var onRenderTargetDispose = function ( event ) {
  11031. var renderTarget = event.target;
  11032. renderTarget.removeEventListener( 'dispose', onRenderTargetDispose );
  11033. deallocateRenderTarget( renderTarget );
  11034. _this.info.memory.textures --;
  11035. };
  11036. var onMaterialDispose = function ( event ) {
  11037. var material = event.target;
  11038. material.removeEventListener( 'dispose', onMaterialDispose );
  11039. deallocateMaterial( material );
  11040. };
  11041. // Buffer deallocation
  11042. var deleteBuffers = function ( geometry ) {
  11043. var buffers = [
  11044. '__webglVertexBuffer',
  11045. '__webglNormalBuffer',
  11046. '__webglTangentBuffer',
  11047. '__webglColorBuffer',
  11048. '__webglUVBuffer',
  11049. '__webglUV2Buffer',
  11050. '__webglSkinIndicesBuffer',
  11051. '__webglSkinWeightsBuffer',
  11052. '__webglFaceBuffer',
  11053. '__webglLineBuffer',
  11054. '__webglLineDistanceBuffer'
  11055. ];
  11056. for ( var i = 0, l = buffers.length; i < l; i ++ ) {
  11057. var name = buffers[ i ];
  11058. if ( geometry[ name ] !== undefined ) {
  11059. _gl.deleteBuffer( geometry[ name ] );
  11060. delete geometry[ name ];
  11061. }
  11062. }
  11063. // custom attributes
  11064. if ( geometry.__webglCustomAttributesList !== undefined ) {
  11065. for ( var name in geometry.__webglCustomAttributesList ) {
  11066. _gl.deleteBuffer( geometry.__webglCustomAttributesList[ name ].buffer );
  11067. }
  11068. delete geometry.__webglCustomAttributesList;
  11069. }
  11070. _this.info.memory.geometries --;
  11071. };
  11072. var deallocateGeometry = function ( geometry ) {
  11073. delete geometry.__webglInit;
  11074. if ( geometry instanceof THREE.BufferGeometry ) {
  11075. for ( var name in geometry.attributes ) {
  11076. var attribute = geometry.attributes[ name ];
  11077. if ( attribute.buffer !== undefined ) {
  11078. _gl.deleteBuffer( attribute.buffer );
  11079. delete attribute.buffer;
  11080. }
  11081. }
  11082. _this.info.memory.geometries --;
  11083. } else {
  11084. var geometryGroupsList = geometryGroups[ geometry.id ];
  11085. if ( geometryGroupsList !== undefined ) {
  11086. for ( var i = 0,l = geometryGroupsList.length; i < l; i ++ ) {
  11087. var geometryGroup = geometryGroupsList[ i ];
  11088. if ( geometryGroup.numMorphTargets !== undefined ) {
  11089. for ( var m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  11090. _gl.deleteBuffer( geometryGroup.__webglMorphTargetsBuffers[ m ] );
  11091. }
  11092. delete geometryGroup.__webglMorphTargetsBuffers;
  11093. }
  11094. if ( geometryGroup.numMorphNormals !== undefined ) {
  11095. for ( var m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  11096. _gl.deleteBuffer( geometryGroup.__webglMorphNormalsBuffers[ m ] );
  11097. }
  11098. delete geometryGroup.__webglMorphNormalsBuffers;
  11099. }
  11100. deleteBuffers( geometryGroup );
  11101. }
  11102. delete geometryGroups[ geometry.id ];
  11103. } else {
  11104. deleteBuffers( geometry );
  11105. }
  11106. }
  11107. // TOFIX: Workaround for deleted geometry being currently bound
  11108. _currentGeometryGroupHash = - 1;
  11109. };
  11110. var deallocateTexture = function ( texture ) {
  11111. if ( texture.image && texture.image.__webglTextureCube ) {
  11112. // cube texture
  11113. _gl.deleteTexture( texture.image.__webglTextureCube );
  11114. delete texture.image.__webglTextureCube;
  11115. } else {
  11116. // 2D texture
  11117. if ( texture.__webglInit === undefined ) return;
  11118. _gl.deleteTexture( texture.__webglTexture );
  11119. delete texture.__webglTexture;
  11120. delete texture.__webglInit;
  11121. }
  11122. };
  11123. var deallocateRenderTarget = function ( renderTarget ) {
  11124. if ( ! renderTarget || renderTarget.__webglTexture === undefined ) return;
  11125. _gl.deleteTexture( renderTarget.__webglTexture );
  11126. delete renderTarget.__webglTexture;
  11127. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  11128. for ( var i = 0; i < 6; i ++ ) {
  11129. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer[ i ] );
  11130. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer[ i ] );
  11131. }
  11132. } else {
  11133. _gl.deleteFramebuffer( renderTarget.__webglFramebuffer );
  11134. _gl.deleteRenderbuffer( renderTarget.__webglRenderbuffer );
  11135. }
  11136. delete renderTarget.__webglFramebuffer;
  11137. delete renderTarget.__webglRenderbuffer;
  11138. };
  11139. var deallocateMaterial = function ( material ) {
  11140. var program = material.program.program;
  11141. if ( program === undefined ) return;
  11142. material.program = undefined;
  11143. // only deallocate GL program if this was the last use of shared program
  11144. // assumed there is only single copy of any program in the _programs list
  11145. // (that's how it's constructed)
  11146. var i, il, programInfo;
  11147. var deleteProgram = false;
  11148. for ( i = 0, il = _programs.length; i < il; i ++ ) {
  11149. programInfo = _programs[ i ];
  11150. if ( programInfo.program === program ) {
  11151. programInfo.usedTimes --;
  11152. if ( programInfo.usedTimes === 0 ) {
  11153. deleteProgram = true;
  11154. }
  11155. break;
  11156. }
  11157. }
  11158. if ( deleteProgram === true ) {
  11159. // avoid using array.splice, this is costlier than creating new array from scratch
  11160. var newPrograms = [];
  11161. for ( i = 0, il = _programs.length; i < il; i ++ ) {
  11162. programInfo = _programs[ i ];
  11163. if ( programInfo.program !== program ) {
  11164. newPrograms.push( programInfo );
  11165. }
  11166. }
  11167. _programs = newPrograms;
  11168. _gl.deleteProgram( program );
  11169. _this.info.memory.programs --;
  11170. }
  11171. };
  11172. // Buffer initialization
  11173. function initCustomAttributes ( object ) {
  11174. var geometry = object.geometry;
  11175. var material = object.material;
  11176. var nvertices = geometry.vertices.length;
  11177. if ( material.attributes ) {
  11178. if ( geometry.__webglCustomAttributesList === undefined ) {
  11179. geometry.__webglCustomAttributesList = [];
  11180. }
  11181. for ( var name in material.attributes ) {
  11182. var attribute = material.attributes[ name ];
  11183. if ( ! attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  11184. attribute.__webglInitialized = true;
  11185. var size = 1; // "f" and "i"
  11186. if ( attribute.type === 'v2' ) size = 2;
  11187. else if ( attribute.type === 'v3' ) size = 3;
  11188. else if ( attribute.type === 'v4' ) size = 4;
  11189. else if ( attribute.type === 'c' ) size = 3;
  11190. attribute.size = size;
  11191. attribute.array = new Float32Array( nvertices * size );
  11192. attribute.buffer = _gl.createBuffer();
  11193. attribute.buffer.belongsToAttribute = name;
  11194. attribute.needsUpdate = true;
  11195. }
  11196. geometry.__webglCustomAttributesList.push( attribute );
  11197. }
  11198. }
  11199. };
  11200. function initParticleBuffers ( geometry, object ) {
  11201. var nvertices = geometry.vertices.length;
  11202. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  11203. geometry.__colorArray = new Float32Array( nvertices * 3 );
  11204. geometry.__sortArray = [];
  11205. geometry.__webglParticleCount = nvertices;
  11206. initCustomAttributes( object );
  11207. };
  11208. function initLineBuffers ( geometry, object ) {
  11209. var nvertices = geometry.vertices.length;
  11210. geometry.__vertexArray = new Float32Array( nvertices * 3 );
  11211. geometry.__colorArray = new Float32Array( nvertices * 3 );
  11212. geometry.__lineDistanceArray = new Float32Array( nvertices * 1 );
  11213. geometry.__webglLineCount = nvertices;
  11214. initCustomAttributes( object );
  11215. };
  11216. function initMeshBuffers ( geometryGroup, object ) {
  11217. var geometry = object.geometry,
  11218. faces3 = geometryGroup.faces3,
  11219. nvertices = faces3.length * 3,
  11220. ntris = faces3.length * 1,
  11221. nlines = faces3.length * 3,
  11222. material = getBufferMaterial( object, geometryGroup );
  11223. geometryGroup.__vertexArray = new Float32Array( nvertices * 3 );
  11224. geometryGroup.__normalArray = new Float32Array( nvertices * 3 );
  11225. geometryGroup.__colorArray = new Float32Array( nvertices * 3 );
  11226. geometryGroup.__uvArray = new Float32Array( nvertices * 2 );
  11227. if ( geometry.faceVertexUvs.length > 1 ) {
  11228. geometryGroup.__uv2Array = new Float32Array( nvertices * 2 );
  11229. }
  11230. if ( geometry.hasTangents ) {
  11231. geometryGroup.__tangentArray = new Float32Array( nvertices * 4 );
  11232. }
  11233. if ( object.geometry.skinWeights.length && object.geometry.skinIndices.length ) {
  11234. geometryGroup.__skinIndexArray = new Float32Array( nvertices * 4 );
  11235. geometryGroup.__skinWeightArray = new Float32Array( nvertices * 4 );
  11236. }
  11237. var UintArray = extensions.get( 'OES_element_index_uint' ) !== null && ntris > 21845 ? Uint32Array : Uint16Array; // 65535 / 3
  11238. geometryGroup.__typeArray = UintArray;
  11239. geometryGroup.__faceArray = new UintArray( ntris * 3 );
  11240. geometryGroup.__lineArray = new UintArray( nlines * 2 );
  11241. var m, ml;
  11242. if ( geometryGroup.numMorphTargets ) {
  11243. geometryGroup.__morphTargetsArrays = [];
  11244. for ( m = 0, ml = geometryGroup.numMorphTargets; m < ml; m ++ ) {
  11245. geometryGroup.__morphTargetsArrays.push( new Float32Array( nvertices * 3 ) );
  11246. }
  11247. }
  11248. if ( geometryGroup.numMorphNormals ) {
  11249. geometryGroup.__morphNormalsArrays = [];
  11250. for ( m = 0, ml = geometryGroup.numMorphNormals; m < ml; m ++ ) {
  11251. geometryGroup.__morphNormalsArrays.push( new Float32Array( nvertices * 3 ) );
  11252. }
  11253. }
  11254. geometryGroup.__webglFaceCount = ntris * 3;
  11255. geometryGroup.__webglLineCount = nlines * 2;
  11256. // custom attributes
  11257. if ( material.attributes ) {
  11258. if ( geometryGroup.__webglCustomAttributesList === undefined ) {
  11259. geometryGroup.__webglCustomAttributesList = [];
  11260. }
  11261. for ( var name in material.attributes ) {
  11262. // Do a shallow copy of the attribute object so different geometryGroup chunks use different
  11263. // attribute buffers which are correctly indexed in the setMeshBuffers function
  11264. var originalAttribute = material.attributes[ name ];
  11265. var attribute = {};
  11266. for ( var property in originalAttribute ) {
  11267. attribute[ property ] = originalAttribute[ property ];
  11268. }
  11269. if ( ! attribute.__webglInitialized || attribute.createUniqueBuffers ) {
  11270. attribute.__webglInitialized = true;
  11271. var size = 1; // "f" and "i"
  11272. if ( attribute.type === 'v2' ) size = 2;
  11273. else if ( attribute.type === 'v3' ) size = 3;
  11274. else if ( attribute.type === 'v4' ) size = 4;
  11275. else if ( attribute.type === 'c' ) size = 3;
  11276. attribute.size = size;
  11277. attribute.array = new Float32Array( nvertices * size );
  11278. attribute.buffer = _gl.createBuffer();
  11279. attribute.buffer.belongsToAttribute = name;
  11280. originalAttribute.needsUpdate = true;
  11281. attribute.__original = originalAttribute;
  11282. }
  11283. geometryGroup.__webglCustomAttributesList.push( attribute );
  11284. }
  11285. }
  11286. geometryGroup.__inittedArrays = true;
  11287. };
  11288. function getBufferMaterial( object, geometryGroup ) {
  11289. return object.material instanceof THREE.MeshFaceMaterial
  11290. ? object.material.materials[ geometryGroup.materialIndex ]
  11291. : object.material;
  11292. };
  11293. function materialNeedsSmoothNormals ( material ) {
  11294. return material && material.shading !== undefined && material.shading === THREE.SmoothShading;
  11295. };
  11296. // Buffer setting
  11297. function setParticleBuffers ( geometry, hint, object ) {
  11298. var v, c, vertex, offset, index, color,
  11299. vertices = geometry.vertices,
  11300. vl = vertices.length,
  11301. colors = geometry.colors,
  11302. cl = colors.length,
  11303. vertexArray = geometry.__vertexArray,
  11304. colorArray = geometry.__colorArray,
  11305. sortArray = geometry.__sortArray,
  11306. dirtyVertices = geometry.verticesNeedUpdate,
  11307. dirtyElements = geometry.elementsNeedUpdate,
  11308. dirtyColors = geometry.colorsNeedUpdate,
  11309. customAttributes = geometry.__webglCustomAttributesList,
  11310. i, il,
  11311. a, ca, cal, value,
  11312. customAttribute;
  11313. if ( dirtyVertices ) {
  11314. for ( v = 0; v < vl; v ++ ) {
  11315. vertex = vertices[ v ];
  11316. offset = v * 3;
  11317. vertexArray[ offset ] = vertex.x;
  11318. vertexArray[ offset + 1 ] = vertex.y;
  11319. vertexArray[ offset + 2 ] = vertex.z;
  11320. }
  11321. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  11322. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  11323. }
  11324. if ( dirtyColors ) {
  11325. for ( c = 0; c < cl; c ++ ) {
  11326. color = colors[ c ];
  11327. offset = c * 3;
  11328. colorArray[ offset ] = color.r;
  11329. colorArray[ offset + 1 ] = color.g;
  11330. colorArray[ offset + 2 ] = color.b;
  11331. }
  11332. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  11333. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  11334. }
  11335. if ( customAttributes ) {
  11336. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  11337. customAttribute = customAttributes[ i ];
  11338. if ( customAttribute.needsUpdate && ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) ) {
  11339. cal = customAttribute.value.length;
  11340. offset = 0;
  11341. if ( customAttribute.size === 1 ) {
  11342. for ( ca = 0; ca < cal; ca ++ ) {
  11343. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  11344. }
  11345. } else if ( customAttribute.size === 2 ) {
  11346. for ( ca = 0; ca < cal; ca ++ ) {
  11347. value = customAttribute.value[ ca ];
  11348. customAttribute.array[ offset ] = value.x;
  11349. customAttribute.array[ offset + 1 ] = value.y;
  11350. offset += 2;
  11351. }
  11352. } else if ( customAttribute.size === 3 ) {
  11353. if ( customAttribute.type === 'c' ) {
  11354. for ( ca = 0; ca < cal; ca ++ ) {
  11355. value = customAttribute.value[ ca ];
  11356. customAttribute.array[ offset ] = value.r;
  11357. customAttribute.array[ offset + 1 ] = value.g;
  11358. customAttribute.array[ offset + 2 ] = value.b;
  11359. offset += 3;
  11360. }
  11361. } else {
  11362. for ( ca = 0; ca < cal; ca ++ ) {
  11363. value = customAttribute.value[ ca ];
  11364. customAttribute.array[ offset ] = value.x;
  11365. customAttribute.array[ offset + 1 ] = value.y;
  11366. customAttribute.array[ offset + 2 ] = value.z;
  11367. offset += 3;
  11368. }
  11369. }
  11370. } else if ( customAttribute.size === 4 ) {
  11371. for ( ca = 0; ca < cal; ca ++ ) {
  11372. value = customAttribute.value[ ca ];
  11373. customAttribute.array[ offset ] = value.x;
  11374. customAttribute.array[ offset + 1 ] = value.y;
  11375. customAttribute.array[ offset + 2 ] = value.z;
  11376. customAttribute.array[ offset + 3 ] = value.w;
  11377. offset += 4;
  11378. }
  11379. }
  11380. }
  11381. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  11382. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  11383. customAttribute.needsUpdate = false;
  11384. }
  11385. }
  11386. }
  11387. function setLineBuffers ( geometry, hint ) {
  11388. var v, c, d, vertex, offset, color,
  11389. vertices = geometry.vertices,
  11390. colors = geometry.colors,
  11391. lineDistances = geometry.lineDistances,
  11392. vl = vertices.length,
  11393. cl = colors.length,
  11394. dl = lineDistances.length,
  11395. vertexArray = geometry.__vertexArray,
  11396. colorArray = geometry.__colorArray,
  11397. lineDistanceArray = geometry.__lineDistanceArray,
  11398. dirtyVertices = geometry.verticesNeedUpdate,
  11399. dirtyColors = geometry.colorsNeedUpdate,
  11400. dirtyLineDistances = geometry.lineDistancesNeedUpdate,
  11401. customAttributes = geometry.__webglCustomAttributesList,
  11402. i, il,
  11403. a, ca, cal, value,
  11404. customAttribute;
  11405. if ( dirtyVertices ) {
  11406. for ( v = 0; v < vl; v ++ ) {
  11407. vertex = vertices[ v ];
  11408. offset = v * 3;
  11409. vertexArray[ offset ] = vertex.x;
  11410. vertexArray[ offset + 1 ] = vertex.y;
  11411. vertexArray[ offset + 2 ] = vertex.z;
  11412. }
  11413. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglVertexBuffer );
  11414. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  11415. }
  11416. if ( dirtyColors ) {
  11417. for ( c = 0; c < cl; c ++ ) {
  11418. color = colors[ c ];
  11419. offset = c * 3;
  11420. colorArray[ offset ] = color.r;
  11421. colorArray[ offset + 1 ] = color.g;
  11422. colorArray[ offset + 2 ] = color.b;
  11423. }
  11424. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglColorBuffer );
  11425. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  11426. }
  11427. if ( dirtyLineDistances ) {
  11428. for ( d = 0; d < dl; d ++ ) {
  11429. lineDistanceArray[ d ] = lineDistances[ d ];
  11430. }
  11431. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometry.__webglLineDistanceBuffer );
  11432. _gl.bufferData( _gl.ARRAY_BUFFER, lineDistanceArray, hint );
  11433. }
  11434. if ( customAttributes ) {
  11435. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  11436. customAttribute = customAttributes[ i ];
  11437. if ( customAttribute.needsUpdate && ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) ) {
  11438. offset = 0;
  11439. cal = customAttribute.value.length;
  11440. if ( customAttribute.size === 1 ) {
  11441. for ( ca = 0; ca < cal; ca ++ ) {
  11442. customAttribute.array[ ca ] = customAttribute.value[ ca ];
  11443. }
  11444. } else if ( customAttribute.size === 2 ) {
  11445. for ( ca = 0; ca < cal; ca ++ ) {
  11446. value = customAttribute.value[ ca ];
  11447. customAttribute.array[ offset ] = value.x;
  11448. customAttribute.array[ offset + 1 ] = value.y;
  11449. offset += 2;
  11450. }
  11451. } else if ( customAttribute.size === 3 ) {
  11452. if ( customAttribute.type === 'c' ) {
  11453. for ( ca = 0; ca < cal; ca ++ ) {
  11454. value = customAttribute.value[ ca ];
  11455. customAttribute.array[ offset ] = value.r;
  11456. customAttribute.array[ offset + 1 ] = value.g;
  11457. customAttribute.array[ offset + 2 ] = value.b;
  11458. offset += 3;
  11459. }
  11460. } else {
  11461. for ( ca = 0; ca < cal; ca ++ ) {
  11462. value = customAttribute.value[ ca ];
  11463. customAttribute.array[ offset ] = value.x;
  11464. customAttribute.array[ offset + 1 ] = value.y;
  11465. customAttribute.array[ offset + 2 ] = value.z;
  11466. offset += 3;
  11467. }
  11468. }
  11469. } else if ( customAttribute.size === 4 ) {
  11470. for ( ca = 0; ca < cal; ca ++ ) {
  11471. value = customAttribute.value[ ca ];
  11472. customAttribute.array[ offset ] = value.x;
  11473. customAttribute.array[ offset + 1 ] = value.y;
  11474. customAttribute.array[ offset + 2 ] = value.z;
  11475. customAttribute.array[ offset + 3 ] = value.w;
  11476. offset += 4;
  11477. }
  11478. }
  11479. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  11480. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  11481. customAttribute.needsUpdate = false;
  11482. }
  11483. }
  11484. }
  11485. }
  11486. function setMeshBuffers( geometryGroup, object, hint, dispose, material ) {
  11487. if ( ! geometryGroup.__inittedArrays ) {
  11488. return;
  11489. }
  11490. var needsSmoothNormals = materialNeedsSmoothNormals( material );
  11491. var f, fl, fi, face,
  11492. vertexNormals, faceNormal, normal,
  11493. vertexColors, faceColor,
  11494. vertexTangents,
  11495. uv, uv2, v1, v2, v3, v4, t1, t2, t3, t4, n1, n2, n3, n4,
  11496. c1, c2, c3,
  11497. sw1, sw2, sw3, sw4,
  11498. si1, si2, si3, si4,
  11499. sa1, sa2, sa3, sa4,
  11500. sb1, sb2, sb3, sb4,
  11501. m, ml, i, il,
  11502. vn, uvi, uv2i,
  11503. vk, vkl, vka,
  11504. nka, chf, faceVertexNormals,
  11505. a,
  11506. vertexIndex = 0,
  11507. offset = 0,
  11508. offset_uv = 0,
  11509. offset_uv2 = 0,
  11510. offset_face = 0,
  11511. offset_normal = 0,
  11512. offset_tangent = 0,
  11513. offset_line = 0,
  11514. offset_color = 0,
  11515. offset_skin = 0,
  11516. offset_morphTarget = 0,
  11517. offset_custom = 0,
  11518. offset_customSrc = 0,
  11519. value,
  11520. vertexArray = geometryGroup.__vertexArray,
  11521. uvArray = geometryGroup.__uvArray,
  11522. uv2Array = geometryGroup.__uv2Array,
  11523. normalArray = geometryGroup.__normalArray,
  11524. tangentArray = geometryGroup.__tangentArray,
  11525. colorArray = geometryGroup.__colorArray,
  11526. skinIndexArray = geometryGroup.__skinIndexArray,
  11527. skinWeightArray = geometryGroup.__skinWeightArray,
  11528. morphTargetsArrays = geometryGroup.__morphTargetsArrays,
  11529. morphNormalsArrays = geometryGroup.__morphNormalsArrays,
  11530. customAttributes = geometryGroup.__webglCustomAttributesList,
  11531. customAttribute,
  11532. faceArray = geometryGroup.__faceArray,
  11533. lineArray = geometryGroup.__lineArray,
  11534. geometry = object.geometry, // this is shared for all chunks
  11535. dirtyVertices = geometry.verticesNeedUpdate,
  11536. dirtyElements = geometry.elementsNeedUpdate,
  11537. dirtyUvs = geometry.uvsNeedUpdate,
  11538. dirtyNormals = geometry.normalsNeedUpdate,
  11539. dirtyTangents = geometry.tangentsNeedUpdate,
  11540. dirtyColors = geometry.colorsNeedUpdate,
  11541. dirtyMorphTargets = geometry.morphTargetsNeedUpdate,
  11542. vertices = geometry.vertices,
  11543. chunk_faces3 = geometryGroup.faces3,
  11544. obj_faces = geometry.faces,
  11545. obj_uvs = geometry.faceVertexUvs[ 0 ],
  11546. obj_uvs2 = geometry.faceVertexUvs[ 1 ],
  11547. obj_colors = geometry.colors,
  11548. obj_skinIndices = geometry.skinIndices,
  11549. obj_skinWeights = geometry.skinWeights,
  11550. morphTargets = geometry.morphTargets,
  11551. morphNormals = geometry.morphNormals;
  11552. if ( dirtyVertices ) {
  11553. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11554. face = obj_faces[ chunk_faces3[ f ] ];
  11555. v1 = vertices[ face.a ];
  11556. v2 = vertices[ face.b ];
  11557. v3 = vertices[ face.c ];
  11558. vertexArray[ offset ] = v1.x;
  11559. vertexArray[ offset + 1 ] = v1.y;
  11560. vertexArray[ offset + 2 ] = v1.z;
  11561. vertexArray[ offset + 3 ] = v2.x;
  11562. vertexArray[ offset + 4 ] = v2.y;
  11563. vertexArray[ offset + 5 ] = v2.z;
  11564. vertexArray[ offset + 6 ] = v3.x;
  11565. vertexArray[ offset + 7 ] = v3.y;
  11566. vertexArray[ offset + 8 ] = v3.z;
  11567. offset += 9;
  11568. }
  11569. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  11570. _gl.bufferData( _gl.ARRAY_BUFFER, vertexArray, hint );
  11571. }
  11572. if ( dirtyMorphTargets ) {
  11573. for ( vk = 0, vkl = morphTargets.length; vk < vkl; vk ++ ) {
  11574. offset_morphTarget = 0;
  11575. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11576. chf = chunk_faces3[ f ];
  11577. face = obj_faces[ chf ];
  11578. // morph positions
  11579. v1 = morphTargets[ vk ].vertices[ face.a ];
  11580. v2 = morphTargets[ vk ].vertices[ face.b ];
  11581. v3 = morphTargets[ vk ].vertices[ face.c ];
  11582. vka = morphTargetsArrays[ vk ];
  11583. vka[ offset_morphTarget ] = v1.x;
  11584. vka[ offset_morphTarget + 1 ] = v1.y;
  11585. vka[ offset_morphTarget + 2 ] = v1.z;
  11586. vka[ offset_morphTarget + 3 ] = v2.x;
  11587. vka[ offset_morphTarget + 4 ] = v2.y;
  11588. vka[ offset_morphTarget + 5 ] = v2.z;
  11589. vka[ offset_morphTarget + 6 ] = v3.x;
  11590. vka[ offset_morphTarget + 7 ] = v3.y;
  11591. vka[ offset_morphTarget + 8 ] = v3.z;
  11592. // morph normals
  11593. if ( material.morphNormals ) {
  11594. if ( needsSmoothNormals ) {
  11595. faceVertexNormals = morphNormals[ vk ].vertexNormals[ chf ];
  11596. n1 = faceVertexNormals.a;
  11597. n2 = faceVertexNormals.b;
  11598. n3 = faceVertexNormals.c;
  11599. } else {
  11600. n1 = morphNormals[ vk ].faceNormals[ chf ];
  11601. n2 = n1;
  11602. n3 = n1;
  11603. }
  11604. nka = morphNormalsArrays[ vk ];
  11605. nka[ offset_morphTarget ] = n1.x;
  11606. nka[ offset_morphTarget + 1 ] = n1.y;
  11607. nka[ offset_morphTarget + 2 ] = n1.z;
  11608. nka[ offset_morphTarget + 3 ] = n2.x;
  11609. nka[ offset_morphTarget + 4 ] = n2.y;
  11610. nka[ offset_morphTarget + 5 ] = n2.z;
  11611. nka[ offset_morphTarget + 6 ] = n3.x;
  11612. nka[ offset_morphTarget + 7 ] = n3.y;
  11613. nka[ offset_morphTarget + 8 ] = n3.z;
  11614. }
  11615. //
  11616. offset_morphTarget += 9;
  11617. }
  11618. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ vk ] );
  11619. _gl.bufferData( _gl.ARRAY_BUFFER, morphTargetsArrays[ vk ], hint );
  11620. if ( material.morphNormals ) {
  11621. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ vk ] );
  11622. _gl.bufferData( _gl.ARRAY_BUFFER, morphNormalsArrays[ vk ], hint );
  11623. }
  11624. }
  11625. }
  11626. if ( obj_skinWeights.length ) {
  11627. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11628. face = obj_faces[ chunk_faces3[ f ] ];
  11629. // weights
  11630. sw1 = obj_skinWeights[ face.a ];
  11631. sw2 = obj_skinWeights[ face.b ];
  11632. sw3 = obj_skinWeights[ face.c ];
  11633. skinWeightArray[ offset_skin ] = sw1.x;
  11634. skinWeightArray[ offset_skin + 1 ] = sw1.y;
  11635. skinWeightArray[ offset_skin + 2 ] = sw1.z;
  11636. skinWeightArray[ offset_skin + 3 ] = sw1.w;
  11637. skinWeightArray[ offset_skin + 4 ] = sw2.x;
  11638. skinWeightArray[ offset_skin + 5 ] = sw2.y;
  11639. skinWeightArray[ offset_skin + 6 ] = sw2.z;
  11640. skinWeightArray[ offset_skin + 7 ] = sw2.w;
  11641. skinWeightArray[ offset_skin + 8 ] = sw3.x;
  11642. skinWeightArray[ offset_skin + 9 ] = sw3.y;
  11643. skinWeightArray[ offset_skin + 10 ] = sw3.z;
  11644. skinWeightArray[ offset_skin + 11 ] = sw3.w;
  11645. // indices
  11646. si1 = obj_skinIndices[ face.a ];
  11647. si2 = obj_skinIndices[ face.b ];
  11648. si3 = obj_skinIndices[ face.c ];
  11649. skinIndexArray[ offset_skin ] = si1.x;
  11650. skinIndexArray[ offset_skin + 1 ] = si1.y;
  11651. skinIndexArray[ offset_skin + 2 ] = si1.z;
  11652. skinIndexArray[ offset_skin + 3 ] = si1.w;
  11653. skinIndexArray[ offset_skin + 4 ] = si2.x;
  11654. skinIndexArray[ offset_skin + 5 ] = si2.y;
  11655. skinIndexArray[ offset_skin + 6 ] = si2.z;
  11656. skinIndexArray[ offset_skin + 7 ] = si2.w;
  11657. skinIndexArray[ offset_skin + 8 ] = si3.x;
  11658. skinIndexArray[ offset_skin + 9 ] = si3.y;
  11659. skinIndexArray[ offset_skin + 10 ] = si3.z;
  11660. skinIndexArray[ offset_skin + 11 ] = si3.w;
  11661. offset_skin += 12;
  11662. }
  11663. if ( offset_skin > 0 ) {
  11664. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  11665. _gl.bufferData( _gl.ARRAY_BUFFER, skinIndexArray, hint );
  11666. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  11667. _gl.bufferData( _gl.ARRAY_BUFFER, skinWeightArray, hint );
  11668. }
  11669. }
  11670. if ( dirtyColors ) {
  11671. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11672. face = obj_faces[ chunk_faces3[ f ] ];
  11673. vertexColors = face.vertexColors;
  11674. faceColor = face.color;
  11675. if ( vertexColors.length === 3 && material.vertexColors === THREE.VertexColors ) {
  11676. c1 = vertexColors[ 0 ];
  11677. c2 = vertexColors[ 1 ];
  11678. c3 = vertexColors[ 2 ];
  11679. } else {
  11680. c1 = faceColor;
  11681. c2 = faceColor;
  11682. c3 = faceColor;
  11683. }
  11684. colorArray[ offset_color ] = c1.r;
  11685. colorArray[ offset_color + 1 ] = c1.g;
  11686. colorArray[ offset_color + 2 ] = c1.b;
  11687. colorArray[ offset_color + 3 ] = c2.r;
  11688. colorArray[ offset_color + 4 ] = c2.g;
  11689. colorArray[ offset_color + 5 ] = c2.b;
  11690. colorArray[ offset_color + 6 ] = c3.r;
  11691. colorArray[ offset_color + 7 ] = c3.g;
  11692. colorArray[ offset_color + 8 ] = c3.b;
  11693. offset_color += 9;
  11694. }
  11695. if ( offset_color > 0 ) {
  11696. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  11697. _gl.bufferData( _gl.ARRAY_BUFFER, colorArray, hint );
  11698. }
  11699. }
  11700. if ( dirtyTangents && geometry.hasTangents ) {
  11701. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11702. face = obj_faces[ chunk_faces3[ f ] ];
  11703. vertexTangents = face.vertexTangents;
  11704. t1 = vertexTangents[ 0 ];
  11705. t2 = vertexTangents[ 1 ];
  11706. t3 = vertexTangents[ 2 ];
  11707. tangentArray[ offset_tangent ] = t1.x;
  11708. tangentArray[ offset_tangent + 1 ] = t1.y;
  11709. tangentArray[ offset_tangent + 2 ] = t1.z;
  11710. tangentArray[ offset_tangent + 3 ] = t1.w;
  11711. tangentArray[ offset_tangent + 4 ] = t2.x;
  11712. tangentArray[ offset_tangent + 5 ] = t2.y;
  11713. tangentArray[ offset_tangent + 6 ] = t2.z;
  11714. tangentArray[ offset_tangent + 7 ] = t2.w;
  11715. tangentArray[ offset_tangent + 8 ] = t3.x;
  11716. tangentArray[ offset_tangent + 9 ] = t3.y;
  11717. tangentArray[ offset_tangent + 10 ] = t3.z;
  11718. tangentArray[ offset_tangent + 11 ] = t3.w;
  11719. offset_tangent += 12;
  11720. }
  11721. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  11722. _gl.bufferData( _gl.ARRAY_BUFFER, tangentArray, hint );
  11723. }
  11724. if ( dirtyNormals ) {
  11725. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11726. face = obj_faces[ chunk_faces3[ f ] ];
  11727. vertexNormals = face.vertexNormals;
  11728. faceNormal = face.normal;
  11729. if ( vertexNormals.length === 3 && needsSmoothNormals ) {
  11730. for ( i = 0; i < 3; i ++ ) {
  11731. vn = vertexNormals[ i ];
  11732. normalArray[ offset_normal ] = vn.x;
  11733. normalArray[ offset_normal + 1 ] = vn.y;
  11734. normalArray[ offset_normal + 2 ] = vn.z;
  11735. offset_normal += 3;
  11736. }
  11737. } else {
  11738. for ( i = 0; i < 3; i ++ ) {
  11739. normalArray[ offset_normal ] = faceNormal.x;
  11740. normalArray[ offset_normal + 1 ] = faceNormal.y;
  11741. normalArray[ offset_normal + 2 ] = faceNormal.z;
  11742. offset_normal += 3;
  11743. }
  11744. }
  11745. }
  11746. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  11747. _gl.bufferData( _gl.ARRAY_BUFFER, normalArray, hint );
  11748. }
  11749. if ( dirtyUvs && obj_uvs ) {
  11750. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11751. fi = chunk_faces3[ f ];
  11752. uv = obj_uvs[ fi ];
  11753. if ( uv === undefined ) continue;
  11754. for ( i = 0; i < 3; i ++ ) {
  11755. uvi = uv[ i ];
  11756. uvArray[ offset_uv ] = uvi.x;
  11757. uvArray[ offset_uv + 1 ] = uvi.y;
  11758. offset_uv += 2;
  11759. }
  11760. }
  11761. if ( offset_uv > 0 ) {
  11762. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  11763. _gl.bufferData( _gl.ARRAY_BUFFER, uvArray, hint );
  11764. }
  11765. }
  11766. if ( dirtyUvs && obj_uvs2 ) {
  11767. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11768. fi = chunk_faces3[ f ];
  11769. uv2 = obj_uvs2[ fi ];
  11770. if ( uv2 === undefined ) continue;
  11771. for ( i = 0; i < 3; i ++ ) {
  11772. uv2i = uv2[ i ];
  11773. uv2Array[ offset_uv2 ] = uv2i.x;
  11774. uv2Array[ offset_uv2 + 1 ] = uv2i.y;
  11775. offset_uv2 += 2;
  11776. }
  11777. }
  11778. if ( offset_uv2 > 0 ) {
  11779. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  11780. _gl.bufferData( _gl.ARRAY_BUFFER, uv2Array, hint );
  11781. }
  11782. }
  11783. if ( dirtyElements ) {
  11784. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11785. faceArray[ offset_face ] = vertexIndex;
  11786. faceArray[ offset_face + 1 ] = vertexIndex + 1;
  11787. faceArray[ offset_face + 2 ] = vertexIndex + 2;
  11788. offset_face += 3;
  11789. lineArray[ offset_line ] = vertexIndex;
  11790. lineArray[ offset_line + 1 ] = vertexIndex + 1;
  11791. lineArray[ offset_line + 2 ] = vertexIndex;
  11792. lineArray[ offset_line + 3 ] = vertexIndex + 2;
  11793. lineArray[ offset_line + 4 ] = vertexIndex + 1;
  11794. lineArray[ offset_line + 5 ] = vertexIndex + 2;
  11795. offset_line += 6;
  11796. vertexIndex += 3;
  11797. }
  11798. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  11799. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, faceArray, hint );
  11800. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  11801. _gl.bufferData( _gl.ELEMENT_ARRAY_BUFFER, lineArray, hint );
  11802. }
  11803. if ( customAttributes ) {
  11804. for ( i = 0, il = customAttributes.length; i < il; i ++ ) {
  11805. customAttribute = customAttributes[ i ];
  11806. if ( ! customAttribute.__original.needsUpdate ) continue;
  11807. offset_custom = 0;
  11808. offset_customSrc = 0;
  11809. if ( customAttribute.size === 1 ) {
  11810. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  11811. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11812. face = obj_faces[ chunk_faces3[ f ] ];
  11813. customAttribute.array[ offset_custom ] = customAttribute.value[ face.a ];
  11814. customAttribute.array[ offset_custom + 1 ] = customAttribute.value[ face.b ];
  11815. customAttribute.array[ offset_custom + 2 ] = customAttribute.value[ face.c ];
  11816. offset_custom += 3;
  11817. }
  11818. } else if ( customAttribute.boundTo === 'faces' ) {
  11819. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11820. value = customAttribute.value[ chunk_faces3[ f ] ];
  11821. customAttribute.array[ offset_custom ] = value;
  11822. customAttribute.array[ offset_custom + 1 ] = value;
  11823. customAttribute.array[ offset_custom + 2 ] = value;
  11824. offset_custom += 3;
  11825. }
  11826. }
  11827. } else if ( customAttribute.size === 2 ) {
  11828. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  11829. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11830. face = obj_faces[ chunk_faces3[ f ] ];
  11831. v1 = customAttribute.value[ face.a ];
  11832. v2 = customAttribute.value[ face.b ];
  11833. v3 = customAttribute.value[ face.c ];
  11834. customAttribute.array[ offset_custom ] = v1.x;
  11835. customAttribute.array[ offset_custom + 1 ] = v1.y;
  11836. customAttribute.array[ offset_custom + 2 ] = v2.x;
  11837. customAttribute.array[ offset_custom + 3 ] = v2.y;
  11838. customAttribute.array[ offset_custom + 4 ] = v3.x;
  11839. customAttribute.array[ offset_custom + 5 ] = v3.y;
  11840. offset_custom += 6;
  11841. }
  11842. } else if ( customAttribute.boundTo === 'faces' ) {
  11843. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11844. value = customAttribute.value[ chunk_faces3[ f ] ];
  11845. v1 = value;
  11846. v2 = value;
  11847. v3 = value;
  11848. customAttribute.array[ offset_custom ] = v1.x;
  11849. customAttribute.array[ offset_custom + 1 ] = v1.y;
  11850. customAttribute.array[ offset_custom + 2 ] = v2.x;
  11851. customAttribute.array[ offset_custom + 3 ] = v2.y;
  11852. customAttribute.array[ offset_custom + 4 ] = v3.x;
  11853. customAttribute.array[ offset_custom + 5 ] = v3.y;
  11854. offset_custom += 6;
  11855. }
  11856. }
  11857. } else if ( customAttribute.size === 3 ) {
  11858. var pp;
  11859. if ( customAttribute.type === 'c' ) {
  11860. pp = [ 'r', 'g', 'b' ];
  11861. } else {
  11862. pp = [ 'x', 'y', 'z' ];
  11863. }
  11864. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  11865. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11866. face = obj_faces[ chunk_faces3[ f ] ];
  11867. v1 = customAttribute.value[ face.a ];
  11868. v2 = customAttribute.value[ face.b ];
  11869. v3 = customAttribute.value[ face.c ];
  11870. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  11871. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  11872. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  11873. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  11874. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  11875. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  11876. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  11877. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  11878. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  11879. offset_custom += 9;
  11880. }
  11881. } else if ( customAttribute.boundTo === 'faces' ) {
  11882. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11883. value = customAttribute.value[ chunk_faces3[ f ] ];
  11884. v1 = value;
  11885. v2 = value;
  11886. v3 = value;
  11887. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  11888. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  11889. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  11890. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  11891. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  11892. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  11893. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  11894. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  11895. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  11896. offset_custom += 9;
  11897. }
  11898. } else if ( customAttribute.boundTo === 'faceVertices' ) {
  11899. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11900. value = customAttribute.value[ chunk_faces3[ f ] ];
  11901. v1 = value[ 0 ];
  11902. v2 = value[ 1 ];
  11903. v3 = value[ 2 ];
  11904. customAttribute.array[ offset_custom ] = v1[ pp[ 0 ] ];
  11905. customAttribute.array[ offset_custom + 1 ] = v1[ pp[ 1 ] ];
  11906. customAttribute.array[ offset_custom + 2 ] = v1[ pp[ 2 ] ];
  11907. customAttribute.array[ offset_custom + 3 ] = v2[ pp[ 0 ] ];
  11908. customAttribute.array[ offset_custom + 4 ] = v2[ pp[ 1 ] ];
  11909. customAttribute.array[ offset_custom + 5 ] = v2[ pp[ 2 ] ];
  11910. customAttribute.array[ offset_custom + 6 ] = v3[ pp[ 0 ] ];
  11911. customAttribute.array[ offset_custom + 7 ] = v3[ pp[ 1 ] ];
  11912. customAttribute.array[ offset_custom + 8 ] = v3[ pp[ 2 ] ];
  11913. offset_custom += 9;
  11914. }
  11915. }
  11916. } else if ( customAttribute.size === 4 ) {
  11917. if ( customAttribute.boundTo === undefined || customAttribute.boundTo === 'vertices' ) {
  11918. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11919. face = obj_faces[ chunk_faces3[ f ] ];
  11920. v1 = customAttribute.value[ face.a ];
  11921. v2 = customAttribute.value[ face.b ];
  11922. v3 = customAttribute.value[ face.c ];
  11923. customAttribute.array[ offset_custom ] = v1.x;
  11924. customAttribute.array[ offset_custom + 1 ] = v1.y;
  11925. customAttribute.array[ offset_custom + 2 ] = v1.z;
  11926. customAttribute.array[ offset_custom + 3 ] = v1.w;
  11927. customAttribute.array[ offset_custom + 4 ] = v2.x;
  11928. customAttribute.array[ offset_custom + 5 ] = v2.y;
  11929. customAttribute.array[ offset_custom + 6 ] = v2.z;
  11930. customAttribute.array[ offset_custom + 7 ] = v2.w;
  11931. customAttribute.array[ offset_custom + 8 ] = v3.x;
  11932. customAttribute.array[ offset_custom + 9 ] = v3.y;
  11933. customAttribute.array[ offset_custom + 10 ] = v3.z;
  11934. customAttribute.array[ offset_custom + 11 ] = v3.w;
  11935. offset_custom += 12;
  11936. }
  11937. } else if ( customAttribute.boundTo === 'faces' ) {
  11938. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11939. value = customAttribute.value[ chunk_faces3[ f ] ];
  11940. v1 = value;
  11941. v2 = value;
  11942. v3 = value;
  11943. customAttribute.array[ offset_custom ] = v1.x;
  11944. customAttribute.array[ offset_custom + 1 ] = v1.y;
  11945. customAttribute.array[ offset_custom + 2 ] = v1.z;
  11946. customAttribute.array[ offset_custom + 3 ] = v1.w;
  11947. customAttribute.array[ offset_custom + 4 ] = v2.x;
  11948. customAttribute.array[ offset_custom + 5 ] = v2.y;
  11949. customAttribute.array[ offset_custom + 6 ] = v2.z;
  11950. customAttribute.array[ offset_custom + 7 ] = v2.w;
  11951. customAttribute.array[ offset_custom + 8 ] = v3.x;
  11952. customAttribute.array[ offset_custom + 9 ] = v3.y;
  11953. customAttribute.array[ offset_custom + 10 ] = v3.z;
  11954. customAttribute.array[ offset_custom + 11 ] = v3.w;
  11955. offset_custom += 12;
  11956. }
  11957. } else if ( customAttribute.boundTo === 'faceVertices' ) {
  11958. for ( f = 0, fl = chunk_faces3.length; f < fl; f ++ ) {
  11959. value = customAttribute.value[ chunk_faces3[ f ] ];
  11960. v1 = value[ 0 ];
  11961. v2 = value[ 1 ];
  11962. v3 = value[ 2 ];
  11963. customAttribute.array[ offset_custom ] = v1.x;
  11964. customAttribute.array[ offset_custom + 1 ] = v1.y;
  11965. customAttribute.array[ offset_custom + 2 ] = v1.z;
  11966. customAttribute.array[ offset_custom + 3 ] = v1.w;
  11967. customAttribute.array[ offset_custom + 4 ] = v2.x;
  11968. customAttribute.array[ offset_custom + 5 ] = v2.y;
  11969. customAttribute.array[ offset_custom + 6 ] = v2.z;
  11970. customAttribute.array[ offset_custom + 7 ] = v2.w;
  11971. customAttribute.array[ offset_custom + 8 ] = v3.x;
  11972. customAttribute.array[ offset_custom + 9 ] = v3.y;
  11973. customAttribute.array[ offset_custom + 10 ] = v3.z;
  11974. customAttribute.array[ offset_custom + 11 ] = v3.w;
  11975. offset_custom += 12;
  11976. }
  11977. }
  11978. }
  11979. _gl.bindBuffer( _gl.ARRAY_BUFFER, customAttribute.buffer );
  11980. _gl.bufferData( _gl.ARRAY_BUFFER, customAttribute.array, hint );
  11981. }
  11982. }
  11983. if ( dispose ) {
  11984. delete geometryGroup.__inittedArrays;
  11985. delete geometryGroup.__colorArray;
  11986. delete geometryGroup.__normalArray;
  11987. delete geometryGroup.__tangentArray;
  11988. delete geometryGroup.__uvArray;
  11989. delete geometryGroup.__uv2Array;
  11990. delete geometryGroup.__faceArray;
  11991. delete geometryGroup.__vertexArray;
  11992. delete geometryGroup.__lineArray;
  11993. delete geometryGroup.__skinIndexArray;
  11994. delete geometryGroup.__skinWeightArray;
  11995. }
  11996. };
  11997. function setDirectBuffers( geometry ) {
  11998. var attributes = geometry.attributes;
  11999. var attributesKeys = geometry.attributesKeys;
  12000. for ( var i = 0, l = attributesKeys.length; i < l; i ++ ) {
  12001. var key = attributesKeys[ i ];
  12002. var attribute = attributes[ key ];
  12003. if ( attribute.buffer === undefined ) {
  12004. attribute.buffer = _gl.createBuffer();
  12005. attribute.needsUpdate = true;
  12006. }
  12007. if ( attribute.needsUpdate === true ) {
  12008. var bufferType = ( key === 'index' ) ? _gl.ELEMENT_ARRAY_BUFFER : _gl.ARRAY_BUFFER;
  12009. _gl.bindBuffer( bufferType, attribute.buffer );
  12010. _gl.bufferData( bufferType, attribute.array, _gl.STATIC_DRAW );
  12011. attribute.needsUpdate = false;
  12012. }
  12013. }
  12014. }
  12015. // Buffer rendering
  12016. this.renderBufferImmediate = function ( object, program, material ) {
  12017. initAttributes();
  12018. if ( object.hasPositions && ! object.__webglVertexBuffer ) object.__webglVertexBuffer = _gl.createBuffer();
  12019. if ( object.hasNormals && ! object.__webglNormalBuffer ) object.__webglNormalBuffer = _gl.createBuffer();
  12020. if ( object.hasUvs && ! object.__webglUvBuffer ) object.__webglUvBuffer = _gl.createBuffer();
  12021. if ( object.hasColors && ! object.__webglColorBuffer ) object.__webglColorBuffer = _gl.createBuffer();
  12022. if ( object.hasPositions ) {
  12023. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglVertexBuffer );
  12024. _gl.bufferData( _gl.ARRAY_BUFFER, object.positionArray, _gl.DYNAMIC_DRAW );
  12025. enableAttribute( program.attributes.position );
  12026. _gl.vertexAttribPointer( program.attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  12027. }
  12028. if ( object.hasNormals ) {
  12029. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglNormalBuffer );
  12030. if ( material.shading === THREE.FlatShading ) {
  12031. var nx, ny, nz,
  12032. nax, nbx, ncx, nay, nby, ncy, naz, nbz, ncz,
  12033. normalArray,
  12034. i, il = object.count * 3;
  12035. for ( i = 0; i < il; i += 9 ) {
  12036. normalArray = object.normalArray;
  12037. nax = normalArray[ i ];
  12038. nay = normalArray[ i + 1 ];
  12039. naz = normalArray[ i + 2 ];
  12040. nbx = normalArray[ i + 3 ];
  12041. nby = normalArray[ i + 4 ];
  12042. nbz = normalArray[ i + 5 ];
  12043. ncx = normalArray[ i + 6 ];
  12044. ncy = normalArray[ i + 7 ];
  12045. ncz = normalArray[ i + 8 ];
  12046. nx = ( nax + nbx + ncx ) / 3;
  12047. ny = ( nay + nby + ncy ) / 3;
  12048. nz = ( naz + nbz + ncz ) / 3;
  12049. normalArray[ i ] = nx;
  12050. normalArray[ i + 1 ] = ny;
  12051. normalArray[ i + 2 ] = nz;
  12052. normalArray[ i + 3 ] = nx;
  12053. normalArray[ i + 4 ] = ny;
  12054. normalArray[ i + 5 ] = nz;
  12055. normalArray[ i + 6 ] = nx;
  12056. normalArray[ i + 7 ] = ny;
  12057. normalArray[ i + 8 ] = nz;
  12058. }
  12059. }
  12060. _gl.bufferData( _gl.ARRAY_BUFFER, object.normalArray, _gl.DYNAMIC_DRAW );
  12061. enableAttribute( program.attributes.normal );
  12062. _gl.vertexAttribPointer( program.attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  12063. }
  12064. if ( object.hasUvs && material.map ) {
  12065. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglUvBuffer );
  12066. _gl.bufferData( _gl.ARRAY_BUFFER, object.uvArray, _gl.DYNAMIC_DRAW );
  12067. enableAttribute( program.attributes.uv );
  12068. _gl.vertexAttribPointer( program.attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  12069. }
  12070. if ( object.hasColors && material.vertexColors !== THREE.NoColors ) {
  12071. _gl.bindBuffer( _gl.ARRAY_BUFFER, object.__webglColorBuffer );
  12072. _gl.bufferData( _gl.ARRAY_BUFFER, object.colorArray, _gl.DYNAMIC_DRAW );
  12073. enableAttribute( program.attributes.color );
  12074. _gl.vertexAttribPointer( program.attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  12075. }
  12076. disableUnusedAttributes();
  12077. _gl.drawArrays( _gl.TRIANGLES, 0, object.count );
  12078. object.count = 0;
  12079. };
  12080. function setupVertexAttributes( material, program, geometry, startIndex ) {
  12081. var geometryAttributes = geometry.attributes;
  12082. var programAttributes = program.attributes;
  12083. var programAttributesKeys = program.attributesKeys;
  12084. for ( var i = 0, l = programAttributesKeys.length; i < l; i ++ ) {
  12085. var key = programAttributesKeys[ i ];
  12086. var programAttribute = programAttributes[ key ];
  12087. if ( programAttribute >= 0 ) {
  12088. var geometryAttribute = geometryAttributes[ key ];
  12089. if ( geometryAttribute !== undefined ) {
  12090. var size = geometryAttribute.itemSize;
  12091. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryAttribute.buffer );
  12092. enableAttribute( programAttribute );
  12093. _gl.vertexAttribPointer( programAttribute, size, _gl.FLOAT, false, 0, startIndex * size * 4 ); // 4 bytes per Float32
  12094. } else if ( material.defaultAttributeValues !== undefined ) {
  12095. if ( material.defaultAttributeValues[ key ].length === 2 ) {
  12096. _gl.vertexAttrib2fv( programAttribute, material.defaultAttributeValues[ key ] );
  12097. } else if ( material.defaultAttributeValues[ key ].length === 3 ) {
  12098. _gl.vertexAttrib3fv( programAttribute, material.defaultAttributeValues[ key ] );
  12099. }
  12100. }
  12101. }
  12102. }
  12103. disableUnusedAttributes();
  12104. }
  12105. this.renderBufferDirect = function ( camera, lights, fog, material, geometry, object ) {
  12106. if ( material.visible === false ) return;
  12107. var program = setProgram( camera, lights, fog, material, object );
  12108. var updateBuffers = false,
  12109. wireframeBit = material.wireframe ? 1 : 0,
  12110. geometryHash = ( geometry.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  12111. if ( geometryHash !== _currentGeometryGroupHash ) {
  12112. _currentGeometryGroupHash = geometryHash;
  12113. updateBuffers = true;
  12114. }
  12115. if ( updateBuffers ) {
  12116. initAttributes();
  12117. }
  12118. // render mesh
  12119. if ( object instanceof THREE.Mesh ) {
  12120. var mode = material.wireframe === true ? _gl.LINES : _gl.TRIANGLES;
  12121. var index = geometry.attributes.index;
  12122. if ( index ) {
  12123. // indexed triangles
  12124. var type, size;
  12125. if ( index.array instanceof Uint32Array && extensions.get( 'OES_element_index_uint' ) ) {
  12126. type = _gl.UNSIGNED_INT;
  12127. size = 4;
  12128. } else {
  12129. type = _gl.UNSIGNED_SHORT;
  12130. size = 2;
  12131. }
  12132. var offsets = geometry.offsets;
  12133. if ( offsets.length === 0 ) {
  12134. if ( updateBuffers ) {
  12135. setupVertexAttributes( material, program, geometry, 0 );
  12136. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  12137. }
  12138. _gl.drawElements( mode, index.array.length, type, 0 );
  12139. _this.info.render.calls ++;
  12140. _this.info.render.vertices += index.array.length; // not really true, here vertices can be shared
  12141. _this.info.render.faces += index.array.length / 3;
  12142. } else {
  12143. // if there is more than 1 chunk
  12144. // must set attribute pointers to use new offsets for each chunk
  12145. // even if geometry and materials didn't change
  12146. updateBuffers = true;
  12147. for ( var i = 0, il = offsets.length; i < il; i ++ ) {
  12148. var startIndex = offsets[ i ].index;
  12149. if ( updateBuffers ) {
  12150. setupVertexAttributes( material, program, geometry, startIndex );
  12151. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  12152. }
  12153. // render indexed triangles
  12154. _gl.drawElements( mode, offsets[ i ].count, type, offsets[ i ].start * size );
  12155. _this.info.render.calls ++;
  12156. _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
  12157. _this.info.render.faces += offsets[ i ].count / 3;
  12158. }
  12159. }
  12160. } else {
  12161. // non-indexed triangles
  12162. if ( updateBuffers ) {
  12163. setupVertexAttributes( material, program, geometry, 0 );
  12164. }
  12165. var position = geometry.attributes[ 'position' ];
  12166. // render non-indexed triangles
  12167. _gl.drawArrays( mode, 0, position.array.length / 3 );
  12168. _this.info.render.calls ++;
  12169. _this.info.render.vertices += position.array.length / 3;
  12170. _this.info.render.faces += position.array.length / 9;
  12171. }
  12172. } else if ( object instanceof THREE.PointCloud ) {
  12173. // render particles
  12174. var mode = _gl.POINTS;
  12175. var index = geometry.attributes.index;
  12176. if ( index ) {
  12177. // indexed points
  12178. var type, size;
  12179. if ( index.array instanceof Uint32Array && extensions.get( 'OES_element_index_uint' ) ) {
  12180. type = _gl.UNSIGNED_INT;
  12181. size = 4;
  12182. } else {
  12183. type = _gl.UNSIGNED_SHORT;
  12184. size = 2;
  12185. }
  12186. var offsets = geometry.offsets;
  12187. if ( offsets.length === 0 ) {
  12188. if ( updateBuffers ) {
  12189. setupVertexAttributes( material, program, geometry, 0 );
  12190. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  12191. }
  12192. _gl.drawElements( mode, index.array.length, type, 0);
  12193. _this.info.render.calls ++;
  12194. _this.info.render.points += index.array.length;
  12195. } else {
  12196. // if there is more than 1 chunk
  12197. // must set attribute pointers to use new offsets for each chunk
  12198. // even if geometry and materials didn't change
  12199. if ( offsets.length > 1 ) updateBuffers = true;
  12200. for ( var i = 0, il = offsets.length; i < il; i ++ ) {
  12201. var startIndex = offsets[ i ].index;
  12202. if ( updateBuffers ) {
  12203. setupVertexAttributes( material, program, geometry, startIndex );
  12204. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  12205. }
  12206. // render indexed points
  12207. _gl.drawElements( mode, offsets[ i ].count, type, offsets[ i ].start * size );
  12208. _this.info.render.calls ++;
  12209. _this.info.render.points += offsets[ i ].count;
  12210. }
  12211. }
  12212. } else {
  12213. // non-indexed points
  12214. if ( updateBuffers ) {
  12215. setupVertexAttributes( material, program, geometry, 0 );
  12216. }
  12217. var position = geometry.attributes.position;
  12218. _gl.drawArrays( mode, 0, position.array.length / 3 );
  12219. _this.info.render.calls ++;
  12220. _this.info.render.points += position.array.length / 3;
  12221. }
  12222. } else if ( object instanceof THREE.Line ) {
  12223. var mode = ( object.mode === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  12224. setLineWidth( material.linewidth );
  12225. var index = geometry.attributes.index;
  12226. if ( index ) {
  12227. // indexed lines
  12228. var type, size;
  12229. if ( index.array instanceof Uint32Array ) {
  12230. type = _gl.UNSIGNED_INT;
  12231. size = 4;
  12232. } else {
  12233. type = _gl.UNSIGNED_SHORT;
  12234. size = 2;
  12235. }
  12236. var offsets = geometry.offsets;
  12237. if ( offsets.length === 0 ) {
  12238. if ( updateBuffers ) {
  12239. setupVertexAttributes( material, program, geometry, 0 );
  12240. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  12241. }
  12242. _gl.drawElements( mode, index.array.length, type, 0 ); // 2 bytes per Uint16Array
  12243. _this.info.render.calls ++;
  12244. _this.info.render.vertices += index.array.length; // not really true, here vertices can be shared
  12245. } else {
  12246. // if there is more than 1 chunk
  12247. // must set attribute pointers to use new offsets for each chunk
  12248. // even if geometry and materials didn't change
  12249. if ( offsets.length > 1 ) updateBuffers = true;
  12250. for ( var i = 0, il = offsets.length; i < il; i ++ ) {
  12251. var startIndex = offsets[ i ].index;
  12252. if ( updateBuffers ) {
  12253. setupVertexAttributes( material, program, geometry, startIndex );
  12254. _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, index.buffer );
  12255. }
  12256. // render indexed lines
  12257. _gl.drawElements( mode, offsets[ i ].count, type, offsets[ i ].start * size ); // 2 bytes per Uint16Array
  12258. _this.info.render.calls ++;
  12259. _this.info.render.vertices += offsets[ i ].count; // not really true, here vertices can be shared
  12260. }
  12261. }
  12262. } else {
  12263. // non-indexed lines
  12264. if ( updateBuffers ) {
  12265. setupVertexAttributes( material, program, geometry, 0 );
  12266. }
  12267. var position = geometry.attributes.position;
  12268. _gl.drawArrays( mode, 0, position.array.length / 3 );
  12269. _this.info.render.calls ++;
  12270. _this.info.render.points += position.array.length / 3;
  12271. }
  12272. }
  12273. };
  12274. this.renderBuffer = function ( camera, lights, fog, material, geometryGroup, object ) {
  12275. if ( material.visible === false ) return;
  12276. var program = setProgram( camera, lights, fog, material, object );
  12277. var attributes = program.attributes;
  12278. var updateBuffers = false,
  12279. wireframeBit = material.wireframe ? 1 : 0,
  12280. geometryGroupHash = ( geometryGroup.id * 0xffffff ) + ( program.id * 2 ) + wireframeBit;
  12281. if ( geometryGroupHash !== _currentGeometryGroupHash ) {
  12282. _currentGeometryGroupHash = geometryGroupHash;
  12283. updateBuffers = true;
  12284. }
  12285. if ( updateBuffers ) {
  12286. initAttributes();
  12287. }
  12288. // vertices
  12289. if ( ! material.morphTargets && attributes.position >= 0 ) {
  12290. if ( updateBuffers ) {
  12291. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  12292. enableAttribute( attributes.position );
  12293. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  12294. }
  12295. } else {
  12296. if ( object.morphTargetBase ) {
  12297. setupMorphTargets( material, geometryGroup, object );
  12298. }
  12299. }
  12300. if ( updateBuffers ) {
  12301. // custom attributes
  12302. // Use the per-geometryGroup custom attribute arrays which are setup in initMeshBuffers
  12303. if ( geometryGroup.__webglCustomAttributesList ) {
  12304. for ( var i = 0, il = geometryGroup.__webglCustomAttributesList.length; i < il; i ++ ) {
  12305. var attribute = geometryGroup.__webglCustomAttributesList[ i ];
  12306. if ( attributes[ attribute.buffer.belongsToAttribute ] >= 0 ) {
  12307. _gl.bindBuffer( _gl.ARRAY_BUFFER, attribute.buffer );
  12308. enableAttribute( attributes[ attribute.buffer.belongsToAttribute ] );
  12309. _gl.vertexAttribPointer( attributes[ attribute.buffer.belongsToAttribute ], attribute.size, _gl.FLOAT, false, 0, 0 );
  12310. }
  12311. }
  12312. }
  12313. // colors
  12314. if ( attributes.color >= 0 ) {
  12315. if ( object.geometry.colors.length > 0 || object.geometry.faces.length > 0 ) {
  12316. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglColorBuffer );
  12317. enableAttribute( attributes.color );
  12318. _gl.vertexAttribPointer( attributes.color, 3, _gl.FLOAT, false, 0, 0 );
  12319. } else if ( material.defaultAttributeValues !== undefined ) {
  12320. _gl.vertexAttrib3fv( attributes.color, material.defaultAttributeValues.color );
  12321. }
  12322. }
  12323. // normals
  12324. if ( attributes.normal >= 0 ) {
  12325. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglNormalBuffer );
  12326. enableAttribute( attributes.normal );
  12327. _gl.vertexAttribPointer( attributes.normal, 3, _gl.FLOAT, false, 0, 0 );
  12328. }
  12329. // tangents
  12330. if ( attributes.tangent >= 0 ) {
  12331. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglTangentBuffer );
  12332. enableAttribute( attributes.tangent );
  12333. _gl.vertexAttribPointer( attributes.tangent, 4, _gl.FLOAT, false, 0, 0 );
  12334. }
  12335. // uvs
  12336. if ( attributes.uv >= 0 ) {
  12337. if ( object.geometry.faceVertexUvs[ 0 ] ) {
  12338. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUVBuffer );
  12339. enableAttribute( attributes.uv );
  12340. _gl.vertexAttribPointer( attributes.uv, 2, _gl.FLOAT, false, 0, 0 );
  12341. } else if ( material.defaultAttributeValues !== undefined ) {
  12342. _gl.vertexAttrib2fv( attributes.uv, material.defaultAttributeValues.uv );
  12343. }
  12344. }
  12345. if ( attributes.uv2 >= 0 ) {
  12346. if ( object.geometry.faceVertexUvs[ 1 ] ) {
  12347. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglUV2Buffer );
  12348. enableAttribute( attributes.uv2 );
  12349. _gl.vertexAttribPointer( attributes.uv2, 2, _gl.FLOAT, false, 0, 0 );
  12350. } else if ( material.defaultAttributeValues !== undefined ) {
  12351. _gl.vertexAttrib2fv( attributes.uv2, material.defaultAttributeValues.uv2 );
  12352. }
  12353. }
  12354. if ( material.skinning &&
  12355. attributes.skinIndex >= 0 && attributes.skinWeight >= 0 ) {
  12356. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinIndicesBuffer );
  12357. enableAttribute( attributes.skinIndex );
  12358. _gl.vertexAttribPointer( attributes.skinIndex, 4, _gl.FLOAT, false, 0, 0 );
  12359. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglSkinWeightsBuffer );
  12360. enableAttribute( attributes.skinWeight );
  12361. _gl.vertexAttribPointer( attributes.skinWeight, 4, _gl.FLOAT, false, 0, 0 );
  12362. }
  12363. // line distances
  12364. if ( attributes.lineDistance >= 0 ) {
  12365. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglLineDistanceBuffer );
  12366. enableAttribute( attributes.lineDistance );
  12367. _gl.vertexAttribPointer( attributes.lineDistance, 1, _gl.FLOAT, false, 0, 0 );
  12368. }
  12369. }
  12370. disableUnusedAttributes();
  12371. // render mesh
  12372. if ( object instanceof THREE.Mesh ) {
  12373. var type = geometryGroup.__typeArray === Uint32Array ? _gl.UNSIGNED_INT : _gl.UNSIGNED_SHORT;
  12374. // wireframe
  12375. if ( material.wireframe ) {
  12376. setLineWidth( material.wireframeLinewidth );
  12377. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglLineBuffer );
  12378. _gl.drawElements( _gl.LINES, geometryGroup.__webglLineCount, type, 0 );
  12379. // triangles
  12380. } else {
  12381. if ( updateBuffers ) _gl.bindBuffer( _gl.ELEMENT_ARRAY_BUFFER, geometryGroup.__webglFaceBuffer );
  12382. _gl.drawElements( _gl.TRIANGLES, geometryGroup.__webglFaceCount, type, 0 );
  12383. }
  12384. _this.info.render.calls ++;
  12385. _this.info.render.vertices += geometryGroup.__webglFaceCount;
  12386. _this.info.render.faces += geometryGroup.__webglFaceCount / 3;
  12387. // render lines
  12388. } else if ( object instanceof THREE.Line ) {
  12389. var mode = ( object.mode === THREE.LineStrip ) ? _gl.LINE_STRIP : _gl.LINES;
  12390. setLineWidth( material.linewidth );
  12391. _gl.drawArrays( mode, 0, geometryGroup.__webglLineCount );
  12392. _this.info.render.calls ++;
  12393. // render particles
  12394. } else if ( object instanceof THREE.PointCloud ) {
  12395. _gl.drawArrays( _gl.POINTS, 0, geometryGroup.__webglParticleCount );
  12396. _this.info.render.calls ++;
  12397. _this.info.render.points += geometryGroup.__webglParticleCount;
  12398. }
  12399. };
  12400. function initAttributes() {
  12401. for ( var i = 0, l = _newAttributes.length; i < l; i ++ ) {
  12402. _newAttributes[ i ] = 0;
  12403. }
  12404. }
  12405. function enableAttribute( attribute ) {
  12406. _newAttributes[ attribute ] = 1;
  12407. if ( _enabledAttributes[ attribute ] === 0 ) {
  12408. _gl.enableVertexAttribArray( attribute );
  12409. _enabledAttributes[ attribute ] = 1;
  12410. }
  12411. }
  12412. function disableUnusedAttributes() {
  12413. for ( var i = 0, l = _enabledAttributes.length; i < l; i ++ ) {
  12414. if ( _enabledAttributes[ i ] !== _newAttributes[ i ] ) {
  12415. _gl.disableVertexAttribArray( i );
  12416. _enabledAttributes[ i ] = 0;
  12417. }
  12418. }
  12419. }
  12420. function setupMorphTargets ( material, geometryGroup, object ) {
  12421. // set base
  12422. var attributes = material.program.attributes;
  12423. if ( object.morphTargetBase !== - 1 && attributes.position >= 0 ) {
  12424. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ object.morphTargetBase ] );
  12425. enableAttribute( attributes.position );
  12426. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  12427. } else if ( attributes.position >= 0 ) {
  12428. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglVertexBuffer );
  12429. enableAttribute( attributes.position );
  12430. _gl.vertexAttribPointer( attributes.position, 3, _gl.FLOAT, false, 0, 0 );
  12431. }
  12432. if ( object.morphTargetForcedOrder.length ) {
  12433. // set forced order
  12434. var m = 0;
  12435. var order = object.morphTargetForcedOrder;
  12436. var influences = object.morphTargetInfluences;
  12437. while ( m < material.numSupportedMorphTargets && m < order.length ) {
  12438. if ( attributes[ 'morphTarget' + m ] >= 0 ) {
  12439. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ order[ m ] ] );
  12440. enableAttribute( attributes[ 'morphTarget' + m ] );
  12441. _gl.vertexAttribPointer( attributes[ 'morphTarget' + m ], 3, _gl.FLOAT, false, 0, 0 );
  12442. }
  12443. if ( attributes[ 'morphNormal' + m ] >= 0 && material.morphNormals ) {
  12444. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ order[ m ] ] );
  12445. enableAttribute( attributes[ 'morphNormal' + m ] );
  12446. _gl.vertexAttribPointer( attributes[ 'morphNormal' + m ], 3, _gl.FLOAT, false, 0, 0 );
  12447. }
  12448. object.__webglMorphTargetInfluences[ m ] = influences[ order[ m ] ];
  12449. m ++;
  12450. }
  12451. } else {
  12452. // find the most influencing
  12453. var influence, activeInfluenceIndices = [];
  12454. var influences = object.morphTargetInfluences;
  12455. var i, il = influences.length;
  12456. for ( i = 0; i < il; i ++ ) {
  12457. influence = influences[ i ];
  12458. if ( influence > 0 ) {
  12459. activeInfluenceIndices.push( [ influence, i ] );
  12460. }
  12461. }
  12462. if ( activeInfluenceIndices.length > material.numSupportedMorphTargets ) {
  12463. activeInfluenceIndices.sort( numericalSort );
  12464. activeInfluenceIndices.length = material.numSupportedMorphTargets;
  12465. } else if ( activeInfluenceIndices.length > material.numSupportedMorphNormals ) {
  12466. activeInfluenceIndices.sort( numericalSort );
  12467. } else if ( activeInfluenceIndices.length === 0 ) {
  12468. activeInfluenceIndices.push( [ 0, 0 ] );
  12469. };
  12470. var influenceIndex, m = 0;
  12471. while ( m < material.numSupportedMorphTargets ) {
  12472. if ( activeInfluenceIndices[ m ] ) {
  12473. influenceIndex = activeInfluenceIndices[ m ][ 1 ];
  12474. if ( attributes[ 'morphTarget' + m ] >= 0 ) {
  12475. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphTargetsBuffers[ influenceIndex ] );
  12476. enableAttribute( attributes[ 'morphTarget' + m ] );
  12477. _gl.vertexAttribPointer( attributes[ 'morphTarget' + m ], 3, _gl.FLOAT, false, 0, 0 );
  12478. }
  12479. if ( attributes[ 'morphNormal' + m ] >= 0 && material.morphNormals ) {
  12480. _gl.bindBuffer( _gl.ARRAY_BUFFER, geometryGroup.__webglMorphNormalsBuffers[ influenceIndex ] );
  12481. enableAttribute( attributes[ 'morphNormal' + m ] );
  12482. _gl.vertexAttribPointer( attributes[ 'morphNormal' + m ], 3, _gl.FLOAT, false, 0, 0 );
  12483. }
  12484. object.__webglMorphTargetInfluences[ m ] = influences[ influenceIndex ];
  12485. } else {
  12486. /*
  12487. _gl.vertexAttribPointer( attributes[ "morphTarget" + m ], 3, _gl.FLOAT, false, 0, 0 );
  12488. if ( material.morphNormals ) {
  12489. _gl.vertexAttribPointer( attributes[ "morphNormal" + m ], 3, _gl.FLOAT, false, 0, 0 );
  12490. }
  12491. */
  12492. object.__webglMorphTargetInfluences[ m ] = 0;
  12493. }
  12494. m ++;
  12495. }
  12496. }
  12497. // load updated influences uniform
  12498. if ( material.program.uniforms.morphTargetInfluences !== null ) {
  12499. _gl.uniform1fv( material.program.uniforms.morphTargetInfluences, object.__webglMorphTargetInfluences );
  12500. }
  12501. }
  12502. // Sorting
  12503. function painterSortStable ( a, b ) {
  12504. if ( a.material.id !== b.material.id ) {
  12505. return b.material.id - a.material.id;
  12506. } else if ( a.z !== b.z ) {
  12507. return b.z - a.z;
  12508. } else {
  12509. return a.id - b.id;
  12510. }
  12511. }
  12512. function reversePainterSortStable ( a, b ) {
  12513. if ( a.z !== b.z ) {
  12514. return a.z - b.z;
  12515. } else {
  12516. return a.id - b.id;
  12517. }
  12518. }
  12519. function numericalSort ( a, b ) {
  12520. return b[ 0 ] - a[ 0 ];
  12521. }
  12522. // Rendering
  12523. this.render = function ( scene, camera, renderTarget, forceClear ) {
  12524. if ( camera instanceof THREE.Camera === false ) {
  12525. console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' );
  12526. return;
  12527. }
  12528. var fog = scene.fog;
  12529. // reset caching for this frame
  12530. _currentGeometryGroupHash = - 1;
  12531. _currentMaterialId = - 1;
  12532. _currentCamera = null;
  12533. _lightsNeedUpdate = true;
  12534. // update scene graph
  12535. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  12536. // update camera matrices and frustum
  12537. if ( camera.parent === undefined ) camera.updateMatrixWorld();
  12538. // update Skeleton objects
  12539. scene.traverse( function ( object ) {
  12540. if ( object instanceof THREE.SkinnedMesh ) {
  12541. object.skeleton.update();
  12542. }
  12543. } );
  12544. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  12545. _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse );
  12546. _frustum.setFromMatrix( _projScreenMatrix );
  12547. lights.length = 0;
  12548. opaqueObjects.length = 0;
  12549. transparentObjects.length = 0;
  12550. sprites.length = 0;
  12551. lensFlares.length = 0;
  12552. projectObject( scene, scene );
  12553. if ( _this.sortObjects === true ) {
  12554. opaqueObjects.sort( painterSortStable );
  12555. transparentObjects.sort( reversePainterSortStable );
  12556. }
  12557. // custom render plugins (pre pass)
  12558. shadowMapPlugin.render( scene, camera );
  12559. //
  12560. _this.info.render.calls = 0;
  12561. _this.info.render.vertices = 0;
  12562. _this.info.render.faces = 0;
  12563. _this.info.render.points = 0;
  12564. this.setRenderTarget( renderTarget );
  12565. if ( this.autoClear || forceClear ) {
  12566. this.clear( this.autoClearColor, this.autoClearDepth, this.autoClearStencil );
  12567. }
  12568. // set matrices for immediate objects
  12569. for ( var i = 0, il = _webglObjectsImmediate.length; i < il; i ++ ) {
  12570. var webglObject = _webglObjectsImmediate[ i ];
  12571. var object = webglObject.object;
  12572. if ( object.visible ) {
  12573. setupMatrices( object, camera );
  12574. unrollImmediateBufferMaterial( webglObject );
  12575. }
  12576. }
  12577. if ( scene.overrideMaterial ) {
  12578. var material = scene.overrideMaterial;
  12579. this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  12580. this.setDepthTest( material.depthTest );
  12581. this.setDepthWrite( material.depthWrite );
  12582. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  12583. renderObjects( opaqueObjects, camera, lights, fog, true, material );
  12584. renderObjects( transparentObjects, camera, lights, fog, true, material );
  12585. renderObjectsImmediate( _webglObjectsImmediate, '', camera, lights, fog, false, material );
  12586. } else {
  12587. var material = null;
  12588. // opaque pass (front-to-back order)
  12589. this.setBlending( THREE.NoBlending );
  12590. renderObjects( opaqueObjects, camera, lights, fog, false, material );
  12591. renderObjectsImmediate( _webglObjectsImmediate, 'opaque', camera, lights, fog, false, material );
  12592. // transparent pass (back-to-front order)
  12593. renderObjects( transparentObjects, camera, lights, fog, true, material );
  12594. renderObjectsImmediate( _webglObjectsImmediate, 'transparent', camera, lights, fog, true, material );
  12595. }
  12596. // custom render plugins (post pass)
  12597. spritePlugin.render( scene, camera );
  12598. lensFlarePlugin.render( scene, camera, _currentWidth, _currentHeight );
  12599. // Generate mipmap if we're using any kind of mipmap filtering
  12600. if ( renderTarget && renderTarget.generateMipmaps && renderTarget.minFilter !== THREE.NearestFilter && renderTarget.minFilter !== THREE.LinearFilter ) {
  12601. updateRenderTargetMipmap( renderTarget );
  12602. }
  12603. // Ensure depth buffer writing is enabled so it can be cleared on next render
  12604. this.setDepthTest( true );
  12605. this.setDepthWrite( true );
  12606. // _gl.finish();
  12607. };
  12608. function projectObject( scene, object ) {
  12609. if ( object.visible === false ) return;
  12610. if ( object instanceof THREE.Scene || object instanceof THREE.Group ) {
  12611. // skip
  12612. } else {
  12613. initObject( object, scene );
  12614. if ( object instanceof THREE.Light ) {
  12615. lights.push( object );
  12616. } else if ( object instanceof THREE.Sprite ) {
  12617. sprites.push( object );
  12618. } else if ( object instanceof THREE.LensFlare ) {
  12619. lensFlares.push( object );
  12620. } else {
  12621. var webglObjects = _webglObjects[ object.id ];
  12622. if ( webglObjects && ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) ) {
  12623. updateObject( object, scene );
  12624. for ( var i = 0, l = webglObjects.length; i < l; i ++ ) {
  12625. var webglObject = webglObjects[i];
  12626. unrollBufferMaterial( webglObject );
  12627. webglObject.render = true;
  12628. if ( _this.sortObjects === true ) {
  12629. if ( object.renderDepth !== null ) {
  12630. webglObject.z = object.renderDepth;
  12631. } else {
  12632. _vector3.setFromMatrixPosition( object.matrixWorld );
  12633. _vector3.applyProjection( _projScreenMatrix );
  12634. webglObject.z = _vector3.z;
  12635. }
  12636. }
  12637. }
  12638. }
  12639. }
  12640. }
  12641. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  12642. projectObject( scene, object.children[ i ] );
  12643. }
  12644. }
  12645. function renderObjects( renderList, camera, lights, fog, useBlending, overrideMaterial ) {
  12646. var material;
  12647. for ( var i = renderList.length - 1; i !== - 1; i -- ) {
  12648. var webglObject = renderList[ i ];
  12649. var object = webglObject.object;
  12650. var buffer = webglObject.buffer;
  12651. setupMatrices( object, camera );
  12652. if ( overrideMaterial ) {
  12653. material = overrideMaterial;
  12654. } else {
  12655. material = webglObject.material;
  12656. if ( ! material ) continue;
  12657. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  12658. _this.setDepthTest( material.depthTest );
  12659. _this.setDepthWrite( material.depthWrite );
  12660. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  12661. }
  12662. _this.setMaterialFaces( material );
  12663. if ( buffer instanceof THREE.BufferGeometry ) {
  12664. _this.renderBufferDirect( camera, lights, fog, material, buffer, object );
  12665. } else {
  12666. _this.renderBuffer( camera, lights, fog, material, buffer, object );
  12667. }
  12668. }
  12669. }
  12670. function renderObjectsImmediate ( renderList, materialType, camera, lights, fog, useBlending, overrideMaterial ) {
  12671. var material;
  12672. for ( var i = 0, il = renderList.length; i < il; i ++ ) {
  12673. var webglObject = renderList[ i ];
  12674. var object = webglObject.object;
  12675. if ( object.visible ) {
  12676. if ( overrideMaterial ) {
  12677. material = overrideMaterial;
  12678. } else {
  12679. material = webglObject[ materialType ];
  12680. if ( ! material ) continue;
  12681. if ( useBlending ) _this.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  12682. _this.setDepthTest( material.depthTest );
  12683. _this.setDepthWrite( material.depthWrite );
  12684. setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits );
  12685. }
  12686. _this.renderImmediateObject( camera, lights, fog, material, object );
  12687. }
  12688. }
  12689. }
  12690. this.renderImmediateObject = function ( camera, lights, fog, material, object ) {
  12691. var program = setProgram( camera, lights, fog, material, object );
  12692. _currentGeometryGroupHash = - 1;
  12693. _this.setMaterialFaces( material );
  12694. if ( object.immediateRenderCallback ) {
  12695. object.immediateRenderCallback( program, _gl, _frustum );
  12696. } else {
  12697. object.render( function ( object ) { _this.renderBufferImmediate( object, program, material ); } );
  12698. }
  12699. };
  12700. function unrollImmediateBufferMaterial ( globject ) {
  12701. var object = globject.object,
  12702. material = object.material;
  12703. if ( material.transparent ) {
  12704. globject.transparent = material;
  12705. globject.opaque = null;
  12706. } else {
  12707. globject.opaque = material;
  12708. globject.transparent = null;
  12709. }
  12710. }
  12711. function unrollBufferMaterial ( globject ) {
  12712. var object = globject.object;
  12713. var buffer = globject.buffer;
  12714. var geometry = object.geometry;
  12715. var material = object.material;
  12716. if ( material instanceof THREE.MeshFaceMaterial ) {
  12717. var materialIndex = geometry instanceof THREE.BufferGeometry ? 0 : buffer.materialIndex;
  12718. material = material.materials[ materialIndex ];
  12719. globject.material = material;
  12720. if ( material.transparent ) {
  12721. transparentObjects.push( globject );
  12722. } else {
  12723. opaqueObjects.push( globject );
  12724. }
  12725. } else if ( material ) {
  12726. globject.material = material;
  12727. if ( material.transparent ) {
  12728. transparentObjects.push( globject );
  12729. } else {
  12730. opaqueObjects.push( globject );
  12731. }
  12732. }
  12733. }
  12734. function initObject( object, scene ) {
  12735. if ( object.__webglInit === undefined ) {
  12736. object.__webglInit = true;
  12737. object._modelViewMatrix = new THREE.Matrix4();
  12738. object._normalMatrix = new THREE.Matrix3();
  12739. object.addEventListener( 'removed', onObjectRemoved );
  12740. }
  12741. var geometry = object.geometry;
  12742. if ( geometry === undefined ) {
  12743. // ImmediateRenderObject
  12744. } else if ( geometry.__webglInit === undefined ) {
  12745. geometry.__webglInit = true;
  12746. geometry.addEventListener( 'dispose', onGeometryDispose );
  12747. if ( geometry instanceof THREE.BufferGeometry ) {
  12748. //
  12749. } else if ( object instanceof THREE.Mesh ) {
  12750. initGeometryGroups( scene, object, geometry );
  12751. } else if ( object instanceof THREE.Line ) {
  12752. if ( geometry.__webglVertexBuffer === undefined ) {
  12753. createLineBuffers( geometry );
  12754. initLineBuffers( geometry, object );
  12755. geometry.verticesNeedUpdate = true;
  12756. geometry.colorsNeedUpdate = true;
  12757. geometry.lineDistancesNeedUpdate = true;
  12758. }
  12759. } else if ( object instanceof THREE.PointCloud ) {
  12760. if ( geometry.__webglVertexBuffer === undefined ) {
  12761. createParticleBuffers( geometry );
  12762. initParticleBuffers( geometry, object );
  12763. geometry.verticesNeedUpdate = true;
  12764. geometry.colorsNeedUpdate = true;
  12765. }
  12766. }
  12767. }
  12768. if ( object.__webglActive === undefined) {
  12769. object.__webglActive = true;
  12770. if ( object instanceof THREE.Mesh ) {
  12771. if ( geometry instanceof THREE.BufferGeometry ) {
  12772. addBuffer( _webglObjects, geometry, object );
  12773. } else if ( geometry instanceof THREE.Geometry ) {
  12774. var geometryGroupsList = geometryGroups[ geometry.id ];
  12775. for ( var i = 0,l = geometryGroupsList.length; i < l; i ++ ) {
  12776. addBuffer( _webglObjects, geometryGroupsList[ i ], object );
  12777. }
  12778. }
  12779. } else if ( object instanceof THREE.Line || object instanceof THREE.PointCloud ) {
  12780. addBuffer( _webglObjects, geometry, object );
  12781. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  12782. addBufferImmediate( _webglObjectsImmediate, object );
  12783. }
  12784. }
  12785. }
  12786. // Geometry splitting
  12787. var geometryGroups = {};
  12788. var geometryGroupCounter = 0;
  12789. function makeGroups( geometry, usesFaceMaterial ) {
  12790. var maxVerticesInGroup = extensions.get( 'OES_element_index_uint' ) ? 4294967296 : 65535;
  12791. var groupHash, hash_map = {};
  12792. var numMorphTargets = geometry.morphTargets.length;
  12793. var numMorphNormals = geometry.morphNormals.length;
  12794. var group;
  12795. var groups = {};
  12796. var groupsList = [];
  12797. for ( var f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  12798. var face = geometry.faces[ f ];
  12799. var materialIndex = usesFaceMaterial ? face.materialIndex : 0;
  12800. if ( ! ( materialIndex in hash_map ) ) {
  12801. hash_map[ materialIndex ] = { hash: materialIndex, counter: 0 };
  12802. }
  12803. groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter;
  12804. if ( ! ( groupHash in groups ) ) {
  12805. group = {
  12806. id: geometryGroupCounter ++,
  12807. faces3: [],
  12808. materialIndex: materialIndex,
  12809. vertices: 0,
  12810. numMorphTargets: numMorphTargets,
  12811. numMorphNormals: numMorphNormals
  12812. };
  12813. groups[ groupHash ] = group;
  12814. groupsList.push( group );
  12815. }
  12816. if ( groups[ groupHash ].vertices + 3 > maxVerticesInGroup ) {
  12817. hash_map[ materialIndex ].counter += 1;
  12818. groupHash = hash_map[ materialIndex ].hash + '_' + hash_map[ materialIndex ].counter;
  12819. if ( ! ( groupHash in groups ) ) {
  12820. group = {
  12821. id: geometryGroupCounter ++,
  12822. faces3: [],
  12823. materialIndex: materialIndex,
  12824. vertices: 0,
  12825. numMorphTargets: numMorphTargets,
  12826. numMorphNormals: numMorphNormals
  12827. };
  12828. groups[ groupHash ] = group;
  12829. groupsList.push( group );
  12830. }
  12831. }
  12832. groups[ groupHash ].faces3.push( f );
  12833. groups[ groupHash ].vertices += 3;
  12834. }
  12835. return groupsList;
  12836. }
  12837. function initGeometryGroups( scene, object, geometry ) {
  12838. var material = object.material, addBuffers = false;
  12839. if ( geometryGroups[ geometry.id ] === undefined || geometry.groupsNeedUpdate === true ) {
  12840. delete _webglObjects[ object.id ];
  12841. geometryGroups[ geometry.id ] = makeGroups( geometry, material instanceof THREE.MeshFaceMaterial );
  12842. geometry.groupsNeedUpdate = false;
  12843. }
  12844. var geometryGroupsList = geometryGroups[ geometry.id ];
  12845. // create separate VBOs per geometry chunk
  12846. for ( var i = 0, il = geometryGroupsList.length; i < il; i ++ ) {
  12847. var geometryGroup = geometryGroupsList[ i ];
  12848. // initialise VBO on the first access
  12849. if ( geometryGroup.__webglVertexBuffer === undefined ) {
  12850. createMeshBuffers( geometryGroup );
  12851. initMeshBuffers( geometryGroup, object );
  12852. geometry.verticesNeedUpdate = true;
  12853. geometry.morphTargetsNeedUpdate = true;
  12854. geometry.elementsNeedUpdate = true;
  12855. geometry.uvsNeedUpdate = true;
  12856. geometry.normalsNeedUpdate = true;
  12857. geometry.tangentsNeedUpdate = true;
  12858. geometry.colorsNeedUpdate = true;
  12859. addBuffers = true;
  12860. } else {
  12861. addBuffers = false;
  12862. }
  12863. if ( addBuffers || object.__webglActive === undefined ) {
  12864. addBuffer( _webglObjects, geometryGroup, object );
  12865. }
  12866. }
  12867. object.__webglActive = true;
  12868. }
  12869. function addBuffer( objlist, buffer, object ) {
  12870. var id = object.id;
  12871. objlist[id] = objlist[id] || [];
  12872. objlist[id].push(
  12873. {
  12874. id: id,
  12875. buffer: buffer,
  12876. object: object,
  12877. material: null,
  12878. z: 0
  12879. }
  12880. );
  12881. };
  12882. function addBufferImmediate( objlist, object ) {
  12883. objlist.push(
  12884. {
  12885. id: null,
  12886. object: object,
  12887. opaque: null,
  12888. transparent: null,
  12889. z: 0
  12890. }
  12891. );
  12892. };
  12893. // Objects updates
  12894. function updateObject( object, scene ) {
  12895. var geometry = object.geometry, customAttributesDirty, material;
  12896. if ( geometry instanceof THREE.BufferGeometry ) {
  12897. setDirectBuffers( geometry );
  12898. } else if ( object instanceof THREE.Mesh ) {
  12899. // check all geometry groups
  12900. if ( geometry.groupsNeedUpdate === true ) {
  12901. initGeometryGroups( scene, object, geometry );
  12902. }
  12903. var geometryGroupsList = geometryGroups[ geometry.id ];
  12904. for ( var i = 0, il = geometryGroupsList.length; i < il; i ++ ) {
  12905. var geometryGroup = geometryGroupsList[ i ];
  12906. material = getBufferMaterial( object, geometryGroup );
  12907. if ( geometry.groupsNeedUpdate === true ) {
  12908. initMeshBuffers( geometryGroup, object );
  12909. }
  12910. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  12911. if ( geometry.verticesNeedUpdate || geometry.morphTargetsNeedUpdate || geometry.elementsNeedUpdate ||
  12912. geometry.uvsNeedUpdate || geometry.normalsNeedUpdate ||
  12913. geometry.colorsNeedUpdate || geometry.tangentsNeedUpdate || customAttributesDirty ) {
  12914. setMeshBuffers( geometryGroup, object, _gl.DYNAMIC_DRAW, ! geometry.dynamic, material );
  12915. }
  12916. }
  12917. geometry.verticesNeedUpdate = false;
  12918. geometry.morphTargetsNeedUpdate = false;
  12919. geometry.elementsNeedUpdate = false;
  12920. geometry.uvsNeedUpdate = false;
  12921. geometry.normalsNeedUpdate = false;
  12922. geometry.colorsNeedUpdate = false;
  12923. geometry.tangentsNeedUpdate = false;
  12924. material.attributes && clearCustomAttributes( material );
  12925. } else if ( object instanceof THREE.Line ) {
  12926. material = getBufferMaterial( object, geometry );
  12927. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  12928. if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || geometry.lineDistancesNeedUpdate || customAttributesDirty ) {
  12929. setLineBuffers( geometry, _gl.DYNAMIC_DRAW );
  12930. }
  12931. geometry.verticesNeedUpdate = false;
  12932. geometry.colorsNeedUpdate = false;
  12933. geometry.lineDistancesNeedUpdate = false;
  12934. material.attributes && clearCustomAttributes( material );
  12935. } else if ( object instanceof THREE.PointCloud ) {
  12936. material = getBufferMaterial( object, geometry );
  12937. customAttributesDirty = material.attributes && areCustomAttributesDirty( material );
  12938. if ( geometry.verticesNeedUpdate || geometry.colorsNeedUpdate || customAttributesDirty ) {
  12939. setParticleBuffers( geometry, _gl.DYNAMIC_DRAW, object );
  12940. }
  12941. geometry.verticesNeedUpdate = false;
  12942. geometry.colorsNeedUpdate = false;
  12943. material.attributes && clearCustomAttributes( material );
  12944. }
  12945. }
  12946. // Objects updates - custom attributes check
  12947. function areCustomAttributesDirty( material ) {
  12948. for ( var name in material.attributes ) {
  12949. if ( material.attributes[ name ].needsUpdate ) return true;
  12950. }
  12951. return false;
  12952. }
  12953. function clearCustomAttributes( material ) {
  12954. for ( var name in material.attributes ) {
  12955. material.attributes[ name ].needsUpdate = false;
  12956. }
  12957. }
  12958. // Objects removal
  12959. function removeObject( object ) {
  12960. if ( object instanceof THREE.Mesh ||
  12961. object instanceof THREE.PointCloud ||
  12962. object instanceof THREE.Line ) {
  12963. delete _webglObjects[ object.id ];
  12964. } else if ( object instanceof THREE.ImmediateRenderObject || object.immediateRenderCallback ) {
  12965. removeInstances( _webglObjectsImmediate, object );
  12966. }
  12967. delete object.__webglInit;
  12968. delete object._modelViewMatrix;
  12969. delete object._normalMatrix;
  12970. delete object.__webglActive;
  12971. }
  12972. function removeInstances( objlist, object ) {
  12973. for ( var o = objlist.length - 1; o >= 0; o -- ) {
  12974. if ( objlist[ o ].object === object ) {
  12975. objlist.splice( o, 1 );
  12976. }
  12977. }
  12978. }
  12979. // Materials
  12980. function initMaterial( material, lights, fog, object ) {
  12981. material.addEventListener( 'dispose', onMaterialDispose );
  12982. var shaderID;
  12983. if ( material instanceof THREE.MeshDepthMaterial ) {
  12984. shaderID = 'depth';
  12985. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  12986. shaderID = 'normal';
  12987. } else if ( material instanceof THREE.MeshBasicMaterial ) {
  12988. shaderID = 'basic';
  12989. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  12990. shaderID = 'lambert';
  12991. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  12992. shaderID = 'phong';
  12993. } else if ( material instanceof THREE.LineBasicMaterial ) {
  12994. shaderID = 'basic';
  12995. } else if ( material instanceof THREE.LineDashedMaterial ) {
  12996. shaderID = 'dashed';
  12997. } else if ( material instanceof THREE.PointCloudMaterial ) {
  12998. shaderID = 'particle_basic';
  12999. }
  13000. if ( shaderID ) {
  13001. var shader = THREE.ShaderLib[ shaderID ];
  13002. material.__webglShader = {
  13003. uniforms: THREE.UniformsUtils.clone( shader.uniforms ),
  13004. vertexShader: shader.vertexShader,
  13005. fragmentShader: shader.fragmentShader
  13006. }
  13007. } else {
  13008. material.__webglShader = {
  13009. uniforms: material.uniforms,
  13010. vertexShader: material.vertexShader,
  13011. fragmentShader: material.fragmentShader
  13012. }
  13013. }
  13014. // heuristics to create shader parameters according to lights in the scene
  13015. // (not to blow over maxLights budget)
  13016. var maxLightCount = allocateLights( lights );
  13017. var maxShadows = allocateShadows( lights );
  13018. var maxBones = allocateBones( object );
  13019. var parameters = {
  13020. precision: _precision,
  13021. supportsVertexTextures: _supportsVertexTextures,
  13022. map: !! material.map,
  13023. envMap: !! material.envMap,
  13024. lightMap: !! material.lightMap,
  13025. bumpMap: !! material.bumpMap,
  13026. normalMap: !! material.normalMap,
  13027. specularMap: !! material.specularMap,
  13028. alphaMap: !! material.alphaMap,
  13029. vertexColors: material.vertexColors,
  13030. fog: fog,
  13031. useFog: material.fog,
  13032. fogExp: fog instanceof THREE.FogExp2,
  13033. sizeAttenuation: material.sizeAttenuation,
  13034. logarithmicDepthBuffer: _logarithmicDepthBuffer,
  13035. skinning: material.skinning,
  13036. maxBones: maxBones,
  13037. useVertexTexture: _supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture,
  13038. morphTargets: material.morphTargets,
  13039. morphNormals: material.morphNormals,
  13040. maxMorphTargets: _this.maxMorphTargets,
  13041. maxMorphNormals: _this.maxMorphNormals,
  13042. maxDirLights: maxLightCount.directional,
  13043. maxPointLights: maxLightCount.point,
  13044. maxSpotLights: maxLightCount.spot,
  13045. maxHemiLights: maxLightCount.hemi,
  13046. maxShadows: maxShadows,
  13047. shadowMapEnabled: _this.shadowMapEnabled && object.receiveShadow && maxShadows > 0,
  13048. shadowMapType: _this.shadowMapType,
  13049. shadowMapDebug: _this.shadowMapDebug,
  13050. shadowMapCascade: _this.shadowMapCascade,
  13051. alphaTest: material.alphaTest,
  13052. metal: material.metal,
  13053. wrapAround: material.wrapAround,
  13054. doubleSided: material.side === THREE.DoubleSide,
  13055. flipSided: material.side === THREE.BackSide
  13056. };
  13057. // Generate code
  13058. var chunks = [];
  13059. if ( shaderID ) {
  13060. chunks.push( shaderID );
  13061. } else {
  13062. chunks.push( material.fragmentShader );
  13063. chunks.push( material.vertexShader );
  13064. }
  13065. if ( material.defines !== undefined ) {
  13066. for ( var name in material.defines ) {
  13067. chunks.push( name );
  13068. chunks.push( material.defines[ name ] );
  13069. }
  13070. }
  13071. for ( var name in parameters ) {
  13072. chunks.push( name );
  13073. chunks.push( parameters[ name ] );
  13074. }
  13075. var code = chunks.join();
  13076. var program;
  13077. // Check if code has been already compiled
  13078. for ( var p = 0, pl = _programs.length; p < pl; p ++ ) {
  13079. var programInfo = _programs[ p ];
  13080. if ( programInfo.code === code ) {
  13081. program = programInfo;
  13082. program.usedTimes ++;
  13083. break;
  13084. }
  13085. }
  13086. if ( program === undefined ) {
  13087. program = new THREE.WebGLProgram( _this, code, material, parameters );
  13088. _programs.push( program );
  13089. _this.info.memory.programs = _programs.length;
  13090. }
  13091. material.program = program;
  13092. var attributes = program.attributes;
  13093. if ( material.morphTargets ) {
  13094. material.numSupportedMorphTargets = 0;
  13095. var id, base = 'morphTarget';
  13096. for ( var i = 0; i < _this.maxMorphTargets; i ++ ) {
  13097. id = base + i;
  13098. if ( attributes[ id ] >= 0 ) {
  13099. material.numSupportedMorphTargets ++;
  13100. }
  13101. }
  13102. }
  13103. if ( material.morphNormals ) {
  13104. material.numSupportedMorphNormals = 0;
  13105. var id, base = 'morphNormal';
  13106. for ( i = 0; i < _this.maxMorphNormals; i ++ ) {
  13107. id = base + i;
  13108. if ( attributes[ id ] >= 0 ) {
  13109. material.numSupportedMorphNormals ++;
  13110. }
  13111. }
  13112. }
  13113. material.uniformsList = [];
  13114. for ( var u in material.__webglShader.uniforms ) {
  13115. var location = material.program.uniforms[ u ];
  13116. if ( location ) {
  13117. material.uniformsList.push( [ material.__webglShader.uniforms[ u ], location ] );
  13118. }
  13119. }
  13120. }
  13121. function setProgram( camera, lights, fog, material, object ) {
  13122. _usedTextureUnits = 0;
  13123. if ( material.needsUpdate ) {
  13124. if ( material.program ) deallocateMaterial( material );
  13125. initMaterial( material, lights, fog, object );
  13126. material.needsUpdate = false;
  13127. }
  13128. if ( material.morphTargets ) {
  13129. if ( ! object.__webglMorphTargetInfluences ) {
  13130. object.__webglMorphTargetInfluences = new Float32Array( _this.maxMorphTargets );
  13131. }
  13132. }
  13133. var refreshProgram = false;
  13134. var refreshMaterial = false;
  13135. var refreshLights = false;
  13136. var program = material.program,
  13137. p_uniforms = program.uniforms,
  13138. m_uniforms = material.__webglShader.uniforms;
  13139. if ( program.id !== _currentProgram ) {
  13140. _gl.useProgram( program.program );
  13141. _currentProgram = program.id;
  13142. refreshProgram = true;
  13143. refreshMaterial = true;
  13144. refreshLights = true;
  13145. }
  13146. if ( material.id !== _currentMaterialId ) {
  13147. if ( _currentMaterialId === -1 ) refreshLights = true;
  13148. _currentMaterialId = material.id;
  13149. refreshMaterial = true;
  13150. }
  13151. if ( refreshProgram || camera !== _currentCamera ) {
  13152. _gl.uniformMatrix4fv( p_uniforms.projectionMatrix, false, camera.projectionMatrix.elements );
  13153. if ( _logarithmicDepthBuffer ) {
  13154. _gl.uniform1f( p_uniforms.logDepthBufFC, 2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) );
  13155. }
  13156. if ( camera !== _currentCamera ) _currentCamera = camera;
  13157. // load material specific uniforms
  13158. // (shader material also gets them for the sake of genericity)
  13159. if ( material instanceof THREE.ShaderMaterial ||
  13160. material instanceof THREE.MeshPhongMaterial ||
  13161. material.envMap ) {
  13162. if ( p_uniforms.cameraPosition !== null ) {
  13163. _vector3.setFromMatrixPosition( camera.matrixWorld );
  13164. _gl.uniform3f( p_uniforms.cameraPosition, _vector3.x, _vector3.y, _vector3.z );
  13165. }
  13166. }
  13167. if ( material instanceof THREE.MeshPhongMaterial ||
  13168. material instanceof THREE.MeshLambertMaterial ||
  13169. material instanceof THREE.MeshBasicMaterial ||
  13170. material instanceof THREE.ShaderMaterial ||
  13171. material.skinning ) {
  13172. if ( p_uniforms.viewMatrix !== null ) {
  13173. _gl.uniformMatrix4fv( p_uniforms.viewMatrix, false, camera.matrixWorldInverse.elements );
  13174. }
  13175. }
  13176. }
  13177. // skinning uniforms must be set even if material didn't change
  13178. // auto-setting of texture unit for bone texture must go before other textures
  13179. // not sure why, but otherwise weird things happen
  13180. if ( material.skinning ) {
  13181. if ( object.bindMatrix && p_uniforms.bindMatrix !== null ) {
  13182. _gl.uniformMatrix4fv( p_uniforms.bindMatrix, false, object.bindMatrix.elements );
  13183. }
  13184. if ( object.bindMatrixInverse && p_uniforms.bindMatrixInverse !== null ) {
  13185. _gl.uniformMatrix4fv( p_uniforms.bindMatrixInverse, false, object.bindMatrixInverse.elements );
  13186. }
  13187. if ( _supportsBoneTextures && object.skeleton && object.skeleton.useVertexTexture ) {
  13188. if ( p_uniforms.boneTexture !== null ) {
  13189. var textureUnit = getTextureUnit();
  13190. _gl.uniform1i( p_uniforms.boneTexture, textureUnit );
  13191. _this.setTexture( object.skeleton.boneTexture, textureUnit );
  13192. }
  13193. if ( p_uniforms.boneTextureWidth !== null ) {
  13194. _gl.uniform1i( p_uniforms.boneTextureWidth, object.skeleton.boneTextureWidth );
  13195. }
  13196. if ( p_uniforms.boneTextureHeight !== null ) {
  13197. _gl.uniform1i( p_uniforms.boneTextureHeight, object.skeleton.boneTextureHeight );
  13198. }
  13199. } else if ( object.skeleton && object.skeleton.boneMatrices ) {
  13200. if ( p_uniforms.boneGlobalMatrices !== null ) {
  13201. _gl.uniformMatrix4fv( p_uniforms.boneGlobalMatrices, false, object.skeleton.boneMatrices );
  13202. }
  13203. }
  13204. }
  13205. if ( refreshMaterial ) {
  13206. // refresh uniforms common to several materials
  13207. if ( fog && material.fog ) {
  13208. refreshUniformsFog( m_uniforms, fog );
  13209. }
  13210. if ( material instanceof THREE.MeshPhongMaterial ||
  13211. material instanceof THREE.MeshLambertMaterial ||
  13212. material.lights ) {
  13213. if ( _lightsNeedUpdate ) {
  13214. refreshLights = true;
  13215. setupLights( lights );
  13216. _lightsNeedUpdate = false;
  13217. }
  13218. if ( refreshLights ) {
  13219. refreshUniformsLights( m_uniforms, _lights );
  13220. markUniformsLightsNeedsUpdate( m_uniforms, true );
  13221. } else {
  13222. markUniformsLightsNeedsUpdate( m_uniforms, false );
  13223. }
  13224. }
  13225. if ( material instanceof THREE.MeshBasicMaterial ||
  13226. material instanceof THREE.MeshLambertMaterial ||
  13227. material instanceof THREE.MeshPhongMaterial ) {
  13228. refreshUniformsCommon( m_uniforms, material );
  13229. }
  13230. // refresh single material specific uniforms
  13231. if ( material instanceof THREE.LineBasicMaterial ) {
  13232. refreshUniformsLine( m_uniforms, material );
  13233. } else if ( material instanceof THREE.LineDashedMaterial ) {
  13234. refreshUniformsLine( m_uniforms, material );
  13235. refreshUniformsDash( m_uniforms, material );
  13236. } else if ( material instanceof THREE.PointCloudMaterial ) {
  13237. refreshUniformsParticle( m_uniforms, material );
  13238. } else if ( material instanceof THREE.MeshPhongMaterial ) {
  13239. refreshUniformsPhong( m_uniforms, material );
  13240. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  13241. refreshUniformsLambert( m_uniforms, material );
  13242. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  13243. m_uniforms.mNear.value = camera.near;
  13244. m_uniforms.mFar.value = camera.far;
  13245. m_uniforms.opacity.value = material.opacity;
  13246. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  13247. m_uniforms.opacity.value = material.opacity;
  13248. }
  13249. if ( object.receiveShadow && ! material._shadowPass ) {
  13250. refreshUniformsShadow( m_uniforms, lights );
  13251. }
  13252. // load common uniforms
  13253. loadUniformsGeneric( material.uniformsList );
  13254. }
  13255. loadUniformsMatrices( p_uniforms, object );
  13256. if ( p_uniforms.modelMatrix !== null ) {
  13257. _gl.uniformMatrix4fv( p_uniforms.modelMatrix, false, object.matrixWorld.elements );
  13258. }
  13259. return program;
  13260. }
  13261. // Uniforms (refresh uniforms objects)
  13262. function refreshUniformsCommon ( uniforms, material ) {
  13263. uniforms.opacity.value = material.opacity;
  13264. if ( _this.gammaInput ) {
  13265. uniforms.diffuse.value.copyGammaToLinear( material.color );
  13266. } else {
  13267. uniforms.diffuse.value = material.color;
  13268. }
  13269. uniforms.map.value = material.map;
  13270. uniforms.lightMap.value = material.lightMap;
  13271. uniforms.specularMap.value = material.specularMap;
  13272. uniforms.alphaMap.value = material.alphaMap;
  13273. if ( material.bumpMap ) {
  13274. uniforms.bumpMap.value = material.bumpMap;
  13275. uniforms.bumpScale.value = material.bumpScale;
  13276. }
  13277. if ( material.normalMap ) {
  13278. uniforms.normalMap.value = material.normalMap;
  13279. uniforms.normalScale.value.copy( material.normalScale );
  13280. }
  13281. // uv repeat and offset setting priorities
  13282. // 1. color map
  13283. // 2. specular map
  13284. // 3. normal map
  13285. // 4. bump map
  13286. // 5. alpha map
  13287. var uvScaleMap;
  13288. if ( material.map ) {
  13289. uvScaleMap = material.map;
  13290. } else if ( material.specularMap ) {
  13291. uvScaleMap = material.specularMap;
  13292. } else if ( material.normalMap ) {
  13293. uvScaleMap = material.normalMap;
  13294. } else if ( material.bumpMap ) {
  13295. uvScaleMap = material.bumpMap;
  13296. } else if ( material.alphaMap ) {
  13297. uvScaleMap = material.alphaMap;
  13298. }
  13299. if ( uvScaleMap !== undefined ) {
  13300. var offset = uvScaleMap.offset;
  13301. var repeat = uvScaleMap.repeat;
  13302. uniforms.offsetRepeat.value.set( offset.x, offset.y, repeat.x, repeat.y );
  13303. }
  13304. uniforms.envMap.value = material.envMap;
  13305. uniforms.flipEnvMap.value = ( material.envMap instanceof THREE.WebGLRenderTargetCube ) ? 1 : - 1;
  13306. if ( _this.gammaInput ) {
  13307. //uniforms.reflectivity.value = material.reflectivity * material.reflectivity;
  13308. uniforms.reflectivity.value = material.reflectivity;
  13309. } else {
  13310. uniforms.reflectivity.value = material.reflectivity;
  13311. }
  13312. uniforms.refractionRatio.value = material.refractionRatio;
  13313. uniforms.combine.value = material.combine;
  13314. uniforms.useRefract.value = material.envMap && (
  13315. material.envMap.mapping === THREE.CubeRefractionMapping ||
  13316. material.envMap.mapping === THREE.EquirectangularRefractionMapping );
  13317. }
  13318. function refreshUniformsLine ( uniforms, material ) {
  13319. uniforms.diffuse.value = material.color;
  13320. uniforms.opacity.value = material.opacity;
  13321. }
  13322. function refreshUniformsDash ( uniforms, material ) {
  13323. uniforms.dashSize.value = material.dashSize;
  13324. uniforms.totalSize.value = material.dashSize + material.gapSize;
  13325. uniforms.scale.value = material.scale;
  13326. }
  13327. function refreshUniformsParticle ( uniforms, material ) {
  13328. uniforms.psColor.value = material.color;
  13329. uniforms.opacity.value = material.opacity;
  13330. uniforms.size.value = material.size;
  13331. uniforms.scale.value = _canvas.height / 2.0; // TODO: Cache this.
  13332. uniforms.map.value = material.map;
  13333. }
  13334. function refreshUniformsFog ( uniforms, fog ) {
  13335. uniforms.fogColor.value = fog.color;
  13336. if ( fog instanceof THREE.Fog ) {
  13337. uniforms.fogNear.value = fog.near;
  13338. uniforms.fogFar.value = fog.far;
  13339. } else if ( fog instanceof THREE.FogExp2 ) {
  13340. uniforms.fogDensity.value = fog.density;
  13341. }
  13342. }
  13343. function refreshUniformsPhong ( uniforms, material ) {
  13344. uniforms.shininess.value = material.shininess;
  13345. if ( _this.gammaInput ) {
  13346. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  13347. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  13348. uniforms.specular.value.copyGammaToLinear( material.specular );
  13349. } else {
  13350. uniforms.ambient.value = material.ambient;
  13351. uniforms.emissive.value = material.emissive;
  13352. uniforms.specular.value = material.specular;
  13353. }
  13354. if ( material.wrapAround ) {
  13355. uniforms.wrapRGB.value.copy( material.wrapRGB );
  13356. }
  13357. }
  13358. function refreshUniformsLambert ( uniforms, material ) {
  13359. if ( _this.gammaInput ) {
  13360. uniforms.ambient.value.copyGammaToLinear( material.ambient );
  13361. uniforms.emissive.value.copyGammaToLinear( material.emissive );
  13362. } else {
  13363. uniforms.ambient.value = material.ambient;
  13364. uniforms.emissive.value = material.emissive;
  13365. }
  13366. if ( material.wrapAround ) {
  13367. uniforms.wrapRGB.value.copy( material.wrapRGB );
  13368. }
  13369. }
  13370. function refreshUniformsLights ( uniforms, lights ) {
  13371. uniforms.ambientLightColor.value = lights.ambient;
  13372. uniforms.directionalLightColor.value = lights.directional.colors;
  13373. uniforms.directionalLightDirection.value = lights.directional.positions;
  13374. uniforms.pointLightColor.value = lights.point.colors;
  13375. uniforms.pointLightPosition.value = lights.point.positions;
  13376. uniforms.pointLightDistance.value = lights.point.distances;
  13377. uniforms.spotLightColor.value = lights.spot.colors;
  13378. uniforms.spotLightPosition.value = lights.spot.positions;
  13379. uniforms.spotLightDistance.value = lights.spot.distances;
  13380. uniforms.spotLightDirection.value = lights.spot.directions;
  13381. uniforms.spotLightAngleCos.value = lights.spot.anglesCos;
  13382. uniforms.spotLightExponent.value = lights.spot.exponents;
  13383. uniforms.hemisphereLightSkyColor.value = lights.hemi.skyColors;
  13384. uniforms.hemisphereLightGroundColor.value = lights.hemi.groundColors;
  13385. uniforms.hemisphereLightDirection.value = lights.hemi.positions;
  13386. }
  13387. // If uniforms are marked as clean, they don't need to be loaded to the GPU.
  13388. function markUniformsLightsNeedsUpdate ( uniforms, boolean ) {
  13389. uniforms.ambientLightColor.needsUpdate = boolean;
  13390. uniforms.directionalLightColor.needsUpdate = boolean;
  13391. uniforms.directionalLightDirection.needsUpdate = boolean;
  13392. uniforms.pointLightColor.needsUpdate = boolean;
  13393. uniforms.pointLightPosition.needsUpdate = boolean;
  13394. uniforms.pointLightDistance.needsUpdate = boolean;
  13395. uniforms.spotLightColor.needsUpdate = boolean;
  13396. uniforms.spotLightPosition.needsUpdate = boolean;
  13397. uniforms.spotLightDistance.needsUpdate = boolean;
  13398. uniforms.spotLightDirection.needsUpdate = boolean;
  13399. uniforms.spotLightAngleCos.needsUpdate = boolean;
  13400. uniforms.spotLightExponent.needsUpdate = boolean;
  13401. uniforms.hemisphereLightSkyColor.needsUpdate = boolean;
  13402. uniforms.hemisphereLightGroundColor.needsUpdate = boolean;
  13403. uniforms.hemisphereLightDirection.needsUpdate = boolean;
  13404. }
  13405. function refreshUniformsShadow ( uniforms, lights ) {
  13406. if ( uniforms.shadowMatrix ) {
  13407. var j = 0;
  13408. for ( var i = 0, il = lights.length; i < il; i ++ ) {
  13409. var light = lights[ i ];
  13410. if ( ! light.castShadow ) continue;
  13411. if ( light instanceof THREE.SpotLight || ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) ) {
  13412. uniforms.shadowMap.value[ j ] = light.shadowMap;
  13413. uniforms.shadowMapSize.value[ j ] = light.shadowMapSize;
  13414. uniforms.shadowMatrix.value[ j ] = light.shadowMatrix;
  13415. uniforms.shadowDarkness.value[ j ] = light.shadowDarkness;
  13416. uniforms.shadowBias.value[ j ] = light.shadowBias;
  13417. j ++;
  13418. }
  13419. }
  13420. }
  13421. }
  13422. // Uniforms (load to GPU)
  13423. function loadUniformsMatrices ( uniforms, object ) {
  13424. _gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, object._modelViewMatrix.elements );
  13425. if ( uniforms.normalMatrix ) {
  13426. _gl.uniformMatrix3fv( uniforms.normalMatrix, false, object._normalMatrix.elements );
  13427. }
  13428. }
  13429. function getTextureUnit() {
  13430. var textureUnit = _usedTextureUnits;
  13431. if ( textureUnit >= _maxTextures ) {
  13432. console.warn( 'WebGLRenderer: trying to use ' + textureUnit + ' texture units while this GPU supports only ' + _maxTextures );
  13433. }
  13434. _usedTextureUnits += 1;
  13435. return textureUnit;
  13436. }
  13437. function loadUniformsGeneric ( uniforms ) {
  13438. var texture, textureUnit, offset;
  13439. for ( var j = 0, jl = uniforms.length; j < jl; j ++ ) {
  13440. var uniform = uniforms[ j ][ 0 ];
  13441. // needsUpdate property is not added to all uniforms.
  13442. if ( uniform.needsUpdate === false ) continue;
  13443. var type = uniform.type;
  13444. var value = uniform.value;
  13445. var location = uniforms[ j ][ 1 ];
  13446. switch ( type ) {
  13447. case '1i':
  13448. _gl.uniform1i( location, value );
  13449. break;
  13450. case '1f':
  13451. _gl.uniform1f( location, value );
  13452. break;
  13453. case '2f':
  13454. _gl.uniform2f( location, value[ 0 ], value[ 1 ] );
  13455. break;
  13456. case '3f':
  13457. _gl.uniform3f( location, value[ 0 ], value[ 1 ], value[ 2 ] );
  13458. break;
  13459. case '4f':
  13460. _gl.uniform4f( location, value[ 0 ], value[ 1 ], value[ 2 ], value[ 3 ] );
  13461. break;
  13462. case '1iv':
  13463. _gl.uniform1iv( location, value );
  13464. break;
  13465. case '3iv':
  13466. _gl.uniform3iv( location, value );
  13467. break;
  13468. case '1fv':
  13469. _gl.uniform1fv( location, value );
  13470. break;
  13471. case '2fv':
  13472. _gl.uniform2fv( location, value );
  13473. break;
  13474. case '3fv':
  13475. _gl.uniform3fv( location, value );
  13476. break;
  13477. case '4fv':
  13478. _gl.uniform4fv( location, value );
  13479. break;
  13480. case 'Matrix3fv':
  13481. _gl.uniformMatrix3fv( location, false, value );
  13482. break;
  13483. case 'Matrix4fv':
  13484. _gl.uniformMatrix4fv( location, false, value );
  13485. break;
  13486. //
  13487. case 'i':
  13488. // single integer
  13489. _gl.uniform1i( location, value );
  13490. break;
  13491. case 'f':
  13492. // single float
  13493. _gl.uniform1f( location, value );
  13494. break;
  13495. case 'v2':
  13496. // single THREE.Vector2
  13497. _gl.uniform2f( location, value.x, value.y );
  13498. break;
  13499. case 'v3':
  13500. // single THREE.Vector3
  13501. _gl.uniform3f( location, value.x, value.y, value.z );
  13502. break;
  13503. case 'v4':
  13504. // single THREE.Vector4
  13505. _gl.uniform4f( location, value.x, value.y, value.z, value.w );
  13506. break;
  13507. case 'c':
  13508. // single THREE.Color
  13509. _gl.uniform3f( location, value.r, value.g, value.b );
  13510. break;
  13511. case 'iv1':
  13512. // flat array of integers (JS or typed array)
  13513. _gl.uniform1iv( location, value );
  13514. break;
  13515. case 'iv':
  13516. // flat array of integers with 3 x N size (JS or typed array)
  13517. _gl.uniform3iv( location, value );
  13518. break;
  13519. case 'fv1':
  13520. // flat array of floats (JS or typed array)
  13521. _gl.uniform1fv( location, value );
  13522. break;
  13523. case 'fv':
  13524. // flat array of floats with 3 x N size (JS or typed array)
  13525. _gl.uniform3fv( location, value );
  13526. break;
  13527. case 'v2v':
  13528. // array of THREE.Vector2
  13529. if ( uniform._array === undefined ) {
  13530. uniform._array = new Float32Array( 2 * value.length );
  13531. }
  13532. for ( var i = 0, il = value.length; i < il; i ++ ) {
  13533. offset = i * 2;
  13534. uniform._array[ offset ] = value[ i ].x;
  13535. uniform._array[ offset + 1 ] = value[ i ].y;
  13536. }
  13537. _gl.uniform2fv( location, uniform._array );
  13538. break;
  13539. case 'v3v':
  13540. // array of THREE.Vector3
  13541. if ( uniform._array === undefined ) {
  13542. uniform._array = new Float32Array( 3 * value.length );
  13543. }
  13544. for ( var i = 0, il = value.length; i < il; i ++ ) {
  13545. offset = i * 3;
  13546. uniform._array[ offset ] = value[ i ].x;
  13547. uniform._array[ offset + 1 ] = value[ i ].y;
  13548. uniform._array[ offset + 2 ] = value[ i ].z;
  13549. }
  13550. _gl.uniform3fv( location, uniform._array );
  13551. break;
  13552. case 'v4v':
  13553. // array of THREE.Vector4
  13554. if ( uniform._array === undefined ) {
  13555. uniform._array = new Float32Array( 4 * value.length );
  13556. }
  13557. for ( var i = 0, il = value.length; i < il; i ++ ) {
  13558. offset = i * 4;
  13559. uniform._array[ offset ] = value[ i ].x;
  13560. uniform._array[ offset + 1 ] = value[ i ].y;
  13561. uniform._array[ offset + 2 ] = value[ i ].z;
  13562. uniform._array[ offset + 3 ] = value[ i ].w;
  13563. }
  13564. _gl.uniform4fv( location, uniform._array );
  13565. break;
  13566. case 'm3':
  13567. // single THREE.Matrix3
  13568. _gl.uniformMatrix3fv( location, false, value.elements );
  13569. break;
  13570. case 'm3v':
  13571. // array of THREE.Matrix3
  13572. if ( uniform._array === undefined ) {
  13573. uniform._array = new Float32Array( 9 * value.length );
  13574. }
  13575. for ( var i = 0, il = value.length; i < il; i ++ ) {
  13576. value[ i ].flattenToArrayOffset( uniform._array, i * 9 );
  13577. }
  13578. _gl.uniformMatrix3fv( location, false, uniform._array );
  13579. break;
  13580. case 'm4':
  13581. // single THREE.Matrix4
  13582. _gl.uniformMatrix4fv( location, false, value.elements );
  13583. break;
  13584. case 'm4v':
  13585. // array of THREE.Matrix4
  13586. if ( uniform._array === undefined ) {
  13587. uniform._array = new Float32Array( 16 * value.length );
  13588. }
  13589. for ( var i = 0, il = value.length; i < il; i ++ ) {
  13590. value[ i ].flattenToArrayOffset( uniform._array, i * 16 );
  13591. }
  13592. _gl.uniformMatrix4fv( location, false, uniform._array );
  13593. break;
  13594. case 't':
  13595. // single THREE.Texture (2d or cube)
  13596. texture = value;
  13597. textureUnit = getTextureUnit();
  13598. _gl.uniform1i( location, textureUnit );
  13599. if ( ! texture ) continue;
  13600. if ( texture instanceof THREE.CubeTexture ||
  13601. ( texture.image instanceof Array && texture.image.length === 6 ) ) { // CompressedTexture can have Array in image :/
  13602. setCubeTexture( texture, textureUnit );
  13603. } else if ( texture instanceof THREE.WebGLRenderTargetCube ) {
  13604. setCubeTextureDynamic( texture, textureUnit );
  13605. } else {
  13606. _this.setTexture( texture, textureUnit );
  13607. }
  13608. break;
  13609. case 'tv':
  13610. // array of THREE.Texture (2d)
  13611. if ( uniform._array === undefined ) {
  13612. uniform._array = [];
  13613. }
  13614. for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
  13615. uniform._array[ i ] = getTextureUnit();
  13616. }
  13617. _gl.uniform1iv( location, uniform._array );
  13618. for ( var i = 0, il = uniform.value.length; i < il; i ++ ) {
  13619. texture = uniform.value[ i ];
  13620. textureUnit = uniform._array[ i ];
  13621. if ( ! texture ) continue;
  13622. _this.setTexture( texture, textureUnit );
  13623. }
  13624. break;
  13625. default:
  13626. console.warn( 'THREE.WebGLRenderer: Unknown uniform type: ' + type );
  13627. }
  13628. }
  13629. }
  13630. function setupMatrices ( object, camera ) {
  13631. object._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld );
  13632. object._normalMatrix.getNormalMatrix( object._modelViewMatrix );
  13633. }
  13634. //
  13635. function setColorGamma( array, offset, color, intensitySq ) {
  13636. array[ offset ] = color.r * color.r * intensitySq;
  13637. array[ offset + 1 ] = color.g * color.g * intensitySq;
  13638. array[ offset + 2 ] = color.b * color.b * intensitySq;
  13639. }
  13640. function setColorLinear( array, offset, color, intensity ) {
  13641. array[ offset ] = color.r * intensity;
  13642. array[ offset + 1 ] = color.g * intensity;
  13643. array[ offset + 2 ] = color.b * intensity;
  13644. }
  13645. function setupLights ( lights ) {
  13646. var l, ll, light, n,
  13647. r = 0, g = 0, b = 0,
  13648. color, skyColor, groundColor,
  13649. intensity, intensitySq,
  13650. position,
  13651. distance,
  13652. zlights = _lights,
  13653. dirColors = zlights.directional.colors,
  13654. dirPositions = zlights.directional.positions,
  13655. pointColors = zlights.point.colors,
  13656. pointPositions = zlights.point.positions,
  13657. pointDistances = zlights.point.distances,
  13658. spotColors = zlights.spot.colors,
  13659. spotPositions = zlights.spot.positions,
  13660. spotDistances = zlights.spot.distances,
  13661. spotDirections = zlights.spot.directions,
  13662. spotAnglesCos = zlights.spot.anglesCos,
  13663. spotExponents = zlights.spot.exponents,
  13664. hemiSkyColors = zlights.hemi.skyColors,
  13665. hemiGroundColors = zlights.hemi.groundColors,
  13666. hemiPositions = zlights.hemi.positions,
  13667. dirLength = 0,
  13668. pointLength = 0,
  13669. spotLength = 0,
  13670. hemiLength = 0,
  13671. dirCount = 0,
  13672. pointCount = 0,
  13673. spotCount = 0,
  13674. hemiCount = 0,
  13675. dirOffset = 0,
  13676. pointOffset = 0,
  13677. spotOffset = 0,
  13678. hemiOffset = 0;
  13679. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  13680. light = lights[ l ];
  13681. if ( light.onlyShadow ) continue;
  13682. color = light.color;
  13683. intensity = light.intensity;
  13684. distance = light.distance;
  13685. if ( light instanceof THREE.AmbientLight ) {
  13686. if ( ! light.visible ) continue;
  13687. if ( _this.gammaInput ) {
  13688. r += color.r * color.r;
  13689. g += color.g * color.g;
  13690. b += color.b * color.b;
  13691. } else {
  13692. r += color.r;
  13693. g += color.g;
  13694. b += color.b;
  13695. }
  13696. } else if ( light instanceof THREE.DirectionalLight ) {
  13697. dirCount += 1;
  13698. if ( ! light.visible ) continue;
  13699. _direction.setFromMatrixPosition( light.matrixWorld );
  13700. _vector3.setFromMatrixPosition( light.target.matrixWorld );
  13701. _direction.sub( _vector3 );
  13702. _direction.normalize();
  13703. dirOffset = dirLength * 3;
  13704. dirPositions[ dirOffset ] = _direction.x;
  13705. dirPositions[ dirOffset + 1 ] = _direction.y;
  13706. dirPositions[ dirOffset + 2 ] = _direction.z;
  13707. if ( _this.gammaInput ) {
  13708. setColorGamma( dirColors, dirOffset, color, intensity * intensity );
  13709. } else {
  13710. setColorLinear( dirColors, dirOffset, color, intensity );
  13711. }
  13712. dirLength += 1;
  13713. } else if ( light instanceof THREE.PointLight ) {
  13714. pointCount += 1;
  13715. if ( ! light.visible ) continue;
  13716. pointOffset = pointLength * 3;
  13717. if ( _this.gammaInput ) {
  13718. setColorGamma( pointColors, pointOffset, color, intensity * intensity );
  13719. } else {
  13720. setColorLinear( pointColors, pointOffset, color, intensity );
  13721. }
  13722. _vector3.setFromMatrixPosition( light.matrixWorld );
  13723. pointPositions[ pointOffset ] = _vector3.x;
  13724. pointPositions[ pointOffset + 1 ] = _vector3.y;
  13725. pointPositions[ pointOffset + 2 ] = _vector3.z;
  13726. pointDistances[ pointLength ] = distance;
  13727. pointLength += 1;
  13728. } else if ( light instanceof THREE.SpotLight ) {
  13729. spotCount += 1;
  13730. if ( ! light.visible ) continue;
  13731. spotOffset = spotLength * 3;
  13732. if ( _this.gammaInput ) {
  13733. setColorGamma( spotColors, spotOffset, color, intensity * intensity );
  13734. } else {
  13735. setColorLinear( spotColors, spotOffset, color, intensity );
  13736. }
  13737. _direction.setFromMatrixPosition( light.matrixWorld );
  13738. spotPositions[ spotOffset ] = _direction.x;
  13739. spotPositions[ spotOffset + 1 ] = _direction.y;
  13740. spotPositions[ spotOffset + 2 ] = _direction.z;
  13741. spotDistances[ spotLength ] = distance;
  13742. _vector3.setFromMatrixPosition( light.target.matrixWorld );
  13743. _direction.sub( _vector3 );
  13744. _direction.normalize();
  13745. spotDirections[ spotOffset ] = _direction.x;
  13746. spotDirections[ spotOffset + 1 ] = _direction.y;
  13747. spotDirections[ spotOffset + 2 ] = _direction.z;
  13748. spotAnglesCos[ spotLength ] = Math.cos( light.angle );
  13749. spotExponents[ spotLength ] = light.exponent;
  13750. spotLength += 1;
  13751. } else if ( light instanceof THREE.HemisphereLight ) {
  13752. hemiCount += 1;
  13753. if ( ! light.visible ) continue;
  13754. _direction.setFromMatrixPosition( light.matrixWorld );
  13755. _direction.normalize();
  13756. hemiOffset = hemiLength * 3;
  13757. hemiPositions[ hemiOffset ] = _direction.x;
  13758. hemiPositions[ hemiOffset + 1 ] = _direction.y;
  13759. hemiPositions[ hemiOffset + 2 ] = _direction.z;
  13760. skyColor = light.color;
  13761. groundColor = light.groundColor;
  13762. if ( _this.gammaInput ) {
  13763. intensitySq = intensity * intensity;
  13764. setColorGamma( hemiSkyColors, hemiOffset, skyColor, intensitySq );
  13765. setColorGamma( hemiGroundColors, hemiOffset, groundColor, intensitySq );
  13766. } else {
  13767. setColorLinear( hemiSkyColors, hemiOffset, skyColor, intensity );
  13768. setColorLinear( hemiGroundColors, hemiOffset, groundColor, intensity );
  13769. }
  13770. hemiLength += 1;
  13771. }
  13772. }
  13773. // null eventual remains from removed lights
  13774. // (this is to avoid if in shader)
  13775. for ( l = dirLength * 3, ll = Math.max( dirColors.length, dirCount * 3 ); l < ll; l ++ ) dirColors[ l ] = 0.0;
  13776. for ( l = pointLength * 3, ll = Math.max( pointColors.length, pointCount * 3 ); l < ll; l ++ ) pointColors[ l ] = 0.0;
  13777. for ( l = spotLength * 3, ll = Math.max( spotColors.length, spotCount * 3 ); l < ll; l ++ ) spotColors[ l ] = 0.0;
  13778. for ( l = hemiLength * 3, ll = Math.max( hemiSkyColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiSkyColors[ l ] = 0.0;
  13779. for ( l = hemiLength * 3, ll = Math.max( hemiGroundColors.length, hemiCount * 3 ); l < ll; l ++ ) hemiGroundColors[ l ] = 0.0;
  13780. zlights.directional.length = dirLength;
  13781. zlights.point.length = pointLength;
  13782. zlights.spot.length = spotLength;
  13783. zlights.hemi.length = hemiLength;
  13784. zlights.ambient[ 0 ] = r;
  13785. zlights.ambient[ 1 ] = g;
  13786. zlights.ambient[ 2 ] = b;
  13787. }
  13788. // GL state setting
  13789. this.setFaceCulling = function ( cullFace, frontFaceDirection ) {
  13790. if ( cullFace === THREE.CullFaceNone ) {
  13791. _gl.disable( _gl.CULL_FACE );
  13792. } else {
  13793. if ( frontFaceDirection === THREE.FrontFaceDirectionCW ) {
  13794. _gl.frontFace( _gl.CW );
  13795. } else {
  13796. _gl.frontFace( _gl.CCW );
  13797. }
  13798. if ( cullFace === THREE.CullFaceBack ) {
  13799. _gl.cullFace( _gl.BACK );
  13800. } else if ( cullFace === THREE.CullFaceFront ) {
  13801. _gl.cullFace( _gl.FRONT );
  13802. } else {
  13803. _gl.cullFace( _gl.FRONT_AND_BACK );
  13804. }
  13805. _gl.enable( _gl.CULL_FACE );
  13806. }
  13807. };
  13808. this.setMaterialFaces = function ( material ) {
  13809. var doubleSided = material.side === THREE.DoubleSide;
  13810. var flipSided = material.side === THREE.BackSide;
  13811. if ( _oldDoubleSided !== doubleSided ) {
  13812. if ( doubleSided ) {
  13813. _gl.disable( _gl.CULL_FACE );
  13814. } else {
  13815. _gl.enable( _gl.CULL_FACE );
  13816. }
  13817. _oldDoubleSided = doubleSided;
  13818. }
  13819. if ( _oldFlipSided !== flipSided ) {
  13820. if ( flipSided ) {
  13821. _gl.frontFace( _gl.CW );
  13822. } else {
  13823. _gl.frontFace( _gl.CCW );
  13824. }
  13825. _oldFlipSided = flipSided;
  13826. }
  13827. };
  13828. this.setDepthTest = function ( depthTest ) {
  13829. if ( _oldDepthTest !== depthTest ) {
  13830. if ( depthTest ) {
  13831. _gl.enable( _gl.DEPTH_TEST );
  13832. } else {
  13833. _gl.disable( _gl.DEPTH_TEST );
  13834. }
  13835. _oldDepthTest = depthTest;
  13836. }
  13837. };
  13838. this.setDepthWrite = function ( depthWrite ) {
  13839. if ( _oldDepthWrite !== depthWrite ) {
  13840. _gl.depthMask( depthWrite );
  13841. _oldDepthWrite = depthWrite;
  13842. }
  13843. };
  13844. function setLineWidth ( width ) {
  13845. if ( width !== _oldLineWidth ) {
  13846. _gl.lineWidth( width );
  13847. _oldLineWidth = width;
  13848. }
  13849. }
  13850. function setPolygonOffset ( polygonoffset, factor, units ) {
  13851. if ( _oldPolygonOffset !== polygonoffset ) {
  13852. if ( polygonoffset ) {
  13853. _gl.enable( _gl.POLYGON_OFFSET_FILL );
  13854. } else {
  13855. _gl.disable( _gl.POLYGON_OFFSET_FILL );
  13856. }
  13857. _oldPolygonOffset = polygonoffset;
  13858. }
  13859. if ( polygonoffset && ( _oldPolygonOffsetFactor !== factor || _oldPolygonOffsetUnits !== units ) ) {
  13860. _gl.polygonOffset( factor, units );
  13861. _oldPolygonOffsetFactor = factor;
  13862. _oldPolygonOffsetUnits = units;
  13863. }
  13864. }
  13865. this.setBlending = function ( blending, blendEquation, blendSrc, blendDst ) {
  13866. if ( blending !== _oldBlending ) {
  13867. if ( blending === THREE.NoBlending ) {
  13868. _gl.disable( _gl.BLEND );
  13869. } else if ( blending === THREE.AdditiveBlending ) {
  13870. _gl.enable( _gl.BLEND );
  13871. _gl.blendEquation( _gl.FUNC_ADD );
  13872. _gl.blendFunc( _gl.SRC_ALPHA, _gl.ONE );
  13873. } else if ( blending === THREE.SubtractiveBlending ) {
  13874. // TODO: Find blendFuncSeparate() combination
  13875. _gl.enable( _gl.BLEND );
  13876. _gl.blendEquation( _gl.FUNC_ADD );
  13877. _gl.blendFunc( _gl.ZERO, _gl.ONE_MINUS_SRC_COLOR );
  13878. } else if ( blending === THREE.MultiplyBlending ) {
  13879. // TODO: Find blendFuncSeparate() combination
  13880. _gl.enable( _gl.BLEND );
  13881. _gl.blendEquation( _gl.FUNC_ADD );
  13882. _gl.blendFunc( _gl.ZERO, _gl.SRC_COLOR );
  13883. } else if ( blending === THREE.CustomBlending ) {
  13884. _gl.enable( _gl.BLEND );
  13885. } else {
  13886. _gl.enable( _gl.BLEND );
  13887. _gl.blendEquationSeparate( _gl.FUNC_ADD, _gl.FUNC_ADD );
  13888. _gl.blendFuncSeparate( _gl.SRC_ALPHA, _gl.ONE_MINUS_SRC_ALPHA, _gl.ONE, _gl.ONE_MINUS_SRC_ALPHA );
  13889. }
  13890. _oldBlending = blending;
  13891. }
  13892. if ( blending === THREE.CustomBlending ) {
  13893. if ( blendEquation !== _oldBlendEquation ) {
  13894. _gl.blendEquation( paramThreeToGL( blendEquation ) );
  13895. _oldBlendEquation = blendEquation;
  13896. }
  13897. if ( blendSrc !== _oldBlendSrc || blendDst !== _oldBlendDst ) {
  13898. _gl.blendFunc( paramThreeToGL( blendSrc ), paramThreeToGL( blendDst ) );
  13899. _oldBlendSrc = blendSrc;
  13900. _oldBlendDst = blendDst;
  13901. }
  13902. } else {
  13903. _oldBlendEquation = null;
  13904. _oldBlendSrc = null;
  13905. _oldBlendDst = null;
  13906. }
  13907. };
  13908. // Textures
  13909. function setTextureParameters ( textureType, texture, isImagePowerOfTwo ) {
  13910. var extension;
  13911. if ( isImagePowerOfTwo ) {
  13912. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, paramThreeToGL( texture.wrapS ) );
  13913. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, paramThreeToGL( texture.wrapT ) );
  13914. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, paramThreeToGL( texture.magFilter ) );
  13915. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, paramThreeToGL( texture.minFilter ) );
  13916. } else {
  13917. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_S, _gl.CLAMP_TO_EDGE );
  13918. _gl.texParameteri( textureType, _gl.TEXTURE_WRAP_T, _gl.CLAMP_TO_EDGE );
  13919. if ( texture.wrapS !== THREE.ClampToEdgeWrapping || texture.wrapT !== THREE.ClampToEdgeWrapping ) {
  13920. console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT is set to THREE.ClampToEdgeWrapping. ( ' + texture.sourceFile + ' )' );
  13921. }
  13922. _gl.texParameteri( textureType, _gl.TEXTURE_MAG_FILTER, filterFallback( texture.magFilter ) );
  13923. _gl.texParameteri( textureType, _gl.TEXTURE_MIN_FILTER, filterFallback( texture.minFilter ) );
  13924. if ( texture.minFilter !== THREE.NearestFilter && texture.minFilter !== THREE.LinearFilter ) {
  13925. console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter is set to THREE.LinearFilter or THREE.NearestFilter. ( ' + texture.sourceFile + ' )' );
  13926. }
  13927. }
  13928. extension = extensions.get( 'EXT_texture_filter_anisotropic' );
  13929. if ( extension && texture.type !== THREE.FloatType ) {
  13930. if ( texture.anisotropy > 1 || texture.__oldAnisotropy ) {
  13931. _gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, _this.getMaxAnisotropy() ) );
  13932. texture.__oldAnisotropy = texture.anisotropy;
  13933. }
  13934. }
  13935. }
  13936. this.uploadTexture = function ( texture ) {
  13937. if ( texture.__webglInit === undefined ) {
  13938. texture.__webglInit = true;
  13939. texture.addEventListener( 'dispose', onTextureDispose );
  13940. texture.__webglTexture = _gl.createTexture();
  13941. _this.info.memory.textures ++;
  13942. }
  13943. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  13944. _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  13945. _gl.pixelStorei( _gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha );
  13946. _gl.pixelStorei( _gl.UNPACK_ALIGNMENT, texture.unpackAlignment );
  13947. texture.image = clampToMaxSize( texture.image, _maxTextureSize );
  13948. var image = texture.image,
  13949. isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ),
  13950. glFormat = paramThreeToGL( texture.format ),
  13951. glType = paramThreeToGL( texture.type );
  13952. setTextureParameters( _gl.TEXTURE_2D, texture, isImagePowerOfTwo );
  13953. var mipmap, mipmaps = texture.mipmaps;
  13954. if ( texture instanceof THREE.DataTexture ) {
  13955. // use manually created mipmaps if available
  13956. // if there are no manual mipmaps
  13957. // set 0 level mipmap and then use GL to generate other mipmap levels
  13958. if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
  13959. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  13960. mipmap = mipmaps[ i ];
  13961. _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  13962. }
  13963. texture.generateMipmaps = false;
  13964. } else {
  13965. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, image.width, image.height, 0, glFormat, glType, image.data );
  13966. }
  13967. } else if ( texture instanceof THREE.CompressedTexture ) {
  13968. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  13969. mipmap = mipmaps[ i ];
  13970. if ( texture.format !== THREE.RGBAFormat && texture.format !== THREE.RGBFormat ) {
  13971. if ( getCompressedTextureFormats().indexOf( glFormat ) > -1 ) {
  13972. _gl.compressedTexImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  13973. } else {
  13974. console.warn( "Attempt to load unsupported compressed texture format" );
  13975. }
  13976. } else {
  13977. _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  13978. }
  13979. }
  13980. } else { // regular Texture (image, video, canvas)
  13981. // use manually created mipmaps if available
  13982. // if there are no manual mipmaps
  13983. // set 0 level mipmap and then use GL to generate other mipmap levels
  13984. if ( mipmaps.length > 0 && isImagePowerOfTwo ) {
  13985. for ( var i = 0, il = mipmaps.length; i < il; i ++ ) {
  13986. mipmap = mipmaps[ i ];
  13987. _gl.texImage2D( _gl.TEXTURE_2D, i, glFormat, glFormat, glType, mipmap );
  13988. }
  13989. texture.generateMipmaps = false;
  13990. } else {
  13991. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, glFormat, glType, texture.image );
  13992. }
  13993. }
  13994. if ( texture.generateMipmaps && isImagePowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  13995. texture.needsUpdate = false;
  13996. if ( texture.onUpdate ) texture.onUpdate();
  13997. };
  13998. this.setTexture = function ( texture, slot ) {
  13999. _gl.activeTexture( _gl.TEXTURE0 + slot );
  14000. if ( texture.needsUpdate ) {
  14001. _this.uploadTexture( texture );
  14002. } else {
  14003. _gl.bindTexture( _gl.TEXTURE_2D, texture.__webglTexture );
  14004. }
  14005. };
  14006. function clampToMaxSize ( image, maxSize ) {
  14007. if ( image.width > maxSize || image.height > maxSize ) {
  14008. // Warning: Scaling through the canvas will only work with images that use
  14009. // premultiplied alpha.
  14010. var scale = maxSize / Math.max( image.width, image.height );
  14011. var canvas = document.createElement( 'canvas' );
  14012. canvas.width = Math.floor( image.width * scale );
  14013. canvas.height = Math.floor( image.height * scale );
  14014. var context = canvas.getContext( '2d' );
  14015. context.drawImage( image, 0, 0, image.width, image.height, 0, 0, canvas.width, canvas.height );
  14016. console.log( 'THREE.WebGLRenderer:', image, 'is too big (' + image.width + 'x' + image.height + '). Resized to ' + canvas.width + 'x' + canvas.height + '.' );
  14017. return canvas;
  14018. }
  14019. return image;
  14020. }
  14021. function setCubeTexture ( texture, slot ) {
  14022. if ( texture.image.length === 6 ) {
  14023. if ( texture.needsUpdate ) {
  14024. if ( ! texture.image.__webglTextureCube ) {
  14025. texture.addEventListener( 'dispose', onTextureDispose );
  14026. texture.image.__webglTextureCube = _gl.createTexture();
  14027. _this.info.memory.textures ++;
  14028. }
  14029. _gl.activeTexture( _gl.TEXTURE0 + slot );
  14030. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  14031. _gl.pixelStorei( _gl.UNPACK_FLIP_Y_WEBGL, texture.flipY );
  14032. var isCompressed = texture instanceof THREE.CompressedTexture;
  14033. var isDataTexture = texture.image[ 0 ] instanceof THREE.DataTexture;
  14034. var cubeImage = [];
  14035. for ( var i = 0; i < 6; i ++ ) {
  14036. if ( _this.autoScaleCubemaps && ! isCompressed && ! isDataTexture ) {
  14037. cubeImage[ i ] = clampToMaxSize( texture.image[ i ], _maxCubemapSize );
  14038. } else {
  14039. cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ];
  14040. }
  14041. }
  14042. var image = cubeImage[ 0 ],
  14043. isImagePowerOfTwo = THREE.Math.isPowerOfTwo( image.width ) && THREE.Math.isPowerOfTwo( image.height ),
  14044. glFormat = paramThreeToGL( texture.format ),
  14045. glType = paramThreeToGL( texture.type );
  14046. setTextureParameters( _gl.TEXTURE_CUBE_MAP, texture, isImagePowerOfTwo );
  14047. for ( var i = 0; i < 6; i ++ ) {
  14048. if ( ! isCompressed ) {
  14049. if ( isDataTexture ) {
  14050. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data );
  14051. } else {
  14052. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, glFormat, glType, cubeImage[ i ] );
  14053. }
  14054. } else {
  14055. var mipmap, mipmaps = cubeImage[ i ].mipmaps;
  14056. for ( var j = 0, jl = mipmaps.length; j < jl; j ++ ) {
  14057. mipmap = mipmaps[ j ];
  14058. if ( texture.format !== THREE.RGBAFormat && texture.format !== THREE.RGBFormat ) {
  14059. if ( getCompressedTextureFormats().indexOf( glFormat ) > -1 ) {
  14060. _gl.compressedTexImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, mipmap.data );
  14061. } else {
  14062. console.warn( "Attempt to load unsupported compressed texture format" );
  14063. }
  14064. } else {
  14065. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, j, glFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data );
  14066. }
  14067. }
  14068. }
  14069. }
  14070. if ( texture.generateMipmaps && isImagePowerOfTwo ) {
  14071. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  14072. }
  14073. texture.needsUpdate = false;
  14074. if ( texture.onUpdate ) texture.onUpdate();
  14075. } else {
  14076. _gl.activeTexture( _gl.TEXTURE0 + slot );
  14077. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.image.__webglTextureCube );
  14078. }
  14079. }
  14080. }
  14081. function setCubeTextureDynamic ( texture, slot ) {
  14082. _gl.activeTexture( _gl.TEXTURE0 + slot );
  14083. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, texture.__webglTexture );
  14084. }
  14085. // Render targets
  14086. function setupFrameBuffer ( framebuffer, renderTarget, textureTarget ) {
  14087. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  14088. _gl.framebufferTexture2D( _gl.FRAMEBUFFER, _gl.COLOR_ATTACHMENT0, textureTarget, renderTarget.__webglTexture, 0 );
  14089. }
  14090. function setupRenderBuffer ( renderbuffer, renderTarget ) {
  14091. _gl.bindRenderbuffer( _gl.RENDERBUFFER, renderbuffer );
  14092. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  14093. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_COMPONENT16, renderTarget.width, renderTarget.height );
  14094. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  14095. /* For some reason this is not working. Defaulting to RGBA4.
  14096. } else if ( ! renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  14097. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.STENCIL_INDEX8, renderTarget.width, renderTarget.height );
  14098. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  14099. */
  14100. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  14101. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.DEPTH_STENCIL, renderTarget.width, renderTarget.height );
  14102. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderbuffer );
  14103. } else {
  14104. _gl.renderbufferStorage( _gl.RENDERBUFFER, _gl.RGBA4, renderTarget.width, renderTarget.height );
  14105. }
  14106. }
  14107. this.setRenderTarget = function ( renderTarget ) {
  14108. var isCube = ( renderTarget instanceof THREE.WebGLRenderTargetCube );
  14109. if ( renderTarget && renderTarget.__webglFramebuffer === undefined ) {
  14110. if ( renderTarget.depthBuffer === undefined ) renderTarget.depthBuffer = true;
  14111. if ( renderTarget.stencilBuffer === undefined ) renderTarget.stencilBuffer = true;
  14112. renderTarget.addEventListener( 'dispose', onRenderTargetDispose );
  14113. renderTarget.__webglTexture = _gl.createTexture();
  14114. _this.info.memory.textures ++;
  14115. // Setup texture, create render and frame buffers
  14116. var isTargetPowerOfTwo = THREE.Math.isPowerOfTwo( renderTarget.width ) && THREE.Math.isPowerOfTwo( renderTarget.height ),
  14117. glFormat = paramThreeToGL( renderTarget.format ),
  14118. glType = paramThreeToGL( renderTarget.type );
  14119. if ( isCube ) {
  14120. renderTarget.__webglFramebuffer = [];
  14121. renderTarget.__webglRenderbuffer = [];
  14122. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  14123. setTextureParameters( _gl.TEXTURE_CUBE_MAP, renderTarget, isTargetPowerOfTwo );
  14124. for ( var i = 0; i < 6; i ++ ) {
  14125. renderTarget.__webglFramebuffer[ i ] = _gl.createFramebuffer();
  14126. renderTarget.__webglRenderbuffer[ i ] = _gl.createRenderbuffer();
  14127. _gl.texImage2D( _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  14128. setupFrameBuffer( renderTarget.__webglFramebuffer[ i ], renderTarget, _gl.TEXTURE_CUBE_MAP_POSITIVE_X + i );
  14129. setupRenderBuffer( renderTarget.__webglRenderbuffer[ i ], renderTarget );
  14130. }
  14131. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  14132. } else {
  14133. renderTarget.__webglFramebuffer = _gl.createFramebuffer();
  14134. if ( renderTarget.shareDepthFrom ) {
  14135. renderTarget.__webglRenderbuffer = renderTarget.shareDepthFrom.__webglRenderbuffer;
  14136. } else {
  14137. renderTarget.__webglRenderbuffer = _gl.createRenderbuffer();
  14138. }
  14139. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  14140. setTextureParameters( _gl.TEXTURE_2D, renderTarget, isTargetPowerOfTwo );
  14141. _gl.texImage2D( _gl.TEXTURE_2D, 0, glFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null );
  14142. setupFrameBuffer( renderTarget.__webglFramebuffer, renderTarget, _gl.TEXTURE_2D );
  14143. if ( renderTarget.shareDepthFrom ) {
  14144. if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) {
  14145. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
  14146. } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) {
  14147. _gl.framebufferRenderbuffer( _gl.FRAMEBUFFER, _gl.DEPTH_STENCIL_ATTACHMENT, _gl.RENDERBUFFER, renderTarget.__webglRenderbuffer );
  14148. }
  14149. } else {
  14150. setupRenderBuffer( renderTarget.__webglRenderbuffer, renderTarget );
  14151. }
  14152. if ( isTargetPowerOfTwo ) _gl.generateMipmap( _gl.TEXTURE_2D );
  14153. }
  14154. // Release everything
  14155. if ( isCube ) {
  14156. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  14157. } else {
  14158. _gl.bindTexture( _gl.TEXTURE_2D, null );
  14159. }
  14160. _gl.bindRenderbuffer( _gl.RENDERBUFFER, null );
  14161. _gl.bindFramebuffer( _gl.FRAMEBUFFER, null );
  14162. }
  14163. var framebuffer, width, height, vx, vy;
  14164. if ( renderTarget ) {
  14165. if ( isCube ) {
  14166. framebuffer = renderTarget.__webglFramebuffer[ renderTarget.activeCubeFace ];
  14167. } else {
  14168. framebuffer = renderTarget.__webglFramebuffer;
  14169. }
  14170. width = renderTarget.width;
  14171. height = renderTarget.height;
  14172. vx = 0;
  14173. vy = 0;
  14174. } else {
  14175. framebuffer = null;
  14176. width = _viewportWidth;
  14177. height = _viewportHeight;
  14178. vx = _viewportX;
  14179. vy = _viewportY;
  14180. }
  14181. if ( framebuffer !== _currentFramebuffer ) {
  14182. _gl.bindFramebuffer( _gl.FRAMEBUFFER, framebuffer );
  14183. _gl.viewport( vx, vy, width, height );
  14184. _currentFramebuffer = framebuffer;
  14185. }
  14186. _currentWidth = width;
  14187. _currentHeight = height;
  14188. };
  14189. function updateRenderTargetMipmap ( renderTarget ) {
  14190. if ( renderTarget instanceof THREE.WebGLRenderTargetCube ) {
  14191. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, renderTarget.__webglTexture );
  14192. _gl.generateMipmap( _gl.TEXTURE_CUBE_MAP );
  14193. _gl.bindTexture( _gl.TEXTURE_CUBE_MAP, null );
  14194. } else {
  14195. _gl.bindTexture( _gl.TEXTURE_2D, renderTarget.__webglTexture );
  14196. _gl.generateMipmap( _gl.TEXTURE_2D );
  14197. _gl.bindTexture( _gl.TEXTURE_2D, null );
  14198. }
  14199. }
  14200. // Fallback filters for non-power-of-2 textures
  14201. function filterFallback ( f ) {
  14202. if ( f === THREE.NearestFilter || f === THREE.NearestMipMapNearestFilter || f === THREE.NearestMipMapLinearFilter ) {
  14203. return _gl.NEAREST;
  14204. }
  14205. return _gl.LINEAR;
  14206. }
  14207. // Map three.js constants to WebGL constants
  14208. function paramThreeToGL ( p ) {
  14209. var extension;
  14210. if ( p === THREE.RepeatWrapping ) return _gl.REPEAT;
  14211. if ( p === THREE.ClampToEdgeWrapping ) return _gl.CLAMP_TO_EDGE;
  14212. if ( p === THREE.MirroredRepeatWrapping ) return _gl.MIRRORED_REPEAT;
  14213. if ( p === THREE.NearestFilter ) return _gl.NEAREST;
  14214. if ( p === THREE.NearestMipMapNearestFilter ) return _gl.NEAREST_MIPMAP_NEAREST;
  14215. if ( p === THREE.NearestMipMapLinearFilter ) return _gl.NEAREST_MIPMAP_LINEAR;
  14216. if ( p === THREE.LinearFilter ) return _gl.LINEAR;
  14217. if ( p === THREE.LinearMipMapNearestFilter ) return _gl.LINEAR_MIPMAP_NEAREST;
  14218. if ( p === THREE.LinearMipMapLinearFilter ) return _gl.LINEAR_MIPMAP_LINEAR;
  14219. if ( p === THREE.UnsignedByteType ) return _gl.UNSIGNED_BYTE;
  14220. if ( p === THREE.UnsignedShort4444Type ) return _gl.UNSIGNED_SHORT_4_4_4_4;
  14221. if ( p === THREE.UnsignedShort5551Type ) return _gl.UNSIGNED_SHORT_5_5_5_1;
  14222. if ( p === THREE.UnsignedShort565Type ) return _gl.UNSIGNED_SHORT_5_6_5;
  14223. if ( p === THREE.ByteType ) return _gl.BYTE;
  14224. if ( p === THREE.ShortType ) return _gl.SHORT;
  14225. if ( p === THREE.UnsignedShortType ) return _gl.UNSIGNED_SHORT;
  14226. if ( p === THREE.IntType ) return _gl.INT;
  14227. if ( p === THREE.UnsignedIntType ) return _gl.UNSIGNED_INT;
  14228. if ( p === THREE.FloatType ) return _gl.FLOAT;
  14229. if ( p === THREE.AlphaFormat ) return _gl.ALPHA;
  14230. if ( p === THREE.RGBFormat ) return _gl.RGB;
  14231. if ( p === THREE.RGBAFormat ) return _gl.RGBA;
  14232. if ( p === THREE.LuminanceFormat ) return _gl.LUMINANCE;
  14233. if ( p === THREE.LuminanceAlphaFormat ) return _gl.LUMINANCE_ALPHA;
  14234. if ( p === THREE.AddEquation ) return _gl.FUNC_ADD;
  14235. if ( p === THREE.SubtractEquation ) return _gl.FUNC_SUBTRACT;
  14236. if ( p === THREE.ReverseSubtractEquation ) return _gl.FUNC_REVERSE_SUBTRACT;
  14237. if ( p === THREE.ZeroFactor ) return _gl.ZERO;
  14238. if ( p === THREE.OneFactor ) return _gl.ONE;
  14239. if ( p === THREE.SrcColorFactor ) return _gl.SRC_COLOR;
  14240. if ( p === THREE.OneMinusSrcColorFactor ) return _gl.ONE_MINUS_SRC_COLOR;
  14241. if ( p === THREE.SrcAlphaFactor ) return _gl.SRC_ALPHA;
  14242. if ( p === THREE.OneMinusSrcAlphaFactor ) return _gl.ONE_MINUS_SRC_ALPHA;
  14243. if ( p === THREE.DstAlphaFactor ) return _gl.DST_ALPHA;
  14244. if ( p === THREE.OneMinusDstAlphaFactor ) return _gl.ONE_MINUS_DST_ALPHA;
  14245. if ( p === THREE.DstColorFactor ) return _gl.DST_COLOR;
  14246. if ( p === THREE.OneMinusDstColorFactor ) return _gl.ONE_MINUS_DST_COLOR;
  14247. if ( p === THREE.SrcAlphaSaturateFactor ) return _gl.SRC_ALPHA_SATURATE;
  14248. extension = extensions.get( 'WEBGL_compressed_texture_s3tc' );
  14249. if ( extension !== null ) {
  14250. if ( p === THREE.RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT;
  14251. if ( p === THREE.RGBA_S3TC_DXT1_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT;
  14252. if ( p === THREE.RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT;
  14253. if ( p === THREE.RGBA_S3TC_DXT5_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT;
  14254. }
  14255. extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' );
  14256. if ( extension !== null ) {
  14257. if ( p === THREE.RGB_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG;
  14258. if ( p === THREE.RGB_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG;
  14259. if ( p === THREE.RGBA_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG;
  14260. if ( p === THREE.RGBA_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG;
  14261. }
  14262. extension = extensions.get( 'EXT_blend_minmax' );
  14263. if ( extension !== null ) {
  14264. if ( p === THREE.MinEquation ) return extension.MIN_EXT;
  14265. if ( p === THREE.MaxEquation ) return extension.MAX_EXT;
  14266. }
  14267. return 0;
  14268. }
  14269. // Allocations
  14270. function allocateBones ( object ) {
  14271. if ( _supportsBoneTextures && object && object.skeleton && object.skeleton.useVertexTexture ) {
  14272. return 1024;
  14273. } else {
  14274. // default for when object is not specified
  14275. // ( for example when prebuilding shader
  14276. // to be used with multiple objects )
  14277. //
  14278. // - leave some extra space for other uniforms
  14279. // - limit here is ANGLE's 254 max uniform vectors
  14280. // (up to 54 should be safe)
  14281. var nVertexUniforms = _gl.getParameter( _gl.MAX_VERTEX_UNIFORM_VECTORS );
  14282. var nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 );
  14283. var maxBones = nVertexMatrices;
  14284. if ( object !== undefined && object instanceof THREE.SkinnedMesh ) {
  14285. maxBones = Math.min( object.skeleton.bones.length, maxBones );
  14286. if ( maxBones < object.skeleton.bones.length ) {
  14287. console.warn( 'WebGLRenderer: too many bones - ' + object.skeleton.bones.length + ', this GPU supports just ' + maxBones + ' (try OpenGL instead of ANGLE)' );
  14288. }
  14289. }
  14290. return maxBones;
  14291. }
  14292. }
  14293. function allocateLights( lights ) {
  14294. var dirLights = 0;
  14295. var pointLights = 0;
  14296. var spotLights = 0;
  14297. var hemiLights = 0;
  14298. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  14299. var light = lights[ l ];
  14300. if ( light.onlyShadow || light.visible === false ) continue;
  14301. if ( light instanceof THREE.DirectionalLight ) dirLights ++;
  14302. if ( light instanceof THREE.PointLight ) pointLights ++;
  14303. if ( light instanceof THREE.SpotLight ) spotLights ++;
  14304. if ( light instanceof THREE.HemisphereLight ) hemiLights ++;
  14305. }
  14306. return { 'directional': dirLights, 'point': pointLights, 'spot': spotLights, 'hemi': hemiLights };
  14307. }
  14308. function allocateShadows( lights ) {
  14309. var maxShadows = 0;
  14310. for ( var l = 0, ll = lights.length; l < ll; l ++ ) {
  14311. var light = lights[ l ];
  14312. if ( ! light.castShadow ) continue;
  14313. if ( light instanceof THREE.SpotLight ) maxShadows ++;
  14314. if ( light instanceof THREE.DirectionalLight && ! light.shadowCascade ) maxShadows ++;
  14315. }
  14316. return maxShadows;
  14317. }
  14318. // DEPRECATED
  14319. this.initMaterial = function () {
  14320. console.warn( 'THREE.WebGLRenderer: .initMaterial() has been removed.' );
  14321. };
  14322. this.addPrePlugin = function () {
  14323. console.warn( 'THREE.WebGLRenderer: .addPrePlugin() has been removed.' );
  14324. };
  14325. this.addPostPlugin = function () {
  14326. console.warn( 'THREE.WebGLRenderer: .addPostPlugin() has been removed.' );
  14327. };
  14328. this.updateShadowMap = function () {
  14329. console.warn( 'THREE.WebGLRenderer: .updateShadowMap() has been removed.' );
  14330. };
  14331. };
  14332. // File:src/renderers/WebGLRenderTarget.js
  14333. /**
  14334. * @author szimek / https://github.com/szimek/
  14335. * @author alteredq / http://alteredqualia.com/
  14336. */
  14337. THREE.WebGLRenderTarget = function ( width, height, options ) {
  14338. this.width = width;
  14339. this.height = height;
  14340. options = options || {};
  14341. this.wrapS = options.wrapS !== undefined ? options.wrapS : THREE.ClampToEdgeWrapping;
  14342. this.wrapT = options.wrapT !== undefined ? options.wrapT : THREE.ClampToEdgeWrapping;
  14343. this.magFilter = options.magFilter !== undefined ? options.magFilter : THREE.LinearFilter;
  14344. this.minFilter = options.minFilter !== undefined ? options.minFilter : THREE.LinearMipMapLinearFilter;
  14345. this.anisotropy = options.anisotropy !== undefined ? options.anisotropy : 1;
  14346. this.offset = new THREE.Vector2( 0, 0 );
  14347. this.repeat = new THREE.Vector2( 1, 1 );
  14348. this.format = options.format !== undefined ? options.format : THREE.RGBAFormat;
  14349. this.type = options.type !== undefined ? options.type : THREE.UnsignedByteType;
  14350. this.depthBuffer = options.depthBuffer !== undefined ? options.depthBuffer : true;
  14351. this.stencilBuffer = options.stencilBuffer !== undefined ? options.stencilBuffer : true;
  14352. this.generateMipmaps = true;
  14353. this.shareDepthFrom = null;
  14354. };
  14355. THREE.WebGLRenderTarget.prototype = {
  14356. constructor: THREE.WebGLRenderTarget,
  14357. setSize: function ( width, height ) {
  14358. this.width = width;
  14359. this.height = height;
  14360. },
  14361. clone: function () {
  14362. var tmp = new THREE.WebGLRenderTarget( this.width, this.height );
  14363. tmp.wrapS = this.wrapS;
  14364. tmp.wrapT = this.wrapT;
  14365. tmp.magFilter = this.magFilter;
  14366. tmp.minFilter = this.minFilter;
  14367. tmp.anisotropy = this.anisotropy;
  14368. tmp.offset.copy( this.offset );
  14369. tmp.repeat.copy( this.repeat );
  14370. tmp.format = this.format;
  14371. tmp.type = this.type;
  14372. tmp.depthBuffer = this.depthBuffer;
  14373. tmp.stencilBuffer = this.stencilBuffer;
  14374. tmp.generateMipmaps = this.generateMipmaps;
  14375. tmp.shareDepthFrom = this.shareDepthFrom;
  14376. return tmp;
  14377. },
  14378. dispose: function () {
  14379. this.dispatchEvent( { type: 'dispose' } );
  14380. }
  14381. };
  14382. THREE.EventDispatcher.prototype.apply( THREE.WebGLRenderTarget.prototype );
  14383. // File:src/renderers/WebGLRenderTargetCube.js
  14384. /**
  14385. * @author alteredq / http://alteredqualia.com
  14386. */
  14387. THREE.WebGLRenderTargetCube = function ( width, height, options ) {
  14388. THREE.WebGLRenderTarget.call( this, width, height, options );
  14389. this.activeCubeFace = 0; // PX 0, NX 1, PY 2, NY 3, PZ 4, NZ 5
  14390. };
  14391. THREE.WebGLRenderTargetCube.prototype = Object.create( THREE.WebGLRenderTarget.prototype );
  14392. THREE.WebGLRenderTargetCube.prototype.constructor = THREE.WebGLRenderTargetCube;
  14393. // File:src/renderers/webgl/WebGLExtensions.js
  14394. /**
  14395. * @author mrdoob / http://mrdoob.com/
  14396. */
  14397. THREE.WebGLExtensions = function ( gl ) {
  14398. var extensions = {};
  14399. this.get = function ( name ) {
  14400. if ( extensions[ name ] !== undefined ) {
  14401. return extensions[ name ];
  14402. }
  14403. var extension;
  14404. switch ( name ) {
  14405. case 'EXT_texture_filter_anisotropic':
  14406. extension = gl.getExtension( 'EXT_texture_filter_anisotropic' ) || gl.getExtension( 'MOZ_EXT_texture_filter_anisotropic' ) || gl.getExtension( 'WEBKIT_EXT_texture_filter_anisotropic' );
  14407. break;
  14408. case 'WEBGL_compressed_texture_s3tc':
  14409. extension = gl.getExtension( 'WEBGL_compressed_texture_s3tc' ) || gl.getExtension( 'MOZ_WEBGL_compressed_texture_s3tc' ) || gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_s3tc' );
  14410. break;
  14411. case 'WEBGL_compressed_texture_pvrtc':
  14412. extension = gl.getExtension( 'WEBGL_compressed_texture_pvrtc' ) || gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_pvrtc' );
  14413. break;
  14414. default:
  14415. extension = gl.getExtension( name );
  14416. }
  14417. if ( extension === null ) {
  14418. console.log( 'THREE.WebGLRenderer: ' + name + ' extension not supported.' );
  14419. }
  14420. extensions[ name ] = extension;
  14421. return extension;
  14422. };
  14423. };
  14424. // File:src/renderers/webgl/WebGLProgram.js
  14425. THREE.WebGLProgram = ( function () {
  14426. var programIdCount = 0;
  14427. var generateDefines = function ( defines ) {
  14428. var value, chunk, chunks = [];
  14429. for ( var d in defines ) {
  14430. value = defines[ d ];
  14431. if ( value === false ) continue;
  14432. chunk = "#define " + d + " " + value;
  14433. chunks.push( chunk );
  14434. }
  14435. return chunks.join( "\n" );
  14436. };
  14437. var cacheUniformLocations = function ( gl, program, identifiers ) {
  14438. var uniforms = {};
  14439. for ( var i = 0, l = identifiers.length; i < l; i ++ ) {
  14440. var id = identifiers[ i ];
  14441. uniforms[ id ] = gl.getUniformLocation( program, id );
  14442. }
  14443. return uniforms;
  14444. };
  14445. var cacheAttributeLocations = function ( gl, program, identifiers ) {
  14446. var attributes = {};
  14447. for ( var i = 0, l = identifiers.length; i < l; i ++ ) {
  14448. var id = identifiers[ i ];
  14449. attributes[ id ] = gl.getAttribLocation( program, id );
  14450. }
  14451. return attributes;
  14452. };
  14453. return function ( renderer, code, material, parameters ) {
  14454. var _this = renderer;
  14455. var _gl = _this.context;
  14456. var defines = material.defines;
  14457. var uniforms = material.__webglShader.uniforms;
  14458. var attributes = material.attributes;
  14459. var vertexShader = material.__webglShader.vertexShader;
  14460. var fragmentShader = material.__webglShader.fragmentShader;
  14461. var index0AttributeName = material.index0AttributeName;
  14462. if ( index0AttributeName === undefined && parameters.morphTargets === true ) {
  14463. // programs with morphTargets displace position out of attribute 0
  14464. index0AttributeName = 'position';
  14465. }
  14466. var shadowMapTypeDefine = "SHADOWMAP_TYPE_BASIC";
  14467. if ( parameters.shadowMapType === THREE.PCFShadowMap ) {
  14468. shadowMapTypeDefine = "SHADOWMAP_TYPE_PCF";
  14469. } else if ( parameters.shadowMapType === THREE.PCFSoftShadowMap ) {
  14470. shadowMapTypeDefine = "SHADOWMAP_TYPE_PCF_SOFT";
  14471. }
  14472. var envMapTypeDefine = null;
  14473. if ( parameters.envMap ) {
  14474. switch ( material.envMap.mapping ) {
  14475. case THREE.CubeReflectionMapping:
  14476. case THREE.CubeRefractionMapping:
  14477. envMapTypeDefine = "ENVMAP_TYPE_CUBE";
  14478. break;
  14479. case THREE.EquirectangularReflectionMapping:
  14480. case THREE.EquirectangularRefractionMapping:
  14481. envMapTypeDefine = "ENVMAP_TYPE_EQUIREC";
  14482. break;
  14483. case THREE.SphericalReflectionMapping:
  14484. envMapTypeDefine = "ENVMAP_TYPE_SPHERE";
  14485. break;
  14486. }
  14487. }
  14488. // console.log( "building new program " );
  14489. //
  14490. var customDefines = generateDefines( defines );
  14491. //
  14492. var program = _gl.createProgram();
  14493. var prefix_vertex, prefix_fragment;
  14494. if ( material instanceof THREE.RawShaderMaterial ) {
  14495. prefix_vertex = '';
  14496. prefix_fragment = '';
  14497. } else {
  14498. prefix_vertex = [
  14499. "precision " + parameters.precision + " float;",
  14500. "precision " + parameters.precision + " int;",
  14501. customDefines,
  14502. parameters.supportsVertexTextures ? "#define VERTEX_TEXTURES" : "",
  14503. _this.gammaInput ? "#define GAMMA_INPUT" : "",
  14504. _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
  14505. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  14506. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  14507. "#define MAX_SPOT_LIGHTS " + parameters.maxSpotLights,
  14508. "#define MAX_HEMI_LIGHTS " + parameters.maxHemiLights,
  14509. "#define MAX_SHADOWS " + parameters.maxShadows,
  14510. "#define MAX_BONES " + parameters.maxBones,
  14511. parameters.map ? "#define USE_MAP" : "",
  14512. parameters.envMap ? "#define USE_ENVMAP" : "",
  14513. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  14514. parameters.bumpMap ? "#define USE_BUMPMAP" : "",
  14515. parameters.normalMap ? "#define USE_NORMALMAP" : "",
  14516. parameters.specularMap ? "#define USE_SPECULARMAP" : "",
  14517. parameters.alphaMap ? "#define USE_ALPHAMAP" : "",
  14518. parameters.vertexColors ? "#define USE_COLOR" : "",
  14519. parameters.skinning ? "#define USE_SKINNING" : "",
  14520. parameters.useVertexTexture ? "#define BONE_TEXTURE" : "",
  14521. parameters.morphTargets ? "#define USE_MORPHTARGETS" : "",
  14522. parameters.morphNormals ? "#define USE_MORPHNORMALS" : "",
  14523. parameters.wrapAround ? "#define WRAP_AROUND" : "",
  14524. parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
  14525. parameters.flipSided ? "#define FLIP_SIDED" : "",
  14526. parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
  14527. parameters.shadowMapEnabled ? "#define " + shadowMapTypeDefine : "",
  14528. parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
  14529. parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
  14530. parameters.sizeAttenuation ? "#define USE_SIZEATTENUATION" : "",
  14531. parameters.logarithmicDepthBuffer ? "#define USE_LOGDEPTHBUF" : "",
  14532. //_this._glExtensionFragDepth ? "#define USE_LOGDEPTHBUF_EXT" : "",
  14533. "uniform mat4 modelMatrix;",
  14534. "uniform mat4 modelViewMatrix;",
  14535. "uniform mat4 projectionMatrix;",
  14536. "uniform mat4 viewMatrix;",
  14537. "uniform mat3 normalMatrix;",
  14538. "uniform vec3 cameraPosition;",
  14539. "attribute vec3 position;",
  14540. "attribute vec3 normal;",
  14541. "attribute vec2 uv;",
  14542. "attribute vec2 uv2;",
  14543. "#ifdef USE_COLOR",
  14544. " attribute vec3 color;",
  14545. "#endif",
  14546. "#ifdef USE_MORPHTARGETS",
  14547. " attribute vec3 morphTarget0;",
  14548. " attribute vec3 morphTarget1;",
  14549. " attribute vec3 morphTarget2;",
  14550. " attribute vec3 morphTarget3;",
  14551. " #ifdef USE_MORPHNORMALS",
  14552. " attribute vec3 morphNormal0;",
  14553. " attribute vec3 morphNormal1;",
  14554. " attribute vec3 morphNormal2;",
  14555. " attribute vec3 morphNormal3;",
  14556. " #else",
  14557. " attribute vec3 morphTarget4;",
  14558. " attribute vec3 morphTarget5;",
  14559. " attribute vec3 morphTarget6;",
  14560. " attribute vec3 morphTarget7;",
  14561. " #endif",
  14562. "#endif",
  14563. "#ifdef USE_SKINNING",
  14564. " attribute vec4 skinIndex;",
  14565. " attribute vec4 skinWeight;",
  14566. "#endif",
  14567. ""
  14568. ].join( '\n' );
  14569. prefix_fragment = [
  14570. "precision " + parameters.precision + " float;",
  14571. "precision " + parameters.precision + " int;",
  14572. ( parameters.bumpMap || parameters.normalMap ) ? "#extension GL_OES_standard_derivatives : enable" : "",
  14573. customDefines,
  14574. "#define MAX_DIR_LIGHTS " + parameters.maxDirLights,
  14575. "#define MAX_POINT_LIGHTS " + parameters.maxPointLights,
  14576. "#define MAX_SPOT_LIGHTS " + parameters.maxSpotLights,
  14577. "#define MAX_HEMI_LIGHTS " + parameters.maxHemiLights,
  14578. "#define MAX_SHADOWS " + parameters.maxShadows,
  14579. parameters.alphaTest ? "#define ALPHATEST " + parameters.alphaTest: "",
  14580. _this.gammaInput ? "#define GAMMA_INPUT" : "",
  14581. _this.gammaOutput ? "#define GAMMA_OUTPUT" : "",
  14582. ( parameters.useFog && parameters.fog ) ? "#define USE_FOG" : "",
  14583. ( parameters.useFog && parameters.fogExp ) ? "#define FOG_EXP2" : "",
  14584. parameters.map ? "#define USE_MAP" : "",
  14585. parameters.envMap ? "#define USE_ENVMAP" : "",
  14586. envMapTypeDefine ? "#define " + envMapTypeDefine : "",
  14587. parameters.lightMap ? "#define USE_LIGHTMAP" : "",
  14588. parameters.bumpMap ? "#define USE_BUMPMAP" : "",
  14589. parameters.normalMap ? "#define USE_NORMALMAP" : "",
  14590. parameters.specularMap ? "#define USE_SPECULARMAP" : "",
  14591. parameters.alphaMap ? "#define USE_ALPHAMAP" : "",
  14592. parameters.vertexColors ? "#define USE_COLOR" : "",
  14593. parameters.metal ? "#define METAL" : "",
  14594. parameters.wrapAround ? "#define WRAP_AROUND" : "",
  14595. parameters.doubleSided ? "#define DOUBLE_SIDED" : "",
  14596. parameters.flipSided ? "#define FLIP_SIDED" : "",
  14597. parameters.shadowMapEnabled ? "#define USE_SHADOWMAP" : "",
  14598. parameters.shadowMapEnabled ? "#define " + shadowMapTypeDefine : "",
  14599. parameters.shadowMapDebug ? "#define SHADOWMAP_DEBUG" : "",
  14600. parameters.shadowMapCascade ? "#define SHADOWMAP_CASCADE" : "",
  14601. parameters.logarithmicDepthBuffer ? "#define USE_LOGDEPTHBUF" : "",
  14602. //_this._glExtensionFragDepth ? "#define USE_LOGDEPTHBUF_EXT" : "",
  14603. "uniform mat4 viewMatrix;",
  14604. "uniform vec3 cameraPosition;",
  14605. ""
  14606. ].join( '\n' );
  14607. }
  14608. var glVertexShader = new THREE.WebGLShader( _gl, _gl.VERTEX_SHADER, prefix_vertex + vertexShader );
  14609. var glFragmentShader = new THREE.WebGLShader( _gl, _gl.FRAGMENT_SHADER, prefix_fragment + fragmentShader );
  14610. _gl.attachShader( program, glVertexShader );
  14611. _gl.attachShader( program, glFragmentShader );
  14612. if ( index0AttributeName !== undefined ) {
  14613. // Force a particular attribute to index 0.
  14614. // because potentially expensive emulation is done by browser if attribute 0 is disabled.
  14615. // And, color, for example is often automatically bound to index 0 so disabling it
  14616. _gl.bindAttribLocation( program, 0, index0AttributeName );
  14617. }
  14618. _gl.linkProgram( program );
  14619. if ( _gl.getProgramParameter( program, _gl.LINK_STATUS ) === false ) {
  14620. console.error( 'THREE.WebGLProgram: Could not initialise shader.' );
  14621. console.error( 'gl.VALIDATE_STATUS', _gl.getProgramParameter( program, _gl.VALIDATE_STATUS ) );
  14622. console.error( 'gl.getError()', _gl.getError() );
  14623. }
  14624. if ( _gl.getProgramInfoLog( program ) !== '' ) {
  14625. console.warn( 'THREE.WebGLProgram: gl.getProgramInfoLog()', _gl.getProgramInfoLog( program ) );
  14626. }
  14627. // clean up
  14628. _gl.deleteShader( glVertexShader );
  14629. _gl.deleteShader( glFragmentShader );
  14630. // cache uniform locations
  14631. var identifiers = [
  14632. 'viewMatrix', 'modelViewMatrix', 'projectionMatrix', 'normalMatrix', 'modelMatrix', 'cameraPosition', 'morphTargetInfluences', 'bindMatrix', 'bindMatrixInverse'
  14633. ];
  14634. if ( parameters.useVertexTexture ) {
  14635. identifiers.push( 'boneTexture' );
  14636. identifiers.push( 'boneTextureWidth' );
  14637. identifiers.push( 'boneTextureHeight' );
  14638. } else {
  14639. identifiers.push( 'boneGlobalMatrices' );
  14640. }
  14641. if ( parameters.logarithmicDepthBuffer ) {
  14642. identifiers.push('logDepthBufFC');
  14643. }
  14644. for ( var u in uniforms ) {
  14645. identifiers.push( u );
  14646. }
  14647. this.uniforms = cacheUniformLocations( _gl, program, identifiers );
  14648. // cache attributes locations
  14649. identifiers = [
  14650. "position", "normal", "uv", "uv2", "tangent", "color",
  14651. "skinIndex", "skinWeight", "lineDistance"
  14652. ];
  14653. for ( var i = 0; i < parameters.maxMorphTargets; i ++ ) {
  14654. identifiers.push( "morphTarget" + i );
  14655. }
  14656. for ( var i = 0; i < parameters.maxMorphNormals; i ++ ) {
  14657. identifiers.push( "morphNormal" + i );
  14658. }
  14659. for ( var a in attributes ) {
  14660. identifiers.push( a );
  14661. }
  14662. this.attributes = cacheAttributeLocations( _gl, program, identifiers );
  14663. this.attributesKeys = Object.keys( this.attributes );
  14664. //
  14665. this.id = programIdCount ++;
  14666. this.code = code;
  14667. this.usedTimes = 1;
  14668. this.program = program;
  14669. this.vertexShader = glVertexShader;
  14670. this.fragmentShader = glFragmentShader;
  14671. return this;
  14672. };
  14673. } )();
  14674. // File:src/renderers/webgl/WebGLShader.js
  14675. THREE.WebGLShader = ( function () {
  14676. var addLineNumbers = function ( string ) {
  14677. var lines = string.split( '\n' );
  14678. for ( var i = 0; i < lines.length; i ++ ) {
  14679. lines[ i ] = ( i + 1 ) + ': ' + lines[ i ];
  14680. }
  14681. return lines.join( '\n' );
  14682. };
  14683. return function ( gl, type, string ) {
  14684. var shader = gl.createShader( type );
  14685. gl.shaderSource( shader, string );
  14686. gl.compileShader( shader );
  14687. if ( gl.getShaderParameter( shader, gl.COMPILE_STATUS ) === false ) {
  14688. console.error( 'THREE.WebGLShader: Shader couldn\'t compile.' );
  14689. }
  14690. if ( gl.getShaderInfoLog( shader ) !== '' ) {
  14691. console.warn( 'THREE.WebGLShader: gl.getShaderInfoLog()', gl.getShaderInfoLog( shader ) );
  14692. console.warn( addLineNumbers( string ) );
  14693. }
  14694. // --enable-privileged-webgl-extension
  14695. // console.log( type, gl.getExtension( 'WEBGL_debug_shaders' ).getTranslatedShaderSource( shader ) );
  14696. return shader;
  14697. };
  14698. } )();
  14699. // File:src/renderers/webgl/plugins/LensFlarePlugin.js
  14700. /**
  14701. * @author mikael emtinger / http://gomo.se/
  14702. * @author alteredq / http://alteredqualia.com/
  14703. */
  14704. THREE.LensFlarePlugin = function ( renderer, flares ) {
  14705. var gl = renderer.context;
  14706. var vertexBuffer, elementBuffer;
  14707. var program, attributes, uniforms;
  14708. var hasVertexTexture;
  14709. var tempTexture, occlusionTexture;
  14710. var init = function () {
  14711. var vertices = new Float32Array( [
  14712. -1, -1, 0, 0,
  14713. 1, -1, 1, 0,
  14714. 1, 1, 1, 1,
  14715. -1, 1, 0, 1
  14716. ] );
  14717. var faces = new Uint16Array( [
  14718. 0, 1, 2,
  14719. 0, 2, 3
  14720. ] );
  14721. // buffers
  14722. vertexBuffer = gl.createBuffer();
  14723. elementBuffer = gl.createBuffer();
  14724. gl.bindBuffer( gl.ARRAY_BUFFER, vertexBuffer );
  14725. gl.bufferData( gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW );
  14726. gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, elementBuffer );
  14727. gl.bufferData( gl.ELEMENT_ARRAY_BUFFER, faces, gl.STATIC_DRAW );
  14728. // textures
  14729. tempTexture = gl.createTexture();
  14730. occlusionTexture = gl.createTexture();
  14731. gl.bindTexture( gl.TEXTURE_2D, tempTexture );
  14732. gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGB, 16, 16, 0, gl.RGB, gl.UNSIGNED_BYTE, null );
  14733. gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE );
  14734. gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE );
  14735. gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  14736. gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  14737. gl.bindTexture( gl.TEXTURE_2D, occlusionTexture );
  14738. gl.texImage2D( gl.TEXTURE_2D, 0, gl.RGBA, 16, 16, 0, gl.RGBA, gl.UNSIGNED_BYTE, null );
  14739. gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE );
  14740. gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE );
  14741. gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST );
  14742. gl.texParameteri( gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST );
  14743. hasVertexTexture = gl.getParameter( gl.MAX_VERTEX_TEXTURE_IMAGE_UNITS ) > 0;
  14744. var shader;
  14745. if ( hasVertexTexture ) {
  14746. shader = {
  14747. vertexShader: [
  14748. "uniform lowp int renderType;",
  14749. "uniform vec3 screenPosition;",
  14750. "uniform vec2 scale;",
  14751. "uniform float rotation;",
  14752. "uniform sampler2D occlusionMap;",
  14753. "attribute vec2 position;",
  14754. "attribute vec2 uv;",
  14755. "varying vec2 vUV;",
  14756. "varying float vVisibility;",
  14757. "void main() {",
  14758. "vUV = uv;",
  14759. "vec2 pos = position;",
  14760. "if( renderType == 2 ) {",
  14761. "vec4 visibility = texture2D( occlusionMap, vec2( 0.1, 0.1 ) );",
  14762. "visibility += texture2D( occlusionMap, vec2( 0.5, 0.1 ) );",
  14763. "visibility += texture2D( occlusionMap, vec2( 0.9, 0.1 ) );",
  14764. "visibility += texture2D( occlusionMap, vec2( 0.9, 0.5 ) );",
  14765. "visibility += texture2D( occlusionMap, vec2( 0.9, 0.9 ) );",
  14766. "visibility += texture2D( occlusionMap, vec2( 0.5, 0.9 ) );",
  14767. "visibility += texture2D( occlusionMap, vec2( 0.1, 0.9 ) );",
  14768. "visibility += texture2D( occlusionMap, vec2( 0.1, 0.5 ) );",
  14769. "visibility += texture2D( occlusionMap, vec2( 0.5, 0.5 ) );",
  14770. "vVisibility = visibility.r / 9.0;",
  14771. "vVisibility *= 1.0 - visibility.g / 9.0;",
  14772. "vVisibility *= visibility.b / 9.0;",
  14773. "vVisibility *= 1.0 - visibility.a / 9.0;",
  14774. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  14775. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  14776. "}",
  14777. "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  14778. "}"
  14779. ].join( "\n" ),
  14780. fragmentShader: [
  14781. "uniform lowp int renderType;",
  14782. "uniform sampler2D map;",
  14783. "uniform float opacity;",
  14784. "uniform vec3 color;",
  14785. "varying vec2 vUV;",
  14786. "varying float vVisibility;",
  14787. "void main() {",
  14788. // pink square
  14789. "if( renderType == 0 ) {",
  14790. "gl_FragColor = vec4( 1.0, 0.0, 1.0, 0.0 );",
  14791. // restore
  14792. "} else if( renderType == 1 ) {",
  14793. "gl_FragColor = texture2D( map, vUV );",
  14794. // flare
  14795. "} else {",
  14796. "vec4 texture = texture2D( map, vUV );",
  14797. "texture.a *= opacity * vVisibility;",
  14798. "gl_FragColor = texture;",
  14799. "gl_FragColor.rgb *= color;",
  14800. "}",
  14801. "}"
  14802. ].join( "\n" )
  14803. };
  14804. } else {
  14805. shader = {
  14806. vertexShader: [
  14807. "uniform lowp int renderType;",
  14808. "uniform vec3 screenPosition;",
  14809. "uniform vec2 scale;",
  14810. "uniform float rotation;",
  14811. "attribute vec2 position;",
  14812. "attribute vec2 uv;",
  14813. "varying vec2 vUV;",
  14814. "void main() {",
  14815. "vUV = uv;",
  14816. "vec2 pos = position;",
  14817. "if( renderType == 2 ) {",
  14818. "pos.x = cos( rotation ) * position.x - sin( rotation ) * position.y;",
  14819. "pos.y = sin( rotation ) * position.x + cos( rotation ) * position.y;",
  14820. "}",
  14821. "gl_Position = vec4( ( pos * scale + screenPosition.xy ).xy, screenPosition.z, 1.0 );",
  14822. "}"
  14823. ].join( "\n" ),
  14824. fragmentShader: [
  14825. "precision mediump float;",
  14826. "uniform lowp int renderType;",
  14827. "uniform sampler2D map;",
  14828. "uniform sampler2D occlusionMap;",
  14829. "uniform float opacity;",
  14830. "uniform vec3 color;",
  14831. "varying vec2 vUV;",
  14832. "void main() {",
  14833. // pink square
  14834. "if( renderType == 0 ) {",
  14835. "gl_FragColor = vec4( texture2D( map, vUV ).rgb, 0.0 );",
  14836. // restore
  14837. "} else if( renderType == 1 ) {",
  14838. "gl_FragColor = texture2D( map, vUV );",
  14839. // flare
  14840. "} else {",
  14841. "float visibility = texture2D( occlusionMap, vec2( 0.5, 0.1 ) ).a;",
  14842. "visibility += texture2D( occlusionMap, vec2( 0.9, 0.5 ) ).a;",
  14843. "visibility += texture2D( occlusionMap, vec2( 0.5, 0.9 ) ).a;",
  14844. "visibility += texture2D( occlusionMap, vec2( 0.1, 0.5 ) ).a;",
  14845. "visibility = ( 1.0 - visibility / 4.0 );",
  14846. "vec4 texture = texture2D( map, vUV );",
  14847. "texture.a *= opacity * visibility;",
  14848. "gl_FragColor = texture;",
  14849. "gl_FragColor.rgb *= color;",
  14850. "}",
  14851. "}"
  14852. ].join( "\n" )
  14853. };
  14854. }
  14855. program = createProgram( shader );
  14856. attributes = {
  14857. vertex: gl.getAttribLocation ( program, "position" ),
  14858. uv: gl.getAttribLocation ( program, "uv" )
  14859. }
  14860. uniforms = {
  14861. renderType: gl.getUniformLocation( program, "renderType" ),
  14862. map: gl.getUniformLocation( program, "map" ),
  14863. occlusionMap: gl.getUniformLocation( program, "occlusionMap" ),
  14864. opacity: gl.getUniformLocation( program, "opacity" ),
  14865. color: gl.getUniformLocation( program, "color" ),
  14866. scale: gl.getUniformLocation( program, "scale" ),
  14867. rotation: gl.getUniformLocation( program, "rotation" ),
  14868. screenPosition: gl.getUniformLocation( program, "screenPosition" )
  14869. };
  14870. };
  14871. /*
  14872. * Render lens flares
  14873. * Method: renders 16x16 0xff00ff-colored points scattered over the light source area,
  14874. * reads these back and calculates occlusion.
  14875. */
  14876. this.render = function ( scene, camera, viewportWidth, viewportHeight ) {
  14877. if ( flares.length === 0 ) return;
  14878. var tempPosition = new THREE.Vector3();
  14879. var invAspect = viewportHeight / viewportWidth,
  14880. halfViewportWidth = viewportWidth * 0.5,
  14881. halfViewportHeight = viewportHeight * 0.5;
  14882. var size = 16 / viewportHeight,
  14883. scale = new THREE.Vector2( size * invAspect, size );
  14884. var screenPosition = new THREE.Vector3( 1, 1, 0 ),
  14885. screenPositionPixels = new THREE.Vector2( 1, 1 );
  14886. if ( program === undefined ) {
  14887. init();
  14888. }
  14889. gl.useProgram( program );
  14890. gl.enableVertexAttribArray( attributes.vertex );
  14891. gl.enableVertexAttribArray( attributes.uv );
  14892. // loop through all lens flares to update their occlusion and positions
  14893. // setup gl and common used attribs/unforms
  14894. gl.uniform1i( uniforms.occlusionMap, 0 );
  14895. gl.uniform1i( uniforms.map, 1 );
  14896. gl.bindBuffer( gl.ARRAY_BUFFER, vertexBuffer );
  14897. gl.vertexAttribPointer( attributes.vertex, 2, gl.FLOAT, false, 2 * 8, 0 );
  14898. gl.vertexAttribPointer( attributes.uv, 2, gl.FLOAT, false, 2 * 8, 8 );
  14899. gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, elementBuffer );
  14900. gl.disable( gl.CULL_FACE );
  14901. gl.depthMask( false );
  14902. for ( var i = 0, l = flares.length; i < l; i ++ ) {
  14903. size = 16 / viewportHeight;
  14904. scale.set( size * invAspect, size );
  14905. // calc object screen position
  14906. var flare = flares[ i ];
  14907. tempPosition.set( flare.matrixWorld.elements[12], flare.matrixWorld.elements[13], flare.matrixWorld.elements[14] );
  14908. tempPosition.applyMatrix4( camera.matrixWorldInverse );
  14909. tempPosition.applyProjection( camera.projectionMatrix );
  14910. // setup arrays for gl programs
  14911. screenPosition.copy( tempPosition )
  14912. screenPositionPixels.x = screenPosition.x * halfViewportWidth + halfViewportWidth;
  14913. screenPositionPixels.y = screenPosition.y * halfViewportHeight + halfViewportHeight;
  14914. // screen cull
  14915. if ( hasVertexTexture || (
  14916. screenPositionPixels.x > 0 &&
  14917. screenPositionPixels.x < viewportWidth &&
  14918. screenPositionPixels.y > 0 &&
  14919. screenPositionPixels.y < viewportHeight ) ) {
  14920. // save current RGB to temp texture
  14921. gl.activeTexture( gl.TEXTURE1 );
  14922. gl.bindTexture( gl.TEXTURE_2D, tempTexture );
  14923. gl.copyTexImage2D( gl.TEXTURE_2D, 0, gl.RGB, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
  14924. // render pink quad
  14925. gl.uniform1i( uniforms.renderType, 0 );
  14926. gl.uniform2f( uniforms.scale, scale.x, scale.y );
  14927. gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z );
  14928. gl.disable( gl.BLEND );
  14929. gl.enable( gl.DEPTH_TEST );
  14930. gl.drawElements( gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0 );
  14931. // copy result to occlusionMap
  14932. gl.activeTexture( gl.TEXTURE0 );
  14933. gl.bindTexture( gl.TEXTURE_2D, occlusionTexture );
  14934. gl.copyTexImage2D( gl.TEXTURE_2D, 0, gl.RGBA, screenPositionPixels.x - 8, screenPositionPixels.y - 8, 16, 16, 0 );
  14935. // restore graphics
  14936. gl.uniform1i( uniforms.renderType, 1 );
  14937. gl.disable( gl.DEPTH_TEST );
  14938. gl.activeTexture( gl.TEXTURE1 );
  14939. gl.bindTexture( gl.TEXTURE_2D, tempTexture );
  14940. gl.drawElements( gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0 );
  14941. // update object positions
  14942. flare.positionScreen.copy( screenPosition )
  14943. if ( flare.customUpdateCallback ) {
  14944. flare.customUpdateCallback( flare );
  14945. } else {
  14946. flare.updateLensFlares();
  14947. }
  14948. // render flares
  14949. gl.uniform1i( uniforms.renderType, 2 );
  14950. gl.enable( gl.BLEND );
  14951. for ( var j = 0, jl = flare.lensFlares.length; j < jl; j ++ ) {
  14952. var sprite = flare.lensFlares[ j ];
  14953. if ( sprite.opacity > 0.001 && sprite.scale > 0.001 ) {
  14954. screenPosition.x = sprite.x;
  14955. screenPosition.y = sprite.y;
  14956. screenPosition.z = sprite.z;
  14957. size = sprite.size * sprite.scale / viewportHeight;
  14958. scale.x = size * invAspect;
  14959. scale.y = size;
  14960. gl.uniform3f( uniforms.screenPosition, screenPosition.x, screenPosition.y, screenPosition.z );
  14961. gl.uniform2f( uniforms.scale, scale.x, scale.y );
  14962. gl.uniform1f( uniforms.rotation, sprite.rotation );
  14963. gl.uniform1f( uniforms.opacity, sprite.opacity );
  14964. gl.uniform3f( uniforms.color, sprite.color.r, sprite.color.g, sprite.color.b );
  14965. renderer.setBlending( sprite.blending, sprite.blendEquation, sprite.blendSrc, sprite.blendDst );
  14966. renderer.setTexture( sprite.texture, 1 );
  14967. gl.drawElements( gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0 );
  14968. }
  14969. }
  14970. }
  14971. }
  14972. // restore gl
  14973. gl.enable( gl.CULL_FACE );
  14974. gl.enable( gl.DEPTH_TEST );
  14975. gl.depthMask( true );
  14976. renderer.resetGLState();
  14977. };
  14978. function createProgram ( shader ) {
  14979. var program = gl.createProgram();
  14980. var fragmentShader = gl.createShader( gl.FRAGMENT_SHADER );
  14981. var vertexShader = gl.createShader( gl.VERTEX_SHADER );
  14982. var prefix = "precision " + renderer.getPrecision() + " float;\n";
  14983. gl.shaderSource( fragmentShader, prefix + shader.fragmentShader );
  14984. gl.shaderSource( vertexShader, prefix + shader.vertexShader );
  14985. gl.compileShader( fragmentShader );
  14986. gl.compileShader( vertexShader );
  14987. gl.attachShader( program, fragmentShader );
  14988. gl.attachShader( program, vertexShader );
  14989. gl.linkProgram( program );
  14990. return program;
  14991. }
  14992. };
  14993. // File:src/renderers/webgl/plugins/ShadowMapPlugin.js
  14994. /**
  14995. * @author alteredq / http://alteredqualia.com/
  14996. */
  14997. THREE.ShadowMapPlugin = function ( _renderer, _lights, _webglObjects, _webglObjectsImmediate ) {
  14998. var _gl = _renderer.context;
  14999. var _depthMaterial, _depthMaterialMorph, _depthMaterialSkin, _depthMaterialMorphSkin,
  15000. _frustum = new THREE.Frustum(),
  15001. _projScreenMatrix = new THREE.Matrix4(),
  15002. _min = new THREE.Vector3(),
  15003. _max = new THREE.Vector3(),
  15004. _matrixPosition = new THREE.Vector3(),
  15005. _renderList = [];
  15006. // init
  15007. var depthShader = THREE.ShaderLib[ "depthRGBA" ];
  15008. var depthUniforms = THREE.UniformsUtils.clone( depthShader.uniforms );
  15009. _depthMaterial = new THREE.ShaderMaterial( {
  15010. uniforms: depthUniforms,
  15011. vertexShader: depthShader.vertexShader,
  15012. fragmentShader: depthShader.fragmentShader
  15013. } );
  15014. _depthMaterialMorph = new THREE.ShaderMaterial( {
  15015. uniforms: depthUniforms,
  15016. vertexShader: depthShader.vertexShader,
  15017. fragmentShader: depthShader.fragmentShader,
  15018. morphTargets: true
  15019. } );
  15020. _depthMaterialSkin = new THREE.ShaderMaterial( {
  15021. uniforms: depthUniforms,
  15022. vertexShader: depthShader.vertexShader,
  15023. fragmentShader: depthShader.fragmentShader,
  15024. skinning: true
  15025. } );
  15026. _depthMaterialMorphSkin = new THREE.ShaderMaterial( {
  15027. uniforms: depthUniforms,
  15028. vertexShader: depthShader.vertexShader,
  15029. fragmentShader: depthShader.fragmentShader,
  15030. morphTargets: true,
  15031. skinning: true
  15032. } );
  15033. _depthMaterial._shadowPass = true;
  15034. _depthMaterialMorph._shadowPass = true;
  15035. _depthMaterialSkin._shadowPass = true;
  15036. _depthMaterialMorphSkin._shadowPass = true;
  15037. this.render = function ( scene, camera ) {
  15038. if ( _renderer.shadowMapEnabled === false ) return;
  15039. var i, il, j, jl, n,
  15040. shadowMap, shadowMatrix, shadowCamera,
  15041. program, buffer, material,
  15042. webglObject, object, light,
  15043. lights = [],
  15044. k = 0,
  15045. fog = null;
  15046. // set GL state for depth map
  15047. _gl.clearColor( 1, 1, 1, 1 );
  15048. _gl.disable( _gl.BLEND );
  15049. _gl.enable( _gl.CULL_FACE );
  15050. _gl.frontFace( _gl.CCW );
  15051. if ( _renderer.shadowMapCullFace === THREE.CullFaceFront ) {
  15052. _gl.cullFace( _gl.FRONT );
  15053. } else {
  15054. _gl.cullFace( _gl.BACK );
  15055. }
  15056. _renderer.setDepthTest( true );
  15057. // preprocess lights
  15058. // - skip lights that are not casting shadows
  15059. // - create virtual lights for cascaded shadow maps
  15060. for ( i = 0, il = _lights.length; i < il; i ++ ) {
  15061. light = _lights[ i ];
  15062. if ( ! light.castShadow ) continue;
  15063. if ( ( light instanceof THREE.DirectionalLight ) && light.shadowCascade ) {
  15064. for ( n = 0; n < light.shadowCascadeCount; n ++ ) {
  15065. var virtualLight;
  15066. if ( ! light.shadowCascadeArray[ n ] ) {
  15067. virtualLight = createVirtualLight( light, n );
  15068. virtualLight.originalCamera = camera;
  15069. var gyro = new THREE.Gyroscope();
  15070. gyro.position.copy( light.shadowCascadeOffset );
  15071. gyro.add( virtualLight );
  15072. gyro.add( virtualLight.target );
  15073. camera.add( gyro );
  15074. light.shadowCascadeArray[ n ] = virtualLight;
  15075. console.log( "Created virtualLight", virtualLight );
  15076. } else {
  15077. virtualLight = light.shadowCascadeArray[ n ];
  15078. }
  15079. updateVirtualLight( light, n );
  15080. lights[ k ] = virtualLight;
  15081. k ++;
  15082. }
  15083. } else {
  15084. lights[ k ] = light;
  15085. k ++;
  15086. }
  15087. }
  15088. // render depth map
  15089. for ( i = 0, il = lights.length; i < il; i ++ ) {
  15090. light = lights[ i ];
  15091. if ( ! light.shadowMap ) {
  15092. var shadowFilter = THREE.LinearFilter;
  15093. if ( _renderer.shadowMapType === THREE.PCFSoftShadowMap ) {
  15094. shadowFilter = THREE.NearestFilter;
  15095. }
  15096. var pars = { minFilter: shadowFilter, magFilter: shadowFilter, format: THREE.RGBAFormat };
  15097. light.shadowMap = new THREE.WebGLRenderTarget( light.shadowMapWidth, light.shadowMapHeight, pars );
  15098. light.shadowMapSize = new THREE.Vector2( light.shadowMapWidth, light.shadowMapHeight );
  15099. light.shadowMatrix = new THREE.Matrix4();
  15100. }
  15101. if ( ! light.shadowCamera ) {
  15102. if ( light instanceof THREE.SpotLight ) {
  15103. light.shadowCamera = new THREE.PerspectiveCamera( light.shadowCameraFov, light.shadowMapWidth / light.shadowMapHeight, light.shadowCameraNear, light.shadowCameraFar );
  15104. } else if ( light instanceof THREE.DirectionalLight ) {
  15105. light.shadowCamera = new THREE.OrthographicCamera( light.shadowCameraLeft, light.shadowCameraRight, light.shadowCameraTop, light.shadowCameraBottom, light.shadowCameraNear, light.shadowCameraFar );
  15106. } else {
  15107. console.error( "Unsupported light type for shadow" );
  15108. continue;
  15109. }
  15110. scene.add( light.shadowCamera );
  15111. if ( scene.autoUpdate === true ) scene.updateMatrixWorld();
  15112. }
  15113. if ( light.shadowCameraVisible && ! light.cameraHelper ) {
  15114. light.cameraHelper = new THREE.CameraHelper( light.shadowCamera );
  15115. scene.add( light.cameraHelper );
  15116. }
  15117. if ( light.isVirtual && virtualLight.originalCamera == camera ) {
  15118. updateShadowCamera( camera, light );
  15119. }
  15120. shadowMap = light.shadowMap;
  15121. shadowMatrix = light.shadowMatrix;
  15122. shadowCamera = light.shadowCamera;
  15123. //
  15124. shadowCamera.position.setFromMatrixPosition( light.matrixWorld );
  15125. _matrixPosition.setFromMatrixPosition( light.target.matrixWorld );
  15126. shadowCamera.lookAt( _matrixPosition );
  15127. shadowCamera.updateMatrixWorld();
  15128. shadowCamera.matrixWorldInverse.getInverse( shadowCamera.matrixWorld );
  15129. //
  15130. if ( light.cameraHelper ) light.cameraHelper.visible = light.shadowCameraVisible;
  15131. if ( light.shadowCameraVisible ) light.cameraHelper.update();
  15132. // compute shadow matrix
  15133. shadowMatrix.set(
  15134. 0.5, 0.0, 0.0, 0.5,
  15135. 0.0, 0.5, 0.0, 0.5,
  15136. 0.0, 0.0, 0.5, 0.5,
  15137. 0.0, 0.0, 0.0, 1.0
  15138. );
  15139. shadowMatrix.multiply( shadowCamera.projectionMatrix );
  15140. shadowMatrix.multiply( shadowCamera.matrixWorldInverse );
  15141. // update camera matrices and frustum
  15142. _projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse );
  15143. _frustum.setFromMatrix( _projScreenMatrix );
  15144. // render shadow map
  15145. _renderer.setRenderTarget( shadowMap );
  15146. _renderer.clear();
  15147. // set object matrices & frustum culling
  15148. _renderList.length = 0;
  15149. projectObject( scene, scene, shadowCamera );
  15150. // render regular objects
  15151. var objectMaterial, useMorphing, useSkinning;
  15152. for ( j = 0, jl = _renderList.length; j < jl; j ++ ) {
  15153. webglObject = _renderList[ j ];
  15154. object = webglObject.object;
  15155. buffer = webglObject.buffer;
  15156. // culling is overriden globally for all objects
  15157. // while rendering depth map
  15158. // need to deal with MeshFaceMaterial somehow
  15159. // in that case just use the first of material.materials for now
  15160. // (proper solution would require to break objects by materials
  15161. // similarly to regular rendering and then set corresponding
  15162. // depth materials per each chunk instead of just once per object)
  15163. objectMaterial = getObjectMaterial( object );
  15164. useMorphing = object.geometry.morphTargets !== undefined && object.geometry.morphTargets.length > 0 && objectMaterial.morphTargets;
  15165. useSkinning = object instanceof THREE.SkinnedMesh && objectMaterial.skinning;
  15166. if ( object.customDepthMaterial ) {
  15167. material = object.customDepthMaterial;
  15168. } else if ( useSkinning ) {
  15169. material = useMorphing ? _depthMaterialMorphSkin : _depthMaterialSkin;
  15170. } else if ( useMorphing ) {
  15171. material = _depthMaterialMorph;
  15172. } else {
  15173. material = _depthMaterial;
  15174. }
  15175. _renderer.setMaterialFaces( objectMaterial );
  15176. if ( buffer instanceof THREE.BufferGeometry ) {
  15177. _renderer.renderBufferDirect( shadowCamera, _lights, fog, material, buffer, object );
  15178. } else {
  15179. _renderer.renderBuffer( shadowCamera, _lights, fog, material, buffer, object );
  15180. }
  15181. }
  15182. // set matrices and render immediate objects
  15183. for ( j = 0, jl = _webglObjectsImmediate.length; j < jl; j ++ ) {
  15184. webglObject = _webglObjectsImmediate[ j ];
  15185. object = webglObject.object;
  15186. if ( object.visible && object.castShadow ) {
  15187. object._modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
  15188. _renderer.renderImmediateObject( shadowCamera, _lights, fog, _depthMaterial, object );
  15189. }
  15190. }
  15191. }
  15192. // restore GL state
  15193. var clearColor = _renderer.getClearColor(),
  15194. clearAlpha = _renderer.getClearAlpha();
  15195. _gl.clearColor( clearColor.r, clearColor.g, clearColor.b, clearAlpha );
  15196. _gl.enable( _gl.BLEND );
  15197. if ( _renderer.shadowMapCullFace === THREE.CullFaceFront ) {
  15198. _gl.cullFace( _gl.BACK );
  15199. }
  15200. _renderer.resetGLState();
  15201. };
  15202. function projectObject( scene, object, shadowCamera ){
  15203. if ( object.visible ) {
  15204. var webglObjects = _webglObjects[ object.id ];
  15205. if ( webglObjects && object.castShadow && (object.frustumCulled === false || _frustum.intersectsObject( object ) === true) ) {
  15206. for ( var i = 0, l = webglObjects.length; i < l; i ++ ) {
  15207. var webglObject = webglObjects[ i ];
  15208. object._modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld );
  15209. _renderList.push( webglObject );
  15210. }
  15211. }
  15212. for ( var i = 0, l = object.children.length; i < l; i ++ ) {
  15213. projectObject( scene, object.children[ i ], shadowCamera );
  15214. }
  15215. }
  15216. }
  15217. function createVirtualLight( light, cascade ) {
  15218. var virtualLight = new THREE.DirectionalLight();
  15219. virtualLight.isVirtual = true;
  15220. virtualLight.onlyShadow = true;
  15221. virtualLight.castShadow = true;
  15222. virtualLight.shadowCameraNear = light.shadowCameraNear;
  15223. virtualLight.shadowCameraFar = light.shadowCameraFar;
  15224. virtualLight.shadowCameraLeft = light.shadowCameraLeft;
  15225. virtualLight.shadowCameraRight = light.shadowCameraRight;
  15226. virtualLight.shadowCameraBottom = light.shadowCameraBottom;
  15227. virtualLight.shadowCameraTop = light.shadowCameraTop;
  15228. virtualLight.shadowCameraVisible = light.shadowCameraVisible;
  15229. virtualLight.shadowDarkness = light.shadowDarkness;
  15230. virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
  15231. virtualLight.shadowMapWidth = light.shadowCascadeWidth[ cascade ];
  15232. virtualLight.shadowMapHeight = light.shadowCascadeHeight[ cascade ];
  15233. virtualLight.pointsWorld = [];
  15234. virtualLight.pointsFrustum = [];
  15235. var pointsWorld = virtualLight.pointsWorld,
  15236. pointsFrustum = virtualLight.pointsFrustum;
  15237. for ( var i = 0; i < 8; i ++ ) {
  15238. pointsWorld[ i ] = new THREE.Vector3();
  15239. pointsFrustum[ i ] = new THREE.Vector3();
  15240. }
  15241. var nearZ = light.shadowCascadeNearZ[ cascade ];
  15242. var farZ = light.shadowCascadeFarZ[ cascade ];
  15243. pointsFrustum[ 0 ].set( - 1, - 1, nearZ );
  15244. pointsFrustum[ 1 ].set( 1, - 1, nearZ );
  15245. pointsFrustum[ 2 ].set( - 1, 1, nearZ );
  15246. pointsFrustum[ 3 ].set( 1, 1, nearZ );
  15247. pointsFrustum[ 4 ].set( - 1, - 1, farZ );
  15248. pointsFrustum[ 5 ].set( 1, - 1, farZ );
  15249. pointsFrustum[ 6 ].set( - 1, 1, farZ );
  15250. pointsFrustum[ 7 ].set( 1, 1, farZ );
  15251. return virtualLight;
  15252. }
  15253. // Synchronize virtual light with the original light
  15254. function updateVirtualLight( light, cascade ) {
  15255. var virtualLight = light.shadowCascadeArray[ cascade ];
  15256. virtualLight.position.copy( light.position );
  15257. virtualLight.target.position.copy( light.target.position );
  15258. virtualLight.lookAt( virtualLight.target );
  15259. virtualLight.shadowCameraVisible = light.shadowCameraVisible;
  15260. virtualLight.shadowDarkness = light.shadowDarkness;
  15261. virtualLight.shadowBias = light.shadowCascadeBias[ cascade ];
  15262. var nearZ = light.shadowCascadeNearZ[ cascade ];
  15263. var farZ = light.shadowCascadeFarZ[ cascade ];
  15264. var pointsFrustum = virtualLight.pointsFrustum;
  15265. pointsFrustum[ 0 ].z = nearZ;
  15266. pointsFrustum[ 1 ].z = nearZ;
  15267. pointsFrustum[ 2 ].z = nearZ;
  15268. pointsFrustum[ 3 ].z = nearZ;
  15269. pointsFrustum[ 4 ].z = farZ;
  15270. pointsFrustum[ 5 ].z = farZ;
  15271. pointsFrustum[ 6 ].z = farZ;
  15272. pointsFrustum[ 7 ].z = farZ;
  15273. }
  15274. // Fit shadow camera's ortho frustum to camera frustum
  15275. function updateShadowCamera( camera, light ) {
  15276. var shadowCamera = light.shadowCamera,
  15277. pointsFrustum = light.pointsFrustum,
  15278. pointsWorld = light.pointsWorld;
  15279. _min.set( Infinity, Infinity, Infinity );
  15280. _max.set( - Infinity, - Infinity, - Infinity );
  15281. for ( var i = 0; i < 8; i ++ ) {
  15282. var p = pointsWorld[ i ];
  15283. p.copy( pointsFrustum[ i ] );
  15284. p.unproject( camera );
  15285. p.applyMatrix4( shadowCamera.matrixWorldInverse );
  15286. if ( p.x < _min.x ) _min.x = p.x;
  15287. if ( p.x > _max.x ) _max.x = p.x;
  15288. if ( p.y < _min.y ) _min.y = p.y;
  15289. if ( p.y > _max.y ) _max.y = p.y;
  15290. if ( p.z < _min.z ) _min.z = p.z;
  15291. if ( p.z > _max.z ) _max.z = p.z;
  15292. }
  15293. shadowCamera.left = _min.x;
  15294. shadowCamera.right = _max.x;
  15295. shadowCamera.top = _max.y;
  15296. shadowCamera.bottom = _min.y;
  15297. // can't really fit near/far
  15298. //shadowCamera.near = _min.z;
  15299. //shadowCamera.far = _max.z;
  15300. shadowCamera.updateProjectionMatrix();
  15301. }
  15302. // For the moment just ignore objects that have multiple materials with different animation methods
  15303. // Only the first material will be taken into account for deciding which depth material to use for shadow maps
  15304. function getObjectMaterial( object ) {
  15305. return object.material instanceof THREE.MeshFaceMaterial
  15306. ? object.material.materials[ 0 ]
  15307. : object.material;
  15308. };
  15309. };
  15310. // File:src/renderers/webgl/plugins/SpritePlugin.js
  15311. /**
  15312. * @author mikael emtinger / http://gomo.se/
  15313. * @author alteredq / http://alteredqualia.com/
  15314. */
  15315. THREE.SpritePlugin = function ( renderer, sprites ) {
  15316. var gl = renderer.context;
  15317. var vertexBuffer, elementBuffer;
  15318. var program, attributes, uniforms;
  15319. var texture;
  15320. // decompose matrixWorld
  15321. var spritePosition = new THREE.Vector3();
  15322. var spriteRotation = new THREE.Quaternion();
  15323. var spriteScale = new THREE.Vector3();
  15324. var init = function () {
  15325. var vertices = new Float32Array( [
  15326. - 0.5, - 0.5, 0, 0,
  15327. 0.5, - 0.5, 1, 0,
  15328. 0.5, 0.5, 1, 1,
  15329. - 0.5, 0.5, 0, 1
  15330. ] );
  15331. var faces = new Uint16Array( [
  15332. 0, 1, 2,
  15333. 0, 2, 3
  15334. ] );
  15335. vertexBuffer = gl.createBuffer();
  15336. elementBuffer = gl.createBuffer();
  15337. gl.bindBuffer( gl.ARRAY_BUFFER, vertexBuffer );
  15338. gl.bufferData( gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW );
  15339. gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, elementBuffer );
  15340. gl.bufferData( gl.ELEMENT_ARRAY_BUFFER, faces, gl.STATIC_DRAW );
  15341. program = createProgram();
  15342. attributes = {
  15343. position: gl.getAttribLocation ( program, 'position' ),
  15344. uv: gl.getAttribLocation ( program, 'uv' )
  15345. };
  15346. uniforms = {
  15347. uvOffset: gl.getUniformLocation( program, 'uvOffset' ),
  15348. uvScale: gl.getUniformLocation( program, 'uvScale' ),
  15349. rotation: gl.getUniformLocation( program, 'rotation' ),
  15350. scale: gl.getUniformLocation( program, 'scale' ),
  15351. color: gl.getUniformLocation( program, 'color' ),
  15352. map: gl.getUniformLocation( program, 'map' ),
  15353. opacity: gl.getUniformLocation( program, 'opacity' ),
  15354. modelViewMatrix: gl.getUniformLocation( program, 'modelViewMatrix' ),
  15355. projectionMatrix: gl.getUniformLocation( program, 'projectionMatrix' ),
  15356. fogType: gl.getUniformLocation( program, 'fogType' ),
  15357. fogDensity: gl.getUniformLocation( program, 'fogDensity' ),
  15358. fogNear: gl.getUniformLocation( program, 'fogNear' ),
  15359. fogFar: gl.getUniformLocation( program, 'fogFar' ),
  15360. fogColor: gl.getUniformLocation( program, 'fogColor' ),
  15361. alphaTest: gl.getUniformLocation( program, 'alphaTest' )
  15362. };
  15363. var canvas = document.createElement( 'canvas' );
  15364. canvas.width = 8;
  15365. canvas.height = 8;
  15366. var context = canvas.getContext( '2d' );
  15367. context.fillStyle = 'white';
  15368. context.fillRect( 0, 0, 8, 8 );
  15369. texture = new THREE.Texture( canvas );
  15370. texture.needsUpdate = true;
  15371. };
  15372. this.render = function ( scene, camera ) {
  15373. if ( sprites.length === 0 ) return;
  15374. // setup gl
  15375. if ( program === undefined ) {
  15376. init();
  15377. }
  15378. gl.useProgram( program );
  15379. gl.enableVertexAttribArray( attributes.position );
  15380. gl.enableVertexAttribArray( attributes.uv );
  15381. gl.disable( gl.CULL_FACE );
  15382. gl.enable( gl.BLEND );
  15383. gl.bindBuffer( gl.ARRAY_BUFFER, vertexBuffer );
  15384. gl.vertexAttribPointer( attributes.position, 2, gl.FLOAT, false, 2 * 8, 0 );
  15385. gl.vertexAttribPointer( attributes.uv, 2, gl.FLOAT, false, 2 * 8, 8 );
  15386. gl.bindBuffer( gl.ELEMENT_ARRAY_BUFFER, elementBuffer );
  15387. gl.uniformMatrix4fv( uniforms.projectionMatrix, false, camera.projectionMatrix.elements );
  15388. gl.activeTexture( gl.TEXTURE0 );
  15389. gl.uniform1i( uniforms.map, 0 );
  15390. var oldFogType = 0;
  15391. var sceneFogType = 0;
  15392. var fog = scene.fog;
  15393. if ( fog ) {
  15394. gl.uniform3f( uniforms.fogColor, fog.color.r, fog.color.g, fog.color.b );
  15395. if ( fog instanceof THREE.Fog ) {
  15396. gl.uniform1f( uniforms.fogNear, fog.near );
  15397. gl.uniform1f( uniforms.fogFar, fog.far );
  15398. gl.uniform1i( uniforms.fogType, 1 );
  15399. oldFogType = 1;
  15400. sceneFogType = 1;
  15401. } else if ( fog instanceof THREE.FogExp2 ) {
  15402. gl.uniform1f( uniforms.fogDensity, fog.density );
  15403. gl.uniform1i( uniforms.fogType, 2 );
  15404. oldFogType = 2;
  15405. sceneFogType = 2;
  15406. }
  15407. } else {
  15408. gl.uniform1i( uniforms.fogType, 0 );
  15409. oldFogType = 0;
  15410. sceneFogType = 0;
  15411. }
  15412. // update positions and sort
  15413. for ( var i = 0, l = sprites.length; i < l; i ++ ) {
  15414. var sprite = sprites[ i ];
  15415. sprite._modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, sprite.matrixWorld );
  15416. if ( sprite.renderDepth === null ) {
  15417. sprite.z = - sprite._modelViewMatrix.elements[ 14 ];
  15418. } else {
  15419. sprite.z = sprite.renderDepth;
  15420. }
  15421. }
  15422. sprites.sort( painterSortStable );
  15423. // render all sprites
  15424. var scale = [];
  15425. for ( var i = 0, l = sprites.length; i < l; i ++ ) {
  15426. var sprite = sprites[ i ];
  15427. var material = sprite.material;
  15428. gl.uniform1f( uniforms.alphaTest, material.alphaTest );
  15429. gl.uniformMatrix4fv( uniforms.modelViewMatrix, false, sprite._modelViewMatrix.elements );
  15430. sprite.matrixWorld.decompose( spritePosition, spriteRotation, spriteScale );
  15431. scale[ 0 ] = spriteScale.x;
  15432. scale[ 1 ] = spriteScale.y;
  15433. var fogType = 0;
  15434. if ( scene.fog && material.fog ) {
  15435. fogType = sceneFogType;
  15436. }
  15437. if ( oldFogType !== fogType ) {
  15438. gl.uniform1i( uniforms.fogType, fogType );
  15439. oldFogType = fogType;
  15440. }
  15441. if ( material.map !== null ) {
  15442. gl.uniform2f( uniforms.uvOffset, material.map.offset.x, material.map.offset.y );
  15443. gl.uniform2f( uniforms.uvScale, material.map.repeat.x, material.map.repeat.y );
  15444. } else {
  15445. gl.uniform2f( uniforms.uvOffset, 0, 0 );
  15446. gl.uniform2f( uniforms.uvScale, 1, 1 );
  15447. }
  15448. gl.uniform1f( uniforms.opacity, material.opacity );
  15449. gl.uniform3f( uniforms.color, material.color.r, material.color.g, material.color.b );
  15450. gl.uniform1f( uniforms.rotation, material.rotation );
  15451. gl.uniform2fv( uniforms.scale, scale );
  15452. renderer.setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst );
  15453. renderer.setDepthTest( material.depthTest );
  15454. renderer.setDepthWrite( material.depthWrite );
  15455. if ( material.map && material.map.image && material.map.image.width ) {
  15456. renderer.setTexture( material.map, 0 );
  15457. } else {
  15458. renderer.setTexture( texture, 0 );
  15459. }
  15460. gl.drawElements( gl.TRIANGLES, 6, gl.UNSIGNED_SHORT, 0 );
  15461. }
  15462. // restore gl
  15463. gl.enable( gl.CULL_FACE );
  15464. renderer.resetGLState();
  15465. };
  15466. function createProgram () {
  15467. var program = gl.createProgram();
  15468. var vertexShader = gl.createShader( gl.VERTEX_SHADER );
  15469. var fragmentShader = gl.createShader( gl.FRAGMENT_SHADER );
  15470. gl.shaderSource( vertexShader, [
  15471. 'precision ' + renderer.getPrecision() + ' float;',
  15472. 'uniform mat4 modelViewMatrix;',
  15473. 'uniform mat4 projectionMatrix;',
  15474. 'uniform float rotation;',
  15475. 'uniform vec2 scale;',
  15476. 'uniform vec2 uvOffset;',
  15477. 'uniform vec2 uvScale;',
  15478. 'attribute vec2 position;',
  15479. 'attribute vec2 uv;',
  15480. 'varying vec2 vUV;',
  15481. 'void main() {',
  15482. 'vUV = uvOffset + uv * uvScale;',
  15483. 'vec2 alignedPosition = position * scale;',
  15484. 'vec2 rotatedPosition;',
  15485. 'rotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;',
  15486. 'rotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;',
  15487. 'vec4 finalPosition;',
  15488. 'finalPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );',
  15489. 'finalPosition.xy += rotatedPosition;',
  15490. 'finalPosition = projectionMatrix * finalPosition;',
  15491. 'gl_Position = finalPosition;',
  15492. '}'
  15493. ].join( '\n' ) );
  15494. gl.shaderSource( fragmentShader, [
  15495. 'precision ' + renderer.getPrecision() + ' float;',
  15496. 'uniform vec3 color;',
  15497. 'uniform sampler2D map;',
  15498. 'uniform float opacity;',
  15499. 'uniform int fogType;',
  15500. 'uniform vec3 fogColor;',
  15501. 'uniform float fogDensity;',
  15502. 'uniform float fogNear;',
  15503. 'uniform float fogFar;',
  15504. 'uniform float alphaTest;',
  15505. 'varying vec2 vUV;',
  15506. 'void main() {',
  15507. 'vec4 texture = texture2D( map, vUV );',
  15508. 'if ( texture.a < alphaTest ) discard;',
  15509. 'gl_FragColor = vec4( color * texture.xyz, texture.a * opacity );',
  15510. 'if ( fogType > 0 ) {',
  15511. 'float depth = gl_FragCoord.z / gl_FragCoord.w;',
  15512. 'float fogFactor = 0.0;',
  15513. 'if ( fogType == 1 ) {',
  15514. 'fogFactor = smoothstep( fogNear, fogFar, depth );',
  15515. '} else {',
  15516. 'const float LOG2 = 1.442695;',
  15517. 'float fogFactor = exp2( - fogDensity * fogDensity * depth * depth * LOG2 );',
  15518. 'fogFactor = 1.0 - clamp( fogFactor, 0.0, 1.0 );',
  15519. '}',
  15520. 'gl_FragColor = mix( gl_FragColor, vec4( fogColor, gl_FragColor.w ), fogFactor );',
  15521. '}',
  15522. '}'
  15523. ].join( '\n' ) );
  15524. gl.compileShader( vertexShader );
  15525. gl.compileShader( fragmentShader );
  15526. gl.attachShader( program, vertexShader );
  15527. gl.attachShader( program, fragmentShader );
  15528. gl.linkProgram( program );
  15529. return program;
  15530. };
  15531. function painterSortStable ( a, b ) {
  15532. if ( a.z !== b.z ) {
  15533. return b.z - a.z;
  15534. } else {
  15535. return b.id - a.id;
  15536. }
  15537. };
  15538. };
  15539. // File:src/extras/GeometryUtils.js
  15540. /**
  15541. * @author mrdoob / http://mrdoob.com/
  15542. */
  15543. THREE.GeometryUtils = {
  15544. merge: function ( geometry1, geometry2, materialIndexOffset ) {
  15545. console.warn( 'THREE.GeometryUtils: .merge() has been moved to Geometry. Use geometry.merge( geometry2, matrix, materialIndexOffset ) instead.' );
  15546. var matrix;
  15547. if ( geometry2 instanceof THREE.Mesh ) {
  15548. geometry2.matrixAutoUpdate && geometry2.updateMatrix();
  15549. matrix = geometry2.matrix;
  15550. geometry2 = geometry2.geometry;
  15551. }
  15552. geometry1.merge( geometry2, matrix, materialIndexOffset );
  15553. },
  15554. center: function ( geometry ) {
  15555. console.warn( 'THREE.GeometryUtils: .center() has been moved to Geometry. Use geometry.center() instead.' );
  15556. return geometry.center();
  15557. }
  15558. };
  15559. // File:src/extras/ImageUtils.js
  15560. /**
  15561. * @author alteredq / http://alteredqualia.com/
  15562. * @author mrdoob / http://mrdoob.com/
  15563. * @author Daosheng Mu / https://github.com/DaoshengMu/
  15564. */
  15565. THREE.ImageUtils = {
  15566. crossOrigin: undefined,
  15567. loadTexture: function ( url, mapping, onLoad, onError ) {
  15568. var loader = new THREE.ImageLoader();
  15569. loader.crossOrigin = this.crossOrigin;
  15570. var texture = new THREE.Texture( undefined, mapping );
  15571. loader.load( url, function ( image ) {
  15572. texture.image = image;
  15573. texture.needsUpdate = true;
  15574. if ( onLoad ) onLoad( texture );
  15575. }, undefined, function ( event ) {
  15576. if ( onError ) onError( event );
  15577. } );
  15578. texture.sourceFile = url;
  15579. return texture;
  15580. },
  15581. loadTextureCube: function ( array, mapping, onLoad, onError ) {
  15582. var images = [];
  15583. var loader = new THREE.ImageLoader();
  15584. loader.crossOrigin = this.crossOrigin;
  15585. var texture = new THREE.CubeTexture( images, mapping );
  15586. // no flipping needed for cube textures
  15587. texture.flipY = false;
  15588. var loaded = 0;
  15589. var loadTexture = function ( i ) {
  15590. loader.load( array[ i ], function ( image ) {
  15591. texture.images[ i ] = image;
  15592. loaded += 1;
  15593. if ( loaded === 6 ) {
  15594. texture.needsUpdate = true;
  15595. if ( onLoad ) onLoad( texture );
  15596. }
  15597. } );
  15598. }
  15599. for ( var i = 0, il = array.length; i < il; ++ i ) {
  15600. loadTexture( i );
  15601. }
  15602. return texture;
  15603. },
  15604. loadCompressedTexture: function () {
  15605. console.error( 'THREE.ImageUtils.loadCompressedTexture has been removed. Use THREE.DDSLoader instead.' )
  15606. },
  15607. loadCompressedTextureCube: function () {
  15608. console.error( 'THREE.ImageUtils.loadCompressedTextureCube has been removed. Use THREE.DDSLoader instead.' )
  15609. },
  15610. getNormalMap: function ( image, depth ) {
  15611. // Adapted from http://www.paulbrunt.co.uk/lab/heightnormal/
  15612. var cross = function ( a, b ) {
  15613. return [ a[ 1 ] * b[ 2 ] - a[ 2 ] * b[ 1 ], a[ 2 ] * b[ 0 ] - a[ 0 ] * b[ 2 ], a[ 0 ] * b[ 1 ] - a[ 1 ] * b[ 0 ] ];
  15614. }
  15615. var subtract = function ( a, b ) {
  15616. return [ a[ 0 ] - b[ 0 ], a[ 1 ] - b[ 1 ], a[ 2 ] - b[ 2 ] ];
  15617. }
  15618. var normalize = function ( a ) {
  15619. var l = Math.sqrt( a[ 0 ] * a[ 0 ] + a[ 1 ] * a[ 1 ] + a[ 2 ] * a[ 2 ] );
  15620. return [ a[ 0 ] / l, a[ 1 ] / l, a[ 2 ] / l ];
  15621. }
  15622. depth = depth | 1;
  15623. var width = image.width;
  15624. var height = image.height;
  15625. var canvas = document.createElement( 'canvas' );
  15626. canvas.width = width;
  15627. canvas.height = height;
  15628. var context = canvas.getContext( '2d' );
  15629. context.drawImage( image, 0, 0 );
  15630. var data = context.getImageData( 0, 0, width, height ).data;
  15631. var imageData = context.createImageData( width, height );
  15632. var output = imageData.data;
  15633. for ( var x = 0; x < width; x ++ ) {
  15634. for ( var y = 0; y < height; y ++ ) {
  15635. var ly = y - 1 < 0 ? 0 : y - 1;
  15636. var uy = y + 1 > height - 1 ? height - 1 : y + 1;
  15637. var lx = x - 1 < 0 ? 0 : x - 1;
  15638. var ux = x + 1 > width - 1 ? width - 1 : x + 1;
  15639. var points = [];
  15640. var origin = [ 0, 0, data[ ( y * width + x ) * 4 ] / 255 * depth ];
  15641. points.push( [ - 1, 0, data[ ( y * width + lx ) * 4 ] / 255 * depth ] );
  15642. points.push( [ - 1, - 1, data[ ( ly * width + lx ) * 4 ] / 255 * depth ] );
  15643. points.push( [ 0, - 1, data[ ( ly * width + x ) * 4 ] / 255 * depth ] );
  15644. points.push( [ 1, - 1, data[ ( ly * width + ux ) * 4 ] / 255 * depth ] );
  15645. points.push( [ 1, 0, data[ ( y * width + ux ) * 4 ] / 255 * depth ] );
  15646. points.push( [ 1, 1, data[ ( uy * width + ux ) * 4 ] / 255 * depth ] );
  15647. points.push( [ 0, 1, data[ ( uy * width + x ) * 4 ] / 255 * depth ] );
  15648. points.push( [ - 1, 1, data[ ( uy * width + lx ) * 4 ] / 255 * depth ] );
  15649. var normals = [];
  15650. var num_points = points.length;
  15651. for ( var i = 0; i < num_points; i ++ ) {
  15652. var v1 = points[ i ];
  15653. var v2 = points[ ( i + 1 ) % num_points ];
  15654. v1 = subtract( v1, origin );
  15655. v2 = subtract( v2, origin );
  15656. normals.push( normalize( cross( v1, v2 ) ) );
  15657. }
  15658. var normal = [ 0, 0, 0 ];
  15659. for ( var i = 0; i < normals.length; i ++ ) {
  15660. normal[ 0 ] += normals[ i ][ 0 ];
  15661. normal[ 1 ] += normals[ i ][ 1 ];
  15662. normal[ 2 ] += normals[ i ][ 2 ];
  15663. }
  15664. normal[ 0 ] /= normals.length;
  15665. normal[ 1 ] /= normals.length;
  15666. normal[ 2 ] /= normals.length;
  15667. var idx = ( y * width + x ) * 4;
  15668. output[ idx ] = ( ( normal[ 0 ] + 1.0 ) / 2.0 * 255 ) | 0;
  15669. output[ idx + 1 ] = ( ( normal[ 1 ] + 1.0 ) / 2.0 * 255 ) | 0;
  15670. output[ idx + 2 ] = ( normal[ 2 ] * 255 ) | 0;
  15671. output[ idx + 3 ] = 255;
  15672. }
  15673. }
  15674. context.putImageData( imageData, 0, 0 );
  15675. return canvas;
  15676. },
  15677. generateDataTexture: function ( width, height, color ) {
  15678. var size = width * height;
  15679. var data = new Uint8Array( 3 * size );
  15680. var r = Math.floor( color.r * 255 );
  15681. var g = Math.floor( color.g * 255 );
  15682. var b = Math.floor( color.b * 255 );
  15683. for ( var i = 0; i < size; i ++ ) {
  15684. data[ i * 3 ] = r;
  15685. data[ i * 3 + 1 ] = g;
  15686. data[ i * 3 + 2 ] = b;
  15687. }
  15688. var texture = new THREE.DataTexture( data, width, height, THREE.RGBFormat );
  15689. texture.needsUpdate = true;
  15690. return texture;
  15691. }
  15692. };
  15693. // File:src/extras/SceneUtils.js
  15694. /**
  15695. * @author alteredq / http://alteredqualia.com/
  15696. */
  15697. THREE.SceneUtils = {
  15698. createMultiMaterialObject: function ( geometry, materials ) {
  15699. var group = new THREE.Object3D();
  15700. for ( var i = 0, l = materials.length; i < l; i ++ ) {
  15701. group.add( new THREE.Mesh( geometry, materials[ i ] ) );
  15702. }
  15703. return group;
  15704. },
  15705. detach: function ( child, parent, scene ) {
  15706. child.applyMatrix( parent.matrixWorld );
  15707. parent.remove( child );
  15708. scene.add( child );
  15709. },
  15710. attach: function ( child, scene, parent ) {
  15711. var matrixWorldInverse = new THREE.Matrix4();
  15712. matrixWorldInverse.getInverse( parent.matrixWorld );
  15713. child.applyMatrix( matrixWorldInverse );
  15714. scene.remove( child );
  15715. parent.add( child );
  15716. }
  15717. };
  15718. // File:src/extras/FontUtils.js
  15719. /**
  15720. * @author zz85 / http://www.lab4games.net/zz85/blog
  15721. * @author alteredq / http://alteredqualia.com/
  15722. *
  15723. * For Text operations in three.js (See TextGeometry)
  15724. *
  15725. * It uses techniques used in:
  15726. *
  15727. * typeface.js and canvastext
  15728. * For converting fonts and rendering with javascript
  15729. * http://typeface.neocracy.org
  15730. *
  15731. * Triangulation ported from AS3
  15732. * Simple Polygon Triangulation
  15733. * http://actionsnippet.com/?p=1462
  15734. *
  15735. * A Method to triangulate shapes with holes
  15736. * http://www.sakri.net/blog/2009/06/12/an-approach-to-triangulating-polygons-with-holes/
  15737. *
  15738. */
  15739. THREE.FontUtils = {
  15740. faces: {},
  15741. // Just for now. face[weight][style]
  15742. face: 'helvetiker',
  15743. weight: 'normal',
  15744. style: 'normal',
  15745. size: 150,
  15746. divisions: 10,
  15747. getFace: function () {
  15748. try {
  15749. return this.faces[ this.face ][ this.weight ][ this.style ];
  15750. } catch (e) {
  15751. throw "The font " + this.face + " with " + this.weight + " weight and " + this.style + " style is missing."
  15752. };
  15753. },
  15754. loadFace: function ( data ) {
  15755. var family = data.familyName.toLowerCase();
  15756. var ThreeFont = this;
  15757. ThreeFont.faces[ family ] = ThreeFont.faces[ family ] || {};
  15758. ThreeFont.faces[ family ][ data.cssFontWeight ] = ThreeFont.faces[ family ][ data.cssFontWeight ] || {};
  15759. ThreeFont.faces[ family ][ data.cssFontWeight ][ data.cssFontStyle ] = data;
  15760. var face = ThreeFont.faces[ family ][ data.cssFontWeight ][ data.cssFontStyle ] = data;
  15761. return data;
  15762. },
  15763. drawText: function ( text ) {
  15764. var characterPts = [], allPts = [];
  15765. // RenderText
  15766. var i, p,
  15767. face = this.getFace(),
  15768. scale = this.size / face.resolution,
  15769. offset = 0,
  15770. chars = String( text ).split( '' ),
  15771. length = chars.length;
  15772. var fontPaths = [];
  15773. for ( i = 0; i < length; i ++ ) {
  15774. var path = new THREE.Path();
  15775. var ret = this.extractGlyphPoints( chars[ i ], face, scale, offset, path );
  15776. offset += ret.offset;
  15777. fontPaths.push( ret.path );
  15778. }
  15779. // get the width
  15780. var width = offset / 2;
  15781. //
  15782. // for ( p = 0; p < allPts.length; p++ ) {
  15783. //
  15784. // allPts[ p ].x -= width;
  15785. //
  15786. // }
  15787. //var extract = this.extractPoints( allPts, characterPts );
  15788. //extract.contour = allPts;
  15789. //extract.paths = fontPaths;
  15790. //extract.offset = width;
  15791. return { paths: fontPaths, offset: width };
  15792. },
  15793. extractGlyphPoints: function ( c, face, scale, offset, path ) {
  15794. var pts = [];
  15795. var i, i2, divisions,
  15796. outline, action, length,
  15797. scaleX, scaleY,
  15798. x, y, cpx, cpy, cpx0, cpy0, cpx1, cpy1, cpx2, cpy2,
  15799. laste,
  15800. glyph = face.glyphs[ c ] || face.glyphs[ '?' ];
  15801. if ( ! glyph ) return;
  15802. if ( glyph.o ) {
  15803. outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) );
  15804. length = outline.length;
  15805. scaleX = scale;
  15806. scaleY = scale;
  15807. for ( i = 0; i < length; ) {
  15808. action = outline[ i ++ ];
  15809. //console.log( action );
  15810. switch ( action ) {
  15811. case 'm':
  15812. // Move To
  15813. x = outline[ i ++ ] * scaleX + offset;
  15814. y = outline[ i ++ ] * scaleY;
  15815. path.moveTo( x, y );
  15816. break;
  15817. case 'l':
  15818. // Line To
  15819. x = outline[ i ++ ] * scaleX + offset;
  15820. y = outline[ i ++ ] * scaleY;
  15821. path.lineTo( x,y );
  15822. break;
  15823. case 'q':
  15824. // QuadraticCurveTo
  15825. cpx = outline[ i ++ ] * scaleX + offset;
  15826. cpy = outline[ i ++ ] * scaleY;
  15827. cpx1 = outline[ i ++ ] * scaleX + offset;
  15828. cpy1 = outline[ i ++ ] * scaleY;
  15829. path.quadraticCurveTo( cpx1, cpy1, cpx, cpy );
  15830. laste = pts[ pts.length - 1 ];
  15831. if ( laste ) {
  15832. cpx0 = laste.x;
  15833. cpy0 = laste.y;
  15834. for ( i2 = 1, divisions = this.divisions; i2 <= divisions; i2 ++ ) {
  15835. var t = i2 / divisions;
  15836. var tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx );
  15837. var ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy );
  15838. }
  15839. }
  15840. break;
  15841. case 'b':
  15842. // Cubic Bezier Curve
  15843. cpx = outline[ i ++ ] * scaleX + offset;
  15844. cpy = outline[ i ++ ] * scaleY;
  15845. cpx1 = outline[ i ++ ] * scaleX + offset;
  15846. cpy1 = outline[ i ++ ] * scaleY;
  15847. cpx2 = outline[ i ++ ] * scaleX + offset;
  15848. cpy2 = outline[ i ++ ] * scaleY;
  15849. path.bezierCurveTo( cpx1, cpy1, cpx2, cpy2, cpx, cpy );
  15850. laste = pts[ pts.length - 1 ];
  15851. if ( laste ) {
  15852. cpx0 = laste.x;
  15853. cpy0 = laste.y;
  15854. for ( i2 = 1, divisions = this.divisions; i2 <= divisions; i2 ++ ) {
  15855. var t = i2 / divisions;
  15856. var tx = THREE.Shape.Utils.b3( t, cpx0, cpx1, cpx2, cpx );
  15857. var ty = THREE.Shape.Utils.b3( t, cpy0, cpy1, cpy2, cpy );
  15858. }
  15859. }
  15860. break;
  15861. }
  15862. }
  15863. }
  15864. return { offset: glyph.ha * scale, path:path };
  15865. }
  15866. };
  15867. THREE.FontUtils.generateShapes = function ( text, parameters ) {
  15868. // Parameters
  15869. parameters = parameters || {};
  15870. var size = parameters.size !== undefined ? parameters.size : 100;
  15871. var curveSegments = parameters.curveSegments !== undefined ? parameters.curveSegments : 4;
  15872. var font = parameters.font !== undefined ? parameters.font : 'helvetiker';
  15873. var weight = parameters.weight !== undefined ? parameters.weight : 'normal';
  15874. var style = parameters.style !== undefined ? parameters.style : 'normal';
  15875. THREE.FontUtils.size = size;
  15876. THREE.FontUtils.divisions = curveSegments;
  15877. THREE.FontUtils.face = font;
  15878. THREE.FontUtils.weight = weight;
  15879. THREE.FontUtils.style = style;
  15880. // Get a Font data json object
  15881. var data = THREE.FontUtils.drawText( text );
  15882. var paths = data.paths;
  15883. var shapes = [];
  15884. for ( var p = 0, pl = paths.length; p < pl; p ++ ) {
  15885. Array.prototype.push.apply( shapes, paths[ p ].toShapes() );
  15886. }
  15887. return shapes;
  15888. };
  15889. /**
  15890. * This code is a quick port of code written in C++ which was submitted to
  15891. * flipcode.com by John W. Ratcliff // July 22, 2000
  15892. * See original code and more information here:
  15893. * http://www.flipcode.com/archives/Efficient_Polygon_Triangulation.shtml
  15894. *
  15895. * ported to actionscript by Zevan Rosser
  15896. * www.actionsnippet.com
  15897. *
  15898. * ported to javascript by Joshua Koo
  15899. * http://www.lab4games.net/zz85/blog
  15900. *
  15901. */
  15902. ( function ( namespace ) {
  15903. var EPSILON = 0.0000000001;
  15904. // takes in an contour array and returns
  15905. var process = function ( contour, indices ) {
  15906. var n = contour.length;
  15907. if ( n < 3 ) return null;
  15908. var result = [],
  15909. verts = [],
  15910. vertIndices = [];
  15911. /* we want a counter-clockwise polygon in verts */
  15912. var u, v, w;
  15913. if ( area( contour ) > 0.0 ) {
  15914. for ( v = 0; v < n; v ++ ) verts[ v ] = v;
  15915. } else {
  15916. for ( v = 0; v < n; v ++ ) verts[ v ] = ( n - 1 ) - v;
  15917. }
  15918. var nv = n;
  15919. /* remove nv - 2 vertices, creating 1 triangle every time */
  15920. var count = 2 * nv; /* error detection */
  15921. for ( v = nv - 1; nv > 2; ) {
  15922. /* if we loop, it is probably a non-simple polygon */
  15923. if ( ( count -- ) <= 0 ) {
  15924. //** Triangulate: ERROR - probable bad polygon!
  15925. //throw ( "Warning, unable to triangulate polygon!" );
  15926. //return null;
  15927. // Sometimes warning is fine, especially polygons are triangulated in reverse.
  15928. console.log( 'Warning, unable to triangulate polygon!' );
  15929. if ( indices ) return vertIndices;
  15930. return result;
  15931. }
  15932. /* three consecutive vertices in current polygon, <u,v,w> */
  15933. u = v; if ( nv <= u ) u = 0; /* previous */
  15934. v = u + 1; if ( nv <= v ) v = 0; /* new v */
  15935. w = v + 1; if ( nv <= w ) w = 0; /* next */
  15936. if ( snip( contour, u, v, w, nv, verts ) ) {
  15937. var a, b, c, s, t;
  15938. /* true names of the vertices */
  15939. a = verts[ u ];
  15940. b = verts[ v ];
  15941. c = verts[ w ];
  15942. /* output Triangle */
  15943. result.push( [ contour[ a ],
  15944. contour[ b ],
  15945. contour[ c ] ] );
  15946. vertIndices.push( [ verts[ u ], verts[ v ], verts[ w ] ] );
  15947. /* remove v from the remaining polygon */
  15948. for ( s = v, t = v + 1; t < nv; s++, t++ ) {
  15949. verts[ s ] = verts[ t ];
  15950. }
  15951. nv --;
  15952. /* reset error detection counter */
  15953. count = 2 * nv;
  15954. }
  15955. }
  15956. if ( indices ) return vertIndices;
  15957. return result;
  15958. };
  15959. // calculate area of the contour polygon
  15960. var area = function ( contour ) {
  15961. var n = contour.length;
  15962. var a = 0.0;
  15963. for ( var p = n - 1, q = 0; q < n; p = q ++ ) {
  15964. a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y;
  15965. }
  15966. return a * 0.5;
  15967. };
  15968. var snip = function ( contour, u, v, w, n, verts ) {
  15969. var p;
  15970. var ax, ay, bx, by;
  15971. var cx, cy, px, py;
  15972. ax = contour[ verts[ u ] ].x;
  15973. ay = contour[ verts[ u ] ].y;
  15974. bx = contour[ verts[ v ] ].x;
  15975. by = contour[ verts[ v ] ].y;
  15976. cx = contour[ verts[ w ] ].x;
  15977. cy = contour[ verts[ w ] ].y;
  15978. if ( EPSILON > ( ( ( bx - ax ) * ( cy - ay ) ) - ( ( by - ay ) * ( cx - ax ) ) ) ) return false;
  15979. var aX, aY, bX, bY, cX, cY;
  15980. var apx, apy, bpx, bpy, cpx, cpy;
  15981. var cCROSSap, bCROSScp, aCROSSbp;
  15982. aX = cx - bx; aY = cy - by;
  15983. bX = ax - cx; bY = ay - cy;
  15984. cX = bx - ax; cY = by - ay;
  15985. for ( p = 0; p < n; p ++ ) {
  15986. px = contour[ verts[ p ] ].x
  15987. py = contour[ verts[ p ] ].y
  15988. if ( ( ( px === ax ) && ( py === ay ) ) ||
  15989. ( ( px === bx ) && ( py === by ) ) ||
  15990. ( ( px === cx ) && ( py === cy ) ) ) continue;
  15991. apx = px - ax; apy = py - ay;
  15992. bpx = px - bx; bpy = py - by;
  15993. cpx = px - cx; cpy = py - cy;
  15994. // see if p is inside triangle abc
  15995. aCROSSbp = aX * bpy - aY * bpx;
  15996. cCROSSap = cX * apy - cY * apx;
  15997. bCROSScp = bX * cpy - bY * cpx;
  15998. if ( ( aCROSSbp >= - EPSILON ) && ( bCROSScp >= - EPSILON ) && ( cCROSSap >= - EPSILON ) ) return false;
  15999. }
  16000. return true;
  16001. };
  16002. namespace.Triangulate = process;
  16003. namespace.Triangulate.area = area;
  16004. return namespace;
  16005. } )( THREE.FontUtils );
  16006. // To use the typeface.js face files, hook up the API
  16007. self._typeface_js = { faces: THREE.FontUtils.faces, loadFace: THREE.FontUtils.loadFace };
  16008. THREE.typeface_js = self._typeface_js;
  16009. // File:src/extras/audio/Audio.js
  16010. /**
  16011. * @author mrdoob / http://mrdoob.com/
  16012. */
  16013. THREE.Audio = function ( listener ) {
  16014. THREE.Object3D.call( this );
  16015. this.type = 'Audio';
  16016. this.context = listener.context;
  16017. this.source = this.context.createBufferSource();
  16018. this.gain = this.context.createGain();
  16019. this.gain.connect( this.context.destination );
  16020. this.panner = this.context.createPanner();
  16021. this.panner.connect( this.gain );
  16022. };
  16023. THREE.Audio.prototype = Object.create( THREE.Object3D.prototype );
  16024. THREE.Audio.prototype.constructor = THREE.Audio;
  16025. THREE.Audio.prototype.load = function ( file ) {
  16026. var scope = this;
  16027. var request = new XMLHttpRequest();
  16028. request.open( 'GET', file, true );
  16029. request.responseType = 'arraybuffer';
  16030. request.onload = function ( e ) {
  16031. scope.context.decodeAudioData( this.response, function ( buffer ) {
  16032. scope.source.buffer = buffer;
  16033. scope.source.connect( scope.panner );
  16034. scope.source.start( 0 );
  16035. } );
  16036. };
  16037. request.send();
  16038. return this;
  16039. };
  16040. THREE.Audio.prototype.setLoop = function ( value ) {
  16041. this.source.loop = value;
  16042. };
  16043. THREE.Audio.prototype.setRefDistance = function ( value ) {
  16044. this.panner.refDistance = value;
  16045. };
  16046. THREE.Audio.prototype.setRolloffFactor = function ( value ) {
  16047. this.panner.rolloffFactor = value;
  16048. };
  16049. THREE.Audio.prototype.updateMatrixWorld = ( function () {
  16050. var position = new THREE.Vector3();
  16051. return function ( force ) {
  16052. THREE.Object3D.prototype.updateMatrixWorld.call( this, force );
  16053. position.setFromMatrixPosition( this.matrixWorld );
  16054. this.panner.setPosition( position.x, position.y, position.z );
  16055. };
  16056. } )();
  16057. // File:src/extras/audio/AudioListener.js
  16058. /**
  16059. * @author mrdoob / http://mrdoob.com/
  16060. */
  16061. THREE.AudioListener = function () {
  16062. THREE.Object3D.call( this );
  16063. this.type = 'AudioListener';
  16064. this.context = new ( window.AudioContext || window.webkitAudioContext )();
  16065. };
  16066. THREE.AudioListener.prototype = Object.create( THREE.Object3D.prototype );
  16067. THREE.AudioListener.prototype.constructor = THREE.AudioListener;
  16068. THREE.AudioListener.prototype.updateMatrixWorld = ( function () {
  16069. var position = new THREE.Vector3();
  16070. var quaternion = new THREE.Quaternion();
  16071. var scale = new THREE.Vector3();
  16072. var orientation = new THREE.Vector3();
  16073. var velocity = new THREE.Vector3();
  16074. var positionPrev = new THREE.Vector3();
  16075. return function ( force ) {
  16076. THREE.Object3D.prototype.updateMatrixWorld.call( this, force );
  16077. var listener = this.context.listener;
  16078. this.matrixWorld.decompose( position, quaternion, scale );
  16079. orientation.set( 0, 0, -1 ).applyQuaternion( quaternion );
  16080. velocity.subVectors( position, positionPrev );
  16081. listener.setPosition( position.x, position.y, position.z );
  16082. listener.setOrientation( orientation.x, orientation.y, orientation.z, this.up.x, this.up.y, this.up.z );
  16083. listener.setVelocity( velocity.x, velocity.y, velocity.z );
  16084. positionPrev.copy( position );
  16085. };
  16086. } )();
  16087. // File:src/extras/core/Curve.js
  16088. /**
  16089. * @author zz85 / http://www.lab4games.net/zz85/blog
  16090. * Extensible curve object
  16091. *
  16092. * Some common of Curve methods
  16093. * .getPoint(t), getTangent(t)
  16094. * .getPointAt(u), getTagentAt(u)
  16095. * .getPoints(), .getSpacedPoints()
  16096. * .getLength()
  16097. * .updateArcLengths()
  16098. *
  16099. * This following classes subclasses THREE.Curve:
  16100. *
  16101. * -- 2d classes --
  16102. * THREE.LineCurve
  16103. * THREE.QuadraticBezierCurve
  16104. * THREE.CubicBezierCurve
  16105. * THREE.SplineCurve
  16106. * THREE.ArcCurve
  16107. * THREE.EllipseCurve
  16108. *
  16109. * -- 3d classes --
  16110. * THREE.LineCurve3
  16111. * THREE.QuadraticBezierCurve3
  16112. * THREE.CubicBezierCurve3
  16113. * THREE.SplineCurve3
  16114. * THREE.ClosedSplineCurve3
  16115. *
  16116. * A series of curves can be represented as a THREE.CurvePath
  16117. *
  16118. **/
  16119. /**************************************************************
  16120. * Abstract Curve base class
  16121. **************************************************************/
  16122. THREE.Curve = function () {
  16123. };
  16124. // Virtual base class method to overwrite and implement in subclasses
  16125. // - t [0 .. 1]
  16126. THREE.Curve.prototype.getPoint = function ( t ) {
  16127. console.log( "Warning, getPoint() not implemented!" );
  16128. return null;
  16129. };
  16130. // Get point at relative position in curve according to arc length
  16131. // - u [0 .. 1]
  16132. THREE.Curve.prototype.getPointAt = function ( u ) {
  16133. var t = this.getUtoTmapping( u );
  16134. return this.getPoint( t );
  16135. };
  16136. // Get sequence of points using getPoint( t )
  16137. THREE.Curve.prototype.getPoints = function ( divisions ) {
  16138. if ( ! divisions ) divisions = 5;
  16139. var d, pts = [];
  16140. for ( d = 0; d <= divisions; d ++ ) {
  16141. pts.push( this.getPoint( d / divisions ) );
  16142. }
  16143. return pts;
  16144. };
  16145. // Get sequence of points using getPointAt( u )
  16146. THREE.Curve.prototype.getSpacedPoints = function ( divisions ) {
  16147. if ( ! divisions ) divisions = 5;
  16148. var d, pts = [];
  16149. for ( d = 0; d <= divisions; d ++ ) {
  16150. pts.push( this.getPointAt( d / divisions ) );
  16151. }
  16152. return pts;
  16153. };
  16154. // Get total curve arc length
  16155. THREE.Curve.prototype.getLength = function () {
  16156. var lengths = this.getLengths();
  16157. return lengths[ lengths.length - 1 ];
  16158. };
  16159. // Get list of cumulative segment lengths
  16160. THREE.Curve.prototype.getLengths = function ( divisions ) {
  16161. if ( ! divisions ) divisions = (this.__arcLengthDivisions) ? (this.__arcLengthDivisions): 200;
  16162. if ( this.cacheArcLengths
  16163. && ( this.cacheArcLengths.length == divisions + 1 )
  16164. && ! this.needsUpdate) {
  16165. //console.log( "cached", this.cacheArcLengths );
  16166. return this.cacheArcLengths;
  16167. }
  16168. this.needsUpdate = false;
  16169. var cache = [];
  16170. var current, last = this.getPoint( 0 );
  16171. var p, sum = 0;
  16172. cache.push( 0 );
  16173. for ( p = 1; p <= divisions; p ++ ) {
  16174. current = this.getPoint ( p / divisions );
  16175. sum += current.distanceTo( last );
  16176. cache.push( sum );
  16177. last = current;
  16178. }
  16179. this.cacheArcLengths = cache;
  16180. return cache; // { sums: cache, sum:sum }; Sum is in the last element.
  16181. };
  16182. THREE.Curve.prototype.updateArcLengths = function() {
  16183. this.needsUpdate = true;
  16184. this.getLengths();
  16185. };
  16186. // Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equi distance
  16187. THREE.Curve.prototype.getUtoTmapping = function ( u, distance ) {
  16188. var arcLengths = this.getLengths();
  16189. var i = 0, il = arcLengths.length;
  16190. var targetArcLength; // The targeted u distance value to get
  16191. if ( distance ) {
  16192. targetArcLength = distance;
  16193. } else {
  16194. targetArcLength = u * arcLengths[ il - 1 ];
  16195. }
  16196. //var time = Date.now();
  16197. // binary search for the index with largest value smaller than target u distance
  16198. var low = 0, high = il - 1, comparison;
  16199. while ( low <= high ) {
  16200. i = Math.floor( low + ( high - low ) / 2 ); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats
  16201. comparison = arcLengths[ i ] - targetArcLength;
  16202. if ( comparison < 0 ) {
  16203. low = i + 1;
  16204. continue;
  16205. } else if ( comparison > 0 ) {
  16206. high = i - 1;
  16207. continue;
  16208. } else {
  16209. high = i;
  16210. break;
  16211. // DONE
  16212. }
  16213. }
  16214. i = high;
  16215. //console.log('b' , i, low, high, Date.now()- time);
  16216. if ( arcLengths[ i ] == targetArcLength ) {
  16217. var t = i / ( il - 1 );
  16218. return t;
  16219. }
  16220. // we could get finer grain at lengths, or use simple interpolatation between two points
  16221. var lengthBefore = arcLengths[ i ];
  16222. var lengthAfter = arcLengths[ i + 1 ];
  16223. var segmentLength = lengthAfter - lengthBefore;
  16224. // determine where we are between the 'before' and 'after' points
  16225. var segmentFraction = ( targetArcLength - lengthBefore ) / segmentLength;
  16226. // add that fractional amount to t
  16227. var t = ( i + segmentFraction ) / ( il -1 );
  16228. return t;
  16229. };
  16230. // Returns a unit vector tangent at t
  16231. // In case any sub curve does not implement its tangent derivation,
  16232. // 2 points a small delta apart will be used to find its gradient
  16233. // which seems to give a reasonable approximation
  16234. THREE.Curve.prototype.getTangent = function( t ) {
  16235. var delta = 0.0001;
  16236. var t1 = t - delta;
  16237. var t2 = t + delta;
  16238. // Capping in case of danger
  16239. if ( t1 < 0 ) t1 = 0;
  16240. if ( t2 > 1 ) t2 = 1;
  16241. var pt1 = this.getPoint( t1 );
  16242. var pt2 = this.getPoint( t2 );
  16243. var vec = pt2.clone().sub(pt1);
  16244. return vec.normalize();
  16245. };
  16246. THREE.Curve.prototype.getTangentAt = function ( u ) {
  16247. var t = this.getUtoTmapping( u );
  16248. return this.getTangent( t );
  16249. };
  16250. /**************************************************************
  16251. * Utils
  16252. **************************************************************/
  16253. THREE.Curve.Utils = {
  16254. tangentQuadraticBezier: function ( t, p0, p1, p2 ) {
  16255. return 2 * ( 1 - t ) * ( p1 - p0 ) + 2 * t * ( p2 - p1 );
  16256. },
  16257. // Puay Bing, thanks for helping with this derivative!
  16258. tangentCubicBezier: function (t, p0, p1, p2, p3 ) {
  16259. return - 3 * p0 * (1 - t) * (1 - t) +
  16260. 3 * p1 * (1 - t) * (1-t) - 6 *t *p1 * (1-t) +
  16261. 6 * t * p2 * (1-t) - 3 * t * t * p2 +
  16262. 3 * t * t * p3;
  16263. },
  16264. tangentSpline: function ( t, p0, p1, p2, p3 ) {
  16265. // To check if my formulas are correct
  16266. var h00 = 6 * t * t - 6 * t; // derived from 2t^3 − 3t^2 + 1
  16267. var h10 = 3 * t * t - 4 * t + 1; // t^3 − 2t^2 + t
  16268. var h01 = - 6 * t * t + 6 * t; // − 2t3 + 3t2
  16269. var h11 = 3 * t * t - 2 * t; // t3 − t2
  16270. return h00 + h10 + h01 + h11;
  16271. },
  16272. // Catmull-Rom
  16273. interpolate: function( p0, p1, p2, p3, t ) {
  16274. var v0 = ( p2 - p0 ) * 0.5;
  16275. var v1 = ( p3 - p1 ) * 0.5;
  16276. var t2 = t * t;
  16277. var t3 = t * t2;
  16278. return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  16279. }
  16280. };
  16281. // TODO: Transformation for Curves?
  16282. /**************************************************************
  16283. * 3D Curves
  16284. **************************************************************/
  16285. // A Factory method for creating new curve subclasses
  16286. THREE.Curve.create = function ( constructor, getPointFunc ) {
  16287. constructor.prototype = Object.create( THREE.Curve.prototype );
  16288. constructor.prototype.constructor = constructor;
  16289. constructor.prototype.getPoint = getPointFunc;
  16290. return constructor;
  16291. };
  16292. // File:src/extras/core/CurvePath.js
  16293. /**
  16294. * @author zz85 / http://www.lab4games.net/zz85/blog
  16295. *
  16296. **/
  16297. /**************************************************************
  16298. * Curved Path - a curve path is simply a array of connected
  16299. * curves, but retains the api of a curve
  16300. **************************************************************/
  16301. THREE.CurvePath = function () {
  16302. this.curves = [];
  16303. this.bends = [];
  16304. this.autoClose = false; // Automatically closes the path
  16305. };
  16306. THREE.CurvePath.prototype = Object.create( THREE.Curve.prototype );
  16307. THREE.CurvePath.prototype.constructor = THREE.CurvePath;
  16308. THREE.CurvePath.prototype.add = function ( curve ) {
  16309. this.curves.push( curve );
  16310. };
  16311. THREE.CurvePath.prototype.checkConnection = function() {
  16312. // TODO
  16313. // If the ending of curve is not connected to the starting
  16314. // or the next curve, then, this is not a real path
  16315. };
  16316. THREE.CurvePath.prototype.closePath = function() {
  16317. // TODO Test
  16318. // and verify for vector3 (needs to implement equals)
  16319. // Add a line curve if start and end of lines are not connected
  16320. var startPoint = this.curves[0].getPoint(0);
  16321. var endPoint = this.curves[this.curves.length-1].getPoint(1);
  16322. if (! startPoint.equals(endPoint)) {
  16323. this.curves.push( new THREE.LineCurve(endPoint, startPoint) );
  16324. }
  16325. };
  16326. // To get accurate point with reference to
  16327. // entire path distance at time t,
  16328. // following has to be done:
  16329. // 1. Length of each sub path have to be known
  16330. // 2. Locate and identify type of curve
  16331. // 3. Get t for the curve
  16332. // 4. Return curve.getPointAt(t')
  16333. THREE.CurvePath.prototype.getPoint = function( t ) {
  16334. var d = t * this.getLength();
  16335. var curveLengths = this.getCurveLengths();
  16336. var i = 0, diff, curve;
  16337. // To think about boundaries points.
  16338. while ( i < curveLengths.length ) {
  16339. if ( curveLengths[ i ] >= d ) {
  16340. diff = curveLengths[ i ] - d;
  16341. curve = this.curves[ i ];
  16342. var u = 1 - diff / curve.getLength();
  16343. return curve.getPointAt( u );
  16344. break;
  16345. }
  16346. i ++;
  16347. }
  16348. return null;
  16349. // loop where sum != 0, sum > d , sum+1 <d
  16350. };
  16351. /*
  16352. THREE.CurvePath.prototype.getTangent = function( t ) {
  16353. };*/
  16354. // We cannot use the default THREE.Curve getPoint() with getLength() because in
  16355. // THREE.Curve, getLength() depends on getPoint() but in THREE.CurvePath
  16356. // getPoint() depends on getLength
  16357. THREE.CurvePath.prototype.getLength = function() {
  16358. var lens = this.getCurveLengths();
  16359. return lens[ lens.length - 1 ];
  16360. };
  16361. // Compute lengths and cache them
  16362. // We cannot overwrite getLengths() because UtoT mapping uses it.
  16363. THREE.CurvePath.prototype.getCurveLengths = function() {
  16364. // We use cache values if curves and cache array are same length
  16365. if ( this.cacheLengths && this.cacheLengths.length == this.curves.length ) {
  16366. return this.cacheLengths;
  16367. };
  16368. // Get length of subsurve
  16369. // Push sums into cached array
  16370. var lengths = [], sums = 0;
  16371. var i, il = this.curves.length;
  16372. for ( i = 0; i < il; i ++ ) {
  16373. sums += this.curves[ i ].getLength();
  16374. lengths.push( sums );
  16375. }
  16376. this.cacheLengths = lengths;
  16377. return lengths;
  16378. };
  16379. // Returns min and max coordinates
  16380. THREE.CurvePath.prototype.getBoundingBox = function () {
  16381. var points = this.getPoints();
  16382. var maxX, maxY, maxZ;
  16383. var minX, minY, minZ;
  16384. maxX = maxY = Number.NEGATIVE_INFINITY;
  16385. minX = minY = Number.POSITIVE_INFINITY;
  16386. var p, i, il, sum;
  16387. var v3 = points[0] instanceof THREE.Vector3;
  16388. sum = v3 ? new THREE.Vector3() : new THREE.Vector2();
  16389. for ( i = 0, il = points.length; i < il; i ++ ) {
  16390. p = points[ i ];
  16391. if ( p.x > maxX ) maxX = p.x;
  16392. else if ( p.x < minX ) minX = p.x;
  16393. if ( p.y > maxY ) maxY = p.y;
  16394. else if ( p.y < minY ) minY = p.y;
  16395. if ( v3 ) {
  16396. if ( p.z > maxZ ) maxZ = p.z;
  16397. else if ( p.z < minZ ) minZ = p.z;
  16398. }
  16399. sum.add( p );
  16400. }
  16401. var ret = {
  16402. minX: minX,
  16403. minY: minY,
  16404. maxX: maxX,
  16405. maxY: maxY
  16406. };
  16407. if ( v3 ) {
  16408. ret.maxZ = maxZ;
  16409. ret.minZ = minZ;
  16410. }
  16411. return ret;
  16412. };
  16413. /**************************************************************
  16414. * Create Geometries Helpers
  16415. **************************************************************/
  16416. /// Generate geometry from path points (for Line or Points objects)
  16417. THREE.CurvePath.prototype.createPointsGeometry = function( divisions ) {
  16418. var pts = this.getPoints( divisions, true );
  16419. return this.createGeometry( pts );
  16420. };
  16421. // Generate geometry from equidistance sampling along the path
  16422. THREE.CurvePath.prototype.createSpacedPointsGeometry = function( divisions ) {
  16423. var pts = this.getSpacedPoints( divisions, true );
  16424. return this.createGeometry( pts );
  16425. };
  16426. THREE.CurvePath.prototype.createGeometry = function( points ) {
  16427. var geometry = new THREE.Geometry();
  16428. for ( var i = 0; i < points.length; i ++ ) {
  16429. geometry.vertices.push( new THREE.Vector3( points[ i ].x, points[ i ].y, points[ i ].z || 0) );
  16430. }
  16431. return geometry;
  16432. };
  16433. /**************************************************************
  16434. * Bend / Wrap Helper Methods
  16435. **************************************************************/
  16436. // Wrap path / Bend modifiers?
  16437. THREE.CurvePath.prototype.addWrapPath = function ( bendpath ) {
  16438. this.bends.push( bendpath );
  16439. };
  16440. THREE.CurvePath.prototype.getTransformedPoints = function( segments, bends ) {
  16441. var oldPts = this.getPoints( segments ); // getPoints getSpacedPoints
  16442. var i, il;
  16443. if ( ! bends ) {
  16444. bends = this.bends;
  16445. }
  16446. for ( i = 0, il = bends.length; i < il; i ++ ) {
  16447. oldPts = this.getWrapPoints( oldPts, bends[ i ] );
  16448. }
  16449. return oldPts;
  16450. };
  16451. THREE.CurvePath.prototype.getTransformedSpacedPoints = function( segments, bends ) {
  16452. var oldPts = this.getSpacedPoints( segments );
  16453. var i, il;
  16454. if ( ! bends ) {
  16455. bends = this.bends;
  16456. }
  16457. for ( i = 0, il = bends.length; i < il; i ++ ) {
  16458. oldPts = this.getWrapPoints( oldPts, bends[ i ] );
  16459. }
  16460. return oldPts;
  16461. };
  16462. // This returns getPoints() bend/wrapped around the contour of a path.
  16463. // Read http://www.planetclegg.com/projects/WarpingTextToSplines.html
  16464. THREE.CurvePath.prototype.getWrapPoints = function ( oldPts, path ) {
  16465. var bounds = this.getBoundingBox();
  16466. var i, il, p, oldX, oldY, xNorm;
  16467. for ( i = 0, il = oldPts.length; i < il; i ++ ) {
  16468. p = oldPts[ i ];
  16469. oldX = p.x;
  16470. oldY = p.y;
  16471. xNorm = oldX / bounds.maxX;
  16472. // If using actual distance, for length > path, requires line extrusions
  16473. //xNorm = path.getUtoTmapping(xNorm, oldX); // 3 styles. 1) wrap stretched. 2) wrap stretch by arc length 3) warp by actual distance
  16474. xNorm = path.getUtoTmapping( xNorm, oldX );
  16475. // check for out of bounds?
  16476. var pathPt = path.getPoint( xNorm );
  16477. var normal = path.getTangent( xNorm );
  16478. normal.set( - normal.y, normal.x ).multiplyScalar( oldY );
  16479. p.x = pathPt.x + normal.x;
  16480. p.y = pathPt.y + normal.y;
  16481. }
  16482. return oldPts;
  16483. };
  16484. // File:src/extras/core/Gyroscope.js
  16485. /**
  16486. * @author alteredq / http://alteredqualia.com/
  16487. */
  16488. THREE.Gyroscope = function () {
  16489. THREE.Object3D.call( this );
  16490. };
  16491. THREE.Gyroscope.prototype = Object.create( THREE.Object3D.prototype );
  16492. THREE.Gyroscope.prototype.constructor = THREE.Gyroscope;
  16493. THREE.Gyroscope.prototype.updateMatrixWorld = ( function () {
  16494. var translationObject = new THREE.Vector3();
  16495. var quaternionObject = new THREE.Quaternion();
  16496. var scaleObject = new THREE.Vector3();
  16497. var translationWorld = new THREE.Vector3();
  16498. var quaternionWorld = new THREE.Quaternion();
  16499. var scaleWorld = new THREE.Vector3();
  16500. return function ( force ) {
  16501. this.matrixAutoUpdate && this.updateMatrix();
  16502. // update matrixWorld
  16503. if ( this.matrixWorldNeedsUpdate || force ) {
  16504. if ( this.parent ) {
  16505. this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix );
  16506. this.matrixWorld.decompose( translationWorld, quaternionWorld, scaleWorld );
  16507. this.matrix.decompose( translationObject, quaternionObject, scaleObject );
  16508. this.matrixWorld.compose( translationWorld, quaternionObject, scaleWorld );
  16509. } else {
  16510. this.matrixWorld.copy( this.matrix );
  16511. }
  16512. this.matrixWorldNeedsUpdate = false;
  16513. force = true;
  16514. }
  16515. // update children
  16516. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  16517. this.children[ i ].updateMatrixWorld( force );
  16518. }
  16519. };
  16520. }() );
  16521. // File:src/extras/core/Path.js
  16522. /**
  16523. * @author zz85 / http://www.lab4games.net/zz85/blog
  16524. * Creates free form 2d path using series of points, lines or curves.
  16525. *
  16526. **/
  16527. THREE.Path = function ( points ) {
  16528. THREE.CurvePath.call(this);
  16529. this.actions = [];
  16530. if ( points ) {
  16531. this.fromPoints( points );
  16532. }
  16533. };
  16534. THREE.Path.prototype = Object.create( THREE.CurvePath.prototype );
  16535. THREE.Path.prototype.constructor = THREE.Path;
  16536. THREE.PathActions = {
  16537. MOVE_TO: 'moveTo',
  16538. LINE_TO: 'lineTo',
  16539. QUADRATIC_CURVE_TO: 'quadraticCurveTo', // Bezier quadratic curve
  16540. BEZIER_CURVE_TO: 'bezierCurveTo', // Bezier cubic curve
  16541. CSPLINE_THRU: 'splineThru', // Catmull-rom spline
  16542. ARC: 'arc', // Circle
  16543. ELLIPSE: 'ellipse'
  16544. };
  16545. // TODO Clean up PATH API
  16546. // Create path using straight lines to connect all points
  16547. // - vectors: array of Vector2
  16548. THREE.Path.prototype.fromPoints = function ( vectors ) {
  16549. this.moveTo( vectors[ 0 ].x, vectors[ 0 ].y );
  16550. for ( var v = 1, vlen = vectors.length; v < vlen; v ++ ) {
  16551. this.lineTo( vectors[ v ].x, vectors[ v ].y );
  16552. };
  16553. };
  16554. // startPath() endPath()?
  16555. THREE.Path.prototype.moveTo = function ( x, y ) {
  16556. var args = Array.prototype.slice.call( arguments );
  16557. this.actions.push( { action: THREE.PathActions.MOVE_TO, args: args } );
  16558. };
  16559. THREE.Path.prototype.lineTo = function ( x, y ) {
  16560. var args = Array.prototype.slice.call( arguments );
  16561. var lastargs = this.actions[ this.actions.length - 1 ].args;
  16562. var x0 = lastargs[ lastargs.length - 2 ];
  16563. var y0 = lastargs[ lastargs.length - 1 ];
  16564. var curve = new THREE.LineCurve( new THREE.Vector2( x0, y0 ), new THREE.Vector2( x, y ) );
  16565. this.curves.push( curve );
  16566. this.actions.push( { action: THREE.PathActions.LINE_TO, args: args } );
  16567. };
  16568. THREE.Path.prototype.quadraticCurveTo = function( aCPx, aCPy, aX, aY ) {
  16569. var args = Array.prototype.slice.call( arguments );
  16570. var lastargs = this.actions[ this.actions.length - 1 ].args;
  16571. var x0 = lastargs[ lastargs.length - 2 ];
  16572. var y0 = lastargs[ lastargs.length - 1 ];
  16573. var curve = new THREE.QuadraticBezierCurve( new THREE.Vector2( x0, y0 ),
  16574. new THREE.Vector2( aCPx, aCPy ),
  16575. new THREE.Vector2( aX, aY ) );
  16576. this.curves.push( curve );
  16577. this.actions.push( { action: THREE.PathActions.QUADRATIC_CURVE_TO, args: args } );
  16578. };
  16579. THREE.Path.prototype.bezierCurveTo = function( aCP1x, aCP1y,
  16580. aCP2x, aCP2y,
  16581. aX, aY ) {
  16582. var args = Array.prototype.slice.call( arguments );
  16583. var lastargs = this.actions[ this.actions.length - 1 ].args;
  16584. var x0 = lastargs[ lastargs.length - 2 ];
  16585. var y0 = lastargs[ lastargs.length - 1 ];
  16586. var curve = new THREE.CubicBezierCurve( new THREE.Vector2( x0, y0 ),
  16587. new THREE.Vector2( aCP1x, aCP1y ),
  16588. new THREE.Vector2( aCP2x, aCP2y ),
  16589. new THREE.Vector2( aX, aY ) );
  16590. this.curves.push( curve );
  16591. this.actions.push( { action: THREE.PathActions.BEZIER_CURVE_TO, args: args } );
  16592. };
  16593. THREE.Path.prototype.splineThru = function( pts /*Array of Vector*/ ) {
  16594. var args = Array.prototype.slice.call( arguments );
  16595. var lastargs = this.actions[ this.actions.length - 1 ].args;
  16596. var x0 = lastargs[ lastargs.length - 2 ];
  16597. var y0 = lastargs[ lastargs.length - 1 ];
  16598. //---
  16599. var npts = [ new THREE.Vector2( x0, y0 ) ];
  16600. Array.prototype.push.apply( npts, pts );
  16601. var curve = new THREE.SplineCurve( npts );
  16602. this.curves.push( curve );
  16603. this.actions.push( { action: THREE.PathActions.CSPLINE_THRU, args: args } );
  16604. };
  16605. // FUTURE: Change the API or follow canvas API?
  16606. THREE.Path.prototype.arc = function ( aX, aY, aRadius,
  16607. aStartAngle, aEndAngle, aClockwise ) {
  16608. var lastargs = this.actions[ this.actions.length - 1].args;
  16609. var x0 = lastargs[ lastargs.length - 2 ];
  16610. var y0 = lastargs[ lastargs.length - 1 ];
  16611. this.absarc(aX + x0, aY + y0, aRadius,
  16612. aStartAngle, aEndAngle, aClockwise );
  16613. };
  16614. THREE.Path.prototype.absarc = function ( aX, aY, aRadius,
  16615. aStartAngle, aEndAngle, aClockwise ) {
  16616. this.absellipse(aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise);
  16617. };
  16618. THREE.Path.prototype.ellipse = function ( aX, aY, xRadius, yRadius,
  16619. aStartAngle, aEndAngle, aClockwise ) {
  16620. var lastargs = this.actions[ this.actions.length - 1].args;
  16621. var x0 = lastargs[ lastargs.length - 2 ];
  16622. var y0 = lastargs[ lastargs.length - 1 ];
  16623. this.absellipse(aX + x0, aY + y0, xRadius, yRadius,
  16624. aStartAngle, aEndAngle, aClockwise );
  16625. };
  16626. THREE.Path.prototype.absellipse = function ( aX, aY, xRadius, yRadius,
  16627. aStartAngle, aEndAngle, aClockwise ) {
  16628. var args = Array.prototype.slice.call( arguments );
  16629. var curve = new THREE.EllipseCurve( aX, aY, xRadius, yRadius,
  16630. aStartAngle, aEndAngle, aClockwise );
  16631. this.curves.push( curve );
  16632. var lastPoint = curve.getPoint(1);
  16633. args.push(lastPoint.x);
  16634. args.push(lastPoint.y);
  16635. this.actions.push( { action: THREE.PathActions.ELLIPSE, args: args } );
  16636. };
  16637. THREE.Path.prototype.getSpacedPoints = function ( divisions, closedPath ) {
  16638. if ( ! divisions ) divisions = 40;
  16639. var points = [];
  16640. for ( var i = 0; i < divisions; i ++ ) {
  16641. points.push( this.getPoint( i / divisions ) );
  16642. //if( !this.getPoint( i / divisions ) ) throw "DIE";
  16643. }
  16644. // if ( closedPath ) {
  16645. //
  16646. // points.push( points[ 0 ] );
  16647. //
  16648. // }
  16649. return points;
  16650. };
  16651. /* Return an array of vectors based on contour of the path */
  16652. THREE.Path.prototype.getPoints = function( divisions, closedPath ) {
  16653. if (this.useSpacedPoints) {
  16654. console.log('tata');
  16655. return this.getSpacedPoints( divisions, closedPath );
  16656. }
  16657. divisions = divisions || 12;
  16658. var points = [];
  16659. var i, il, item, action, args;
  16660. var cpx, cpy, cpx2, cpy2, cpx1, cpy1, cpx0, cpy0,
  16661. laste, j,
  16662. t, tx, ty;
  16663. for ( i = 0, il = this.actions.length; i < il; i ++ ) {
  16664. item = this.actions[ i ];
  16665. action = item.action;
  16666. args = item.args;
  16667. switch( action ) {
  16668. case THREE.PathActions.MOVE_TO:
  16669. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  16670. break;
  16671. case THREE.PathActions.LINE_TO:
  16672. points.push( new THREE.Vector2( args[ 0 ], args[ 1 ] ) );
  16673. break;
  16674. case THREE.PathActions.QUADRATIC_CURVE_TO:
  16675. cpx = args[ 2 ];
  16676. cpy = args[ 3 ];
  16677. cpx1 = args[ 0 ];
  16678. cpy1 = args[ 1 ];
  16679. if ( points.length > 0 ) {
  16680. laste = points[ points.length - 1 ];
  16681. cpx0 = laste.x;
  16682. cpy0 = laste.y;
  16683. } else {
  16684. laste = this.actions[ i - 1 ].args;
  16685. cpx0 = laste[ laste.length - 2 ];
  16686. cpy0 = laste[ laste.length - 1 ];
  16687. }
  16688. for ( j = 1; j <= divisions; j ++ ) {
  16689. t = j / divisions;
  16690. tx = THREE.Shape.Utils.b2( t, cpx0, cpx1, cpx );
  16691. ty = THREE.Shape.Utils.b2( t, cpy0, cpy1, cpy );
  16692. points.push( new THREE.Vector2( tx, ty ) );
  16693. }
  16694. break;
  16695. case THREE.PathActions.BEZIER_CURVE_TO:
  16696. cpx = args[ 4 ];
  16697. cpy = args[ 5 ];
  16698. cpx1 = args[ 0 ];
  16699. cpy1 = args[ 1 ];
  16700. cpx2 = args[ 2 ];
  16701. cpy2 = args[ 3 ];
  16702. if ( points.length > 0 ) {
  16703. laste = points[ points.length - 1 ];
  16704. cpx0 = laste.x;
  16705. cpy0 = laste.y;
  16706. } else {
  16707. laste = this.actions[ i - 1 ].args;
  16708. cpx0 = laste[ laste.length - 2 ];
  16709. cpy0 = laste[ laste.length - 1 ];
  16710. }
  16711. for ( j = 1; j <= divisions; j ++ ) {
  16712. t = j / divisions;
  16713. tx = THREE.Shape.Utils.b3( t, cpx0, cpx1, cpx2, cpx );
  16714. ty = THREE.Shape.Utils.b3( t, cpy0, cpy1, cpy2, cpy );
  16715. points.push( new THREE.Vector2( tx, ty ) );
  16716. }
  16717. break;
  16718. case THREE.PathActions.CSPLINE_THRU:
  16719. laste = this.actions[ i - 1 ].args;
  16720. var last = new THREE.Vector2( laste[ laste.length - 2 ], laste[ laste.length - 1 ] );
  16721. var spts = [ last ];
  16722. var n = divisions * args[ 0 ].length;
  16723. spts = spts.concat( args[ 0 ] );
  16724. var spline = new THREE.SplineCurve( spts );
  16725. for ( j = 1; j <= n; j ++ ) {
  16726. points.push( spline.getPointAt( j / n ) ) ;
  16727. }
  16728. break;
  16729. case THREE.PathActions.ARC:
  16730. var aX = args[ 0 ], aY = args[ 1 ],
  16731. aRadius = args[ 2 ],
  16732. aStartAngle = args[ 3 ], aEndAngle = args[ 4 ],
  16733. aClockwise = !! args[ 5 ];
  16734. var deltaAngle = aEndAngle - aStartAngle;
  16735. var angle;
  16736. var tdivisions = divisions * 2;
  16737. for ( j = 1; j <= tdivisions; j ++ ) {
  16738. t = j / tdivisions;
  16739. if ( ! aClockwise ) {
  16740. t = 1 - t;
  16741. }
  16742. angle = aStartAngle + t * deltaAngle;
  16743. tx = aX + aRadius * Math.cos( angle );
  16744. ty = aY + aRadius * Math.sin( angle );
  16745. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  16746. points.push( new THREE.Vector2( tx, ty ) );
  16747. }
  16748. //console.log(points);
  16749. break;
  16750. case THREE.PathActions.ELLIPSE:
  16751. var aX = args[ 0 ], aY = args[ 1 ],
  16752. xRadius = args[ 2 ],
  16753. yRadius = args[ 3 ],
  16754. aStartAngle = args[ 4 ], aEndAngle = args[ 5 ],
  16755. aClockwise = !! args[ 6 ];
  16756. var deltaAngle = aEndAngle - aStartAngle;
  16757. var angle;
  16758. var tdivisions = divisions * 2;
  16759. for ( j = 1; j <= tdivisions; j ++ ) {
  16760. t = j / tdivisions;
  16761. if ( ! aClockwise ) {
  16762. t = 1 - t;
  16763. }
  16764. angle = aStartAngle + t * deltaAngle;
  16765. tx = aX + xRadius * Math.cos( angle );
  16766. ty = aY + yRadius * Math.sin( angle );
  16767. //console.log('t', t, 'angle', angle, 'tx', tx, 'ty', ty);
  16768. points.push( new THREE.Vector2( tx, ty ) );
  16769. }
  16770. //console.log(points);
  16771. break;
  16772. } // end switch
  16773. }
  16774. // Normalize to remove the closing point by default.
  16775. var lastPoint = points[ points.length - 1];
  16776. var EPSILON = 0.0000000001;
  16777. if ( Math.abs(lastPoint.x - points[ 0 ].x) < EPSILON &&
  16778. Math.abs(lastPoint.y - points[ 0 ].y) < EPSILON)
  16779. points.splice( points.length - 1, 1);
  16780. if ( closedPath ) {
  16781. points.push( points[ 0 ] );
  16782. }
  16783. return points;
  16784. };
  16785. //
  16786. // Breaks path into shapes
  16787. //
  16788. // Assumptions (if parameter isCCW==true the opposite holds):
  16789. // - solid shapes are defined clockwise (CW)
  16790. // - holes are defined counterclockwise (CCW)
  16791. //
  16792. // If parameter noHoles==true:
  16793. // - all subPaths are regarded as solid shapes
  16794. // - definition order CW/CCW has no relevance
  16795. //
  16796. THREE.Path.prototype.toShapes = function( isCCW, noHoles ) {
  16797. function extractSubpaths( inActions ) {
  16798. var i, il, item, action, args;
  16799. var subPaths = [], lastPath = new THREE.Path();
  16800. for ( i = 0, il = inActions.length; i < il; i ++ ) {
  16801. item = inActions[ i ];
  16802. args = item.args;
  16803. action = item.action;
  16804. if ( action == THREE.PathActions.MOVE_TO ) {
  16805. if ( lastPath.actions.length != 0 ) {
  16806. subPaths.push( lastPath );
  16807. lastPath = new THREE.Path();
  16808. }
  16809. }
  16810. lastPath[ action ].apply( lastPath, args );
  16811. }
  16812. if ( lastPath.actions.length != 0 ) {
  16813. subPaths.push( lastPath );
  16814. }
  16815. // console.log(subPaths);
  16816. return subPaths;
  16817. }
  16818. function toShapesNoHoles( inSubpaths ) {
  16819. var shapes = [];
  16820. for ( var i = 0, il = inSubpaths.length; i < il; i ++ ) {
  16821. var tmpPath = inSubpaths[ i ];
  16822. var tmpShape = new THREE.Shape();
  16823. tmpShape.actions = tmpPath.actions;
  16824. tmpShape.curves = tmpPath.curves;
  16825. shapes.push( tmpShape );
  16826. }
  16827. //console.log("shape", shapes);
  16828. return shapes;
  16829. };
  16830. function isPointInsidePolygon( inPt, inPolygon ) {
  16831. var EPSILON = 0.0000000001;
  16832. var polyLen = inPolygon.length;
  16833. // inPt on polygon contour => immediate success or
  16834. // toggling of inside/outside at every single! intersection point of an edge
  16835. // with the horizontal line through inPt, left of inPt
  16836. // not counting lowerY endpoints of edges and whole edges on that line
  16837. var inside = false;
  16838. for( var p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) {
  16839. var edgeLowPt = inPolygon[ p ];
  16840. var edgeHighPt = inPolygon[ q ];
  16841. var edgeDx = edgeHighPt.x - edgeLowPt.x;
  16842. var edgeDy = edgeHighPt.y - edgeLowPt.y;
  16843. if ( Math.abs(edgeDy) > EPSILON ) { // not parallel
  16844. if ( edgeDy < 0 ) {
  16845. edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx;
  16846. edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy;
  16847. }
  16848. if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue;
  16849. if ( inPt.y == edgeLowPt.y ) {
  16850. if ( inPt.x == edgeLowPt.x ) return true; // inPt is on contour ?
  16851. // continue; // no intersection or edgeLowPt => doesn't count !!!
  16852. } else {
  16853. var perpEdge = edgeDy * (inPt.x - edgeLowPt.x) - edgeDx * (inPt.y - edgeLowPt.y);
  16854. if ( perpEdge == 0 ) return true; // inPt is on contour ?
  16855. if ( perpEdge < 0 ) continue;
  16856. inside = ! inside; // true intersection left of inPt
  16857. }
  16858. } else { // parallel or colinear
  16859. if ( inPt.y != edgeLowPt.y ) continue; // parallel
  16860. // egde lies on the same horizontal line as inPt
  16861. if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) ||
  16862. ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour !
  16863. // continue;
  16864. }
  16865. }
  16866. return inside;
  16867. }
  16868. var subPaths = extractSubpaths( this.actions );
  16869. if ( subPaths.length == 0 ) return [];
  16870. if ( noHoles === true ) return toShapesNoHoles( subPaths );
  16871. var solid, tmpPath, tmpShape, shapes = [];
  16872. if ( subPaths.length == 1) {
  16873. tmpPath = subPaths[0];
  16874. tmpShape = new THREE.Shape();
  16875. tmpShape.actions = tmpPath.actions;
  16876. tmpShape.curves = tmpPath.curves;
  16877. shapes.push( tmpShape );
  16878. return shapes;
  16879. }
  16880. var holesFirst = ! THREE.Shape.Utils.isClockWise( subPaths[ 0 ].getPoints() );
  16881. holesFirst = isCCW ? ! holesFirst : holesFirst;
  16882. // console.log("Holes first", holesFirst);
  16883. var betterShapeHoles = [];
  16884. var newShapes = [];
  16885. var newShapeHoles = [];
  16886. var mainIdx = 0;
  16887. var tmpPoints;
  16888. newShapes[mainIdx] = undefined;
  16889. newShapeHoles[mainIdx] = [];
  16890. var i, il;
  16891. for ( i = 0, il = subPaths.length; i < il; i ++ ) {
  16892. tmpPath = subPaths[ i ];
  16893. tmpPoints = tmpPath.getPoints();
  16894. solid = THREE.Shape.Utils.isClockWise( tmpPoints );
  16895. solid = isCCW ? ! solid : solid;
  16896. if ( solid ) {
  16897. if ( (! holesFirst ) && ( newShapes[mainIdx] ) ) mainIdx ++;
  16898. newShapes[mainIdx] = { s: new THREE.Shape(), p: tmpPoints };
  16899. newShapes[mainIdx].s.actions = tmpPath.actions;
  16900. newShapes[mainIdx].s.curves = tmpPath.curves;
  16901. if ( holesFirst ) mainIdx ++;
  16902. newShapeHoles[mainIdx] = [];
  16903. //console.log('cw', i);
  16904. } else {
  16905. newShapeHoles[mainIdx].push( { h: tmpPath, p: tmpPoints[0] } );
  16906. //console.log('ccw', i);
  16907. }
  16908. }
  16909. // only Holes? -> probably all Shapes with wrong orientation
  16910. if ( ! newShapes[0] ) return toShapesNoHoles( subPaths );
  16911. if ( newShapes.length > 1 ) {
  16912. var ambigious = false;
  16913. var toChange = [];
  16914. for (var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
  16915. betterShapeHoles[sIdx] = [];
  16916. }
  16917. for (var sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) {
  16918. var sh = newShapes[sIdx];
  16919. var sho = newShapeHoles[sIdx];
  16920. for (var hIdx = 0; hIdx < sho.length; hIdx ++ ) {
  16921. var ho = sho[hIdx];
  16922. var hole_unassigned = true;
  16923. for (var s2Idx = 0; s2Idx < newShapes.length; s2Idx ++ ) {
  16924. if ( isPointInsidePolygon( ho.p, newShapes[s2Idx].p ) ) {
  16925. if ( sIdx != s2Idx ) toChange.push( { froms: sIdx, tos: s2Idx, hole: hIdx } );
  16926. if ( hole_unassigned ) {
  16927. hole_unassigned = false;
  16928. betterShapeHoles[s2Idx].push( ho );
  16929. } else {
  16930. ambigious = true;
  16931. }
  16932. }
  16933. }
  16934. if ( hole_unassigned ) { betterShapeHoles[sIdx].push( ho ); }
  16935. }
  16936. }
  16937. // console.log("ambigious: ", ambigious);
  16938. if ( toChange.length > 0 ) {
  16939. // console.log("to change: ", toChange);
  16940. if (! ambigious) newShapeHoles = betterShapeHoles;
  16941. }
  16942. }
  16943. var tmpHoles, j, jl;
  16944. for ( i = 0, il = newShapes.length; i < il; i ++ ) {
  16945. tmpShape = newShapes[i].s;
  16946. shapes.push( tmpShape );
  16947. tmpHoles = newShapeHoles[i];
  16948. for ( j = 0, jl = tmpHoles.length; j < jl; j ++ ) {
  16949. tmpShape.holes.push( tmpHoles[j].h );
  16950. }
  16951. }
  16952. //console.log("shape", shapes);
  16953. return shapes;
  16954. };
  16955. // File:src/extras/core/Shape.js
  16956. /**
  16957. * @author zz85 / http://www.lab4games.net/zz85/blog
  16958. * Defines a 2d shape plane using paths.
  16959. **/
  16960. // STEP 1 Create a path.
  16961. // STEP 2 Turn path into shape.
  16962. // STEP 3 ExtrudeGeometry takes in Shape/Shapes
  16963. // STEP 3a - Extract points from each shape, turn to vertices
  16964. // STEP 3b - Triangulate each shape, add faces.
  16965. THREE.Shape = function () {
  16966. THREE.Path.apply( this, arguments );
  16967. this.holes = [];
  16968. };
  16969. THREE.Shape.prototype = Object.create( THREE.Path.prototype );
  16970. THREE.Shape.prototype.constructor = THREE.Shape;
  16971. // Convenience method to return ExtrudeGeometry
  16972. THREE.Shape.prototype.extrude = function ( options ) {
  16973. var extruded = new THREE.ExtrudeGeometry( this, options );
  16974. return extruded;
  16975. };
  16976. // Convenience method to return ShapeGeometry
  16977. THREE.Shape.prototype.makeGeometry = function ( options ) {
  16978. var geometry = new THREE.ShapeGeometry( this, options );
  16979. return geometry;
  16980. };
  16981. // Get points of holes
  16982. THREE.Shape.prototype.getPointsHoles = function ( divisions ) {
  16983. var i, il = this.holes.length, holesPts = [];
  16984. for ( i = 0; i < il; i ++ ) {
  16985. holesPts[ i ] = this.holes[ i ].getTransformedPoints( divisions, this.bends );
  16986. }
  16987. return holesPts;
  16988. };
  16989. // Get points of holes (spaced by regular distance)
  16990. THREE.Shape.prototype.getSpacedPointsHoles = function ( divisions ) {
  16991. var i, il = this.holes.length, holesPts = [];
  16992. for ( i = 0; i < il; i ++ ) {
  16993. holesPts[ i ] = this.holes[ i ].getTransformedSpacedPoints( divisions, this.bends );
  16994. }
  16995. return holesPts;
  16996. };
  16997. // Get points of shape and holes (keypoints based on segments parameter)
  16998. THREE.Shape.prototype.extractAllPoints = function ( divisions ) {
  16999. return {
  17000. shape: this.getTransformedPoints( divisions ),
  17001. holes: this.getPointsHoles( divisions )
  17002. };
  17003. };
  17004. THREE.Shape.prototype.extractPoints = function ( divisions ) {
  17005. if (this.useSpacedPoints) {
  17006. return this.extractAllSpacedPoints(divisions);
  17007. }
  17008. return this.extractAllPoints(divisions);
  17009. };
  17010. //
  17011. // THREE.Shape.prototype.extractAllPointsWithBend = function ( divisions, bend ) {
  17012. //
  17013. // return {
  17014. //
  17015. // shape: this.transform( bend, divisions ),
  17016. // holes: this.getPointsHoles( divisions, bend )
  17017. //
  17018. // };
  17019. //
  17020. // };
  17021. // Get points of shape and holes (spaced by regular distance)
  17022. THREE.Shape.prototype.extractAllSpacedPoints = function ( divisions ) {
  17023. return {
  17024. shape: this.getTransformedSpacedPoints( divisions ),
  17025. holes: this.getSpacedPointsHoles( divisions )
  17026. };
  17027. };
  17028. /**************************************************************
  17029. * Utils
  17030. **************************************************************/
  17031. THREE.Shape.Utils = {
  17032. triangulateShape: function ( contour, holes ) {
  17033. function point_in_segment_2D_colin( inSegPt1, inSegPt2, inOtherPt ) {
  17034. // inOtherPt needs to be colinear to the inSegment
  17035. if ( inSegPt1.x != inSegPt2.x ) {
  17036. if ( inSegPt1.x < inSegPt2.x ) {
  17037. return ( ( inSegPt1.x <= inOtherPt.x ) && ( inOtherPt.x <= inSegPt2.x ) );
  17038. } else {
  17039. return ( ( inSegPt2.x <= inOtherPt.x ) && ( inOtherPt.x <= inSegPt1.x ) );
  17040. }
  17041. } else {
  17042. if ( inSegPt1.y < inSegPt2.y ) {
  17043. return ( ( inSegPt1.y <= inOtherPt.y ) && ( inOtherPt.y <= inSegPt2.y ) );
  17044. } else {
  17045. return ( ( inSegPt2.y <= inOtherPt.y ) && ( inOtherPt.y <= inSegPt1.y ) );
  17046. }
  17047. }
  17048. }
  17049. function intersect_segments_2D( inSeg1Pt1, inSeg1Pt2, inSeg2Pt1, inSeg2Pt2, inExcludeAdjacentSegs ) {
  17050. var EPSILON = 0.0000000001;
  17051. var seg1dx = inSeg1Pt2.x - inSeg1Pt1.x, seg1dy = inSeg1Pt2.y - inSeg1Pt1.y;
  17052. var seg2dx = inSeg2Pt2.x - inSeg2Pt1.x, seg2dy = inSeg2Pt2.y - inSeg2Pt1.y;
  17053. var seg1seg2dx = inSeg1Pt1.x - inSeg2Pt1.x;
  17054. var seg1seg2dy = inSeg1Pt1.y - inSeg2Pt1.y;
  17055. var limit = seg1dy * seg2dx - seg1dx * seg2dy;
  17056. var perpSeg1 = seg1dy * seg1seg2dx - seg1dx * seg1seg2dy;
  17057. if ( Math.abs(limit) > EPSILON ) { // not parallel
  17058. var perpSeg2;
  17059. if ( limit > 0 ) {
  17060. if ( ( perpSeg1 < 0 ) || ( perpSeg1 > limit ) ) return [];
  17061. perpSeg2 = seg2dy * seg1seg2dx - seg2dx * seg1seg2dy;
  17062. if ( ( perpSeg2 < 0 ) || ( perpSeg2 > limit ) ) return [];
  17063. } else {
  17064. if ( ( perpSeg1 > 0 ) || ( perpSeg1 < limit ) ) return [];
  17065. perpSeg2 = seg2dy * seg1seg2dx - seg2dx * seg1seg2dy;
  17066. if ( ( perpSeg2 > 0 ) || ( perpSeg2 < limit ) ) return [];
  17067. }
  17068. // i.e. to reduce rounding errors
  17069. // intersection at endpoint of segment#1?
  17070. if ( perpSeg2 == 0 ) {
  17071. if ( ( inExcludeAdjacentSegs ) &&
  17072. ( ( perpSeg1 == 0 ) || ( perpSeg1 == limit ) ) ) return [];
  17073. return [ inSeg1Pt1 ];
  17074. }
  17075. if ( perpSeg2 == limit ) {
  17076. if ( ( inExcludeAdjacentSegs ) &&
  17077. ( ( perpSeg1 == 0 ) || ( perpSeg1 == limit ) ) ) return [];
  17078. return [ inSeg1Pt2 ];
  17079. }
  17080. // intersection at endpoint of segment#2?
  17081. if ( perpSeg1 == 0 ) return [ inSeg2Pt1 ];
  17082. if ( perpSeg1 == limit ) return [ inSeg2Pt2 ];
  17083. // return real intersection point
  17084. var factorSeg1 = perpSeg2 / limit;
  17085. return [ { x: inSeg1Pt1.x + factorSeg1 * seg1dx,
  17086. y: inSeg1Pt1.y + factorSeg1 * seg1dy } ];
  17087. } else { // parallel or colinear
  17088. if ( ( perpSeg1 != 0 ) ||
  17089. ( seg2dy * seg1seg2dx != seg2dx * seg1seg2dy ) ) return [];
  17090. // they are collinear or degenerate
  17091. var seg1Pt = ( (seg1dx == 0) && (seg1dy == 0) ); // segment1 ist just a point?
  17092. var seg2Pt = ( (seg2dx == 0) && (seg2dy == 0) ); // segment2 ist just a point?
  17093. // both segments are points
  17094. if ( seg1Pt && seg2Pt ) {
  17095. if ( (inSeg1Pt1.x != inSeg2Pt1.x) ||
  17096. (inSeg1Pt1.y != inSeg2Pt1.y) ) return []; // they are distinct points
  17097. return [ inSeg1Pt1 ]; // they are the same point
  17098. }
  17099. // segment#1 is a single point
  17100. if ( seg1Pt ) {
  17101. if (! point_in_segment_2D_colin( inSeg2Pt1, inSeg2Pt2, inSeg1Pt1 ) ) return []; // but not in segment#2
  17102. return [ inSeg1Pt1 ];
  17103. }
  17104. // segment#2 is a single point
  17105. if ( seg2Pt ) {
  17106. if (! point_in_segment_2D_colin( inSeg1Pt1, inSeg1Pt2, inSeg2Pt1 ) ) return []; // but not in segment#1
  17107. return [ inSeg2Pt1 ];
  17108. }
  17109. // they are collinear segments, which might overlap
  17110. var seg1min, seg1max, seg1minVal, seg1maxVal;
  17111. var seg2min, seg2max, seg2minVal, seg2maxVal;
  17112. if (seg1dx != 0) { // the segments are NOT on a vertical line
  17113. if ( inSeg1Pt1.x < inSeg1Pt2.x ) {
  17114. seg1min = inSeg1Pt1; seg1minVal = inSeg1Pt1.x;
  17115. seg1max = inSeg1Pt2; seg1maxVal = inSeg1Pt2.x;
  17116. } else {
  17117. seg1min = inSeg1Pt2; seg1minVal = inSeg1Pt2.x;
  17118. seg1max = inSeg1Pt1; seg1maxVal = inSeg1Pt1.x;
  17119. }
  17120. if ( inSeg2Pt1.x < inSeg2Pt2.x ) {
  17121. seg2min = inSeg2Pt1; seg2minVal = inSeg2Pt1.x;
  17122. seg2max = inSeg2Pt2; seg2maxVal = inSeg2Pt2.x;
  17123. } else {
  17124. seg2min = inSeg2Pt2; seg2minVal = inSeg2Pt2.x;
  17125. seg2max = inSeg2Pt1; seg2maxVal = inSeg2Pt1.x;
  17126. }
  17127. } else { // the segments are on a vertical line
  17128. if ( inSeg1Pt1.y < inSeg1Pt2.y ) {
  17129. seg1min = inSeg1Pt1; seg1minVal = inSeg1Pt1.y;
  17130. seg1max = inSeg1Pt2; seg1maxVal = inSeg1Pt2.y;
  17131. } else {
  17132. seg1min = inSeg1Pt2; seg1minVal = inSeg1Pt2.y;
  17133. seg1max = inSeg1Pt1; seg1maxVal = inSeg1Pt1.y;
  17134. }
  17135. if ( inSeg2Pt1.y < inSeg2Pt2.y ) {
  17136. seg2min = inSeg2Pt1; seg2minVal = inSeg2Pt1.y;
  17137. seg2max = inSeg2Pt2; seg2maxVal = inSeg2Pt2.y;
  17138. } else {
  17139. seg2min = inSeg2Pt2; seg2minVal = inSeg2Pt2.y;
  17140. seg2max = inSeg2Pt1; seg2maxVal = inSeg2Pt1.y;
  17141. }
  17142. }
  17143. if ( seg1minVal <= seg2minVal ) {
  17144. if ( seg1maxVal < seg2minVal ) return [];
  17145. if ( seg1maxVal == seg2minVal ) {
  17146. if ( inExcludeAdjacentSegs ) return [];
  17147. return [ seg2min ];
  17148. }
  17149. if ( seg1maxVal <= seg2maxVal ) return [ seg2min, seg1max ];
  17150. return [ seg2min, seg2max ];
  17151. } else {
  17152. if ( seg1minVal > seg2maxVal ) return [];
  17153. if ( seg1minVal == seg2maxVal ) {
  17154. if ( inExcludeAdjacentSegs ) return [];
  17155. return [ seg1min ];
  17156. }
  17157. if ( seg1maxVal <= seg2maxVal ) return [ seg1min, seg1max ];
  17158. return [ seg1min, seg2max ];
  17159. }
  17160. }
  17161. }
  17162. function isPointInsideAngle( inVertex, inLegFromPt, inLegToPt, inOtherPt ) {
  17163. // The order of legs is important
  17164. var EPSILON = 0.0000000001;
  17165. // translation of all points, so that Vertex is at (0,0)
  17166. var legFromPtX = inLegFromPt.x - inVertex.x, legFromPtY = inLegFromPt.y - inVertex.y;
  17167. var legToPtX = inLegToPt.x - inVertex.x, legToPtY = inLegToPt.y - inVertex.y;
  17168. var otherPtX = inOtherPt.x - inVertex.x, otherPtY = inOtherPt.y - inVertex.y;
  17169. // main angle >0: < 180 deg.; 0: 180 deg.; <0: > 180 deg.
  17170. var from2toAngle = legFromPtX * legToPtY - legFromPtY * legToPtX;
  17171. var from2otherAngle = legFromPtX * otherPtY - legFromPtY * otherPtX;
  17172. if ( Math.abs(from2toAngle) > EPSILON ) { // angle != 180 deg.
  17173. var other2toAngle = otherPtX * legToPtY - otherPtY * legToPtX;
  17174. // console.log( "from2to: " + from2toAngle + ", from2other: " + from2otherAngle + ", other2to: " + other2toAngle );
  17175. if ( from2toAngle > 0 ) { // main angle < 180 deg.
  17176. return ( ( from2otherAngle >= 0 ) && ( other2toAngle >= 0 ) );
  17177. } else { // main angle > 180 deg.
  17178. return ( ( from2otherAngle >= 0 ) || ( other2toAngle >= 0 ) );
  17179. }
  17180. } else { // angle == 180 deg.
  17181. // console.log( "from2to: 180 deg., from2other: " + from2otherAngle );
  17182. return ( from2otherAngle > 0 );
  17183. }
  17184. }
  17185. function removeHoles( contour, holes ) {
  17186. var shape = contour.concat(); // work on this shape
  17187. var hole;
  17188. function isCutLineInsideAngles( inShapeIdx, inHoleIdx ) {
  17189. // Check if hole point lies within angle around shape point
  17190. var lastShapeIdx = shape.length - 1;
  17191. var prevShapeIdx = inShapeIdx - 1;
  17192. if ( prevShapeIdx < 0 ) prevShapeIdx = lastShapeIdx;
  17193. var nextShapeIdx = inShapeIdx + 1;
  17194. if ( nextShapeIdx > lastShapeIdx ) nextShapeIdx = 0;
  17195. var insideAngle = isPointInsideAngle( shape[inShapeIdx], shape[ prevShapeIdx ], shape[ nextShapeIdx ], hole[inHoleIdx] );
  17196. if (! insideAngle ) {
  17197. // console.log( "Vertex (Shape): " + inShapeIdx + ", Point: " + hole[inHoleIdx].x + "/" + hole[inHoleIdx].y );
  17198. return false;
  17199. }
  17200. // Check if shape point lies within angle around hole point
  17201. var lastHoleIdx = hole.length - 1;
  17202. var prevHoleIdx = inHoleIdx - 1;
  17203. if ( prevHoleIdx < 0 ) prevHoleIdx = lastHoleIdx;
  17204. var nextHoleIdx = inHoleIdx + 1;
  17205. if ( nextHoleIdx > lastHoleIdx ) nextHoleIdx = 0;
  17206. insideAngle = isPointInsideAngle( hole[inHoleIdx], hole[ prevHoleIdx ], hole[ nextHoleIdx ], shape[inShapeIdx] );
  17207. if (! insideAngle ) {
  17208. // console.log( "Vertex (Hole): " + inHoleIdx + ", Point: " + shape[inShapeIdx].x + "/" + shape[inShapeIdx].y );
  17209. return false;
  17210. }
  17211. return true;
  17212. }
  17213. function intersectsShapeEdge( inShapePt, inHolePt ) {
  17214. // checks for intersections with shape edges
  17215. var sIdx, nextIdx, intersection;
  17216. for ( sIdx = 0; sIdx < shape.length; sIdx ++ ) {
  17217. nextIdx = sIdx+1; nextIdx %= shape.length;
  17218. intersection = intersect_segments_2D( inShapePt, inHolePt, shape[sIdx], shape[nextIdx], true );
  17219. if ( intersection.length > 0 ) return true;
  17220. }
  17221. return false;
  17222. }
  17223. var indepHoles = [];
  17224. function intersectsHoleEdge( inShapePt, inHolePt ) {
  17225. // checks for intersections with hole edges
  17226. var ihIdx, chkHole,
  17227. hIdx, nextIdx, intersection;
  17228. for ( ihIdx = 0; ihIdx < indepHoles.length; ihIdx ++ ) {
  17229. chkHole = holes[indepHoles[ihIdx]];
  17230. for ( hIdx = 0; hIdx < chkHole.length; hIdx ++ ) {
  17231. nextIdx = hIdx+1; nextIdx %= chkHole.length;
  17232. intersection = intersect_segments_2D( inShapePt, inHolePt, chkHole[hIdx], chkHole[nextIdx], true );
  17233. if ( intersection.length > 0 ) return true;
  17234. }
  17235. }
  17236. return false;
  17237. }
  17238. var holeIndex, shapeIndex,
  17239. shapePt, holePt,
  17240. holeIdx, cutKey, failedCuts = [],
  17241. tmpShape1, tmpShape2,
  17242. tmpHole1, tmpHole2;
  17243. for ( var h = 0, hl = holes.length; h < hl; h ++ ) {
  17244. indepHoles.push( h );
  17245. }
  17246. var minShapeIndex = 0;
  17247. var counter = indepHoles.length * 2;
  17248. while ( indepHoles.length > 0 ) {
  17249. counter --;
  17250. if ( counter < 0 ) {
  17251. console.log( "Infinite Loop! Holes left:" + indepHoles.length + ", Probably Hole outside Shape!" );
  17252. break;
  17253. }
  17254. // search for shape-vertex and hole-vertex,
  17255. // which can be connected without intersections
  17256. for ( shapeIndex = minShapeIndex; shapeIndex < shape.length; shapeIndex ++ ) {
  17257. shapePt = shape[ shapeIndex ];
  17258. holeIndex = - 1;
  17259. // search for hole which can be reached without intersections
  17260. for ( var h = 0; h < indepHoles.length; h ++ ) {
  17261. holeIdx = indepHoles[h];
  17262. // prevent multiple checks
  17263. cutKey = shapePt.x + ":" + shapePt.y + ":" + holeIdx;
  17264. if ( failedCuts[cutKey] !== undefined ) continue;
  17265. hole = holes[holeIdx];
  17266. for ( var h2 = 0; h2 < hole.length; h2 ++ ) {
  17267. holePt = hole[ h2 ];
  17268. if (! isCutLineInsideAngles( shapeIndex, h2 ) ) continue;
  17269. if ( intersectsShapeEdge( shapePt, holePt ) ) continue;
  17270. if ( intersectsHoleEdge( shapePt, holePt ) ) continue;
  17271. holeIndex = h2;
  17272. indepHoles.splice(h,1);
  17273. tmpShape1 = shape.slice( 0, shapeIndex+1 );
  17274. tmpShape2 = shape.slice( shapeIndex );
  17275. tmpHole1 = hole.slice( holeIndex );
  17276. tmpHole2 = hole.slice( 0, holeIndex+1 );
  17277. shape = tmpShape1.concat( tmpHole1 ).concat( tmpHole2 ).concat( tmpShape2 );
  17278. minShapeIndex = shapeIndex;
  17279. // Debug only, to show the selected cuts
  17280. // glob_CutLines.push( [ shapePt, holePt ] );
  17281. break;
  17282. }
  17283. if ( holeIndex >= 0 ) break; // hole-vertex found
  17284. failedCuts[cutKey] = true; // remember failure
  17285. }
  17286. if ( holeIndex >= 0 ) break; // hole-vertex found
  17287. }
  17288. }
  17289. return shape; /* shape with no holes */
  17290. }
  17291. var i, il, f, face,
  17292. key, index,
  17293. allPointsMap = {};
  17294. // To maintain reference to old shape, one must match coordinates, or offset the indices from original arrays. It's probably easier to do the first.
  17295. var allpoints = contour.concat();
  17296. for ( var h = 0, hl = holes.length; h < hl; h ++ ) {
  17297. Array.prototype.push.apply( allpoints, holes[h] );
  17298. }
  17299. //console.log( "allpoints",allpoints, allpoints.length );
  17300. // prepare all points map
  17301. for ( i = 0, il = allpoints.length; i < il; i ++ ) {
  17302. key = allpoints[ i ].x + ":" + allpoints[ i ].y;
  17303. if ( allPointsMap[ key ] !== undefined ) {
  17304. console.log( "Duplicate point", key );
  17305. }
  17306. allPointsMap[ key ] = i;
  17307. }
  17308. // remove holes by cutting paths to holes and adding them to the shape
  17309. var shapeWithoutHoles = removeHoles( contour, holes );
  17310. var triangles = THREE.FontUtils.Triangulate( shapeWithoutHoles, false ); // True returns indices for points of spooled shape
  17311. //console.log( "triangles",triangles, triangles.length );
  17312. // check all face vertices against all points map
  17313. for ( i = 0, il = triangles.length; i < il; i ++ ) {
  17314. face = triangles[ i ];
  17315. for ( f = 0; f < 3; f ++ ) {
  17316. key = face[ f ].x + ":" + face[ f ].y;
  17317. index = allPointsMap[ key ];
  17318. if ( index !== undefined ) {
  17319. face[ f ] = index;
  17320. }
  17321. }
  17322. }
  17323. return triangles.concat();
  17324. },
  17325. isClockWise: function ( pts ) {
  17326. return THREE.FontUtils.Triangulate.area( pts ) < 0;
  17327. },
  17328. // Bezier Curves formulas obtained from
  17329. // http://en.wikipedia.org/wiki/B%C3%A9zier_curve
  17330. // Quad Bezier Functions
  17331. b2p0: function ( t, p ) {
  17332. var k = 1 - t;
  17333. return k * k * p;
  17334. },
  17335. b2p1: function ( t, p ) {
  17336. return 2 * ( 1 - t ) * t * p;
  17337. },
  17338. b2p2: function ( t, p ) {
  17339. return t * t * p;
  17340. },
  17341. b2: function ( t, p0, p1, p2 ) {
  17342. return this.b2p0( t, p0 ) + this.b2p1( t, p1 ) + this.b2p2( t, p2 );
  17343. },
  17344. // Cubic Bezier Functions
  17345. b3p0: function ( t, p ) {
  17346. var k = 1 - t;
  17347. return k * k * k * p;
  17348. },
  17349. b3p1: function ( t, p ) {
  17350. var k = 1 - t;
  17351. return 3 * k * k * t * p;
  17352. },
  17353. b3p2: function ( t, p ) {
  17354. var k = 1 - t;
  17355. return 3 * k * t * t * p;
  17356. },
  17357. b3p3: function ( t, p ) {
  17358. return t * t * t * p;
  17359. },
  17360. b3: function ( t, p0, p1, p2, p3 ) {
  17361. return this.b3p0( t, p0 ) + this.b3p1( t, p1 ) + this.b3p2( t, p2 ) + this.b3p3( t, p3 );
  17362. }
  17363. };
  17364. // File:src/extras/curves/LineCurve.js
  17365. /**************************************************************
  17366. * Line
  17367. **************************************************************/
  17368. THREE.LineCurve = function ( v1, v2 ) {
  17369. this.v1 = v1;
  17370. this.v2 = v2;
  17371. };
  17372. THREE.LineCurve.prototype = Object.create( THREE.Curve.prototype );
  17373. THREE.LineCurve.prototype.constructor = THREE.LineCurve;
  17374. THREE.LineCurve.prototype.getPoint = function ( t ) {
  17375. var point = this.v2.clone().sub(this.v1);
  17376. point.multiplyScalar( t ).add( this.v1 );
  17377. return point;
  17378. };
  17379. // Line curve is linear, so we can overwrite default getPointAt
  17380. THREE.LineCurve.prototype.getPointAt = function ( u ) {
  17381. return this.getPoint( u );
  17382. };
  17383. THREE.LineCurve.prototype.getTangent = function( t ) {
  17384. var tangent = this.v2.clone().sub(this.v1);
  17385. return tangent.normalize();
  17386. };
  17387. // File:src/extras/curves/QuadraticBezierCurve.js
  17388. /**************************************************************
  17389. * Quadratic Bezier curve
  17390. **************************************************************/
  17391. THREE.QuadraticBezierCurve = function ( v0, v1, v2 ) {
  17392. this.v0 = v0;
  17393. this.v1 = v1;
  17394. this.v2 = v2;
  17395. };
  17396. THREE.QuadraticBezierCurve.prototype = Object.create( THREE.Curve.prototype );
  17397. THREE.QuadraticBezierCurve.prototype.constructor = THREE.QuadraticBezierCurve;
  17398. THREE.QuadraticBezierCurve.prototype.getPoint = function ( t ) {
  17399. var vector = new THREE.Vector2();
  17400. vector.x = THREE.Shape.Utils.b2( t, this.v0.x, this.v1.x, this.v2.x );
  17401. vector.y = THREE.Shape.Utils.b2( t, this.v0.y, this.v1.y, this.v2.y );
  17402. return vector;
  17403. };
  17404. THREE.QuadraticBezierCurve.prototype.getTangent = function( t ) {
  17405. var vector = new THREE.Vector2();
  17406. vector.x = THREE.Curve.Utils.tangentQuadraticBezier( t, this.v0.x, this.v1.x, this.v2.x );
  17407. vector.y = THREE.Curve.Utils.tangentQuadraticBezier( t, this.v0.y, this.v1.y, this.v2.y );
  17408. // returns unit vector
  17409. return vector.normalize();
  17410. };
  17411. // File:src/extras/curves/CubicBezierCurve.js
  17412. /**************************************************************
  17413. * Cubic Bezier curve
  17414. **************************************************************/
  17415. THREE.CubicBezierCurve = function ( v0, v1, v2, v3 ) {
  17416. this.v0 = v0;
  17417. this.v1 = v1;
  17418. this.v2 = v2;
  17419. this.v3 = v3;
  17420. };
  17421. THREE.CubicBezierCurve.prototype = Object.create( THREE.Curve.prototype );
  17422. THREE.CubicBezierCurve.prototype.constructor = THREE.CubicBezierCurve;
  17423. THREE.CubicBezierCurve.prototype.getPoint = function ( t ) {
  17424. var tx, ty;
  17425. tx = THREE.Shape.Utils.b3( t, this.v0.x, this.v1.x, this.v2.x, this.v3.x );
  17426. ty = THREE.Shape.Utils.b3( t, this.v0.y, this.v1.y, this.v2.y, this.v3.y );
  17427. return new THREE.Vector2( tx, ty );
  17428. };
  17429. THREE.CubicBezierCurve.prototype.getTangent = function( t ) {
  17430. var tx, ty;
  17431. tx = THREE.Curve.Utils.tangentCubicBezier( t, this.v0.x, this.v1.x, this.v2.x, this.v3.x );
  17432. ty = THREE.Curve.Utils.tangentCubicBezier( t, this.v0.y, this.v1.y, this.v2.y, this.v3.y );
  17433. var tangent = new THREE.Vector2( tx, ty );
  17434. tangent.normalize();
  17435. return tangent;
  17436. };
  17437. // File:src/extras/curves/SplineCurve.js
  17438. /**************************************************************
  17439. * Spline curve
  17440. **************************************************************/
  17441. THREE.SplineCurve = function ( points /* array of Vector2 */ ) {
  17442. this.points = ( points == undefined ) ? [] : points;
  17443. };
  17444. THREE.SplineCurve.prototype = Object.create( THREE.Curve.prototype );
  17445. THREE.SplineCurve.prototype.constructor = THREE.SplineCurve;
  17446. THREE.SplineCurve.prototype.getPoint = function ( t ) {
  17447. var points = this.points;
  17448. var point = ( points.length - 1 ) * t;
  17449. var intPoint = Math.floor( point );
  17450. var weight = point - intPoint;
  17451. var point0 = points[ intPoint == 0 ? intPoint : intPoint - 1 ]
  17452. var point1 = points[ intPoint ]
  17453. var point2 = points[ intPoint > points.length - 2 ? points.length -1 : intPoint + 1 ]
  17454. var point3 = points[ intPoint > points.length - 3 ? points.length -1 : intPoint + 2 ]
  17455. var vector = new THREE.Vector2();
  17456. vector.x = THREE.Curve.Utils.interpolate( point0.x, point1.x, point2.x, point3.x, weight );
  17457. vector.y = THREE.Curve.Utils.interpolate( point0.y, point1.y, point2.y, point3.y, weight );
  17458. return vector;
  17459. };
  17460. // File:src/extras/curves/EllipseCurve.js
  17461. /**************************************************************
  17462. * Ellipse curve
  17463. **************************************************************/
  17464. THREE.EllipseCurve = function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise ) {
  17465. this.aX = aX;
  17466. this.aY = aY;
  17467. this.xRadius = xRadius;
  17468. this.yRadius = yRadius;
  17469. this.aStartAngle = aStartAngle;
  17470. this.aEndAngle = aEndAngle;
  17471. this.aClockwise = aClockwise;
  17472. };
  17473. THREE.EllipseCurve.prototype = Object.create( THREE.Curve.prototype );
  17474. THREE.EllipseCurve.prototype.constructor = THREE.EllipseCurve;
  17475. THREE.EllipseCurve.prototype.getPoint = function ( t ) {
  17476. var deltaAngle = this.aEndAngle - this.aStartAngle;
  17477. if ( deltaAngle < 0 ) deltaAngle += Math.PI * 2;
  17478. if ( deltaAngle > Math.PI * 2 ) deltaAngle -= Math.PI * 2;
  17479. var angle;
  17480. if ( this.aClockwise === true ) {
  17481. angle = this.aEndAngle + ( 1 - t ) * ( Math.PI * 2 - deltaAngle );
  17482. } else {
  17483. angle = this.aStartAngle + t * deltaAngle;
  17484. }
  17485. var vector = new THREE.Vector2();
  17486. vector.x = this.aX + this.xRadius * Math.cos( angle );
  17487. vector.y = this.aY + this.yRadius * Math.sin( angle );
  17488. return vector;
  17489. };
  17490. // File:src/extras/curves/ArcCurve.js
  17491. /**************************************************************
  17492. * Arc curve
  17493. **************************************************************/
  17494. THREE.ArcCurve = function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
  17495. THREE.EllipseCurve.call( this, aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
  17496. };
  17497. THREE.ArcCurve.prototype = Object.create( THREE.EllipseCurve.prototype );
  17498. THREE.ArcCurve.prototype.constructor = THREE.ArcCurve;
  17499. // File:src/extras/curves/LineCurve3.js
  17500. /**************************************************************
  17501. * Line3D
  17502. **************************************************************/
  17503. THREE.LineCurve3 = THREE.Curve.create(
  17504. function ( v1, v2 ) {
  17505. this.v1 = v1;
  17506. this.v2 = v2;
  17507. },
  17508. function ( t ) {
  17509. var vector = new THREE.Vector3();
  17510. vector.subVectors( this.v2, this.v1 ); // diff
  17511. vector.multiplyScalar( t );
  17512. vector.add( this.v1 );
  17513. return vector;
  17514. }
  17515. );
  17516. // File:src/extras/curves/QuadraticBezierCurve3.js
  17517. /**************************************************************
  17518. * Quadratic Bezier 3D curve
  17519. **************************************************************/
  17520. THREE.QuadraticBezierCurve3 = THREE.Curve.create(
  17521. function ( v0, v1, v2 ) {
  17522. this.v0 = v0;
  17523. this.v1 = v1;
  17524. this.v2 = v2;
  17525. },
  17526. function ( t ) {
  17527. var vector = new THREE.Vector3();
  17528. vector.x = THREE.Shape.Utils.b2( t, this.v0.x, this.v1.x, this.v2.x );
  17529. vector.y = THREE.Shape.Utils.b2( t, this.v0.y, this.v1.y, this.v2.y );
  17530. vector.z = THREE.Shape.Utils.b2( t, this.v0.z, this.v1.z, this.v2.z );
  17531. return vector;
  17532. }
  17533. );
  17534. // File:src/extras/curves/CubicBezierCurve3.js
  17535. /**************************************************************
  17536. * Cubic Bezier 3D curve
  17537. **************************************************************/
  17538. THREE.CubicBezierCurve3 = THREE.Curve.create(
  17539. function ( v0, v1, v2, v3 ) {
  17540. this.v0 = v0;
  17541. this.v1 = v1;
  17542. this.v2 = v2;
  17543. this.v3 = v3;
  17544. },
  17545. function ( t ) {
  17546. var vector = new THREE.Vector3();
  17547. vector.x = THREE.Shape.Utils.b3( t, this.v0.x, this.v1.x, this.v2.x, this.v3.x );
  17548. vector.y = THREE.Shape.Utils.b3( t, this.v0.y, this.v1.y, this.v2.y, this.v3.y );
  17549. vector.z = THREE.Shape.Utils.b3( t, this.v0.z, this.v1.z, this.v2.z, this.v3.z );
  17550. return vector;
  17551. }
  17552. );
  17553. // File:src/extras/curves/SplineCurve3.js
  17554. /**************************************************************
  17555. * Spline 3D curve
  17556. **************************************************************/
  17557. THREE.SplineCurve3 = THREE.Curve.create(
  17558. function ( points /* array of Vector3 */) {
  17559. this.points = ( points == undefined ) ? [] : points;
  17560. },
  17561. function ( t ) {
  17562. var points = this.points;
  17563. var point = ( points.length - 1 ) * t;
  17564. var intPoint = Math.floor( point );
  17565. var weight = point - intPoint;
  17566. var point0 = points[ intPoint == 0 ? intPoint : intPoint - 1 ];
  17567. var point1 = points[ intPoint ];
  17568. var point2 = points[ intPoint > points.length - 2 ? points.length - 1 : intPoint + 1 ];
  17569. var point3 = points[ intPoint > points.length - 3 ? points.length - 1 : intPoint + 2 ];
  17570. var vector = new THREE.Vector3();
  17571. vector.x = THREE.Curve.Utils.interpolate( point0.x, point1.x, point2.x, point3.x, weight );
  17572. vector.y = THREE.Curve.Utils.interpolate( point0.y, point1.y, point2.y, point3.y, weight );
  17573. vector.z = THREE.Curve.Utils.interpolate( point0.z, point1.z, point2.z, point3.z, weight );
  17574. return vector;
  17575. }
  17576. );
  17577. // File:src/extras/curves/ClosedSplineCurve3.js
  17578. /**************************************************************
  17579. * Closed Spline 3D curve
  17580. **************************************************************/
  17581. THREE.ClosedSplineCurve3 = THREE.Curve.create(
  17582. function ( points /* array of Vector3 */) {
  17583. this.points = ( points == undefined ) ? [] : points;
  17584. },
  17585. function ( t ) {
  17586. var points = this.points;
  17587. var point = ( points.length - 0 ) * t; // This needs to be from 0-length +1
  17588. var intPoint = Math.floor( point );
  17589. var weight = point - intPoint;
  17590. intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / points.length ) + 1 ) * points.length;
  17591. var point0 = points[ ( intPoint - 1 ) % points.length ];
  17592. var point1 = points[ ( intPoint ) % points.length ];
  17593. var point2 = points[ ( intPoint + 1 ) % points.length ];
  17594. var point3 = points[ ( intPoint + 2 ) % points.length ];
  17595. var vector = new THREE.Vector3();
  17596. vector.x = THREE.Curve.Utils.interpolate( point0.x, point1.x, point2.x, point3.x, weight );
  17597. vector.y = THREE.Curve.Utils.interpolate( point0.y, point1.y, point2.y, point3.y, weight );
  17598. vector.z = THREE.Curve.Utils.interpolate( point0.z, point1.z, point2.z, point3.z, weight );
  17599. return vector;
  17600. }
  17601. );
  17602. // File:src/extras/animation/AnimationHandler.js
  17603. /**
  17604. * @author mikael emtinger / http://gomo.se/
  17605. */
  17606. THREE.AnimationHandler = {
  17607. LINEAR: 0,
  17608. CATMULLROM: 1,
  17609. CATMULLROM_FORWARD: 2,
  17610. //
  17611. add: function () { console.warn( 'THREE.AnimationHandler.add() has been deprecated.' ); },
  17612. get: function () { console.warn( 'THREE.AnimationHandler.get() has been deprecated.' ); },
  17613. remove: function () { console.warn( 'THREE.AnimationHandler.remove() has been deprecated.' ); },
  17614. //
  17615. animations: [],
  17616. init: function ( data ) {
  17617. if ( data.initialized === true ) return data;
  17618. // loop through all keys
  17619. for ( var h = 0; h < data.hierarchy.length; h ++ ) {
  17620. for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
  17621. // remove minus times
  17622. if ( data.hierarchy[ h ].keys[ k ].time < 0 ) {
  17623. data.hierarchy[ h ].keys[ k ].time = 0;
  17624. }
  17625. // create quaternions
  17626. if ( data.hierarchy[ h ].keys[ k ].rot !== undefined &&
  17627. ! ( data.hierarchy[ h ].keys[ k ].rot instanceof THREE.Quaternion ) ) {
  17628. var quat = data.hierarchy[ h ].keys[ k ].rot;
  17629. data.hierarchy[ h ].keys[ k ].rot = new THREE.Quaternion().fromArray( quat );
  17630. }
  17631. }
  17632. // prepare morph target keys
  17633. if ( data.hierarchy[ h ].keys.length && data.hierarchy[ h ].keys[ 0 ].morphTargets !== undefined ) {
  17634. // get all used
  17635. var usedMorphTargets = {};
  17636. for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
  17637. for ( var m = 0; m < data.hierarchy[ h ].keys[ k ].morphTargets.length; m ++ ) {
  17638. var morphTargetName = data.hierarchy[ h ].keys[ k ].morphTargets[ m ];
  17639. usedMorphTargets[ morphTargetName ] = - 1;
  17640. }
  17641. }
  17642. data.hierarchy[ h ].usedMorphTargets = usedMorphTargets;
  17643. // set all used on all frames
  17644. for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
  17645. var influences = {};
  17646. for ( var morphTargetName in usedMorphTargets ) {
  17647. for ( var m = 0; m < data.hierarchy[ h ].keys[ k ].morphTargets.length; m ++ ) {
  17648. if ( data.hierarchy[ h ].keys[ k ].morphTargets[ m ] === morphTargetName ) {
  17649. influences[ morphTargetName ] = data.hierarchy[ h ].keys[ k ].morphTargetsInfluences[ m ];
  17650. break;
  17651. }
  17652. }
  17653. if ( m === data.hierarchy[ h ].keys[ k ].morphTargets.length ) {
  17654. influences[ morphTargetName ] = 0;
  17655. }
  17656. }
  17657. data.hierarchy[ h ].keys[ k ].morphTargetsInfluences = influences;
  17658. }
  17659. }
  17660. // remove all keys that are on the same time
  17661. for ( var k = 1; k < data.hierarchy[ h ].keys.length; k ++ ) {
  17662. if ( data.hierarchy[ h ].keys[ k ].time === data.hierarchy[ h ].keys[ k - 1 ].time ) {
  17663. data.hierarchy[ h ].keys.splice( k, 1 );
  17664. k --;
  17665. }
  17666. }
  17667. // set index
  17668. for ( var k = 0; k < data.hierarchy[ h ].keys.length; k ++ ) {
  17669. data.hierarchy[ h ].keys[ k ].index = k;
  17670. }
  17671. }
  17672. data.initialized = true;
  17673. return data;
  17674. },
  17675. parse: function ( root ) {
  17676. var parseRecurseHierarchy = function ( root, hierarchy ) {
  17677. hierarchy.push( root );
  17678. for ( var c = 0; c < root.children.length; c ++ )
  17679. parseRecurseHierarchy( root.children[ c ], hierarchy );
  17680. };
  17681. // setup hierarchy
  17682. var hierarchy = [];
  17683. if ( root instanceof THREE.SkinnedMesh ) {
  17684. for ( var b = 0; b < root.skeleton.bones.length; b ++ ) {
  17685. hierarchy.push( root.skeleton.bones[ b ] );
  17686. }
  17687. } else {
  17688. parseRecurseHierarchy( root, hierarchy );
  17689. }
  17690. return hierarchy;
  17691. },
  17692. play: function ( animation ) {
  17693. if ( this.animations.indexOf( animation ) === - 1 ) {
  17694. this.animations.push( animation );
  17695. }
  17696. },
  17697. stop: function ( animation ) {
  17698. var index = this.animations.indexOf( animation );
  17699. if ( index !== - 1 ) {
  17700. this.animations.splice( index, 1 );
  17701. }
  17702. },
  17703. update: function ( deltaTimeMS ) {
  17704. for ( var i = 0; i < this.animations.length; i ++ ) {
  17705. this.animations[ i ].resetBlendWeights( );
  17706. }
  17707. for ( var i = 0; i < this.animations.length; i ++ ) {
  17708. this.animations[ i ].update( deltaTimeMS );
  17709. }
  17710. }
  17711. };
  17712. // File:src/extras/animation/Animation.js
  17713. /**
  17714. * @author mikael emtinger / http://gomo.se/
  17715. * @author mrdoob / http://mrdoob.com/
  17716. * @author alteredq / http://alteredqualia.com/
  17717. */
  17718. THREE.Animation = function ( root, data ) {
  17719. this.root = root;
  17720. this.data = THREE.AnimationHandler.init( data );
  17721. this.hierarchy = THREE.AnimationHandler.parse( root );
  17722. this.currentTime = 0;
  17723. this.timeScale = 1;
  17724. this.isPlaying = false;
  17725. this.loop = true;
  17726. this.weight = 0;
  17727. this.interpolationType = THREE.AnimationHandler.LINEAR;
  17728. };
  17729. THREE.Animation.prototype.keyTypes = [ "pos", "rot", "scl" ];
  17730. THREE.Animation.prototype.play = function ( startTime, weight ) {
  17731. this.currentTime = startTime !== undefined ? startTime : 0;
  17732. this.weight = weight !== undefined ? weight: 1;
  17733. this.isPlaying = true;
  17734. this.reset();
  17735. THREE.AnimationHandler.play( this );
  17736. };
  17737. THREE.Animation.prototype.stop = function() {
  17738. this.isPlaying = false;
  17739. THREE.AnimationHandler.stop( this );
  17740. };
  17741. THREE.Animation.prototype.reset = function () {
  17742. for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
  17743. var object = this.hierarchy[ h ];
  17744. object.matrixAutoUpdate = true;
  17745. if ( object.animationCache === undefined ) {
  17746. object.animationCache = {
  17747. animations: {},
  17748. blending: {
  17749. positionWeight: 0.0,
  17750. quaternionWeight: 0.0,
  17751. scaleWeight: 0.0
  17752. }
  17753. };
  17754. }
  17755. if ( object.animationCache.animations[this.data.name] === undefined ) {
  17756. object.animationCache.animations[this.data.name] = {};
  17757. object.animationCache.animations[this.data.name].prevKey = { pos: 0, rot: 0, scl: 0 };
  17758. object.animationCache.animations[this.data.name].nextKey = { pos: 0, rot: 0, scl: 0 };
  17759. object.animationCache.animations[this.data.name].originalMatrix = object.matrix;
  17760. }
  17761. var animationCache = object.animationCache.animations[this.data.name];
  17762. // Get keys to match our current time
  17763. for ( var t = 0; t < 3; t ++ ) {
  17764. var type = this.keyTypes[ t ];
  17765. var prevKey = this.data.hierarchy[ h ].keys[ 0 ];
  17766. var nextKey = this.getNextKeyWith( type, h, 1 );
  17767. while ( nextKey.time < this.currentTime && nextKey.index > prevKey.index ) {
  17768. prevKey = nextKey;
  17769. nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
  17770. }
  17771. animationCache.prevKey[ type ] = prevKey;
  17772. animationCache.nextKey[ type ] = nextKey;
  17773. }
  17774. }
  17775. };
  17776. THREE.Animation.prototype.resetBlendWeights = function () {
  17777. for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
  17778. var object = this.hierarchy[ h ];
  17779. if ( object.animationCache !== undefined ) {
  17780. object.animationCache.blending.positionWeight = 0.0;
  17781. object.animationCache.blending.quaternionWeight = 0.0;
  17782. object.animationCache.blending.scaleWeight = 0.0;
  17783. }
  17784. }
  17785. };
  17786. THREE.Animation.prototype.update = (function(){
  17787. var points = [];
  17788. var target = new THREE.Vector3();
  17789. var newVector = new THREE.Vector3();
  17790. var newQuat = new THREE.Quaternion();
  17791. // Catmull-Rom spline
  17792. var interpolateCatmullRom = function ( points, scale ) {
  17793. var c = [], v3 = [],
  17794. point, intPoint, weight, w2, w3,
  17795. pa, pb, pc, pd;
  17796. point = ( points.length - 1 ) * scale;
  17797. intPoint = Math.floor( point );
  17798. weight = point - intPoint;
  17799. c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1;
  17800. c[ 1 ] = intPoint;
  17801. c[ 2 ] = intPoint > points.length - 2 ? intPoint : intPoint + 1;
  17802. c[ 3 ] = intPoint > points.length - 3 ? intPoint : intPoint + 2;
  17803. pa = points[ c[ 0 ] ];
  17804. pb = points[ c[ 1 ] ];
  17805. pc = points[ c[ 2 ] ];
  17806. pd = points[ c[ 3 ] ];
  17807. w2 = weight * weight;
  17808. w3 = weight * w2;
  17809. v3[ 0 ] = interpolate( pa[ 0 ], pb[ 0 ], pc[ 0 ], pd[ 0 ], weight, w2, w3 );
  17810. v3[ 1 ] = interpolate( pa[ 1 ], pb[ 1 ], pc[ 1 ], pd[ 1 ], weight, w2, w3 );
  17811. v3[ 2 ] = interpolate( pa[ 2 ], pb[ 2 ], pc[ 2 ], pd[ 2 ], weight, w2, w3 );
  17812. return v3;
  17813. };
  17814. var interpolate = function ( p0, p1, p2, p3, t, t2, t3 ) {
  17815. var v0 = ( p2 - p0 ) * 0.5,
  17816. v1 = ( p3 - p1 ) * 0.5;
  17817. return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  17818. };
  17819. return function ( delta ) {
  17820. if ( this.isPlaying === false ) return;
  17821. this.currentTime += delta * this.timeScale;
  17822. if ( this.weight === 0 )
  17823. return;
  17824. //
  17825. var duration = this.data.length;
  17826. if ( this.currentTime > duration || this.currentTime < 0 ) {
  17827. if ( this.loop ) {
  17828. this.currentTime %= duration;
  17829. if ( this.currentTime < 0 )
  17830. this.currentTime += duration;
  17831. this.reset();
  17832. } else {
  17833. this.stop();
  17834. return;
  17835. }
  17836. }
  17837. for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
  17838. var object = this.hierarchy[ h ];
  17839. var animationCache = object.animationCache.animations[this.data.name];
  17840. var blending = object.animationCache.blending;
  17841. // loop through pos/rot/scl
  17842. for ( var t = 0; t < 3; t ++ ) {
  17843. // get keys
  17844. var type = this.keyTypes[ t ];
  17845. var prevKey = animationCache.prevKey[ type ];
  17846. var nextKey = animationCache.nextKey[ type ];
  17847. if ( ( this.timeScale > 0 && nextKey.time <= this.currentTime ) ||
  17848. ( this.timeScale < 0 && prevKey.time >= this.currentTime ) ) {
  17849. prevKey = this.data.hierarchy[ h ].keys[ 0 ];
  17850. nextKey = this.getNextKeyWith( type, h, 1 );
  17851. while ( nextKey.time < this.currentTime && nextKey.index > prevKey.index ) {
  17852. prevKey = nextKey;
  17853. nextKey = this.getNextKeyWith( type, h, nextKey.index + 1 );
  17854. }
  17855. animationCache.prevKey[ type ] = prevKey;
  17856. animationCache.nextKey[ type ] = nextKey;
  17857. }
  17858. object.matrixAutoUpdate = true;
  17859. object.matrixWorldNeedsUpdate = true;
  17860. var scale = ( this.currentTime - prevKey.time ) / ( nextKey.time - prevKey.time );
  17861. var prevXYZ = prevKey[ type ];
  17862. var nextXYZ = nextKey[ type ];
  17863. if ( scale < 0 ) scale = 0;
  17864. if ( scale > 1 ) scale = 1;
  17865. // interpolate
  17866. if ( type === "pos" ) {
  17867. if ( this.interpolationType === THREE.AnimationHandler.LINEAR ) {
  17868. newVector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
  17869. newVector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
  17870. newVector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
  17871. // blend
  17872. var proportionalWeight = this.weight / ( this.weight + blending.positionWeight );
  17873. object.position.lerp( newVector, proportionalWeight );
  17874. blending.positionWeight += this.weight;
  17875. } else if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
  17876. this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  17877. points[ 0 ] = this.getPrevKeyWith( "pos", h, prevKey.index - 1 )[ "pos" ];
  17878. points[ 1 ] = prevXYZ;
  17879. points[ 2 ] = nextXYZ;
  17880. points[ 3 ] = this.getNextKeyWith( "pos", h, nextKey.index + 1 )[ "pos" ];
  17881. scale = scale * 0.33 + 0.33;
  17882. var currentPoint = interpolateCatmullRom( points, scale );
  17883. var proportionalWeight = this.weight / ( this.weight + blending.positionWeight );
  17884. blending.positionWeight += this.weight;
  17885. // blend
  17886. var vector = object.position;
  17887. vector.x = vector.x + ( currentPoint[ 0 ] - vector.x ) * proportionalWeight;
  17888. vector.y = vector.y + ( currentPoint[ 1 ] - vector.y ) * proportionalWeight;
  17889. vector.z = vector.z + ( currentPoint[ 2 ] - vector.z ) * proportionalWeight;
  17890. if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  17891. var forwardPoint = interpolateCatmullRom( points, scale * 1.01 );
  17892. target.set( forwardPoint[ 0 ], forwardPoint[ 1 ], forwardPoint[ 2 ] );
  17893. target.sub( vector );
  17894. target.y = 0;
  17895. target.normalize();
  17896. var angle = Math.atan2( target.x, target.z );
  17897. object.rotation.set( 0, angle, 0 );
  17898. }
  17899. }
  17900. } else if ( type === "rot" ) {
  17901. THREE.Quaternion.slerp( prevXYZ, nextXYZ, newQuat, scale );
  17902. // Avoid paying the cost of an additional slerp if we don't have to
  17903. if ( blending.quaternionWeight === 0 ) {
  17904. object.quaternion.copy(newQuat);
  17905. blending.quaternionWeight = this.weight;
  17906. } else {
  17907. var proportionalWeight = this.weight / ( this.weight + blending.quaternionWeight );
  17908. THREE.Quaternion.slerp( object.quaternion, newQuat, object.quaternion, proportionalWeight );
  17909. blending.quaternionWeight += this.weight;
  17910. }
  17911. } else if ( type === "scl" ) {
  17912. newVector.x = prevXYZ[ 0 ] + ( nextXYZ[ 0 ] - prevXYZ[ 0 ] ) * scale;
  17913. newVector.y = prevXYZ[ 1 ] + ( nextXYZ[ 1 ] - prevXYZ[ 1 ] ) * scale;
  17914. newVector.z = prevXYZ[ 2 ] + ( nextXYZ[ 2 ] - prevXYZ[ 2 ] ) * scale;
  17915. var proportionalWeight = this.weight / ( this.weight + blending.scaleWeight );
  17916. object.scale.lerp( newVector, proportionalWeight );
  17917. blending.scaleWeight += this.weight;
  17918. }
  17919. }
  17920. }
  17921. return true;
  17922. };
  17923. })();
  17924. // Get next key with
  17925. THREE.Animation.prototype.getNextKeyWith = function ( type, h, key ) {
  17926. var keys = this.data.hierarchy[ h ].keys;
  17927. if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
  17928. this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  17929. key = key < keys.length - 1 ? key : keys.length - 1;
  17930. } else {
  17931. key = key % keys.length;
  17932. }
  17933. for ( ; key < keys.length; key ++ ) {
  17934. if ( keys[ key ][ type ] !== undefined ) {
  17935. return keys[ key ];
  17936. }
  17937. }
  17938. return this.data.hierarchy[ h ].keys[ 0 ];
  17939. };
  17940. // Get previous key with
  17941. THREE.Animation.prototype.getPrevKeyWith = function ( type, h, key ) {
  17942. var keys = this.data.hierarchy[ h ].keys;
  17943. if ( this.interpolationType === THREE.AnimationHandler.CATMULLROM ||
  17944. this.interpolationType === THREE.AnimationHandler.CATMULLROM_FORWARD ) {
  17945. key = key > 0 ? key : 0;
  17946. } else {
  17947. key = key >= 0 ? key : key + keys.length;
  17948. }
  17949. for ( ; key >= 0; key -- ) {
  17950. if ( keys[ key ][ type ] !== undefined ) {
  17951. return keys[ key ];
  17952. }
  17953. }
  17954. return this.data.hierarchy[ h ].keys[ keys.length - 1 ];
  17955. };
  17956. // File:src/extras/animation/KeyFrameAnimation.js
  17957. /**
  17958. * @author mikael emtinger / http://gomo.se/
  17959. * @author mrdoob / http://mrdoob.com/
  17960. * @author alteredq / http://alteredqualia.com/
  17961. * @author khang duong
  17962. * @author erik kitson
  17963. */
  17964. THREE.KeyFrameAnimation = function ( data ) {
  17965. this.root = data.node;
  17966. this.data = THREE.AnimationHandler.init( data );
  17967. this.hierarchy = THREE.AnimationHandler.parse( this.root );
  17968. this.currentTime = 0;
  17969. this.timeScale = 0.001;
  17970. this.isPlaying = false;
  17971. this.isPaused = true;
  17972. this.loop = true;
  17973. // initialize to first keyframes
  17974. for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
  17975. var keys = this.data.hierarchy[h].keys,
  17976. sids = this.data.hierarchy[h].sids,
  17977. obj = this.hierarchy[h];
  17978. if ( keys.length && sids ) {
  17979. for ( var s = 0; s < sids.length; s ++ ) {
  17980. var sid = sids[ s ],
  17981. next = this.getNextKeyWith( sid, h, 0 );
  17982. if ( next ) {
  17983. next.apply( sid );
  17984. }
  17985. }
  17986. obj.matrixAutoUpdate = false;
  17987. this.data.hierarchy[h].node.updateMatrix();
  17988. obj.matrixWorldNeedsUpdate = true;
  17989. }
  17990. }
  17991. };
  17992. THREE.KeyFrameAnimation.prototype.play = function ( startTime ) {
  17993. this.currentTime = startTime !== undefined ? startTime : 0;
  17994. if ( this.isPlaying === false ) {
  17995. this.isPlaying = true;
  17996. // reset key cache
  17997. var h, hl = this.hierarchy.length,
  17998. object,
  17999. node;
  18000. for ( h = 0; h < hl; h ++ ) {
  18001. object = this.hierarchy[ h ];
  18002. node = this.data.hierarchy[ h ];
  18003. if ( node.animationCache === undefined ) {
  18004. node.animationCache = {};
  18005. node.animationCache.prevKey = null;
  18006. node.animationCache.nextKey = null;
  18007. node.animationCache.originalMatrix = object.matrix;
  18008. }
  18009. var keys = this.data.hierarchy[h].keys;
  18010. if (keys.length) {
  18011. node.animationCache.prevKey = keys[ 0 ];
  18012. node.animationCache.nextKey = keys[ 1 ];
  18013. this.startTime = Math.min( keys[0].time, this.startTime );
  18014. this.endTime = Math.max( keys[keys.length - 1].time, this.endTime );
  18015. }
  18016. }
  18017. this.update( 0 );
  18018. }
  18019. this.isPaused = false;
  18020. THREE.AnimationHandler.play( this );
  18021. };
  18022. THREE.KeyFrameAnimation.prototype.stop = function() {
  18023. this.isPlaying = false;
  18024. this.isPaused = false;
  18025. THREE.AnimationHandler.stop( this );
  18026. // reset JIT matrix and remove cache
  18027. for ( var h = 0; h < this.data.hierarchy.length; h ++ ) {
  18028. var obj = this.hierarchy[ h ];
  18029. var node = this.data.hierarchy[ h ];
  18030. if ( node.animationCache !== undefined ) {
  18031. var original = node.animationCache.originalMatrix;
  18032. original.copy( obj.matrix );
  18033. obj.matrix = original;
  18034. delete node.animationCache;
  18035. }
  18036. }
  18037. };
  18038. // Update
  18039. THREE.KeyFrameAnimation.prototype.update = function ( delta ) {
  18040. if ( this.isPlaying === false ) return;
  18041. this.currentTime += delta * this.timeScale;
  18042. //
  18043. var duration = this.data.length;
  18044. if ( this.loop === true && this.currentTime > duration ) {
  18045. this.currentTime %= duration;
  18046. }
  18047. this.currentTime = Math.min( this.currentTime, duration );
  18048. for ( var h = 0, hl = this.hierarchy.length; h < hl; h ++ ) {
  18049. var object = this.hierarchy[ h ];
  18050. var node = this.data.hierarchy[ h ];
  18051. var keys = node.keys,
  18052. animationCache = node.animationCache;
  18053. if ( keys.length ) {
  18054. var prevKey = animationCache.prevKey;
  18055. var nextKey = animationCache.nextKey;
  18056. if ( nextKey.time <= this.currentTime ) {
  18057. while ( nextKey.time < this.currentTime && nextKey.index > prevKey.index ) {
  18058. prevKey = nextKey;
  18059. nextKey = keys[ prevKey.index + 1 ];
  18060. }
  18061. animationCache.prevKey = prevKey;
  18062. animationCache.nextKey = nextKey;
  18063. }
  18064. if ( nextKey.time >= this.currentTime ) {
  18065. prevKey.interpolate( nextKey, this.currentTime );
  18066. } else {
  18067. prevKey.interpolate( nextKey, nextKey.time );
  18068. }
  18069. this.data.hierarchy[ h ].node.updateMatrix();
  18070. object.matrixWorldNeedsUpdate = true;
  18071. }
  18072. }
  18073. };
  18074. // Get next key with
  18075. THREE.KeyFrameAnimation.prototype.getNextKeyWith = function( sid, h, key ) {
  18076. var keys = this.data.hierarchy[ h ].keys;
  18077. key = key % keys.length;
  18078. for ( ; key < keys.length; key ++ ) {
  18079. if ( keys[ key ].hasTarget( sid ) ) {
  18080. return keys[ key ];
  18081. }
  18082. }
  18083. return keys[ 0 ];
  18084. };
  18085. // Get previous key with
  18086. THREE.KeyFrameAnimation.prototype.getPrevKeyWith = function( sid, h, key ) {
  18087. var keys = this.data.hierarchy[ h ].keys;
  18088. key = key >= 0 ? key : key + keys.length;
  18089. for ( ; key >= 0; key -- ) {
  18090. if ( keys[ key ].hasTarget( sid ) ) {
  18091. return keys[ key ];
  18092. }
  18093. }
  18094. return keys[ keys.length - 1 ];
  18095. };
  18096. // File:src/extras/animation/MorphAnimation.js
  18097. /**
  18098. * @author mrdoob / http://mrdoob.com
  18099. * @author willy-vvu / http://willy-vvu.github.io
  18100. */
  18101. THREE.MorphAnimation = function ( mesh ) {
  18102. this.mesh = mesh;
  18103. this.frames = mesh.morphTargetInfluences.length;
  18104. this.currentTime = 0;
  18105. this.duration = 1000;
  18106. this.loop = true;
  18107. this.lastFrame = 0;
  18108. this.currentFrame = 0;
  18109. this.isPlaying = false;
  18110. };
  18111. THREE.MorphAnimation.prototype = {
  18112. constructor: THREE.MorphAnimation,
  18113. play: function () {
  18114. this.isPlaying = true;
  18115. },
  18116. pause: function () {
  18117. this.isPlaying = false;
  18118. },
  18119. update: function ( delta ) {
  18120. if ( this.isPlaying === false ) return;
  18121. this.currentTime += delta;
  18122. if ( this.loop === true && this.currentTime > this.duration ) {
  18123. this.currentTime %= this.duration;
  18124. }
  18125. this.currentTime = Math.min( this.currentTime, this.duration );
  18126. var interpolation = this.duration / this.frames;
  18127. var frame = Math.floor( this.currentTime / interpolation );
  18128. if ( frame != this.currentFrame ) {
  18129. this.mesh.morphTargetInfluences[ this.lastFrame ] = 0;
  18130. this.mesh.morphTargetInfluences[ this.currentFrame ] = 1;
  18131. this.mesh.morphTargetInfluences[ frame ] = 0;
  18132. this.lastFrame = this.currentFrame;
  18133. this.currentFrame = frame;
  18134. }
  18135. this.mesh.morphTargetInfluences[ frame ] = ( this.currentTime % interpolation ) / interpolation;
  18136. this.mesh.morphTargetInfluences[ this.lastFrame ] = 1 - this.mesh.morphTargetInfluences[ frame ];
  18137. }
  18138. };
  18139. // File:src/extras/geometries/BoxGeometry.js
  18140. /**
  18141. * @author mrdoob / http://mrdoob.com/
  18142. * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Cube.as
  18143. */
  18144. THREE.BoxGeometry = function ( width, height, depth, widthSegments, heightSegments, depthSegments ) {
  18145. THREE.Geometry.call( this );
  18146. this.type = 'BoxGeometry';
  18147. this.parameters = {
  18148. width: width,
  18149. height: height,
  18150. depth: depth,
  18151. widthSegments: widthSegments,
  18152. heightSegments: heightSegments,
  18153. depthSegments: depthSegments
  18154. };
  18155. this.widthSegments = widthSegments || 1;
  18156. this.heightSegments = heightSegments || 1;
  18157. this.depthSegments = depthSegments || 1;
  18158. var scope = this;
  18159. var width_half = width / 2;
  18160. var height_half = height / 2;
  18161. var depth_half = depth / 2;
  18162. buildPlane( 'z', 'y', - 1, - 1, depth, height, width_half, 0 ); // px
  18163. buildPlane( 'z', 'y', 1, - 1, depth, height, - width_half, 1 ); // nx
  18164. buildPlane( 'x', 'z', 1, 1, width, depth, height_half, 2 ); // py
  18165. buildPlane( 'x', 'z', 1, - 1, width, depth, - height_half, 3 ); // ny
  18166. buildPlane( 'x', 'y', 1, - 1, width, height, depth_half, 4 ); // pz
  18167. buildPlane( 'x', 'y', - 1, - 1, width, height, - depth_half, 5 ); // nz
  18168. function buildPlane( u, v, udir, vdir, width, height, depth, materialIndex ) {
  18169. var w, ix, iy,
  18170. gridX = scope.widthSegments,
  18171. gridY = scope.heightSegments,
  18172. width_half = width / 2,
  18173. height_half = height / 2,
  18174. offset = scope.vertices.length;
  18175. if ( ( u === 'x' && v === 'y' ) || ( u === 'y' && v === 'x' ) ) {
  18176. w = 'z';
  18177. } else if ( ( u === 'x' && v === 'z' ) || ( u === 'z' && v === 'x' ) ) {
  18178. w = 'y';
  18179. gridY = scope.depthSegments;
  18180. } else if ( ( u === 'z' && v === 'y' ) || ( u === 'y' && v === 'z' ) ) {
  18181. w = 'x';
  18182. gridX = scope.depthSegments;
  18183. }
  18184. var gridX1 = gridX + 1,
  18185. gridY1 = gridY + 1,
  18186. segment_width = width / gridX,
  18187. segment_height = height / gridY,
  18188. normal = new THREE.Vector3();
  18189. normal[ w ] = depth > 0 ? 1 : - 1;
  18190. for ( iy = 0; iy < gridY1; iy ++ ) {
  18191. for ( ix = 0; ix < gridX1; ix ++ ) {
  18192. var vector = new THREE.Vector3();
  18193. vector[ u ] = ( ix * segment_width - width_half ) * udir;
  18194. vector[ v ] = ( iy * segment_height - height_half ) * vdir;
  18195. vector[ w ] = depth;
  18196. scope.vertices.push( vector );
  18197. }
  18198. }
  18199. for ( iy = 0; iy < gridY; iy ++ ) {
  18200. for ( ix = 0; ix < gridX; ix ++ ) {
  18201. var a = ix + gridX1 * iy;
  18202. var b = ix + gridX1 * ( iy + 1 );
  18203. var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  18204. var d = ( ix + 1 ) + gridX1 * iy;
  18205. var uva = new THREE.Vector2( ix / gridX, 1 - iy / gridY );
  18206. var uvb = new THREE.Vector2( ix / gridX, 1 - ( iy + 1 ) / gridY );
  18207. var uvc = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - ( iy + 1 ) / gridY );
  18208. var uvd = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - iy / gridY );
  18209. var face = new THREE.Face3( a + offset, b + offset, d + offset );
  18210. face.normal.copy( normal );
  18211. face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() );
  18212. face.materialIndex = materialIndex;
  18213. scope.faces.push( face );
  18214. scope.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] );
  18215. face = new THREE.Face3( b + offset, c + offset, d + offset );
  18216. face.normal.copy( normal );
  18217. face.vertexNormals.push( normal.clone(), normal.clone(), normal.clone() );
  18218. face.materialIndex = materialIndex;
  18219. scope.faces.push( face );
  18220. scope.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] );
  18221. }
  18222. }
  18223. }
  18224. this.mergeVertices();
  18225. };
  18226. THREE.BoxGeometry.prototype = Object.create( THREE.Geometry.prototype );
  18227. THREE.BoxGeometry.prototype.constructor = THREE.BoxGeometry;
  18228. // File:src/extras/geometries/CircleGeometry.js
  18229. /**
  18230. * @author hughes
  18231. */
  18232. THREE.CircleGeometry = function ( radius, segments, thetaStart, thetaLength ) {
  18233. THREE.Geometry.call( this );
  18234. this.type = 'CircleGeometry';
  18235. this.parameters = {
  18236. radius: radius,
  18237. segments: segments,
  18238. thetaStart: thetaStart,
  18239. thetaLength: thetaLength
  18240. };
  18241. radius = radius || 50;
  18242. segments = segments !== undefined ? Math.max( 3, segments ) : 8;
  18243. thetaStart = thetaStart !== undefined ? thetaStart : 0;
  18244. thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
  18245. var i, uvs = [],
  18246. center = new THREE.Vector3(), centerUV = new THREE.Vector2( 0.5, 0.5 );
  18247. this.vertices.push(center);
  18248. uvs.push( centerUV );
  18249. for ( i = 0; i <= segments; i ++ ) {
  18250. var vertex = new THREE.Vector3();
  18251. var segment = thetaStart + i / segments * thetaLength;
  18252. vertex.x = radius * Math.cos( segment );
  18253. vertex.y = radius * Math.sin( segment );
  18254. this.vertices.push( vertex );
  18255. uvs.push( new THREE.Vector2( ( vertex.x / radius + 1 ) / 2, ( vertex.y / radius + 1 ) / 2 ) );
  18256. }
  18257. var n = new THREE.Vector3( 0, 0, 1 );
  18258. for ( i = 1; i <= segments; i ++ ) {
  18259. this.faces.push( new THREE.Face3( i, i + 1, 0, [ n.clone(), n.clone(), n.clone() ] ) );
  18260. this.faceVertexUvs[ 0 ].push( [ uvs[ i ].clone(), uvs[ i + 1 ].clone(), centerUV.clone() ] );
  18261. }
  18262. this.computeFaceNormals();
  18263. this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius );
  18264. };
  18265. THREE.CircleGeometry.prototype = Object.create( THREE.Geometry.prototype );
  18266. THREE.CircleGeometry.prototype.constructor = THREE.CircleGeometry;
  18267. // File:src/extras/geometries/CubeGeometry.js
  18268. /**
  18269. * @author mrdoob / http://mrdoob.com/
  18270. */
  18271. THREE.CubeGeometry = function ( width, height, depth, widthSegments, heightSegments, depthSegments ) {
  18272. console.warn( 'THREE.CubeGeometry has been renamed to THREE.BoxGeometry.' );
  18273. return new THREE.BoxGeometry( width, height, depth, widthSegments, heightSegments, depthSegments );
  18274. };
  18275. // File:src/extras/geometries/CylinderGeometry.js
  18276. /**
  18277. * @author mrdoob / http://mrdoob.com/
  18278. */
  18279. THREE.CylinderGeometry = function ( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded ) {
  18280. THREE.Geometry.call( this );
  18281. this.type = 'CylinderGeometry';
  18282. this.parameters = {
  18283. radiusTop: radiusTop,
  18284. radiusBottom: radiusBottom,
  18285. height: height,
  18286. radialSegments: radialSegments,
  18287. heightSegments: heightSegments,
  18288. openEnded: openEnded
  18289. };
  18290. radiusTop = radiusTop !== undefined ? radiusTop : 20;
  18291. radiusBottom = radiusBottom !== undefined ? radiusBottom : 20;
  18292. height = height !== undefined ? height : 100;
  18293. radialSegments = radialSegments || 8;
  18294. heightSegments = heightSegments || 1;
  18295. openEnded = openEnded !== undefined ? openEnded : false;
  18296. var heightHalf = height / 2;
  18297. var x, y, vertices = [], uvs = [];
  18298. for ( y = 0; y <= heightSegments; y ++ ) {
  18299. var verticesRow = [];
  18300. var uvsRow = [];
  18301. var v = y / heightSegments;
  18302. var radius = v * ( radiusBottom - radiusTop ) + radiusTop;
  18303. for ( x = 0; x <= radialSegments; x ++ ) {
  18304. var u = x / radialSegments;
  18305. var vertex = new THREE.Vector3();
  18306. vertex.x = radius * Math.sin( u * Math.PI * 2 );
  18307. vertex.y = - v * height + heightHalf;
  18308. vertex.z = radius * Math.cos( u * Math.PI * 2 );
  18309. this.vertices.push( vertex );
  18310. verticesRow.push( this.vertices.length - 1 );
  18311. uvsRow.push( new THREE.Vector2( u, 1 - v ) );
  18312. }
  18313. vertices.push( verticesRow );
  18314. uvs.push( uvsRow );
  18315. }
  18316. var tanTheta = ( radiusBottom - radiusTop ) / height;
  18317. var na, nb;
  18318. for ( x = 0; x < radialSegments; x ++ ) {
  18319. if ( radiusTop !== 0 ) {
  18320. na = this.vertices[ vertices[ 0 ][ x ] ].clone();
  18321. nb = this.vertices[ vertices[ 0 ][ x + 1 ] ].clone();
  18322. } else {
  18323. na = this.vertices[ vertices[ 1 ][ x ] ].clone();
  18324. nb = this.vertices[ vertices[ 1 ][ x + 1 ] ].clone();
  18325. }
  18326. na.setY( Math.sqrt( na.x * na.x + na.z * na.z ) * tanTheta ).normalize();
  18327. nb.setY( Math.sqrt( nb.x * nb.x + nb.z * nb.z ) * tanTheta ).normalize();
  18328. for ( y = 0; y < heightSegments; y ++ ) {
  18329. var v1 = vertices[ y ][ x ];
  18330. var v2 = vertices[ y + 1 ][ x ];
  18331. var v3 = vertices[ y + 1 ][ x + 1 ];
  18332. var v4 = vertices[ y ][ x + 1 ];
  18333. var n1 = na.clone();
  18334. var n2 = na.clone();
  18335. var n3 = nb.clone();
  18336. var n4 = nb.clone();
  18337. var uv1 = uvs[ y ][ x ].clone();
  18338. var uv2 = uvs[ y + 1 ][ x ].clone();
  18339. var uv3 = uvs[ y + 1 ][ x + 1 ].clone();
  18340. var uv4 = uvs[ y ][ x + 1 ].clone();
  18341. this.faces.push( new THREE.Face3( v1, v2, v4, [ n1, n2, n4 ] ) );
  18342. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv4 ] );
  18343. this.faces.push( new THREE.Face3( v2, v3, v4, [ n2.clone(), n3, n4.clone() ] ) );
  18344. this.faceVertexUvs[ 0 ].push( [ uv2.clone(), uv3, uv4.clone() ] );
  18345. }
  18346. }
  18347. // top cap
  18348. if ( openEnded === false && radiusTop > 0 ) {
  18349. this.vertices.push( new THREE.Vector3( 0, heightHalf, 0 ) );
  18350. for ( x = 0; x < radialSegments; x ++ ) {
  18351. var v1 = vertices[ 0 ][ x ];
  18352. var v2 = vertices[ 0 ][ x + 1 ];
  18353. var v3 = this.vertices.length - 1;
  18354. var n1 = new THREE.Vector3( 0, 1, 0 );
  18355. var n2 = new THREE.Vector3( 0, 1, 0 );
  18356. var n3 = new THREE.Vector3( 0, 1, 0 );
  18357. var uv1 = uvs[ 0 ][ x ].clone();
  18358. var uv2 = uvs[ 0 ][ x + 1 ].clone();
  18359. var uv3 = new THREE.Vector2( uv2.x, 0 );
  18360. this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
  18361. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
  18362. }
  18363. }
  18364. // bottom cap
  18365. if ( openEnded === false && radiusBottom > 0 ) {
  18366. this.vertices.push( new THREE.Vector3( 0, - heightHalf, 0 ) );
  18367. for ( x = 0; x < radialSegments; x ++ ) {
  18368. var v1 = vertices[ y ][ x + 1 ];
  18369. var v2 = vertices[ y ][ x ];
  18370. var v3 = this.vertices.length - 1;
  18371. var n1 = new THREE.Vector3( 0, - 1, 0 );
  18372. var n2 = new THREE.Vector3( 0, - 1, 0 );
  18373. var n3 = new THREE.Vector3( 0, - 1, 0 );
  18374. var uv1 = uvs[ y ][ x + 1 ].clone();
  18375. var uv2 = uvs[ y ][ x ].clone();
  18376. var uv3 = new THREE.Vector2( uv2.x, 1 );
  18377. this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
  18378. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
  18379. }
  18380. }
  18381. this.computeFaceNormals();
  18382. }
  18383. THREE.CylinderGeometry.prototype = Object.create( THREE.Geometry.prototype );
  18384. THREE.CylinderGeometry.prototype.constructor = THREE.CylinderGeometry;
  18385. // File:src/extras/geometries/ExtrudeGeometry.js
  18386. /**
  18387. * @author zz85 / http://www.lab4games.net/zz85/blog
  18388. *
  18389. * Creates extruded geometry from a path shape.
  18390. *
  18391. * parameters = {
  18392. *
  18393. * curveSegments: <int>, // number of points on the curves
  18394. * steps: <int>, // number of points for z-side extrusions / used for subdividing segements of extrude spline too
  18395. * amount: <int>, // Depth to extrude the shape
  18396. *
  18397. * bevelEnabled: <bool>, // turn on bevel
  18398. * bevelThickness: <float>, // how deep into the original shape bevel goes
  18399. * bevelSize: <float>, // how far from shape outline is bevel
  18400. * bevelSegments: <int>, // number of bevel layers
  18401. *
  18402. * extrudePath: <THREE.CurvePath> // 3d spline path to extrude shape along. (creates Frames if .frames aren't defined)
  18403. * frames: <THREE.TubeGeometry.FrenetFrames> // containing arrays of tangents, normals, binormals
  18404. *
  18405. * material: <int> // material index for front and back faces
  18406. * extrudeMaterial: <int> // material index for extrusion and beveled faces
  18407. * uvGenerator: <Object> // object that provides UV generator functions
  18408. *
  18409. * }
  18410. **/
  18411. THREE.ExtrudeGeometry = function ( shapes, options ) {
  18412. if ( typeof( shapes ) === "undefined" ) {
  18413. shapes = [];
  18414. return;
  18415. }
  18416. THREE.Geometry.call( this );
  18417. this.type = 'ExtrudeGeometry';
  18418. shapes = shapes instanceof Array ? shapes : [ shapes ];
  18419. this.addShapeList( shapes, options );
  18420. this.computeFaceNormals();
  18421. // can't really use automatic vertex normals
  18422. // as then front and back sides get smoothed too
  18423. // should do separate smoothing just for sides
  18424. //this.computeVertexNormals();
  18425. //console.log( "took", ( Date.now() - startTime ) );
  18426. };
  18427. THREE.ExtrudeGeometry.prototype = Object.create( THREE.Geometry.prototype );
  18428. THREE.ExtrudeGeometry.prototype.constructor = THREE.ExtrudeGeometry;
  18429. THREE.ExtrudeGeometry.prototype.addShapeList = function ( shapes, options ) {
  18430. var sl = shapes.length;
  18431. for ( var s = 0; s < sl; s ++ ) {
  18432. var shape = shapes[ s ];
  18433. this.addShape( shape, options );
  18434. }
  18435. };
  18436. THREE.ExtrudeGeometry.prototype.addShape = function ( shape, options ) {
  18437. var amount = options.amount !== undefined ? options.amount : 100;
  18438. var bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 6; // 10
  18439. var bevelSize = options.bevelSize !== undefined ? options.bevelSize : bevelThickness - 2; // 8
  18440. var bevelSegments = options.bevelSegments !== undefined ? options.bevelSegments : 3;
  18441. var bevelEnabled = options.bevelEnabled !== undefined ? options.bevelEnabled : true; // false
  18442. var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
  18443. var steps = options.steps !== undefined ? options.steps : 1;
  18444. var extrudePath = options.extrudePath;
  18445. var extrudePts, extrudeByPath = false;
  18446. var material = options.material;
  18447. var extrudeMaterial = options.extrudeMaterial;
  18448. // Use default WorldUVGenerator if no UV generators are specified.
  18449. var uvgen = options.UVGenerator !== undefined ? options.UVGenerator : THREE.ExtrudeGeometry.WorldUVGenerator;
  18450. var splineTube, binormal, normal, position2;
  18451. if ( extrudePath ) {
  18452. extrudePts = extrudePath.getSpacedPoints( steps );
  18453. extrudeByPath = true;
  18454. bevelEnabled = false; // bevels not supported for path extrusion
  18455. // SETUP TNB variables
  18456. // Reuse TNB from TubeGeomtry for now.
  18457. // TODO1 - have a .isClosed in spline?
  18458. splineTube = options.frames !== undefined ? options.frames : new THREE.TubeGeometry.FrenetFrames(extrudePath, steps, false);
  18459. // console.log(splineTube, 'splineTube', splineTube.normals.length, 'steps', steps, 'extrudePts', extrudePts.length);
  18460. binormal = new THREE.Vector3();
  18461. normal = new THREE.Vector3();
  18462. position2 = new THREE.Vector3();
  18463. }
  18464. // Safeguards if bevels are not enabled
  18465. if ( ! bevelEnabled ) {
  18466. bevelSegments = 0;
  18467. bevelThickness = 0;
  18468. bevelSize = 0;
  18469. }
  18470. // Variables initalization
  18471. var ahole, h, hl; // looping of holes
  18472. var scope = this;
  18473. var bevelPoints = [];
  18474. var shapesOffset = this.vertices.length;
  18475. var shapePoints = shape.extractPoints( curveSegments );
  18476. var vertices = shapePoints.shape;
  18477. var holes = shapePoints.holes;
  18478. var reverse = ! THREE.Shape.Utils.isClockWise( vertices ) ;
  18479. if ( reverse ) {
  18480. vertices = vertices.reverse();
  18481. // Maybe we should also check if holes are in the opposite direction, just to be safe ...
  18482. for ( h = 0, hl = holes.length; h < hl; h ++ ) {
  18483. ahole = holes[ h ];
  18484. if ( THREE.Shape.Utils.isClockWise( ahole ) ) {
  18485. holes[ h ] = ahole.reverse();
  18486. }
  18487. }
  18488. reverse = false; // If vertices are in order now, we shouldn't need to worry about them again (hopefully)!
  18489. }
  18490. var faces = THREE.Shape.Utils.triangulateShape ( vertices, holes );
  18491. /* Vertices */
  18492. var contour = vertices; // vertices has all points but contour has only points of circumference
  18493. for ( h = 0, hl = holes.length; h < hl; h ++ ) {
  18494. ahole = holes[ h ];
  18495. vertices = vertices.concat( ahole );
  18496. }
  18497. function scalePt2 ( pt, vec, size ) {
  18498. if ( ! vec ) console.log( "die" );
  18499. return vec.clone().multiplyScalar( size ).add( pt );
  18500. }
  18501. var b, bs, t, z,
  18502. vert, vlen = vertices.length,
  18503. face, flen = faces.length,
  18504. cont, clen = contour.length;
  18505. // Find directions for point movement
  18506. var RAD_TO_DEGREES = 180 / Math.PI;
  18507. function getBevelVec( inPt, inPrev, inNext ) {
  18508. var EPSILON = 0.0000000001;
  18509. // computes for inPt the corresponding point inPt' on a new contour
  18510. // shiftet by 1 unit (length of normalized vector) to the left
  18511. // if we walk along contour clockwise, this new contour is outside the old one
  18512. //
  18513. // inPt' is the intersection of the two lines parallel to the two
  18514. // adjacent edges of inPt at a distance of 1 unit on the left side.
  18515. var v_trans_x, v_trans_y, shrink_by = 1; // resulting translation vector for inPt
  18516. // good reading for geometry algorithms (here: line-line intersection)
  18517. // http://geomalgorithms.com/a05-_intersect-1.html
  18518. var v_prev_x = inPt.x - inPrev.x, v_prev_y = inPt.y - inPrev.y;
  18519. var v_next_x = inNext.x - inPt.x, v_next_y = inNext.y - inPt.y;
  18520. var v_prev_lensq = ( v_prev_x * v_prev_x + v_prev_y * v_prev_y );
  18521. // check for colinear edges
  18522. var colinear0 = ( v_prev_x * v_next_y - v_prev_y * v_next_x );
  18523. if ( Math.abs( colinear0 ) > EPSILON ) { // not colinear
  18524. // length of vectors for normalizing
  18525. var v_prev_len = Math.sqrt( v_prev_lensq );
  18526. var v_next_len = Math.sqrt( v_next_x * v_next_x + v_next_y * v_next_y );
  18527. // shift adjacent points by unit vectors to the left
  18528. var ptPrevShift_x = ( inPrev.x - v_prev_y / v_prev_len );
  18529. var ptPrevShift_y = ( inPrev.y + v_prev_x / v_prev_len );
  18530. var ptNextShift_x = ( inNext.x - v_next_y / v_next_len );
  18531. var ptNextShift_y = ( inNext.y + v_next_x / v_next_len );
  18532. // scaling factor for v_prev to intersection point
  18533. var sf = ( ( ptNextShift_x - ptPrevShift_x ) * v_next_y -
  18534. ( ptNextShift_y - ptPrevShift_y ) * v_next_x ) /
  18535. ( v_prev_x * v_next_y - v_prev_y * v_next_x );
  18536. // vector from inPt to intersection point
  18537. v_trans_x = ( ptPrevShift_x + v_prev_x * sf - inPt.x );
  18538. v_trans_y = ( ptPrevShift_y + v_prev_y * sf - inPt.y );
  18539. // Don't normalize!, otherwise sharp corners become ugly
  18540. // but prevent crazy spikes
  18541. var v_trans_lensq = ( v_trans_x * v_trans_x + v_trans_y * v_trans_y )
  18542. if ( v_trans_lensq <= 2 ) {
  18543. return new THREE.Vector2( v_trans_x, v_trans_y );
  18544. } else {
  18545. shrink_by = Math.sqrt( v_trans_lensq / 2 );
  18546. }
  18547. } else { // handle special case of colinear edges
  18548. var direction_eq = false; // assumes: opposite
  18549. if ( v_prev_x > EPSILON ) {
  18550. if ( v_next_x > EPSILON ) { direction_eq = true; }
  18551. } else {
  18552. if ( v_prev_x < - EPSILON ) {
  18553. if ( v_next_x < - EPSILON ) { direction_eq = true; }
  18554. } else {
  18555. if ( Math.sign(v_prev_y) == Math.sign(v_next_y) ) { direction_eq = true; }
  18556. }
  18557. }
  18558. if ( direction_eq ) {
  18559. // console.log("Warning: lines are a straight sequence");
  18560. v_trans_x = - v_prev_y;
  18561. v_trans_y = v_prev_x;
  18562. shrink_by = Math.sqrt( v_prev_lensq );
  18563. } else {
  18564. // console.log("Warning: lines are a straight spike");
  18565. v_trans_x = v_prev_x;
  18566. v_trans_y = v_prev_y;
  18567. shrink_by = Math.sqrt( v_prev_lensq / 2 );
  18568. }
  18569. }
  18570. return new THREE.Vector2( v_trans_x / shrink_by, v_trans_y / shrink_by );
  18571. }
  18572. var contourMovements = [];
  18573. for ( var i = 0, il = contour.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
  18574. if ( j === il ) j = 0;
  18575. if ( k === il ) k = 0;
  18576. // (j)---(i)---(k)
  18577. // console.log('i,j,k', i, j , k)
  18578. var pt_i = contour[ i ];
  18579. var pt_j = contour[ j ];
  18580. var pt_k = contour[ k ];
  18581. contourMovements[ i ]= getBevelVec( contour[ i ], contour[ j ], contour[ k ] );
  18582. }
  18583. var holesMovements = [], oneHoleMovements, verticesMovements = contourMovements.concat();
  18584. for ( h = 0, hl = holes.length; h < hl; h ++ ) {
  18585. ahole = holes[ h ];
  18586. oneHoleMovements = [];
  18587. for ( i = 0, il = ahole.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) {
  18588. if ( j === il ) j = 0;
  18589. if ( k === il ) k = 0;
  18590. // (j)---(i)---(k)
  18591. oneHoleMovements[ i ]= getBevelVec( ahole[ i ], ahole[ j ], ahole[ k ] );
  18592. }
  18593. holesMovements.push( oneHoleMovements );
  18594. verticesMovements = verticesMovements.concat( oneHoleMovements );
  18595. }
  18596. // Loop bevelSegments, 1 for the front, 1 for the back
  18597. for ( b = 0; b < bevelSegments; b ++ ) {
  18598. //for ( b = bevelSegments; b > 0; b -- ) {
  18599. t = b / bevelSegments;
  18600. z = bevelThickness * ( 1 - t );
  18601. //z = bevelThickness * t;
  18602. bs = bevelSize * ( Math.sin ( t * Math.PI/2 ) ) ; // curved
  18603. //bs = bevelSize * t ; // linear
  18604. // contract shape
  18605. for ( i = 0, il = contour.length; i < il; i ++ ) {
  18606. vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
  18607. v( vert.x, vert.y, - z );
  18608. }
  18609. // expand holes
  18610. for ( h = 0, hl = holes.length; h < hl; h ++ ) {
  18611. ahole = holes[ h ];
  18612. oneHoleMovements = holesMovements[ h ];
  18613. for ( i = 0, il = ahole.length; i < il; i ++ ) {
  18614. vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
  18615. v( vert.x, vert.y, - z );
  18616. }
  18617. }
  18618. }
  18619. bs = bevelSize;
  18620. // Back facing vertices
  18621. for ( i = 0; i < vlen; i ++ ) {
  18622. vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
  18623. if ( ! extrudeByPath ) {
  18624. v( vert.x, vert.y, 0 );
  18625. } else {
  18626. // v( vert.x, vert.y + extrudePts[ 0 ].y, extrudePts[ 0 ].x );
  18627. normal.copy( splineTube.normals[0] ).multiplyScalar(vert.x);
  18628. binormal.copy( splineTube.binormals[0] ).multiplyScalar(vert.y);
  18629. position2.copy( extrudePts[0] ).add(normal).add(binormal);
  18630. v( position2.x, position2.y, position2.z );
  18631. }
  18632. }
  18633. // Add stepped vertices...
  18634. // Including front facing vertices
  18635. var s;
  18636. for ( s = 1; s <= steps; s ++ ) {
  18637. for ( i = 0; i < vlen; i ++ ) {
  18638. vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ];
  18639. if ( ! extrudeByPath ) {
  18640. v( vert.x, vert.y, amount / steps * s );
  18641. } else {
  18642. // v( vert.x, vert.y + extrudePts[ s - 1 ].y, extrudePts[ s - 1 ].x );
  18643. normal.copy( splineTube.normals[s] ).multiplyScalar( vert.x );
  18644. binormal.copy( splineTube.binormals[s] ).multiplyScalar( vert.y );
  18645. position2.copy( extrudePts[s] ).add( normal ).add( binormal );
  18646. v( position2.x, position2.y, position2.z );
  18647. }
  18648. }
  18649. }
  18650. // Add bevel segments planes
  18651. //for ( b = 1; b <= bevelSegments; b ++ ) {
  18652. for ( b = bevelSegments - 1; b >= 0; b -- ) {
  18653. t = b / bevelSegments;
  18654. z = bevelThickness * ( 1 - t );
  18655. //bs = bevelSize * ( 1-Math.sin ( ( 1 - t ) * Math.PI/2 ) );
  18656. bs = bevelSize * Math.sin ( t * Math.PI/2 ) ;
  18657. // contract shape
  18658. for ( i = 0, il = contour.length; i < il; i ++ ) {
  18659. vert = scalePt2( contour[ i ], contourMovements[ i ], bs );
  18660. v( vert.x, vert.y, amount + z );
  18661. }
  18662. // expand holes
  18663. for ( h = 0, hl = holes.length; h < hl; h ++ ) {
  18664. ahole = holes[ h ];
  18665. oneHoleMovements = holesMovements[ h ];
  18666. for ( i = 0, il = ahole.length; i < il; i ++ ) {
  18667. vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs );
  18668. if ( ! extrudeByPath ) {
  18669. v( vert.x, vert.y, amount + z );
  18670. } else {
  18671. v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x + z );
  18672. }
  18673. }
  18674. }
  18675. }
  18676. /* Faces */
  18677. // Top and bottom faces
  18678. buildLidFaces();
  18679. // Sides faces
  18680. buildSideFaces();
  18681. ///// Internal functions
  18682. function buildLidFaces() {
  18683. if ( bevelEnabled ) {
  18684. var layer = 0 ; // steps + 1
  18685. var offset = vlen * layer;
  18686. // Bottom faces
  18687. for ( i = 0; i < flen; i ++ ) {
  18688. face = faces[ i ];
  18689. f3( face[ 2 ]+ offset, face[ 1 ]+ offset, face[ 0 ] + offset );
  18690. }
  18691. layer = steps + bevelSegments * 2;
  18692. offset = vlen * layer;
  18693. // Top faces
  18694. for ( i = 0; i < flen; i ++ ) {
  18695. face = faces[ i ];
  18696. f3( face[ 0 ] + offset, face[ 1 ] + offset, face[ 2 ] + offset );
  18697. }
  18698. } else {
  18699. // Bottom faces
  18700. for ( i = 0; i < flen; i ++ ) {
  18701. face = faces[ i ];
  18702. f3( face[ 2 ], face[ 1 ], face[ 0 ] );
  18703. }
  18704. // Top faces
  18705. for ( i = 0; i < flen; i ++ ) {
  18706. face = faces[ i ];
  18707. f3( face[ 0 ] + vlen * steps, face[ 1 ] + vlen * steps, face[ 2 ] + vlen * steps );
  18708. }
  18709. }
  18710. }
  18711. // Create faces for the z-sides of the shape
  18712. function buildSideFaces() {
  18713. var layeroffset = 0;
  18714. sidewalls( contour, layeroffset );
  18715. layeroffset += contour.length;
  18716. for ( h = 0, hl = holes.length; h < hl; h ++ ) {
  18717. ahole = holes[ h ];
  18718. sidewalls( ahole, layeroffset );
  18719. //, true
  18720. layeroffset += ahole.length;
  18721. }
  18722. }
  18723. function sidewalls( contour, layeroffset ) {
  18724. var j, k;
  18725. i = contour.length;
  18726. while ( --i >= 0 ) {
  18727. j = i;
  18728. k = i - 1;
  18729. if ( k < 0 ) k = contour.length - 1;
  18730. //console.log('b', i,j, i-1, k,vertices.length);
  18731. var s = 0, sl = steps + bevelSegments * 2;
  18732. for ( s = 0; s < sl; s ++ ) {
  18733. var slen1 = vlen * s;
  18734. var slen2 = vlen * ( s + 1 );
  18735. var a = layeroffset + j + slen1,
  18736. b = layeroffset + k + slen1,
  18737. c = layeroffset + k + slen2,
  18738. d = layeroffset + j + slen2;
  18739. f4( a, b, c, d, contour, s, sl, j, k );
  18740. }
  18741. }
  18742. }
  18743. function v( x, y, z ) {
  18744. scope.vertices.push( new THREE.Vector3( x, y, z ) );
  18745. }
  18746. function f3( a, b, c ) {
  18747. a += shapesOffset;
  18748. b += shapesOffset;
  18749. c += shapesOffset;
  18750. // normal, color, material
  18751. scope.faces.push( new THREE.Face3( a, b, c, null, null, material ) );
  18752. var uvs = uvgen.generateTopUV( scope, a, b, c );
  18753. scope.faceVertexUvs[ 0 ].push( uvs );
  18754. }
  18755. function f4( a, b, c, d, wallContour, stepIndex, stepsLength, contourIndex1, contourIndex2 ) {
  18756. a += shapesOffset;
  18757. b += shapesOffset;
  18758. c += shapesOffset;
  18759. d += shapesOffset;
  18760. scope.faces.push( new THREE.Face3( a, b, d, null, null, extrudeMaterial ) );
  18761. scope.faces.push( new THREE.Face3( b, c, d, null, null, extrudeMaterial ) );
  18762. var uvs = uvgen.generateSideWallUV( scope, a, b, c, d );
  18763. scope.faceVertexUvs[ 0 ].push( [ uvs[ 0 ], uvs[ 1 ], uvs[ 3 ] ] );
  18764. scope.faceVertexUvs[ 0 ].push( [ uvs[ 1 ], uvs[ 2 ], uvs[ 3 ] ] );
  18765. }
  18766. };
  18767. THREE.ExtrudeGeometry.WorldUVGenerator = {
  18768. generateTopUV: function ( geometry, indexA, indexB, indexC ) {
  18769. var vertices = geometry.vertices;
  18770. var a = vertices[ indexA ];
  18771. var b = vertices[ indexB ];
  18772. var c = vertices[ indexC ];
  18773. return [
  18774. new THREE.Vector2( a.x, a.y ),
  18775. new THREE.Vector2( b.x, b.y ),
  18776. new THREE.Vector2( c.x, c.y )
  18777. ];
  18778. },
  18779. generateSideWallUV: function ( geometry, indexA, indexB, indexC, indexD ) {
  18780. var vertices = geometry.vertices;
  18781. var a = vertices[ indexA ];
  18782. var b = vertices[ indexB ];
  18783. var c = vertices[ indexC ];
  18784. var d = vertices[ indexD ];
  18785. if ( Math.abs( a.y - b.y ) < 0.01 ) {
  18786. return [
  18787. new THREE.Vector2( a.x, 1 - a.z ),
  18788. new THREE.Vector2( b.x, 1 - b.z ),
  18789. new THREE.Vector2( c.x, 1 - c.z ),
  18790. new THREE.Vector2( d.x, 1 - d.z )
  18791. ];
  18792. } else {
  18793. return [
  18794. new THREE.Vector2( a.y, 1 - a.z ),
  18795. new THREE.Vector2( b.y, 1 - b.z ),
  18796. new THREE.Vector2( c.y, 1 - c.z ),
  18797. new THREE.Vector2( d.y, 1 - d.z )
  18798. ];
  18799. }
  18800. }
  18801. };
  18802. // File:src/extras/geometries/ShapeGeometry.js
  18803. /**
  18804. * @author jonobr1 / http://jonobr1.com
  18805. *
  18806. * Creates a one-sided polygonal geometry from a path shape. Similar to
  18807. * ExtrudeGeometry.
  18808. *
  18809. * parameters = {
  18810. *
  18811. * curveSegments: <int>, // number of points on the curves. NOT USED AT THE MOMENT.
  18812. *
  18813. * material: <int> // material index for front and back faces
  18814. * uvGenerator: <Object> // object that provides UV generator functions
  18815. *
  18816. * }
  18817. **/
  18818. THREE.ShapeGeometry = function ( shapes, options ) {
  18819. THREE.Geometry.call( this );
  18820. this.type = 'ShapeGeometry';
  18821. if ( shapes instanceof Array === false ) shapes = [ shapes ];
  18822. this.addShapeList( shapes, options );
  18823. this.computeFaceNormals();
  18824. };
  18825. THREE.ShapeGeometry.prototype = Object.create( THREE.Geometry.prototype );
  18826. THREE.ShapeGeometry.prototype.constructor = THREE.ShapeGeometry;
  18827. /**
  18828. * Add an array of shapes to THREE.ShapeGeometry.
  18829. */
  18830. THREE.ShapeGeometry.prototype.addShapeList = function ( shapes, options ) {
  18831. for ( var i = 0, l = shapes.length; i < l; i ++ ) {
  18832. this.addShape( shapes[ i ], options );
  18833. }
  18834. return this;
  18835. };
  18836. /**
  18837. * Adds a shape to THREE.ShapeGeometry, based on THREE.ExtrudeGeometry.
  18838. */
  18839. THREE.ShapeGeometry.prototype.addShape = function ( shape, options ) {
  18840. if ( options === undefined ) options = {};
  18841. var curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12;
  18842. var material = options.material;
  18843. var uvgen = options.UVGenerator === undefined ? THREE.ExtrudeGeometry.WorldUVGenerator : options.UVGenerator;
  18844. //
  18845. var i, l, hole, s;
  18846. var shapesOffset = this.vertices.length;
  18847. var shapePoints = shape.extractPoints( curveSegments );
  18848. var vertices = shapePoints.shape;
  18849. var holes = shapePoints.holes;
  18850. var reverse = ! THREE.Shape.Utils.isClockWise( vertices );
  18851. if ( reverse ) {
  18852. vertices = vertices.reverse();
  18853. // Maybe we should also check if holes are in the opposite direction, just to be safe...
  18854. for ( i = 0, l = holes.length; i < l; i ++ ) {
  18855. hole = holes[ i ];
  18856. if ( THREE.Shape.Utils.isClockWise( hole ) ) {
  18857. holes[ i ] = hole.reverse();
  18858. }
  18859. }
  18860. reverse = false;
  18861. }
  18862. var faces = THREE.Shape.Utils.triangulateShape( vertices, holes );
  18863. // Vertices
  18864. var contour = vertices;
  18865. for ( i = 0, l = holes.length; i < l; i ++ ) {
  18866. hole = holes[ i ];
  18867. vertices = vertices.concat( hole );
  18868. }
  18869. //
  18870. var vert, vlen = vertices.length;
  18871. var face, flen = faces.length;
  18872. var cont, clen = contour.length;
  18873. for ( i = 0; i < vlen; i ++ ) {
  18874. vert = vertices[ i ];
  18875. this.vertices.push( new THREE.Vector3( vert.x, vert.y, 0 ) );
  18876. }
  18877. for ( i = 0; i < flen; i ++ ) {
  18878. face = faces[ i ];
  18879. var a = face[ 0 ] + shapesOffset;
  18880. var b = face[ 1 ] + shapesOffset;
  18881. var c = face[ 2 ] + shapesOffset;
  18882. this.faces.push( new THREE.Face3( a, b, c, null, null, material ) );
  18883. this.faceVertexUvs[ 0 ].push( uvgen.generateTopUV( this, a, b, c ) );
  18884. }
  18885. };
  18886. // File:src/extras/geometries/LatheGeometry.js
  18887. /**
  18888. * @author astrodud / http://astrodud.isgreat.org/
  18889. * @author zz85 / https://github.com/zz85
  18890. * @author bhouston / http://exocortex.com
  18891. */
  18892. // points - to create a closed torus, one must use a set of points
  18893. // like so: [ a, b, c, d, a ], see first is the same as last.
  18894. // segments - the number of circumference segments to create
  18895. // phiStart - the starting radian
  18896. // phiLength - the radian (0 to 2*PI) range of the lathed section
  18897. // 2*pi is a closed lathe, less than 2PI is a portion.
  18898. THREE.LatheGeometry = function ( points, segments, phiStart, phiLength ) {
  18899. THREE.Geometry.call( this );
  18900. this.type = 'LatheGeometry';
  18901. this.parameters = {
  18902. points: points,
  18903. segments: segments,
  18904. phiStart: phiStart,
  18905. phiLength: phiLength
  18906. };
  18907. segments = segments || 12;
  18908. phiStart = phiStart || 0;
  18909. phiLength = phiLength || 2 * Math.PI;
  18910. var inversePointLength = 1.0 / ( points.length - 1 );
  18911. var inverseSegments = 1.0 / segments;
  18912. for ( var i = 0, il = segments; i <= il; i ++ ) {
  18913. var phi = phiStart + i * inverseSegments * phiLength;
  18914. var c = Math.cos( phi ),
  18915. s = Math.sin( phi );
  18916. for ( var j = 0, jl = points.length; j < jl; j ++ ) {
  18917. var pt = points[ j ];
  18918. var vertex = new THREE.Vector3();
  18919. vertex.x = c * pt.x - s * pt.y;
  18920. vertex.y = s * pt.x + c * pt.y;
  18921. vertex.z = pt.z;
  18922. this.vertices.push( vertex );
  18923. }
  18924. }
  18925. var np = points.length;
  18926. for ( var i = 0, il = segments; i < il; i ++ ) {
  18927. for ( var j = 0, jl = points.length - 1; j < jl; j ++ ) {
  18928. var base = j + np * i;
  18929. var a = base;
  18930. var b = base + np;
  18931. var c = base + 1 + np;
  18932. var d = base + 1;
  18933. var u0 = i * inverseSegments;
  18934. var v0 = j * inversePointLength;
  18935. var u1 = u0 + inverseSegments;
  18936. var v1 = v0 + inversePointLength;
  18937. this.faces.push( new THREE.Face3( a, b, d ) );
  18938. this.faceVertexUvs[ 0 ].push( [
  18939. new THREE.Vector2( u0, v0 ),
  18940. new THREE.Vector2( u1, v0 ),
  18941. new THREE.Vector2( u0, v1 )
  18942. ] );
  18943. this.faces.push( new THREE.Face3( b, c, d ) );
  18944. this.faceVertexUvs[ 0 ].push( [
  18945. new THREE.Vector2( u1, v0 ),
  18946. new THREE.Vector2( u1, v1 ),
  18947. new THREE.Vector2( u0, v1 )
  18948. ] );
  18949. }
  18950. }
  18951. this.mergeVertices();
  18952. this.computeFaceNormals();
  18953. this.computeVertexNormals();
  18954. };
  18955. THREE.LatheGeometry.prototype = Object.create( THREE.Geometry.prototype );
  18956. THREE.LatheGeometry.prototype.constructor = THREE.LatheGeometry;
  18957. // File:src/extras/geometries/PlaneGeometry.js
  18958. /**
  18959. * @author mrdoob / http://mrdoob.com/
  18960. * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Plane.as
  18961. */
  18962. THREE.PlaneGeometry = function ( width, height, widthSegments, heightSegments ) {
  18963. console.info( 'THREE.PlaneGeometry: Consider using THREE.PlaneBufferGeometry for lower memory footprint.' );
  18964. THREE.Geometry.call( this );
  18965. this.type = 'PlaneGeometry';
  18966. this.parameters = {
  18967. width: width,
  18968. height: height,
  18969. widthSegments: widthSegments,
  18970. heightSegments: heightSegments
  18971. };
  18972. this.fromBufferGeometry( new THREE.PlaneBufferGeometry( width, height, widthSegments, heightSegments ) );
  18973. };
  18974. THREE.PlaneGeometry.prototype = Object.create( THREE.Geometry.prototype );
  18975. THREE.PlaneGeometry.prototype.constructor = THREE.PlaneGeometry;
  18976. // File:src/extras/geometries/PlaneBufferGeometry.js
  18977. /**
  18978. * @author mrdoob / http://mrdoob.com/
  18979. * based on http://papervision3d.googlecode.com/svn/trunk/as3/trunk/src/org/papervision3d/objects/primitives/Plane.as
  18980. */
  18981. THREE.PlaneBufferGeometry = function ( width, height, widthSegments, heightSegments ) {
  18982. THREE.BufferGeometry.call( this );
  18983. this.type = 'PlaneBufferGeometry';
  18984. this.parameters = {
  18985. width: width,
  18986. height: height,
  18987. widthSegments: widthSegments,
  18988. heightSegments: heightSegments
  18989. };
  18990. var width_half = width / 2;
  18991. var height_half = height / 2;
  18992. var gridX = widthSegments || 1;
  18993. var gridY = heightSegments || 1;
  18994. var gridX1 = gridX + 1;
  18995. var gridY1 = gridY + 1;
  18996. var segment_width = width / gridX;
  18997. var segment_height = height / gridY;
  18998. var vertices = new Float32Array( gridX1 * gridY1 * 3 );
  18999. var normals = new Float32Array( gridX1 * gridY1 * 3 );
  19000. var uvs = new Float32Array( gridX1 * gridY1 * 2 );
  19001. var offset = 0;
  19002. var offset2 = 0;
  19003. for ( var iy = 0; iy < gridY1; iy ++ ) {
  19004. var y = iy * segment_height - height_half;
  19005. for ( var ix = 0; ix < gridX1; ix ++ ) {
  19006. var x = ix * segment_width - width_half;
  19007. vertices[ offset ] = x;
  19008. vertices[ offset + 1 ] = - y;
  19009. normals[ offset + 2 ] = 1;
  19010. uvs[ offset2 ] = ix / gridX;
  19011. uvs[ offset2 + 1 ] = 1 - ( iy / gridY );
  19012. offset += 3;
  19013. offset2 += 2;
  19014. }
  19015. }
  19016. offset = 0;
  19017. var indices = new ( ( vertices.length / 3 ) > 65535 ? Uint32Array : Uint16Array )( gridX * gridY * 6 );
  19018. for ( var iy = 0; iy < gridY; iy ++ ) {
  19019. for ( var ix = 0; ix < gridX; ix ++ ) {
  19020. var a = ix + gridX1 * iy;
  19021. var b = ix + gridX1 * ( iy + 1 );
  19022. var c = ( ix + 1 ) + gridX1 * ( iy + 1 );
  19023. var d = ( ix + 1 ) + gridX1 * iy;
  19024. indices[ offset ] = a;
  19025. indices[ offset + 1 ] = b;
  19026. indices[ offset + 2 ] = d;
  19027. indices[ offset + 3 ] = b;
  19028. indices[ offset + 4 ] = c;
  19029. indices[ offset + 5 ] = d;
  19030. offset += 6;
  19031. }
  19032. }
  19033. this.addAttribute( 'index', new THREE.BufferAttribute( indices, 1 ) );
  19034. this.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  19035. this.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  19036. this.addAttribute( 'uv', new THREE.BufferAttribute( uvs, 2 ) );
  19037. };
  19038. THREE.PlaneBufferGeometry.prototype = Object.create( THREE.BufferGeometry.prototype );
  19039. THREE.PlaneBufferGeometry.prototype.constructor = THREE.PlaneBufferGeometry;
  19040. // File:src/extras/geometries/RingGeometry.js
  19041. /**
  19042. * @author Kaleb Murphy
  19043. */
  19044. THREE.RingGeometry = function ( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) {
  19045. THREE.Geometry.call( this );
  19046. this.type = 'RingGeometry';
  19047. this.parameters = {
  19048. innerRadius: innerRadius,
  19049. outerRadius: outerRadius,
  19050. thetaSegments: thetaSegments,
  19051. phiSegments: phiSegments,
  19052. thetaStart: thetaStart,
  19053. thetaLength: thetaLength
  19054. };
  19055. innerRadius = innerRadius || 0;
  19056. outerRadius = outerRadius || 50;
  19057. thetaStart = thetaStart !== undefined ? thetaStart : 0;
  19058. thetaLength = thetaLength !== undefined ? thetaLength : Math.PI * 2;
  19059. thetaSegments = thetaSegments !== undefined ? Math.max( 3, thetaSegments ) : 8;
  19060. phiSegments = phiSegments !== undefined ? Math.max( 1, phiSegments ) : 8;
  19061. var i, o, uvs = [], radius = innerRadius, radiusStep = ( ( outerRadius - innerRadius ) / phiSegments );
  19062. for ( i = 0; i < phiSegments + 1; i ++ ) { // concentric circles inside ring
  19063. for ( o = 0; o < thetaSegments + 1; o ++ ) { // number of segments per circle
  19064. var vertex = new THREE.Vector3();
  19065. var segment = thetaStart + o / thetaSegments * thetaLength;
  19066. vertex.x = radius * Math.cos( segment );
  19067. vertex.y = radius * Math.sin( segment );
  19068. this.vertices.push( vertex );
  19069. uvs.push( new THREE.Vector2( ( vertex.x / outerRadius + 1 ) / 2, ( vertex.y / outerRadius + 1 ) / 2 ) );
  19070. }
  19071. radius += radiusStep;
  19072. }
  19073. var n = new THREE.Vector3( 0, 0, 1 );
  19074. for ( i = 0; i < phiSegments; i ++ ) { // concentric circles inside ring
  19075. var thetaSegment = i * (thetaSegments + 1);
  19076. for ( o = 0; o < thetaSegments ; o ++ ) { // number of segments per circle
  19077. var segment = o + thetaSegment;
  19078. var v1 = segment;
  19079. var v2 = segment + thetaSegments + 1;
  19080. var v3 = segment + thetaSegments + 2;
  19081. this.faces.push( new THREE.Face3( v1, v2, v3, [ n.clone(), n.clone(), n.clone() ] ) );
  19082. this.faceVertexUvs[ 0 ].push( [ uvs[ v1 ].clone(), uvs[ v2 ].clone(), uvs[ v3 ].clone() ]);
  19083. v1 = segment;
  19084. v2 = segment + thetaSegments + 2;
  19085. v3 = segment + 1;
  19086. this.faces.push( new THREE.Face3( v1, v2, v3, [ n.clone(), n.clone(), n.clone() ] ) );
  19087. this.faceVertexUvs[ 0 ].push( [ uvs[ v1 ].clone(), uvs[ v2 ].clone(), uvs[ v3 ].clone() ]);
  19088. }
  19089. }
  19090. this.computeFaceNormals();
  19091. this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius );
  19092. };
  19093. THREE.RingGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19094. THREE.RingGeometry.prototype.constructor = THREE.RingGeometry;
  19095. // File:src/extras/geometries/SphereGeometry.js
  19096. /**
  19097. * @author mrdoob / http://mrdoob.com/
  19098. */
  19099. THREE.SphereGeometry = function ( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) {
  19100. THREE.Geometry.call( this );
  19101. this.type = 'SphereGeometry';
  19102. this.parameters = {
  19103. radius: radius,
  19104. widthSegments: widthSegments,
  19105. heightSegments: heightSegments,
  19106. phiStart: phiStart,
  19107. phiLength: phiLength,
  19108. thetaStart: thetaStart,
  19109. thetaLength: thetaLength
  19110. };
  19111. radius = radius || 50;
  19112. widthSegments = Math.max( 3, Math.floor( widthSegments ) || 8 );
  19113. heightSegments = Math.max( 2, Math.floor( heightSegments ) || 6 );
  19114. phiStart = phiStart !== undefined ? phiStart : 0;
  19115. phiLength = phiLength !== undefined ? phiLength : Math.PI * 2;
  19116. thetaStart = thetaStart !== undefined ? thetaStart : 0;
  19117. thetaLength = thetaLength !== undefined ? thetaLength : Math.PI;
  19118. var x, y, vertices = [], uvs = [];
  19119. for ( y = 0; y <= heightSegments; y ++ ) {
  19120. var verticesRow = [];
  19121. var uvsRow = [];
  19122. for ( x = 0; x <= widthSegments; x ++ ) {
  19123. var u = x / widthSegments;
  19124. var v = y / heightSegments;
  19125. var vertex = new THREE.Vector3();
  19126. vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  19127. vertex.y = radius * Math.cos( thetaStart + v * thetaLength );
  19128. vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength );
  19129. this.vertices.push( vertex );
  19130. verticesRow.push( this.vertices.length - 1 );
  19131. uvsRow.push( new THREE.Vector2( u, 1 - v ) );
  19132. }
  19133. vertices.push( verticesRow );
  19134. uvs.push( uvsRow );
  19135. }
  19136. for ( y = 0; y < heightSegments; y ++ ) {
  19137. for ( x = 0; x < widthSegments; x ++ ) {
  19138. var v1 = vertices[ y ][ x + 1 ];
  19139. var v2 = vertices[ y ][ x ];
  19140. var v3 = vertices[ y + 1 ][ x ];
  19141. var v4 = vertices[ y + 1 ][ x + 1 ];
  19142. var n1 = this.vertices[ v1 ].clone().normalize();
  19143. var n2 = this.vertices[ v2 ].clone().normalize();
  19144. var n3 = this.vertices[ v3 ].clone().normalize();
  19145. var n4 = this.vertices[ v4 ].clone().normalize();
  19146. var uv1 = uvs[ y ][ x + 1 ].clone();
  19147. var uv2 = uvs[ y ][ x ].clone();
  19148. var uv3 = uvs[ y + 1 ][ x ].clone();
  19149. var uv4 = uvs[ y + 1 ][ x + 1 ].clone();
  19150. if ( Math.abs( this.vertices[ v1 ].y ) === radius ) {
  19151. uv1.x = ( uv1.x + uv2.x ) / 2;
  19152. this.faces.push( new THREE.Face3( v1, v3, v4, [ n1, n3, n4 ] ) );
  19153. this.faceVertexUvs[ 0 ].push( [ uv1, uv3, uv4 ] );
  19154. } else if ( Math.abs( this.vertices[ v3 ].y ) === radius ) {
  19155. uv3.x = ( uv3.x + uv4.x ) / 2;
  19156. this.faces.push( new THREE.Face3( v1, v2, v3, [ n1, n2, n3 ] ) );
  19157. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv3 ] );
  19158. } else {
  19159. this.faces.push( new THREE.Face3( v1, v2, v4, [ n1, n2, n4 ] ) );
  19160. this.faceVertexUvs[ 0 ].push( [ uv1, uv2, uv4 ] );
  19161. this.faces.push( new THREE.Face3( v2, v3, v4, [ n2.clone(), n3, n4.clone() ] ) );
  19162. this.faceVertexUvs[ 0 ].push( [ uv2.clone(), uv3, uv4.clone() ] );
  19163. }
  19164. }
  19165. }
  19166. this.computeFaceNormals();
  19167. this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius );
  19168. };
  19169. THREE.SphereGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19170. THREE.SphereGeometry.prototype.constructor = THREE.SphereGeometry;
  19171. // File:src/extras/geometries/TextGeometry.js
  19172. /**
  19173. * @author zz85 / http://www.lab4games.net/zz85/blog
  19174. * @author alteredq / http://alteredqualia.com/
  19175. *
  19176. * For creating 3D text geometry in three.js
  19177. *
  19178. * Text = 3D Text
  19179. *
  19180. * parameters = {
  19181. * size: <float>, // size of the text
  19182. * height: <float>, // thickness to extrude text
  19183. * curveSegments: <int>, // number of points on the curves
  19184. *
  19185. * font: <string>, // font name
  19186. * weight: <string>, // font weight (normal, bold)
  19187. * style: <string>, // font style (normal, italics)
  19188. *
  19189. * bevelEnabled: <bool>, // turn on bevel
  19190. * bevelThickness: <float>, // how deep into text bevel goes
  19191. * bevelSize: <float>, // how far from text outline is bevel
  19192. * }
  19193. *
  19194. */
  19195. /* Usage Examples
  19196. // TextGeometry wrapper
  19197. var text3d = new TextGeometry( text, options );
  19198. // Complete manner
  19199. var textShapes = THREE.FontUtils.generateShapes( text, options );
  19200. var text3d = new ExtrudeGeometry( textShapes, options );
  19201. */
  19202. THREE.TextGeometry = function ( text, parameters ) {
  19203. parameters = parameters || {};
  19204. var textShapes = THREE.FontUtils.generateShapes( text, parameters );
  19205. // translate parameters to ExtrudeGeometry API
  19206. parameters.amount = parameters.height !== undefined ? parameters.height : 50;
  19207. // defaults
  19208. if ( parameters.bevelThickness === undefined ) parameters.bevelThickness = 10;
  19209. if ( parameters.bevelSize === undefined ) parameters.bevelSize = 8;
  19210. if ( parameters.bevelEnabled === undefined ) parameters.bevelEnabled = false;
  19211. THREE.ExtrudeGeometry.call( this, textShapes, parameters );
  19212. this.type = 'TextGeometry';
  19213. };
  19214. THREE.TextGeometry.prototype = Object.create( THREE.ExtrudeGeometry.prototype );
  19215. THREE.TextGeometry.prototype.constructor = THREE.TextGeometry;
  19216. // File:src/extras/geometries/TorusGeometry.js
  19217. /**
  19218. * @author oosmoxiecode
  19219. * @author mrdoob / http://mrdoob.com/
  19220. * based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3DLite/src/away3dlite/primitives/Torus.as?r=2888
  19221. */
  19222. THREE.TorusGeometry = function ( radius, tube, radialSegments, tubularSegments, arc ) {
  19223. THREE.Geometry.call( this );
  19224. this.type = 'TorusGeometry';
  19225. this.parameters = {
  19226. radius: radius,
  19227. tube: tube,
  19228. radialSegments: radialSegments,
  19229. tubularSegments: tubularSegments,
  19230. arc: arc
  19231. };
  19232. radius = radius || 100;
  19233. tube = tube || 40;
  19234. radialSegments = radialSegments || 8;
  19235. tubularSegments = tubularSegments || 6;
  19236. arc = arc || Math.PI * 2;
  19237. var center = new THREE.Vector3(), uvs = [], normals = [];
  19238. for ( var j = 0; j <= radialSegments; j ++ ) {
  19239. for ( var i = 0; i <= tubularSegments; i ++ ) {
  19240. var u = i / tubularSegments * arc;
  19241. var v = j / radialSegments * Math.PI * 2;
  19242. center.x = radius * Math.cos( u );
  19243. center.y = radius * Math.sin( u );
  19244. var vertex = new THREE.Vector3();
  19245. vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u );
  19246. vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u );
  19247. vertex.z = tube * Math.sin( v );
  19248. this.vertices.push( vertex );
  19249. uvs.push( new THREE.Vector2( i / tubularSegments, j / radialSegments ) );
  19250. normals.push( vertex.clone().sub( center ).normalize() );
  19251. }
  19252. }
  19253. for ( var j = 1; j <= radialSegments; j ++ ) {
  19254. for ( var i = 1; i <= tubularSegments; i ++ ) {
  19255. var a = ( tubularSegments + 1 ) * j + i - 1;
  19256. var b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1;
  19257. var c = ( tubularSegments + 1 ) * ( j - 1 ) + i;
  19258. var d = ( tubularSegments + 1 ) * j + i;
  19259. var face = new THREE.Face3( a, b, d, [ normals[ a ].clone(), normals[ b ].clone(), normals[ d ].clone() ] );
  19260. this.faces.push( face );
  19261. this.faceVertexUvs[ 0 ].push( [ uvs[ a ].clone(), uvs[ b ].clone(), uvs[ d ].clone() ] );
  19262. face = new THREE.Face3( b, c, d, [ normals[ b ].clone(), normals[ c ].clone(), normals[ d ].clone() ] );
  19263. this.faces.push( face );
  19264. this.faceVertexUvs[ 0 ].push( [ uvs[ b ].clone(), uvs[ c ].clone(), uvs[ d ].clone() ] );
  19265. }
  19266. }
  19267. this.computeFaceNormals();
  19268. };
  19269. THREE.TorusGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19270. THREE.TorusGeometry.prototype.constructor = THREE.TorusGeometry;
  19271. // File:src/extras/geometries/TorusKnotGeometry.js
  19272. /**
  19273. * @author oosmoxiecode
  19274. * based on http://code.google.com/p/away3d/source/browse/trunk/fp10/Away3D/src/away3d/primitives/TorusKnot.as?spec=svn2473&r=2473
  19275. */
  19276. THREE.TorusKnotGeometry = function ( radius, tube, radialSegments, tubularSegments, p, q, heightScale ) {
  19277. THREE.Geometry.call( this );
  19278. this.type = 'TorusKnotGeometry';
  19279. this.parameters = {
  19280. radius: radius,
  19281. tube: tube,
  19282. radialSegments: radialSegments,
  19283. tubularSegments: tubularSegments,
  19284. p: p,
  19285. q: q,
  19286. heightScale: heightScale
  19287. };
  19288. radius = radius || 100;
  19289. tube = tube || 40;
  19290. radialSegments = radialSegments || 64;
  19291. tubularSegments = tubularSegments || 8;
  19292. p = p || 2;
  19293. q = q || 3;
  19294. heightScale = heightScale || 1;
  19295. var grid = new Array( radialSegments );
  19296. var tang = new THREE.Vector3();
  19297. var n = new THREE.Vector3();
  19298. var bitan = new THREE.Vector3();
  19299. for ( var i = 0; i < radialSegments; ++ i ) {
  19300. grid[ i ] = new Array( tubularSegments );
  19301. var u = i / radialSegments * 2 * p * Math.PI;
  19302. var p1 = getPos( u, q, p, radius, heightScale );
  19303. var p2 = getPos( u + 0.01, q, p, radius, heightScale );
  19304. tang.subVectors( p2, p1 );
  19305. n.addVectors( p2, p1 );
  19306. bitan.crossVectors( tang, n );
  19307. n.crossVectors( bitan, tang );
  19308. bitan.normalize();
  19309. n.normalize();
  19310. for ( var j = 0; j < tubularSegments; ++ j ) {
  19311. var v = j / tubularSegments * 2 * Math.PI;
  19312. var cx = - tube * Math.cos( v ); // TODO: Hack: Negating it so it faces outside.
  19313. var cy = tube * Math.sin( v );
  19314. var pos = new THREE.Vector3();
  19315. pos.x = p1.x + cx * n.x + cy * bitan.x;
  19316. pos.y = p1.y + cx * n.y + cy * bitan.y;
  19317. pos.z = p1.z + cx * n.z + cy * bitan.z;
  19318. grid[ i ][ j ] = this.vertices.push( pos ) - 1;
  19319. }
  19320. }
  19321. for ( var i = 0; i < radialSegments; ++ i ) {
  19322. for ( var j = 0; j < tubularSegments; ++ j ) {
  19323. var ip = ( i + 1 ) % radialSegments;
  19324. var jp = ( j + 1 ) % tubularSegments;
  19325. var a = grid[ i ][ j ];
  19326. var b = grid[ ip ][ j ];
  19327. var c = grid[ ip ][ jp ];
  19328. var d = grid[ i ][ jp ];
  19329. var uva = new THREE.Vector2( i / radialSegments, j / tubularSegments );
  19330. var uvb = new THREE.Vector2( ( i + 1 ) / radialSegments, j / tubularSegments );
  19331. var uvc = new THREE.Vector2( ( i + 1 ) / radialSegments, ( j + 1 ) / tubularSegments );
  19332. var uvd = new THREE.Vector2( i / radialSegments, ( j + 1 ) / tubularSegments );
  19333. this.faces.push( new THREE.Face3( a, b, d ) );
  19334. this.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] );
  19335. this.faces.push( new THREE.Face3( b, c, d ) );
  19336. this.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] );
  19337. }
  19338. }
  19339. this.computeFaceNormals();
  19340. this.computeVertexNormals();
  19341. function getPos( u, in_q, in_p, radius, heightScale ) {
  19342. var cu = Math.cos( u );
  19343. var su = Math.sin( u );
  19344. var quOverP = in_q / in_p * u;
  19345. var cs = Math.cos( quOverP );
  19346. var tx = radius * ( 2 + cs ) * 0.5 * cu;
  19347. var ty = radius * ( 2 + cs ) * su * 0.5;
  19348. var tz = heightScale * radius * Math.sin( quOverP ) * 0.5;
  19349. return new THREE.Vector3( tx, ty, tz );
  19350. }
  19351. };
  19352. THREE.TorusKnotGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19353. THREE.TorusKnotGeometry.prototype.constructor = THREE.TorusKnotGeometry;
  19354. // File:src/extras/geometries/TubeGeometry.js
  19355. /**
  19356. * @author WestLangley / https://github.com/WestLangley
  19357. * @author zz85 / https://github.com/zz85
  19358. * @author miningold / https://github.com/miningold
  19359. *
  19360. * Modified from the TorusKnotGeometry by @oosmoxiecode
  19361. *
  19362. * Creates a tube which extrudes along a 3d spline
  19363. *
  19364. * Uses parallel transport frames as described in
  19365. * http://www.cs.indiana.edu/pub/techreports/TR425.pdf
  19366. */
  19367. THREE.TubeGeometry = function ( path, segments, radius, radialSegments, closed ) {
  19368. THREE.Geometry.call( this );
  19369. this.type = 'TubeGeometry';
  19370. this.parameters = {
  19371. path: path,
  19372. segments: segments,
  19373. radius: radius,
  19374. radialSegments: radialSegments,
  19375. closed: closed
  19376. };
  19377. segments = segments || 64;
  19378. radius = radius || 1;
  19379. radialSegments = radialSegments || 8;
  19380. closed = closed || false;
  19381. var grid = [];
  19382. var scope = this,
  19383. tangent,
  19384. normal,
  19385. binormal,
  19386. numpoints = segments + 1,
  19387. x, y, z,
  19388. tx, ty, tz,
  19389. u, v,
  19390. cx, cy,
  19391. pos, pos2 = new THREE.Vector3(),
  19392. i, j,
  19393. ip, jp,
  19394. a, b, c, d,
  19395. uva, uvb, uvc, uvd;
  19396. var frames = new THREE.TubeGeometry.FrenetFrames( path, segments, closed ),
  19397. tangents = frames.tangents,
  19398. normals = frames.normals,
  19399. binormals = frames.binormals;
  19400. // proxy internals
  19401. this.tangents = tangents;
  19402. this.normals = normals;
  19403. this.binormals = binormals;
  19404. function vert( x, y, z ) {
  19405. return scope.vertices.push( new THREE.Vector3( x, y, z ) ) - 1;
  19406. }
  19407. // consruct the grid
  19408. for ( i = 0; i < numpoints; i ++ ) {
  19409. grid[ i ] = [];
  19410. u = i / ( numpoints - 1 );
  19411. pos = path.getPointAt( u );
  19412. tangent = tangents[ i ];
  19413. normal = normals[ i ];
  19414. binormal = binormals[ i ];
  19415. for ( j = 0; j < radialSegments; j ++ ) {
  19416. v = j / radialSegments * 2 * Math.PI;
  19417. cx = - radius * Math.cos( v ); // TODO: Hack: Negating it so it faces outside.
  19418. cy = radius * Math.sin( v );
  19419. pos2.copy( pos );
  19420. pos2.x += cx * normal.x + cy * binormal.x;
  19421. pos2.y += cx * normal.y + cy * binormal.y;
  19422. pos2.z += cx * normal.z + cy * binormal.z;
  19423. grid[ i ][ j ] = vert( pos2.x, pos2.y, pos2.z );
  19424. }
  19425. }
  19426. // construct the mesh
  19427. for ( i = 0; i < segments; i ++ ) {
  19428. for ( j = 0; j < radialSegments; j ++ ) {
  19429. ip = ( closed ) ? (i + 1) % segments : i + 1;
  19430. jp = (j + 1) % radialSegments;
  19431. a = grid[ i ][ j ]; // *** NOT NECESSARILY PLANAR ! ***
  19432. b = grid[ ip ][ j ];
  19433. c = grid[ ip ][ jp ];
  19434. d = grid[ i ][ jp ];
  19435. uva = new THREE.Vector2( i / segments, j / radialSegments );
  19436. uvb = new THREE.Vector2( ( i + 1 ) / segments, j / radialSegments );
  19437. uvc = new THREE.Vector2( ( i + 1 ) / segments, ( j + 1 ) / radialSegments );
  19438. uvd = new THREE.Vector2( i / segments, ( j + 1 ) / radialSegments );
  19439. this.faces.push( new THREE.Face3( a, b, d ) );
  19440. this.faceVertexUvs[ 0 ].push( [ uva, uvb, uvd ] );
  19441. this.faces.push( new THREE.Face3( b, c, d ) );
  19442. this.faceVertexUvs[ 0 ].push( [ uvb.clone(), uvc, uvd.clone() ] );
  19443. }
  19444. }
  19445. this.computeFaceNormals();
  19446. this.computeVertexNormals();
  19447. };
  19448. THREE.TubeGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19449. THREE.TubeGeometry.prototype.constructor = THREE.TubeGeometry;
  19450. // For computing of Frenet frames, exposing the tangents, normals and binormals the spline
  19451. THREE.TubeGeometry.FrenetFrames = function ( path, segments, closed ) {
  19452. var tangent = new THREE.Vector3(),
  19453. normal = new THREE.Vector3(),
  19454. binormal = new THREE.Vector3(),
  19455. tangents = [],
  19456. normals = [],
  19457. binormals = [],
  19458. vec = new THREE.Vector3(),
  19459. mat = new THREE.Matrix4(),
  19460. numpoints = segments + 1,
  19461. theta,
  19462. epsilon = 0.0001,
  19463. smallest,
  19464. tx, ty, tz,
  19465. i, u, v;
  19466. // expose internals
  19467. this.tangents = tangents;
  19468. this.normals = normals;
  19469. this.binormals = binormals;
  19470. // compute the tangent vectors for each segment on the path
  19471. for ( i = 0; i < numpoints; i ++ ) {
  19472. u = i / ( numpoints - 1 );
  19473. tangents[ i ] = path.getTangentAt( u );
  19474. tangents[ i ].normalize();
  19475. }
  19476. initialNormal3();
  19477. /*
  19478. function initialNormal1(lastBinormal) {
  19479. // fixed start binormal. Has dangers of 0 vectors
  19480. normals[ 0 ] = new THREE.Vector3();
  19481. binormals[ 0 ] = new THREE.Vector3();
  19482. if (lastBinormal===undefined) lastBinormal = new THREE.Vector3( 0, 0, 1 );
  19483. normals[ 0 ].crossVectors( lastBinormal, tangents[ 0 ] ).normalize();
  19484. binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] ).normalize();
  19485. }
  19486. function initialNormal2() {
  19487. // This uses the Frenet-Serret formula for deriving binormal
  19488. var t2 = path.getTangentAt( epsilon );
  19489. normals[ 0 ] = new THREE.Vector3().subVectors( t2, tangents[ 0 ] ).normalize();
  19490. binormals[ 0 ] = new THREE.Vector3().crossVectors( tangents[ 0 ], normals[ 0 ] );
  19491. normals[ 0 ].crossVectors( binormals[ 0 ], tangents[ 0 ] ).normalize(); // last binormal x tangent
  19492. binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] ).normalize();
  19493. }
  19494. */
  19495. function initialNormal3() {
  19496. // select an initial normal vector perpenicular to the first tangent vector,
  19497. // and in the direction of the smallest tangent xyz component
  19498. normals[ 0 ] = new THREE.Vector3();
  19499. binormals[ 0 ] = new THREE.Vector3();
  19500. smallest = Number.MAX_VALUE;
  19501. tx = Math.abs( tangents[ 0 ].x );
  19502. ty = Math.abs( tangents[ 0 ].y );
  19503. tz = Math.abs( tangents[ 0 ].z );
  19504. if ( tx <= smallest ) {
  19505. smallest = tx;
  19506. normal.set( 1, 0, 0 );
  19507. }
  19508. if ( ty <= smallest ) {
  19509. smallest = ty;
  19510. normal.set( 0, 1, 0 );
  19511. }
  19512. if ( tz <= smallest ) {
  19513. normal.set( 0, 0, 1 );
  19514. }
  19515. vec.crossVectors( tangents[ 0 ], normal ).normalize();
  19516. normals[ 0 ].crossVectors( tangents[ 0 ], vec );
  19517. binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] );
  19518. }
  19519. // compute the slowly-varying normal and binormal vectors for each segment on the path
  19520. for ( i = 1; i < numpoints; i ++ ) {
  19521. normals[ i ] = normals[ i-1 ].clone();
  19522. binormals[ i ] = binormals[ i-1 ].clone();
  19523. vec.crossVectors( tangents[ i-1 ], tangents[ i ] );
  19524. if ( vec.length() > epsilon ) {
  19525. vec.normalize();
  19526. theta = Math.acos( THREE.Math.clamp( tangents[ i-1 ].dot( tangents[ i ] ), - 1, 1 ) ); // clamp for floating pt errors
  19527. normals[ i ].applyMatrix4( mat.makeRotationAxis( vec, theta ) );
  19528. }
  19529. binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
  19530. }
  19531. // if the curve is closed, postprocess the vectors so the first and last normal vectors are the same
  19532. if ( closed ) {
  19533. theta = Math.acos( THREE.Math.clamp( normals[ 0 ].dot( normals[ numpoints-1 ] ), - 1, 1 ) );
  19534. theta /= ( numpoints - 1 );
  19535. if ( tangents[ 0 ].dot( vec.crossVectors( normals[ 0 ], normals[ numpoints-1 ] ) ) > 0 ) {
  19536. theta = - theta;
  19537. }
  19538. for ( i = 1; i < numpoints; i ++ ) {
  19539. // twist a little...
  19540. normals[ i ].applyMatrix4( mat.makeRotationAxis( tangents[ i ], theta * i ) );
  19541. binormals[ i ].crossVectors( tangents[ i ], normals[ i ] );
  19542. }
  19543. }
  19544. };
  19545. // File:src/extras/geometries/PolyhedronGeometry.js
  19546. /**
  19547. * @author clockworkgeek / https://github.com/clockworkgeek
  19548. * @author timothypratley / https://github.com/timothypratley
  19549. * @author WestLangley / http://github.com/WestLangley
  19550. */
  19551. THREE.PolyhedronGeometry = function ( vertices, indices, radius, detail ) {
  19552. THREE.Geometry.call( this );
  19553. this.type = 'PolyhedronGeometry';
  19554. this.parameters = {
  19555. vertices: vertices,
  19556. indices: indices,
  19557. radius: radius,
  19558. detail: detail
  19559. };
  19560. radius = radius || 1;
  19561. detail = detail || 0;
  19562. var that = this;
  19563. for ( var i = 0, l = vertices.length; i < l; i += 3 ) {
  19564. prepare( new THREE.Vector3( vertices[ i ], vertices[ i + 1 ], vertices[ i + 2 ] ) );
  19565. }
  19566. var midpoints = [], p = this.vertices;
  19567. var faces = [];
  19568. for ( var i = 0, j = 0, l = indices.length; i < l; i += 3, j ++ ) {
  19569. var v1 = p[ indices[ i ] ];
  19570. var v2 = p[ indices[ i + 1 ] ];
  19571. var v3 = p[ indices[ i + 2 ] ];
  19572. faces[ j ] = new THREE.Face3( v1.index, v2.index, v3.index, [ v1.clone(), v2.clone(), v3.clone() ] );
  19573. }
  19574. var centroid = new THREE.Vector3();
  19575. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  19576. subdivide( faces[ i ], detail );
  19577. }
  19578. // Handle case when face straddles the seam
  19579. for ( var i = 0, l = this.faceVertexUvs[ 0 ].length; i < l; i ++ ) {
  19580. var uvs = this.faceVertexUvs[ 0 ][ i ];
  19581. var x0 = uvs[ 0 ].x;
  19582. var x1 = uvs[ 1 ].x;
  19583. var x2 = uvs[ 2 ].x;
  19584. var max = Math.max( x0, Math.max( x1, x2 ) );
  19585. var min = Math.min( x0, Math.min( x1, x2 ) );
  19586. if ( max > 0.9 && min < 0.1 ) { // 0.9 is somewhat arbitrary
  19587. if ( x0 < 0.2 ) uvs[ 0 ].x += 1;
  19588. if ( x1 < 0.2 ) uvs[ 1 ].x += 1;
  19589. if ( x2 < 0.2 ) uvs[ 2 ].x += 1;
  19590. }
  19591. }
  19592. // Apply radius
  19593. for ( var i = 0, l = this.vertices.length; i < l; i ++ ) {
  19594. this.vertices[ i ].multiplyScalar( radius );
  19595. }
  19596. // Merge vertices
  19597. this.mergeVertices();
  19598. this.computeFaceNormals();
  19599. this.boundingSphere = new THREE.Sphere( new THREE.Vector3(), radius );
  19600. // Project vector onto sphere's surface
  19601. function prepare( vector ) {
  19602. var vertex = vector.normalize().clone();
  19603. vertex.index = that.vertices.push( vertex ) - 1;
  19604. // Texture coords are equivalent to map coords, calculate angle and convert to fraction of a circle.
  19605. var u = azimuth( vector ) / 2 / Math.PI + 0.5;
  19606. var v = inclination( vector ) / Math.PI + 0.5;
  19607. vertex.uv = new THREE.Vector2( u, 1 - v );
  19608. return vertex;
  19609. }
  19610. // Approximate a curved face with recursively sub-divided triangles.
  19611. function make( v1, v2, v3 ) {
  19612. var face = new THREE.Face3( v1.index, v2.index, v3.index, [ v1.clone(), v2.clone(), v3.clone() ] );
  19613. that.faces.push( face );
  19614. centroid.copy( v1 ).add( v2 ).add( v3 ).divideScalar( 3 );
  19615. var azi = azimuth( centroid );
  19616. that.faceVertexUvs[ 0 ].push( [
  19617. correctUV( v1.uv, v1, azi ),
  19618. correctUV( v2.uv, v2, azi ),
  19619. correctUV( v3.uv, v3, azi )
  19620. ] );
  19621. }
  19622. // Analytically subdivide a face to the required detail level.
  19623. function subdivide( face, detail ) {
  19624. var cols = Math.pow(2, detail);
  19625. var cells = Math.pow(4, detail);
  19626. var a = prepare( that.vertices[ face.a ] );
  19627. var b = prepare( that.vertices[ face.b ] );
  19628. var c = prepare( that.vertices[ face.c ] );
  19629. var v = [];
  19630. // Construct all of the vertices for this subdivision.
  19631. for ( var i = 0 ; i <= cols; i ++ ) {
  19632. v[ i ] = [];
  19633. var aj = prepare( a.clone().lerp( c, i / cols ) );
  19634. var bj = prepare( b.clone().lerp( c, i / cols ) );
  19635. var rows = cols - i;
  19636. for ( var j = 0; j <= rows; j ++) {
  19637. if ( j == 0 && i == cols ) {
  19638. v[ i ][ j ] = aj;
  19639. } else {
  19640. v[ i ][ j ] = prepare( aj.clone().lerp( bj, j / rows ) );
  19641. }
  19642. }
  19643. }
  19644. // Construct all of the faces.
  19645. for ( var i = 0; i < cols ; i ++ ) {
  19646. for ( var j = 0; j < 2 * (cols - i) - 1; j ++ ) {
  19647. var k = Math.floor( j / 2 );
  19648. if ( j % 2 == 0 ) {
  19649. make(
  19650. v[ i ][ k + 1],
  19651. v[ i + 1 ][ k ],
  19652. v[ i ][ k ]
  19653. );
  19654. } else {
  19655. make(
  19656. v[ i ][ k + 1 ],
  19657. v[ i + 1][ k + 1],
  19658. v[ i + 1 ][ k ]
  19659. );
  19660. }
  19661. }
  19662. }
  19663. }
  19664. // Angle around the Y axis, counter-clockwise when looking from above.
  19665. function azimuth( vector ) {
  19666. return Math.atan2( vector.z, - vector.x );
  19667. }
  19668. // Angle above the XZ plane.
  19669. function inclination( vector ) {
  19670. return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) );
  19671. }
  19672. // Texture fixing helper. Spheres have some odd behaviours.
  19673. function correctUV( uv, vector, azimuth ) {
  19674. if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) uv = new THREE.Vector2( uv.x - 1, uv.y );
  19675. if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) uv = new THREE.Vector2( azimuth / 2 / Math.PI + 0.5, uv.y );
  19676. return uv.clone();
  19677. }
  19678. };
  19679. THREE.PolyhedronGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19680. THREE.PolyhedronGeometry.prototype.constructor = THREE.PolyhedronGeometry;
  19681. // File:src/extras/geometries/DodecahedronGeometry.js
  19682. /**
  19683. * @author Abe Pazos / https://hamoid.com
  19684. */
  19685. THREE.DodecahedronGeometry = function ( radius, detail ) {
  19686. this.parameters = {
  19687. radius: radius,
  19688. detail: detail
  19689. };
  19690. var t = ( 1 + Math.sqrt( 5 ) ) / 2;
  19691. var r = 1 / t;
  19692. var vertices = [
  19693. // (±1, ±1, ±1)
  19694. -1, -1, -1, -1, -1, 1,
  19695. -1, 1, -1, -1, 1, 1,
  19696. 1, -1, -1, 1, -1, 1,
  19697. 1, 1, -1, 1, 1, 1,
  19698. // (0, ±1/φ, ±φ)
  19699. 0, -r, -t, 0, -r, t,
  19700. 0, r, -t, 0, r, t,
  19701. // (±1/φ, ±φ, 0)
  19702. -r, -t, 0, -r, t, 0,
  19703. r, -t, 0, r, t, 0,
  19704. // (±φ, 0, ±1/φ)
  19705. -t, 0, -r, t, 0, -r,
  19706. -t, 0, r, t, 0, r
  19707. ];
  19708. var indices = [
  19709. 3, 11, 7, 3, 7, 15, 3, 15, 13,
  19710. 7, 19, 17, 7, 17, 6, 7, 6, 15,
  19711. 17, 4, 8, 17, 8, 10, 17, 10, 6,
  19712. 8, 0, 16, 8, 16, 2, 8, 2, 10,
  19713. 0, 12, 1, 0, 1, 18, 0, 18, 16,
  19714. 6, 10, 2, 6, 2, 13, 6, 13, 15,
  19715. 2, 16, 18, 2, 18, 3, 2, 3, 13,
  19716. 18, 1, 9, 18, 9, 11, 18, 11, 3,
  19717. 4, 14, 12, 4, 12, 0, 4, 0, 8,
  19718. 11, 9, 5, 11, 5, 19, 11, 19, 7,
  19719. 19, 5, 14, 19, 14, 4, 19, 4, 17,
  19720. 1, 12, 14, 1, 14, 5, 1, 5, 9
  19721. ];
  19722. THREE.PolyhedronGeometry.call( this, vertices, indices, radius, detail );
  19723. };
  19724. THREE.DodecahedronGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19725. THREE.DodecahedronGeometry.prototype.constructor = THREE.DodecahedronGeometry;
  19726. // File:src/extras/geometries/IcosahedronGeometry.js
  19727. /**
  19728. * @author timothypratley / https://github.com/timothypratley
  19729. */
  19730. THREE.IcosahedronGeometry = function ( radius, detail ) {
  19731. var t = ( 1 + Math.sqrt( 5 ) ) / 2;
  19732. var vertices = [
  19733. - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t, 0,
  19734. 0, - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t,
  19735. t, 0, - 1, t, 0, 1, - t, 0, - 1, - t, 0, 1
  19736. ];
  19737. var indices = [
  19738. 0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11,
  19739. 1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8,
  19740. 3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9,
  19741. 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1
  19742. ];
  19743. THREE.PolyhedronGeometry.call( this, vertices, indices, radius, detail );
  19744. this.type = 'IcosahedronGeometry';
  19745. this.parameters = {
  19746. radius: radius,
  19747. detail: detail
  19748. };
  19749. };
  19750. THREE.IcosahedronGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19751. THREE.IcosahedronGeometry.prototype.constructor = THREE.IcosahedronGeometry;
  19752. // File:src/extras/geometries/OctahedronGeometry.js
  19753. /**
  19754. * @author timothypratley / https://github.com/timothypratley
  19755. */
  19756. THREE.OctahedronGeometry = function ( radius, detail ) {
  19757. this.parameters = {
  19758. radius: radius,
  19759. detail: detail
  19760. };
  19761. var vertices = [
  19762. 1, 0, 0, - 1, 0, 0, 0, 1, 0, 0,- 1, 0, 0, 0, 1, 0, 0,- 1
  19763. ];
  19764. var indices = [
  19765. 0, 2, 4, 0, 4, 3, 0, 3, 5, 0, 5, 2, 1, 2, 5, 1, 5, 3, 1, 3, 4, 1, 4, 2
  19766. ];
  19767. THREE.PolyhedronGeometry.call( this, vertices, indices, radius, detail );
  19768. this.type = 'OctahedronGeometry';
  19769. this.parameters = {
  19770. radius: radius,
  19771. detail: detail
  19772. };
  19773. };
  19774. THREE.OctahedronGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19775. THREE.OctahedronGeometry.prototype.constructor = THREE.OctahedronGeometry;
  19776. // File:src/extras/geometries/TetrahedronGeometry.js
  19777. /**
  19778. * @author timothypratley / https://github.com/timothypratley
  19779. */
  19780. THREE.TetrahedronGeometry = function ( radius, detail ) {
  19781. var vertices = [
  19782. 1, 1, 1, - 1, - 1, 1, - 1, 1, - 1, 1, - 1, - 1
  19783. ];
  19784. var indices = [
  19785. 2, 1, 0, 0, 3, 2, 1, 3, 0, 2, 3, 1
  19786. ];
  19787. THREE.PolyhedronGeometry.call( this, vertices, indices, radius, detail );
  19788. this.type = 'TetrahedronGeometry';
  19789. this.parameters = {
  19790. radius: radius,
  19791. detail: detail
  19792. };
  19793. };
  19794. THREE.TetrahedronGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19795. THREE.TetrahedronGeometry.prototype.constructor = THREE.TetrahedronGeometry;
  19796. // File:src/extras/geometries/ParametricGeometry.js
  19797. /**
  19798. * @author zz85 / https://github.com/zz85
  19799. * Parametric Surfaces Geometry
  19800. * based on the brilliant article by @prideout http://prideout.net/blog/?p=44
  19801. *
  19802. * new THREE.ParametricGeometry( parametricFunction, uSegments, ySegements );
  19803. *
  19804. */
  19805. THREE.ParametricGeometry = function ( func, slices, stacks ) {
  19806. THREE.Geometry.call( this );
  19807. this.type = 'ParametricGeometry';
  19808. this.parameters = {
  19809. func: func,
  19810. slices: slices,
  19811. stacks: stacks
  19812. };
  19813. var verts = this.vertices;
  19814. var faces = this.faces;
  19815. var uvs = this.faceVertexUvs[ 0 ];
  19816. var i, il, j, p;
  19817. var u, v;
  19818. var stackCount = stacks + 1;
  19819. var sliceCount = slices + 1;
  19820. for ( i = 0; i <= stacks; i ++ ) {
  19821. v = i / stacks;
  19822. for ( j = 0; j <= slices; j ++ ) {
  19823. u = j / slices;
  19824. p = func( u, v );
  19825. verts.push( p );
  19826. }
  19827. }
  19828. var a, b, c, d;
  19829. var uva, uvb, uvc, uvd;
  19830. for ( i = 0; i < stacks; i ++ ) {
  19831. for ( j = 0; j < slices; j ++ ) {
  19832. a = i * sliceCount + j;
  19833. b = i * sliceCount + j + 1;
  19834. c = (i + 1) * sliceCount + j + 1;
  19835. d = (i + 1) * sliceCount + j;
  19836. uva = new THREE.Vector2( j / slices, i / stacks );
  19837. uvb = new THREE.Vector2( ( j + 1 ) / slices, i / stacks );
  19838. uvc = new THREE.Vector2( ( j + 1 ) / slices, ( i + 1 ) / stacks );
  19839. uvd = new THREE.Vector2( j / slices, ( i + 1 ) / stacks );
  19840. faces.push( new THREE.Face3( a, b, d ) );
  19841. uvs.push( [ uva, uvb, uvd ] );
  19842. faces.push( new THREE.Face3( b, c, d ) );
  19843. uvs.push( [ uvb.clone(), uvc, uvd.clone() ] );
  19844. }
  19845. }
  19846. // console.log(this);
  19847. // magic bullet
  19848. // var diff = this.mergeVertices();
  19849. // console.log('removed ', diff, ' vertices by merging');
  19850. this.computeFaceNormals();
  19851. this.computeVertexNormals();
  19852. };
  19853. THREE.ParametricGeometry.prototype = Object.create( THREE.Geometry.prototype );
  19854. THREE.ParametricGeometry.prototype.constructor = THREE.ParametricGeometry;
  19855. // File:src/extras/helpers/AxisHelper.js
  19856. /**
  19857. * @author sroucheray / http://sroucheray.org/
  19858. * @author mrdoob / http://mrdoob.com/
  19859. */
  19860. THREE.AxisHelper = function ( size ) {
  19861. size = size || 1;
  19862. var vertices = new Float32Array( [
  19863. 0, 0, 0, size, 0, 0,
  19864. 0, 0, 0, 0, size, 0,
  19865. 0, 0, 0, 0, 0, size
  19866. ] );
  19867. var colors = new Float32Array( [
  19868. 1, 0, 0, 1, 0.6, 0,
  19869. 0, 1, 0, 0.6, 1, 0,
  19870. 0, 0, 1, 0, 0.6, 1
  19871. ] );
  19872. var geometry = new THREE.BufferGeometry();
  19873. geometry.addAttribute( 'position', new THREE.BufferAttribute( vertices, 3 ) );
  19874. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  19875. var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
  19876. THREE.Line.call( this, geometry, material, THREE.LinePieces );
  19877. };
  19878. THREE.AxisHelper.prototype = Object.create( THREE.Line.prototype );
  19879. THREE.AxisHelper.prototype.constructor = THREE.AxisHelper;
  19880. // File:src/extras/helpers/ArrowHelper.js
  19881. /**
  19882. * @author WestLangley / http://github.com/WestLangley
  19883. * @author zz85 / http://github.com/zz85
  19884. * @author bhouston / http://exocortex.com
  19885. *
  19886. * Creates an arrow for visualizing directions
  19887. *
  19888. * Parameters:
  19889. * dir - Vector3
  19890. * origin - Vector3
  19891. * length - Number
  19892. * color - color in hex value
  19893. * headLength - Number
  19894. * headWidth - Number
  19895. */
  19896. THREE.ArrowHelper = ( function () {
  19897. var lineGeometry = new THREE.Geometry();
  19898. lineGeometry.vertices.push( new THREE.Vector3( 0, 0, 0 ), new THREE.Vector3( 0, 1, 0 ) );
  19899. var coneGeometry = new THREE.CylinderGeometry( 0, 0.5, 1, 5, 1 );
  19900. coneGeometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, - 0.5, 0 ) );
  19901. return function ( dir, origin, length, color, headLength, headWidth ) {
  19902. // dir is assumed to be normalized
  19903. THREE.Object3D.call( this );
  19904. if ( color === undefined ) color = 0xffff00;
  19905. if ( length === undefined ) length = 1;
  19906. if ( headLength === undefined ) headLength = 0.2 * length;
  19907. if ( headWidth === undefined ) headWidth = 0.2 * headLength;
  19908. this.position.copy( origin );
  19909. this.line = new THREE.Line( lineGeometry, new THREE.LineBasicMaterial( { color: color } ) );
  19910. this.line.matrixAutoUpdate = false;
  19911. this.add( this.line );
  19912. this.cone = new THREE.Mesh( coneGeometry, new THREE.MeshBasicMaterial( { color: color } ) );
  19913. this.cone.matrixAutoUpdate = false;
  19914. this.add( this.cone );
  19915. this.setDirection( dir );
  19916. this.setLength( length, headLength, headWidth );
  19917. }
  19918. }() );
  19919. THREE.ArrowHelper.prototype = Object.create( THREE.Object3D.prototype );
  19920. THREE.ArrowHelper.prototype.constructor = THREE.ArrowHelper;
  19921. THREE.ArrowHelper.prototype.setDirection = ( function () {
  19922. var axis = new THREE.Vector3();
  19923. var radians;
  19924. return function ( dir ) {
  19925. // dir is assumed to be normalized
  19926. if ( dir.y > 0.99999 ) {
  19927. this.quaternion.set( 0, 0, 0, 1 );
  19928. } else if ( dir.y < - 0.99999 ) {
  19929. this.quaternion.set( 1, 0, 0, 0 );
  19930. } else {
  19931. axis.set( dir.z, 0, - dir.x ).normalize();
  19932. radians = Math.acos( dir.y );
  19933. this.quaternion.setFromAxisAngle( axis, radians );
  19934. }
  19935. };
  19936. }() );
  19937. THREE.ArrowHelper.prototype.setLength = function ( length, headLength, headWidth ) {
  19938. if ( headLength === undefined ) headLength = 0.2 * length;
  19939. if ( headWidth === undefined ) headWidth = 0.2 * headLength;
  19940. this.line.scale.set( 1, length - headLength, 1 );
  19941. this.line.updateMatrix();
  19942. this.cone.scale.set( headWidth, headLength, headWidth );
  19943. this.cone.position.y = length;
  19944. this.cone.updateMatrix();
  19945. };
  19946. THREE.ArrowHelper.prototype.setColor = function ( color ) {
  19947. this.line.material.color.set( color );
  19948. this.cone.material.color.set( color );
  19949. };
  19950. // File:src/extras/helpers/BoxHelper.js
  19951. /**
  19952. * @author mrdoob / http://mrdoob.com/
  19953. */
  19954. THREE.BoxHelper = function ( object ) {
  19955. var geometry = new THREE.BufferGeometry();
  19956. geometry.addAttribute( 'position', new THREE.BufferAttribute( new Float32Array( 72 ), 3 ) );
  19957. THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: 0xffff00 } ), THREE.LinePieces );
  19958. if ( object !== undefined ) {
  19959. this.update( object );
  19960. }
  19961. };
  19962. THREE.BoxHelper.prototype = Object.create( THREE.Line.prototype );
  19963. THREE.BoxHelper.prototype.constructor = THREE.BoxHelper;
  19964. THREE.BoxHelper.prototype.update = function ( object ) {
  19965. var geometry = object.geometry;
  19966. if ( geometry.boundingBox === null ) {
  19967. geometry.computeBoundingBox();
  19968. }
  19969. var min = geometry.boundingBox.min;
  19970. var max = geometry.boundingBox.max;
  19971. /*
  19972. 5____4
  19973. 1/___0/|
  19974. | 6__|_7
  19975. 2/___3/
  19976. 0: max.x, max.y, max.z
  19977. 1: min.x, max.y, max.z
  19978. 2: min.x, min.y, max.z
  19979. 3: max.x, min.y, max.z
  19980. 4: max.x, max.y, min.z
  19981. 5: min.x, max.y, min.z
  19982. 6: min.x, min.y, min.z
  19983. 7: max.x, min.y, min.z
  19984. */
  19985. var vertices = this.geometry.attributes.position.array;
  19986. vertices[ 0 ] = max.x; vertices[ 1 ] = max.y; vertices[ 2 ] = max.z;
  19987. vertices[ 3 ] = min.x; vertices[ 4 ] = max.y; vertices[ 5 ] = max.z;
  19988. vertices[ 6 ] = min.x; vertices[ 7 ] = max.y; vertices[ 8 ] = max.z;
  19989. vertices[ 9 ] = min.x; vertices[ 10 ] = min.y; vertices[ 11 ] = max.z;
  19990. vertices[ 12 ] = min.x; vertices[ 13 ] = min.y; vertices[ 14 ] = max.z;
  19991. vertices[ 15 ] = max.x; vertices[ 16 ] = min.y; vertices[ 17 ] = max.z;
  19992. vertices[ 18 ] = max.x; vertices[ 19 ] = min.y; vertices[ 20 ] = max.z;
  19993. vertices[ 21 ] = max.x; vertices[ 22 ] = max.y; vertices[ 23 ] = max.z;
  19994. //
  19995. vertices[ 24 ] = max.x; vertices[ 25 ] = max.y; vertices[ 26 ] = min.z;
  19996. vertices[ 27 ] = min.x; vertices[ 28 ] = max.y; vertices[ 29 ] = min.z;
  19997. vertices[ 30 ] = min.x; vertices[ 31 ] = max.y; vertices[ 32 ] = min.z;
  19998. vertices[ 33 ] = min.x; vertices[ 34 ] = min.y; vertices[ 35 ] = min.z;
  19999. vertices[ 36 ] = min.x; vertices[ 37 ] = min.y; vertices[ 38 ] = min.z;
  20000. vertices[ 39 ] = max.x; vertices[ 40 ] = min.y; vertices[ 41 ] = min.z;
  20001. vertices[ 42 ] = max.x; vertices[ 43 ] = min.y; vertices[ 44 ] = min.z;
  20002. vertices[ 45 ] = max.x; vertices[ 46 ] = max.y; vertices[ 47 ] = min.z;
  20003. //
  20004. vertices[ 48 ] = max.x; vertices[ 49 ] = max.y; vertices[ 50 ] = max.z;
  20005. vertices[ 51 ] = max.x; vertices[ 52 ] = max.y; vertices[ 53 ] = min.z;
  20006. vertices[ 54 ] = min.x; vertices[ 55 ] = max.y; vertices[ 56 ] = max.z;
  20007. vertices[ 57 ] = min.x; vertices[ 58 ] = max.y; vertices[ 59 ] = min.z;
  20008. vertices[ 60 ] = min.x; vertices[ 61 ] = min.y; vertices[ 62 ] = max.z;
  20009. vertices[ 63 ] = min.x; vertices[ 64 ] = min.y; vertices[ 65 ] = min.z;
  20010. vertices[ 66 ] = max.x; vertices[ 67 ] = min.y; vertices[ 68 ] = max.z;
  20011. vertices[ 69 ] = max.x; vertices[ 70 ] = min.y; vertices[ 71 ] = min.z;
  20012. this.geometry.attributes.position.needsUpdate = true;
  20013. this.geometry.computeBoundingSphere();
  20014. this.matrix = object.matrixWorld;
  20015. this.matrixAutoUpdate = false;
  20016. };
  20017. // File:src/extras/helpers/BoundingBoxHelper.js
  20018. /**
  20019. * @author WestLangley / http://github.com/WestLangley
  20020. */
  20021. // a helper to show the world-axis-aligned bounding box for an object
  20022. THREE.BoundingBoxHelper = function ( object, hex ) {
  20023. var color = ( hex !== undefined ) ? hex : 0x888888;
  20024. this.object = object;
  20025. this.box = new THREE.Box3();
  20026. THREE.Mesh.call( this, new THREE.BoxGeometry( 1, 1, 1 ), new THREE.MeshBasicMaterial( { color: color, wireframe: true } ) );
  20027. };
  20028. THREE.BoundingBoxHelper.prototype = Object.create( THREE.Mesh.prototype );
  20029. THREE.BoundingBoxHelper.prototype.constructor = THREE.BoundingBoxHelper;
  20030. THREE.BoundingBoxHelper.prototype.update = function () {
  20031. this.box.setFromObject( this.object );
  20032. this.box.size( this.scale );
  20033. this.box.center( this.position );
  20034. };
  20035. // File:src/extras/helpers/CameraHelper.js
  20036. /**
  20037. * @author alteredq / http://alteredqualia.com/
  20038. *
  20039. * - shows frustum, line of sight and up of the camera
  20040. * - suitable for fast updates
  20041. * - based on frustum visualization in lightgl.js shadowmap example
  20042. * http://evanw.github.com/lightgl.js/tests/shadowmap.html
  20043. */
  20044. THREE.CameraHelper = function ( camera ) {
  20045. var geometry = new THREE.Geometry();
  20046. var material = new THREE.LineBasicMaterial( { color: 0xffffff, vertexColors: THREE.FaceColors } );
  20047. var pointMap = {};
  20048. // colors
  20049. var hexFrustum = 0xffaa00;
  20050. var hexCone = 0xff0000;
  20051. var hexUp = 0x00aaff;
  20052. var hexTarget = 0xffffff;
  20053. var hexCross = 0x333333;
  20054. // near
  20055. addLine( "n1", "n2", hexFrustum );
  20056. addLine( "n2", "n4", hexFrustum );
  20057. addLine( "n4", "n3", hexFrustum );
  20058. addLine( "n3", "n1", hexFrustum );
  20059. // far
  20060. addLine( "f1", "f2", hexFrustum );
  20061. addLine( "f2", "f4", hexFrustum );
  20062. addLine( "f4", "f3", hexFrustum );
  20063. addLine( "f3", "f1", hexFrustum );
  20064. // sides
  20065. addLine( "n1", "f1", hexFrustum );
  20066. addLine( "n2", "f2", hexFrustum );
  20067. addLine( "n3", "f3", hexFrustum );
  20068. addLine( "n4", "f4", hexFrustum );
  20069. // cone
  20070. addLine( "p", "n1", hexCone );
  20071. addLine( "p", "n2", hexCone );
  20072. addLine( "p", "n3", hexCone );
  20073. addLine( "p", "n4", hexCone );
  20074. // up
  20075. addLine( "u1", "u2", hexUp );
  20076. addLine( "u2", "u3", hexUp );
  20077. addLine( "u3", "u1", hexUp );
  20078. // target
  20079. addLine( "c", "t", hexTarget );
  20080. addLine( "p", "c", hexCross );
  20081. // cross
  20082. addLine( "cn1", "cn2", hexCross );
  20083. addLine( "cn3", "cn4", hexCross );
  20084. addLine( "cf1", "cf2", hexCross );
  20085. addLine( "cf3", "cf4", hexCross );
  20086. function addLine( a, b, hex ) {
  20087. addPoint( a, hex );
  20088. addPoint( b, hex );
  20089. }
  20090. function addPoint( id, hex ) {
  20091. geometry.vertices.push( new THREE.Vector3() );
  20092. geometry.colors.push( new THREE.Color( hex ) );
  20093. if ( pointMap[ id ] === undefined ) {
  20094. pointMap[ id ] = [];
  20095. }
  20096. pointMap[ id ].push( geometry.vertices.length - 1 );
  20097. }
  20098. THREE.Line.call( this, geometry, material, THREE.LinePieces );
  20099. this.camera = camera;
  20100. this.matrix = camera.matrixWorld;
  20101. this.matrixAutoUpdate = false;
  20102. this.pointMap = pointMap;
  20103. this.update();
  20104. };
  20105. THREE.CameraHelper.prototype = Object.create( THREE.Line.prototype );
  20106. THREE.CameraHelper.prototype.constructor = THREE.CameraHelper;
  20107. THREE.CameraHelper.prototype.update = function () {
  20108. var geometry, pointMap;
  20109. var vector = new THREE.Vector3();
  20110. var camera = new THREE.Camera();
  20111. var setPoint = function ( point, x, y, z ) {
  20112. vector.set( x, y, z ).unproject( camera );
  20113. var points = pointMap[ point ];
  20114. if ( points !== undefined ) {
  20115. for ( var i = 0, il = points.length; i < il; i ++ ) {
  20116. geometry.vertices[ points[ i ] ].copy( vector );
  20117. }
  20118. }
  20119. };
  20120. return function () {
  20121. geometry = this.geometry;
  20122. pointMap = this.pointMap;
  20123. var w = 1, h = 1;
  20124. // we need just camera projection matrix
  20125. // world matrix must be identity
  20126. camera.projectionMatrix.copy( this.camera.projectionMatrix );
  20127. // center / target
  20128. setPoint( "c", 0, 0, - 1 );
  20129. setPoint( "t", 0, 0, 1 );
  20130. // near
  20131. setPoint( "n1", - w, - h, - 1 );
  20132. setPoint( "n2", w, - h, - 1 );
  20133. setPoint( "n3", - w, h, - 1 );
  20134. setPoint( "n4", w, h, - 1 );
  20135. // far
  20136. setPoint( "f1", - w, - h, 1 );
  20137. setPoint( "f2", w, - h, 1 );
  20138. setPoint( "f3", - w, h, 1 );
  20139. setPoint( "f4", w, h, 1 );
  20140. // up
  20141. setPoint( "u1", w * 0.7, h * 1.1, - 1 );
  20142. setPoint( "u2", - w * 0.7, h * 1.1, - 1 );
  20143. setPoint( "u3", 0, h * 2, - 1 );
  20144. // cross
  20145. setPoint( "cf1", - w, 0, 1 );
  20146. setPoint( "cf2", w, 0, 1 );
  20147. setPoint( "cf3", 0, - h, 1 );
  20148. setPoint( "cf4", 0, h, 1 );
  20149. setPoint( "cn1", - w, 0, - 1 );
  20150. setPoint( "cn2", w, 0, - 1 );
  20151. setPoint( "cn3", 0, - h, - 1 );
  20152. setPoint( "cn4", 0, h, - 1 );
  20153. geometry.verticesNeedUpdate = true;
  20154. };
  20155. }();
  20156. // File:src/extras/helpers/DirectionalLightHelper.js
  20157. /**
  20158. * @author alteredq / http://alteredqualia.com/
  20159. * @author mrdoob / http://mrdoob.com/
  20160. * @author WestLangley / http://github.com/WestLangley
  20161. */
  20162. THREE.DirectionalLightHelper = function ( light, size ) {
  20163. THREE.Object3D.call( this );
  20164. this.light = light;
  20165. this.light.updateMatrixWorld();
  20166. this.matrix = light.matrixWorld;
  20167. this.matrixAutoUpdate = false;
  20168. size = size || 1;
  20169. var geometry = new THREE.Geometry();
  20170. geometry.vertices.push(
  20171. new THREE.Vector3( - size, size, 0 ),
  20172. new THREE.Vector3( size, size, 0 ),
  20173. new THREE.Vector3( size, - size, 0 ),
  20174. new THREE.Vector3( - size, - size, 0 ),
  20175. new THREE.Vector3( - size, size, 0 )
  20176. );
  20177. var material = new THREE.LineBasicMaterial( { fog: false } );
  20178. material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  20179. this.lightPlane = new THREE.Line( geometry, material );
  20180. this.add( this.lightPlane );
  20181. geometry = new THREE.Geometry();
  20182. geometry.vertices.push(
  20183. new THREE.Vector3(),
  20184. new THREE.Vector3()
  20185. );
  20186. material = new THREE.LineBasicMaterial( { fog: false } );
  20187. material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  20188. this.targetLine = new THREE.Line( geometry, material );
  20189. this.add( this.targetLine );
  20190. this.update();
  20191. };
  20192. THREE.DirectionalLightHelper.prototype = Object.create( THREE.Object3D.prototype );
  20193. THREE.DirectionalLightHelper.prototype.constructor = THREE.DirectionalLightHelper;
  20194. THREE.DirectionalLightHelper.prototype.dispose = function () {
  20195. this.lightPlane.geometry.dispose();
  20196. this.lightPlane.material.dispose();
  20197. this.targetLine.geometry.dispose();
  20198. this.targetLine.material.dispose();
  20199. };
  20200. THREE.DirectionalLightHelper.prototype.update = function () {
  20201. var v1 = new THREE.Vector3();
  20202. var v2 = new THREE.Vector3();
  20203. var v3 = new THREE.Vector3();
  20204. return function () {
  20205. v1.setFromMatrixPosition( this.light.matrixWorld );
  20206. v2.setFromMatrixPosition( this.light.target.matrixWorld );
  20207. v3.subVectors( v2, v1 );
  20208. this.lightPlane.lookAt( v3 );
  20209. this.lightPlane.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  20210. this.targetLine.geometry.vertices[ 1 ].copy( v3 );
  20211. this.targetLine.geometry.verticesNeedUpdate = true;
  20212. this.targetLine.material.color.copy( this.lightPlane.material.color );
  20213. };
  20214. }();
  20215. // File:src/extras/helpers/EdgesHelper.js
  20216. /**
  20217. * @author WestLangley / http://github.com/WestLangley
  20218. */
  20219. THREE.EdgesHelper = function ( object, hex ) {
  20220. var color = ( hex !== undefined ) ? hex : 0xffffff;
  20221. var edge = [ 0, 0 ], hash = {};
  20222. var sortFunction = function ( a, b ) { return a - b };
  20223. var keys = [ 'a', 'b', 'c' ];
  20224. var geometry = new THREE.BufferGeometry();
  20225. var geometry2 = object.geometry.clone();
  20226. geometry2.mergeVertices();
  20227. geometry2.computeFaceNormals();
  20228. var vertices = geometry2.vertices;
  20229. var faces = geometry2.faces;
  20230. var numEdges = 0;
  20231. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  20232. var face = faces[ i ];
  20233. for ( var j = 0; j < 3; j ++ ) {
  20234. edge[ 0 ] = face[ keys[ j ] ];
  20235. edge[ 1 ] = face[ keys[ ( j + 1 ) % 3 ] ];
  20236. edge.sort( sortFunction );
  20237. var key = edge.toString();
  20238. if ( hash[ key ] === undefined ) {
  20239. hash[ key ] = { vert1: edge[ 0 ], vert2: edge[ 1 ], face1: i, face2: undefined };
  20240. numEdges ++;
  20241. } else {
  20242. hash[ key ].face2 = i;
  20243. }
  20244. }
  20245. }
  20246. var coords = new Float32Array( numEdges * 2 * 3 );
  20247. var index = 0;
  20248. for ( var key in hash ) {
  20249. var h = hash[ key ];
  20250. if ( h.face2 === undefined || faces[ h.face1 ].normal.dot( faces[ h.face2 ].normal ) < 0.9999 ) { // hardwired const OK
  20251. var vertex = vertices[ h.vert1 ];
  20252. coords[ index ++ ] = vertex.x;
  20253. coords[ index ++ ] = vertex.y;
  20254. coords[ index ++ ] = vertex.z;
  20255. vertex = vertices[ h.vert2 ];
  20256. coords[ index ++ ] = vertex.x;
  20257. coords[ index ++ ] = vertex.y;
  20258. coords[ index ++ ] = vertex.z;
  20259. }
  20260. }
  20261. geometry.addAttribute( 'position', new THREE.BufferAttribute( coords, 3 ) );
  20262. THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color } ), THREE.LinePieces );
  20263. this.matrix = object.matrixWorld;
  20264. this.matrixAutoUpdate = false;
  20265. };
  20266. THREE.EdgesHelper.prototype = Object.create( THREE.Line.prototype );
  20267. THREE.EdgesHelper.prototype.constructor = THREE.EdgesHelper;
  20268. // File:src/extras/helpers/FaceNormalsHelper.js
  20269. /**
  20270. * @author mrdoob / http://mrdoob.com/
  20271. * @author WestLangley / http://github.com/WestLangley
  20272. */
  20273. THREE.FaceNormalsHelper = function ( object, size, hex, linewidth ) {
  20274. this.object = object;
  20275. this.size = ( size !== undefined ) ? size : 1;
  20276. var color = ( hex !== undefined ) ? hex : 0xffff00;
  20277. var width = ( linewidth !== undefined ) ? linewidth : 1;
  20278. var geometry = new THREE.Geometry();
  20279. var faces = this.object.geometry.faces;
  20280. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  20281. geometry.vertices.push( new THREE.Vector3(), new THREE.Vector3() );
  20282. }
  20283. THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color, linewidth: width } ), THREE.LinePieces );
  20284. this.matrixAutoUpdate = false;
  20285. this.normalMatrix = new THREE.Matrix3();
  20286. this.update();
  20287. };
  20288. THREE.FaceNormalsHelper.prototype = Object.create( THREE.Line.prototype );
  20289. THREE.FaceNormalsHelper.prototype.constructor = THREE.FaceNormalsHelper;
  20290. THREE.FaceNormalsHelper.prototype.update = function () {
  20291. var vertices = this.geometry.vertices;
  20292. var object = this.object;
  20293. var objectVertices = object.geometry.vertices;
  20294. var objectFaces = object.geometry.faces;
  20295. var objectWorldMatrix = object.matrixWorld;
  20296. object.updateMatrixWorld( true );
  20297. this.normalMatrix.getNormalMatrix( objectWorldMatrix );
  20298. for ( var i = 0, i2 = 0, l = objectFaces.length; i < l; i ++, i2 += 2 ) {
  20299. var face = objectFaces[ i ];
  20300. vertices[ i2 ].copy( objectVertices[ face.a ] )
  20301. .add( objectVertices[ face.b ] )
  20302. .add( objectVertices[ face.c ] )
  20303. .divideScalar( 3 )
  20304. .applyMatrix4( objectWorldMatrix );
  20305. vertices[ i2 + 1 ].copy( face.normal )
  20306. .applyMatrix3( this.normalMatrix )
  20307. .normalize()
  20308. .multiplyScalar( this.size )
  20309. .add( vertices[ i2 ] );
  20310. }
  20311. this.geometry.verticesNeedUpdate = true;
  20312. return this;
  20313. };
  20314. // File:src/extras/helpers/GridHelper.js
  20315. /**
  20316. * @author mrdoob / http://mrdoob.com/
  20317. */
  20318. THREE.GridHelper = function ( size, step ) {
  20319. var geometry = new THREE.Geometry();
  20320. var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors } );
  20321. this.color1 = new THREE.Color( 0x444444 );
  20322. this.color2 = new THREE.Color( 0x888888 );
  20323. for ( var i = - size; i <= size; i += step ) {
  20324. geometry.vertices.push(
  20325. new THREE.Vector3( - size, 0, i ), new THREE.Vector3( size, 0, i ),
  20326. new THREE.Vector3( i, 0, - size ), new THREE.Vector3( i, 0, size )
  20327. );
  20328. var color = i === 0 ? this.color1 : this.color2;
  20329. geometry.colors.push( color, color, color, color );
  20330. }
  20331. THREE.Line.call( this, geometry, material, THREE.LinePieces );
  20332. };
  20333. THREE.GridHelper.prototype = Object.create( THREE.Line.prototype );
  20334. THREE.GridHelper.prototype.constructor = THREE.GridHelper;
  20335. THREE.GridHelper.prototype.setColors = function( colorCenterLine, colorGrid ) {
  20336. this.color1.set( colorCenterLine );
  20337. this.color2.set( colorGrid );
  20338. this.geometry.colorsNeedUpdate = true;
  20339. }
  20340. // File:src/extras/helpers/HemisphereLightHelper.js
  20341. /**
  20342. * @author alteredq / http://alteredqualia.com/
  20343. * @author mrdoob / http://mrdoob.com/
  20344. */
  20345. THREE.HemisphereLightHelper = function ( light, sphereSize, arrowLength, domeSize ) {
  20346. THREE.Object3D.call( this );
  20347. this.light = light;
  20348. this.light.updateMatrixWorld();
  20349. this.matrix = light.matrixWorld;
  20350. this.matrixAutoUpdate = false;
  20351. this.colors = [ new THREE.Color(), new THREE.Color() ];
  20352. var geometry = new THREE.SphereGeometry( sphereSize, 4, 2 );
  20353. geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
  20354. for ( var i = 0, il = 8; i < il; i ++ ) {
  20355. geometry.faces[ i ].color = this.colors[ i < 4 ? 0 : 1 ];
  20356. }
  20357. var material = new THREE.MeshBasicMaterial( { vertexColors: THREE.FaceColors, wireframe: true } );
  20358. this.lightSphere = new THREE.Mesh( geometry, material );
  20359. this.add( this.lightSphere );
  20360. this.update();
  20361. };
  20362. THREE.HemisphereLightHelper.prototype = Object.create( THREE.Object3D.prototype );
  20363. THREE.HemisphereLightHelper.prototype.constructor = THREE.HemisphereLightHelper;
  20364. THREE.HemisphereLightHelper.prototype.dispose = function () {
  20365. this.lightSphere.geometry.dispose();
  20366. this.lightSphere.material.dispose();
  20367. };
  20368. THREE.HemisphereLightHelper.prototype.update = function () {
  20369. var vector = new THREE.Vector3();
  20370. return function () {
  20371. this.colors[ 0 ].copy( this.light.color ).multiplyScalar( this.light.intensity );
  20372. this.colors[ 1 ].copy( this.light.groundColor ).multiplyScalar( this.light.intensity );
  20373. this.lightSphere.lookAt( vector.setFromMatrixPosition( this.light.matrixWorld ).negate() );
  20374. this.lightSphere.geometry.colorsNeedUpdate = true;
  20375. }
  20376. }();
  20377. // File:src/extras/helpers/PointLightHelper.js
  20378. /**
  20379. * @author alteredq / http://alteredqualia.com/
  20380. * @author mrdoob / http://mrdoob.com/
  20381. */
  20382. THREE.PointLightHelper = function ( light, sphereSize ) {
  20383. this.light = light;
  20384. this.light.updateMatrixWorld();
  20385. var geometry = new THREE.SphereGeometry( sphereSize, 4, 2 );
  20386. var material = new THREE.MeshBasicMaterial( { wireframe: true, fog: false } );
  20387. material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  20388. THREE.Mesh.call( this, geometry, material );
  20389. this.matrix = this.light.matrixWorld;
  20390. this.matrixAutoUpdate = false;
  20391. /*
  20392. var distanceGeometry = new THREE.IcosahedronGeometry( 1, 2 );
  20393. var distanceMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false, wireframe: true, opacity: 0.1, transparent: true } );
  20394. this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial );
  20395. this.lightDistance = new THREE.Mesh( distanceGeometry, distanceMaterial );
  20396. var d = light.distance;
  20397. if ( d === 0.0 ) {
  20398. this.lightDistance.visible = false;
  20399. } else {
  20400. this.lightDistance.scale.set( d, d, d );
  20401. }
  20402. this.add( this.lightDistance );
  20403. */
  20404. };
  20405. THREE.PointLightHelper.prototype = Object.create( THREE.Mesh.prototype );
  20406. THREE.PointLightHelper.prototype.constructor = THREE.PointLightHelper;
  20407. THREE.PointLightHelper.prototype.dispose = function () {
  20408. this.geometry.dispose();
  20409. this.material.dispose();
  20410. };
  20411. THREE.PointLightHelper.prototype.update = function () {
  20412. this.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  20413. /*
  20414. var d = this.light.distance;
  20415. if ( d === 0.0 ) {
  20416. this.lightDistance.visible = false;
  20417. } else {
  20418. this.lightDistance.visible = true;
  20419. this.lightDistance.scale.set( d, d, d );
  20420. }
  20421. */
  20422. };
  20423. // File:src/extras/helpers/SkeletonHelper.js
  20424. /**
  20425. * @author Sean Griffin / http://twitter.com/sgrif
  20426. * @author Michael Guerrero / http://realitymeltdown.com
  20427. * @author mrdoob / http://mrdoob.com/
  20428. * @author ikerr / http://verold.com
  20429. */
  20430. THREE.SkeletonHelper = function ( object ) {
  20431. this.bones = this.getBoneList( object );
  20432. var geometry = new THREE.Geometry();
  20433. for ( var i = 0; i < this.bones.length; i ++ ) {
  20434. var bone = this.bones[ i ];
  20435. if ( bone.parent instanceof THREE.Bone ) {
  20436. geometry.vertices.push( new THREE.Vector3() );
  20437. geometry.vertices.push( new THREE.Vector3() );
  20438. geometry.colors.push( new THREE.Color( 0, 0, 1 ) );
  20439. geometry.colors.push( new THREE.Color( 0, 1, 0 ) );
  20440. }
  20441. }
  20442. var material = new THREE.LineBasicMaterial( { vertexColors: THREE.VertexColors, depthTest: false, depthWrite: false, transparent: true } );
  20443. THREE.Line.call( this, geometry, material, THREE.LinePieces );
  20444. this.root = object;
  20445. this.matrix = object.matrixWorld;
  20446. this.matrixAutoUpdate = false;
  20447. this.update();
  20448. };
  20449. THREE.SkeletonHelper.prototype = Object.create( THREE.Line.prototype );
  20450. THREE.SkeletonHelper.prototype.constructor = THREE.SkeletonHelper;
  20451. THREE.SkeletonHelper.prototype.getBoneList = function( object ) {
  20452. var boneList = [];
  20453. if ( object instanceof THREE.Bone ) {
  20454. boneList.push( object );
  20455. }
  20456. for ( var i = 0; i < object.children.length; i ++ ) {
  20457. boneList.push.apply( boneList, this.getBoneList( object.children[ i ] ) );
  20458. }
  20459. return boneList;
  20460. };
  20461. THREE.SkeletonHelper.prototype.update = function () {
  20462. var geometry = this.geometry;
  20463. var matrixWorldInv = new THREE.Matrix4().getInverse( this.root.matrixWorld );
  20464. var boneMatrix = new THREE.Matrix4();
  20465. var j = 0;
  20466. for ( var i = 0; i < this.bones.length; i ++ ) {
  20467. var bone = this.bones[ i ];
  20468. if ( bone.parent instanceof THREE.Bone ) {
  20469. boneMatrix.multiplyMatrices( matrixWorldInv, bone.matrixWorld );
  20470. geometry.vertices[ j ].setFromMatrixPosition( boneMatrix );
  20471. boneMatrix.multiplyMatrices( matrixWorldInv, bone.parent.matrixWorld );
  20472. geometry.vertices[ j + 1 ].setFromMatrixPosition( boneMatrix );
  20473. j += 2;
  20474. }
  20475. }
  20476. geometry.verticesNeedUpdate = true;
  20477. geometry.computeBoundingSphere();
  20478. };
  20479. // File:src/extras/helpers/SpotLightHelper.js
  20480. /**
  20481. * @author alteredq / http://alteredqualia.com/
  20482. * @author mrdoob / http://mrdoob.com/
  20483. * @author WestLangley / http://github.com/WestLangley
  20484. */
  20485. THREE.SpotLightHelper = function ( light ) {
  20486. THREE.Object3D.call( this );
  20487. this.light = light;
  20488. this.light.updateMatrixWorld();
  20489. this.matrix = light.matrixWorld;
  20490. this.matrixAutoUpdate = false;
  20491. var geometry = new THREE.CylinderGeometry( 0, 1, 1, 8, 1, true );
  20492. geometry.applyMatrix( new THREE.Matrix4().makeTranslation( 0, - 0.5, 0 ) );
  20493. geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
  20494. var material = new THREE.MeshBasicMaterial( { wireframe: true, fog: false } );
  20495. this.cone = new THREE.Mesh( geometry, material );
  20496. this.add( this.cone );
  20497. this.update();
  20498. };
  20499. THREE.SpotLightHelper.prototype = Object.create( THREE.Object3D.prototype );
  20500. THREE.SpotLightHelper.prototype.constructor = THREE.SpotLightHelper;
  20501. THREE.SpotLightHelper.prototype.dispose = function () {
  20502. this.cone.geometry.dispose();
  20503. this.cone.material.dispose();
  20504. };
  20505. THREE.SpotLightHelper.prototype.update = function () {
  20506. var vector = new THREE.Vector3();
  20507. var vector2 = new THREE.Vector3();
  20508. return function () {
  20509. var coneLength = this.light.distance ? this.light.distance : 10000;
  20510. var coneWidth = coneLength * Math.tan( this.light.angle );
  20511. this.cone.scale.set( coneWidth, coneWidth, coneLength );
  20512. vector.setFromMatrixPosition( this.light.matrixWorld );
  20513. vector2.setFromMatrixPosition( this.light.target.matrixWorld );
  20514. this.cone.lookAt( vector2.sub( vector ) );
  20515. this.cone.material.color.copy( this.light.color ).multiplyScalar( this.light.intensity );
  20516. };
  20517. }();
  20518. // File:src/extras/helpers/VertexNormalsHelper.js
  20519. /**
  20520. * @author mrdoob / http://mrdoob.com/
  20521. * @author WestLangley / http://github.com/WestLangley
  20522. */
  20523. THREE.VertexNormalsHelper = function ( object, size, hex, linewidth ) {
  20524. this.object = object;
  20525. this.size = ( size !== undefined ) ? size : 1;
  20526. var color = ( hex !== undefined ) ? hex : 0xff0000;
  20527. var width = ( linewidth !== undefined ) ? linewidth : 1;
  20528. var geometry = new THREE.Geometry();
  20529. var vertices = object.geometry.vertices;
  20530. var faces = object.geometry.faces;
  20531. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  20532. var face = faces[ i ];
  20533. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  20534. geometry.vertices.push( new THREE.Vector3(), new THREE.Vector3() );
  20535. }
  20536. }
  20537. THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color, linewidth: width } ), THREE.LinePieces );
  20538. this.matrixAutoUpdate = false;
  20539. this.normalMatrix = new THREE.Matrix3();
  20540. this.update();
  20541. };
  20542. THREE.VertexNormalsHelper.prototype = Object.create( THREE.Line.prototype );
  20543. THREE.VertexNormalsHelper.prototype.constructor = THREE.VertexNormalsHelper;
  20544. THREE.VertexNormalsHelper.prototype.update = ( function ( object ) {
  20545. var v1 = new THREE.Vector3();
  20546. return function( object ) {
  20547. var keys = [ 'a', 'b', 'c', 'd' ];
  20548. this.object.updateMatrixWorld( true );
  20549. this.normalMatrix.getNormalMatrix( this.object.matrixWorld );
  20550. var vertices = this.geometry.vertices;
  20551. var verts = this.object.geometry.vertices;
  20552. var faces = this.object.geometry.faces;
  20553. var worldMatrix = this.object.matrixWorld;
  20554. var idx = 0;
  20555. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  20556. var face = faces[ i ];
  20557. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  20558. var vertexId = face[ keys[ j ] ];
  20559. var vertex = verts[ vertexId ];
  20560. var normal = face.vertexNormals[ j ];
  20561. vertices[ idx ].copy( vertex ).applyMatrix4( worldMatrix );
  20562. v1.copy( normal ).applyMatrix3( this.normalMatrix ).normalize().multiplyScalar( this.size );
  20563. v1.add( vertices[ idx ] );
  20564. idx = idx + 1;
  20565. vertices[ idx ].copy( v1 );
  20566. idx = idx + 1;
  20567. }
  20568. }
  20569. this.geometry.verticesNeedUpdate = true;
  20570. return this;
  20571. }
  20572. }());
  20573. // File:src/extras/helpers/VertexTangentsHelper.js
  20574. /**
  20575. * @author mrdoob / http://mrdoob.com/
  20576. * @author WestLangley / http://github.com/WestLangley
  20577. */
  20578. THREE.VertexTangentsHelper = function ( object, size, hex, linewidth ) {
  20579. this.object = object;
  20580. this.size = ( size !== undefined ) ? size : 1;
  20581. var color = ( hex !== undefined ) ? hex : 0x0000ff;
  20582. var width = ( linewidth !== undefined ) ? linewidth : 1;
  20583. var geometry = new THREE.Geometry();
  20584. var vertices = object.geometry.vertices;
  20585. var faces = object.geometry.faces;
  20586. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  20587. var face = faces[ i ];
  20588. for ( var j = 0, jl = face.vertexTangents.length; j < jl; j ++ ) {
  20589. geometry.vertices.push( new THREE.Vector3() );
  20590. geometry.vertices.push( new THREE.Vector3() );
  20591. }
  20592. }
  20593. THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color, linewidth: width } ), THREE.LinePieces );
  20594. this.matrixAutoUpdate = false;
  20595. this.update();
  20596. };
  20597. THREE.VertexTangentsHelper.prototype = Object.create( THREE.Line.prototype );
  20598. THREE.VertexTangentsHelper.prototype.constructor = THREE.VertexTangentsHelper;
  20599. THREE.VertexTangentsHelper.prototype.update = ( function ( object ) {
  20600. var v1 = new THREE.Vector3();
  20601. return function( object ) {
  20602. var keys = [ 'a', 'b', 'c', 'd' ];
  20603. this.object.updateMatrixWorld( true );
  20604. var vertices = this.geometry.vertices;
  20605. var verts = this.object.geometry.vertices;
  20606. var faces = this.object.geometry.faces;
  20607. var worldMatrix = this.object.matrixWorld;
  20608. var idx = 0;
  20609. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  20610. var face = faces[ i ];
  20611. for ( var j = 0, jl = face.vertexTangents.length; j < jl; j ++ ) {
  20612. var vertexId = face[ keys[ j ] ];
  20613. var vertex = verts[ vertexId ];
  20614. var tangent = face.vertexTangents[ j ];
  20615. vertices[ idx ].copy( vertex ).applyMatrix4( worldMatrix );
  20616. v1.copy( tangent ).transformDirection( worldMatrix ).multiplyScalar( this.size );
  20617. v1.add( vertices[ idx ] );
  20618. idx = idx + 1;
  20619. vertices[ idx ].copy( v1 );
  20620. idx = idx + 1;
  20621. }
  20622. }
  20623. this.geometry.verticesNeedUpdate = true;
  20624. return this;
  20625. }
  20626. }());
  20627. // File:src/extras/helpers/WireframeHelper.js
  20628. /**
  20629. * @author mrdoob / http://mrdoob.com/
  20630. */
  20631. THREE.WireframeHelper = function ( object, hex ) {
  20632. var color = ( hex !== undefined ) ? hex : 0xffffff;
  20633. var edge = [ 0, 0 ], hash = {};
  20634. var sortFunction = function ( a, b ) { return a - b };
  20635. var keys = [ 'a', 'b', 'c' ];
  20636. var geometry = new THREE.BufferGeometry();
  20637. if ( object.geometry instanceof THREE.Geometry ) {
  20638. var vertices = object.geometry.vertices;
  20639. var faces = object.geometry.faces;
  20640. var numEdges = 0;
  20641. // allocate maximal size
  20642. var edges = new Uint32Array( 6 * faces.length );
  20643. for ( var i = 0, l = faces.length; i < l; i ++ ) {
  20644. var face = faces[ i ];
  20645. for ( var j = 0; j < 3; j ++ ) {
  20646. edge[ 0 ] = face[ keys[ j ] ];
  20647. edge[ 1 ] = face[ keys[ ( j + 1 ) % 3 ] ];
  20648. edge.sort( sortFunction );
  20649. var key = edge.toString();
  20650. if ( hash[ key ] === undefined ) {
  20651. edges[ 2 * numEdges ] = edge[ 0 ];
  20652. edges[ 2 * numEdges + 1 ] = edge[ 1 ];
  20653. hash[ key ] = true;
  20654. numEdges ++;
  20655. }
  20656. }
  20657. }
  20658. var coords = new Float32Array( numEdges * 2 * 3 );
  20659. for ( var i = 0, l = numEdges; i < l; i ++ ) {
  20660. for ( var j = 0; j < 2; j ++ ) {
  20661. var vertex = vertices[ edges [ 2 * i + j] ];
  20662. var index = 6 * i + 3 * j;
  20663. coords[ index + 0 ] = vertex.x;
  20664. coords[ index + 1 ] = vertex.y;
  20665. coords[ index + 2 ] = vertex.z;
  20666. }
  20667. }
  20668. geometry.addAttribute( 'position', new THREE.BufferAttribute( coords, 3 ) );
  20669. } else if ( object.geometry instanceof THREE.BufferGeometry ) {
  20670. if ( object.geometry.attributes.index !== undefined ) { // Indexed BufferGeometry
  20671. var vertices = object.geometry.attributes.position.array;
  20672. var indices = object.geometry.attributes.index.array;
  20673. var drawcalls = object.geometry.drawcalls;
  20674. var numEdges = 0;
  20675. if ( drawcalls.length === 0 ) {
  20676. drawcalls = [ { count : indices.length, index : 0, start : 0 } ];
  20677. }
  20678. // allocate maximal size
  20679. var edges = new Uint32Array( 2 * indices.length );
  20680. for ( var o = 0, ol = drawcalls.length; o < ol; ++ o ) {
  20681. var start = drawcalls[ o ].start;
  20682. var count = drawcalls[ o ].count;
  20683. var index = drawcalls[ o ].index;
  20684. for ( var i = start, il = start + count; i < il; i += 3 ) {
  20685. for ( var j = 0; j < 3; j ++ ) {
  20686. edge[ 0 ] = index + indices[ i + j ];
  20687. edge[ 1 ] = index + indices[ i + ( j + 1 ) % 3 ];
  20688. edge.sort( sortFunction );
  20689. var key = edge.toString();
  20690. if ( hash[ key ] === undefined ) {
  20691. edges[ 2 * numEdges ] = edge[ 0 ];
  20692. edges[ 2 * numEdges + 1 ] = edge[ 1 ];
  20693. hash[ key ] = true;
  20694. numEdges ++;
  20695. }
  20696. }
  20697. }
  20698. }
  20699. var coords = new Float32Array( numEdges * 2 * 3 );
  20700. for ( var i = 0, l = numEdges; i < l; i ++ ) {
  20701. for ( var j = 0; j < 2; j ++ ) {
  20702. var index = 6 * i + 3 * j;
  20703. var index2 = 3 * edges[ 2 * i + j];
  20704. coords[ index + 0 ] = vertices[ index2 ];
  20705. coords[ index + 1 ] = vertices[ index2 + 1 ];
  20706. coords[ index + 2 ] = vertices[ index2 + 2 ];
  20707. }
  20708. }
  20709. geometry.addAttribute( 'position', new THREE.BufferAttribute( coords, 3 ) );
  20710. } else { // non-indexed BufferGeometry
  20711. var vertices = object.geometry.attributes.position.array;
  20712. var numEdges = vertices.length / 3;
  20713. var numTris = numEdges / 3;
  20714. var coords = new Float32Array( numEdges * 2 * 3 );
  20715. for ( var i = 0, l = numTris; i < l; i ++ ) {
  20716. for ( var j = 0; j < 3; j ++ ) {
  20717. var index = 18 * i + 6 * j;
  20718. var index1 = 9 * i + 3 * j;
  20719. coords[ index + 0 ] = vertices[ index1 ];
  20720. coords[ index + 1 ] = vertices[ index1 + 1 ];
  20721. coords[ index + 2 ] = vertices[ index1 + 2 ];
  20722. var index2 = 9 * i + 3 * ( ( j + 1 ) % 3 );
  20723. coords[ index + 3 ] = vertices[ index2 ];
  20724. coords[ index + 4 ] = vertices[ index2 + 1 ];
  20725. coords[ index + 5 ] = vertices[ index2 + 2 ];
  20726. }
  20727. }
  20728. geometry.addAttribute( 'position', new THREE.BufferAttribute( coords, 3 ) );
  20729. }
  20730. }
  20731. THREE.Line.call( this, geometry, new THREE.LineBasicMaterial( { color: color } ), THREE.LinePieces );
  20732. this.matrix = object.matrixWorld;
  20733. this.matrixAutoUpdate = false;
  20734. };
  20735. THREE.WireframeHelper.prototype = Object.create( THREE.Line.prototype );
  20736. THREE.WireframeHelper.prototype.constructor = THREE.WireframeHelper;
  20737. // File:src/extras/objects/ImmediateRenderObject.js
  20738. /**
  20739. * @author alteredq / http://alteredqualia.com/
  20740. */
  20741. THREE.ImmediateRenderObject = function () {
  20742. THREE.Object3D.call( this );
  20743. this.render = function ( renderCallback ) {};
  20744. };
  20745. THREE.ImmediateRenderObject.prototype = Object.create( THREE.Object3D.prototype );
  20746. THREE.ImmediateRenderObject.prototype.constructor = THREE.ImmediateRenderObject;
  20747. // File:src/extras/objects/MorphBlendMesh.js
  20748. /**
  20749. * @author alteredq / http://alteredqualia.com/
  20750. */
  20751. THREE.MorphBlendMesh = function( geometry, material ) {
  20752. THREE.Mesh.call( this, geometry, material );
  20753. this.animationsMap = {};
  20754. this.animationsList = [];
  20755. // prepare default animation
  20756. // (all frames played together in 1 second)
  20757. var numFrames = this.geometry.morphTargets.length;
  20758. var name = "__default";
  20759. var startFrame = 0;
  20760. var endFrame = numFrames - 1;
  20761. var fps = numFrames / 1;
  20762. this.createAnimation( name, startFrame, endFrame, fps );
  20763. this.setAnimationWeight( name, 1 );
  20764. };
  20765. THREE.MorphBlendMesh.prototype = Object.create( THREE.Mesh.prototype );
  20766. THREE.MorphBlendMesh.prototype.constructor = THREE.MorphBlendMesh;
  20767. THREE.MorphBlendMesh.prototype.createAnimation = function ( name, start, end, fps ) {
  20768. var animation = {
  20769. startFrame: start,
  20770. endFrame: end,
  20771. length: end - start + 1,
  20772. fps: fps,
  20773. duration: ( end - start ) / fps,
  20774. lastFrame: 0,
  20775. currentFrame: 0,
  20776. active: false,
  20777. time: 0,
  20778. direction: 1,
  20779. weight: 1,
  20780. directionBackwards: false,
  20781. mirroredLoop: false
  20782. };
  20783. this.animationsMap[ name ] = animation;
  20784. this.animationsList.push( animation );
  20785. };
  20786. THREE.MorphBlendMesh.prototype.autoCreateAnimations = function ( fps ) {
  20787. var pattern = /([a-z]+)_?(\d+)/;
  20788. var firstAnimation, frameRanges = {};
  20789. var geometry = this.geometry;
  20790. for ( var i = 0, il = geometry.morphTargets.length; i < il; i ++ ) {
  20791. var morph = geometry.morphTargets[ i ];
  20792. var chunks = morph.name.match( pattern );
  20793. if ( chunks && chunks.length > 1 ) {
  20794. var name = chunks[ 1 ];
  20795. var num = chunks[ 2 ];
  20796. if ( ! frameRanges[ name ] ) frameRanges[ name ] = { start: Infinity, end: - Infinity };
  20797. var range = frameRanges[ name ];
  20798. if ( i < range.start ) range.start = i;
  20799. if ( i > range.end ) range.end = i;
  20800. if ( ! firstAnimation ) firstAnimation = name;
  20801. }
  20802. }
  20803. for ( var name in frameRanges ) {
  20804. var range = frameRanges[ name ];
  20805. this.createAnimation( name, range.start, range.end, fps );
  20806. }
  20807. this.firstAnimation = firstAnimation;
  20808. };
  20809. THREE.MorphBlendMesh.prototype.setAnimationDirectionForward = function ( name ) {
  20810. var animation = this.animationsMap[ name ];
  20811. if ( animation ) {
  20812. animation.direction = 1;
  20813. animation.directionBackwards = false;
  20814. }
  20815. };
  20816. THREE.MorphBlendMesh.prototype.setAnimationDirectionBackward = function ( name ) {
  20817. var animation = this.animationsMap[ name ];
  20818. if ( animation ) {
  20819. animation.direction = - 1;
  20820. animation.directionBackwards = true;
  20821. }
  20822. };
  20823. THREE.MorphBlendMesh.prototype.setAnimationFPS = function ( name, fps ) {
  20824. var animation = this.animationsMap[ name ];
  20825. if ( animation ) {
  20826. animation.fps = fps;
  20827. animation.duration = ( animation.end - animation.start ) / animation.fps;
  20828. }
  20829. };
  20830. THREE.MorphBlendMesh.prototype.setAnimationDuration = function ( name, duration ) {
  20831. var animation = this.animationsMap[ name ];
  20832. if ( animation ) {
  20833. animation.duration = duration;
  20834. animation.fps = ( animation.end - animation.start ) / animation.duration;
  20835. }
  20836. };
  20837. THREE.MorphBlendMesh.prototype.setAnimationWeight = function ( name, weight ) {
  20838. var animation = this.animationsMap[ name ];
  20839. if ( animation ) {
  20840. animation.weight = weight;
  20841. }
  20842. };
  20843. THREE.MorphBlendMesh.prototype.setAnimationTime = function ( name, time ) {
  20844. var animation = this.animationsMap[ name ];
  20845. if ( animation ) {
  20846. animation.time = time;
  20847. }
  20848. };
  20849. THREE.MorphBlendMesh.prototype.getAnimationTime = function ( name ) {
  20850. var time = 0;
  20851. var animation = this.animationsMap[ name ];
  20852. if ( animation ) {
  20853. time = animation.time;
  20854. }
  20855. return time;
  20856. };
  20857. THREE.MorphBlendMesh.prototype.getAnimationDuration = function ( name ) {
  20858. var duration = - 1;
  20859. var animation = this.animationsMap[ name ];
  20860. if ( animation ) {
  20861. duration = animation.duration;
  20862. }
  20863. return duration;
  20864. };
  20865. THREE.MorphBlendMesh.prototype.playAnimation = function ( name ) {
  20866. var animation = this.animationsMap[ name ];
  20867. if ( animation ) {
  20868. animation.time = 0;
  20869. animation.active = true;
  20870. } else {
  20871. console.warn( "animation[" + name + "] undefined" );
  20872. }
  20873. };
  20874. THREE.MorphBlendMesh.prototype.stopAnimation = function ( name ) {
  20875. var animation = this.animationsMap[ name ];
  20876. if ( animation ) {
  20877. animation.active = false;
  20878. }
  20879. };
  20880. THREE.MorphBlendMesh.prototype.update = function ( delta ) {
  20881. for ( var i = 0, il = this.animationsList.length; i < il; i ++ ) {
  20882. var animation = this.animationsList[ i ];
  20883. if ( ! animation.active ) continue;
  20884. var frameTime = animation.duration / animation.length;
  20885. animation.time += animation.direction * delta;
  20886. if ( animation.mirroredLoop ) {
  20887. if ( animation.time > animation.duration || animation.time < 0 ) {
  20888. animation.direction *= - 1;
  20889. if ( animation.time > animation.duration ) {
  20890. animation.time = animation.duration;
  20891. animation.directionBackwards = true;
  20892. }
  20893. if ( animation.time < 0 ) {
  20894. animation.time = 0;
  20895. animation.directionBackwards = false;
  20896. }
  20897. }
  20898. } else {
  20899. animation.time = animation.time % animation.duration;
  20900. if ( animation.time < 0 ) animation.time += animation.duration;
  20901. }
  20902. var keyframe = animation.startFrame + THREE.Math.clamp( Math.floor( animation.time / frameTime ), 0, animation.length - 1 );
  20903. var weight = animation.weight;
  20904. if ( keyframe !== animation.currentFrame ) {
  20905. this.morphTargetInfluences[ animation.lastFrame ] = 0;
  20906. this.morphTargetInfluences[ animation.currentFrame ] = 1 * weight;
  20907. this.morphTargetInfluences[ keyframe ] = 0;
  20908. animation.lastFrame = animation.currentFrame;
  20909. animation.currentFrame = keyframe;
  20910. }
  20911. var mix = ( animation.time % frameTime ) / frameTime;
  20912. if ( animation.directionBackwards ) mix = 1 - mix;
  20913. this.morphTargetInfluences[ animation.currentFrame ] = mix * weight;
  20914. this.morphTargetInfluences[ animation.lastFrame ] = ( 1 - mix ) * weight;
  20915. }
  20916. };