1
0

ThreeSVG.js 138 KB

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