ThreeCanvas.js 158 KB

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