three-math.js 126 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848
  1. // File:src/Three.js
  2. /**
  3. * @author mrdoob / http://mrdoob.com/
  4. */
  5. var THREE = { REVISION: '69dev' };
  6. // browserify support
  7. if ( typeof module === 'object' ) {
  8. module.exports = THREE;
  9. }
  10. // GL STATE CONSTANTS
  11. THREE.CullFaceNone = 0;
  12. THREE.CullFaceBack = 1;
  13. THREE.CullFaceFront = 2;
  14. THREE.CullFaceFrontBack = 3;
  15. THREE.FrontFaceDirectionCW = 0;
  16. THREE.FrontFaceDirectionCCW = 1;
  17. // SHADOWING TYPES
  18. THREE.BasicShadowMap = 0;
  19. THREE.PCFShadowMap = 1;
  20. THREE.PCFSoftShadowMap = 2;
  21. // MATERIAL CONSTANTS
  22. // side
  23. THREE.FrontSide = 0;
  24. THREE.BackSide = 1;
  25. THREE.DoubleSide = 2;
  26. // shading
  27. THREE.NoShading = 0;
  28. THREE.FlatShading = 1;
  29. THREE.SmoothShading = 2;
  30. // colors
  31. THREE.NoColors = 0;
  32. THREE.FaceColors = 1;
  33. THREE.VertexColors = 2;
  34. // blending modes
  35. THREE.NoBlending = 0;
  36. THREE.NormalBlending = 1;
  37. THREE.AdditiveBlending = 2;
  38. THREE.SubtractiveBlending = 3;
  39. THREE.MultiplyBlending = 4;
  40. THREE.CustomBlending = 5;
  41. // custom blending equations
  42. // (numbers start from 100 not to clash with other
  43. // mappings to OpenGL constants defined in Texture.js)
  44. THREE.AddEquation = 100;
  45. THREE.SubtractEquation = 101;
  46. THREE.ReverseSubtractEquation = 102;
  47. // custom blending destination factors
  48. THREE.ZeroFactor = 200;
  49. THREE.OneFactor = 201;
  50. THREE.SrcColorFactor = 202;
  51. THREE.OneMinusSrcColorFactor = 203;
  52. THREE.SrcAlphaFactor = 204;
  53. THREE.OneMinusSrcAlphaFactor = 205;
  54. THREE.DstAlphaFactor = 206;
  55. THREE.OneMinusDstAlphaFactor = 207;
  56. // custom blending source factors
  57. //THREE.ZeroFactor = 200;
  58. //THREE.OneFactor = 201;
  59. //THREE.SrcAlphaFactor = 204;
  60. //THREE.OneMinusSrcAlphaFactor = 205;
  61. //THREE.DstAlphaFactor = 206;
  62. //THREE.OneMinusDstAlphaFactor = 207;
  63. THREE.DstColorFactor = 208;
  64. THREE.OneMinusDstColorFactor = 209;
  65. THREE.SrcAlphaSaturateFactor = 210;
  66. // TEXTURE CONSTANTS
  67. THREE.MultiplyOperation = 0;
  68. THREE.MixOperation = 1;
  69. THREE.AddOperation = 2;
  70. // Mapping modes
  71. THREE.UVMapping = function () {};
  72. THREE.CubeReflectionMapping = function () {};
  73. THREE.CubeRefractionMapping = function () {};
  74. THREE.SphericalReflectionMapping = function () {};
  75. THREE.SphericalRefractionMapping = function () {};
  76. // Wrapping modes
  77. THREE.RepeatWrapping = 1000;
  78. THREE.ClampToEdgeWrapping = 1001;
  79. THREE.MirroredRepeatWrapping = 1002;
  80. // Filters
  81. THREE.NearestFilter = 1003;
  82. THREE.NearestMipMapNearestFilter = 1004;
  83. THREE.NearestMipMapLinearFilter = 1005;
  84. THREE.LinearFilter = 1006;
  85. THREE.LinearMipMapNearestFilter = 1007;
  86. THREE.LinearMipMapLinearFilter = 1008;
  87. // Data types
  88. THREE.UnsignedByteType = 1009;
  89. THREE.ByteType = 1010;
  90. THREE.ShortType = 1011;
  91. THREE.UnsignedShortType = 1012;
  92. THREE.IntType = 1013;
  93. THREE.UnsignedIntType = 1014;
  94. THREE.FloatType = 1015;
  95. // Pixel types
  96. //THREE.UnsignedByteType = 1009;
  97. THREE.UnsignedShort4444Type = 1016;
  98. THREE.UnsignedShort5551Type = 1017;
  99. THREE.UnsignedShort565Type = 1018;
  100. // Pixel formats
  101. THREE.AlphaFormat = 1019;
  102. THREE.RGBFormat = 1020;
  103. THREE.RGBAFormat = 1021;
  104. THREE.LuminanceFormat = 1022;
  105. THREE.LuminanceAlphaFormat = 1023;
  106. // Compressed texture formats
  107. THREE.RGB_S3TC_DXT1_Format = 2001;
  108. THREE.RGBA_S3TC_DXT1_Format = 2002;
  109. THREE.RGBA_S3TC_DXT3_Format = 2003;
  110. THREE.RGBA_S3TC_DXT5_Format = 2004;
  111. /*
  112. // Potential future PVRTC compressed texture formats
  113. THREE.RGB_PVRTC_4BPPV1_Format = 2100;
  114. THREE.RGB_PVRTC_2BPPV1_Format = 2101;
  115. THREE.RGBA_PVRTC_4BPPV1_Format = 2102;
  116. THREE.RGBA_PVRTC_2BPPV1_Format = 2103;
  117. */
  118. // File:src/math/Color.js
  119. /**
  120. * @author mrdoob / http://mrdoob.com/
  121. */
  122. THREE.Color = function ( color ) {
  123. if ( arguments.length === 3 ) {
  124. return this.setRGB( arguments[ 0 ], arguments[ 1 ], arguments[ 2 ] );
  125. }
  126. return this.set( color )
  127. };
  128. THREE.Color.prototype = {
  129. constructor: THREE.Color,
  130. r: 1, g: 1, b: 1,
  131. set: function ( value ) {
  132. if ( value instanceof THREE.Color ) {
  133. this.copy( value );
  134. } else if ( typeof value === 'number' ) {
  135. this.setHex( value );
  136. } else if ( typeof value === 'string' ) {
  137. this.setStyle( value );
  138. }
  139. return this;
  140. },
  141. setHex: function ( hex ) {
  142. hex = Math.floor( hex );
  143. this.r = ( hex >> 16 & 255 ) / 255;
  144. this.g = ( hex >> 8 & 255 ) / 255;
  145. this.b = ( hex & 255 ) / 255;
  146. return this;
  147. },
  148. setRGB: function ( r, g, b ) {
  149. this.r = r;
  150. this.g = g;
  151. this.b = b;
  152. return this;
  153. },
  154. setHSL: function ( h, s, l ) {
  155. // h,s,l ranges are in 0.0 - 1.0
  156. if ( s === 0 ) {
  157. this.r = this.g = this.b = l;
  158. } else {
  159. var hue2rgb = function ( p, q, t ) {
  160. if ( t < 0 ) t += 1;
  161. if ( t > 1 ) t -= 1;
  162. if ( t < 1 / 6 ) return p + ( q - p ) * 6 * t;
  163. if ( t < 1 / 2 ) return q;
  164. if ( t < 2 / 3 ) return p + ( q - p ) * 6 * ( 2 / 3 - t );
  165. return p;
  166. };
  167. var p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s );
  168. var q = ( 2 * l ) - p;
  169. this.r = hue2rgb( q, p, h + 1 / 3 );
  170. this.g = hue2rgb( q, p, h );
  171. this.b = hue2rgb( q, p, h - 1 / 3 );
  172. }
  173. return this;
  174. },
  175. setStyle: function ( style ) {
  176. // rgb(255,0,0)
  177. if ( /^rgb\((\d+), ?(\d+), ?(\d+)\)$/i.test( style ) ) {
  178. var color = /^rgb\((\d+), ?(\d+), ?(\d+)\)$/i.exec( style );
  179. this.r = Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255;
  180. this.g = Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255;
  181. this.b = Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255;
  182. return this;
  183. }
  184. // rgb(100%,0%,0%)
  185. if ( /^rgb\((\d+)\%, ?(\d+)\%, ?(\d+)\%\)$/i.test( style ) ) {
  186. var color = /^rgb\((\d+)\%, ?(\d+)\%, ?(\d+)\%\)$/i.exec( style );
  187. this.r = Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100;
  188. this.g = Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100;
  189. this.b = Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100;
  190. return this;
  191. }
  192. // #ff0000
  193. if ( /^\#([0-9a-f]{6})$/i.test( style ) ) {
  194. var color = /^\#([0-9a-f]{6})$/i.exec( style );
  195. this.setHex( parseInt( color[ 1 ], 16 ) );
  196. return this;
  197. }
  198. // #f00
  199. if ( /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i.test( style ) ) {
  200. var color = /^\#([0-9a-f])([0-9a-f])([0-9a-f])$/i.exec( style );
  201. this.setHex( parseInt( color[ 1 ] + color[ 1 ] + color[ 2 ] + color[ 2 ] + color[ 3 ] + color[ 3 ], 16 ) );
  202. return this;
  203. }
  204. // red
  205. if ( /^(\w+)$/i.test( style ) ) {
  206. this.setHex( THREE.ColorKeywords[ style ] );
  207. return this;
  208. }
  209. },
  210. copy: function ( color ) {
  211. this.r = color.r;
  212. this.g = color.g;
  213. this.b = color.b;
  214. return this;
  215. },
  216. copyGammaToLinear: function ( color ) {
  217. this.r = color.r * color.r;
  218. this.g = color.g * color.g;
  219. this.b = color.b * color.b;
  220. return this;
  221. },
  222. copyLinearToGamma: function ( color ) {
  223. this.r = Math.sqrt( color.r );
  224. this.g = Math.sqrt( color.g );
  225. this.b = Math.sqrt( color.b );
  226. return this;
  227. },
  228. convertGammaToLinear: function () {
  229. var r = this.r, g = this.g, b = this.b;
  230. this.r = r * r;
  231. this.g = g * g;
  232. this.b = b * b;
  233. return this;
  234. },
  235. convertLinearToGamma: function () {
  236. this.r = Math.sqrt( this.r );
  237. this.g = Math.sqrt( this.g );
  238. this.b = Math.sqrt( this.b );
  239. return this;
  240. },
  241. getHex: function () {
  242. return ( this.r * 255 ) << 16 ^ ( this.g * 255 ) << 8 ^ ( this.b * 255 ) << 0;
  243. },
  244. getHexString: function () {
  245. return ( '000000' + this.getHex().toString( 16 ) ).slice( - 6 );
  246. },
  247. getHSL: function ( optionalTarget ) {
  248. // h,s,l ranges are in 0.0 - 1.0
  249. var hsl = optionalTarget || { h: 0, s: 0, l: 0 };
  250. var r = this.r, g = this.g, b = this.b;
  251. var max = Math.max( r, g, b );
  252. var min = Math.min( r, g, b );
  253. var hue, saturation;
  254. var lightness = ( min + max ) / 2.0;
  255. if ( min === max ) {
  256. hue = 0;
  257. saturation = 0;
  258. } else {
  259. var delta = max - min;
  260. saturation = lightness <= 0.5 ? delta / ( max + min ) : delta / ( 2 - max - min );
  261. switch ( max ) {
  262. case r: hue = ( g - b ) / delta + ( g < b ? 6 : 0 ); break;
  263. case g: hue = ( b - r ) / delta + 2; break;
  264. case b: hue = ( r - g ) / delta + 4; break;
  265. }
  266. hue /= 6;
  267. }
  268. hsl.h = hue;
  269. hsl.s = saturation;
  270. hsl.l = lightness;
  271. return hsl;
  272. },
  273. getStyle: function () {
  274. return 'rgb(' + ( ( this.r * 255 ) | 0 ) + ',' + ( ( this.g * 255 ) | 0 ) + ',' + ( ( this.b * 255 ) | 0 ) + ')';
  275. },
  276. offsetHSL: function ( h, s, l ) {
  277. var hsl = this.getHSL();
  278. hsl.h += h; hsl.s += s; hsl.l += l;
  279. this.setHSL( hsl.h, hsl.s, hsl.l );
  280. return this;
  281. },
  282. add: function ( color ) {
  283. this.r += color.r;
  284. this.g += color.g;
  285. this.b += color.b;
  286. return this;
  287. },
  288. addColors: function ( color1, color2 ) {
  289. this.r = color1.r + color2.r;
  290. this.g = color1.g + color2.g;
  291. this.b = color1.b + color2.b;
  292. return this;
  293. },
  294. addScalar: function ( s ) {
  295. this.r += s;
  296. this.g += s;
  297. this.b += s;
  298. return this;
  299. },
  300. multiply: function ( color ) {
  301. this.r *= color.r;
  302. this.g *= color.g;
  303. this.b *= color.b;
  304. return this;
  305. },
  306. multiplyScalar: function ( s ) {
  307. this.r *= s;
  308. this.g *= s;
  309. this.b *= s;
  310. return this;
  311. },
  312. lerp: function ( color, alpha ) {
  313. this.r += ( color.r - this.r ) * alpha;
  314. this.g += ( color.g - this.g ) * alpha;
  315. this.b += ( color.b - this.b ) * alpha;
  316. return this;
  317. },
  318. equals: function ( c ) {
  319. return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b );
  320. },
  321. fromArray: function ( array ) {
  322. this.r = array[ 0 ];
  323. this.g = array[ 1 ];
  324. this.b = array[ 2 ];
  325. return this;
  326. },
  327. toArray: function () {
  328. return [ this.r, this.g, this.b ];
  329. },
  330. clone: function () {
  331. return new THREE.Color().setRGB( this.r, this.g, this.b );
  332. }
  333. };
  334. THREE.ColorKeywords = { 'aliceblue': 0xF0F8FF, 'antiquewhite': 0xFAEBD7, 'aqua': 0x00FFFF, 'aquamarine': 0x7FFFD4, 'azure': 0xF0FFFF,
  335. 'beige': 0xF5F5DC, 'bisque': 0xFFE4C4, 'black': 0x000000, 'blanchedalmond': 0xFFEBCD, 'blue': 0x0000FF, 'blueviolet': 0x8A2BE2,
  336. 'brown': 0xA52A2A, 'burlywood': 0xDEB887, 'cadetblue': 0x5F9EA0, 'chartreuse': 0x7FFF00, 'chocolate': 0xD2691E, 'coral': 0xFF7F50,
  337. 'cornflowerblue': 0x6495ED, 'cornsilk': 0xFFF8DC, 'crimson': 0xDC143C, 'cyan': 0x00FFFF, 'darkblue': 0x00008B, 'darkcyan': 0x008B8B,
  338. 'darkgoldenrod': 0xB8860B, 'darkgray': 0xA9A9A9, 'darkgreen': 0x006400, 'darkgrey': 0xA9A9A9, 'darkkhaki': 0xBDB76B, 'darkmagenta': 0x8B008B,
  339. 'darkolivegreen': 0x556B2F, 'darkorange': 0xFF8C00, 'darkorchid': 0x9932CC, 'darkred': 0x8B0000, 'darksalmon': 0xE9967A, 'darkseagreen': 0x8FBC8F,
  340. 'darkslateblue': 0x483D8B, 'darkslategray': 0x2F4F4F, 'darkslategrey': 0x2F4F4F, 'darkturquoise': 0x00CED1, 'darkviolet': 0x9400D3,
  341. 'deeppink': 0xFF1493, 'deepskyblue': 0x00BFFF, 'dimgray': 0x696969, 'dimgrey': 0x696969, 'dodgerblue': 0x1E90FF, 'firebrick': 0xB22222,
  342. 'floralwhite': 0xFFFAF0, 'forestgreen': 0x228B22, 'fuchsia': 0xFF00FF, 'gainsboro': 0xDCDCDC, 'ghostwhite': 0xF8F8FF, 'gold': 0xFFD700,
  343. 'goldenrod': 0xDAA520, 'gray': 0x808080, 'green': 0x008000, 'greenyellow': 0xADFF2F, 'grey': 0x808080, 'honeydew': 0xF0FFF0, 'hotpink': 0xFF69B4,
  344. 'indianred': 0xCD5C5C, 'indigo': 0x4B0082, 'ivory': 0xFFFFF0, 'khaki': 0xF0E68C, 'lavender': 0xE6E6FA, 'lavenderblush': 0xFFF0F5, 'lawngreen': 0x7CFC00,
  345. 'lemonchiffon': 0xFFFACD, 'lightblue': 0xADD8E6, 'lightcoral': 0xF08080, 'lightcyan': 0xE0FFFF, 'lightgoldenrodyellow': 0xFAFAD2, 'lightgray': 0xD3D3D3,
  346. 'lightgreen': 0x90EE90, 'lightgrey': 0xD3D3D3, 'lightpink': 0xFFB6C1, 'lightsalmon': 0xFFA07A, 'lightseagreen': 0x20B2AA, 'lightskyblue': 0x87CEFA,
  347. 'lightslategray': 0x778899, 'lightslategrey': 0x778899, 'lightsteelblue': 0xB0C4DE, 'lightyellow': 0xFFFFE0, 'lime': 0x00FF00, 'limegreen': 0x32CD32,
  348. 'linen': 0xFAF0E6, 'magenta': 0xFF00FF, 'maroon': 0x800000, 'mediumaquamarine': 0x66CDAA, 'mediumblue': 0x0000CD, 'mediumorchid': 0xBA55D3,
  349. 'mediumpurple': 0x9370DB, 'mediumseagreen': 0x3CB371, 'mediumslateblue': 0x7B68EE, 'mediumspringgreen': 0x00FA9A, 'mediumturquoise': 0x48D1CC,
  350. 'mediumvioletred': 0xC71585, 'midnightblue': 0x191970, 'mintcream': 0xF5FFFA, 'mistyrose': 0xFFE4E1, 'moccasin': 0xFFE4B5, 'navajowhite': 0xFFDEAD,
  351. 'navy': 0x000080, 'oldlace': 0xFDF5E6, 'olive': 0x808000, 'olivedrab': 0x6B8E23, 'orange': 0xFFA500, 'orangered': 0xFF4500, 'orchid': 0xDA70D6,
  352. 'palegoldenrod': 0xEEE8AA, 'palegreen': 0x98FB98, 'paleturquoise': 0xAFEEEE, 'palevioletred': 0xDB7093, 'papayawhip': 0xFFEFD5, 'peachpuff': 0xFFDAB9,
  353. 'peru': 0xCD853F, 'pink': 0xFFC0CB, 'plum': 0xDDA0DD, 'powderblue': 0xB0E0E6, 'purple': 0x800080, 'red': 0xFF0000, 'rosybrown': 0xBC8F8F,
  354. 'royalblue': 0x4169E1, 'saddlebrown': 0x8B4513, 'salmon': 0xFA8072, 'sandybrown': 0xF4A460, 'seagreen': 0x2E8B57, 'seashell': 0xFFF5EE,
  355. 'sienna': 0xA0522D, 'silver': 0xC0C0C0, 'skyblue': 0x87CEEB, 'slateblue': 0x6A5ACD, 'slategray': 0x708090, 'slategrey': 0x708090, 'snow': 0xFFFAFA,
  356. 'springgreen': 0x00FF7F, 'steelblue': 0x4682B4, 'tan': 0xD2B48C, 'teal': 0x008080, 'thistle': 0xD8BFD8, 'tomato': 0xFF6347, 'turquoise': 0x40E0D0,
  357. 'violet': 0xEE82EE, 'wheat': 0xF5DEB3, 'white': 0xFFFFFF, 'whitesmoke': 0xF5F5F5, 'yellow': 0xFFFF00, 'yellowgreen': 0x9ACD32 };
  358. // File:src/math/Quaternion.js
  359. /**
  360. * @author mikael emtinger / http://gomo.se/
  361. * @author alteredq / http://alteredqualia.com/
  362. * @author WestLangley / http://github.com/WestLangley
  363. * @author bhouston / http://exocortex.com
  364. */
  365. THREE.Quaternion = function ( x, y, z, w ) {
  366. this._x = x || 0;
  367. this._y = y || 0;
  368. this._z = z || 0;
  369. this._w = ( w !== undefined ) ? w : 1;
  370. };
  371. THREE.Quaternion.prototype = {
  372. constructor: THREE.Quaternion,
  373. _x: 0,_y: 0, _z: 0, _w: 0,
  374. get x () {
  375. return this._x;
  376. },
  377. set x ( value ) {
  378. this._x = value;
  379. this.onChangeCallback();
  380. },
  381. get y () {
  382. return this._y;
  383. },
  384. set y ( value ) {
  385. this._y = value;
  386. this.onChangeCallback();
  387. },
  388. get z () {
  389. return this._z;
  390. },
  391. set z ( value ) {
  392. this._z = value;
  393. this.onChangeCallback();
  394. },
  395. get w () {
  396. return this._w;
  397. },
  398. set w ( value ) {
  399. this._w = value;
  400. this.onChangeCallback();
  401. },
  402. set: function ( x, y, z, w ) {
  403. this._x = x;
  404. this._y = y;
  405. this._z = z;
  406. this._w = w;
  407. this.onChangeCallback();
  408. return this;
  409. },
  410. copy: function ( quaternion ) {
  411. this._x = quaternion.x;
  412. this._y = quaternion.y;
  413. this._z = quaternion.z;
  414. this._w = quaternion.w;
  415. this.onChangeCallback();
  416. return this;
  417. },
  418. setFromEuler: function ( euler, update ) {
  419. if ( euler instanceof THREE.Euler === false ) {
  420. throw new Error( 'THREE.Quaternion: .setFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  421. }
  422. // http://www.mathworks.com/matlabcentral/fileexchange/
  423. // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/
  424. // content/SpinCalc.m
  425. var c1 = Math.cos( euler._x / 2 );
  426. var c2 = Math.cos( euler._y / 2 );
  427. var c3 = Math.cos( euler._z / 2 );
  428. var s1 = Math.sin( euler._x / 2 );
  429. var s2 = Math.sin( euler._y / 2 );
  430. var s3 = Math.sin( euler._z / 2 );
  431. if ( euler.order === 'XYZ' ) {
  432. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  433. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  434. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  435. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  436. } else if ( euler.order === 'YXZ' ) {
  437. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  438. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  439. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  440. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  441. } else if ( euler.order === 'ZXY' ) {
  442. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  443. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  444. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  445. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  446. } else if ( euler.order === 'ZYX' ) {
  447. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  448. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  449. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  450. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  451. } else if ( euler.order === 'YZX' ) {
  452. this._x = s1 * c2 * c3 + c1 * s2 * s3;
  453. this._y = c1 * s2 * c3 + s1 * c2 * s3;
  454. this._z = c1 * c2 * s3 - s1 * s2 * c3;
  455. this._w = c1 * c2 * c3 - s1 * s2 * s3;
  456. } else if ( euler.order === 'XZY' ) {
  457. this._x = s1 * c2 * c3 - c1 * s2 * s3;
  458. this._y = c1 * s2 * c3 - s1 * c2 * s3;
  459. this._z = c1 * c2 * s3 + s1 * s2 * c3;
  460. this._w = c1 * c2 * c3 + s1 * s2 * s3;
  461. }
  462. if ( update !== false ) this.onChangeCallback();
  463. return this;
  464. },
  465. setFromAxisAngle: function ( axis, angle ) {
  466. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm
  467. // assumes axis is normalized
  468. var halfAngle = angle / 2, s = Math.sin( halfAngle );
  469. this._x = axis.x * s;
  470. this._y = axis.y * s;
  471. this._z = axis.z * s;
  472. this._w = Math.cos( halfAngle );
  473. this.onChangeCallback();
  474. return this;
  475. },
  476. setFromRotationMatrix: function ( m ) {
  477. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm
  478. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  479. var te = m.elements,
  480. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  481. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  482. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ],
  483. trace = m11 + m22 + m33,
  484. s;
  485. if ( trace > 0 ) {
  486. s = 0.5 / Math.sqrt( trace + 1.0 );
  487. this._w = 0.25 / s;
  488. this._x = ( m32 - m23 ) * s;
  489. this._y = ( m13 - m31 ) * s;
  490. this._z = ( m21 - m12 ) * s;
  491. } else if ( m11 > m22 && m11 > m33 ) {
  492. s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 );
  493. this._w = ( m32 - m23 ) / s;
  494. this._x = 0.25 * s;
  495. this._y = ( m12 + m21 ) / s;
  496. this._z = ( m13 + m31 ) / s;
  497. } else if ( m22 > m33 ) {
  498. s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 );
  499. this._w = ( m13 - m31 ) / s;
  500. this._x = ( m12 + m21 ) / s;
  501. this._y = 0.25 * s;
  502. this._z = ( m23 + m32 ) / s;
  503. } else {
  504. s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 );
  505. this._w = ( m21 - m12 ) / s;
  506. this._x = ( m13 + m31 ) / s;
  507. this._y = ( m23 + m32 ) / s;
  508. this._z = 0.25 * s;
  509. }
  510. this.onChangeCallback();
  511. return this;
  512. },
  513. setFromUnitVectors: function () {
  514. // http://lolengine.net/blog/2014/02/24/quaternion-from-two-vectors-final
  515. // assumes direction vectors vFrom and vTo are normalized
  516. var v1, r;
  517. var EPS = 0.000001;
  518. return function ( vFrom, vTo ) {
  519. if ( v1 === undefined ) v1 = new THREE.Vector3();
  520. r = vFrom.dot( vTo ) + 1;
  521. if ( r < EPS ) {
  522. r = 0;
  523. if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) {
  524. v1.set( - vFrom.y, vFrom.x, 0 );
  525. } else {
  526. v1.set( 0, - vFrom.z, vFrom.y );
  527. }
  528. } else {
  529. v1.crossVectors( vFrom, vTo );
  530. }
  531. this._x = v1.x;
  532. this._y = v1.y;
  533. this._z = v1.z;
  534. this._w = r;
  535. this.normalize();
  536. return this;
  537. }
  538. }(),
  539. inverse: function () {
  540. this.conjugate().normalize();
  541. return this;
  542. },
  543. conjugate: function () {
  544. this._x *= - 1;
  545. this._y *= - 1;
  546. this._z *= - 1;
  547. this.onChangeCallback();
  548. return this;
  549. },
  550. dot: function ( v ) {
  551. return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w;
  552. },
  553. lengthSq: function () {
  554. return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w;
  555. },
  556. length: function () {
  557. return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w );
  558. },
  559. normalize: function () {
  560. var l = this.length();
  561. if ( l === 0 ) {
  562. this._x = 0;
  563. this._y = 0;
  564. this._z = 0;
  565. this._w = 1;
  566. } else {
  567. l = 1 / l;
  568. this._x = this._x * l;
  569. this._y = this._y * l;
  570. this._z = this._z * l;
  571. this._w = this._w * l;
  572. }
  573. this.onChangeCallback();
  574. return this;
  575. },
  576. multiply: function ( q, p ) {
  577. if ( p !== undefined ) {
  578. console.warn( 'THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' );
  579. return this.multiplyQuaternions( q, p );
  580. }
  581. return this.multiplyQuaternions( this, q );
  582. },
  583. multiplyQuaternions: function ( a, b ) {
  584. // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm
  585. var qax = a._x, qay = a._y, qaz = a._z, qaw = a._w;
  586. var qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w;
  587. this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby;
  588. this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz;
  589. this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx;
  590. this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz;
  591. this.onChangeCallback();
  592. return this;
  593. },
  594. multiplyVector3: function ( vector ) {
  595. console.warn( 'THREE.Quaternion: .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' );
  596. return vector.applyQuaternion( this );
  597. },
  598. slerp: function ( qb, t ) {
  599. if ( t === 0 ) return this;
  600. if ( t === 1 ) return this.copy( qb );
  601. var x = this._x, y = this._y, z = this._z, w = this._w;
  602. // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/
  603. var cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z;
  604. if ( cosHalfTheta < 0 ) {
  605. this._w = - qb._w;
  606. this._x = - qb._x;
  607. this._y = - qb._y;
  608. this._z = - qb._z;
  609. cosHalfTheta = - cosHalfTheta;
  610. } else {
  611. this.copy( qb );
  612. }
  613. if ( cosHalfTheta >= 1.0 ) {
  614. this._w = w;
  615. this._x = x;
  616. this._y = y;
  617. this._z = z;
  618. return this;
  619. }
  620. var halfTheta = Math.acos( cosHalfTheta );
  621. var sinHalfTheta = Math.sqrt( 1.0 - cosHalfTheta * cosHalfTheta );
  622. if ( Math.abs( sinHalfTheta ) < 0.001 ) {
  623. this._w = 0.5 * ( w + this._w );
  624. this._x = 0.5 * ( x + this._x );
  625. this._y = 0.5 * ( y + this._y );
  626. this._z = 0.5 * ( z + this._z );
  627. return this;
  628. }
  629. var ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta,
  630. ratioB = Math.sin( t * halfTheta ) / sinHalfTheta;
  631. this._w = ( w * ratioA + this._w * ratioB );
  632. this._x = ( x * ratioA + this._x * ratioB );
  633. this._y = ( y * ratioA + this._y * ratioB );
  634. this._z = ( z * ratioA + this._z * ratioB );
  635. this.onChangeCallback();
  636. return this;
  637. },
  638. equals: function ( quaternion ) {
  639. return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w );
  640. },
  641. fromArray: function ( array ) {
  642. this._x = array[ 0 ];
  643. this._y = array[ 1 ];
  644. this._z = array[ 2 ];
  645. this._w = array[ 3 ];
  646. this.onChangeCallback();
  647. return this;
  648. },
  649. toArray: function () {
  650. return [ this._x, this._y, this._z, this._w ];
  651. },
  652. onChange: function ( callback ) {
  653. this.onChangeCallback = callback;
  654. return this;
  655. },
  656. onChangeCallback: function () {},
  657. clone: function () {
  658. return new THREE.Quaternion( this._x, this._y, this._z, this._w );
  659. }
  660. };
  661. THREE.Quaternion.slerp = function ( qa, qb, qm, t ) {
  662. return qm.copy( qa ).slerp( qb, t );
  663. }
  664. // File:src/math/Vector2.js
  665. /**
  666. * @author mrdoob / http://mrdoob.com/
  667. * @author philogb / http://blog.thejit.org/
  668. * @author egraether / http://egraether.com/
  669. * @author zz85 / http://www.lab4games.net/zz85/blog
  670. */
  671. THREE.Vector2 = function ( x, y ) {
  672. this.x = x || 0;
  673. this.y = y || 0;
  674. };
  675. THREE.Vector2.prototype = {
  676. constructor: THREE.Vector2,
  677. set: function ( x, y ) {
  678. this.x = x;
  679. this.y = y;
  680. return this;
  681. },
  682. setX: function ( x ) {
  683. this.x = x;
  684. return this;
  685. },
  686. setY: function ( y ) {
  687. this.y = y;
  688. return this;
  689. },
  690. setComponent: function ( index, value ) {
  691. switch ( index ) {
  692. case 0: this.x = value; break;
  693. case 1: this.y = value; break;
  694. default: throw new Error( 'index is out of range: ' + index );
  695. }
  696. },
  697. getComponent: function ( index ) {
  698. switch ( index ) {
  699. case 0: return this.x;
  700. case 1: return this.y;
  701. default: throw new Error( 'index is out of range: ' + index );
  702. }
  703. },
  704. copy: function ( v ) {
  705. this.x = v.x;
  706. this.y = v.y;
  707. return this;
  708. },
  709. add: function ( v, w ) {
  710. if ( w !== undefined ) {
  711. console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  712. return this.addVectors( v, w );
  713. }
  714. this.x += v.x;
  715. this.y += v.y;
  716. return this;
  717. },
  718. addVectors: function ( a, b ) {
  719. this.x = a.x + b.x;
  720. this.y = a.y + b.y;
  721. return this;
  722. },
  723. addScalar: function ( s ) {
  724. this.x += s;
  725. this.y += s;
  726. return this;
  727. },
  728. sub: function ( v, w ) {
  729. if ( w !== undefined ) {
  730. console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  731. return this.subVectors( v, w );
  732. }
  733. this.x -= v.x;
  734. this.y -= v.y;
  735. return this;
  736. },
  737. subVectors: function ( a, b ) {
  738. this.x = a.x - b.x;
  739. this.y = a.y - b.y;
  740. return this;
  741. },
  742. multiply: function ( v ) {
  743. this.x *= v.x;
  744. this.y *= v.y;
  745. return this;
  746. },
  747. multiplyScalar: function ( s ) {
  748. this.x *= s;
  749. this.y *= s;
  750. return this;
  751. },
  752. divide: function ( v ) {
  753. this.x /= v.x;
  754. this.y /= v.y;
  755. return this;
  756. },
  757. divideScalar: function ( scalar ) {
  758. if ( scalar !== 0 ) {
  759. var invScalar = 1 / scalar;
  760. this.x *= invScalar;
  761. this.y *= invScalar;
  762. } else {
  763. this.x = 0;
  764. this.y = 0;
  765. }
  766. return this;
  767. },
  768. min: function ( v ) {
  769. if ( this.x > v.x ) {
  770. this.x = v.x;
  771. }
  772. if ( this.y > v.y ) {
  773. this.y = v.y;
  774. }
  775. return this;
  776. },
  777. max: function ( v ) {
  778. if ( this.x < v.x ) {
  779. this.x = v.x;
  780. }
  781. if ( this.y < v.y ) {
  782. this.y = v.y;
  783. }
  784. return this;
  785. },
  786. clamp: function ( min, max ) {
  787. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  788. if ( this.x < min.x ) {
  789. this.x = min.x;
  790. } else if ( this.x > max.x ) {
  791. this.x = max.x;
  792. }
  793. if ( this.y < min.y ) {
  794. this.y = min.y;
  795. } else if ( this.y > max.y ) {
  796. this.y = max.y;
  797. }
  798. return this;
  799. },
  800. clampScalar: ( function () {
  801. var min, max;
  802. return function ( minVal, maxVal ) {
  803. if ( min === undefined ) {
  804. min = new THREE.Vector2();
  805. max = new THREE.Vector2();
  806. }
  807. min.set( minVal, minVal );
  808. max.set( maxVal, maxVal );
  809. return this.clamp( min, max );
  810. };
  811. } )(),
  812. floor: function () {
  813. this.x = Math.floor( this.x );
  814. this.y = Math.floor( this.y );
  815. return this;
  816. },
  817. ceil: function () {
  818. this.x = Math.ceil( this.x );
  819. this.y = Math.ceil( this.y );
  820. return this;
  821. },
  822. round: function () {
  823. this.x = Math.round( this.x );
  824. this.y = Math.round( this.y );
  825. return this;
  826. },
  827. roundToZero: function () {
  828. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  829. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  830. return this;
  831. },
  832. negate: function () {
  833. this.x = - this.x;
  834. this.y = - this.y;
  835. return this;
  836. },
  837. dot: function ( v ) {
  838. return this.x * v.x + this.y * v.y;
  839. },
  840. lengthSq: function () {
  841. return this.x * this.x + this.y * this.y;
  842. },
  843. length: function () {
  844. return Math.sqrt( this.x * this.x + this.y * this.y );
  845. },
  846. normalize: function () {
  847. return this.divideScalar( this.length() );
  848. },
  849. distanceTo: function ( v ) {
  850. return Math.sqrt( this.distanceToSquared( v ) );
  851. },
  852. distanceToSquared: function ( v ) {
  853. var dx = this.x - v.x, dy = this.y - v.y;
  854. return dx * dx + dy * dy;
  855. },
  856. setLength: function ( l ) {
  857. var oldLength = this.length();
  858. if ( oldLength !== 0 && l !== oldLength ) {
  859. this.multiplyScalar( l / oldLength );
  860. }
  861. return this;
  862. },
  863. lerp: function ( v, alpha ) {
  864. this.x += ( v.x - this.x ) * alpha;
  865. this.y += ( v.y - this.y ) * alpha;
  866. return this;
  867. },
  868. equals: function ( v ) {
  869. return ( ( v.x === this.x ) && ( v.y === this.y ) );
  870. },
  871. fromArray: function ( array ) {
  872. this.x = array[ 0 ];
  873. this.y = array[ 1 ];
  874. return this;
  875. },
  876. toArray: function () {
  877. return [ this.x, this.y ];
  878. },
  879. clone: function () {
  880. return new THREE.Vector2( this.x, this.y );
  881. }
  882. };
  883. // File:src/math/Vector3.js
  884. /**
  885. * @author mrdoob / http://mrdoob.com/
  886. * @author *kile / http://kile.stravaganza.org/
  887. * @author philogb / http://blog.thejit.org/
  888. * @author mikael emtinger / http://gomo.se/
  889. * @author egraether / http://egraether.com/
  890. * @author WestLangley / http://github.com/WestLangley
  891. */
  892. THREE.Vector3 = function ( x, y, z ) {
  893. this.x = x || 0;
  894. this.y = y || 0;
  895. this.z = z || 0;
  896. };
  897. THREE.Vector3.prototype = {
  898. constructor: THREE.Vector3,
  899. set: function ( x, y, z ) {
  900. this.x = x;
  901. this.y = y;
  902. this.z = z;
  903. return this;
  904. },
  905. setX: function ( x ) {
  906. this.x = x;
  907. return this;
  908. },
  909. setY: function ( y ) {
  910. this.y = y;
  911. return this;
  912. },
  913. setZ: function ( z ) {
  914. this.z = z;
  915. return this;
  916. },
  917. setComponent: function ( index, value ) {
  918. switch ( index ) {
  919. case 0: this.x = value; break;
  920. case 1: this.y = value; break;
  921. case 2: this.z = value; break;
  922. default: throw new Error( 'index is out of range: ' + index );
  923. }
  924. },
  925. getComponent: function ( index ) {
  926. switch ( index ) {
  927. case 0: return this.x;
  928. case 1: return this.y;
  929. case 2: return this.z;
  930. default: throw new Error( 'index is out of range: ' + index );
  931. }
  932. },
  933. copy: function ( v ) {
  934. this.x = v.x;
  935. this.y = v.y;
  936. this.z = v.z;
  937. return this;
  938. },
  939. add: function ( v, w ) {
  940. if ( w !== undefined ) {
  941. console.warn( 'THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  942. return this.addVectors( v, w );
  943. }
  944. this.x += v.x;
  945. this.y += v.y;
  946. this.z += v.z;
  947. return this;
  948. },
  949. addScalar: function ( s ) {
  950. this.x += s;
  951. this.y += s;
  952. this.z += s;
  953. return this;
  954. },
  955. addVectors: function ( a, b ) {
  956. this.x = a.x + b.x;
  957. this.y = a.y + b.y;
  958. this.z = a.z + b.z;
  959. return this;
  960. },
  961. sub: function ( v, w ) {
  962. if ( w !== undefined ) {
  963. console.warn( 'THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  964. return this.subVectors( v, w );
  965. }
  966. this.x -= v.x;
  967. this.y -= v.y;
  968. this.z -= v.z;
  969. return this;
  970. },
  971. subVectors: function ( a, b ) {
  972. this.x = a.x - b.x;
  973. this.y = a.y - b.y;
  974. this.z = a.z - b.z;
  975. return this;
  976. },
  977. multiply: function ( v, w ) {
  978. if ( w !== undefined ) {
  979. console.warn( 'THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' );
  980. return this.multiplyVectors( v, w );
  981. }
  982. this.x *= v.x;
  983. this.y *= v.y;
  984. this.z *= v.z;
  985. return this;
  986. },
  987. multiplyScalar: function ( scalar ) {
  988. this.x *= scalar;
  989. this.y *= scalar;
  990. this.z *= scalar;
  991. return this;
  992. },
  993. multiplyVectors: function ( a, b ) {
  994. this.x = a.x * b.x;
  995. this.y = a.y * b.y;
  996. this.z = a.z * b.z;
  997. return this;
  998. },
  999. applyEuler: function () {
  1000. var quaternion;
  1001. return function ( euler ) {
  1002. if ( euler instanceof THREE.Euler === false ) {
  1003. console.error( 'THREE.Vector3: .applyEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  1004. }
  1005. if ( quaternion === undefined ) quaternion = new THREE.Quaternion();
  1006. this.applyQuaternion( quaternion.setFromEuler( euler ) );
  1007. return this;
  1008. };
  1009. }(),
  1010. applyAxisAngle: function () {
  1011. var quaternion;
  1012. return function ( axis, angle ) {
  1013. if ( quaternion === undefined ) quaternion = new THREE.Quaternion();
  1014. this.applyQuaternion( quaternion.setFromAxisAngle( axis, angle ) );
  1015. return this;
  1016. };
  1017. }(),
  1018. applyMatrix3: function ( m ) {
  1019. var x = this.x;
  1020. var y = this.y;
  1021. var z = this.z;
  1022. var e = m.elements;
  1023. this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z;
  1024. this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z;
  1025. this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z;
  1026. return this;
  1027. },
  1028. applyMatrix4: function ( m ) {
  1029. // input: THREE.Matrix4 affine matrix
  1030. var x = this.x, y = this.y, z = this.z;
  1031. var e = m.elements;
  1032. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ];
  1033. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ];
  1034. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ];
  1035. return this;
  1036. },
  1037. applyProjection: function ( m ) {
  1038. // input: THREE.Matrix4 projection matrix
  1039. var x = this.x, y = this.y, z = this.z;
  1040. var e = m.elements;
  1041. var d = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] ); // perspective divide
  1042. this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * d;
  1043. this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * d;
  1044. this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * d;
  1045. return this;
  1046. },
  1047. applyQuaternion: function ( q ) {
  1048. var x = this.x;
  1049. var y = this.y;
  1050. var z = this.z;
  1051. var qx = q.x;
  1052. var qy = q.y;
  1053. var qz = q.z;
  1054. var qw = q.w;
  1055. // calculate quat * vector
  1056. var ix = qw * x + qy * z - qz * y;
  1057. var iy = qw * y + qz * x - qx * z;
  1058. var iz = qw * z + qx * y - qy * x;
  1059. var iw = - qx * x - qy * y - qz * z;
  1060. // calculate result * inverse quat
  1061. this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy;
  1062. this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz;
  1063. this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx;
  1064. return this;
  1065. },
  1066. transformDirection: function ( m ) {
  1067. // input: THREE.Matrix4 affine matrix
  1068. // vector interpreted as a direction
  1069. var x = this.x, y = this.y, z = this.z;
  1070. var e = m.elements;
  1071. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z;
  1072. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z;
  1073. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z;
  1074. this.normalize();
  1075. return this;
  1076. },
  1077. divide: function ( v ) {
  1078. this.x /= v.x;
  1079. this.y /= v.y;
  1080. this.z /= v.z;
  1081. return this;
  1082. },
  1083. divideScalar: function ( scalar ) {
  1084. if ( scalar !== 0 ) {
  1085. var invScalar = 1 / scalar;
  1086. this.x *= invScalar;
  1087. this.y *= invScalar;
  1088. this.z *= invScalar;
  1089. } else {
  1090. this.x = 0;
  1091. this.y = 0;
  1092. this.z = 0;
  1093. }
  1094. return this;
  1095. },
  1096. min: function ( v ) {
  1097. if ( this.x > v.x ) {
  1098. this.x = v.x;
  1099. }
  1100. if ( this.y > v.y ) {
  1101. this.y = v.y;
  1102. }
  1103. if ( this.z > v.z ) {
  1104. this.z = v.z;
  1105. }
  1106. return this;
  1107. },
  1108. max: function ( v ) {
  1109. if ( this.x < v.x ) {
  1110. this.x = v.x;
  1111. }
  1112. if ( this.y < v.y ) {
  1113. this.y = v.y;
  1114. }
  1115. if ( this.z < v.z ) {
  1116. this.z = v.z;
  1117. }
  1118. return this;
  1119. },
  1120. clamp: function ( min, max ) {
  1121. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  1122. if ( this.x < min.x ) {
  1123. this.x = min.x;
  1124. } else if ( this.x > max.x ) {
  1125. this.x = max.x;
  1126. }
  1127. if ( this.y < min.y ) {
  1128. this.y = min.y;
  1129. } else if ( this.y > max.y ) {
  1130. this.y = max.y;
  1131. }
  1132. if ( this.z < min.z ) {
  1133. this.z = min.z;
  1134. } else if ( this.z > max.z ) {
  1135. this.z = max.z;
  1136. }
  1137. return this;
  1138. },
  1139. clampScalar: ( function () {
  1140. var min, max;
  1141. return function ( minVal, maxVal ) {
  1142. if ( min === undefined ) {
  1143. min = new THREE.Vector3();
  1144. max = new THREE.Vector3();
  1145. }
  1146. min.set( minVal, minVal, minVal );
  1147. max.set( maxVal, maxVal, maxVal );
  1148. return this.clamp( min, max );
  1149. };
  1150. } )(),
  1151. floor: function () {
  1152. this.x = Math.floor( this.x );
  1153. this.y = Math.floor( this.y );
  1154. this.z = Math.floor( this.z );
  1155. return this;
  1156. },
  1157. ceil: function () {
  1158. this.x = Math.ceil( this.x );
  1159. this.y = Math.ceil( this.y );
  1160. this.z = Math.ceil( this.z );
  1161. return this;
  1162. },
  1163. round: function () {
  1164. this.x = Math.round( this.x );
  1165. this.y = Math.round( this.y );
  1166. this.z = Math.round( this.z );
  1167. return this;
  1168. },
  1169. roundToZero: function () {
  1170. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  1171. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  1172. this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
  1173. return this;
  1174. },
  1175. negate: function () {
  1176. this.x = - this.x;
  1177. this.y = - this.y;
  1178. this.z = - this.z;
  1179. return this;
  1180. },
  1181. dot: function ( v ) {
  1182. return this.x * v.x + this.y * v.y + this.z * v.z;
  1183. },
  1184. lengthSq: function () {
  1185. return this.x * this.x + this.y * this.y + this.z * this.z;
  1186. },
  1187. length: function () {
  1188. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z );
  1189. },
  1190. lengthManhattan: function () {
  1191. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z );
  1192. },
  1193. normalize: function () {
  1194. return this.divideScalar( this.length() );
  1195. },
  1196. setLength: function ( l ) {
  1197. var oldLength = this.length();
  1198. if ( oldLength !== 0 && l !== oldLength ) {
  1199. this.multiplyScalar( l / oldLength );
  1200. }
  1201. return this;
  1202. },
  1203. lerp: function ( v, alpha ) {
  1204. this.x += ( v.x - this.x ) * alpha;
  1205. this.y += ( v.y - this.y ) * alpha;
  1206. this.z += ( v.z - this.z ) * alpha;
  1207. return this;
  1208. },
  1209. cross: function ( v, w ) {
  1210. if ( w !== undefined ) {
  1211. console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' );
  1212. return this.crossVectors( v, w );
  1213. }
  1214. var x = this.x, y = this.y, z = this.z;
  1215. this.x = y * v.z - z * v.y;
  1216. this.y = z * v.x - x * v.z;
  1217. this.z = x * v.y - y * v.x;
  1218. return this;
  1219. },
  1220. crossVectors: function ( a, b ) {
  1221. var ax = a.x, ay = a.y, az = a.z;
  1222. var bx = b.x, by = b.y, bz = b.z;
  1223. this.x = ay * bz - az * by;
  1224. this.y = az * bx - ax * bz;
  1225. this.z = ax * by - ay * bx;
  1226. return this;
  1227. },
  1228. projectOnVector: function () {
  1229. var v1, dot;
  1230. return function ( vector ) {
  1231. if ( v1 === undefined ) v1 = new THREE.Vector3();
  1232. v1.copy( vector ).normalize();
  1233. dot = this.dot( v1 );
  1234. return this.copy( v1 ).multiplyScalar( dot );
  1235. };
  1236. }(),
  1237. projectOnPlane: function () {
  1238. var v1;
  1239. return function ( planeNormal ) {
  1240. if ( v1 === undefined ) v1 = new THREE.Vector3();
  1241. v1.copy( this ).projectOnVector( planeNormal );
  1242. return this.sub( v1 );
  1243. }
  1244. }(),
  1245. reflect: function () {
  1246. // reflect incident vector off plane orthogonal to normal
  1247. // normal is assumed to have unit length
  1248. var v1;
  1249. return function ( normal ) {
  1250. if ( v1 === undefined ) v1 = new THREE.Vector3();
  1251. return this.sub( v1.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) );
  1252. }
  1253. }(),
  1254. angleTo: function ( v ) {
  1255. var theta = this.dot( v ) / ( this.length() * v.length() );
  1256. // clamp, to handle numerical problems
  1257. return Math.acos( THREE.Math.clamp( theta, - 1, 1 ) );
  1258. },
  1259. distanceTo: function ( v ) {
  1260. return Math.sqrt( this.distanceToSquared( v ) );
  1261. },
  1262. distanceToSquared: function ( v ) {
  1263. var dx = this.x - v.x;
  1264. var dy = this.y - v.y;
  1265. var dz = this.z - v.z;
  1266. return dx * dx + dy * dy + dz * dz;
  1267. },
  1268. setEulerFromRotationMatrix: function ( m, order ) {
  1269. console.error( 'THREE.Vector3: .setEulerFromRotationMatrix() has been removed. Use Euler.setFromRotationMatrix() instead.' );
  1270. },
  1271. setEulerFromQuaternion: function ( q, order ) {
  1272. console.error( 'THREE.Vector3: .setEulerFromQuaternion() has been removed. Use Euler.setFromQuaternion() instead.' );
  1273. },
  1274. getPositionFromMatrix: function ( m ) {
  1275. console.warn( 'THREE.Vector3: .getPositionFromMatrix() has been renamed to .setFromMatrixPosition().' );
  1276. return this.setFromMatrixPosition( m );
  1277. },
  1278. getScaleFromMatrix: function ( m ) {
  1279. console.warn( 'THREE.Vector3: .getScaleFromMatrix() has been renamed to .setFromMatrixScale().' );
  1280. return this.setFromMatrixScale( m );
  1281. },
  1282. getColumnFromMatrix: function ( index, matrix ) {
  1283. console.warn( 'THREE.Vector3: .getColumnFromMatrix() has been renamed to .setFromMatrixColumn().' );
  1284. return this.setFromMatrixColumn( index, matrix );
  1285. },
  1286. setFromMatrixPosition: function ( m ) {
  1287. this.x = m.elements[ 12 ];
  1288. this.y = m.elements[ 13 ];
  1289. this.z = m.elements[ 14 ];
  1290. return this;
  1291. },
  1292. setFromMatrixScale: function ( m ) {
  1293. var sx = this.set( m.elements[ 0 ], m.elements[ 1 ], m.elements[ 2 ] ).length();
  1294. var sy = this.set( m.elements[ 4 ], m.elements[ 5 ], m.elements[ 6 ] ).length();
  1295. var sz = this.set( m.elements[ 8 ], m.elements[ 9 ], m.elements[ 10 ] ).length();
  1296. this.x = sx;
  1297. this.y = sy;
  1298. this.z = sz;
  1299. return this;
  1300. },
  1301. setFromMatrixColumn: function ( index, matrix ) {
  1302. var offset = index * 4;
  1303. var me = matrix.elements;
  1304. this.x = me[ offset ];
  1305. this.y = me[ offset + 1 ];
  1306. this.z = me[ offset + 2 ];
  1307. return this;
  1308. },
  1309. equals: function ( v ) {
  1310. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) );
  1311. },
  1312. fromArray: function ( array ) {
  1313. this.x = array[ 0 ];
  1314. this.y = array[ 1 ];
  1315. this.z = array[ 2 ];
  1316. return this;
  1317. },
  1318. toArray: function () {
  1319. return [ this.x, this.y, this.z ];
  1320. },
  1321. clone: function () {
  1322. return new THREE.Vector3( this.x, this.y, this.z );
  1323. }
  1324. };
  1325. // File:src/math/Vector4.js
  1326. /**
  1327. * @author supereggbert / http://www.paulbrunt.co.uk/
  1328. * @author philogb / http://blog.thejit.org/
  1329. * @author mikael emtinger / http://gomo.se/
  1330. * @author egraether / http://egraether.com/
  1331. * @author WestLangley / http://github.com/WestLangley
  1332. */
  1333. THREE.Vector4 = function ( x, y, z, w ) {
  1334. this.x = x || 0;
  1335. this.y = y || 0;
  1336. this.z = z || 0;
  1337. this.w = ( w !== undefined ) ? w : 1;
  1338. };
  1339. THREE.Vector4.prototype = {
  1340. constructor: THREE.Vector4,
  1341. set: function ( x, y, z, w ) {
  1342. this.x = x;
  1343. this.y = y;
  1344. this.z = z;
  1345. this.w = w;
  1346. return this;
  1347. },
  1348. setX: function ( x ) {
  1349. this.x = x;
  1350. return this;
  1351. },
  1352. setY: function ( y ) {
  1353. this.y = y;
  1354. return this;
  1355. },
  1356. setZ: function ( z ) {
  1357. this.z = z;
  1358. return this;
  1359. },
  1360. setW: function ( w ) {
  1361. this.w = w;
  1362. return this;
  1363. },
  1364. setComponent: function ( index, value ) {
  1365. switch ( index ) {
  1366. case 0: this.x = value; break;
  1367. case 1: this.y = value; break;
  1368. case 2: this.z = value; break;
  1369. case 3: this.w = value; break;
  1370. default: throw new Error( 'index is out of range: ' + index );
  1371. }
  1372. },
  1373. getComponent: function ( index ) {
  1374. switch ( index ) {
  1375. case 0: return this.x;
  1376. case 1: return this.y;
  1377. case 2: return this.z;
  1378. case 3: return this.w;
  1379. default: throw new Error( 'index is out of range: ' + index );
  1380. }
  1381. },
  1382. copy: function ( v ) {
  1383. this.x = v.x;
  1384. this.y = v.y;
  1385. this.z = v.z;
  1386. this.w = ( v.w !== undefined ) ? v.w : 1;
  1387. return this;
  1388. },
  1389. add: function ( v, w ) {
  1390. if ( w !== undefined ) {
  1391. console.warn( 'THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' );
  1392. return this.addVectors( v, w );
  1393. }
  1394. this.x += v.x;
  1395. this.y += v.y;
  1396. this.z += v.z;
  1397. this.w += v.w;
  1398. return this;
  1399. },
  1400. addScalar: function ( s ) {
  1401. this.x += s;
  1402. this.y += s;
  1403. this.z += s;
  1404. this.w += s;
  1405. return this;
  1406. },
  1407. addVectors: function ( a, b ) {
  1408. this.x = a.x + b.x;
  1409. this.y = a.y + b.y;
  1410. this.z = a.z + b.z;
  1411. this.w = a.w + b.w;
  1412. return this;
  1413. },
  1414. sub: function ( v, w ) {
  1415. if ( w !== undefined ) {
  1416. console.warn( 'THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' );
  1417. return this.subVectors( v, w );
  1418. }
  1419. this.x -= v.x;
  1420. this.y -= v.y;
  1421. this.z -= v.z;
  1422. this.w -= v.w;
  1423. return this;
  1424. },
  1425. subVectors: function ( a, b ) {
  1426. this.x = a.x - b.x;
  1427. this.y = a.y - b.y;
  1428. this.z = a.z - b.z;
  1429. this.w = a.w - b.w;
  1430. return this;
  1431. },
  1432. multiplyScalar: function ( scalar ) {
  1433. this.x *= scalar;
  1434. this.y *= scalar;
  1435. this.z *= scalar;
  1436. this.w *= scalar;
  1437. return this;
  1438. },
  1439. applyMatrix4: function ( m ) {
  1440. var x = this.x;
  1441. var y = this.y;
  1442. var z = this.z;
  1443. var w = this.w;
  1444. var e = m.elements;
  1445. this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w;
  1446. this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w;
  1447. this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w;
  1448. this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w;
  1449. return this;
  1450. },
  1451. divideScalar: function ( scalar ) {
  1452. if ( scalar !== 0 ) {
  1453. var invScalar = 1 / scalar;
  1454. this.x *= invScalar;
  1455. this.y *= invScalar;
  1456. this.z *= invScalar;
  1457. this.w *= invScalar;
  1458. } else {
  1459. this.x = 0;
  1460. this.y = 0;
  1461. this.z = 0;
  1462. this.w = 1;
  1463. }
  1464. return this;
  1465. },
  1466. setAxisAngleFromQuaternion: function ( q ) {
  1467. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm
  1468. // q is assumed to be normalized
  1469. this.w = 2 * Math.acos( q.w );
  1470. var s = Math.sqrt( 1 - q.w * q.w );
  1471. if ( s < 0.0001 ) {
  1472. this.x = 1;
  1473. this.y = 0;
  1474. this.z = 0;
  1475. } else {
  1476. this.x = q.x / s;
  1477. this.y = q.y / s;
  1478. this.z = q.z / s;
  1479. }
  1480. return this;
  1481. },
  1482. setAxisAngleFromRotationMatrix: function ( m ) {
  1483. // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm
  1484. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  1485. var angle, x, y, z, // variables for result
  1486. epsilon = 0.01, // margin to allow for rounding errors
  1487. epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees
  1488. te = m.elements,
  1489. m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ],
  1490. m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ],
  1491. m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  1492. if ( ( Math.abs( m12 - m21 ) < epsilon )
  1493. && ( Math.abs( m13 - m31 ) < epsilon )
  1494. && ( Math.abs( m23 - m32 ) < epsilon ) ) {
  1495. // singularity found
  1496. // first check for identity matrix which must have +1 for all terms
  1497. // in leading diagonal and zero in other terms
  1498. if ( ( Math.abs( m12 + m21 ) < epsilon2 )
  1499. && ( Math.abs( m13 + m31 ) < epsilon2 )
  1500. && ( Math.abs( m23 + m32 ) < epsilon2 )
  1501. && ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) {
  1502. // this singularity is identity matrix so angle = 0
  1503. this.set( 1, 0, 0, 0 );
  1504. return this; // zero angle, arbitrary axis
  1505. }
  1506. // otherwise this singularity is angle = 180
  1507. angle = Math.PI;
  1508. var xx = ( m11 + 1 ) / 2;
  1509. var yy = ( m22 + 1 ) / 2;
  1510. var zz = ( m33 + 1 ) / 2;
  1511. var xy = ( m12 + m21 ) / 4;
  1512. var xz = ( m13 + m31 ) / 4;
  1513. var yz = ( m23 + m32 ) / 4;
  1514. if ( ( xx > yy ) && ( xx > zz ) ) { // m11 is the largest diagonal term
  1515. if ( xx < epsilon ) {
  1516. x = 0;
  1517. y = 0.707106781;
  1518. z = 0.707106781;
  1519. } else {
  1520. x = Math.sqrt( xx );
  1521. y = xy / x;
  1522. z = xz / x;
  1523. }
  1524. } else if ( yy > zz ) { // m22 is the largest diagonal term
  1525. if ( yy < epsilon ) {
  1526. x = 0.707106781;
  1527. y = 0;
  1528. z = 0.707106781;
  1529. } else {
  1530. y = Math.sqrt( yy );
  1531. x = xy / y;
  1532. z = yz / y;
  1533. }
  1534. } else { // m33 is the largest diagonal term so base result on this
  1535. if ( zz < epsilon ) {
  1536. x = 0.707106781;
  1537. y = 0.707106781;
  1538. z = 0;
  1539. } else {
  1540. z = Math.sqrt( zz );
  1541. x = xz / z;
  1542. y = yz / z;
  1543. }
  1544. }
  1545. this.set( x, y, z, angle );
  1546. return this; // return 180 deg rotation
  1547. }
  1548. // as we have reached here there are no singularities so we can handle normally
  1549. var s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 )
  1550. + ( m13 - m31 ) * ( m13 - m31 )
  1551. + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize
  1552. if ( Math.abs( s ) < 0.001 ) s = 1;
  1553. // prevent divide by zero, should not happen if matrix is orthogonal and should be
  1554. // caught by singularity test above, but I've left it in just in case
  1555. this.x = ( m32 - m23 ) / s;
  1556. this.y = ( m13 - m31 ) / s;
  1557. this.z = ( m21 - m12 ) / s;
  1558. this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 );
  1559. return this;
  1560. },
  1561. min: function ( v ) {
  1562. if ( this.x > v.x ) {
  1563. this.x = v.x;
  1564. }
  1565. if ( this.y > v.y ) {
  1566. this.y = v.y;
  1567. }
  1568. if ( this.z > v.z ) {
  1569. this.z = v.z;
  1570. }
  1571. if ( this.w > v.w ) {
  1572. this.w = v.w;
  1573. }
  1574. return this;
  1575. },
  1576. max: function ( v ) {
  1577. if ( this.x < v.x ) {
  1578. this.x = v.x;
  1579. }
  1580. if ( this.y < v.y ) {
  1581. this.y = v.y;
  1582. }
  1583. if ( this.z < v.z ) {
  1584. this.z = v.z;
  1585. }
  1586. if ( this.w < v.w ) {
  1587. this.w = v.w;
  1588. }
  1589. return this;
  1590. },
  1591. clamp: function ( min, max ) {
  1592. // This function assumes min < max, if this assumption isn't true it will not operate correctly
  1593. if ( this.x < min.x ) {
  1594. this.x = min.x;
  1595. } else if ( this.x > max.x ) {
  1596. this.x = max.x;
  1597. }
  1598. if ( this.y < min.y ) {
  1599. this.y = min.y;
  1600. } else if ( this.y > max.y ) {
  1601. this.y = max.y;
  1602. }
  1603. if ( this.z < min.z ) {
  1604. this.z = min.z;
  1605. } else if ( this.z > max.z ) {
  1606. this.z = max.z;
  1607. }
  1608. if ( this.w < min.w ) {
  1609. this.w = min.w;
  1610. } else if ( this.w > max.w ) {
  1611. this.w = max.w;
  1612. }
  1613. return this;
  1614. },
  1615. clampScalar: ( function () {
  1616. var min, max;
  1617. return function ( minVal, maxVal ) {
  1618. if ( min === undefined ) {
  1619. min = new THREE.Vector4();
  1620. max = new THREE.Vector4();
  1621. }
  1622. min.set( minVal, minVal, minVal, minVal );
  1623. max.set( maxVal, maxVal, maxVal, maxVal );
  1624. return this.clamp( min, max );
  1625. };
  1626. } )(),
  1627. floor: function () {
  1628. this.x = Math.floor( this.x );
  1629. this.y = Math.floor( this.y );
  1630. this.z = Math.floor( this.z );
  1631. this.w = Math.floor( this.w );
  1632. return this;
  1633. },
  1634. ceil: function () {
  1635. this.x = Math.ceil( this.x );
  1636. this.y = Math.ceil( this.y );
  1637. this.z = Math.ceil( this.z );
  1638. this.w = Math.ceil( this.w );
  1639. return this;
  1640. },
  1641. round: function () {
  1642. this.x = Math.round( this.x );
  1643. this.y = Math.round( this.y );
  1644. this.z = Math.round( this.z );
  1645. this.w = Math.round( this.w );
  1646. return this;
  1647. },
  1648. roundToZero: function () {
  1649. this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x );
  1650. this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y );
  1651. this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z );
  1652. this.w = ( this.w < 0 ) ? Math.ceil( this.w ) : Math.floor( this.w );
  1653. return this;
  1654. },
  1655. negate: function () {
  1656. this.x = - this.x;
  1657. this.y = - this.y;
  1658. this.z = - this.z;
  1659. this.w = - this.w;
  1660. return this;
  1661. },
  1662. dot: function ( v ) {
  1663. return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w;
  1664. },
  1665. lengthSq: function () {
  1666. return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w;
  1667. },
  1668. length: function () {
  1669. return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w );
  1670. },
  1671. lengthManhattan: function () {
  1672. return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w );
  1673. },
  1674. normalize: function () {
  1675. return this.divideScalar( this.length() );
  1676. },
  1677. setLength: function ( l ) {
  1678. var oldLength = this.length();
  1679. if ( oldLength !== 0 && l !== oldLength ) {
  1680. this.multiplyScalar( l / oldLength );
  1681. }
  1682. return this;
  1683. },
  1684. lerp: function ( v, alpha ) {
  1685. this.x += ( v.x - this.x ) * alpha;
  1686. this.y += ( v.y - this.y ) * alpha;
  1687. this.z += ( v.z - this.z ) * alpha;
  1688. this.w += ( v.w - this.w ) * alpha;
  1689. return this;
  1690. },
  1691. equals: function ( v ) {
  1692. return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) );
  1693. },
  1694. fromArray: function ( array ) {
  1695. this.x = array[ 0 ];
  1696. this.y = array[ 1 ];
  1697. this.z = array[ 2 ];
  1698. this.w = array[ 3 ];
  1699. return this;
  1700. },
  1701. toArray: function () {
  1702. return [ this.x, this.y, this.z, this.w ];
  1703. },
  1704. clone: function () {
  1705. return new THREE.Vector4( this.x, this.y, this.z, this.w );
  1706. }
  1707. };
  1708. // File:src/math/Euler.js
  1709. /**
  1710. * @author mrdoob / http://mrdoob.com/
  1711. * @author WestLangley / http://github.com/WestLangley
  1712. * @author bhouston / http://exocortex.com
  1713. */
  1714. THREE.Euler = function ( x, y, z, order ) {
  1715. this._x = x || 0;
  1716. this._y = y || 0;
  1717. this._z = z || 0;
  1718. this._order = order || THREE.Euler.DefaultOrder;
  1719. };
  1720. THREE.Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ];
  1721. THREE.Euler.DefaultOrder = 'XYZ';
  1722. THREE.Euler.prototype = {
  1723. constructor: THREE.Euler,
  1724. _x: 0, _y: 0, _z: 0, _order: THREE.Euler.DefaultOrder,
  1725. get x () {
  1726. return this._x;
  1727. },
  1728. set x ( value ) {
  1729. this._x = value;
  1730. this.onChangeCallback();
  1731. },
  1732. get y () {
  1733. return this._y;
  1734. },
  1735. set y ( value ) {
  1736. this._y = value;
  1737. this.onChangeCallback();
  1738. },
  1739. get z () {
  1740. return this._z;
  1741. },
  1742. set z ( value ) {
  1743. this._z = value;
  1744. this.onChangeCallback();
  1745. },
  1746. get order () {
  1747. return this._order;
  1748. },
  1749. set order ( value ) {
  1750. this._order = value;
  1751. this.onChangeCallback();
  1752. },
  1753. set: function ( x, y, z, order ) {
  1754. this._x = x;
  1755. this._y = y;
  1756. this._z = z;
  1757. this._order = order || this._order;
  1758. this.onChangeCallback();
  1759. return this;
  1760. },
  1761. copy: function ( euler ) {
  1762. this._x = euler._x;
  1763. this._y = euler._y;
  1764. this._z = euler._z;
  1765. this._order = euler._order;
  1766. this.onChangeCallback();
  1767. return this;
  1768. },
  1769. setFromRotationMatrix: function ( m, order ) {
  1770. var clamp = THREE.Math.clamp;
  1771. // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled)
  1772. var te = m.elements;
  1773. var m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ];
  1774. var m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ];
  1775. var m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ];
  1776. order = order || this._order;
  1777. if ( order === 'XYZ' ) {
  1778. this._y = Math.asin( clamp( m13, - 1, 1 ) );
  1779. if ( Math.abs( m13 ) < 0.99999 ) {
  1780. this._x = Math.atan2( - m23, m33 );
  1781. this._z = Math.atan2( - m12, m11 );
  1782. } else {
  1783. this._x = Math.atan2( m32, m22 );
  1784. this._z = 0;
  1785. }
  1786. } else if ( order === 'YXZ' ) {
  1787. this._x = Math.asin( - clamp( m23, - 1, 1 ) );
  1788. if ( Math.abs( m23 ) < 0.99999 ) {
  1789. this._y = Math.atan2( m13, m33 );
  1790. this._z = Math.atan2( m21, m22 );
  1791. } else {
  1792. this._y = Math.atan2( - m31, m11 );
  1793. this._z = 0;
  1794. }
  1795. } else if ( order === 'ZXY' ) {
  1796. this._x = Math.asin( clamp( m32, - 1, 1 ) );
  1797. if ( Math.abs( m32 ) < 0.99999 ) {
  1798. this._y = Math.atan2( - m31, m33 );
  1799. this._z = Math.atan2( - m12, m22 );
  1800. } else {
  1801. this._y = 0;
  1802. this._z = Math.atan2( m21, m11 );
  1803. }
  1804. } else if ( order === 'ZYX' ) {
  1805. this._y = Math.asin( - clamp( m31, - 1, 1 ) );
  1806. if ( Math.abs( m31 ) < 0.99999 ) {
  1807. this._x = Math.atan2( m32, m33 );
  1808. this._z = Math.atan2( m21, m11 );
  1809. } else {
  1810. this._x = 0;
  1811. this._z = Math.atan2( - m12, m22 );
  1812. }
  1813. } else if ( order === 'YZX' ) {
  1814. this._z = Math.asin( clamp( m21, - 1, 1 ) );
  1815. if ( Math.abs( m21 ) < 0.99999 ) {
  1816. this._x = Math.atan2( - m23, m22 );
  1817. this._y = Math.atan2( - m31, m11 );
  1818. } else {
  1819. this._x = 0;
  1820. this._y = Math.atan2( m13, m33 );
  1821. }
  1822. } else if ( order === 'XZY' ) {
  1823. this._z = Math.asin( - clamp( m12, - 1, 1 ) );
  1824. if ( Math.abs( m12 ) < 0.99999 ) {
  1825. this._x = Math.atan2( m32, m22 );
  1826. this._y = Math.atan2( m13, m11 );
  1827. } else {
  1828. this._x = Math.atan2( - m23, m33 );
  1829. this._y = 0;
  1830. }
  1831. } else {
  1832. console.warn( 'THREE.Euler: .setFromRotationMatrix() given unsupported order: ' + order )
  1833. }
  1834. this._order = order;
  1835. this.onChangeCallback();
  1836. return this;
  1837. },
  1838. setFromQuaternion: function ( q, order, update ) {
  1839. var clamp = THREE.Math.clamp;
  1840. // q is assumed to be normalized
  1841. // http://www.mathworks.com/matlabcentral/fileexchange/20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/content/SpinCalc.m
  1842. var sqx = q.x * q.x;
  1843. var sqy = q.y * q.y;
  1844. var sqz = q.z * q.z;
  1845. var sqw = q.w * q.w;
  1846. order = order || this._order;
  1847. if ( order === 'XYZ' ) {
  1848. this._x = Math.atan2( 2 * ( q.x * q.w - q.y * q.z ), ( sqw - sqx - sqy + sqz ) );
  1849. this._y = Math.asin( clamp( 2 * ( q.x * q.z + q.y * q.w ), - 1, 1 ) );
  1850. this._z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw + sqx - sqy - sqz ) );
  1851. } else if ( order === 'YXZ' ) {
  1852. this._x = Math.asin( clamp( 2 * ( q.x * q.w - q.y * q.z ), - 1, 1 ) );
  1853. this._y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw - sqx - sqy + sqz ) );
  1854. this._z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw - sqx + sqy - sqz ) );
  1855. } else if ( order === 'ZXY' ) {
  1856. this._x = Math.asin( clamp( 2 * ( q.x * q.w + q.y * q.z ), - 1, 1 ) );
  1857. this._y = Math.atan2( 2 * ( q.y * q.w - q.z * q.x ), ( sqw - sqx - sqy + sqz ) );
  1858. this._z = Math.atan2( 2 * ( q.z * q.w - q.x * q.y ), ( sqw - sqx + sqy - sqz ) );
  1859. } else if ( order === 'ZYX' ) {
  1860. this._x = Math.atan2( 2 * ( q.x * q.w + q.z * q.y ), ( sqw - sqx - sqy + sqz ) );
  1861. this._y = Math.asin( clamp( 2 * ( q.y * q.w - q.x * q.z ), - 1, 1 ) );
  1862. this._z = Math.atan2( 2 * ( q.x * q.y + q.z * q.w ), ( sqw + sqx - sqy - sqz ) );
  1863. } else if ( order === 'YZX' ) {
  1864. this._x = Math.atan2( 2 * ( q.x * q.w - q.z * q.y ), ( sqw - sqx + sqy - sqz ) );
  1865. this._y = Math.atan2( 2 * ( q.y * q.w - q.x * q.z ), ( sqw + sqx - sqy - sqz ) );
  1866. this._z = Math.asin( clamp( 2 * ( q.x * q.y + q.z * q.w ), - 1, 1 ) );
  1867. } else if ( order === 'XZY' ) {
  1868. this._x = Math.atan2( 2 * ( q.x * q.w + q.y * q.z ), ( sqw - sqx + sqy - sqz ) );
  1869. this._y = Math.atan2( 2 * ( q.x * q.z + q.y * q.w ), ( sqw + sqx - sqy - sqz ) );
  1870. this._z = Math.asin( clamp( 2 * ( q.z * q.w - q.x * q.y ), - 1, 1 ) );
  1871. } else {
  1872. console.warn( 'THREE.Euler: .setFromQuaternion() given unsupported order: ' + order )
  1873. }
  1874. this._order = order;
  1875. if ( update !== false ) this.onChangeCallback();
  1876. return this;
  1877. },
  1878. reorder: function () {
  1879. // WARNING: this discards revolution information -bhouston
  1880. var q = new THREE.Quaternion();
  1881. return function ( newOrder ) {
  1882. q.setFromEuler( this );
  1883. this.setFromQuaternion( q, newOrder );
  1884. };
  1885. }(),
  1886. equals: function ( euler ) {
  1887. return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order );
  1888. },
  1889. fromArray: function ( array ) {
  1890. this._x = array[ 0 ];
  1891. this._y = array[ 1 ];
  1892. this._z = array[ 2 ];
  1893. if ( array[ 3 ] !== undefined ) this._order = array[ 3 ];
  1894. this.onChangeCallback();
  1895. return this;
  1896. },
  1897. toArray: function () {
  1898. return [ this._x, this._y, this._z, this._order ];
  1899. },
  1900. onChange: function ( callback ) {
  1901. this.onChangeCallback = callback;
  1902. return this;
  1903. },
  1904. onChangeCallback: function () {},
  1905. clone: function () {
  1906. return new THREE.Euler( this._x, this._y, this._z, this._order );
  1907. }
  1908. };
  1909. // File:src/math/Line3.js
  1910. /**
  1911. * @author bhouston / http://exocortex.com
  1912. */
  1913. THREE.Line3 = function ( start, end ) {
  1914. this.start = ( start !== undefined ) ? start : new THREE.Vector3();
  1915. this.end = ( end !== undefined ) ? end : new THREE.Vector3();
  1916. };
  1917. THREE.Line3.prototype = {
  1918. constructor: THREE.Line3,
  1919. set: function ( start, end ) {
  1920. this.start.copy( start );
  1921. this.end.copy( end );
  1922. return this;
  1923. },
  1924. copy: function ( line ) {
  1925. this.start.copy( line.start );
  1926. this.end.copy( line.end );
  1927. return this;
  1928. },
  1929. center: function ( optionalTarget ) {
  1930. var result = optionalTarget || new THREE.Vector3();
  1931. return result.addVectors( this.start, this.end ).multiplyScalar( 0.5 );
  1932. },
  1933. delta: function ( optionalTarget ) {
  1934. var result = optionalTarget || new THREE.Vector3();
  1935. return result.subVectors( this.end, this.start );
  1936. },
  1937. distanceSq: function () {
  1938. return this.start.distanceToSquared( this.end );
  1939. },
  1940. distance: function () {
  1941. return this.start.distanceTo( this.end );
  1942. },
  1943. at: function ( t, optionalTarget ) {
  1944. var result = optionalTarget || new THREE.Vector3();
  1945. return this.delta( result ).multiplyScalar( t ).add( this.start );
  1946. },
  1947. closestPointToPointParameter: function () {
  1948. var startP = new THREE.Vector3();
  1949. var startEnd = new THREE.Vector3();
  1950. return function ( point, clampToLine ) {
  1951. startP.subVectors( point, this.start );
  1952. startEnd.subVectors( this.end, this.start );
  1953. var startEnd2 = startEnd.dot( startEnd );
  1954. var startEnd_startP = startEnd.dot( startP );
  1955. var t = startEnd_startP / startEnd2;
  1956. if ( clampToLine ) {
  1957. t = THREE.Math.clamp( t, 0, 1 );
  1958. }
  1959. return t;
  1960. };
  1961. }(),
  1962. closestPointToPoint: function ( point, clampToLine, optionalTarget ) {
  1963. var t = this.closestPointToPointParameter( point, clampToLine );
  1964. var result = optionalTarget || new THREE.Vector3();
  1965. return this.delta( result ).multiplyScalar( t ).add( this.start );
  1966. },
  1967. applyMatrix4: function ( matrix ) {
  1968. this.start.applyMatrix4( matrix );
  1969. this.end.applyMatrix4( matrix );
  1970. return this;
  1971. },
  1972. equals: function ( line ) {
  1973. return line.start.equals( this.start ) && line.end.equals( this.end );
  1974. },
  1975. clone: function () {
  1976. return new THREE.Line3().copy( this );
  1977. }
  1978. };
  1979. // File:src/math/Box2.js
  1980. /**
  1981. * @author bhouston / http://exocortex.com
  1982. */
  1983. THREE.Box2 = function ( min, max ) {
  1984. this.min = ( min !== undefined ) ? min : new THREE.Vector2( Infinity, Infinity );
  1985. this.max = ( max !== undefined ) ? max : new THREE.Vector2( - Infinity, - Infinity );
  1986. };
  1987. THREE.Box2.prototype = {
  1988. constructor: THREE.Box2,
  1989. set: function ( min, max ) {
  1990. this.min.copy( min );
  1991. this.max.copy( max );
  1992. return this;
  1993. },
  1994. setFromPoints: function ( points ) {
  1995. this.makeEmpty();
  1996. for ( var i = 0, il = points.length; i < il; i ++ ) {
  1997. this.expandByPoint( points[ i ] )
  1998. }
  1999. return this;
  2000. },
  2001. setFromCenterAndSize: function () {
  2002. var v1 = new THREE.Vector2();
  2003. return function ( center, size ) {
  2004. var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
  2005. this.min.copy( center ).sub( halfSize );
  2006. this.max.copy( center ).add( halfSize );
  2007. return this;
  2008. };
  2009. }(),
  2010. copy: function ( box ) {
  2011. this.min.copy( box.min );
  2012. this.max.copy( box.max );
  2013. return this;
  2014. },
  2015. makeEmpty: function () {
  2016. this.min.x = this.min.y = Infinity;
  2017. this.max.x = this.max.y = - Infinity;
  2018. return this;
  2019. },
  2020. empty: function () {
  2021. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  2022. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y );
  2023. },
  2024. center: function ( optionalTarget ) {
  2025. var result = optionalTarget || new THREE.Vector2();
  2026. return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  2027. },
  2028. size: function ( optionalTarget ) {
  2029. var result = optionalTarget || new THREE.Vector2();
  2030. return result.subVectors( this.max, this.min );
  2031. },
  2032. expandByPoint: function ( point ) {
  2033. this.min.min( point );
  2034. this.max.max( point );
  2035. return this;
  2036. },
  2037. expandByVector: function ( vector ) {
  2038. this.min.sub( vector );
  2039. this.max.add( vector );
  2040. return this;
  2041. },
  2042. expandByScalar: function ( scalar ) {
  2043. this.min.addScalar( - scalar );
  2044. this.max.addScalar( scalar );
  2045. return this;
  2046. },
  2047. containsPoint: function ( point ) {
  2048. if ( point.x < this.min.x || point.x > this.max.x ||
  2049. point.y < this.min.y || point.y > this.max.y ) {
  2050. return false;
  2051. }
  2052. return true;
  2053. },
  2054. containsBox: function ( box ) {
  2055. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  2056. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) ) {
  2057. return true;
  2058. }
  2059. return false;
  2060. },
  2061. getParameter: function ( point, optionalTarget ) {
  2062. // This can potentially have a divide by zero if the box
  2063. // has a size dimension of 0.
  2064. var result = optionalTarget || new THREE.Vector2();
  2065. return result.set(
  2066. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  2067. ( point.y - this.min.y ) / ( this.max.y - this.min.y )
  2068. );
  2069. },
  2070. isIntersectionBox: function ( box ) {
  2071. // using 6 splitting planes to rule out intersections.
  2072. if ( box.max.x < this.min.x || box.min.x > this.max.x ||
  2073. box.max.y < this.min.y || box.min.y > this.max.y ) {
  2074. return false;
  2075. }
  2076. return true;
  2077. },
  2078. clampPoint: function ( point, optionalTarget ) {
  2079. var result = optionalTarget || new THREE.Vector2();
  2080. return result.copy( point ).clamp( this.min, this.max );
  2081. },
  2082. distanceToPoint: function () {
  2083. var v1 = new THREE.Vector2();
  2084. return function ( point ) {
  2085. var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
  2086. return clampedPoint.sub( point ).length();
  2087. };
  2088. }(),
  2089. intersect: function ( box ) {
  2090. this.min.max( box.min );
  2091. this.max.min( box.max );
  2092. return this;
  2093. },
  2094. union: function ( box ) {
  2095. this.min.min( box.min );
  2096. this.max.max( box.max );
  2097. return this;
  2098. },
  2099. translate: function ( offset ) {
  2100. this.min.add( offset );
  2101. this.max.add( offset );
  2102. return this;
  2103. },
  2104. equals: function ( box ) {
  2105. return box.min.equals( this.min ) && box.max.equals( this.max );
  2106. },
  2107. clone: function () {
  2108. return new THREE.Box2().copy( this );
  2109. }
  2110. };
  2111. // File:src/math/Box3.js
  2112. /**
  2113. * @author bhouston / http://exocortex.com
  2114. * @author WestLangley / http://github.com/WestLangley
  2115. */
  2116. THREE.Box3 = function ( min, max ) {
  2117. this.min = ( min !== undefined ) ? min : new THREE.Vector3( Infinity, Infinity, Infinity );
  2118. this.max = ( max !== undefined ) ? max : new THREE.Vector3( - Infinity, - Infinity, - Infinity );
  2119. };
  2120. THREE.Box3.prototype = {
  2121. constructor: THREE.Box3,
  2122. set: function ( min, max ) {
  2123. this.min.copy( min );
  2124. this.max.copy( max );
  2125. return this;
  2126. },
  2127. setFromPoints: function ( points ) {
  2128. this.makeEmpty();
  2129. for ( var i = 0, il = points.length; i < il; i ++ ) {
  2130. this.expandByPoint( points[ i ] )
  2131. }
  2132. return this;
  2133. },
  2134. setFromCenterAndSize: function () {
  2135. var v1 = new THREE.Vector3();
  2136. return function ( center, size ) {
  2137. var halfSize = v1.copy( size ).multiplyScalar( 0.5 );
  2138. this.min.copy( center ).sub( halfSize );
  2139. this.max.copy( center ).add( halfSize );
  2140. return this;
  2141. };
  2142. }(),
  2143. setFromObject: function () {
  2144. // Computes the world-axis-aligned bounding box of an object (including its children),
  2145. // accounting for both the object's, and childrens', world transforms
  2146. var v1 = new THREE.Vector3();
  2147. return function ( object ) {
  2148. var scope = this;
  2149. object.updateMatrixWorld( true );
  2150. this.makeEmpty();
  2151. object.traverse( function ( node ) {
  2152. var geometry = node.geometry;
  2153. if ( geometry !== undefined ) {
  2154. if ( geometry instanceof THREE.Geometry ) {
  2155. var vertices = geometry.vertices;
  2156. for ( var i = 0, il = vertices.length; i < il; i ++ ) {
  2157. v1.copy( vertices[ i ] );
  2158. v1.applyMatrix4( node.matrixWorld );
  2159. scope.expandByPoint( v1 );
  2160. }
  2161. } else if ( geometry instanceof THREE.BufferGeometry && geometry.attributes[ 'position' ] !== undefined ) {
  2162. var positions = geometry.attributes[ 'position' ].array;
  2163. for ( var i = 0, il = positions.length; i < il; i += 3 ) {
  2164. v1.set( positions[ i ], positions[ i + 1 ], positions[ i + 2 ] );
  2165. v1.applyMatrix4( node.matrixWorld );
  2166. scope.expandByPoint( v1 );
  2167. }
  2168. }
  2169. }
  2170. } );
  2171. return this;
  2172. };
  2173. }(),
  2174. copy: function ( box ) {
  2175. this.min.copy( box.min );
  2176. this.max.copy( box.max );
  2177. return this;
  2178. },
  2179. makeEmpty: function () {
  2180. this.min.x = this.min.y = this.min.z = Infinity;
  2181. this.max.x = this.max.y = this.max.z = - Infinity;
  2182. return this;
  2183. },
  2184. empty: function () {
  2185. // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes
  2186. return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z );
  2187. },
  2188. center: function ( optionalTarget ) {
  2189. var result = optionalTarget || new THREE.Vector3();
  2190. return result.addVectors( this.min, this.max ).multiplyScalar( 0.5 );
  2191. },
  2192. size: function ( optionalTarget ) {
  2193. var result = optionalTarget || new THREE.Vector3();
  2194. return result.subVectors( this.max, this.min );
  2195. },
  2196. expandByPoint: function ( point ) {
  2197. this.min.min( point );
  2198. this.max.max( point );
  2199. return this;
  2200. },
  2201. expandByVector: function ( vector ) {
  2202. this.min.sub( vector );
  2203. this.max.add( vector );
  2204. return this;
  2205. },
  2206. expandByScalar: function ( scalar ) {
  2207. this.min.addScalar( - scalar );
  2208. this.max.addScalar( scalar );
  2209. return this;
  2210. },
  2211. containsPoint: function ( point ) {
  2212. if ( point.x < this.min.x || point.x > this.max.x ||
  2213. point.y < this.min.y || point.y > this.max.y ||
  2214. point.z < this.min.z || point.z > this.max.z ) {
  2215. return false;
  2216. }
  2217. return true;
  2218. },
  2219. containsBox: function ( box ) {
  2220. if ( ( this.min.x <= box.min.x ) && ( box.max.x <= this.max.x ) &&
  2221. ( this.min.y <= box.min.y ) && ( box.max.y <= this.max.y ) &&
  2222. ( this.min.z <= box.min.z ) && ( box.max.z <= this.max.z ) ) {
  2223. return true;
  2224. }
  2225. return false;
  2226. },
  2227. getParameter: function ( point, optionalTarget ) {
  2228. // This can potentially have a divide by zero if the box
  2229. // has a size dimension of 0.
  2230. var result = optionalTarget || new THREE.Vector3();
  2231. return result.set(
  2232. ( point.x - this.min.x ) / ( this.max.x - this.min.x ),
  2233. ( point.y - this.min.y ) / ( this.max.y - this.min.y ),
  2234. ( point.z - this.min.z ) / ( this.max.z - this.min.z )
  2235. );
  2236. },
  2237. isIntersectionBox: function ( box ) {
  2238. // using 6 splitting planes to rule out intersections.
  2239. if ( box.max.x < this.min.x || box.min.x > this.max.x ||
  2240. box.max.y < this.min.y || box.min.y > this.max.y ||
  2241. box.max.z < this.min.z || box.min.z > this.max.z ) {
  2242. return false;
  2243. }
  2244. return true;
  2245. },
  2246. clampPoint: function ( point, optionalTarget ) {
  2247. var result = optionalTarget || new THREE.Vector3();
  2248. return result.copy( point ).clamp( this.min, this.max );
  2249. },
  2250. distanceToPoint: function () {
  2251. var v1 = new THREE.Vector3();
  2252. return function ( point ) {
  2253. var clampedPoint = v1.copy( point ).clamp( this.min, this.max );
  2254. return clampedPoint.sub( point ).length();
  2255. };
  2256. }(),
  2257. getBoundingSphere: function () {
  2258. var v1 = new THREE.Vector3();
  2259. return function ( optionalTarget ) {
  2260. var result = optionalTarget || new THREE.Sphere();
  2261. result.center = this.center();
  2262. result.radius = this.size( v1 ).length() * 0.5;
  2263. return result;
  2264. };
  2265. }(),
  2266. intersect: function ( box ) {
  2267. this.min.max( box.min );
  2268. this.max.min( box.max );
  2269. return this;
  2270. },
  2271. union: function ( box ) {
  2272. this.min.min( box.min );
  2273. this.max.max( box.max );
  2274. return this;
  2275. },
  2276. applyMatrix4: function () {
  2277. var points = [
  2278. new THREE.Vector3(),
  2279. new THREE.Vector3(),
  2280. new THREE.Vector3(),
  2281. new THREE.Vector3(),
  2282. new THREE.Vector3(),
  2283. new THREE.Vector3(),
  2284. new THREE.Vector3(),
  2285. new THREE.Vector3()
  2286. ];
  2287. return function ( matrix ) {
  2288. // NOTE: I am using a binary pattern to specify all 2^3 combinations below
  2289. points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000
  2290. points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001
  2291. points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010
  2292. points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011
  2293. points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100
  2294. points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101
  2295. points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110
  2296. points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111
  2297. this.makeEmpty();
  2298. this.setFromPoints( points );
  2299. return this;
  2300. };
  2301. }(),
  2302. translate: function ( offset ) {
  2303. this.min.add( offset );
  2304. this.max.add( offset );
  2305. return this;
  2306. },
  2307. equals: function ( box ) {
  2308. return box.min.equals( this.min ) && box.max.equals( this.max );
  2309. },
  2310. clone: function () {
  2311. return new THREE.Box3().copy( this );
  2312. }
  2313. };
  2314. // File:src/math/Matrix3.js
  2315. /**
  2316. * @author alteredq / http://alteredqualia.com/
  2317. * @author WestLangley / http://github.com/WestLangley
  2318. * @author bhouston / http://exocortex.com
  2319. */
  2320. THREE.Matrix3 = function () {
  2321. this.elements = new Float32Array( [
  2322. 1, 0, 0,
  2323. 0, 1, 0,
  2324. 0, 0, 1
  2325. ] );
  2326. if ( arguments.length > 0 ) {
  2327. console.error( 'THREE.Matrix3: the constructor no longer reads arguments. use .set() instead.' );
  2328. }
  2329. };
  2330. THREE.Matrix3.prototype = {
  2331. constructor: THREE.Matrix3,
  2332. set: function ( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) {
  2333. var te = this.elements;
  2334. te[ 0 ] = n11; te[ 3 ] = n12; te[ 6 ] = n13;
  2335. te[ 1 ] = n21; te[ 4 ] = n22; te[ 7 ] = n23;
  2336. te[ 2 ] = n31; te[ 5 ] = n32; te[ 8 ] = n33;
  2337. return this;
  2338. },
  2339. identity: function () {
  2340. this.set(
  2341. 1, 0, 0,
  2342. 0, 1, 0,
  2343. 0, 0, 1
  2344. );
  2345. return this;
  2346. },
  2347. copy: function ( m ) {
  2348. var me = m.elements;
  2349. this.set(
  2350. me[ 0 ], me[ 3 ], me[ 6 ],
  2351. me[ 1 ], me[ 4 ], me[ 7 ],
  2352. me[ 2 ], me[ 5 ], me[ 8 ]
  2353. );
  2354. return this;
  2355. },
  2356. multiplyVector3: function ( vector ) {
  2357. console.warn( 'THREE.Matrix3: .multiplyVector3() has been removed. Use vector.applyMatrix3( matrix ) instead.' );
  2358. return vector.applyMatrix3( this );
  2359. },
  2360. multiplyVector3Array: function ( a ) {
  2361. console.warn( 'THREE.Matrix3: .multiplyVector3Array() has been renamed. Use matrix.applyToVector3Array( array ) instead.' );
  2362. return this.applyToVector3Array( a );
  2363. },
  2364. applyToVector3Array: function () {
  2365. var v1 = new THREE.Vector3();
  2366. return function ( array, offset, length ) {
  2367. if ( offset === undefined ) offset = 0;
  2368. if ( length === undefined ) length = array.length;
  2369. for ( var i = 0, j = offset, il; i < length; i += 3, j += 3 ) {
  2370. v1.x = array[ j ];
  2371. v1.y = array[ j + 1 ];
  2372. v1.z = array[ j + 2 ];
  2373. v1.applyMatrix3( this );
  2374. array[ j ] = v1.x;
  2375. array[ j + 1 ] = v1.y;
  2376. array[ j + 2 ] = v1.z;
  2377. }
  2378. return array;
  2379. };
  2380. }(),
  2381. multiplyScalar: function ( s ) {
  2382. var te = this.elements;
  2383. te[ 0 ] *= s; te[ 3 ] *= s; te[ 6 ] *= s;
  2384. te[ 1 ] *= s; te[ 4 ] *= s; te[ 7 ] *= s;
  2385. te[ 2 ] *= s; te[ 5 ] *= s; te[ 8 ] *= s;
  2386. return this;
  2387. },
  2388. determinant: function () {
  2389. var te = this.elements;
  2390. var a = te[ 0 ], b = te[ 1 ], c = te[ 2 ],
  2391. d = te[ 3 ], e = te[ 4 ], f = te[ 5 ],
  2392. g = te[ 6 ], h = te[ 7 ], i = te[ 8 ];
  2393. return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g;
  2394. },
  2395. getInverse: function ( matrix, throwOnInvertible ) {
  2396. // input: THREE.Matrix4
  2397. // ( based on http://code.google.com/p/webgl-mjs/ )
  2398. var me = matrix.elements;
  2399. var te = this.elements;
  2400. te[ 0 ] = me[ 10 ] * me[ 5 ] - me[ 6 ] * me[ 9 ];
  2401. te[ 1 ] = - me[ 10 ] * me[ 1 ] + me[ 2 ] * me[ 9 ];
  2402. te[ 2 ] = me[ 6 ] * me[ 1 ] - me[ 2 ] * me[ 5 ];
  2403. te[ 3 ] = - me[ 10 ] * me[ 4 ] + me[ 6 ] * me[ 8 ];
  2404. te[ 4 ] = me[ 10 ] * me[ 0 ] - me[ 2 ] * me[ 8 ];
  2405. te[ 5 ] = - me[ 6 ] * me[ 0 ] + me[ 2 ] * me[ 4 ];
  2406. te[ 6 ] = me[ 9 ] * me[ 4 ] - me[ 5 ] * me[ 8 ];
  2407. te[ 7 ] = - me[ 9 ] * me[ 0 ] + me[ 1 ] * me[ 8 ];
  2408. te[ 8 ] = me[ 5 ] * me[ 0 ] - me[ 1 ] * me[ 4 ];
  2409. var det = me[ 0 ] * te[ 0 ] + me[ 1 ] * te[ 3 ] + me[ 2 ] * te[ 6 ];
  2410. // no inverse
  2411. if ( det === 0 ) {
  2412. var msg = "Matrix3.getInverse(): can't invert matrix, determinant is 0";
  2413. if ( throwOnInvertible || false ) {
  2414. throw new Error( msg );
  2415. } else {
  2416. console.warn( msg );
  2417. }
  2418. this.identity();
  2419. return this;
  2420. }
  2421. this.multiplyScalar( 1.0 / det );
  2422. return this;
  2423. },
  2424. transpose: function () {
  2425. var tmp, m = this.elements;
  2426. tmp = m[ 1 ]; m[ 1 ] = m[ 3 ]; m[ 3 ] = tmp;
  2427. tmp = m[ 2 ]; m[ 2 ] = m[ 6 ]; m[ 6 ] = tmp;
  2428. tmp = m[ 5 ]; m[ 5 ] = m[ 7 ]; m[ 7 ] = tmp;
  2429. return this;
  2430. },
  2431. flattenToArrayOffset: function ( array, offset ) {
  2432. var te = this.elements;
  2433. array[ offset ] = te[ 0 ];
  2434. array[ offset + 1 ] = te[ 1 ];
  2435. array[ offset + 2 ] = te[ 2 ];
  2436. array[ offset + 3 ] = te[ 3 ];
  2437. array[ offset + 4 ] = te[ 4 ];
  2438. array[ offset + 5 ] = te[ 5 ];
  2439. array[ offset + 6 ] = te[ 6 ];
  2440. array[ offset + 7 ] = te[ 7 ];
  2441. array[ offset + 8 ] = te[ 8 ];
  2442. return array;
  2443. },
  2444. getNormalMatrix: function ( m ) {
  2445. // input: THREE.Matrix4
  2446. this.getInverse( m ).transpose();
  2447. return this;
  2448. },
  2449. transposeIntoArray: function ( r ) {
  2450. var m = this.elements;
  2451. r[ 0 ] = m[ 0 ];
  2452. r[ 1 ] = m[ 3 ];
  2453. r[ 2 ] = m[ 6 ];
  2454. r[ 3 ] = m[ 1 ];
  2455. r[ 4 ] = m[ 4 ];
  2456. r[ 5 ] = m[ 7 ];
  2457. r[ 6 ] = m[ 2 ];
  2458. r[ 7 ] = m[ 5 ];
  2459. r[ 8 ] = m[ 8 ];
  2460. return this;
  2461. },
  2462. fromArray: function ( array ) {
  2463. this.elements.set( array );
  2464. return this;
  2465. },
  2466. toArray: function () {
  2467. var te = this.elements;
  2468. return [
  2469. te[ 0 ], te[ 1 ], te[ 2 ],
  2470. te[ 3 ], te[ 4 ], te[ 5 ],
  2471. te[ 6 ], te[ 7 ], te[ 8 ]
  2472. ];
  2473. },
  2474. clone: function () {
  2475. return new THREE.Matrix3().fromArray( this.elements );
  2476. }
  2477. };
  2478. // File:src/math/Matrix4.js
  2479. /**
  2480. * @author mrdoob / http://mrdoob.com/
  2481. * @author supereggbert / http://www.paulbrunt.co.uk/
  2482. * @author philogb / http://blog.thejit.org/
  2483. * @author jordi_ros / http://plattsoft.com
  2484. * @author D1plo1d / http://github.com/D1plo1d
  2485. * @author alteredq / http://alteredqualia.com/
  2486. * @author mikael emtinger / http://gomo.se/
  2487. * @author timknip / http://www.floorplanner.com/
  2488. * @author bhouston / http://exocortex.com
  2489. * @author WestLangley / http://github.com/WestLangley
  2490. */
  2491. THREE.Matrix4 = function () {
  2492. this.elements = new Float32Array( [
  2493. 1, 0, 0, 0,
  2494. 0, 1, 0, 0,
  2495. 0, 0, 1, 0,
  2496. 0, 0, 0, 1
  2497. ] );
  2498. if ( arguments.length > 0 ) {
  2499. console.error( 'THREE.Matrix4: the constructor no longer reads arguments. use .set() instead.' );
  2500. }
  2501. };
  2502. THREE.Matrix4.prototype = {
  2503. constructor: THREE.Matrix4,
  2504. set: function ( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) {
  2505. var te = this.elements;
  2506. te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14;
  2507. te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24;
  2508. te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34;
  2509. te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44;
  2510. return this;
  2511. },
  2512. identity: function () {
  2513. this.set(
  2514. 1, 0, 0, 0,
  2515. 0, 1, 0, 0,
  2516. 0, 0, 1, 0,
  2517. 0, 0, 0, 1
  2518. );
  2519. return this;
  2520. },
  2521. copy: function ( m ) {
  2522. this.elements.set( m.elements );
  2523. return this;
  2524. },
  2525. extractPosition: function ( m ) {
  2526. console.warn( 'THREE.Matrix4: .extractPosition() has been renamed to .copyPosition().' );
  2527. return this.copyPosition( m );
  2528. },
  2529. copyPosition: function ( m ) {
  2530. var te = this.elements;
  2531. var me = m.elements;
  2532. te[ 12 ] = me[ 12 ];
  2533. te[ 13 ] = me[ 13 ];
  2534. te[ 14 ] = me[ 14 ];
  2535. return this;
  2536. },
  2537. extractRotation: function () {
  2538. var v1 = new THREE.Vector3();
  2539. return function ( m ) {
  2540. var te = this.elements;
  2541. var me = m.elements;
  2542. var scaleX = 1 / v1.set( me[ 0 ], me[ 1 ], me[ 2 ] ).length();
  2543. var scaleY = 1 / v1.set( me[ 4 ], me[ 5 ], me[ 6 ] ).length();
  2544. var scaleZ = 1 / v1.set( me[ 8 ], me[ 9 ], me[ 10 ] ).length();
  2545. te[ 0 ] = me[ 0 ] * scaleX;
  2546. te[ 1 ] = me[ 1 ] * scaleX;
  2547. te[ 2 ] = me[ 2 ] * scaleX;
  2548. te[ 4 ] = me[ 4 ] * scaleY;
  2549. te[ 5 ] = me[ 5 ] * scaleY;
  2550. te[ 6 ] = me[ 6 ] * scaleY;
  2551. te[ 8 ] = me[ 8 ] * scaleZ;
  2552. te[ 9 ] = me[ 9 ] * scaleZ;
  2553. te[ 10 ] = me[ 10 ] * scaleZ;
  2554. return this;
  2555. };
  2556. }(),
  2557. makeRotationFromEuler: function ( euler ) {
  2558. if ( euler instanceof THREE.Euler === false ) {
  2559. console.error( 'THREE.Matrix: .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order.' );
  2560. }
  2561. var te = this.elements;
  2562. var x = euler.x, y = euler.y, z = euler.z;
  2563. var a = Math.cos( x ), b = Math.sin( x );
  2564. var c = Math.cos( y ), d = Math.sin( y );
  2565. var e = Math.cos( z ), f = Math.sin( z );
  2566. if ( euler.order === 'XYZ' ) {
  2567. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  2568. te[ 0 ] = c * e;
  2569. te[ 4 ] = - c * f;
  2570. te[ 8 ] = d;
  2571. te[ 1 ] = af + be * d;
  2572. te[ 5 ] = ae - bf * d;
  2573. te[ 9 ] = - b * c;
  2574. te[ 2 ] = bf - ae * d;
  2575. te[ 6 ] = be + af * d;
  2576. te[ 10 ] = a * c;
  2577. } else if ( euler.order === 'YXZ' ) {
  2578. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  2579. te[ 0 ] = ce + df * b;
  2580. te[ 4 ] = de * b - cf;
  2581. te[ 8 ] = a * d;
  2582. te[ 1 ] = a * f;
  2583. te[ 5 ] = a * e;
  2584. te[ 9 ] = - b;
  2585. te[ 2 ] = cf * b - de;
  2586. te[ 6 ] = df + ce * b;
  2587. te[ 10 ] = a * c;
  2588. } else if ( euler.order === 'ZXY' ) {
  2589. var ce = c * e, cf = c * f, de = d * e, df = d * f;
  2590. te[ 0 ] = ce - df * b;
  2591. te[ 4 ] = - a * f;
  2592. te[ 8 ] = de + cf * b;
  2593. te[ 1 ] = cf + de * b;
  2594. te[ 5 ] = a * e;
  2595. te[ 9 ] = df - ce * b;
  2596. te[ 2 ] = - a * d;
  2597. te[ 6 ] = b;
  2598. te[ 10 ] = a * c;
  2599. } else if ( euler.order === 'ZYX' ) {
  2600. var ae = a * e, af = a * f, be = b * e, bf = b * f;
  2601. te[ 0 ] = c * e;
  2602. te[ 4 ] = be * d - af;
  2603. te[ 8 ] = ae * d + bf;
  2604. te[ 1 ] = c * f;
  2605. te[ 5 ] = bf * d + ae;
  2606. te[ 9 ] = af * d - be;
  2607. te[ 2 ] = - d;
  2608. te[ 6 ] = b * c;
  2609. te[ 10 ] = a * c;
  2610. } else if ( euler.order === 'YZX' ) {
  2611. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  2612. te[ 0 ] = c * e;
  2613. te[ 4 ] = bd - ac * f;
  2614. te[ 8 ] = bc * f + ad;
  2615. te[ 1 ] = f;
  2616. te[ 5 ] = a * e;
  2617. te[ 9 ] = - b * e;
  2618. te[ 2 ] = - d * e;
  2619. te[ 6 ] = ad * f + bc;
  2620. te[ 10 ] = ac - bd * f;
  2621. } else if ( euler.order === 'XZY' ) {
  2622. var ac = a * c, ad = a * d, bc = b * c, bd = b * d;
  2623. te[ 0 ] = c * e;
  2624. te[ 4 ] = - f;
  2625. te[ 8 ] = d * e;
  2626. te[ 1 ] = ac * f + bd;
  2627. te[ 5 ] = a * e;
  2628. te[ 9 ] = ad * f - bc;
  2629. te[ 2 ] = bc * f - ad;
  2630. te[ 6 ] = b * e;
  2631. te[ 10 ] = bd * f + ac;
  2632. }
  2633. // last column
  2634. te[ 3 ] = 0;
  2635. te[ 7 ] = 0;
  2636. te[ 11 ] = 0;
  2637. // bottom row
  2638. te[ 12 ] = 0;
  2639. te[ 13 ] = 0;
  2640. te[ 14 ] = 0;
  2641. te[ 15 ] = 1;
  2642. return this;
  2643. },
  2644. setRotationFromQuaternion: function ( q ) {
  2645. console.warn( 'THREE.Matrix4: .setRotationFromQuaternion() has been renamed to .makeRotationFromQuaternion().' );
  2646. return this.makeRotationFromQuaternion( q );
  2647. },
  2648. makeRotationFromQuaternion: function ( q ) {
  2649. var te = this.elements;
  2650. var x = q.x, y = q.y, z = q.z, w = q.w;
  2651. var x2 = x + x, y2 = y + y, z2 = z + z;
  2652. var xx = x * x2, xy = x * y2, xz = x * z2;
  2653. var yy = y * y2, yz = y * z2, zz = z * z2;
  2654. var wx = w * x2, wy = w * y2, wz = w * z2;
  2655. te[ 0 ] = 1 - ( yy + zz );
  2656. te[ 4 ] = xy - wz;
  2657. te[ 8 ] = xz + wy;
  2658. te[ 1 ] = xy + wz;
  2659. te[ 5 ] = 1 - ( xx + zz );
  2660. te[ 9 ] = yz - wx;
  2661. te[ 2 ] = xz - wy;
  2662. te[ 6 ] = yz + wx;
  2663. te[ 10 ] = 1 - ( xx + yy );
  2664. // last column
  2665. te[ 3 ] = 0;
  2666. te[ 7 ] = 0;
  2667. te[ 11 ] = 0;
  2668. // bottom row
  2669. te[ 12 ] = 0;
  2670. te[ 13 ] = 0;
  2671. te[ 14 ] = 0;
  2672. te[ 15 ] = 1;
  2673. return this;
  2674. },
  2675. lookAt: function () {
  2676. var x = new THREE.Vector3();
  2677. var y = new THREE.Vector3();
  2678. var z = new THREE.Vector3();
  2679. return function ( eye, target, up ) {
  2680. var te = this.elements;
  2681. z.subVectors( eye, target ).normalize();
  2682. if ( z.length() === 0 ) {
  2683. z.z = 1;
  2684. }
  2685. x.crossVectors( up, z ).normalize();
  2686. if ( x.length() === 0 ) {
  2687. z.x += 0.0001;
  2688. x.crossVectors( up, z ).normalize();
  2689. }
  2690. y.crossVectors( z, x );
  2691. te[ 0 ] = x.x; te[ 4 ] = y.x; te[ 8 ] = z.x;
  2692. te[ 1 ] = x.y; te[ 5 ] = y.y; te[ 9 ] = z.y;
  2693. te[ 2 ] = x.z; te[ 6 ] = y.z; te[ 10 ] = z.z;
  2694. return this;
  2695. };
  2696. }(),
  2697. multiply: function ( m, n ) {
  2698. if ( n !== undefined ) {
  2699. console.warn( 'THREE.Matrix4: .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.' );
  2700. return this.multiplyMatrices( m, n );
  2701. }
  2702. return this.multiplyMatrices( this, m );
  2703. },
  2704. multiplyMatrices: function ( a, b ) {
  2705. var ae = a.elements;
  2706. var be = b.elements;
  2707. var te = this.elements;
  2708. var a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ];
  2709. var a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ];
  2710. var a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ];
  2711. var a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ];
  2712. var b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ];
  2713. var b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ];
  2714. var b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ];
  2715. var b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ];
  2716. te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41;
  2717. te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42;
  2718. te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43;
  2719. te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44;
  2720. te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41;
  2721. te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42;
  2722. te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43;
  2723. te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44;
  2724. te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41;
  2725. te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42;
  2726. te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43;
  2727. te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44;
  2728. te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41;
  2729. te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42;
  2730. te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43;
  2731. te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44;
  2732. return this;
  2733. },
  2734. multiplyToArray: function ( a, b, r ) {
  2735. var te = this.elements;
  2736. this.multiplyMatrices( a, b );
  2737. r[ 0 ] = te[ 0 ]; r[ 1 ] = te[ 1 ]; r[ 2 ] = te[ 2 ]; r[ 3 ] = te[ 3 ];
  2738. r[ 4 ] = te[ 4 ]; r[ 5 ] = te[ 5 ]; r[ 6 ] = te[ 6 ]; r[ 7 ] = te[ 7 ];
  2739. r[ 8 ] = te[ 8 ]; r[ 9 ] = te[ 9 ]; r[ 10 ] = te[ 10 ]; r[ 11 ] = te[ 11 ];
  2740. r[ 12 ] = te[ 12 ]; r[ 13 ] = te[ 13 ]; r[ 14 ] = te[ 14 ]; r[ 15 ] = te[ 15 ];
  2741. return this;
  2742. },
  2743. multiplyScalar: function ( s ) {
  2744. var te = this.elements;
  2745. te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s;
  2746. te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s;
  2747. te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s;
  2748. te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s;
  2749. return this;
  2750. },
  2751. multiplyVector3: function ( vector ) {
  2752. console.warn( 'THREE.Matrix4: .multiplyVector3() has been removed. Use vector.applyMatrix4( matrix ) or vector.applyProjection( matrix ) instead.' );
  2753. return vector.applyProjection( this );
  2754. },
  2755. multiplyVector4: function ( vector ) {
  2756. console.warn( 'THREE.Matrix4: .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
  2757. return vector.applyMatrix4( this );
  2758. },
  2759. multiplyVector3Array: function ( a ) {
  2760. console.warn( 'THREE.Matrix4: .multiplyVector3Array() has been renamed. Use matrix.applyToVector3Array( array ) instead.' );
  2761. return this.applyToVector3Array( a );
  2762. },
  2763. applyToVector3Array: function () {
  2764. var v1 = new THREE.Vector3();
  2765. return function ( array, offset, length ) {
  2766. if ( offset === undefined ) offset = 0;
  2767. if ( length === undefined ) length = array.length;
  2768. for ( var i = 0, j = offset, il; i < length; i += 3, j += 3 ) {
  2769. v1.x = array[ j ];
  2770. v1.y = array[ j + 1 ];
  2771. v1.z = array[ j + 2 ];
  2772. v1.applyMatrix4( this );
  2773. array[ j ] = v1.x;
  2774. array[ j + 1 ] = v1.y;
  2775. array[ j + 2 ] = v1.z;
  2776. }
  2777. return array;
  2778. };
  2779. }(),
  2780. rotateAxis: function ( v ) {
  2781. console.warn( 'THREE.Matrix4: .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' );
  2782. v.transformDirection( this );
  2783. },
  2784. crossVector: function ( vector ) {
  2785. console.warn( 'THREE.Matrix4: .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' );
  2786. return vector.applyMatrix4( this );
  2787. },
  2788. determinant: function () {
  2789. var te = this.elements;
  2790. var n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ];
  2791. var n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ];
  2792. var n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ];
  2793. var n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ];
  2794. //TODO: make this more efficient
  2795. //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm )
  2796. return (
  2797. n41 * (
  2798. + n14 * n23 * n32
  2799. - n13 * n24 * n32
  2800. - n14 * n22 * n33
  2801. + n12 * n24 * n33
  2802. + n13 * n22 * n34
  2803. - n12 * n23 * n34
  2804. ) +
  2805. n42 * (
  2806. + n11 * n23 * n34
  2807. - n11 * n24 * n33
  2808. + n14 * n21 * n33
  2809. - n13 * n21 * n34
  2810. + n13 * n24 * n31
  2811. - n14 * n23 * n31
  2812. ) +
  2813. n43 * (
  2814. + n11 * n24 * n32
  2815. - n11 * n22 * n34
  2816. - n14 * n21 * n32
  2817. + n12 * n21 * n34
  2818. + n14 * n22 * n31
  2819. - n12 * n24 * n31
  2820. ) +
  2821. n44 * (
  2822. - n13 * n22 * n31
  2823. - n11 * n23 * n32
  2824. + n11 * n22 * n33
  2825. + n13 * n21 * n32
  2826. - n12 * n21 * n33
  2827. + n12 * n23 * n31
  2828. )
  2829. );
  2830. },
  2831. transpose: function () {
  2832. var te = this.elements;
  2833. var tmp;
  2834. tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp;
  2835. tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp;
  2836. tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp;
  2837. tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp;
  2838. tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp;
  2839. tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp;
  2840. return this;
  2841. },
  2842. flattenToArrayOffset: function ( array, offset ) {
  2843. var te = this.elements;
  2844. array[ offset ] = te[ 0 ];
  2845. array[ offset + 1 ] = te[ 1 ];
  2846. array[ offset + 2 ] = te[ 2 ];
  2847. array[ offset + 3 ] = te[ 3 ];
  2848. array[ offset + 4 ] = te[ 4 ];
  2849. array[ offset + 5 ] = te[ 5 ];
  2850. array[ offset + 6 ] = te[ 6 ];
  2851. array[ offset + 7 ] = te[ 7 ];
  2852. array[ offset + 8 ] = te[ 8 ];
  2853. array[ offset + 9 ] = te[ 9 ];
  2854. array[ offset + 10 ] = te[ 10 ];
  2855. array[ offset + 11 ] = te[ 11 ];
  2856. array[ offset + 12 ] = te[ 12 ];
  2857. array[ offset + 13 ] = te[ 13 ];
  2858. array[ offset + 14 ] = te[ 14 ];
  2859. array[ offset + 15 ] = te[ 15 ];
  2860. return array;
  2861. },
  2862. getPosition: function () {
  2863. var v1 = new THREE.Vector3();
  2864. return function () {
  2865. console.warn( 'THREE.Matrix4: .getPosition() has been removed. Use Vector3.setFromMatrixPosition( matrix ) instead.' );
  2866. var te = this.elements;
  2867. return v1.set( te[ 12 ], te[ 13 ], te[ 14 ] );
  2868. };
  2869. }(),
  2870. setPosition: function ( v ) {
  2871. var te = this.elements;
  2872. te[ 12 ] = v.x;
  2873. te[ 13 ] = v.y;
  2874. te[ 14 ] = v.z;
  2875. return this;
  2876. },
  2877. getInverse: function ( m, throwOnInvertible ) {
  2878. // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm
  2879. var te = this.elements;
  2880. var me = m.elements;
  2881. var n11 = me[ 0 ], n12 = me[ 4 ], n13 = me[ 8 ], n14 = me[ 12 ];
  2882. var n21 = me[ 1 ], n22 = me[ 5 ], n23 = me[ 9 ], n24 = me[ 13 ];
  2883. var n31 = me[ 2 ], n32 = me[ 6 ], n33 = me[ 10 ], n34 = me[ 14 ];
  2884. var n41 = me[ 3 ], n42 = me[ 7 ], n43 = me[ 11 ], n44 = me[ 15 ];
  2885. te[ 0 ] = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44;
  2886. te[ 4 ] = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44;
  2887. te[ 8 ] = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44;
  2888. te[ 12 ] = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34;
  2889. te[ 1 ] = n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44;
  2890. te[ 5 ] = n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44;
  2891. te[ 9 ] = n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44;
  2892. te[ 13 ] = n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34;
  2893. te[ 2 ] = n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44;
  2894. te[ 6 ] = n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44;
  2895. te[ 10 ] = n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44;
  2896. te[ 14 ] = n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34;
  2897. te[ 3 ] = n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43;
  2898. te[ 7 ] = n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43;
  2899. te[ 11 ] = n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43;
  2900. te[ 15 ] = n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33;
  2901. var det = n11 * te[ 0 ] + n21 * te[ 4 ] + n31 * te[ 8 ] + n41 * te[ 12 ];
  2902. if ( det == 0 ) {
  2903. var msg = "Matrix4.getInverse(): can't invert matrix, determinant is 0";
  2904. if ( throwOnInvertible || false ) {
  2905. throw new Error( msg );
  2906. } else {
  2907. console.warn( msg );
  2908. }
  2909. this.identity();
  2910. return this;
  2911. }
  2912. this.multiplyScalar( 1 / det );
  2913. return this;
  2914. },
  2915. translate: function ( v ) {
  2916. console.warn( 'THREE.Matrix4: .translate() has been removed.' );
  2917. },
  2918. rotateX: function ( angle ) {
  2919. console.warn( 'THREE.Matrix4: .rotateX() has been removed.' );
  2920. },
  2921. rotateY: function ( angle ) {
  2922. console.warn( 'THREE.Matrix4: .rotateY() has been removed.' );
  2923. },
  2924. rotateZ: function ( angle ) {
  2925. console.warn( 'THREE.Matrix4: .rotateZ() has been removed.' );
  2926. },
  2927. rotateByAxis: function ( axis, angle ) {
  2928. console.warn( 'THREE.Matrix4: .rotateByAxis() has been removed.' );
  2929. },
  2930. scale: function ( v ) {
  2931. var te = this.elements;
  2932. var x = v.x, y = v.y, z = v.z;
  2933. te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z;
  2934. te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z;
  2935. te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z;
  2936. te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z;
  2937. return this;
  2938. },
  2939. getMaxScaleOnAxis: function () {
  2940. var te = this.elements;
  2941. var scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ];
  2942. var scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ];
  2943. var scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ];
  2944. return Math.sqrt( Math.max( scaleXSq, Math.max( scaleYSq, scaleZSq ) ) );
  2945. },
  2946. makeTranslation: function ( x, y, z ) {
  2947. this.set(
  2948. 1, 0, 0, x,
  2949. 0, 1, 0, y,
  2950. 0, 0, 1, z,
  2951. 0, 0, 0, 1
  2952. );
  2953. return this;
  2954. },
  2955. makeRotationX: function ( theta ) {
  2956. var c = Math.cos( theta ), s = Math.sin( theta );
  2957. this.set(
  2958. 1, 0, 0, 0,
  2959. 0, c, - s, 0,
  2960. 0, s, c, 0,
  2961. 0, 0, 0, 1
  2962. );
  2963. return this;
  2964. },
  2965. makeRotationY: function ( theta ) {
  2966. var c = Math.cos( theta ), s = Math.sin( theta );
  2967. this.set(
  2968. c, 0, s, 0,
  2969. 0, 1, 0, 0,
  2970. - s, 0, c, 0,
  2971. 0, 0, 0, 1
  2972. );
  2973. return this;
  2974. },
  2975. makeRotationZ: function ( theta ) {
  2976. var c = Math.cos( theta ), s = Math.sin( theta );
  2977. this.set(
  2978. c, - s, 0, 0,
  2979. s, c, 0, 0,
  2980. 0, 0, 1, 0,
  2981. 0, 0, 0, 1
  2982. );
  2983. return this;
  2984. },
  2985. makeRotationAxis: function ( axis, angle ) {
  2986. // Based on http://www.gamedev.net/reference/articles/article1199.asp
  2987. var c = Math.cos( angle );
  2988. var s = Math.sin( angle );
  2989. var t = 1 - c;
  2990. var x = axis.x, y = axis.y, z = axis.z;
  2991. var tx = t * x, ty = t * y;
  2992. this.set(
  2993. tx * x + c, tx * y - s * z, tx * z + s * y, 0,
  2994. tx * y + s * z, ty * y + c, ty * z - s * x, 0,
  2995. tx * z - s * y, ty * z + s * x, t * z * z + c, 0,
  2996. 0, 0, 0, 1
  2997. );
  2998. return this;
  2999. },
  3000. makeScale: function ( x, y, z ) {
  3001. this.set(
  3002. x, 0, 0, 0,
  3003. 0, y, 0, 0,
  3004. 0, 0, z, 0,
  3005. 0, 0, 0, 1
  3006. );
  3007. return this;
  3008. },
  3009. compose: function ( position, quaternion, scale ) {
  3010. this.makeRotationFromQuaternion( quaternion );
  3011. this.scale( scale );
  3012. this.setPosition( position );
  3013. return this;
  3014. },
  3015. decompose: function () {
  3016. var vector = new THREE.Vector3();
  3017. var matrix = new THREE.Matrix4();
  3018. return function ( position, quaternion, scale ) {
  3019. var te = this.elements;
  3020. var sx = vector.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length();
  3021. var sy = vector.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length();
  3022. var sz = vector.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length();
  3023. // if determine is negative, we need to invert one scale
  3024. var det = this.determinant();
  3025. if ( det < 0 ) {
  3026. sx = - sx;
  3027. }
  3028. position.x = te[ 12 ];
  3029. position.y = te[ 13 ];
  3030. position.z = te[ 14 ];
  3031. // scale the rotation part
  3032. matrix.elements.set( this.elements ); // at this point matrix is incomplete so we can't use .copy()
  3033. var invSX = 1 / sx;
  3034. var invSY = 1 / sy;
  3035. var invSZ = 1 / sz;
  3036. matrix.elements[ 0 ] *= invSX;
  3037. matrix.elements[ 1 ] *= invSX;
  3038. matrix.elements[ 2 ] *= invSX;
  3039. matrix.elements[ 4 ] *= invSY;
  3040. matrix.elements[ 5 ] *= invSY;
  3041. matrix.elements[ 6 ] *= invSY;
  3042. matrix.elements[ 8 ] *= invSZ;
  3043. matrix.elements[ 9 ] *= invSZ;
  3044. matrix.elements[ 10 ] *= invSZ;
  3045. quaternion.setFromRotationMatrix( matrix );
  3046. scale.x = sx;
  3047. scale.y = sy;
  3048. scale.z = sz;
  3049. return this;
  3050. };
  3051. }(),
  3052. makeFrustum: function ( left, right, bottom, top, near, far ) {
  3053. var te = this.elements;
  3054. var x = 2 * near / ( right - left );
  3055. var y = 2 * near / ( top - bottom );
  3056. var a = ( right + left ) / ( right - left );
  3057. var b = ( top + bottom ) / ( top - bottom );
  3058. var c = - ( far + near ) / ( far - near );
  3059. var d = - 2 * far * near / ( far - near );
  3060. te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0;
  3061. te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0;
  3062. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d;
  3063. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0;
  3064. return this;
  3065. },
  3066. makePerspective: function ( fov, aspect, near, far ) {
  3067. var ymax = near * Math.tan( THREE.Math.degToRad( fov * 0.5 ) );
  3068. var ymin = - ymax;
  3069. var xmin = ymin * aspect;
  3070. var xmax = ymax * aspect;
  3071. return this.makeFrustum( xmin, xmax, ymin, ymax, near, far );
  3072. },
  3073. makeOrthographic: function ( left, right, top, bottom, near, far ) {
  3074. var te = this.elements;
  3075. var w = right - left;
  3076. var h = top - bottom;
  3077. var p = far - near;
  3078. var x = ( right + left ) / w;
  3079. var y = ( top + bottom ) / h;
  3080. var z = ( far + near ) / p;
  3081. te[ 0 ] = 2 / w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x;
  3082. te[ 1 ] = 0; te[ 5 ] = 2 / h; te[ 9 ] = 0; te[ 13 ] = - y;
  3083. te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = - 2 / p; te[ 14 ] = - z;
  3084. te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1;
  3085. return this;
  3086. },
  3087. fromArray: function ( array ) {
  3088. this.elements.set( array );
  3089. return this;
  3090. },
  3091. toArray: function () {
  3092. var te = this.elements;
  3093. return [
  3094. te[ 0 ], te[ 1 ], te[ 2 ], te[ 3 ],
  3095. te[ 4 ], te[ 5 ], te[ 6 ], te[ 7 ],
  3096. te[ 8 ], te[ 9 ], te[ 10 ], te[ 11 ],
  3097. te[ 12 ], te[ 13 ], te[ 14 ], te[ 15 ]
  3098. ];
  3099. },
  3100. clone: function () {
  3101. return new THREE.Matrix4().fromArray( this.elements );
  3102. }
  3103. };
  3104. // File:src/math/Ray.js
  3105. /**
  3106. * @author bhouston / http://exocortex.com
  3107. */
  3108. THREE.Ray = function ( origin, direction ) {
  3109. this.origin = ( origin !== undefined ) ? origin : new THREE.Vector3();
  3110. this.direction = ( direction !== undefined ) ? direction : new THREE.Vector3();
  3111. };
  3112. THREE.Ray.prototype = {
  3113. constructor: THREE.Ray,
  3114. set: function ( origin, direction ) {
  3115. this.origin.copy( origin );
  3116. this.direction.copy( direction );
  3117. return this;
  3118. },
  3119. copy: function ( ray ) {
  3120. this.origin.copy( ray.origin );
  3121. this.direction.copy( ray.direction );
  3122. return this;
  3123. },
  3124. at: function ( t, optionalTarget ) {
  3125. var result = optionalTarget || new THREE.Vector3();
  3126. return result.copy( this.direction ).multiplyScalar( t ).add( this.origin );
  3127. },
  3128. recast: function () {
  3129. var v1 = new THREE.Vector3();
  3130. return function ( t ) {
  3131. this.origin.copy( this.at( t, v1 ) );
  3132. return this;
  3133. };
  3134. }(),
  3135. closestPointToPoint: function ( point, optionalTarget ) {
  3136. var result = optionalTarget || new THREE.Vector3();
  3137. result.subVectors( point, this.origin );
  3138. var directionDistance = result.dot( this.direction );
  3139. if ( directionDistance < 0 ) {
  3140. return result.copy( this.origin );
  3141. }
  3142. return result.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  3143. },
  3144. distanceToPoint: function () {
  3145. var v1 = new THREE.Vector3();
  3146. return function ( point ) {
  3147. var directionDistance = v1.subVectors( point, this.origin ).dot( this.direction );
  3148. // point behind the ray
  3149. if ( directionDistance < 0 ) {
  3150. return this.origin.distanceTo( point );
  3151. }
  3152. v1.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin );
  3153. return v1.distanceTo( point );
  3154. };
  3155. }(),
  3156. distanceSqToSegment: function ( v0, v1, optionalPointOnRay, optionalPointOnSegment ) {
  3157. // from http://www.geometrictools.com/LibMathematics/Distance/Wm5DistRay3Segment3.cpp
  3158. // It returns the min distance between the ray and the segment
  3159. // defined by v0 and v1
  3160. // It can also set two optional targets :
  3161. // - The closest point on the ray
  3162. // - The closest point on the segment
  3163. var segCenter = v0.clone().add( v1 ).multiplyScalar( 0.5 );
  3164. var segDir = v1.clone().sub( v0 ).normalize();
  3165. var segExtent = v0.distanceTo( v1 ) * 0.5;
  3166. var diff = this.origin.clone().sub( segCenter );
  3167. var a01 = - this.direction.dot( segDir );
  3168. var b0 = diff.dot( this.direction );
  3169. var b1 = - diff.dot( segDir );
  3170. var c = diff.lengthSq();
  3171. var det = Math.abs( 1 - a01 * a01 );
  3172. var s0, s1, sqrDist, extDet;
  3173. if ( det >= 0 ) {
  3174. // The ray and segment are not parallel.
  3175. s0 = a01 * b1 - b0;
  3176. s1 = a01 * b0 - b1;
  3177. extDet = segExtent * det;
  3178. if ( s0 >= 0 ) {
  3179. if ( s1 >= - extDet ) {
  3180. if ( s1 <= extDet ) {
  3181. // region 0
  3182. // Minimum at interior points of ray and segment.
  3183. var invDet = 1 / det;
  3184. s0 *= invDet;
  3185. s1 *= invDet;
  3186. sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c;
  3187. } else {
  3188. // region 1
  3189. s1 = segExtent;
  3190. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3191. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3192. }
  3193. } else {
  3194. // region 5
  3195. s1 = - segExtent;
  3196. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3197. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3198. }
  3199. } else {
  3200. if ( s1 <= - extDet ) {
  3201. // region 4
  3202. s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) );
  3203. s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3204. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3205. } else if ( s1 <= extDet ) {
  3206. // region 3
  3207. s0 = 0;
  3208. s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3209. sqrDist = s1 * ( s1 + 2 * b1 ) + c;
  3210. } else {
  3211. // region 2
  3212. s0 = Math.max( 0, - ( a01 * segExtent + b0 ) );
  3213. s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent );
  3214. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3215. }
  3216. }
  3217. } else {
  3218. // Ray and segment are parallel.
  3219. s1 = ( a01 > 0 ) ? - segExtent : segExtent;
  3220. s0 = Math.max( 0, - ( a01 * s1 + b0 ) );
  3221. sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c;
  3222. }
  3223. if ( optionalPointOnRay ) {
  3224. optionalPointOnRay.copy( this.direction.clone().multiplyScalar( s0 ).add( this.origin ) );
  3225. }
  3226. if ( optionalPointOnSegment ) {
  3227. optionalPointOnSegment.copy( segDir.clone().multiplyScalar( s1 ).add( segCenter ) );
  3228. }
  3229. return sqrDist;
  3230. },
  3231. isIntersectionSphere: function ( sphere ) {
  3232. return this.distanceToPoint( sphere.center ) <= sphere.radius;
  3233. },
  3234. intersectSphere: function () {
  3235. // from http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-sphere-intersection/
  3236. var v1 = new THREE.Vector3();
  3237. return function ( sphere, optionalTarget ) {
  3238. v1.subVectors( sphere.center, this.origin );
  3239. var tca = v1.dot( this.direction );
  3240. var d2 = v1.dot( v1 ) - tca * tca;
  3241. var radius2 = sphere.radius * sphere.radius;
  3242. if ( d2 > radius2 ) return null;
  3243. var thc = Math.sqrt( radius2 - d2 );
  3244. // t0 = first intersect point - entrance on front of sphere
  3245. var t0 = tca - thc;
  3246. // t1 = second intersect point - exit point on back of sphere
  3247. var t1 = tca + thc;
  3248. // test to see if both t0 and t1 are behind the ray - if so, return null
  3249. if ( t0 < 0 && t1 < 0 ) return null;
  3250. // test to see if t0 is behind the ray:
  3251. // if it is, the ray is inside the sphere, so return the second exit point scaled by t1,
  3252. // in order to always return an intersect point that is in front of the ray.
  3253. if ( t0 < 0 ) return this.at( t1, optionalTarget );
  3254. // else t0 is in front of the ray, so return the first collision point scaled by t0
  3255. return this.at( t0, optionalTarget );
  3256. }
  3257. }(),
  3258. isIntersectionPlane: function ( plane ) {
  3259. // check if the ray lies on the plane first
  3260. var distToPoint = plane.distanceToPoint( this.origin );
  3261. if ( distToPoint === 0 ) {
  3262. return true;
  3263. }
  3264. var denominator = plane.normal.dot( this.direction );
  3265. if ( denominator * distToPoint < 0 ) {
  3266. return true;
  3267. }
  3268. // ray origin is behind the plane (and is pointing behind it)
  3269. return false;
  3270. },
  3271. distanceToPlane: function ( plane ) {
  3272. var denominator = plane.normal.dot( this.direction );
  3273. if ( denominator == 0 ) {
  3274. // line is coplanar, return origin
  3275. if ( plane.distanceToPoint( this.origin ) == 0 ) {
  3276. return 0;
  3277. }
  3278. // Null is preferable to undefined since undefined means.... it is undefined
  3279. return null;
  3280. }
  3281. var t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator;
  3282. // Return if the ray never intersects the plane
  3283. return t >= 0 ? t : null;
  3284. },
  3285. intersectPlane: function ( plane, optionalTarget ) {
  3286. var t = this.distanceToPlane( plane );
  3287. if ( t === null ) {
  3288. return null;
  3289. }
  3290. return this.at( t, optionalTarget );
  3291. },
  3292. isIntersectionBox: function () {
  3293. var v = new THREE.Vector3();
  3294. return function ( box ) {
  3295. return this.intersectBox( box, v ) !== null;
  3296. };
  3297. }(),
  3298. intersectBox: function ( box , optionalTarget ) {
  3299. // http://www.scratchapixel.com/lessons/3d-basic-lessons/lesson-7-intersecting-simple-shapes/ray-box-intersection/
  3300. var tmin,tmax,tymin,tymax,tzmin,tzmax;
  3301. var invdirx = 1 / this.direction.x,
  3302. invdiry = 1 / this.direction.y,
  3303. invdirz = 1 / this.direction.z;
  3304. var origin = this.origin;
  3305. if ( invdirx >= 0 ) {
  3306. tmin = ( box.min.x - origin.x ) * invdirx;
  3307. tmax = ( box.max.x - origin.x ) * invdirx;
  3308. } else {
  3309. tmin = ( box.max.x - origin.x ) * invdirx;
  3310. tmax = ( box.min.x - origin.x ) * invdirx;
  3311. }
  3312. if ( invdiry >= 0 ) {
  3313. tymin = ( box.min.y - origin.y ) * invdiry;
  3314. tymax = ( box.max.y - origin.y ) * invdiry;
  3315. } else {
  3316. tymin = ( box.max.y - origin.y ) * invdiry;
  3317. tymax = ( box.min.y - origin.y ) * invdiry;
  3318. }
  3319. if ( ( tmin > tymax ) || ( tymin > tmax ) ) return null;
  3320. // These lines also handle the case where tmin or tmax is NaN
  3321. // (result of 0 * Infinity). x !== x returns true if x is NaN
  3322. if ( tymin > tmin || tmin !== tmin ) tmin = tymin;
  3323. if ( tymax < tmax || tmax !== tmax ) tmax = tymax;
  3324. if ( invdirz >= 0 ) {
  3325. tzmin = ( box.min.z - origin.z ) * invdirz;
  3326. tzmax = ( box.max.z - origin.z ) * invdirz;
  3327. } else {
  3328. tzmin = ( box.max.z - origin.z ) * invdirz;
  3329. tzmax = ( box.min.z - origin.z ) * invdirz;
  3330. }
  3331. if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) return null;
  3332. if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin;
  3333. if ( tzmax < tmax || tmax !== tmax ) tmax = tzmax;
  3334. //return point closest to the ray (positive side)
  3335. if ( tmax < 0 ) return null;
  3336. return this.at( tmin >= 0 ? tmin : tmax, optionalTarget );
  3337. },
  3338. intersectTriangle: function () {
  3339. // Compute the offset origin, edges, and normal.
  3340. var diff = new THREE.Vector3();
  3341. var edge1 = new THREE.Vector3();
  3342. var edge2 = new THREE.Vector3();
  3343. var normal = new THREE.Vector3();
  3344. return function ( a, b, c, backfaceCulling, optionalTarget ) {
  3345. // from http://www.geometrictools.com/LibMathematics/Intersection/Wm5IntrRay3Triangle3.cpp
  3346. edge1.subVectors( b, a );
  3347. edge2.subVectors( c, a );
  3348. normal.crossVectors( edge1, edge2 );
  3349. // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction,
  3350. // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by
  3351. // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2))
  3352. // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q))
  3353. // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N)
  3354. var DdN = this.direction.dot( normal );
  3355. var sign;
  3356. if ( DdN > 0 ) {
  3357. if ( backfaceCulling ) return null;
  3358. sign = 1;
  3359. } else if ( DdN < 0 ) {
  3360. sign = - 1;
  3361. DdN = - DdN;
  3362. } else {
  3363. return null;
  3364. }
  3365. diff.subVectors( this.origin, a );
  3366. var DdQxE2 = sign * this.direction.dot( edge2.crossVectors( diff, edge2 ) );
  3367. // b1 < 0, no intersection
  3368. if ( DdQxE2 < 0 ) {
  3369. return null;
  3370. }
  3371. var DdE1xQ = sign * this.direction.dot( edge1.cross( diff ) );
  3372. // b2 < 0, no intersection
  3373. if ( DdE1xQ < 0 ) {
  3374. return null;
  3375. }
  3376. // b1+b2 > 1, no intersection
  3377. if ( DdQxE2 + DdE1xQ > DdN ) {
  3378. return null;
  3379. }
  3380. // Line intersects triangle, check if ray does.
  3381. var QdN = - sign * diff.dot( normal );
  3382. // t < 0, no intersection
  3383. if ( QdN < 0 ) {
  3384. return null;
  3385. }
  3386. // Ray intersects triangle.
  3387. return this.at( QdN / DdN, optionalTarget );
  3388. };
  3389. }(),
  3390. applyMatrix4: function ( matrix4 ) {
  3391. this.direction.add( this.origin ).applyMatrix4( matrix4 );
  3392. this.origin.applyMatrix4( matrix4 );
  3393. this.direction.sub( this.origin );
  3394. this.direction.normalize();
  3395. return this;
  3396. },
  3397. equals: function ( ray ) {
  3398. return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction );
  3399. },
  3400. clone: function () {
  3401. return new THREE.Ray().copy( this );
  3402. }
  3403. };
  3404. // File:src/math/Sphere.js
  3405. /**
  3406. * @author bhouston / http://exocortex.com
  3407. * @author mrdoob / http://mrdoob.com/
  3408. */
  3409. THREE.Sphere = function ( center, radius ) {
  3410. this.center = ( center !== undefined ) ? center : new THREE.Vector3();
  3411. this.radius = ( radius !== undefined ) ? radius : 0;
  3412. };
  3413. THREE.Sphere.prototype = {
  3414. constructor: THREE.Sphere,
  3415. set: function ( center, radius ) {
  3416. this.center.copy( center );
  3417. this.radius = radius;
  3418. return this;
  3419. },
  3420. setFromPoints: function () {
  3421. var box = new THREE.Box3();
  3422. return function ( points, optionalCenter ) {
  3423. var center = this.center;
  3424. if ( optionalCenter !== undefined ) {
  3425. center.copy( optionalCenter );
  3426. } else {
  3427. box.setFromPoints( points ).center( center );
  3428. }
  3429. var maxRadiusSq = 0;
  3430. for ( var i = 0, il = points.length; i < il; i ++ ) {
  3431. maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) );
  3432. }
  3433. this.radius = Math.sqrt( maxRadiusSq );
  3434. return this;
  3435. };
  3436. }(),
  3437. copy: function ( sphere ) {
  3438. this.center.copy( sphere.center );
  3439. this.radius = sphere.radius;
  3440. return this;
  3441. },
  3442. empty: function () {
  3443. return ( this.radius <= 0 );
  3444. },
  3445. containsPoint: function ( point ) {
  3446. return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) );
  3447. },
  3448. distanceToPoint: function ( point ) {
  3449. return ( point.distanceTo( this.center ) - this.radius );
  3450. },
  3451. intersectsSphere: function ( sphere ) {
  3452. var radiusSum = this.radius + sphere.radius;
  3453. return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum );
  3454. },
  3455. clampPoint: function ( point, optionalTarget ) {
  3456. var deltaLengthSq = this.center.distanceToSquared( point );
  3457. var result = optionalTarget || new THREE.Vector3();
  3458. result.copy( point );
  3459. if ( deltaLengthSq > ( this.radius * this.radius ) ) {
  3460. result.sub( this.center ).normalize();
  3461. result.multiplyScalar( this.radius ).add( this.center );
  3462. }
  3463. return result;
  3464. },
  3465. getBoundingBox: function ( optionalTarget ) {
  3466. var box = optionalTarget || new THREE.Box3();
  3467. box.set( this.center, this.center );
  3468. box.expandByScalar( this.radius );
  3469. return box;
  3470. },
  3471. applyMatrix4: function ( matrix ) {
  3472. this.center.applyMatrix4( matrix );
  3473. this.radius = this.radius * matrix.getMaxScaleOnAxis();
  3474. return this;
  3475. },
  3476. translate: function ( offset ) {
  3477. this.center.add( offset );
  3478. return this;
  3479. },
  3480. equals: function ( sphere ) {
  3481. return sphere.center.equals( this.center ) && ( sphere.radius === this.radius );
  3482. },
  3483. clone: function () {
  3484. return new THREE.Sphere().copy( this );
  3485. }
  3486. };
  3487. // File:src/math/Frustum.js
  3488. /**
  3489. * @author mrdoob / http://mrdoob.com/
  3490. * @author alteredq / http://alteredqualia.com/
  3491. * @author bhouston / http://exocortex.com
  3492. */
  3493. THREE.Frustum = function ( p0, p1, p2, p3, p4, p5 ) {
  3494. this.planes = [
  3495. ( p0 !== undefined ) ? p0 : new THREE.Plane(),
  3496. ( p1 !== undefined ) ? p1 : new THREE.Plane(),
  3497. ( p2 !== undefined ) ? p2 : new THREE.Plane(),
  3498. ( p3 !== undefined ) ? p3 : new THREE.Plane(),
  3499. ( p4 !== undefined ) ? p4 : new THREE.Plane(),
  3500. ( p5 !== undefined ) ? p5 : new THREE.Plane()
  3501. ];
  3502. };
  3503. THREE.Frustum.prototype = {
  3504. constructor: THREE.Frustum,
  3505. set: function ( p0, p1, p2, p3, p4, p5 ) {
  3506. var planes = this.planes;
  3507. planes[ 0 ].copy( p0 );
  3508. planes[ 1 ].copy( p1 );
  3509. planes[ 2 ].copy( p2 );
  3510. planes[ 3 ].copy( p3 );
  3511. planes[ 4 ].copy( p4 );
  3512. planes[ 5 ].copy( p5 );
  3513. return this;
  3514. },
  3515. copy: function ( frustum ) {
  3516. var planes = this.planes;
  3517. for ( var i = 0; i < 6; i ++ ) {
  3518. planes[ i ].copy( frustum.planes[ i ] );
  3519. }
  3520. return this;
  3521. },
  3522. setFromMatrix: function ( m ) {
  3523. var planes = this.planes;
  3524. var me = m.elements;
  3525. var me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ];
  3526. var me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ];
  3527. var me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ];
  3528. var me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ];
  3529. planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize();
  3530. planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize();
  3531. planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize();
  3532. planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize();
  3533. planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize();
  3534. planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize();
  3535. return this;
  3536. },
  3537. intersectsObject: function () {
  3538. var sphere = new THREE.Sphere();
  3539. return function ( object ) {
  3540. var geometry = object.geometry;
  3541. if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere();
  3542. sphere.copy( geometry.boundingSphere );
  3543. sphere.applyMatrix4( object.matrixWorld );
  3544. return this.intersectsSphere( sphere );
  3545. };
  3546. }(),
  3547. intersectsSphere: function ( sphere ) {
  3548. var planes = this.planes;
  3549. var center = sphere.center;
  3550. var negRadius = - sphere.radius;
  3551. for ( var i = 0; i < 6; i ++ ) {
  3552. var distance = planes[ i ].distanceToPoint( center );
  3553. if ( distance < negRadius ) {
  3554. return false;
  3555. }
  3556. }
  3557. return true;
  3558. },
  3559. intersectsBox: function () {
  3560. var p1 = new THREE.Vector3(),
  3561. p2 = new THREE.Vector3();
  3562. return function ( box ) {
  3563. var planes = this.planes;
  3564. for ( var i = 0; i < 6 ; i ++ ) {
  3565. var plane = planes[ i ];
  3566. p1.x = plane.normal.x > 0 ? box.min.x : box.max.x;
  3567. p2.x = plane.normal.x > 0 ? box.max.x : box.min.x;
  3568. p1.y = plane.normal.y > 0 ? box.min.y : box.max.y;
  3569. p2.y = plane.normal.y > 0 ? box.max.y : box.min.y;
  3570. p1.z = plane.normal.z > 0 ? box.min.z : box.max.z;
  3571. p2.z = plane.normal.z > 0 ? box.max.z : box.min.z;
  3572. var d1 = plane.distanceToPoint( p1 );
  3573. var d2 = plane.distanceToPoint( p2 );
  3574. // if both outside plane, no intersection
  3575. if ( d1 < 0 && d2 < 0 ) {
  3576. return false;
  3577. }
  3578. }
  3579. return true;
  3580. };
  3581. }(),
  3582. containsPoint: function ( point ) {
  3583. var planes = this.planes;
  3584. for ( var i = 0; i < 6; i ++ ) {
  3585. if ( planes[ i ].distanceToPoint( point ) < 0 ) {
  3586. return false;
  3587. }
  3588. }
  3589. return true;
  3590. },
  3591. clone: function () {
  3592. return new THREE.Frustum().copy( this );
  3593. }
  3594. };
  3595. // File:src/math/Plane.js
  3596. /**
  3597. * @author bhouston / http://exocortex.com
  3598. */
  3599. THREE.Plane = function ( normal, constant ) {
  3600. this.normal = ( normal !== undefined ) ? normal : new THREE.Vector3( 1, 0, 0 );
  3601. this.constant = ( constant !== undefined ) ? constant : 0;
  3602. };
  3603. THREE.Plane.prototype = {
  3604. constructor: THREE.Plane,
  3605. set: function ( normal, constant ) {
  3606. this.normal.copy( normal );
  3607. this.constant = constant;
  3608. return this;
  3609. },
  3610. setComponents: function ( x, y, z, w ) {
  3611. this.normal.set( x, y, z );
  3612. this.constant = w;
  3613. return this;
  3614. },
  3615. setFromNormalAndCoplanarPoint: function ( normal, point ) {
  3616. this.normal.copy( normal );
  3617. this.constant = - point.dot( this.normal ); // must be this.normal, not normal, as this.normal is normalized
  3618. return this;
  3619. },
  3620. setFromCoplanarPoints: function () {
  3621. var v1 = new THREE.Vector3();
  3622. var v2 = new THREE.Vector3();
  3623. return function ( a, b, c ) {
  3624. var normal = v1.subVectors( c, b ).cross( v2.subVectors( a, b ) ).normalize();
  3625. // Q: should an error be thrown if normal is zero (e.g. degenerate plane)?
  3626. this.setFromNormalAndCoplanarPoint( normal, a );
  3627. return this;
  3628. };
  3629. }(),
  3630. copy: function ( plane ) {
  3631. this.normal.copy( plane.normal );
  3632. this.constant = plane.constant;
  3633. return this;
  3634. },
  3635. normalize: function () {
  3636. // Note: will lead to a divide by zero if the plane is invalid.
  3637. var inverseNormalLength = 1.0 / this.normal.length();
  3638. this.normal.multiplyScalar( inverseNormalLength );
  3639. this.constant *= inverseNormalLength;
  3640. return this;
  3641. },
  3642. negate: function () {
  3643. this.constant *= - 1;
  3644. this.normal.negate();
  3645. return this;
  3646. },
  3647. distanceToPoint: function ( point ) {
  3648. return this.normal.dot( point ) + this.constant;
  3649. },
  3650. distanceToSphere: function ( sphere ) {
  3651. return this.distanceToPoint( sphere.center ) - sphere.radius;
  3652. },
  3653. projectPoint: function ( point, optionalTarget ) {
  3654. return this.orthoPoint( point, optionalTarget ).sub( point ).negate();
  3655. },
  3656. orthoPoint: function ( point, optionalTarget ) {
  3657. var perpendicularMagnitude = this.distanceToPoint( point );
  3658. var result = optionalTarget || new THREE.Vector3();
  3659. return result.copy( this.normal ).multiplyScalar( perpendicularMagnitude );
  3660. },
  3661. isIntersectionLine: function ( line ) {
  3662. // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it.
  3663. var startSign = this.distanceToPoint( line.start );
  3664. var endSign = this.distanceToPoint( line.end );
  3665. return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 );
  3666. },
  3667. intersectLine: function () {
  3668. var v1 = new THREE.Vector3();
  3669. return function ( line, optionalTarget ) {
  3670. var result = optionalTarget || new THREE.Vector3();
  3671. var direction = line.delta( v1 );
  3672. var denominator = this.normal.dot( direction );
  3673. if ( denominator == 0 ) {
  3674. // line is coplanar, return origin
  3675. if ( this.distanceToPoint( line.start ) == 0 ) {
  3676. return result.copy( line.start );
  3677. }
  3678. // Unsure if this is the correct method to handle this case.
  3679. return undefined;
  3680. }
  3681. var t = - ( line.start.dot( this.normal ) + this.constant ) / denominator;
  3682. if ( t < 0 || t > 1 ) {
  3683. return undefined;
  3684. }
  3685. return result.copy( direction ).multiplyScalar( t ).add( line.start );
  3686. };
  3687. }(),
  3688. coplanarPoint: function ( optionalTarget ) {
  3689. var result = optionalTarget || new THREE.Vector3();
  3690. return result.copy( this.normal ).multiplyScalar( - this.constant );
  3691. },
  3692. applyMatrix4: function () {
  3693. var v1 = new THREE.Vector3();
  3694. var v2 = new THREE.Vector3();
  3695. var m1 = new THREE.Matrix3();
  3696. return function ( matrix, optionalNormalMatrix ) {
  3697. // compute new normal based on theory here:
  3698. // http://www.songho.ca/opengl/gl_normaltransform.html
  3699. var normalMatrix = optionalNormalMatrix || m1.getNormalMatrix( matrix );
  3700. var newNormal = v1.copy( this.normal ).applyMatrix3( normalMatrix );
  3701. var newCoplanarPoint = this.coplanarPoint( v2 );
  3702. newCoplanarPoint.applyMatrix4( matrix );
  3703. this.setFromNormalAndCoplanarPoint( newNormal, newCoplanarPoint );
  3704. return this;
  3705. };
  3706. }(),
  3707. translate: function ( offset ) {
  3708. this.constant = this.constant - offset.dot( this.normal );
  3709. return this;
  3710. },
  3711. equals: function ( plane ) {
  3712. return plane.normal.equals( this.normal ) && ( plane.constant == this.constant );
  3713. },
  3714. clone: function () {
  3715. return new THREE.Plane().copy( this );
  3716. }
  3717. };
  3718. // File:src/math/Math.js
  3719. /**
  3720. * @author alteredq / http://alteredqualia.com/
  3721. * @author mrdoob / http://mrdoob.com/
  3722. */
  3723. THREE.Math = {
  3724. generateUUID: function () {
  3725. // http://www.broofa.com/Tools/Math.uuid.htm
  3726. var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.split( '' );
  3727. var uuid = new Array( 36 );
  3728. var rnd = 0, r;
  3729. return function () {
  3730. for ( var i = 0; i < 36; i ++ ) {
  3731. if ( i == 8 || i == 13 || i == 18 || i == 23 ) {
  3732. uuid[ i ] = '-';
  3733. } else if ( i == 14 ) {
  3734. uuid[ i ] = '4';
  3735. } else {
  3736. if ( rnd <= 0x02 ) rnd = 0x2000000 + ( Math.random() * 0x1000000 ) | 0;
  3737. r = rnd & 0xf;
  3738. rnd = rnd >> 4;
  3739. uuid[ i ] = chars[ ( i == 19 ) ? ( r & 0x3 ) | 0x8 : r ];
  3740. }
  3741. }
  3742. return uuid.join( '' );
  3743. };
  3744. }(),
  3745. // Clamp value to range <a, b>
  3746. clamp: function ( x, a, b ) {
  3747. return ( x < a ) ? a : ( ( x > b ) ? b : x );
  3748. },
  3749. // Clamp value to range <a, inf)
  3750. clampBottom: function ( x, a ) {
  3751. return x < a ? a : x;
  3752. },
  3753. // Linear mapping from range <a1, a2> to range <b1, b2>
  3754. mapLinear: function ( x, a1, a2, b1, b2 ) {
  3755. return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 );
  3756. },
  3757. // http://en.wikipedia.org/wiki/Smoothstep
  3758. smoothstep: function ( x, min, max ) {
  3759. if ( x <= min ) return 0;
  3760. if ( x >= max ) return 1;
  3761. x = ( x - min ) / ( max - min );
  3762. return x * x * ( 3 - 2 * x );
  3763. },
  3764. smootherstep: function ( x, min, max ) {
  3765. if ( x <= min ) return 0;
  3766. if ( x >= max ) return 1;
  3767. x = ( x - min ) / ( max - min );
  3768. return x * x * x * ( x * ( x * 6 - 15 ) + 10 );
  3769. },
  3770. // Random float from <0, 1> with 16 bits of randomness
  3771. // (standard Math.random() creates repetitive patterns when applied over larger space)
  3772. random16: function () {
  3773. return ( 65280 * Math.random() + 255 * Math.random() ) / 65535;
  3774. },
  3775. // Random integer from <low, high> interval
  3776. randInt: function ( low, high ) {
  3777. return low + Math.floor( Math.random() * ( high - low + 1 ) );
  3778. },
  3779. // Random float from <low, high> interval
  3780. randFloat: function ( low, high ) {
  3781. return low + Math.random() * ( high - low );
  3782. },
  3783. // Random float from <-range/2, range/2> interval
  3784. randFloatSpread: function ( range ) {
  3785. return range * ( 0.5 - Math.random() );
  3786. },
  3787. sign: function ( x ) {
  3788. return ( x < 0 ) ? - 1 : ( x > 0 ) ? 1 : 0;
  3789. },
  3790. degToRad: function () {
  3791. var degreeToRadiansFactor = Math.PI / 180;
  3792. return function ( degrees ) {
  3793. return degrees * degreeToRadiansFactor;
  3794. };
  3795. }(),
  3796. radToDeg: function () {
  3797. var radianToDegreesFactor = 180 / Math.PI;
  3798. return function ( radians ) {
  3799. return radians * radianToDegreesFactor;
  3800. };
  3801. }(),
  3802. isPowerOfTwo: function ( value ) {
  3803. return ( value & ( value - 1 ) ) === 0 && value !== 0;
  3804. }
  3805. };
  3806. // File:src/math/Spline.js
  3807. /**
  3808. * Spline from Tween.js, slightly optimized (and trashed)
  3809. * http://sole.github.com/tween.js/examples/05_spline.html
  3810. *
  3811. * @author mrdoob / http://mrdoob.com/
  3812. * @author alteredq / http://alteredqualia.com/
  3813. */
  3814. THREE.Spline = function ( points ) {
  3815. this.points = points;
  3816. var c = [], v3 = { x: 0, y: 0, z: 0 },
  3817. point, intPoint, weight, w2, w3,
  3818. pa, pb, pc, pd;
  3819. this.initFromArray = function ( a ) {
  3820. this.points = [];
  3821. for ( var i = 0; i < a.length; i ++ ) {
  3822. this.points[ i ] = { x: a[ i ][ 0 ], y: a[ i ][ 1 ], z: a[ i ][ 2 ] };
  3823. }
  3824. };
  3825. this.getPoint = function ( k ) {
  3826. point = ( this.points.length - 1 ) * k;
  3827. intPoint = Math.floor( point );
  3828. weight = point - intPoint;
  3829. c[ 0 ] = intPoint === 0 ? intPoint : intPoint - 1;
  3830. c[ 1 ] = intPoint;
  3831. c[ 2 ] = intPoint > this.points.length - 2 ? this.points.length - 1 : intPoint + 1;
  3832. c[ 3 ] = intPoint > this.points.length - 3 ? this.points.length - 1 : intPoint + 2;
  3833. pa = this.points[ c[ 0 ] ];
  3834. pb = this.points[ c[ 1 ] ];
  3835. pc = this.points[ c[ 2 ] ];
  3836. pd = this.points[ c[ 3 ] ];
  3837. w2 = weight * weight;
  3838. w3 = weight * w2;
  3839. v3.x = interpolate( pa.x, pb.x, pc.x, pd.x, weight, w2, w3 );
  3840. v3.y = interpolate( pa.y, pb.y, pc.y, pd.y, weight, w2, w3 );
  3841. v3.z = interpolate( pa.z, pb.z, pc.z, pd.z, weight, w2, w3 );
  3842. return v3;
  3843. };
  3844. this.getControlPointsArray = function () {
  3845. var i, p, l = this.points.length,
  3846. coords = [];
  3847. for ( i = 0; i < l; i ++ ) {
  3848. p = this.points[ i ];
  3849. coords[ i ] = [ p.x, p.y, p.z ];
  3850. }
  3851. return coords;
  3852. };
  3853. // approximate length by summing linear segments
  3854. this.getLength = function ( nSubDivisions ) {
  3855. var i, index, nSamples, position,
  3856. point = 0, intPoint = 0, oldIntPoint = 0,
  3857. oldPosition = new THREE.Vector3(),
  3858. tmpVec = new THREE.Vector3(),
  3859. chunkLengths = [],
  3860. totalLength = 0;
  3861. // first point has 0 length
  3862. chunkLengths[ 0 ] = 0;
  3863. if ( ! nSubDivisions ) nSubDivisions = 100;
  3864. nSamples = this.points.length * nSubDivisions;
  3865. oldPosition.copy( this.points[ 0 ] );
  3866. for ( i = 1; i < nSamples; i ++ ) {
  3867. index = i / nSamples;
  3868. position = this.getPoint( index );
  3869. tmpVec.copy( position );
  3870. totalLength += tmpVec.distanceTo( oldPosition );
  3871. oldPosition.copy( position );
  3872. point = ( this.points.length - 1 ) * index;
  3873. intPoint = Math.floor( point );
  3874. if ( intPoint != oldIntPoint ) {
  3875. chunkLengths[ intPoint ] = totalLength;
  3876. oldIntPoint = intPoint;
  3877. }
  3878. }
  3879. // last point ends with total length
  3880. chunkLengths[ chunkLengths.length ] = totalLength;
  3881. return { chunks: chunkLengths, total: totalLength };
  3882. };
  3883. this.reparametrizeByArcLength = function ( samplingCoef ) {
  3884. var i, j,
  3885. index, indexCurrent, indexNext,
  3886. linearDistance, realDistance,
  3887. sampling, position,
  3888. newpoints = [],
  3889. tmpVec = new THREE.Vector3(),
  3890. sl = this.getLength();
  3891. newpoints.push( tmpVec.copy( this.points[ 0 ] ).clone() );
  3892. for ( i = 1; i < this.points.length; i ++ ) {
  3893. //tmpVec.copy( this.points[ i - 1 ] );
  3894. //linearDistance = tmpVec.distanceTo( this.points[ i ] );
  3895. realDistance = sl.chunks[ i ] - sl.chunks[ i - 1 ];
  3896. sampling = Math.ceil( samplingCoef * realDistance / sl.total );
  3897. indexCurrent = ( i - 1 ) / ( this.points.length - 1 );
  3898. indexNext = i / ( this.points.length - 1 );
  3899. for ( j = 1; j < sampling - 1; j ++ ) {
  3900. index = indexCurrent + j * ( 1 / sampling ) * ( indexNext - indexCurrent );
  3901. position = this.getPoint( index );
  3902. newpoints.push( tmpVec.copy( position ).clone() );
  3903. }
  3904. newpoints.push( tmpVec.copy( this.points[ i ] ).clone() );
  3905. }
  3906. this.points = newpoints;
  3907. };
  3908. // Catmull-Rom
  3909. function interpolate( p0, p1, p2, p3, t, t2, t3 ) {
  3910. var v0 = ( p2 - p0 ) * 0.5,
  3911. v1 = ( p3 - p1 ) * 0.5;
  3912. return ( 2 * ( p1 - p2 ) + v0 + v1 ) * t3 + ( - 3 * ( p1 - p2 ) - 2 * v0 - v1 ) * t2 + v0 * t + p1;
  3913. };
  3914. };
  3915. // File:src/math/Triangle.js
  3916. /**
  3917. * @author bhouston / http://exocortex.com
  3918. * @author mrdoob / http://mrdoob.com/
  3919. */
  3920. THREE.Triangle = function ( a, b, c ) {
  3921. this.a = ( a !== undefined ) ? a : new THREE.Vector3();
  3922. this.b = ( b !== undefined ) ? b : new THREE.Vector3();
  3923. this.c = ( c !== undefined ) ? c : new THREE.Vector3();
  3924. };
  3925. THREE.Triangle.normal = function () {
  3926. var v0 = new THREE.Vector3();
  3927. return function ( a, b, c, optionalTarget ) {
  3928. var result = optionalTarget || new THREE.Vector3();
  3929. result.subVectors( c, b );
  3930. v0.subVectors( a, b );
  3931. result.cross( v0 );
  3932. var resultLengthSq = result.lengthSq();
  3933. if ( resultLengthSq > 0 ) {
  3934. return result.multiplyScalar( 1 / Math.sqrt( resultLengthSq ) );
  3935. }
  3936. return result.set( 0, 0, 0 );
  3937. };
  3938. }();
  3939. // static/instance method to calculate barycoordinates
  3940. // based on: http://www.blackpawn.com/texts/pointinpoly/default.html
  3941. THREE.Triangle.barycoordFromPoint = function () {
  3942. var v0 = new THREE.Vector3();
  3943. var v1 = new THREE.Vector3();
  3944. var v2 = new THREE.Vector3();
  3945. return function ( point, a, b, c, optionalTarget ) {
  3946. v0.subVectors( c, a );
  3947. v1.subVectors( b, a );
  3948. v2.subVectors( point, a );
  3949. var dot00 = v0.dot( v0 );
  3950. var dot01 = v0.dot( v1 );
  3951. var dot02 = v0.dot( v2 );
  3952. var dot11 = v1.dot( v1 );
  3953. var dot12 = v1.dot( v2 );
  3954. var denom = ( dot00 * dot11 - dot01 * dot01 );
  3955. var result = optionalTarget || new THREE.Vector3();
  3956. // colinear or singular triangle
  3957. if ( denom == 0 ) {
  3958. // arbitrary location outside of triangle?
  3959. // not sure if this is the best idea, maybe should be returning undefined
  3960. return result.set( - 2, - 1, - 1 );
  3961. }
  3962. var invDenom = 1 / denom;
  3963. var u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom;
  3964. var v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom;
  3965. // barycoordinates must always sum to 1
  3966. return result.set( 1 - u - v, v, u );
  3967. };
  3968. }();
  3969. THREE.Triangle.containsPoint = function () {
  3970. var v1 = new THREE.Vector3();
  3971. return function ( point, a, b, c ) {
  3972. var result = THREE.Triangle.barycoordFromPoint( point, a, b, c, v1 );
  3973. return ( result.x >= 0 ) && ( result.y >= 0 ) && ( ( result.x + result.y ) <= 1 );
  3974. };
  3975. }();
  3976. THREE.Triangle.prototype = {
  3977. constructor: THREE.Triangle,
  3978. set: function ( a, b, c ) {
  3979. this.a.copy( a );
  3980. this.b.copy( b );
  3981. this.c.copy( c );
  3982. return this;
  3983. },
  3984. setFromPointsAndIndices: function ( points, i0, i1, i2 ) {
  3985. this.a.copy( points[ i0 ] );
  3986. this.b.copy( points[ i1 ] );
  3987. this.c.copy( points[ i2 ] );
  3988. return this;
  3989. },
  3990. copy: function ( triangle ) {
  3991. this.a.copy( triangle.a );
  3992. this.b.copy( triangle.b );
  3993. this.c.copy( triangle.c );
  3994. return this;
  3995. },
  3996. area: function () {
  3997. var v0 = new THREE.Vector3();
  3998. var v1 = new THREE.Vector3();
  3999. return function () {
  4000. v0.subVectors( this.c, this.b );
  4001. v1.subVectors( this.a, this.b );
  4002. return v0.cross( v1 ).length() * 0.5;
  4003. };
  4004. }(),
  4005. midpoint: function ( optionalTarget ) {
  4006. var result = optionalTarget || new THREE.Vector3();
  4007. return result.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 );
  4008. },
  4009. normal: function ( optionalTarget ) {
  4010. return THREE.Triangle.normal( this.a, this.b, this.c, optionalTarget );
  4011. },
  4012. plane: function ( optionalTarget ) {
  4013. var result = optionalTarget || new THREE.Plane();
  4014. return result.setFromCoplanarPoints( this.a, this.b, this.c );
  4015. },
  4016. barycoordFromPoint: function ( point, optionalTarget ) {
  4017. return THREE.Triangle.barycoordFromPoint( point, this.a, this.b, this.c, optionalTarget );
  4018. },
  4019. containsPoint: function ( point ) {
  4020. return THREE.Triangle.containsPoint( point, this.a, this.b, this.c );
  4021. },
  4022. equals: function ( triangle ) {
  4023. return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c );
  4024. },
  4025. clone: function () {
  4026. return new THREE.Triangle().copy( this );
  4027. }
  4028. };