ThreeCanvas.js 159 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944
  1. /**
  2. * @author mr.doob / http://mrdoob.com/
  3. */
  4. var THREE = THREE || { REVISION: '49dev' };
  5. if ( ! self.Int32Array ) {
  6. self.Int32Array = Array;
  7. self.Float32Array = Array;
  8. }
  9. // http://paulirish.com/2011/requestanimationframe-for-smart-animating/
  10. // http://my.opera.com/emoller/blog/2011/12/20/requestanimationframe-for-smart-er-animating
  11. // requestAnimationFrame polyfill by Erik Möller
  12. // fixes from Paul Irish and Tino Zijdel
  13. (function() {
  14. var lastTime = 0;
  15. var vendors = ['ms', 'moz', 'webkit', 'o'];
  16. for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  17. window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
  18. window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
  19. || window[vendors[x]+'CancelRequestAnimationFrame'];
  20. }
  21. if (!window.requestAnimationFrame)
  22. window.requestAnimationFrame = function(callback, element) {
  23. var currTime = new Date().getTime();
  24. var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  25. var id = window.setTimeout(function() { callback(currTime + timeToCall); },
  26. timeToCall);
  27. lastTime = currTime + timeToCall;
  28. return id;
  29. };
  30. if (!window.cancelAnimationFrame)
  31. window.cancelAnimationFrame = function(id) {
  32. clearTimeout(id);
  33. };
  34. }());
  35. /**
  36. * @author mr.doob / http://mrdoob.com/
  37. */
  38. THREE.Color = function ( hex ) {
  39. if ( hex !== undefined ) this.setHex( hex );
  40. return this;
  41. };
  42. THREE.Color.prototype = {
  43. constructor: THREE.Color,
  44. r: 1, g: 1, b: 1,
  45. copy: function ( color ) {
  46. this.r = color.r;
  47. this.g = color.g;
  48. this.b = color.b;
  49. return this;
  50. },
  51. copyGammaToLinear: function ( color ) {
  52. this.r = color.r * color.r;
  53. this.g = color.g * color.g;
  54. this.b = color.b * color.b;
  55. return this;
  56. },
  57. copyLinearToGamma: function ( color ) {
  58. this.r = Math.sqrt( color.r );
  59. this.g = Math.sqrt( color.g );
  60. this.b = Math.sqrt( color.b );
  61. return this;
  62. },
  63. convertGammaToLinear: function () {
  64. var r = this.r, g = this.g, b = this.b;
  65. this.r = r * r;
  66. this.g = g * g;
  67. this.b = b * b;
  68. return this;
  69. },
  70. convertLinearToGamma: function () {
  71. this.r = Math.sqrt( this.r );
  72. this.g = Math.sqrt( this.g );
  73. this.b = Math.sqrt( this.b );
  74. return this;
  75. },
  76. setRGB: function ( r, g, b ) {
  77. this.r = r;
  78. this.g = g;
  79. this.b = b;
  80. return this;
  81. },
  82. setHSV: function ( h, s, v ) {
  83. // based on MochiKit implementation by Bob Ippolito
  84. // h,s,v ranges are < 0.0 - 1.0 >
  85. var i, f, p, q, t;
  86. if ( v === 0 ) {
  87. this.r = this.g = this.b = 0;
  88. } else {
  89. i = Math.floor( h * 6 );
  90. f = ( h * 6 ) - i;
  91. p = v * ( 1 - s );
  92. q = v * ( 1 - ( s * f ) );
  93. t = v * ( 1 - ( s * ( 1 - f ) ) );
  94. switch ( i ) {
  95. case 1: this.r = q; this.g = v; this.b = p; break;
  96. case 2: this.r = p; this.g = v; this.b = t; break;
  97. case 3: this.r = p; this.g = q; this.b = v; break;
  98. case 4: this.r = t; this.g = p; this.b = v; break;
  99. case 5: this.r = v; this.g = p; this.b = q; break;
  100. case 6: // fall through
  101. case 0: this.r = v; this.g = t; this.b = p; break;
  102. }
  103. }
  104. return this;
  105. },
  106. setHex: function ( hex ) {
  107. hex = Math.floor( hex );
  108. this.r = ( hex >> 16 & 255 ) / 255;
  109. this.g = ( hex >> 8 & 255 ) / 255;
  110. this.b = ( hex & 255 ) / 255;
  111. return this;
  112. },
  113. lerpSelf: function ( color, alpha ) {
  114. this.r += ( color.r - this.r ) * alpha;
  115. this.g += ( color.g - this.g ) * alpha;
  116. this.b += ( color.b - this.b ) * alpha;
  117. return this;
  118. },
  119. getHex: function () {
  120. return Math.floor( this.r * 255 ) << 16 ^ Math.floor( this.g * 255 ) << 8 ^ Math.floor( this.b * 255 );
  121. },
  122. getContextStyle: function () {
  123. return 'rgb(' + Math.floor( this.r * 255 ) + ',' + Math.floor( this.g * 255 ) + ',' + Math.floor( this.b * 255 ) + ')';
  124. },
  125. clone: function () {
  126. return new THREE.Color().setRGB( this.r, this.g, this.b );
  127. }
  128. };
  129. /**
  130. * @author mr.doob / http://mrdoob.com/
  131. * @author philogb / http://blog.thejit.org/
  132. * @author egraether / http://egraether.com/
  133. * @author zz85 / http://www.lab4games.net/zz85/blog
  134. */
  135. THREE.Vector2 = function ( x, y ) {
  136. this.x = x || 0;
  137. this.y = y || 0;
  138. };
  139. THREE.Vector2.prototype = {
  140. constructor: THREE.Vector2,
  141. set: function ( x, y ) {
  142. this.x = x;
  143. this.y = y;
  144. return this;
  145. },
  146. copy: function ( v ) {
  147. this.x = v.x;
  148. this.y = v.y;
  149. return this;
  150. },
  151. add: function ( a, b ) {
  152. this.x = a.x + b.x;
  153. this.y = a.y + b.y;
  154. return this;
  155. },
  156. addSelf: function ( v ) {
  157. this.x += v.x;
  158. this.y += v.y;
  159. return this;
  160. },
  161. sub: function ( a, b ) {
  162. this.x = a.x - b.x;
  163. this.y = a.y - b.y;
  164. return this;
  165. },
  166. subSelf: function ( v ) {
  167. this.x -= v.x;
  168. this.y -= v.y;
  169. return this;
  170. },
  171. multiplyScalar: function ( s ) {
  172. this.x *= s;
  173. this.y *= s;
  174. return this;
  175. },
  176. divideScalar: function ( s ) {
  177. if ( s ) {
  178. this.x /= s;
  179. this.y /= s;
  180. } else {
  181. this.set( 0, 0 );
  182. }
  183. return this;
  184. },
  185. negate: function() {
  186. return this.multiplyScalar( - 1 );
  187. },
  188. dot: function ( v ) {
  189. return this.x * v.x + this.y * v.y;
  190. },
  191. lengthSq: function () {
  192. return this.x * this.x + this.y * this.y;
  193. },
  194. length: function () {
  195. return Math.sqrt( this.lengthSq() );
  196. },
  197. normalize: function () {
  198. return this.divideScalar( this.length() );
  199. },
  200. distanceTo: function ( v ) {
  201. return Math.sqrt( this.distanceToSquared( v ) );
  202. },
  203. distanceToSquared: function ( v ) {
  204. var dx = this.x - v.x, dy = this.y - v.y;
  205. return dx * dx + dy * dy;
  206. },
  207. setLength: function ( l ) {
  208. return this.normalize().multiplyScalar( l );
  209. },
  210. lerpSelf: function ( v, alpha ) {
  211. this.x += ( v.x - this.x ) * alpha;
  212. this.y += ( v.y - this.y ) * alpha;
  213. return this;
  214. },
  215. equals: function( v ) {
  216. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  217. },
  218. isZero: function () {
  219. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  220. },
  221. clone: function () {
  222. return new THREE.Vector2( this.x, this.y );
  223. }
  224. };
  225. /**
  226. * @author mr.doob / http://mrdoob.com/
  227. * @author kile / http://kile.stravaganza.org/
  228. * @author philogb / http://blog.thejit.org/
  229. * @author mikael emtinger / http://gomo.se/
  230. * @author egraether / http://egraether.com/
  231. */
  232. THREE.Vector3 = function ( x, y, z ) {
  233. this.x = x || 0;
  234. this.y = y || 0;
  235. this.z = z || 0;
  236. };
  237. THREE.Vector3.prototype = {
  238. constructor: THREE.Vector3,
  239. set: function ( x, y, z ) {
  240. this.x = x;
  241. this.y = y;
  242. this.z = z;
  243. return this;
  244. },
  245. setX: function ( x ) {
  246. this.x = x;
  247. return this;
  248. },
  249. setY: function ( y ) {
  250. this.y = y;
  251. return this;
  252. },
  253. setZ: function ( z ) {
  254. this.z = z;
  255. return this;
  256. },
  257. copy: function ( v ) {
  258. this.x = v.x;
  259. this.y = v.y;
  260. this.z = v.z;
  261. return this;
  262. },
  263. add: function ( a, b ) {
  264. this.x = a.x + b.x;
  265. this.y = a.y + b.y;
  266. this.z = a.z + b.z;
  267. return this;
  268. },
  269. addSelf: function ( v ) {
  270. this.x += v.x;
  271. this.y += v.y;
  272. this.z += v.z;
  273. return this;
  274. },
  275. addScalar: function ( s ) {
  276. this.x += s;
  277. this.y += s;
  278. this.z += s;
  279. return this;
  280. },
  281. sub: function ( a, b ) {
  282. this.x = a.x - b.x;
  283. this.y = a.y - b.y;
  284. this.z = a.z - b.z;
  285. return this;
  286. },
  287. subSelf: function ( v ) {
  288. this.x -= v.x;
  289. this.y -= v.y;
  290. this.z -= v.z;
  291. return this;
  292. },
  293. multiply: function ( a, b ) {
  294. this.x = a.x * b.x;
  295. this.y = a.y * b.y;
  296. this.z = a.z * b.z;
  297. return this;
  298. },
  299. multiplySelf: function ( v ) {
  300. this.x *= v.x;
  301. this.y *= v.y;
  302. this.z *= v.z;
  303. return this;
  304. },
  305. multiplyScalar: function ( s ) {
  306. this.x *= s;
  307. this.y *= s;
  308. this.z *= s;
  309. return this;
  310. },
  311. divideSelf: function ( v ) {
  312. this.x /= v.x;
  313. this.y /= v.y;
  314. this.z /= v.z;
  315. return this;
  316. },
  317. divideScalar: function ( s ) {
  318. if ( s ) {
  319. this.x /= s;
  320. this.y /= s;
  321. this.z /= s;
  322. } else {
  323. this.x = 0;
  324. this.y = 0;
  325. this.z = 0;
  326. }
  327. return this;
  328. },
  329. negate: function() {
  330. return this.multiplyScalar( - 1 );
  331. },
  332. dot: function ( v ) {
  333. return this.x * v.x + this.y * v.y + this.z * v.z;
  334. },
  335. lengthSq: function () {
  336. return this.x * this.x + this.y * this.y + this.z * this.z;
  337. },
  338. length: function () {
  339. return Math.sqrt( this.lengthSq() );
  340. },
  341. lengthManhattan: function () {
  342. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  343. },
  344. normalize: function () {
  345. return this.divideScalar( this.length() );
  346. },
  347. setLength: function ( l ) {
  348. return this.normalize().multiplyScalar( l );
  349. },
  350. lerpSelf: function ( v, alpha ) {
  351. this.x += ( v.x - this.x ) * alpha;
  352. this.y += ( v.y - this.y ) * alpha;
  353. this.z += ( v.z - this.z ) * alpha;
  354. return this;
  355. },
  356. cross: function ( a, b ) {
  357. this.x = a.y * b.z - a.z * b.y;
  358. this.y = a.z * b.x - a.x * b.z;
  359. this.z = a.x * b.y - a.y * b.x;
  360. return this;
  361. },
  362. crossSelf: function ( v ) {
  363. var x = this.x, y = this.y, z = this.z;
  364. this.x = y * v.z - z * v.y;
  365. this.y = z * v.x - x * v.z;
  366. this.z = x * v.y - y * v.x;
  367. return this;
  368. },
  369. distanceTo: function ( v ) {
  370. return Math.sqrt( this.distanceToSquared( v ) );
  371. },
  372. distanceToSquared: function ( v ) {
  373. return new THREE.Vector3().sub( this, v ).lengthSq();
  374. },
  375. getPositionFromMatrix: function ( m ) {
  376. this.x = m.elements[12];
  377. this.y = m.elements[13];
  378. this.z = m.elements[14];
  379. return this;
  380. },
  381. getRotationFromMatrix: function ( m, scale ) {
  382. var sx = scale ? scale.x : 1;
  383. var sy = scale ? scale.y : 1;
  384. var sz = scale ? scale.z : 1;
  385. var m11 = m.elements[0] / sx, m12 = m.elements[4] / sy, m13 = m.elements[8] / sz;
  386. var m21 = m.elements[1] / sx, m22 = m.elements[5] / sy, m23 = m.elements[9] / sz;
  387. var m33 = m.elements[10] / sz;
  388. this.y = Math.asin( m13 );
  389. var cosY = Math.cos( this.y );
  390. if ( Math.abs( cosY ) > 0.00001 ) {
  391. this.x = Math.atan2( - m23 / cosY, m33 / cosY );
  392. this.z = Math.atan2( - m12 / cosY, m11 / cosY );
  393. } else {
  394. this.x = 0;
  395. this.z = Math.atan2( m21, m22 );
  396. }
  397. return this;
  398. },
  399. /*
  400. // from http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  401. // order XYZ
  402. getEulerXYZFromQuaternion: function ( q ) {
  403. this.x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z ) );
  404. this.y = Math.asin( 2 * ( q.x * q.z + q.y * q.w ) );
  405. this.z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z ) );
  406. },
  407. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/index.htm
  408. // order YZX (assuming heading == y, attitude == z, bank == x)
  409. getEulerYZXFromQuaternion: function ( q ) {
  410. var sqw = q.w * q.w;
  411. var sqx = q.x * q.x;
  412. var sqy = q.y * q.y;
  413. var sqz = q.z * q.z;
  414. var unit = sqx + sqy + sqz + sqw; // if normalised is one, otherwise is correction factor
  415. var test = q.x * q.y + q.z * q.w;
  416. if ( test > 0.499 * unit ) { // singularity at north pole
  417. this.y = 2 * Math.atan2( q.x, q.w );
  418. this.z = Math.PI / 2;
  419. this.x = 0;
  420. return;
  421. }
  422. if ( test < -0.499 * unit ) { // singularity at south pole
  423. this.y = -2 * Math.atan2( q.x, q.w );
  424. this.z = -Math.PI / 2;
  425. this.x = 0;
  426. return;
  427. }
  428. this.y = Math.atan2( 2 * q.y * q.w - 2 * q.x * q.z, sqx - sqy - sqz + sqw );
  429. this.z = Math.asin( 2 * test / unit );
  430. this.x = Math.atan2( 2 * q.x * q.w - 2 * q.y * q.z, -sqx + sqy - sqz + sqw );
  431. },
  432. */
  433. getScaleFromMatrix: function ( m ) {
  434. var sx = this.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  435. var sy = this.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  436. var sz = this.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  437. this.x = sx;
  438. this.y = sy;
  439. this.z = sz;
  440. },
  441. equals: function ( v ) {
  442. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  443. },
  444. isZero: function () {
  445. return ( this.lengthSq() < 0.0001 /* almostZero */ );
  446. },
  447. clone: function () {
  448. return new THREE.Vector3( this.x, this.y, this.z );
  449. }
  450. };
  451. /**
  452. * @author supereggbert / http://www.paulbrunt.co.uk/
  453. * @author philogb / http://blog.thejit.org/
  454. * @author mikael emtinger / http://gomo.se/
  455. * @author egraether / http://egraether.com/
  456. */
  457. THREE.Vector4 = function ( x, y, z, w ) {
  458. this.x = x || 0;
  459. this.y = y || 0;
  460. this.z = z || 0;
  461. this.w = ( w !== undefined ) ? w : 1;
  462. };
  463. THREE.Vector4.prototype = {
  464. constructor: THREE.Vector4,
  465. set: function ( x, y, z, w ) {
  466. this.x = x;
  467. this.y = y;
  468. this.z = z;
  469. this.w = w;
  470. return this;
  471. },
  472. copy: function ( v ) {
  473. this.x = v.x;
  474. this.y = v.y;
  475. this.z = v.z;
  476. this.w = ( v.w !== undefined ) ? v.w : 1;
  477. return this;
  478. },
  479. add: function ( a, b ) {
  480. this.x = a.x + b.x;
  481. this.y = a.y + b.y;
  482. this.z = a.z + b.z;
  483. this.w = a.w + b.w;
  484. return this;
  485. },
  486. addSelf: function ( v ) {
  487. this.x += v.x;
  488. this.y += v.y;
  489. this.z += v.z;
  490. this.w += v.w;
  491. return this;
  492. },
  493. sub: function ( a, b ) {
  494. this.x = a.x - b.x;
  495. this.y = a.y - b.y;
  496. this.z = a.z - b.z;
  497. this.w = a.w - b.w;
  498. return this;
  499. },
  500. subSelf: function ( v ) {
  501. this.x -= v.x;
  502. this.y -= v.y;
  503. this.z -= v.z;
  504. this.w -= v.w;
  505. return this;
  506. },
  507. multiplyScalar: function ( s ) {
  508. this.x *= s;
  509. this.y *= s;
  510. this.z *= s;
  511. this.w *= s;
  512. return this;
  513. },
  514. divideScalar: function ( s ) {
  515. if ( s ) {
  516. this.x /= s;
  517. this.y /= s;
  518. this.z /= s;
  519. this.w /= s;
  520. } else {
  521. this.x = 0;
  522. this.y = 0;
  523. this.z = 0;
  524. this.w = 1;
  525. }
  526. return this;
  527. },
  528. negate: function() {
  529. return this.multiplyScalar( -1 );
  530. },
  531. dot: function ( v ) {
  532. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  533. },
  534. lengthSq: function () {
  535. return this.dot( this );
  536. },
  537. length: function () {
  538. return Math.sqrt( this.lengthSq() );
  539. },
  540. normalize: function () {
  541. return this.divideScalar( this.length() );
  542. },
  543. setLength: function ( l ) {
  544. return this.normalize().multiplyScalar( l );
  545. },
  546. lerpSelf: function ( v, alpha ) {
  547. this.x += ( v.x - this.x ) * alpha;
  548. this.y += ( v.y - this.y ) * alpha;
  549. this.z += ( v.z - this.z ) * alpha;
  550. this.w += ( v.w - this.w ) * alpha;
  551. return this;
  552. },
  553. clone: function () {
  554. return new THREE.Vector4( this.x, this.y, this.z, this.w );
  555. }
  556. };
  557. /**
  558. * @author mrdoob / http://mrdoob.com/
  559. * @author alteredq / http://alteredqualia.com/
  560. */
  561. THREE.Frustum = function ( ) {
  562. this.planes = [
  563. new THREE.Vector4(),
  564. new THREE.Vector4(),
  565. new THREE.Vector4(),
  566. new THREE.Vector4(),
  567. new THREE.Vector4(),
  568. new THREE.Vector4()
  569. ];
  570. };
  571. THREE.Frustum.prototype.setFromMatrix = function ( m ) {
  572. var i, plane,
  573. planes = this.planes;
  574. planes[ 0 ].set( m.elements[3] - m.elements[0], m.elements[7] - m.elements[4], m.elements[11] - m.elements[8], m.elements[15] - m.elements[12] );
  575. planes[ 1 ].set( m.elements[3] + m.elements[0], m.elements[7] + m.elements[4], m.elements[11] + m.elements[8], m.elements[15] + m.elements[12] );
  576. planes[ 2 ].set( m.elements[3] + m.elements[1], m.elements[7] + m.elements[5], m.elements[11] + m.elements[9], m.elements[15] + m.elements[13] );
  577. planes[ 3 ].set( m.elements[3] - m.elements[1], m.elements[7] - m.elements[5], m.elements[11] - m.elements[9], m.elements[15] - m.elements[13] );
  578. planes[ 4 ].set( m.elements[3] - m.elements[2], m.elements[7] - m.elements[6], m.elements[11] - m.elements[10], m.elements[15] - m.elements[14] );
  579. planes[ 5 ].set( m.elements[3] + m.elements[2], m.elements[7] + m.elements[6], m.elements[11] + m.elements[10], m.elements[15] + m.elements[14] );
  580. for ( i = 0; i < 6; i ++ ) {
  581. plane = planes[ i ];
  582. plane.divideScalar( Math.sqrt( plane.x * plane.x + plane.y * plane.y + plane.z * plane.z ) );
  583. }
  584. };
  585. THREE.Frustum.prototype.contains = function ( object ) {
  586. var distance,
  587. planes = this.planes,
  588. matrix = object.matrixWorld,
  589. scale = THREE.Frustum.__v1.set( matrix.getColumnX().length(), matrix.getColumnY().length(), matrix.getColumnZ().length() ),
  590. radius = - object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) );
  591. for ( var i = 0; i < 6; i ++ ) {
  592. distance = planes[ i ].x * matrix.elements[12] + planes[ i ].y * matrix.elements[13] + planes[ i ].z * matrix.elements[14] + planes[ i ].w;
  593. if ( distance <= radius ) return false;
  594. }
  595. return true;
  596. };
  597. THREE.Frustum.__v1 = new THREE.Vector3();
  598. /**
  599. * @author mr.doob / http://mrdoob.com/
  600. */
  601. THREE.Ray = function ( origin, direction ) {
  602. this.origin = origin || new THREE.Vector3();
  603. this.direction = direction || new THREE.Vector3();
  604. var precision = 0.0001;
  605. this.setPrecision = function ( value ) {
  606. precision = value;
  607. };
  608. var a = new THREE.Vector3();
  609. var b = new THREE.Vector3();
  610. var c = new THREE.Vector3();
  611. var d = new THREE.Vector3();
  612. var originCopy = new THREE.Vector3();
  613. var directionCopy = new THREE.Vector3();
  614. var vector = new THREE.Vector3();
  615. var normal = new THREE.Vector3();
  616. var intersectPoint = new THREE.Vector3()
  617. this.intersectObject = function ( object ) {
  618. var intersect, intersects = [];
  619. if ( object instanceof THREE.Particle ) {
  620. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  621. if ( distance > object.scale.x ) {
  622. return [];
  623. }
  624. intersect = {
  625. distance: distance,
  626. point: object.position,
  627. face: null,
  628. object: object
  629. };
  630. intersects.push( intersect );
  631. } else if ( object instanceof THREE.Mesh ) {
  632. // Checking boundingSphere
  633. var distance = distanceFromIntersection( this.origin, this.direction, object.matrixWorld.getPosition() );
  634. var scale = THREE.Frustum.__v1.set( object.matrixWorld.getColumnX().length(), object.matrixWorld.getColumnY().length(), object.matrixWorld.getColumnZ().length() );
  635. if ( distance > object.geometry.boundingSphere.radius * Math.max( scale.x, Math.max( scale.y, scale.z ) ) ) {
  636. return intersects;
  637. }
  638. // Checking faces
  639. var f, fl, face, dot, scalar,
  640. geometry = object.geometry,
  641. vertices = geometry.vertices,
  642. objMatrix;
  643. object.matrixRotationWorld.extractRotation( object.matrixWorld );
  644. for ( f = 0, fl = geometry.faces.length; f < fl; f ++ ) {
  645. face = geometry.faces[ f ];
  646. originCopy.copy( this.origin );
  647. directionCopy.copy( this.direction );
  648. objMatrix = object.matrixWorld;
  649. // determine if ray intersects the plane of the face
  650. // note: this works regardless of the direction of the face normal
  651. vector = objMatrix.multiplyVector3( vector.copy( face.centroid ) ).subSelf( originCopy );
  652. normal = object.matrixRotationWorld.multiplyVector3( normal.copy( face.normal ) );
  653. dot = directionCopy.dot( normal );
  654. // bail if ray and plane are parallel
  655. if ( Math.abs( dot ) < precision ) continue;
  656. // calc distance to plane
  657. scalar = normal.dot( vector ) / dot;
  658. // if negative distance, then plane is behind ray
  659. if ( scalar < 0 ) continue;
  660. if ( object.doubleSided || ( object.flipSided ? dot > 0 : dot < 0 ) ) {
  661. intersectPoint.add( originCopy, directionCopy.multiplyScalar( scalar ) );
  662. if ( face instanceof THREE.Face3 ) {
  663. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ].position ) );
  664. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ].position ) );
  665. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ].position ) );
  666. if ( pointInFace3( intersectPoint, a, b, c ) ) {
  667. intersect = {
  668. distance: originCopy.distanceTo( intersectPoint ),
  669. point: intersectPoint.clone(),
  670. face: face,
  671. object: object
  672. };
  673. intersects.push( intersect );
  674. }
  675. } else if ( face instanceof THREE.Face4 ) {
  676. a = objMatrix.multiplyVector3( a.copy( vertices[ face.a ].position ) );
  677. b = objMatrix.multiplyVector3( b.copy( vertices[ face.b ].position ) );
  678. c = objMatrix.multiplyVector3( c.copy( vertices[ face.c ].position ) );
  679. d = objMatrix.multiplyVector3( d.copy( vertices[ face.d ].position ) );
  680. if ( pointInFace3( intersectPoint, a, b, d ) || pointInFace3( intersectPoint, b, c, d ) ) {
  681. intersect = {
  682. distance: originCopy.distanceTo( intersectPoint ),
  683. point: intersectPoint.clone(),
  684. face: face,
  685. object: object
  686. };
  687. intersects.push( intersect );
  688. }
  689. }
  690. }
  691. }
  692. }
  693. return intersects;
  694. }
  695. this.intersectObjects = function ( objects ) {
  696. var intersects = [];
  697. for ( var i = 0, l = objects.length; i < l; i ++ ) {
  698. Array.prototype.push.apply( intersects, this.intersectObject( objects[ i ] ) );
  699. }
  700. intersects.sort( function ( a, b ) { return a.distance - b.distance; } );
  701. return intersects;
  702. };
  703. var v0 = new THREE.Vector3(), v1 = new THREE.Vector3(), v2 = new THREE.Vector3();
  704. var dot, intersect, distance;
  705. function distanceFromIntersection( origin, direction, position ) {
  706. v0.sub( position, origin );
  707. dot = v0.dot( direction );
  708. intersect = v1.add( origin, v2.copy( direction ).multiplyScalar( dot ) );
  709. distance = position.distanceTo( intersect );
  710. return distance;
  711. }
  712. // http://www.blackpawn.com/texts/pointinpoly/default.html
  713. var dot00, dot01, dot02, dot11, dot12, invDenom, u, v;
  714. function pointInFace3( p, a, b, c ) {
  715. v0.sub( c, a );
  716. v1.sub( b, a );
  717. v2.sub( p, a );
  718. dot00 = v0.dot( v0 );
  719. dot01 = v0.dot( v1 );
  720. dot02 = v0.dot( v2 );
  721. dot11 = v1.dot( v1 );
  722. dot12 = v1.dot( v2 );
  723. invDenom = 1 / ( dot00 * dot11 - dot01 * dot01 );
  724. u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  725. v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  726. return ( u >= 0 ) && ( v >= 0 ) && ( u + v < 1 );
  727. }
  728. };/**
  729. * @author mr.doob / http://mrdoob.com/
  730. */
  731. THREE.Rectangle = function () {
  732. var _left, _top, _right, _bottom,
  733. _width, _height, _isEmpty = true;
  734. function resize() {
  735. _width = _right - _left;
  736. _height = _bottom - _top;
  737. }
  738. this.getX = function () {
  739. return _left;
  740. };
  741. this.getY = function () {
  742. return _top;
  743. };
  744. this.getWidth = function () {
  745. return _width;
  746. };
  747. this.getHeight = function () {
  748. return _height;
  749. };
  750. this.getLeft = function() {
  751. return _left;
  752. };
  753. this.getTop = function() {
  754. return _top;
  755. };
  756. this.getRight = function() {
  757. return _right;
  758. };
  759. this.getBottom = function() {
  760. return _bottom;
  761. };
  762. this.set = function ( left, top, right, bottom ) {
  763. _isEmpty = false;
  764. _left = left; _top = top;
  765. _right = right; _bottom = bottom;
  766. resize();
  767. };
  768. this.addPoint = function ( x, y ) {
  769. if ( _isEmpty ) {
  770. _isEmpty = false;
  771. _left = x; _top = y;
  772. _right = x; _bottom = y;
  773. resize();
  774. } else {
  775. _left = _left < x ? _left : x; // Math.min( _left, x );
  776. _top = _top < y ? _top : y; // Math.min( _top, y );
  777. _right = _right > x ? _right : x; // Math.max( _right, x );
  778. _bottom = _bottom > y ? _bottom : y; // Math.max( _bottom, y );
  779. resize();
  780. }
  781. };
  782. this.add3Points = function ( x1, y1, x2, y2, x3, y3 ) {
  783. if (_isEmpty) {
  784. _isEmpty = false;
  785. _left = x1 < x2 ? ( x1 < x3 ? x1 : x3 ) : ( x2 < x3 ? x2 : x3 );
  786. _top = y1 < y2 ? ( y1 < y3 ? y1 : y3 ) : ( y2 < y3 ? y2 : y3 );
  787. _right = x1 > x2 ? ( x1 > x3 ? x1 : x3 ) : ( x2 > x3 ? x2 : x3 );
  788. _bottom = y1 > y2 ? ( y1 > y3 ? y1 : y3 ) : ( y2 > y3 ? y2 : y3 );
  789. resize();
  790. } else {
  791. _left = x1 < x2 ? ( x1 < x3 ? ( x1 < _left ? x1 : _left ) : ( x3 < _left ? x3 : _left ) ) : ( x2 < x3 ? ( x2 < _left ? x2 : _left ) : ( x3 < _left ? x3 : _left ) );
  792. _top = y1 < y2 ? ( y1 < y3 ? ( y1 < _top ? y1 : _top ) : ( y3 < _top ? y3 : _top ) ) : ( y2 < y3 ? ( y2 < _top ? y2 : _top ) : ( y3 < _top ? y3 : _top ) );
  793. _right = x1 > x2 ? ( x1 > x3 ? ( x1 > _right ? x1 : _right ) : ( x3 > _right ? x3 : _right ) ) : ( x2 > x3 ? ( x2 > _right ? x2 : _right ) : ( x3 > _right ? x3 : _right ) );
  794. _bottom = y1 > y2 ? ( y1 > y3 ? ( y1 > _bottom ? y1 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) ) : ( y2 > y3 ? ( y2 > _bottom ? y2 : _bottom ) : ( y3 > _bottom ? y3 : _bottom ) );
  795. resize();
  796. };
  797. };
  798. this.addRectangle = function ( r ) {
  799. if ( _isEmpty ) {
  800. _isEmpty = false;
  801. _left = r.getLeft(); _top = r.getTop();
  802. _right = r.getRight(); _bottom = r.getBottom();
  803. resize();
  804. } else {
  805. _left = _left < r.getLeft() ? _left : r.getLeft(); // Math.min(_left, r.getLeft() );
  806. _top = _top < r.getTop() ? _top : r.getTop(); // Math.min(_top, r.getTop() );
  807. _right = _right > r.getRight() ? _right : r.getRight(); // Math.max(_right, r.getRight() );
  808. _bottom = _bottom > r.getBottom() ? _bottom : r.getBottom(); // Math.max(_bottom, r.getBottom() );
  809. resize();
  810. }
  811. };
  812. this.inflate = function ( v ) {
  813. _left -= v; _top -= v;
  814. _right += v; _bottom += v;
  815. resize();
  816. };
  817. this.minSelf = function ( r ) {
  818. _left = _left > r.getLeft() ? _left : r.getLeft(); // Math.max( _left, r.getLeft() );
  819. _top = _top > r.getTop() ? _top : r.getTop(); // Math.max( _top, r.getTop() );
  820. _right = _right < r.getRight() ? _right : r.getRight(); // Math.min( _right, r.getRight() );
  821. _bottom = _bottom < r.getBottom() ? _bottom : r.getBottom(); // Math.min( _bottom, r.getBottom() );
  822. resize();
  823. };
  824. this.intersects = function ( r ) {
  825. // http://gamemath.com/2011/09/detecting-whether-two-boxes-overlap/
  826. if ( _right < r.getLeft() ) return false;
  827. if ( _left > r.getRight() ) return false;
  828. if ( _bottom < r.getTop() ) return false;
  829. if ( _top > r.getBottom() ) return false;
  830. return true;
  831. };
  832. this.empty = function () {
  833. _isEmpty = true;
  834. _left = 0; _top = 0;
  835. _right = 0; _bottom = 0;
  836. resize();
  837. };
  838. this.isEmpty = function () {
  839. return _isEmpty;
  840. };
  841. };
  842. /**
  843. * @author alteredq / http://alteredqualia.com/
  844. */
  845. THREE.Math = {
  846. // Clamp value to range <a, b>
  847. clamp: function ( x, a, b ) {
  848. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  849. },
  850. // Clamp value to range <a, inf)
  851. clampBottom: function ( x, a ) {
  852. return x < a ? a : x;
  853. },
  854. // Linear mapping from range <a1, a2> to range <b1, b2>
  855. mapLinear: function ( x, a1, a2, b1, b2 ) {
  856. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  857. },
  858. // Random float from <0, 1> with 16 bits of randomness
  859. // (standard Math.random() creates repetitive patterns when applied over larger space)
  860. random16: function () {
  861. return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
  862. },
  863. // Random integer from <low, high> interval
  864. randInt: function ( low, high ) {
  865. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  866. },
  867. // Random float from <low, high> interval
  868. randFloat: function ( low, high ) {
  869. return low + Math.random() * ( high - low );
  870. },
  871. // Random float from <-range/2, range/2> interval
  872. randFloatSpread: function ( range ) {
  873. return range * ( 0.5 - Math.random() );
  874. },
  875. sign: function ( x ) {
  876. return ( x < 0 ) ? -1 : ( ( x > 0 ) ? 1 : 0 );
  877. }
  878. };
  879. /**
  880. * @author alteredq / http://alteredqualia.com/
  881. */
  882. THREE.Matrix3 = function () {
  883. this.m = [];
  884. };
  885. THREE.Matrix3.prototype = {
  886. constructor: THREE.Matrix3,
  887. getInverse: function ( matrix ) {
  888. // input: THREE.Matrix4
  889. // ( based on http://code.google.com/p/webgl-mjs/ )
  890. var a11 = matrix.elements[10] * matrix.elements[5] - matrix.elements[6] * matrix.elements[9];
  891. var a21 = - matrix.elements[10] * matrix.elements[1] + matrix.elements[2] * matrix.elements[9];
  892. var a31 = matrix.elements[6] * matrix.elements[1] - matrix.elements[2] * matrix.elements[5];
  893. var a12 = - matrix.elements[10] * matrix.elements[4] + matrix.elements[6] * matrix.elements[8];
  894. var a22 = matrix.elements[10] * matrix.elements[0] - matrix.elements[2] * matrix.elements[8];
  895. var a32 = - matrix.elements[6] * matrix.elements[0] + matrix.elements[2] * matrix.elements[4];
  896. var a13 = matrix.elements[9] * matrix.elements[4] - matrix.elements[5] * matrix.elements[8];
  897. var a23 = - matrix.elements[9] * matrix.elements[0] + matrix.elements[1] * matrix.elements[8];
  898. var a33 = matrix.elements[5] * matrix.elements[0] - matrix.elements[1] * matrix.elements[4];
  899. var det = matrix.elements[0] * a11 + matrix.elements[1] * a12 + matrix.elements[2] * a13;
  900. // no inverse
  901. if ( det === 0 ) {
  902. console.warn( "Matrix3.getInverse(): determinant == 0" );
  903. }
  904. var idet = 1.0 / det;
  905. var m = this.m;
  906. m[ 0 ] = idet * a11; m[ 1 ] = idet * a21; m[ 2 ] = idet * a31;
  907. m[ 3 ] = idet * a12; m[ 4 ] = idet * a22; m[ 5 ] = idet * a32;
  908. m[ 6 ] = idet * a13; m[ 7 ] = idet * a23; m[ 8 ] = idet * a33;
  909. return this;
  910. },
  911. /*
  912. transpose: function () {
  913. var tmp, m = this.m;
  914. tmp = m[1]; m[1] = m[3]; m[3] = tmp;
  915. tmp = m[2]; m[2] = m[6]; m[6] = tmp;
  916. tmp = m[5]; m[5] = m[7]; m[7] = tmp;
  917. return this;
  918. },
  919. */
  920. transposeIntoArray: function ( r ) {
  921. var m = this.m;
  922. r[ 0 ] = m[ 0 ];
  923. r[ 1 ] = m[ 3 ];
  924. r[ 2 ] = m[ 6 ];
  925. r[ 3 ] = m[ 1 ];
  926. r[ 4 ] = m[ 4 ];
  927. r[ 5 ] = m[ 7 ];
  928. r[ 6 ] = m[ 2 ];
  929. r[ 7 ] = m[ 5 ];
  930. r[ 8 ] = m[ 8 ];
  931. return this;
  932. }
  933. };
  934. /**
  935. * @author mr.doob / http://mrdoob.com/
  936. * @author supereggbert / http://www.paulbrunt.co.uk/
  937. * @author philogb / http://blog.thejit.org/
  938. * @author jordi_ros / http://plattsoft.com
  939. * @author D1plo1d / http://github.com/D1plo1d
  940. * @author alteredq / http://alteredqualia.com/
  941. * @author mikael emtinger / http://gomo.se/
  942. * @author timknip / http://www.floorplanner.com/
  943. */
  944. THREE.Matrix4 = function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  945. this.elements = new Float32Array(16);
  946. this.set(
  947. ( n11 !== undefined ) ? n11 : 1, n12 || 0, n13 || 0, n14 || 0,
  948. n21 || 0, ( n22 !== undefined ) ? n22 : 1, n23 || 0, n24 || 0,
  949. n31 || 0, n32 || 0, ( n33 !== undefined ) ? n33 : 1, n34 || 0,
  950. n41 || 0, n42 || 0, n43 || 0, ( n44 !== undefined ) ? n44 : 1
  951. );
  952. };
  953. THREE.Matrix4.prototype = {
  954. constructor: THREE.Matrix4,
  955. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  956. var te = this.elements;
  957. te[0] = n11; te[4] = n12; te[8] = n13; te[12] = n14;
  958. te[1] = n21; te[5] = n22; te[9] = n23; te[13] = n24;
  959. te[2] = n31; te[6] = n32; te[10] = n33; te[14] = n34;
  960. te[3] = n41; te[7] = n42; te[11] = n43; te[15] = n44;
  961. return this;
  962. },
  963. identity: function () {
  964. this.set(
  965. 1, 0, 0, 0,
  966. 0, 1, 0, 0,
  967. 0, 0, 1, 0,
  968. 0, 0, 0, 1
  969. );
  970. return this;
  971. },
  972. copy: function ( m ) {
  973. this.set(
  974. m.elements[0], m.elements[4], m.elements[8], m.elements[12],
  975. m.elements[1], m.elements[5], m.elements[9], m.elements[13],
  976. m.elements[2], m.elements[6], m.elements[10], m.elements[14],
  977. m.elements[3], m.elements[7], m.elements[11], m.elements[15]
  978. );
  979. return this;
  980. },
  981. lookAt: function ( eye, target, up ) {
  982. var te = this.elements;
  983. var x = THREE.Matrix4.__v1;
  984. var y = THREE.Matrix4.__v2;
  985. var z = THREE.Matrix4.__v3;
  986. z.sub( eye, target ).normalize();
  987. if ( z.length() === 0 ) {
  988. z.z = 1;
  989. }
  990. x.cross( up, z ).normalize();
  991. if ( x.length() === 0 ) {
  992. z.x += 0.0001;
  993. x.cross( up, z ).normalize();
  994. }
  995. y.cross( z, x );
  996. te[0] = x.x; te[4] = y.x; te[8] = z.x;
  997. te[1] = x.y; te[5] = y.y; te[9] = z.y;
  998. te[2] = x.z; te[6] = y.z; te[10] = z.z;
  999. return this;
  1000. },
  1001. multiply: function ( a, b ) {
  1002. var ae = a.elements,
  1003. be = b.elements,
  1004. te = this.elements;
  1005. var a11 = ae[0], a12 = ae[4], a13 = ae[8], a14 = ae[12];
  1006. var a21 = ae[1], a22 = ae[5], a23 = ae[9], a24 = ae[13];
  1007. var a31 = ae[2], a32 = ae[6], a33 = ae[10], a34 = ae[14];
  1008. var a41 = ae[3], a42 = ae[7], a43 = ae[11], a44 = ae[15];
  1009. var b11 = be[0], b12 = be[4], b13 = be[8], b14 = be[12];
  1010. var b21 = be[1], b22 = be[5], b23 = be[9], b24 = be[13];
  1011. var b31 = be[2], b32 = be[6], b33 = be[10], b34 = be[14];
  1012. var b41 = be[3], b42 = be[7], b43 = be[11], b44 = be[15];
  1013. te[0] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  1014. te[4] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  1015. te[8] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  1016. te[12] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  1017. te[1] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  1018. te[5] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  1019. te[9] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  1020. te[13] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  1021. te[2] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  1022. te[6] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  1023. te[10] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  1024. te[14] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  1025. te[3] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  1026. te[7] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  1027. te[11] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  1028. te[15] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  1029. return this;
  1030. },
  1031. multiplySelf: function ( m ) {
  1032. return this.multiply( this, m );
  1033. },
  1034. multiplyToArray: function ( a, b, r ) {
  1035. var te = this.elements;
  1036. this.multiply( a, b );
  1037. r[ 0 ] = te[0]; r[ 1 ] = te[1]; r[ 2 ] = te[2]; r[ 3 ] = te[3];
  1038. r[ 4 ] = te[4]; r[ 5 ] = te[5]; r[ 6 ] = te[6]; r[ 7 ] = te[7];
  1039. r[ 8 ] = te[8]; r[ 9 ] = te[9]; r[ 10 ] = te[10]; r[ 11 ] = te[11];
  1040. r[ 12 ] = te[12]; r[ 13 ] = te[13]; r[ 14 ] = te[14]; r[ 15 ] = te[15];
  1041. return this;
  1042. },
  1043. multiplyScalar: function ( s ) {
  1044. var te = this.elements;
  1045. te[0] *= s; te[4] *= s; te[8] *= s; te[12] *= s;
  1046. te[1] *= s; te[5] *= s; te[9] *= s; te[13] *= s;
  1047. te[2] *= s; te[6] *= s; te[10] *= s; te[14] *= s;
  1048. te[3] *= s; te[7] *= s; te[11] *= s; te[15] *= s;
  1049. return this;
  1050. },
  1051. multiplyVector3: function ( v ) {
  1052. var te = this.elements;
  1053. var vx = v.x, vy = v.y, vz = v.z;
  1054. var d = 1 / ( te[3] * vx + te[7] * vy + te[11] * vz + te[15] );
  1055. v.x = ( te[0] * vx + te[4] * vy + te[8] * vz + te[12] ) * d;
  1056. v.y = ( te[1] * vx + te[5] * vy + te[9] * vz + te[13] ) * d;
  1057. v.z = ( te[2] * vx + te[6] * vy + te[10] * vz + te[14] ) * d;
  1058. return v;
  1059. },
  1060. multiplyVector4: function ( v ) {
  1061. var te = this.elements;
  1062. var vx = v.x, vy = v.y, vz = v.z, vw = v.w;
  1063. v.x = te[0] * vx + te[4] * vy + te[8] * vz + te[12] * vw;
  1064. v.y = te[1] * vx + te[5] * vy + te[9] * vz + te[13] * vw;
  1065. v.z = te[2] * vx + te[6] * vy + te[10] * vz + te[14] * vw;
  1066. v.w = te[3] * vx + te[7] * vy + te[11] * vz + te[15] * vw;
  1067. return v;
  1068. },
  1069. rotateAxis: function ( v ) {
  1070. var te = this.elements;
  1071. var vx = v.x, vy = v.y, vz = v.z;
  1072. v.x = vx * te[0] + vy * te[4] + vz * te[8];
  1073. v.y = vx * te[1] + vy * te[5] + vz * te[9];
  1074. v.z = vx * te[2] + vy * te[6] + vz * te[10];
  1075. v.normalize();
  1076. return v;
  1077. },
  1078. crossVector: function ( a ) {
  1079. var te = this.elements;
  1080. var v = new THREE.Vector4();
  1081. v.x = te[0] * a.x + te[4] * a.y + te[8] * a.z + te[12] * a.w;
  1082. v.y = te[1] * a.x + te[5] * a.y + te[9] * a.z + te[13] * a.w;
  1083. v.z = te[2] * a.x + te[6] * a.y + te[10] * a.z + te[14] * a.w;
  1084. v.w = ( a.w ) ? te[3] * a.x + te[7] * a.y + te[11] * a.z + te[15] * a.w : 1;
  1085. return v;
  1086. },
  1087. determinant: function () {
  1088. var te = this.elements;
  1089. var n11 = te[0], n12 = te[4], n13 = te[8], n14 = te[12];
  1090. var n21 = te[1], n22 = te[5], n23 = te[9], n24 = te[13];
  1091. var n31 = te[2], n32 = te[6], n33 = te[10], n34 = te[14];
  1092. var n41 = te[3], n42 = te[7], n43 = te[11], n44 = te[15];
  1093. //TODO: make this more efficient
  1094. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  1095. return (
  1096. n14 * n23 * n32 * n41-
  1097. n13 * n24 * n32 * n41-
  1098. n14 * n22 * n33 * n41+
  1099. n12 * n24 * n33 * n41+
  1100. n13 * n22 * n34 * n41-
  1101. n12 * n23 * n34 * n41-
  1102. n14 * n23 * n31 * n42+
  1103. n13 * n24 * n31 * n42+
  1104. n14 * n21 * n33 * n42-
  1105. n11 * n24 * n33 * n42-
  1106. n13 * n21 * n34 * n42+
  1107. n11 * n23 * n34 * n42+
  1108. n14 * n22 * n31 * n43-
  1109. n12 * n24 * n31 * n43-
  1110. n14 * n21 * n32 * n43+
  1111. n11 * n24 * n32 * n43+
  1112. n12 * n21 * n34 * n43-
  1113. n11 * n22 * n34 * n43-
  1114. n13 * n22 * n31 * n44+
  1115. n12 * n23 * n31 * n44+
  1116. n13 * n21 * n32 * n44-
  1117. n11 * n23 * n32 * n44-
  1118. n12 * n21 * n33 * n44+
  1119. n11 * n22 * n33 * n44
  1120. );
  1121. },
  1122. transpose: function () {
  1123. var te = this.elements;
  1124. var tmp;
  1125. tmp = te[1]; te[1] = te[4]; te[4] = tmp;
  1126. tmp = te[2]; te[2] = te[8]; te[8] = tmp;
  1127. tmp = te[6]; te[6] = te[9]; te[9] = tmp;
  1128. tmp = te[3]; te[3] = te[12]; te[12] = tmp;
  1129. tmp = te[7]; te[7] = te[13]; te[13] = tmp;
  1130. tmp = te[11]; te[11] = te[14]; te[14] = tmp;
  1131. return this;
  1132. },
  1133. flattenToArray: function ( flat ) {
  1134. var te = this.elements;
  1135. flat[ 0 ] = te[0]; flat[ 1 ] = te[1]; flat[ 2 ] = te[2]; flat[ 3 ] = te[3];
  1136. flat[ 4 ] = te[4]; flat[ 5 ] = te[5]; flat[ 6 ] = te[6]; flat[ 7 ] = te[7];
  1137. flat[ 8 ] = te[8]; flat[ 9 ] = te[9]; flat[ 10 ] = te[10]; flat[ 11 ] = te[11];
  1138. flat[ 12 ] = te[12]; flat[ 13 ] = te[13]; flat[ 14 ] = te[14]; flat[ 15 ] = te[15];
  1139. return flat;
  1140. },
  1141. flattenToArrayOffset: function( flat, offset ) {
  1142. var te = this.elements;
  1143. flat[ offset ] = te[0];
  1144. flat[ offset + 1 ] = te[1];
  1145. flat[ offset + 2 ] = te[2];
  1146. flat[ offset + 3 ] = te[3];
  1147. flat[ offset + 4 ] = te[4];
  1148. flat[ offset + 5 ] = te[5];
  1149. flat[ offset + 6 ] = te[6];
  1150. flat[ offset + 7 ] = te[7];
  1151. flat[ offset + 8 ] = te[8];
  1152. flat[ offset + 9 ] = te[9];
  1153. flat[ offset + 10 ] = te[10];
  1154. flat[ offset + 11 ] = te[11];
  1155. flat[ offset + 12 ] = te[12];
  1156. flat[ offset + 13 ] = te[13];
  1157. flat[ offset + 14 ] = te[14];
  1158. flat[ offset + 15 ] = te[15];
  1159. return flat;
  1160. },
  1161. getPosition: function () {
  1162. var te = this.elements;
  1163. return THREE.Matrix4.__v1.set( te[12], te[13], te[14] );
  1164. },
  1165. setPosition: function ( v ) {
  1166. var te = this.elements;
  1167. te[12] = v.x;
  1168. te[13] = v.y;
  1169. te[14] = v.z;
  1170. return this;
  1171. },
  1172. getColumnX: function () {
  1173. var te = this.elements;
  1174. return THREE.Matrix4.__v1.set( te[0], te[1], te[2] );
  1175. },
  1176. getColumnY: function () {
  1177. var te = this.elements;
  1178. return THREE.Matrix4.__v1.set( te[4], te[5], te[6] );
  1179. },
  1180. getColumnZ: function() {
  1181. var te = this.elements;
  1182. return THREE.Matrix4.__v1.set( te[8], te[9], te[10] );
  1183. },
  1184. getInverse: function ( m ) {
  1185. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  1186. var te = this.elements;
  1187. var n11 = m.elements[0], n12 = m.elements[4], n13 = m.elements[8], n14 = m.elements[12];
  1188. var n21 = m.elements[1], n22 = m.elements[5], n23 = m.elements[9], n24 = m.elements[13];
  1189. var n31 = m.elements[2], n32 = m.elements[6], n33 = m.elements[10], n34 = m.elements[14];
  1190. var n41 = m.elements[3], n42 = m.elements[7], n43 = m.elements[11], n44 = m.elements[15];
  1191. te[0] = n23*n34*n42 - n24*n33*n42 + n24*n32*n43 - n22*n34*n43 - n23*n32*n44 + n22*n33*n44;
  1192. te[4] = n14*n33*n42 - n13*n34*n42 - n14*n32*n43 + n12*n34*n43 + n13*n32*n44 - n12*n33*n44;
  1193. te[8] = n13*n24*n42 - n14*n23*n42 + n14*n22*n43 - n12*n24*n43 - n13*n22*n44 + n12*n23*n44;
  1194. te[12] = n14*n23*n32 - n13*n24*n32 - n14*n22*n33 + n12*n24*n33 + n13*n22*n34 - n12*n23*n34;
  1195. te[1] = n24*n33*n41 - n23*n34*n41 - n24*n31*n43 + n21*n34*n43 + n23*n31*n44 - n21*n33*n44;
  1196. te[5] = n13*n34*n41 - n14*n33*n41 + n14*n31*n43 - n11*n34*n43 - n13*n31*n44 + n11*n33*n44;
  1197. te[9] = n14*n23*n41 - n13*n24*n41 - n14*n21*n43 + n11*n24*n43 + n13*n21*n44 - n11*n23*n44;
  1198. te[13] = n13*n24*n31 - n14*n23*n31 + n14*n21*n33 - n11*n24*n33 - n13*n21*n34 + n11*n23*n34;
  1199. te[2] = n22*n34*n41 - n24*n32*n41 + n24*n31*n42 - n21*n34*n42 - n22*n31*n44 + n21*n32*n44;
  1200. te[6] = n14*n32*n41 - n12*n34*n41 - n14*n31*n42 + n11*n34*n42 + n12*n31*n44 - n11*n32*n44;
  1201. te[10] = n12*n24*n41 - n14*n22*n41 + n14*n21*n42 - n11*n24*n42 - n12*n21*n44 + n11*n22*n44;
  1202. te[14] = n14*n22*n31 - n12*n24*n31 - n14*n21*n32 + n11*n24*n32 + n12*n21*n34 - n11*n22*n34;
  1203. te[3] = n23*n32*n41 - n22*n33*n41 - n23*n31*n42 + n21*n33*n42 + n22*n31*n43 - n21*n32*n43;
  1204. te[7] = n12*n33*n41 - n13*n32*n41 + n13*n31*n42 - n11*n33*n42 - n12*n31*n43 + n11*n32*n43;
  1205. te[11] = n13*n22*n41 - n12*n23*n41 - n13*n21*n42 + n11*n23*n42 + n12*n21*n43 - n11*n22*n43;
  1206. te[15] = n12*n23*n31 - n13*n22*n31 + n13*n21*n32 - n11*n23*n32 - n12*n21*n33 + n11*n22*n33;
  1207. this.multiplyScalar( 1 / m.determinant() );
  1208. return this;
  1209. },
  1210. setRotationFromEuler: function( v, order ) {
  1211. var te = this.elements;
  1212. var x = v.x, y = v.y, z = v.z;
  1213. var a = Math.cos( x ), b = Math.sin( x );
  1214. var c = Math.cos( y ), d = Math.sin( y );
  1215. var e = Math.cos( z ), f = Math.sin( z );
  1216. switch ( order ) {
  1217. case 'YXZ':
  1218. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  1219. te[0] = ce + df * b;
  1220. te[4] = de * b - cf;
  1221. te[8] = a * d;
  1222. te[1] = a * f;
  1223. te[5] = a * e;
  1224. te[9] = - b;
  1225. te[2] = cf * b - de;
  1226. te[6] = df + ce * b;
  1227. te[10] = a * c;
  1228. break;
  1229. case 'ZXY':
  1230. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  1231. te[0] = ce - df * b;
  1232. te[4] = - a * f;
  1233. te[8] = de + cf * b;
  1234. te[1] = cf + de * b;
  1235. te[5] = a * e;
  1236. te[9] = df - ce * b;
  1237. te[2] = - a * d;
  1238. te[6] = b;
  1239. te[10] = a * c;
  1240. break;
  1241. case 'ZYX':
  1242. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  1243. te[0] = c * e;
  1244. te[4] = be * d - af;
  1245. te[8] = ae * d + bf;
  1246. te[1] = c * f;
  1247. te[5] = bf * d + ae;
  1248. te[9] = af * d - be;
  1249. te[2] = - d;
  1250. te[6] = b * c;
  1251. te[10] = a * c;
  1252. break;
  1253. case 'YZX':
  1254. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  1255. te[0] = c * e;
  1256. te[4] = bd - ac * f;
  1257. te[8] = bc * f + ad;
  1258. te[1] = f;
  1259. te[5] = a * e;
  1260. te[9] = - b * e;
  1261. te[2] = - d * e;
  1262. te[6] = ad * f + bc;
  1263. te[10] = ac - bd * f;
  1264. break;
  1265. case 'XZY':
  1266. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  1267. te[0] = c * e;
  1268. te[4] = - f;
  1269. te[8] = d * e;
  1270. te[1] = ac * f + bd;
  1271. te[5] = a * e;
  1272. te[9] = ad * f - bc;
  1273. te[2] = bc * f - ad;
  1274. te[6] = b * e;
  1275. te[10] = bd * f + ac;
  1276. break;
  1277. default: // 'XYZ'
  1278. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  1279. te[0] = c * e;
  1280. te[4] = - c * f;
  1281. te[8] = d;
  1282. te[1] = af + be * d;
  1283. te[5] = ae - bf * d;
  1284. te[9] = - b * c;
  1285. te[2] = bf - ae * d;
  1286. te[6] = be + af * d;
  1287. te[10] = a * c;
  1288. break;
  1289. }
  1290. return this;
  1291. },
  1292. setRotationFromQuaternion: function( q ) {
  1293. var te = this.elements;
  1294. var x = q.x, y = q.y, z = q.z, w = q.w;
  1295. var x2 = x + x, y2 = y + y, z2 = z + z;
  1296. var xx = x * x2, xy = x * y2, xz = x * z2;
  1297. var yy = y * y2, yz = y * z2, zz = z * z2;
  1298. var wx = w * x2, wy = w * y2, wz = w * z2;
  1299. te[0] = 1 - ( yy + zz );
  1300. te[4] = xy - wz;
  1301. te[8] = xz + wy;
  1302. te[1] = xy + wz;
  1303. te[5] = 1 - ( xx + zz );
  1304. te[9] = yz - wx;
  1305. te[2] = xz - wy;
  1306. te[6] = yz + wx;
  1307. te[10] = 1 - ( xx + yy );
  1308. return this;
  1309. },
  1310. compose: function ( translation, rotation, scale ) {
  1311. var te = this.elements;
  1312. var mRotation = THREE.Matrix4.__m1;
  1313. var mScale = THREE.Matrix4.__m2;
  1314. mRotation.identity();
  1315. mRotation.setRotationFromQuaternion( rotation );
  1316. mScale.makeScale( scale.x, scale.y, scale.z );
  1317. this.multiply( mRotation, mScale );
  1318. te[12] = translation.x;
  1319. te[13] = translation.y;
  1320. te[14] = translation.z;
  1321. return this;
  1322. },
  1323. decompose: function ( translation, rotation, scale ) {
  1324. // grab the axis vectors
  1325. var te = this.elements;
  1326. var x = THREE.Matrix4.__v1;
  1327. var y = THREE.Matrix4.__v2;
  1328. var z = THREE.Matrix4.__v3;
  1329. x.set( te[0], te[1], te[2] );
  1330. y.set( te[4], te[5], te[6] );
  1331. z.set( te[8], te[9], te[10] );
  1332. translation = ( translation instanceof THREE.Vector3 ) ? translation : new THREE.Vector3();
  1333. rotation = ( rotation instanceof THREE.Quaternion ) ? rotation : new THREE.Quaternion();
  1334. scale = ( scale instanceof THREE.Vector3 ) ? scale : new THREE.Vector3();
  1335. scale.x = x.length();
  1336. scale.y = y.length();
  1337. scale.z = z.length();
  1338. translation.x = te[12];
  1339. translation.y = te[13];
  1340. translation.z = te[14];
  1341. // scale the rotation part
  1342. var matrix = THREE.Matrix4.__m1;
  1343. matrix.copy( this );
  1344. matrix.elements[0] /= scale.x;
  1345. matrix.elements[1] /= scale.x;
  1346. matrix.elements[2] /= scale.x;
  1347. matrix.elements[4] /= scale.y;
  1348. matrix.elements[5] /= scale.y;
  1349. matrix.elements[6] /= scale.y;
  1350. matrix.elements[8] /= scale.z;
  1351. matrix.elements[9] /= scale.z;
  1352. matrix.elements[10] /= scale.z;
  1353. rotation.setFromRotationMatrix( matrix );
  1354. return [ translation, rotation, scale ];
  1355. },
  1356. extractPosition: function ( m ) {
  1357. var te = this.elements;
  1358. te[12] = m.elements[12];
  1359. te[13] = m.elements[13];
  1360. te[14] = m.elements[14];
  1361. return this;
  1362. },
  1363. extractRotation: function ( m ) {
  1364. var te = this.elements;
  1365. var vector = THREE.Matrix4.__v1;
  1366. var scaleX = 1 / vector.set( m.elements[0], m.elements[1], m.elements[2] ).length();
  1367. var scaleY = 1 / vector.set( m.elements[4], m.elements[5], m.elements[6] ).length();
  1368. var scaleZ = 1 / vector.set( m.elements[8], m.elements[9], m.elements[10] ).length();
  1369. te[0] = m.elements[0] * scaleX;
  1370. te[1] = m.elements[1] * scaleX;
  1371. te[2] = m.elements[2] * scaleX;
  1372. te[4] = m.elements[4] * scaleY;
  1373. te[5] = m.elements[5] * scaleY;
  1374. te[6] = m.elements[6] * scaleY;
  1375. te[8] = m.elements[8] * scaleZ;
  1376. te[9] = m.elements[9] * scaleZ;
  1377. te[10] = m.elements[10] * scaleZ;
  1378. return this;
  1379. },
  1380. //
  1381. translate: function ( v ) {
  1382. var te = this.elements;
  1383. var x = v.x, y = v.y, z = v.z;
  1384. te[12] = te[0] * x + te[4] * y + te[8] * z + te[12];
  1385. te[13] = te[1] * x + te[5] * y + te[9] * z + te[13];
  1386. te[14] = te[2] * x + te[6] * y + te[10] * z + te[14];
  1387. te[15] = te[3] * x + te[7] * y + te[11] * z + te[15];
  1388. return this;
  1389. },
  1390. rotateX: function ( angle ) {
  1391. var te = this.elements;
  1392. var m12 = te[4];
  1393. var m22 = te[5];
  1394. var m32 = te[6];
  1395. var m42 = te[7];
  1396. var m13 = te[8];
  1397. var m23 = te[9];
  1398. var m33 = te[10];
  1399. var m43 = te[11];
  1400. var c = Math.cos( angle );
  1401. var s = Math.sin( angle );
  1402. te[4] = c * m12 + s * m13;
  1403. te[5] = c * m22 + s * m23;
  1404. te[6] = c * m32 + s * m33;
  1405. te[7] = c * m42 + s * m43;
  1406. te[8] = c * m13 - s * m12;
  1407. te[9] = c * m23 - s * m22;
  1408. te[10] = c * m33 - s * m32;
  1409. te[11] = c * m43 - s * m42;
  1410. return this;
  1411. },
  1412. rotateY: function ( angle ) {
  1413. var te = this.elements;
  1414. var m11 = te[0];
  1415. var m21 = te[1];
  1416. var m31 = te[2];
  1417. var m41 = te[3];
  1418. var m13 = te[8];
  1419. var m23 = te[9];
  1420. var m33 = te[10];
  1421. var m43 = te[11];
  1422. var c = Math.cos( angle );
  1423. var s = Math.sin( angle );
  1424. te[0] = c * m11 - s * m13;
  1425. te[1] = c * m21 - s * m23;
  1426. te[2] = c * m31 - s * m33;
  1427. te[3] = c * m41 - s * m43;
  1428. te[8] = c * m13 + s * m11;
  1429. te[9] = c * m23 + s * m21;
  1430. te[10] = c * m33 + s * m31;
  1431. te[11] = c * m43 + s * m41;
  1432. return this;
  1433. },
  1434. rotateZ: function ( angle ) {
  1435. var te = this.elements;
  1436. var m11 = te[0];
  1437. var m21 = te[1];
  1438. var m31 = te[2];
  1439. var m41 = te[3];
  1440. var m12 = te[4];
  1441. var m22 = te[5];
  1442. var m32 = te[6];
  1443. var m42 = te[7];
  1444. var c = Math.cos( angle );
  1445. var s = Math.sin( angle );
  1446. te[0] = c * m11 + s * m12;
  1447. te[1] = c * m21 + s * m22;
  1448. te[2] = c * m31 + s * m32;
  1449. te[3] = c * m41 + s * m42;
  1450. te[4] = c * m12 - s * m11;
  1451. te[5] = c * m22 - s * m21;
  1452. te[6] = c * m32 - s * m31;
  1453. te[7] = c * m42 - s * m41;
  1454. return this;
  1455. },
  1456. rotateByAxis: function ( axis, angle ) {
  1457. var te = this.elements;
  1458. // optimize by checking axis
  1459. if ( axis.x === 1 && axis.y === 0 && axis.z === 0 ) {
  1460. return this.rotateX( angle );
  1461. } else if ( axis.x === 0 && axis.y === 1 && axis.z === 0 ) {
  1462. return this.rotateY( angle );
  1463. } else if ( axis.x === 0 && axis.y === 0 && axis.z === 1 ) {
  1464. return this.rotateZ( angle );
  1465. }
  1466. var x = axis.x, y = axis.y, z = axis.z;
  1467. var n = Math.sqrt(x * x + y * y + z * z);
  1468. x /= n;
  1469. y /= n;
  1470. z /= n;
  1471. var xx = x * x, yy = y * y, zz = z * z;
  1472. var c = Math.cos( angle );
  1473. var s = Math.sin( angle );
  1474. var oneMinusCosine = 1 - c;
  1475. var xy = x * y * oneMinusCosine;
  1476. var xz = x * z * oneMinusCosine;
  1477. var yz = y * z * oneMinusCosine;
  1478. var xs = x * s;
  1479. var ys = y * s;
  1480. var zs = z * s;
  1481. var r11 = xx + (1 - xx) * c;
  1482. var r21 = xy + zs;
  1483. var r31 = xz - ys;
  1484. var r12 = xy - zs;
  1485. var r22 = yy + (1 - yy) * c;
  1486. var r32 = yz + xs;
  1487. var r13 = xz + ys;
  1488. var r23 = yz - xs;
  1489. var r33 = zz + (1 - zz) * c;
  1490. var m11 = te[0], m21 = te[1], m31 = te[2], m41 = te[3];
  1491. var m12 = te[4], m22 = te[5], m32 = te[6], m42 = te[7];
  1492. var m13 = te[8], m23 = te[9], m33 = te[10], m43 = te[11];
  1493. var m14 = te[12], m24 = te[13], m34 = te[14], m44 = te[15];
  1494. te[0] = r11 * m11 + r21 * m12 + r31 * m13;
  1495. te[1] = r11 * m21 + r21 * m22 + r31 * m23;
  1496. te[2] = r11 * m31 + r21 * m32 + r31 * m33;
  1497. te[3] = r11 * m41 + r21 * m42 + r31 * m43;
  1498. te[4] = r12 * m11 + r22 * m12 + r32 * m13;
  1499. te[5] = r12 * m21 + r22 * m22 + r32 * m23;
  1500. te[6] = r12 * m31 + r22 * m32 + r32 * m33;
  1501. te[7] = r12 * m41 + r22 * m42 + r32 * m43;
  1502. te[8] = r13 * m11 + r23 * m12 + r33 * m13;
  1503. te[9] = r13 * m21 + r23 * m22 + r33 * m23;
  1504. te[10] = r13 * m31 + r23 * m32 + r33 * m33;
  1505. te[11] = r13 * m41 + r23 * m42 + r33 * m43;
  1506. return this;
  1507. },
  1508. scale: function ( v ) {
  1509. var te = this.elements;
  1510. var x = v.x, y = v.y, z = v.z;
  1511. te[0] *= x; te[4] *= y; te[8] *= z;
  1512. te[1] *= x; te[5] *= y; te[9] *= z;
  1513. te[2] *= x; te[6] *= y; te[10] *= z;
  1514. te[3] *= x; te[7] *= y; te[11] *= z;
  1515. return this;
  1516. },
  1517. //
  1518. makeTranslation: function ( x, y, z ) {
  1519. this.set(
  1520. 1, 0, 0, x,
  1521. 0, 1, 0, y,
  1522. 0, 0, 1, z,
  1523. 0, 0, 0, 1
  1524. );
  1525. return this;
  1526. },
  1527. makeRotationX: function ( theta ) {
  1528. var c = Math.cos( theta ), s = Math.sin( theta );
  1529. this.set(
  1530. 1, 0, 0, 0,
  1531. 0, c, -s, 0,
  1532. 0, s, c, 0,
  1533. 0, 0, 0, 1
  1534. );
  1535. return this;
  1536. },
  1537. makeRotationY: function ( theta ) {
  1538. var c = Math.cos( theta ), s = Math.sin( theta );
  1539. this.set(
  1540. c, 0, s, 0,
  1541. 0, 1, 0, 0,
  1542. -s, 0, c, 0,
  1543. 0, 0, 0, 1
  1544. );
  1545. return this;
  1546. },
  1547. makeRotationZ: function ( theta ) {
  1548. var c = Math.cos( theta ), s = Math.sin( theta );
  1549. this.set(
  1550. c, -s, 0, 0,
  1551. s, c, 0, 0,
  1552. 0, 0, 1, 0,
  1553. 0, 0, 0, 1
  1554. );
  1555. return this;
  1556. },
  1557. makeRotationAxis: function ( axis, angle ) {
  1558. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  1559. var c = Math.cos( angle );
  1560. var s = Math.sin( angle );
  1561. var t = 1 - c;
  1562. var x = axis.x, y = axis.y, z = axis.z;
  1563. var tx = t * x, ty = t * y;
  1564. this.set(
  1565. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  1566. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  1567. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  1568. 0, 0, 0, 1
  1569. );
  1570. return this;
  1571. },
  1572. makeScale: function ( x, y, z ) {
  1573. this.set(
  1574. x, 0, 0, 0,
  1575. 0, y, 0, 0,
  1576. 0, 0, z, 0,
  1577. 0, 0, 0, 1
  1578. );
  1579. return this;
  1580. },
  1581. makeFrustum: function ( left, right, bottom, top, near, far ) {
  1582. var te = this.elements;
  1583. var x = 2 * near / ( right - left );
  1584. var y = 2 * near / ( top - bottom );
  1585. var a = ( right + left ) / ( right - left );
  1586. var b = ( top + bottom ) / ( top - bottom );
  1587. var c = - ( far + near ) / ( far - near );
  1588. var d = - 2 * far * near / ( far - near );
  1589. te[0] = x; te[4] = 0; te[8] = a; te[12] = 0;
  1590. te[1] = 0; te[5] = y; te[9] = b; te[13] = 0;
  1591. te[2] = 0; te[6] = 0; te[10] = c; te[14] = d;
  1592. te[3] = 0; te[7] = 0; te[11] = - 1; te[15] = 0;
  1593. return this;
  1594. },
  1595. makePerspective: function ( fov, aspect, near, far ) {
  1596. var ymax = near * Math.tan( fov * Math.PI / 360 );
  1597. var ymin = - ymax;
  1598. var xmin = ymin * aspect;
  1599. var xmax = ymax * aspect;
  1600. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  1601. },
  1602. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  1603. var te = this.elements;
  1604. var w = right - left;
  1605. var h = top - bottom;
  1606. var p = far - near;
  1607. var x = ( right + left ) / w;
  1608. var y = ( top + bottom ) / h;
  1609. var z = ( far + near ) / p;
  1610. te[0] = 2 / w; te[4] = 0; te[8] = 0; te[12] = -x;
  1611. te[1] = 0; te[5] = 2 / h; te[9] = 0; te[13] = -y;
  1612. te[2] = 0; te[6] = 0; te[10] = -2 / p; te[14] = -z;
  1613. te[3] = 0; te[7] = 0; te[11] = 0; te[15] = 1;
  1614. return this;
  1615. },
  1616. clone: function () {
  1617. var te = this.elements;
  1618. return new THREE.Matrix4(
  1619. te[0], te[4], te[8], te[12],
  1620. te[1], te[5], te[9], te[13],
  1621. te[2], te[6], te[10], te[14],
  1622. te[3], te[7], te[11], te[15]
  1623. );
  1624. }
  1625. };
  1626. THREE.Matrix4.__v1 = new THREE.Vector3();
  1627. THREE.Matrix4.__v2 = new THREE.Vector3();
  1628. THREE.Matrix4.__v3 = new THREE.Vector3();
  1629. THREE.Matrix4.__m1 = new THREE.Matrix4();
  1630. THREE.Matrix4.__m2 = new THREE.Matrix4();
  1631. /**
  1632. * @author mr.doob / http://mrdoob.com/
  1633. * @author mikael emtinger / http://gomo.se/
  1634. * @author alteredq / http://alteredqualia.com/
  1635. */
  1636. THREE.Object3D = function () {
  1637. this.id = THREE.Object3DCount ++;
  1638. this.name = '';
  1639. this.parent = undefined;
  1640. this.children = [];
  1641. this.up = new THREE.Vector3( 0, 1, 0 );
  1642. this.position = new THREE.Vector3();
  1643. this.rotation = new THREE.Vector3();
  1644. this.eulerOrder = 'XYZ';
  1645. this.scale = new THREE.Vector3( 1, 1, 1 );
  1646. this.doubleSided = false;
  1647. this.flipSided = false;
  1648. this.renderDepth = null;
  1649. this.rotationAutoUpdate = true;
  1650. this.matrix = new THREE.Matrix4();
  1651. this.matrixWorld = new THREE.Matrix4();
  1652. this.matrixRotationWorld = new THREE.Matrix4();
  1653. this.matrixAutoUpdate = true;
  1654. this.matrixWorldNeedsUpdate = true;
  1655. this.quaternion = new THREE.Quaternion();
  1656. this.useQuaternion = false;
  1657. this.boundRadius = 0.0;
  1658. this.boundRadiusScale = 1.0;
  1659. this.visible = true;
  1660. this.castShadow = false;
  1661. this.receiveShadow = false;
  1662. this.frustumCulled = true;
  1663. this._vector = new THREE.Vector3();
  1664. };
  1665. THREE.Object3D.prototype = {
  1666. constructor: THREE.Object3D,
  1667. applyMatrix: function ( matrix ) {
  1668. this.matrix.multiply( matrix, this.matrix );
  1669. this.scale.getScaleFromMatrix( this.matrix );
  1670. this.rotation.getRotationFromMatrix( this.matrix, this.scale );
  1671. this.position.getPositionFromMatrix( this.matrix );
  1672. },
  1673. translate: function ( distance, axis ) {
  1674. this.matrix.rotateAxis( axis );
  1675. this.position.addSelf( axis.multiplyScalar( distance ) );
  1676. },
  1677. translateX: function ( distance ) {
  1678. this.translate( distance, this._vector.set( 1, 0, 0 ) );
  1679. },
  1680. translateY: function ( distance ) {
  1681. this.translate( distance, this._vector.set( 0, 1, 0 ) );
  1682. },
  1683. translateZ: function ( distance ) {
  1684. this.translate( distance, this._vector.set( 0, 0, 1 ) );
  1685. },
  1686. lookAt: function ( vector ) {
  1687. // TODO: Add hierarchy support.
  1688. this.matrix.lookAt( vector, this.position, this.up );
  1689. if ( this.rotationAutoUpdate ) {
  1690. this.rotation.getRotationFromMatrix( this.matrix );
  1691. }
  1692. },
  1693. add: function ( object ) {
  1694. if ( object === this ) {
  1695. console.warn( 'THREE.Object3D.add: An object can\'t be added as a child of itself.' );
  1696. return;
  1697. }
  1698. if ( this.children.indexOf( object ) === - 1 ) {
  1699. if ( object.parent !== undefined ) {
  1700. object.parent.remove( object );
  1701. }
  1702. object.parent = this;
  1703. this.children.push( object );
  1704. // add to scene
  1705. var scene = this;
  1706. while ( scene.parent !== undefined ) {
  1707. scene = scene.parent;
  1708. }
  1709. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  1710. scene.__addObject( object );
  1711. }
  1712. }
  1713. },
  1714. remove: function ( object ) {
  1715. var index = this.children.indexOf( object );
  1716. if ( index !== - 1 ) {
  1717. object.parent = undefined;
  1718. this.children.splice( index, 1 );
  1719. // remove from scene
  1720. var scene = this;
  1721. while ( scene.parent !== undefined ) {
  1722. scene = scene.parent;
  1723. }
  1724. if ( scene !== undefined && scene instanceof THREE.Scene ) {
  1725. scene.__removeObject( object );
  1726. }
  1727. }
  1728. },
  1729. getChildByName: function ( name, recursive ) {
  1730. var c, cl, child;
  1731. for ( c = 0, cl = this.children.length; c < cl; c ++ ) {
  1732. child = this.children[ c ];
  1733. if ( child.name === name ) {
  1734. return child;
  1735. }
  1736. if ( recursive ) {
  1737. child = child.getChildByName( name, recursive );
  1738. if ( child !== undefined ) {
  1739. return child;
  1740. }
  1741. }
  1742. }
  1743. return undefined;
  1744. },
  1745. updateMatrix: function () {
  1746. this.matrix.setPosition( this.position );
  1747. if ( this.useQuaternion ) {
  1748. this.matrix.setRotationFromQuaternion( this.quaternion );
  1749. } else {
  1750. this.matrix.setRotationFromEuler( this.rotation, this.eulerOrder );
  1751. }
  1752. if ( this.scale.x !== 1 || this.scale.y !== 1 || this.scale.z !== 1 ) {
  1753. this.matrix.scale( this.scale );
  1754. this.boundRadiusScale = Math.max( this.scale.x, Math.max( this.scale.y, this.scale.z ) );
  1755. }
  1756. this.matrixWorldNeedsUpdate = true;
  1757. },
  1758. updateMatrixWorld: function ( force ) {
  1759. this.matrixAutoUpdate && this.updateMatrix();
  1760. // update matrixWorld
  1761. if ( this.matrixWorldNeedsUpdate || force ) {
  1762. if ( this.parent ) {
  1763. this.matrixWorld.multiply( this.parent.matrixWorld, this.matrix );
  1764. } else {
  1765. this.matrixWorld.copy( this.matrix );
  1766. }
  1767. this.matrixWorldNeedsUpdate = false;
  1768. force = true;
  1769. }
  1770. // update children
  1771. for ( var i = 0, l = this.children.length; i < l; i ++ ) {
  1772. this.children[ i ].updateMatrixWorld( force );
  1773. }
  1774. }
  1775. };
  1776. THREE.Object3DCount = 0;
  1777. /**
  1778. * @author mr.doob / http://mrdoob.com/
  1779. * @author supereggbert / http://www.paulbrunt.co.uk/
  1780. * @author julianwa / https://github.com/julianwa
  1781. */
  1782. THREE.Projector = function() {
  1783. var _object, _objectCount, _objectPool = [],
  1784. _vertex, _vertexCount, _vertexPool = [],
  1785. _face, _face3Count, _face3Pool = [], _face4Count, _face4Pool = [],
  1786. _line, _lineCount, _linePool = [],
  1787. _particle, _particleCount, _particlePool = [],
  1788. _renderData = { objects: [], sprites: [], lights: [], elements: [] },
  1789. _vector3 = new THREE.Vector3(),
  1790. _vector4 = new THREE.Vector4(),
  1791. _projScreenMatrix = new THREE.Matrix4(),
  1792. _projScreenobjectMatrixWorld = new THREE.Matrix4(),
  1793. _frustum = new THREE.Frustum(),
  1794. _clippedVertex1PositionScreen = new THREE.Vector4(),
  1795. _clippedVertex2PositionScreen = new THREE.Vector4(),
  1796. _face3VertexNormals;
  1797. this.projectVector = function ( vector, camera ) {
  1798. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1799. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1800. _projScreenMatrix.multiplyVector3( vector );
  1801. return vector;
  1802. };
  1803. this.unprojectVector = function ( vector, camera ) {
  1804. camera.projectionMatrixInverse.getInverse( camera.projectionMatrix );
  1805. _projScreenMatrix.multiply( camera.matrixWorld, camera.projectionMatrixInverse );
  1806. _projScreenMatrix.multiplyVector3( vector );
  1807. return vector;
  1808. };
  1809. this.pickingRay = function ( vector, camera ) {
  1810. var end, ray, t;
  1811. // set two vectors with opposing z values
  1812. vector.z = -1.0;
  1813. end = new THREE.Vector3( vector.x, vector.y, 1.0 );
  1814. this.unprojectVector( vector, camera );
  1815. this.unprojectVector( end, camera );
  1816. // find direction from vector to end
  1817. end.subSelf( vector ).normalize();
  1818. return new THREE.Ray( vector, end );
  1819. };
  1820. this.projectGraph = function ( root, sort ) {
  1821. _objectCount = 0;
  1822. _renderData.objects.length = 0;
  1823. _renderData.sprites.length = 0;
  1824. _renderData.lights.length = 0;
  1825. var projectObject = function ( object ) {
  1826. if ( object.visible === false ) return;
  1827. if ( ( object instanceof THREE.Mesh || object instanceof THREE.Line ) &&
  1828. ( object.frustumCulled === false || _frustum.contains( object ) ) ) {
  1829. _vector3.copy( object.matrixWorld.getPosition() );
  1830. _projScreenMatrix.multiplyVector3( _vector3 );
  1831. _object = getNextObjectInPool();
  1832. _object.object = object;
  1833. _object.z = _vector3.z;
  1834. _renderData.objects.push( _object );
  1835. } else if ( object instanceof THREE.Sprite || object instanceof THREE.Particle ) {
  1836. _vector3.copy( object.matrixWorld.getPosition() );
  1837. _projScreenMatrix.multiplyVector3( _vector3 );
  1838. _object = getNextObjectInPool();
  1839. _object.object = object;
  1840. _object.z = _vector3.z;
  1841. _renderData.sprites.push( _object );
  1842. } else if ( object instanceof THREE.Light ) {
  1843. _renderData.lights.push( object );
  1844. }
  1845. for ( var c = 0, cl = object.children.length; c < cl; c ++ ) {
  1846. projectObject( object.children[ c ] );
  1847. }
  1848. };
  1849. projectObject( root );
  1850. sort && _renderData.objects.sort( painterSort );
  1851. return _renderData;
  1852. };
  1853. this.projectScene = function ( scene, camera, sort ) {
  1854. var near = camera.near, far = camera.far, visible = false,
  1855. o, ol, v, vl, f, fl, n, nl, c, cl, u, ul, object,
  1856. objectMatrixWorld, objectMatrixWorldRotation,
  1857. geometry, geometryMaterials, vertices, vertex, vertexPositionScreen,
  1858. faces, face, faceVertexNormals, normal, faceVertexUvs, uvs,
  1859. v1, v2, v3, v4;
  1860. _face3Count = 0;
  1861. _face4Count = 0;
  1862. _lineCount = 0;
  1863. _particleCount = 0;
  1864. _renderData.elements.length = 0;
  1865. if ( camera.parent === undefined ) {
  1866. console.warn( 'DEPRECATED: Camera hasn\'t been added to a Scene. Adding it...' );
  1867. scene.add( camera );
  1868. }
  1869. scene.updateMatrixWorld();
  1870. camera.matrixWorldInverse.getInverse( camera.matrixWorld );
  1871. _projScreenMatrix.multiply( camera.projectionMatrix, camera.matrixWorldInverse );
  1872. _frustum.setFromMatrix( _projScreenMatrix );
  1873. _renderData = this.projectGraph( scene, false );
  1874. for ( o = 0, ol = _renderData.objects.length; o < ol; o++ ) {
  1875. object = _renderData.objects[ o ].object;
  1876. objectMatrixWorld = object.matrixWorld;
  1877. _vertexCount = 0;
  1878. if ( object instanceof THREE.Mesh ) {
  1879. geometry = object.geometry;
  1880. geometryMaterials = object.geometry.materials;
  1881. vertices = geometry.vertices;
  1882. faces = geometry.faces;
  1883. faceVertexUvs = geometry.faceVertexUvs;
  1884. objectMatrixWorldRotation = object.matrixRotationWorld.extractRotation( objectMatrixWorld );
  1885. for ( v = 0, vl = vertices.length; v < vl; v ++ ) {
  1886. _vertex = getNextVertexInPool();
  1887. _vertex.positionWorld.copy( vertices[ v ].position );
  1888. objectMatrixWorld.multiplyVector3( _vertex.positionWorld );
  1889. _vertex.positionScreen.copy( _vertex.positionWorld );
  1890. _projScreenMatrix.multiplyVector4( _vertex.positionScreen );
  1891. _vertex.positionScreen.x /= _vertex.positionScreen.w;
  1892. _vertex.positionScreen.y /= _vertex.positionScreen.w;
  1893. _vertex.visible = _vertex.positionScreen.z > near && _vertex.positionScreen.z < far;
  1894. }
  1895. for ( f = 0, fl = faces.length; f < fl; f ++ ) {
  1896. face = faces[ f ];
  1897. if ( face instanceof THREE.Face3 ) {
  1898. v1 = _vertexPool[ face.a ];
  1899. v2 = _vertexPool[ face.b ];
  1900. v3 = _vertexPool[ face.c ];
  1901. if ( v1.visible && v2.visible && v3.visible ) {
  1902. visible = ( ( v3.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  1903. ( v3.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) ) < 0;
  1904. if ( object.doubleSided || visible != object.flipSided ) {
  1905. _face = getNextFace3InPool();
  1906. _face.v1.copy( v1 );
  1907. _face.v2.copy( v2 );
  1908. _face.v3.copy( v3 );
  1909. } else {
  1910. continue;
  1911. }
  1912. } else {
  1913. continue;
  1914. }
  1915. } else if ( face instanceof THREE.Face4 ) {
  1916. v1 = _vertexPool[ face.a ];
  1917. v2 = _vertexPool[ face.b ];
  1918. v3 = _vertexPool[ face.c ];
  1919. v4 = _vertexPool[ face.d ];
  1920. if ( v1.visible && v2.visible && v3.visible && v4.visible ) {
  1921. visible = ( v4.positionScreen.x - v1.positionScreen.x ) * ( v2.positionScreen.y - v1.positionScreen.y ) -
  1922. ( v4.positionScreen.y - v1.positionScreen.y ) * ( v2.positionScreen.x - v1.positionScreen.x ) < 0 ||
  1923. ( v2.positionScreen.x - v3.positionScreen.x ) * ( v4.positionScreen.y - v3.positionScreen.y ) -
  1924. ( v2.positionScreen.y - v3.positionScreen.y ) * ( v4.positionScreen.x - v3.positionScreen.x ) < 0;
  1925. if ( object.doubleSided || visible != object.flipSided ) {
  1926. _face = getNextFace4InPool();
  1927. _face.v1.copy( v1 );
  1928. _face.v2.copy( v2 );
  1929. _face.v3.copy( v3 );
  1930. _face.v4.copy( v4 );
  1931. } else {
  1932. continue;
  1933. }
  1934. } else {
  1935. continue;
  1936. }
  1937. }
  1938. _face.normalWorld.copy( face.normal );
  1939. if ( !visible && ( object.flipSided || object.doubleSided ) ) _face.normalWorld.negate();
  1940. objectMatrixWorldRotation.multiplyVector3( _face.normalWorld );
  1941. _face.centroidWorld.copy( face.centroid );
  1942. objectMatrixWorld.multiplyVector3( _face.centroidWorld );
  1943. _face.centroidScreen.copy( _face.centroidWorld );
  1944. _projScreenMatrix.multiplyVector3( _face.centroidScreen );
  1945. faceVertexNormals = face.vertexNormals;
  1946. for ( n = 0, nl = faceVertexNormals.length; n < nl; n ++ ) {
  1947. normal = _face.vertexNormalsWorld[ n ];
  1948. normal.copy( faceVertexNormals[ n ] );
  1949. if ( !visible && ( object.flipSided || object.doubleSided ) ) normal.negate();
  1950. objectMatrixWorldRotation.multiplyVector3( normal );
  1951. }
  1952. for ( c = 0, cl = faceVertexUvs.length; c < cl; c ++ ) {
  1953. uvs = faceVertexUvs[ c ][ f ];
  1954. if ( !uvs ) continue;
  1955. for ( u = 0, ul = uvs.length; u < ul; u ++ ) {
  1956. _face.uvs[ c ][ u ] = uvs[ u ];
  1957. }
  1958. }
  1959. _face.material = object.material;
  1960. _face.faceMaterial = face.materialIndex !== null ? geometryMaterials[ face.materialIndex ] : null;
  1961. _face.z = _face.centroidScreen.z;
  1962. _renderData.elements.push( _face );
  1963. }
  1964. } else if ( object instanceof THREE.Line ) {
  1965. _projScreenobjectMatrixWorld.multiply( _projScreenMatrix, objectMatrixWorld );
  1966. vertices = object.geometry.vertices;
  1967. v1 = getNextVertexInPool();
  1968. v1.positionScreen.copy( vertices[ 0 ].position );
  1969. _projScreenobjectMatrixWorld.multiplyVector4( v1.positionScreen );
  1970. // Handle LineStrip and LinePieces
  1971. var step = object.type === THREE.LinePieces ? 2 : 1;
  1972. for ( v = 1, vl = vertices.length; v < vl; v ++ ) {
  1973. v1 = getNextVertexInPool();
  1974. v1.positionScreen.copy( vertices[ v ].position );
  1975. _projScreenobjectMatrixWorld.multiplyVector4( v1.positionScreen );
  1976. if ( ( v + 1 ) % step > 0 ) continue;
  1977. v2 = _vertexPool[ _vertexCount - 2 ];
  1978. _clippedVertex1PositionScreen.copy( v1.positionScreen );
  1979. _clippedVertex2PositionScreen.copy( v2.positionScreen );
  1980. if ( clipLine( _clippedVertex1PositionScreen, _clippedVertex2PositionScreen ) ) {
  1981. // Perform the perspective divide
  1982. _clippedVertex1PositionScreen.multiplyScalar( 1 / _clippedVertex1PositionScreen.w );
  1983. _clippedVertex2PositionScreen.multiplyScalar( 1 / _clippedVertex2PositionScreen.w );
  1984. _line = getNextLineInPool();
  1985. _line.v1.positionScreen.copy( _clippedVertex1PositionScreen );
  1986. _line.v2.positionScreen.copy( _clippedVertex2PositionScreen );
  1987. _line.z = Math.max( _clippedVertex1PositionScreen.z, _clippedVertex2PositionScreen.z );
  1988. _line.material = object.material;
  1989. _renderData.elements.push( _line );
  1990. }
  1991. }
  1992. }
  1993. }
  1994. for ( o = 0, ol = _renderData.sprites.length; o < ol; o++ ) {
  1995. object = _renderData.sprites[ o ].object;
  1996. objectMatrixWorld = object.matrixWorld;
  1997. if ( object instanceof THREE.Particle ) {
  1998. _vector4.set( objectMatrixWorld.elements[12], objectMatrixWorld.elements[13], objectMatrixWorld.elements[14], 1 );
  1999. _projScreenMatrix.multiplyVector4( _vector4 );
  2000. _vector4.z /= _vector4.w;
  2001. if ( _vector4.z > 0 && _vector4.z < 1 ) {
  2002. _particle = getNextParticleInPool();
  2003. _particle.x = _vector4.x / _vector4.w;
  2004. _particle.y = _vector4.y / _vector4.w;
  2005. _particle.z = _vector4.z;
  2006. _particle.rotation = object.rotation.z;
  2007. _particle.scale.x = object.scale.x * Math.abs( _particle.x - ( _vector4.x + camera.projectionMatrix.elements[0] ) / ( _vector4.w + camera.projectionMatrix.elements[12] ) );
  2008. _particle.scale.y = object.scale.y * Math.abs( _particle.y - ( _vector4.y + camera.projectionMatrix.elements[5] ) / ( _vector4.w + camera.projectionMatrix.elements[13] ) );
  2009. _particle.material = object.material;
  2010. _renderData.elements.push( _particle );
  2011. }
  2012. }
  2013. }
  2014. sort && _renderData.elements.sort( painterSort );
  2015. return _renderData;
  2016. };
  2017. // Pools
  2018. function getNextObjectInPool() {
  2019. var object = _objectPool[ _objectCount ] = _objectPool[ _objectCount ] || new THREE.RenderableObject();
  2020. _objectCount ++;
  2021. return object;
  2022. }
  2023. function getNextVertexInPool() {
  2024. var vertex = _vertexPool[ _vertexCount ] = _vertexPool[ _vertexCount ] || new THREE.RenderableVertex();
  2025. _vertexCount ++;
  2026. return vertex;
  2027. }
  2028. function getNextFace3InPool() {
  2029. var face = _face3Pool[ _face3Count ] = _face3Pool[ _face3Count ] || new THREE.RenderableFace3();
  2030. _face3Count ++;
  2031. return face;
  2032. }
  2033. function getNextFace4InPool() {
  2034. var face = _face4Pool[ _face4Count ] = _face4Pool[ _face4Count ] || new THREE.RenderableFace4();
  2035. _face4Count ++;
  2036. return face;
  2037. }
  2038. function getNextLineInPool() {
  2039. var line = _linePool[ _lineCount ] = _linePool[ _lineCount ] || new THREE.RenderableLine();
  2040. _lineCount ++;
  2041. return line;
  2042. }
  2043. function getNextParticleInPool() {
  2044. var particle = _particlePool[ _particleCount ] = _particlePool[ _particleCount ] || new THREE.RenderableParticle();
  2045. _particleCount ++;
  2046. return particle;
  2047. }
  2048. //
  2049. function painterSort( a, b ) {
  2050. return b.z - a.z;
  2051. }
  2052. function clipLine( s1, s2 ) {
  2053. var alpha1 = 0, alpha2 = 1,
  2054. // Calculate the boundary coordinate of each vertex for the near and far clip planes,
  2055. // Z = -1 and Z = +1, respectively.
  2056. bc1near = s1.z + s1.w,
  2057. bc2near = s2.z + s2.w,
  2058. bc1far = - s1.z + s1.w,
  2059. bc2far = - s2.z + s2.w;
  2060. if ( bc1near >= 0 && bc2near >= 0 && bc1far >= 0 && bc2far >= 0 ) {
  2061. // Both vertices lie entirely within all clip planes.
  2062. return true;
  2063. } else if ( ( bc1near < 0 && bc2near < 0) || (bc1far < 0 && bc2far < 0 ) ) {
  2064. // Both vertices lie entirely outside one of the clip planes.
  2065. return false;
  2066. } else {
  2067. // The line segment spans at least one clip plane.
  2068. if ( bc1near < 0 ) {
  2069. // v1 lies outside the near plane, v2 inside
  2070. alpha1 = Math.max( alpha1, bc1near / ( bc1near - bc2near ) );
  2071. } else if ( bc2near < 0 ) {
  2072. // v2 lies outside the near plane, v1 inside
  2073. alpha2 = Math.min( alpha2, bc1near / ( bc1near - bc2near ) );
  2074. }
  2075. if ( bc1far < 0 ) {
  2076. // v1 lies outside the far plane, v2 inside
  2077. alpha1 = Math.max( alpha1, bc1far / ( bc1far - bc2far ) );
  2078. } else if ( bc2far < 0 ) {
  2079. // v2 lies outside the far plane, v2 inside
  2080. alpha2 = Math.min( alpha2, bc1far / ( bc1far - bc2far ) );
  2081. }
  2082. if ( alpha2 < alpha1 ) {
  2083. // The line segment spans two boundaries, but is outside both of them.
  2084. // (This can't happen when we're only clipping against just near/far but good
  2085. // to leave the check here for future usage if other clip planes are added.)
  2086. return false;
  2087. } else {
  2088. // Update the s1 and s2 vertices to match the clipped line segment.
  2089. s1.lerpSelf( s2, alpha1 );
  2090. s2.lerpSelf( s1, 1 - alpha2 );
  2091. return true;
  2092. }
  2093. }
  2094. }
  2095. };
  2096. /**
  2097. * @author mikael emtinger / http://gomo.se/
  2098. * @author alteredq / http://alteredqualia.com/
  2099. */
  2100. THREE.Quaternion = function( x, y, z, w ) {
  2101. this.x = x || 0;
  2102. this.y = y || 0;
  2103. this.z = z || 0;
  2104. this.w = ( w !== undefined ) ? w : 1;
  2105. };
  2106. THREE.Quaternion.prototype = {
  2107. constructor: THREE.Quaternion,
  2108. set: function ( x, y, z, w ) {
  2109. this.x = x;
  2110. this.y = y;
  2111. this.z = z;
  2112. this.w = w;
  2113. return this;
  2114. },
  2115. copy: function ( q ) {
  2116. this.x = q.x;
  2117. this.y = q.y;
  2118. this.z = q.z;
  2119. this.w = q.w;
  2120. return this;
  2121. },
  2122. setFromEuler: function ( vector ) {
  2123. var c = Math.PI / 360, // 0.5 * Math.PI / 360, // 0.5 is an optimization
  2124. x = vector.x * c,
  2125. y = vector.y * c,
  2126. z = vector.z * c,
  2127. c1 = Math.cos( y ),
  2128. s1 = Math.sin( y ),
  2129. c2 = Math.cos( -z ),
  2130. s2 = Math.sin( -z ),
  2131. c3 = Math.cos( x ),
  2132. s3 = Math.sin( x ),
  2133. c1c2 = c1 * c2,
  2134. s1s2 = s1 * s2;
  2135. this.w = c1c2 * c3 - s1s2 * s3;
  2136. this.x = c1c2 * s3 + s1s2 * c3;
  2137. this.y = s1 * c2 * c3 + c1 * s2 * s3;
  2138. this.z = c1 * s2 * c3 - s1 * c2 * s3;
  2139. return this;
  2140. },
  2141. setFromAxisAngle: function ( axis, angle ) {
  2142. // from http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  2143. // axis have to be normalized
  2144. var halfAngle = angle / 2,
  2145. s = Math.sin( halfAngle );
  2146. this.x = axis.x * s;
  2147. this.y = axis.y * s;
  2148. this.z = axis.z * s;
  2149. this.w = Math.cos( halfAngle );
  2150. return this;
  2151. },
  2152. setFromRotationMatrix: function ( m ) {
  2153. // Adapted from: http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  2154. function copySign( a, b ) {
  2155. return b < 0 ? -Math.abs( a ) : Math.abs( a );
  2156. }
  2157. var absQ = Math.pow( m.determinant(), 1.0 / 3.0 );
  2158. this.w = Math.sqrt( Math.max( 0, absQ + m.elements[0] + m.elements[5] + m.elements[10] ) ) / 2;
  2159. this.x = Math.sqrt( Math.max( 0, absQ + m.elements[0] - m.elements[5] - m.elements[10] ) ) / 2;
  2160. this.y = Math.sqrt( Math.max( 0, absQ - m.elements[0] + m.elements[5] - m.elements[10] ) ) / 2;
  2161. this.z = Math.sqrt( Math.max( 0, absQ - m.elements[0] - m.elements[5] + m.elements[10] ) ) / 2;
  2162. this.x = copySign( this.x, ( m.elements[6] - m.elements[9] ) );
  2163. this.y = copySign( this.y, ( m.elements[8] - m.elements[2] ) );
  2164. this.z = copySign( this.z, ( m.elements[1] - m.elements[4] ) );
  2165. this.normalize();
  2166. return this;
  2167. },
  2168. calculateW : function () {
  2169. this.w = - Math.sqrt( Math.abs( 1.0 - this.x * this.x - this.y * this.y - this.z * this.z ) );
  2170. return this;
  2171. },
  2172. inverse: function () {
  2173. this.x *= -1;
  2174. this.y *= -1;
  2175. this.z *= -1;
  2176. return this;
  2177. },
  2178. length: function () {
  2179. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  2180. },
  2181. normalize: function () {
  2182. var l = Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  2183. if ( l === 0 ) {
  2184. this.x = 0;
  2185. this.y = 0;
  2186. this.z = 0;
  2187. this.w = 0;
  2188. } else {
  2189. l = 1 / l;
  2190. this.x = this.x * l;
  2191. this.y = this.y * l;
  2192. this.z = this.z * l;
  2193. this.w = this.w * l;
  2194. }
  2195. return this;
  2196. },
  2197. multiply: function ( a, b ) {
  2198. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  2199. this.x = a.x * b.w + a.y * b.z - a.z * b.y + a.w * b.x;
  2200. this.y = -a.x * b.z + a.y * b.w + a.z * b.x + a.w * b.y;
  2201. this.z = a.x * b.y - a.y * b.x + a.z * b.w + a.w * b.z;
  2202. this.w = -a.x * b.x - a.y * b.y - a.z * b.z + a.w * b.w;
  2203. return this;
  2204. },
  2205. multiplySelf: function ( b ) {
  2206. var qax = this.x, qay = this.y, qaz = this.z, qaw = this.w,
  2207. qbx = b.x, qby = b.y, qbz = b.z, qbw = b.w;
  2208. this.x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  2209. this.y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  2210. this.z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  2211. this.w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  2212. return this;
  2213. },
  2214. multiplyVector3: function ( vector, dest ) {
  2215. if ( !dest ) { dest = vector; }
  2216. var x = vector.x, y = vector.y, z = vector.z,
  2217. qx = this.x, qy = this.y, qz = this.z, qw = this.w;
  2218. // calculate quat * vector
  2219. var ix = qw * x + qy * z - qz * y,
  2220. iy = qw * y + qz * x - qx * z,
  2221. iz = qw * z + qx * y - qy * x,
  2222. iw = -qx * x - qy * y - qz * z;
  2223. // calculate result * inverse quat
  2224. dest.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
  2225. dest.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
  2226. dest.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
  2227. return dest;
  2228. },
  2229. clone: function () {
  2230. return new THREE.Quaternion( this.x, this.y, this.z, this.w );
  2231. }
  2232. }
  2233. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  2234. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  2235. var cosHalfTheta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z;
  2236. if (cosHalfTheta < 0) {
  2237. qm.w = -qb.w; qm.x = -qb.x; qm.y = -qb.y; qm.z = -qb.z;
  2238. cosHalfTheta = -cosHalfTheta;
  2239. } else {
  2240. qm.copy(qb);
  2241. }
  2242. if ( Math.abs( cosHalfTheta ) >= 1.0 ) {
  2243. qm.w = qa.w; qm.x = qa.x; qm.y = qa.y; qm.z = qa.z;
  2244. return qm;
  2245. }
  2246. var halfTheta = Math.acos( cosHalfTheta ),
  2247. sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  2248. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  2249. qm.w = 0.5 * ( qa.w + qb.w );
  2250. qm.x = 0.5 * ( qa.x + qb.x );
  2251. qm.y = 0.5 * ( qa.y + qb.y );
  2252. qm.z = 0.5 * ( qa.z + qb.z );
  2253. return qm;
  2254. }
  2255. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  2256. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  2257. qm.w = ( qa.w * ratioA + qm.w * ratioB );
  2258. qm.x = ( qa.x * ratioA + qm.x * ratioB );
  2259. qm.y = ( qa.y * ratioA + qm.y * ratioB );
  2260. qm.z = ( qa.z * ratioA + qm.z * ratioB );
  2261. return qm;
  2262. }
  2263. /**
  2264. * @author mr.doob / http://mrdoob.com/
  2265. */
  2266. THREE.Vertex = function ( position ) {
  2267. this.position = position || new THREE.Vector3();
  2268. };
  2269. THREE.Vertex.prototype = {
  2270. constructor: THREE.Vertex,
  2271. clone: function () {
  2272. return new THREE.Vertex( this.position.clone() );
  2273. }
  2274. };
  2275. /**
  2276. * @author mr.doob / http://mrdoob.com/
  2277. * @author alteredq / http://alteredqualia.com/
  2278. */
  2279. THREE.Face3 = function ( a, b, c, normal, color, materialIndex ) {
  2280. this.a = a;
  2281. this.b = b;
  2282. this.c = c;
  2283. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  2284. this.vertexNormals = normal instanceof Array ? normal : [ ];
  2285. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  2286. this.vertexColors = color instanceof Array ? color : [];
  2287. this.vertexTangents = [];
  2288. this.materialIndex = materialIndex;
  2289. this.centroid = new THREE.Vector3();
  2290. };
  2291. THREE.Face3.prototype = {
  2292. constructor: THREE.Face3,
  2293. clone: function () {
  2294. var face = new THREE.Face3( this.a, this.b, this.c );
  2295. face.normal.copy( this.normal );
  2296. face.color.copy( this.color );
  2297. face.centroid.copy( this.centroid );
  2298. face.materialIndex = this.materialIndex;
  2299. var i, il;
  2300. for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  2301. for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  2302. for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  2303. return face;
  2304. }
  2305. };
  2306. /**
  2307. * @author mr.doob / http://mrdoob.com/
  2308. * @author alteredq / http://alteredqualia.com/
  2309. */
  2310. THREE.Face4 = function ( a, b, c, d, normal, color, materialIndex ) {
  2311. this.a = a;
  2312. this.b = b;
  2313. this.c = c;
  2314. this.d = d;
  2315. this.normal = normal instanceof THREE.Vector3 ? normal : new THREE.Vector3();
  2316. this.vertexNormals = normal instanceof Array ? normal : [ ];
  2317. this.color = color instanceof THREE.Color ? color : new THREE.Color();
  2318. this.vertexColors = color instanceof Array ? color : [];
  2319. this.vertexTangents = [];
  2320. this.materialIndex = materialIndex;
  2321. this.centroid = new THREE.Vector3();
  2322. };
  2323. THREE.Face4.prototype = {
  2324. constructor: THREE.Face4,
  2325. clone: function () {
  2326. var face = new THREE.Face4( this.a, this.b, this.c, this.d );
  2327. face.normal.copy( this.normal );
  2328. face.color.copy( this.color );
  2329. face.centroid.copy( this.centroid );
  2330. face.materialIndex = this.materialIndex;
  2331. var i, il;
  2332. for ( i = 0, il = this.vertexNormals.length; i < il; i ++ ) face.vertexNormals[ i ] = this.vertexNormals[ i ].clone();
  2333. for ( i = 0, il = this.vertexColors.length; i < il; i ++ ) face.vertexColors[ i ] = this.vertexColors[ i ].clone();
  2334. for ( i = 0, il = this.vertexTangents.length; i < il; i ++ ) face.vertexTangents[ i ] = this.vertexTangents[ i ].clone();
  2335. return face;
  2336. }
  2337. };
  2338. /**
  2339. * @author mr.doob / http://mrdoob.com/
  2340. */
  2341. THREE.UV = function ( u, v ) {
  2342. this.u = u || 0;
  2343. this.v = v || 0;
  2344. };
  2345. THREE.UV.prototype = {
  2346. constructor: THREE.UV,
  2347. set: function ( u, v ) {
  2348. this.u = u;
  2349. this.v = v;
  2350. return this;
  2351. },
  2352. copy: function ( uv ) {
  2353. this.u = uv.u;
  2354. this.v = uv.v;
  2355. return this;
  2356. },
  2357. lerpSelf: function ( uv, alpha ) {
  2358. this.u += ( uv.u - this.u ) * alpha;
  2359. this.v += ( uv.v - this.v ) * alpha;
  2360. return this;
  2361. },
  2362. clone: function () {
  2363. return new THREE.UV( this.u, this.v );
  2364. }
  2365. };
  2366. /**
  2367. * @author mr.doob / http://mrdoob.com/
  2368. * @author kile / http://kile.stravaganza.org/
  2369. * @author alteredq / http://alteredqualia.com/
  2370. * @author mikael emtinger / http://gomo.se/
  2371. * @author zz85 / http://www.lab4games.net/zz85/blog
  2372. */
  2373. THREE.Geometry = function () {
  2374. this.id = THREE.GeometryCount ++;
  2375. this.vertices = [];
  2376. this.colors = []; // one-to-one vertex colors, used in ParticleSystem, Line and Ribbon
  2377. this.materials = [];
  2378. this.faces = [];
  2379. this.faceUvs = [[]];
  2380. this.faceVertexUvs = [[]];
  2381. this.morphTargets = [];
  2382. this.morphColors = [];
  2383. this.morphNormals = [];
  2384. this.skinWeights = [];
  2385. this.skinIndices = [];
  2386. this.boundingBox = null;
  2387. this.boundingSphere = null;
  2388. this.hasTangents = false;
  2389. this.dynamic = false; // unless set to true the *Arrays will be deleted once sent to a buffer.
  2390. };
  2391. THREE.Geometry.prototype = {
  2392. constructor : THREE.Geometry,
  2393. applyMatrix: function ( matrix ) {
  2394. var matrixRotation = new THREE.Matrix4();
  2395. matrixRotation.extractRotation( matrix );
  2396. for ( var i = 0, il = this.vertices.length; i < il; i ++ ) {
  2397. var vertex = this.vertices[ i ];
  2398. matrix.multiplyVector3( vertex.position );
  2399. }
  2400. for ( var i = 0, il = this.faces.length; i < il; i ++ ) {
  2401. var face = this.faces[ i ];
  2402. matrixRotation.multiplyVector3( face.normal );
  2403. for ( var j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) {
  2404. matrixRotation.multiplyVector3( face.vertexNormals[ j ] );
  2405. }
  2406. matrix.multiplyVector3( face.centroid );
  2407. }
  2408. },
  2409. computeCentroids: function () {
  2410. var f, fl, face;
  2411. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2412. face = this.faces[ f ];
  2413. face.centroid.set( 0, 0, 0 );
  2414. if ( face instanceof THREE.Face3 ) {
  2415. face.centroid.addSelf( this.vertices[ face.a ].position );
  2416. face.centroid.addSelf( this.vertices[ face.b ].position );
  2417. face.centroid.addSelf( this.vertices[ face.c ].position );
  2418. face.centroid.divideScalar( 3 );
  2419. } else if ( face instanceof THREE.Face4 ) {
  2420. face.centroid.addSelf( this.vertices[ face.a ].position );
  2421. face.centroid.addSelf( this.vertices[ face.b ].position );
  2422. face.centroid.addSelf( this.vertices[ face.c ].position );
  2423. face.centroid.addSelf( this.vertices[ face.d ].position );
  2424. face.centroid.divideScalar( 4 );
  2425. }
  2426. }
  2427. },
  2428. computeFaceNormals: function () {
  2429. var n, nl, v, vl, vertex, f, fl, face, vA, vB, vC,
  2430. cb = new THREE.Vector3(), ab = new THREE.Vector3();
  2431. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2432. face = this.faces[ f ];
  2433. vA = this.vertices[ face.a ];
  2434. vB = this.vertices[ face.b ];
  2435. vC = this.vertices[ face.c ];
  2436. cb.sub( vC.position, vB.position );
  2437. ab.sub( vA.position, vB.position );
  2438. cb.crossSelf( ab );
  2439. if ( !cb.isZero() ) {
  2440. cb.normalize();
  2441. }
  2442. face.normal.copy( cb );
  2443. }
  2444. },
  2445. computeVertexNormals: function () {
  2446. var v, vl, f, fl, face, vertices;
  2447. // create internal buffers for reuse when calling this method repeatedly
  2448. // (otherwise memory allocation / deallocation every frame is big resource hog)
  2449. if ( this.__tmpVertices === undefined ) {
  2450. this.__tmpVertices = new Array( this.vertices.length );
  2451. vertices = this.__tmpVertices;
  2452. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2453. vertices[ v ] = new THREE.Vector3();
  2454. }
  2455. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2456. face = this.faces[ f ];
  2457. if ( face instanceof THREE.Face3 ) {
  2458. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  2459. } else if ( face instanceof THREE.Face4 ) {
  2460. face.vertexNormals = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  2461. }
  2462. }
  2463. } else {
  2464. vertices = this.__tmpVertices;
  2465. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2466. vertices[ v ].set( 0, 0, 0 );
  2467. }
  2468. }
  2469. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2470. face = this.faces[ f ];
  2471. if ( face instanceof THREE.Face3 ) {
  2472. vertices[ face.a ].addSelf( face.normal );
  2473. vertices[ face.b ].addSelf( face.normal );
  2474. vertices[ face.c ].addSelf( face.normal );
  2475. } else if ( face instanceof THREE.Face4 ) {
  2476. vertices[ face.a ].addSelf( face.normal );
  2477. vertices[ face.b ].addSelf( face.normal );
  2478. vertices[ face.c ].addSelf( face.normal );
  2479. vertices[ face.d ].addSelf( face.normal );
  2480. }
  2481. }
  2482. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2483. vertices[ v ].normalize();
  2484. }
  2485. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2486. face = this.faces[ f ];
  2487. if ( face instanceof THREE.Face3 ) {
  2488. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  2489. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  2490. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  2491. } else if ( face instanceof THREE.Face4 ) {
  2492. face.vertexNormals[ 0 ].copy( vertices[ face.a ] );
  2493. face.vertexNormals[ 1 ].copy( vertices[ face.b ] );
  2494. face.vertexNormals[ 2 ].copy( vertices[ face.c ] );
  2495. face.vertexNormals[ 3 ].copy( vertices[ face.d ] );
  2496. }
  2497. }
  2498. },
  2499. computeMorphNormals: function () {
  2500. var i, il, f, fl, face;
  2501. // save original normals
  2502. // - create temp variables on first access
  2503. // otherwise just copy (for faster repeated calls)
  2504. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2505. face = this.faces[ f ];
  2506. if ( ! face.__originalFaceNormal ) {
  2507. face.__originalFaceNormal = face.normal.clone();
  2508. } else {
  2509. face.__originalFaceNormal.copy( face.normal );
  2510. }
  2511. if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = [];
  2512. for ( i = 0, il = face.vertexNormals.length; i < il; i ++ ) {
  2513. if ( ! face.__originalVertexNormals[ i ] ) {
  2514. face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone();
  2515. } else {
  2516. face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] );
  2517. }
  2518. }
  2519. }
  2520. // use temp geometry to compute face and vertex normals for each morph
  2521. var tmpGeo = new THREE.Geometry();
  2522. tmpGeo.faces = this.faces;
  2523. for ( i = 0, il = this.morphTargets.length; i < il; i ++ ) {
  2524. // create on first access
  2525. if ( ! this.morphNormals[ i ] ) {
  2526. this.morphNormals[ i ] = {};
  2527. this.morphNormals[ i ].faceNormals = [];
  2528. this.morphNormals[ i ].vertexNormals = [];
  2529. var dstNormalsFace = this.morphNormals[ i ].faceNormals;
  2530. var dstNormalsVertex = this.morphNormals[ i ].vertexNormals;
  2531. var faceNormal, vertexNormals;
  2532. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2533. face = this.faces[ f ];
  2534. faceNormal = new THREE.Vector3();
  2535. if ( face instanceof THREE.Face3 ) {
  2536. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3() };
  2537. } else {
  2538. vertexNormals = { a: new THREE.Vector3(), b: new THREE.Vector3(), c: new THREE.Vector3(), d: new THREE.Vector3() };
  2539. }
  2540. dstNormalsFace.push( faceNormal );
  2541. dstNormalsVertex.push( vertexNormals );
  2542. }
  2543. }
  2544. var morphNormals = this.morphNormals[ i ];
  2545. // set vertices to morph target
  2546. tmpGeo.vertices = this.morphTargets[ i ].vertices;
  2547. // compute morph normals
  2548. tmpGeo.computeFaceNormals();
  2549. tmpGeo.computeVertexNormals();
  2550. // store morph normals
  2551. var faceNormal, vertexNormals;
  2552. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2553. face = this.faces[ f ];
  2554. faceNormal = morphNormals.faceNormals[ f ];
  2555. vertexNormals = morphNormals.vertexNormals[ f ];
  2556. faceNormal.copy( face.normal );
  2557. if ( face instanceof THREE.Face3 ) {
  2558. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  2559. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  2560. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  2561. } else {
  2562. vertexNormals.a.copy( face.vertexNormals[ 0 ] );
  2563. vertexNormals.b.copy( face.vertexNormals[ 1 ] );
  2564. vertexNormals.c.copy( face.vertexNormals[ 2 ] );
  2565. vertexNormals.d.copy( face.vertexNormals[ 3 ] );
  2566. }
  2567. }
  2568. }
  2569. // restore original normals
  2570. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2571. face = this.faces[ f ];
  2572. face.normal = face.__originalFaceNormal;
  2573. face.vertexNormals = face.__originalVertexNormals;
  2574. }
  2575. },
  2576. computeTangents: function () {
  2577. // based on http://www.terathon.com/code/tangent.html
  2578. // tangents go to vertices
  2579. var f, fl, v, vl, i, il, vertexIndex,
  2580. face, uv, vA, vB, vC, uvA, uvB, uvC,
  2581. x1, x2, y1, y2, z1, z2,
  2582. s1, s2, t1, t2, r, t, test,
  2583. tan1 = [], tan2 = [],
  2584. sdir = new THREE.Vector3(), tdir = new THREE.Vector3(),
  2585. tmp = new THREE.Vector3(), tmp2 = new THREE.Vector3(),
  2586. n = new THREE.Vector3(), w;
  2587. for ( v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2588. tan1[ v ] = new THREE.Vector3();
  2589. tan2[ v ] = new THREE.Vector3();
  2590. }
  2591. function handleTriangle( context, a, b, c, ua, ub, uc ) {
  2592. vA = context.vertices[ a ].position;
  2593. vB = context.vertices[ b ].position;
  2594. vC = context.vertices[ c ].position;
  2595. uvA = uv[ ua ];
  2596. uvB = uv[ ub ];
  2597. uvC = uv[ uc ];
  2598. x1 = vB.x - vA.x;
  2599. x2 = vC.x - vA.x;
  2600. y1 = vB.y - vA.y;
  2601. y2 = vC.y - vA.y;
  2602. z1 = vB.z - vA.z;
  2603. z2 = vC.z - vA.z;
  2604. s1 = uvB.u - uvA.u;
  2605. s2 = uvC.u - uvA.u;
  2606. t1 = uvB.v - uvA.v;
  2607. t2 = uvC.v - uvA.v;
  2608. r = 1.0 / ( s1 * t2 - s2 * t1 );
  2609. sdir.set( ( t2 * x1 - t1 * x2 ) * r,
  2610. ( t2 * y1 - t1 * y2 ) * r,
  2611. ( t2 * z1 - t1 * z2 ) * r );
  2612. tdir.set( ( s1 * x2 - s2 * x1 ) * r,
  2613. ( s1 * y2 - s2 * y1 ) * r,
  2614. ( s1 * z2 - s2 * z1 ) * r );
  2615. tan1[ a ].addSelf( sdir );
  2616. tan1[ b ].addSelf( sdir );
  2617. tan1[ c ].addSelf( sdir );
  2618. tan2[ a ].addSelf( tdir );
  2619. tan2[ b ].addSelf( tdir );
  2620. tan2[ c ].addSelf( tdir );
  2621. }
  2622. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2623. face = this.faces[ f ];
  2624. uv = this.faceVertexUvs[ 0 ][ f ]; // use UV layer 0 for tangents
  2625. if ( face instanceof THREE.Face3 ) {
  2626. handleTriangle( this, face.a, face.b, face.c, 0, 1, 2 );
  2627. } else if ( face instanceof THREE.Face4 ) {
  2628. handleTriangle( this, face.a, face.b, face.d, 0, 1, 3 );
  2629. handleTriangle( this, face.b, face.c, face.d, 1, 2, 3 );
  2630. }
  2631. }
  2632. var faceIndex = [ 'a', 'b', 'c', 'd' ];
  2633. for ( f = 0, fl = this.faces.length; f < fl; f ++ ) {
  2634. face = this.faces[ f ];
  2635. for ( i = 0; i < face.vertexNormals.length; i++ ) {
  2636. n.copy( face.vertexNormals[ i ] );
  2637. vertexIndex = face[ faceIndex[ i ] ];
  2638. t = tan1[ vertexIndex ];
  2639. // Gram-Schmidt orthogonalize
  2640. tmp.copy( t );
  2641. tmp.subSelf( n.multiplyScalar( n.dot( t ) ) ).normalize();
  2642. // Calculate handedness
  2643. tmp2.cross( face.vertexNormals[ i ], t );
  2644. test = tmp2.dot( tan2[ vertexIndex ] );
  2645. w = (test < 0.0) ? -1.0 : 1.0;
  2646. face.vertexTangents[ i ] = new THREE.Vector4( tmp.x, tmp.y, tmp.z, w );
  2647. }
  2648. }
  2649. this.hasTangents = true;
  2650. },
  2651. computeBoundingBox: function () {
  2652. if ( ! this.boundingBox ) {
  2653. this.boundingBox = { min: new THREE.Vector3(), max: new THREE.Vector3() };
  2654. }
  2655. if ( this.vertices.length > 0 ) {
  2656. var position, firstPosition = this.vertices[ 0 ].position;
  2657. this.boundingBox.min.copy( firstPosition );
  2658. this.boundingBox.max.copy( firstPosition );
  2659. var min = this.boundingBox.min,
  2660. max = this.boundingBox.max;
  2661. for ( var v = 1, vl = this.vertices.length; v < vl; v ++ ) {
  2662. position = this.vertices[ v ].position;
  2663. if ( position.x < min.x ) {
  2664. min.x = position.x;
  2665. } else if ( position.x > max.x ) {
  2666. max.x = position.x;
  2667. }
  2668. if ( position.y < min.y ) {
  2669. min.y = position.y;
  2670. } else if ( position.y > max.y ) {
  2671. max.y = position.y;
  2672. }
  2673. if ( position.z < min.z ) {
  2674. min.z = position.z;
  2675. } else if ( position.z > max.z ) {
  2676. max.z = position.z;
  2677. }
  2678. }
  2679. } else {
  2680. this.boundingBox.min.set( 0, 0, 0 );
  2681. this.boundingBox.max.set( 0, 0, 0 );
  2682. }
  2683. },
  2684. computeBoundingSphere: function () {
  2685. if ( ! this.boundingSphere ) this.boundingSphere = { radius: 0 };
  2686. var radius, maxRadius = 0;
  2687. for ( var v = 0, vl = this.vertices.length; v < vl; v ++ ) {
  2688. radius = this.vertices[ v ].position.length();
  2689. if ( radius > maxRadius ) maxRadius = radius;
  2690. }
  2691. this.boundingSphere.radius = maxRadius;
  2692. },
  2693. /*
  2694. * Checks for duplicate vertices with hashmap.
  2695. * Duplicated vertices are removed
  2696. * and faces' vertices are updated.
  2697. */
  2698. mergeVertices: function() {
  2699. var verticesMap = {}; // Hashmap for looking up vertice by position coordinates (and making sure they are unique)
  2700. var unique = [], changes = [];
  2701. var v, key;
  2702. var precisionPoints = 4; // number of decimal points, eg. 4 for epsilon of 0.0001
  2703. var precision = Math.pow( 10, precisionPoints );
  2704. var i,il, face;
  2705. for ( i = 0, il = this.vertices.length; i < il; i ++ ) {
  2706. v = this.vertices[ i ].position;
  2707. key = [ Math.round( v.x * precision ), Math.round( v.y * precision ), Math.round( v.z * precision ) ].join( '_' );
  2708. if ( verticesMap[ key ] === undefined ) {
  2709. verticesMap[ key ] = i;
  2710. unique.push( this.vertices[ i ] );
  2711. changes[ i ] = unique.length - 1;
  2712. } else {
  2713. //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]);
  2714. changes[ i ] = changes[ verticesMap[ key ] ];
  2715. }
  2716. };
  2717. // Start to patch face indices
  2718. for( i = 0, il = this.faces.length; i < il; i ++ ) {
  2719. face = this.faces[ i ];
  2720. if ( face instanceof THREE.Face3 ) {
  2721. face.a = changes[ face.a ];
  2722. face.b = changes[ face.b ];
  2723. face.c = changes[ face.c ];
  2724. } else if ( face instanceof THREE.Face4 ) {
  2725. face.a = changes[ face.a ];
  2726. face.b = changes[ face.b ];
  2727. face.c = changes[ face.c ];
  2728. face.d = changes[ face.d ];
  2729. }
  2730. }
  2731. // Use unique set of vertices
  2732. this.vertices = unique;
  2733. }
  2734. };
  2735. THREE.GeometryCount = 0;
  2736. /**
  2737. * @author mr.doob / http://mrdoob.com/
  2738. * @author mikael emtinger / http://gomo.se/
  2739. */
  2740. THREE.Camera = function () {
  2741. THREE.Object3D.call( this );
  2742. this.matrixWorldInverse = new THREE.Matrix4();
  2743. this.projectionMatrix = new THREE.Matrix4();
  2744. this.projectionMatrixInverse = new THREE.Matrix4();
  2745. };
  2746. THREE.Camera.prototype = new THREE.Object3D();
  2747. THREE.Camera.prototype.constructor = THREE.Camera;
  2748. THREE.Camera.prototype.lookAt = function ( vector ) {
  2749. // TODO: Add hierarchy support.
  2750. this.matrix.lookAt( this.position, vector, this.up );
  2751. if ( this.rotationAutoUpdate ) {
  2752. this.rotation.getRotationFromMatrix( this.matrix );
  2753. }
  2754. };
  2755. /**
  2756. * @author alteredq / http://alteredqualia.com/
  2757. */
  2758. THREE.OrthographicCamera = function ( left, right, top, bottom, near, far ) {
  2759. THREE.Camera.call( this );
  2760. this.left = left;
  2761. this.right = right;
  2762. this.top = top;
  2763. this.bottom = bottom;
  2764. this.near = ( near !== undefined ) ? near : 0.1;
  2765. this.far = ( far !== undefined ) ? far : 2000;
  2766. this.updateProjectionMatrix();
  2767. };
  2768. THREE.OrthographicCamera.prototype = new THREE.Camera();
  2769. THREE.OrthographicCamera.prototype.constructor = THREE.OrthographicCamera;
  2770. THREE.OrthographicCamera.prototype.updateProjectionMatrix = function () {
  2771. this.projectionMatrix.makeOrthographic( this.left, this.right, this.top, this.bottom, this.near, this.far );
  2772. };
  2773. /**
  2774. * @author mr.doob / http://mrdoob.com/
  2775. * @author greggman / http://games.greggman.com/
  2776. * @author zz85 / http://www.lab4games.net/zz85/blog
  2777. */
  2778. THREE.PerspectiveCamera = function ( fov, aspect, near, far ) {
  2779. THREE.Camera.call( this );
  2780. this.fov = fov !== undefined ? fov : 50;
  2781. this.aspect = aspect !== undefined ? aspect : 1;
  2782. this.near = near !== undefined ? near : 0.1;
  2783. this.far = far !== undefined ? far : 2000;
  2784. this.updateProjectionMatrix();
  2785. };
  2786. THREE.PerspectiveCamera.prototype = new THREE.Camera();
  2787. THREE.PerspectiveCamera.prototype.constructor = THREE.PerspectiveCamera;
  2788. /**
  2789. * Uses Focal Length (in mm) to estimate and set FOV
  2790. * 35mm (fullframe) camera is used if frame size is not specified;
  2791. * Formula based on http://www.bobatkins.com/photography/technical/field_of_view.html
  2792. */
  2793. THREE.PerspectiveCamera.prototype.setLens = function ( focalLength, frameHeight ) {
  2794. frameHeight = frameHeight !== undefined ? frameHeight : 24;
  2795. this.fov = 2 * Math.atan( frameHeight / ( focalLength * 2 ) ) * ( 180 / Math.PI );
  2796. this.updateProjectionMatrix();
  2797. }
  2798. /**
  2799. * Sets an offset in a larger frustum. This is useful for multi-window or
  2800. * multi-monitor/multi-machine setups.
  2801. *
  2802. * For example, if you have 3x2 monitors and each monitor is 1920x1080 and
  2803. * the monitors are in grid like this
  2804. *
  2805. * +---+---+---+
  2806. * | A | B | C |
  2807. * +---+---+---+
  2808. * | D | E | F |
  2809. * +---+---+---+
  2810. *
  2811. * then for each monitor you would call it like this
  2812. *
  2813. * var w = 1920;
  2814. * var h = 1080;
  2815. * var fullWidth = w * 3;
  2816. * var fullHeight = h * 2;
  2817. *
  2818. * --A--
  2819. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 0, w, h );
  2820. * --B--
  2821. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 0, w, h );
  2822. * --C--
  2823. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 0, w, h );
  2824. * --D--
  2825. * camera.setOffset( fullWidth, fullHeight, w * 0, h * 1, w, h );
  2826. * --E--
  2827. * camera.setOffset( fullWidth, fullHeight, w * 1, h * 1, w, h );
  2828. * --F--
  2829. * camera.setOffset( fullWidth, fullHeight, w * 2, h * 1, w, h );
  2830. *
  2831. * Note there is no reason monitors have to be the same size or in a grid.
  2832. */
  2833. THREE.PerspectiveCamera.prototype.setViewOffset = function ( fullWidth, fullHeight, x, y, width, height ) {
  2834. this.fullWidth = fullWidth;
  2835. this.fullHeight = fullHeight;
  2836. this.x = x;
  2837. this.y = y;
  2838. this.width = width;
  2839. this.height = height;
  2840. this.updateProjectionMatrix();
  2841. };
  2842. THREE.PerspectiveCamera.prototype.updateProjectionMatrix = function () {
  2843. if ( this.fullWidth ) {
  2844. var aspect = this.fullWidth / this.fullHeight;
  2845. var top = Math.tan( this.fov * Math.PI / 360 ) * this.near;
  2846. var bottom = -top;
  2847. var left = aspect * bottom;
  2848. var right = aspect * top;
  2849. var width = Math.abs( right - left );
  2850. var height = Math.abs( top - bottom );
  2851. this.projectionMatrix.makeFrustum(
  2852. left + this.x * width / this.fullWidth,
  2853. left + ( this.x + this.width ) * width / this.fullWidth,
  2854. top - ( this.y + this.height ) * height / this.fullHeight,
  2855. top - this.y * height / this.fullHeight,
  2856. this.near,
  2857. this.far
  2858. );
  2859. } else {
  2860. this.projectionMatrix.makePerspective( this.fov, this.aspect, this.near, this.far );
  2861. }
  2862. };
  2863. /**
  2864. * @author mr.doob / http://mrdoob.com/
  2865. * @author alteredq / http://alteredqualia.com/
  2866. */
  2867. THREE.Light = function ( hex ) {
  2868. THREE.Object3D.call( this );
  2869. this.color = new THREE.Color( hex );
  2870. };
  2871. THREE.Light.prototype = new THREE.Object3D();
  2872. THREE.Light.prototype.constructor = THREE.Light;
  2873. THREE.Light.prototype.supr = THREE.Object3D.prototype;
  2874. /**
  2875. * @author mr.doob / http://mrdoob.com/
  2876. */
  2877. THREE.AmbientLight = function ( hex ) {
  2878. THREE.Light.call( this, hex );
  2879. };
  2880. THREE.AmbientLight.prototype = new THREE.Light();
  2881. THREE.AmbientLight.prototype.constructor = THREE.AmbientLight;
  2882. /**
  2883. * @author mr.doob / http://mrdoob.com/
  2884. * @author alteredq / http://alteredqualia.com/
  2885. */
  2886. THREE.DirectionalLight = function ( hex, intensity, distance ) {
  2887. THREE.Light.call( this, hex );
  2888. this.position = new THREE.Vector3( 0, 1, 0 );
  2889. this.target = new THREE.Object3D();
  2890. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  2891. this.distance = ( distance !== undefined ) ? distance : 0;
  2892. this.castShadow = false;
  2893. this.onlyShadow = false;
  2894. //
  2895. this.shadowCameraNear = 50;
  2896. this.shadowCameraFar = 5000;
  2897. this.shadowCameraLeft = -500;
  2898. this.shadowCameraRight = 500;
  2899. this.shadowCameraTop = 500;
  2900. this.shadowCameraBottom = -500;
  2901. this.shadowCameraVisible = false;
  2902. this.shadowBias = 0;
  2903. this.shadowDarkness = 0.5;
  2904. this.shadowMapWidth = 512;
  2905. this.shadowMapHeight = 512;
  2906. //
  2907. this.shadowCascade = false;
  2908. this.shadowCascadeOffset = new THREE.Vector3( 0, 0, -1000 );
  2909. this.shadowCascadeCount = 2;
  2910. this.shadowCascadeBias = [ 0, 0, 0 ];
  2911. this.shadowCascadeWidth = [ 512, 512, 512 ];
  2912. this.shadowCascadeHeight = [ 512, 512, 512 ];
  2913. this.shadowCascadeNearZ = [ -1.000, 0.990, 0.998 ];
  2914. this.shadowCascadeFarZ = [ 0.990, 0.998, 1.000 ];
  2915. this.shadowCascadeArray = [];
  2916. //
  2917. this.shadowMap = null;
  2918. this.shadowMapSize = null;
  2919. this.shadowCamera = null;
  2920. this.shadowMatrix = null;
  2921. };
  2922. THREE.DirectionalLight.prototype = new THREE.Light();
  2923. THREE.DirectionalLight.prototype.constructor = THREE.DirectionalLight;
  2924. /**
  2925. * @author mr.doob / http://mrdoob.com/
  2926. */
  2927. THREE.PointLight = function ( hex, intensity, distance ) {
  2928. THREE.Light.call( this, hex );
  2929. this.position = new THREE.Vector3( 0, 0, 0 );
  2930. this.intensity = ( intensity !== undefined ) ? intensity : 1;
  2931. this.distance = ( distance !== undefined ) ? distance : 0;
  2932. };
  2933. THREE.PointLight.prototype = new THREE.Light();
  2934. THREE.PointLight.prototype.constructor = THREE.PointLight;
  2935. /**
  2936. * @author mr.doob / http://mrdoob.com/
  2937. * @author alteredq / http://alteredqualia.com/
  2938. */
  2939. THREE.Material = function ( parameters ) {
  2940. parameters = parameters || {};
  2941. this.id = THREE.MaterialCount ++;
  2942. this.name = '';
  2943. this.opacity = parameters.opacity !== undefined ? parameters.opacity : 1;
  2944. this.transparent = parameters.transparent !== undefined ? parameters.transparent : false;
  2945. this.blending = parameters.blending !== undefined ? parameters.blending : THREE.NormalBlending;
  2946. this.blendSrc = parameters.blendSrc !== undefined ? parameters.blendSrc : THREE.SrcAlphaFactor;
  2947. this.blendDst = parameters.blendDst !== undefined ? parameters.blendDst : THREE.OneMinusSrcAlphaFactor;
  2948. this.blendEquation = parameters.blendEquation !== undefined ? parameters.blendEquation : THREE.AddEquation;
  2949. this.depthTest = parameters.depthTest !== undefined ? parameters.depthTest : true;
  2950. this.depthWrite = parameters.depthWrite !== undefined ? parameters.depthWrite : true;
  2951. this.polygonOffset = parameters.polygonOffset !== undefined ? parameters.polygonOffset : false;
  2952. this.polygonOffsetFactor = parameters.polygonOffsetFactor !== undefined ? parameters.polygonOffsetFactor : 0;
  2953. this.polygonOffsetUnits = parameters.polygonOffsetUnits !== undefined ? parameters.polygonOffsetUnits : 0;
  2954. this.alphaTest = parameters.alphaTest !== undefined ? parameters.alphaTest : 0;
  2955. this.overdraw = parameters.overdraw !== undefined ? parameters.overdraw : false; // Boolean for fixing antialiasing gaps in CanvasRenderer
  2956. this.needsUpdate = true;
  2957. }
  2958. THREE.MaterialCount = 0;
  2959. // shading
  2960. THREE.NoShading = 0;
  2961. THREE.FlatShading = 1;
  2962. THREE.SmoothShading = 2;
  2963. // colors
  2964. THREE.NoColors = 0;
  2965. THREE.FaceColors = 1;
  2966. THREE.VertexColors = 2;
  2967. // blending modes
  2968. THREE.NoBlending = 0;
  2969. THREE.NormalBlending = 1;
  2970. THREE.AdditiveBlending = 2;
  2971. THREE.SubtractiveBlending = 3;
  2972. THREE.MultiplyBlending = 4;
  2973. THREE.AdditiveAlphaBlending = 5;
  2974. THREE.CustomBlending = 6;
  2975. // custom blending equations
  2976. // (numbers start from 100 not to clash with other
  2977. // mappings to OpenGL constants defined in Texture.js)
  2978. THREE.AddEquation = 100;
  2979. THREE.SubtractEquation = 101;
  2980. THREE.ReverseSubtractEquation = 102;
  2981. // custom blending destination factors
  2982. THREE.ZeroFactor = 200;
  2983. THREE.OneFactor = 201;
  2984. THREE.SrcColorFactor = 202;
  2985. THREE.OneMinusSrcColorFactor = 203;
  2986. THREE.SrcAlphaFactor = 204;
  2987. THREE.OneMinusSrcAlphaFactor = 205;
  2988. THREE.DstAlphaFactor = 206;
  2989. THREE.OneMinusDstAlphaFactor = 207;
  2990. // custom blending source factors
  2991. //THREE.ZeroFactor = 200;
  2992. //THREE.OneFactor = 201;
  2993. //THREE.SrcAlphaFactor = 204;
  2994. //THREE.OneMinusSrcAlphaFactor = 205;
  2995. //THREE.DstAlphaFactor = 206;
  2996. //THREE.OneMinusDstAlphaFactor = 207;
  2997. THREE.DstColorFactor = 208;
  2998. THREE.OneMinusDstColorFactor = 209;
  2999. THREE.SrcAlphaSaturateFactor = 210;
  3000. /**
  3001. * @author mr.doob / http://mrdoob.com/
  3002. * @author alteredq / http://alteredqualia.com/
  3003. *
  3004. * parameters = {
  3005. * color: <hex>,
  3006. * opacity: <float>,
  3007. *
  3008. * blending: THREE.NormalBlending,
  3009. * depthTest: <bool>,
  3010. *
  3011. * linewidth: <float>,
  3012. * linecap: "round",
  3013. * linejoin: "round",
  3014. *
  3015. * vertexColors: <bool>
  3016. *
  3017. * fog: <bool>
  3018. * }
  3019. */
  3020. THREE.LineBasicMaterial = function ( parameters ) {
  3021. THREE.Material.call( this, parameters );
  3022. parameters = parameters || {};
  3023. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3024. this.linewidth = parameters.linewidth !== undefined ? parameters.linewidth : 1;
  3025. this.linecap = parameters.linecap !== undefined ? parameters.linecap : 'round';
  3026. this.linejoin = parameters.linejoin !== undefined ? parameters.linejoin : 'round';
  3027. this.vertexColors = parameters.vertexColors ? parameters.vertexColors : false;
  3028. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3029. };
  3030. THREE.LineBasicMaterial.prototype = new THREE.Material();
  3031. THREE.LineBasicMaterial.prototype.constructor = THREE.LineBasicMaterial;
  3032. /**
  3033. * @author mr.doob / http://mrdoob.com/
  3034. * @author alteredq / http://alteredqualia.com/
  3035. *
  3036. * parameters = {
  3037. * color: <hex>,
  3038. * opacity: <float>,
  3039. * map: new THREE.Texture( <Image> ),
  3040. *
  3041. * lightMap: new THREE.Texture( <Image> ),
  3042. *
  3043. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3044. * combine: THREE.Multiply,
  3045. * reflectivity: <float>,
  3046. * refractionRatio: <float>,
  3047. *
  3048. * shading: THREE.SmoothShading,
  3049. * blending: THREE.NormalBlending,
  3050. * depthTest: <bool>,
  3051. *
  3052. * wireframe: <boolean>,
  3053. * wireframeLinewidth: <float>,
  3054. *
  3055. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3056. *
  3057. * skinning: <bool>,
  3058. * morphTargets: <bool>,
  3059. *
  3060. * fog: <bool>
  3061. * }
  3062. */
  3063. THREE.MeshBasicMaterial = function ( parameters ) {
  3064. THREE.Material.call( this, parameters );
  3065. parameters = parameters || {};
  3066. // color property represents emissive for MeshBasicMaterial
  3067. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3068. this.map = parameters.map !== undefined ? parameters.map : null;
  3069. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3070. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3071. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3072. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3073. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3074. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3075. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3076. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3077. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3078. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3079. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3080. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3081. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3082. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3083. };
  3084. THREE.MeshBasicMaterial.prototype = new THREE.Material();
  3085. THREE.MeshBasicMaterial.prototype.constructor = THREE.MeshBasicMaterial;
  3086. /**
  3087. * @author mr.doob / http://mrdoob.com/
  3088. * @author alteredq / http://alteredqualia.com/
  3089. *
  3090. * parameters = {
  3091. * color: <hex>,
  3092. * ambient: <hex>,
  3093. * emissive: <hex>,
  3094. * opacity: <float>,
  3095. *
  3096. * map: new THREE.Texture( <Image> ),
  3097. *
  3098. * lightMap: new THREE.Texture( <Image> ),
  3099. *
  3100. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3101. * combine: THREE.Multiply,
  3102. * reflectivity: <float>,
  3103. * refractionRatio: <float>,
  3104. *
  3105. * shading: THREE.SmoothShading,
  3106. * blending: THREE.NormalBlending,
  3107. * depthTest: <bool>,
  3108. *
  3109. * wireframe: <boolean>,
  3110. * wireframeLinewidth: <float>,
  3111. *
  3112. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3113. *
  3114. * skinning: <bool>,
  3115. * morphTargets: <bool>,
  3116. * morphNormals: <bool>,
  3117. *
  3118. * fog: <bool>
  3119. * }
  3120. */
  3121. THREE.MeshLambertMaterial = function ( parameters ) {
  3122. THREE.Material.call( this, parameters );
  3123. parameters = parameters || {};
  3124. // color property represents diffuse for MeshLambertMaterial
  3125. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3126. this.ambient = parameters.ambient !== undefined ? new THREE.Color( parameters.ambient ) : new THREE.Color( 0xffffff );
  3127. this.emissive = parameters.emissive !== undefined ? new THREE.Color( parameters.emissive ) : new THREE.Color( 0x000000 );
  3128. this.wrapAround = parameters.wrapAround !== undefined ? parameters.wrapAround: false;
  3129. this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
  3130. this.map = parameters.map !== undefined ? parameters.map : null;
  3131. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3132. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3133. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3134. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3135. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3136. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3137. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3138. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3139. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3140. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3141. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3142. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3143. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3144. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3145. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false;
  3146. };
  3147. THREE.MeshLambertMaterial.prototype = new THREE.Material();
  3148. THREE.MeshLambertMaterial.prototype.constructor = THREE.MeshLambertMaterial;
  3149. /**
  3150. * @author mr.doob / http://mrdoob.com/
  3151. * @author alteredq / http://alteredqualia.com/
  3152. *
  3153. * parameters = {
  3154. * color: <hex>,
  3155. * ambient: <hex>,
  3156. * emissive: <hex>,
  3157. * specular: <hex>,
  3158. * shininess: <float>,
  3159. * opacity: <float>,
  3160. *
  3161. * map: new THREE.Texture( <Image> ),
  3162. *
  3163. * lightMap: new THREE.Texture( <Image> ),
  3164. *
  3165. * envMap: new THREE.TextureCube( [posx, negx, posy, negy, posz, negz] ),
  3166. * combine: THREE.Multiply,
  3167. * reflectivity: <float>,
  3168. * refractionRatio: <float>,
  3169. *
  3170. * shading: THREE.SmoothShading,
  3171. * blending: THREE.NormalBlending,
  3172. * depthTest: <bool>,
  3173. *
  3174. * wireframe: <boolean>,
  3175. * wireframeLinewidth: <float>,
  3176. *
  3177. * vertexColors: THREE.NoColors / THREE.VertexColors / THREE.FaceColors,
  3178. *
  3179. * skinning: <bool>,
  3180. * morphTargets: <bool>,
  3181. * morphNormals: <bool>,
  3182. *
  3183. * fog: <bool>
  3184. * }
  3185. */
  3186. THREE.MeshPhongMaterial = function ( parameters ) {
  3187. THREE.Material.call( this, parameters );
  3188. parameters = parameters || {};
  3189. // color property represents diffuse for MeshPhongMaterial
  3190. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3191. this.ambient = parameters.ambient !== undefined ? new THREE.Color( parameters.ambient ) : new THREE.Color( 0xffffff );
  3192. this.emissive = parameters.emissive !== undefined ? new THREE.Color( parameters.emissive ) : new THREE.Color( 0x000000 );
  3193. this.specular = parameters.specular !== undefined ? new THREE.Color( parameters.specular ) : new THREE.Color( 0x111111 );
  3194. this.shininess = parameters.shininess !== undefined ? parameters.shininess : 30;
  3195. this.metal = parameters.metal !== undefined ? parameters.metal : false;
  3196. this.perPixel = parameters.perPixel !== undefined ? parameters.perPixel : false;
  3197. this.wrapAround = parameters.wrapAround !== undefined ? parameters.wrapAround: false;
  3198. this.wrapRGB = new THREE.Vector3( 1, 1, 1 );
  3199. this.map = parameters.map !== undefined ? parameters.map : null;
  3200. this.lightMap = parameters.lightMap !== undefined ? parameters.lightMap : null;
  3201. this.envMap = parameters.envMap !== undefined ? parameters.envMap : null;
  3202. this.combine = parameters.combine !== undefined ? parameters.combine : THREE.MultiplyOperation;
  3203. this.reflectivity = parameters.reflectivity !== undefined ? parameters.reflectivity : 1;
  3204. this.refractionRatio = parameters.refractionRatio !== undefined ? parameters.refractionRatio : 0.98;
  3205. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3206. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading;
  3207. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3208. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3209. this.wireframeLinecap = parameters.wireframeLinecap !== undefined ? parameters.wireframeLinecap : 'round';
  3210. this.wireframeLinejoin = parameters.wireframeLinejoin !== undefined ? parameters.wireframeLinejoin : 'round';
  3211. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : THREE.NoColors;
  3212. this.skinning = parameters.skinning !== undefined ? parameters.skinning : false;
  3213. this.morphTargets = parameters.morphTargets !== undefined ? parameters.morphTargets : false;
  3214. this.morphNormals = parameters.morphNormals !== undefined ? parameters.morphNormals : false;
  3215. };
  3216. THREE.MeshPhongMaterial.prototype = new THREE.Material();
  3217. THREE.MeshPhongMaterial.prototype.constructor = THREE.MeshPhongMaterial;
  3218. /**
  3219. * @author mr.doob / http://mrdoob.com/
  3220. * @author alteredq / http://alteredqualia.com/
  3221. *
  3222. * parameters = {
  3223. * opacity: <float>,
  3224. * blending: THREE.NormalBlending,
  3225. * depthTest: <bool>,
  3226. * wireframe: <boolean>,
  3227. * wireframeLinewidth: <float>
  3228. * }
  3229. */
  3230. THREE.MeshDepthMaterial = function ( parameters ) {
  3231. THREE.Material.call( this, parameters );
  3232. parameters = parameters || {};
  3233. this.shading = parameters.shading !== undefined ? parameters.shading : THREE.SmoothShading; // doesn't really apply here, normals are not used
  3234. this.wireframe = parameters.wireframe !== undefined ? parameters.wireframe : false;
  3235. this.wireframeLinewidth = parameters.wireframeLinewidth !== undefined ? parameters.wireframeLinewidth : 1;
  3236. };
  3237. THREE.MeshDepthMaterial.prototype = new THREE.Material();
  3238. THREE.MeshDepthMaterial.prototype.constructor = THREE.MeshDepthMaterial;
  3239. /**
  3240. * @author mr.doob / http://mrdoob.com/
  3241. *
  3242. * parameters = {
  3243. * opacity: <float>,
  3244. * shading: THREE.FlatShading,
  3245. * blending: THREE.NormalBlending,
  3246. * depthTest: <bool>,
  3247. * wireframe: <boolean>,
  3248. * wireframeLinewidth: <float>
  3249. * }
  3250. */
  3251. THREE.MeshNormalMaterial = function ( parameters ) {
  3252. THREE.Material.call( this, parameters );
  3253. parameters = parameters || {};
  3254. this.shading = parameters.shading ? parameters.shading : THREE.FlatShading;
  3255. this.wireframe = parameters.wireframe ? parameters.wireframe : false;
  3256. this.wireframeLinewidth = parameters.wireframeLinewidth ? parameters.wireframeLinewidth : 1;
  3257. };
  3258. THREE.MeshNormalMaterial.prototype = new THREE.Material();
  3259. THREE.MeshNormalMaterial.prototype.constructor = THREE.MeshNormalMaterial;
  3260. /**
  3261. * @author mr.doob / http://mrdoob.com/
  3262. */
  3263. THREE.MeshFaceMaterial = function () {
  3264. };
  3265. /**
  3266. * @author mr.doob / http://mrdoob.com/
  3267. * @author alteredq / http://alteredqualia.com/
  3268. *
  3269. * parameters = {
  3270. * color: <hex>,
  3271. * opacity: <float>,
  3272. * map: new THREE.Texture( <Image> ),
  3273. *
  3274. * size: <float>,
  3275. *
  3276. * blending: THREE.NormalBlending,
  3277. * depthTest: <bool>,
  3278. *
  3279. * vertexColors: <bool>,
  3280. *
  3281. * fog: <bool>
  3282. * }
  3283. */
  3284. THREE.ParticleBasicMaterial = function ( parameters ) {
  3285. THREE.Material.call( this, parameters );
  3286. parameters = parameters || {};
  3287. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3288. this.map = parameters.map !== undefined ? parameters.map : null;
  3289. this.size = parameters.size !== undefined ? parameters.size : 1;
  3290. this.sizeAttenuation = parameters.sizeAttenuation !== undefined ? parameters.sizeAttenuation : true;
  3291. this.vertexColors = parameters.vertexColors !== undefined ? parameters.vertexColors : false;
  3292. this.fog = parameters.fog !== undefined ? parameters.fog : true;
  3293. };
  3294. THREE.ParticleBasicMaterial.prototype = new THREE.Material();
  3295. THREE.ParticleBasicMaterial.prototype.constructor = THREE.ParticleBasicMaterial;
  3296. /**
  3297. * @author mr.doob / http://mrdoob.com/
  3298. *
  3299. * parameters = {
  3300. * color: <hex>,
  3301. * program: <function>,
  3302. * opacity: <float>,
  3303. * blending: THREE.NormalBlending
  3304. * }
  3305. */
  3306. THREE.ParticleCanvasMaterial = function ( parameters ) {
  3307. THREE.Material.call( this, parameters );
  3308. parameters = parameters || {};
  3309. this.color = parameters.color !== undefined ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3310. this.program = parameters.program !== undefined ? parameters.program : function ( context, color ) {};
  3311. };
  3312. THREE.ParticleCanvasMaterial.prototype = new THREE.Material();
  3313. THREE.ParticleCanvasMaterial.prototype.constructor = THREE.ParticleCanvasMaterial;
  3314. /**
  3315. * @author mr.doob / http://mrdoob.com/
  3316. * @author alteredq / http://alteredqualia.com/
  3317. * @author szimek / https://github.com/szimek/
  3318. */
  3319. THREE.Texture = function ( image, mapping, wrapS, wrapT, magFilter, minFilter, format, type ) {
  3320. this.id = THREE.TextureCount ++;
  3321. this.image = image;
  3322. this.mapping = mapping !== undefined ? mapping : new THREE.UVMapping();
  3323. this.wrapS = wrapS !== undefined ? wrapS : THREE.ClampToEdgeWrapping;
  3324. this.wrapT = wrapT !== undefined ? wrapT : THREE.ClampToEdgeWrapping;
  3325. this.magFilter = magFilter !== undefined ? magFilter : THREE.LinearFilter;
  3326. this.minFilter = minFilter !== undefined ? minFilter : THREE.LinearMipMapLinearFilter;
  3327. this.format = format !== undefined ? format : THREE.RGBAFormat;
  3328. this.type = type !== undefined ? type : THREE.UnsignedByteType;
  3329. this.offset = new THREE.Vector2( 0, 0 );
  3330. this.repeat = new THREE.Vector2( 1, 1 );
  3331. this.generateMipmaps = true;
  3332. this.premultiplyAlpha = false;
  3333. this.needsUpdate = false;
  3334. this.onUpdate = null;
  3335. };
  3336. THREE.Texture.prototype = {
  3337. constructor: THREE.Texture,
  3338. clone: function () {
  3339. var clonedTexture = new THREE.Texture( this.image, this.mapping, this.wrapS, this.wrapT, this.magFilter, this.minFilter, this.format, this.type );
  3340. clonedTexture.offset.copy( this.offset );
  3341. clonedTexture.repeat.copy( this.repeat );
  3342. return clonedTexture;
  3343. }
  3344. };
  3345. THREE.TextureCount = 0;
  3346. THREE.MultiplyOperation = 0;
  3347. THREE.MixOperation = 1;
  3348. // Mapping modes
  3349. THREE.UVMapping = function () {};
  3350. THREE.CubeReflectionMapping = function () {};
  3351. THREE.CubeRefractionMapping = function () {};
  3352. THREE.SphericalReflectionMapping = function () {};
  3353. THREE.SphericalRefractionMapping = function () {};
  3354. // Wrapping modes
  3355. THREE.RepeatWrapping = 0;
  3356. THREE.ClampToEdgeWrapping = 1;
  3357. THREE.MirroredRepeatWrapping = 2;
  3358. // Filters
  3359. THREE.NearestFilter = 3;
  3360. THREE.NearestMipMapNearestFilter = 4;
  3361. THREE.NearestMipMapLinearFilter = 5;
  3362. THREE.LinearFilter = 6;
  3363. THREE.LinearMipMapNearestFilter = 7;
  3364. THREE.LinearMipMapLinearFilter = 8;
  3365. // Types
  3366. THREE.ByteType = 9;
  3367. THREE.UnsignedByteType = 10;
  3368. THREE.ShortType = 11;
  3369. THREE.UnsignedShortType = 12;
  3370. THREE.IntType = 13;
  3371. THREE.UnsignedIntType = 14;
  3372. THREE.FloatType = 15;
  3373. // Formats
  3374. THREE.AlphaFormat = 16;
  3375. THREE.RGBFormat = 17;
  3376. THREE.RGBAFormat = 18;
  3377. THREE.LuminanceFormat = 19;
  3378. THREE.LuminanceAlphaFormat = 20;
  3379. /**
  3380. * @author alteredq / http://alteredqualia.com/
  3381. */
  3382. THREE.DataTexture = function ( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter ) {
  3383. THREE.Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type );
  3384. this.image = { data: data, width: width, height: height };
  3385. };
  3386. THREE.DataTexture.prototype = new THREE.Texture();
  3387. THREE.DataTexture.prototype.constructor = THREE.DataTexture;
  3388. THREE.DataTexture.prototype.clone = function () {
  3389. var clonedTexture = new THREE.DataTexture( this.image.data, this.image.width, this.image.height, this.format, this.type, this.mapping, this.wrapS, this.wrapT, this.magFilter, this.minFilter );
  3390. clonedTexture.offset.copy( this.offset );
  3391. clonedTexture.repeat.copy( this.repeat );
  3392. return clonedTexture;
  3393. };
  3394. /**
  3395. * @author mr.doob / http://mrdoob.com/
  3396. */
  3397. THREE.Particle = function ( material ) {
  3398. THREE.Object3D.call( this );
  3399. this.material = material;
  3400. };
  3401. THREE.Particle.prototype = new THREE.Object3D();
  3402. THREE.Particle.prototype.constructor = THREE.Particle;
  3403. /**
  3404. * @author mr.doob / http://mrdoob.com/
  3405. */
  3406. THREE.Line = function ( geometry, material, type ) {
  3407. THREE.Object3D.call( this );
  3408. this.geometry = geometry;
  3409. this.material = ( material !== undefined ) ? material : new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff } );
  3410. this.type = ( type !== undefined ) ? type : THREE.LineStrip;
  3411. if ( this.geometry ) {
  3412. if ( ! this.geometry.boundingSphere ) {
  3413. this.geometry.computeBoundingSphere();
  3414. }
  3415. }
  3416. };
  3417. THREE.LineStrip = 0;
  3418. THREE.LinePieces = 1;
  3419. THREE.Line.prototype = new THREE.Object3D();
  3420. THREE.Line.prototype.constructor = THREE.Line;
  3421. /**
  3422. * @author mr.doob / http://mrdoob.com/
  3423. * @author alteredq / http://alteredqualia.com/
  3424. * @author mikael emtinger / http://gomo.se/
  3425. */
  3426. THREE.Mesh = function ( geometry, material ) {
  3427. THREE.Object3D.call( this );
  3428. this.geometry = geometry;
  3429. this.material = ( material !== undefined ) ? material : new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: true } );
  3430. if ( this.geometry ) {
  3431. // calc bound radius
  3432. if( ! this.geometry.boundingSphere ) {
  3433. this.geometry.computeBoundingSphere();
  3434. }
  3435. this.boundRadius = geometry.boundingSphere.radius;
  3436. // setup morph targets
  3437. if( this.geometry.morphTargets.length ) {
  3438. this.morphTargetBase = -1;
  3439. this.morphTargetForcedOrder = [];
  3440. this.morphTargetInfluences = [];
  3441. this.morphTargetDictionary = {};
  3442. for( var m = 0; m < this.geometry.morphTargets.length; m ++ ) {
  3443. this.morphTargetInfluences.push( 0 );
  3444. this.morphTargetDictionary[ this.geometry.morphTargets[ m ].name ] = m;
  3445. }
  3446. }
  3447. }
  3448. }
  3449. THREE.Mesh.prototype = new THREE.Object3D();
  3450. THREE.Mesh.prototype.constructor = THREE.Mesh;
  3451. THREE.Mesh.prototype.supr = THREE.Object3D.prototype;
  3452. /*
  3453. * Get Morph Target Index by Name
  3454. */
  3455. THREE.Mesh.prototype.getMorphTargetIndexByName = function( name ) {
  3456. if ( this.morphTargetDictionary[ name ] !== undefined ) {
  3457. return this.morphTargetDictionary[ name ];
  3458. }
  3459. console.log( "THREE.Mesh.getMorphTargetIndexByName: morph target " + name + " does not exist. Returning 0." );
  3460. return 0;
  3461. }
  3462. /**
  3463. * @author mikael emtinger / http://gomo.se/
  3464. * @author alteredq / http://alteredqualia.com/
  3465. */
  3466. THREE.Bone = function( belongsToSkin ) {
  3467. THREE.Object3D.call( this );
  3468. this.skin = belongsToSkin;
  3469. this.skinMatrix = new THREE.Matrix4();
  3470. };
  3471. THREE.Bone.prototype = new THREE.Object3D();
  3472. THREE.Bone.prototype.constructor = THREE.Bone;
  3473. THREE.Bone.prototype.supr = THREE.Object3D.prototype;
  3474. THREE.Bone.prototype.update = function( parentSkinMatrix, forceUpdate ) {
  3475. // update local
  3476. if ( this.matrixAutoUpdate ) {
  3477. forceUpdate |= this.updateMatrix();
  3478. }
  3479. // update skin matrix
  3480. if ( forceUpdate || this.matrixWorldNeedsUpdate ) {
  3481. if( parentSkinMatrix ) {
  3482. this.skinMatrix.multiply( parentSkinMatrix, this.matrix );
  3483. } else {
  3484. this.skinMatrix.copy( this.matrix );
  3485. }
  3486. this.matrixWorldNeedsUpdate = false;
  3487. forceUpdate = true;
  3488. }
  3489. // update children
  3490. var child, i, l = this.children.length;
  3491. for ( i = 0; i < l; i ++ ) {
  3492. this.children[ i ].update( this.skinMatrix, forceUpdate );
  3493. }
  3494. };
  3495. /**
  3496. * @author mikael emtinger / http://gomo.se/
  3497. */
  3498. THREE.Sprite = function ( parameters ) {
  3499. THREE.Object3D.call( this );
  3500. this.color = ( parameters.color !== undefined ) ? new THREE.Color( parameters.color ) : new THREE.Color( 0xffffff );
  3501. this.map = ( parameters.map !== undefined ) ? parameters.map : new THREE.Texture();
  3502. this.blending = ( parameters.blending !== undefined ) ? parameters.blending : THREE.NormalBlending;
  3503. this.blendSrc = parameters.blendSrc !== undefined ? parameters.blendSrc : THREE.SrcAlphaFactor;
  3504. this.blendDst = parameters.blendDst !== undefined ? parameters.blendDst : THREE.OneMinusSrcAlphaFactor;
  3505. this.blendEquation = parameters.blendEquation !== undefined ? parameters.blendEquation : THREE.AddEquation;
  3506. this.useScreenCoordinates = ( parameters.useScreenCoordinates !== undefined ) ? parameters.useScreenCoordinates : true;
  3507. this.mergeWith3D = ( parameters.mergeWith3D !== undefined ) ? parameters.mergeWith3D : !this.useScreenCoordinates;
  3508. this.affectedByDistance = ( parameters.affectedByDistance !== undefined ) ? parameters.affectedByDistance : !this.useScreenCoordinates;
  3509. this.scaleByViewport = ( parameters.scaleByViewport !== undefined ) ? parameters.scaleByViewport : !this.affectedByDistance;
  3510. this.alignment = ( parameters.alignment instanceof THREE.Vector2 ) ? parameters.alignment : THREE.SpriteAlignment.center;
  3511. this.rotation3d = this.rotation;
  3512. this.rotation = 0;
  3513. this.opacity = 1;
  3514. this.uvOffset = new THREE.Vector2( 0, 0 );
  3515. this.uvScale = new THREE.Vector2( 1, 1 );
  3516. };
  3517. THREE.Sprite.prototype = new THREE.Object3D();
  3518. THREE.Sprite.prototype.constructor = THREE.Sprite;
  3519. /*
  3520. * Custom update matrix
  3521. */
  3522. THREE.Sprite.prototype.updateMatrix = function () {
  3523. this.matrix.setPosition( this.position );
  3524. this.rotation3d.set( 0, 0, this.rotation );
  3525. this.matrix.setRotationFromEuler( this.rotation3d );
  3526. if ( this.scale.x !== 1 || this.scale.y !== 1 ) {
  3527. this.matrix.scale( this.scale );
  3528. this.boundRadiusScale = Math.max( this.scale.x, this.scale.y );
  3529. }
  3530. this.matrixWorldNeedsUpdate = true;
  3531. };
  3532. /*
  3533. * Alignment
  3534. */
  3535. THREE.SpriteAlignment = {};
  3536. THREE.SpriteAlignment.topLeft = new THREE.Vector2( 1, -1 );
  3537. THREE.SpriteAlignment.topCenter = new THREE.Vector2( 0, -1 );
  3538. THREE.SpriteAlignment.topRight = new THREE.Vector2( -1, -1 );
  3539. THREE.SpriteAlignment.centerLeft = new THREE.Vector2( 1, 0 );
  3540. THREE.SpriteAlignment.center = new THREE.Vector2( 0, 0 );
  3541. THREE.SpriteAlignment.centerRight = new THREE.Vector2( -1, 0 );
  3542. THREE.SpriteAlignment.bottomLeft = new THREE.Vector2( 1, 1 );
  3543. THREE.SpriteAlignment.bottomCenter = new THREE.Vector2( 0, 1 );
  3544. THREE.SpriteAlignment.bottomRight = new THREE.Vector2( -1, 1 );
  3545. /**
  3546. * @author mr.doob / http://mrdoob.com/
  3547. */
  3548. THREE.Scene = function () {
  3549. THREE.Object3D.call( this );
  3550. this.fog = null;
  3551. this.overrideMaterial = null;
  3552. this.matrixAutoUpdate = false;
  3553. this.__objects = [];
  3554. this.__lights = [];
  3555. this.__objectsAdded = [];
  3556. this.__objectsRemoved = [];
  3557. };
  3558. THREE.Scene.prototype = new THREE.Object3D();
  3559. THREE.Scene.prototype.constructor = THREE.Scene;
  3560. THREE.Scene.prototype.__addObject = function ( object ) {
  3561. if ( object instanceof THREE.Light ) {
  3562. if ( this.__lights.indexOf( object ) === - 1 ) {
  3563. this.__lights.push( object );
  3564. }
  3565. } else if ( !( object instanceof THREE.Camera || object instanceof THREE.Bone ) ) {
  3566. if ( this.__objects.indexOf( object ) === - 1 ) {
  3567. this.__objects.push( object );
  3568. this.__objectsAdded.push( object );
  3569. // check if previously removed
  3570. var i = this.__objectsRemoved.indexOf( object );
  3571. if ( i !== -1 ) {
  3572. this.__objectsRemoved.splice( i, 1 );
  3573. }
  3574. }
  3575. }
  3576. for ( var c = 0; c < object.children.length; c ++ ) {
  3577. this.__addObject( object.children[ c ] );
  3578. }
  3579. };
  3580. THREE.Scene.prototype.__removeObject = function ( object ) {
  3581. if ( object instanceof THREE.Light ) {
  3582. var i = this.__lights.indexOf( object );
  3583. if ( i !== -1 ) {
  3584. this.__lights.splice( i, 1 );
  3585. }
  3586. } else if ( !( object instanceof THREE.Camera ) ) {
  3587. var i = this.__objects.indexOf( object );
  3588. if( i !== -1 ) {
  3589. this.__objects.splice( i, 1 );
  3590. this.__objectsRemoved.push( object );
  3591. // check if previously added
  3592. var ai = this.__objectsAdded.indexOf( object );
  3593. if ( ai !== -1 ) {
  3594. this.__objectsAdded.splice( ai, 1 );
  3595. }
  3596. }
  3597. }
  3598. for ( var c = 0; c < object.children.length; c ++ ) {
  3599. this.__removeObject( object.children[ c ] );
  3600. }
  3601. };
  3602. /**
  3603. * @author mr.doob / http://mrdoob.com/
  3604. */
  3605. THREE.CanvasRenderer = function ( parameters ) {
  3606. parameters = parameters || {};
  3607. var _this = this,
  3608. _renderData, _elements, _lights,
  3609. _projector = new THREE.Projector(),
  3610. _canvas = parameters.canvas !== undefined ? parameters.canvas : document.createElement( 'canvas' ),
  3611. _canvasWidth, _canvasHeight, _canvasWidthHalf, _canvasHeightHalf,
  3612. _context = _canvas.getContext( '2d' ),
  3613. _clearColor = new THREE.Color( 0x000000 ),
  3614. _clearOpacity = 0,
  3615. _contextGlobalAlpha = 1,
  3616. _contextGlobalCompositeOperation = 0,
  3617. _contextStrokeStyle = null,
  3618. _contextFillStyle = null,
  3619. _contextLineWidth = null,
  3620. _contextLineCap = null,
  3621. _contextLineJoin = null,
  3622. _v1, _v2, _v3, _v4,
  3623. _v5 = new THREE.RenderableVertex(),
  3624. _v6 = new THREE.RenderableVertex(),
  3625. _v1x, _v1y, _v2x, _v2y, _v3x, _v3y,
  3626. _v4x, _v4y, _v5x, _v5y, _v6x, _v6y,
  3627. _color = new THREE.Color(),
  3628. _color1 = new THREE.Color(),
  3629. _color2 = new THREE.Color(),
  3630. _color3 = new THREE.Color(),
  3631. _color4 = new THREE.Color(),
  3632. _patterns = [], _imagedatas = [],
  3633. _near, _far,
  3634. _image, _uvs,
  3635. _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y,
  3636. _clipRect = new THREE.Rectangle(),
  3637. _clearRect = new THREE.Rectangle(),
  3638. _bboxRect = new THREE.Rectangle(),
  3639. _enableLighting = false,
  3640. _ambientLight = new THREE.Color(),
  3641. _directionalLights = new THREE.Color(),
  3642. _pointLights = new THREE.Color(),
  3643. _pi2 = Math.PI * 2,
  3644. _vector3 = new THREE.Vector3(), // Needed for PointLight
  3645. _pixelMap, _pixelMapContext, _pixelMapImage, _pixelMapData,
  3646. _gradientMap, _gradientMapContext, _gradientMapQuality = 16;
  3647. _pixelMap = document.createElement( 'canvas' );
  3648. _pixelMap.width = _pixelMap.height = 2;
  3649. _pixelMapContext = _pixelMap.getContext( '2d' );
  3650. _pixelMapContext.fillStyle = 'rgba(0,0,0,1)';
  3651. _pixelMapContext.fillRect( 0, 0, 2, 2 );
  3652. _pixelMapImage = _pixelMapContext.getImageData( 0, 0, 2, 2 );
  3653. _pixelMapData = _pixelMapImage.data;
  3654. _gradientMap = document.createElement( 'canvas' );
  3655. _gradientMap.width = _gradientMap.height = _gradientMapQuality;
  3656. _gradientMapContext = _gradientMap.getContext( '2d' );
  3657. _gradientMapContext.translate( - _gradientMapQuality / 2, - _gradientMapQuality / 2 );
  3658. _gradientMapContext.scale( _gradientMapQuality, _gradientMapQuality );
  3659. _gradientMapQuality --; // Fix UVs
  3660. this.domElement = _canvas;
  3661. this.autoClear = true;
  3662. this.sortObjects = true;
  3663. this.sortElements = true;
  3664. this.info = {
  3665. render: {
  3666. vertices: 0,
  3667. faces: 0
  3668. }
  3669. }
  3670. this.setSize = function ( width, height ) {
  3671. _canvasWidth = width;
  3672. _canvasHeight = height;
  3673. _canvasWidthHalf = Math.floor( _canvasWidth / 2 );
  3674. _canvasHeightHalf = Math.floor( _canvasHeight / 2 );
  3675. _canvas.width = _canvasWidth;
  3676. _canvas.height = _canvasHeight;
  3677. _clipRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  3678. _clearRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  3679. _contextGlobalAlpha = 1;
  3680. _contextGlobalCompositeOperation = 0;
  3681. _contextStrokeStyle = null;
  3682. _contextFillStyle = null;
  3683. _contextLineWidth = null;
  3684. _contextLineCap = null;
  3685. _contextLineJoin = null;
  3686. };
  3687. this.setClearColor = function ( color, opacity ) {
  3688. _clearColor.copy( color );
  3689. _clearOpacity = opacity !== undefined ? opacity : 1;
  3690. _clearRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  3691. };
  3692. this.setClearColorHex = function ( hex, opacity ) {
  3693. _clearColor.setHex( hex );
  3694. _clearOpacity = opacity !== undefined ? opacity : 1;
  3695. _clearRect.set( - _canvasWidthHalf, - _canvasHeightHalf, _canvasWidthHalf, _canvasHeightHalf );
  3696. };
  3697. this.clear = function () {
  3698. _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  3699. if ( !_clearRect.isEmpty() ) {
  3700. _clearRect.minSelf( _clipRect );
  3701. _clearRect.inflate( 2 );
  3702. if ( _clearOpacity < 1 ) {
  3703. _context.clearRect( Math.floor( _clearRect.getX() ), Math.floor( _clearRect.getY() ), Math.floor( _clearRect.getWidth() ), Math.floor( _clearRect.getHeight() ) );
  3704. }
  3705. if ( _clearOpacity > 0 ) {
  3706. setBlending( THREE.NormalBlending );
  3707. setOpacity( 1 );
  3708. setFillStyle( 'rgba(' + Math.floor( _clearColor.r * 255 ) + ',' + Math.floor( _clearColor.g * 255 ) + ',' + Math.floor( _clearColor.b * 255 ) + ',' + _clearOpacity + ')' );
  3709. _context.fillRect( Math.floor( _clearRect.getX() ), Math.floor( _clearRect.getY() ), Math.floor( _clearRect.getWidth() ), Math.floor( _clearRect.getHeight() ) );
  3710. }
  3711. _clearRect.empty();
  3712. }
  3713. };
  3714. this.render = function ( scene, camera ) {
  3715. var e, el, element, material;
  3716. this.autoClear ? this.clear() : _context.setTransform( 1, 0, 0, - 1, _canvasWidthHalf, _canvasHeightHalf );
  3717. _this.info.render.vertices = 0;
  3718. _this.info.render.faces = 0;
  3719. _renderData = _projector.projectScene( scene, camera, this.sortElements );
  3720. _elements = _renderData.elements;
  3721. _lights = _renderData.lights;
  3722. /* DEBUG
  3723. _context.fillStyle = 'rgba( 0, 255, 255, 0.5 )';
  3724. _context.fillRect( _clipRect.getX(), _clipRect.getY(), _clipRect.getWidth(), _clipRect.getHeight() );
  3725. */
  3726. _enableLighting = _lights.length > 0;
  3727. if ( _enableLighting ) {
  3728. calculateLights( _lights );
  3729. }
  3730. for ( e = 0, el = _elements.length; e < el; e++ ) {
  3731. element = _elements[ e ];
  3732. material = element.material;
  3733. material = material instanceof THREE.MeshFaceMaterial ? element.faceMaterial : material;
  3734. if ( material == null || material.opacity == 0 ) continue;
  3735. _bboxRect.empty();
  3736. if ( element instanceof THREE.RenderableParticle ) {
  3737. _v1 = element;
  3738. _v1.x *= _canvasWidthHalf; _v1.y *= _canvasHeightHalf;
  3739. renderParticle( _v1, element, material, scene );
  3740. } else if ( element instanceof THREE.RenderableLine ) {
  3741. _v1 = element.v1; _v2 = element.v2;
  3742. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  3743. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  3744. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  3745. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  3746. if ( _clipRect.intersects( _bboxRect ) ) {
  3747. renderLine( _v1, _v2, element, material, scene );
  3748. }
  3749. } else if ( element instanceof THREE.RenderableFace3 ) {
  3750. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3;
  3751. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  3752. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  3753. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  3754. if ( material.overdraw ) {
  3755. expand( _v1.positionScreen, _v2.positionScreen );
  3756. expand( _v2.positionScreen, _v3.positionScreen );
  3757. expand( _v3.positionScreen, _v1.positionScreen );
  3758. }
  3759. _bboxRect.add3Points( _v1.positionScreen.x, _v1.positionScreen.y,
  3760. _v2.positionScreen.x, _v2.positionScreen.y,
  3761. _v3.positionScreen.x, _v3.positionScreen.y );
  3762. if ( _clipRect.intersects( _bboxRect ) ) {
  3763. renderFace3( _v1, _v2, _v3, 0, 1, 2, element, material, scene );
  3764. }
  3765. } else if ( element instanceof THREE.RenderableFace4 ) {
  3766. _v1 = element.v1; _v2 = element.v2; _v3 = element.v3; _v4 = element.v4;
  3767. _v1.positionScreen.x *= _canvasWidthHalf; _v1.positionScreen.y *= _canvasHeightHalf;
  3768. _v2.positionScreen.x *= _canvasWidthHalf; _v2.positionScreen.y *= _canvasHeightHalf;
  3769. _v3.positionScreen.x *= _canvasWidthHalf; _v3.positionScreen.y *= _canvasHeightHalf;
  3770. _v4.positionScreen.x *= _canvasWidthHalf; _v4.positionScreen.y *= _canvasHeightHalf;
  3771. _v5.positionScreen.copy( _v2.positionScreen );
  3772. _v6.positionScreen.copy( _v4.positionScreen );
  3773. if ( material.overdraw ) {
  3774. expand( _v1.positionScreen, _v2.positionScreen );
  3775. expand( _v2.positionScreen, _v4.positionScreen );
  3776. expand( _v4.positionScreen, _v1.positionScreen );
  3777. expand( _v3.positionScreen, _v5.positionScreen );
  3778. expand( _v3.positionScreen, _v6.positionScreen );
  3779. }
  3780. _bboxRect.addPoint( _v1.positionScreen.x, _v1.positionScreen.y );
  3781. _bboxRect.addPoint( _v2.positionScreen.x, _v2.positionScreen.y );
  3782. _bboxRect.addPoint( _v3.positionScreen.x, _v3.positionScreen.y );
  3783. _bboxRect.addPoint( _v4.positionScreen.x, _v4.positionScreen.y );
  3784. if ( _clipRect.intersects( _bboxRect ) ) {
  3785. renderFace4( _v1, _v2, _v3, _v4, _v5, _v6, element, material, scene );
  3786. }
  3787. }
  3788. /*
  3789. _context.lineWidth = 1;
  3790. _context.strokeStyle = 'rgba( 0, 255, 0, 0.5 )';
  3791. _context.strokeRect( _bboxRect.getX(), _bboxRect.getY(), _bboxRect.getWidth(), _bboxRect.getHeight() );
  3792. */
  3793. _clearRect.addRectangle( _bboxRect );
  3794. }
  3795. /* DEBUG
  3796. _context.lineWidth = 1;
  3797. _context.strokeStyle = 'rgba( 255, 0, 0, 0.5 )';
  3798. _context.strokeRect( _clearRect.getX(), _clearRect.getY(), _clearRect.getWidth(), _clearRect.getHeight() );
  3799. */
  3800. _context.setTransform( 1, 0, 0, 1, 0, 0 );
  3801. //
  3802. function calculateLights( lights ) {
  3803. var l, ll, light, lightColor;
  3804. _ambientLight.setRGB( 0, 0, 0 );
  3805. _directionalLights.setRGB( 0, 0, 0 );
  3806. _pointLights.setRGB( 0, 0, 0 );
  3807. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  3808. light = lights[ l ];
  3809. lightColor = light.color;
  3810. if ( light instanceof THREE.AmbientLight ) {
  3811. _ambientLight.r += lightColor.r;
  3812. _ambientLight.g += lightColor.g;
  3813. _ambientLight.b += lightColor.b;
  3814. } else if ( light instanceof THREE.DirectionalLight ) {
  3815. // for particles
  3816. _directionalLights.r += lightColor.r;
  3817. _directionalLights.g += lightColor.g;
  3818. _directionalLights.b += lightColor.b;
  3819. } else if ( light instanceof THREE.PointLight ) {
  3820. // for particles
  3821. _pointLights.r += lightColor.r;
  3822. _pointLights.g += lightColor.g;
  3823. _pointLights.b += lightColor.b;
  3824. }
  3825. }
  3826. }
  3827. function calculateLight( lights, position, normal, color ) {
  3828. var l, ll, light, lightColor, lightPosition, amount;
  3829. for ( l = 0, ll = lights.length; l < ll; l ++ ) {
  3830. light = lights[ l ];
  3831. lightColor = light.color;
  3832. if ( light instanceof THREE.DirectionalLight ) {
  3833. lightPosition = light.matrixWorld.getPosition();
  3834. amount = normal.dot( lightPosition );
  3835. if ( amount <= 0 ) continue;
  3836. amount *= light.intensity;
  3837. color.r += lightColor.r * amount;
  3838. color.g += lightColor.g * amount;
  3839. color.b += lightColor.b * amount;
  3840. } else if ( light instanceof THREE.PointLight ) {
  3841. lightPosition = light.matrixWorld.getPosition();
  3842. amount = normal.dot( _vector3.sub( lightPosition, position ).normalize() );
  3843. if ( amount <= 0 ) continue;
  3844. amount *= light.distance == 0 ? 1 : 1 - Math.min( position.distanceTo( lightPosition ) / light.distance, 1 );
  3845. if ( amount == 0 ) continue;
  3846. amount *= light.intensity;
  3847. color.r += lightColor.r * amount;
  3848. color.g += lightColor.g * amount;
  3849. color.b += lightColor.b * amount;
  3850. }
  3851. }
  3852. }
  3853. function renderParticle ( v1, element, material, scene ) {
  3854. setOpacity( material.opacity );
  3855. setBlending( material.blending );
  3856. var width, height, scaleX, scaleY,
  3857. bitmap, bitmapWidth, bitmapHeight;
  3858. if ( material instanceof THREE.ParticleBasicMaterial ) {
  3859. if ( material.map ) {
  3860. bitmap = material.map.image;
  3861. bitmapWidth = bitmap.width >> 1;
  3862. bitmapHeight = bitmap.height >> 1;
  3863. scaleX = element.scale.x * _canvasWidthHalf;
  3864. scaleY = element.scale.y * _canvasHeightHalf;
  3865. width = scaleX * bitmapWidth;
  3866. height = scaleY * bitmapHeight;
  3867. // TODO: Rotations break this...
  3868. _bboxRect.set( v1.x - width, v1.y - height, v1.x + width, v1.y + height );
  3869. if ( !_clipRect.intersects( _bboxRect ) ) {
  3870. return;
  3871. }
  3872. _context.save();
  3873. _context.translate( v1.x, v1.y );
  3874. _context.rotate( - element.rotation );
  3875. _context.scale( scaleX, - scaleY );
  3876. _context.translate( - bitmapWidth, - bitmapHeight );
  3877. _context.drawImage( bitmap, 0, 0 );
  3878. _context.restore();
  3879. }
  3880. /* DEBUG
  3881. _context.beginPath();
  3882. _context.moveTo( v1.x - 10, v1.y );
  3883. _context.lineTo( v1.x + 10, v1.y );
  3884. _context.moveTo( v1.x, v1.y - 10 );
  3885. _context.lineTo( v1.x, v1.y + 10 );
  3886. _context.closePath();
  3887. _context.strokeStyle = 'rgb(255,255,0)';
  3888. _context.stroke();
  3889. */
  3890. } else if ( material instanceof THREE.ParticleCanvasMaterial ) {
  3891. width = element.scale.x * _canvasWidthHalf;
  3892. height = element.scale.y * _canvasHeightHalf;
  3893. _bboxRect.set( v1.x - width, v1.y - height, v1.x + width, v1.y + height );
  3894. if ( !_clipRect.intersects( _bboxRect ) ) {
  3895. return;
  3896. }
  3897. setStrokeStyle( material.color.getContextStyle() );
  3898. setFillStyle( material.color.getContextStyle() );
  3899. _context.save();
  3900. _context.translate( v1.x, v1.y );
  3901. _context.rotate( - element.rotation );
  3902. _context.scale( width, height );
  3903. material.program( _context );
  3904. _context.restore();
  3905. }
  3906. }
  3907. function renderLine( v1, v2, element, material, scene ) {
  3908. setOpacity( material.opacity );
  3909. setBlending( material.blending );
  3910. _context.beginPath();
  3911. _context.moveTo( v1.positionScreen.x, v1.positionScreen.y );
  3912. _context.lineTo( v2.positionScreen.x, v2.positionScreen.y );
  3913. _context.closePath();
  3914. if ( material instanceof THREE.LineBasicMaterial ) {
  3915. setLineWidth( material.linewidth );
  3916. setLineCap( material.linecap );
  3917. setLineJoin( material.linejoin );
  3918. setStrokeStyle( material.color.getContextStyle() );
  3919. _context.stroke();
  3920. _bboxRect.inflate( material.linewidth * 2 );
  3921. }
  3922. }
  3923. function renderFace3( v1, v2, v3, uv1, uv2, uv3, element, material, scene ) {
  3924. _this.info.render.vertices += 3;
  3925. _this.info.render.faces ++;
  3926. setOpacity( material.opacity );
  3927. setBlending( material.blending );
  3928. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  3929. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  3930. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  3931. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y );
  3932. if ( material instanceof THREE.MeshBasicMaterial ) {
  3933. if ( material.map/* && !material.wireframe*/ ) {
  3934. if ( material.map.mapping instanceof THREE.UVMapping ) {
  3935. _uvs = element.uvs[ 0 ];
  3936. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uvs[ uv1 ].u, _uvs[ uv1 ].v, _uvs[ uv2 ].u, _uvs[ uv2 ].v, _uvs[ uv3 ].u, _uvs[ uv3 ].v, material.map );
  3937. }
  3938. } else if ( material.envMap ) {
  3939. if ( material.envMap.mapping instanceof THREE.SphericalReflectionMapping ) {
  3940. var cameraMatrix = camera.matrixWorldInverse;
  3941. _vector3.copy( element.vertexNormalsWorld[ uv1 ] );
  3942. _uv1x = ( _vector3.x * cameraMatrix.elements[0] + _vector3.y * cameraMatrix.elements[4] + _vector3.z * cameraMatrix.elements[8] ) * 0.5 + 0.5;
  3943. _uv1y = - ( _vector3.x * cameraMatrix.elements[1] + _vector3.y * cameraMatrix.elements[5] + _vector3.z * cameraMatrix.elements[9] ) * 0.5 + 0.5;
  3944. _vector3.copy( element.vertexNormalsWorld[ uv2 ] );
  3945. _uv2x = ( _vector3.x * cameraMatrix.elements[0] + _vector3.y * cameraMatrix.elements[4] + _vector3.z * cameraMatrix.elements[8] ) * 0.5 + 0.5;
  3946. _uv2y = - ( _vector3.x * cameraMatrix.elements[1] + _vector3.y * cameraMatrix.elements[5] + _vector3.z * cameraMatrix.elements[9] ) * 0.5 + 0.5;
  3947. _vector3.copy( element.vertexNormalsWorld[ uv3 ] );
  3948. _uv3x = ( _vector3.x * cameraMatrix.elements[0] + _vector3.y * cameraMatrix.elements[4] + _vector3.z * cameraMatrix.elements[8] ) * 0.5 + 0.5;
  3949. _uv3y = - ( _vector3.x * cameraMatrix.elements[1] + _vector3.y * cameraMatrix.elements[5] + _vector3.z * cameraMatrix.elements[9] ) * 0.5 + 0.5;
  3950. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uv1x, _uv1y, _uv2x, _uv2y, _uv3x, _uv3y, material.envMap );
  3951. }/* else if ( material.envMap.mapping == THREE.SphericalRefractionMapping ) {
  3952. }*/
  3953. } else {
  3954. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  3955. }
  3956. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  3957. if ( material.map && !material.wireframe ) {
  3958. if ( material.map.mapping instanceof THREE.UVMapping ) {
  3959. _uvs = element.uvs[ 0 ];
  3960. patternPath( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _uvs[ uv1 ].u, _uvs[ uv1 ].v, _uvs[ uv2 ].u, _uvs[ uv2 ].v, _uvs[ uv3 ].u, _uvs[ uv3 ].v, material.map );
  3961. }
  3962. setBlending( THREE.SubtractiveBlending );
  3963. }
  3964. if ( _enableLighting ) {
  3965. if ( !material.wireframe && material.shading == THREE.SmoothShading && element.vertexNormalsWorld.length == 3 ) {
  3966. _color1.r = _color2.r = _color3.r = _ambientLight.r;
  3967. _color1.g = _color2.g = _color3.g = _ambientLight.g;
  3968. _color1.b = _color2.b = _color3.b = _ambientLight.b;
  3969. calculateLight( _lights, element.v1.positionWorld, element.vertexNormalsWorld[ 0 ], _color1 );
  3970. calculateLight( _lights, element.v2.positionWorld, element.vertexNormalsWorld[ 1 ], _color2 );
  3971. calculateLight( _lights, element.v3.positionWorld, element.vertexNormalsWorld[ 2 ], _color3 );
  3972. _color1.r = Math.max( 0, Math.min( material.color.r * _color1.r, 1 ) );
  3973. _color1.g = Math.max( 0, Math.min( material.color.g * _color1.g, 1 ) );
  3974. _color1.b = Math.max( 0, Math.min( material.color.b * _color1.b, 1 ) );
  3975. _color2.r = Math.max( 0, Math.min( material.color.r * _color2.r, 1 ) );
  3976. _color2.g = Math.max( 0, Math.min( material.color.g * _color2.g, 1 ) );
  3977. _color2.b = Math.max( 0, Math.min( material.color.b * _color2.b, 1 ) );
  3978. _color3.r = Math.max( 0, Math.min( material.color.r * _color3.r, 1 ) );
  3979. _color3.g = Math.max( 0, Math.min( material.color.g * _color3.g, 1 ) );
  3980. _color3.b = Math.max( 0, Math.min( material.color.b * _color3.b, 1 ) );
  3981. _color4.r = ( _color2.r + _color3.r ) * 0.5;
  3982. _color4.g = ( _color2.g + _color3.g ) * 0.5;
  3983. _color4.b = ( _color2.b + _color3.b ) * 0.5;
  3984. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  3985. clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image );
  3986. } else {
  3987. _color.r = _ambientLight.r;
  3988. _color.g = _ambientLight.g;
  3989. _color.b = _ambientLight.b;
  3990. calculateLight( _lights, element.centroidWorld, element.normalWorld, _color );
  3991. _color.r = Math.max( 0, Math.min( material.color.r * _color.r, 1 ) );
  3992. _color.g = Math.max( 0, Math.min( material.color.g * _color.g, 1 ) );
  3993. _color.b = Math.max( 0, Math.min( material.color.b * _color.b, 1 ) );
  3994. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  3995. }
  3996. } else {
  3997. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  3998. }
  3999. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  4000. _near = camera.near;
  4001. _far = camera.far;
  4002. _color1.r = _color1.g = _color1.b = 1 - smoothstep( v1.positionScreen.z, _near, _far );
  4003. _color2.r = _color2.g = _color2.b = 1 - smoothstep( v2.positionScreen.z, _near, _far );
  4004. _color3.r = _color3.g = _color3.b = 1 - smoothstep( v3.positionScreen.z, _near, _far );
  4005. _color4.r = ( _color2.r + _color3.r ) * 0.5;
  4006. _color4.g = ( _color2.g + _color3.g ) * 0.5;
  4007. _color4.b = ( _color2.b + _color3.b ) * 0.5;
  4008. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  4009. clipImage( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, 0, 0, 1, 0, 0, 1, _image );
  4010. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  4011. _color.r = normalToComponent( element.normalWorld.x );
  4012. _color.g = normalToComponent( element.normalWorld.y );
  4013. _color.b = normalToComponent( element.normalWorld.z );
  4014. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  4015. }
  4016. }
  4017. function renderFace4( v1, v2, v3, v4, v5, v6, element, material, scene ) {
  4018. _this.info.render.vertices += 4;
  4019. _this.info.render.faces ++;
  4020. setOpacity( material.opacity );
  4021. setBlending( material.blending );
  4022. if ( material.map || material.envMap ) {
  4023. // Let renderFace3() handle this
  4024. renderFace3( v1, v2, v4, 0, 1, 3, element, material, scene );
  4025. renderFace3( v5, v3, v6, 1, 2, 3, element, material, scene );
  4026. return;
  4027. }
  4028. _v1x = v1.positionScreen.x; _v1y = v1.positionScreen.y;
  4029. _v2x = v2.positionScreen.x; _v2y = v2.positionScreen.y;
  4030. _v3x = v3.positionScreen.x; _v3y = v3.positionScreen.y;
  4031. _v4x = v4.positionScreen.x; _v4y = v4.positionScreen.y;
  4032. _v5x = v5.positionScreen.x; _v5y = v5.positionScreen.y;
  4033. _v6x = v6.positionScreen.x; _v6y = v6.positionScreen.y;
  4034. if ( material instanceof THREE.MeshBasicMaterial ) {
  4035. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  4036. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  4037. } else if ( material instanceof THREE.MeshLambertMaterial ) {
  4038. if ( _enableLighting ) {
  4039. if ( !material.wireframe && material.shading == THREE.SmoothShading && element.vertexNormalsWorld.length == 4 ) {
  4040. _color1.r = _color2.r = _color3.r = _color4.r = _ambientLight.r;
  4041. _color1.g = _color2.g = _color3.g = _color4.g = _ambientLight.g;
  4042. _color1.b = _color2.b = _color3.b = _color4.b = _ambientLight.b;
  4043. calculateLight( _lights, element.v1.positionWorld, element.vertexNormalsWorld[ 0 ], _color1 );
  4044. calculateLight( _lights, element.v2.positionWorld, element.vertexNormalsWorld[ 1 ], _color2 );
  4045. calculateLight( _lights, element.v4.positionWorld, element.vertexNormalsWorld[ 3 ], _color3 );
  4046. calculateLight( _lights, element.v3.positionWorld, element.vertexNormalsWorld[ 2 ], _color4 );
  4047. _color1.r = Math.max( 0, Math.min( material.color.r * _color1.r, 1 ) );
  4048. _color1.g = Math.max( 0, Math.min( material.color.g * _color1.g, 1 ) );
  4049. _color1.b = Math.max( 0, Math.min( material.color.b * _color1.b, 1 ) );
  4050. _color2.r = Math.max( 0, Math.min( material.color.r * _color2.r, 1 ) );
  4051. _color2.g = Math.max( 0, Math.min( material.color.g * _color2.g, 1 ) );
  4052. _color2.b = Math.max( 0, Math.min( material.color.b * _color2.b, 1 ) );
  4053. _color3.r = Math.max( 0, Math.min( material.color.r * _color3.r, 1 ) );
  4054. _color3.g = Math.max( 0, Math.min( material.color.g * _color3.g, 1 ) );
  4055. _color3.b = Math.max( 0, Math.min( material.color.b * _color3.b, 1 ) );
  4056. _color4.r = Math.max( 0, Math.min( material.color.r * _color4.r, 1 ) );
  4057. _color4.g = Math.max( 0, Math.min( material.color.g * _color4.g, 1 ) );
  4058. _color4.b = Math.max( 0, Math.min( material.color.b * _color4.b, 1 ) );
  4059. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  4060. // TODO: UVs are incorrect, v4->v3?
  4061. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y );
  4062. clipImage( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, 0, 0, 1, 0, 0, 1, _image );
  4063. drawTriangle( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y );
  4064. clipImage( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, 1, 0, 1, 1, 0, 1, _image );
  4065. } else {
  4066. _color.r = _ambientLight.r;
  4067. _color.g = _ambientLight.g;
  4068. _color.b = _ambientLight.b;
  4069. calculateLight( _lights, element.centroidWorld, element.normalWorld, _color );
  4070. _color.r = Math.max( 0, Math.min( material.color.r * _color.r, 1 ) );
  4071. _color.g = Math.max( 0, Math.min( material.color.g * _color.g, 1 ) );
  4072. _color.b = Math.max( 0, Math.min( material.color.b * _color.b, 1 ) );
  4073. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  4074. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  4075. }
  4076. } else {
  4077. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  4078. material.wireframe ? strokePath( material.color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( material.color );
  4079. }
  4080. } else if ( material instanceof THREE.MeshNormalMaterial ) {
  4081. _color.r = normalToComponent( element.normalWorld.x );
  4082. _color.g = normalToComponent( element.normalWorld.y );
  4083. _color.b = normalToComponent( element.normalWorld.z );
  4084. drawQuad( _v1x, _v1y, _v2x, _v2y, _v3x, _v3y, _v4x, _v4y );
  4085. material.wireframe ? strokePath( _color, material.wireframeLinewidth, material.wireframeLinecap, material.wireframeLinejoin ) : fillPath( _color );
  4086. } else if ( material instanceof THREE.MeshDepthMaterial ) {
  4087. _near = camera.near;
  4088. _far = camera.far;
  4089. _color1.r = _color1.g = _color1.b = 1 - smoothstep( v1.positionScreen.z, _near, _far );
  4090. _color2.r = _color2.g = _color2.b = 1 - smoothstep( v2.positionScreen.z, _near, _far );
  4091. _color3.r = _color3.g = _color3.b = 1 - smoothstep( v4.positionScreen.z, _near, _far );
  4092. _color4.r = _color4.g = _color4.b = 1 - smoothstep( v3.positionScreen.z, _near, _far );
  4093. _image = getGradientTexture( _color1, _color2, _color3, _color4 );
  4094. // TODO: UVs are incorrect, v4->v3?
  4095. drawTriangle( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y );
  4096. clipImage( _v1x, _v1y, _v2x, _v2y, _v4x, _v4y, 0, 0, 1, 0, 0, 1, _image );
  4097. drawTriangle( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y );
  4098. clipImage( _v5x, _v5y, _v3x, _v3y, _v6x, _v6y, 1, 0, 1, 1, 0, 1, _image );
  4099. }
  4100. }
  4101. //
  4102. function drawTriangle( x0, y0, x1, y1, x2, y2 ) {
  4103. _context.beginPath();
  4104. _context.moveTo( x0, y0 );
  4105. _context.lineTo( x1, y1 );
  4106. _context.lineTo( x2, y2 );
  4107. _context.lineTo( x0, y0 );
  4108. _context.closePath();
  4109. }
  4110. function drawQuad( x0, y0, x1, y1, x2, y2, x3, y3 ) {
  4111. _context.beginPath();
  4112. _context.moveTo( x0, y0 );
  4113. _context.lineTo( x1, y1 );
  4114. _context.lineTo( x2, y2 );
  4115. _context.lineTo( x3, y3 );
  4116. _context.lineTo( x0, y0 );
  4117. _context.closePath();
  4118. }
  4119. function strokePath( color, linewidth, linecap, linejoin ) {
  4120. setLineWidth( linewidth );
  4121. setLineCap( linecap );
  4122. setLineJoin( linejoin );
  4123. setStrokeStyle( color.getContextStyle() );
  4124. _context.stroke();
  4125. _bboxRect.inflate( linewidth * 2 );
  4126. }
  4127. function fillPath( color ) {
  4128. setFillStyle( color.getContextStyle() );
  4129. _context.fill();
  4130. }
  4131. function patternPath( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, texture ) {
  4132. if ( texture.image.width == 0 ) return;
  4133. if ( texture.needsUpdate == true || _patterns[ texture.id ] == undefined ) {
  4134. var repeatX = texture.wrapS == THREE.RepeatWrapping;
  4135. var repeatY = texture.wrapT == THREE.RepeatWrapping;
  4136. _patterns[ texture.id ] = _context.createPattern( texture.image, repeatX && repeatY ? 'repeat' : repeatX && !repeatY ? 'repeat-x' : !repeatX && repeatY ? 'repeat-y' : 'no-repeat' );
  4137. texture.needsUpdate = false;
  4138. }
  4139. setFillStyle( _patterns[ texture.id ] );
  4140. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  4141. var a, b, c, d, e, f, det, idet,
  4142. offsetX = texture.offset.x / texture.repeat.x,
  4143. offsetY = texture.offset.y / texture.repeat.y,
  4144. width = texture.image.width * texture.repeat.x,
  4145. height = texture.image.height * texture.repeat.y;
  4146. u0 = ( u0 + offsetX ) * width;
  4147. v0 = ( v0 + offsetY ) * height;
  4148. u1 = ( u1 + offsetX ) * width;
  4149. v1 = ( v1 + offsetY ) * height;
  4150. u2 = ( u2 + offsetX ) * width;
  4151. v2 = ( v2 + offsetY ) * height;
  4152. x1 -= x0; y1 -= y0;
  4153. x2 -= x0; y2 -= y0;
  4154. u1 -= u0; v1 -= v0;
  4155. u2 -= u0; v2 -= v0;
  4156. det = u1 * v2 - u2 * v1;
  4157. if ( det == 0 ) {
  4158. if ( _imagedatas[ texture.id ] === undefined ) {
  4159. var canvas = document.createElement( 'canvas' )
  4160. canvas.width = texture.image.width;
  4161. canvas.height = texture.image.height;
  4162. var context = canvas.getContext( '2d' );
  4163. context.drawImage( texture.image, 0, 0 );
  4164. _imagedatas[ texture.id ] = context.getImageData( 0, 0, texture.image.width, texture.image.height ).data;
  4165. // variables cannot be deleted in ES5 strict mode
  4166. //delete canvas;
  4167. }
  4168. var data = _imagedatas[ texture.id ];
  4169. var index = ( Math.floor( u0 ) + Math.floor( v0 ) * texture.image.width ) * 4;
  4170. _color.setRGB( data[ index ] / 255, data[ index + 1 ] / 255, data[ index + 2 ] / 255 );
  4171. fillPath( _color );
  4172. return;
  4173. }
  4174. idet = 1 / det;
  4175. a = ( v2 * x1 - v1 * x2 ) * idet;
  4176. b = ( v2 * y1 - v1 * y2 ) * idet;
  4177. c = ( u1 * x2 - u2 * x1 ) * idet;
  4178. d = ( u1 * y2 - u2 * y1 ) * idet;
  4179. e = x0 - a * u0 - c * v0;
  4180. f = y0 - b * u0 - d * v0;
  4181. _context.save();
  4182. _context.transform( a, b, c, d, e, f );
  4183. _context.fill();
  4184. _context.restore();
  4185. }
  4186. function clipImage( x0, y0, x1, y1, x2, y2, u0, v0, u1, v1, u2, v2, image ) {
  4187. // http://extremelysatisfactorytotalitarianism.com/blog/?p=2120
  4188. var a, b, c, d, e, f, det, idet,
  4189. width = image.width - 1,
  4190. height = image.height - 1;
  4191. u0 *= width; v0 *= height;
  4192. u1 *= width; v1 *= height;
  4193. u2 *= width; v2 *= height;
  4194. x1 -= x0; y1 -= y0;
  4195. x2 -= x0; y2 -= y0;
  4196. u1 -= u0; v1 -= v0;
  4197. u2 -= u0; v2 -= v0;
  4198. det = u1 * v2 - u2 * v1;
  4199. idet = 1 / det;
  4200. a = ( v2 * x1 - v1 * x2 ) * idet;
  4201. b = ( v2 * y1 - v1 * y2 ) * idet;
  4202. c = ( u1 * x2 - u2 * x1 ) * idet;
  4203. d = ( u1 * y2 - u2 * y1 ) * idet;
  4204. e = x0 - a * u0 - c * v0;
  4205. f = y0 - b * u0 - d * v0;
  4206. _context.save();
  4207. _context.transform( a, b, c, d, e, f );
  4208. _context.clip();
  4209. _context.drawImage( image, 0, 0 );
  4210. _context.restore();
  4211. }
  4212. function getGradientTexture( color1, color2, color3, color4 ) {
  4213. // http://mrdoob.com/blog/post/710
  4214. var c1r = ~~ ( color1.r * 255 ), c1g = ~~ ( color1.g * 255 ), c1b = ~~ ( color1.b * 255 ),
  4215. c2r = ~~ ( color2.r * 255 ), c2g = ~~ ( color2.g * 255 ), c2b = ~~ ( color2.b * 255 ),
  4216. c3r = ~~ ( color3.r * 255 ), c3g = ~~ ( color3.g * 255 ), c3b = ~~ ( color3.b * 255 ),
  4217. c4r = ~~ ( color4.r * 255 ), c4g = ~~ ( color4.g * 255 ), c4b = ~~ ( color4.b * 255 );
  4218. _pixelMapData[ 0 ] = c1r < 0 ? 0 : c1r > 255 ? 255 : c1r;
  4219. _pixelMapData[ 1 ] = c1g < 0 ? 0 : c1g > 255 ? 255 : c1g;
  4220. _pixelMapData[ 2 ] = c1b < 0 ? 0 : c1b > 255 ? 255 : c1b;
  4221. _pixelMapData[ 4 ] = c2r < 0 ? 0 : c2r > 255 ? 255 : c2r;
  4222. _pixelMapData[ 5 ] = c2g < 0 ? 0 : c2g > 255 ? 255 : c2g;
  4223. _pixelMapData[ 6 ] = c2b < 0 ? 0 : c2b > 255 ? 255 : c2b;
  4224. _pixelMapData[ 8 ] = c3r < 0 ? 0 : c3r > 255 ? 255 : c3r;
  4225. _pixelMapData[ 9 ] = c3g < 0 ? 0 : c3g > 255 ? 255 : c3g;
  4226. _pixelMapData[ 10 ] = c3b < 0 ? 0 : c3b > 255 ? 255 : c3b;
  4227. _pixelMapData[ 12 ] = c4r < 0 ? 0 : c4r > 255 ? 255 : c4r;
  4228. _pixelMapData[ 13 ] = c4g < 0 ? 0 : c4g > 255 ? 255 : c4g;
  4229. _pixelMapData[ 14 ] = c4b < 0 ? 0 : c4b > 255 ? 255 : c4b;
  4230. _pixelMapContext.putImageData( _pixelMapImage, 0, 0 );
  4231. _gradientMapContext.drawImage( _pixelMap, 0, 0 );
  4232. return _gradientMap;
  4233. }
  4234. function smoothstep( value, min, max ) {
  4235. var x = ( value - min ) / ( max - min );
  4236. return x * x * ( 3 - 2 * x );
  4237. }
  4238. function normalToComponent( normal ) {
  4239. var component = ( normal + 1 ) * 0.5;
  4240. return component < 0 ? 0 : ( component > 1 ? 1 : component );
  4241. }
  4242. // Hide anti-alias gaps
  4243. function expand( v1, v2 ) {
  4244. var x = v2.x - v1.x, y = v2.y - v1.y,
  4245. det = x * x + y * y, idet;
  4246. if ( det == 0 ) return;
  4247. idet = 1 / Math.sqrt( det );
  4248. x *= idet; y *= idet;
  4249. v2.x += x; v2.y += y;
  4250. v1.x -= x; v1.y -= y;
  4251. }
  4252. };
  4253. // Context cached methods.
  4254. function setOpacity( value ) {
  4255. if ( _contextGlobalAlpha != value ) {
  4256. _context.globalAlpha = _contextGlobalAlpha = value;
  4257. }
  4258. }
  4259. function setBlending( value ) {
  4260. if ( _contextGlobalCompositeOperation != value ) {
  4261. switch ( value ) {
  4262. case THREE.NormalBlending:
  4263. _context.globalCompositeOperation = 'source-over';
  4264. break;
  4265. case THREE.AdditiveBlending:
  4266. _context.globalCompositeOperation = 'lighter';
  4267. break;
  4268. case THREE.SubtractiveBlending:
  4269. _context.globalCompositeOperation = 'darker';
  4270. break;
  4271. }
  4272. _contextGlobalCompositeOperation = value;
  4273. }
  4274. }
  4275. function setLineWidth( value ) {
  4276. if ( _contextLineWidth != value ) {
  4277. _context.lineWidth = _contextLineWidth = value;
  4278. }
  4279. }
  4280. function setLineCap( value ) {
  4281. // "butt", "round", "square"
  4282. if ( _contextLineCap != value ) {
  4283. _context.lineCap = _contextLineCap = value;
  4284. }
  4285. }
  4286. function setLineJoin( value ) {
  4287. // "round", "bevel", "miter"
  4288. if ( _contextLineJoin != value ) {
  4289. _context.lineJoin = _contextLineJoin = value;
  4290. }
  4291. }
  4292. function setStrokeStyle( style ) {
  4293. if ( _contextStrokeStyle != style ) {
  4294. _context.strokeStyle = _contextStrokeStyle = style;
  4295. }
  4296. }
  4297. function setFillStyle( style ) {
  4298. if ( _contextFillStyle != style ) {
  4299. _context.fillStyle = _contextFillStyle = style;
  4300. }
  4301. }
  4302. };
  4303. /**
  4304. * @author mr.doob / http://mrdoob.com/
  4305. */
  4306. THREE.RenderableVertex = function () {
  4307. this.positionWorld = new THREE.Vector3();
  4308. this.positionScreen = new THREE.Vector4();
  4309. this.visible = true;
  4310. };
  4311. THREE.RenderableVertex.prototype.copy = function ( vertex ) {
  4312. this.positionWorld.copy( vertex.positionWorld );
  4313. this.positionScreen.copy( vertex.positionScreen );
  4314. }
  4315. /**
  4316. * @author mr.doob / http://mrdoob.com/
  4317. */
  4318. THREE.RenderableFace3 = function () {
  4319. this.v1 = new THREE.RenderableVertex();
  4320. this.v2 = new THREE.RenderableVertex();
  4321. this.v3 = new THREE.RenderableVertex();
  4322. this.centroidWorld = new THREE.Vector3();
  4323. this.centroidScreen = new THREE.Vector3();
  4324. this.normalWorld = new THREE.Vector3();
  4325. this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  4326. this.material = null;
  4327. this.faceMaterial = null;
  4328. this.uvs = [[]];
  4329. this.z = null;
  4330. };
  4331. /**
  4332. * @author mr.doob / http://mrdoob.com/
  4333. */
  4334. THREE.RenderableFace4 = function () {
  4335. this.v1 = new THREE.RenderableVertex();
  4336. this.v2 = new THREE.RenderableVertex();
  4337. this.v3 = new THREE.RenderableVertex();
  4338. this.v4 = new THREE.RenderableVertex();
  4339. this.centroidWorld = new THREE.Vector3();
  4340. this.centroidScreen = new THREE.Vector3();
  4341. this.normalWorld = new THREE.Vector3();
  4342. this.vertexNormalsWorld = [ new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3(), new THREE.Vector3() ];
  4343. this.material = null;
  4344. this.faceMaterial = null;
  4345. this.uvs = [[]];
  4346. this.z = null;
  4347. };
  4348. /**
  4349. * @author mr.doob / http://mrdoob.com/
  4350. */
  4351. THREE.RenderableObject = function () {
  4352. this.object = null;
  4353. this.z = null;
  4354. };
  4355. /**
  4356. * @author mr.doob / http://mrdoob.com/
  4357. */
  4358. THREE.RenderableParticle = function () {
  4359. this.x = null;
  4360. this.y = null;
  4361. this.z = null;
  4362. this.rotation = null;
  4363. this.scale = new THREE.Vector2();
  4364. this.material = null;
  4365. };
  4366. /**
  4367. * @author mr.doob / http://mrdoob.com/
  4368. */
  4369. THREE.RenderableLine = function () {
  4370. this.z = null;
  4371. this.v1 = new THREE.RenderableVertex();
  4372. this.v2 = new THREE.RenderableVertex();
  4373. this.material = null;
  4374. };
粤ICP备19079148号