ThreeSVG.js 139 KB

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